blob: ec951887a0afcfbf7b6d209e142a1df1b60fc9a7 [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
99 AsFlags []string // Flags that apply to assembly source files
100 CFlags []string // Flags that apply to C and C++ source files
101 ConlyFlags []string // Flags that apply to C source files
102 CppFlags []string // Flags that apply to C++ source files
103 YaccFlags []string // Flags that apply to Yacc source files
Colin Cross0c461f12016-10-20 16:11:43 -0700104 protoFlags []string // Flags that apply to proto source files
Dan Willemsene1240db2016-11-03 14:28:51 -0700105 aidlFlags []string // Flags that apply to aidl source files
Colin Cross28344522015-04-22 13:07:53 -0700106 LdFlags []string // Flags that apply to linker command lines
Colin Cross16b23492016-01-06 14:41:07 -0800107 libFlags []string // Flags to add libraries early to the link order
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700108 TidyFlags []string // Flags that apply to clang-tidy
Colin Cross91e90042016-12-02 17:13:24 -0800109 YasmFlags []string // Flags that apply to yasm assembly source files
Colin Cross28344522015-04-22 13:07:53 -0700110
Colin Crossb98c8b02016-07-29 13:44:28 -0700111 Toolchain config.Toolchain
Colin Cross28344522015-04-22 13:07:53 -0700112 Clang bool
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700113 Tidy bool
Dan Willemsen581341d2017-02-09 16:16:31 -0800114 Coverage bool
Colin Crossca860ac2016-01-04 14:34:37 -0800115
116 RequiredInstructionSet string
Colin Cross16b23492016-01-06 14:41:07 -0800117 DynamicLinker string
118
Colin Cross635c3b02016-05-18 15:37:25 -0700119 CFlagsDeps android.Paths // Files depended on by compiler flags
Colin Cross18c0c5a2016-12-01 14:45:23 -0800120
121 GroupStaticLibs bool
Colin Crossc472d572015-03-17 15:06:21 -0700122}
123
Colin Cross81413472016-04-11 14:37:39 -0700124type ObjectLinkerProperties struct {
125 // names of other cc_object modules to link into this module using partial linking
126 Objs []string `android:"arch_variant"`
127}
128
Colin Crossca860ac2016-01-04 14:34:37 -0800129// Properties used to compile all C or C++ modules
130type BaseProperties struct {
131 // compile module with clang instead of gcc
132 Clang *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700133
134 // Minimum sdk version supported when compiling against the ndk
135 Sdk_version string
136
Dan Willemsend2ede872016-11-18 14:54:24 -0800137 // Whether to compile against the VNDK
138 Use_vndk bool
139
Colin Crossca860ac2016-01-04 14:34:37 -0800140 // don't insert default compiler flags into asflags, cflags,
141 // cppflags, conlyflags, ldflags, or include_dirs
142 No_default_compiler_flags *bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700143
144 AndroidMkSharedLibs []string `blueprint:"mutated"`
Colin Crossbc6fb162016-05-24 15:39:04 -0700145 HideFromMake bool `blueprint:"mutated"`
Colin Crossb0f28952016-09-19 16:46:53 -0700146 PreventInstall bool `blueprint:"mutated"`
Dan Willemsend2ede872016-11-18 14:54:24 -0800147 Vndk_version string `blueprint:"mutated"`
Colin Crossca860ac2016-01-04 14:34:37 -0800148}
149
Colin Crossca860ac2016-01-04 14:34:37 -0800150type UnusedProperties struct {
Dan Willemsen581341d2017-02-09 16:16:31 -0800151 Tags []string
Colin Crosscfad1192015-11-02 16:43:11 -0800152}
153
Colin Crossca860ac2016-01-04 14:34:37 -0800154type ModuleContextIntf interface {
Colin Crossca860ac2016-01-04 14:34:37 -0800155 static() bool
156 staticBinary() bool
157 clang() bool
Colin Crossb98c8b02016-07-29 13:44:28 -0700158 toolchain() config.Toolchain
Colin Crossca860ac2016-01-04 14:34:37 -0800159 noDefaultCompilerFlags() bool
160 sdk() bool
161 sdkVersion() string
Dan Willemsend2ede872016-11-18 14:54:24 -0800162 vndk() bool
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700163 selectedStl() string
Colin Crossce75d2c2016-10-06 16:12:58 -0700164 baseModuleName() string
Jiyong Park4c48f722017-01-20 08:57:02 +0900165 isNdk() bool
166 isVndk() bool
167 isSameProcessHal() bool
Colin Crossca860ac2016-01-04 14:34:37 -0800168}
169
170type ModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700171 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800172 ModuleContextIntf
173}
174
175type BaseModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700176 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800177 ModuleContextIntf
178}
179
Colin Cross37047f12016-12-13 17:06:13 -0800180type DepsContext interface {
181 android.BottomUpMutatorContext
182 ModuleContextIntf
183}
184
Colin Crossca860ac2016-01-04 14:34:37 -0800185type feature interface {
186 begin(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800187 deps(ctx DepsContext, deps Deps) Deps
Colin Crossca860ac2016-01-04 14:34:37 -0800188 flags(ctx ModuleContext, flags Flags) Flags
189 props() []interface{}
190}
191
192type compiler interface {
Colin Cross42742b82016-08-01 13:20:05 -0700193 compilerInit(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800194 compilerDeps(ctx DepsContext, deps Deps) Deps
Colin Cross42742b82016-08-01 13:20:05 -0700195 compilerFlags(ctx ModuleContext, flags Flags) Flags
196 compilerProps() []interface{}
197
Colin Cross76fada02016-07-27 10:31:13 -0700198 appendCflags([]string)
199 appendAsflags([]string)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700200 compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects
Colin Crossca860ac2016-01-04 14:34:37 -0800201}
202
203type linker interface {
Colin Cross42742b82016-08-01 13:20:05 -0700204 linkerInit(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800205 linkerDeps(ctx DepsContext, deps Deps) Deps
Colin Cross42742b82016-08-01 13:20:05 -0700206 linkerFlags(ctx ModuleContext, flags Flags) Flags
207 linkerProps() []interface{}
208
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700209 link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path
Colin Cross76fada02016-07-27 10:31:13 -0700210 appendLdflags([]string)
Colin Crossca860ac2016-01-04 14:34:37 -0800211}
212
213type installer interface {
Colin Cross42742b82016-08-01 13:20:05 -0700214 installerProps() []interface{}
Colin Cross635c3b02016-05-18 15:37:25 -0700215 install(ctx ModuleContext, path android.Path)
Colin Crossca860ac2016-01-04 14:34:37 -0800216 inData() bool
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700217 hostToolPath() android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800218}
219
Colin Crossc99deeb2016-04-11 15:06:20 -0700220type dependencyTag struct {
221 blueprint.BaseDependencyTag
222 name string
223 library bool
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700224
225 reexportFlags bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700226}
227
228var (
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700229 sharedDepTag = dependencyTag{name: "shared", library: true}
230 sharedExportDepTag = dependencyTag{name: "shared", library: true, reexportFlags: true}
231 lateSharedDepTag = dependencyTag{name: "late shared", library: true}
232 staticDepTag = dependencyTag{name: "static", library: true}
233 staticExportDepTag = dependencyTag{name: "static", library: true, reexportFlags: true}
234 lateStaticDepTag = dependencyTag{name: "late static", library: true}
235 wholeStaticDepTag = dependencyTag{name: "whole static", library: true, reexportFlags: true}
Colin Cross32ec36c2016-12-15 07:39:51 -0800236 headerDepTag = dependencyTag{name: "header", library: true}
237 headerExportDepTag = dependencyTag{name: "header", library: true, reexportFlags: true}
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700238 genSourceDepTag = dependencyTag{name: "gen source"}
239 genHeaderDepTag = dependencyTag{name: "gen header"}
240 genHeaderExportDepTag = dependencyTag{name: "gen header", reexportFlags: true}
241 objDepTag = dependencyTag{name: "obj"}
242 crtBeginDepTag = dependencyTag{name: "crtbegin"}
243 crtEndDepTag = dependencyTag{name: "crtend"}
244 reuseObjTag = dependencyTag{name: "reuse objects"}
245 ndkStubDepTag = dependencyTag{name: "ndk stub", library: true}
246 ndkLateStubDepTag = dependencyTag{name: "ndk late stub", library: true}
Colin Crossc99deeb2016-04-11 15:06:20 -0700247)
248
Colin Crossca860ac2016-01-04 14:34:37 -0800249// Module contains the properties and members used by all C/C++ module types, and implements
250// the blueprint.Module interface. It delegates to compiler, linker, and installer interfaces
251// to construct the output file. Behavior can be customized with a Customizer interface
252type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700253 android.ModuleBase
254 android.DefaultableModule
Colin Crossc472d572015-03-17 15:06:21 -0700255
Colin Crossca860ac2016-01-04 14:34:37 -0800256 Properties BaseProperties
257 unused UnusedProperties
Colin Crossfa138792015-04-24 17:31:52 -0700258
Colin Crossca860ac2016-01-04 14:34:37 -0800259 // initialize before calling Init
Colin Cross635c3b02016-05-18 15:37:25 -0700260 hod android.HostOrDeviceSupported
261 multilib android.Multilib
Colin Crossc472d572015-03-17 15:06:21 -0700262
Colin Crossca860ac2016-01-04 14:34:37 -0800263 // delegates, initialize before calling Init
Colin Crossb4ce0ec2016-09-13 13:41:39 -0700264 features []feature
265 compiler compiler
266 linker linker
267 installer installer
268 stl *stl
269 sanitize *sanitize
Dan Willemsen581341d2017-02-09 16:16:31 -0800270 coverage *coverage
Colin Cross16b23492016-01-06 14:41:07 -0800271
272 androidMkSharedLibDeps []string
Colin Cross74d1ec02015-04-28 13:30:13 -0700273
Colin Cross635c3b02016-05-18 15:37:25 -0700274 outputFile android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800275
Colin Crossb98c8b02016-07-29 13:44:28 -0700276 cachedToolchain config.Toolchain
Colin Crossb916a382016-07-29 17:28:03 -0700277
278 subAndroidMkOnce map[subAndroidMkProvider]bool
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800279
280 // Flags used to compile this module
281 flags Flags
Colin Crossc472d572015-03-17 15:06:21 -0700282}
283
Colin Crossca860ac2016-01-04 14:34:37 -0800284func (c *Module) Init() (blueprint.Module, []interface{}) {
285 props := []interface{}{&c.Properties, &c.unused}
Colin Crossca860ac2016-01-04 14:34:37 -0800286 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700287 props = append(props, c.compiler.compilerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800288 }
289 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700290 props = append(props, c.linker.linkerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800291 }
292 if c.installer != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700293 props = append(props, c.installer.installerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800294 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700295 if c.stl != nil {
296 props = append(props, c.stl.props()...)
297 }
Colin Cross16b23492016-01-06 14:41:07 -0800298 if c.sanitize != nil {
299 props = append(props, c.sanitize.props()...)
300 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800301 if c.coverage != nil {
302 props = append(props, c.coverage.props()...)
303 }
Colin Crossca860ac2016-01-04 14:34:37 -0800304 for _, feature := range c.features {
305 props = append(props, feature.props()...)
306 }
Colin Crossc472d572015-03-17 15:06:21 -0700307
Colin Cross635c3b02016-05-18 15:37:25 -0700308 _, props = android.InitAndroidArchModule(c, c.hod, c.multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700309
Colin Cross635c3b02016-05-18 15:37:25 -0700310 return android.InitDefaultableModule(c, c, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700311}
312
Colin Crossb916a382016-07-29 17:28:03 -0700313// Returns true for dependency roots (binaries)
314// TODO(ccross): also handle dlopenable libraries
315func (c *Module) isDependencyRoot() bool {
316 if root, ok := c.linker.(interface {
317 isDependencyRoot() bool
318 }); ok {
319 return root.isDependencyRoot()
320 }
321 return false
322}
323
Colin Crossca860ac2016-01-04 14:34:37 -0800324type baseModuleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700325 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800326 moduleContextImpl
327}
328
Colin Cross37047f12016-12-13 17:06:13 -0800329type depsContext struct {
330 android.BottomUpMutatorContext
331 moduleContextImpl
332}
333
Colin Crossca860ac2016-01-04 14:34:37 -0800334type moduleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700335 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800336 moduleContextImpl
337}
338
339type moduleContextImpl struct {
340 mod *Module
341 ctx BaseModuleContext
342}
343
Colin Crossca860ac2016-01-04 14:34:37 -0800344func (ctx *moduleContextImpl) clang() bool {
345 return ctx.mod.clang(ctx.ctx)
346}
347
Colin Crossb98c8b02016-07-29 13:44:28 -0700348func (ctx *moduleContextImpl) toolchain() config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800349 return ctx.mod.toolchain(ctx.ctx)
350}
351
352func (ctx *moduleContextImpl) static() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700353 if static, ok := ctx.mod.linker.(interface {
354 static() bool
355 }); ok {
356 return static.static()
Colin Crossca860ac2016-01-04 14:34:37 -0800357 }
Colin Crossb916a382016-07-29 17:28:03 -0700358 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800359}
360
361func (ctx *moduleContextImpl) staticBinary() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700362 if static, ok := ctx.mod.linker.(interface {
363 staticBinary() bool
364 }); ok {
365 return static.staticBinary()
Colin Crossca860ac2016-01-04 14:34:37 -0800366 }
Colin Crossb916a382016-07-29 17:28:03 -0700367 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800368}
369
370func (ctx *moduleContextImpl) noDefaultCompilerFlags() bool {
371 return Bool(ctx.mod.Properties.No_default_compiler_flags)
372}
373
374func (ctx *moduleContextImpl) sdk() bool {
Dan Willemsena96ff642016-06-07 12:34:45 -0700375 if ctx.ctx.Device() {
376 return ctx.mod.Properties.Sdk_version != ""
377 }
378 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800379}
380
381func (ctx *moduleContextImpl) sdkVersion() string {
Dan Willemsena96ff642016-06-07 12:34:45 -0700382 if ctx.ctx.Device() {
Dan Willemsend2ede872016-11-18 14:54:24 -0800383 if ctx.mod.Properties.Use_vndk {
384 return ctx.mod.Properties.Vndk_version
385 } else {
386 return ctx.mod.Properties.Sdk_version
387 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700388 }
389 return ""
Colin Crossca860ac2016-01-04 14:34:37 -0800390}
391
Dan Willemsend2ede872016-11-18 14:54:24 -0800392func (ctx *moduleContextImpl) vndk() bool {
393 if ctx.ctx.Device() {
394 return ctx.mod.Properties.Use_vndk
395 }
396 return false
397}
398
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700399func (ctx *moduleContextImpl) selectedStl() string {
400 if stl := ctx.mod.stl; stl != nil {
401 return stl.Properties.SelectedStl
402 }
403 return ""
404}
405
Colin Crossce75d2c2016-10-06 16:12:58 -0700406func (ctx *moduleContextImpl) baseModuleName() string {
407 return ctx.mod.ModuleBase.BaseModuleName()
408}
409
Jiyong Park4c48f722017-01-20 08:57:02 +0900410func (ctx *moduleContextImpl) isNdk() bool {
411 return inList(ctx.baseModuleName(), ndkPrebuiltSharedLibraries)
412}
413
414func (ctx *moduleContextImpl) isVndk() bool {
415 return config.IsVndkLibrary(ctx.baseModuleName())
416}
417
418func (ctx *moduleContextImpl) isSameProcessHal() bool {
419 return inList(ctx.baseModuleName(), ctx.ctx.DeviceConfig().SameProcessHalDeps())
420}
421
Colin Cross635c3b02016-05-18 15:37:25 -0700422func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800423 return &Module{
424 hod: hod,
425 multilib: multilib,
426 }
427}
428
Colin Cross635c3b02016-05-18 15:37:25 -0700429func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800430 module := newBaseModule(hod, multilib)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700431 module.features = []feature{
432 &tidyFeature{},
433 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700434 module.stl = &stl{}
Colin Cross16b23492016-01-06 14:41:07 -0800435 module.sanitize = &sanitize{}
Dan Willemsen581341d2017-02-09 16:16:31 -0800436 module.coverage = &coverage{}
Colin Crossca860ac2016-01-04 14:34:37 -0800437 return module
438}
439
Colin Crossce75d2c2016-10-06 16:12:58 -0700440func (c *Module) Prebuilt() *android.Prebuilt {
441 if p, ok := c.linker.(prebuiltLinkerInterface); ok {
442 return p.prebuilt()
443 }
444 return nil
445}
446
447func (c *Module) Name() string {
448 name := c.ModuleBase.Name()
449 if p, ok := c.linker.(prebuiltLinkerInterface); ok {
450 name = p.Name(name)
451 }
452 return name
453}
454
Colin Cross635c3b02016-05-18 15:37:25 -0700455func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800456 ctx := &moduleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700457 ModuleContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800458 moduleContextImpl: moduleContextImpl{
459 mod: c,
460 },
461 }
462 ctx.ctx = ctx
463
464 flags := Flags{
465 Toolchain: c.toolchain(ctx),
466 Clang: c.clang(ctx),
467 }
Colin Crossca860ac2016-01-04 14:34:37 -0800468 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700469 flags = c.compiler.compilerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800470 }
471 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700472 flags = c.linker.linkerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800473 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700474 if c.stl != nil {
475 flags = c.stl.flags(ctx, flags)
476 }
Colin Cross16b23492016-01-06 14:41:07 -0800477 if c.sanitize != nil {
478 flags = c.sanitize.flags(ctx, flags)
479 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800480 if c.coverage != nil {
481 flags = c.coverage.flags(ctx, flags)
482 }
Colin Crossca860ac2016-01-04 14:34:37 -0800483 for _, feature := range c.features {
484 flags = feature.flags(ctx, flags)
485 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800486 if ctx.Failed() {
487 return
488 }
489
Colin Crossb98c8b02016-07-29 13:44:28 -0700490 flags.CFlags, _ = filterList(flags.CFlags, config.IllegalFlags)
491 flags.CppFlags, _ = filterList(flags.CppFlags, config.IllegalFlags)
492 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, config.IllegalFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800493
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800494 deps := c.depsToPaths(ctx)
495 if ctx.Failed() {
496 return
497 }
498 flags.GlobalFlags = append(flags.GlobalFlags, deps.Flags...)
499 c.flags = flags
500
Colin Crossca860ac2016-01-04 14:34:37 -0800501 // Optimization to reduce size of build.ninja
502 // Replace the long list of flags for each file with a module-local variable
503 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
504 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
505 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
506 flags.CFlags = []string{"$cflags"}
507 flags.CppFlags = []string{"$cppflags"}
508 flags.AsFlags = []string{"$asflags"}
509
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700510 var objs Objects
Colin Crossca860ac2016-01-04 14:34:37 -0800511 if c.compiler != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700512 objs = c.compiler.compile(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -0800513 if ctx.Failed() {
514 return
515 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800516 }
517
Colin Crossca860ac2016-01-04 14:34:37 -0800518 if c.linker != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700519 outputFile := c.linker.link(ctx, flags, deps, objs)
Colin Crossca860ac2016-01-04 14:34:37 -0800520 if ctx.Failed() {
521 return
522 }
Colin Cross635c3b02016-05-18 15:37:25 -0700523 c.outputFile = android.OptionalPathForPath(outputFile)
Colin Crossce75d2c2016-10-06 16:12:58 -0700524 }
Colin Cross5049f022015-03-18 13:28:46 -0700525
Colin Crossce75d2c2016-10-06 16:12:58 -0700526 if c.installer != nil && !c.Properties.PreventInstall && c.outputFile.Valid() {
527 c.installer.install(ctx, c.outputFile.Path())
528 if ctx.Failed() {
529 return
Colin Crossca860ac2016-01-04 14:34:37 -0800530 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700531 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800532}
533
Colin Crossb98c8b02016-07-29 13:44:28 -0700534func (c *Module) toolchain(ctx BaseModuleContext) config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800535 if c.cachedToolchain == nil {
Colin Crossb98c8b02016-07-29 13:44:28 -0700536 c.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
Colin Cross3f40fa42015-01-30 17:27:36 -0800537 }
Colin Crossca860ac2016-01-04 14:34:37 -0800538 return c.cachedToolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800539}
540
Colin Crossca860ac2016-01-04 14:34:37 -0800541func (c *Module) begin(ctx BaseModuleContext) {
542 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700543 c.compiler.compilerInit(ctx)
Colin Cross21b9a242015-03-24 14:15:58 -0700544 }
Colin Crossca860ac2016-01-04 14:34:37 -0800545 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700546 c.linker.linkerInit(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800547 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700548 if c.stl != nil {
549 c.stl.begin(ctx)
550 }
Colin Cross16b23492016-01-06 14:41:07 -0800551 if c.sanitize != nil {
552 c.sanitize.begin(ctx)
553 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800554 if c.coverage != nil {
555 c.coverage.begin(ctx)
556 }
Colin Crossca860ac2016-01-04 14:34:37 -0800557 for _, feature := range c.features {
558 feature.begin(ctx)
559 }
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700560 if ctx.sdk() {
Dan Willemsend2ede872016-11-18 14:54:24 -0800561 if ctx.vndk() {
562 ctx.PropertyErrorf("use_vndk",
563 "sdk_version and use_vndk cannot be used at the same time")
564 }
565
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700566 version, err := normalizeNdkApiLevel(ctx.sdkVersion(), ctx.Arch())
567 if err != nil {
568 ctx.PropertyErrorf("sdk_version", err.Error())
569 }
Dan Albert90f7a4d2016-11-08 14:34:24 -0800570 c.Properties.Sdk_version = version
Dan Willemsend2ede872016-11-18 14:54:24 -0800571 } else if ctx.vndk() {
572 version, err := normalizeNdkApiLevel(ctx.DeviceConfig().VndkVersion(), ctx.Arch())
573 if err != nil {
574 ctx.ModuleErrorf("Bad BOARD_VNDK_VERSION: %s", err.Error())
575 }
576 c.Properties.Vndk_version = version
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700577 }
Colin Crossca860ac2016-01-04 14:34:37 -0800578}
579
Colin Cross37047f12016-12-13 17:06:13 -0800580func (c *Module) deps(ctx DepsContext) Deps {
Colin Crossc99deeb2016-04-11 15:06:20 -0700581 deps := Deps{}
582
583 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700584 deps = c.compiler.compilerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700585 }
586 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700587 deps = c.linker.linkerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700588 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700589 if c.stl != nil {
590 deps = c.stl.deps(ctx, deps)
591 }
Colin Cross16b23492016-01-06 14:41:07 -0800592 if c.sanitize != nil {
593 deps = c.sanitize.deps(ctx, deps)
594 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800595 if c.coverage != nil {
596 deps = c.coverage.deps(ctx, deps)
597 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700598 for _, feature := range c.features {
599 deps = feature.deps(ctx, deps)
600 }
601
602 deps.WholeStaticLibs = lastUniqueElements(deps.WholeStaticLibs)
603 deps.StaticLibs = lastUniqueElements(deps.StaticLibs)
604 deps.LateStaticLibs = lastUniqueElements(deps.LateStaticLibs)
605 deps.SharedLibs = lastUniqueElements(deps.SharedLibs)
606 deps.LateSharedLibs = lastUniqueElements(deps.LateSharedLibs)
Colin Cross5950f382016-12-13 12:50:57 -0800607 deps.HeaderLibs = lastUniqueElements(deps.HeaderLibs)
Colin Crossc99deeb2016-04-11 15:06:20 -0700608
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700609 for _, lib := range deps.ReexportSharedLibHeaders {
610 if !inList(lib, deps.SharedLibs) {
611 ctx.PropertyErrorf("export_shared_lib_headers", "Shared library not in shared_libs: '%s'", lib)
612 }
613 }
614
615 for _, lib := range deps.ReexportStaticLibHeaders {
616 if !inList(lib, deps.StaticLibs) {
617 ctx.PropertyErrorf("export_static_lib_headers", "Static library not in static_libs: '%s'", lib)
618 }
619 }
620
Colin Cross5950f382016-12-13 12:50:57 -0800621 for _, lib := range deps.ReexportHeaderLibHeaders {
622 if !inList(lib, deps.HeaderLibs) {
623 ctx.PropertyErrorf("export_header_lib_headers", "Header library not in header_libs: '%s'", lib)
624 }
625 }
626
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700627 for _, gen := range deps.ReexportGeneratedHeaders {
628 if !inList(gen, deps.GeneratedHeaders) {
629 ctx.PropertyErrorf("export_generated_headers", "Generated header module not in generated_headers: '%s'", gen)
630 }
631 }
632
Colin Crossc99deeb2016-04-11 15:06:20 -0700633 return deps
634}
635
Dan Albert7e9d2952016-08-04 13:02:36 -0700636func (c *Module) beginMutator(actx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800637 ctx := &baseModuleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700638 BaseContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800639 moduleContextImpl: moduleContextImpl{
640 mod: c,
641 },
642 }
643 ctx.ctx = ctx
644
Colin Crossca860ac2016-01-04 14:34:37 -0800645 c.begin(ctx)
Dan Albert7e9d2952016-08-04 13:02:36 -0700646}
647
Colin Cross1e676be2016-10-12 14:38:15 -0700648func (c *Module) DepsMutator(actx android.BottomUpMutatorContext) {
649 if !c.Enabled() {
650 return
651 }
652
Colin Cross37047f12016-12-13 17:06:13 -0800653 ctx := &depsContext{
654 BottomUpMutatorContext: actx,
Dan Albert7e9d2952016-08-04 13:02:36 -0700655 moduleContextImpl: moduleContextImpl{
656 mod: c,
657 },
658 }
659 ctx.ctx = ctx
Colin Crossca860ac2016-01-04 14:34:37 -0800660
Colin Crossc99deeb2016-04-11 15:06:20 -0700661 deps := c.deps(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800662
Colin Crossb5bc4b42016-07-11 16:11:59 -0700663 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.SharedLibs...)
664 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.LateSharedLibs...)
Dan Willemsen72d39932016-07-08 23:23:48 -0700665
Dan Albert914449f2016-06-17 16:45:24 -0700666 variantNdkLibs := []string{}
667 variantLateNdkLibs := []string{}
Dan Willemsend2ede872016-11-18 14:54:24 -0800668 if ctx.sdk() || ctx.vndk() {
Dan Albert914449f2016-06-17 16:45:24 -0700669 version := ctx.sdkVersion()
Dan Willemsen72d39932016-07-08 23:23:48 -0700670
Dan Albert914449f2016-06-17 16:45:24 -0700671 // Rewrites the names of shared libraries into the names of the NDK
672 // libraries where appropriate. This returns two slices.
673 //
674 // The first is a list of non-variant shared libraries (either rewritten
675 // NDK libraries to the modules in prebuilts/ndk, or not rewritten
676 // because they are not NDK libraries).
677 //
678 // The second is a list of ndk_library modules. These need to be
679 // separated because they are a variation dependency and must be added
680 // in a different manner.
681 rewriteNdkLibs := func(list []string) ([]string, []string) {
682 variantLibs := []string{}
683 nonvariantLibs := []string{}
684 for _, entry := range list {
Dan Willemsen72d39932016-07-08 23:23:48 -0700685 if inList(entry, ndkPrebuiltSharedLibraries) {
Dan Albert914449f2016-06-17 16:45:24 -0700686 if !inList(entry, ndkMigratedLibs) {
687 nonvariantLibs = append(nonvariantLibs, entry+".ndk."+version)
688 } else {
689 variantLibs = append(variantLibs, entry+ndkLibrarySuffix)
690 }
691 } else {
692 nonvariantLibs = append(variantLibs, entry)
Dan Willemsen72d39932016-07-08 23:23:48 -0700693 }
694 }
Dan Albert914449f2016-06-17 16:45:24 -0700695 return nonvariantLibs, variantLibs
Dan Willemsen72d39932016-07-08 23:23:48 -0700696 }
697
Dan Albert914449f2016-06-17 16:45:24 -0700698 deps.SharedLibs, variantNdkLibs = rewriteNdkLibs(deps.SharedLibs)
699 deps.LateSharedLibs, variantLateNdkLibs = rewriteNdkLibs(deps.LateSharedLibs)
Dan Willemsen72d39932016-07-08 23:23:48 -0700700 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700701
Colin Cross32ec36c2016-12-15 07:39:51 -0800702 for _, lib := range deps.HeaderLibs {
703 depTag := headerDepTag
704 if inList(lib, deps.ReexportHeaderLibHeaders) {
705 depTag = headerExportDepTag
706 }
707 actx.AddVariationDependencies(nil, depTag, lib)
708 }
Colin Cross5950f382016-12-13 12:50:57 -0800709
Colin Crossc99deeb2016-04-11 15:06:20 -0700710 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, wholeStaticDepTag,
711 deps.WholeStaticLibs...)
712
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700713 for _, lib := range deps.StaticLibs {
714 depTag := staticDepTag
715 if inList(lib, deps.ReexportStaticLibHeaders) {
716 depTag = staticExportDepTag
717 }
Colin Cross15a0d462016-07-14 14:49:58 -0700718 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700719 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700720
721 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, lateStaticDepTag,
722 deps.LateStaticLibs...)
723
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700724 for _, lib := range deps.SharedLibs {
725 depTag := sharedDepTag
726 if inList(lib, deps.ReexportSharedLibHeaders) {
727 depTag = sharedExportDepTag
728 }
Colin Cross15a0d462016-07-14 14:49:58 -0700729 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700730 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700731
732 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, lateSharedDepTag,
733 deps.LateSharedLibs...)
734
Colin Cross68861832016-07-08 10:41:41 -0700735 actx.AddDependency(c, genSourceDepTag, deps.GeneratedSources...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700736
737 for _, gen := range deps.GeneratedHeaders {
738 depTag := genHeaderDepTag
739 if inList(gen, deps.ReexportGeneratedHeaders) {
740 depTag = genHeaderExportDepTag
741 }
742 actx.AddDependency(c, depTag, gen)
743 }
Dan Willemsenb40aab62016-04-20 14:21:14 -0700744
Colin Cross68861832016-07-08 10:41:41 -0700745 actx.AddDependency(c, objDepTag, deps.ObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700746
747 if deps.CrtBegin != "" {
Colin Cross68861832016-07-08 10:41:41 -0700748 actx.AddDependency(c, crtBeginDepTag, deps.CrtBegin)
Colin Crossca860ac2016-01-04 14:34:37 -0800749 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700750 if deps.CrtEnd != "" {
Colin Cross68861832016-07-08 10:41:41 -0700751 actx.AddDependency(c, crtEndDepTag, deps.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700752 }
Dan Albert914449f2016-06-17 16:45:24 -0700753
754 version := ctx.sdkVersion()
755 actx.AddVariationDependencies([]blueprint.Variation{
756 {"ndk_api", version}, {"link", "shared"}}, ndkStubDepTag, variantNdkLibs...)
757 actx.AddVariationDependencies([]blueprint.Variation{
758 {"ndk_api", version}, {"link", "shared"}}, ndkLateStubDepTag, variantLateNdkLibs...)
Colin Cross6362e272015-10-29 15:25:03 -0700759}
Colin Cross21b9a242015-03-24 14:15:58 -0700760
Dan Albert7e9d2952016-08-04 13:02:36 -0700761func beginMutator(ctx android.BottomUpMutatorContext) {
762 if c, ok := ctx.Module().(*Module); ok && c.Enabled() {
763 c.beginMutator(ctx)
764 }
765}
766
Colin Crossca860ac2016-01-04 14:34:37 -0800767func (c *Module) clang(ctx BaseModuleContext) bool {
768 clang := Bool(c.Properties.Clang)
769
770 if c.Properties.Clang == nil {
771 if ctx.Host() {
772 clang = true
773 }
774
775 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
776 clang = true
777 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800778 }
Colin Cross28344522015-04-22 13:07:53 -0700779
Colin Crossca860ac2016-01-04 14:34:37 -0800780 if !c.toolchain(ctx).ClangSupported() {
781 clang = false
782 }
783
784 return clang
785}
786
Colin Crossc99deeb2016-04-11 15:06:20 -0700787// Convert dependencies to paths. Returns a PathDeps containing paths
Colin Cross635c3b02016-05-18 15:37:25 -0700788func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
Colin Crossca860ac2016-01-04 14:34:37 -0800789 var depPaths PathDeps
Colin Crossca860ac2016-01-04 14:34:37 -0800790
Dan Willemsena96ff642016-06-07 12:34:45 -0700791 // Whether a module can link to another module, taking into
792 // account NDK linking.
Dan Albert9e10cd42016-08-03 14:12:14 -0700793 checkLinkType := func(from, to *Module) {
Dan Willemsena96ff642016-06-07 12:34:45 -0700794 if from.Target().Os != android.Android {
795 // Host code is not restricted
Dan Albert9e10cd42016-08-03 14:12:14 -0700796 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700797 }
798 if from.Properties.Sdk_version == "" {
799 // Platform code can link to anything
Dan Albert9e10cd42016-08-03 14:12:14 -0700800 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700801 }
Colin Crossb916a382016-07-29 17:28:03 -0700802 if _, ok := to.linker.(*toolchainLibraryDecorator); ok {
Dan Willemsena96ff642016-06-07 12:34:45 -0700803 // These are always allowed
Dan Albert9e10cd42016-08-03 14:12:14 -0700804 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700805 }
806 if _, ok := to.linker.(*ndkPrebuiltLibraryLinker); ok {
807 // These are allowed, but don't set sdk_version
Dan Albert9e10cd42016-08-03 14:12:14 -0700808 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700809 }
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700810 if _, ok := to.linker.(*ndkPrebuiltStlLinker); ok {
811 // These are allowed, but don't set sdk_version
Dan Albert9e10cd42016-08-03 14:12:14 -0700812 return
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700813 }
Colin Crossb916a382016-07-29 17:28:03 -0700814 if _, ok := to.linker.(*stubDecorator); ok {
Dan Albert914449f2016-06-17 16:45:24 -0700815 // These aren't real libraries, but are the stub shared libraries that are included in
816 // the NDK.
Dan Albert9e10cd42016-08-03 14:12:14 -0700817 return
Dan Albert914449f2016-06-17 16:45:24 -0700818 }
Dan Albert9e10cd42016-08-03 14:12:14 -0700819 if to.Properties.Sdk_version == "" {
820 // NDK code linking to platform code is never okay.
821 ctx.ModuleErrorf("depends on non-NDK-built library %q",
822 ctx.OtherModuleName(to))
823 }
824
825 // All this point we know we have two NDK libraries, but we need to
826 // check that we're not linking against anything built against a higher
827 // API level, as it is only valid to link against older or equivalent
828 // APIs.
829
830 if from.Properties.Sdk_version == "current" {
831 // Current can link against anything.
832 return
833 } else if to.Properties.Sdk_version == "current" {
834 // Current can't be linked against by anything else.
835 ctx.ModuleErrorf("links %q built against newer API version %q",
836 ctx.OtherModuleName(to), "current")
837 }
838
839 fromApi, err := strconv.Atoi(from.Properties.Sdk_version)
840 if err != nil {
841 ctx.PropertyErrorf("sdk_version",
842 "Invalid sdk_version value (must be int): %q",
843 from.Properties.Sdk_version)
844 }
845 toApi, err := strconv.Atoi(to.Properties.Sdk_version)
846 if err != nil {
847 ctx.PropertyErrorf("sdk_version",
848 "Invalid sdk_version value (must be int): %q",
849 to.Properties.Sdk_version)
850 }
851
852 if toApi > fromApi {
853 ctx.ModuleErrorf("links %q built against newer API version %q",
854 ctx.OtherModuleName(to), to.Properties.Sdk_version)
855 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700856 }
857
Colin Crossc99deeb2016-04-11 15:06:20 -0700858 ctx.VisitDirectDeps(func(m blueprint.Module) {
859 name := ctx.OtherModuleName(m)
860 tag := ctx.OtherModuleDependencyTag(m)
Colin Crossca860ac2016-01-04 14:34:37 -0800861
Colin Cross635c3b02016-05-18 15:37:25 -0700862 a, _ := m.(android.Module)
Colin Crossc99deeb2016-04-11 15:06:20 -0700863 if a == nil {
864 ctx.ModuleErrorf("module %q not an android module", name)
865 return
Colin Crossca860ac2016-01-04 14:34:37 -0800866 }
Colin Crossca860ac2016-01-04 14:34:37 -0800867
Dan Willemsena96ff642016-06-07 12:34:45 -0700868 cc, _ := m.(*Module)
869 if cc == nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700870 switch tag {
Colin Cross068e0fe2016-12-13 15:23:47 -0800871 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700872 case genSourceDepTag:
873 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
874 depPaths.GeneratedSources = append(depPaths.GeneratedSources,
875 genRule.GeneratedSourceFiles()...)
876 } else {
877 ctx.ModuleErrorf("module %q is not a gensrcs or genrule", name)
878 }
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700879 case genHeaderDepTag, genHeaderExportDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700880 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
881 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders,
882 genRule.GeneratedSourceFiles()...)
Colin Cross5ed99c62016-11-22 12:55:55 -0800883 flags := includeDirsToFlags(genRule.GeneratedHeaderDirs())
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700884 depPaths.Flags = append(depPaths.Flags, flags)
885 if tag == genHeaderExportDepTag {
886 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700887 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps,
888 genRule.GeneratedSourceFiles()...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700889 }
Dan Willemsenb40aab62016-04-20 14:21:14 -0700890 } else {
891 ctx.ModuleErrorf("module %q is not a genrule", name)
892 }
893 default:
Colin Crossc99deeb2016-04-11 15:06:20 -0700894 ctx.ModuleErrorf("depends on non-cc module %q", name)
Colin Crossca860ac2016-01-04 14:34:37 -0800895 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700896 return
897 }
898
899 if !a.Enabled() {
Colin Crossa8f5e9a2016-12-13 12:51:11 -0800900 if ctx.AConfig().AllowMissingDependencies() {
901 ctx.AddMissingDependencies([]string{name})
902 } else {
903 ctx.ModuleErrorf("depends on disabled module %q", name)
904 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700905 return
906 }
907
Colin Crossa1ad8d12016-06-01 17:09:44 -0700908 if a.Target().Os != ctx.Os() {
909 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), name)
910 return
911 }
912
913 if a.Target().Arch.ArchType != ctx.Arch().ArchType {
914 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700915 return
916 }
917
Colin Crossc99deeb2016-04-11 15:06:20 -0700918 if tag == reuseObjTag {
Colin Crossbba99042016-11-23 15:45:05 -0800919 if l, ok := cc.compiler.(libraryInterface); ok {
920 depPaths.Objs = depPaths.Objs.Append(l.reuseObjs())
921 return
922 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700923 }
924
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700925 if t, ok := tag.(dependencyTag); ok && t.library {
Dan Willemsena96ff642016-06-07 12:34:45 -0700926 if i, ok := cc.linker.(exportedFlagsProducer); ok {
Dan Willemsen76f08272016-07-09 00:14:08 -0700927 flags := i.exportedFlags()
Dan Willemsen847dcc72016-09-29 12:13:36 -0700928 deps := i.exportedFlagsDeps()
Dan Willemsen76f08272016-07-09 00:14:08 -0700929 depPaths.Flags = append(depPaths.Flags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700930 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders, deps...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700931
932 if t.reexportFlags {
Dan Willemsen76f08272016-07-09 00:14:08 -0700933 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700934 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps, deps...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700935 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700936 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700937
Dan Albert9e10cd42016-08-03 14:12:14 -0700938 checkLinkType(c, cc)
Colin Crossc99deeb2016-04-11 15:06:20 -0700939 }
940
Colin Cross26c34ed2016-09-30 17:10:16 -0700941 var ptr *android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -0700942 var depPtr *android.Paths
Colin Crossc99deeb2016-04-11 15:06:20 -0700943
Colin Cross26c34ed2016-09-30 17:10:16 -0700944 linkFile := cc.outputFile
945 depFile := android.OptionalPath{}
946
Colin Crossc99deeb2016-04-11 15:06:20 -0700947 switch tag {
Dan Albert914449f2016-06-17 16:45:24 -0700948 case ndkStubDepTag, sharedDepTag, sharedExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700949 ptr = &depPaths.SharedLibs
950 depPtr = &depPaths.SharedLibsDeps
951 depFile = cc.linker.(libraryInterface).toc()
Dan Albert914449f2016-06-17 16:45:24 -0700952 case lateSharedDepTag, ndkLateStubDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700953 ptr = &depPaths.LateSharedLibs
954 depPtr = &depPaths.LateSharedLibsDeps
955 depFile = cc.linker.(libraryInterface).toc()
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700956 case staticDepTag, staticExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700957 ptr = &depPaths.StaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -0700958 case lateStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700959 ptr = &depPaths.LateStaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -0700960 case wholeStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700961 ptr = &depPaths.WholeStaticLibs
Colin Crossb916a382016-07-29 17:28:03 -0700962 staticLib, ok := cc.linker.(libraryInterface)
963 if !ok || !staticLib.static() {
Dan Willemsena96ff642016-06-07 12:34:45 -0700964 ctx.ModuleErrorf("module %q not a static library", name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700965 return
966 }
967
968 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
969 postfix := " (required by " + ctx.OtherModuleName(m) + ")"
970 for i := range missingDeps {
971 missingDeps[i] += postfix
972 }
973 ctx.AddMissingDependencies(missingDeps)
974 }
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700975 depPaths.WholeStaticLibObjs = depPaths.WholeStaticLibObjs.Append(staticLib.objs())
Colin Cross5950f382016-12-13 12:50:57 -0800976 case headerDepTag:
977 // Nothing
Colin Crossc99deeb2016-04-11 15:06:20 -0700978 case objDepTag:
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700979 depPaths.Objs.objFiles = append(depPaths.Objs.objFiles, linkFile.Path())
Colin Crossc99deeb2016-04-11 15:06:20 -0700980 case crtBeginDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700981 depPaths.CrtBegin = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -0700982 case crtEndDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700983 depPaths.CrtEnd = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -0700984 }
985
Dan Willemsen581341d2017-02-09 16:16:31 -0800986 switch tag {
987 case staticDepTag, staticExportDepTag, lateStaticDepTag:
988 staticLib, ok := cc.linker.(libraryInterface)
989 if !ok || !staticLib.static() {
990 ctx.ModuleErrorf("module %q not a static library", name)
991 return
992 }
993
994 // When combining coverage files for shared libraries and executables, coverage files
995 // in static libraries act as if they were whole static libraries.
996 depPaths.StaticLibObjs.coverageFiles = append(depPaths.StaticLibObjs.coverageFiles,
997 staticLib.objs().coverageFiles...)
998 }
999
Colin Cross26c34ed2016-09-30 17:10:16 -07001000 if ptr != nil {
Colin Crossce75d2c2016-10-06 16:12:58 -07001001 if !linkFile.Valid() {
1002 ctx.ModuleErrorf("module %q missing output file", name)
1003 return
1004 }
Colin Cross26c34ed2016-09-30 17:10:16 -07001005 *ptr = append(*ptr, linkFile.Path())
1006 }
1007
Colin Crossc99deeb2016-04-11 15:06:20 -07001008 if depPtr != nil {
Colin Cross26c34ed2016-09-30 17:10:16 -07001009 dep := depFile
1010 if !dep.Valid() {
1011 dep = linkFile
1012 }
1013 *depPtr = append(*depPtr, dep.Path())
Colin Crossca860ac2016-01-04 14:34:37 -08001014 }
1015 })
1016
1017 return depPaths
1018}
1019
1020func (c *Module) InstallInData() bool {
1021 if c.installer == nil {
1022 return false
1023 }
Colin Cross94610402016-08-29 13:41:32 -07001024 if c.sanitize != nil && c.sanitize.inData() {
1025 return true
1026 }
Colin Crossca860ac2016-01-04 14:34:37 -08001027 return c.installer.inData()
1028}
1029
Dan Willemsen4aa75ca2016-09-28 16:18:03 -07001030func (c *Module) HostToolPath() android.OptionalPath {
1031 if c.installer == nil {
1032 return android.OptionalPath{}
1033 }
1034 return c.installer.hostToolPath()
1035}
1036
Colin Cross2ba19d92015-05-07 15:44:20 -07001037//
Colin Crosscfad1192015-11-02 16:43:11 -08001038// Defaults
1039//
Colin Crossca860ac2016-01-04 14:34:37 -08001040type Defaults struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001041 android.ModuleBase
1042 android.DefaultsModule
Colin Crosscfad1192015-11-02 16:43:11 -08001043}
1044
Colin Cross635c3b02016-05-18 15:37:25 -07001045func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -08001046}
1047
Colin Cross1e676be2016-10-12 14:38:15 -07001048func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
1049}
1050
Colin Crossca860ac2016-01-04 14:34:37 -08001051func defaultsFactory() (blueprint.Module, []interface{}) {
Colin Crosse1d764e2016-08-18 14:18:32 -07001052 return DefaultsFactory()
1053}
1054
1055func DefaultsFactory(props ...interface{}) (blueprint.Module, []interface{}) {
Colin Crossca860ac2016-01-04 14:34:37 -08001056 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -08001057
Colin Crosse1d764e2016-08-18 14:18:32 -07001058 props = append(props,
Colin Crossca860ac2016-01-04 14:34:37 -08001059 &BaseProperties{},
1060 &BaseCompilerProperties{},
1061 &BaseLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -07001062 &LibraryProperties{},
Colin Cross919281a2016-04-05 16:42:05 -07001063 &FlagExporterProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001064 &BinaryLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -07001065 &TestProperties{},
1066 &TestBinaryProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001067 &UnusedProperties{},
1068 &StlProperties{},
Colin Cross16b23492016-01-06 14:41:07 -08001069 &SanitizeProperties{},
Colin Cross665dce92016-04-28 14:50:03 -07001070 &StripProperties{},
Dan Willemsen7424d612016-09-01 13:45:39 -07001071 &InstallerProperties{},
Dan Willemsena03cf6d2016-09-26 15:45:04 -07001072 &TidyProperties{},
Dan Willemsen581341d2017-02-09 16:16:31 -08001073 &CoverageProperties{},
Colin Crosse1d764e2016-08-18 14:18:32 -07001074 )
Colin Crosscfad1192015-11-02 16:43:11 -08001075
Colin Crosse1d764e2016-08-18 14:18:32 -07001076 return android.InitDefaultsModule(module, module, props...)
Colin Crosscfad1192015-11-02 16:43:11 -08001077}
1078
Colin Cross74d1ec02015-04-28 13:30:13 -07001079// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
1080// modifies the slice contents in place, and returns a subslice of the original slice
1081func lastUniqueElements(list []string) []string {
1082 totalSkip := 0
1083 for i := len(list) - 1; i >= totalSkip; i-- {
1084 skip := 0
1085 for j := i - 1; j >= totalSkip; j-- {
1086 if list[i] == list[j] {
1087 skip++
1088 } else {
1089 list[j+skip] = list[j]
1090 }
1091 }
1092 totalSkip += skip
1093 }
1094 return list[totalSkip:]
1095}
Colin Cross06a931b2015-10-28 17:23:31 -07001096
1097var Bool = proptools.Bool