blob: 28354a8f59dbcfbda75d1a07c7406121800997bd [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()
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -080052 ctx.TopDown("vndk_deps", sabiDepsMutator)
Colin Cross1e676be2016-10-12 14:38:15 -070053 })
Colin Crossb98c8b02016-07-29 13:44:28 -070054
55 pctx.Import("android/soong/cc/config")
Colin Cross463a90e2015-06-17 14:20:06 -070056}
57
Colin Crossca860ac2016-01-04 14:34:37 -080058type Deps struct {
59 SharedLibs, LateSharedLibs []string
60 StaticLibs, LateStaticLibs, WholeStaticLibs []string
Colin Cross5950f382016-12-13 12:50:57 -080061 HeaderLibs []string
Colin Crossc472d572015-03-17 15:06:21 -070062
Colin Cross5950f382016-12-13 12:50:57 -080063 ReexportSharedLibHeaders, ReexportStaticLibHeaders, ReexportHeaderLibHeaders []string
Dan Willemsen490a8dc2016-06-06 18:22:19 -070064
Colin Cross81413472016-04-11 14:37:39 -070065 ObjFiles []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -070066
Dan Willemsenb40aab62016-04-20 14:21:14 -070067 GeneratedSources []string
68 GeneratedHeaders []string
69
Dan Willemsenb3454ab2016-09-28 17:34:58 -070070 ReexportGeneratedHeaders []string
71
Colin Cross97ba0732015-03-23 17:50:24 -070072 CrtBegin, CrtEnd string
Colin Crossc472d572015-03-17 15:06:21 -070073}
74
Colin Crossca860ac2016-01-04 14:34:37 -080075type PathDeps struct {
Colin Cross26c34ed2016-09-30 17:10:16 -070076 // Paths to .so files
77 SharedLibs, LateSharedLibs android.Paths
78 // Paths to the dependencies to use for .so files (.so.toc files)
79 SharedLibsDeps, LateSharedLibsDeps android.Paths
80 // Paths to .a files
Colin Cross635c3b02016-05-18 15:37:25 -070081 StaticLibs, LateStaticLibs, WholeStaticLibs android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -070082
Colin Cross26c34ed2016-09-30 17:10:16 -070083 // Paths to .o files
Dan Willemsen5cb580f2016-09-26 17:33:01 -070084 Objs Objects
Dan Willemsen581341d2017-02-09 16:16:31 -080085 StaticLibObjs Objects
Dan Willemsen5cb580f2016-09-26 17:33:01 -070086 WholeStaticLibObjs Objects
Dan Willemsen34cc69e2015-09-23 15:26:20 -070087
Colin Cross26c34ed2016-09-30 17:10:16 -070088 // Paths to generated source files
Colin Cross635c3b02016-05-18 15:37:25 -070089 GeneratedSources android.Paths
90 GeneratedHeaders android.Paths
Dan Willemsenb40aab62016-04-20 14:21:14 -070091
Dan Willemsen76f08272016-07-09 00:14:08 -070092 Flags, ReexportedFlags []string
Dan Willemsen847dcc72016-09-29 12:13:36 -070093 ReexportedFlagsDeps android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -070094
Colin Cross26c34ed2016-09-30 17:10:16 -070095 // Paths to crt*.o files
Colin Cross635c3b02016-05-18 15:37:25 -070096 CrtBegin, CrtEnd android.OptionalPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -070097}
98
Colin Crossca860ac2016-01-04 14:34:37 -080099type Flags struct {
Colin Cross28344522015-04-22 13:07:53 -0700100 GlobalFlags []string // Flags that apply to C, C++, and assembly source files
Vishwath Mohan83d9f712017-03-16 11:01:23 -0700101 ArFlags []string // Flags that apply to ar
Colin Cross28344522015-04-22 13:07:53 -0700102 AsFlags []string // Flags that apply to assembly source files
103 CFlags []string // Flags that apply to C and C++ source files
104 ConlyFlags []string // Flags that apply to C source files
105 CppFlags []string // Flags that apply to C++ source files
106 YaccFlags []string // Flags that apply to Yacc source files
Colin Cross0c461f12016-10-20 16:11:43 -0700107 protoFlags []string // Flags that apply to proto source files
Dan Willemsene1240db2016-11-03 14:28:51 -0700108 aidlFlags []string // Flags that apply to aidl source files
Colin Cross2a252be2017-05-01 17:37:24 -0700109 rsFlags []string // Flags that apply to renderscript source files
Colin Cross28344522015-04-22 13:07:53 -0700110 LdFlags []string // Flags that apply to linker command lines
Colin Cross16b23492016-01-06 14:41:07 -0800111 libFlags []string // Flags to add libraries early to the link order
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700112 TidyFlags []string // Flags that apply to clang-tidy
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800113 SAbiFlags []string // Flags that apply to header-abi-dumper
Colin Cross91e90042016-12-02 17:13:24 -0800114 YasmFlags []string // Flags that apply to yasm assembly source files
Colin Cross28344522015-04-22 13:07:53 -0700115
Colin Crossc3199482017-03-30 15:03:04 -0700116 // Global include flags that apply to C, C++, and assembly source files
117 // These must be after any module include flags, which will be in GlobalFlags.
118 SystemIncludeFlags []string
119
Colin Crossb98c8b02016-07-29 13:44:28 -0700120 Toolchain config.Toolchain
Colin Cross28344522015-04-22 13:07:53 -0700121 Clang bool
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700122 Tidy bool
Dan Willemsen581341d2017-02-09 16:16:31 -0800123 Coverage bool
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800124 SAbiDump bool
Colin Crossca860ac2016-01-04 14:34:37 -0800125
126 RequiredInstructionSet string
Colin Cross16b23492016-01-06 14:41:07 -0800127 DynamicLinker string
128
Colin Cross635c3b02016-05-18 15:37:25 -0700129 CFlagsDeps android.Paths // Files depended on by compiler flags
Colin Cross18c0c5a2016-12-01 14:45:23 -0800130
131 GroupStaticLibs bool
Colin Crossc472d572015-03-17 15:06:21 -0700132}
133
Colin Cross81413472016-04-11 14:37:39 -0700134type ObjectLinkerProperties struct {
135 // names of other cc_object modules to link into this module using partial linking
136 Objs []string `android:"arch_variant"`
137}
138
Colin Crossca860ac2016-01-04 14:34:37 -0800139// Properties used to compile all C or C++ modules
140type BaseProperties struct {
141 // compile module with clang instead of gcc
142 Clang *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700143
144 // Minimum sdk version supported when compiling against the ndk
145 Sdk_version string
146
Colin Crossca860ac2016-01-04 14:34:37 -0800147 // don't insert default compiler flags into asflags, cflags,
148 // cppflags, conlyflags, ldflags, or include_dirs
149 No_default_compiler_flags *bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700150
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700151 // whether this module should be allowed to install onto /vendor as
152 // well as /system. The two variants will be built separately, one
153 // like normal, and the other limited to the set of libraries and
154 // headers that are exposed to /vendor modules.
155 //
156 // The vendor variant may be used with a different (newer) /system,
157 // so it shouldn't have any unversioned runtime dependencies, or
158 // make assumptions about the system that may not be true in the
159 // future.
160 //
161 // Nothing happens if BOARD_VNDK_VERSION isn't set in the BoardConfig.mk
162 Vendor_available *bool
163
Colin Crossc99deeb2016-04-11 15:06:20 -0700164 AndroidMkSharedLibs []string `blueprint:"mutated"`
Colin Crossbc6fb162016-05-24 15:39:04 -0700165 HideFromMake bool `blueprint:"mutated"`
Colin Crossb0f28952016-09-19 16:46:53 -0700166 PreventInstall bool `blueprint:"mutated"`
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700167
168 UseVndk bool `blueprint:"mutated"`
Colin Crossca860ac2016-01-04 14:34:37 -0800169}
170
Colin Crossca860ac2016-01-04 14:34:37 -0800171type UnusedProperties struct {
Dan Willemsen581341d2017-02-09 16:16:31 -0800172 Tags []string
Colin Crosscfad1192015-11-02 16:43:11 -0800173}
174
Colin Crossca860ac2016-01-04 14:34:37 -0800175type ModuleContextIntf interface {
Colin Crossca860ac2016-01-04 14:34:37 -0800176 static() bool
177 staticBinary() bool
178 clang() bool
Colin Crossb98c8b02016-07-29 13:44:28 -0700179 toolchain() config.Toolchain
Colin Crossca860ac2016-01-04 14:34:37 -0800180 noDefaultCompilerFlags() bool
181 sdk() bool
182 sdkVersion() string
Dan Willemsend2ede872016-11-18 14:54:24 -0800183 vndk() bool
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800184 createVndkSourceAbiDump() bool
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700185 selectedStl() string
Colin Crossce75d2c2016-10-06 16:12:58 -0700186 baseModuleName() string
Colin Crossca860ac2016-01-04 14:34:37 -0800187}
188
189type ModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700190 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800191 ModuleContextIntf
192}
193
194type BaseModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700195 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800196 ModuleContextIntf
197}
198
Colin Cross37047f12016-12-13 17:06:13 -0800199type DepsContext interface {
200 android.BottomUpMutatorContext
201 ModuleContextIntf
202}
203
Colin Crossca860ac2016-01-04 14:34:37 -0800204type feature interface {
205 begin(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800206 deps(ctx DepsContext, deps Deps) Deps
Colin Crossca860ac2016-01-04 14:34:37 -0800207 flags(ctx ModuleContext, flags Flags) Flags
208 props() []interface{}
209}
210
211type compiler interface {
Colin Cross42742b82016-08-01 13:20:05 -0700212 compilerInit(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800213 compilerDeps(ctx DepsContext, deps Deps) Deps
Colin Cross42742b82016-08-01 13:20:05 -0700214 compilerFlags(ctx ModuleContext, flags Flags) Flags
215 compilerProps() []interface{}
216
Colin Cross76fada02016-07-27 10:31:13 -0700217 appendCflags([]string)
218 appendAsflags([]string)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700219 compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects
Colin Crossca860ac2016-01-04 14:34:37 -0800220}
221
222type linker interface {
Colin Cross42742b82016-08-01 13:20:05 -0700223 linkerInit(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800224 linkerDeps(ctx DepsContext, deps Deps) Deps
Colin Cross42742b82016-08-01 13:20:05 -0700225 linkerFlags(ctx ModuleContext, flags Flags) Flags
226 linkerProps() []interface{}
227
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700228 link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path
Colin Cross76fada02016-07-27 10:31:13 -0700229 appendLdflags([]string)
Colin Crossca860ac2016-01-04 14:34:37 -0800230}
231
232type installer interface {
Colin Cross42742b82016-08-01 13:20:05 -0700233 installerProps() []interface{}
Colin Cross635c3b02016-05-18 15:37:25 -0700234 install(ctx ModuleContext, path android.Path)
Colin Crossca860ac2016-01-04 14:34:37 -0800235 inData() bool
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700236 inSanitizerDir() bool
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700237 hostToolPath() android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800238}
239
Colin Crossc99deeb2016-04-11 15:06:20 -0700240type dependencyTag struct {
241 blueprint.BaseDependencyTag
242 name string
243 library bool
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700244
245 reexportFlags bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700246}
247
248var (
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700249 sharedDepTag = dependencyTag{name: "shared", library: true}
250 sharedExportDepTag = dependencyTag{name: "shared", library: true, reexportFlags: true}
251 lateSharedDepTag = dependencyTag{name: "late shared", library: true}
252 staticDepTag = dependencyTag{name: "static", library: true}
253 staticExportDepTag = dependencyTag{name: "static", library: true, reexportFlags: true}
254 lateStaticDepTag = dependencyTag{name: "late static", library: true}
255 wholeStaticDepTag = dependencyTag{name: "whole static", library: true, reexportFlags: true}
Colin Cross32ec36c2016-12-15 07:39:51 -0800256 headerDepTag = dependencyTag{name: "header", library: true}
257 headerExportDepTag = dependencyTag{name: "header", library: true, reexportFlags: true}
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700258 genSourceDepTag = dependencyTag{name: "gen source"}
259 genHeaderDepTag = dependencyTag{name: "gen header"}
260 genHeaderExportDepTag = dependencyTag{name: "gen header", reexportFlags: true}
261 objDepTag = dependencyTag{name: "obj"}
262 crtBeginDepTag = dependencyTag{name: "crtbegin"}
263 crtEndDepTag = dependencyTag{name: "crtend"}
264 reuseObjTag = dependencyTag{name: "reuse objects"}
265 ndkStubDepTag = dependencyTag{name: "ndk stub", library: true}
266 ndkLateStubDepTag = dependencyTag{name: "ndk late stub", library: true}
Colin Crossc99deeb2016-04-11 15:06:20 -0700267)
268
Colin Crossca860ac2016-01-04 14:34:37 -0800269// Module contains the properties and members used by all C/C++ module types, and implements
270// the blueprint.Module interface. It delegates to compiler, linker, and installer interfaces
271// to construct the output file. Behavior can be customized with a Customizer interface
272type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700273 android.ModuleBase
274 android.DefaultableModule
Colin Crossc472d572015-03-17 15:06:21 -0700275
Colin Crossca860ac2016-01-04 14:34:37 -0800276 Properties BaseProperties
277 unused UnusedProperties
Colin Crossfa138792015-04-24 17:31:52 -0700278
Colin Crossca860ac2016-01-04 14:34:37 -0800279 // initialize before calling Init
Colin Cross635c3b02016-05-18 15:37:25 -0700280 hod android.HostOrDeviceSupported
281 multilib android.Multilib
Colin Crossc472d572015-03-17 15:06:21 -0700282
Colin Crossca860ac2016-01-04 14:34:37 -0800283 // delegates, initialize before calling Init
Colin Crossb4ce0ec2016-09-13 13:41:39 -0700284 features []feature
285 compiler compiler
286 linker linker
287 installer installer
288 stl *stl
289 sanitize *sanitize
Dan Willemsen581341d2017-02-09 16:16:31 -0800290 coverage *coverage
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800291 sabi *sabi
Colin Cross16b23492016-01-06 14:41:07 -0800292
293 androidMkSharedLibDeps []string
Colin Cross74d1ec02015-04-28 13:30:13 -0700294
Colin Cross635c3b02016-05-18 15:37:25 -0700295 outputFile android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800296
Colin Crossb98c8b02016-07-29 13:44:28 -0700297 cachedToolchain config.Toolchain
Colin Crossb916a382016-07-29 17:28:03 -0700298
299 subAndroidMkOnce map[subAndroidMkProvider]bool
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800300
301 // Flags used to compile this module
302 flags Flags
Colin Crossc472d572015-03-17 15:06:21 -0700303}
304
Colin Crossca860ac2016-01-04 14:34:37 -0800305func (c *Module) Init() (blueprint.Module, []interface{}) {
306 props := []interface{}{&c.Properties, &c.unused}
Colin Crossca860ac2016-01-04 14:34:37 -0800307 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700308 props = append(props, c.compiler.compilerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800309 }
310 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700311 props = append(props, c.linker.linkerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800312 }
313 if c.installer != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700314 props = append(props, c.installer.installerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800315 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700316 if c.stl != nil {
317 props = append(props, c.stl.props()...)
318 }
Colin Cross16b23492016-01-06 14:41:07 -0800319 if c.sanitize != nil {
320 props = append(props, c.sanitize.props()...)
321 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800322 if c.coverage != nil {
323 props = append(props, c.coverage.props()...)
324 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800325 if c.sabi != nil {
326 props = append(props, c.sabi.props()...)
327 }
Colin Crossca860ac2016-01-04 14:34:37 -0800328 for _, feature := range c.features {
329 props = append(props, feature.props()...)
330 }
Colin Crossc472d572015-03-17 15:06:21 -0700331
Colin Cross635c3b02016-05-18 15:37:25 -0700332 _, props = android.InitAndroidArchModule(c, c.hod, c.multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700333
Colin Cross635c3b02016-05-18 15:37:25 -0700334 return android.InitDefaultableModule(c, c, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700335}
336
Colin Crossb916a382016-07-29 17:28:03 -0700337// Returns true for dependency roots (binaries)
338// TODO(ccross): also handle dlopenable libraries
339func (c *Module) isDependencyRoot() bool {
340 if root, ok := c.linker.(interface {
341 isDependencyRoot() bool
342 }); ok {
343 return root.isDependencyRoot()
344 }
345 return false
346}
347
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700348func (c *Module) vndk() bool {
349 return c.Properties.UseVndk
350}
351
Colin Crossca860ac2016-01-04 14:34:37 -0800352type baseModuleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700353 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800354 moduleContextImpl
355}
356
Colin Cross37047f12016-12-13 17:06:13 -0800357type depsContext struct {
358 android.BottomUpMutatorContext
359 moduleContextImpl
360}
361
Colin Crossca860ac2016-01-04 14:34:37 -0800362type moduleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700363 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800364 moduleContextImpl
365}
366
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700367// Vendor returns true for vendor modules so that they get installed onto the
368// correct partition
369func (ctx *moduleContext) Vendor() bool {
370 return ctx.ModuleContext.Vendor() || ctx.moduleContextImpl.mod.Properties.UseVndk
371}
372
Colin Crossca860ac2016-01-04 14:34:37 -0800373type moduleContextImpl struct {
374 mod *Module
375 ctx BaseModuleContext
376}
377
Colin Crossca860ac2016-01-04 14:34:37 -0800378func (ctx *moduleContextImpl) clang() bool {
379 return ctx.mod.clang(ctx.ctx)
380}
381
Colin Crossb98c8b02016-07-29 13:44:28 -0700382func (ctx *moduleContextImpl) toolchain() config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800383 return ctx.mod.toolchain(ctx.ctx)
384}
385
386func (ctx *moduleContextImpl) static() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700387 if static, ok := ctx.mod.linker.(interface {
388 static() bool
389 }); ok {
390 return static.static()
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) staticBinary() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700396 if static, ok := ctx.mod.linker.(interface {
397 staticBinary() bool
398 }); ok {
399 return static.staticBinary()
Colin Crossca860ac2016-01-04 14:34:37 -0800400 }
Colin Crossb916a382016-07-29 17:28:03 -0700401 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800402}
403
404func (ctx *moduleContextImpl) noDefaultCompilerFlags() bool {
405 return Bool(ctx.mod.Properties.No_default_compiler_flags)
406}
407
408func (ctx *moduleContextImpl) sdk() bool {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700409 if ctx.ctx.Device() && !ctx.vndk() {
Dan Willemsena96ff642016-06-07 12:34:45 -0700410 return ctx.mod.Properties.Sdk_version != ""
411 }
412 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800413}
414
415func (ctx *moduleContextImpl) sdkVersion() string {
Dan Willemsena96ff642016-06-07 12:34:45 -0700416 if ctx.ctx.Device() {
Dan Willemsen11b26142017-03-19 18:30:37 -0700417 if ctx.vndk() {
418 return "current"
Dan Willemsend2ede872016-11-18 14:54:24 -0800419 } else {
420 return ctx.mod.Properties.Sdk_version
421 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700422 }
423 return ""
Colin Crossca860ac2016-01-04 14:34:37 -0800424}
425
Dan Willemsend2ede872016-11-18 14:54:24 -0800426func (ctx *moduleContextImpl) vndk() bool {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700427 return ctx.mod.vndk()
Dan Willemsend2ede872016-11-18 14:54:24 -0800428}
429
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800430// Create source abi dumps if the module belongs to the list of VndkLibraries.
431func (ctx *moduleContextImpl) createVndkSourceAbiDump() bool {
432 return ctx.ctx.Device() && (inList(ctx.baseModuleName(), config.LLndkLibraries())) ||
433 (inList(ctx.baseModuleName(), config.VndkLibraries()))
434}
435
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700436func (ctx *moduleContextImpl) selectedStl() string {
437 if stl := ctx.mod.stl; stl != nil {
438 return stl.Properties.SelectedStl
439 }
440 return ""
441}
442
Colin Crossce75d2c2016-10-06 16:12:58 -0700443func (ctx *moduleContextImpl) baseModuleName() string {
444 return ctx.mod.ModuleBase.BaseModuleName()
445}
446
Colin Cross635c3b02016-05-18 15:37:25 -0700447func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800448 return &Module{
449 hod: hod,
450 multilib: multilib,
451 }
452}
453
Colin Cross635c3b02016-05-18 15:37:25 -0700454func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800455 module := newBaseModule(hod, multilib)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700456 module.features = []feature{
457 &tidyFeature{},
458 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700459 module.stl = &stl{}
Colin Cross16b23492016-01-06 14:41:07 -0800460 module.sanitize = &sanitize{}
Dan Willemsen581341d2017-02-09 16:16:31 -0800461 module.coverage = &coverage{}
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800462 module.sabi = &sabi{}
Colin Crossca860ac2016-01-04 14:34:37 -0800463 return module
464}
465
Colin Crossce75d2c2016-10-06 16:12:58 -0700466func (c *Module) Prebuilt() *android.Prebuilt {
467 if p, ok := c.linker.(prebuiltLinkerInterface); ok {
468 return p.prebuilt()
469 }
470 return nil
471}
472
473func (c *Module) Name() string {
474 name := c.ModuleBase.Name()
Dan Willemsen01a90592017-04-07 15:21:13 -0700475 if p, ok := c.linker.(interface {
476 Name(string) string
477 }); ok {
Colin Crossce75d2c2016-10-06 16:12:58 -0700478 name = p.Name(name)
479 }
480 return name
481}
482
Colin Cross635c3b02016-05-18 15:37:25 -0700483func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800484 ctx := &moduleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700485 ModuleContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800486 moduleContextImpl: moduleContextImpl{
487 mod: c,
488 },
489 }
490 ctx.ctx = ctx
491
492 flags := Flags{
493 Toolchain: c.toolchain(ctx),
494 Clang: c.clang(ctx),
495 }
Colin Crossca860ac2016-01-04 14:34:37 -0800496 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700497 flags = c.compiler.compilerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800498 }
499 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700500 flags = c.linker.linkerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800501 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700502 if c.stl != nil {
503 flags = c.stl.flags(ctx, flags)
504 }
Colin Cross16b23492016-01-06 14:41:07 -0800505 if c.sanitize != nil {
506 flags = c.sanitize.flags(ctx, flags)
507 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800508 if c.coverage != nil {
509 flags = c.coverage.flags(ctx, flags)
510 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800511 if c.sabi != nil {
512 flags = c.sabi.flags(ctx, flags)
513 }
Colin Crossca860ac2016-01-04 14:34:37 -0800514 for _, feature := range c.features {
515 flags = feature.flags(ctx, flags)
516 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800517 if ctx.Failed() {
518 return
519 }
520
Colin Crossb98c8b02016-07-29 13:44:28 -0700521 flags.CFlags, _ = filterList(flags.CFlags, config.IllegalFlags)
522 flags.CppFlags, _ = filterList(flags.CppFlags, config.IllegalFlags)
523 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, config.IllegalFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800524
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800525 deps := c.depsToPaths(ctx)
526 if ctx.Failed() {
527 return
528 }
529 flags.GlobalFlags = append(flags.GlobalFlags, deps.Flags...)
530 c.flags = flags
531
Colin Crossca860ac2016-01-04 14:34:37 -0800532 // Optimization to reduce size of build.ninja
533 // Replace the long list of flags for each file with a module-local variable
534 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
535 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
536 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
537 flags.CFlags = []string{"$cflags"}
538 flags.CppFlags = []string{"$cppflags"}
539 flags.AsFlags = []string{"$asflags"}
540
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700541 var objs Objects
Colin Crossca860ac2016-01-04 14:34:37 -0800542 if c.compiler != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700543 objs = c.compiler.compile(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -0800544 if ctx.Failed() {
545 return
546 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800547 }
548
Colin Crossca860ac2016-01-04 14:34:37 -0800549 if c.linker != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700550 outputFile := c.linker.link(ctx, flags, deps, objs)
Colin Crossca860ac2016-01-04 14:34:37 -0800551 if ctx.Failed() {
552 return
553 }
Colin Cross635c3b02016-05-18 15:37:25 -0700554 c.outputFile = android.OptionalPathForPath(outputFile)
Colin Crossce75d2c2016-10-06 16:12:58 -0700555 }
Colin Cross5049f022015-03-18 13:28:46 -0700556
Colin Crossce75d2c2016-10-06 16:12:58 -0700557 if c.installer != nil && !c.Properties.PreventInstall && c.outputFile.Valid() {
558 c.installer.install(ctx, c.outputFile.Path())
559 if ctx.Failed() {
560 return
Colin Crossca860ac2016-01-04 14:34:37 -0800561 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700562 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800563}
564
Colin Crossb98c8b02016-07-29 13:44:28 -0700565func (c *Module) toolchain(ctx BaseModuleContext) config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800566 if c.cachedToolchain == nil {
Colin Crossb98c8b02016-07-29 13:44:28 -0700567 c.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
Colin Cross3f40fa42015-01-30 17:27:36 -0800568 }
Colin Crossca860ac2016-01-04 14:34:37 -0800569 return c.cachedToolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800570}
571
Colin Crossca860ac2016-01-04 14:34:37 -0800572func (c *Module) begin(ctx BaseModuleContext) {
573 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700574 c.compiler.compilerInit(ctx)
Colin Cross21b9a242015-03-24 14:15:58 -0700575 }
Colin Crossca860ac2016-01-04 14:34:37 -0800576 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700577 c.linker.linkerInit(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800578 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700579 if c.stl != nil {
580 c.stl.begin(ctx)
581 }
Colin Cross16b23492016-01-06 14:41:07 -0800582 if c.sanitize != nil {
583 c.sanitize.begin(ctx)
584 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800585 if c.coverage != nil {
586 c.coverage.begin(ctx)
587 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800588 if c.sabi != nil {
589 c.sabi.begin(ctx)
590 }
Colin Crossca860ac2016-01-04 14:34:37 -0800591 for _, feature := range c.features {
592 feature.begin(ctx)
593 }
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700594 if ctx.sdk() {
595 version, err := normalizeNdkApiLevel(ctx.sdkVersion(), ctx.Arch())
596 if err != nil {
597 ctx.PropertyErrorf("sdk_version", err.Error())
598 }
Dan Albert90f7a4d2016-11-08 14:34:24 -0800599 c.Properties.Sdk_version = version
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700600 }
Colin Crossca860ac2016-01-04 14:34:37 -0800601}
602
Colin Cross37047f12016-12-13 17:06:13 -0800603func (c *Module) deps(ctx DepsContext) Deps {
Colin Crossc99deeb2016-04-11 15:06:20 -0700604 deps := Deps{}
605
606 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700607 deps = c.compiler.compilerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700608 }
609 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700610 deps = c.linker.linkerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700611 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700612 if c.stl != nil {
613 deps = c.stl.deps(ctx, deps)
614 }
Colin Cross16b23492016-01-06 14:41:07 -0800615 if c.sanitize != nil {
616 deps = c.sanitize.deps(ctx, deps)
617 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800618 if c.coverage != nil {
619 deps = c.coverage.deps(ctx, deps)
620 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800621 if c.sabi != nil {
622 deps = c.sabi.deps(ctx, deps)
623 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700624 for _, feature := range c.features {
625 deps = feature.deps(ctx, deps)
626 }
627
628 deps.WholeStaticLibs = lastUniqueElements(deps.WholeStaticLibs)
629 deps.StaticLibs = lastUniqueElements(deps.StaticLibs)
630 deps.LateStaticLibs = lastUniqueElements(deps.LateStaticLibs)
631 deps.SharedLibs = lastUniqueElements(deps.SharedLibs)
632 deps.LateSharedLibs = lastUniqueElements(deps.LateSharedLibs)
Colin Cross5950f382016-12-13 12:50:57 -0800633 deps.HeaderLibs = lastUniqueElements(deps.HeaderLibs)
Colin Crossc99deeb2016-04-11 15:06:20 -0700634
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700635 for _, lib := range deps.ReexportSharedLibHeaders {
636 if !inList(lib, deps.SharedLibs) {
637 ctx.PropertyErrorf("export_shared_lib_headers", "Shared library not in shared_libs: '%s'", lib)
638 }
639 }
640
641 for _, lib := range deps.ReexportStaticLibHeaders {
642 if !inList(lib, deps.StaticLibs) {
643 ctx.PropertyErrorf("export_static_lib_headers", "Static library not in static_libs: '%s'", lib)
644 }
645 }
646
Colin Cross5950f382016-12-13 12:50:57 -0800647 for _, lib := range deps.ReexportHeaderLibHeaders {
648 if !inList(lib, deps.HeaderLibs) {
649 ctx.PropertyErrorf("export_header_lib_headers", "Header library not in header_libs: '%s'", lib)
650 }
651 }
652
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700653 for _, gen := range deps.ReexportGeneratedHeaders {
654 if !inList(gen, deps.GeneratedHeaders) {
655 ctx.PropertyErrorf("export_generated_headers", "Generated header module not in generated_headers: '%s'", gen)
656 }
657 }
658
Colin Crossc99deeb2016-04-11 15:06:20 -0700659 return deps
660}
661
Dan Albert7e9d2952016-08-04 13:02:36 -0700662func (c *Module) beginMutator(actx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800663 ctx := &baseModuleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700664 BaseContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800665 moduleContextImpl: moduleContextImpl{
666 mod: c,
667 },
668 }
669 ctx.ctx = ctx
670
Colin Crossca860ac2016-01-04 14:34:37 -0800671 c.begin(ctx)
Dan Albert7e9d2952016-08-04 13:02:36 -0700672}
673
Colin Cross1e676be2016-10-12 14:38:15 -0700674func (c *Module) DepsMutator(actx android.BottomUpMutatorContext) {
675 if !c.Enabled() {
676 return
677 }
678
Colin Cross37047f12016-12-13 17:06:13 -0800679 ctx := &depsContext{
680 BottomUpMutatorContext: actx,
Dan Albert7e9d2952016-08-04 13:02:36 -0700681 moduleContextImpl: moduleContextImpl{
682 mod: c,
683 },
684 }
685 ctx.ctx = ctx
Colin Crossca860ac2016-01-04 14:34:37 -0800686
Colin Crossc99deeb2016-04-11 15:06:20 -0700687 deps := c.deps(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800688
Colin Crossb5bc4b42016-07-11 16:11:59 -0700689 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.SharedLibs...)
690 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.LateSharedLibs...)
Dan Willemsen72d39932016-07-08 23:23:48 -0700691
Dan Albert914449f2016-06-17 16:45:24 -0700692 variantNdkLibs := []string{}
693 variantLateNdkLibs := []string{}
Dan Willemsenb916b802017-03-19 13:44:32 -0700694 if ctx.Os() == android.Android {
Dan Albert914449f2016-06-17 16:45:24 -0700695 version := ctx.sdkVersion()
Dan Willemsen72d39932016-07-08 23:23:48 -0700696
Dan Albert914449f2016-06-17 16:45:24 -0700697 // Rewrites the names of shared libraries into the names of the NDK
698 // libraries where appropriate. This returns two slices.
699 //
700 // The first is a list of non-variant shared libraries (either rewritten
701 // NDK libraries to the modules in prebuilts/ndk, or not rewritten
702 // because they are not NDK libraries).
703 //
704 // The second is a list of ndk_library modules. These need to be
705 // separated because they are a variation dependency and must be added
706 // in a different manner.
707 rewriteNdkLibs := func(list []string) ([]string, []string) {
708 variantLibs := []string{}
709 nonvariantLibs := []string{}
710 for _, entry := range list {
Dan Willemsenb916b802017-03-19 13:44:32 -0700711 if ctx.sdk() && inList(entry, ndkPrebuiltSharedLibraries) {
Dan Albert914449f2016-06-17 16:45:24 -0700712 if !inList(entry, ndkMigratedLibs) {
713 nonvariantLibs = append(nonvariantLibs, entry+".ndk."+version)
714 } else {
715 variantLibs = append(variantLibs, entry+ndkLibrarySuffix)
716 }
Dan Willemsenb916b802017-03-19 13:44:32 -0700717 } else if ctx.vndk() && inList(entry, config.LLndkLibraries()) {
718 nonvariantLibs = append(nonvariantLibs, entry+llndkLibrarySuffix)
Dan Albert914449f2016-06-17 16:45:24 -0700719 } else {
Dan Willemsen7cbf5f82017-03-28 00:08:30 -0700720 nonvariantLibs = append(nonvariantLibs, entry)
Dan Willemsen72d39932016-07-08 23:23:48 -0700721 }
722 }
Dan Albert914449f2016-06-17 16:45:24 -0700723 return nonvariantLibs, variantLibs
Dan Willemsen72d39932016-07-08 23:23:48 -0700724 }
725
Dan Albert914449f2016-06-17 16:45:24 -0700726 deps.SharedLibs, variantNdkLibs = rewriteNdkLibs(deps.SharedLibs)
727 deps.LateSharedLibs, variantLateNdkLibs = rewriteNdkLibs(deps.LateSharedLibs)
Dan Willemsen72d39932016-07-08 23:23:48 -0700728 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700729
Colin Cross32ec36c2016-12-15 07:39:51 -0800730 for _, lib := range deps.HeaderLibs {
731 depTag := headerDepTag
732 if inList(lib, deps.ReexportHeaderLibHeaders) {
733 depTag = headerExportDepTag
734 }
735 actx.AddVariationDependencies(nil, depTag, lib)
736 }
Colin Cross5950f382016-12-13 12:50:57 -0800737
Colin Crossc99deeb2016-04-11 15:06:20 -0700738 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, wholeStaticDepTag,
739 deps.WholeStaticLibs...)
740
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700741 for _, lib := range deps.StaticLibs {
742 depTag := staticDepTag
743 if inList(lib, deps.ReexportStaticLibHeaders) {
744 depTag = staticExportDepTag
745 }
Colin Cross15a0d462016-07-14 14:49:58 -0700746 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700747 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700748
749 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, lateStaticDepTag,
750 deps.LateStaticLibs...)
751
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700752 for _, lib := range deps.SharedLibs {
753 depTag := sharedDepTag
754 if inList(lib, deps.ReexportSharedLibHeaders) {
755 depTag = sharedExportDepTag
756 }
Colin Cross15a0d462016-07-14 14:49:58 -0700757 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700758 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700759
760 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, lateSharedDepTag,
761 deps.LateSharedLibs...)
762
Colin Cross68861832016-07-08 10:41:41 -0700763 actx.AddDependency(c, genSourceDepTag, deps.GeneratedSources...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700764
765 for _, gen := range deps.GeneratedHeaders {
766 depTag := genHeaderDepTag
767 if inList(gen, deps.ReexportGeneratedHeaders) {
768 depTag = genHeaderExportDepTag
769 }
770 actx.AddDependency(c, depTag, gen)
771 }
Dan Willemsenb40aab62016-04-20 14:21:14 -0700772
Colin Cross68861832016-07-08 10:41:41 -0700773 actx.AddDependency(c, objDepTag, deps.ObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700774
775 if deps.CrtBegin != "" {
Colin Cross68861832016-07-08 10:41:41 -0700776 actx.AddDependency(c, crtBeginDepTag, deps.CrtBegin)
Colin Crossca860ac2016-01-04 14:34:37 -0800777 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700778 if deps.CrtEnd != "" {
Colin Cross68861832016-07-08 10:41:41 -0700779 actx.AddDependency(c, crtEndDepTag, deps.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700780 }
Dan Albert914449f2016-06-17 16:45:24 -0700781
782 version := ctx.sdkVersion()
783 actx.AddVariationDependencies([]blueprint.Variation{
784 {"ndk_api", version}, {"link", "shared"}}, ndkStubDepTag, variantNdkLibs...)
785 actx.AddVariationDependencies([]blueprint.Variation{
786 {"ndk_api", version}, {"link", "shared"}}, ndkLateStubDepTag, variantLateNdkLibs...)
Colin Cross6362e272015-10-29 15:25:03 -0700787}
Colin Cross21b9a242015-03-24 14:15:58 -0700788
Dan Albert7e9d2952016-08-04 13:02:36 -0700789func beginMutator(ctx android.BottomUpMutatorContext) {
790 if c, ok := ctx.Module().(*Module); ok && c.Enabled() {
791 c.beginMutator(ctx)
792 }
793}
794
Colin Crossca860ac2016-01-04 14:34:37 -0800795func (c *Module) clang(ctx BaseModuleContext) bool {
796 clang := Bool(c.Properties.Clang)
797
798 if c.Properties.Clang == nil {
799 if ctx.Host() {
800 clang = true
801 }
802
803 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
804 clang = true
805 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800806 }
Colin Cross28344522015-04-22 13:07:53 -0700807
Colin Crossca860ac2016-01-04 14:34:37 -0800808 if !c.toolchain(ctx).ClangSupported() {
809 clang = false
810 }
811
812 return clang
813}
814
Colin Crossc99deeb2016-04-11 15:06:20 -0700815// Convert dependencies to paths. Returns a PathDeps containing paths
Colin Cross635c3b02016-05-18 15:37:25 -0700816func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
Colin Crossca860ac2016-01-04 14:34:37 -0800817 var depPaths PathDeps
Colin Crossca860ac2016-01-04 14:34:37 -0800818
Dan Willemsena96ff642016-06-07 12:34:45 -0700819 // Whether a module can link to another module, taking into
820 // account NDK linking.
Dan Albert9e10cd42016-08-03 14:12:14 -0700821 checkLinkType := func(from, to *Module) {
Dan Willemsena96ff642016-06-07 12:34:45 -0700822 if from.Target().Os != android.Android {
823 // Host code is not restricted
Dan Albert9e10cd42016-08-03 14:12:14 -0700824 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700825 }
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700826 if from.Properties.UseVndk {
827 // Vendor code is already limited by the vendor mutator
828 return
829 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700830 if from.Properties.Sdk_version == "" {
831 // Platform code can link to anything
Dan Albert9e10cd42016-08-03 14:12:14 -0700832 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700833 }
Colin Crossb916a382016-07-29 17:28:03 -0700834 if _, ok := to.linker.(*toolchainLibraryDecorator); ok {
Dan Willemsena96ff642016-06-07 12:34:45 -0700835 // These are always allowed
Dan Albert9e10cd42016-08-03 14:12:14 -0700836 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700837 }
838 if _, ok := to.linker.(*ndkPrebuiltLibraryLinker); ok {
839 // These are allowed, but don't set sdk_version
Dan Albert9e10cd42016-08-03 14:12:14 -0700840 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700841 }
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700842 if _, ok := to.linker.(*ndkPrebuiltStlLinker); ok {
843 // These are allowed, but don't set sdk_version
Dan Albert9e10cd42016-08-03 14:12:14 -0700844 return
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700845 }
Colin Crossb916a382016-07-29 17:28:03 -0700846 if _, ok := to.linker.(*stubDecorator); ok {
Dan Albert914449f2016-06-17 16:45:24 -0700847 // These aren't real libraries, but are the stub shared libraries that are included in
848 // the NDK.
Dan Albert9e10cd42016-08-03 14:12:14 -0700849 return
Dan Albert914449f2016-06-17 16:45:24 -0700850 }
Dan Albert9e10cd42016-08-03 14:12:14 -0700851 if to.Properties.Sdk_version == "" {
852 // NDK code linking to platform code is never okay.
853 ctx.ModuleErrorf("depends on non-NDK-built library %q",
854 ctx.OtherModuleName(to))
855 }
856
857 // All this point we know we have two NDK libraries, but we need to
858 // check that we're not linking against anything built against a higher
859 // API level, as it is only valid to link against older or equivalent
860 // APIs.
861
862 if from.Properties.Sdk_version == "current" {
863 // Current can link against anything.
864 return
865 } else if to.Properties.Sdk_version == "current" {
866 // Current can't be linked against by anything else.
867 ctx.ModuleErrorf("links %q built against newer API version %q",
868 ctx.OtherModuleName(to), "current")
869 }
870
871 fromApi, err := strconv.Atoi(from.Properties.Sdk_version)
872 if err != nil {
873 ctx.PropertyErrorf("sdk_version",
874 "Invalid sdk_version value (must be int): %q",
875 from.Properties.Sdk_version)
876 }
877 toApi, err := strconv.Atoi(to.Properties.Sdk_version)
878 if err != nil {
879 ctx.PropertyErrorf("sdk_version",
880 "Invalid sdk_version value (must be int): %q",
881 to.Properties.Sdk_version)
882 }
883
884 if toApi > fromApi {
885 ctx.ModuleErrorf("links %q built against newer API version %q",
886 ctx.OtherModuleName(to), to.Properties.Sdk_version)
887 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700888 }
889
Colin Crossc99deeb2016-04-11 15:06:20 -0700890 ctx.VisitDirectDeps(func(m blueprint.Module) {
891 name := ctx.OtherModuleName(m)
892 tag := ctx.OtherModuleDependencyTag(m)
Colin Crossca860ac2016-01-04 14:34:37 -0800893
Colin Cross635c3b02016-05-18 15:37:25 -0700894 a, _ := m.(android.Module)
Colin Crossc99deeb2016-04-11 15:06:20 -0700895 if a == nil {
896 ctx.ModuleErrorf("module %q not an android module", name)
897 return
Colin Crossca860ac2016-01-04 14:34:37 -0800898 }
Colin Crossca860ac2016-01-04 14:34:37 -0800899
Dan Willemsena96ff642016-06-07 12:34:45 -0700900 cc, _ := m.(*Module)
901 if cc == nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700902 switch tag {
Colin Cross068e0fe2016-12-13 15:23:47 -0800903 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700904 case genSourceDepTag:
905 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
906 depPaths.GeneratedSources = append(depPaths.GeneratedSources,
907 genRule.GeneratedSourceFiles()...)
908 } else {
909 ctx.ModuleErrorf("module %q is not a gensrcs or genrule", name)
910 }
Colin Crosse90bfd12017-04-26 16:59:26 -0700911 // Support exported headers from a generated_sources dependency
912 fallthrough
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700913 case genHeaderDepTag, genHeaderExportDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700914 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
915 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders,
916 genRule.GeneratedSourceFiles()...)
Colin Cross5ed99c62016-11-22 12:55:55 -0800917 flags := includeDirsToFlags(genRule.GeneratedHeaderDirs())
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700918 depPaths.Flags = append(depPaths.Flags, flags)
919 if tag == genHeaderExportDepTag {
920 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700921 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps,
922 genRule.GeneratedSourceFiles()...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700923 }
Dan Willemsenb40aab62016-04-20 14:21:14 -0700924 } else {
925 ctx.ModuleErrorf("module %q is not a genrule", name)
926 }
927 default:
Colin Crossc99deeb2016-04-11 15:06:20 -0700928 ctx.ModuleErrorf("depends on non-cc module %q", name)
Colin Crossca860ac2016-01-04 14:34:37 -0800929 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700930 return
931 }
932
933 if !a.Enabled() {
Colin Crossa8f5e9a2016-12-13 12:51:11 -0800934 if ctx.AConfig().AllowMissingDependencies() {
935 ctx.AddMissingDependencies([]string{name})
936 } else {
937 ctx.ModuleErrorf("depends on disabled module %q", name)
938 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700939 return
940 }
941
Colin Crossa1ad8d12016-06-01 17:09:44 -0700942 if a.Target().Os != ctx.Os() {
943 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), name)
944 return
945 }
946
947 if a.Target().Arch.ArchType != ctx.Arch().ArchType {
948 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700949 return
950 }
951
Colin Crossc99deeb2016-04-11 15:06:20 -0700952 if tag == reuseObjTag {
Colin Crossbba99042016-11-23 15:45:05 -0800953 if l, ok := cc.compiler.(libraryInterface); ok {
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700954 objs, flags, deps := l.reuseObjs()
Colin Cross10d22312017-05-03 11:01:58 -0700955 depPaths.Objs = depPaths.Objs.Append(objs)
956 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700957 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps, deps...)
Colin Crossbba99042016-11-23 15:45:05 -0800958 return
959 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700960 }
961
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700962 if t, ok := tag.(dependencyTag); ok && t.library {
Dan Willemsena96ff642016-06-07 12:34:45 -0700963 if i, ok := cc.linker.(exportedFlagsProducer); ok {
Dan Willemsen76f08272016-07-09 00:14:08 -0700964 flags := i.exportedFlags()
Dan Willemsen847dcc72016-09-29 12:13:36 -0700965 deps := i.exportedFlagsDeps()
Dan Willemsen76f08272016-07-09 00:14:08 -0700966 depPaths.Flags = append(depPaths.Flags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700967 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders, deps...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700968
969 if t.reexportFlags {
Dan Willemsen76f08272016-07-09 00:14:08 -0700970 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700971 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps, deps...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700972 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700973 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700974
Dan Albert9e10cd42016-08-03 14:12:14 -0700975 checkLinkType(c, cc)
Colin Crossc99deeb2016-04-11 15:06:20 -0700976 }
977
Colin Cross26c34ed2016-09-30 17:10:16 -0700978 var ptr *android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -0700979 var depPtr *android.Paths
Colin Crossc99deeb2016-04-11 15:06:20 -0700980
Colin Cross26c34ed2016-09-30 17:10:16 -0700981 linkFile := cc.outputFile
982 depFile := android.OptionalPath{}
983
Colin Crossc99deeb2016-04-11 15:06:20 -0700984 switch tag {
Dan Albert914449f2016-06-17 16:45:24 -0700985 case ndkStubDepTag, sharedDepTag, sharedExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700986 ptr = &depPaths.SharedLibs
987 depPtr = &depPaths.SharedLibsDeps
988 depFile = cc.linker.(libraryInterface).toc()
Dan Albert914449f2016-06-17 16:45:24 -0700989 case lateSharedDepTag, ndkLateStubDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700990 ptr = &depPaths.LateSharedLibs
991 depPtr = &depPaths.LateSharedLibsDeps
992 depFile = cc.linker.(libraryInterface).toc()
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700993 case staticDepTag, staticExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700994 ptr = &depPaths.StaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -0700995 case lateStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700996 ptr = &depPaths.LateStaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -0700997 case wholeStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700998 ptr = &depPaths.WholeStaticLibs
Colin Crossb916a382016-07-29 17:28:03 -0700999 staticLib, ok := cc.linker.(libraryInterface)
1000 if !ok || !staticLib.static() {
Dan Willemsena96ff642016-06-07 12:34:45 -07001001 ctx.ModuleErrorf("module %q not a static library", name)
Colin Crossc99deeb2016-04-11 15:06:20 -07001002 return
1003 }
1004
1005 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
1006 postfix := " (required by " + ctx.OtherModuleName(m) + ")"
1007 for i := range missingDeps {
1008 missingDeps[i] += postfix
1009 }
1010 ctx.AddMissingDependencies(missingDeps)
1011 }
Dan Willemsen5cb580f2016-09-26 17:33:01 -07001012 depPaths.WholeStaticLibObjs = depPaths.WholeStaticLibObjs.Append(staticLib.objs())
Colin Cross5950f382016-12-13 12:50:57 -08001013 case headerDepTag:
1014 // Nothing
Colin Crossc99deeb2016-04-11 15:06:20 -07001015 case objDepTag:
Dan Willemsen5cb580f2016-09-26 17:33:01 -07001016 depPaths.Objs.objFiles = append(depPaths.Objs.objFiles, linkFile.Path())
Colin Crossc99deeb2016-04-11 15:06:20 -07001017 case crtBeginDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001018 depPaths.CrtBegin = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -07001019 case crtEndDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001020 depPaths.CrtEnd = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -07001021 }
1022
Dan Willemsen581341d2017-02-09 16:16:31 -08001023 switch tag {
1024 case staticDepTag, staticExportDepTag, lateStaticDepTag:
1025 staticLib, ok := cc.linker.(libraryInterface)
1026 if !ok || !staticLib.static() {
1027 ctx.ModuleErrorf("module %q not a static library", name)
1028 return
1029 }
1030
1031 // When combining coverage files for shared libraries and executables, coverage files
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001032 // in static libraries act as if they were whole static libraries. The same goes for
1033 // source based Abi dump files.
Dan Willemsen581341d2017-02-09 16:16:31 -08001034 depPaths.StaticLibObjs.coverageFiles = append(depPaths.StaticLibObjs.coverageFiles,
1035 staticLib.objs().coverageFiles...)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001036 depPaths.StaticLibObjs.sAbiDumpFiles = append(depPaths.StaticLibObjs.sAbiDumpFiles,
1037 staticLib.objs().sAbiDumpFiles...)
Dan Willemsen581341d2017-02-09 16:16:31 -08001038 }
1039
Colin Cross26c34ed2016-09-30 17:10:16 -07001040 if ptr != nil {
Colin Crossce75d2c2016-10-06 16:12:58 -07001041 if !linkFile.Valid() {
1042 ctx.ModuleErrorf("module %q missing output file", name)
1043 return
1044 }
Colin Cross26c34ed2016-09-30 17:10:16 -07001045 *ptr = append(*ptr, linkFile.Path())
1046 }
1047
Colin Crossc99deeb2016-04-11 15:06:20 -07001048 if depPtr != nil {
Colin Cross26c34ed2016-09-30 17:10:16 -07001049 dep := depFile
1050 if !dep.Valid() {
1051 dep = linkFile
1052 }
1053 *depPtr = append(*depPtr, dep.Path())
Colin Crossca860ac2016-01-04 14:34:37 -08001054 }
1055 })
1056
1057 return depPaths
1058}
1059
1060func (c *Module) InstallInData() bool {
1061 if c.installer == nil {
1062 return false
1063 }
Vishwath Mohan1dd88392017-03-29 22:00:18 -07001064 return c.installer.inData()
1065}
1066
1067func (c *Module) InstallInSanitizerDir() bool {
1068 if c.installer == nil {
1069 return false
1070 }
1071 if c.sanitize != nil && c.sanitize.inSanitizerDir() {
Colin Cross94610402016-08-29 13:41:32 -07001072 return true
1073 }
Vishwath Mohan1dd88392017-03-29 22:00:18 -07001074 return c.installer.inSanitizerDir()
Colin Crossca860ac2016-01-04 14:34:37 -08001075}
1076
Dan Willemsen4aa75ca2016-09-28 16:18:03 -07001077func (c *Module) HostToolPath() android.OptionalPath {
1078 if c.installer == nil {
1079 return android.OptionalPath{}
1080 }
1081 return c.installer.hostToolPath()
1082}
1083
Colin Cross2ba19d92015-05-07 15:44:20 -07001084//
Colin Crosscfad1192015-11-02 16:43:11 -08001085// Defaults
1086//
Colin Crossca860ac2016-01-04 14:34:37 -08001087type Defaults struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001088 android.ModuleBase
1089 android.DefaultsModule
Colin Crosscfad1192015-11-02 16:43:11 -08001090}
1091
Colin Cross635c3b02016-05-18 15:37:25 -07001092func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -08001093}
1094
Colin Cross1e676be2016-10-12 14:38:15 -07001095func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
1096}
1097
Colin Crossca860ac2016-01-04 14:34:37 -08001098func defaultsFactory() (blueprint.Module, []interface{}) {
Colin Crosse1d764e2016-08-18 14:18:32 -07001099 return DefaultsFactory()
1100}
1101
1102func DefaultsFactory(props ...interface{}) (blueprint.Module, []interface{}) {
Colin Crossca860ac2016-01-04 14:34:37 -08001103 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -08001104
Colin Crosse1d764e2016-08-18 14:18:32 -07001105 props = append(props,
Colin Crossca860ac2016-01-04 14:34:37 -08001106 &BaseProperties{},
1107 &BaseCompilerProperties{},
1108 &BaseLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -07001109 &LibraryProperties{},
Colin Cross919281a2016-04-05 16:42:05 -07001110 &FlagExporterProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001111 &BinaryLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -07001112 &TestProperties{},
1113 &TestBinaryProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001114 &UnusedProperties{},
1115 &StlProperties{},
Colin Cross16b23492016-01-06 14:41:07 -08001116 &SanitizeProperties{},
Colin Cross665dce92016-04-28 14:50:03 -07001117 &StripProperties{},
Dan Willemsen7424d612016-09-01 13:45:39 -07001118 &InstallerProperties{},
Dan Willemsena03cf6d2016-09-26 15:45:04 -07001119 &TidyProperties{},
Dan Willemsen581341d2017-02-09 16:16:31 -08001120 &CoverageProperties{},
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001121 &SAbiProperties{},
Colin Crosse1d764e2016-08-18 14:18:32 -07001122 )
Colin Crosscfad1192015-11-02 16:43:11 -08001123
Colin Crosse1d764e2016-08-18 14:18:32 -07001124 return android.InitDefaultsModule(module, module, props...)
Colin Crosscfad1192015-11-02 16:43:11 -08001125}
1126
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001127const (
1128 // coreMode is the variant used for framework-private libraries, or
1129 // SDK libraries. (which framework-private libraries can use)
1130 coreMode = "core"
1131
1132 // vendorMode is the variant used for /vendor code that compiles
1133 // against the VNDK.
1134 vendorMode = "vendor"
1135)
1136
1137func vendorMutator(mctx android.BottomUpMutatorContext) {
1138 if mctx.Os() != android.Android {
1139 return
1140 }
1141
1142 m, ok := mctx.Module().(*Module)
1143 if !ok {
1144 return
1145 }
1146
1147 // Sanity check
1148 if Bool(m.Properties.Vendor_available) && mctx.Vendor() {
1149 mctx.PropertyErrorf("vendor_available",
1150 "doesn't make sense at the same time as `vendor: true` or `proprietary: true`")
1151 return
1152 }
1153
1154 if !mctx.DeviceConfig().CompileVndk() {
1155 // If the device isn't compiling against the VNDK, we always
1156 // use the core mode.
1157 mctx.CreateVariations(coreMode)
1158 } else if _, ok := m.linker.(*llndkStubDecorator); ok {
1159 // LL-NDK stubs only exist in the vendor variant, since the
1160 // real libraries will be used in the core variant.
1161 mctx.CreateVariations(vendorMode)
1162 } else if Bool(m.Properties.Vendor_available) {
1163 // This will be available in both /system and /vendor
1164 mod := mctx.CreateVariations(coreMode, vendorMode)
1165 mod[1].(*Module).Properties.UseVndk = true
1166 } else if mctx.Vendor() && m.Properties.Sdk_version == "" {
1167 // This will be available in /vendor only
1168 mod := mctx.CreateVariations(vendorMode)
1169 mod[0].(*Module).Properties.UseVndk = true
1170 } else {
1171 // This is either in /system (or similar: /data), or is a
1172 // modules built with the NDK. Modules built with the NDK
1173 // will be restricted using the existing link type checks.
1174 mctx.CreateVariations(coreMode)
1175 }
1176}
1177
Colin Cross74d1ec02015-04-28 13:30:13 -07001178// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
1179// modifies the slice contents in place, and returns a subslice of the original slice
1180func lastUniqueElements(list []string) []string {
1181 totalSkip := 0
1182 for i := len(list) - 1; i >= totalSkip; i-- {
1183 skip := 0
1184 for j := i - 1; j >= totalSkip; j-- {
1185 if list[i] == list[j] {
1186 skip++
1187 } else {
1188 list[j+skip] = list[j]
1189 }
1190 }
1191 totalSkip += skip
1192 }
1193 return list[totalSkip:]
1194}
Colin Cross06a931b2015-10-28 17:23:31 -07001195
1196var Bool = proptools.Bool