blob: 9a44bdeea69483e8af8c459b363de36104f3f40d [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
Colin Crossc472d572015-03-17 15:06:21 -0700160}
161
Colin Cross81413472016-04-11 14:37:39 -0700162type ObjectLinkerProperties struct {
163 // names of other cc_object modules to link into this module using partial linking
164 Objs []string `android:"arch_variant"`
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700165
166 // if set, add an extra objcopy --prefix-symbols= step
Nan Zhang0007d812017-11-07 10:57:05 -0800167 Prefix_symbols *string
Colin Cross81413472016-04-11 14:37:39 -0700168}
169
Colin Crossca860ac2016-01-04 14:34:37 -0800170// Properties used to compile all C or C++ modules
171type BaseProperties struct {
Dan Willemsen742a5452018-07-23 17:19:36 -0700172 // Deprecated. true is the default, false is invalid.
Colin Crossca860ac2016-01-04 14:34:37 -0800173 Clang *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700174
175 // Minimum sdk version supported when compiling against the ndk
Nan Zhang0007d812017-11-07 10:57:05 -0800176 Sdk_version *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700177
Jaewoong Jung555c1142018-11-09 22:25:37 +0000178 AndroidMkSharedLibs []string `blueprint:"mutated"`
179 AndroidMkRuntimeLibs []string `blueprint:"mutated"`
180 HideFromMake bool `blueprint:"mutated"`
181 PreventInstall bool `blueprint:"mutated"`
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700182
183 UseVndk bool `blueprint:"mutated"`
Colin Cross5beccee2017-12-07 15:28:59 -0800184
185 // *.logtags files, to combine together in order to generate the /system/etc/event-log-tags
186 // file
187 Logtags []string
Jiyong Parkf9332f12018-02-01 00:54:12 +0900188
189 // Make this module available when building for recovery
190 Recovery_available *bool
191
192 InRecovery bool `blueprint:"mutated"`
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700193}
194
195type VendorProperties struct {
Jiyong Park82e2bf32017-08-16 14:05:54 +0900196 // whether this module should be allowed to be directly depended by other
197 // modules with `vendor: true`, `proprietary: true`, or `vendor_available:true`.
198 // If set to true, two variants will be built separately, one like
199 // normal, and the other limited to the set of libraries and headers
200 // that are exposed to /vendor modules.
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700201 //
202 // The vendor variant may be used with a different (newer) /system,
203 // so it shouldn't have any unversioned runtime dependencies, or
204 // make assumptions about the system that may not be true in the
205 // future.
206 //
Jiyong Park82e2bf32017-08-16 14:05:54 +0900207 // If set to false, this module becomes inaccessible from /vendor modules.
208 //
209 // Default value is true when vndk: {enabled: true} or vendor: true.
210 //
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700211 // Nothing happens if BOARD_VNDK_VERSION isn't set in the BoardConfig.mk
212 Vendor_available *bool
Jiyong Park5fb8c102018-04-09 12:03:06 +0900213
214 // whether this module is capable of being loaded with other instance
215 // (possibly an older version) of the same module in the same process.
216 // Currently, a shared library that is a member of VNDK (vndk: {enabled: true})
217 // can be double loaded in a vendor process if the library is also a
218 // (direct and indirect) dependency of an LLNDK library. Such libraries must be
219 // explicitly marked as `double_loadable: true` by the owner, or the dependency
220 // from the LLNDK lib should be cut if the lib is not designed to be double loaded.
221 Double_loadable *bool
Colin Crossca860ac2016-01-04 14:34:37 -0800222}
223
Colin Crossca860ac2016-01-04 14:34:37 -0800224type ModuleContextIntf interface {
Colin Crossca860ac2016-01-04 14:34:37 -0800225 static() bool
226 staticBinary() bool
Colin Crossb98c8b02016-07-29 13:44:28 -0700227 toolchain() config.Toolchain
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700228 useSdk() bool
Colin Crossca860ac2016-01-04 14:34:37 -0800229 sdkVersion() string
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700230 useVndk() bool
Justin Yun8effde42017-06-23 19:24:43 +0900231 isVndk() bool
232 isVndkSp() bool
Logan Chienf3511742017-10-31 18:04:35 +0800233 isVndkExt() bool
Jiyong Parkf9332f12018-02-01 00:54:12 +0900234 inRecovery() bool
Logan Chien2f2b8902018-07-10 15:01:19 +0800235 shouldCreateVndkSourceAbiDump() bool
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700236 selectedStl() string
Colin Crossce75d2c2016-10-06 16:12:58 -0700237 baseModuleName() string
Logan Chienf3511742017-10-31 18:04:35 +0800238 getVndkExtendsModuleName() string
Yi Kong7e53c572018-02-14 18:16:12 +0800239 isPgoCompile() bool
Colin Crossca860ac2016-01-04 14:34:37 -0800240}
241
242type ModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700243 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800244 ModuleContextIntf
245}
246
247type BaseModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700248 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800249 ModuleContextIntf
250}
251
Colin Cross37047f12016-12-13 17:06:13 -0800252type DepsContext interface {
253 android.BottomUpMutatorContext
254 ModuleContextIntf
255}
256
Colin Crossca860ac2016-01-04 14:34:37 -0800257type feature interface {
258 begin(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800259 deps(ctx DepsContext, deps Deps) Deps
Colin Crossca860ac2016-01-04 14:34:37 -0800260 flags(ctx ModuleContext, flags Flags) Flags
261 props() []interface{}
262}
263
264type compiler interface {
Colin Cross42742b82016-08-01 13:20:05 -0700265 compilerInit(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800266 compilerDeps(ctx DepsContext, deps Deps) Deps
Colin Crossf18e1102017-11-16 14:33:08 -0800267 compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags
Colin Cross42742b82016-08-01 13:20:05 -0700268 compilerProps() []interface{}
269
Colin Cross76fada02016-07-27 10:31:13 -0700270 appendCflags([]string)
271 appendAsflags([]string)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700272 compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects
Colin Crossca860ac2016-01-04 14:34:37 -0800273}
274
275type linker interface {
Colin Cross42742b82016-08-01 13:20:05 -0700276 linkerInit(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800277 linkerDeps(ctx DepsContext, deps Deps) Deps
Colin Cross42742b82016-08-01 13:20:05 -0700278 linkerFlags(ctx ModuleContext, flags Flags) Flags
279 linkerProps() []interface{}
280
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700281 link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path
Colin Cross76fada02016-07-27 10:31:13 -0700282 appendLdflags([]string)
Colin Crossca860ac2016-01-04 14:34:37 -0800283}
284
285type installer interface {
Colin Cross42742b82016-08-01 13:20:05 -0700286 installerProps() []interface{}
Colin Cross635c3b02016-05-18 15:37:25 -0700287 install(ctx ModuleContext, path android.Path)
Colin Crossca860ac2016-01-04 14:34:37 -0800288 inData() bool
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700289 inSanitizerDir() bool
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700290 hostToolPath() android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800291}
292
Colin Crossc99deeb2016-04-11 15:06:20 -0700293type dependencyTag struct {
294 blueprint.BaseDependencyTag
295 name string
296 library bool
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700297
298 reexportFlags bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700299}
300
301var (
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700302 sharedDepTag = dependencyTag{name: "shared", library: true}
303 sharedExportDepTag = dependencyTag{name: "shared", library: true, reexportFlags: true}
304 lateSharedDepTag = dependencyTag{name: "late shared", library: true}
305 staticDepTag = dependencyTag{name: "static", library: true}
306 staticExportDepTag = dependencyTag{name: "static", library: true, reexportFlags: true}
307 lateStaticDepTag = dependencyTag{name: "late static", library: true}
308 wholeStaticDepTag = dependencyTag{name: "whole static", library: true, reexportFlags: true}
Colin Cross32ec36c2016-12-15 07:39:51 -0800309 headerDepTag = dependencyTag{name: "header", library: true}
310 headerExportDepTag = dependencyTag{name: "header", library: true, reexportFlags: true}
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700311 genSourceDepTag = dependencyTag{name: "gen source"}
312 genHeaderDepTag = dependencyTag{name: "gen header"}
313 genHeaderExportDepTag = dependencyTag{name: "gen header", reexportFlags: true}
314 objDepTag = dependencyTag{name: "obj"}
315 crtBeginDepTag = dependencyTag{name: "crtbegin"}
316 crtEndDepTag = dependencyTag{name: "crtend"}
Dan Willemsena0790e32018-10-12 00:24:23 -0700317 linkerFlagsDepTag = dependencyTag{name: "linker flags file"}
318 dynamicLinkerDepTag = dependencyTag{name: "dynamic linker"}
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700319 reuseObjTag = dependencyTag{name: "reuse objects"}
320 ndkStubDepTag = dependencyTag{name: "ndk stub", library: true}
321 ndkLateStubDepTag = dependencyTag{name: "ndk late stub", library: true}
Logan Chienf3511742017-10-31 18:04:35 +0800322 vndkExtDepTag = dependencyTag{name: "vndk extends", library: true}
Logan Chien43d34c32017-12-20 01:17:32 +0800323 runtimeDepTag = dependencyTag{name: "runtime lib"}
Colin Crossc99deeb2016-04-11 15:06:20 -0700324)
325
Colin Crossca860ac2016-01-04 14:34:37 -0800326// Module contains the properties and members used by all C/C++ module types, and implements
327// the blueprint.Module interface. It delegates to compiler, linker, and installer interfaces
328// to construct the output file. Behavior can be customized with a Customizer interface
329type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700330 android.ModuleBase
Colin Cross1f44a3a2017-07-07 14:33:33 -0700331 android.DefaultableModuleBase
Jiyong Park9d452992018-10-03 00:38:19 +0900332 android.ApexModuleBase
Colin Crossc472d572015-03-17 15:06:21 -0700333
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700334 Properties BaseProperties
335 VendorProperties VendorProperties
Colin Crossfa138792015-04-24 17:31:52 -0700336
Colin Crossca860ac2016-01-04 14:34:37 -0800337 // initialize before calling Init
Colin Cross635c3b02016-05-18 15:37:25 -0700338 hod android.HostOrDeviceSupported
339 multilib android.Multilib
Colin Crossc472d572015-03-17 15:06:21 -0700340
Colin Crossca860ac2016-01-04 14:34:37 -0800341 // delegates, initialize before calling Init
Colin Crossb4ce0ec2016-09-13 13:41:39 -0700342 features []feature
343 compiler compiler
344 linker linker
345 installer installer
346 stl *stl
347 sanitize *sanitize
Dan Willemsen581341d2017-02-09 16:16:31 -0800348 coverage *coverage
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800349 sabi *sabi
Justin Yun8effde42017-06-23 19:24:43 +0900350 vndkdep *vndkdep
Stephen Craneba090d12017-05-09 15:44:35 -0700351 lto *lto
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700352 pgo *pgo
Colin Cross16b23492016-01-06 14:41:07 -0800353
354 androidMkSharedLibDeps []string
Colin Cross74d1ec02015-04-28 13:30:13 -0700355
Colin Cross635c3b02016-05-18 15:37:25 -0700356 outputFile android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800357
Colin Crossb98c8b02016-07-29 13:44:28 -0700358 cachedToolchain config.Toolchain
Colin Crossb916a382016-07-29 17:28:03 -0700359
360 subAndroidMkOnce map[subAndroidMkProvider]bool
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800361
362 // Flags used to compile this module
363 flags Flags
Jeff Gaston294356f2017-09-27 17:05:30 -0700364
365 // 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 -0800366 // line invocation. depsInLinkOrder stores the proper ordering of all of the transitive
Jeff Gaston294356f2017-09-27 17:05:30 -0700367 // deps of this module
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -0800368 depsInLinkOrder android.Paths
369
370 // only non-nil when this is a shared library that reuses the objects of a static library
371 staticVariant *Module
Colin Crossc472d572015-03-17 15:06:21 -0700372}
373
Jiyong Parkc20eee32018-09-05 22:36:17 +0900374func (c *Module) OutputFile() android.OptionalPath {
375 return c.outputFile
376}
377
Colin Cross36242852017-06-23 15:06:31 -0700378func (c *Module) Init() android.Module {
Dan Willemsenf923f2b2018-05-09 13:45:03 -0700379 c.AddProperties(&c.Properties, &c.VendorProperties)
Colin Crossca860ac2016-01-04 14:34:37 -0800380 if c.compiler != nil {
Colin Cross36242852017-06-23 15:06:31 -0700381 c.AddProperties(c.compiler.compilerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800382 }
383 if c.linker != nil {
Colin Cross36242852017-06-23 15:06:31 -0700384 c.AddProperties(c.linker.linkerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800385 }
386 if c.installer != nil {
Colin Cross36242852017-06-23 15:06:31 -0700387 c.AddProperties(c.installer.installerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800388 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700389 if c.stl != nil {
Colin Cross36242852017-06-23 15:06:31 -0700390 c.AddProperties(c.stl.props()...)
Colin Crossa8e07cc2016-04-04 15:07:06 -0700391 }
Colin Cross16b23492016-01-06 14:41:07 -0800392 if c.sanitize != nil {
Colin Cross36242852017-06-23 15:06:31 -0700393 c.AddProperties(c.sanitize.props()...)
Colin Cross16b23492016-01-06 14:41:07 -0800394 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800395 if c.coverage != nil {
Colin Cross36242852017-06-23 15:06:31 -0700396 c.AddProperties(c.coverage.props()...)
Dan Willemsen581341d2017-02-09 16:16:31 -0800397 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800398 if c.sabi != nil {
Colin Cross36242852017-06-23 15:06:31 -0700399 c.AddProperties(c.sabi.props()...)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800400 }
Justin Yun8effde42017-06-23 19:24:43 +0900401 if c.vndkdep != nil {
402 c.AddProperties(c.vndkdep.props()...)
403 }
Stephen Craneba090d12017-05-09 15:44:35 -0700404 if c.lto != nil {
405 c.AddProperties(c.lto.props()...)
406 }
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700407 if c.pgo != nil {
408 c.AddProperties(c.pgo.props()...)
409 }
Colin Crossca860ac2016-01-04 14:34:37 -0800410 for _, feature := range c.features {
Colin Cross36242852017-06-23 15:06:31 -0700411 c.AddProperties(feature.props()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800412 }
Colin Crossc472d572015-03-17 15:06:21 -0700413
Colin Crossa9d8bee2018-10-02 13:59:46 -0700414 c.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
415 switch class {
416 case android.Device:
417 return ctx.Config().DevicePrefer32BitExecutables()
418 case android.HostCross:
419 // Windows builds always prefer 32-bit
420 return true
421 default:
422 return false
423 }
424 })
Colin Cross36242852017-06-23 15:06:31 -0700425 android.InitAndroidArchModule(c, c.hod, c.multilib)
Colin Crossc472d572015-03-17 15:06:21 -0700426
Colin Cross1f44a3a2017-07-07 14:33:33 -0700427 android.InitDefaultableModule(c)
Colin Cross36242852017-06-23 15:06:31 -0700428
Jiyong Park9d452992018-10-03 00:38:19 +0900429 android.InitApexModule(c)
430
Colin Cross36242852017-06-23 15:06:31 -0700431 return c
Colin Crossc472d572015-03-17 15:06:21 -0700432}
433
Colin Crossb916a382016-07-29 17:28:03 -0700434// Returns true for dependency roots (binaries)
435// TODO(ccross): also handle dlopenable libraries
436func (c *Module) isDependencyRoot() bool {
437 if root, ok := c.linker.(interface {
438 isDependencyRoot() bool
439 }); ok {
440 return root.isDependencyRoot()
441 }
442 return false
443}
444
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700445func (c *Module) useVndk() bool {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700446 return c.Properties.UseVndk
447}
448
Justin Yun8effde42017-06-23 19:24:43 +0900449func (c *Module) isVndk() bool {
Logan Chienf3511742017-10-31 18:04:35 +0800450 if vndkdep := c.vndkdep; vndkdep != nil {
451 return vndkdep.isVndk()
Justin Yun8effde42017-06-23 19:24:43 +0900452 }
453 return false
454}
455
Yi Kong7e53c572018-02-14 18:16:12 +0800456func (c *Module) isPgoCompile() bool {
457 if pgo := c.pgo; pgo != nil {
458 return pgo.Properties.PgoCompile
459 }
460 return false
461}
462
Logan Chienf3511742017-10-31 18:04:35 +0800463func (c *Module) isVndkSp() bool {
464 if vndkdep := c.vndkdep; vndkdep != nil {
465 return vndkdep.isVndkSp()
466 }
467 return false
468}
469
470func (c *Module) isVndkExt() bool {
471 if vndkdep := c.vndkdep; vndkdep != nil {
472 return vndkdep.isVndkExt()
473 }
474 return false
475}
476
477func (c *Module) getVndkExtendsModuleName() string {
478 if vndkdep := c.vndkdep; vndkdep != nil {
479 return vndkdep.getVndkExtendsModuleName()
480 }
481 return ""
482}
483
Jiyong Park82e2bf32017-08-16 14:05:54 +0900484// Returns true only when this module is configured to have core and vendor
485// variants.
486func (c *Module) hasVendorVariant() bool {
487 return c.isVndk() || Bool(c.VendorProperties.Vendor_available)
488}
489
Jiyong Parkf9332f12018-02-01 00:54:12 +0900490func (c *Module) inRecovery() bool {
491 return c.Properties.InRecovery || c.ModuleBase.InstallInRecovery()
492}
493
494func (c *Module) onlyInRecovery() bool {
495 return c.ModuleBase.InstallInRecovery()
496}
497
Colin Crossca860ac2016-01-04 14:34:37 -0800498type baseModuleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700499 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800500 moduleContextImpl
501}
502
Colin Cross37047f12016-12-13 17:06:13 -0800503type depsContext struct {
504 android.BottomUpMutatorContext
505 moduleContextImpl
506}
507
Colin Crossca860ac2016-01-04 14:34:37 -0800508type moduleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700509 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800510 moduleContextImpl
511}
512
Jiyong Park2db76922017-11-08 16:03:48 +0900513func (ctx *moduleContext) SocSpecific() bool {
514 return ctx.ModuleContext.SocSpecific() ||
515 (ctx.mod.hasVendorVariant() && ctx.mod.useVndk() && !ctx.mod.isVndk())
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700516}
517
Colin Crossca860ac2016-01-04 14:34:37 -0800518type moduleContextImpl struct {
519 mod *Module
520 ctx BaseModuleContext
521}
522
Colin Crossb98c8b02016-07-29 13:44:28 -0700523func (ctx *moduleContextImpl) toolchain() config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800524 return ctx.mod.toolchain(ctx.ctx)
525}
526
527func (ctx *moduleContextImpl) static() bool {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000528 return ctx.mod.static()
Colin Crossca860ac2016-01-04 14:34:37 -0800529}
530
531func (ctx *moduleContextImpl) staticBinary() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700532 if static, ok := ctx.mod.linker.(interface {
533 staticBinary() bool
534 }); ok {
535 return static.staticBinary()
Colin Crossca860ac2016-01-04 14:34:37 -0800536 }
Colin Crossb916a382016-07-29 17:28:03 -0700537 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800538}
539
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700540func (ctx *moduleContextImpl) useSdk() bool {
Jiyong Parkf9332f12018-02-01 00:54:12 +0900541 if ctx.ctx.Device() && !ctx.useVndk() && !ctx.inRecovery() {
Nan Zhang0007d812017-11-07 10:57:05 -0800542 return String(ctx.mod.Properties.Sdk_version) != ""
Dan Willemsena96ff642016-06-07 12:34:45 -0700543 }
544 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800545}
546
547func (ctx *moduleContextImpl) sdkVersion() string {
Dan Willemsena96ff642016-06-07 12:34:45 -0700548 if ctx.ctx.Device() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700549 if ctx.useVndk() {
Justin Yun732aa6a2018-03-23 17:43:47 +0900550 vndk_ver := ctx.ctx.DeviceConfig().VndkVersion()
Ryan Prichard05206112018-03-26 21:25:27 -0700551 if vndk_ver == "current" {
Justin Yun732aa6a2018-03-23 17:43:47 +0900552 platform_vndk_ver := ctx.ctx.DeviceConfig().PlatformVndkVersion()
553 if inList(platform_vndk_ver, ctx.ctx.Config().PlatformVersionCombinedCodenames()) {
554 return "current"
555 }
556 return platform_vndk_ver
557 }
558 return vndk_ver
Dan Willemsend2ede872016-11-18 14:54:24 -0800559 }
Justin Yun732aa6a2018-03-23 17:43:47 +0900560 return String(ctx.mod.Properties.Sdk_version)
Dan Willemsena96ff642016-06-07 12:34:45 -0700561 }
562 return ""
Colin Crossca860ac2016-01-04 14:34:37 -0800563}
564
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700565func (ctx *moduleContextImpl) useVndk() bool {
566 return ctx.mod.useVndk()
567}
Justin Yun8effde42017-06-23 19:24:43 +0900568
Logan Chienf3511742017-10-31 18:04:35 +0800569func (ctx *moduleContextImpl) isVndk() bool {
570 return ctx.mod.isVndk()
571}
572
Yi Kong7e53c572018-02-14 18:16:12 +0800573func (ctx *moduleContextImpl) isPgoCompile() bool {
574 return ctx.mod.isPgoCompile()
575}
576
Justin Yun8effde42017-06-23 19:24:43 +0900577func (ctx *moduleContextImpl) isVndkSp() bool {
Logan Chienf3511742017-10-31 18:04:35 +0800578 return ctx.mod.isVndkSp()
579}
580
581func (ctx *moduleContextImpl) isVndkExt() bool {
582 return ctx.mod.isVndkExt()
Justin Yun8effde42017-06-23 19:24:43 +0900583}
584
Jiyong Parkf9332f12018-02-01 00:54:12 +0900585func (ctx *moduleContextImpl) inRecovery() bool {
586 return ctx.mod.inRecovery()
587}
588
Logan Chien2f2b8902018-07-10 15:01:19 +0800589// Check whether ABI dumps should be created for this module.
590func (ctx *moduleContextImpl) shouldCreateVndkSourceAbiDump() bool {
591 if ctx.ctx.Config().IsEnvTrue("SKIP_ABI_CHECKS") {
592 return false
Jayant Chowdharyea0a2e12018-03-01 19:12:16 -0800593 }
Logan Chien2f2b8902018-07-10 15:01:19 +0800594 if sanitize := ctx.mod.sanitize; sanitize != nil {
595 if !sanitize.isVariantOnProductionDevice() {
596 return false
597 }
598 }
599 if !ctx.ctx.Device() {
600 // Host modules do not need ABI dumps.
601 return false
602 }
603 if inList(ctx.baseModuleName(), llndkLibraries) {
604 return true
605 }
Logan Chienf4b79c62018-08-02 02:27:02 +0800606 if inList(ctx.baseModuleName(), ndkMigratedLibs) {
607 return true
608 }
Logan Chien2f2b8902018-07-10 15:01:19 +0800609 if ctx.useVndk() && ctx.isVndk() {
610 // Return true if this is VNDK-core, VNDK-SP, or VNDK-Ext and this is not
611 // VNDK-private.
612 return Bool(ctx.mod.VendorProperties.Vendor_available) || ctx.isVndkExt()
613 }
614 return false
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800615}
616
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700617func (ctx *moduleContextImpl) selectedStl() string {
618 if stl := ctx.mod.stl; stl != nil {
619 return stl.Properties.SelectedStl
620 }
621 return ""
622}
623
Colin Crossce75d2c2016-10-06 16:12:58 -0700624func (ctx *moduleContextImpl) baseModuleName() string {
625 return ctx.mod.ModuleBase.BaseModuleName()
626}
627
Logan Chienf3511742017-10-31 18:04:35 +0800628func (ctx *moduleContextImpl) getVndkExtendsModuleName() string {
629 return ctx.mod.getVndkExtendsModuleName()
630}
631
Colin Cross635c3b02016-05-18 15:37:25 -0700632func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800633 return &Module{
634 hod: hod,
635 multilib: multilib,
636 }
637}
638
Colin Cross635c3b02016-05-18 15:37:25 -0700639func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800640 module := newBaseModule(hod, multilib)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700641 module.features = []feature{
642 &tidyFeature{},
643 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700644 module.stl = &stl{}
Colin Cross16b23492016-01-06 14:41:07 -0800645 module.sanitize = &sanitize{}
Dan Willemsen581341d2017-02-09 16:16:31 -0800646 module.coverage = &coverage{}
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800647 module.sabi = &sabi{}
Justin Yun8effde42017-06-23 19:24:43 +0900648 module.vndkdep = &vndkdep{}
Stephen Craneba090d12017-05-09 15:44:35 -0700649 module.lto = &lto{}
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700650 module.pgo = &pgo{}
Colin Crossca860ac2016-01-04 14:34:37 -0800651 return module
652}
653
Colin Crossce75d2c2016-10-06 16:12:58 -0700654func (c *Module) Prebuilt() *android.Prebuilt {
655 if p, ok := c.linker.(prebuiltLinkerInterface); ok {
656 return p.prebuilt()
657 }
658 return nil
659}
660
661func (c *Module) Name() string {
662 name := c.ModuleBase.Name()
Dan Willemsen01a90592017-04-07 15:21:13 -0700663 if p, ok := c.linker.(interface {
664 Name(string) string
665 }); ok {
Colin Crossce75d2c2016-10-06 16:12:58 -0700666 name = p.Name(name)
667 }
668 return name
669}
670
Jeff Gaston294356f2017-09-27 17:05:30 -0700671// orderDeps reorders dependencies into a list such that if module A depends on B, then
672// A will precede B in the resultant list.
673// This is convenient for passing into a linker.
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -0800674// Note that directSharedDeps should be the analogous static library for each shared lib dep
675func 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 -0700676 // If A depends on B, then
677 // Every list containing A will also contain B later in the list
678 // So, after concatenating all lists, the final instance of B will have come from the same
679 // original list as the final instance of A
680 // So, the final instance of B will be later in the concatenation than the final A
681 // So, keeping only the final instance of A and of B ensures that A is earlier in the output
682 // list than B
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -0800683 for _, dep := range directStaticDeps {
Jeff Gaston294356f2017-09-27 17:05:30 -0700684 orderedAllDeps = append(orderedAllDeps, dep)
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -0800685 orderedAllDeps = append(orderedAllDeps, allTransitiveDeps[dep]...)
686 }
687 for _, dep := range directSharedDeps {
688 orderedAllDeps = append(orderedAllDeps, dep)
689 orderedAllDeps = append(orderedAllDeps, allTransitiveDeps[dep]...)
Jeff Gaston294356f2017-09-27 17:05:30 -0700690 }
691
Colin Crossb6715442017-10-24 11:13:31 -0700692 orderedAllDeps = android.LastUniquePaths(orderedAllDeps)
Jeff Gaston294356f2017-09-27 17:05:30 -0700693
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -0800694 // We don't want to add any new dependencies into directStaticDeps (to allow the caller to
Jeff Gaston294356f2017-09-27 17:05:30 -0700695 // intentionally exclude or replace any unwanted transitive dependencies), so we limit the
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -0800696 // resultant list to only what the caller has chosen to include in directStaticDeps
697 _, orderedDeclaredDeps = android.FilterPathList(orderedAllDeps, directStaticDeps)
Jeff Gaston294356f2017-09-27 17:05:30 -0700698
699 return orderedAllDeps, orderedDeclaredDeps
700}
701
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -0800702func orderStaticModuleDeps(module *Module, staticDeps []*Module, sharedDeps []*Module) (results []android.Path) {
703 // convert Module to Path
704 allTransitiveDeps := make(map[android.Path][]android.Path, len(staticDeps))
705 staticDepFiles := []android.Path{}
706 for _, dep := range staticDeps {
707 allTransitiveDeps[dep.outputFile.Path()] = dep.depsInLinkOrder
708 staticDepFiles = append(staticDepFiles, dep.outputFile.Path())
Jeff Gaston294356f2017-09-27 17:05:30 -0700709 }
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -0800710 sharedDepFiles := []android.Path{}
711 for _, sharedDep := range sharedDeps {
712 staticAnalogue := sharedDep.staticVariant
713 if staticAnalogue != nil {
714 allTransitiveDeps[staticAnalogue.outputFile.Path()] = staticAnalogue.depsInLinkOrder
715 sharedDepFiles = append(sharedDepFiles, staticAnalogue.outputFile.Path())
716 }
Jeff Gaston294356f2017-09-27 17:05:30 -0700717 }
718
719 // reorder the dependencies based on transitive dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -0800720 module.depsInLinkOrder, results = orderDeps(staticDepFiles, sharedDepFiles, allTransitiveDeps)
Jeff Gaston294356f2017-09-27 17:05:30 -0700721
722 return results
Jeff Gaston294356f2017-09-27 17:05:30 -0700723}
724
Colin Cross635c3b02016-05-18 15:37:25 -0700725func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800726 ctx := &moduleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700727 ModuleContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800728 moduleContextImpl: moduleContextImpl{
729 mod: c,
730 },
731 }
732 ctx.ctx = ctx
733
Colin Crossf18e1102017-11-16 14:33:08 -0800734 deps := c.depsToPaths(ctx)
735 if ctx.Failed() {
736 return
737 }
738
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700739 if c.Properties.Clang != nil && *c.Properties.Clang == false {
740 ctx.PropertyErrorf("clang", "false (GCC) is no longer supported")
741 }
742
Colin Crossca860ac2016-01-04 14:34:37 -0800743 flags := Flags{
744 Toolchain: c.toolchain(ctx),
Colin Crossca860ac2016-01-04 14:34:37 -0800745 }
Colin Crossca860ac2016-01-04 14:34:37 -0800746 if c.compiler != nil {
Colin Crossf18e1102017-11-16 14:33:08 -0800747 flags = c.compiler.compilerFlags(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -0800748 }
749 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700750 flags = c.linker.linkerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800751 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700752 if c.stl != nil {
753 flags = c.stl.flags(ctx, flags)
754 }
Colin Cross16b23492016-01-06 14:41:07 -0800755 if c.sanitize != nil {
756 flags = c.sanitize.flags(ctx, flags)
757 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800758 if c.coverage != nil {
759 flags = c.coverage.flags(ctx, flags)
760 }
Stephen Craneba090d12017-05-09 15:44:35 -0700761 if c.lto != nil {
762 flags = c.lto.flags(ctx, flags)
763 }
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700764 if c.pgo != nil {
765 flags = c.pgo.flags(ctx, flags)
766 }
Colin Crossca860ac2016-01-04 14:34:37 -0800767 for _, feature := range c.features {
768 flags = feature.flags(ctx, flags)
769 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800770 if ctx.Failed() {
771 return
772 }
773
Colin Crossb98c8b02016-07-29 13:44:28 -0700774 flags.CFlags, _ = filterList(flags.CFlags, config.IllegalFlags)
775 flags.CppFlags, _ = filterList(flags.CppFlags, config.IllegalFlags)
776 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, config.IllegalFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800777
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800778 flags.GlobalFlags = append(flags.GlobalFlags, deps.Flags...)
779 c.flags = flags
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700780 // We need access to all the flags seen by a source file.
781 if c.sabi != nil {
782 flags = c.sabi.flags(ctx, flags)
783 }
Colin Crossca860ac2016-01-04 14:34:37 -0800784 // Optimization to reduce size of build.ninja
785 // Replace the long list of flags for each file with a module-local variable
786 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
787 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
788 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
789 flags.CFlags = []string{"$cflags"}
790 flags.CppFlags = []string{"$cppflags"}
791 flags.AsFlags = []string{"$asflags"}
792
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700793 var objs Objects
Colin Crossca860ac2016-01-04 14:34:37 -0800794 if c.compiler != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700795 objs = c.compiler.compile(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -0800796 if ctx.Failed() {
797 return
798 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800799 }
800
Colin Crossca860ac2016-01-04 14:34:37 -0800801 if c.linker != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700802 outputFile := c.linker.link(ctx, flags, deps, objs)
Colin Crossca860ac2016-01-04 14:34:37 -0800803 if ctx.Failed() {
804 return
805 }
Colin Cross635c3b02016-05-18 15:37:25 -0700806 c.outputFile = android.OptionalPathForPath(outputFile)
Colin Crossce75d2c2016-10-06 16:12:58 -0700807 }
Colin Cross5049f022015-03-18 13:28:46 -0700808
Jiyong Park9d452992018-10-03 00:38:19 +0900809 if c.installer != nil && !c.Properties.PreventInstall && c.IsForPlatform() && c.outputFile.Valid() {
Colin Crossce75d2c2016-10-06 16:12:58 -0700810 c.installer.install(ctx, c.outputFile.Path())
811 if ctx.Failed() {
812 return
Colin Crossca860ac2016-01-04 14:34:37 -0800813 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700814 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800815}
816
Colin Crossb98c8b02016-07-29 13:44:28 -0700817func (c *Module) toolchain(ctx BaseModuleContext) config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800818 if c.cachedToolchain == nil {
Colin Crossb98c8b02016-07-29 13:44:28 -0700819 c.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
Colin Cross3f40fa42015-01-30 17:27:36 -0800820 }
Colin Crossca860ac2016-01-04 14:34:37 -0800821 return c.cachedToolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800822}
823
Colin Crossca860ac2016-01-04 14:34:37 -0800824func (c *Module) begin(ctx BaseModuleContext) {
825 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700826 c.compiler.compilerInit(ctx)
Colin Cross21b9a242015-03-24 14:15:58 -0700827 }
Colin Crossca860ac2016-01-04 14:34:37 -0800828 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700829 c.linker.linkerInit(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800830 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700831 if c.stl != nil {
832 c.stl.begin(ctx)
833 }
Colin Cross16b23492016-01-06 14:41:07 -0800834 if c.sanitize != nil {
835 c.sanitize.begin(ctx)
836 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800837 if c.coverage != nil {
838 c.coverage.begin(ctx)
839 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800840 if c.sabi != nil {
841 c.sabi.begin(ctx)
842 }
Justin Yun8effde42017-06-23 19:24:43 +0900843 if c.vndkdep != nil {
844 c.vndkdep.begin(ctx)
845 }
Stephen Craneba090d12017-05-09 15:44:35 -0700846 if c.lto != nil {
847 c.lto.begin(ctx)
848 }
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700849 if c.pgo != nil {
850 c.pgo.begin(ctx)
851 }
Colin Crossca860ac2016-01-04 14:34:37 -0800852 for _, feature := range c.features {
853 feature.begin(ctx)
854 }
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700855 if ctx.useSdk() {
Dan Albertf5415d72017-08-17 16:19:59 -0700856 version, err := normalizeNdkApiLevel(ctx, ctx.sdkVersion(), ctx.Arch())
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700857 if err != nil {
858 ctx.PropertyErrorf("sdk_version", err.Error())
859 }
Nan Zhang0007d812017-11-07 10:57:05 -0800860 c.Properties.Sdk_version = StringPtr(version)
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700861 }
Colin Crossca860ac2016-01-04 14:34:37 -0800862}
863
Colin Cross37047f12016-12-13 17:06:13 -0800864func (c *Module) deps(ctx DepsContext) Deps {
Colin Crossc99deeb2016-04-11 15:06:20 -0700865 deps := Deps{}
866
867 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700868 deps = c.compiler.compilerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700869 }
Pirama Arumuga Nainar0b882f02018-04-23 22:44:39 +0000870 // Add the PGO dependency (the clang_rt.profile runtime library), which
871 // sometimes depends on symbols from libgcc, before libgcc gets added
872 // in linkerDeps().
Pirama Arumuga Nainar49b53d52017-10-04 16:47:29 -0700873 if c.pgo != nil {
874 deps = c.pgo.deps(ctx, deps)
875 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700876 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700877 deps = c.linker.linkerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700878 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700879 if c.stl != nil {
880 deps = c.stl.deps(ctx, deps)
881 }
Colin Cross16b23492016-01-06 14:41:07 -0800882 if c.sanitize != nil {
883 deps = c.sanitize.deps(ctx, deps)
884 }
Pirama Arumuga Nainar0b882f02018-04-23 22:44:39 +0000885 if c.coverage != nil {
886 deps = c.coverage.deps(ctx, deps)
887 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800888 if c.sabi != nil {
889 deps = c.sabi.deps(ctx, deps)
890 }
Justin Yun8effde42017-06-23 19:24:43 +0900891 if c.vndkdep != nil {
892 deps = c.vndkdep.deps(ctx, deps)
893 }
Stephen Craneba090d12017-05-09 15:44:35 -0700894 if c.lto != nil {
895 deps = c.lto.deps(ctx, deps)
896 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700897 for _, feature := range c.features {
898 deps = feature.deps(ctx, deps)
899 }
900
Colin Crossb6715442017-10-24 11:13:31 -0700901 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
902 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
903 deps.LateStaticLibs = android.LastUniqueStrings(deps.LateStaticLibs)
904 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
905 deps.LateSharedLibs = android.LastUniqueStrings(deps.LateSharedLibs)
906 deps.HeaderLibs = android.LastUniqueStrings(deps.HeaderLibs)
Logan Chien43d34c32017-12-20 01:17:32 +0800907 deps.RuntimeLibs = android.LastUniqueStrings(deps.RuntimeLibs)
Colin Crossc99deeb2016-04-11 15:06:20 -0700908
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700909 for _, lib := range deps.ReexportSharedLibHeaders {
910 if !inList(lib, deps.SharedLibs) {
911 ctx.PropertyErrorf("export_shared_lib_headers", "Shared library not in shared_libs: '%s'", lib)
912 }
913 }
914
915 for _, lib := range deps.ReexportStaticLibHeaders {
916 if !inList(lib, deps.StaticLibs) {
917 ctx.PropertyErrorf("export_static_lib_headers", "Static library not in static_libs: '%s'", lib)
918 }
919 }
920
Colin Cross5950f382016-12-13 12:50:57 -0800921 for _, lib := range deps.ReexportHeaderLibHeaders {
922 if !inList(lib, deps.HeaderLibs) {
923 ctx.PropertyErrorf("export_header_lib_headers", "Header library not in header_libs: '%s'", lib)
924 }
925 }
926
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700927 for _, gen := range deps.ReexportGeneratedHeaders {
928 if !inList(gen, deps.GeneratedHeaders) {
929 ctx.PropertyErrorf("export_generated_headers", "Generated header module not in generated_headers: '%s'", gen)
930 }
931 }
932
Colin Crossc99deeb2016-04-11 15:06:20 -0700933 return deps
934}
935
Dan Albert7e9d2952016-08-04 13:02:36 -0700936func (c *Module) beginMutator(actx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800937 ctx := &baseModuleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700938 BaseContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800939 moduleContextImpl: moduleContextImpl{
940 mod: c,
941 },
942 }
943 ctx.ctx = ctx
944
Colin Crossca860ac2016-01-04 14:34:37 -0800945 c.begin(ctx)
Dan Albert7e9d2952016-08-04 13:02:36 -0700946}
947
Jiyong Park7ed9de32018-10-15 22:25:07 +0900948// Split name#version into name and version
949func stubsLibNameAndVersion(name string) (string, string) {
950 if sharp := strings.LastIndex(name, "#"); sharp != -1 && sharp != len(name)-1 {
951 version := name[sharp+1:]
952 libname := name[:sharp]
953 return libname, version
954 }
955 return name, ""
956}
957
Colin Cross1e676be2016-10-12 14:38:15 -0700958func (c *Module) DepsMutator(actx android.BottomUpMutatorContext) {
Colin Cross37047f12016-12-13 17:06:13 -0800959 ctx := &depsContext{
960 BottomUpMutatorContext: actx,
Dan Albert7e9d2952016-08-04 13:02:36 -0700961 moduleContextImpl: moduleContextImpl{
962 mod: c,
963 },
964 }
965 ctx.ctx = ctx
Colin Crossca860ac2016-01-04 14:34:37 -0800966
Colin Crossc99deeb2016-04-11 15:06:20 -0700967 deps := c.deps(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800968
Dan Albert914449f2016-06-17 16:45:24 -0700969 variantNdkLibs := []string{}
970 variantLateNdkLibs := []string{}
Dan Willemsenb916b802017-03-19 13:44:32 -0700971 if ctx.Os() == android.Android {
Dan Albert914449f2016-06-17 16:45:24 -0700972 version := ctx.sdkVersion()
Dan Willemsen72d39932016-07-08 23:23:48 -0700973
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700974 // rewriteNdkLibs takes a list of names of shared libraries and scans it for three types
975 // of names:
Dan Albert914449f2016-06-17 16:45:24 -0700976 //
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700977 // 1. Name of an NDK library that refers to a prebuilt module.
978 // For each of these, it adds the name of the prebuilt module (which will be in
979 // prebuilts/ndk) to the list of nonvariant libs.
980 // 2. Name of an NDK library that refers to an ndk_library module.
981 // For each of these, it adds the name of the ndk_library module to the list of
982 // variant libs.
983 // 3. Anything else (so anything that isn't an NDK library).
984 // It adds these to the nonvariantLibs list.
Dan Albert914449f2016-06-17 16:45:24 -0700985 //
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700986 // The caller can then know to add the variantLibs dependencies differently from the
987 // nonvariantLibs
988 rewriteNdkLibs := func(list []string) (nonvariantLibs []string, variantLibs []string) {
989 variantLibs = []string{}
990 nonvariantLibs = []string{}
Dan Albert914449f2016-06-17 16:45:24 -0700991 for _, entry := range list {
Jiyong Park7ed9de32018-10-15 22:25:07 +0900992 // strip #version suffix out
993 name, _ := stubsLibNameAndVersion(entry)
994 if ctx.useSdk() && inList(name, ndkPrebuiltSharedLibraries) {
995 if !inList(name, ndkMigratedLibs) {
996 nonvariantLibs = append(nonvariantLibs, name+".ndk."+version)
Dan Albert914449f2016-06-17 16:45:24 -0700997 } else {
Jiyong Park7ed9de32018-10-15 22:25:07 +0900998 variantLibs = append(variantLibs, name+ndkLibrarySuffix)
Dan Albert914449f2016-06-17 16:45:24 -0700999 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09001000 } else if ctx.useVndk() && inList(name, llndkLibraries) {
1001 nonvariantLibs = append(nonvariantLibs, name+llndkLibrarySuffix)
1002 } else if (ctx.Platform() || ctx.ProductSpecific()) && inList(name, vendorPublicLibraries) {
1003 vendorPublicLib := name + vendorPublicLibrarySuffix
Jiyong Park374510b2018-03-19 18:23:01 +09001004 if actx.OtherModuleExists(vendorPublicLib) {
1005 nonvariantLibs = append(nonvariantLibs, vendorPublicLib)
1006 } else {
1007 // This can happen if vendor_public_library module is defined in a
1008 // namespace that isn't visible to the current module. In that case,
1009 // link to the original library.
Jiyong Park7ed9de32018-10-15 22:25:07 +09001010 nonvariantLibs = append(nonvariantLibs, name)
Jiyong Park374510b2018-03-19 18:23:01 +09001011 }
Dan Albert914449f2016-06-17 16:45:24 -07001012 } else {
Jiyong Park7ed9de32018-10-15 22:25:07 +09001013 // put name#version back
Dan Willemsen7cbf5f82017-03-28 00:08:30 -07001014 nonvariantLibs = append(nonvariantLibs, entry)
Dan Willemsen72d39932016-07-08 23:23:48 -07001015 }
1016 }
Dan Albert914449f2016-06-17 16:45:24 -07001017 return nonvariantLibs, variantLibs
Dan Willemsen72d39932016-07-08 23:23:48 -07001018 }
1019
Dan Albert914449f2016-06-17 16:45:24 -07001020 deps.SharedLibs, variantNdkLibs = rewriteNdkLibs(deps.SharedLibs)
1021 deps.LateSharedLibs, variantLateNdkLibs = rewriteNdkLibs(deps.LateSharedLibs)
Jiyong Park4c35af02017-07-05 13:41:55 +09001022 deps.ReexportSharedLibHeaders, _ = rewriteNdkLibs(deps.ReexportSharedLibHeaders)
Dan Willemsen72d39932016-07-08 23:23:48 -07001023 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001024
Jiyong Park7ed9de32018-10-15 22:25:07 +09001025 if c.linker != nil {
1026 if library, ok := c.linker.(*libraryDecorator); ok {
1027 if library.buildStubs() {
1028 // Stubs lib does not have dependency to other libraries. Don't proceed.
1029 return
1030 }
1031 }
1032 }
1033
Colin Cross32ec36c2016-12-15 07:39:51 -08001034 for _, lib := range deps.HeaderLibs {
1035 depTag := headerDepTag
1036 if inList(lib, deps.ReexportHeaderLibHeaders) {
1037 depTag = headerExportDepTag
1038 }
1039 actx.AddVariationDependencies(nil, depTag, lib)
1040 }
Colin Cross5950f382016-12-13 12:50:57 -08001041
Dan Willemsen59339a22018-07-22 21:18:45 -07001042 actx.AddVariationDependencies([]blueprint.Variation{
1043 {Mutator: "link", Variation: "static"},
1044 }, wholeStaticDepTag, deps.WholeStaticLibs...)
Colin Crossc99deeb2016-04-11 15:06:20 -07001045
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001046 for _, lib := range deps.StaticLibs {
1047 depTag := staticDepTag
1048 if inList(lib, deps.ReexportStaticLibHeaders) {
1049 depTag = staticExportDepTag
1050 }
Dan Willemsen59339a22018-07-22 21:18:45 -07001051 actx.AddVariationDependencies([]blueprint.Variation{
1052 {Mutator: "link", Variation: "static"},
1053 }, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001054 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001055
Dan Willemsen59339a22018-07-22 21:18:45 -07001056 actx.AddVariationDependencies([]blueprint.Variation{
1057 {Mutator: "link", Variation: "static"},
1058 }, lateStaticDepTag, deps.LateStaticLibs...)
Colin Crossc99deeb2016-04-11 15:06:20 -07001059
Jiyong Park7ed9de32018-10-15 22:25:07 +09001060 // shared lib names without the #version suffix
1061 var sharedLibNames []string
1062
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001063 for _, lib := range deps.SharedLibs {
Jiyong Park7ed9de32018-10-15 22:25:07 +09001064 name, version := stubsLibNameAndVersion(lib)
1065 sharedLibNames = append(sharedLibNames, name)
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001066 depTag := sharedDepTag
1067 if inList(lib, deps.ReexportSharedLibHeaders) {
1068 depTag = sharedExportDepTag
1069 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09001070 var variations []blueprint.Variation
1071 variations = append(variations, blueprint.Variation{Mutator: "link", Variation: "shared"})
1072 if version != "" && ctx.Os() == android.Android && !ctx.useVndk() && !c.inRecovery() {
1073 variations = append(variations, blueprint.Variation{Mutator: "version", Variation: version})
1074 }
1075 actx.AddVariationDependencies(variations, depTag, name)
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001076 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001077
Jiyong Park7ed9de32018-10-15 22:25:07 +09001078 for _, lib := range deps.LateSharedLibs {
1079 name, version := stubsLibNameAndVersion(lib)
1080 if inList(name, sharedLibNames) {
1081 // This is to handle the case that some of the late shared libs (libc, libdl, libm, ...)
1082 // are added also to SharedLibs with version (e.g., libc#10). If not skipped, we will be
1083 // linking against both the stubs lib and the non-stubs lib at the same time.
1084 continue
1085 }
1086 depTag := lateSharedDepTag
1087 var variations []blueprint.Variation
1088 variations = append(variations, blueprint.Variation{Mutator: "link", Variation: "shared"})
1089 if version != "" && ctx.Os() == android.Android && !ctx.useVndk() && !c.inRecovery() {
1090 variations = append(variations, blueprint.Variation{Mutator: "version", Variation: version})
1091 }
1092 actx.AddVariationDependencies(variations, depTag, name)
1093 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001094
Dan Willemsen59339a22018-07-22 21:18:45 -07001095 actx.AddVariationDependencies([]blueprint.Variation{
1096 {Mutator: "link", Variation: "shared"},
1097 }, runtimeDepTag, deps.RuntimeLibs...)
Logan Chien43d34c32017-12-20 01:17:32 +08001098
Colin Cross68861832016-07-08 10:41:41 -07001099 actx.AddDependency(c, genSourceDepTag, deps.GeneratedSources...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -07001100
1101 for _, gen := range deps.GeneratedHeaders {
1102 depTag := genHeaderDepTag
1103 if inList(gen, deps.ReexportGeneratedHeaders) {
1104 depTag = genHeaderExportDepTag
1105 }
1106 actx.AddDependency(c, depTag, gen)
1107 }
Dan Willemsenb40aab62016-04-20 14:21:14 -07001108
Colin Cross42d48b72018-08-29 14:10:52 -07001109 actx.AddVariationDependencies(nil, objDepTag, deps.ObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -07001110
1111 if deps.CrtBegin != "" {
Colin Cross42d48b72018-08-29 14:10:52 -07001112 actx.AddVariationDependencies(nil, crtBeginDepTag, deps.CrtBegin)
Colin Crossca860ac2016-01-04 14:34:37 -08001113 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001114 if deps.CrtEnd != "" {
Colin Cross42d48b72018-08-29 14:10:52 -07001115 actx.AddVariationDependencies(nil, crtEndDepTag, deps.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -07001116 }
Dan Willemsena0790e32018-10-12 00:24:23 -07001117 if deps.LinkerFlagsFile != "" {
1118 actx.AddDependency(c, linkerFlagsDepTag, deps.LinkerFlagsFile)
1119 }
1120 if deps.DynamicLinker != "" {
1121 actx.AddDependency(c, dynamicLinkerDepTag, deps.DynamicLinker)
Dan Willemsenc77a0b32017-09-18 23:19:12 -07001122 }
Dan Albert914449f2016-06-17 16:45:24 -07001123
1124 version := ctx.sdkVersion()
1125 actx.AddVariationDependencies([]blueprint.Variation{
Dan Willemsen59339a22018-07-22 21:18:45 -07001126 {Mutator: "ndk_api", Variation: version},
1127 {Mutator: "link", Variation: "shared"},
1128 }, ndkStubDepTag, variantNdkLibs...)
Dan Albert914449f2016-06-17 16:45:24 -07001129 actx.AddVariationDependencies([]blueprint.Variation{
Dan Willemsen59339a22018-07-22 21:18:45 -07001130 {Mutator: "ndk_api", Variation: version},
1131 {Mutator: "link", Variation: "shared"},
1132 }, ndkLateStubDepTag, variantLateNdkLibs...)
Logan Chienf3511742017-10-31 18:04:35 +08001133
1134 if vndkdep := c.vndkdep; vndkdep != nil {
1135 if vndkdep.isVndkExt() {
1136 baseModuleMode := vendorMode
1137 if actx.DeviceConfig().VndkVersion() == "" {
1138 baseModuleMode = coreMode
1139 }
1140 actx.AddVariationDependencies([]blueprint.Variation{
Dan Willemsen59339a22018-07-22 21:18:45 -07001141 {Mutator: "image", Variation: baseModuleMode},
1142 {Mutator: "link", Variation: "shared"},
1143 }, vndkExtDepTag, vndkdep.getVndkExtendsModuleName())
Logan Chienf3511742017-10-31 18:04:35 +08001144 }
1145 }
Colin Cross6362e272015-10-29 15:25:03 -07001146}
Colin Cross21b9a242015-03-24 14:15:58 -07001147
Colin Crosse40b4ea2018-10-02 22:25:58 -07001148func BeginMutator(ctx android.BottomUpMutatorContext) {
Dan Albert7e9d2952016-08-04 13:02:36 -07001149 if c, ok := ctx.Module().(*Module); ok && c.Enabled() {
1150 c.beginMutator(ctx)
1151 }
1152}
1153
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001154// Whether a module can link to another module, taking into
1155// account NDK linking.
Logan Chienf3511742017-10-31 18:04:35 +08001156func checkLinkType(ctx android.ModuleContext, from *Module, to *Module, tag dependencyTag) {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001157 if from.Target().Os != android.Android {
1158 // Host code is not restricted
1159 return
1160 }
1161 if from.Properties.UseVndk {
1162 // Though vendor code is limited by the vendor mutator,
1163 // each vendor-available module needs to check
1164 // link-type for VNDK.
1165 if from.vndkdep != nil {
Logan Chienf3511742017-10-31 18:04:35 +08001166 from.vndkdep.vndkCheckLinkType(ctx, to, tag)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001167 }
1168 return
1169 }
Nan Zhang0007d812017-11-07 10:57:05 -08001170 if String(from.Properties.Sdk_version) == "" {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001171 // Platform code can link to anything
1172 return
1173 }
Jiyong Parkf9332f12018-02-01 00:54:12 +09001174 if from.inRecovery() {
1175 // Recovery code is not NDK
1176 return
1177 }
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001178 if _, ok := to.linker.(*toolchainLibraryDecorator); ok {
1179 // These are always allowed
1180 return
1181 }
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001182 if _, ok := to.linker.(*ndkPrebuiltStlLinker); ok {
1183 // These are allowed, but they don't set sdk_version
1184 return
1185 }
1186 if _, ok := to.linker.(*stubDecorator); ok {
1187 // These aren't real libraries, but are the stub shared libraries that are included in
1188 // the NDK.
1189 return
1190 }
Nan Zhang0007d812017-11-07 10:57:05 -08001191 if String(to.Properties.Sdk_version) == "" {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001192 // NDK code linking to platform code is never okay.
1193 ctx.ModuleErrorf("depends on non-NDK-built library %q",
1194 ctx.OtherModuleName(to))
1195 }
1196
1197 // At this point we know we have two NDK libraries, but we need to
1198 // check that we're not linking against anything built against a higher
1199 // API level, as it is only valid to link against older or equivalent
1200 // APIs.
1201
Inseob Kim01a28722018-04-11 09:48:45 +09001202 // Current can link against anything.
1203 if String(from.Properties.Sdk_version) != "current" {
1204 // Otherwise we need to check.
1205 if String(to.Properties.Sdk_version) == "current" {
1206 // Current can't be linked against by anything else.
1207 ctx.ModuleErrorf("links %q built against newer API version %q",
1208 ctx.OtherModuleName(to), "current")
1209 } else {
1210 fromApi, err := strconv.Atoi(String(from.Properties.Sdk_version))
1211 if err != nil {
1212 ctx.PropertyErrorf("sdk_version",
Inseob Kim34b22832018-04-11 10:13:16 +09001213 "Invalid sdk_version value (must be int or current): %q",
Inseob Kim01a28722018-04-11 09:48:45 +09001214 String(from.Properties.Sdk_version))
1215 }
1216 toApi, err := strconv.Atoi(String(to.Properties.Sdk_version))
1217 if err != nil {
1218 ctx.PropertyErrorf("sdk_version",
Inseob Kim34b22832018-04-11 10:13:16 +09001219 "Invalid sdk_version value (must be int or current): %q",
Inseob Kim01a28722018-04-11 09:48:45 +09001220 String(to.Properties.Sdk_version))
1221 }
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001222
Inseob Kim01a28722018-04-11 09:48:45 +09001223 if toApi > fromApi {
1224 ctx.ModuleErrorf("links %q built against newer API version %q",
1225 ctx.OtherModuleName(to), String(to.Properties.Sdk_version))
1226 }
1227 }
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001228 }
Dan Albert202fe492017-12-15 13:56:59 -08001229
1230 // Also check that the two STL choices are compatible.
1231 fromStl := from.stl.Properties.SelectedStl
1232 toStl := to.stl.Properties.SelectedStl
1233 if fromStl == "" || toStl == "" {
1234 // Libraries that don't use the STL are unrestricted.
Inseob Kimda2171a2018-04-11 15:41:38 +09001235 } else if fromStl == "ndk_system" || toStl == "ndk_system" {
Dan Albert202fe492017-12-15 13:56:59 -08001236 // We can be permissive with the system "STL" since it is only the C++
1237 // ABI layer, but in the future we should make sure that everyone is
1238 // using either libc++ or nothing.
Colin Crossb60190a2018-09-04 16:28:17 -07001239 } else if getNdkStlFamily(from) != getNdkStlFamily(to) {
Dan Albert202fe492017-12-15 13:56:59 -08001240 ctx.ModuleErrorf("uses %q and depends on %q which uses incompatible %q",
1241 from.stl.Properties.SelectedStl, ctx.OtherModuleName(to),
1242 to.stl.Properties.SelectedStl)
1243 }
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001244}
1245
Jiyong Park5fb8c102018-04-09 12:03:06 +09001246// Tests whether the dependent library is okay to be double loaded inside a single process.
1247// If a library is a member of VNDK and at the same time dependencies of an LLNDK library,
1248// it is subject to be double loaded. Such lib should be explicitly marked as double_loaded: true
1249// or as vndk-sp (vndk: { enabled: true, support_system_process: true}).
1250func checkDoubleLoadableLibries(ctx android.ModuleContext, from *Module, to *Module) {
1251 if _, ok := from.linker.(*libraryDecorator); !ok {
1252 return
1253 }
1254
1255 if inList(ctx.ModuleName(), llndkLibraries) ||
1256 (from.useVndk() && Bool(from.VendorProperties.Double_loadable)) {
1257 _, depIsLlndk := to.linker.(*llndkStubDecorator)
1258 depIsVndkSp := false
1259 if to.vndkdep != nil && to.vndkdep.isVndkSp() {
1260 depIsVndkSp = true
1261 }
1262 depIsVndk := false
1263 if to.vndkdep != nil && to.vndkdep.isVndk() {
1264 depIsVndk = true
1265 }
1266 depIsDoubleLoadable := Bool(to.VendorProperties.Double_loadable)
1267 if !depIsLlndk && !depIsVndkSp && !depIsDoubleLoadable && depIsVndk {
1268 ctx.ModuleErrorf("links VNDK library %q that isn't double_loadable.",
1269 ctx.OtherModuleName(to))
1270 }
1271 }
1272}
1273
Colin Crossc99deeb2016-04-11 15:06:20 -07001274// Convert dependencies to paths. Returns a PathDeps containing paths
Colin Cross635c3b02016-05-18 15:37:25 -07001275func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
Colin Crossca860ac2016-01-04 14:34:37 -08001276 var depPaths PathDeps
Colin Crossca860ac2016-01-04 14:34:37 -08001277
Jeff Gaston294356f2017-09-27 17:05:30 -07001278 directStaticDeps := []*Module{}
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001279 directSharedDeps := []*Module{}
Jeff Gaston294356f2017-09-27 17:05:30 -07001280
Colin Crossd11fcda2017-10-23 17:59:01 -07001281 ctx.VisitDirectDeps(func(dep android.Module) {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001282 depName := ctx.OtherModuleName(dep)
1283 depTag := ctx.OtherModuleDependencyTag(dep)
Dan Albert9e10cd42016-08-03 14:12:14 -07001284
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001285 ccDep, _ := dep.(*Module)
1286 if ccDep == nil {
1287 // handling for a few module types that aren't cc Module but that are also supported
1288 switch depTag {
Dan Willemsenb40aab62016-04-20 14:21:14 -07001289 case genSourceDepTag:
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001290 if genRule, ok := dep.(genrule.SourceFileGenerator); ok {
Dan Willemsenb40aab62016-04-20 14:21:14 -07001291 depPaths.GeneratedSources = append(depPaths.GeneratedSources,
1292 genRule.GeneratedSourceFiles()...)
1293 } else {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001294 ctx.ModuleErrorf("module %q is not a gensrcs or genrule", depName)
Dan Willemsenb40aab62016-04-20 14:21:14 -07001295 }
Colin Crosse90bfd12017-04-26 16:59:26 -07001296 // Support exported headers from a generated_sources dependency
1297 fallthrough
Dan Willemsenb3454ab2016-09-28 17:34:58 -07001298 case genHeaderDepTag, genHeaderExportDepTag:
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001299 if genRule, ok := dep.(genrule.SourceFileGenerator); ok {
Dan Willemsenb40aab62016-04-20 14:21:14 -07001300 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders,
Dan Willemsen9da9d492018-02-21 18:28:18 -08001301 genRule.GeneratedDeps()...)
Colin Cross5ed99c62016-11-22 12:55:55 -08001302 flags := includeDirsToFlags(genRule.GeneratedHeaderDirs())
Dan Willemsenb3454ab2016-09-28 17:34:58 -07001303 depPaths.Flags = append(depPaths.Flags, flags)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001304 if depTag == genHeaderExportDepTag {
Dan Willemsenb3454ab2016-09-28 17:34:58 -07001305 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags)
Dan Willemsen847dcc72016-09-29 12:13:36 -07001306 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps,
Dan Willemsen9da9d492018-02-21 18:28:18 -08001307 genRule.GeneratedDeps()...)
Jayant Chowdhary715cac32017-04-20 06:53:59 -07001308 // Add these re-exported flags to help header-abi-dumper to infer the abi exported by a library.
1309 c.sabi.Properties.ReexportedIncludeFlags = append(c.sabi.Properties.ReexportedIncludeFlags, flags)
1310
Dan Willemsenb3454ab2016-09-28 17:34:58 -07001311 }
Dan Willemsenb40aab62016-04-20 14:21:14 -07001312 } else {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001313 ctx.ModuleErrorf("module %q is not a genrule", depName)
Dan Willemsenb40aab62016-04-20 14:21:14 -07001314 }
Dan Willemsena0790e32018-10-12 00:24:23 -07001315 case linkerFlagsDepTag:
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001316 if genRule, ok := dep.(genrule.SourceFileGenerator); ok {
Dan Willemsenc77a0b32017-09-18 23:19:12 -07001317 files := genRule.GeneratedSourceFiles()
1318 if len(files) == 1 {
Dan Willemsena0790e32018-10-12 00:24:23 -07001319 depPaths.LinkerFlagsFile = android.OptionalPathForPath(files[0])
Dan Willemsenc77a0b32017-09-18 23:19:12 -07001320 } else if len(files) > 1 {
Dan Willemsena0790e32018-10-12 00:24:23 -07001321 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 -07001322 }
1323 } else {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001324 ctx.ModuleErrorf("module %q is not a genrule", depName)
Dan Willemsenc77a0b32017-09-18 23:19:12 -07001325 }
Colin Crossca860ac2016-01-04 14:34:37 -08001326 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001327 return
1328 }
1329
Colin Crossd11fcda2017-10-23 17:59:01 -07001330 if dep.Target().Os != ctx.Os() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001331 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1332 return
1333 }
Colin Crossd11fcda2017-10-23 17:59:01 -07001334 if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001335 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
Colin Crossa1ad8d12016-06-01 17:09:44 -07001336 return
1337 }
1338
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001339 // re-exporting flags
1340 if depTag == reuseObjTag {
1341 if l, ok := ccDep.compiler.(libraryInterface); ok {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001342 c.staticVariant = ccDep
Colin Crossbbc9f4d2017-05-03 16:24:55 -07001343 objs, flags, deps := l.reuseObjs()
Colin Cross10d22312017-05-03 11:01:58 -07001344 depPaths.Objs = depPaths.Objs.Append(objs)
1345 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Colin Crossbbc9f4d2017-05-03 16:24:55 -07001346 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps, deps...)
Colin Crossbba99042016-11-23 15:45:05 -08001347 return
1348 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001349 }
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001350 if t, ok := depTag.(dependencyTag); ok && t.library {
1351 if i, ok := ccDep.linker.(exportedFlagsProducer); ok {
Dan Willemsen76f08272016-07-09 00:14:08 -07001352 flags := i.exportedFlags()
Dan Willemsen847dcc72016-09-29 12:13:36 -07001353 deps := i.exportedFlagsDeps()
Dan Willemsen76f08272016-07-09 00:14:08 -07001354 depPaths.Flags = append(depPaths.Flags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -07001355 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders, deps...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001356
1357 if t.reexportFlags {
Dan Willemsen76f08272016-07-09 00:14:08 -07001358 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -07001359 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps, deps...)
Jayant Chowdhary715cac32017-04-20 06:53:59 -07001360 // 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 -07001361 // Re-exported shared library headers must be included as well since they can help us with type information
1362 // about template instantiations (instantiated from their headers).
1363 c.sabi.Properties.ReexportedIncludeFlags = append(c.sabi.Properties.ReexportedIncludeFlags, flags...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001364 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001365 }
Dan Willemsena96ff642016-06-07 12:34:45 -07001366
Logan Chienf3511742017-10-31 18:04:35 +08001367 checkLinkType(ctx, c, ccDep, t)
Jiyong Park5fb8c102018-04-09 12:03:06 +09001368 checkDoubleLoadableLibries(ctx, c, ccDep)
Colin Crossc99deeb2016-04-11 15:06:20 -07001369 }
1370
Colin Cross26c34ed2016-09-30 17:10:16 -07001371 var ptr *android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -07001372 var depPtr *android.Paths
Colin Crossc99deeb2016-04-11 15:06:20 -07001373
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001374 linkFile := ccDep.outputFile
Colin Cross26c34ed2016-09-30 17:10:16 -07001375 depFile := android.OptionalPath{}
1376
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001377 switch depTag {
Dan Albert914449f2016-06-17 16:45:24 -07001378 case ndkStubDepTag, sharedDepTag, sharedExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001379 ptr = &depPaths.SharedLibs
1380 depPtr = &depPaths.SharedLibsDeps
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001381 depFile = ccDep.linker.(libraryInterface).toc()
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001382 directSharedDeps = append(directSharedDeps, ccDep)
Dan Albert914449f2016-06-17 16:45:24 -07001383 case lateSharedDepTag, ndkLateStubDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001384 ptr = &depPaths.LateSharedLibs
1385 depPtr = &depPaths.LateSharedLibsDeps
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001386 depFile = ccDep.linker.(libraryInterface).toc()
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001387 case staticDepTag, staticExportDepTag:
Jeff Gaston294356f2017-09-27 17:05:30 -07001388 ptr = nil
1389 directStaticDeps = append(directStaticDeps, ccDep)
Colin Crossc99deeb2016-04-11 15:06:20 -07001390 case lateStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001391 ptr = &depPaths.LateStaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -07001392 case wholeStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001393 ptr = &depPaths.WholeStaticLibs
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001394 staticLib, ok := ccDep.linker.(libraryInterface)
Colin Crossb916a382016-07-29 17:28:03 -07001395 if !ok || !staticLib.static() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001396 ctx.ModuleErrorf("module %q not a static library", depName)
Colin Crossc99deeb2016-04-11 15:06:20 -07001397 return
1398 }
1399
1400 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001401 postfix := " (required by " + ctx.OtherModuleName(dep) + ")"
Colin Crossc99deeb2016-04-11 15:06:20 -07001402 for i := range missingDeps {
1403 missingDeps[i] += postfix
1404 }
1405 ctx.AddMissingDependencies(missingDeps)
1406 }
Dan Willemsen5cb580f2016-09-26 17:33:01 -07001407 depPaths.WholeStaticLibObjs = depPaths.WholeStaticLibObjs.Append(staticLib.objs())
Colin Cross5950f382016-12-13 12:50:57 -08001408 case headerDepTag:
1409 // Nothing
Colin Crossc99deeb2016-04-11 15:06:20 -07001410 case objDepTag:
Dan Willemsen5cb580f2016-09-26 17:33:01 -07001411 depPaths.Objs.objFiles = append(depPaths.Objs.objFiles, linkFile.Path())
Colin Crossc99deeb2016-04-11 15:06:20 -07001412 case crtBeginDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001413 depPaths.CrtBegin = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -07001414 case crtEndDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001415 depPaths.CrtEnd = linkFile
Dan Willemsena0790e32018-10-12 00:24:23 -07001416 case dynamicLinkerDepTag:
1417 depPaths.DynamicLinker = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -07001418 }
1419
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001420 switch depTag {
Dan Willemsen581341d2017-02-09 16:16:31 -08001421 case staticDepTag, staticExportDepTag, lateStaticDepTag:
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001422 staticLib, ok := ccDep.linker.(libraryInterface)
Dan Willemsen581341d2017-02-09 16:16:31 -08001423 if !ok || !staticLib.static() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001424 ctx.ModuleErrorf("module %q not a static library", depName)
Dan Willemsen581341d2017-02-09 16:16:31 -08001425 return
1426 }
1427
1428 // When combining coverage files for shared libraries and executables, coverage files
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001429 // in static libraries act as if they were whole static libraries. The same goes for
1430 // source based Abi dump files.
Dan Willemsen581341d2017-02-09 16:16:31 -08001431 depPaths.StaticLibObjs.coverageFiles = append(depPaths.StaticLibObjs.coverageFiles,
1432 staticLib.objs().coverageFiles...)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001433 depPaths.StaticLibObjs.sAbiDumpFiles = append(depPaths.StaticLibObjs.sAbiDumpFiles,
1434 staticLib.objs().sAbiDumpFiles...)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001435
Dan Willemsen581341d2017-02-09 16:16:31 -08001436 }
1437
Colin Cross26c34ed2016-09-30 17:10:16 -07001438 if ptr != nil {
Colin Crossce75d2c2016-10-06 16:12:58 -07001439 if !linkFile.Valid() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001440 ctx.ModuleErrorf("module %q missing output file", depName)
Colin Crossce75d2c2016-10-06 16:12:58 -07001441 return
1442 }
Colin Cross26c34ed2016-09-30 17:10:16 -07001443 *ptr = append(*ptr, linkFile.Path())
1444 }
1445
Colin Crossc99deeb2016-04-11 15:06:20 -07001446 if depPtr != nil {
Colin Cross26c34ed2016-09-30 17:10:16 -07001447 dep := depFile
1448 if !dep.Valid() {
1449 dep = linkFile
1450 }
1451 *depPtr = append(*depPtr, dep.Path())
Colin Crossca860ac2016-01-04 14:34:37 -08001452 }
Jiyong Park27b188b2017-07-18 13:23:39 +09001453
Logan Chien43d34c32017-12-20 01:17:32 +08001454 makeLibName := func(depName string) string {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001455 libName := strings.TrimSuffix(depName, llndkLibrarySuffix)
Jiyong Park374510b2018-03-19 18:23:01 +09001456 libName = strings.TrimSuffix(libName, vendorPublicLibrarySuffix)
Jiyong Park27b188b2017-07-18 13:23:39 +09001457 libName = strings.TrimPrefix(libName, "prebuilt_")
Jiyong Parkd5b18a52017-08-03 21:22:50 +09001458 isLLndk := inList(libName, llndkLibraries)
Jiyong Park374510b2018-03-19 18:23:01 +09001459 isVendorPublicLib := inList(libName, vendorPublicLibraries)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001460 bothVendorAndCoreVariantsExist := ccDep.hasVendorVariant() || isLLndk
1461 if c.useVndk() && bothVendorAndCoreVariantsExist {
1462 // The vendor module in Make will have been renamed to not conflict with the core
1463 // module, so update the dependency name here accordingly.
Logan Chien43d34c32017-12-20 01:17:32 +08001464 return libName + vendorSuffix
Jiyong Park374510b2018-03-19 18:23:01 +09001465 } else if (ctx.Platform() || ctx.ProductSpecific()) && isVendorPublicLib {
Logan Chien43d34c32017-12-20 01:17:32 +08001466 return libName + vendorPublicLibrarySuffix
Jiyong Parkf9332f12018-02-01 00:54:12 +09001467 } else if ccDep.inRecovery() && !ccDep.onlyInRecovery() {
1468 return libName + recoverySuffix
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001469 } else {
Logan Chien43d34c32017-12-20 01:17:32 +08001470 return libName
Jiyong Park27b188b2017-07-18 13:23:39 +09001471 }
Logan Chien43d34c32017-12-20 01:17:32 +08001472 }
1473
1474 // Export the shared libs to Make.
1475 switch depTag {
1476 case sharedDepTag, sharedExportDepTag, lateSharedDepTag:
Jiyong Park27b188b2017-07-18 13:23:39 +09001477 // Note: the order of libs in this list is not important because
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001478 // they merely serve as Make dependencies and do not affect this lib itself.
Logan Chien43d34c32017-12-20 01:17:32 +08001479 c.Properties.AndroidMkSharedLibs = append(
1480 c.Properties.AndroidMkSharedLibs, makeLibName(depName))
1481 case runtimeDepTag:
1482 c.Properties.AndroidMkRuntimeLibs = append(
1483 c.Properties.AndroidMkRuntimeLibs, makeLibName(depName))
Jiyong Park27b188b2017-07-18 13:23:39 +09001484 }
Colin Crossca860ac2016-01-04 14:34:37 -08001485 })
1486
Jeff Gaston294356f2017-09-27 17:05:30 -07001487 // use the ordered dependencies as this module's dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001488 depPaths.StaticLibs = append(depPaths.StaticLibs, orderStaticModuleDeps(c, directStaticDeps, directSharedDeps)...)
Jeff Gaston294356f2017-09-27 17:05:30 -07001489
Colin Crossdd84e052017-05-17 13:44:16 -07001490 // Dedup exported flags from dependencies
Colin Crossb6715442017-10-24 11:13:31 -07001491 depPaths.Flags = android.FirstUniqueStrings(depPaths.Flags)
Dan Willemsenfe92c962017-08-29 12:28:37 -07001492 depPaths.GeneratedHeaders = android.FirstUniquePaths(depPaths.GeneratedHeaders)
Colin Crossb6715442017-10-24 11:13:31 -07001493 depPaths.ReexportedFlags = android.FirstUniqueStrings(depPaths.ReexportedFlags)
Dan Willemsenfe92c962017-08-29 12:28:37 -07001494 depPaths.ReexportedFlagsDeps = android.FirstUniquePaths(depPaths.ReexportedFlagsDeps)
1495
1496 if c.sabi != nil {
Colin Crossb6715442017-10-24 11:13:31 -07001497 c.sabi.Properties.ReexportedIncludeFlags = android.FirstUniqueStrings(c.sabi.Properties.ReexportedIncludeFlags)
Dan Willemsenfe92c962017-08-29 12:28:37 -07001498 }
Colin Crossdd84e052017-05-17 13:44:16 -07001499
Colin Crossca860ac2016-01-04 14:34:37 -08001500 return depPaths
1501}
1502
1503func (c *Module) InstallInData() bool {
1504 if c.installer == nil {
1505 return false
1506 }
Vishwath Mohan1dd88392017-03-29 22:00:18 -07001507 return c.installer.inData()
1508}
1509
1510func (c *Module) InstallInSanitizerDir() bool {
1511 if c.installer == nil {
1512 return false
1513 }
1514 if c.sanitize != nil && c.sanitize.inSanitizerDir() {
Colin Cross94610402016-08-29 13:41:32 -07001515 return true
1516 }
Vishwath Mohan1dd88392017-03-29 22:00:18 -07001517 return c.installer.inSanitizerDir()
Colin Crossca860ac2016-01-04 14:34:37 -08001518}
1519
Jiyong Parkf9332f12018-02-01 00:54:12 +09001520func (c *Module) InstallInRecovery() bool {
1521 return c.inRecovery()
1522}
1523
Dan Willemsen4aa75ca2016-09-28 16:18:03 -07001524func (c *Module) HostToolPath() android.OptionalPath {
1525 if c.installer == nil {
1526 return android.OptionalPath{}
1527 }
1528 return c.installer.hostToolPath()
1529}
1530
Nan Zhangd4e641b2017-07-12 12:55:28 -07001531func (c *Module) IntermPathForModuleOut() android.OptionalPath {
1532 return c.outputFile
1533}
1534
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001535func (c *Module) Srcs() android.Paths {
1536 if c.outputFile.Valid() {
1537 return android.Paths{c.outputFile.Path()}
1538 }
1539 return android.Paths{}
1540}
1541
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001542func (c *Module) static() bool {
1543 if static, ok := c.linker.(interface {
1544 static() bool
1545 }); ok {
1546 return static.static()
1547 }
1548 return false
1549}
1550
Colin Crossb60190a2018-09-04 16:28:17 -07001551func (c *Module) getMakeLinkType() string {
1552 if c.useVndk() {
1553 if inList(c.Name(), vndkCoreLibraries) || inList(c.Name(), vndkSpLibraries) || inList(c.Name(), llndkLibraries) {
1554 if inList(c.Name(), vndkPrivateLibraries) {
1555 return "native:vndk_private"
1556 } else {
1557 return "native:vndk"
1558 }
1559 } else {
1560 return "native:vendor"
1561 }
1562 } else if c.inRecovery() {
1563 return "native:recovery"
1564 } else if c.Target().Os == android.Android && String(c.Properties.Sdk_version) != "" {
1565 return "native:ndk:none:none"
1566 // TODO(b/114741097): use the correct ndk stl once build errors have been fixed
1567 //family, link := getNdkStlFamilyAndLinkType(c)
1568 //return fmt.Sprintf("native:ndk:%s:%s", family, link)
1569 } else {
1570 return "native:platform"
1571 }
1572}
1573
Jiyong Park9d452992018-10-03 00:38:19 +09001574// Overrides ApexModule.IsInstallabeToApex()
1575// Only shared libraries are installable to APEX.
1576func (c *Module) IsInstallableToApex() bool {
1577 if shared, ok := c.linker.(interface {
1578 shared() bool
1579 }); ok {
1580 return shared.shared()
1581 }
1582 return false
1583}
1584
Colin Cross2ba19d92015-05-07 15:44:20 -07001585//
Colin Crosscfad1192015-11-02 16:43:11 -08001586// Defaults
1587//
Colin Crossca860ac2016-01-04 14:34:37 -08001588type Defaults struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001589 android.ModuleBase
Colin Cross1f44a3a2017-07-07 14:33:33 -07001590 android.DefaultsModuleBase
Jiyong Park9d452992018-10-03 00:38:19 +09001591 android.ApexModuleBase
Colin Crosscfad1192015-11-02 16:43:11 -08001592}
1593
Colin Cross635c3b02016-05-18 15:37:25 -07001594func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -08001595}
1596
Colin Cross1e676be2016-10-12 14:38:15 -07001597func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
1598}
1599
Colin Cross36242852017-06-23 15:06:31 -07001600func defaultsFactory() android.Module {
Colin Crosse1d764e2016-08-18 14:18:32 -07001601 return DefaultsFactory()
1602}
1603
Colin Cross36242852017-06-23 15:06:31 -07001604func DefaultsFactory(props ...interface{}) android.Module {
Colin Crossca860ac2016-01-04 14:34:37 -08001605 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -08001606
Colin Cross36242852017-06-23 15:06:31 -07001607 module.AddProperties(props...)
1608 module.AddProperties(
Colin Crossca860ac2016-01-04 14:34:37 -08001609 &BaseProperties{},
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001610 &VendorProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001611 &BaseCompilerProperties{},
1612 &BaseLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -07001613 &LibraryProperties{},
Colin Cross919281a2016-04-05 16:42:05 -07001614 &FlagExporterProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001615 &BinaryLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -07001616 &TestProperties{},
1617 &TestBinaryProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001618 &StlProperties{},
Colin Cross16b23492016-01-06 14:41:07 -08001619 &SanitizeProperties{},
Colin Cross665dce92016-04-28 14:50:03 -07001620 &StripProperties{},
Dan Willemsen7424d612016-09-01 13:45:39 -07001621 &InstallerProperties{},
Dan Willemsena03cf6d2016-09-26 15:45:04 -07001622 &TidyProperties{},
Dan Willemsen581341d2017-02-09 16:16:31 -08001623 &CoverageProperties{},
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001624 &SAbiProperties{},
Justin Yun4b2382f2017-07-26 14:22:10 +09001625 &VndkProperties{},
Stephen Craneba090d12017-05-09 15:44:35 -07001626 &LTOProperties{},
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -07001627 &PgoProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08001628 &android.ProtoProperties{},
Colin Crosse1d764e2016-08-18 14:18:32 -07001629 )
Colin Crosscfad1192015-11-02 16:43:11 -08001630
Colin Cross1f44a3a2017-07-07 14:33:33 -07001631 android.InitDefaultsModule(module)
Jiyong Park9d452992018-10-03 00:38:19 +09001632 android.InitApexModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001633
1634 return module
Colin Crosscfad1192015-11-02 16:43:11 -08001635}
1636
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001637const (
1638 // coreMode is the variant used for framework-private libraries, or
1639 // SDK libraries. (which framework-private libraries can use)
1640 coreMode = "core"
1641
1642 // vendorMode is the variant used for /vendor code that compiles
1643 // against the VNDK.
1644 vendorMode = "vendor"
Jiyong Parkf9332f12018-02-01 00:54:12 +09001645
1646 recoveryMode = "recovery"
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001647)
1648
Jiyong Park6a43f042017-10-12 23:05:00 +09001649func squashVendorSrcs(m *Module) {
1650 if lib, ok := m.compiler.(*libraryDecorator); ok {
1651 lib.baseCompiler.Properties.Srcs = append(lib.baseCompiler.Properties.Srcs,
1652 lib.baseCompiler.Properties.Target.Vendor.Srcs...)
1653
1654 lib.baseCompiler.Properties.Exclude_srcs = append(lib.baseCompiler.Properties.Exclude_srcs,
1655 lib.baseCompiler.Properties.Target.Vendor.Exclude_srcs...)
1656 }
1657}
1658
Jiyong Parkf9332f12018-02-01 00:54:12 +09001659func squashRecoverySrcs(m *Module) {
1660 if lib, ok := m.compiler.(*libraryDecorator); ok {
1661 lib.baseCompiler.Properties.Srcs = append(lib.baseCompiler.Properties.Srcs,
1662 lib.baseCompiler.Properties.Target.Recovery.Srcs...)
1663
1664 lib.baseCompiler.Properties.Exclude_srcs = append(lib.baseCompiler.Properties.Exclude_srcs,
1665 lib.baseCompiler.Properties.Target.Recovery.Exclude_srcs...)
1666 }
1667}
1668
1669func imageMutator(mctx android.BottomUpMutatorContext) {
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001670 if mctx.Os() != android.Android {
1671 return
1672 }
1673
Jiyong Park7ed9de32018-10-15 22:25:07 +09001674 if g, ok := mctx.Module().(*genrule.Module); ok {
1675 if props, ok := g.Extra.(*GenruleExtraProperties); ok {
Jiyong Park3f736c92018-05-24 13:36:56 +09001676 var coreVariantNeeded bool = false
1677 var vendorVariantNeeded bool = false
1678 var recoveryVariantNeeded bool = false
Justin Yun71549282017-11-17 12:10:28 +09001679 if mctx.DeviceConfig().VndkVersion() == "" {
Jiyong Park3f736c92018-05-24 13:36:56 +09001680 coreVariantNeeded = true
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001681 } else if Bool(props.Vendor_available) {
Jiyong Park3f736c92018-05-24 13:36:56 +09001682 coreVariantNeeded = true
1683 vendorVariantNeeded = true
Jiyong Park2db76922017-11-08 16:03:48 +09001684 } else if mctx.SocSpecific() || mctx.DeviceSpecific() {
Jiyong Park3f736c92018-05-24 13:36:56 +09001685 vendorVariantNeeded = true
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001686 } else {
Jiyong Park3f736c92018-05-24 13:36:56 +09001687 coreVariantNeeded = true
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001688 }
Jiyong Park3f736c92018-05-24 13:36:56 +09001689 if Bool(props.Recovery_available) {
1690 recoveryVariantNeeded = true
1691 }
1692
Jiyong Park413cc742018-06-19 01:46:06 +09001693 if recoveryVariantNeeded {
Jiyong Park8d52f862018-07-07 18:02:07 +09001694 primaryArch := mctx.Config().DevicePrimaryArchType()
Jiyong Park7ed9de32018-10-15 22:25:07 +09001695 moduleArch := g.Target().Arch.ArchType
Jiyong Park8d52f862018-07-07 18:02:07 +09001696 if moduleArch != primaryArch {
Jiyong Park413cc742018-06-19 01:46:06 +09001697 recoveryVariantNeeded = false
1698 }
1699 }
1700
Jiyong Park3f736c92018-05-24 13:36:56 +09001701 var variants []string
1702 if coreVariantNeeded {
1703 variants = append(variants, coreMode)
1704 }
1705 if vendorVariantNeeded {
1706 variants = append(variants, vendorMode)
1707 }
1708 if recoveryVariantNeeded {
1709 variants = append(variants, recoveryMode)
1710 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09001711 mod := mctx.CreateVariations(variants...)
1712 for i, v := range variants {
1713 if v == recoveryMode {
1714 m := mod[i].(*genrule.Module)
1715 m.Extra.(*GenruleExtraProperties).InRecovery = true
1716 }
1717 }
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001718 }
1719 }
1720
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001721 m, ok := mctx.Module().(*Module)
1722 if !ok {
1723 return
1724 }
1725
1726 // Sanity check
Logan Chienf3511742017-10-31 18:04:35 +08001727 vendorSpecific := mctx.SocSpecific() || mctx.DeviceSpecific()
1728
1729 if m.VendorProperties.Vendor_available != nil && vendorSpecific {
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001730 mctx.PropertyErrorf("vendor_available",
Jiyong Park2db76922017-11-08 16:03:48 +09001731 "doesn't make sense at the same time as `vendor: true`, `proprietary: true`, or `device_specific:true`")
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001732 return
1733 }
Logan Chienf3511742017-10-31 18:04:35 +08001734
1735 if vndkdep := m.vndkdep; vndkdep != nil {
1736 if vndkdep.isVndk() {
1737 if vendorSpecific {
1738 if !vndkdep.isVndkExt() {
1739 mctx.PropertyErrorf("vndk",
1740 "must set `extends: \"...\"` to vndk extension")
1741 return
1742 }
1743 } else {
1744 if vndkdep.isVndkExt() {
1745 mctx.PropertyErrorf("vndk",
1746 "must set `vendor: true` to set `extends: %q`",
1747 m.getVndkExtendsModuleName())
1748 return
1749 }
1750 if m.VendorProperties.Vendor_available == nil {
1751 mctx.PropertyErrorf("vndk",
1752 "vendor_available must be set to either true or false when `vndk: {enabled: true}`")
1753 return
1754 }
1755 }
1756 } else {
1757 if vndkdep.isVndkSp() {
1758 mctx.PropertyErrorf("vndk",
1759 "must set `enabled: true` to set `support_system_process: true`")
1760 return
1761 }
1762 if vndkdep.isVndkExt() {
1763 mctx.PropertyErrorf("vndk",
1764 "must set `enabled: true` to set `extends: %q`",
1765 m.getVndkExtendsModuleName())
1766 return
1767 }
Justin Yun8effde42017-06-23 19:24:43 +09001768 }
1769 }
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001770
Jiyong Parkf9332f12018-02-01 00:54:12 +09001771 var coreVariantNeeded bool = false
1772 var vendorVariantNeeded bool = false
1773 var recoveryVariantNeeded bool = false
1774
Justin Yun71549282017-11-17 12:10:28 +09001775 if mctx.DeviceConfig().VndkVersion() == "" {
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001776 // If the device isn't compiling against the VNDK, we always
1777 // use the core mode.
Jiyong Parkf9332f12018-02-01 00:54:12 +09001778 coreVariantNeeded = true
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001779 } else if _, ok := m.linker.(*llndkStubDecorator); ok {
1780 // LL-NDK stubs only exist in the vendor variant, since the
1781 // real libraries will be used in the core variant.
Jiyong Parkf9332f12018-02-01 00:54:12 +09001782 vendorVariantNeeded = true
Jiyong Park2a454122017-10-19 15:59:33 +09001783 } else if _, ok := m.linker.(*llndkHeadersDecorator); ok {
1784 // ... and LL-NDK headers as well
Jiyong Parkf9332f12018-02-01 00:54:12 +09001785 vendorVariantNeeded = true
Justin Yun312ccb92018-01-23 12:07:46 +09001786 } else if _, ok := m.linker.(*vndkPrebuiltLibraryDecorator); ok {
Justin Yun71549282017-11-17 12:10:28 +09001787 // Make vendor variants only for the versions in BOARD_VNDK_VERSION and
1788 // PRODUCT_EXTRA_VNDK_VERSIONS.
Jiyong Parkf9332f12018-02-01 00:54:12 +09001789 vendorVariantNeeded = true
Logan Chienf3511742017-10-31 18:04:35 +08001790 } else if m.hasVendorVariant() && !vendorSpecific {
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001791 // This will be available in both /system and /vendor
Justin Yun8effde42017-06-23 19:24:43 +09001792 // or a /system directory that is available to vendor.
Jiyong Parkf9332f12018-02-01 00:54:12 +09001793 coreVariantNeeded = true
1794 vendorVariantNeeded = true
Logan Chienf3511742017-10-31 18:04:35 +08001795 } else if vendorSpecific && String(m.Properties.Sdk_version) == "" {
Jiyong Park2db76922017-11-08 16:03:48 +09001796 // This will be available in /vendor (or /odm) only
Jiyong Parkf9332f12018-02-01 00:54:12 +09001797 vendorVariantNeeded = true
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001798 } else {
1799 // This is either in /system (or similar: /data), or is a
1800 // modules built with the NDK. Modules built with the NDK
1801 // will be restricted using the existing link type checks.
Jiyong Parkf9332f12018-02-01 00:54:12 +09001802 coreVariantNeeded = true
1803 }
1804
1805 if Bool(m.Properties.Recovery_available) {
1806 recoveryVariantNeeded = true
1807 }
1808
1809 if m.ModuleBase.InstallInRecovery() {
1810 recoveryVariantNeeded = true
1811 coreVariantNeeded = false
1812 }
1813
Jiyong Park413cc742018-06-19 01:46:06 +09001814 if recoveryVariantNeeded {
Jiyong Park8d52f862018-07-07 18:02:07 +09001815 primaryArch := mctx.Config().DevicePrimaryArchType()
1816 moduleArch := m.Target().Arch.ArchType
1817 if moduleArch != primaryArch {
Jiyong Park413cc742018-06-19 01:46:06 +09001818 recoveryVariantNeeded = false
1819 }
1820 }
1821
Jiyong Parkf9332f12018-02-01 00:54:12 +09001822 var variants []string
1823 if coreVariantNeeded {
1824 variants = append(variants, coreMode)
1825 }
1826 if vendorVariantNeeded {
1827 variants = append(variants, vendorMode)
1828 }
1829 if recoveryVariantNeeded {
1830 variants = append(variants, recoveryMode)
1831 }
1832 mod := mctx.CreateVariations(variants...)
1833 for i, v := range variants {
1834 if v == vendorMode {
1835 m := mod[i].(*Module)
1836 m.Properties.UseVndk = true
1837 squashVendorSrcs(m)
1838 } else if v == recoveryMode {
1839 m := mod[i].(*Module)
1840 m.Properties.InRecovery = true
Jiyong Park5baac542018-08-28 09:55:37 +09001841 m.MakeAsPlatform()
Jiyong Parkf9332f12018-02-01 00:54:12 +09001842 squashRecoverySrcs(m)
1843 }
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001844 }
1845}
1846
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -07001847func getCurrentNdkPrebuiltVersion(ctx DepsContext) string {
Colin Cross6510f912017-11-29 00:27:14 -08001848 if ctx.Config().PlatformSdkVersionInt() > config.NdkMaxPrebuiltVersionInt {
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -07001849 return strconv.Itoa(config.NdkMaxPrebuiltVersionInt)
1850 }
Colin Cross6510f912017-11-29 00:27:14 -08001851 return ctx.Config().PlatformSdkVersion()
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -07001852}
1853
Colin Cross06a931b2015-10-28 17:23:31 -07001854var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07001855var BoolDefault = proptools.BoolDefault
Nan Zhang0007d812017-11-07 10:57:05 -08001856var BoolPtr = proptools.BoolPtr
1857var String = proptools.String
1858var StringPtr = proptools.StringPtr