blob: c4e1d0e8ce5195671a0f169dc9783e5d33801c49 [file] [log] [blame]
Colin Cross5049f022015-03-18 13:28:46 -07001// Copyright 2015 Google Inc. All rights reserved.
Colin Cross3f40fa42015-01-30 17:27:36 -08002//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package cc
16
17// This file contains the module types for compiling C/C++ for Android, and converts the properties
18// into the flags and filenames necessary to pass to the compiler. The final creation of the rules
19// is handled in builder.go
20
21import (
Colin Cross3f40fa42015-01-30 17:27:36 -080022 "fmt"
23 "path/filepath"
Colin Cross0af4b842015-04-30 16:36:18 -070024 "runtime"
Colin Cross3f40fa42015-01-30 17:27:36 -080025 "strings"
26
Colin Cross97ba0732015-03-23 17:50:24 -070027 "github.com/google/blueprint"
28 "github.com/google/blueprint/pathtools"
29
Colin Cross463a90e2015-06-17 14:20:06 -070030 "android/soong"
Colin Cross3f40fa42015-01-30 17:27:36 -080031 "android/soong/common"
Colin Cross5049f022015-03-18 13:28:46 -070032 "android/soong/genrule"
Colin Cross3f40fa42015-01-30 17:27:36 -080033)
34
Colin Cross463a90e2015-06-17 14:20:06 -070035func init() {
36 soong.RegisterModuleType("cc_library_static", CCLibraryStaticFactory)
37 soong.RegisterModuleType("cc_library_shared", CCLibrarySharedFactory)
38 soong.RegisterModuleType("cc_library", CCLibraryFactory)
39 soong.RegisterModuleType("cc_object", CCObjectFactory)
40 soong.RegisterModuleType("cc_binary", CCBinaryFactory)
41 soong.RegisterModuleType("cc_test", CCTestFactory)
42 soong.RegisterModuleType("cc_benchmark", CCBenchmarkFactory)
43
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.
59 soong.RegisterEarlyMutator("link", LinkageMutator)
60 soong.RegisterEarlyMutator("test_per_src", TestPerSrcMutator)
61}
62
Colin Cross3f40fa42015-01-30 17:27:36 -080063var (
Colin Cross1332b002015-04-07 17:11:30 -070064 HostPrebuiltTag = pctx.VariableConfigMethod("HostPrebuiltTag", common.Config.PrebuiltOS)
65 SrcDir = pctx.VariableConfigMethod("SrcDir", common.Config.SrcDir)
Colin Cross3f40fa42015-01-30 17:27:36 -080066
Dan Willemsen87b17d12015-07-14 00:39:06 -070067 LibcRoot = pctx.StaticVariable("LibcRoot", "bionic/libc")
68 LibmRoot = pctx.StaticVariable("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 "-fdiagnostics-color",
83 "-fdebug-prefix-map=/proc/self/cwd=",
Colin Cross3f40fa42015-01-30 17:27:36 -080084
85 // COMMON_RELEASE_CFLAGS
86 "-DNDEBUG",
87 "-UDEBUG",
88 }
89
90 deviceGlobalCflags = []string{
91 // TARGET_ERROR_FLAGS
92 "-Werror=return-type",
93 "-Werror=non-virtual-dtor",
94 "-Werror=address",
95 "-Werror=sequence-point",
96 }
97
98 hostGlobalCflags = []string{}
99
100 commonGlobalCppflags = []string{
101 "-Wsign-promo",
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700102 }
103
104 illegalFlags = []string{
105 "-w",
Colin Cross3f40fa42015-01-30 17:27:36 -0800106 }
107)
108
109func init() {
110 pctx.StaticVariable("commonGlobalCflags", strings.Join(commonGlobalCflags, " "))
111 pctx.StaticVariable("deviceGlobalCflags", strings.Join(deviceGlobalCflags, " "))
112 pctx.StaticVariable("hostGlobalCflags", strings.Join(hostGlobalCflags, " "))
113
114 pctx.StaticVariable("commonGlobalCppflags", strings.Join(commonGlobalCppflags, " "))
115
116 pctx.StaticVariable("commonClangGlobalCflags",
117 strings.Join(clangFilterUnknownCflags(commonGlobalCflags), " "))
118 pctx.StaticVariable("deviceClangGlobalCflags",
119 strings.Join(clangFilterUnknownCflags(deviceGlobalCflags), " "))
120 pctx.StaticVariable("hostClangGlobalCflags",
121 strings.Join(clangFilterUnknownCflags(hostGlobalCflags), " "))
Tim Kilbournf2948142015-03-11 12:03:03 -0700122 pctx.StaticVariable("commonClangGlobalCppflags",
123 strings.Join(clangFilterUnknownCflags(commonGlobalCppflags), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800124
125 // Everything in this list is a crime against abstraction and dependency tracking.
126 // Do not add anything to this list.
127 pctx.StaticVariable("commonGlobalIncludes", strings.Join([]string{
128 "-isystem ${SrcDir}/system/core/include",
129 "-isystem ${SrcDir}/hardware/libhardware/include",
130 "-isystem ${SrcDir}/hardware/libhardware_legacy/include",
131 "-isystem ${SrcDir}/hardware/ril/include",
132 "-isystem ${SrcDir}/libnativehelper/include",
133 "-isystem ${SrcDir}/frameworks/native/include",
134 "-isystem ${SrcDir}/frameworks/native/opengl/include",
135 "-isystem ${SrcDir}/frameworks/av/include",
136 "-isystem ${SrcDir}/frameworks/base/include",
137 }, " "))
138
139 pctx.StaticVariable("clangPath", "${SrcDir}/prebuilts/clang/${HostPrebuiltTag}/host/3.6/bin/")
140}
141
Colin Cross3f40fa42015-01-30 17:27:36 -0800142// Building C/C++ code is handled by objects that satisfy this interface via composition
Colin Cross97ba0732015-03-23 17:50:24 -0700143type CCModuleType interface {
Colin Cross3f40fa42015-01-30 17:27:36 -0800144 common.AndroidModule
145
Colin Crossfa138792015-04-24 17:31:52 -0700146 // Modify property values after parsing Blueprints file but before starting dependency
147 // resolution or build rule generation
148 ModifyProperties(common.AndroidBaseContext)
149
Colin Cross21b9a242015-03-24 14:15:58 -0700150 // Modify the ccFlags
Colin Cross0676e2d2015-04-24 17:39:18 -0700151 flags(common.AndroidModuleContext, CCFlags) CCFlags
Colin Cross3f40fa42015-01-30 17:27:36 -0800152
Colin Cross21b9a242015-03-24 14:15:58 -0700153 // Return list of dependency names for use in AndroidDynamicDependencies and in depsToPaths
Colin Cross0676e2d2015-04-24 17:39:18 -0700154 depNames(common.AndroidBaseContext, CCDeps) CCDeps
Colin Cross3f40fa42015-01-30 17:27:36 -0800155
156 // Compile objects into final module
Colin Cross97ba0732015-03-23 17:50:24 -0700157 compileModule(common.AndroidModuleContext, CCFlags, CCDeps, []string)
Colin Cross3f40fa42015-01-30 17:27:36 -0800158
Dan Albertc403f7c2015-03-18 14:01:18 -0700159 // Install the built module.
Colin Cross97ba0732015-03-23 17:50:24 -0700160 installModule(common.AndroidModuleContext, CCFlags)
Dan Albertc403f7c2015-03-18 14:01:18 -0700161
Colin Cross3f40fa42015-01-30 17:27:36 -0800162 // Return the output file (.o, .a or .so) for use by other modules
163 outputFile() string
164}
165
Colin Cross97ba0732015-03-23 17:50:24 -0700166type CCDeps struct {
Colin Cross28344522015-04-22 13:07:53 -0700167 StaticLibs, SharedLibs, LateStaticLibs, WholeStaticLibs, ObjFiles, Cflags []string
Colin Crossc472d572015-03-17 15:06:21 -0700168
Colin Cross21b9a242015-03-24 14:15:58 -0700169 WholeStaticLibObjFiles []string
170
Colin Cross97ba0732015-03-23 17:50:24 -0700171 CrtBegin, CrtEnd string
Colin Crossc472d572015-03-17 15:06:21 -0700172}
173
Colin Cross97ba0732015-03-23 17:50:24 -0700174type CCFlags struct {
Colin Cross28344522015-04-22 13:07:53 -0700175 GlobalFlags []string // Flags that apply to C, C++, and assembly source files
176 AsFlags []string // Flags that apply to assembly source files
177 CFlags []string // Flags that apply to C and C++ source files
178 ConlyFlags []string // Flags that apply to C source files
179 CppFlags []string // Flags that apply to C++ source files
180 YaccFlags []string // Flags that apply to Yacc source files
181 LdFlags []string // Flags that apply to linker command lines
182
183 Nocrt bool
184 Toolchain Toolchain
185 Clang bool
Colin Crossc472d572015-03-17 15:06:21 -0700186}
187
Colin Cross7d5136f2015-05-11 13:39:40 -0700188// Properties used to compile all C or C++ modules
189type CCBaseProperties struct {
190 // 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 -0700191 Srcs []string `android:"arch_variant"`
192
193 // list of source files that should not be used to build the C/C++ module.
194 // This is most useful in the arch/multilib variants to remove non-common files
195 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700196
197 // list of module-specific flags that will be used for C and C++ compiles.
198 Cflags []string `android:"arch_variant"`
199
200 // list of module-specific flags that will be used for C++ compiles
201 Cppflags []string `android:"arch_variant"`
202
203 // list of module-specific flags that will be used for C compiles
204 Conlyflags []string `android:"arch_variant"`
205
206 // list of module-specific flags that will be used for .S compiles
207 Asflags []string `android:"arch_variant"`
208
209 // list of module-specific flags that will be used for .y and .yy compiles
210 Yaccflags []string
211
212 // list of module-specific flags that will be used for all link steps
213 Ldflags []string `android:"arch_variant"`
214
215 // the instruction set architecture to use to compile the C/C++
216 // module.
217 Instruction_set string `android:"arch_variant"`
218
219 // list of directories relative to the root of the source tree that will
220 // be added to the include path using -I.
221 // If possible, don't use this. If adding paths from the current directory use
222 // local_include_dirs, if adding paths from other modules use export_include_dirs in
223 // that module.
224 Include_dirs []string `android:"arch_variant"`
225
Colin Cross39d97f22015-09-14 12:30:50 -0700226 // list of files relative to the root of the source tree that will be included
227 // using -include.
228 // If possible, don't use this.
229 Include_files []string `android:"arch_variant"`
230
Colin Cross7d5136f2015-05-11 13:39:40 -0700231 // list of directories relative to the Blueprints file that will
232 // be added to the include path using -I
233 Local_include_dirs []string `android:"arch_variant"`
234
Colin Cross39d97f22015-09-14 12:30:50 -0700235 // list of files relative to the Blueprints file that will be included
236 // using -include.
237 // If possible, don't use this.
238 Local_include_files []string `android:"arch_variant"`
239
Colin Cross7d5136f2015-05-11 13:39:40 -0700240 // list of directories relative to the Blueprints file that will
241 // be added to the include path using -I for any module that links against this module
242 Export_include_dirs []string `android:"arch_variant"`
243
244 // list of module-specific flags that will be used for C and C++ compiles when
245 // compiling with clang
246 Clang_cflags []string `android:"arch_variant"`
247
248 // list of module-specific flags that will be used for .S compiles when
249 // compiling with clang
250 Clang_asflags []string `android:"arch_variant"`
251
252 // list of system libraries that will be dynamically linked to
253 // shared library and executable modules. If unset, generally defaults to libc
254 // and libm. Set to [] to prevent linking against libc and libm.
255 System_shared_libs []string
256
257 // list of modules whose object files should be linked into this module
258 // in their entirety. For static library modules, all of the .o files from the intermediate
259 // directory of the dependency will be linked into this modules .a file. For a shared library,
260 // the dependency's .a file will be linked into this module using -Wl,--whole-archive.
261 Whole_static_libs []string `android:"arch_variant"`
262
263 // list of modules that should be statically linked into this module.
264 Static_libs []string `android:"arch_variant"`
265
266 // list of modules that should be dynamically linked into this module.
267 Shared_libs []string `android:"arch_variant"`
268
269 // allow the module to contain undefined symbols. By default,
270 // modules cannot contain undefined symbols that are not satisified by their immediate
271 // dependencies. Set this flag to true to remove --no-undefined from the linker flags.
272 // This flag should only be necessary for compiling low-level libraries like libc.
273 Allow_undefined_symbols bool
274
275 // don't link in crt_begin and crt_end. This flag should only be necessary for
276 // compiling crt or libc.
277 Nocrt bool `android:"arch_variant"`
278
Dan Willemsend67be222015-09-16 15:19:33 -0700279 // don't link in libgcc.a
280 No_libgcc bool
281
Colin Cross7d5136f2015-05-11 13:39:40 -0700282 // don't insert default compiler flags into asflags, cflags,
283 // cppflags, conlyflags, ldflags, or include_dirs
284 No_default_compiler_flags bool
285
286 // compile module with clang instead of gcc
287 Clang bool `android:"arch_variant"`
288
289 // pass -frtti instead of -fno-rtti
290 Rtti bool
291
292 // -l arguments to pass to linker for host-provided shared libraries
293 Host_ldlibs []string `android:"arch_variant"`
294
295 // select the STL library to use. Possible values are "libc++", "libc++_static",
296 // "stlport", "stlport_static", "ndk", "libstdc++", or "none". Leave blank to select the
297 // default
298 Stl string
299
300 // Set for combined shared/static libraries to prevent compiling object files a second time
301 SkipCompileObjs bool `blueprint:"mutated"`
302
303 Debug, Release struct {
304 // list of module-specific flags that will be used for C and C++ compiles in debug or
305 // release builds
306 Cflags []string `android:"arch_variant"`
307 } `android:"arch_variant"`
308
309 // Minimum sdk version supported when compiling against the ndk
310 Sdk_version string
311
312 // install to a subdirectory of the default install path for the module
313 Relative_install_path string
314}
315
Colin Crossfa138792015-04-24 17:31:52 -0700316// CCBase contains the properties and members used by all C/C++ module types, and implements
Colin Crossc472d572015-03-17 15:06:21 -0700317// the blueprint.Module interface. It expects to be embedded into an outer specialization struct,
318// and uses a ccModuleType interface to that struct to create the build steps.
Colin Crossfa138792015-04-24 17:31:52 -0700319type CCBase struct {
Colin Crossc472d572015-03-17 15:06:21 -0700320 common.AndroidModuleBase
Colin Cross97ba0732015-03-23 17:50:24 -0700321 module CCModuleType
Colin Crossc472d572015-03-17 15:06:21 -0700322
Colin Cross7d5136f2015-05-11 13:39:40 -0700323 Properties CCBaseProperties
Colin Crossfa138792015-04-24 17:31:52 -0700324
325 unused struct {
Colin Crossb43a1592015-09-16 14:00:32 -0700326 Native_coverage bool
327 Required []string
Dan Willemsen9e0cb352015-09-16 15:41:55 -0700328 Sanitize []string `android:"arch_variant"`
Colin Crossb43a1592015-09-16 14:00:32 -0700329 Sanitize_recover []string
330 Strip string
331 Tags []string
Colin Crossfa138792015-04-24 17:31:52 -0700332 }
Colin Crossc472d572015-03-17 15:06:21 -0700333
334 installPath string
Colin Cross74d1ec02015-04-28 13:30:13 -0700335
336 savedDepNames CCDeps
Colin Crossc472d572015-03-17 15:06:21 -0700337}
338
Colin Crossfa138792015-04-24 17:31:52 -0700339func newCCBase(base *CCBase, module CCModuleType, hod common.HostOrDeviceSupported,
Colin Crossc472d572015-03-17 15:06:21 -0700340 multilib common.Multilib, props ...interface{}) (blueprint.Module, []interface{}) {
341
342 base.module = module
343
Colin Crossfa138792015-04-24 17:31:52 -0700344 props = append(props, &base.Properties, &base.unused)
Colin Crossc472d572015-03-17 15:06:21 -0700345
Colin Cross5049f022015-03-18 13:28:46 -0700346 return common.InitAndroidArchModule(module, hod, multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700347}
348
Colin Crossfa138792015-04-24 17:31:52 -0700349func (c *CCBase) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800350 toolchain := c.findToolchain(ctx)
351 if ctx.Failed() {
352 return
353 }
354
Colin Cross21b9a242015-03-24 14:15:58 -0700355 flags := c.collectFlags(ctx, toolchain)
Colin Cross3f40fa42015-01-30 17:27:36 -0800356 if ctx.Failed() {
357 return
358 }
359
Colin Cross74d1ec02015-04-28 13:30:13 -0700360 deps := c.depsToPaths(ctx, c.savedDepNames)
Colin Cross3f40fa42015-01-30 17:27:36 -0800361 if ctx.Failed() {
362 return
363 }
364
Colin Cross28344522015-04-22 13:07:53 -0700365 flags.CFlags = append(flags.CFlags, deps.Cflags...)
Colin Crossed9f8682015-03-18 17:17:35 -0700366
Colin Cross581c1892015-04-07 16:50:10 -0700367 objFiles := c.compileObjs(ctx, flags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800368 if ctx.Failed() {
369 return
370 }
371
Colin Cross581c1892015-04-07 16:50:10 -0700372 generatedObjFiles := c.compileGeneratedObjs(ctx, flags)
Colin Cross5049f022015-03-18 13:28:46 -0700373 if ctx.Failed() {
374 return
375 }
376
377 objFiles = append(objFiles, generatedObjFiles...)
378
Colin Cross3f40fa42015-01-30 17:27:36 -0800379 c.ccModuleType().compileModule(ctx, flags, deps, objFiles)
380 if ctx.Failed() {
381 return
382 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700383
384 c.ccModuleType().installModule(ctx, flags)
385 if ctx.Failed() {
386 return
387 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800388}
389
Colin Crossfa138792015-04-24 17:31:52 -0700390func (c *CCBase) ccModuleType() CCModuleType {
Colin Cross3f40fa42015-01-30 17:27:36 -0800391 return c.module
392}
393
Colin Crossfa138792015-04-24 17:31:52 -0700394var _ common.AndroidDynamicDepender = (*CCBase)(nil)
Colin Cross3f40fa42015-01-30 17:27:36 -0800395
Colin Crossfa138792015-04-24 17:31:52 -0700396func (c *CCBase) findToolchain(ctx common.AndroidModuleContext) Toolchain {
Colin Cross3f40fa42015-01-30 17:27:36 -0800397 arch := ctx.Arch()
Colin Crossd3ba0392015-05-07 14:11:29 -0700398 hod := ctx.HostOrDevice()
399 factory := toolchainFactories[hod][arch.ArchType]
Colin Cross3f40fa42015-01-30 17:27:36 -0800400 if factory == nil {
401 panic(fmt.Sprintf("Toolchain not found for %s arch %q",
Colin Crossd3ba0392015-05-07 14:11:29 -0700402 hod.String(), arch.String()))
Colin Cross3f40fa42015-01-30 17:27:36 -0800403 }
404 return factory(arch.ArchVariant, arch.CpuVariant)
405}
406
Colin Crossfa138792015-04-24 17:31:52 -0700407func (c *CCBase) ModifyProperties(ctx common.AndroidBaseContext) {
408}
409
Colin Crosse11befc2015-04-27 17:49:17 -0700410func (c *CCBase) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Crossfa138792015-04-24 17:31:52 -0700411 depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.Properties.Whole_static_libs...)
412 depNames.StaticLibs = append(depNames.StaticLibs, c.Properties.Static_libs...)
413 depNames.SharedLibs = append(depNames.SharedLibs, c.Properties.Shared_libs...)
Colin Cross21b9a242015-03-24 14:15:58 -0700414
Colin Cross21b9a242015-03-24 14:15:58 -0700415 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -0800416}
417
Colin Crossfa138792015-04-24 17:31:52 -0700418func (c *CCBase) AndroidDynamicDependencies(ctx common.AndroidDynamicDependerModuleContext) []string {
419 c.module.ModifyProperties(ctx)
420
Colin Cross74d1ec02015-04-28 13:30:13 -0700421 c.savedDepNames = c.module.depNames(ctx, CCDeps{})
422 c.savedDepNames.WholeStaticLibs = lastUniqueElements(c.savedDepNames.WholeStaticLibs)
423 c.savedDepNames.StaticLibs = lastUniqueElements(c.savedDepNames.StaticLibs)
424 c.savedDepNames.SharedLibs = lastUniqueElements(c.savedDepNames.SharedLibs)
425
426 staticLibs := c.savedDepNames.WholeStaticLibs
427 staticLibs = append(staticLibs, c.savedDepNames.StaticLibs...)
428 staticLibs = append(staticLibs, c.savedDepNames.LateStaticLibs...)
Colin Cross21b9a242015-03-24 14:15:58 -0700429 ctx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, staticLibs...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800430
Colin Cross74d1ec02015-04-28 13:30:13 -0700431 ctx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, c.savedDepNames.SharedLibs...)
Colin Cross21b9a242015-03-24 14:15:58 -0700432
Colin Cross74d1ec02015-04-28 13:30:13 -0700433 ret := append([]string(nil), c.savedDepNames.ObjFiles...)
434 if c.savedDepNames.CrtBegin != "" {
435 ret = append(ret, c.savedDepNames.CrtBegin)
Colin Cross21b9a242015-03-24 14:15:58 -0700436 }
Colin Cross74d1ec02015-04-28 13:30:13 -0700437 if c.savedDepNames.CrtEnd != "" {
438 ret = append(ret, c.savedDepNames.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700439 }
440
441 return ret
Colin Cross3f40fa42015-01-30 17:27:36 -0800442}
443
444// Create a ccFlags struct that collects the compile flags from global values,
445// per-target values, module type values, and per-module Blueprints properties
Colin Crossfa138792015-04-24 17:31:52 -0700446func (c *CCBase) collectFlags(ctx common.AndroidModuleContext, toolchain Toolchain) CCFlags {
Colin Cross97ba0732015-03-23 17:50:24 -0700447 flags := CCFlags{
Colin Crossfa138792015-04-24 17:31:52 -0700448 CFlags: c.Properties.Cflags,
449 CppFlags: c.Properties.Cppflags,
450 ConlyFlags: c.Properties.Conlyflags,
451 LdFlags: c.Properties.Ldflags,
452 AsFlags: c.Properties.Asflags,
453 YaccFlags: c.Properties.Yaccflags,
454 Nocrt: c.Properties.Nocrt,
Colin Cross97ba0732015-03-23 17:50:24 -0700455 Toolchain: toolchain,
Colin Crossfa138792015-04-24 17:31:52 -0700456 Clang: c.Properties.Clang,
Colin Cross3f40fa42015-01-30 17:27:36 -0800457 }
Colin Cross28344522015-04-22 13:07:53 -0700458
459 // Include dir cflags
Colin Crossf2298272015-05-12 11:36:53 -0700460 common.CheckSrcDirsExist(ctx, c.Properties.Include_dirs, "include_dirs")
461 common.CheckModuleSrcDirsExist(ctx, c.Properties.Local_include_dirs, "local_include_dirs")
462
Colin Crossfa138792015-04-24 17:31:52 -0700463 rootIncludeDirs := pathtools.PrefixPaths(c.Properties.Include_dirs, ctx.AConfig().SrcDir())
464 localIncludeDirs := pathtools.PrefixPaths(c.Properties.Local_include_dirs, common.ModuleSrcDir(ctx))
Colin Cross28344522015-04-22 13:07:53 -0700465 flags.GlobalFlags = append(flags.GlobalFlags,
Dan Willemsen1e898b92015-09-23 15:26:32 -0700466 includeDirsToFlags(localIncludeDirs),
467 includeDirsToFlags(rootIncludeDirs))
Colin Cross28344522015-04-22 13:07:53 -0700468
Colin Cross39d97f22015-09-14 12:30:50 -0700469 rootIncludeFiles := pathtools.PrefixPaths(c.Properties.Include_files, ctx.AConfig().SrcDir())
470 localIncludeFiles := pathtools.PrefixPaths(c.Properties.Local_include_files, common.ModuleSrcDir(ctx))
471
472 flags.GlobalFlags = append(flags.GlobalFlags,
473 includeFilesToFlags(rootIncludeFiles),
474 includeFilesToFlags(localIncludeFiles))
475
Colin Crossfa138792015-04-24 17:31:52 -0700476 if !c.Properties.No_default_compiler_flags {
477 if c.Properties.Sdk_version == "" || ctx.Host() {
Colin Cross28344522015-04-22 13:07:53 -0700478 flags.GlobalFlags = append(flags.GlobalFlags,
479 "${commonGlobalIncludes}",
480 toolchain.IncludeFlags(),
481 "-I${SrcDir}/libnativehelper/include/nativehelper")
482 }
483
484 flags.GlobalFlags = append(flags.GlobalFlags, []string{
485 "-I" + common.ModuleSrcDir(ctx),
486 "-I" + common.ModuleOutDir(ctx),
487 "-I" + common.ModuleGenDir(ctx),
488 }...)
489 }
490
Dan Willemsendd0e2c32015-10-20 14:29:35 -0700491 if !ctx.ContainsProperty("clang") {
492 if ctx.Host() {
493 flags.Clang = true
494 }
495
496 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
497 flags.Clang = true
498 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800499 }
500
Dan Willemsen6d11dd82015-11-03 14:27:00 -0800501 instructionSet := c.Properties.Instruction_set
502 instructionSetFlags, err := toolchain.InstructionSetFlags(instructionSet)
503 if flags.Clang {
504 instructionSetFlags, err = toolchain.ClangInstructionSetFlags(instructionSet)
505 }
506 if err != nil {
507 ctx.ModuleErrorf("%s", err)
508 }
509
510 // TODO: debug
511 flags.CFlags = append(flags.CFlags, c.Properties.Release.Cflags...)
512
Colin Cross97ba0732015-03-23 17:50:24 -0700513 if flags.Clang {
514 flags.CFlags = clangFilterUnknownCflags(flags.CFlags)
Colin Crossfa138792015-04-24 17:31:52 -0700515 flags.CFlags = append(flags.CFlags, c.Properties.Clang_cflags...)
516 flags.AsFlags = append(flags.AsFlags, c.Properties.Clang_asflags...)
Colin Cross97ba0732015-03-23 17:50:24 -0700517 flags.CppFlags = clangFilterUnknownCflags(flags.CppFlags)
518 flags.ConlyFlags = clangFilterUnknownCflags(flags.ConlyFlags)
519 flags.LdFlags = clangFilterUnknownCflags(flags.LdFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800520
Colin Cross97ba0732015-03-23 17:50:24 -0700521 flags.CFlags = append(flags.CFlags, "${clangExtraCflags}")
522 flags.ConlyFlags = append(flags.ConlyFlags, "${clangExtraConlyflags}")
Colin Crossf6566ed2015-03-24 11:13:38 -0700523 if ctx.Device() {
Colin Cross97ba0732015-03-23 17:50:24 -0700524 flags.CFlags = append(flags.CFlags, "${clangExtraTargetCflags}")
Colin Crossbdd7b1c2015-03-16 16:21:20 -0700525 }
526
Colin Cross3f40fa42015-01-30 17:27:36 -0800527 target := "-target " + toolchain.ClangTriple()
528 gccPrefix := "-B" + filepath.Join(toolchain.GccRoot(), toolchain.GccTriple(), "bin")
529
Colin Cross97ba0732015-03-23 17:50:24 -0700530 flags.CFlags = append(flags.CFlags, target, gccPrefix)
531 flags.AsFlags = append(flags.AsFlags, target, gccPrefix)
532 flags.LdFlags = append(flags.LdFlags, target, gccPrefix)
Colin Cross3f40fa42015-01-30 17:27:36 -0800533 }
534
Colin Crossfa138792015-04-24 17:31:52 -0700535 if !c.Properties.No_default_compiler_flags {
536 if ctx.Device() && !c.Properties.Allow_undefined_symbols {
Colin Cross97ba0732015-03-23 17:50:24 -0700537 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-undefined")
Colin Cross3f40fa42015-01-30 17:27:36 -0800538 }
539
Colin Cross56b4d452015-04-21 17:38:44 -0700540 flags.GlobalFlags = append(flags.GlobalFlags, instructionSetFlags)
541
Colin Cross97ba0732015-03-23 17:50:24 -0700542 if flags.Clang {
543 flags.CppFlags = append(flags.CppFlags, "${commonClangGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -0700544 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -0800545 toolchain.ClangCflags(),
546 "${commonClangGlobalCflags}",
Colin Crossd3ba0392015-05-07 14:11:29 -0700547 fmt.Sprintf("${%sClangGlobalCflags}", ctx.HostOrDevice()))
Colin Cross3f40fa42015-01-30 17:27:36 -0800548 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700549 flags.CppFlags = append(flags.CppFlags, "${commonGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -0700550 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -0800551 toolchain.Cflags(),
552 "${commonGlobalCflags}",
Colin Crossd3ba0392015-05-07 14:11:29 -0700553 fmt.Sprintf("${%sGlobalCflags}", ctx.HostOrDevice()))
Colin Cross3f40fa42015-01-30 17:27:36 -0800554 }
555
Colin Crossf6566ed2015-03-24 11:13:38 -0700556 if ctx.Device() {
Colin Crossfa138792015-04-24 17:31:52 -0700557 if c.Properties.Rtti {
Colin Cross97ba0732015-03-23 17:50:24 -0700558 flags.CppFlags = append(flags.CppFlags, "-frtti")
Colin Cross3f40fa42015-01-30 17:27:36 -0800559 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700560 flags.CppFlags = append(flags.CppFlags, "-fno-rtti")
Colin Cross3f40fa42015-01-30 17:27:36 -0800561 }
562 }
563
Colin Cross97ba0732015-03-23 17:50:24 -0700564 flags.AsFlags = append(flags.AsFlags, "-D__ASSEMBLY__")
Colin Cross3f40fa42015-01-30 17:27:36 -0800565
Colin Cross97ba0732015-03-23 17:50:24 -0700566 if flags.Clang {
567 flags.CppFlags = append(flags.CppFlags, toolchain.ClangCppflags())
568 flags.LdFlags = append(flags.LdFlags, toolchain.ClangLdflags())
Colin Cross3f40fa42015-01-30 17:27:36 -0800569 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700570 flags.CppFlags = append(flags.CppFlags, toolchain.Cppflags())
571 flags.LdFlags = append(flags.LdFlags, toolchain.Ldflags())
Colin Cross3f40fa42015-01-30 17:27:36 -0800572 }
Colin Cross28344522015-04-22 13:07:53 -0700573
574 if ctx.Host() {
Colin Crossfa138792015-04-24 17:31:52 -0700575 flags.LdFlags = append(flags.LdFlags, c.Properties.Host_ldlibs...)
Colin Cross28344522015-04-22 13:07:53 -0700576 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800577 }
578
Colin Cross0676e2d2015-04-24 17:39:18 -0700579 flags = c.ccModuleType().flags(ctx, flags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800580
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700581 if c.Properties.Sdk_version == "" {
582 if ctx.Host() && !flags.Clang {
583 // The host GCC doesn't support C++14 (and is deprecated, so likely
584 // never will). Build these modules with C++11.
585 flags.CppFlags = append(flags.CppFlags, "-std=gnu++11")
586 } else {
587 flags.CppFlags = append(flags.CppFlags, "-std=gnu++14")
588 }
589 }
590
591 flags.CFlags, _ = filterList(flags.CFlags, illegalFlags)
592 flags.CppFlags, _ = filterList(flags.CppFlags, illegalFlags)
593 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, illegalFlags)
594
Colin Cross3f40fa42015-01-30 17:27:36 -0800595 // Optimization to reduce size of build.ninja
596 // Replace the long list of flags for each file with a module-local variable
Colin Cross97ba0732015-03-23 17:50:24 -0700597 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
598 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
599 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
600 flags.CFlags = []string{"$cflags"}
601 flags.CppFlags = []string{"$cppflags"}
602 flags.AsFlags = []string{"$asflags"}
Colin Cross3f40fa42015-01-30 17:27:36 -0800603
604 return flags
605}
606
Colin Cross0676e2d2015-04-24 17:39:18 -0700607func (c *CCBase) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Cross3f40fa42015-01-30 17:27:36 -0800608 return flags
609}
610
611// Compile a list of source files into objects a specified subdirectory
Colin Crossfa138792015-04-24 17:31:52 -0700612func (c *CCBase) customCompileObjs(ctx common.AndroidModuleContext, flags CCFlags,
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700613 subdir string, srcFiles, excludes []string) []string {
Colin Cross581c1892015-04-07 16:50:10 -0700614
615 buildFlags := ccFlagsToBuilderFlags(flags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800616
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700617 srcFiles = ctx.ExpandSources(srcFiles, excludes)
Colin Cross581c1892015-04-07 16:50:10 -0700618 srcFiles, deps := genSources(ctx, srcFiles, buildFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800619
Colin Cross581c1892015-04-07 16:50:10 -0700620 return TransformSourceToObj(ctx, subdir, srcFiles, buildFlags, deps)
Colin Cross3f40fa42015-01-30 17:27:36 -0800621}
622
Colin Crossfa138792015-04-24 17:31:52 -0700623// Compile files listed in c.Properties.Srcs into objects
624func (c *CCBase) compileObjs(ctx common.AndroidModuleContext, flags CCFlags) []string {
Colin Cross3f40fa42015-01-30 17:27:36 -0800625
Colin Crossfa138792015-04-24 17:31:52 -0700626 if c.Properties.SkipCompileObjs {
Colin Cross3f40fa42015-01-30 17:27:36 -0800627 return nil
628 }
629
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700630 return c.customCompileObjs(ctx, flags, "", c.Properties.Srcs, c.Properties.Exclude_srcs)
Colin Cross3f40fa42015-01-30 17:27:36 -0800631}
632
Colin Cross5049f022015-03-18 13:28:46 -0700633// Compile generated source files from dependencies
Colin Crossfa138792015-04-24 17:31:52 -0700634func (c *CCBase) compileGeneratedObjs(ctx common.AndroidModuleContext, flags CCFlags) []string {
Colin Cross5049f022015-03-18 13:28:46 -0700635 var srcs []string
636
Colin Crossfa138792015-04-24 17:31:52 -0700637 if c.Properties.SkipCompileObjs {
Colin Cross5049f022015-03-18 13:28:46 -0700638 return nil
639 }
640
641 ctx.VisitDirectDeps(func(module blueprint.Module) {
642 if gen, ok := module.(genrule.SourceFileGenerator); ok {
643 srcs = append(srcs, gen.GeneratedSourceFiles()...)
644 }
645 })
646
647 if len(srcs) == 0 {
648 return nil
649 }
650
Colin Cross581c1892015-04-07 16:50:10 -0700651 return TransformSourceToObj(ctx, "", srcs, ccFlagsToBuilderFlags(flags), nil)
Colin Cross5049f022015-03-18 13:28:46 -0700652}
653
Colin Crossfa138792015-04-24 17:31:52 -0700654func (c *CCBase) outputFile() string {
Colin Cross3f40fa42015-01-30 17:27:36 -0800655 return ""
656}
657
Colin Crossfa138792015-04-24 17:31:52 -0700658func (c *CCBase) depsToPathsFromList(ctx common.AndroidModuleContext,
Colin Cross3f40fa42015-01-30 17:27:36 -0800659 names []string) (modules []common.AndroidModule,
Colin Cross28344522015-04-22 13:07:53 -0700660 outputFiles []string, exportedFlags []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800661
662 for _, n := range names {
663 found := false
664 ctx.VisitDirectDeps(func(m blueprint.Module) {
665 otherName := ctx.OtherModuleName(m)
666 if otherName != n {
667 return
668 }
669
Colin Cross97ba0732015-03-23 17:50:24 -0700670 if a, ok := m.(CCModuleType); ok {
Colin Cross3f40fa42015-01-30 17:27:36 -0800671 if a.Disabled() {
672 // If a cc_library host+device module depends on a library that exists as both
673 // cc_library_shared and cc_library_host_shared, it will end up with two
674 // dependencies with the same name, one of which is marked disabled for each
675 // of host and device. Ignore the disabled one.
676 return
677 }
Colin Crossd3ba0392015-05-07 14:11:29 -0700678 if a.HostOrDevice() != ctx.HostOrDevice() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800679 ctx.ModuleErrorf("host/device mismatch between %q and %q", ctx.ModuleName(),
680 otherName)
681 return
682 }
683
684 if outputFile := a.outputFile(); outputFile != "" {
685 if found {
686 ctx.ModuleErrorf("multiple modules satisified dependency on %q", otherName)
687 return
688 }
689 outputFiles = append(outputFiles, outputFile)
690 modules = append(modules, a)
Colin Cross28344522015-04-22 13:07:53 -0700691 if i, ok := a.(ccExportedFlagsProducer); ok {
692 exportedFlags = append(exportedFlags, i.exportedFlags()...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800693 }
694 found = true
695 } else {
696 ctx.ModuleErrorf("module %q missing output file", otherName)
697 return
698 }
699 } else {
700 ctx.ModuleErrorf("module %q not an android module", otherName)
701 return
702 }
703 })
704 if !found {
705 ctx.ModuleErrorf("unsatisified dependency on %q", n)
706 }
707 }
708
Colin Cross28344522015-04-22 13:07:53 -0700709 return modules, outputFiles, exportedFlags
Colin Cross3f40fa42015-01-30 17:27:36 -0800710}
711
Colin Cross21b9a242015-03-24 14:15:58 -0700712// Convert depenedency names to paths. Takes a CCDeps containing names and returns a CCDeps
713// containing paths
Colin Crossfa138792015-04-24 17:31:52 -0700714func (c *CCBase) depsToPaths(ctx common.AndroidModuleContext, depNames CCDeps) CCDeps {
Colin Cross21b9a242015-03-24 14:15:58 -0700715 var depPaths CCDeps
Colin Cross28344522015-04-22 13:07:53 -0700716 var newCflags []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800717
Colin Cross21b9a242015-03-24 14:15:58 -0700718 var wholeStaticLibModules []common.AndroidModule
Colin Cross3f40fa42015-01-30 17:27:36 -0800719
Colin Cross28344522015-04-22 13:07:53 -0700720 wholeStaticLibModules, depPaths.WholeStaticLibs, newCflags =
Colin Cross21b9a242015-03-24 14:15:58 -0700721 c.depsToPathsFromList(ctx, depNames.WholeStaticLibs)
Colin Cross28344522015-04-22 13:07:53 -0700722 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800723
Colin Cross21b9a242015-03-24 14:15:58 -0700724 for _, m := range wholeStaticLibModules {
725 if staticLib, ok := m.(ccLibraryInterface); ok && staticLib.static() {
726 depPaths.WholeStaticLibObjFiles =
727 append(depPaths.WholeStaticLibObjFiles, staticLib.allObjFiles()...)
728 } else {
729 ctx.ModuleErrorf("module %q not a static library", ctx.OtherModuleName(m))
730 }
731 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800732
Colin Cross28344522015-04-22 13:07:53 -0700733 _, depPaths.StaticLibs, newCflags = c.depsToPathsFromList(ctx, depNames.StaticLibs)
734 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross21b9a242015-03-24 14:15:58 -0700735
Colin Cross28344522015-04-22 13:07:53 -0700736 _, depPaths.LateStaticLibs, newCflags = c.depsToPathsFromList(ctx, depNames.LateStaticLibs)
737 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross21b9a242015-03-24 14:15:58 -0700738
Colin Cross28344522015-04-22 13:07:53 -0700739 _, depPaths.SharedLibs, newCflags = c.depsToPathsFromList(ctx, depNames.SharedLibs)
740 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross21b9a242015-03-24 14:15:58 -0700741
742 ctx.VisitDirectDeps(func(m blueprint.Module) {
Dan Albertc3144b12015-04-28 18:17:56 -0700743 if obj, ok := m.(ccObjectProvider); ok {
Colin Cross21b9a242015-03-24 14:15:58 -0700744 otherName := ctx.OtherModuleName(m)
745 if otherName == depNames.CrtBegin {
Colin Crossfa138792015-04-24 17:31:52 -0700746 if !c.Properties.Nocrt {
Dan Albertc3144b12015-04-28 18:17:56 -0700747 depPaths.CrtBegin = obj.object().outputFile()
Colin Cross21b9a242015-03-24 14:15:58 -0700748 }
749 } else if otherName == depNames.CrtEnd {
Colin Crossfa138792015-04-24 17:31:52 -0700750 if !c.Properties.Nocrt {
Dan Albertc3144b12015-04-28 18:17:56 -0700751 depPaths.CrtEnd = obj.object().outputFile()
Colin Cross21b9a242015-03-24 14:15:58 -0700752 }
753 } else {
Dan Albertc3144b12015-04-28 18:17:56 -0700754 depPaths.ObjFiles = append(depPaths.ObjFiles, obj.object().outputFile())
Colin Cross21b9a242015-03-24 14:15:58 -0700755 }
756 }
757 })
758
759 return depPaths
Colin Cross3f40fa42015-01-30 17:27:36 -0800760}
761
Colin Cross7d5136f2015-05-11 13:39:40 -0700762type ccLinkedProperties struct {
763 VariantIsShared bool `blueprint:"mutated"`
764 VariantIsStatic bool `blueprint:"mutated"`
765 VariantIsStaticBinary bool `blueprint:"mutated"`
766}
767
Colin Crossfa138792015-04-24 17:31:52 -0700768// CCLinked contains the properties and members used by libraries and executables
769type CCLinked struct {
770 CCBase
Colin Cross7d5136f2015-05-11 13:39:40 -0700771 dynamicProperties ccLinkedProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800772}
773
Colin Crossfa138792015-04-24 17:31:52 -0700774func newCCDynamic(dynamic *CCLinked, module CCModuleType, hod common.HostOrDeviceSupported,
Colin Crossc472d572015-03-17 15:06:21 -0700775 multilib common.Multilib, props ...interface{}) (blueprint.Module, []interface{}) {
776
Colin Crossed4cf0b2015-03-26 14:43:45 -0700777 props = append(props, &dynamic.dynamicProperties)
778
Colin Crossfa138792015-04-24 17:31:52 -0700779 return newCCBase(&dynamic.CCBase, module, hod, multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700780}
781
Colin Crossfa138792015-04-24 17:31:52 -0700782func (c *CCLinked) systemSharedLibs(ctx common.AndroidBaseContext) []string {
Colin Cross28d76592015-03-26 16:14:04 -0700783 if ctx.ContainsProperty("system_shared_libs") {
Colin Crossfa138792015-04-24 17:31:52 -0700784 return c.Properties.System_shared_libs
785 } else if ctx.Device() && c.Properties.Sdk_version == "" {
Colin Cross577f6e42015-03-27 18:23:34 -0700786 return []string{"libc", "libm"}
Colin Cross28d76592015-03-26 16:14:04 -0700787 } else {
Colin Cross577f6e42015-03-27 18:23:34 -0700788 return nil
Colin Cross3f40fa42015-01-30 17:27:36 -0800789 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800790}
791
Colin Crossfa138792015-04-24 17:31:52 -0700792func (c *CCLinked) stl(ctx common.AndroidBaseContext) string {
793 if c.Properties.Sdk_version != "" && ctx.Device() {
794 switch c.Properties.Stl {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700795 case "":
796 return "ndk_system"
797 case "c++_shared", "c++_static",
798 "stlport_shared", "stlport_static",
799 "gnustl_static":
Colin Crossfa138792015-04-24 17:31:52 -0700800 return "ndk_lib" + c.Properties.Stl
Colin Crossed4cf0b2015-03-26 14:43:45 -0700801 default:
Colin Crossfa138792015-04-24 17:31:52 -0700802 ctx.ModuleErrorf("stl: %q is not a supported STL with sdk_version set", c.Properties.Stl)
Colin Crossed4cf0b2015-03-26 14:43:45 -0700803 return ""
804 }
805 }
806
Colin Crossfa138792015-04-24 17:31:52 -0700807 switch c.Properties.Stl {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700808 case "libc++", "libc++_static",
Colin Crossed4cf0b2015-03-26 14:43:45 -0700809 "libstdc++":
Colin Crossfa138792015-04-24 17:31:52 -0700810 return c.Properties.Stl
Colin Crossed4cf0b2015-03-26 14:43:45 -0700811 case "none":
812 return ""
813 case "":
Colin Cross18b6dc52015-04-28 13:20:37 -0700814 if c.static() {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700815 return "libc++_static"
Colin Cross18b6dc52015-04-28 13:20:37 -0700816 } else {
817 return "libc++" // TODO: mingw needs libstdc++
Colin Crossed4cf0b2015-03-26 14:43:45 -0700818 }
819 default:
Colin Crossfa138792015-04-24 17:31:52 -0700820 ctx.ModuleErrorf("stl: %q is not a supported STL", c.Properties.Stl)
Colin Crossed4cf0b2015-03-26 14:43:45 -0700821 return ""
822 }
823}
824
Colin Cross0af4b842015-04-30 16:36:18 -0700825var hostDynamicGccLibs, hostStaticGccLibs []string
826
827func init() {
828 if runtime.GOOS == "darwin" {
829 hostDynamicGccLibs = []string{"-lc", "-lSystem"}
830 hostStaticGccLibs = []string{"NO_STATIC_HOST_BINARIES_ON_DARWIN"}
831 } else {
832 hostDynamicGccLibs = []string{"-lgcc_s", "-lgcc", "-lc", "-lgcc_s", "-lgcc"}
833 hostStaticGccLibs = []string{"-Wl,--start-group", "-lgcc", "-lgcc_eh", "-lc", "-Wl,--end-group"}
834 }
835}
Colin Cross712fc022015-04-27 11:13:34 -0700836
Colin Crosse11befc2015-04-27 17:49:17 -0700837func (c *CCLinked) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700838 stl := c.stl(ctx)
839 if ctx.Failed() {
840 return flags
841 }
842
843 switch stl {
844 case "libc++", "libc++_static":
845 flags.CFlags = append(flags.CFlags, "-D_USING_LIBCXX")
Colin Crossed4cf0b2015-03-26 14:43:45 -0700846 if ctx.Host() {
847 flags.CppFlags = append(flags.CppFlags, "-nostdinc++")
848 flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs")
Colin Cross712fc022015-04-27 11:13:34 -0700849 flags.LdFlags = append(flags.LdFlags, "-lm", "-lpthread")
Colin Cross18b6dc52015-04-28 13:20:37 -0700850 if c.staticBinary() {
Colin Cross712fc022015-04-27 11:13:34 -0700851 flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs...)
Colin Cross18b6dc52015-04-28 13:20:37 -0700852 } else {
853 flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs...)
Colin Cross712fc022015-04-27 11:13:34 -0700854 }
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700855 } else {
856 if ctx.Arch().ArchType == common.Arm {
857 flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,libunwind_llvm.a")
858 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700859 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700860 case "libstdc++":
861 // Using bionic's basic libstdc++. Not actually an STL. Only around until the
862 // tree is in good enough shape to not need it.
863 // Host builds will use GNU libstdc++.
864 if ctx.Device() {
Colin Cross28344522015-04-22 13:07:53 -0700865 flags.CFlags = append(flags.CFlags, "-I${SrcDir}/bionic/libstdc++/include")
Colin Crossed4cf0b2015-03-26 14:43:45 -0700866 }
867 case "ndk_system":
Colin Cross1332b002015-04-07 17:11:30 -0700868 ndkSrcRoot := ctx.AConfig().SrcDir() + "/prebuilts/ndk/current/sources/"
Colin Cross28344522015-04-22 13:07:53 -0700869 flags.CFlags = append(flags.CFlags, "-isystem "+ndkSrcRoot+"cxx-stl/system/include")
Colin Crossed4cf0b2015-03-26 14:43:45 -0700870 case "ndk_libc++_shared", "ndk_libc++_static":
871 // TODO(danalbert): This really shouldn't be here...
872 flags.CppFlags = append(flags.CppFlags, "-std=c++11")
873 case "ndk_libstlport_shared", "ndk_libstlport_static", "ndk_libgnustl_static":
874 // Nothing
875 case "":
876 // None or error.
877 if ctx.Host() {
878 flags.CppFlags = append(flags.CppFlags, "-nostdinc++")
879 flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs")
Colin Cross18b6dc52015-04-28 13:20:37 -0700880 if c.staticBinary() {
Colin Cross712fc022015-04-27 11:13:34 -0700881 flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs...)
Colin Cross18b6dc52015-04-28 13:20:37 -0700882 } else {
883 flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs...)
Colin Cross712fc022015-04-27 11:13:34 -0700884 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700885 }
886 default:
Colin Crossfa138792015-04-24 17:31:52 -0700887 panic(fmt.Errorf("Unknown stl in CCLinked.Flags: %q", stl))
Colin Crossed4cf0b2015-03-26 14:43:45 -0700888 }
889
890 return flags
891}
892
Colin Crosse11befc2015-04-27 17:49:17 -0700893func (c *CCLinked) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
894 depNames = c.CCBase.depNames(ctx, depNames)
Colin Cross3f40fa42015-01-30 17:27:36 -0800895
Colin Crossed4cf0b2015-03-26 14:43:45 -0700896 stl := c.stl(ctx)
897 if ctx.Failed() {
898 return depNames
899 }
900
901 switch stl {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700902 case "libstdc++":
903 if ctx.Device() {
904 depNames.SharedLibs = append(depNames.SharedLibs, stl)
905 }
Colin Cross74d1ec02015-04-28 13:30:13 -0700906 case "libc++", "libc++_static":
907 if stl == "libc++" {
908 depNames.SharedLibs = append(depNames.SharedLibs, stl)
909 } else {
910 depNames.StaticLibs = append(depNames.StaticLibs, stl)
911 }
912 if ctx.Device() {
913 if ctx.Arch().ArchType == common.Arm {
914 depNames.StaticLibs = append(depNames.StaticLibs, "libunwind_llvm")
915 }
916 if c.staticBinary() {
917 depNames.StaticLibs = append(depNames.StaticLibs, "libdl")
918 } else {
919 depNames.SharedLibs = append(depNames.SharedLibs, "libdl")
920 }
921 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700922 case "":
923 // None or error.
924 case "ndk_system":
925 // TODO: Make a system STL prebuilt for the NDK.
926 // 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 -0700927 // its own includes. The includes are handled in CCBase.Flags().
Colin Cross577f6e42015-03-27 18:23:34 -0700928 depNames.SharedLibs = append([]string{"libstdc++"}, depNames.SharedLibs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -0700929 case "ndk_libc++_shared", "ndk_libstlport_shared":
930 depNames.SharedLibs = append(depNames.SharedLibs, stl)
931 case "ndk_libc++_static", "ndk_libstlport_static", "ndk_libgnustl_static":
932 depNames.StaticLibs = append(depNames.StaticLibs, stl)
933 default:
Colin Crosse11befc2015-04-27 17:49:17 -0700934 panic(fmt.Errorf("Unknown stl in CCLinked.depNames: %q", stl))
Colin Crossed4cf0b2015-03-26 14:43:45 -0700935 }
936
Colin Cross74d1ec02015-04-28 13:30:13 -0700937 if ctx.ModuleName() != "libcompiler_rt-extras" {
938 depNames.StaticLibs = append(depNames.StaticLibs, "libcompiler_rt-extras")
939 }
940
Colin Crossf6566ed2015-03-24 11:13:38 -0700941 if ctx.Device() {
Colin Cross77b00fa2015-03-16 16:15:49 -0700942 // libgcc and libatomic have to be last on the command line
Dan Willemsend67be222015-09-16 15:19:33 -0700943 depNames.LateStaticLibs = append(depNames.LateStaticLibs, "libgcov", "libatomic")
944 if !c.Properties.No_libgcc {
945 depNames.LateStaticLibs = append(depNames.LateStaticLibs, "libgcc")
946 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700947
Colin Cross18b6dc52015-04-28 13:20:37 -0700948 if !c.static() {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700949 depNames.SharedLibs = append(depNames.SharedLibs, c.systemSharedLibs(ctx)...)
950 }
Colin Cross577f6e42015-03-27 18:23:34 -0700951
Colin Crossfa138792015-04-24 17:31:52 -0700952 if c.Properties.Sdk_version != "" {
953 version := c.Properties.Sdk_version
Colin Cross577f6e42015-03-27 18:23:34 -0700954 depNames.SharedLibs = append(depNames.SharedLibs,
955 "ndk_libc."+version,
956 "ndk_libm."+version,
957 )
958 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800959 }
960
Colin Cross21b9a242015-03-24 14:15:58 -0700961 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -0800962}
963
Colin Crossed4cf0b2015-03-26 14:43:45 -0700964// ccLinkedInterface interface is used on ccLinked to deal with static or shared variants
965type ccLinkedInterface interface {
966 // Returns true if the build options for the module have selected a static or shared build
967 buildStatic() bool
968 buildShared() bool
969
970 // Sets whether a specific variant is static or shared
Colin Cross18b6dc52015-04-28 13:20:37 -0700971 setStatic(bool)
Colin Crossed4cf0b2015-03-26 14:43:45 -0700972
Colin Cross18b6dc52015-04-28 13:20:37 -0700973 // Returns whether a specific variant is a static library or binary
Colin Crossed4cf0b2015-03-26 14:43:45 -0700974 static() bool
Colin Cross18b6dc52015-04-28 13:20:37 -0700975
976 // Returns whether a module is a static binary
977 staticBinary() bool
Colin Crossed4cf0b2015-03-26 14:43:45 -0700978}
979
980var _ ccLinkedInterface = (*CCLibrary)(nil)
981var _ ccLinkedInterface = (*CCBinary)(nil)
982
Colin Crossfa138792015-04-24 17:31:52 -0700983func (c *CCLinked) static() bool {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700984 return c.dynamicProperties.VariantIsStatic
985}
986
Colin Cross18b6dc52015-04-28 13:20:37 -0700987func (c *CCLinked) staticBinary() bool {
988 return c.dynamicProperties.VariantIsStaticBinary
Colin Crossed4cf0b2015-03-26 14:43:45 -0700989}
990
Colin Cross18b6dc52015-04-28 13:20:37 -0700991func (c *CCLinked) setStatic(static bool) {
992 c.dynamicProperties.VariantIsStatic = static
Colin Crossed4cf0b2015-03-26 14:43:45 -0700993}
994
Colin Cross28344522015-04-22 13:07:53 -0700995type ccExportedFlagsProducer interface {
996 exportedFlags() []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800997}
998
999//
1000// Combined static+shared libraries
1001//
1002
Colin Cross7d5136f2015-05-11 13:39:40 -07001003type CCLibraryProperties struct {
1004 BuildStatic bool `blueprint:"mutated"`
1005 BuildShared bool `blueprint:"mutated"`
1006 Static struct {
1007 Srcs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001008 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -07001009 Cflags []string `android:"arch_variant"`
1010 Whole_static_libs []string `android:"arch_variant"`
1011 Static_libs []string `android:"arch_variant"`
1012 Shared_libs []string `android:"arch_variant"`
1013 } `android:"arch_variant"`
1014 Shared struct {
1015 Srcs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001016 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -07001017 Cflags []string `android:"arch_variant"`
1018 Whole_static_libs []string `android:"arch_variant"`
1019 Static_libs []string `android:"arch_variant"`
1020 Shared_libs []string `android:"arch_variant"`
1021 } `android:"arch_variant"`
Colin Crossaee540a2015-07-06 17:48:31 -07001022
1023 // local file name to pass to the linker as --version_script
1024 Version_script string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -07001025}
1026
Colin Cross97ba0732015-03-23 17:50:24 -07001027type CCLibrary struct {
Colin Crossfa138792015-04-24 17:31:52 -07001028 CCLinked
Colin Cross3f40fa42015-01-30 17:27:36 -08001029
Colin Cross28344522015-04-22 13:07:53 -07001030 reuseFrom ccLibraryInterface
1031 reuseObjFiles []string
1032 objFiles []string
1033 exportFlags []string
1034 out string
Colin Cross3f40fa42015-01-30 17:27:36 -08001035
Colin Cross7d5136f2015-05-11 13:39:40 -07001036 LibraryProperties CCLibraryProperties
Colin Cross3f40fa42015-01-30 17:27:36 -08001037}
1038
Colin Crossed4cf0b2015-03-26 14:43:45 -07001039func (c *CCLibrary) buildStatic() bool {
1040 return c.LibraryProperties.BuildStatic
1041}
1042
1043func (c *CCLibrary) buildShared() bool {
1044 return c.LibraryProperties.BuildShared
1045}
1046
Colin Cross97ba0732015-03-23 17:50:24 -07001047type ccLibraryInterface interface {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001048 ccLinkedInterface
Colin Cross97ba0732015-03-23 17:50:24 -07001049 ccLibrary() *CCLibrary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001050 setReuseFrom(ccLibraryInterface)
1051 getReuseFrom() ccLibraryInterface
1052 getReuseObjFiles() []string
Colin Cross97ba0732015-03-23 17:50:24 -07001053 allObjFiles() []string
Colin Crossc472d572015-03-17 15:06:21 -07001054}
1055
Colin Crossed4cf0b2015-03-26 14:43:45 -07001056var _ ccLibraryInterface = (*CCLibrary)(nil)
1057
Colin Cross97ba0732015-03-23 17:50:24 -07001058func (c *CCLibrary) ccLibrary() *CCLibrary {
1059 return c
Colin Cross3f40fa42015-01-30 17:27:36 -08001060}
1061
Colin Cross97ba0732015-03-23 17:50:24 -07001062func NewCCLibrary(library *CCLibrary, module CCModuleType,
1063 hod common.HostOrDeviceSupported) (blueprint.Module, []interface{}) {
1064
Colin Crossfa138792015-04-24 17:31:52 -07001065 return newCCDynamic(&library.CCLinked, module, hod, common.MultilibBoth,
Colin Cross97ba0732015-03-23 17:50:24 -07001066 &library.LibraryProperties)
1067}
1068
1069func CCLibraryFactory() (blueprint.Module, []interface{}) {
1070 module := &CCLibrary{}
1071
1072 module.LibraryProperties.BuildShared = true
1073 module.LibraryProperties.BuildStatic = true
1074
1075 return NewCCLibrary(module, module, common.HostAndDeviceSupported)
1076}
1077
Colin Cross0676e2d2015-04-24 17:39:18 -07001078func (c *CCLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Crosse11befc2015-04-27 17:49:17 -07001079 depNames = c.CCLinked.depNames(ctx, depNames)
Colin Cross2732e9a2015-04-28 13:23:52 -07001080 if c.static() {
1081 depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.LibraryProperties.Static.Whole_static_libs...)
1082 depNames.StaticLibs = append(depNames.StaticLibs, c.LibraryProperties.Static.Static_libs...)
1083 depNames.SharedLibs = append(depNames.SharedLibs, c.LibraryProperties.Static.Shared_libs...)
1084 } else {
Colin Crossf6566ed2015-03-24 11:13:38 -07001085 if ctx.Device() {
Dan Albertc3144b12015-04-28 18:17:56 -07001086 if c.Properties.Sdk_version == "" {
1087 depNames.CrtBegin = "crtbegin_so"
1088 depNames.CrtEnd = "crtend_so"
1089 } else {
1090 depNames.CrtBegin = "ndk_crtbegin_so." + c.Properties.Sdk_version
1091 depNames.CrtEnd = "ndk_crtend_so." + c.Properties.Sdk_version
1092 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001093 }
Colin Cross2732e9a2015-04-28 13:23:52 -07001094 depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.LibraryProperties.Shared.Whole_static_libs...)
1095 depNames.StaticLibs = append(depNames.StaticLibs, c.LibraryProperties.Shared.Static_libs...)
1096 depNames.SharedLibs = append(depNames.SharedLibs, c.LibraryProperties.Shared.Shared_libs...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001097 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001098
Colin Cross21b9a242015-03-24 14:15:58 -07001099 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -08001100}
1101
Colin Cross97ba0732015-03-23 17:50:24 -07001102func (c *CCLibrary) outputFile() string {
Colin Cross3f40fa42015-01-30 17:27:36 -08001103 return c.out
1104}
1105
Colin Crossed4cf0b2015-03-26 14:43:45 -07001106func (c *CCLibrary) getReuseObjFiles() []string {
1107 return c.reuseObjFiles
1108}
1109
1110func (c *CCLibrary) setReuseFrom(reuseFrom ccLibraryInterface) {
1111 c.reuseFrom = reuseFrom
1112}
1113
1114func (c *CCLibrary) getReuseFrom() ccLibraryInterface {
1115 return c.reuseFrom
1116}
1117
Colin Cross97ba0732015-03-23 17:50:24 -07001118func (c *CCLibrary) allObjFiles() []string {
Colin Cross3f40fa42015-01-30 17:27:36 -08001119 return c.objFiles
1120}
1121
Colin Cross28344522015-04-22 13:07:53 -07001122func (c *CCLibrary) exportedFlags() []string {
1123 return c.exportFlags
Colin Cross3f40fa42015-01-30 17:27:36 -08001124}
1125
Colin Cross0676e2d2015-04-24 17:39:18 -07001126func (c *CCLibrary) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Crosse11befc2015-04-27 17:49:17 -07001127 flags = c.CCLinked.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001128
Colin Cross97ba0732015-03-23 17:50:24 -07001129 flags.CFlags = append(flags.CFlags, "-fPIC")
Colin Cross3f40fa42015-01-30 17:27:36 -08001130
Colin Crossd8e780d2015-04-28 17:39:43 -07001131 if c.static() {
1132 flags.CFlags = append(flags.CFlags, c.LibraryProperties.Static.Cflags...)
1133 } else {
1134 flags.CFlags = append(flags.CFlags, c.LibraryProperties.Shared.Cflags...)
1135 }
1136
Colin Cross18b6dc52015-04-28 13:20:37 -07001137 if !c.static() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001138 libName := ctx.ModuleName()
1139 // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead
1140 sharedFlag := "-Wl,-shared"
Dan Willemsendd0e2c32015-10-20 14:29:35 -07001141 if flags.Clang || ctx.Host() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001142 sharedFlag = "-shared"
1143 }
Colin Crossf6566ed2015-03-24 11:13:38 -07001144 if ctx.Device() {
Colin Cross97ba0732015-03-23 17:50:24 -07001145 flags.LdFlags = append(flags.LdFlags, "-nostdlib")
Colin Cross3f40fa42015-01-30 17:27:36 -08001146 }
Colin Cross97ba0732015-03-23 17:50:24 -07001147
Colin Cross0af4b842015-04-30 16:36:18 -07001148 if ctx.Darwin() {
1149 flags.LdFlags = append(flags.LdFlags,
1150 "-dynamiclib",
1151 "-single_module",
1152 //"-read_only_relocs suppress",
1153 "-install_name @rpath/"+libName+sharedLibraryExtension,
1154 )
1155 } else {
1156 flags.LdFlags = append(flags.LdFlags,
1157 "-Wl,--gc-sections",
1158 sharedFlag,
1159 "-Wl,-soname,"+libName+sharedLibraryExtension,
1160 )
1161 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001162 }
Colin Cross97ba0732015-03-23 17:50:24 -07001163
1164 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001165}
1166
Colin Cross97ba0732015-03-23 17:50:24 -07001167func (c *CCLibrary) compileStaticLibrary(ctx common.AndroidModuleContext,
1168 flags CCFlags, deps CCDeps, objFiles []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001169
1170 staticFlags := flags
Colin Cross581c1892015-04-07 16:50:10 -07001171 objFilesStatic := c.customCompileObjs(ctx, staticFlags, common.DeviceStaticLibrary,
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001172 c.LibraryProperties.Static.Srcs, c.LibraryProperties.Static.Exclude_srcs)
Colin Cross3f40fa42015-01-30 17:27:36 -08001173
1174 objFiles = append(objFiles, objFilesStatic...)
Colin Cross21b9a242015-03-24 14:15:58 -07001175 objFiles = append(objFiles, deps.WholeStaticLibObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001176
1177 outputFile := filepath.Join(common.ModuleOutDir(ctx), ctx.ModuleName()+staticLibraryExtension)
1178
Colin Cross0af4b842015-04-30 16:36:18 -07001179 if ctx.Darwin() {
1180 TransformDarwinObjToStaticLib(ctx, objFiles, ccFlagsToBuilderFlags(flags), outputFile)
1181 } else {
1182 TransformObjToStaticLib(ctx, objFiles, ccFlagsToBuilderFlags(flags), outputFile)
1183 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001184
1185 c.objFiles = objFiles
1186 c.out = outputFile
Colin Crossf2298272015-05-12 11:36:53 -07001187
1188 common.CheckModuleSrcDirsExist(ctx, c.Properties.Export_include_dirs, "export_include_dirs")
Colin Crossfa138792015-04-24 17:31:52 -07001189 includeDirs := pathtools.PrefixPaths(c.Properties.Export_include_dirs, common.ModuleSrcDir(ctx))
Colin Cross28344522015-04-22 13:07:53 -07001190 c.exportFlags = []string{includeDirsToFlags(includeDirs)}
Colin Cross3f40fa42015-01-30 17:27:36 -08001191
1192 ctx.CheckbuildFile(outputFile)
1193}
1194
Colin Cross97ba0732015-03-23 17:50:24 -07001195func (c *CCLibrary) compileSharedLibrary(ctx common.AndroidModuleContext,
1196 flags CCFlags, deps CCDeps, objFiles []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001197
1198 sharedFlags := flags
Colin Cross581c1892015-04-07 16:50:10 -07001199 objFilesShared := c.customCompileObjs(ctx, sharedFlags, common.DeviceSharedLibrary,
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001200 c.LibraryProperties.Shared.Srcs, c.LibraryProperties.Shared.Exclude_srcs)
Colin Cross3f40fa42015-01-30 17:27:36 -08001201
1202 objFiles = append(objFiles, objFilesShared...)
1203
1204 outputFile := filepath.Join(common.ModuleOutDir(ctx), ctx.ModuleName()+sharedLibraryExtension)
1205
Colin Crossaee540a2015-07-06 17:48:31 -07001206 var linkerDeps []string
1207
1208 if c.LibraryProperties.Version_script != "" {
1209 versionScript := filepath.Join(common.ModuleSrcDir(ctx), c.LibraryProperties.Version_script)
1210 sharedFlags.LdFlags = append(sharedFlags.LdFlags, "-Wl,--version-script,"+versionScript)
1211 linkerDeps = append(linkerDeps, versionScript)
1212 }
1213
Colin Cross97ba0732015-03-23 17:50:24 -07001214 TransformObjToDynamicBinary(ctx, objFiles, deps.SharedLibs, deps.StaticLibs,
Colin Crossaee540a2015-07-06 17:48:31 -07001215 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, false,
Colin Cross77b00fa2015-03-16 16:15:49 -07001216 ccFlagsToBuilderFlags(flags), outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08001217
1218 c.out = outputFile
Colin Crossfa138792015-04-24 17:31:52 -07001219 includeDirs := pathtools.PrefixPaths(c.Properties.Export_include_dirs, common.ModuleSrcDir(ctx))
Colin Cross28344522015-04-22 13:07:53 -07001220 c.exportFlags = []string{includeDirsToFlags(includeDirs)}
Colin Cross3f40fa42015-01-30 17:27:36 -08001221}
1222
Colin Cross97ba0732015-03-23 17:50:24 -07001223func (c *CCLibrary) compileModule(ctx common.AndroidModuleContext,
1224 flags CCFlags, deps CCDeps, objFiles []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001225
1226 // Reuse the object files from the matching static library if it exists
Colin Crossed4cf0b2015-03-26 14:43:45 -07001227 if c.getReuseFrom().ccLibrary() == c {
1228 c.reuseObjFiles = objFiles
Colin Cross3f40fa42015-01-30 17:27:36 -08001229 } else {
Colin Cross2732e9a2015-04-28 13:23:52 -07001230 if c.getReuseFrom().ccLibrary().LibraryProperties.Static.Cflags == nil &&
1231 c.LibraryProperties.Shared.Cflags == nil {
1232 objFiles = append([]string(nil), c.getReuseFrom().getReuseObjFiles()...)
1233 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001234 }
1235
Colin Crossed4cf0b2015-03-26 14:43:45 -07001236 if c.static() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001237 c.compileStaticLibrary(ctx, flags, deps, objFiles)
1238 } else {
1239 c.compileSharedLibrary(ctx, flags, deps, objFiles)
1240 }
1241}
1242
Colin Cross97ba0732015-03-23 17:50:24 -07001243func (c *CCLibrary) installStaticLibrary(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001244 // Static libraries do not get installed.
1245}
1246
Colin Cross97ba0732015-03-23 17:50:24 -07001247func (c *CCLibrary) installSharedLibrary(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001248 installDir := "lib"
Colin Cross97ba0732015-03-23 17:50:24 -07001249 if flags.Toolchain.Is64Bit() {
Dan Albertc403f7c2015-03-18 14:01:18 -07001250 installDir = "lib64"
1251 }
1252
Colin Crossfa138792015-04-24 17:31:52 -07001253 ctx.InstallFile(filepath.Join(installDir, c.Properties.Relative_install_path), c.out)
Dan Albertc403f7c2015-03-18 14:01:18 -07001254}
1255
Colin Cross97ba0732015-03-23 17:50:24 -07001256func (c *CCLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001257 if c.static() {
Dan Albertc403f7c2015-03-18 14:01:18 -07001258 c.installStaticLibrary(ctx, flags)
1259 } else {
1260 c.installSharedLibrary(ctx, flags)
1261 }
1262}
1263
Colin Cross3f40fa42015-01-30 17:27:36 -08001264//
1265// Objects (for crt*.o)
1266//
1267
Dan Albertc3144b12015-04-28 18:17:56 -07001268type ccObjectProvider interface {
1269 object() *ccObject
1270}
1271
Colin Cross3f40fa42015-01-30 17:27:36 -08001272type ccObject struct {
Colin Crossfa138792015-04-24 17:31:52 -07001273 CCBase
Colin Cross3f40fa42015-01-30 17:27:36 -08001274 out string
1275}
1276
Dan Albertc3144b12015-04-28 18:17:56 -07001277func (c *ccObject) object() *ccObject {
1278 return c
1279}
1280
Colin Cross97ba0732015-03-23 17:50:24 -07001281func CCObjectFactory() (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001282 module := &ccObject{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001283
Colin Crossfa138792015-04-24 17:31:52 -07001284 return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth)
Colin Cross3f40fa42015-01-30 17:27:36 -08001285}
1286
1287func (*ccObject) AndroidDynamicDependencies(ctx common.AndroidDynamicDependerModuleContext) []string {
1288 // object files can't have any dynamic dependencies
1289 return nil
1290}
1291
Colin Cross0676e2d2015-04-24 17:39:18 -07001292func (*ccObject) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Cross21b9a242015-03-24 14:15:58 -07001293 // object files can't have any dynamic dependencies
1294 return CCDeps{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001295}
1296
1297func (c *ccObject) compileModule(ctx common.AndroidModuleContext,
Colin Cross97ba0732015-03-23 17:50:24 -07001298 flags CCFlags, deps CCDeps, objFiles []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001299
Colin Cross97ba0732015-03-23 17:50:24 -07001300 objFiles = append(objFiles, deps.ObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001301
1302 var outputFile string
1303 if len(objFiles) == 1 {
1304 outputFile = objFiles[0]
1305 } else {
Dan Albertc3144b12015-04-28 18:17:56 -07001306 outputFile = filepath.Join(common.ModuleOutDir(ctx), ctx.ModuleName()+objectExtension)
Colin Cross3f40fa42015-01-30 17:27:36 -08001307 TransformObjsToObj(ctx, objFiles, ccFlagsToBuilderFlags(flags), outputFile)
1308 }
1309
1310 c.out = outputFile
1311
1312 ctx.CheckbuildFile(outputFile)
1313}
1314
Colin Cross97ba0732015-03-23 17:50:24 -07001315func (c *ccObject) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001316 // Object files do not get installed.
1317}
1318
Colin Cross3f40fa42015-01-30 17:27:36 -08001319func (c *ccObject) outputFile() string {
1320 return c.out
1321}
1322
Dan Albertc3144b12015-04-28 18:17:56 -07001323var _ ccObjectProvider = (*ccObject)(nil)
1324
Colin Cross3f40fa42015-01-30 17:27:36 -08001325//
1326// Executables
1327//
1328
Colin Cross7d5136f2015-05-11 13:39:40 -07001329type CCBinaryProperties struct {
1330 // compile executable with -static
1331 Static_executable bool
1332
1333 // set the name of the output
1334 Stem string `android:"arch_variant"`
1335
1336 // append to the name of the output
1337 Suffix string `android:"arch_variant"`
1338
1339 // if set, add an extra objcopy --prefix-symbols= step
1340 Prefix_symbols string
Colin Cross6002e052015-09-16 16:00:08 -07001341
1342 // Create a separate binary for each source file. Useful when there is
1343 // global state that can not be torn down and reset between each test suite.
1344 Test_per_src bool
Colin Cross7d5136f2015-05-11 13:39:40 -07001345}
1346
Colin Cross97ba0732015-03-23 17:50:24 -07001347type CCBinary struct {
Colin Crossfa138792015-04-24 17:31:52 -07001348 CCLinked
Dan Albertc403f7c2015-03-18 14:01:18 -07001349 out string
Colin Crossd350ecd2015-04-28 13:25:36 -07001350 installFile string
Colin Cross7d5136f2015-05-11 13:39:40 -07001351 BinaryProperties CCBinaryProperties
Colin Cross3f40fa42015-01-30 17:27:36 -08001352}
1353
Colin Crossed4cf0b2015-03-26 14:43:45 -07001354func (c *CCBinary) buildStatic() bool {
1355 return c.BinaryProperties.Static_executable
1356}
1357
1358func (c *CCBinary) buildShared() bool {
1359 return !c.BinaryProperties.Static_executable
1360}
1361
Colin Cross97ba0732015-03-23 17:50:24 -07001362func (c *CCBinary) getStem(ctx common.AndroidModuleContext) string {
Colin Cross4ae185c2015-03-26 15:12:10 -07001363 stem := ctx.ModuleName()
Colin Cross97ba0732015-03-23 17:50:24 -07001364 if c.BinaryProperties.Stem != "" {
Colin Cross4ae185c2015-03-26 15:12:10 -07001365 stem = c.BinaryProperties.Stem
Colin Cross3f40fa42015-01-30 17:27:36 -08001366 }
Colin Cross4ae185c2015-03-26 15:12:10 -07001367
1368 return stem + c.BinaryProperties.Suffix
Colin Cross3f40fa42015-01-30 17:27:36 -08001369}
1370
Colin Cross0676e2d2015-04-24 17:39:18 -07001371func (c *CCBinary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Crosse11befc2015-04-27 17:49:17 -07001372 depNames = c.CCLinked.depNames(ctx, depNames)
Colin Crossf6566ed2015-03-24 11:13:38 -07001373 if ctx.Device() {
Dan Albertc3144b12015-04-28 18:17:56 -07001374 if c.Properties.Sdk_version == "" {
1375 if c.BinaryProperties.Static_executable {
1376 depNames.CrtBegin = "crtbegin_static"
1377 } else {
1378 depNames.CrtBegin = "crtbegin_dynamic"
1379 }
1380 depNames.CrtEnd = "crtend_android"
Colin Cross3f40fa42015-01-30 17:27:36 -08001381 } else {
Dan Albertc3144b12015-04-28 18:17:56 -07001382 if c.BinaryProperties.Static_executable {
1383 depNames.CrtBegin = "ndk_crtbegin_static." + c.Properties.Sdk_version
1384 } else {
1385 depNames.CrtBegin = "ndk_crtbegin_dynamic." + c.Properties.Sdk_version
1386 }
1387 depNames.CrtEnd = "ndk_crtend_android." + c.Properties.Sdk_version
Colin Cross3f40fa42015-01-30 17:27:36 -08001388 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001389
1390 if c.BinaryProperties.Static_executable {
Colin Cross74d1ec02015-04-28 13:30:13 -07001391 if c.stl(ctx) == "libc++_static" {
1392 depNames.StaticLibs = append(depNames.StaticLibs, "libm", "libc", "libdl")
1393 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001394 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
1395 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
1396 // move them to the beginning of deps.LateStaticLibs
1397 var groupLibs []string
1398 depNames.StaticLibs, groupLibs = filterList(depNames.StaticLibs,
1399 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
1400 depNames.LateStaticLibs = append(groupLibs, depNames.LateStaticLibs...)
1401 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001402 }
Colin Cross21b9a242015-03-24 14:15:58 -07001403 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -08001404}
1405
Colin Cross97ba0732015-03-23 17:50:24 -07001406func NewCCBinary(binary *CCBinary, module CCModuleType,
Colin Cross1f8f2342015-03-26 16:09:47 -07001407 hod common.HostOrDeviceSupported, props ...interface{}) (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001408
Colin Cross1f8f2342015-03-26 16:09:47 -07001409 props = append(props, &binary.BinaryProperties)
1410
Colin Crossfa138792015-04-24 17:31:52 -07001411 return newCCDynamic(&binary.CCLinked, module, hod, common.MultilibFirst, props...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001412}
1413
Colin Cross97ba0732015-03-23 17:50:24 -07001414func CCBinaryFactory() (blueprint.Module, []interface{}) {
1415 module := &CCBinary{}
1416
1417 return NewCCBinary(module, module, common.HostAndDeviceSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001418}
1419
Colin Cross18b6dc52015-04-28 13:20:37 -07001420func (c *CCBinary) ModifyProperties(ctx common.AndroidBaseContext) {
Colin Cross0af4b842015-04-30 16:36:18 -07001421 if ctx.Darwin() {
1422 c.BinaryProperties.Static_executable = false
1423 }
Colin Cross18b6dc52015-04-28 13:20:37 -07001424 if c.BinaryProperties.Static_executable {
1425 c.dynamicProperties.VariantIsStaticBinary = true
1426 }
1427}
1428
Colin Cross0676e2d2015-04-24 17:39:18 -07001429func (c *CCBinary) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Crosse11befc2015-04-27 17:49:17 -07001430 flags = c.CCLinked.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001431
Colin Cross97ba0732015-03-23 17:50:24 -07001432 flags.CFlags = append(flags.CFlags, "-fpie")
1433
Colin Crossf6566ed2015-03-24 11:13:38 -07001434 if ctx.Device() {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001435 if c.BinaryProperties.Static_executable {
1436 // Clang driver needs -static to create static executable.
1437 // However, bionic/linker uses -shared to overwrite.
1438 // Linker for x86 targets does not allow coexistance of -static and -shared,
1439 // so we add -static only if -shared is not used.
1440 if !inList("-shared", flags.LdFlags) {
1441 flags.LdFlags = append(flags.LdFlags, "-static")
1442 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001443
Colin Crossed4cf0b2015-03-26 14:43:45 -07001444 flags.LdFlags = append(flags.LdFlags,
1445 "-nostdlib",
1446 "-Bstatic",
1447 "-Wl,--gc-sections",
1448 )
1449
1450 } else {
1451 linker := "/system/bin/linker"
1452 if flags.Toolchain.Is64Bit() {
1453 linker = "/system/bin/linker64"
1454 }
1455
1456 flags.LdFlags = append(flags.LdFlags,
1457 "-nostdlib",
1458 "-Bdynamic",
1459 fmt.Sprintf("-Wl,-dynamic-linker,%s", linker),
1460 "-Wl,--gc-sections",
1461 "-Wl,-z,nocopyreloc",
1462 )
1463 }
Colin Cross0af4b842015-04-30 16:36:18 -07001464 } else if ctx.Darwin() {
1465 flags.LdFlags = append(flags.LdFlags, "-Wl,-headerpad_max_install_names")
Colin Cross3f40fa42015-01-30 17:27:36 -08001466 }
1467
Colin Cross97ba0732015-03-23 17:50:24 -07001468 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001469}
1470
Colin Cross97ba0732015-03-23 17:50:24 -07001471func (c *CCBinary) compileModule(ctx common.AndroidModuleContext,
1472 flags CCFlags, deps CCDeps, objFiles []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001473
Colin Crossfa138792015-04-24 17:31:52 -07001474 if !c.BinaryProperties.Static_executable && inList("libc", c.Properties.Static_libs) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001475 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
1476 "from static libs or set static_executable: true")
1477 }
1478
1479 outputFile := filepath.Join(common.ModuleOutDir(ctx), c.getStem(ctx))
Dan Albertc403f7c2015-03-18 14:01:18 -07001480 c.out = outputFile
Colin Crossbfae8852015-03-26 14:44:11 -07001481 if c.BinaryProperties.Prefix_symbols != "" {
1482 afterPrefixSymbols := outputFile
1483 outputFile = outputFile + ".intermediate"
1484 TransformBinaryPrefixSymbols(ctx, c.BinaryProperties.Prefix_symbols, outputFile,
1485 ccFlagsToBuilderFlags(flags), afterPrefixSymbols)
1486 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001487
Colin Crossaee540a2015-07-06 17:48:31 -07001488 var linkerDeps []string
1489
Colin Cross97ba0732015-03-23 17:50:24 -07001490 TransformObjToDynamicBinary(ctx, objFiles, deps.SharedLibs, deps.StaticLibs,
Colin Crossaee540a2015-07-06 17:48:31 -07001491 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
Colin Cross77b00fa2015-03-16 16:15:49 -07001492 ccFlagsToBuilderFlags(flags), outputFile)
Dan Albertc403f7c2015-03-18 14:01:18 -07001493}
Colin Cross3f40fa42015-01-30 17:27:36 -08001494
Colin Cross97ba0732015-03-23 17:50:24 -07001495func (c *CCBinary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Colin Crossd350ecd2015-04-28 13:25:36 -07001496 c.installFile = ctx.InstallFile(filepath.Join("bin", c.Properties.Relative_install_path), c.out)
1497}
1498
1499func (c *CCBinary) HostToolPath() string {
1500 if c.HostOrDevice().Host() {
1501 return c.installFile
1502 }
1503 return ""
Dan Albertc403f7c2015-03-18 14:01:18 -07001504}
1505
Colin Cross6002e052015-09-16 16:00:08 -07001506func (c *CCBinary) testPerSrc() bool {
1507 return c.BinaryProperties.Test_per_src
1508}
1509
1510func (c *CCBinary) binary() *CCBinary {
1511 return c
1512}
1513
1514type testPerSrc interface {
1515 binary() *CCBinary
1516 testPerSrc() bool
1517}
1518
1519var _ testPerSrc = (*CCBinary)(nil)
1520
1521func TestPerSrcMutator(mctx blueprint.EarlyMutatorContext) {
1522 if test, ok := mctx.Module().(testPerSrc); ok {
1523 if test.testPerSrc() {
1524 testNames := make([]string, len(test.binary().Properties.Srcs))
1525 for i, src := range test.binary().Properties.Srcs {
1526 testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
1527 }
1528 tests := mctx.CreateLocalVariations(testNames...)
1529 for i, src := range test.binary().Properties.Srcs {
1530 tests[i].(testPerSrc).binary().Properties.Srcs = []string{src}
1531 tests[i].(testPerSrc).binary().BinaryProperties.Stem = mctx.ModuleName() + "_" + testNames[i]
1532 }
1533 }
1534 }
Colin Cross7d5136f2015-05-11 13:39:40 -07001535}
1536
Colin Cross9ffb4f52015-04-24 17:48:09 -07001537type CCTest struct {
Colin Cross97ba0732015-03-23 17:50:24 -07001538 CCBinary
Dan Albertc403f7c2015-03-18 14:01:18 -07001539}
1540
Colin Cross9ffb4f52015-04-24 17:48:09 -07001541func (c *CCTest) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Cross0676e2d2015-04-24 17:39:18 -07001542 flags = c.CCBinary.flags(ctx, flags)
Dan Albertc403f7c2015-03-18 14:01:18 -07001543
Colin Cross97ba0732015-03-23 17:50:24 -07001544 flags.CFlags = append(flags.CFlags, "-DGTEST_HAS_STD_STRING")
Colin Crossf6566ed2015-03-24 11:13:38 -07001545 if ctx.Host() {
Colin Cross97ba0732015-03-23 17:50:24 -07001546 flags.CFlags = append(flags.CFlags, "-O0", "-g")
Colin Cross28344522015-04-22 13:07:53 -07001547 flags.LdFlags = append(flags.LdFlags, "-lpthread")
Dan Albertc403f7c2015-03-18 14:01:18 -07001548 }
1549
1550 // TODO(danalbert): Make gtest export its dependencies.
Colin Cross28344522015-04-22 13:07:53 -07001551 flags.CFlags = append(flags.CFlags,
1552 "-I"+filepath.Join(ctx.AConfig().SrcDir(), "external/gtest/include"))
Dan Albertc403f7c2015-03-18 14:01:18 -07001553
Colin Cross21b9a242015-03-24 14:15:58 -07001554 return flags
Dan Albertc403f7c2015-03-18 14:01:18 -07001555}
1556
Colin Cross9ffb4f52015-04-24 17:48:09 -07001557func (c *CCTest) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Dan Willemsene6540452015-10-20 15:21:33 -07001558 depNames.StaticLibs = append(depNames.StaticLibs, "libgtest_main", "libgtest")
Colin Crossa8a93d32015-04-28 13:26:49 -07001559 depNames = c.CCBinary.depNames(ctx, depNames)
Colin Cross21b9a242015-03-24 14:15:58 -07001560 return depNames
Dan Albertc403f7c2015-03-18 14:01:18 -07001561}
1562
Colin Cross9ffb4f52015-04-24 17:48:09 -07001563func (c *CCTest) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Colin Crossf6566ed2015-03-24 11:13:38 -07001564 if ctx.Device() {
Colin Crossa8a93d32015-04-28 13:26:49 -07001565 ctx.InstallFile("../data/nativetest"+ctx.Arch().ArchType.Multilib[3:]+"/"+ctx.ModuleName(), c.out)
Dan Albertc403f7c2015-03-18 14:01:18 -07001566 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001567 c.CCBinary.installModule(ctx, flags)
Dan Albertc403f7c2015-03-18 14:01:18 -07001568 }
1569}
1570
Colin Cross9ffb4f52015-04-24 17:48:09 -07001571func NewCCTest(test *CCTest, module CCModuleType,
1572 hod common.HostOrDeviceSupported, props ...interface{}) (blueprint.Module, []interface{}) {
1573
Colin Cross9ffb4f52015-04-24 17:48:09 -07001574 return NewCCBinary(&test.CCBinary, module, hod, props...)
1575}
1576
1577func CCTestFactory() (blueprint.Module, []interface{}) {
1578 module := &CCTest{}
1579
1580 return NewCCTest(module, module, common.HostAndDeviceSupported)
1581}
1582
Colin Cross2ba19d92015-05-07 15:44:20 -07001583type CCBenchmark struct {
1584 CCBinary
1585}
1586
1587func (c *CCBenchmark) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
1588 depNames = c.CCBinary.depNames(ctx, depNames)
Dan Willemsenf8e98b02015-09-11 17:41:44 -07001589 depNames.StaticLibs = append(depNames.StaticLibs, "libbenchmark", "libbase")
Colin Cross2ba19d92015-05-07 15:44:20 -07001590 return depNames
1591}
1592
1593func (c *CCBenchmark) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
1594 if ctx.Device() {
1595 ctx.InstallFile("../data/nativetest"+ctx.Arch().ArchType.Multilib[3:]+"/"+ctx.ModuleName(), c.out)
1596 } else {
1597 c.CCBinary.installModule(ctx, flags)
1598 }
1599}
1600
1601func NewCCBenchmark(test *CCBenchmark, module CCModuleType,
1602 hod common.HostOrDeviceSupported, props ...interface{}) (blueprint.Module, []interface{}) {
1603
1604 return NewCCBinary(&test.CCBinary, module, hod, props...)
1605}
1606
1607func CCBenchmarkFactory() (blueprint.Module, []interface{}) {
1608 module := &CCBenchmark{}
1609
1610 return NewCCBenchmark(module, module, common.HostAndDeviceSupported)
1611}
1612
Colin Cross3f40fa42015-01-30 17:27:36 -08001613//
1614// Static library
1615//
1616
Colin Cross97ba0732015-03-23 17:50:24 -07001617func CCLibraryStaticFactory() (blueprint.Module, []interface{}) {
1618 module := &CCLibrary{}
1619 module.LibraryProperties.BuildStatic = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001620
Colin Cross97ba0732015-03-23 17:50:24 -07001621 return NewCCLibrary(module, module, common.HostAndDeviceSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001622}
1623
1624//
1625// Shared libraries
1626//
1627
Colin Cross97ba0732015-03-23 17:50:24 -07001628func CCLibrarySharedFactory() (blueprint.Module, []interface{}) {
1629 module := &CCLibrary{}
1630 module.LibraryProperties.BuildShared = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001631
Colin Cross97ba0732015-03-23 17:50:24 -07001632 return NewCCLibrary(module, module, common.HostAndDeviceSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001633}
1634
1635//
1636// Host static library
1637//
1638
Colin Cross97ba0732015-03-23 17:50:24 -07001639func CCLibraryHostStaticFactory() (blueprint.Module, []interface{}) {
1640 module := &CCLibrary{}
1641 module.LibraryProperties.BuildStatic = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001642
Colin Cross97ba0732015-03-23 17:50:24 -07001643 return NewCCLibrary(module, module, common.HostSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001644}
1645
1646//
1647// Host Shared libraries
1648//
1649
Colin Cross97ba0732015-03-23 17:50:24 -07001650func CCLibraryHostSharedFactory() (blueprint.Module, []interface{}) {
1651 module := &CCLibrary{}
1652 module.LibraryProperties.BuildShared = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001653
Colin Cross97ba0732015-03-23 17:50:24 -07001654 return NewCCLibrary(module, module, common.HostSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001655}
1656
1657//
1658// Host Binaries
1659//
1660
Colin Cross97ba0732015-03-23 17:50:24 -07001661func CCBinaryHostFactory() (blueprint.Module, []interface{}) {
1662 module := &CCBinary{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001663
Colin Cross97ba0732015-03-23 17:50:24 -07001664 return NewCCBinary(module, module, common.HostSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001665}
1666
1667//
Colin Cross1f8f2342015-03-26 16:09:47 -07001668// Host Tests
1669//
1670
1671func CCTestHostFactory() (blueprint.Module, []interface{}) {
Colin Cross9ffb4f52015-04-24 17:48:09 -07001672 module := &CCTest{}
Colin Cross6002e052015-09-16 16:00:08 -07001673 return NewCCBinary(&module.CCBinary, module, common.HostSupported)
Colin Cross1f8f2342015-03-26 16:09:47 -07001674}
1675
1676//
Colin Cross2ba19d92015-05-07 15:44:20 -07001677// Host Benchmarks
1678//
1679
1680func CCBenchmarkHostFactory() (blueprint.Module, []interface{}) {
1681 module := &CCBenchmark{}
1682 return NewCCBinary(&module.CCBinary, module, common.HostSupported)
1683}
1684
1685//
Colin Cross3f40fa42015-01-30 17:27:36 -08001686// Device libraries shipped with gcc
1687//
1688
1689type toolchainLibrary struct {
Colin Cross97ba0732015-03-23 17:50:24 -07001690 CCLibrary
Colin Cross3f40fa42015-01-30 17:27:36 -08001691}
1692
1693func (*toolchainLibrary) AndroidDynamicDependencies(ctx common.AndroidDynamicDependerModuleContext) []string {
1694 // toolchain libraries can't have any dependencies
1695 return nil
1696}
1697
Colin Cross0676e2d2015-04-24 17:39:18 -07001698func (*toolchainLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Cross3f40fa42015-01-30 17:27:36 -08001699 // toolchain libraries can't have any dependencies
Colin Cross21b9a242015-03-24 14:15:58 -07001700 return CCDeps{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001701}
1702
Colin Cross97ba0732015-03-23 17:50:24 -07001703func ToolchainLibraryFactory() (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001704 module := &toolchainLibrary{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001705
Colin Cross97ba0732015-03-23 17:50:24 -07001706 module.LibraryProperties.BuildStatic = true
1707
Colin Crossfa138792015-04-24 17:31:52 -07001708 return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth,
Colin Cross21b9a242015-03-24 14:15:58 -07001709 &module.LibraryProperties)
Colin Cross3f40fa42015-01-30 17:27:36 -08001710}
1711
1712func (c *toolchainLibrary) compileModule(ctx common.AndroidModuleContext,
Colin Cross97ba0732015-03-23 17:50:24 -07001713 flags CCFlags, deps CCDeps, objFiles []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001714
1715 libName := ctx.ModuleName() + staticLibraryExtension
1716 outputFile := filepath.Join(common.ModuleOutDir(ctx), libName)
1717
1718 CopyGccLib(ctx, libName, ccFlagsToBuilderFlags(flags), outputFile)
1719
1720 c.out = outputFile
1721
1722 ctx.CheckbuildFile(outputFile)
1723}
1724
Colin Cross97ba0732015-03-23 17:50:24 -07001725func (c *toolchainLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001726 // Toolchain libraries do not get installed.
1727}
1728
Dan Albertbe961682015-03-18 23:38:50 -07001729// NDK prebuilt libraries.
1730//
1731// These differ from regular prebuilts in that they aren't stripped and usually aren't installed
1732// either (with the exception of the shared STLs, which are installed to the app's directory rather
1733// than to the system image).
1734
1735func getNdkLibDir(ctx common.AndroidModuleContext, toolchain Toolchain, version string) string {
1736 return fmt.Sprintf("%s/prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/lib",
Colin Cross1332b002015-04-07 17:11:30 -07001737 ctx.AConfig().SrcDir(), version, toolchain.Name())
Dan Albertbe961682015-03-18 23:38:50 -07001738}
1739
Dan Albertc3144b12015-04-28 18:17:56 -07001740func ndkPrebuiltModuleToPath(ctx common.AndroidModuleContext, toolchain Toolchain,
1741 ext string, version string) string {
1742
1743 // NDK prebuilts are named like: ndk_NAME.EXT.SDK_VERSION.
1744 // We want to translate to just NAME.EXT
1745 name := strings.Split(strings.TrimPrefix(ctx.ModuleName(), "ndk_"), ".")[0]
1746 dir := getNdkLibDir(ctx, toolchain, version)
1747 return filepath.Join(dir, name+ext)
1748}
1749
1750type ndkPrebuiltObject struct {
1751 ccObject
1752}
1753
1754func (*ndkPrebuiltObject) AndroidDynamicDependencies(
1755 ctx common.AndroidDynamicDependerModuleContext) []string {
1756
1757 // NDK objects can't have any dependencies
1758 return nil
1759}
1760
1761func (*ndkPrebuiltObject) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
1762 // NDK objects can't have any dependencies
1763 return CCDeps{}
1764}
1765
1766func NdkPrebuiltObjectFactory() (blueprint.Module, []interface{}) {
1767 module := &ndkPrebuiltObject{}
1768 return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth)
1769}
1770
1771func (c *ndkPrebuiltObject) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
1772 deps CCDeps, objFiles []string) {
1773 // A null build step, but it sets up the output path.
1774 if !strings.HasPrefix(ctx.ModuleName(), "ndk_crt") {
1775 ctx.ModuleErrorf("NDK prebuilts must have an ndk_crt prefixed name")
1776 }
1777
1778 c.out = ndkPrebuiltModuleToPath(ctx, flags.Toolchain, objectExtension, c.Properties.Sdk_version)
1779}
1780
1781func (c *ndkPrebuiltObject) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
1782 // Objects do not get installed.
1783}
1784
1785var _ ccObjectProvider = (*ndkPrebuiltObject)(nil)
1786
Dan Albertbe961682015-03-18 23:38:50 -07001787type ndkPrebuiltLibrary struct {
1788 CCLibrary
1789}
1790
1791func (*ndkPrebuiltLibrary) AndroidDynamicDependencies(
1792 ctx common.AndroidDynamicDependerModuleContext) []string {
1793
1794 // NDK libraries can't have any dependencies
1795 return nil
1796}
1797
Colin Cross0676e2d2015-04-24 17:39:18 -07001798func (*ndkPrebuiltLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Dan Albertbe961682015-03-18 23:38:50 -07001799 // NDK libraries can't have any dependencies
1800 return CCDeps{}
1801}
1802
1803func NdkPrebuiltLibraryFactory() (blueprint.Module, []interface{}) {
1804 module := &ndkPrebuiltLibrary{}
1805 module.LibraryProperties.BuildShared = true
1806 return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
1807}
1808
1809func (c *ndkPrebuiltLibrary) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
1810 deps CCDeps, objFiles []string) {
1811 // A null build step, but it sets up the output path.
1812 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
1813 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
1814 }
1815
Colin Crossfa138792015-04-24 17:31:52 -07001816 includeDirs := pathtools.PrefixPaths(c.Properties.Export_include_dirs, common.ModuleSrcDir(ctx))
Colin Cross28344522015-04-22 13:07:53 -07001817 c.exportFlags = []string{common.JoinWithPrefix(includeDirs, "-isystem ")}
Dan Albertbe961682015-03-18 23:38:50 -07001818
Dan Albertc3144b12015-04-28 18:17:56 -07001819 c.out = ndkPrebuiltModuleToPath(ctx, flags.Toolchain, sharedLibraryExtension,
1820 c.Properties.Sdk_version)
Dan Albertbe961682015-03-18 23:38:50 -07001821}
1822
1823func (c *ndkPrebuiltLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc3144b12015-04-28 18:17:56 -07001824 // NDK prebuilt libraries do not get installed.
Dan Albertbe961682015-03-18 23:38:50 -07001825}
1826
1827// The NDK STLs are slightly different from the prebuilt system libraries:
1828// * Are not specific to each platform version.
1829// * The libraries are not in a predictable location for each STL.
1830
1831type ndkPrebuiltStl struct {
1832 ndkPrebuiltLibrary
1833}
1834
1835type ndkPrebuiltStaticStl struct {
1836 ndkPrebuiltStl
1837}
1838
1839type ndkPrebuiltSharedStl struct {
1840 ndkPrebuiltStl
1841}
1842
1843func NdkPrebuiltSharedStlFactory() (blueprint.Module, []interface{}) {
1844 module := &ndkPrebuiltSharedStl{}
1845 module.LibraryProperties.BuildShared = true
1846 return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
1847}
1848
1849func NdkPrebuiltStaticStlFactory() (blueprint.Module, []interface{}) {
1850 module := &ndkPrebuiltStaticStl{}
1851 module.LibraryProperties.BuildStatic = true
1852 return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
1853}
1854
1855func getNdkStlLibDir(ctx common.AndroidModuleContext, toolchain Toolchain, stl string) string {
1856 gccVersion := toolchain.GccVersion()
1857 var libDir string
1858 switch stl {
1859 case "libstlport":
1860 libDir = "cxx-stl/stlport/libs"
1861 case "libc++":
1862 libDir = "cxx-stl/llvm-libc++/libs"
1863 case "libgnustl":
1864 libDir = fmt.Sprintf("cxx-stl/gnu-libstdc++/%s/libs", gccVersion)
1865 }
1866
1867 if libDir != "" {
Colin Cross1332b002015-04-07 17:11:30 -07001868 ndkSrcRoot := ctx.AConfig().SrcDir() + "/prebuilts/ndk/current/sources"
Dan Albertbe961682015-03-18 23:38:50 -07001869 return fmt.Sprintf("%s/%s/%s", ndkSrcRoot, libDir, ctx.Arch().Abi)
1870 }
1871
1872 ctx.ModuleErrorf("Unknown NDK STL: %s", stl)
1873 return ""
1874}
1875
1876func (c *ndkPrebuiltStl) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
1877 deps CCDeps, objFiles []string) {
1878 // A null build step, but it sets up the output path.
1879 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
1880 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
1881 }
1882
Colin Crossfa138792015-04-24 17:31:52 -07001883 includeDirs := pathtools.PrefixPaths(c.Properties.Export_include_dirs, common.ModuleSrcDir(ctx))
Colin Cross28344522015-04-22 13:07:53 -07001884 c.exportFlags = []string{includeDirsToFlags(includeDirs)}
Dan Albertbe961682015-03-18 23:38:50 -07001885
1886 libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_")
1887 libExt := sharedLibraryExtension
1888 if c.LibraryProperties.BuildStatic {
1889 libExt = staticLibraryExtension
1890 }
1891
1892 stlName := strings.TrimSuffix(libName, "_shared")
1893 stlName = strings.TrimSuffix(stlName, "_static")
1894 libDir := getNdkStlLibDir(ctx, flags.Toolchain, stlName)
1895 c.out = libDir + "/" + libName + libExt
1896}
1897
Colin Cross3f40fa42015-01-30 17:27:36 -08001898func LinkageMutator(mctx blueprint.EarlyMutatorContext) {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001899 if c, ok := mctx.Module().(ccLinkedInterface); ok {
Colin Cross3f40fa42015-01-30 17:27:36 -08001900 var modules []blueprint.Module
Colin Crossed4cf0b2015-03-26 14:43:45 -07001901 if c.buildStatic() && c.buildShared() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001902 modules = mctx.CreateLocalVariations("static", "shared")
Colin Cross18b6dc52015-04-28 13:20:37 -07001903 modules[0].(ccLinkedInterface).setStatic(true)
1904 modules[1].(ccLinkedInterface).setStatic(false)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001905 } else if c.buildStatic() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001906 modules = mctx.CreateLocalVariations("static")
Colin Cross18b6dc52015-04-28 13:20:37 -07001907 modules[0].(ccLinkedInterface).setStatic(true)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001908 } else if c.buildShared() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001909 modules = mctx.CreateLocalVariations("shared")
Colin Cross18b6dc52015-04-28 13:20:37 -07001910 modules[0].(ccLinkedInterface).setStatic(false)
Colin Cross3f40fa42015-01-30 17:27:36 -08001911 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001912 panic(fmt.Errorf("ccLibrary %q not static or shared", mctx.ModuleName()))
Colin Cross3f40fa42015-01-30 17:27:36 -08001913 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001914
1915 if _, ok := c.(ccLibraryInterface); ok {
1916 reuseFrom := modules[0].(ccLibraryInterface)
1917 for _, m := range modules {
1918 m.(ccLibraryInterface).setReuseFrom(reuseFrom)
Colin Cross3f40fa42015-01-30 17:27:36 -08001919 }
1920 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001921 }
1922}
Colin Cross74d1ec02015-04-28 13:30:13 -07001923
1924// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
1925// modifies the slice contents in place, and returns a subslice of the original slice
1926func lastUniqueElements(list []string) []string {
1927 totalSkip := 0
1928 for i := len(list) - 1; i >= totalSkip; i-- {
1929 skip := 0
1930 for j := i - 1; j >= totalSkip; j-- {
1931 if list[i] == list[j] {
1932 skip++
1933 } else {
1934 list[j+skip] = list[j]
1935 }
1936 }
1937 totalSkip += skip
1938 }
1939 return list[totalSkip:]
1940}