blob: a10aaaac746ea27599641a56e43a1241f7f17eeb [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 Willemsen95421a42017-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 Chowdhary756b1c22017-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 Cross28344522015-04-22 13:07:53 -0700109 LdFlags []string // Flags that apply to linker command lines
Colin Cross16b23492016-01-06 14:41:07 -0800110 libFlags []string // Flags to add libraries early to the link order
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700111 TidyFlags []string // Flags that apply to clang-tidy
Jayant Chowdhary756b1c22017-02-08 13:45:53 -0800112 SAbiFlags []string // Flags that apply to header-abi-dumper
Colin Cross91e90042016-12-02 17:13:24 -0800113 YasmFlags []string // Flags that apply to yasm assembly source files
Colin Cross28344522015-04-22 13:07:53 -0700114
Colin Cross1a805c22017-03-30 15:03:04 -0700115 // Global include flags that apply to C, C++, and assembly source files
116 // These must be after any module include flags, which will be in GlobalFlags.
117 SystemIncludeFlags []string
118
Colin Crossb98c8b02016-07-29 13:44:28 -0700119 Toolchain config.Toolchain
Colin Cross28344522015-04-22 13:07:53 -0700120 Clang bool
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700121 Tidy bool
Dan Willemsen581341d2017-02-09 16:16:31 -0800122 Coverage bool
Jayant Chowdhary756b1c22017-02-08 13:45:53 -0800123 SAbiDump bool
Colin Crossca860ac2016-01-04 14:34:37 -0800124
125 RequiredInstructionSet string
Colin Cross16b23492016-01-06 14:41:07 -0800126 DynamicLinker string
127
Colin Cross635c3b02016-05-18 15:37:25 -0700128 CFlagsDeps android.Paths // Files depended on by compiler flags
Colin Cross18c0c5a2016-12-01 14:45:23 -0800129
130 GroupStaticLibs bool
Colin Crossc472d572015-03-17 15:06:21 -0700131}
132
Colin Cross81413472016-04-11 14:37:39 -0700133type ObjectLinkerProperties struct {
134 // names of other cc_object modules to link into this module using partial linking
135 Objs []string `android:"arch_variant"`
136}
137
Colin Crossca860ac2016-01-04 14:34:37 -0800138// Properties used to compile all C or C++ modules
139type BaseProperties struct {
140 // compile module with clang instead of gcc
141 Clang *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700142
143 // Minimum sdk version supported when compiling against the ndk
144 Sdk_version string
145
Colin Crossca860ac2016-01-04 14:34:37 -0800146 // don't insert default compiler flags into asflags, cflags,
147 // cppflags, conlyflags, ldflags, or include_dirs
148 No_default_compiler_flags *bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700149
Dan Willemsen95421a42017-04-06 12:43:22 -0700150 // whether this module should be allowed to install onto /vendor as
151 // well as /system. The two variants will be built separately, one
152 // like normal, and the other limited to the set of libraries and
153 // headers that are exposed to /vendor modules.
154 //
155 // The vendor variant may be used with a different (newer) /system,
156 // so it shouldn't have any unversioned runtime dependencies, or
157 // make assumptions about the system that may not be true in the
158 // future.
159 //
160 // Nothing happens if BOARD_VNDK_VERSION isn't set in the BoardConfig.mk
161 Vendor_available *bool
162
Colin Crossc99deeb2016-04-11 15:06:20 -0700163 AndroidMkSharedLibs []string `blueprint:"mutated"`
Colin Crossbc6fb162016-05-24 15:39:04 -0700164 HideFromMake bool `blueprint:"mutated"`
Colin Crossb0f28952016-09-19 16:46:53 -0700165 PreventInstall bool `blueprint:"mutated"`
Dan Willemsen95421a42017-04-06 12:43:22 -0700166
167 UseVndk bool `blueprint:"mutated"`
Colin Crossca860ac2016-01-04 14:34:37 -0800168}
169
Colin Crossca860ac2016-01-04 14:34:37 -0800170type UnusedProperties struct {
Dan Willemsen581341d2017-02-09 16:16:31 -0800171 Tags []string
Colin Crosscfad1192015-11-02 16:43:11 -0800172}
173
Colin Crossca860ac2016-01-04 14:34:37 -0800174type ModuleContextIntf interface {
Colin Crossca860ac2016-01-04 14:34:37 -0800175 static() bool
176 staticBinary() bool
177 clang() bool
Colin Crossb98c8b02016-07-29 13:44:28 -0700178 toolchain() config.Toolchain
Colin Crossca860ac2016-01-04 14:34:37 -0800179 noDefaultCompilerFlags() bool
180 sdk() bool
181 sdkVersion() string
Dan Willemsend2ede872016-11-18 14:54:24 -0800182 vndk() bool
Jayant Chowdhary756b1c22017-02-08 13:45:53 -0800183 createVndkSourceAbiDump() bool
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700184 selectedStl() string
Colin Crossce75d2c2016-10-06 16:12:58 -0700185 baseModuleName() string
Colin Crossca860ac2016-01-04 14:34:37 -0800186}
187
188type ModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700189 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800190 ModuleContextIntf
191}
192
193type BaseModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700194 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800195 ModuleContextIntf
196}
197
Colin Cross37047f12016-12-13 17:06:13 -0800198type DepsContext interface {
199 android.BottomUpMutatorContext
200 ModuleContextIntf
201}
202
Colin Crossca860ac2016-01-04 14:34:37 -0800203type feature interface {
204 begin(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800205 deps(ctx DepsContext, deps Deps) Deps
Colin Crossca860ac2016-01-04 14:34:37 -0800206 flags(ctx ModuleContext, flags Flags) Flags
207 props() []interface{}
208}
209
210type compiler interface {
Colin Cross42742b82016-08-01 13:20:05 -0700211 compilerInit(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800212 compilerDeps(ctx DepsContext, deps Deps) Deps
Colin Cross42742b82016-08-01 13:20:05 -0700213 compilerFlags(ctx ModuleContext, flags Flags) Flags
214 compilerProps() []interface{}
215
Colin Cross76fada02016-07-27 10:31:13 -0700216 appendCflags([]string)
217 appendAsflags([]string)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700218 compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects
Colin Crossca860ac2016-01-04 14:34:37 -0800219}
220
221type linker interface {
Colin Cross42742b82016-08-01 13:20:05 -0700222 linkerInit(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800223 linkerDeps(ctx DepsContext, deps Deps) Deps
Colin Cross42742b82016-08-01 13:20:05 -0700224 linkerFlags(ctx ModuleContext, flags Flags) Flags
225 linkerProps() []interface{}
226
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700227 link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path
Colin Cross76fada02016-07-27 10:31:13 -0700228 appendLdflags([]string)
Colin Crossca860ac2016-01-04 14:34:37 -0800229}
230
231type installer interface {
Colin Cross42742b82016-08-01 13:20:05 -0700232 installerProps() []interface{}
Colin Cross635c3b02016-05-18 15:37:25 -0700233 install(ctx ModuleContext, path android.Path)
Colin Crossca860ac2016-01-04 14:34:37 -0800234 inData() bool
Vishwath Mohan0d0a6f32017-03-29 22:00:18 -0700235 inSanitizerDir() bool
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700236 hostToolPath() android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800237}
238
Colin Crossc99deeb2016-04-11 15:06:20 -0700239type dependencyTag struct {
240 blueprint.BaseDependencyTag
241 name string
242 library bool
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700243
244 reexportFlags bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700245}
246
247var (
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700248 sharedDepTag = dependencyTag{name: "shared", library: true}
249 sharedExportDepTag = dependencyTag{name: "shared", library: true, reexportFlags: true}
250 lateSharedDepTag = dependencyTag{name: "late shared", library: true}
251 staticDepTag = dependencyTag{name: "static", library: true}
252 staticExportDepTag = dependencyTag{name: "static", library: true, reexportFlags: true}
253 lateStaticDepTag = dependencyTag{name: "late static", library: true}
254 wholeStaticDepTag = dependencyTag{name: "whole static", library: true, reexportFlags: true}
Colin Cross32ec36c2016-12-15 07:39:51 -0800255 headerDepTag = dependencyTag{name: "header", library: true}
256 headerExportDepTag = dependencyTag{name: "header", library: true, reexportFlags: true}
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700257 genSourceDepTag = dependencyTag{name: "gen source"}
258 genHeaderDepTag = dependencyTag{name: "gen header"}
259 genHeaderExportDepTag = dependencyTag{name: "gen header", reexportFlags: true}
260 objDepTag = dependencyTag{name: "obj"}
261 crtBeginDepTag = dependencyTag{name: "crtbegin"}
262 crtEndDepTag = dependencyTag{name: "crtend"}
263 reuseObjTag = dependencyTag{name: "reuse objects"}
264 ndkStubDepTag = dependencyTag{name: "ndk stub", library: true}
265 ndkLateStubDepTag = dependencyTag{name: "ndk late stub", library: true}
Colin Crossc99deeb2016-04-11 15:06:20 -0700266)
267
Colin Crossca860ac2016-01-04 14:34:37 -0800268// Module contains the properties and members used by all C/C++ module types, and implements
269// the blueprint.Module interface. It delegates to compiler, linker, and installer interfaces
270// to construct the output file. Behavior can be customized with a Customizer interface
271type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700272 android.ModuleBase
273 android.DefaultableModule
Colin Crossc472d572015-03-17 15:06:21 -0700274
Colin Crossca860ac2016-01-04 14:34:37 -0800275 Properties BaseProperties
276 unused UnusedProperties
Colin Crossfa138792015-04-24 17:31:52 -0700277
Colin Crossca860ac2016-01-04 14:34:37 -0800278 // initialize before calling Init
Colin Cross635c3b02016-05-18 15:37:25 -0700279 hod android.HostOrDeviceSupported
280 multilib android.Multilib
Colin Crossc472d572015-03-17 15:06:21 -0700281
Colin Crossca860ac2016-01-04 14:34:37 -0800282 // delegates, initialize before calling Init
Colin Crossb4ce0ec2016-09-13 13:41:39 -0700283 features []feature
284 compiler compiler
285 linker linker
286 installer installer
287 stl *stl
288 sanitize *sanitize
Dan Willemsen581341d2017-02-09 16:16:31 -0800289 coverage *coverage
Jayant Chowdhary756b1c22017-02-08 13:45:53 -0800290 sabi *sabi
Colin Cross16b23492016-01-06 14:41:07 -0800291
292 androidMkSharedLibDeps []string
Colin Cross74d1ec02015-04-28 13:30:13 -0700293
Colin Cross635c3b02016-05-18 15:37:25 -0700294 outputFile android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800295
Colin Crossb98c8b02016-07-29 13:44:28 -0700296 cachedToolchain config.Toolchain
Colin Crossb916a382016-07-29 17:28:03 -0700297
298 subAndroidMkOnce map[subAndroidMkProvider]bool
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800299
300 // Flags used to compile this module
301 flags Flags
Colin Crossc472d572015-03-17 15:06:21 -0700302}
303
Colin Crossca860ac2016-01-04 14:34:37 -0800304func (c *Module) Init() (blueprint.Module, []interface{}) {
305 props := []interface{}{&c.Properties, &c.unused}
Colin Crossca860ac2016-01-04 14:34:37 -0800306 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700307 props = append(props, c.compiler.compilerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800308 }
309 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700310 props = append(props, c.linker.linkerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800311 }
312 if c.installer != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700313 props = append(props, c.installer.installerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800314 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700315 if c.stl != nil {
316 props = append(props, c.stl.props()...)
317 }
Colin Cross16b23492016-01-06 14:41:07 -0800318 if c.sanitize != nil {
319 props = append(props, c.sanitize.props()...)
320 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800321 if c.coverage != nil {
322 props = append(props, c.coverage.props()...)
323 }
Jayant Chowdhary756b1c22017-02-08 13:45:53 -0800324 if c.sabi != nil {
325 props = append(props, c.sabi.props()...)
326 }
Colin Crossca860ac2016-01-04 14:34:37 -0800327 for _, feature := range c.features {
328 props = append(props, feature.props()...)
329 }
Colin Crossc472d572015-03-17 15:06:21 -0700330
Colin Cross635c3b02016-05-18 15:37:25 -0700331 _, props = android.InitAndroidArchModule(c, c.hod, c.multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700332
Colin Cross635c3b02016-05-18 15:37:25 -0700333 return android.InitDefaultableModule(c, c, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700334}
335
Colin Crossb916a382016-07-29 17:28:03 -0700336// Returns true for dependency roots (binaries)
337// TODO(ccross): also handle dlopenable libraries
338func (c *Module) isDependencyRoot() bool {
339 if root, ok := c.linker.(interface {
340 isDependencyRoot() bool
341 }); ok {
342 return root.isDependencyRoot()
343 }
344 return false
345}
346
Dan Willemsen95421a42017-04-06 12:43:22 -0700347func (c *Module) vndk() bool {
348 return c.Properties.UseVndk
349}
350
Colin Crossca860ac2016-01-04 14:34:37 -0800351type baseModuleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700352 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800353 moduleContextImpl
354}
355
Colin Cross37047f12016-12-13 17:06:13 -0800356type depsContext struct {
357 android.BottomUpMutatorContext
358 moduleContextImpl
359}
360
Colin Crossca860ac2016-01-04 14:34:37 -0800361type moduleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700362 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800363 moduleContextImpl
364}
365
Dan Willemsen95421a42017-04-06 12:43:22 -0700366// Vendor returns true for vendor modules so that they get installed onto the
367// correct partition
368func (ctx *moduleContext) Vendor() bool {
369 return ctx.ModuleContext.Vendor() || ctx.moduleContextImpl.mod.Properties.UseVndk
370}
371
Colin Crossca860ac2016-01-04 14:34:37 -0800372type moduleContextImpl struct {
373 mod *Module
374 ctx BaseModuleContext
375}
376
Colin Crossca860ac2016-01-04 14:34:37 -0800377func (ctx *moduleContextImpl) clang() bool {
378 return ctx.mod.clang(ctx.ctx)
379}
380
Colin Crossb98c8b02016-07-29 13:44:28 -0700381func (ctx *moduleContextImpl) toolchain() config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800382 return ctx.mod.toolchain(ctx.ctx)
383}
384
385func (ctx *moduleContextImpl) static() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700386 if static, ok := ctx.mod.linker.(interface {
387 static() bool
388 }); ok {
389 return static.static()
Colin Crossca860ac2016-01-04 14:34:37 -0800390 }
Colin Crossb916a382016-07-29 17:28:03 -0700391 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800392}
393
394func (ctx *moduleContextImpl) staticBinary() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700395 if static, ok := ctx.mod.linker.(interface {
396 staticBinary() bool
397 }); ok {
398 return static.staticBinary()
Colin Crossca860ac2016-01-04 14:34:37 -0800399 }
Colin Crossb916a382016-07-29 17:28:03 -0700400 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800401}
402
403func (ctx *moduleContextImpl) noDefaultCompilerFlags() bool {
404 return Bool(ctx.mod.Properties.No_default_compiler_flags)
405}
406
407func (ctx *moduleContextImpl) sdk() bool {
Dan Willemsen95421a42017-04-06 12:43:22 -0700408 if ctx.ctx.Device() && !ctx.vndk() {
Dan Willemsena96ff642016-06-07 12:34:45 -0700409 return ctx.mod.Properties.Sdk_version != ""
410 }
411 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800412}
413
414func (ctx *moduleContextImpl) sdkVersion() string {
Dan Willemsena96ff642016-06-07 12:34:45 -0700415 if ctx.ctx.Device() {
Dan Willemsenba78b5a2017-03-19 18:30:37 -0700416 if ctx.vndk() {
417 return "current"
Dan Willemsend2ede872016-11-18 14:54:24 -0800418 } else {
419 return ctx.mod.Properties.Sdk_version
420 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700421 }
422 return ""
Colin Crossca860ac2016-01-04 14:34:37 -0800423}
424
Dan Willemsend2ede872016-11-18 14:54:24 -0800425func (ctx *moduleContextImpl) vndk() bool {
Dan Willemsen95421a42017-04-06 12:43:22 -0700426 return ctx.mod.vndk()
Dan Willemsend2ede872016-11-18 14:54:24 -0800427}
428
Jayant Chowdhary756b1c22017-02-08 13:45:53 -0800429// Create source abi dumps if the module belongs to the list of VndkLibraries.
430func (ctx *moduleContextImpl) createVndkSourceAbiDump() bool {
Jayant Chowdhary8cede072017-04-20 06:53:59 -0700431 return ctx.ctx.Device() && ((Bool(ctx.mod.Properties.Vendor_available)) || (inList(ctx.baseModuleName(), config.LLndkLibraries())))
Jayant Chowdhary756b1c22017-02-08 13:45:53 -0800432}
433
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700434func (ctx *moduleContextImpl) selectedStl() string {
435 if stl := ctx.mod.stl; stl != nil {
436 return stl.Properties.SelectedStl
437 }
438 return ""
439}
440
Colin Crossce75d2c2016-10-06 16:12:58 -0700441func (ctx *moduleContextImpl) baseModuleName() string {
442 return ctx.mod.ModuleBase.BaseModuleName()
443}
444
Colin Cross635c3b02016-05-18 15:37:25 -0700445func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800446 return &Module{
447 hod: hod,
448 multilib: multilib,
449 }
450}
451
Colin Cross635c3b02016-05-18 15:37:25 -0700452func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800453 module := newBaseModule(hod, multilib)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700454 module.features = []feature{
455 &tidyFeature{},
456 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700457 module.stl = &stl{}
Colin Cross16b23492016-01-06 14:41:07 -0800458 module.sanitize = &sanitize{}
Dan Willemsen581341d2017-02-09 16:16:31 -0800459 module.coverage = &coverage{}
Jayant Chowdhary756b1c22017-02-08 13:45:53 -0800460 module.sabi = &sabi{}
Colin Crossca860ac2016-01-04 14:34:37 -0800461 return module
462}
463
Colin Crossce75d2c2016-10-06 16:12:58 -0700464func (c *Module) Prebuilt() *android.Prebuilt {
465 if p, ok := c.linker.(prebuiltLinkerInterface); ok {
466 return p.prebuilt()
467 }
468 return nil
469}
470
471func (c *Module) Name() string {
472 name := c.ModuleBase.Name()
Dan Willemsen8ecfbad2017-04-07 15:21:13 -0700473 if p, ok := c.linker.(interface {
474 Name(string) string
475 }); ok {
Colin Crossce75d2c2016-10-06 16:12:58 -0700476 name = p.Name(name)
477 }
478 return name
479}
480
Colin Cross635c3b02016-05-18 15:37:25 -0700481func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800482 ctx := &moduleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700483 ModuleContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800484 moduleContextImpl: moduleContextImpl{
485 mod: c,
486 },
487 }
488 ctx.ctx = ctx
489
490 flags := Flags{
491 Toolchain: c.toolchain(ctx),
492 Clang: c.clang(ctx),
493 }
Colin Crossca860ac2016-01-04 14:34:37 -0800494 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700495 flags = c.compiler.compilerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800496 }
497 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700498 flags = c.linker.linkerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800499 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700500 if c.stl != nil {
501 flags = c.stl.flags(ctx, flags)
502 }
Colin Cross16b23492016-01-06 14:41:07 -0800503 if c.sanitize != nil {
504 flags = c.sanitize.flags(ctx, flags)
505 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800506 if c.coverage != nil {
507 flags = c.coverage.flags(ctx, flags)
508 }
Jayant Chowdhary756b1c22017-02-08 13:45:53 -0800509 if c.sabi != nil {
510 flags = c.sabi.flags(ctx, flags)
511 }
Colin Crossca860ac2016-01-04 14:34:37 -0800512 for _, feature := range c.features {
513 flags = feature.flags(ctx, flags)
514 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800515 if ctx.Failed() {
516 return
517 }
518
Colin Crossb98c8b02016-07-29 13:44:28 -0700519 flags.CFlags, _ = filterList(flags.CFlags, config.IllegalFlags)
520 flags.CppFlags, _ = filterList(flags.CppFlags, config.IllegalFlags)
521 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, config.IllegalFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800522
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800523 deps := c.depsToPaths(ctx)
524 if ctx.Failed() {
525 return
526 }
527 flags.GlobalFlags = append(flags.GlobalFlags, deps.Flags...)
528 c.flags = flags
529
Colin Crossca860ac2016-01-04 14:34:37 -0800530 // Optimization to reduce size of build.ninja
531 // Replace the long list of flags for each file with a module-local variable
532 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
533 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
534 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
535 flags.CFlags = []string{"$cflags"}
536 flags.CppFlags = []string{"$cppflags"}
537 flags.AsFlags = []string{"$asflags"}
538
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700539 var objs Objects
Colin Crossca860ac2016-01-04 14:34:37 -0800540 if c.compiler != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700541 objs = c.compiler.compile(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -0800542 if ctx.Failed() {
543 return
544 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800545 }
546
Colin Crossca860ac2016-01-04 14:34:37 -0800547 if c.linker != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700548 outputFile := c.linker.link(ctx, flags, deps, objs)
Colin Crossca860ac2016-01-04 14:34:37 -0800549 if ctx.Failed() {
550 return
551 }
Colin Cross635c3b02016-05-18 15:37:25 -0700552 c.outputFile = android.OptionalPathForPath(outputFile)
Colin Crossce75d2c2016-10-06 16:12:58 -0700553 }
Colin Cross5049f022015-03-18 13:28:46 -0700554
Colin Crossce75d2c2016-10-06 16:12:58 -0700555 if c.installer != nil && !c.Properties.PreventInstall && c.outputFile.Valid() {
556 c.installer.install(ctx, c.outputFile.Path())
557 if ctx.Failed() {
558 return
Colin Crossca860ac2016-01-04 14:34:37 -0800559 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700560 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800561}
562
Colin Crossb98c8b02016-07-29 13:44:28 -0700563func (c *Module) toolchain(ctx BaseModuleContext) config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800564 if c.cachedToolchain == nil {
Colin Crossb98c8b02016-07-29 13:44:28 -0700565 c.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
Colin Cross3f40fa42015-01-30 17:27:36 -0800566 }
Colin Crossca860ac2016-01-04 14:34:37 -0800567 return c.cachedToolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800568}
569
Colin Crossca860ac2016-01-04 14:34:37 -0800570func (c *Module) begin(ctx BaseModuleContext) {
571 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700572 c.compiler.compilerInit(ctx)
Colin Cross21b9a242015-03-24 14:15:58 -0700573 }
Colin Crossca860ac2016-01-04 14:34:37 -0800574 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700575 c.linker.linkerInit(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800576 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700577 if c.stl != nil {
578 c.stl.begin(ctx)
579 }
Colin Cross16b23492016-01-06 14:41:07 -0800580 if c.sanitize != nil {
581 c.sanitize.begin(ctx)
582 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800583 if c.coverage != nil {
584 c.coverage.begin(ctx)
585 }
Jayant Chowdhary756b1c22017-02-08 13:45:53 -0800586 if c.sabi != nil {
587 c.sabi.begin(ctx)
588 }
Colin Crossca860ac2016-01-04 14:34:37 -0800589 for _, feature := range c.features {
590 feature.begin(ctx)
591 }
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700592 if ctx.sdk() {
593 version, err := normalizeNdkApiLevel(ctx.sdkVersion(), ctx.Arch())
594 if err != nil {
595 ctx.PropertyErrorf("sdk_version", err.Error())
596 }
Dan Albert90f7a4d2016-11-08 14:34:24 -0800597 c.Properties.Sdk_version = version
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700598 }
Colin Crossca860ac2016-01-04 14:34:37 -0800599}
600
Colin Cross37047f12016-12-13 17:06:13 -0800601func (c *Module) deps(ctx DepsContext) Deps {
Colin Crossc99deeb2016-04-11 15:06:20 -0700602 deps := Deps{}
603
604 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700605 deps = c.compiler.compilerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700606 }
607 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700608 deps = c.linker.linkerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700609 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700610 if c.stl != nil {
611 deps = c.stl.deps(ctx, deps)
612 }
Colin Cross16b23492016-01-06 14:41:07 -0800613 if c.sanitize != nil {
614 deps = c.sanitize.deps(ctx, deps)
615 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800616 if c.coverage != nil {
617 deps = c.coverage.deps(ctx, deps)
618 }
Jayant Chowdhary756b1c22017-02-08 13:45:53 -0800619 if c.sabi != nil {
620 deps = c.sabi.deps(ctx, deps)
621 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700622 for _, feature := range c.features {
623 deps = feature.deps(ctx, deps)
624 }
625
626 deps.WholeStaticLibs = lastUniqueElements(deps.WholeStaticLibs)
627 deps.StaticLibs = lastUniqueElements(deps.StaticLibs)
628 deps.LateStaticLibs = lastUniqueElements(deps.LateStaticLibs)
629 deps.SharedLibs = lastUniqueElements(deps.SharedLibs)
630 deps.LateSharedLibs = lastUniqueElements(deps.LateSharedLibs)
Colin Cross5950f382016-12-13 12:50:57 -0800631 deps.HeaderLibs = lastUniqueElements(deps.HeaderLibs)
Colin Crossc99deeb2016-04-11 15:06:20 -0700632
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700633 for _, lib := range deps.ReexportSharedLibHeaders {
634 if !inList(lib, deps.SharedLibs) {
635 ctx.PropertyErrorf("export_shared_lib_headers", "Shared library not in shared_libs: '%s'", lib)
636 }
637 }
638
639 for _, lib := range deps.ReexportStaticLibHeaders {
640 if !inList(lib, deps.StaticLibs) {
641 ctx.PropertyErrorf("export_static_lib_headers", "Static library not in static_libs: '%s'", lib)
642 }
643 }
644
Colin Cross5950f382016-12-13 12:50:57 -0800645 for _, lib := range deps.ReexportHeaderLibHeaders {
646 if !inList(lib, deps.HeaderLibs) {
647 ctx.PropertyErrorf("export_header_lib_headers", "Header library not in header_libs: '%s'", lib)
648 }
649 }
650
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700651 for _, gen := range deps.ReexportGeneratedHeaders {
652 if !inList(gen, deps.GeneratedHeaders) {
653 ctx.PropertyErrorf("export_generated_headers", "Generated header module not in generated_headers: '%s'", gen)
654 }
655 }
656
Colin Crossc99deeb2016-04-11 15:06:20 -0700657 return deps
658}
659
Dan Albert7e9d2952016-08-04 13:02:36 -0700660func (c *Module) beginMutator(actx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800661 ctx := &baseModuleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700662 BaseContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800663 moduleContextImpl: moduleContextImpl{
664 mod: c,
665 },
666 }
667 ctx.ctx = ctx
668
Colin Crossca860ac2016-01-04 14:34:37 -0800669 c.begin(ctx)
Dan Albert7e9d2952016-08-04 13:02:36 -0700670}
671
Colin Cross1e676be2016-10-12 14:38:15 -0700672func (c *Module) DepsMutator(actx android.BottomUpMutatorContext) {
673 if !c.Enabled() {
674 return
675 }
676
Colin Cross37047f12016-12-13 17:06:13 -0800677 ctx := &depsContext{
678 BottomUpMutatorContext: actx,
Dan Albert7e9d2952016-08-04 13:02:36 -0700679 moduleContextImpl: moduleContextImpl{
680 mod: c,
681 },
682 }
683 ctx.ctx = ctx
Colin Crossca860ac2016-01-04 14:34:37 -0800684
Colin Crossc99deeb2016-04-11 15:06:20 -0700685 deps := c.deps(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800686
Colin Crossb5bc4b42016-07-11 16:11:59 -0700687 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.SharedLibs...)
688 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.LateSharedLibs...)
Dan Willemsen72d39932016-07-08 23:23:48 -0700689
Dan Albert914449f2016-06-17 16:45:24 -0700690 variantNdkLibs := []string{}
691 variantLateNdkLibs := []string{}
Dan Willemsen04176992017-03-19 13:44:32 -0700692 if ctx.Os() == android.Android {
Dan Albert914449f2016-06-17 16:45:24 -0700693 version := ctx.sdkVersion()
Dan Willemsen72d39932016-07-08 23:23:48 -0700694
Dan Albert914449f2016-06-17 16:45:24 -0700695 // Rewrites the names of shared libraries into the names of the NDK
696 // libraries where appropriate. This returns two slices.
697 //
698 // The first is a list of non-variant shared libraries (either rewritten
699 // NDK libraries to the modules in prebuilts/ndk, or not rewritten
700 // because they are not NDK libraries).
701 //
702 // The second is a list of ndk_library modules. These need to be
703 // separated because they are a variation dependency and must be added
704 // in a different manner.
705 rewriteNdkLibs := func(list []string) ([]string, []string) {
706 variantLibs := []string{}
707 nonvariantLibs := []string{}
708 for _, entry := range list {
Dan Willemsen04176992017-03-19 13:44:32 -0700709 if ctx.sdk() && inList(entry, ndkPrebuiltSharedLibraries) {
Dan Albert914449f2016-06-17 16:45:24 -0700710 if !inList(entry, ndkMigratedLibs) {
711 nonvariantLibs = append(nonvariantLibs, entry+".ndk."+version)
712 } else {
713 variantLibs = append(variantLibs, entry+ndkLibrarySuffix)
714 }
Dan Willemsen04176992017-03-19 13:44:32 -0700715 } else if ctx.vndk() && inList(entry, config.LLndkLibraries()) {
716 nonvariantLibs = append(nonvariantLibs, entry+llndkLibrarySuffix)
Dan Albert914449f2016-06-17 16:45:24 -0700717 } else {
Dan Willemsencb813a52017-03-28 00:08:30 -0700718 nonvariantLibs = append(nonvariantLibs, entry)
Dan Willemsen72d39932016-07-08 23:23:48 -0700719 }
720 }
Dan Albert914449f2016-06-17 16:45:24 -0700721 return nonvariantLibs, variantLibs
Dan Willemsen72d39932016-07-08 23:23:48 -0700722 }
723
Dan Albert914449f2016-06-17 16:45:24 -0700724 deps.SharedLibs, variantNdkLibs = rewriteNdkLibs(deps.SharedLibs)
725 deps.LateSharedLibs, variantLateNdkLibs = rewriteNdkLibs(deps.LateSharedLibs)
Dan Willemsen72d39932016-07-08 23:23:48 -0700726 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700727
Colin Cross32ec36c2016-12-15 07:39:51 -0800728 for _, lib := range deps.HeaderLibs {
729 depTag := headerDepTag
730 if inList(lib, deps.ReexportHeaderLibHeaders) {
731 depTag = headerExportDepTag
732 }
733 actx.AddVariationDependencies(nil, depTag, lib)
734 }
Colin Cross5950f382016-12-13 12:50:57 -0800735
Colin Crossc99deeb2016-04-11 15:06:20 -0700736 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, wholeStaticDepTag,
737 deps.WholeStaticLibs...)
738
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700739 for _, lib := range deps.StaticLibs {
740 depTag := staticDepTag
741 if inList(lib, deps.ReexportStaticLibHeaders) {
742 depTag = staticExportDepTag
743 }
Colin Cross15a0d462016-07-14 14:49:58 -0700744 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700745 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700746
747 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, lateStaticDepTag,
748 deps.LateStaticLibs...)
749
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700750 for _, lib := range deps.SharedLibs {
751 depTag := sharedDepTag
752 if inList(lib, deps.ReexportSharedLibHeaders) {
753 depTag = sharedExportDepTag
754 }
Colin Cross15a0d462016-07-14 14:49:58 -0700755 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700756 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700757
758 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, lateSharedDepTag,
759 deps.LateSharedLibs...)
760
Colin Cross68861832016-07-08 10:41:41 -0700761 actx.AddDependency(c, genSourceDepTag, deps.GeneratedSources...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700762
763 for _, gen := range deps.GeneratedHeaders {
764 depTag := genHeaderDepTag
765 if inList(gen, deps.ReexportGeneratedHeaders) {
766 depTag = genHeaderExportDepTag
767 }
768 actx.AddDependency(c, depTag, gen)
769 }
Dan Willemsenb40aab62016-04-20 14:21:14 -0700770
Colin Cross68861832016-07-08 10:41:41 -0700771 actx.AddDependency(c, objDepTag, deps.ObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700772
773 if deps.CrtBegin != "" {
Colin Cross68861832016-07-08 10:41:41 -0700774 actx.AddDependency(c, crtBeginDepTag, deps.CrtBegin)
Colin Crossca860ac2016-01-04 14:34:37 -0800775 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700776 if deps.CrtEnd != "" {
Colin Cross68861832016-07-08 10:41:41 -0700777 actx.AddDependency(c, crtEndDepTag, deps.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700778 }
Dan Albert914449f2016-06-17 16:45:24 -0700779
780 version := ctx.sdkVersion()
781 actx.AddVariationDependencies([]blueprint.Variation{
782 {"ndk_api", version}, {"link", "shared"}}, ndkStubDepTag, variantNdkLibs...)
783 actx.AddVariationDependencies([]blueprint.Variation{
784 {"ndk_api", version}, {"link", "shared"}}, ndkLateStubDepTag, variantLateNdkLibs...)
Colin Cross6362e272015-10-29 15:25:03 -0700785}
Colin Cross21b9a242015-03-24 14:15:58 -0700786
Dan Albert7e9d2952016-08-04 13:02:36 -0700787func beginMutator(ctx android.BottomUpMutatorContext) {
788 if c, ok := ctx.Module().(*Module); ok && c.Enabled() {
789 c.beginMutator(ctx)
790 }
791}
792
Colin Crossca860ac2016-01-04 14:34:37 -0800793func (c *Module) clang(ctx BaseModuleContext) bool {
794 clang := Bool(c.Properties.Clang)
795
796 if c.Properties.Clang == nil {
797 if ctx.Host() {
798 clang = true
799 }
800
801 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
802 clang = true
803 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800804 }
Colin Cross28344522015-04-22 13:07:53 -0700805
Colin Crossca860ac2016-01-04 14:34:37 -0800806 if !c.toolchain(ctx).ClangSupported() {
807 clang = false
808 }
809
810 return clang
811}
812
Colin Crossc99deeb2016-04-11 15:06:20 -0700813// Convert dependencies to paths. Returns a PathDeps containing paths
Colin Cross635c3b02016-05-18 15:37:25 -0700814func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
Colin Crossca860ac2016-01-04 14:34:37 -0800815 var depPaths PathDeps
Colin Crossca860ac2016-01-04 14:34:37 -0800816
Dan Willemsena96ff642016-06-07 12:34:45 -0700817 // Whether a module can link to another module, taking into
818 // account NDK linking.
Dan Albert9e10cd42016-08-03 14:12:14 -0700819 checkLinkType := func(from, to *Module) {
Dan Willemsena96ff642016-06-07 12:34:45 -0700820 if from.Target().Os != android.Android {
821 // Host code is not restricted
Dan Albert9e10cd42016-08-03 14:12:14 -0700822 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700823 }
Dan Willemsen95421a42017-04-06 12:43:22 -0700824 if from.Properties.UseVndk {
825 // Vendor code is already limited by the vendor mutator
826 return
827 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700828 if from.Properties.Sdk_version == "" {
829 // Platform code can link to anything
Dan Albert9e10cd42016-08-03 14:12:14 -0700830 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700831 }
Colin Crossb916a382016-07-29 17:28:03 -0700832 if _, ok := to.linker.(*toolchainLibraryDecorator); ok {
Dan Willemsena96ff642016-06-07 12:34:45 -0700833 // These are always allowed
Dan Albert9e10cd42016-08-03 14:12:14 -0700834 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700835 }
836 if _, ok := to.linker.(*ndkPrebuiltLibraryLinker); ok {
837 // These are allowed, but don't set sdk_version
Dan Albert9e10cd42016-08-03 14:12:14 -0700838 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700839 }
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700840 if _, ok := to.linker.(*ndkPrebuiltStlLinker); ok {
841 // These are allowed, but don't set sdk_version
Dan Albert9e10cd42016-08-03 14:12:14 -0700842 return
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700843 }
Colin Crossb916a382016-07-29 17:28:03 -0700844 if _, ok := to.linker.(*stubDecorator); ok {
Dan Albert914449f2016-06-17 16:45:24 -0700845 // These aren't real libraries, but are the stub shared libraries that are included in
846 // the NDK.
Dan Albert9e10cd42016-08-03 14:12:14 -0700847 return
Dan Albert914449f2016-06-17 16:45:24 -0700848 }
Dan Albert9e10cd42016-08-03 14:12:14 -0700849 if to.Properties.Sdk_version == "" {
850 // NDK code linking to platform code is never okay.
851 ctx.ModuleErrorf("depends on non-NDK-built library %q",
852 ctx.OtherModuleName(to))
853 }
854
855 // All this point we know we have two NDK libraries, but we need to
856 // check that we're not linking against anything built against a higher
857 // API level, as it is only valid to link against older or equivalent
858 // APIs.
859
860 if from.Properties.Sdk_version == "current" {
861 // Current can link against anything.
862 return
863 } else if to.Properties.Sdk_version == "current" {
864 // Current can't be linked against by anything else.
865 ctx.ModuleErrorf("links %q built against newer API version %q",
866 ctx.OtherModuleName(to), "current")
867 }
868
869 fromApi, err := strconv.Atoi(from.Properties.Sdk_version)
870 if err != nil {
871 ctx.PropertyErrorf("sdk_version",
872 "Invalid sdk_version value (must be int): %q",
873 from.Properties.Sdk_version)
874 }
875 toApi, err := strconv.Atoi(to.Properties.Sdk_version)
876 if err != nil {
877 ctx.PropertyErrorf("sdk_version",
878 "Invalid sdk_version value (must be int): %q",
879 to.Properties.Sdk_version)
880 }
881
882 if toApi > fromApi {
883 ctx.ModuleErrorf("links %q built against newer API version %q",
884 ctx.OtherModuleName(to), to.Properties.Sdk_version)
885 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700886 }
887
Colin Crossc99deeb2016-04-11 15:06:20 -0700888 ctx.VisitDirectDeps(func(m blueprint.Module) {
889 name := ctx.OtherModuleName(m)
890 tag := ctx.OtherModuleDependencyTag(m)
Colin Crossca860ac2016-01-04 14:34:37 -0800891
Colin Cross635c3b02016-05-18 15:37:25 -0700892 a, _ := m.(android.Module)
Colin Crossc99deeb2016-04-11 15:06:20 -0700893 if a == nil {
894 ctx.ModuleErrorf("module %q not an android module", name)
895 return
Colin Crossca860ac2016-01-04 14:34:37 -0800896 }
Colin Crossca860ac2016-01-04 14:34:37 -0800897
Dan Willemsena96ff642016-06-07 12:34:45 -0700898 cc, _ := m.(*Module)
899 if cc == nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700900 switch tag {
Colin Cross068e0fe2016-12-13 15:23:47 -0800901 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700902 case genSourceDepTag:
903 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
904 depPaths.GeneratedSources = append(depPaths.GeneratedSources,
905 genRule.GeneratedSourceFiles()...)
906 } else {
907 ctx.ModuleErrorf("module %q is not a gensrcs or genrule", name)
908 }
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700909 case genHeaderDepTag, genHeaderExportDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700910 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
911 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders,
912 genRule.GeneratedSourceFiles()...)
Colin Cross5ed99c62016-11-22 12:55:55 -0800913 flags := includeDirsToFlags(genRule.GeneratedHeaderDirs())
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700914 depPaths.Flags = append(depPaths.Flags, flags)
915 if tag == genHeaderExportDepTag {
916 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700917 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps,
918 genRule.GeneratedSourceFiles()...)
Jayant Chowdhary8cede072017-04-20 06:53:59 -0700919 // Add these re-exported flags to help header-abi-dumper to infer the abi exported by a library.
920 c.sabi.Properties.ReexportedIncludeFlags = append(c.sabi.Properties.ReexportedIncludeFlags, flags)
921
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700922 }
Dan Willemsenb40aab62016-04-20 14:21:14 -0700923 } else {
924 ctx.ModuleErrorf("module %q is not a genrule", name)
925 }
926 default:
Colin Crossc99deeb2016-04-11 15:06:20 -0700927 ctx.ModuleErrorf("depends on non-cc module %q", name)
Colin Crossca860ac2016-01-04 14:34:37 -0800928 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700929 return
930 }
931
932 if !a.Enabled() {
Colin Crossa8f5e9a2016-12-13 12:51:11 -0800933 if ctx.AConfig().AllowMissingDependencies() {
934 ctx.AddMissingDependencies([]string{name})
935 } else {
936 ctx.ModuleErrorf("depends on disabled module %q", name)
937 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700938 return
939 }
940
Colin Crossa1ad8d12016-06-01 17:09:44 -0700941 if a.Target().Os != ctx.Os() {
942 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), name)
943 return
944 }
945
946 if a.Target().Arch.ArchType != ctx.Arch().ArchType {
947 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700948 return
949 }
950
Colin Crossc99deeb2016-04-11 15:06:20 -0700951 if tag == reuseObjTag {
Colin Crossbba99042016-11-23 15:45:05 -0800952 if l, ok := cc.compiler.(libraryInterface); ok {
953 depPaths.Objs = depPaths.Objs.Append(l.reuseObjs())
954 return
955 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700956 }
957
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700958 if t, ok := tag.(dependencyTag); ok && t.library {
Dan Willemsena96ff642016-06-07 12:34:45 -0700959 if i, ok := cc.linker.(exportedFlagsProducer); ok {
Dan Willemsen76f08272016-07-09 00:14:08 -0700960 flags := i.exportedFlags()
Dan Willemsen847dcc72016-09-29 12:13:36 -0700961 deps := i.exportedFlagsDeps()
Dan Willemsen76f08272016-07-09 00:14:08 -0700962 depPaths.Flags = append(depPaths.Flags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700963 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders, deps...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700964
965 if t.reexportFlags {
Dan Willemsen76f08272016-07-09 00:14:08 -0700966 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700967 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps, deps...)
Jayant Chowdhary8cede072017-04-20 06:53:59 -0700968 // Add these re-exported flags to help header-abi-dumper to infer the abi exported by a library.
969 // Re-exported flags from shared library dependencies are not included as those shared libraries
970 // will be included in the vndk set.
971 if tag == staticExportDepTag || tag == headerExportDepTag {
972 c.sabi.Properties.ReexportedIncludeFlags = append(c.sabi.Properties.ReexportedIncludeFlags, flags...)
973 }
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700974 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700975 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700976
Dan Albert9e10cd42016-08-03 14:12:14 -0700977 checkLinkType(c, cc)
Colin Crossc99deeb2016-04-11 15:06:20 -0700978 }
979
Colin Cross26c34ed2016-09-30 17:10:16 -0700980 var ptr *android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -0700981 var depPtr *android.Paths
Colin Crossc99deeb2016-04-11 15:06:20 -0700982
Colin Cross26c34ed2016-09-30 17:10:16 -0700983 linkFile := cc.outputFile
984 depFile := android.OptionalPath{}
985
Colin Crossc99deeb2016-04-11 15:06:20 -0700986 switch tag {
Dan Albert914449f2016-06-17 16:45:24 -0700987 case ndkStubDepTag, sharedDepTag, sharedExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700988 ptr = &depPaths.SharedLibs
989 depPtr = &depPaths.SharedLibsDeps
990 depFile = cc.linker.(libraryInterface).toc()
Dan Albert914449f2016-06-17 16:45:24 -0700991 case lateSharedDepTag, ndkLateStubDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700992 ptr = &depPaths.LateSharedLibs
993 depPtr = &depPaths.LateSharedLibsDeps
994 depFile = cc.linker.(libraryInterface).toc()
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700995 case staticDepTag, staticExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700996 ptr = &depPaths.StaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -0700997 case lateStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700998 ptr = &depPaths.LateStaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -0700999 case wholeStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001000 ptr = &depPaths.WholeStaticLibs
Colin Crossb916a382016-07-29 17:28:03 -07001001 staticLib, ok := cc.linker.(libraryInterface)
1002 if !ok || !staticLib.static() {
Dan Willemsena96ff642016-06-07 12:34:45 -07001003 ctx.ModuleErrorf("module %q not a static library", name)
Colin Crossc99deeb2016-04-11 15:06:20 -07001004 return
1005 }
1006
1007 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
1008 postfix := " (required by " + ctx.OtherModuleName(m) + ")"
1009 for i := range missingDeps {
1010 missingDeps[i] += postfix
1011 }
1012 ctx.AddMissingDependencies(missingDeps)
1013 }
Dan Willemsen5cb580f2016-09-26 17:33:01 -07001014 depPaths.WholeStaticLibObjs = depPaths.WholeStaticLibObjs.Append(staticLib.objs())
Colin Cross5950f382016-12-13 12:50:57 -08001015 case headerDepTag:
1016 // Nothing
Colin Crossc99deeb2016-04-11 15:06:20 -07001017 case objDepTag:
Dan Willemsen5cb580f2016-09-26 17:33:01 -07001018 depPaths.Objs.objFiles = append(depPaths.Objs.objFiles, linkFile.Path())
Colin Crossc99deeb2016-04-11 15:06:20 -07001019 case crtBeginDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001020 depPaths.CrtBegin = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -07001021 case crtEndDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001022 depPaths.CrtEnd = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -07001023 }
1024
Dan Willemsen581341d2017-02-09 16:16:31 -08001025 switch tag {
1026 case staticDepTag, staticExportDepTag, lateStaticDepTag:
1027 staticLib, ok := cc.linker.(libraryInterface)
1028 if !ok || !staticLib.static() {
1029 ctx.ModuleErrorf("module %q not a static library", name)
1030 return
1031 }
1032
1033 // When combining coverage files for shared libraries and executables, coverage files
Jayant Chowdhary756b1c22017-02-08 13:45:53 -08001034 // in static libraries act as if they were whole static libraries. The same goes for
1035 // source based Abi dump files.
Dan Willemsen581341d2017-02-09 16:16:31 -08001036 depPaths.StaticLibObjs.coverageFiles = append(depPaths.StaticLibObjs.coverageFiles,
1037 staticLib.objs().coverageFiles...)
Jayant Chowdhary756b1c22017-02-08 13:45:53 -08001038 depPaths.StaticLibObjs.sAbiDumpFiles = append(depPaths.StaticLibObjs.sAbiDumpFiles,
1039 staticLib.objs().sAbiDumpFiles...)
Dan Willemsen581341d2017-02-09 16:16:31 -08001040 }
1041
Colin Cross26c34ed2016-09-30 17:10:16 -07001042 if ptr != nil {
Colin Crossce75d2c2016-10-06 16:12:58 -07001043 if !linkFile.Valid() {
1044 ctx.ModuleErrorf("module %q missing output file", name)
1045 return
1046 }
Colin Cross26c34ed2016-09-30 17:10:16 -07001047 *ptr = append(*ptr, linkFile.Path())
1048 }
1049
Colin Crossc99deeb2016-04-11 15:06:20 -07001050 if depPtr != nil {
Colin Cross26c34ed2016-09-30 17:10:16 -07001051 dep := depFile
1052 if !dep.Valid() {
1053 dep = linkFile
1054 }
1055 *depPtr = append(*depPtr, dep.Path())
Colin Crossca860ac2016-01-04 14:34:37 -08001056 }
1057 })
1058
1059 return depPaths
1060}
1061
1062func (c *Module) InstallInData() bool {
1063 if c.installer == nil {
1064 return false
1065 }
Vishwath Mohan0d0a6f32017-03-29 22:00:18 -07001066 return c.installer.inData()
1067}
1068
1069func (c *Module) InstallInSanitizerDir() bool {
1070 if c.installer == nil {
1071 return false
1072 }
1073 if c.sanitize != nil && c.sanitize.inSanitizerDir() {
Colin Cross94610402016-08-29 13:41:32 -07001074 return true
1075 }
Vishwath Mohan0d0a6f32017-03-29 22:00:18 -07001076 return c.installer.inSanitizerDir()
Colin Crossca860ac2016-01-04 14:34:37 -08001077}
1078
Dan Willemsen4aa75ca2016-09-28 16:18:03 -07001079func (c *Module) HostToolPath() android.OptionalPath {
1080 if c.installer == nil {
1081 return android.OptionalPath{}
1082 }
1083 return c.installer.hostToolPath()
1084}
1085
Colin Cross2ba19d92015-05-07 15:44:20 -07001086//
Colin Crosscfad1192015-11-02 16:43:11 -08001087// Defaults
1088//
Colin Crossca860ac2016-01-04 14:34:37 -08001089type Defaults struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001090 android.ModuleBase
1091 android.DefaultsModule
Colin Crosscfad1192015-11-02 16:43:11 -08001092}
1093
Colin Cross635c3b02016-05-18 15:37:25 -07001094func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -08001095}
1096
Colin Cross1e676be2016-10-12 14:38:15 -07001097func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
1098}
1099
Colin Crossca860ac2016-01-04 14:34:37 -08001100func defaultsFactory() (blueprint.Module, []interface{}) {
Colin Crosse1d764e2016-08-18 14:18:32 -07001101 return DefaultsFactory()
1102}
1103
1104func DefaultsFactory(props ...interface{}) (blueprint.Module, []interface{}) {
Colin Crossca860ac2016-01-04 14:34:37 -08001105 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -08001106
Colin Crosse1d764e2016-08-18 14:18:32 -07001107 props = append(props,
Colin Crossca860ac2016-01-04 14:34:37 -08001108 &BaseProperties{},
1109 &BaseCompilerProperties{},
1110 &BaseLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -07001111 &LibraryProperties{},
Colin Cross919281a2016-04-05 16:42:05 -07001112 &FlagExporterProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001113 &BinaryLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -07001114 &TestProperties{},
1115 &TestBinaryProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001116 &UnusedProperties{},
1117 &StlProperties{},
Colin Cross16b23492016-01-06 14:41:07 -08001118 &SanitizeProperties{},
Colin Cross665dce92016-04-28 14:50:03 -07001119 &StripProperties{},
Dan Willemsen7424d612016-09-01 13:45:39 -07001120 &InstallerProperties{},
Dan Willemsena03cf6d2016-09-26 15:45:04 -07001121 &TidyProperties{},
Dan Willemsen581341d2017-02-09 16:16:31 -08001122 &CoverageProperties{},
Jayant Chowdhary756b1c22017-02-08 13:45:53 -08001123 &SAbiProperties{},
Colin Crosse1d764e2016-08-18 14:18:32 -07001124 )
Colin Crosscfad1192015-11-02 16:43:11 -08001125
Colin Crosse1d764e2016-08-18 14:18:32 -07001126 return android.InitDefaultsModule(module, module, props...)
Colin Crosscfad1192015-11-02 16:43:11 -08001127}
1128
Dan Willemsen95421a42017-04-06 12:43:22 -07001129const (
1130 // coreMode is the variant used for framework-private libraries, or
1131 // SDK libraries. (which framework-private libraries can use)
1132 coreMode = "core"
1133
1134 // vendorMode is the variant used for /vendor code that compiles
1135 // against the VNDK.
1136 vendorMode = "vendor"
1137)
1138
1139func vendorMutator(mctx android.BottomUpMutatorContext) {
1140 if mctx.Os() != android.Android {
1141 return
1142 }
1143
1144 m, ok := mctx.Module().(*Module)
1145 if !ok {
1146 return
1147 }
1148
1149 // Sanity check
1150 if Bool(m.Properties.Vendor_available) && mctx.Vendor() {
1151 mctx.PropertyErrorf("vendor_available",
1152 "doesn't make sense at the same time as `vendor: true` or `proprietary: true`")
1153 return
1154 }
1155
1156 if !mctx.DeviceConfig().CompileVndk() {
1157 // If the device isn't compiling against the VNDK, we always
1158 // use the core mode.
1159 mctx.CreateVariations(coreMode)
1160 } else if _, ok := m.linker.(*llndkStubDecorator); ok {
1161 // LL-NDK stubs only exist in the vendor variant, since the
1162 // real libraries will be used in the core variant.
1163 mctx.CreateVariations(vendorMode)
1164 } else if Bool(m.Properties.Vendor_available) {
1165 // This will be available in both /system and /vendor
1166 mod := mctx.CreateVariations(coreMode, vendorMode)
1167 mod[1].(*Module).Properties.UseVndk = true
1168 } else if mctx.Vendor() && m.Properties.Sdk_version == "" {
1169 // This will be available in /vendor only
1170 mod := mctx.CreateVariations(vendorMode)
1171 mod[0].(*Module).Properties.UseVndk = true
1172 } else {
1173 // This is either in /system (or similar: /data), or is a
1174 // modules built with the NDK. Modules built with the NDK
1175 // will be restricted using the existing link type checks.
1176 mctx.CreateVariations(coreMode)
1177 }
1178}
1179
Colin Cross74d1ec02015-04-28 13:30:13 -07001180// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
1181// modifies the slice contents in place, and returns a subslice of the original slice
1182func lastUniqueElements(list []string) []string {
1183 totalSkip := 0
1184 for i := len(list) - 1; i >= totalSkip; i-- {
1185 skip := 0
1186 for j := i - 1; j >= totalSkip; j-- {
1187 if list[i] == list[j] {
1188 skip++
1189 } else {
1190 list[j+skip] = list[j]
1191 }
1192 }
1193 totalSkip += skip
1194 }
1195 return list[totalSkip:]
1196}
Colin Cross06a931b2015-10-28 17:23:31 -07001197
1198var Bool = proptools.Bool