blob: 05695637aebbcbbfd825ef31757dcda0d127cc45 [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) {
Jiyong Parkf9332f12018-02-01 00:54:12 +090037 ctx.BottomUp("image", imageMutator).Parallel()
Colin Crosse40b4ea2018-10-02 22:25:58 -070038 ctx.BottomUp("link", LinkageMutator).Parallel()
Jiyong Parkd5b18a52017-08-03 21:22:50 +090039 ctx.BottomUp("vndk", vndkMutator).Parallel()
Colin Cross1e676be2016-10-12 14:38:15 -070040 ctx.BottomUp("ndk_api", ndkApiMutator).Parallel()
41 ctx.BottomUp("test_per_src", testPerSrcMutator).Parallel()
Jiyong Park7ed9de32018-10-15 22:25:07 +090042 ctx.BottomUp("version", versionMutator).Parallel()
Colin Crosse40b4ea2018-10-02 22:25:58 -070043 ctx.BottomUp("begin", BeginMutator).Parallel()
Colin Cross1e676be2016-10-12 14:38:15 -070044 })
Colin Cross16b23492016-01-06 14:41:07 -080045
Colin Cross1e676be2016-10-12 14:38:15 -070046 android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
47 ctx.TopDown("asan_deps", sanitizerDepsMutator(asan))
48 ctx.BottomUp("asan", sanitizerMutator(asan)).Parallel()
Colin Cross16b23492016-01-06 14:41:07 -080049
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070050 ctx.TopDown("hwasan_deps", sanitizerDepsMutator(hwasan))
51 ctx.BottomUp("hwasan", sanitizerMutator(hwasan)).Parallel()
52
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000053 ctx.TopDown("cfi_deps", sanitizerDepsMutator(cfi))
54 ctx.BottomUp("cfi", sanitizerMutator(cfi)).Parallel()
55
Colin Cross1e676be2016-10-12 14:38:15 -070056 ctx.TopDown("tsan_deps", sanitizerDepsMutator(tsan))
57 ctx.BottomUp("tsan", sanitizerMutator(tsan)).Parallel()
Dan Willemsen581341d2017-02-09 16:16:31 -080058
Colin Cross6b753602018-06-21 13:03:07 -070059 ctx.TopDown("sanitize_runtime_deps", sanitizerRuntimeDepsMutator)
Ivan Lozano30c5db22018-02-21 15:49:20 -080060
Pirama Arumuga Nainar0b882f02018-04-23 22:44:39 +000061 ctx.BottomUp("coverage", coverageLinkingMutator).Parallel()
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -080062 ctx.TopDown("vndk_deps", sabiDepsMutator)
Stephen Craneba090d12017-05-09 15:44:35 -070063
64 ctx.TopDown("lto_deps", ltoDepsMutator)
65 ctx.BottomUp("lto", ltoMutator).Parallel()
Colin Cross1e676be2016-10-12 14:38:15 -070066 })
Colin Crossb98c8b02016-07-29 13:44:28 -070067
68 pctx.Import("android/soong/cc/config")
Colin Cross463a90e2015-06-17 14:20:06 -070069}
70
Colin Crossca860ac2016-01-04 14:34:37 -080071type Deps struct {
72 SharedLibs, LateSharedLibs []string
73 StaticLibs, LateStaticLibs, WholeStaticLibs []string
Colin Cross5950f382016-12-13 12:50:57 -080074 HeaderLibs []string
Logan Chien43d34c32017-12-20 01:17:32 +080075 RuntimeLibs []string
Colin Crossc472d572015-03-17 15:06:21 -070076
Colin Cross5950f382016-12-13 12:50:57 -080077 ReexportSharedLibHeaders, ReexportStaticLibHeaders, ReexportHeaderLibHeaders []string
Dan Willemsen490a8dc2016-06-06 18:22:19 -070078
Colin Cross81413472016-04-11 14:37:39 -070079 ObjFiles []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -070080
Dan Willemsenb40aab62016-04-20 14:21:14 -070081 GeneratedSources []string
82 GeneratedHeaders []string
83
Dan Willemsenb3454ab2016-09-28 17:34:58 -070084 ReexportGeneratedHeaders []string
85
Colin Cross97ba0732015-03-23 17:50:24 -070086 CrtBegin, CrtEnd string
Dan Willemsena0790e32018-10-12 00:24:23 -070087
88 // Used for host bionic
89 LinkerFlagsFile string
90 DynamicLinker string
Colin Crossc472d572015-03-17 15:06:21 -070091}
92
Colin Crossca860ac2016-01-04 14:34:37 -080093type PathDeps struct {
Colin Cross26c34ed2016-09-30 17:10:16 -070094 // Paths to .so files
95 SharedLibs, LateSharedLibs android.Paths
96 // Paths to the dependencies to use for .so files (.so.toc files)
97 SharedLibsDeps, LateSharedLibsDeps android.Paths
98 // Paths to .a files
Colin Cross635c3b02016-05-18 15:37:25 -070099 StaticLibs, LateStaticLibs, WholeStaticLibs android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700100
Colin Cross26c34ed2016-09-30 17:10:16 -0700101 // Paths to .o files
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700102 Objs Objects
Dan Willemsen581341d2017-02-09 16:16:31 -0800103 StaticLibObjs Objects
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700104 WholeStaticLibObjs Objects
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700105
Colin Cross26c34ed2016-09-30 17:10:16 -0700106 // Paths to generated source files
Colin Cross635c3b02016-05-18 15:37:25 -0700107 GeneratedSources android.Paths
108 GeneratedHeaders android.Paths
Dan Willemsenb40aab62016-04-20 14:21:14 -0700109
Dan Willemsen76f08272016-07-09 00:14:08 -0700110 Flags, ReexportedFlags []string
Dan Willemsen847dcc72016-09-29 12:13:36 -0700111 ReexportedFlagsDeps android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700112
Colin Cross26c34ed2016-09-30 17:10:16 -0700113 // Paths to crt*.o files
Colin Cross635c3b02016-05-18 15:37:25 -0700114 CrtBegin, CrtEnd android.OptionalPath
Dan Willemsena0790e32018-10-12 00:24:23 -0700115
116 // Path to the file container flags to use with the linker
117 LinkerFlagsFile android.OptionalPath
118
119 // Path to the dynamic linker binary
120 DynamicLinker android.OptionalPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700121}
122
Colin Crossca860ac2016-01-04 14:34:37 -0800123type Flags struct {
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700124 GlobalFlags []string // Flags that apply to C, C++, and assembly source files
125 ArFlags []string // Flags that apply to ar
126 AsFlags []string // Flags that apply to assembly source files
127 CFlags []string // Flags that apply to C and C++ source files
128 ToolingCFlags []string // Flags that apply to C and C++ source files parsed by clang LibTooling tools
129 ConlyFlags []string // Flags that apply to C source files
130 CppFlags []string // Flags that apply to C++ source files
131 ToolingCppFlags []string // Flags that apply to C++ source files parsed by clang LibTooling tools
132 YaccFlags []string // Flags that apply to Yacc source files
133 protoFlags []string // Flags that apply to proto source files
Joe Onorato09e94ab2017-11-18 18:23:14 -0800134 protoOutParams []string // Flags that modify the output of proto generated files
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700135 aidlFlags []string // Flags that apply to aidl source files
136 rsFlags []string // Flags that apply to renderscript source files
137 LdFlags []string // Flags that apply to linker command lines
138 libFlags []string // Flags to add libraries early to the link order
139 TidyFlags []string // Flags that apply to clang-tidy
140 SAbiFlags []string // Flags that apply to header-abi-dumper
141 YasmFlags []string // Flags that apply to yasm assembly source files
Colin Cross28344522015-04-22 13:07:53 -0700142
Colin Crossc3199482017-03-30 15:03:04 -0700143 // Global include flags that apply to C, C++, and assembly source files
144 // These must be after any module include flags, which will be in GlobalFlags.
145 SystemIncludeFlags []string
146
Colin Crossb98c8b02016-07-29 13:44:28 -0700147 Toolchain config.Toolchain
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700148 Tidy bool
Dan Willemsen581341d2017-02-09 16:16:31 -0800149 Coverage bool
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800150 SAbiDump bool
Dan Willemsenab9f4262018-02-14 13:58:34 -0800151 ProtoRoot bool
Colin Crossca860ac2016-01-04 14:34:37 -0800152
153 RequiredInstructionSet string
Colin Cross16b23492016-01-06 14:41:07 -0800154 DynamicLinker string
155
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700156 CFlagsDeps android.Paths // Files depended on by compiler flags
157 LdFlagsDeps android.Paths // Files depended on by linker flags
Colin Cross18c0c5a2016-12-01 14:45:23 -0800158
159 GroupStaticLibs bool
Zhizhou Yang51be6322018-02-08 18:32:11 -0800160 ArGoldPlugin bool // Whether LLVM gold plugin option is passed to llvm-ar
Colin Crossc472d572015-03-17 15:06:21 -0700161}
162
Colin Cross81413472016-04-11 14:37:39 -0700163type ObjectLinkerProperties struct {
164 // names of other cc_object modules to link into this module using partial linking
165 Objs []string `android:"arch_variant"`
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700166
167 // if set, add an extra objcopy --prefix-symbols= step
Nan Zhang0007d812017-11-07 10:57:05 -0800168 Prefix_symbols *string
Colin Cross81413472016-04-11 14:37:39 -0700169}
170
Colin Crossca860ac2016-01-04 14:34:37 -0800171// Properties used to compile all C or C++ modules
172type BaseProperties struct {
Dan Willemsen742a5452018-07-23 17:19:36 -0700173 // Deprecated. true is the default, false is invalid.
Colin Crossca860ac2016-01-04 14:34:37 -0800174 Clang *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700175
176 // Minimum sdk version supported when compiling against the ndk
Nan Zhang0007d812017-11-07 10:57:05 -0800177 Sdk_version *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700178
Jaewoong Jung3affc072018-11-06 18:00:39 +0000179 AndroidMkSharedLibs []string `blueprint:"mutated"`
180 AndroidMkRuntimeLibs []string `blueprint:"mutated"`
181 HideFromMake bool `blueprint:"mutated"`
182 PreventInstall bool `blueprint:"mutated"`
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700183
184 UseVndk bool `blueprint:"mutated"`
Colin Cross5beccee2017-12-07 15:28:59 -0800185
186 // *.logtags files, to combine together in order to generate the /system/etc/event-log-tags
187 // file
188 Logtags []string
Jiyong Parkf9332f12018-02-01 00:54:12 +0900189
190 // Make this module available when building for recovery
191 Recovery_available *bool
192
193 InRecovery bool `blueprint:"mutated"`
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700194}
195
196type VendorProperties struct {
Jiyong Park82e2bf32017-08-16 14:05:54 +0900197 // whether this module should be allowed to be directly depended by other
198 // modules with `vendor: true`, `proprietary: true`, or `vendor_available:true`.
199 // If set to true, two variants will be built separately, one like
200 // normal, and the other limited to the set of libraries and headers
201 // that are exposed to /vendor modules.
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700202 //
203 // The vendor variant may be used with a different (newer) /system,
204 // so it shouldn't have any unversioned runtime dependencies, or
205 // make assumptions about the system that may not be true in the
206 // future.
207 //
Jiyong Park82e2bf32017-08-16 14:05:54 +0900208 // If set to false, this module becomes inaccessible from /vendor modules.
209 //
210 // Default value is true when vndk: {enabled: true} or vendor: true.
211 //
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700212 // Nothing happens if BOARD_VNDK_VERSION isn't set in the BoardConfig.mk
213 Vendor_available *bool
Jiyong Park5fb8c102018-04-09 12:03:06 +0900214
215 // whether this module is capable of being loaded with other instance
216 // (possibly an older version) of the same module in the same process.
217 // Currently, a shared library that is a member of VNDK (vndk: {enabled: true})
218 // can be double loaded in a vendor process if the library is also a
219 // (direct and indirect) dependency of an LLNDK library. Such libraries must be
220 // explicitly marked as `double_loadable: true` by the owner, or the dependency
221 // from the LLNDK lib should be cut if the lib is not designed to be double loaded.
222 Double_loadable *bool
Colin Crossca860ac2016-01-04 14:34:37 -0800223}
224
Colin Crossca860ac2016-01-04 14:34:37 -0800225type ModuleContextIntf interface {
Colin Crossca860ac2016-01-04 14:34:37 -0800226 static() bool
227 staticBinary() bool
Colin Crossb98c8b02016-07-29 13:44:28 -0700228 toolchain() config.Toolchain
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700229 useSdk() bool
Colin Crossca860ac2016-01-04 14:34:37 -0800230 sdkVersion() string
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700231 useVndk() bool
Justin Yun8effde42017-06-23 19:24:43 +0900232 isVndk() bool
233 isVndkSp() bool
Logan Chienf3511742017-10-31 18:04:35 +0800234 isVndkExt() bool
Jiyong Parkf9332f12018-02-01 00:54:12 +0900235 inRecovery() bool
Logan Chien2f2b8902018-07-10 15:01:19 +0800236 shouldCreateVndkSourceAbiDump() bool
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700237 selectedStl() string
Colin Crossce75d2c2016-10-06 16:12:58 -0700238 baseModuleName() string
Logan Chienf3511742017-10-31 18:04:35 +0800239 getVndkExtendsModuleName() string
Yi Kong7e53c572018-02-14 18:16:12 +0800240 isPgoCompile() bool
Colin Crossca860ac2016-01-04 14:34:37 -0800241}
242
243type ModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700244 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800245 ModuleContextIntf
246}
247
248type BaseModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700249 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800250 ModuleContextIntf
251}
252
Colin Cross37047f12016-12-13 17:06:13 -0800253type DepsContext interface {
254 android.BottomUpMutatorContext
255 ModuleContextIntf
256}
257
Colin Crossca860ac2016-01-04 14:34:37 -0800258type feature interface {
259 begin(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800260 deps(ctx DepsContext, deps Deps) Deps
Colin Crossca860ac2016-01-04 14:34:37 -0800261 flags(ctx ModuleContext, flags Flags) Flags
262 props() []interface{}
263}
264
265type compiler interface {
Colin Cross42742b82016-08-01 13:20:05 -0700266 compilerInit(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800267 compilerDeps(ctx DepsContext, deps Deps) Deps
Colin Crossf18e1102017-11-16 14:33:08 -0800268 compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags
Colin Cross42742b82016-08-01 13:20:05 -0700269 compilerProps() []interface{}
270
Colin Cross76fada02016-07-27 10:31:13 -0700271 appendCflags([]string)
272 appendAsflags([]string)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700273 compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects
Colin Crossca860ac2016-01-04 14:34:37 -0800274}
275
276type linker interface {
Colin Cross42742b82016-08-01 13:20:05 -0700277 linkerInit(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800278 linkerDeps(ctx DepsContext, deps Deps) Deps
Colin Cross42742b82016-08-01 13:20:05 -0700279 linkerFlags(ctx ModuleContext, flags Flags) Flags
280 linkerProps() []interface{}
281
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700282 link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path
Colin Cross76fada02016-07-27 10:31:13 -0700283 appendLdflags([]string)
Colin Crossca860ac2016-01-04 14:34:37 -0800284}
285
286type installer interface {
Colin Cross42742b82016-08-01 13:20:05 -0700287 installerProps() []interface{}
Colin Cross635c3b02016-05-18 15:37:25 -0700288 install(ctx ModuleContext, path android.Path)
Colin Crossca860ac2016-01-04 14:34:37 -0800289 inData() bool
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700290 inSanitizerDir() bool
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700291 hostToolPath() android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800292}
293
Colin Crossc99deeb2016-04-11 15:06:20 -0700294type dependencyTag struct {
295 blueprint.BaseDependencyTag
296 name string
297 library bool
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700298
299 reexportFlags bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700300}
301
302var (
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700303 sharedDepTag = dependencyTag{name: "shared", library: true}
304 sharedExportDepTag = dependencyTag{name: "shared", library: true, reexportFlags: true}
305 lateSharedDepTag = dependencyTag{name: "late shared", library: true}
306 staticDepTag = dependencyTag{name: "static", library: true}
307 staticExportDepTag = dependencyTag{name: "static", library: true, reexportFlags: true}
308 lateStaticDepTag = dependencyTag{name: "late static", library: true}
309 wholeStaticDepTag = dependencyTag{name: "whole static", library: true, reexportFlags: true}
Colin Cross32ec36c2016-12-15 07:39:51 -0800310 headerDepTag = dependencyTag{name: "header", library: true}
311 headerExportDepTag = dependencyTag{name: "header", library: true, reexportFlags: true}
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700312 genSourceDepTag = dependencyTag{name: "gen source"}
313 genHeaderDepTag = dependencyTag{name: "gen header"}
314 genHeaderExportDepTag = dependencyTag{name: "gen header", reexportFlags: true}
315 objDepTag = dependencyTag{name: "obj"}
316 crtBeginDepTag = dependencyTag{name: "crtbegin"}
317 crtEndDepTag = dependencyTag{name: "crtend"}
Dan Willemsena0790e32018-10-12 00:24:23 -0700318 linkerFlagsDepTag = dependencyTag{name: "linker flags file"}
319 dynamicLinkerDepTag = dependencyTag{name: "dynamic linker"}
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700320 reuseObjTag = dependencyTag{name: "reuse objects"}
321 ndkStubDepTag = dependencyTag{name: "ndk stub", library: true}
322 ndkLateStubDepTag = dependencyTag{name: "ndk late stub", library: true}
Logan Chienf3511742017-10-31 18:04:35 +0800323 vndkExtDepTag = dependencyTag{name: "vndk extends", library: true}
Logan Chien43d34c32017-12-20 01:17:32 +0800324 runtimeDepTag = dependencyTag{name: "runtime lib"}
Colin Crossc99deeb2016-04-11 15:06:20 -0700325)
326
Colin Crossca860ac2016-01-04 14:34:37 -0800327// Module contains the properties and members used by all C/C++ module types, and implements
328// the blueprint.Module interface. It delegates to compiler, linker, and installer interfaces
329// to construct the output file. Behavior can be customized with a Customizer interface
330type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700331 android.ModuleBase
Colin Cross1f44a3a2017-07-07 14:33:33 -0700332 android.DefaultableModuleBase
Jiyong Park9d452992018-10-03 00:38:19 +0900333 android.ApexModuleBase
Colin Crossc472d572015-03-17 15:06:21 -0700334
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700335 Properties BaseProperties
336 VendorProperties VendorProperties
Colin Crossfa138792015-04-24 17:31:52 -0700337
Colin Crossca860ac2016-01-04 14:34:37 -0800338 // initialize before calling Init
Colin Cross635c3b02016-05-18 15:37:25 -0700339 hod android.HostOrDeviceSupported
340 multilib android.Multilib
Colin Crossc472d572015-03-17 15:06:21 -0700341
Colin Crossca860ac2016-01-04 14:34:37 -0800342 // delegates, initialize before calling Init
Colin Crossb4ce0ec2016-09-13 13:41:39 -0700343 features []feature
344 compiler compiler
345 linker linker
346 installer installer
347 stl *stl
348 sanitize *sanitize
Dan Willemsen581341d2017-02-09 16:16:31 -0800349 coverage *coverage
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800350 sabi *sabi
Justin Yun8effde42017-06-23 19:24:43 +0900351 vndkdep *vndkdep
Stephen Craneba090d12017-05-09 15:44:35 -0700352 lto *lto
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700353 pgo *pgo
Colin Cross16b23492016-01-06 14:41:07 -0800354
355 androidMkSharedLibDeps []string
Colin Cross74d1ec02015-04-28 13:30:13 -0700356
Colin Cross635c3b02016-05-18 15:37:25 -0700357 outputFile android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800358
Colin Crossb98c8b02016-07-29 13:44:28 -0700359 cachedToolchain config.Toolchain
Colin Crossb916a382016-07-29 17:28:03 -0700360
361 subAndroidMkOnce map[subAndroidMkProvider]bool
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800362
363 // Flags used to compile this module
364 flags Flags
Jeff Gaston294356f2017-09-27 17:05:30 -0700365
366 // When calling a linker, if module A depends on module B, then A must precede B in its command
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -0800367 // line invocation. depsInLinkOrder stores the proper ordering of all of the transitive
Jeff Gaston294356f2017-09-27 17:05:30 -0700368 // deps of this module
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -0800369 depsInLinkOrder android.Paths
370
371 // only non-nil when this is a shared library that reuses the objects of a static library
372 staticVariant *Module
Colin Crossc472d572015-03-17 15:06:21 -0700373}
374
Jiyong Parkc20eee32018-09-05 22:36:17 +0900375func (c *Module) OutputFile() android.OptionalPath {
376 return c.outputFile
377}
378
Colin Cross36242852017-06-23 15:06:31 -0700379func (c *Module) Init() android.Module {
Dan Willemsenf923f2b2018-05-09 13:45:03 -0700380 c.AddProperties(&c.Properties, &c.VendorProperties)
Colin Crossca860ac2016-01-04 14:34:37 -0800381 if c.compiler != nil {
Colin Cross36242852017-06-23 15:06:31 -0700382 c.AddProperties(c.compiler.compilerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800383 }
384 if c.linker != nil {
Colin Cross36242852017-06-23 15:06:31 -0700385 c.AddProperties(c.linker.linkerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800386 }
387 if c.installer != nil {
Colin Cross36242852017-06-23 15:06:31 -0700388 c.AddProperties(c.installer.installerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800389 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700390 if c.stl != nil {
Colin Cross36242852017-06-23 15:06:31 -0700391 c.AddProperties(c.stl.props()...)
Colin Crossa8e07cc2016-04-04 15:07:06 -0700392 }
Colin Cross16b23492016-01-06 14:41:07 -0800393 if c.sanitize != nil {
Colin Cross36242852017-06-23 15:06:31 -0700394 c.AddProperties(c.sanitize.props()...)
Colin Cross16b23492016-01-06 14:41:07 -0800395 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800396 if c.coverage != nil {
Colin Cross36242852017-06-23 15:06:31 -0700397 c.AddProperties(c.coverage.props()...)
Dan Willemsen581341d2017-02-09 16:16:31 -0800398 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800399 if c.sabi != nil {
Colin Cross36242852017-06-23 15:06:31 -0700400 c.AddProperties(c.sabi.props()...)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800401 }
Justin Yun8effde42017-06-23 19:24:43 +0900402 if c.vndkdep != nil {
403 c.AddProperties(c.vndkdep.props()...)
404 }
Stephen Craneba090d12017-05-09 15:44:35 -0700405 if c.lto != nil {
406 c.AddProperties(c.lto.props()...)
407 }
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700408 if c.pgo != nil {
409 c.AddProperties(c.pgo.props()...)
410 }
Colin Crossca860ac2016-01-04 14:34:37 -0800411 for _, feature := range c.features {
Colin Cross36242852017-06-23 15:06:31 -0700412 c.AddProperties(feature.props()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800413 }
Colin Crossc472d572015-03-17 15:06:21 -0700414
Colin Crossa9d8bee2018-10-02 13:59:46 -0700415 c.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
416 switch class {
417 case android.Device:
418 return ctx.Config().DevicePrefer32BitExecutables()
419 case android.HostCross:
420 // Windows builds always prefer 32-bit
421 return true
422 default:
423 return false
424 }
425 })
Colin Cross36242852017-06-23 15:06:31 -0700426 android.InitAndroidArchModule(c, c.hod, c.multilib)
Colin Crossc472d572015-03-17 15:06:21 -0700427
Colin Cross1f44a3a2017-07-07 14:33:33 -0700428 android.InitDefaultableModule(c)
Colin Cross36242852017-06-23 15:06:31 -0700429
Jiyong Park9d452992018-10-03 00:38:19 +0900430 android.InitApexModule(c)
431
Colin Cross36242852017-06-23 15:06:31 -0700432 return c
Colin Crossc472d572015-03-17 15:06:21 -0700433}
434
Colin Crossb916a382016-07-29 17:28:03 -0700435// Returns true for dependency roots (binaries)
436// TODO(ccross): also handle dlopenable libraries
437func (c *Module) isDependencyRoot() bool {
438 if root, ok := c.linker.(interface {
439 isDependencyRoot() bool
440 }); ok {
441 return root.isDependencyRoot()
442 }
443 return false
444}
445
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700446func (c *Module) useVndk() bool {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700447 return c.Properties.UseVndk
448}
449
Justin Yun8effde42017-06-23 19:24:43 +0900450func (c *Module) isVndk() bool {
Logan Chienf3511742017-10-31 18:04:35 +0800451 if vndkdep := c.vndkdep; vndkdep != nil {
452 return vndkdep.isVndk()
Justin Yun8effde42017-06-23 19:24:43 +0900453 }
454 return false
455}
456
Yi Kong7e53c572018-02-14 18:16:12 +0800457func (c *Module) isPgoCompile() bool {
458 if pgo := c.pgo; pgo != nil {
459 return pgo.Properties.PgoCompile
460 }
461 return false
462}
463
Logan Chienf3511742017-10-31 18:04:35 +0800464func (c *Module) isVndkSp() bool {
465 if vndkdep := c.vndkdep; vndkdep != nil {
466 return vndkdep.isVndkSp()
467 }
468 return false
469}
470
471func (c *Module) isVndkExt() bool {
472 if vndkdep := c.vndkdep; vndkdep != nil {
473 return vndkdep.isVndkExt()
474 }
475 return false
476}
477
478func (c *Module) getVndkExtendsModuleName() string {
479 if vndkdep := c.vndkdep; vndkdep != nil {
480 return vndkdep.getVndkExtendsModuleName()
481 }
482 return ""
483}
484
Jiyong Park82e2bf32017-08-16 14:05:54 +0900485// Returns true only when this module is configured to have core and vendor
486// variants.
487func (c *Module) hasVendorVariant() bool {
488 return c.isVndk() || Bool(c.VendorProperties.Vendor_available)
489}
490
Jiyong Parkf9332f12018-02-01 00:54:12 +0900491func (c *Module) inRecovery() bool {
492 return c.Properties.InRecovery || c.ModuleBase.InstallInRecovery()
493}
494
495func (c *Module) onlyInRecovery() bool {
496 return c.ModuleBase.InstallInRecovery()
497}
498
Colin Crossca860ac2016-01-04 14:34:37 -0800499type baseModuleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700500 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800501 moduleContextImpl
502}
503
Colin Cross37047f12016-12-13 17:06:13 -0800504type depsContext struct {
505 android.BottomUpMutatorContext
506 moduleContextImpl
507}
508
Colin Crossca860ac2016-01-04 14:34:37 -0800509type moduleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700510 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800511 moduleContextImpl
512}
513
Jiyong Park2db76922017-11-08 16:03:48 +0900514func (ctx *moduleContext) SocSpecific() bool {
515 return ctx.ModuleContext.SocSpecific() ||
516 (ctx.mod.hasVendorVariant() && ctx.mod.useVndk() && !ctx.mod.isVndk())
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700517}
518
Colin Crossca860ac2016-01-04 14:34:37 -0800519type moduleContextImpl struct {
520 mod *Module
521 ctx BaseModuleContext
522}
523
Colin Crossb98c8b02016-07-29 13:44:28 -0700524func (ctx *moduleContextImpl) toolchain() config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800525 return ctx.mod.toolchain(ctx.ctx)
526}
527
528func (ctx *moduleContextImpl) static() bool {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000529 return ctx.mod.static()
Colin Crossca860ac2016-01-04 14:34:37 -0800530}
531
532func (ctx *moduleContextImpl) staticBinary() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700533 if static, ok := ctx.mod.linker.(interface {
534 staticBinary() bool
535 }); ok {
536 return static.staticBinary()
Colin Crossca860ac2016-01-04 14:34:37 -0800537 }
Colin Crossb916a382016-07-29 17:28:03 -0700538 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800539}
540
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700541func (ctx *moduleContextImpl) useSdk() bool {
Jiyong Parkf9332f12018-02-01 00:54:12 +0900542 if ctx.ctx.Device() && !ctx.useVndk() && !ctx.inRecovery() {
Nan Zhang0007d812017-11-07 10:57:05 -0800543 return String(ctx.mod.Properties.Sdk_version) != ""
Dan Willemsena96ff642016-06-07 12:34:45 -0700544 }
545 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800546}
547
548func (ctx *moduleContextImpl) sdkVersion() string {
Dan Willemsena96ff642016-06-07 12:34:45 -0700549 if ctx.ctx.Device() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700550 if ctx.useVndk() {
Justin Yun732aa6a2018-03-23 17:43:47 +0900551 vndk_ver := ctx.ctx.DeviceConfig().VndkVersion()
Ryan Prichard05206112018-03-26 21:25:27 -0700552 if vndk_ver == "current" {
Justin Yun732aa6a2018-03-23 17:43:47 +0900553 platform_vndk_ver := ctx.ctx.DeviceConfig().PlatformVndkVersion()
554 if inList(platform_vndk_ver, ctx.ctx.Config().PlatformVersionCombinedCodenames()) {
555 return "current"
556 }
557 return platform_vndk_ver
558 }
559 return vndk_ver
Dan Willemsend2ede872016-11-18 14:54:24 -0800560 }
Justin Yun732aa6a2018-03-23 17:43:47 +0900561 return String(ctx.mod.Properties.Sdk_version)
Dan Willemsena96ff642016-06-07 12:34:45 -0700562 }
563 return ""
Colin Crossca860ac2016-01-04 14:34:37 -0800564}
565
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700566func (ctx *moduleContextImpl) useVndk() bool {
567 return ctx.mod.useVndk()
568}
Justin Yun8effde42017-06-23 19:24:43 +0900569
Logan Chienf3511742017-10-31 18:04:35 +0800570func (ctx *moduleContextImpl) isVndk() bool {
571 return ctx.mod.isVndk()
572}
573
Yi Kong7e53c572018-02-14 18:16:12 +0800574func (ctx *moduleContextImpl) isPgoCompile() bool {
575 return ctx.mod.isPgoCompile()
576}
577
Justin Yun8effde42017-06-23 19:24:43 +0900578func (ctx *moduleContextImpl) isVndkSp() bool {
Logan Chienf3511742017-10-31 18:04:35 +0800579 return ctx.mod.isVndkSp()
580}
581
582func (ctx *moduleContextImpl) isVndkExt() bool {
583 return ctx.mod.isVndkExt()
Justin Yun8effde42017-06-23 19:24:43 +0900584}
585
Jiyong Parkf9332f12018-02-01 00:54:12 +0900586func (ctx *moduleContextImpl) inRecovery() bool {
587 return ctx.mod.inRecovery()
588}
589
Logan Chien2f2b8902018-07-10 15:01:19 +0800590// Check whether ABI dumps should be created for this module.
591func (ctx *moduleContextImpl) shouldCreateVndkSourceAbiDump() bool {
592 if ctx.ctx.Config().IsEnvTrue("SKIP_ABI_CHECKS") {
593 return false
Jayant Chowdharyea0a2e12018-03-01 19:12:16 -0800594 }
Logan Chien2f2b8902018-07-10 15:01:19 +0800595 if sanitize := ctx.mod.sanitize; sanitize != nil {
596 if !sanitize.isVariantOnProductionDevice() {
597 return false
598 }
599 }
600 if !ctx.ctx.Device() {
601 // Host modules do not need ABI dumps.
602 return false
603 }
604 if inList(ctx.baseModuleName(), llndkLibraries) {
605 return true
606 }
Logan Chienf4b79c62018-08-02 02:27:02 +0800607 if inList(ctx.baseModuleName(), ndkMigratedLibs) {
608 return true
609 }
Logan Chien2f2b8902018-07-10 15:01:19 +0800610 if ctx.useVndk() && ctx.isVndk() {
611 // Return true if this is VNDK-core, VNDK-SP, or VNDK-Ext and this is not
612 // VNDK-private.
613 return Bool(ctx.mod.VendorProperties.Vendor_available) || ctx.isVndkExt()
614 }
615 return false
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800616}
617
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700618func (ctx *moduleContextImpl) selectedStl() string {
619 if stl := ctx.mod.stl; stl != nil {
620 return stl.Properties.SelectedStl
621 }
622 return ""
623}
624
Colin Crossce75d2c2016-10-06 16:12:58 -0700625func (ctx *moduleContextImpl) baseModuleName() string {
626 return ctx.mod.ModuleBase.BaseModuleName()
627}
628
Logan Chienf3511742017-10-31 18:04:35 +0800629func (ctx *moduleContextImpl) getVndkExtendsModuleName() string {
630 return ctx.mod.getVndkExtendsModuleName()
631}
632
Colin Cross635c3b02016-05-18 15:37:25 -0700633func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800634 return &Module{
635 hod: hod,
636 multilib: multilib,
637 }
638}
639
Colin Cross635c3b02016-05-18 15:37:25 -0700640func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800641 module := newBaseModule(hod, multilib)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700642 module.features = []feature{
643 &tidyFeature{},
644 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700645 module.stl = &stl{}
Colin Cross16b23492016-01-06 14:41:07 -0800646 module.sanitize = &sanitize{}
Dan Willemsen581341d2017-02-09 16:16:31 -0800647 module.coverage = &coverage{}
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800648 module.sabi = &sabi{}
Justin Yun8effde42017-06-23 19:24:43 +0900649 module.vndkdep = &vndkdep{}
Stephen Craneba090d12017-05-09 15:44:35 -0700650 module.lto = &lto{}
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700651 module.pgo = &pgo{}
Colin Crossca860ac2016-01-04 14:34:37 -0800652 return module
653}
654
Colin Crossce75d2c2016-10-06 16:12:58 -0700655func (c *Module) Prebuilt() *android.Prebuilt {
656 if p, ok := c.linker.(prebuiltLinkerInterface); ok {
657 return p.prebuilt()
658 }
659 return nil
660}
661
662func (c *Module) Name() string {
663 name := c.ModuleBase.Name()
Dan Willemsen01a90592017-04-07 15:21:13 -0700664 if p, ok := c.linker.(interface {
665 Name(string) string
666 }); ok {
Colin Crossce75d2c2016-10-06 16:12:58 -0700667 name = p.Name(name)
668 }
669 return name
670}
671
Jeff Gaston294356f2017-09-27 17:05:30 -0700672// orderDeps reorders dependencies into a list such that if module A depends on B, then
673// A will precede B in the resultant list.
674// This is convenient for passing into a linker.
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -0800675// Note that directSharedDeps should be the analogous static library for each shared lib dep
676func orderDeps(directStaticDeps []android.Path, directSharedDeps []android.Path, allTransitiveDeps map[android.Path][]android.Path) (orderedAllDeps []android.Path, orderedDeclaredDeps []android.Path) {
Jeff Gaston294356f2017-09-27 17:05:30 -0700677 // If A depends on B, then
678 // Every list containing A will also contain B later in the list
679 // So, after concatenating all lists, the final instance of B will have come from the same
680 // original list as the final instance of A
681 // So, the final instance of B will be later in the concatenation than the final A
682 // So, keeping only the final instance of A and of B ensures that A is earlier in the output
683 // list than B
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -0800684 for _, dep := range directStaticDeps {
Jeff Gaston294356f2017-09-27 17:05:30 -0700685 orderedAllDeps = append(orderedAllDeps, dep)
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -0800686 orderedAllDeps = append(orderedAllDeps, allTransitiveDeps[dep]...)
687 }
688 for _, dep := range directSharedDeps {
689 orderedAllDeps = append(orderedAllDeps, dep)
690 orderedAllDeps = append(orderedAllDeps, allTransitiveDeps[dep]...)
Jeff Gaston294356f2017-09-27 17:05:30 -0700691 }
692
Colin Crossb6715442017-10-24 11:13:31 -0700693 orderedAllDeps = android.LastUniquePaths(orderedAllDeps)
Jeff Gaston294356f2017-09-27 17:05:30 -0700694
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -0800695 // We don't want to add any new dependencies into directStaticDeps (to allow the caller to
Jeff Gaston294356f2017-09-27 17:05:30 -0700696 // intentionally exclude or replace any unwanted transitive dependencies), so we limit the
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -0800697 // resultant list to only what the caller has chosen to include in directStaticDeps
698 _, orderedDeclaredDeps = android.FilterPathList(orderedAllDeps, directStaticDeps)
Jeff Gaston294356f2017-09-27 17:05:30 -0700699
700 return orderedAllDeps, orderedDeclaredDeps
701}
702
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -0800703func orderStaticModuleDeps(module *Module, staticDeps []*Module, sharedDeps []*Module) (results []android.Path) {
704 // convert Module to Path
705 allTransitiveDeps := make(map[android.Path][]android.Path, len(staticDeps))
706 staticDepFiles := []android.Path{}
707 for _, dep := range staticDeps {
708 allTransitiveDeps[dep.outputFile.Path()] = dep.depsInLinkOrder
709 staticDepFiles = append(staticDepFiles, dep.outputFile.Path())
Jeff Gaston294356f2017-09-27 17:05:30 -0700710 }
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -0800711 sharedDepFiles := []android.Path{}
712 for _, sharedDep := range sharedDeps {
713 staticAnalogue := sharedDep.staticVariant
714 if staticAnalogue != nil {
715 allTransitiveDeps[staticAnalogue.outputFile.Path()] = staticAnalogue.depsInLinkOrder
716 sharedDepFiles = append(sharedDepFiles, staticAnalogue.outputFile.Path())
717 }
Jeff Gaston294356f2017-09-27 17:05:30 -0700718 }
719
720 // reorder the dependencies based on transitive dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -0800721 module.depsInLinkOrder, results = orderDeps(staticDepFiles, sharedDepFiles, allTransitiveDeps)
Jeff Gaston294356f2017-09-27 17:05:30 -0700722
723 return results
Jeff Gaston294356f2017-09-27 17:05:30 -0700724}
725
Colin Cross635c3b02016-05-18 15:37:25 -0700726func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800727 ctx := &moduleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700728 ModuleContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800729 moduleContextImpl: moduleContextImpl{
730 mod: c,
731 },
732 }
733 ctx.ctx = ctx
734
Colin Crossf18e1102017-11-16 14:33:08 -0800735 deps := c.depsToPaths(ctx)
736 if ctx.Failed() {
737 return
738 }
739
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700740 if c.Properties.Clang != nil && *c.Properties.Clang == false {
741 ctx.PropertyErrorf("clang", "false (GCC) is no longer supported")
742 }
743
Colin Crossca860ac2016-01-04 14:34:37 -0800744 flags := Flags{
745 Toolchain: c.toolchain(ctx),
Colin Crossca860ac2016-01-04 14:34:37 -0800746 }
Colin Crossca860ac2016-01-04 14:34:37 -0800747 if c.compiler != nil {
Colin Crossf18e1102017-11-16 14:33:08 -0800748 flags = c.compiler.compilerFlags(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -0800749 }
750 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700751 flags = c.linker.linkerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800752 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700753 if c.stl != nil {
754 flags = c.stl.flags(ctx, flags)
755 }
Colin Cross16b23492016-01-06 14:41:07 -0800756 if c.sanitize != nil {
757 flags = c.sanitize.flags(ctx, flags)
758 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800759 if c.coverage != nil {
760 flags = c.coverage.flags(ctx, flags)
761 }
Stephen Craneba090d12017-05-09 15:44:35 -0700762 if c.lto != nil {
763 flags = c.lto.flags(ctx, flags)
764 }
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700765 if c.pgo != nil {
766 flags = c.pgo.flags(ctx, flags)
767 }
Colin Crossca860ac2016-01-04 14:34:37 -0800768 for _, feature := range c.features {
769 flags = feature.flags(ctx, flags)
770 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800771 if ctx.Failed() {
772 return
773 }
774
Colin Crossb98c8b02016-07-29 13:44:28 -0700775 flags.CFlags, _ = filterList(flags.CFlags, config.IllegalFlags)
776 flags.CppFlags, _ = filterList(flags.CppFlags, config.IllegalFlags)
777 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, config.IllegalFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800778
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800779 flags.GlobalFlags = append(flags.GlobalFlags, deps.Flags...)
780 c.flags = flags
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700781 // We need access to all the flags seen by a source file.
782 if c.sabi != nil {
783 flags = c.sabi.flags(ctx, flags)
784 }
Colin Crossca860ac2016-01-04 14:34:37 -0800785 // Optimization to reduce size of build.ninja
786 // Replace the long list of flags for each file with a module-local variable
787 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
788 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
789 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
790 flags.CFlags = []string{"$cflags"}
791 flags.CppFlags = []string{"$cppflags"}
792 flags.AsFlags = []string{"$asflags"}
793
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700794 var objs Objects
Colin Crossca860ac2016-01-04 14:34:37 -0800795 if c.compiler != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700796 objs = c.compiler.compile(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -0800797 if ctx.Failed() {
798 return
799 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800800 }
801
Colin Crossca860ac2016-01-04 14:34:37 -0800802 if c.linker != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700803 outputFile := c.linker.link(ctx, flags, deps, objs)
Colin Crossca860ac2016-01-04 14:34:37 -0800804 if ctx.Failed() {
805 return
806 }
Colin Cross635c3b02016-05-18 15:37:25 -0700807 c.outputFile = android.OptionalPathForPath(outputFile)
Colin Crossce75d2c2016-10-06 16:12:58 -0700808 }
Colin Cross5049f022015-03-18 13:28:46 -0700809
Jiyong Park9d452992018-10-03 00:38:19 +0900810 if c.installer != nil && !c.Properties.PreventInstall && c.IsForPlatform() && c.outputFile.Valid() {
Colin Crossce75d2c2016-10-06 16:12:58 -0700811 c.installer.install(ctx, c.outputFile.Path())
812 if ctx.Failed() {
813 return
Colin Crossca860ac2016-01-04 14:34:37 -0800814 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700815 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800816}
817
Colin Crossb98c8b02016-07-29 13:44:28 -0700818func (c *Module) toolchain(ctx BaseModuleContext) config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800819 if c.cachedToolchain == nil {
Colin Crossb98c8b02016-07-29 13:44:28 -0700820 c.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
Colin Cross3f40fa42015-01-30 17:27:36 -0800821 }
Colin Crossca860ac2016-01-04 14:34:37 -0800822 return c.cachedToolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800823}
824
Colin Crossca860ac2016-01-04 14:34:37 -0800825func (c *Module) begin(ctx BaseModuleContext) {
826 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700827 c.compiler.compilerInit(ctx)
Colin Cross21b9a242015-03-24 14:15:58 -0700828 }
Colin Crossca860ac2016-01-04 14:34:37 -0800829 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700830 c.linker.linkerInit(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800831 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700832 if c.stl != nil {
833 c.stl.begin(ctx)
834 }
Colin Cross16b23492016-01-06 14:41:07 -0800835 if c.sanitize != nil {
836 c.sanitize.begin(ctx)
837 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800838 if c.coverage != nil {
839 c.coverage.begin(ctx)
840 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800841 if c.sabi != nil {
842 c.sabi.begin(ctx)
843 }
Justin Yun8effde42017-06-23 19:24:43 +0900844 if c.vndkdep != nil {
845 c.vndkdep.begin(ctx)
846 }
Stephen Craneba090d12017-05-09 15:44:35 -0700847 if c.lto != nil {
848 c.lto.begin(ctx)
849 }
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700850 if c.pgo != nil {
851 c.pgo.begin(ctx)
852 }
Colin Crossca860ac2016-01-04 14:34:37 -0800853 for _, feature := range c.features {
854 feature.begin(ctx)
855 }
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700856 if ctx.useSdk() {
Dan Albertf5415d72017-08-17 16:19:59 -0700857 version, err := normalizeNdkApiLevel(ctx, ctx.sdkVersion(), ctx.Arch())
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700858 if err != nil {
859 ctx.PropertyErrorf("sdk_version", err.Error())
860 }
Nan Zhang0007d812017-11-07 10:57:05 -0800861 c.Properties.Sdk_version = StringPtr(version)
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700862 }
Colin Crossca860ac2016-01-04 14:34:37 -0800863}
864
Colin Cross37047f12016-12-13 17:06:13 -0800865func (c *Module) deps(ctx DepsContext) Deps {
Colin Crossc99deeb2016-04-11 15:06:20 -0700866 deps := Deps{}
867
868 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700869 deps = c.compiler.compilerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700870 }
Pirama Arumuga Nainar0b882f02018-04-23 22:44:39 +0000871 // Add the PGO dependency (the clang_rt.profile runtime library), which
872 // sometimes depends on symbols from libgcc, before libgcc gets added
873 // in linkerDeps().
Pirama Arumuga Nainar49b53d52017-10-04 16:47:29 -0700874 if c.pgo != nil {
875 deps = c.pgo.deps(ctx, deps)
876 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700877 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700878 deps = c.linker.linkerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700879 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700880 if c.stl != nil {
881 deps = c.stl.deps(ctx, deps)
882 }
Colin Cross16b23492016-01-06 14:41:07 -0800883 if c.sanitize != nil {
884 deps = c.sanitize.deps(ctx, deps)
885 }
Pirama Arumuga Nainar0b882f02018-04-23 22:44:39 +0000886 if c.coverage != nil {
887 deps = c.coverage.deps(ctx, deps)
888 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800889 if c.sabi != nil {
890 deps = c.sabi.deps(ctx, deps)
891 }
Justin Yun8effde42017-06-23 19:24:43 +0900892 if c.vndkdep != nil {
893 deps = c.vndkdep.deps(ctx, deps)
894 }
Stephen Craneba090d12017-05-09 15:44:35 -0700895 if c.lto != nil {
896 deps = c.lto.deps(ctx, deps)
897 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700898 for _, feature := range c.features {
899 deps = feature.deps(ctx, deps)
900 }
901
Colin Crossb6715442017-10-24 11:13:31 -0700902 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
903 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
904 deps.LateStaticLibs = android.LastUniqueStrings(deps.LateStaticLibs)
905 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
906 deps.LateSharedLibs = android.LastUniqueStrings(deps.LateSharedLibs)
907 deps.HeaderLibs = android.LastUniqueStrings(deps.HeaderLibs)
Logan Chien43d34c32017-12-20 01:17:32 +0800908 deps.RuntimeLibs = android.LastUniqueStrings(deps.RuntimeLibs)
Colin Crossc99deeb2016-04-11 15:06:20 -0700909
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700910 for _, lib := range deps.ReexportSharedLibHeaders {
911 if !inList(lib, deps.SharedLibs) {
912 ctx.PropertyErrorf("export_shared_lib_headers", "Shared library not in shared_libs: '%s'", lib)
913 }
914 }
915
916 for _, lib := range deps.ReexportStaticLibHeaders {
917 if !inList(lib, deps.StaticLibs) {
918 ctx.PropertyErrorf("export_static_lib_headers", "Static library not in static_libs: '%s'", lib)
919 }
920 }
921
Colin Cross5950f382016-12-13 12:50:57 -0800922 for _, lib := range deps.ReexportHeaderLibHeaders {
923 if !inList(lib, deps.HeaderLibs) {
924 ctx.PropertyErrorf("export_header_lib_headers", "Header library not in header_libs: '%s'", lib)
925 }
926 }
927
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700928 for _, gen := range deps.ReexportGeneratedHeaders {
929 if !inList(gen, deps.GeneratedHeaders) {
930 ctx.PropertyErrorf("export_generated_headers", "Generated header module not in generated_headers: '%s'", gen)
931 }
932 }
933
Colin Crossc99deeb2016-04-11 15:06:20 -0700934 return deps
935}
936
Dan Albert7e9d2952016-08-04 13:02:36 -0700937func (c *Module) beginMutator(actx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800938 ctx := &baseModuleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700939 BaseContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800940 moduleContextImpl: moduleContextImpl{
941 mod: c,
942 },
943 }
944 ctx.ctx = ctx
945
Colin Crossca860ac2016-01-04 14:34:37 -0800946 c.begin(ctx)
Dan Albert7e9d2952016-08-04 13:02:36 -0700947}
948
Jiyong Park7ed9de32018-10-15 22:25:07 +0900949// Split name#version into name and version
950func stubsLibNameAndVersion(name string) (string, string) {
951 if sharp := strings.LastIndex(name, "#"); sharp != -1 && sharp != len(name)-1 {
952 version := name[sharp+1:]
953 libname := name[:sharp]
954 return libname, version
955 }
956 return name, ""
957}
958
Colin Cross1e676be2016-10-12 14:38:15 -0700959func (c *Module) DepsMutator(actx android.BottomUpMutatorContext) {
Colin Cross37047f12016-12-13 17:06:13 -0800960 ctx := &depsContext{
961 BottomUpMutatorContext: actx,
Dan Albert7e9d2952016-08-04 13:02:36 -0700962 moduleContextImpl: moduleContextImpl{
963 mod: c,
964 },
965 }
966 ctx.ctx = ctx
Colin Crossca860ac2016-01-04 14:34:37 -0800967
Colin Crossc99deeb2016-04-11 15:06:20 -0700968 deps := c.deps(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800969
Dan Albert914449f2016-06-17 16:45:24 -0700970 variantNdkLibs := []string{}
971 variantLateNdkLibs := []string{}
Dan Willemsenb916b802017-03-19 13:44:32 -0700972 if ctx.Os() == android.Android {
Dan Albert914449f2016-06-17 16:45:24 -0700973 version := ctx.sdkVersion()
Dan Willemsen72d39932016-07-08 23:23:48 -0700974
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700975 // rewriteNdkLibs takes a list of names of shared libraries and scans it for three types
976 // of names:
Dan Albert914449f2016-06-17 16:45:24 -0700977 //
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700978 // 1. Name of an NDK library that refers to a prebuilt module.
979 // For each of these, it adds the name of the prebuilt module (which will be in
980 // prebuilts/ndk) to the list of nonvariant libs.
981 // 2. Name of an NDK library that refers to an ndk_library module.
982 // For each of these, it adds the name of the ndk_library module to the list of
983 // variant libs.
984 // 3. Anything else (so anything that isn't an NDK library).
985 // It adds these to the nonvariantLibs list.
Dan Albert914449f2016-06-17 16:45:24 -0700986 //
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700987 // The caller can then know to add the variantLibs dependencies differently from the
988 // nonvariantLibs
989 rewriteNdkLibs := func(list []string) (nonvariantLibs []string, variantLibs []string) {
990 variantLibs = []string{}
991 nonvariantLibs = []string{}
Dan Albert914449f2016-06-17 16:45:24 -0700992 for _, entry := range list {
Jiyong Park7ed9de32018-10-15 22:25:07 +0900993 // strip #version suffix out
994 name, _ := stubsLibNameAndVersion(entry)
995 if ctx.useSdk() && inList(name, ndkPrebuiltSharedLibraries) {
996 if !inList(name, ndkMigratedLibs) {
997 nonvariantLibs = append(nonvariantLibs, name+".ndk."+version)
Dan Albert914449f2016-06-17 16:45:24 -0700998 } else {
Jiyong Park7ed9de32018-10-15 22:25:07 +0900999 variantLibs = append(variantLibs, name+ndkLibrarySuffix)
Dan Albert914449f2016-06-17 16:45:24 -07001000 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09001001 } else if ctx.useVndk() && inList(name, llndkLibraries) {
1002 nonvariantLibs = append(nonvariantLibs, name+llndkLibrarySuffix)
1003 } else if (ctx.Platform() || ctx.ProductSpecific()) && inList(name, vendorPublicLibraries) {
1004 vendorPublicLib := name + vendorPublicLibrarySuffix
Jiyong Park374510b2018-03-19 18:23:01 +09001005 if actx.OtherModuleExists(vendorPublicLib) {
1006 nonvariantLibs = append(nonvariantLibs, vendorPublicLib)
1007 } else {
1008 // This can happen if vendor_public_library module is defined in a
1009 // namespace that isn't visible to the current module. In that case,
1010 // link to the original library.
Jiyong Park7ed9de32018-10-15 22:25:07 +09001011 nonvariantLibs = append(nonvariantLibs, name)
Jiyong Park374510b2018-03-19 18:23:01 +09001012 }
Dan Albert914449f2016-06-17 16:45:24 -07001013 } else {
Jiyong Park7ed9de32018-10-15 22:25:07 +09001014 // put name#version back
Dan Willemsen7cbf5f82017-03-28 00:08:30 -07001015 nonvariantLibs = append(nonvariantLibs, entry)
Dan Willemsen72d39932016-07-08 23:23:48 -07001016 }
1017 }
Dan Albert914449f2016-06-17 16:45:24 -07001018 return nonvariantLibs, variantLibs
Dan Willemsen72d39932016-07-08 23:23:48 -07001019 }
1020
Dan Albert914449f2016-06-17 16:45:24 -07001021 deps.SharedLibs, variantNdkLibs = rewriteNdkLibs(deps.SharedLibs)
1022 deps.LateSharedLibs, variantLateNdkLibs = rewriteNdkLibs(deps.LateSharedLibs)
Jiyong Park4c35af02017-07-05 13:41:55 +09001023 deps.ReexportSharedLibHeaders, _ = rewriteNdkLibs(deps.ReexportSharedLibHeaders)
Dan Willemsen72d39932016-07-08 23:23:48 -07001024 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001025
Jiyong Park7ed9de32018-10-15 22:25:07 +09001026 if c.linker != nil {
1027 if library, ok := c.linker.(*libraryDecorator); ok {
1028 if library.buildStubs() {
1029 // Stubs lib does not have dependency to other libraries. Don't proceed.
1030 return
1031 }
1032 }
1033 }
1034
Colin Cross32ec36c2016-12-15 07:39:51 -08001035 for _, lib := range deps.HeaderLibs {
1036 depTag := headerDepTag
1037 if inList(lib, deps.ReexportHeaderLibHeaders) {
1038 depTag = headerExportDepTag
1039 }
1040 actx.AddVariationDependencies(nil, depTag, lib)
1041 }
Colin Cross5950f382016-12-13 12:50:57 -08001042
Dan Willemsen59339a22018-07-22 21:18:45 -07001043 actx.AddVariationDependencies([]blueprint.Variation{
1044 {Mutator: "link", Variation: "static"},
1045 }, wholeStaticDepTag, deps.WholeStaticLibs...)
Colin Crossc99deeb2016-04-11 15:06:20 -07001046
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001047 for _, lib := range deps.StaticLibs {
1048 depTag := staticDepTag
1049 if inList(lib, deps.ReexportStaticLibHeaders) {
1050 depTag = staticExportDepTag
1051 }
Dan Willemsen59339a22018-07-22 21:18:45 -07001052 actx.AddVariationDependencies([]blueprint.Variation{
1053 {Mutator: "link", Variation: "static"},
1054 }, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001055 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001056
Dan Willemsen59339a22018-07-22 21:18:45 -07001057 actx.AddVariationDependencies([]blueprint.Variation{
1058 {Mutator: "link", Variation: "static"},
1059 }, lateStaticDepTag, deps.LateStaticLibs...)
Colin Crossc99deeb2016-04-11 15:06:20 -07001060
Jiyong Park7ed9de32018-10-15 22:25:07 +09001061 // shared lib names without the #version suffix
1062 var sharedLibNames []string
1063
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001064 for _, lib := range deps.SharedLibs {
Jiyong Park7ed9de32018-10-15 22:25:07 +09001065 name, version := stubsLibNameAndVersion(lib)
1066 sharedLibNames = append(sharedLibNames, name)
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001067 depTag := sharedDepTag
1068 if inList(lib, deps.ReexportSharedLibHeaders) {
1069 depTag = sharedExportDepTag
1070 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09001071 var variations []blueprint.Variation
1072 variations = append(variations, blueprint.Variation{Mutator: "link", Variation: "shared"})
1073 if version != "" && ctx.Os() == android.Android && !ctx.useVndk() && !c.inRecovery() {
1074 variations = append(variations, blueprint.Variation{Mutator: "version", Variation: version})
1075 }
1076 actx.AddVariationDependencies(variations, depTag, name)
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001077 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001078
Jiyong Park7ed9de32018-10-15 22:25:07 +09001079 for _, lib := range deps.LateSharedLibs {
1080 name, version := stubsLibNameAndVersion(lib)
1081 if inList(name, sharedLibNames) {
1082 // This is to handle the case that some of the late shared libs (libc, libdl, libm, ...)
1083 // are added also to SharedLibs with version (e.g., libc#10). If not skipped, we will be
1084 // linking against both the stubs lib and the non-stubs lib at the same time.
1085 continue
1086 }
1087 depTag := lateSharedDepTag
1088 var variations []blueprint.Variation
1089 variations = append(variations, blueprint.Variation{Mutator: "link", Variation: "shared"})
1090 if version != "" && ctx.Os() == android.Android && !ctx.useVndk() && !c.inRecovery() {
1091 variations = append(variations, blueprint.Variation{Mutator: "version", Variation: version})
1092 }
1093 actx.AddVariationDependencies(variations, depTag, name)
1094 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001095
Dan Willemsen59339a22018-07-22 21:18:45 -07001096 actx.AddVariationDependencies([]blueprint.Variation{
1097 {Mutator: "link", Variation: "shared"},
1098 }, runtimeDepTag, deps.RuntimeLibs...)
Logan Chien43d34c32017-12-20 01:17:32 +08001099
Colin Cross68861832016-07-08 10:41:41 -07001100 actx.AddDependency(c, genSourceDepTag, deps.GeneratedSources...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -07001101
1102 for _, gen := range deps.GeneratedHeaders {
1103 depTag := genHeaderDepTag
1104 if inList(gen, deps.ReexportGeneratedHeaders) {
1105 depTag = genHeaderExportDepTag
1106 }
1107 actx.AddDependency(c, depTag, gen)
1108 }
Dan Willemsenb40aab62016-04-20 14:21:14 -07001109
Colin Cross42d48b72018-08-29 14:10:52 -07001110 actx.AddVariationDependencies(nil, objDepTag, deps.ObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -07001111
1112 if deps.CrtBegin != "" {
Colin Cross42d48b72018-08-29 14:10:52 -07001113 actx.AddVariationDependencies(nil, crtBeginDepTag, deps.CrtBegin)
Colin Crossca860ac2016-01-04 14:34:37 -08001114 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001115 if deps.CrtEnd != "" {
Colin Cross42d48b72018-08-29 14:10:52 -07001116 actx.AddVariationDependencies(nil, crtEndDepTag, deps.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -07001117 }
Dan Willemsena0790e32018-10-12 00:24:23 -07001118 if deps.LinkerFlagsFile != "" {
1119 actx.AddDependency(c, linkerFlagsDepTag, deps.LinkerFlagsFile)
1120 }
1121 if deps.DynamicLinker != "" {
1122 actx.AddDependency(c, dynamicLinkerDepTag, deps.DynamicLinker)
Dan Willemsenc77a0b32017-09-18 23:19:12 -07001123 }
Dan Albert914449f2016-06-17 16:45:24 -07001124
1125 version := ctx.sdkVersion()
1126 actx.AddVariationDependencies([]blueprint.Variation{
Dan Willemsen59339a22018-07-22 21:18:45 -07001127 {Mutator: "ndk_api", Variation: version},
1128 {Mutator: "link", Variation: "shared"},
1129 }, ndkStubDepTag, variantNdkLibs...)
Dan Albert914449f2016-06-17 16:45:24 -07001130 actx.AddVariationDependencies([]blueprint.Variation{
Dan Willemsen59339a22018-07-22 21:18:45 -07001131 {Mutator: "ndk_api", Variation: version},
1132 {Mutator: "link", Variation: "shared"},
1133 }, ndkLateStubDepTag, variantLateNdkLibs...)
Logan Chienf3511742017-10-31 18:04:35 +08001134
1135 if vndkdep := c.vndkdep; vndkdep != nil {
1136 if vndkdep.isVndkExt() {
1137 baseModuleMode := vendorMode
1138 if actx.DeviceConfig().VndkVersion() == "" {
1139 baseModuleMode = coreMode
1140 }
1141 actx.AddVariationDependencies([]blueprint.Variation{
Dan Willemsen59339a22018-07-22 21:18:45 -07001142 {Mutator: "image", Variation: baseModuleMode},
1143 {Mutator: "link", Variation: "shared"},
1144 }, vndkExtDepTag, vndkdep.getVndkExtendsModuleName())
Logan Chienf3511742017-10-31 18:04:35 +08001145 }
1146 }
Colin Cross6362e272015-10-29 15:25:03 -07001147}
Colin Cross21b9a242015-03-24 14:15:58 -07001148
Colin Crosse40b4ea2018-10-02 22:25:58 -07001149func BeginMutator(ctx android.BottomUpMutatorContext) {
Dan Albert7e9d2952016-08-04 13:02:36 -07001150 if c, ok := ctx.Module().(*Module); ok && c.Enabled() {
1151 c.beginMutator(ctx)
1152 }
1153}
1154
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001155// Whether a module can link to another module, taking into
1156// account NDK linking.
Logan Chienf3511742017-10-31 18:04:35 +08001157func checkLinkType(ctx android.ModuleContext, from *Module, to *Module, tag dependencyTag) {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001158 if from.Target().Os != android.Android {
1159 // Host code is not restricted
1160 return
1161 }
1162 if from.Properties.UseVndk {
1163 // Though vendor code is limited by the vendor mutator,
1164 // each vendor-available module needs to check
1165 // link-type for VNDK.
1166 if from.vndkdep != nil {
Logan Chienf3511742017-10-31 18:04:35 +08001167 from.vndkdep.vndkCheckLinkType(ctx, to, tag)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001168 }
1169 return
1170 }
Nan Zhang0007d812017-11-07 10:57:05 -08001171 if String(from.Properties.Sdk_version) == "" {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001172 // Platform code can link to anything
1173 return
1174 }
Jiyong Parkf9332f12018-02-01 00:54:12 +09001175 if from.inRecovery() {
1176 // Recovery code is not NDK
1177 return
1178 }
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001179 if _, ok := to.linker.(*toolchainLibraryDecorator); ok {
1180 // These are always allowed
1181 return
1182 }
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001183 if _, ok := to.linker.(*ndkPrebuiltStlLinker); ok {
1184 // These are allowed, but they don't set sdk_version
1185 return
1186 }
1187 if _, ok := to.linker.(*stubDecorator); ok {
1188 // These aren't real libraries, but are the stub shared libraries that are included in
1189 // the NDK.
1190 return
1191 }
Nan Zhang0007d812017-11-07 10:57:05 -08001192 if String(to.Properties.Sdk_version) == "" {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001193 // NDK code linking to platform code is never okay.
1194 ctx.ModuleErrorf("depends on non-NDK-built library %q",
1195 ctx.OtherModuleName(to))
1196 }
1197
1198 // At this point we know we have two NDK libraries, but we need to
1199 // check that we're not linking against anything built against a higher
1200 // API level, as it is only valid to link against older or equivalent
1201 // APIs.
1202
Inseob Kim01a28722018-04-11 09:48:45 +09001203 // Current can link against anything.
1204 if String(from.Properties.Sdk_version) != "current" {
1205 // Otherwise we need to check.
1206 if String(to.Properties.Sdk_version) == "current" {
1207 // Current can't be linked against by anything else.
1208 ctx.ModuleErrorf("links %q built against newer API version %q",
1209 ctx.OtherModuleName(to), "current")
1210 } else {
1211 fromApi, err := strconv.Atoi(String(from.Properties.Sdk_version))
1212 if err != nil {
1213 ctx.PropertyErrorf("sdk_version",
Inseob Kim34b22832018-04-11 10:13:16 +09001214 "Invalid sdk_version value (must be int or current): %q",
Inseob Kim01a28722018-04-11 09:48:45 +09001215 String(from.Properties.Sdk_version))
1216 }
1217 toApi, err := strconv.Atoi(String(to.Properties.Sdk_version))
1218 if err != nil {
1219 ctx.PropertyErrorf("sdk_version",
Inseob Kim34b22832018-04-11 10:13:16 +09001220 "Invalid sdk_version value (must be int or current): %q",
Inseob Kim01a28722018-04-11 09:48:45 +09001221 String(to.Properties.Sdk_version))
1222 }
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001223
Inseob Kim01a28722018-04-11 09:48:45 +09001224 if toApi > fromApi {
1225 ctx.ModuleErrorf("links %q built against newer API version %q",
1226 ctx.OtherModuleName(to), String(to.Properties.Sdk_version))
1227 }
1228 }
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001229 }
Dan Albert202fe492017-12-15 13:56:59 -08001230
1231 // Also check that the two STL choices are compatible.
1232 fromStl := from.stl.Properties.SelectedStl
1233 toStl := to.stl.Properties.SelectedStl
1234 if fromStl == "" || toStl == "" {
1235 // Libraries that don't use the STL are unrestricted.
Inseob Kimda2171a2018-04-11 15:41:38 +09001236 } else if fromStl == "ndk_system" || toStl == "ndk_system" {
Dan Albert202fe492017-12-15 13:56:59 -08001237 // We can be permissive with the system "STL" since it is only the C++
1238 // ABI layer, but in the future we should make sure that everyone is
1239 // using either libc++ or nothing.
Colin Crossb60190a2018-09-04 16:28:17 -07001240 } else if getNdkStlFamily(from) != getNdkStlFamily(to) {
Dan Albert202fe492017-12-15 13:56:59 -08001241 ctx.ModuleErrorf("uses %q and depends on %q which uses incompatible %q",
1242 from.stl.Properties.SelectedStl, ctx.OtherModuleName(to),
1243 to.stl.Properties.SelectedStl)
1244 }
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001245}
1246
Jiyong Park5fb8c102018-04-09 12:03:06 +09001247// Tests whether the dependent library is okay to be double loaded inside a single process.
1248// If a library is a member of VNDK and at the same time dependencies of an LLNDK library,
1249// it is subject to be double loaded. Such lib should be explicitly marked as double_loaded: true
1250// or as vndk-sp (vndk: { enabled: true, support_system_process: true}).
1251func checkDoubleLoadableLibries(ctx android.ModuleContext, from *Module, to *Module) {
1252 if _, ok := from.linker.(*libraryDecorator); !ok {
1253 return
1254 }
1255
1256 if inList(ctx.ModuleName(), llndkLibraries) ||
1257 (from.useVndk() && Bool(from.VendorProperties.Double_loadable)) {
1258 _, depIsLlndk := to.linker.(*llndkStubDecorator)
1259 depIsVndkSp := false
1260 if to.vndkdep != nil && to.vndkdep.isVndkSp() {
1261 depIsVndkSp = true
1262 }
1263 depIsVndk := false
1264 if to.vndkdep != nil && to.vndkdep.isVndk() {
1265 depIsVndk = true
1266 }
1267 depIsDoubleLoadable := Bool(to.VendorProperties.Double_loadable)
1268 if !depIsLlndk && !depIsVndkSp && !depIsDoubleLoadable && depIsVndk {
1269 ctx.ModuleErrorf("links VNDK library %q that isn't double_loadable.",
1270 ctx.OtherModuleName(to))
1271 }
1272 }
1273}
1274
Colin Crossc99deeb2016-04-11 15:06:20 -07001275// Convert dependencies to paths. Returns a PathDeps containing paths
Colin Cross635c3b02016-05-18 15:37:25 -07001276func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
Colin Crossca860ac2016-01-04 14:34:37 -08001277 var depPaths PathDeps
Colin Crossca860ac2016-01-04 14:34:37 -08001278
Jeff Gaston294356f2017-09-27 17:05:30 -07001279 directStaticDeps := []*Module{}
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001280 directSharedDeps := []*Module{}
Jeff Gaston294356f2017-09-27 17:05:30 -07001281
Colin Crossd11fcda2017-10-23 17:59:01 -07001282 ctx.VisitDirectDeps(func(dep android.Module) {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001283 depName := ctx.OtherModuleName(dep)
1284 depTag := ctx.OtherModuleDependencyTag(dep)
Dan Albert9e10cd42016-08-03 14:12:14 -07001285
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001286 ccDep, _ := dep.(*Module)
1287 if ccDep == nil {
1288 // handling for a few module types that aren't cc Module but that are also supported
1289 switch depTag {
Dan Willemsenb40aab62016-04-20 14:21:14 -07001290 case genSourceDepTag:
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001291 if genRule, ok := dep.(genrule.SourceFileGenerator); ok {
Dan Willemsenb40aab62016-04-20 14:21:14 -07001292 depPaths.GeneratedSources = append(depPaths.GeneratedSources,
1293 genRule.GeneratedSourceFiles()...)
1294 } else {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001295 ctx.ModuleErrorf("module %q is not a gensrcs or genrule", depName)
Dan Willemsenb40aab62016-04-20 14:21:14 -07001296 }
Colin Crosse90bfd12017-04-26 16:59:26 -07001297 // Support exported headers from a generated_sources dependency
1298 fallthrough
Dan Willemsenb3454ab2016-09-28 17:34:58 -07001299 case genHeaderDepTag, genHeaderExportDepTag:
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001300 if genRule, ok := dep.(genrule.SourceFileGenerator); ok {
Dan Willemsenb40aab62016-04-20 14:21:14 -07001301 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders,
Dan Willemsen9da9d492018-02-21 18:28:18 -08001302 genRule.GeneratedDeps()...)
Colin Cross5ed99c62016-11-22 12:55:55 -08001303 flags := includeDirsToFlags(genRule.GeneratedHeaderDirs())
Dan Willemsenb3454ab2016-09-28 17:34:58 -07001304 depPaths.Flags = append(depPaths.Flags, flags)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001305 if depTag == genHeaderExportDepTag {
Dan Willemsenb3454ab2016-09-28 17:34:58 -07001306 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags)
Dan Willemsen847dcc72016-09-29 12:13:36 -07001307 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps,
Dan Willemsen9da9d492018-02-21 18:28:18 -08001308 genRule.GeneratedDeps()...)
Jayant Chowdhary715cac32017-04-20 06:53:59 -07001309 // Add these re-exported flags to help header-abi-dumper to infer the abi exported by a library.
1310 c.sabi.Properties.ReexportedIncludeFlags = append(c.sabi.Properties.ReexportedIncludeFlags, flags)
1311
Dan Willemsenb3454ab2016-09-28 17:34:58 -07001312 }
Dan Willemsenb40aab62016-04-20 14:21:14 -07001313 } else {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001314 ctx.ModuleErrorf("module %q is not a genrule", depName)
Dan Willemsenb40aab62016-04-20 14:21:14 -07001315 }
Dan Willemsena0790e32018-10-12 00:24:23 -07001316 case linkerFlagsDepTag:
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001317 if genRule, ok := dep.(genrule.SourceFileGenerator); ok {
Dan Willemsenc77a0b32017-09-18 23:19:12 -07001318 files := genRule.GeneratedSourceFiles()
1319 if len(files) == 1 {
Dan Willemsena0790e32018-10-12 00:24:23 -07001320 depPaths.LinkerFlagsFile = android.OptionalPathForPath(files[0])
Dan Willemsenc77a0b32017-09-18 23:19:12 -07001321 } else if len(files) > 1 {
Dan Willemsena0790e32018-10-12 00:24:23 -07001322 ctx.ModuleErrorf("module %q can only generate a single file if used for a linker flag file", depName)
Dan Willemsenc77a0b32017-09-18 23:19:12 -07001323 }
1324 } else {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001325 ctx.ModuleErrorf("module %q is not a genrule", depName)
Dan Willemsenc77a0b32017-09-18 23:19:12 -07001326 }
Colin Crossca860ac2016-01-04 14:34:37 -08001327 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001328 return
1329 }
1330
Colin Crossd11fcda2017-10-23 17:59:01 -07001331 if dep.Target().Os != ctx.Os() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001332 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1333 return
1334 }
Colin Crossd11fcda2017-10-23 17:59:01 -07001335 if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001336 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
Colin Crossa1ad8d12016-06-01 17:09:44 -07001337 return
1338 }
1339
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001340 // re-exporting flags
1341 if depTag == reuseObjTag {
1342 if l, ok := ccDep.compiler.(libraryInterface); ok {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001343 c.staticVariant = ccDep
Colin Crossbbc9f4d2017-05-03 16:24:55 -07001344 objs, flags, deps := l.reuseObjs()
Colin Cross10d22312017-05-03 11:01:58 -07001345 depPaths.Objs = depPaths.Objs.Append(objs)
1346 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Colin Crossbbc9f4d2017-05-03 16:24:55 -07001347 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps, deps...)
Colin Crossbba99042016-11-23 15:45:05 -08001348 return
1349 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001350 }
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001351 if t, ok := depTag.(dependencyTag); ok && t.library {
1352 if i, ok := ccDep.linker.(exportedFlagsProducer); ok {
Dan Willemsen76f08272016-07-09 00:14:08 -07001353 flags := i.exportedFlags()
Dan Willemsen847dcc72016-09-29 12:13:36 -07001354 deps := i.exportedFlagsDeps()
Dan Willemsen76f08272016-07-09 00:14:08 -07001355 depPaths.Flags = append(depPaths.Flags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -07001356 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders, deps...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001357
1358 if t.reexportFlags {
Dan Willemsen76f08272016-07-09 00:14:08 -07001359 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -07001360 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps, deps...)
Jayant Chowdhary715cac32017-04-20 06:53:59 -07001361 // 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 -07001362 // Re-exported shared library headers must be included as well since they can help us with type information
1363 // about template instantiations (instantiated from their headers).
1364 c.sabi.Properties.ReexportedIncludeFlags = append(c.sabi.Properties.ReexportedIncludeFlags, flags...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001365 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001366 }
Dan Willemsena96ff642016-06-07 12:34:45 -07001367
Logan Chienf3511742017-10-31 18:04:35 +08001368 checkLinkType(ctx, c, ccDep, t)
Jiyong Park5fb8c102018-04-09 12:03:06 +09001369 checkDoubleLoadableLibries(ctx, c, ccDep)
Colin Crossc99deeb2016-04-11 15:06:20 -07001370 }
1371
Colin Cross26c34ed2016-09-30 17:10:16 -07001372 var ptr *android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -07001373 var depPtr *android.Paths
Colin Crossc99deeb2016-04-11 15:06:20 -07001374
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001375 linkFile := ccDep.outputFile
Colin Cross26c34ed2016-09-30 17:10:16 -07001376 depFile := android.OptionalPath{}
1377
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001378 switch depTag {
Dan Albert914449f2016-06-17 16:45:24 -07001379 case ndkStubDepTag, sharedDepTag, sharedExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001380 ptr = &depPaths.SharedLibs
1381 depPtr = &depPaths.SharedLibsDeps
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001382 depFile = ccDep.linker.(libraryInterface).toc()
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001383 directSharedDeps = append(directSharedDeps, ccDep)
Dan Albert914449f2016-06-17 16:45:24 -07001384 case lateSharedDepTag, ndkLateStubDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001385 ptr = &depPaths.LateSharedLibs
1386 depPtr = &depPaths.LateSharedLibsDeps
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001387 depFile = ccDep.linker.(libraryInterface).toc()
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001388 case staticDepTag, staticExportDepTag:
Jeff Gaston294356f2017-09-27 17:05:30 -07001389 ptr = nil
1390 directStaticDeps = append(directStaticDeps, ccDep)
Colin Crossc99deeb2016-04-11 15:06:20 -07001391 case lateStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001392 ptr = &depPaths.LateStaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -07001393 case wholeStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001394 ptr = &depPaths.WholeStaticLibs
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001395 staticLib, ok := ccDep.linker.(libraryInterface)
Colin Crossb916a382016-07-29 17:28:03 -07001396 if !ok || !staticLib.static() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001397 ctx.ModuleErrorf("module %q not a static library", depName)
Colin Crossc99deeb2016-04-11 15:06:20 -07001398 return
1399 }
1400
1401 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001402 postfix := " (required by " + ctx.OtherModuleName(dep) + ")"
Colin Crossc99deeb2016-04-11 15:06:20 -07001403 for i := range missingDeps {
1404 missingDeps[i] += postfix
1405 }
1406 ctx.AddMissingDependencies(missingDeps)
1407 }
Dan Willemsen5cb580f2016-09-26 17:33:01 -07001408 depPaths.WholeStaticLibObjs = depPaths.WholeStaticLibObjs.Append(staticLib.objs())
Colin Cross5950f382016-12-13 12:50:57 -08001409 case headerDepTag:
1410 // Nothing
Colin Crossc99deeb2016-04-11 15:06:20 -07001411 case objDepTag:
Dan Willemsen5cb580f2016-09-26 17:33:01 -07001412 depPaths.Objs.objFiles = append(depPaths.Objs.objFiles, linkFile.Path())
Colin Crossc99deeb2016-04-11 15:06:20 -07001413 case crtBeginDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001414 depPaths.CrtBegin = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -07001415 case crtEndDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001416 depPaths.CrtEnd = linkFile
Dan Willemsena0790e32018-10-12 00:24:23 -07001417 case dynamicLinkerDepTag:
1418 depPaths.DynamicLinker = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -07001419 }
1420
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001421 switch depTag {
Dan Willemsen581341d2017-02-09 16:16:31 -08001422 case staticDepTag, staticExportDepTag, lateStaticDepTag:
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001423 staticLib, ok := ccDep.linker.(libraryInterface)
Dan Willemsen581341d2017-02-09 16:16:31 -08001424 if !ok || !staticLib.static() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001425 ctx.ModuleErrorf("module %q not a static library", depName)
Dan Willemsen581341d2017-02-09 16:16:31 -08001426 return
1427 }
1428
1429 // When combining coverage files for shared libraries and executables, coverage files
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001430 // in static libraries act as if they were whole static libraries. The same goes for
1431 // source based Abi dump files.
Dan Willemsen581341d2017-02-09 16:16:31 -08001432 depPaths.StaticLibObjs.coverageFiles = append(depPaths.StaticLibObjs.coverageFiles,
1433 staticLib.objs().coverageFiles...)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001434 depPaths.StaticLibObjs.sAbiDumpFiles = append(depPaths.StaticLibObjs.sAbiDumpFiles,
1435 staticLib.objs().sAbiDumpFiles...)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001436
Dan Willemsen581341d2017-02-09 16:16:31 -08001437 }
1438
Colin Cross26c34ed2016-09-30 17:10:16 -07001439 if ptr != nil {
Colin Crossce75d2c2016-10-06 16:12:58 -07001440 if !linkFile.Valid() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001441 ctx.ModuleErrorf("module %q missing output file", depName)
Colin Crossce75d2c2016-10-06 16:12:58 -07001442 return
1443 }
Colin Cross26c34ed2016-09-30 17:10:16 -07001444 *ptr = append(*ptr, linkFile.Path())
1445 }
1446
Colin Crossc99deeb2016-04-11 15:06:20 -07001447 if depPtr != nil {
Colin Cross26c34ed2016-09-30 17:10:16 -07001448 dep := depFile
1449 if !dep.Valid() {
1450 dep = linkFile
1451 }
1452 *depPtr = append(*depPtr, dep.Path())
Colin Crossca860ac2016-01-04 14:34:37 -08001453 }
Jiyong Park27b188b2017-07-18 13:23:39 +09001454
Logan Chien43d34c32017-12-20 01:17:32 +08001455 makeLibName := func(depName string) string {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001456 libName := strings.TrimSuffix(depName, llndkLibrarySuffix)
Jiyong Park374510b2018-03-19 18:23:01 +09001457 libName = strings.TrimSuffix(libName, vendorPublicLibrarySuffix)
Jiyong Park27b188b2017-07-18 13:23:39 +09001458 libName = strings.TrimPrefix(libName, "prebuilt_")
Jiyong Parkd5b18a52017-08-03 21:22:50 +09001459 isLLndk := inList(libName, llndkLibraries)
Jiyong Park374510b2018-03-19 18:23:01 +09001460 isVendorPublicLib := inList(libName, vendorPublicLibraries)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001461 bothVendorAndCoreVariantsExist := ccDep.hasVendorVariant() || isLLndk
1462 if c.useVndk() && bothVendorAndCoreVariantsExist {
1463 // The vendor module in Make will have been renamed to not conflict with the core
1464 // module, so update the dependency name here accordingly.
Logan Chien43d34c32017-12-20 01:17:32 +08001465 return libName + vendorSuffix
Jiyong Park374510b2018-03-19 18:23:01 +09001466 } else if (ctx.Platform() || ctx.ProductSpecific()) && isVendorPublicLib {
Logan Chien43d34c32017-12-20 01:17:32 +08001467 return libName + vendorPublicLibrarySuffix
Jiyong Parkf9332f12018-02-01 00:54:12 +09001468 } else if ccDep.inRecovery() && !ccDep.onlyInRecovery() {
1469 return libName + recoverySuffix
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001470 } else {
Logan Chien43d34c32017-12-20 01:17:32 +08001471 return libName
Jiyong Park27b188b2017-07-18 13:23:39 +09001472 }
Logan Chien43d34c32017-12-20 01:17:32 +08001473 }
1474
1475 // Export the shared libs to Make.
1476 switch depTag {
1477 case sharedDepTag, sharedExportDepTag, lateSharedDepTag:
Jiyong Park27b188b2017-07-18 13:23:39 +09001478 // Note: the order of libs in this list is not important because
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001479 // they merely serve as Make dependencies and do not affect this lib itself.
Logan Chien43d34c32017-12-20 01:17:32 +08001480 c.Properties.AndroidMkSharedLibs = append(
1481 c.Properties.AndroidMkSharedLibs, makeLibName(depName))
1482 case runtimeDepTag:
1483 c.Properties.AndroidMkRuntimeLibs = append(
1484 c.Properties.AndroidMkRuntimeLibs, makeLibName(depName))
Jiyong Park27b188b2017-07-18 13:23:39 +09001485 }
Colin Crossca860ac2016-01-04 14:34:37 -08001486 })
1487
Jeff Gaston294356f2017-09-27 17:05:30 -07001488 // use the ordered dependencies as this module's dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001489 depPaths.StaticLibs = append(depPaths.StaticLibs, orderStaticModuleDeps(c, directStaticDeps, directSharedDeps)...)
Jeff Gaston294356f2017-09-27 17:05:30 -07001490
Colin Crossdd84e052017-05-17 13:44:16 -07001491 // Dedup exported flags from dependencies
Colin Crossb6715442017-10-24 11:13:31 -07001492 depPaths.Flags = android.FirstUniqueStrings(depPaths.Flags)
Dan Willemsenfe92c962017-08-29 12:28:37 -07001493 depPaths.GeneratedHeaders = android.FirstUniquePaths(depPaths.GeneratedHeaders)
Colin Crossb6715442017-10-24 11:13:31 -07001494 depPaths.ReexportedFlags = android.FirstUniqueStrings(depPaths.ReexportedFlags)
Dan Willemsenfe92c962017-08-29 12:28:37 -07001495 depPaths.ReexportedFlagsDeps = android.FirstUniquePaths(depPaths.ReexportedFlagsDeps)
1496
1497 if c.sabi != nil {
Colin Crossb6715442017-10-24 11:13:31 -07001498 c.sabi.Properties.ReexportedIncludeFlags = android.FirstUniqueStrings(c.sabi.Properties.ReexportedIncludeFlags)
Dan Willemsenfe92c962017-08-29 12:28:37 -07001499 }
Colin Crossdd84e052017-05-17 13:44:16 -07001500
Colin Crossca860ac2016-01-04 14:34:37 -08001501 return depPaths
1502}
1503
1504func (c *Module) InstallInData() bool {
1505 if c.installer == nil {
1506 return false
1507 }
Vishwath Mohan1dd88392017-03-29 22:00:18 -07001508 return c.installer.inData()
1509}
1510
1511func (c *Module) InstallInSanitizerDir() bool {
1512 if c.installer == nil {
1513 return false
1514 }
1515 if c.sanitize != nil && c.sanitize.inSanitizerDir() {
Colin Cross94610402016-08-29 13:41:32 -07001516 return true
1517 }
Vishwath Mohan1dd88392017-03-29 22:00:18 -07001518 return c.installer.inSanitizerDir()
Colin Crossca860ac2016-01-04 14:34:37 -08001519}
1520
Jiyong Parkf9332f12018-02-01 00:54:12 +09001521func (c *Module) InstallInRecovery() bool {
1522 return c.inRecovery()
1523}
1524
Dan Willemsen4aa75ca2016-09-28 16:18:03 -07001525func (c *Module) HostToolPath() android.OptionalPath {
1526 if c.installer == nil {
1527 return android.OptionalPath{}
1528 }
1529 return c.installer.hostToolPath()
1530}
1531
Nan Zhangd4e641b2017-07-12 12:55:28 -07001532func (c *Module) IntermPathForModuleOut() android.OptionalPath {
1533 return c.outputFile
1534}
1535
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001536func (c *Module) Srcs() android.Paths {
1537 if c.outputFile.Valid() {
1538 return android.Paths{c.outputFile.Path()}
1539 }
1540 return android.Paths{}
1541}
1542
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001543func (c *Module) static() bool {
1544 if static, ok := c.linker.(interface {
1545 static() bool
1546 }); ok {
1547 return static.static()
1548 }
1549 return false
1550}
1551
Colin Crossb60190a2018-09-04 16:28:17 -07001552func (c *Module) getMakeLinkType() string {
1553 if c.useVndk() {
1554 if inList(c.Name(), vndkCoreLibraries) || inList(c.Name(), vndkSpLibraries) || inList(c.Name(), llndkLibraries) {
1555 if inList(c.Name(), vndkPrivateLibraries) {
1556 return "native:vndk_private"
1557 } else {
1558 return "native:vndk"
1559 }
1560 } else {
1561 return "native:vendor"
1562 }
1563 } else if c.inRecovery() {
1564 return "native:recovery"
1565 } else if c.Target().Os == android.Android && String(c.Properties.Sdk_version) != "" {
1566 return "native:ndk:none:none"
1567 // TODO(b/114741097): use the correct ndk stl once build errors have been fixed
1568 //family, link := getNdkStlFamilyAndLinkType(c)
1569 //return fmt.Sprintf("native:ndk:%s:%s", family, link)
1570 } else {
1571 return "native:platform"
1572 }
1573}
1574
Jiyong Park9d452992018-10-03 00:38:19 +09001575// Overrides ApexModule.IsInstallabeToApex()
1576// Only shared libraries are installable to APEX.
1577func (c *Module) IsInstallableToApex() bool {
1578 if shared, ok := c.linker.(interface {
1579 shared() bool
1580 }); ok {
1581 return shared.shared()
1582 }
1583 return false
1584}
1585
Colin Cross2ba19d92015-05-07 15:44:20 -07001586//
Colin Crosscfad1192015-11-02 16:43:11 -08001587// Defaults
1588//
Colin Crossca860ac2016-01-04 14:34:37 -08001589type Defaults struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001590 android.ModuleBase
Colin Cross1f44a3a2017-07-07 14:33:33 -07001591 android.DefaultsModuleBase
Jiyong Park9d452992018-10-03 00:38:19 +09001592 android.ApexModuleBase
Colin Crosscfad1192015-11-02 16:43:11 -08001593}
1594
Colin Cross635c3b02016-05-18 15:37:25 -07001595func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -08001596}
1597
Colin Cross1e676be2016-10-12 14:38:15 -07001598func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
1599}
1600
Colin Cross36242852017-06-23 15:06:31 -07001601func defaultsFactory() android.Module {
Colin Crosse1d764e2016-08-18 14:18:32 -07001602 return DefaultsFactory()
1603}
1604
Colin Cross36242852017-06-23 15:06:31 -07001605func DefaultsFactory(props ...interface{}) android.Module {
Colin Crossca860ac2016-01-04 14:34:37 -08001606 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -08001607
Colin Cross36242852017-06-23 15:06:31 -07001608 module.AddProperties(props...)
1609 module.AddProperties(
Colin Crossca860ac2016-01-04 14:34:37 -08001610 &BaseProperties{},
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001611 &VendorProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001612 &BaseCompilerProperties{},
1613 &BaseLinkerProperties{},
Chih-Hung Hsieh86814712018-05-23 18:30:46 -07001614 &MoreBaseLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -07001615 &LibraryProperties{},
Colin Cross919281a2016-04-05 16:42:05 -07001616 &FlagExporterProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001617 &BinaryLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -07001618 &TestProperties{},
1619 &TestBinaryProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001620 &StlProperties{},
Colin Cross16b23492016-01-06 14:41:07 -08001621 &SanitizeProperties{},
Colin Cross665dce92016-04-28 14:50:03 -07001622 &StripProperties{},
Dan Willemsen7424d612016-09-01 13:45:39 -07001623 &InstallerProperties{},
Dan Willemsena03cf6d2016-09-26 15:45:04 -07001624 &TidyProperties{},
Dan Willemsen581341d2017-02-09 16:16:31 -08001625 &CoverageProperties{},
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001626 &SAbiProperties{},
Justin Yun4b2382f2017-07-26 14:22:10 +09001627 &VndkProperties{},
Stephen Craneba090d12017-05-09 15:44:35 -07001628 &LTOProperties{},
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -07001629 &PgoProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08001630 &android.ProtoProperties{},
Colin Crosse1d764e2016-08-18 14:18:32 -07001631 )
Colin Crosscfad1192015-11-02 16:43:11 -08001632
Colin Cross1f44a3a2017-07-07 14:33:33 -07001633 android.InitDefaultsModule(module)
Jiyong Park9d452992018-10-03 00:38:19 +09001634 android.InitApexModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001635
1636 return module
Colin Crosscfad1192015-11-02 16:43:11 -08001637}
1638
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001639const (
1640 // coreMode is the variant used for framework-private libraries, or
1641 // SDK libraries. (which framework-private libraries can use)
1642 coreMode = "core"
1643
1644 // vendorMode is the variant used for /vendor code that compiles
1645 // against the VNDK.
1646 vendorMode = "vendor"
Jiyong Parkf9332f12018-02-01 00:54:12 +09001647
1648 recoveryMode = "recovery"
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001649)
1650
Jiyong Park6a43f042017-10-12 23:05:00 +09001651func squashVendorSrcs(m *Module) {
1652 if lib, ok := m.compiler.(*libraryDecorator); ok {
1653 lib.baseCompiler.Properties.Srcs = append(lib.baseCompiler.Properties.Srcs,
1654 lib.baseCompiler.Properties.Target.Vendor.Srcs...)
1655
1656 lib.baseCompiler.Properties.Exclude_srcs = append(lib.baseCompiler.Properties.Exclude_srcs,
1657 lib.baseCompiler.Properties.Target.Vendor.Exclude_srcs...)
1658 }
1659}
1660
Jiyong Parkf9332f12018-02-01 00:54:12 +09001661func squashRecoverySrcs(m *Module) {
1662 if lib, ok := m.compiler.(*libraryDecorator); ok {
1663 lib.baseCompiler.Properties.Srcs = append(lib.baseCompiler.Properties.Srcs,
1664 lib.baseCompiler.Properties.Target.Recovery.Srcs...)
1665
1666 lib.baseCompiler.Properties.Exclude_srcs = append(lib.baseCompiler.Properties.Exclude_srcs,
1667 lib.baseCompiler.Properties.Target.Recovery.Exclude_srcs...)
1668 }
1669}
1670
1671func imageMutator(mctx android.BottomUpMutatorContext) {
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001672 if mctx.Os() != android.Android {
1673 return
1674 }
1675
Jiyong Park7ed9de32018-10-15 22:25:07 +09001676 if g, ok := mctx.Module().(*genrule.Module); ok {
1677 if props, ok := g.Extra.(*GenruleExtraProperties); ok {
Jiyong Park3f736c92018-05-24 13:36:56 +09001678 var coreVariantNeeded bool = false
1679 var vendorVariantNeeded bool = false
1680 var recoveryVariantNeeded bool = false
Justin Yun71549282017-11-17 12:10:28 +09001681 if mctx.DeviceConfig().VndkVersion() == "" {
Jiyong Park3f736c92018-05-24 13:36:56 +09001682 coreVariantNeeded = true
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001683 } else if Bool(props.Vendor_available) {
Jiyong Park3f736c92018-05-24 13:36:56 +09001684 coreVariantNeeded = true
1685 vendorVariantNeeded = true
Jiyong Park2db76922017-11-08 16:03:48 +09001686 } else if mctx.SocSpecific() || mctx.DeviceSpecific() {
Jiyong Park3f736c92018-05-24 13:36:56 +09001687 vendorVariantNeeded = true
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001688 } else {
Jiyong Park3f736c92018-05-24 13:36:56 +09001689 coreVariantNeeded = true
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001690 }
Jiyong Park3f736c92018-05-24 13:36:56 +09001691 if Bool(props.Recovery_available) {
1692 recoveryVariantNeeded = true
1693 }
1694
Jiyong Park413cc742018-06-19 01:46:06 +09001695 if recoveryVariantNeeded {
Jiyong Park8d52f862018-07-07 18:02:07 +09001696 primaryArch := mctx.Config().DevicePrimaryArchType()
Jiyong Park7ed9de32018-10-15 22:25:07 +09001697 moduleArch := g.Target().Arch.ArchType
Jiyong Park8d52f862018-07-07 18:02:07 +09001698 if moduleArch != primaryArch {
Jiyong Park413cc742018-06-19 01:46:06 +09001699 recoveryVariantNeeded = false
1700 }
1701 }
1702
Jiyong Park3f736c92018-05-24 13:36:56 +09001703 var variants []string
1704 if coreVariantNeeded {
1705 variants = append(variants, coreMode)
1706 }
1707 if vendorVariantNeeded {
1708 variants = append(variants, vendorMode)
1709 }
1710 if recoveryVariantNeeded {
1711 variants = append(variants, recoveryMode)
1712 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09001713 mod := mctx.CreateVariations(variants...)
1714 for i, v := range variants {
1715 if v == recoveryMode {
1716 m := mod[i].(*genrule.Module)
1717 m.Extra.(*GenruleExtraProperties).InRecovery = true
1718 }
1719 }
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001720 }
1721 }
1722
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001723 m, ok := mctx.Module().(*Module)
1724 if !ok {
1725 return
1726 }
1727
1728 // Sanity check
Logan Chienf3511742017-10-31 18:04:35 +08001729 vendorSpecific := mctx.SocSpecific() || mctx.DeviceSpecific()
1730
1731 if m.VendorProperties.Vendor_available != nil && vendorSpecific {
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001732 mctx.PropertyErrorf("vendor_available",
Jiyong Park2db76922017-11-08 16:03:48 +09001733 "doesn't make sense at the same time as `vendor: true`, `proprietary: true`, or `device_specific:true`")
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001734 return
1735 }
Logan Chienf3511742017-10-31 18:04:35 +08001736
1737 if vndkdep := m.vndkdep; vndkdep != nil {
1738 if vndkdep.isVndk() {
1739 if vendorSpecific {
1740 if !vndkdep.isVndkExt() {
1741 mctx.PropertyErrorf("vndk",
1742 "must set `extends: \"...\"` to vndk extension")
1743 return
1744 }
1745 } else {
1746 if vndkdep.isVndkExt() {
1747 mctx.PropertyErrorf("vndk",
1748 "must set `vendor: true` to set `extends: %q`",
1749 m.getVndkExtendsModuleName())
1750 return
1751 }
1752 if m.VendorProperties.Vendor_available == nil {
1753 mctx.PropertyErrorf("vndk",
1754 "vendor_available must be set to either true or false when `vndk: {enabled: true}`")
1755 return
1756 }
1757 }
1758 } else {
1759 if vndkdep.isVndkSp() {
1760 mctx.PropertyErrorf("vndk",
1761 "must set `enabled: true` to set `support_system_process: true`")
1762 return
1763 }
1764 if vndkdep.isVndkExt() {
1765 mctx.PropertyErrorf("vndk",
1766 "must set `enabled: true` to set `extends: %q`",
1767 m.getVndkExtendsModuleName())
1768 return
1769 }
Justin Yun8effde42017-06-23 19:24:43 +09001770 }
1771 }
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001772
Jiyong Parkf9332f12018-02-01 00:54:12 +09001773 var coreVariantNeeded bool = false
1774 var vendorVariantNeeded bool = false
1775 var recoveryVariantNeeded bool = false
1776
Justin Yun71549282017-11-17 12:10:28 +09001777 if mctx.DeviceConfig().VndkVersion() == "" {
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001778 // If the device isn't compiling against the VNDK, we always
1779 // use the core mode.
Jiyong Parkf9332f12018-02-01 00:54:12 +09001780 coreVariantNeeded = true
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001781 } else if _, ok := m.linker.(*llndkStubDecorator); ok {
1782 // LL-NDK stubs only exist in the vendor variant, since the
1783 // real libraries will be used in the core variant.
Jiyong Parkf9332f12018-02-01 00:54:12 +09001784 vendorVariantNeeded = true
Jiyong Park2a454122017-10-19 15:59:33 +09001785 } else if _, ok := m.linker.(*llndkHeadersDecorator); ok {
1786 // ... and LL-NDK headers as well
Jiyong Parkf9332f12018-02-01 00:54:12 +09001787 vendorVariantNeeded = true
Justin Yun312ccb92018-01-23 12:07:46 +09001788 } else if _, ok := m.linker.(*vndkPrebuiltLibraryDecorator); ok {
Justin Yun71549282017-11-17 12:10:28 +09001789 // Make vendor variants only for the versions in BOARD_VNDK_VERSION and
1790 // PRODUCT_EXTRA_VNDK_VERSIONS.
Jiyong Parkf9332f12018-02-01 00:54:12 +09001791 vendorVariantNeeded = true
Logan Chienf3511742017-10-31 18:04:35 +08001792 } else if m.hasVendorVariant() && !vendorSpecific {
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001793 // This will be available in both /system and /vendor
Justin Yun8effde42017-06-23 19:24:43 +09001794 // or a /system directory that is available to vendor.
Jiyong Parkf9332f12018-02-01 00:54:12 +09001795 coreVariantNeeded = true
1796 vendorVariantNeeded = true
Logan Chienf3511742017-10-31 18:04:35 +08001797 } else if vendorSpecific && String(m.Properties.Sdk_version) == "" {
Jiyong Park2db76922017-11-08 16:03:48 +09001798 // This will be available in /vendor (or /odm) only
Jiyong Parkf9332f12018-02-01 00:54:12 +09001799 vendorVariantNeeded = true
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001800 } else {
1801 // This is either in /system (or similar: /data), or is a
1802 // modules built with the NDK. Modules built with the NDK
1803 // will be restricted using the existing link type checks.
Jiyong Parkf9332f12018-02-01 00:54:12 +09001804 coreVariantNeeded = true
1805 }
1806
1807 if Bool(m.Properties.Recovery_available) {
1808 recoveryVariantNeeded = true
1809 }
1810
1811 if m.ModuleBase.InstallInRecovery() {
1812 recoveryVariantNeeded = true
1813 coreVariantNeeded = false
1814 }
1815
Jiyong Park413cc742018-06-19 01:46:06 +09001816 if recoveryVariantNeeded {
Jiyong Park8d52f862018-07-07 18:02:07 +09001817 primaryArch := mctx.Config().DevicePrimaryArchType()
1818 moduleArch := m.Target().Arch.ArchType
1819 if moduleArch != primaryArch {
Jiyong Park413cc742018-06-19 01:46:06 +09001820 recoveryVariantNeeded = false
1821 }
1822 }
1823
Jiyong Parkf9332f12018-02-01 00:54:12 +09001824 var variants []string
1825 if coreVariantNeeded {
1826 variants = append(variants, coreMode)
1827 }
1828 if vendorVariantNeeded {
1829 variants = append(variants, vendorMode)
1830 }
1831 if recoveryVariantNeeded {
1832 variants = append(variants, recoveryMode)
1833 }
1834 mod := mctx.CreateVariations(variants...)
1835 for i, v := range variants {
1836 if v == vendorMode {
1837 m := mod[i].(*Module)
1838 m.Properties.UseVndk = true
1839 squashVendorSrcs(m)
1840 } else if v == recoveryMode {
1841 m := mod[i].(*Module)
1842 m.Properties.InRecovery = true
Jiyong Park5baac542018-08-28 09:55:37 +09001843 m.MakeAsPlatform()
Jiyong Parkf9332f12018-02-01 00:54:12 +09001844 squashRecoverySrcs(m)
1845 }
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001846 }
1847}
1848
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -07001849func getCurrentNdkPrebuiltVersion(ctx DepsContext) string {
Colin Cross6510f912017-11-29 00:27:14 -08001850 if ctx.Config().PlatformSdkVersionInt() > config.NdkMaxPrebuiltVersionInt {
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -07001851 return strconv.Itoa(config.NdkMaxPrebuiltVersionInt)
1852 }
Colin Cross6510f912017-11-29 00:27:14 -08001853 return ctx.Config().PlatformSdkVersion()
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -07001854}
1855
Colin Cross06a931b2015-10-28 17:23:31 -07001856var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07001857var BoolDefault = proptools.BoolDefault
Nan Zhang0007d812017-11-07 10:57:05 -08001858var BoolPtr = proptools.BoolPtr
1859var String = proptools.String
1860var StringPtr = proptools.StringPtr