blob: a4f8e58b7568f1db3d79d9f61ed2074a006351ba [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 (
Dan Albert9e10cd42016-08-03 14:12:14 -070022 "strconv"
Colin Cross3f40fa42015-01-30 17:27:36 -080023 "strings"
24
Colin Cross97ba0732015-03-23 17:50:24 -070025 "github.com/google/blueprint"
Colin Cross06a931b2015-10-28 17:23:31 -070026 "github.com/google/blueprint/proptools"
Colin Cross97ba0732015-03-23 17:50:24 -070027
Colin Cross635c3b02016-05-18 15:37:25 -070028 "android/soong/android"
Colin Crossb98c8b02016-07-29 13:44:28 -070029 "android/soong/cc/config"
Colin Cross5049f022015-03-18 13:28:46 -070030 "android/soong/genrule"
Colin Cross3f40fa42015-01-30 17:27:36 -080031)
32
Colin Cross463a90e2015-06-17 14:20:06 -070033func init() {
Colin Cross798bfce2016-10-12 14:28:16 -070034 android.RegisterModuleType("cc_defaults", defaultsFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070035
Colin Cross1e676be2016-10-12 14:38:15 -070036 android.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
37 ctx.BottomUp("link", linkageMutator).Parallel()
38 ctx.BottomUp("ndk_api", ndkApiMutator).Parallel()
39 ctx.BottomUp("test_per_src", testPerSrcMutator).Parallel()
40 ctx.BottomUp("begin", beginMutator).Parallel()
41 })
Colin Cross16b23492016-01-06 14:41:07 -080042
Colin Cross1e676be2016-10-12 14:38:15 -070043 android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
44 ctx.TopDown("asan_deps", sanitizerDepsMutator(asan))
45 ctx.BottomUp("asan", sanitizerMutator(asan)).Parallel()
Colin Cross16b23492016-01-06 14:41:07 -080046
Colin Cross1e676be2016-10-12 14:38:15 -070047 ctx.TopDown("tsan_deps", sanitizerDepsMutator(tsan))
48 ctx.BottomUp("tsan", sanitizerMutator(tsan)).Parallel()
49 })
Colin Crossb98c8b02016-07-29 13:44:28 -070050
51 pctx.Import("android/soong/cc/config")
Colin Cross463a90e2015-06-17 14:20:06 -070052}
53
Colin Crossca860ac2016-01-04 14:34:37 -080054type Deps struct {
55 SharedLibs, LateSharedLibs []string
56 StaticLibs, LateStaticLibs, WholeStaticLibs []string
Colin Crossc472d572015-03-17 15:06:21 -070057
Dan Willemsen490a8dc2016-06-06 18:22:19 -070058 ReexportSharedLibHeaders, ReexportStaticLibHeaders []string
59
Colin Cross81413472016-04-11 14:37:39 -070060 ObjFiles []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -070061
Dan Willemsenb40aab62016-04-20 14:21:14 -070062 GeneratedSources []string
63 GeneratedHeaders []string
64
Dan Willemsenb3454ab2016-09-28 17:34:58 -070065 ReexportGeneratedHeaders []string
66
Colin Cross97ba0732015-03-23 17:50:24 -070067 CrtBegin, CrtEnd string
Colin Crossc472d572015-03-17 15:06:21 -070068}
69
Colin Crossca860ac2016-01-04 14:34:37 -080070type PathDeps struct {
Colin Cross26c34ed2016-09-30 17:10:16 -070071 // Paths to .so files
72 SharedLibs, LateSharedLibs android.Paths
73 // Paths to the dependencies to use for .so files (.so.toc files)
74 SharedLibsDeps, LateSharedLibsDeps android.Paths
75 // Paths to .a files
Colin Cross635c3b02016-05-18 15:37:25 -070076 StaticLibs, LateStaticLibs, WholeStaticLibs android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -070077
Colin Cross26c34ed2016-09-30 17:10:16 -070078 // Paths to .o files
Dan Willemsen5cb580f2016-09-26 17:33:01 -070079 Objs Objects
80 WholeStaticLibObjs Objects
Dan Willemsen34cc69e2015-09-23 15:26:20 -070081
Colin Cross26c34ed2016-09-30 17:10:16 -070082 // Paths to generated source files
Colin Cross635c3b02016-05-18 15:37:25 -070083 GeneratedSources android.Paths
84 GeneratedHeaders android.Paths
Dan Willemsenb40aab62016-04-20 14:21:14 -070085
Dan Willemsen76f08272016-07-09 00:14:08 -070086 Flags, ReexportedFlags []string
Dan Willemsen847dcc72016-09-29 12:13:36 -070087 ReexportedFlagsDeps android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -070088
Colin Cross26c34ed2016-09-30 17:10:16 -070089 // Paths to crt*.o files
Colin Cross635c3b02016-05-18 15:37:25 -070090 CrtBegin, CrtEnd android.OptionalPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -070091}
92
Colin Crossca860ac2016-01-04 14:34:37 -080093type Flags struct {
Colin Cross28344522015-04-22 13:07:53 -070094 GlobalFlags []string // Flags that apply to C, C++, and assembly source files
95 AsFlags []string // Flags that apply to assembly source files
96 CFlags []string // Flags that apply to C and C++ source files
97 ConlyFlags []string // Flags that apply to C source files
98 CppFlags []string // Flags that apply to C++ source files
99 YaccFlags []string // Flags that apply to Yacc source files
Colin Cross0c461f12016-10-20 16:11:43 -0700100 protoFlags []string // Flags that apply to proto source files
Colin Cross28344522015-04-22 13:07:53 -0700101 LdFlags []string // Flags that apply to linker command lines
Colin Cross16b23492016-01-06 14:41:07 -0800102 libFlags []string // Flags to add libraries early to the link order
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700103 TidyFlags []string // Flags that apply to clang-tidy
Colin Cross91e90042016-12-02 17:13:24 -0800104 YasmFlags []string // Flags that apply to yasm assembly source files
Colin Cross28344522015-04-22 13:07:53 -0700105
Colin Crossb98c8b02016-07-29 13:44:28 -0700106 Toolchain config.Toolchain
Colin Cross28344522015-04-22 13:07:53 -0700107 Clang bool
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700108 Tidy bool
Colin Crossca860ac2016-01-04 14:34:37 -0800109
110 RequiredInstructionSet string
Colin Cross16b23492016-01-06 14:41:07 -0800111 DynamicLinker string
112
Colin Cross635c3b02016-05-18 15:37:25 -0700113 CFlagsDeps android.Paths // Files depended on by compiler flags
Colin Cross18c0c5a2016-12-01 14:45:23 -0800114
115 GroupStaticLibs bool
Colin Crossc472d572015-03-17 15:06:21 -0700116}
117
Colin Cross81413472016-04-11 14:37:39 -0700118type ObjectLinkerProperties struct {
119 // names of other cc_object modules to link into this module using partial linking
120 Objs []string `android:"arch_variant"`
121}
122
Colin Crossca860ac2016-01-04 14:34:37 -0800123// Properties used to compile all C or C++ modules
124type BaseProperties struct {
125 // compile module with clang instead of gcc
126 Clang *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700127
128 // Minimum sdk version supported when compiling against the ndk
129 Sdk_version string
130
Dan Willemsend2ede872016-11-18 14:54:24 -0800131 // Whether to compile against the VNDK
132 Use_vndk bool
133
Colin Crossca860ac2016-01-04 14:34:37 -0800134 // don't insert default compiler flags into asflags, cflags,
135 // cppflags, conlyflags, ldflags, or include_dirs
136 No_default_compiler_flags *bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700137
138 AndroidMkSharedLibs []string `blueprint:"mutated"`
Colin Crossbc6fb162016-05-24 15:39:04 -0700139 HideFromMake bool `blueprint:"mutated"`
Colin Crossb0f28952016-09-19 16:46:53 -0700140 PreventInstall bool `blueprint:"mutated"`
Dan Willemsend2ede872016-11-18 14:54:24 -0800141 Vndk_version string `blueprint:"mutated"`
Colin Crossca860ac2016-01-04 14:34:37 -0800142}
143
Colin Crossca860ac2016-01-04 14:34:37 -0800144type UnusedProperties struct {
Colin Cross21b481b2016-04-15 16:27:17 -0700145 Native_coverage *bool
Colin Cross21b481b2016-04-15 16:27:17 -0700146 Tags []string
Colin Crosscfad1192015-11-02 16:43:11 -0800147}
148
Colin Crossca860ac2016-01-04 14:34:37 -0800149type ModuleContextIntf interface {
Colin Crossca860ac2016-01-04 14:34:37 -0800150 static() bool
151 staticBinary() bool
152 clang() bool
Colin Crossb98c8b02016-07-29 13:44:28 -0700153 toolchain() config.Toolchain
Colin Crossca860ac2016-01-04 14:34:37 -0800154 noDefaultCompilerFlags() bool
155 sdk() bool
156 sdkVersion() string
Dan Willemsend2ede872016-11-18 14:54:24 -0800157 vndk() bool
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700158 selectedStl() string
Colin Crossce75d2c2016-10-06 16:12:58 -0700159 baseModuleName() string
Colin Crossca860ac2016-01-04 14:34:37 -0800160}
161
162type ModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700163 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800164 ModuleContextIntf
165}
166
167type BaseModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700168 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800169 ModuleContextIntf
170}
171
Colin Crossca860ac2016-01-04 14:34:37 -0800172type feature interface {
173 begin(ctx BaseModuleContext)
174 deps(ctx BaseModuleContext, deps Deps) Deps
175 flags(ctx ModuleContext, flags Flags) Flags
176 props() []interface{}
177}
178
179type compiler interface {
Colin Cross42742b82016-08-01 13:20:05 -0700180 compilerInit(ctx BaseModuleContext)
181 compilerDeps(ctx BaseModuleContext, deps Deps) Deps
182 compilerFlags(ctx ModuleContext, flags Flags) Flags
183 compilerProps() []interface{}
184
Colin Cross76fada02016-07-27 10:31:13 -0700185 appendCflags([]string)
186 appendAsflags([]string)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700187 compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects
Colin Crossca860ac2016-01-04 14:34:37 -0800188}
189
190type linker interface {
Colin Cross42742b82016-08-01 13:20:05 -0700191 linkerInit(ctx BaseModuleContext)
192 linkerDeps(ctx BaseModuleContext, deps Deps) Deps
193 linkerFlags(ctx ModuleContext, flags Flags) Flags
194 linkerProps() []interface{}
195
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700196 link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path
Colin Cross76fada02016-07-27 10:31:13 -0700197 appendLdflags([]string)
Colin Crossca860ac2016-01-04 14:34:37 -0800198}
199
200type installer interface {
Colin Cross42742b82016-08-01 13:20:05 -0700201 installerProps() []interface{}
Colin Cross635c3b02016-05-18 15:37:25 -0700202 install(ctx ModuleContext, path android.Path)
Colin Crossca860ac2016-01-04 14:34:37 -0800203 inData() bool
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700204 hostToolPath() android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800205}
206
Colin Crossc99deeb2016-04-11 15:06:20 -0700207type dependencyTag struct {
208 blueprint.BaseDependencyTag
209 name string
210 library bool
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700211
212 reexportFlags bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700213}
214
215var (
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700216 sharedDepTag = dependencyTag{name: "shared", library: true}
217 sharedExportDepTag = dependencyTag{name: "shared", library: true, reexportFlags: true}
218 lateSharedDepTag = dependencyTag{name: "late shared", library: true}
219 staticDepTag = dependencyTag{name: "static", library: true}
220 staticExportDepTag = dependencyTag{name: "static", library: true, reexportFlags: true}
221 lateStaticDepTag = dependencyTag{name: "late static", library: true}
222 wholeStaticDepTag = dependencyTag{name: "whole static", library: true, reexportFlags: true}
223 genSourceDepTag = dependencyTag{name: "gen source"}
224 genHeaderDepTag = dependencyTag{name: "gen header"}
225 genHeaderExportDepTag = dependencyTag{name: "gen header", reexportFlags: true}
226 objDepTag = dependencyTag{name: "obj"}
227 crtBeginDepTag = dependencyTag{name: "crtbegin"}
228 crtEndDepTag = dependencyTag{name: "crtend"}
229 reuseObjTag = dependencyTag{name: "reuse objects"}
230 ndkStubDepTag = dependencyTag{name: "ndk stub", library: true}
231 ndkLateStubDepTag = dependencyTag{name: "ndk late stub", library: true}
Colin Crossc99deeb2016-04-11 15:06:20 -0700232)
233
Colin Crossca860ac2016-01-04 14:34:37 -0800234// Module contains the properties and members used by all C/C++ module types, and implements
235// the blueprint.Module interface. It delegates to compiler, linker, and installer interfaces
236// to construct the output file. Behavior can be customized with a Customizer interface
237type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700238 android.ModuleBase
239 android.DefaultableModule
Colin Crossc472d572015-03-17 15:06:21 -0700240
Colin Crossca860ac2016-01-04 14:34:37 -0800241 Properties BaseProperties
242 unused UnusedProperties
Colin Crossfa138792015-04-24 17:31:52 -0700243
Colin Crossca860ac2016-01-04 14:34:37 -0800244 // initialize before calling Init
Colin Cross635c3b02016-05-18 15:37:25 -0700245 hod android.HostOrDeviceSupported
246 multilib android.Multilib
Colin Crossc472d572015-03-17 15:06:21 -0700247
Colin Crossca860ac2016-01-04 14:34:37 -0800248 // delegates, initialize before calling Init
Colin Crossb4ce0ec2016-09-13 13:41:39 -0700249 features []feature
250 compiler compiler
251 linker linker
252 installer installer
253 stl *stl
254 sanitize *sanitize
Colin Cross16b23492016-01-06 14:41:07 -0800255
256 androidMkSharedLibDeps []string
Colin Cross74d1ec02015-04-28 13:30:13 -0700257
Colin Cross635c3b02016-05-18 15:37:25 -0700258 outputFile android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800259
Colin Crossb98c8b02016-07-29 13:44:28 -0700260 cachedToolchain config.Toolchain
Colin Crossb916a382016-07-29 17:28:03 -0700261
262 subAndroidMkOnce map[subAndroidMkProvider]bool
Colin Crossc472d572015-03-17 15:06:21 -0700263}
264
Colin Crossca860ac2016-01-04 14:34:37 -0800265func (c *Module) Init() (blueprint.Module, []interface{}) {
266 props := []interface{}{&c.Properties, &c.unused}
Colin Crossca860ac2016-01-04 14:34:37 -0800267 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700268 props = append(props, c.compiler.compilerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800269 }
270 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700271 props = append(props, c.linker.linkerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800272 }
273 if c.installer != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700274 props = append(props, c.installer.installerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800275 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700276 if c.stl != nil {
277 props = append(props, c.stl.props()...)
278 }
Colin Cross16b23492016-01-06 14:41:07 -0800279 if c.sanitize != nil {
280 props = append(props, c.sanitize.props()...)
281 }
Colin Crossca860ac2016-01-04 14:34:37 -0800282 for _, feature := range c.features {
283 props = append(props, feature.props()...)
284 }
Colin Crossc472d572015-03-17 15:06:21 -0700285
Colin Cross635c3b02016-05-18 15:37:25 -0700286 _, props = android.InitAndroidArchModule(c, c.hod, c.multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700287
Colin Cross635c3b02016-05-18 15:37:25 -0700288 return android.InitDefaultableModule(c, c, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700289}
290
Colin Crossb916a382016-07-29 17:28:03 -0700291// Returns true for dependency roots (binaries)
292// TODO(ccross): also handle dlopenable libraries
293func (c *Module) isDependencyRoot() bool {
294 if root, ok := c.linker.(interface {
295 isDependencyRoot() bool
296 }); ok {
297 return root.isDependencyRoot()
298 }
299 return false
300}
301
Colin Crossca860ac2016-01-04 14:34:37 -0800302type baseModuleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700303 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800304 moduleContextImpl
305}
306
307type moduleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700308 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800309 moduleContextImpl
310}
311
312type moduleContextImpl struct {
313 mod *Module
314 ctx BaseModuleContext
315}
316
Colin Crossca860ac2016-01-04 14:34:37 -0800317func (ctx *moduleContextImpl) clang() bool {
318 return ctx.mod.clang(ctx.ctx)
319}
320
Colin Crossb98c8b02016-07-29 13:44:28 -0700321func (ctx *moduleContextImpl) toolchain() config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800322 return ctx.mod.toolchain(ctx.ctx)
323}
324
325func (ctx *moduleContextImpl) static() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700326 if static, ok := ctx.mod.linker.(interface {
327 static() bool
328 }); ok {
329 return static.static()
Colin Crossca860ac2016-01-04 14:34:37 -0800330 }
Colin Crossb916a382016-07-29 17:28:03 -0700331 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800332}
333
334func (ctx *moduleContextImpl) staticBinary() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700335 if static, ok := ctx.mod.linker.(interface {
336 staticBinary() bool
337 }); ok {
338 return static.staticBinary()
Colin Crossca860ac2016-01-04 14:34:37 -0800339 }
Colin Crossb916a382016-07-29 17:28:03 -0700340 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800341}
342
343func (ctx *moduleContextImpl) noDefaultCompilerFlags() bool {
344 return Bool(ctx.mod.Properties.No_default_compiler_flags)
345}
346
347func (ctx *moduleContextImpl) sdk() bool {
Dan Willemsena96ff642016-06-07 12:34:45 -0700348 if ctx.ctx.Device() {
349 return ctx.mod.Properties.Sdk_version != ""
350 }
351 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800352}
353
354func (ctx *moduleContextImpl) sdkVersion() string {
Dan Willemsena96ff642016-06-07 12:34:45 -0700355 if ctx.ctx.Device() {
Dan Willemsend2ede872016-11-18 14:54:24 -0800356 if ctx.mod.Properties.Use_vndk {
357 return ctx.mod.Properties.Vndk_version
358 } else {
359 return ctx.mod.Properties.Sdk_version
360 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700361 }
362 return ""
Colin Crossca860ac2016-01-04 14:34:37 -0800363}
364
Dan Willemsend2ede872016-11-18 14:54:24 -0800365func (ctx *moduleContextImpl) vndk() bool {
366 if ctx.ctx.Device() {
367 return ctx.mod.Properties.Use_vndk
368 }
369 return false
370}
371
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700372func (ctx *moduleContextImpl) selectedStl() string {
373 if stl := ctx.mod.stl; stl != nil {
374 return stl.Properties.SelectedStl
375 }
376 return ""
377}
378
Colin Crossce75d2c2016-10-06 16:12:58 -0700379func (ctx *moduleContextImpl) baseModuleName() string {
380 return ctx.mod.ModuleBase.BaseModuleName()
381}
382
Colin Cross635c3b02016-05-18 15:37:25 -0700383func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800384 return &Module{
385 hod: hod,
386 multilib: multilib,
387 }
388}
389
Colin Cross635c3b02016-05-18 15:37:25 -0700390func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800391 module := newBaseModule(hod, multilib)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700392 module.features = []feature{
393 &tidyFeature{},
394 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700395 module.stl = &stl{}
Colin Cross16b23492016-01-06 14:41:07 -0800396 module.sanitize = &sanitize{}
Colin Crossca860ac2016-01-04 14:34:37 -0800397 return module
398}
399
Colin Crossce75d2c2016-10-06 16:12:58 -0700400func (c *Module) Prebuilt() *android.Prebuilt {
401 if p, ok := c.linker.(prebuiltLinkerInterface); ok {
402 return p.prebuilt()
403 }
404 return nil
405}
406
407func (c *Module) Name() string {
408 name := c.ModuleBase.Name()
409 if p, ok := c.linker.(prebuiltLinkerInterface); ok {
410 name = p.Name(name)
411 }
412 return name
413}
414
Colin Cross635c3b02016-05-18 15:37:25 -0700415func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800416 ctx := &moduleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700417 ModuleContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800418 moduleContextImpl: moduleContextImpl{
419 mod: c,
420 },
421 }
422 ctx.ctx = ctx
423
424 flags := Flags{
425 Toolchain: c.toolchain(ctx),
426 Clang: c.clang(ctx),
427 }
Colin Crossca860ac2016-01-04 14:34:37 -0800428 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700429 flags = c.compiler.compilerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800430 }
431 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700432 flags = c.linker.linkerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800433 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700434 if c.stl != nil {
435 flags = c.stl.flags(ctx, flags)
436 }
Colin Cross16b23492016-01-06 14:41:07 -0800437 if c.sanitize != nil {
438 flags = c.sanitize.flags(ctx, flags)
439 }
Colin Crossca860ac2016-01-04 14:34:37 -0800440 for _, feature := range c.features {
441 flags = feature.flags(ctx, flags)
442 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800443 if ctx.Failed() {
444 return
445 }
446
Colin Crossb98c8b02016-07-29 13:44:28 -0700447 flags.CFlags, _ = filterList(flags.CFlags, config.IllegalFlags)
448 flags.CppFlags, _ = filterList(flags.CppFlags, config.IllegalFlags)
449 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, config.IllegalFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800450
Colin Crossca860ac2016-01-04 14:34:37 -0800451 // Optimization to reduce size of build.ninja
452 // Replace the long list of flags for each file with a module-local variable
453 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
454 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
455 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
456 flags.CFlags = []string{"$cflags"}
457 flags.CppFlags = []string{"$cppflags"}
458 flags.AsFlags = []string{"$asflags"}
459
Colin Crossc99deeb2016-04-11 15:06:20 -0700460 deps := c.depsToPaths(ctx)
Colin Cross3f40fa42015-01-30 17:27:36 -0800461 if ctx.Failed() {
462 return
463 }
464
Dan Willemsen76f08272016-07-09 00:14:08 -0700465 flags.GlobalFlags = append(flags.GlobalFlags, deps.Flags...)
Colin Crossed9f8682015-03-18 17:17:35 -0700466
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700467 var objs Objects
Colin Crossca860ac2016-01-04 14:34:37 -0800468 if c.compiler != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700469 objs = c.compiler.compile(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -0800470 if ctx.Failed() {
471 return
472 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800473 }
474
Colin Crossca860ac2016-01-04 14:34:37 -0800475 if c.linker != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700476 outputFile := c.linker.link(ctx, flags, deps, objs)
Colin Crossca860ac2016-01-04 14:34:37 -0800477 if ctx.Failed() {
478 return
479 }
Colin Cross635c3b02016-05-18 15:37:25 -0700480 c.outputFile = android.OptionalPathForPath(outputFile)
Colin Crossce75d2c2016-10-06 16:12:58 -0700481 }
Colin Cross5049f022015-03-18 13:28:46 -0700482
Colin Crossce75d2c2016-10-06 16:12:58 -0700483 if c.installer != nil && !c.Properties.PreventInstall && c.outputFile.Valid() {
484 c.installer.install(ctx, c.outputFile.Path())
485 if ctx.Failed() {
486 return
Colin Crossca860ac2016-01-04 14:34:37 -0800487 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700488 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800489}
490
Colin Crossb98c8b02016-07-29 13:44:28 -0700491func (c *Module) toolchain(ctx BaseModuleContext) config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800492 if c.cachedToolchain == nil {
Colin Crossb98c8b02016-07-29 13:44:28 -0700493 c.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
Colin Cross3f40fa42015-01-30 17:27:36 -0800494 }
Colin Crossca860ac2016-01-04 14:34:37 -0800495 return c.cachedToolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800496}
497
Colin Crossca860ac2016-01-04 14:34:37 -0800498func (c *Module) begin(ctx BaseModuleContext) {
499 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700500 c.compiler.compilerInit(ctx)
Colin Cross21b9a242015-03-24 14:15:58 -0700501 }
Colin Crossca860ac2016-01-04 14:34:37 -0800502 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700503 c.linker.linkerInit(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800504 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700505 if c.stl != nil {
506 c.stl.begin(ctx)
507 }
Colin Cross16b23492016-01-06 14:41:07 -0800508 if c.sanitize != nil {
509 c.sanitize.begin(ctx)
510 }
Colin Crossca860ac2016-01-04 14:34:37 -0800511 for _, feature := range c.features {
512 feature.begin(ctx)
513 }
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700514 if ctx.sdk() {
Dan Willemsend2ede872016-11-18 14:54:24 -0800515 if ctx.vndk() {
516 ctx.PropertyErrorf("use_vndk",
517 "sdk_version and use_vndk cannot be used at the same time")
518 }
519
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700520 version, err := normalizeNdkApiLevel(ctx.sdkVersion(), ctx.Arch())
521 if err != nil {
522 ctx.PropertyErrorf("sdk_version", err.Error())
523 }
Dan Albert90f7a4d2016-11-08 14:34:24 -0800524 c.Properties.Sdk_version = version
Dan Willemsend2ede872016-11-18 14:54:24 -0800525 } else if ctx.vndk() {
526 version, err := normalizeNdkApiLevel(ctx.DeviceConfig().VndkVersion(), ctx.Arch())
527 if err != nil {
528 ctx.ModuleErrorf("Bad BOARD_VNDK_VERSION: %s", err.Error())
529 }
530 c.Properties.Vndk_version = version
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700531 }
Colin Crossca860ac2016-01-04 14:34:37 -0800532}
533
Colin Crossc99deeb2016-04-11 15:06:20 -0700534func (c *Module) deps(ctx BaseModuleContext) Deps {
535 deps := Deps{}
536
537 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700538 deps = c.compiler.compilerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700539 }
540 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700541 deps = c.linker.linkerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700542 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700543 if c.stl != nil {
544 deps = c.stl.deps(ctx, deps)
545 }
Colin Cross16b23492016-01-06 14:41:07 -0800546 if c.sanitize != nil {
547 deps = c.sanitize.deps(ctx, deps)
548 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700549 for _, feature := range c.features {
550 deps = feature.deps(ctx, deps)
551 }
552
553 deps.WholeStaticLibs = lastUniqueElements(deps.WholeStaticLibs)
554 deps.StaticLibs = lastUniqueElements(deps.StaticLibs)
555 deps.LateStaticLibs = lastUniqueElements(deps.LateStaticLibs)
556 deps.SharedLibs = lastUniqueElements(deps.SharedLibs)
557 deps.LateSharedLibs = lastUniqueElements(deps.LateSharedLibs)
558
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700559 for _, lib := range deps.ReexportSharedLibHeaders {
560 if !inList(lib, deps.SharedLibs) {
561 ctx.PropertyErrorf("export_shared_lib_headers", "Shared library not in shared_libs: '%s'", lib)
562 }
563 }
564
565 for _, lib := range deps.ReexportStaticLibHeaders {
566 if !inList(lib, deps.StaticLibs) {
567 ctx.PropertyErrorf("export_static_lib_headers", "Static library not in static_libs: '%s'", lib)
568 }
569 }
570
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700571 for _, gen := range deps.ReexportGeneratedHeaders {
572 if !inList(gen, deps.GeneratedHeaders) {
573 ctx.PropertyErrorf("export_generated_headers", "Generated header module not in generated_headers: '%s'", gen)
574 }
575 }
576
Colin Crossc99deeb2016-04-11 15:06:20 -0700577 return deps
578}
579
Dan Albert7e9d2952016-08-04 13:02:36 -0700580func (c *Module) beginMutator(actx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800581 ctx := &baseModuleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700582 BaseContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800583 moduleContextImpl: moduleContextImpl{
584 mod: c,
585 },
586 }
587 ctx.ctx = ctx
588
Colin Crossca860ac2016-01-04 14:34:37 -0800589 c.begin(ctx)
Dan Albert7e9d2952016-08-04 13:02:36 -0700590}
591
Colin Cross1e676be2016-10-12 14:38:15 -0700592func (c *Module) DepsMutator(actx android.BottomUpMutatorContext) {
593 if !c.Enabled() {
594 return
595 }
596
Dan Albert7e9d2952016-08-04 13:02:36 -0700597 ctx := &baseModuleContext{
598 BaseContext: actx,
599 moduleContextImpl: moduleContextImpl{
600 mod: c,
601 },
602 }
603 ctx.ctx = ctx
Colin Crossca860ac2016-01-04 14:34:37 -0800604
Colin Crossc99deeb2016-04-11 15:06:20 -0700605 deps := c.deps(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800606
Colin Crossb5bc4b42016-07-11 16:11:59 -0700607 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.SharedLibs...)
608 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.LateSharedLibs...)
Dan Willemsen72d39932016-07-08 23:23:48 -0700609
Dan Albert914449f2016-06-17 16:45:24 -0700610 variantNdkLibs := []string{}
611 variantLateNdkLibs := []string{}
Dan Willemsend2ede872016-11-18 14:54:24 -0800612 if ctx.sdk() || ctx.vndk() {
Dan Albert914449f2016-06-17 16:45:24 -0700613 version := ctx.sdkVersion()
Dan Willemsen72d39932016-07-08 23:23:48 -0700614
Dan Albert914449f2016-06-17 16:45:24 -0700615 // Rewrites the names of shared libraries into the names of the NDK
616 // libraries where appropriate. This returns two slices.
617 //
618 // The first is a list of non-variant shared libraries (either rewritten
619 // NDK libraries to the modules in prebuilts/ndk, or not rewritten
620 // because they are not NDK libraries).
621 //
622 // The second is a list of ndk_library modules. These need to be
623 // separated because they are a variation dependency and must be added
624 // in a different manner.
625 rewriteNdkLibs := func(list []string) ([]string, []string) {
626 variantLibs := []string{}
627 nonvariantLibs := []string{}
628 for _, entry := range list {
Dan Willemsen72d39932016-07-08 23:23:48 -0700629 if inList(entry, ndkPrebuiltSharedLibraries) {
Dan Albert914449f2016-06-17 16:45:24 -0700630 if !inList(entry, ndkMigratedLibs) {
631 nonvariantLibs = append(nonvariantLibs, entry+".ndk."+version)
632 } else {
633 variantLibs = append(variantLibs, entry+ndkLibrarySuffix)
634 }
635 } else {
636 nonvariantLibs = append(variantLibs, entry)
Dan Willemsen72d39932016-07-08 23:23:48 -0700637 }
638 }
Dan Albert914449f2016-06-17 16:45:24 -0700639 return nonvariantLibs, variantLibs
Dan Willemsen72d39932016-07-08 23:23:48 -0700640 }
641
Dan Albert914449f2016-06-17 16:45:24 -0700642 deps.SharedLibs, variantNdkLibs = rewriteNdkLibs(deps.SharedLibs)
643 deps.LateSharedLibs, variantLateNdkLibs = rewriteNdkLibs(deps.LateSharedLibs)
Dan Willemsen72d39932016-07-08 23:23:48 -0700644 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700645
646 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, wholeStaticDepTag,
647 deps.WholeStaticLibs...)
648
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700649 for _, lib := range deps.StaticLibs {
650 depTag := staticDepTag
651 if inList(lib, deps.ReexportStaticLibHeaders) {
652 depTag = staticExportDepTag
653 }
Colin Cross15a0d462016-07-14 14:49:58 -0700654 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700655 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700656
657 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, lateStaticDepTag,
658 deps.LateStaticLibs...)
659
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700660 for _, lib := range deps.SharedLibs {
661 depTag := sharedDepTag
662 if inList(lib, deps.ReexportSharedLibHeaders) {
663 depTag = sharedExportDepTag
664 }
Colin Cross15a0d462016-07-14 14:49:58 -0700665 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700666 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700667
668 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, lateSharedDepTag,
669 deps.LateSharedLibs...)
670
Colin Cross68861832016-07-08 10:41:41 -0700671 actx.AddDependency(c, genSourceDepTag, deps.GeneratedSources...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700672
673 for _, gen := range deps.GeneratedHeaders {
674 depTag := genHeaderDepTag
675 if inList(gen, deps.ReexportGeneratedHeaders) {
676 depTag = genHeaderExportDepTag
677 }
678 actx.AddDependency(c, depTag, gen)
679 }
Dan Willemsenb40aab62016-04-20 14:21:14 -0700680
Colin Cross68861832016-07-08 10:41:41 -0700681 actx.AddDependency(c, objDepTag, deps.ObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700682
683 if deps.CrtBegin != "" {
Colin Cross68861832016-07-08 10:41:41 -0700684 actx.AddDependency(c, crtBeginDepTag, deps.CrtBegin)
Colin Crossca860ac2016-01-04 14:34:37 -0800685 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700686 if deps.CrtEnd != "" {
Colin Cross68861832016-07-08 10:41:41 -0700687 actx.AddDependency(c, crtEndDepTag, deps.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700688 }
Dan Albert914449f2016-06-17 16:45:24 -0700689
690 version := ctx.sdkVersion()
691 actx.AddVariationDependencies([]blueprint.Variation{
692 {"ndk_api", version}, {"link", "shared"}}, ndkStubDepTag, variantNdkLibs...)
693 actx.AddVariationDependencies([]blueprint.Variation{
694 {"ndk_api", version}, {"link", "shared"}}, ndkLateStubDepTag, variantLateNdkLibs...)
Colin Cross6362e272015-10-29 15:25:03 -0700695}
Colin Cross21b9a242015-03-24 14:15:58 -0700696
Dan Albert7e9d2952016-08-04 13:02:36 -0700697func beginMutator(ctx android.BottomUpMutatorContext) {
698 if c, ok := ctx.Module().(*Module); ok && c.Enabled() {
699 c.beginMutator(ctx)
700 }
701}
702
Colin Crossca860ac2016-01-04 14:34:37 -0800703func (c *Module) clang(ctx BaseModuleContext) bool {
704 clang := Bool(c.Properties.Clang)
705
706 if c.Properties.Clang == nil {
707 if ctx.Host() {
708 clang = true
709 }
710
711 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
712 clang = true
713 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800714 }
Colin Cross28344522015-04-22 13:07:53 -0700715
Colin Crossca860ac2016-01-04 14:34:37 -0800716 if !c.toolchain(ctx).ClangSupported() {
717 clang = false
718 }
719
720 return clang
721}
722
Colin Crossc99deeb2016-04-11 15:06:20 -0700723// Convert dependencies to paths. Returns a PathDeps containing paths
Colin Cross635c3b02016-05-18 15:37:25 -0700724func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
Colin Crossca860ac2016-01-04 14:34:37 -0800725 var depPaths PathDeps
Colin Crossca860ac2016-01-04 14:34:37 -0800726
Dan Willemsena96ff642016-06-07 12:34:45 -0700727 // Whether a module can link to another module, taking into
728 // account NDK linking.
Dan Albert9e10cd42016-08-03 14:12:14 -0700729 checkLinkType := func(from, to *Module) {
Dan Willemsena96ff642016-06-07 12:34:45 -0700730 if from.Target().Os != android.Android {
731 // Host code is not restricted
Dan Albert9e10cd42016-08-03 14:12:14 -0700732 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700733 }
734 if from.Properties.Sdk_version == "" {
735 // Platform code can link to anything
Dan Albert9e10cd42016-08-03 14:12:14 -0700736 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700737 }
Colin Crossb916a382016-07-29 17:28:03 -0700738 if _, ok := to.linker.(*toolchainLibraryDecorator); ok {
Dan Willemsena96ff642016-06-07 12:34:45 -0700739 // These are always allowed
Dan Albert9e10cd42016-08-03 14:12:14 -0700740 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700741 }
742 if _, ok := to.linker.(*ndkPrebuiltLibraryLinker); ok {
743 // These are allowed, but don't set sdk_version
Dan Albert9e10cd42016-08-03 14:12:14 -0700744 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700745 }
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700746 if _, ok := to.linker.(*ndkPrebuiltStlLinker); ok {
747 // These are allowed, but don't set sdk_version
Dan Albert9e10cd42016-08-03 14:12:14 -0700748 return
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700749 }
Colin Crossb916a382016-07-29 17:28:03 -0700750 if _, ok := to.linker.(*stubDecorator); ok {
Dan Albert914449f2016-06-17 16:45:24 -0700751 // These aren't real libraries, but are the stub shared libraries that are included in
752 // the NDK.
Dan Albert9e10cd42016-08-03 14:12:14 -0700753 return
Dan Albert914449f2016-06-17 16:45:24 -0700754 }
Dan Albert9e10cd42016-08-03 14:12:14 -0700755 if to.Properties.Sdk_version == "" {
756 // NDK code linking to platform code is never okay.
757 ctx.ModuleErrorf("depends on non-NDK-built library %q",
758 ctx.OtherModuleName(to))
759 }
760
761 // All this point we know we have two NDK libraries, but we need to
762 // check that we're not linking against anything built against a higher
763 // API level, as it is only valid to link against older or equivalent
764 // APIs.
765
766 if from.Properties.Sdk_version == "current" {
767 // Current can link against anything.
768 return
769 } else if to.Properties.Sdk_version == "current" {
770 // Current can't be linked against by anything else.
771 ctx.ModuleErrorf("links %q built against newer API version %q",
772 ctx.OtherModuleName(to), "current")
773 }
774
775 fromApi, err := strconv.Atoi(from.Properties.Sdk_version)
776 if err != nil {
777 ctx.PropertyErrorf("sdk_version",
778 "Invalid sdk_version value (must be int): %q",
779 from.Properties.Sdk_version)
780 }
781 toApi, err := strconv.Atoi(to.Properties.Sdk_version)
782 if err != nil {
783 ctx.PropertyErrorf("sdk_version",
784 "Invalid sdk_version value (must be int): %q",
785 to.Properties.Sdk_version)
786 }
787
788 if toApi > fromApi {
789 ctx.ModuleErrorf("links %q built against newer API version %q",
790 ctx.OtherModuleName(to), to.Properties.Sdk_version)
791 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700792 }
793
Colin Crossc99deeb2016-04-11 15:06:20 -0700794 ctx.VisitDirectDeps(func(m blueprint.Module) {
795 name := ctx.OtherModuleName(m)
796 tag := ctx.OtherModuleDependencyTag(m)
Colin Crossca860ac2016-01-04 14:34:37 -0800797
Colin Cross635c3b02016-05-18 15:37:25 -0700798 a, _ := m.(android.Module)
Colin Crossc99deeb2016-04-11 15:06:20 -0700799 if a == nil {
800 ctx.ModuleErrorf("module %q not an android module", name)
801 return
Colin Crossca860ac2016-01-04 14:34:37 -0800802 }
Colin Crossca860ac2016-01-04 14:34:37 -0800803
Dan Willemsena96ff642016-06-07 12:34:45 -0700804 cc, _ := m.(*Module)
805 if cc == nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700806 switch tag {
Colin Cross635c3b02016-05-18 15:37:25 -0700807 case android.DefaultsDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700808 case genSourceDepTag:
809 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
810 depPaths.GeneratedSources = append(depPaths.GeneratedSources,
811 genRule.GeneratedSourceFiles()...)
812 } else {
813 ctx.ModuleErrorf("module %q is not a gensrcs or genrule", name)
814 }
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700815 case genHeaderDepTag, genHeaderExportDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700816 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
817 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders,
818 genRule.GeneratedSourceFiles()...)
Colin Cross5ed99c62016-11-22 12:55:55 -0800819 flags := includeDirsToFlags(genRule.GeneratedHeaderDirs())
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700820 depPaths.Flags = append(depPaths.Flags, flags)
821 if tag == genHeaderExportDepTag {
822 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700823 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps,
824 genRule.GeneratedSourceFiles()...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700825 }
Dan Willemsenb40aab62016-04-20 14:21:14 -0700826 } else {
827 ctx.ModuleErrorf("module %q is not a genrule", name)
828 }
829 default:
Colin Crossc99deeb2016-04-11 15:06:20 -0700830 ctx.ModuleErrorf("depends on non-cc module %q", name)
Colin Crossca860ac2016-01-04 14:34:37 -0800831 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700832 return
833 }
834
835 if !a.Enabled() {
836 ctx.ModuleErrorf("depends on disabled module %q", name)
837 return
838 }
839
Colin Crossa1ad8d12016-06-01 17:09:44 -0700840 if a.Target().Os != ctx.Os() {
841 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), name)
842 return
843 }
844
845 if a.Target().Arch.ArchType != ctx.Arch().ArchType {
846 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700847 return
848 }
849
Colin Crossc99deeb2016-04-11 15:06:20 -0700850 if tag == reuseObjTag {
Colin Crossbba99042016-11-23 15:45:05 -0800851 if l, ok := cc.compiler.(libraryInterface); ok {
852 depPaths.Objs = depPaths.Objs.Append(l.reuseObjs())
853 return
854 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700855 }
856
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700857 if t, ok := tag.(dependencyTag); ok && t.library {
Dan Willemsena96ff642016-06-07 12:34:45 -0700858 if i, ok := cc.linker.(exportedFlagsProducer); ok {
Dan Willemsen76f08272016-07-09 00:14:08 -0700859 flags := i.exportedFlags()
Dan Willemsen847dcc72016-09-29 12:13:36 -0700860 deps := i.exportedFlagsDeps()
Dan Willemsen76f08272016-07-09 00:14:08 -0700861 depPaths.Flags = append(depPaths.Flags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700862 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders, deps...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700863
864 if t.reexportFlags {
Dan Willemsen76f08272016-07-09 00:14:08 -0700865 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700866 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps, deps...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700867 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700868 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700869
Dan Albert9e10cd42016-08-03 14:12:14 -0700870 checkLinkType(c, cc)
Colin Crossc99deeb2016-04-11 15:06:20 -0700871 }
872
Colin Cross26c34ed2016-09-30 17:10:16 -0700873 var ptr *android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -0700874 var depPtr *android.Paths
Colin Crossc99deeb2016-04-11 15:06:20 -0700875
Colin Cross26c34ed2016-09-30 17:10:16 -0700876 linkFile := cc.outputFile
877 depFile := android.OptionalPath{}
878
Colin Crossc99deeb2016-04-11 15:06:20 -0700879 switch tag {
Dan Albert914449f2016-06-17 16:45:24 -0700880 case ndkStubDepTag, sharedDepTag, sharedExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700881 ptr = &depPaths.SharedLibs
882 depPtr = &depPaths.SharedLibsDeps
883 depFile = cc.linker.(libraryInterface).toc()
Dan Albert914449f2016-06-17 16:45:24 -0700884 case lateSharedDepTag, ndkLateStubDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700885 ptr = &depPaths.LateSharedLibs
886 depPtr = &depPaths.LateSharedLibsDeps
887 depFile = cc.linker.(libraryInterface).toc()
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700888 case staticDepTag, staticExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700889 ptr = &depPaths.StaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -0700890 case lateStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700891 ptr = &depPaths.LateStaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -0700892 case wholeStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700893 ptr = &depPaths.WholeStaticLibs
Colin Crossb916a382016-07-29 17:28:03 -0700894 staticLib, ok := cc.linker.(libraryInterface)
895 if !ok || !staticLib.static() {
Dan Willemsena96ff642016-06-07 12:34:45 -0700896 ctx.ModuleErrorf("module %q not a static library", name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700897 return
898 }
899
900 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
901 postfix := " (required by " + ctx.OtherModuleName(m) + ")"
902 for i := range missingDeps {
903 missingDeps[i] += postfix
904 }
905 ctx.AddMissingDependencies(missingDeps)
906 }
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700907 depPaths.WholeStaticLibObjs = depPaths.WholeStaticLibObjs.Append(staticLib.objs())
Colin Crossc99deeb2016-04-11 15:06:20 -0700908 case objDepTag:
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700909 depPaths.Objs.objFiles = append(depPaths.Objs.objFiles, linkFile.Path())
Colin Crossc99deeb2016-04-11 15:06:20 -0700910 case crtBeginDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700911 depPaths.CrtBegin = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -0700912 case crtEndDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700913 depPaths.CrtEnd = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -0700914 }
915
Colin Cross26c34ed2016-09-30 17:10:16 -0700916 if ptr != nil {
Colin Crossce75d2c2016-10-06 16:12:58 -0700917 if !linkFile.Valid() {
918 ctx.ModuleErrorf("module %q missing output file", name)
919 return
920 }
Colin Cross26c34ed2016-09-30 17:10:16 -0700921 *ptr = append(*ptr, linkFile.Path())
922 }
923
Colin Crossc99deeb2016-04-11 15:06:20 -0700924 if depPtr != nil {
Colin Cross26c34ed2016-09-30 17:10:16 -0700925 dep := depFile
926 if !dep.Valid() {
927 dep = linkFile
928 }
929 *depPtr = append(*depPtr, dep.Path())
Colin Crossca860ac2016-01-04 14:34:37 -0800930 }
931 })
932
933 return depPaths
934}
935
936func (c *Module) InstallInData() bool {
937 if c.installer == nil {
938 return false
939 }
Colin Cross94610402016-08-29 13:41:32 -0700940 if c.sanitize != nil && c.sanitize.inData() {
941 return true
942 }
Colin Crossca860ac2016-01-04 14:34:37 -0800943 return c.installer.inData()
944}
945
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700946func (c *Module) HostToolPath() android.OptionalPath {
947 if c.installer == nil {
948 return android.OptionalPath{}
949 }
950 return c.installer.hostToolPath()
951}
952
Colin Cross2ba19d92015-05-07 15:44:20 -0700953//
Colin Crosscfad1192015-11-02 16:43:11 -0800954// Defaults
955//
Colin Crossca860ac2016-01-04 14:34:37 -0800956type Defaults struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700957 android.ModuleBase
958 android.DefaultsModule
Colin Crosscfad1192015-11-02 16:43:11 -0800959}
960
Colin Cross635c3b02016-05-18 15:37:25 -0700961func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -0800962}
963
Colin Cross1e676be2016-10-12 14:38:15 -0700964func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
965}
966
Colin Crossca860ac2016-01-04 14:34:37 -0800967func defaultsFactory() (blueprint.Module, []interface{}) {
Colin Crosse1d764e2016-08-18 14:18:32 -0700968 return DefaultsFactory()
969}
970
971func DefaultsFactory(props ...interface{}) (blueprint.Module, []interface{}) {
Colin Crossca860ac2016-01-04 14:34:37 -0800972 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -0800973
Colin Crosse1d764e2016-08-18 14:18:32 -0700974 props = append(props,
Colin Crossca860ac2016-01-04 14:34:37 -0800975 &BaseProperties{},
976 &BaseCompilerProperties{},
977 &BaseLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -0700978 &LibraryProperties{},
Colin Cross919281a2016-04-05 16:42:05 -0700979 &FlagExporterProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -0800980 &BinaryLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -0700981 &TestProperties{},
982 &TestBinaryProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -0800983 &UnusedProperties{},
984 &StlProperties{},
Colin Cross16b23492016-01-06 14:41:07 -0800985 &SanitizeProperties{},
Colin Cross665dce92016-04-28 14:50:03 -0700986 &StripProperties{},
Dan Willemsen7424d612016-09-01 13:45:39 -0700987 &InstallerProperties{},
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700988 &TidyProperties{},
Colin Crosse1d764e2016-08-18 14:18:32 -0700989 )
Colin Crosscfad1192015-11-02 16:43:11 -0800990
Colin Crosse1d764e2016-08-18 14:18:32 -0700991 return android.InitDefaultsModule(module, module, props...)
Colin Crosscfad1192015-11-02 16:43:11 -0800992}
993
Colin Cross74d1ec02015-04-28 13:30:13 -0700994// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
995// modifies the slice contents in place, and returns a subslice of the original slice
996func lastUniqueElements(list []string) []string {
997 totalSkip := 0
998 for i := len(list) - 1; i >= totalSkip; i-- {
999 skip := 0
1000 for j := i - 1; j >= totalSkip; j-- {
1001 if list[i] == list[j] {
1002 skip++
1003 } else {
1004 list[j+skip] = list[j]
1005 }
1006 }
1007 totalSkip += skip
1008 }
1009 return list[totalSkip:]
1010}
Colin Cross06a931b2015-10-28 17:23:31 -07001011
1012var Bool = proptools.Bool