blob: 8ef606f617cf13066ef6e3e6ae79b052c4144cdf [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()
433 if p, ok := c.linker.(prebuiltLinkerInterface); ok {
434 name = p.Name(name)
435 }
436 return name
437}
438
Colin Cross635c3b02016-05-18 15:37:25 -0700439func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800440 ctx := &moduleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700441 ModuleContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800442 moduleContextImpl: moduleContextImpl{
443 mod: c,
444 },
445 }
446 ctx.ctx = ctx
447
448 flags := Flags{
449 Toolchain: c.toolchain(ctx),
450 Clang: c.clang(ctx),
451 }
Colin Crossca860ac2016-01-04 14:34:37 -0800452 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700453 flags = c.compiler.compilerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800454 }
455 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700456 flags = c.linker.linkerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800457 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700458 if c.stl != nil {
459 flags = c.stl.flags(ctx, flags)
460 }
Colin Cross16b23492016-01-06 14:41:07 -0800461 if c.sanitize != nil {
462 flags = c.sanitize.flags(ctx, flags)
463 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800464 if c.coverage != nil {
465 flags = c.coverage.flags(ctx, flags)
466 }
Colin Crossca860ac2016-01-04 14:34:37 -0800467 for _, feature := range c.features {
468 flags = feature.flags(ctx, flags)
469 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800470 if ctx.Failed() {
471 return
472 }
473
Colin Crossb98c8b02016-07-29 13:44:28 -0700474 flags.CFlags, _ = filterList(flags.CFlags, config.IllegalFlags)
475 flags.CppFlags, _ = filterList(flags.CppFlags, config.IllegalFlags)
476 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, config.IllegalFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800477
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800478 deps := c.depsToPaths(ctx)
479 if ctx.Failed() {
480 return
481 }
482 flags.GlobalFlags = append(flags.GlobalFlags, deps.Flags...)
483 c.flags = flags
484
Colin Crossca860ac2016-01-04 14:34:37 -0800485 // Optimization to reduce size of build.ninja
486 // Replace the long list of flags for each file with a module-local variable
487 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
488 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
489 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
490 flags.CFlags = []string{"$cflags"}
491 flags.CppFlags = []string{"$cppflags"}
492 flags.AsFlags = []string{"$asflags"}
493
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700494 var objs Objects
Colin Crossca860ac2016-01-04 14:34:37 -0800495 if c.compiler != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700496 objs = c.compiler.compile(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -0800497 if ctx.Failed() {
498 return
499 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800500 }
501
Colin Crossca860ac2016-01-04 14:34:37 -0800502 if c.linker != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700503 outputFile := c.linker.link(ctx, flags, deps, objs)
Colin Crossca860ac2016-01-04 14:34:37 -0800504 if ctx.Failed() {
505 return
506 }
Colin Cross635c3b02016-05-18 15:37:25 -0700507 c.outputFile = android.OptionalPathForPath(outputFile)
Colin Crossce75d2c2016-10-06 16:12:58 -0700508 }
Colin Cross5049f022015-03-18 13:28:46 -0700509
Colin Crossce75d2c2016-10-06 16:12:58 -0700510 if c.installer != nil && !c.Properties.PreventInstall && c.outputFile.Valid() {
511 c.installer.install(ctx, c.outputFile.Path())
512 if ctx.Failed() {
513 return
Colin Crossca860ac2016-01-04 14:34:37 -0800514 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700515 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800516}
517
Colin Crossb98c8b02016-07-29 13:44:28 -0700518func (c *Module) toolchain(ctx BaseModuleContext) config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800519 if c.cachedToolchain == nil {
Colin Crossb98c8b02016-07-29 13:44:28 -0700520 c.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
Colin Cross3f40fa42015-01-30 17:27:36 -0800521 }
Colin Crossca860ac2016-01-04 14:34:37 -0800522 return c.cachedToolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800523}
524
Colin Crossca860ac2016-01-04 14:34:37 -0800525func (c *Module) begin(ctx BaseModuleContext) {
526 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700527 c.compiler.compilerInit(ctx)
Colin Cross21b9a242015-03-24 14:15:58 -0700528 }
Colin Crossca860ac2016-01-04 14:34:37 -0800529 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700530 c.linker.linkerInit(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800531 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700532 if c.stl != nil {
533 c.stl.begin(ctx)
534 }
Colin Cross16b23492016-01-06 14:41:07 -0800535 if c.sanitize != nil {
536 c.sanitize.begin(ctx)
537 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800538 if c.coverage != nil {
539 c.coverage.begin(ctx)
540 }
Colin Crossca860ac2016-01-04 14:34:37 -0800541 for _, feature := range c.features {
542 feature.begin(ctx)
543 }
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700544 if ctx.sdk() {
545 version, err := normalizeNdkApiLevel(ctx.sdkVersion(), ctx.Arch())
546 if err != nil {
547 ctx.PropertyErrorf("sdk_version", err.Error())
548 }
Dan Albert90f7a4d2016-11-08 14:34:24 -0800549 c.Properties.Sdk_version = version
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700550 }
Colin Crossca860ac2016-01-04 14:34:37 -0800551}
552
Colin Cross37047f12016-12-13 17:06:13 -0800553func (c *Module) deps(ctx DepsContext) Deps {
Colin Crossc99deeb2016-04-11 15:06:20 -0700554 deps := Deps{}
555
556 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700557 deps = c.compiler.compilerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700558 }
559 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700560 deps = c.linker.linkerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700561 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700562 if c.stl != nil {
563 deps = c.stl.deps(ctx, deps)
564 }
Colin Cross16b23492016-01-06 14:41:07 -0800565 if c.sanitize != nil {
566 deps = c.sanitize.deps(ctx, deps)
567 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800568 if c.coverage != nil {
569 deps = c.coverage.deps(ctx, deps)
570 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700571 for _, feature := range c.features {
572 deps = feature.deps(ctx, deps)
573 }
574
575 deps.WholeStaticLibs = lastUniqueElements(deps.WholeStaticLibs)
576 deps.StaticLibs = lastUniqueElements(deps.StaticLibs)
577 deps.LateStaticLibs = lastUniqueElements(deps.LateStaticLibs)
578 deps.SharedLibs = lastUniqueElements(deps.SharedLibs)
579 deps.LateSharedLibs = lastUniqueElements(deps.LateSharedLibs)
Colin Cross5950f382016-12-13 12:50:57 -0800580 deps.HeaderLibs = lastUniqueElements(deps.HeaderLibs)
Colin Crossc99deeb2016-04-11 15:06:20 -0700581
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700582 for _, lib := range deps.ReexportSharedLibHeaders {
583 if !inList(lib, deps.SharedLibs) {
584 ctx.PropertyErrorf("export_shared_lib_headers", "Shared library not in shared_libs: '%s'", lib)
585 }
586 }
587
588 for _, lib := range deps.ReexportStaticLibHeaders {
589 if !inList(lib, deps.StaticLibs) {
590 ctx.PropertyErrorf("export_static_lib_headers", "Static library not in static_libs: '%s'", lib)
591 }
592 }
593
Colin Cross5950f382016-12-13 12:50:57 -0800594 for _, lib := range deps.ReexportHeaderLibHeaders {
595 if !inList(lib, deps.HeaderLibs) {
596 ctx.PropertyErrorf("export_header_lib_headers", "Header library not in header_libs: '%s'", lib)
597 }
598 }
599
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700600 for _, gen := range deps.ReexportGeneratedHeaders {
601 if !inList(gen, deps.GeneratedHeaders) {
602 ctx.PropertyErrorf("export_generated_headers", "Generated header module not in generated_headers: '%s'", gen)
603 }
604 }
605
Colin Crossc99deeb2016-04-11 15:06:20 -0700606 return deps
607}
608
Dan Albert7e9d2952016-08-04 13:02:36 -0700609func (c *Module) beginMutator(actx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800610 ctx := &baseModuleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700611 BaseContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800612 moduleContextImpl: moduleContextImpl{
613 mod: c,
614 },
615 }
616 ctx.ctx = ctx
617
Colin Crossca860ac2016-01-04 14:34:37 -0800618 c.begin(ctx)
Dan Albert7e9d2952016-08-04 13:02:36 -0700619}
620
Colin Cross1e676be2016-10-12 14:38:15 -0700621func (c *Module) DepsMutator(actx android.BottomUpMutatorContext) {
622 if !c.Enabled() {
623 return
624 }
625
Colin Cross37047f12016-12-13 17:06:13 -0800626 ctx := &depsContext{
627 BottomUpMutatorContext: actx,
Dan Albert7e9d2952016-08-04 13:02:36 -0700628 moduleContextImpl: moduleContextImpl{
629 mod: c,
630 },
631 }
632 ctx.ctx = ctx
Colin Crossca860ac2016-01-04 14:34:37 -0800633
Colin Crossc99deeb2016-04-11 15:06:20 -0700634 deps := c.deps(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800635
Colin Crossb5bc4b42016-07-11 16:11:59 -0700636 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.SharedLibs...)
637 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.LateSharedLibs...)
Dan Willemsen72d39932016-07-08 23:23:48 -0700638
Dan Albert914449f2016-06-17 16:45:24 -0700639 variantNdkLibs := []string{}
640 variantLateNdkLibs := []string{}
Dan Willemsenb916b802017-03-19 13:44:32 -0700641 if ctx.Os() == android.Android {
Dan Albert914449f2016-06-17 16:45:24 -0700642 version := ctx.sdkVersion()
Dan Willemsen72d39932016-07-08 23:23:48 -0700643
Dan Albert914449f2016-06-17 16:45:24 -0700644 // Rewrites the names of shared libraries into the names of the NDK
645 // libraries where appropriate. This returns two slices.
646 //
647 // The first is a list of non-variant shared libraries (either rewritten
648 // NDK libraries to the modules in prebuilts/ndk, or not rewritten
649 // because they are not NDK libraries).
650 //
651 // The second is a list of ndk_library modules. These need to be
652 // separated because they are a variation dependency and must be added
653 // in a different manner.
654 rewriteNdkLibs := func(list []string) ([]string, []string) {
655 variantLibs := []string{}
656 nonvariantLibs := []string{}
657 for _, entry := range list {
Dan Willemsenb916b802017-03-19 13:44:32 -0700658 if ctx.sdk() && inList(entry, ndkPrebuiltSharedLibraries) {
Dan Albert914449f2016-06-17 16:45:24 -0700659 if !inList(entry, ndkMigratedLibs) {
660 nonvariantLibs = append(nonvariantLibs, entry+".ndk."+version)
661 } else {
662 variantLibs = append(variantLibs, entry+ndkLibrarySuffix)
663 }
Dan Willemsenb916b802017-03-19 13:44:32 -0700664 } else if ctx.vndk() && inList(entry, config.LLndkLibraries()) {
665 nonvariantLibs = append(nonvariantLibs, entry+llndkLibrarySuffix)
Dan Albert914449f2016-06-17 16:45:24 -0700666 } else {
Dan Willemsen7cbf5f82017-03-28 00:08:30 -0700667 nonvariantLibs = append(nonvariantLibs, entry)
Dan Willemsen72d39932016-07-08 23:23:48 -0700668 }
669 }
Dan Albert914449f2016-06-17 16:45:24 -0700670 return nonvariantLibs, variantLibs
Dan Willemsen72d39932016-07-08 23:23:48 -0700671 }
672
Dan Albert914449f2016-06-17 16:45:24 -0700673 deps.SharedLibs, variantNdkLibs = rewriteNdkLibs(deps.SharedLibs)
674 deps.LateSharedLibs, variantLateNdkLibs = rewriteNdkLibs(deps.LateSharedLibs)
Dan Willemsen72d39932016-07-08 23:23:48 -0700675 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700676
Colin Cross32ec36c2016-12-15 07:39:51 -0800677 for _, lib := range deps.HeaderLibs {
678 depTag := headerDepTag
679 if inList(lib, deps.ReexportHeaderLibHeaders) {
680 depTag = headerExportDepTag
681 }
682 actx.AddVariationDependencies(nil, depTag, lib)
683 }
Colin Cross5950f382016-12-13 12:50:57 -0800684
Colin Crossc99deeb2016-04-11 15:06:20 -0700685 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, wholeStaticDepTag,
686 deps.WholeStaticLibs...)
687
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700688 for _, lib := range deps.StaticLibs {
689 depTag := staticDepTag
690 if inList(lib, deps.ReexportStaticLibHeaders) {
691 depTag = staticExportDepTag
692 }
Colin Cross15a0d462016-07-14 14:49:58 -0700693 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700694 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700695
696 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, lateStaticDepTag,
697 deps.LateStaticLibs...)
698
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700699 for _, lib := range deps.SharedLibs {
700 depTag := sharedDepTag
701 if inList(lib, deps.ReexportSharedLibHeaders) {
702 depTag = sharedExportDepTag
703 }
Colin Cross15a0d462016-07-14 14:49:58 -0700704 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700705 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700706
707 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, lateSharedDepTag,
708 deps.LateSharedLibs...)
709
Colin Cross68861832016-07-08 10:41:41 -0700710 actx.AddDependency(c, genSourceDepTag, deps.GeneratedSources...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700711
712 for _, gen := range deps.GeneratedHeaders {
713 depTag := genHeaderDepTag
714 if inList(gen, deps.ReexportGeneratedHeaders) {
715 depTag = genHeaderExportDepTag
716 }
717 actx.AddDependency(c, depTag, gen)
718 }
Dan Willemsenb40aab62016-04-20 14:21:14 -0700719
Colin Cross68861832016-07-08 10:41:41 -0700720 actx.AddDependency(c, objDepTag, deps.ObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700721
722 if deps.CrtBegin != "" {
Colin Cross68861832016-07-08 10:41:41 -0700723 actx.AddDependency(c, crtBeginDepTag, deps.CrtBegin)
Colin Crossca860ac2016-01-04 14:34:37 -0800724 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700725 if deps.CrtEnd != "" {
Colin Cross68861832016-07-08 10:41:41 -0700726 actx.AddDependency(c, crtEndDepTag, deps.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700727 }
Dan Albert914449f2016-06-17 16:45:24 -0700728
729 version := ctx.sdkVersion()
730 actx.AddVariationDependencies([]blueprint.Variation{
731 {"ndk_api", version}, {"link", "shared"}}, ndkStubDepTag, variantNdkLibs...)
732 actx.AddVariationDependencies([]blueprint.Variation{
733 {"ndk_api", version}, {"link", "shared"}}, ndkLateStubDepTag, variantLateNdkLibs...)
Colin Cross6362e272015-10-29 15:25:03 -0700734}
Colin Cross21b9a242015-03-24 14:15:58 -0700735
Dan Albert7e9d2952016-08-04 13:02:36 -0700736func beginMutator(ctx android.BottomUpMutatorContext) {
737 if c, ok := ctx.Module().(*Module); ok && c.Enabled() {
738 c.beginMutator(ctx)
739 }
740}
741
Colin Crossca860ac2016-01-04 14:34:37 -0800742func (c *Module) clang(ctx BaseModuleContext) bool {
743 clang := Bool(c.Properties.Clang)
744
745 if c.Properties.Clang == nil {
746 if ctx.Host() {
747 clang = true
748 }
749
750 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
751 clang = true
752 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800753 }
Colin Cross28344522015-04-22 13:07:53 -0700754
Colin Crossca860ac2016-01-04 14:34:37 -0800755 if !c.toolchain(ctx).ClangSupported() {
756 clang = false
757 }
758
759 return clang
760}
761
Colin Crossc99deeb2016-04-11 15:06:20 -0700762// Convert dependencies to paths. Returns a PathDeps containing paths
Colin Cross635c3b02016-05-18 15:37:25 -0700763func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
Colin Crossca860ac2016-01-04 14:34:37 -0800764 var depPaths PathDeps
Colin Crossca860ac2016-01-04 14:34:37 -0800765
Dan Willemsena96ff642016-06-07 12:34:45 -0700766 // Whether a module can link to another module, taking into
767 // account NDK linking.
Dan Albert9e10cd42016-08-03 14:12:14 -0700768 checkLinkType := func(from, to *Module) {
Dan Willemsena96ff642016-06-07 12:34:45 -0700769 if from.Target().Os != android.Android {
770 // Host code is not restricted
Dan Albert9e10cd42016-08-03 14:12:14 -0700771 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700772 }
773 if from.Properties.Sdk_version == "" {
774 // Platform code can link to anything
Dan Albert9e10cd42016-08-03 14:12:14 -0700775 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700776 }
Colin Crossb916a382016-07-29 17:28:03 -0700777 if _, ok := to.linker.(*toolchainLibraryDecorator); ok {
Dan Willemsena96ff642016-06-07 12:34:45 -0700778 // These are always allowed
Dan Albert9e10cd42016-08-03 14:12:14 -0700779 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700780 }
781 if _, ok := to.linker.(*ndkPrebuiltLibraryLinker); ok {
782 // These are allowed, but don't set sdk_version
Dan Albert9e10cd42016-08-03 14:12:14 -0700783 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700784 }
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700785 if _, ok := to.linker.(*ndkPrebuiltStlLinker); ok {
786 // These are allowed, but don't set sdk_version
Dan Albert9e10cd42016-08-03 14:12:14 -0700787 return
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700788 }
Colin Crossb916a382016-07-29 17:28:03 -0700789 if _, ok := to.linker.(*stubDecorator); ok {
Dan Albert914449f2016-06-17 16:45:24 -0700790 // These aren't real libraries, but are the stub shared libraries that are included in
791 // the NDK.
Dan Albert9e10cd42016-08-03 14:12:14 -0700792 return
Dan Albert914449f2016-06-17 16:45:24 -0700793 }
Dan Albert9e10cd42016-08-03 14:12:14 -0700794 if to.Properties.Sdk_version == "" {
795 // NDK code linking to platform code is never okay.
796 ctx.ModuleErrorf("depends on non-NDK-built library %q",
797 ctx.OtherModuleName(to))
798 }
799
800 // All this point we know we have two NDK libraries, but we need to
801 // check that we're not linking against anything built against a higher
802 // API level, as it is only valid to link against older or equivalent
803 // APIs.
804
805 if from.Properties.Sdk_version == "current" {
806 // Current can link against anything.
807 return
808 } else if to.Properties.Sdk_version == "current" {
809 // Current can't be linked against by anything else.
810 ctx.ModuleErrorf("links %q built against newer API version %q",
811 ctx.OtherModuleName(to), "current")
812 }
813
814 fromApi, err := strconv.Atoi(from.Properties.Sdk_version)
815 if err != nil {
816 ctx.PropertyErrorf("sdk_version",
817 "Invalid sdk_version value (must be int): %q",
818 from.Properties.Sdk_version)
819 }
820 toApi, err := strconv.Atoi(to.Properties.Sdk_version)
821 if err != nil {
822 ctx.PropertyErrorf("sdk_version",
823 "Invalid sdk_version value (must be int): %q",
824 to.Properties.Sdk_version)
825 }
826
827 if toApi > fromApi {
828 ctx.ModuleErrorf("links %q built against newer API version %q",
829 ctx.OtherModuleName(to), to.Properties.Sdk_version)
830 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700831 }
832
Colin Crossc99deeb2016-04-11 15:06:20 -0700833 ctx.VisitDirectDeps(func(m blueprint.Module) {
834 name := ctx.OtherModuleName(m)
835 tag := ctx.OtherModuleDependencyTag(m)
Colin Crossca860ac2016-01-04 14:34:37 -0800836
Colin Cross635c3b02016-05-18 15:37:25 -0700837 a, _ := m.(android.Module)
Colin Crossc99deeb2016-04-11 15:06:20 -0700838 if a == nil {
839 ctx.ModuleErrorf("module %q not an android module", name)
840 return
Colin Crossca860ac2016-01-04 14:34:37 -0800841 }
Colin Crossca860ac2016-01-04 14:34:37 -0800842
Dan Willemsena96ff642016-06-07 12:34:45 -0700843 cc, _ := m.(*Module)
844 if cc == nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700845 switch tag {
Colin Cross068e0fe2016-12-13 15:23:47 -0800846 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700847 case genSourceDepTag:
848 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
849 depPaths.GeneratedSources = append(depPaths.GeneratedSources,
850 genRule.GeneratedSourceFiles()...)
851 } else {
852 ctx.ModuleErrorf("module %q is not a gensrcs or genrule", name)
853 }
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700854 case genHeaderDepTag, genHeaderExportDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700855 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
856 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders,
857 genRule.GeneratedSourceFiles()...)
Colin Cross5ed99c62016-11-22 12:55:55 -0800858 flags := includeDirsToFlags(genRule.GeneratedHeaderDirs())
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700859 depPaths.Flags = append(depPaths.Flags, flags)
860 if tag == genHeaderExportDepTag {
861 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700862 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps,
863 genRule.GeneratedSourceFiles()...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700864 }
Dan Willemsenb40aab62016-04-20 14:21:14 -0700865 } else {
866 ctx.ModuleErrorf("module %q is not a genrule", name)
867 }
868 default:
Colin Crossc99deeb2016-04-11 15:06:20 -0700869 ctx.ModuleErrorf("depends on non-cc module %q", name)
Colin Crossca860ac2016-01-04 14:34:37 -0800870 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700871 return
872 }
873
874 if !a.Enabled() {
Colin Crossa8f5e9a2016-12-13 12:51:11 -0800875 if ctx.AConfig().AllowMissingDependencies() {
876 ctx.AddMissingDependencies([]string{name})
877 } else {
878 ctx.ModuleErrorf("depends on disabled module %q", name)
879 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700880 return
881 }
882
Colin Crossa1ad8d12016-06-01 17:09:44 -0700883 if a.Target().Os != ctx.Os() {
884 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), name)
885 return
886 }
887
888 if a.Target().Arch.ArchType != ctx.Arch().ArchType {
889 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700890 return
891 }
892
Colin Crossc99deeb2016-04-11 15:06:20 -0700893 if tag == reuseObjTag {
Colin Crossbba99042016-11-23 15:45:05 -0800894 if l, ok := cc.compiler.(libraryInterface); ok {
895 depPaths.Objs = depPaths.Objs.Append(l.reuseObjs())
896 return
897 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700898 }
899
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700900 if t, ok := tag.(dependencyTag); ok && t.library {
Dan Willemsena96ff642016-06-07 12:34:45 -0700901 if i, ok := cc.linker.(exportedFlagsProducer); ok {
Dan Willemsen76f08272016-07-09 00:14:08 -0700902 flags := i.exportedFlags()
Dan Willemsen847dcc72016-09-29 12:13:36 -0700903 deps := i.exportedFlagsDeps()
Dan Willemsen76f08272016-07-09 00:14:08 -0700904 depPaths.Flags = append(depPaths.Flags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700905 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders, deps...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700906
907 if t.reexportFlags {
Dan Willemsen76f08272016-07-09 00:14:08 -0700908 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700909 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps, deps...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700910 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700911 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700912
Dan Albert9e10cd42016-08-03 14:12:14 -0700913 checkLinkType(c, cc)
Colin Crossc99deeb2016-04-11 15:06:20 -0700914 }
915
Colin Cross26c34ed2016-09-30 17:10:16 -0700916 var ptr *android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -0700917 var depPtr *android.Paths
Colin Crossc99deeb2016-04-11 15:06:20 -0700918
Colin Cross26c34ed2016-09-30 17:10:16 -0700919 linkFile := cc.outputFile
920 depFile := android.OptionalPath{}
921
Colin Crossc99deeb2016-04-11 15:06:20 -0700922 switch tag {
Dan Albert914449f2016-06-17 16:45:24 -0700923 case ndkStubDepTag, sharedDepTag, sharedExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700924 ptr = &depPaths.SharedLibs
925 depPtr = &depPaths.SharedLibsDeps
926 depFile = cc.linker.(libraryInterface).toc()
Dan Albert914449f2016-06-17 16:45:24 -0700927 case lateSharedDepTag, ndkLateStubDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700928 ptr = &depPaths.LateSharedLibs
929 depPtr = &depPaths.LateSharedLibsDeps
930 depFile = cc.linker.(libraryInterface).toc()
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700931 case staticDepTag, staticExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700932 ptr = &depPaths.StaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -0700933 case lateStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700934 ptr = &depPaths.LateStaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -0700935 case wholeStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700936 ptr = &depPaths.WholeStaticLibs
Colin Crossb916a382016-07-29 17:28:03 -0700937 staticLib, ok := cc.linker.(libraryInterface)
938 if !ok || !staticLib.static() {
Dan Willemsena96ff642016-06-07 12:34:45 -0700939 ctx.ModuleErrorf("module %q not a static library", name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700940 return
941 }
942
943 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
944 postfix := " (required by " + ctx.OtherModuleName(m) + ")"
945 for i := range missingDeps {
946 missingDeps[i] += postfix
947 }
948 ctx.AddMissingDependencies(missingDeps)
949 }
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700950 depPaths.WholeStaticLibObjs = depPaths.WholeStaticLibObjs.Append(staticLib.objs())
Colin Cross5950f382016-12-13 12:50:57 -0800951 case headerDepTag:
952 // Nothing
Colin Crossc99deeb2016-04-11 15:06:20 -0700953 case objDepTag:
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700954 depPaths.Objs.objFiles = append(depPaths.Objs.objFiles, linkFile.Path())
Colin Crossc99deeb2016-04-11 15:06:20 -0700955 case crtBeginDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700956 depPaths.CrtBegin = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -0700957 case crtEndDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700958 depPaths.CrtEnd = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -0700959 }
960
Dan Willemsen581341d2017-02-09 16:16:31 -0800961 switch tag {
962 case staticDepTag, staticExportDepTag, lateStaticDepTag:
963 staticLib, ok := cc.linker.(libraryInterface)
964 if !ok || !staticLib.static() {
965 ctx.ModuleErrorf("module %q not a static library", name)
966 return
967 }
968
969 // When combining coverage files for shared libraries and executables, coverage files
970 // in static libraries act as if they were whole static libraries.
971 depPaths.StaticLibObjs.coverageFiles = append(depPaths.StaticLibObjs.coverageFiles,
972 staticLib.objs().coverageFiles...)
973 }
974
Colin Cross26c34ed2016-09-30 17:10:16 -0700975 if ptr != nil {
Colin Crossce75d2c2016-10-06 16:12:58 -0700976 if !linkFile.Valid() {
977 ctx.ModuleErrorf("module %q missing output file", name)
978 return
979 }
Colin Cross26c34ed2016-09-30 17:10:16 -0700980 *ptr = append(*ptr, linkFile.Path())
981 }
982
Colin Crossc99deeb2016-04-11 15:06:20 -0700983 if depPtr != nil {
Colin Cross26c34ed2016-09-30 17:10:16 -0700984 dep := depFile
985 if !dep.Valid() {
986 dep = linkFile
987 }
988 *depPtr = append(*depPtr, dep.Path())
Colin Crossca860ac2016-01-04 14:34:37 -0800989 }
990 })
991
992 return depPaths
993}
994
995func (c *Module) InstallInData() bool {
996 if c.installer == nil {
997 return false
998 }
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700999 return c.installer.inData()
1000}
1001
1002func (c *Module) InstallInSanitizerDir() bool {
1003 if c.installer == nil {
1004 return false
1005 }
1006 if c.sanitize != nil && c.sanitize.inSanitizerDir() {
Colin Cross94610402016-08-29 13:41:32 -07001007 return true
1008 }
Vishwath Mohan1dd88392017-03-29 22:00:18 -07001009 return c.installer.inSanitizerDir()
Colin Crossca860ac2016-01-04 14:34:37 -08001010}
1011
Dan Willemsen4aa75ca2016-09-28 16:18:03 -07001012func (c *Module) HostToolPath() android.OptionalPath {
1013 if c.installer == nil {
1014 return android.OptionalPath{}
1015 }
1016 return c.installer.hostToolPath()
1017}
1018
Colin Cross2ba19d92015-05-07 15:44:20 -07001019//
Colin Crosscfad1192015-11-02 16:43:11 -08001020// Defaults
1021//
Colin Crossca860ac2016-01-04 14:34:37 -08001022type Defaults struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001023 android.ModuleBase
1024 android.DefaultsModule
Colin Crosscfad1192015-11-02 16:43:11 -08001025}
1026
Colin Cross635c3b02016-05-18 15:37:25 -07001027func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -08001028}
1029
Colin Cross1e676be2016-10-12 14:38:15 -07001030func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
1031}
1032
Colin Crossca860ac2016-01-04 14:34:37 -08001033func defaultsFactory() (blueprint.Module, []interface{}) {
Colin Crosse1d764e2016-08-18 14:18:32 -07001034 return DefaultsFactory()
1035}
1036
1037func DefaultsFactory(props ...interface{}) (blueprint.Module, []interface{}) {
Colin Crossca860ac2016-01-04 14:34:37 -08001038 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -08001039
Colin Crosse1d764e2016-08-18 14:18:32 -07001040 props = append(props,
Colin Crossca860ac2016-01-04 14:34:37 -08001041 &BaseProperties{},
1042 &BaseCompilerProperties{},
1043 &BaseLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -07001044 &LibraryProperties{},
Colin Cross919281a2016-04-05 16:42:05 -07001045 &FlagExporterProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001046 &BinaryLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -07001047 &TestProperties{},
1048 &TestBinaryProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001049 &UnusedProperties{},
1050 &StlProperties{},
Colin Cross16b23492016-01-06 14:41:07 -08001051 &SanitizeProperties{},
Colin Cross665dce92016-04-28 14:50:03 -07001052 &StripProperties{},
Dan Willemsen7424d612016-09-01 13:45:39 -07001053 &InstallerProperties{},
Dan Willemsena03cf6d2016-09-26 15:45:04 -07001054 &TidyProperties{},
Dan Willemsen581341d2017-02-09 16:16:31 -08001055 &CoverageProperties{},
Colin Crosse1d764e2016-08-18 14:18:32 -07001056 )
Colin Crosscfad1192015-11-02 16:43:11 -08001057
Colin Crosse1d764e2016-08-18 14:18:32 -07001058 return android.InitDefaultsModule(module, module, props...)
Colin Crosscfad1192015-11-02 16:43:11 -08001059}
1060
Colin Cross74d1ec02015-04-28 13:30:13 -07001061// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
1062// modifies the slice contents in place, and returns a subslice of the original slice
1063func lastUniqueElements(list []string) []string {
1064 totalSkip := 0
1065 for i := len(list) - 1; i >= totalSkip; i-- {
1066 skip := 0
1067 for j := i - 1; j >= totalSkip; j-- {
1068 if list[i] == list[j] {
1069 skip++
1070 } else {
1071 list[j+skip] = list[j]
1072 }
1073 }
1074 totalSkip += skip
1075 }
1076 return list[totalSkip:]
1077}
Colin Cross06a931b2015-10-28 17:23:31 -07001078
1079var Bool = proptools.Bool