blob: bc48622598bf322500cf345e6e5f3d9e049aa14f [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",
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800118 strings.Join(append(clangFilterUnknownCflags(commonGlobalCflags), "${clangExtraCflags}"), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800119 pctx.StaticVariable("deviceClangGlobalCflags",
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800120 strings.Join(append(clangFilterUnknownCflags(deviceGlobalCflags), "${clangExtraTargetCflags}"), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800121 pctx.StaticVariable("hostClangGlobalCflags",
122 strings.Join(clangFilterUnknownCflags(hostGlobalCflags), " "))
Tim Kilbournf2948142015-03-11 12:03:03 -0700123 pctx.StaticVariable("commonClangGlobalCppflags",
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800124 strings.Join(append(clangFilterUnknownCflags(commonGlobalCppflags), "${clangExtraCppflags}"), " "))
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
553 target := "-target " + toolchain.ClangTriple()
554 gccPrefix := "-B" + filepath.Join(toolchain.GccRoot(), toolchain.GccTriple(), "bin")
555
Colin Cross97ba0732015-03-23 17:50:24 -0700556 flags.CFlags = append(flags.CFlags, target, gccPrefix)
557 flags.AsFlags = append(flags.AsFlags, target, gccPrefix)
558 flags.LdFlags = append(flags.LdFlags, target, gccPrefix)
Colin Cross3f40fa42015-01-30 17:27:36 -0800559 }
560
Colin Cross06a931b2015-10-28 17:23:31 -0700561 if !Bool(c.Properties.No_default_compiler_flags) {
562 if ctx.Device() && !Bool(c.Properties.Allow_undefined_symbols) {
Colin Cross97ba0732015-03-23 17:50:24 -0700563 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-undefined")
Colin Cross3f40fa42015-01-30 17:27:36 -0800564 }
565
Colin Cross56b4d452015-04-21 17:38:44 -0700566 flags.GlobalFlags = append(flags.GlobalFlags, instructionSetFlags)
567
Colin Cross97ba0732015-03-23 17:50:24 -0700568 if flags.Clang {
Dan Willemsen32968a22016-01-12 22:25:34 -0800569 flags.AsFlags = append(flags.AsFlags, toolchain.ClangAsflags())
Colin Cross97ba0732015-03-23 17:50:24 -0700570 flags.CppFlags = append(flags.CppFlags, "${commonClangGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -0700571 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -0800572 toolchain.ClangCflags(),
573 "${commonClangGlobalCflags}",
Colin Crossd3ba0392015-05-07 14:11:29 -0700574 fmt.Sprintf("${%sClangGlobalCflags}", ctx.HostOrDevice()))
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800575
576 flags.ConlyFlags = append(flags.ConlyFlags, "${clangExtraConlyflags}")
Colin Cross3f40fa42015-01-30 17:27:36 -0800577 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700578 flags.CppFlags = append(flags.CppFlags, "${commonGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -0700579 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -0800580 toolchain.Cflags(),
581 "${commonGlobalCflags}",
Colin Crossd3ba0392015-05-07 14:11:29 -0700582 fmt.Sprintf("${%sGlobalCflags}", ctx.HostOrDevice()))
Colin Cross3f40fa42015-01-30 17:27:36 -0800583 }
584
Colin Cross7b66f152015-12-15 16:07:43 -0800585 if Bool(ctx.AConfig().ProductVariables.Brillo) {
586 flags.GlobalFlags = append(flags.GlobalFlags, "-D__BRILLO__")
587 }
588
Colin Crossf6566ed2015-03-24 11:13:38 -0700589 if ctx.Device() {
Colin Cross06a931b2015-10-28 17:23:31 -0700590 if Bool(c.Properties.Rtti) {
Colin Cross97ba0732015-03-23 17:50:24 -0700591 flags.CppFlags = append(flags.CppFlags, "-frtti")
Colin Cross3f40fa42015-01-30 17:27:36 -0800592 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700593 flags.CppFlags = append(flags.CppFlags, "-fno-rtti")
Colin Cross3f40fa42015-01-30 17:27:36 -0800594 }
595 }
596
Colin Cross97ba0732015-03-23 17:50:24 -0700597 flags.AsFlags = append(flags.AsFlags, "-D__ASSEMBLY__")
Colin Cross3f40fa42015-01-30 17:27:36 -0800598
Colin Cross97ba0732015-03-23 17:50:24 -0700599 if flags.Clang {
600 flags.CppFlags = append(flags.CppFlags, toolchain.ClangCppflags())
601 flags.LdFlags = append(flags.LdFlags, toolchain.ClangLdflags())
Colin Cross3f40fa42015-01-30 17:27:36 -0800602 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700603 flags.CppFlags = append(flags.CppFlags, toolchain.Cppflags())
604 flags.LdFlags = append(flags.LdFlags, toolchain.Ldflags())
Colin Cross3f40fa42015-01-30 17:27:36 -0800605 }
Colin Cross28344522015-04-22 13:07:53 -0700606
607 if ctx.Host() {
Colin Crossfa138792015-04-24 17:31:52 -0700608 flags.LdFlags = append(flags.LdFlags, c.Properties.Host_ldlibs...)
Colin Cross28344522015-04-22 13:07:53 -0700609 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800610 }
611
Colin Crossc4bde762015-11-23 16:11:30 -0800612 if flags.Clang {
613 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainClangCflags())
614 } else {
615 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainCflags())
Colin Crossc4bde762015-11-23 16:11:30 -0800616 }
Dan Willemsen6dd06602016-01-12 21:51:11 -0800617 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainLdflags())
Colin Crossc4bde762015-11-23 16:11:30 -0800618
Colin Cross0676e2d2015-04-24 17:39:18 -0700619 flags = c.ccModuleType().flags(ctx, flags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800620
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700621 if c.Properties.Sdk_version == "" {
622 if ctx.Host() && !flags.Clang {
623 // The host GCC doesn't support C++14 (and is deprecated, so likely
624 // never will). Build these modules with C++11.
625 flags.CppFlags = append(flags.CppFlags, "-std=gnu++11")
626 } else {
627 flags.CppFlags = append(flags.CppFlags, "-std=gnu++14")
628 }
629 }
630
631 flags.CFlags, _ = filterList(flags.CFlags, illegalFlags)
632 flags.CppFlags, _ = filterList(flags.CppFlags, illegalFlags)
633 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, illegalFlags)
634
Colin Cross3f40fa42015-01-30 17:27:36 -0800635 // Optimization to reduce size of build.ninja
636 // Replace the long list of flags for each file with a module-local variable
Colin Cross97ba0732015-03-23 17:50:24 -0700637 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
638 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
639 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
640 flags.CFlags = []string{"$cflags"}
641 flags.CppFlags = []string{"$cppflags"}
642 flags.AsFlags = []string{"$asflags"}
Colin Cross3f40fa42015-01-30 17:27:36 -0800643
644 return flags
645}
646
Colin Cross0676e2d2015-04-24 17:39:18 -0700647func (c *CCBase) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Cross3f40fa42015-01-30 17:27:36 -0800648 return flags
649}
650
651// Compile a list of source files into objects a specified subdirectory
Colin Crossfa138792015-04-24 17:31:52 -0700652func (c *CCBase) customCompileObjs(ctx common.AndroidModuleContext, flags CCFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700653 subdir string, srcFiles, excludes []string) common.Paths {
Colin Cross581c1892015-04-07 16:50:10 -0700654
655 buildFlags := ccFlagsToBuilderFlags(flags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800656
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700657 inputFiles := ctx.ExpandSources(srcFiles, excludes)
658 srcPaths, deps := genSources(ctx, inputFiles, buildFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800659
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700660 return TransformSourceToObj(ctx, subdir, srcPaths, buildFlags, deps)
Colin Cross3f40fa42015-01-30 17:27:36 -0800661}
662
Colin Crossfa138792015-04-24 17:31:52 -0700663// Compile files listed in c.Properties.Srcs into objects
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700664func (c *CCBase) compileObjs(ctx common.AndroidModuleContext, flags CCFlags) common.Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800665
Colin Crossfa138792015-04-24 17:31:52 -0700666 if c.Properties.SkipCompileObjs {
Colin Cross3f40fa42015-01-30 17:27:36 -0800667 return nil
668 }
669
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700670 return c.customCompileObjs(ctx, flags, "", c.Properties.Srcs, c.Properties.Exclude_srcs)
Colin Cross3f40fa42015-01-30 17:27:36 -0800671}
672
Colin Cross5049f022015-03-18 13:28:46 -0700673// Compile generated source files from dependencies
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700674func (c *CCBase) compileGeneratedObjs(ctx common.AndroidModuleContext, flags CCFlags) common.Paths {
675 var srcs common.Paths
Colin Cross5049f022015-03-18 13:28:46 -0700676
Colin Crossfa138792015-04-24 17:31:52 -0700677 if c.Properties.SkipCompileObjs {
Colin Cross5049f022015-03-18 13:28:46 -0700678 return nil
679 }
680
681 ctx.VisitDirectDeps(func(module blueprint.Module) {
682 if gen, ok := module.(genrule.SourceFileGenerator); ok {
683 srcs = append(srcs, gen.GeneratedSourceFiles()...)
684 }
685 })
686
687 if len(srcs) == 0 {
688 return nil
689 }
690
Colin Cross581c1892015-04-07 16:50:10 -0700691 return TransformSourceToObj(ctx, "", srcs, ccFlagsToBuilderFlags(flags), nil)
Colin Cross5049f022015-03-18 13:28:46 -0700692}
693
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700694func (c *CCBase) outputFile() common.OptionalPath {
695 return common.OptionalPath{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800696}
697
Colin Crossfa138792015-04-24 17:31:52 -0700698func (c *CCBase) depsToPathsFromList(ctx common.AndroidModuleContext,
Colin Cross3f40fa42015-01-30 17:27:36 -0800699 names []string) (modules []common.AndroidModule,
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700700 outputFiles common.Paths, exportedFlags []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800701
702 for _, n := range names {
703 found := false
704 ctx.VisitDirectDeps(func(m blueprint.Module) {
705 otherName := ctx.OtherModuleName(m)
706 if otherName != n {
707 return
708 }
709
Colin Cross97ba0732015-03-23 17:50:24 -0700710 if a, ok := m.(CCModuleType); ok {
Dan Willemsen0effe062015-11-30 16:06:01 -0800711 if !a.Enabled() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800712 // If a cc_library host+device module depends on a library that exists as both
713 // cc_library_shared and cc_library_host_shared, it will end up with two
714 // dependencies with the same name, one of which is marked disabled for each
715 // of host and device. Ignore the disabled one.
716 return
717 }
Colin Crossd3ba0392015-05-07 14:11:29 -0700718 if a.HostOrDevice() != ctx.HostOrDevice() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800719 ctx.ModuleErrorf("host/device mismatch between %q and %q", ctx.ModuleName(),
720 otherName)
721 return
722 }
723
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700724 if outputFile := a.outputFile(); outputFile.Valid() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800725 if found {
726 ctx.ModuleErrorf("multiple modules satisified dependency on %q", otherName)
727 return
728 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700729 outputFiles = append(outputFiles, outputFile.Path())
Colin Cross3f40fa42015-01-30 17:27:36 -0800730 modules = append(modules, a)
Colin Cross28344522015-04-22 13:07:53 -0700731 if i, ok := a.(ccExportedFlagsProducer); ok {
732 exportedFlags = append(exportedFlags, i.exportedFlags()...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800733 }
734 found = true
735 } else {
736 ctx.ModuleErrorf("module %q missing output file", otherName)
737 return
738 }
739 } else {
740 ctx.ModuleErrorf("module %q not an android module", otherName)
741 return
742 }
743 })
Colin Cross6ff51382015-12-17 16:39:19 -0800744 if !found && !inList(n, ctx.GetMissingDependencies()) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800745 ctx.ModuleErrorf("unsatisified dependency on %q", n)
746 }
747 }
748
Colin Cross28344522015-04-22 13:07:53 -0700749 return modules, outputFiles, exportedFlags
Colin Cross3f40fa42015-01-30 17:27:36 -0800750}
751
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700752// Convert dependency names to paths. Takes a CCDeps containing names and returns a CCPathDeps
Colin Cross21b9a242015-03-24 14:15:58 -0700753// containing paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700754func (c *CCBase) depsToPaths(ctx common.AndroidModuleContext, depNames CCDeps) CCPathDeps {
755 var depPaths CCPathDeps
Colin Cross28344522015-04-22 13:07:53 -0700756 var newCflags []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800757
Colin Cross21b9a242015-03-24 14:15:58 -0700758 var wholeStaticLibModules []common.AndroidModule
Colin Cross3f40fa42015-01-30 17:27:36 -0800759
Colin Cross28344522015-04-22 13:07:53 -0700760 wholeStaticLibModules, depPaths.WholeStaticLibs, newCflags =
Colin Cross21b9a242015-03-24 14:15:58 -0700761 c.depsToPathsFromList(ctx, depNames.WholeStaticLibs)
Colin Cross28344522015-04-22 13:07:53 -0700762 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Crossa48f71f2015-11-16 18:00:41 -0800763 depPaths.ReexportedCflags = append(depPaths.ReexportedCflags, newCflags...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800764
Colin Cross21b9a242015-03-24 14:15:58 -0700765 for _, m := range wholeStaticLibModules {
766 if staticLib, ok := m.(ccLibraryInterface); ok && staticLib.static() {
767 depPaths.WholeStaticLibObjFiles =
768 append(depPaths.WholeStaticLibObjFiles, staticLib.allObjFiles()...)
769 } else {
770 ctx.ModuleErrorf("module %q not a static library", ctx.OtherModuleName(m))
771 }
772 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800773
Colin Cross28344522015-04-22 13:07:53 -0700774 _, depPaths.StaticLibs, newCflags = c.depsToPathsFromList(ctx, depNames.StaticLibs)
775 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross21b9a242015-03-24 14:15:58 -0700776
Colin Cross28344522015-04-22 13:07:53 -0700777 _, depPaths.LateStaticLibs, newCflags = c.depsToPathsFromList(ctx, depNames.LateStaticLibs)
778 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross21b9a242015-03-24 14:15:58 -0700779
Colin Cross28344522015-04-22 13:07:53 -0700780 _, depPaths.SharedLibs, newCflags = c.depsToPathsFromList(ctx, depNames.SharedLibs)
781 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross21b9a242015-03-24 14:15:58 -0700782
783 ctx.VisitDirectDeps(func(m blueprint.Module) {
Dan Albertc3144b12015-04-28 18:17:56 -0700784 if obj, ok := m.(ccObjectProvider); ok {
Colin Cross21b9a242015-03-24 14:15:58 -0700785 otherName := ctx.OtherModuleName(m)
786 if otherName == depNames.CrtBegin {
Colin Cross06a931b2015-10-28 17:23:31 -0700787 if !Bool(c.Properties.Nocrt) {
Dan Albertc3144b12015-04-28 18:17:56 -0700788 depPaths.CrtBegin = obj.object().outputFile()
Colin Cross21b9a242015-03-24 14:15:58 -0700789 }
790 } else if otherName == depNames.CrtEnd {
Colin Cross06a931b2015-10-28 17:23:31 -0700791 if !Bool(c.Properties.Nocrt) {
Dan Albertc3144b12015-04-28 18:17:56 -0700792 depPaths.CrtEnd = obj.object().outputFile()
Colin Cross21b9a242015-03-24 14:15:58 -0700793 }
794 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700795 output := obj.object().outputFile()
796 if output.Valid() {
797 depPaths.ObjFiles = append(depPaths.ObjFiles, output.Path())
798 } else {
799 ctx.ModuleErrorf("module %s did not provide an output file", otherName)
800 }
Colin Cross21b9a242015-03-24 14:15:58 -0700801 }
802 }
803 })
804
805 return depPaths
Colin Cross3f40fa42015-01-30 17:27:36 -0800806}
807
Colin Cross7d5136f2015-05-11 13:39:40 -0700808type ccLinkedProperties struct {
809 VariantIsShared bool `blueprint:"mutated"`
810 VariantIsStatic bool `blueprint:"mutated"`
811 VariantIsStaticBinary bool `blueprint:"mutated"`
812}
813
Colin Crossfa138792015-04-24 17:31:52 -0700814// CCLinked contains the properties and members used by libraries and executables
815type CCLinked struct {
816 CCBase
Colin Cross7d5136f2015-05-11 13:39:40 -0700817 dynamicProperties ccLinkedProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800818}
819
Colin Crossfa138792015-04-24 17:31:52 -0700820func newCCDynamic(dynamic *CCLinked, module CCModuleType, hod common.HostOrDeviceSupported,
Colin Crossc472d572015-03-17 15:06:21 -0700821 multilib common.Multilib, props ...interface{}) (blueprint.Module, []interface{}) {
822
Colin Crossed4cf0b2015-03-26 14:43:45 -0700823 props = append(props, &dynamic.dynamicProperties)
824
Colin Crossfa138792015-04-24 17:31:52 -0700825 return newCCBase(&dynamic.CCBase, module, hod, multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700826}
827
Colin Crossfa138792015-04-24 17:31:52 -0700828func (c *CCLinked) systemSharedLibs(ctx common.AndroidBaseContext) []string {
Colin Cross06a931b2015-10-28 17:23:31 -0700829 if c.Properties.System_shared_libs != nil {
Colin Crossfa138792015-04-24 17:31:52 -0700830 return c.Properties.System_shared_libs
831 } else if ctx.Device() && c.Properties.Sdk_version == "" {
Colin Cross577f6e42015-03-27 18:23:34 -0700832 return []string{"libc", "libm"}
Colin Cross28d76592015-03-26 16:14:04 -0700833 } else {
Colin Cross577f6e42015-03-27 18:23:34 -0700834 return nil
Colin Cross3f40fa42015-01-30 17:27:36 -0800835 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800836}
837
Colin Crossfa138792015-04-24 17:31:52 -0700838func (c *CCLinked) stl(ctx common.AndroidBaseContext) string {
839 if c.Properties.Sdk_version != "" && ctx.Device() {
840 switch c.Properties.Stl {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700841 case "":
842 return "ndk_system"
843 case "c++_shared", "c++_static",
844 "stlport_shared", "stlport_static",
845 "gnustl_static":
Colin Crossfa138792015-04-24 17:31:52 -0700846 return "ndk_lib" + c.Properties.Stl
Colin Crossed4cf0b2015-03-26 14:43:45 -0700847 default:
Colin Crossfa138792015-04-24 17:31:52 -0700848 ctx.ModuleErrorf("stl: %q is not a supported STL with sdk_version set", c.Properties.Stl)
Colin Crossed4cf0b2015-03-26 14:43:45 -0700849 return ""
850 }
851 }
852
Dan Willemsen490fd492015-11-24 17:53:15 -0800853 if ctx.HostType() == common.Windows {
854 switch c.Properties.Stl {
855 case "libc++", "libc++_static", "libstdc++", "":
856 // libc++ is not supported on mingw
857 return "libstdc++"
858 case "none":
859 return ""
860 default:
861 ctx.ModuleErrorf("stl: %q is not a supported STL", c.Properties.Stl)
862 return ""
Colin Crossed4cf0b2015-03-26 14:43:45 -0700863 }
Dan Willemsen490fd492015-11-24 17:53:15 -0800864 } else {
865 switch c.Properties.Stl {
866 case "libc++", "libc++_static",
867 "libstdc++":
868 return c.Properties.Stl
869 case "none":
870 return ""
871 case "":
872 if c.static() {
873 return "libc++_static"
874 } else {
875 return "libc++"
876 }
877 default:
878 ctx.ModuleErrorf("stl: %q is not a supported STL", c.Properties.Stl)
879 return ""
880 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700881 }
882}
883
Dan Willemsen490fd492015-11-24 17:53:15 -0800884var hostDynamicGccLibs, hostStaticGccLibs map[common.HostType][]string
Colin Cross0af4b842015-04-30 16:36:18 -0700885
886func init() {
Dan Willemsen490fd492015-11-24 17:53:15 -0800887 hostDynamicGccLibs = map[common.HostType][]string{
888 common.Linux: []string{"-lgcc_s", "-lgcc", "-lc", "-lgcc_s", "-lgcc"},
889 common.Darwin: []string{"-lc", "-lSystem"},
890 common.Windows: []string{"-lmsvcr110", "-lmingw32", "-lgcc", "-lmoldname",
891 "-lmingwex", "-lmsvcrt", "-ladvapi32", "-lshell32", "-luser32",
892 "-lkernel32", "-lmingw32", "-lgcc", "-lmoldname", "-lmingwex",
893 "-lmsvcrt"},
894 }
895 hostStaticGccLibs = map[common.HostType][]string{
896 common.Linux: []string{"-Wl,--start-group", "-lgcc", "-lgcc_eh", "-lc", "-Wl,--end-group"},
897 common.Darwin: []string{"NO_STATIC_HOST_BINARIES_ON_DARWIN"},
898 common.Windows: []string{"NO_STATIC_HOST_BINARIES_ON_WINDOWS"},
Colin Cross0af4b842015-04-30 16:36:18 -0700899 }
900}
Colin Cross712fc022015-04-27 11:13:34 -0700901
Colin Crosse11befc2015-04-27 17:49:17 -0700902func (c *CCLinked) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700903 stl := c.stl(ctx)
904 if ctx.Failed() {
905 return flags
906 }
907
908 switch stl {
909 case "libc++", "libc++_static":
910 flags.CFlags = append(flags.CFlags, "-D_USING_LIBCXX")
Colin Crossed4cf0b2015-03-26 14:43:45 -0700911 if ctx.Host() {
912 flags.CppFlags = append(flags.CppFlags, "-nostdinc++")
913 flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs")
Colin Cross712fc022015-04-27 11:13:34 -0700914 flags.LdFlags = append(flags.LdFlags, "-lm", "-lpthread")
Colin Cross18b6dc52015-04-28 13:20:37 -0700915 if c.staticBinary() {
Dan Willemsen490fd492015-11-24 17:53:15 -0800916 flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs[ctx.HostType()]...)
Colin Cross18b6dc52015-04-28 13:20:37 -0700917 } else {
Dan Willemsen490fd492015-11-24 17:53:15 -0800918 flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs[ctx.HostType()]...)
Colin Cross712fc022015-04-27 11:13:34 -0700919 }
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700920 } else {
921 if ctx.Arch().ArchType == common.Arm {
922 flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,libunwind_llvm.a")
923 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700924 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700925 case "libstdc++":
926 // Using bionic's basic libstdc++. Not actually an STL. Only around until the
927 // tree is in good enough shape to not need it.
928 // Host builds will use GNU libstdc++.
929 if ctx.Device() {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700930 flags.CFlags = append(flags.CFlags, "-I"+common.PathForSource(ctx, "bionic/libstdc++/include").String())
Colin Crossed4cf0b2015-03-26 14:43:45 -0700931 }
932 case "ndk_system":
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700933 ndkSrcRoot := common.PathForSource(ctx, "prebuilts/ndk/current/sources/cxx-stl/system/include")
934 flags.CFlags = append(flags.CFlags, "-isystem "+ndkSrcRoot.String())
Colin Crossed4cf0b2015-03-26 14:43:45 -0700935 case "ndk_libc++_shared", "ndk_libc++_static":
936 // TODO(danalbert): This really shouldn't be here...
937 flags.CppFlags = append(flags.CppFlags, "-std=c++11")
938 case "ndk_libstlport_shared", "ndk_libstlport_static", "ndk_libgnustl_static":
939 // Nothing
940 case "":
941 // None or error.
942 if ctx.Host() {
943 flags.CppFlags = append(flags.CppFlags, "-nostdinc++")
944 flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs")
Colin Cross18b6dc52015-04-28 13:20:37 -0700945 if c.staticBinary() {
Dan Willemsen490fd492015-11-24 17:53:15 -0800946 flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs[ctx.HostType()]...)
Colin Cross18b6dc52015-04-28 13:20:37 -0700947 } else {
Dan Willemsen490fd492015-11-24 17:53:15 -0800948 flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs[ctx.HostType()]...)
Colin Cross712fc022015-04-27 11:13:34 -0700949 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700950 }
951 default:
Colin Crossfa138792015-04-24 17:31:52 -0700952 panic(fmt.Errorf("Unknown stl in CCLinked.Flags: %q", stl))
Colin Crossed4cf0b2015-03-26 14:43:45 -0700953 }
954
955 return flags
956}
957
Colin Crosse11befc2015-04-27 17:49:17 -0700958func (c *CCLinked) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
959 depNames = c.CCBase.depNames(ctx, depNames)
Colin Cross3f40fa42015-01-30 17:27:36 -0800960
Colin Crossed4cf0b2015-03-26 14:43:45 -0700961 stl := c.stl(ctx)
962 if ctx.Failed() {
963 return depNames
964 }
965
966 switch stl {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700967 case "libstdc++":
968 if ctx.Device() {
969 depNames.SharedLibs = append(depNames.SharedLibs, stl)
970 }
Colin Cross74d1ec02015-04-28 13:30:13 -0700971 case "libc++", "libc++_static":
972 if stl == "libc++" {
973 depNames.SharedLibs = append(depNames.SharedLibs, stl)
974 } else {
975 depNames.StaticLibs = append(depNames.StaticLibs, stl)
976 }
977 if ctx.Device() {
978 if ctx.Arch().ArchType == common.Arm {
979 depNames.StaticLibs = append(depNames.StaticLibs, "libunwind_llvm")
980 }
981 if c.staticBinary() {
982 depNames.StaticLibs = append(depNames.StaticLibs, "libdl")
983 } else {
984 depNames.SharedLibs = append(depNames.SharedLibs, "libdl")
985 }
986 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700987 case "":
988 // None or error.
989 case "ndk_system":
990 // TODO: Make a system STL prebuilt for the NDK.
991 // 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 -0700992 // its own includes. The includes are handled in CCBase.Flags().
Colin Cross577f6e42015-03-27 18:23:34 -0700993 depNames.SharedLibs = append([]string{"libstdc++"}, depNames.SharedLibs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -0700994 case "ndk_libc++_shared", "ndk_libstlport_shared":
995 depNames.SharedLibs = append(depNames.SharedLibs, stl)
996 case "ndk_libc++_static", "ndk_libstlport_static", "ndk_libgnustl_static":
997 depNames.StaticLibs = append(depNames.StaticLibs, stl)
998 default:
Colin Crosse11befc2015-04-27 17:49:17 -0700999 panic(fmt.Errorf("Unknown stl in CCLinked.depNames: %q", stl))
Colin Crossed4cf0b2015-03-26 14:43:45 -07001000 }
1001
Colin Cross74d1ec02015-04-28 13:30:13 -07001002 if ctx.ModuleName() != "libcompiler_rt-extras" {
1003 depNames.StaticLibs = append(depNames.StaticLibs, "libcompiler_rt-extras")
1004 }
1005
Colin Crossf6566ed2015-03-24 11:13:38 -07001006 if ctx.Device() {
Colin Cross77b00fa2015-03-16 16:15:49 -07001007 // libgcc and libatomic have to be last on the command line
Dan Willemsen415cb0f2016-01-12 21:40:17 -08001008 depNames.LateStaticLibs = append(depNames.LateStaticLibs, "libatomic")
Colin Cross06a931b2015-10-28 17:23:31 -07001009 if !Bool(c.Properties.No_libgcc) {
Dan Willemsend67be222015-09-16 15:19:33 -07001010 depNames.LateStaticLibs = append(depNames.LateStaticLibs, "libgcc")
1011 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001012
Colin Cross18b6dc52015-04-28 13:20:37 -07001013 if !c.static() {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001014 depNames.SharedLibs = append(depNames.SharedLibs, c.systemSharedLibs(ctx)...)
1015 }
Colin Cross577f6e42015-03-27 18:23:34 -07001016
Colin Crossfa138792015-04-24 17:31:52 -07001017 if c.Properties.Sdk_version != "" {
1018 version := c.Properties.Sdk_version
Colin Cross577f6e42015-03-27 18:23:34 -07001019 depNames.SharedLibs = append(depNames.SharedLibs,
1020 "ndk_libc."+version,
1021 "ndk_libm."+version,
1022 )
1023 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001024 }
1025
Colin Cross21b9a242015-03-24 14:15:58 -07001026 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -08001027}
1028
Colin Crossed4cf0b2015-03-26 14:43:45 -07001029// ccLinkedInterface interface is used on ccLinked to deal with static or shared variants
1030type ccLinkedInterface interface {
1031 // Returns true if the build options for the module have selected a static or shared build
1032 buildStatic() bool
1033 buildShared() bool
1034
1035 // Sets whether a specific variant is static or shared
Colin Cross18b6dc52015-04-28 13:20:37 -07001036 setStatic(bool)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001037
Colin Cross18b6dc52015-04-28 13:20:37 -07001038 // Returns whether a specific variant is a static library or binary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001039 static() bool
Colin Cross18b6dc52015-04-28 13:20:37 -07001040
1041 // Returns whether a module is a static binary
1042 staticBinary() bool
Colin Crossed4cf0b2015-03-26 14:43:45 -07001043}
1044
1045var _ ccLinkedInterface = (*CCLibrary)(nil)
1046var _ ccLinkedInterface = (*CCBinary)(nil)
1047
Colin Crossfa138792015-04-24 17:31:52 -07001048func (c *CCLinked) static() bool {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001049 return c.dynamicProperties.VariantIsStatic
1050}
1051
Colin Cross18b6dc52015-04-28 13:20:37 -07001052func (c *CCLinked) staticBinary() bool {
1053 return c.dynamicProperties.VariantIsStaticBinary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001054}
1055
Colin Cross18b6dc52015-04-28 13:20:37 -07001056func (c *CCLinked) setStatic(static bool) {
1057 c.dynamicProperties.VariantIsStatic = static
Colin Crossed4cf0b2015-03-26 14:43:45 -07001058}
1059
Colin Cross28344522015-04-22 13:07:53 -07001060type ccExportedFlagsProducer interface {
1061 exportedFlags() []string
Colin Cross3f40fa42015-01-30 17:27:36 -08001062}
1063
1064//
1065// Combined static+shared libraries
1066//
1067
Colin Cross7d5136f2015-05-11 13:39:40 -07001068type CCLibraryProperties struct {
1069 BuildStatic bool `blueprint:"mutated"`
1070 BuildShared bool `blueprint:"mutated"`
1071 Static struct {
1072 Srcs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001073 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -07001074 Cflags []string `android:"arch_variant"`
1075 Whole_static_libs []string `android:"arch_variant"`
1076 Static_libs []string `android:"arch_variant"`
1077 Shared_libs []string `android:"arch_variant"`
1078 } `android:"arch_variant"`
1079 Shared struct {
1080 Srcs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001081 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -07001082 Cflags []string `android:"arch_variant"`
1083 Whole_static_libs []string `android:"arch_variant"`
1084 Static_libs []string `android:"arch_variant"`
1085 Shared_libs []string `android:"arch_variant"`
1086 } `android:"arch_variant"`
Colin Crossaee540a2015-07-06 17:48:31 -07001087
1088 // local file name to pass to the linker as --version_script
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001089 Version_script *string `android:"arch_variant"`
Dan Willemsen93c28312015-12-04 14:59:08 -08001090 // local file name to pass to the linker as -unexported_symbols_list
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001091 Unexported_symbols_list *string `android:"arch_variant"`
Dan Willemsen93c28312015-12-04 14:59:08 -08001092 // local file name to pass to the linker as -force_symbols_not_weak_list
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001093 Force_symbols_not_weak_list *string `android:"arch_variant"`
Dan Willemsen93c28312015-12-04 14:59:08 -08001094 // local file name to pass to the linker as -force_symbols_weak_list
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001095 Force_symbols_weak_list *string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -07001096}
1097
Colin Cross97ba0732015-03-23 17:50:24 -07001098type CCLibrary struct {
Colin Crossfa138792015-04-24 17:31:52 -07001099 CCLinked
Colin Cross3f40fa42015-01-30 17:27:36 -08001100
Colin Cross28344522015-04-22 13:07:53 -07001101 reuseFrom ccLibraryInterface
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001102 reuseObjFiles common.Paths
1103 objFiles common.Paths
Colin Cross28344522015-04-22 13:07:53 -07001104 exportFlags []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001105 out common.Path
Dan Willemsen218f6562015-07-08 18:13:11 -07001106 systemLibs []string
Colin Cross3f40fa42015-01-30 17:27:36 -08001107
Colin Cross7d5136f2015-05-11 13:39:40 -07001108 LibraryProperties CCLibraryProperties
Colin Cross3f40fa42015-01-30 17:27:36 -08001109}
1110
Colin Crossed4cf0b2015-03-26 14:43:45 -07001111func (c *CCLibrary) buildStatic() bool {
1112 return c.LibraryProperties.BuildStatic
1113}
1114
1115func (c *CCLibrary) buildShared() bool {
1116 return c.LibraryProperties.BuildShared
1117}
1118
Colin Cross97ba0732015-03-23 17:50:24 -07001119type ccLibraryInterface interface {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001120 ccLinkedInterface
Colin Cross97ba0732015-03-23 17:50:24 -07001121 ccLibrary() *CCLibrary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001122 setReuseFrom(ccLibraryInterface)
1123 getReuseFrom() ccLibraryInterface
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001124 getReuseObjFiles() common.Paths
1125 allObjFiles() common.Paths
Colin Crossc472d572015-03-17 15:06:21 -07001126}
1127
Colin Crossed4cf0b2015-03-26 14:43:45 -07001128var _ ccLibraryInterface = (*CCLibrary)(nil)
1129
Colin Cross97ba0732015-03-23 17:50:24 -07001130func (c *CCLibrary) ccLibrary() *CCLibrary {
1131 return c
Colin Cross3f40fa42015-01-30 17:27:36 -08001132}
1133
Colin Cross97ba0732015-03-23 17:50:24 -07001134func NewCCLibrary(library *CCLibrary, module CCModuleType,
1135 hod common.HostOrDeviceSupported) (blueprint.Module, []interface{}) {
1136
Colin Crossfa138792015-04-24 17:31:52 -07001137 return newCCDynamic(&library.CCLinked, module, hod, common.MultilibBoth,
Colin Cross97ba0732015-03-23 17:50:24 -07001138 &library.LibraryProperties)
1139}
1140
1141func CCLibraryFactory() (blueprint.Module, []interface{}) {
1142 module := &CCLibrary{}
1143
1144 module.LibraryProperties.BuildShared = true
1145 module.LibraryProperties.BuildStatic = true
1146
1147 return NewCCLibrary(module, module, common.HostAndDeviceSupported)
1148}
1149
Colin Cross0676e2d2015-04-24 17:39:18 -07001150func (c *CCLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Crosse11befc2015-04-27 17:49:17 -07001151 depNames = c.CCLinked.depNames(ctx, depNames)
Colin Cross2732e9a2015-04-28 13:23:52 -07001152 if c.static() {
1153 depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.LibraryProperties.Static.Whole_static_libs...)
1154 depNames.StaticLibs = append(depNames.StaticLibs, c.LibraryProperties.Static.Static_libs...)
1155 depNames.SharedLibs = append(depNames.SharedLibs, c.LibraryProperties.Static.Shared_libs...)
1156 } else {
Colin Crossf6566ed2015-03-24 11:13:38 -07001157 if ctx.Device() {
Dan Albertc3144b12015-04-28 18:17:56 -07001158 if c.Properties.Sdk_version == "" {
1159 depNames.CrtBegin = "crtbegin_so"
1160 depNames.CrtEnd = "crtend_so"
1161 } else {
1162 depNames.CrtBegin = "ndk_crtbegin_so." + c.Properties.Sdk_version
1163 depNames.CrtEnd = "ndk_crtend_so." + c.Properties.Sdk_version
1164 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001165 }
Colin Cross2732e9a2015-04-28 13:23:52 -07001166 depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.LibraryProperties.Shared.Whole_static_libs...)
1167 depNames.StaticLibs = append(depNames.StaticLibs, c.LibraryProperties.Shared.Static_libs...)
1168 depNames.SharedLibs = append(depNames.SharedLibs, c.LibraryProperties.Shared.Shared_libs...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001169 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001170
Dan Willemsen218f6562015-07-08 18:13:11 -07001171 c.systemLibs = c.systemSharedLibs(ctx)
1172
Colin Cross21b9a242015-03-24 14:15:58 -07001173 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -08001174}
1175
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001176func (c *CCLibrary) outputFile() common.OptionalPath {
1177 return common.OptionalPathForPath(c.out)
Colin Cross3f40fa42015-01-30 17:27:36 -08001178}
1179
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001180func (c *CCLibrary) getReuseObjFiles() common.Paths {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001181 return c.reuseObjFiles
1182}
1183
1184func (c *CCLibrary) setReuseFrom(reuseFrom ccLibraryInterface) {
1185 c.reuseFrom = reuseFrom
1186}
1187
1188func (c *CCLibrary) getReuseFrom() ccLibraryInterface {
1189 return c.reuseFrom
1190}
1191
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001192func (c *CCLibrary) allObjFiles() common.Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -08001193 return c.objFiles
1194}
1195
Colin Cross28344522015-04-22 13:07:53 -07001196func (c *CCLibrary) exportedFlags() []string {
1197 return c.exportFlags
Colin Cross3f40fa42015-01-30 17:27:36 -08001198}
1199
Colin Cross0676e2d2015-04-24 17:39:18 -07001200func (c *CCLibrary) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Crosse11befc2015-04-27 17:49:17 -07001201 flags = c.CCLinked.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001202
Dan Willemsen490fd492015-11-24 17:53:15 -08001203 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
1204 // all code is position independent, and then those warnings get promoted to
1205 // errors.
1206 if ctx.HostType() != common.Windows {
1207 flags.CFlags = append(flags.CFlags, "-fPIC")
1208 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001209
Colin Crossd8e780d2015-04-28 17:39:43 -07001210 if c.static() {
1211 flags.CFlags = append(flags.CFlags, c.LibraryProperties.Static.Cflags...)
1212 } else {
1213 flags.CFlags = append(flags.CFlags, c.LibraryProperties.Shared.Cflags...)
1214 }
1215
Colin Cross18b6dc52015-04-28 13:20:37 -07001216 if !c.static() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001217 libName := ctx.ModuleName()
1218 // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead
1219 sharedFlag := "-Wl,-shared"
Dan Willemsendd0e2c32015-10-20 14:29:35 -07001220 if flags.Clang || ctx.Host() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001221 sharedFlag = "-shared"
1222 }
Colin Crossf6566ed2015-03-24 11:13:38 -07001223 if ctx.Device() {
Colin Cross97ba0732015-03-23 17:50:24 -07001224 flags.LdFlags = append(flags.LdFlags, "-nostdlib")
Colin Cross3f40fa42015-01-30 17:27:36 -08001225 }
Colin Cross97ba0732015-03-23 17:50:24 -07001226
Colin Cross0af4b842015-04-30 16:36:18 -07001227 if ctx.Darwin() {
1228 flags.LdFlags = append(flags.LdFlags,
1229 "-dynamiclib",
1230 "-single_module",
1231 //"-read_only_relocs suppress",
Dan Willemsen490fd492015-11-24 17:53:15 -08001232 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
Colin Cross0af4b842015-04-30 16:36:18 -07001233 )
1234 } else {
1235 flags.LdFlags = append(flags.LdFlags,
1236 "-Wl,--gc-sections",
1237 sharedFlag,
Dan Willemsen490fd492015-11-24 17:53:15 -08001238 "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix(),
Colin Cross0af4b842015-04-30 16:36:18 -07001239 )
1240 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001241 }
Colin Cross97ba0732015-03-23 17:50:24 -07001242
1243 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001244}
1245
Colin Cross97ba0732015-03-23 17:50:24 -07001246func (c *CCLibrary) compileStaticLibrary(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001247 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001248
1249 staticFlags := flags
Colin Cross581c1892015-04-07 16:50:10 -07001250 objFilesStatic := c.customCompileObjs(ctx, staticFlags, common.DeviceStaticLibrary,
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001251 c.LibraryProperties.Static.Srcs, c.LibraryProperties.Static.Exclude_srcs)
Colin Cross3f40fa42015-01-30 17:27:36 -08001252
1253 objFiles = append(objFiles, objFilesStatic...)
Colin Cross21b9a242015-03-24 14:15:58 -07001254 objFiles = append(objFiles, deps.WholeStaticLibObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001255
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001256 outputFile := common.PathForModuleOut(ctx, ctx.ModuleName()+staticLibraryExtension)
Colin Cross3f40fa42015-01-30 17:27:36 -08001257
Colin Cross0af4b842015-04-30 16:36:18 -07001258 if ctx.Darwin() {
1259 TransformDarwinObjToStaticLib(ctx, objFiles, ccFlagsToBuilderFlags(flags), outputFile)
1260 } else {
1261 TransformObjToStaticLib(ctx, objFiles, ccFlagsToBuilderFlags(flags), outputFile)
1262 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001263
1264 c.objFiles = objFiles
1265 c.out = outputFile
Colin Crossf2298272015-05-12 11:36:53 -07001266
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001267 includeDirs := common.PathsForModuleSrc(ctx, c.Properties.Export_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -07001268 c.exportFlags = []string{includeDirsToFlags(includeDirs)}
Colin Crossa48f71f2015-11-16 18:00:41 -08001269 c.exportFlags = append(c.exportFlags, deps.ReexportedCflags...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001270
1271 ctx.CheckbuildFile(outputFile)
1272}
1273
Colin Cross97ba0732015-03-23 17:50:24 -07001274func (c *CCLibrary) compileSharedLibrary(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001275 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001276
1277 sharedFlags := flags
Colin Cross581c1892015-04-07 16:50:10 -07001278 objFilesShared := c.customCompileObjs(ctx, sharedFlags, common.DeviceSharedLibrary,
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001279 c.LibraryProperties.Shared.Srcs, c.LibraryProperties.Shared.Exclude_srcs)
Colin Cross3f40fa42015-01-30 17:27:36 -08001280
1281 objFiles = append(objFiles, objFilesShared...)
1282
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001283 outputFile := common.PathForModuleOut(ctx, ctx.ModuleName()+flags.Toolchain.ShlibSuffix())
Colin Cross3f40fa42015-01-30 17:27:36 -08001284
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001285 var linkerDeps common.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07001286
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001287 versionScript := common.OptionalPathForModuleSrc(ctx, c.LibraryProperties.Version_script)
1288 unexportedSymbols := common.OptionalPathForModuleSrc(ctx, c.LibraryProperties.Unexported_symbols_list)
1289 forceNotWeakSymbols := common.OptionalPathForModuleSrc(ctx, c.LibraryProperties.Force_symbols_not_weak_list)
1290 forceWeakSymbols := common.OptionalPathForModuleSrc(ctx, c.LibraryProperties.Force_symbols_weak_list)
Dan Willemsen93c28312015-12-04 14:59:08 -08001291 if !ctx.Darwin() {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001292 if versionScript.Valid() {
1293 sharedFlags.LdFlags = append(sharedFlags.LdFlags, "-Wl,--version-script,"+versionScript.String())
1294 linkerDeps = append(linkerDeps, versionScript.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001295 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001296 if unexportedSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001297 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
1298 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001299 if forceNotWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001300 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
1301 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001302 if forceWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001303 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
1304 }
1305 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001306 if versionScript.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001307 ctx.PropertyErrorf("version_script", "Not supported on Darwin")
1308 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001309 if unexportedSymbols.Valid() {
1310 sharedFlags.LdFlags = append(sharedFlags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
1311 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001312 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001313 if forceNotWeakSymbols.Valid() {
1314 sharedFlags.LdFlags = append(sharedFlags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
1315 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001316 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001317 if forceWeakSymbols.Valid() {
1318 sharedFlags.LdFlags = append(sharedFlags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
1319 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001320 }
Colin Crossaee540a2015-07-06 17:48:31 -07001321 }
1322
Colin Cross97ba0732015-03-23 17:50:24 -07001323 TransformObjToDynamicBinary(ctx, objFiles, deps.SharedLibs, deps.StaticLibs,
Colin Crossaee540a2015-07-06 17:48:31 -07001324 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, false,
Dan Willemsen6203ac02015-11-24 12:58:57 -08001325 ccFlagsToBuilderFlags(sharedFlags), outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08001326
1327 c.out = outputFile
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001328 includeDirs := common.PathsForModuleSrc(ctx, c.Properties.Export_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -07001329 c.exportFlags = []string{includeDirsToFlags(includeDirs)}
Colin Crossa48f71f2015-11-16 18:00:41 -08001330 c.exportFlags = append(c.exportFlags, deps.ReexportedCflags...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001331}
1332
Colin Cross97ba0732015-03-23 17:50:24 -07001333func (c *CCLibrary) compileModule(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001334 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001335
1336 // Reuse the object files from the matching static library if it exists
Colin Crossed4cf0b2015-03-26 14:43:45 -07001337 if c.getReuseFrom().ccLibrary() == c {
1338 c.reuseObjFiles = objFiles
Colin Cross3f40fa42015-01-30 17:27:36 -08001339 } else {
Colin Cross2732e9a2015-04-28 13:23:52 -07001340 if c.getReuseFrom().ccLibrary().LibraryProperties.Static.Cflags == nil &&
1341 c.LibraryProperties.Shared.Cflags == nil {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001342 objFiles = append(common.Paths(nil), c.getReuseFrom().getReuseObjFiles()...)
Colin Cross2732e9a2015-04-28 13:23:52 -07001343 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001344 }
1345
Colin Crossed4cf0b2015-03-26 14:43:45 -07001346 if c.static() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001347 c.compileStaticLibrary(ctx, flags, deps, objFiles)
1348 } else {
1349 c.compileSharedLibrary(ctx, flags, deps, objFiles)
1350 }
1351}
1352
Colin Cross97ba0732015-03-23 17:50:24 -07001353func (c *CCLibrary) installStaticLibrary(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001354 // Static libraries do not get installed.
1355}
1356
Colin Cross97ba0732015-03-23 17:50:24 -07001357func (c *CCLibrary) installSharedLibrary(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001358 installDir := "lib"
Colin Cross97ba0732015-03-23 17:50:24 -07001359 if flags.Toolchain.Is64Bit() {
Dan Albertc403f7c2015-03-18 14:01:18 -07001360 installDir = "lib64"
1361 }
1362
Dan Willemsen782a2d12015-12-21 14:55:28 -08001363 ctx.InstallFile(common.PathForModuleInstall(ctx, installDir, c.Properties.Relative_install_path), c.out)
Dan Albertc403f7c2015-03-18 14:01:18 -07001364}
1365
Colin Cross97ba0732015-03-23 17:50:24 -07001366func (c *CCLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001367 if c.static() {
Dan Albertc403f7c2015-03-18 14:01:18 -07001368 c.installStaticLibrary(ctx, flags)
1369 } else {
1370 c.installSharedLibrary(ctx, flags)
1371 }
1372}
1373
Colin Cross3f40fa42015-01-30 17:27:36 -08001374//
1375// Objects (for crt*.o)
1376//
1377
Dan Albertc3144b12015-04-28 18:17:56 -07001378type ccObjectProvider interface {
1379 object() *ccObject
1380}
1381
Colin Cross3f40fa42015-01-30 17:27:36 -08001382type ccObject struct {
Colin Crossfa138792015-04-24 17:31:52 -07001383 CCBase
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001384 out common.OptionalPath
Colin Cross3f40fa42015-01-30 17:27:36 -08001385}
1386
Dan Albertc3144b12015-04-28 18:17:56 -07001387func (c *ccObject) object() *ccObject {
1388 return c
1389}
1390
Colin Cross97ba0732015-03-23 17:50:24 -07001391func CCObjectFactory() (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001392 module := &ccObject{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001393
Colin Crossfa138792015-04-24 17:31:52 -07001394 return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth)
Colin Cross3f40fa42015-01-30 17:27:36 -08001395}
1396
Colin Cross0676e2d2015-04-24 17:39:18 -07001397func (*ccObject) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Cross21b9a242015-03-24 14:15:58 -07001398 // object files can't have any dynamic dependencies
1399 return CCDeps{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001400}
1401
1402func (c *ccObject) compileModule(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001403 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001404
Colin Cross97ba0732015-03-23 17:50:24 -07001405 objFiles = append(objFiles, deps.ObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001406
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001407 var outputFile common.Path
Colin Cross3f40fa42015-01-30 17:27:36 -08001408 if len(objFiles) == 1 {
1409 outputFile = objFiles[0]
1410 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001411 output := common.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension)
1412 TransformObjsToObj(ctx, objFiles, ccFlagsToBuilderFlags(flags), output)
1413 outputFile = output
Colin Cross3f40fa42015-01-30 17:27:36 -08001414 }
1415
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001416 c.out = common.OptionalPathForPath(outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08001417
1418 ctx.CheckbuildFile(outputFile)
1419}
1420
Colin Cross97ba0732015-03-23 17:50:24 -07001421func (c *ccObject) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001422 // Object files do not get installed.
1423}
1424
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001425func (c *ccObject) outputFile() common.OptionalPath {
Colin Cross3f40fa42015-01-30 17:27:36 -08001426 return c.out
1427}
1428
Dan Albertc3144b12015-04-28 18:17:56 -07001429var _ ccObjectProvider = (*ccObject)(nil)
1430
Colin Cross3f40fa42015-01-30 17:27:36 -08001431//
1432// Executables
1433//
1434
Colin Cross7d5136f2015-05-11 13:39:40 -07001435type CCBinaryProperties struct {
1436 // compile executable with -static
Colin Cross06a931b2015-10-28 17:23:31 -07001437 Static_executable *bool
Colin Cross7d5136f2015-05-11 13:39:40 -07001438
1439 // set the name of the output
1440 Stem string `android:"arch_variant"`
1441
1442 // append to the name of the output
1443 Suffix string `android:"arch_variant"`
1444
1445 // if set, add an extra objcopy --prefix-symbols= step
1446 Prefix_symbols string
1447}
1448
Colin Cross97ba0732015-03-23 17:50:24 -07001449type CCBinary struct {
Colin Crossfa138792015-04-24 17:31:52 -07001450 CCLinked
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001451 out common.Path
1452 installFile common.Path
Colin Cross7d5136f2015-05-11 13:39:40 -07001453 BinaryProperties CCBinaryProperties
Colin Cross3f40fa42015-01-30 17:27:36 -08001454}
1455
Colin Crossed4cf0b2015-03-26 14:43:45 -07001456func (c *CCBinary) buildStatic() bool {
Colin Cross06a931b2015-10-28 17:23:31 -07001457 return Bool(c.BinaryProperties.Static_executable)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001458}
1459
1460func (c *CCBinary) buildShared() bool {
Colin Cross06a931b2015-10-28 17:23:31 -07001461 return !Bool(c.BinaryProperties.Static_executable)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001462}
1463
Colin Cross97ba0732015-03-23 17:50:24 -07001464func (c *CCBinary) getStem(ctx common.AndroidModuleContext) string {
Colin Cross4ae185c2015-03-26 15:12:10 -07001465 stem := ctx.ModuleName()
Colin Cross97ba0732015-03-23 17:50:24 -07001466 if c.BinaryProperties.Stem != "" {
Colin Cross4ae185c2015-03-26 15:12:10 -07001467 stem = c.BinaryProperties.Stem
Colin Cross3f40fa42015-01-30 17:27:36 -08001468 }
Colin Cross4ae185c2015-03-26 15:12:10 -07001469
1470 return stem + c.BinaryProperties.Suffix
Colin Cross3f40fa42015-01-30 17:27:36 -08001471}
1472
Colin Cross0676e2d2015-04-24 17:39:18 -07001473func (c *CCBinary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Crosse11befc2015-04-27 17:49:17 -07001474 depNames = c.CCLinked.depNames(ctx, depNames)
Colin Crossf6566ed2015-03-24 11:13:38 -07001475 if ctx.Device() {
Dan Albertc3144b12015-04-28 18:17:56 -07001476 if c.Properties.Sdk_version == "" {
Colin Cross06a931b2015-10-28 17:23:31 -07001477 if Bool(c.BinaryProperties.Static_executable) {
Dan Albertc3144b12015-04-28 18:17:56 -07001478 depNames.CrtBegin = "crtbegin_static"
1479 } else {
1480 depNames.CrtBegin = "crtbegin_dynamic"
1481 }
1482 depNames.CrtEnd = "crtend_android"
Colin Cross3f40fa42015-01-30 17:27:36 -08001483 } else {
Colin Cross06a931b2015-10-28 17:23:31 -07001484 if Bool(c.BinaryProperties.Static_executable) {
Dan Albertc3144b12015-04-28 18:17:56 -07001485 depNames.CrtBegin = "ndk_crtbegin_static." + c.Properties.Sdk_version
1486 } else {
1487 depNames.CrtBegin = "ndk_crtbegin_dynamic." + c.Properties.Sdk_version
1488 }
1489 depNames.CrtEnd = "ndk_crtend_android." + c.Properties.Sdk_version
Colin Cross3f40fa42015-01-30 17:27:36 -08001490 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001491
Colin Cross06a931b2015-10-28 17:23:31 -07001492 if Bool(c.BinaryProperties.Static_executable) {
Colin Cross74d1ec02015-04-28 13:30:13 -07001493 if c.stl(ctx) == "libc++_static" {
1494 depNames.StaticLibs = append(depNames.StaticLibs, "libm", "libc", "libdl")
1495 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001496 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
1497 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
1498 // move them to the beginning of deps.LateStaticLibs
1499 var groupLibs []string
1500 depNames.StaticLibs, groupLibs = filterList(depNames.StaticLibs,
1501 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
1502 depNames.LateStaticLibs = append(groupLibs, depNames.LateStaticLibs...)
1503 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001504 }
Colin Cross21b9a242015-03-24 14:15:58 -07001505 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -08001506}
1507
Colin Cross97ba0732015-03-23 17:50:24 -07001508func NewCCBinary(binary *CCBinary, module CCModuleType,
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001509 hod common.HostOrDeviceSupported, multilib common.Multilib,
1510 props ...interface{}) (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001511
Colin Cross1f8f2342015-03-26 16:09:47 -07001512 props = append(props, &binary.BinaryProperties)
1513
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001514 return newCCDynamic(&binary.CCLinked, module, hod, multilib, props...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001515}
1516
Colin Cross97ba0732015-03-23 17:50:24 -07001517func CCBinaryFactory() (blueprint.Module, []interface{}) {
1518 module := &CCBinary{}
1519
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001520 return NewCCBinary(module, module, common.HostAndDeviceSupported, common.MultilibFirst)
Colin Cross3f40fa42015-01-30 17:27:36 -08001521}
1522
Colin Cross6362e272015-10-29 15:25:03 -07001523func (c *CCBinary) ModifyProperties(ctx CCModuleContext) {
Colin Cross0af4b842015-04-30 16:36:18 -07001524 if ctx.Darwin() {
Colin Cross06a931b2015-10-28 17:23:31 -07001525 c.BinaryProperties.Static_executable = proptools.BoolPtr(false)
Colin Cross0af4b842015-04-30 16:36:18 -07001526 }
Colin Cross06a931b2015-10-28 17:23:31 -07001527 if Bool(c.BinaryProperties.Static_executable) {
Colin Cross18b6dc52015-04-28 13:20:37 -07001528 c.dynamicProperties.VariantIsStaticBinary = true
1529 }
1530}
1531
Colin Cross0676e2d2015-04-24 17:39:18 -07001532func (c *CCBinary) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Crosse11befc2015-04-27 17:49:17 -07001533 flags = c.CCLinked.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001534
Dan Willemsen490fd492015-11-24 17:53:15 -08001535 if ctx.Host() {
1536 flags.LdFlags = append(flags.LdFlags, "-pie")
1537 if ctx.HostType() == common.Windows {
1538 flags.LdFlags = append(flags.LdFlags, "-Wl,-e_mainCRTStartup")
1539 }
1540 }
1541
1542 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
1543 // all code is position independent, and then those warnings get promoted to
1544 // errors.
1545 if ctx.HostType() != common.Windows {
1546 flags.CFlags = append(flags.CFlags, "-fpie")
1547 }
Colin Cross97ba0732015-03-23 17:50:24 -07001548
Colin Crossf6566ed2015-03-24 11:13:38 -07001549 if ctx.Device() {
Colin Cross06a931b2015-10-28 17:23:31 -07001550 if Bool(c.BinaryProperties.Static_executable) {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001551 // Clang driver needs -static to create static executable.
1552 // However, bionic/linker uses -shared to overwrite.
1553 // Linker for x86 targets does not allow coexistance of -static and -shared,
1554 // so we add -static only if -shared is not used.
1555 if !inList("-shared", flags.LdFlags) {
1556 flags.LdFlags = append(flags.LdFlags, "-static")
1557 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001558
Colin Crossed4cf0b2015-03-26 14:43:45 -07001559 flags.LdFlags = append(flags.LdFlags,
1560 "-nostdlib",
1561 "-Bstatic",
1562 "-Wl,--gc-sections",
1563 )
1564
1565 } else {
1566 linker := "/system/bin/linker"
1567 if flags.Toolchain.Is64Bit() {
1568 linker = "/system/bin/linker64"
1569 }
1570
1571 flags.LdFlags = append(flags.LdFlags,
Colin Cross979422c2015-12-01 14:09:48 -08001572 "-pie",
Colin Crossed4cf0b2015-03-26 14:43:45 -07001573 "-nostdlib",
1574 "-Bdynamic",
1575 fmt.Sprintf("-Wl,-dynamic-linker,%s", linker),
1576 "-Wl,--gc-sections",
1577 "-Wl,-z,nocopyreloc",
1578 )
1579 }
Colin Cross0af4b842015-04-30 16:36:18 -07001580 } else if ctx.Darwin() {
1581 flags.LdFlags = append(flags.LdFlags, "-Wl,-headerpad_max_install_names")
Colin Cross3f40fa42015-01-30 17:27:36 -08001582 }
1583
Colin Cross97ba0732015-03-23 17:50:24 -07001584 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001585}
1586
Colin Cross97ba0732015-03-23 17:50:24 -07001587func (c *CCBinary) compileModule(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001588 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001589
Colin Cross06a931b2015-10-28 17:23:31 -07001590 if !Bool(c.BinaryProperties.Static_executable) && inList("libc", c.Properties.Static_libs) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001591 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
1592 "from static libs or set static_executable: true")
1593 }
1594
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001595 outputFile := common.PathForModuleOut(ctx, c.getStem(ctx)+flags.Toolchain.ExecutableSuffix())
Dan Albertc403f7c2015-03-18 14:01:18 -07001596 c.out = outputFile
Colin Crossbfae8852015-03-26 14:44:11 -07001597 if c.BinaryProperties.Prefix_symbols != "" {
1598 afterPrefixSymbols := outputFile
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001599 outputFile = common.PathForModuleOut(ctx, c.getStem(ctx)+".intermediate")
Colin Crossbfae8852015-03-26 14:44:11 -07001600 TransformBinaryPrefixSymbols(ctx, c.BinaryProperties.Prefix_symbols, outputFile,
1601 ccFlagsToBuilderFlags(flags), afterPrefixSymbols)
1602 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001603
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001604 var linkerDeps common.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07001605
Colin Cross97ba0732015-03-23 17:50:24 -07001606 TransformObjToDynamicBinary(ctx, objFiles, deps.SharedLibs, deps.StaticLibs,
Colin Crossaee540a2015-07-06 17:48:31 -07001607 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
Colin Cross77b00fa2015-03-16 16:15:49 -07001608 ccFlagsToBuilderFlags(flags), outputFile)
Dan Albertc403f7c2015-03-18 14:01:18 -07001609}
Colin Cross3f40fa42015-01-30 17:27:36 -08001610
Colin Cross97ba0732015-03-23 17:50:24 -07001611func (c *CCBinary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Willemsen782a2d12015-12-21 14:55:28 -08001612 c.installFile = ctx.InstallFile(common.PathForModuleInstall(ctx, "bin", c.Properties.Relative_install_path), c.out)
Colin Crossd350ecd2015-04-28 13:25:36 -07001613}
1614
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001615func (c *CCBinary) HostToolPath() common.OptionalPath {
Colin Crossd350ecd2015-04-28 13:25:36 -07001616 if c.HostOrDevice().Host() {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001617 return common.OptionalPathForPath(c.installFile)
Colin Crossd350ecd2015-04-28 13:25:36 -07001618 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001619 return common.OptionalPath{}
Dan Albertc403f7c2015-03-18 14:01:18 -07001620}
1621
Colin Cross6002e052015-09-16 16:00:08 -07001622func (c *CCBinary) binary() *CCBinary {
1623 return c
1624}
1625
1626type testPerSrc interface {
1627 binary() *CCBinary
1628 testPerSrc() bool
1629}
1630
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001631var _ testPerSrc = (*CCTest)(nil)
Colin Cross6002e052015-09-16 16:00:08 -07001632
Colin Cross6362e272015-10-29 15:25:03 -07001633func testPerSrcMutator(mctx common.AndroidBottomUpMutatorContext) {
Colin Cross6002e052015-09-16 16:00:08 -07001634 if test, ok := mctx.Module().(testPerSrc); ok {
1635 if test.testPerSrc() {
1636 testNames := make([]string, len(test.binary().Properties.Srcs))
1637 for i, src := range test.binary().Properties.Srcs {
1638 testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
1639 }
1640 tests := mctx.CreateLocalVariations(testNames...)
1641 for i, src := range test.binary().Properties.Srcs {
1642 tests[i].(testPerSrc).binary().Properties.Srcs = []string{src}
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001643 tests[i].(testPerSrc).binary().BinaryProperties.Stem = testNames[i]
Colin Cross6002e052015-09-16 16:00:08 -07001644 }
1645 }
1646 }
Colin Cross7d5136f2015-05-11 13:39:40 -07001647}
1648
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001649type CCTestProperties struct {
1650 // if set, build against the gtest library. Defaults to true.
1651 Gtest bool
1652
1653 // Create a separate binary for each source file. Useful when there is
1654 // global state that can not be torn down and reset between each test suite.
1655 Test_per_src *bool
1656}
1657
Colin Cross9ffb4f52015-04-24 17:48:09 -07001658type CCTest struct {
Colin Cross97ba0732015-03-23 17:50:24 -07001659 CCBinary
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001660
1661 TestProperties CCTestProperties
Dan Albertc403f7c2015-03-18 14:01:18 -07001662}
1663
Colin Cross9ffb4f52015-04-24 17:48:09 -07001664func (c *CCTest) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Cross0676e2d2015-04-24 17:39:18 -07001665 flags = c.CCBinary.flags(ctx, flags)
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001666 if !c.TestProperties.Gtest {
1667 return flags
1668 }
Dan Albertc403f7c2015-03-18 14:01:18 -07001669
Colin Cross97ba0732015-03-23 17:50:24 -07001670 flags.CFlags = append(flags.CFlags, "-DGTEST_HAS_STD_STRING")
Colin Crossf6566ed2015-03-24 11:13:38 -07001671 if ctx.Host() {
Colin Cross97ba0732015-03-23 17:50:24 -07001672 flags.CFlags = append(flags.CFlags, "-O0", "-g")
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001673
1674 if ctx.HostType() == common.Windows {
1675 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_WINDOWS")
1676 } else {
1677 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX")
1678 flags.LdFlags = append(flags.LdFlags, "-lpthread")
1679 }
1680 } else {
1681 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX_ANDROID")
Dan Albertc403f7c2015-03-18 14:01:18 -07001682 }
1683
1684 // TODO(danalbert): Make gtest export its dependencies.
Colin Cross28344522015-04-22 13:07:53 -07001685 flags.CFlags = append(flags.CFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001686 "-I"+common.PathForSource(ctx, "external/gtest/include").String())
Dan Albertc403f7c2015-03-18 14:01:18 -07001687
Colin Cross21b9a242015-03-24 14:15:58 -07001688 return flags
Dan Albertc403f7c2015-03-18 14:01:18 -07001689}
1690
Colin Cross9ffb4f52015-04-24 17:48:09 -07001691func (c *CCTest) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001692 if c.TestProperties.Gtest {
1693 depNames.StaticLibs = append(depNames.StaticLibs, "libgtest_main", "libgtest")
1694 }
Colin Crossa8a93d32015-04-28 13:26:49 -07001695 depNames = c.CCBinary.depNames(ctx, depNames)
Colin Cross21b9a242015-03-24 14:15:58 -07001696 return depNames
Dan Albertc403f7c2015-03-18 14:01:18 -07001697}
1698
Dan Willemsen782a2d12015-12-21 14:55:28 -08001699func (c *CCTest) InstallInData() bool {
1700 return true
1701}
1702
Colin Cross9ffb4f52015-04-24 17:48:09 -07001703func (c *CCTest) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001704 installDir := "nativetest"
1705 if flags.Toolchain.Is64Bit() {
1706 installDir = "nativetest64"
Dan Albertc403f7c2015-03-18 14:01:18 -07001707 }
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001708 ctx.InstallFile(common.PathForModuleInstall(ctx, installDir, ctx.ModuleName()), c.out)
1709}
1710
1711func (c *CCTest) testPerSrc() bool {
1712 return Bool(c.TestProperties.Test_per_src)
Dan Albertc403f7c2015-03-18 14:01:18 -07001713}
1714
Colin Cross9ffb4f52015-04-24 17:48:09 -07001715func NewCCTest(test *CCTest, module CCModuleType,
1716 hod common.HostOrDeviceSupported, props ...interface{}) (blueprint.Module, []interface{}) {
1717
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001718 props = append(props, &test.TestProperties)
1719
1720 return NewCCBinary(&test.CCBinary, module, hod, common.MultilibBoth, props...)
Colin Cross9ffb4f52015-04-24 17:48:09 -07001721}
1722
1723func CCTestFactory() (blueprint.Module, []interface{}) {
1724 module := &CCTest{}
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001725 module.TestProperties.Gtest = true
Colin Cross9ffb4f52015-04-24 17:48:09 -07001726
1727 return NewCCTest(module, module, common.HostAndDeviceSupported)
1728}
1729
Colin Cross2ba19d92015-05-07 15:44:20 -07001730type CCBenchmark struct {
1731 CCBinary
1732}
1733
1734func (c *CCBenchmark) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
1735 depNames = c.CCBinary.depNames(ctx, depNames)
Dan Willemsenf8e98b02015-09-11 17:41:44 -07001736 depNames.StaticLibs = append(depNames.StaticLibs, "libbenchmark", "libbase")
Colin Cross2ba19d92015-05-07 15:44:20 -07001737 return depNames
1738}
1739
Dan Willemsen782a2d12015-12-21 14:55:28 -08001740func (c *CCBenchmark) InstallInData() bool {
1741 return true
1742}
1743
Colin Cross2ba19d92015-05-07 15:44:20 -07001744func (c *CCBenchmark) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
1745 if ctx.Device() {
Dan Willemsen782a2d12015-12-21 14:55:28 -08001746 installDir := "nativetest"
1747 if flags.Toolchain.Is64Bit() {
1748 installDir = "nativetest64"
1749 }
1750 ctx.InstallFile(common.PathForModuleInstall(ctx, installDir, ctx.ModuleName()), c.out)
Colin Cross2ba19d92015-05-07 15:44:20 -07001751 } else {
1752 c.CCBinary.installModule(ctx, flags)
1753 }
1754}
1755
1756func NewCCBenchmark(test *CCBenchmark, module CCModuleType,
1757 hod common.HostOrDeviceSupported, props ...interface{}) (blueprint.Module, []interface{}) {
1758
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001759 return NewCCBinary(&test.CCBinary, module, hod, common.MultilibFirst, props...)
Colin Cross2ba19d92015-05-07 15:44:20 -07001760}
1761
1762func CCBenchmarkFactory() (blueprint.Module, []interface{}) {
1763 module := &CCBenchmark{}
1764
1765 return NewCCBenchmark(module, module, common.HostAndDeviceSupported)
1766}
1767
Colin Cross3f40fa42015-01-30 17:27:36 -08001768//
1769// Static library
1770//
1771
Colin Cross97ba0732015-03-23 17:50:24 -07001772func CCLibraryStaticFactory() (blueprint.Module, []interface{}) {
1773 module := &CCLibrary{}
1774 module.LibraryProperties.BuildStatic = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001775
Colin Cross97ba0732015-03-23 17:50:24 -07001776 return NewCCLibrary(module, module, common.HostAndDeviceSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001777}
1778
1779//
1780// Shared libraries
1781//
1782
Colin Cross97ba0732015-03-23 17:50:24 -07001783func CCLibrarySharedFactory() (blueprint.Module, []interface{}) {
1784 module := &CCLibrary{}
1785 module.LibraryProperties.BuildShared = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001786
Colin Cross97ba0732015-03-23 17:50:24 -07001787 return NewCCLibrary(module, module, common.HostAndDeviceSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001788}
1789
1790//
1791// Host static library
1792//
1793
Colin Cross97ba0732015-03-23 17:50:24 -07001794func CCLibraryHostStaticFactory() (blueprint.Module, []interface{}) {
1795 module := &CCLibrary{}
1796 module.LibraryProperties.BuildStatic = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001797
Colin Cross97ba0732015-03-23 17:50:24 -07001798 return NewCCLibrary(module, module, common.HostSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001799}
1800
1801//
1802// Host Shared libraries
1803//
1804
Colin Cross97ba0732015-03-23 17:50:24 -07001805func CCLibraryHostSharedFactory() (blueprint.Module, []interface{}) {
1806 module := &CCLibrary{}
1807 module.LibraryProperties.BuildShared = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001808
Colin Cross97ba0732015-03-23 17:50:24 -07001809 return NewCCLibrary(module, module, common.HostSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001810}
1811
1812//
1813// Host Binaries
1814//
1815
Colin Cross97ba0732015-03-23 17:50:24 -07001816func CCBinaryHostFactory() (blueprint.Module, []interface{}) {
1817 module := &CCBinary{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001818
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001819 return NewCCBinary(module, module, common.HostSupported, common.MultilibFirst)
Colin Cross3f40fa42015-01-30 17:27:36 -08001820}
1821
1822//
Colin Cross1f8f2342015-03-26 16:09:47 -07001823// Host Tests
1824//
1825
1826func CCTestHostFactory() (blueprint.Module, []interface{}) {
Colin Cross9ffb4f52015-04-24 17:48:09 -07001827 module := &CCTest{}
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001828 return NewCCTest(module, module, common.HostSupported)
Colin Cross1f8f2342015-03-26 16:09:47 -07001829}
1830
1831//
Colin Cross2ba19d92015-05-07 15:44:20 -07001832// Host Benchmarks
1833//
1834
1835func CCBenchmarkHostFactory() (blueprint.Module, []interface{}) {
1836 module := &CCBenchmark{}
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001837 return NewCCBinary(&module.CCBinary, module, common.HostSupported, common.MultilibFirst)
Colin Cross2ba19d92015-05-07 15:44:20 -07001838}
1839
1840//
Colin Crosscfad1192015-11-02 16:43:11 -08001841// Defaults
1842//
1843type CCDefaults struct {
1844 common.AndroidModuleBase
1845 common.DefaultsModule
1846}
1847
1848func (*CCDefaults) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
1849}
1850
1851func CCDefaultsFactory() (blueprint.Module, []interface{}) {
1852 module := &CCDefaults{}
1853
1854 propertyStructs := []interface{}{
1855 &CCBaseProperties{},
1856 &CCLibraryProperties{},
1857 &CCBinaryProperties{},
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001858 &CCTestProperties{},
Colin Crosscfad1192015-11-02 16:43:11 -08001859 &CCUnusedProperties{},
1860 }
1861
Dan Willemsen218f6562015-07-08 18:13:11 -07001862 _, propertyStructs = common.InitAndroidArchModule(module, common.HostAndDeviceDefault,
1863 common.MultilibDefault, propertyStructs...)
Colin Crosscfad1192015-11-02 16:43:11 -08001864
1865 return common.InitDefaultsModule(module, module, propertyStructs...)
1866}
1867
1868//
Colin Cross3f40fa42015-01-30 17:27:36 -08001869// Device libraries shipped with gcc
1870//
1871
1872type toolchainLibrary struct {
Colin Cross97ba0732015-03-23 17:50:24 -07001873 CCLibrary
Colin Cross3f40fa42015-01-30 17:27:36 -08001874}
1875
Colin Cross0676e2d2015-04-24 17:39:18 -07001876func (*toolchainLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Cross3f40fa42015-01-30 17:27:36 -08001877 // toolchain libraries can't have any dependencies
Colin Cross21b9a242015-03-24 14:15:58 -07001878 return CCDeps{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001879}
1880
Colin Cross97ba0732015-03-23 17:50:24 -07001881func ToolchainLibraryFactory() (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001882 module := &toolchainLibrary{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001883
Colin Cross97ba0732015-03-23 17:50:24 -07001884 module.LibraryProperties.BuildStatic = true
1885
Colin Crossfa138792015-04-24 17:31:52 -07001886 return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth,
Colin Cross21b9a242015-03-24 14:15:58 -07001887 &module.LibraryProperties)
Colin Cross3f40fa42015-01-30 17:27:36 -08001888}
1889
1890func (c *toolchainLibrary) compileModule(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001891 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001892
1893 libName := ctx.ModuleName() + staticLibraryExtension
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001894 outputFile := common.PathForModuleOut(ctx, libName)
Colin Cross3f40fa42015-01-30 17:27:36 -08001895
1896 CopyGccLib(ctx, libName, ccFlagsToBuilderFlags(flags), outputFile)
1897
1898 c.out = outputFile
1899
1900 ctx.CheckbuildFile(outputFile)
1901}
1902
Colin Cross97ba0732015-03-23 17:50:24 -07001903func (c *toolchainLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001904 // Toolchain libraries do not get installed.
1905}
1906
Dan Albertbe961682015-03-18 23:38:50 -07001907// NDK prebuilt libraries.
1908//
1909// These differ from regular prebuilts in that they aren't stripped and usually aren't installed
1910// either (with the exception of the shared STLs, which are installed to the app's directory rather
1911// than to the system image).
1912
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001913func getNdkLibDir(ctx common.AndroidModuleContext, toolchain Toolchain, version string) common.SourcePath {
1914 return common.PathForSource(ctx, fmt.Sprintf("prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/lib",
1915 version, toolchain.Name()))
Dan Albertbe961682015-03-18 23:38:50 -07001916}
1917
Dan Albertc3144b12015-04-28 18:17:56 -07001918func ndkPrebuiltModuleToPath(ctx common.AndroidModuleContext, toolchain Toolchain,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001919 ext string, version string) common.Path {
Dan Albertc3144b12015-04-28 18:17:56 -07001920
1921 // NDK prebuilts are named like: ndk_NAME.EXT.SDK_VERSION.
1922 // We want to translate to just NAME.EXT
1923 name := strings.Split(strings.TrimPrefix(ctx.ModuleName(), "ndk_"), ".")[0]
1924 dir := getNdkLibDir(ctx, toolchain, version)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001925 return dir.Join(ctx, name+ext)
Dan Albertc3144b12015-04-28 18:17:56 -07001926}
1927
1928type ndkPrebuiltObject struct {
1929 ccObject
1930}
1931
Dan Albertc3144b12015-04-28 18:17:56 -07001932func (*ndkPrebuiltObject) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
1933 // NDK objects can't have any dependencies
1934 return CCDeps{}
1935}
1936
1937func NdkPrebuiltObjectFactory() (blueprint.Module, []interface{}) {
1938 module := &ndkPrebuiltObject{}
1939 return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth)
1940}
1941
1942func (c *ndkPrebuiltObject) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001943 deps CCPathDeps, objFiles common.Paths) {
Dan Albertc3144b12015-04-28 18:17:56 -07001944 // A null build step, but it sets up the output path.
1945 if !strings.HasPrefix(ctx.ModuleName(), "ndk_crt") {
1946 ctx.ModuleErrorf("NDK prebuilts must have an ndk_crt prefixed name")
1947 }
1948
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001949 c.out = common.OptionalPathForPath(ndkPrebuiltModuleToPath(ctx, flags.Toolchain, objectExtension, c.Properties.Sdk_version))
Dan Albertc3144b12015-04-28 18:17:56 -07001950}
1951
1952func (c *ndkPrebuiltObject) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
1953 // Objects do not get installed.
1954}
1955
1956var _ ccObjectProvider = (*ndkPrebuiltObject)(nil)
1957
Dan Albertbe961682015-03-18 23:38:50 -07001958type ndkPrebuiltLibrary struct {
1959 CCLibrary
1960}
1961
Colin Cross0676e2d2015-04-24 17:39:18 -07001962func (*ndkPrebuiltLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Dan Albertbe961682015-03-18 23:38:50 -07001963 // NDK libraries can't have any dependencies
1964 return CCDeps{}
1965}
1966
1967func NdkPrebuiltLibraryFactory() (blueprint.Module, []interface{}) {
1968 module := &ndkPrebuiltLibrary{}
1969 module.LibraryProperties.BuildShared = true
1970 return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
1971}
1972
1973func (c *ndkPrebuiltLibrary) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001974 deps CCPathDeps, objFiles common.Paths) {
Dan Albertbe961682015-03-18 23:38:50 -07001975 // A null build step, but it sets up the output path.
1976 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
1977 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
1978 }
1979
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001980 includeDirs := common.PathsForModuleSrc(ctx, c.Properties.Export_include_dirs)
1981 c.exportFlags = []string{common.JoinWithPrefix(includeDirs.Strings(), "-isystem ")}
Dan Albertbe961682015-03-18 23:38:50 -07001982
Dan Willemsen490fd492015-11-24 17:53:15 -08001983 c.out = ndkPrebuiltModuleToPath(ctx, flags.Toolchain, flags.Toolchain.ShlibSuffix(),
Dan Albertc3144b12015-04-28 18:17:56 -07001984 c.Properties.Sdk_version)
Dan Albertbe961682015-03-18 23:38:50 -07001985}
1986
1987func (c *ndkPrebuiltLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc3144b12015-04-28 18:17:56 -07001988 // NDK prebuilt libraries do not get installed.
Dan Albertbe961682015-03-18 23:38:50 -07001989}
1990
1991// The NDK STLs are slightly different from the prebuilt system libraries:
1992// * Are not specific to each platform version.
1993// * The libraries are not in a predictable location for each STL.
1994
1995type ndkPrebuiltStl struct {
1996 ndkPrebuiltLibrary
1997}
1998
1999type ndkPrebuiltStaticStl struct {
2000 ndkPrebuiltStl
2001}
2002
2003type ndkPrebuiltSharedStl struct {
2004 ndkPrebuiltStl
2005}
2006
2007func NdkPrebuiltSharedStlFactory() (blueprint.Module, []interface{}) {
2008 module := &ndkPrebuiltSharedStl{}
2009 module.LibraryProperties.BuildShared = true
2010 return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
2011}
2012
2013func NdkPrebuiltStaticStlFactory() (blueprint.Module, []interface{}) {
2014 module := &ndkPrebuiltStaticStl{}
2015 module.LibraryProperties.BuildStatic = true
2016 return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
2017}
2018
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002019func getNdkStlLibDir(ctx common.AndroidModuleContext, toolchain Toolchain, stl string) common.SourcePath {
Dan Albertbe961682015-03-18 23:38:50 -07002020 gccVersion := toolchain.GccVersion()
2021 var libDir string
2022 switch stl {
2023 case "libstlport":
2024 libDir = "cxx-stl/stlport/libs"
2025 case "libc++":
2026 libDir = "cxx-stl/llvm-libc++/libs"
2027 case "libgnustl":
2028 libDir = fmt.Sprintf("cxx-stl/gnu-libstdc++/%s/libs", gccVersion)
2029 }
2030
2031 if libDir != "" {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002032 ndkSrcRoot := "prebuilts/ndk/current/sources"
2033 return common.PathForSource(ctx, ndkSrcRoot).Join(ctx, libDir, ctx.Arch().Abi[0])
Dan Albertbe961682015-03-18 23:38:50 -07002034 }
2035
2036 ctx.ModuleErrorf("Unknown NDK STL: %s", stl)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002037 return common.PathForSource(ctx, "")
Dan Albertbe961682015-03-18 23:38:50 -07002038}
2039
2040func (c *ndkPrebuiltStl) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002041 deps CCPathDeps, objFiles common.Paths) {
Dan Albertbe961682015-03-18 23:38:50 -07002042 // A null build step, but it sets up the output path.
2043 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
2044 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
2045 }
2046
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002047 includeDirs := common.PathsForModuleSrc(ctx, c.Properties.Export_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -07002048 c.exportFlags = []string{includeDirsToFlags(includeDirs)}
Dan Albertbe961682015-03-18 23:38:50 -07002049
2050 libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_")
Dan Willemsen490fd492015-11-24 17:53:15 -08002051 libExt := flags.Toolchain.ShlibSuffix()
Dan Albertbe961682015-03-18 23:38:50 -07002052 if c.LibraryProperties.BuildStatic {
2053 libExt = staticLibraryExtension
2054 }
2055
2056 stlName := strings.TrimSuffix(libName, "_shared")
2057 stlName = strings.TrimSuffix(stlName, "_static")
2058 libDir := getNdkStlLibDir(ctx, flags.Toolchain, stlName)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002059 c.out = libDir.Join(ctx, libName+libExt)
Dan Albertbe961682015-03-18 23:38:50 -07002060}
2061
Colin Cross6362e272015-10-29 15:25:03 -07002062func linkageMutator(mctx common.AndroidBottomUpMutatorContext) {
Colin Crossed4cf0b2015-03-26 14:43:45 -07002063 if c, ok := mctx.Module().(ccLinkedInterface); ok {
Colin Cross3f40fa42015-01-30 17:27:36 -08002064 var modules []blueprint.Module
Colin Crossed4cf0b2015-03-26 14:43:45 -07002065 if c.buildStatic() && c.buildShared() {
Colin Cross3f40fa42015-01-30 17:27:36 -08002066 modules = mctx.CreateLocalVariations("static", "shared")
Colin Cross18b6dc52015-04-28 13:20:37 -07002067 modules[0].(ccLinkedInterface).setStatic(true)
2068 modules[1].(ccLinkedInterface).setStatic(false)
Colin Crossed4cf0b2015-03-26 14:43:45 -07002069 } else if c.buildStatic() {
Colin Cross3f40fa42015-01-30 17:27:36 -08002070 modules = mctx.CreateLocalVariations("static")
Colin Cross18b6dc52015-04-28 13:20:37 -07002071 modules[0].(ccLinkedInterface).setStatic(true)
Colin Crossed4cf0b2015-03-26 14:43:45 -07002072 } else if c.buildShared() {
Colin Cross3f40fa42015-01-30 17:27:36 -08002073 modules = mctx.CreateLocalVariations("shared")
Colin Cross18b6dc52015-04-28 13:20:37 -07002074 modules[0].(ccLinkedInterface).setStatic(false)
Colin Cross3f40fa42015-01-30 17:27:36 -08002075 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07002076 panic(fmt.Errorf("ccLibrary %q not static or shared", mctx.ModuleName()))
Colin Cross3f40fa42015-01-30 17:27:36 -08002077 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07002078
2079 if _, ok := c.(ccLibraryInterface); ok {
2080 reuseFrom := modules[0].(ccLibraryInterface)
2081 for _, m := range modules {
2082 m.(ccLibraryInterface).setReuseFrom(reuseFrom)
Colin Cross3f40fa42015-01-30 17:27:36 -08002083 }
2084 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002085 }
2086}
Colin Cross74d1ec02015-04-28 13:30:13 -07002087
2088// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
2089// modifies the slice contents in place, and returns a subslice of the original slice
2090func lastUniqueElements(list []string) []string {
2091 totalSkip := 0
2092 for i := len(list) - 1; i >= totalSkip; i-- {
2093 skip := 0
2094 for j := i - 1; j >= totalSkip; j-- {
2095 if list[i] == list[j] {
2096 skip++
2097 } else {
2098 list[j+skip] = list[j]
2099 }
2100 }
2101 totalSkip += skip
2102 }
2103 return list[totalSkip:]
2104}
Colin Cross06a931b2015-10-28 17:23:31 -07002105
2106var Bool = proptools.Bool