blob: ba3dd2843aea2960a45a4344013553c37bd14615 [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 {
Colin Crossb43a1592015-09-16 14:00:32 -0700321 Native_coverage bool
322 Required []string
323 Sanitize []string
324 Sanitize_recover []string
325 Strip string
326 Tags []string
Colin Crossfa138792015-04-24 17:31:52 -0700327 }
Colin Crossc472d572015-03-17 15:06:21 -0700328
329 installPath string
Colin Cross74d1ec02015-04-28 13:30:13 -0700330
331 savedDepNames CCDeps
Colin Crossc472d572015-03-17 15:06:21 -0700332}
333
Colin Crossfa138792015-04-24 17:31:52 -0700334func newCCBase(base *CCBase, module CCModuleType, hod common.HostOrDeviceSupported,
Colin Crossc472d572015-03-17 15:06:21 -0700335 multilib common.Multilib, props ...interface{}) (blueprint.Module, []interface{}) {
336
337 base.module = module
338
Colin Crossfa138792015-04-24 17:31:52 -0700339 props = append(props, &base.Properties, &base.unused)
Colin Crossc472d572015-03-17 15:06:21 -0700340
Colin Cross5049f022015-03-18 13:28:46 -0700341 return common.InitAndroidArchModule(module, hod, multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700342}
343
Colin Crossfa138792015-04-24 17:31:52 -0700344func (c *CCBase) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800345 toolchain := c.findToolchain(ctx)
346 if ctx.Failed() {
347 return
348 }
349
Colin Cross21b9a242015-03-24 14:15:58 -0700350 flags := c.collectFlags(ctx, toolchain)
Colin Cross3f40fa42015-01-30 17:27:36 -0800351 if ctx.Failed() {
352 return
353 }
354
Colin Cross74d1ec02015-04-28 13:30:13 -0700355 deps := c.depsToPaths(ctx, c.savedDepNames)
Colin Cross3f40fa42015-01-30 17:27:36 -0800356 if ctx.Failed() {
357 return
358 }
359
Colin Cross28344522015-04-22 13:07:53 -0700360 flags.CFlags = append(flags.CFlags, deps.Cflags...)
Colin Crossed9f8682015-03-18 17:17:35 -0700361
Colin Cross581c1892015-04-07 16:50:10 -0700362 objFiles := c.compileObjs(ctx, flags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800363 if ctx.Failed() {
364 return
365 }
366
Colin Cross581c1892015-04-07 16:50:10 -0700367 generatedObjFiles := c.compileGeneratedObjs(ctx, flags)
Colin Cross5049f022015-03-18 13:28:46 -0700368 if ctx.Failed() {
369 return
370 }
371
372 objFiles = append(objFiles, generatedObjFiles...)
373
Colin Cross3f40fa42015-01-30 17:27:36 -0800374 c.ccModuleType().compileModule(ctx, flags, deps, objFiles)
375 if ctx.Failed() {
376 return
377 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700378
379 c.ccModuleType().installModule(ctx, flags)
380 if ctx.Failed() {
381 return
382 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800383}
384
Colin Crossfa138792015-04-24 17:31:52 -0700385func (c *CCBase) ccModuleType() CCModuleType {
Colin Cross3f40fa42015-01-30 17:27:36 -0800386 return c.module
387}
388
Colin Crossfa138792015-04-24 17:31:52 -0700389var _ common.AndroidDynamicDepender = (*CCBase)(nil)
Colin Cross3f40fa42015-01-30 17:27:36 -0800390
Colin Crossfa138792015-04-24 17:31:52 -0700391func (c *CCBase) findToolchain(ctx common.AndroidModuleContext) Toolchain {
Colin Cross3f40fa42015-01-30 17:27:36 -0800392 arch := ctx.Arch()
Colin Crossd3ba0392015-05-07 14:11:29 -0700393 hod := ctx.HostOrDevice()
394 factory := toolchainFactories[hod][arch.ArchType]
Colin Cross3f40fa42015-01-30 17:27:36 -0800395 if factory == nil {
396 panic(fmt.Sprintf("Toolchain not found for %s arch %q",
Colin Crossd3ba0392015-05-07 14:11:29 -0700397 hod.String(), arch.String()))
Colin Cross3f40fa42015-01-30 17:27:36 -0800398 }
399 return factory(arch.ArchVariant, arch.CpuVariant)
400}
401
Colin Crossfa138792015-04-24 17:31:52 -0700402func (c *CCBase) ModifyProperties(ctx common.AndroidBaseContext) {
403}
404
Colin Crosse11befc2015-04-27 17:49:17 -0700405func (c *CCBase) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Crossfa138792015-04-24 17:31:52 -0700406 depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.Properties.Whole_static_libs...)
407 depNames.StaticLibs = append(depNames.StaticLibs, c.Properties.Static_libs...)
408 depNames.SharedLibs = append(depNames.SharedLibs, c.Properties.Shared_libs...)
Colin Cross21b9a242015-03-24 14:15:58 -0700409
Colin Cross21b9a242015-03-24 14:15:58 -0700410 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -0800411}
412
Colin Crossfa138792015-04-24 17:31:52 -0700413func (c *CCBase) AndroidDynamicDependencies(ctx common.AndroidDynamicDependerModuleContext) []string {
414 c.module.ModifyProperties(ctx)
415
Colin Cross74d1ec02015-04-28 13:30:13 -0700416 c.savedDepNames = c.module.depNames(ctx, CCDeps{})
417 c.savedDepNames.WholeStaticLibs = lastUniqueElements(c.savedDepNames.WholeStaticLibs)
418 c.savedDepNames.StaticLibs = lastUniqueElements(c.savedDepNames.StaticLibs)
419 c.savedDepNames.SharedLibs = lastUniqueElements(c.savedDepNames.SharedLibs)
420
421 staticLibs := c.savedDepNames.WholeStaticLibs
422 staticLibs = append(staticLibs, c.savedDepNames.StaticLibs...)
423 staticLibs = append(staticLibs, c.savedDepNames.LateStaticLibs...)
Colin Cross21b9a242015-03-24 14:15:58 -0700424 ctx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, staticLibs...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800425
Colin Cross74d1ec02015-04-28 13:30:13 -0700426 ctx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, c.savedDepNames.SharedLibs...)
Colin Cross21b9a242015-03-24 14:15:58 -0700427
Colin Cross74d1ec02015-04-28 13:30:13 -0700428 ret := append([]string(nil), c.savedDepNames.ObjFiles...)
429 if c.savedDepNames.CrtBegin != "" {
430 ret = append(ret, c.savedDepNames.CrtBegin)
Colin Cross21b9a242015-03-24 14:15:58 -0700431 }
Colin Cross74d1ec02015-04-28 13:30:13 -0700432 if c.savedDepNames.CrtEnd != "" {
433 ret = append(ret, c.savedDepNames.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700434 }
435
436 return ret
Colin Cross3f40fa42015-01-30 17:27:36 -0800437}
438
439// Create a ccFlags struct that collects the compile flags from global values,
440// per-target values, module type values, and per-module Blueprints properties
Colin Crossfa138792015-04-24 17:31:52 -0700441func (c *CCBase) collectFlags(ctx common.AndroidModuleContext, toolchain Toolchain) CCFlags {
Colin Cross97ba0732015-03-23 17:50:24 -0700442 flags := CCFlags{
Colin Crossfa138792015-04-24 17:31:52 -0700443 CFlags: c.Properties.Cflags,
444 CppFlags: c.Properties.Cppflags,
445 ConlyFlags: c.Properties.Conlyflags,
446 LdFlags: c.Properties.Ldflags,
447 AsFlags: c.Properties.Asflags,
448 YaccFlags: c.Properties.Yaccflags,
449 Nocrt: c.Properties.Nocrt,
Colin Cross97ba0732015-03-23 17:50:24 -0700450 Toolchain: toolchain,
Colin Crossfa138792015-04-24 17:31:52 -0700451 Clang: c.Properties.Clang,
Colin Cross3f40fa42015-01-30 17:27:36 -0800452 }
Colin Cross28344522015-04-22 13:07:53 -0700453
454 // Include dir cflags
Colin Crossf2298272015-05-12 11:36:53 -0700455 common.CheckSrcDirsExist(ctx, c.Properties.Include_dirs, "include_dirs")
456 common.CheckModuleSrcDirsExist(ctx, c.Properties.Local_include_dirs, "local_include_dirs")
457
Colin Crossfa138792015-04-24 17:31:52 -0700458 rootIncludeDirs := pathtools.PrefixPaths(c.Properties.Include_dirs, ctx.AConfig().SrcDir())
459 localIncludeDirs := pathtools.PrefixPaths(c.Properties.Local_include_dirs, common.ModuleSrcDir(ctx))
Colin Cross28344522015-04-22 13:07:53 -0700460 flags.GlobalFlags = append(flags.GlobalFlags,
461 includeDirsToFlags(rootIncludeDirs),
462 includeDirsToFlags(localIncludeDirs))
463
Colin Cross39d97f22015-09-14 12:30:50 -0700464 rootIncludeFiles := pathtools.PrefixPaths(c.Properties.Include_files, ctx.AConfig().SrcDir())
465 localIncludeFiles := pathtools.PrefixPaths(c.Properties.Local_include_files, common.ModuleSrcDir(ctx))
466
467 flags.GlobalFlags = append(flags.GlobalFlags,
468 includeFilesToFlags(rootIncludeFiles),
469 includeFilesToFlags(localIncludeFiles))
470
Colin Crossfa138792015-04-24 17:31:52 -0700471 if !c.Properties.No_default_compiler_flags {
472 if c.Properties.Sdk_version == "" || ctx.Host() {
Colin Cross28344522015-04-22 13:07:53 -0700473 flags.GlobalFlags = append(flags.GlobalFlags,
474 "${commonGlobalIncludes}",
475 toolchain.IncludeFlags(),
476 "-I${SrcDir}/libnativehelper/include/nativehelper")
477 }
478
479 flags.GlobalFlags = append(flags.GlobalFlags, []string{
480 "-I" + common.ModuleSrcDir(ctx),
481 "-I" + common.ModuleOutDir(ctx),
482 "-I" + common.ModuleGenDir(ctx),
483 }...)
484 }
485
Colin Crossfa138792015-04-24 17:31:52 -0700486 instructionSet := c.Properties.Instruction_set
Tim Kilbourn1a9bf262015-03-18 12:28:32 -0700487 instructionSetFlags, err := toolchain.InstructionSetFlags(instructionSet)
488 if err != nil {
489 ctx.ModuleErrorf("%s", err)
490 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800491
Colin Crossaf19a292015-03-18 12:07:10 -0700492 // TODO: debug
Colin Crossfa138792015-04-24 17:31:52 -0700493 flags.CFlags = append(flags.CFlags, c.Properties.Release.Cflags...)
Colin Crossaf19a292015-03-18 12:07:10 -0700494
Colin Cross28d76592015-03-26 16:14:04 -0700495 if ctx.Host() && !ctx.ContainsProperty("clang") {
Colin Cross97ba0732015-03-23 17:50:24 -0700496 flags.Clang = true
Colin Cross3f40fa42015-01-30 17:27:36 -0800497 }
498
Colin Cross97ba0732015-03-23 17:50:24 -0700499 if flags.Clang {
500 flags.CFlags = clangFilterUnknownCflags(flags.CFlags)
Colin Crossfa138792015-04-24 17:31:52 -0700501 flags.CFlags = append(flags.CFlags, c.Properties.Clang_cflags...)
502 flags.AsFlags = append(flags.AsFlags, c.Properties.Clang_asflags...)
Colin Cross97ba0732015-03-23 17:50:24 -0700503 flags.CppFlags = clangFilterUnknownCflags(flags.CppFlags)
504 flags.ConlyFlags = clangFilterUnknownCflags(flags.ConlyFlags)
505 flags.LdFlags = clangFilterUnknownCflags(flags.LdFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800506
Colin Cross97ba0732015-03-23 17:50:24 -0700507 flags.CFlags = append(flags.CFlags, "${clangExtraCflags}")
508 flags.ConlyFlags = append(flags.ConlyFlags, "${clangExtraConlyflags}")
Colin Crossf6566ed2015-03-24 11:13:38 -0700509 if ctx.Device() {
Colin Cross97ba0732015-03-23 17:50:24 -0700510 flags.CFlags = append(flags.CFlags, "${clangExtraTargetCflags}")
Colin Crossbdd7b1c2015-03-16 16:21:20 -0700511 }
512
Colin Cross3f40fa42015-01-30 17:27:36 -0800513 target := "-target " + toolchain.ClangTriple()
514 gccPrefix := "-B" + filepath.Join(toolchain.GccRoot(), toolchain.GccTriple(), "bin")
515
Colin Cross97ba0732015-03-23 17:50:24 -0700516 flags.CFlags = append(flags.CFlags, target, gccPrefix)
517 flags.AsFlags = append(flags.AsFlags, target, gccPrefix)
518 flags.LdFlags = append(flags.LdFlags, target, gccPrefix)
Colin Cross3f40fa42015-01-30 17:27:36 -0800519 }
520
Colin Crossfa138792015-04-24 17:31:52 -0700521 if !c.Properties.No_default_compiler_flags {
522 if ctx.Device() && !c.Properties.Allow_undefined_symbols {
Colin Cross97ba0732015-03-23 17:50:24 -0700523 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-undefined")
Colin Cross3f40fa42015-01-30 17:27:36 -0800524 }
525
Colin Cross56b4d452015-04-21 17:38:44 -0700526 flags.GlobalFlags = append(flags.GlobalFlags, instructionSetFlags)
527
Colin Cross97ba0732015-03-23 17:50:24 -0700528 if flags.Clang {
529 flags.CppFlags = append(flags.CppFlags, "${commonClangGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -0700530 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -0800531 toolchain.ClangCflags(),
532 "${commonClangGlobalCflags}",
Colin Crossd3ba0392015-05-07 14:11:29 -0700533 fmt.Sprintf("${%sClangGlobalCflags}", ctx.HostOrDevice()))
Colin Cross3f40fa42015-01-30 17:27:36 -0800534 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700535 flags.CppFlags = append(flags.CppFlags, "${commonGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -0700536 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -0800537 toolchain.Cflags(),
538 "${commonGlobalCflags}",
Colin Crossd3ba0392015-05-07 14:11:29 -0700539 fmt.Sprintf("${%sGlobalCflags}", ctx.HostOrDevice()))
Colin Cross3f40fa42015-01-30 17:27:36 -0800540 }
541
Colin Crossf6566ed2015-03-24 11:13:38 -0700542 if ctx.Device() {
Colin Crossfa138792015-04-24 17:31:52 -0700543 if c.Properties.Rtti {
Colin Cross97ba0732015-03-23 17:50:24 -0700544 flags.CppFlags = append(flags.CppFlags, "-frtti")
Colin Cross3f40fa42015-01-30 17:27:36 -0800545 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700546 flags.CppFlags = append(flags.CppFlags, "-fno-rtti")
Colin Cross3f40fa42015-01-30 17:27:36 -0800547 }
548 }
549
Colin Cross97ba0732015-03-23 17:50:24 -0700550 flags.AsFlags = append(flags.AsFlags, "-D__ASSEMBLY__")
Colin Cross3f40fa42015-01-30 17:27:36 -0800551
Colin Cross97ba0732015-03-23 17:50:24 -0700552 if flags.Clang {
553 flags.CppFlags = append(flags.CppFlags, toolchain.ClangCppflags())
554 flags.LdFlags = append(flags.LdFlags, toolchain.ClangLdflags())
Colin Cross3f40fa42015-01-30 17:27:36 -0800555 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700556 flags.CppFlags = append(flags.CppFlags, toolchain.Cppflags())
557 flags.LdFlags = append(flags.LdFlags, toolchain.Ldflags())
Colin Cross3f40fa42015-01-30 17:27:36 -0800558 }
Colin Cross28344522015-04-22 13:07:53 -0700559
560 if ctx.Host() {
Colin Crossfa138792015-04-24 17:31:52 -0700561 flags.LdFlags = append(flags.LdFlags, c.Properties.Host_ldlibs...)
Colin Cross28344522015-04-22 13:07:53 -0700562 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800563 }
564
Colin Cross0676e2d2015-04-24 17:39:18 -0700565 flags = c.ccModuleType().flags(ctx, flags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800566
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700567 if c.Properties.Sdk_version == "" {
568 if ctx.Host() && !flags.Clang {
569 // The host GCC doesn't support C++14 (and is deprecated, so likely
570 // never will). Build these modules with C++11.
571 flags.CppFlags = append(flags.CppFlags, "-std=gnu++11")
572 } else {
573 flags.CppFlags = append(flags.CppFlags, "-std=gnu++14")
574 }
575 }
576
577 flags.CFlags, _ = filterList(flags.CFlags, illegalFlags)
578 flags.CppFlags, _ = filterList(flags.CppFlags, illegalFlags)
579 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, illegalFlags)
580
Colin Cross3f40fa42015-01-30 17:27:36 -0800581 // Optimization to reduce size of build.ninja
582 // Replace the long list of flags for each file with a module-local variable
Colin Cross97ba0732015-03-23 17:50:24 -0700583 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
584 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
585 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
586 flags.CFlags = []string{"$cflags"}
587 flags.CppFlags = []string{"$cppflags"}
588 flags.AsFlags = []string{"$asflags"}
Colin Cross3f40fa42015-01-30 17:27:36 -0800589
590 return flags
591}
592
Colin Cross0676e2d2015-04-24 17:39:18 -0700593func (c *CCBase) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Cross3f40fa42015-01-30 17:27:36 -0800594 return flags
595}
596
597// Compile a list of source files into objects a specified subdirectory
Colin Crossfa138792015-04-24 17:31:52 -0700598func (c *CCBase) customCompileObjs(ctx common.AndroidModuleContext, flags CCFlags,
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700599 subdir string, srcFiles, excludes []string) []string {
Colin Cross581c1892015-04-07 16:50:10 -0700600
601 buildFlags := ccFlagsToBuilderFlags(flags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800602
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700603 srcFiles = ctx.ExpandSources(srcFiles, excludes)
Colin Cross581c1892015-04-07 16:50:10 -0700604 srcFiles, deps := genSources(ctx, srcFiles, buildFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800605
Colin Cross581c1892015-04-07 16:50:10 -0700606 return TransformSourceToObj(ctx, subdir, srcFiles, buildFlags, deps)
Colin Cross3f40fa42015-01-30 17:27:36 -0800607}
608
Colin Crossfa138792015-04-24 17:31:52 -0700609// Compile files listed in c.Properties.Srcs into objects
610func (c *CCBase) compileObjs(ctx common.AndroidModuleContext, flags CCFlags) []string {
Colin Cross3f40fa42015-01-30 17:27:36 -0800611
Colin Crossfa138792015-04-24 17:31:52 -0700612 if c.Properties.SkipCompileObjs {
Colin Cross3f40fa42015-01-30 17:27:36 -0800613 return nil
614 }
615
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700616 return c.customCompileObjs(ctx, flags, "", c.Properties.Srcs, c.Properties.Exclude_srcs)
Colin Cross3f40fa42015-01-30 17:27:36 -0800617}
618
Colin Cross5049f022015-03-18 13:28:46 -0700619// Compile generated source files from dependencies
Colin Crossfa138792015-04-24 17:31:52 -0700620func (c *CCBase) compileGeneratedObjs(ctx common.AndroidModuleContext, flags CCFlags) []string {
Colin Cross5049f022015-03-18 13:28:46 -0700621 var srcs []string
622
Colin Crossfa138792015-04-24 17:31:52 -0700623 if c.Properties.SkipCompileObjs {
Colin Cross5049f022015-03-18 13:28:46 -0700624 return nil
625 }
626
627 ctx.VisitDirectDeps(func(module blueprint.Module) {
628 if gen, ok := module.(genrule.SourceFileGenerator); ok {
629 srcs = append(srcs, gen.GeneratedSourceFiles()...)
630 }
631 })
632
633 if len(srcs) == 0 {
634 return nil
635 }
636
Colin Cross581c1892015-04-07 16:50:10 -0700637 return TransformSourceToObj(ctx, "", srcs, ccFlagsToBuilderFlags(flags), nil)
Colin Cross5049f022015-03-18 13:28:46 -0700638}
639
Colin Crossfa138792015-04-24 17:31:52 -0700640func (c *CCBase) outputFile() string {
Colin Cross3f40fa42015-01-30 17:27:36 -0800641 return ""
642}
643
Colin Crossfa138792015-04-24 17:31:52 -0700644func (c *CCBase) depsToPathsFromList(ctx common.AndroidModuleContext,
Colin Cross3f40fa42015-01-30 17:27:36 -0800645 names []string) (modules []common.AndroidModule,
Colin Cross28344522015-04-22 13:07:53 -0700646 outputFiles []string, exportedFlags []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800647
648 for _, n := range names {
649 found := false
650 ctx.VisitDirectDeps(func(m blueprint.Module) {
651 otherName := ctx.OtherModuleName(m)
652 if otherName != n {
653 return
654 }
655
Colin Cross97ba0732015-03-23 17:50:24 -0700656 if a, ok := m.(CCModuleType); ok {
Colin Cross3f40fa42015-01-30 17:27:36 -0800657 if a.Disabled() {
658 // If a cc_library host+device module depends on a library that exists as both
659 // cc_library_shared and cc_library_host_shared, it will end up with two
660 // dependencies with the same name, one of which is marked disabled for each
661 // of host and device. Ignore the disabled one.
662 return
663 }
Colin Crossd3ba0392015-05-07 14:11:29 -0700664 if a.HostOrDevice() != ctx.HostOrDevice() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800665 ctx.ModuleErrorf("host/device mismatch between %q and %q", ctx.ModuleName(),
666 otherName)
667 return
668 }
669
670 if outputFile := a.outputFile(); outputFile != "" {
671 if found {
672 ctx.ModuleErrorf("multiple modules satisified dependency on %q", otherName)
673 return
674 }
675 outputFiles = append(outputFiles, outputFile)
676 modules = append(modules, a)
Colin Cross28344522015-04-22 13:07:53 -0700677 if i, ok := a.(ccExportedFlagsProducer); ok {
678 exportedFlags = append(exportedFlags, i.exportedFlags()...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800679 }
680 found = true
681 } else {
682 ctx.ModuleErrorf("module %q missing output file", otherName)
683 return
684 }
685 } else {
686 ctx.ModuleErrorf("module %q not an android module", otherName)
687 return
688 }
689 })
690 if !found {
691 ctx.ModuleErrorf("unsatisified dependency on %q", n)
692 }
693 }
694
Colin Cross28344522015-04-22 13:07:53 -0700695 return modules, outputFiles, exportedFlags
Colin Cross3f40fa42015-01-30 17:27:36 -0800696}
697
Colin Cross21b9a242015-03-24 14:15:58 -0700698// Convert depenedency names to paths. Takes a CCDeps containing names and returns a CCDeps
699// containing paths
Colin Crossfa138792015-04-24 17:31:52 -0700700func (c *CCBase) depsToPaths(ctx common.AndroidModuleContext, depNames CCDeps) CCDeps {
Colin Cross21b9a242015-03-24 14:15:58 -0700701 var depPaths CCDeps
Colin Cross28344522015-04-22 13:07:53 -0700702 var newCflags []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800703
Colin Cross21b9a242015-03-24 14:15:58 -0700704 var wholeStaticLibModules []common.AndroidModule
Colin Cross3f40fa42015-01-30 17:27:36 -0800705
Colin Cross28344522015-04-22 13:07:53 -0700706 wholeStaticLibModules, depPaths.WholeStaticLibs, newCflags =
Colin Cross21b9a242015-03-24 14:15:58 -0700707 c.depsToPathsFromList(ctx, depNames.WholeStaticLibs)
Colin Cross28344522015-04-22 13:07:53 -0700708 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800709
Colin Cross21b9a242015-03-24 14:15:58 -0700710 for _, m := range wholeStaticLibModules {
711 if staticLib, ok := m.(ccLibraryInterface); ok && staticLib.static() {
712 depPaths.WholeStaticLibObjFiles =
713 append(depPaths.WholeStaticLibObjFiles, staticLib.allObjFiles()...)
714 } else {
715 ctx.ModuleErrorf("module %q not a static library", ctx.OtherModuleName(m))
716 }
717 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800718
Colin Cross28344522015-04-22 13:07:53 -0700719 _, depPaths.StaticLibs, newCflags = c.depsToPathsFromList(ctx, depNames.StaticLibs)
720 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross21b9a242015-03-24 14:15:58 -0700721
Colin Cross28344522015-04-22 13:07:53 -0700722 _, depPaths.LateStaticLibs, newCflags = c.depsToPathsFromList(ctx, depNames.LateStaticLibs)
723 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross21b9a242015-03-24 14:15:58 -0700724
Colin Cross28344522015-04-22 13:07:53 -0700725 _, depPaths.SharedLibs, newCflags = c.depsToPathsFromList(ctx, depNames.SharedLibs)
726 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross21b9a242015-03-24 14:15:58 -0700727
728 ctx.VisitDirectDeps(func(m blueprint.Module) {
Dan Albertc3144b12015-04-28 18:17:56 -0700729 if obj, ok := m.(ccObjectProvider); ok {
Colin Cross21b9a242015-03-24 14:15:58 -0700730 otherName := ctx.OtherModuleName(m)
731 if otherName == depNames.CrtBegin {
Colin Crossfa138792015-04-24 17:31:52 -0700732 if !c.Properties.Nocrt {
Dan Albertc3144b12015-04-28 18:17:56 -0700733 depPaths.CrtBegin = obj.object().outputFile()
Colin Cross21b9a242015-03-24 14:15:58 -0700734 }
735 } else if otherName == depNames.CrtEnd {
Colin Crossfa138792015-04-24 17:31:52 -0700736 if !c.Properties.Nocrt {
Dan Albertc3144b12015-04-28 18:17:56 -0700737 depPaths.CrtEnd = obj.object().outputFile()
Colin Cross21b9a242015-03-24 14:15:58 -0700738 }
739 } else {
Dan Albertc3144b12015-04-28 18:17:56 -0700740 depPaths.ObjFiles = append(depPaths.ObjFiles, obj.object().outputFile())
Colin Cross21b9a242015-03-24 14:15:58 -0700741 }
742 }
743 })
744
745 return depPaths
Colin Cross3f40fa42015-01-30 17:27:36 -0800746}
747
Colin Cross7d5136f2015-05-11 13:39:40 -0700748type ccLinkedProperties struct {
749 VariantIsShared bool `blueprint:"mutated"`
750 VariantIsStatic bool `blueprint:"mutated"`
751 VariantIsStaticBinary bool `blueprint:"mutated"`
752}
753
Colin Crossfa138792015-04-24 17:31:52 -0700754// CCLinked contains the properties and members used by libraries and executables
755type CCLinked struct {
756 CCBase
Colin Cross7d5136f2015-05-11 13:39:40 -0700757 dynamicProperties ccLinkedProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800758}
759
Colin Crossfa138792015-04-24 17:31:52 -0700760func newCCDynamic(dynamic *CCLinked, module CCModuleType, hod common.HostOrDeviceSupported,
Colin Crossc472d572015-03-17 15:06:21 -0700761 multilib common.Multilib, props ...interface{}) (blueprint.Module, []interface{}) {
762
Colin Crossed4cf0b2015-03-26 14:43:45 -0700763 props = append(props, &dynamic.dynamicProperties)
764
Colin Crossfa138792015-04-24 17:31:52 -0700765 return newCCBase(&dynamic.CCBase, module, hod, multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700766}
767
Colin Crossfa138792015-04-24 17:31:52 -0700768func (c *CCLinked) systemSharedLibs(ctx common.AndroidBaseContext) []string {
Colin Cross28d76592015-03-26 16:14:04 -0700769 if ctx.ContainsProperty("system_shared_libs") {
Colin Crossfa138792015-04-24 17:31:52 -0700770 return c.Properties.System_shared_libs
771 } else if ctx.Device() && c.Properties.Sdk_version == "" {
Colin Cross577f6e42015-03-27 18:23:34 -0700772 return []string{"libc", "libm"}
Colin Cross28d76592015-03-26 16:14:04 -0700773 } else {
Colin Cross577f6e42015-03-27 18:23:34 -0700774 return nil
Colin Cross3f40fa42015-01-30 17:27:36 -0800775 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800776}
777
Colin Crossfa138792015-04-24 17:31:52 -0700778func (c *CCLinked) stl(ctx common.AndroidBaseContext) string {
779 if c.Properties.Sdk_version != "" && ctx.Device() {
780 switch c.Properties.Stl {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700781 case "":
782 return "ndk_system"
783 case "c++_shared", "c++_static",
784 "stlport_shared", "stlport_static",
785 "gnustl_static":
Colin Crossfa138792015-04-24 17:31:52 -0700786 return "ndk_lib" + c.Properties.Stl
Colin Crossed4cf0b2015-03-26 14:43:45 -0700787 default:
Colin Crossfa138792015-04-24 17:31:52 -0700788 ctx.ModuleErrorf("stl: %q is not a supported STL with sdk_version set", c.Properties.Stl)
Colin Crossed4cf0b2015-03-26 14:43:45 -0700789 return ""
790 }
791 }
792
Colin Crossfa138792015-04-24 17:31:52 -0700793 switch c.Properties.Stl {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700794 case "libc++", "libc++_static",
Colin Crossed4cf0b2015-03-26 14:43:45 -0700795 "libstdc++":
Colin Crossfa138792015-04-24 17:31:52 -0700796 return c.Properties.Stl
Colin Crossed4cf0b2015-03-26 14:43:45 -0700797 case "none":
798 return ""
799 case "":
Colin Cross18b6dc52015-04-28 13:20:37 -0700800 if c.static() {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700801 return "libc++_static"
Colin Cross18b6dc52015-04-28 13:20:37 -0700802 } else {
803 return "libc++" // TODO: mingw needs libstdc++
Colin Crossed4cf0b2015-03-26 14:43:45 -0700804 }
805 default:
Colin Crossfa138792015-04-24 17:31:52 -0700806 ctx.ModuleErrorf("stl: %q is not a supported STL", c.Properties.Stl)
Colin Crossed4cf0b2015-03-26 14:43:45 -0700807 return ""
808 }
809}
810
Colin Cross0af4b842015-04-30 16:36:18 -0700811var hostDynamicGccLibs, hostStaticGccLibs []string
812
813func init() {
814 if runtime.GOOS == "darwin" {
815 hostDynamicGccLibs = []string{"-lc", "-lSystem"}
816 hostStaticGccLibs = []string{"NO_STATIC_HOST_BINARIES_ON_DARWIN"}
817 } else {
818 hostDynamicGccLibs = []string{"-lgcc_s", "-lgcc", "-lc", "-lgcc_s", "-lgcc"}
819 hostStaticGccLibs = []string{"-Wl,--start-group", "-lgcc", "-lgcc_eh", "-lc", "-Wl,--end-group"}
820 }
821}
Colin Cross712fc022015-04-27 11:13:34 -0700822
Colin Crosse11befc2015-04-27 17:49:17 -0700823func (c *CCLinked) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700824 stl := c.stl(ctx)
825 if ctx.Failed() {
826 return flags
827 }
828
829 switch stl {
830 case "libc++", "libc++_static":
831 flags.CFlags = append(flags.CFlags, "-D_USING_LIBCXX")
Colin Cross28344522015-04-22 13:07:53 -0700832 flags.CFlags = append(flags.CFlags, "-I${SrcDir}/external/libcxx/include")
Colin Crossed4cf0b2015-03-26 14:43:45 -0700833 if ctx.Host() {
834 flags.CppFlags = append(flags.CppFlags, "-nostdinc++")
835 flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs")
Colin Cross712fc022015-04-27 11:13:34 -0700836 flags.LdFlags = append(flags.LdFlags, "-lm", "-lpthread")
Colin Cross18b6dc52015-04-28 13:20:37 -0700837 if c.staticBinary() {
Colin Cross712fc022015-04-27 11:13:34 -0700838 flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs...)
Colin Cross18b6dc52015-04-28 13:20:37 -0700839 } else {
840 flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs...)
Colin Cross712fc022015-04-27 11:13:34 -0700841 }
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700842 } else {
843 if ctx.Arch().ArchType == common.Arm {
844 flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,libunwind_llvm.a")
845 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700846 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700847 case "libstdc++":
848 // Using bionic's basic libstdc++. Not actually an STL. Only around until the
849 // tree is in good enough shape to not need it.
850 // Host builds will use GNU libstdc++.
851 if ctx.Device() {
Colin Cross28344522015-04-22 13:07:53 -0700852 flags.CFlags = append(flags.CFlags, "-I${SrcDir}/bionic/libstdc++/include")
Colin Crossed4cf0b2015-03-26 14:43:45 -0700853 }
854 case "ndk_system":
Colin Cross1332b002015-04-07 17:11:30 -0700855 ndkSrcRoot := ctx.AConfig().SrcDir() + "/prebuilts/ndk/current/sources/"
Colin Cross28344522015-04-22 13:07:53 -0700856 flags.CFlags = append(flags.CFlags, "-isystem "+ndkSrcRoot+"cxx-stl/system/include")
Colin Crossed4cf0b2015-03-26 14:43:45 -0700857 case "ndk_libc++_shared", "ndk_libc++_static":
858 // TODO(danalbert): This really shouldn't be here...
859 flags.CppFlags = append(flags.CppFlags, "-std=c++11")
860 case "ndk_libstlport_shared", "ndk_libstlport_static", "ndk_libgnustl_static":
861 // Nothing
862 case "":
863 // None or error.
864 if ctx.Host() {
865 flags.CppFlags = append(flags.CppFlags, "-nostdinc++")
866 flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs")
Colin Cross18b6dc52015-04-28 13:20:37 -0700867 if c.staticBinary() {
Colin Cross712fc022015-04-27 11:13:34 -0700868 flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs...)
Colin Cross18b6dc52015-04-28 13:20:37 -0700869 } else {
870 flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs...)
Colin Cross712fc022015-04-27 11:13:34 -0700871 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700872 }
873 default:
Colin Crossfa138792015-04-24 17:31:52 -0700874 panic(fmt.Errorf("Unknown stl in CCLinked.Flags: %q", stl))
Colin Crossed4cf0b2015-03-26 14:43:45 -0700875 }
876
877 return flags
878}
879
Colin Crosse11befc2015-04-27 17:49:17 -0700880func (c *CCLinked) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
881 depNames = c.CCBase.depNames(ctx, depNames)
Colin Cross3f40fa42015-01-30 17:27:36 -0800882
Colin Crossed4cf0b2015-03-26 14:43:45 -0700883 stl := c.stl(ctx)
884 if ctx.Failed() {
885 return depNames
886 }
887
888 switch stl {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700889 case "libstdc++":
890 if ctx.Device() {
891 depNames.SharedLibs = append(depNames.SharedLibs, stl)
892 }
Colin Cross74d1ec02015-04-28 13:30:13 -0700893 case "libc++", "libc++_static":
894 if stl == "libc++" {
895 depNames.SharedLibs = append(depNames.SharedLibs, stl)
896 } else {
897 depNames.StaticLibs = append(depNames.StaticLibs, stl)
898 }
899 if ctx.Device() {
900 if ctx.Arch().ArchType == common.Arm {
901 depNames.StaticLibs = append(depNames.StaticLibs, "libunwind_llvm")
902 }
903 if c.staticBinary() {
904 depNames.StaticLibs = append(depNames.StaticLibs, "libdl")
905 } else {
906 depNames.SharedLibs = append(depNames.SharedLibs, "libdl")
907 }
908 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700909 case "":
910 // None or error.
911 case "ndk_system":
912 // TODO: Make a system STL prebuilt for the NDK.
913 // 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 -0700914 // its own includes. The includes are handled in CCBase.Flags().
Colin Cross577f6e42015-03-27 18:23:34 -0700915 depNames.SharedLibs = append([]string{"libstdc++"}, depNames.SharedLibs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -0700916 case "ndk_libc++_shared", "ndk_libstlport_shared":
917 depNames.SharedLibs = append(depNames.SharedLibs, stl)
918 case "ndk_libc++_static", "ndk_libstlport_static", "ndk_libgnustl_static":
919 depNames.StaticLibs = append(depNames.StaticLibs, stl)
920 default:
Colin Crosse11befc2015-04-27 17:49:17 -0700921 panic(fmt.Errorf("Unknown stl in CCLinked.depNames: %q", stl))
Colin Crossed4cf0b2015-03-26 14:43:45 -0700922 }
923
Colin Cross74d1ec02015-04-28 13:30:13 -0700924 if ctx.ModuleName() != "libcompiler_rt-extras" {
925 depNames.StaticLibs = append(depNames.StaticLibs, "libcompiler_rt-extras")
926 }
927
Colin Crossf6566ed2015-03-24 11:13:38 -0700928 if ctx.Device() {
Colin Cross77b00fa2015-03-16 16:15:49 -0700929 // libgcc and libatomic have to be last on the command line
Colin Cross21b9a242015-03-24 14:15:58 -0700930 depNames.LateStaticLibs = append(depNames.LateStaticLibs, "libgcov", "libatomic", "libgcc")
Colin Crossed4cf0b2015-03-26 14:43:45 -0700931
Colin Cross18b6dc52015-04-28 13:20:37 -0700932 if !c.static() {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700933 depNames.SharedLibs = append(depNames.SharedLibs, c.systemSharedLibs(ctx)...)
934 }
Colin Cross577f6e42015-03-27 18:23:34 -0700935
Colin Crossfa138792015-04-24 17:31:52 -0700936 if c.Properties.Sdk_version != "" {
937 version := c.Properties.Sdk_version
Colin Cross577f6e42015-03-27 18:23:34 -0700938 depNames.SharedLibs = append(depNames.SharedLibs,
939 "ndk_libc."+version,
940 "ndk_libm."+version,
941 )
942 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800943 }
944
Colin Cross21b9a242015-03-24 14:15:58 -0700945 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -0800946}
947
Colin Crossed4cf0b2015-03-26 14:43:45 -0700948// ccLinkedInterface interface is used on ccLinked to deal with static or shared variants
949type ccLinkedInterface interface {
950 // Returns true if the build options for the module have selected a static or shared build
951 buildStatic() bool
952 buildShared() bool
953
954 // Sets whether a specific variant is static or shared
Colin Cross18b6dc52015-04-28 13:20:37 -0700955 setStatic(bool)
Colin Crossed4cf0b2015-03-26 14:43:45 -0700956
Colin Cross18b6dc52015-04-28 13:20:37 -0700957 // Returns whether a specific variant is a static library or binary
Colin Crossed4cf0b2015-03-26 14:43:45 -0700958 static() bool
Colin Cross18b6dc52015-04-28 13:20:37 -0700959
960 // Returns whether a module is a static binary
961 staticBinary() bool
Colin Crossed4cf0b2015-03-26 14:43:45 -0700962}
963
964var _ ccLinkedInterface = (*CCLibrary)(nil)
965var _ ccLinkedInterface = (*CCBinary)(nil)
966
Colin Crossfa138792015-04-24 17:31:52 -0700967func (c *CCLinked) static() bool {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700968 return c.dynamicProperties.VariantIsStatic
969}
970
Colin Cross18b6dc52015-04-28 13:20:37 -0700971func (c *CCLinked) staticBinary() bool {
972 return c.dynamicProperties.VariantIsStaticBinary
Colin Crossed4cf0b2015-03-26 14:43:45 -0700973}
974
Colin Cross18b6dc52015-04-28 13:20:37 -0700975func (c *CCLinked) setStatic(static bool) {
976 c.dynamicProperties.VariantIsStatic = static
Colin Crossed4cf0b2015-03-26 14:43:45 -0700977}
978
Colin Cross28344522015-04-22 13:07:53 -0700979type ccExportedFlagsProducer interface {
980 exportedFlags() []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800981}
982
983//
984// Combined static+shared libraries
985//
986
Colin Cross7d5136f2015-05-11 13:39:40 -0700987type CCLibraryProperties struct {
988 BuildStatic bool `blueprint:"mutated"`
989 BuildShared bool `blueprint:"mutated"`
990 Static struct {
991 Srcs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700992 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700993 Cflags []string `android:"arch_variant"`
994 Whole_static_libs []string `android:"arch_variant"`
995 Static_libs []string `android:"arch_variant"`
996 Shared_libs []string `android:"arch_variant"`
997 } `android:"arch_variant"`
998 Shared struct {
999 Srcs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001000 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -07001001 Cflags []string `android:"arch_variant"`
1002 Whole_static_libs []string `android:"arch_variant"`
1003 Static_libs []string `android:"arch_variant"`
1004 Shared_libs []string `android:"arch_variant"`
1005 } `android:"arch_variant"`
Colin Crossaee540a2015-07-06 17:48:31 -07001006
1007 // local file name to pass to the linker as --version_script
1008 Version_script string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -07001009}
1010
Colin Cross97ba0732015-03-23 17:50:24 -07001011type CCLibrary struct {
Colin Crossfa138792015-04-24 17:31:52 -07001012 CCLinked
Colin Cross3f40fa42015-01-30 17:27:36 -08001013
Colin Cross28344522015-04-22 13:07:53 -07001014 reuseFrom ccLibraryInterface
1015 reuseObjFiles []string
1016 objFiles []string
1017 exportFlags []string
1018 out string
Colin Cross3f40fa42015-01-30 17:27:36 -08001019
Colin Cross7d5136f2015-05-11 13:39:40 -07001020 LibraryProperties CCLibraryProperties
Colin Cross3f40fa42015-01-30 17:27:36 -08001021}
1022
Colin Crossed4cf0b2015-03-26 14:43:45 -07001023func (c *CCLibrary) buildStatic() bool {
1024 return c.LibraryProperties.BuildStatic
1025}
1026
1027func (c *CCLibrary) buildShared() bool {
1028 return c.LibraryProperties.BuildShared
1029}
1030
Colin Cross97ba0732015-03-23 17:50:24 -07001031type ccLibraryInterface interface {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001032 ccLinkedInterface
Colin Cross97ba0732015-03-23 17:50:24 -07001033 ccLibrary() *CCLibrary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001034 setReuseFrom(ccLibraryInterface)
1035 getReuseFrom() ccLibraryInterface
1036 getReuseObjFiles() []string
Colin Cross97ba0732015-03-23 17:50:24 -07001037 allObjFiles() []string
Colin Crossc472d572015-03-17 15:06:21 -07001038}
1039
Colin Crossed4cf0b2015-03-26 14:43:45 -07001040var _ ccLibraryInterface = (*CCLibrary)(nil)
1041
Colin Cross97ba0732015-03-23 17:50:24 -07001042func (c *CCLibrary) ccLibrary() *CCLibrary {
1043 return c
Colin Cross3f40fa42015-01-30 17:27:36 -08001044}
1045
Colin Cross97ba0732015-03-23 17:50:24 -07001046func NewCCLibrary(library *CCLibrary, module CCModuleType,
1047 hod common.HostOrDeviceSupported) (blueprint.Module, []interface{}) {
1048
Colin Crossfa138792015-04-24 17:31:52 -07001049 return newCCDynamic(&library.CCLinked, module, hod, common.MultilibBoth,
Colin Cross97ba0732015-03-23 17:50:24 -07001050 &library.LibraryProperties)
1051}
1052
1053func CCLibraryFactory() (blueprint.Module, []interface{}) {
1054 module := &CCLibrary{}
1055
1056 module.LibraryProperties.BuildShared = true
1057 module.LibraryProperties.BuildStatic = true
1058
1059 return NewCCLibrary(module, module, common.HostAndDeviceSupported)
1060}
1061
Colin Cross0676e2d2015-04-24 17:39:18 -07001062func (c *CCLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Crosse11befc2015-04-27 17:49:17 -07001063 depNames = c.CCLinked.depNames(ctx, depNames)
Colin Cross2732e9a2015-04-28 13:23:52 -07001064 if c.static() {
1065 depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.LibraryProperties.Static.Whole_static_libs...)
1066 depNames.StaticLibs = append(depNames.StaticLibs, c.LibraryProperties.Static.Static_libs...)
1067 depNames.SharedLibs = append(depNames.SharedLibs, c.LibraryProperties.Static.Shared_libs...)
1068 } else {
Colin Crossf6566ed2015-03-24 11:13:38 -07001069 if ctx.Device() {
Dan Albertc3144b12015-04-28 18:17:56 -07001070 if c.Properties.Sdk_version == "" {
1071 depNames.CrtBegin = "crtbegin_so"
1072 depNames.CrtEnd = "crtend_so"
1073 } else {
1074 depNames.CrtBegin = "ndk_crtbegin_so." + c.Properties.Sdk_version
1075 depNames.CrtEnd = "ndk_crtend_so." + c.Properties.Sdk_version
1076 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001077 }
Colin Cross2732e9a2015-04-28 13:23:52 -07001078 depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.LibraryProperties.Shared.Whole_static_libs...)
1079 depNames.StaticLibs = append(depNames.StaticLibs, c.LibraryProperties.Shared.Static_libs...)
1080 depNames.SharedLibs = append(depNames.SharedLibs, c.LibraryProperties.Shared.Shared_libs...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001081 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001082
Colin Cross21b9a242015-03-24 14:15:58 -07001083 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -08001084}
1085
Colin Cross97ba0732015-03-23 17:50:24 -07001086func (c *CCLibrary) outputFile() string {
Colin Cross3f40fa42015-01-30 17:27:36 -08001087 return c.out
1088}
1089
Colin Crossed4cf0b2015-03-26 14:43:45 -07001090func (c *CCLibrary) getReuseObjFiles() []string {
1091 return c.reuseObjFiles
1092}
1093
1094func (c *CCLibrary) setReuseFrom(reuseFrom ccLibraryInterface) {
1095 c.reuseFrom = reuseFrom
1096}
1097
1098func (c *CCLibrary) getReuseFrom() ccLibraryInterface {
1099 return c.reuseFrom
1100}
1101
Colin Cross97ba0732015-03-23 17:50:24 -07001102func (c *CCLibrary) allObjFiles() []string {
Colin Cross3f40fa42015-01-30 17:27:36 -08001103 return c.objFiles
1104}
1105
Colin Cross28344522015-04-22 13:07:53 -07001106func (c *CCLibrary) exportedFlags() []string {
1107 return c.exportFlags
Colin Cross3f40fa42015-01-30 17:27:36 -08001108}
1109
Colin Cross0676e2d2015-04-24 17:39:18 -07001110func (c *CCLibrary) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Crosse11befc2015-04-27 17:49:17 -07001111 flags = c.CCLinked.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001112
Colin Cross97ba0732015-03-23 17:50:24 -07001113 flags.CFlags = append(flags.CFlags, "-fPIC")
Colin Cross3f40fa42015-01-30 17:27:36 -08001114
Colin Crossd8e780d2015-04-28 17:39:43 -07001115 if c.static() {
1116 flags.CFlags = append(flags.CFlags, c.LibraryProperties.Static.Cflags...)
1117 } else {
1118 flags.CFlags = append(flags.CFlags, c.LibraryProperties.Shared.Cflags...)
1119 }
1120
Colin Cross18b6dc52015-04-28 13:20:37 -07001121 if !c.static() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001122 libName := ctx.ModuleName()
1123 // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead
1124 sharedFlag := "-Wl,-shared"
Colin Crossfa138792015-04-24 17:31:52 -07001125 if c.Properties.Clang || ctx.Host() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001126 sharedFlag = "-shared"
1127 }
Colin Crossf6566ed2015-03-24 11:13:38 -07001128 if ctx.Device() {
Colin Cross97ba0732015-03-23 17:50:24 -07001129 flags.LdFlags = append(flags.LdFlags, "-nostdlib")
Colin Cross3f40fa42015-01-30 17:27:36 -08001130 }
Colin Cross97ba0732015-03-23 17:50:24 -07001131
Colin Cross0af4b842015-04-30 16:36:18 -07001132 if ctx.Darwin() {
1133 flags.LdFlags = append(flags.LdFlags,
1134 "-dynamiclib",
1135 "-single_module",
1136 //"-read_only_relocs suppress",
1137 "-install_name @rpath/"+libName+sharedLibraryExtension,
1138 )
1139 } else {
1140 flags.LdFlags = append(flags.LdFlags,
1141 "-Wl,--gc-sections",
1142 sharedFlag,
1143 "-Wl,-soname,"+libName+sharedLibraryExtension,
1144 )
1145 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001146 }
Colin Cross97ba0732015-03-23 17:50:24 -07001147
1148 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001149}
1150
Colin Cross97ba0732015-03-23 17:50:24 -07001151func (c *CCLibrary) compileStaticLibrary(ctx common.AndroidModuleContext,
1152 flags CCFlags, deps CCDeps, objFiles []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001153
1154 staticFlags := flags
Colin Cross581c1892015-04-07 16:50:10 -07001155 objFilesStatic := c.customCompileObjs(ctx, staticFlags, common.DeviceStaticLibrary,
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001156 c.LibraryProperties.Static.Srcs, c.LibraryProperties.Static.Exclude_srcs)
Colin Cross3f40fa42015-01-30 17:27:36 -08001157
1158 objFiles = append(objFiles, objFilesStatic...)
Colin Cross21b9a242015-03-24 14:15:58 -07001159 objFiles = append(objFiles, deps.WholeStaticLibObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001160
1161 outputFile := filepath.Join(common.ModuleOutDir(ctx), ctx.ModuleName()+staticLibraryExtension)
1162
Colin Cross0af4b842015-04-30 16:36:18 -07001163 if ctx.Darwin() {
1164 TransformDarwinObjToStaticLib(ctx, objFiles, ccFlagsToBuilderFlags(flags), outputFile)
1165 } else {
1166 TransformObjToStaticLib(ctx, objFiles, ccFlagsToBuilderFlags(flags), outputFile)
1167 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001168
1169 c.objFiles = objFiles
1170 c.out = outputFile
Colin Crossf2298272015-05-12 11:36:53 -07001171
1172 common.CheckModuleSrcDirsExist(ctx, c.Properties.Export_include_dirs, "export_include_dirs")
Colin Crossfa138792015-04-24 17:31:52 -07001173 includeDirs := pathtools.PrefixPaths(c.Properties.Export_include_dirs, common.ModuleSrcDir(ctx))
Colin Cross28344522015-04-22 13:07:53 -07001174 c.exportFlags = []string{includeDirsToFlags(includeDirs)}
Colin Cross3f40fa42015-01-30 17:27:36 -08001175
1176 ctx.CheckbuildFile(outputFile)
1177}
1178
Colin Cross97ba0732015-03-23 17:50:24 -07001179func (c *CCLibrary) compileSharedLibrary(ctx common.AndroidModuleContext,
1180 flags CCFlags, deps CCDeps, objFiles []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001181
1182 sharedFlags := flags
Colin Cross581c1892015-04-07 16:50:10 -07001183 objFilesShared := c.customCompileObjs(ctx, sharedFlags, common.DeviceSharedLibrary,
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001184 c.LibraryProperties.Shared.Srcs, c.LibraryProperties.Shared.Exclude_srcs)
Colin Cross3f40fa42015-01-30 17:27:36 -08001185
1186 objFiles = append(objFiles, objFilesShared...)
1187
1188 outputFile := filepath.Join(common.ModuleOutDir(ctx), ctx.ModuleName()+sharedLibraryExtension)
1189
Colin Crossaee540a2015-07-06 17:48:31 -07001190 var linkerDeps []string
1191
1192 if c.LibraryProperties.Version_script != "" {
1193 versionScript := filepath.Join(common.ModuleSrcDir(ctx), c.LibraryProperties.Version_script)
1194 sharedFlags.LdFlags = append(sharedFlags.LdFlags, "-Wl,--version-script,"+versionScript)
1195 linkerDeps = append(linkerDeps, versionScript)
1196 }
1197
Colin Cross97ba0732015-03-23 17:50:24 -07001198 TransformObjToDynamicBinary(ctx, objFiles, deps.SharedLibs, deps.StaticLibs,
Colin Crossaee540a2015-07-06 17:48:31 -07001199 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, false,
Colin Cross77b00fa2015-03-16 16:15:49 -07001200 ccFlagsToBuilderFlags(flags), outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08001201
1202 c.out = outputFile
Colin Crossfa138792015-04-24 17:31:52 -07001203 includeDirs := pathtools.PrefixPaths(c.Properties.Export_include_dirs, common.ModuleSrcDir(ctx))
Colin Cross28344522015-04-22 13:07:53 -07001204 c.exportFlags = []string{includeDirsToFlags(includeDirs)}
Colin Cross3f40fa42015-01-30 17:27:36 -08001205}
1206
Colin Cross97ba0732015-03-23 17:50:24 -07001207func (c *CCLibrary) compileModule(ctx common.AndroidModuleContext,
1208 flags CCFlags, deps CCDeps, objFiles []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001209
1210 // Reuse the object files from the matching static library if it exists
Colin Crossed4cf0b2015-03-26 14:43:45 -07001211 if c.getReuseFrom().ccLibrary() == c {
1212 c.reuseObjFiles = objFiles
Colin Cross3f40fa42015-01-30 17:27:36 -08001213 } else {
Colin Cross2732e9a2015-04-28 13:23:52 -07001214 if c.getReuseFrom().ccLibrary().LibraryProperties.Static.Cflags == nil &&
1215 c.LibraryProperties.Shared.Cflags == nil {
1216 objFiles = append([]string(nil), c.getReuseFrom().getReuseObjFiles()...)
1217 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001218 }
1219
Colin Crossed4cf0b2015-03-26 14:43:45 -07001220 if c.static() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001221 c.compileStaticLibrary(ctx, flags, deps, objFiles)
1222 } else {
1223 c.compileSharedLibrary(ctx, flags, deps, objFiles)
1224 }
1225}
1226
Colin Cross97ba0732015-03-23 17:50:24 -07001227func (c *CCLibrary) installStaticLibrary(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001228 // Static libraries do not get installed.
1229}
1230
Colin Cross97ba0732015-03-23 17:50:24 -07001231func (c *CCLibrary) installSharedLibrary(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001232 installDir := "lib"
Colin Cross97ba0732015-03-23 17:50:24 -07001233 if flags.Toolchain.Is64Bit() {
Dan Albertc403f7c2015-03-18 14:01:18 -07001234 installDir = "lib64"
1235 }
1236
Colin Crossfa138792015-04-24 17:31:52 -07001237 ctx.InstallFile(filepath.Join(installDir, c.Properties.Relative_install_path), c.out)
Dan Albertc403f7c2015-03-18 14:01:18 -07001238}
1239
Colin Cross97ba0732015-03-23 17:50:24 -07001240func (c *CCLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001241 if c.static() {
Dan Albertc403f7c2015-03-18 14:01:18 -07001242 c.installStaticLibrary(ctx, flags)
1243 } else {
1244 c.installSharedLibrary(ctx, flags)
1245 }
1246}
1247
Colin Cross3f40fa42015-01-30 17:27:36 -08001248//
1249// Objects (for crt*.o)
1250//
1251
Dan Albertc3144b12015-04-28 18:17:56 -07001252type ccObjectProvider interface {
1253 object() *ccObject
1254}
1255
Colin Cross3f40fa42015-01-30 17:27:36 -08001256type ccObject struct {
Colin Crossfa138792015-04-24 17:31:52 -07001257 CCBase
Colin Cross3f40fa42015-01-30 17:27:36 -08001258 out string
1259}
1260
Dan Albertc3144b12015-04-28 18:17:56 -07001261func (c *ccObject) object() *ccObject {
1262 return c
1263}
1264
Colin Cross97ba0732015-03-23 17:50:24 -07001265func CCObjectFactory() (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001266 module := &ccObject{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001267
Colin Crossfa138792015-04-24 17:31:52 -07001268 return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth)
Colin Cross3f40fa42015-01-30 17:27:36 -08001269}
1270
1271func (*ccObject) AndroidDynamicDependencies(ctx common.AndroidDynamicDependerModuleContext) []string {
1272 // object files can't have any dynamic dependencies
1273 return nil
1274}
1275
Colin Cross0676e2d2015-04-24 17:39:18 -07001276func (*ccObject) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Cross21b9a242015-03-24 14:15:58 -07001277 // object files can't have any dynamic dependencies
1278 return CCDeps{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001279}
1280
1281func (c *ccObject) compileModule(ctx common.AndroidModuleContext,
Colin Cross97ba0732015-03-23 17:50:24 -07001282 flags CCFlags, deps CCDeps, objFiles []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001283
Colin Cross97ba0732015-03-23 17:50:24 -07001284 objFiles = append(objFiles, deps.ObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001285
1286 var outputFile string
1287 if len(objFiles) == 1 {
1288 outputFile = objFiles[0]
1289 } else {
Dan Albertc3144b12015-04-28 18:17:56 -07001290 outputFile = filepath.Join(common.ModuleOutDir(ctx), ctx.ModuleName()+objectExtension)
Colin Cross3f40fa42015-01-30 17:27:36 -08001291 TransformObjsToObj(ctx, objFiles, ccFlagsToBuilderFlags(flags), outputFile)
1292 }
1293
1294 c.out = outputFile
1295
1296 ctx.CheckbuildFile(outputFile)
1297}
1298
Colin Cross97ba0732015-03-23 17:50:24 -07001299func (c *ccObject) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001300 // Object files do not get installed.
1301}
1302
Colin Cross3f40fa42015-01-30 17:27:36 -08001303func (c *ccObject) outputFile() string {
1304 return c.out
1305}
1306
Dan Albertc3144b12015-04-28 18:17:56 -07001307var _ ccObjectProvider = (*ccObject)(nil)
1308
Colin Cross3f40fa42015-01-30 17:27:36 -08001309//
1310// Executables
1311//
1312
Colin Cross7d5136f2015-05-11 13:39:40 -07001313type CCBinaryProperties struct {
1314 // compile executable with -static
1315 Static_executable bool
1316
1317 // set the name of the output
1318 Stem string `android:"arch_variant"`
1319
1320 // append to the name of the output
1321 Suffix string `android:"arch_variant"`
1322
1323 // if set, add an extra objcopy --prefix-symbols= step
1324 Prefix_symbols string
1325}
1326
Colin Cross97ba0732015-03-23 17:50:24 -07001327type CCBinary struct {
Colin Crossfa138792015-04-24 17:31:52 -07001328 CCLinked
Dan Albertc403f7c2015-03-18 14:01:18 -07001329 out string
Colin Crossd350ecd2015-04-28 13:25:36 -07001330 installFile string
Colin Cross7d5136f2015-05-11 13:39:40 -07001331 BinaryProperties CCBinaryProperties
Colin Cross3f40fa42015-01-30 17:27:36 -08001332}
1333
Colin Crossed4cf0b2015-03-26 14:43:45 -07001334func (c *CCBinary) buildStatic() bool {
1335 return c.BinaryProperties.Static_executable
1336}
1337
1338func (c *CCBinary) buildShared() bool {
1339 return !c.BinaryProperties.Static_executable
1340}
1341
Colin Cross97ba0732015-03-23 17:50:24 -07001342func (c *CCBinary) getStem(ctx common.AndroidModuleContext) string {
Colin Cross4ae185c2015-03-26 15:12:10 -07001343 stem := ctx.ModuleName()
Colin Cross97ba0732015-03-23 17:50:24 -07001344 if c.BinaryProperties.Stem != "" {
Colin Cross4ae185c2015-03-26 15:12:10 -07001345 stem = c.BinaryProperties.Stem
Colin Cross3f40fa42015-01-30 17:27:36 -08001346 }
Colin Cross4ae185c2015-03-26 15:12:10 -07001347
1348 return stem + c.BinaryProperties.Suffix
Colin Cross3f40fa42015-01-30 17:27:36 -08001349}
1350
Colin Cross0676e2d2015-04-24 17:39:18 -07001351func (c *CCBinary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Crosse11befc2015-04-27 17:49:17 -07001352 depNames = c.CCLinked.depNames(ctx, depNames)
Colin Crossf6566ed2015-03-24 11:13:38 -07001353 if ctx.Device() {
Dan Albertc3144b12015-04-28 18:17:56 -07001354 if c.Properties.Sdk_version == "" {
1355 if c.BinaryProperties.Static_executable {
1356 depNames.CrtBegin = "crtbegin_static"
1357 } else {
1358 depNames.CrtBegin = "crtbegin_dynamic"
1359 }
1360 depNames.CrtEnd = "crtend_android"
Colin Cross3f40fa42015-01-30 17:27:36 -08001361 } else {
Dan Albertc3144b12015-04-28 18:17:56 -07001362 if c.BinaryProperties.Static_executable {
1363 depNames.CrtBegin = "ndk_crtbegin_static." + c.Properties.Sdk_version
1364 } else {
1365 depNames.CrtBegin = "ndk_crtbegin_dynamic." + c.Properties.Sdk_version
1366 }
1367 depNames.CrtEnd = "ndk_crtend_android." + c.Properties.Sdk_version
Colin Cross3f40fa42015-01-30 17:27:36 -08001368 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001369
1370 if c.BinaryProperties.Static_executable {
Colin Cross74d1ec02015-04-28 13:30:13 -07001371 if c.stl(ctx) == "libc++_static" {
1372 depNames.StaticLibs = append(depNames.StaticLibs, "libm", "libc", "libdl")
1373 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001374 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
1375 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
1376 // move them to the beginning of deps.LateStaticLibs
1377 var groupLibs []string
1378 depNames.StaticLibs, groupLibs = filterList(depNames.StaticLibs,
1379 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
1380 depNames.LateStaticLibs = append(groupLibs, depNames.LateStaticLibs...)
1381 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001382 }
Colin Cross21b9a242015-03-24 14:15:58 -07001383 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -08001384}
1385
Colin Cross97ba0732015-03-23 17:50:24 -07001386func NewCCBinary(binary *CCBinary, module CCModuleType,
Colin Cross1f8f2342015-03-26 16:09:47 -07001387 hod common.HostOrDeviceSupported, props ...interface{}) (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001388
Colin Cross1f8f2342015-03-26 16:09:47 -07001389 props = append(props, &binary.BinaryProperties)
1390
Colin Crossfa138792015-04-24 17:31:52 -07001391 return newCCDynamic(&binary.CCLinked, module, hod, common.MultilibFirst, props...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001392}
1393
Colin Cross97ba0732015-03-23 17:50:24 -07001394func CCBinaryFactory() (blueprint.Module, []interface{}) {
1395 module := &CCBinary{}
1396
1397 return NewCCBinary(module, module, common.HostAndDeviceSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001398}
1399
Colin Cross18b6dc52015-04-28 13:20:37 -07001400func (c *CCBinary) ModifyProperties(ctx common.AndroidBaseContext) {
Colin Cross0af4b842015-04-30 16:36:18 -07001401 if ctx.Darwin() {
1402 c.BinaryProperties.Static_executable = false
1403 }
Colin Cross18b6dc52015-04-28 13:20:37 -07001404 if c.BinaryProperties.Static_executable {
1405 c.dynamicProperties.VariantIsStaticBinary = true
1406 }
1407}
1408
Colin Cross0676e2d2015-04-24 17:39:18 -07001409func (c *CCBinary) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Crosse11befc2015-04-27 17:49:17 -07001410 flags = c.CCLinked.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001411
Colin Cross97ba0732015-03-23 17:50:24 -07001412 flags.CFlags = append(flags.CFlags, "-fpie")
1413
Colin Crossf6566ed2015-03-24 11:13:38 -07001414 if ctx.Device() {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001415 if c.BinaryProperties.Static_executable {
1416 // Clang driver needs -static to create static executable.
1417 // However, bionic/linker uses -shared to overwrite.
1418 // Linker for x86 targets does not allow coexistance of -static and -shared,
1419 // so we add -static only if -shared is not used.
1420 if !inList("-shared", flags.LdFlags) {
1421 flags.LdFlags = append(flags.LdFlags, "-static")
1422 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001423
Colin Crossed4cf0b2015-03-26 14:43:45 -07001424 flags.LdFlags = append(flags.LdFlags,
1425 "-nostdlib",
1426 "-Bstatic",
1427 "-Wl,--gc-sections",
1428 )
1429
1430 } else {
1431 linker := "/system/bin/linker"
1432 if flags.Toolchain.Is64Bit() {
1433 linker = "/system/bin/linker64"
1434 }
1435
1436 flags.LdFlags = append(flags.LdFlags,
1437 "-nostdlib",
1438 "-Bdynamic",
1439 fmt.Sprintf("-Wl,-dynamic-linker,%s", linker),
1440 "-Wl,--gc-sections",
1441 "-Wl,-z,nocopyreloc",
1442 )
1443 }
Colin Cross0af4b842015-04-30 16:36:18 -07001444 } else if ctx.Darwin() {
1445 flags.LdFlags = append(flags.LdFlags, "-Wl,-headerpad_max_install_names")
Colin Cross3f40fa42015-01-30 17:27:36 -08001446 }
1447
Colin Cross97ba0732015-03-23 17:50:24 -07001448 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001449}
1450
Colin Cross97ba0732015-03-23 17:50:24 -07001451func (c *CCBinary) compileModule(ctx common.AndroidModuleContext,
1452 flags CCFlags, deps CCDeps, objFiles []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001453
Colin Crossfa138792015-04-24 17:31:52 -07001454 if !c.BinaryProperties.Static_executable && inList("libc", c.Properties.Static_libs) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001455 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
1456 "from static libs or set static_executable: true")
1457 }
1458
1459 outputFile := filepath.Join(common.ModuleOutDir(ctx), c.getStem(ctx))
Dan Albertc403f7c2015-03-18 14:01:18 -07001460 c.out = outputFile
Colin Crossbfae8852015-03-26 14:44:11 -07001461 if c.BinaryProperties.Prefix_symbols != "" {
1462 afterPrefixSymbols := outputFile
1463 outputFile = outputFile + ".intermediate"
1464 TransformBinaryPrefixSymbols(ctx, c.BinaryProperties.Prefix_symbols, outputFile,
1465 ccFlagsToBuilderFlags(flags), afterPrefixSymbols)
1466 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001467
Colin Crossaee540a2015-07-06 17:48:31 -07001468 var linkerDeps []string
1469
Colin Cross97ba0732015-03-23 17:50:24 -07001470 TransformObjToDynamicBinary(ctx, objFiles, deps.SharedLibs, deps.StaticLibs,
Colin Crossaee540a2015-07-06 17:48:31 -07001471 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
Colin Cross77b00fa2015-03-16 16:15:49 -07001472 ccFlagsToBuilderFlags(flags), outputFile)
Dan Albertc403f7c2015-03-18 14:01:18 -07001473}
Colin Cross3f40fa42015-01-30 17:27:36 -08001474
Colin Cross97ba0732015-03-23 17:50:24 -07001475func (c *CCBinary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Colin Crossd350ecd2015-04-28 13:25:36 -07001476 c.installFile = ctx.InstallFile(filepath.Join("bin", c.Properties.Relative_install_path), c.out)
1477}
1478
1479func (c *CCBinary) HostToolPath() string {
1480 if c.HostOrDevice().Host() {
1481 return c.installFile
1482 }
1483 return ""
Dan Albertc403f7c2015-03-18 14:01:18 -07001484}
1485
Colin Cross7d5136f2015-05-11 13:39:40 -07001486type CCTestProperties struct {
1487 // Create a separate test for each source file. Useful when there is
1488 // global state that can not be torn down and reset between each test suite.
1489 Test_per_src bool
1490}
1491
Colin Cross9ffb4f52015-04-24 17:48:09 -07001492type CCTest struct {
Colin Cross97ba0732015-03-23 17:50:24 -07001493 CCBinary
Colin Cross6b290692015-03-19 14:05:33 -07001494
Colin Cross7d5136f2015-05-11 13:39:40 -07001495 TestProperties CCTestProperties
Dan Albertc403f7c2015-03-18 14:01:18 -07001496}
1497
Colin Cross9ffb4f52015-04-24 17:48:09 -07001498func (c *CCTest) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Cross0676e2d2015-04-24 17:39:18 -07001499 flags = c.CCBinary.flags(ctx, flags)
Dan Albertc403f7c2015-03-18 14:01:18 -07001500
Colin Cross97ba0732015-03-23 17:50:24 -07001501 flags.CFlags = append(flags.CFlags, "-DGTEST_HAS_STD_STRING")
Colin Crossf6566ed2015-03-24 11:13:38 -07001502 if ctx.Host() {
Colin Cross97ba0732015-03-23 17:50:24 -07001503 flags.CFlags = append(flags.CFlags, "-O0", "-g")
Colin Cross28344522015-04-22 13:07:53 -07001504 flags.LdFlags = append(flags.LdFlags, "-lpthread")
Dan Albertc403f7c2015-03-18 14:01:18 -07001505 }
1506
1507 // TODO(danalbert): Make gtest export its dependencies.
Colin Cross28344522015-04-22 13:07:53 -07001508 flags.CFlags = append(flags.CFlags,
1509 "-I"+filepath.Join(ctx.AConfig().SrcDir(), "external/gtest/include"))
Dan Albertc403f7c2015-03-18 14:01:18 -07001510
Colin Cross21b9a242015-03-24 14:15:58 -07001511 return flags
Dan Albertc403f7c2015-03-18 14:01:18 -07001512}
1513
Colin Cross9ffb4f52015-04-24 17:48:09 -07001514func (c *CCTest) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Cross21b9a242015-03-24 14:15:58 -07001515 depNames.StaticLibs = append(depNames.StaticLibs, "libgtest", "libgtest_main")
Colin Crossa8a93d32015-04-28 13:26:49 -07001516 depNames = c.CCBinary.depNames(ctx, depNames)
Colin Cross21b9a242015-03-24 14:15:58 -07001517 return depNames
Dan Albertc403f7c2015-03-18 14:01:18 -07001518}
1519
Colin Cross9ffb4f52015-04-24 17:48:09 -07001520func (c *CCTest) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Colin Crossf6566ed2015-03-24 11:13:38 -07001521 if ctx.Device() {
Colin Crossa8a93d32015-04-28 13:26:49 -07001522 ctx.InstallFile("../data/nativetest"+ctx.Arch().ArchType.Multilib[3:]+"/"+ctx.ModuleName(), c.out)
Dan Albertc403f7c2015-03-18 14:01:18 -07001523 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001524 c.CCBinary.installModule(ctx, flags)
Dan Albertc403f7c2015-03-18 14:01:18 -07001525 }
1526}
1527
Colin Cross9ffb4f52015-04-24 17:48:09 -07001528func (c *CCTest) testPerSrc() bool {
1529 return c.TestProperties.Test_per_src
Colin Cross6b290692015-03-19 14:05:33 -07001530}
1531
Colin Cross9ffb4f52015-04-24 17:48:09 -07001532func (c *CCTest) test() *CCTest {
1533 return c
1534}
1535
1536func NewCCTest(test *CCTest, module CCModuleType,
1537 hod common.HostOrDeviceSupported, props ...interface{}) (blueprint.Module, []interface{}) {
1538
1539 props = append(props, &test.TestProperties)
1540
1541 return NewCCBinary(&test.CCBinary, module, hod, props...)
1542}
1543
1544func CCTestFactory() (blueprint.Module, []interface{}) {
1545 module := &CCTest{}
1546
1547 return NewCCTest(module, module, common.HostAndDeviceSupported)
1548}
1549
1550type testPerSrc interface {
1551 test() *CCTest
1552 testPerSrc() bool
1553}
1554
1555var _ testPerSrc = (*CCTest)(nil)
1556
Colin Cross6b290692015-03-19 14:05:33 -07001557func TestPerSrcMutator(mctx blueprint.EarlyMutatorContext) {
Colin Cross9ffb4f52015-04-24 17:48:09 -07001558 if test, ok := mctx.Module().(testPerSrc); ok {
1559 if test.testPerSrc() {
1560 testNames := make([]string, len(test.test().Properties.Srcs))
1561 for i, src := range test.test().Properties.Srcs {
Colin Cross6b290692015-03-19 14:05:33 -07001562 testNames[i] = strings.TrimSuffix(src, filepath.Ext(src))
1563 }
1564 tests := mctx.CreateLocalVariations(testNames...)
Colin Cross9ffb4f52015-04-24 17:48:09 -07001565 for i, src := range test.test().Properties.Srcs {
1566 tests[i].(testPerSrc).test().Properties.Srcs = []string{src}
1567 tests[i].(testPerSrc).test().BinaryProperties.Stem = testNames[i]
Colin Cross6b290692015-03-19 14:05:33 -07001568 }
1569 }
1570 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001571}
1572
Colin Cross2ba19d92015-05-07 15:44:20 -07001573type CCBenchmark struct {
1574 CCBinary
1575}
1576
1577func (c *CCBenchmark) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
1578 depNames = c.CCBinary.depNames(ctx, depNames)
Dan Willemsenf8e98b02015-09-11 17:41:44 -07001579 depNames.StaticLibs = append(depNames.StaticLibs, "libbenchmark", "libbase")
Colin Cross2ba19d92015-05-07 15:44:20 -07001580 return depNames
1581}
1582
1583func (c *CCBenchmark) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
1584 if ctx.Device() {
1585 ctx.InstallFile("../data/nativetest"+ctx.Arch().ArchType.Multilib[3:]+"/"+ctx.ModuleName(), c.out)
1586 } else {
1587 c.CCBinary.installModule(ctx, flags)
1588 }
1589}
1590
1591func NewCCBenchmark(test *CCBenchmark, module CCModuleType,
1592 hod common.HostOrDeviceSupported, props ...interface{}) (blueprint.Module, []interface{}) {
1593
1594 return NewCCBinary(&test.CCBinary, module, hod, props...)
1595}
1596
1597func CCBenchmarkFactory() (blueprint.Module, []interface{}) {
1598 module := &CCBenchmark{}
1599
1600 return NewCCBenchmark(module, module, common.HostAndDeviceSupported)
1601}
1602
Colin Cross3f40fa42015-01-30 17:27:36 -08001603//
1604// Static library
1605//
1606
Colin Cross97ba0732015-03-23 17:50:24 -07001607func CCLibraryStaticFactory() (blueprint.Module, []interface{}) {
1608 module := &CCLibrary{}
1609 module.LibraryProperties.BuildStatic = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001610
Colin Cross97ba0732015-03-23 17:50:24 -07001611 return NewCCLibrary(module, module, common.HostAndDeviceSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001612}
1613
1614//
1615// Shared libraries
1616//
1617
Colin Cross97ba0732015-03-23 17:50:24 -07001618func CCLibrarySharedFactory() (blueprint.Module, []interface{}) {
1619 module := &CCLibrary{}
1620 module.LibraryProperties.BuildShared = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001621
Colin Cross97ba0732015-03-23 17:50:24 -07001622 return NewCCLibrary(module, module, common.HostAndDeviceSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001623}
1624
1625//
1626// Host static library
1627//
1628
Colin Cross97ba0732015-03-23 17:50:24 -07001629func CCLibraryHostStaticFactory() (blueprint.Module, []interface{}) {
1630 module := &CCLibrary{}
1631 module.LibraryProperties.BuildStatic = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001632
Colin Cross97ba0732015-03-23 17:50:24 -07001633 return NewCCLibrary(module, module, common.HostSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001634}
1635
1636//
1637// Host Shared libraries
1638//
1639
Colin Cross97ba0732015-03-23 17:50:24 -07001640func CCLibraryHostSharedFactory() (blueprint.Module, []interface{}) {
1641 module := &CCLibrary{}
1642 module.LibraryProperties.BuildShared = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001643
Colin Cross97ba0732015-03-23 17:50:24 -07001644 return NewCCLibrary(module, module, common.HostSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001645}
1646
1647//
1648// Host Binaries
1649//
1650
Colin Cross97ba0732015-03-23 17:50:24 -07001651func CCBinaryHostFactory() (blueprint.Module, []interface{}) {
1652 module := &CCBinary{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001653
Colin Cross97ba0732015-03-23 17:50:24 -07001654 return NewCCBinary(module, module, common.HostSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001655}
1656
1657//
Colin Cross1f8f2342015-03-26 16:09:47 -07001658// Host Tests
1659//
1660
1661func CCTestHostFactory() (blueprint.Module, []interface{}) {
Colin Cross9ffb4f52015-04-24 17:48:09 -07001662 module := &CCTest{}
Colin Cross1f8f2342015-03-26 16:09:47 -07001663 return NewCCBinary(&module.CCBinary, module, common.HostSupported,
Colin Cross9ffb4f52015-04-24 17:48:09 -07001664 &module.TestProperties)
Colin Cross1f8f2342015-03-26 16:09:47 -07001665}
1666
1667//
Colin Cross2ba19d92015-05-07 15:44:20 -07001668// Host Benchmarks
1669//
1670
1671func CCBenchmarkHostFactory() (blueprint.Module, []interface{}) {
1672 module := &CCBenchmark{}
1673 return NewCCBinary(&module.CCBinary, module, common.HostSupported)
1674}
1675
1676//
Colin Cross3f40fa42015-01-30 17:27:36 -08001677// Device libraries shipped with gcc
1678//
1679
1680type toolchainLibrary struct {
Colin Cross97ba0732015-03-23 17:50:24 -07001681 CCLibrary
Colin Cross3f40fa42015-01-30 17:27:36 -08001682}
1683
1684func (*toolchainLibrary) AndroidDynamicDependencies(ctx common.AndroidDynamicDependerModuleContext) []string {
1685 // toolchain libraries can't have any dependencies
1686 return nil
1687}
1688
Colin Cross0676e2d2015-04-24 17:39:18 -07001689func (*toolchainLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Cross3f40fa42015-01-30 17:27:36 -08001690 // toolchain libraries can't have any dependencies
Colin Cross21b9a242015-03-24 14:15:58 -07001691 return CCDeps{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001692}
1693
Colin Cross97ba0732015-03-23 17:50:24 -07001694func ToolchainLibraryFactory() (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001695 module := &toolchainLibrary{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001696
Colin Cross97ba0732015-03-23 17:50:24 -07001697 module.LibraryProperties.BuildStatic = true
1698
Colin Crossfa138792015-04-24 17:31:52 -07001699 return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth,
Colin Cross21b9a242015-03-24 14:15:58 -07001700 &module.LibraryProperties)
Colin Cross3f40fa42015-01-30 17:27:36 -08001701}
1702
1703func (c *toolchainLibrary) compileModule(ctx common.AndroidModuleContext,
Colin Cross97ba0732015-03-23 17:50:24 -07001704 flags CCFlags, deps CCDeps, objFiles []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001705
1706 libName := ctx.ModuleName() + staticLibraryExtension
1707 outputFile := filepath.Join(common.ModuleOutDir(ctx), libName)
1708
1709 CopyGccLib(ctx, libName, ccFlagsToBuilderFlags(flags), outputFile)
1710
1711 c.out = outputFile
1712
1713 ctx.CheckbuildFile(outputFile)
1714}
1715
Colin Cross97ba0732015-03-23 17:50:24 -07001716func (c *toolchainLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001717 // Toolchain libraries do not get installed.
1718}
1719
Dan Albertbe961682015-03-18 23:38:50 -07001720// NDK prebuilt libraries.
1721//
1722// These differ from regular prebuilts in that they aren't stripped and usually aren't installed
1723// either (with the exception of the shared STLs, which are installed to the app's directory rather
1724// than to the system image).
1725
1726func getNdkLibDir(ctx common.AndroidModuleContext, toolchain Toolchain, version string) string {
1727 return fmt.Sprintf("%s/prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/lib",
Colin Cross1332b002015-04-07 17:11:30 -07001728 ctx.AConfig().SrcDir(), version, toolchain.Name())
Dan Albertbe961682015-03-18 23:38:50 -07001729}
1730
Dan Albertc3144b12015-04-28 18:17:56 -07001731func ndkPrebuiltModuleToPath(ctx common.AndroidModuleContext, toolchain Toolchain,
1732 ext string, version string) string {
1733
1734 // NDK prebuilts are named like: ndk_NAME.EXT.SDK_VERSION.
1735 // We want to translate to just NAME.EXT
1736 name := strings.Split(strings.TrimPrefix(ctx.ModuleName(), "ndk_"), ".")[0]
1737 dir := getNdkLibDir(ctx, toolchain, version)
1738 return filepath.Join(dir, name+ext)
1739}
1740
1741type ndkPrebuiltObject struct {
1742 ccObject
1743}
1744
1745func (*ndkPrebuiltObject) AndroidDynamicDependencies(
1746 ctx common.AndroidDynamicDependerModuleContext) []string {
1747
1748 // NDK objects can't have any dependencies
1749 return nil
1750}
1751
1752func (*ndkPrebuiltObject) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
1753 // NDK objects can't have any dependencies
1754 return CCDeps{}
1755}
1756
1757func NdkPrebuiltObjectFactory() (blueprint.Module, []interface{}) {
1758 module := &ndkPrebuiltObject{}
1759 return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth)
1760}
1761
1762func (c *ndkPrebuiltObject) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
1763 deps CCDeps, objFiles []string) {
1764 // A null build step, but it sets up the output path.
1765 if !strings.HasPrefix(ctx.ModuleName(), "ndk_crt") {
1766 ctx.ModuleErrorf("NDK prebuilts must have an ndk_crt prefixed name")
1767 }
1768
1769 c.out = ndkPrebuiltModuleToPath(ctx, flags.Toolchain, objectExtension, c.Properties.Sdk_version)
1770}
1771
1772func (c *ndkPrebuiltObject) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
1773 // Objects do not get installed.
1774}
1775
1776var _ ccObjectProvider = (*ndkPrebuiltObject)(nil)
1777
Dan Albertbe961682015-03-18 23:38:50 -07001778type ndkPrebuiltLibrary struct {
1779 CCLibrary
1780}
1781
1782func (*ndkPrebuiltLibrary) AndroidDynamicDependencies(
1783 ctx common.AndroidDynamicDependerModuleContext) []string {
1784
1785 // NDK libraries can't have any dependencies
1786 return nil
1787}
1788
Colin Cross0676e2d2015-04-24 17:39:18 -07001789func (*ndkPrebuiltLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Dan Albertbe961682015-03-18 23:38:50 -07001790 // NDK libraries can't have any dependencies
1791 return CCDeps{}
1792}
1793
1794func NdkPrebuiltLibraryFactory() (blueprint.Module, []interface{}) {
1795 module := &ndkPrebuiltLibrary{}
1796 module.LibraryProperties.BuildShared = true
1797 return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
1798}
1799
1800func (c *ndkPrebuiltLibrary) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
1801 deps CCDeps, objFiles []string) {
1802 // A null build step, but it sets up the output path.
1803 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
1804 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
1805 }
1806
Colin Crossfa138792015-04-24 17:31:52 -07001807 includeDirs := pathtools.PrefixPaths(c.Properties.Export_include_dirs, common.ModuleSrcDir(ctx))
Colin Cross28344522015-04-22 13:07:53 -07001808 c.exportFlags = []string{common.JoinWithPrefix(includeDirs, "-isystem ")}
Dan Albertbe961682015-03-18 23:38:50 -07001809
Dan Albertc3144b12015-04-28 18:17:56 -07001810 c.out = ndkPrebuiltModuleToPath(ctx, flags.Toolchain, sharedLibraryExtension,
1811 c.Properties.Sdk_version)
Dan Albertbe961682015-03-18 23:38:50 -07001812}
1813
1814func (c *ndkPrebuiltLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc3144b12015-04-28 18:17:56 -07001815 // NDK prebuilt libraries do not get installed.
Dan Albertbe961682015-03-18 23:38:50 -07001816}
1817
1818// The NDK STLs are slightly different from the prebuilt system libraries:
1819// * Are not specific to each platform version.
1820// * The libraries are not in a predictable location for each STL.
1821
1822type ndkPrebuiltStl struct {
1823 ndkPrebuiltLibrary
1824}
1825
1826type ndkPrebuiltStaticStl struct {
1827 ndkPrebuiltStl
1828}
1829
1830type ndkPrebuiltSharedStl struct {
1831 ndkPrebuiltStl
1832}
1833
1834func NdkPrebuiltSharedStlFactory() (blueprint.Module, []interface{}) {
1835 module := &ndkPrebuiltSharedStl{}
1836 module.LibraryProperties.BuildShared = true
1837 return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
1838}
1839
1840func NdkPrebuiltStaticStlFactory() (blueprint.Module, []interface{}) {
1841 module := &ndkPrebuiltStaticStl{}
1842 module.LibraryProperties.BuildStatic = true
1843 return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
1844}
1845
1846func getNdkStlLibDir(ctx common.AndroidModuleContext, toolchain Toolchain, stl string) string {
1847 gccVersion := toolchain.GccVersion()
1848 var libDir string
1849 switch stl {
1850 case "libstlport":
1851 libDir = "cxx-stl/stlport/libs"
1852 case "libc++":
1853 libDir = "cxx-stl/llvm-libc++/libs"
1854 case "libgnustl":
1855 libDir = fmt.Sprintf("cxx-stl/gnu-libstdc++/%s/libs", gccVersion)
1856 }
1857
1858 if libDir != "" {
Colin Cross1332b002015-04-07 17:11:30 -07001859 ndkSrcRoot := ctx.AConfig().SrcDir() + "/prebuilts/ndk/current/sources"
Dan Albertbe961682015-03-18 23:38:50 -07001860 return fmt.Sprintf("%s/%s/%s", ndkSrcRoot, libDir, ctx.Arch().Abi)
1861 }
1862
1863 ctx.ModuleErrorf("Unknown NDK STL: %s", stl)
1864 return ""
1865}
1866
1867func (c *ndkPrebuiltStl) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
1868 deps CCDeps, objFiles []string) {
1869 // A null build step, but it sets up the output path.
1870 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
1871 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
1872 }
1873
Colin Crossfa138792015-04-24 17:31:52 -07001874 includeDirs := pathtools.PrefixPaths(c.Properties.Export_include_dirs, common.ModuleSrcDir(ctx))
Colin Cross28344522015-04-22 13:07:53 -07001875 c.exportFlags = []string{includeDirsToFlags(includeDirs)}
Dan Albertbe961682015-03-18 23:38:50 -07001876
1877 libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_")
1878 libExt := sharedLibraryExtension
1879 if c.LibraryProperties.BuildStatic {
1880 libExt = staticLibraryExtension
1881 }
1882
1883 stlName := strings.TrimSuffix(libName, "_shared")
1884 stlName = strings.TrimSuffix(stlName, "_static")
1885 libDir := getNdkStlLibDir(ctx, flags.Toolchain, stlName)
1886 c.out = libDir + "/" + libName + libExt
1887}
1888
Colin Cross3f40fa42015-01-30 17:27:36 -08001889func LinkageMutator(mctx blueprint.EarlyMutatorContext) {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001890 if c, ok := mctx.Module().(ccLinkedInterface); ok {
Colin Cross3f40fa42015-01-30 17:27:36 -08001891 var modules []blueprint.Module
Colin Crossed4cf0b2015-03-26 14:43:45 -07001892 if c.buildStatic() && c.buildShared() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001893 modules = mctx.CreateLocalVariations("static", "shared")
Colin Cross18b6dc52015-04-28 13:20:37 -07001894 modules[0].(ccLinkedInterface).setStatic(true)
1895 modules[1].(ccLinkedInterface).setStatic(false)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001896 } else if c.buildStatic() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001897 modules = mctx.CreateLocalVariations("static")
Colin Cross18b6dc52015-04-28 13:20:37 -07001898 modules[0].(ccLinkedInterface).setStatic(true)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001899 } else if c.buildShared() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001900 modules = mctx.CreateLocalVariations("shared")
Colin Cross18b6dc52015-04-28 13:20:37 -07001901 modules[0].(ccLinkedInterface).setStatic(false)
Colin Cross3f40fa42015-01-30 17:27:36 -08001902 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001903 panic(fmt.Errorf("ccLibrary %q not static or shared", mctx.ModuleName()))
Colin Cross3f40fa42015-01-30 17:27:36 -08001904 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001905
1906 if _, ok := c.(ccLibraryInterface); ok {
1907 reuseFrom := modules[0].(ccLibraryInterface)
1908 for _, m := range modules {
1909 m.(ccLibraryInterface).setReuseFrom(reuseFrom)
Colin Cross3f40fa42015-01-30 17:27:36 -08001910 }
1911 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001912 }
1913}
Colin Cross74d1ec02015-04-28 13:30:13 -07001914
1915// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
1916// modifies the slice contents in place, and returns a subslice of the original slice
1917func lastUniqueElements(list []string) []string {
1918 totalSkip := 0
1919 for i := len(list) - 1; i >= totalSkip; i-- {
1920 skip := 0
1921 for j := i - 1; j >= totalSkip; j-- {
1922 if list[i] == list[j] {
1923 skip++
1924 } else {
1925 list[j+skip] = list[j]
1926 }
1927 }
1928 totalSkip += skip
1929 }
1930 return list[totalSkip:]
1931}