blob: f057405a1f0e19b58e13fcdb6c0d37d05cdf8cc0 [file] [log] [blame]
Colin Cross5049f022015-03-18 13:28:46 -07001// Copyright 2015 Google Inc. All rights reserved.
Colin Cross3f40fa42015-01-30 17:27:36 -08002//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package cc
16
17// This file contains the module types for compiling C/C++ for Android, and converts the properties
18// into the flags and filenames necessary to pass to the compiler. The final creation of the rules
19// is handled in builder.go
20
21import (
Colin Cross3f40fa42015-01-30 17:27:36 -080022 "fmt"
23 "path/filepath"
24 "strings"
25
Colin Cross97ba0732015-03-23 17:50:24 -070026 "github.com/google/blueprint"
27 "github.com/google/blueprint/pathtools"
28
Colin Cross3f40fa42015-01-30 17:27:36 -080029 "android/soong/common"
Colin Cross5049f022015-03-18 13:28:46 -070030 "android/soong/genrule"
Colin Cross3f40fa42015-01-30 17:27:36 -080031)
32
Colin Cross3f40fa42015-01-30 17:27:36 -080033var (
Colin Cross1332b002015-04-07 17:11:30 -070034 HostPrebuiltTag = pctx.VariableConfigMethod("HostPrebuiltTag", common.Config.PrebuiltOS)
35 SrcDir = pctx.VariableConfigMethod("SrcDir", common.Config.SrcDir)
Colin Cross3f40fa42015-01-30 17:27:36 -080036
37 LibcRoot = pctx.StaticVariable("LibcRoot", "${SrcDir}/bionic/libc")
38 LibmRoot = pctx.StaticVariable("LibmRoot", "${SrcDir}/bionic/libm")
39)
40
41// Flags used by lots of devices. Putting them in package static variables will save bytes in
42// build.ninja so they aren't repeated for every file
43var (
44 commonGlobalCflags = []string{
45 "-DANDROID",
46 "-fmessage-length=0",
47 "-W",
48 "-Wall",
49 "-Wno-unused",
50 "-Winit-self",
51 "-Wpointer-arith",
52
53 // COMMON_RELEASE_CFLAGS
54 "-DNDEBUG",
55 "-UDEBUG",
56 }
57
58 deviceGlobalCflags = []string{
59 // TARGET_ERROR_FLAGS
60 "-Werror=return-type",
61 "-Werror=non-virtual-dtor",
62 "-Werror=address",
63 "-Werror=sequence-point",
64 }
65
66 hostGlobalCflags = []string{}
67
68 commonGlobalCppflags = []string{
69 "-Wsign-promo",
70 "-std=gnu++11",
71 }
72)
73
74func init() {
75 pctx.StaticVariable("commonGlobalCflags", strings.Join(commonGlobalCflags, " "))
76 pctx.StaticVariable("deviceGlobalCflags", strings.Join(deviceGlobalCflags, " "))
77 pctx.StaticVariable("hostGlobalCflags", strings.Join(hostGlobalCflags, " "))
78
79 pctx.StaticVariable("commonGlobalCppflags", strings.Join(commonGlobalCppflags, " "))
80
81 pctx.StaticVariable("commonClangGlobalCflags",
82 strings.Join(clangFilterUnknownCflags(commonGlobalCflags), " "))
83 pctx.StaticVariable("deviceClangGlobalCflags",
84 strings.Join(clangFilterUnknownCflags(deviceGlobalCflags), " "))
85 pctx.StaticVariable("hostClangGlobalCflags",
86 strings.Join(clangFilterUnknownCflags(hostGlobalCflags), " "))
Tim Kilbournf2948142015-03-11 12:03:03 -070087 pctx.StaticVariable("commonClangGlobalCppflags",
88 strings.Join(clangFilterUnknownCflags(commonGlobalCppflags), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -080089
90 // Everything in this list is a crime against abstraction and dependency tracking.
91 // Do not add anything to this list.
92 pctx.StaticVariable("commonGlobalIncludes", strings.Join([]string{
93 "-isystem ${SrcDir}/system/core/include",
94 "-isystem ${SrcDir}/hardware/libhardware/include",
95 "-isystem ${SrcDir}/hardware/libhardware_legacy/include",
96 "-isystem ${SrcDir}/hardware/ril/include",
97 "-isystem ${SrcDir}/libnativehelper/include",
98 "-isystem ${SrcDir}/frameworks/native/include",
99 "-isystem ${SrcDir}/frameworks/native/opengl/include",
100 "-isystem ${SrcDir}/frameworks/av/include",
101 "-isystem ${SrcDir}/frameworks/base/include",
102 }, " "))
103
104 pctx.StaticVariable("clangPath", "${SrcDir}/prebuilts/clang/${HostPrebuiltTag}/host/3.6/bin/")
105}
106
Colin Cross3f40fa42015-01-30 17:27:36 -0800107// Building C/C++ code is handled by objects that satisfy this interface via composition
Colin Cross97ba0732015-03-23 17:50:24 -0700108type CCModuleType interface {
Colin Cross3f40fa42015-01-30 17:27:36 -0800109 common.AndroidModule
110
Colin Crossfa138792015-04-24 17:31:52 -0700111 // Modify property values after parsing Blueprints file but before starting dependency
112 // resolution or build rule generation
113 ModifyProperties(common.AndroidBaseContext)
114
Colin Cross21b9a242015-03-24 14:15:58 -0700115 // Modify the ccFlags
Colin Cross0676e2d2015-04-24 17:39:18 -0700116 flags(common.AndroidModuleContext, CCFlags) CCFlags
Colin Cross3f40fa42015-01-30 17:27:36 -0800117
Colin Cross21b9a242015-03-24 14:15:58 -0700118 // Return list of dependency names for use in AndroidDynamicDependencies and in depsToPaths
Colin Cross0676e2d2015-04-24 17:39:18 -0700119 depNames(common.AndroidBaseContext, CCDeps) CCDeps
Colin Cross3f40fa42015-01-30 17:27:36 -0800120
121 // Compile objects into final module
Colin Cross97ba0732015-03-23 17:50:24 -0700122 compileModule(common.AndroidModuleContext, CCFlags, CCDeps, []string)
Colin Cross3f40fa42015-01-30 17:27:36 -0800123
Dan Albertc403f7c2015-03-18 14:01:18 -0700124 // Install the built module.
Colin Cross97ba0732015-03-23 17:50:24 -0700125 installModule(common.AndroidModuleContext, CCFlags)
Dan Albertc403f7c2015-03-18 14:01:18 -0700126
Colin Cross3f40fa42015-01-30 17:27:36 -0800127 // Return the output file (.o, .a or .so) for use by other modules
128 outputFile() string
129}
130
Colin Cross97ba0732015-03-23 17:50:24 -0700131type CCDeps struct {
Colin Cross28344522015-04-22 13:07:53 -0700132 StaticLibs, SharedLibs, LateStaticLibs, WholeStaticLibs, ObjFiles, Cflags []string
Colin Crossc472d572015-03-17 15:06:21 -0700133
Colin Cross21b9a242015-03-24 14:15:58 -0700134 WholeStaticLibObjFiles []string
135
Colin Cross97ba0732015-03-23 17:50:24 -0700136 CrtBegin, CrtEnd string
Colin Crossc472d572015-03-17 15:06:21 -0700137}
138
Colin Cross97ba0732015-03-23 17:50:24 -0700139type CCFlags struct {
Colin Cross28344522015-04-22 13:07:53 -0700140 GlobalFlags []string // Flags that apply to C, C++, and assembly source files
141 AsFlags []string // Flags that apply to assembly source files
142 CFlags []string // Flags that apply to C and C++ source files
143 ConlyFlags []string // Flags that apply to C source files
144 CppFlags []string // Flags that apply to C++ source files
145 YaccFlags []string // Flags that apply to Yacc source files
146 LdFlags []string // Flags that apply to linker command lines
147
148 Nocrt bool
149 Toolchain Toolchain
150 Clang bool
Colin Crossc472d572015-03-17 15:06:21 -0700151}
152
Colin Crossfa138792015-04-24 17:31:52 -0700153// CCBase contains the properties and members used by all C/C++ module types, and implements
Colin Crossc472d572015-03-17 15:06:21 -0700154// the blueprint.Module interface. It expects to be embedded into an outer specialization struct,
155// and uses a ccModuleType interface to that struct to create the build steps.
Colin Crossfa138792015-04-24 17:31:52 -0700156type CCBase struct {
Colin Crossc472d572015-03-17 15:06:21 -0700157 common.AndroidModuleBase
Colin Cross97ba0732015-03-23 17:50:24 -0700158 module CCModuleType
Colin Crossc472d572015-03-17 15:06:21 -0700159
Colin Crossfa138792015-04-24 17:31:52 -0700160 // Properties used to compile all C or C++ modules
161 Properties struct {
162 // srcs: list of source files used to compile the C/C++ module. May be .c, .cpp, or .S files.
163 Srcs []string `android:"arch_variant,arch_subtract"`
164
165 // cflags: list of module-specific flags that will be used for C and C++ compiles.
166 Cflags []string `android:"arch_variant"`
167
168 // cppflags: list of module-specific flags that will be used for C++ compiles
169 Cppflags []string `android:"arch_variant"`
170
171 // conlyflags: list of module-specific flags that will be used for C compiles
172 Conlyflags []string `android:"arch_variant"`
173
174 // asflags: list of module-specific flags that will be used for .S compiles
175 Asflags []string `android:"arch_variant"`
176
177 // yaccflags: list of module-specific flags that will be used for .y and .yy compiles
178 Yaccflags []string
179
180 // ldflags: list of module-specific flags that will be used for all link steps
181 Ldflags []string `android:"arch_variant"`
182
183 // instruction_set: the instruction set architecture to use to compile the C/C++
184 // module.
185 Instruction_set string `android:"arch_variant"`
186
187 // include_dirs: list of directories relative to the root of the source tree that will
188 // be added to the include path using -I.
189 // If possible, don't use this. If adding paths from the current directory use
190 // local_include_dirs, if adding paths from other modules use export_include_dirs in
191 // that module.
192 Include_dirs []string `android:"arch_variant"`
193
194 // local_include_dirs: list of directories relative to the Blueprints file that will
195 // be added to the include path using -I
196 Local_include_dirs []string `android:"arch_variant"`
197
198 // export_include_dirs: list of directories relative to the Blueprints file that will
199 // be added to the include path using -I for any module that links against this module
200 Export_include_dirs []string `android:"arch_variant"`
201
202 // clang_cflags: list of module-specific flags that will be used for C and C++ compiles when
203 // compiling with clang
204 Clang_cflags []string `android:"arch_variant"`
205
206 // clang_asflags: list of module-specific flags that will be used for .S compiles when
207 // compiling with clang
208 Clang_asflags []string `android:"arch_variant"`
209
210 // system_shared_libs: list of system libraries that will be dynamically linked to
211 // shared library and executable modules. If unset, generally defaults to libc
212 // and libm. Set to [] to prevent linking against libc and libm.
213 System_shared_libs []string
214
215 // whole_static_libs: list of modules whose object files should be linked into this module
216 // in their entirety. For static library modules, all of the .o files from the intermediate
217 // directory of the dependency will be linked into this modules .a file. For a shared library,
218 // the dependency's .a file will be linked into this module using -Wl,--whole-archive.
219 Whole_static_libs []string `android:"arch_variant"`
220
221 // static_libs: list of modules that should be statically linked into this module.
222 Static_libs []string `android:"arch_variant"`
223
224 // shared_libs: list of modules that should be dynamically linked into this module.
225 Shared_libs []string `android:"arch_variant"`
226
227 // allow_undefined_symbols: allow the module to contain undefined symbols. By default,
228 // modules cannot contain undefined symbols that are not satisified by their immediate
229 // dependencies. Set this flag to true to remove --no-undefined from the linker flags.
230 // This flag should only be necessary for compiling low-level libraries like libc.
231 Allow_undefined_symbols bool
232
233 // nocrt: don't link in crt_begin and crt_end. This flag should only be necessary for
234 // compiling crt or libc.
235 Nocrt bool `android:"arch_variant"`
236
237 // no_default_compiler_flags: don't insert default compiler flags into asflags, cflags,
238 // cppflags, conlyflags, ldflags, or include_dirs
239 No_default_compiler_flags bool
240
241 // clang: compile module with clang instead of gcc
242 Clang bool `android:"arch_variant"`
243
244 // rtti: pass -frtti instead of -fno-rtti
245 Rtti bool
246
247 // host_ldlibs: -l arguments to pass to linker for host-provided shared libraries
248 Host_ldlibs []string `android:"arch_variant"`
249
250 // stl: select the STL library to use. Possible values are "libc++", "libc++_static",
251 // "stlport", "stlport_static", "ndk", "libstdc++", or "none". Leave blank to select the
252 // default
253 Stl string
254
255 // Set for combined shared/static libraries to prevent compiling object files a second time
256 SkipCompileObjs bool `blueprint:"mutated"`
257
258 Debug struct {
259 Cflags []string `android:"arch_variant"`
260 } `android:"arch_variant"`
261 Release struct {
262 Cflags []string `android:"arch_variant"`
263 } `android:"arch_variant"`
264
265 // Minimum sdk version supported when compiling against the ndk
266 Sdk_version string
267
268 // relative_install_path: install to a subdirectory of the default install path for the module
269 Relative_install_path string
270 }
271
272 unused struct {
273 Asan bool
274 Native_coverage bool
275 Strip string
276 Tags []string
277 Required []string
278 }
Colin Crossc472d572015-03-17 15:06:21 -0700279
280 installPath string
281}
282
Colin Crossfa138792015-04-24 17:31:52 -0700283func newCCBase(base *CCBase, module CCModuleType, hod common.HostOrDeviceSupported,
Colin Crossc472d572015-03-17 15:06:21 -0700284 multilib common.Multilib, props ...interface{}) (blueprint.Module, []interface{}) {
285
286 base.module = module
287
Colin Crossfa138792015-04-24 17:31:52 -0700288 props = append(props, &base.Properties, &base.unused)
Colin Crossc472d572015-03-17 15:06:21 -0700289
Colin Cross5049f022015-03-18 13:28:46 -0700290 return common.InitAndroidArchModule(module, hod, multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700291}
292
Colin Crossfa138792015-04-24 17:31:52 -0700293func (c *CCBase) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800294 toolchain := c.findToolchain(ctx)
295 if ctx.Failed() {
296 return
297 }
298
Colin Cross21b9a242015-03-24 14:15:58 -0700299 flags := c.collectFlags(ctx, toolchain)
Colin Cross3f40fa42015-01-30 17:27:36 -0800300 if ctx.Failed() {
301 return
302 }
303
Colin Cross0676e2d2015-04-24 17:39:18 -0700304 depNames := c.module.depNames(ctx, CCDeps{})
Colin Cross3f40fa42015-01-30 17:27:36 -0800305 if ctx.Failed() {
306 return
307 }
308
Colin Cross21b9a242015-03-24 14:15:58 -0700309 deps := c.depsToPaths(ctx, depNames)
Colin Cross3f40fa42015-01-30 17:27:36 -0800310 if ctx.Failed() {
311 return
312 }
313
Colin Cross28344522015-04-22 13:07:53 -0700314 flags.CFlags = append(flags.CFlags, deps.Cflags...)
Colin Crossed9f8682015-03-18 17:17:35 -0700315
Colin Cross581c1892015-04-07 16:50:10 -0700316 objFiles := c.compileObjs(ctx, flags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800317 if ctx.Failed() {
318 return
319 }
320
Colin Cross581c1892015-04-07 16:50:10 -0700321 generatedObjFiles := c.compileGeneratedObjs(ctx, flags)
Colin Cross5049f022015-03-18 13:28:46 -0700322 if ctx.Failed() {
323 return
324 }
325
326 objFiles = append(objFiles, generatedObjFiles...)
327
Colin Cross3f40fa42015-01-30 17:27:36 -0800328 c.ccModuleType().compileModule(ctx, flags, deps, objFiles)
329 if ctx.Failed() {
330 return
331 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700332
333 c.ccModuleType().installModule(ctx, flags)
334 if ctx.Failed() {
335 return
336 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800337}
338
Colin Crossfa138792015-04-24 17:31:52 -0700339func (c *CCBase) ccModuleType() CCModuleType {
Colin Cross3f40fa42015-01-30 17:27:36 -0800340 return c.module
341}
342
Colin Crossfa138792015-04-24 17:31:52 -0700343var _ common.AndroidDynamicDepender = (*CCBase)(nil)
Colin Cross3f40fa42015-01-30 17:27:36 -0800344
Colin Crossfa138792015-04-24 17:31:52 -0700345func (c *CCBase) findToolchain(ctx common.AndroidModuleContext) Toolchain {
Colin Cross3f40fa42015-01-30 17:27:36 -0800346 arch := ctx.Arch()
347 factory := toolchainFactories[arch.HostOrDevice][arch.ArchType]
348 if factory == nil {
349 panic(fmt.Sprintf("Toolchain not found for %s arch %q",
350 arch.HostOrDevice.String(), arch.String()))
351 }
352 return factory(arch.ArchVariant, arch.CpuVariant)
353}
354
Colin Crossfa138792015-04-24 17:31:52 -0700355func (c *CCBase) ModifyProperties(ctx common.AndroidBaseContext) {
356}
357
Colin Crosse11befc2015-04-27 17:49:17 -0700358func (c *CCBase) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Crossfa138792015-04-24 17:31:52 -0700359 depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.Properties.Whole_static_libs...)
360 depNames.StaticLibs = append(depNames.StaticLibs, c.Properties.Static_libs...)
361 depNames.SharedLibs = append(depNames.SharedLibs, c.Properties.Shared_libs...)
Colin Cross21b9a242015-03-24 14:15:58 -0700362
Colin Cross21b9a242015-03-24 14:15:58 -0700363 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -0800364}
365
Colin Crossfa138792015-04-24 17:31:52 -0700366func (c *CCBase) AndroidDynamicDependencies(ctx common.AndroidDynamicDependerModuleContext) []string {
367 c.module.ModifyProperties(ctx)
368
Colin Cross21b9a242015-03-24 14:15:58 -0700369 depNames := CCDeps{}
Colin Cross0676e2d2015-04-24 17:39:18 -0700370 depNames = c.module.depNames(ctx, depNames)
Colin Cross21b9a242015-03-24 14:15:58 -0700371 staticLibs := depNames.WholeStaticLibs
372 staticLibs = append(staticLibs, depNames.StaticLibs...)
373 staticLibs = append(staticLibs, depNames.LateStaticLibs...)
374 ctx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, staticLibs...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800375
Colin Cross21b9a242015-03-24 14:15:58 -0700376 ctx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, depNames.SharedLibs...)
377
378 ret := append([]string(nil), depNames.ObjFiles...)
379 if depNames.CrtBegin != "" {
380 ret = append(ret, depNames.CrtBegin)
381 }
382 if depNames.CrtEnd != "" {
383 ret = append(ret, depNames.CrtEnd)
384 }
385
386 return ret
Colin Cross3f40fa42015-01-30 17:27:36 -0800387}
388
389// Create a ccFlags struct that collects the compile flags from global values,
390// per-target values, module type values, and per-module Blueprints properties
Colin Crossfa138792015-04-24 17:31:52 -0700391func (c *CCBase) collectFlags(ctx common.AndroidModuleContext, toolchain Toolchain) CCFlags {
Colin Cross97ba0732015-03-23 17:50:24 -0700392 flags := CCFlags{
Colin Crossfa138792015-04-24 17:31:52 -0700393 CFlags: c.Properties.Cflags,
394 CppFlags: c.Properties.Cppflags,
395 ConlyFlags: c.Properties.Conlyflags,
396 LdFlags: c.Properties.Ldflags,
397 AsFlags: c.Properties.Asflags,
398 YaccFlags: c.Properties.Yaccflags,
399 Nocrt: c.Properties.Nocrt,
Colin Cross97ba0732015-03-23 17:50:24 -0700400 Toolchain: toolchain,
Colin Crossfa138792015-04-24 17:31:52 -0700401 Clang: c.Properties.Clang,
Colin Cross3f40fa42015-01-30 17:27:36 -0800402 }
Colin Cross28344522015-04-22 13:07:53 -0700403
404 // Include dir cflags
Colin Crossfa138792015-04-24 17:31:52 -0700405 rootIncludeDirs := pathtools.PrefixPaths(c.Properties.Include_dirs, ctx.AConfig().SrcDir())
406 localIncludeDirs := pathtools.PrefixPaths(c.Properties.Local_include_dirs, common.ModuleSrcDir(ctx))
Colin Cross28344522015-04-22 13:07:53 -0700407 flags.GlobalFlags = append(flags.GlobalFlags,
408 includeDirsToFlags(rootIncludeDirs),
409 includeDirsToFlags(localIncludeDirs))
410
Colin Crossfa138792015-04-24 17:31:52 -0700411 if !c.Properties.No_default_compiler_flags {
412 if c.Properties.Sdk_version == "" || ctx.Host() {
Colin Cross28344522015-04-22 13:07:53 -0700413 flags.GlobalFlags = append(flags.GlobalFlags,
414 "${commonGlobalIncludes}",
415 toolchain.IncludeFlags(),
416 "-I${SrcDir}/libnativehelper/include/nativehelper")
417 }
418
419 flags.GlobalFlags = append(flags.GlobalFlags, []string{
420 "-I" + common.ModuleSrcDir(ctx),
421 "-I" + common.ModuleOutDir(ctx),
422 "-I" + common.ModuleGenDir(ctx),
423 }...)
424 }
425
Colin Crossfa138792015-04-24 17:31:52 -0700426 instructionSet := c.Properties.Instruction_set
Tim Kilbourn1a9bf262015-03-18 12:28:32 -0700427 instructionSetFlags, err := toolchain.InstructionSetFlags(instructionSet)
428 if err != nil {
429 ctx.ModuleErrorf("%s", err)
430 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800431
Colin Crossaf19a292015-03-18 12:07:10 -0700432 // TODO: debug
Colin Crossfa138792015-04-24 17:31:52 -0700433 flags.CFlags = append(flags.CFlags, c.Properties.Release.Cflags...)
Colin Crossaf19a292015-03-18 12:07:10 -0700434
Colin Cross28d76592015-03-26 16:14:04 -0700435 if ctx.Host() && !ctx.ContainsProperty("clang") {
Colin Cross97ba0732015-03-23 17:50:24 -0700436 flags.Clang = true
Colin Cross3f40fa42015-01-30 17:27:36 -0800437 }
438
Colin Cross97ba0732015-03-23 17:50:24 -0700439 if flags.Clang {
440 flags.CFlags = clangFilterUnknownCflags(flags.CFlags)
Colin Crossfa138792015-04-24 17:31:52 -0700441 flags.CFlags = append(flags.CFlags, c.Properties.Clang_cflags...)
442 flags.AsFlags = append(flags.AsFlags, c.Properties.Clang_asflags...)
Colin Cross97ba0732015-03-23 17:50:24 -0700443 flags.CppFlags = clangFilterUnknownCflags(flags.CppFlags)
444 flags.ConlyFlags = clangFilterUnknownCflags(flags.ConlyFlags)
445 flags.LdFlags = clangFilterUnknownCflags(flags.LdFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800446
Colin Cross97ba0732015-03-23 17:50:24 -0700447 flags.CFlags = append(flags.CFlags, "${clangExtraCflags}")
448 flags.ConlyFlags = append(flags.ConlyFlags, "${clangExtraConlyflags}")
Colin Crossf6566ed2015-03-24 11:13:38 -0700449 if ctx.Device() {
Colin Cross97ba0732015-03-23 17:50:24 -0700450 flags.CFlags = append(flags.CFlags, "${clangExtraTargetCflags}")
Colin Crossbdd7b1c2015-03-16 16:21:20 -0700451 }
452
Colin Cross3f40fa42015-01-30 17:27:36 -0800453 target := "-target " + toolchain.ClangTriple()
454 gccPrefix := "-B" + filepath.Join(toolchain.GccRoot(), toolchain.GccTriple(), "bin")
455
Colin Cross97ba0732015-03-23 17:50:24 -0700456 flags.CFlags = append(flags.CFlags, target, gccPrefix)
457 flags.AsFlags = append(flags.AsFlags, target, gccPrefix)
458 flags.LdFlags = append(flags.LdFlags, target, gccPrefix)
Colin Cross3f40fa42015-01-30 17:27:36 -0800459
Colin Crossf6566ed2015-03-24 11:13:38 -0700460 if ctx.Host() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800461 gccToolchain := "--gcc-toolchain=" + toolchain.GccRoot()
462 sysroot := "--sysroot=" + filepath.Join(toolchain.GccRoot(), "sysroot")
463
464 // TODO: also need more -B, -L flags to make host builds hermetic
Colin Cross97ba0732015-03-23 17:50:24 -0700465 flags.CFlags = append(flags.CFlags, gccToolchain, sysroot)
466 flags.AsFlags = append(flags.AsFlags, gccToolchain, sysroot)
467 flags.LdFlags = append(flags.LdFlags, gccToolchain, sysroot)
Colin Cross3f40fa42015-01-30 17:27:36 -0800468 }
469 }
470
Colin Crossfa138792015-04-24 17:31:52 -0700471 if !c.Properties.No_default_compiler_flags {
472 if ctx.Device() && !c.Properties.Allow_undefined_symbols {
Colin Cross97ba0732015-03-23 17:50:24 -0700473 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-undefined")
Colin Cross3f40fa42015-01-30 17:27:36 -0800474 }
475
Colin Cross56b4d452015-04-21 17:38:44 -0700476 flags.GlobalFlags = append(flags.GlobalFlags, instructionSetFlags)
477
Colin Cross97ba0732015-03-23 17:50:24 -0700478 if flags.Clang {
479 flags.CppFlags = append(flags.CppFlags, "${commonClangGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -0700480 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -0800481 toolchain.ClangCflags(),
482 "${commonClangGlobalCflags}",
Colin Cross56b4d452015-04-21 17:38:44 -0700483 fmt.Sprintf("${%sClangGlobalCflags}", ctx.Arch().HostOrDevice))
Colin Cross3f40fa42015-01-30 17:27:36 -0800484 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700485 flags.CppFlags = append(flags.CppFlags, "${commonGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -0700486 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -0800487 toolchain.Cflags(),
488 "${commonGlobalCflags}",
Colin Cross56b4d452015-04-21 17:38:44 -0700489 fmt.Sprintf("${%sGlobalCflags}", ctx.Arch().HostOrDevice))
Colin Cross3f40fa42015-01-30 17:27:36 -0800490 }
491
Colin Crossf6566ed2015-03-24 11:13:38 -0700492 if ctx.Device() {
Colin Crossfa138792015-04-24 17:31:52 -0700493 if c.Properties.Rtti {
Colin Cross97ba0732015-03-23 17:50:24 -0700494 flags.CppFlags = append(flags.CppFlags, "-frtti")
Colin Cross3f40fa42015-01-30 17:27:36 -0800495 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700496 flags.CppFlags = append(flags.CppFlags, "-fno-rtti")
Colin Cross3f40fa42015-01-30 17:27:36 -0800497 }
498 }
499
Colin Cross97ba0732015-03-23 17:50:24 -0700500 flags.AsFlags = append(flags.AsFlags, "-D__ASSEMBLY__")
Colin Cross3f40fa42015-01-30 17:27:36 -0800501
Colin Cross97ba0732015-03-23 17:50:24 -0700502 if flags.Clang {
503 flags.CppFlags = append(flags.CppFlags, toolchain.ClangCppflags())
504 flags.LdFlags = append(flags.LdFlags, toolchain.ClangLdflags())
Colin Cross3f40fa42015-01-30 17:27:36 -0800505 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700506 flags.CppFlags = append(flags.CppFlags, toolchain.Cppflags())
507 flags.LdFlags = append(flags.LdFlags, toolchain.Ldflags())
Colin Cross3f40fa42015-01-30 17:27:36 -0800508 }
Colin Cross28344522015-04-22 13:07:53 -0700509
510 if ctx.Host() {
Colin Crossfa138792015-04-24 17:31:52 -0700511 flags.LdFlags = append(flags.LdFlags, c.Properties.Host_ldlibs...)
Colin Cross28344522015-04-22 13:07:53 -0700512 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800513 }
514
Colin Cross0676e2d2015-04-24 17:39:18 -0700515 flags = c.ccModuleType().flags(ctx, flags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800516
517 // Optimization to reduce size of build.ninja
518 // Replace the long list of flags for each file with a module-local variable
Colin Cross97ba0732015-03-23 17:50:24 -0700519 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
520 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
521 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
522 flags.CFlags = []string{"$cflags"}
523 flags.CppFlags = []string{"$cppflags"}
524 flags.AsFlags = []string{"$asflags"}
Colin Cross3f40fa42015-01-30 17:27:36 -0800525
526 return flags
527}
528
Colin Cross0676e2d2015-04-24 17:39:18 -0700529func (c *CCBase) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Cross3f40fa42015-01-30 17:27:36 -0800530 return flags
531}
532
533// Compile a list of source files into objects a specified subdirectory
Colin Crossfa138792015-04-24 17:31:52 -0700534func (c *CCBase) customCompileObjs(ctx common.AndroidModuleContext, flags CCFlags,
Colin Cross581c1892015-04-07 16:50:10 -0700535 subdir string, srcFiles []string) []string {
536
537 buildFlags := ccFlagsToBuilderFlags(flags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800538
Colin Crossfce53272015-04-08 11:21:40 -0700539 srcFiles = common.ExpandSources(ctx, srcFiles)
Colin Cross581c1892015-04-07 16:50:10 -0700540 srcFiles, deps := genSources(ctx, srcFiles, buildFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800541
Colin Cross581c1892015-04-07 16:50:10 -0700542 return TransformSourceToObj(ctx, subdir, srcFiles, buildFlags, deps)
Colin Cross3f40fa42015-01-30 17:27:36 -0800543}
544
Colin Crossfa138792015-04-24 17:31:52 -0700545// Compile files listed in c.Properties.Srcs into objects
546func (c *CCBase) compileObjs(ctx common.AndroidModuleContext, flags CCFlags) []string {
Colin Cross3f40fa42015-01-30 17:27:36 -0800547
Colin Crossfa138792015-04-24 17:31:52 -0700548 if c.Properties.SkipCompileObjs {
Colin Cross3f40fa42015-01-30 17:27:36 -0800549 return nil
550 }
551
Colin Crossfa138792015-04-24 17:31:52 -0700552 return c.customCompileObjs(ctx, flags, "", c.Properties.Srcs)
Colin Cross3f40fa42015-01-30 17:27:36 -0800553}
554
Colin Cross5049f022015-03-18 13:28:46 -0700555// Compile generated source files from dependencies
Colin Crossfa138792015-04-24 17:31:52 -0700556func (c *CCBase) compileGeneratedObjs(ctx common.AndroidModuleContext, flags CCFlags) []string {
Colin Cross5049f022015-03-18 13:28:46 -0700557 var srcs []string
558
Colin Crossfa138792015-04-24 17:31:52 -0700559 if c.Properties.SkipCompileObjs {
Colin Cross5049f022015-03-18 13:28:46 -0700560 return nil
561 }
562
563 ctx.VisitDirectDeps(func(module blueprint.Module) {
564 if gen, ok := module.(genrule.SourceFileGenerator); ok {
565 srcs = append(srcs, gen.GeneratedSourceFiles()...)
566 }
567 })
568
569 if len(srcs) == 0 {
570 return nil
571 }
572
Colin Cross581c1892015-04-07 16:50:10 -0700573 return TransformSourceToObj(ctx, "", srcs, ccFlagsToBuilderFlags(flags), nil)
Colin Cross5049f022015-03-18 13:28:46 -0700574}
575
Colin Crossfa138792015-04-24 17:31:52 -0700576func (c *CCBase) outputFile() string {
Colin Cross3f40fa42015-01-30 17:27:36 -0800577 return ""
578}
579
Colin Crossfa138792015-04-24 17:31:52 -0700580func (c *CCBase) depsToPathsFromList(ctx common.AndroidModuleContext,
Colin Cross3f40fa42015-01-30 17:27:36 -0800581 names []string) (modules []common.AndroidModule,
Colin Cross28344522015-04-22 13:07:53 -0700582 outputFiles []string, exportedFlags []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800583
584 for _, n := range names {
585 found := false
586 ctx.VisitDirectDeps(func(m blueprint.Module) {
587 otherName := ctx.OtherModuleName(m)
588 if otherName != n {
589 return
590 }
591
Colin Cross97ba0732015-03-23 17:50:24 -0700592 if a, ok := m.(CCModuleType); ok {
Colin Cross3f40fa42015-01-30 17:27:36 -0800593 if a.Disabled() {
594 // If a cc_library host+device module depends on a library that exists as both
595 // cc_library_shared and cc_library_host_shared, it will end up with two
596 // dependencies with the same name, one of which is marked disabled for each
597 // of host and device. Ignore the disabled one.
598 return
599 }
600 if a.HostOrDevice() != ctx.Arch().HostOrDevice {
601 ctx.ModuleErrorf("host/device mismatch between %q and %q", ctx.ModuleName(),
602 otherName)
603 return
604 }
605
606 if outputFile := a.outputFile(); outputFile != "" {
607 if found {
608 ctx.ModuleErrorf("multiple modules satisified dependency on %q", otherName)
609 return
610 }
611 outputFiles = append(outputFiles, outputFile)
612 modules = append(modules, a)
Colin Cross28344522015-04-22 13:07:53 -0700613 if i, ok := a.(ccExportedFlagsProducer); ok {
614 exportedFlags = append(exportedFlags, i.exportedFlags()...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800615 }
616 found = true
617 } else {
618 ctx.ModuleErrorf("module %q missing output file", otherName)
619 return
620 }
621 } else {
622 ctx.ModuleErrorf("module %q not an android module", otherName)
623 return
624 }
625 })
626 if !found {
627 ctx.ModuleErrorf("unsatisified dependency on %q", n)
628 }
629 }
630
Colin Cross28344522015-04-22 13:07:53 -0700631 return modules, outputFiles, exportedFlags
Colin Cross3f40fa42015-01-30 17:27:36 -0800632}
633
Colin Cross21b9a242015-03-24 14:15:58 -0700634// Convert depenedency names to paths. Takes a CCDeps containing names and returns a CCDeps
635// containing paths
Colin Crossfa138792015-04-24 17:31:52 -0700636func (c *CCBase) depsToPaths(ctx common.AndroidModuleContext, depNames CCDeps) CCDeps {
Colin Cross21b9a242015-03-24 14:15:58 -0700637 var depPaths CCDeps
Colin Cross28344522015-04-22 13:07:53 -0700638 var newCflags []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800639
Colin Cross21b9a242015-03-24 14:15:58 -0700640 var wholeStaticLibModules []common.AndroidModule
Colin Cross3f40fa42015-01-30 17:27:36 -0800641
Colin Cross28344522015-04-22 13:07:53 -0700642 wholeStaticLibModules, depPaths.WholeStaticLibs, newCflags =
Colin Cross21b9a242015-03-24 14:15:58 -0700643 c.depsToPathsFromList(ctx, depNames.WholeStaticLibs)
Colin Cross28344522015-04-22 13:07:53 -0700644 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800645
Colin Cross21b9a242015-03-24 14:15:58 -0700646 for _, m := range wholeStaticLibModules {
647 if staticLib, ok := m.(ccLibraryInterface); ok && staticLib.static() {
648 depPaths.WholeStaticLibObjFiles =
649 append(depPaths.WholeStaticLibObjFiles, staticLib.allObjFiles()...)
650 } else {
651 ctx.ModuleErrorf("module %q not a static library", ctx.OtherModuleName(m))
652 }
653 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800654
Colin Cross28344522015-04-22 13:07:53 -0700655 _, depPaths.StaticLibs, newCflags = c.depsToPathsFromList(ctx, depNames.StaticLibs)
656 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross21b9a242015-03-24 14:15:58 -0700657
Colin Cross28344522015-04-22 13:07:53 -0700658 _, depPaths.LateStaticLibs, newCflags = c.depsToPathsFromList(ctx, depNames.LateStaticLibs)
659 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross21b9a242015-03-24 14:15:58 -0700660
Colin Cross28344522015-04-22 13:07:53 -0700661 _, depPaths.SharedLibs, newCflags = c.depsToPathsFromList(ctx, depNames.SharedLibs)
662 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross21b9a242015-03-24 14:15:58 -0700663
664 ctx.VisitDirectDeps(func(m blueprint.Module) {
665 if obj, ok := m.(*ccObject); ok {
666 otherName := ctx.OtherModuleName(m)
667 if otherName == depNames.CrtBegin {
Colin Crossfa138792015-04-24 17:31:52 -0700668 if !c.Properties.Nocrt {
Colin Cross21b9a242015-03-24 14:15:58 -0700669 depPaths.CrtBegin = obj.outputFile()
670 }
671 } else if otherName == depNames.CrtEnd {
Colin Crossfa138792015-04-24 17:31:52 -0700672 if !c.Properties.Nocrt {
Colin Cross21b9a242015-03-24 14:15:58 -0700673 depPaths.CrtEnd = obj.outputFile()
674 }
675 } else {
676 depPaths.ObjFiles = append(depPaths.ObjFiles, obj.outputFile())
677 }
678 }
679 })
680
681 return depPaths
Colin Cross3f40fa42015-01-30 17:27:36 -0800682}
683
Colin Crossfa138792015-04-24 17:31:52 -0700684// CCLinked contains the properties and members used by libraries and executables
685type CCLinked struct {
686 CCBase
Colin Crossed4cf0b2015-03-26 14:43:45 -0700687
688 dynamicProperties struct {
Colin Cross18b6dc52015-04-28 13:20:37 -0700689 VariantIsShared bool `blueprint:"mutated"`
690 VariantIsStatic bool `blueprint:"mutated"`
691 VariantIsStaticBinary bool `blueprint:"mutated"`
Colin Crossed4cf0b2015-03-26 14:43:45 -0700692 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800693}
694
Colin Crossfa138792015-04-24 17:31:52 -0700695func newCCDynamic(dynamic *CCLinked, module CCModuleType, hod common.HostOrDeviceSupported,
Colin Crossc472d572015-03-17 15:06:21 -0700696 multilib common.Multilib, props ...interface{}) (blueprint.Module, []interface{}) {
697
Colin Crossed4cf0b2015-03-26 14:43:45 -0700698 props = append(props, &dynamic.dynamicProperties)
699
Colin Crossfa138792015-04-24 17:31:52 -0700700 return newCCBase(&dynamic.CCBase, module, hod, multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700701}
702
Colin Crossfa138792015-04-24 17:31:52 -0700703func (c *CCLinked) systemSharedLibs(ctx common.AndroidBaseContext) []string {
Colin Cross28d76592015-03-26 16:14:04 -0700704 if ctx.ContainsProperty("system_shared_libs") {
Colin Crossfa138792015-04-24 17:31:52 -0700705 return c.Properties.System_shared_libs
706 } else if ctx.Device() && c.Properties.Sdk_version == "" {
Colin Cross577f6e42015-03-27 18:23:34 -0700707 return []string{"libc", "libm"}
Colin Cross28d76592015-03-26 16:14:04 -0700708 } else {
Colin Cross577f6e42015-03-27 18:23:34 -0700709 return nil
Colin Cross3f40fa42015-01-30 17:27:36 -0800710 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800711}
712
Colin Crossfa138792015-04-24 17:31:52 -0700713func (c *CCLinked) stl(ctx common.AndroidBaseContext) string {
714 if c.Properties.Sdk_version != "" && ctx.Device() {
715 switch c.Properties.Stl {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700716 case "":
717 return "ndk_system"
718 case "c++_shared", "c++_static",
719 "stlport_shared", "stlport_static",
720 "gnustl_static":
Colin Crossfa138792015-04-24 17:31:52 -0700721 return "ndk_lib" + c.Properties.Stl
Colin Crossed4cf0b2015-03-26 14:43:45 -0700722 default:
Colin Crossfa138792015-04-24 17:31:52 -0700723 ctx.ModuleErrorf("stl: %q is not a supported STL with sdk_version set", c.Properties.Stl)
Colin Crossed4cf0b2015-03-26 14:43:45 -0700724 return ""
725 }
726 }
727
Colin Crossfa138792015-04-24 17:31:52 -0700728 switch c.Properties.Stl {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700729 case "libc++", "libc++_static",
730 "stlport", "stlport_static",
731 "libstdc++":
Colin Crossfa138792015-04-24 17:31:52 -0700732 return c.Properties.Stl
Colin Crossed4cf0b2015-03-26 14:43:45 -0700733 case "none":
734 return ""
735 case "":
Colin Cross18b6dc52015-04-28 13:20:37 -0700736 if c.static() {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700737 return "libc++_static"
Colin Cross18b6dc52015-04-28 13:20:37 -0700738 } else {
739 return "libc++" // TODO: mingw needs libstdc++
Colin Crossed4cf0b2015-03-26 14:43:45 -0700740 }
741 default:
Colin Crossfa138792015-04-24 17:31:52 -0700742 ctx.ModuleErrorf("stl: %q is not a supported STL", c.Properties.Stl)
Colin Crossed4cf0b2015-03-26 14:43:45 -0700743 return ""
744 }
745}
746
Colin Cross712fc022015-04-27 11:13:34 -0700747var (
748 hostDynamicGccLibs = []string{"-lgcc_s", "-lgcc", "-lc", "-lgcc_s", "-lgcc"}
749 hostStaticGccLibs = []string{"-Wl,--start-group", "-lgcc", "-lgcc_eh", "-lc", "-Wl,--end-group"}
750)
751
Colin Crosse11befc2015-04-27 17:49:17 -0700752func (c *CCLinked) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700753 stl := c.stl(ctx)
754 if ctx.Failed() {
755 return flags
756 }
757
758 switch stl {
759 case "libc++", "libc++_static":
760 flags.CFlags = append(flags.CFlags, "-D_USING_LIBCXX")
Colin Cross28344522015-04-22 13:07:53 -0700761 flags.CFlags = append(flags.CFlags, "-I${SrcDir}/external/libcxx/include")
Colin Crossed4cf0b2015-03-26 14:43:45 -0700762 if ctx.Host() {
763 flags.CppFlags = append(flags.CppFlags, "-nostdinc++")
764 flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs")
Colin Cross712fc022015-04-27 11:13:34 -0700765 flags.LdFlags = append(flags.LdFlags, "-lm", "-lpthread")
Colin Cross18b6dc52015-04-28 13:20:37 -0700766 if c.staticBinary() {
Colin Cross712fc022015-04-27 11:13:34 -0700767 flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs...)
Colin Cross18b6dc52015-04-28 13:20:37 -0700768 } else {
769 flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs...)
Colin Cross712fc022015-04-27 11:13:34 -0700770 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700771 }
772 case "stlport", "stlport_static":
773 if ctx.Device() {
Colin Cross28344522015-04-22 13:07:53 -0700774 flags.CFlags = append(flags.CFlags,
775 "-I${SrcDir}/external/stlport/stlport",
776 "-I${SrcDir}/bionic/libstdc++/include",
777 "-I${SrcDir}/bionic")
Colin Crossed4cf0b2015-03-26 14:43:45 -0700778 }
779 case "libstdc++":
780 // Using bionic's basic libstdc++. Not actually an STL. Only around until the
781 // tree is in good enough shape to not need it.
782 // Host builds will use GNU libstdc++.
783 if ctx.Device() {
Colin Cross28344522015-04-22 13:07:53 -0700784 flags.CFlags = append(flags.CFlags, "-I${SrcDir}/bionic/libstdc++/include")
Colin Crossed4cf0b2015-03-26 14:43:45 -0700785 }
786 case "ndk_system":
Colin Cross1332b002015-04-07 17:11:30 -0700787 ndkSrcRoot := ctx.AConfig().SrcDir() + "/prebuilts/ndk/current/sources/"
Colin Cross28344522015-04-22 13:07:53 -0700788 flags.CFlags = append(flags.CFlags, "-isystem "+ndkSrcRoot+"cxx-stl/system/include")
Colin Crossed4cf0b2015-03-26 14:43:45 -0700789 case "ndk_libc++_shared", "ndk_libc++_static":
790 // TODO(danalbert): This really shouldn't be here...
791 flags.CppFlags = append(flags.CppFlags, "-std=c++11")
792 case "ndk_libstlport_shared", "ndk_libstlport_static", "ndk_libgnustl_static":
793 // Nothing
794 case "":
795 // None or error.
796 if ctx.Host() {
797 flags.CppFlags = append(flags.CppFlags, "-nostdinc++")
798 flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs")
Colin Cross18b6dc52015-04-28 13:20:37 -0700799 if c.staticBinary() {
Colin Cross712fc022015-04-27 11:13:34 -0700800 flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs...)
Colin Cross18b6dc52015-04-28 13:20:37 -0700801 } else {
802 flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs...)
Colin Cross712fc022015-04-27 11:13:34 -0700803 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700804 }
805 default:
Colin Crossfa138792015-04-24 17:31:52 -0700806 panic(fmt.Errorf("Unknown stl in CCLinked.Flags: %q", stl))
Colin Crossed4cf0b2015-03-26 14:43:45 -0700807 }
808
809 return flags
810}
811
Colin Crosse11befc2015-04-27 17:49:17 -0700812func (c *CCLinked) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
813 depNames = c.CCBase.depNames(ctx, depNames)
Colin Cross3f40fa42015-01-30 17:27:36 -0800814
Colin Crossed4cf0b2015-03-26 14:43:45 -0700815 stl := c.stl(ctx)
816 if ctx.Failed() {
817 return depNames
818 }
819
820 switch stl {
821 case "libc++":
822 depNames.SharedLibs = append(depNames.SharedLibs, stl)
823 case "libstdc++":
824 if ctx.Device() {
825 depNames.SharedLibs = append(depNames.SharedLibs, stl)
826 }
827 case "libc++_static":
828 depNames.StaticLibs = append(depNames.StaticLibs, stl)
829 case "stlport":
830 depNames.SharedLibs = append(depNames.SharedLibs, "libstdc++", "libstlport")
831 case "stlport_static":
832 depNames.StaticLibs = append(depNames.StaticLibs, "libstdc++", "libstlport_static")
833 case "":
834 // None or error.
835 case "ndk_system":
836 // TODO: Make a system STL prebuilt for the NDK.
837 // 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 -0700838 // its own includes. The includes are handled in CCBase.Flags().
Colin Cross577f6e42015-03-27 18:23:34 -0700839 depNames.SharedLibs = append([]string{"libstdc++"}, depNames.SharedLibs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -0700840 case "ndk_libc++_shared", "ndk_libstlport_shared":
841 depNames.SharedLibs = append(depNames.SharedLibs, stl)
842 case "ndk_libc++_static", "ndk_libstlport_static", "ndk_libgnustl_static":
843 depNames.StaticLibs = append(depNames.StaticLibs, stl)
844 default:
Colin Crosse11befc2015-04-27 17:49:17 -0700845 panic(fmt.Errorf("Unknown stl in CCLinked.depNames: %q", stl))
Colin Crossed4cf0b2015-03-26 14:43:45 -0700846 }
847
Colin Crossf6566ed2015-03-24 11:13:38 -0700848 if ctx.Device() {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700849 if ctx.ModuleName() != "libcompiler_rt-extras" {
850 depNames.StaticLibs = append(depNames.StaticLibs, "libcompiler_rt-extras")
851 }
Colin Cross77b00fa2015-03-16 16:15:49 -0700852 // libgcc and libatomic have to be last on the command line
Colin Cross21b9a242015-03-24 14:15:58 -0700853 depNames.LateStaticLibs = append(depNames.LateStaticLibs, "libgcov", "libatomic", "libgcc")
Colin Crossed4cf0b2015-03-26 14:43:45 -0700854
Colin Cross18b6dc52015-04-28 13:20:37 -0700855 if !c.static() {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700856 depNames.SharedLibs = append(depNames.SharedLibs, c.systemSharedLibs(ctx)...)
857 }
Colin Cross577f6e42015-03-27 18:23:34 -0700858
Colin Crossfa138792015-04-24 17:31:52 -0700859 if c.Properties.Sdk_version != "" {
860 version := c.Properties.Sdk_version
Colin Cross577f6e42015-03-27 18:23:34 -0700861 depNames.SharedLibs = append(depNames.SharedLibs,
862 "ndk_libc."+version,
863 "ndk_libm."+version,
864 )
865 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800866 }
867
Colin Cross21b9a242015-03-24 14:15:58 -0700868 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -0800869}
870
Colin Crossed4cf0b2015-03-26 14:43:45 -0700871// ccLinkedInterface interface is used on ccLinked to deal with static or shared variants
872type ccLinkedInterface interface {
873 // Returns true if the build options for the module have selected a static or shared build
874 buildStatic() bool
875 buildShared() bool
876
877 // Sets whether a specific variant is static or shared
Colin Cross18b6dc52015-04-28 13:20:37 -0700878 setStatic(bool)
Colin Crossed4cf0b2015-03-26 14:43:45 -0700879
Colin Cross18b6dc52015-04-28 13:20:37 -0700880 // Returns whether a specific variant is a static library or binary
Colin Crossed4cf0b2015-03-26 14:43:45 -0700881 static() bool
Colin Cross18b6dc52015-04-28 13:20:37 -0700882
883 // Returns whether a module is a static binary
884 staticBinary() bool
Colin Crossed4cf0b2015-03-26 14:43:45 -0700885}
886
887var _ ccLinkedInterface = (*CCLibrary)(nil)
888var _ ccLinkedInterface = (*CCBinary)(nil)
889
Colin Crossfa138792015-04-24 17:31:52 -0700890func (c *CCLinked) static() bool {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700891 return c.dynamicProperties.VariantIsStatic
892}
893
Colin Cross18b6dc52015-04-28 13:20:37 -0700894func (c *CCLinked) staticBinary() bool {
895 return c.dynamicProperties.VariantIsStaticBinary
Colin Crossed4cf0b2015-03-26 14:43:45 -0700896}
897
Colin Cross18b6dc52015-04-28 13:20:37 -0700898func (c *CCLinked) setStatic(static bool) {
899 c.dynamicProperties.VariantIsStatic = static
Colin Crossed4cf0b2015-03-26 14:43:45 -0700900}
901
Colin Cross28344522015-04-22 13:07:53 -0700902type ccExportedFlagsProducer interface {
903 exportedFlags() []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800904}
905
906//
907// Combined static+shared libraries
908//
909
Colin Cross97ba0732015-03-23 17:50:24 -0700910type CCLibrary struct {
Colin Crossfa138792015-04-24 17:31:52 -0700911 CCLinked
Colin Cross3f40fa42015-01-30 17:27:36 -0800912
Colin Cross28344522015-04-22 13:07:53 -0700913 reuseFrom ccLibraryInterface
914 reuseObjFiles []string
915 objFiles []string
916 exportFlags []string
917 out string
Colin Cross3f40fa42015-01-30 17:27:36 -0800918
Colin Cross97ba0732015-03-23 17:50:24 -0700919 LibraryProperties struct {
Colin Cross3f40fa42015-01-30 17:27:36 -0800920 BuildStatic bool `blueprint:"mutated"`
921 BuildShared bool `blueprint:"mutated"`
Colin Crossed4cf0b2015-03-26 14:43:45 -0700922 Static struct {
Colin Cross2732e9a2015-04-28 13:23:52 -0700923 Srcs []string `android:"arch_variant"`
924 Cflags []string `android:"arch_variant"`
925 Whole_static_libs []string `android:"arch_variant"`
926 Static_libs []string `android:"arch_variant"`
927 Shared_libs []string `android:"arch_variant"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800928 } `android:"arch_variant"`
929 Shared struct {
Colin Cross2732e9a2015-04-28 13:23:52 -0700930 Srcs []string `android:"arch_variant"`
931 Cflags []string `android:"arch_variant"`
932 Whole_static_libs []string `android:"arch_variant"`
933 Static_libs []string `android:"arch_variant"`
934 Shared_libs []string `android:"arch_variant"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800935 } `android:"arch_variant"`
936 }
937}
938
Colin Crossed4cf0b2015-03-26 14:43:45 -0700939func (c *CCLibrary) buildStatic() bool {
940 return c.LibraryProperties.BuildStatic
941}
942
943func (c *CCLibrary) buildShared() bool {
944 return c.LibraryProperties.BuildShared
945}
946
Colin Cross97ba0732015-03-23 17:50:24 -0700947type ccLibraryInterface interface {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700948 ccLinkedInterface
Colin Cross97ba0732015-03-23 17:50:24 -0700949 ccLibrary() *CCLibrary
Colin Crossed4cf0b2015-03-26 14:43:45 -0700950 setReuseFrom(ccLibraryInterface)
951 getReuseFrom() ccLibraryInterface
952 getReuseObjFiles() []string
Colin Cross97ba0732015-03-23 17:50:24 -0700953 allObjFiles() []string
Colin Crossc472d572015-03-17 15:06:21 -0700954}
955
Colin Crossed4cf0b2015-03-26 14:43:45 -0700956var _ ccLibraryInterface = (*CCLibrary)(nil)
957
Colin Cross97ba0732015-03-23 17:50:24 -0700958func (c *CCLibrary) ccLibrary() *CCLibrary {
959 return c
Colin Cross3f40fa42015-01-30 17:27:36 -0800960}
961
Colin Cross97ba0732015-03-23 17:50:24 -0700962func NewCCLibrary(library *CCLibrary, module CCModuleType,
963 hod common.HostOrDeviceSupported) (blueprint.Module, []interface{}) {
964
Colin Crossfa138792015-04-24 17:31:52 -0700965 return newCCDynamic(&library.CCLinked, module, hod, common.MultilibBoth,
Colin Cross97ba0732015-03-23 17:50:24 -0700966 &library.LibraryProperties)
967}
968
969func CCLibraryFactory() (blueprint.Module, []interface{}) {
970 module := &CCLibrary{}
971
972 module.LibraryProperties.BuildShared = true
973 module.LibraryProperties.BuildStatic = true
974
975 return NewCCLibrary(module, module, common.HostAndDeviceSupported)
976}
977
Colin Cross0676e2d2015-04-24 17:39:18 -0700978func (c *CCLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Crosse11befc2015-04-27 17:49:17 -0700979 depNames = c.CCLinked.depNames(ctx, depNames)
Colin Cross2732e9a2015-04-28 13:23:52 -0700980 if c.static() {
981 depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.LibraryProperties.Static.Whole_static_libs...)
982 depNames.StaticLibs = append(depNames.StaticLibs, c.LibraryProperties.Static.Static_libs...)
983 depNames.SharedLibs = append(depNames.SharedLibs, c.LibraryProperties.Static.Shared_libs...)
984 } else {
Colin Crossf6566ed2015-03-24 11:13:38 -0700985 if ctx.Device() {
Colin Cross21b9a242015-03-24 14:15:58 -0700986 depNames.CrtBegin = "crtbegin_so"
987 depNames.CrtEnd = "crtend_so"
Colin Cross3f40fa42015-01-30 17:27:36 -0800988 }
Colin Cross2732e9a2015-04-28 13:23:52 -0700989 depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.LibraryProperties.Shared.Whole_static_libs...)
990 depNames.StaticLibs = append(depNames.StaticLibs, c.LibraryProperties.Shared.Static_libs...)
991 depNames.SharedLibs = append(depNames.SharedLibs, c.LibraryProperties.Shared.Shared_libs...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800992 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800993
Colin Cross21b9a242015-03-24 14:15:58 -0700994 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -0800995}
996
Colin Cross97ba0732015-03-23 17:50:24 -0700997func (c *CCLibrary) outputFile() string {
Colin Cross3f40fa42015-01-30 17:27:36 -0800998 return c.out
999}
1000
Colin Crossed4cf0b2015-03-26 14:43:45 -07001001func (c *CCLibrary) getReuseObjFiles() []string {
1002 return c.reuseObjFiles
1003}
1004
1005func (c *CCLibrary) setReuseFrom(reuseFrom ccLibraryInterface) {
1006 c.reuseFrom = reuseFrom
1007}
1008
1009func (c *CCLibrary) getReuseFrom() ccLibraryInterface {
1010 return c.reuseFrom
1011}
1012
Colin Cross97ba0732015-03-23 17:50:24 -07001013func (c *CCLibrary) allObjFiles() []string {
Colin Cross3f40fa42015-01-30 17:27:36 -08001014 return c.objFiles
1015}
1016
Colin Cross28344522015-04-22 13:07:53 -07001017func (c *CCLibrary) exportedFlags() []string {
1018 return c.exportFlags
Colin Cross3f40fa42015-01-30 17:27:36 -08001019}
1020
Colin Cross0676e2d2015-04-24 17:39:18 -07001021func (c *CCLibrary) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Crosse11befc2015-04-27 17:49:17 -07001022 flags = c.CCLinked.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001023
Colin Cross97ba0732015-03-23 17:50:24 -07001024 flags.CFlags = append(flags.CFlags, "-fPIC")
Colin Cross3f40fa42015-01-30 17:27:36 -08001025
Colin Cross18b6dc52015-04-28 13:20:37 -07001026 if !c.static() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001027 libName := ctx.ModuleName()
1028 // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead
1029 sharedFlag := "-Wl,-shared"
Colin Crossfa138792015-04-24 17:31:52 -07001030 if c.Properties.Clang || ctx.Host() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001031 sharedFlag = "-shared"
1032 }
Colin Crossf6566ed2015-03-24 11:13:38 -07001033 if ctx.Device() {
Colin Cross97ba0732015-03-23 17:50:24 -07001034 flags.LdFlags = append(flags.LdFlags, "-nostdlib")
Colin Cross3f40fa42015-01-30 17:27:36 -08001035 }
Colin Cross97ba0732015-03-23 17:50:24 -07001036
1037 flags.LdFlags = append(flags.LdFlags,
1038 "-Wl,--gc-sections",
1039 sharedFlag,
1040 "-Wl,-soname,"+libName+sharedLibraryExtension,
1041 )
Colin Cross3f40fa42015-01-30 17:27:36 -08001042 }
Colin Cross97ba0732015-03-23 17:50:24 -07001043
1044 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001045}
1046
Colin Cross97ba0732015-03-23 17:50:24 -07001047func (c *CCLibrary) compileStaticLibrary(ctx common.AndroidModuleContext,
1048 flags CCFlags, deps CCDeps, objFiles []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001049
1050 staticFlags := flags
Colin Cross97ba0732015-03-23 17:50:24 -07001051 staticFlags.CFlags = append(staticFlags.CFlags, c.LibraryProperties.Static.Cflags...)
Colin Cross581c1892015-04-07 16:50:10 -07001052 objFilesStatic := c.customCompileObjs(ctx, staticFlags, common.DeviceStaticLibrary,
Colin Cross97ba0732015-03-23 17:50:24 -07001053 c.LibraryProperties.Static.Srcs)
Colin Cross3f40fa42015-01-30 17:27:36 -08001054
1055 objFiles = append(objFiles, objFilesStatic...)
Colin Cross21b9a242015-03-24 14:15:58 -07001056 objFiles = append(objFiles, deps.WholeStaticLibObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001057
1058 outputFile := filepath.Join(common.ModuleOutDir(ctx), ctx.ModuleName()+staticLibraryExtension)
1059
1060 TransformObjToStaticLib(ctx, objFiles, ccFlagsToBuilderFlags(flags), outputFile)
1061
1062 c.objFiles = objFiles
1063 c.out = outputFile
Colin Crossfa138792015-04-24 17:31:52 -07001064 includeDirs := pathtools.PrefixPaths(c.Properties.Export_include_dirs, common.ModuleSrcDir(ctx))
Colin Cross28344522015-04-22 13:07:53 -07001065 c.exportFlags = []string{includeDirsToFlags(includeDirs)}
Colin Cross3f40fa42015-01-30 17:27:36 -08001066
1067 ctx.CheckbuildFile(outputFile)
1068}
1069
Colin Cross97ba0732015-03-23 17:50:24 -07001070func (c *CCLibrary) compileSharedLibrary(ctx common.AndroidModuleContext,
1071 flags CCFlags, deps CCDeps, objFiles []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001072
1073 sharedFlags := flags
Colin Cross97ba0732015-03-23 17:50:24 -07001074 sharedFlags.CFlags = append(sharedFlags.CFlags, c.LibraryProperties.Shared.Cflags...)
Colin Cross581c1892015-04-07 16:50:10 -07001075 objFilesShared := c.customCompileObjs(ctx, sharedFlags, common.DeviceSharedLibrary,
Colin Cross97ba0732015-03-23 17:50:24 -07001076 c.LibraryProperties.Shared.Srcs)
Colin Cross3f40fa42015-01-30 17:27:36 -08001077
1078 objFiles = append(objFiles, objFilesShared...)
1079
1080 outputFile := filepath.Join(common.ModuleOutDir(ctx), ctx.ModuleName()+sharedLibraryExtension)
1081
Colin Cross97ba0732015-03-23 17:50:24 -07001082 TransformObjToDynamicBinary(ctx, objFiles, deps.SharedLibs, deps.StaticLibs,
Colin Crossed4cf0b2015-03-26 14:43:45 -07001083 deps.LateStaticLibs, deps.WholeStaticLibs, deps.CrtBegin, deps.CrtEnd, false,
Colin Cross77b00fa2015-03-16 16:15:49 -07001084 ccFlagsToBuilderFlags(flags), outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08001085
1086 c.out = outputFile
Colin Crossfa138792015-04-24 17:31:52 -07001087 includeDirs := pathtools.PrefixPaths(c.Properties.Export_include_dirs, common.ModuleSrcDir(ctx))
Colin Cross28344522015-04-22 13:07:53 -07001088 c.exportFlags = []string{includeDirsToFlags(includeDirs)}
Colin Cross3f40fa42015-01-30 17:27:36 -08001089}
1090
Colin Cross97ba0732015-03-23 17:50:24 -07001091func (c *CCLibrary) compileModule(ctx common.AndroidModuleContext,
1092 flags CCFlags, deps CCDeps, objFiles []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001093
1094 // Reuse the object files from the matching static library if it exists
Colin Crossed4cf0b2015-03-26 14:43:45 -07001095 if c.getReuseFrom().ccLibrary() == c {
1096 c.reuseObjFiles = objFiles
Colin Cross3f40fa42015-01-30 17:27:36 -08001097 } else {
Colin Cross2732e9a2015-04-28 13:23:52 -07001098 if c.getReuseFrom().ccLibrary().LibraryProperties.Static.Cflags == nil &&
1099 c.LibraryProperties.Shared.Cflags == nil {
1100 objFiles = append([]string(nil), c.getReuseFrom().getReuseObjFiles()...)
1101 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001102 }
1103
Colin Crossed4cf0b2015-03-26 14:43:45 -07001104 if c.static() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001105 c.compileStaticLibrary(ctx, flags, deps, objFiles)
1106 } else {
1107 c.compileSharedLibrary(ctx, flags, deps, objFiles)
1108 }
1109}
1110
Colin Cross97ba0732015-03-23 17:50:24 -07001111func (c *CCLibrary) installStaticLibrary(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001112 // Static libraries do not get installed.
1113}
1114
Colin Cross97ba0732015-03-23 17:50:24 -07001115func (c *CCLibrary) installSharedLibrary(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001116 installDir := "lib"
Colin Cross97ba0732015-03-23 17:50:24 -07001117 if flags.Toolchain.Is64Bit() {
Dan Albertc403f7c2015-03-18 14:01:18 -07001118 installDir = "lib64"
1119 }
1120
Colin Crossfa138792015-04-24 17:31:52 -07001121 ctx.InstallFile(filepath.Join(installDir, c.Properties.Relative_install_path), c.out)
Dan Albertc403f7c2015-03-18 14:01:18 -07001122}
1123
Colin Cross97ba0732015-03-23 17:50:24 -07001124func (c *CCLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001125 if c.static() {
Dan Albertc403f7c2015-03-18 14:01:18 -07001126 c.installStaticLibrary(ctx, flags)
1127 } else {
1128 c.installSharedLibrary(ctx, flags)
1129 }
1130}
1131
Colin Cross3f40fa42015-01-30 17:27:36 -08001132//
1133// Objects (for crt*.o)
1134//
1135
1136type ccObject struct {
Colin Crossfa138792015-04-24 17:31:52 -07001137 CCBase
Colin Cross3f40fa42015-01-30 17:27:36 -08001138 out string
1139}
1140
Colin Cross97ba0732015-03-23 17:50:24 -07001141func CCObjectFactory() (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001142 module := &ccObject{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001143
Colin Crossfa138792015-04-24 17:31:52 -07001144 return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth)
Colin Cross3f40fa42015-01-30 17:27:36 -08001145}
1146
1147func (*ccObject) AndroidDynamicDependencies(ctx common.AndroidDynamicDependerModuleContext) []string {
1148 // object files can't have any dynamic dependencies
1149 return nil
1150}
1151
Colin Cross0676e2d2015-04-24 17:39:18 -07001152func (*ccObject) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Cross21b9a242015-03-24 14:15:58 -07001153 // object files can't have any dynamic dependencies
1154 return CCDeps{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001155}
1156
1157func (c *ccObject) compileModule(ctx common.AndroidModuleContext,
Colin Cross97ba0732015-03-23 17:50:24 -07001158 flags CCFlags, deps CCDeps, objFiles []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001159
Colin Cross97ba0732015-03-23 17:50:24 -07001160 objFiles = append(objFiles, deps.ObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001161
1162 var outputFile string
1163 if len(objFiles) == 1 {
1164 outputFile = objFiles[0]
1165 } else {
1166 outputFile = filepath.Join(common.ModuleOutDir(ctx), ctx.ModuleName()+".o")
1167 TransformObjsToObj(ctx, objFiles, ccFlagsToBuilderFlags(flags), outputFile)
1168 }
1169
1170 c.out = outputFile
1171
1172 ctx.CheckbuildFile(outputFile)
1173}
1174
Colin Cross97ba0732015-03-23 17:50:24 -07001175func (c *ccObject) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001176 // Object files do not get installed.
1177}
1178
Colin Cross3f40fa42015-01-30 17:27:36 -08001179func (c *ccObject) outputFile() string {
1180 return c.out
1181}
1182
1183//
1184// Executables
1185//
1186
Colin Cross97ba0732015-03-23 17:50:24 -07001187type CCBinary struct {
Colin Crossfa138792015-04-24 17:31:52 -07001188 CCLinked
Dan Albertc403f7c2015-03-18 14:01:18 -07001189 out string
Colin Cross97ba0732015-03-23 17:50:24 -07001190 BinaryProperties struct {
1191 // static_executable: compile executable with -static
1192 Static_executable bool
1193
1194 // stem: set the name of the output
1195 Stem string `android:"arch_variant"`
1196
Colin Cross4ae185c2015-03-26 15:12:10 -07001197 // suffix: append to the name of the output
1198 Suffix string `android:"arch_variant"`
1199
Colin Cross97ba0732015-03-23 17:50:24 -07001200 // prefix_symbols: if set, add an extra objcopy --prefix-symbols= step
1201 Prefix_symbols string
1202 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001203}
1204
Colin Crossed4cf0b2015-03-26 14:43:45 -07001205func (c *CCBinary) buildStatic() bool {
1206 return c.BinaryProperties.Static_executable
1207}
1208
1209func (c *CCBinary) buildShared() bool {
1210 return !c.BinaryProperties.Static_executable
1211}
1212
Colin Cross97ba0732015-03-23 17:50:24 -07001213func (c *CCBinary) getStem(ctx common.AndroidModuleContext) string {
Colin Cross4ae185c2015-03-26 15:12:10 -07001214 stem := ctx.ModuleName()
Colin Cross97ba0732015-03-23 17:50:24 -07001215 if c.BinaryProperties.Stem != "" {
Colin Cross4ae185c2015-03-26 15:12:10 -07001216 stem = c.BinaryProperties.Stem
Colin Cross3f40fa42015-01-30 17:27:36 -08001217 }
Colin Cross4ae185c2015-03-26 15:12:10 -07001218
1219 return stem + c.BinaryProperties.Suffix
Colin Cross3f40fa42015-01-30 17:27:36 -08001220}
1221
Colin Cross0676e2d2015-04-24 17:39:18 -07001222func (c *CCBinary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Crosse11befc2015-04-27 17:49:17 -07001223 depNames = c.CCLinked.depNames(ctx, depNames)
Colin Crossf6566ed2015-03-24 11:13:38 -07001224 if ctx.Device() {
Colin Cross97ba0732015-03-23 17:50:24 -07001225 if c.BinaryProperties.Static_executable {
Colin Cross21b9a242015-03-24 14:15:58 -07001226 depNames.CrtBegin = "crtbegin_static"
Colin Cross3f40fa42015-01-30 17:27:36 -08001227 } else {
Colin Cross21b9a242015-03-24 14:15:58 -07001228 depNames.CrtBegin = "crtbegin_dynamic"
Colin Cross3f40fa42015-01-30 17:27:36 -08001229 }
Colin Cross21b9a242015-03-24 14:15:58 -07001230 depNames.CrtEnd = "crtend_android"
Colin Crossed4cf0b2015-03-26 14:43:45 -07001231
1232 if c.BinaryProperties.Static_executable {
1233 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
1234 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
1235 // move them to the beginning of deps.LateStaticLibs
1236 var groupLibs []string
1237 depNames.StaticLibs, groupLibs = filterList(depNames.StaticLibs,
1238 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
1239 depNames.LateStaticLibs = append(groupLibs, depNames.LateStaticLibs...)
1240 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001241 }
Colin Cross21b9a242015-03-24 14:15:58 -07001242 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -08001243}
1244
Colin Cross97ba0732015-03-23 17:50:24 -07001245func NewCCBinary(binary *CCBinary, module CCModuleType,
Colin Cross1f8f2342015-03-26 16:09:47 -07001246 hod common.HostOrDeviceSupported, props ...interface{}) (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001247
Colin Cross1f8f2342015-03-26 16:09:47 -07001248 props = append(props, &binary.BinaryProperties)
1249
Colin Crossfa138792015-04-24 17:31:52 -07001250 return newCCDynamic(&binary.CCLinked, module, hod, common.MultilibFirst, props...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001251}
1252
Colin Cross97ba0732015-03-23 17:50:24 -07001253func CCBinaryFactory() (blueprint.Module, []interface{}) {
1254 module := &CCBinary{}
1255
1256 return NewCCBinary(module, module, common.HostAndDeviceSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001257}
1258
Colin Cross18b6dc52015-04-28 13:20:37 -07001259func (c *CCBinary) ModifyProperties(ctx common.AndroidBaseContext) {
1260 if c.BinaryProperties.Static_executable {
1261 c.dynamicProperties.VariantIsStaticBinary = true
1262 }
1263}
1264
Colin Cross0676e2d2015-04-24 17:39:18 -07001265func (c *CCBinary) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Crosse11befc2015-04-27 17:49:17 -07001266 flags = c.CCLinked.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001267
Colin Cross97ba0732015-03-23 17:50:24 -07001268 flags.CFlags = append(flags.CFlags, "-fpie")
1269
Colin Crossf6566ed2015-03-24 11:13:38 -07001270 if ctx.Device() {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001271 if c.BinaryProperties.Static_executable {
1272 // Clang driver needs -static to create static executable.
1273 // However, bionic/linker uses -shared to overwrite.
1274 // Linker for x86 targets does not allow coexistance of -static and -shared,
1275 // so we add -static only if -shared is not used.
1276 if !inList("-shared", flags.LdFlags) {
1277 flags.LdFlags = append(flags.LdFlags, "-static")
1278 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001279
Colin Crossed4cf0b2015-03-26 14:43:45 -07001280 flags.LdFlags = append(flags.LdFlags,
1281 "-nostdlib",
1282 "-Bstatic",
1283 "-Wl,--gc-sections",
1284 )
1285
1286 } else {
1287 linker := "/system/bin/linker"
1288 if flags.Toolchain.Is64Bit() {
1289 linker = "/system/bin/linker64"
1290 }
1291
1292 flags.LdFlags = append(flags.LdFlags,
1293 "-nostdlib",
1294 "-Bdynamic",
1295 fmt.Sprintf("-Wl,-dynamic-linker,%s", linker),
1296 "-Wl,--gc-sections",
1297 "-Wl,-z,nocopyreloc",
1298 )
1299 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001300 }
1301
Colin Cross97ba0732015-03-23 17:50:24 -07001302 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001303}
1304
Colin Cross97ba0732015-03-23 17:50:24 -07001305func (c *CCBinary) compileModule(ctx common.AndroidModuleContext,
1306 flags CCFlags, deps CCDeps, objFiles []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001307
Colin Crossfa138792015-04-24 17:31:52 -07001308 if !c.BinaryProperties.Static_executable && inList("libc", c.Properties.Static_libs) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001309 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
1310 "from static libs or set static_executable: true")
1311 }
1312
1313 outputFile := filepath.Join(common.ModuleOutDir(ctx), c.getStem(ctx))
Dan Albertc403f7c2015-03-18 14:01:18 -07001314 c.out = outputFile
Colin Crossbfae8852015-03-26 14:44:11 -07001315 if c.BinaryProperties.Prefix_symbols != "" {
1316 afterPrefixSymbols := outputFile
1317 outputFile = outputFile + ".intermediate"
1318 TransformBinaryPrefixSymbols(ctx, c.BinaryProperties.Prefix_symbols, outputFile,
1319 ccFlagsToBuilderFlags(flags), afterPrefixSymbols)
1320 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001321
Colin Cross97ba0732015-03-23 17:50:24 -07001322 TransformObjToDynamicBinary(ctx, objFiles, deps.SharedLibs, deps.StaticLibs,
Colin Crossed4cf0b2015-03-26 14:43:45 -07001323 deps.LateStaticLibs, deps.WholeStaticLibs, deps.CrtBegin, deps.CrtEnd, true,
Colin Cross77b00fa2015-03-16 16:15:49 -07001324 ccFlagsToBuilderFlags(flags), outputFile)
Dan Albertc403f7c2015-03-18 14:01:18 -07001325}
Colin Cross3f40fa42015-01-30 17:27:36 -08001326
Colin Cross97ba0732015-03-23 17:50:24 -07001327func (c *CCBinary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Colin Crossfa138792015-04-24 17:31:52 -07001328 ctx.InstallFile(filepath.Join("bin", c.Properties.Relative_install_path), c.out)
Dan Albertc403f7c2015-03-18 14:01:18 -07001329}
1330
Colin Cross9ffb4f52015-04-24 17:48:09 -07001331type CCTest struct {
Colin Cross97ba0732015-03-23 17:50:24 -07001332 CCBinary
Colin Cross6b290692015-03-19 14:05:33 -07001333
Colin Cross9ffb4f52015-04-24 17:48:09 -07001334 TestProperties struct {
Colin Cross6b290692015-03-19 14:05:33 -07001335 // test_per_src: Create a separate test for each source file. Useful when there is
1336 // global state that can not be torn down and reset between each test suite.
1337 Test_per_src bool
1338 }
Dan Albertc403f7c2015-03-18 14:01:18 -07001339}
1340
Colin Cross9ffb4f52015-04-24 17:48:09 -07001341func (c *CCTest) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Cross0676e2d2015-04-24 17:39:18 -07001342 flags = c.CCBinary.flags(ctx, flags)
Dan Albertc403f7c2015-03-18 14:01:18 -07001343
Colin Cross97ba0732015-03-23 17:50:24 -07001344 flags.CFlags = append(flags.CFlags, "-DGTEST_HAS_STD_STRING")
Colin Crossf6566ed2015-03-24 11:13:38 -07001345 if ctx.Host() {
Colin Cross97ba0732015-03-23 17:50:24 -07001346 flags.CFlags = append(flags.CFlags, "-O0", "-g")
Colin Cross28344522015-04-22 13:07:53 -07001347 flags.LdFlags = append(flags.LdFlags, "-lpthread")
Dan Albertc403f7c2015-03-18 14:01:18 -07001348 }
1349
1350 // TODO(danalbert): Make gtest export its dependencies.
Colin Cross28344522015-04-22 13:07:53 -07001351 flags.CFlags = append(flags.CFlags,
1352 "-I"+filepath.Join(ctx.AConfig().SrcDir(), "external/gtest/include"))
Dan Albertc403f7c2015-03-18 14:01:18 -07001353
Colin Cross21b9a242015-03-24 14:15:58 -07001354 return flags
Dan Albertc403f7c2015-03-18 14:01:18 -07001355}
1356
Colin Cross9ffb4f52015-04-24 17:48:09 -07001357func (c *CCTest) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Cross0676e2d2015-04-24 17:39:18 -07001358 depNames = c.CCBinary.depNames(ctx, depNames)
Colin Cross21b9a242015-03-24 14:15:58 -07001359 depNames.StaticLibs = append(depNames.StaticLibs, "libgtest", "libgtest_main")
1360 return depNames
Dan Albertc403f7c2015-03-18 14:01:18 -07001361}
1362
Colin Cross9ffb4f52015-04-24 17:48:09 -07001363func (c *CCTest) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Colin Crossf6566ed2015-03-24 11:13:38 -07001364 if ctx.Device() {
Tim Kilbourn5ccc7302015-03-19 10:02:21 -07001365 ctx.InstallFile("../data/nativetest/"+ctx.ModuleName(), c.out)
Dan Albertc403f7c2015-03-18 14:01:18 -07001366 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001367 c.CCBinary.installModule(ctx, flags)
Dan Albertc403f7c2015-03-18 14:01:18 -07001368 }
1369}
1370
Colin Cross9ffb4f52015-04-24 17:48:09 -07001371func (c *CCTest) testPerSrc() bool {
1372 return c.TestProperties.Test_per_src
Colin Cross6b290692015-03-19 14:05:33 -07001373}
1374
Colin Cross9ffb4f52015-04-24 17:48:09 -07001375func (c *CCTest) test() *CCTest {
1376 return c
1377}
1378
1379func NewCCTest(test *CCTest, module CCModuleType,
1380 hod common.HostOrDeviceSupported, props ...interface{}) (blueprint.Module, []interface{}) {
1381
1382 props = append(props, &test.TestProperties)
1383
1384 return NewCCBinary(&test.CCBinary, module, hod, props...)
1385}
1386
1387func CCTestFactory() (blueprint.Module, []interface{}) {
1388 module := &CCTest{}
1389
1390 return NewCCTest(module, module, common.HostAndDeviceSupported)
1391}
1392
1393type testPerSrc interface {
1394 test() *CCTest
1395 testPerSrc() bool
1396}
1397
1398var _ testPerSrc = (*CCTest)(nil)
1399
Colin Cross6b290692015-03-19 14:05:33 -07001400func TestPerSrcMutator(mctx blueprint.EarlyMutatorContext) {
Colin Cross9ffb4f52015-04-24 17:48:09 -07001401 if test, ok := mctx.Module().(testPerSrc); ok {
1402 if test.testPerSrc() {
1403 testNames := make([]string, len(test.test().Properties.Srcs))
1404 for i, src := range test.test().Properties.Srcs {
Colin Cross6b290692015-03-19 14:05:33 -07001405 testNames[i] = strings.TrimSuffix(src, filepath.Ext(src))
1406 }
1407 tests := mctx.CreateLocalVariations(testNames...)
Colin Cross9ffb4f52015-04-24 17:48:09 -07001408 for i, src := range test.test().Properties.Srcs {
1409 tests[i].(testPerSrc).test().Properties.Srcs = []string{src}
1410 tests[i].(testPerSrc).test().BinaryProperties.Stem = testNames[i]
Colin Cross6b290692015-03-19 14:05:33 -07001411 }
1412 }
1413 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001414}
1415
1416//
1417// Static library
1418//
1419
Colin Cross97ba0732015-03-23 17:50:24 -07001420func CCLibraryStaticFactory() (blueprint.Module, []interface{}) {
1421 module := &CCLibrary{}
1422 module.LibraryProperties.BuildStatic = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001423
Colin Cross97ba0732015-03-23 17:50:24 -07001424 return NewCCLibrary(module, module, common.HostAndDeviceSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001425}
1426
1427//
1428// Shared libraries
1429//
1430
Colin Cross97ba0732015-03-23 17:50:24 -07001431func CCLibrarySharedFactory() (blueprint.Module, []interface{}) {
1432 module := &CCLibrary{}
1433 module.LibraryProperties.BuildShared = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001434
Colin Cross97ba0732015-03-23 17:50:24 -07001435 return NewCCLibrary(module, module, common.HostAndDeviceSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001436}
1437
1438//
1439// Host static library
1440//
1441
Colin Cross97ba0732015-03-23 17:50:24 -07001442func CCLibraryHostStaticFactory() (blueprint.Module, []interface{}) {
1443 module := &CCLibrary{}
1444 module.LibraryProperties.BuildStatic = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001445
Colin Cross97ba0732015-03-23 17:50:24 -07001446 return NewCCLibrary(module, module, common.HostSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001447}
1448
1449//
1450// Host Shared libraries
1451//
1452
Colin Cross97ba0732015-03-23 17:50:24 -07001453func CCLibraryHostSharedFactory() (blueprint.Module, []interface{}) {
1454 module := &CCLibrary{}
1455 module.LibraryProperties.BuildShared = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001456
Colin Cross97ba0732015-03-23 17:50:24 -07001457 return NewCCLibrary(module, module, common.HostSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001458}
1459
1460//
1461// Host Binaries
1462//
1463
Colin Cross97ba0732015-03-23 17:50:24 -07001464func CCBinaryHostFactory() (blueprint.Module, []interface{}) {
1465 module := &CCBinary{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001466
Colin Cross97ba0732015-03-23 17:50:24 -07001467 return NewCCBinary(module, module, common.HostSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001468}
1469
1470//
Colin Cross1f8f2342015-03-26 16:09:47 -07001471// Host Tests
1472//
1473
1474func CCTestHostFactory() (blueprint.Module, []interface{}) {
Colin Cross9ffb4f52015-04-24 17:48:09 -07001475 module := &CCTest{}
Colin Cross1f8f2342015-03-26 16:09:47 -07001476 return NewCCBinary(&module.CCBinary, module, common.HostSupported,
Colin Cross9ffb4f52015-04-24 17:48:09 -07001477 &module.TestProperties)
Colin Cross1f8f2342015-03-26 16:09:47 -07001478}
1479
1480//
Colin Cross3f40fa42015-01-30 17:27:36 -08001481// Device libraries shipped with gcc
1482//
1483
1484type toolchainLibrary struct {
Colin Cross97ba0732015-03-23 17:50:24 -07001485 CCLibrary
Colin Cross3f40fa42015-01-30 17:27:36 -08001486}
1487
1488func (*toolchainLibrary) AndroidDynamicDependencies(ctx common.AndroidDynamicDependerModuleContext) []string {
1489 // toolchain libraries can't have any dependencies
1490 return nil
1491}
1492
Colin Cross0676e2d2015-04-24 17:39:18 -07001493func (*toolchainLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Cross3f40fa42015-01-30 17:27:36 -08001494 // toolchain libraries can't have any dependencies
Colin Cross21b9a242015-03-24 14:15:58 -07001495 return CCDeps{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001496}
1497
Colin Cross97ba0732015-03-23 17:50:24 -07001498func ToolchainLibraryFactory() (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001499 module := &toolchainLibrary{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001500
Colin Cross97ba0732015-03-23 17:50:24 -07001501 module.LibraryProperties.BuildStatic = true
1502
Colin Crossfa138792015-04-24 17:31:52 -07001503 return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth,
Colin Cross21b9a242015-03-24 14:15:58 -07001504 &module.LibraryProperties)
Colin Cross3f40fa42015-01-30 17:27:36 -08001505}
1506
1507func (c *toolchainLibrary) compileModule(ctx common.AndroidModuleContext,
Colin Cross97ba0732015-03-23 17:50:24 -07001508 flags CCFlags, deps CCDeps, objFiles []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001509
1510 libName := ctx.ModuleName() + staticLibraryExtension
1511 outputFile := filepath.Join(common.ModuleOutDir(ctx), libName)
1512
1513 CopyGccLib(ctx, libName, ccFlagsToBuilderFlags(flags), outputFile)
1514
1515 c.out = outputFile
1516
1517 ctx.CheckbuildFile(outputFile)
1518}
1519
Colin Cross97ba0732015-03-23 17:50:24 -07001520func (c *toolchainLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001521 // Toolchain libraries do not get installed.
1522}
1523
Dan Albertbe961682015-03-18 23:38:50 -07001524// NDK prebuilt libraries.
1525//
1526// These differ from regular prebuilts in that they aren't stripped and usually aren't installed
1527// either (with the exception of the shared STLs, which are installed to the app's directory rather
1528// than to the system image).
1529
1530func getNdkLibDir(ctx common.AndroidModuleContext, toolchain Toolchain, version string) string {
1531 return fmt.Sprintf("%s/prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/lib",
Colin Cross1332b002015-04-07 17:11:30 -07001532 ctx.AConfig().SrcDir(), version, toolchain.Name())
Dan Albertbe961682015-03-18 23:38:50 -07001533}
1534
1535type ndkPrebuiltLibrary struct {
1536 CCLibrary
1537}
1538
1539func (*ndkPrebuiltLibrary) AndroidDynamicDependencies(
1540 ctx common.AndroidDynamicDependerModuleContext) []string {
1541
1542 // NDK libraries can't have any dependencies
1543 return nil
1544}
1545
Colin Cross0676e2d2015-04-24 17:39:18 -07001546func (*ndkPrebuiltLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Dan Albertbe961682015-03-18 23:38:50 -07001547 // NDK libraries can't have any dependencies
1548 return CCDeps{}
1549}
1550
1551func NdkPrebuiltLibraryFactory() (blueprint.Module, []interface{}) {
1552 module := &ndkPrebuiltLibrary{}
1553 module.LibraryProperties.BuildShared = true
1554 return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
1555}
1556
1557func (c *ndkPrebuiltLibrary) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
1558 deps CCDeps, objFiles []string) {
1559 // A null build step, but it sets up the output path.
1560 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
1561 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
1562 }
1563
Colin Crossfa138792015-04-24 17:31:52 -07001564 includeDirs := pathtools.PrefixPaths(c.Properties.Export_include_dirs, common.ModuleSrcDir(ctx))
Colin Cross28344522015-04-22 13:07:53 -07001565 c.exportFlags = []string{common.JoinWithPrefix(includeDirs, "-isystem ")}
Dan Albertbe961682015-03-18 23:38:50 -07001566
1567 // NDK prebuilt libraries are named like: ndk_LIBNAME.SDK_VERSION.
1568 // We want to translate to just LIBNAME.
1569 libName := strings.Split(strings.TrimPrefix(ctx.ModuleName(), "ndk_"), ".")[0]
Colin Crossfa138792015-04-24 17:31:52 -07001570 libDir := getNdkLibDir(ctx, flags.Toolchain, c.Properties.Sdk_version)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001571 c.out = filepath.Join(libDir, libName+sharedLibraryExtension)
Dan Albertbe961682015-03-18 23:38:50 -07001572}
1573
1574func (c *ndkPrebuiltLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
1575 // Toolchain libraries do not get installed.
1576}
1577
1578// The NDK STLs are slightly different from the prebuilt system libraries:
1579// * Are not specific to each platform version.
1580// * The libraries are not in a predictable location for each STL.
1581
1582type ndkPrebuiltStl struct {
1583 ndkPrebuiltLibrary
1584}
1585
1586type ndkPrebuiltStaticStl struct {
1587 ndkPrebuiltStl
1588}
1589
1590type ndkPrebuiltSharedStl struct {
1591 ndkPrebuiltStl
1592}
1593
1594func NdkPrebuiltSharedStlFactory() (blueprint.Module, []interface{}) {
1595 module := &ndkPrebuiltSharedStl{}
1596 module.LibraryProperties.BuildShared = true
1597 return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
1598}
1599
1600func NdkPrebuiltStaticStlFactory() (blueprint.Module, []interface{}) {
1601 module := &ndkPrebuiltStaticStl{}
1602 module.LibraryProperties.BuildStatic = true
1603 return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
1604}
1605
1606func getNdkStlLibDir(ctx common.AndroidModuleContext, toolchain Toolchain, stl string) string {
1607 gccVersion := toolchain.GccVersion()
1608 var libDir string
1609 switch stl {
1610 case "libstlport":
1611 libDir = "cxx-stl/stlport/libs"
1612 case "libc++":
1613 libDir = "cxx-stl/llvm-libc++/libs"
1614 case "libgnustl":
1615 libDir = fmt.Sprintf("cxx-stl/gnu-libstdc++/%s/libs", gccVersion)
1616 }
1617
1618 if libDir != "" {
Colin Cross1332b002015-04-07 17:11:30 -07001619 ndkSrcRoot := ctx.AConfig().SrcDir() + "/prebuilts/ndk/current/sources"
Dan Albertbe961682015-03-18 23:38:50 -07001620 return fmt.Sprintf("%s/%s/%s", ndkSrcRoot, libDir, ctx.Arch().Abi)
1621 }
1622
1623 ctx.ModuleErrorf("Unknown NDK STL: %s", stl)
1624 return ""
1625}
1626
1627func (c *ndkPrebuiltStl) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
1628 deps CCDeps, objFiles []string) {
1629 // A null build step, but it sets up the output path.
1630 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
1631 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
1632 }
1633
Colin Crossfa138792015-04-24 17:31:52 -07001634 includeDirs := pathtools.PrefixPaths(c.Properties.Export_include_dirs, common.ModuleSrcDir(ctx))
Colin Cross28344522015-04-22 13:07:53 -07001635 c.exportFlags = []string{includeDirsToFlags(includeDirs)}
Dan Albertbe961682015-03-18 23:38:50 -07001636
1637 libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_")
1638 libExt := sharedLibraryExtension
1639 if c.LibraryProperties.BuildStatic {
1640 libExt = staticLibraryExtension
1641 }
1642
1643 stlName := strings.TrimSuffix(libName, "_shared")
1644 stlName = strings.TrimSuffix(stlName, "_static")
1645 libDir := getNdkStlLibDir(ctx, flags.Toolchain, stlName)
1646 c.out = libDir + "/" + libName + libExt
1647}
1648
Colin Cross3f40fa42015-01-30 17:27:36 -08001649func LinkageMutator(mctx blueprint.EarlyMutatorContext) {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001650 if c, ok := mctx.Module().(ccLinkedInterface); ok {
Colin Cross3f40fa42015-01-30 17:27:36 -08001651 var modules []blueprint.Module
Colin Crossed4cf0b2015-03-26 14:43:45 -07001652 if c.buildStatic() && c.buildShared() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001653 modules = mctx.CreateLocalVariations("static", "shared")
Colin Cross18b6dc52015-04-28 13:20:37 -07001654 modules[0].(ccLinkedInterface).setStatic(true)
1655 modules[1].(ccLinkedInterface).setStatic(false)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001656 } else if c.buildStatic() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001657 modules = mctx.CreateLocalVariations("static")
Colin Cross18b6dc52015-04-28 13:20:37 -07001658 modules[0].(ccLinkedInterface).setStatic(true)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001659 } else if c.buildShared() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001660 modules = mctx.CreateLocalVariations("shared")
Colin Cross18b6dc52015-04-28 13:20:37 -07001661 modules[0].(ccLinkedInterface).setStatic(false)
Colin Cross3f40fa42015-01-30 17:27:36 -08001662 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001663 panic(fmt.Errorf("ccLibrary %q not static or shared", mctx.ModuleName()))
Colin Cross3f40fa42015-01-30 17:27:36 -08001664 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001665
1666 if _, ok := c.(ccLibraryInterface); ok {
1667 reuseFrom := modules[0].(ccLibraryInterface)
1668 for _, m := range modules {
1669 m.(ccLibraryInterface).setReuseFrom(reuseFrom)
Colin Cross3f40fa42015-01-30 17:27:36 -08001670 }
1671 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001672 }
1673}