blob: c532e50a7cced15cf7bd2221635fd54e0505acf1 [file] [log] [blame]
Colin Cross5049f022015-03-18 13:28:46 -07001// Copyright 2015 Google Inc. All rights reserved.
Colin Cross3f40fa42015-01-30 17:27:36 -08002//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package cc
16
17// This file contains the module types for compiling C/C++ for Android, and converts the properties
18// into the flags and filenames necessary to pass to the compiler. The final creation of the rules
19// is handled in builder.go
20
21import (
Colin Cross3f40fa42015-01-30 17:27:36 -080022 "fmt"
23 "path/filepath"
24 "strings"
25
Colin Cross97ba0732015-03-23 17:50:24 -070026 "github.com/google/blueprint"
Colin Cross06a931b2015-10-28 17:23:31 -070027 "github.com/google/blueprint/proptools"
Colin Cross97ba0732015-03-23 17:50:24 -070028
Colin Cross463a90e2015-06-17 14:20:06 -070029 "android/soong"
Colin Cross3f40fa42015-01-30 17:27:36 -080030 "android/soong/common"
Colin Cross5049f022015-03-18 13:28:46 -070031 "android/soong/genrule"
Colin Cross3f40fa42015-01-30 17:27:36 -080032)
33
Colin Cross463a90e2015-06-17 14:20:06 -070034func init() {
35 soong.RegisterModuleType("cc_library_static", CCLibraryStaticFactory)
36 soong.RegisterModuleType("cc_library_shared", CCLibrarySharedFactory)
37 soong.RegisterModuleType("cc_library", CCLibraryFactory)
38 soong.RegisterModuleType("cc_object", CCObjectFactory)
39 soong.RegisterModuleType("cc_binary", CCBinaryFactory)
40 soong.RegisterModuleType("cc_test", CCTestFactory)
41 soong.RegisterModuleType("cc_benchmark", CCBenchmarkFactory)
Colin Crosscfad1192015-11-02 16:43:11 -080042 soong.RegisterModuleType("cc_defaults", CCDefaultsFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070043
44 soong.RegisterModuleType("toolchain_library", ToolchainLibraryFactory)
45 soong.RegisterModuleType("ndk_prebuilt_library", NdkPrebuiltLibraryFactory)
46 soong.RegisterModuleType("ndk_prebuilt_object", NdkPrebuiltObjectFactory)
47 soong.RegisterModuleType("ndk_prebuilt_static_stl", NdkPrebuiltStaticStlFactory)
48 soong.RegisterModuleType("ndk_prebuilt_shared_stl", NdkPrebuiltSharedStlFactory)
49
50 soong.RegisterModuleType("cc_library_host_static", CCLibraryHostStaticFactory)
51 soong.RegisterModuleType("cc_library_host_shared", CCLibraryHostSharedFactory)
52 soong.RegisterModuleType("cc_binary_host", CCBinaryHostFactory)
53 soong.RegisterModuleType("cc_test_host", CCTestHostFactory)
54 soong.RegisterModuleType("cc_benchmark_host", CCBenchmarkHostFactory)
55
56 // LinkageMutator must be registered after common.ArchMutator, but that is guaranteed by
57 // the Go initialization order because this package depends on common, so common's init
58 // functions will run first.
Colin Cross6362e272015-10-29 15:25:03 -070059 common.RegisterBottomUpMutator("link", linkageMutator)
60 common.RegisterBottomUpMutator("test_per_src", testPerSrcMutator)
61 common.RegisterBottomUpMutator("deps", depsMutator)
Colin Cross463a90e2015-06-17 14:20:06 -070062}
63
Colin Cross3f40fa42015-01-30 17:27:36 -080064var (
Colin Cross1332b002015-04-07 17:11:30 -070065 HostPrebuiltTag = pctx.VariableConfigMethod("HostPrebuiltTag", common.Config.PrebuiltOS)
Colin Cross3f40fa42015-01-30 17:27:36 -080066
Dan Willemsen34cc69e2015-09-23 15:26:20 -070067 LibcRoot = pctx.SourcePathVariable("LibcRoot", "bionic/libc")
68 LibmRoot = pctx.SourcePathVariable("LibmRoot", "bionic/libm")
Colin Cross3f40fa42015-01-30 17:27:36 -080069)
70
71// Flags used by lots of devices. Putting them in package static variables will save bytes in
72// build.ninja so they aren't repeated for every file
73var (
74 commonGlobalCflags = []string{
75 "-DANDROID",
76 "-fmessage-length=0",
77 "-W",
78 "-Wall",
79 "-Wno-unused",
80 "-Winit-self",
81 "-Wpointer-arith",
Dan Willemsene6540452015-10-20 15:21:33 -070082 "-fdebug-prefix-map=/proc/self/cwd=",
Colin Cross3f40fa42015-01-30 17:27:36 -080083
84 // COMMON_RELEASE_CFLAGS
85 "-DNDEBUG",
86 "-UDEBUG",
87 }
88
89 deviceGlobalCflags = []string{
Dan Willemsen490fd492015-11-24 17:53:15 -080090 "-fdiagnostics-color",
91
Colin Cross3f40fa42015-01-30 17:27:36 -080092 // TARGET_ERROR_FLAGS
93 "-Werror=return-type",
94 "-Werror=non-virtual-dtor",
95 "-Werror=address",
96 "-Werror=sequence-point",
97 }
98
99 hostGlobalCflags = []string{}
100
101 commonGlobalCppflags = []string{
102 "-Wsign-promo",
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700103 }
104
105 illegalFlags = []string{
106 "-w",
Colin Cross3f40fa42015-01-30 17:27:36 -0800107 }
108)
109
110func init() {
111 pctx.StaticVariable("commonGlobalCflags", strings.Join(commonGlobalCflags, " "))
112 pctx.StaticVariable("deviceGlobalCflags", strings.Join(deviceGlobalCflags, " "))
113 pctx.StaticVariable("hostGlobalCflags", strings.Join(hostGlobalCflags, " "))
114
115 pctx.StaticVariable("commonGlobalCppflags", strings.Join(commonGlobalCppflags, " "))
116
117 pctx.StaticVariable("commonClangGlobalCflags",
118 strings.Join(clangFilterUnknownCflags(commonGlobalCflags), " "))
119 pctx.StaticVariable("deviceClangGlobalCflags",
120 strings.Join(clangFilterUnknownCflags(deviceGlobalCflags), " "))
121 pctx.StaticVariable("hostClangGlobalCflags",
122 strings.Join(clangFilterUnknownCflags(hostGlobalCflags), " "))
Tim Kilbournf2948142015-03-11 12:03:03 -0700123 pctx.StaticVariable("commonClangGlobalCppflags",
124 strings.Join(clangFilterUnknownCflags(commonGlobalCppflags), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800125
126 // Everything in this list is a crime against abstraction and dependency tracking.
127 // Do not add anything to this list.
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800128 pctx.PrefixedPathsForOptionalSourceVariable("commonGlobalIncludes", "-isystem ",
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700129 []string{
130 "system/core/include",
131 "hardware/libhardware/include",
132 "hardware/libhardware_legacy/include",
133 "hardware/ril/include",
134 "libnativehelper/include",
135 "frameworks/native/include",
136 "frameworks/native/opengl/include",
137 "frameworks/av/include",
138 "frameworks/base/include",
139 })
Colin Cross3f40fa42015-01-30 17:27:36 -0800140
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700141 pctx.SourcePathVariable("clangPath", "prebuilts/clang/host/${HostPrebuiltTag}/3.8/bin")
Colin Cross3f40fa42015-01-30 17:27:36 -0800142}
143
Colin Cross6362e272015-10-29 15:25:03 -0700144type CCModuleContext common.AndroidBaseContext
145
Colin Cross3f40fa42015-01-30 17:27:36 -0800146// Building C/C++ code is handled by objects that satisfy this interface via composition
Colin Cross97ba0732015-03-23 17:50:24 -0700147type CCModuleType interface {
Colin Cross3f40fa42015-01-30 17:27:36 -0800148 common.AndroidModule
149
Colin Crossfa138792015-04-24 17:31:52 -0700150 // Modify property values after parsing Blueprints file but before starting dependency
151 // resolution or build rule generation
Colin Cross6362e272015-10-29 15:25:03 -0700152 ModifyProperties(CCModuleContext)
Colin Crossfa138792015-04-24 17:31:52 -0700153
Colin Cross21b9a242015-03-24 14:15:58 -0700154 // Modify the ccFlags
Colin Cross0676e2d2015-04-24 17:39:18 -0700155 flags(common.AndroidModuleContext, CCFlags) CCFlags
Colin Cross3f40fa42015-01-30 17:27:36 -0800156
Colin Cross6362e272015-10-29 15:25:03 -0700157 // Return list of dependency names for use in depsMutator
Colin Cross0676e2d2015-04-24 17:39:18 -0700158 depNames(common.AndroidBaseContext, CCDeps) CCDeps
Colin Cross3f40fa42015-01-30 17:27:36 -0800159
Colin Cross6362e272015-10-29 15:25:03 -0700160 // Add dynamic dependencies
161 depsMutator(common.AndroidBottomUpMutatorContext)
162
Colin Cross3f40fa42015-01-30 17:27:36 -0800163 // Compile objects into final module
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700164 compileModule(common.AndroidModuleContext, CCFlags, CCPathDeps, common.Paths)
Colin Cross3f40fa42015-01-30 17:27:36 -0800165
Dan Albertc403f7c2015-03-18 14:01:18 -0700166 // Install the built module.
Colin Cross97ba0732015-03-23 17:50:24 -0700167 installModule(common.AndroidModuleContext, CCFlags)
Dan Albertc403f7c2015-03-18 14:01:18 -0700168
Colin Cross3f40fa42015-01-30 17:27:36 -0800169 // Return the output file (.o, .a or .so) for use by other modules
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700170 outputFile() common.OptionalPath
Colin Cross3f40fa42015-01-30 17:27:36 -0800171}
172
Colin Cross97ba0732015-03-23 17:50:24 -0700173type CCDeps struct {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700174 StaticLibs, SharedLibs, LateStaticLibs, WholeStaticLibs []string
Colin Crossc472d572015-03-17 15:06:21 -0700175
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700176 ObjFiles common.Paths
177
178 Cflags, ReexportedCflags []string
Colin Cross21b9a242015-03-24 14:15:58 -0700179
Colin Cross97ba0732015-03-23 17:50:24 -0700180 CrtBegin, CrtEnd string
Colin Crossc472d572015-03-17 15:06:21 -0700181}
182
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700183type CCPathDeps struct {
184 StaticLibs, SharedLibs, LateStaticLibs, WholeStaticLibs common.Paths
185
186 ObjFiles common.Paths
187 WholeStaticLibObjFiles common.Paths
188
189 Cflags, ReexportedCflags []string
190
191 CrtBegin, CrtEnd common.OptionalPath
192}
193
Colin Cross97ba0732015-03-23 17:50:24 -0700194type CCFlags struct {
Colin Cross28344522015-04-22 13:07:53 -0700195 GlobalFlags []string // Flags that apply to C, C++, and assembly source files
196 AsFlags []string // Flags that apply to assembly source files
197 CFlags []string // Flags that apply to C and C++ source files
198 ConlyFlags []string // Flags that apply to C source files
199 CppFlags []string // Flags that apply to C++ source files
200 YaccFlags []string // Flags that apply to Yacc source files
201 LdFlags []string // Flags that apply to linker command lines
202
203 Nocrt bool
204 Toolchain Toolchain
205 Clang bool
Colin Crossc472d572015-03-17 15:06:21 -0700206}
207
Colin Cross7d5136f2015-05-11 13:39:40 -0700208// Properties used to compile all C or C++ modules
209type CCBaseProperties struct {
210 // list of source files used to compile the C/C++ module. May be .c, .cpp, or .S files.
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700211 Srcs []string `android:"arch_variant"`
212
213 // list of source files that should not be used to build the C/C++ module.
214 // This is most useful in the arch/multilib variants to remove non-common files
215 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700216
217 // list of module-specific flags that will be used for C and C++ compiles.
218 Cflags []string `android:"arch_variant"`
219
220 // list of module-specific flags that will be used for C++ compiles
221 Cppflags []string `android:"arch_variant"`
222
223 // list of module-specific flags that will be used for C compiles
224 Conlyflags []string `android:"arch_variant"`
225
226 // list of module-specific flags that will be used for .S compiles
227 Asflags []string `android:"arch_variant"`
228
229 // list of module-specific flags that will be used for .y and .yy compiles
230 Yaccflags []string
231
232 // list of module-specific flags that will be used for all link steps
233 Ldflags []string `android:"arch_variant"`
234
235 // the instruction set architecture to use to compile the C/C++
236 // module.
237 Instruction_set string `android:"arch_variant"`
238
239 // list of directories relative to the root of the source tree that will
240 // be added to the include path using -I.
241 // If possible, don't use this. If adding paths from the current directory use
242 // local_include_dirs, if adding paths from other modules use export_include_dirs in
243 // that module.
244 Include_dirs []string `android:"arch_variant"`
245
Colin Cross39d97f22015-09-14 12:30:50 -0700246 // list of files relative to the root of the source tree that will be included
247 // using -include.
248 // If possible, don't use this.
249 Include_files []string `android:"arch_variant"`
250
Colin Cross7d5136f2015-05-11 13:39:40 -0700251 // list of directories relative to the Blueprints file that will
252 // be added to the include path using -I
253 Local_include_dirs []string `android:"arch_variant"`
254
Colin Cross39d97f22015-09-14 12:30:50 -0700255 // list of files relative to the Blueprints file that will be included
256 // using -include.
257 // If possible, don't use this.
258 Local_include_files []string `android:"arch_variant"`
259
Colin Cross7d5136f2015-05-11 13:39:40 -0700260 // list of directories relative to the Blueprints file that will
261 // be added to the include path using -I for any module that links against this module
262 Export_include_dirs []string `android:"arch_variant"`
263
264 // list of module-specific flags that will be used for C and C++ compiles when
265 // compiling with clang
266 Clang_cflags []string `android:"arch_variant"`
267
268 // list of module-specific flags that will be used for .S compiles when
269 // compiling with clang
270 Clang_asflags []string `android:"arch_variant"`
271
272 // list of system libraries that will be dynamically linked to
273 // shared library and executable modules. If unset, generally defaults to libc
274 // and libm. Set to [] to prevent linking against libc and libm.
275 System_shared_libs []string
276
277 // list of modules whose object files should be linked into this module
278 // in their entirety. For static library modules, all of the .o files from the intermediate
279 // directory of the dependency will be linked into this modules .a file. For a shared library,
280 // the dependency's .a file will be linked into this module using -Wl,--whole-archive.
281 Whole_static_libs []string `android:"arch_variant"`
282
283 // list of modules that should be statically linked into this module.
284 Static_libs []string `android:"arch_variant"`
285
286 // list of modules that should be dynamically linked into this module.
287 Shared_libs []string `android:"arch_variant"`
288
289 // allow the module to contain undefined symbols. By default,
290 // modules cannot contain undefined symbols that are not satisified by their immediate
291 // dependencies. Set this flag to true to remove --no-undefined from the linker flags.
292 // This flag should only be necessary for compiling low-level libraries like libc.
Colin Cross06a931b2015-10-28 17:23:31 -0700293 Allow_undefined_symbols *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700294
295 // don't link in crt_begin and crt_end. This flag should only be necessary for
296 // compiling crt or libc.
Colin Cross06a931b2015-10-28 17:23:31 -0700297 Nocrt *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700298
Dan Willemsend67be222015-09-16 15:19:33 -0700299 // don't link in libgcc.a
Colin Cross06a931b2015-10-28 17:23:31 -0700300 No_libgcc *bool
Dan Willemsend67be222015-09-16 15:19:33 -0700301
Colin Cross7d5136f2015-05-11 13:39:40 -0700302 // don't insert default compiler flags into asflags, cflags,
303 // cppflags, conlyflags, ldflags, or include_dirs
Colin Cross06a931b2015-10-28 17:23:31 -0700304 No_default_compiler_flags *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700305
306 // compile module with clang instead of gcc
Colin Cross06a931b2015-10-28 17:23:31 -0700307 Clang *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700308
309 // pass -frtti instead of -fno-rtti
Colin Cross06a931b2015-10-28 17:23:31 -0700310 Rtti *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700311
312 // -l arguments to pass to linker for host-provided shared libraries
313 Host_ldlibs []string `android:"arch_variant"`
314
315 // select the STL library to use. Possible values are "libc++", "libc++_static",
316 // "stlport", "stlport_static", "ndk", "libstdc++", or "none". Leave blank to select the
317 // default
318 Stl string
319
320 // Set for combined shared/static libraries to prevent compiling object files a second time
321 SkipCompileObjs bool `blueprint:"mutated"`
322
323 Debug, Release struct {
324 // list of module-specific flags that will be used for C and C++ compiles in debug or
325 // release builds
326 Cflags []string `android:"arch_variant"`
327 } `android:"arch_variant"`
328
329 // Minimum sdk version supported when compiling against the ndk
330 Sdk_version string
331
332 // install to a subdirectory of the default install path for the module
333 Relative_install_path string
334}
335
Colin Crosscfad1192015-11-02 16:43:11 -0800336type CCUnusedProperties struct {
337 Native_coverage *bool
338 Required []string
339 Sanitize []string `android:"arch_variant"`
340 Sanitize_recover []string
341 Strip string
342 Tags []string
343}
344
Colin Crossfa138792015-04-24 17:31:52 -0700345// CCBase contains the properties and members used by all C/C++ module types, and implements
Colin Crossc472d572015-03-17 15:06:21 -0700346// the blueprint.Module interface. It expects to be embedded into an outer specialization struct,
347// and uses a ccModuleType interface to that struct to create the build steps.
Colin Crossfa138792015-04-24 17:31:52 -0700348type CCBase struct {
Colin Crossc472d572015-03-17 15:06:21 -0700349 common.AndroidModuleBase
Colin Crosscfad1192015-11-02 16:43:11 -0800350 common.DefaultableModule
Colin Cross97ba0732015-03-23 17:50:24 -0700351 module CCModuleType
Colin Crossc472d572015-03-17 15:06:21 -0700352
Colin Cross7d5136f2015-05-11 13:39:40 -0700353 Properties CCBaseProperties
Colin Crossfa138792015-04-24 17:31:52 -0700354
Colin Crosscfad1192015-11-02 16:43:11 -0800355 unused CCUnusedProperties
Colin Crossc472d572015-03-17 15:06:21 -0700356
357 installPath string
Colin Cross74d1ec02015-04-28 13:30:13 -0700358
359 savedDepNames CCDeps
Colin Crossc472d572015-03-17 15:06:21 -0700360}
361
Colin Crossfa138792015-04-24 17:31:52 -0700362func newCCBase(base *CCBase, module CCModuleType, hod common.HostOrDeviceSupported,
Colin Crossc472d572015-03-17 15:06:21 -0700363 multilib common.Multilib, props ...interface{}) (blueprint.Module, []interface{}) {
364
365 base.module = module
366
Colin Crossfa138792015-04-24 17:31:52 -0700367 props = append(props, &base.Properties, &base.unused)
Colin Crossc472d572015-03-17 15:06:21 -0700368
Colin Crosscfad1192015-11-02 16:43:11 -0800369 _, props = common.InitAndroidArchModule(module, hod, multilib, props...)
370
371 return common.InitDefaultableModule(module, base, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700372}
373
Colin Crossfa138792015-04-24 17:31:52 -0700374func (c *CCBase) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800375 toolchain := c.findToolchain(ctx)
376 if ctx.Failed() {
377 return
378 }
379
Colin Cross21b9a242015-03-24 14:15:58 -0700380 flags := c.collectFlags(ctx, toolchain)
Colin Cross3f40fa42015-01-30 17:27:36 -0800381 if ctx.Failed() {
382 return
383 }
384
Colin Cross74d1ec02015-04-28 13:30:13 -0700385 deps := c.depsToPaths(ctx, c.savedDepNames)
Colin Cross3f40fa42015-01-30 17:27:36 -0800386 if ctx.Failed() {
387 return
388 }
389
Colin Cross28344522015-04-22 13:07:53 -0700390 flags.CFlags = append(flags.CFlags, deps.Cflags...)
Colin Crossed9f8682015-03-18 17:17:35 -0700391
Colin Cross581c1892015-04-07 16:50:10 -0700392 objFiles := c.compileObjs(ctx, flags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800393 if ctx.Failed() {
394 return
395 }
396
Colin Cross581c1892015-04-07 16:50:10 -0700397 generatedObjFiles := c.compileGeneratedObjs(ctx, flags)
Colin Cross5049f022015-03-18 13:28:46 -0700398 if ctx.Failed() {
399 return
400 }
401
402 objFiles = append(objFiles, generatedObjFiles...)
403
Colin Cross3f40fa42015-01-30 17:27:36 -0800404 c.ccModuleType().compileModule(ctx, flags, deps, objFiles)
405 if ctx.Failed() {
406 return
407 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700408
409 c.ccModuleType().installModule(ctx, flags)
410 if ctx.Failed() {
411 return
412 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800413}
414
Colin Crossfa138792015-04-24 17:31:52 -0700415func (c *CCBase) ccModuleType() CCModuleType {
Colin Cross3f40fa42015-01-30 17:27:36 -0800416 return c.module
417}
418
Colin Crossfa138792015-04-24 17:31:52 -0700419func (c *CCBase) findToolchain(ctx common.AndroidModuleContext) Toolchain {
Colin Cross3f40fa42015-01-30 17:27:36 -0800420 arch := ctx.Arch()
Colin Crossd3ba0392015-05-07 14:11:29 -0700421 hod := ctx.HostOrDevice()
Dan Willemsen490fd492015-11-24 17:53:15 -0800422 ht := ctx.HostType()
423 factory := toolchainFactories[hod][ht][arch.ArchType]
Colin Cross3f40fa42015-01-30 17:27:36 -0800424 if factory == nil {
Dan Willemsen490fd492015-11-24 17:53:15 -0800425 ctx.ModuleErrorf("Toolchain not found for %s %s arch %q", hod.String(), ht.String(), arch.String())
Colin Crosseeabb892015-11-20 13:07:51 -0800426 return nil
Colin Cross3f40fa42015-01-30 17:27:36 -0800427 }
Colin Crossc5c24ad2015-11-20 15:35:00 -0800428 return factory(arch)
Colin Cross3f40fa42015-01-30 17:27:36 -0800429}
430
Colin Cross6362e272015-10-29 15:25:03 -0700431func (c *CCBase) ModifyProperties(ctx CCModuleContext) {
Colin Crossfa138792015-04-24 17:31:52 -0700432}
433
Colin Crosse11befc2015-04-27 17:49:17 -0700434func (c *CCBase) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Crossfa138792015-04-24 17:31:52 -0700435 depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.Properties.Whole_static_libs...)
436 depNames.StaticLibs = append(depNames.StaticLibs, c.Properties.Static_libs...)
437 depNames.SharedLibs = append(depNames.SharedLibs, c.Properties.Shared_libs...)
Colin Cross21b9a242015-03-24 14:15:58 -0700438
Colin Cross21b9a242015-03-24 14:15:58 -0700439 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -0800440}
441
Colin Cross6362e272015-10-29 15:25:03 -0700442func (c *CCBase) depsMutator(ctx common.AndroidBottomUpMutatorContext) {
Colin Cross74d1ec02015-04-28 13:30:13 -0700443 c.savedDepNames = c.module.depNames(ctx, CCDeps{})
444 c.savedDepNames.WholeStaticLibs = lastUniqueElements(c.savedDepNames.WholeStaticLibs)
445 c.savedDepNames.StaticLibs = lastUniqueElements(c.savedDepNames.StaticLibs)
446 c.savedDepNames.SharedLibs = lastUniqueElements(c.savedDepNames.SharedLibs)
447
448 staticLibs := c.savedDepNames.WholeStaticLibs
449 staticLibs = append(staticLibs, c.savedDepNames.StaticLibs...)
450 staticLibs = append(staticLibs, c.savedDepNames.LateStaticLibs...)
Colin Cross21b9a242015-03-24 14:15:58 -0700451 ctx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, staticLibs...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800452
Colin Cross74d1ec02015-04-28 13:30:13 -0700453 ctx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, c.savedDepNames.SharedLibs...)
Colin Cross21b9a242015-03-24 14:15:58 -0700454
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700455 ctx.AddDependency(ctx.Module(), c.savedDepNames.ObjFiles.Strings()...)
Colin Cross74d1ec02015-04-28 13:30:13 -0700456 if c.savedDepNames.CrtBegin != "" {
Colin Cross6362e272015-10-29 15:25:03 -0700457 ctx.AddDependency(ctx.Module(), c.savedDepNames.CrtBegin)
Colin Cross21b9a242015-03-24 14:15:58 -0700458 }
Colin Cross74d1ec02015-04-28 13:30:13 -0700459 if c.savedDepNames.CrtEnd != "" {
Colin Cross6362e272015-10-29 15:25:03 -0700460 ctx.AddDependency(ctx.Module(), c.savedDepNames.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700461 }
Colin Cross6362e272015-10-29 15:25:03 -0700462}
Colin Cross21b9a242015-03-24 14:15:58 -0700463
Colin Cross6362e272015-10-29 15:25:03 -0700464func depsMutator(ctx common.AndroidBottomUpMutatorContext) {
465 if c, ok := ctx.Module().(CCModuleType); ok {
466 c.ModifyProperties(ctx)
467 c.depsMutator(ctx)
468 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800469}
470
471// Create a ccFlags struct that collects the compile flags from global values,
472// per-target values, module type values, and per-module Blueprints properties
Colin Crossfa138792015-04-24 17:31:52 -0700473func (c *CCBase) collectFlags(ctx common.AndroidModuleContext, toolchain Toolchain) CCFlags {
Colin Cross97ba0732015-03-23 17:50:24 -0700474 flags := CCFlags{
Colin Crossfa138792015-04-24 17:31:52 -0700475 CFlags: c.Properties.Cflags,
476 CppFlags: c.Properties.Cppflags,
477 ConlyFlags: c.Properties.Conlyflags,
478 LdFlags: c.Properties.Ldflags,
479 AsFlags: c.Properties.Asflags,
480 YaccFlags: c.Properties.Yaccflags,
Colin Cross06a931b2015-10-28 17:23:31 -0700481 Nocrt: Bool(c.Properties.Nocrt),
Colin Cross97ba0732015-03-23 17:50:24 -0700482 Toolchain: toolchain,
Colin Cross06a931b2015-10-28 17:23:31 -0700483 Clang: Bool(c.Properties.Clang),
Colin Cross3f40fa42015-01-30 17:27:36 -0800484 }
Colin Cross28344522015-04-22 13:07:53 -0700485
486 // Include dir cflags
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700487 rootIncludeDirs := common.PathsForSource(ctx, c.Properties.Include_dirs)
488 localIncludeDirs := common.PathsForModuleSrc(ctx, c.Properties.Local_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -0700489 flags.GlobalFlags = append(flags.GlobalFlags,
Dan Willemsen1e898b92015-09-23 15:26:32 -0700490 includeDirsToFlags(localIncludeDirs),
491 includeDirsToFlags(rootIncludeDirs))
Colin Cross28344522015-04-22 13:07:53 -0700492
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700493 rootIncludeFiles := common.PathsForSource(ctx, c.Properties.Include_files)
494 localIncludeFiles := common.PathsForModuleSrc(ctx, c.Properties.Local_include_files)
Colin Cross39d97f22015-09-14 12:30:50 -0700495
496 flags.GlobalFlags = append(flags.GlobalFlags,
497 includeFilesToFlags(rootIncludeFiles),
498 includeFilesToFlags(localIncludeFiles))
499
Colin Cross06a931b2015-10-28 17:23:31 -0700500 if !Bool(c.Properties.No_default_compiler_flags) {
Colin Crossfa138792015-04-24 17:31:52 -0700501 if c.Properties.Sdk_version == "" || ctx.Host() {
Colin Cross28344522015-04-22 13:07:53 -0700502 flags.GlobalFlags = append(flags.GlobalFlags,
503 "${commonGlobalIncludes}",
504 toolchain.IncludeFlags(),
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700505 "-I"+common.PathForSource(ctx, "libnativehelper/include/nativehelper").String())
Colin Cross28344522015-04-22 13:07:53 -0700506 }
507
508 flags.GlobalFlags = append(flags.GlobalFlags, []string{
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700509 "-I" + common.PathForModuleSrc(ctx).String(),
510 "-I" + common.PathForModuleOut(ctx).String(),
511 "-I" + common.PathForModuleGen(ctx).String(),
Colin Cross28344522015-04-22 13:07:53 -0700512 }...)
513 }
514
Colin Cross06a931b2015-10-28 17:23:31 -0700515 if c.Properties.Clang == nil {
Dan Willemsendd0e2c32015-10-20 14:29:35 -0700516 if ctx.Host() {
517 flags.Clang = true
518 }
519
520 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
521 flags.Clang = true
522 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800523 }
524
Dan Willemsen490fd492015-11-24 17:53:15 -0800525 if !toolchain.ClangSupported() {
526 flags.Clang = false
527 }
528
Dan Willemsen6d11dd82015-11-03 14:27:00 -0800529 instructionSet := c.Properties.Instruction_set
530 instructionSetFlags, err := toolchain.InstructionSetFlags(instructionSet)
531 if flags.Clang {
532 instructionSetFlags, err = toolchain.ClangInstructionSetFlags(instructionSet)
533 }
534 if err != nil {
535 ctx.ModuleErrorf("%s", err)
536 }
537
538 // TODO: debug
539 flags.CFlags = append(flags.CFlags, c.Properties.Release.Cflags...)
540
Colin Cross97ba0732015-03-23 17:50:24 -0700541 if flags.Clang {
542 flags.CFlags = clangFilterUnknownCflags(flags.CFlags)
Colin Crossfa138792015-04-24 17:31:52 -0700543 flags.CFlags = append(flags.CFlags, c.Properties.Clang_cflags...)
544 flags.AsFlags = append(flags.AsFlags, c.Properties.Clang_asflags...)
Colin Cross97ba0732015-03-23 17:50:24 -0700545 flags.CppFlags = clangFilterUnknownCflags(flags.CppFlags)
546 flags.ConlyFlags = clangFilterUnknownCflags(flags.ConlyFlags)
547 flags.LdFlags = clangFilterUnknownCflags(flags.LdFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800548
Colin Cross97ba0732015-03-23 17:50:24 -0700549 flags.CFlags = append(flags.CFlags, "${clangExtraCflags}")
550 flags.ConlyFlags = append(flags.ConlyFlags, "${clangExtraConlyflags}")
Colin Crossf6566ed2015-03-24 11:13:38 -0700551 if ctx.Device() {
Colin Cross97ba0732015-03-23 17:50:24 -0700552 flags.CFlags = append(flags.CFlags, "${clangExtraTargetCflags}")
Colin Crossbdd7b1c2015-03-16 16:21:20 -0700553 }
554
Colin Cross3f40fa42015-01-30 17:27:36 -0800555 target := "-target " + toolchain.ClangTriple()
556 gccPrefix := "-B" + filepath.Join(toolchain.GccRoot(), toolchain.GccTriple(), "bin")
557
Colin Cross97ba0732015-03-23 17:50:24 -0700558 flags.CFlags = append(flags.CFlags, target, gccPrefix)
559 flags.AsFlags = append(flags.AsFlags, target, gccPrefix)
560 flags.LdFlags = append(flags.LdFlags, target, gccPrefix)
Colin Cross3f40fa42015-01-30 17:27:36 -0800561 }
562
Colin Cross06a931b2015-10-28 17:23:31 -0700563 if !Bool(c.Properties.No_default_compiler_flags) {
564 if ctx.Device() && !Bool(c.Properties.Allow_undefined_symbols) {
Colin Cross97ba0732015-03-23 17:50:24 -0700565 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-undefined")
Colin Cross3f40fa42015-01-30 17:27:36 -0800566 }
567
Colin Cross56b4d452015-04-21 17:38:44 -0700568 flags.GlobalFlags = append(flags.GlobalFlags, instructionSetFlags)
569
Colin Cross97ba0732015-03-23 17:50:24 -0700570 if flags.Clang {
571 flags.CppFlags = append(flags.CppFlags, "${commonClangGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -0700572 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -0800573 toolchain.ClangCflags(),
574 "${commonClangGlobalCflags}",
Colin Crossd3ba0392015-05-07 14:11:29 -0700575 fmt.Sprintf("${%sClangGlobalCflags}", ctx.HostOrDevice()))
Colin Cross3f40fa42015-01-30 17:27:36 -0800576 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700577 flags.CppFlags = append(flags.CppFlags, "${commonGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -0700578 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -0800579 toolchain.Cflags(),
580 "${commonGlobalCflags}",
Colin Crossd3ba0392015-05-07 14:11:29 -0700581 fmt.Sprintf("${%sGlobalCflags}", ctx.HostOrDevice()))
Colin Cross3f40fa42015-01-30 17:27:36 -0800582 }
583
Colin Cross7b66f152015-12-15 16:07:43 -0800584 if Bool(ctx.AConfig().ProductVariables.Brillo) {
585 flags.GlobalFlags = append(flags.GlobalFlags, "-D__BRILLO__")
586 }
587
Colin Crossf6566ed2015-03-24 11:13:38 -0700588 if ctx.Device() {
Colin Cross06a931b2015-10-28 17:23:31 -0700589 if Bool(c.Properties.Rtti) {
Colin Cross97ba0732015-03-23 17:50:24 -0700590 flags.CppFlags = append(flags.CppFlags, "-frtti")
Colin Cross3f40fa42015-01-30 17:27:36 -0800591 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700592 flags.CppFlags = append(flags.CppFlags, "-fno-rtti")
Colin Cross3f40fa42015-01-30 17:27:36 -0800593 }
594 }
595
Colin Cross97ba0732015-03-23 17:50:24 -0700596 flags.AsFlags = append(flags.AsFlags, "-D__ASSEMBLY__")
Colin Cross3f40fa42015-01-30 17:27:36 -0800597
Colin Cross97ba0732015-03-23 17:50:24 -0700598 if flags.Clang {
599 flags.CppFlags = append(flags.CppFlags, toolchain.ClangCppflags())
600 flags.LdFlags = append(flags.LdFlags, toolchain.ClangLdflags())
Colin Cross3f40fa42015-01-30 17:27:36 -0800601 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700602 flags.CppFlags = append(flags.CppFlags, toolchain.Cppflags())
603 flags.LdFlags = append(flags.LdFlags, toolchain.Ldflags())
Colin Cross3f40fa42015-01-30 17:27:36 -0800604 }
Colin Cross28344522015-04-22 13:07:53 -0700605
606 if ctx.Host() {
Colin Crossfa138792015-04-24 17:31:52 -0700607 flags.LdFlags = append(flags.LdFlags, c.Properties.Host_ldlibs...)
Colin Cross28344522015-04-22 13:07:53 -0700608 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800609 }
610
Colin Crossc4bde762015-11-23 16:11:30 -0800611 if flags.Clang {
612 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainClangCflags())
613 } else {
614 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainCflags())
615 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainLdflags())
616 }
617
Colin Cross0676e2d2015-04-24 17:39:18 -0700618 flags = c.ccModuleType().flags(ctx, flags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800619
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700620 if c.Properties.Sdk_version == "" {
621 if ctx.Host() && !flags.Clang {
622 // The host GCC doesn't support C++14 (and is deprecated, so likely
623 // never will). Build these modules with C++11.
624 flags.CppFlags = append(flags.CppFlags, "-std=gnu++11")
625 } else {
626 flags.CppFlags = append(flags.CppFlags, "-std=gnu++14")
627 }
628 }
629
630 flags.CFlags, _ = filterList(flags.CFlags, illegalFlags)
631 flags.CppFlags, _ = filterList(flags.CppFlags, illegalFlags)
632 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, illegalFlags)
633
Colin Cross3f40fa42015-01-30 17:27:36 -0800634 // Optimization to reduce size of build.ninja
635 // Replace the long list of flags for each file with a module-local variable
Colin Cross97ba0732015-03-23 17:50:24 -0700636 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
637 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
638 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
639 flags.CFlags = []string{"$cflags"}
640 flags.CppFlags = []string{"$cppflags"}
641 flags.AsFlags = []string{"$asflags"}
Colin Cross3f40fa42015-01-30 17:27:36 -0800642
643 return flags
644}
645
Colin Cross0676e2d2015-04-24 17:39:18 -0700646func (c *CCBase) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Cross3f40fa42015-01-30 17:27:36 -0800647 return flags
648}
649
650// Compile a list of source files into objects a specified subdirectory
Colin Crossfa138792015-04-24 17:31:52 -0700651func (c *CCBase) customCompileObjs(ctx common.AndroidModuleContext, flags CCFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700652 subdir string, srcFiles, excludes []string) common.Paths {
Colin Cross581c1892015-04-07 16:50:10 -0700653
654 buildFlags := ccFlagsToBuilderFlags(flags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800655
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700656 inputFiles := ctx.ExpandSources(srcFiles, excludes)
657 srcPaths, deps := genSources(ctx, inputFiles, buildFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800658
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700659 return TransformSourceToObj(ctx, subdir, srcPaths, buildFlags, deps)
Colin Cross3f40fa42015-01-30 17:27:36 -0800660}
661
Colin Crossfa138792015-04-24 17:31:52 -0700662// Compile files listed in c.Properties.Srcs into objects
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700663func (c *CCBase) compileObjs(ctx common.AndroidModuleContext, flags CCFlags) common.Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800664
Colin Crossfa138792015-04-24 17:31:52 -0700665 if c.Properties.SkipCompileObjs {
Colin Cross3f40fa42015-01-30 17:27:36 -0800666 return nil
667 }
668
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700669 return c.customCompileObjs(ctx, flags, "", c.Properties.Srcs, c.Properties.Exclude_srcs)
Colin Cross3f40fa42015-01-30 17:27:36 -0800670}
671
Colin Cross5049f022015-03-18 13:28:46 -0700672// Compile generated source files from dependencies
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700673func (c *CCBase) compileGeneratedObjs(ctx common.AndroidModuleContext, flags CCFlags) common.Paths {
674 var srcs common.Paths
Colin Cross5049f022015-03-18 13:28:46 -0700675
Colin Crossfa138792015-04-24 17:31:52 -0700676 if c.Properties.SkipCompileObjs {
Colin Cross5049f022015-03-18 13:28:46 -0700677 return nil
678 }
679
680 ctx.VisitDirectDeps(func(module blueprint.Module) {
681 if gen, ok := module.(genrule.SourceFileGenerator); ok {
682 srcs = append(srcs, gen.GeneratedSourceFiles()...)
683 }
684 })
685
686 if len(srcs) == 0 {
687 return nil
688 }
689
Colin Cross581c1892015-04-07 16:50:10 -0700690 return TransformSourceToObj(ctx, "", srcs, ccFlagsToBuilderFlags(flags), nil)
Colin Cross5049f022015-03-18 13:28:46 -0700691}
692
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700693func (c *CCBase) outputFile() common.OptionalPath {
694 return common.OptionalPath{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800695}
696
Colin Crossfa138792015-04-24 17:31:52 -0700697func (c *CCBase) depsToPathsFromList(ctx common.AndroidModuleContext,
Colin Cross3f40fa42015-01-30 17:27:36 -0800698 names []string) (modules []common.AndroidModule,
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700699 outputFiles common.Paths, exportedFlags []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800700
701 for _, n := range names {
702 found := false
703 ctx.VisitDirectDeps(func(m blueprint.Module) {
704 otherName := ctx.OtherModuleName(m)
705 if otherName != n {
706 return
707 }
708
Colin Cross97ba0732015-03-23 17:50:24 -0700709 if a, ok := m.(CCModuleType); ok {
Dan Willemsen0effe062015-11-30 16:06:01 -0800710 if !a.Enabled() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800711 // If a cc_library host+device module depends on a library that exists as both
712 // cc_library_shared and cc_library_host_shared, it will end up with two
713 // dependencies with the same name, one of which is marked disabled for each
714 // of host and device. Ignore the disabled one.
715 return
716 }
Colin Crossd3ba0392015-05-07 14:11:29 -0700717 if a.HostOrDevice() != ctx.HostOrDevice() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800718 ctx.ModuleErrorf("host/device mismatch between %q and %q", ctx.ModuleName(),
719 otherName)
720 return
721 }
722
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700723 if outputFile := a.outputFile(); outputFile.Valid() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800724 if found {
725 ctx.ModuleErrorf("multiple modules satisified dependency on %q", otherName)
726 return
727 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700728 outputFiles = append(outputFiles, outputFile.Path())
Colin Cross3f40fa42015-01-30 17:27:36 -0800729 modules = append(modules, a)
Colin Cross28344522015-04-22 13:07:53 -0700730 if i, ok := a.(ccExportedFlagsProducer); ok {
731 exportedFlags = append(exportedFlags, i.exportedFlags()...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800732 }
733 found = true
734 } else {
735 ctx.ModuleErrorf("module %q missing output file", otherName)
736 return
737 }
738 } else {
739 ctx.ModuleErrorf("module %q not an android module", otherName)
740 return
741 }
742 })
Colin Cross6ff51382015-12-17 16:39:19 -0800743 if !found && !inList(n, ctx.GetMissingDependencies()) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800744 ctx.ModuleErrorf("unsatisified dependency on %q", n)
745 }
746 }
747
Colin Cross28344522015-04-22 13:07:53 -0700748 return modules, outputFiles, exportedFlags
Colin Cross3f40fa42015-01-30 17:27:36 -0800749}
750
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700751// Convert dependency names to paths. Takes a CCDeps containing names and returns a CCPathDeps
Colin Cross21b9a242015-03-24 14:15:58 -0700752// containing paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700753func (c *CCBase) depsToPaths(ctx common.AndroidModuleContext, depNames CCDeps) CCPathDeps {
754 var depPaths CCPathDeps
Colin Cross28344522015-04-22 13:07:53 -0700755 var newCflags []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800756
Colin Cross21b9a242015-03-24 14:15:58 -0700757 var wholeStaticLibModules []common.AndroidModule
Colin Cross3f40fa42015-01-30 17:27:36 -0800758
Colin Cross28344522015-04-22 13:07:53 -0700759 wholeStaticLibModules, depPaths.WholeStaticLibs, newCflags =
Colin Cross21b9a242015-03-24 14:15:58 -0700760 c.depsToPathsFromList(ctx, depNames.WholeStaticLibs)
Colin Cross28344522015-04-22 13:07:53 -0700761 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Crossa48f71f2015-11-16 18:00:41 -0800762 depPaths.ReexportedCflags = append(depPaths.ReexportedCflags, newCflags...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800763
Colin Cross21b9a242015-03-24 14:15:58 -0700764 for _, m := range wholeStaticLibModules {
765 if staticLib, ok := m.(ccLibraryInterface); ok && staticLib.static() {
766 depPaths.WholeStaticLibObjFiles =
767 append(depPaths.WholeStaticLibObjFiles, staticLib.allObjFiles()...)
768 } else {
769 ctx.ModuleErrorf("module %q not a static library", ctx.OtherModuleName(m))
770 }
771 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800772
Colin Cross28344522015-04-22 13:07:53 -0700773 _, depPaths.StaticLibs, newCflags = c.depsToPathsFromList(ctx, depNames.StaticLibs)
774 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross21b9a242015-03-24 14:15:58 -0700775
Colin Cross28344522015-04-22 13:07:53 -0700776 _, depPaths.LateStaticLibs, newCflags = c.depsToPathsFromList(ctx, depNames.LateStaticLibs)
777 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross21b9a242015-03-24 14:15:58 -0700778
Colin Cross28344522015-04-22 13:07:53 -0700779 _, depPaths.SharedLibs, newCflags = c.depsToPathsFromList(ctx, depNames.SharedLibs)
780 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross21b9a242015-03-24 14:15:58 -0700781
782 ctx.VisitDirectDeps(func(m blueprint.Module) {
Dan Albertc3144b12015-04-28 18:17:56 -0700783 if obj, ok := m.(ccObjectProvider); ok {
Colin Cross21b9a242015-03-24 14:15:58 -0700784 otherName := ctx.OtherModuleName(m)
785 if otherName == depNames.CrtBegin {
Colin Cross06a931b2015-10-28 17:23:31 -0700786 if !Bool(c.Properties.Nocrt) {
Dan Albertc3144b12015-04-28 18:17:56 -0700787 depPaths.CrtBegin = obj.object().outputFile()
Colin Cross21b9a242015-03-24 14:15:58 -0700788 }
789 } else if otherName == depNames.CrtEnd {
Colin Cross06a931b2015-10-28 17:23:31 -0700790 if !Bool(c.Properties.Nocrt) {
Dan Albertc3144b12015-04-28 18:17:56 -0700791 depPaths.CrtEnd = obj.object().outputFile()
Colin Cross21b9a242015-03-24 14:15:58 -0700792 }
793 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700794 output := obj.object().outputFile()
795 if output.Valid() {
796 depPaths.ObjFiles = append(depPaths.ObjFiles, output.Path())
797 } else {
798 ctx.ModuleErrorf("module %s did not provide an output file", otherName)
799 }
Colin Cross21b9a242015-03-24 14:15:58 -0700800 }
801 }
802 })
803
804 return depPaths
Colin Cross3f40fa42015-01-30 17:27:36 -0800805}
806
Colin Cross7d5136f2015-05-11 13:39:40 -0700807type ccLinkedProperties struct {
808 VariantIsShared bool `blueprint:"mutated"`
809 VariantIsStatic bool `blueprint:"mutated"`
810 VariantIsStaticBinary bool `blueprint:"mutated"`
811}
812
Colin Crossfa138792015-04-24 17:31:52 -0700813// CCLinked contains the properties and members used by libraries and executables
814type CCLinked struct {
815 CCBase
Colin Cross7d5136f2015-05-11 13:39:40 -0700816 dynamicProperties ccLinkedProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800817}
818
Colin Crossfa138792015-04-24 17:31:52 -0700819func newCCDynamic(dynamic *CCLinked, module CCModuleType, hod common.HostOrDeviceSupported,
Colin Crossc472d572015-03-17 15:06:21 -0700820 multilib common.Multilib, props ...interface{}) (blueprint.Module, []interface{}) {
821
Colin Crossed4cf0b2015-03-26 14:43:45 -0700822 props = append(props, &dynamic.dynamicProperties)
823
Colin Crossfa138792015-04-24 17:31:52 -0700824 return newCCBase(&dynamic.CCBase, module, hod, multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700825}
826
Colin Crossfa138792015-04-24 17:31:52 -0700827func (c *CCLinked) systemSharedLibs(ctx common.AndroidBaseContext) []string {
Colin Cross06a931b2015-10-28 17:23:31 -0700828 if c.Properties.System_shared_libs != nil {
Colin Crossfa138792015-04-24 17:31:52 -0700829 return c.Properties.System_shared_libs
830 } else if ctx.Device() && c.Properties.Sdk_version == "" {
Colin Cross577f6e42015-03-27 18:23:34 -0700831 return []string{"libc", "libm"}
Colin Cross28d76592015-03-26 16:14:04 -0700832 } else {
Colin Cross577f6e42015-03-27 18:23:34 -0700833 return nil
Colin Cross3f40fa42015-01-30 17:27:36 -0800834 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800835}
836
Colin Crossfa138792015-04-24 17:31:52 -0700837func (c *CCLinked) stl(ctx common.AndroidBaseContext) string {
838 if c.Properties.Sdk_version != "" && ctx.Device() {
839 switch c.Properties.Stl {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700840 case "":
841 return "ndk_system"
842 case "c++_shared", "c++_static",
843 "stlport_shared", "stlport_static",
844 "gnustl_static":
Colin Crossfa138792015-04-24 17:31:52 -0700845 return "ndk_lib" + c.Properties.Stl
Colin Crossed4cf0b2015-03-26 14:43:45 -0700846 default:
Colin Crossfa138792015-04-24 17:31:52 -0700847 ctx.ModuleErrorf("stl: %q is not a supported STL with sdk_version set", c.Properties.Stl)
Colin Crossed4cf0b2015-03-26 14:43:45 -0700848 return ""
849 }
850 }
851
Dan Willemsen490fd492015-11-24 17:53:15 -0800852 if ctx.HostType() == common.Windows {
853 switch c.Properties.Stl {
854 case "libc++", "libc++_static", "libstdc++", "":
855 // libc++ is not supported on mingw
856 return "libstdc++"
857 case "none":
858 return ""
859 default:
860 ctx.ModuleErrorf("stl: %q is not a supported STL", c.Properties.Stl)
861 return ""
Colin Crossed4cf0b2015-03-26 14:43:45 -0700862 }
Dan Willemsen490fd492015-11-24 17:53:15 -0800863 } else {
864 switch c.Properties.Stl {
865 case "libc++", "libc++_static",
866 "libstdc++":
867 return c.Properties.Stl
868 case "none":
869 return ""
870 case "":
871 if c.static() {
872 return "libc++_static"
873 } else {
874 return "libc++"
875 }
876 default:
877 ctx.ModuleErrorf("stl: %q is not a supported STL", c.Properties.Stl)
878 return ""
879 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700880 }
881}
882
Dan Willemsen490fd492015-11-24 17:53:15 -0800883var hostDynamicGccLibs, hostStaticGccLibs map[common.HostType][]string
Colin Cross0af4b842015-04-30 16:36:18 -0700884
885func init() {
Dan Willemsen490fd492015-11-24 17:53:15 -0800886 hostDynamicGccLibs = map[common.HostType][]string{
887 common.Linux: []string{"-lgcc_s", "-lgcc", "-lc", "-lgcc_s", "-lgcc"},
888 common.Darwin: []string{"-lc", "-lSystem"},
889 common.Windows: []string{"-lmsvcr110", "-lmingw32", "-lgcc", "-lmoldname",
890 "-lmingwex", "-lmsvcrt", "-ladvapi32", "-lshell32", "-luser32",
891 "-lkernel32", "-lmingw32", "-lgcc", "-lmoldname", "-lmingwex",
892 "-lmsvcrt"},
893 }
894 hostStaticGccLibs = map[common.HostType][]string{
895 common.Linux: []string{"-Wl,--start-group", "-lgcc", "-lgcc_eh", "-lc", "-Wl,--end-group"},
896 common.Darwin: []string{"NO_STATIC_HOST_BINARIES_ON_DARWIN"},
897 common.Windows: []string{"NO_STATIC_HOST_BINARIES_ON_WINDOWS"},
Colin Cross0af4b842015-04-30 16:36:18 -0700898 }
899}
Colin Cross712fc022015-04-27 11:13:34 -0700900
Colin Crosse11befc2015-04-27 17:49:17 -0700901func (c *CCLinked) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700902 stl := c.stl(ctx)
903 if ctx.Failed() {
904 return flags
905 }
906
907 switch stl {
908 case "libc++", "libc++_static":
909 flags.CFlags = append(flags.CFlags, "-D_USING_LIBCXX")
Colin Crossed4cf0b2015-03-26 14:43:45 -0700910 if ctx.Host() {
911 flags.CppFlags = append(flags.CppFlags, "-nostdinc++")
912 flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs")
Colin Cross712fc022015-04-27 11:13:34 -0700913 flags.LdFlags = append(flags.LdFlags, "-lm", "-lpthread")
Colin Cross18b6dc52015-04-28 13:20:37 -0700914 if c.staticBinary() {
Dan Willemsen490fd492015-11-24 17:53:15 -0800915 flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs[ctx.HostType()]...)
Colin Cross18b6dc52015-04-28 13:20:37 -0700916 } else {
Dan Willemsen490fd492015-11-24 17:53:15 -0800917 flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs[ctx.HostType()]...)
Colin Cross712fc022015-04-27 11:13:34 -0700918 }
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700919 } else {
920 if ctx.Arch().ArchType == common.Arm {
921 flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,libunwind_llvm.a")
922 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700923 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700924 case "libstdc++":
925 // Using bionic's basic libstdc++. Not actually an STL. Only around until the
926 // tree is in good enough shape to not need it.
927 // Host builds will use GNU libstdc++.
928 if ctx.Device() {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700929 flags.CFlags = append(flags.CFlags, "-I"+common.PathForSource(ctx, "bionic/libstdc++/include").String())
Colin Crossed4cf0b2015-03-26 14:43:45 -0700930 }
931 case "ndk_system":
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700932 ndkSrcRoot := common.PathForSource(ctx, "prebuilts/ndk/current/sources/cxx-stl/system/include")
933 flags.CFlags = append(flags.CFlags, "-isystem "+ndkSrcRoot.String())
Colin Crossed4cf0b2015-03-26 14:43:45 -0700934 case "ndk_libc++_shared", "ndk_libc++_static":
935 // TODO(danalbert): This really shouldn't be here...
936 flags.CppFlags = append(flags.CppFlags, "-std=c++11")
937 case "ndk_libstlport_shared", "ndk_libstlport_static", "ndk_libgnustl_static":
938 // Nothing
939 case "":
940 // None or error.
941 if ctx.Host() {
942 flags.CppFlags = append(flags.CppFlags, "-nostdinc++")
943 flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs")
Colin Cross18b6dc52015-04-28 13:20:37 -0700944 if c.staticBinary() {
Dan Willemsen490fd492015-11-24 17:53:15 -0800945 flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs[ctx.HostType()]...)
Colin Cross18b6dc52015-04-28 13:20:37 -0700946 } else {
Dan Willemsen490fd492015-11-24 17:53:15 -0800947 flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs[ctx.HostType()]...)
Colin Cross712fc022015-04-27 11:13:34 -0700948 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700949 }
950 default:
Colin Crossfa138792015-04-24 17:31:52 -0700951 panic(fmt.Errorf("Unknown stl in CCLinked.Flags: %q", stl))
Colin Crossed4cf0b2015-03-26 14:43:45 -0700952 }
953
954 return flags
955}
956
Colin Crosse11befc2015-04-27 17:49:17 -0700957func (c *CCLinked) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
958 depNames = c.CCBase.depNames(ctx, depNames)
Colin Cross3f40fa42015-01-30 17:27:36 -0800959
Colin Crossed4cf0b2015-03-26 14:43:45 -0700960 stl := c.stl(ctx)
961 if ctx.Failed() {
962 return depNames
963 }
964
965 switch stl {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700966 case "libstdc++":
967 if ctx.Device() {
968 depNames.SharedLibs = append(depNames.SharedLibs, stl)
969 }
Colin Cross74d1ec02015-04-28 13:30:13 -0700970 case "libc++", "libc++_static":
971 if stl == "libc++" {
972 depNames.SharedLibs = append(depNames.SharedLibs, stl)
973 } else {
974 depNames.StaticLibs = append(depNames.StaticLibs, stl)
975 }
976 if ctx.Device() {
977 if ctx.Arch().ArchType == common.Arm {
978 depNames.StaticLibs = append(depNames.StaticLibs, "libunwind_llvm")
979 }
980 if c.staticBinary() {
981 depNames.StaticLibs = append(depNames.StaticLibs, "libdl")
982 } else {
983 depNames.SharedLibs = append(depNames.SharedLibs, "libdl")
984 }
985 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700986 case "":
987 // None or error.
988 case "ndk_system":
989 // TODO: Make a system STL prebuilt for the NDK.
990 // 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 -0700991 // its own includes. The includes are handled in CCBase.Flags().
Colin Cross577f6e42015-03-27 18:23:34 -0700992 depNames.SharedLibs = append([]string{"libstdc++"}, depNames.SharedLibs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -0700993 case "ndk_libc++_shared", "ndk_libstlport_shared":
994 depNames.SharedLibs = append(depNames.SharedLibs, stl)
995 case "ndk_libc++_static", "ndk_libstlport_static", "ndk_libgnustl_static":
996 depNames.StaticLibs = append(depNames.StaticLibs, stl)
997 default:
Colin Crosse11befc2015-04-27 17:49:17 -0700998 panic(fmt.Errorf("Unknown stl in CCLinked.depNames: %q", stl))
Colin Crossed4cf0b2015-03-26 14:43:45 -0700999 }
1000
Colin Cross74d1ec02015-04-28 13:30:13 -07001001 if ctx.ModuleName() != "libcompiler_rt-extras" {
1002 depNames.StaticLibs = append(depNames.StaticLibs, "libcompiler_rt-extras")
1003 }
1004
Colin Crossf6566ed2015-03-24 11:13:38 -07001005 if ctx.Device() {
Colin Cross77b00fa2015-03-16 16:15:49 -07001006 // libgcc and libatomic have to be last on the command line
Dan Willemsend67be222015-09-16 15:19:33 -07001007 depNames.LateStaticLibs = append(depNames.LateStaticLibs, "libgcov", "libatomic")
Colin Cross06a931b2015-10-28 17:23:31 -07001008 if !Bool(c.Properties.No_libgcc) {
Dan Willemsend67be222015-09-16 15:19:33 -07001009 depNames.LateStaticLibs = append(depNames.LateStaticLibs, "libgcc")
1010 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001011
Colin Cross18b6dc52015-04-28 13:20:37 -07001012 if !c.static() {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001013 depNames.SharedLibs = append(depNames.SharedLibs, c.systemSharedLibs(ctx)...)
1014 }
Colin Cross577f6e42015-03-27 18:23:34 -07001015
Colin Crossfa138792015-04-24 17:31:52 -07001016 if c.Properties.Sdk_version != "" {
1017 version := c.Properties.Sdk_version
Colin Cross577f6e42015-03-27 18:23:34 -07001018 depNames.SharedLibs = append(depNames.SharedLibs,
1019 "ndk_libc."+version,
1020 "ndk_libm."+version,
1021 )
1022 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001023 }
1024
Colin Cross21b9a242015-03-24 14:15:58 -07001025 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -08001026}
1027
Colin Crossed4cf0b2015-03-26 14:43:45 -07001028// ccLinkedInterface interface is used on ccLinked to deal with static or shared variants
1029type ccLinkedInterface interface {
1030 // Returns true if the build options for the module have selected a static or shared build
1031 buildStatic() bool
1032 buildShared() bool
1033
1034 // Sets whether a specific variant is static or shared
Colin Cross18b6dc52015-04-28 13:20:37 -07001035 setStatic(bool)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001036
Colin Cross18b6dc52015-04-28 13:20:37 -07001037 // Returns whether a specific variant is a static library or binary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001038 static() bool
Colin Cross18b6dc52015-04-28 13:20:37 -07001039
1040 // Returns whether a module is a static binary
1041 staticBinary() bool
Colin Crossed4cf0b2015-03-26 14:43:45 -07001042}
1043
1044var _ ccLinkedInterface = (*CCLibrary)(nil)
1045var _ ccLinkedInterface = (*CCBinary)(nil)
1046
Colin Crossfa138792015-04-24 17:31:52 -07001047func (c *CCLinked) static() bool {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001048 return c.dynamicProperties.VariantIsStatic
1049}
1050
Colin Cross18b6dc52015-04-28 13:20:37 -07001051func (c *CCLinked) staticBinary() bool {
1052 return c.dynamicProperties.VariantIsStaticBinary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001053}
1054
Colin Cross18b6dc52015-04-28 13:20:37 -07001055func (c *CCLinked) setStatic(static bool) {
1056 c.dynamicProperties.VariantIsStatic = static
Colin Crossed4cf0b2015-03-26 14:43:45 -07001057}
1058
Colin Cross28344522015-04-22 13:07:53 -07001059type ccExportedFlagsProducer interface {
1060 exportedFlags() []string
Colin Cross3f40fa42015-01-30 17:27:36 -08001061}
1062
1063//
1064// Combined static+shared libraries
1065//
1066
Colin Cross7d5136f2015-05-11 13:39:40 -07001067type CCLibraryProperties struct {
1068 BuildStatic bool `blueprint:"mutated"`
1069 BuildShared bool `blueprint:"mutated"`
1070 Static struct {
1071 Srcs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001072 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -07001073 Cflags []string `android:"arch_variant"`
1074 Whole_static_libs []string `android:"arch_variant"`
1075 Static_libs []string `android:"arch_variant"`
1076 Shared_libs []string `android:"arch_variant"`
1077 } `android:"arch_variant"`
1078 Shared struct {
1079 Srcs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001080 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -07001081 Cflags []string `android:"arch_variant"`
1082 Whole_static_libs []string `android:"arch_variant"`
1083 Static_libs []string `android:"arch_variant"`
1084 Shared_libs []string `android:"arch_variant"`
1085 } `android:"arch_variant"`
Colin Crossaee540a2015-07-06 17:48:31 -07001086
1087 // local file name to pass to the linker as --version_script
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001088 Version_script *string `android:"arch_variant"`
Dan Willemsen93c28312015-12-04 14:59:08 -08001089 // local file name to pass to the linker as -unexported_symbols_list
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001090 Unexported_symbols_list *string `android:"arch_variant"`
Dan Willemsen93c28312015-12-04 14:59:08 -08001091 // local file name to pass to the linker as -force_symbols_not_weak_list
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001092 Force_symbols_not_weak_list *string `android:"arch_variant"`
Dan Willemsen93c28312015-12-04 14:59:08 -08001093 // local file name to pass to the linker as -force_symbols_weak_list
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001094 Force_symbols_weak_list *string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -07001095}
1096
Colin Cross97ba0732015-03-23 17:50:24 -07001097type CCLibrary struct {
Colin Crossfa138792015-04-24 17:31:52 -07001098 CCLinked
Colin Cross3f40fa42015-01-30 17:27:36 -08001099
Colin Cross28344522015-04-22 13:07:53 -07001100 reuseFrom ccLibraryInterface
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001101 reuseObjFiles common.Paths
1102 objFiles common.Paths
Colin Cross28344522015-04-22 13:07:53 -07001103 exportFlags []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001104 out common.Path
Dan Willemsen218f6562015-07-08 18:13:11 -07001105 systemLibs []string
Colin Cross3f40fa42015-01-30 17:27:36 -08001106
Colin Cross7d5136f2015-05-11 13:39:40 -07001107 LibraryProperties CCLibraryProperties
Colin Cross3f40fa42015-01-30 17:27:36 -08001108}
1109
Colin Crossed4cf0b2015-03-26 14:43:45 -07001110func (c *CCLibrary) buildStatic() bool {
1111 return c.LibraryProperties.BuildStatic
1112}
1113
1114func (c *CCLibrary) buildShared() bool {
1115 return c.LibraryProperties.BuildShared
1116}
1117
Colin Cross97ba0732015-03-23 17:50:24 -07001118type ccLibraryInterface interface {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001119 ccLinkedInterface
Colin Cross97ba0732015-03-23 17:50:24 -07001120 ccLibrary() *CCLibrary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001121 setReuseFrom(ccLibraryInterface)
1122 getReuseFrom() ccLibraryInterface
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001123 getReuseObjFiles() common.Paths
1124 allObjFiles() common.Paths
Colin Crossc472d572015-03-17 15:06:21 -07001125}
1126
Colin Crossed4cf0b2015-03-26 14:43:45 -07001127var _ ccLibraryInterface = (*CCLibrary)(nil)
1128
Colin Cross97ba0732015-03-23 17:50:24 -07001129func (c *CCLibrary) ccLibrary() *CCLibrary {
1130 return c
Colin Cross3f40fa42015-01-30 17:27:36 -08001131}
1132
Colin Cross97ba0732015-03-23 17:50:24 -07001133func NewCCLibrary(library *CCLibrary, module CCModuleType,
1134 hod common.HostOrDeviceSupported) (blueprint.Module, []interface{}) {
1135
Colin Crossfa138792015-04-24 17:31:52 -07001136 return newCCDynamic(&library.CCLinked, module, hod, common.MultilibBoth,
Colin Cross97ba0732015-03-23 17:50:24 -07001137 &library.LibraryProperties)
1138}
1139
1140func CCLibraryFactory() (blueprint.Module, []interface{}) {
1141 module := &CCLibrary{}
1142
1143 module.LibraryProperties.BuildShared = true
1144 module.LibraryProperties.BuildStatic = true
1145
1146 return NewCCLibrary(module, module, common.HostAndDeviceSupported)
1147}
1148
Colin Cross0676e2d2015-04-24 17:39:18 -07001149func (c *CCLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Crosse11befc2015-04-27 17:49:17 -07001150 depNames = c.CCLinked.depNames(ctx, depNames)
Colin Cross2732e9a2015-04-28 13:23:52 -07001151 if c.static() {
1152 depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.LibraryProperties.Static.Whole_static_libs...)
1153 depNames.StaticLibs = append(depNames.StaticLibs, c.LibraryProperties.Static.Static_libs...)
1154 depNames.SharedLibs = append(depNames.SharedLibs, c.LibraryProperties.Static.Shared_libs...)
1155 } else {
Colin Crossf6566ed2015-03-24 11:13:38 -07001156 if ctx.Device() {
Dan Albertc3144b12015-04-28 18:17:56 -07001157 if c.Properties.Sdk_version == "" {
1158 depNames.CrtBegin = "crtbegin_so"
1159 depNames.CrtEnd = "crtend_so"
1160 } else {
1161 depNames.CrtBegin = "ndk_crtbegin_so." + c.Properties.Sdk_version
1162 depNames.CrtEnd = "ndk_crtend_so." + c.Properties.Sdk_version
1163 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001164 }
Colin Cross2732e9a2015-04-28 13:23:52 -07001165 depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.LibraryProperties.Shared.Whole_static_libs...)
1166 depNames.StaticLibs = append(depNames.StaticLibs, c.LibraryProperties.Shared.Static_libs...)
1167 depNames.SharedLibs = append(depNames.SharedLibs, c.LibraryProperties.Shared.Shared_libs...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001168 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001169
Dan Willemsen218f6562015-07-08 18:13:11 -07001170 c.systemLibs = c.systemSharedLibs(ctx)
1171
Colin Cross21b9a242015-03-24 14:15:58 -07001172 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -08001173}
1174
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001175func (c *CCLibrary) outputFile() common.OptionalPath {
1176 return common.OptionalPathForPath(c.out)
Colin Cross3f40fa42015-01-30 17:27:36 -08001177}
1178
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001179func (c *CCLibrary) getReuseObjFiles() common.Paths {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001180 return c.reuseObjFiles
1181}
1182
1183func (c *CCLibrary) setReuseFrom(reuseFrom ccLibraryInterface) {
1184 c.reuseFrom = reuseFrom
1185}
1186
1187func (c *CCLibrary) getReuseFrom() ccLibraryInterface {
1188 return c.reuseFrom
1189}
1190
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001191func (c *CCLibrary) allObjFiles() common.Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -08001192 return c.objFiles
1193}
1194
Colin Cross28344522015-04-22 13:07:53 -07001195func (c *CCLibrary) exportedFlags() []string {
1196 return c.exportFlags
Colin Cross3f40fa42015-01-30 17:27:36 -08001197}
1198
Colin Cross0676e2d2015-04-24 17:39:18 -07001199func (c *CCLibrary) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Crosse11befc2015-04-27 17:49:17 -07001200 flags = c.CCLinked.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001201
Dan Willemsen490fd492015-11-24 17:53:15 -08001202 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
1203 // all code is position independent, and then those warnings get promoted to
1204 // errors.
1205 if ctx.HostType() != common.Windows {
1206 flags.CFlags = append(flags.CFlags, "-fPIC")
1207 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001208
Colin Crossd8e780d2015-04-28 17:39:43 -07001209 if c.static() {
1210 flags.CFlags = append(flags.CFlags, c.LibraryProperties.Static.Cflags...)
1211 } else {
1212 flags.CFlags = append(flags.CFlags, c.LibraryProperties.Shared.Cflags...)
1213 }
1214
Colin Cross18b6dc52015-04-28 13:20:37 -07001215 if !c.static() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001216 libName := ctx.ModuleName()
1217 // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead
1218 sharedFlag := "-Wl,-shared"
Dan Willemsendd0e2c32015-10-20 14:29:35 -07001219 if flags.Clang || ctx.Host() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001220 sharedFlag = "-shared"
1221 }
Colin Crossf6566ed2015-03-24 11:13:38 -07001222 if ctx.Device() {
Colin Cross97ba0732015-03-23 17:50:24 -07001223 flags.LdFlags = append(flags.LdFlags, "-nostdlib")
Colin Cross3f40fa42015-01-30 17:27:36 -08001224 }
Colin Cross97ba0732015-03-23 17:50:24 -07001225
Colin Cross0af4b842015-04-30 16:36:18 -07001226 if ctx.Darwin() {
1227 flags.LdFlags = append(flags.LdFlags,
1228 "-dynamiclib",
1229 "-single_module",
1230 //"-read_only_relocs suppress",
Dan Willemsen490fd492015-11-24 17:53:15 -08001231 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
Colin Cross0af4b842015-04-30 16:36:18 -07001232 )
1233 } else {
1234 flags.LdFlags = append(flags.LdFlags,
1235 "-Wl,--gc-sections",
1236 sharedFlag,
Dan Willemsen490fd492015-11-24 17:53:15 -08001237 "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix(),
Colin Cross0af4b842015-04-30 16:36:18 -07001238 )
1239 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001240 }
Colin Cross97ba0732015-03-23 17:50:24 -07001241
1242 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001243}
1244
Colin Cross97ba0732015-03-23 17:50:24 -07001245func (c *CCLibrary) compileStaticLibrary(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001246 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001247
1248 staticFlags := flags
Colin Cross581c1892015-04-07 16:50:10 -07001249 objFilesStatic := c.customCompileObjs(ctx, staticFlags, common.DeviceStaticLibrary,
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001250 c.LibraryProperties.Static.Srcs, c.LibraryProperties.Static.Exclude_srcs)
Colin Cross3f40fa42015-01-30 17:27:36 -08001251
1252 objFiles = append(objFiles, objFilesStatic...)
Colin Cross21b9a242015-03-24 14:15:58 -07001253 objFiles = append(objFiles, deps.WholeStaticLibObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001254
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001255 outputFile := common.PathForModuleOut(ctx, ctx.ModuleName()+staticLibraryExtension)
Colin Cross3f40fa42015-01-30 17:27:36 -08001256
Colin Cross0af4b842015-04-30 16:36:18 -07001257 if ctx.Darwin() {
1258 TransformDarwinObjToStaticLib(ctx, objFiles, ccFlagsToBuilderFlags(flags), outputFile)
1259 } else {
1260 TransformObjToStaticLib(ctx, objFiles, ccFlagsToBuilderFlags(flags), outputFile)
1261 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001262
1263 c.objFiles = objFiles
1264 c.out = outputFile
Colin Crossf2298272015-05-12 11:36:53 -07001265
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001266 includeDirs := common.PathsForModuleSrc(ctx, c.Properties.Export_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -07001267 c.exportFlags = []string{includeDirsToFlags(includeDirs)}
Colin Crossa48f71f2015-11-16 18:00:41 -08001268 c.exportFlags = append(c.exportFlags, deps.ReexportedCflags...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001269
1270 ctx.CheckbuildFile(outputFile)
1271}
1272
Colin Cross97ba0732015-03-23 17:50:24 -07001273func (c *CCLibrary) compileSharedLibrary(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001274 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001275
1276 sharedFlags := flags
Colin Cross581c1892015-04-07 16:50:10 -07001277 objFilesShared := c.customCompileObjs(ctx, sharedFlags, common.DeviceSharedLibrary,
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001278 c.LibraryProperties.Shared.Srcs, c.LibraryProperties.Shared.Exclude_srcs)
Colin Cross3f40fa42015-01-30 17:27:36 -08001279
1280 objFiles = append(objFiles, objFilesShared...)
1281
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001282 outputFile := common.PathForModuleOut(ctx, ctx.ModuleName()+flags.Toolchain.ShlibSuffix())
Colin Cross3f40fa42015-01-30 17:27:36 -08001283
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001284 var linkerDeps common.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07001285
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001286 versionScript := common.OptionalPathForModuleSrc(ctx, c.LibraryProperties.Version_script)
1287 unexportedSymbols := common.OptionalPathForModuleSrc(ctx, c.LibraryProperties.Unexported_symbols_list)
1288 forceNotWeakSymbols := common.OptionalPathForModuleSrc(ctx, c.LibraryProperties.Force_symbols_not_weak_list)
1289 forceWeakSymbols := common.OptionalPathForModuleSrc(ctx, c.LibraryProperties.Force_symbols_weak_list)
Dan Willemsen93c28312015-12-04 14:59:08 -08001290 if !ctx.Darwin() {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001291 if versionScript.Valid() {
1292 sharedFlags.LdFlags = append(sharedFlags.LdFlags, "-Wl,--version-script,"+versionScript.String())
1293 linkerDeps = append(linkerDeps, versionScript.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001294 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001295 if unexportedSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001296 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
1297 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001298 if forceNotWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001299 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
1300 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001301 if forceWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001302 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
1303 }
1304 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001305 if versionScript.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001306 ctx.PropertyErrorf("version_script", "Not supported on Darwin")
1307 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001308 if unexportedSymbols.Valid() {
1309 sharedFlags.LdFlags = append(sharedFlags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
1310 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001311 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001312 if forceNotWeakSymbols.Valid() {
1313 sharedFlags.LdFlags = append(sharedFlags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
1314 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001315 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001316 if forceWeakSymbols.Valid() {
1317 sharedFlags.LdFlags = append(sharedFlags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
1318 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001319 }
Colin Crossaee540a2015-07-06 17:48:31 -07001320 }
1321
Colin Cross97ba0732015-03-23 17:50:24 -07001322 TransformObjToDynamicBinary(ctx, objFiles, deps.SharedLibs, deps.StaticLibs,
Colin Crossaee540a2015-07-06 17:48:31 -07001323 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, false,
Dan Willemsen6203ac02015-11-24 12:58:57 -08001324 ccFlagsToBuilderFlags(sharedFlags), outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08001325
1326 c.out = outputFile
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001327 includeDirs := common.PathsForModuleSrc(ctx, c.Properties.Export_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -07001328 c.exportFlags = []string{includeDirsToFlags(includeDirs)}
Colin Crossa48f71f2015-11-16 18:00:41 -08001329 c.exportFlags = append(c.exportFlags, deps.ReexportedCflags...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001330}
1331
Colin Cross97ba0732015-03-23 17:50:24 -07001332func (c *CCLibrary) compileModule(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001333 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001334
1335 // Reuse the object files from the matching static library if it exists
Colin Crossed4cf0b2015-03-26 14:43:45 -07001336 if c.getReuseFrom().ccLibrary() == c {
1337 c.reuseObjFiles = objFiles
Colin Cross3f40fa42015-01-30 17:27:36 -08001338 } else {
Colin Cross2732e9a2015-04-28 13:23:52 -07001339 if c.getReuseFrom().ccLibrary().LibraryProperties.Static.Cflags == nil &&
1340 c.LibraryProperties.Shared.Cflags == nil {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001341 objFiles = append(common.Paths(nil), c.getReuseFrom().getReuseObjFiles()...)
Colin Cross2732e9a2015-04-28 13:23:52 -07001342 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001343 }
1344
Colin Crossed4cf0b2015-03-26 14:43:45 -07001345 if c.static() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001346 c.compileStaticLibrary(ctx, flags, deps, objFiles)
1347 } else {
1348 c.compileSharedLibrary(ctx, flags, deps, objFiles)
1349 }
1350}
1351
Colin Cross97ba0732015-03-23 17:50:24 -07001352func (c *CCLibrary) installStaticLibrary(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001353 // Static libraries do not get installed.
1354}
1355
Colin Cross97ba0732015-03-23 17:50:24 -07001356func (c *CCLibrary) installSharedLibrary(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001357 installDir := "lib"
Colin Cross97ba0732015-03-23 17:50:24 -07001358 if flags.Toolchain.Is64Bit() {
Dan Albertc403f7c2015-03-18 14:01:18 -07001359 installDir = "lib64"
1360 }
1361
Colin Crossfa138792015-04-24 17:31:52 -07001362 ctx.InstallFile(filepath.Join(installDir, c.Properties.Relative_install_path), c.out)
Dan Albertc403f7c2015-03-18 14:01:18 -07001363}
1364
Colin Cross97ba0732015-03-23 17:50:24 -07001365func (c *CCLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001366 if c.static() {
Dan Albertc403f7c2015-03-18 14:01:18 -07001367 c.installStaticLibrary(ctx, flags)
1368 } else {
1369 c.installSharedLibrary(ctx, flags)
1370 }
1371}
1372
Colin Cross3f40fa42015-01-30 17:27:36 -08001373//
1374// Objects (for crt*.o)
1375//
1376
Dan Albertc3144b12015-04-28 18:17:56 -07001377type ccObjectProvider interface {
1378 object() *ccObject
1379}
1380
Colin Cross3f40fa42015-01-30 17:27:36 -08001381type ccObject struct {
Colin Crossfa138792015-04-24 17:31:52 -07001382 CCBase
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001383 out common.OptionalPath
Colin Cross3f40fa42015-01-30 17:27:36 -08001384}
1385
Dan Albertc3144b12015-04-28 18:17:56 -07001386func (c *ccObject) object() *ccObject {
1387 return c
1388}
1389
Colin Cross97ba0732015-03-23 17:50:24 -07001390func CCObjectFactory() (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001391 module := &ccObject{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001392
Colin Crossfa138792015-04-24 17:31:52 -07001393 return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth)
Colin Cross3f40fa42015-01-30 17:27:36 -08001394}
1395
Colin Cross0676e2d2015-04-24 17:39:18 -07001396func (*ccObject) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Cross21b9a242015-03-24 14:15:58 -07001397 // object files can't have any dynamic dependencies
1398 return CCDeps{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001399}
1400
1401func (c *ccObject) compileModule(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001402 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001403
Colin Cross97ba0732015-03-23 17:50:24 -07001404 objFiles = append(objFiles, deps.ObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001405
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001406 var outputFile common.Path
Colin Cross3f40fa42015-01-30 17:27:36 -08001407 if len(objFiles) == 1 {
1408 outputFile = objFiles[0]
1409 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001410 output := common.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension)
1411 TransformObjsToObj(ctx, objFiles, ccFlagsToBuilderFlags(flags), output)
1412 outputFile = output
Colin Cross3f40fa42015-01-30 17:27:36 -08001413 }
1414
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001415 c.out = common.OptionalPathForPath(outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08001416
1417 ctx.CheckbuildFile(outputFile)
1418}
1419
Colin Cross97ba0732015-03-23 17:50:24 -07001420func (c *ccObject) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001421 // Object files do not get installed.
1422}
1423
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001424func (c *ccObject) outputFile() common.OptionalPath {
Colin Cross3f40fa42015-01-30 17:27:36 -08001425 return c.out
1426}
1427
Dan Albertc3144b12015-04-28 18:17:56 -07001428var _ ccObjectProvider = (*ccObject)(nil)
1429
Colin Cross3f40fa42015-01-30 17:27:36 -08001430//
1431// Executables
1432//
1433
Colin Cross7d5136f2015-05-11 13:39:40 -07001434type CCBinaryProperties struct {
1435 // compile executable with -static
Colin Cross06a931b2015-10-28 17:23:31 -07001436 Static_executable *bool
Colin Cross7d5136f2015-05-11 13:39:40 -07001437
1438 // set the name of the output
1439 Stem string `android:"arch_variant"`
1440
1441 // append to the name of the output
1442 Suffix string `android:"arch_variant"`
1443
1444 // if set, add an extra objcopy --prefix-symbols= step
1445 Prefix_symbols string
Colin Cross6002e052015-09-16 16:00:08 -07001446
1447 // Create a separate binary for each source file. Useful when there is
1448 // global state that can not be torn down and reset between each test suite.
Colin Cross06a931b2015-10-28 17:23:31 -07001449 Test_per_src *bool
Colin Cross7d5136f2015-05-11 13:39:40 -07001450}
1451
Colin Cross97ba0732015-03-23 17:50:24 -07001452type CCBinary struct {
Colin Crossfa138792015-04-24 17:31:52 -07001453 CCLinked
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001454 out common.Path
1455 installFile common.Path
Colin Cross7d5136f2015-05-11 13:39:40 -07001456 BinaryProperties CCBinaryProperties
Colin Cross3f40fa42015-01-30 17:27:36 -08001457}
1458
Colin Crossed4cf0b2015-03-26 14:43:45 -07001459func (c *CCBinary) buildStatic() bool {
Colin Cross06a931b2015-10-28 17:23:31 -07001460 return Bool(c.BinaryProperties.Static_executable)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001461}
1462
1463func (c *CCBinary) buildShared() bool {
Colin Cross06a931b2015-10-28 17:23:31 -07001464 return !Bool(c.BinaryProperties.Static_executable)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001465}
1466
Colin Cross97ba0732015-03-23 17:50:24 -07001467func (c *CCBinary) getStem(ctx common.AndroidModuleContext) string {
Colin Cross4ae185c2015-03-26 15:12:10 -07001468 stem := ctx.ModuleName()
Colin Cross97ba0732015-03-23 17:50:24 -07001469 if c.BinaryProperties.Stem != "" {
Colin Cross4ae185c2015-03-26 15:12:10 -07001470 stem = c.BinaryProperties.Stem
Colin Cross3f40fa42015-01-30 17:27:36 -08001471 }
Colin Cross4ae185c2015-03-26 15:12:10 -07001472
1473 return stem + c.BinaryProperties.Suffix
Colin Cross3f40fa42015-01-30 17:27:36 -08001474}
1475
Colin Cross0676e2d2015-04-24 17:39:18 -07001476func (c *CCBinary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Crosse11befc2015-04-27 17:49:17 -07001477 depNames = c.CCLinked.depNames(ctx, depNames)
Colin Crossf6566ed2015-03-24 11:13:38 -07001478 if ctx.Device() {
Dan Albertc3144b12015-04-28 18:17:56 -07001479 if c.Properties.Sdk_version == "" {
Colin Cross06a931b2015-10-28 17:23:31 -07001480 if Bool(c.BinaryProperties.Static_executable) {
Dan Albertc3144b12015-04-28 18:17:56 -07001481 depNames.CrtBegin = "crtbegin_static"
1482 } else {
1483 depNames.CrtBegin = "crtbegin_dynamic"
1484 }
1485 depNames.CrtEnd = "crtend_android"
Colin Cross3f40fa42015-01-30 17:27:36 -08001486 } else {
Colin Cross06a931b2015-10-28 17:23:31 -07001487 if Bool(c.BinaryProperties.Static_executable) {
Dan Albertc3144b12015-04-28 18:17:56 -07001488 depNames.CrtBegin = "ndk_crtbegin_static." + c.Properties.Sdk_version
1489 } else {
1490 depNames.CrtBegin = "ndk_crtbegin_dynamic." + c.Properties.Sdk_version
1491 }
1492 depNames.CrtEnd = "ndk_crtend_android." + c.Properties.Sdk_version
Colin Cross3f40fa42015-01-30 17:27:36 -08001493 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001494
Colin Cross06a931b2015-10-28 17:23:31 -07001495 if Bool(c.BinaryProperties.Static_executable) {
Colin Cross74d1ec02015-04-28 13:30:13 -07001496 if c.stl(ctx) == "libc++_static" {
1497 depNames.StaticLibs = append(depNames.StaticLibs, "libm", "libc", "libdl")
1498 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001499 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
1500 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
1501 // move them to the beginning of deps.LateStaticLibs
1502 var groupLibs []string
1503 depNames.StaticLibs, groupLibs = filterList(depNames.StaticLibs,
1504 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
1505 depNames.LateStaticLibs = append(groupLibs, depNames.LateStaticLibs...)
1506 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001507 }
Colin Cross21b9a242015-03-24 14:15:58 -07001508 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -08001509}
1510
Colin Cross97ba0732015-03-23 17:50:24 -07001511func NewCCBinary(binary *CCBinary, module CCModuleType,
Colin Cross1f8f2342015-03-26 16:09:47 -07001512 hod common.HostOrDeviceSupported, props ...interface{}) (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001513
Colin Cross1f8f2342015-03-26 16:09:47 -07001514 props = append(props, &binary.BinaryProperties)
1515
Colin Crossfa138792015-04-24 17:31:52 -07001516 return newCCDynamic(&binary.CCLinked, module, hod, common.MultilibFirst, props...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001517}
1518
Colin Cross97ba0732015-03-23 17:50:24 -07001519func CCBinaryFactory() (blueprint.Module, []interface{}) {
1520 module := &CCBinary{}
1521
1522 return NewCCBinary(module, module, common.HostAndDeviceSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001523}
1524
Colin Cross6362e272015-10-29 15:25:03 -07001525func (c *CCBinary) ModifyProperties(ctx CCModuleContext) {
Colin Cross0af4b842015-04-30 16:36:18 -07001526 if ctx.Darwin() {
Colin Cross06a931b2015-10-28 17:23:31 -07001527 c.BinaryProperties.Static_executable = proptools.BoolPtr(false)
Colin Cross0af4b842015-04-30 16:36:18 -07001528 }
Colin Cross06a931b2015-10-28 17:23:31 -07001529 if Bool(c.BinaryProperties.Static_executable) {
Colin Cross18b6dc52015-04-28 13:20:37 -07001530 c.dynamicProperties.VariantIsStaticBinary = true
1531 }
1532}
1533
Colin Cross0676e2d2015-04-24 17:39:18 -07001534func (c *CCBinary) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Crosse11befc2015-04-27 17:49:17 -07001535 flags = c.CCLinked.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001536
Dan Willemsen490fd492015-11-24 17:53:15 -08001537 if ctx.Host() {
1538 flags.LdFlags = append(flags.LdFlags, "-pie")
1539 if ctx.HostType() == common.Windows {
1540 flags.LdFlags = append(flags.LdFlags, "-Wl,-e_mainCRTStartup")
1541 }
1542 }
1543
1544 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
1545 // all code is position independent, and then those warnings get promoted to
1546 // errors.
1547 if ctx.HostType() != common.Windows {
1548 flags.CFlags = append(flags.CFlags, "-fpie")
1549 }
Colin Cross97ba0732015-03-23 17:50:24 -07001550
Colin Crossf6566ed2015-03-24 11:13:38 -07001551 if ctx.Device() {
Colin Cross06a931b2015-10-28 17:23:31 -07001552 if Bool(c.BinaryProperties.Static_executable) {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001553 // Clang driver needs -static to create static executable.
1554 // However, bionic/linker uses -shared to overwrite.
1555 // Linker for x86 targets does not allow coexistance of -static and -shared,
1556 // so we add -static only if -shared is not used.
1557 if !inList("-shared", flags.LdFlags) {
1558 flags.LdFlags = append(flags.LdFlags, "-static")
1559 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001560
Colin Crossed4cf0b2015-03-26 14:43:45 -07001561 flags.LdFlags = append(flags.LdFlags,
1562 "-nostdlib",
1563 "-Bstatic",
1564 "-Wl,--gc-sections",
1565 )
1566
1567 } else {
1568 linker := "/system/bin/linker"
1569 if flags.Toolchain.Is64Bit() {
1570 linker = "/system/bin/linker64"
1571 }
1572
1573 flags.LdFlags = append(flags.LdFlags,
Colin Cross979422c2015-12-01 14:09:48 -08001574 "-pie",
Colin Crossed4cf0b2015-03-26 14:43:45 -07001575 "-nostdlib",
1576 "-Bdynamic",
1577 fmt.Sprintf("-Wl,-dynamic-linker,%s", linker),
1578 "-Wl,--gc-sections",
1579 "-Wl,-z,nocopyreloc",
1580 )
1581 }
Colin Cross0af4b842015-04-30 16:36:18 -07001582 } else if ctx.Darwin() {
1583 flags.LdFlags = append(flags.LdFlags, "-Wl,-headerpad_max_install_names")
Colin Cross3f40fa42015-01-30 17:27:36 -08001584 }
1585
Colin Cross97ba0732015-03-23 17:50:24 -07001586 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001587}
1588
Colin Cross97ba0732015-03-23 17:50:24 -07001589func (c *CCBinary) compileModule(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001590 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001591
Colin Cross06a931b2015-10-28 17:23:31 -07001592 if !Bool(c.BinaryProperties.Static_executable) && inList("libc", c.Properties.Static_libs) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001593 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
1594 "from static libs or set static_executable: true")
1595 }
1596
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001597 outputFile := common.PathForModuleOut(ctx, c.getStem(ctx)+flags.Toolchain.ExecutableSuffix())
Dan Albertc403f7c2015-03-18 14:01:18 -07001598 c.out = outputFile
Colin Crossbfae8852015-03-26 14:44:11 -07001599 if c.BinaryProperties.Prefix_symbols != "" {
1600 afterPrefixSymbols := outputFile
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001601 outputFile = common.PathForModuleOut(ctx, c.getStem(ctx)+".intermediate")
Colin Crossbfae8852015-03-26 14:44:11 -07001602 TransformBinaryPrefixSymbols(ctx, c.BinaryProperties.Prefix_symbols, outputFile,
1603 ccFlagsToBuilderFlags(flags), afterPrefixSymbols)
1604 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001605
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001606 var linkerDeps common.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07001607
Colin Cross97ba0732015-03-23 17:50:24 -07001608 TransformObjToDynamicBinary(ctx, objFiles, deps.SharedLibs, deps.StaticLibs,
Colin Crossaee540a2015-07-06 17:48:31 -07001609 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
Colin Cross77b00fa2015-03-16 16:15:49 -07001610 ccFlagsToBuilderFlags(flags), outputFile)
Dan Albertc403f7c2015-03-18 14:01:18 -07001611}
Colin Cross3f40fa42015-01-30 17:27:36 -08001612
Colin Cross97ba0732015-03-23 17:50:24 -07001613func (c *CCBinary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Colin Crossd350ecd2015-04-28 13:25:36 -07001614 c.installFile = ctx.InstallFile(filepath.Join("bin", c.Properties.Relative_install_path), c.out)
1615}
1616
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001617func (c *CCBinary) HostToolPath() common.OptionalPath {
Colin Crossd350ecd2015-04-28 13:25:36 -07001618 if c.HostOrDevice().Host() {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001619 return common.OptionalPathForPath(c.installFile)
Colin Crossd350ecd2015-04-28 13:25:36 -07001620 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001621 return common.OptionalPath{}
Dan Albertc403f7c2015-03-18 14:01:18 -07001622}
1623
Colin Cross6002e052015-09-16 16:00:08 -07001624func (c *CCBinary) testPerSrc() bool {
Colin Cross06a931b2015-10-28 17:23:31 -07001625 return Bool(c.BinaryProperties.Test_per_src)
Colin Cross6002e052015-09-16 16:00:08 -07001626}
1627
1628func (c *CCBinary) binary() *CCBinary {
1629 return c
1630}
1631
1632type testPerSrc interface {
1633 binary() *CCBinary
1634 testPerSrc() bool
1635}
1636
1637var _ testPerSrc = (*CCBinary)(nil)
1638
Colin Cross6362e272015-10-29 15:25:03 -07001639func testPerSrcMutator(mctx common.AndroidBottomUpMutatorContext) {
Colin Cross6002e052015-09-16 16:00:08 -07001640 if test, ok := mctx.Module().(testPerSrc); ok {
1641 if test.testPerSrc() {
1642 testNames := make([]string, len(test.binary().Properties.Srcs))
1643 for i, src := range test.binary().Properties.Srcs {
1644 testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
1645 }
1646 tests := mctx.CreateLocalVariations(testNames...)
1647 for i, src := range test.binary().Properties.Srcs {
1648 tests[i].(testPerSrc).binary().Properties.Srcs = []string{src}
1649 tests[i].(testPerSrc).binary().BinaryProperties.Stem = mctx.ModuleName() + "_" + testNames[i]
1650 }
1651 }
1652 }
Colin Cross7d5136f2015-05-11 13:39:40 -07001653}
1654
Colin Cross9ffb4f52015-04-24 17:48:09 -07001655type CCTest struct {
Colin Cross97ba0732015-03-23 17:50:24 -07001656 CCBinary
Dan Albertc403f7c2015-03-18 14:01:18 -07001657}
1658
Colin Cross9ffb4f52015-04-24 17:48:09 -07001659func (c *CCTest) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Cross0676e2d2015-04-24 17:39:18 -07001660 flags = c.CCBinary.flags(ctx, flags)
Dan Albertc403f7c2015-03-18 14:01:18 -07001661
Colin Cross97ba0732015-03-23 17:50:24 -07001662 flags.CFlags = append(flags.CFlags, "-DGTEST_HAS_STD_STRING")
Colin Crossf6566ed2015-03-24 11:13:38 -07001663 if ctx.Host() {
Colin Cross97ba0732015-03-23 17:50:24 -07001664 flags.CFlags = append(flags.CFlags, "-O0", "-g")
Colin Cross28344522015-04-22 13:07:53 -07001665 flags.LdFlags = append(flags.LdFlags, "-lpthread")
Dan Albertc403f7c2015-03-18 14:01:18 -07001666 }
1667
1668 // TODO(danalbert): Make gtest export its dependencies.
Colin Cross28344522015-04-22 13:07:53 -07001669 flags.CFlags = append(flags.CFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001670 "-I"+common.PathForSource(ctx, "external/gtest/include").String())
Dan Albertc403f7c2015-03-18 14:01:18 -07001671
Colin Cross21b9a242015-03-24 14:15:58 -07001672 return flags
Dan Albertc403f7c2015-03-18 14:01:18 -07001673}
1674
Colin Cross9ffb4f52015-04-24 17:48:09 -07001675func (c *CCTest) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Dan Willemsene6540452015-10-20 15:21:33 -07001676 depNames.StaticLibs = append(depNames.StaticLibs, "libgtest_main", "libgtest")
Colin Crossa8a93d32015-04-28 13:26:49 -07001677 depNames = c.CCBinary.depNames(ctx, depNames)
Colin Cross21b9a242015-03-24 14:15:58 -07001678 return depNames
Dan Albertc403f7c2015-03-18 14:01:18 -07001679}
1680
Colin Cross9ffb4f52015-04-24 17:48:09 -07001681func (c *CCTest) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Colin Crossf6566ed2015-03-24 11:13:38 -07001682 if ctx.Device() {
Colin Crossa8a93d32015-04-28 13:26:49 -07001683 ctx.InstallFile("../data/nativetest"+ctx.Arch().ArchType.Multilib[3:]+"/"+ctx.ModuleName(), c.out)
Dan Albertc403f7c2015-03-18 14:01:18 -07001684 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001685 c.CCBinary.installModule(ctx, flags)
Dan Albertc403f7c2015-03-18 14:01:18 -07001686 }
1687}
1688
Colin Cross9ffb4f52015-04-24 17:48:09 -07001689func NewCCTest(test *CCTest, module CCModuleType,
1690 hod common.HostOrDeviceSupported, props ...interface{}) (blueprint.Module, []interface{}) {
1691
Colin Cross9ffb4f52015-04-24 17:48:09 -07001692 return NewCCBinary(&test.CCBinary, module, hod, props...)
1693}
1694
1695func CCTestFactory() (blueprint.Module, []interface{}) {
1696 module := &CCTest{}
1697
1698 return NewCCTest(module, module, common.HostAndDeviceSupported)
1699}
1700
Colin Cross2ba19d92015-05-07 15:44:20 -07001701type CCBenchmark struct {
1702 CCBinary
1703}
1704
1705func (c *CCBenchmark) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
1706 depNames = c.CCBinary.depNames(ctx, depNames)
Dan Willemsenf8e98b02015-09-11 17:41:44 -07001707 depNames.StaticLibs = append(depNames.StaticLibs, "libbenchmark", "libbase")
Colin Cross2ba19d92015-05-07 15:44:20 -07001708 return depNames
1709}
1710
1711func (c *CCBenchmark) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
1712 if ctx.Device() {
1713 ctx.InstallFile("../data/nativetest"+ctx.Arch().ArchType.Multilib[3:]+"/"+ctx.ModuleName(), c.out)
1714 } else {
1715 c.CCBinary.installModule(ctx, flags)
1716 }
1717}
1718
1719func NewCCBenchmark(test *CCBenchmark, module CCModuleType,
1720 hod common.HostOrDeviceSupported, props ...interface{}) (blueprint.Module, []interface{}) {
1721
1722 return NewCCBinary(&test.CCBinary, module, hod, props...)
1723}
1724
1725func CCBenchmarkFactory() (blueprint.Module, []interface{}) {
1726 module := &CCBenchmark{}
1727
1728 return NewCCBenchmark(module, module, common.HostAndDeviceSupported)
1729}
1730
Colin Cross3f40fa42015-01-30 17:27:36 -08001731//
1732// Static library
1733//
1734
Colin Cross97ba0732015-03-23 17:50:24 -07001735func CCLibraryStaticFactory() (blueprint.Module, []interface{}) {
1736 module := &CCLibrary{}
1737 module.LibraryProperties.BuildStatic = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001738
Colin Cross97ba0732015-03-23 17:50:24 -07001739 return NewCCLibrary(module, module, common.HostAndDeviceSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001740}
1741
1742//
1743// Shared libraries
1744//
1745
Colin Cross97ba0732015-03-23 17:50:24 -07001746func CCLibrarySharedFactory() (blueprint.Module, []interface{}) {
1747 module := &CCLibrary{}
1748 module.LibraryProperties.BuildShared = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001749
Colin Cross97ba0732015-03-23 17:50:24 -07001750 return NewCCLibrary(module, module, common.HostAndDeviceSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001751}
1752
1753//
1754// Host static library
1755//
1756
Colin Cross97ba0732015-03-23 17:50:24 -07001757func CCLibraryHostStaticFactory() (blueprint.Module, []interface{}) {
1758 module := &CCLibrary{}
1759 module.LibraryProperties.BuildStatic = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001760
Colin Cross97ba0732015-03-23 17:50:24 -07001761 return NewCCLibrary(module, module, common.HostSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001762}
1763
1764//
1765// Host Shared libraries
1766//
1767
Colin Cross97ba0732015-03-23 17:50:24 -07001768func CCLibraryHostSharedFactory() (blueprint.Module, []interface{}) {
1769 module := &CCLibrary{}
1770 module.LibraryProperties.BuildShared = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001771
Colin Cross97ba0732015-03-23 17:50:24 -07001772 return NewCCLibrary(module, module, common.HostSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001773}
1774
1775//
1776// Host Binaries
1777//
1778
Colin Cross97ba0732015-03-23 17:50:24 -07001779func CCBinaryHostFactory() (blueprint.Module, []interface{}) {
1780 module := &CCBinary{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001781
Colin Cross97ba0732015-03-23 17:50:24 -07001782 return NewCCBinary(module, module, common.HostSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001783}
1784
1785//
Colin Cross1f8f2342015-03-26 16:09:47 -07001786// Host Tests
1787//
1788
1789func CCTestHostFactory() (blueprint.Module, []interface{}) {
Colin Cross9ffb4f52015-04-24 17:48:09 -07001790 module := &CCTest{}
Colin Cross6002e052015-09-16 16:00:08 -07001791 return NewCCBinary(&module.CCBinary, module, common.HostSupported)
Colin Cross1f8f2342015-03-26 16:09:47 -07001792}
1793
1794//
Colin Cross2ba19d92015-05-07 15:44:20 -07001795// Host Benchmarks
1796//
1797
1798func CCBenchmarkHostFactory() (blueprint.Module, []interface{}) {
1799 module := &CCBenchmark{}
1800 return NewCCBinary(&module.CCBinary, module, common.HostSupported)
1801}
1802
1803//
Colin Crosscfad1192015-11-02 16:43:11 -08001804// Defaults
1805//
1806type CCDefaults struct {
1807 common.AndroidModuleBase
1808 common.DefaultsModule
1809}
1810
1811func (*CCDefaults) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
1812}
1813
1814func CCDefaultsFactory() (blueprint.Module, []interface{}) {
1815 module := &CCDefaults{}
1816
1817 propertyStructs := []interface{}{
1818 &CCBaseProperties{},
1819 &CCLibraryProperties{},
1820 &CCBinaryProperties{},
1821 &CCUnusedProperties{},
1822 }
1823
Dan Willemsen218f6562015-07-08 18:13:11 -07001824 _, propertyStructs = common.InitAndroidArchModule(module, common.HostAndDeviceDefault,
1825 common.MultilibDefault, propertyStructs...)
Colin Crosscfad1192015-11-02 16:43:11 -08001826
1827 return common.InitDefaultsModule(module, module, propertyStructs...)
1828}
1829
1830//
Colin Cross3f40fa42015-01-30 17:27:36 -08001831// Device libraries shipped with gcc
1832//
1833
1834type toolchainLibrary struct {
Colin Cross97ba0732015-03-23 17:50:24 -07001835 CCLibrary
Colin Cross3f40fa42015-01-30 17:27:36 -08001836}
1837
Colin Cross0676e2d2015-04-24 17:39:18 -07001838func (*toolchainLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Cross3f40fa42015-01-30 17:27:36 -08001839 // toolchain libraries can't have any dependencies
Colin Cross21b9a242015-03-24 14:15:58 -07001840 return CCDeps{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001841}
1842
Colin Cross97ba0732015-03-23 17:50:24 -07001843func ToolchainLibraryFactory() (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001844 module := &toolchainLibrary{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001845
Colin Cross97ba0732015-03-23 17:50:24 -07001846 module.LibraryProperties.BuildStatic = true
1847
Colin Crossfa138792015-04-24 17:31:52 -07001848 return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth,
Colin Cross21b9a242015-03-24 14:15:58 -07001849 &module.LibraryProperties)
Colin Cross3f40fa42015-01-30 17:27:36 -08001850}
1851
1852func (c *toolchainLibrary) compileModule(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001853 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001854
1855 libName := ctx.ModuleName() + staticLibraryExtension
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001856 outputFile := common.PathForModuleOut(ctx, libName)
Colin Cross3f40fa42015-01-30 17:27:36 -08001857
1858 CopyGccLib(ctx, libName, ccFlagsToBuilderFlags(flags), outputFile)
1859
1860 c.out = outputFile
1861
1862 ctx.CheckbuildFile(outputFile)
1863}
1864
Colin Cross97ba0732015-03-23 17:50:24 -07001865func (c *toolchainLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001866 // Toolchain libraries do not get installed.
1867}
1868
Dan Albertbe961682015-03-18 23:38:50 -07001869// NDK prebuilt libraries.
1870//
1871// These differ from regular prebuilts in that they aren't stripped and usually aren't installed
1872// either (with the exception of the shared STLs, which are installed to the app's directory rather
1873// than to the system image).
1874
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001875func getNdkLibDir(ctx common.AndroidModuleContext, toolchain Toolchain, version string) common.SourcePath {
1876 return common.PathForSource(ctx, fmt.Sprintf("prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/lib",
1877 version, toolchain.Name()))
Dan Albertbe961682015-03-18 23:38:50 -07001878}
1879
Dan Albertc3144b12015-04-28 18:17:56 -07001880func ndkPrebuiltModuleToPath(ctx common.AndroidModuleContext, toolchain Toolchain,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001881 ext string, version string) common.Path {
Dan Albertc3144b12015-04-28 18:17:56 -07001882
1883 // NDK prebuilts are named like: ndk_NAME.EXT.SDK_VERSION.
1884 // We want to translate to just NAME.EXT
1885 name := strings.Split(strings.TrimPrefix(ctx.ModuleName(), "ndk_"), ".")[0]
1886 dir := getNdkLibDir(ctx, toolchain, version)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001887 return dir.Join(ctx, name+ext)
Dan Albertc3144b12015-04-28 18:17:56 -07001888}
1889
1890type ndkPrebuiltObject struct {
1891 ccObject
1892}
1893
Dan Albertc3144b12015-04-28 18:17:56 -07001894func (*ndkPrebuiltObject) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
1895 // NDK objects can't have any dependencies
1896 return CCDeps{}
1897}
1898
1899func NdkPrebuiltObjectFactory() (blueprint.Module, []interface{}) {
1900 module := &ndkPrebuiltObject{}
1901 return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth)
1902}
1903
1904func (c *ndkPrebuiltObject) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001905 deps CCPathDeps, objFiles common.Paths) {
Dan Albertc3144b12015-04-28 18:17:56 -07001906 // A null build step, but it sets up the output path.
1907 if !strings.HasPrefix(ctx.ModuleName(), "ndk_crt") {
1908 ctx.ModuleErrorf("NDK prebuilts must have an ndk_crt prefixed name")
1909 }
1910
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001911 c.out = common.OptionalPathForPath(ndkPrebuiltModuleToPath(ctx, flags.Toolchain, objectExtension, c.Properties.Sdk_version))
Dan Albertc3144b12015-04-28 18:17:56 -07001912}
1913
1914func (c *ndkPrebuiltObject) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
1915 // Objects do not get installed.
1916}
1917
1918var _ ccObjectProvider = (*ndkPrebuiltObject)(nil)
1919
Dan Albertbe961682015-03-18 23:38:50 -07001920type ndkPrebuiltLibrary struct {
1921 CCLibrary
1922}
1923
Colin Cross0676e2d2015-04-24 17:39:18 -07001924func (*ndkPrebuiltLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Dan Albertbe961682015-03-18 23:38:50 -07001925 // NDK libraries can't have any dependencies
1926 return CCDeps{}
1927}
1928
1929func NdkPrebuiltLibraryFactory() (blueprint.Module, []interface{}) {
1930 module := &ndkPrebuiltLibrary{}
1931 module.LibraryProperties.BuildShared = true
1932 return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
1933}
1934
1935func (c *ndkPrebuiltLibrary) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001936 deps CCPathDeps, objFiles common.Paths) {
Dan Albertbe961682015-03-18 23:38:50 -07001937 // A null build step, but it sets up the output path.
1938 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
1939 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
1940 }
1941
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001942 includeDirs := common.PathsForModuleSrc(ctx, c.Properties.Export_include_dirs)
1943 c.exportFlags = []string{common.JoinWithPrefix(includeDirs.Strings(), "-isystem ")}
Dan Albertbe961682015-03-18 23:38:50 -07001944
Dan Willemsen490fd492015-11-24 17:53:15 -08001945 c.out = ndkPrebuiltModuleToPath(ctx, flags.Toolchain, flags.Toolchain.ShlibSuffix(),
Dan Albertc3144b12015-04-28 18:17:56 -07001946 c.Properties.Sdk_version)
Dan Albertbe961682015-03-18 23:38:50 -07001947}
1948
1949func (c *ndkPrebuiltLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc3144b12015-04-28 18:17:56 -07001950 // NDK prebuilt libraries do not get installed.
Dan Albertbe961682015-03-18 23:38:50 -07001951}
1952
1953// The NDK STLs are slightly different from the prebuilt system libraries:
1954// * Are not specific to each platform version.
1955// * The libraries are not in a predictable location for each STL.
1956
1957type ndkPrebuiltStl struct {
1958 ndkPrebuiltLibrary
1959}
1960
1961type ndkPrebuiltStaticStl struct {
1962 ndkPrebuiltStl
1963}
1964
1965type ndkPrebuiltSharedStl struct {
1966 ndkPrebuiltStl
1967}
1968
1969func NdkPrebuiltSharedStlFactory() (blueprint.Module, []interface{}) {
1970 module := &ndkPrebuiltSharedStl{}
1971 module.LibraryProperties.BuildShared = true
1972 return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
1973}
1974
1975func NdkPrebuiltStaticStlFactory() (blueprint.Module, []interface{}) {
1976 module := &ndkPrebuiltStaticStl{}
1977 module.LibraryProperties.BuildStatic = true
1978 return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
1979}
1980
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001981func getNdkStlLibDir(ctx common.AndroidModuleContext, toolchain Toolchain, stl string) common.SourcePath {
Dan Albertbe961682015-03-18 23:38:50 -07001982 gccVersion := toolchain.GccVersion()
1983 var libDir string
1984 switch stl {
1985 case "libstlport":
1986 libDir = "cxx-stl/stlport/libs"
1987 case "libc++":
1988 libDir = "cxx-stl/llvm-libc++/libs"
1989 case "libgnustl":
1990 libDir = fmt.Sprintf("cxx-stl/gnu-libstdc++/%s/libs", gccVersion)
1991 }
1992
1993 if libDir != "" {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001994 ndkSrcRoot := "prebuilts/ndk/current/sources"
1995 return common.PathForSource(ctx, ndkSrcRoot).Join(ctx, libDir, ctx.Arch().Abi[0])
Dan Albertbe961682015-03-18 23:38:50 -07001996 }
1997
1998 ctx.ModuleErrorf("Unknown NDK STL: %s", stl)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001999 return common.PathForSource(ctx, "")
Dan Albertbe961682015-03-18 23:38:50 -07002000}
2001
2002func (c *ndkPrebuiltStl) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002003 deps CCPathDeps, objFiles common.Paths) {
Dan Albertbe961682015-03-18 23:38:50 -07002004 // A null build step, but it sets up the output path.
2005 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
2006 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
2007 }
2008
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002009 includeDirs := common.PathsForModuleSrc(ctx, c.Properties.Export_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -07002010 c.exportFlags = []string{includeDirsToFlags(includeDirs)}
Dan Albertbe961682015-03-18 23:38:50 -07002011
2012 libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_")
Dan Willemsen490fd492015-11-24 17:53:15 -08002013 libExt := flags.Toolchain.ShlibSuffix()
Dan Albertbe961682015-03-18 23:38:50 -07002014 if c.LibraryProperties.BuildStatic {
2015 libExt = staticLibraryExtension
2016 }
2017
2018 stlName := strings.TrimSuffix(libName, "_shared")
2019 stlName = strings.TrimSuffix(stlName, "_static")
2020 libDir := getNdkStlLibDir(ctx, flags.Toolchain, stlName)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002021 c.out = libDir.Join(ctx, libName+libExt)
Dan Albertbe961682015-03-18 23:38:50 -07002022}
2023
Colin Cross6362e272015-10-29 15:25:03 -07002024func linkageMutator(mctx common.AndroidBottomUpMutatorContext) {
Colin Crossed4cf0b2015-03-26 14:43:45 -07002025 if c, ok := mctx.Module().(ccLinkedInterface); ok {
Colin Cross3f40fa42015-01-30 17:27:36 -08002026 var modules []blueprint.Module
Colin Crossed4cf0b2015-03-26 14:43:45 -07002027 if c.buildStatic() && c.buildShared() {
Colin Cross3f40fa42015-01-30 17:27:36 -08002028 modules = mctx.CreateLocalVariations("static", "shared")
Colin Cross18b6dc52015-04-28 13:20:37 -07002029 modules[0].(ccLinkedInterface).setStatic(true)
2030 modules[1].(ccLinkedInterface).setStatic(false)
Colin Crossed4cf0b2015-03-26 14:43:45 -07002031 } else if c.buildStatic() {
Colin Cross3f40fa42015-01-30 17:27:36 -08002032 modules = mctx.CreateLocalVariations("static")
Colin Cross18b6dc52015-04-28 13:20:37 -07002033 modules[0].(ccLinkedInterface).setStatic(true)
Colin Crossed4cf0b2015-03-26 14:43:45 -07002034 } else if c.buildShared() {
Colin Cross3f40fa42015-01-30 17:27:36 -08002035 modules = mctx.CreateLocalVariations("shared")
Colin Cross18b6dc52015-04-28 13:20:37 -07002036 modules[0].(ccLinkedInterface).setStatic(false)
Colin Cross3f40fa42015-01-30 17:27:36 -08002037 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07002038 panic(fmt.Errorf("ccLibrary %q not static or shared", mctx.ModuleName()))
Colin Cross3f40fa42015-01-30 17:27:36 -08002039 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07002040
2041 if _, ok := c.(ccLibraryInterface); ok {
2042 reuseFrom := modules[0].(ccLibraryInterface)
2043 for _, m := range modules {
2044 m.(ccLibraryInterface).setReuseFrom(reuseFrom)
Colin Cross3f40fa42015-01-30 17:27:36 -08002045 }
2046 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002047 }
2048}
Colin Cross74d1ec02015-04-28 13:30:13 -07002049
2050// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
2051// modifies the slice contents in place, and returns a subslice of the original slice
2052func lastUniqueElements(list []string) []string {
2053 totalSkip := 0
2054 for i := len(list) - 1; i >= totalSkip; i-- {
2055 skip := 0
2056 for j := i - 1; j >= totalSkip; j-- {
2057 if list[i] == list[j] {
2058 skip++
2059 } else {
2060 list[j+skip] = list[j]
2061 }
2062 }
2063 totalSkip += skip
2064 }
2065 return list[totalSkip:]
2066}
Colin Cross06a931b2015-10-28 17:23:31 -07002067
2068var Bool = proptools.Bool