blob: 5d72097b0adafddaedd7b7f06987262502ca1095 [file] [log] [blame]
Colin Cross5049f022015-03-18 13:28:46 -07001// Copyright 2015 Google Inc. All rights reserved.
Colin Cross3f40fa42015-01-30 17:27:36 -08002//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package cc
16
17// This file contains the module types for compiling C/C++ for Android, and converts the properties
18// into the flags and filenames necessary to pass to the compiler. The final creation of the rules
19// is handled in builder.go
20
21import (
Colin Cross3f40fa42015-01-30 17:27:36 -080022 "fmt"
23 "path/filepath"
Colin Cross0af4b842015-04-30 16:36:18 -070024 "runtime"
Colin Cross3f40fa42015-01-30 17:27:36 -080025 "strings"
26
Colin Cross97ba0732015-03-23 17:50:24 -070027 "github.com/google/blueprint"
28 "github.com/google/blueprint/pathtools"
29
Colin Cross3f40fa42015-01-30 17:27:36 -080030 "android/soong/common"
Colin Cross5049f022015-03-18 13:28:46 -070031 "android/soong/genrule"
Colin Cross3f40fa42015-01-30 17:27:36 -080032)
33
Colin Cross3f40fa42015-01-30 17:27:36 -080034var (
Colin Cross1332b002015-04-07 17:11:30 -070035 HostPrebuiltTag = pctx.VariableConfigMethod("HostPrebuiltTag", common.Config.PrebuiltOS)
36 SrcDir = pctx.VariableConfigMethod("SrcDir", common.Config.SrcDir)
Colin Cross3f40fa42015-01-30 17:27:36 -080037
38 LibcRoot = pctx.StaticVariable("LibcRoot", "${SrcDir}/bionic/libc")
39 LibmRoot = pctx.StaticVariable("LibmRoot", "${SrcDir}/bionic/libm")
40)
41
42// Flags used by lots of devices. Putting them in package static variables will save bytes in
43// build.ninja so they aren't repeated for every file
44var (
45 commonGlobalCflags = []string{
46 "-DANDROID",
47 "-fmessage-length=0",
48 "-W",
49 "-Wall",
50 "-Wno-unused",
51 "-Winit-self",
52 "-Wpointer-arith",
53
54 // COMMON_RELEASE_CFLAGS
55 "-DNDEBUG",
56 "-UDEBUG",
57 }
58
59 deviceGlobalCflags = []string{
60 // TARGET_ERROR_FLAGS
61 "-Werror=return-type",
62 "-Werror=non-virtual-dtor",
63 "-Werror=address",
64 "-Werror=sequence-point",
65 }
66
67 hostGlobalCflags = []string{}
68
69 commonGlobalCppflags = []string{
70 "-Wsign-promo",
71 "-std=gnu++11",
72 }
73)
74
75func init() {
76 pctx.StaticVariable("commonGlobalCflags", strings.Join(commonGlobalCflags, " "))
77 pctx.StaticVariable("deviceGlobalCflags", strings.Join(deviceGlobalCflags, " "))
78 pctx.StaticVariable("hostGlobalCflags", strings.Join(hostGlobalCflags, " "))
79
80 pctx.StaticVariable("commonGlobalCppflags", strings.Join(commonGlobalCppflags, " "))
81
82 pctx.StaticVariable("commonClangGlobalCflags",
83 strings.Join(clangFilterUnknownCflags(commonGlobalCflags), " "))
84 pctx.StaticVariable("deviceClangGlobalCflags",
85 strings.Join(clangFilterUnknownCflags(deviceGlobalCflags), " "))
86 pctx.StaticVariable("hostClangGlobalCflags",
87 strings.Join(clangFilterUnknownCflags(hostGlobalCflags), " "))
Tim Kilbournf2948142015-03-11 12:03:03 -070088 pctx.StaticVariable("commonClangGlobalCppflags",
89 strings.Join(clangFilterUnknownCflags(commonGlobalCppflags), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -080090
91 // Everything in this list is a crime against abstraction and dependency tracking.
92 // Do not add anything to this list.
93 pctx.StaticVariable("commonGlobalIncludes", strings.Join([]string{
94 "-isystem ${SrcDir}/system/core/include",
95 "-isystem ${SrcDir}/hardware/libhardware/include",
96 "-isystem ${SrcDir}/hardware/libhardware_legacy/include",
97 "-isystem ${SrcDir}/hardware/ril/include",
98 "-isystem ${SrcDir}/libnativehelper/include",
99 "-isystem ${SrcDir}/frameworks/native/include",
100 "-isystem ${SrcDir}/frameworks/native/opengl/include",
101 "-isystem ${SrcDir}/frameworks/av/include",
102 "-isystem ${SrcDir}/frameworks/base/include",
103 }, " "))
104
105 pctx.StaticVariable("clangPath", "${SrcDir}/prebuilts/clang/${HostPrebuiltTag}/host/3.6/bin/")
106}
107
Colin Cross3f40fa42015-01-30 17:27:36 -0800108// Building C/C++ code is handled by objects that satisfy this interface via composition
Colin Cross97ba0732015-03-23 17:50:24 -0700109type CCModuleType interface {
Colin Cross3f40fa42015-01-30 17:27:36 -0800110 common.AndroidModule
111
Colin Crossfa138792015-04-24 17:31:52 -0700112 // Modify property values after parsing Blueprints file but before starting dependency
113 // resolution or build rule generation
114 ModifyProperties(common.AndroidBaseContext)
115
Colin Cross21b9a242015-03-24 14:15:58 -0700116 // Modify the ccFlags
Colin Cross0676e2d2015-04-24 17:39:18 -0700117 flags(common.AndroidModuleContext, CCFlags) CCFlags
Colin Cross3f40fa42015-01-30 17:27:36 -0800118
Colin Cross21b9a242015-03-24 14:15:58 -0700119 // Return list of dependency names for use in AndroidDynamicDependencies and in depsToPaths
Colin Cross0676e2d2015-04-24 17:39:18 -0700120 depNames(common.AndroidBaseContext, CCDeps) CCDeps
Colin Cross3f40fa42015-01-30 17:27:36 -0800121
122 // Compile objects into final module
Colin Cross97ba0732015-03-23 17:50:24 -0700123 compileModule(common.AndroidModuleContext, CCFlags, CCDeps, []string)
Colin Cross3f40fa42015-01-30 17:27:36 -0800124
Dan Albertc403f7c2015-03-18 14:01:18 -0700125 // Install the built module.
Colin Cross97ba0732015-03-23 17:50:24 -0700126 installModule(common.AndroidModuleContext, CCFlags)
Dan Albertc403f7c2015-03-18 14:01:18 -0700127
Colin Cross3f40fa42015-01-30 17:27:36 -0800128 // Return the output file (.o, .a or .so) for use by other modules
129 outputFile() string
130}
131
Colin Cross97ba0732015-03-23 17:50:24 -0700132type CCDeps struct {
Colin Cross28344522015-04-22 13:07:53 -0700133 StaticLibs, SharedLibs, LateStaticLibs, WholeStaticLibs, ObjFiles, Cflags []string
Colin Crossc472d572015-03-17 15:06:21 -0700134
Colin Cross21b9a242015-03-24 14:15:58 -0700135 WholeStaticLibObjFiles []string
136
Colin Cross97ba0732015-03-23 17:50:24 -0700137 CrtBegin, CrtEnd string
Colin Crossc472d572015-03-17 15:06:21 -0700138}
139
Colin Cross97ba0732015-03-23 17:50:24 -0700140type CCFlags struct {
Colin Cross28344522015-04-22 13:07:53 -0700141 GlobalFlags []string // Flags that apply to C, C++, and assembly source files
142 AsFlags []string // Flags that apply to assembly source files
143 CFlags []string // Flags that apply to C and C++ source files
144 ConlyFlags []string // Flags that apply to C source files
145 CppFlags []string // Flags that apply to C++ source files
146 YaccFlags []string // Flags that apply to Yacc source files
147 LdFlags []string // Flags that apply to linker command lines
148
149 Nocrt bool
150 Toolchain Toolchain
151 Clang bool
Colin Crossc472d572015-03-17 15:06:21 -0700152}
153
Colin Crossfa138792015-04-24 17:31:52 -0700154// CCBase contains the properties and members used by all C/C++ module types, and implements
Colin Crossc472d572015-03-17 15:06:21 -0700155// the blueprint.Module interface. It expects to be embedded into an outer specialization struct,
156// and uses a ccModuleType interface to that struct to create the build steps.
Colin Crossfa138792015-04-24 17:31:52 -0700157type CCBase struct {
Colin Crossc472d572015-03-17 15:06:21 -0700158 common.AndroidModuleBase
Colin Cross97ba0732015-03-23 17:50:24 -0700159 module CCModuleType
Colin Crossc472d572015-03-17 15:06:21 -0700160
Colin Crossfa138792015-04-24 17:31:52 -0700161 // Properties used to compile all C or C++ modules
162 Properties struct {
163 // srcs: list of source files used to compile the C/C++ module. May be .c, .cpp, or .S files.
164 Srcs []string `android:"arch_variant,arch_subtract"`
165
166 // cflags: list of module-specific flags that will be used for C and C++ compiles.
167 Cflags []string `android:"arch_variant"`
168
169 // cppflags: list of module-specific flags that will be used for C++ compiles
170 Cppflags []string `android:"arch_variant"`
171
172 // conlyflags: list of module-specific flags that will be used for C compiles
173 Conlyflags []string `android:"arch_variant"`
174
175 // asflags: list of module-specific flags that will be used for .S compiles
176 Asflags []string `android:"arch_variant"`
177
178 // yaccflags: list of module-specific flags that will be used for .y and .yy compiles
179 Yaccflags []string
180
181 // ldflags: list of module-specific flags that will be used for all link steps
182 Ldflags []string `android:"arch_variant"`
183
184 // instruction_set: the instruction set architecture to use to compile the C/C++
185 // module.
186 Instruction_set string `android:"arch_variant"`
187
188 // include_dirs: list of directories relative to the root of the source tree that will
189 // be added to the include path using -I.
190 // If possible, don't use this. If adding paths from the current directory use
191 // local_include_dirs, if adding paths from other modules use export_include_dirs in
192 // that module.
193 Include_dirs []string `android:"arch_variant"`
194
195 // local_include_dirs: list of directories relative to the Blueprints file that will
196 // be added to the include path using -I
197 Local_include_dirs []string `android:"arch_variant"`
198
199 // export_include_dirs: list of directories relative to the Blueprints file that will
200 // be added to the include path using -I for any module that links against this module
201 Export_include_dirs []string `android:"arch_variant"`
202
203 // clang_cflags: list of module-specific flags that will be used for C and C++ compiles when
204 // compiling with clang
205 Clang_cflags []string `android:"arch_variant"`
206
207 // clang_asflags: list of module-specific flags that will be used for .S compiles when
208 // compiling with clang
209 Clang_asflags []string `android:"arch_variant"`
210
211 // system_shared_libs: list of system libraries that will be dynamically linked to
212 // shared library and executable modules. If unset, generally defaults to libc
213 // and libm. Set to [] to prevent linking against libc and libm.
214 System_shared_libs []string
215
216 // whole_static_libs: list of modules whose object files should be linked into this module
217 // in their entirety. For static library modules, all of the .o files from the intermediate
218 // directory of the dependency will be linked into this modules .a file. For a shared library,
219 // the dependency's .a file will be linked into this module using -Wl,--whole-archive.
220 Whole_static_libs []string `android:"arch_variant"`
221
222 // static_libs: list of modules that should be statically linked into this module.
223 Static_libs []string `android:"arch_variant"`
224
225 // shared_libs: list of modules that should be dynamically linked into this module.
226 Shared_libs []string `android:"arch_variant"`
227
228 // allow_undefined_symbols: allow the module to contain undefined symbols. By default,
229 // modules cannot contain undefined symbols that are not satisified by their immediate
230 // dependencies. Set this flag to true to remove --no-undefined from the linker flags.
231 // This flag should only be necessary for compiling low-level libraries like libc.
232 Allow_undefined_symbols bool
233
234 // nocrt: don't link in crt_begin and crt_end. This flag should only be necessary for
235 // compiling crt or libc.
236 Nocrt bool `android:"arch_variant"`
237
238 // no_default_compiler_flags: don't insert default compiler flags into asflags, cflags,
239 // cppflags, conlyflags, ldflags, or include_dirs
240 No_default_compiler_flags bool
241
242 // clang: compile module with clang instead of gcc
243 Clang bool `android:"arch_variant"`
244
245 // rtti: pass -frtti instead of -fno-rtti
246 Rtti bool
247
248 // host_ldlibs: -l arguments to pass to linker for host-provided shared libraries
249 Host_ldlibs []string `android:"arch_variant"`
250
251 // stl: select the STL library to use. Possible values are "libc++", "libc++_static",
252 // "stlport", "stlport_static", "ndk", "libstdc++", or "none". Leave blank to select the
253 // default
254 Stl string
255
256 // Set for combined shared/static libraries to prevent compiling object files a second time
257 SkipCompileObjs bool `blueprint:"mutated"`
258
259 Debug struct {
260 Cflags []string `android:"arch_variant"`
261 } `android:"arch_variant"`
262 Release struct {
263 Cflags []string `android:"arch_variant"`
264 } `android:"arch_variant"`
265
266 // Minimum sdk version supported when compiling against the ndk
267 Sdk_version string
268
269 // relative_install_path: install to a subdirectory of the default install path for the module
270 Relative_install_path string
271 }
272
273 unused struct {
274 Asan bool
275 Native_coverage bool
276 Strip string
277 Tags []string
278 Required []string
279 }
Colin Crossc472d572015-03-17 15:06:21 -0700280
281 installPath string
Colin Cross74d1ec02015-04-28 13:30:13 -0700282
283 savedDepNames CCDeps
Colin Crossc472d572015-03-17 15:06:21 -0700284}
285
Colin Crossfa138792015-04-24 17:31:52 -0700286func newCCBase(base *CCBase, module CCModuleType, hod common.HostOrDeviceSupported,
Colin Crossc472d572015-03-17 15:06:21 -0700287 multilib common.Multilib, props ...interface{}) (blueprint.Module, []interface{}) {
288
289 base.module = module
290
Colin Crossfa138792015-04-24 17:31:52 -0700291 props = append(props, &base.Properties, &base.unused)
Colin Crossc472d572015-03-17 15:06:21 -0700292
Colin Cross5049f022015-03-18 13:28:46 -0700293 return common.InitAndroidArchModule(module, hod, multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700294}
295
Colin Crossfa138792015-04-24 17:31:52 -0700296func (c *CCBase) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800297 toolchain := c.findToolchain(ctx)
298 if ctx.Failed() {
299 return
300 }
301
Colin Cross21b9a242015-03-24 14:15:58 -0700302 flags := c.collectFlags(ctx, toolchain)
Colin Cross3f40fa42015-01-30 17:27:36 -0800303 if ctx.Failed() {
304 return
305 }
306
Colin Cross74d1ec02015-04-28 13:30:13 -0700307 deps := c.depsToPaths(ctx, c.savedDepNames)
Colin Cross3f40fa42015-01-30 17:27:36 -0800308 if ctx.Failed() {
309 return
310 }
311
Colin Cross28344522015-04-22 13:07:53 -0700312 flags.CFlags = append(flags.CFlags, deps.Cflags...)
Colin Crossed9f8682015-03-18 17:17:35 -0700313
Colin Cross581c1892015-04-07 16:50:10 -0700314 objFiles := c.compileObjs(ctx, flags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800315 if ctx.Failed() {
316 return
317 }
318
Colin Cross581c1892015-04-07 16:50:10 -0700319 generatedObjFiles := c.compileGeneratedObjs(ctx, flags)
Colin Cross5049f022015-03-18 13:28:46 -0700320 if ctx.Failed() {
321 return
322 }
323
324 objFiles = append(objFiles, generatedObjFiles...)
325
Colin Cross3f40fa42015-01-30 17:27:36 -0800326 c.ccModuleType().compileModule(ctx, flags, deps, objFiles)
327 if ctx.Failed() {
328 return
329 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700330
331 c.ccModuleType().installModule(ctx, flags)
332 if ctx.Failed() {
333 return
334 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800335}
336
Colin Crossfa138792015-04-24 17:31:52 -0700337func (c *CCBase) ccModuleType() CCModuleType {
Colin Cross3f40fa42015-01-30 17:27:36 -0800338 return c.module
339}
340
Colin Crossfa138792015-04-24 17:31:52 -0700341var _ common.AndroidDynamicDepender = (*CCBase)(nil)
Colin Cross3f40fa42015-01-30 17:27:36 -0800342
Colin Crossfa138792015-04-24 17:31:52 -0700343func (c *CCBase) findToolchain(ctx common.AndroidModuleContext) Toolchain {
Colin Cross3f40fa42015-01-30 17:27:36 -0800344 arch := ctx.Arch()
345 factory := toolchainFactories[arch.HostOrDevice][arch.ArchType]
346 if factory == nil {
347 panic(fmt.Sprintf("Toolchain not found for %s arch %q",
348 arch.HostOrDevice.String(), arch.String()))
349 }
350 return factory(arch.ArchVariant, arch.CpuVariant)
351}
352
Colin Crossfa138792015-04-24 17:31:52 -0700353func (c *CCBase) ModifyProperties(ctx common.AndroidBaseContext) {
354}
355
Colin Crosse11befc2015-04-27 17:49:17 -0700356func (c *CCBase) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Crossfa138792015-04-24 17:31:52 -0700357 depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.Properties.Whole_static_libs...)
358 depNames.StaticLibs = append(depNames.StaticLibs, c.Properties.Static_libs...)
359 depNames.SharedLibs = append(depNames.SharedLibs, c.Properties.Shared_libs...)
Colin Cross21b9a242015-03-24 14:15:58 -0700360
Colin Cross21b9a242015-03-24 14:15:58 -0700361 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -0800362}
363
Colin Crossfa138792015-04-24 17:31:52 -0700364func (c *CCBase) AndroidDynamicDependencies(ctx common.AndroidDynamicDependerModuleContext) []string {
365 c.module.ModifyProperties(ctx)
366
Colin Cross74d1ec02015-04-28 13:30:13 -0700367 c.savedDepNames = c.module.depNames(ctx, CCDeps{})
368 c.savedDepNames.WholeStaticLibs = lastUniqueElements(c.savedDepNames.WholeStaticLibs)
369 c.savedDepNames.StaticLibs = lastUniqueElements(c.savedDepNames.StaticLibs)
370 c.savedDepNames.SharedLibs = lastUniqueElements(c.savedDepNames.SharedLibs)
371
372 staticLibs := c.savedDepNames.WholeStaticLibs
373 staticLibs = append(staticLibs, c.savedDepNames.StaticLibs...)
374 staticLibs = append(staticLibs, c.savedDepNames.LateStaticLibs...)
Colin Cross21b9a242015-03-24 14:15:58 -0700375 ctx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, staticLibs...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800376
Colin Cross74d1ec02015-04-28 13:30:13 -0700377 ctx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, c.savedDepNames.SharedLibs...)
Colin Cross21b9a242015-03-24 14:15:58 -0700378
Colin Cross74d1ec02015-04-28 13:30:13 -0700379 ret := append([]string(nil), c.savedDepNames.ObjFiles...)
380 if c.savedDepNames.CrtBegin != "" {
381 ret = append(ret, c.savedDepNames.CrtBegin)
Colin Cross21b9a242015-03-24 14:15:58 -0700382 }
Colin Cross74d1ec02015-04-28 13:30:13 -0700383 if c.savedDepNames.CrtEnd != "" {
384 ret = append(ret, c.savedDepNames.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700385 }
386
387 return ret
Colin Cross3f40fa42015-01-30 17:27:36 -0800388}
389
390// Create a ccFlags struct that collects the compile flags from global values,
391// per-target values, module type values, and per-module Blueprints properties
Colin Crossfa138792015-04-24 17:31:52 -0700392func (c *CCBase) collectFlags(ctx common.AndroidModuleContext, toolchain Toolchain) CCFlags {
Colin Cross97ba0732015-03-23 17:50:24 -0700393 flags := CCFlags{
Colin Crossfa138792015-04-24 17:31:52 -0700394 CFlags: c.Properties.Cflags,
395 CppFlags: c.Properties.Cppflags,
396 ConlyFlags: c.Properties.Conlyflags,
397 LdFlags: c.Properties.Ldflags,
398 AsFlags: c.Properties.Asflags,
399 YaccFlags: c.Properties.Yaccflags,
400 Nocrt: c.Properties.Nocrt,
Colin Cross97ba0732015-03-23 17:50:24 -0700401 Toolchain: toolchain,
Colin Crossfa138792015-04-24 17:31:52 -0700402 Clang: c.Properties.Clang,
Colin Cross3f40fa42015-01-30 17:27:36 -0800403 }
Colin Cross28344522015-04-22 13:07:53 -0700404
405 // Include dir cflags
Colin Crossfa138792015-04-24 17:31:52 -0700406 rootIncludeDirs := pathtools.PrefixPaths(c.Properties.Include_dirs, ctx.AConfig().SrcDir())
407 localIncludeDirs := pathtools.PrefixPaths(c.Properties.Local_include_dirs, common.ModuleSrcDir(ctx))
Colin Cross28344522015-04-22 13:07:53 -0700408 flags.GlobalFlags = append(flags.GlobalFlags,
409 includeDirsToFlags(rootIncludeDirs),
410 includeDirsToFlags(localIncludeDirs))
411
Colin Crossfa138792015-04-24 17:31:52 -0700412 if !c.Properties.No_default_compiler_flags {
413 if c.Properties.Sdk_version == "" || ctx.Host() {
Colin Cross28344522015-04-22 13:07:53 -0700414 flags.GlobalFlags = append(flags.GlobalFlags,
415 "${commonGlobalIncludes}",
416 toolchain.IncludeFlags(),
417 "-I${SrcDir}/libnativehelper/include/nativehelper")
418 }
419
420 flags.GlobalFlags = append(flags.GlobalFlags, []string{
421 "-I" + common.ModuleSrcDir(ctx),
422 "-I" + common.ModuleOutDir(ctx),
423 "-I" + common.ModuleGenDir(ctx),
424 }...)
425 }
426
Colin Crossfa138792015-04-24 17:31:52 -0700427 instructionSet := c.Properties.Instruction_set
Tim Kilbourn1a9bf262015-03-18 12:28:32 -0700428 instructionSetFlags, err := toolchain.InstructionSetFlags(instructionSet)
429 if err != nil {
430 ctx.ModuleErrorf("%s", err)
431 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800432
Colin Crossaf19a292015-03-18 12:07:10 -0700433 // TODO: debug
Colin Crossfa138792015-04-24 17:31:52 -0700434 flags.CFlags = append(flags.CFlags, c.Properties.Release.Cflags...)
Colin Crossaf19a292015-03-18 12:07:10 -0700435
Colin Cross28d76592015-03-26 16:14:04 -0700436 if ctx.Host() && !ctx.ContainsProperty("clang") {
Colin Cross97ba0732015-03-23 17:50:24 -0700437 flags.Clang = true
Colin Cross3f40fa42015-01-30 17:27:36 -0800438 }
439
Colin Cross97ba0732015-03-23 17:50:24 -0700440 if flags.Clang {
441 flags.CFlags = clangFilterUnknownCflags(flags.CFlags)
Colin Crossfa138792015-04-24 17:31:52 -0700442 flags.CFlags = append(flags.CFlags, c.Properties.Clang_cflags...)
443 flags.AsFlags = append(flags.AsFlags, c.Properties.Clang_asflags...)
Colin Cross97ba0732015-03-23 17:50:24 -0700444 flags.CppFlags = clangFilterUnknownCflags(flags.CppFlags)
445 flags.ConlyFlags = clangFilterUnknownCflags(flags.ConlyFlags)
446 flags.LdFlags = clangFilterUnknownCflags(flags.LdFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800447
Colin Cross97ba0732015-03-23 17:50:24 -0700448 flags.CFlags = append(flags.CFlags, "${clangExtraCflags}")
449 flags.ConlyFlags = append(flags.ConlyFlags, "${clangExtraConlyflags}")
Colin Crossf6566ed2015-03-24 11:13:38 -0700450 if ctx.Device() {
Colin Cross97ba0732015-03-23 17:50:24 -0700451 flags.CFlags = append(flags.CFlags, "${clangExtraTargetCflags}")
Colin Crossbdd7b1c2015-03-16 16:21:20 -0700452 }
453
Colin Cross3f40fa42015-01-30 17:27:36 -0800454 target := "-target " + toolchain.ClangTriple()
455 gccPrefix := "-B" + filepath.Join(toolchain.GccRoot(), toolchain.GccTriple(), "bin")
456
Colin Cross97ba0732015-03-23 17:50:24 -0700457 flags.CFlags = append(flags.CFlags, target, gccPrefix)
458 flags.AsFlags = append(flags.AsFlags, target, gccPrefix)
459 flags.LdFlags = append(flags.LdFlags, target, gccPrefix)
Colin Cross3f40fa42015-01-30 17:27:36 -0800460 }
461
Colin Crossfa138792015-04-24 17:31:52 -0700462 if !c.Properties.No_default_compiler_flags {
463 if ctx.Device() && !c.Properties.Allow_undefined_symbols {
Colin Cross97ba0732015-03-23 17:50:24 -0700464 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-undefined")
Colin Cross3f40fa42015-01-30 17:27:36 -0800465 }
466
Colin Cross56b4d452015-04-21 17:38:44 -0700467 flags.GlobalFlags = append(flags.GlobalFlags, instructionSetFlags)
468
Colin Cross97ba0732015-03-23 17:50:24 -0700469 if flags.Clang {
470 flags.CppFlags = append(flags.CppFlags, "${commonClangGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -0700471 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -0800472 toolchain.ClangCflags(),
473 "${commonClangGlobalCflags}",
Colin Cross56b4d452015-04-21 17:38:44 -0700474 fmt.Sprintf("${%sClangGlobalCflags}", ctx.Arch().HostOrDevice))
Colin Cross3f40fa42015-01-30 17:27:36 -0800475 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700476 flags.CppFlags = append(flags.CppFlags, "${commonGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -0700477 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -0800478 toolchain.Cflags(),
479 "${commonGlobalCflags}",
Colin Cross56b4d452015-04-21 17:38:44 -0700480 fmt.Sprintf("${%sGlobalCflags}", ctx.Arch().HostOrDevice))
Colin Cross3f40fa42015-01-30 17:27:36 -0800481 }
482
Colin Crossf6566ed2015-03-24 11:13:38 -0700483 if ctx.Device() {
Colin Crossfa138792015-04-24 17:31:52 -0700484 if c.Properties.Rtti {
Colin Cross97ba0732015-03-23 17:50:24 -0700485 flags.CppFlags = append(flags.CppFlags, "-frtti")
Colin Cross3f40fa42015-01-30 17:27:36 -0800486 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700487 flags.CppFlags = append(flags.CppFlags, "-fno-rtti")
Colin Cross3f40fa42015-01-30 17:27:36 -0800488 }
489 }
490
Colin Cross97ba0732015-03-23 17:50:24 -0700491 flags.AsFlags = append(flags.AsFlags, "-D__ASSEMBLY__")
Colin Cross3f40fa42015-01-30 17:27:36 -0800492
Colin Cross97ba0732015-03-23 17:50:24 -0700493 if flags.Clang {
494 flags.CppFlags = append(flags.CppFlags, toolchain.ClangCppflags())
495 flags.LdFlags = append(flags.LdFlags, toolchain.ClangLdflags())
Colin Cross3f40fa42015-01-30 17:27:36 -0800496 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700497 flags.CppFlags = append(flags.CppFlags, toolchain.Cppflags())
498 flags.LdFlags = append(flags.LdFlags, toolchain.Ldflags())
Colin Cross3f40fa42015-01-30 17:27:36 -0800499 }
Colin Cross28344522015-04-22 13:07:53 -0700500
501 if ctx.Host() {
Colin Crossfa138792015-04-24 17:31:52 -0700502 flags.LdFlags = append(flags.LdFlags, c.Properties.Host_ldlibs...)
Colin Cross28344522015-04-22 13:07:53 -0700503 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800504 }
505
Colin Cross0676e2d2015-04-24 17:39:18 -0700506 flags = c.ccModuleType().flags(ctx, flags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800507
508 // Optimization to reduce size of build.ninja
509 // Replace the long list of flags for each file with a module-local variable
Colin Cross97ba0732015-03-23 17:50:24 -0700510 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
511 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
512 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
513 flags.CFlags = []string{"$cflags"}
514 flags.CppFlags = []string{"$cppflags"}
515 flags.AsFlags = []string{"$asflags"}
Colin Cross3f40fa42015-01-30 17:27:36 -0800516
517 return flags
518}
519
Colin Cross0676e2d2015-04-24 17:39:18 -0700520func (c *CCBase) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Cross3f40fa42015-01-30 17:27:36 -0800521 return flags
522}
523
524// Compile a list of source files into objects a specified subdirectory
Colin Crossfa138792015-04-24 17:31:52 -0700525func (c *CCBase) customCompileObjs(ctx common.AndroidModuleContext, flags CCFlags,
Colin Cross581c1892015-04-07 16:50:10 -0700526 subdir string, srcFiles []string) []string {
527
528 buildFlags := ccFlagsToBuilderFlags(flags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800529
Colin Crossfce53272015-04-08 11:21:40 -0700530 srcFiles = common.ExpandSources(ctx, srcFiles)
Colin Cross581c1892015-04-07 16:50:10 -0700531 srcFiles, deps := genSources(ctx, srcFiles, buildFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800532
Colin Cross581c1892015-04-07 16:50:10 -0700533 return TransformSourceToObj(ctx, subdir, srcFiles, buildFlags, deps)
Colin Cross3f40fa42015-01-30 17:27:36 -0800534}
535
Colin Crossfa138792015-04-24 17:31:52 -0700536// Compile files listed in c.Properties.Srcs into objects
537func (c *CCBase) compileObjs(ctx common.AndroidModuleContext, flags CCFlags) []string {
Colin Cross3f40fa42015-01-30 17:27:36 -0800538
Colin Crossfa138792015-04-24 17:31:52 -0700539 if c.Properties.SkipCompileObjs {
Colin Cross3f40fa42015-01-30 17:27:36 -0800540 return nil
541 }
542
Colin Crossfa138792015-04-24 17:31:52 -0700543 return c.customCompileObjs(ctx, flags, "", c.Properties.Srcs)
Colin Cross3f40fa42015-01-30 17:27:36 -0800544}
545
Colin Cross5049f022015-03-18 13:28:46 -0700546// Compile generated source files from dependencies
Colin Crossfa138792015-04-24 17:31:52 -0700547func (c *CCBase) compileGeneratedObjs(ctx common.AndroidModuleContext, flags CCFlags) []string {
Colin Cross5049f022015-03-18 13:28:46 -0700548 var srcs []string
549
Colin Crossfa138792015-04-24 17:31:52 -0700550 if c.Properties.SkipCompileObjs {
Colin Cross5049f022015-03-18 13:28:46 -0700551 return nil
552 }
553
554 ctx.VisitDirectDeps(func(module blueprint.Module) {
555 if gen, ok := module.(genrule.SourceFileGenerator); ok {
556 srcs = append(srcs, gen.GeneratedSourceFiles()...)
557 }
558 })
559
560 if len(srcs) == 0 {
561 return nil
562 }
563
Colin Cross581c1892015-04-07 16:50:10 -0700564 return TransformSourceToObj(ctx, "", srcs, ccFlagsToBuilderFlags(flags), nil)
Colin Cross5049f022015-03-18 13:28:46 -0700565}
566
Colin Crossfa138792015-04-24 17:31:52 -0700567func (c *CCBase) outputFile() string {
Colin Cross3f40fa42015-01-30 17:27:36 -0800568 return ""
569}
570
Colin Crossfa138792015-04-24 17:31:52 -0700571func (c *CCBase) depsToPathsFromList(ctx common.AndroidModuleContext,
Colin Cross3f40fa42015-01-30 17:27:36 -0800572 names []string) (modules []common.AndroidModule,
Colin Cross28344522015-04-22 13:07:53 -0700573 outputFiles []string, exportedFlags []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800574
575 for _, n := range names {
576 found := false
577 ctx.VisitDirectDeps(func(m blueprint.Module) {
578 otherName := ctx.OtherModuleName(m)
579 if otherName != n {
580 return
581 }
582
Colin Cross97ba0732015-03-23 17:50:24 -0700583 if a, ok := m.(CCModuleType); ok {
Colin Cross3f40fa42015-01-30 17:27:36 -0800584 if a.Disabled() {
585 // If a cc_library host+device module depends on a library that exists as both
586 // cc_library_shared and cc_library_host_shared, it will end up with two
587 // dependencies with the same name, one of which is marked disabled for each
588 // of host and device. Ignore the disabled one.
589 return
590 }
591 if a.HostOrDevice() != ctx.Arch().HostOrDevice {
592 ctx.ModuleErrorf("host/device mismatch between %q and %q", ctx.ModuleName(),
593 otherName)
594 return
595 }
596
597 if outputFile := a.outputFile(); outputFile != "" {
598 if found {
599 ctx.ModuleErrorf("multiple modules satisified dependency on %q", otherName)
600 return
601 }
602 outputFiles = append(outputFiles, outputFile)
603 modules = append(modules, a)
Colin Cross28344522015-04-22 13:07:53 -0700604 if i, ok := a.(ccExportedFlagsProducer); ok {
605 exportedFlags = append(exportedFlags, i.exportedFlags()...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800606 }
607 found = true
608 } else {
609 ctx.ModuleErrorf("module %q missing output file", otherName)
610 return
611 }
612 } else {
613 ctx.ModuleErrorf("module %q not an android module", otherName)
614 return
615 }
616 })
617 if !found {
618 ctx.ModuleErrorf("unsatisified dependency on %q", n)
619 }
620 }
621
Colin Cross28344522015-04-22 13:07:53 -0700622 return modules, outputFiles, exportedFlags
Colin Cross3f40fa42015-01-30 17:27:36 -0800623}
624
Colin Cross21b9a242015-03-24 14:15:58 -0700625// Convert depenedency names to paths. Takes a CCDeps containing names and returns a CCDeps
626// containing paths
Colin Crossfa138792015-04-24 17:31:52 -0700627func (c *CCBase) depsToPaths(ctx common.AndroidModuleContext, depNames CCDeps) CCDeps {
Colin Cross21b9a242015-03-24 14:15:58 -0700628 var depPaths CCDeps
Colin Cross28344522015-04-22 13:07:53 -0700629 var newCflags []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800630
Colin Cross21b9a242015-03-24 14:15:58 -0700631 var wholeStaticLibModules []common.AndroidModule
Colin Cross3f40fa42015-01-30 17:27:36 -0800632
Colin Cross28344522015-04-22 13:07:53 -0700633 wholeStaticLibModules, depPaths.WholeStaticLibs, newCflags =
Colin Cross21b9a242015-03-24 14:15:58 -0700634 c.depsToPathsFromList(ctx, depNames.WholeStaticLibs)
Colin Cross28344522015-04-22 13:07:53 -0700635 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800636
Colin Cross21b9a242015-03-24 14:15:58 -0700637 for _, m := range wholeStaticLibModules {
638 if staticLib, ok := m.(ccLibraryInterface); ok && staticLib.static() {
639 depPaths.WholeStaticLibObjFiles =
640 append(depPaths.WholeStaticLibObjFiles, staticLib.allObjFiles()...)
641 } else {
642 ctx.ModuleErrorf("module %q not a static library", ctx.OtherModuleName(m))
643 }
644 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800645
Colin Cross28344522015-04-22 13:07:53 -0700646 _, depPaths.StaticLibs, newCflags = c.depsToPathsFromList(ctx, depNames.StaticLibs)
647 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross21b9a242015-03-24 14:15:58 -0700648
Colin Cross28344522015-04-22 13:07:53 -0700649 _, depPaths.LateStaticLibs, newCflags = c.depsToPathsFromList(ctx, depNames.LateStaticLibs)
650 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross21b9a242015-03-24 14:15:58 -0700651
Colin Cross28344522015-04-22 13:07:53 -0700652 _, depPaths.SharedLibs, newCflags = c.depsToPathsFromList(ctx, depNames.SharedLibs)
653 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross21b9a242015-03-24 14:15:58 -0700654
655 ctx.VisitDirectDeps(func(m blueprint.Module) {
Dan Albertc3144b12015-04-28 18:17:56 -0700656 if obj, ok := m.(ccObjectProvider); ok {
Colin Cross21b9a242015-03-24 14:15:58 -0700657 otherName := ctx.OtherModuleName(m)
658 if otherName == depNames.CrtBegin {
Colin Crossfa138792015-04-24 17:31:52 -0700659 if !c.Properties.Nocrt {
Dan Albertc3144b12015-04-28 18:17:56 -0700660 depPaths.CrtBegin = obj.object().outputFile()
Colin Cross21b9a242015-03-24 14:15:58 -0700661 }
662 } else if otherName == depNames.CrtEnd {
Colin Crossfa138792015-04-24 17:31:52 -0700663 if !c.Properties.Nocrt {
Dan Albertc3144b12015-04-28 18:17:56 -0700664 depPaths.CrtEnd = obj.object().outputFile()
Colin Cross21b9a242015-03-24 14:15:58 -0700665 }
666 } else {
Dan Albertc3144b12015-04-28 18:17:56 -0700667 depPaths.ObjFiles = append(depPaths.ObjFiles, obj.object().outputFile())
Colin Cross21b9a242015-03-24 14:15:58 -0700668 }
669 }
670 })
671
672 return depPaths
Colin Cross3f40fa42015-01-30 17:27:36 -0800673}
674
Colin Crossfa138792015-04-24 17:31:52 -0700675// CCLinked contains the properties and members used by libraries and executables
676type CCLinked struct {
677 CCBase
Colin Crossed4cf0b2015-03-26 14:43:45 -0700678
679 dynamicProperties struct {
Colin Cross18b6dc52015-04-28 13:20:37 -0700680 VariantIsShared bool `blueprint:"mutated"`
681 VariantIsStatic bool `blueprint:"mutated"`
682 VariantIsStaticBinary bool `blueprint:"mutated"`
Colin Crossed4cf0b2015-03-26 14:43:45 -0700683 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800684}
685
Colin Crossfa138792015-04-24 17:31:52 -0700686func newCCDynamic(dynamic *CCLinked, module CCModuleType, hod common.HostOrDeviceSupported,
Colin Crossc472d572015-03-17 15:06:21 -0700687 multilib common.Multilib, props ...interface{}) (blueprint.Module, []interface{}) {
688
Colin Crossed4cf0b2015-03-26 14:43:45 -0700689 props = append(props, &dynamic.dynamicProperties)
690
Colin Crossfa138792015-04-24 17:31:52 -0700691 return newCCBase(&dynamic.CCBase, module, hod, multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700692}
693
Colin Crossfa138792015-04-24 17:31:52 -0700694func (c *CCLinked) systemSharedLibs(ctx common.AndroidBaseContext) []string {
Colin Cross28d76592015-03-26 16:14:04 -0700695 if ctx.ContainsProperty("system_shared_libs") {
Colin Crossfa138792015-04-24 17:31:52 -0700696 return c.Properties.System_shared_libs
697 } else if ctx.Device() && c.Properties.Sdk_version == "" {
Colin Cross577f6e42015-03-27 18:23:34 -0700698 return []string{"libc", "libm"}
Colin Cross28d76592015-03-26 16:14:04 -0700699 } else {
Colin Cross577f6e42015-03-27 18:23:34 -0700700 return nil
Colin Cross3f40fa42015-01-30 17:27:36 -0800701 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800702}
703
Colin Crossfa138792015-04-24 17:31:52 -0700704func (c *CCLinked) stl(ctx common.AndroidBaseContext) string {
705 if c.Properties.Sdk_version != "" && ctx.Device() {
706 switch c.Properties.Stl {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700707 case "":
708 return "ndk_system"
709 case "c++_shared", "c++_static",
710 "stlport_shared", "stlport_static",
711 "gnustl_static":
Colin Crossfa138792015-04-24 17:31:52 -0700712 return "ndk_lib" + c.Properties.Stl
Colin Crossed4cf0b2015-03-26 14:43:45 -0700713 default:
Colin Crossfa138792015-04-24 17:31:52 -0700714 ctx.ModuleErrorf("stl: %q is not a supported STL with sdk_version set", c.Properties.Stl)
Colin Crossed4cf0b2015-03-26 14:43:45 -0700715 return ""
716 }
717 }
718
Colin Crossfa138792015-04-24 17:31:52 -0700719 switch c.Properties.Stl {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700720 case "libc++", "libc++_static",
721 "stlport", "stlport_static",
722 "libstdc++":
Colin Crossfa138792015-04-24 17:31:52 -0700723 return c.Properties.Stl
Colin Crossed4cf0b2015-03-26 14:43:45 -0700724 case "none":
725 return ""
726 case "":
Colin Cross18b6dc52015-04-28 13:20:37 -0700727 if c.static() {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700728 return "libc++_static"
Colin Cross18b6dc52015-04-28 13:20:37 -0700729 } else {
730 return "libc++" // TODO: mingw needs libstdc++
Colin Crossed4cf0b2015-03-26 14:43:45 -0700731 }
732 default:
Colin Crossfa138792015-04-24 17:31:52 -0700733 ctx.ModuleErrorf("stl: %q is not a supported STL", c.Properties.Stl)
Colin Crossed4cf0b2015-03-26 14:43:45 -0700734 return ""
735 }
736}
737
Colin Cross0af4b842015-04-30 16:36:18 -0700738var hostDynamicGccLibs, hostStaticGccLibs []string
739
740func init() {
741 if runtime.GOOS == "darwin" {
742 hostDynamicGccLibs = []string{"-lc", "-lSystem"}
743 hostStaticGccLibs = []string{"NO_STATIC_HOST_BINARIES_ON_DARWIN"}
744 } else {
745 hostDynamicGccLibs = []string{"-lgcc_s", "-lgcc", "-lc", "-lgcc_s", "-lgcc"}
746 hostStaticGccLibs = []string{"-Wl,--start-group", "-lgcc", "-lgcc_eh", "-lc", "-Wl,--end-group"}
747 }
748}
Colin Cross712fc022015-04-27 11:13:34 -0700749
Colin Crosse11befc2015-04-27 17:49:17 -0700750func (c *CCLinked) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700751 stl := c.stl(ctx)
752 if ctx.Failed() {
753 return flags
754 }
755
756 switch stl {
757 case "libc++", "libc++_static":
758 flags.CFlags = append(flags.CFlags, "-D_USING_LIBCXX")
Colin Cross28344522015-04-22 13:07:53 -0700759 flags.CFlags = append(flags.CFlags, "-I${SrcDir}/external/libcxx/include")
Colin Crossed4cf0b2015-03-26 14:43:45 -0700760 if ctx.Host() {
761 flags.CppFlags = append(flags.CppFlags, "-nostdinc++")
762 flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs")
Colin Cross712fc022015-04-27 11:13:34 -0700763 flags.LdFlags = append(flags.LdFlags, "-lm", "-lpthread")
Colin Cross18b6dc52015-04-28 13:20:37 -0700764 if c.staticBinary() {
Colin Cross712fc022015-04-27 11:13:34 -0700765 flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs...)
Colin Cross18b6dc52015-04-28 13:20:37 -0700766 } else {
767 flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs...)
Colin Cross712fc022015-04-27 11:13:34 -0700768 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700769 }
770 case "stlport", "stlport_static":
771 if ctx.Device() {
Colin Cross28344522015-04-22 13:07:53 -0700772 flags.CFlags = append(flags.CFlags,
773 "-I${SrcDir}/external/stlport/stlport",
774 "-I${SrcDir}/bionic/libstdc++/include",
775 "-I${SrcDir}/bionic")
Colin Crossed4cf0b2015-03-26 14:43:45 -0700776 }
777 case "libstdc++":
778 // Using bionic's basic libstdc++. Not actually an STL. Only around until the
779 // tree is in good enough shape to not need it.
780 // Host builds will use GNU libstdc++.
781 if ctx.Device() {
Colin Cross28344522015-04-22 13:07:53 -0700782 flags.CFlags = append(flags.CFlags, "-I${SrcDir}/bionic/libstdc++/include")
Colin Crossed4cf0b2015-03-26 14:43:45 -0700783 }
784 case "ndk_system":
Colin Cross1332b002015-04-07 17:11:30 -0700785 ndkSrcRoot := ctx.AConfig().SrcDir() + "/prebuilts/ndk/current/sources/"
Colin Cross28344522015-04-22 13:07:53 -0700786 flags.CFlags = append(flags.CFlags, "-isystem "+ndkSrcRoot+"cxx-stl/system/include")
Colin Crossed4cf0b2015-03-26 14:43:45 -0700787 case "ndk_libc++_shared", "ndk_libc++_static":
788 // TODO(danalbert): This really shouldn't be here...
789 flags.CppFlags = append(flags.CppFlags, "-std=c++11")
790 case "ndk_libstlport_shared", "ndk_libstlport_static", "ndk_libgnustl_static":
791 // Nothing
792 case "":
793 // None or error.
794 if ctx.Host() {
795 flags.CppFlags = append(flags.CppFlags, "-nostdinc++")
796 flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs")
Colin Cross18b6dc52015-04-28 13:20:37 -0700797 if c.staticBinary() {
Colin Cross712fc022015-04-27 11:13:34 -0700798 flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs...)
Colin Cross18b6dc52015-04-28 13:20:37 -0700799 } else {
800 flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs...)
Colin Cross712fc022015-04-27 11:13:34 -0700801 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700802 }
803 default:
Colin Crossfa138792015-04-24 17:31:52 -0700804 panic(fmt.Errorf("Unknown stl in CCLinked.Flags: %q", stl))
Colin Crossed4cf0b2015-03-26 14:43:45 -0700805 }
806
807 return flags
808}
809
Colin Crosse11befc2015-04-27 17:49:17 -0700810func (c *CCLinked) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
811 depNames = c.CCBase.depNames(ctx, depNames)
Colin Cross3f40fa42015-01-30 17:27:36 -0800812
Colin Crossed4cf0b2015-03-26 14:43:45 -0700813 stl := c.stl(ctx)
814 if ctx.Failed() {
815 return depNames
816 }
817
818 switch stl {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700819 case "libstdc++":
820 if ctx.Device() {
821 depNames.SharedLibs = append(depNames.SharedLibs, stl)
822 }
Colin Cross74d1ec02015-04-28 13:30:13 -0700823 case "libc++", "libc++_static":
824 if stl == "libc++" {
825 depNames.SharedLibs = append(depNames.SharedLibs, stl)
826 } else {
827 depNames.StaticLibs = append(depNames.StaticLibs, stl)
828 }
829 if ctx.Device() {
830 if ctx.Arch().ArchType == common.Arm {
831 depNames.StaticLibs = append(depNames.StaticLibs, "libunwind_llvm")
832 }
833 if c.staticBinary() {
834 depNames.StaticLibs = append(depNames.StaticLibs, "libdl")
835 } else {
836 depNames.SharedLibs = append(depNames.SharedLibs, "libdl")
837 }
838 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700839 case "stlport":
840 depNames.SharedLibs = append(depNames.SharedLibs, "libstdc++", "libstlport")
841 case "stlport_static":
842 depNames.StaticLibs = append(depNames.StaticLibs, "libstdc++", "libstlport_static")
843 case "":
844 // None or error.
845 case "ndk_system":
846 // TODO: Make a system STL prebuilt for the NDK.
847 // The system STL doesn't have a prebuilt (it uses the system's libstdc++), but it does have
Colin Crossfa138792015-04-24 17:31:52 -0700848 // its own includes. The includes are handled in CCBase.Flags().
Colin Cross577f6e42015-03-27 18:23:34 -0700849 depNames.SharedLibs = append([]string{"libstdc++"}, depNames.SharedLibs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -0700850 case "ndk_libc++_shared", "ndk_libstlport_shared":
851 depNames.SharedLibs = append(depNames.SharedLibs, stl)
852 case "ndk_libc++_static", "ndk_libstlport_static", "ndk_libgnustl_static":
853 depNames.StaticLibs = append(depNames.StaticLibs, stl)
854 default:
Colin Crosse11befc2015-04-27 17:49:17 -0700855 panic(fmt.Errorf("Unknown stl in CCLinked.depNames: %q", stl))
Colin Crossed4cf0b2015-03-26 14:43:45 -0700856 }
857
Colin Cross74d1ec02015-04-28 13:30:13 -0700858 if ctx.ModuleName() != "libcompiler_rt-extras" {
859 depNames.StaticLibs = append(depNames.StaticLibs, "libcompiler_rt-extras")
860 }
861
Colin Crossf6566ed2015-03-24 11:13:38 -0700862 if ctx.Device() {
Colin Cross77b00fa2015-03-16 16:15:49 -0700863 // libgcc and libatomic have to be last on the command line
Colin Cross21b9a242015-03-24 14:15:58 -0700864 depNames.LateStaticLibs = append(depNames.LateStaticLibs, "libgcov", "libatomic", "libgcc")
Colin Crossed4cf0b2015-03-26 14:43:45 -0700865
Colin Cross18b6dc52015-04-28 13:20:37 -0700866 if !c.static() {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700867 depNames.SharedLibs = append(depNames.SharedLibs, c.systemSharedLibs(ctx)...)
868 }
Colin Cross577f6e42015-03-27 18:23:34 -0700869
Colin Crossfa138792015-04-24 17:31:52 -0700870 if c.Properties.Sdk_version != "" {
871 version := c.Properties.Sdk_version
Colin Cross577f6e42015-03-27 18:23:34 -0700872 depNames.SharedLibs = append(depNames.SharedLibs,
873 "ndk_libc."+version,
874 "ndk_libm."+version,
875 )
876 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800877 }
878
Colin Cross21b9a242015-03-24 14:15:58 -0700879 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -0800880}
881
Colin Crossed4cf0b2015-03-26 14:43:45 -0700882// ccLinkedInterface interface is used on ccLinked to deal with static or shared variants
883type ccLinkedInterface interface {
884 // Returns true if the build options for the module have selected a static or shared build
885 buildStatic() bool
886 buildShared() bool
887
888 // Sets whether a specific variant is static or shared
Colin Cross18b6dc52015-04-28 13:20:37 -0700889 setStatic(bool)
Colin Crossed4cf0b2015-03-26 14:43:45 -0700890
Colin Cross18b6dc52015-04-28 13:20:37 -0700891 // Returns whether a specific variant is a static library or binary
Colin Crossed4cf0b2015-03-26 14:43:45 -0700892 static() bool
Colin Cross18b6dc52015-04-28 13:20:37 -0700893
894 // Returns whether a module is a static binary
895 staticBinary() bool
Colin Crossed4cf0b2015-03-26 14:43:45 -0700896}
897
898var _ ccLinkedInterface = (*CCLibrary)(nil)
899var _ ccLinkedInterface = (*CCBinary)(nil)
900
Colin Crossfa138792015-04-24 17:31:52 -0700901func (c *CCLinked) static() bool {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700902 return c.dynamicProperties.VariantIsStatic
903}
904
Colin Cross18b6dc52015-04-28 13:20:37 -0700905func (c *CCLinked) staticBinary() bool {
906 return c.dynamicProperties.VariantIsStaticBinary
Colin Crossed4cf0b2015-03-26 14:43:45 -0700907}
908
Colin Cross18b6dc52015-04-28 13:20:37 -0700909func (c *CCLinked) setStatic(static bool) {
910 c.dynamicProperties.VariantIsStatic = static
Colin Crossed4cf0b2015-03-26 14:43:45 -0700911}
912
Colin Cross28344522015-04-22 13:07:53 -0700913type ccExportedFlagsProducer interface {
914 exportedFlags() []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800915}
916
917//
918// Combined static+shared libraries
919//
920
Colin Cross97ba0732015-03-23 17:50:24 -0700921type CCLibrary struct {
Colin Crossfa138792015-04-24 17:31:52 -0700922 CCLinked
Colin Cross3f40fa42015-01-30 17:27:36 -0800923
Colin Cross28344522015-04-22 13:07:53 -0700924 reuseFrom ccLibraryInterface
925 reuseObjFiles []string
926 objFiles []string
927 exportFlags []string
928 out string
Colin Cross3f40fa42015-01-30 17:27:36 -0800929
Colin Cross97ba0732015-03-23 17:50:24 -0700930 LibraryProperties struct {
Colin Cross3f40fa42015-01-30 17:27:36 -0800931 BuildStatic bool `blueprint:"mutated"`
932 BuildShared bool `blueprint:"mutated"`
Colin Crossed4cf0b2015-03-26 14:43:45 -0700933 Static struct {
Colin Cross2732e9a2015-04-28 13:23:52 -0700934 Srcs []string `android:"arch_variant"`
935 Cflags []string `android:"arch_variant"`
936 Whole_static_libs []string `android:"arch_variant"`
937 Static_libs []string `android:"arch_variant"`
938 Shared_libs []string `android:"arch_variant"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800939 } `android:"arch_variant"`
940 Shared struct {
Colin Cross2732e9a2015-04-28 13:23:52 -0700941 Srcs []string `android:"arch_variant"`
942 Cflags []string `android:"arch_variant"`
943 Whole_static_libs []string `android:"arch_variant"`
944 Static_libs []string `android:"arch_variant"`
945 Shared_libs []string `android:"arch_variant"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800946 } `android:"arch_variant"`
947 }
948}
949
Colin Crossed4cf0b2015-03-26 14:43:45 -0700950func (c *CCLibrary) buildStatic() bool {
951 return c.LibraryProperties.BuildStatic
952}
953
954func (c *CCLibrary) buildShared() bool {
955 return c.LibraryProperties.BuildShared
956}
957
Colin Cross97ba0732015-03-23 17:50:24 -0700958type ccLibraryInterface interface {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700959 ccLinkedInterface
Colin Cross97ba0732015-03-23 17:50:24 -0700960 ccLibrary() *CCLibrary
Colin Crossed4cf0b2015-03-26 14:43:45 -0700961 setReuseFrom(ccLibraryInterface)
962 getReuseFrom() ccLibraryInterface
963 getReuseObjFiles() []string
Colin Cross97ba0732015-03-23 17:50:24 -0700964 allObjFiles() []string
Colin Crossc472d572015-03-17 15:06:21 -0700965}
966
Colin Crossed4cf0b2015-03-26 14:43:45 -0700967var _ ccLibraryInterface = (*CCLibrary)(nil)
968
Colin Cross97ba0732015-03-23 17:50:24 -0700969func (c *CCLibrary) ccLibrary() *CCLibrary {
970 return c
Colin Cross3f40fa42015-01-30 17:27:36 -0800971}
972
Colin Cross97ba0732015-03-23 17:50:24 -0700973func NewCCLibrary(library *CCLibrary, module CCModuleType,
974 hod common.HostOrDeviceSupported) (blueprint.Module, []interface{}) {
975
Colin Crossfa138792015-04-24 17:31:52 -0700976 return newCCDynamic(&library.CCLinked, module, hod, common.MultilibBoth,
Colin Cross97ba0732015-03-23 17:50:24 -0700977 &library.LibraryProperties)
978}
979
980func CCLibraryFactory() (blueprint.Module, []interface{}) {
981 module := &CCLibrary{}
982
983 module.LibraryProperties.BuildShared = true
984 module.LibraryProperties.BuildStatic = true
985
986 return NewCCLibrary(module, module, common.HostAndDeviceSupported)
987}
988
Colin Cross0676e2d2015-04-24 17:39:18 -0700989func (c *CCLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Crosse11befc2015-04-27 17:49:17 -0700990 depNames = c.CCLinked.depNames(ctx, depNames)
Colin Cross2732e9a2015-04-28 13:23:52 -0700991 if c.static() {
992 depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.LibraryProperties.Static.Whole_static_libs...)
993 depNames.StaticLibs = append(depNames.StaticLibs, c.LibraryProperties.Static.Static_libs...)
994 depNames.SharedLibs = append(depNames.SharedLibs, c.LibraryProperties.Static.Shared_libs...)
995 } else {
Colin Crossf6566ed2015-03-24 11:13:38 -0700996 if ctx.Device() {
Dan Albertc3144b12015-04-28 18:17:56 -0700997 if c.Properties.Sdk_version == "" {
998 depNames.CrtBegin = "crtbegin_so"
999 depNames.CrtEnd = "crtend_so"
1000 } else {
1001 depNames.CrtBegin = "ndk_crtbegin_so." + c.Properties.Sdk_version
1002 depNames.CrtEnd = "ndk_crtend_so." + c.Properties.Sdk_version
1003 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001004 }
Colin Cross2732e9a2015-04-28 13:23:52 -07001005 depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.LibraryProperties.Shared.Whole_static_libs...)
1006 depNames.StaticLibs = append(depNames.StaticLibs, c.LibraryProperties.Shared.Static_libs...)
1007 depNames.SharedLibs = append(depNames.SharedLibs, c.LibraryProperties.Shared.Shared_libs...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001008 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001009
Colin Cross21b9a242015-03-24 14:15:58 -07001010 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -08001011}
1012
Colin Cross97ba0732015-03-23 17:50:24 -07001013func (c *CCLibrary) outputFile() string {
Colin Cross3f40fa42015-01-30 17:27:36 -08001014 return c.out
1015}
1016
Colin Crossed4cf0b2015-03-26 14:43:45 -07001017func (c *CCLibrary) getReuseObjFiles() []string {
1018 return c.reuseObjFiles
1019}
1020
1021func (c *CCLibrary) setReuseFrom(reuseFrom ccLibraryInterface) {
1022 c.reuseFrom = reuseFrom
1023}
1024
1025func (c *CCLibrary) getReuseFrom() ccLibraryInterface {
1026 return c.reuseFrom
1027}
1028
Colin Cross97ba0732015-03-23 17:50:24 -07001029func (c *CCLibrary) allObjFiles() []string {
Colin Cross3f40fa42015-01-30 17:27:36 -08001030 return c.objFiles
1031}
1032
Colin Cross28344522015-04-22 13:07:53 -07001033func (c *CCLibrary) exportedFlags() []string {
1034 return c.exportFlags
Colin Cross3f40fa42015-01-30 17:27:36 -08001035}
1036
Colin Cross0676e2d2015-04-24 17:39:18 -07001037func (c *CCLibrary) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Crosse11befc2015-04-27 17:49:17 -07001038 flags = c.CCLinked.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001039
Colin Cross97ba0732015-03-23 17:50:24 -07001040 flags.CFlags = append(flags.CFlags, "-fPIC")
Colin Cross3f40fa42015-01-30 17:27:36 -08001041
Colin Crossd8e780d2015-04-28 17:39:43 -07001042 if c.static() {
1043 flags.CFlags = append(flags.CFlags, c.LibraryProperties.Static.Cflags...)
1044 } else {
1045 flags.CFlags = append(flags.CFlags, c.LibraryProperties.Shared.Cflags...)
1046 }
1047
Colin Cross18b6dc52015-04-28 13:20:37 -07001048 if !c.static() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001049 libName := ctx.ModuleName()
1050 // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead
1051 sharedFlag := "-Wl,-shared"
Colin Crossfa138792015-04-24 17:31:52 -07001052 if c.Properties.Clang || ctx.Host() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001053 sharedFlag = "-shared"
1054 }
Colin Crossf6566ed2015-03-24 11:13:38 -07001055 if ctx.Device() {
Colin Cross97ba0732015-03-23 17:50:24 -07001056 flags.LdFlags = append(flags.LdFlags, "-nostdlib")
Colin Cross3f40fa42015-01-30 17:27:36 -08001057 }
Colin Cross97ba0732015-03-23 17:50:24 -07001058
Colin Cross0af4b842015-04-30 16:36:18 -07001059 if ctx.Darwin() {
1060 flags.LdFlags = append(flags.LdFlags,
1061 "-dynamiclib",
1062 "-single_module",
1063 //"-read_only_relocs suppress",
1064 "-install_name @rpath/"+libName+sharedLibraryExtension,
1065 )
1066 } else {
1067 flags.LdFlags = append(flags.LdFlags,
1068 "-Wl,--gc-sections",
1069 sharedFlag,
1070 "-Wl,-soname,"+libName+sharedLibraryExtension,
1071 )
1072 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001073 }
Colin Cross97ba0732015-03-23 17:50:24 -07001074
1075 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001076}
1077
Colin Cross97ba0732015-03-23 17:50:24 -07001078func (c *CCLibrary) compileStaticLibrary(ctx common.AndroidModuleContext,
1079 flags CCFlags, deps CCDeps, objFiles []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001080
1081 staticFlags := flags
Colin Cross581c1892015-04-07 16:50:10 -07001082 objFilesStatic := c.customCompileObjs(ctx, staticFlags, common.DeviceStaticLibrary,
Colin Cross97ba0732015-03-23 17:50:24 -07001083 c.LibraryProperties.Static.Srcs)
Colin Cross3f40fa42015-01-30 17:27:36 -08001084
1085 objFiles = append(objFiles, objFilesStatic...)
Colin Cross21b9a242015-03-24 14:15:58 -07001086 objFiles = append(objFiles, deps.WholeStaticLibObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001087
1088 outputFile := filepath.Join(common.ModuleOutDir(ctx), ctx.ModuleName()+staticLibraryExtension)
1089
Colin Cross0af4b842015-04-30 16:36:18 -07001090 if ctx.Darwin() {
1091 TransformDarwinObjToStaticLib(ctx, objFiles, ccFlagsToBuilderFlags(flags), outputFile)
1092 } else {
1093 TransformObjToStaticLib(ctx, objFiles, ccFlagsToBuilderFlags(flags), outputFile)
1094 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001095
1096 c.objFiles = objFiles
1097 c.out = outputFile
Colin Crossfa138792015-04-24 17:31:52 -07001098 includeDirs := pathtools.PrefixPaths(c.Properties.Export_include_dirs, common.ModuleSrcDir(ctx))
Colin Cross28344522015-04-22 13:07:53 -07001099 c.exportFlags = []string{includeDirsToFlags(includeDirs)}
Colin Cross3f40fa42015-01-30 17:27:36 -08001100
1101 ctx.CheckbuildFile(outputFile)
1102}
1103
Colin Cross97ba0732015-03-23 17:50:24 -07001104func (c *CCLibrary) compileSharedLibrary(ctx common.AndroidModuleContext,
1105 flags CCFlags, deps CCDeps, objFiles []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001106
1107 sharedFlags := flags
Colin Cross581c1892015-04-07 16:50:10 -07001108 objFilesShared := c.customCompileObjs(ctx, sharedFlags, common.DeviceSharedLibrary,
Colin Cross97ba0732015-03-23 17:50:24 -07001109 c.LibraryProperties.Shared.Srcs)
Colin Cross3f40fa42015-01-30 17:27:36 -08001110
1111 objFiles = append(objFiles, objFilesShared...)
1112
1113 outputFile := filepath.Join(common.ModuleOutDir(ctx), ctx.ModuleName()+sharedLibraryExtension)
1114
Colin Cross97ba0732015-03-23 17:50:24 -07001115 TransformObjToDynamicBinary(ctx, objFiles, deps.SharedLibs, deps.StaticLibs,
Colin Crossed4cf0b2015-03-26 14:43:45 -07001116 deps.LateStaticLibs, deps.WholeStaticLibs, deps.CrtBegin, deps.CrtEnd, false,
Colin Cross77b00fa2015-03-16 16:15:49 -07001117 ccFlagsToBuilderFlags(flags), outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08001118
1119 c.out = outputFile
Colin Crossfa138792015-04-24 17:31:52 -07001120 includeDirs := pathtools.PrefixPaths(c.Properties.Export_include_dirs, common.ModuleSrcDir(ctx))
Colin Cross28344522015-04-22 13:07:53 -07001121 c.exportFlags = []string{includeDirsToFlags(includeDirs)}
Colin Cross3f40fa42015-01-30 17:27:36 -08001122}
1123
Colin Cross97ba0732015-03-23 17:50:24 -07001124func (c *CCLibrary) compileModule(ctx common.AndroidModuleContext,
1125 flags CCFlags, deps CCDeps, objFiles []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001126
1127 // Reuse the object files from the matching static library if it exists
Colin Crossed4cf0b2015-03-26 14:43:45 -07001128 if c.getReuseFrom().ccLibrary() == c {
1129 c.reuseObjFiles = objFiles
Colin Cross3f40fa42015-01-30 17:27:36 -08001130 } else {
Colin Cross2732e9a2015-04-28 13:23:52 -07001131 if c.getReuseFrom().ccLibrary().LibraryProperties.Static.Cflags == nil &&
1132 c.LibraryProperties.Shared.Cflags == nil {
1133 objFiles = append([]string(nil), c.getReuseFrom().getReuseObjFiles()...)
1134 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001135 }
1136
Colin Crossed4cf0b2015-03-26 14:43:45 -07001137 if c.static() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001138 c.compileStaticLibrary(ctx, flags, deps, objFiles)
1139 } else {
1140 c.compileSharedLibrary(ctx, flags, deps, objFiles)
1141 }
1142}
1143
Colin Cross97ba0732015-03-23 17:50:24 -07001144func (c *CCLibrary) installStaticLibrary(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001145 // Static libraries do not get installed.
1146}
1147
Colin Cross97ba0732015-03-23 17:50:24 -07001148func (c *CCLibrary) installSharedLibrary(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001149 installDir := "lib"
Colin Cross97ba0732015-03-23 17:50:24 -07001150 if flags.Toolchain.Is64Bit() {
Dan Albertc403f7c2015-03-18 14:01:18 -07001151 installDir = "lib64"
1152 }
1153
Colin Crossfa138792015-04-24 17:31:52 -07001154 ctx.InstallFile(filepath.Join(installDir, c.Properties.Relative_install_path), c.out)
Dan Albertc403f7c2015-03-18 14:01:18 -07001155}
1156
Colin Cross97ba0732015-03-23 17:50:24 -07001157func (c *CCLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001158 if c.static() {
Dan Albertc403f7c2015-03-18 14:01:18 -07001159 c.installStaticLibrary(ctx, flags)
1160 } else {
1161 c.installSharedLibrary(ctx, flags)
1162 }
1163}
1164
Colin Cross3f40fa42015-01-30 17:27:36 -08001165//
1166// Objects (for crt*.o)
1167//
1168
Dan Albertc3144b12015-04-28 18:17:56 -07001169type ccObjectProvider interface {
1170 object() *ccObject
1171}
1172
Colin Cross3f40fa42015-01-30 17:27:36 -08001173type ccObject struct {
Colin Crossfa138792015-04-24 17:31:52 -07001174 CCBase
Colin Cross3f40fa42015-01-30 17:27:36 -08001175 out string
1176}
1177
Dan Albertc3144b12015-04-28 18:17:56 -07001178func (c *ccObject) object() *ccObject {
1179 return c
1180}
1181
Colin Cross97ba0732015-03-23 17:50:24 -07001182func CCObjectFactory() (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001183 module := &ccObject{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001184
Colin Crossfa138792015-04-24 17:31:52 -07001185 return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth)
Colin Cross3f40fa42015-01-30 17:27:36 -08001186}
1187
1188func (*ccObject) AndroidDynamicDependencies(ctx common.AndroidDynamicDependerModuleContext) []string {
1189 // object files can't have any dynamic dependencies
1190 return nil
1191}
1192
Colin Cross0676e2d2015-04-24 17:39:18 -07001193func (*ccObject) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Cross21b9a242015-03-24 14:15:58 -07001194 // object files can't have any dynamic dependencies
1195 return CCDeps{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001196}
1197
1198func (c *ccObject) compileModule(ctx common.AndroidModuleContext,
Colin Cross97ba0732015-03-23 17:50:24 -07001199 flags CCFlags, deps CCDeps, objFiles []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001200
Colin Cross97ba0732015-03-23 17:50:24 -07001201 objFiles = append(objFiles, deps.ObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001202
1203 var outputFile string
1204 if len(objFiles) == 1 {
1205 outputFile = objFiles[0]
1206 } else {
Dan Albertc3144b12015-04-28 18:17:56 -07001207 outputFile = filepath.Join(common.ModuleOutDir(ctx), ctx.ModuleName()+objectExtension)
Colin Cross3f40fa42015-01-30 17:27:36 -08001208 TransformObjsToObj(ctx, objFiles, ccFlagsToBuilderFlags(flags), outputFile)
1209 }
1210
1211 c.out = outputFile
1212
1213 ctx.CheckbuildFile(outputFile)
1214}
1215
Colin Cross97ba0732015-03-23 17:50:24 -07001216func (c *ccObject) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001217 // Object files do not get installed.
1218}
1219
Colin Cross3f40fa42015-01-30 17:27:36 -08001220func (c *ccObject) outputFile() string {
1221 return c.out
1222}
1223
Dan Albertc3144b12015-04-28 18:17:56 -07001224var _ ccObjectProvider = (*ccObject)(nil)
1225
Colin Cross3f40fa42015-01-30 17:27:36 -08001226//
1227// Executables
1228//
1229
Colin Cross97ba0732015-03-23 17:50:24 -07001230type CCBinary struct {
Colin Crossfa138792015-04-24 17:31:52 -07001231 CCLinked
Dan Albertc403f7c2015-03-18 14:01:18 -07001232 out string
Colin Crossd350ecd2015-04-28 13:25:36 -07001233 installFile string
Colin Cross97ba0732015-03-23 17:50:24 -07001234 BinaryProperties struct {
1235 // static_executable: compile executable with -static
1236 Static_executable bool
1237
1238 // stem: set the name of the output
1239 Stem string `android:"arch_variant"`
1240
Colin Cross4ae185c2015-03-26 15:12:10 -07001241 // suffix: append to the name of the output
1242 Suffix string `android:"arch_variant"`
1243
Colin Cross97ba0732015-03-23 17:50:24 -07001244 // prefix_symbols: if set, add an extra objcopy --prefix-symbols= step
1245 Prefix_symbols string
1246 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001247}
1248
Colin Crossed4cf0b2015-03-26 14:43:45 -07001249func (c *CCBinary) buildStatic() bool {
1250 return c.BinaryProperties.Static_executable
1251}
1252
1253func (c *CCBinary) buildShared() bool {
1254 return !c.BinaryProperties.Static_executable
1255}
1256
Colin Cross97ba0732015-03-23 17:50:24 -07001257func (c *CCBinary) getStem(ctx common.AndroidModuleContext) string {
Colin Cross4ae185c2015-03-26 15:12:10 -07001258 stem := ctx.ModuleName()
Colin Cross97ba0732015-03-23 17:50:24 -07001259 if c.BinaryProperties.Stem != "" {
Colin Cross4ae185c2015-03-26 15:12:10 -07001260 stem = c.BinaryProperties.Stem
Colin Cross3f40fa42015-01-30 17:27:36 -08001261 }
Colin Cross4ae185c2015-03-26 15:12:10 -07001262
1263 return stem + c.BinaryProperties.Suffix
Colin Cross3f40fa42015-01-30 17:27:36 -08001264}
1265
Colin Cross0676e2d2015-04-24 17:39:18 -07001266func (c *CCBinary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Crosse11befc2015-04-27 17:49:17 -07001267 depNames = c.CCLinked.depNames(ctx, depNames)
Colin Crossf6566ed2015-03-24 11:13:38 -07001268 if ctx.Device() {
Dan Albertc3144b12015-04-28 18:17:56 -07001269 if c.Properties.Sdk_version == "" {
1270 if c.BinaryProperties.Static_executable {
1271 depNames.CrtBegin = "crtbegin_static"
1272 } else {
1273 depNames.CrtBegin = "crtbegin_dynamic"
1274 }
1275 depNames.CrtEnd = "crtend_android"
Colin Cross3f40fa42015-01-30 17:27:36 -08001276 } else {
Dan Albertc3144b12015-04-28 18:17:56 -07001277 if c.BinaryProperties.Static_executable {
1278 depNames.CrtBegin = "ndk_crtbegin_static." + c.Properties.Sdk_version
1279 } else {
1280 depNames.CrtBegin = "ndk_crtbegin_dynamic." + c.Properties.Sdk_version
1281 }
1282 depNames.CrtEnd = "ndk_crtend_android." + c.Properties.Sdk_version
Colin Cross3f40fa42015-01-30 17:27:36 -08001283 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001284
1285 if c.BinaryProperties.Static_executable {
Colin Cross74d1ec02015-04-28 13:30:13 -07001286 if c.stl(ctx) == "libc++_static" {
1287 depNames.StaticLibs = append(depNames.StaticLibs, "libm", "libc", "libdl")
1288 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001289 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
1290 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
1291 // move them to the beginning of deps.LateStaticLibs
1292 var groupLibs []string
1293 depNames.StaticLibs, groupLibs = filterList(depNames.StaticLibs,
1294 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
1295 depNames.LateStaticLibs = append(groupLibs, depNames.LateStaticLibs...)
1296 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001297 }
Colin Cross21b9a242015-03-24 14:15:58 -07001298 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -08001299}
1300
Colin Cross97ba0732015-03-23 17:50:24 -07001301func NewCCBinary(binary *CCBinary, module CCModuleType,
Colin Cross1f8f2342015-03-26 16:09:47 -07001302 hod common.HostOrDeviceSupported, props ...interface{}) (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001303
Colin Cross1f8f2342015-03-26 16:09:47 -07001304 props = append(props, &binary.BinaryProperties)
1305
Colin Crossfa138792015-04-24 17:31:52 -07001306 return newCCDynamic(&binary.CCLinked, module, hod, common.MultilibFirst, props...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001307}
1308
Colin Cross97ba0732015-03-23 17:50:24 -07001309func CCBinaryFactory() (blueprint.Module, []interface{}) {
1310 module := &CCBinary{}
1311
1312 return NewCCBinary(module, module, common.HostAndDeviceSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001313}
1314
Colin Cross18b6dc52015-04-28 13:20:37 -07001315func (c *CCBinary) ModifyProperties(ctx common.AndroidBaseContext) {
Colin Cross0af4b842015-04-30 16:36:18 -07001316 if ctx.Darwin() {
1317 c.BinaryProperties.Static_executable = false
1318 }
Colin Cross18b6dc52015-04-28 13:20:37 -07001319 if c.BinaryProperties.Static_executable {
1320 c.dynamicProperties.VariantIsStaticBinary = true
1321 }
1322}
1323
Colin Cross0676e2d2015-04-24 17:39:18 -07001324func (c *CCBinary) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Crosse11befc2015-04-27 17:49:17 -07001325 flags = c.CCLinked.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001326
Colin Cross97ba0732015-03-23 17:50:24 -07001327 flags.CFlags = append(flags.CFlags, "-fpie")
1328
Colin Crossf6566ed2015-03-24 11:13:38 -07001329 if ctx.Device() {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001330 if c.BinaryProperties.Static_executable {
1331 // Clang driver needs -static to create static executable.
1332 // However, bionic/linker uses -shared to overwrite.
1333 // Linker for x86 targets does not allow coexistance of -static and -shared,
1334 // so we add -static only if -shared is not used.
1335 if !inList("-shared", flags.LdFlags) {
1336 flags.LdFlags = append(flags.LdFlags, "-static")
1337 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001338
Colin Crossed4cf0b2015-03-26 14:43:45 -07001339 flags.LdFlags = append(flags.LdFlags,
1340 "-nostdlib",
1341 "-Bstatic",
1342 "-Wl,--gc-sections",
1343 )
1344
1345 } else {
1346 linker := "/system/bin/linker"
1347 if flags.Toolchain.Is64Bit() {
1348 linker = "/system/bin/linker64"
1349 }
1350
1351 flags.LdFlags = append(flags.LdFlags,
1352 "-nostdlib",
1353 "-Bdynamic",
1354 fmt.Sprintf("-Wl,-dynamic-linker,%s", linker),
1355 "-Wl,--gc-sections",
1356 "-Wl,-z,nocopyreloc",
1357 )
1358 }
Colin Cross0af4b842015-04-30 16:36:18 -07001359 } else if ctx.Darwin() {
1360 flags.LdFlags = append(flags.LdFlags, "-Wl,-headerpad_max_install_names")
Colin Cross3f40fa42015-01-30 17:27:36 -08001361 }
1362
Colin Cross97ba0732015-03-23 17:50:24 -07001363 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001364}
1365
Colin Cross97ba0732015-03-23 17:50:24 -07001366func (c *CCBinary) compileModule(ctx common.AndroidModuleContext,
1367 flags CCFlags, deps CCDeps, objFiles []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001368
Colin Crossfa138792015-04-24 17:31:52 -07001369 if !c.BinaryProperties.Static_executable && inList("libc", c.Properties.Static_libs) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001370 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
1371 "from static libs or set static_executable: true")
1372 }
1373
1374 outputFile := filepath.Join(common.ModuleOutDir(ctx), c.getStem(ctx))
Dan Albertc403f7c2015-03-18 14:01:18 -07001375 c.out = outputFile
Colin Crossbfae8852015-03-26 14:44:11 -07001376 if c.BinaryProperties.Prefix_symbols != "" {
1377 afterPrefixSymbols := outputFile
1378 outputFile = outputFile + ".intermediate"
1379 TransformBinaryPrefixSymbols(ctx, c.BinaryProperties.Prefix_symbols, outputFile,
1380 ccFlagsToBuilderFlags(flags), afterPrefixSymbols)
1381 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001382
Colin Cross97ba0732015-03-23 17:50:24 -07001383 TransformObjToDynamicBinary(ctx, objFiles, deps.SharedLibs, deps.StaticLibs,
Colin Crossed4cf0b2015-03-26 14:43:45 -07001384 deps.LateStaticLibs, deps.WholeStaticLibs, deps.CrtBegin, deps.CrtEnd, true,
Colin Cross77b00fa2015-03-16 16:15:49 -07001385 ccFlagsToBuilderFlags(flags), outputFile)
Dan Albertc403f7c2015-03-18 14:01:18 -07001386}
Colin Cross3f40fa42015-01-30 17:27:36 -08001387
Colin Cross97ba0732015-03-23 17:50:24 -07001388func (c *CCBinary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Colin Crossd350ecd2015-04-28 13:25:36 -07001389 c.installFile = ctx.InstallFile(filepath.Join("bin", c.Properties.Relative_install_path), c.out)
1390}
1391
1392func (c *CCBinary) HostToolPath() string {
1393 if c.HostOrDevice().Host() {
1394 return c.installFile
1395 }
1396 return ""
Dan Albertc403f7c2015-03-18 14:01:18 -07001397}
1398
Colin Cross9ffb4f52015-04-24 17:48:09 -07001399type CCTest struct {
Colin Cross97ba0732015-03-23 17:50:24 -07001400 CCBinary
Colin Cross6b290692015-03-19 14:05:33 -07001401
Colin Cross9ffb4f52015-04-24 17:48:09 -07001402 TestProperties struct {
Colin Cross6b290692015-03-19 14:05:33 -07001403 // test_per_src: Create a separate test for each source file. Useful when there is
1404 // global state that can not be torn down and reset between each test suite.
1405 Test_per_src bool
1406 }
Dan Albertc403f7c2015-03-18 14:01:18 -07001407}
1408
Colin Cross9ffb4f52015-04-24 17:48:09 -07001409func (c *CCTest) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Cross0676e2d2015-04-24 17:39:18 -07001410 flags = c.CCBinary.flags(ctx, flags)
Dan Albertc403f7c2015-03-18 14:01:18 -07001411
Colin Cross97ba0732015-03-23 17:50:24 -07001412 flags.CFlags = append(flags.CFlags, "-DGTEST_HAS_STD_STRING")
Colin Crossf6566ed2015-03-24 11:13:38 -07001413 if ctx.Host() {
Colin Cross97ba0732015-03-23 17:50:24 -07001414 flags.CFlags = append(flags.CFlags, "-O0", "-g")
Colin Cross28344522015-04-22 13:07:53 -07001415 flags.LdFlags = append(flags.LdFlags, "-lpthread")
Dan Albertc403f7c2015-03-18 14:01:18 -07001416 }
1417
1418 // TODO(danalbert): Make gtest export its dependencies.
Colin Cross28344522015-04-22 13:07:53 -07001419 flags.CFlags = append(flags.CFlags,
1420 "-I"+filepath.Join(ctx.AConfig().SrcDir(), "external/gtest/include"))
Dan Albertc403f7c2015-03-18 14:01:18 -07001421
Colin Cross21b9a242015-03-24 14:15:58 -07001422 return flags
Dan Albertc403f7c2015-03-18 14:01:18 -07001423}
1424
Colin Cross9ffb4f52015-04-24 17:48:09 -07001425func (c *CCTest) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Cross21b9a242015-03-24 14:15:58 -07001426 depNames.StaticLibs = append(depNames.StaticLibs, "libgtest", "libgtest_main")
Colin Crossa8a93d32015-04-28 13:26:49 -07001427 depNames = c.CCBinary.depNames(ctx, depNames)
Colin Cross21b9a242015-03-24 14:15:58 -07001428 return depNames
Dan Albertc403f7c2015-03-18 14:01:18 -07001429}
1430
Colin Cross9ffb4f52015-04-24 17:48:09 -07001431func (c *CCTest) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Colin Crossf6566ed2015-03-24 11:13:38 -07001432 if ctx.Device() {
Colin Crossa8a93d32015-04-28 13:26:49 -07001433 ctx.InstallFile("../data/nativetest"+ctx.Arch().ArchType.Multilib[3:]+"/"+ctx.ModuleName(), c.out)
Dan Albertc403f7c2015-03-18 14:01:18 -07001434 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001435 c.CCBinary.installModule(ctx, flags)
Dan Albertc403f7c2015-03-18 14:01:18 -07001436 }
1437}
1438
Colin Cross9ffb4f52015-04-24 17:48:09 -07001439func (c *CCTest) testPerSrc() bool {
1440 return c.TestProperties.Test_per_src
Colin Cross6b290692015-03-19 14:05:33 -07001441}
1442
Colin Cross9ffb4f52015-04-24 17:48:09 -07001443func (c *CCTest) test() *CCTest {
1444 return c
1445}
1446
1447func NewCCTest(test *CCTest, module CCModuleType,
1448 hod common.HostOrDeviceSupported, props ...interface{}) (blueprint.Module, []interface{}) {
1449
1450 props = append(props, &test.TestProperties)
1451
1452 return NewCCBinary(&test.CCBinary, module, hod, props...)
1453}
1454
1455func CCTestFactory() (blueprint.Module, []interface{}) {
1456 module := &CCTest{}
1457
1458 return NewCCTest(module, module, common.HostAndDeviceSupported)
1459}
1460
1461type testPerSrc interface {
1462 test() *CCTest
1463 testPerSrc() bool
1464}
1465
1466var _ testPerSrc = (*CCTest)(nil)
1467
Colin Cross6b290692015-03-19 14:05:33 -07001468func TestPerSrcMutator(mctx blueprint.EarlyMutatorContext) {
Colin Cross9ffb4f52015-04-24 17:48:09 -07001469 if test, ok := mctx.Module().(testPerSrc); ok {
1470 if test.testPerSrc() {
1471 testNames := make([]string, len(test.test().Properties.Srcs))
1472 for i, src := range test.test().Properties.Srcs {
Colin Cross6b290692015-03-19 14:05:33 -07001473 testNames[i] = strings.TrimSuffix(src, filepath.Ext(src))
1474 }
1475 tests := mctx.CreateLocalVariations(testNames...)
Colin Cross9ffb4f52015-04-24 17:48:09 -07001476 for i, src := range test.test().Properties.Srcs {
1477 tests[i].(testPerSrc).test().Properties.Srcs = []string{src}
1478 tests[i].(testPerSrc).test().BinaryProperties.Stem = testNames[i]
Colin Cross6b290692015-03-19 14:05:33 -07001479 }
1480 }
1481 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001482}
1483
Colin Cross2ba19d92015-05-07 15:44:20 -07001484type CCBenchmark struct {
1485 CCBinary
1486}
1487
1488func (c *CCBenchmark) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
1489 depNames = c.CCBinary.depNames(ctx, depNames)
1490 depNames.StaticLibs = append(depNames.StaticLibs, "libbenchmark")
1491 return depNames
1492}
1493
1494func (c *CCBenchmark) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
1495 if ctx.Device() {
1496 ctx.InstallFile("../data/nativetest"+ctx.Arch().ArchType.Multilib[3:]+"/"+ctx.ModuleName(), c.out)
1497 } else {
1498 c.CCBinary.installModule(ctx, flags)
1499 }
1500}
1501
1502func NewCCBenchmark(test *CCBenchmark, module CCModuleType,
1503 hod common.HostOrDeviceSupported, props ...interface{}) (blueprint.Module, []interface{}) {
1504
1505 return NewCCBinary(&test.CCBinary, module, hod, props...)
1506}
1507
1508func CCBenchmarkFactory() (blueprint.Module, []interface{}) {
1509 module := &CCBenchmark{}
1510
1511 return NewCCBenchmark(module, module, common.HostAndDeviceSupported)
1512}
1513
Colin Cross3f40fa42015-01-30 17:27:36 -08001514//
1515// Static library
1516//
1517
Colin Cross97ba0732015-03-23 17:50:24 -07001518func CCLibraryStaticFactory() (blueprint.Module, []interface{}) {
1519 module := &CCLibrary{}
1520 module.LibraryProperties.BuildStatic = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001521
Colin Cross97ba0732015-03-23 17:50:24 -07001522 return NewCCLibrary(module, module, common.HostAndDeviceSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001523}
1524
1525//
1526// Shared libraries
1527//
1528
Colin Cross97ba0732015-03-23 17:50:24 -07001529func CCLibrarySharedFactory() (blueprint.Module, []interface{}) {
1530 module := &CCLibrary{}
1531 module.LibraryProperties.BuildShared = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001532
Colin Cross97ba0732015-03-23 17:50:24 -07001533 return NewCCLibrary(module, module, common.HostAndDeviceSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001534}
1535
1536//
1537// Host static library
1538//
1539
Colin Cross97ba0732015-03-23 17:50:24 -07001540func CCLibraryHostStaticFactory() (blueprint.Module, []interface{}) {
1541 module := &CCLibrary{}
1542 module.LibraryProperties.BuildStatic = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001543
Colin Cross97ba0732015-03-23 17:50:24 -07001544 return NewCCLibrary(module, module, common.HostSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001545}
1546
1547//
1548// Host Shared libraries
1549//
1550
Colin Cross97ba0732015-03-23 17:50:24 -07001551func CCLibraryHostSharedFactory() (blueprint.Module, []interface{}) {
1552 module := &CCLibrary{}
1553 module.LibraryProperties.BuildShared = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001554
Colin Cross97ba0732015-03-23 17:50:24 -07001555 return NewCCLibrary(module, module, common.HostSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001556}
1557
1558//
1559// Host Binaries
1560//
1561
Colin Cross97ba0732015-03-23 17:50:24 -07001562func CCBinaryHostFactory() (blueprint.Module, []interface{}) {
1563 module := &CCBinary{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001564
Colin Cross97ba0732015-03-23 17:50:24 -07001565 return NewCCBinary(module, module, common.HostSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001566}
1567
1568//
Colin Cross1f8f2342015-03-26 16:09:47 -07001569// Host Tests
1570//
1571
1572func CCTestHostFactory() (blueprint.Module, []interface{}) {
Colin Cross9ffb4f52015-04-24 17:48:09 -07001573 module := &CCTest{}
Colin Cross1f8f2342015-03-26 16:09:47 -07001574 return NewCCBinary(&module.CCBinary, module, common.HostSupported,
Colin Cross9ffb4f52015-04-24 17:48:09 -07001575 &module.TestProperties)
Colin Cross1f8f2342015-03-26 16:09:47 -07001576}
1577
1578//
Colin Cross2ba19d92015-05-07 15:44:20 -07001579// Host Benchmarks
1580//
1581
1582func CCBenchmarkHostFactory() (blueprint.Module, []interface{}) {
1583 module := &CCBenchmark{}
1584 return NewCCBinary(&module.CCBinary, module, common.HostSupported)
1585}
1586
1587//
Colin Cross3f40fa42015-01-30 17:27:36 -08001588// Device libraries shipped with gcc
1589//
1590
1591type toolchainLibrary struct {
Colin Cross97ba0732015-03-23 17:50:24 -07001592 CCLibrary
Colin Cross3f40fa42015-01-30 17:27:36 -08001593}
1594
1595func (*toolchainLibrary) AndroidDynamicDependencies(ctx common.AndroidDynamicDependerModuleContext) []string {
1596 // toolchain libraries can't have any dependencies
1597 return nil
1598}
1599
Colin Cross0676e2d2015-04-24 17:39:18 -07001600func (*toolchainLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Cross3f40fa42015-01-30 17:27:36 -08001601 // toolchain libraries can't have any dependencies
Colin Cross21b9a242015-03-24 14:15:58 -07001602 return CCDeps{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001603}
1604
Colin Cross97ba0732015-03-23 17:50:24 -07001605func ToolchainLibraryFactory() (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001606 module := &toolchainLibrary{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001607
Colin Cross97ba0732015-03-23 17:50:24 -07001608 module.LibraryProperties.BuildStatic = true
1609
Colin Crossfa138792015-04-24 17:31:52 -07001610 return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth,
Colin Cross21b9a242015-03-24 14:15:58 -07001611 &module.LibraryProperties)
Colin Cross3f40fa42015-01-30 17:27:36 -08001612}
1613
1614func (c *toolchainLibrary) compileModule(ctx common.AndroidModuleContext,
Colin Cross97ba0732015-03-23 17:50:24 -07001615 flags CCFlags, deps CCDeps, objFiles []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001616
1617 libName := ctx.ModuleName() + staticLibraryExtension
1618 outputFile := filepath.Join(common.ModuleOutDir(ctx), libName)
1619
1620 CopyGccLib(ctx, libName, ccFlagsToBuilderFlags(flags), outputFile)
1621
1622 c.out = outputFile
1623
1624 ctx.CheckbuildFile(outputFile)
1625}
1626
Colin Cross97ba0732015-03-23 17:50:24 -07001627func (c *toolchainLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001628 // Toolchain libraries do not get installed.
1629}
1630
Dan Albertbe961682015-03-18 23:38:50 -07001631// NDK prebuilt libraries.
1632//
1633// These differ from regular prebuilts in that they aren't stripped and usually aren't installed
1634// either (with the exception of the shared STLs, which are installed to the app's directory rather
1635// than to the system image).
1636
1637func getNdkLibDir(ctx common.AndroidModuleContext, toolchain Toolchain, version string) string {
1638 return fmt.Sprintf("%s/prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/lib",
Colin Cross1332b002015-04-07 17:11:30 -07001639 ctx.AConfig().SrcDir(), version, toolchain.Name())
Dan Albertbe961682015-03-18 23:38:50 -07001640}
1641
Dan Albertc3144b12015-04-28 18:17:56 -07001642func ndkPrebuiltModuleToPath(ctx common.AndroidModuleContext, toolchain Toolchain,
1643 ext string, version string) string {
1644
1645 // NDK prebuilts are named like: ndk_NAME.EXT.SDK_VERSION.
1646 // We want to translate to just NAME.EXT
1647 name := strings.Split(strings.TrimPrefix(ctx.ModuleName(), "ndk_"), ".")[0]
1648 dir := getNdkLibDir(ctx, toolchain, version)
1649 return filepath.Join(dir, name+ext)
1650}
1651
1652type ndkPrebuiltObject struct {
1653 ccObject
1654}
1655
1656func (*ndkPrebuiltObject) AndroidDynamicDependencies(
1657 ctx common.AndroidDynamicDependerModuleContext) []string {
1658
1659 // NDK objects can't have any dependencies
1660 return nil
1661}
1662
1663func (*ndkPrebuiltObject) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
1664 // NDK objects can't have any dependencies
1665 return CCDeps{}
1666}
1667
1668func NdkPrebuiltObjectFactory() (blueprint.Module, []interface{}) {
1669 module := &ndkPrebuiltObject{}
1670 return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth)
1671}
1672
1673func (c *ndkPrebuiltObject) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
1674 deps CCDeps, objFiles []string) {
1675 // A null build step, but it sets up the output path.
1676 if !strings.HasPrefix(ctx.ModuleName(), "ndk_crt") {
1677 ctx.ModuleErrorf("NDK prebuilts must have an ndk_crt prefixed name")
1678 }
1679
1680 c.out = ndkPrebuiltModuleToPath(ctx, flags.Toolchain, objectExtension, c.Properties.Sdk_version)
1681}
1682
1683func (c *ndkPrebuiltObject) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
1684 // Objects do not get installed.
1685}
1686
1687var _ ccObjectProvider = (*ndkPrebuiltObject)(nil)
1688
Dan Albertbe961682015-03-18 23:38:50 -07001689type ndkPrebuiltLibrary struct {
1690 CCLibrary
1691}
1692
1693func (*ndkPrebuiltLibrary) AndroidDynamicDependencies(
1694 ctx common.AndroidDynamicDependerModuleContext) []string {
1695
1696 // NDK libraries can't have any dependencies
1697 return nil
1698}
1699
Colin Cross0676e2d2015-04-24 17:39:18 -07001700func (*ndkPrebuiltLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Dan Albertbe961682015-03-18 23:38:50 -07001701 // NDK libraries can't have any dependencies
1702 return CCDeps{}
1703}
1704
1705func NdkPrebuiltLibraryFactory() (blueprint.Module, []interface{}) {
1706 module := &ndkPrebuiltLibrary{}
1707 module.LibraryProperties.BuildShared = true
1708 return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
1709}
1710
1711func (c *ndkPrebuiltLibrary) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
1712 deps CCDeps, objFiles []string) {
1713 // A null build step, but it sets up the output path.
1714 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
1715 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
1716 }
1717
Colin Crossfa138792015-04-24 17:31:52 -07001718 includeDirs := pathtools.PrefixPaths(c.Properties.Export_include_dirs, common.ModuleSrcDir(ctx))
Colin Cross28344522015-04-22 13:07:53 -07001719 c.exportFlags = []string{common.JoinWithPrefix(includeDirs, "-isystem ")}
Dan Albertbe961682015-03-18 23:38:50 -07001720
Dan Albertc3144b12015-04-28 18:17:56 -07001721 c.out = ndkPrebuiltModuleToPath(ctx, flags.Toolchain, sharedLibraryExtension,
1722 c.Properties.Sdk_version)
Dan Albertbe961682015-03-18 23:38:50 -07001723}
1724
1725func (c *ndkPrebuiltLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc3144b12015-04-28 18:17:56 -07001726 // NDK prebuilt libraries do not get installed.
Dan Albertbe961682015-03-18 23:38:50 -07001727}
1728
1729// The NDK STLs are slightly different from the prebuilt system libraries:
1730// * Are not specific to each platform version.
1731// * The libraries are not in a predictable location for each STL.
1732
1733type ndkPrebuiltStl struct {
1734 ndkPrebuiltLibrary
1735}
1736
1737type ndkPrebuiltStaticStl struct {
1738 ndkPrebuiltStl
1739}
1740
1741type ndkPrebuiltSharedStl struct {
1742 ndkPrebuiltStl
1743}
1744
1745func NdkPrebuiltSharedStlFactory() (blueprint.Module, []interface{}) {
1746 module := &ndkPrebuiltSharedStl{}
1747 module.LibraryProperties.BuildShared = true
1748 return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
1749}
1750
1751func NdkPrebuiltStaticStlFactory() (blueprint.Module, []interface{}) {
1752 module := &ndkPrebuiltStaticStl{}
1753 module.LibraryProperties.BuildStatic = true
1754 return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
1755}
1756
1757func getNdkStlLibDir(ctx common.AndroidModuleContext, toolchain Toolchain, stl string) string {
1758 gccVersion := toolchain.GccVersion()
1759 var libDir string
1760 switch stl {
1761 case "libstlport":
1762 libDir = "cxx-stl/stlport/libs"
1763 case "libc++":
1764 libDir = "cxx-stl/llvm-libc++/libs"
1765 case "libgnustl":
1766 libDir = fmt.Sprintf("cxx-stl/gnu-libstdc++/%s/libs", gccVersion)
1767 }
1768
1769 if libDir != "" {
Colin Cross1332b002015-04-07 17:11:30 -07001770 ndkSrcRoot := ctx.AConfig().SrcDir() + "/prebuilts/ndk/current/sources"
Dan Albertbe961682015-03-18 23:38:50 -07001771 return fmt.Sprintf("%s/%s/%s", ndkSrcRoot, libDir, ctx.Arch().Abi)
1772 }
1773
1774 ctx.ModuleErrorf("Unknown NDK STL: %s", stl)
1775 return ""
1776}
1777
1778func (c *ndkPrebuiltStl) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
1779 deps CCDeps, objFiles []string) {
1780 // A null build step, but it sets up the output path.
1781 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
1782 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
1783 }
1784
Colin Crossfa138792015-04-24 17:31:52 -07001785 includeDirs := pathtools.PrefixPaths(c.Properties.Export_include_dirs, common.ModuleSrcDir(ctx))
Colin Cross28344522015-04-22 13:07:53 -07001786 c.exportFlags = []string{includeDirsToFlags(includeDirs)}
Dan Albertbe961682015-03-18 23:38:50 -07001787
1788 libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_")
1789 libExt := sharedLibraryExtension
1790 if c.LibraryProperties.BuildStatic {
1791 libExt = staticLibraryExtension
1792 }
1793
1794 stlName := strings.TrimSuffix(libName, "_shared")
1795 stlName = strings.TrimSuffix(stlName, "_static")
1796 libDir := getNdkStlLibDir(ctx, flags.Toolchain, stlName)
1797 c.out = libDir + "/" + libName + libExt
1798}
1799
Colin Cross3f40fa42015-01-30 17:27:36 -08001800func LinkageMutator(mctx blueprint.EarlyMutatorContext) {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001801 if c, ok := mctx.Module().(ccLinkedInterface); ok {
Colin Cross3f40fa42015-01-30 17:27:36 -08001802 var modules []blueprint.Module
Colin Crossed4cf0b2015-03-26 14:43:45 -07001803 if c.buildStatic() && c.buildShared() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001804 modules = mctx.CreateLocalVariations("static", "shared")
Colin Cross18b6dc52015-04-28 13:20:37 -07001805 modules[0].(ccLinkedInterface).setStatic(true)
1806 modules[1].(ccLinkedInterface).setStatic(false)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001807 } else if c.buildStatic() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001808 modules = mctx.CreateLocalVariations("static")
Colin Cross18b6dc52015-04-28 13:20:37 -07001809 modules[0].(ccLinkedInterface).setStatic(true)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001810 } else if c.buildShared() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001811 modules = mctx.CreateLocalVariations("shared")
Colin Cross18b6dc52015-04-28 13:20:37 -07001812 modules[0].(ccLinkedInterface).setStatic(false)
Colin Cross3f40fa42015-01-30 17:27:36 -08001813 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001814 panic(fmt.Errorf("ccLibrary %q not static or shared", mctx.ModuleName()))
Colin Cross3f40fa42015-01-30 17:27:36 -08001815 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001816
1817 if _, ok := c.(ccLibraryInterface); ok {
1818 reuseFrom := modules[0].(ccLibraryInterface)
1819 for _, m := range modules {
1820 m.(ccLibraryInterface).setReuseFrom(reuseFrom)
Colin Cross3f40fa42015-01-30 17:27:36 -08001821 }
1822 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001823 }
1824}
Colin Cross74d1ec02015-04-28 13:30:13 -07001825
1826// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
1827// modifies the slice contents in place, and returns a subslice of the original slice
1828func lastUniqueElements(list []string) []string {
1829 totalSkip := 0
1830 for i := len(list) - 1; i >= totalSkip; i-- {
1831 skip := 0
1832 for j := i - 1; j >= totalSkip; j-- {
1833 if list[i] == list[j] {
1834 skip++
1835 } else {
1836 list[j+skip] = list[j]
1837 }
1838 }
1839 totalSkip += skip
1840 }
1841 return list[totalSkip:]
1842}