blob: ae203a326b9e15f47e72e9cb141f6e135dba3a1d [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
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700215 inSanitizerDir() bool
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700216 hostToolPath() android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800217}
218
Colin Crossc99deeb2016-04-11 15:06:20 -0700219type dependencyTag struct {
220 blueprint.BaseDependencyTag
221 name string
222 library bool
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700223
224 reexportFlags bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700225}
226
227var (
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700228 sharedDepTag = dependencyTag{name: "shared", library: true}
229 sharedExportDepTag = dependencyTag{name: "shared", library: true, reexportFlags: true}
230 lateSharedDepTag = dependencyTag{name: "late shared", library: true}
231 staticDepTag = dependencyTag{name: "static", library: true}
232 staticExportDepTag = dependencyTag{name: "static", library: true, reexportFlags: true}
233 lateStaticDepTag = dependencyTag{name: "late static", library: true}
234 wholeStaticDepTag = dependencyTag{name: "whole static", library: true, reexportFlags: true}
Colin Cross32ec36c2016-12-15 07:39:51 -0800235 headerDepTag = dependencyTag{name: "header", library: true}
236 headerExportDepTag = dependencyTag{name: "header", library: true, reexportFlags: true}
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700237 genSourceDepTag = dependencyTag{name: "gen source"}
238 genHeaderDepTag = dependencyTag{name: "gen header"}
239 genHeaderExportDepTag = dependencyTag{name: "gen header", reexportFlags: true}
240 objDepTag = dependencyTag{name: "obj"}
241 crtBeginDepTag = dependencyTag{name: "crtbegin"}
242 crtEndDepTag = dependencyTag{name: "crtend"}
243 reuseObjTag = dependencyTag{name: "reuse objects"}
244 ndkStubDepTag = dependencyTag{name: "ndk stub", library: true}
245 ndkLateStubDepTag = dependencyTag{name: "ndk late stub", library: true}
Colin Crossc99deeb2016-04-11 15:06:20 -0700246)
247
Colin Crossca860ac2016-01-04 14:34:37 -0800248// Module contains the properties and members used by all C/C++ module types, and implements
249// the blueprint.Module interface. It delegates to compiler, linker, and installer interfaces
250// to construct the output file. Behavior can be customized with a Customizer interface
251type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700252 android.ModuleBase
253 android.DefaultableModule
Colin Crossc472d572015-03-17 15:06:21 -0700254
Colin Crossca860ac2016-01-04 14:34:37 -0800255 Properties BaseProperties
256 unused UnusedProperties
Colin Crossfa138792015-04-24 17:31:52 -0700257
Colin Crossca860ac2016-01-04 14:34:37 -0800258 // initialize before calling Init
Colin Cross635c3b02016-05-18 15:37:25 -0700259 hod android.HostOrDeviceSupported
260 multilib android.Multilib
Colin Crossc472d572015-03-17 15:06:21 -0700261
Colin Crossca860ac2016-01-04 14:34:37 -0800262 // delegates, initialize before calling Init
Colin Crossb4ce0ec2016-09-13 13:41:39 -0700263 features []feature
264 compiler compiler
265 linker linker
266 installer installer
267 stl *stl
268 sanitize *sanitize
Dan Willemsen581341d2017-02-09 16:16:31 -0800269 coverage *coverage
Colin Cross16b23492016-01-06 14:41:07 -0800270
271 androidMkSharedLibDeps []string
Colin Cross74d1ec02015-04-28 13:30:13 -0700272
Colin Cross635c3b02016-05-18 15:37:25 -0700273 outputFile android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800274
Colin Crossb98c8b02016-07-29 13:44:28 -0700275 cachedToolchain config.Toolchain
Colin Crossb916a382016-07-29 17:28:03 -0700276
277 subAndroidMkOnce map[subAndroidMkProvider]bool
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800278
279 // Flags used to compile this module
280 flags Flags
Colin Crossc472d572015-03-17 15:06:21 -0700281}
282
Colin Crossca860ac2016-01-04 14:34:37 -0800283func (c *Module) Init() (blueprint.Module, []interface{}) {
284 props := []interface{}{&c.Properties, &c.unused}
Colin Crossca860ac2016-01-04 14:34:37 -0800285 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700286 props = append(props, c.compiler.compilerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800287 }
288 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700289 props = append(props, c.linker.linkerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800290 }
291 if c.installer != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700292 props = append(props, c.installer.installerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800293 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700294 if c.stl != nil {
295 props = append(props, c.stl.props()...)
296 }
Colin Cross16b23492016-01-06 14:41:07 -0800297 if c.sanitize != nil {
298 props = append(props, c.sanitize.props()...)
299 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800300 if c.coverage != nil {
301 props = append(props, c.coverage.props()...)
302 }
Colin Crossca860ac2016-01-04 14:34:37 -0800303 for _, feature := range c.features {
304 props = append(props, feature.props()...)
305 }
Colin Crossc472d572015-03-17 15:06:21 -0700306
Colin Cross635c3b02016-05-18 15:37:25 -0700307 _, props = android.InitAndroidArchModule(c, c.hod, c.multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700308
Colin Cross635c3b02016-05-18 15:37:25 -0700309 return android.InitDefaultableModule(c, c, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700310}
311
Colin Crossb916a382016-07-29 17:28:03 -0700312// Returns true for dependency roots (binaries)
313// TODO(ccross): also handle dlopenable libraries
314func (c *Module) isDependencyRoot() bool {
315 if root, ok := c.linker.(interface {
316 isDependencyRoot() bool
317 }); ok {
318 return root.isDependencyRoot()
319 }
320 return false
321}
322
Colin Crossca860ac2016-01-04 14:34:37 -0800323type baseModuleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700324 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800325 moduleContextImpl
326}
327
Colin Cross37047f12016-12-13 17:06:13 -0800328type depsContext struct {
329 android.BottomUpMutatorContext
330 moduleContextImpl
331}
332
Colin Crossca860ac2016-01-04 14:34:37 -0800333type moduleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700334 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800335 moduleContextImpl
336}
337
338type moduleContextImpl struct {
339 mod *Module
340 ctx BaseModuleContext
341}
342
Colin Crossca860ac2016-01-04 14:34:37 -0800343func (ctx *moduleContextImpl) clang() bool {
344 return ctx.mod.clang(ctx.ctx)
345}
346
Colin Crossb98c8b02016-07-29 13:44:28 -0700347func (ctx *moduleContextImpl) toolchain() config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800348 return ctx.mod.toolchain(ctx.ctx)
349}
350
351func (ctx *moduleContextImpl) static() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700352 if static, ok := ctx.mod.linker.(interface {
353 static() bool
354 }); ok {
355 return static.static()
Colin Crossca860ac2016-01-04 14:34:37 -0800356 }
Colin Crossb916a382016-07-29 17:28:03 -0700357 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800358}
359
360func (ctx *moduleContextImpl) staticBinary() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700361 if static, ok := ctx.mod.linker.(interface {
362 staticBinary() bool
363 }); ok {
364 return static.staticBinary()
Colin Crossca860ac2016-01-04 14:34:37 -0800365 }
Colin Crossb916a382016-07-29 17:28:03 -0700366 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800367}
368
369func (ctx *moduleContextImpl) noDefaultCompilerFlags() bool {
370 return Bool(ctx.mod.Properties.No_default_compiler_flags)
371}
372
373func (ctx *moduleContextImpl) sdk() bool {
Dan Willemsena96ff642016-06-07 12:34:45 -0700374 if ctx.ctx.Device() {
375 return ctx.mod.Properties.Sdk_version != ""
376 }
377 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800378}
379
380func (ctx *moduleContextImpl) sdkVersion() string {
Dan Willemsena96ff642016-06-07 12:34:45 -0700381 if ctx.ctx.Device() {
Dan Willemsen11b26142017-03-19 18:30:37 -0700382 if ctx.vndk() {
383 return "current"
Dan Willemsend2ede872016-11-18 14:54:24 -0800384 } else {
385 return ctx.mod.Properties.Sdk_version
386 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700387 }
388 return ""
Colin Crossca860ac2016-01-04 14:34:37 -0800389}
390
Dan Willemsend2ede872016-11-18 14:54:24 -0800391func (ctx *moduleContextImpl) vndk() bool {
Dan Willemsenaa118f92017-04-06 12:49:58 -0700392 return ctx.ctx.Os() == android.Android && ctx.ctx.Vendor() && ctx.ctx.DeviceConfig().CompileVndk()
Dan Willemsend2ede872016-11-18 14:54:24 -0800393}
394
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700395func (ctx *moduleContextImpl) selectedStl() string {
396 if stl := ctx.mod.stl; stl != nil {
397 return stl.Properties.SelectedStl
398 }
399 return ""
400}
401
Colin Crossce75d2c2016-10-06 16:12:58 -0700402func (ctx *moduleContextImpl) baseModuleName() string {
403 return ctx.mod.ModuleBase.BaseModuleName()
404}
405
Colin Cross635c3b02016-05-18 15:37:25 -0700406func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800407 return &Module{
408 hod: hod,
409 multilib: multilib,
410 }
411}
412
Colin Cross635c3b02016-05-18 15:37:25 -0700413func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800414 module := newBaseModule(hod, multilib)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700415 module.features = []feature{
416 &tidyFeature{},
417 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700418 module.stl = &stl{}
Colin Cross16b23492016-01-06 14:41:07 -0800419 module.sanitize = &sanitize{}
Dan Willemsen581341d2017-02-09 16:16:31 -0800420 module.coverage = &coverage{}
Colin Crossca860ac2016-01-04 14:34:37 -0800421 return module
422}
423
Colin Crossce75d2c2016-10-06 16:12:58 -0700424func (c *Module) Prebuilt() *android.Prebuilt {
425 if p, ok := c.linker.(prebuiltLinkerInterface); ok {
426 return p.prebuilt()
427 }
428 return nil
429}
430
431func (c *Module) Name() string {
432 name := c.ModuleBase.Name()
Dan Willemsen01a90592017-04-07 15:21:13 -0700433 if p, ok := c.linker.(interface {
434 Name(string) string
435 }); ok {
Colin Crossce75d2c2016-10-06 16:12:58 -0700436 name = p.Name(name)
437 }
438 return name
439}
440
Colin Cross635c3b02016-05-18 15:37:25 -0700441func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800442 ctx := &moduleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700443 ModuleContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800444 moduleContextImpl: moduleContextImpl{
445 mod: c,
446 },
447 }
448 ctx.ctx = ctx
449
450 flags := Flags{
451 Toolchain: c.toolchain(ctx),
452 Clang: c.clang(ctx),
453 }
Colin Crossca860ac2016-01-04 14:34:37 -0800454 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700455 flags = c.compiler.compilerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800456 }
457 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700458 flags = c.linker.linkerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800459 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700460 if c.stl != nil {
461 flags = c.stl.flags(ctx, flags)
462 }
Colin Cross16b23492016-01-06 14:41:07 -0800463 if c.sanitize != nil {
464 flags = c.sanitize.flags(ctx, flags)
465 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800466 if c.coverage != nil {
467 flags = c.coverage.flags(ctx, flags)
468 }
Colin Crossca860ac2016-01-04 14:34:37 -0800469 for _, feature := range c.features {
470 flags = feature.flags(ctx, flags)
471 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800472 if ctx.Failed() {
473 return
474 }
475
Colin Crossb98c8b02016-07-29 13:44:28 -0700476 flags.CFlags, _ = filterList(flags.CFlags, config.IllegalFlags)
477 flags.CppFlags, _ = filterList(flags.CppFlags, config.IllegalFlags)
478 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, config.IllegalFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800479
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800480 deps := c.depsToPaths(ctx)
481 if ctx.Failed() {
482 return
483 }
484 flags.GlobalFlags = append(flags.GlobalFlags, deps.Flags...)
485 c.flags = flags
486
Colin Crossca860ac2016-01-04 14:34:37 -0800487 // Optimization to reduce size of build.ninja
488 // Replace the long list of flags for each file with a module-local variable
489 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
490 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
491 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
492 flags.CFlags = []string{"$cflags"}
493 flags.CppFlags = []string{"$cppflags"}
494 flags.AsFlags = []string{"$asflags"}
495
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700496 var objs Objects
Colin Crossca860ac2016-01-04 14:34:37 -0800497 if c.compiler != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700498 objs = c.compiler.compile(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -0800499 if ctx.Failed() {
500 return
501 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800502 }
503
Colin Crossca860ac2016-01-04 14:34:37 -0800504 if c.linker != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700505 outputFile := c.linker.link(ctx, flags, deps, objs)
Colin Crossca860ac2016-01-04 14:34:37 -0800506 if ctx.Failed() {
507 return
508 }
Colin Cross635c3b02016-05-18 15:37:25 -0700509 c.outputFile = android.OptionalPathForPath(outputFile)
Colin Crossce75d2c2016-10-06 16:12:58 -0700510 }
Colin Cross5049f022015-03-18 13:28:46 -0700511
Colin Crossce75d2c2016-10-06 16:12:58 -0700512 if c.installer != nil && !c.Properties.PreventInstall && c.outputFile.Valid() {
513 c.installer.install(ctx, c.outputFile.Path())
514 if ctx.Failed() {
515 return
Colin Crossca860ac2016-01-04 14:34:37 -0800516 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700517 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800518}
519
Colin Crossb98c8b02016-07-29 13:44:28 -0700520func (c *Module) toolchain(ctx BaseModuleContext) config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800521 if c.cachedToolchain == nil {
Colin Crossb98c8b02016-07-29 13:44:28 -0700522 c.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
Colin Cross3f40fa42015-01-30 17:27:36 -0800523 }
Colin Crossca860ac2016-01-04 14:34:37 -0800524 return c.cachedToolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800525}
526
Colin Crossca860ac2016-01-04 14:34:37 -0800527func (c *Module) begin(ctx BaseModuleContext) {
528 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700529 c.compiler.compilerInit(ctx)
Colin Cross21b9a242015-03-24 14:15:58 -0700530 }
Colin Crossca860ac2016-01-04 14:34:37 -0800531 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700532 c.linker.linkerInit(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800533 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700534 if c.stl != nil {
535 c.stl.begin(ctx)
536 }
Colin Cross16b23492016-01-06 14:41:07 -0800537 if c.sanitize != nil {
538 c.sanitize.begin(ctx)
539 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800540 if c.coverage != nil {
541 c.coverage.begin(ctx)
542 }
Colin Crossca860ac2016-01-04 14:34:37 -0800543 for _, feature := range c.features {
544 feature.begin(ctx)
545 }
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700546 if ctx.sdk() {
547 version, err := normalizeNdkApiLevel(ctx.sdkVersion(), ctx.Arch())
548 if err != nil {
549 ctx.PropertyErrorf("sdk_version", err.Error())
550 }
Dan Albert90f7a4d2016-11-08 14:34:24 -0800551 c.Properties.Sdk_version = version
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700552 }
Colin Crossca860ac2016-01-04 14:34:37 -0800553}
554
Colin Cross37047f12016-12-13 17:06:13 -0800555func (c *Module) deps(ctx DepsContext) Deps {
Colin Crossc99deeb2016-04-11 15:06:20 -0700556 deps := Deps{}
557
558 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700559 deps = c.compiler.compilerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700560 }
561 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700562 deps = c.linker.linkerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700563 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700564 if c.stl != nil {
565 deps = c.stl.deps(ctx, deps)
566 }
Colin Cross16b23492016-01-06 14:41:07 -0800567 if c.sanitize != nil {
568 deps = c.sanitize.deps(ctx, deps)
569 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800570 if c.coverage != nil {
571 deps = c.coverage.deps(ctx, deps)
572 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700573 for _, feature := range c.features {
574 deps = feature.deps(ctx, deps)
575 }
576
577 deps.WholeStaticLibs = lastUniqueElements(deps.WholeStaticLibs)
578 deps.StaticLibs = lastUniqueElements(deps.StaticLibs)
579 deps.LateStaticLibs = lastUniqueElements(deps.LateStaticLibs)
580 deps.SharedLibs = lastUniqueElements(deps.SharedLibs)
581 deps.LateSharedLibs = lastUniqueElements(deps.LateSharedLibs)
Colin Cross5950f382016-12-13 12:50:57 -0800582 deps.HeaderLibs = lastUniqueElements(deps.HeaderLibs)
Colin Crossc99deeb2016-04-11 15:06:20 -0700583
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700584 for _, lib := range deps.ReexportSharedLibHeaders {
585 if !inList(lib, deps.SharedLibs) {
586 ctx.PropertyErrorf("export_shared_lib_headers", "Shared library not in shared_libs: '%s'", lib)
587 }
588 }
589
590 for _, lib := range deps.ReexportStaticLibHeaders {
591 if !inList(lib, deps.StaticLibs) {
592 ctx.PropertyErrorf("export_static_lib_headers", "Static library not in static_libs: '%s'", lib)
593 }
594 }
595
Colin Cross5950f382016-12-13 12:50:57 -0800596 for _, lib := range deps.ReexportHeaderLibHeaders {
597 if !inList(lib, deps.HeaderLibs) {
598 ctx.PropertyErrorf("export_header_lib_headers", "Header library not in header_libs: '%s'", lib)
599 }
600 }
601
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700602 for _, gen := range deps.ReexportGeneratedHeaders {
603 if !inList(gen, deps.GeneratedHeaders) {
604 ctx.PropertyErrorf("export_generated_headers", "Generated header module not in generated_headers: '%s'", gen)
605 }
606 }
607
Colin Crossc99deeb2016-04-11 15:06:20 -0700608 return deps
609}
610
Dan Albert7e9d2952016-08-04 13:02:36 -0700611func (c *Module) beginMutator(actx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800612 ctx := &baseModuleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700613 BaseContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800614 moduleContextImpl: moduleContextImpl{
615 mod: c,
616 },
617 }
618 ctx.ctx = ctx
619
Colin Crossca860ac2016-01-04 14:34:37 -0800620 c.begin(ctx)
Dan Albert7e9d2952016-08-04 13:02:36 -0700621}
622
Colin Cross1e676be2016-10-12 14:38:15 -0700623func (c *Module) DepsMutator(actx android.BottomUpMutatorContext) {
624 if !c.Enabled() {
625 return
626 }
627
Colin Cross37047f12016-12-13 17:06:13 -0800628 ctx := &depsContext{
629 BottomUpMutatorContext: actx,
Dan Albert7e9d2952016-08-04 13:02:36 -0700630 moduleContextImpl: moduleContextImpl{
631 mod: c,
632 },
633 }
634 ctx.ctx = ctx
Colin Crossca860ac2016-01-04 14:34:37 -0800635
Colin Crossc99deeb2016-04-11 15:06:20 -0700636 deps := c.deps(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800637
Colin Crossb5bc4b42016-07-11 16:11:59 -0700638 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.SharedLibs...)
639 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.LateSharedLibs...)
Dan Willemsen72d39932016-07-08 23:23:48 -0700640
Dan Albert914449f2016-06-17 16:45:24 -0700641 variantNdkLibs := []string{}
642 variantLateNdkLibs := []string{}
Dan Willemsenb916b802017-03-19 13:44:32 -0700643 if ctx.Os() == android.Android {
Dan Albert914449f2016-06-17 16:45:24 -0700644 version := ctx.sdkVersion()
Dan Willemsen72d39932016-07-08 23:23:48 -0700645
Dan Albert914449f2016-06-17 16:45:24 -0700646 // Rewrites the names of shared libraries into the names of the NDK
647 // libraries where appropriate. This returns two slices.
648 //
649 // The first is a list of non-variant shared libraries (either rewritten
650 // NDK libraries to the modules in prebuilts/ndk, or not rewritten
651 // because they are not NDK libraries).
652 //
653 // The second is a list of ndk_library modules. These need to be
654 // separated because they are a variation dependency and must be added
655 // in a different manner.
656 rewriteNdkLibs := func(list []string) ([]string, []string) {
657 variantLibs := []string{}
658 nonvariantLibs := []string{}
659 for _, entry := range list {
Dan Willemsenb916b802017-03-19 13:44:32 -0700660 if ctx.sdk() && inList(entry, ndkPrebuiltSharedLibraries) {
Dan Albert914449f2016-06-17 16:45:24 -0700661 if !inList(entry, ndkMigratedLibs) {
662 nonvariantLibs = append(nonvariantLibs, entry+".ndk."+version)
663 } else {
664 variantLibs = append(variantLibs, entry+ndkLibrarySuffix)
665 }
Dan Willemsenb916b802017-03-19 13:44:32 -0700666 } else if ctx.vndk() && inList(entry, config.LLndkLibraries()) {
667 nonvariantLibs = append(nonvariantLibs, entry+llndkLibrarySuffix)
Dan Albert914449f2016-06-17 16:45:24 -0700668 } else {
Dan Willemsen7cbf5f82017-03-28 00:08:30 -0700669 nonvariantLibs = append(nonvariantLibs, entry)
Dan Willemsen72d39932016-07-08 23:23:48 -0700670 }
671 }
Dan Albert914449f2016-06-17 16:45:24 -0700672 return nonvariantLibs, variantLibs
Dan Willemsen72d39932016-07-08 23:23:48 -0700673 }
674
Dan Albert914449f2016-06-17 16:45:24 -0700675 deps.SharedLibs, variantNdkLibs = rewriteNdkLibs(deps.SharedLibs)
676 deps.LateSharedLibs, variantLateNdkLibs = rewriteNdkLibs(deps.LateSharedLibs)
Dan Willemsen72d39932016-07-08 23:23:48 -0700677 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700678
Colin Cross32ec36c2016-12-15 07:39:51 -0800679 for _, lib := range deps.HeaderLibs {
680 depTag := headerDepTag
681 if inList(lib, deps.ReexportHeaderLibHeaders) {
682 depTag = headerExportDepTag
683 }
684 actx.AddVariationDependencies(nil, depTag, lib)
685 }
Colin Cross5950f382016-12-13 12:50:57 -0800686
Colin Crossc99deeb2016-04-11 15:06:20 -0700687 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, wholeStaticDepTag,
688 deps.WholeStaticLibs...)
689
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700690 for _, lib := range deps.StaticLibs {
691 depTag := staticDepTag
692 if inList(lib, deps.ReexportStaticLibHeaders) {
693 depTag = staticExportDepTag
694 }
Colin Cross15a0d462016-07-14 14:49:58 -0700695 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700696 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700697
698 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, lateStaticDepTag,
699 deps.LateStaticLibs...)
700
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700701 for _, lib := range deps.SharedLibs {
702 depTag := sharedDepTag
703 if inList(lib, deps.ReexportSharedLibHeaders) {
704 depTag = sharedExportDepTag
705 }
Colin Cross15a0d462016-07-14 14:49:58 -0700706 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700707 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700708
709 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, lateSharedDepTag,
710 deps.LateSharedLibs...)
711
Colin Cross68861832016-07-08 10:41:41 -0700712 actx.AddDependency(c, genSourceDepTag, deps.GeneratedSources...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700713
714 for _, gen := range deps.GeneratedHeaders {
715 depTag := genHeaderDepTag
716 if inList(gen, deps.ReexportGeneratedHeaders) {
717 depTag = genHeaderExportDepTag
718 }
719 actx.AddDependency(c, depTag, gen)
720 }
Dan Willemsenb40aab62016-04-20 14:21:14 -0700721
Colin Cross68861832016-07-08 10:41:41 -0700722 actx.AddDependency(c, objDepTag, deps.ObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700723
724 if deps.CrtBegin != "" {
Colin Cross68861832016-07-08 10:41:41 -0700725 actx.AddDependency(c, crtBeginDepTag, deps.CrtBegin)
Colin Crossca860ac2016-01-04 14:34:37 -0800726 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700727 if deps.CrtEnd != "" {
Colin Cross68861832016-07-08 10:41:41 -0700728 actx.AddDependency(c, crtEndDepTag, deps.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700729 }
Dan Albert914449f2016-06-17 16:45:24 -0700730
731 version := ctx.sdkVersion()
732 actx.AddVariationDependencies([]blueprint.Variation{
733 {"ndk_api", version}, {"link", "shared"}}, ndkStubDepTag, variantNdkLibs...)
734 actx.AddVariationDependencies([]blueprint.Variation{
735 {"ndk_api", version}, {"link", "shared"}}, ndkLateStubDepTag, variantLateNdkLibs...)
Colin Cross6362e272015-10-29 15:25:03 -0700736}
Colin Cross21b9a242015-03-24 14:15:58 -0700737
Dan Albert7e9d2952016-08-04 13:02:36 -0700738func beginMutator(ctx android.BottomUpMutatorContext) {
739 if c, ok := ctx.Module().(*Module); ok && c.Enabled() {
740 c.beginMutator(ctx)
741 }
742}
743
Colin Crossca860ac2016-01-04 14:34:37 -0800744func (c *Module) clang(ctx BaseModuleContext) bool {
745 clang := Bool(c.Properties.Clang)
746
747 if c.Properties.Clang == nil {
748 if ctx.Host() {
749 clang = true
750 }
751
752 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
753 clang = true
754 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800755 }
Colin Cross28344522015-04-22 13:07:53 -0700756
Colin Crossca860ac2016-01-04 14:34:37 -0800757 if !c.toolchain(ctx).ClangSupported() {
758 clang = false
759 }
760
761 return clang
762}
763
Colin Crossc99deeb2016-04-11 15:06:20 -0700764// Convert dependencies to paths. Returns a PathDeps containing paths
Colin Cross635c3b02016-05-18 15:37:25 -0700765func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
Colin Crossca860ac2016-01-04 14:34:37 -0800766 var depPaths PathDeps
Colin Crossca860ac2016-01-04 14:34:37 -0800767
Dan Willemsena96ff642016-06-07 12:34:45 -0700768 // Whether a module can link to another module, taking into
769 // account NDK linking.
Dan Albert9e10cd42016-08-03 14:12:14 -0700770 checkLinkType := func(from, to *Module) {
Dan Willemsena96ff642016-06-07 12:34:45 -0700771 if from.Target().Os != android.Android {
772 // Host code is not restricted
Dan Albert9e10cd42016-08-03 14:12:14 -0700773 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700774 }
775 if from.Properties.Sdk_version == "" {
776 // Platform code can link to anything
Dan Albert9e10cd42016-08-03 14:12:14 -0700777 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700778 }
Colin Crossb916a382016-07-29 17:28:03 -0700779 if _, ok := to.linker.(*toolchainLibraryDecorator); ok {
Dan Willemsena96ff642016-06-07 12:34:45 -0700780 // These are always allowed
Dan Albert9e10cd42016-08-03 14:12:14 -0700781 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700782 }
783 if _, ok := to.linker.(*ndkPrebuiltLibraryLinker); ok {
784 // These are allowed, but don't set sdk_version
Dan Albert9e10cd42016-08-03 14:12:14 -0700785 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700786 }
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700787 if _, ok := to.linker.(*ndkPrebuiltStlLinker); ok {
788 // These are allowed, but don't set sdk_version
Dan Albert9e10cd42016-08-03 14:12:14 -0700789 return
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700790 }
Colin Crossb916a382016-07-29 17:28:03 -0700791 if _, ok := to.linker.(*stubDecorator); ok {
Dan Albert914449f2016-06-17 16:45:24 -0700792 // These aren't real libraries, but are the stub shared libraries that are included in
793 // the NDK.
Dan Albert9e10cd42016-08-03 14:12:14 -0700794 return
Dan Albert914449f2016-06-17 16:45:24 -0700795 }
Dan Albert9e10cd42016-08-03 14:12:14 -0700796 if to.Properties.Sdk_version == "" {
797 // NDK code linking to platform code is never okay.
798 ctx.ModuleErrorf("depends on non-NDK-built library %q",
799 ctx.OtherModuleName(to))
800 }
801
802 // All this point we know we have two NDK libraries, but we need to
803 // check that we're not linking against anything built against a higher
804 // API level, as it is only valid to link against older or equivalent
805 // APIs.
806
807 if from.Properties.Sdk_version == "current" {
808 // Current can link against anything.
809 return
810 } else if to.Properties.Sdk_version == "current" {
811 // Current can't be linked against by anything else.
812 ctx.ModuleErrorf("links %q built against newer API version %q",
813 ctx.OtherModuleName(to), "current")
814 }
815
816 fromApi, err := strconv.Atoi(from.Properties.Sdk_version)
817 if err != nil {
818 ctx.PropertyErrorf("sdk_version",
819 "Invalid sdk_version value (must be int): %q",
820 from.Properties.Sdk_version)
821 }
822 toApi, err := strconv.Atoi(to.Properties.Sdk_version)
823 if err != nil {
824 ctx.PropertyErrorf("sdk_version",
825 "Invalid sdk_version value (must be int): %q",
826 to.Properties.Sdk_version)
827 }
828
829 if toApi > fromApi {
830 ctx.ModuleErrorf("links %q built against newer API version %q",
831 ctx.OtherModuleName(to), to.Properties.Sdk_version)
832 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700833 }
834
Colin Crossc99deeb2016-04-11 15:06:20 -0700835 ctx.VisitDirectDeps(func(m blueprint.Module) {
836 name := ctx.OtherModuleName(m)
837 tag := ctx.OtherModuleDependencyTag(m)
Colin Crossca860ac2016-01-04 14:34:37 -0800838
Colin Cross635c3b02016-05-18 15:37:25 -0700839 a, _ := m.(android.Module)
Colin Crossc99deeb2016-04-11 15:06:20 -0700840 if a == nil {
841 ctx.ModuleErrorf("module %q not an android module", name)
842 return
Colin Crossca860ac2016-01-04 14:34:37 -0800843 }
Colin Crossca860ac2016-01-04 14:34:37 -0800844
Dan Willemsena96ff642016-06-07 12:34:45 -0700845 cc, _ := m.(*Module)
846 if cc == nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700847 switch tag {
Colin Cross068e0fe2016-12-13 15:23:47 -0800848 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700849 case genSourceDepTag:
850 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
851 depPaths.GeneratedSources = append(depPaths.GeneratedSources,
852 genRule.GeneratedSourceFiles()...)
853 } else {
854 ctx.ModuleErrorf("module %q is not a gensrcs or genrule", name)
855 }
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700856 case genHeaderDepTag, genHeaderExportDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700857 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
858 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders,
859 genRule.GeneratedSourceFiles()...)
Colin Cross5ed99c62016-11-22 12:55:55 -0800860 flags := includeDirsToFlags(genRule.GeneratedHeaderDirs())
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700861 depPaths.Flags = append(depPaths.Flags, flags)
862 if tag == genHeaderExportDepTag {
863 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700864 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps,
865 genRule.GeneratedSourceFiles()...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700866 }
Dan Willemsenb40aab62016-04-20 14:21:14 -0700867 } else {
868 ctx.ModuleErrorf("module %q is not a genrule", name)
869 }
870 default:
Colin Crossc99deeb2016-04-11 15:06:20 -0700871 ctx.ModuleErrorf("depends on non-cc module %q", name)
Colin Crossca860ac2016-01-04 14:34:37 -0800872 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700873 return
874 }
875
876 if !a.Enabled() {
Colin Crossa8f5e9a2016-12-13 12:51:11 -0800877 if ctx.AConfig().AllowMissingDependencies() {
878 ctx.AddMissingDependencies([]string{name})
879 } else {
880 ctx.ModuleErrorf("depends on disabled module %q", name)
881 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700882 return
883 }
884
Colin Crossa1ad8d12016-06-01 17:09:44 -0700885 if a.Target().Os != ctx.Os() {
886 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), name)
887 return
888 }
889
890 if a.Target().Arch.ArchType != ctx.Arch().ArchType {
891 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700892 return
893 }
894
Colin Crossc99deeb2016-04-11 15:06:20 -0700895 if tag == reuseObjTag {
Colin Crossbba99042016-11-23 15:45:05 -0800896 if l, ok := cc.compiler.(libraryInterface); ok {
897 depPaths.Objs = depPaths.Objs.Append(l.reuseObjs())
898 return
899 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700900 }
901
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700902 if t, ok := tag.(dependencyTag); ok && t.library {
Dan Willemsena96ff642016-06-07 12:34:45 -0700903 if i, ok := cc.linker.(exportedFlagsProducer); ok {
Dan Willemsen76f08272016-07-09 00:14:08 -0700904 flags := i.exportedFlags()
Dan Willemsen847dcc72016-09-29 12:13:36 -0700905 deps := i.exportedFlagsDeps()
Dan Willemsen76f08272016-07-09 00:14:08 -0700906 depPaths.Flags = append(depPaths.Flags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700907 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders, deps...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700908
909 if t.reexportFlags {
Dan Willemsen76f08272016-07-09 00:14:08 -0700910 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700911 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps, deps...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700912 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700913 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700914
Dan Albert9e10cd42016-08-03 14:12:14 -0700915 checkLinkType(c, cc)
Colin Crossc99deeb2016-04-11 15:06:20 -0700916 }
917
Colin Cross26c34ed2016-09-30 17:10:16 -0700918 var ptr *android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -0700919 var depPtr *android.Paths
Colin Crossc99deeb2016-04-11 15:06:20 -0700920
Colin Cross26c34ed2016-09-30 17:10:16 -0700921 linkFile := cc.outputFile
922 depFile := android.OptionalPath{}
923
Colin Crossc99deeb2016-04-11 15:06:20 -0700924 switch tag {
Dan Albert914449f2016-06-17 16:45:24 -0700925 case ndkStubDepTag, sharedDepTag, sharedExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700926 ptr = &depPaths.SharedLibs
927 depPtr = &depPaths.SharedLibsDeps
928 depFile = cc.linker.(libraryInterface).toc()
Dan Albert914449f2016-06-17 16:45:24 -0700929 case lateSharedDepTag, ndkLateStubDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700930 ptr = &depPaths.LateSharedLibs
931 depPtr = &depPaths.LateSharedLibsDeps
932 depFile = cc.linker.(libraryInterface).toc()
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700933 case staticDepTag, staticExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700934 ptr = &depPaths.StaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -0700935 case lateStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700936 ptr = &depPaths.LateStaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -0700937 case wholeStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700938 ptr = &depPaths.WholeStaticLibs
Colin Crossb916a382016-07-29 17:28:03 -0700939 staticLib, ok := cc.linker.(libraryInterface)
940 if !ok || !staticLib.static() {
Dan Willemsena96ff642016-06-07 12:34:45 -0700941 ctx.ModuleErrorf("module %q not a static library", name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700942 return
943 }
944
945 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
946 postfix := " (required by " + ctx.OtherModuleName(m) + ")"
947 for i := range missingDeps {
948 missingDeps[i] += postfix
949 }
950 ctx.AddMissingDependencies(missingDeps)
951 }
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700952 depPaths.WholeStaticLibObjs = depPaths.WholeStaticLibObjs.Append(staticLib.objs())
Colin Cross5950f382016-12-13 12:50:57 -0800953 case headerDepTag:
954 // Nothing
Colin Crossc99deeb2016-04-11 15:06:20 -0700955 case objDepTag:
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700956 depPaths.Objs.objFiles = append(depPaths.Objs.objFiles, linkFile.Path())
Colin Crossc99deeb2016-04-11 15:06:20 -0700957 case crtBeginDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700958 depPaths.CrtBegin = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -0700959 case crtEndDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700960 depPaths.CrtEnd = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -0700961 }
962
Dan Willemsen581341d2017-02-09 16:16:31 -0800963 switch tag {
964 case staticDepTag, staticExportDepTag, lateStaticDepTag:
965 staticLib, ok := cc.linker.(libraryInterface)
966 if !ok || !staticLib.static() {
967 ctx.ModuleErrorf("module %q not a static library", name)
968 return
969 }
970
971 // When combining coverage files for shared libraries and executables, coverage files
972 // in static libraries act as if they were whole static libraries.
973 depPaths.StaticLibObjs.coverageFiles = append(depPaths.StaticLibObjs.coverageFiles,
974 staticLib.objs().coverageFiles...)
975 }
976
Colin Cross26c34ed2016-09-30 17:10:16 -0700977 if ptr != nil {
Colin Crossce75d2c2016-10-06 16:12:58 -0700978 if !linkFile.Valid() {
979 ctx.ModuleErrorf("module %q missing output file", name)
980 return
981 }
Colin Cross26c34ed2016-09-30 17:10:16 -0700982 *ptr = append(*ptr, linkFile.Path())
983 }
984
Colin Crossc99deeb2016-04-11 15:06:20 -0700985 if depPtr != nil {
Colin Cross26c34ed2016-09-30 17:10:16 -0700986 dep := depFile
987 if !dep.Valid() {
988 dep = linkFile
989 }
990 *depPtr = append(*depPtr, dep.Path())
Colin Crossca860ac2016-01-04 14:34:37 -0800991 }
992 })
993
994 return depPaths
995}
996
997func (c *Module) InstallInData() bool {
998 if c.installer == nil {
999 return false
1000 }
Vishwath Mohan1dd88392017-03-29 22:00:18 -07001001 return c.installer.inData()
1002}
1003
1004func (c *Module) InstallInSanitizerDir() bool {
1005 if c.installer == nil {
1006 return false
1007 }
1008 if c.sanitize != nil && c.sanitize.inSanitizerDir() {
Colin Cross94610402016-08-29 13:41:32 -07001009 return true
1010 }
Vishwath Mohan1dd88392017-03-29 22:00:18 -07001011 return c.installer.inSanitizerDir()
Colin Crossca860ac2016-01-04 14:34:37 -08001012}
1013
Dan Willemsen4aa75ca2016-09-28 16:18:03 -07001014func (c *Module) HostToolPath() android.OptionalPath {
1015 if c.installer == nil {
1016 return android.OptionalPath{}
1017 }
1018 return c.installer.hostToolPath()
1019}
1020
Colin Cross2ba19d92015-05-07 15:44:20 -07001021//
Colin Crosscfad1192015-11-02 16:43:11 -08001022// Defaults
1023//
Colin Crossca860ac2016-01-04 14:34:37 -08001024type Defaults struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001025 android.ModuleBase
1026 android.DefaultsModule
Colin Crosscfad1192015-11-02 16:43:11 -08001027}
1028
Colin Cross635c3b02016-05-18 15:37:25 -07001029func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -08001030}
1031
Colin Cross1e676be2016-10-12 14:38:15 -07001032func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
1033}
1034
Colin Crossca860ac2016-01-04 14:34:37 -08001035func defaultsFactory() (blueprint.Module, []interface{}) {
Colin Crosse1d764e2016-08-18 14:18:32 -07001036 return DefaultsFactory()
1037}
1038
1039func DefaultsFactory(props ...interface{}) (blueprint.Module, []interface{}) {
Colin Crossca860ac2016-01-04 14:34:37 -08001040 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -08001041
Colin Crosse1d764e2016-08-18 14:18:32 -07001042 props = append(props,
Colin Crossca860ac2016-01-04 14:34:37 -08001043 &BaseProperties{},
1044 &BaseCompilerProperties{},
1045 &BaseLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -07001046 &LibraryProperties{},
Colin Cross919281a2016-04-05 16:42:05 -07001047 &FlagExporterProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001048 &BinaryLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -07001049 &TestProperties{},
1050 &TestBinaryProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001051 &UnusedProperties{},
1052 &StlProperties{},
Colin Cross16b23492016-01-06 14:41:07 -08001053 &SanitizeProperties{},
Colin Cross665dce92016-04-28 14:50:03 -07001054 &StripProperties{},
Dan Willemsen7424d612016-09-01 13:45:39 -07001055 &InstallerProperties{},
Dan Willemsena03cf6d2016-09-26 15:45:04 -07001056 &TidyProperties{},
Dan Willemsen581341d2017-02-09 16:16:31 -08001057 &CoverageProperties{},
Colin Crosse1d764e2016-08-18 14:18:32 -07001058 )
Colin Crosscfad1192015-11-02 16:43:11 -08001059
Colin Crosse1d764e2016-08-18 14:18:32 -07001060 return android.InitDefaultsModule(module, module, props...)
Colin Crosscfad1192015-11-02 16:43:11 -08001061}
1062
Colin Cross74d1ec02015-04-28 13:30:13 -07001063// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
1064// modifies the slice contents in place, and returns a subslice of the original slice
1065func lastUniqueElements(list []string) []string {
1066 totalSkip := 0
1067 for i := len(list) - 1; i >= totalSkip; i-- {
1068 skip := 0
1069 for j := i - 1; j >= totalSkip; j-- {
1070 if list[i] == list[j] {
1071 skip++
1072 } else {
1073 list[j+skip] = list[j]
1074 }
1075 }
1076 totalSkip += skip
1077 }
1078 return list[totalSkip:]
1079}
Colin Cross06a931b2015-10-28 17:23:31 -07001080
1081var Bool = proptools.Bool