blob: 3fc694fe3d1828cd00b836a2decddce33976e13a [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 Cross5950f382016-12-13 12:50:57 -080057 HeaderLibs []string
Colin Crossc472d572015-03-17 15:06:21 -070058
Colin Cross5950f382016-12-13 12:50:57 -080059 ReexportSharedLibHeaders, ReexportStaticLibHeaders, ReexportHeaderLibHeaders []string
Dan Willemsen490a8dc2016-06-06 18:22:19 -070060
Colin Cross81413472016-04-11 14:37:39 -070061 ObjFiles []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -070062
Dan Willemsenb40aab62016-04-20 14:21:14 -070063 GeneratedSources []string
64 GeneratedHeaders []string
65
Dan Willemsenb3454ab2016-09-28 17:34:58 -070066 ReexportGeneratedHeaders []string
67
Colin Cross97ba0732015-03-23 17:50:24 -070068 CrtBegin, CrtEnd string
Colin Crossc472d572015-03-17 15:06:21 -070069}
70
Colin Crossca860ac2016-01-04 14:34:37 -080071type PathDeps struct {
Colin Cross26c34ed2016-09-30 17:10:16 -070072 // Paths to .so files
73 SharedLibs, LateSharedLibs android.Paths
74 // Paths to the dependencies to use for .so files (.so.toc files)
75 SharedLibsDeps, LateSharedLibsDeps android.Paths
76 // Paths to .a files
Colin Cross635c3b02016-05-18 15:37:25 -070077 StaticLibs, LateStaticLibs, WholeStaticLibs android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -070078
Colin Cross26c34ed2016-09-30 17:10:16 -070079 // Paths to .o files
Dan Willemsen5cb580f2016-09-26 17:33:01 -070080 Objs Objects
81 WholeStaticLibObjs Objects
Dan Willemsen34cc69e2015-09-23 15:26:20 -070082
Colin Cross26c34ed2016-09-30 17:10:16 -070083 // Paths to generated source files
Colin Cross635c3b02016-05-18 15:37:25 -070084 GeneratedSources android.Paths
85 GeneratedHeaders android.Paths
Dan Willemsenb40aab62016-04-20 14:21:14 -070086
Dan Willemsen76f08272016-07-09 00:14:08 -070087 Flags, ReexportedFlags []string
Dan Willemsen847dcc72016-09-29 12:13:36 -070088 ReexportedFlagsDeps android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -070089
Colin Cross26c34ed2016-09-30 17:10:16 -070090 // Paths to crt*.o files
Colin Cross635c3b02016-05-18 15:37:25 -070091 CrtBegin, CrtEnd android.OptionalPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -070092}
93
Colin Crossca860ac2016-01-04 14:34:37 -080094type Flags struct {
Colin Cross28344522015-04-22 13:07:53 -070095 GlobalFlags []string // Flags that apply to C, C++, and assembly source files
96 AsFlags []string // Flags that apply to assembly source files
97 CFlags []string // Flags that apply to C and C++ source files
98 ConlyFlags []string // Flags that apply to C source files
99 CppFlags []string // Flags that apply to C++ source files
100 YaccFlags []string // Flags that apply to Yacc source files
Colin Cross0c461f12016-10-20 16:11:43 -0700101 protoFlags []string // Flags that apply to proto source files
Dan Willemsene1240db2016-11-03 14:28:51 -0700102 aidlFlags []string // Flags that apply to aidl source files
Colin Cross28344522015-04-22 13:07:53 -0700103 LdFlags []string // Flags that apply to linker command lines
Colin Cross16b23492016-01-06 14:41:07 -0800104 libFlags []string // Flags to add libraries early to the link order
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700105 TidyFlags []string // Flags that apply to clang-tidy
Colin Cross91e90042016-12-02 17:13:24 -0800106 YasmFlags []string // Flags that apply to yasm assembly source files
Colin Cross28344522015-04-22 13:07:53 -0700107
Colin Crossb98c8b02016-07-29 13:44:28 -0700108 Toolchain config.Toolchain
Colin Cross28344522015-04-22 13:07:53 -0700109 Clang bool
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700110 Tidy bool
Colin Crossca860ac2016-01-04 14:34:37 -0800111
112 RequiredInstructionSet string
Colin Cross16b23492016-01-06 14:41:07 -0800113 DynamicLinker string
114
Colin Cross635c3b02016-05-18 15:37:25 -0700115 CFlagsDeps android.Paths // Files depended on by compiler flags
Colin Cross18c0c5a2016-12-01 14:45:23 -0800116
117 GroupStaticLibs bool
Colin Crossc472d572015-03-17 15:06:21 -0700118}
119
Colin Cross81413472016-04-11 14:37:39 -0700120type ObjectLinkerProperties struct {
121 // names of other cc_object modules to link into this module using partial linking
122 Objs []string `android:"arch_variant"`
123}
124
Colin Crossca860ac2016-01-04 14:34:37 -0800125// Properties used to compile all C or C++ modules
126type BaseProperties struct {
127 // compile module with clang instead of gcc
128 Clang *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700129
130 // Minimum sdk version supported when compiling against the ndk
131 Sdk_version string
132
Dan Willemsend2ede872016-11-18 14:54:24 -0800133 // Whether to compile against the VNDK
134 Use_vndk bool
135
Colin Crossca860ac2016-01-04 14:34:37 -0800136 // don't insert default compiler flags into asflags, cflags,
137 // cppflags, conlyflags, ldflags, or include_dirs
138 No_default_compiler_flags *bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700139
140 AndroidMkSharedLibs []string `blueprint:"mutated"`
Colin Crossbc6fb162016-05-24 15:39:04 -0700141 HideFromMake bool `blueprint:"mutated"`
Colin Crossb0f28952016-09-19 16:46:53 -0700142 PreventInstall bool `blueprint:"mutated"`
Dan Willemsend2ede872016-11-18 14:54:24 -0800143 Vndk_version string `blueprint:"mutated"`
Colin Crossca860ac2016-01-04 14:34:37 -0800144}
145
Colin Crossca860ac2016-01-04 14:34:37 -0800146type UnusedProperties struct {
Colin Cross21b481b2016-04-15 16:27:17 -0700147 Native_coverage *bool
Colin Cross21b481b2016-04-15 16:27:17 -0700148 Tags []string
Colin Crosscfad1192015-11-02 16:43:11 -0800149}
150
Colin Crossca860ac2016-01-04 14:34:37 -0800151type ModuleContextIntf interface {
Colin Crossca860ac2016-01-04 14:34:37 -0800152 static() bool
153 staticBinary() bool
154 clang() bool
Colin Crossb98c8b02016-07-29 13:44:28 -0700155 toolchain() config.Toolchain
Colin Crossca860ac2016-01-04 14:34:37 -0800156 noDefaultCompilerFlags() bool
157 sdk() bool
158 sdkVersion() string
Dan Willemsend2ede872016-11-18 14:54:24 -0800159 vndk() bool
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700160 selectedStl() string
Colin Crossce75d2c2016-10-06 16:12:58 -0700161 baseModuleName() string
Colin Crossca860ac2016-01-04 14:34:37 -0800162}
163
164type ModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700165 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800166 ModuleContextIntf
167}
168
169type BaseModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700170 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800171 ModuleContextIntf
172}
173
Colin Crossca860ac2016-01-04 14:34:37 -0800174type feature interface {
175 begin(ctx BaseModuleContext)
176 deps(ctx BaseModuleContext, deps Deps) Deps
177 flags(ctx ModuleContext, flags Flags) Flags
178 props() []interface{}
179}
180
181type compiler interface {
Colin Cross42742b82016-08-01 13:20:05 -0700182 compilerInit(ctx BaseModuleContext)
183 compilerDeps(ctx BaseModuleContext, deps Deps) Deps
184 compilerFlags(ctx ModuleContext, flags Flags) Flags
185 compilerProps() []interface{}
186
Colin Cross76fada02016-07-27 10:31:13 -0700187 appendCflags([]string)
188 appendAsflags([]string)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700189 compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects
Colin Crossca860ac2016-01-04 14:34:37 -0800190}
191
192type linker interface {
Colin Cross42742b82016-08-01 13:20:05 -0700193 linkerInit(ctx BaseModuleContext)
194 linkerDeps(ctx BaseModuleContext, deps Deps) Deps
195 linkerFlags(ctx ModuleContext, flags Flags) Flags
196 linkerProps() []interface{}
197
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700198 link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path
Colin Cross76fada02016-07-27 10:31:13 -0700199 appendLdflags([]string)
Colin Crossca860ac2016-01-04 14:34:37 -0800200}
201
202type installer interface {
Colin Cross42742b82016-08-01 13:20:05 -0700203 installerProps() []interface{}
Colin Cross635c3b02016-05-18 15:37:25 -0700204 install(ctx ModuleContext, path android.Path)
Colin Crossca860ac2016-01-04 14:34:37 -0800205 inData() bool
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700206 hostToolPath() android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800207}
208
Colin Crossc99deeb2016-04-11 15:06:20 -0700209type dependencyTag struct {
210 blueprint.BaseDependencyTag
211 name string
212 library bool
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700213
214 reexportFlags bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700215}
216
217var (
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700218 sharedDepTag = dependencyTag{name: "shared", library: true}
219 sharedExportDepTag = dependencyTag{name: "shared", library: true, reexportFlags: true}
220 lateSharedDepTag = dependencyTag{name: "late shared", library: true}
221 staticDepTag = dependencyTag{name: "static", library: true}
222 staticExportDepTag = dependencyTag{name: "static", library: true, reexportFlags: true}
223 lateStaticDepTag = dependencyTag{name: "late static", library: true}
224 wholeStaticDepTag = dependencyTag{name: "whole static", library: true, reexportFlags: true}
Colin Cross5950f382016-12-13 12:50:57 -0800225 headerDepTag = dependencyTag{name: "header", library: true, reexportFlags: true}
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700226 genSourceDepTag = dependencyTag{name: "gen source"}
227 genHeaderDepTag = dependencyTag{name: "gen header"}
228 genHeaderExportDepTag = dependencyTag{name: "gen header", reexportFlags: true}
229 objDepTag = dependencyTag{name: "obj"}
230 crtBeginDepTag = dependencyTag{name: "crtbegin"}
231 crtEndDepTag = dependencyTag{name: "crtend"}
232 reuseObjTag = dependencyTag{name: "reuse objects"}
233 ndkStubDepTag = dependencyTag{name: "ndk stub", library: true}
234 ndkLateStubDepTag = dependencyTag{name: "ndk late stub", library: true}
Colin Crossc99deeb2016-04-11 15:06:20 -0700235)
236
Colin Crossca860ac2016-01-04 14:34:37 -0800237// Module contains the properties and members used by all C/C++ module types, and implements
238// the blueprint.Module interface. It delegates to compiler, linker, and installer interfaces
239// to construct the output file. Behavior can be customized with a Customizer interface
240type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700241 android.ModuleBase
242 android.DefaultableModule
Colin Crossc472d572015-03-17 15:06:21 -0700243
Colin Crossca860ac2016-01-04 14:34:37 -0800244 Properties BaseProperties
245 unused UnusedProperties
Colin Crossfa138792015-04-24 17:31:52 -0700246
Colin Crossca860ac2016-01-04 14:34:37 -0800247 // initialize before calling Init
Colin Cross635c3b02016-05-18 15:37:25 -0700248 hod android.HostOrDeviceSupported
249 multilib android.Multilib
Colin Crossc472d572015-03-17 15:06:21 -0700250
Colin Crossca860ac2016-01-04 14:34:37 -0800251 // delegates, initialize before calling Init
Colin Crossb4ce0ec2016-09-13 13:41:39 -0700252 features []feature
253 compiler compiler
254 linker linker
255 installer installer
256 stl *stl
257 sanitize *sanitize
Colin Cross16b23492016-01-06 14:41:07 -0800258
259 androidMkSharedLibDeps []string
Colin Cross74d1ec02015-04-28 13:30:13 -0700260
Colin Cross635c3b02016-05-18 15:37:25 -0700261 outputFile android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800262
Colin Crossb98c8b02016-07-29 13:44:28 -0700263 cachedToolchain config.Toolchain
Colin Crossb916a382016-07-29 17:28:03 -0700264
265 subAndroidMkOnce map[subAndroidMkProvider]bool
Colin Crossc472d572015-03-17 15:06:21 -0700266}
267
Colin Crossca860ac2016-01-04 14:34:37 -0800268func (c *Module) Init() (blueprint.Module, []interface{}) {
269 props := []interface{}{&c.Properties, &c.unused}
Colin Crossca860ac2016-01-04 14:34:37 -0800270 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700271 props = append(props, c.compiler.compilerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800272 }
273 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700274 props = append(props, c.linker.linkerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800275 }
276 if c.installer != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700277 props = append(props, c.installer.installerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800278 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700279 if c.stl != nil {
280 props = append(props, c.stl.props()...)
281 }
Colin Cross16b23492016-01-06 14:41:07 -0800282 if c.sanitize != nil {
283 props = append(props, c.sanitize.props()...)
284 }
Colin Crossca860ac2016-01-04 14:34:37 -0800285 for _, feature := range c.features {
286 props = append(props, feature.props()...)
287 }
Colin Crossc472d572015-03-17 15:06:21 -0700288
Colin Cross635c3b02016-05-18 15:37:25 -0700289 _, props = android.InitAndroidArchModule(c, c.hod, c.multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700290
Colin Cross635c3b02016-05-18 15:37:25 -0700291 return android.InitDefaultableModule(c, c, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700292}
293
Colin Crossb916a382016-07-29 17:28:03 -0700294// Returns true for dependency roots (binaries)
295// TODO(ccross): also handle dlopenable libraries
296func (c *Module) isDependencyRoot() bool {
297 if root, ok := c.linker.(interface {
298 isDependencyRoot() bool
299 }); ok {
300 return root.isDependencyRoot()
301 }
302 return false
303}
304
Colin Crossca860ac2016-01-04 14:34:37 -0800305type baseModuleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700306 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800307 moduleContextImpl
308}
309
310type moduleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700311 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800312 moduleContextImpl
313}
314
315type moduleContextImpl struct {
316 mod *Module
317 ctx BaseModuleContext
318}
319
Colin Crossca860ac2016-01-04 14:34:37 -0800320func (ctx *moduleContextImpl) clang() bool {
321 return ctx.mod.clang(ctx.ctx)
322}
323
Colin Crossb98c8b02016-07-29 13:44:28 -0700324func (ctx *moduleContextImpl) toolchain() config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800325 return ctx.mod.toolchain(ctx.ctx)
326}
327
328func (ctx *moduleContextImpl) static() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700329 if static, ok := ctx.mod.linker.(interface {
330 static() bool
331 }); ok {
332 return static.static()
Colin Crossca860ac2016-01-04 14:34:37 -0800333 }
Colin Crossb916a382016-07-29 17:28:03 -0700334 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800335}
336
337func (ctx *moduleContextImpl) staticBinary() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700338 if static, ok := ctx.mod.linker.(interface {
339 staticBinary() bool
340 }); ok {
341 return static.staticBinary()
Colin Crossca860ac2016-01-04 14:34:37 -0800342 }
Colin Crossb916a382016-07-29 17:28:03 -0700343 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800344}
345
346func (ctx *moduleContextImpl) noDefaultCompilerFlags() bool {
347 return Bool(ctx.mod.Properties.No_default_compiler_flags)
348}
349
350func (ctx *moduleContextImpl) sdk() bool {
Dan Willemsena96ff642016-06-07 12:34:45 -0700351 if ctx.ctx.Device() {
352 return ctx.mod.Properties.Sdk_version != ""
353 }
354 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800355}
356
357func (ctx *moduleContextImpl) sdkVersion() string {
Dan Willemsena96ff642016-06-07 12:34:45 -0700358 if ctx.ctx.Device() {
Dan Willemsend2ede872016-11-18 14:54:24 -0800359 if ctx.mod.Properties.Use_vndk {
360 return ctx.mod.Properties.Vndk_version
361 } else {
362 return ctx.mod.Properties.Sdk_version
363 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700364 }
365 return ""
Colin Crossca860ac2016-01-04 14:34:37 -0800366}
367
Dan Willemsend2ede872016-11-18 14:54:24 -0800368func (ctx *moduleContextImpl) vndk() bool {
369 if ctx.ctx.Device() {
370 return ctx.mod.Properties.Use_vndk
371 }
372 return false
373}
374
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700375func (ctx *moduleContextImpl) selectedStl() string {
376 if stl := ctx.mod.stl; stl != nil {
377 return stl.Properties.SelectedStl
378 }
379 return ""
380}
381
Colin Crossce75d2c2016-10-06 16:12:58 -0700382func (ctx *moduleContextImpl) baseModuleName() string {
383 return ctx.mod.ModuleBase.BaseModuleName()
384}
385
Colin Cross635c3b02016-05-18 15:37:25 -0700386func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800387 return &Module{
388 hod: hod,
389 multilib: multilib,
390 }
391}
392
Colin Cross635c3b02016-05-18 15:37:25 -0700393func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800394 module := newBaseModule(hod, multilib)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700395 module.features = []feature{
396 &tidyFeature{},
397 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700398 module.stl = &stl{}
Colin Cross16b23492016-01-06 14:41:07 -0800399 module.sanitize = &sanitize{}
Colin Crossca860ac2016-01-04 14:34:37 -0800400 return module
401}
402
Colin Crossce75d2c2016-10-06 16:12:58 -0700403func (c *Module) Prebuilt() *android.Prebuilt {
404 if p, ok := c.linker.(prebuiltLinkerInterface); ok {
405 return p.prebuilt()
406 }
407 return nil
408}
409
410func (c *Module) Name() string {
411 name := c.ModuleBase.Name()
412 if p, ok := c.linker.(prebuiltLinkerInterface); ok {
413 name = p.Name(name)
414 }
415 return name
416}
417
Colin Cross635c3b02016-05-18 15:37:25 -0700418func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800419 ctx := &moduleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700420 ModuleContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800421 moduleContextImpl: moduleContextImpl{
422 mod: c,
423 },
424 }
425 ctx.ctx = ctx
426
427 flags := Flags{
428 Toolchain: c.toolchain(ctx),
429 Clang: c.clang(ctx),
430 }
Colin Crossca860ac2016-01-04 14:34:37 -0800431 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700432 flags = c.compiler.compilerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800433 }
434 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700435 flags = c.linker.linkerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800436 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700437 if c.stl != nil {
438 flags = c.stl.flags(ctx, flags)
439 }
Colin Cross16b23492016-01-06 14:41:07 -0800440 if c.sanitize != nil {
441 flags = c.sanitize.flags(ctx, flags)
442 }
Colin Crossca860ac2016-01-04 14:34:37 -0800443 for _, feature := range c.features {
444 flags = feature.flags(ctx, flags)
445 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800446 if ctx.Failed() {
447 return
448 }
449
Colin Crossb98c8b02016-07-29 13:44:28 -0700450 flags.CFlags, _ = filterList(flags.CFlags, config.IllegalFlags)
451 flags.CppFlags, _ = filterList(flags.CppFlags, config.IllegalFlags)
452 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, config.IllegalFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800453
Colin Crossca860ac2016-01-04 14:34:37 -0800454 // Optimization to reduce size of build.ninja
455 // Replace the long list of flags for each file with a module-local variable
456 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
457 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
458 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
459 flags.CFlags = []string{"$cflags"}
460 flags.CppFlags = []string{"$cppflags"}
461 flags.AsFlags = []string{"$asflags"}
462
Colin Crossc99deeb2016-04-11 15:06:20 -0700463 deps := c.depsToPaths(ctx)
Colin Cross3f40fa42015-01-30 17:27:36 -0800464 if ctx.Failed() {
465 return
466 }
467
Dan Willemsen76f08272016-07-09 00:14:08 -0700468 flags.GlobalFlags = append(flags.GlobalFlags, deps.Flags...)
Colin Crossed9f8682015-03-18 17:17:35 -0700469
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700470 var objs Objects
Colin Crossca860ac2016-01-04 14:34:37 -0800471 if c.compiler != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700472 objs = c.compiler.compile(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -0800473 if ctx.Failed() {
474 return
475 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800476 }
477
Colin Crossca860ac2016-01-04 14:34:37 -0800478 if c.linker != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700479 outputFile := c.linker.link(ctx, flags, deps, objs)
Colin Crossca860ac2016-01-04 14:34:37 -0800480 if ctx.Failed() {
481 return
482 }
Colin Cross635c3b02016-05-18 15:37:25 -0700483 c.outputFile = android.OptionalPathForPath(outputFile)
Colin Crossce75d2c2016-10-06 16:12:58 -0700484 }
Colin Cross5049f022015-03-18 13:28:46 -0700485
Colin Crossce75d2c2016-10-06 16:12:58 -0700486 if c.installer != nil && !c.Properties.PreventInstall && c.outputFile.Valid() {
487 c.installer.install(ctx, c.outputFile.Path())
488 if ctx.Failed() {
489 return
Colin Crossca860ac2016-01-04 14:34:37 -0800490 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700491 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800492}
493
Colin Crossb98c8b02016-07-29 13:44:28 -0700494func (c *Module) toolchain(ctx BaseModuleContext) config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800495 if c.cachedToolchain == nil {
Colin Crossb98c8b02016-07-29 13:44:28 -0700496 c.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
Colin Cross3f40fa42015-01-30 17:27:36 -0800497 }
Colin Crossca860ac2016-01-04 14:34:37 -0800498 return c.cachedToolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800499}
500
Colin Crossca860ac2016-01-04 14:34:37 -0800501func (c *Module) begin(ctx BaseModuleContext) {
502 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700503 c.compiler.compilerInit(ctx)
Colin Cross21b9a242015-03-24 14:15:58 -0700504 }
Colin Crossca860ac2016-01-04 14:34:37 -0800505 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700506 c.linker.linkerInit(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800507 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700508 if c.stl != nil {
509 c.stl.begin(ctx)
510 }
Colin Cross16b23492016-01-06 14:41:07 -0800511 if c.sanitize != nil {
512 c.sanitize.begin(ctx)
513 }
Colin Crossca860ac2016-01-04 14:34:37 -0800514 for _, feature := range c.features {
515 feature.begin(ctx)
516 }
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700517 if ctx.sdk() {
Dan Willemsend2ede872016-11-18 14:54:24 -0800518 if ctx.vndk() {
519 ctx.PropertyErrorf("use_vndk",
520 "sdk_version and use_vndk cannot be used at the same time")
521 }
522
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700523 version, err := normalizeNdkApiLevel(ctx.sdkVersion(), ctx.Arch())
524 if err != nil {
525 ctx.PropertyErrorf("sdk_version", err.Error())
526 }
Dan Albert90f7a4d2016-11-08 14:34:24 -0800527 c.Properties.Sdk_version = version
Dan Willemsend2ede872016-11-18 14:54:24 -0800528 } else if ctx.vndk() {
529 version, err := normalizeNdkApiLevel(ctx.DeviceConfig().VndkVersion(), ctx.Arch())
530 if err != nil {
531 ctx.ModuleErrorf("Bad BOARD_VNDK_VERSION: %s", err.Error())
532 }
533 c.Properties.Vndk_version = version
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700534 }
Colin Crossca860ac2016-01-04 14:34:37 -0800535}
536
Colin Crossc99deeb2016-04-11 15:06:20 -0700537func (c *Module) deps(ctx BaseModuleContext) Deps {
538 deps := Deps{}
539
540 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700541 deps = c.compiler.compilerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700542 }
543 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700544 deps = c.linker.linkerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700545 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700546 if c.stl != nil {
547 deps = c.stl.deps(ctx, deps)
548 }
Colin Cross16b23492016-01-06 14:41:07 -0800549 if c.sanitize != nil {
550 deps = c.sanitize.deps(ctx, deps)
551 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700552 for _, feature := range c.features {
553 deps = feature.deps(ctx, deps)
554 }
555
556 deps.WholeStaticLibs = lastUniqueElements(deps.WholeStaticLibs)
557 deps.StaticLibs = lastUniqueElements(deps.StaticLibs)
558 deps.LateStaticLibs = lastUniqueElements(deps.LateStaticLibs)
559 deps.SharedLibs = lastUniqueElements(deps.SharedLibs)
560 deps.LateSharedLibs = lastUniqueElements(deps.LateSharedLibs)
Colin Cross5950f382016-12-13 12:50:57 -0800561 deps.HeaderLibs = lastUniqueElements(deps.HeaderLibs)
Colin Crossc99deeb2016-04-11 15:06:20 -0700562
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700563 for _, lib := range deps.ReexportSharedLibHeaders {
564 if !inList(lib, deps.SharedLibs) {
565 ctx.PropertyErrorf("export_shared_lib_headers", "Shared library not in shared_libs: '%s'", lib)
566 }
567 }
568
569 for _, lib := range deps.ReexportStaticLibHeaders {
570 if !inList(lib, deps.StaticLibs) {
571 ctx.PropertyErrorf("export_static_lib_headers", "Static library not in static_libs: '%s'", lib)
572 }
573 }
574
Colin Cross5950f382016-12-13 12:50:57 -0800575 for _, lib := range deps.ReexportHeaderLibHeaders {
576 if !inList(lib, deps.HeaderLibs) {
577 ctx.PropertyErrorf("export_header_lib_headers", "Header library not in header_libs: '%s'", lib)
578 }
579 }
580
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700581 for _, gen := range deps.ReexportGeneratedHeaders {
582 if !inList(gen, deps.GeneratedHeaders) {
583 ctx.PropertyErrorf("export_generated_headers", "Generated header module not in generated_headers: '%s'", gen)
584 }
585 }
586
Colin Crossc99deeb2016-04-11 15:06:20 -0700587 return deps
588}
589
Dan Albert7e9d2952016-08-04 13:02:36 -0700590func (c *Module) beginMutator(actx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800591 ctx := &baseModuleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700592 BaseContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800593 moduleContextImpl: moduleContextImpl{
594 mod: c,
595 },
596 }
597 ctx.ctx = ctx
598
Colin Crossca860ac2016-01-04 14:34:37 -0800599 c.begin(ctx)
Dan Albert7e9d2952016-08-04 13:02:36 -0700600}
601
Colin Cross1e676be2016-10-12 14:38:15 -0700602func (c *Module) DepsMutator(actx android.BottomUpMutatorContext) {
603 if !c.Enabled() {
604 return
605 }
606
Dan Albert7e9d2952016-08-04 13:02:36 -0700607 ctx := &baseModuleContext{
608 BaseContext: actx,
609 moduleContextImpl: moduleContextImpl{
610 mod: c,
611 },
612 }
613 ctx.ctx = ctx
Colin Crossca860ac2016-01-04 14:34:37 -0800614
Colin Crossc99deeb2016-04-11 15:06:20 -0700615 deps := c.deps(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800616
Colin Crossb5bc4b42016-07-11 16:11:59 -0700617 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.SharedLibs...)
618 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.LateSharedLibs...)
Dan Willemsen72d39932016-07-08 23:23:48 -0700619
Dan Albert914449f2016-06-17 16:45:24 -0700620 variantNdkLibs := []string{}
621 variantLateNdkLibs := []string{}
Dan Willemsend2ede872016-11-18 14:54:24 -0800622 if ctx.sdk() || ctx.vndk() {
Dan Albert914449f2016-06-17 16:45:24 -0700623 version := ctx.sdkVersion()
Dan Willemsen72d39932016-07-08 23:23:48 -0700624
Dan Albert914449f2016-06-17 16:45:24 -0700625 // Rewrites the names of shared libraries into the names of the NDK
626 // libraries where appropriate. This returns two slices.
627 //
628 // The first is a list of non-variant shared libraries (either rewritten
629 // NDK libraries to the modules in prebuilts/ndk, or not rewritten
630 // because they are not NDK libraries).
631 //
632 // The second is a list of ndk_library modules. These need to be
633 // separated because they are a variation dependency and must be added
634 // in a different manner.
635 rewriteNdkLibs := func(list []string) ([]string, []string) {
636 variantLibs := []string{}
637 nonvariantLibs := []string{}
638 for _, entry := range list {
Dan Willemsen72d39932016-07-08 23:23:48 -0700639 if inList(entry, ndkPrebuiltSharedLibraries) {
Dan Albert914449f2016-06-17 16:45:24 -0700640 if !inList(entry, ndkMigratedLibs) {
641 nonvariantLibs = append(nonvariantLibs, entry+".ndk."+version)
642 } else {
643 variantLibs = append(variantLibs, entry+ndkLibrarySuffix)
644 }
645 } else {
646 nonvariantLibs = append(variantLibs, entry)
Dan Willemsen72d39932016-07-08 23:23:48 -0700647 }
648 }
Dan Albert914449f2016-06-17 16:45:24 -0700649 return nonvariantLibs, variantLibs
Dan Willemsen72d39932016-07-08 23:23:48 -0700650 }
651
Dan Albert914449f2016-06-17 16:45:24 -0700652 deps.SharedLibs, variantNdkLibs = rewriteNdkLibs(deps.SharedLibs)
653 deps.LateSharedLibs, variantLateNdkLibs = rewriteNdkLibs(deps.LateSharedLibs)
Dan Willemsen72d39932016-07-08 23:23:48 -0700654 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700655
Colin Cross5950f382016-12-13 12:50:57 -0800656 actx.AddVariationDependencies(nil, headerDepTag, deps.HeaderLibs...)
657
Colin Crossc99deeb2016-04-11 15:06:20 -0700658 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, wholeStaticDepTag,
659 deps.WholeStaticLibs...)
660
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700661 for _, lib := range deps.StaticLibs {
662 depTag := staticDepTag
663 if inList(lib, deps.ReexportStaticLibHeaders) {
664 depTag = staticExportDepTag
665 }
Colin Cross15a0d462016-07-14 14:49:58 -0700666 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700667 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700668
669 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, lateStaticDepTag,
670 deps.LateStaticLibs...)
671
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700672 for _, lib := range deps.SharedLibs {
673 depTag := sharedDepTag
674 if inList(lib, deps.ReexportSharedLibHeaders) {
675 depTag = sharedExportDepTag
676 }
Colin Cross15a0d462016-07-14 14:49:58 -0700677 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700678 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700679
680 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, lateSharedDepTag,
681 deps.LateSharedLibs...)
682
Colin Cross68861832016-07-08 10:41:41 -0700683 actx.AddDependency(c, genSourceDepTag, deps.GeneratedSources...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700684
685 for _, gen := range deps.GeneratedHeaders {
686 depTag := genHeaderDepTag
687 if inList(gen, deps.ReexportGeneratedHeaders) {
688 depTag = genHeaderExportDepTag
689 }
690 actx.AddDependency(c, depTag, gen)
691 }
Dan Willemsenb40aab62016-04-20 14:21:14 -0700692
Colin Cross68861832016-07-08 10:41:41 -0700693 actx.AddDependency(c, objDepTag, deps.ObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700694
695 if deps.CrtBegin != "" {
Colin Cross68861832016-07-08 10:41:41 -0700696 actx.AddDependency(c, crtBeginDepTag, deps.CrtBegin)
Colin Crossca860ac2016-01-04 14:34:37 -0800697 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700698 if deps.CrtEnd != "" {
Colin Cross68861832016-07-08 10:41:41 -0700699 actx.AddDependency(c, crtEndDepTag, deps.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700700 }
Dan Albert914449f2016-06-17 16:45:24 -0700701
702 version := ctx.sdkVersion()
703 actx.AddVariationDependencies([]blueprint.Variation{
704 {"ndk_api", version}, {"link", "shared"}}, ndkStubDepTag, variantNdkLibs...)
705 actx.AddVariationDependencies([]blueprint.Variation{
706 {"ndk_api", version}, {"link", "shared"}}, ndkLateStubDepTag, variantLateNdkLibs...)
Colin Cross6362e272015-10-29 15:25:03 -0700707}
Colin Cross21b9a242015-03-24 14:15:58 -0700708
Dan Albert7e9d2952016-08-04 13:02:36 -0700709func beginMutator(ctx android.BottomUpMutatorContext) {
710 if c, ok := ctx.Module().(*Module); ok && c.Enabled() {
711 c.beginMutator(ctx)
712 }
713}
714
Colin Crossca860ac2016-01-04 14:34:37 -0800715func (c *Module) clang(ctx BaseModuleContext) bool {
716 clang := Bool(c.Properties.Clang)
717
718 if c.Properties.Clang == nil {
719 if ctx.Host() {
720 clang = true
721 }
722
723 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
724 clang = true
725 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800726 }
Colin Cross28344522015-04-22 13:07:53 -0700727
Colin Crossca860ac2016-01-04 14:34:37 -0800728 if !c.toolchain(ctx).ClangSupported() {
729 clang = false
730 }
731
732 return clang
733}
734
Colin Crossc99deeb2016-04-11 15:06:20 -0700735// Convert dependencies to paths. Returns a PathDeps containing paths
Colin Cross635c3b02016-05-18 15:37:25 -0700736func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
Colin Crossca860ac2016-01-04 14:34:37 -0800737 var depPaths PathDeps
Colin Crossca860ac2016-01-04 14:34:37 -0800738
Dan Willemsena96ff642016-06-07 12:34:45 -0700739 // Whether a module can link to another module, taking into
740 // account NDK linking.
Dan Albert9e10cd42016-08-03 14:12:14 -0700741 checkLinkType := func(from, to *Module) {
Dan Willemsena96ff642016-06-07 12:34:45 -0700742 if from.Target().Os != android.Android {
743 // Host code is not restricted
Dan Albert9e10cd42016-08-03 14:12:14 -0700744 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700745 }
746 if from.Properties.Sdk_version == "" {
747 // Platform code can link to anything
Dan Albert9e10cd42016-08-03 14:12:14 -0700748 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700749 }
Colin Crossb916a382016-07-29 17:28:03 -0700750 if _, ok := to.linker.(*toolchainLibraryDecorator); ok {
Dan Willemsena96ff642016-06-07 12:34:45 -0700751 // These are always allowed
Dan Albert9e10cd42016-08-03 14:12:14 -0700752 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700753 }
754 if _, ok := to.linker.(*ndkPrebuiltLibraryLinker); ok {
755 // These are allowed, but don't set sdk_version
Dan Albert9e10cd42016-08-03 14:12:14 -0700756 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700757 }
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700758 if _, ok := to.linker.(*ndkPrebuiltStlLinker); ok {
759 // These are allowed, but don't set sdk_version
Dan Albert9e10cd42016-08-03 14:12:14 -0700760 return
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700761 }
Colin Crossb916a382016-07-29 17:28:03 -0700762 if _, ok := to.linker.(*stubDecorator); ok {
Dan Albert914449f2016-06-17 16:45:24 -0700763 // These aren't real libraries, but are the stub shared libraries that are included in
764 // the NDK.
Dan Albert9e10cd42016-08-03 14:12:14 -0700765 return
Dan Albert914449f2016-06-17 16:45:24 -0700766 }
Dan Albert9e10cd42016-08-03 14:12:14 -0700767 if to.Properties.Sdk_version == "" {
768 // NDK code linking to platform code is never okay.
769 ctx.ModuleErrorf("depends on non-NDK-built library %q",
770 ctx.OtherModuleName(to))
771 }
772
773 // All this point we know we have two NDK libraries, but we need to
774 // check that we're not linking against anything built against a higher
775 // API level, as it is only valid to link against older or equivalent
776 // APIs.
777
778 if from.Properties.Sdk_version == "current" {
779 // Current can link against anything.
780 return
781 } else if to.Properties.Sdk_version == "current" {
782 // Current can't be linked against by anything else.
783 ctx.ModuleErrorf("links %q built against newer API version %q",
784 ctx.OtherModuleName(to), "current")
785 }
786
787 fromApi, err := strconv.Atoi(from.Properties.Sdk_version)
788 if err != nil {
789 ctx.PropertyErrorf("sdk_version",
790 "Invalid sdk_version value (must be int): %q",
791 from.Properties.Sdk_version)
792 }
793 toApi, err := strconv.Atoi(to.Properties.Sdk_version)
794 if err != nil {
795 ctx.PropertyErrorf("sdk_version",
796 "Invalid sdk_version value (must be int): %q",
797 to.Properties.Sdk_version)
798 }
799
800 if toApi > fromApi {
801 ctx.ModuleErrorf("links %q built against newer API version %q",
802 ctx.OtherModuleName(to), to.Properties.Sdk_version)
803 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700804 }
805
Colin Crossc99deeb2016-04-11 15:06:20 -0700806 ctx.VisitDirectDeps(func(m blueprint.Module) {
807 name := ctx.OtherModuleName(m)
808 tag := ctx.OtherModuleDependencyTag(m)
Colin Crossca860ac2016-01-04 14:34:37 -0800809
Colin Cross635c3b02016-05-18 15:37:25 -0700810 a, _ := m.(android.Module)
Colin Crossc99deeb2016-04-11 15:06:20 -0700811 if a == nil {
812 ctx.ModuleErrorf("module %q not an android module", name)
813 return
Colin Crossca860ac2016-01-04 14:34:37 -0800814 }
Colin Crossca860ac2016-01-04 14:34:37 -0800815
Dan Willemsena96ff642016-06-07 12:34:45 -0700816 cc, _ := m.(*Module)
817 if cc == nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700818 switch tag {
Colin Cross635c3b02016-05-18 15:37:25 -0700819 case android.DefaultsDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700820 case genSourceDepTag:
821 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
822 depPaths.GeneratedSources = append(depPaths.GeneratedSources,
823 genRule.GeneratedSourceFiles()...)
824 } else {
825 ctx.ModuleErrorf("module %q is not a gensrcs or genrule", name)
826 }
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700827 case genHeaderDepTag, genHeaderExportDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700828 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
829 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders,
830 genRule.GeneratedSourceFiles()...)
Colin Cross5ed99c62016-11-22 12:55:55 -0800831 flags := includeDirsToFlags(genRule.GeneratedHeaderDirs())
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700832 depPaths.Flags = append(depPaths.Flags, flags)
833 if tag == genHeaderExportDepTag {
834 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700835 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps,
836 genRule.GeneratedSourceFiles()...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700837 }
Dan Willemsenb40aab62016-04-20 14:21:14 -0700838 } else {
839 ctx.ModuleErrorf("module %q is not a genrule", name)
840 }
841 default:
Colin Crossc99deeb2016-04-11 15:06:20 -0700842 ctx.ModuleErrorf("depends on non-cc module %q", name)
Colin Crossca860ac2016-01-04 14:34:37 -0800843 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700844 return
845 }
846
847 if !a.Enabled() {
Colin Crossa8f5e9a2016-12-13 12:51:11 -0800848 if ctx.AConfig().AllowMissingDependencies() {
849 ctx.AddMissingDependencies([]string{name})
850 } else {
851 ctx.ModuleErrorf("depends on disabled module %q", name)
852 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700853 return
854 }
855
Colin Crossa1ad8d12016-06-01 17:09:44 -0700856 if a.Target().Os != ctx.Os() {
857 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), name)
858 return
859 }
860
861 if a.Target().Arch.ArchType != ctx.Arch().ArchType {
862 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700863 return
864 }
865
Colin Crossc99deeb2016-04-11 15:06:20 -0700866 if tag == reuseObjTag {
Colin Crossbba99042016-11-23 15:45:05 -0800867 if l, ok := cc.compiler.(libraryInterface); ok {
868 depPaths.Objs = depPaths.Objs.Append(l.reuseObjs())
869 return
870 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700871 }
872
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700873 if t, ok := tag.(dependencyTag); ok && t.library {
Dan Willemsena96ff642016-06-07 12:34:45 -0700874 if i, ok := cc.linker.(exportedFlagsProducer); ok {
Dan Willemsen76f08272016-07-09 00:14:08 -0700875 flags := i.exportedFlags()
Dan Willemsen847dcc72016-09-29 12:13:36 -0700876 deps := i.exportedFlagsDeps()
Dan Willemsen76f08272016-07-09 00:14:08 -0700877 depPaths.Flags = append(depPaths.Flags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700878 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders, deps...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700879
880 if t.reexportFlags {
Dan Willemsen76f08272016-07-09 00:14:08 -0700881 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700882 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps, deps...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700883 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700884 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700885
Dan Albert9e10cd42016-08-03 14:12:14 -0700886 checkLinkType(c, cc)
Colin Crossc99deeb2016-04-11 15:06:20 -0700887 }
888
Colin Cross26c34ed2016-09-30 17:10:16 -0700889 var ptr *android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -0700890 var depPtr *android.Paths
Colin Crossc99deeb2016-04-11 15:06:20 -0700891
Colin Cross26c34ed2016-09-30 17:10:16 -0700892 linkFile := cc.outputFile
893 depFile := android.OptionalPath{}
894
Colin Crossc99deeb2016-04-11 15:06:20 -0700895 switch tag {
Dan Albert914449f2016-06-17 16:45:24 -0700896 case ndkStubDepTag, sharedDepTag, sharedExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700897 ptr = &depPaths.SharedLibs
898 depPtr = &depPaths.SharedLibsDeps
899 depFile = cc.linker.(libraryInterface).toc()
Dan Albert914449f2016-06-17 16:45:24 -0700900 case lateSharedDepTag, ndkLateStubDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700901 ptr = &depPaths.LateSharedLibs
902 depPtr = &depPaths.LateSharedLibsDeps
903 depFile = cc.linker.(libraryInterface).toc()
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700904 case staticDepTag, staticExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700905 ptr = &depPaths.StaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -0700906 case lateStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700907 ptr = &depPaths.LateStaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -0700908 case wholeStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700909 ptr = &depPaths.WholeStaticLibs
Colin Crossb916a382016-07-29 17:28:03 -0700910 staticLib, ok := cc.linker.(libraryInterface)
911 if !ok || !staticLib.static() {
Dan Willemsena96ff642016-06-07 12:34:45 -0700912 ctx.ModuleErrorf("module %q not a static library", name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700913 return
914 }
915
916 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
917 postfix := " (required by " + ctx.OtherModuleName(m) + ")"
918 for i := range missingDeps {
919 missingDeps[i] += postfix
920 }
921 ctx.AddMissingDependencies(missingDeps)
922 }
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700923 depPaths.WholeStaticLibObjs = depPaths.WholeStaticLibObjs.Append(staticLib.objs())
Colin Cross5950f382016-12-13 12:50:57 -0800924 case headerDepTag:
925 // Nothing
Colin Crossc99deeb2016-04-11 15:06:20 -0700926 case objDepTag:
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700927 depPaths.Objs.objFiles = append(depPaths.Objs.objFiles, linkFile.Path())
Colin Crossc99deeb2016-04-11 15:06:20 -0700928 case crtBeginDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700929 depPaths.CrtBegin = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -0700930 case crtEndDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700931 depPaths.CrtEnd = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -0700932 }
933
Colin Cross26c34ed2016-09-30 17:10:16 -0700934 if ptr != nil {
Colin Crossce75d2c2016-10-06 16:12:58 -0700935 if !linkFile.Valid() {
936 ctx.ModuleErrorf("module %q missing output file", name)
937 return
938 }
Colin Cross26c34ed2016-09-30 17:10:16 -0700939 *ptr = append(*ptr, linkFile.Path())
940 }
941
Colin Crossc99deeb2016-04-11 15:06:20 -0700942 if depPtr != nil {
Colin Cross26c34ed2016-09-30 17:10:16 -0700943 dep := depFile
944 if !dep.Valid() {
945 dep = linkFile
946 }
947 *depPtr = append(*depPtr, dep.Path())
Colin Crossca860ac2016-01-04 14:34:37 -0800948 }
949 })
950
951 return depPaths
952}
953
954func (c *Module) InstallInData() bool {
955 if c.installer == nil {
956 return false
957 }
Colin Cross94610402016-08-29 13:41:32 -0700958 if c.sanitize != nil && c.sanitize.inData() {
959 return true
960 }
Colin Crossca860ac2016-01-04 14:34:37 -0800961 return c.installer.inData()
962}
963
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700964func (c *Module) HostToolPath() android.OptionalPath {
965 if c.installer == nil {
966 return android.OptionalPath{}
967 }
968 return c.installer.hostToolPath()
969}
970
Colin Cross2ba19d92015-05-07 15:44:20 -0700971//
Colin Crosscfad1192015-11-02 16:43:11 -0800972// Defaults
973//
Colin Crossca860ac2016-01-04 14:34:37 -0800974type Defaults struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700975 android.ModuleBase
976 android.DefaultsModule
Colin Crosscfad1192015-11-02 16:43:11 -0800977}
978
Colin Cross635c3b02016-05-18 15:37:25 -0700979func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -0800980}
981
Colin Cross1e676be2016-10-12 14:38:15 -0700982func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
983}
984
Colin Crossca860ac2016-01-04 14:34:37 -0800985func defaultsFactory() (blueprint.Module, []interface{}) {
Colin Crosse1d764e2016-08-18 14:18:32 -0700986 return DefaultsFactory()
987}
988
989func DefaultsFactory(props ...interface{}) (blueprint.Module, []interface{}) {
Colin Crossca860ac2016-01-04 14:34:37 -0800990 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -0800991
Colin Crosse1d764e2016-08-18 14:18:32 -0700992 props = append(props,
Colin Crossca860ac2016-01-04 14:34:37 -0800993 &BaseProperties{},
994 &BaseCompilerProperties{},
995 &BaseLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -0700996 &LibraryProperties{},
Colin Cross919281a2016-04-05 16:42:05 -0700997 &FlagExporterProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -0800998 &BinaryLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -0700999 &TestProperties{},
1000 &TestBinaryProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001001 &UnusedProperties{},
1002 &StlProperties{},
Colin Cross16b23492016-01-06 14:41:07 -08001003 &SanitizeProperties{},
Colin Cross665dce92016-04-28 14:50:03 -07001004 &StripProperties{},
Dan Willemsen7424d612016-09-01 13:45:39 -07001005 &InstallerProperties{},
Dan Willemsena03cf6d2016-09-26 15:45:04 -07001006 &TidyProperties{},
Colin Crosse1d764e2016-08-18 14:18:32 -07001007 )
Colin Crosscfad1192015-11-02 16:43:11 -08001008
Colin Crosse1d764e2016-08-18 14:18:32 -07001009 return android.InitDefaultsModule(module, module, props...)
Colin Crosscfad1192015-11-02 16:43:11 -08001010}
1011
Colin Cross74d1ec02015-04-28 13:30:13 -07001012// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
1013// modifies the slice contents in place, and returns a subslice of the original slice
1014func lastUniqueElements(list []string) []string {
1015 totalSkip := 0
1016 for i := len(list) - 1; i >= totalSkip; i-- {
1017 skip := 0
1018 for j := i - 1; j >= totalSkip; j-- {
1019 if list[i] == list[j] {
1020 skip++
1021 } else {
1022 list[j+skip] = list[j]
1023 }
1024 }
1025 totalSkip += skip
1026 }
1027 return list[totalSkip:]
1028}
Colin Cross06a931b2015-10-28 17:23:31 -07001029
1030var Bool = proptools.Bool