blob: b497d66e3c19c8161341ddfaf327d9d1cbd72a19 [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"
24 "strings"
25
Colin Cross97ba0732015-03-23 17:50:24 -070026 "github.com/google/blueprint"
Colin Cross06a931b2015-10-28 17:23:31 -070027 "github.com/google/blueprint/proptools"
Colin Cross97ba0732015-03-23 17:50:24 -070028
Colin Cross463a90e2015-06-17 14:20:06 -070029 "android/soong"
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 Cross463a90e2015-06-17 14:20:06 -070034func init() {
35 soong.RegisterModuleType("cc_library_static", CCLibraryStaticFactory)
36 soong.RegisterModuleType("cc_library_shared", CCLibrarySharedFactory)
37 soong.RegisterModuleType("cc_library", CCLibraryFactory)
38 soong.RegisterModuleType("cc_object", CCObjectFactory)
39 soong.RegisterModuleType("cc_binary", CCBinaryFactory)
40 soong.RegisterModuleType("cc_test", CCTestFactory)
41 soong.RegisterModuleType("cc_benchmark", CCBenchmarkFactory)
Colin Crosscfad1192015-11-02 16:43:11 -080042 soong.RegisterModuleType("cc_defaults", CCDefaultsFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070043
44 soong.RegisterModuleType("toolchain_library", ToolchainLibraryFactory)
45 soong.RegisterModuleType("ndk_prebuilt_library", NdkPrebuiltLibraryFactory)
46 soong.RegisterModuleType("ndk_prebuilt_object", NdkPrebuiltObjectFactory)
47 soong.RegisterModuleType("ndk_prebuilt_static_stl", NdkPrebuiltStaticStlFactory)
48 soong.RegisterModuleType("ndk_prebuilt_shared_stl", NdkPrebuiltSharedStlFactory)
49
50 soong.RegisterModuleType("cc_library_host_static", CCLibraryHostStaticFactory)
51 soong.RegisterModuleType("cc_library_host_shared", CCLibraryHostSharedFactory)
52 soong.RegisterModuleType("cc_binary_host", CCBinaryHostFactory)
53 soong.RegisterModuleType("cc_test_host", CCTestHostFactory)
54 soong.RegisterModuleType("cc_benchmark_host", CCBenchmarkHostFactory)
55
56 // LinkageMutator must be registered after common.ArchMutator, but that is guaranteed by
57 // the Go initialization order because this package depends on common, so common's init
58 // functions will run first.
Colin Cross6362e272015-10-29 15:25:03 -070059 common.RegisterBottomUpMutator("link", linkageMutator)
60 common.RegisterBottomUpMutator("test_per_src", testPerSrcMutator)
61 common.RegisterBottomUpMutator("deps", depsMutator)
Colin Cross463a90e2015-06-17 14:20:06 -070062}
63
Colin Cross3f40fa42015-01-30 17:27:36 -080064var (
Colin Cross1332b002015-04-07 17:11:30 -070065 HostPrebuiltTag = pctx.VariableConfigMethod("HostPrebuiltTag", common.Config.PrebuiltOS)
Colin Cross3f40fa42015-01-30 17:27:36 -080066
Dan Willemsen34cc69e2015-09-23 15:26:20 -070067 LibcRoot = pctx.SourcePathVariable("LibcRoot", "bionic/libc")
68 LibmRoot = pctx.SourcePathVariable("LibmRoot", "bionic/libm")
Colin Cross3f40fa42015-01-30 17:27:36 -080069)
70
71// Flags used by lots of devices. Putting them in package static variables will save bytes in
72// build.ninja so they aren't repeated for every file
73var (
74 commonGlobalCflags = []string{
75 "-DANDROID",
76 "-fmessage-length=0",
77 "-W",
78 "-Wall",
79 "-Wno-unused",
80 "-Winit-self",
81 "-Wpointer-arith",
Dan Willemsene6540452015-10-20 15:21:33 -070082 "-fdebug-prefix-map=/proc/self/cwd=",
Colin Cross3f40fa42015-01-30 17:27:36 -080083
84 // COMMON_RELEASE_CFLAGS
85 "-DNDEBUG",
86 "-UDEBUG",
87 }
88
89 deviceGlobalCflags = []string{
Dan Willemsen490fd492015-11-24 17:53:15 -080090 "-fdiagnostics-color",
91
Colin Cross3f40fa42015-01-30 17:27:36 -080092 // TARGET_ERROR_FLAGS
93 "-Werror=return-type",
94 "-Werror=non-virtual-dtor",
95 "-Werror=address",
96 "-Werror=sequence-point",
97 }
98
99 hostGlobalCflags = []string{}
100
101 commonGlobalCppflags = []string{
102 "-Wsign-promo",
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700103 }
104
105 illegalFlags = []string{
106 "-w",
Colin Cross3f40fa42015-01-30 17:27:36 -0800107 }
108)
109
110func init() {
111 pctx.StaticVariable("commonGlobalCflags", strings.Join(commonGlobalCflags, " "))
112 pctx.StaticVariable("deviceGlobalCflags", strings.Join(deviceGlobalCflags, " "))
113 pctx.StaticVariable("hostGlobalCflags", strings.Join(hostGlobalCflags, " "))
114
115 pctx.StaticVariable("commonGlobalCppflags", strings.Join(commonGlobalCppflags, " "))
116
117 pctx.StaticVariable("commonClangGlobalCflags",
118 strings.Join(clangFilterUnknownCflags(commonGlobalCflags), " "))
119 pctx.StaticVariable("deviceClangGlobalCflags",
120 strings.Join(clangFilterUnknownCflags(deviceGlobalCflags), " "))
121 pctx.StaticVariable("hostClangGlobalCflags",
122 strings.Join(clangFilterUnknownCflags(hostGlobalCflags), " "))
Tim Kilbournf2948142015-03-11 12:03:03 -0700123 pctx.StaticVariable("commonClangGlobalCppflags",
124 strings.Join(clangFilterUnknownCflags(commonGlobalCppflags), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800125
126 // Everything in this list is a crime against abstraction and dependency tracking.
127 // Do not add anything to this list.
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700128 pctx.PrefixedPathsForSourceVariable("commonGlobalIncludes", "-isystem ",
129 []string{
130 "system/core/include",
131 "hardware/libhardware/include",
132 "hardware/libhardware_legacy/include",
133 "hardware/ril/include",
134 "libnativehelper/include",
135 "frameworks/native/include",
136 "frameworks/native/opengl/include",
137 "frameworks/av/include",
138 "frameworks/base/include",
139 })
Colin Cross3f40fa42015-01-30 17:27:36 -0800140
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700141 pctx.SourcePathVariable("clangPath", "prebuilts/clang/host/${HostPrebuiltTag}/3.8/bin")
Colin Cross3f40fa42015-01-30 17:27:36 -0800142}
143
Colin Cross6362e272015-10-29 15:25:03 -0700144type CCModuleContext common.AndroidBaseContext
145
Colin Cross3f40fa42015-01-30 17:27:36 -0800146// Building C/C++ code is handled by objects that satisfy this interface via composition
Colin Cross97ba0732015-03-23 17:50:24 -0700147type CCModuleType interface {
Colin Cross3f40fa42015-01-30 17:27:36 -0800148 common.AndroidModule
149
Colin Crossfa138792015-04-24 17:31:52 -0700150 // Modify property values after parsing Blueprints file but before starting dependency
151 // resolution or build rule generation
Colin Cross6362e272015-10-29 15:25:03 -0700152 ModifyProperties(CCModuleContext)
Colin Crossfa138792015-04-24 17:31:52 -0700153
Colin Cross21b9a242015-03-24 14:15:58 -0700154 // Modify the ccFlags
Colin Cross0676e2d2015-04-24 17:39:18 -0700155 flags(common.AndroidModuleContext, CCFlags) CCFlags
Colin Cross3f40fa42015-01-30 17:27:36 -0800156
Colin Cross6362e272015-10-29 15:25:03 -0700157 // Return list of dependency names for use in depsMutator
Colin Cross0676e2d2015-04-24 17:39:18 -0700158 depNames(common.AndroidBaseContext, CCDeps) CCDeps
Colin Cross3f40fa42015-01-30 17:27:36 -0800159
Colin Cross6362e272015-10-29 15:25:03 -0700160 // Add dynamic dependencies
161 depsMutator(common.AndroidBottomUpMutatorContext)
162
Colin Cross3f40fa42015-01-30 17:27:36 -0800163 // Compile objects into final module
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700164 compileModule(common.AndroidModuleContext, CCFlags, CCPathDeps, common.Paths)
Colin Cross3f40fa42015-01-30 17:27:36 -0800165
Dan Albertc403f7c2015-03-18 14:01:18 -0700166 // Install the built module.
Colin Cross97ba0732015-03-23 17:50:24 -0700167 installModule(common.AndroidModuleContext, CCFlags)
Dan Albertc403f7c2015-03-18 14:01:18 -0700168
Colin Cross3f40fa42015-01-30 17:27:36 -0800169 // Return the output file (.o, .a or .so) for use by other modules
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700170 outputFile() common.OptionalPath
Colin Cross3f40fa42015-01-30 17:27:36 -0800171}
172
Colin Cross97ba0732015-03-23 17:50:24 -0700173type CCDeps struct {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700174 StaticLibs, SharedLibs, LateStaticLibs, WholeStaticLibs []string
Colin Crossc472d572015-03-17 15:06:21 -0700175
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700176 ObjFiles common.Paths
177
178 Cflags, ReexportedCflags []string
Colin Cross21b9a242015-03-24 14:15:58 -0700179
Colin Cross97ba0732015-03-23 17:50:24 -0700180 CrtBegin, CrtEnd string
Colin Crossc472d572015-03-17 15:06:21 -0700181}
182
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700183type CCPathDeps struct {
184 StaticLibs, SharedLibs, LateStaticLibs, WholeStaticLibs common.Paths
185
186 ObjFiles common.Paths
187 WholeStaticLibObjFiles common.Paths
188
189 Cflags, ReexportedCflags []string
190
191 CrtBegin, CrtEnd common.OptionalPath
192}
193
Colin Cross97ba0732015-03-23 17:50:24 -0700194type CCFlags struct {
Colin Cross28344522015-04-22 13:07:53 -0700195 GlobalFlags []string // Flags that apply to C, C++, and assembly source files
196 AsFlags []string // Flags that apply to assembly source files
197 CFlags []string // Flags that apply to C and C++ source files
198 ConlyFlags []string // Flags that apply to C source files
199 CppFlags []string // Flags that apply to C++ source files
200 YaccFlags []string // Flags that apply to Yacc source files
201 LdFlags []string // Flags that apply to linker command lines
202
203 Nocrt bool
204 Toolchain Toolchain
205 Clang bool
Colin Crossc472d572015-03-17 15:06:21 -0700206}
207
Colin Cross7d5136f2015-05-11 13:39:40 -0700208// Properties used to compile all C or C++ modules
209type CCBaseProperties struct {
210 // list of source files used to compile the C/C++ module. May be .c, .cpp, or .S files.
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700211 Srcs []string `android:"arch_variant"`
212
213 // list of source files that should not be used to build the C/C++ module.
214 // This is most useful in the arch/multilib variants to remove non-common files
215 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700216
217 // list of module-specific flags that will be used for C and C++ compiles.
218 Cflags []string `android:"arch_variant"`
219
220 // list of module-specific flags that will be used for C++ compiles
221 Cppflags []string `android:"arch_variant"`
222
223 // list of module-specific flags that will be used for C compiles
224 Conlyflags []string `android:"arch_variant"`
225
226 // list of module-specific flags that will be used for .S compiles
227 Asflags []string `android:"arch_variant"`
228
229 // list of module-specific flags that will be used for .y and .yy compiles
230 Yaccflags []string
231
232 // list of module-specific flags that will be used for all link steps
233 Ldflags []string `android:"arch_variant"`
234
235 // the instruction set architecture to use to compile the C/C++
236 // module.
237 Instruction_set string `android:"arch_variant"`
238
239 // list of directories relative to the root of the source tree that will
240 // be added to the include path using -I.
241 // If possible, don't use this. If adding paths from the current directory use
242 // local_include_dirs, if adding paths from other modules use export_include_dirs in
243 // that module.
244 Include_dirs []string `android:"arch_variant"`
245
Colin Cross39d97f22015-09-14 12:30:50 -0700246 // list of files relative to the root of the source tree that will be included
247 // using -include.
248 // If possible, don't use this.
249 Include_files []string `android:"arch_variant"`
250
Colin Cross7d5136f2015-05-11 13:39:40 -0700251 // list of directories relative to the Blueprints file that will
252 // be added to the include path using -I
253 Local_include_dirs []string `android:"arch_variant"`
254
Colin Cross39d97f22015-09-14 12:30:50 -0700255 // list of files relative to the Blueprints file that will be included
256 // using -include.
257 // If possible, don't use this.
258 Local_include_files []string `android:"arch_variant"`
259
Colin Cross7d5136f2015-05-11 13:39:40 -0700260 // list of directories relative to the Blueprints file that will
261 // be added to the include path using -I for any module that links against this module
262 Export_include_dirs []string `android:"arch_variant"`
263
264 // list of module-specific flags that will be used for C and C++ compiles when
265 // compiling with clang
266 Clang_cflags []string `android:"arch_variant"`
267
268 // list of module-specific flags that will be used for .S compiles when
269 // compiling with clang
270 Clang_asflags []string `android:"arch_variant"`
271
272 // list of system libraries that will be dynamically linked to
273 // shared library and executable modules. If unset, generally defaults to libc
274 // and libm. Set to [] to prevent linking against libc and libm.
275 System_shared_libs []string
276
277 // list of modules whose object files should be linked into this module
278 // in their entirety. For static library modules, all of the .o files from the intermediate
279 // directory of the dependency will be linked into this modules .a file. For a shared library,
280 // the dependency's .a file will be linked into this module using -Wl,--whole-archive.
281 Whole_static_libs []string `android:"arch_variant"`
282
283 // list of modules that should be statically linked into this module.
284 Static_libs []string `android:"arch_variant"`
285
286 // list of modules that should be dynamically linked into this module.
287 Shared_libs []string `android:"arch_variant"`
288
289 // allow the module to contain undefined symbols. By default,
290 // modules cannot contain undefined symbols that are not satisified by their immediate
291 // dependencies. Set this flag to true to remove --no-undefined from the linker flags.
292 // This flag should only be necessary for compiling low-level libraries like libc.
Colin Cross06a931b2015-10-28 17:23:31 -0700293 Allow_undefined_symbols *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700294
295 // don't link in crt_begin and crt_end. This flag should only be necessary for
296 // compiling crt or libc.
Colin Cross06a931b2015-10-28 17:23:31 -0700297 Nocrt *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700298
Dan Willemsend67be222015-09-16 15:19:33 -0700299 // don't link in libgcc.a
Colin Cross06a931b2015-10-28 17:23:31 -0700300 No_libgcc *bool
Dan Willemsend67be222015-09-16 15:19:33 -0700301
Colin Cross7d5136f2015-05-11 13:39:40 -0700302 // don't insert default compiler flags into asflags, cflags,
303 // cppflags, conlyflags, ldflags, or include_dirs
Colin Cross06a931b2015-10-28 17:23:31 -0700304 No_default_compiler_flags *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700305
306 // compile module with clang instead of gcc
Colin Cross06a931b2015-10-28 17:23:31 -0700307 Clang *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700308
309 // pass -frtti instead of -fno-rtti
Colin Cross06a931b2015-10-28 17:23:31 -0700310 Rtti *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700311
312 // -l arguments to pass to linker for host-provided shared libraries
313 Host_ldlibs []string `android:"arch_variant"`
314
315 // select the STL library to use. Possible values are "libc++", "libc++_static",
316 // "stlport", "stlport_static", "ndk", "libstdc++", or "none". Leave blank to select the
317 // default
318 Stl string
319
320 // Set for combined shared/static libraries to prevent compiling object files a second time
321 SkipCompileObjs bool `blueprint:"mutated"`
322
323 Debug, Release struct {
324 // list of module-specific flags that will be used for C and C++ compiles in debug or
325 // release builds
326 Cflags []string `android:"arch_variant"`
327 } `android:"arch_variant"`
328
329 // Minimum sdk version supported when compiling against the ndk
330 Sdk_version string
331
332 // install to a subdirectory of the default install path for the module
333 Relative_install_path string
334}
335
Colin Crosscfad1192015-11-02 16:43:11 -0800336type CCUnusedProperties struct {
337 Native_coverage *bool
338 Required []string
339 Sanitize []string `android:"arch_variant"`
340 Sanitize_recover []string
341 Strip string
342 Tags []string
343}
344
Colin Crossfa138792015-04-24 17:31:52 -0700345// CCBase contains the properties and members used by all C/C++ module types, and implements
Colin Crossc472d572015-03-17 15:06:21 -0700346// the blueprint.Module interface. It expects to be embedded into an outer specialization struct,
347// and uses a ccModuleType interface to that struct to create the build steps.
Colin Crossfa138792015-04-24 17:31:52 -0700348type CCBase struct {
Colin Crossc472d572015-03-17 15:06:21 -0700349 common.AndroidModuleBase
Colin Crosscfad1192015-11-02 16:43:11 -0800350 common.DefaultableModule
Colin Cross97ba0732015-03-23 17:50:24 -0700351 module CCModuleType
Colin Crossc472d572015-03-17 15:06:21 -0700352
Colin Cross7d5136f2015-05-11 13:39:40 -0700353 Properties CCBaseProperties
Colin Crossfa138792015-04-24 17:31:52 -0700354
Colin Crosscfad1192015-11-02 16:43:11 -0800355 unused CCUnusedProperties
Colin Crossc472d572015-03-17 15:06:21 -0700356
357 installPath string
Colin Cross74d1ec02015-04-28 13:30:13 -0700358
359 savedDepNames CCDeps
Colin Crossc472d572015-03-17 15:06:21 -0700360}
361
Colin Crossfa138792015-04-24 17:31:52 -0700362func newCCBase(base *CCBase, module CCModuleType, hod common.HostOrDeviceSupported,
Colin Crossc472d572015-03-17 15:06:21 -0700363 multilib common.Multilib, props ...interface{}) (blueprint.Module, []interface{}) {
364
365 base.module = module
366
Colin Crossfa138792015-04-24 17:31:52 -0700367 props = append(props, &base.Properties, &base.unused)
Colin Crossc472d572015-03-17 15:06:21 -0700368
Colin Crosscfad1192015-11-02 16:43:11 -0800369 _, props = common.InitAndroidArchModule(module, hod, multilib, props...)
370
371 return common.InitDefaultableModule(module, base, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700372}
373
Colin Crossfa138792015-04-24 17:31:52 -0700374func (c *CCBase) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800375 toolchain := c.findToolchain(ctx)
376 if ctx.Failed() {
377 return
378 }
379
Colin Cross21b9a242015-03-24 14:15:58 -0700380 flags := c.collectFlags(ctx, toolchain)
Colin Cross3f40fa42015-01-30 17:27:36 -0800381 if ctx.Failed() {
382 return
383 }
384
Colin Cross74d1ec02015-04-28 13:30:13 -0700385 deps := c.depsToPaths(ctx, c.savedDepNames)
Colin Cross3f40fa42015-01-30 17:27:36 -0800386 if ctx.Failed() {
387 return
388 }
389
Colin Cross28344522015-04-22 13:07:53 -0700390 flags.CFlags = append(flags.CFlags, deps.Cflags...)
Colin Crossed9f8682015-03-18 17:17:35 -0700391
Colin Cross581c1892015-04-07 16:50:10 -0700392 objFiles := c.compileObjs(ctx, flags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800393 if ctx.Failed() {
394 return
395 }
396
Colin Cross581c1892015-04-07 16:50:10 -0700397 generatedObjFiles := c.compileGeneratedObjs(ctx, flags)
Colin Cross5049f022015-03-18 13:28:46 -0700398 if ctx.Failed() {
399 return
400 }
401
402 objFiles = append(objFiles, generatedObjFiles...)
403
Colin Cross3f40fa42015-01-30 17:27:36 -0800404 c.ccModuleType().compileModule(ctx, flags, deps, objFiles)
405 if ctx.Failed() {
406 return
407 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700408
409 c.ccModuleType().installModule(ctx, flags)
410 if ctx.Failed() {
411 return
412 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800413}
414
Colin Crossfa138792015-04-24 17:31:52 -0700415func (c *CCBase) ccModuleType() CCModuleType {
Colin Cross3f40fa42015-01-30 17:27:36 -0800416 return c.module
417}
418
Colin Crossfa138792015-04-24 17:31:52 -0700419func (c *CCBase) findToolchain(ctx common.AndroidModuleContext) Toolchain {
Colin Cross3f40fa42015-01-30 17:27:36 -0800420 arch := ctx.Arch()
Colin Crossd3ba0392015-05-07 14:11:29 -0700421 hod := ctx.HostOrDevice()
Dan Willemsen490fd492015-11-24 17:53:15 -0800422 ht := ctx.HostType()
423 factory := toolchainFactories[hod][ht][arch.ArchType]
Colin Cross3f40fa42015-01-30 17:27:36 -0800424 if factory == nil {
Dan Willemsen490fd492015-11-24 17:53:15 -0800425 ctx.ModuleErrorf("Toolchain not found for %s %s arch %q", hod.String(), ht.String(), arch.String())
Colin Crosseeabb892015-11-20 13:07:51 -0800426 return nil
Colin Cross3f40fa42015-01-30 17:27:36 -0800427 }
Colin Crossc5c24ad2015-11-20 15:35:00 -0800428 return factory(arch)
Colin Cross3f40fa42015-01-30 17:27:36 -0800429}
430
Colin Cross6362e272015-10-29 15:25:03 -0700431func (c *CCBase) ModifyProperties(ctx CCModuleContext) {
Colin Crossfa138792015-04-24 17:31:52 -0700432}
433
Colin Crosse11befc2015-04-27 17:49:17 -0700434func (c *CCBase) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Crossfa138792015-04-24 17:31:52 -0700435 depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.Properties.Whole_static_libs...)
436 depNames.StaticLibs = append(depNames.StaticLibs, c.Properties.Static_libs...)
437 depNames.SharedLibs = append(depNames.SharedLibs, c.Properties.Shared_libs...)
Colin Cross21b9a242015-03-24 14:15:58 -0700438
Colin Cross21b9a242015-03-24 14:15:58 -0700439 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -0800440}
441
Colin Cross6362e272015-10-29 15:25:03 -0700442func (c *CCBase) depsMutator(ctx common.AndroidBottomUpMutatorContext) {
Colin Cross74d1ec02015-04-28 13:30:13 -0700443 c.savedDepNames = c.module.depNames(ctx, CCDeps{})
444 c.savedDepNames.WholeStaticLibs = lastUniqueElements(c.savedDepNames.WholeStaticLibs)
445 c.savedDepNames.StaticLibs = lastUniqueElements(c.savedDepNames.StaticLibs)
446 c.savedDepNames.SharedLibs = lastUniqueElements(c.savedDepNames.SharedLibs)
447
448 staticLibs := c.savedDepNames.WholeStaticLibs
449 staticLibs = append(staticLibs, c.savedDepNames.StaticLibs...)
450 staticLibs = append(staticLibs, c.savedDepNames.LateStaticLibs...)
Colin Cross21b9a242015-03-24 14:15:58 -0700451 ctx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, staticLibs...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800452
Colin Cross74d1ec02015-04-28 13:30:13 -0700453 ctx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, c.savedDepNames.SharedLibs...)
Colin Cross21b9a242015-03-24 14:15:58 -0700454
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700455 ctx.AddDependency(ctx.Module(), c.savedDepNames.ObjFiles.Strings()...)
Colin Cross74d1ec02015-04-28 13:30:13 -0700456 if c.savedDepNames.CrtBegin != "" {
Colin Cross6362e272015-10-29 15:25:03 -0700457 ctx.AddDependency(ctx.Module(), c.savedDepNames.CrtBegin)
Colin Cross21b9a242015-03-24 14:15:58 -0700458 }
Colin Cross74d1ec02015-04-28 13:30:13 -0700459 if c.savedDepNames.CrtEnd != "" {
Colin Cross6362e272015-10-29 15:25:03 -0700460 ctx.AddDependency(ctx.Module(), c.savedDepNames.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700461 }
Colin Cross6362e272015-10-29 15:25:03 -0700462}
Colin Cross21b9a242015-03-24 14:15:58 -0700463
Colin Cross6362e272015-10-29 15:25:03 -0700464func depsMutator(ctx common.AndroidBottomUpMutatorContext) {
465 if c, ok := ctx.Module().(CCModuleType); ok {
466 c.ModifyProperties(ctx)
467 c.depsMutator(ctx)
468 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800469}
470
471// Create a ccFlags struct that collects the compile flags from global values,
472// per-target values, module type values, and per-module Blueprints properties
Colin Crossfa138792015-04-24 17:31:52 -0700473func (c *CCBase) collectFlags(ctx common.AndroidModuleContext, toolchain Toolchain) CCFlags {
Colin Cross97ba0732015-03-23 17:50:24 -0700474 flags := CCFlags{
Colin Crossfa138792015-04-24 17:31:52 -0700475 CFlags: c.Properties.Cflags,
476 CppFlags: c.Properties.Cppflags,
477 ConlyFlags: c.Properties.Conlyflags,
478 LdFlags: c.Properties.Ldflags,
479 AsFlags: c.Properties.Asflags,
480 YaccFlags: c.Properties.Yaccflags,
Colin Cross06a931b2015-10-28 17:23:31 -0700481 Nocrt: Bool(c.Properties.Nocrt),
Colin Cross97ba0732015-03-23 17:50:24 -0700482 Toolchain: toolchain,
Colin Cross06a931b2015-10-28 17:23:31 -0700483 Clang: Bool(c.Properties.Clang),
Colin Cross3f40fa42015-01-30 17:27:36 -0800484 }
Colin Cross28344522015-04-22 13:07:53 -0700485
486 // Include dir cflags
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700487 rootIncludeDirs := common.PathsForSource(ctx, c.Properties.Include_dirs)
488 localIncludeDirs := common.PathsForModuleSrc(ctx, c.Properties.Local_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -0700489 flags.GlobalFlags = append(flags.GlobalFlags,
Dan Willemsen1e898b92015-09-23 15:26:32 -0700490 includeDirsToFlags(localIncludeDirs),
491 includeDirsToFlags(rootIncludeDirs))
Colin Cross28344522015-04-22 13:07:53 -0700492
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700493 rootIncludeFiles := common.PathsForSource(ctx, c.Properties.Include_files)
494 localIncludeFiles := common.PathsForModuleSrc(ctx, c.Properties.Local_include_files)
Colin Cross39d97f22015-09-14 12:30:50 -0700495
496 flags.GlobalFlags = append(flags.GlobalFlags,
497 includeFilesToFlags(rootIncludeFiles),
498 includeFilesToFlags(localIncludeFiles))
499
Colin Cross06a931b2015-10-28 17:23:31 -0700500 if !Bool(c.Properties.No_default_compiler_flags) {
Colin Crossfa138792015-04-24 17:31:52 -0700501 if c.Properties.Sdk_version == "" || ctx.Host() {
Colin Cross28344522015-04-22 13:07:53 -0700502 flags.GlobalFlags = append(flags.GlobalFlags,
503 "${commonGlobalIncludes}",
504 toolchain.IncludeFlags(),
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700505 "-I"+common.PathForSource(ctx, "libnativehelper/include/nativehelper").String())
Colin Cross28344522015-04-22 13:07:53 -0700506 }
507
508 flags.GlobalFlags = append(flags.GlobalFlags, []string{
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700509 "-I" + common.PathForModuleSrc(ctx).String(),
510 "-I" + common.PathForModuleOut(ctx).String(),
511 "-I" + common.PathForModuleGen(ctx).String(),
Colin Cross28344522015-04-22 13:07:53 -0700512 }...)
513 }
514
Colin Cross06a931b2015-10-28 17:23:31 -0700515 if c.Properties.Clang == nil {
Dan Willemsendd0e2c32015-10-20 14:29:35 -0700516 if ctx.Host() {
517 flags.Clang = true
518 }
519
520 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
521 flags.Clang = true
522 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800523 }
524
Dan Willemsen490fd492015-11-24 17:53:15 -0800525 if !toolchain.ClangSupported() {
526 flags.Clang = false
527 }
528
Dan Willemsen6d11dd82015-11-03 14:27:00 -0800529 instructionSet := c.Properties.Instruction_set
530 instructionSetFlags, err := toolchain.InstructionSetFlags(instructionSet)
531 if flags.Clang {
532 instructionSetFlags, err = toolchain.ClangInstructionSetFlags(instructionSet)
533 }
534 if err != nil {
535 ctx.ModuleErrorf("%s", err)
536 }
537
538 // TODO: debug
539 flags.CFlags = append(flags.CFlags, c.Properties.Release.Cflags...)
540
Colin Cross97ba0732015-03-23 17:50:24 -0700541 if flags.Clang {
542 flags.CFlags = clangFilterUnknownCflags(flags.CFlags)
Colin Crossfa138792015-04-24 17:31:52 -0700543 flags.CFlags = append(flags.CFlags, c.Properties.Clang_cflags...)
544 flags.AsFlags = append(flags.AsFlags, c.Properties.Clang_asflags...)
Colin Cross97ba0732015-03-23 17:50:24 -0700545 flags.CppFlags = clangFilterUnknownCflags(flags.CppFlags)
546 flags.ConlyFlags = clangFilterUnknownCflags(flags.ConlyFlags)
547 flags.LdFlags = clangFilterUnknownCflags(flags.LdFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800548
Colin Cross97ba0732015-03-23 17:50:24 -0700549 flags.CFlags = append(flags.CFlags, "${clangExtraCflags}")
550 flags.ConlyFlags = append(flags.ConlyFlags, "${clangExtraConlyflags}")
Colin Crossf6566ed2015-03-24 11:13:38 -0700551 if ctx.Device() {
Colin Cross97ba0732015-03-23 17:50:24 -0700552 flags.CFlags = append(flags.CFlags, "${clangExtraTargetCflags}")
Colin Crossbdd7b1c2015-03-16 16:21:20 -0700553 }
554
Colin Cross3f40fa42015-01-30 17:27:36 -0800555 target := "-target " + toolchain.ClangTriple()
556 gccPrefix := "-B" + filepath.Join(toolchain.GccRoot(), toolchain.GccTriple(), "bin")
557
Colin Cross97ba0732015-03-23 17:50:24 -0700558 flags.CFlags = append(flags.CFlags, target, gccPrefix)
559 flags.AsFlags = append(flags.AsFlags, target, gccPrefix)
560 flags.LdFlags = append(flags.LdFlags, target, gccPrefix)
Colin Cross3f40fa42015-01-30 17:27:36 -0800561 }
562
Colin Cross06a931b2015-10-28 17:23:31 -0700563 if !Bool(c.Properties.No_default_compiler_flags) {
564 if ctx.Device() && !Bool(c.Properties.Allow_undefined_symbols) {
Colin Cross97ba0732015-03-23 17:50:24 -0700565 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-undefined")
Colin Cross3f40fa42015-01-30 17:27:36 -0800566 }
567
Colin Cross56b4d452015-04-21 17:38:44 -0700568 flags.GlobalFlags = append(flags.GlobalFlags, instructionSetFlags)
569
Colin Cross97ba0732015-03-23 17:50:24 -0700570 if flags.Clang {
571 flags.CppFlags = append(flags.CppFlags, "${commonClangGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -0700572 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -0800573 toolchain.ClangCflags(),
574 "${commonClangGlobalCflags}",
Colin Crossd3ba0392015-05-07 14:11:29 -0700575 fmt.Sprintf("${%sClangGlobalCflags}", ctx.HostOrDevice()))
Colin Cross3f40fa42015-01-30 17:27:36 -0800576 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700577 flags.CppFlags = append(flags.CppFlags, "${commonGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -0700578 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -0800579 toolchain.Cflags(),
580 "${commonGlobalCflags}",
Colin Crossd3ba0392015-05-07 14:11:29 -0700581 fmt.Sprintf("${%sGlobalCflags}", ctx.HostOrDevice()))
Colin Cross3f40fa42015-01-30 17:27:36 -0800582 }
583
Colin Crossf6566ed2015-03-24 11:13:38 -0700584 if ctx.Device() {
Colin Cross06a931b2015-10-28 17:23:31 -0700585 if Bool(c.Properties.Rtti) {
Colin Cross97ba0732015-03-23 17:50:24 -0700586 flags.CppFlags = append(flags.CppFlags, "-frtti")
Colin Cross3f40fa42015-01-30 17:27:36 -0800587 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700588 flags.CppFlags = append(flags.CppFlags, "-fno-rtti")
Colin Cross3f40fa42015-01-30 17:27:36 -0800589 }
590 }
591
Colin Cross97ba0732015-03-23 17:50:24 -0700592 flags.AsFlags = append(flags.AsFlags, "-D__ASSEMBLY__")
Colin Cross3f40fa42015-01-30 17:27:36 -0800593
Colin Cross97ba0732015-03-23 17:50:24 -0700594 if flags.Clang {
595 flags.CppFlags = append(flags.CppFlags, toolchain.ClangCppflags())
596 flags.LdFlags = append(flags.LdFlags, toolchain.ClangLdflags())
Colin Cross3f40fa42015-01-30 17:27:36 -0800597 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700598 flags.CppFlags = append(flags.CppFlags, toolchain.Cppflags())
599 flags.LdFlags = append(flags.LdFlags, toolchain.Ldflags())
Colin Cross3f40fa42015-01-30 17:27:36 -0800600 }
Colin Cross28344522015-04-22 13:07:53 -0700601
602 if ctx.Host() {
Colin Crossfa138792015-04-24 17:31:52 -0700603 flags.LdFlags = append(flags.LdFlags, c.Properties.Host_ldlibs...)
Colin Cross28344522015-04-22 13:07:53 -0700604 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800605 }
606
Colin Crossc4bde762015-11-23 16:11:30 -0800607 if flags.Clang {
608 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainClangCflags())
609 } else {
610 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainCflags())
611 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainLdflags())
612 }
613
Colin Cross0676e2d2015-04-24 17:39:18 -0700614 flags = c.ccModuleType().flags(ctx, flags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800615
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700616 if c.Properties.Sdk_version == "" {
617 if ctx.Host() && !flags.Clang {
618 // The host GCC doesn't support C++14 (and is deprecated, so likely
619 // never will). Build these modules with C++11.
620 flags.CppFlags = append(flags.CppFlags, "-std=gnu++11")
621 } else {
622 flags.CppFlags = append(flags.CppFlags, "-std=gnu++14")
623 }
624 }
625
626 flags.CFlags, _ = filterList(flags.CFlags, illegalFlags)
627 flags.CppFlags, _ = filterList(flags.CppFlags, illegalFlags)
628 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, illegalFlags)
629
Colin Cross3f40fa42015-01-30 17:27:36 -0800630 // Optimization to reduce size of build.ninja
631 // Replace the long list of flags for each file with a module-local variable
Colin Cross97ba0732015-03-23 17:50:24 -0700632 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
633 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
634 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
635 flags.CFlags = []string{"$cflags"}
636 flags.CppFlags = []string{"$cppflags"}
637 flags.AsFlags = []string{"$asflags"}
Colin Cross3f40fa42015-01-30 17:27:36 -0800638
639 return flags
640}
641
Colin Cross0676e2d2015-04-24 17:39:18 -0700642func (c *CCBase) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Cross3f40fa42015-01-30 17:27:36 -0800643 return flags
644}
645
646// Compile a list of source files into objects a specified subdirectory
Colin Crossfa138792015-04-24 17:31:52 -0700647func (c *CCBase) customCompileObjs(ctx common.AndroidModuleContext, flags CCFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700648 subdir string, srcFiles, excludes []string) common.Paths {
Colin Cross581c1892015-04-07 16:50:10 -0700649
650 buildFlags := ccFlagsToBuilderFlags(flags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800651
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700652 inputFiles := ctx.ExpandSources(srcFiles, excludes)
653 srcPaths, deps := genSources(ctx, inputFiles, buildFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800654
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700655 return TransformSourceToObj(ctx, subdir, srcPaths, buildFlags, deps)
Colin Cross3f40fa42015-01-30 17:27:36 -0800656}
657
Colin Crossfa138792015-04-24 17:31:52 -0700658// Compile files listed in c.Properties.Srcs into objects
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700659func (c *CCBase) compileObjs(ctx common.AndroidModuleContext, flags CCFlags) common.Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800660
Colin Crossfa138792015-04-24 17:31:52 -0700661 if c.Properties.SkipCompileObjs {
Colin Cross3f40fa42015-01-30 17:27:36 -0800662 return nil
663 }
664
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700665 return c.customCompileObjs(ctx, flags, "", c.Properties.Srcs, c.Properties.Exclude_srcs)
Colin Cross3f40fa42015-01-30 17:27:36 -0800666}
667
Colin Cross5049f022015-03-18 13:28:46 -0700668// Compile generated source files from dependencies
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700669func (c *CCBase) compileGeneratedObjs(ctx common.AndroidModuleContext, flags CCFlags) common.Paths {
670 var srcs common.Paths
Colin Cross5049f022015-03-18 13:28:46 -0700671
Colin Crossfa138792015-04-24 17:31:52 -0700672 if c.Properties.SkipCompileObjs {
Colin Cross5049f022015-03-18 13:28:46 -0700673 return nil
674 }
675
676 ctx.VisitDirectDeps(func(module blueprint.Module) {
677 if gen, ok := module.(genrule.SourceFileGenerator); ok {
678 srcs = append(srcs, gen.GeneratedSourceFiles()...)
679 }
680 })
681
682 if len(srcs) == 0 {
683 return nil
684 }
685
Colin Cross581c1892015-04-07 16:50:10 -0700686 return TransformSourceToObj(ctx, "", srcs, ccFlagsToBuilderFlags(flags), nil)
Colin Cross5049f022015-03-18 13:28:46 -0700687}
688
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700689func (c *CCBase) outputFile() common.OptionalPath {
690 return common.OptionalPath{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800691}
692
Colin Crossfa138792015-04-24 17:31:52 -0700693func (c *CCBase) depsToPathsFromList(ctx common.AndroidModuleContext,
Colin Cross3f40fa42015-01-30 17:27:36 -0800694 names []string) (modules []common.AndroidModule,
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700695 outputFiles common.Paths, exportedFlags []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800696
697 for _, n := range names {
698 found := false
699 ctx.VisitDirectDeps(func(m blueprint.Module) {
700 otherName := ctx.OtherModuleName(m)
701 if otherName != n {
702 return
703 }
704
Colin Cross97ba0732015-03-23 17:50:24 -0700705 if a, ok := m.(CCModuleType); ok {
Dan Willemsen0effe062015-11-30 16:06:01 -0800706 if !a.Enabled() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800707 // If a cc_library host+device module depends on a library that exists as both
708 // cc_library_shared and cc_library_host_shared, it will end up with two
709 // dependencies with the same name, one of which is marked disabled for each
710 // of host and device. Ignore the disabled one.
711 return
712 }
Colin Crossd3ba0392015-05-07 14:11:29 -0700713 if a.HostOrDevice() != ctx.HostOrDevice() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800714 ctx.ModuleErrorf("host/device mismatch between %q and %q", ctx.ModuleName(),
715 otherName)
716 return
717 }
718
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700719 if outputFile := a.outputFile(); outputFile.Valid() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800720 if found {
721 ctx.ModuleErrorf("multiple modules satisified dependency on %q", otherName)
722 return
723 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700724 outputFiles = append(outputFiles, outputFile.Path())
Colin Cross3f40fa42015-01-30 17:27:36 -0800725 modules = append(modules, a)
Colin Cross28344522015-04-22 13:07:53 -0700726 if i, ok := a.(ccExportedFlagsProducer); ok {
727 exportedFlags = append(exportedFlags, i.exportedFlags()...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800728 }
729 found = true
730 } else {
731 ctx.ModuleErrorf("module %q missing output file", otherName)
732 return
733 }
734 } else {
735 ctx.ModuleErrorf("module %q not an android module", otherName)
736 return
737 }
738 })
739 if !found {
740 ctx.ModuleErrorf("unsatisified dependency on %q", n)
741 }
742 }
743
Colin Cross28344522015-04-22 13:07:53 -0700744 return modules, outputFiles, exportedFlags
Colin Cross3f40fa42015-01-30 17:27:36 -0800745}
746
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700747// Convert dependency names to paths. Takes a CCDeps containing names and returns a CCPathDeps
Colin Cross21b9a242015-03-24 14:15:58 -0700748// containing paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700749func (c *CCBase) depsToPaths(ctx common.AndroidModuleContext, depNames CCDeps) CCPathDeps {
750 var depPaths CCPathDeps
Colin Cross28344522015-04-22 13:07:53 -0700751 var newCflags []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800752
Colin Cross21b9a242015-03-24 14:15:58 -0700753 var wholeStaticLibModules []common.AndroidModule
Colin Cross3f40fa42015-01-30 17:27:36 -0800754
Colin Cross28344522015-04-22 13:07:53 -0700755 wholeStaticLibModules, depPaths.WholeStaticLibs, newCflags =
Colin Cross21b9a242015-03-24 14:15:58 -0700756 c.depsToPathsFromList(ctx, depNames.WholeStaticLibs)
Colin Cross28344522015-04-22 13:07:53 -0700757 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Crossa48f71f2015-11-16 18:00:41 -0800758 depPaths.ReexportedCflags = append(depPaths.ReexportedCflags, newCflags...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800759
Colin Cross21b9a242015-03-24 14:15:58 -0700760 for _, m := range wholeStaticLibModules {
761 if staticLib, ok := m.(ccLibraryInterface); ok && staticLib.static() {
762 depPaths.WholeStaticLibObjFiles =
763 append(depPaths.WholeStaticLibObjFiles, staticLib.allObjFiles()...)
764 } else {
765 ctx.ModuleErrorf("module %q not a static library", ctx.OtherModuleName(m))
766 }
767 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800768
Colin Cross28344522015-04-22 13:07:53 -0700769 _, depPaths.StaticLibs, newCflags = c.depsToPathsFromList(ctx, depNames.StaticLibs)
770 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross21b9a242015-03-24 14:15:58 -0700771
Colin Cross28344522015-04-22 13:07:53 -0700772 _, depPaths.LateStaticLibs, newCflags = c.depsToPathsFromList(ctx, depNames.LateStaticLibs)
773 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross21b9a242015-03-24 14:15:58 -0700774
Colin Cross28344522015-04-22 13:07:53 -0700775 _, depPaths.SharedLibs, newCflags = c.depsToPathsFromList(ctx, depNames.SharedLibs)
776 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross21b9a242015-03-24 14:15:58 -0700777
778 ctx.VisitDirectDeps(func(m blueprint.Module) {
Dan Albertc3144b12015-04-28 18:17:56 -0700779 if obj, ok := m.(ccObjectProvider); ok {
Colin Cross21b9a242015-03-24 14:15:58 -0700780 otherName := ctx.OtherModuleName(m)
781 if otherName == depNames.CrtBegin {
Colin Cross06a931b2015-10-28 17:23:31 -0700782 if !Bool(c.Properties.Nocrt) {
Dan Albertc3144b12015-04-28 18:17:56 -0700783 depPaths.CrtBegin = obj.object().outputFile()
Colin Cross21b9a242015-03-24 14:15:58 -0700784 }
785 } else if otherName == depNames.CrtEnd {
Colin Cross06a931b2015-10-28 17:23:31 -0700786 if !Bool(c.Properties.Nocrt) {
Dan Albertc3144b12015-04-28 18:17:56 -0700787 depPaths.CrtEnd = obj.object().outputFile()
Colin Cross21b9a242015-03-24 14:15:58 -0700788 }
789 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700790 output := obj.object().outputFile()
791 if output.Valid() {
792 depPaths.ObjFiles = append(depPaths.ObjFiles, output.Path())
793 } else {
794 ctx.ModuleErrorf("module %s did not provide an output file", otherName)
795 }
Colin Cross21b9a242015-03-24 14:15:58 -0700796 }
797 }
798 })
799
800 return depPaths
Colin Cross3f40fa42015-01-30 17:27:36 -0800801}
802
Colin Cross7d5136f2015-05-11 13:39:40 -0700803type ccLinkedProperties struct {
804 VariantIsShared bool `blueprint:"mutated"`
805 VariantIsStatic bool `blueprint:"mutated"`
806 VariantIsStaticBinary bool `blueprint:"mutated"`
807}
808
Colin Crossfa138792015-04-24 17:31:52 -0700809// CCLinked contains the properties and members used by libraries and executables
810type CCLinked struct {
811 CCBase
Colin Cross7d5136f2015-05-11 13:39:40 -0700812 dynamicProperties ccLinkedProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800813}
814
Colin Crossfa138792015-04-24 17:31:52 -0700815func newCCDynamic(dynamic *CCLinked, module CCModuleType, hod common.HostOrDeviceSupported,
Colin Crossc472d572015-03-17 15:06:21 -0700816 multilib common.Multilib, props ...interface{}) (blueprint.Module, []interface{}) {
817
Colin Crossed4cf0b2015-03-26 14:43:45 -0700818 props = append(props, &dynamic.dynamicProperties)
819
Colin Crossfa138792015-04-24 17:31:52 -0700820 return newCCBase(&dynamic.CCBase, module, hod, multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700821}
822
Colin Crossfa138792015-04-24 17:31:52 -0700823func (c *CCLinked) systemSharedLibs(ctx common.AndroidBaseContext) []string {
Colin Cross06a931b2015-10-28 17:23:31 -0700824 if c.Properties.System_shared_libs != nil {
Colin Crossfa138792015-04-24 17:31:52 -0700825 return c.Properties.System_shared_libs
826 } else if ctx.Device() && c.Properties.Sdk_version == "" {
Colin Cross577f6e42015-03-27 18:23:34 -0700827 return []string{"libc", "libm"}
Colin Cross28d76592015-03-26 16:14:04 -0700828 } else {
Colin Cross577f6e42015-03-27 18:23:34 -0700829 return nil
Colin Cross3f40fa42015-01-30 17:27:36 -0800830 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800831}
832
Colin Crossfa138792015-04-24 17:31:52 -0700833func (c *CCLinked) stl(ctx common.AndroidBaseContext) string {
834 if c.Properties.Sdk_version != "" && ctx.Device() {
835 switch c.Properties.Stl {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700836 case "":
837 return "ndk_system"
838 case "c++_shared", "c++_static",
839 "stlport_shared", "stlport_static",
840 "gnustl_static":
Colin Crossfa138792015-04-24 17:31:52 -0700841 return "ndk_lib" + c.Properties.Stl
Colin Crossed4cf0b2015-03-26 14:43:45 -0700842 default:
Colin Crossfa138792015-04-24 17:31:52 -0700843 ctx.ModuleErrorf("stl: %q is not a supported STL with sdk_version set", c.Properties.Stl)
Colin Crossed4cf0b2015-03-26 14:43:45 -0700844 return ""
845 }
846 }
847
Dan Willemsen490fd492015-11-24 17:53:15 -0800848 if ctx.HostType() == common.Windows {
849 switch c.Properties.Stl {
850 case "libc++", "libc++_static", "libstdc++", "":
851 // libc++ is not supported on mingw
852 return "libstdc++"
853 case "none":
854 return ""
855 default:
856 ctx.ModuleErrorf("stl: %q is not a supported STL", c.Properties.Stl)
857 return ""
Colin Crossed4cf0b2015-03-26 14:43:45 -0700858 }
Dan Willemsen490fd492015-11-24 17:53:15 -0800859 } else {
860 switch c.Properties.Stl {
861 case "libc++", "libc++_static",
862 "libstdc++":
863 return c.Properties.Stl
864 case "none":
865 return ""
866 case "":
867 if c.static() {
868 return "libc++_static"
869 } else {
870 return "libc++"
871 }
872 default:
873 ctx.ModuleErrorf("stl: %q is not a supported STL", c.Properties.Stl)
874 return ""
875 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700876 }
877}
878
Dan Willemsen490fd492015-11-24 17:53:15 -0800879var hostDynamicGccLibs, hostStaticGccLibs map[common.HostType][]string
Colin Cross0af4b842015-04-30 16:36:18 -0700880
881func init() {
Dan Willemsen490fd492015-11-24 17:53:15 -0800882 hostDynamicGccLibs = map[common.HostType][]string{
883 common.Linux: []string{"-lgcc_s", "-lgcc", "-lc", "-lgcc_s", "-lgcc"},
884 common.Darwin: []string{"-lc", "-lSystem"},
885 common.Windows: []string{"-lmsvcr110", "-lmingw32", "-lgcc", "-lmoldname",
886 "-lmingwex", "-lmsvcrt", "-ladvapi32", "-lshell32", "-luser32",
887 "-lkernel32", "-lmingw32", "-lgcc", "-lmoldname", "-lmingwex",
888 "-lmsvcrt"},
889 }
890 hostStaticGccLibs = map[common.HostType][]string{
891 common.Linux: []string{"-Wl,--start-group", "-lgcc", "-lgcc_eh", "-lc", "-Wl,--end-group"},
892 common.Darwin: []string{"NO_STATIC_HOST_BINARIES_ON_DARWIN"},
893 common.Windows: []string{"NO_STATIC_HOST_BINARIES_ON_WINDOWS"},
Colin Cross0af4b842015-04-30 16:36:18 -0700894 }
895}
Colin Cross712fc022015-04-27 11:13:34 -0700896
Colin Crosse11befc2015-04-27 17:49:17 -0700897func (c *CCLinked) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700898 stl := c.stl(ctx)
899 if ctx.Failed() {
900 return flags
901 }
902
903 switch stl {
904 case "libc++", "libc++_static":
905 flags.CFlags = append(flags.CFlags, "-D_USING_LIBCXX")
Colin Crossed4cf0b2015-03-26 14:43:45 -0700906 if ctx.Host() {
907 flags.CppFlags = append(flags.CppFlags, "-nostdinc++")
908 flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs")
Colin Cross712fc022015-04-27 11:13:34 -0700909 flags.LdFlags = append(flags.LdFlags, "-lm", "-lpthread")
Colin Cross18b6dc52015-04-28 13:20:37 -0700910 if c.staticBinary() {
Dan Willemsen490fd492015-11-24 17:53:15 -0800911 flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs[ctx.HostType()]...)
Colin Cross18b6dc52015-04-28 13:20:37 -0700912 } else {
Dan Willemsen490fd492015-11-24 17:53:15 -0800913 flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs[ctx.HostType()]...)
Colin Cross712fc022015-04-27 11:13:34 -0700914 }
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700915 } else {
916 if ctx.Arch().ArchType == common.Arm {
917 flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,libunwind_llvm.a")
918 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700919 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700920 case "libstdc++":
921 // Using bionic's basic libstdc++. Not actually an STL. Only around until the
922 // tree is in good enough shape to not need it.
923 // Host builds will use GNU libstdc++.
924 if ctx.Device() {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700925 flags.CFlags = append(flags.CFlags, "-I"+common.PathForSource(ctx, "bionic/libstdc++/include").String())
Colin Crossed4cf0b2015-03-26 14:43:45 -0700926 }
927 case "ndk_system":
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700928 ndkSrcRoot := common.PathForSource(ctx, "prebuilts/ndk/current/sources/cxx-stl/system/include")
929 flags.CFlags = append(flags.CFlags, "-isystem "+ndkSrcRoot.String())
Colin Crossed4cf0b2015-03-26 14:43:45 -0700930 case "ndk_libc++_shared", "ndk_libc++_static":
931 // TODO(danalbert): This really shouldn't be here...
932 flags.CppFlags = append(flags.CppFlags, "-std=c++11")
933 case "ndk_libstlport_shared", "ndk_libstlport_static", "ndk_libgnustl_static":
934 // Nothing
935 case "":
936 // None or error.
937 if ctx.Host() {
938 flags.CppFlags = append(flags.CppFlags, "-nostdinc++")
939 flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs")
Colin Cross18b6dc52015-04-28 13:20:37 -0700940 if c.staticBinary() {
Dan Willemsen490fd492015-11-24 17:53:15 -0800941 flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs[ctx.HostType()]...)
Colin Cross18b6dc52015-04-28 13:20:37 -0700942 } else {
Dan Willemsen490fd492015-11-24 17:53:15 -0800943 flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs[ctx.HostType()]...)
Colin Cross712fc022015-04-27 11:13:34 -0700944 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700945 }
946 default:
Colin Crossfa138792015-04-24 17:31:52 -0700947 panic(fmt.Errorf("Unknown stl in CCLinked.Flags: %q", stl))
Colin Crossed4cf0b2015-03-26 14:43:45 -0700948 }
949
950 return flags
951}
952
Colin Crosse11befc2015-04-27 17:49:17 -0700953func (c *CCLinked) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
954 depNames = c.CCBase.depNames(ctx, depNames)
Colin Cross3f40fa42015-01-30 17:27:36 -0800955
Colin Crossed4cf0b2015-03-26 14:43:45 -0700956 stl := c.stl(ctx)
957 if ctx.Failed() {
958 return depNames
959 }
960
961 switch stl {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700962 case "libstdc++":
963 if ctx.Device() {
964 depNames.SharedLibs = append(depNames.SharedLibs, stl)
965 }
Colin Cross74d1ec02015-04-28 13:30:13 -0700966 case "libc++", "libc++_static":
967 if stl == "libc++" {
968 depNames.SharedLibs = append(depNames.SharedLibs, stl)
969 } else {
970 depNames.StaticLibs = append(depNames.StaticLibs, stl)
971 }
972 if ctx.Device() {
973 if ctx.Arch().ArchType == common.Arm {
974 depNames.StaticLibs = append(depNames.StaticLibs, "libunwind_llvm")
975 }
976 if c.staticBinary() {
977 depNames.StaticLibs = append(depNames.StaticLibs, "libdl")
978 } else {
979 depNames.SharedLibs = append(depNames.SharedLibs, "libdl")
980 }
981 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700982 case "":
983 // None or error.
984 case "ndk_system":
985 // TODO: Make a system STL prebuilt for the NDK.
986 // 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 -0700987 // its own includes. The includes are handled in CCBase.Flags().
Colin Cross577f6e42015-03-27 18:23:34 -0700988 depNames.SharedLibs = append([]string{"libstdc++"}, depNames.SharedLibs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -0700989 case "ndk_libc++_shared", "ndk_libstlport_shared":
990 depNames.SharedLibs = append(depNames.SharedLibs, stl)
991 case "ndk_libc++_static", "ndk_libstlport_static", "ndk_libgnustl_static":
992 depNames.StaticLibs = append(depNames.StaticLibs, stl)
993 default:
Colin Crosse11befc2015-04-27 17:49:17 -0700994 panic(fmt.Errorf("Unknown stl in CCLinked.depNames: %q", stl))
Colin Crossed4cf0b2015-03-26 14:43:45 -0700995 }
996
Colin Cross74d1ec02015-04-28 13:30:13 -0700997 if ctx.ModuleName() != "libcompiler_rt-extras" {
998 depNames.StaticLibs = append(depNames.StaticLibs, "libcompiler_rt-extras")
999 }
1000
Colin Crossf6566ed2015-03-24 11:13:38 -07001001 if ctx.Device() {
Colin Cross77b00fa2015-03-16 16:15:49 -07001002 // libgcc and libatomic have to be last on the command line
Dan Willemsend67be222015-09-16 15:19:33 -07001003 depNames.LateStaticLibs = append(depNames.LateStaticLibs, "libgcov", "libatomic")
Colin Cross06a931b2015-10-28 17:23:31 -07001004 if !Bool(c.Properties.No_libgcc) {
Dan Willemsend67be222015-09-16 15:19:33 -07001005 depNames.LateStaticLibs = append(depNames.LateStaticLibs, "libgcc")
1006 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001007
Colin Cross18b6dc52015-04-28 13:20:37 -07001008 if !c.static() {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001009 depNames.SharedLibs = append(depNames.SharedLibs, c.systemSharedLibs(ctx)...)
1010 }
Colin Cross577f6e42015-03-27 18:23:34 -07001011
Colin Crossfa138792015-04-24 17:31:52 -07001012 if c.Properties.Sdk_version != "" {
1013 version := c.Properties.Sdk_version
Colin Cross577f6e42015-03-27 18:23:34 -07001014 depNames.SharedLibs = append(depNames.SharedLibs,
1015 "ndk_libc."+version,
1016 "ndk_libm."+version,
1017 )
1018 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001019 }
1020
Colin Cross21b9a242015-03-24 14:15:58 -07001021 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -08001022}
1023
Colin Crossed4cf0b2015-03-26 14:43:45 -07001024// ccLinkedInterface interface is used on ccLinked to deal with static or shared variants
1025type ccLinkedInterface interface {
1026 // Returns true if the build options for the module have selected a static or shared build
1027 buildStatic() bool
1028 buildShared() bool
1029
1030 // Sets whether a specific variant is static or shared
Colin Cross18b6dc52015-04-28 13:20:37 -07001031 setStatic(bool)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001032
Colin Cross18b6dc52015-04-28 13:20:37 -07001033 // Returns whether a specific variant is a static library or binary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001034 static() bool
Colin Cross18b6dc52015-04-28 13:20:37 -07001035
1036 // Returns whether a module is a static binary
1037 staticBinary() bool
Colin Crossed4cf0b2015-03-26 14:43:45 -07001038}
1039
1040var _ ccLinkedInterface = (*CCLibrary)(nil)
1041var _ ccLinkedInterface = (*CCBinary)(nil)
1042
Colin Crossfa138792015-04-24 17:31:52 -07001043func (c *CCLinked) static() bool {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001044 return c.dynamicProperties.VariantIsStatic
1045}
1046
Colin Cross18b6dc52015-04-28 13:20:37 -07001047func (c *CCLinked) staticBinary() bool {
1048 return c.dynamicProperties.VariantIsStaticBinary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001049}
1050
Colin Cross18b6dc52015-04-28 13:20:37 -07001051func (c *CCLinked) setStatic(static bool) {
1052 c.dynamicProperties.VariantIsStatic = static
Colin Crossed4cf0b2015-03-26 14:43:45 -07001053}
1054
Colin Cross28344522015-04-22 13:07:53 -07001055type ccExportedFlagsProducer interface {
1056 exportedFlags() []string
Colin Cross3f40fa42015-01-30 17:27:36 -08001057}
1058
1059//
1060// Combined static+shared libraries
1061//
1062
Colin Cross7d5136f2015-05-11 13:39:40 -07001063type CCLibraryProperties struct {
1064 BuildStatic bool `blueprint:"mutated"`
1065 BuildShared bool `blueprint:"mutated"`
1066 Static struct {
1067 Srcs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001068 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -07001069 Cflags []string `android:"arch_variant"`
1070 Whole_static_libs []string `android:"arch_variant"`
1071 Static_libs []string `android:"arch_variant"`
1072 Shared_libs []string `android:"arch_variant"`
1073 } `android:"arch_variant"`
1074 Shared struct {
1075 Srcs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001076 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -07001077 Cflags []string `android:"arch_variant"`
1078 Whole_static_libs []string `android:"arch_variant"`
1079 Static_libs []string `android:"arch_variant"`
1080 Shared_libs []string `android:"arch_variant"`
1081 } `android:"arch_variant"`
Colin Crossaee540a2015-07-06 17:48:31 -07001082
1083 // local file name to pass to the linker as --version_script
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001084 Version_script *string `android:"arch_variant"`
Dan Willemsen93c28312015-12-04 14:59:08 -08001085 // local file name to pass to the linker as -unexported_symbols_list
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001086 Unexported_symbols_list *string `android:"arch_variant"`
Dan Willemsen93c28312015-12-04 14:59:08 -08001087 // local file name to pass to the linker as -force_symbols_not_weak_list
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001088 Force_symbols_not_weak_list *string `android:"arch_variant"`
Dan Willemsen93c28312015-12-04 14:59:08 -08001089 // local file name to pass to the linker as -force_symbols_weak_list
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001090 Force_symbols_weak_list *string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -07001091}
1092
Colin Cross97ba0732015-03-23 17:50:24 -07001093type CCLibrary struct {
Colin Crossfa138792015-04-24 17:31:52 -07001094 CCLinked
Colin Cross3f40fa42015-01-30 17:27:36 -08001095
Colin Cross28344522015-04-22 13:07:53 -07001096 reuseFrom ccLibraryInterface
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001097 reuseObjFiles common.Paths
1098 objFiles common.Paths
Colin Cross28344522015-04-22 13:07:53 -07001099 exportFlags []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001100 out common.Path
Dan Willemsen218f6562015-07-08 18:13:11 -07001101 systemLibs []string
Colin Cross3f40fa42015-01-30 17:27:36 -08001102
Colin Cross7d5136f2015-05-11 13:39:40 -07001103 LibraryProperties CCLibraryProperties
Colin Cross3f40fa42015-01-30 17:27:36 -08001104}
1105
Colin Crossed4cf0b2015-03-26 14:43:45 -07001106func (c *CCLibrary) buildStatic() bool {
1107 return c.LibraryProperties.BuildStatic
1108}
1109
1110func (c *CCLibrary) buildShared() bool {
1111 return c.LibraryProperties.BuildShared
1112}
1113
Colin Cross97ba0732015-03-23 17:50:24 -07001114type ccLibraryInterface interface {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001115 ccLinkedInterface
Colin Cross97ba0732015-03-23 17:50:24 -07001116 ccLibrary() *CCLibrary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001117 setReuseFrom(ccLibraryInterface)
1118 getReuseFrom() ccLibraryInterface
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001119 getReuseObjFiles() common.Paths
1120 allObjFiles() common.Paths
Colin Crossc472d572015-03-17 15:06:21 -07001121}
1122
Colin Crossed4cf0b2015-03-26 14:43:45 -07001123var _ ccLibraryInterface = (*CCLibrary)(nil)
1124
Colin Cross97ba0732015-03-23 17:50:24 -07001125func (c *CCLibrary) ccLibrary() *CCLibrary {
1126 return c
Colin Cross3f40fa42015-01-30 17:27:36 -08001127}
1128
Colin Cross97ba0732015-03-23 17:50:24 -07001129func NewCCLibrary(library *CCLibrary, module CCModuleType,
1130 hod common.HostOrDeviceSupported) (blueprint.Module, []interface{}) {
1131
Colin Crossfa138792015-04-24 17:31:52 -07001132 return newCCDynamic(&library.CCLinked, module, hod, common.MultilibBoth,
Colin Cross97ba0732015-03-23 17:50:24 -07001133 &library.LibraryProperties)
1134}
1135
1136func CCLibraryFactory() (blueprint.Module, []interface{}) {
1137 module := &CCLibrary{}
1138
1139 module.LibraryProperties.BuildShared = true
1140 module.LibraryProperties.BuildStatic = true
1141
1142 return NewCCLibrary(module, module, common.HostAndDeviceSupported)
1143}
1144
Colin Cross0676e2d2015-04-24 17:39:18 -07001145func (c *CCLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Crosse11befc2015-04-27 17:49:17 -07001146 depNames = c.CCLinked.depNames(ctx, depNames)
Colin Cross2732e9a2015-04-28 13:23:52 -07001147 if c.static() {
1148 depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.LibraryProperties.Static.Whole_static_libs...)
1149 depNames.StaticLibs = append(depNames.StaticLibs, c.LibraryProperties.Static.Static_libs...)
1150 depNames.SharedLibs = append(depNames.SharedLibs, c.LibraryProperties.Static.Shared_libs...)
1151 } else {
Colin Crossf6566ed2015-03-24 11:13:38 -07001152 if ctx.Device() {
Dan Albertc3144b12015-04-28 18:17:56 -07001153 if c.Properties.Sdk_version == "" {
1154 depNames.CrtBegin = "crtbegin_so"
1155 depNames.CrtEnd = "crtend_so"
1156 } else {
1157 depNames.CrtBegin = "ndk_crtbegin_so." + c.Properties.Sdk_version
1158 depNames.CrtEnd = "ndk_crtend_so." + c.Properties.Sdk_version
1159 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001160 }
Colin Cross2732e9a2015-04-28 13:23:52 -07001161 depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.LibraryProperties.Shared.Whole_static_libs...)
1162 depNames.StaticLibs = append(depNames.StaticLibs, c.LibraryProperties.Shared.Static_libs...)
1163 depNames.SharedLibs = append(depNames.SharedLibs, c.LibraryProperties.Shared.Shared_libs...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001164 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001165
Dan Willemsen218f6562015-07-08 18:13:11 -07001166 c.systemLibs = c.systemSharedLibs(ctx)
1167
Colin Cross21b9a242015-03-24 14:15:58 -07001168 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -08001169}
1170
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001171func (c *CCLibrary) outputFile() common.OptionalPath {
1172 return common.OptionalPathForPath(c.out)
Colin Cross3f40fa42015-01-30 17:27:36 -08001173}
1174
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001175func (c *CCLibrary) getReuseObjFiles() common.Paths {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001176 return c.reuseObjFiles
1177}
1178
1179func (c *CCLibrary) setReuseFrom(reuseFrom ccLibraryInterface) {
1180 c.reuseFrom = reuseFrom
1181}
1182
1183func (c *CCLibrary) getReuseFrom() ccLibraryInterface {
1184 return c.reuseFrom
1185}
1186
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001187func (c *CCLibrary) allObjFiles() common.Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -08001188 return c.objFiles
1189}
1190
Colin Cross28344522015-04-22 13:07:53 -07001191func (c *CCLibrary) exportedFlags() []string {
1192 return c.exportFlags
Colin Cross3f40fa42015-01-30 17:27:36 -08001193}
1194
Colin Cross0676e2d2015-04-24 17:39:18 -07001195func (c *CCLibrary) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Crosse11befc2015-04-27 17:49:17 -07001196 flags = c.CCLinked.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001197
Dan Willemsen490fd492015-11-24 17:53:15 -08001198 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
1199 // all code is position independent, and then those warnings get promoted to
1200 // errors.
1201 if ctx.HostType() != common.Windows {
1202 flags.CFlags = append(flags.CFlags, "-fPIC")
1203 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001204
Colin Crossd8e780d2015-04-28 17:39:43 -07001205 if c.static() {
1206 flags.CFlags = append(flags.CFlags, c.LibraryProperties.Static.Cflags...)
1207 } else {
1208 flags.CFlags = append(flags.CFlags, c.LibraryProperties.Shared.Cflags...)
1209 }
1210
Colin Cross18b6dc52015-04-28 13:20:37 -07001211 if !c.static() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001212 libName := ctx.ModuleName()
1213 // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead
1214 sharedFlag := "-Wl,-shared"
Dan Willemsendd0e2c32015-10-20 14:29:35 -07001215 if flags.Clang || ctx.Host() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001216 sharedFlag = "-shared"
1217 }
Colin Crossf6566ed2015-03-24 11:13:38 -07001218 if ctx.Device() {
Colin Cross97ba0732015-03-23 17:50:24 -07001219 flags.LdFlags = append(flags.LdFlags, "-nostdlib")
Colin Cross3f40fa42015-01-30 17:27:36 -08001220 }
Colin Cross97ba0732015-03-23 17:50:24 -07001221
Colin Cross0af4b842015-04-30 16:36:18 -07001222 if ctx.Darwin() {
1223 flags.LdFlags = append(flags.LdFlags,
1224 "-dynamiclib",
1225 "-single_module",
1226 //"-read_only_relocs suppress",
Dan Willemsen490fd492015-11-24 17:53:15 -08001227 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
Colin Cross0af4b842015-04-30 16:36:18 -07001228 )
1229 } else {
1230 flags.LdFlags = append(flags.LdFlags,
1231 "-Wl,--gc-sections",
1232 sharedFlag,
Dan Willemsen490fd492015-11-24 17:53:15 -08001233 "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix(),
Colin Cross0af4b842015-04-30 16:36:18 -07001234 )
1235 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001236 }
Colin Cross97ba0732015-03-23 17:50:24 -07001237
1238 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001239}
1240
Colin Cross97ba0732015-03-23 17:50:24 -07001241func (c *CCLibrary) compileStaticLibrary(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001242 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001243
1244 staticFlags := flags
Colin Cross581c1892015-04-07 16:50:10 -07001245 objFilesStatic := c.customCompileObjs(ctx, staticFlags, common.DeviceStaticLibrary,
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001246 c.LibraryProperties.Static.Srcs, c.LibraryProperties.Static.Exclude_srcs)
Colin Cross3f40fa42015-01-30 17:27:36 -08001247
1248 objFiles = append(objFiles, objFilesStatic...)
Colin Cross21b9a242015-03-24 14:15:58 -07001249 objFiles = append(objFiles, deps.WholeStaticLibObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001250
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001251 outputFile := common.PathForModuleOut(ctx, ctx.ModuleName()+staticLibraryExtension)
Colin Cross3f40fa42015-01-30 17:27:36 -08001252
Colin Cross0af4b842015-04-30 16:36:18 -07001253 if ctx.Darwin() {
1254 TransformDarwinObjToStaticLib(ctx, objFiles, ccFlagsToBuilderFlags(flags), outputFile)
1255 } else {
1256 TransformObjToStaticLib(ctx, objFiles, ccFlagsToBuilderFlags(flags), outputFile)
1257 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001258
1259 c.objFiles = objFiles
1260 c.out = outputFile
Colin Crossf2298272015-05-12 11:36:53 -07001261
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001262 includeDirs := common.PathsForModuleSrc(ctx, c.Properties.Export_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -07001263 c.exportFlags = []string{includeDirsToFlags(includeDirs)}
Colin Crossa48f71f2015-11-16 18:00:41 -08001264 c.exportFlags = append(c.exportFlags, deps.ReexportedCflags...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001265
1266 ctx.CheckbuildFile(outputFile)
1267}
1268
Colin Cross97ba0732015-03-23 17:50:24 -07001269func (c *CCLibrary) compileSharedLibrary(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001270 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001271
1272 sharedFlags := flags
Colin Cross581c1892015-04-07 16:50:10 -07001273 objFilesShared := c.customCompileObjs(ctx, sharedFlags, common.DeviceSharedLibrary,
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001274 c.LibraryProperties.Shared.Srcs, c.LibraryProperties.Shared.Exclude_srcs)
Colin Cross3f40fa42015-01-30 17:27:36 -08001275
1276 objFiles = append(objFiles, objFilesShared...)
1277
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001278 outputFile := common.PathForModuleOut(ctx, ctx.ModuleName()+flags.Toolchain.ShlibSuffix())
Colin Cross3f40fa42015-01-30 17:27:36 -08001279
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001280 var linkerDeps common.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07001281
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001282 versionScript := common.OptionalPathForModuleSrc(ctx, c.LibraryProperties.Version_script)
1283 unexportedSymbols := common.OptionalPathForModuleSrc(ctx, c.LibraryProperties.Unexported_symbols_list)
1284 forceNotWeakSymbols := common.OptionalPathForModuleSrc(ctx, c.LibraryProperties.Force_symbols_not_weak_list)
1285 forceWeakSymbols := common.OptionalPathForModuleSrc(ctx, c.LibraryProperties.Force_symbols_weak_list)
Dan Willemsen93c28312015-12-04 14:59:08 -08001286 if !ctx.Darwin() {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001287 if versionScript.Valid() {
1288 sharedFlags.LdFlags = append(sharedFlags.LdFlags, "-Wl,--version-script,"+versionScript.String())
1289 linkerDeps = append(linkerDeps, versionScript.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001290 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001291 if unexportedSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001292 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
1293 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001294 if forceNotWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001295 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
1296 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001297 if forceWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001298 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
1299 }
1300 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001301 if versionScript.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001302 ctx.PropertyErrorf("version_script", "Not supported on Darwin")
1303 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001304 if unexportedSymbols.Valid() {
1305 sharedFlags.LdFlags = append(sharedFlags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
1306 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001307 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001308 if forceNotWeakSymbols.Valid() {
1309 sharedFlags.LdFlags = append(sharedFlags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
1310 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001311 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001312 if forceWeakSymbols.Valid() {
1313 sharedFlags.LdFlags = append(sharedFlags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
1314 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001315 }
Colin Crossaee540a2015-07-06 17:48:31 -07001316 }
1317
Colin Cross97ba0732015-03-23 17:50:24 -07001318 TransformObjToDynamicBinary(ctx, objFiles, deps.SharedLibs, deps.StaticLibs,
Colin Crossaee540a2015-07-06 17:48:31 -07001319 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, false,
Dan Willemsen6203ac02015-11-24 12:58:57 -08001320 ccFlagsToBuilderFlags(sharedFlags), outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08001321
1322 c.out = outputFile
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001323 includeDirs := common.PathsForModuleSrc(ctx, c.Properties.Export_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -07001324 c.exportFlags = []string{includeDirsToFlags(includeDirs)}
Colin Crossa48f71f2015-11-16 18:00:41 -08001325 c.exportFlags = append(c.exportFlags, deps.ReexportedCflags...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001326}
1327
Colin Cross97ba0732015-03-23 17:50:24 -07001328func (c *CCLibrary) compileModule(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001329 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001330
1331 // Reuse the object files from the matching static library if it exists
Colin Crossed4cf0b2015-03-26 14:43:45 -07001332 if c.getReuseFrom().ccLibrary() == c {
1333 c.reuseObjFiles = objFiles
Colin Cross3f40fa42015-01-30 17:27:36 -08001334 } else {
Colin Cross2732e9a2015-04-28 13:23:52 -07001335 if c.getReuseFrom().ccLibrary().LibraryProperties.Static.Cflags == nil &&
1336 c.LibraryProperties.Shared.Cflags == nil {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001337 objFiles = append(common.Paths(nil), c.getReuseFrom().getReuseObjFiles()...)
Colin Cross2732e9a2015-04-28 13:23:52 -07001338 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001339 }
1340
Colin Crossed4cf0b2015-03-26 14:43:45 -07001341 if c.static() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001342 c.compileStaticLibrary(ctx, flags, deps, objFiles)
1343 } else {
1344 c.compileSharedLibrary(ctx, flags, deps, objFiles)
1345 }
1346}
1347
Colin Cross97ba0732015-03-23 17:50:24 -07001348func (c *CCLibrary) installStaticLibrary(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001349 // Static libraries do not get installed.
1350}
1351
Colin Cross97ba0732015-03-23 17:50:24 -07001352func (c *CCLibrary) installSharedLibrary(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001353 installDir := "lib"
Colin Cross97ba0732015-03-23 17:50:24 -07001354 if flags.Toolchain.Is64Bit() {
Dan Albertc403f7c2015-03-18 14:01:18 -07001355 installDir = "lib64"
1356 }
1357
Colin Crossfa138792015-04-24 17:31:52 -07001358 ctx.InstallFile(filepath.Join(installDir, c.Properties.Relative_install_path), c.out)
Dan Albertc403f7c2015-03-18 14:01:18 -07001359}
1360
Colin Cross97ba0732015-03-23 17:50:24 -07001361func (c *CCLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001362 if c.static() {
Dan Albertc403f7c2015-03-18 14:01:18 -07001363 c.installStaticLibrary(ctx, flags)
1364 } else {
1365 c.installSharedLibrary(ctx, flags)
1366 }
1367}
1368
Colin Cross3f40fa42015-01-30 17:27:36 -08001369//
1370// Objects (for crt*.o)
1371//
1372
Dan Albertc3144b12015-04-28 18:17:56 -07001373type ccObjectProvider interface {
1374 object() *ccObject
1375}
1376
Colin Cross3f40fa42015-01-30 17:27:36 -08001377type ccObject struct {
Colin Crossfa138792015-04-24 17:31:52 -07001378 CCBase
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001379 out common.OptionalPath
Colin Cross3f40fa42015-01-30 17:27:36 -08001380}
1381
Dan Albertc3144b12015-04-28 18:17:56 -07001382func (c *ccObject) object() *ccObject {
1383 return c
1384}
1385
Colin Cross97ba0732015-03-23 17:50:24 -07001386func CCObjectFactory() (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001387 module := &ccObject{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001388
Colin Crossfa138792015-04-24 17:31:52 -07001389 return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth)
Colin Cross3f40fa42015-01-30 17:27:36 -08001390}
1391
Colin Cross0676e2d2015-04-24 17:39:18 -07001392func (*ccObject) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Cross21b9a242015-03-24 14:15:58 -07001393 // object files can't have any dynamic dependencies
1394 return CCDeps{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001395}
1396
1397func (c *ccObject) compileModule(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001398 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001399
Colin Cross97ba0732015-03-23 17:50:24 -07001400 objFiles = append(objFiles, deps.ObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001401
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001402 var outputFile common.Path
Colin Cross3f40fa42015-01-30 17:27:36 -08001403 if len(objFiles) == 1 {
1404 outputFile = objFiles[0]
1405 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001406 output := common.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension)
1407 TransformObjsToObj(ctx, objFiles, ccFlagsToBuilderFlags(flags), output)
1408 outputFile = output
Colin Cross3f40fa42015-01-30 17:27:36 -08001409 }
1410
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001411 c.out = common.OptionalPathForPath(outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08001412
1413 ctx.CheckbuildFile(outputFile)
1414}
1415
Colin Cross97ba0732015-03-23 17:50:24 -07001416func (c *ccObject) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001417 // Object files do not get installed.
1418}
1419
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001420func (c *ccObject) outputFile() common.OptionalPath {
Colin Cross3f40fa42015-01-30 17:27:36 -08001421 return c.out
1422}
1423
Dan Albertc3144b12015-04-28 18:17:56 -07001424var _ ccObjectProvider = (*ccObject)(nil)
1425
Colin Cross3f40fa42015-01-30 17:27:36 -08001426//
1427// Executables
1428//
1429
Colin Cross7d5136f2015-05-11 13:39:40 -07001430type CCBinaryProperties struct {
1431 // compile executable with -static
Colin Cross06a931b2015-10-28 17:23:31 -07001432 Static_executable *bool
Colin Cross7d5136f2015-05-11 13:39:40 -07001433
1434 // set the name of the output
1435 Stem string `android:"arch_variant"`
1436
1437 // append to the name of the output
1438 Suffix string `android:"arch_variant"`
1439
1440 // if set, add an extra objcopy --prefix-symbols= step
1441 Prefix_symbols string
Colin Cross6002e052015-09-16 16:00:08 -07001442
1443 // Create a separate binary for each source file. Useful when there is
1444 // global state that can not be torn down and reset between each test suite.
Colin Cross06a931b2015-10-28 17:23:31 -07001445 Test_per_src *bool
Colin Cross7d5136f2015-05-11 13:39:40 -07001446}
1447
Colin Cross97ba0732015-03-23 17:50:24 -07001448type CCBinary struct {
Colin Crossfa138792015-04-24 17:31:52 -07001449 CCLinked
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001450 out common.Path
1451 installFile common.Path
Colin Cross7d5136f2015-05-11 13:39:40 -07001452 BinaryProperties CCBinaryProperties
Colin Cross3f40fa42015-01-30 17:27:36 -08001453}
1454
Colin Crossed4cf0b2015-03-26 14:43:45 -07001455func (c *CCBinary) buildStatic() bool {
Colin Cross06a931b2015-10-28 17:23:31 -07001456 return Bool(c.BinaryProperties.Static_executable)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001457}
1458
1459func (c *CCBinary) buildShared() bool {
Colin Cross06a931b2015-10-28 17:23:31 -07001460 return !Bool(c.BinaryProperties.Static_executable)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001461}
1462
Colin Cross97ba0732015-03-23 17:50:24 -07001463func (c *CCBinary) getStem(ctx common.AndroidModuleContext) string {
Colin Cross4ae185c2015-03-26 15:12:10 -07001464 stem := ctx.ModuleName()
Colin Cross97ba0732015-03-23 17:50:24 -07001465 if c.BinaryProperties.Stem != "" {
Colin Cross4ae185c2015-03-26 15:12:10 -07001466 stem = c.BinaryProperties.Stem
Colin Cross3f40fa42015-01-30 17:27:36 -08001467 }
Colin Cross4ae185c2015-03-26 15:12:10 -07001468
1469 return stem + c.BinaryProperties.Suffix
Colin Cross3f40fa42015-01-30 17:27:36 -08001470}
1471
Colin Cross0676e2d2015-04-24 17:39:18 -07001472func (c *CCBinary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Crosse11befc2015-04-27 17:49:17 -07001473 depNames = c.CCLinked.depNames(ctx, depNames)
Colin Crossf6566ed2015-03-24 11:13:38 -07001474 if ctx.Device() {
Dan Albertc3144b12015-04-28 18:17:56 -07001475 if c.Properties.Sdk_version == "" {
Colin Cross06a931b2015-10-28 17:23:31 -07001476 if Bool(c.BinaryProperties.Static_executable) {
Dan Albertc3144b12015-04-28 18:17:56 -07001477 depNames.CrtBegin = "crtbegin_static"
1478 } else {
1479 depNames.CrtBegin = "crtbegin_dynamic"
1480 }
1481 depNames.CrtEnd = "crtend_android"
Colin Cross3f40fa42015-01-30 17:27:36 -08001482 } else {
Colin Cross06a931b2015-10-28 17:23:31 -07001483 if Bool(c.BinaryProperties.Static_executable) {
Dan Albertc3144b12015-04-28 18:17:56 -07001484 depNames.CrtBegin = "ndk_crtbegin_static." + c.Properties.Sdk_version
1485 } else {
1486 depNames.CrtBegin = "ndk_crtbegin_dynamic." + c.Properties.Sdk_version
1487 }
1488 depNames.CrtEnd = "ndk_crtend_android." + c.Properties.Sdk_version
Colin Cross3f40fa42015-01-30 17:27:36 -08001489 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001490
Colin Cross06a931b2015-10-28 17:23:31 -07001491 if Bool(c.BinaryProperties.Static_executable) {
Colin Cross74d1ec02015-04-28 13:30:13 -07001492 if c.stl(ctx) == "libc++_static" {
1493 depNames.StaticLibs = append(depNames.StaticLibs, "libm", "libc", "libdl")
1494 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001495 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
1496 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
1497 // move them to the beginning of deps.LateStaticLibs
1498 var groupLibs []string
1499 depNames.StaticLibs, groupLibs = filterList(depNames.StaticLibs,
1500 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
1501 depNames.LateStaticLibs = append(groupLibs, depNames.LateStaticLibs...)
1502 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001503 }
Colin Cross21b9a242015-03-24 14:15:58 -07001504 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -08001505}
1506
Colin Cross97ba0732015-03-23 17:50:24 -07001507func NewCCBinary(binary *CCBinary, module CCModuleType,
Colin Cross1f8f2342015-03-26 16:09:47 -07001508 hod common.HostOrDeviceSupported, props ...interface{}) (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001509
Colin Cross1f8f2342015-03-26 16:09:47 -07001510 props = append(props, &binary.BinaryProperties)
1511
Colin Crossfa138792015-04-24 17:31:52 -07001512 return newCCDynamic(&binary.CCLinked, module, hod, common.MultilibFirst, props...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001513}
1514
Colin Cross97ba0732015-03-23 17:50:24 -07001515func CCBinaryFactory() (blueprint.Module, []interface{}) {
1516 module := &CCBinary{}
1517
1518 return NewCCBinary(module, module, common.HostAndDeviceSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001519}
1520
Colin Cross6362e272015-10-29 15:25:03 -07001521func (c *CCBinary) ModifyProperties(ctx CCModuleContext) {
Colin Cross0af4b842015-04-30 16:36:18 -07001522 if ctx.Darwin() {
Colin Cross06a931b2015-10-28 17:23:31 -07001523 c.BinaryProperties.Static_executable = proptools.BoolPtr(false)
Colin Cross0af4b842015-04-30 16:36:18 -07001524 }
Colin Cross06a931b2015-10-28 17:23:31 -07001525 if Bool(c.BinaryProperties.Static_executable) {
Colin Cross18b6dc52015-04-28 13:20:37 -07001526 c.dynamicProperties.VariantIsStaticBinary = true
1527 }
1528}
1529
Colin Cross0676e2d2015-04-24 17:39:18 -07001530func (c *CCBinary) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Crosse11befc2015-04-27 17:49:17 -07001531 flags = c.CCLinked.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001532
Dan Willemsen490fd492015-11-24 17:53:15 -08001533 if ctx.Host() {
1534 flags.LdFlags = append(flags.LdFlags, "-pie")
1535 if ctx.HostType() == common.Windows {
1536 flags.LdFlags = append(flags.LdFlags, "-Wl,-e_mainCRTStartup")
1537 }
1538 }
1539
1540 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
1541 // all code is position independent, and then those warnings get promoted to
1542 // errors.
1543 if ctx.HostType() != common.Windows {
1544 flags.CFlags = append(flags.CFlags, "-fpie")
1545 }
Colin Cross97ba0732015-03-23 17:50:24 -07001546
Colin Crossf6566ed2015-03-24 11:13:38 -07001547 if ctx.Device() {
Colin Cross06a931b2015-10-28 17:23:31 -07001548 if Bool(c.BinaryProperties.Static_executable) {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001549 // Clang driver needs -static to create static executable.
1550 // However, bionic/linker uses -shared to overwrite.
1551 // Linker for x86 targets does not allow coexistance of -static and -shared,
1552 // so we add -static only if -shared is not used.
1553 if !inList("-shared", flags.LdFlags) {
1554 flags.LdFlags = append(flags.LdFlags, "-static")
1555 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001556
Colin Crossed4cf0b2015-03-26 14:43:45 -07001557 flags.LdFlags = append(flags.LdFlags,
1558 "-nostdlib",
1559 "-Bstatic",
1560 "-Wl,--gc-sections",
1561 )
1562
1563 } else {
1564 linker := "/system/bin/linker"
1565 if flags.Toolchain.Is64Bit() {
1566 linker = "/system/bin/linker64"
1567 }
1568
1569 flags.LdFlags = append(flags.LdFlags,
Colin Cross979422c2015-12-01 14:09:48 -08001570 "-pie",
Colin Crossed4cf0b2015-03-26 14:43:45 -07001571 "-nostdlib",
1572 "-Bdynamic",
1573 fmt.Sprintf("-Wl,-dynamic-linker,%s", linker),
1574 "-Wl,--gc-sections",
1575 "-Wl,-z,nocopyreloc",
1576 )
1577 }
Colin Cross0af4b842015-04-30 16:36:18 -07001578 } else if ctx.Darwin() {
1579 flags.LdFlags = append(flags.LdFlags, "-Wl,-headerpad_max_install_names")
Colin Cross3f40fa42015-01-30 17:27:36 -08001580 }
1581
Colin Cross97ba0732015-03-23 17:50:24 -07001582 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001583}
1584
Colin Cross97ba0732015-03-23 17:50:24 -07001585func (c *CCBinary) compileModule(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001586 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001587
Colin Cross06a931b2015-10-28 17:23:31 -07001588 if !Bool(c.BinaryProperties.Static_executable) && inList("libc", c.Properties.Static_libs) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001589 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
1590 "from static libs or set static_executable: true")
1591 }
1592
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001593 outputFile := common.PathForModuleOut(ctx, c.getStem(ctx)+flags.Toolchain.ExecutableSuffix())
Dan Albertc403f7c2015-03-18 14:01:18 -07001594 c.out = outputFile
Colin Crossbfae8852015-03-26 14:44:11 -07001595 if c.BinaryProperties.Prefix_symbols != "" {
1596 afterPrefixSymbols := outputFile
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001597 outputFile = common.PathForModuleOut(ctx, c.getStem(ctx)+".intermediate")
Colin Crossbfae8852015-03-26 14:44:11 -07001598 TransformBinaryPrefixSymbols(ctx, c.BinaryProperties.Prefix_symbols, outputFile,
1599 ccFlagsToBuilderFlags(flags), afterPrefixSymbols)
1600 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001601
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001602 var linkerDeps common.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07001603
Colin Cross97ba0732015-03-23 17:50:24 -07001604 TransformObjToDynamicBinary(ctx, objFiles, deps.SharedLibs, deps.StaticLibs,
Colin Crossaee540a2015-07-06 17:48:31 -07001605 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
Colin Cross77b00fa2015-03-16 16:15:49 -07001606 ccFlagsToBuilderFlags(flags), outputFile)
Dan Albertc403f7c2015-03-18 14:01:18 -07001607}
Colin Cross3f40fa42015-01-30 17:27:36 -08001608
Colin Cross97ba0732015-03-23 17:50:24 -07001609func (c *CCBinary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Colin Crossd350ecd2015-04-28 13:25:36 -07001610 c.installFile = ctx.InstallFile(filepath.Join("bin", c.Properties.Relative_install_path), c.out)
1611}
1612
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001613func (c *CCBinary) HostToolPath() common.OptionalPath {
Colin Crossd350ecd2015-04-28 13:25:36 -07001614 if c.HostOrDevice().Host() {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001615 return common.OptionalPathForPath(c.installFile)
Colin Crossd350ecd2015-04-28 13:25:36 -07001616 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001617 return common.OptionalPath{}
Dan Albertc403f7c2015-03-18 14:01:18 -07001618}
1619
Colin Cross6002e052015-09-16 16:00:08 -07001620func (c *CCBinary) testPerSrc() bool {
Colin Cross06a931b2015-10-28 17:23:31 -07001621 return Bool(c.BinaryProperties.Test_per_src)
Colin Cross6002e052015-09-16 16:00:08 -07001622}
1623
1624func (c *CCBinary) binary() *CCBinary {
1625 return c
1626}
1627
1628type testPerSrc interface {
1629 binary() *CCBinary
1630 testPerSrc() bool
1631}
1632
1633var _ testPerSrc = (*CCBinary)(nil)
1634
Colin Cross6362e272015-10-29 15:25:03 -07001635func testPerSrcMutator(mctx common.AndroidBottomUpMutatorContext) {
Colin Cross6002e052015-09-16 16:00:08 -07001636 if test, ok := mctx.Module().(testPerSrc); ok {
1637 if test.testPerSrc() {
1638 testNames := make([]string, len(test.binary().Properties.Srcs))
1639 for i, src := range test.binary().Properties.Srcs {
1640 testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
1641 }
1642 tests := mctx.CreateLocalVariations(testNames...)
1643 for i, src := range test.binary().Properties.Srcs {
1644 tests[i].(testPerSrc).binary().Properties.Srcs = []string{src}
1645 tests[i].(testPerSrc).binary().BinaryProperties.Stem = mctx.ModuleName() + "_" + testNames[i]
1646 }
1647 }
1648 }
Colin Cross7d5136f2015-05-11 13:39:40 -07001649}
1650
Colin Cross9ffb4f52015-04-24 17:48:09 -07001651type CCTest struct {
Colin Cross97ba0732015-03-23 17:50:24 -07001652 CCBinary
Dan Albertc403f7c2015-03-18 14:01:18 -07001653}
1654
Colin Cross9ffb4f52015-04-24 17:48:09 -07001655func (c *CCTest) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Cross0676e2d2015-04-24 17:39:18 -07001656 flags = c.CCBinary.flags(ctx, flags)
Dan Albertc403f7c2015-03-18 14:01:18 -07001657
Colin Cross97ba0732015-03-23 17:50:24 -07001658 flags.CFlags = append(flags.CFlags, "-DGTEST_HAS_STD_STRING")
Colin Crossf6566ed2015-03-24 11:13:38 -07001659 if ctx.Host() {
Colin Cross97ba0732015-03-23 17:50:24 -07001660 flags.CFlags = append(flags.CFlags, "-O0", "-g")
Colin Cross28344522015-04-22 13:07:53 -07001661 flags.LdFlags = append(flags.LdFlags, "-lpthread")
Dan Albertc403f7c2015-03-18 14:01:18 -07001662 }
1663
1664 // TODO(danalbert): Make gtest export its dependencies.
Colin Cross28344522015-04-22 13:07:53 -07001665 flags.CFlags = append(flags.CFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001666 "-I"+common.PathForSource(ctx, "external/gtest/include").String())
Dan Albertc403f7c2015-03-18 14:01:18 -07001667
Colin Cross21b9a242015-03-24 14:15:58 -07001668 return flags
Dan Albertc403f7c2015-03-18 14:01:18 -07001669}
1670
Colin Cross9ffb4f52015-04-24 17:48:09 -07001671func (c *CCTest) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Dan Willemsene6540452015-10-20 15:21:33 -07001672 depNames.StaticLibs = append(depNames.StaticLibs, "libgtest_main", "libgtest")
Colin Crossa8a93d32015-04-28 13:26:49 -07001673 depNames = c.CCBinary.depNames(ctx, depNames)
Colin Cross21b9a242015-03-24 14:15:58 -07001674 return depNames
Dan Albertc403f7c2015-03-18 14:01:18 -07001675}
1676
Colin Cross9ffb4f52015-04-24 17:48:09 -07001677func (c *CCTest) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Colin Crossf6566ed2015-03-24 11:13:38 -07001678 if ctx.Device() {
Colin Crossa8a93d32015-04-28 13:26:49 -07001679 ctx.InstallFile("../data/nativetest"+ctx.Arch().ArchType.Multilib[3:]+"/"+ctx.ModuleName(), c.out)
Dan Albertc403f7c2015-03-18 14:01:18 -07001680 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001681 c.CCBinary.installModule(ctx, flags)
Dan Albertc403f7c2015-03-18 14:01:18 -07001682 }
1683}
1684
Colin Cross9ffb4f52015-04-24 17:48:09 -07001685func NewCCTest(test *CCTest, module CCModuleType,
1686 hod common.HostOrDeviceSupported, props ...interface{}) (blueprint.Module, []interface{}) {
1687
Colin Cross9ffb4f52015-04-24 17:48:09 -07001688 return NewCCBinary(&test.CCBinary, module, hod, props...)
1689}
1690
1691func CCTestFactory() (blueprint.Module, []interface{}) {
1692 module := &CCTest{}
1693
1694 return NewCCTest(module, module, common.HostAndDeviceSupported)
1695}
1696
Colin Cross2ba19d92015-05-07 15:44:20 -07001697type CCBenchmark struct {
1698 CCBinary
1699}
1700
1701func (c *CCBenchmark) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
1702 depNames = c.CCBinary.depNames(ctx, depNames)
Dan Willemsenf8e98b02015-09-11 17:41:44 -07001703 depNames.StaticLibs = append(depNames.StaticLibs, "libbenchmark", "libbase")
Colin Cross2ba19d92015-05-07 15:44:20 -07001704 return depNames
1705}
1706
1707func (c *CCBenchmark) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
1708 if ctx.Device() {
1709 ctx.InstallFile("../data/nativetest"+ctx.Arch().ArchType.Multilib[3:]+"/"+ctx.ModuleName(), c.out)
1710 } else {
1711 c.CCBinary.installModule(ctx, flags)
1712 }
1713}
1714
1715func NewCCBenchmark(test *CCBenchmark, module CCModuleType,
1716 hod common.HostOrDeviceSupported, props ...interface{}) (blueprint.Module, []interface{}) {
1717
1718 return NewCCBinary(&test.CCBinary, module, hod, props...)
1719}
1720
1721func CCBenchmarkFactory() (blueprint.Module, []interface{}) {
1722 module := &CCBenchmark{}
1723
1724 return NewCCBenchmark(module, module, common.HostAndDeviceSupported)
1725}
1726
Colin Cross3f40fa42015-01-30 17:27:36 -08001727//
1728// Static library
1729//
1730
Colin Cross97ba0732015-03-23 17:50:24 -07001731func CCLibraryStaticFactory() (blueprint.Module, []interface{}) {
1732 module := &CCLibrary{}
1733 module.LibraryProperties.BuildStatic = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001734
Colin Cross97ba0732015-03-23 17:50:24 -07001735 return NewCCLibrary(module, module, common.HostAndDeviceSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001736}
1737
1738//
1739// Shared libraries
1740//
1741
Colin Cross97ba0732015-03-23 17:50:24 -07001742func CCLibrarySharedFactory() (blueprint.Module, []interface{}) {
1743 module := &CCLibrary{}
1744 module.LibraryProperties.BuildShared = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001745
Colin Cross97ba0732015-03-23 17:50:24 -07001746 return NewCCLibrary(module, module, common.HostAndDeviceSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001747}
1748
1749//
1750// Host static library
1751//
1752
Colin Cross97ba0732015-03-23 17:50:24 -07001753func CCLibraryHostStaticFactory() (blueprint.Module, []interface{}) {
1754 module := &CCLibrary{}
1755 module.LibraryProperties.BuildStatic = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001756
Colin Cross97ba0732015-03-23 17:50:24 -07001757 return NewCCLibrary(module, module, common.HostSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001758}
1759
1760//
1761// Host Shared libraries
1762//
1763
Colin Cross97ba0732015-03-23 17:50:24 -07001764func CCLibraryHostSharedFactory() (blueprint.Module, []interface{}) {
1765 module := &CCLibrary{}
1766 module.LibraryProperties.BuildShared = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001767
Colin Cross97ba0732015-03-23 17:50:24 -07001768 return NewCCLibrary(module, module, common.HostSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001769}
1770
1771//
1772// Host Binaries
1773//
1774
Colin Cross97ba0732015-03-23 17:50:24 -07001775func CCBinaryHostFactory() (blueprint.Module, []interface{}) {
1776 module := &CCBinary{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001777
Colin Cross97ba0732015-03-23 17:50:24 -07001778 return NewCCBinary(module, module, common.HostSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001779}
1780
1781//
Colin Cross1f8f2342015-03-26 16:09:47 -07001782// Host Tests
1783//
1784
1785func CCTestHostFactory() (blueprint.Module, []interface{}) {
Colin Cross9ffb4f52015-04-24 17:48:09 -07001786 module := &CCTest{}
Colin Cross6002e052015-09-16 16:00:08 -07001787 return NewCCBinary(&module.CCBinary, module, common.HostSupported)
Colin Cross1f8f2342015-03-26 16:09:47 -07001788}
1789
1790//
Colin Cross2ba19d92015-05-07 15:44:20 -07001791// Host Benchmarks
1792//
1793
1794func CCBenchmarkHostFactory() (blueprint.Module, []interface{}) {
1795 module := &CCBenchmark{}
1796 return NewCCBinary(&module.CCBinary, module, common.HostSupported)
1797}
1798
1799//
Colin Crosscfad1192015-11-02 16:43:11 -08001800// Defaults
1801//
1802type CCDefaults struct {
1803 common.AndroidModuleBase
1804 common.DefaultsModule
1805}
1806
1807func (*CCDefaults) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
1808}
1809
1810func CCDefaultsFactory() (blueprint.Module, []interface{}) {
1811 module := &CCDefaults{}
1812
1813 propertyStructs := []interface{}{
1814 &CCBaseProperties{},
1815 &CCLibraryProperties{},
1816 &CCBinaryProperties{},
1817 &CCUnusedProperties{},
1818 }
1819
Dan Willemsen218f6562015-07-08 18:13:11 -07001820 _, propertyStructs = common.InitAndroidArchModule(module, common.HostAndDeviceDefault,
1821 common.MultilibDefault, propertyStructs...)
Colin Crosscfad1192015-11-02 16:43:11 -08001822
1823 return common.InitDefaultsModule(module, module, propertyStructs...)
1824}
1825
1826//
Colin Cross3f40fa42015-01-30 17:27:36 -08001827// Device libraries shipped with gcc
1828//
1829
1830type toolchainLibrary struct {
Colin Cross97ba0732015-03-23 17:50:24 -07001831 CCLibrary
Colin Cross3f40fa42015-01-30 17:27:36 -08001832}
1833
Colin Cross0676e2d2015-04-24 17:39:18 -07001834func (*toolchainLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Cross3f40fa42015-01-30 17:27:36 -08001835 // toolchain libraries can't have any dependencies
Colin Cross21b9a242015-03-24 14:15:58 -07001836 return CCDeps{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001837}
1838
Colin Cross97ba0732015-03-23 17:50:24 -07001839func ToolchainLibraryFactory() (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001840 module := &toolchainLibrary{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001841
Colin Cross97ba0732015-03-23 17:50:24 -07001842 module.LibraryProperties.BuildStatic = true
1843
Colin Crossfa138792015-04-24 17:31:52 -07001844 return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth,
Colin Cross21b9a242015-03-24 14:15:58 -07001845 &module.LibraryProperties)
Colin Cross3f40fa42015-01-30 17:27:36 -08001846}
1847
1848func (c *toolchainLibrary) compileModule(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001849 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001850
1851 libName := ctx.ModuleName() + staticLibraryExtension
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001852 outputFile := common.PathForModuleOut(ctx, libName)
Colin Cross3f40fa42015-01-30 17:27:36 -08001853
1854 CopyGccLib(ctx, libName, ccFlagsToBuilderFlags(flags), outputFile)
1855
1856 c.out = outputFile
1857
1858 ctx.CheckbuildFile(outputFile)
1859}
1860
Colin Cross97ba0732015-03-23 17:50:24 -07001861func (c *toolchainLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001862 // Toolchain libraries do not get installed.
1863}
1864
Dan Albertbe961682015-03-18 23:38:50 -07001865// NDK prebuilt libraries.
1866//
1867// These differ from regular prebuilts in that they aren't stripped and usually aren't installed
1868// either (with the exception of the shared STLs, which are installed to the app's directory rather
1869// than to the system image).
1870
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001871func getNdkLibDir(ctx common.AndroidModuleContext, toolchain Toolchain, version string) common.SourcePath {
1872 return common.PathForSource(ctx, fmt.Sprintf("prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/lib",
1873 version, toolchain.Name()))
Dan Albertbe961682015-03-18 23:38:50 -07001874}
1875
Dan Albertc3144b12015-04-28 18:17:56 -07001876func ndkPrebuiltModuleToPath(ctx common.AndroidModuleContext, toolchain Toolchain,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001877 ext string, version string) common.Path {
Dan Albertc3144b12015-04-28 18:17:56 -07001878
1879 // NDK prebuilts are named like: ndk_NAME.EXT.SDK_VERSION.
1880 // We want to translate to just NAME.EXT
1881 name := strings.Split(strings.TrimPrefix(ctx.ModuleName(), "ndk_"), ".")[0]
1882 dir := getNdkLibDir(ctx, toolchain, version)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001883 return dir.Join(ctx, name+ext)
Dan Albertc3144b12015-04-28 18:17:56 -07001884}
1885
1886type ndkPrebuiltObject struct {
1887 ccObject
1888}
1889
Dan Albertc3144b12015-04-28 18:17:56 -07001890func (*ndkPrebuiltObject) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
1891 // NDK objects can't have any dependencies
1892 return CCDeps{}
1893}
1894
1895func NdkPrebuiltObjectFactory() (blueprint.Module, []interface{}) {
1896 module := &ndkPrebuiltObject{}
1897 return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth)
1898}
1899
1900func (c *ndkPrebuiltObject) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001901 deps CCPathDeps, objFiles common.Paths) {
Dan Albertc3144b12015-04-28 18:17:56 -07001902 // A null build step, but it sets up the output path.
1903 if !strings.HasPrefix(ctx.ModuleName(), "ndk_crt") {
1904 ctx.ModuleErrorf("NDK prebuilts must have an ndk_crt prefixed name")
1905 }
1906
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001907 c.out = common.OptionalPathForPath(ndkPrebuiltModuleToPath(ctx, flags.Toolchain, objectExtension, c.Properties.Sdk_version))
Dan Albertc3144b12015-04-28 18:17:56 -07001908}
1909
1910func (c *ndkPrebuiltObject) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
1911 // Objects do not get installed.
1912}
1913
1914var _ ccObjectProvider = (*ndkPrebuiltObject)(nil)
1915
Dan Albertbe961682015-03-18 23:38:50 -07001916type ndkPrebuiltLibrary struct {
1917 CCLibrary
1918}
1919
Colin Cross0676e2d2015-04-24 17:39:18 -07001920func (*ndkPrebuiltLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Dan Albertbe961682015-03-18 23:38:50 -07001921 // NDK libraries can't have any dependencies
1922 return CCDeps{}
1923}
1924
1925func NdkPrebuiltLibraryFactory() (blueprint.Module, []interface{}) {
1926 module := &ndkPrebuiltLibrary{}
1927 module.LibraryProperties.BuildShared = true
1928 return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
1929}
1930
1931func (c *ndkPrebuiltLibrary) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001932 deps CCPathDeps, objFiles common.Paths) {
Dan Albertbe961682015-03-18 23:38:50 -07001933 // A null build step, but it sets up the output path.
1934 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
1935 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
1936 }
1937
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001938 includeDirs := common.PathsForModuleSrc(ctx, c.Properties.Export_include_dirs)
1939 c.exportFlags = []string{common.JoinWithPrefix(includeDirs.Strings(), "-isystem ")}
Dan Albertbe961682015-03-18 23:38:50 -07001940
Dan Willemsen490fd492015-11-24 17:53:15 -08001941 c.out = ndkPrebuiltModuleToPath(ctx, flags.Toolchain, flags.Toolchain.ShlibSuffix(),
Dan Albertc3144b12015-04-28 18:17:56 -07001942 c.Properties.Sdk_version)
Dan Albertbe961682015-03-18 23:38:50 -07001943}
1944
1945func (c *ndkPrebuiltLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc3144b12015-04-28 18:17:56 -07001946 // NDK prebuilt libraries do not get installed.
Dan Albertbe961682015-03-18 23:38:50 -07001947}
1948
1949// The NDK STLs are slightly different from the prebuilt system libraries:
1950// * Are not specific to each platform version.
1951// * The libraries are not in a predictable location for each STL.
1952
1953type ndkPrebuiltStl struct {
1954 ndkPrebuiltLibrary
1955}
1956
1957type ndkPrebuiltStaticStl struct {
1958 ndkPrebuiltStl
1959}
1960
1961type ndkPrebuiltSharedStl struct {
1962 ndkPrebuiltStl
1963}
1964
1965func NdkPrebuiltSharedStlFactory() (blueprint.Module, []interface{}) {
1966 module := &ndkPrebuiltSharedStl{}
1967 module.LibraryProperties.BuildShared = true
1968 return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
1969}
1970
1971func NdkPrebuiltStaticStlFactory() (blueprint.Module, []interface{}) {
1972 module := &ndkPrebuiltStaticStl{}
1973 module.LibraryProperties.BuildStatic = true
1974 return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
1975}
1976
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001977func getNdkStlLibDir(ctx common.AndroidModuleContext, toolchain Toolchain, stl string) common.SourcePath {
Dan Albertbe961682015-03-18 23:38:50 -07001978 gccVersion := toolchain.GccVersion()
1979 var libDir string
1980 switch stl {
1981 case "libstlport":
1982 libDir = "cxx-stl/stlport/libs"
1983 case "libc++":
1984 libDir = "cxx-stl/llvm-libc++/libs"
1985 case "libgnustl":
1986 libDir = fmt.Sprintf("cxx-stl/gnu-libstdc++/%s/libs", gccVersion)
1987 }
1988
1989 if libDir != "" {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001990 ndkSrcRoot := "prebuilts/ndk/current/sources"
1991 return common.PathForSource(ctx, ndkSrcRoot).Join(ctx, libDir, ctx.Arch().Abi[0])
Dan Albertbe961682015-03-18 23:38:50 -07001992 }
1993
1994 ctx.ModuleErrorf("Unknown NDK STL: %s", stl)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001995 return common.PathForSource(ctx, "")
Dan Albertbe961682015-03-18 23:38:50 -07001996}
1997
1998func (c *ndkPrebuiltStl) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001999 deps CCPathDeps, objFiles common.Paths) {
Dan Albertbe961682015-03-18 23:38:50 -07002000 // A null build step, but it sets up the output path.
2001 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
2002 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
2003 }
2004
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002005 includeDirs := common.PathsForModuleSrc(ctx, c.Properties.Export_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -07002006 c.exportFlags = []string{includeDirsToFlags(includeDirs)}
Dan Albertbe961682015-03-18 23:38:50 -07002007
2008 libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_")
Dan Willemsen490fd492015-11-24 17:53:15 -08002009 libExt := flags.Toolchain.ShlibSuffix()
Dan Albertbe961682015-03-18 23:38:50 -07002010 if c.LibraryProperties.BuildStatic {
2011 libExt = staticLibraryExtension
2012 }
2013
2014 stlName := strings.TrimSuffix(libName, "_shared")
2015 stlName = strings.TrimSuffix(stlName, "_static")
2016 libDir := getNdkStlLibDir(ctx, flags.Toolchain, stlName)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002017 c.out = libDir.Join(ctx, libName+libExt)
Dan Albertbe961682015-03-18 23:38:50 -07002018}
2019
Colin Cross6362e272015-10-29 15:25:03 -07002020func linkageMutator(mctx common.AndroidBottomUpMutatorContext) {
Colin Crossed4cf0b2015-03-26 14:43:45 -07002021 if c, ok := mctx.Module().(ccLinkedInterface); ok {
Colin Cross3f40fa42015-01-30 17:27:36 -08002022 var modules []blueprint.Module
Colin Crossed4cf0b2015-03-26 14:43:45 -07002023 if c.buildStatic() && c.buildShared() {
Colin Cross3f40fa42015-01-30 17:27:36 -08002024 modules = mctx.CreateLocalVariations("static", "shared")
Colin Cross18b6dc52015-04-28 13:20:37 -07002025 modules[0].(ccLinkedInterface).setStatic(true)
2026 modules[1].(ccLinkedInterface).setStatic(false)
Colin Crossed4cf0b2015-03-26 14:43:45 -07002027 } else if c.buildStatic() {
Colin Cross3f40fa42015-01-30 17:27:36 -08002028 modules = mctx.CreateLocalVariations("static")
Colin Cross18b6dc52015-04-28 13:20:37 -07002029 modules[0].(ccLinkedInterface).setStatic(true)
Colin Crossed4cf0b2015-03-26 14:43:45 -07002030 } else if c.buildShared() {
Colin Cross3f40fa42015-01-30 17:27:36 -08002031 modules = mctx.CreateLocalVariations("shared")
Colin Cross18b6dc52015-04-28 13:20:37 -07002032 modules[0].(ccLinkedInterface).setStatic(false)
Colin Cross3f40fa42015-01-30 17:27:36 -08002033 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07002034 panic(fmt.Errorf("ccLibrary %q not static or shared", mctx.ModuleName()))
Colin Cross3f40fa42015-01-30 17:27:36 -08002035 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07002036
2037 if _, ok := c.(ccLibraryInterface); ok {
2038 reuseFrom := modules[0].(ccLibraryInterface)
2039 for _, m := range modules {
2040 m.(ccLibraryInterface).setReuseFrom(reuseFrom)
Colin Cross3f40fa42015-01-30 17:27:36 -08002041 }
2042 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002043 }
2044}
Colin Cross74d1ec02015-04-28 13:30:13 -07002045
2046// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
2047// modifies the slice contents in place, and returns a subslice of the original slice
2048func lastUniqueElements(list []string) []string {
2049 totalSkip := 0
2050 for i := len(list) - 1; i >= totalSkip; i-- {
2051 skip := 0
2052 for j := i - 1; j >= totalSkip; j-- {
2053 if list[i] == list[j] {
2054 skip++
2055 } else {
2056 list[j+skip] = list[j]
2057 }
2058 }
2059 totalSkip += skip
2060 }
2061 return list[totalSkip:]
2062}
Colin Cross06a931b2015-10-28 17:23:31 -07002063
2064var Bool = proptools.Bool