blob: 63caf3a436af3ae619d285b73a9a4e027f21989b [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()
Dan Willemsen4416e5d2017-04-06 12:43:22 -070038 ctx.BottomUp("image", vendorMutator).Parallel()
Colin Cross1e676be2016-10-12 14:38:15 -070039 ctx.BottomUp("ndk_api", ndkApiMutator).Parallel()
40 ctx.BottomUp("test_per_src", testPerSrcMutator).Parallel()
41 ctx.BottomUp("begin", beginMutator).Parallel()
42 })
Colin Cross16b23492016-01-06 14:41:07 -080043
Colin Cross1e676be2016-10-12 14:38:15 -070044 android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
45 ctx.TopDown("asan_deps", sanitizerDepsMutator(asan))
46 ctx.BottomUp("asan", sanitizerMutator(asan)).Parallel()
Colin Cross16b23492016-01-06 14:41:07 -080047
Colin Cross1e676be2016-10-12 14:38:15 -070048 ctx.TopDown("tsan_deps", sanitizerDepsMutator(tsan))
49 ctx.BottomUp("tsan", sanitizerMutator(tsan)).Parallel()
Dan Willemsen581341d2017-02-09 16:16:31 -080050
51 ctx.BottomUp("coverage", coverageLinkingMutator).Parallel()
Colin Cross1e676be2016-10-12 14:38:15 -070052 })
Colin Crossb98c8b02016-07-29 13:44:28 -070053
54 pctx.Import("android/soong/cc/config")
Colin Cross463a90e2015-06-17 14:20:06 -070055}
56
Colin Crossca860ac2016-01-04 14:34:37 -080057type Deps struct {
58 SharedLibs, LateSharedLibs []string
59 StaticLibs, LateStaticLibs, WholeStaticLibs []string
Colin Cross5950f382016-12-13 12:50:57 -080060 HeaderLibs []string
Colin Crossc472d572015-03-17 15:06:21 -070061
Colin Cross5950f382016-12-13 12:50:57 -080062 ReexportSharedLibHeaders, ReexportStaticLibHeaders, ReexportHeaderLibHeaders []string
Dan Willemsen490a8dc2016-06-06 18:22:19 -070063
Colin Cross81413472016-04-11 14:37:39 -070064 ObjFiles []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -070065
Dan Willemsenb40aab62016-04-20 14:21:14 -070066 GeneratedSources []string
67 GeneratedHeaders []string
68
Dan Willemsenb3454ab2016-09-28 17:34:58 -070069 ReexportGeneratedHeaders []string
70
Colin Cross97ba0732015-03-23 17:50:24 -070071 CrtBegin, CrtEnd string
Colin Crossc472d572015-03-17 15:06:21 -070072}
73
Colin Crossca860ac2016-01-04 14:34:37 -080074type PathDeps struct {
Colin Cross26c34ed2016-09-30 17:10:16 -070075 // Paths to .so files
76 SharedLibs, LateSharedLibs android.Paths
77 // Paths to the dependencies to use for .so files (.so.toc files)
78 SharedLibsDeps, LateSharedLibsDeps android.Paths
79 // Paths to .a files
Colin Cross635c3b02016-05-18 15:37:25 -070080 StaticLibs, LateStaticLibs, WholeStaticLibs android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -070081
Colin Cross26c34ed2016-09-30 17:10:16 -070082 // Paths to .o files
Dan Willemsen5cb580f2016-09-26 17:33:01 -070083 Objs Objects
Dan Willemsen581341d2017-02-09 16:16:31 -080084 StaticLibObjs Objects
Dan Willemsen5cb580f2016-09-26 17:33:01 -070085 WholeStaticLibObjs Objects
Dan Willemsen34cc69e2015-09-23 15:26:20 -070086
Colin Cross26c34ed2016-09-30 17:10:16 -070087 // Paths to generated source files
Colin Cross635c3b02016-05-18 15:37:25 -070088 GeneratedSources android.Paths
89 GeneratedHeaders android.Paths
Dan Willemsenb40aab62016-04-20 14:21:14 -070090
Dan Willemsen76f08272016-07-09 00:14:08 -070091 Flags, ReexportedFlags []string
Dan Willemsen847dcc72016-09-29 12:13:36 -070092 ReexportedFlagsDeps android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -070093
Colin Cross26c34ed2016-09-30 17:10:16 -070094 // Paths to crt*.o files
Colin Cross635c3b02016-05-18 15:37:25 -070095 CrtBegin, CrtEnd android.OptionalPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -070096}
97
Colin Crossca860ac2016-01-04 14:34:37 -080098type Flags struct {
Colin Cross28344522015-04-22 13:07:53 -070099 GlobalFlags []string // Flags that apply to C, C++, and assembly source files
Vishwath Mohan83d9f712017-03-16 11:01:23 -0700100 ArFlags []string // Flags that apply to ar
Colin Cross28344522015-04-22 13:07:53 -0700101 AsFlags []string // Flags that apply to assembly source files
102 CFlags []string // Flags that apply to C and C++ source files
103 ConlyFlags []string // Flags that apply to C source files
104 CppFlags []string // Flags that apply to C++ source files
105 YaccFlags []string // Flags that apply to Yacc source files
Colin Cross0c461f12016-10-20 16:11:43 -0700106 protoFlags []string // Flags that apply to proto source files
Dan Willemsene1240db2016-11-03 14:28:51 -0700107 aidlFlags []string // Flags that apply to aidl source files
Colin Cross28344522015-04-22 13:07:53 -0700108 LdFlags []string // Flags that apply to linker command lines
Colin Cross16b23492016-01-06 14:41:07 -0800109 libFlags []string // Flags to add libraries early to the link order
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700110 TidyFlags []string // Flags that apply to clang-tidy
Colin Cross91e90042016-12-02 17:13:24 -0800111 YasmFlags []string // Flags that apply to yasm assembly source files
Colin Cross28344522015-04-22 13:07:53 -0700112
Colin Crossc3199482017-03-30 15:03:04 -0700113 // Global include flags that apply to C, C++, and assembly source files
114 // These must be after any module include flags, which will be in GlobalFlags.
115 SystemIncludeFlags []string
116
Colin Crossb98c8b02016-07-29 13:44:28 -0700117 Toolchain config.Toolchain
Colin Cross28344522015-04-22 13:07:53 -0700118 Clang bool
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700119 Tidy bool
Dan Willemsen581341d2017-02-09 16:16:31 -0800120 Coverage bool
Colin Crossca860ac2016-01-04 14:34:37 -0800121
122 RequiredInstructionSet string
Colin Cross16b23492016-01-06 14:41:07 -0800123 DynamicLinker string
124
Colin Cross635c3b02016-05-18 15:37:25 -0700125 CFlagsDeps android.Paths // Files depended on by compiler flags
Colin Cross18c0c5a2016-12-01 14:45:23 -0800126
127 GroupStaticLibs bool
Colin Crossc472d572015-03-17 15:06:21 -0700128}
129
Colin Cross81413472016-04-11 14:37:39 -0700130type ObjectLinkerProperties struct {
131 // names of other cc_object modules to link into this module using partial linking
132 Objs []string `android:"arch_variant"`
133}
134
Colin Crossca860ac2016-01-04 14:34:37 -0800135// Properties used to compile all C or C++ modules
136type BaseProperties struct {
137 // compile module with clang instead of gcc
138 Clang *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700139
140 // Minimum sdk version supported when compiling against the ndk
141 Sdk_version string
142
Colin Crossca860ac2016-01-04 14:34:37 -0800143 // don't insert default compiler flags into asflags, cflags,
144 // cppflags, conlyflags, ldflags, or include_dirs
145 No_default_compiler_flags *bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700146
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700147 // whether this module should be allowed to install onto /vendor as
148 // well as /system. The two variants will be built separately, one
149 // like normal, and the other limited to the set of libraries and
150 // headers that are exposed to /vendor modules.
151 //
152 // The vendor variant may be used with a different (newer) /system,
153 // so it shouldn't have any unversioned runtime dependencies, or
154 // make assumptions about the system that may not be true in the
155 // future.
156 //
157 // Nothing happens if BOARD_VNDK_VERSION isn't set in the BoardConfig.mk
158 Vendor_available *bool
159
Colin Crossc99deeb2016-04-11 15:06:20 -0700160 AndroidMkSharedLibs []string `blueprint:"mutated"`
Colin Crossbc6fb162016-05-24 15:39:04 -0700161 HideFromMake bool `blueprint:"mutated"`
Colin Crossb0f28952016-09-19 16:46:53 -0700162 PreventInstall bool `blueprint:"mutated"`
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700163
164 UseVndk bool `blueprint:"mutated"`
Colin Crossca860ac2016-01-04 14:34:37 -0800165}
166
Colin Crossca860ac2016-01-04 14:34:37 -0800167type UnusedProperties struct {
Dan Willemsen581341d2017-02-09 16:16:31 -0800168 Tags []string
Colin Crosscfad1192015-11-02 16:43:11 -0800169}
170
Colin Crossca860ac2016-01-04 14:34:37 -0800171type ModuleContextIntf interface {
Colin Crossca860ac2016-01-04 14:34:37 -0800172 static() bool
173 staticBinary() bool
174 clang() bool
Colin Crossb98c8b02016-07-29 13:44:28 -0700175 toolchain() config.Toolchain
Colin Crossca860ac2016-01-04 14:34:37 -0800176 noDefaultCompilerFlags() bool
177 sdk() bool
178 sdkVersion() string
Dan Willemsend2ede872016-11-18 14:54:24 -0800179 vndk() bool
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700180 selectedStl() string
Colin Crossce75d2c2016-10-06 16:12:58 -0700181 baseModuleName() string
Colin Crossca860ac2016-01-04 14:34:37 -0800182}
183
184type ModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700185 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800186 ModuleContextIntf
187}
188
189type BaseModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700190 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800191 ModuleContextIntf
192}
193
Colin Cross37047f12016-12-13 17:06:13 -0800194type DepsContext interface {
195 android.BottomUpMutatorContext
196 ModuleContextIntf
197}
198
Colin Crossca860ac2016-01-04 14:34:37 -0800199type feature interface {
200 begin(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800201 deps(ctx DepsContext, deps Deps) Deps
Colin Crossca860ac2016-01-04 14:34:37 -0800202 flags(ctx ModuleContext, flags Flags) Flags
203 props() []interface{}
204}
205
206type compiler interface {
Colin Cross42742b82016-08-01 13:20:05 -0700207 compilerInit(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800208 compilerDeps(ctx DepsContext, deps Deps) Deps
Colin Cross42742b82016-08-01 13:20:05 -0700209 compilerFlags(ctx ModuleContext, flags Flags) Flags
210 compilerProps() []interface{}
211
Colin Cross76fada02016-07-27 10:31:13 -0700212 appendCflags([]string)
213 appendAsflags([]string)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700214 compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects
Colin Crossca860ac2016-01-04 14:34:37 -0800215}
216
217type linker interface {
Colin Cross42742b82016-08-01 13:20:05 -0700218 linkerInit(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800219 linkerDeps(ctx DepsContext, deps Deps) Deps
Colin Cross42742b82016-08-01 13:20:05 -0700220 linkerFlags(ctx ModuleContext, flags Flags) Flags
221 linkerProps() []interface{}
222
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700223 link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path
Colin Cross76fada02016-07-27 10:31:13 -0700224 appendLdflags([]string)
Colin Crossca860ac2016-01-04 14:34:37 -0800225}
226
227type installer interface {
Colin Cross42742b82016-08-01 13:20:05 -0700228 installerProps() []interface{}
Colin Cross635c3b02016-05-18 15:37:25 -0700229 install(ctx ModuleContext, path android.Path)
Colin Crossca860ac2016-01-04 14:34:37 -0800230 inData() bool
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700231 inSanitizerDir() bool
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700232 hostToolPath() android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800233}
234
Colin Crossc99deeb2016-04-11 15:06:20 -0700235type dependencyTag struct {
236 blueprint.BaseDependencyTag
237 name string
238 library bool
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700239
240 reexportFlags bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700241}
242
243var (
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700244 sharedDepTag = dependencyTag{name: "shared", library: true}
245 sharedExportDepTag = dependencyTag{name: "shared", library: true, reexportFlags: true}
246 lateSharedDepTag = dependencyTag{name: "late shared", library: true}
247 staticDepTag = dependencyTag{name: "static", library: true}
248 staticExportDepTag = dependencyTag{name: "static", library: true, reexportFlags: true}
249 lateStaticDepTag = dependencyTag{name: "late static", library: true}
250 wholeStaticDepTag = dependencyTag{name: "whole static", library: true, reexportFlags: true}
Colin Cross32ec36c2016-12-15 07:39:51 -0800251 headerDepTag = dependencyTag{name: "header", library: true}
252 headerExportDepTag = dependencyTag{name: "header", library: true, reexportFlags: true}
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700253 genSourceDepTag = dependencyTag{name: "gen source"}
254 genHeaderDepTag = dependencyTag{name: "gen header"}
255 genHeaderExportDepTag = dependencyTag{name: "gen header", reexportFlags: true}
256 objDepTag = dependencyTag{name: "obj"}
257 crtBeginDepTag = dependencyTag{name: "crtbegin"}
258 crtEndDepTag = dependencyTag{name: "crtend"}
259 reuseObjTag = dependencyTag{name: "reuse objects"}
260 ndkStubDepTag = dependencyTag{name: "ndk stub", library: true}
261 ndkLateStubDepTag = dependencyTag{name: "ndk late stub", library: true}
Colin Crossc99deeb2016-04-11 15:06:20 -0700262)
263
Colin Crossca860ac2016-01-04 14:34:37 -0800264// Module contains the properties and members used by all C/C++ module types, and implements
265// the blueprint.Module interface. It delegates to compiler, linker, and installer interfaces
266// to construct the output file. Behavior can be customized with a Customizer interface
267type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700268 android.ModuleBase
269 android.DefaultableModule
Colin Crossc472d572015-03-17 15:06:21 -0700270
Colin Crossca860ac2016-01-04 14:34:37 -0800271 Properties BaseProperties
272 unused UnusedProperties
Colin Crossfa138792015-04-24 17:31:52 -0700273
Colin Crossca860ac2016-01-04 14:34:37 -0800274 // initialize before calling Init
Colin Cross635c3b02016-05-18 15:37:25 -0700275 hod android.HostOrDeviceSupported
276 multilib android.Multilib
Colin Crossc472d572015-03-17 15:06:21 -0700277
Colin Crossca860ac2016-01-04 14:34:37 -0800278 // delegates, initialize before calling Init
Colin Crossb4ce0ec2016-09-13 13:41:39 -0700279 features []feature
280 compiler compiler
281 linker linker
282 installer installer
283 stl *stl
284 sanitize *sanitize
Dan Willemsen581341d2017-02-09 16:16:31 -0800285 coverage *coverage
Colin Cross16b23492016-01-06 14:41:07 -0800286
287 androidMkSharedLibDeps []string
Colin Cross74d1ec02015-04-28 13:30:13 -0700288
Colin Cross635c3b02016-05-18 15:37:25 -0700289 outputFile android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800290
Colin Crossb98c8b02016-07-29 13:44:28 -0700291 cachedToolchain config.Toolchain
Colin Crossb916a382016-07-29 17:28:03 -0700292
293 subAndroidMkOnce map[subAndroidMkProvider]bool
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800294
295 // Flags used to compile this module
296 flags Flags
Colin Crossc472d572015-03-17 15:06:21 -0700297}
298
Colin Crossca860ac2016-01-04 14:34:37 -0800299func (c *Module) Init() (blueprint.Module, []interface{}) {
300 props := []interface{}{&c.Properties, &c.unused}
Colin Crossca860ac2016-01-04 14:34:37 -0800301 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700302 props = append(props, c.compiler.compilerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800303 }
304 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700305 props = append(props, c.linker.linkerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800306 }
307 if c.installer != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700308 props = append(props, c.installer.installerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800309 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700310 if c.stl != nil {
311 props = append(props, c.stl.props()...)
312 }
Colin Cross16b23492016-01-06 14:41:07 -0800313 if c.sanitize != nil {
314 props = append(props, c.sanitize.props()...)
315 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800316 if c.coverage != nil {
317 props = append(props, c.coverage.props()...)
318 }
Colin Crossca860ac2016-01-04 14:34:37 -0800319 for _, feature := range c.features {
320 props = append(props, feature.props()...)
321 }
Colin Crossc472d572015-03-17 15:06:21 -0700322
Colin Cross635c3b02016-05-18 15:37:25 -0700323 _, props = android.InitAndroidArchModule(c, c.hod, c.multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700324
Colin Cross635c3b02016-05-18 15:37:25 -0700325 return android.InitDefaultableModule(c, c, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700326}
327
Colin Crossb916a382016-07-29 17:28:03 -0700328// Returns true for dependency roots (binaries)
329// TODO(ccross): also handle dlopenable libraries
330func (c *Module) isDependencyRoot() bool {
331 if root, ok := c.linker.(interface {
332 isDependencyRoot() bool
333 }); ok {
334 return root.isDependencyRoot()
335 }
336 return false
337}
338
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700339func (c *Module) vndk() bool {
340 return c.Properties.UseVndk
341}
342
Colin Crossca860ac2016-01-04 14:34:37 -0800343type baseModuleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700344 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800345 moduleContextImpl
346}
347
Colin Cross37047f12016-12-13 17:06:13 -0800348type depsContext struct {
349 android.BottomUpMutatorContext
350 moduleContextImpl
351}
352
Colin Crossca860ac2016-01-04 14:34:37 -0800353type moduleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700354 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800355 moduleContextImpl
356}
357
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700358// Vendor returns true for vendor modules so that they get installed onto the
359// correct partition
360func (ctx *moduleContext) Vendor() bool {
361 return ctx.ModuleContext.Vendor() || ctx.moduleContextImpl.mod.Properties.UseVndk
362}
363
Colin Crossca860ac2016-01-04 14:34:37 -0800364type moduleContextImpl struct {
365 mod *Module
366 ctx BaseModuleContext
367}
368
Colin Crossca860ac2016-01-04 14:34:37 -0800369func (ctx *moduleContextImpl) clang() bool {
370 return ctx.mod.clang(ctx.ctx)
371}
372
Colin Crossb98c8b02016-07-29 13:44:28 -0700373func (ctx *moduleContextImpl) toolchain() config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800374 return ctx.mod.toolchain(ctx.ctx)
375}
376
377func (ctx *moduleContextImpl) static() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700378 if static, ok := ctx.mod.linker.(interface {
379 static() bool
380 }); ok {
381 return static.static()
Colin Crossca860ac2016-01-04 14:34:37 -0800382 }
Colin Crossb916a382016-07-29 17:28:03 -0700383 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800384}
385
386func (ctx *moduleContextImpl) staticBinary() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700387 if static, ok := ctx.mod.linker.(interface {
388 staticBinary() bool
389 }); ok {
390 return static.staticBinary()
Colin Crossca860ac2016-01-04 14:34:37 -0800391 }
Colin Crossb916a382016-07-29 17:28:03 -0700392 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800393}
394
395func (ctx *moduleContextImpl) noDefaultCompilerFlags() bool {
396 return Bool(ctx.mod.Properties.No_default_compiler_flags)
397}
398
399func (ctx *moduleContextImpl) sdk() bool {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700400 if ctx.ctx.Device() && !ctx.vndk() {
Dan Willemsena96ff642016-06-07 12:34:45 -0700401 return ctx.mod.Properties.Sdk_version != ""
402 }
403 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800404}
405
406func (ctx *moduleContextImpl) sdkVersion() string {
Dan Willemsena96ff642016-06-07 12:34:45 -0700407 if ctx.ctx.Device() {
Dan Willemsen11b26142017-03-19 18:30:37 -0700408 if ctx.vndk() {
409 return "current"
Dan Willemsend2ede872016-11-18 14:54:24 -0800410 } else {
411 return ctx.mod.Properties.Sdk_version
412 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700413 }
414 return ""
Colin Crossca860ac2016-01-04 14:34:37 -0800415}
416
Dan Willemsend2ede872016-11-18 14:54:24 -0800417func (ctx *moduleContextImpl) vndk() bool {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700418 return ctx.mod.vndk()
Dan Willemsend2ede872016-11-18 14:54:24 -0800419}
420
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700421func (ctx *moduleContextImpl) selectedStl() string {
422 if stl := ctx.mod.stl; stl != nil {
423 return stl.Properties.SelectedStl
424 }
425 return ""
426}
427
Colin Crossce75d2c2016-10-06 16:12:58 -0700428func (ctx *moduleContextImpl) baseModuleName() string {
429 return ctx.mod.ModuleBase.BaseModuleName()
430}
431
Colin Cross635c3b02016-05-18 15:37:25 -0700432func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800433 return &Module{
434 hod: hod,
435 multilib: multilib,
436 }
437}
438
Colin Cross635c3b02016-05-18 15:37:25 -0700439func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800440 module := newBaseModule(hod, multilib)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700441 module.features = []feature{
442 &tidyFeature{},
443 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700444 module.stl = &stl{}
Colin Cross16b23492016-01-06 14:41:07 -0800445 module.sanitize = &sanitize{}
Dan Willemsen581341d2017-02-09 16:16:31 -0800446 module.coverage = &coverage{}
Colin Crossca860ac2016-01-04 14:34:37 -0800447 return module
448}
449
Colin Crossce75d2c2016-10-06 16:12:58 -0700450func (c *Module) Prebuilt() *android.Prebuilt {
451 if p, ok := c.linker.(prebuiltLinkerInterface); ok {
452 return p.prebuilt()
453 }
454 return nil
455}
456
457func (c *Module) Name() string {
458 name := c.ModuleBase.Name()
Dan Willemsen01a90592017-04-07 15:21:13 -0700459 if p, ok := c.linker.(interface {
460 Name(string) string
461 }); ok {
Colin Crossce75d2c2016-10-06 16:12:58 -0700462 name = p.Name(name)
463 }
464 return name
465}
466
Colin Cross635c3b02016-05-18 15:37:25 -0700467func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800468 ctx := &moduleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700469 ModuleContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800470 moduleContextImpl: moduleContextImpl{
471 mod: c,
472 },
473 }
474 ctx.ctx = ctx
475
476 flags := Flags{
477 Toolchain: c.toolchain(ctx),
478 Clang: c.clang(ctx),
479 }
Colin Crossca860ac2016-01-04 14:34:37 -0800480 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700481 flags = c.compiler.compilerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800482 }
483 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700484 flags = c.linker.linkerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800485 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700486 if c.stl != nil {
487 flags = c.stl.flags(ctx, flags)
488 }
Colin Cross16b23492016-01-06 14:41:07 -0800489 if c.sanitize != nil {
490 flags = c.sanitize.flags(ctx, flags)
491 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800492 if c.coverage != nil {
493 flags = c.coverage.flags(ctx, flags)
494 }
Colin Crossca860ac2016-01-04 14:34:37 -0800495 for _, feature := range c.features {
496 flags = feature.flags(ctx, flags)
497 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800498 if ctx.Failed() {
499 return
500 }
501
Colin Crossb98c8b02016-07-29 13:44:28 -0700502 flags.CFlags, _ = filterList(flags.CFlags, config.IllegalFlags)
503 flags.CppFlags, _ = filterList(flags.CppFlags, config.IllegalFlags)
504 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, config.IllegalFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800505
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800506 deps := c.depsToPaths(ctx)
507 if ctx.Failed() {
508 return
509 }
510 flags.GlobalFlags = append(flags.GlobalFlags, deps.Flags...)
511 c.flags = flags
512
Colin Crossca860ac2016-01-04 14:34:37 -0800513 // Optimization to reduce size of build.ninja
514 // Replace the long list of flags for each file with a module-local variable
515 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
516 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
517 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
518 flags.CFlags = []string{"$cflags"}
519 flags.CppFlags = []string{"$cppflags"}
520 flags.AsFlags = []string{"$asflags"}
521
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700522 var objs Objects
Colin Crossca860ac2016-01-04 14:34:37 -0800523 if c.compiler != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700524 objs = c.compiler.compile(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -0800525 if ctx.Failed() {
526 return
527 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800528 }
529
Colin Crossca860ac2016-01-04 14:34:37 -0800530 if c.linker != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700531 outputFile := c.linker.link(ctx, flags, deps, objs)
Colin Crossca860ac2016-01-04 14:34:37 -0800532 if ctx.Failed() {
533 return
534 }
Colin Cross635c3b02016-05-18 15:37:25 -0700535 c.outputFile = android.OptionalPathForPath(outputFile)
Colin Crossce75d2c2016-10-06 16:12:58 -0700536 }
Colin Cross5049f022015-03-18 13:28:46 -0700537
Colin Crossce75d2c2016-10-06 16:12:58 -0700538 if c.installer != nil && !c.Properties.PreventInstall && c.outputFile.Valid() {
539 c.installer.install(ctx, c.outputFile.Path())
540 if ctx.Failed() {
541 return
Colin Crossca860ac2016-01-04 14:34:37 -0800542 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700543 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800544}
545
Colin Crossb98c8b02016-07-29 13:44:28 -0700546func (c *Module) toolchain(ctx BaseModuleContext) config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800547 if c.cachedToolchain == nil {
Colin Crossb98c8b02016-07-29 13:44:28 -0700548 c.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
Colin Cross3f40fa42015-01-30 17:27:36 -0800549 }
Colin Crossca860ac2016-01-04 14:34:37 -0800550 return c.cachedToolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800551}
552
Colin Crossca860ac2016-01-04 14:34:37 -0800553func (c *Module) begin(ctx BaseModuleContext) {
554 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700555 c.compiler.compilerInit(ctx)
Colin Cross21b9a242015-03-24 14:15:58 -0700556 }
Colin Crossca860ac2016-01-04 14:34:37 -0800557 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700558 c.linker.linkerInit(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800559 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700560 if c.stl != nil {
561 c.stl.begin(ctx)
562 }
Colin Cross16b23492016-01-06 14:41:07 -0800563 if c.sanitize != nil {
564 c.sanitize.begin(ctx)
565 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800566 if c.coverage != nil {
567 c.coverage.begin(ctx)
568 }
Colin Crossca860ac2016-01-04 14:34:37 -0800569 for _, feature := range c.features {
570 feature.begin(ctx)
571 }
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700572 if ctx.sdk() {
573 version, err := normalizeNdkApiLevel(ctx.sdkVersion(), ctx.Arch())
574 if err != nil {
575 ctx.PropertyErrorf("sdk_version", err.Error())
576 }
Dan Albert90f7a4d2016-11-08 14:34:24 -0800577 c.Properties.Sdk_version = version
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700578 }
Colin Crossca860ac2016-01-04 14:34:37 -0800579}
580
Colin Cross37047f12016-12-13 17:06:13 -0800581func (c *Module) deps(ctx DepsContext) Deps {
Colin Crossc99deeb2016-04-11 15:06:20 -0700582 deps := Deps{}
583
584 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700585 deps = c.compiler.compilerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700586 }
587 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700588 deps = c.linker.linkerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700589 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700590 if c.stl != nil {
591 deps = c.stl.deps(ctx, deps)
592 }
Colin Cross16b23492016-01-06 14:41:07 -0800593 if c.sanitize != nil {
594 deps = c.sanitize.deps(ctx, deps)
595 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800596 if c.coverage != nil {
597 deps = c.coverage.deps(ctx, deps)
598 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700599 for _, feature := range c.features {
600 deps = feature.deps(ctx, deps)
601 }
602
603 deps.WholeStaticLibs = lastUniqueElements(deps.WholeStaticLibs)
604 deps.StaticLibs = lastUniqueElements(deps.StaticLibs)
605 deps.LateStaticLibs = lastUniqueElements(deps.LateStaticLibs)
606 deps.SharedLibs = lastUniqueElements(deps.SharedLibs)
607 deps.LateSharedLibs = lastUniqueElements(deps.LateSharedLibs)
Colin Cross5950f382016-12-13 12:50:57 -0800608 deps.HeaderLibs = lastUniqueElements(deps.HeaderLibs)
Colin Crossc99deeb2016-04-11 15:06:20 -0700609
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700610 for _, lib := range deps.ReexportSharedLibHeaders {
611 if !inList(lib, deps.SharedLibs) {
612 ctx.PropertyErrorf("export_shared_lib_headers", "Shared library not in shared_libs: '%s'", lib)
613 }
614 }
615
616 for _, lib := range deps.ReexportStaticLibHeaders {
617 if !inList(lib, deps.StaticLibs) {
618 ctx.PropertyErrorf("export_static_lib_headers", "Static library not in static_libs: '%s'", lib)
619 }
620 }
621
Colin Cross5950f382016-12-13 12:50:57 -0800622 for _, lib := range deps.ReexportHeaderLibHeaders {
623 if !inList(lib, deps.HeaderLibs) {
624 ctx.PropertyErrorf("export_header_lib_headers", "Header library not in header_libs: '%s'", lib)
625 }
626 }
627
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700628 for _, gen := range deps.ReexportGeneratedHeaders {
629 if !inList(gen, deps.GeneratedHeaders) {
630 ctx.PropertyErrorf("export_generated_headers", "Generated header module not in generated_headers: '%s'", gen)
631 }
632 }
633
Colin Crossc99deeb2016-04-11 15:06:20 -0700634 return deps
635}
636
Dan Albert7e9d2952016-08-04 13:02:36 -0700637func (c *Module) beginMutator(actx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800638 ctx := &baseModuleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700639 BaseContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800640 moduleContextImpl: moduleContextImpl{
641 mod: c,
642 },
643 }
644 ctx.ctx = ctx
645
Colin Crossca860ac2016-01-04 14:34:37 -0800646 c.begin(ctx)
Dan Albert7e9d2952016-08-04 13:02:36 -0700647}
648
Colin Cross1e676be2016-10-12 14:38:15 -0700649func (c *Module) DepsMutator(actx android.BottomUpMutatorContext) {
650 if !c.Enabled() {
651 return
652 }
653
Colin Cross37047f12016-12-13 17:06:13 -0800654 ctx := &depsContext{
655 BottomUpMutatorContext: actx,
Dan Albert7e9d2952016-08-04 13:02:36 -0700656 moduleContextImpl: moduleContextImpl{
657 mod: c,
658 },
659 }
660 ctx.ctx = ctx
Colin Crossca860ac2016-01-04 14:34:37 -0800661
Colin Crossc99deeb2016-04-11 15:06:20 -0700662 deps := c.deps(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800663
Colin Crossb5bc4b42016-07-11 16:11:59 -0700664 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.SharedLibs...)
665 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.LateSharedLibs...)
Dan Willemsen72d39932016-07-08 23:23:48 -0700666
Dan Albert914449f2016-06-17 16:45:24 -0700667 variantNdkLibs := []string{}
668 variantLateNdkLibs := []string{}
Dan Willemsenb916b802017-03-19 13:44:32 -0700669 if ctx.Os() == android.Android {
Dan Albert914449f2016-06-17 16:45:24 -0700670 version := ctx.sdkVersion()
Dan Willemsen72d39932016-07-08 23:23:48 -0700671
Dan Albert914449f2016-06-17 16:45:24 -0700672 // Rewrites the names of shared libraries into the names of the NDK
673 // libraries where appropriate. This returns two slices.
674 //
675 // The first is a list of non-variant shared libraries (either rewritten
676 // NDK libraries to the modules in prebuilts/ndk, or not rewritten
677 // because they are not NDK libraries).
678 //
679 // The second is a list of ndk_library modules. These need to be
680 // separated because they are a variation dependency and must be added
681 // in a different manner.
682 rewriteNdkLibs := func(list []string) ([]string, []string) {
683 variantLibs := []string{}
684 nonvariantLibs := []string{}
685 for _, entry := range list {
Dan Willemsenb916b802017-03-19 13:44:32 -0700686 if ctx.sdk() && inList(entry, ndkPrebuiltSharedLibraries) {
Dan Albert914449f2016-06-17 16:45:24 -0700687 if !inList(entry, ndkMigratedLibs) {
688 nonvariantLibs = append(nonvariantLibs, entry+".ndk."+version)
689 } else {
690 variantLibs = append(variantLibs, entry+ndkLibrarySuffix)
691 }
Dan Willemsenb916b802017-03-19 13:44:32 -0700692 } else if ctx.vndk() && inList(entry, config.LLndkLibraries()) {
693 nonvariantLibs = append(nonvariantLibs, entry+llndkLibrarySuffix)
Dan Albert914449f2016-06-17 16:45:24 -0700694 } else {
Dan Willemsen7cbf5f82017-03-28 00:08:30 -0700695 nonvariantLibs = append(nonvariantLibs, entry)
Dan Willemsen72d39932016-07-08 23:23:48 -0700696 }
697 }
Dan Albert914449f2016-06-17 16:45:24 -0700698 return nonvariantLibs, variantLibs
Dan Willemsen72d39932016-07-08 23:23:48 -0700699 }
700
Dan Albert914449f2016-06-17 16:45:24 -0700701 deps.SharedLibs, variantNdkLibs = rewriteNdkLibs(deps.SharedLibs)
702 deps.LateSharedLibs, variantLateNdkLibs = rewriteNdkLibs(deps.LateSharedLibs)
Dan Willemsen72d39932016-07-08 23:23:48 -0700703 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700704
Colin Cross32ec36c2016-12-15 07:39:51 -0800705 for _, lib := range deps.HeaderLibs {
706 depTag := headerDepTag
707 if inList(lib, deps.ReexportHeaderLibHeaders) {
708 depTag = headerExportDepTag
709 }
710 actx.AddVariationDependencies(nil, depTag, lib)
711 }
Colin Cross5950f382016-12-13 12:50:57 -0800712
Colin Crossc99deeb2016-04-11 15:06:20 -0700713 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, wholeStaticDepTag,
714 deps.WholeStaticLibs...)
715
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700716 for _, lib := range deps.StaticLibs {
717 depTag := staticDepTag
718 if inList(lib, deps.ReexportStaticLibHeaders) {
719 depTag = staticExportDepTag
720 }
Colin Cross15a0d462016-07-14 14:49:58 -0700721 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700722 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700723
724 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, lateStaticDepTag,
725 deps.LateStaticLibs...)
726
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700727 for _, lib := range deps.SharedLibs {
728 depTag := sharedDepTag
729 if inList(lib, deps.ReexportSharedLibHeaders) {
730 depTag = sharedExportDepTag
731 }
Colin Cross15a0d462016-07-14 14:49:58 -0700732 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700733 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700734
735 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, lateSharedDepTag,
736 deps.LateSharedLibs...)
737
Colin Cross68861832016-07-08 10:41:41 -0700738 actx.AddDependency(c, genSourceDepTag, deps.GeneratedSources...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700739
740 for _, gen := range deps.GeneratedHeaders {
741 depTag := genHeaderDepTag
742 if inList(gen, deps.ReexportGeneratedHeaders) {
743 depTag = genHeaderExportDepTag
744 }
745 actx.AddDependency(c, depTag, gen)
746 }
Dan Willemsenb40aab62016-04-20 14:21:14 -0700747
Colin Cross68861832016-07-08 10:41:41 -0700748 actx.AddDependency(c, objDepTag, deps.ObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700749
750 if deps.CrtBegin != "" {
Colin Cross68861832016-07-08 10:41:41 -0700751 actx.AddDependency(c, crtBeginDepTag, deps.CrtBegin)
Colin Crossca860ac2016-01-04 14:34:37 -0800752 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700753 if deps.CrtEnd != "" {
Colin Cross68861832016-07-08 10:41:41 -0700754 actx.AddDependency(c, crtEndDepTag, deps.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700755 }
Dan Albert914449f2016-06-17 16:45:24 -0700756
757 version := ctx.sdkVersion()
758 actx.AddVariationDependencies([]blueprint.Variation{
759 {"ndk_api", version}, {"link", "shared"}}, ndkStubDepTag, variantNdkLibs...)
760 actx.AddVariationDependencies([]blueprint.Variation{
761 {"ndk_api", version}, {"link", "shared"}}, ndkLateStubDepTag, variantLateNdkLibs...)
Colin Cross6362e272015-10-29 15:25:03 -0700762}
Colin Cross21b9a242015-03-24 14:15:58 -0700763
Dan Albert7e9d2952016-08-04 13:02:36 -0700764func beginMutator(ctx android.BottomUpMutatorContext) {
765 if c, ok := ctx.Module().(*Module); ok && c.Enabled() {
766 c.beginMutator(ctx)
767 }
768}
769
Colin Crossca860ac2016-01-04 14:34:37 -0800770func (c *Module) clang(ctx BaseModuleContext) bool {
771 clang := Bool(c.Properties.Clang)
772
773 if c.Properties.Clang == nil {
774 if ctx.Host() {
775 clang = true
776 }
777
778 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
779 clang = true
780 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800781 }
Colin Cross28344522015-04-22 13:07:53 -0700782
Colin Crossca860ac2016-01-04 14:34:37 -0800783 if !c.toolchain(ctx).ClangSupported() {
784 clang = false
785 }
786
787 return clang
788}
789
Colin Crossc99deeb2016-04-11 15:06:20 -0700790// Convert dependencies to paths. Returns a PathDeps containing paths
Colin Cross635c3b02016-05-18 15:37:25 -0700791func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
Colin Crossca860ac2016-01-04 14:34:37 -0800792 var depPaths PathDeps
Colin Crossca860ac2016-01-04 14:34:37 -0800793
Dan Willemsena96ff642016-06-07 12:34:45 -0700794 // Whether a module can link to another module, taking into
795 // account NDK linking.
Dan Albert9e10cd42016-08-03 14:12:14 -0700796 checkLinkType := func(from, to *Module) {
Dan Willemsena96ff642016-06-07 12:34:45 -0700797 if from.Target().Os != android.Android {
798 // Host code is not restricted
Dan Albert9e10cd42016-08-03 14:12:14 -0700799 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700800 }
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700801 if from.Properties.UseVndk {
802 // Vendor code is already limited by the vendor mutator
803 return
804 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700805 if from.Properties.Sdk_version == "" {
806 // Platform code can link to anything
Dan Albert9e10cd42016-08-03 14:12:14 -0700807 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700808 }
Colin Crossb916a382016-07-29 17:28:03 -0700809 if _, ok := to.linker.(*toolchainLibraryDecorator); ok {
Dan Willemsena96ff642016-06-07 12:34:45 -0700810 // These are always allowed
Dan Albert9e10cd42016-08-03 14:12:14 -0700811 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700812 }
813 if _, ok := to.linker.(*ndkPrebuiltLibraryLinker); ok {
814 // These are allowed, but don't set sdk_version
Dan Albert9e10cd42016-08-03 14:12:14 -0700815 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700816 }
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700817 if _, ok := to.linker.(*ndkPrebuiltStlLinker); ok {
818 // These are allowed, but don't set sdk_version
Dan Albert9e10cd42016-08-03 14:12:14 -0700819 return
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700820 }
Colin Crossb916a382016-07-29 17:28:03 -0700821 if _, ok := to.linker.(*stubDecorator); ok {
Dan Albert914449f2016-06-17 16:45:24 -0700822 // These aren't real libraries, but are the stub shared libraries that are included in
823 // the NDK.
Dan Albert9e10cd42016-08-03 14:12:14 -0700824 return
Dan Albert914449f2016-06-17 16:45:24 -0700825 }
Dan Albert9e10cd42016-08-03 14:12:14 -0700826 if to.Properties.Sdk_version == "" {
827 // NDK code linking to platform code is never okay.
828 ctx.ModuleErrorf("depends on non-NDK-built library %q",
829 ctx.OtherModuleName(to))
830 }
831
832 // All this point we know we have two NDK libraries, but we need to
833 // check that we're not linking against anything built against a higher
834 // API level, as it is only valid to link against older or equivalent
835 // APIs.
836
837 if from.Properties.Sdk_version == "current" {
838 // Current can link against anything.
839 return
840 } else if to.Properties.Sdk_version == "current" {
841 // Current can't be linked against by anything else.
842 ctx.ModuleErrorf("links %q built against newer API version %q",
843 ctx.OtherModuleName(to), "current")
844 }
845
846 fromApi, err := strconv.Atoi(from.Properties.Sdk_version)
847 if err != nil {
848 ctx.PropertyErrorf("sdk_version",
849 "Invalid sdk_version value (must be int): %q",
850 from.Properties.Sdk_version)
851 }
852 toApi, err := strconv.Atoi(to.Properties.Sdk_version)
853 if err != nil {
854 ctx.PropertyErrorf("sdk_version",
855 "Invalid sdk_version value (must be int): %q",
856 to.Properties.Sdk_version)
857 }
858
859 if toApi > fromApi {
860 ctx.ModuleErrorf("links %q built against newer API version %q",
861 ctx.OtherModuleName(to), to.Properties.Sdk_version)
862 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700863 }
864
Colin Crossc99deeb2016-04-11 15:06:20 -0700865 ctx.VisitDirectDeps(func(m blueprint.Module) {
866 name := ctx.OtherModuleName(m)
867 tag := ctx.OtherModuleDependencyTag(m)
Colin Crossca860ac2016-01-04 14:34:37 -0800868
Colin Cross635c3b02016-05-18 15:37:25 -0700869 a, _ := m.(android.Module)
Colin Crossc99deeb2016-04-11 15:06:20 -0700870 if a == nil {
871 ctx.ModuleErrorf("module %q not an android module", name)
872 return
Colin Crossca860ac2016-01-04 14:34:37 -0800873 }
Colin Crossca860ac2016-01-04 14:34:37 -0800874
Dan Willemsena96ff642016-06-07 12:34:45 -0700875 cc, _ := m.(*Module)
876 if cc == nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700877 switch tag {
Colin Cross068e0fe2016-12-13 15:23:47 -0800878 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700879 case genSourceDepTag:
880 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
881 depPaths.GeneratedSources = append(depPaths.GeneratedSources,
882 genRule.GeneratedSourceFiles()...)
883 } else {
884 ctx.ModuleErrorf("module %q is not a gensrcs or genrule", name)
885 }
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700886 case genHeaderDepTag, genHeaderExportDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700887 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
888 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders,
889 genRule.GeneratedSourceFiles()...)
Colin Cross5ed99c62016-11-22 12:55:55 -0800890 flags := includeDirsToFlags(genRule.GeneratedHeaderDirs())
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700891 depPaths.Flags = append(depPaths.Flags, flags)
892 if tag == genHeaderExportDepTag {
893 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700894 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps,
895 genRule.GeneratedSourceFiles()...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700896 }
Dan Willemsenb40aab62016-04-20 14:21:14 -0700897 } else {
898 ctx.ModuleErrorf("module %q is not a genrule", name)
899 }
900 default:
Colin Crossc99deeb2016-04-11 15:06:20 -0700901 ctx.ModuleErrorf("depends on non-cc module %q", name)
Colin Crossca860ac2016-01-04 14:34:37 -0800902 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700903 return
904 }
905
906 if !a.Enabled() {
Colin Crossa8f5e9a2016-12-13 12:51:11 -0800907 if ctx.AConfig().AllowMissingDependencies() {
908 ctx.AddMissingDependencies([]string{name})
909 } else {
910 ctx.ModuleErrorf("depends on disabled module %q", name)
911 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700912 return
913 }
914
Colin Crossa1ad8d12016-06-01 17:09:44 -0700915 if a.Target().Os != ctx.Os() {
916 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), name)
917 return
918 }
919
920 if a.Target().Arch.ArchType != ctx.Arch().ArchType {
921 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700922 return
923 }
924
Colin Crossc99deeb2016-04-11 15:06:20 -0700925 if tag == reuseObjTag {
Colin Crossbba99042016-11-23 15:45:05 -0800926 if l, ok := cc.compiler.(libraryInterface); ok {
927 depPaths.Objs = depPaths.Objs.Append(l.reuseObjs())
928 return
929 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700930 }
931
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700932 if t, ok := tag.(dependencyTag); ok && t.library {
Dan Willemsena96ff642016-06-07 12:34:45 -0700933 if i, ok := cc.linker.(exportedFlagsProducer); ok {
Dan Willemsen76f08272016-07-09 00:14:08 -0700934 flags := i.exportedFlags()
Dan Willemsen847dcc72016-09-29 12:13:36 -0700935 deps := i.exportedFlagsDeps()
Dan Willemsen76f08272016-07-09 00:14:08 -0700936 depPaths.Flags = append(depPaths.Flags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700937 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders, deps...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700938
939 if t.reexportFlags {
Dan Willemsen76f08272016-07-09 00:14:08 -0700940 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700941 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps, deps...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700942 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700943 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700944
Dan Albert9e10cd42016-08-03 14:12:14 -0700945 checkLinkType(c, cc)
Colin Crossc99deeb2016-04-11 15:06:20 -0700946 }
947
Colin Cross26c34ed2016-09-30 17:10:16 -0700948 var ptr *android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -0700949 var depPtr *android.Paths
Colin Crossc99deeb2016-04-11 15:06:20 -0700950
Colin Cross26c34ed2016-09-30 17:10:16 -0700951 linkFile := cc.outputFile
952 depFile := android.OptionalPath{}
953
Colin Crossc99deeb2016-04-11 15:06:20 -0700954 switch tag {
Dan Albert914449f2016-06-17 16:45:24 -0700955 case ndkStubDepTag, sharedDepTag, sharedExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700956 ptr = &depPaths.SharedLibs
957 depPtr = &depPaths.SharedLibsDeps
958 depFile = cc.linker.(libraryInterface).toc()
Dan Albert914449f2016-06-17 16:45:24 -0700959 case lateSharedDepTag, ndkLateStubDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700960 ptr = &depPaths.LateSharedLibs
961 depPtr = &depPaths.LateSharedLibsDeps
962 depFile = cc.linker.(libraryInterface).toc()
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700963 case staticDepTag, staticExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700964 ptr = &depPaths.StaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -0700965 case lateStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700966 ptr = &depPaths.LateStaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -0700967 case wholeStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700968 ptr = &depPaths.WholeStaticLibs
Colin Crossb916a382016-07-29 17:28:03 -0700969 staticLib, ok := cc.linker.(libraryInterface)
970 if !ok || !staticLib.static() {
Dan Willemsena96ff642016-06-07 12:34:45 -0700971 ctx.ModuleErrorf("module %q not a static library", name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700972 return
973 }
974
975 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
976 postfix := " (required by " + ctx.OtherModuleName(m) + ")"
977 for i := range missingDeps {
978 missingDeps[i] += postfix
979 }
980 ctx.AddMissingDependencies(missingDeps)
981 }
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700982 depPaths.WholeStaticLibObjs = depPaths.WholeStaticLibObjs.Append(staticLib.objs())
Colin Cross5950f382016-12-13 12:50:57 -0800983 case headerDepTag:
984 // Nothing
Colin Crossc99deeb2016-04-11 15:06:20 -0700985 case objDepTag:
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700986 depPaths.Objs.objFiles = append(depPaths.Objs.objFiles, linkFile.Path())
Colin Crossc99deeb2016-04-11 15:06:20 -0700987 case crtBeginDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700988 depPaths.CrtBegin = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -0700989 case crtEndDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700990 depPaths.CrtEnd = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -0700991 }
992
Dan Willemsen581341d2017-02-09 16:16:31 -0800993 switch tag {
994 case staticDepTag, staticExportDepTag, lateStaticDepTag:
995 staticLib, ok := cc.linker.(libraryInterface)
996 if !ok || !staticLib.static() {
997 ctx.ModuleErrorf("module %q not a static library", name)
998 return
999 }
1000
1001 // When combining coverage files for shared libraries and executables, coverage files
1002 // in static libraries act as if they were whole static libraries.
1003 depPaths.StaticLibObjs.coverageFiles = append(depPaths.StaticLibObjs.coverageFiles,
1004 staticLib.objs().coverageFiles...)
1005 }
1006
Colin Cross26c34ed2016-09-30 17:10:16 -07001007 if ptr != nil {
Colin Crossce75d2c2016-10-06 16:12:58 -07001008 if !linkFile.Valid() {
1009 ctx.ModuleErrorf("module %q missing output file", name)
1010 return
1011 }
Colin Cross26c34ed2016-09-30 17:10:16 -07001012 *ptr = append(*ptr, linkFile.Path())
1013 }
1014
Colin Crossc99deeb2016-04-11 15:06:20 -07001015 if depPtr != nil {
Colin Cross26c34ed2016-09-30 17:10:16 -07001016 dep := depFile
1017 if !dep.Valid() {
1018 dep = linkFile
1019 }
1020 *depPtr = append(*depPtr, dep.Path())
Colin Crossca860ac2016-01-04 14:34:37 -08001021 }
1022 })
1023
1024 return depPaths
1025}
1026
1027func (c *Module) InstallInData() bool {
1028 if c.installer == nil {
1029 return false
1030 }
Vishwath Mohan1dd88392017-03-29 22:00:18 -07001031 return c.installer.inData()
1032}
1033
1034func (c *Module) InstallInSanitizerDir() bool {
1035 if c.installer == nil {
1036 return false
1037 }
1038 if c.sanitize != nil && c.sanitize.inSanitizerDir() {
Colin Cross94610402016-08-29 13:41:32 -07001039 return true
1040 }
Vishwath Mohan1dd88392017-03-29 22:00:18 -07001041 return c.installer.inSanitizerDir()
Colin Crossca860ac2016-01-04 14:34:37 -08001042}
1043
Dan Willemsen4aa75ca2016-09-28 16:18:03 -07001044func (c *Module) HostToolPath() android.OptionalPath {
1045 if c.installer == nil {
1046 return android.OptionalPath{}
1047 }
1048 return c.installer.hostToolPath()
1049}
1050
Colin Cross2ba19d92015-05-07 15:44:20 -07001051//
Colin Crosscfad1192015-11-02 16:43:11 -08001052// Defaults
1053//
Colin Crossca860ac2016-01-04 14:34:37 -08001054type Defaults struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001055 android.ModuleBase
1056 android.DefaultsModule
Colin Crosscfad1192015-11-02 16:43:11 -08001057}
1058
Colin Cross635c3b02016-05-18 15:37:25 -07001059func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -08001060}
1061
Colin Cross1e676be2016-10-12 14:38:15 -07001062func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
1063}
1064
Colin Crossca860ac2016-01-04 14:34:37 -08001065func defaultsFactory() (blueprint.Module, []interface{}) {
Colin Crosse1d764e2016-08-18 14:18:32 -07001066 return DefaultsFactory()
1067}
1068
1069func DefaultsFactory(props ...interface{}) (blueprint.Module, []interface{}) {
Colin Crossca860ac2016-01-04 14:34:37 -08001070 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -08001071
Colin Crosse1d764e2016-08-18 14:18:32 -07001072 props = append(props,
Colin Crossca860ac2016-01-04 14:34:37 -08001073 &BaseProperties{},
1074 &BaseCompilerProperties{},
1075 &BaseLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -07001076 &LibraryProperties{},
Colin Cross919281a2016-04-05 16:42:05 -07001077 &FlagExporterProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001078 &BinaryLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -07001079 &TestProperties{},
1080 &TestBinaryProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001081 &UnusedProperties{},
1082 &StlProperties{},
Colin Cross16b23492016-01-06 14:41:07 -08001083 &SanitizeProperties{},
Colin Cross665dce92016-04-28 14:50:03 -07001084 &StripProperties{},
Dan Willemsen7424d612016-09-01 13:45:39 -07001085 &InstallerProperties{},
Dan Willemsena03cf6d2016-09-26 15:45:04 -07001086 &TidyProperties{},
Dan Willemsen581341d2017-02-09 16:16:31 -08001087 &CoverageProperties{},
Colin Crosse1d764e2016-08-18 14:18:32 -07001088 )
Colin Crosscfad1192015-11-02 16:43:11 -08001089
Colin Crosse1d764e2016-08-18 14:18:32 -07001090 return android.InitDefaultsModule(module, module, props...)
Colin Crosscfad1192015-11-02 16:43:11 -08001091}
1092
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001093const (
1094 // coreMode is the variant used for framework-private libraries, or
1095 // SDK libraries. (which framework-private libraries can use)
1096 coreMode = "core"
1097
1098 // vendorMode is the variant used for /vendor code that compiles
1099 // against the VNDK.
1100 vendorMode = "vendor"
1101)
1102
1103func vendorMutator(mctx android.BottomUpMutatorContext) {
1104 if mctx.Os() != android.Android {
1105 return
1106 }
1107
1108 m, ok := mctx.Module().(*Module)
1109 if !ok {
1110 return
1111 }
1112
1113 // Sanity check
1114 if Bool(m.Properties.Vendor_available) && mctx.Vendor() {
1115 mctx.PropertyErrorf("vendor_available",
1116 "doesn't make sense at the same time as `vendor: true` or `proprietary: true`")
1117 return
1118 }
1119
1120 if !mctx.DeviceConfig().CompileVndk() {
1121 // If the device isn't compiling against the VNDK, we always
1122 // use the core mode.
1123 mctx.CreateVariations(coreMode)
1124 } else if _, ok := m.linker.(*llndkStubDecorator); ok {
1125 // LL-NDK stubs only exist in the vendor variant, since the
1126 // real libraries will be used in the core variant.
1127 mctx.CreateVariations(vendorMode)
1128 } else if Bool(m.Properties.Vendor_available) {
1129 // This will be available in both /system and /vendor
1130 mod := mctx.CreateVariations(coreMode, vendorMode)
1131 mod[1].(*Module).Properties.UseVndk = true
1132 } else if mctx.Vendor() && m.Properties.Sdk_version == "" {
1133 // This will be available in /vendor only
1134 mod := mctx.CreateVariations(vendorMode)
1135 mod[0].(*Module).Properties.UseVndk = true
1136 } else {
1137 // This is either in /system (or similar: /data), or is a
1138 // modules built with the NDK. Modules built with the NDK
1139 // will be restricted using the existing link type checks.
1140 mctx.CreateVariations(coreMode)
1141 }
1142}
1143
Colin Cross74d1ec02015-04-28 13:30:13 -07001144// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
1145// modifies the slice contents in place, and returns a subslice of the original slice
1146func lastUniqueElements(list []string) []string {
1147 totalSkip := 0
1148 for i := len(list) - 1; i >= totalSkip; i-- {
1149 skip := 0
1150 for j := i - 1; j >= totalSkip; j-- {
1151 if list[i] == list[j] {
1152 skip++
1153 } else {
1154 list[j+skip] = list[j]
1155 }
1156 }
1157 totalSkip += skip
1158 }
1159 return list[totalSkip:]
1160}
Colin Cross06a931b2015-10-28 17:23:31 -07001161
1162var Bool = proptools.Bool