blob: 2278ec0d7bd0f3b9ec389a4930799b00837f00a0 [file] [log] [blame]
Colin Cross5049f022015-03-18 13:28:46 -07001// Copyright 2015 Google Inc. All rights reserved.
Colin Cross3f40fa42015-01-30 17:27:36 -08002//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package cc
16
17// This file contains the module types for compiling C/C++ for Android, and converts the properties
18// into the flags and filenames necessary to pass to the compiler. The final creation of the rules
19// is handled in builder.go
20
21import (
Colin Cross3f40fa42015-01-30 17:27:36 -080022 "fmt"
23 "path/filepath"
Colin Cross0af4b842015-04-30 16:36:18 -070024 "runtime"
Colin Cross3f40fa42015-01-30 17:27:36 -080025 "strings"
26
Colin Cross97ba0732015-03-23 17:50:24 -070027 "github.com/google/blueprint"
28 "github.com/google/blueprint/pathtools"
29
Colin Cross463a90e2015-06-17 14:20:06 -070030 "android/soong"
Colin Cross3f40fa42015-01-30 17:27:36 -080031 "android/soong/common"
Colin Cross5049f022015-03-18 13:28:46 -070032 "android/soong/genrule"
Colin Cross3f40fa42015-01-30 17:27:36 -080033)
34
Colin Cross463a90e2015-06-17 14:20:06 -070035func init() {
36 soong.RegisterModuleType("cc_library_static", CCLibraryStaticFactory)
37 soong.RegisterModuleType("cc_library_shared", CCLibrarySharedFactory)
38 soong.RegisterModuleType("cc_library", CCLibraryFactory)
39 soong.RegisterModuleType("cc_object", CCObjectFactory)
40 soong.RegisterModuleType("cc_binary", CCBinaryFactory)
41 soong.RegisterModuleType("cc_test", CCTestFactory)
42 soong.RegisterModuleType("cc_benchmark", CCBenchmarkFactory)
43
44 soong.RegisterModuleType("toolchain_library", ToolchainLibraryFactory)
45 soong.RegisterModuleType("ndk_prebuilt_library", NdkPrebuiltLibraryFactory)
46 soong.RegisterModuleType("ndk_prebuilt_object", NdkPrebuiltObjectFactory)
47 soong.RegisterModuleType("ndk_prebuilt_static_stl", NdkPrebuiltStaticStlFactory)
48 soong.RegisterModuleType("ndk_prebuilt_shared_stl", NdkPrebuiltSharedStlFactory)
49
50 soong.RegisterModuleType("cc_library_host_static", CCLibraryHostStaticFactory)
51 soong.RegisterModuleType("cc_library_host_shared", CCLibraryHostSharedFactory)
52 soong.RegisterModuleType("cc_binary_host", CCBinaryHostFactory)
53 soong.RegisterModuleType("cc_test_host", CCTestHostFactory)
54 soong.RegisterModuleType("cc_benchmark_host", CCBenchmarkHostFactory)
55
56 // LinkageMutator must be registered after common.ArchMutator, but that is guaranteed by
57 // the Go initialization order because this package depends on common, so common's init
58 // functions will run first.
59 soong.RegisterEarlyMutator("link", LinkageMutator)
60 soong.RegisterEarlyMutator("test_per_src", TestPerSrcMutator)
61}
62
Colin Cross3f40fa42015-01-30 17:27:36 -080063var (
Colin Cross1332b002015-04-07 17:11:30 -070064 HostPrebuiltTag = pctx.VariableConfigMethod("HostPrebuiltTag", common.Config.PrebuiltOS)
65 SrcDir = pctx.VariableConfigMethod("SrcDir", common.Config.SrcDir)
Colin Cross3f40fa42015-01-30 17:27:36 -080066
Dan Willemsen87b17d12015-07-14 00:39:06 -070067 LibcRoot = pctx.StaticVariable("LibcRoot", "bionic/libc")
68 LibmRoot = pctx.StaticVariable("LibmRoot", "bionic/libm")
Colin Cross3f40fa42015-01-30 17:27:36 -080069)
70
71// Flags used by lots of devices. Putting them in package static variables will save bytes in
72// build.ninja so they aren't repeated for every file
73var (
74 commonGlobalCflags = []string{
75 "-DANDROID",
76 "-fmessage-length=0",
77 "-W",
78 "-Wall",
79 "-Wno-unused",
80 "-Winit-self",
81 "-Wpointer-arith",
Dan Willemsene6540452015-10-20 15:21:33 -070082 "-fdiagnostics-color",
83 "-fdebug-prefix-map=/proc/self/cwd=",
Colin Cross3f40fa42015-01-30 17:27:36 -080084
85 // COMMON_RELEASE_CFLAGS
86 "-DNDEBUG",
87 "-UDEBUG",
88 }
89
90 deviceGlobalCflags = []string{
91 // TARGET_ERROR_FLAGS
92 "-Werror=return-type",
93 "-Werror=non-virtual-dtor",
94 "-Werror=address",
95 "-Werror=sequence-point",
96 }
97
98 hostGlobalCflags = []string{}
99
100 commonGlobalCppflags = []string{
101 "-Wsign-promo",
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700102 }
103
104 illegalFlags = []string{
105 "-w",
Colin Cross3f40fa42015-01-30 17:27:36 -0800106 }
107)
108
109func init() {
110 pctx.StaticVariable("commonGlobalCflags", strings.Join(commonGlobalCflags, " "))
111 pctx.StaticVariable("deviceGlobalCflags", strings.Join(deviceGlobalCflags, " "))
112 pctx.StaticVariable("hostGlobalCflags", strings.Join(hostGlobalCflags, " "))
113
114 pctx.StaticVariable("commonGlobalCppflags", strings.Join(commonGlobalCppflags, " "))
115
116 pctx.StaticVariable("commonClangGlobalCflags",
117 strings.Join(clangFilterUnknownCflags(commonGlobalCflags), " "))
118 pctx.StaticVariable("deviceClangGlobalCflags",
119 strings.Join(clangFilterUnknownCflags(deviceGlobalCflags), " "))
120 pctx.StaticVariable("hostClangGlobalCflags",
121 strings.Join(clangFilterUnknownCflags(hostGlobalCflags), " "))
Tim Kilbournf2948142015-03-11 12:03:03 -0700122 pctx.StaticVariable("commonClangGlobalCppflags",
123 strings.Join(clangFilterUnknownCflags(commonGlobalCppflags), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800124
125 // Everything in this list is a crime against abstraction and dependency tracking.
126 // Do not add anything to this list.
127 pctx.StaticVariable("commonGlobalIncludes", strings.Join([]string{
128 "-isystem ${SrcDir}/system/core/include",
129 "-isystem ${SrcDir}/hardware/libhardware/include",
130 "-isystem ${SrcDir}/hardware/libhardware_legacy/include",
131 "-isystem ${SrcDir}/hardware/ril/include",
132 "-isystem ${SrcDir}/libnativehelper/include",
133 "-isystem ${SrcDir}/frameworks/native/include",
134 "-isystem ${SrcDir}/frameworks/native/opengl/include",
135 "-isystem ${SrcDir}/frameworks/av/include",
136 "-isystem ${SrcDir}/frameworks/base/include",
137 }, " "))
138
139 pctx.StaticVariable("clangPath", "${SrcDir}/prebuilts/clang/${HostPrebuiltTag}/host/3.6/bin/")
140}
141
Colin Cross3f40fa42015-01-30 17:27:36 -0800142// Building C/C++ code is handled by objects that satisfy this interface via composition
Colin Cross97ba0732015-03-23 17:50:24 -0700143type CCModuleType interface {
Colin Cross3f40fa42015-01-30 17:27:36 -0800144 common.AndroidModule
145
Colin Crossfa138792015-04-24 17:31:52 -0700146 // Modify property values after parsing Blueprints file but before starting dependency
147 // resolution or build rule generation
148 ModifyProperties(common.AndroidBaseContext)
149
Colin Cross21b9a242015-03-24 14:15:58 -0700150 // Modify the ccFlags
Colin Cross0676e2d2015-04-24 17:39:18 -0700151 flags(common.AndroidModuleContext, CCFlags) CCFlags
Colin Cross3f40fa42015-01-30 17:27:36 -0800152
Colin Cross21b9a242015-03-24 14:15:58 -0700153 // Return list of dependency names for use in AndroidDynamicDependencies and in depsToPaths
Colin Cross0676e2d2015-04-24 17:39:18 -0700154 depNames(common.AndroidBaseContext, CCDeps) CCDeps
Colin Cross3f40fa42015-01-30 17:27:36 -0800155
156 // Compile objects into final module
Colin Cross97ba0732015-03-23 17:50:24 -0700157 compileModule(common.AndroidModuleContext, CCFlags, CCDeps, []string)
Colin Cross3f40fa42015-01-30 17:27:36 -0800158
Dan Albertc403f7c2015-03-18 14:01:18 -0700159 // Install the built module.
Colin Cross97ba0732015-03-23 17:50:24 -0700160 installModule(common.AndroidModuleContext, CCFlags)
Dan Albertc403f7c2015-03-18 14:01:18 -0700161
Colin Cross3f40fa42015-01-30 17:27:36 -0800162 // Return the output file (.o, .a or .so) for use by other modules
163 outputFile() string
164}
165
Colin Cross97ba0732015-03-23 17:50:24 -0700166type CCDeps struct {
Colin Cross28344522015-04-22 13:07:53 -0700167 StaticLibs, SharedLibs, LateStaticLibs, WholeStaticLibs, ObjFiles, Cflags []string
Colin Crossc472d572015-03-17 15:06:21 -0700168
Colin Cross21b9a242015-03-24 14:15:58 -0700169 WholeStaticLibObjFiles []string
170
Colin Cross97ba0732015-03-23 17:50:24 -0700171 CrtBegin, CrtEnd string
Colin Crossc472d572015-03-17 15:06:21 -0700172}
173
Colin Cross97ba0732015-03-23 17:50:24 -0700174type CCFlags struct {
Colin Cross28344522015-04-22 13:07:53 -0700175 GlobalFlags []string // Flags that apply to C, C++, and assembly source files
176 AsFlags []string // Flags that apply to assembly source files
177 CFlags []string // Flags that apply to C and C++ source files
178 ConlyFlags []string // Flags that apply to C source files
179 CppFlags []string // Flags that apply to C++ source files
180 YaccFlags []string // Flags that apply to Yacc source files
181 LdFlags []string // Flags that apply to linker command lines
182
183 Nocrt bool
184 Toolchain Toolchain
185 Clang bool
Colin Crossc472d572015-03-17 15:06:21 -0700186}
187
Colin Cross7d5136f2015-05-11 13:39:40 -0700188// Properties used to compile all C or C++ modules
189type CCBaseProperties struct {
190 // list of source files used to compile the C/C++ module. May be .c, .cpp, or .S files.
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700191 Srcs []string `android:"arch_variant"`
192
193 // list of source files that should not be used to build the C/C++ module.
194 // This is most useful in the arch/multilib variants to remove non-common files
195 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700196
197 // list of module-specific flags that will be used for C and C++ compiles.
198 Cflags []string `android:"arch_variant"`
199
200 // list of module-specific flags that will be used for C++ compiles
201 Cppflags []string `android:"arch_variant"`
202
203 // list of module-specific flags that will be used for C compiles
204 Conlyflags []string `android:"arch_variant"`
205
206 // list of module-specific flags that will be used for .S compiles
207 Asflags []string `android:"arch_variant"`
208
209 // list of module-specific flags that will be used for .y and .yy compiles
210 Yaccflags []string
211
212 // list of module-specific flags that will be used for all link steps
213 Ldflags []string `android:"arch_variant"`
214
215 // the instruction set architecture to use to compile the C/C++
216 // module.
217 Instruction_set string `android:"arch_variant"`
218
219 // list of directories relative to the root of the source tree that will
220 // be added to the include path using -I.
221 // If possible, don't use this. If adding paths from the current directory use
222 // local_include_dirs, if adding paths from other modules use export_include_dirs in
223 // that module.
224 Include_dirs []string `android:"arch_variant"`
225
Colin Cross39d97f22015-09-14 12:30:50 -0700226 // list of files relative to the root of the source tree that will be included
227 // using -include.
228 // If possible, don't use this.
229 Include_files []string `android:"arch_variant"`
230
Colin Cross7d5136f2015-05-11 13:39:40 -0700231 // list of directories relative to the Blueprints file that will
232 // be added to the include path using -I
233 Local_include_dirs []string `android:"arch_variant"`
234
Colin Cross39d97f22015-09-14 12:30:50 -0700235 // list of files relative to the Blueprints file that will be included
236 // using -include.
237 // If possible, don't use this.
238 Local_include_files []string `android:"arch_variant"`
239
Colin Cross7d5136f2015-05-11 13:39:40 -0700240 // list of directories relative to the Blueprints file that will
241 // be added to the include path using -I for any module that links against this module
242 Export_include_dirs []string `android:"arch_variant"`
243
244 // list of module-specific flags that will be used for C and C++ compiles when
245 // compiling with clang
246 Clang_cflags []string `android:"arch_variant"`
247
248 // list of module-specific flags that will be used for .S compiles when
249 // compiling with clang
250 Clang_asflags []string `android:"arch_variant"`
251
252 // list of system libraries that will be dynamically linked to
253 // shared library and executable modules. If unset, generally defaults to libc
254 // and libm. Set to [] to prevent linking against libc and libm.
255 System_shared_libs []string
256
257 // list of modules whose object files should be linked into this module
258 // in their entirety. For static library modules, all of the .o files from the intermediate
259 // directory of the dependency will be linked into this modules .a file. For a shared library,
260 // the dependency's .a file will be linked into this module using -Wl,--whole-archive.
261 Whole_static_libs []string `android:"arch_variant"`
262
263 // list of modules that should be statically linked into this module.
264 Static_libs []string `android:"arch_variant"`
265
266 // list of modules that should be dynamically linked into this module.
267 Shared_libs []string `android:"arch_variant"`
268
269 // allow the module to contain undefined symbols. By default,
270 // modules cannot contain undefined symbols that are not satisified by their immediate
271 // dependencies. Set this flag to true to remove --no-undefined from the linker flags.
272 // This flag should only be necessary for compiling low-level libraries like libc.
273 Allow_undefined_symbols bool
274
275 // don't link in crt_begin and crt_end. This flag should only be necessary for
276 // compiling crt or libc.
277 Nocrt bool `android:"arch_variant"`
278
Dan Willemsend67be222015-09-16 15:19:33 -0700279 // don't link in libgcc.a
280 No_libgcc bool
281
Colin Cross7d5136f2015-05-11 13:39:40 -0700282 // don't insert default compiler flags into asflags, cflags,
283 // cppflags, conlyflags, ldflags, or include_dirs
284 No_default_compiler_flags bool
285
286 // compile module with clang instead of gcc
287 Clang bool `android:"arch_variant"`
288
289 // pass -frtti instead of -fno-rtti
290 Rtti bool
291
292 // -l arguments to pass to linker for host-provided shared libraries
293 Host_ldlibs []string `android:"arch_variant"`
294
295 // select the STL library to use. Possible values are "libc++", "libc++_static",
296 // "stlport", "stlport_static", "ndk", "libstdc++", or "none". Leave blank to select the
297 // default
298 Stl string
299
300 // Set for combined shared/static libraries to prevent compiling object files a second time
301 SkipCompileObjs bool `blueprint:"mutated"`
302
303 Debug, Release struct {
304 // list of module-specific flags that will be used for C and C++ compiles in debug or
305 // release builds
306 Cflags []string `android:"arch_variant"`
307 } `android:"arch_variant"`
308
309 // Minimum sdk version supported when compiling against the ndk
310 Sdk_version string
311
312 // install to a subdirectory of the default install path for the module
313 Relative_install_path string
314}
315
Colin Crossfa138792015-04-24 17:31:52 -0700316// CCBase contains the properties and members used by all C/C++ module types, and implements
Colin Crossc472d572015-03-17 15:06:21 -0700317// the blueprint.Module interface. It expects to be embedded into an outer specialization struct,
318// and uses a ccModuleType interface to that struct to create the build steps.
Colin Crossfa138792015-04-24 17:31:52 -0700319type CCBase struct {
Colin Crossc472d572015-03-17 15:06:21 -0700320 common.AndroidModuleBase
Colin Cross97ba0732015-03-23 17:50:24 -0700321 module CCModuleType
Colin Crossc472d572015-03-17 15:06:21 -0700322
Colin Cross7d5136f2015-05-11 13:39:40 -0700323 Properties CCBaseProperties
Colin Crossfa138792015-04-24 17:31:52 -0700324
325 unused struct {
Colin Crossb43a1592015-09-16 14:00:32 -0700326 Native_coverage bool
327 Required []string
Dan Willemsen9e0cb352015-09-16 15:41:55 -0700328 Sanitize []string `android:"arch_variant"`
Colin Crossb43a1592015-09-16 14:00:32 -0700329 Sanitize_recover []string
330 Strip string
331 Tags []string
Colin Crossfa138792015-04-24 17:31:52 -0700332 }
Colin Crossc472d572015-03-17 15:06:21 -0700333
334 installPath string
Colin Cross74d1ec02015-04-28 13:30:13 -0700335
336 savedDepNames CCDeps
Colin Crossc472d572015-03-17 15:06:21 -0700337}
338
Colin Crossfa138792015-04-24 17:31:52 -0700339func newCCBase(base *CCBase, module CCModuleType, hod common.HostOrDeviceSupported,
Colin Crossc472d572015-03-17 15:06:21 -0700340 multilib common.Multilib, props ...interface{}) (blueprint.Module, []interface{}) {
341
342 base.module = module
343
Colin Crossfa138792015-04-24 17:31:52 -0700344 props = append(props, &base.Properties, &base.unused)
Colin Crossc472d572015-03-17 15:06:21 -0700345
Colin Cross5049f022015-03-18 13:28:46 -0700346 return common.InitAndroidArchModule(module, hod, multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700347}
348
Colin Crossfa138792015-04-24 17:31:52 -0700349func (c *CCBase) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800350 toolchain := c.findToolchain(ctx)
351 if ctx.Failed() {
352 return
353 }
354
Colin Cross21b9a242015-03-24 14:15:58 -0700355 flags := c.collectFlags(ctx, toolchain)
Colin Cross3f40fa42015-01-30 17:27:36 -0800356 if ctx.Failed() {
357 return
358 }
359
Colin Cross74d1ec02015-04-28 13:30:13 -0700360 deps := c.depsToPaths(ctx, c.savedDepNames)
Colin Cross3f40fa42015-01-30 17:27:36 -0800361 if ctx.Failed() {
362 return
363 }
364
Colin Cross28344522015-04-22 13:07:53 -0700365 flags.CFlags = append(flags.CFlags, deps.Cflags...)
Colin Crossed9f8682015-03-18 17:17:35 -0700366
Colin Cross581c1892015-04-07 16:50:10 -0700367 objFiles := c.compileObjs(ctx, flags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800368 if ctx.Failed() {
369 return
370 }
371
Colin Cross581c1892015-04-07 16:50:10 -0700372 generatedObjFiles := c.compileGeneratedObjs(ctx, flags)
Colin Cross5049f022015-03-18 13:28:46 -0700373 if ctx.Failed() {
374 return
375 }
376
377 objFiles = append(objFiles, generatedObjFiles...)
378
Colin Cross3f40fa42015-01-30 17:27:36 -0800379 c.ccModuleType().compileModule(ctx, flags, deps, objFiles)
380 if ctx.Failed() {
381 return
382 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700383
384 c.ccModuleType().installModule(ctx, flags)
385 if ctx.Failed() {
386 return
387 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800388}
389
Colin Crossfa138792015-04-24 17:31:52 -0700390func (c *CCBase) ccModuleType() CCModuleType {
Colin Cross3f40fa42015-01-30 17:27:36 -0800391 return c.module
392}
393
Colin Crossfa138792015-04-24 17:31:52 -0700394var _ common.AndroidDynamicDepender = (*CCBase)(nil)
Colin Cross3f40fa42015-01-30 17:27:36 -0800395
Colin Crossfa138792015-04-24 17:31:52 -0700396func (c *CCBase) findToolchain(ctx common.AndroidModuleContext) Toolchain {
Colin Cross3f40fa42015-01-30 17:27:36 -0800397 arch := ctx.Arch()
Colin Crossd3ba0392015-05-07 14:11:29 -0700398 hod := ctx.HostOrDevice()
399 factory := toolchainFactories[hod][arch.ArchType]
Colin Cross3f40fa42015-01-30 17:27:36 -0800400 if factory == nil {
401 panic(fmt.Sprintf("Toolchain not found for %s arch %q",
Colin Crossd3ba0392015-05-07 14:11:29 -0700402 hod.String(), arch.String()))
Colin Cross3f40fa42015-01-30 17:27:36 -0800403 }
404 return factory(arch.ArchVariant, arch.CpuVariant)
405}
406
Colin Crossfa138792015-04-24 17:31:52 -0700407func (c *CCBase) ModifyProperties(ctx common.AndroidBaseContext) {
408}
409
Colin Crosse11befc2015-04-27 17:49:17 -0700410func (c *CCBase) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Crossfa138792015-04-24 17:31:52 -0700411 depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.Properties.Whole_static_libs...)
412 depNames.StaticLibs = append(depNames.StaticLibs, c.Properties.Static_libs...)
413 depNames.SharedLibs = append(depNames.SharedLibs, c.Properties.Shared_libs...)
Colin Cross21b9a242015-03-24 14:15:58 -0700414
Colin Cross21b9a242015-03-24 14:15:58 -0700415 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -0800416}
417
Colin Crossfa138792015-04-24 17:31:52 -0700418func (c *CCBase) AndroidDynamicDependencies(ctx common.AndroidDynamicDependerModuleContext) []string {
419 c.module.ModifyProperties(ctx)
420
Colin Cross74d1ec02015-04-28 13:30:13 -0700421 c.savedDepNames = c.module.depNames(ctx, CCDeps{})
422 c.savedDepNames.WholeStaticLibs = lastUniqueElements(c.savedDepNames.WholeStaticLibs)
423 c.savedDepNames.StaticLibs = lastUniqueElements(c.savedDepNames.StaticLibs)
424 c.savedDepNames.SharedLibs = lastUniqueElements(c.savedDepNames.SharedLibs)
425
426 staticLibs := c.savedDepNames.WholeStaticLibs
427 staticLibs = append(staticLibs, c.savedDepNames.StaticLibs...)
428 staticLibs = append(staticLibs, c.savedDepNames.LateStaticLibs...)
Colin Cross21b9a242015-03-24 14:15:58 -0700429 ctx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, staticLibs...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800430
Colin Cross74d1ec02015-04-28 13:30:13 -0700431 ctx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, c.savedDepNames.SharedLibs...)
Colin Cross21b9a242015-03-24 14:15:58 -0700432
Colin Cross74d1ec02015-04-28 13:30:13 -0700433 ret := append([]string(nil), c.savedDepNames.ObjFiles...)
434 if c.savedDepNames.CrtBegin != "" {
435 ret = append(ret, c.savedDepNames.CrtBegin)
Colin Cross21b9a242015-03-24 14:15:58 -0700436 }
Colin Cross74d1ec02015-04-28 13:30:13 -0700437 if c.savedDepNames.CrtEnd != "" {
438 ret = append(ret, c.savedDepNames.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700439 }
440
441 return ret
Colin Cross3f40fa42015-01-30 17:27:36 -0800442}
443
444// Create a ccFlags struct that collects the compile flags from global values,
445// per-target values, module type values, and per-module Blueprints properties
Colin Crossfa138792015-04-24 17:31:52 -0700446func (c *CCBase) collectFlags(ctx common.AndroidModuleContext, toolchain Toolchain) CCFlags {
Colin Cross97ba0732015-03-23 17:50:24 -0700447 flags := CCFlags{
Colin Crossfa138792015-04-24 17:31:52 -0700448 CFlags: c.Properties.Cflags,
449 CppFlags: c.Properties.Cppflags,
450 ConlyFlags: c.Properties.Conlyflags,
451 LdFlags: c.Properties.Ldflags,
452 AsFlags: c.Properties.Asflags,
453 YaccFlags: c.Properties.Yaccflags,
454 Nocrt: c.Properties.Nocrt,
Colin Cross97ba0732015-03-23 17:50:24 -0700455 Toolchain: toolchain,
Colin Crossfa138792015-04-24 17:31:52 -0700456 Clang: c.Properties.Clang,
Colin Cross3f40fa42015-01-30 17:27:36 -0800457 }
Colin Cross28344522015-04-22 13:07:53 -0700458
459 // Include dir cflags
Colin Crossf2298272015-05-12 11:36:53 -0700460 common.CheckSrcDirsExist(ctx, c.Properties.Include_dirs, "include_dirs")
461 common.CheckModuleSrcDirsExist(ctx, c.Properties.Local_include_dirs, "local_include_dirs")
462
Colin Crossfa138792015-04-24 17:31:52 -0700463 rootIncludeDirs := pathtools.PrefixPaths(c.Properties.Include_dirs, ctx.AConfig().SrcDir())
464 localIncludeDirs := pathtools.PrefixPaths(c.Properties.Local_include_dirs, common.ModuleSrcDir(ctx))
Colin Cross28344522015-04-22 13:07:53 -0700465 flags.GlobalFlags = append(flags.GlobalFlags,
Dan Willemsen1e898b92015-09-23 15:26:32 -0700466 includeDirsToFlags(localIncludeDirs),
467 includeDirsToFlags(rootIncludeDirs))
Colin Cross28344522015-04-22 13:07:53 -0700468
Colin Cross39d97f22015-09-14 12:30:50 -0700469 rootIncludeFiles := pathtools.PrefixPaths(c.Properties.Include_files, ctx.AConfig().SrcDir())
470 localIncludeFiles := pathtools.PrefixPaths(c.Properties.Local_include_files, common.ModuleSrcDir(ctx))
471
472 flags.GlobalFlags = append(flags.GlobalFlags,
473 includeFilesToFlags(rootIncludeFiles),
474 includeFilesToFlags(localIncludeFiles))
475
Colin Crossfa138792015-04-24 17:31:52 -0700476 if !c.Properties.No_default_compiler_flags {
477 if c.Properties.Sdk_version == "" || ctx.Host() {
Colin Cross28344522015-04-22 13:07:53 -0700478 flags.GlobalFlags = append(flags.GlobalFlags,
479 "${commonGlobalIncludes}",
480 toolchain.IncludeFlags(),
481 "-I${SrcDir}/libnativehelper/include/nativehelper")
482 }
483
484 flags.GlobalFlags = append(flags.GlobalFlags, []string{
485 "-I" + common.ModuleSrcDir(ctx),
486 "-I" + common.ModuleOutDir(ctx),
487 "-I" + common.ModuleGenDir(ctx),
488 }...)
489 }
490
Colin Crossfa138792015-04-24 17:31:52 -0700491 instructionSet := c.Properties.Instruction_set
Tim Kilbourn1a9bf262015-03-18 12:28:32 -0700492 instructionSetFlags, err := toolchain.InstructionSetFlags(instructionSet)
493 if err != nil {
494 ctx.ModuleErrorf("%s", err)
495 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800496
Colin Crossaf19a292015-03-18 12:07:10 -0700497 // TODO: debug
Colin Crossfa138792015-04-24 17:31:52 -0700498 flags.CFlags = append(flags.CFlags, c.Properties.Release.Cflags...)
Colin Crossaf19a292015-03-18 12:07:10 -0700499
Dan Willemsendd0e2c32015-10-20 14:29:35 -0700500 if !ctx.ContainsProperty("clang") {
501 if ctx.Host() {
502 flags.Clang = true
503 }
504
505 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
506 flags.Clang = true
507 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800508 }
509
Colin Cross97ba0732015-03-23 17:50:24 -0700510 if flags.Clang {
511 flags.CFlags = clangFilterUnknownCflags(flags.CFlags)
Colin Crossfa138792015-04-24 17:31:52 -0700512 flags.CFlags = append(flags.CFlags, c.Properties.Clang_cflags...)
513 flags.AsFlags = append(flags.AsFlags, c.Properties.Clang_asflags...)
Colin Cross97ba0732015-03-23 17:50:24 -0700514 flags.CppFlags = clangFilterUnknownCflags(flags.CppFlags)
515 flags.ConlyFlags = clangFilterUnknownCflags(flags.ConlyFlags)
516 flags.LdFlags = clangFilterUnknownCflags(flags.LdFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800517
Colin Cross97ba0732015-03-23 17:50:24 -0700518 flags.CFlags = append(flags.CFlags, "${clangExtraCflags}")
519 flags.ConlyFlags = append(flags.ConlyFlags, "${clangExtraConlyflags}")
Colin Crossf6566ed2015-03-24 11:13:38 -0700520 if ctx.Device() {
Colin Cross97ba0732015-03-23 17:50:24 -0700521 flags.CFlags = append(flags.CFlags, "${clangExtraTargetCflags}")
Colin Crossbdd7b1c2015-03-16 16:21:20 -0700522 }
523
Colin Cross3f40fa42015-01-30 17:27:36 -0800524 target := "-target " + toolchain.ClangTriple()
525 gccPrefix := "-B" + filepath.Join(toolchain.GccRoot(), toolchain.GccTriple(), "bin")
526
Colin Cross97ba0732015-03-23 17:50:24 -0700527 flags.CFlags = append(flags.CFlags, target, gccPrefix)
528 flags.AsFlags = append(flags.AsFlags, target, gccPrefix)
529 flags.LdFlags = append(flags.LdFlags, target, gccPrefix)
Colin Cross3f40fa42015-01-30 17:27:36 -0800530 }
531
Colin Crossfa138792015-04-24 17:31:52 -0700532 if !c.Properties.No_default_compiler_flags {
533 if ctx.Device() && !c.Properties.Allow_undefined_symbols {
Colin Cross97ba0732015-03-23 17:50:24 -0700534 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-undefined")
Colin Cross3f40fa42015-01-30 17:27:36 -0800535 }
536
Colin Cross56b4d452015-04-21 17:38:44 -0700537 flags.GlobalFlags = append(flags.GlobalFlags, instructionSetFlags)
538
Colin Cross97ba0732015-03-23 17:50:24 -0700539 if flags.Clang {
540 flags.CppFlags = append(flags.CppFlags, "${commonClangGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -0700541 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -0800542 toolchain.ClangCflags(),
543 "${commonClangGlobalCflags}",
Colin Crossd3ba0392015-05-07 14:11:29 -0700544 fmt.Sprintf("${%sClangGlobalCflags}", ctx.HostOrDevice()))
Colin Cross3f40fa42015-01-30 17:27:36 -0800545 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700546 flags.CppFlags = append(flags.CppFlags, "${commonGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -0700547 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -0800548 toolchain.Cflags(),
549 "${commonGlobalCflags}",
Colin Crossd3ba0392015-05-07 14:11:29 -0700550 fmt.Sprintf("${%sGlobalCflags}", ctx.HostOrDevice()))
Colin Cross3f40fa42015-01-30 17:27:36 -0800551 }
552
Colin Crossf6566ed2015-03-24 11:13:38 -0700553 if ctx.Device() {
Colin Crossfa138792015-04-24 17:31:52 -0700554 if c.Properties.Rtti {
Colin Cross97ba0732015-03-23 17:50:24 -0700555 flags.CppFlags = append(flags.CppFlags, "-frtti")
Colin Cross3f40fa42015-01-30 17:27:36 -0800556 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700557 flags.CppFlags = append(flags.CppFlags, "-fno-rtti")
Colin Cross3f40fa42015-01-30 17:27:36 -0800558 }
559 }
560
Colin Cross97ba0732015-03-23 17:50:24 -0700561 flags.AsFlags = append(flags.AsFlags, "-D__ASSEMBLY__")
Colin Cross3f40fa42015-01-30 17:27:36 -0800562
Colin Cross97ba0732015-03-23 17:50:24 -0700563 if flags.Clang {
564 flags.CppFlags = append(flags.CppFlags, toolchain.ClangCppflags())
565 flags.LdFlags = append(flags.LdFlags, toolchain.ClangLdflags())
Colin Cross3f40fa42015-01-30 17:27:36 -0800566 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700567 flags.CppFlags = append(flags.CppFlags, toolchain.Cppflags())
568 flags.LdFlags = append(flags.LdFlags, toolchain.Ldflags())
Colin Cross3f40fa42015-01-30 17:27:36 -0800569 }
Colin Cross28344522015-04-22 13:07:53 -0700570
571 if ctx.Host() {
Colin Crossfa138792015-04-24 17:31:52 -0700572 flags.LdFlags = append(flags.LdFlags, c.Properties.Host_ldlibs...)
Colin Cross28344522015-04-22 13:07:53 -0700573 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800574 }
575
Colin Cross0676e2d2015-04-24 17:39:18 -0700576 flags = c.ccModuleType().flags(ctx, flags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800577
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700578 if c.Properties.Sdk_version == "" {
579 if ctx.Host() && !flags.Clang {
580 // The host GCC doesn't support C++14 (and is deprecated, so likely
581 // never will). Build these modules with C++11.
582 flags.CppFlags = append(flags.CppFlags, "-std=gnu++11")
583 } else {
584 flags.CppFlags = append(flags.CppFlags, "-std=gnu++14")
585 }
586 }
587
588 flags.CFlags, _ = filterList(flags.CFlags, illegalFlags)
589 flags.CppFlags, _ = filterList(flags.CppFlags, illegalFlags)
590 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, illegalFlags)
591
Colin Cross3f40fa42015-01-30 17:27:36 -0800592 // Optimization to reduce size of build.ninja
593 // Replace the long list of flags for each file with a module-local variable
Colin Cross97ba0732015-03-23 17:50:24 -0700594 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
595 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
596 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
597 flags.CFlags = []string{"$cflags"}
598 flags.CppFlags = []string{"$cppflags"}
599 flags.AsFlags = []string{"$asflags"}
Colin Cross3f40fa42015-01-30 17:27:36 -0800600
601 return flags
602}
603
Colin Cross0676e2d2015-04-24 17:39:18 -0700604func (c *CCBase) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Cross3f40fa42015-01-30 17:27:36 -0800605 return flags
606}
607
608// Compile a list of source files into objects a specified subdirectory
Colin Crossfa138792015-04-24 17:31:52 -0700609func (c *CCBase) customCompileObjs(ctx common.AndroidModuleContext, flags CCFlags,
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700610 subdir string, srcFiles, excludes []string) []string {
Colin Cross581c1892015-04-07 16:50:10 -0700611
612 buildFlags := ccFlagsToBuilderFlags(flags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800613
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700614 srcFiles = ctx.ExpandSources(srcFiles, excludes)
Colin Cross581c1892015-04-07 16:50:10 -0700615 srcFiles, deps := genSources(ctx, srcFiles, buildFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800616
Colin Cross581c1892015-04-07 16:50:10 -0700617 return TransformSourceToObj(ctx, subdir, srcFiles, buildFlags, deps)
Colin Cross3f40fa42015-01-30 17:27:36 -0800618}
619
Colin Crossfa138792015-04-24 17:31:52 -0700620// Compile files listed in c.Properties.Srcs into objects
621func (c *CCBase) compileObjs(ctx common.AndroidModuleContext, flags CCFlags) []string {
Colin Cross3f40fa42015-01-30 17:27:36 -0800622
Colin Crossfa138792015-04-24 17:31:52 -0700623 if c.Properties.SkipCompileObjs {
Colin Cross3f40fa42015-01-30 17:27:36 -0800624 return nil
625 }
626
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700627 return c.customCompileObjs(ctx, flags, "", c.Properties.Srcs, c.Properties.Exclude_srcs)
Colin Cross3f40fa42015-01-30 17:27:36 -0800628}
629
Colin Cross5049f022015-03-18 13:28:46 -0700630// Compile generated source files from dependencies
Colin Crossfa138792015-04-24 17:31:52 -0700631func (c *CCBase) compileGeneratedObjs(ctx common.AndroidModuleContext, flags CCFlags) []string {
Colin Cross5049f022015-03-18 13:28:46 -0700632 var srcs []string
633
Colin Crossfa138792015-04-24 17:31:52 -0700634 if c.Properties.SkipCompileObjs {
Colin Cross5049f022015-03-18 13:28:46 -0700635 return nil
636 }
637
638 ctx.VisitDirectDeps(func(module blueprint.Module) {
639 if gen, ok := module.(genrule.SourceFileGenerator); ok {
640 srcs = append(srcs, gen.GeneratedSourceFiles()...)
641 }
642 })
643
644 if len(srcs) == 0 {
645 return nil
646 }
647
Colin Cross581c1892015-04-07 16:50:10 -0700648 return TransformSourceToObj(ctx, "", srcs, ccFlagsToBuilderFlags(flags), nil)
Colin Cross5049f022015-03-18 13:28:46 -0700649}
650
Colin Crossfa138792015-04-24 17:31:52 -0700651func (c *CCBase) outputFile() string {
Colin Cross3f40fa42015-01-30 17:27:36 -0800652 return ""
653}
654
Colin Crossfa138792015-04-24 17:31:52 -0700655func (c *CCBase) depsToPathsFromList(ctx common.AndroidModuleContext,
Colin Cross3f40fa42015-01-30 17:27:36 -0800656 names []string) (modules []common.AndroidModule,
Colin Cross28344522015-04-22 13:07:53 -0700657 outputFiles []string, exportedFlags []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800658
659 for _, n := range names {
660 found := false
661 ctx.VisitDirectDeps(func(m blueprint.Module) {
662 otherName := ctx.OtherModuleName(m)
663 if otherName != n {
664 return
665 }
666
Colin Cross97ba0732015-03-23 17:50:24 -0700667 if a, ok := m.(CCModuleType); ok {
Colin Cross3f40fa42015-01-30 17:27:36 -0800668 if a.Disabled() {
669 // If a cc_library host+device module depends on a library that exists as both
670 // cc_library_shared and cc_library_host_shared, it will end up with two
671 // dependencies with the same name, one of which is marked disabled for each
672 // of host and device. Ignore the disabled one.
673 return
674 }
Colin Crossd3ba0392015-05-07 14:11:29 -0700675 if a.HostOrDevice() != ctx.HostOrDevice() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800676 ctx.ModuleErrorf("host/device mismatch between %q and %q", ctx.ModuleName(),
677 otherName)
678 return
679 }
680
681 if outputFile := a.outputFile(); outputFile != "" {
682 if found {
683 ctx.ModuleErrorf("multiple modules satisified dependency on %q", otherName)
684 return
685 }
686 outputFiles = append(outputFiles, outputFile)
687 modules = append(modules, a)
Colin Cross28344522015-04-22 13:07:53 -0700688 if i, ok := a.(ccExportedFlagsProducer); ok {
689 exportedFlags = append(exportedFlags, i.exportedFlags()...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800690 }
691 found = true
692 } else {
693 ctx.ModuleErrorf("module %q missing output file", otherName)
694 return
695 }
696 } else {
697 ctx.ModuleErrorf("module %q not an android module", otherName)
698 return
699 }
700 })
701 if !found {
702 ctx.ModuleErrorf("unsatisified dependency on %q", n)
703 }
704 }
705
Colin Cross28344522015-04-22 13:07:53 -0700706 return modules, outputFiles, exportedFlags
Colin Cross3f40fa42015-01-30 17:27:36 -0800707}
708
Colin Cross21b9a242015-03-24 14:15:58 -0700709// Convert depenedency names to paths. Takes a CCDeps containing names and returns a CCDeps
710// containing paths
Colin Crossfa138792015-04-24 17:31:52 -0700711func (c *CCBase) depsToPaths(ctx common.AndroidModuleContext, depNames CCDeps) CCDeps {
Colin Cross21b9a242015-03-24 14:15:58 -0700712 var depPaths CCDeps
Colin Cross28344522015-04-22 13:07:53 -0700713 var newCflags []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800714
Colin Cross21b9a242015-03-24 14:15:58 -0700715 var wholeStaticLibModules []common.AndroidModule
Colin Cross3f40fa42015-01-30 17:27:36 -0800716
Colin Cross28344522015-04-22 13:07:53 -0700717 wholeStaticLibModules, depPaths.WholeStaticLibs, newCflags =
Colin Cross21b9a242015-03-24 14:15:58 -0700718 c.depsToPathsFromList(ctx, depNames.WholeStaticLibs)
Colin Cross28344522015-04-22 13:07:53 -0700719 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800720
Colin Cross21b9a242015-03-24 14:15:58 -0700721 for _, m := range wholeStaticLibModules {
722 if staticLib, ok := m.(ccLibraryInterface); ok && staticLib.static() {
723 depPaths.WholeStaticLibObjFiles =
724 append(depPaths.WholeStaticLibObjFiles, staticLib.allObjFiles()...)
725 } else {
726 ctx.ModuleErrorf("module %q not a static library", ctx.OtherModuleName(m))
727 }
728 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800729
Colin Cross28344522015-04-22 13:07:53 -0700730 _, depPaths.StaticLibs, newCflags = c.depsToPathsFromList(ctx, depNames.StaticLibs)
731 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross21b9a242015-03-24 14:15:58 -0700732
Colin Cross28344522015-04-22 13:07:53 -0700733 _, depPaths.LateStaticLibs, newCflags = c.depsToPathsFromList(ctx, depNames.LateStaticLibs)
734 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross21b9a242015-03-24 14:15:58 -0700735
Colin Cross28344522015-04-22 13:07:53 -0700736 _, depPaths.SharedLibs, newCflags = c.depsToPathsFromList(ctx, depNames.SharedLibs)
737 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross21b9a242015-03-24 14:15:58 -0700738
739 ctx.VisitDirectDeps(func(m blueprint.Module) {
Dan Albertc3144b12015-04-28 18:17:56 -0700740 if obj, ok := m.(ccObjectProvider); ok {
Colin Cross21b9a242015-03-24 14:15:58 -0700741 otherName := ctx.OtherModuleName(m)
742 if otherName == depNames.CrtBegin {
Colin Crossfa138792015-04-24 17:31:52 -0700743 if !c.Properties.Nocrt {
Dan Albertc3144b12015-04-28 18:17:56 -0700744 depPaths.CrtBegin = obj.object().outputFile()
Colin Cross21b9a242015-03-24 14:15:58 -0700745 }
746 } else if otherName == depNames.CrtEnd {
Colin Crossfa138792015-04-24 17:31:52 -0700747 if !c.Properties.Nocrt {
Dan Albertc3144b12015-04-28 18:17:56 -0700748 depPaths.CrtEnd = obj.object().outputFile()
Colin Cross21b9a242015-03-24 14:15:58 -0700749 }
750 } else {
Dan Albertc3144b12015-04-28 18:17:56 -0700751 depPaths.ObjFiles = append(depPaths.ObjFiles, obj.object().outputFile())
Colin Cross21b9a242015-03-24 14:15:58 -0700752 }
753 }
754 })
755
756 return depPaths
Colin Cross3f40fa42015-01-30 17:27:36 -0800757}
758
Colin Cross7d5136f2015-05-11 13:39:40 -0700759type ccLinkedProperties struct {
760 VariantIsShared bool `blueprint:"mutated"`
761 VariantIsStatic bool `blueprint:"mutated"`
762 VariantIsStaticBinary bool `blueprint:"mutated"`
763}
764
Colin Crossfa138792015-04-24 17:31:52 -0700765// CCLinked contains the properties and members used by libraries and executables
766type CCLinked struct {
767 CCBase
Colin Cross7d5136f2015-05-11 13:39:40 -0700768 dynamicProperties ccLinkedProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800769}
770
Colin Crossfa138792015-04-24 17:31:52 -0700771func newCCDynamic(dynamic *CCLinked, module CCModuleType, hod common.HostOrDeviceSupported,
Colin Crossc472d572015-03-17 15:06:21 -0700772 multilib common.Multilib, props ...interface{}) (blueprint.Module, []interface{}) {
773
Colin Crossed4cf0b2015-03-26 14:43:45 -0700774 props = append(props, &dynamic.dynamicProperties)
775
Colin Crossfa138792015-04-24 17:31:52 -0700776 return newCCBase(&dynamic.CCBase, module, hod, multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700777}
778
Colin Crossfa138792015-04-24 17:31:52 -0700779func (c *CCLinked) systemSharedLibs(ctx common.AndroidBaseContext) []string {
Colin Cross28d76592015-03-26 16:14:04 -0700780 if ctx.ContainsProperty("system_shared_libs") {
Colin Crossfa138792015-04-24 17:31:52 -0700781 return c.Properties.System_shared_libs
782 } else if ctx.Device() && c.Properties.Sdk_version == "" {
Colin Cross577f6e42015-03-27 18:23:34 -0700783 return []string{"libc", "libm"}
Colin Cross28d76592015-03-26 16:14:04 -0700784 } else {
Colin Cross577f6e42015-03-27 18:23:34 -0700785 return nil
Colin Cross3f40fa42015-01-30 17:27:36 -0800786 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800787}
788
Colin Crossfa138792015-04-24 17:31:52 -0700789func (c *CCLinked) stl(ctx common.AndroidBaseContext) string {
790 if c.Properties.Sdk_version != "" && ctx.Device() {
791 switch c.Properties.Stl {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700792 case "":
793 return "ndk_system"
794 case "c++_shared", "c++_static",
795 "stlport_shared", "stlport_static",
796 "gnustl_static":
Colin Crossfa138792015-04-24 17:31:52 -0700797 return "ndk_lib" + c.Properties.Stl
Colin Crossed4cf0b2015-03-26 14:43:45 -0700798 default:
Colin Crossfa138792015-04-24 17:31:52 -0700799 ctx.ModuleErrorf("stl: %q is not a supported STL with sdk_version set", c.Properties.Stl)
Colin Crossed4cf0b2015-03-26 14:43:45 -0700800 return ""
801 }
802 }
803
Colin Crossfa138792015-04-24 17:31:52 -0700804 switch c.Properties.Stl {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700805 case "libc++", "libc++_static",
Colin Crossed4cf0b2015-03-26 14:43:45 -0700806 "libstdc++":
Colin Crossfa138792015-04-24 17:31:52 -0700807 return c.Properties.Stl
Colin Crossed4cf0b2015-03-26 14:43:45 -0700808 case "none":
809 return ""
810 case "":
Colin Cross18b6dc52015-04-28 13:20:37 -0700811 if c.static() {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700812 return "libc++_static"
Colin Cross18b6dc52015-04-28 13:20:37 -0700813 } else {
814 return "libc++" // TODO: mingw needs libstdc++
Colin Crossed4cf0b2015-03-26 14:43:45 -0700815 }
816 default:
Colin Crossfa138792015-04-24 17:31:52 -0700817 ctx.ModuleErrorf("stl: %q is not a supported STL", c.Properties.Stl)
Colin Crossed4cf0b2015-03-26 14:43:45 -0700818 return ""
819 }
820}
821
Colin Cross0af4b842015-04-30 16:36:18 -0700822var hostDynamicGccLibs, hostStaticGccLibs []string
823
824func init() {
825 if runtime.GOOS == "darwin" {
826 hostDynamicGccLibs = []string{"-lc", "-lSystem"}
827 hostStaticGccLibs = []string{"NO_STATIC_HOST_BINARIES_ON_DARWIN"}
828 } else {
829 hostDynamicGccLibs = []string{"-lgcc_s", "-lgcc", "-lc", "-lgcc_s", "-lgcc"}
830 hostStaticGccLibs = []string{"-Wl,--start-group", "-lgcc", "-lgcc_eh", "-lc", "-Wl,--end-group"}
831 }
832}
Colin Cross712fc022015-04-27 11:13:34 -0700833
Colin Crosse11befc2015-04-27 17:49:17 -0700834func (c *CCLinked) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700835 stl := c.stl(ctx)
836 if ctx.Failed() {
837 return flags
838 }
839
840 switch stl {
841 case "libc++", "libc++_static":
842 flags.CFlags = append(flags.CFlags, "-D_USING_LIBCXX")
Colin Crossed4cf0b2015-03-26 14:43:45 -0700843 if ctx.Host() {
844 flags.CppFlags = append(flags.CppFlags, "-nostdinc++")
845 flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs")
Colin Cross712fc022015-04-27 11:13:34 -0700846 flags.LdFlags = append(flags.LdFlags, "-lm", "-lpthread")
Colin Cross18b6dc52015-04-28 13:20:37 -0700847 if c.staticBinary() {
Colin Cross712fc022015-04-27 11:13:34 -0700848 flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs...)
Colin Cross18b6dc52015-04-28 13:20:37 -0700849 } else {
850 flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs...)
Colin Cross712fc022015-04-27 11:13:34 -0700851 }
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700852 } else {
853 if ctx.Arch().ArchType == common.Arm {
854 flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,libunwind_llvm.a")
855 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700856 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700857 case "libstdc++":
858 // Using bionic's basic libstdc++. Not actually an STL. Only around until the
859 // tree is in good enough shape to not need it.
860 // Host builds will use GNU libstdc++.
861 if ctx.Device() {
Colin Cross28344522015-04-22 13:07:53 -0700862 flags.CFlags = append(flags.CFlags, "-I${SrcDir}/bionic/libstdc++/include")
Colin Crossed4cf0b2015-03-26 14:43:45 -0700863 }
864 case "ndk_system":
Colin Cross1332b002015-04-07 17:11:30 -0700865 ndkSrcRoot := ctx.AConfig().SrcDir() + "/prebuilts/ndk/current/sources/"
Colin Cross28344522015-04-22 13:07:53 -0700866 flags.CFlags = append(flags.CFlags, "-isystem "+ndkSrcRoot+"cxx-stl/system/include")
Colin Crossed4cf0b2015-03-26 14:43:45 -0700867 case "ndk_libc++_shared", "ndk_libc++_static":
868 // TODO(danalbert): This really shouldn't be here...
869 flags.CppFlags = append(flags.CppFlags, "-std=c++11")
870 case "ndk_libstlport_shared", "ndk_libstlport_static", "ndk_libgnustl_static":
871 // Nothing
872 case "":
873 // None or error.
874 if ctx.Host() {
875 flags.CppFlags = append(flags.CppFlags, "-nostdinc++")
876 flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs")
Colin Cross18b6dc52015-04-28 13:20:37 -0700877 if c.staticBinary() {
Colin Cross712fc022015-04-27 11:13:34 -0700878 flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs...)
Colin Cross18b6dc52015-04-28 13:20:37 -0700879 } else {
880 flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs...)
Colin Cross712fc022015-04-27 11:13:34 -0700881 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700882 }
883 default:
Colin Crossfa138792015-04-24 17:31:52 -0700884 panic(fmt.Errorf("Unknown stl in CCLinked.Flags: %q", stl))
Colin Crossed4cf0b2015-03-26 14:43:45 -0700885 }
886
887 return flags
888}
889
Colin Crosse11befc2015-04-27 17:49:17 -0700890func (c *CCLinked) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
891 depNames = c.CCBase.depNames(ctx, depNames)
Colin Cross3f40fa42015-01-30 17:27:36 -0800892
Colin Crossed4cf0b2015-03-26 14:43:45 -0700893 stl := c.stl(ctx)
894 if ctx.Failed() {
895 return depNames
896 }
897
898 switch stl {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700899 case "libstdc++":
900 if ctx.Device() {
901 depNames.SharedLibs = append(depNames.SharedLibs, stl)
902 }
Colin Cross74d1ec02015-04-28 13:30:13 -0700903 case "libc++", "libc++_static":
904 if stl == "libc++" {
905 depNames.SharedLibs = append(depNames.SharedLibs, stl)
906 } else {
907 depNames.StaticLibs = append(depNames.StaticLibs, stl)
908 }
909 if ctx.Device() {
910 if ctx.Arch().ArchType == common.Arm {
911 depNames.StaticLibs = append(depNames.StaticLibs, "libunwind_llvm")
912 }
913 if c.staticBinary() {
914 depNames.StaticLibs = append(depNames.StaticLibs, "libdl")
915 } else {
916 depNames.SharedLibs = append(depNames.SharedLibs, "libdl")
917 }
918 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700919 case "":
920 // None or error.
921 case "ndk_system":
922 // TODO: Make a system STL prebuilt for the NDK.
923 // 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 -0700924 // its own includes. The includes are handled in CCBase.Flags().
Colin Cross577f6e42015-03-27 18:23:34 -0700925 depNames.SharedLibs = append([]string{"libstdc++"}, depNames.SharedLibs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -0700926 case "ndk_libc++_shared", "ndk_libstlport_shared":
927 depNames.SharedLibs = append(depNames.SharedLibs, stl)
928 case "ndk_libc++_static", "ndk_libstlport_static", "ndk_libgnustl_static":
929 depNames.StaticLibs = append(depNames.StaticLibs, stl)
930 default:
Colin Crosse11befc2015-04-27 17:49:17 -0700931 panic(fmt.Errorf("Unknown stl in CCLinked.depNames: %q", stl))
Colin Crossed4cf0b2015-03-26 14:43:45 -0700932 }
933
Colin Cross74d1ec02015-04-28 13:30:13 -0700934 if ctx.ModuleName() != "libcompiler_rt-extras" {
935 depNames.StaticLibs = append(depNames.StaticLibs, "libcompiler_rt-extras")
936 }
937
Colin Crossf6566ed2015-03-24 11:13:38 -0700938 if ctx.Device() {
Colin Cross77b00fa2015-03-16 16:15:49 -0700939 // libgcc and libatomic have to be last on the command line
Dan Willemsend67be222015-09-16 15:19:33 -0700940 depNames.LateStaticLibs = append(depNames.LateStaticLibs, "libgcov", "libatomic")
941 if !c.Properties.No_libgcc {
942 depNames.LateStaticLibs = append(depNames.LateStaticLibs, "libgcc")
943 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700944
Colin Cross18b6dc52015-04-28 13:20:37 -0700945 if !c.static() {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700946 depNames.SharedLibs = append(depNames.SharedLibs, c.systemSharedLibs(ctx)...)
947 }
Colin Cross577f6e42015-03-27 18:23:34 -0700948
Colin Crossfa138792015-04-24 17:31:52 -0700949 if c.Properties.Sdk_version != "" {
950 version := c.Properties.Sdk_version
Colin Cross577f6e42015-03-27 18:23:34 -0700951 depNames.SharedLibs = append(depNames.SharedLibs,
952 "ndk_libc."+version,
953 "ndk_libm."+version,
954 )
955 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800956 }
957
Colin Cross21b9a242015-03-24 14:15:58 -0700958 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -0800959}
960
Colin Crossed4cf0b2015-03-26 14:43:45 -0700961// ccLinkedInterface interface is used on ccLinked to deal with static or shared variants
962type ccLinkedInterface interface {
963 // Returns true if the build options for the module have selected a static or shared build
964 buildStatic() bool
965 buildShared() bool
966
967 // Sets whether a specific variant is static or shared
Colin Cross18b6dc52015-04-28 13:20:37 -0700968 setStatic(bool)
Colin Crossed4cf0b2015-03-26 14:43:45 -0700969
Colin Cross18b6dc52015-04-28 13:20:37 -0700970 // Returns whether a specific variant is a static library or binary
Colin Crossed4cf0b2015-03-26 14:43:45 -0700971 static() bool
Colin Cross18b6dc52015-04-28 13:20:37 -0700972
973 // Returns whether a module is a static binary
974 staticBinary() bool
Colin Crossed4cf0b2015-03-26 14:43:45 -0700975}
976
977var _ ccLinkedInterface = (*CCLibrary)(nil)
978var _ ccLinkedInterface = (*CCBinary)(nil)
979
Colin Crossfa138792015-04-24 17:31:52 -0700980func (c *CCLinked) static() bool {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700981 return c.dynamicProperties.VariantIsStatic
982}
983
Colin Cross18b6dc52015-04-28 13:20:37 -0700984func (c *CCLinked) staticBinary() bool {
985 return c.dynamicProperties.VariantIsStaticBinary
Colin Crossed4cf0b2015-03-26 14:43:45 -0700986}
987
Colin Cross18b6dc52015-04-28 13:20:37 -0700988func (c *CCLinked) setStatic(static bool) {
989 c.dynamicProperties.VariantIsStatic = static
Colin Crossed4cf0b2015-03-26 14:43:45 -0700990}
991
Colin Cross28344522015-04-22 13:07:53 -0700992type ccExportedFlagsProducer interface {
993 exportedFlags() []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800994}
995
996//
997// Combined static+shared libraries
998//
999
Colin Cross7d5136f2015-05-11 13:39:40 -07001000type CCLibraryProperties struct {
1001 BuildStatic bool `blueprint:"mutated"`
1002 BuildShared bool `blueprint:"mutated"`
1003 Static struct {
1004 Srcs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001005 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -07001006 Cflags []string `android:"arch_variant"`
1007 Whole_static_libs []string `android:"arch_variant"`
1008 Static_libs []string `android:"arch_variant"`
1009 Shared_libs []string `android:"arch_variant"`
1010 } `android:"arch_variant"`
1011 Shared struct {
1012 Srcs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001013 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -07001014 Cflags []string `android:"arch_variant"`
1015 Whole_static_libs []string `android:"arch_variant"`
1016 Static_libs []string `android:"arch_variant"`
1017 Shared_libs []string `android:"arch_variant"`
1018 } `android:"arch_variant"`
Colin Crossaee540a2015-07-06 17:48:31 -07001019
1020 // local file name to pass to the linker as --version_script
1021 Version_script string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -07001022}
1023
Colin Cross97ba0732015-03-23 17:50:24 -07001024type CCLibrary struct {
Colin Crossfa138792015-04-24 17:31:52 -07001025 CCLinked
Colin Cross3f40fa42015-01-30 17:27:36 -08001026
Colin Cross28344522015-04-22 13:07:53 -07001027 reuseFrom ccLibraryInterface
1028 reuseObjFiles []string
1029 objFiles []string
1030 exportFlags []string
1031 out string
Colin Cross3f40fa42015-01-30 17:27:36 -08001032
Colin Cross7d5136f2015-05-11 13:39:40 -07001033 LibraryProperties CCLibraryProperties
Colin Cross3f40fa42015-01-30 17:27:36 -08001034}
1035
Colin Crossed4cf0b2015-03-26 14:43:45 -07001036func (c *CCLibrary) buildStatic() bool {
1037 return c.LibraryProperties.BuildStatic
1038}
1039
1040func (c *CCLibrary) buildShared() bool {
1041 return c.LibraryProperties.BuildShared
1042}
1043
Colin Cross97ba0732015-03-23 17:50:24 -07001044type ccLibraryInterface interface {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001045 ccLinkedInterface
Colin Cross97ba0732015-03-23 17:50:24 -07001046 ccLibrary() *CCLibrary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001047 setReuseFrom(ccLibraryInterface)
1048 getReuseFrom() ccLibraryInterface
1049 getReuseObjFiles() []string
Colin Cross97ba0732015-03-23 17:50:24 -07001050 allObjFiles() []string
Colin Crossc472d572015-03-17 15:06:21 -07001051}
1052
Colin Crossed4cf0b2015-03-26 14:43:45 -07001053var _ ccLibraryInterface = (*CCLibrary)(nil)
1054
Colin Cross97ba0732015-03-23 17:50:24 -07001055func (c *CCLibrary) ccLibrary() *CCLibrary {
1056 return c
Colin Cross3f40fa42015-01-30 17:27:36 -08001057}
1058
Colin Cross97ba0732015-03-23 17:50:24 -07001059func NewCCLibrary(library *CCLibrary, module CCModuleType,
1060 hod common.HostOrDeviceSupported) (blueprint.Module, []interface{}) {
1061
Colin Crossfa138792015-04-24 17:31:52 -07001062 return newCCDynamic(&library.CCLinked, module, hod, common.MultilibBoth,
Colin Cross97ba0732015-03-23 17:50:24 -07001063 &library.LibraryProperties)
1064}
1065
1066func CCLibraryFactory() (blueprint.Module, []interface{}) {
1067 module := &CCLibrary{}
1068
1069 module.LibraryProperties.BuildShared = true
1070 module.LibraryProperties.BuildStatic = true
1071
1072 return NewCCLibrary(module, module, common.HostAndDeviceSupported)
1073}
1074
Colin Cross0676e2d2015-04-24 17:39:18 -07001075func (c *CCLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Crosse11befc2015-04-27 17:49:17 -07001076 depNames = c.CCLinked.depNames(ctx, depNames)
Colin Cross2732e9a2015-04-28 13:23:52 -07001077 if c.static() {
1078 depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.LibraryProperties.Static.Whole_static_libs...)
1079 depNames.StaticLibs = append(depNames.StaticLibs, c.LibraryProperties.Static.Static_libs...)
1080 depNames.SharedLibs = append(depNames.SharedLibs, c.LibraryProperties.Static.Shared_libs...)
1081 } else {
Colin Crossf6566ed2015-03-24 11:13:38 -07001082 if ctx.Device() {
Dan Albertc3144b12015-04-28 18:17:56 -07001083 if c.Properties.Sdk_version == "" {
1084 depNames.CrtBegin = "crtbegin_so"
1085 depNames.CrtEnd = "crtend_so"
1086 } else {
1087 depNames.CrtBegin = "ndk_crtbegin_so." + c.Properties.Sdk_version
1088 depNames.CrtEnd = "ndk_crtend_so." + c.Properties.Sdk_version
1089 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001090 }
Colin Cross2732e9a2015-04-28 13:23:52 -07001091 depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.LibraryProperties.Shared.Whole_static_libs...)
1092 depNames.StaticLibs = append(depNames.StaticLibs, c.LibraryProperties.Shared.Static_libs...)
1093 depNames.SharedLibs = append(depNames.SharedLibs, c.LibraryProperties.Shared.Shared_libs...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001094 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001095
Colin Cross21b9a242015-03-24 14:15:58 -07001096 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -08001097}
1098
Colin Cross97ba0732015-03-23 17:50:24 -07001099func (c *CCLibrary) outputFile() string {
Colin Cross3f40fa42015-01-30 17:27:36 -08001100 return c.out
1101}
1102
Colin Crossed4cf0b2015-03-26 14:43:45 -07001103func (c *CCLibrary) getReuseObjFiles() []string {
1104 return c.reuseObjFiles
1105}
1106
1107func (c *CCLibrary) setReuseFrom(reuseFrom ccLibraryInterface) {
1108 c.reuseFrom = reuseFrom
1109}
1110
1111func (c *CCLibrary) getReuseFrom() ccLibraryInterface {
1112 return c.reuseFrom
1113}
1114
Colin Cross97ba0732015-03-23 17:50:24 -07001115func (c *CCLibrary) allObjFiles() []string {
Colin Cross3f40fa42015-01-30 17:27:36 -08001116 return c.objFiles
1117}
1118
Colin Cross28344522015-04-22 13:07:53 -07001119func (c *CCLibrary) exportedFlags() []string {
1120 return c.exportFlags
Colin Cross3f40fa42015-01-30 17:27:36 -08001121}
1122
Colin Cross0676e2d2015-04-24 17:39:18 -07001123func (c *CCLibrary) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Crosse11befc2015-04-27 17:49:17 -07001124 flags = c.CCLinked.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001125
Colin Cross97ba0732015-03-23 17:50:24 -07001126 flags.CFlags = append(flags.CFlags, "-fPIC")
Colin Cross3f40fa42015-01-30 17:27:36 -08001127
Colin Crossd8e780d2015-04-28 17:39:43 -07001128 if c.static() {
1129 flags.CFlags = append(flags.CFlags, c.LibraryProperties.Static.Cflags...)
1130 } else {
1131 flags.CFlags = append(flags.CFlags, c.LibraryProperties.Shared.Cflags...)
1132 }
1133
Colin Cross18b6dc52015-04-28 13:20:37 -07001134 if !c.static() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001135 libName := ctx.ModuleName()
1136 // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead
1137 sharedFlag := "-Wl,-shared"
Dan Willemsendd0e2c32015-10-20 14:29:35 -07001138 if flags.Clang || ctx.Host() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001139 sharedFlag = "-shared"
1140 }
Colin Crossf6566ed2015-03-24 11:13:38 -07001141 if ctx.Device() {
Colin Cross97ba0732015-03-23 17:50:24 -07001142 flags.LdFlags = append(flags.LdFlags, "-nostdlib")
Colin Cross3f40fa42015-01-30 17:27:36 -08001143 }
Colin Cross97ba0732015-03-23 17:50:24 -07001144
Colin Cross0af4b842015-04-30 16:36:18 -07001145 if ctx.Darwin() {
1146 flags.LdFlags = append(flags.LdFlags,
1147 "-dynamiclib",
1148 "-single_module",
1149 //"-read_only_relocs suppress",
1150 "-install_name @rpath/"+libName+sharedLibraryExtension,
1151 )
1152 } else {
1153 flags.LdFlags = append(flags.LdFlags,
1154 "-Wl,--gc-sections",
1155 sharedFlag,
1156 "-Wl,-soname,"+libName+sharedLibraryExtension,
1157 )
1158 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001159 }
Colin Cross97ba0732015-03-23 17:50:24 -07001160
1161 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001162}
1163
Colin Cross97ba0732015-03-23 17:50:24 -07001164func (c *CCLibrary) compileStaticLibrary(ctx common.AndroidModuleContext,
1165 flags CCFlags, deps CCDeps, objFiles []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001166
1167 staticFlags := flags
Colin Cross581c1892015-04-07 16:50:10 -07001168 objFilesStatic := c.customCompileObjs(ctx, staticFlags, common.DeviceStaticLibrary,
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001169 c.LibraryProperties.Static.Srcs, c.LibraryProperties.Static.Exclude_srcs)
Colin Cross3f40fa42015-01-30 17:27:36 -08001170
1171 objFiles = append(objFiles, objFilesStatic...)
Colin Cross21b9a242015-03-24 14:15:58 -07001172 objFiles = append(objFiles, deps.WholeStaticLibObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001173
1174 outputFile := filepath.Join(common.ModuleOutDir(ctx), ctx.ModuleName()+staticLibraryExtension)
1175
Colin Cross0af4b842015-04-30 16:36:18 -07001176 if ctx.Darwin() {
1177 TransformDarwinObjToStaticLib(ctx, objFiles, ccFlagsToBuilderFlags(flags), outputFile)
1178 } else {
1179 TransformObjToStaticLib(ctx, objFiles, ccFlagsToBuilderFlags(flags), outputFile)
1180 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001181
1182 c.objFiles = objFiles
1183 c.out = outputFile
Colin Crossf2298272015-05-12 11:36:53 -07001184
1185 common.CheckModuleSrcDirsExist(ctx, c.Properties.Export_include_dirs, "export_include_dirs")
Colin Crossfa138792015-04-24 17:31:52 -07001186 includeDirs := pathtools.PrefixPaths(c.Properties.Export_include_dirs, common.ModuleSrcDir(ctx))
Colin Cross28344522015-04-22 13:07:53 -07001187 c.exportFlags = []string{includeDirsToFlags(includeDirs)}
Colin Cross3f40fa42015-01-30 17:27:36 -08001188
1189 ctx.CheckbuildFile(outputFile)
1190}
1191
Colin Cross97ba0732015-03-23 17:50:24 -07001192func (c *CCLibrary) compileSharedLibrary(ctx common.AndroidModuleContext,
1193 flags CCFlags, deps CCDeps, objFiles []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001194
1195 sharedFlags := flags
Colin Cross581c1892015-04-07 16:50:10 -07001196 objFilesShared := c.customCompileObjs(ctx, sharedFlags, common.DeviceSharedLibrary,
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001197 c.LibraryProperties.Shared.Srcs, c.LibraryProperties.Shared.Exclude_srcs)
Colin Cross3f40fa42015-01-30 17:27:36 -08001198
1199 objFiles = append(objFiles, objFilesShared...)
1200
1201 outputFile := filepath.Join(common.ModuleOutDir(ctx), ctx.ModuleName()+sharedLibraryExtension)
1202
Colin Crossaee540a2015-07-06 17:48:31 -07001203 var linkerDeps []string
1204
1205 if c.LibraryProperties.Version_script != "" {
1206 versionScript := filepath.Join(common.ModuleSrcDir(ctx), c.LibraryProperties.Version_script)
1207 sharedFlags.LdFlags = append(sharedFlags.LdFlags, "-Wl,--version-script,"+versionScript)
1208 linkerDeps = append(linkerDeps, versionScript)
1209 }
1210
Colin Cross97ba0732015-03-23 17:50:24 -07001211 TransformObjToDynamicBinary(ctx, objFiles, deps.SharedLibs, deps.StaticLibs,
Colin Crossaee540a2015-07-06 17:48:31 -07001212 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, false,
Colin Cross77b00fa2015-03-16 16:15:49 -07001213 ccFlagsToBuilderFlags(flags), outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08001214
1215 c.out = outputFile
Colin Crossfa138792015-04-24 17:31:52 -07001216 includeDirs := pathtools.PrefixPaths(c.Properties.Export_include_dirs, common.ModuleSrcDir(ctx))
Colin Cross28344522015-04-22 13:07:53 -07001217 c.exportFlags = []string{includeDirsToFlags(includeDirs)}
Colin Cross3f40fa42015-01-30 17:27:36 -08001218}
1219
Colin Cross97ba0732015-03-23 17:50:24 -07001220func (c *CCLibrary) compileModule(ctx common.AndroidModuleContext,
1221 flags CCFlags, deps CCDeps, objFiles []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001222
1223 // Reuse the object files from the matching static library if it exists
Colin Crossed4cf0b2015-03-26 14:43:45 -07001224 if c.getReuseFrom().ccLibrary() == c {
1225 c.reuseObjFiles = objFiles
Colin Cross3f40fa42015-01-30 17:27:36 -08001226 } else {
Colin Cross2732e9a2015-04-28 13:23:52 -07001227 if c.getReuseFrom().ccLibrary().LibraryProperties.Static.Cflags == nil &&
1228 c.LibraryProperties.Shared.Cflags == nil {
1229 objFiles = append([]string(nil), c.getReuseFrom().getReuseObjFiles()...)
1230 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001231 }
1232
Colin Crossed4cf0b2015-03-26 14:43:45 -07001233 if c.static() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001234 c.compileStaticLibrary(ctx, flags, deps, objFiles)
1235 } else {
1236 c.compileSharedLibrary(ctx, flags, deps, objFiles)
1237 }
1238}
1239
Colin Cross97ba0732015-03-23 17:50:24 -07001240func (c *CCLibrary) installStaticLibrary(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001241 // Static libraries do not get installed.
1242}
1243
Colin Cross97ba0732015-03-23 17:50:24 -07001244func (c *CCLibrary) installSharedLibrary(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001245 installDir := "lib"
Colin Cross97ba0732015-03-23 17:50:24 -07001246 if flags.Toolchain.Is64Bit() {
Dan Albertc403f7c2015-03-18 14:01:18 -07001247 installDir = "lib64"
1248 }
1249
Colin Crossfa138792015-04-24 17:31:52 -07001250 ctx.InstallFile(filepath.Join(installDir, c.Properties.Relative_install_path), c.out)
Dan Albertc403f7c2015-03-18 14:01:18 -07001251}
1252
Colin Cross97ba0732015-03-23 17:50:24 -07001253func (c *CCLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001254 if c.static() {
Dan Albertc403f7c2015-03-18 14:01:18 -07001255 c.installStaticLibrary(ctx, flags)
1256 } else {
1257 c.installSharedLibrary(ctx, flags)
1258 }
1259}
1260
Colin Cross3f40fa42015-01-30 17:27:36 -08001261//
1262// Objects (for crt*.o)
1263//
1264
Dan Albertc3144b12015-04-28 18:17:56 -07001265type ccObjectProvider interface {
1266 object() *ccObject
1267}
1268
Colin Cross3f40fa42015-01-30 17:27:36 -08001269type ccObject struct {
Colin Crossfa138792015-04-24 17:31:52 -07001270 CCBase
Colin Cross3f40fa42015-01-30 17:27:36 -08001271 out string
1272}
1273
Dan Albertc3144b12015-04-28 18:17:56 -07001274func (c *ccObject) object() *ccObject {
1275 return c
1276}
1277
Colin Cross97ba0732015-03-23 17:50:24 -07001278func CCObjectFactory() (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001279 module := &ccObject{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001280
Colin Crossfa138792015-04-24 17:31:52 -07001281 return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth)
Colin Cross3f40fa42015-01-30 17:27:36 -08001282}
1283
1284func (*ccObject) AndroidDynamicDependencies(ctx common.AndroidDynamicDependerModuleContext) []string {
1285 // object files can't have any dynamic dependencies
1286 return nil
1287}
1288
Colin Cross0676e2d2015-04-24 17:39:18 -07001289func (*ccObject) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Cross21b9a242015-03-24 14:15:58 -07001290 // object files can't have any dynamic dependencies
1291 return CCDeps{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001292}
1293
1294func (c *ccObject) compileModule(ctx common.AndroidModuleContext,
Colin Cross97ba0732015-03-23 17:50:24 -07001295 flags CCFlags, deps CCDeps, objFiles []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001296
Colin Cross97ba0732015-03-23 17:50:24 -07001297 objFiles = append(objFiles, deps.ObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001298
1299 var outputFile string
1300 if len(objFiles) == 1 {
1301 outputFile = objFiles[0]
1302 } else {
Dan Albertc3144b12015-04-28 18:17:56 -07001303 outputFile = filepath.Join(common.ModuleOutDir(ctx), ctx.ModuleName()+objectExtension)
Colin Cross3f40fa42015-01-30 17:27:36 -08001304 TransformObjsToObj(ctx, objFiles, ccFlagsToBuilderFlags(flags), outputFile)
1305 }
1306
1307 c.out = outputFile
1308
1309 ctx.CheckbuildFile(outputFile)
1310}
1311
Colin Cross97ba0732015-03-23 17:50:24 -07001312func (c *ccObject) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001313 // Object files do not get installed.
1314}
1315
Colin Cross3f40fa42015-01-30 17:27:36 -08001316func (c *ccObject) outputFile() string {
1317 return c.out
1318}
1319
Dan Albertc3144b12015-04-28 18:17:56 -07001320var _ ccObjectProvider = (*ccObject)(nil)
1321
Colin Cross3f40fa42015-01-30 17:27:36 -08001322//
1323// Executables
1324//
1325
Colin Cross7d5136f2015-05-11 13:39:40 -07001326type CCBinaryProperties struct {
1327 // compile executable with -static
1328 Static_executable bool
1329
1330 // set the name of the output
1331 Stem string `android:"arch_variant"`
1332
1333 // append to the name of the output
1334 Suffix string `android:"arch_variant"`
1335
1336 // if set, add an extra objcopy --prefix-symbols= step
1337 Prefix_symbols string
Colin Cross6002e052015-09-16 16:00:08 -07001338
1339 // Create a separate binary for each source file. Useful when there is
1340 // global state that can not be torn down and reset between each test suite.
1341 Test_per_src bool
Colin Cross7d5136f2015-05-11 13:39:40 -07001342}
1343
Colin Cross97ba0732015-03-23 17:50:24 -07001344type CCBinary struct {
Colin Crossfa138792015-04-24 17:31:52 -07001345 CCLinked
Dan Albertc403f7c2015-03-18 14:01:18 -07001346 out string
Colin Crossd350ecd2015-04-28 13:25:36 -07001347 installFile string
Colin Cross7d5136f2015-05-11 13:39:40 -07001348 BinaryProperties CCBinaryProperties
Colin Cross3f40fa42015-01-30 17:27:36 -08001349}
1350
Colin Crossed4cf0b2015-03-26 14:43:45 -07001351func (c *CCBinary) buildStatic() bool {
1352 return c.BinaryProperties.Static_executable
1353}
1354
1355func (c *CCBinary) buildShared() bool {
1356 return !c.BinaryProperties.Static_executable
1357}
1358
Colin Cross97ba0732015-03-23 17:50:24 -07001359func (c *CCBinary) getStem(ctx common.AndroidModuleContext) string {
Colin Cross4ae185c2015-03-26 15:12:10 -07001360 stem := ctx.ModuleName()
Colin Cross97ba0732015-03-23 17:50:24 -07001361 if c.BinaryProperties.Stem != "" {
Colin Cross4ae185c2015-03-26 15:12:10 -07001362 stem = c.BinaryProperties.Stem
Colin Cross3f40fa42015-01-30 17:27:36 -08001363 }
Colin Cross4ae185c2015-03-26 15:12:10 -07001364
1365 return stem + c.BinaryProperties.Suffix
Colin Cross3f40fa42015-01-30 17:27:36 -08001366}
1367
Colin Cross0676e2d2015-04-24 17:39:18 -07001368func (c *CCBinary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Crosse11befc2015-04-27 17:49:17 -07001369 depNames = c.CCLinked.depNames(ctx, depNames)
Colin Crossf6566ed2015-03-24 11:13:38 -07001370 if ctx.Device() {
Dan Albertc3144b12015-04-28 18:17:56 -07001371 if c.Properties.Sdk_version == "" {
1372 if c.BinaryProperties.Static_executable {
1373 depNames.CrtBegin = "crtbegin_static"
1374 } else {
1375 depNames.CrtBegin = "crtbegin_dynamic"
1376 }
1377 depNames.CrtEnd = "crtend_android"
Colin Cross3f40fa42015-01-30 17:27:36 -08001378 } else {
Dan Albertc3144b12015-04-28 18:17:56 -07001379 if c.BinaryProperties.Static_executable {
1380 depNames.CrtBegin = "ndk_crtbegin_static." + c.Properties.Sdk_version
1381 } else {
1382 depNames.CrtBegin = "ndk_crtbegin_dynamic." + c.Properties.Sdk_version
1383 }
1384 depNames.CrtEnd = "ndk_crtend_android." + c.Properties.Sdk_version
Colin Cross3f40fa42015-01-30 17:27:36 -08001385 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001386
1387 if c.BinaryProperties.Static_executable {
Colin Cross74d1ec02015-04-28 13:30:13 -07001388 if c.stl(ctx) == "libc++_static" {
1389 depNames.StaticLibs = append(depNames.StaticLibs, "libm", "libc", "libdl")
1390 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001391 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
1392 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
1393 // move them to the beginning of deps.LateStaticLibs
1394 var groupLibs []string
1395 depNames.StaticLibs, groupLibs = filterList(depNames.StaticLibs,
1396 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
1397 depNames.LateStaticLibs = append(groupLibs, depNames.LateStaticLibs...)
1398 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001399 }
Colin Cross21b9a242015-03-24 14:15:58 -07001400 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -08001401}
1402
Colin Cross97ba0732015-03-23 17:50:24 -07001403func NewCCBinary(binary *CCBinary, module CCModuleType,
Colin Cross1f8f2342015-03-26 16:09:47 -07001404 hod common.HostOrDeviceSupported, props ...interface{}) (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001405
Colin Cross1f8f2342015-03-26 16:09:47 -07001406 props = append(props, &binary.BinaryProperties)
1407
Colin Crossfa138792015-04-24 17:31:52 -07001408 return newCCDynamic(&binary.CCLinked, module, hod, common.MultilibFirst, props...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001409}
1410
Colin Cross97ba0732015-03-23 17:50:24 -07001411func CCBinaryFactory() (blueprint.Module, []interface{}) {
1412 module := &CCBinary{}
1413
1414 return NewCCBinary(module, module, common.HostAndDeviceSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001415}
1416
Colin Cross18b6dc52015-04-28 13:20:37 -07001417func (c *CCBinary) ModifyProperties(ctx common.AndroidBaseContext) {
Colin Cross0af4b842015-04-30 16:36:18 -07001418 if ctx.Darwin() {
1419 c.BinaryProperties.Static_executable = false
1420 }
Colin Cross18b6dc52015-04-28 13:20:37 -07001421 if c.BinaryProperties.Static_executable {
1422 c.dynamicProperties.VariantIsStaticBinary = true
1423 }
1424}
1425
Colin Cross0676e2d2015-04-24 17:39:18 -07001426func (c *CCBinary) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Crosse11befc2015-04-27 17:49:17 -07001427 flags = c.CCLinked.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001428
Colin Cross97ba0732015-03-23 17:50:24 -07001429 flags.CFlags = append(flags.CFlags, "-fpie")
1430
Colin Crossf6566ed2015-03-24 11:13:38 -07001431 if ctx.Device() {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001432 if c.BinaryProperties.Static_executable {
1433 // Clang driver needs -static to create static executable.
1434 // However, bionic/linker uses -shared to overwrite.
1435 // Linker for x86 targets does not allow coexistance of -static and -shared,
1436 // so we add -static only if -shared is not used.
1437 if !inList("-shared", flags.LdFlags) {
1438 flags.LdFlags = append(flags.LdFlags, "-static")
1439 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001440
Colin Crossed4cf0b2015-03-26 14:43:45 -07001441 flags.LdFlags = append(flags.LdFlags,
1442 "-nostdlib",
1443 "-Bstatic",
1444 "-Wl,--gc-sections",
1445 )
1446
1447 } else {
1448 linker := "/system/bin/linker"
1449 if flags.Toolchain.Is64Bit() {
1450 linker = "/system/bin/linker64"
1451 }
1452
1453 flags.LdFlags = append(flags.LdFlags,
1454 "-nostdlib",
1455 "-Bdynamic",
1456 fmt.Sprintf("-Wl,-dynamic-linker,%s", linker),
1457 "-Wl,--gc-sections",
1458 "-Wl,-z,nocopyreloc",
1459 )
1460 }
Colin Cross0af4b842015-04-30 16:36:18 -07001461 } else if ctx.Darwin() {
1462 flags.LdFlags = append(flags.LdFlags, "-Wl,-headerpad_max_install_names")
Colin Cross3f40fa42015-01-30 17:27:36 -08001463 }
1464
Colin Cross97ba0732015-03-23 17:50:24 -07001465 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001466}
1467
Colin Cross97ba0732015-03-23 17:50:24 -07001468func (c *CCBinary) compileModule(ctx common.AndroidModuleContext,
1469 flags CCFlags, deps CCDeps, objFiles []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001470
Colin Crossfa138792015-04-24 17:31:52 -07001471 if !c.BinaryProperties.Static_executable && inList("libc", c.Properties.Static_libs) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001472 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
1473 "from static libs or set static_executable: true")
1474 }
1475
1476 outputFile := filepath.Join(common.ModuleOutDir(ctx), c.getStem(ctx))
Dan Albertc403f7c2015-03-18 14:01:18 -07001477 c.out = outputFile
Colin Crossbfae8852015-03-26 14:44:11 -07001478 if c.BinaryProperties.Prefix_symbols != "" {
1479 afterPrefixSymbols := outputFile
1480 outputFile = outputFile + ".intermediate"
1481 TransformBinaryPrefixSymbols(ctx, c.BinaryProperties.Prefix_symbols, outputFile,
1482 ccFlagsToBuilderFlags(flags), afterPrefixSymbols)
1483 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001484
Colin Crossaee540a2015-07-06 17:48:31 -07001485 var linkerDeps []string
1486
Colin Cross97ba0732015-03-23 17:50:24 -07001487 TransformObjToDynamicBinary(ctx, objFiles, deps.SharedLibs, deps.StaticLibs,
Colin Crossaee540a2015-07-06 17:48:31 -07001488 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
Colin Cross77b00fa2015-03-16 16:15:49 -07001489 ccFlagsToBuilderFlags(flags), outputFile)
Dan Albertc403f7c2015-03-18 14:01:18 -07001490}
Colin Cross3f40fa42015-01-30 17:27:36 -08001491
Colin Cross97ba0732015-03-23 17:50:24 -07001492func (c *CCBinary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Colin Crossd350ecd2015-04-28 13:25:36 -07001493 c.installFile = ctx.InstallFile(filepath.Join("bin", c.Properties.Relative_install_path), c.out)
1494}
1495
1496func (c *CCBinary) HostToolPath() string {
1497 if c.HostOrDevice().Host() {
1498 return c.installFile
1499 }
1500 return ""
Dan Albertc403f7c2015-03-18 14:01:18 -07001501}
1502
Colin Cross6002e052015-09-16 16:00:08 -07001503func (c *CCBinary) testPerSrc() bool {
1504 return c.BinaryProperties.Test_per_src
1505}
1506
1507func (c *CCBinary) binary() *CCBinary {
1508 return c
1509}
1510
1511type testPerSrc interface {
1512 binary() *CCBinary
1513 testPerSrc() bool
1514}
1515
1516var _ testPerSrc = (*CCBinary)(nil)
1517
1518func TestPerSrcMutator(mctx blueprint.EarlyMutatorContext) {
1519 if test, ok := mctx.Module().(testPerSrc); ok {
1520 if test.testPerSrc() {
1521 testNames := make([]string, len(test.binary().Properties.Srcs))
1522 for i, src := range test.binary().Properties.Srcs {
1523 testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
1524 }
1525 tests := mctx.CreateLocalVariations(testNames...)
1526 for i, src := range test.binary().Properties.Srcs {
1527 tests[i].(testPerSrc).binary().Properties.Srcs = []string{src}
1528 tests[i].(testPerSrc).binary().BinaryProperties.Stem = mctx.ModuleName() + "_" + testNames[i]
1529 }
1530 }
1531 }
Colin Cross7d5136f2015-05-11 13:39:40 -07001532}
1533
Colin Cross9ffb4f52015-04-24 17:48:09 -07001534type CCTest struct {
Colin Cross97ba0732015-03-23 17:50:24 -07001535 CCBinary
Dan Albertc403f7c2015-03-18 14:01:18 -07001536}
1537
Colin Cross9ffb4f52015-04-24 17:48:09 -07001538func (c *CCTest) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Cross0676e2d2015-04-24 17:39:18 -07001539 flags = c.CCBinary.flags(ctx, flags)
Dan Albertc403f7c2015-03-18 14:01:18 -07001540
Colin Cross97ba0732015-03-23 17:50:24 -07001541 flags.CFlags = append(flags.CFlags, "-DGTEST_HAS_STD_STRING")
Colin Crossf6566ed2015-03-24 11:13:38 -07001542 if ctx.Host() {
Colin Cross97ba0732015-03-23 17:50:24 -07001543 flags.CFlags = append(flags.CFlags, "-O0", "-g")
Colin Cross28344522015-04-22 13:07:53 -07001544 flags.LdFlags = append(flags.LdFlags, "-lpthread")
Dan Albertc403f7c2015-03-18 14:01:18 -07001545 }
1546
1547 // TODO(danalbert): Make gtest export its dependencies.
Colin Cross28344522015-04-22 13:07:53 -07001548 flags.CFlags = append(flags.CFlags,
1549 "-I"+filepath.Join(ctx.AConfig().SrcDir(), "external/gtest/include"))
Dan Albertc403f7c2015-03-18 14:01:18 -07001550
Colin Cross21b9a242015-03-24 14:15:58 -07001551 return flags
Dan Albertc403f7c2015-03-18 14:01:18 -07001552}
1553
Colin Cross9ffb4f52015-04-24 17:48:09 -07001554func (c *CCTest) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Dan Willemsene6540452015-10-20 15:21:33 -07001555 depNames.StaticLibs = append(depNames.StaticLibs, "libgtest_main", "libgtest")
Colin Crossa8a93d32015-04-28 13:26:49 -07001556 depNames = c.CCBinary.depNames(ctx, depNames)
Colin Cross21b9a242015-03-24 14:15:58 -07001557 return depNames
Dan Albertc403f7c2015-03-18 14:01:18 -07001558}
1559
Colin Cross9ffb4f52015-04-24 17:48:09 -07001560func (c *CCTest) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Colin Crossf6566ed2015-03-24 11:13:38 -07001561 if ctx.Device() {
Colin Crossa8a93d32015-04-28 13:26:49 -07001562 ctx.InstallFile("../data/nativetest"+ctx.Arch().ArchType.Multilib[3:]+"/"+ctx.ModuleName(), c.out)
Dan Albertc403f7c2015-03-18 14:01:18 -07001563 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001564 c.CCBinary.installModule(ctx, flags)
Dan Albertc403f7c2015-03-18 14:01:18 -07001565 }
1566}
1567
Colin Cross9ffb4f52015-04-24 17:48:09 -07001568func NewCCTest(test *CCTest, module CCModuleType,
1569 hod common.HostOrDeviceSupported, props ...interface{}) (blueprint.Module, []interface{}) {
1570
Colin Cross9ffb4f52015-04-24 17:48:09 -07001571 return NewCCBinary(&test.CCBinary, module, hod, props...)
1572}
1573
1574func CCTestFactory() (blueprint.Module, []interface{}) {
1575 module := &CCTest{}
1576
1577 return NewCCTest(module, module, common.HostAndDeviceSupported)
1578}
1579
Colin Cross2ba19d92015-05-07 15:44:20 -07001580type CCBenchmark struct {
1581 CCBinary
1582}
1583
1584func (c *CCBenchmark) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
1585 depNames = c.CCBinary.depNames(ctx, depNames)
Dan Willemsenf8e98b02015-09-11 17:41:44 -07001586 depNames.StaticLibs = append(depNames.StaticLibs, "libbenchmark", "libbase")
Colin Cross2ba19d92015-05-07 15:44:20 -07001587 return depNames
1588}
1589
1590func (c *CCBenchmark) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
1591 if ctx.Device() {
1592 ctx.InstallFile("../data/nativetest"+ctx.Arch().ArchType.Multilib[3:]+"/"+ctx.ModuleName(), c.out)
1593 } else {
1594 c.CCBinary.installModule(ctx, flags)
1595 }
1596}
1597
1598func NewCCBenchmark(test *CCBenchmark, module CCModuleType,
1599 hod common.HostOrDeviceSupported, props ...interface{}) (blueprint.Module, []interface{}) {
1600
1601 return NewCCBinary(&test.CCBinary, module, hod, props...)
1602}
1603
1604func CCBenchmarkFactory() (blueprint.Module, []interface{}) {
1605 module := &CCBenchmark{}
1606
1607 return NewCCBenchmark(module, module, common.HostAndDeviceSupported)
1608}
1609
Colin Cross3f40fa42015-01-30 17:27:36 -08001610//
1611// Static library
1612//
1613
Colin Cross97ba0732015-03-23 17:50:24 -07001614func CCLibraryStaticFactory() (blueprint.Module, []interface{}) {
1615 module := &CCLibrary{}
1616 module.LibraryProperties.BuildStatic = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001617
Colin Cross97ba0732015-03-23 17:50:24 -07001618 return NewCCLibrary(module, module, common.HostAndDeviceSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001619}
1620
1621//
1622// Shared libraries
1623//
1624
Colin Cross97ba0732015-03-23 17:50:24 -07001625func CCLibrarySharedFactory() (blueprint.Module, []interface{}) {
1626 module := &CCLibrary{}
1627 module.LibraryProperties.BuildShared = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001628
Colin Cross97ba0732015-03-23 17:50:24 -07001629 return NewCCLibrary(module, module, common.HostAndDeviceSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001630}
1631
1632//
1633// Host static library
1634//
1635
Colin Cross97ba0732015-03-23 17:50:24 -07001636func CCLibraryHostStaticFactory() (blueprint.Module, []interface{}) {
1637 module := &CCLibrary{}
1638 module.LibraryProperties.BuildStatic = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001639
Colin Cross97ba0732015-03-23 17:50:24 -07001640 return NewCCLibrary(module, module, common.HostSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001641}
1642
1643//
1644// Host Shared libraries
1645//
1646
Colin Cross97ba0732015-03-23 17:50:24 -07001647func CCLibraryHostSharedFactory() (blueprint.Module, []interface{}) {
1648 module := &CCLibrary{}
1649 module.LibraryProperties.BuildShared = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001650
Colin Cross97ba0732015-03-23 17:50:24 -07001651 return NewCCLibrary(module, module, common.HostSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001652}
1653
1654//
1655// Host Binaries
1656//
1657
Colin Cross97ba0732015-03-23 17:50:24 -07001658func CCBinaryHostFactory() (blueprint.Module, []interface{}) {
1659 module := &CCBinary{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001660
Colin Cross97ba0732015-03-23 17:50:24 -07001661 return NewCCBinary(module, module, common.HostSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001662}
1663
1664//
Colin Cross1f8f2342015-03-26 16:09:47 -07001665// Host Tests
1666//
1667
1668func CCTestHostFactory() (blueprint.Module, []interface{}) {
Colin Cross9ffb4f52015-04-24 17:48:09 -07001669 module := &CCTest{}
Colin Cross6002e052015-09-16 16:00:08 -07001670 return NewCCBinary(&module.CCBinary, module, common.HostSupported)
Colin Cross1f8f2342015-03-26 16:09:47 -07001671}
1672
1673//
Colin Cross2ba19d92015-05-07 15:44:20 -07001674// Host Benchmarks
1675//
1676
1677func CCBenchmarkHostFactory() (blueprint.Module, []interface{}) {
1678 module := &CCBenchmark{}
1679 return NewCCBinary(&module.CCBinary, module, common.HostSupported)
1680}
1681
1682//
Colin Cross3f40fa42015-01-30 17:27:36 -08001683// Device libraries shipped with gcc
1684//
1685
1686type toolchainLibrary struct {
Colin Cross97ba0732015-03-23 17:50:24 -07001687 CCLibrary
Colin Cross3f40fa42015-01-30 17:27:36 -08001688}
1689
1690func (*toolchainLibrary) AndroidDynamicDependencies(ctx common.AndroidDynamicDependerModuleContext) []string {
1691 // toolchain libraries can't have any dependencies
1692 return nil
1693}
1694
Colin Cross0676e2d2015-04-24 17:39:18 -07001695func (*toolchainLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Cross3f40fa42015-01-30 17:27:36 -08001696 // toolchain libraries can't have any dependencies
Colin Cross21b9a242015-03-24 14:15:58 -07001697 return CCDeps{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001698}
1699
Colin Cross97ba0732015-03-23 17:50:24 -07001700func ToolchainLibraryFactory() (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001701 module := &toolchainLibrary{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001702
Colin Cross97ba0732015-03-23 17:50:24 -07001703 module.LibraryProperties.BuildStatic = true
1704
Colin Crossfa138792015-04-24 17:31:52 -07001705 return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth,
Colin Cross21b9a242015-03-24 14:15:58 -07001706 &module.LibraryProperties)
Colin Cross3f40fa42015-01-30 17:27:36 -08001707}
1708
1709func (c *toolchainLibrary) compileModule(ctx common.AndroidModuleContext,
Colin Cross97ba0732015-03-23 17:50:24 -07001710 flags CCFlags, deps CCDeps, objFiles []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001711
1712 libName := ctx.ModuleName() + staticLibraryExtension
1713 outputFile := filepath.Join(common.ModuleOutDir(ctx), libName)
1714
1715 CopyGccLib(ctx, libName, ccFlagsToBuilderFlags(flags), outputFile)
1716
1717 c.out = outputFile
1718
1719 ctx.CheckbuildFile(outputFile)
1720}
1721
Colin Cross97ba0732015-03-23 17:50:24 -07001722func (c *toolchainLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001723 // Toolchain libraries do not get installed.
1724}
1725
Dan Albertbe961682015-03-18 23:38:50 -07001726// NDK prebuilt libraries.
1727//
1728// These differ from regular prebuilts in that they aren't stripped and usually aren't installed
1729// either (with the exception of the shared STLs, which are installed to the app's directory rather
1730// than to the system image).
1731
1732func getNdkLibDir(ctx common.AndroidModuleContext, toolchain Toolchain, version string) string {
1733 return fmt.Sprintf("%s/prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/lib",
Colin Cross1332b002015-04-07 17:11:30 -07001734 ctx.AConfig().SrcDir(), version, toolchain.Name())
Dan Albertbe961682015-03-18 23:38:50 -07001735}
1736
Dan Albertc3144b12015-04-28 18:17:56 -07001737func ndkPrebuiltModuleToPath(ctx common.AndroidModuleContext, toolchain Toolchain,
1738 ext string, version string) string {
1739
1740 // NDK prebuilts are named like: ndk_NAME.EXT.SDK_VERSION.
1741 // We want to translate to just NAME.EXT
1742 name := strings.Split(strings.TrimPrefix(ctx.ModuleName(), "ndk_"), ".")[0]
1743 dir := getNdkLibDir(ctx, toolchain, version)
1744 return filepath.Join(dir, name+ext)
1745}
1746
1747type ndkPrebuiltObject struct {
1748 ccObject
1749}
1750
1751func (*ndkPrebuiltObject) AndroidDynamicDependencies(
1752 ctx common.AndroidDynamicDependerModuleContext) []string {
1753
1754 // NDK objects can't have any dependencies
1755 return nil
1756}
1757
1758func (*ndkPrebuiltObject) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
1759 // NDK objects can't have any dependencies
1760 return CCDeps{}
1761}
1762
1763func NdkPrebuiltObjectFactory() (blueprint.Module, []interface{}) {
1764 module := &ndkPrebuiltObject{}
1765 return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth)
1766}
1767
1768func (c *ndkPrebuiltObject) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
1769 deps CCDeps, objFiles []string) {
1770 // A null build step, but it sets up the output path.
1771 if !strings.HasPrefix(ctx.ModuleName(), "ndk_crt") {
1772 ctx.ModuleErrorf("NDK prebuilts must have an ndk_crt prefixed name")
1773 }
1774
1775 c.out = ndkPrebuiltModuleToPath(ctx, flags.Toolchain, objectExtension, c.Properties.Sdk_version)
1776}
1777
1778func (c *ndkPrebuiltObject) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
1779 // Objects do not get installed.
1780}
1781
1782var _ ccObjectProvider = (*ndkPrebuiltObject)(nil)
1783
Dan Albertbe961682015-03-18 23:38:50 -07001784type ndkPrebuiltLibrary struct {
1785 CCLibrary
1786}
1787
1788func (*ndkPrebuiltLibrary) AndroidDynamicDependencies(
1789 ctx common.AndroidDynamicDependerModuleContext) []string {
1790
1791 // NDK libraries can't have any dependencies
1792 return nil
1793}
1794
Colin Cross0676e2d2015-04-24 17:39:18 -07001795func (*ndkPrebuiltLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Dan Albertbe961682015-03-18 23:38:50 -07001796 // NDK libraries can't have any dependencies
1797 return CCDeps{}
1798}
1799
1800func NdkPrebuiltLibraryFactory() (blueprint.Module, []interface{}) {
1801 module := &ndkPrebuiltLibrary{}
1802 module.LibraryProperties.BuildShared = true
1803 return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
1804}
1805
1806func (c *ndkPrebuiltLibrary) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
1807 deps CCDeps, objFiles []string) {
1808 // A null build step, but it sets up the output path.
1809 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
1810 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
1811 }
1812
Colin Crossfa138792015-04-24 17:31:52 -07001813 includeDirs := pathtools.PrefixPaths(c.Properties.Export_include_dirs, common.ModuleSrcDir(ctx))
Colin Cross28344522015-04-22 13:07:53 -07001814 c.exportFlags = []string{common.JoinWithPrefix(includeDirs, "-isystem ")}
Dan Albertbe961682015-03-18 23:38:50 -07001815
Dan Albertc3144b12015-04-28 18:17:56 -07001816 c.out = ndkPrebuiltModuleToPath(ctx, flags.Toolchain, sharedLibraryExtension,
1817 c.Properties.Sdk_version)
Dan Albertbe961682015-03-18 23:38:50 -07001818}
1819
1820func (c *ndkPrebuiltLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc3144b12015-04-28 18:17:56 -07001821 // NDK prebuilt libraries do not get installed.
Dan Albertbe961682015-03-18 23:38:50 -07001822}
1823
1824// The NDK STLs are slightly different from the prebuilt system libraries:
1825// * Are not specific to each platform version.
1826// * The libraries are not in a predictable location for each STL.
1827
1828type ndkPrebuiltStl struct {
1829 ndkPrebuiltLibrary
1830}
1831
1832type ndkPrebuiltStaticStl struct {
1833 ndkPrebuiltStl
1834}
1835
1836type ndkPrebuiltSharedStl struct {
1837 ndkPrebuiltStl
1838}
1839
1840func NdkPrebuiltSharedStlFactory() (blueprint.Module, []interface{}) {
1841 module := &ndkPrebuiltSharedStl{}
1842 module.LibraryProperties.BuildShared = true
1843 return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
1844}
1845
1846func NdkPrebuiltStaticStlFactory() (blueprint.Module, []interface{}) {
1847 module := &ndkPrebuiltStaticStl{}
1848 module.LibraryProperties.BuildStatic = true
1849 return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
1850}
1851
1852func getNdkStlLibDir(ctx common.AndroidModuleContext, toolchain Toolchain, stl string) string {
1853 gccVersion := toolchain.GccVersion()
1854 var libDir string
1855 switch stl {
1856 case "libstlport":
1857 libDir = "cxx-stl/stlport/libs"
1858 case "libc++":
1859 libDir = "cxx-stl/llvm-libc++/libs"
1860 case "libgnustl":
1861 libDir = fmt.Sprintf("cxx-stl/gnu-libstdc++/%s/libs", gccVersion)
1862 }
1863
1864 if libDir != "" {
Colin Cross1332b002015-04-07 17:11:30 -07001865 ndkSrcRoot := ctx.AConfig().SrcDir() + "/prebuilts/ndk/current/sources"
Dan Albertbe961682015-03-18 23:38:50 -07001866 return fmt.Sprintf("%s/%s/%s", ndkSrcRoot, libDir, ctx.Arch().Abi)
1867 }
1868
1869 ctx.ModuleErrorf("Unknown NDK STL: %s", stl)
1870 return ""
1871}
1872
1873func (c *ndkPrebuiltStl) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
1874 deps CCDeps, objFiles []string) {
1875 // A null build step, but it sets up the output path.
1876 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
1877 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
1878 }
1879
Colin Crossfa138792015-04-24 17:31:52 -07001880 includeDirs := pathtools.PrefixPaths(c.Properties.Export_include_dirs, common.ModuleSrcDir(ctx))
Colin Cross28344522015-04-22 13:07:53 -07001881 c.exportFlags = []string{includeDirsToFlags(includeDirs)}
Dan Albertbe961682015-03-18 23:38:50 -07001882
1883 libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_")
1884 libExt := sharedLibraryExtension
1885 if c.LibraryProperties.BuildStatic {
1886 libExt = staticLibraryExtension
1887 }
1888
1889 stlName := strings.TrimSuffix(libName, "_shared")
1890 stlName = strings.TrimSuffix(stlName, "_static")
1891 libDir := getNdkStlLibDir(ctx, flags.Toolchain, stlName)
1892 c.out = libDir + "/" + libName + libExt
1893}
1894
Colin Cross3f40fa42015-01-30 17:27:36 -08001895func LinkageMutator(mctx blueprint.EarlyMutatorContext) {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001896 if c, ok := mctx.Module().(ccLinkedInterface); ok {
Colin Cross3f40fa42015-01-30 17:27:36 -08001897 var modules []blueprint.Module
Colin Crossed4cf0b2015-03-26 14:43:45 -07001898 if c.buildStatic() && c.buildShared() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001899 modules = mctx.CreateLocalVariations("static", "shared")
Colin Cross18b6dc52015-04-28 13:20:37 -07001900 modules[0].(ccLinkedInterface).setStatic(true)
1901 modules[1].(ccLinkedInterface).setStatic(false)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001902 } else if c.buildStatic() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001903 modules = mctx.CreateLocalVariations("static")
Colin Cross18b6dc52015-04-28 13:20:37 -07001904 modules[0].(ccLinkedInterface).setStatic(true)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001905 } else if c.buildShared() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001906 modules = mctx.CreateLocalVariations("shared")
Colin Cross18b6dc52015-04-28 13:20:37 -07001907 modules[0].(ccLinkedInterface).setStatic(false)
Colin Cross3f40fa42015-01-30 17:27:36 -08001908 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001909 panic(fmt.Errorf("ccLibrary %q not static or shared", mctx.ModuleName()))
Colin Cross3f40fa42015-01-30 17:27:36 -08001910 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001911
1912 if _, ok := c.(ccLibraryInterface); ok {
1913 reuseFrom := modules[0].(ccLibraryInterface)
1914 for _, m := range modules {
1915 m.(ccLibraryInterface).setReuseFrom(reuseFrom)
Colin Cross3f40fa42015-01-30 17:27:36 -08001916 }
1917 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001918 }
1919}
Colin Cross74d1ec02015-04-28 13:30:13 -07001920
1921// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
1922// modifies the slice contents in place, and returns a subslice of the original slice
1923func lastUniqueElements(list []string) []string {
1924 totalSkip := 0
1925 for i := len(list) - 1; i >= totalSkip; i-- {
1926 skip := 0
1927 for j := i - 1; j >= totalSkip; j-- {
1928 if list[i] == list[j] {
1929 skip++
1930 } else {
1931 list[j+skip] = list[j]
1932 }
1933 }
1934 totalSkip += skip
1935 }
1936 return list[totalSkip:]
1937}