blob: bd81dd7aedf6fdff8798facf7242cfea4ad66611 [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
33type Config interface {
34 SrcDir() string
35 PrebuiltOS() string
36}
37
38var (
39 HostPrebuiltTag = pctx.VariableConfigMethod("HostPrebuiltTag", Config.PrebuiltOS)
40 SrcDir = pctx.VariableConfigMethod("SrcDir", Config.SrcDir)
41
42 LibcRoot = pctx.StaticVariable("LibcRoot", "${SrcDir}/bionic/libc")
43 LibmRoot = pctx.StaticVariable("LibmRoot", "${SrcDir}/bionic/libm")
44)
45
46// Flags used by lots of devices. Putting them in package static variables will save bytes in
47// build.ninja so they aren't repeated for every file
48var (
49 commonGlobalCflags = []string{
50 "-DANDROID",
51 "-fmessage-length=0",
52 "-W",
53 "-Wall",
54 "-Wno-unused",
55 "-Winit-self",
56 "-Wpointer-arith",
57
58 // COMMON_RELEASE_CFLAGS
59 "-DNDEBUG",
60 "-UDEBUG",
61 }
62
63 deviceGlobalCflags = []string{
64 // TARGET_ERROR_FLAGS
65 "-Werror=return-type",
66 "-Werror=non-virtual-dtor",
67 "-Werror=address",
68 "-Werror=sequence-point",
69 }
70
71 hostGlobalCflags = []string{}
72
73 commonGlobalCppflags = []string{
74 "-Wsign-promo",
75 "-std=gnu++11",
76 }
77)
78
79func init() {
80 pctx.StaticVariable("commonGlobalCflags", strings.Join(commonGlobalCflags, " "))
81 pctx.StaticVariable("deviceGlobalCflags", strings.Join(deviceGlobalCflags, " "))
82 pctx.StaticVariable("hostGlobalCflags", strings.Join(hostGlobalCflags, " "))
83
84 pctx.StaticVariable("commonGlobalCppflags", strings.Join(commonGlobalCppflags, " "))
85
86 pctx.StaticVariable("commonClangGlobalCflags",
87 strings.Join(clangFilterUnknownCflags(commonGlobalCflags), " "))
88 pctx.StaticVariable("deviceClangGlobalCflags",
89 strings.Join(clangFilterUnknownCflags(deviceGlobalCflags), " "))
90 pctx.StaticVariable("hostClangGlobalCflags",
91 strings.Join(clangFilterUnknownCflags(hostGlobalCflags), " "))
Tim Kilbournf2948142015-03-11 12:03:03 -070092 pctx.StaticVariable("commonClangGlobalCppflags",
93 strings.Join(clangFilterUnknownCflags(commonGlobalCppflags), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -080094
95 // Everything in this list is a crime against abstraction and dependency tracking.
96 // Do not add anything to this list.
97 pctx.StaticVariable("commonGlobalIncludes", strings.Join([]string{
98 "-isystem ${SrcDir}/system/core/include",
99 "-isystem ${SrcDir}/hardware/libhardware/include",
100 "-isystem ${SrcDir}/hardware/libhardware_legacy/include",
101 "-isystem ${SrcDir}/hardware/ril/include",
102 "-isystem ${SrcDir}/libnativehelper/include",
103 "-isystem ${SrcDir}/frameworks/native/include",
104 "-isystem ${SrcDir}/frameworks/native/opengl/include",
105 "-isystem ${SrcDir}/frameworks/av/include",
106 "-isystem ${SrcDir}/frameworks/base/include",
107 }, " "))
108
109 pctx.StaticVariable("clangPath", "${SrcDir}/prebuilts/clang/${HostPrebuiltTag}/host/3.6/bin/")
110}
111
Colin Cross97ba0732015-03-23 17:50:24 -0700112// ccProperties describes properties used to compile all C or C++ modules
Colin Cross3f40fa42015-01-30 17:27:36 -0800113type ccProperties struct {
114 // srcs: list of source files used to compile the C/C++ module. May be .c, .cpp, or .S files.
115 Srcs []string `android:"arch_variant,arch_subtract"`
116
117 // cflags: list of module-specific flags that will be used for C and C++ compiles.
118 Cflags []string `android:"arch_variant"`
119
120 // cppflags: list of module-specific flags that will be used for C++ compiles
121 Cppflags []string `android:"arch_variant"`
122
123 // conlyflags: list of module-specific flags that will be used for C compiles
124 Conlyflags []string `android:"arch_variant"`
125
126 // asflags: list of module-specific flags that will be used for .S compiles
127 Asflags []string `android:"arch_variant"`
128
129 // ldflags: list of module-specific flags that will be used for all link steps
130 Ldflags []string `android:"arch_variant"`
131
Tim Kilbourn1a9bf262015-03-18 12:28:32 -0700132 // instruction_set: the instruction set architecture to use to compile the C/C++
133 // module.
134 Instruction_set string `android:"arch_variant"`
135
Colin Cross3f40fa42015-01-30 17:27:36 -0800136 // include_dirs: list of directories relative to the root of the source tree that will
137 // be added to the include path using -I.
138 // If possible, don't use this. If adding paths from the current directory use
139 // local_include_dirs, if adding paths from other modules use export_include_dirs in
140 // that module.
141 Include_dirs []string `android:"arch_variant"`
142
143 // local_include_dirs: list of directories relative to the Blueprints file that will
144 // be added to the include path using -I
145 Local_include_dirs []string `android:"arch_variant"`
146
147 // export_include_dirs: list of directories relative to the Blueprints file that will
148 // be added to the include path using -I for any module that links against this module
149 Export_include_dirs []string
150
151 // clang_cflags: list of module-specific flags that will be used for C and C++ compiles when
152 // compiling with clang
153 Clang_cflags []string `android:"arch_variant"`
154
155 // clang_asflags: list of module-specific flags that will be used for .S compiles when
156 // compiling with clang
157 Clang_asflags []string `android:"arch_variant"`
158
159 // system_shared_libs: list of system libraries that will be dynamically linked to
160 // shared library and executable modules. If unset, generally defaults to libc
161 // and libm. Set to [] to prevent linking against libc and libm.
162 System_shared_libs []string
163
164 // whole_static_libs: list of modules whose object files should be linked into this module
165 // in their entirety. For static library modules, all of the .o files from the intermediate
166 // directory of the dependency will be linked into this modules .a file. For a shared library,
167 // the dependency's .a file will be linked into this module using -Wl,--whole-archive.
168 Whole_static_libs []string `android:"arch_variant"`
169
170 // static_libs: list of modules that should be statically linked into this module.
171 Static_libs []string `android:"arch_variant"`
172
173 // shared_libs: list of modules that should be dynamically linked into this module.
174 Shared_libs []string `android:"arch_variant"`
175
176 // allow_undefined_symbols: allow the module to contain undefined symbols. By default,
177 // modules cannot contain undefined symbols that are not satisified by their immediate
178 // dependencies. Set this flag to true to remove --no-undefined from the linker flags.
179 // This flag should only be necessary for compiling low-level libraries like libc.
180 Allow_undefined_symbols bool
181
182 // nocrt: don't link in crt_begin and crt_end. This flag should only be necessary for
183 // compiling crt or libc.
184 Nocrt bool `android:"arch_variant"`
185
186 // no_default_compiler_flags: don't insert default compiler flags into asflags, cflags,
187 // cppflags, conlyflags, ldflags, or include_dirs
188 No_default_compiler_flags bool
189
190 // clang: compile module with clang instead of gcc
191 Clang bool `android:"arch_variant"`
192
193 // rtti: pass -frtti instead of -fno-rtti
194 Rtti bool
195
196 // host_ldlibs: -l arguments to pass to linker for host-provided shared libraries
197 Host_ldlibs []string `android:"arch_variant"`
198
199 // stl: select the STL library to use. Possible values are "libc++", "libc++_static",
200 // "stlport", "stlport_static", "ndk", "libstdc++", or "none". Leave blank to select the
201 // default
202 Stl string
203
204 // Set for combined shared/static libraries to prevent compiling object files a second time
205 SkipCompileObjs bool `blueprint:"mutated"`
Colin Crossaf19a292015-03-18 12:07:10 -0700206
207 Debug struct {
208 Cflags []string `android:"arch_variant"`
209 } `android:"arch_variant"`
210 Release struct {
211 Cflags []string `android:"arch_variant"`
212 } `android:"arch_variant"`
Colin Crossefd8e482015-03-18 17:17:35 -0700213
214 // Minimum sdk version supported when compiling against the ndk
215 Sdk_version string
Colin Cross3f40fa42015-01-30 17:27:36 -0800216}
217
218type unusedProperties struct {
219 Asan bool
220 Native_coverage bool
221 Strip string
222 Tags []string
223 Required []string
224}
225
226// Building C/C++ code is handled by objects that satisfy this interface via composition
Colin Cross97ba0732015-03-23 17:50:24 -0700227type CCModuleType interface {
Colin Cross3f40fa42015-01-30 17:27:36 -0800228 common.AndroidModule
229
Colin Cross97ba0732015-03-23 17:50:24 -0700230 // Modify the ccFlags that are specific to this _type_ of module
231 ModuleTypeFlags(common.AndroidModuleContext, CCFlags) CCFlags
Colin Cross3f40fa42015-01-30 17:27:36 -0800232
233 // Create a ccDeps struct that collects the module dependency info. Can also
234 // modify ccFlags in order to add dependency include directories, etc.
Colin Cross97ba0732015-03-23 17:50:24 -0700235 collectDeps(common.AndroidModuleContext, CCFlags) (CCDeps, CCFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800236
237 // Compile objects into final module
Colin Cross97ba0732015-03-23 17:50:24 -0700238 compileModule(common.AndroidModuleContext, CCFlags, CCDeps, []string)
Colin Cross3f40fa42015-01-30 17:27:36 -0800239
Dan Albertc403f7c2015-03-18 14:01:18 -0700240 // Install the built module.
Colin Cross97ba0732015-03-23 17:50:24 -0700241 installModule(common.AndroidModuleContext, CCFlags)
Dan Albertc403f7c2015-03-18 14:01:18 -0700242
Colin Cross3f40fa42015-01-30 17:27:36 -0800243 // Return the output file (.o, .a or .so) for use by other modules
244 outputFile() string
245}
246
Colin Cross97ba0732015-03-23 17:50:24 -0700247type CCDeps struct {
248 StaticLibs, SharedLibs, LateStaticLibs, WholeStaticLibs, ObjFiles, IncludeDirs []string
Colin Crossc472d572015-03-17 15:06:21 -0700249
Colin Cross97ba0732015-03-23 17:50:24 -0700250 CrtBegin, CrtEnd string
Colin Crossc472d572015-03-17 15:06:21 -0700251}
252
Colin Cross97ba0732015-03-23 17:50:24 -0700253type CCFlags struct {
254 GlobalFlags []string
255 AsFlags []string
256 CFlags []string
257 ConlyFlags []string
258 CppFlags []string
259 LdFlags []string
260 LdLibs []string
261 IncludeDirs []string
262 Nocrt bool
263 Toolchain Toolchain
264 Clang bool
Colin Crossc472d572015-03-17 15:06:21 -0700265
Colin Cross97ba0732015-03-23 17:50:24 -0700266 ExtraStaticLibs []string
267 ExtraSharedLibs []string
Colin Crossc472d572015-03-17 15:06:21 -0700268}
269
270// ccBase contains the properties and members used by all C/C++ module types, and implements
271// the blueprint.Module interface. It expects to be embedded into an outer specialization struct,
272// and uses a ccModuleType interface to that struct to create the build steps.
273type ccBase struct {
274 common.AndroidModuleBase
Colin Cross97ba0732015-03-23 17:50:24 -0700275 module CCModuleType
Colin Crossc472d572015-03-17 15:06:21 -0700276
277 properties ccProperties
278 unused unusedProperties
279
280 installPath string
281}
282
Colin Cross97ba0732015-03-23 17:50:24 -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
288 props = append(props, &base.properties, &base.unused)
289
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 Cross3f40fa42015-01-30 17:27:36 -0800293func (c *ccBase) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
294 toolchain := c.findToolchain(ctx)
295 if ctx.Failed() {
296 return
297 }
298
299 flags := c.flags(ctx, toolchain)
300 if ctx.Failed() {
301 return
302 }
303
304 flags = c.addStlFlags(ctx, flags)
305 if ctx.Failed() {
306 return
307 }
308
309 deps, flags := c.ccModuleType().collectDeps(ctx, flags)
310 if ctx.Failed() {
311 return
312 }
313
Colin Cross97ba0732015-03-23 17:50:24 -0700314 flags.IncludeDirs = append(flags.IncludeDirs, deps.IncludeDirs...)
Colin Crossed9f8682015-03-18 17:17:35 -0700315
Colin Cross3f40fa42015-01-30 17:27:36 -0800316 objFiles := c.compileObjs(ctx, flags, deps)
317 if ctx.Failed() {
318 return
319 }
320
Colin Cross5049f022015-03-18 13:28:46 -0700321 generatedObjFiles := c.compileGeneratedObjs(ctx, flags, deps)
322 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 Cross97ba0732015-03-23 17:50:24 -0700339func (c *ccBase) ccModuleType() CCModuleType {
Colin Cross3f40fa42015-01-30 17:27:36 -0800340 return c.module
341}
342
343var _ common.AndroidDynamicDepender = (*ccBase)(nil)
344
Colin Cross97ba0732015-03-23 17:50:24 -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 Cross97ba0732015-03-23 17:50:24 -0700355func (c *ccBase) ModuleTypeFlags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
356 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -0800357}
358
359func (c *ccBase) AndroidDynamicDependencies(ctx common.AndroidDynamicDependerModuleContext) []string {
360 ctx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, c.properties.Whole_static_libs...)
361 ctx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, c.properties.Static_libs...)
362 ctx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, c.properties.Shared_libs...)
363
364 return nil
365}
366
367// Create a ccFlags struct that collects the compile flags from global values,
368// per-target values, module type values, and per-module Blueprints properties
Colin Cross97ba0732015-03-23 17:50:24 -0700369func (c *ccBase) flags(ctx common.AndroidModuleContext, toolchain Toolchain) CCFlags {
Colin Cross97ba0732015-03-23 17:50:24 -0700370 flags := CCFlags{
371 CFlags: c.properties.Cflags,
372 CppFlags: c.properties.Cppflags,
373 ConlyFlags: c.properties.Conlyflags,
374 LdFlags: c.properties.Ldflags,
375 AsFlags: c.properties.Asflags,
376 Nocrt: c.properties.Nocrt,
377 Toolchain: toolchain,
378 Clang: c.properties.Clang,
Colin Cross3f40fa42015-01-30 17:27:36 -0800379 }
Tim Kilbourn1a9bf262015-03-18 12:28:32 -0700380 instructionSet := c.properties.Instruction_set
381 instructionSetFlags, err := toolchain.InstructionSetFlags(instructionSet)
382 if err != nil {
383 ctx.ModuleErrorf("%s", err)
384 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800385
Colin Crossaf19a292015-03-18 12:07:10 -0700386 // TODO: debug
Colin Cross97ba0732015-03-23 17:50:24 -0700387 flags.CFlags = append(flags.CFlags, c.properties.Release.Cflags...)
Colin Crossaf19a292015-03-18 12:07:10 -0700388
Colin Crossf6566ed2015-03-24 11:13:38 -0700389 if ctx.Host() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800390 // TODO: allow per-module clang disable for host
Colin Cross97ba0732015-03-23 17:50:24 -0700391 flags.Clang = true
Colin Cross3f40fa42015-01-30 17:27:36 -0800392 }
393
Colin Cross97ba0732015-03-23 17:50:24 -0700394 if flags.Clang {
395 flags.CFlags = clangFilterUnknownCflags(flags.CFlags)
396 flags.CFlags = append(flags.CFlags, c.properties.Clang_cflags...)
397 flags.AsFlags = append(flags.AsFlags, c.properties.Clang_asflags...)
398 flags.CppFlags = clangFilterUnknownCflags(flags.CppFlags)
399 flags.ConlyFlags = clangFilterUnknownCflags(flags.ConlyFlags)
400 flags.LdFlags = clangFilterUnknownCflags(flags.LdFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800401
Colin Cross97ba0732015-03-23 17:50:24 -0700402 flags.CFlags = append(flags.CFlags, "${clangExtraCflags}")
403 flags.ConlyFlags = append(flags.ConlyFlags, "${clangExtraConlyflags}")
Colin Crossf6566ed2015-03-24 11:13:38 -0700404 if ctx.Device() {
Colin Cross97ba0732015-03-23 17:50:24 -0700405 flags.CFlags = append(flags.CFlags, "${clangExtraTargetCflags}")
Colin Crossbdd7b1c2015-03-16 16:21:20 -0700406 }
407
Colin Cross3f40fa42015-01-30 17:27:36 -0800408 target := "-target " + toolchain.ClangTriple()
409 gccPrefix := "-B" + filepath.Join(toolchain.GccRoot(), toolchain.GccTriple(), "bin")
410
Colin Cross97ba0732015-03-23 17:50:24 -0700411 flags.CFlags = append(flags.CFlags, target, gccPrefix)
412 flags.AsFlags = append(flags.AsFlags, target, gccPrefix)
413 flags.LdFlags = append(flags.LdFlags, target, gccPrefix)
Colin Cross3f40fa42015-01-30 17:27:36 -0800414
Colin Crossf6566ed2015-03-24 11:13:38 -0700415 if ctx.Host() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800416 gccToolchain := "--gcc-toolchain=" + toolchain.GccRoot()
417 sysroot := "--sysroot=" + filepath.Join(toolchain.GccRoot(), "sysroot")
418
419 // TODO: also need more -B, -L flags to make host builds hermetic
Colin Cross97ba0732015-03-23 17:50:24 -0700420 flags.CFlags = append(flags.CFlags, gccToolchain, sysroot)
421 flags.AsFlags = append(flags.AsFlags, gccToolchain, sysroot)
422 flags.LdFlags = append(flags.LdFlags, gccToolchain, sysroot)
Colin Cross3f40fa42015-01-30 17:27:36 -0800423 }
424 }
425
Colin Cross97ba0732015-03-23 17:50:24 -0700426 flags.IncludeDirs = pathtools.PrefixPaths(c.properties.Include_dirs, ctx.Config().(Config).SrcDir())
Colin Cross3f40fa42015-01-30 17:27:36 -0800427 localIncludeDirs := pathtools.PrefixPaths(c.properties.Local_include_dirs, common.ModuleSrcDir(ctx))
Colin Cross97ba0732015-03-23 17:50:24 -0700428 flags.IncludeDirs = append(flags.IncludeDirs, localIncludeDirs...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800429
430 if !c.properties.No_default_compiler_flags {
Colin Cross97ba0732015-03-23 17:50:24 -0700431 flags.IncludeDirs = append(flags.IncludeDirs, []string{
Colin Cross3f40fa42015-01-30 17:27:36 -0800432 common.ModuleSrcDir(ctx),
433 common.ModuleOutDir(ctx),
434 common.ModuleGenDir(ctx),
435 }...)
436
Colin Crossefd8e482015-03-18 17:17:35 -0700437 if c.properties.Sdk_version == "" {
Colin Cross97ba0732015-03-23 17:50:24 -0700438 flags.IncludeDirs = append(flags.IncludeDirs, "${SrcDir}/libnativehelper/include/nativehelper")
Colin Crossefd8e482015-03-18 17:17:35 -0700439 }
440
Colin Crossf6566ed2015-03-24 11:13:38 -0700441 if ctx.Device() && !c.properties.Allow_undefined_symbols {
Colin Cross97ba0732015-03-23 17:50:24 -0700442 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-undefined")
Colin Cross3f40fa42015-01-30 17:27:36 -0800443 }
444
Colin Cross97ba0732015-03-23 17:50:24 -0700445 if flags.Clang {
446 flags.CppFlags = append(flags.CppFlags, "${commonClangGlobalCppflags}")
447 flags.GlobalFlags = []string{
Colin Cross3f40fa42015-01-30 17:27:36 -0800448 "${commonGlobalIncludes}",
449 toolchain.IncludeFlags(),
Tim Kilbourn1a9bf262015-03-18 12:28:32 -0700450 instructionSetFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -0800451 toolchain.ClangCflags(),
452 "${commonClangGlobalCflags}",
Colin Crossf6566ed2015-03-24 11:13:38 -0700453 fmt.Sprintf("${%sClangGlobalCflags}", ctx.Arch().HostOrDevice),
Colin Cross3f40fa42015-01-30 17:27:36 -0800454 }
455 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700456 flags.CppFlags = append(flags.CppFlags, "${commonGlobalCppflags}")
457 flags.GlobalFlags = []string{
Colin Cross3f40fa42015-01-30 17:27:36 -0800458 "${commonGlobalIncludes}",
459 toolchain.IncludeFlags(),
Tim Kilbourn1a9bf262015-03-18 12:28:32 -0700460 instructionSetFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -0800461 toolchain.Cflags(),
462 "${commonGlobalCflags}",
Colin Crossf6566ed2015-03-24 11:13:38 -0700463 fmt.Sprintf("${%sGlobalCflags}", ctx.Arch().HostOrDevice),
Colin Cross3f40fa42015-01-30 17:27:36 -0800464 }
465 }
466
Colin Crossf6566ed2015-03-24 11:13:38 -0700467 if ctx.Host() {
Colin Cross97ba0732015-03-23 17:50:24 -0700468 flags.LdFlags = append(flags.LdFlags, c.properties.Host_ldlibs...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800469 }
470
Colin Crossf6566ed2015-03-24 11:13:38 -0700471 if ctx.Device() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800472 if c.properties.Rtti {
Colin Cross97ba0732015-03-23 17:50:24 -0700473 flags.CppFlags = append(flags.CppFlags, "-frtti")
Colin Cross3f40fa42015-01-30 17:27:36 -0800474 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700475 flags.CppFlags = append(flags.CppFlags, "-fno-rtti")
Colin Cross3f40fa42015-01-30 17:27:36 -0800476 }
477 }
478
Colin Cross97ba0732015-03-23 17:50:24 -0700479 flags.AsFlags = append(flags.AsFlags, "-D__ASSEMBLY__")
Colin Cross3f40fa42015-01-30 17:27:36 -0800480
Colin Cross97ba0732015-03-23 17:50:24 -0700481 if flags.Clang {
482 flags.CppFlags = append(flags.CppFlags, toolchain.ClangCppflags())
483 flags.LdFlags = append(flags.LdFlags, toolchain.ClangLdflags())
Colin Cross3f40fa42015-01-30 17:27:36 -0800484 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700485 flags.CppFlags = append(flags.CppFlags, toolchain.Cppflags())
486 flags.LdFlags = append(flags.LdFlags, toolchain.Ldflags())
Colin Cross3f40fa42015-01-30 17:27:36 -0800487 }
488 }
489
Colin Cross97ba0732015-03-23 17:50:24 -0700490 flags = c.ccModuleType().ModuleTypeFlags(ctx, flags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800491
492 // Optimization to reduce size of build.ninja
493 // Replace the long list of flags for each file with a module-local variable
Colin Cross97ba0732015-03-23 17:50:24 -0700494 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
495 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
496 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
497 flags.CFlags = []string{"$cflags"}
498 flags.CppFlags = []string{"$cppflags"}
499 flags.AsFlags = []string{"$asflags"}
Colin Cross3f40fa42015-01-30 17:27:36 -0800500
501 return flags
502}
503
504// Modify ccFlags structs with STL library info
Colin Cross97ba0732015-03-23 17:50:24 -0700505func (c *ccBase) addStlFlags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Cross3f40fa42015-01-30 17:27:36 -0800506 if !c.properties.No_default_compiler_flags {
Colin Cross3f40fa42015-01-30 17:27:36 -0800507 stl := "libc++" // TODO: mingw needs libstdc++
508 if c.properties.Stl != "" {
509 stl = c.properties.Stl
510 }
511
512 stlStatic := false
513 if strings.HasSuffix(stl, "_static") {
514 stlStatic = true
515 }
516
517 switch stl {
518 case "libc++", "libc++_static":
Colin Cross97ba0732015-03-23 17:50:24 -0700519 flags.CFlags = append(flags.CFlags, "-D_USING_LIBCXX")
520 flags.IncludeDirs = append(flags.IncludeDirs, "${SrcDir}/external/libcxx/include")
Colin Crossf6566ed2015-03-24 11:13:38 -0700521 if ctx.Host() {
Colin Cross97ba0732015-03-23 17:50:24 -0700522 flags.CppFlags = append(flags.CppFlags, "-nostdinc++")
523 flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs")
524 flags.LdLibs = append(flags.LdLibs, "-lc", "-lm", "-lpthread")
Colin Cross3f40fa42015-01-30 17:27:36 -0800525 }
526 if stlStatic {
Colin Cross97ba0732015-03-23 17:50:24 -0700527 flags.ExtraStaticLibs = append(flags.ExtraStaticLibs, "libc++_static")
Colin Cross3f40fa42015-01-30 17:27:36 -0800528 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700529 flags.ExtraSharedLibs = append(flags.ExtraSharedLibs, "libc++")
Colin Cross3f40fa42015-01-30 17:27:36 -0800530 }
531 case "stlport", "stlport_static":
Colin Crossf6566ed2015-03-24 11:13:38 -0700532 if ctx.Device() {
Colin Cross97ba0732015-03-23 17:50:24 -0700533 flags.IncludeDirs = append(flags.IncludeDirs,
Colin Cross3f40fa42015-01-30 17:27:36 -0800534 "${SrcDir}/external/stlport/stlport",
535 "${SrcDir}/bionic/libstdc++/include",
536 "${SrcDir}/bionic")
537 if stlStatic {
Colin Cross97ba0732015-03-23 17:50:24 -0700538 flags.ExtraStaticLibs = append(flags.ExtraStaticLibs, "libstdc++", "libstlport_static")
Colin Cross3f40fa42015-01-30 17:27:36 -0800539 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700540 flags.ExtraSharedLibs = append(flags.ExtraSharedLibs, "libstdc++", "libstlport")
Colin Cross3f40fa42015-01-30 17:27:36 -0800541 }
542 }
543 case "ndk":
544 panic("TODO")
545 case "libstdc++":
546 // Using bionic's basic libstdc++. Not actually an STL. Only around until the
547 // tree is in good enough shape to not need it.
548 // Host builds will use GNU libstdc++.
Colin Crossf6566ed2015-03-24 11:13:38 -0700549 if ctx.Device() {
Colin Cross97ba0732015-03-23 17:50:24 -0700550 flags.IncludeDirs = append(flags.IncludeDirs, "${SrcDir}/bionic/libstdc++/include")
551 flags.ExtraSharedLibs = append(flags.ExtraSharedLibs, "libstdc++")
Colin Cross3f40fa42015-01-30 17:27:36 -0800552 }
553 case "none":
Colin Crossf6566ed2015-03-24 11:13:38 -0700554 if ctx.Host() {
Colin Cross97ba0732015-03-23 17:50:24 -0700555 flags.CppFlags = append(flags.CppFlags, "-nostdinc++")
556 flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs")
557 flags.LdLibs = append(flags.LdLibs, "-lc", "-lm")
Colin Cross3f40fa42015-01-30 17:27:36 -0800558 }
559 default:
560 ctx.ModuleErrorf("stl: %q is not a supported STL", stl)
561 }
562
563 }
564 return flags
565}
566
567// Compile a list of source files into objects a specified subdirectory
Colin Cross97ba0732015-03-23 17:50:24 -0700568func (c *ccBase) customCompileObjs(ctx common.AndroidModuleContext, flags CCFlags,
569 deps CCDeps, subdir string, srcFiles []string) []string {
Colin Cross3f40fa42015-01-30 17:27:36 -0800570
571 srcFiles = pathtools.PrefixPaths(srcFiles, common.ModuleSrcDir(ctx))
572 srcFiles = common.ExpandGlobs(ctx, srcFiles)
573
574 return TransformSourceToObj(ctx, subdir, srcFiles, ccFlagsToBuilderFlags(flags))
575}
576
577// Compile files listed in c.properties.Srcs into objects
Colin Cross97ba0732015-03-23 17:50:24 -0700578func (c *ccBase) compileObjs(ctx common.AndroidModuleContext, flags CCFlags,
579 deps CCDeps) []string {
Colin Cross3f40fa42015-01-30 17:27:36 -0800580
581 if c.properties.SkipCompileObjs {
582 return nil
583 }
584
585 return c.customCompileObjs(ctx, flags, deps, "", c.properties.Srcs)
586}
587
Colin Cross5049f022015-03-18 13:28:46 -0700588// Compile generated source files from dependencies
Colin Cross97ba0732015-03-23 17:50:24 -0700589func (c *ccBase) compileGeneratedObjs(ctx common.AndroidModuleContext, flags CCFlags,
590 deps CCDeps) []string {
Colin Cross5049f022015-03-18 13:28:46 -0700591 var srcs []string
592
593 if c.properties.SkipCompileObjs {
594 return nil
595 }
596
597 ctx.VisitDirectDeps(func(module blueprint.Module) {
598 if gen, ok := module.(genrule.SourceFileGenerator); ok {
599 srcs = append(srcs, gen.GeneratedSourceFiles()...)
600 }
601 })
602
603 if len(srcs) == 0 {
604 return nil
605 }
606
607 return TransformSourceToObj(ctx, "", srcs, ccFlagsToBuilderFlags(flags))
608}
609
Colin Cross3f40fa42015-01-30 17:27:36 -0800610func (c *ccBase) outputFile() string {
611 return ""
612}
613
614func (c *ccBase) collectDepsFromList(ctx common.AndroidModuleContext,
615 names []string) (modules []common.AndroidModule,
616 outputFiles []string, exportedIncludeDirs []string) {
617
618 for _, n := range names {
619 found := false
620 ctx.VisitDirectDeps(func(m blueprint.Module) {
621 otherName := ctx.OtherModuleName(m)
622 if otherName != n {
623 return
624 }
625
Colin Cross97ba0732015-03-23 17:50:24 -0700626 if a, ok := m.(CCModuleType); ok {
Colin Cross3f40fa42015-01-30 17:27:36 -0800627 if a.Disabled() {
628 // If a cc_library host+device module depends on a library that exists as both
629 // cc_library_shared and cc_library_host_shared, it will end up with two
630 // dependencies with the same name, one of which is marked disabled for each
631 // of host and device. Ignore the disabled one.
632 return
633 }
634 if a.HostOrDevice() != ctx.Arch().HostOrDevice {
635 ctx.ModuleErrorf("host/device mismatch between %q and %q", ctx.ModuleName(),
636 otherName)
637 return
638 }
639
640 if outputFile := a.outputFile(); outputFile != "" {
641 if found {
642 ctx.ModuleErrorf("multiple modules satisified dependency on %q", otherName)
643 return
644 }
645 outputFiles = append(outputFiles, outputFile)
646 modules = append(modules, a)
647 if i, ok := a.(ccExportedIncludeDirsProducer); ok {
648 exportedIncludeDirs = append(exportedIncludeDirs, i.exportedIncludeDirs()...)
649 }
650 found = true
651 } else {
652 ctx.ModuleErrorf("module %q missing output file", otherName)
653 return
654 }
655 } else {
656 ctx.ModuleErrorf("module %q not an android module", otherName)
657 return
658 }
659 })
660 if !found {
661 ctx.ModuleErrorf("unsatisified dependency on %q", n)
662 }
663 }
664
665 return modules, outputFiles, exportedIncludeDirs
666}
667
Colin Cross97ba0732015-03-23 17:50:24 -0700668func (c *ccBase) collectDeps(ctx common.AndroidModuleContext, flags CCFlags) (CCDeps, CCFlags) {
669 var deps CCDeps
Colin Cross3f40fa42015-01-30 17:27:36 -0800670 var newIncludeDirs []string
671
672 wholeStaticLibNames := c.properties.Whole_static_libs
Colin Cross97ba0732015-03-23 17:50:24 -0700673 _, deps.WholeStaticLibs, newIncludeDirs = c.collectDepsFromList(ctx, wholeStaticLibNames)
Colin Cross3f40fa42015-01-30 17:27:36 -0800674
Colin Cross97ba0732015-03-23 17:50:24 -0700675 deps.IncludeDirs = append(deps.IncludeDirs, newIncludeDirs...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800676
677 staticLibNames := c.properties.Static_libs
Colin Cross97ba0732015-03-23 17:50:24 -0700678 staticLibNames = append(staticLibNames, flags.ExtraStaticLibs...)
679 _, deps.StaticLibs, newIncludeDirs = c.collectDepsFromList(ctx, staticLibNames)
680 deps.IncludeDirs = append(deps.IncludeDirs, newIncludeDirs...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800681
682 return deps, flags
683}
684
685// ccDynamic contains the properties and members used by shared libraries and dynamic executables
686type ccDynamic struct {
687 ccBase
688}
689
Colin Cross97ba0732015-03-23 17:50:24 -0700690func newCCDynamic(dynamic *ccDynamic, module CCModuleType, hod common.HostOrDeviceSupported,
Colin Crossc472d572015-03-17 15:06:21 -0700691 multilib common.Multilib, props ...interface{}) (blueprint.Module, []interface{}) {
692
693 dynamic.properties.System_shared_libs = []string{defaultSystemSharedLibraries}
694
695 return newCCBase(&dynamic.ccBase, module, hod, multilib, props...)
696}
697
Colin Cross3f40fa42015-01-30 17:27:36 -0800698const defaultSystemSharedLibraries = "__default__"
699
Colin Crossf6566ed2015-03-24 11:13:38 -0700700func (c *ccDynamic) systemSharedLibs(ctx common.AndroidBaseContext) []string {
Colin Cross3f40fa42015-01-30 17:27:36 -0800701 if len(c.properties.System_shared_libs) == 1 &&
702 c.properties.System_shared_libs[0] == defaultSystemSharedLibraries {
703
Colin Crossf6566ed2015-03-24 11:13:38 -0700704 if ctx.Host() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800705 return []string{}
706 } else {
707 return []string{"libc", "libm"}
708 }
709 }
710 return c.properties.System_shared_libs
711}
712
713var (
714 stlSharedLibs = []string{"libc++", "libstlport", "libstdc++"}
715 stlSharedHostLibs = []string{"libc++"}
716 stlStaticLibs = []string{"libc++_static", "libstlport_static", "libstdc++"}
717 stlStaticHostLibs = []string{"libc++_static"}
718)
719
720func (c *ccDynamic) AndroidDynamicDependencies(ctx common.AndroidDynamicDependerModuleContext) []string {
721 deps := c.ccBase.AndroidDynamicDependencies(ctx)
722
Colin Crossf6566ed2015-03-24 11:13:38 -0700723 if ctx.Device() {
724 ctx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, c.systemSharedLibs(ctx)...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800725 ctx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}},
726 "libcompiler_rt-extras",
Colin Cross77b00fa2015-03-16 16:15:49 -0700727 "libgcov",
Colin Cross3f40fa42015-01-30 17:27:36 -0800728 "libatomic",
729 "libgcc")
730
731 if c.properties.Stl != "none" {
732 ctx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, stlSharedLibs...)
733 ctx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, stlStaticLibs...)
734 }
735 } else {
736 if c.properties.Stl != "none" {
737 ctx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, stlSharedHostLibs...)
738 ctx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, stlStaticHostLibs...)
739 }
740 }
741
742 return deps
743}
744
Colin Cross97ba0732015-03-23 17:50:24 -0700745func (c *ccDynamic) collectDeps(ctx common.AndroidModuleContext, flags CCFlags) (CCDeps, CCFlags) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800746 var newIncludeDirs []string
747
748 deps, flags := c.ccBase.collectDeps(ctx, flags)
749
Colin Crossf6566ed2015-03-24 11:13:38 -0700750 systemSharedLibs := c.systemSharedLibs(ctx)
Colin Cross3f40fa42015-01-30 17:27:36 -0800751 sharedLibNames := make([]string, 0, len(c.properties.Shared_libs)+len(systemSharedLibs)+
Colin Cross97ba0732015-03-23 17:50:24 -0700752 len(flags.ExtraSharedLibs))
Colin Cross3f40fa42015-01-30 17:27:36 -0800753 sharedLibNames = append(sharedLibNames, c.properties.Shared_libs...)
754 sharedLibNames = append(sharedLibNames, systemSharedLibs...)
Colin Cross97ba0732015-03-23 17:50:24 -0700755 sharedLibNames = append(sharedLibNames, flags.ExtraSharedLibs...)
756 _, deps.SharedLibs, newIncludeDirs = c.collectDepsFromList(ctx, sharedLibNames)
757 deps.IncludeDirs = append(deps.IncludeDirs, newIncludeDirs...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800758
Colin Crossf6566ed2015-03-24 11:13:38 -0700759 if ctx.Device() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800760 var staticLibs []string
Colin Cross77b00fa2015-03-16 16:15:49 -0700761 staticLibNames := []string{"libcompiler_rt-extras"}
Colin Cross3f40fa42015-01-30 17:27:36 -0800762 _, staticLibs, newIncludeDirs = c.collectDepsFromList(ctx, staticLibNames)
Colin Cross97ba0732015-03-23 17:50:24 -0700763 deps.StaticLibs = append(deps.StaticLibs, staticLibs...)
764 deps.IncludeDirs = append(deps.IncludeDirs, newIncludeDirs...)
Colin Cross77b00fa2015-03-16 16:15:49 -0700765
766 // libgcc and libatomic have to be last on the command line
767 staticLibNames = []string{"libgcov", "libatomic", "libgcc"}
768 _, staticLibs, newIncludeDirs = c.collectDepsFromList(ctx, staticLibNames)
Colin Cross97ba0732015-03-23 17:50:24 -0700769 deps.LateStaticLibs = append(deps.LateStaticLibs, staticLibs...)
770 deps.IncludeDirs = append(deps.IncludeDirs, newIncludeDirs...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800771 }
772
773 ctx.VisitDirectDeps(func(m blueprint.Module) {
774 if obj, ok := m.(*ccObject); ok {
775 otherName := ctx.OtherModuleName(m)
776 if strings.HasPrefix(otherName, "crtbegin") {
777 if !c.properties.Nocrt {
Colin Cross97ba0732015-03-23 17:50:24 -0700778 deps.CrtBegin = obj.outputFile()
Colin Cross3f40fa42015-01-30 17:27:36 -0800779 }
780 } else if strings.HasPrefix(otherName, "crtend") {
781 if !c.properties.Nocrt {
Colin Cross97ba0732015-03-23 17:50:24 -0700782 deps.CrtEnd = obj.outputFile()
Colin Cross3f40fa42015-01-30 17:27:36 -0800783 }
784 } else {
785 ctx.ModuleErrorf("object module type only support for crtbegin and crtend, found %q",
786 ctx.OtherModuleName(m))
787 }
788 }
789 })
790
Colin Cross3f40fa42015-01-30 17:27:36 -0800791 return deps, flags
792}
793
794type ccExportedIncludeDirsProducer interface {
795 exportedIncludeDirs() []string
796}
797
798//
799// Combined static+shared libraries
800//
801
Colin Cross97ba0732015-03-23 17:50:24 -0700802type CCLibrary struct {
Colin Cross3f40fa42015-01-30 17:27:36 -0800803 ccDynamic
804
Colin Cross97ba0732015-03-23 17:50:24 -0700805 primary *CCLibrary
Colin Cross3f40fa42015-01-30 17:27:36 -0800806 primaryObjFiles []string
807 objFiles []string
808 exportIncludeDirs []string
809 out string
810
Colin Cross97ba0732015-03-23 17:50:24 -0700811 LibraryProperties struct {
Colin Cross3f40fa42015-01-30 17:27:36 -0800812 BuildStatic bool `blueprint:"mutated"`
813 BuildShared bool `blueprint:"mutated"`
814 IsShared bool `blueprint:"mutated"`
815 IsStatic bool `blueprint:"mutated"`
816
817 Static struct {
818 Srcs []string `android:"arch_variant"`
819 Cflags []string `android:"arch_variant"`
820 } `android:"arch_variant"`
821 Shared struct {
822 Srcs []string `android:"arch_variant"`
823 Cflags []string `android:"arch_variant"`
824 } `android:"arch_variant"`
825 }
826}
827
Colin Cross97ba0732015-03-23 17:50:24 -0700828type ccLibraryInterface interface {
829 ccLibrary() *CCLibrary
830 static() bool
831 shared() bool
832 allObjFiles() []string
Colin Crossc472d572015-03-17 15:06:21 -0700833}
834
Colin Cross97ba0732015-03-23 17:50:24 -0700835func (c *CCLibrary) ccLibrary() *CCLibrary {
836 return c
Colin Cross3f40fa42015-01-30 17:27:36 -0800837}
838
Colin Cross97ba0732015-03-23 17:50:24 -0700839func (c *CCLibrary) static() bool {
840 return c.LibraryProperties.IsStatic
841}
842
843func (c *CCLibrary) shared() bool {
844 return c.LibraryProperties.IsShared
845}
846
847func NewCCLibrary(library *CCLibrary, module CCModuleType,
848 hod common.HostOrDeviceSupported) (blueprint.Module, []interface{}) {
849
850 return newCCDynamic(&library.ccDynamic, module, hod, common.MultilibBoth,
851 &library.LibraryProperties)
852}
853
854func CCLibraryFactory() (blueprint.Module, []interface{}) {
855 module := &CCLibrary{}
856
857 module.LibraryProperties.BuildShared = true
858 module.LibraryProperties.BuildStatic = true
859
860 return NewCCLibrary(module, module, common.HostAndDeviceSupported)
861}
862
863func (c *CCLibrary) AndroidDynamicDependencies(ctx common.AndroidDynamicDependerModuleContext) []string {
864 if c.LibraryProperties.IsShared {
Colin Cross3f40fa42015-01-30 17:27:36 -0800865 deps := c.ccDynamic.AndroidDynamicDependencies(ctx)
Colin Crossf6566ed2015-03-24 11:13:38 -0700866 if ctx.Device() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800867 deps = append(deps, "crtbegin_so", "crtend_so")
868 }
869 return deps
870 } else {
871 return c.ccBase.AndroidDynamicDependencies(ctx)
872 }
873}
874
Colin Cross97ba0732015-03-23 17:50:24 -0700875func (c *CCLibrary) collectDeps(ctx common.AndroidModuleContext, flags CCFlags) (CCDeps, CCFlags) {
876 if c.LibraryProperties.IsStatic {
Colin Cross3f40fa42015-01-30 17:27:36 -0800877 deps, flags := c.ccBase.collectDeps(ctx, flags)
878 wholeStaticLibNames := c.properties.Whole_static_libs
879 wholeStaticLibs, _, _ := c.collectDepsFromList(ctx, wholeStaticLibNames)
880
881 for _, m := range wholeStaticLibs {
Colin Cross97ba0732015-03-23 17:50:24 -0700882 if staticLib, ok := m.(ccLibraryInterface); ok && staticLib.static() {
883 deps.ObjFiles = append(deps.ObjFiles, staticLib.allObjFiles()...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800884 } else {
885 ctx.ModuleErrorf("module %q not a static library", ctx.OtherModuleName(m))
886 }
887 }
888
Colin Crossed9f8682015-03-18 17:17:35 -0700889 // Collect exported includes from shared lib dependencies
890 sharedLibNames := c.properties.Shared_libs
891 _, _, newIncludeDirs := c.collectDepsFromList(ctx, sharedLibNames)
Colin Cross97ba0732015-03-23 17:50:24 -0700892 deps.IncludeDirs = append(deps.IncludeDirs, newIncludeDirs...)
Colin Crossed9f8682015-03-18 17:17:35 -0700893
Colin Cross3f40fa42015-01-30 17:27:36 -0800894 return deps, flags
Colin Cross97ba0732015-03-23 17:50:24 -0700895 } else if c.LibraryProperties.IsShared {
Colin Cross3f40fa42015-01-30 17:27:36 -0800896 return c.ccDynamic.collectDeps(ctx, flags)
897 } else {
898 panic("Not shared or static")
899 }
900}
901
Colin Cross97ba0732015-03-23 17:50:24 -0700902func (c *CCLibrary) outputFile() string {
Colin Cross3f40fa42015-01-30 17:27:36 -0800903 return c.out
904}
905
Colin Cross97ba0732015-03-23 17:50:24 -0700906func (c *CCLibrary) allObjFiles() []string {
Colin Cross3f40fa42015-01-30 17:27:36 -0800907 return c.objFiles
908}
909
Colin Cross97ba0732015-03-23 17:50:24 -0700910func (c *CCLibrary) exportedIncludeDirs() []string {
Colin Cross3f40fa42015-01-30 17:27:36 -0800911 return c.exportIncludeDirs
912}
913
Colin Cross97ba0732015-03-23 17:50:24 -0700914func (c *CCLibrary) ModuleTypeFlags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
915 flags.CFlags = append(flags.CFlags, "-fPIC")
Colin Cross3f40fa42015-01-30 17:27:36 -0800916
Colin Cross97ba0732015-03-23 17:50:24 -0700917 if c.LibraryProperties.IsShared {
Colin Cross3f40fa42015-01-30 17:27:36 -0800918 libName := ctx.ModuleName()
919 // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead
920 sharedFlag := "-Wl,-shared"
Colin Crossf6566ed2015-03-24 11:13:38 -0700921 if c.properties.Clang || ctx.Host() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800922 sharedFlag = "-shared"
923 }
Colin Crossf6566ed2015-03-24 11:13:38 -0700924 if ctx.Device() {
Colin Cross97ba0732015-03-23 17:50:24 -0700925 flags.LdFlags = append(flags.LdFlags, "-nostdlib")
Colin Cross3f40fa42015-01-30 17:27:36 -0800926 }
Colin Cross97ba0732015-03-23 17:50:24 -0700927
928 flags.LdFlags = append(flags.LdFlags,
929 "-Wl,--gc-sections",
930 sharedFlag,
931 "-Wl,-soname,"+libName+sharedLibraryExtension,
932 )
Colin Cross3f40fa42015-01-30 17:27:36 -0800933 }
Colin Cross97ba0732015-03-23 17:50:24 -0700934
935 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -0800936}
937
Colin Cross97ba0732015-03-23 17:50:24 -0700938func (c *CCLibrary) compileStaticLibrary(ctx common.AndroidModuleContext,
939 flags CCFlags, deps CCDeps, objFiles []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800940
941 staticFlags := flags
Colin Cross97ba0732015-03-23 17:50:24 -0700942 staticFlags.CFlags = append(staticFlags.CFlags, c.LibraryProperties.Static.Cflags...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800943 objFilesStatic := c.customCompileObjs(ctx, staticFlags, deps, common.DeviceStaticLibrary,
Colin Cross97ba0732015-03-23 17:50:24 -0700944 c.LibraryProperties.Static.Srcs)
Colin Cross3f40fa42015-01-30 17:27:36 -0800945
946 objFiles = append(objFiles, objFilesStatic...)
947
948 var includeDirs []string
949
950 wholeStaticLibNames := c.properties.Whole_static_libs
951 wholeStaticLibs, _, newIncludeDirs := c.collectDepsFromList(ctx, wholeStaticLibNames)
952 includeDirs = append(includeDirs, newIncludeDirs...)
953
954 for _, m := range wholeStaticLibs {
Colin Cross97ba0732015-03-23 17:50:24 -0700955 if staticLib, ok := m.(ccLibraryInterface); ok && staticLib.static() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800956 objFiles = append(objFiles, staticLib.allObjFiles()...)
957 } else {
958 ctx.ModuleErrorf("module %q not a static library", ctx.OtherModuleName(m))
959 }
960 }
961
962 staticLibNames := c.properties.Static_libs
963 _, _, newIncludeDirs = c.collectDepsFromList(ctx, staticLibNames)
964 includeDirs = append(includeDirs, newIncludeDirs...)
965
966 ctx.VisitDirectDeps(func(m blueprint.Module) {
967 if obj, ok := m.(*ccObject); ok {
968 otherName := ctx.OtherModuleName(m)
969 if !strings.HasPrefix(otherName, "crtbegin") && !strings.HasPrefix(otherName, "crtend") {
970 objFiles = append(objFiles, obj.outputFile())
971 }
972 }
973 })
974
975 outputFile := filepath.Join(common.ModuleOutDir(ctx), ctx.ModuleName()+staticLibraryExtension)
976
977 TransformObjToStaticLib(ctx, objFiles, ccFlagsToBuilderFlags(flags), outputFile)
978
979 c.objFiles = objFiles
980 c.out = outputFile
981 c.exportIncludeDirs = pathtools.PrefixPaths(c.properties.Export_include_dirs,
982 common.ModuleSrcDir(ctx))
983
984 ctx.CheckbuildFile(outputFile)
985}
986
Colin Cross97ba0732015-03-23 17:50:24 -0700987func (c *CCLibrary) compileSharedLibrary(ctx common.AndroidModuleContext,
988 flags CCFlags, deps CCDeps, objFiles []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800989
990 sharedFlags := flags
Colin Cross97ba0732015-03-23 17:50:24 -0700991 sharedFlags.CFlags = append(sharedFlags.CFlags, c.LibraryProperties.Shared.Cflags...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800992 objFilesShared := c.customCompileObjs(ctx, sharedFlags, deps, common.DeviceSharedLibrary,
Colin Cross97ba0732015-03-23 17:50:24 -0700993 c.LibraryProperties.Shared.Srcs)
Colin Cross3f40fa42015-01-30 17:27:36 -0800994
995 objFiles = append(objFiles, objFilesShared...)
996
997 outputFile := filepath.Join(common.ModuleOutDir(ctx), ctx.ModuleName()+sharedLibraryExtension)
998
Colin Cross97ba0732015-03-23 17:50:24 -0700999 TransformObjToDynamicBinary(ctx, objFiles, deps.SharedLibs, deps.StaticLibs,
1000 deps.LateStaticLibs, deps.WholeStaticLibs, deps.CrtBegin, deps.CrtEnd,
Colin Cross77b00fa2015-03-16 16:15:49 -07001001 ccFlagsToBuilderFlags(flags), outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08001002
1003 c.out = outputFile
1004 c.exportIncludeDirs = pathtools.PrefixPaths(c.properties.Export_include_dirs,
1005 common.ModuleSrcDir(ctx))
Colin Cross3f40fa42015-01-30 17:27:36 -08001006}
1007
Colin Cross97ba0732015-03-23 17:50:24 -07001008func (c *CCLibrary) compileModule(ctx common.AndroidModuleContext,
1009 flags CCFlags, deps CCDeps, objFiles []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001010
1011 // Reuse the object files from the matching static library if it exists
1012 if c.primary == c {
1013 c.primaryObjFiles = objFiles
1014 } else {
1015 objFiles = append([]string(nil), c.primary.primaryObjFiles...)
1016 }
1017
Colin Cross97ba0732015-03-23 17:50:24 -07001018 if c.LibraryProperties.IsStatic {
Colin Cross3f40fa42015-01-30 17:27:36 -08001019 c.compileStaticLibrary(ctx, flags, deps, objFiles)
1020 } else {
1021 c.compileSharedLibrary(ctx, flags, deps, objFiles)
1022 }
1023}
1024
Colin Cross97ba0732015-03-23 17:50:24 -07001025func (c *CCLibrary) installStaticLibrary(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001026 // Static libraries do not get installed.
1027}
1028
Colin Cross97ba0732015-03-23 17:50:24 -07001029func (c *CCLibrary) installSharedLibrary(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001030 installDir := "lib"
Colin Cross97ba0732015-03-23 17:50:24 -07001031 if flags.Toolchain.Is64Bit() {
Dan Albertc403f7c2015-03-18 14:01:18 -07001032 installDir = "lib64"
1033 }
1034
1035 ctx.InstallFile(installDir, c.out)
1036}
1037
Colin Cross97ba0732015-03-23 17:50:24 -07001038func (c *CCLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
1039 if c.LibraryProperties.IsStatic {
Dan Albertc403f7c2015-03-18 14:01:18 -07001040 c.installStaticLibrary(ctx, flags)
1041 } else {
1042 c.installSharedLibrary(ctx, flags)
1043 }
1044}
1045
Colin Cross3f40fa42015-01-30 17:27:36 -08001046//
1047// Objects (for crt*.o)
1048//
1049
1050type ccObject struct {
1051 ccBase
1052 out string
1053}
1054
Colin Cross97ba0732015-03-23 17:50:24 -07001055func CCObjectFactory() (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001056 module := &ccObject{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001057
Colin Crossc472d572015-03-17 15:06:21 -07001058 return newCCBase(&module.ccBase, module, common.DeviceSupported, common.MultilibBoth)
Colin Cross3f40fa42015-01-30 17:27:36 -08001059}
1060
1061func (*ccObject) AndroidDynamicDependencies(ctx common.AndroidDynamicDependerModuleContext) []string {
1062 // object files can't have any dynamic dependencies
1063 return nil
1064}
1065
Colin Cross97ba0732015-03-23 17:50:24 -07001066func (c *ccObject) collectDeps(ctx common.AndroidModuleContext, flags CCFlags) (CCDeps, CCFlags) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001067 deps, flags := c.ccBase.collectDeps(ctx, flags)
1068 ctx.VisitDirectDeps(func(m blueprint.Module) {
1069 if obj, ok := m.(*ccObject); ok {
Colin Cross97ba0732015-03-23 17:50:24 -07001070 deps.ObjFiles = append(deps.ObjFiles, obj.outputFile())
Colin Cross3f40fa42015-01-30 17:27:36 -08001071 } else {
1072 ctx.ModuleErrorf("Unknown module type for dependency %q", ctx.OtherModuleName(m))
1073 }
1074 })
1075
1076 return deps, flags
1077}
1078
1079func (c *ccObject) compileModule(ctx common.AndroidModuleContext,
Colin Cross97ba0732015-03-23 17:50:24 -07001080 flags CCFlags, deps CCDeps, objFiles []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001081
Colin Cross97ba0732015-03-23 17:50:24 -07001082 objFiles = append(objFiles, deps.ObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001083
1084 var outputFile string
1085 if len(objFiles) == 1 {
1086 outputFile = objFiles[0]
1087 } else {
1088 outputFile = filepath.Join(common.ModuleOutDir(ctx), ctx.ModuleName()+".o")
1089 TransformObjsToObj(ctx, objFiles, ccFlagsToBuilderFlags(flags), outputFile)
1090 }
1091
1092 c.out = outputFile
1093
1094 ctx.CheckbuildFile(outputFile)
1095}
1096
Colin Cross97ba0732015-03-23 17:50:24 -07001097func (c *ccObject) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001098 // Object files do not get installed.
1099}
1100
Colin Cross3f40fa42015-01-30 17:27:36 -08001101func (c *ccObject) outputFile() string {
1102 return c.out
1103}
1104
1105//
1106// Executables
1107//
1108
Colin Cross97ba0732015-03-23 17:50:24 -07001109type CCBinary struct {
Colin Cross3f40fa42015-01-30 17:27:36 -08001110 ccDynamic
Dan Albertc403f7c2015-03-18 14:01:18 -07001111 out string
Colin Cross97ba0732015-03-23 17:50:24 -07001112 BinaryProperties struct {
1113 // static_executable: compile executable with -static
1114 Static_executable bool
1115
1116 // stem: set the name of the output
1117 Stem string `android:"arch_variant"`
1118
1119 // prefix_symbols: if set, add an extra objcopy --prefix-symbols= step
1120 Prefix_symbols string
1121 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001122}
1123
Colin Cross97ba0732015-03-23 17:50:24 -07001124func (c *CCBinary) getStem(ctx common.AndroidModuleContext) string {
1125 if c.BinaryProperties.Stem != "" {
1126 return c.BinaryProperties.Stem
Colin Cross3f40fa42015-01-30 17:27:36 -08001127 }
1128 return ctx.ModuleName()
1129}
1130
Colin Cross97ba0732015-03-23 17:50:24 -07001131func (c *CCBinary) AndroidDynamicDependencies(ctx common.AndroidDynamicDependerModuleContext) []string {
Colin Cross3f40fa42015-01-30 17:27:36 -08001132 deps := c.ccDynamic.AndroidDynamicDependencies(ctx)
Colin Crossf6566ed2015-03-24 11:13:38 -07001133 if ctx.Device() {
Colin Cross97ba0732015-03-23 17:50:24 -07001134 if c.BinaryProperties.Static_executable {
Colin Cross3f40fa42015-01-30 17:27:36 -08001135 deps = append(deps, "crtbegin_static", "crtend_android")
1136 } else {
1137 deps = append(deps, "crtbegin_dynamic", "crtend_android")
1138 }
1139 }
1140 return deps
1141}
1142
Colin Cross97ba0732015-03-23 17:50:24 -07001143func NewCCBinary(binary *CCBinary, module CCModuleType,
1144 hod common.HostOrDeviceSupported) (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001145
Colin Cross97ba0732015-03-23 17:50:24 -07001146 return newCCDynamic(&binary.ccDynamic, module, hod, common.MultilibFirst,
1147 &binary.BinaryProperties)
Colin Cross3f40fa42015-01-30 17:27:36 -08001148}
1149
Colin Cross97ba0732015-03-23 17:50:24 -07001150func CCBinaryFactory() (blueprint.Module, []interface{}) {
1151 module := &CCBinary{}
1152
1153 return NewCCBinary(module, module, common.HostAndDeviceSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001154}
1155
Colin Cross97ba0732015-03-23 17:50:24 -07001156func (c *CCBinary) ModuleTypeFlags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
1157 flags.CFlags = append(flags.CFlags, "-fpie")
1158
Colin Crossf6566ed2015-03-24 11:13:38 -07001159 if ctx.Device() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001160 linker := "/system/bin/linker"
Colin Cross97ba0732015-03-23 17:50:24 -07001161 if flags.Toolchain.Is64Bit() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001162 linker = "/system/bin/linker64"
1163 }
1164
Colin Cross97ba0732015-03-23 17:50:24 -07001165 flags.LdFlags = append(flags.LdFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -08001166 "-nostdlib",
1167 "-Bdynamic",
1168 fmt.Sprintf("-Wl,-dynamic-linker,%s", linker),
1169 "-Wl,--gc-sections",
1170 "-Wl,-z,nocopyreloc",
Colin Cross97ba0732015-03-23 17:50:24 -07001171 )
Colin Cross3f40fa42015-01-30 17:27:36 -08001172 }
1173
Colin Cross97ba0732015-03-23 17:50:24 -07001174 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001175}
1176
Colin Cross97ba0732015-03-23 17:50:24 -07001177func (c *CCBinary) compileModule(ctx common.AndroidModuleContext,
1178 flags CCFlags, deps CCDeps, objFiles []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001179
Colin Cross97ba0732015-03-23 17:50:24 -07001180 if !c.BinaryProperties.Static_executable && inList("libc", c.properties.Static_libs) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001181 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
1182 "from static libs or set static_executable: true")
1183 }
1184
1185 outputFile := filepath.Join(common.ModuleOutDir(ctx), c.getStem(ctx))
Dan Albertc403f7c2015-03-18 14:01:18 -07001186 c.out = outputFile
Colin Cross3f40fa42015-01-30 17:27:36 -08001187
Colin Cross97ba0732015-03-23 17:50:24 -07001188 TransformObjToDynamicBinary(ctx, objFiles, deps.SharedLibs, deps.StaticLibs,
1189 deps.LateStaticLibs, deps.WholeStaticLibs, deps.CrtBegin, deps.CrtEnd,
Colin Cross77b00fa2015-03-16 16:15:49 -07001190 ccFlagsToBuilderFlags(flags), outputFile)
Dan Albertc403f7c2015-03-18 14:01:18 -07001191}
Colin Cross3f40fa42015-01-30 17:27:36 -08001192
Colin Cross97ba0732015-03-23 17:50:24 -07001193func (c *CCBinary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001194 ctx.InstallFile("bin", c.out)
1195}
1196
1197type ccTest struct {
Colin Cross97ba0732015-03-23 17:50:24 -07001198 CCBinary
Colin Cross6b290692015-03-19 14:05:33 -07001199
1200 testProperties struct {
1201 // test_per_src: Create a separate test for each source file. Useful when there is
1202 // global state that can not be torn down and reset between each test suite.
1203 Test_per_src bool
1204 }
Dan Albertc403f7c2015-03-18 14:01:18 -07001205}
1206
1207var (
1208 gtestLibs = []string{"libgtest", "libgtest_main"}
1209)
1210
Colin Cross97ba0732015-03-23 17:50:24 -07001211func (c *ccTest) collectDeps(ctx common.AndroidModuleContext, flags CCFlags) (CCDeps, CCFlags) {
1212 deps, flags := c.CCBinary.collectDeps(ctx, flags)
Dan Albertc403f7c2015-03-18 14:01:18 -07001213
Colin Cross97ba0732015-03-23 17:50:24 -07001214 flags.CFlags = append(flags.CFlags, "-DGTEST_HAS_STD_STRING")
Colin Crossf6566ed2015-03-24 11:13:38 -07001215 if ctx.Host() {
Colin Cross97ba0732015-03-23 17:50:24 -07001216 flags.CFlags = append(flags.CFlags, "-O0", "-g")
1217 flags.LdLibs = append(flags.LdLibs, "-lpthread")
Dan Albertc403f7c2015-03-18 14:01:18 -07001218 }
1219
1220 // TODO(danalbert): Make gtest export its dependencies.
Colin Cross97ba0732015-03-23 17:50:24 -07001221 flags.IncludeDirs = append(flags.IncludeDirs,
Tim Kilbourn5ccc7302015-03-19 10:02:21 -07001222 filepath.Join(ctx.Config().(Config).SrcDir(), "external/gtest/include"))
Dan Albertc403f7c2015-03-18 14:01:18 -07001223
1224 _, staticLibs, _ := c.collectDepsFromList(ctx, gtestLibs)
Colin Cross97ba0732015-03-23 17:50:24 -07001225 deps.StaticLibs = append(deps.StaticLibs, staticLibs...)
Dan Albertc403f7c2015-03-18 14:01:18 -07001226
1227 return deps, flags
1228}
1229
1230func (c *ccTest) AndroidDynamicDependencies(ctx common.AndroidDynamicDependerModuleContext) []string {
1231 ctx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, gtestLibs...)
Colin Cross97ba0732015-03-23 17:50:24 -07001232 deps := c.CCBinary.AndroidDynamicDependencies(ctx)
Dan Albertc403f7c2015-03-18 14:01:18 -07001233 return append(deps, gtestLibs...)
1234}
1235
Colin Cross97ba0732015-03-23 17:50:24 -07001236func (c *ccTest) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Colin Crossf6566ed2015-03-24 11:13:38 -07001237 if ctx.Device() {
Tim Kilbourn5ccc7302015-03-19 10:02:21 -07001238 ctx.InstallFile("../data/nativetest/"+ctx.ModuleName(), c.out)
Dan Albertc403f7c2015-03-18 14:01:18 -07001239 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001240 c.CCBinary.installModule(ctx, flags)
Dan Albertc403f7c2015-03-18 14:01:18 -07001241 }
1242}
1243
Colin Cross97ba0732015-03-23 17:50:24 -07001244func CCTestFactory() (blueprint.Module, []interface{}) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001245 module := &ccTest{}
1246 return newCCDynamic(&module.ccDynamic, module, common.HostAndDeviceSupported,
Colin Cross97ba0732015-03-23 17:50:24 -07001247 common.MultilibFirst, &module.BinaryProperties, &module.testProperties)
Colin Cross6b290692015-03-19 14:05:33 -07001248}
1249
1250func TestPerSrcMutator(mctx blueprint.EarlyMutatorContext) {
1251 if test, ok := mctx.Module().(*ccTest); ok {
1252 if test.testProperties.Test_per_src {
1253 testNames := make([]string, len(test.properties.Srcs))
1254 for i, src := range test.properties.Srcs {
1255 testNames[i] = strings.TrimSuffix(src, filepath.Ext(src))
1256 }
1257 tests := mctx.CreateLocalVariations(testNames...)
1258 for i, src := range test.properties.Srcs {
1259 tests[i].(*ccTest).properties.Srcs = []string{src}
Colin Cross97ba0732015-03-23 17:50:24 -07001260 tests[i].(*ccTest).BinaryProperties.Stem = testNames[i]
Colin Cross6b290692015-03-19 14:05:33 -07001261 }
1262 }
1263 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001264}
1265
1266//
1267// Static library
1268//
1269
Colin Cross97ba0732015-03-23 17:50:24 -07001270func CCLibraryStaticFactory() (blueprint.Module, []interface{}) {
1271 module := &CCLibrary{}
1272 module.LibraryProperties.BuildStatic = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001273
Colin Cross97ba0732015-03-23 17:50:24 -07001274 return NewCCLibrary(module, module, common.HostAndDeviceSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001275}
1276
1277//
1278// Shared libraries
1279//
1280
Colin Cross97ba0732015-03-23 17:50:24 -07001281func CCLibrarySharedFactory() (blueprint.Module, []interface{}) {
1282 module := &CCLibrary{}
1283 module.LibraryProperties.BuildShared = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001284
Colin Cross97ba0732015-03-23 17:50:24 -07001285 return NewCCLibrary(module, module, common.HostAndDeviceSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001286}
1287
1288//
1289// Host static library
1290//
1291
Colin Cross97ba0732015-03-23 17:50:24 -07001292func CCLibraryHostStaticFactory() (blueprint.Module, []interface{}) {
1293 module := &CCLibrary{}
1294 module.LibraryProperties.BuildStatic = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001295
Colin Cross97ba0732015-03-23 17:50:24 -07001296 return NewCCLibrary(module, module, common.HostSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001297}
1298
1299//
1300// Host Shared libraries
1301//
1302
Colin Cross97ba0732015-03-23 17:50:24 -07001303func CCLibraryHostSharedFactory() (blueprint.Module, []interface{}) {
1304 module := &CCLibrary{}
1305 module.LibraryProperties.BuildShared = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001306
Colin Cross97ba0732015-03-23 17:50:24 -07001307 return NewCCLibrary(module, module, common.HostSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001308}
1309
1310//
1311// Host Binaries
1312//
1313
Colin Cross97ba0732015-03-23 17:50:24 -07001314func CCBinaryHostFactory() (blueprint.Module, []interface{}) {
1315 module := &CCBinary{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001316
Colin Cross97ba0732015-03-23 17:50:24 -07001317 return NewCCBinary(module, module, common.HostSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001318}
1319
1320//
1321// Device libraries shipped with gcc
1322//
1323
1324type toolchainLibrary struct {
Colin Cross97ba0732015-03-23 17:50:24 -07001325 CCLibrary
Colin Cross3f40fa42015-01-30 17:27:36 -08001326}
1327
1328func (*toolchainLibrary) AndroidDynamicDependencies(ctx common.AndroidDynamicDependerModuleContext) []string {
1329 // toolchain libraries can't have any dependencies
1330 return nil
1331}
1332
Colin Cross97ba0732015-03-23 17:50:24 -07001333func (*toolchainLibrary) collectDeps(ctx common.AndroidModuleContext, flags CCFlags) (CCDeps, CCFlags) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001334 // toolchain libraries can't have any dependencies
Colin Cross97ba0732015-03-23 17:50:24 -07001335 return CCDeps{}, flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001336}
1337
Colin Cross97ba0732015-03-23 17:50:24 -07001338func ToolchainLibraryFactory() (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001339 module := &toolchainLibrary{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001340
Colin Cross97ba0732015-03-23 17:50:24 -07001341 module.LibraryProperties.BuildStatic = true
1342
Colin Crossc472d572015-03-17 15:06:21 -07001343 return newCCBase(&module.ccBase, module, common.DeviceSupported, common.MultilibBoth)
Colin Cross3f40fa42015-01-30 17:27:36 -08001344}
1345
1346func (c *toolchainLibrary) compileModule(ctx common.AndroidModuleContext,
Colin Cross97ba0732015-03-23 17:50:24 -07001347 flags CCFlags, deps CCDeps, objFiles []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001348
1349 libName := ctx.ModuleName() + staticLibraryExtension
1350 outputFile := filepath.Join(common.ModuleOutDir(ctx), libName)
1351
1352 CopyGccLib(ctx, libName, ccFlagsToBuilderFlags(flags), outputFile)
1353
1354 c.out = outputFile
1355
1356 ctx.CheckbuildFile(outputFile)
1357}
1358
Colin Cross97ba0732015-03-23 17:50:24 -07001359func (c *toolchainLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001360 // Toolchain libraries do not get installed.
1361}
1362
Colin Cross3f40fa42015-01-30 17:27:36 -08001363func LinkageMutator(mctx blueprint.EarlyMutatorContext) {
Colin Cross97ba0732015-03-23 17:50:24 -07001364 if c, ok := mctx.Module().(ccLibraryInterface); ok {
Colin Cross3f40fa42015-01-30 17:27:36 -08001365 var modules []blueprint.Module
Colin Cross97ba0732015-03-23 17:50:24 -07001366 if c.ccLibrary().LibraryProperties.BuildStatic && c.ccLibrary().LibraryProperties.BuildShared {
Colin Cross3f40fa42015-01-30 17:27:36 -08001367 modules = mctx.CreateLocalVariations("static", "shared")
Colin Cross97ba0732015-03-23 17:50:24 -07001368 modules[0].(ccLibraryInterface).ccLibrary().LibraryProperties.IsStatic = true
1369 modules[1].(ccLibraryInterface).ccLibrary().LibraryProperties.IsShared = true
1370 } else if c.ccLibrary().LibraryProperties.BuildStatic {
Colin Cross3f40fa42015-01-30 17:27:36 -08001371 modules = mctx.CreateLocalVariations("static")
Colin Cross97ba0732015-03-23 17:50:24 -07001372 modules[0].(ccLibraryInterface).ccLibrary().LibraryProperties.IsStatic = true
1373 } else if c.ccLibrary().LibraryProperties.BuildShared {
Colin Cross3f40fa42015-01-30 17:27:36 -08001374 modules = mctx.CreateLocalVariations("shared")
Colin Cross97ba0732015-03-23 17:50:24 -07001375 modules[0].(ccLibraryInterface).ccLibrary().LibraryProperties.IsShared = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001376 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001377 panic(fmt.Errorf("ccLibrary %q not static or shared", mctx.ModuleName()))
Colin Cross3f40fa42015-01-30 17:27:36 -08001378 }
Colin Cross97ba0732015-03-23 17:50:24 -07001379 primary := modules[0].(ccLibraryInterface).ccLibrary()
Colin Cross3f40fa42015-01-30 17:27:36 -08001380 for _, m := range modules {
Colin Cross97ba0732015-03-23 17:50:24 -07001381 m.(ccLibraryInterface).ccLibrary().primary = primary
1382 if m.(ccLibraryInterface).ccLibrary() != primary {
1383 m.(ccLibraryInterface).ccLibrary().properties.SkipCompileObjs = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001384 }
1385 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001386 }
1387}