blob: 6e2f6a503a4dc87d1275623351fdeef176965644 [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 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 Chowdhary3e231fd2017-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 Crossc3199482017-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 Chowdhary3e231fd2017-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 Willemsen4416e5d2017-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 Willemsen4416e5d2017-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 Chowdhary3e231fd2017-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 Mohan1dd88392017-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 Chowdhary3e231fd2017-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 Chowdhary3e231fd2017-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 Willemsen4416e5d2017-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 Willemsen4416e5d2017-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 Willemsen4416e5d2017-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 Willemsen11b26142017-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 Willemsen4416e5d2017-04-06 12:43:22 -0700426 return ctx.mod.vndk()
Dan Willemsend2ede872016-11-18 14:54:24 -0800427}
428
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800429// Create source abi dumps if the module belongs to the list of VndkLibraries.
430func (ctx *moduleContextImpl) createVndkSourceAbiDump() bool {
431 return ctx.ctx.Device() && (inList(ctx.baseModuleName(), config.LLndkLibraries())) ||
432 (inList(ctx.baseModuleName(), config.VndkLibraries()))
433}
434
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700435func (ctx *moduleContextImpl) selectedStl() string {
436 if stl := ctx.mod.stl; stl != nil {
437 return stl.Properties.SelectedStl
438 }
439 return ""
440}
441
Colin Crossce75d2c2016-10-06 16:12:58 -0700442func (ctx *moduleContextImpl) baseModuleName() string {
443 return ctx.mod.ModuleBase.BaseModuleName()
444}
445
Colin Cross635c3b02016-05-18 15:37:25 -0700446func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800447 return &Module{
448 hod: hod,
449 multilib: multilib,
450 }
451}
452
Colin Cross635c3b02016-05-18 15:37:25 -0700453func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800454 module := newBaseModule(hod, multilib)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700455 module.features = []feature{
456 &tidyFeature{},
457 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700458 module.stl = &stl{}
Colin Cross16b23492016-01-06 14:41:07 -0800459 module.sanitize = &sanitize{}
Dan Willemsen581341d2017-02-09 16:16:31 -0800460 module.coverage = &coverage{}
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800461 module.sabi = &sabi{}
Colin Crossca860ac2016-01-04 14:34:37 -0800462 return module
463}
464
Colin Crossce75d2c2016-10-06 16:12:58 -0700465func (c *Module) Prebuilt() *android.Prebuilt {
466 if p, ok := c.linker.(prebuiltLinkerInterface); ok {
467 return p.prebuilt()
468 }
469 return nil
470}
471
472func (c *Module) Name() string {
473 name := c.ModuleBase.Name()
Dan Willemsen01a90592017-04-07 15:21:13 -0700474 if p, ok := c.linker.(interface {
475 Name(string) string
476 }); ok {
Colin Crossce75d2c2016-10-06 16:12:58 -0700477 name = p.Name(name)
478 }
479 return name
480}
481
Colin Cross635c3b02016-05-18 15:37:25 -0700482func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800483 ctx := &moduleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700484 ModuleContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800485 moduleContextImpl: moduleContextImpl{
486 mod: c,
487 },
488 }
489 ctx.ctx = ctx
490
491 flags := Flags{
492 Toolchain: c.toolchain(ctx),
493 Clang: c.clang(ctx),
494 }
Colin Crossca860ac2016-01-04 14:34:37 -0800495 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700496 flags = c.compiler.compilerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800497 }
498 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700499 flags = c.linker.linkerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800500 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700501 if c.stl != nil {
502 flags = c.stl.flags(ctx, flags)
503 }
Colin Cross16b23492016-01-06 14:41:07 -0800504 if c.sanitize != nil {
505 flags = c.sanitize.flags(ctx, flags)
506 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800507 if c.coverage != nil {
508 flags = c.coverage.flags(ctx, flags)
509 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800510 if c.sabi != nil {
511 flags = c.sabi.flags(ctx, flags)
512 }
Colin Crossca860ac2016-01-04 14:34:37 -0800513 for _, feature := range c.features {
514 flags = feature.flags(ctx, flags)
515 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800516 if ctx.Failed() {
517 return
518 }
519
Colin Crossb98c8b02016-07-29 13:44:28 -0700520 flags.CFlags, _ = filterList(flags.CFlags, config.IllegalFlags)
521 flags.CppFlags, _ = filterList(flags.CppFlags, config.IllegalFlags)
522 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, config.IllegalFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800523
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800524 deps := c.depsToPaths(ctx)
525 if ctx.Failed() {
526 return
527 }
528 flags.GlobalFlags = append(flags.GlobalFlags, deps.Flags...)
529 c.flags = flags
530
Colin Crossca860ac2016-01-04 14:34:37 -0800531 // Optimization to reduce size of build.ninja
532 // Replace the long list of flags for each file with a module-local variable
533 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
534 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
535 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
536 flags.CFlags = []string{"$cflags"}
537 flags.CppFlags = []string{"$cppflags"}
538 flags.AsFlags = []string{"$asflags"}
539
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700540 var objs Objects
Colin Crossca860ac2016-01-04 14:34:37 -0800541 if c.compiler != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700542 objs = c.compiler.compile(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -0800543 if ctx.Failed() {
544 return
545 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800546 }
547
Colin Crossca860ac2016-01-04 14:34:37 -0800548 if c.linker != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700549 outputFile := c.linker.link(ctx, flags, deps, objs)
Colin Crossca860ac2016-01-04 14:34:37 -0800550 if ctx.Failed() {
551 return
552 }
Colin Cross635c3b02016-05-18 15:37:25 -0700553 c.outputFile = android.OptionalPathForPath(outputFile)
Colin Crossce75d2c2016-10-06 16:12:58 -0700554 }
Colin Cross5049f022015-03-18 13:28:46 -0700555
Colin Crossce75d2c2016-10-06 16:12:58 -0700556 if c.installer != nil && !c.Properties.PreventInstall && c.outputFile.Valid() {
557 c.installer.install(ctx, c.outputFile.Path())
558 if ctx.Failed() {
559 return
Colin Crossca860ac2016-01-04 14:34:37 -0800560 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700561 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800562}
563
Colin Crossb98c8b02016-07-29 13:44:28 -0700564func (c *Module) toolchain(ctx BaseModuleContext) config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800565 if c.cachedToolchain == nil {
Colin Crossb98c8b02016-07-29 13:44:28 -0700566 c.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
Colin Cross3f40fa42015-01-30 17:27:36 -0800567 }
Colin Crossca860ac2016-01-04 14:34:37 -0800568 return c.cachedToolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800569}
570
Colin Crossca860ac2016-01-04 14:34:37 -0800571func (c *Module) begin(ctx BaseModuleContext) {
572 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700573 c.compiler.compilerInit(ctx)
Colin Cross21b9a242015-03-24 14:15:58 -0700574 }
Colin Crossca860ac2016-01-04 14:34:37 -0800575 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700576 c.linker.linkerInit(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800577 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700578 if c.stl != nil {
579 c.stl.begin(ctx)
580 }
Colin Cross16b23492016-01-06 14:41:07 -0800581 if c.sanitize != nil {
582 c.sanitize.begin(ctx)
583 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800584 if c.coverage != nil {
585 c.coverage.begin(ctx)
586 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800587 if c.sabi != nil {
588 c.sabi.begin(ctx)
589 }
Colin Crossca860ac2016-01-04 14:34:37 -0800590 for _, feature := range c.features {
591 feature.begin(ctx)
592 }
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700593 if ctx.sdk() {
594 version, err := normalizeNdkApiLevel(ctx.sdkVersion(), ctx.Arch())
595 if err != nil {
596 ctx.PropertyErrorf("sdk_version", err.Error())
597 }
Dan Albert90f7a4d2016-11-08 14:34:24 -0800598 c.Properties.Sdk_version = version
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700599 }
Colin Crossca860ac2016-01-04 14:34:37 -0800600}
601
Colin Cross37047f12016-12-13 17:06:13 -0800602func (c *Module) deps(ctx DepsContext) Deps {
Colin Crossc99deeb2016-04-11 15:06:20 -0700603 deps := Deps{}
604
605 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700606 deps = c.compiler.compilerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700607 }
608 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700609 deps = c.linker.linkerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700610 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700611 if c.stl != nil {
612 deps = c.stl.deps(ctx, deps)
613 }
Colin Cross16b23492016-01-06 14:41:07 -0800614 if c.sanitize != nil {
615 deps = c.sanitize.deps(ctx, deps)
616 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800617 if c.coverage != nil {
618 deps = c.coverage.deps(ctx, deps)
619 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800620 if c.sabi != nil {
621 deps = c.sabi.deps(ctx, deps)
622 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700623 for _, feature := range c.features {
624 deps = feature.deps(ctx, deps)
625 }
626
627 deps.WholeStaticLibs = lastUniqueElements(deps.WholeStaticLibs)
628 deps.StaticLibs = lastUniqueElements(deps.StaticLibs)
629 deps.LateStaticLibs = lastUniqueElements(deps.LateStaticLibs)
630 deps.SharedLibs = lastUniqueElements(deps.SharedLibs)
631 deps.LateSharedLibs = lastUniqueElements(deps.LateSharedLibs)
Colin Cross5950f382016-12-13 12:50:57 -0800632 deps.HeaderLibs = lastUniqueElements(deps.HeaderLibs)
Colin Crossc99deeb2016-04-11 15:06:20 -0700633
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700634 for _, lib := range deps.ReexportSharedLibHeaders {
635 if !inList(lib, deps.SharedLibs) {
636 ctx.PropertyErrorf("export_shared_lib_headers", "Shared library not in shared_libs: '%s'", lib)
637 }
638 }
639
640 for _, lib := range deps.ReexportStaticLibHeaders {
641 if !inList(lib, deps.StaticLibs) {
642 ctx.PropertyErrorf("export_static_lib_headers", "Static library not in static_libs: '%s'", lib)
643 }
644 }
645
Colin Cross5950f382016-12-13 12:50:57 -0800646 for _, lib := range deps.ReexportHeaderLibHeaders {
647 if !inList(lib, deps.HeaderLibs) {
648 ctx.PropertyErrorf("export_header_lib_headers", "Header library not in header_libs: '%s'", lib)
649 }
650 }
651
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700652 for _, gen := range deps.ReexportGeneratedHeaders {
653 if !inList(gen, deps.GeneratedHeaders) {
654 ctx.PropertyErrorf("export_generated_headers", "Generated header module not in generated_headers: '%s'", gen)
655 }
656 }
657
Colin Crossc99deeb2016-04-11 15:06:20 -0700658 return deps
659}
660
Dan Albert7e9d2952016-08-04 13:02:36 -0700661func (c *Module) beginMutator(actx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800662 ctx := &baseModuleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700663 BaseContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800664 moduleContextImpl: moduleContextImpl{
665 mod: c,
666 },
667 }
668 ctx.ctx = ctx
669
Colin Crossca860ac2016-01-04 14:34:37 -0800670 c.begin(ctx)
Dan Albert7e9d2952016-08-04 13:02:36 -0700671}
672
Colin Cross1e676be2016-10-12 14:38:15 -0700673func (c *Module) DepsMutator(actx android.BottomUpMutatorContext) {
674 if !c.Enabled() {
675 return
676 }
677
Colin Cross37047f12016-12-13 17:06:13 -0800678 ctx := &depsContext{
679 BottomUpMutatorContext: actx,
Dan Albert7e9d2952016-08-04 13:02:36 -0700680 moduleContextImpl: moduleContextImpl{
681 mod: c,
682 },
683 }
684 ctx.ctx = ctx
Colin Crossca860ac2016-01-04 14:34:37 -0800685
Colin Crossc99deeb2016-04-11 15:06:20 -0700686 deps := c.deps(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800687
Colin Crossb5bc4b42016-07-11 16:11:59 -0700688 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.SharedLibs...)
689 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.LateSharedLibs...)
Dan Willemsen72d39932016-07-08 23:23:48 -0700690
Dan Albert914449f2016-06-17 16:45:24 -0700691 variantNdkLibs := []string{}
692 variantLateNdkLibs := []string{}
Dan Willemsenb916b802017-03-19 13:44:32 -0700693 if ctx.Os() == android.Android {
Dan Albert914449f2016-06-17 16:45:24 -0700694 version := ctx.sdkVersion()
Dan Willemsen72d39932016-07-08 23:23:48 -0700695
Dan Albert914449f2016-06-17 16:45:24 -0700696 // Rewrites the names of shared libraries into the names of the NDK
697 // libraries where appropriate. This returns two slices.
698 //
699 // The first is a list of non-variant shared libraries (either rewritten
700 // NDK libraries to the modules in prebuilts/ndk, or not rewritten
701 // because they are not NDK libraries).
702 //
703 // The second is a list of ndk_library modules. These need to be
704 // separated because they are a variation dependency and must be added
705 // in a different manner.
706 rewriteNdkLibs := func(list []string) ([]string, []string) {
707 variantLibs := []string{}
708 nonvariantLibs := []string{}
709 for _, entry := range list {
Dan Willemsenb916b802017-03-19 13:44:32 -0700710 if ctx.sdk() && inList(entry, ndkPrebuiltSharedLibraries) {
Dan Albert914449f2016-06-17 16:45:24 -0700711 if !inList(entry, ndkMigratedLibs) {
712 nonvariantLibs = append(nonvariantLibs, entry+".ndk."+version)
713 } else {
714 variantLibs = append(variantLibs, entry+ndkLibrarySuffix)
715 }
Dan Willemsenb916b802017-03-19 13:44:32 -0700716 } else if ctx.vndk() && inList(entry, config.LLndkLibraries()) {
717 nonvariantLibs = append(nonvariantLibs, entry+llndkLibrarySuffix)
Dan Albert914449f2016-06-17 16:45:24 -0700718 } else {
Dan Willemsen7cbf5f82017-03-28 00:08:30 -0700719 nonvariantLibs = append(nonvariantLibs, entry)
Dan Willemsen72d39932016-07-08 23:23:48 -0700720 }
721 }
Dan Albert914449f2016-06-17 16:45:24 -0700722 return nonvariantLibs, variantLibs
Dan Willemsen72d39932016-07-08 23:23:48 -0700723 }
724
Dan Albert914449f2016-06-17 16:45:24 -0700725 deps.SharedLibs, variantNdkLibs = rewriteNdkLibs(deps.SharedLibs)
726 deps.LateSharedLibs, variantLateNdkLibs = rewriteNdkLibs(deps.LateSharedLibs)
Dan Willemsen72d39932016-07-08 23:23:48 -0700727 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700728
Colin Cross32ec36c2016-12-15 07:39:51 -0800729 for _, lib := range deps.HeaderLibs {
730 depTag := headerDepTag
731 if inList(lib, deps.ReexportHeaderLibHeaders) {
732 depTag = headerExportDepTag
733 }
734 actx.AddVariationDependencies(nil, depTag, lib)
735 }
Colin Cross5950f382016-12-13 12:50:57 -0800736
Colin Crossc99deeb2016-04-11 15:06:20 -0700737 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, wholeStaticDepTag,
738 deps.WholeStaticLibs...)
739
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700740 for _, lib := range deps.StaticLibs {
741 depTag := staticDepTag
742 if inList(lib, deps.ReexportStaticLibHeaders) {
743 depTag = staticExportDepTag
744 }
Colin Cross15a0d462016-07-14 14:49:58 -0700745 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700746 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700747
748 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, lateStaticDepTag,
749 deps.LateStaticLibs...)
750
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700751 for _, lib := range deps.SharedLibs {
752 depTag := sharedDepTag
753 if inList(lib, deps.ReexportSharedLibHeaders) {
754 depTag = sharedExportDepTag
755 }
Colin Cross15a0d462016-07-14 14:49:58 -0700756 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700757 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700758
759 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, lateSharedDepTag,
760 deps.LateSharedLibs...)
761
Colin Cross68861832016-07-08 10:41:41 -0700762 actx.AddDependency(c, genSourceDepTag, deps.GeneratedSources...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700763
764 for _, gen := range deps.GeneratedHeaders {
765 depTag := genHeaderDepTag
766 if inList(gen, deps.ReexportGeneratedHeaders) {
767 depTag = genHeaderExportDepTag
768 }
769 actx.AddDependency(c, depTag, gen)
770 }
Dan Willemsenb40aab62016-04-20 14:21:14 -0700771
Colin Cross68861832016-07-08 10:41:41 -0700772 actx.AddDependency(c, objDepTag, deps.ObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700773
774 if deps.CrtBegin != "" {
Colin Cross68861832016-07-08 10:41:41 -0700775 actx.AddDependency(c, crtBeginDepTag, deps.CrtBegin)
Colin Crossca860ac2016-01-04 14:34:37 -0800776 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700777 if deps.CrtEnd != "" {
Colin Cross68861832016-07-08 10:41:41 -0700778 actx.AddDependency(c, crtEndDepTag, deps.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700779 }
Dan Albert914449f2016-06-17 16:45:24 -0700780
781 version := ctx.sdkVersion()
782 actx.AddVariationDependencies([]blueprint.Variation{
783 {"ndk_api", version}, {"link", "shared"}}, ndkStubDepTag, variantNdkLibs...)
784 actx.AddVariationDependencies([]blueprint.Variation{
785 {"ndk_api", version}, {"link", "shared"}}, ndkLateStubDepTag, variantLateNdkLibs...)
Colin Cross6362e272015-10-29 15:25:03 -0700786}
Colin Cross21b9a242015-03-24 14:15:58 -0700787
Dan Albert7e9d2952016-08-04 13:02:36 -0700788func beginMutator(ctx android.BottomUpMutatorContext) {
789 if c, ok := ctx.Module().(*Module); ok && c.Enabled() {
790 c.beginMutator(ctx)
791 }
792}
793
Colin Crossca860ac2016-01-04 14:34:37 -0800794func (c *Module) clang(ctx BaseModuleContext) bool {
795 clang := Bool(c.Properties.Clang)
796
797 if c.Properties.Clang == nil {
798 if ctx.Host() {
799 clang = true
800 }
801
802 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
803 clang = true
804 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800805 }
Colin Cross28344522015-04-22 13:07:53 -0700806
Colin Crossca860ac2016-01-04 14:34:37 -0800807 if !c.toolchain(ctx).ClangSupported() {
808 clang = false
809 }
810
811 return clang
812}
813
Colin Crossc99deeb2016-04-11 15:06:20 -0700814// Convert dependencies to paths. Returns a PathDeps containing paths
Colin Cross635c3b02016-05-18 15:37:25 -0700815func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
Colin Crossca860ac2016-01-04 14:34:37 -0800816 var depPaths PathDeps
Colin Crossca860ac2016-01-04 14:34:37 -0800817
Dan Willemsena96ff642016-06-07 12:34:45 -0700818 // Whether a module can link to another module, taking into
819 // account NDK linking.
Dan Albert9e10cd42016-08-03 14:12:14 -0700820 checkLinkType := func(from, to *Module) {
Dan Willemsena96ff642016-06-07 12:34:45 -0700821 if from.Target().Os != android.Android {
822 // Host code is not restricted
Dan Albert9e10cd42016-08-03 14:12:14 -0700823 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700824 }
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700825 if from.Properties.UseVndk {
826 // Vendor code is already limited by the vendor mutator
827 return
828 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700829 if from.Properties.Sdk_version == "" {
830 // Platform code can link to anything
Dan Albert9e10cd42016-08-03 14:12:14 -0700831 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700832 }
Colin Crossb916a382016-07-29 17:28:03 -0700833 if _, ok := to.linker.(*toolchainLibraryDecorator); ok {
Dan Willemsena96ff642016-06-07 12:34:45 -0700834 // These are always allowed
Dan Albert9e10cd42016-08-03 14:12:14 -0700835 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700836 }
837 if _, ok := to.linker.(*ndkPrebuiltLibraryLinker); ok {
838 // These are allowed, but don't set sdk_version
Dan Albert9e10cd42016-08-03 14:12:14 -0700839 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700840 }
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700841 if _, ok := to.linker.(*ndkPrebuiltStlLinker); ok {
842 // These are allowed, but don't set sdk_version
Dan Albert9e10cd42016-08-03 14:12:14 -0700843 return
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700844 }
Colin Crossb916a382016-07-29 17:28:03 -0700845 if _, ok := to.linker.(*stubDecorator); ok {
Dan Albert914449f2016-06-17 16:45:24 -0700846 // These aren't real libraries, but are the stub shared libraries that are included in
847 // the NDK.
Dan Albert9e10cd42016-08-03 14:12:14 -0700848 return
Dan Albert914449f2016-06-17 16:45:24 -0700849 }
Dan Albert9e10cd42016-08-03 14:12:14 -0700850 if to.Properties.Sdk_version == "" {
851 // NDK code linking to platform code is never okay.
852 ctx.ModuleErrorf("depends on non-NDK-built library %q",
853 ctx.OtherModuleName(to))
854 }
855
856 // All this point we know we have two NDK libraries, but we need to
857 // check that we're not linking against anything built against a higher
858 // API level, as it is only valid to link against older or equivalent
859 // APIs.
860
861 if from.Properties.Sdk_version == "current" {
862 // Current can link against anything.
863 return
864 } else if to.Properties.Sdk_version == "current" {
865 // Current can't be linked against by anything else.
866 ctx.ModuleErrorf("links %q built against newer API version %q",
867 ctx.OtherModuleName(to), "current")
868 }
869
870 fromApi, err := strconv.Atoi(from.Properties.Sdk_version)
871 if err != nil {
872 ctx.PropertyErrorf("sdk_version",
873 "Invalid sdk_version value (must be int): %q",
874 from.Properties.Sdk_version)
875 }
876 toApi, err := strconv.Atoi(to.Properties.Sdk_version)
877 if err != nil {
878 ctx.PropertyErrorf("sdk_version",
879 "Invalid sdk_version value (must be int): %q",
880 to.Properties.Sdk_version)
881 }
882
883 if toApi > fromApi {
884 ctx.ModuleErrorf("links %q built against newer API version %q",
885 ctx.OtherModuleName(to), to.Properties.Sdk_version)
886 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700887 }
888
Colin Crossc99deeb2016-04-11 15:06:20 -0700889 ctx.VisitDirectDeps(func(m blueprint.Module) {
890 name := ctx.OtherModuleName(m)
891 tag := ctx.OtherModuleDependencyTag(m)
Colin Crossca860ac2016-01-04 14:34:37 -0800892
Colin Cross635c3b02016-05-18 15:37:25 -0700893 a, _ := m.(android.Module)
Colin Crossc99deeb2016-04-11 15:06:20 -0700894 if a == nil {
895 ctx.ModuleErrorf("module %q not an android module", name)
896 return
Colin Crossca860ac2016-01-04 14:34:37 -0800897 }
Colin Crossca860ac2016-01-04 14:34:37 -0800898
Dan Willemsena96ff642016-06-07 12:34:45 -0700899 cc, _ := m.(*Module)
900 if cc == nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700901 switch tag {
Colin Cross068e0fe2016-12-13 15:23:47 -0800902 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700903 case genSourceDepTag:
904 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
905 depPaths.GeneratedSources = append(depPaths.GeneratedSources,
906 genRule.GeneratedSourceFiles()...)
907 } else {
908 ctx.ModuleErrorf("module %q is not a gensrcs or genrule", name)
909 }
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700910 case genHeaderDepTag, genHeaderExportDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700911 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
912 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders,
913 genRule.GeneratedSourceFiles()...)
Colin Cross5ed99c62016-11-22 12:55:55 -0800914 flags := includeDirsToFlags(genRule.GeneratedHeaderDirs())
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700915 depPaths.Flags = append(depPaths.Flags, flags)
916 if tag == genHeaderExportDepTag {
917 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700918 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps,
919 genRule.GeneratedSourceFiles()...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700920 }
Dan Willemsenb40aab62016-04-20 14:21:14 -0700921 } else {
922 ctx.ModuleErrorf("module %q is not a genrule", name)
923 }
924 default:
Colin Crossc99deeb2016-04-11 15:06:20 -0700925 ctx.ModuleErrorf("depends on non-cc module %q", name)
Colin Crossca860ac2016-01-04 14:34:37 -0800926 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700927 return
928 }
929
930 if !a.Enabled() {
Colin Crossa8f5e9a2016-12-13 12:51:11 -0800931 if ctx.AConfig().AllowMissingDependencies() {
932 ctx.AddMissingDependencies([]string{name})
933 } else {
934 ctx.ModuleErrorf("depends on disabled module %q", name)
935 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700936 return
937 }
938
Colin Crossa1ad8d12016-06-01 17:09:44 -0700939 if a.Target().Os != ctx.Os() {
940 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), name)
941 return
942 }
943
944 if a.Target().Arch.ArchType != ctx.Arch().ArchType {
945 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700946 return
947 }
948
Colin Crossc99deeb2016-04-11 15:06:20 -0700949 if tag == reuseObjTag {
Colin Crossbba99042016-11-23 15:45:05 -0800950 if l, ok := cc.compiler.(libraryInterface); ok {
951 depPaths.Objs = depPaths.Objs.Append(l.reuseObjs())
952 return
953 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700954 }
955
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700956 if t, ok := tag.(dependencyTag); ok && t.library {
Dan Willemsena96ff642016-06-07 12:34:45 -0700957 if i, ok := cc.linker.(exportedFlagsProducer); ok {
Dan Willemsen76f08272016-07-09 00:14:08 -0700958 flags := i.exportedFlags()
Dan Willemsen847dcc72016-09-29 12:13:36 -0700959 deps := i.exportedFlagsDeps()
Dan Willemsen76f08272016-07-09 00:14:08 -0700960 depPaths.Flags = append(depPaths.Flags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700961 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders, deps...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700962
963 if t.reexportFlags {
Dan Willemsen76f08272016-07-09 00:14:08 -0700964 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700965 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps, deps...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700966 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700967 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700968
Dan Albert9e10cd42016-08-03 14:12:14 -0700969 checkLinkType(c, cc)
Colin Crossc99deeb2016-04-11 15:06:20 -0700970 }
971
Colin Cross26c34ed2016-09-30 17:10:16 -0700972 var ptr *android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -0700973 var depPtr *android.Paths
Colin Crossc99deeb2016-04-11 15:06:20 -0700974
Colin Cross26c34ed2016-09-30 17:10:16 -0700975 linkFile := cc.outputFile
976 depFile := android.OptionalPath{}
977
Colin Crossc99deeb2016-04-11 15:06:20 -0700978 switch tag {
Dan Albert914449f2016-06-17 16:45:24 -0700979 case ndkStubDepTag, sharedDepTag, sharedExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700980 ptr = &depPaths.SharedLibs
981 depPtr = &depPaths.SharedLibsDeps
982 depFile = cc.linker.(libraryInterface).toc()
Dan Albert914449f2016-06-17 16:45:24 -0700983 case lateSharedDepTag, ndkLateStubDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700984 ptr = &depPaths.LateSharedLibs
985 depPtr = &depPaths.LateSharedLibsDeps
986 depFile = cc.linker.(libraryInterface).toc()
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700987 case staticDepTag, staticExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700988 ptr = &depPaths.StaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -0700989 case lateStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700990 ptr = &depPaths.LateStaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -0700991 case wholeStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700992 ptr = &depPaths.WholeStaticLibs
Colin Crossb916a382016-07-29 17:28:03 -0700993 staticLib, ok := cc.linker.(libraryInterface)
994 if !ok || !staticLib.static() {
Dan Willemsena96ff642016-06-07 12:34:45 -0700995 ctx.ModuleErrorf("module %q not a static library", name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700996 return
997 }
998
999 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
1000 postfix := " (required by " + ctx.OtherModuleName(m) + ")"
1001 for i := range missingDeps {
1002 missingDeps[i] += postfix
1003 }
1004 ctx.AddMissingDependencies(missingDeps)
1005 }
Dan Willemsen5cb580f2016-09-26 17:33:01 -07001006 depPaths.WholeStaticLibObjs = depPaths.WholeStaticLibObjs.Append(staticLib.objs())
Colin Cross5950f382016-12-13 12:50:57 -08001007 case headerDepTag:
1008 // Nothing
Colin Crossc99deeb2016-04-11 15:06:20 -07001009 case objDepTag:
Dan Willemsen5cb580f2016-09-26 17:33:01 -07001010 depPaths.Objs.objFiles = append(depPaths.Objs.objFiles, linkFile.Path())
Colin Crossc99deeb2016-04-11 15:06:20 -07001011 case crtBeginDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001012 depPaths.CrtBegin = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -07001013 case crtEndDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001014 depPaths.CrtEnd = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -07001015 }
1016
Dan Willemsen581341d2017-02-09 16:16:31 -08001017 switch tag {
1018 case staticDepTag, staticExportDepTag, lateStaticDepTag:
1019 staticLib, ok := cc.linker.(libraryInterface)
1020 if !ok || !staticLib.static() {
1021 ctx.ModuleErrorf("module %q not a static library", name)
1022 return
1023 }
1024
1025 // When combining coverage files for shared libraries and executables, coverage files
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001026 // in static libraries act as if they were whole static libraries. The same goes for
1027 // source based Abi dump files.
Dan Willemsen581341d2017-02-09 16:16:31 -08001028 depPaths.StaticLibObjs.coverageFiles = append(depPaths.StaticLibObjs.coverageFiles,
1029 staticLib.objs().coverageFiles...)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001030 depPaths.StaticLibObjs.sAbiDumpFiles = append(depPaths.StaticLibObjs.sAbiDumpFiles,
1031 staticLib.objs().sAbiDumpFiles...)
Dan Willemsen581341d2017-02-09 16:16:31 -08001032 }
1033
Colin Cross26c34ed2016-09-30 17:10:16 -07001034 if ptr != nil {
Colin Crossce75d2c2016-10-06 16:12:58 -07001035 if !linkFile.Valid() {
1036 ctx.ModuleErrorf("module %q missing output file", name)
1037 return
1038 }
Colin Cross26c34ed2016-09-30 17:10:16 -07001039 *ptr = append(*ptr, linkFile.Path())
1040 }
1041
Colin Crossc99deeb2016-04-11 15:06:20 -07001042 if depPtr != nil {
Colin Cross26c34ed2016-09-30 17:10:16 -07001043 dep := depFile
1044 if !dep.Valid() {
1045 dep = linkFile
1046 }
1047 *depPtr = append(*depPtr, dep.Path())
Colin Crossca860ac2016-01-04 14:34:37 -08001048 }
1049 })
1050
1051 return depPaths
1052}
1053
1054func (c *Module) InstallInData() bool {
1055 if c.installer == nil {
1056 return false
1057 }
Vishwath Mohan1dd88392017-03-29 22:00:18 -07001058 return c.installer.inData()
1059}
1060
1061func (c *Module) InstallInSanitizerDir() bool {
1062 if c.installer == nil {
1063 return false
1064 }
1065 if c.sanitize != nil && c.sanitize.inSanitizerDir() {
Colin Cross94610402016-08-29 13:41:32 -07001066 return true
1067 }
Vishwath Mohan1dd88392017-03-29 22:00:18 -07001068 return c.installer.inSanitizerDir()
Colin Crossca860ac2016-01-04 14:34:37 -08001069}
1070
Dan Willemsen4aa75ca2016-09-28 16:18:03 -07001071func (c *Module) HostToolPath() android.OptionalPath {
1072 if c.installer == nil {
1073 return android.OptionalPath{}
1074 }
1075 return c.installer.hostToolPath()
1076}
1077
Colin Cross2ba19d92015-05-07 15:44:20 -07001078//
Colin Crosscfad1192015-11-02 16:43:11 -08001079// Defaults
1080//
Colin Crossca860ac2016-01-04 14:34:37 -08001081type Defaults struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001082 android.ModuleBase
1083 android.DefaultsModule
Colin Crosscfad1192015-11-02 16:43:11 -08001084}
1085
Colin Cross635c3b02016-05-18 15:37:25 -07001086func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -08001087}
1088
Colin Cross1e676be2016-10-12 14:38:15 -07001089func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
1090}
1091
Colin Crossca860ac2016-01-04 14:34:37 -08001092func defaultsFactory() (blueprint.Module, []interface{}) {
Colin Crosse1d764e2016-08-18 14:18:32 -07001093 return DefaultsFactory()
1094}
1095
1096func DefaultsFactory(props ...interface{}) (blueprint.Module, []interface{}) {
Colin Crossca860ac2016-01-04 14:34:37 -08001097 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -08001098
Colin Crosse1d764e2016-08-18 14:18:32 -07001099 props = append(props,
Colin Crossca860ac2016-01-04 14:34:37 -08001100 &BaseProperties{},
1101 &BaseCompilerProperties{},
1102 &BaseLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -07001103 &LibraryProperties{},
Colin Cross919281a2016-04-05 16:42:05 -07001104 &FlagExporterProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001105 &BinaryLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -07001106 &TestProperties{},
1107 &TestBinaryProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001108 &UnusedProperties{},
1109 &StlProperties{},
Colin Cross16b23492016-01-06 14:41:07 -08001110 &SanitizeProperties{},
Colin Cross665dce92016-04-28 14:50:03 -07001111 &StripProperties{},
Dan Willemsen7424d612016-09-01 13:45:39 -07001112 &InstallerProperties{},
Dan Willemsena03cf6d2016-09-26 15:45:04 -07001113 &TidyProperties{},
Dan Willemsen581341d2017-02-09 16:16:31 -08001114 &CoverageProperties{},
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001115 &SAbiProperties{},
Colin Crosse1d764e2016-08-18 14:18:32 -07001116 )
Colin Crosscfad1192015-11-02 16:43:11 -08001117
Colin Crosse1d764e2016-08-18 14:18:32 -07001118 return android.InitDefaultsModule(module, module, props...)
Colin Crosscfad1192015-11-02 16:43:11 -08001119}
1120
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001121const (
1122 // coreMode is the variant used for framework-private libraries, or
1123 // SDK libraries. (which framework-private libraries can use)
1124 coreMode = "core"
1125
1126 // vendorMode is the variant used for /vendor code that compiles
1127 // against the VNDK.
1128 vendorMode = "vendor"
1129)
1130
1131func vendorMutator(mctx android.BottomUpMutatorContext) {
1132 if mctx.Os() != android.Android {
1133 return
1134 }
1135
1136 m, ok := mctx.Module().(*Module)
1137 if !ok {
1138 return
1139 }
1140
1141 // Sanity check
1142 if Bool(m.Properties.Vendor_available) && mctx.Vendor() {
1143 mctx.PropertyErrorf("vendor_available",
1144 "doesn't make sense at the same time as `vendor: true` or `proprietary: true`")
1145 return
1146 }
1147
1148 if !mctx.DeviceConfig().CompileVndk() {
1149 // If the device isn't compiling against the VNDK, we always
1150 // use the core mode.
1151 mctx.CreateVariations(coreMode)
1152 } else if _, ok := m.linker.(*llndkStubDecorator); ok {
1153 // LL-NDK stubs only exist in the vendor variant, since the
1154 // real libraries will be used in the core variant.
1155 mctx.CreateVariations(vendorMode)
1156 } else if Bool(m.Properties.Vendor_available) {
1157 // This will be available in both /system and /vendor
1158 mod := mctx.CreateVariations(coreMode, vendorMode)
1159 mod[1].(*Module).Properties.UseVndk = true
1160 } else if mctx.Vendor() && m.Properties.Sdk_version == "" {
1161 // This will be available in /vendor only
1162 mod := mctx.CreateVariations(vendorMode)
1163 mod[0].(*Module).Properties.UseVndk = true
1164 } else {
1165 // This is either in /system (or similar: /data), or is a
1166 // modules built with the NDK. Modules built with the NDK
1167 // will be restricted using the existing link type checks.
1168 mctx.CreateVariations(coreMode)
1169 }
1170}
1171
Colin Cross74d1ec02015-04-28 13:30:13 -07001172// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
1173// modifies the slice contents in place, and returns a subslice of the original slice
1174func lastUniqueElements(list []string) []string {
1175 totalSkip := 0
1176 for i := len(list) - 1; i >= totalSkip; i-- {
1177 skip := 0
1178 for j := i - 1; j >= totalSkip; j-- {
1179 if list[i] == list[j] {
1180 skip++
1181 } else {
1182 list[j+skip] = list[j]
1183 }
1184 }
1185 totalSkip += skip
1186 }
1187 return list[totalSkip:]
1188}
Colin Cross06a931b2015-10-28 17:23:31 -07001189
1190var Bool = proptools.Bool