blob: b107d015ed1970953a3f759d56edc5c9d3be15ca [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()
Dan Willemsen581341d2017-02-09 16:16:31 -080049
50 ctx.BottomUp("coverage", coverageLinkingMutator).Parallel()
Colin Cross1e676be2016-10-12 14:38:15 -070051 })
Colin Crossb98c8b02016-07-29 13:44:28 -070052
53 pctx.Import("android/soong/cc/config")
Colin Cross463a90e2015-06-17 14:20:06 -070054}
55
Colin Crossca860ac2016-01-04 14:34:37 -080056type Deps struct {
57 SharedLibs, LateSharedLibs []string
58 StaticLibs, LateStaticLibs, WholeStaticLibs []string
Colin Cross5950f382016-12-13 12:50:57 -080059 HeaderLibs []string
Colin Crossc472d572015-03-17 15:06:21 -070060
Colin Cross5950f382016-12-13 12:50:57 -080061 ReexportSharedLibHeaders, ReexportStaticLibHeaders, ReexportHeaderLibHeaders []string
Dan Willemsen490a8dc2016-06-06 18:22:19 -070062
Colin Cross81413472016-04-11 14:37:39 -070063 ObjFiles []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -070064
Dan Willemsenb40aab62016-04-20 14:21:14 -070065 GeneratedSources []string
66 GeneratedHeaders []string
67
Dan Willemsenb3454ab2016-09-28 17:34:58 -070068 ReexportGeneratedHeaders []string
69
Colin Cross97ba0732015-03-23 17:50:24 -070070 CrtBegin, CrtEnd string
Colin Crossc472d572015-03-17 15:06:21 -070071}
72
Colin Crossca860ac2016-01-04 14:34:37 -080073type PathDeps struct {
Colin Cross26c34ed2016-09-30 17:10:16 -070074 // Paths to .so files
75 SharedLibs, LateSharedLibs android.Paths
76 // Paths to the dependencies to use for .so files (.so.toc files)
77 SharedLibsDeps, LateSharedLibsDeps android.Paths
78 // Paths to .a files
Colin Cross635c3b02016-05-18 15:37:25 -070079 StaticLibs, LateStaticLibs, WholeStaticLibs android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -070080
Colin Cross26c34ed2016-09-30 17:10:16 -070081 // Paths to .o files
Dan Willemsen5cb580f2016-09-26 17:33:01 -070082 Objs Objects
Dan Willemsen581341d2017-02-09 16:16:31 -080083 StaticLibObjs Objects
Dan Willemsen5cb580f2016-09-26 17:33:01 -070084 WholeStaticLibObjs Objects
Dan Willemsen34cc69e2015-09-23 15:26:20 -070085
Colin Cross26c34ed2016-09-30 17:10:16 -070086 // Paths to generated source files
Colin Cross635c3b02016-05-18 15:37:25 -070087 GeneratedSources android.Paths
88 GeneratedHeaders android.Paths
Dan Willemsenb40aab62016-04-20 14:21:14 -070089
Dan Willemsen76f08272016-07-09 00:14:08 -070090 Flags, ReexportedFlags []string
Dan Willemsen847dcc72016-09-29 12:13:36 -070091 ReexportedFlagsDeps android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -070092
Colin Cross26c34ed2016-09-30 17:10:16 -070093 // Paths to crt*.o files
Colin Cross635c3b02016-05-18 15:37:25 -070094 CrtBegin, CrtEnd android.OptionalPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -070095}
96
Colin Crossca860ac2016-01-04 14:34:37 -080097type Flags struct {
Colin Cross28344522015-04-22 13:07:53 -070098 GlobalFlags []string // Flags that apply to C, C++, and assembly source files
Vishwath Mohan83d9f712017-03-16 11:01:23 -070099 ArFlags []string // Flags that apply to ar
Colin Cross28344522015-04-22 13:07:53 -0700100 AsFlags []string // Flags that apply to assembly source files
101 CFlags []string // Flags that apply to C and C++ source files
102 ConlyFlags []string // Flags that apply to C source files
103 CppFlags []string // Flags that apply to C++ source files
104 YaccFlags []string // Flags that apply to Yacc source files
Colin Cross0c461f12016-10-20 16:11:43 -0700105 protoFlags []string // Flags that apply to proto source files
Dan Willemsene1240db2016-11-03 14:28:51 -0700106 aidlFlags []string // Flags that apply to aidl source files
Colin Cross28344522015-04-22 13:07:53 -0700107 LdFlags []string // Flags that apply to linker command lines
Colin Cross16b23492016-01-06 14:41:07 -0800108 libFlags []string // Flags to add libraries early to the link order
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700109 TidyFlags []string // Flags that apply to clang-tidy
Colin Cross91e90042016-12-02 17:13:24 -0800110 YasmFlags []string // Flags that apply to yasm assembly source files
Colin Cross28344522015-04-22 13:07:53 -0700111
Colin Crossc3199482017-03-30 15:03:04 -0700112 // Global include flags that apply to C, C++, and assembly source files
113 // These must be after any module include flags, which will be in GlobalFlags.
114 SystemIncludeFlags []string
115
Colin Crossb98c8b02016-07-29 13:44:28 -0700116 Toolchain config.Toolchain
Colin Cross28344522015-04-22 13:07:53 -0700117 Clang bool
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700118 Tidy bool
Dan Willemsen581341d2017-02-09 16:16:31 -0800119 Coverage bool
Colin Crossca860ac2016-01-04 14:34:37 -0800120
121 RequiredInstructionSet string
Colin Cross16b23492016-01-06 14:41:07 -0800122 DynamicLinker string
123
Colin Cross635c3b02016-05-18 15:37:25 -0700124 CFlagsDeps android.Paths // Files depended on by compiler flags
Colin Cross18c0c5a2016-12-01 14:45:23 -0800125
126 GroupStaticLibs bool
Colin Crossc472d572015-03-17 15:06:21 -0700127}
128
Colin Cross81413472016-04-11 14:37:39 -0700129type ObjectLinkerProperties struct {
130 // names of other cc_object modules to link into this module using partial linking
131 Objs []string `android:"arch_variant"`
132}
133
Colin Crossca860ac2016-01-04 14:34:37 -0800134// Properties used to compile all C or C++ modules
135type BaseProperties struct {
136 // compile module with clang instead of gcc
137 Clang *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700138
139 // Minimum sdk version supported when compiling against the ndk
140 Sdk_version string
141
Colin Crossca860ac2016-01-04 14:34:37 -0800142 // don't insert default compiler flags into asflags, cflags,
143 // cppflags, conlyflags, ldflags, or include_dirs
144 No_default_compiler_flags *bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700145
146 AndroidMkSharedLibs []string `blueprint:"mutated"`
Colin Crossbc6fb162016-05-24 15:39:04 -0700147 HideFromMake bool `blueprint:"mutated"`
Colin Crossb0f28952016-09-19 16:46:53 -0700148 PreventInstall bool `blueprint:"mutated"`
Colin Crossca860ac2016-01-04 14:34:37 -0800149}
150
Colin Crossca860ac2016-01-04 14:34:37 -0800151type UnusedProperties struct {
Dan Willemsen581341d2017-02-09 16:16:31 -0800152 Tags []string
Colin Crosscfad1192015-11-02 16:43:11 -0800153}
154
Colin Crossca860ac2016-01-04 14:34:37 -0800155type ModuleContextIntf interface {
Colin Crossca860ac2016-01-04 14:34:37 -0800156 static() bool
157 staticBinary() bool
158 clang() bool
Colin Crossb98c8b02016-07-29 13:44:28 -0700159 toolchain() config.Toolchain
Colin Crossca860ac2016-01-04 14:34:37 -0800160 noDefaultCompilerFlags() bool
161 sdk() bool
162 sdkVersion() string
Dan Willemsend2ede872016-11-18 14:54:24 -0800163 vndk() bool
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700164 selectedStl() string
Colin Crossce75d2c2016-10-06 16:12:58 -0700165 baseModuleName() string
Colin Crossca860ac2016-01-04 14:34:37 -0800166}
167
168type ModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700169 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800170 ModuleContextIntf
171}
172
173type BaseModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700174 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800175 ModuleContextIntf
176}
177
Colin Cross37047f12016-12-13 17:06:13 -0800178type DepsContext interface {
179 android.BottomUpMutatorContext
180 ModuleContextIntf
181}
182
Colin Crossca860ac2016-01-04 14:34:37 -0800183type feature interface {
184 begin(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800185 deps(ctx DepsContext, deps Deps) Deps
Colin Crossca860ac2016-01-04 14:34:37 -0800186 flags(ctx ModuleContext, flags Flags) Flags
187 props() []interface{}
188}
189
190type compiler interface {
Colin Cross42742b82016-08-01 13:20:05 -0700191 compilerInit(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800192 compilerDeps(ctx DepsContext, deps Deps) Deps
Colin Cross42742b82016-08-01 13:20:05 -0700193 compilerFlags(ctx ModuleContext, flags Flags) Flags
194 compilerProps() []interface{}
195
Colin Cross76fada02016-07-27 10:31:13 -0700196 appendCflags([]string)
197 appendAsflags([]string)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700198 compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects
Colin Crossca860ac2016-01-04 14:34:37 -0800199}
200
201type linker interface {
Colin Cross42742b82016-08-01 13:20:05 -0700202 linkerInit(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800203 linkerDeps(ctx DepsContext, deps Deps) Deps
Colin Cross42742b82016-08-01 13:20:05 -0700204 linkerFlags(ctx ModuleContext, flags Flags) Flags
205 linkerProps() []interface{}
206
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700207 link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path
Colin Cross76fada02016-07-27 10:31:13 -0700208 appendLdflags([]string)
Colin Crossca860ac2016-01-04 14:34:37 -0800209}
210
211type installer interface {
Colin Cross42742b82016-08-01 13:20:05 -0700212 installerProps() []interface{}
Colin Cross635c3b02016-05-18 15:37:25 -0700213 install(ctx ModuleContext, path android.Path)
Colin Crossca860ac2016-01-04 14:34:37 -0800214 inData() bool
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700215 hostToolPath() android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800216}
217
Colin Crossc99deeb2016-04-11 15:06:20 -0700218type dependencyTag struct {
219 blueprint.BaseDependencyTag
220 name string
221 library bool
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700222
223 reexportFlags bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700224}
225
226var (
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700227 sharedDepTag = dependencyTag{name: "shared", library: true}
228 sharedExportDepTag = dependencyTag{name: "shared", library: true, reexportFlags: true}
229 lateSharedDepTag = dependencyTag{name: "late shared", library: true}
230 staticDepTag = dependencyTag{name: "static", library: true}
231 staticExportDepTag = dependencyTag{name: "static", library: true, reexportFlags: true}
232 lateStaticDepTag = dependencyTag{name: "late static", library: true}
233 wholeStaticDepTag = dependencyTag{name: "whole static", library: true, reexportFlags: true}
Colin Cross32ec36c2016-12-15 07:39:51 -0800234 headerDepTag = dependencyTag{name: "header", library: true}
235 headerExportDepTag = dependencyTag{name: "header", library: true, reexportFlags: true}
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700236 genSourceDepTag = dependencyTag{name: "gen source"}
237 genHeaderDepTag = dependencyTag{name: "gen header"}
238 genHeaderExportDepTag = dependencyTag{name: "gen header", reexportFlags: true}
239 objDepTag = dependencyTag{name: "obj"}
240 crtBeginDepTag = dependencyTag{name: "crtbegin"}
241 crtEndDepTag = dependencyTag{name: "crtend"}
242 reuseObjTag = dependencyTag{name: "reuse objects"}
243 ndkStubDepTag = dependencyTag{name: "ndk stub", library: true}
244 ndkLateStubDepTag = dependencyTag{name: "ndk late stub", library: true}
Colin Crossc99deeb2016-04-11 15:06:20 -0700245)
246
Colin Crossca860ac2016-01-04 14:34:37 -0800247// Module contains the properties and members used by all C/C++ module types, and implements
248// the blueprint.Module interface. It delegates to compiler, linker, and installer interfaces
249// to construct the output file. Behavior can be customized with a Customizer interface
250type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700251 android.ModuleBase
252 android.DefaultableModule
Colin Crossc472d572015-03-17 15:06:21 -0700253
Colin Crossca860ac2016-01-04 14:34:37 -0800254 Properties BaseProperties
255 unused UnusedProperties
Colin Crossfa138792015-04-24 17:31:52 -0700256
Colin Crossca860ac2016-01-04 14:34:37 -0800257 // initialize before calling Init
Colin Cross635c3b02016-05-18 15:37:25 -0700258 hod android.HostOrDeviceSupported
259 multilib android.Multilib
Colin Crossc472d572015-03-17 15:06:21 -0700260
Colin Crossca860ac2016-01-04 14:34:37 -0800261 // delegates, initialize before calling Init
Colin Crossb4ce0ec2016-09-13 13:41:39 -0700262 features []feature
263 compiler compiler
264 linker linker
265 installer installer
266 stl *stl
267 sanitize *sanitize
Dan Willemsen581341d2017-02-09 16:16:31 -0800268 coverage *coverage
Colin Cross16b23492016-01-06 14:41:07 -0800269
270 androidMkSharedLibDeps []string
Colin Cross74d1ec02015-04-28 13:30:13 -0700271
Colin Cross635c3b02016-05-18 15:37:25 -0700272 outputFile android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800273
Colin Crossb98c8b02016-07-29 13:44:28 -0700274 cachedToolchain config.Toolchain
Colin Crossb916a382016-07-29 17:28:03 -0700275
276 subAndroidMkOnce map[subAndroidMkProvider]bool
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800277
278 // Flags used to compile this module
279 flags Flags
Colin Crossc472d572015-03-17 15:06:21 -0700280}
281
Colin Crossca860ac2016-01-04 14:34:37 -0800282func (c *Module) Init() (blueprint.Module, []interface{}) {
283 props := []interface{}{&c.Properties, &c.unused}
Colin Crossca860ac2016-01-04 14:34:37 -0800284 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700285 props = append(props, c.compiler.compilerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800286 }
287 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700288 props = append(props, c.linker.linkerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800289 }
290 if c.installer != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700291 props = append(props, c.installer.installerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800292 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700293 if c.stl != nil {
294 props = append(props, c.stl.props()...)
295 }
Colin Cross16b23492016-01-06 14:41:07 -0800296 if c.sanitize != nil {
297 props = append(props, c.sanitize.props()...)
298 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800299 if c.coverage != nil {
300 props = append(props, c.coverage.props()...)
301 }
Colin Crossca860ac2016-01-04 14:34:37 -0800302 for _, feature := range c.features {
303 props = append(props, feature.props()...)
304 }
Colin Crossc472d572015-03-17 15:06:21 -0700305
Colin Cross635c3b02016-05-18 15:37:25 -0700306 _, props = android.InitAndroidArchModule(c, c.hod, c.multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700307
Colin Cross635c3b02016-05-18 15:37:25 -0700308 return android.InitDefaultableModule(c, c, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700309}
310
Colin Crossb916a382016-07-29 17:28:03 -0700311// Returns true for dependency roots (binaries)
312// TODO(ccross): also handle dlopenable libraries
313func (c *Module) isDependencyRoot() bool {
314 if root, ok := c.linker.(interface {
315 isDependencyRoot() bool
316 }); ok {
317 return root.isDependencyRoot()
318 }
319 return false
320}
321
Colin Crossca860ac2016-01-04 14:34:37 -0800322type baseModuleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700323 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800324 moduleContextImpl
325}
326
Colin Cross37047f12016-12-13 17:06:13 -0800327type depsContext struct {
328 android.BottomUpMutatorContext
329 moduleContextImpl
330}
331
Colin Crossca860ac2016-01-04 14:34:37 -0800332type moduleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700333 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800334 moduleContextImpl
335}
336
337type moduleContextImpl struct {
338 mod *Module
339 ctx BaseModuleContext
340}
341
Colin Crossca860ac2016-01-04 14:34:37 -0800342func (ctx *moduleContextImpl) clang() bool {
343 return ctx.mod.clang(ctx.ctx)
344}
345
Colin Crossb98c8b02016-07-29 13:44:28 -0700346func (ctx *moduleContextImpl) toolchain() config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800347 return ctx.mod.toolchain(ctx.ctx)
348}
349
350func (ctx *moduleContextImpl) static() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700351 if static, ok := ctx.mod.linker.(interface {
352 static() bool
353 }); ok {
354 return static.static()
Colin Crossca860ac2016-01-04 14:34:37 -0800355 }
Colin Crossb916a382016-07-29 17:28:03 -0700356 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800357}
358
359func (ctx *moduleContextImpl) staticBinary() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700360 if static, ok := ctx.mod.linker.(interface {
361 staticBinary() bool
362 }); ok {
363 return static.staticBinary()
Colin Crossca860ac2016-01-04 14:34:37 -0800364 }
Colin Crossb916a382016-07-29 17:28:03 -0700365 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800366}
367
368func (ctx *moduleContextImpl) noDefaultCompilerFlags() bool {
369 return Bool(ctx.mod.Properties.No_default_compiler_flags)
370}
371
372func (ctx *moduleContextImpl) sdk() bool {
Dan Willemsena96ff642016-06-07 12:34:45 -0700373 if ctx.ctx.Device() {
374 return ctx.mod.Properties.Sdk_version != ""
375 }
376 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800377}
378
379func (ctx *moduleContextImpl) sdkVersion() string {
Dan Willemsena96ff642016-06-07 12:34:45 -0700380 if ctx.ctx.Device() {
Dan Willemsen11b26142017-03-19 18:30:37 -0700381 if ctx.vndk() {
382 return "current"
Dan Willemsend2ede872016-11-18 14:54:24 -0800383 } else {
384 return ctx.mod.Properties.Sdk_version
385 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700386 }
387 return ""
Colin Crossca860ac2016-01-04 14:34:37 -0800388}
389
Dan Willemsend2ede872016-11-18 14:54:24 -0800390func (ctx *moduleContextImpl) vndk() bool {
Dan Willemsen11b26142017-03-19 18:30:37 -0700391 return ctx.ctx.Os() == android.Android && ctx.ctx.Proprietary() && ctx.ctx.DeviceConfig().CompileVndk()
Dan Willemsend2ede872016-11-18 14:54:24 -0800392}
393
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700394func (ctx *moduleContextImpl) selectedStl() string {
395 if stl := ctx.mod.stl; stl != nil {
396 return stl.Properties.SelectedStl
397 }
398 return ""
399}
400
Colin Crossce75d2c2016-10-06 16:12:58 -0700401func (ctx *moduleContextImpl) baseModuleName() string {
402 return ctx.mod.ModuleBase.BaseModuleName()
403}
404
Colin Cross635c3b02016-05-18 15:37:25 -0700405func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800406 return &Module{
407 hod: hod,
408 multilib: multilib,
409 }
410}
411
Colin Cross635c3b02016-05-18 15:37:25 -0700412func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800413 module := newBaseModule(hod, multilib)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700414 module.features = []feature{
415 &tidyFeature{},
416 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700417 module.stl = &stl{}
Colin Cross16b23492016-01-06 14:41:07 -0800418 module.sanitize = &sanitize{}
Dan Willemsen581341d2017-02-09 16:16:31 -0800419 module.coverage = &coverage{}
Colin Crossca860ac2016-01-04 14:34:37 -0800420 return module
421}
422
Colin Crossce75d2c2016-10-06 16:12:58 -0700423func (c *Module) Prebuilt() *android.Prebuilt {
424 if p, ok := c.linker.(prebuiltLinkerInterface); ok {
425 return p.prebuilt()
426 }
427 return nil
428}
429
430func (c *Module) Name() string {
431 name := c.ModuleBase.Name()
432 if p, ok := c.linker.(prebuiltLinkerInterface); ok {
433 name = p.Name(name)
434 }
435 return name
436}
437
Colin Cross635c3b02016-05-18 15:37:25 -0700438func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800439 ctx := &moduleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700440 ModuleContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800441 moduleContextImpl: moduleContextImpl{
442 mod: c,
443 },
444 }
445 ctx.ctx = ctx
446
447 flags := Flags{
448 Toolchain: c.toolchain(ctx),
449 Clang: c.clang(ctx),
450 }
Colin Crossca860ac2016-01-04 14:34:37 -0800451 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700452 flags = c.compiler.compilerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800453 }
454 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700455 flags = c.linker.linkerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800456 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700457 if c.stl != nil {
458 flags = c.stl.flags(ctx, flags)
459 }
Colin Cross16b23492016-01-06 14:41:07 -0800460 if c.sanitize != nil {
461 flags = c.sanitize.flags(ctx, flags)
462 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800463 if c.coverage != nil {
464 flags = c.coverage.flags(ctx, flags)
465 }
Colin Crossca860ac2016-01-04 14:34:37 -0800466 for _, feature := range c.features {
467 flags = feature.flags(ctx, flags)
468 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800469 if ctx.Failed() {
470 return
471 }
472
Colin Crossb98c8b02016-07-29 13:44:28 -0700473 flags.CFlags, _ = filterList(flags.CFlags, config.IllegalFlags)
474 flags.CppFlags, _ = filterList(flags.CppFlags, config.IllegalFlags)
475 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, config.IllegalFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800476
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800477 deps := c.depsToPaths(ctx)
478 if ctx.Failed() {
479 return
480 }
481 flags.GlobalFlags = append(flags.GlobalFlags, deps.Flags...)
482 c.flags = flags
483
Colin Crossca860ac2016-01-04 14:34:37 -0800484 // Optimization to reduce size of build.ninja
485 // Replace the long list of flags for each file with a module-local variable
486 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
487 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
488 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
489 flags.CFlags = []string{"$cflags"}
490 flags.CppFlags = []string{"$cppflags"}
491 flags.AsFlags = []string{"$asflags"}
492
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700493 var objs Objects
Colin Crossca860ac2016-01-04 14:34:37 -0800494 if c.compiler != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700495 objs = c.compiler.compile(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -0800496 if ctx.Failed() {
497 return
498 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800499 }
500
Colin Crossca860ac2016-01-04 14:34:37 -0800501 if c.linker != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700502 outputFile := c.linker.link(ctx, flags, deps, objs)
Colin Crossca860ac2016-01-04 14:34:37 -0800503 if ctx.Failed() {
504 return
505 }
Colin Cross635c3b02016-05-18 15:37:25 -0700506 c.outputFile = android.OptionalPathForPath(outputFile)
Colin Crossce75d2c2016-10-06 16:12:58 -0700507 }
Colin Cross5049f022015-03-18 13:28:46 -0700508
Colin Crossce75d2c2016-10-06 16:12:58 -0700509 if c.installer != nil && !c.Properties.PreventInstall && c.outputFile.Valid() {
510 c.installer.install(ctx, c.outputFile.Path())
511 if ctx.Failed() {
512 return
Colin Crossca860ac2016-01-04 14:34:37 -0800513 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700514 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800515}
516
Colin Crossb98c8b02016-07-29 13:44:28 -0700517func (c *Module) toolchain(ctx BaseModuleContext) config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800518 if c.cachedToolchain == nil {
Colin Crossb98c8b02016-07-29 13:44:28 -0700519 c.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
Colin Cross3f40fa42015-01-30 17:27:36 -0800520 }
Colin Crossca860ac2016-01-04 14:34:37 -0800521 return c.cachedToolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800522}
523
Colin Crossca860ac2016-01-04 14:34:37 -0800524func (c *Module) begin(ctx BaseModuleContext) {
525 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700526 c.compiler.compilerInit(ctx)
Colin Cross21b9a242015-03-24 14:15:58 -0700527 }
Colin Crossca860ac2016-01-04 14:34:37 -0800528 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700529 c.linker.linkerInit(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800530 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700531 if c.stl != nil {
532 c.stl.begin(ctx)
533 }
Colin Cross16b23492016-01-06 14:41:07 -0800534 if c.sanitize != nil {
535 c.sanitize.begin(ctx)
536 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800537 if c.coverage != nil {
538 c.coverage.begin(ctx)
539 }
Colin Crossca860ac2016-01-04 14:34:37 -0800540 for _, feature := range c.features {
541 feature.begin(ctx)
542 }
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700543 if ctx.sdk() {
544 version, err := normalizeNdkApiLevel(ctx.sdkVersion(), ctx.Arch())
545 if err != nil {
546 ctx.PropertyErrorf("sdk_version", err.Error())
547 }
Dan Albert90f7a4d2016-11-08 14:34:24 -0800548 c.Properties.Sdk_version = version
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700549 }
Colin Crossca860ac2016-01-04 14:34:37 -0800550}
551
Colin Cross37047f12016-12-13 17:06:13 -0800552func (c *Module) deps(ctx DepsContext) Deps {
Colin Crossc99deeb2016-04-11 15:06:20 -0700553 deps := Deps{}
554
555 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700556 deps = c.compiler.compilerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700557 }
558 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700559 deps = c.linker.linkerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700560 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700561 if c.stl != nil {
562 deps = c.stl.deps(ctx, deps)
563 }
Colin Cross16b23492016-01-06 14:41:07 -0800564 if c.sanitize != nil {
565 deps = c.sanitize.deps(ctx, deps)
566 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800567 if c.coverage != nil {
568 deps = c.coverage.deps(ctx, deps)
569 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700570 for _, feature := range c.features {
571 deps = feature.deps(ctx, deps)
572 }
573
574 deps.WholeStaticLibs = lastUniqueElements(deps.WholeStaticLibs)
575 deps.StaticLibs = lastUniqueElements(deps.StaticLibs)
576 deps.LateStaticLibs = lastUniqueElements(deps.LateStaticLibs)
577 deps.SharedLibs = lastUniqueElements(deps.SharedLibs)
578 deps.LateSharedLibs = lastUniqueElements(deps.LateSharedLibs)
Colin Cross5950f382016-12-13 12:50:57 -0800579 deps.HeaderLibs = lastUniqueElements(deps.HeaderLibs)
Colin Crossc99deeb2016-04-11 15:06:20 -0700580
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700581 for _, lib := range deps.ReexportSharedLibHeaders {
582 if !inList(lib, deps.SharedLibs) {
583 ctx.PropertyErrorf("export_shared_lib_headers", "Shared library not in shared_libs: '%s'", lib)
584 }
585 }
586
587 for _, lib := range deps.ReexportStaticLibHeaders {
588 if !inList(lib, deps.StaticLibs) {
589 ctx.PropertyErrorf("export_static_lib_headers", "Static library not in static_libs: '%s'", lib)
590 }
591 }
592
Colin Cross5950f382016-12-13 12:50:57 -0800593 for _, lib := range deps.ReexportHeaderLibHeaders {
594 if !inList(lib, deps.HeaderLibs) {
595 ctx.PropertyErrorf("export_header_lib_headers", "Header library not in header_libs: '%s'", lib)
596 }
597 }
598
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700599 for _, gen := range deps.ReexportGeneratedHeaders {
600 if !inList(gen, deps.GeneratedHeaders) {
601 ctx.PropertyErrorf("export_generated_headers", "Generated header module not in generated_headers: '%s'", gen)
602 }
603 }
604
Colin Crossc99deeb2016-04-11 15:06:20 -0700605 return deps
606}
607
Dan Albert7e9d2952016-08-04 13:02:36 -0700608func (c *Module) beginMutator(actx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800609 ctx := &baseModuleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700610 BaseContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800611 moduleContextImpl: moduleContextImpl{
612 mod: c,
613 },
614 }
615 ctx.ctx = ctx
616
Colin Crossca860ac2016-01-04 14:34:37 -0800617 c.begin(ctx)
Dan Albert7e9d2952016-08-04 13:02:36 -0700618}
619
Colin Cross1e676be2016-10-12 14:38:15 -0700620func (c *Module) DepsMutator(actx android.BottomUpMutatorContext) {
621 if !c.Enabled() {
622 return
623 }
624
Colin Cross37047f12016-12-13 17:06:13 -0800625 ctx := &depsContext{
626 BottomUpMutatorContext: actx,
Dan Albert7e9d2952016-08-04 13:02:36 -0700627 moduleContextImpl: moduleContextImpl{
628 mod: c,
629 },
630 }
631 ctx.ctx = ctx
Colin Crossca860ac2016-01-04 14:34:37 -0800632
Colin Crossc99deeb2016-04-11 15:06:20 -0700633 deps := c.deps(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800634
Colin Crossb5bc4b42016-07-11 16:11:59 -0700635 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.SharedLibs...)
636 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.LateSharedLibs...)
Dan Willemsen72d39932016-07-08 23:23:48 -0700637
Dan Albert914449f2016-06-17 16:45:24 -0700638 variantNdkLibs := []string{}
639 variantLateNdkLibs := []string{}
Dan Willemsend2ede872016-11-18 14:54:24 -0800640 if ctx.sdk() || ctx.vndk() {
Dan Albert914449f2016-06-17 16:45:24 -0700641 version := ctx.sdkVersion()
Dan Willemsen72d39932016-07-08 23:23:48 -0700642
Dan Albert914449f2016-06-17 16:45:24 -0700643 // Rewrites the names of shared libraries into the names of the NDK
644 // libraries where appropriate. This returns two slices.
645 //
646 // The first is a list of non-variant shared libraries (either rewritten
647 // NDK libraries to the modules in prebuilts/ndk, or not rewritten
648 // because they are not NDK libraries).
649 //
650 // The second is a list of ndk_library modules. These need to be
651 // separated because they are a variation dependency and must be added
652 // in a different manner.
653 rewriteNdkLibs := func(list []string) ([]string, []string) {
654 variantLibs := []string{}
655 nonvariantLibs := []string{}
656 for _, entry := range list {
Dan Willemsen72d39932016-07-08 23:23:48 -0700657 if inList(entry, ndkPrebuiltSharedLibraries) {
Dan Albert914449f2016-06-17 16:45:24 -0700658 if !inList(entry, ndkMigratedLibs) {
659 nonvariantLibs = append(nonvariantLibs, entry+".ndk."+version)
660 } else {
661 variantLibs = append(variantLibs, entry+ndkLibrarySuffix)
662 }
663 } else {
Dan Willemsen7cbf5f82017-03-28 00:08:30 -0700664 nonvariantLibs = append(nonvariantLibs, entry)
Dan Willemsen72d39932016-07-08 23:23:48 -0700665 }
666 }
Dan Albert914449f2016-06-17 16:45:24 -0700667 return nonvariantLibs, variantLibs
Dan Willemsen72d39932016-07-08 23:23:48 -0700668 }
669
Dan Albert914449f2016-06-17 16:45:24 -0700670 deps.SharedLibs, variantNdkLibs = rewriteNdkLibs(deps.SharedLibs)
671 deps.LateSharedLibs, variantLateNdkLibs = rewriteNdkLibs(deps.LateSharedLibs)
Dan Willemsen72d39932016-07-08 23:23:48 -0700672 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700673
Colin Cross32ec36c2016-12-15 07:39:51 -0800674 for _, lib := range deps.HeaderLibs {
675 depTag := headerDepTag
676 if inList(lib, deps.ReexportHeaderLibHeaders) {
677 depTag = headerExportDepTag
678 }
679 actx.AddVariationDependencies(nil, depTag, lib)
680 }
Colin Cross5950f382016-12-13 12:50:57 -0800681
Colin Crossc99deeb2016-04-11 15:06:20 -0700682 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, wholeStaticDepTag,
683 deps.WholeStaticLibs...)
684
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700685 for _, lib := range deps.StaticLibs {
686 depTag := staticDepTag
687 if inList(lib, deps.ReexportStaticLibHeaders) {
688 depTag = staticExportDepTag
689 }
Colin Cross15a0d462016-07-14 14:49:58 -0700690 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700691 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700692
693 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, lateStaticDepTag,
694 deps.LateStaticLibs...)
695
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700696 for _, lib := range deps.SharedLibs {
697 depTag := sharedDepTag
698 if inList(lib, deps.ReexportSharedLibHeaders) {
699 depTag = sharedExportDepTag
700 }
Colin Cross15a0d462016-07-14 14:49:58 -0700701 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700702 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700703
704 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, lateSharedDepTag,
705 deps.LateSharedLibs...)
706
Colin Cross68861832016-07-08 10:41:41 -0700707 actx.AddDependency(c, genSourceDepTag, deps.GeneratedSources...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700708
709 for _, gen := range deps.GeneratedHeaders {
710 depTag := genHeaderDepTag
711 if inList(gen, deps.ReexportGeneratedHeaders) {
712 depTag = genHeaderExportDepTag
713 }
714 actx.AddDependency(c, depTag, gen)
715 }
Dan Willemsenb40aab62016-04-20 14:21:14 -0700716
Colin Cross68861832016-07-08 10:41:41 -0700717 actx.AddDependency(c, objDepTag, deps.ObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700718
719 if deps.CrtBegin != "" {
Colin Cross68861832016-07-08 10:41:41 -0700720 actx.AddDependency(c, crtBeginDepTag, deps.CrtBegin)
Colin Crossca860ac2016-01-04 14:34:37 -0800721 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700722 if deps.CrtEnd != "" {
Colin Cross68861832016-07-08 10:41:41 -0700723 actx.AddDependency(c, crtEndDepTag, deps.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700724 }
Dan Albert914449f2016-06-17 16:45:24 -0700725
726 version := ctx.sdkVersion()
727 actx.AddVariationDependencies([]blueprint.Variation{
728 {"ndk_api", version}, {"link", "shared"}}, ndkStubDepTag, variantNdkLibs...)
729 actx.AddVariationDependencies([]blueprint.Variation{
730 {"ndk_api", version}, {"link", "shared"}}, ndkLateStubDepTag, variantLateNdkLibs...)
Colin Cross6362e272015-10-29 15:25:03 -0700731}
Colin Cross21b9a242015-03-24 14:15:58 -0700732
Dan Albert7e9d2952016-08-04 13:02:36 -0700733func beginMutator(ctx android.BottomUpMutatorContext) {
734 if c, ok := ctx.Module().(*Module); ok && c.Enabled() {
735 c.beginMutator(ctx)
736 }
737}
738
Colin Crossca860ac2016-01-04 14:34:37 -0800739func (c *Module) clang(ctx BaseModuleContext) bool {
740 clang := Bool(c.Properties.Clang)
741
742 if c.Properties.Clang == nil {
743 if ctx.Host() {
744 clang = true
745 }
746
747 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
748 clang = true
749 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800750 }
Colin Cross28344522015-04-22 13:07:53 -0700751
Colin Crossca860ac2016-01-04 14:34:37 -0800752 if !c.toolchain(ctx).ClangSupported() {
753 clang = false
754 }
755
756 return clang
757}
758
Colin Crossc99deeb2016-04-11 15:06:20 -0700759// Convert dependencies to paths. Returns a PathDeps containing paths
Colin Cross635c3b02016-05-18 15:37:25 -0700760func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
Colin Crossca860ac2016-01-04 14:34:37 -0800761 var depPaths PathDeps
Colin Crossca860ac2016-01-04 14:34:37 -0800762
Dan Willemsena96ff642016-06-07 12:34:45 -0700763 // Whether a module can link to another module, taking into
764 // account NDK linking.
Dan Albert9e10cd42016-08-03 14:12:14 -0700765 checkLinkType := func(from, to *Module) {
Dan Willemsena96ff642016-06-07 12:34:45 -0700766 if from.Target().Os != android.Android {
767 // Host code is not restricted
Dan Albert9e10cd42016-08-03 14:12:14 -0700768 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700769 }
770 if from.Properties.Sdk_version == "" {
771 // Platform code can link to anything
Dan Albert9e10cd42016-08-03 14:12:14 -0700772 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700773 }
Colin Crossb916a382016-07-29 17:28:03 -0700774 if _, ok := to.linker.(*toolchainLibraryDecorator); ok {
Dan Willemsena96ff642016-06-07 12:34:45 -0700775 // These are always allowed
Dan Albert9e10cd42016-08-03 14:12:14 -0700776 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700777 }
778 if _, ok := to.linker.(*ndkPrebuiltLibraryLinker); ok {
779 // These are allowed, but don't set sdk_version
Dan Albert9e10cd42016-08-03 14:12:14 -0700780 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700781 }
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700782 if _, ok := to.linker.(*ndkPrebuiltStlLinker); ok {
783 // These are allowed, but don't set sdk_version
Dan Albert9e10cd42016-08-03 14:12:14 -0700784 return
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700785 }
Colin Crossb916a382016-07-29 17:28:03 -0700786 if _, ok := to.linker.(*stubDecorator); ok {
Dan Albert914449f2016-06-17 16:45:24 -0700787 // These aren't real libraries, but are the stub shared libraries that are included in
788 // the NDK.
Dan Albert9e10cd42016-08-03 14:12:14 -0700789 return
Dan Albert914449f2016-06-17 16:45:24 -0700790 }
Dan Albert9e10cd42016-08-03 14:12:14 -0700791 if to.Properties.Sdk_version == "" {
792 // NDK code linking to platform code is never okay.
793 ctx.ModuleErrorf("depends on non-NDK-built library %q",
794 ctx.OtherModuleName(to))
795 }
796
797 // All this point we know we have two NDK libraries, but we need to
798 // check that we're not linking against anything built against a higher
799 // API level, as it is only valid to link against older or equivalent
800 // APIs.
801
802 if from.Properties.Sdk_version == "current" {
803 // Current can link against anything.
804 return
805 } else if to.Properties.Sdk_version == "current" {
806 // Current can't be linked against by anything else.
807 ctx.ModuleErrorf("links %q built against newer API version %q",
808 ctx.OtherModuleName(to), "current")
809 }
810
811 fromApi, err := strconv.Atoi(from.Properties.Sdk_version)
812 if err != nil {
813 ctx.PropertyErrorf("sdk_version",
814 "Invalid sdk_version value (must be int): %q",
815 from.Properties.Sdk_version)
816 }
817 toApi, err := strconv.Atoi(to.Properties.Sdk_version)
818 if err != nil {
819 ctx.PropertyErrorf("sdk_version",
820 "Invalid sdk_version value (must be int): %q",
821 to.Properties.Sdk_version)
822 }
823
824 if toApi > fromApi {
825 ctx.ModuleErrorf("links %q built against newer API version %q",
826 ctx.OtherModuleName(to), to.Properties.Sdk_version)
827 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700828 }
829
Colin Crossc99deeb2016-04-11 15:06:20 -0700830 ctx.VisitDirectDeps(func(m blueprint.Module) {
831 name := ctx.OtherModuleName(m)
832 tag := ctx.OtherModuleDependencyTag(m)
Colin Crossca860ac2016-01-04 14:34:37 -0800833
Colin Cross635c3b02016-05-18 15:37:25 -0700834 a, _ := m.(android.Module)
Colin Crossc99deeb2016-04-11 15:06:20 -0700835 if a == nil {
836 ctx.ModuleErrorf("module %q not an android module", name)
837 return
Colin Crossca860ac2016-01-04 14:34:37 -0800838 }
Colin Crossca860ac2016-01-04 14:34:37 -0800839
Dan Willemsena96ff642016-06-07 12:34:45 -0700840 cc, _ := m.(*Module)
841 if cc == nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700842 switch tag {
Colin Cross068e0fe2016-12-13 15:23:47 -0800843 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700844 case genSourceDepTag:
845 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
846 depPaths.GeneratedSources = append(depPaths.GeneratedSources,
847 genRule.GeneratedSourceFiles()...)
848 } else {
849 ctx.ModuleErrorf("module %q is not a gensrcs or genrule", name)
850 }
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700851 case genHeaderDepTag, genHeaderExportDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700852 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
853 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders,
854 genRule.GeneratedSourceFiles()...)
Colin Cross5ed99c62016-11-22 12:55:55 -0800855 flags := includeDirsToFlags(genRule.GeneratedHeaderDirs())
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700856 depPaths.Flags = append(depPaths.Flags, flags)
857 if tag == genHeaderExportDepTag {
858 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700859 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps,
860 genRule.GeneratedSourceFiles()...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700861 }
Dan Willemsenb40aab62016-04-20 14:21:14 -0700862 } else {
863 ctx.ModuleErrorf("module %q is not a genrule", name)
864 }
865 default:
Colin Crossc99deeb2016-04-11 15:06:20 -0700866 ctx.ModuleErrorf("depends on non-cc module %q", name)
Colin Crossca860ac2016-01-04 14:34:37 -0800867 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700868 return
869 }
870
871 if !a.Enabled() {
Colin Crossa8f5e9a2016-12-13 12:51:11 -0800872 if ctx.AConfig().AllowMissingDependencies() {
873 ctx.AddMissingDependencies([]string{name})
874 } else {
875 ctx.ModuleErrorf("depends on disabled module %q", name)
876 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700877 return
878 }
879
Colin Crossa1ad8d12016-06-01 17:09:44 -0700880 if a.Target().Os != ctx.Os() {
881 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), name)
882 return
883 }
884
885 if a.Target().Arch.ArchType != ctx.Arch().ArchType {
886 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700887 return
888 }
889
Colin Crossc99deeb2016-04-11 15:06:20 -0700890 if tag == reuseObjTag {
Colin Crossbba99042016-11-23 15:45:05 -0800891 if l, ok := cc.compiler.(libraryInterface); ok {
892 depPaths.Objs = depPaths.Objs.Append(l.reuseObjs())
893 return
894 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700895 }
896
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700897 if t, ok := tag.(dependencyTag); ok && t.library {
Dan Willemsena96ff642016-06-07 12:34:45 -0700898 if i, ok := cc.linker.(exportedFlagsProducer); ok {
Dan Willemsen76f08272016-07-09 00:14:08 -0700899 flags := i.exportedFlags()
Dan Willemsen847dcc72016-09-29 12:13:36 -0700900 deps := i.exportedFlagsDeps()
Dan Willemsen76f08272016-07-09 00:14:08 -0700901 depPaths.Flags = append(depPaths.Flags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700902 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders, deps...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700903
904 if t.reexportFlags {
Dan Willemsen76f08272016-07-09 00:14:08 -0700905 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700906 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps, deps...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700907 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700908 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700909
Dan Albert9e10cd42016-08-03 14:12:14 -0700910 checkLinkType(c, cc)
Colin Crossc99deeb2016-04-11 15:06:20 -0700911 }
912
Colin Cross26c34ed2016-09-30 17:10:16 -0700913 var ptr *android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -0700914 var depPtr *android.Paths
Colin Crossc99deeb2016-04-11 15:06:20 -0700915
Colin Cross26c34ed2016-09-30 17:10:16 -0700916 linkFile := cc.outputFile
917 depFile := android.OptionalPath{}
918
Colin Crossc99deeb2016-04-11 15:06:20 -0700919 switch tag {
Dan Albert914449f2016-06-17 16:45:24 -0700920 case ndkStubDepTag, sharedDepTag, sharedExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700921 ptr = &depPaths.SharedLibs
922 depPtr = &depPaths.SharedLibsDeps
923 depFile = cc.linker.(libraryInterface).toc()
Dan Albert914449f2016-06-17 16:45:24 -0700924 case lateSharedDepTag, ndkLateStubDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700925 ptr = &depPaths.LateSharedLibs
926 depPtr = &depPaths.LateSharedLibsDeps
927 depFile = cc.linker.(libraryInterface).toc()
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700928 case staticDepTag, staticExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700929 ptr = &depPaths.StaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -0700930 case lateStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700931 ptr = &depPaths.LateStaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -0700932 case wholeStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700933 ptr = &depPaths.WholeStaticLibs
Colin Crossb916a382016-07-29 17:28:03 -0700934 staticLib, ok := cc.linker.(libraryInterface)
935 if !ok || !staticLib.static() {
Dan Willemsena96ff642016-06-07 12:34:45 -0700936 ctx.ModuleErrorf("module %q not a static library", name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700937 return
938 }
939
940 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
941 postfix := " (required by " + ctx.OtherModuleName(m) + ")"
942 for i := range missingDeps {
943 missingDeps[i] += postfix
944 }
945 ctx.AddMissingDependencies(missingDeps)
946 }
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700947 depPaths.WholeStaticLibObjs = depPaths.WholeStaticLibObjs.Append(staticLib.objs())
Colin Cross5950f382016-12-13 12:50:57 -0800948 case headerDepTag:
949 // Nothing
Colin Crossc99deeb2016-04-11 15:06:20 -0700950 case objDepTag:
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700951 depPaths.Objs.objFiles = append(depPaths.Objs.objFiles, linkFile.Path())
Colin Crossc99deeb2016-04-11 15:06:20 -0700952 case crtBeginDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700953 depPaths.CrtBegin = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -0700954 case crtEndDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700955 depPaths.CrtEnd = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -0700956 }
957
Dan Willemsen581341d2017-02-09 16:16:31 -0800958 switch tag {
959 case staticDepTag, staticExportDepTag, lateStaticDepTag:
960 staticLib, ok := cc.linker.(libraryInterface)
961 if !ok || !staticLib.static() {
962 ctx.ModuleErrorf("module %q not a static library", name)
963 return
964 }
965
966 // When combining coverage files for shared libraries and executables, coverage files
967 // in static libraries act as if they were whole static libraries.
968 depPaths.StaticLibObjs.coverageFiles = append(depPaths.StaticLibObjs.coverageFiles,
969 staticLib.objs().coverageFiles...)
970 }
971
Colin Cross26c34ed2016-09-30 17:10:16 -0700972 if ptr != nil {
Colin Crossce75d2c2016-10-06 16:12:58 -0700973 if !linkFile.Valid() {
974 ctx.ModuleErrorf("module %q missing output file", name)
975 return
976 }
Colin Cross26c34ed2016-09-30 17:10:16 -0700977 *ptr = append(*ptr, linkFile.Path())
978 }
979
Colin Crossc99deeb2016-04-11 15:06:20 -0700980 if depPtr != nil {
Colin Cross26c34ed2016-09-30 17:10:16 -0700981 dep := depFile
982 if !dep.Valid() {
983 dep = linkFile
984 }
985 *depPtr = append(*depPtr, dep.Path())
Colin Crossca860ac2016-01-04 14:34:37 -0800986 }
987 })
988
989 return depPaths
990}
991
992func (c *Module) InstallInData() bool {
993 if c.installer == nil {
994 return false
995 }
Colin Cross94610402016-08-29 13:41:32 -0700996 if c.sanitize != nil && c.sanitize.inData() {
997 return true
998 }
Colin Crossca860ac2016-01-04 14:34:37 -0800999 return c.installer.inData()
1000}
1001
Dan Willemsen4aa75ca2016-09-28 16:18:03 -07001002func (c *Module) HostToolPath() android.OptionalPath {
1003 if c.installer == nil {
1004 return android.OptionalPath{}
1005 }
1006 return c.installer.hostToolPath()
1007}
1008
Colin Cross2ba19d92015-05-07 15:44:20 -07001009//
Colin Crosscfad1192015-11-02 16:43:11 -08001010// Defaults
1011//
Colin Crossca860ac2016-01-04 14:34:37 -08001012type Defaults struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001013 android.ModuleBase
1014 android.DefaultsModule
Colin Crosscfad1192015-11-02 16:43:11 -08001015}
1016
Colin Cross635c3b02016-05-18 15:37:25 -07001017func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -08001018}
1019
Colin Cross1e676be2016-10-12 14:38:15 -07001020func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
1021}
1022
Colin Crossca860ac2016-01-04 14:34:37 -08001023func defaultsFactory() (blueprint.Module, []interface{}) {
Colin Crosse1d764e2016-08-18 14:18:32 -07001024 return DefaultsFactory()
1025}
1026
1027func DefaultsFactory(props ...interface{}) (blueprint.Module, []interface{}) {
Colin Crossca860ac2016-01-04 14:34:37 -08001028 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -08001029
Colin Crosse1d764e2016-08-18 14:18:32 -07001030 props = append(props,
Colin Crossca860ac2016-01-04 14:34:37 -08001031 &BaseProperties{},
1032 &BaseCompilerProperties{},
1033 &BaseLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -07001034 &LibraryProperties{},
Colin Cross919281a2016-04-05 16:42:05 -07001035 &FlagExporterProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001036 &BinaryLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -07001037 &TestProperties{},
1038 &TestBinaryProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001039 &UnusedProperties{},
1040 &StlProperties{},
Colin Cross16b23492016-01-06 14:41:07 -08001041 &SanitizeProperties{},
Colin Cross665dce92016-04-28 14:50:03 -07001042 &StripProperties{},
Dan Willemsen7424d612016-09-01 13:45:39 -07001043 &InstallerProperties{},
Dan Willemsena03cf6d2016-09-26 15:45:04 -07001044 &TidyProperties{},
Dan Willemsen581341d2017-02-09 16:16:31 -08001045 &CoverageProperties{},
Colin Crosse1d764e2016-08-18 14:18:32 -07001046 )
Colin Crosscfad1192015-11-02 16:43:11 -08001047
Colin Crosse1d764e2016-08-18 14:18:32 -07001048 return android.InitDefaultsModule(module, module, props...)
Colin Crosscfad1192015-11-02 16:43:11 -08001049}
1050
Colin Cross74d1ec02015-04-28 13:30:13 -07001051// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
1052// modifies the slice contents in place, and returns a subslice of the original slice
1053func lastUniqueElements(list []string) []string {
1054 totalSkip := 0
1055 for i := len(list) - 1; i >= totalSkip; i-- {
1056 skip := 0
1057 for j := i - 1; j >= totalSkip; j-- {
1058 if list[i] == list[j] {
1059 skip++
1060 } else {
1061 list[j+skip] = list[j]
1062 }
1063 }
1064 totalSkip += skip
1065 }
1066 return list[totalSkip:]
1067}
Colin Cross06a931b2015-10-28 17:23:31 -07001068
1069var Bool = proptools.Bool