blob: ce3a464ceccd7ac5f6b4989925243b4943c3a850 [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 Willemsen7b310ee2015-12-18 15:11:17 -0800128 pctx.PrefixedPathsForOptionalSourceVariable("commonGlobalIncludes", "-isystem ",
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700129 []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 })
Dan Willemsene0378dd2016-01-07 17:42:34 -0800140 // This is used by non-NDK modules to get jni.h. export_include_dirs doesn't help
141 // with this, since there is no associated library.
142 pctx.PrefixedPathsForOptionalSourceVariable("commonNativehelperInclude", "-I",
143 []string{"libnativehelper/include/nativehelper"})
Colin Cross3f40fa42015-01-30 17:27:36 -0800144
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700145 pctx.SourcePathVariable("clangPath", "prebuilts/clang/host/${HostPrebuiltTag}/3.8/bin")
Colin Cross3f40fa42015-01-30 17:27:36 -0800146}
147
Colin Cross6362e272015-10-29 15:25:03 -0700148type CCModuleContext common.AndroidBaseContext
149
Colin Cross3f40fa42015-01-30 17:27:36 -0800150// Building C/C++ code is handled by objects that satisfy this interface via composition
Colin Cross97ba0732015-03-23 17:50:24 -0700151type CCModuleType interface {
Colin Cross3f40fa42015-01-30 17:27:36 -0800152 common.AndroidModule
153
Colin Crossfa138792015-04-24 17:31:52 -0700154 // Modify property values after parsing Blueprints file but before starting dependency
155 // resolution or build rule generation
Colin Cross6362e272015-10-29 15:25:03 -0700156 ModifyProperties(CCModuleContext)
Colin Crossfa138792015-04-24 17:31:52 -0700157
Colin Cross21b9a242015-03-24 14:15:58 -0700158 // Modify the ccFlags
Colin Cross0676e2d2015-04-24 17:39:18 -0700159 flags(common.AndroidModuleContext, CCFlags) CCFlags
Colin Cross3f40fa42015-01-30 17:27:36 -0800160
Colin Cross6362e272015-10-29 15:25:03 -0700161 // Return list of dependency names for use in depsMutator
Colin Cross0676e2d2015-04-24 17:39:18 -0700162 depNames(common.AndroidBaseContext, CCDeps) CCDeps
Colin Cross3f40fa42015-01-30 17:27:36 -0800163
Colin Cross6362e272015-10-29 15:25:03 -0700164 // Add dynamic dependencies
165 depsMutator(common.AndroidBottomUpMutatorContext)
166
Colin Cross3f40fa42015-01-30 17:27:36 -0800167 // Compile objects into final module
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700168 compileModule(common.AndroidModuleContext, CCFlags, CCPathDeps, common.Paths)
Colin Cross3f40fa42015-01-30 17:27:36 -0800169
Dan Albertc403f7c2015-03-18 14:01:18 -0700170 // Install the built module.
Colin Cross97ba0732015-03-23 17:50:24 -0700171 installModule(common.AndroidModuleContext, CCFlags)
Dan Albertc403f7c2015-03-18 14:01:18 -0700172
Colin Cross3f40fa42015-01-30 17:27:36 -0800173 // Return the output file (.o, .a or .so) for use by other modules
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700174 outputFile() common.OptionalPath
Colin Cross3f40fa42015-01-30 17:27:36 -0800175}
176
Colin Cross97ba0732015-03-23 17:50:24 -0700177type CCDeps struct {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700178 StaticLibs, SharedLibs, LateStaticLibs, WholeStaticLibs []string
Colin Crossc472d572015-03-17 15:06:21 -0700179
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700180 ObjFiles common.Paths
181
182 Cflags, ReexportedCflags []string
Colin Cross21b9a242015-03-24 14:15:58 -0700183
Colin Cross97ba0732015-03-23 17:50:24 -0700184 CrtBegin, CrtEnd string
Colin Crossc472d572015-03-17 15:06:21 -0700185}
186
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700187type CCPathDeps struct {
188 StaticLibs, SharedLibs, LateStaticLibs, WholeStaticLibs common.Paths
189
190 ObjFiles common.Paths
191 WholeStaticLibObjFiles common.Paths
192
193 Cflags, ReexportedCflags []string
194
195 CrtBegin, CrtEnd common.OptionalPath
196}
197
Colin Cross97ba0732015-03-23 17:50:24 -0700198type CCFlags struct {
Colin Cross28344522015-04-22 13:07:53 -0700199 GlobalFlags []string // Flags that apply to C, C++, and assembly source files
200 AsFlags []string // Flags that apply to assembly source files
201 CFlags []string // Flags that apply to C and C++ source files
202 ConlyFlags []string // Flags that apply to C source files
203 CppFlags []string // Flags that apply to C++ source files
204 YaccFlags []string // Flags that apply to Yacc source files
205 LdFlags []string // Flags that apply to linker command lines
206
207 Nocrt bool
208 Toolchain Toolchain
209 Clang bool
Colin Crossc472d572015-03-17 15:06:21 -0700210}
211
Colin Cross7d5136f2015-05-11 13:39:40 -0700212// Properties used to compile all C or C++ modules
213type CCBaseProperties struct {
214 // 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 -0700215 Srcs []string `android:"arch_variant"`
216
217 // list of source files that should not be used to build the C/C++ module.
218 // This is most useful in the arch/multilib variants to remove non-common files
219 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700220
221 // list of module-specific flags that will be used for C and C++ compiles.
222 Cflags []string `android:"arch_variant"`
223
224 // list of module-specific flags that will be used for C++ compiles
225 Cppflags []string `android:"arch_variant"`
226
227 // list of module-specific flags that will be used for C compiles
228 Conlyflags []string `android:"arch_variant"`
229
230 // list of module-specific flags that will be used for .S compiles
231 Asflags []string `android:"arch_variant"`
232
233 // list of module-specific flags that will be used for .y and .yy compiles
234 Yaccflags []string
235
236 // list of module-specific flags that will be used for all link steps
237 Ldflags []string `android:"arch_variant"`
238
239 // the instruction set architecture to use to compile the C/C++
240 // module.
241 Instruction_set string `android:"arch_variant"`
242
243 // list of directories relative to the root of the source tree that will
244 // be added to the include path using -I.
245 // If possible, don't use this. If adding paths from the current directory use
246 // local_include_dirs, if adding paths from other modules use export_include_dirs in
247 // that module.
248 Include_dirs []string `android:"arch_variant"`
249
Colin Cross39d97f22015-09-14 12:30:50 -0700250 // list of files relative to the root of the source tree that will be included
251 // using -include.
252 // If possible, don't use this.
253 Include_files []string `android:"arch_variant"`
254
Colin Cross7d5136f2015-05-11 13:39:40 -0700255 // list of directories relative to the Blueprints file that will
256 // be added to the include path using -I
257 Local_include_dirs []string `android:"arch_variant"`
258
Colin Cross39d97f22015-09-14 12:30:50 -0700259 // list of files relative to the Blueprints file that will be included
260 // using -include.
261 // If possible, don't use this.
262 Local_include_files []string `android:"arch_variant"`
263
Colin Cross7d5136f2015-05-11 13:39:40 -0700264 // list of directories relative to the Blueprints file that will
265 // be added to the include path using -I for any module that links against this module
266 Export_include_dirs []string `android:"arch_variant"`
267
268 // list of module-specific flags that will be used for C and C++ compiles when
269 // compiling with clang
270 Clang_cflags []string `android:"arch_variant"`
271
272 // list of module-specific flags that will be used for .S compiles when
273 // compiling with clang
274 Clang_asflags []string `android:"arch_variant"`
275
276 // list of system libraries that will be dynamically linked to
277 // shared library and executable modules. If unset, generally defaults to libc
278 // and libm. Set to [] to prevent linking against libc and libm.
279 System_shared_libs []string
280
281 // list of modules whose object files should be linked into this module
282 // in their entirety. For static library modules, all of the .o files from the intermediate
283 // directory of the dependency will be linked into this modules .a file. For a shared library,
284 // the dependency's .a file will be linked into this module using -Wl,--whole-archive.
285 Whole_static_libs []string `android:"arch_variant"`
286
287 // list of modules that should be statically linked into this module.
288 Static_libs []string `android:"arch_variant"`
289
290 // list of modules that should be dynamically linked into this module.
291 Shared_libs []string `android:"arch_variant"`
292
293 // allow the module to contain undefined symbols. By default,
294 // modules cannot contain undefined symbols that are not satisified by their immediate
295 // dependencies. Set this flag to true to remove --no-undefined from the linker flags.
296 // This flag should only be necessary for compiling low-level libraries like libc.
Colin Cross06a931b2015-10-28 17:23:31 -0700297 Allow_undefined_symbols *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700298
299 // don't link in crt_begin and crt_end. This flag should only be necessary for
300 // compiling crt or libc.
Colin Cross06a931b2015-10-28 17:23:31 -0700301 Nocrt *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700302
Dan Willemsend67be222015-09-16 15:19:33 -0700303 // don't link in libgcc.a
Colin Cross06a931b2015-10-28 17:23:31 -0700304 No_libgcc *bool
Dan Willemsend67be222015-09-16 15:19:33 -0700305
Colin Cross7d5136f2015-05-11 13:39:40 -0700306 // don't insert default compiler flags into asflags, cflags,
307 // cppflags, conlyflags, ldflags, or include_dirs
Colin Cross06a931b2015-10-28 17:23:31 -0700308 No_default_compiler_flags *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700309
310 // compile module with clang instead of gcc
Colin Cross06a931b2015-10-28 17:23:31 -0700311 Clang *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700312
313 // pass -frtti instead of -fno-rtti
Colin Cross06a931b2015-10-28 17:23:31 -0700314 Rtti *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700315
316 // -l arguments to pass to linker for host-provided shared libraries
317 Host_ldlibs []string `android:"arch_variant"`
318
319 // select the STL library to use. Possible values are "libc++", "libc++_static",
320 // "stlport", "stlport_static", "ndk", "libstdc++", or "none". Leave blank to select the
321 // default
322 Stl string
323
324 // Set for combined shared/static libraries to prevent compiling object files a second time
325 SkipCompileObjs bool `blueprint:"mutated"`
326
327 Debug, Release struct {
328 // list of module-specific flags that will be used for C and C++ compiles in debug or
329 // release builds
330 Cflags []string `android:"arch_variant"`
331 } `android:"arch_variant"`
332
333 // Minimum sdk version supported when compiling against the ndk
334 Sdk_version string
335
336 // install to a subdirectory of the default install path for the module
337 Relative_install_path string
338}
339
Colin Crosscfad1192015-11-02 16:43:11 -0800340type CCUnusedProperties struct {
341 Native_coverage *bool
342 Required []string
343 Sanitize []string `android:"arch_variant"`
344 Sanitize_recover []string
345 Strip string
346 Tags []string
347}
348
Colin Crossfa138792015-04-24 17:31:52 -0700349// CCBase contains the properties and members used by all C/C++ module types, and implements
Colin Crossc472d572015-03-17 15:06:21 -0700350// the blueprint.Module interface. It expects to be embedded into an outer specialization struct,
351// and uses a ccModuleType interface to that struct to create the build steps.
Colin Crossfa138792015-04-24 17:31:52 -0700352type CCBase struct {
Colin Crossc472d572015-03-17 15:06:21 -0700353 common.AndroidModuleBase
Colin Crosscfad1192015-11-02 16:43:11 -0800354 common.DefaultableModule
Colin Cross97ba0732015-03-23 17:50:24 -0700355 module CCModuleType
Colin Crossc472d572015-03-17 15:06:21 -0700356
Colin Cross7d5136f2015-05-11 13:39:40 -0700357 Properties CCBaseProperties
Colin Crossfa138792015-04-24 17:31:52 -0700358
Colin Crosscfad1192015-11-02 16:43:11 -0800359 unused CCUnusedProperties
Colin Crossc472d572015-03-17 15:06:21 -0700360
361 installPath string
Colin Cross74d1ec02015-04-28 13:30:13 -0700362
363 savedDepNames CCDeps
Colin Crossc472d572015-03-17 15:06:21 -0700364}
365
Colin Crossfa138792015-04-24 17:31:52 -0700366func newCCBase(base *CCBase, module CCModuleType, hod common.HostOrDeviceSupported,
Colin Crossc472d572015-03-17 15:06:21 -0700367 multilib common.Multilib, props ...interface{}) (blueprint.Module, []interface{}) {
368
369 base.module = module
370
Colin Crossfa138792015-04-24 17:31:52 -0700371 props = append(props, &base.Properties, &base.unused)
Colin Crossc472d572015-03-17 15:06:21 -0700372
Colin Crosscfad1192015-11-02 16:43:11 -0800373 _, props = common.InitAndroidArchModule(module, hod, multilib, props...)
374
375 return common.InitDefaultableModule(module, base, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700376}
377
Colin Crossfa138792015-04-24 17:31:52 -0700378func (c *CCBase) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800379 toolchain := c.findToolchain(ctx)
380 if ctx.Failed() {
381 return
382 }
383
Colin Cross21b9a242015-03-24 14:15:58 -0700384 flags := c.collectFlags(ctx, toolchain)
Colin Cross3f40fa42015-01-30 17:27:36 -0800385 if ctx.Failed() {
386 return
387 }
388
Colin Cross74d1ec02015-04-28 13:30:13 -0700389 deps := c.depsToPaths(ctx, c.savedDepNames)
Colin Cross3f40fa42015-01-30 17:27:36 -0800390 if ctx.Failed() {
391 return
392 }
393
Colin Cross28344522015-04-22 13:07:53 -0700394 flags.CFlags = append(flags.CFlags, deps.Cflags...)
Colin Crossed9f8682015-03-18 17:17:35 -0700395
Colin Cross581c1892015-04-07 16:50:10 -0700396 objFiles := c.compileObjs(ctx, flags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800397 if ctx.Failed() {
398 return
399 }
400
Colin Cross581c1892015-04-07 16:50:10 -0700401 generatedObjFiles := c.compileGeneratedObjs(ctx, flags)
Colin Cross5049f022015-03-18 13:28:46 -0700402 if ctx.Failed() {
403 return
404 }
405
406 objFiles = append(objFiles, generatedObjFiles...)
407
Colin Cross3f40fa42015-01-30 17:27:36 -0800408 c.ccModuleType().compileModule(ctx, flags, deps, objFiles)
409 if ctx.Failed() {
410 return
411 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700412
413 c.ccModuleType().installModule(ctx, flags)
414 if ctx.Failed() {
415 return
416 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800417}
418
Colin Crossfa138792015-04-24 17:31:52 -0700419func (c *CCBase) ccModuleType() CCModuleType {
Colin Cross3f40fa42015-01-30 17:27:36 -0800420 return c.module
421}
422
Colin Crossfa138792015-04-24 17:31:52 -0700423func (c *CCBase) findToolchain(ctx common.AndroidModuleContext) Toolchain {
Colin Cross3f40fa42015-01-30 17:27:36 -0800424 arch := ctx.Arch()
Colin Crossd3ba0392015-05-07 14:11:29 -0700425 hod := ctx.HostOrDevice()
Dan Willemsen490fd492015-11-24 17:53:15 -0800426 ht := ctx.HostType()
427 factory := toolchainFactories[hod][ht][arch.ArchType]
Colin Cross3f40fa42015-01-30 17:27:36 -0800428 if factory == nil {
Dan Willemsen490fd492015-11-24 17:53:15 -0800429 ctx.ModuleErrorf("Toolchain not found for %s %s arch %q", hod.String(), ht.String(), arch.String())
Colin Crosseeabb892015-11-20 13:07:51 -0800430 return nil
Colin Cross3f40fa42015-01-30 17:27:36 -0800431 }
Colin Crossc5c24ad2015-11-20 15:35:00 -0800432 return factory(arch)
Colin Cross3f40fa42015-01-30 17:27:36 -0800433}
434
Colin Cross6362e272015-10-29 15:25:03 -0700435func (c *CCBase) ModifyProperties(ctx CCModuleContext) {
Colin Crossfa138792015-04-24 17:31:52 -0700436}
437
Colin Crosse11befc2015-04-27 17:49:17 -0700438func (c *CCBase) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Crossfa138792015-04-24 17:31:52 -0700439 depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.Properties.Whole_static_libs...)
440 depNames.StaticLibs = append(depNames.StaticLibs, c.Properties.Static_libs...)
441 depNames.SharedLibs = append(depNames.SharedLibs, c.Properties.Shared_libs...)
Colin Cross21b9a242015-03-24 14:15:58 -0700442
Colin Cross21b9a242015-03-24 14:15:58 -0700443 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -0800444}
445
Colin Cross6362e272015-10-29 15:25:03 -0700446func (c *CCBase) depsMutator(ctx common.AndroidBottomUpMutatorContext) {
Colin Cross74d1ec02015-04-28 13:30:13 -0700447 c.savedDepNames = c.module.depNames(ctx, CCDeps{})
448 c.savedDepNames.WholeStaticLibs = lastUniqueElements(c.savedDepNames.WholeStaticLibs)
449 c.savedDepNames.StaticLibs = lastUniqueElements(c.savedDepNames.StaticLibs)
450 c.savedDepNames.SharedLibs = lastUniqueElements(c.savedDepNames.SharedLibs)
451
452 staticLibs := c.savedDepNames.WholeStaticLibs
453 staticLibs = append(staticLibs, c.savedDepNames.StaticLibs...)
454 staticLibs = append(staticLibs, c.savedDepNames.LateStaticLibs...)
Colin Cross21b9a242015-03-24 14:15:58 -0700455 ctx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, staticLibs...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800456
Colin Cross74d1ec02015-04-28 13:30:13 -0700457 ctx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, c.savedDepNames.SharedLibs...)
Colin Cross21b9a242015-03-24 14:15:58 -0700458
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700459 ctx.AddDependency(ctx.Module(), c.savedDepNames.ObjFiles.Strings()...)
Colin Cross74d1ec02015-04-28 13:30:13 -0700460 if c.savedDepNames.CrtBegin != "" {
Colin Cross6362e272015-10-29 15:25:03 -0700461 ctx.AddDependency(ctx.Module(), c.savedDepNames.CrtBegin)
Colin Cross21b9a242015-03-24 14:15:58 -0700462 }
Colin Cross74d1ec02015-04-28 13:30:13 -0700463 if c.savedDepNames.CrtEnd != "" {
Colin Cross6362e272015-10-29 15:25:03 -0700464 ctx.AddDependency(ctx.Module(), c.savedDepNames.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700465 }
Colin Cross6362e272015-10-29 15:25:03 -0700466}
Colin Cross21b9a242015-03-24 14:15:58 -0700467
Colin Cross6362e272015-10-29 15:25:03 -0700468func depsMutator(ctx common.AndroidBottomUpMutatorContext) {
469 if c, ok := ctx.Module().(CCModuleType); ok {
470 c.ModifyProperties(ctx)
471 c.depsMutator(ctx)
472 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800473}
474
475// Create a ccFlags struct that collects the compile flags from global values,
476// per-target values, module type values, and per-module Blueprints properties
Colin Crossfa138792015-04-24 17:31:52 -0700477func (c *CCBase) collectFlags(ctx common.AndroidModuleContext, toolchain Toolchain) CCFlags {
Colin Cross97ba0732015-03-23 17:50:24 -0700478 flags := CCFlags{
Colin Crossfa138792015-04-24 17:31:52 -0700479 CFlags: c.Properties.Cflags,
480 CppFlags: c.Properties.Cppflags,
481 ConlyFlags: c.Properties.Conlyflags,
482 LdFlags: c.Properties.Ldflags,
483 AsFlags: c.Properties.Asflags,
484 YaccFlags: c.Properties.Yaccflags,
Colin Cross06a931b2015-10-28 17:23:31 -0700485 Nocrt: Bool(c.Properties.Nocrt),
Colin Cross97ba0732015-03-23 17:50:24 -0700486 Toolchain: toolchain,
Colin Cross06a931b2015-10-28 17:23:31 -0700487 Clang: Bool(c.Properties.Clang),
Colin Cross3f40fa42015-01-30 17:27:36 -0800488 }
Colin Cross28344522015-04-22 13:07:53 -0700489
490 // Include dir cflags
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700491 rootIncludeDirs := common.PathsForSource(ctx, c.Properties.Include_dirs)
492 localIncludeDirs := common.PathsForModuleSrc(ctx, c.Properties.Local_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -0700493 flags.GlobalFlags = append(flags.GlobalFlags,
Dan Willemsen1e898b92015-09-23 15:26:32 -0700494 includeDirsToFlags(localIncludeDirs),
495 includeDirsToFlags(rootIncludeDirs))
Colin Cross28344522015-04-22 13:07:53 -0700496
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700497 rootIncludeFiles := common.PathsForSource(ctx, c.Properties.Include_files)
498 localIncludeFiles := common.PathsForModuleSrc(ctx, c.Properties.Local_include_files)
Colin Cross39d97f22015-09-14 12:30:50 -0700499
500 flags.GlobalFlags = append(flags.GlobalFlags,
501 includeFilesToFlags(rootIncludeFiles),
502 includeFilesToFlags(localIncludeFiles))
503
Colin Cross06a931b2015-10-28 17:23:31 -0700504 if !Bool(c.Properties.No_default_compiler_flags) {
Colin Crossfa138792015-04-24 17:31:52 -0700505 if c.Properties.Sdk_version == "" || ctx.Host() {
Colin Cross28344522015-04-22 13:07:53 -0700506 flags.GlobalFlags = append(flags.GlobalFlags,
507 "${commonGlobalIncludes}",
508 toolchain.IncludeFlags(),
Dan Willemsene0378dd2016-01-07 17:42:34 -0800509 "${commonNativehelperInclude}")
Colin Cross28344522015-04-22 13:07:53 -0700510 }
511
512 flags.GlobalFlags = append(flags.GlobalFlags, []string{
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700513 "-I" + common.PathForModuleSrc(ctx).String(),
514 "-I" + common.PathForModuleOut(ctx).String(),
515 "-I" + common.PathForModuleGen(ctx).String(),
Colin Cross28344522015-04-22 13:07:53 -0700516 }...)
517 }
518
Colin Cross06a931b2015-10-28 17:23:31 -0700519 if c.Properties.Clang == nil {
Dan Willemsendd0e2c32015-10-20 14:29:35 -0700520 if ctx.Host() {
521 flags.Clang = true
522 }
523
524 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
525 flags.Clang = true
526 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800527 }
528
Dan Willemsen490fd492015-11-24 17:53:15 -0800529 if !toolchain.ClangSupported() {
530 flags.Clang = false
531 }
532
Dan Willemsen6d11dd82015-11-03 14:27:00 -0800533 instructionSet := c.Properties.Instruction_set
534 instructionSetFlags, err := toolchain.InstructionSetFlags(instructionSet)
535 if flags.Clang {
536 instructionSetFlags, err = toolchain.ClangInstructionSetFlags(instructionSet)
537 }
538 if err != nil {
539 ctx.ModuleErrorf("%s", err)
540 }
541
542 // TODO: debug
543 flags.CFlags = append(flags.CFlags, c.Properties.Release.Cflags...)
544
Colin Cross97ba0732015-03-23 17:50:24 -0700545 if flags.Clang {
546 flags.CFlags = clangFilterUnknownCflags(flags.CFlags)
Colin Crossfa138792015-04-24 17:31:52 -0700547 flags.CFlags = append(flags.CFlags, c.Properties.Clang_cflags...)
548 flags.AsFlags = append(flags.AsFlags, c.Properties.Clang_asflags...)
Colin Cross97ba0732015-03-23 17:50:24 -0700549 flags.CppFlags = clangFilterUnknownCflags(flags.CppFlags)
550 flags.ConlyFlags = clangFilterUnknownCflags(flags.ConlyFlags)
551 flags.LdFlags = clangFilterUnknownCflags(flags.LdFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800552
Colin Cross97ba0732015-03-23 17:50:24 -0700553 flags.CFlags = append(flags.CFlags, "${clangExtraCflags}")
554 flags.ConlyFlags = append(flags.ConlyFlags, "${clangExtraConlyflags}")
Colin Crossf6566ed2015-03-24 11:13:38 -0700555 if ctx.Device() {
Colin Cross97ba0732015-03-23 17:50:24 -0700556 flags.CFlags = append(flags.CFlags, "${clangExtraTargetCflags}")
Colin Crossbdd7b1c2015-03-16 16:21:20 -0700557 }
558
Colin Cross3f40fa42015-01-30 17:27:36 -0800559 target := "-target " + toolchain.ClangTriple()
560 gccPrefix := "-B" + filepath.Join(toolchain.GccRoot(), toolchain.GccTriple(), "bin")
561
Colin Cross97ba0732015-03-23 17:50:24 -0700562 flags.CFlags = append(flags.CFlags, target, gccPrefix)
563 flags.AsFlags = append(flags.AsFlags, target, gccPrefix)
564 flags.LdFlags = append(flags.LdFlags, target, gccPrefix)
Colin Cross3f40fa42015-01-30 17:27:36 -0800565 }
566
Colin Cross06a931b2015-10-28 17:23:31 -0700567 if !Bool(c.Properties.No_default_compiler_flags) {
568 if ctx.Device() && !Bool(c.Properties.Allow_undefined_symbols) {
Colin Cross97ba0732015-03-23 17:50:24 -0700569 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-undefined")
Colin Cross3f40fa42015-01-30 17:27:36 -0800570 }
571
Colin Cross56b4d452015-04-21 17:38:44 -0700572 flags.GlobalFlags = append(flags.GlobalFlags, instructionSetFlags)
573
Colin Cross97ba0732015-03-23 17:50:24 -0700574 if flags.Clang {
575 flags.CppFlags = append(flags.CppFlags, "${commonClangGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -0700576 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -0800577 toolchain.ClangCflags(),
578 "${commonClangGlobalCflags}",
Colin Crossd3ba0392015-05-07 14:11:29 -0700579 fmt.Sprintf("${%sClangGlobalCflags}", ctx.HostOrDevice()))
Colin Cross3f40fa42015-01-30 17:27:36 -0800580 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700581 flags.CppFlags = append(flags.CppFlags, "${commonGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -0700582 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -0800583 toolchain.Cflags(),
584 "${commonGlobalCflags}",
Colin Crossd3ba0392015-05-07 14:11:29 -0700585 fmt.Sprintf("${%sGlobalCflags}", ctx.HostOrDevice()))
Colin Cross3f40fa42015-01-30 17:27:36 -0800586 }
587
Colin Cross7b66f152015-12-15 16:07:43 -0800588 if Bool(ctx.AConfig().ProductVariables.Brillo) {
589 flags.GlobalFlags = append(flags.GlobalFlags, "-D__BRILLO__")
590 }
591
Colin Crossf6566ed2015-03-24 11:13:38 -0700592 if ctx.Device() {
Colin Cross06a931b2015-10-28 17:23:31 -0700593 if Bool(c.Properties.Rtti) {
Colin Cross97ba0732015-03-23 17:50:24 -0700594 flags.CppFlags = append(flags.CppFlags, "-frtti")
Colin Cross3f40fa42015-01-30 17:27:36 -0800595 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700596 flags.CppFlags = append(flags.CppFlags, "-fno-rtti")
Colin Cross3f40fa42015-01-30 17:27:36 -0800597 }
598 }
599
Colin Cross97ba0732015-03-23 17:50:24 -0700600 flags.AsFlags = append(flags.AsFlags, "-D__ASSEMBLY__")
Colin Cross3f40fa42015-01-30 17:27:36 -0800601
Colin Cross97ba0732015-03-23 17:50:24 -0700602 if flags.Clang {
603 flags.CppFlags = append(flags.CppFlags, toolchain.ClangCppflags())
604 flags.LdFlags = append(flags.LdFlags, toolchain.ClangLdflags())
Colin Cross3f40fa42015-01-30 17:27:36 -0800605 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700606 flags.CppFlags = append(flags.CppFlags, toolchain.Cppflags())
607 flags.LdFlags = append(flags.LdFlags, toolchain.Ldflags())
Colin Cross3f40fa42015-01-30 17:27:36 -0800608 }
Colin Cross28344522015-04-22 13:07:53 -0700609
610 if ctx.Host() {
Colin Crossfa138792015-04-24 17:31:52 -0700611 flags.LdFlags = append(flags.LdFlags, c.Properties.Host_ldlibs...)
Colin Cross28344522015-04-22 13:07:53 -0700612 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800613 }
614
Colin Crossc4bde762015-11-23 16:11:30 -0800615 if flags.Clang {
616 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainClangCflags())
617 } else {
618 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainCflags())
619 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainLdflags())
620 }
621
Colin Cross0676e2d2015-04-24 17:39:18 -0700622 flags = c.ccModuleType().flags(ctx, flags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800623
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700624 if c.Properties.Sdk_version == "" {
625 if ctx.Host() && !flags.Clang {
626 // The host GCC doesn't support C++14 (and is deprecated, so likely
627 // never will). Build these modules with C++11.
628 flags.CppFlags = append(flags.CppFlags, "-std=gnu++11")
629 } else {
630 flags.CppFlags = append(flags.CppFlags, "-std=gnu++14")
631 }
632 }
633
634 flags.CFlags, _ = filterList(flags.CFlags, illegalFlags)
635 flags.CppFlags, _ = filterList(flags.CppFlags, illegalFlags)
636 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, illegalFlags)
637
Colin Cross3f40fa42015-01-30 17:27:36 -0800638 // Optimization to reduce size of build.ninja
639 // Replace the long list of flags for each file with a module-local variable
Colin Cross97ba0732015-03-23 17:50:24 -0700640 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
641 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
642 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
643 flags.CFlags = []string{"$cflags"}
644 flags.CppFlags = []string{"$cppflags"}
645 flags.AsFlags = []string{"$asflags"}
Colin Cross3f40fa42015-01-30 17:27:36 -0800646
647 return flags
648}
649
Colin Cross0676e2d2015-04-24 17:39:18 -0700650func (c *CCBase) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Cross3f40fa42015-01-30 17:27:36 -0800651 return flags
652}
653
654// Compile a list of source files into objects a specified subdirectory
Colin Crossfa138792015-04-24 17:31:52 -0700655func (c *CCBase) customCompileObjs(ctx common.AndroidModuleContext, flags CCFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700656 subdir string, srcFiles, excludes []string) common.Paths {
Colin Cross581c1892015-04-07 16:50:10 -0700657
658 buildFlags := ccFlagsToBuilderFlags(flags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800659
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700660 inputFiles := ctx.ExpandSources(srcFiles, excludes)
661 srcPaths, deps := genSources(ctx, inputFiles, buildFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800662
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700663 return TransformSourceToObj(ctx, subdir, srcPaths, buildFlags, deps)
Colin Cross3f40fa42015-01-30 17:27:36 -0800664}
665
Colin Crossfa138792015-04-24 17:31:52 -0700666// Compile files listed in c.Properties.Srcs into objects
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700667func (c *CCBase) compileObjs(ctx common.AndroidModuleContext, flags CCFlags) common.Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800668
Colin Crossfa138792015-04-24 17:31:52 -0700669 if c.Properties.SkipCompileObjs {
Colin Cross3f40fa42015-01-30 17:27:36 -0800670 return nil
671 }
672
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700673 return c.customCompileObjs(ctx, flags, "", c.Properties.Srcs, c.Properties.Exclude_srcs)
Colin Cross3f40fa42015-01-30 17:27:36 -0800674}
675
Colin Cross5049f022015-03-18 13:28:46 -0700676// Compile generated source files from dependencies
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700677func (c *CCBase) compileGeneratedObjs(ctx common.AndroidModuleContext, flags CCFlags) common.Paths {
678 var srcs common.Paths
Colin Cross5049f022015-03-18 13:28:46 -0700679
Colin Crossfa138792015-04-24 17:31:52 -0700680 if c.Properties.SkipCompileObjs {
Colin Cross5049f022015-03-18 13:28:46 -0700681 return nil
682 }
683
684 ctx.VisitDirectDeps(func(module blueprint.Module) {
685 if gen, ok := module.(genrule.SourceFileGenerator); ok {
686 srcs = append(srcs, gen.GeneratedSourceFiles()...)
687 }
688 })
689
690 if len(srcs) == 0 {
691 return nil
692 }
693
Colin Cross581c1892015-04-07 16:50:10 -0700694 return TransformSourceToObj(ctx, "", srcs, ccFlagsToBuilderFlags(flags), nil)
Colin Cross5049f022015-03-18 13:28:46 -0700695}
696
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700697func (c *CCBase) outputFile() common.OptionalPath {
698 return common.OptionalPath{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800699}
700
Colin Crossfa138792015-04-24 17:31:52 -0700701func (c *CCBase) depsToPathsFromList(ctx common.AndroidModuleContext,
Colin Cross3f40fa42015-01-30 17:27:36 -0800702 names []string) (modules []common.AndroidModule,
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700703 outputFiles common.Paths, exportedFlags []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800704
705 for _, n := range names {
706 found := false
707 ctx.VisitDirectDeps(func(m blueprint.Module) {
708 otherName := ctx.OtherModuleName(m)
709 if otherName != n {
710 return
711 }
712
Colin Cross97ba0732015-03-23 17:50:24 -0700713 if a, ok := m.(CCModuleType); ok {
Dan Willemsen0effe062015-11-30 16:06:01 -0800714 if !a.Enabled() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800715 // If a cc_library host+device module depends on a library that exists as both
716 // cc_library_shared and cc_library_host_shared, it will end up with two
717 // dependencies with the same name, one of which is marked disabled for each
718 // of host and device. Ignore the disabled one.
719 return
720 }
Colin Crossd3ba0392015-05-07 14:11:29 -0700721 if a.HostOrDevice() != ctx.HostOrDevice() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800722 ctx.ModuleErrorf("host/device mismatch between %q and %q", ctx.ModuleName(),
723 otherName)
724 return
725 }
726
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700727 if outputFile := a.outputFile(); outputFile.Valid() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800728 if found {
729 ctx.ModuleErrorf("multiple modules satisified dependency on %q", otherName)
730 return
731 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700732 outputFiles = append(outputFiles, outputFile.Path())
Colin Cross3f40fa42015-01-30 17:27:36 -0800733 modules = append(modules, a)
Colin Cross28344522015-04-22 13:07:53 -0700734 if i, ok := a.(ccExportedFlagsProducer); ok {
735 exportedFlags = append(exportedFlags, i.exportedFlags()...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800736 }
737 found = true
738 } else {
739 ctx.ModuleErrorf("module %q missing output file", otherName)
740 return
741 }
742 } else {
743 ctx.ModuleErrorf("module %q not an android module", otherName)
744 return
745 }
746 })
Colin Cross6ff51382015-12-17 16:39:19 -0800747 if !found && !inList(n, ctx.GetMissingDependencies()) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800748 ctx.ModuleErrorf("unsatisified dependency on %q", n)
749 }
750 }
751
Colin Cross28344522015-04-22 13:07:53 -0700752 return modules, outputFiles, exportedFlags
Colin Cross3f40fa42015-01-30 17:27:36 -0800753}
754
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700755// Convert dependency names to paths. Takes a CCDeps containing names and returns a CCPathDeps
Colin Cross21b9a242015-03-24 14:15:58 -0700756// containing paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700757func (c *CCBase) depsToPaths(ctx common.AndroidModuleContext, depNames CCDeps) CCPathDeps {
758 var depPaths CCPathDeps
Colin Cross28344522015-04-22 13:07:53 -0700759 var newCflags []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800760
Colin Cross21b9a242015-03-24 14:15:58 -0700761 var wholeStaticLibModules []common.AndroidModule
Colin Cross3f40fa42015-01-30 17:27:36 -0800762
Colin Cross28344522015-04-22 13:07:53 -0700763 wholeStaticLibModules, depPaths.WholeStaticLibs, newCflags =
Colin Cross21b9a242015-03-24 14:15:58 -0700764 c.depsToPathsFromList(ctx, depNames.WholeStaticLibs)
Colin Cross28344522015-04-22 13:07:53 -0700765 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Crossa48f71f2015-11-16 18:00:41 -0800766 depPaths.ReexportedCflags = append(depPaths.ReexportedCflags, newCflags...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800767
Colin Cross21b9a242015-03-24 14:15:58 -0700768 for _, m := range wholeStaticLibModules {
769 if staticLib, ok := m.(ccLibraryInterface); ok && staticLib.static() {
770 depPaths.WholeStaticLibObjFiles =
771 append(depPaths.WholeStaticLibObjFiles, staticLib.allObjFiles()...)
772 } else {
773 ctx.ModuleErrorf("module %q not a static library", ctx.OtherModuleName(m))
774 }
775 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800776
Colin Cross28344522015-04-22 13:07:53 -0700777 _, depPaths.StaticLibs, newCflags = c.depsToPathsFromList(ctx, depNames.StaticLibs)
778 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross21b9a242015-03-24 14:15:58 -0700779
Colin Cross28344522015-04-22 13:07:53 -0700780 _, depPaths.LateStaticLibs, newCflags = c.depsToPathsFromList(ctx, depNames.LateStaticLibs)
781 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross21b9a242015-03-24 14:15:58 -0700782
Colin Cross28344522015-04-22 13:07:53 -0700783 _, depPaths.SharedLibs, newCflags = c.depsToPathsFromList(ctx, depNames.SharedLibs)
784 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross21b9a242015-03-24 14:15:58 -0700785
786 ctx.VisitDirectDeps(func(m blueprint.Module) {
Dan Albertc3144b12015-04-28 18:17:56 -0700787 if obj, ok := m.(ccObjectProvider); ok {
Colin Cross21b9a242015-03-24 14:15:58 -0700788 otherName := ctx.OtherModuleName(m)
789 if otherName == depNames.CrtBegin {
Colin Cross06a931b2015-10-28 17:23:31 -0700790 if !Bool(c.Properties.Nocrt) {
Dan Albertc3144b12015-04-28 18:17:56 -0700791 depPaths.CrtBegin = obj.object().outputFile()
Colin Cross21b9a242015-03-24 14:15:58 -0700792 }
793 } else if otherName == depNames.CrtEnd {
Colin Cross06a931b2015-10-28 17:23:31 -0700794 if !Bool(c.Properties.Nocrt) {
Dan Albertc3144b12015-04-28 18:17:56 -0700795 depPaths.CrtEnd = obj.object().outputFile()
Colin Cross21b9a242015-03-24 14:15:58 -0700796 }
797 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700798 output := obj.object().outputFile()
799 if output.Valid() {
800 depPaths.ObjFiles = append(depPaths.ObjFiles, output.Path())
801 } else {
802 ctx.ModuleErrorf("module %s did not provide an output file", otherName)
803 }
Colin Cross21b9a242015-03-24 14:15:58 -0700804 }
805 }
806 })
807
808 return depPaths
Colin Cross3f40fa42015-01-30 17:27:36 -0800809}
810
Colin Cross7d5136f2015-05-11 13:39:40 -0700811type ccLinkedProperties struct {
812 VariantIsShared bool `blueprint:"mutated"`
813 VariantIsStatic bool `blueprint:"mutated"`
814 VariantIsStaticBinary bool `blueprint:"mutated"`
815}
816
Colin Crossfa138792015-04-24 17:31:52 -0700817// CCLinked contains the properties and members used by libraries and executables
818type CCLinked struct {
819 CCBase
Colin Cross7d5136f2015-05-11 13:39:40 -0700820 dynamicProperties ccLinkedProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800821}
822
Colin Crossfa138792015-04-24 17:31:52 -0700823func newCCDynamic(dynamic *CCLinked, module CCModuleType, hod common.HostOrDeviceSupported,
Colin Crossc472d572015-03-17 15:06:21 -0700824 multilib common.Multilib, props ...interface{}) (blueprint.Module, []interface{}) {
825
Colin Crossed4cf0b2015-03-26 14:43:45 -0700826 props = append(props, &dynamic.dynamicProperties)
827
Colin Crossfa138792015-04-24 17:31:52 -0700828 return newCCBase(&dynamic.CCBase, module, hod, multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700829}
830
Colin Crossfa138792015-04-24 17:31:52 -0700831func (c *CCLinked) systemSharedLibs(ctx common.AndroidBaseContext) []string {
Colin Cross06a931b2015-10-28 17:23:31 -0700832 if c.Properties.System_shared_libs != nil {
Colin Crossfa138792015-04-24 17:31:52 -0700833 return c.Properties.System_shared_libs
834 } else if ctx.Device() && c.Properties.Sdk_version == "" {
Colin Cross577f6e42015-03-27 18:23:34 -0700835 return []string{"libc", "libm"}
Colin Cross28d76592015-03-26 16:14:04 -0700836 } else {
Colin Cross577f6e42015-03-27 18:23:34 -0700837 return nil
Colin Cross3f40fa42015-01-30 17:27:36 -0800838 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800839}
840
Colin Crossfa138792015-04-24 17:31:52 -0700841func (c *CCLinked) stl(ctx common.AndroidBaseContext) string {
842 if c.Properties.Sdk_version != "" && ctx.Device() {
843 switch c.Properties.Stl {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700844 case "":
845 return "ndk_system"
846 case "c++_shared", "c++_static",
847 "stlport_shared", "stlport_static",
848 "gnustl_static":
Colin Crossfa138792015-04-24 17:31:52 -0700849 return "ndk_lib" + c.Properties.Stl
Colin Crossed4cf0b2015-03-26 14:43:45 -0700850 default:
Colin Crossfa138792015-04-24 17:31:52 -0700851 ctx.ModuleErrorf("stl: %q is not a supported STL with sdk_version set", c.Properties.Stl)
Colin Crossed4cf0b2015-03-26 14:43:45 -0700852 return ""
853 }
854 }
855
Dan Willemsen490fd492015-11-24 17:53:15 -0800856 if ctx.HostType() == common.Windows {
857 switch c.Properties.Stl {
858 case "libc++", "libc++_static", "libstdc++", "":
859 // libc++ is not supported on mingw
860 return "libstdc++"
861 case "none":
862 return ""
863 default:
864 ctx.ModuleErrorf("stl: %q is not a supported STL", c.Properties.Stl)
865 return ""
Colin Crossed4cf0b2015-03-26 14:43:45 -0700866 }
Dan Willemsen490fd492015-11-24 17:53:15 -0800867 } else {
868 switch c.Properties.Stl {
869 case "libc++", "libc++_static",
870 "libstdc++":
871 return c.Properties.Stl
872 case "none":
873 return ""
874 case "":
875 if c.static() {
876 return "libc++_static"
877 } else {
878 return "libc++"
879 }
880 default:
881 ctx.ModuleErrorf("stl: %q is not a supported STL", c.Properties.Stl)
882 return ""
883 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700884 }
885}
886
Dan Willemsen490fd492015-11-24 17:53:15 -0800887var hostDynamicGccLibs, hostStaticGccLibs map[common.HostType][]string
Colin Cross0af4b842015-04-30 16:36:18 -0700888
889func init() {
Dan Willemsen490fd492015-11-24 17:53:15 -0800890 hostDynamicGccLibs = map[common.HostType][]string{
891 common.Linux: []string{"-lgcc_s", "-lgcc", "-lc", "-lgcc_s", "-lgcc"},
892 common.Darwin: []string{"-lc", "-lSystem"},
893 common.Windows: []string{"-lmsvcr110", "-lmingw32", "-lgcc", "-lmoldname",
894 "-lmingwex", "-lmsvcrt", "-ladvapi32", "-lshell32", "-luser32",
895 "-lkernel32", "-lmingw32", "-lgcc", "-lmoldname", "-lmingwex",
896 "-lmsvcrt"},
897 }
898 hostStaticGccLibs = map[common.HostType][]string{
899 common.Linux: []string{"-Wl,--start-group", "-lgcc", "-lgcc_eh", "-lc", "-Wl,--end-group"},
900 common.Darwin: []string{"NO_STATIC_HOST_BINARIES_ON_DARWIN"},
901 common.Windows: []string{"NO_STATIC_HOST_BINARIES_ON_WINDOWS"},
Colin Cross0af4b842015-04-30 16:36:18 -0700902 }
903}
Colin Cross712fc022015-04-27 11:13:34 -0700904
Colin Crosse11befc2015-04-27 17:49:17 -0700905func (c *CCLinked) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700906 stl := c.stl(ctx)
907 if ctx.Failed() {
908 return flags
909 }
910
911 switch stl {
912 case "libc++", "libc++_static":
913 flags.CFlags = append(flags.CFlags, "-D_USING_LIBCXX")
Colin Crossed4cf0b2015-03-26 14:43:45 -0700914 if ctx.Host() {
915 flags.CppFlags = append(flags.CppFlags, "-nostdinc++")
916 flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs")
Colin Cross712fc022015-04-27 11:13:34 -0700917 flags.LdFlags = append(flags.LdFlags, "-lm", "-lpthread")
Colin Cross18b6dc52015-04-28 13:20:37 -0700918 if c.staticBinary() {
Dan Willemsen490fd492015-11-24 17:53:15 -0800919 flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs[ctx.HostType()]...)
Colin Cross18b6dc52015-04-28 13:20:37 -0700920 } else {
Dan Willemsen490fd492015-11-24 17:53:15 -0800921 flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs[ctx.HostType()]...)
Colin Cross712fc022015-04-27 11:13:34 -0700922 }
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700923 } else {
924 if ctx.Arch().ArchType == common.Arm {
925 flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,libunwind_llvm.a")
926 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700927 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700928 case "libstdc++":
929 // Using bionic's basic libstdc++. Not actually an STL. Only around until the
930 // tree is in good enough shape to not need it.
931 // Host builds will use GNU libstdc++.
932 if ctx.Device() {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700933 flags.CFlags = append(flags.CFlags, "-I"+common.PathForSource(ctx, "bionic/libstdc++/include").String())
Colin Crossed4cf0b2015-03-26 14:43:45 -0700934 }
935 case "ndk_system":
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700936 ndkSrcRoot := common.PathForSource(ctx, "prebuilts/ndk/current/sources/cxx-stl/system/include")
937 flags.CFlags = append(flags.CFlags, "-isystem "+ndkSrcRoot.String())
Colin Crossed4cf0b2015-03-26 14:43:45 -0700938 case "ndk_libc++_shared", "ndk_libc++_static":
939 // TODO(danalbert): This really shouldn't be here...
940 flags.CppFlags = append(flags.CppFlags, "-std=c++11")
941 case "ndk_libstlport_shared", "ndk_libstlport_static", "ndk_libgnustl_static":
942 // Nothing
943 case "":
944 // None or error.
945 if ctx.Host() {
946 flags.CppFlags = append(flags.CppFlags, "-nostdinc++")
947 flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs")
Colin Cross18b6dc52015-04-28 13:20:37 -0700948 if c.staticBinary() {
Dan Willemsen490fd492015-11-24 17:53:15 -0800949 flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs[ctx.HostType()]...)
Colin Cross18b6dc52015-04-28 13:20:37 -0700950 } else {
Dan Willemsen490fd492015-11-24 17:53:15 -0800951 flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs[ctx.HostType()]...)
Colin Cross712fc022015-04-27 11:13:34 -0700952 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700953 }
954 default:
Colin Crossfa138792015-04-24 17:31:52 -0700955 panic(fmt.Errorf("Unknown stl in CCLinked.Flags: %q", stl))
Colin Crossed4cf0b2015-03-26 14:43:45 -0700956 }
957
958 return flags
959}
960
Colin Crosse11befc2015-04-27 17:49:17 -0700961func (c *CCLinked) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
962 depNames = c.CCBase.depNames(ctx, depNames)
Colin Cross3f40fa42015-01-30 17:27:36 -0800963
Colin Crossed4cf0b2015-03-26 14:43:45 -0700964 stl := c.stl(ctx)
965 if ctx.Failed() {
966 return depNames
967 }
968
969 switch stl {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700970 case "libstdc++":
971 if ctx.Device() {
972 depNames.SharedLibs = append(depNames.SharedLibs, stl)
973 }
Colin Cross74d1ec02015-04-28 13:30:13 -0700974 case "libc++", "libc++_static":
975 if stl == "libc++" {
976 depNames.SharedLibs = append(depNames.SharedLibs, stl)
977 } else {
978 depNames.StaticLibs = append(depNames.StaticLibs, stl)
979 }
980 if ctx.Device() {
981 if ctx.Arch().ArchType == common.Arm {
982 depNames.StaticLibs = append(depNames.StaticLibs, "libunwind_llvm")
983 }
984 if c.staticBinary() {
985 depNames.StaticLibs = append(depNames.StaticLibs, "libdl")
986 } else {
987 depNames.SharedLibs = append(depNames.SharedLibs, "libdl")
988 }
989 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700990 case "":
991 // None or error.
992 case "ndk_system":
993 // TODO: Make a system STL prebuilt for the NDK.
994 // 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 -0700995 // its own includes. The includes are handled in CCBase.Flags().
Colin Cross577f6e42015-03-27 18:23:34 -0700996 depNames.SharedLibs = append([]string{"libstdc++"}, depNames.SharedLibs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -0700997 case "ndk_libc++_shared", "ndk_libstlport_shared":
998 depNames.SharedLibs = append(depNames.SharedLibs, stl)
999 case "ndk_libc++_static", "ndk_libstlport_static", "ndk_libgnustl_static":
1000 depNames.StaticLibs = append(depNames.StaticLibs, stl)
1001 default:
Colin Crosse11befc2015-04-27 17:49:17 -07001002 panic(fmt.Errorf("Unknown stl in CCLinked.depNames: %q", stl))
Colin Crossed4cf0b2015-03-26 14:43:45 -07001003 }
1004
Colin Cross74d1ec02015-04-28 13:30:13 -07001005 if ctx.ModuleName() != "libcompiler_rt-extras" {
1006 depNames.StaticLibs = append(depNames.StaticLibs, "libcompiler_rt-extras")
1007 }
1008
Colin Crossf6566ed2015-03-24 11:13:38 -07001009 if ctx.Device() {
Colin Cross77b00fa2015-03-16 16:15:49 -07001010 // libgcc and libatomic have to be last on the command line
Dan Willemsend67be222015-09-16 15:19:33 -07001011 depNames.LateStaticLibs = append(depNames.LateStaticLibs, "libgcov", "libatomic")
Colin Cross06a931b2015-10-28 17:23:31 -07001012 if !Bool(c.Properties.No_libgcc) {
Dan Willemsend67be222015-09-16 15:19:33 -07001013 depNames.LateStaticLibs = append(depNames.LateStaticLibs, "libgcc")
1014 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001015
Colin Cross18b6dc52015-04-28 13:20:37 -07001016 if !c.static() {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001017 depNames.SharedLibs = append(depNames.SharedLibs, c.systemSharedLibs(ctx)...)
1018 }
Colin Cross577f6e42015-03-27 18:23:34 -07001019
Colin Crossfa138792015-04-24 17:31:52 -07001020 if c.Properties.Sdk_version != "" {
1021 version := c.Properties.Sdk_version
Colin Cross577f6e42015-03-27 18:23:34 -07001022 depNames.SharedLibs = append(depNames.SharedLibs,
1023 "ndk_libc."+version,
1024 "ndk_libm."+version,
1025 )
1026 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001027 }
1028
Colin Cross21b9a242015-03-24 14:15:58 -07001029 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -08001030}
1031
Colin Crossed4cf0b2015-03-26 14:43:45 -07001032// ccLinkedInterface interface is used on ccLinked to deal with static or shared variants
1033type ccLinkedInterface interface {
1034 // Returns true if the build options for the module have selected a static or shared build
1035 buildStatic() bool
1036 buildShared() bool
1037
1038 // Sets whether a specific variant is static or shared
Colin Cross18b6dc52015-04-28 13:20:37 -07001039 setStatic(bool)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001040
Colin Cross18b6dc52015-04-28 13:20:37 -07001041 // Returns whether a specific variant is a static library or binary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001042 static() bool
Colin Cross18b6dc52015-04-28 13:20:37 -07001043
1044 // Returns whether a module is a static binary
1045 staticBinary() bool
Colin Crossed4cf0b2015-03-26 14:43:45 -07001046}
1047
1048var _ ccLinkedInterface = (*CCLibrary)(nil)
1049var _ ccLinkedInterface = (*CCBinary)(nil)
1050
Colin Crossfa138792015-04-24 17:31:52 -07001051func (c *CCLinked) static() bool {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001052 return c.dynamicProperties.VariantIsStatic
1053}
1054
Colin Cross18b6dc52015-04-28 13:20:37 -07001055func (c *CCLinked) staticBinary() bool {
1056 return c.dynamicProperties.VariantIsStaticBinary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001057}
1058
Colin Cross18b6dc52015-04-28 13:20:37 -07001059func (c *CCLinked) setStatic(static bool) {
1060 c.dynamicProperties.VariantIsStatic = static
Colin Crossed4cf0b2015-03-26 14:43:45 -07001061}
1062
Colin Cross28344522015-04-22 13:07:53 -07001063type ccExportedFlagsProducer interface {
1064 exportedFlags() []string
Colin Cross3f40fa42015-01-30 17:27:36 -08001065}
1066
1067//
1068// Combined static+shared libraries
1069//
1070
Colin Cross7d5136f2015-05-11 13:39:40 -07001071type CCLibraryProperties struct {
1072 BuildStatic bool `blueprint:"mutated"`
1073 BuildShared bool `blueprint:"mutated"`
1074 Static 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"`
1082 Shared struct {
1083 Srcs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001084 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -07001085 Cflags []string `android:"arch_variant"`
1086 Whole_static_libs []string `android:"arch_variant"`
1087 Static_libs []string `android:"arch_variant"`
1088 Shared_libs []string `android:"arch_variant"`
1089 } `android:"arch_variant"`
Colin Crossaee540a2015-07-06 17:48:31 -07001090
1091 // local file name to pass to the linker as --version_script
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001092 Version_script *string `android:"arch_variant"`
Dan Willemsen93c28312015-12-04 14:59:08 -08001093 // local file name to pass to the linker as -unexported_symbols_list
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001094 Unexported_symbols_list *string `android:"arch_variant"`
Dan Willemsen93c28312015-12-04 14:59:08 -08001095 // local file name to pass to the linker as -force_symbols_not_weak_list
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001096 Force_symbols_not_weak_list *string `android:"arch_variant"`
Dan Willemsen93c28312015-12-04 14:59:08 -08001097 // local file name to pass to the linker as -force_symbols_weak_list
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001098 Force_symbols_weak_list *string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -07001099}
1100
Colin Cross97ba0732015-03-23 17:50:24 -07001101type CCLibrary struct {
Colin Crossfa138792015-04-24 17:31:52 -07001102 CCLinked
Colin Cross3f40fa42015-01-30 17:27:36 -08001103
Colin Cross28344522015-04-22 13:07:53 -07001104 reuseFrom ccLibraryInterface
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001105 reuseObjFiles common.Paths
1106 objFiles common.Paths
Colin Cross28344522015-04-22 13:07:53 -07001107 exportFlags []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001108 out common.Path
Dan Willemsen218f6562015-07-08 18:13:11 -07001109 systemLibs []string
Colin Cross3f40fa42015-01-30 17:27:36 -08001110
Colin Cross7d5136f2015-05-11 13:39:40 -07001111 LibraryProperties CCLibraryProperties
Colin Cross3f40fa42015-01-30 17:27:36 -08001112}
1113
Colin Crossed4cf0b2015-03-26 14:43:45 -07001114func (c *CCLibrary) buildStatic() bool {
1115 return c.LibraryProperties.BuildStatic
1116}
1117
1118func (c *CCLibrary) buildShared() bool {
1119 return c.LibraryProperties.BuildShared
1120}
1121
Colin Cross97ba0732015-03-23 17:50:24 -07001122type ccLibraryInterface interface {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001123 ccLinkedInterface
Colin Cross97ba0732015-03-23 17:50:24 -07001124 ccLibrary() *CCLibrary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001125 setReuseFrom(ccLibraryInterface)
1126 getReuseFrom() ccLibraryInterface
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001127 getReuseObjFiles() common.Paths
1128 allObjFiles() common.Paths
Colin Crossc472d572015-03-17 15:06:21 -07001129}
1130
Colin Crossed4cf0b2015-03-26 14:43:45 -07001131var _ ccLibraryInterface = (*CCLibrary)(nil)
1132
Colin Cross97ba0732015-03-23 17:50:24 -07001133func (c *CCLibrary) ccLibrary() *CCLibrary {
1134 return c
Colin Cross3f40fa42015-01-30 17:27:36 -08001135}
1136
Colin Cross97ba0732015-03-23 17:50:24 -07001137func NewCCLibrary(library *CCLibrary, module CCModuleType,
1138 hod common.HostOrDeviceSupported) (blueprint.Module, []interface{}) {
1139
Colin Crossfa138792015-04-24 17:31:52 -07001140 return newCCDynamic(&library.CCLinked, module, hod, common.MultilibBoth,
Colin Cross97ba0732015-03-23 17:50:24 -07001141 &library.LibraryProperties)
1142}
1143
1144func CCLibraryFactory() (blueprint.Module, []interface{}) {
1145 module := &CCLibrary{}
1146
1147 module.LibraryProperties.BuildShared = true
1148 module.LibraryProperties.BuildStatic = true
1149
1150 return NewCCLibrary(module, module, common.HostAndDeviceSupported)
1151}
1152
Colin Cross0676e2d2015-04-24 17:39:18 -07001153func (c *CCLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Crosse11befc2015-04-27 17:49:17 -07001154 depNames = c.CCLinked.depNames(ctx, depNames)
Colin Cross2732e9a2015-04-28 13:23:52 -07001155 if c.static() {
1156 depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.LibraryProperties.Static.Whole_static_libs...)
1157 depNames.StaticLibs = append(depNames.StaticLibs, c.LibraryProperties.Static.Static_libs...)
1158 depNames.SharedLibs = append(depNames.SharedLibs, c.LibraryProperties.Static.Shared_libs...)
1159 } else {
Colin Crossf6566ed2015-03-24 11:13:38 -07001160 if ctx.Device() {
Dan Albertc3144b12015-04-28 18:17:56 -07001161 if c.Properties.Sdk_version == "" {
1162 depNames.CrtBegin = "crtbegin_so"
1163 depNames.CrtEnd = "crtend_so"
1164 } else {
1165 depNames.CrtBegin = "ndk_crtbegin_so." + c.Properties.Sdk_version
1166 depNames.CrtEnd = "ndk_crtend_so." + c.Properties.Sdk_version
1167 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001168 }
Colin Cross2732e9a2015-04-28 13:23:52 -07001169 depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.LibraryProperties.Shared.Whole_static_libs...)
1170 depNames.StaticLibs = append(depNames.StaticLibs, c.LibraryProperties.Shared.Static_libs...)
1171 depNames.SharedLibs = append(depNames.SharedLibs, c.LibraryProperties.Shared.Shared_libs...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001172 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001173
Dan Willemsen218f6562015-07-08 18:13:11 -07001174 c.systemLibs = c.systemSharedLibs(ctx)
1175
Colin Cross21b9a242015-03-24 14:15:58 -07001176 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -08001177}
1178
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001179func (c *CCLibrary) outputFile() common.OptionalPath {
1180 return common.OptionalPathForPath(c.out)
Colin Cross3f40fa42015-01-30 17:27:36 -08001181}
1182
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001183func (c *CCLibrary) getReuseObjFiles() common.Paths {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001184 return c.reuseObjFiles
1185}
1186
1187func (c *CCLibrary) setReuseFrom(reuseFrom ccLibraryInterface) {
1188 c.reuseFrom = reuseFrom
1189}
1190
1191func (c *CCLibrary) getReuseFrom() ccLibraryInterface {
1192 return c.reuseFrom
1193}
1194
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001195func (c *CCLibrary) allObjFiles() common.Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -08001196 return c.objFiles
1197}
1198
Colin Cross28344522015-04-22 13:07:53 -07001199func (c *CCLibrary) exportedFlags() []string {
1200 return c.exportFlags
Colin Cross3f40fa42015-01-30 17:27:36 -08001201}
1202
Colin Cross0676e2d2015-04-24 17:39:18 -07001203func (c *CCLibrary) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Crosse11befc2015-04-27 17:49:17 -07001204 flags = c.CCLinked.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001205
Dan Willemsen490fd492015-11-24 17:53:15 -08001206 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
1207 // all code is position independent, and then those warnings get promoted to
1208 // errors.
1209 if ctx.HostType() != common.Windows {
1210 flags.CFlags = append(flags.CFlags, "-fPIC")
1211 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001212
Colin Crossd8e780d2015-04-28 17:39:43 -07001213 if c.static() {
1214 flags.CFlags = append(flags.CFlags, c.LibraryProperties.Static.Cflags...)
1215 } else {
1216 flags.CFlags = append(flags.CFlags, c.LibraryProperties.Shared.Cflags...)
1217 }
1218
Colin Cross18b6dc52015-04-28 13:20:37 -07001219 if !c.static() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001220 libName := ctx.ModuleName()
1221 // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead
1222 sharedFlag := "-Wl,-shared"
Dan Willemsendd0e2c32015-10-20 14:29:35 -07001223 if flags.Clang || ctx.Host() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001224 sharedFlag = "-shared"
1225 }
Colin Crossf6566ed2015-03-24 11:13:38 -07001226 if ctx.Device() {
Colin Cross97ba0732015-03-23 17:50:24 -07001227 flags.LdFlags = append(flags.LdFlags, "-nostdlib")
Colin Cross3f40fa42015-01-30 17:27:36 -08001228 }
Colin Cross97ba0732015-03-23 17:50:24 -07001229
Colin Cross0af4b842015-04-30 16:36:18 -07001230 if ctx.Darwin() {
1231 flags.LdFlags = append(flags.LdFlags,
1232 "-dynamiclib",
1233 "-single_module",
1234 //"-read_only_relocs suppress",
Dan Willemsen490fd492015-11-24 17:53:15 -08001235 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
Colin Cross0af4b842015-04-30 16:36:18 -07001236 )
1237 } else {
1238 flags.LdFlags = append(flags.LdFlags,
1239 "-Wl,--gc-sections",
1240 sharedFlag,
Dan Willemsen490fd492015-11-24 17:53:15 -08001241 "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix(),
Colin Cross0af4b842015-04-30 16:36:18 -07001242 )
1243 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001244 }
Colin Cross97ba0732015-03-23 17:50:24 -07001245
1246 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001247}
1248
Colin Cross97ba0732015-03-23 17:50:24 -07001249func (c *CCLibrary) compileStaticLibrary(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001250 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001251
1252 staticFlags := flags
Colin Cross581c1892015-04-07 16:50:10 -07001253 objFilesStatic := c.customCompileObjs(ctx, staticFlags, common.DeviceStaticLibrary,
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001254 c.LibraryProperties.Static.Srcs, c.LibraryProperties.Static.Exclude_srcs)
Colin Cross3f40fa42015-01-30 17:27:36 -08001255
1256 objFiles = append(objFiles, objFilesStatic...)
Colin Cross21b9a242015-03-24 14:15:58 -07001257 objFiles = append(objFiles, deps.WholeStaticLibObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001258
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001259 outputFile := common.PathForModuleOut(ctx, ctx.ModuleName()+staticLibraryExtension)
Colin Cross3f40fa42015-01-30 17:27:36 -08001260
Colin Cross0af4b842015-04-30 16:36:18 -07001261 if ctx.Darwin() {
1262 TransformDarwinObjToStaticLib(ctx, objFiles, ccFlagsToBuilderFlags(flags), outputFile)
1263 } else {
1264 TransformObjToStaticLib(ctx, objFiles, ccFlagsToBuilderFlags(flags), outputFile)
1265 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001266
1267 c.objFiles = objFiles
1268 c.out = outputFile
Colin Crossf2298272015-05-12 11:36:53 -07001269
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001270 includeDirs := common.PathsForModuleSrc(ctx, c.Properties.Export_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -07001271 c.exportFlags = []string{includeDirsToFlags(includeDirs)}
Colin Crossa48f71f2015-11-16 18:00:41 -08001272 c.exportFlags = append(c.exportFlags, deps.ReexportedCflags...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001273
1274 ctx.CheckbuildFile(outputFile)
1275}
1276
Colin Cross97ba0732015-03-23 17:50:24 -07001277func (c *CCLibrary) compileSharedLibrary(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001278 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001279
1280 sharedFlags := flags
Colin Cross581c1892015-04-07 16:50:10 -07001281 objFilesShared := c.customCompileObjs(ctx, sharedFlags, common.DeviceSharedLibrary,
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001282 c.LibraryProperties.Shared.Srcs, c.LibraryProperties.Shared.Exclude_srcs)
Colin Cross3f40fa42015-01-30 17:27:36 -08001283
1284 objFiles = append(objFiles, objFilesShared...)
1285
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001286 outputFile := common.PathForModuleOut(ctx, ctx.ModuleName()+flags.Toolchain.ShlibSuffix())
Colin Cross3f40fa42015-01-30 17:27:36 -08001287
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001288 var linkerDeps common.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07001289
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001290 versionScript := common.OptionalPathForModuleSrc(ctx, c.LibraryProperties.Version_script)
1291 unexportedSymbols := common.OptionalPathForModuleSrc(ctx, c.LibraryProperties.Unexported_symbols_list)
1292 forceNotWeakSymbols := common.OptionalPathForModuleSrc(ctx, c.LibraryProperties.Force_symbols_not_weak_list)
1293 forceWeakSymbols := common.OptionalPathForModuleSrc(ctx, c.LibraryProperties.Force_symbols_weak_list)
Dan Willemsen93c28312015-12-04 14:59:08 -08001294 if !ctx.Darwin() {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001295 if versionScript.Valid() {
1296 sharedFlags.LdFlags = append(sharedFlags.LdFlags, "-Wl,--version-script,"+versionScript.String())
1297 linkerDeps = append(linkerDeps, versionScript.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001298 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001299 if unexportedSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001300 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
1301 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001302 if forceNotWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001303 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
1304 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001305 if forceWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001306 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
1307 }
1308 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001309 if versionScript.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001310 ctx.PropertyErrorf("version_script", "Not supported on Darwin")
1311 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001312 if unexportedSymbols.Valid() {
1313 sharedFlags.LdFlags = append(sharedFlags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
1314 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001315 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001316 if forceNotWeakSymbols.Valid() {
1317 sharedFlags.LdFlags = append(sharedFlags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
1318 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001319 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001320 if forceWeakSymbols.Valid() {
1321 sharedFlags.LdFlags = append(sharedFlags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
1322 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001323 }
Colin Crossaee540a2015-07-06 17:48:31 -07001324 }
1325
Colin Cross97ba0732015-03-23 17:50:24 -07001326 TransformObjToDynamicBinary(ctx, objFiles, deps.SharedLibs, deps.StaticLibs,
Colin Crossaee540a2015-07-06 17:48:31 -07001327 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, false,
Dan Willemsen6203ac02015-11-24 12:58:57 -08001328 ccFlagsToBuilderFlags(sharedFlags), outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08001329
1330 c.out = outputFile
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001331 includeDirs := common.PathsForModuleSrc(ctx, c.Properties.Export_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -07001332 c.exportFlags = []string{includeDirsToFlags(includeDirs)}
Colin Crossa48f71f2015-11-16 18:00:41 -08001333 c.exportFlags = append(c.exportFlags, deps.ReexportedCflags...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001334}
1335
Colin Cross97ba0732015-03-23 17:50:24 -07001336func (c *CCLibrary) compileModule(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001337 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001338
1339 // Reuse the object files from the matching static library if it exists
Colin Crossed4cf0b2015-03-26 14:43:45 -07001340 if c.getReuseFrom().ccLibrary() == c {
1341 c.reuseObjFiles = objFiles
Colin Cross3f40fa42015-01-30 17:27:36 -08001342 } else {
Colin Cross2732e9a2015-04-28 13:23:52 -07001343 if c.getReuseFrom().ccLibrary().LibraryProperties.Static.Cflags == nil &&
1344 c.LibraryProperties.Shared.Cflags == nil {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001345 objFiles = append(common.Paths(nil), c.getReuseFrom().getReuseObjFiles()...)
Colin Cross2732e9a2015-04-28 13:23:52 -07001346 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001347 }
1348
Colin Crossed4cf0b2015-03-26 14:43:45 -07001349 if c.static() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001350 c.compileStaticLibrary(ctx, flags, deps, objFiles)
1351 } else {
1352 c.compileSharedLibrary(ctx, flags, deps, objFiles)
1353 }
1354}
1355
Colin Cross97ba0732015-03-23 17:50:24 -07001356func (c *CCLibrary) installStaticLibrary(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001357 // Static libraries do not get installed.
1358}
1359
Colin Cross97ba0732015-03-23 17:50:24 -07001360func (c *CCLibrary) installSharedLibrary(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001361 installDir := "lib"
Colin Cross97ba0732015-03-23 17:50:24 -07001362 if flags.Toolchain.Is64Bit() {
Dan Albertc403f7c2015-03-18 14:01:18 -07001363 installDir = "lib64"
1364 }
1365
Dan Willemsen782a2d12015-12-21 14:55:28 -08001366 ctx.InstallFile(common.PathForModuleInstall(ctx, installDir, c.Properties.Relative_install_path), c.out)
Dan Albertc403f7c2015-03-18 14:01:18 -07001367}
1368
Colin Cross97ba0732015-03-23 17:50:24 -07001369func (c *CCLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001370 if c.static() {
Dan Albertc403f7c2015-03-18 14:01:18 -07001371 c.installStaticLibrary(ctx, flags)
1372 } else {
1373 c.installSharedLibrary(ctx, flags)
1374 }
1375}
1376
Colin Cross3f40fa42015-01-30 17:27:36 -08001377//
1378// Objects (for crt*.o)
1379//
1380
Dan Albertc3144b12015-04-28 18:17:56 -07001381type ccObjectProvider interface {
1382 object() *ccObject
1383}
1384
Colin Cross3f40fa42015-01-30 17:27:36 -08001385type ccObject struct {
Colin Crossfa138792015-04-24 17:31:52 -07001386 CCBase
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001387 out common.OptionalPath
Colin Cross3f40fa42015-01-30 17:27:36 -08001388}
1389
Dan Albertc3144b12015-04-28 18:17:56 -07001390func (c *ccObject) object() *ccObject {
1391 return c
1392}
1393
Colin Cross97ba0732015-03-23 17:50:24 -07001394func CCObjectFactory() (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001395 module := &ccObject{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001396
Colin Crossfa138792015-04-24 17:31:52 -07001397 return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth)
Colin Cross3f40fa42015-01-30 17:27:36 -08001398}
1399
Colin Cross0676e2d2015-04-24 17:39:18 -07001400func (*ccObject) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Cross21b9a242015-03-24 14:15:58 -07001401 // object files can't have any dynamic dependencies
1402 return CCDeps{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001403}
1404
1405func (c *ccObject) compileModule(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001406 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001407
Colin Cross97ba0732015-03-23 17:50:24 -07001408 objFiles = append(objFiles, deps.ObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001409
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001410 var outputFile common.Path
Colin Cross3f40fa42015-01-30 17:27:36 -08001411 if len(objFiles) == 1 {
1412 outputFile = objFiles[0]
1413 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001414 output := common.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension)
1415 TransformObjsToObj(ctx, objFiles, ccFlagsToBuilderFlags(flags), output)
1416 outputFile = output
Colin Cross3f40fa42015-01-30 17:27:36 -08001417 }
1418
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001419 c.out = common.OptionalPathForPath(outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08001420
1421 ctx.CheckbuildFile(outputFile)
1422}
1423
Colin Cross97ba0732015-03-23 17:50:24 -07001424func (c *ccObject) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001425 // Object files do not get installed.
1426}
1427
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001428func (c *ccObject) outputFile() common.OptionalPath {
Colin Cross3f40fa42015-01-30 17:27:36 -08001429 return c.out
1430}
1431
Dan Albertc3144b12015-04-28 18:17:56 -07001432var _ ccObjectProvider = (*ccObject)(nil)
1433
Colin Cross3f40fa42015-01-30 17:27:36 -08001434//
1435// Executables
1436//
1437
Colin Cross7d5136f2015-05-11 13:39:40 -07001438type CCBinaryProperties struct {
1439 // compile executable with -static
Colin Cross06a931b2015-10-28 17:23:31 -07001440 Static_executable *bool
Colin Cross7d5136f2015-05-11 13:39:40 -07001441
1442 // set the name of the output
1443 Stem string `android:"arch_variant"`
1444
1445 // append to the name of the output
1446 Suffix string `android:"arch_variant"`
1447
1448 // if set, add an extra objcopy --prefix-symbols= step
1449 Prefix_symbols string
1450}
1451
Colin Cross97ba0732015-03-23 17:50:24 -07001452type CCBinary struct {
Colin Crossfa138792015-04-24 17:31:52 -07001453 CCLinked
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001454 out common.Path
1455 installFile common.Path
Colin Cross7d5136f2015-05-11 13:39:40 -07001456 BinaryProperties CCBinaryProperties
Colin Cross3f40fa42015-01-30 17:27:36 -08001457}
1458
Colin Crossed4cf0b2015-03-26 14:43:45 -07001459func (c *CCBinary) buildStatic() bool {
Colin Cross06a931b2015-10-28 17:23:31 -07001460 return Bool(c.BinaryProperties.Static_executable)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001461}
1462
1463func (c *CCBinary) buildShared() bool {
Colin Cross06a931b2015-10-28 17:23:31 -07001464 return !Bool(c.BinaryProperties.Static_executable)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001465}
1466
Colin Cross97ba0732015-03-23 17:50:24 -07001467func (c *CCBinary) getStem(ctx common.AndroidModuleContext) string {
Colin Cross4ae185c2015-03-26 15:12:10 -07001468 stem := ctx.ModuleName()
Colin Cross97ba0732015-03-23 17:50:24 -07001469 if c.BinaryProperties.Stem != "" {
Colin Cross4ae185c2015-03-26 15:12:10 -07001470 stem = c.BinaryProperties.Stem
Colin Cross3f40fa42015-01-30 17:27:36 -08001471 }
Colin Cross4ae185c2015-03-26 15:12:10 -07001472
1473 return stem + c.BinaryProperties.Suffix
Colin Cross3f40fa42015-01-30 17:27:36 -08001474}
1475
Colin Cross0676e2d2015-04-24 17:39:18 -07001476func (c *CCBinary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Crosse11befc2015-04-27 17:49:17 -07001477 depNames = c.CCLinked.depNames(ctx, depNames)
Colin Crossf6566ed2015-03-24 11:13:38 -07001478 if ctx.Device() {
Dan Albertc3144b12015-04-28 18:17:56 -07001479 if c.Properties.Sdk_version == "" {
Colin Cross06a931b2015-10-28 17:23:31 -07001480 if Bool(c.BinaryProperties.Static_executable) {
Dan Albertc3144b12015-04-28 18:17:56 -07001481 depNames.CrtBegin = "crtbegin_static"
1482 } else {
1483 depNames.CrtBegin = "crtbegin_dynamic"
1484 }
1485 depNames.CrtEnd = "crtend_android"
Colin Cross3f40fa42015-01-30 17:27:36 -08001486 } else {
Colin Cross06a931b2015-10-28 17:23:31 -07001487 if Bool(c.BinaryProperties.Static_executable) {
Dan Albertc3144b12015-04-28 18:17:56 -07001488 depNames.CrtBegin = "ndk_crtbegin_static." + c.Properties.Sdk_version
1489 } else {
1490 depNames.CrtBegin = "ndk_crtbegin_dynamic." + c.Properties.Sdk_version
1491 }
1492 depNames.CrtEnd = "ndk_crtend_android." + c.Properties.Sdk_version
Colin Cross3f40fa42015-01-30 17:27:36 -08001493 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001494
Colin Cross06a931b2015-10-28 17:23:31 -07001495 if Bool(c.BinaryProperties.Static_executable) {
Colin Cross74d1ec02015-04-28 13:30:13 -07001496 if c.stl(ctx) == "libc++_static" {
1497 depNames.StaticLibs = append(depNames.StaticLibs, "libm", "libc", "libdl")
1498 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001499 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
1500 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
1501 // move them to the beginning of deps.LateStaticLibs
1502 var groupLibs []string
1503 depNames.StaticLibs, groupLibs = filterList(depNames.StaticLibs,
1504 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
1505 depNames.LateStaticLibs = append(groupLibs, depNames.LateStaticLibs...)
1506 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001507 }
Colin Cross21b9a242015-03-24 14:15:58 -07001508 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -08001509}
1510
Colin Cross97ba0732015-03-23 17:50:24 -07001511func NewCCBinary(binary *CCBinary, module CCModuleType,
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001512 hod common.HostOrDeviceSupported, multilib common.Multilib,
1513 props ...interface{}) (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001514
Colin Cross1f8f2342015-03-26 16:09:47 -07001515 props = append(props, &binary.BinaryProperties)
1516
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001517 return newCCDynamic(&binary.CCLinked, module, hod, multilib, props...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001518}
1519
Colin Cross97ba0732015-03-23 17:50:24 -07001520func CCBinaryFactory() (blueprint.Module, []interface{}) {
1521 module := &CCBinary{}
1522
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001523 return NewCCBinary(module, module, common.HostAndDeviceSupported, common.MultilibFirst)
Colin Cross3f40fa42015-01-30 17:27:36 -08001524}
1525
Colin Cross6362e272015-10-29 15:25:03 -07001526func (c *CCBinary) ModifyProperties(ctx CCModuleContext) {
Colin Cross0af4b842015-04-30 16:36:18 -07001527 if ctx.Darwin() {
Colin Cross06a931b2015-10-28 17:23:31 -07001528 c.BinaryProperties.Static_executable = proptools.BoolPtr(false)
Colin Cross0af4b842015-04-30 16:36:18 -07001529 }
Colin Cross06a931b2015-10-28 17:23:31 -07001530 if Bool(c.BinaryProperties.Static_executable) {
Colin Cross18b6dc52015-04-28 13:20:37 -07001531 c.dynamicProperties.VariantIsStaticBinary = true
1532 }
1533}
1534
Colin Cross0676e2d2015-04-24 17:39:18 -07001535func (c *CCBinary) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Crosse11befc2015-04-27 17:49:17 -07001536 flags = c.CCLinked.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001537
Dan Willemsen490fd492015-11-24 17:53:15 -08001538 if ctx.Host() {
1539 flags.LdFlags = append(flags.LdFlags, "-pie")
1540 if ctx.HostType() == common.Windows {
1541 flags.LdFlags = append(flags.LdFlags, "-Wl,-e_mainCRTStartup")
1542 }
1543 }
1544
1545 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
1546 // all code is position independent, and then those warnings get promoted to
1547 // errors.
1548 if ctx.HostType() != common.Windows {
1549 flags.CFlags = append(flags.CFlags, "-fpie")
1550 }
Colin Cross97ba0732015-03-23 17:50:24 -07001551
Colin Crossf6566ed2015-03-24 11:13:38 -07001552 if ctx.Device() {
Colin Cross06a931b2015-10-28 17:23:31 -07001553 if Bool(c.BinaryProperties.Static_executable) {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001554 // Clang driver needs -static to create static executable.
1555 // However, bionic/linker uses -shared to overwrite.
1556 // Linker for x86 targets does not allow coexistance of -static and -shared,
1557 // so we add -static only if -shared is not used.
1558 if !inList("-shared", flags.LdFlags) {
1559 flags.LdFlags = append(flags.LdFlags, "-static")
1560 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001561
Colin Crossed4cf0b2015-03-26 14:43:45 -07001562 flags.LdFlags = append(flags.LdFlags,
1563 "-nostdlib",
1564 "-Bstatic",
1565 "-Wl,--gc-sections",
1566 )
1567
1568 } else {
1569 linker := "/system/bin/linker"
1570 if flags.Toolchain.Is64Bit() {
1571 linker = "/system/bin/linker64"
1572 }
1573
1574 flags.LdFlags = append(flags.LdFlags,
Colin Cross979422c2015-12-01 14:09:48 -08001575 "-pie",
Colin Crossed4cf0b2015-03-26 14:43:45 -07001576 "-nostdlib",
1577 "-Bdynamic",
1578 fmt.Sprintf("-Wl,-dynamic-linker,%s", linker),
1579 "-Wl,--gc-sections",
1580 "-Wl,-z,nocopyreloc",
1581 )
1582 }
Colin Cross0af4b842015-04-30 16:36:18 -07001583 } else if ctx.Darwin() {
1584 flags.LdFlags = append(flags.LdFlags, "-Wl,-headerpad_max_install_names")
Colin Cross3f40fa42015-01-30 17:27:36 -08001585 }
1586
Colin Cross97ba0732015-03-23 17:50:24 -07001587 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001588}
1589
Colin Cross97ba0732015-03-23 17:50:24 -07001590func (c *CCBinary) compileModule(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001591 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001592
Colin Cross06a931b2015-10-28 17:23:31 -07001593 if !Bool(c.BinaryProperties.Static_executable) && inList("libc", c.Properties.Static_libs) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001594 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
1595 "from static libs or set static_executable: true")
1596 }
1597
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001598 outputFile := common.PathForModuleOut(ctx, c.getStem(ctx)+flags.Toolchain.ExecutableSuffix())
Dan Albertc403f7c2015-03-18 14:01:18 -07001599 c.out = outputFile
Colin Crossbfae8852015-03-26 14:44:11 -07001600 if c.BinaryProperties.Prefix_symbols != "" {
1601 afterPrefixSymbols := outputFile
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001602 outputFile = common.PathForModuleOut(ctx, c.getStem(ctx)+".intermediate")
Colin Crossbfae8852015-03-26 14:44:11 -07001603 TransformBinaryPrefixSymbols(ctx, c.BinaryProperties.Prefix_symbols, outputFile,
1604 ccFlagsToBuilderFlags(flags), afterPrefixSymbols)
1605 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001606
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001607 var linkerDeps common.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07001608
Colin Cross97ba0732015-03-23 17:50:24 -07001609 TransformObjToDynamicBinary(ctx, objFiles, deps.SharedLibs, deps.StaticLibs,
Colin Crossaee540a2015-07-06 17:48:31 -07001610 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
Colin Cross77b00fa2015-03-16 16:15:49 -07001611 ccFlagsToBuilderFlags(flags), outputFile)
Dan Albertc403f7c2015-03-18 14:01:18 -07001612}
Colin Cross3f40fa42015-01-30 17:27:36 -08001613
Colin Cross97ba0732015-03-23 17:50:24 -07001614func (c *CCBinary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Willemsen782a2d12015-12-21 14:55:28 -08001615 c.installFile = ctx.InstallFile(common.PathForModuleInstall(ctx, "bin", c.Properties.Relative_install_path), c.out)
Colin Crossd350ecd2015-04-28 13:25:36 -07001616}
1617
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001618func (c *CCBinary) HostToolPath() common.OptionalPath {
Colin Crossd350ecd2015-04-28 13:25:36 -07001619 if c.HostOrDevice().Host() {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001620 return common.OptionalPathForPath(c.installFile)
Colin Crossd350ecd2015-04-28 13:25:36 -07001621 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001622 return common.OptionalPath{}
Dan Albertc403f7c2015-03-18 14:01:18 -07001623}
1624
Colin Cross6002e052015-09-16 16:00:08 -07001625func (c *CCBinary) binary() *CCBinary {
1626 return c
1627}
1628
1629type testPerSrc interface {
1630 binary() *CCBinary
1631 testPerSrc() bool
1632}
1633
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001634var _ testPerSrc = (*CCTest)(nil)
Colin Cross6002e052015-09-16 16:00:08 -07001635
Colin Cross6362e272015-10-29 15:25:03 -07001636func testPerSrcMutator(mctx common.AndroidBottomUpMutatorContext) {
Colin Cross6002e052015-09-16 16:00:08 -07001637 if test, ok := mctx.Module().(testPerSrc); ok {
1638 if test.testPerSrc() {
1639 testNames := make([]string, len(test.binary().Properties.Srcs))
1640 for i, src := range test.binary().Properties.Srcs {
1641 testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
1642 }
1643 tests := mctx.CreateLocalVariations(testNames...)
1644 for i, src := range test.binary().Properties.Srcs {
1645 tests[i].(testPerSrc).binary().Properties.Srcs = []string{src}
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001646 tests[i].(testPerSrc).binary().BinaryProperties.Stem = testNames[i]
Colin Cross6002e052015-09-16 16:00:08 -07001647 }
1648 }
1649 }
Colin Cross7d5136f2015-05-11 13:39:40 -07001650}
1651
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001652type CCTestProperties struct {
1653 // if set, build against the gtest library. Defaults to true.
1654 Gtest bool
1655
1656 // Create a separate binary for each source file. Useful when there is
1657 // global state that can not be torn down and reset between each test suite.
1658 Test_per_src *bool
1659}
1660
Colin Cross9ffb4f52015-04-24 17:48:09 -07001661type CCTest struct {
Colin Cross97ba0732015-03-23 17:50:24 -07001662 CCBinary
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001663
1664 TestProperties CCTestProperties
Dan Albertc403f7c2015-03-18 14:01:18 -07001665}
1666
Colin Cross9ffb4f52015-04-24 17:48:09 -07001667func (c *CCTest) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Cross0676e2d2015-04-24 17:39:18 -07001668 flags = c.CCBinary.flags(ctx, flags)
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001669 if !c.TestProperties.Gtest {
1670 return flags
1671 }
Dan Albertc403f7c2015-03-18 14:01:18 -07001672
Colin Cross97ba0732015-03-23 17:50:24 -07001673 flags.CFlags = append(flags.CFlags, "-DGTEST_HAS_STD_STRING")
Colin Crossf6566ed2015-03-24 11:13:38 -07001674 if ctx.Host() {
Colin Cross97ba0732015-03-23 17:50:24 -07001675 flags.CFlags = append(flags.CFlags, "-O0", "-g")
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001676
1677 if ctx.HostType() == common.Windows {
1678 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_WINDOWS")
1679 } else {
1680 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX")
1681 flags.LdFlags = append(flags.LdFlags, "-lpthread")
1682 }
1683 } else {
1684 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX_ANDROID")
Dan Albertc403f7c2015-03-18 14:01:18 -07001685 }
1686
1687 // TODO(danalbert): Make gtest export its dependencies.
Colin Cross28344522015-04-22 13:07:53 -07001688 flags.CFlags = append(flags.CFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001689 "-I"+common.PathForSource(ctx, "external/gtest/include").String())
Dan Albertc403f7c2015-03-18 14:01:18 -07001690
Colin Cross21b9a242015-03-24 14:15:58 -07001691 return flags
Dan Albertc403f7c2015-03-18 14:01:18 -07001692}
1693
Colin Cross9ffb4f52015-04-24 17:48:09 -07001694func (c *CCTest) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001695 if c.TestProperties.Gtest {
1696 depNames.StaticLibs = append(depNames.StaticLibs, "libgtest_main", "libgtest")
1697 }
Colin Crossa8a93d32015-04-28 13:26:49 -07001698 depNames = c.CCBinary.depNames(ctx, depNames)
Colin Cross21b9a242015-03-24 14:15:58 -07001699 return depNames
Dan Albertc403f7c2015-03-18 14:01:18 -07001700}
1701
Dan Willemsen782a2d12015-12-21 14:55:28 -08001702func (c *CCTest) InstallInData() bool {
1703 return true
1704}
1705
Colin Cross9ffb4f52015-04-24 17:48:09 -07001706func (c *CCTest) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001707 installDir := "nativetest"
1708 if flags.Toolchain.Is64Bit() {
1709 installDir = "nativetest64"
Dan Albertc403f7c2015-03-18 14:01:18 -07001710 }
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001711 ctx.InstallFile(common.PathForModuleInstall(ctx, installDir, ctx.ModuleName()), c.out)
1712}
1713
1714func (c *CCTest) testPerSrc() bool {
1715 return Bool(c.TestProperties.Test_per_src)
Dan Albertc403f7c2015-03-18 14:01:18 -07001716}
1717
Colin Cross9ffb4f52015-04-24 17:48:09 -07001718func NewCCTest(test *CCTest, module CCModuleType,
1719 hod common.HostOrDeviceSupported, props ...interface{}) (blueprint.Module, []interface{}) {
1720
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001721 props = append(props, &test.TestProperties)
1722
1723 return NewCCBinary(&test.CCBinary, module, hod, common.MultilibBoth, props...)
Colin Cross9ffb4f52015-04-24 17:48:09 -07001724}
1725
1726func CCTestFactory() (blueprint.Module, []interface{}) {
1727 module := &CCTest{}
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001728 module.TestProperties.Gtest = true
Colin Cross9ffb4f52015-04-24 17:48:09 -07001729
1730 return NewCCTest(module, module, common.HostAndDeviceSupported)
1731}
1732
Colin Cross2ba19d92015-05-07 15:44:20 -07001733type CCBenchmark struct {
1734 CCBinary
1735}
1736
1737func (c *CCBenchmark) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
1738 depNames = c.CCBinary.depNames(ctx, depNames)
Dan Willemsenf8e98b02015-09-11 17:41:44 -07001739 depNames.StaticLibs = append(depNames.StaticLibs, "libbenchmark", "libbase")
Colin Cross2ba19d92015-05-07 15:44:20 -07001740 return depNames
1741}
1742
Dan Willemsen782a2d12015-12-21 14:55:28 -08001743func (c *CCBenchmark) InstallInData() bool {
1744 return true
1745}
1746
Colin Cross2ba19d92015-05-07 15:44:20 -07001747func (c *CCBenchmark) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
1748 if ctx.Device() {
Dan Willemsen782a2d12015-12-21 14:55:28 -08001749 installDir := "nativetest"
1750 if flags.Toolchain.Is64Bit() {
1751 installDir = "nativetest64"
1752 }
1753 ctx.InstallFile(common.PathForModuleInstall(ctx, installDir, ctx.ModuleName()), c.out)
Colin Cross2ba19d92015-05-07 15:44:20 -07001754 } else {
1755 c.CCBinary.installModule(ctx, flags)
1756 }
1757}
1758
1759func NewCCBenchmark(test *CCBenchmark, module CCModuleType,
1760 hod common.HostOrDeviceSupported, props ...interface{}) (blueprint.Module, []interface{}) {
1761
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001762 return NewCCBinary(&test.CCBinary, module, hod, common.MultilibFirst, props...)
Colin Cross2ba19d92015-05-07 15:44:20 -07001763}
1764
1765func CCBenchmarkFactory() (blueprint.Module, []interface{}) {
1766 module := &CCBenchmark{}
1767
1768 return NewCCBenchmark(module, module, common.HostAndDeviceSupported)
1769}
1770
Colin Cross3f40fa42015-01-30 17:27:36 -08001771//
1772// Static library
1773//
1774
Colin Cross97ba0732015-03-23 17:50:24 -07001775func CCLibraryStaticFactory() (blueprint.Module, []interface{}) {
1776 module := &CCLibrary{}
1777 module.LibraryProperties.BuildStatic = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001778
Colin Cross97ba0732015-03-23 17:50:24 -07001779 return NewCCLibrary(module, module, common.HostAndDeviceSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001780}
1781
1782//
1783// Shared libraries
1784//
1785
Colin Cross97ba0732015-03-23 17:50:24 -07001786func CCLibrarySharedFactory() (blueprint.Module, []interface{}) {
1787 module := &CCLibrary{}
1788 module.LibraryProperties.BuildShared = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001789
Colin Cross97ba0732015-03-23 17:50:24 -07001790 return NewCCLibrary(module, module, common.HostAndDeviceSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001791}
1792
1793//
1794// Host static library
1795//
1796
Colin Cross97ba0732015-03-23 17:50:24 -07001797func CCLibraryHostStaticFactory() (blueprint.Module, []interface{}) {
1798 module := &CCLibrary{}
1799 module.LibraryProperties.BuildStatic = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001800
Colin Cross97ba0732015-03-23 17:50:24 -07001801 return NewCCLibrary(module, module, common.HostSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001802}
1803
1804//
1805// Host Shared libraries
1806//
1807
Colin Cross97ba0732015-03-23 17:50:24 -07001808func CCLibraryHostSharedFactory() (blueprint.Module, []interface{}) {
1809 module := &CCLibrary{}
1810 module.LibraryProperties.BuildShared = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001811
Colin Cross97ba0732015-03-23 17:50:24 -07001812 return NewCCLibrary(module, module, common.HostSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001813}
1814
1815//
1816// Host Binaries
1817//
1818
Colin Cross97ba0732015-03-23 17:50:24 -07001819func CCBinaryHostFactory() (blueprint.Module, []interface{}) {
1820 module := &CCBinary{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001821
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001822 return NewCCBinary(module, module, common.HostSupported, common.MultilibFirst)
Colin Cross3f40fa42015-01-30 17:27:36 -08001823}
1824
1825//
Colin Cross1f8f2342015-03-26 16:09:47 -07001826// Host Tests
1827//
1828
1829func CCTestHostFactory() (blueprint.Module, []interface{}) {
Colin Cross9ffb4f52015-04-24 17:48:09 -07001830 module := &CCTest{}
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001831 return NewCCTest(module, module, common.HostSupported)
Colin Cross1f8f2342015-03-26 16:09:47 -07001832}
1833
1834//
Colin Cross2ba19d92015-05-07 15:44:20 -07001835// Host Benchmarks
1836//
1837
1838func CCBenchmarkHostFactory() (blueprint.Module, []interface{}) {
1839 module := &CCBenchmark{}
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001840 return NewCCBinary(&module.CCBinary, module, common.HostSupported, common.MultilibFirst)
Colin Cross2ba19d92015-05-07 15:44:20 -07001841}
1842
1843//
Colin Crosscfad1192015-11-02 16:43:11 -08001844// Defaults
1845//
1846type CCDefaults struct {
1847 common.AndroidModuleBase
1848 common.DefaultsModule
1849}
1850
1851func (*CCDefaults) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
1852}
1853
1854func CCDefaultsFactory() (blueprint.Module, []interface{}) {
1855 module := &CCDefaults{}
1856
1857 propertyStructs := []interface{}{
1858 &CCBaseProperties{},
1859 &CCLibraryProperties{},
1860 &CCBinaryProperties{},
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001861 &CCTestProperties{},
Colin Crosscfad1192015-11-02 16:43:11 -08001862 &CCUnusedProperties{},
1863 }
1864
Dan Willemsen218f6562015-07-08 18:13:11 -07001865 _, propertyStructs = common.InitAndroidArchModule(module, common.HostAndDeviceDefault,
1866 common.MultilibDefault, propertyStructs...)
Colin Crosscfad1192015-11-02 16:43:11 -08001867
1868 return common.InitDefaultsModule(module, module, propertyStructs...)
1869}
1870
1871//
Colin Cross3f40fa42015-01-30 17:27:36 -08001872// Device libraries shipped with gcc
1873//
1874
1875type toolchainLibrary struct {
Colin Cross97ba0732015-03-23 17:50:24 -07001876 CCLibrary
Colin Cross3f40fa42015-01-30 17:27:36 -08001877}
1878
Colin Cross0676e2d2015-04-24 17:39:18 -07001879func (*toolchainLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Cross3f40fa42015-01-30 17:27:36 -08001880 // toolchain libraries can't have any dependencies
Colin Cross21b9a242015-03-24 14:15:58 -07001881 return CCDeps{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001882}
1883
Colin Cross97ba0732015-03-23 17:50:24 -07001884func ToolchainLibraryFactory() (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001885 module := &toolchainLibrary{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001886
Colin Cross97ba0732015-03-23 17:50:24 -07001887 module.LibraryProperties.BuildStatic = true
1888
Colin Crossfa138792015-04-24 17:31:52 -07001889 return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth,
Colin Cross21b9a242015-03-24 14:15:58 -07001890 &module.LibraryProperties)
Colin Cross3f40fa42015-01-30 17:27:36 -08001891}
1892
1893func (c *toolchainLibrary) compileModule(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001894 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001895
1896 libName := ctx.ModuleName() + staticLibraryExtension
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001897 outputFile := common.PathForModuleOut(ctx, libName)
Colin Cross3f40fa42015-01-30 17:27:36 -08001898
1899 CopyGccLib(ctx, libName, ccFlagsToBuilderFlags(flags), outputFile)
1900
1901 c.out = outputFile
1902
1903 ctx.CheckbuildFile(outputFile)
1904}
1905
Colin Cross97ba0732015-03-23 17:50:24 -07001906func (c *toolchainLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001907 // Toolchain libraries do not get installed.
1908}
1909
Dan Albertbe961682015-03-18 23:38:50 -07001910// NDK prebuilt libraries.
1911//
1912// These differ from regular prebuilts in that they aren't stripped and usually aren't installed
1913// either (with the exception of the shared STLs, which are installed to the app's directory rather
1914// than to the system image).
1915
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001916func getNdkLibDir(ctx common.AndroidModuleContext, toolchain Toolchain, version string) common.SourcePath {
1917 return common.PathForSource(ctx, fmt.Sprintf("prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/lib",
1918 version, toolchain.Name()))
Dan Albertbe961682015-03-18 23:38:50 -07001919}
1920
Dan Albertc3144b12015-04-28 18:17:56 -07001921func ndkPrebuiltModuleToPath(ctx common.AndroidModuleContext, toolchain Toolchain,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001922 ext string, version string) common.Path {
Dan Albertc3144b12015-04-28 18:17:56 -07001923
1924 // NDK prebuilts are named like: ndk_NAME.EXT.SDK_VERSION.
1925 // We want to translate to just NAME.EXT
1926 name := strings.Split(strings.TrimPrefix(ctx.ModuleName(), "ndk_"), ".")[0]
1927 dir := getNdkLibDir(ctx, toolchain, version)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001928 return dir.Join(ctx, name+ext)
Dan Albertc3144b12015-04-28 18:17:56 -07001929}
1930
1931type ndkPrebuiltObject struct {
1932 ccObject
1933}
1934
Dan Albertc3144b12015-04-28 18:17:56 -07001935func (*ndkPrebuiltObject) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
1936 // NDK objects can't have any dependencies
1937 return CCDeps{}
1938}
1939
1940func NdkPrebuiltObjectFactory() (blueprint.Module, []interface{}) {
1941 module := &ndkPrebuiltObject{}
1942 return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth)
1943}
1944
1945func (c *ndkPrebuiltObject) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001946 deps CCPathDeps, objFiles common.Paths) {
Dan Albertc3144b12015-04-28 18:17:56 -07001947 // A null build step, but it sets up the output path.
1948 if !strings.HasPrefix(ctx.ModuleName(), "ndk_crt") {
1949 ctx.ModuleErrorf("NDK prebuilts must have an ndk_crt prefixed name")
1950 }
1951
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001952 c.out = common.OptionalPathForPath(ndkPrebuiltModuleToPath(ctx, flags.Toolchain, objectExtension, c.Properties.Sdk_version))
Dan Albertc3144b12015-04-28 18:17:56 -07001953}
1954
1955func (c *ndkPrebuiltObject) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
1956 // Objects do not get installed.
1957}
1958
1959var _ ccObjectProvider = (*ndkPrebuiltObject)(nil)
1960
Dan Albertbe961682015-03-18 23:38:50 -07001961type ndkPrebuiltLibrary struct {
1962 CCLibrary
1963}
1964
Colin Cross0676e2d2015-04-24 17:39:18 -07001965func (*ndkPrebuiltLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Dan Albertbe961682015-03-18 23:38:50 -07001966 // NDK libraries can't have any dependencies
1967 return CCDeps{}
1968}
1969
1970func NdkPrebuiltLibraryFactory() (blueprint.Module, []interface{}) {
1971 module := &ndkPrebuiltLibrary{}
1972 module.LibraryProperties.BuildShared = true
1973 return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
1974}
1975
1976func (c *ndkPrebuiltLibrary) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001977 deps CCPathDeps, objFiles common.Paths) {
Dan Albertbe961682015-03-18 23:38:50 -07001978 // A null build step, but it sets up the output path.
1979 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
1980 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
1981 }
1982
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001983 includeDirs := common.PathsForModuleSrc(ctx, c.Properties.Export_include_dirs)
1984 c.exportFlags = []string{common.JoinWithPrefix(includeDirs.Strings(), "-isystem ")}
Dan Albertbe961682015-03-18 23:38:50 -07001985
Dan Willemsen490fd492015-11-24 17:53:15 -08001986 c.out = ndkPrebuiltModuleToPath(ctx, flags.Toolchain, flags.Toolchain.ShlibSuffix(),
Dan Albertc3144b12015-04-28 18:17:56 -07001987 c.Properties.Sdk_version)
Dan Albertbe961682015-03-18 23:38:50 -07001988}
1989
1990func (c *ndkPrebuiltLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc3144b12015-04-28 18:17:56 -07001991 // NDK prebuilt libraries do not get installed.
Dan Albertbe961682015-03-18 23:38:50 -07001992}
1993
1994// The NDK STLs are slightly different from the prebuilt system libraries:
1995// * Are not specific to each platform version.
1996// * The libraries are not in a predictable location for each STL.
1997
1998type ndkPrebuiltStl struct {
1999 ndkPrebuiltLibrary
2000}
2001
2002type ndkPrebuiltStaticStl struct {
2003 ndkPrebuiltStl
2004}
2005
2006type ndkPrebuiltSharedStl struct {
2007 ndkPrebuiltStl
2008}
2009
2010func NdkPrebuiltSharedStlFactory() (blueprint.Module, []interface{}) {
2011 module := &ndkPrebuiltSharedStl{}
2012 module.LibraryProperties.BuildShared = true
2013 return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
2014}
2015
2016func NdkPrebuiltStaticStlFactory() (blueprint.Module, []interface{}) {
2017 module := &ndkPrebuiltStaticStl{}
2018 module.LibraryProperties.BuildStatic = true
2019 return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
2020}
2021
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002022func getNdkStlLibDir(ctx common.AndroidModuleContext, toolchain Toolchain, stl string) common.SourcePath {
Dan Albertbe961682015-03-18 23:38:50 -07002023 gccVersion := toolchain.GccVersion()
2024 var libDir string
2025 switch stl {
2026 case "libstlport":
2027 libDir = "cxx-stl/stlport/libs"
2028 case "libc++":
2029 libDir = "cxx-stl/llvm-libc++/libs"
2030 case "libgnustl":
2031 libDir = fmt.Sprintf("cxx-stl/gnu-libstdc++/%s/libs", gccVersion)
2032 }
2033
2034 if libDir != "" {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002035 ndkSrcRoot := "prebuilts/ndk/current/sources"
2036 return common.PathForSource(ctx, ndkSrcRoot).Join(ctx, libDir, ctx.Arch().Abi[0])
Dan Albertbe961682015-03-18 23:38:50 -07002037 }
2038
2039 ctx.ModuleErrorf("Unknown NDK STL: %s", stl)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002040 return common.PathForSource(ctx, "")
Dan Albertbe961682015-03-18 23:38:50 -07002041}
2042
2043func (c *ndkPrebuiltStl) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002044 deps CCPathDeps, objFiles common.Paths) {
Dan Albertbe961682015-03-18 23:38:50 -07002045 // A null build step, but it sets up the output path.
2046 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
2047 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
2048 }
2049
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002050 includeDirs := common.PathsForModuleSrc(ctx, c.Properties.Export_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -07002051 c.exportFlags = []string{includeDirsToFlags(includeDirs)}
Dan Albertbe961682015-03-18 23:38:50 -07002052
2053 libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_")
Dan Willemsen490fd492015-11-24 17:53:15 -08002054 libExt := flags.Toolchain.ShlibSuffix()
Dan Albertbe961682015-03-18 23:38:50 -07002055 if c.LibraryProperties.BuildStatic {
2056 libExt = staticLibraryExtension
2057 }
2058
2059 stlName := strings.TrimSuffix(libName, "_shared")
2060 stlName = strings.TrimSuffix(stlName, "_static")
2061 libDir := getNdkStlLibDir(ctx, flags.Toolchain, stlName)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002062 c.out = libDir.Join(ctx, libName+libExt)
Dan Albertbe961682015-03-18 23:38:50 -07002063}
2064
Colin Cross6362e272015-10-29 15:25:03 -07002065func linkageMutator(mctx common.AndroidBottomUpMutatorContext) {
Colin Crossed4cf0b2015-03-26 14:43:45 -07002066 if c, ok := mctx.Module().(ccLinkedInterface); ok {
Colin Cross3f40fa42015-01-30 17:27:36 -08002067 var modules []blueprint.Module
Colin Crossed4cf0b2015-03-26 14:43:45 -07002068 if c.buildStatic() && c.buildShared() {
Colin Cross3f40fa42015-01-30 17:27:36 -08002069 modules = mctx.CreateLocalVariations("static", "shared")
Colin Cross18b6dc52015-04-28 13:20:37 -07002070 modules[0].(ccLinkedInterface).setStatic(true)
2071 modules[1].(ccLinkedInterface).setStatic(false)
Colin Crossed4cf0b2015-03-26 14:43:45 -07002072 } else if c.buildStatic() {
Colin Cross3f40fa42015-01-30 17:27:36 -08002073 modules = mctx.CreateLocalVariations("static")
Colin Cross18b6dc52015-04-28 13:20:37 -07002074 modules[0].(ccLinkedInterface).setStatic(true)
Colin Crossed4cf0b2015-03-26 14:43:45 -07002075 } else if c.buildShared() {
Colin Cross3f40fa42015-01-30 17:27:36 -08002076 modules = mctx.CreateLocalVariations("shared")
Colin Cross18b6dc52015-04-28 13:20:37 -07002077 modules[0].(ccLinkedInterface).setStatic(false)
Colin Cross3f40fa42015-01-30 17:27:36 -08002078 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07002079 panic(fmt.Errorf("ccLibrary %q not static or shared", mctx.ModuleName()))
Colin Cross3f40fa42015-01-30 17:27:36 -08002080 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07002081
2082 if _, ok := c.(ccLibraryInterface); ok {
2083 reuseFrom := modules[0].(ccLibraryInterface)
2084 for _, m := range modules {
2085 m.(ccLibraryInterface).setReuseFrom(reuseFrom)
Colin Cross3f40fa42015-01-30 17:27:36 -08002086 }
2087 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002088 }
2089}
Colin Cross74d1ec02015-04-28 13:30:13 -07002090
2091// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
2092// modifies the slice contents in place, and returns a subslice of the original slice
2093func lastUniqueElements(list []string) []string {
2094 totalSkip := 0
2095 for i := len(list) - 1; i >= totalSkip; i-- {
2096 skip := 0
2097 for j := i - 1; j >= totalSkip; j-- {
2098 if list[i] == list[j] {
2099 skip++
2100 } else {
2101 list[j+skip] = list[j]
2102 }
2103 }
2104 totalSkip += skip
2105 }
2106 return list[totalSkip:]
2107}
Colin Cross06a931b2015-10-28 17:23:31 -07002108
2109var Bool = proptools.Bool