blob: 7f4006675b074e61567796d916f162e959a1ab83 [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
67 LibcRoot = pctx.StaticVariable("LibcRoot", "${SrcDir}/bionic/libc")
68 LibmRoot = pctx.StaticVariable("LibmRoot", "${SrcDir}/bionic/libm")
69)
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",
82
83 // COMMON_RELEASE_CFLAGS
84 "-DNDEBUG",
85 "-UDEBUG",
86 }
87
88 deviceGlobalCflags = []string{
89 // TARGET_ERROR_FLAGS
90 "-Werror=return-type",
91 "-Werror=non-virtual-dtor",
92 "-Werror=address",
93 "-Werror=sequence-point",
94 }
95
96 hostGlobalCflags = []string{}
97
98 commonGlobalCppflags = []string{
99 "-Wsign-promo",
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700100 }
101
102 illegalFlags = []string{
103 "-w",
Colin Cross3f40fa42015-01-30 17:27:36 -0800104 }
105)
106
107func init() {
108 pctx.StaticVariable("commonGlobalCflags", strings.Join(commonGlobalCflags, " "))
109 pctx.StaticVariable("deviceGlobalCflags", strings.Join(deviceGlobalCflags, " "))
110 pctx.StaticVariable("hostGlobalCflags", strings.Join(hostGlobalCflags, " "))
111
112 pctx.StaticVariable("commonGlobalCppflags", strings.Join(commonGlobalCppflags, " "))
113
114 pctx.StaticVariable("commonClangGlobalCflags",
115 strings.Join(clangFilterUnknownCflags(commonGlobalCflags), " "))
116 pctx.StaticVariable("deviceClangGlobalCflags",
117 strings.Join(clangFilterUnknownCflags(deviceGlobalCflags), " "))
118 pctx.StaticVariable("hostClangGlobalCflags",
119 strings.Join(clangFilterUnknownCflags(hostGlobalCflags), " "))
Tim Kilbournf2948142015-03-11 12:03:03 -0700120 pctx.StaticVariable("commonClangGlobalCppflags",
121 strings.Join(clangFilterUnknownCflags(commonGlobalCppflags), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800122
123 // Everything in this list is a crime against abstraction and dependency tracking.
124 // Do not add anything to this list.
125 pctx.StaticVariable("commonGlobalIncludes", strings.Join([]string{
126 "-isystem ${SrcDir}/system/core/include",
127 "-isystem ${SrcDir}/hardware/libhardware/include",
128 "-isystem ${SrcDir}/hardware/libhardware_legacy/include",
129 "-isystem ${SrcDir}/hardware/ril/include",
130 "-isystem ${SrcDir}/libnativehelper/include",
131 "-isystem ${SrcDir}/frameworks/native/include",
132 "-isystem ${SrcDir}/frameworks/native/opengl/include",
133 "-isystem ${SrcDir}/frameworks/av/include",
134 "-isystem ${SrcDir}/frameworks/base/include",
135 }, " "))
136
137 pctx.StaticVariable("clangPath", "${SrcDir}/prebuilts/clang/${HostPrebuiltTag}/host/3.6/bin/")
138}
139
Colin Cross3f40fa42015-01-30 17:27:36 -0800140// Building C/C++ code is handled by objects that satisfy this interface via composition
Colin Cross97ba0732015-03-23 17:50:24 -0700141type CCModuleType interface {
Colin Cross3f40fa42015-01-30 17:27:36 -0800142 common.AndroidModule
143
Colin Crossfa138792015-04-24 17:31:52 -0700144 // Modify property values after parsing Blueprints file but before starting dependency
145 // resolution or build rule generation
146 ModifyProperties(common.AndroidBaseContext)
147
Colin Cross21b9a242015-03-24 14:15:58 -0700148 // Modify the ccFlags
Colin Cross0676e2d2015-04-24 17:39:18 -0700149 flags(common.AndroidModuleContext, CCFlags) CCFlags
Colin Cross3f40fa42015-01-30 17:27:36 -0800150
Colin Cross21b9a242015-03-24 14:15:58 -0700151 // Return list of dependency names for use in AndroidDynamicDependencies and in depsToPaths
Colin Cross0676e2d2015-04-24 17:39:18 -0700152 depNames(common.AndroidBaseContext, CCDeps) CCDeps
Colin Cross3f40fa42015-01-30 17:27:36 -0800153
154 // Compile objects into final module
Colin Cross97ba0732015-03-23 17:50:24 -0700155 compileModule(common.AndroidModuleContext, CCFlags, CCDeps, []string)
Colin Cross3f40fa42015-01-30 17:27:36 -0800156
Dan Albertc403f7c2015-03-18 14:01:18 -0700157 // Install the built module.
Colin Cross97ba0732015-03-23 17:50:24 -0700158 installModule(common.AndroidModuleContext, CCFlags)
Dan Albertc403f7c2015-03-18 14:01:18 -0700159
Colin Cross3f40fa42015-01-30 17:27:36 -0800160 // Return the output file (.o, .a or .so) for use by other modules
161 outputFile() string
162}
163
Colin Cross97ba0732015-03-23 17:50:24 -0700164type CCDeps struct {
Colin Cross28344522015-04-22 13:07:53 -0700165 StaticLibs, SharedLibs, LateStaticLibs, WholeStaticLibs, ObjFiles, Cflags []string
Colin Crossc472d572015-03-17 15:06:21 -0700166
Colin Cross21b9a242015-03-24 14:15:58 -0700167 WholeStaticLibObjFiles []string
168
Colin Cross97ba0732015-03-23 17:50:24 -0700169 CrtBegin, CrtEnd string
Colin Crossc472d572015-03-17 15:06:21 -0700170}
171
Colin Cross97ba0732015-03-23 17:50:24 -0700172type CCFlags struct {
Colin Cross28344522015-04-22 13:07:53 -0700173 GlobalFlags []string // Flags that apply to C, C++, and assembly source files
174 AsFlags []string // Flags that apply to assembly source files
175 CFlags []string // Flags that apply to C and C++ source files
176 ConlyFlags []string // Flags that apply to C source files
177 CppFlags []string // Flags that apply to C++ source files
178 YaccFlags []string // Flags that apply to Yacc source files
179 LdFlags []string // Flags that apply to linker command lines
180
181 Nocrt bool
182 Toolchain Toolchain
183 Clang bool
Colin Crossc472d572015-03-17 15:06:21 -0700184}
185
Colin Cross7d5136f2015-05-11 13:39:40 -0700186// Properties used to compile all C or C++ modules
187type CCBaseProperties struct {
188 // 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 -0700189 Srcs []string `android:"arch_variant"`
190
191 // list of source files that should not be used to build the C/C++ module.
192 // This is most useful in the arch/multilib variants to remove non-common files
193 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700194
195 // list of module-specific flags that will be used for C and C++ compiles.
196 Cflags []string `android:"arch_variant"`
197
198 // list of module-specific flags that will be used for C++ compiles
199 Cppflags []string `android:"arch_variant"`
200
201 // list of module-specific flags that will be used for C compiles
202 Conlyflags []string `android:"arch_variant"`
203
204 // list of module-specific flags that will be used for .S compiles
205 Asflags []string `android:"arch_variant"`
206
207 // list of module-specific flags that will be used for .y and .yy compiles
208 Yaccflags []string
209
210 // list of module-specific flags that will be used for all link steps
211 Ldflags []string `android:"arch_variant"`
212
213 // the instruction set architecture to use to compile the C/C++
214 // module.
215 Instruction_set string `android:"arch_variant"`
216
217 // list of directories relative to the root of the source tree that will
218 // be added to the include path using -I.
219 // If possible, don't use this. If adding paths from the current directory use
220 // local_include_dirs, if adding paths from other modules use export_include_dirs in
221 // that module.
222 Include_dirs []string `android:"arch_variant"`
223
Colin Cross39d97f22015-09-14 12:30:50 -0700224 // list of files relative to the root of the source tree that will be included
225 // using -include.
226 // If possible, don't use this.
227 Include_files []string `android:"arch_variant"`
228
Colin Cross7d5136f2015-05-11 13:39:40 -0700229 // list of directories relative to the Blueprints file that will
230 // be added to the include path using -I
231 Local_include_dirs []string `android:"arch_variant"`
232
Colin Cross39d97f22015-09-14 12:30:50 -0700233 // list of files relative to the Blueprints file that will be included
234 // using -include.
235 // If possible, don't use this.
236 Local_include_files []string `android:"arch_variant"`
237
Colin Cross7d5136f2015-05-11 13:39:40 -0700238 // list of directories relative to the Blueprints file that will
239 // be added to the include path using -I for any module that links against this module
240 Export_include_dirs []string `android:"arch_variant"`
241
242 // list of module-specific flags that will be used for C and C++ compiles when
243 // compiling with clang
244 Clang_cflags []string `android:"arch_variant"`
245
246 // list of module-specific flags that will be used for .S compiles when
247 // compiling with clang
248 Clang_asflags []string `android:"arch_variant"`
249
250 // list of system libraries that will be dynamically linked to
251 // shared library and executable modules. If unset, generally defaults to libc
252 // and libm. Set to [] to prevent linking against libc and libm.
253 System_shared_libs []string
254
255 // list of modules whose object files should be linked into this module
256 // in their entirety. For static library modules, all of the .o files from the intermediate
257 // directory of the dependency will be linked into this modules .a file. For a shared library,
258 // the dependency's .a file will be linked into this module using -Wl,--whole-archive.
259 Whole_static_libs []string `android:"arch_variant"`
260
261 // list of modules that should be statically linked into this module.
262 Static_libs []string `android:"arch_variant"`
263
264 // list of modules that should be dynamically linked into this module.
265 Shared_libs []string `android:"arch_variant"`
266
267 // allow the module to contain undefined symbols. By default,
268 // modules cannot contain undefined symbols that are not satisified by their immediate
269 // dependencies. Set this flag to true to remove --no-undefined from the linker flags.
270 // This flag should only be necessary for compiling low-level libraries like libc.
271 Allow_undefined_symbols bool
272
273 // don't link in crt_begin and crt_end. This flag should only be necessary for
274 // compiling crt or libc.
275 Nocrt bool `android:"arch_variant"`
276
277 // don't insert default compiler flags into asflags, cflags,
278 // cppflags, conlyflags, ldflags, or include_dirs
279 No_default_compiler_flags bool
280
281 // compile module with clang instead of gcc
282 Clang bool `android:"arch_variant"`
283
284 // pass -frtti instead of -fno-rtti
285 Rtti bool
286
287 // -l arguments to pass to linker for host-provided shared libraries
288 Host_ldlibs []string `android:"arch_variant"`
289
290 // select the STL library to use. Possible values are "libc++", "libc++_static",
291 // "stlport", "stlport_static", "ndk", "libstdc++", or "none". Leave blank to select the
292 // default
293 Stl string
294
295 // Set for combined shared/static libraries to prevent compiling object files a second time
296 SkipCompileObjs bool `blueprint:"mutated"`
297
298 Debug, Release struct {
299 // list of module-specific flags that will be used for C and C++ compiles in debug or
300 // release builds
301 Cflags []string `android:"arch_variant"`
302 } `android:"arch_variant"`
303
304 // Minimum sdk version supported when compiling against the ndk
305 Sdk_version string
306
307 // install to a subdirectory of the default install path for the module
308 Relative_install_path string
309}
310
Colin Crossfa138792015-04-24 17:31:52 -0700311// CCBase contains the properties and members used by all C/C++ module types, and implements
Colin Crossc472d572015-03-17 15:06:21 -0700312// the blueprint.Module interface. It expects to be embedded into an outer specialization struct,
313// and uses a ccModuleType interface to that struct to create the build steps.
Colin Crossfa138792015-04-24 17:31:52 -0700314type CCBase struct {
Colin Crossc472d572015-03-17 15:06:21 -0700315 common.AndroidModuleBase
Colin Cross97ba0732015-03-23 17:50:24 -0700316 module CCModuleType
Colin Crossc472d572015-03-17 15:06:21 -0700317
Colin Cross7d5136f2015-05-11 13:39:40 -0700318 Properties CCBaseProperties
Colin Crossfa138792015-04-24 17:31:52 -0700319
320 unused struct {
321 Asan bool
322 Native_coverage bool
323 Strip string
324 Tags []string
325 Required []string
326 }
Colin Crossc472d572015-03-17 15:06:21 -0700327
328 installPath string
Colin Cross74d1ec02015-04-28 13:30:13 -0700329
330 savedDepNames CCDeps
Colin Crossc472d572015-03-17 15:06:21 -0700331}
332
Colin Crossfa138792015-04-24 17:31:52 -0700333func newCCBase(base *CCBase, module CCModuleType, hod common.HostOrDeviceSupported,
Colin Crossc472d572015-03-17 15:06:21 -0700334 multilib common.Multilib, props ...interface{}) (blueprint.Module, []interface{}) {
335
336 base.module = module
337
Colin Crossfa138792015-04-24 17:31:52 -0700338 props = append(props, &base.Properties, &base.unused)
Colin Crossc472d572015-03-17 15:06:21 -0700339
Colin Cross5049f022015-03-18 13:28:46 -0700340 return common.InitAndroidArchModule(module, hod, multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700341}
342
Colin Crossfa138792015-04-24 17:31:52 -0700343func (c *CCBase) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800344 toolchain := c.findToolchain(ctx)
345 if ctx.Failed() {
346 return
347 }
348
Colin Cross21b9a242015-03-24 14:15:58 -0700349 flags := c.collectFlags(ctx, toolchain)
Colin Cross3f40fa42015-01-30 17:27:36 -0800350 if ctx.Failed() {
351 return
352 }
353
Colin Cross74d1ec02015-04-28 13:30:13 -0700354 deps := c.depsToPaths(ctx, c.savedDepNames)
Colin Cross3f40fa42015-01-30 17:27:36 -0800355 if ctx.Failed() {
356 return
357 }
358
Colin Cross28344522015-04-22 13:07:53 -0700359 flags.CFlags = append(flags.CFlags, deps.Cflags...)
Colin Crossed9f8682015-03-18 17:17:35 -0700360
Colin Cross581c1892015-04-07 16:50:10 -0700361 objFiles := c.compileObjs(ctx, flags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800362 if ctx.Failed() {
363 return
364 }
365
Colin Cross581c1892015-04-07 16:50:10 -0700366 generatedObjFiles := c.compileGeneratedObjs(ctx, flags)
Colin Cross5049f022015-03-18 13:28:46 -0700367 if ctx.Failed() {
368 return
369 }
370
371 objFiles = append(objFiles, generatedObjFiles...)
372
Colin Cross3f40fa42015-01-30 17:27:36 -0800373 c.ccModuleType().compileModule(ctx, flags, deps, objFiles)
374 if ctx.Failed() {
375 return
376 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700377
378 c.ccModuleType().installModule(ctx, flags)
379 if ctx.Failed() {
380 return
381 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800382}
383
Colin Crossfa138792015-04-24 17:31:52 -0700384func (c *CCBase) ccModuleType() CCModuleType {
Colin Cross3f40fa42015-01-30 17:27:36 -0800385 return c.module
386}
387
Colin Crossfa138792015-04-24 17:31:52 -0700388var _ common.AndroidDynamicDepender = (*CCBase)(nil)
Colin Cross3f40fa42015-01-30 17:27:36 -0800389
Colin Crossfa138792015-04-24 17:31:52 -0700390func (c *CCBase) findToolchain(ctx common.AndroidModuleContext) Toolchain {
Colin Cross3f40fa42015-01-30 17:27:36 -0800391 arch := ctx.Arch()
Colin Crossd3ba0392015-05-07 14:11:29 -0700392 hod := ctx.HostOrDevice()
393 factory := toolchainFactories[hod][arch.ArchType]
Colin Cross3f40fa42015-01-30 17:27:36 -0800394 if factory == nil {
395 panic(fmt.Sprintf("Toolchain not found for %s arch %q",
Colin Crossd3ba0392015-05-07 14:11:29 -0700396 hod.String(), arch.String()))
Colin Cross3f40fa42015-01-30 17:27:36 -0800397 }
398 return factory(arch.ArchVariant, arch.CpuVariant)
399}
400
Colin Crossfa138792015-04-24 17:31:52 -0700401func (c *CCBase) ModifyProperties(ctx common.AndroidBaseContext) {
402}
403
Colin Crosse11befc2015-04-27 17:49:17 -0700404func (c *CCBase) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Crossfa138792015-04-24 17:31:52 -0700405 depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.Properties.Whole_static_libs...)
406 depNames.StaticLibs = append(depNames.StaticLibs, c.Properties.Static_libs...)
407 depNames.SharedLibs = append(depNames.SharedLibs, c.Properties.Shared_libs...)
Colin Cross21b9a242015-03-24 14:15:58 -0700408
Colin Cross21b9a242015-03-24 14:15:58 -0700409 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -0800410}
411
Colin Crossfa138792015-04-24 17:31:52 -0700412func (c *CCBase) AndroidDynamicDependencies(ctx common.AndroidDynamicDependerModuleContext) []string {
413 c.module.ModifyProperties(ctx)
414
Colin Cross74d1ec02015-04-28 13:30:13 -0700415 c.savedDepNames = c.module.depNames(ctx, CCDeps{})
416 c.savedDepNames.WholeStaticLibs = lastUniqueElements(c.savedDepNames.WholeStaticLibs)
417 c.savedDepNames.StaticLibs = lastUniqueElements(c.savedDepNames.StaticLibs)
418 c.savedDepNames.SharedLibs = lastUniqueElements(c.savedDepNames.SharedLibs)
419
420 staticLibs := c.savedDepNames.WholeStaticLibs
421 staticLibs = append(staticLibs, c.savedDepNames.StaticLibs...)
422 staticLibs = append(staticLibs, c.savedDepNames.LateStaticLibs...)
Colin Cross21b9a242015-03-24 14:15:58 -0700423 ctx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, staticLibs...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800424
Colin Cross74d1ec02015-04-28 13:30:13 -0700425 ctx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, c.savedDepNames.SharedLibs...)
Colin Cross21b9a242015-03-24 14:15:58 -0700426
Colin Cross74d1ec02015-04-28 13:30:13 -0700427 ret := append([]string(nil), c.savedDepNames.ObjFiles...)
428 if c.savedDepNames.CrtBegin != "" {
429 ret = append(ret, c.savedDepNames.CrtBegin)
Colin Cross21b9a242015-03-24 14:15:58 -0700430 }
Colin Cross74d1ec02015-04-28 13:30:13 -0700431 if c.savedDepNames.CrtEnd != "" {
432 ret = append(ret, c.savedDepNames.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700433 }
434
435 return ret
Colin Cross3f40fa42015-01-30 17:27:36 -0800436}
437
438// Create a ccFlags struct that collects the compile flags from global values,
439// per-target values, module type values, and per-module Blueprints properties
Colin Crossfa138792015-04-24 17:31:52 -0700440func (c *CCBase) collectFlags(ctx common.AndroidModuleContext, toolchain Toolchain) CCFlags {
Colin Cross97ba0732015-03-23 17:50:24 -0700441 flags := CCFlags{
Colin Crossfa138792015-04-24 17:31:52 -0700442 CFlags: c.Properties.Cflags,
443 CppFlags: c.Properties.Cppflags,
444 ConlyFlags: c.Properties.Conlyflags,
445 LdFlags: c.Properties.Ldflags,
446 AsFlags: c.Properties.Asflags,
447 YaccFlags: c.Properties.Yaccflags,
448 Nocrt: c.Properties.Nocrt,
Colin Cross97ba0732015-03-23 17:50:24 -0700449 Toolchain: toolchain,
Colin Crossfa138792015-04-24 17:31:52 -0700450 Clang: c.Properties.Clang,
Colin Cross3f40fa42015-01-30 17:27:36 -0800451 }
Colin Cross28344522015-04-22 13:07:53 -0700452
453 // Include dir cflags
Colin Crossf2298272015-05-12 11:36:53 -0700454 common.CheckSrcDirsExist(ctx, c.Properties.Include_dirs, "include_dirs")
455 common.CheckModuleSrcDirsExist(ctx, c.Properties.Local_include_dirs, "local_include_dirs")
456
Colin Crossfa138792015-04-24 17:31:52 -0700457 rootIncludeDirs := pathtools.PrefixPaths(c.Properties.Include_dirs, ctx.AConfig().SrcDir())
458 localIncludeDirs := pathtools.PrefixPaths(c.Properties.Local_include_dirs, common.ModuleSrcDir(ctx))
Colin Cross28344522015-04-22 13:07:53 -0700459 flags.GlobalFlags = append(flags.GlobalFlags,
460 includeDirsToFlags(rootIncludeDirs),
461 includeDirsToFlags(localIncludeDirs))
462
Colin Cross39d97f22015-09-14 12:30:50 -0700463 rootIncludeFiles := pathtools.PrefixPaths(c.Properties.Include_files, ctx.AConfig().SrcDir())
464 localIncludeFiles := pathtools.PrefixPaths(c.Properties.Local_include_files, common.ModuleSrcDir(ctx))
465
466 flags.GlobalFlags = append(flags.GlobalFlags,
467 includeFilesToFlags(rootIncludeFiles),
468 includeFilesToFlags(localIncludeFiles))
469
Colin Crossfa138792015-04-24 17:31:52 -0700470 if !c.Properties.No_default_compiler_flags {
471 if c.Properties.Sdk_version == "" || ctx.Host() {
Colin Cross28344522015-04-22 13:07:53 -0700472 flags.GlobalFlags = append(flags.GlobalFlags,
473 "${commonGlobalIncludes}",
474 toolchain.IncludeFlags(),
475 "-I${SrcDir}/libnativehelper/include/nativehelper")
476 }
477
478 flags.GlobalFlags = append(flags.GlobalFlags, []string{
479 "-I" + common.ModuleSrcDir(ctx),
480 "-I" + common.ModuleOutDir(ctx),
481 "-I" + common.ModuleGenDir(ctx),
482 }...)
483 }
484
Colin Crossfa138792015-04-24 17:31:52 -0700485 instructionSet := c.Properties.Instruction_set
Tim Kilbourn1a9bf262015-03-18 12:28:32 -0700486 instructionSetFlags, err := toolchain.InstructionSetFlags(instructionSet)
487 if err != nil {
488 ctx.ModuleErrorf("%s", err)
489 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800490
Colin Crossaf19a292015-03-18 12:07:10 -0700491 // TODO: debug
Colin Crossfa138792015-04-24 17:31:52 -0700492 flags.CFlags = append(flags.CFlags, c.Properties.Release.Cflags...)
Colin Crossaf19a292015-03-18 12:07:10 -0700493
Colin Cross28d76592015-03-26 16:14:04 -0700494 if ctx.Host() && !ctx.ContainsProperty("clang") {
Colin Cross97ba0732015-03-23 17:50:24 -0700495 flags.Clang = true
Colin Cross3f40fa42015-01-30 17:27:36 -0800496 }
497
Colin Cross97ba0732015-03-23 17:50:24 -0700498 if flags.Clang {
499 flags.CFlags = clangFilterUnknownCflags(flags.CFlags)
Colin Crossfa138792015-04-24 17:31:52 -0700500 flags.CFlags = append(flags.CFlags, c.Properties.Clang_cflags...)
501 flags.AsFlags = append(flags.AsFlags, c.Properties.Clang_asflags...)
Colin Cross97ba0732015-03-23 17:50:24 -0700502 flags.CppFlags = clangFilterUnknownCflags(flags.CppFlags)
503 flags.ConlyFlags = clangFilterUnknownCflags(flags.ConlyFlags)
504 flags.LdFlags = clangFilterUnknownCflags(flags.LdFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800505
Colin Cross97ba0732015-03-23 17:50:24 -0700506 flags.CFlags = append(flags.CFlags, "${clangExtraCflags}")
507 flags.ConlyFlags = append(flags.ConlyFlags, "${clangExtraConlyflags}")
Colin Crossf6566ed2015-03-24 11:13:38 -0700508 if ctx.Device() {
Colin Cross97ba0732015-03-23 17:50:24 -0700509 flags.CFlags = append(flags.CFlags, "${clangExtraTargetCflags}")
Colin Crossbdd7b1c2015-03-16 16:21:20 -0700510 }
511
Colin Cross3f40fa42015-01-30 17:27:36 -0800512 target := "-target " + toolchain.ClangTriple()
513 gccPrefix := "-B" + filepath.Join(toolchain.GccRoot(), toolchain.GccTriple(), "bin")
514
Colin Cross97ba0732015-03-23 17:50:24 -0700515 flags.CFlags = append(flags.CFlags, target, gccPrefix)
516 flags.AsFlags = append(flags.AsFlags, target, gccPrefix)
517 flags.LdFlags = append(flags.LdFlags, target, gccPrefix)
Colin Cross3f40fa42015-01-30 17:27:36 -0800518 }
519
Colin Crossfa138792015-04-24 17:31:52 -0700520 if !c.Properties.No_default_compiler_flags {
521 if ctx.Device() && !c.Properties.Allow_undefined_symbols {
Colin Cross97ba0732015-03-23 17:50:24 -0700522 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-undefined")
Colin Cross3f40fa42015-01-30 17:27:36 -0800523 }
524
Colin Cross56b4d452015-04-21 17:38:44 -0700525 flags.GlobalFlags = append(flags.GlobalFlags, instructionSetFlags)
526
Colin Cross97ba0732015-03-23 17:50:24 -0700527 if flags.Clang {
528 flags.CppFlags = append(flags.CppFlags, "${commonClangGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -0700529 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -0800530 toolchain.ClangCflags(),
531 "${commonClangGlobalCflags}",
Colin Crossd3ba0392015-05-07 14:11:29 -0700532 fmt.Sprintf("${%sClangGlobalCflags}", ctx.HostOrDevice()))
Colin Cross3f40fa42015-01-30 17:27:36 -0800533 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700534 flags.CppFlags = append(flags.CppFlags, "${commonGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -0700535 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -0800536 toolchain.Cflags(),
537 "${commonGlobalCflags}",
Colin Crossd3ba0392015-05-07 14:11:29 -0700538 fmt.Sprintf("${%sGlobalCflags}", ctx.HostOrDevice()))
Colin Cross3f40fa42015-01-30 17:27:36 -0800539 }
540
Colin Crossf6566ed2015-03-24 11:13:38 -0700541 if ctx.Device() {
Colin Crossfa138792015-04-24 17:31:52 -0700542 if c.Properties.Rtti {
Colin Cross97ba0732015-03-23 17:50:24 -0700543 flags.CppFlags = append(flags.CppFlags, "-frtti")
Colin Cross3f40fa42015-01-30 17:27:36 -0800544 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700545 flags.CppFlags = append(flags.CppFlags, "-fno-rtti")
Colin Cross3f40fa42015-01-30 17:27:36 -0800546 }
547 }
548
Colin Cross97ba0732015-03-23 17:50:24 -0700549 flags.AsFlags = append(flags.AsFlags, "-D__ASSEMBLY__")
Colin Cross3f40fa42015-01-30 17:27:36 -0800550
Colin Cross97ba0732015-03-23 17:50:24 -0700551 if flags.Clang {
552 flags.CppFlags = append(flags.CppFlags, toolchain.ClangCppflags())
553 flags.LdFlags = append(flags.LdFlags, toolchain.ClangLdflags())
Colin Cross3f40fa42015-01-30 17:27:36 -0800554 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700555 flags.CppFlags = append(flags.CppFlags, toolchain.Cppflags())
556 flags.LdFlags = append(flags.LdFlags, toolchain.Ldflags())
Colin Cross3f40fa42015-01-30 17:27:36 -0800557 }
Colin Cross28344522015-04-22 13:07:53 -0700558
559 if ctx.Host() {
Colin Crossfa138792015-04-24 17:31:52 -0700560 flags.LdFlags = append(flags.LdFlags, c.Properties.Host_ldlibs...)
Colin Cross28344522015-04-22 13:07:53 -0700561 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800562 }
563
Colin Cross0676e2d2015-04-24 17:39:18 -0700564 flags = c.ccModuleType().flags(ctx, flags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800565
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700566 if c.Properties.Sdk_version == "" {
567 if ctx.Host() && !flags.Clang {
568 // The host GCC doesn't support C++14 (and is deprecated, so likely
569 // never will). Build these modules with C++11.
570 flags.CppFlags = append(flags.CppFlags, "-std=gnu++11")
571 } else {
572 flags.CppFlags = append(flags.CppFlags, "-std=gnu++14")
573 }
574 }
575
576 flags.CFlags, _ = filterList(flags.CFlags, illegalFlags)
577 flags.CppFlags, _ = filterList(flags.CppFlags, illegalFlags)
578 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, illegalFlags)
579
Colin Cross3f40fa42015-01-30 17:27:36 -0800580 // Optimization to reduce size of build.ninja
581 // Replace the long list of flags for each file with a module-local variable
Colin Cross97ba0732015-03-23 17:50:24 -0700582 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
583 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
584 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
585 flags.CFlags = []string{"$cflags"}
586 flags.CppFlags = []string{"$cppflags"}
587 flags.AsFlags = []string{"$asflags"}
Colin Cross3f40fa42015-01-30 17:27:36 -0800588
589 return flags
590}
591
Colin Cross0676e2d2015-04-24 17:39:18 -0700592func (c *CCBase) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Cross3f40fa42015-01-30 17:27:36 -0800593 return flags
594}
595
596// Compile a list of source files into objects a specified subdirectory
Colin Crossfa138792015-04-24 17:31:52 -0700597func (c *CCBase) customCompileObjs(ctx common.AndroidModuleContext, flags CCFlags,
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700598 subdir string, srcFiles, excludes []string) []string {
Colin Cross581c1892015-04-07 16:50:10 -0700599
600 buildFlags := ccFlagsToBuilderFlags(flags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800601
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700602 srcFiles = ctx.ExpandSources(srcFiles, excludes)
Colin Cross581c1892015-04-07 16:50:10 -0700603 srcFiles, deps := genSources(ctx, srcFiles, buildFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800604
Colin Cross581c1892015-04-07 16:50:10 -0700605 return TransformSourceToObj(ctx, subdir, srcFiles, buildFlags, deps)
Colin Cross3f40fa42015-01-30 17:27:36 -0800606}
607
Colin Crossfa138792015-04-24 17:31:52 -0700608// Compile files listed in c.Properties.Srcs into objects
609func (c *CCBase) compileObjs(ctx common.AndroidModuleContext, flags CCFlags) []string {
Colin Cross3f40fa42015-01-30 17:27:36 -0800610
Colin Crossfa138792015-04-24 17:31:52 -0700611 if c.Properties.SkipCompileObjs {
Colin Cross3f40fa42015-01-30 17:27:36 -0800612 return nil
613 }
614
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700615 return c.customCompileObjs(ctx, flags, "", c.Properties.Srcs, c.Properties.Exclude_srcs)
Colin Cross3f40fa42015-01-30 17:27:36 -0800616}
617
Colin Cross5049f022015-03-18 13:28:46 -0700618// Compile generated source files from dependencies
Colin Crossfa138792015-04-24 17:31:52 -0700619func (c *CCBase) compileGeneratedObjs(ctx common.AndroidModuleContext, flags CCFlags) []string {
Colin Cross5049f022015-03-18 13:28:46 -0700620 var srcs []string
621
Colin Crossfa138792015-04-24 17:31:52 -0700622 if c.Properties.SkipCompileObjs {
Colin Cross5049f022015-03-18 13:28:46 -0700623 return nil
624 }
625
626 ctx.VisitDirectDeps(func(module blueprint.Module) {
627 if gen, ok := module.(genrule.SourceFileGenerator); ok {
628 srcs = append(srcs, gen.GeneratedSourceFiles()...)
629 }
630 })
631
632 if len(srcs) == 0 {
633 return nil
634 }
635
Colin Cross581c1892015-04-07 16:50:10 -0700636 return TransformSourceToObj(ctx, "", srcs, ccFlagsToBuilderFlags(flags), nil)
Colin Cross5049f022015-03-18 13:28:46 -0700637}
638
Colin Crossfa138792015-04-24 17:31:52 -0700639func (c *CCBase) outputFile() string {
Colin Cross3f40fa42015-01-30 17:27:36 -0800640 return ""
641}
642
Colin Crossfa138792015-04-24 17:31:52 -0700643func (c *CCBase) depsToPathsFromList(ctx common.AndroidModuleContext,
Colin Cross3f40fa42015-01-30 17:27:36 -0800644 names []string) (modules []common.AndroidModule,
Colin Cross28344522015-04-22 13:07:53 -0700645 outputFiles []string, exportedFlags []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800646
647 for _, n := range names {
648 found := false
649 ctx.VisitDirectDeps(func(m blueprint.Module) {
650 otherName := ctx.OtherModuleName(m)
651 if otherName != n {
652 return
653 }
654
Colin Cross97ba0732015-03-23 17:50:24 -0700655 if a, ok := m.(CCModuleType); ok {
Colin Cross3f40fa42015-01-30 17:27:36 -0800656 if a.Disabled() {
657 // If a cc_library host+device module depends on a library that exists as both
658 // cc_library_shared and cc_library_host_shared, it will end up with two
659 // dependencies with the same name, one of which is marked disabled for each
660 // of host and device. Ignore the disabled one.
661 return
662 }
Colin Crossd3ba0392015-05-07 14:11:29 -0700663 if a.HostOrDevice() != ctx.HostOrDevice() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800664 ctx.ModuleErrorf("host/device mismatch between %q and %q", ctx.ModuleName(),
665 otherName)
666 return
667 }
668
669 if outputFile := a.outputFile(); outputFile != "" {
670 if found {
671 ctx.ModuleErrorf("multiple modules satisified dependency on %q", otherName)
672 return
673 }
674 outputFiles = append(outputFiles, outputFile)
675 modules = append(modules, a)
Colin Cross28344522015-04-22 13:07:53 -0700676 if i, ok := a.(ccExportedFlagsProducer); ok {
677 exportedFlags = append(exportedFlags, i.exportedFlags()...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800678 }
679 found = true
680 } else {
681 ctx.ModuleErrorf("module %q missing output file", otherName)
682 return
683 }
684 } else {
685 ctx.ModuleErrorf("module %q not an android module", otherName)
686 return
687 }
688 })
689 if !found {
690 ctx.ModuleErrorf("unsatisified dependency on %q", n)
691 }
692 }
693
Colin Cross28344522015-04-22 13:07:53 -0700694 return modules, outputFiles, exportedFlags
Colin Cross3f40fa42015-01-30 17:27:36 -0800695}
696
Colin Cross21b9a242015-03-24 14:15:58 -0700697// Convert depenedency names to paths. Takes a CCDeps containing names and returns a CCDeps
698// containing paths
Colin Crossfa138792015-04-24 17:31:52 -0700699func (c *CCBase) depsToPaths(ctx common.AndroidModuleContext, depNames CCDeps) CCDeps {
Colin Cross21b9a242015-03-24 14:15:58 -0700700 var depPaths CCDeps
Colin Cross28344522015-04-22 13:07:53 -0700701 var newCflags []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800702
Colin Cross21b9a242015-03-24 14:15:58 -0700703 var wholeStaticLibModules []common.AndroidModule
Colin Cross3f40fa42015-01-30 17:27:36 -0800704
Colin Cross28344522015-04-22 13:07:53 -0700705 wholeStaticLibModules, depPaths.WholeStaticLibs, newCflags =
Colin Cross21b9a242015-03-24 14:15:58 -0700706 c.depsToPathsFromList(ctx, depNames.WholeStaticLibs)
Colin Cross28344522015-04-22 13:07:53 -0700707 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800708
Colin Cross21b9a242015-03-24 14:15:58 -0700709 for _, m := range wholeStaticLibModules {
710 if staticLib, ok := m.(ccLibraryInterface); ok && staticLib.static() {
711 depPaths.WholeStaticLibObjFiles =
712 append(depPaths.WholeStaticLibObjFiles, staticLib.allObjFiles()...)
713 } else {
714 ctx.ModuleErrorf("module %q not a static library", ctx.OtherModuleName(m))
715 }
716 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800717
Colin Cross28344522015-04-22 13:07:53 -0700718 _, depPaths.StaticLibs, newCflags = c.depsToPathsFromList(ctx, depNames.StaticLibs)
719 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross21b9a242015-03-24 14:15:58 -0700720
Colin Cross28344522015-04-22 13:07:53 -0700721 _, depPaths.LateStaticLibs, newCflags = c.depsToPathsFromList(ctx, depNames.LateStaticLibs)
722 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross21b9a242015-03-24 14:15:58 -0700723
Colin Cross28344522015-04-22 13:07:53 -0700724 _, depPaths.SharedLibs, newCflags = c.depsToPathsFromList(ctx, depNames.SharedLibs)
725 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross21b9a242015-03-24 14:15:58 -0700726
727 ctx.VisitDirectDeps(func(m blueprint.Module) {
Dan Albertc3144b12015-04-28 18:17:56 -0700728 if obj, ok := m.(ccObjectProvider); ok {
Colin Cross21b9a242015-03-24 14:15:58 -0700729 otherName := ctx.OtherModuleName(m)
730 if otherName == depNames.CrtBegin {
Colin Crossfa138792015-04-24 17:31:52 -0700731 if !c.Properties.Nocrt {
Dan Albertc3144b12015-04-28 18:17:56 -0700732 depPaths.CrtBegin = obj.object().outputFile()
Colin Cross21b9a242015-03-24 14:15:58 -0700733 }
734 } else if otherName == depNames.CrtEnd {
Colin Crossfa138792015-04-24 17:31:52 -0700735 if !c.Properties.Nocrt {
Dan Albertc3144b12015-04-28 18:17:56 -0700736 depPaths.CrtEnd = obj.object().outputFile()
Colin Cross21b9a242015-03-24 14:15:58 -0700737 }
738 } else {
Dan Albertc3144b12015-04-28 18:17:56 -0700739 depPaths.ObjFiles = append(depPaths.ObjFiles, obj.object().outputFile())
Colin Cross21b9a242015-03-24 14:15:58 -0700740 }
741 }
742 })
743
744 return depPaths
Colin Cross3f40fa42015-01-30 17:27:36 -0800745}
746
Colin Cross7d5136f2015-05-11 13:39:40 -0700747type ccLinkedProperties struct {
748 VariantIsShared bool `blueprint:"mutated"`
749 VariantIsStatic bool `blueprint:"mutated"`
750 VariantIsStaticBinary bool `blueprint:"mutated"`
751}
752
Colin Crossfa138792015-04-24 17:31:52 -0700753// CCLinked contains the properties and members used by libraries and executables
754type CCLinked struct {
755 CCBase
Colin Cross7d5136f2015-05-11 13:39:40 -0700756 dynamicProperties ccLinkedProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800757}
758
Colin Crossfa138792015-04-24 17:31:52 -0700759func newCCDynamic(dynamic *CCLinked, module CCModuleType, hod common.HostOrDeviceSupported,
Colin Crossc472d572015-03-17 15:06:21 -0700760 multilib common.Multilib, props ...interface{}) (blueprint.Module, []interface{}) {
761
Colin Crossed4cf0b2015-03-26 14:43:45 -0700762 props = append(props, &dynamic.dynamicProperties)
763
Colin Crossfa138792015-04-24 17:31:52 -0700764 return newCCBase(&dynamic.CCBase, module, hod, multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700765}
766
Colin Crossfa138792015-04-24 17:31:52 -0700767func (c *CCLinked) systemSharedLibs(ctx common.AndroidBaseContext) []string {
Colin Cross28d76592015-03-26 16:14:04 -0700768 if ctx.ContainsProperty("system_shared_libs") {
Colin Crossfa138792015-04-24 17:31:52 -0700769 return c.Properties.System_shared_libs
770 } else if ctx.Device() && c.Properties.Sdk_version == "" {
Colin Cross577f6e42015-03-27 18:23:34 -0700771 return []string{"libc", "libm"}
Colin Cross28d76592015-03-26 16:14:04 -0700772 } else {
Colin Cross577f6e42015-03-27 18:23:34 -0700773 return nil
Colin Cross3f40fa42015-01-30 17:27:36 -0800774 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800775}
776
Colin Crossfa138792015-04-24 17:31:52 -0700777func (c *CCLinked) stl(ctx common.AndroidBaseContext) string {
778 if c.Properties.Sdk_version != "" && ctx.Device() {
779 switch c.Properties.Stl {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700780 case "":
781 return "ndk_system"
782 case "c++_shared", "c++_static",
783 "stlport_shared", "stlport_static",
784 "gnustl_static":
Colin Crossfa138792015-04-24 17:31:52 -0700785 return "ndk_lib" + c.Properties.Stl
Colin Crossed4cf0b2015-03-26 14:43:45 -0700786 default:
Colin Crossfa138792015-04-24 17:31:52 -0700787 ctx.ModuleErrorf("stl: %q is not a supported STL with sdk_version set", c.Properties.Stl)
Colin Crossed4cf0b2015-03-26 14:43:45 -0700788 return ""
789 }
790 }
791
Colin Crossfa138792015-04-24 17:31:52 -0700792 switch c.Properties.Stl {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700793 case "libc++", "libc++_static",
Colin Crossed4cf0b2015-03-26 14:43:45 -0700794 "libstdc++":
Colin Crossfa138792015-04-24 17:31:52 -0700795 return c.Properties.Stl
Colin Crossed4cf0b2015-03-26 14:43:45 -0700796 case "none":
797 return ""
798 case "":
Colin Cross18b6dc52015-04-28 13:20:37 -0700799 if c.static() {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700800 return "libc++_static"
Colin Cross18b6dc52015-04-28 13:20:37 -0700801 } else {
802 return "libc++" // TODO: mingw needs libstdc++
Colin Crossed4cf0b2015-03-26 14:43:45 -0700803 }
804 default:
Colin Crossfa138792015-04-24 17:31:52 -0700805 ctx.ModuleErrorf("stl: %q is not a supported STL", c.Properties.Stl)
Colin Crossed4cf0b2015-03-26 14:43:45 -0700806 return ""
807 }
808}
809
Colin Cross0af4b842015-04-30 16:36:18 -0700810var hostDynamicGccLibs, hostStaticGccLibs []string
811
812func init() {
813 if runtime.GOOS == "darwin" {
814 hostDynamicGccLibs = []string{"-lc", "-lSystem"}
815 hostStaticGccLibs = []string{"NO_STATIC_HOST_BINARIES_ON_DARWIN"}
816 } else {
817 hostDynamicGccLibs = []string{"-lgcc_s", "-lgcc", "-lc", "-lgcc_s", "-lgcc"}
818 hostStaticGccLibs = []string{"-Wl,--start-group", "-lgcc", "-lgcc_eh", "-lc", "-Wl,--end-group"}
819 }
820}
Colin Cross712fc022015-04-27 11:13:34 -0700821
Colin Crosse11befc2015-04-27 17:49:17 -0700822func (c *CCLinked) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700823 stl := c.stl(ctx)
824 if ctx.Failed() {
825 return flags
826 }
827
828 switch stl {
829 case "libc++", "libc++_static":
830 flags.CFlags = append(flags.CFlags, "-D_USING_LIBCXX")
Colin Cross28344522015-04-22 13:07:53 -0700831 flags.CFlags = append(flags.CFlags, "-I${SrcDir}/external/libcxx/include")
Colin Crossed4cf0b2015-03-26 14:43:45 -0700832 if ctx.Host() {
833 flags.CppFlags = append(flags.CppFlags, "-nostdinc++")
834 flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs")
Colin Cross712fc022015-04-27 11:13:34 -0700835 flags.LdFlags = append(flags.LdFlags, "-lm", "-lpthread")
Colin Cross18b6dc52015-04-28 13:20:37 -0700836 if c.staticBinary() {
Colin Cross712fc022015-04-27 11:13:34 -0700837 flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs...)
Colin Cross18b6dc52015-04-28 13:20:37 -0700838 } else {
839 flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs...)
Colin Cross712fc022015-04-27 11:13:34 -0700840 }
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700841 } else {
842 if ctx.Arch().ArchType == common.Arm {
843 flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,libunwind_llvm.a")
844 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700845 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700846 case "libstdc++":
847 // Using bionic's basic libstdc++. Not actually an STL. Only around until the
848 // tree is in good enough shape to not need it.
849 // Host builds will use GNU libstdc++.
850 if ctx.Device() {
Colin Cross28344522015-04-22 13:07:53 -0700851 flags.CFlags = append(flags.CFlags, "-I${SrcDir}/bionic/libstdc++/include")
Colin Crossed4cf0b2015-03-26 14:43:45 -0700852 }
853 case "ndk_system":
Colin Cross1332b002015-04-07 17:11:30 -0700854 ndkSrcRoot := ctx.AConfig().SrcDir() + "/prebuilts/ndk/current/sources/"
Colin Cross28344522015-04-22 13:07:53 -0700855 flags.CFlags = append(flags.CFlags, "-isystem "+ndkSrcRoot+"cxx-stl/system/include")
Colin Crossed4cf0b2015-03-26 14:43:45 -0700856 case "ndk_libc++_shared", "ndk_libc++_static":
857 // TODO(danalbert): This really shouldn't be here...
858 flags.CppFlags = append(flags.CppFlags, "-std=c++11")
859 case "ndk_libstlport_shared", "ndk_libstlport_static", "ndk_libgnustl_static":
860 // Nothing
861 case "":
862 // None or error.
863 if ctx.Host() {
864 flags.CppFlags = append(flags.CppFlags, "-nostdinc++")
865 flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs")
Colin Cross18b6dc52015-04-28 13:20:37 -0700866 if c.staticBinary() {
Colin Cross712fc022015-04-27 11:13:34 -0700867 flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs...)
Colin Cross18b6dc52015-04-28 13:20:37 -0700868 } else {
869 flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs...)
Colin Cross712fc022015-04-27 11:13:34 -0700870 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700871 }
872 default:
Colin Crossfa138792015-04-24 17:31:52 -0700873 panic(fmt.Errorf("Unknown stl in CCLinked.Flags: %q", stl))
Colin Crossed4cf0b2015-03-26 14:43:45 -0700874 }
875
876 return flags
877}
878
Colin Crosse11befc2015-04-27 17:49:17 -0700879func (c *CCLinked) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
880 depNames = c.CCBase.depNames(ctx, depNames)
Colin Cross3f40fa42015-01-30 17:27:36 -0800881
Colin Crossed4cf0b2015-03-26 14:43:45 -0700882 stl := c.stl(ctx)
883 if ctx.Failed() {
884 return depNames
885 }
886
887 switch stl {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700888 case "libstdc++":
889 if ctx.Device() {
890 depNames.SharedLibs = append(depNames.SharedLibs, stl)
891 }
Colin Cross74d1ec02015-04-28 13:30:13 -0700892 case "libc++", "libc++_static":
893 if stl == "libc++" {
894 depNames.SharedLibs = append(depNames.SharedLibs, stl)
895 } else {
896 depNames.StaticLibs = append(depNames.StaticLibs, stl)
897 }
898 if ctx.Device() {
899 if ctx.Arch().ArchType == common.Arm {
900 depNames.StaticLibs = append(depNames.StaticLibs, "libunwind_llvm")
901 }
902 if c.staticBinary() {
903 depNames.StaticLibs = append(depNames.StaticLibs, "libdl")
904 } else {
905 depNames.SharedLibs = append(depNames.SharedLibs, "libdl")
906 }
907 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700908 case "":
909 // None or error.
910 case "ndk_system":
911 // TODO: Make a system STL prebuilt for the NDK.
912 // 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 -0700913 // its own includes. The includes are handled in CCBase.Flags().
Colin Cross577f6e42015-03-27 18:23:34 -0700914 depNames.SharedLibs = append([]string{"libstdc++"}, depNames.SharedLibs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -0700915 case "ndk_libc++_shared", "ndk_libstlport_shared":
916 depNames.SharedLibs = append(depNames.SharedLibs, stl)
917 case "ndk_libc++_static", "ndk_libstlport_static", "ndk_libgnustl_static":
918 depNames.StaticLibs = append(depNames.StaticLibs, stl)
919 default:
Colin Crosse11befc2015-04-27 17:49:17 -0700920 panic(fmt.Errorf("Unknown stl in CCLinked.depNames: %q", stl))
Colin Crossed4cf0b2015-03-26 14:43:45 -0700921 }
922
Colin Cross74d1ec02015-04-28 13:30:13 -0700923 if ctx.ModuleName() != "libcompiler_rt-extras" {
924 depNames.StaticLibs = append(depNames.StaticLibs, "libcompiler_rt-extras")
925 }
926
Colin Crossf6566ed2015-03-24 11:13:38 -0700927 if ctx.Device() {
Colin Cross77b00fa2015-03-16 16:15:49 -0700928 // libgcc and libatomic have to be last on the command line
Colin Cross21b9a242015-03-24 14:15:58 -0700929 depNames.LateStaticLibs = append(depNames.LateStaticLibs, "libgcov", "libatomic", "libgcc")
Colin Crossed4cf0b2015-03-26 14:43:45 -0700930
Colin Cross18b6dc52015-04-28 13:20:37 -0700931 if !c.static() {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700932 depNames.SharedLibs = append(depNames.SharedLibs, c.systemSharedLibs(ctx)...)
933 }
Colin Cross577f6e42015-03-27 18:23:34 -0700934
Colin Crossfa138792015-04-24 17:31:52 -0700935 if c.Properties.Sdk_version != "" {
936 version := c.Properties.Sdk_version
Colin Cross577f6e42015-03-27 18:23:34 -0700937 depNames.SharedLibs = append(depNames.SharedLibs,
938 "ndk_libc."+version,
939 "ndk_libm."+version,
940 )
941 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800942 }
943
Colin Cross21b9a242015-03-24 14:15:58 -0700944 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -0800945}
946
Colin Crossed4cf0b2015-03-26 14:43:45 -0700947// ccLinkedInterface interface is used on ccLinked to deal with static or shared variants
948type ccLinkedInterface interface {
949 // Returns true if the build options for the module have selected a static or shared build
950 buildStatic() bool
951 buildShared() bool
952
953 // Sets whether a specific variant is static or shared
Colin Cross18b6dc52015-04-28 13:20:37 -0700954 setStatic(bool)
Colin Crossed4cf0b2015-03-26 14:43:45 -0700955
Colin Cross18b6dc52015-04-28 13:20:37 -0700956 // Returns whether a specific variant is a static library or binary
Colin Crossed4cf0b2015-03-26 14:43:45 -0700957 static() bool
Colin Cross18b6dc52015-04-28 13:20:37 -0700958
959 // Returns whether a module is a static binary
960 staticBinary() bool
Colin Crossed4cf0b2015-03-26 14:43:45 -0700961}
962
963var _ ccLinkedInterface = (*CCLibrary)(nil)
964var _ ccLinkedInterface = (*CCBinary)(nil)
965
Colin Crossfa138792015-04-24 17:31:52 -0700966func (c *CCLinked) static() bool {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700967 return c.dynamicProperties.VariantIsStatic
968}
969
Colin Cross18b6dc52015-04-28 13:20:37 -0700970func (c *CCLinked) staticBinary() bool {
971 return c.dynamicProperties.VariantIsStaticBinary
Colin Crossed4cf0b2015-03-26 14:43:45 -0700972}
973
Colin Cross18b6dc52015-04-28 13:20:37 -0700974func (c *CCLinked) setStatic(static bool) {
975 c.dynamicProperties.VariantIsStatic = static
Colin Crossed4cf0b2015-03-26 14:43:45 -0700976}
977
Colin Cross28344522015-04-22 13:07:53 -0700978type ccExportedFlagsProducer interface {
979 exportedFlags() []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800980}
981
982//
983// Combined static+shared libraries
984//
985
Colin Cross7d5136f2015-05-11 13:39:40 -0700986type CCLibraryProperties struct {
987 BuildStatic bool `blueprint:"mutated"`
988 BuildShared bool `blueprint:"mutated"`
989 Static struct {
990 Srcs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700991 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700992 Cflags []string `android:"arch_variant"`
993 Whole_static_libs []string `android:"arch_variant"`
994 Static_libs []string `android:"arch_variant"`
995 Shared_libs []string `android:"arch_variant"`
996 } `android:"arch_variant"`
997 Shared struct {
998 Srcs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700999 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -07001000 Cflags []string `android:"arch_variant"`
1001 Whole_static_libs []string `android:"arch_variant"`
1002 Static_libs []string `android:"arch_variant"`
1003 Shared_libs []string `android:"arch_variant"`
1004 } `android:"arch_variant"`
Colin Crossaee540a2015-07-06 17:48:31 -07001005
1006 // local file name to pass to the linker as --version_script
1007 Version_script string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -07001008}
1009
Colin Cross97ba0732015-03-23 17:50:24 -07001010type CCLibrary struct {
Colin Crossfa138792015-04-24 17:31:52 -07001011 CCLinked
Colin Cross3f40fa42015-01-30 17:27:36 -08001012
Colin Cross28344522015-04-22 13:07:53 -07001013 reuseFrom ccLibraryInterface
1014 reuseObjFiles []string
1015 objFiles []string
1016 exportFlags []string
1017 out string
Colin Cross3f40fa42015-01-30 17:27:36 -08001018
Colin Cross7d5136f2015-05-11 13:39:40 -07001019 LibraryProperties CCLibraryProperties
Colin Cross3f40fa42015-01-30 17:27:36 -08001020}
1021
Colin Crossed4cf0b2015-03-26 14:43:45 -07001022func (c *CCLibrary) buildStatic() bool {
1023 return c.LibraryProperties.BuildStatic
1024}
1025
1026func (c *CCLibrary) buildShared() bool {
1027 return c.LibraryProperties.BuildShared
1028}
1029
Colin Cross97ba0732015-03-23 17:50:24 -07001030type ccLibraryInterface interface {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001031 ccLinkedInterface
Colin Cross97ba0732015-03-23 17:50:24 -07001032 ccLibrary() *CCLibrary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001033 setReuseFrom(ccLibraryInterface)
1034 getReuseFrom() ccLibraryInterface
1035 getReuseObjFiles() []string
Colin Cross97ba0732015-03-23 17:50:24 -07001036 allObjFiles() []string
Colin Crossc472d572015-03-17 15:06:21 -07001037}
1038
Colin Crossed4cf0b2015-03-26 14:43:45 -07001039var _ ccLibraryInterface = (*CCLibrary)(nil)
1040
Colin Cross97ba0732015-03-23 17:50:24 -07001041func (c *CCLibrary) ccLibrary() *CCLibrary {
1042 return c
Colin Cross3f40fa42015-01-30 17:27:36 -08001043}
1044
Colin Cross97ba0732015-03-23 17:50:24 -07001045func NewCCLibrary(library *CCLibrary, module CCModuleType,
1046 hod common.HostOrDeviceSupported) (blueprint.Module, []interface{}) {
1047
Colin Crossfa138792015-04-24 17:31:52 -07001048 return newCCDynamic(&library.CCLinked, module, hod, common.MultilibBoth,
Colin Cross97ba0732015-03-23 17:50:24 -07001049 &library.LibraryProperties)
1050}
1051
1052func CCLibraryFactory() (blueprint.Module, []interface{}) {
1053 module := &CCLibrary{}
1054
1055 module.LibraryProperties.BuildShared = true
1056 module.LibraryProperties.BuildStatic = true
1057
1058 return NewCCLibrary(module, module, common.HostAndDeviceSupported)
1059}
1060
Colin Cross0676e2d2015-04-24 17:39:18 -07001061func (c *CCLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Crosse11befc2015-04-27 17:49:17 -07001062 depNames = c.CCLinked.depNames(ctx, depNames)
Colin Cross2732e9a2015-04-28 13:23:52 -07001063 if c.static() {
1064 depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.LibraryProperties.Static.Whole_static_libs...)
1065 depNames.StaticLibs = append(depNames.StaticLibs, c.LibraryProperties.Static.Static_libs...)
1066 depNames.SharedLibs = append(depNames.SharedLibs, c.LibraryProperties.Static.Shared_libs...)
1067 } else {
Colin Crossf6566ed2015-03-24 11:13:38 -07001068 if ctx.Device() {
Dan Albertc3144b12015-04-28 18:17:56 -07001069 if c.Properties.Sdk_version == "" {
1070 depNames.CrtBegin = "crtbegin_so"
1071 depNames.CrtEnd = "crtend_so"
1072 } else {
1073 depNames.CrtBegin = "ndk_crtbegin_so." + c.Properties.Sdk_version
1074 depNames.CrtEnd = "ndk_crtend_so." + c.Properties.Sdk_version
1075 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001076 }
Colin Cross2732e9a2015-04-28 13:23:52 -07001077 depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.LibraryProperties.Shared.Whole_static_libs...)
1078 depNames.StaticLibs = append(depNames.StaticLibs, c.LibraryProperties.Shared.Static_libs...)
1079 depNames.SharedLibs = append(depNames.SharedLibs, c.LibraryProperties.Shared.Shared_libs...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001080 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001081
Colin Cross21b9a242015-03-24 14:15:58 -07001082 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -08001083}
1084
Colin Cross97ba0732015-03-23 17:50:24 -07001085func (c *CCLibrary) outputFile() string {
Colin Cross3f40fa42015-01-30 17:27:36 -08001086 return c.out
1087}
1088
Colin Crossed4cf0b2015-03-26 14:43:45 -07001089func (c *CCLibrary) getReuseObjFiles() []string {
1090 return c.reuseObjFiles
1091}
1092
1093func (c *CCLibrary) setReuseFrom(reuseFrom ccLibraryInterface) {
1094 c.reuseFrom = reuseFrom
1095}
1096
1097func (c *CCLibrary) getReuseFrom() ccLibraryInterface {
1098 return c.reuseFrom
1099}
1100
Colin Cross97ba0732015-03-23 17:50:24 -07001101func (c *CCLibrary) allObjFiles() []string {
Colin Cross3f40fa42015-01-30 17:27:36 -08001102 return c.objFiles
1103}
1104
Colin Cross28344522015-04-22 13:07:53 -07001105func (c *CCLibrary) exportedFlags() []string {
1106 return c.exportFlags
Colin Cross3f40fa42015-01-30 17:27:36 -08001107}
1108
Colin Cross0676e2d2015-04-24 17:39:18 -07001109func (c *CCLibrary) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Crosse11befc2015-04-27 17:49:17 -07001110 flags = c.CCLinked.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001111
Colin Cross97ba0732015-03-23 17:50:24 -07001112 flags.CFlags = append(flags.CFlags, "-fPIC")
Colin Cross3f40fa42015-01-30 17:27:36 -08001113
Colin Crossd8e780d2015-04-28 17:39:43 -07001114 if c.static() {
1115 flags.CFlags = append(flags.CFlags, c.LibraryProperties.Static.Cflags...)
1116 } else {
1117 flags.CFlags = append(flags.CFlags, c.LibraryProperties.Shared.Cflags...)
1118 }
1119
Colin Cross18b6dc52015-04-28 13:20:37 -07001120 if !c.static() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001121 libName := ctx.ModuleName()
1122 // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead
1123 sharedFlag := "-Wl,-shared"
Colin Crossfa138792015-04-24 17:31:52 -07001124 if c.Properties.Clang || ctx.Host() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001125 sharedFlag = "-shared"
1126 }
Colin Crossf6566ed2015-03-24 11:13:38 -07001127 if ctx.Device() {
Colin Cross97ba0732015-03-23 17:50:24 -07001128 flags.LdFlags = append(flags.LdFlags, "-nostdlib")
Colin Cross3f40fa42015-01-30 17:27:36 -08001129 }
Colin Cross97ba0732015-03-23 17:50:24 -07001130
Colin Cross0af4b842015-04-30 16:36:18 -07001131 if ctx.Darwin() {
1132 flags.LdFlags = append(flags.LdFlags,
1133 "-dynamiclib",
1134 "-single_module",
1135 //"-read_only_relocs suppress",
1136 "-install_name @rpath/"+libName+sharedLibraryExtension,
1137 )
1138 } else {
1139 flags.LdFlags = append(flags.LdFlags,
1140 "-Wl,--gc-sections",
1141 sharedFlag,
1142 "-Wl,-soname,"+libName+sharedLibraryExtension,
1143 )
1144 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001145 }
Colin Cross97ba0732015-03-23 17:50:24 -07001146
1147 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001148}
1149
Colin Cross97ba0732015-03-23 17:50:24 -07001150func (c *CCLibrary) compileStaticLibrary(ctx common.AndroidModuleContext,
1151 flags CCFlags, deps CCDeps, objFiles []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001152
1153 staticFlags := flags
Colin Cross581c1892015-04-07 16:50:10 -07001154 objFilesStatic := c.customCompileObjs(ctx, staticFlags, common.DeviceStaticLibrary,
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001155 c.LibraryProperties.Static.Srcs, c.LibraryProperties.Static.Exclude_srcs)
Colin Cross3f40fa42015-01-30 17:27:36 -08001156
1157 objFiles = append(objFiles, objFilesStatic...)
Colin Cross21b9a242015-03-24 14:15:58 -07001158 objFiles = append(objFiles, deps.WholeStaticLibObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001159
1160 outputFile := filepath.Join(common.ModuleOutDir(ctx), ctx.ModuleName()+staticLibraryExtension)
1161
Colin Cross0af4b842015-04-30 16:36:18 -07001162 if ctx.Darwin() {
1163 TransformDarwinObjToStaticLib(ctx, objFiles, ccFlagsToBuilderFlags(flags), outputFile)
1164 } else {
1165 TransformObjToStaticLib(ctx, objFiles, ccFlagsToBuilderFlags(flags), outputFile)
1166 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001167
1168 c.objFiles = objFiles
1169 c.out = outputFile
Colin Crossf2298272015-05-12 11:36:53 -07001170
1171 common.CheckModuleSrcDirsExist(ctx, c.Properties.Export_include_dirs, "export_include_dirs")
Colin Crossfa138792015-04-24 17:31:52 -07001172 includeDirs := pathtools.PrefixPaths(c.Properties.Export_include_dirs, common.ModuleSrcDir(ctx))
Colin Cross28344522015-04-22 13:07:53 -07001173 c.exportFlags = []string{includeDirsToFlags(includeDirs)}
Colin Cross3f40fa42015-01-30 17:27:36 -08001174
1175 ctx.CheckbuildFile(outputFile)
1176}
1177
Colin Cross97ba0732015-03-23 17:50:24 -07001178func (c *CCLibrary) compileSharedLibrary(ctx common.AndroidModuleContext,
1179 flags CCFlags, deps CCDeps, objFiles []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001180
1181 sharedFlags := flags
Colin Cross581c1892015-04-07 16:50:10 -07001182 objFilesShared := c.customCompileObjs(ctx, sharedFlags, common.DeviceSharedLibrary,
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001183 c.LibraryProperties.Shared.Srcs, c.LibraryProperties.Shared.Exclude_srcs)
Colin Cross3f40fa42015-01-30 17:27:36 -08001184
1185 objFiles = append(objFiles, objFilesShared...)
1186
1187 outputFile := filepath.Join(common.ModuleOutDir(ctx), ctx.ModuleName()+sharedLibraryExtension)
1188
Colin Crossaee540a2015-07-06 17:48:31 -07001189 var linkerDeps []string
1190
1191 if c.LibraryProperties.Version_script != "" {
1192 versionScript := filepath.Join(common.ModuleSrcDir(ctx), c.LibraryProperties.Version_script)
1193 sharedFlags.LdFlags = append(sharedFlags.LdFlags, "-Wl,--version-script,"+versionScript)
1194 linkerDeps = append(linkerDeps, versionScript)
1195 }
1196
Colin Cross97ba0732015-03-23 17:50:24 -07001197 TransformObjToDynamicBinary(ctx, objFiles, deps.SharedLibs, deps.StaticLibs,
Colin Crossaee540a2015-07-06 17:48:31 -07001198 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, false,
Colin Cross77b00fa2015-03-16 16:15:49 -07001199 ccFlagsToBuilderFlags(flags), outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08001200
1201 c.out = outputFile
Colin Crossfa138792015-04-24 17:31:52 -07001202 includeDirs := pathtools.PrefixPaths(c.Properties.Export_include_dirs, common.ModuleSrcDir(ctx))
Colin Cross28344522015-04-22 13:07:53 -07001203 c.exportFlags = []string{includeDirsToFlags(includeDirs)}
Colin Cross3f40fa42015-01-30 17:27:36 -08001204}
1205
Colin Cross97ba0732015-03-23 17:50:24 -07001206func (c *CCLibrary) compileModule(ctx common.AndroidModuleContext,
1207 flags CCFlags, deps CCDeps, objFiles []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001208
1209 // Reuse the object files from the matching static library if it exists
Colin Crossed4cf0b2015-03-26 14:43:45 -07001210 if c.getReuseFrom().ccLibrary() == c {
1211 c.reuseObjFiles = objFiles
Colin Cross3f40fa42015-01-30 17:27:36 -08001212 } else {
Colin Cross2732e9a2015-04-28 13:23:52 -07001213 if c.getReuseFrom().ccLibrary().LibraryProperties.Static.Cflags == nil &&
1214 c.LibraryProperties.Shared.Cflags == nil {
1215 objFiles = append([]string(nil), c.getReuseFrom().getReuseObjFiles()...)
1216 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001217 }
1218
Colin Crossed4cf0b2015-03-26 14:43:45 -07001219 if c.static() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001220 c.compileStaticLibrary(ctx, flags, deps, objFiles)
1221 } else {
1222 c.compileSharedLibrary(ctx, flags, deps, objFiles)
1223 }
1224}
1225
Colin Cross97ba0732015-03-23 17:50:24 -07001226func (c *CCLibrary) installStaticLibrary(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001227 // Static libraries do not get installed.
1228}
1229
Colin Cross97ba0732015-03-23 17:50:24 -07001230func (c *CCLibrary) installSharedLibrary(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001231 installDir := "lib"
Colin Cross97ba0732015-03-23 17:50:24 -07001232 if flags.Toolchain.Is64Bit() {
Dan Albertc403f7c2015-03-18 14:01:18 -07001233 installDir = "lib64"
1234 }
1235
Colin Crossfa138792015-04-24 17:31:52 -07001236 ctx.InstallFile(filepath.Join(installDir, c.Properties.Relative_install_path), c.out)
Dan Albertc403f7c2015-03-18 14:01:18 -07001237}
1238
Colin Cross97ba0732015-03-23 17:50:24 -07001239func (c *CCLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001240 if c.static() {
Dan Albertc403f7c2015-03-18 14:01:18 -07001241 c.installStaticLibrary(ctx, flags)
1242 } else {
1243 c.installSharedLibrary(ctx, flags)
1244 }
1245}
1246
Colin Cross3f40fa42015-01-30 17:27:36 -08001247//
1248// Objects (for crt*.o)
1249//
1250
Dan Albertc3144b12015-04-28 18:17:56 -07001251type ccObjectProvider interface {
1252 object() *ccObject
1253}
1254
Colin Cross3f40fa42015-01-30 17:27:36 -08001255type ccObject struct {
Colin Crossfa138792015-04-24 17:31:52 -07001256 CCBase
Colin Cross3f40fa42015-01-30 17:27:36 -08001257 out string
1258}
1259
Dan Albertc3144b12015-04-28 18:17:56 -07001260func (c *ccObject) object() *ccObject {
1261 return c
1262}
1263
Colin Cross97ba0732015-03-23 17:50:24 -07001264func CCObjectFactory() (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001265 module := &ccObject{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001266
Colin Crossfa138792015-04-24 17:31:52 -07001267 return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth)
Colin Cross3f40fa42015-01-30 17:27:36 -08001268}
1269
1270func (*ccObject) AndroidDynamicDependencies(ctx common.AndroidDynamicDependerModuleContext) []string {
1271 // object files can't have any dynamic dependencies
1272 return nil
1273}
1274
Colin Cross0676e2d2015-04-24 17:39:18 -07001275func (*ccObject) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Cross21b9a242015-03-24 14:15:58 -07001276 // object files can't have any dynamic dependencies
1277 return CCDeps{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001278}
1279
1280func (c *ccObject) compileModule(ctx common.AndroidModuleContext,
Colin Cross97ba0732015-03-23 17:50:24 -07001281 flags CCFlags, deps CCDeps, objFiles []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001282
Colin Cross97ba0732015-03-23 17:50:24 -07001283 objFiles = append(objFiles, deps.ObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001284
1285 var outputFile string
1286 if len(objFiles) == 1 {
1287 outputFile = objFiles[0]
1288 } else {
Dan Albertc3144b12015-04-28 18:17:56 -07001289 outputFile = filepath.Join(common.ModuleOutDir(ctx), ctx.ModuleName()+objectExtension)
Colin Cross3f40fa42015-01-30 17:27:36 -08001290 TransformObjsToObj(ctx, objFiles, ccFlagsToBuilderFlags(flags), outputFile)
1291 }
1292
1293 c.out = outputFile
1294
1295 ctx.CheckbuildFile(outputFile)
1296}
1297
Colin Cross97ba0732015-03-23 17:50:24 -07001298func (c *ccObject) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001299 // Object files do not get installed.
1300}
1301
Colin Cross3f40fa42015-01-30 17:27:36 -08001302func (c *ccObject) outputFile() string {
1303 return c.out
1304}
1305
Dan Albertc3144b12015-04-28 18:17:56 -07001306var _ ccObjectProvider = (*ccObject)(nil)
1307
Colin Cross3f40fa42015-01-30 17:27:36 -08001308//
1309// Executables
1310//
1311
Colin Cross7d5136f2015-05-11 13:39:40 -07001312type CCBinaryProperties struct {
1313 // compile executable with -static
1314 Static_executable bool
1315
1316 // set the name of the output
1317 Stem string `android:"arch_variant"`
1318
1319 // append to the name of the output
1320 Suffix string `android:"arch_variant"`
1321
1322 // if set, add an extra objcopy --prefix-symbols= step
1323 Prefix_symbols string
1324}
1325
Colin Cross97ba0732015-03-23 17:50:24 -07001326type CCBinary struct {
Colin Crossfa138792015-04-24 17:31:52 -07001327 CCLinked
Dan Albertc403f7c2015-03-18 14:01:18 -07001328 out string
Colin Crossd350ecd2015-04-28 13:25:36 -07001329 installFile string
Colin Cross7d5136f2015-05-11 13:39:40 -07001330 BinaryProperties CCBinaryProperties
Colin Cross3f40fa42015-01-30 17:27:36 -08001331}
1332
Colin Crossed4cf0b2015-03-26 14:43:45 -07001333func (c *CCBinary) buildStatic() bool {
1334 return c.BinaryProperties.Static_executable
1335}
1336
1337func (c *CCBinary) buildShared() bool {
1338 return !c.BinaryProperties.Static_executable
1339}
1340
Colin Cross97ba0732015-03-23 17:50:24 -07001341func (c *CCBinary) getStem(ctx common.AndroidModuleContext) string {
Colin Cross4ae185c2015-03-26 15:12:10 -07001342 stem := ctx.ModuleName()
Colin Cross97ba0732015-03-23 17:50:24 -07001343 if c.BinaryProperties.Stem != "" {
Colin Cross4ae185c2015-03-26 15:12:10 -07001344 stem = c.BinaryProperties.Stem
Colin Cross3f40fa42015-01-30 17:27:36 -08001345 }
Colin Cross4ae185c2015-03-26 15:12:10 -07001346
1347 return stem + c.BinaryProperties.Suffix
Colin Cross3f40fa42015-01-30 17:27:36 -08001348}
1349
Colin Cross0676e2d2015-04-24 17:39:18 -07001350func (c *CCBinary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Crosse11befc2015-04-27 17:49:17 -07001351 depNames = c.CCLinked.depNames(ctx, depNames)
Colin Crossf6566ed2015-03-24 11:13:38 -07001352 if ctx.Device() {
Dan Albertc3144b12015-04-28 18:17:56 -07001353 if c.Properties.Sdk_version == "" {
1354 if c.BinaryProperties.Static_executable {
1355 depNames.CrtBegin = "crtbegin_static"
1356 } else {
1357 depNames.CrtBegin = "crtbegin_dynamic"
1358 }
1359 depNames.CrtEnd = "crtend_android"
Colin Cross3f40fa42015-01-30 17:27:36 -08001360 } else {
Dan Albertc3144b12015-04-28 18:17:56 -07001361 if c.BinaryProperties.Static_executable {
1362 depNames.CrtBegin = "ndk_crtbegin_static." + c.Properties.Sdk_version
1363 } else {
1364 depNames.CrtBegin = "ndk_crtbegin_dynamic." + c.Properties.Sdk_version
1365 }
1366 depNames.CrtEnd = "ndk_crtend_android." + c.Properties.Sdk_version
Colin Cross3f40fa42015-01-30 17:27:36 -08001367 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001368
1369 if c.BinaryProperties.Static_executable {
Colin Cross74d1ec02015-04-28 13:30:13 -07001370 if c.stl(ctx) == "libc++_static" {
1371 depNames.StaticLibs = append(depNames.StaticLibs, "libm", "libc", "libdl")
1372 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001373 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
1374 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
1375 // move them to the beginning of deps.LateStaticLibs
1376 var groupLibs []string
1377 depNames.StaticLibs, groupLibs = filterList(depNames.StaticLibs,
1378 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
1379 depNames.LateStaticLibs = append(groupLibs, depNames.LateStaticLibs...)
1380 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001381 }
Colin Cross21b9a242015-03-24 14:15:58 -07001382 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -08001383}
1384
Colin Cross97ba0732015-03-23 17:50:24 -07001385func NewCCBinary(binary *CCBinary, module CCModuleType,
Colin Cross1f8f2342015-03-26 16:09:47 -07001386 hod common.HostOrDeviceSupported, props ...interface{}) (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001387
Colin Cross1f8f2342015-03-26 16:09:47 -07001388 props = append(props, &binary.BinaryProperties)
1389
Colin Crossfa138792015-04-24 17:31:52 -07001390 return newCCDynamic(&binary.CCLinked, module, hod, common.MultilibFirst, props...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001391}
1392
Colin Cross97ba0732015-03-23 17:50:24 -07001393func CCBinaryFactory() (blueprint.Module, []interface{}) {
1394 module := &CCBinary{}
1395
1396 return NewCCBinary(module, module, common.HostAndDeviceSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001397}
1398
Colin Cross18b6dc52015-04-28 13:20:37 -07001399func (c *CCBinary) ModifyProperties(ctx common.AndroidBaseContext) {
Colin Cross0af4b842015-04-30 16:36:18 -07001400 if ctx.Darwin() {
1401 c.BinaryProperties.Static_executable = false
1402 }
Colin Cross18b6dc52015-04-28 13:20:37 -07001403 if c.BinaryProperties.Static_executable {
1404 c.dynamicProperties.VariantIsStaticBinary = true
1405 }
1406}
1407
Colin Cross0676e2d2015-04-24 17:39:18 -07001408func (c *CCBinary) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Crosse11befc2015-04-27 17:49:17 -07001409 flags = c.CCLinked.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001410
Colin Cross97ba0732015-03-23 17:50:24 -07001411 flags.CFlags = append(flags.CFlags, "-fpie")
1412
Colin Crossf6566ed2015-03-24 11:13:38 -07001413 if ctx.Device() {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001414 if c.BinaryProperties.Static_executable {
1415 // Clang driver needs -static to create static executable.
1416 // However, bionic/linker uses -shared to overwrite.
1417 // Linker for x86 targets does not allow coexistance of -static and -shared,
1418 // so we add -static only if -shared is not used.
1419 if !inList("-shared", flags.LdFlags) {
1420 flags.LdFlags = append(flags.LdFlags, "-static")
1421 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001422
Colin Crossed4cf0b2015-03-26 14:43:45 -07001423 flags.LdFlags = append(flags.LdFlags,
1424 "-nostdlib",
1425 "-Bstatic",
1426 "-Wl,--gc-sections",
1427 )
1428
1429 } else {
1430 linker := "/system/bin/linker"
1431 if flags.Toolchain.Is64Bit() {
1432 linker = "/system/bin/linker64"
1433 }
1434
1435 flags.LdFlags = append(flags.LdFlags,
1436 "-nostdlib",
1437 "-Bdynamic",
1438 fmt.Sprintf("-Wl,-dynamic-linker,%s", linker),
1439 "-Wl,--gc-sections",
1440 "-Wl,-z,nocopyreloc",
1441 )
1442 }
Colin Cross0af4b842015-04-30 16:36:18 -07001443 } else if ctx.Darwin() {
1444 flags.LdFlags = append(flags.LdFlags, "-Wl,-headerpad_max_install_names")
Colin Cross3f40fa42015-01-30 17:27:36 -08001445 }
1446
Colin Cross97ba0732015-03-23 17:50:24 -07001447 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001448}
1449
Colin Cross97ba0732015-03-23 17:50:24 -07001450func (c *CCBinary) compileModule(ctx common.AndroidModuleContext,
1451 flags CCFlags, deps CCDeps, objFiles []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001452
Colin Crossfa138792015-04-24 17:31:52 -07001453 if !c.BinaryProperties.Static_executable && inList("libc", c.Properties.Static_libs) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001454 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
1455 "from static libs or set static_executable: true")
1456 }
1457
1458 outputFile := filepath.Join(common.ModuleOutDir(ctx), c.getStem(ctx))
Dan Albertc403f7c2015-03-18 14:01:18 -07001459 c.out = outputFile
Colin Crossbfae8852015-03-26 14:44:11 -07001460 if c.BinaryProperties.Prefix_symbols != "" {
1461 afterPrefixSymbols := outputFile
1462 outputFile = outputFile + ".intermediate"
1463 TransformBinaryPrefixSymbols(ctx, c.BinaryProperties.Prefix_symbols, outputFile,
1464 ccFlagsToBuilderFlags(flags), afterPrefixSymbols)
1465 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001466
Colin Crossaee540a2015-07-06 17:48:31 -07001467 var linkerDeps []string
1468
Colin Cross97ba0732015-03-23 17:50:24 -07001469 TransformObjToDynamicBinary(ctx, objFiles, deps.SharedLibs, deps.StaticLibs,
Colin Crossaee540a2015-07-06 17:48:31 -07001470 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
Colin Cross77b00fa2015-03-16 16:15:49 -07001471 ccFlagsToBuilderFlags(flags), outputFile)
Dan Albertc403f7c2015-03-18 14:01:18 -07001472}
Colin Cross3f40fa42015-01-30 17:27:36 -08001473
Colin Cross97ba0732015-03-23 17:50:24 -07001474func (c *CCBinary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Colin Crossd350ecd2015-04-28 13:25:36 -07001475 c.installFile = ctx.InstallFile(filepath.Join("bin", c.Properties.Relative_install_path), c.out)
1476}
1477
1478func (c *CCBinary) HostToolPath() string {
1479 if c.HostOrDevice().Host() {
1480 return c.installFile
1481 }
1482 return ""
Dan Albertc403f7c2015-03-18 14:01:18 -07001483}
1484
Colin Cross7d5136f2015-05-11 13:39:40 -07001485type CCTestProperties struct {
1486 // Create a separate test for each source file. Useful when there is
1487 // global state that can not be torn down and reset between each test suite.
1488 Test_per_src bool
1489}
1490
Colin Cross9ffb4f52015-04-24 17:48:09 -07001491type CCTest struct {
Colin Cross97ba0732015-03-23 17:50:24 -07001492 CCBinary
Colin Cross6b290692015-03-19 14:05:33 -07001493
Colin Cross7d5136f2015-05-11 13:39:40 -07001494 TestProperties CCTestProperties
Dan Albertc403f7c2015-03-18 14:01:18 -07001495}
1496
Colin Cross9ffb4f52015-04-24 17:48:09 -07001497func (c *CCTest) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Cross0676e2d2015-04-24 17:39:18 -07001498 flags = c.CCBinary.flags(ctx, flags)
Dan Albertc403f7c2015-03-18 14:01:18 -07001499
Colin Cross97ba0732015-03-23 17:50:24 -07001500 flags.CFlags = append(flags.CFlags, "-DGTEST_HAS_STD_STRING")
Colin Crossf6566ed2015-03-24 11:13:38 -07001501 if ctx.Host() {
Colin Cross97ba0732015-03-23 17:50:24 -07001502 flags.CFlags = append(flags.CFlags, "-O0", "-g")
Colin Cross28344522015-04-22 13:07:53 -07001503 flags.LdFlags = append(flags.LdFlags, "-lpthread")
Dan Albertc403f7c2015-03-18 14:01:18 -07001504 }
1505
1506 // TODO(danalbert): Make gtest export its dependencies.
Colin Cross28344522015-04-22 13:07:53 -07001507 flags.CFlags = append(flags.CFlags,
1508 "-I"+filepath.Join(ctx.AConfig().SrcDir(), "external/gtest/include"))
Dan Albertc403f7c2015-03-18 14:01:18 -07001509
Colin Cross21b9a242015-03-24 14:15:58 -07001510 return flags
Dan Albertc403f7c2015-03-18 14:01:18 -07001511}
1512
Colin Cross9ffb4f52015-04-24 17:48:09 -07001513func (c *CCTest) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Cross21b9a242015-03-24 14:15:58 -07001514 depNames.StaticLibs = append(depNames.StaticLibs, "libgtest", "libgtest_main")
Colin Crossa8a93d32015-04-28 13:26:49 -07001515 depNames = c.CCBinary.depNames(ctx, depNames)
Colin Cross21b9a242015-03-24 14:15:58 -07001516 return depNames
Dan Albertc403f7c2015-03-18 14:01:18 -07001517}
1518
Colin Cross9ffb4f52015-04-24 17:48:09 -07001519func (c *CCTest) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Colin Crossf6566ed2015-03-24 11:13:38 -07001520 if ctx.Device() {
Colin Crossa8a93d32015-04-28 13:26:49 -07001521 ctx.InstallFile("../data/nativetest"+ctx.Arch().ArchType.Multilib[3:]+"/"+ctx.ModuleName(), c.out)
Dan Albertc403f7c2015-03-18 14:01:18 -07001522 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001523 c.CCBinary.installModule(ctx, flags)
Dan Albertc403f7c2015-03-18 14:01:18 -07001524 }
1525}
1526
Colin Cross9ffb4f52015-04-24 17:48:09 -07001527func (c *CCTest) testPerSrc() bool {
1528 return c.TestProperties.Test_per_src
Colin Cross6b290692015-03-19 14:05:33 -07001529}
1530
Colin Cross9ffb4f52015-04-24 17:48:09 -07001531func (c *CCTest) test() *CCTest {
1532 return c
1533}
1534
1535func NewCCTest(test *CCTest, module CCModuleType,
1536 hod common.HostOrDeviceSupported, props ...interface{}) (blueprint.Module, []interface{}) {
1537
1538 props = append(props, &test.TestProperties)
1539
1540 return NewCCBinary(&test.CCBinary, module, hod, props...)
1541}
1542
1543func CCTestFactory() (blueprint.Module, []interface{}) {
1544 module := &CCTest{}
1545
1546 return NewCCTest(module, module, common.HostAndDeviceSupported)
1547}
1548
1549type testPerSrc interface {
1550 test() *CCTest
1551 testPerSrc() bool
1552}
1553
1554var _ testPerSrc = (*CCTest)(nil)
1555
Colin Cross6b290692015-03-19 14:05:33 -07001556func TestPerSrcMutator(mctx blueprint.EarlyMutatorContext) {
Colin Cross9ffb4f52015-04-24 17:48:09 -07001557 if test, ok := mctx.Module().(testPerSrc); ok {
1558 if test.testPerSrc() {
1559 testNames := make([]string, len(test.test().Properties.Srcs))
1560 for i, src := range test.test().Properties.Srcs {
Colin Cross6b290692015-03-19 14:05:33 -07001561 testNames[i] = strings.TrimSuffix(src, filepath.Ext(src))
1562 }
1563 tests := mctx.CreateLocalVariations(testNames...)
Colin Cross9ffb4f52015-04-24 17:48:09 -07001564 for i, src := range test.test().Properties.Srcs {
1565 tests[i].(testPerSrc).test().Properties.Srcs = []string{src}
1566 tests[i].(testPerSrc).test().BinaryProperties.Stem = testNames[i]
Colin Cross6b290692015-03-19 14:05:33 -07001567 }
1568 }
1569 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001570}
1571
Colin Cross2ba19d92015-05-07 15:44:20 -07001572type CCBenchmark struct {
1573 CCBinary
1574}
1575
1576func (c *CCBenchmark) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
1577 depNames = c.CCBinary.depNames(ctx, depNames)
Dan Willemsenf8e98b02015-09-11 17:41:44 -07001578 depNames.StaticLibs = append(depNames.StaticLibs, "libbenchmark", "libbase")
Colin Cross2ba19d92015-05-07 15:44:20 -07001579 return depNames
1580}
1581
1582func (c *CCBenchmark) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
1583 if ctx.Device() {
1584 ctx.InstallFile("../data/nativetest"+ctx.Arch().ArchType.Multilib[3:]+"/"+ctx.ModuleName(), c.out)
1585 } else {
1586 c.CCBinary.installModule(ctx, flags)
1587 }
1588}
1589
1590func NewCCBenchmark(test *CCBenchmark, module CCModuleType,
1591 hod common.HostOrDeviceSupported, props ...interface{}) (blueprint.Module, []interface{}) {
1592
1593 return NewCCBinary(&test.CCBinary, module, hod, props...)
1594}
1595
1596func CCBenchmarkFactory() (blueprint.Module, []interface{}) {
1597 module := &CCBenchmark{}
1598
1599 return NewCCBenchmark(module, module, common.HostAndDeviceSupported)
1600}
1601
Colin Cross3f40fa42015-01-30 17:27:36 -08001602//
1603// Static library
1604//
1605
Colin Cross97ba0732015-03-23 17:50:24 -07001606func CCLibraryStaticFactory() (blueprint.Module, []interface{}) {
1607 module := &CCLibrary{}
1608 module.LibraryProperties.BuildStatic = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001609
Colin Cross97ba0732015-03-23 17:50:24 -07001610 return NewCCLibrary(module, module, common.HostAndDeviceSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001611}
1612
1613//
1614// Shared libraries
1615//
1616
Colin Cross97ba0732015-03-23 17:50:24 -07001617func CCLibrarySharedFactory() (blueprint.Module, []interface{}) {
1618 module := &CCLibrary{}
1619 module.LibraryProperties.BuildShared = 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// Host static library
1626//
1627
Colin Cross97ba0732015-03-23 17:50:24 -07001628func CCLibraryHostStaticFactory() (blueprint.Module, []interface{}) {
1629 module := &CCLibrary{}
1630 module.LibraryProperties.BuildStatic = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001631
Colin Cross97ba0732015-03-23 17:50:24 -07001632 return NewCCLibrary(module, module, common.HostSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001633}
1634
1635//
1636// Host Shared libraries
1637//
1638
Colin Cross97ba0732015-03-23 17:50:24 -07001639func CCLibraryHostSharedFactory() (blueprint.Module, []interface{}) {
1640 module := &CCLibrary{}
1641 module.LibraryProperties.BuildShared = 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 Binaries
1648//
1649
Colin Cross97ba0732015-03-23 17:50:24 -07001650func CCBinaryHostFactory() (blueprint.Module, []interface{}) {
1651 module := &CCBinary{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001652
Colin Cross97ba0732015-03-23 17:50:24 -07001653 return NewCCBinary(module, module, common.HostSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001654}
1655
1656//
Colin Cross1f8f2342015-03-26 16:09:47 -07001657// Host Tests
1658//
1659
1660func CCTestHostFactory() (blueprint.Module, []interface{}) {
Colin Cross9ffb4f52015-04-24 17:48:09 -07001661 module := &CCTest{}
Colin Cross1f8f2342015-03-26 16:09:47 -07001662 return NewCCBinary(&module.CCBinary, module, common.HostSupported,
Colin Cross9ffb4f52015-04-24 17:48:09 -07001663 &module.TestProperties)
Colin Cross1f8f2342015-03-26 16:09:47 -07001664}
1665
1666//
Colin Cross2ba19d92015-05-07 15:44:20 -07001667// Host Benchmarks
1668//
1669
1670func CCBenchmarkHostFactory() (blueprint.Module, []interface{}) {
1671 module := &CCBenchmark{}
1672 return NewCCBinary(&module.CCBinary, module, common.HostSupported)
1673}
1674
1675//
Colin Cross3f40fa42015-01-30 17:27:36 -08001676// Device libraries shipped with gcc
1677//
1678
1679type toolchainLibrary struct {
Colin Cross97ba0732015-03-23 17:50:24 -07001680 CCLibrary
Colin Cross3f40fa42015-01-30 17:27:36 -08001681}
1682
1683func (*toolchainLibrary) AndroidDynamicDependencies(ctx common.AndroidDynamicDependerModuleContext) []string {
1684 // toolchain libraries can't have any dependencies
1685 return nil
1686}
1687
Colin Cross0676e2d2015-04-24 17:39:18 -07001688func (*toolchainLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Cross3f40fa42015-01-30 17:27:36 -08001689 // toolchain libraries can't have any dependencies
Colin Cross21b9a242015-03-24 14:15:58 -07001690 return CCDeps{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001691}
1692
Colin Cross97ba0732015-03-23 17:50:24 -07001693func ToolchainLibraryFactory() (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001694 module := &toolchainLibrary{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001695
Colin Cross97ba0732015-03-23 17:50:24 -07001696 module.LibraryProperties.BuildStatic = true
1697
Colin Crossfa138792015-04-24 17:31:52 -07001698 return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth,
Colin Cross21b9a242015-03-24 14:15:58 -07001699 &module.LibraryProperties)
Colin Cross3f40fa42015-01-30 17:27:36 -08001700}
1701
1702func (c *toolchainLibrary) compileModule(ctx common.AndroidModuleContext,
Colin Cross97ba0732015-03-23 17:50:24 -07001703 flags CCFlags, deps CCDeps, objFiles []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001704
1705 libName := ctx.ModuleName() + staticLibraryExtension
1706 outputFile := filepath.Join(common.ModuleOutDir(ctx), libName)
1707
1708 CopyGccLib(ctx, libName, ccFlagsToBuilderFlags(flags), outputFile)
1709
1710 c.out = outputFile
1711
1712 ctx.CheckbuildFile(outputFile)
1713}
1714
Colin Cross97ba0732015-03-23 17:50:24 -07001715func (c *toolchainLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001716 // Toolchain libraries do not get installed.
1717}
1718
Dan Albertbe961682015-03-18 23:38:50 -07001719// NDK prebuilt libraries.
1720//
1721// These differ from regular prebuilts in that they aren't stripped and usually aren't installed
1722// either (with the exception of the shared STLs, which are installed to the app's directory rather
1723// than to the system image).
1724
1725func getNdkLibDir(ctx common.AndroidModuleContext, toolchain Toolchain, version string) string {
1726 return fmt.Sprintf("%s/prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/lib",
Colin Cross1332b002015-04-07 17:11:30 -07001727 ctx.AConfig().SrcDir(), version, toolchain.Name())
Dan Albertbe961682015-03-18 23:38:50 -07001728}
1729
Dan Albertc3144b12015-04-28 18:17:56 -07001730func ndkPrebuiltModuleToPath(ctx common.AndroidModuleContext, toolchain Toolchain,
1731 ext string, version string) string {
1732
1733 // NDK prebuilts are named like: ndk_NAME.EXT.SDK_VERSION.
1734 // We want to translate to just NAME.EXT
1735 name := strings.Split(strings.TrimPrefix(ctx.ModuleName(), "ndk_"), ".")[0]
1736 dir := getNdkLibDir(ctx, toolchain, version)
1737 return filepath.Join(dir, name+ext)
1738}
1739
1740type ndkPrebuiltObject struct {
1741 ccObject
1742}
1743
1744func (*ndkPrebuiltObject) AndroidDynamicDependencies(
1745 ctx common.AndroidDynamicDependerModuleContext) []string {
1746
1747 // NDK objects can't have any dependencies
1748 return nil
1749}
1750
1751func (*ndkPrebuiltObject) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
1752 // NDK objects can't have any dependencies
1753 return CCDeps{}
1754}
1755
1756func NdkPrebuiltObjectFactory() (blueprint.Module, []interface{}) {
1757 module := &ndkPrebuiltObject{}
1758 return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth)
1759}
1760
1761func (c *ndkPrebuiltObject) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
1762 deps CCDeps, objFiles []string) {
1763 // A null build step, but it sets up the output path.
1764 if !strings.HasPrefix(ctx.ModuleName(), "ndk_crt") {
1765 ctx.ModuleErrorf("NDK prebuilts must have an ndk_crt prefixed name")
1766 }
1767
1768 c.out = ndkPrebuiltModuleToPath(ctx, flags.Toolchain, objectExtension, c.Properties.Sdk_version)
1769}
1770
1771func (c *ndkPrebuiltObject) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
1772 // Objects do not get installed.
1773}
1774
1775var _ ccObjectProvider = (*ndkPrebuiltObject)(nil)
1776
Dan Albertbe961682015-03-18 23:38:50 -07001777type ndkPrebuiltLibrary struct {
1778 CCLibrary
1779}
1780
1781func (*ndkPrebuiltLibrary) AndroidDynamicDependencies(
1782 ctx common.AndroidDynamicDependerModuleContext) []string {
1783
1784 // NDK libraries can't have any dependencies
1785 return nil
1786}
1787
Colin Cross0676e2d2015-04-24 17:39:18 -07001788func (*ndkPrebuiltLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Dan Albertbe961682015-03-18 23:38:50 -07001789 // NDK libraries can't have any dependencies
1790 return CCDeps{}
1791}
1792
1793func NdkPrebuiltLibraryFactory() (blueprint.Module, []interface{}) {
1794 module := &ndkPrebuiltLibrary{}
1795 module.LibraryProperties.BuildShared = true
1796 return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
1797}
1798
1799func (c *ndkPrebuiltLibrary) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
1800 deps CCDeps, objFiles []string) {
1801 // A null build step, but it sets up the output path.
1802 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
1803 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
1804 }
1805
Colin Crossfa138792015-04-24 17:31:52 -07001806 includeDirs := pathtools.PrefixPaths(c.Properties.Export_include_dirs, common.ModuleSrcDir(ctx))
Colin Cross28344522015-04-22 13:07:53 -07001807 c.exportFlags = []string{common.JoinWithPrefix(includeDirs, "-isystem ")}
Dan Albertbe961682015-03-18 23:38:50 -07001808
Dan Albertc3144b12015-04-28 18:17:56 -07001809 c.out = ndkPrebuiltModuleToPath(ctx, flags.Toolchain, sharedLibraryExtension,
1810 c.Properties.Sdk_version)
Dan Albertbe961682015-03-18 23:38:50 -07001811}
1812
1813func (c *ndkPrebuiltLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc3144b12015-04-28 18:17:56 -07001814 // NDK prebuilt libraries do not get installed.
Dan Albertbe961682015-03-18 23:38:50 -07001815}
1816
1817// The NDK STLs are slightly different from the prebuilt system libraries:
1818// * Are not specific to each platform version.
1819// * The libraries are not in a predictable location for each STL.
1820
1821type ndkPrebuiltStl struct {
1822 ndkPrebuiltLibrary
1823}
1824
1825type ndkPrebuiltStaticStl struct {
1826 ndkPrebuiltStl
1827}
1828
1829type ndkPrebuiltSharedStl struct {
1830 ndkPrebuiltStl
1831}
1832
1833func NdkPrebuiltSharedStlFactory() (blueprint.Module, []interface{}) {
1834 module := &ndkPrebuiltSharedStl{}
1835 module.LibraryProperties.BuildShared = true
1836 return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
1837}
1838
1839func NdkPrebuiltStaticStlFactory() (blueprint.Module, []interface{}) {
1840 module := &ndkPrebuiltStaticStl{}
1841 module.LibraryProperties.BuildStatic = true
1842 return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
1843}
1844
1845func getNdkStlLibDir(ctx common.AndroidModuleContext, toolchain Toolchain, stl string) string {
1846 gccVersion := toolchain.GccVersion()
1847 var libDir string
1848 switch stl {
1849 case "libstlport":
1850 libDir = "cxx-stl/stlport/libs"
1851 case "libc++":
1852 libDir = "cxx-stl/llvm-libc++/libs"
1853 case "libgnustl":
1854 libDir = fmt.Sprintf("cxx-stl/gnu-libstdc++/%s/libs", gccVersion)
1855 }
1856
1857 if libDir != "" {
Colin Cross1332b002015-04-07 17:11:30 -07001858 ndkSrcRoot := ctx.AConfig().SrcDir() + "/prebuilts/ndk/current/sources"
Dan Albertbe961682015-03-18 23:38:50 -07001859 return fmt.Sprintf("%s/%s/%s", ndkSrcRoot, libDir, ctx.Arch().Abi)
1860 }
1861
1862 ctx.ModuleErrorf("Unknown NDK STL: %s", stl)
1863 return ""
1864}
1865
1866func (c *ndkPrebuiltStl) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
1867 deps CCDeps, objFiles []string) {
1868 // A null build step, but it sets up the output path.
1869 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
1870 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
1871 }
1872
Colin Crossfa138792015-04-24 17:31:52 -07001873 includeDirs := pathtools.PrefixPaths(c.Properties.Export_include_dirs, common.ModuleSrcDir(ctx))
Colin Cross28344522015-04-22 13:07:53 -07001874 c.exportFlags = []string{includeDirsToFlags(includeDirs)}
Dan Albertbe961682015-03-18 23:38:50 -07001875
1876 libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_")
1877 libExt := sharedLibraryExtension
1878 if c.LibraryProperties.BuildStatic {
1879 libExt = staticLibraryExtension
1880 }
1881
1882 stlName := strings.TrimSuffix(libName, "_shared")
1883 stlName = strings.TrimSuffix(stlName, "_static")
1884 libDir := getNdkStlLibDir(ctx, flags.Toolchain, stlName)
1885 c.out = libDir + "/" + libName + libExt
1886}
1887
Colin Cross3f40fa42015-01-30 17:27:36 -08001888func LinkageMutator(mctx blueprint.EarlyMutatorContext) {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001889 if c, ok := mctx.Module().(ccLinkedInterface); ok {
Colin Cross3f40fa42015-01-30 17:27:36 -08001890 var modules []blueprint.Module
Colin Crossed4cf0b2015-03-26 14:43:45 -07001891 if c.buildStatic() && c.buildShared() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001892 modules = mctx.CreateLocalVariations("static", "shared")
Colin Cross18b6dc52015-04-28 13:20:37 -07001893 modules[0].(ccLinkedInterface).setStatic(true)
1894 modules[1].(ccLinkedInterface).setStatic(false)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001895 } else if c.buildStatic() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001896 modules = mctx.CreateLocalVariations("static")
Colin Cross18b6dc52015-04-28 13:20:37 -07001897 modules[0].(ccLinkedInterface).setStatic(true)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001898 } else if c.buildShared() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001899 modules = mctx.CreateLocalVariations("shared")
Colin Cross18b6dc52015-04-28 13:20:37 -07001900 modules[0].(ccLinkedInterface).setStatic(false)
Colin Cross3f40fa42015-01-30 17:27:36 -08001901 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001902 panic(fmt.Errorf("ccLibrary %q not static or shared", mctx.ModuleName()))
Colin Cross3f40fa42015-01-30 17:27:36 -08001903 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001904
1905 if _, ok := c.(ccLibraryInterface); ok {
1906 reuseFrom := modules[0].(ccLibraryInterface)
1907 for _, m := range modules {
1908 m.(ccLibraryInterface).setReuseFrom(reuseFrom)
Colin Cross3f40fa42015-01-30 17:27:36 -08001909 }
1910 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001911 }
1912}
Colin Cross74d1ec02015-04-28 13:30:13 -07001913
1914// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
1915// modifies the slice contents in place, and returns a subslice of the original slice
1916func lastUniqueElements(list []string) []string {
1917 totalSkip := 0
1918 for i := len(list) - 1; i >= totalSkip; i-- {
1919 skip := 0
1920 for j := i - 1; j >= totalSkip; j-- {
1921 if list[i] == list[j] {
1922 skip++
1923 } else {
1924 list[j+skip] = list[j]
1925 }
1926 }
1927 totalSkip += skip
1928 }
1929 return list[totalSkip:]
1930}