blob: 8b362a39ee0b664bca57421fcc9ce0c41c81440b [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()
Jiyong Parkd5b18a52017-08-03 21:22:50 +090038 ctx.BottomUp("vndk", vndkMutator).Parallel()
Dan Willemsen4416e5d2017-04-06 12:43:22 -070039 ctx.BottomUp("image", vendorMutator).Parallel()
Colin Cross1e676be2016-10-12 14:38:15 -070040 ctx.BottomUp("ndk_api", ndkApiMutator).Parallel()
41 ctx.BottomUp("test_per_src", testPerSrcMutator).Parallel()
42 ctx.BottomUp("begin", beginMutator).Parallel()
43 })
Colin Cross16b23492016-01-06 14:41:07 -080044
Colin Cross1e676be2016-10-12 14:38:15 -070045 android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
46 ctx.TopDown("asan_deps", sanitizerDepsMutator(asan))
47 ctx.BottomUp("asan", sanitizerMutator(asan)).Parallel()
Colin Cross16b23492016-01-06 14:41:07 -080048
Colin Cross1e676be2016-10-12 14:38:15 -070049 ctx.TopDown("tsan_deps", sanitizerDepsMutator(tsan))
50 ctx.BottomUp("tsan", sanitizerMutator(tsan)).Parallel()
Dan Willemsen581341d2017-02-09 16:16:31 -080051
52 ctx.BottomUp("coverage", coverageLinkingMutator).Parallel()
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -080053 ctx.TopDown("vndk_deps", sabiDepsMutator)
Stephen Craneba090d12017-05-09 15:44:35 -070054
55 ctx.TopDown("lto_deps", ltoDepsMutator)
56 ctx.BottomUp("lto", ltoMutator).Parallel()
Colin Cross1e676be2016-10-12 14:38:15 -070057 })
Colin Crossb98c8b02016-07-29 13:44:28 -070058
59 pctx.Import("android/soong/cc/config")
Colin Cross463a90e2015-06-17 14:20:06 -070060}
61
Colin Crossca860ac2016-01-04 14:34:37 -080062type Deps struct {
63 SharedLibs, LateSharedLibs []string
64 StaticLibs, LateStaticLibs, WholeStaticLibs []string
Colin Cross5950f382016-12-13 12:50:57 -080065 HeaderLibs []string
Colin Crossc472d572015-03-17 15:06:21 -070066
Colin Cross5950f382016-12-13 12:50:57 -080067 ReexportSharedLibHeaders, ReexportStaticLibHeaders, ReexportHeaderLibHeaders []string
Dan Willemsen490a8dc2016-06-06 18:22:19 -070068
Colin Cross81413472016-04-11 14:37:39 -070069 ObjFiles []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -070070
Dan Willemsenb40aab62016-04-20 14:21:14 -070071 GeneratedSources []string
72 GeneratedHeaders []string
73
Dan Willemsenb3454ab2016-09-28 17:34:58 -070074 ReexportGeneratedHeaders []string
75
Colin Cross97ba0732015-03-23 17:50:24 -070076 CrtBegin, CrtEnd string
Dan Willemsenc77a0b32017-09-18 23:19:12 -070077 LinkerScript string
Colin Crossc472d572015-03-17 15:06:21 -070078}
79
Colin Crossca860ac2016-01-04 14:34:37 -080080type PathDeps struct {
Colin Cross26c34ed2016-09-30 17:10:16 -070081 // Paths to .so files
82 SharedLibs, LateSharedLibs android.Paths
83 // Paths to the dependencies to use for .so files (.so.toc files)
84 SharedLibsDeps, LateSharedLibsDeps android.Paths
85 // Paths to .a files
Colin Cross635c3b02016-05-18 15:37:25 -070086 StaticLibs, LateStaticLibs, WholeStaticLibs android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -070087
Colin Cross26c34ed2016-09-30 17:10:16 -070088 // Paths to .o files
Dan Willemsen5cb580f2016-09-26 17:33:01 -070089 Objs Objects
Dan Willemsen581341d2017-02-09 16:16:31 -080090 StaticLibObjs Objects
Dan Willemsen5cb580f2016-09-26 17:33:01 -070091 WholeStaticLibObjs Objects
Dan Willemsen34cc69e2015-09-23 15:26:20 -070092
Colin Cross26c34ed2016-09-30 17:10:16 -070093 // Paths to generated source files
Colin Cross635c3b02016-05-18 15:37:25 -070094 GeneratedSources android.Paths
95 GeneratedHeaders android.Paths
Dan Willemsenb40aab62016-04-20 14:21:14 -070096
Dan Willemsen76f08272016-07-09 00:14:08 -070097 Flags, ReexportedFlags []string
Dan Willemsen847dcc72016-09-29 12:13:36 -070098 ReexportedFlagsDeps android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -070099
Colin Cross26c34ed2016-09-30 17:10:16 -0700100 // Paths to crt*.o files
Colin Cross635c3b02016-05-18 15:37:25 -0700101 CrtBegin, CrtEnd android.OptionalPath
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700102 LinkerScript android.OptionalPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700103}
104
Colin Crossca860ac2016-01-04 14:34:37 -0800105type Flags struct {
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700106 GlobalFlags []string // Flags that apply to C, C++, and assembly source files
107 ArFlags []string // Flags that apply to ar
108 AsFlags []string // Flags that apply to assembly source files
109 CFlags []string // Flags that apply to C and C++ source files
110 ToolingCFlags []string // Flags that apply to C and C++ source files parsed by clang LibTooling tools
111 ConlyFlags []string // Flags that apply to C source files
112 CppFlags []string // Flags that apply to C++ source files
113 ToolingCppFlags []string // Flags that apply to C++ source files parsed by clang LibTooling tools
114 YaccFlags []string // Flags that apply to Yacc source files
115 protoFlags []string // Flags that apply to proto source files
116 aidlFlags []string // Flags that apply to aidl source files
117 rsFlags []string // Flags that apply to renderscript source files
118 LdFlags []string // Flags that apply to linker command lines
119 libFlags []string // Flags to add libraries early to the link order
120 TidyFlags []string // Flags that apply to clang-tidy
121 SAbiFlags []string // Flags that apply to header-abi-dumper
122 YasmFlags []string // Flags that apply to yasm assembly source files
Colin Cross28344522015-04-22 13:07:53 -0700123
Colin Crossc3199482017-03-30 15:03:04 -0700124 // Global include flags that apply to C, C++, and assembly source files
125 // These must be after any module include flags, which will be in GlobalFlags.
126 SystemIncludeFlags []string
127
Colin Crossb98c8b02016-07-29 13:44:28 -0700128 Toolchain config.Toolchain
Colin Cross28344522015-04-22 13:07:53 -0700129 Clang bool
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700130 Tidy bool
Dan Willemsen581341d2017-02-09 16:16:31 -0800131 Coverage bool
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800132 SAbiDump bool
Colin Crossca860ac2016-01-04 14:34:37 -0800133
134 RequiredInstructionSet string
Colin Cross16b23492016-01-06 14:41:07 -0800135 DynamicLinker string
136
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700137 CFlagsDeps android.Paths // Files depended on by compiler flags
138 LdFlagsDeps android.Paths // Files depended on by linker flags
Colin Cross18c0c5a2016-12-01 14:45:23 -0800139
140 GroupStaticLibs bool
Colin Crossc472d572015-03-17 15:06:21 -0700141}
142
Colin Cross81413472016-04-11 14:37:39 -0700143type ObjectLinkerProperties struct {
144 // names of other cc_object modules to link into this module using partial linking
145 Objs []string `android:"arch_variant"`
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700146
147 // if set, add an extra objcopy --prefix-symbols= step
148 Prefix_symbols string
Colin Cross81413472016-04-11 14:37:39 -0700149}
150
Colin Crossca860ac2016-01-04 14:34:37 -0800151// Properties used to compile all C or C++ modules
152type BaseProperties struct {
153 // compile module with clang instead of gcc
154 Clang *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700155
156 // Minimum sdk version supported when compiling against the ndk
157 Sdk_version string
158
Colin Crossca860ac2016-01-04 14:34:37 -0800159 // don't insert default compiler flags into asflags, cflags,
160 // cppflags, conlyflags, ldflags, or include_dirs
161 No_default_compiler_flags *bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700162
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700163 AndroidMkSharedLibs []string `blueprint:"mutated"`
164 HideFromMake bool `blueprint:"mutated"`
165 PreventInstall bool `blueprint:"mutated"`
166
167 UseVndk bool `blueprint:"mutated"`
168}
169
170type VendorProperties struct {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700171 // whether this module should be allowed to install onto /vendor as
172 // well as /system. The two variants will be built separately, one
173 // like normal, and the other limited to the set of libraries and
174 // headers that are exposed to /vendor modules.
175 //
176 // The vendor variant may be used with a different (newer) /system,
177 // so it shouldn't have any unversioned runtime dependencies, or
178 // make assumptions about the system that may not be true in the
179 // future.
180 //
181 // Nothing happens if BOARD_VNDK_VERSION isn't set in the BoardConfig.mk
182 Vendor_available *bool
Colin Crossca860ac2016-01-04 14:34:37 -0800183}
184
Colin Crossca860ac2016-01-04 14:34:37 -0800185type UnusedProperties struct {
Dan Willemsen581341d2017-02-09 16:16:31 -0800186 Tags []string
Colin Crosscfad1192015-11-02 16:43:11 -0800187}
188
Colin Crossca860ac2016-01-04 14:34:37 -0800189type ModuleContextIntf interface {
Colin Crossca860ac2016-01-04 14:34:37 -0800190 static() bool
191 staticBinary() bool
192 clang() bool
Colin Crossb98c8b02016-07-29 13:44:28 -0700193 toolchain() config.Toolchain
Colin Crossca860ac2016-01-04 14:34:37 -0800194 noDefaultCompilerFlags() bool
195 sdk() bool
196 sdkVersion() string
Jeff Gaston2370af02017-09-27 17:01:44 -0700197 useVndk() bool
Justin Yun8effde42017-06-23 19:24:43 +0900198 isVndk() bool
199 isVndkSp() bool
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800200 createVndkSourceAbiDump() bool
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700201 selectedStl() string
Colin Crossce75d2c2016-10-06 16:12:58 -0700202 baseModuleName() string
Colin Crossca860ac2016-01-04 14:34:37 -0800203}
204
205type ModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700206 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800207 ModuleContextIntf
208}
209
210type BaseModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700211 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800212 ModuleContextIntf
213}
214
Colin Cross37047f12016-12-13 17:06:13 -0800215type DepsContext interface {
216 android.BottomUpMutatorContext
217 ModuleContextIntf
218}
219
Colin Crossca860ac2016-01-04 14:34:37 -0800220type feature interface {
221 begin(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800222 deps(ctx DepsContext, deps Deps) Deps
Colin Crossca860ac2016-01-04 14:34:37 -0800223 flags(ctx ModuleContext, flags Flags) Flags
224 props() []interface{}
225}
226
227type compiler interface {
Colin Cross42742b82016-08-01 13:20:05 -0700228 compilerInit(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800229 compilerDeps(ctx DepsContext, deps Deps) Deps
Colin Cross42742b82016-08-01 13:20:05 -0700230 compilerFlags(ctx ModuleContext, flags Flags) Flags
231 compilerProps() []interface{}
232
Colin Cross76fada02016-07-27 10:31:13 -0700233 appendCflags([]string)
234 appendAsflags([]string)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700235 compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects
Colin Crossca860ac2016-01-04 14:34:37 -0800236}
237
238type linker interface {
Colin Cross42742b82016-08-01 13:20:05 -0700239 linkerInit(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800240 linkerDeps(ctx DepsContext, deps Deps) Deps
Colin Cross42742b82016-08-01 13:20:05 -0700241 linkerFlags(ctx ModuleContext, flags Flags) Flags
242 linkerProps() []interface{}
243
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700244 link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path
Colin Cross76fada02016-07-27 10:31:13 -0700245 appendLdflags([]string)
Colin Crossca860ac2016-01-04 14:34:37 -0800246}
247
248type installer interface {
Colin Cross42742b82016-08-01 13:20:05 -0700249 installerProps() []interface{}
Colin Cross635c3b02016-05-18 15:37:25 -0700250 install(ctx ModuleContext, path android.Path)
Colin Crossca860ac2016-01-04 14:34:37 -0800251 inData() bool
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700252 inSanitizerDir() bool
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700253 hostToolPath() android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800254}
255
Colin Crossc99deeb2016-04-11 15:06:20 -0700256type dependencyTag struct {
257 blueprint.BaseDependencyTag
258 name string
259 library bool
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700260
261 reexportFlags bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700262}
263
264var (
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700265 sharedDepTag = dependencyTag{name: "shared", library: true}
266 sharedExportDepTag = dependencyTag{name: "shared", library: true, reexportFlags: true}
267 lateSharedDepTag = dependencyTag{name: "late shared", library: true}
268 staticDepTag = dependencyTag{name: "static", library: true}
269 staticExportDepTag = dependencyTag{name: "static", library: true, reexportFlags: true}
270 lateStaticDepTag = dependencyTag{name: "late static", library: true}
271 wholeStaticDepTag = dependencyTag{name: "whole static", library: true, reexportFlags: true}
Colin Cross32ec36c2016-12-15 07:39:51 -0800272 headerDepTag = dependencyTag{name: "header", library: true}
273 headerExportDepTag = dependencyTag{name: "header", library: true, reexportFlags: true}
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700274 genSourceDepTag = dependencyTag{name: "gen source"}
275 genHeaderDepTag = dependencyTag{name: "gen header"}
276 genHeaderExportDepTag = dependencyTag{name: "gen header", reexportFlags: true}
277 objDepTag = dependencyTag{name: "obj"}
278 crtBeginDepTag = dependencyTag{name: "crtbegin"}
279 crtEndDepTag = dependencyTag{name: "crtend"}
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700280 linkerScriptDepTag = dependencyTag{name: "linker script"}
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700281 reuseObjTag = dependencyTag{name: "reuse objects"}
282 ndkStubDepTag = dependencyTag{name: "ndk stub", library: true}
283 ndkLateStubDepTag = dependencyTag{name: "ndk late stub", library: true}
Colin Crossc99deeb2016-04-11 15:06:20 -0700284)
285
Colin Crossca860ac2016-01-04 14:34:37 -0800286// Module contains the properties and members used by all C/C++ module types, and implements
287// the blueprint.Module interface. It delegates to compiler, linker, and installer interfaces
288// to construct the output file. Behavior can be customized with a Customizer interface
289type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700290 android.ModuleBase
Colin Cross1f44a3a2017-07-07 14:33:33 -0700291 android.DefaultableModuleBase
Colin Crossc472d572015-03-17 15:06:21 -0700292
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700293 Properties BaseProperties
294 VendorProperties VendorProperties
295 unused UnusedProperties
Colin Crossfa138792015-04-24 17:31:52 -0700296
Colin Crossca860ac2016-01-04 14:34:37 -0800297 // initialize before calling Init
Colin Cross635c3b02016-05-18 15:37:25 -0700298 hod android.HostOrDeviceSupported
299 multilib android.Multilib
Colin Crossc472d572015-03-17 15:06:21 -0700300
Colin Crossca860ac2016-01-04 14:34:37 -0800301 // delegates, initialize before calling Init
Colin Crossb4ce0ec2016-09-13 13:41:39 -0700302 features []feature
303 compiler compiler
304 linker linker
305 installer installer
306 stl *stl
307 sanitize *sanitize
Dan Willemsen581341d2017-02-09 16:16:31 -0800308 coverage *coverage
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800309 sabi *sabi
Justin Yun8effde42017-06-23 19:24:43 +0900310 vndkdep *vndkdep
Stephen Craneba090d12017-05-09 15:44:35 -0700311 lto *lto
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700312 pgo *pgo
Colin Cross16b23492016-01-06 14:41:07 -0800313
314 androidMkSharedLibDeps []string
Colin Cross74d1ec02015-04-28 13:30:13 -0700315
Colin Cross635c3b02016-05-18 15:37:25 -0700316 outputFile android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800317
Colin Crossb98c8b02016-07-29 13:44:28 -0700318 cachedToolchain config.Toolchain
Colin Crossb916a382016-07-29 17:28:03 -0700319
320 subAndroidMkOnce map[subAndroidMkProvider]bool
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800321
322 // Flags used to compile this module
323 flags Flags
Colin Crossc472d572015-03-17 15:06:21 -0700324}
325
Colin Cross36242852017-06-23 15:06:31 -0700326func (c *Module) Init() android.Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700327 c.AddProperties(&c.Properties, &c.VendorProperties, &c.unused)
Colin Crossca860ac2016-01-04 14:34:37 -0800328 if c.compiler != nil {
Colin Cross36242852017-06-23 15:06:31 -0700329 c.AddProperties(c.compiler.compilerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800330 }
331 if c.linker != nil {
Colin Cross36242852017-06-23 15:06:31 -0700332 c.AddProperties(c.linker.linkerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800333 }
334 if c.installer != nil {
Colin Cross36242852017-06-23 15:06:31 -0700335 c.AddProperties(c.installer.installerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800336 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700337 if c.stl != nil {
Colin Cross36242852017-06-23 15:06:31 -0700338 c.AddProperties(c.stl.props()...)
Colin Crossa8e07cc2016-04-04 15:07:06 -0700339 }
Colin Cross16b23492016-01-06 14:41:07 -0800340 if c.sanitize != nil {
Colin Cross36242852017-06-23 15:06:31 -0700341 c.AddProperties(c.sanitize.props()...)
Colin Cross16b23492016-01-06 14:41:07 -0800342 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800343 if c.coverage != nil {
Colin Cross36242852017-06-23 15:06:31 -0700344 c.AddProperties(c.coverage.props()...)
Dan Willemsen581341d2017-02-09 16:16:31 -0800345 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800346 if c.sabi != nil {
Colin Cross36242852017-06-23 15:06:31 -0700347 c.AddProperties(c.sabi.props()...)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800348 }
Justin Yun8effde42017-06-23 19:24:43 +0900349 if c.vndkdep != nil {
350 c.AddProperties(c.vndkdep.props()...)
351 }
Stephen Craneba090d12017-05-09 15:44:35 -0700352 if c.lto != nil {
353 c.AddProperties(c.lto.props()...)
354 }
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700355 if c.pgo != nil {
356 c.AddProperties(c.pgo.props()...)
357 }
Colin Crossca860ac2016-01-04 14:34:37 -0800358 for _, feature := range c.features {
Colin Cross36242852017-06-23 15:06:31 -0700359 c.AddProperties(feature.props()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800360 }
Colin Crossc472d572015-03-17 15:06:21 -0700361
Colin Cross36242852017-06-23 15:06:31 -0700362 android.InitAndroidArchModule(c, c.hod, c.multilib)
Colin Crossc472d572015-03-17 15:06:21 -0700363
Colin Cross1f44a3a2017-07-07 14:33:33 -0700364 android.InitDefaultableModule(c)
Colin Cross36242852017-06-23 15:06:31 -0700365
366 return c
Colin Crossc472d572015-03-17 15:06:21 -0700367}
368
Colin Crossb916a382016-07-29 17:28:03 -0700369// Returns true for dependency roots (binaries)
370// TODO(ccross): also handle dlopenable libraries
371func (c *Module) isDependencyRoot() bool {
372 if root, ok := c.linker.(interface {
373 isDependencyRoot() bool
374 }); ok {
375 return root.isDependencyRoot()
376 }
377 return false
378}
379
Jeff Gaston2370af02017-09-27 17:01:44 -0700380func (c *Module) useVndk() bool {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700381 return c.Properties.UseVndk
382}
383
Justin Yun8effde42017-06-23 19:24:43 +0900384func (c *Module) isVndk() bool {
385 if c.vndkdep != nil {
386 return c.vndkdep.isVndk()
387 }
388 return false
389}
390
Colin Crossca860ac2016-01-04 14:34:37 -0800391type baseModuleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700392 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800393 moduleContextImpl
394}
395
Colin Cross37047f12016-12-13 17:06:13 -0800396type depsContext struct {
397 android.BottomUpMutatorContext
398 moduleContextImpl
399}
400
Colin Crossca860ac2016-01-04 14:34:37 -0800401type moduleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700402 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800403 moduleContextImpl
404}
405
Jeff Gaston2370af02017-09-27 17:01:44 -0700406func (ctx *moduleContext) InstallOnVendorPartition() bool {
407 return ctx.ModuleContext.Vendor() || (ctx.mod.useVndk() && !ctx.mod.isVndk())
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700408}
409
Colin Crossca860ac2016-01-04 14:34:37 -0800410type moduleContextImpl struct {
411 mod *Module
412 ctx BaseModuleContext
413}
414
Colin Crossca860ac2016-01-04 14:34:37 -0800415func (ctx *moduleContextImpl) clang() bool {
416 return ctx.mod.clang(ctx.ctx)
417}
418
Colin Crossb98c8b02016-07-29 13:44:28 -0700419func (ctx *moduleContextImpl) toolchain() config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800420 return ctx.mod.toolchain(ctx.ctx)
421}
422
423func (ctx *moduleContextImpl) static() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700424 if static, ok := ctx.mod.linker.(interface {
425 static() bool
426 }); ok {
427 return static.static()
Colin Crossca860ac2016-01-04 14:34:37 -0800428 }
Colin Crossb916a382016-07-29 17:28:03 -0700429 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800430}
431
432func (ctx *moduleContextImpl) staticBinary() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700433 if static, ok := ctx.mod.linker.(interface {
434 staticBinary() bool
435 }); ok {
436 return static.staticBinary()
Colin Crossca860ac2016-01-04 14:34:37 -0800437 }
Colin Crossb916a382016-07-29 17:28:03 -0700438 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800439}
440
441func (ctx *moduleContextImpl) noDefaultCompilerFlags() bool {
442 return Bool(ctx.mod.Properties.No_default_compiler_flags)
443}
444
445func (ctx *moduleContextImpl) sdk() bool {
Jeff Gaston2370af02017-09-27 17:01:44 -0700446 if ctx.ctx.Device() && !ctx.useVndk() {
Dan Willemsena96ff642016-06-07 12:34:45 -0700447 return ctx.mod.Properties.Sdk_version != ""
448 }
449 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800450}
451
452func (ctx *moduleContextImpl) sdkVersion() string {
Dan Willemsena96ff642016-06-07 12:34:45 -0700453 if ctx.ctx.Device() {
Jeff Gaston2370af02017-09-27 17:01:44 -0700454 if ctx.useVndk() {
Dan Willemsen11b26142017-03-19 18:30:37 -0700455 return "current"
Dan Willemsend2ede872016-11-18 14:54:24 -0800456 } else {
457 return ctx.mod.Properties.Sdk_version
458 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700459 }
460 return ""
Colin Crossca860ac2016-01-04 14:34:37 -0800461}
462
Justin Yun8effde42017-06-23 19:24:43 +0900463func (ctx *moduleContextImpl) isVndk() bool {
464 return ctx.mod.isVndk()
465}
Jeff Gaston2370af02017-09-27 17:01:44 -0700466func (ctx *moduleContextImpl) useVndk() bool {
467 return ctx.mod.useVndk()
468}
Justin Yun8effde42017-06-23 19:24:43 +0900469
470func (ctx *moduleContextImpl) isVndkSp() bool {
471 if vndk := ctx.mod.vndkdep; vndk != nil {
472 return vndk.isVndkSp()
473 }
474 return false
475}
476
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800477// Create source abi dumps if the module belongs to the list of VndkLibraries.
478func (ctx *moduleContextImpl) createVndkSourceAbiDump() bool {
Jeff Gaston2370af02017-09-27 17:01:44 -0700479 return ctx.ctx.Device() && ((ctx.useVndk() && ctx.isVndk()) || inList(ctx.baseModuleName(), llndkLibraries))
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800480}
481
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700482func (ctx *moduleContextImpl) selectedStl() string {
483 if stl := ctx.mod.stl; stl != nil {
484 return stl.Properties.SelectedStl
485 }
486 return ""
487}
488
Colin Crossce75d2c2016-10-06 16:12:58 -0700489func (ctx *moduleContextImpl) baseModuleName() string {
490 return ctx.mod.ModuleBase.BaseModuleName()
491}
492
Colin Cross635c3b02016-05-18 15:37:25 -0700493func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800494 return &Module{
495 hod: hod,
496 multilib: multilib,
497 }
498}
499
Colin Cross635c3b02016-05-18 15:37:25 -0700500func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800501 module := newBaseModule(hod, multilib)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700502 module.features = []feature{
503 &tidyFeature{},
504 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700505 module.stl = &stl{}
Colin Cross16b23492016-01-06 14:41:07 -0800506 module.sanitize = &sanitize{}
Dan Willemsen581341d2017-02-09 16:16:31 -0800507 module.coverage = &coverage{}
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800508 module.sabi = &sabi{}
Justin Yun8effde42017-06-23 19:24:43 +0900509 module.vndkdep = &vndkdep{}
Stephen Craneba090d12017-05-09 15:44:35 -0700510 module.lto = &lto{}
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700511 module.pgo = &pgo{}
Colin Crossca860ac2016-01-04 14:34:37 -0800512 return module
513}
514
Colin Crossce75d2c2016-10-06 16:12:58 -0700515func (c *Module) Prebuilt() *android.Prebuilt {
516 if p, ok := c.linker.(prebuiltLinkerInterface); ok {
517 return p.prebuilt()
518 }
519 return nil
520}
521
522func (c *Module) Name() string {
523 name := c.ModuleBase.Name()
Dan Willemsen01a90592017-04-07 15:21:13 -0700524 if p, ok := c.linker.(interface {
525 Name(string) string
526 }); ok {
Colin Crossce75d2c2016-10-06 16:12:58 -0700527 name = p.Name(name)
528 }
529 return name
530}
531
Colin Cross635c3b02016-05-18 15:37:25 -0700532func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
Jeff Gaston2370af02017-09-27 17:01:44 -0700533
Colin Crossca860ac2016-01-04 14:34:37 -0800534 ctx := &moduleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700535 ModuleContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800536 moduleContextImpl: moduleContextImpl{
537 mod: c,
538 },
539 }
540 ctx.ctx = ctx
541
542 flags := Flags{
543 Toolchain: c.toolchain(ctx),
544 Clang: c.clang(ctx),
545 }
Colin Crossca860ac2016-01-04 14:34:37 -0800546 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700547 flags = c.compiler.compilerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800548 }
549 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700550 flags = c.linker.linkerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800551 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700552 if c.stl != nil {
553 flags = c.stl.flags(ctx, flags)
554 }
Colin Cross16b23492016-01-06 14:41:07 -0800555 if c.sanitize != nil {
556 flags = c.sanitize.flags(ctx, flags)
557 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800558 if c.coverage != nil {
559 flags = c.coverage.flags(ctx, flags)
560 }
Stephen Craneba090d12017-05-09 15:44:35 -0700561 if c.lto != nil {
562 flags = c.lto.flags(ctx, flags)
563 }
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700564 if c.pgo != nil {
565 flags = c.pgo.flags(ctx, flags)
566 }
Colin Crossca860ac2016-01-04 14:34:37 -0800567 for _, feature := range c.features {
568 flags = feature.flags(ctx, flags)
569 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800570 if ctx.Failed() {
571 return
572 }
573
Colin Crossb98c8b02016-07-29 13:44:28 -0700574 flags.CFlags, _ = filterList(flags.CFlags, config.IllegalFlags)
575 flags.CppFlags, _ = filterList(flags.CppFlags, config.IllegalFlags)
576 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, config.IllegalFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800577
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800578 deps := c.depsToPaths(ctx)
579 if ctx.Failed() {
580 return
581 }
582 flags.GlobalFlags = append(flags.GlobalFlags, deps.Flags...)
583 c.flags = flags
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700584 // We need access to all the flags seen by a source file.
585 if c.sabi != nil {
586 flags = c.sabi.flags(ctx, flags)
587 }
Colin Crossca860ac2016-01-04 14:34:37 -0800588 // Optimization to reduce size of build.ninja
589 // Replace the long list of flags for each file with a module-local variable
590 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
591 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
592 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
593 flags.CFlags = []string{"$cflags"}
594 flags.CppFlags = []string{"$cppflags"}
595 flags.AsFlags = []string{"$asflags"}
596
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700597 var objs Objects
Colin Crossca860ac2016-01-04 14:34:37 -0800598 if c.compiler != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700599 objs = c.compiler.compile(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -0800600 if ctx.Failed() {
601 return
602 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800603 }
604
Colin Crossca860ac2016-01-04 14:34:37 -0800605 if c.linker != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700606 outputFile := c.linker.link(ctx, flags, deps, objs)
Colin Crossca860ac2016-01-04 14:34:37 -0800607 if ctx.Failed() {
608 return
609 }
Colin Cross635c3b02016-05-18 15:37:25 -0700610 c.outputFile = android.OptionalPathForPath(outputFile)
Colin Crossce75d2c2016-10-06 16:12:58 -0700611 }
Colin Cross5049f022015-03-18 13:28:46 -0700612
Colin Crossce75d2c2016-10-06 16:12:58 -0700613 if c.installer != nil && !c.Properties.PreventInstall && c.outputFile.Valid() {
614 c.installer.install(ctx, c.outputFile.Path())
615 if ctx.Failed() {
616 return
Colin Crossca860ac2016-01-04 14:34:37 -0800617 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700618 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800619}
620
Colin Crossb98c8b02016-07-29 13:44:28 -0700621func (c *Module) toolchain(ctx BaseModuleContext) config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800622 if c.cachedToolchain == nil {
Colin Crossb98c8b02016-07-29 13:44:28 -0700623 c.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
Colin Cross3f40fa42015-01-30 17:27:36 -0800624 }
Colin Crossca860ac2016-01-04 14:34:37 -0800625 return c.cachedToolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800626}
627
Colin Crossca860ac2016-01-04 14:34:37 -0800628func (c *Module) begin(ctx BaseModuleContext) {
629 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700630 c.compiler.compilerInit(ctx)
Colin Cross21b9a242015-03-24 14:15:58 -0700631 }
Colin Crossca860ac2016-01-04 14:34:37 -0800632 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700633 c.linker.linkerInit(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800634 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700635 if c.stl != nil {
636 c.stl.begin(ctx)
637 }
Colin Cross16b23492016-01-06 14:41:07 -0800638 if c.sanitize != nil {
639 c.sanitize.begin(ctx)
640 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800641 if c.coverage != nil {
642 c.coverage.begin(ctx)
643 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800644 if c.sabi != nil {
645 c.sabi.begin(ctx)
646 }
Justin Yun8effde42017-06-23 19:24:43 +0900647 if c.vndkdep != nil {
648 c.vndkdep.begin(ctx)
649 }
Stephen Craneba090d12017-05-09 15:44:35 -0700650 if c.lto != nil {
651 c.lto.begin(ctx)
652 }
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700653 if c.pgo != nil {
654 c.pgo.begin(ctx)
655 }
Colin Crossca860ac2016-01-04 14:34:37 -0800656 for _, feature := range c.features {
657 feature.begin(ctx)
658 }
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700659 if ctx.sdk() {
Dan Albertf5415d72017-08-17 16:19:59 -0700660 version, err := normalizeNdkApiLevel(ctx, ctx.sdkVersion(), ctx.Arch())
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700661 if err != nil {
662 ctx.PropertyErrorf("sdk_version", err.Error())
663 }
Dan Albert90f7a4d2016-11-08 14:34:24 -0800664 c.Properties.Sdk_version = version
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700665 }
Colin Crossca860ac2016-01-04 14:34:37 -0800666}
667
Colin Cross37047f12016-12-13 17:06:13 -0800668func (c *Module) deps(ctx DepsContext) Deps {
Colin Crossc99deeb2016-04-11 15:06:20 -0700669 deps := Deps{}
670
671 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700672 deps = c.compiler.compilerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700673 }
674 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700675 deps = c.linker.linkerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700676 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700677 if c.stl != nil {
678 deps = c.stl.deps(ctx, deps)
679 }
Colin Cross16b23492016-01-06 14:41:07 -0800680 if c.sanitize != nil {
681 deps = c.sanitize.deps(ctx, deps)
682 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800683 if c.coverage != nil {
684 deps = c.coverage.deps(ctx, deps)
685 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800686 if c.sabi != nil {
687 deps = c.sabi.deps(ctx, deps)
688 }
Justin Yun8effde42017-06-23 19:24:43 +0900689 if c.vndkdep != nil {
690 deps = c.vndkdep.deps(ctx, deps)
691 }
Stephen Craneba090d12017-05-09 15:44:35 -0700692 if c.lto != nil {
693 deps = c.lto.deps(ctx, deps)
694 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700695 for _, feature := range c.features {
696 deps = feature.deps(ctx, deps)
697 }
698
699 deps.WholeStaticLibs = lastUniqueElements(deps.WholeStaticLibs)
700 deps.StaticLibs = lastUniqueElements(deps.StaticLibs)
701 deps.LateStaticLibs = lastUniqueElements(deps.LateStaticLibs)
702 deps.SharedLibs = lastUniqueElements(deps.SharedLibs)
703 deps.LateSharedLibs = lastUniqueElements(deps.LateSharedLibs)
Colin Cross5950f382016-12-13 12:50:57 -0800704 deps.HeaderLibs = lastUniqueElements(deps.HeaderLibs)
Colin Crossc99deeb2016-04-11 15:06:20 -0700705
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700706 for _, lib := range deps.ReexportSharedLibHeaders {
707 if !inList(lib, deps.SharedLibs) {
708 ctx.PropertyErrorf("export_shared_lib_headers", "Shared library not in shared_libs: '%s'", lib)
709 }
710 }
711
712 for _, lib := range deps.ReexportStaticLibHeaders {
713 if !inList(lib, deps.StaticLibs) {
714 ctx.PropertyErrorf("export_static_lib_headers", "Static library not in static_libs: '%s'", lib)
715 }
716 }
717
Colin Cross5950f382016-12-13 12:50:57 -0800718 for _, lib := range deps.ReexportHeaderLibHeaders {
719 if !inList(lib, deps.HeaderLibs) {
720 ctx.PropertyErrorf("export_header_lib_headers", "Header library not in header_libs: '%s'", lib)
721 }
722 }
723
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700724 for _, gen := range deps.ReexportGeneratedHeaders {
725 if !inList(gen, deps.GeneratedHeaders) {
726 ctx.PropertyErrorf("export_generated_headers", "Generated header module not in generated_headers: '%s'", gen)
727 }
728 }
729
Colin Crossc99deeb2016-04-11 15:06:20 -0700730 return deps
731}
732
Dan Albert7e9d2952016-08-04 13:02:36 -0700733func (c *Module) beginMutator(actx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800734 ctx := &baseModuleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700735 BaseContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800736 moduleContextImpl: moduleContextImpl{
737 mod: c,
738 },
739 }
740 ctx.ctx = ctx
741
Colin Crossca860ac2016-01-04 14:34:37 -0800742 c.begin(ctx)
Dan Albert7e9d2952016-08-04 13:02:36 -0700743}
744
Colin Cross1e676be2016-10-12 14:38:15 -0700745func (c *Module) DepsMutator(actx android.BottomUpMutatorContext) {
746 if !c.Enabled() {
747 return
748 }
749
Colin Cross37047f12016-12-13 17:06:13 -0800750 ctx := &depsContext{
751 BottomUpMutatorContext: actx,
Dan Albert7e9d2952016-08-04 13:02:36 -0700752 moduleContextImpl: moduleContextImpl{
753 mod: c,
754 },
755 }
756 ctx.ctx = ctx
Colin Crossca860ac2016-01-04 14:34:37 -0800757
Colin Crossc99deeb2016-04-11 15:06:20 -0700758 deps := c.deps(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800759
Dan Albert914449f2016-06-17 16:45:24 -0700760 variantNdkLibs := []string{}
761 variantLateNdkLibs := []string{}
Dan Willemsenb916b802017-03-19 13:44:32 -0700762 if ctx.Os() == android.Android {
Dan Albert914449f2016-06-17 16:45:24 -0700763 version := ctx.sdkVersion()
Dan Willemsen72d39932016-07-08 23:23:48 -0700764
Jeff Gaston2370af02017-09-27 17:01:44 -0700765 // rewriteNdkLibs takes a list of names of shared libraries and scans it for three types
766 // of names:
Dan Albert914449f2016-06-17 16:45:24 -0700767 //
Jeff Gaston2370af02017-09-27 17:01:44 -0700768 // 1. Name of an NDK library that refers to a prebuilt module.
769 // For each of these, it adds the name of the prebuilt module (which will be in
770 // prebuilts/ndk) to the list of nonvariant libs.
771 // 2. Name of an NDK library that refers to an ndk_library module.
772 // For each of these, it adds the name of the ndk_library module to the list of
773 // variant libs.
774 // 3. Anything else (so anything that isn't an NDK library).
775 // It adds these to the nonvariantLibs list.
Dan Albert914449f2016-06-17 16:45:24 -0700776 //
Jeff Gaston2370af02017-09-27 17:01:44 -0700777 // The caller can then know to add the variantLibs dependencies differently from the
778 // nonvariantLibs
779 rewriteNdkLibs := func(list []string) (nonvariantLibs []string, variantLibs []string) {
780 variantLibs = []string{}
781 nonvariantLibs = []string{}
Dan Albert914449f2016-06-17 16:45:24 -0700782 for _, entry := range list {
Dan Willemsenb916b802017-03-19 13:44:32 -0700783 if ctx.sdk() && inList(entry, ndkPrebuiltSharedLibraries) {
Dan Albert914449f2016-06-17 16:45:24 -0700784 if !inList(entry, ndkMigratedLibs) {
785 nonvariantLibs = append(nonvariantLibs, entry+".ndk."+version)
786 } else {
787 variantLibs = append(variantLibs, entry+ndkLibrarySuffix)
788 }
Jeff Gaston2370af02017-09-27 17:01:44 -0700789 } else if ctx.useVndk() && inList(entry, llndkLibraries) {
Dan Willemsenb916b802017-03-19 13:44:32 -0700790 nonvariantLibs = append(nonvariantLibs, entry+llndkLibrarySuffix)
Dan Albert914449f2016-06-17 16:45:24 -0700791 } else {
Dan Willemsen7cbf5f82017-03-28 00:08:30 -0700792 nonvariantLibs = append(nonvariantLibs, entry)
Dan Willemsen72d39932016-07-08 23:23:48 -0700793 }
794 }
Dan Albert914449f2016-06-17 16:45:24 -0700795 return nonvariantLibs, variantLibs
Dan Willemsen72d39932016-07-08 23:23:48 -0700796 }
797
Dan Albert914449f2016-06-17 16:45:24 -0700798 deps.SharedLibs, variantNdkLibs = rewriteNdkLibs(deps.SharedLibs)
799 deps.LateSharedLibs, variantLateNdkLibs = rewriteNdkLibs(deps.LateSharedLibs)
Jiyong Park4c35af02017-07-05 13:41:55 +0900800 deps.ReexportSharedLibHeaders, _ = rewriteNdkLibs(deps.ReexportSharedLibHeaders)
Dan Willemsen72d39932016-07-08 23:23:48 -0700801 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700802
Colin Cross32ec36c2016-12-15 07:39:51 -0800803 for _, lib := range deps.HeaderLibs {
804 depTag := headerDepTag
805 if inList(lib, deps.ReexportHeaderLibHeaders) {
806 depTag = headerExportDepTag
807 }
808 actx.AddVariationDependencies(nil, depTag, lib)
809 }
Colin Cross5950f382016-12-13 12:50:57 -0800810
Colin Crossc99deeb2016-04-11 15:06:20 -0700811 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, wholeStaticDepTag,
812 deps.WholeStaticLibs...)
813
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700814 for _, lib := range deps.StaticLibs {
815 depTag := staticDepTag
816 if inList(lib, deps.ReexportStaticLibHeaders) {
817 depTag = staticExportDepTag
818 }
Colin Cross15a0d462016-07-14 14:49:58 -0700819 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700820 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700821
822 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, lateStaticDepTag,
823 deps.LateStaticLibs...)
824
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700825 for _, lib := range deps.SharedLibs {
826 depTag := sharedDepTag
827 if inList(lib, deps.ReexportSharedLibHeaders) {
828 depTag = sharedExportDepTag
829 }
Colin Cross15a0d462016-07-14 14:49:58 -0700830 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700831 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700832
833 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, lateSharedDepTag,
834 deps.LateSharedLibs...)
835
Colin Cross68861832016-07-08 10:41:41 -0700836 actx.AddDependency(c, genSourceDepTag, deps.GeneratedSources...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700837
838 for _, gen := range deps.GeneratedHeaders {
839 depTag := genHeaderDepTag
840 if inList(gen, deps.ReexportGeneratedHeaders) {
841 depTag = genHeaderExportDepTag
842 }
843 actx.AddDependency(c, depTag, gen)
844 }
Dan Willemsenb40aab62016-04-20 14:21:14 -0700845
Colin Cross68861832016-07-08 10:41:41 -0700846 actx.AddDependency(c, objDepTag, deps.ObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700847
848 if deps.CrtBegin != "" {
Colin Cross68861832016-07-08 10:41:41 -0700849 actx.AddDependency(c, crtBeginDepTag, deps.CrtBegin)
Colin Crossca860ac2016-01-04 14:34:37 -0800850 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700851 if deps.CrtEnd != "" {
Colin Cross68861832016-07-08 10:41:41 -0700852 actx.AddDependency(c, crtEndDepTag, deps.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700853 }
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700854 if deps.LinkerScript != "" {
855 actx.AddDependency(c, linkerScriptDepTag, deps.LinkerScript)
856 }
Dan Albert914449f2016-06-17 16:45:24 -0700857
858 version := ctx.sdkVersion()
859 actx.AddVariationDependencies([]blueprint.Variation{
860 {"ndk_api", version}, {"link", "shared"}}, ndkStubDepTag, variantNdkLibs...)
861 actx.AddVariationDependencies([]blueprint.Variation{
862 {"ndk_api", version}, {"link", "shared"}}, ndkLateStubDepTag, variantLateNdkLibs...)
Colin Cross6362e272015-10-29 15:25:03 -0700863}
Colin Cross21b9a242015-03-24 14:15:58 -0700864
Dan Albert7e9d2952016-08-04 13:02:36 -0700865func beginMutator(ctx android.BottomUpMutatorContext) {
866 if c, ok := ctx.Module().(*Module); ok && c.Enabled() {
867 c.beginMutator(ctx)
868 }
869}
870
Colin Crossca860ac2016-01-04 14:34:37 -0800871func (c *Module) clang(ctx BaseModuleContext) bool {
872 clang := Bool(c.Properties.Clang)
873
874 if c.Properties.Clang == nil {
875 if ctx.Host() {
876 clang = true
877 }
878
879 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
880 clang = true
881 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800882 }
Colin Cross28344522015-04-22 13:07:53 -0700883
Colin Crossca860ac2016-01-04 14:34:37 -0800884 if !c.toolchain(ctx).ClangSupported() {
885 clang = false
886 }
887
888 return clang
889}
890
Jeff Gaston2370af02017-09-27 17:01:44 -0700891// Whether a module can link to another module, taking into
892// account NDK linking.
893func checkLinkType(ctx android.ModuleContext, from *Module, to *Module) {
894 if from.Target().Os != android.Android {
895 // Host code is not restricted
896 return
897 }
898 if from.Properties.UseVndk {
899 // Though vendor code is limited by the vendor mutator,
900 // each vendor-available module needs to check
901 // link-type for VNDK.
902 if from.vndkdep != nil {
903 from.vndkdep.vndkCheckLinkType(ctx, to)
904 }
905 return
906 }
907 if from.Properties.Sdk_version == "" {
908 // Platform code can link to anything
909 return
910 }
911 if _, ok := to.linker.(*toolchainLibraryDecorator); ok {
912 // These are always allowed
913 return
914 }
915 if _, ok := to.linker.(*ndkPrebuiltLibraryLinker); ok {
916 // These are allowed, but they don't set sdk_version
917 return
918 }
919 if _, ok := to.linker.(*ndkPrebuiltStlLinker); ok {
920 // These are allowed, but they don't set sdk_version
921 return
922 }
923 if _, ok := to.linker.(*stubDecorator); ok {
924 // These aren't real libraries, but are the stub shared libraries that are included in
925 // the NDK.
926 return
927 }
928 if to.Properties.Sdk_version == "" {
929 // NDK code linking to platform code is never okay.
930 ctx.ModuleErrorf("depends on non-NDK-built library %q",
931 ctx.OtherModuleName(to))
932 }
933
934 // At this point we know we have two NDK libraries, but we need to
935 // check that we're not linking against anything built against a higher
936 // API level, as it is only valid to link against older or equivalent
937 // APIs.
938
939 if from.Properties.Sdk_version == "current" {
940 // Current can link against anything.
941 return
942 } else if to.Properties.Sdk_version == "current" {
943 // Current can't be linked against by anything else.
944 ctx.ModuleErrorf("links %q built against newer API version %q",
945 ctx.OtherModuleName(to), "current")
946 }
947
948 fromApi, err := strconv.Atoi(from.Properties.Sdk_version)
949 if err != nil {
950 ctx.PropertyErrorf("sdk_version",
951 "Invalid sdk_version value (must be int): %q",
952 from.Properties.Sdk_version)
953 }
954 toApi, err := strconv.Atoi(to.Properties.Sdk_version)
955 if err != nil {
956 ctx.PropertyErrorf("sdk_version",
957 "Invalid sdk_version value (must be int): %q",
958 to.Properties.Sdk_version)
959 }
960
961 if toApi > fromApi {
962 ctx.ModuleErrorf("links %q built against newer API version %q",
963 ctx.OtherModuleName(to), to.Properties.Sdk_version)
964 }
965}
966
Colin Crossc99deeb2016-04-11 15:06:20 -0700967// Convert dependencies to paths. Returns a PathDeps containing paths
Colin Cross635c3b02016-05-18 15:37:25 -0700968func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
Colin Crossca860ac2016-01-04 14:34:37 -0800969 var depPaths PathDeps
Colin Crossca860ac2016-01-04 14:34:37 -0800970
Jeff Gaston2370af02017-09-27 17:01:44 -0700971 ctx.VisitDirectDeps(func(dep blueprint.Module) {
972 depName := ctx.OtherModuleName(dep)
973 depTag := ctx.OtherModuleDependencyTag(dep)
Dan Albert9e10cd42016-08-03 14:12:14 -0700974
Jeff Gaston2370af02017-09-27 17:01:44 -0700975 aDep, _ := dep.(android.Module)
976 if aDep == nil {
977 ctx.ModuleErrorf("module %q not an android module", depName)
Colin Crossc99deeb2016-04-11 15:06:20 -0700978 return
Colin Crossca860ac2016-01-04 14:34:37 -0800979 }
Colin Crossca860ac2016-01-04 14:34:37 -0800980
Jeff Gaston2370af02017-09-27 17:01:44 -0700981 ccDep, _ := dep.(*Module)
982 if ccDep == nil {
983 // handling for a few module types that aren't cc Module but that are also supported
984 switch depTag {
Colin Cross068e0fe2016-12-13 15:23:47 -0800985 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700986 // Nothing to do
Dan Willemsenb40aab62016-04-20 14:21:14 -0700987 case genSourceDepTag:
Jeff Gaston2370af02017-09-27 17:01:44 -0700988 if genRule, ok := dep.(genrule.SourceFileGenerator); ok {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700989 depPaths.GeneratedSources = append(depPaths.GeneratedSources,
990 genRule.GeneratedSourceFiles()...)
991 } else {
Jeff Gaston2370af02017-09-27 17:01:44 -0700992 ctx.ModuleErrorf("module %q is not a gensrcs or genrule", depName)
Dan Willemsenb40aab62016-04-20 14:21:14 -0700993 }
Colin Crosse90bfd12017-04-26 16:59:26 -0700994 // Support exported headers from a generated_sources dependency
995 fallthrough
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700996 case genHeaderDepTag, genHeaderExportDepTag:
Jeff Gaston2370af02017-09-27 17:01:44 -0700997 if genRule, ok := dep.(genrule.SourceFileGenerator); ok {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700998 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders,
999 genRule.GeneratedSourceFiles()...)
Colin Cross5ed99c62016-11-22 12:55:55 -08001000 flags := includeDirsToFlags(genRule.GeneratedHeaderDirs())
Dan Willemsenb3454ab2016-09-28 17:34:58 -07001001 depPaths.Flags = append(depPaths.Flags, flags)
Jeff Gaston2370af02017-09-27 17:01:44 -07001002 if depTag == genHeaderExportDepTag {
Dan Willemsenb3454ab2016-09-28 17:34:58 -07001003 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags)
Dan Willemsen847dcc72016-09-29 12:13:36 -07001004 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps,
1005 genRule.GeneratedSourceFiles()...)
Jayant Chowdhary715cac32017-04-20 06:53:59 -07001006 // Add these re-exported flags to help header-abi-dumper to infer the abi exported by a library.
1007 c.sabi.Properties.ReexportedIncludeFlags = append(c.sabi.Properties.ReexportedIncludeFlags, flags)
1008
Dan Willemsenb3454ab2016-09-28 17:34:58 -07001009 }
Dan Willemsenb40aab62016-04-20 14:21:14 -07001010 } else {
Jeff Gaston2370af02017-09-27 17:01:44 -07001011 ctx.ModuleErrorf("module %q is not a genrule", depName)
Dan Willemsenb40aab62016-04-20 14:21:14 -07001012 }
Dan Willemsenc77a0b32017-09-18 23:19:12 -07001013 case linkerScriptDepTag:
Jeff Gaston2370af02017-09-27 17:01:44 -07001014 if genRule, ok := dep.(genrule.SourceFileGenerator); ok {
Dan Willemsenc77a0b32017-09-18 23:19:12 -07001015 files := genRule.GeneratedSourceFiles()
1016 if len(files) == 1 {
1017 depPaths.LinkerScript = android.OptionalPathForPath(files[0])
1018 } else if len(files) > 1 {
Jeff Gaston2370af02017-09-27 17:01:44 -07001019 ctx.ModuleErrorf("module %q can only generate a single file if used for a linker script", depName)
Dan Willemsenc77a0b32017-09-18 23:19:12 -07001020 }
1021 } else {
Jeff Gaston2370af02017-09-27 17:01:44 -07001022 ctx.ModuleErrorf("module %q is not a genrule", depName)
Dan Willemsenc77a0b32017-09-18 23:19:12 -07001023 }
Dan Willemsenb40aab62016-04-20 14:21:14 -07001024 default:
Jeff Gaston2370af02017-09-27 17:01:44 -07001025 ctx.ModuleErrorf("depends on non-cc module %q", depName)
Colin Crossca860ac2016-01-04 14:34:37 -08001026 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001027 return
1028 }
1029
Jeff Gaston2370af02017-09-27 17:01:44 -07001030 // some validation
1031 if !aDep.Enabled() {
Colin Crossa8f5e9a2016-12-13 12:51:11 -08001032 if ctx.AConfig().AllowMissingDependencies() {
Jeff Gaston2370af02017-09-27 17:01:44 -07001033 ctx.AddMissingDependencies([]string{depName})
Colin Crossa8f5e9a2016-12-13 12:51:11 -08001034 } else {
Jeff Gaston2370af02017-09-27 17:01:44 -07001035 ctx.ModuleErrorf("depends on disabled module %q", depName)
Colin Crossa8f5e9a2016-12-13 12:51:11 -08001036 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001037 return
1038 }
Jeff Gaston2370af02017-09-27 17:01:44 -07001039 if aDep.Target().Os != ctx.Os() {
1040 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1041 return
1042 }
1043 if aDep.Target().Arch.ArchType != ctx.Arch().ArchType {
1044 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
Colin Crossa1ad8d12016-06-01 17:09:44 -07001045 return
1046 }
1047
Jeff Gaston2370af02017-09-27 17:01:44 -07001048 // re-exporting flags
1049 if depTag == reuseObjTag {
1050 if l, ok := ccDep.compiler.(libraryInterface); ok {
Colin Crossbbc9f4d2017-05-03 16:24:55 -07001051 objs, flags, deps := l.reuseObjs()
Colin Cross10d22312017-05-03 11:01:58 -07001052 depPaths.Objs = depPaths.Objs.Append(objs)
1053 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Colin Crossbbc9f4d2017-05-03 16:24:55 -07001054 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps, deps...)
Colin Crossbba99042016-11-23 15:45:05 -08001055 return
1056 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001057 }
Jeff Gaston2370af02017-09-27 17:01:44 -07001058 if t, ok := depTag.(dependencyTag); ok && t.library {
1059 if i, ok := ccDep.linker.(exportedFlagsProducer); ok {
Dan Willemsen76f08272016-07-09 00:14:08 -07001060 flags := i.exportedFlags()
Dan Willemsen847dcc72016-09-29 12:13:36 -07001061 deps := i.exportedFlagsDeps()
Dan Willemsen76f08272016-07-09 00:14:08 -07001062 depPaths.Flags = append(depPaths.Flags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -07001063 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders, deps...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001064
1065 if t.reexportFlags {
Dan Willemsen76f08272016-07-09 00:14:08 -07001066 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -07001067 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps, deps...)
Jayant Chowdhary715cac32017-04-20 06:53:59 -07001068 // Add these re-exported flags to help header-abi-dumper to infer the abi exported by a library.
Jayant Chowdharyaf6eb712017-08-23 16:08:29 -07001069 // Re-exported shared library headers must be included as well since they can help us with type information
1070 // about template instantiations (instantiated from their headers).
1071 c.sabi.Properties.ReexportedIncludeFlags = append(c.sabi.Properties.ReexportedIncludeFlags, flags...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001072 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001073 }
Dan Willemsena96ff642016-06-07 12:34:45 -07001074
Jeff Gaston2370af02017-09-27 17:01:44 -07001075 checkLinkType(ctx, c, ccDep)
Colin Crossc99deeb2016-04-11 15:06:20 -07001076 }
1077
Colin Cross26c34ed2016-09-30 17:10:16 -07001078 var ptr *android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -07001079 var depPtr *android.Paths
Colin Crossc99deeb2016-04-11 15:06:20 -07001080
Jeff Gaston2370af02017-09-27 17:01:44 -07001081 linkFile := ccDep.outputFile
Colin Cross26c34ed2016-09-30 17:10:16 -07001082 depFile := android.OptionalPath{}
1083
Jeff Gaston2370af02017-09-27 17:01:44 -07001084 switch depTag {
Dan Albert914449f2016-06-17 16:45:24 -07001085 case ndkStubDepTag, sharedDepTag, sharedExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001086 ptr = &depPaths.SharedLibs
1087 depPtr = &depPaths.SharedLibsDeps
Jeff Gaston2370af02017-09-27 17:01:44 -07001088 depFile = ccDep.linker.(libraryInterface).toc()
Dan Albert914449f2016-06-17 16:45:24 -07001089 case lateSharedDepTag, ndkLateStubDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001090 ptr = &depPaths.LateSharedLibs
1091 depPtr = &depPaths.LateSharedLibsDeps
Jeff Gaston2370af02017-09-27 17:01:44 -07001092 depFile = ccDep.linker.(libraryInterface).toc()
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001093 case staticDepTag, staticExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001094 ptr = &depPaths.StaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -07001095 case lateStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001096 ptr = &depPaths.LateStaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -07001097 case wholeStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001098 ptr = &depPaths.WholeStaticLibs
Jeff Gaston2370af02017-09-27 17:01:44 -07001099 staticLib, ok := ccDep.linker.(libraryInterface)
Colin Crossb916a382016-07-29 17:28:03 -07001100 if !ok || !staticLib.static() {
Jeff Gaston2370af02017-09-27 17:01:44 -07001101 ctx.ModuleErrorf("module %q not a static library", depName)
Colin Crossc99deeb2016-04-11 15:06:20 -07001102 return
1103 }
1104
1105 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
Jeff Gaston2370af02017-09-27 17:01:44 -07001106 postfix := " (required by " + ctx.OtherModuleName(dep) + ")"
Colin Crossc99deeb2016-04-11 15:06:20 -07001107 for i := range missingDeps {
1108 missingDeps[i] += postfix
1109 }
1110 ctx.AddMissingDependencies(missingDeps)
1111 }
Dan Willemsen5cb580f2016-09-26 17:33:01 -07001112 depPaths.WholeStaticLibObjs = depPaths.WholeStaticLibObjs.Append(staticLib.objs())
Colin Cross5950f382016-12-13 12:50:57 -08001113 case headerDepTag:
1114 // Nothing
Colin Crossc99deeb2016-04-11 15:06:20 -07001115 case objDepTag:
Dan Willemsen5cb580f2016-09-26 17:33:01 -07001116 depPaths.Objs.objFiles = append(depPaths.Objs.objFiles, linkFile.Path())
Colin Crossc99deeb2016-04-11 15:06:20 -07001117 case crtBeginDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001118 depPaths.CrtBegin = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -07001119 case crtEndDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001120 depPaths.CrtEnd = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -07001121 }
1122
Jeff Gaston2370af02017-09-27 17:01:44 -07001123 switch depTag {
Dan Willemsen581341d2017-02-09 16:16:31 -08001124 case staticDepTag, staticExportDepTag, lateStaticDepTag:
Jeff Gaston2370af02017-09-27 17:01:44 -07001125 staticLib, ok := ccDep.linker.(libraryInterface)
Dan Willemsen581341d2017-02-09 16:16:31 -08001126 if !ok || !staticLib.static() {
Jeff Gaston2370af02017-09-27 17:01:44 -07001127 ctx.ModuleErrorf("module %q not a static library", depName)
Dan Willemsen581341d2017-02-09 16:16:31 -08001128 return
1129 }
1130
1131 // When combining coverage files for shared libraries and executables, coverage files
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001132 // in static libraries act as if they were whole static libraries. The same goes for
1133 // source based Abi dump files.
Dan Willemsen581341d2017-02-09 16:16:31 -08001134 depPaths.StaticLibObjs.coverageFiles = append(depPaths.StaticLibObjs.coverageFiles,
1135 staticLib.objs().coverageFiles...)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001136 depPaths.StaticLibObjs.sAbiDumpFiles = append(depPaths.StaticLibObjs.sAbiDumpFiles,
1137 staticLib.objs().sAbiDumpFiles...)
Jeff Gaston2370af02017-09-27 17:01:44 -07001138
Dan Willemsen581341d2017-02-09 16:16:31 -08001139 }
1140
Colin Cross26c34ed2016-09-30 17:10:16 -07001141 if ptr != nil {
Colin Crossce75d2c2016-10-06 16:12:58 -07001142 if !linkFile.Valid() {
Jeff Gaston2370af02017-09-27 17:01:44 -07001143 ctx.ModuleErrorf("module %q missing output file", depName)
Colin Crossce75d2c2016-10-06 16:12:58 -07001144 return
1145 }
Colin Cross26c34ed2016-09-30 17:10:16 -07001146 *ptr = append(*ptr, linkFile.Path())
1147 }
1148
Colin Crossc99deeb2016-04-11 15:06:20 -07001149 if depPtr != nil {
Colin Cross26c34ed2016-09-30 17:10:16 -07001150 dep := depFile
1151 if !dep.Valid() {
1152 dep = linkFile
1153 }
1154 *depPtr = append(*depPtr, dep.Path())
Colin Crossca860ac2016-01-04 14:34:37 -08001155 }
Jiyong Park27b188b2017-07-18 13:23:39 +09001156
Jeff Gaston2370af02017-09-27 17:01:44 -07001157 // Export the shared libs to Make.
1158 switch depTag {
Jiyong Park27b188b2017-07-18 13:23:39 +09001159 case sharedDepTag, sharedExportDepTag, lateSharedDepTag:
Jeff Gaston2370af02017-09-27 17:01:44 -07001160 libName := strings.TrimSuffix(depName, llndkLibrarySuffix)
Jiyong Park27b188b2017-07-18 13:23:39 +09001161 libName = strings.TrimPrefix(libName, "prebuilt_")
Jiyong Parkd5b18a52017-08-03 21:22:50 +09001162 isLLndk := inList(libName, llndkLibraries)
Jeff Gaston2370af02017-09-27 17:01:44 -07001163 var makeLibName string
1164 bothVendorAndCoreVariantsExist := Bool(ccDep.VendorProperties.Vendor_available) || isLLndk
1165 if c.useVndk() && bothVendorAndCoreVariantsExist {
1166 // The vendor module in Make will have been renamed to not conflict with the core
1167 // module, so update the dependency name here accordingly.
1168 makeLibName = libName + vendorSuffix
1169 } else {
1170 makeLibName = libName
Jiyong Park27b188b2017-07-18 13:23:39 +09001171 }
1172 // Note: the order of libs in this list is not important because
Jeff Gaston2370af02017-09-27 17:01:44 -07001173 // they merely serve as Make dependencies and do not affect this lib itself.
1174 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, makeLibName)
Jiyong Park27b188b2017-07-18 13:23:39 +09001175 }
Colin Crossca860ac2016-01-04 14:34:37 -08001176 })
1177
Colin Crossdd84e052017-05-17 13:44:16 -07001178 // Dedup exported flags from dependencies
1179 depPaths.Flags = firstUniqueElements(depPaths.Flags)
Dan Willemsenfe92c962017-08-29 12:28:37 -07001180 depPaths.GeneratedHeaders = android.FirstUniquePaths(depPaths.GeneratedHeaders)
1181 depPaths.ReexportedFlags = firstUniqueElements(depPaths.ReexportedFlags)
1182 depPaths.ReexportedFlagsDeps = android.FirstUniquePaths(depPaths.ReexportedFlagsDeps)
1183
1184 if c.sabi != nil {
1185 c.sabi.Properties.ReexportedIncludeFlags = firstUniqueElements(c.sabi.Properties.ReexportedIncludeFlags)
1186 }
Colin Crossdd84e052017-05-17 13:44:16 -07001187
Colin Crossca860ac2016-01-04 14:34:37 -08001188 return depPaths
1189}
1190
1191func (c *Module) InstallInData() bool {
1192 if c.installer == nil {
1193 return false
1194 }
Vishwath Mohan1dd88392017-03-29 22:00:18 -07001195 return c.installer.inData()
1196}
1197
1198func (c *Module) InstallInSanitizerDir() bool {
1199 if c.installer == nil {
1200 return false
1201 }
1202 if c.sanitize != nil && c.sanitize.inSanitizerDir() {
Colin Cross94610402016-08-29 13:41:32 -07001203 return true
1204 }
Vishwath Mohan1dd88392017-03-29 22:00:18 -07001205 return c.installer.inSanitizerDir()
Colin Crossca860ac2016-01-04 14:34:37 -08001206}
1207
Dan Willemsen4aa75ca2016-09-28 16:18:03 -07001208func (c *Module) HostToolPath() android.OptionalPath {
1209 if c.installer == nil {
1210 return android.OptionalPath{}
1211 }
1212 return c.installer.hostToolPath()
1213}
1214
Nan Zhangd4e641b2017-07-12 12:55:28 -07001215func (c *Module) IntermPathForModuleOut() android.OptionalPath {
1216 return c.outputFile
1217}
1218
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001219func (c *Module) Srcs() android.Paths {
1220 if c.outputFile.Valid() {
1221 return android.Paths{c.outputFile.Path()}
1222 }
1223 return android.Paths{}
1224}
1225
Colin Cross2ba19d92015-05-07 15:44:20 -07001226//
Colin Crosscfad1192015-11-02 16:43:11 -08001227// Defaults
1228//
Colin Crossca860ac2016-01-04 14:34:37 -08001229type Defaults struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001230 android.ModuleBase
Colin Cross1f44a3a2017-07-07 14:33:33 -07001231 android.DefaultsModuleBase
Colin Crosscfad1192015-11-02 16:43:11 -08001232}
1233
Colin Cross635c3b02016-05-18 15:37:25 -07001234func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -08001235}
1236
Colin Cross1e676be2016-10-12 14:38:15 -07001237func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
1238}
1239
Colin Cross36242852017-06-23 15:06:31 -07001240func defaultsFactory() android.Module {
Colin Crosse1d764e2016-08-18 14:18:32 -07001241 return DefaultsFactory()
1242}
1243
Colin Cross36242852017-06-23 15:06:31 -07001244func DefaultsFactory(props ...interface{}) android.Module {
Colin Crossca860ac2016-01-04 14:34:37 -08001245 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -08001246
Colin Cross36242852017-06-23 15:06:31 -07001247 module.AddProperties(props...)
1248 module.AddProperties(
Colin Crossca860ac2016-01-04 14:34:37 -08001249 &BaseProperties{},
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001250 &VendorProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001251 &BaseCompilerProperties{},
1252 &BaseLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -07001253 &LibraryProperties{},
Colin Cross919281a2016-04-05 16:42:05 -07001254 &FlagExporterProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001255 &BinaryLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -07001256 &TestProperties{},
1257 &TestBinaryProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001258 &UnusedProperties{},
1259 &StlProperties{},
Colin Cross16b23492016-01-06 14:41:07 -08001260 &SanitizeProperties{},
Colin Cross665dce92016-04-28 14:50:03 -07001261 &StripProperties{},
Dan Willemsen7424d612016-09-01 13:45:39 -07001262 &InstallerProperties{},
Dan Willemsena03cf6d2016-09-26 15:45:04 -07001263 &TidyProperties{},
Dan Willemsen581341d2017-02-09 16:16:31 -08001264 &CoverageProperties{},
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001265 &SAbiProperties{},
Justin Yun4b2382f2017-07-26 14:22:10 +09001266 &VndkProperties{},
Stephen Craneba090d12017-05-09 15:44:35 -07001267 &LTOProperties{},
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -07001268 &PgoProperties{},
Colin Crosse1d764e2016-08-18 14:18:32 -07001269 )
Colin Crosscfad1192015-11-02 16:43:11 -08001270
Colin Cross1f44a3a2017-07-07 14:33:33 -07001271 android.InitDefaultsModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001272
1273 return module
Colin Crosscfad1192015-11-02 16:43:11 -08001274}
1275
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001276const (
1277 // coreMode is the variant used for framework-private libraries, or
1278 // SDK libraries. (which framework-private libraries can use)
1279 coreMode = "core"
1280
1281 // vendorMode is the variant used for /vendor code that compiles
1282 // against the VNDK.
1283 vendorMode = "vendor"
1284)
1285
1286func vendorMutator(mctx android.BottomUpMutatorContext) {
1287 if mctx.Os() != android.Android {
1288 return
1289 }
1290
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001291 if genrule, ok := mctx.Module().(*genrule.Module); ok {
1292 if props, ok := genrule.Extra.(*VendorProperties); ok {
1293 if !mctx.DeviceConfig().CompileVndk() {
1294 mctx.CreateVariations(coreMode)
1295 } else if Bool(props.Vendor_available) {
1296 mctx.CreateVariations(coreMode, vendorMode)
1297 } else if mctx.Vendor() {
1298 mctx.CreateVariations(vendorMode)
1299 } else {
1300 mctx.CreateVariations(coreMode)
1301 }
1302 }
1303 }
1304
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001305 m, ok := mctx.Module().(*Module)
1306 if !ok {
1307 return
1308 }
1309
1310 // Sanity check
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001311 if Bool(m.VendorProperties.Vendor_available) && mctx.Vendor() {
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001312 mctx.PropertyErrorf("vendor_available",
1313 "doesn't make sense at the same time as `vendor: true` or `proprietary: true`")
1314 return
1315 }
Justin Yun8effde42017-06-23 19:24:43 +09001316 if vndk := m.vndkdep; vndk != nil {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001317 if vndk.isVndk() && !Bool(m.VendorProperties.Vendor_available) {
Justin Yun8effde42017-06-23 19:24:43 +09001318 mctx.PropertyErrorf("vndk",
1319 "has to define `vendor_available: true` to enable vndk")
1320 return
1321 }
1322 if !vndk.isVndk() && vndk.isVndkSp() {
1323 mctx.PropertyErrorf("vndk",
1324 "must set `enabled: true` to set `support_system_process: true`")
1325 return
1326 }
1327 }
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001328
1329 if !mctx.DeviceConfig().CompileVndk() {
1330 // If the device isn't compiling against the VNDK, we always
1331 // use the core mode.
1332 mctx.CreateVariations(coreMode)
1333 } else if _, ok := m.linker.(*llndkStubDecorator); ok {
1334 // LL-NDK stubs only exist in the vendor variant, since the
1335 // real libraries will be used in the core variant.
1336 mctx.CreateVariations(vendorMode)
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001337 } else if Bool(m.VendorProperties.Vendor_available) {
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001338 // This will be available in both /system and /vendor
Justin Yun8effde42017-06-23 19:24:43 +09001339 // or a /system directory that is available to vendor.
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001340 mod := mctx.CreateVariations(coreMode, vendorMode)
1341 mod[1].(*Module).Properties.UseVndk = true
1342 } else if mctx.Vendor() && m.Properties.Sdk_version == "" {
1343 // This will be available in /vendor only
1344 mod := mctx.CreateVariations(vendorMode)
1345 mod[0].(*Module).Properties.UseVndk = true
1346 } else {
1347 // This is either in /system (or similar: /data), or is a
1348 // modules built with the NDK. Modules built with the NDK
1349 // will be restricted using the existing link type checks.
1350 mctx.CreateVariations(coreMode)
1351 }
1352}
1353
Colin Crossdd84e052017-05-17 13:44:16 -07001354// firstUniqueElements returns all unique elements of a slice, keeping the first copy of each
1355// modifies the slice contents in place, and returns a subslice of the original slice
1356func firstUniqueElements(list []string) []string {
1357 k := 0
1358outer:
1359 for i := 0; i < len(list); i++ {
1360 for j := 0; j < k; j++ {
1361 if list[i] == list[j] {
1362 continue outer
1363 }
1364 }
1365 list[k] = list[i]
1366 k++
1367 }
1368 return list[:k]
1369}
1370
Jeff Gaston2370af02017-09-27 17:01:44 -07001371// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each.
1372// It modifies the slice contents in place, and returns a subslice of the original slice
Colin Cross74d1ec02015-04-28 13:30:13 -07001373func lastUniqueElements(list []string) []string {
1374 totalSkip := 0
1375 for i := len(list) - 1; i >= totalSkip; i-- {
1376 skip := 0
1377 for j := i - 1; j >= totalSkip; j-- {
1378 if list[i] == list[j] {
1379 skip++
1380 } else {
1381 list[j+skip] = list[j]
1382 }
1383 }
1384 totalSkip += skip
1385 }
1386 return list[totalSkip:]
1387}
Colin Cross06a931b2015-10-28 17:23:31 -07001388
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -07001389func getCurrentNdkPrebuiltVersion(ctx DepsContext) string {
1390 if ctx.AConfig().PlatformSdkVersionInt() > config.NdkMaxPrebuiltVersionInt {
1391 return strconv.Itoa(config.NdkMaxPrebuiltVersionInt)
1392 }
1393 return ctx.AConfig().PlatformSdkVersion()
1394}
1395
Colin Cross06a931b2015-10-28 17:23:31 -07001396var Bool = proptools.Bool