blob: 1dc0636d5a81058f051038828651cd7d76c92af5 [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 Jungeb05c2a2018-11-09 16:02:23 +0000178 AndroidMkSharedLibs []string `blueprint:"mutated"`
179 AndroidMkStaticLibs []string `blueprint:"mutated"`
180 AndroidMkRuntimeLibs []string `blueprint:"mutated"`
181 AndroidMkWholeStaticLibs []string `blueprint:"mutated"`
182 HideFromMake bool `blueprint:"mutated"`
183 PreventInstall bool `blueprint:"mutated"`
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700184
185 UseVndk bool `blueprint:"mutated"`
Colin Cross5beccee2017-12-07 15:28:59 -0800186
187 // *.logtags files, to combine together in order to generate the /system/etc/event-log-tags
188 // file
189 Logtags []string
Jiyong Parkf9332f12018-02-01 00:54:12 +0900190
191 // Make this module available when building for recovery
192 Recovery_available *bool
193
194 InRecovery bool `blueprint:"mutated"`
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700195}
196
197type VendorProperties struct {
Jiyong Park82e2bf32017-08-16 14:05:54 +0900198 // whether this module should be allowed to be directly depended by other
199 // modules with `vendor: true`, `proprietary: true`, or `vendor_available:true`.
200 // If set to true, two variants will be built separately, one like
201 // normal, and the other limited to the set of libraries and headers
202 // that are exposed to /vendor modules.
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700203 //
204 // The vendor variant may be used with a different (newer) /system,
205 // so it shouldn't have any unversioned runtime dependencies, or
206 // make assumptions about the system that may not be true in the
207 // future.
208 //
Jiyong Park82e2bf32017-08-16 14:05:54 +0900209 // If set to false, this module becomes inaccessible from /vendor modules.
210 //
211 // Default value is true when vndk: {enabled: true} or vendor: true.
212 //
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700213 // Nothing happens if BOARD_VNDK_VERSION isn't set in the BoardConfig.mk
214 Vendor_available *bool
Jiyong Park5fb8c102018-04-09 12:03:06 +0900215
216 // whether this module is capable of being loaded with other instance
217 // (possibly an older version) of the same module in the same process.
218 // Currently, a shared library that is a member of VNDK (vndk: {enabled: true})
219 // can be double loaded in a vendor process if the library is also a
220 // (direct and indirect) dependency of an LLNDK library. Such libraries must be
221 // explicitly marked as `double_loadable: true` by the owner, or the dependency
222 // from the LLNDK lib should be cut if the lib is not designed to be double loaded.
223 Double_loadable *bool
Colin Crossca860ac2016-01-04 14:34:37 -0800224}
225
Colin Crossca860ac2016-01-04 14:34:37 -0800226type ModuleContextIntf interface {
Colin Crossca860ac2016-01-04 14:34:37 -0800227 static() bool
228 staticBinary() bool
Colin Crossb98c8b02016-07-29 13:44:28 -0700229 toolchain() config.Toolchain
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700230 useSdk() bool
Colin Crossca860ac2016-01-04 14:34:37 -0800231 sdkVersion() string
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700232 useVndk() bool
Justin Yun8effde42017-06-23 19:24:43 +0900233 isVndk() bool
234 isVndkSp() bool
Logan Chienf3511742017-10-31 18:04:35 +0800235 isVndkExt() bool
Jiyong Parkf9332f12018-02-01 00:54:12 +0900236 inRecovery() bool
Logan Chien2f2b8902018-07-10 15:01:19 +0800237 shouldCreateVndkSourceAbiDump() bool
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700238 selectedStl() string
Colin Crossce75d2c2016-10-06 16:12:58 -0700239 baseModuleName() string
Logan Chienf3511742017-10-31 18:04:35 +0800240 getVndkExtendsModuleName() string
Yi Kong7e53c572018-02-14 18:16:12 +0800241 isPgoCompile() bool
Colin Crossca860ac2016-01-04 14:34:37 -0800242}
243
244type ModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700245 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800246 ModuleContextIntf
247}
248
249type BaseModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700250 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800251 ModuleContextIntf
252}
253
Colin Cross37047f12016-12-13 17:06:13 -0800254type DepsContext interface {
255 android.BottomUpMutatorContext
256 ModuleContextIntf
257}
258
Colin Crossca860ac2016-01-04 14:34:37 -0800259type feature interface {
260 begin(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800261 deps(ctx DepsContext, deps Deps) Deps
Colin Crossca860ac2016-01-04 14:34:37 -0800262 flags(ctx ModuleContext, flags Flags) Flags
263 props() []interface{}
264}
265
266type compiler interface {
Colin Cross42742b82016-08-01 13:20:05 -0700267 compilerInit(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800268 compilerDeps(ctx DepsContext, deps Deps) Deps
Colin Crossf18e1102017-11-16 14:33:08 -0800269 compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags
Colin Cross42742b82016-08-01 13:20:05 -0700270 compilerProps() []interface{}
271
Colin Cross76fada02016-07-27 10:31:13 -0700272 appendCflags([]string)
273 appendAsflags([]string)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700274 compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects
Colin Crossca860ac2016-01-04 14:34:37 -0800275}
276
277type linker interface {
Colin Cross42742b82016-08-01 13:20:05 -0700278 linkerInit(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800279 linkerDeps(ctx DepsContext, deps Deps) Deps
Colin Cross42742b82016-08-01 13:20:05 -0700280 linkerFlags(ctx ModuleContext, flags Flags) Flags
281 linkerProps() []interface{}
282
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700283 link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path
Colin Cross76fada02016-07-27 10:31:13 -0700284 appendLdflags([]string)
Colin Crossca860ac2016-01-04 14:34:37 -0800285}
286
287type installer interface {
Colin Cross42742b82016-08-01 13:20:05 -0700288 installerProps() []interface{}
Colin Cross635c3b02016-05-18 15:37:25 -0700289 install(ctx ModuleContext, path android.Path)
Colin Crossca860ac2016-01-04 14:34:37 -0800290 inData() bool
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700291 inSanitizerDir() bool
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700292 hostToolPath() android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800293}
294
Colin Crossc99deeb2016-04-11 15:06:20 -0700295type dependencyTag struct {
296 blueprint.BaseDependencyTag
297 name string
298 library bool
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700299
300 reexportFlags bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700301}
302
303var (
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700304 sharedDepTag = dependencyTag{name: "shared", library: true}
305 sharedExportDepTag = dependencyTag{name: "shared", library: true, reexportFlags: true}
306 lateSharedDepTag = dependencyTag{name: "late shared", library: true}
307 staticDepTag = dependencyTag{name: "static", library: true}
308 staticExportDepTag = dependencyTag{name: "static", library: true, reexportFlags: true}
309 lateStaticDepTag = dependencyTag{name: "late static", library: true}
310 wholeStaticDepTag = dependencyTag{name: "whole static", library: true, reexportFlags: true}
Colin Cross32ec36c2016-12-15 07:39:51 -0800311 headerDepTag = dependencyTag{name: "header", library: true}
312 headerExportDepTag = dependencyTag{name: "header", library: true, reexportFlags: true}
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700313 genSourceDepTag = dependencyTag{name: "gen source"}
314 genHeaderDepTag = dependencyTag{name: "gen header"}
315 genHeaderExportDepTag = dependencyTag{name: "gen header", reexportFlags: true}
316 objDepTag = dependencyTag{name: "obj"}
317 crtBeginDepTag = dependencyTag{name: "crtbegin"}
318 crtEndDepTag = dependencyTag{name: "crtend"}
Dan Willemsena0790e32018-10-12 00:24:23 -0700319 linkerFlagsDepTag = dependencyTag{name: "linker flags file"}
320 dynamicLinkerDepTag = dependencyTag{name: "dynamic linker"}
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700321 reuseObjTag = dependencyTag{name: "reuse objects"}
322 ndkStubDepTag = dependencyTag{name: "ndk stub", library: true}
323 ndkLateStubDepTag = dependencyTag{name: "ndk late stub", library: true}
Logan Chienf3511742017-10-31 18:04:35 +0800324 vndkExtDepTag = dependencyTag{name: "vndk extends", library: true}
Logan Chien43d34c32017-12-20 01:17:32 +0800325 runtimeDepTag = dependencyTag{name: "runtime lib"}
Colin Crossc99deeb2016-04-11 15:06:20 -0700326)
327
Colin Crossca860ac2016-01-04 14:34:37 -0800328// Module contains the properties and members used by all C/C++ module types, and implements
329// the blueprint.Module interface. It delegates to compiler, linker, and installer interfaces
330// to construct the output file. Behavior can be customized with a Customizer interface
331type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700332 android.ModuleBase
Colin Cross1f44a3a2017-07-07 14:33:33 -0700333 android.DefaultableModuleBase
Jiyong Park9d452992018-10-03 00:38:19 +0900334 android.ApexModuleBase
Colin Crossc472d572015-03-17 15:06:21 -0700335
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700336 Properties BaseProperties
337 VendorProperties VendorProperties
Colin Crossfa138792015-04-24 17:31:52 -0700338
Colin Crossca860ac2016-01-04 14:34:37 -0800339 // initialize before calling Init
Colin Cross635c3b02016-05-18 15:37:25 -0700340 hod android.HostOrDeviceSupported
341 multilib android.Multilib
Colin Crossc472d572015-03-17 15:06:21 -0700342
Colin Crossca860ac2016-01-04 14:34:37 -0800343 // delegates, initialize before calling Init
Colin Crossb4ce0ec2016-09-13 13:41:39 -0700344 features []feature
345 compiler compiler
346 linker linker
347 installer installer
348 stl *stl
349 sanitize *sanitize
Dan Willemsen581341d2017-02-09 16:16:31 -0800350 coverage *coverage
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800351 sabi *sabi
Justin Yun8effde42017-06-23 19:24:43 +0900352 vndkdep *vndkdep
Stephen Craneba090d12017-05-09 15:44:35 -0700353 lto *lto
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700354 pgo *pgo
Colin Cross16b23492016-01-06 14:41:07 -0800355
356 androidMkSharedLibDeps []string
Colin Cross74d1ec02015-04-28 13:30:13 -0700357
Colin Cross635c3b02016-05-18 15:37:25 -0700358 outputFile android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800359
Colin Crossb98c8b02016-07-29 13:44:28 -0700360 cachedToolchain config.Toolchain
Colin Crossb916a382016-07-29 17:28:03 -0700361
362 subAndroidMkOnce map[subAndroidMkProvider]bool
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800363
364 // Flags used to compile this module
365 flags Flags
Jeff Gaston294356f2017-09-27 17:05:30 -0700366
367 // 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 -0800368 // line invocation. depsInLinkOrder stores the proper ordering of all of the transitive
Jeff Gaston294356f2017-09-27 17:05:30 -0700369 // deps of this module
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -0800370 depsInLinkOrder android.Paths
371
372 // only non-nil when this is a shared library that reuses the objects of a static library
373 staticVariant *Module
Colin Crossc472d572015-03-17 15:06:21 -0700374}
375
Jiyong Parkc20eee32018-09-05 22:36:17 +0900376func (c *Module) OutputFile() android.OptionalPath {
377 return c.outputFile
378}
379
Colin Cross36242852017-06-23 15:06:31 -0700380func (c *Module) Init() android.Module {
Dan Willemsenf923f2b2018-05-09 13:45:03 -0700381 c.AddProperties(&c.Properties, &c.VendorProperties)
Colin Crossca860ac2016-01-04 14:34:37 -0800382 if c.compiler != nil {
Colin Cross36242852017-06-23 15:06:31 -0700383 c.AddProperties(c.compiler.compilerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800384 }
385 if c.linker != nil {
Colin Cross36242852017-06-23 15:06:31 -0700386 c.AddProperties(c.linker.linkerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800387 }
388 if c.installer != nil {
Colin Cross36242852017-06-23 15:06:31 -0700389 c.AddProperties(c.installer.installerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800390 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700391 if c.stl != nil {
Colin Cross36242852017-06-23 15:06:31 -0700392 c.AddProperties(c.stl.props()...)
Colin Crossa8e07cc2016-04-04 15:07:06 -0700393 }
Colin Cross16b23492016-01-06 14:41:07 -0800394 if c.sanitize != nil {
Colin Cross36242852017-06-23 15:06:31 -0700395 c.AddProperties(c.sanitize.props()...)
Colin Cross16b23492016-01-06 14:41:07 -0800396 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800397 if c.coverage != nil {
Colin Cross36242852017-06-23 15:06:31 -0700398 c.AddProperties(c.coverage.props()...)
Dan Willemsen581341d2017-02-09 16:16:31 -0800399 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800400 if c.sabi != nil {
Colin Cross36242852017-06-23 15:06:31 -0700401 c.AddProperties(c.sabi.props()...)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800402 }
Justin Yun8effde42017-06-23 19:24:43 +0900403 if c.vndkdep != nil {
404 c.AddProperties(c.vndkdep.props()...)
405 }
Stephen Craneba090d12017-05-09 15:44:35 -0700406 if c.lto != nil {
407 c.AddProperties(c.lto.props()...)
408 }
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700409 if c.pgo != nil {
410 c.AddProperties(c.pgo.props()...)
411 }
Colin Crossca860ac2016-01-04 14:34:37 -0800412 for _, feature := range c.features {
Colin Cross36242852017-06-23 15:06:31 -0700413 c.AddProperties(feature.props()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800414 }
Colin Crossc472d572015-03-17 15:06:21 -0700415
Colin Crossa9d8bee2018-10-02 13:59:46 -0700416 c.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
417 switch class {
418 case android.Device:
419 return ctx.Config().DevicePrefer32BitExecutables()
420 case android.HostCross:
421 // Windows builds always prefer 32-bit
422 return true
423 default:
424 return false
425 }
426 })
Colin Cross36242852017-06-23 15:06:31 -0700427 android.InitAndroidArchModule(c, c.hod, c.multilib)
Colin Crossc472d572015-03-17 15:06:21 -0700428
Colin Cross1f44a3a2017-07-07 14:33:33 -0700429 android.InitDefaultableModule(c)
Colin Cross36242852017-06-23 15:06:31 -0700430
Jiyong Park9d452992018-10-03 00:38:19 +0900431 android.InitApexModule(c)
432
Colin Cross36242852017-06-23 15:06:31 -0700433 return c
Colin Crossc472d572015-03-17 15:06:21 -0700434}
435
Colin Crossb916a382016-07-29 17:28:03 -0700436// Returns true for dependency roots (binaries)
437// TODO(ccross): also handle dlopenable libraries
438func (c *Module) isDependencyRoot() bool {
439 if root, ok := c.linker.(interface {
440 isDependencyRoot() bool
441 }); ok {
442 return root.isDependencyRoot()
443 }
444 return false
445}
446
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700447func (c *Module) useVndk() bool {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700448 return c.Properties.UseVndk
449}
450
Justin Yun8effde42017-06-23 19:24:43 +0900451func (c *Module) isVndk() bool {
Logan Chienf3511742017-10-31 18:04:35 +0800452 if vndkdep := c.vndkdep; vndkdep != nil {
453 return vndkdep.isVndk()
Justin Yun8effde42017-06-23 19:24:43 +0900454 }
455 return false
456}
457
Yi Kong7e53c572018-02-14 18:16:12 +0800458func (c *Module) isPgoCompile() bool {
459 if pgo := c.pgo; pgo != nil {
460 return pgo.Properties.PgoCompile
461 }
462 return false
463}
464
Logan Chienf3511742017-10-31 18:04:35 +0800465func (c *Module) isVndkSp() bool {
466 if vndkdep := c.vndkdep; vndkdep != nil {
467 return vndkdep.isVndkSp()
468 }
469 return false
470}
471
472func (c *Module) isVndkExt() bool {
473 if vndkdep := c.vndkdep; vndkdep != nil {
474 return vndkdep.isVndkExt()
475 }
476 return false
477}
478
479func (c *Module) getVndkExtendsModuleName() string {
480 if vndkdep := c.vndkdep; vndkdep != nil {
481 return vndkdep.getVndkExtendsModuleName()
482 }
483 return ""
484}
485
Jiyong Park82e2bf32017-08-16 14:05:54 +0900486// Returns true only when this module is configured to have core and vendor
487// variants.
488func (c *Module) hasVendorVariant() bool {
489 return c.isVndk() || Bool(c.VendorProperties.Vendor_available)
490}
491
Jiyong Parkf9332f12018-02-01 00:54:12 +0900492func (c *Module) inRecovery() bool {
493 return c.Properties.InRecovery || c.ModuleBase.InstallInRecovery()
494}
495
496func (c *Module) onlyInRecovery() bool {
497 return c.ModuleBase.InstallInRecovery()
498}
499
Colin Crossca860ac2016-01-04 14:34:37 -0800500type baseModuleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700501 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800502 moduleContextImpl
503}
504
Colin Cross37047f12016-12-13 17:06:13 -0800505type depsContext struct {
506 android.BottomUpMutatorContext
507 moduleContextImpl
508}
509
Colin Crossca860ac2016-01-04 14:34:37 -0800510type moduleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700511 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800512 moduleContextImpl
513}
514
Jiyong Park2db76922017-11-08 16:03:48 +0900515func (ctx *moduleContext) SocSpecific() bool {
516 return ctx.ModuleContext.SocSpecific() ||
517 (ctx.mod.hasVendorVariant() && ctx.mod.useVndk() && !ctx.mod.isVndk())
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700518}
519
Colin Crossca860ac2016-01-04 14:34:37 -0800520type moduleContextImpl struct {
521 mod *Module
522 ctx BaseModuleContext
523}
524
Colin Crossb98c8b02016-07-29 13:44:28 -0700525func (ctx *moduleContextImpl) toolchain() config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800526 return ctx.mod.toolchain(ctx.ctx)
527}
528
529func (ctx *moduleContextImpl) static() bool {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000530 return ctx.mod.static()
Colin Crossca860ac2016-01-04 14:34:37 -0800531}
532
533func (ctx *moduleContextImpl) staticBinary() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700534 if static, ok := ctx.mod.linker.(interface {
535 staticBinary() bool
536 }); ok {
537 return static.staticBinary()
Colin Crossca860ac2016-01-04 14:34:37 -0800538 }
Colin Crossb916a382016-07-29 17:28:03 -0700539 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800540}
541
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700542func (ctx *moduleContextImpl) useSdk() bool {
Jiyong Parkf9332f12018-02-01 00:54:12 +0900543 if ctx.ctx.Device() && !ctx.useVndk() && !ctx.inRecovery() {
Nan Zhang0007d812017-11-07 10:57:05 -0800544 return String(ctx.mod.Properties.Sdk_version) != ""
Dan Willemsena96ff642016-06-07 12:34:45 -0700545 }
546 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800547}
548
549func (ctx *moduleContextImpl) sdkVersion() string {
Dan Willemsena96ff642016-06-07 12:34:45 -0700550 if ctx.ctx.Device() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700551 if ctx.useVndk() {
Justin Yun732aa6a2018-03-23 17:43:47 +0900552 vndk_ver := ctx.ctx.DeviceConfig().VndkVersion()
Ryan Prichard05206112018-03-26 21:25:27 -0700553 if vndk_ver == "current" {
Justin Yun732aa6a2018-03-23 17:43:47 +0900554 platform_vndk_ver := ctx.ctx.DeviceConfig().PlatformVndkVersion()
555 if inList(platform_vndk_ver, ctx.ctx.Config().PlatformVersionCombinedCodenames()) {
556 return "current"
557 }
558 return platform_vndk_ver
559 }
560 return vndk_ver
Dan Willemsend2ede872016-11-18 14:54:24 -0800561 }
Justin Yun732aa6a2018-03-23 17:43:47 +0900562 return String(ctx.mod.Properties.Sdk_version)
Dan Willemsena96ff642016-06-07 12:34:45 -0700563 }
564 return ""
Colin Crossca860ac2016-01-04 14:34:37 -0800565}
566
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700567func (ctx *moduleContextImpl) useVndk() bool {
568 return ctx.mod.useVndk()
569}
Justin Yun8effde42017-06-23 19:24:43 +0900570
Logan Chienf3511742017-10-31 18:04:35 +0800571func (ctx *moduleContextImpl) isVndk() bool {
572 return ctx.mod.isVndk()
573}
574
Yi Kong7e53c572018-02-14 18:16:12 +0800575func (ctx *moduleContextImpl) isPgoCompile() bool {
576 return ctx.mod.isPgoCompile()
577}
578
Justin Yun8effde42017-06-23 19:24:43 +0900579func (ctx *moduleContextImpl) isVndkSp() bool {
Logan Chienf3511742017-10-31 18:04:35 +0800580 return ctx.mod.isVndkSp()
581}
582
583func (ctx *moduleContextImpl) isVndkExt() bool {
584 return ctx.mod.isVndkExt()
Justin Yun8effde42017-06-23 19:24:43 +0900585}
586
Jiyong Parkf9332f12018-02-01 00:54:12 +0900587func (ctx *moduleContextImpl) inRecovery() bool {
588 return ctx.mod.inRecovery()
589}
590
Logan Chien2f2b8902018-07-10 15:01:19 +0800591// Check whether ABI dumps should be created for this module.
592func (ctx *moduleContextImpl) shouldCreateVndkSourceAbiDump() bool {
593 if ctx.ctx.Config().IsEnvTrue("SKIP_ABI_CHECKS") {
594 return false
Jayant Chowdharyea0a2e12018-03-01 19:12:16 -0800595 }
Logan Chien2f2b8902018-07-10 15:01:19 +0800596 if sanitize := ctx.mod.sanitize; sanitize != nil {
597 if !sanitize.isVariantOnProductionDevice() {
598 return false
599 }
600 }
601 if !ctx.ctx.Device() {
602 // Host modules do not need ABI dumps.
603 return false
604 }
605 if inList(ctx.baseModuleName(), llndkLibraries) {
606 return true
607 }
Logan Chienf4b79c62018-08-02 02:27:02 +0800608 if inList(ctx.baseModuleName(), ndkMigratedLibs) {
609 return true
610 }
Logan Chien2f2b8902018-07-10 15:01:19 +0800611 if ctx.useVndk() && ctx.isVndk() {
612 // Return true if this is VNDK-core, VNDK-SP, or VNDK-Ext and this is not
613 // VNDK-private.
614 return Bool(ctx.mod.VendorProperties.Vendor_available) || ctx.isVndkExt()
615 }
616 return false
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800617}
618
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700619func (ctx *moduleContextImpl) selectedStl() string {
620 if stl := ctx.mod.stl; stl != nil {
621 return stl.Properties.SelectedStl
622 }
623 return ""
624}
625
Colin Crossce75d2c2016-10-06 16:12:58 -0700626func (ctx *moduleContextImpl) baseModuleName() string {
627 return ctx.mod.ModuleBase.BaseModuleName()
628}
629
Logan Chienf3511742017-10-31 18:04:35 +0800630func (ctx *moduleContextImpl) getVndkExtendsModuleName() string {
631 return ctx.mod.getVndkExtendsModuleName()
632}
633
Colin Cross635c3b02016-05-18 15:37:25 -0700634func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800635 return &Module{
636 hod: hod,
637 multilib: multilib,
638 }
639}
640
Colin Cross635c3b02016-05-18 15:37:25 -0700641func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800642 module := newBaseModule(hod, multilib)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700643 module.features = []feature{
644 &tidyFeature{},
645 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700646 module.stl = &stl{}
Colin Cross16b23492016-01-06 14:41:07 -0800647 module.sanitize = &sanitize{}
Dan Willemsen581341d2017-02-09 16:16:31 -0800648 module.coverage = &coverage{}
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800649 module.sabi = &sabi{}
Justin Yun8effde42017-06-23 19:24:43 +0900650 module.vndkdep = &vndkdep{}
Stephen Craneba090d12017-05-09 15:44:35 -0700651 module.lto = &lto{}
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700652 module.pgo = &pgo{}
Colin Crossca860ac2016-01-04 14:34:37 -0800653 return module
654}
655
Colin Crossce75d2c2016-10-06 16:12:58 -0700656func (c *Module) Prebuilt() *android.Prebuilt {
657 if p, ok := c.linker.(prebuiltLinkerInterface); ok {
658 return p.prebuilt()
659 }
660 return nil
661}
662
663func (c *Module) Name() string {
664 name := c.ModuleBase.Name()
Dan Willemsen01a90592017-04-07 15:21:13 -0700665 if p, ok := c.linker.(interface {
666 Name(string) string
667 }); ok {
Colin Crossce75d2c2016-10-06 16:12:58 -0700668 name = p.Name(name)
669 }
670 return name
671}
672
Jeff Gaston294356f2017-09-27 17:05:30 -0700673// orderDeps reorders dependencies into a list such that if module A depends on B, then
674// A will precede B in the resultant list.
675// This is convenient for passing into a linker.
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -0800676// Note that directSharedDeps should be the analogous static library for each shared lib dep
677func 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 -0700678 // If A depends on B, then
679 // Every list containing A will also contain B later in the list
680 // So, after concatenating all lists, the final instance of B will have come from the same
681 // original list as the final instance of A
682 // So, the final instance of B will be later in the concatenation than the final A
683 // So, keeping only the final instance of A and of B ensures that A is earlier in the output
684 // list than B
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -0800685 for _, dep := range directStaticDeps {
Jeff Gaston294356f2017-09-27 17:05:30 -0700686 orderedAllDeps = append(orderedAllDeps, dep)
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -0800687 orderedAllDeps = append(orderedAllDeps, allTransitiveDeps[dep]...)
688 }
689 for _, dep := range directSharedDeps {
690 orderedAllDeps = append(orderedAllDeps, dep)
691 orderedAllDeps = append(orderedAllDeps, allTransitiveDeps[dep]...)
Jeff Gaston294356f2017-09-27 17:05:30 -0700692 }
693
Colin Crossb6715442017-10-24 11:13:31 -0700694 orderedAllDeps = android.LastUniquePaths(orderedAllDeps)
Jeff Gaston294356f2017-09-27 17:05:30 -0700695
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -0800696 // We don't want to add any new dependencies into directStaticDeps (to allow the caller to
Jeff Gaston294356f2017-09-27 17:05:30 -0700697 // intentionally exclude or replace any unwanted transitive dependencies), so we limit the
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -0800698 // resultant list to only what the caller has chosen to include in directStaticDeps
699 _, orderedDeclaredDeps = android.FilterPathList(orderedAllDeps, directStaticDeps)
Jeff Gaston294356f2017-09-27 17:05:30 -0700700
701 return orderedAllDeps, orderedDeclaredDeps
702}
703
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -0800704func orderStaticModuleDeps(module *Module, staticDeps []*Module, sharedDeps []*Module) (results []android.Path) {
705 // convert Module to Path
706 allTransitiveDeps := make(map[android.Path][]android.Path, len(staticDeps))
707 staticDepFiles := []android.Path{}
708 for _, dep := range staticDeps {
709 allTransitiveDeps[dep.outputFile.Path()] = dep.depsInLinkOrder
710 staticDepFiles = append(staticDepFiles, dep.outputFile.Path())
Jeff Gaston294356f2017-09-27 17:05:30 -0700711 }
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -0800712 sharedDepFiles := []android.Path{}
713 for _, sharedDep := range sharedDeps {
714 staticAnalogue := sharedDep.staticVariant
715 if staticAnalogue != nil {
716 allTransitiveDeps[staticAnalogue.outputFile.Path()] = staticAnalogue.depsInLinkOrder
717 sharedDepFiles = append(sharedDepFiles, staticAnalogue.outputFile.Path())
718 }
Jeff Gaston294356f2017-09-27 17:05:30 -0700719 }
720
721 // reorder the dependencies based on transitive dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -0800722 module.depsInLinkOrder, results = orderDeps(staticDepFiles, sharedDepFiles, allTransitiveDeps)
Jeff Gaston294356f2017-09-27 17:05:30 -0700723
724 return results
Jeff Gaston294356f2017-09-27 17:05:30 -0700725}
726
Colin Cross635c3b02016-05-18 15:37:25 -0700727func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800728 ctx := &moduleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700729 ModuleContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800730 moduleContextImpl: moduleContextImpl{
731 mod: c,
732 },
733 }
734 ctx.ctx = ctx
735
Colin Crossf18e1102017-11-16 14:33:08 -0800736 deps := c.depsToPaths(ctx)
737 if ctx.Failed() {
738 return
739 }
740
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700741 if c.Properties.Clang != nil && *c.Properties.Clang == false {
742 ctx.PropertyErrorf("clang", "false (GCC) is no longer supported")
743 }
744
Colin Crossca860ac2016-01-04 14:34:37 -0800745 flags := Flags{
746 Toolchain: c.toolchain(ctx),
Colin Crossca860ac2016-01-04 14:34:37 -0800747 }
Colin Crossca860ac2016-01-04 14:34:37 -0800748 if c.compiler != nil {
Colin Crossf18e1102017-11-16 14:33:08 -0800749 flags = c.compiler.compilerFlags(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -0800750 }
751 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700752 flags = c.linker.linkerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800753 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700754 if c.stl != nil {
755 flags = c.stl.flags(ctx, flags)
756 }
Colin Cross16b23492016-01-06 14:41:07 -0800757 if c.sanitize != nil {
758 flags = c.sanitize.flags(ctx, flags)
759 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800760 if c.coverage != nil {
761 flags = c.coverage.flags(ctx, flags)
762 }
Stephen Craneba090d12017-05-09 15:44:35 -0700763 if c.lto != nil {
764 flags = c.lto.flags(ctx, flags)
765 }
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700766 if c.pgo != nil {
767 flags = c.pgo.flags(ctx, flags)
768 }
Colin Crossca860ac2016-01-04 14:34:37 -0800769 for _, feature := range c.features {
770 flags = feature.flags(ctx, flags)
771 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800772 if ctx.Failed() {
773 return
774 }
775
Colin Crossb98c8b02016-07-29 13:44:28 -0700776 flags.CFlags, _ = filterList(flags.CFlags, config.IllegalFlags)
777 flags.CppFlags, _ = filterList(flags.CppFlags, config.IllegalFlags)
778 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, config.IllegalFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800779
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800780 flags.GlobalFlags = append(flags.GlobalFlags, deps.Flags...)
781 c.flags = flags
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700782 // We need access to all the flags seen by a source file.
783 if c.sabi != nil {
784 flags = c.sabi.flags(ctx, flags)
785 }
Colin Crossca860ac2016-01-04 14:34:37 -0800786 // Optimization to reduce size of build.ninja
787 // Replace the long list of flags for each file with a module-local variable
788 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
789 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
790 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
791 flags.CFlags = []string{"$cflags"}
792 flags.CppFlags = []string{"$cppflags"}
793 flags.AsFlags = []string{"$asflags"}
794
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700795 var objs Objects
Colin Crossca860ac2016-01-04 14:34:37 -0800796 if c.compiler != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700797 objs = c.compiler.compile(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -0800798 if ctx.Failed() {
799 return
800 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800801 }
802
Colin Crossca860ac2016-01-04 14:34:37 -0800803 if c.linker != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700804 outputFile := c.linker.link(ctx, flags, deps, objs)
Colin Crossca860ac2016-01-04 14:34:37 -0800805 if ctx.Failed() {
806 return
807 }
Colin Cross635c3b02016-05-18 15:37:25 -0700808 c.outputFile = android.OptionalPathForPath(outputFile)
Colin Crossce75d2c2016-10-06 16:12:58 -0700809 }
Colin Cross5049f022015-03-18 13:28:46 -0700810
Jiyong Park9d452992018-10-03 00:38:19 +0900811 if c.installer != nil && !c.Properties.PreventInstall && c.IsForPlatform() && c.outputFile.Valid() {
Colin Crossce75d2c2016-10-06 16:12:58 -0700812 c.installer.install(ctx, c.outputFile.Path())
813 if ctx.Failed() {
814 return
Colin Crossca860ac2016-01-04 14:34:37 -0800815 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700816 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800817}
818
Colin Crossb98c8b02016-07-29 13:44:28 -0700819func (c *Module) toolchain(ctx BaseModuleContext) config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800820 if c.cachedToolchain == nil {
Colin Crossb98c8b02016-07-29 13:44:28 -0700821 c.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
Colin Cross3f40fa42015-01-30 17:27:36 -0800822 }
Colin Crossca860ac2016-01-04 14:34:37 -0800823 return c.cachedToolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800824}
825
Colin Crossca860ac2016-01-04 14:34:37 -0800826func (c *Module) begin(ctx BaseModuleContext) {
827 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700828 c.compiler.compilerInit(ctx)
Colin Cross21b9a242015-03-24 14:15:58 -0700829 }
Colin Crossca860ac2016-01-04 14:34:37 -0800830 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700831 c.linker.linkerInit(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800832 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700833 if c.stl != nil {
834 c.stl.begin(ctx)
835 }
Colin Cross16b23492016-01-06 14:41:07 -0800836 if c.sanitize != nil {
837 c.sanitize.begin(ctx)
838 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800839 if c.coverage != nil {
840 c.coverage.begin(ctx)
841 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800842 if c.sabi != nil {
843 c.sabi.begin(ctx)
844 }
Justin Yun8effde42017-06-23 19:24:43 +0900845 if c.vndkdep != nil {
846 c.vndkdep.begin(ctx)
847 }
Stephen Craneba090d12017-05-09 15:44:35 -0700848 if c.lto != nil {
849 c.lto.begin(ctx)
850 }
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700851 if c.pgo != nil {
852 c.pgo.begin(ctx)
853 }
Colin Crossca860ac2016-01-04 14:34:37 -0800854 for _, feature := range c.features {
855 feature.begin(ctx)
856 }
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700857 if ctx.useSdk() {
Dan Albertf5415d72017-08-17 16:19:59 -0700858 version, err := normalizeNdkApiLevel(ctx, ctx.sdkVersion(), ctx.Arch())
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700859 if err != nil {
860 ctx.PropertyErrorf("sdk_version", err.Error())
861 }
Nan Zhang0007d812017-11-07 10:57:05 -0800862 c.Properties.Sdk_version = StringPtr(version)
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700863 }
Colin Crossca860ac2016-01-04 14:34:37 -0800864}
865
Colin Cross37047f12016-12-13 17:06:13 -0800866func (c *Module) deps(ctx DepsContext) Deps {
Colin Crossc99deeb2016-04-11 15:06:20 -0700867 deps := Deps{}
868
869 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700870 deps = c.compiler.compilerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700871 }
Pirama Arumuga Nainar0b882f02018-04-23 22:44:39 +0000872 // Add the PGO dependency (the clang_rt.profile runtime library), which
873 // sometimes depends on symbols from libgcc, before libgcc gets added
874 // in linkerDeps().
Pirama Arumuga Nainar49b53d52017-10-04 16:47:29 -0700875 if c.pgo != nil {
876 deps = c.pgo.deps(ctx, deps)
877 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700878 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700879 deps = c.linker.linkerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700880 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700881 if c.stl != nil {
882 deps = c.stl.deps(ctx, deps)
883 }
Colin Cross16b23492016-01-06 14:41:07 -0800884 if c.sanitize != nil {
885 deps = c.sanitize.deps(ctx, deps)
886 }
Pirama Arumuga Nainar0b882f02018-04-23 22:44:39 +0000887 if c.coverage != nil {
888 deps = c.coverage.deps(ctx, deps)
889 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800890 if c.sabi != nil {
891 deps = c.sabi.deps(ctx, deps)
892 }
Justin Yun8effde42017-06-23 19:24:43 +0900893 if c.vndkdep != nil {
894 deps = c.vndkdep.deps(ctx, deps)
895 }
Stephen Craneba090d12017-05-09 15:44:35 -0700896 if c.lto != nil {
897 deps = c.lto.deps(ctx, deps)
898 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700899 for _, feature := range c.features {
900 deps = feature.deps(ctx, deps)
901 }
902
Colin Crossb6715442017-10-24 11:13:31 -0700903 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
904 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
905 deps.LateStaticLibs = android.LastUniqueStrings(deps.LateStaticLibs)
906 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
907 deps.LateSharedLibs = android.LastUniqueStrings(deps.LateSharedLibs)
908 deps.HeaderLibs = android.LastUniqueStrings(deps.HeaderLibs)
Logan Chien43d34c32017-12-20 01:17:32 +0800909 deps.RuntimeLibs = android.LastUniqueStrings(deps.RuntimeLibs)
Colin Crossc99deeb2016-04-11 15:06:20 -0700910
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700911 for _, lib := range deps.ReexportSharedLibHeaders {
912 if !inList(lib, deps.SharedLibs) {
913 ctx.PropertyErrorf("export_shared_lib_headers", "Shared library not in shared_libs: '%s'", lib)
914 }
915 }
916
917 for _, lib := range deps.ReexportStaticLibHeaders {
918 if !inList(lib, deps.StaticLibs) {
919 ctx.PropertyErrorf("export_static_lib_headers", "Static library not in static_libs: '%s'", lib)
920 }
921 }
922
Colin Cross5950f382016-12-13 12:50:57 -0800923 for _, lib := range deps.ReexportHeaderLibHeaders {
924 if !inList(lib, deps.HeaderLibs) {
925 ctx.PropertyErrorf("export_header_lib_headers", "Header library not in header_libs: '%s'", lib)
926 }
927 }
928
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700929 for _, gen := range deps.ReexportGeneratedHeaders {
930 if !inList(gen, deps.GeneratedHeaders) {
931 ctx.PropertyErrorf("export_generated_headers", "Generated header module not in generated_headers: '%s'", gen)
932 }
933 }
934
Colin Crossc99deeb2016-04-11 15:06:20 -0700935 return deps
936}
937
Dan Albert7e9d2952016-08-04 13:02:36 -0700938func (c *Module) beginMutator(actx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800939 ctx := &baseModuleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700940 BaseContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800941 moduleContextImpl: moduleContextImpl{
942 mod: c,
943 },
944 }
945 ctx.ctx = ctx
946
Colin Crossca860ac2016-01-04 14:34:37 -0800947 c.begin(ctx)
Dan Albert7e9d2952016-08-04 13:02:36 -0700948}
949
Jiyong Park7ed9de32018-10-15 22:25:07 +0900950// Split name#version into name and version
951func stubsLibNameAndVersion(name string) (string, string) {
952 if sharp := strings.LastIndex(name, "#"); sharp != -1 && sharp != len(name)-1 {
953 version := name[sharp+1:]
954 libname := name[:sharp]
955 return libname, version
956 }
957 return name, ""
958}
959
Colin Cross1e676be2016-10-12 14:38:15 -0700960func (c *Module) DepsMutator(actx android.BottomUpMutatorContext) {
Colin Cross37047f12016-12-13 17:06:13 -0800961 ctx := &depsContext{
962 BottomUpMutatorContext: actx,
Dan Albert7e9d2952016-08-04 13:02:36 -0700963 moduleContextImpl: moduleContextImpl{
964 mod: c,
965 },
966 }
967 ctx.ctx = ctx
Colin Crossca860ac2016-01-04 14:34:37 -0800968
Colin Crossc99deeb2016-04-11 15:06:20 -0700969 deps := c.deps(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800970
Dan Albert914449f2016-06-17 16:45:24 -0700971 variantNdkLibs := []string{}
972 variantLateNdkLibs := []string{}
Dan Willemsenb916b802017-03-19 13:44:32 -0700973 if ctx.Os() == android.Android {
Dan Albert914449f2016-06-17 16:45:24 -0700974 version := ctx.sdkVersion()
Dan Willemsen72d39932016-07-08 23:23:48 -0700975
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700976 // rewriteNdkLibs takes a list of names of shared libraries and scans it for three types
977 // of names:
Dan Albert914449f2016-06-17 16:45:24 -0700978 //
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700979 // 1. Name of an NDK library that refers to a prebuilt module.
980 // For each of these, it adds the name of the prebuilt module (which will be in
981 // prebuilts/ndk) to the list of nonvariant libs.
982 // 2. Name of an NDK library that refers to an ndk_library module.
983 // For each of these, it adds the name of the ndk_library module to the list of
984 // variant libs.
985 // 3. Anything else (so anything that isn't an NDK library).
986 // It adds these to the nonvariantLibs list.
Dan Albert914449f2016-06-17 16:45:24 -0700987 //
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700988 // The caller can then know to add the variantLibs dependencies differently from the
989 // nonvariantLibs
990 rewriteNdkLibs := func(list []string) (nonvariantLibs []string, variantLibs []string) {
991 variantLibs = []string{}
992 nonvariantLibs = []string{}
Dan Albert914449f2016-06-17 16:45:24 -0700993 for _, entry := range list {
Jiyong Park7ed9de32018-10-15 22:25:07 +0900994 // strip #version suffix out
995 name, _ := stubsLibNameAndVersion(entry)
996 if ctx.useSdk() && inList(name, ndkPrebuiltSharedLibraries) {
997 if !inList(name, ndkMigratedLibs) {
998 nonvariantLibs = append(nonvariantLibs, name+".ndk."+version)
Dan Albert914449f2016-06-17 16:45:24 -0700999 } else {
Jiyong Park7ed9de32018-10-15 22:25:07 +09001000 variantLibs = append(variantLibs, name+ndkLibrarySuffix)
Dan Albert914449f2016-06-17 16:45:24 -07001001 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09001002 } else if ctx.useVndk() && inList(name, llndkLibraries) {
1003 nonvariantLibs = append(nonvariantLibs, name+llndkLibrarySuffix)
1004 } else if (ctx.Platform() || ctx.ProductSpecific()) && inList(name, vendorPublicLibraries) {
1005 vendorPublicLib := name + vendorPublicLibrarySuffix
Jiyong Park374510b2018-03-19 18:23:01 +09001006 if actx.OtherModuleExists(vendorPublicLib) {
1007 nonvariantLibs = append(nonvariantLibs, vendorPublicLib)
1008 } else {
1009 // This can happen if vendor_public_library module is defined in a
1010 // namespace that isn't visible to the current module. In that case,
1011 // link to the original library.
Jiyong Park7ed9de32018-10-15 22:25:07 +09001012 nonvariantLibs = append(nonvariantLibs, name)
Jiyong Park374510b2018-03-19 18:23:01 +09001013 }
Dan Albert914449f2016-06-17 16:45:24 -07001014 } else {
Jiyong Park7ed9de32018-10-15 22:25:07 +09001015 // put name#version back
Dan Willemsen7cbf5f82017-03-28 00:08:30 -07001016 nonvariantLibs = append(nonvariantLibs, entry)
Dan Willemsen72d39932016-07-08 23:23:48 -07001017 }
1018 }
Dan Albert914449f2016-06-17 16:45:24 -07001019 return nonvariantLibs, variantLibs
Dan Willemsen72d39932016-07-08 23:23:48 -07001020 }
1021
Dan Albert914449f2016-06-17 16:45:24 -07001022 deps.SharedLibs, variantNdkLibs = rewriteNdkLibs(deps.SharedLibs)
1023 deps.LateSharedLibs, variantLateNdkLibs = rewriteNdkLibs(deps.LateSharedLibs)
Jiyong Park4c35af02017-07-05 13:41:55 +09001024 deps.ReexportSharedLibHeaders, _ = rewriteNdkLibs(deps.ReexportSharedLibHeaders)
Dan Willemsen72d39932016-07-08 23:23:48 -07001025 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001026
Jiyong Park7ed9de32018-10-15 22:25:07 +09001027 if c.linker != nil {
1028 if library, ok := c.linker.(*libraryDecorator); ok {
1029 if library.buildStubs() {
1030 // Stubs lib does not have dependency to other libraries. Don't proceed.
1031 return
1032 }
1033 }
1034 }
1035
Colin Cross32ec36c2016-12-15 07:39:51 -08001036 for _, lib := range deps.HeaderLibs {
1037 depTag := headerDepTag
1038 if inList(lib, deps.ReexportHeaderLibHeaders) {
1039 depTag = headerExportDepTag
1040 }
1041 actx.AddVariationDependencies(nil, depTag, lib)
1042 }
Colin Cross5950f382016-12-13 12:50:57 -08001043
Dan Willemsen59339a22018-07-22 21:18:45 -07001044 actx.AddVariationDependencies([]blueprint.Variation{
1045 {Mutator: "link", Variation: "static"},
1046 }, wholeStaticDepTag, deps.WholeStaticLibs...)
Colin Crossc99deeb2016-04-11 15:06:20 -07001047
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001048 for _, lib := range deps.StaticLibs {
1049 depTag := staticDepTag
1050 if inList(lib, deps.ReexportStaticLibHeaders) {
1051 depTag = staticExportDepTag
1052 }
Dan Willemsen59339a22018-07-22 21:18:45 -07001053 actx.AddVariationDependencies([]blueprint.Variation{
1054 {Mutator: "link", Variation: "static"},
1055 }, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001056 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001057
Dan Willemsen59339a22018-07-22 21:18:45 -07001058 actx.AddVariationDependencies([]blueprint.Variation{
1059 {Mutator: "link", Variation: "static"},
1060 }, lateStaticDepTag, deps.LateStaticLibs...)
Colin Crossc99deeb2016-04-11 15:06:20 -07001061
Jiyong Park7ed9de32018-10-15 22:25:07 +09001062 // shared lib names without the #version suffix
1063 var sharedLibNames []string
1064
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001065 for _, lib := range deps.SharedLibs {
Jiyong Park7ed9de32018-10-15 22:25:07 +09001066 name, version := stubsLibNameAndVersion(lib)
1067 sharedLibNames = append(sharedLibNames, name)
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001068 depTag := sharedDepTag
1069 if inList(lib, deps.ReexportSharedLibHeaders) {
1070 depTag = sharedExportDepTag
1071 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09001072 var variations []blueprint.Variation
1073 variations = append(variations, blueprint.Variation{Mutator: "link", Variation: "shared"})
1074 if version != "" && ctx.Os() == android.Android && !ctx.useVndk() && !c.inRecovery() {
1075 variations = append(variations, blueprint.Variation{Mutator: "version", Variation: version})
1076 }
1077 actx.AddVariationDependencies(variations, depTag, name)
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001078 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001079
Jiyong Park7ed9de32018-10-15 22:25:07 +09001080 for _, lib := range deps.LateSharedLibs {
1081 name, version := stubsLibNameAndVersion(lib)
1082 if inList(name, sharedLibNames) {
1083 // This is to handle the case that some of the late shared libs (libc, libdl, libm, ...)
1084 // are added also to SharedLibs with version (e.g., libc#10). If not skipped, we will be
1085 // linking against both the stubs lib and the non-stubs lib at the same time.
1086 continue
1087 }
1088 depTag := lateSharedDepTag
1089 var variations []blueprint.Variation
1090 variations = append(variations, blueprint.Variation{Mutator: "link", Variation: "shared"})
1091 if version != "" && ctx.Os() == android.Android && !ctx.useVndk() && !c.inRecovery() {
1092 variations = append(variations, blueprint.Variation{Mutator: "version", Variation: version})
1093 }
1094 actx.AddVariationDependencies(variations, depTag, name)
1095 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001096
Dan Willemsen59339a22018-07-22 21:18:45 -07001097 actx.AddVariationDependencies([]blueprint.Variation{
1098 {Mutator: "link", Variation: "shared"},
1099 }, runtimeDepTag, deps.RuntimeLibs...)
Logan Chien43d34c32017-12-20 01:17:32 +08001100
Colin Cross68861832016-07-08 10:41:41 -07001101 actx.AddDependency(c, genSourceDepTag, deps.GeneratedSources...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -07001102
1103 for _, gen := range deps.GeneratedHeaders {
1104 depTag := genHeaderDepTag
1105 if inList(gen, deps.ReexportGeneratedHeaders) {
1106 depTag = genHeaderExportDepTag
1107 }
1108 actx.AddDependency(c, depTag, gen)
1109 }
Dan Willemsenb40aab62016-04-20 14:21:14 -07001110
Colin Cross42d48b72018-08-29 14:10:52 -07001111 actx.AddVariationDependencies(nil, objDepTag, deps.ObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -07001112
1113 if deps.CrtBegin != "" {
Colin Cross42d48b72018-08-29 14:10:52 -07001114 actx.AddVariationDependencies(nil, crtBeginDepTag, deps.CrtBegin)
Colin Crossca860ac2016-01-04 14:34:37 -08001115 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001116 if deps.CrtEnd != "" {
Colin Cross42d48b72018-08-29 14:10:52 -07001117 actx.AddVariationDependencies(nil, crtEndDepTag, deps.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -07001118 }
Dan Willemsena0790e32018-10-12 00:24:23 -07001119 if deps.LinkerFlagsFile != "" {
1120 actx.AddDependency(c, linkerFlagsDepTag, deps.LinkerFlagsFile)
1121 }
1122 if deps.DynamicLinker != "" {
1123 actx.AddDependency(c, dynamicLinkerDepTag, deps.DynamicLinker)
Dan Willemsenc77a0b32017-09-18 23:19:12 -07001124 }
Dan Albert914449f2016-06-17 16:45:24 -07001125
1126 version := ctx.sdkVersion()
1127 actx.AddVariationDependencies([]blueprint.Variation{
Dan Willemsen59339a22018-07-22 21:18:45 -07001128 {Mutator: "ndk_api", Variation: version},
1129 {Mutator: "link", Variation: "shared"},
1130 }, ndkStubDepTag, variantNdkLibs...)
Dan Albert914449f2016-06-17 16:45:24 -07001131 actx.AddVariationDependencies([]blueprint.Variation{
Dan Willemsen59339a22018-07-22 21:18:45 -07001132 {Mutator: "ndk_api", Variation: version},
1133 {Mutator: "link", Variation: "shared"},
1134 }, ndkLateStubDepTag, variantLateNdkLibs...)
Logan Chienf3511742017-10-31 18:04:35 +08001135
1136 if vndkdep := c.vndkdep; vndkdep != nil {
1137 if vndkdep.isVndkExt() {
1138 baseModuleMode := vendorMode
1139 if actx.DeviceConfig().VndkVersion() == "" {
1140 baseModuleMode = coreMode
1141 }
1142 actx.AddVariationDependencies([]blueprint.Variation{
Dan Willemsen59339a22018-07-22 21:18:45 -07001143 {Mutator: "image", Variation: baseModuleMode},
1144 {Mutator: "link", Variation: "shared"},
1145 }, vndkExtDepTag, vndkdep.getVndkExtendsModuleName())
Logan Chienf3511742017-10-31 18:04:35 +08001146 }
1147 }
Colin Cross6362e272015-10-29 15:25:03 -07001148}
Colin Cross21b9a242015-03-24 14:15:58 -07001149
Colin Crosse40b4ea2018-10-02 22:25:58 -07001150func BeginMutator(ctx android.BottomUpMutatorContext) {
Dan Albert7e9d2952016-08-04 13:02:36 -07001151 if c, ok := ctx.Module().(*Module); ok && c.Enabled() {
1152 c.beginMutator(ctx)
1153 }
1154}
1155
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001156// Whether a module can link to another module, taking into
1157// account NDK linking.
Logan Chienf3511742017-10-31 18:04:35 +08001158func checkLinkType(ctx android.ModuleContext, from *Module, to *Module, tag dependencyTag) {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001159 if from.Target().Os != android.Android {
1160 // Host code is not restricted
1161 return
1162 }
1163 if from.Properties.UseVndk {
1164 // Though vendor code is limited by the vendor mutator,
1165 // each vendor-available module needs to check
1166 // link-type for VNDK.
1167 if from.vndkdep != nil {
Logan Chienf3511742017-10-31 18:04:35 +08001168 from.vndkdep.vndkCheckLinkType(ctx, to, tag)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001169 }
1170 return
1171 }
Nan Zhang0007d812017-11-07 10:57:05 -08001172 if String(from.Properties.Sdk_version) == "" {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001173 // Platform code can link to anything
1174 return
1175 }
Jiyong Parkf9332f12018-02-01 00:54:12 +09001176 if from.inRecovery() {
1177 // Recovery code is not NDK
1178 return
1179 }
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001180 if _, ok := to.linker.(*toolchainLibraryDecorator); ok {
1181 // These are always allowed
1182 return
1183 }
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001184 if _, ok := to.linker.(*ndkPrebuiltStlLinker); ok {
1185 // These are allowed, but they don't set sdk_version
1186 return
1187 }
1188 if _, ok := to.linker.(*stubDecorator); ok {
1189 // These aren't real libraries, but are the stub shared libraries that are included in
1190 // the NDK.
1191 return
1192 }
Nan Zhang0007d812017-11-07 10:57:05 -08001193 if String(to.Properties.Sdk_version) == "" {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001194 // NDK code linking to platform code is never okay.
1195 ctx.ModuleErrorf("depends on non-NDK-built library %q",
1196 ctx.OtherModuleName(to))
1197 }
1198
1199 // At this point we know we have two NDK libraries, but we need to
1200 // check that we're not linking against anything built against a higher
1201 // API level, as it is only valid to link against older or equivalent
1202 // APIs.
1203
Inseob Kim01a28722018-04-11 09:48:45 +09001204 // Current can link against anything.
1205 if String(from.Properties.Sdk_version) != "current" {
1206 // Otherwise we need to check.
1207 if String(to.Properties.Sdk_version) == "current" {
1208 // Current can't be linked against by anything else.
1209 ctx.ModuleErrorf("links %q built against newer API version %q",
1210 ctx.OtherModuleName(to), "current")
1211 } else {
1212 fromApi, err := strconv.Atoi(String(from.Properties.Sdk_version))
1213 if err != nil {
1214 ctx.PropertyErrorf("sdk_version",
Inseob Kim34b22832018-04-11 10:13:16 +09001215 "Invalid sdk_version value (must be int or current): %q",
Inseob Kim01a28722018-04-11 09:48:45 +09001216 String(from.Properties.Sdk_version))
1217 }
1218 toApi, err := strconv.Atoi(String(to.Properties.Sdk_version))
1219 if err != nil {
1220 ctx.PropertyErrorf("sdk_version",
Inseob Kim34b22832018-04-11 10:13:16 +09001221 "Invalid sdk_version value (must be int or current): %q",
Inseob Kim01a28722018-04-11 09:48:45 +09001222 String(to.Properties.Sdk_version))
1223 }
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001224
Inseob Kim01a28722018-04-11 09:48:45 +09001225 if toApi > fromApi {
1226 ctx.ModuleErrorf("links %q built against newer API version %q",
1227 ctx.OtherModuleName(to), String(to.Properties.Sdk_version))
1228 }
1229 }
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001230 }
Dan Albert202fe492017-12-15 13:56:59 -08001231
1232 // Also check that the two STL choices are compatible.
1233 fromStl := from.stl.Properties.SelectedStl
1234 toStl := to.stl.Properties.SelectedStl
1235 if fromStl == "" || toStl == "" {
1236 // Libraries that don't use the STL are unrestricted.
Inseob Kimda2171a2018-04-11 15:41:38 +09001237 } else if fromStl == "ndk_system" || toStl == "ndk_system" {
Dan Albert202fe492017-12-15 13:56:59 -08001238 // We can be permissive with the system "STL" since it is only the C++
1239 // ABI layer, but in the future we should make sure that everyone is
1240 // using either libc++ or nothing.
Colin Crossb60190a2018-09-04 16:28:17 -07001241 } else if getNdkStlFamily(from) != getNdkStlFamily(to) {
Dan Albert202fe492017-12-15 13:56:59 -08001242 ctx.ModuleErrorf("uses %q and depends on %q which uses incompatible %q",
1243 from.stl.Properties.SelectedStl, ctx.OtherModuleName(to),
1244 to.stl.Properties.SelectedStl)
1245 }
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001246}
1247
Jiyong Park5fb8c102018-04-09 12:03:06 +09001248// Tests whether the dependent library is okay to be double loaded inside a single process.
1249// If a library is a member of VNDK and at the same time dependencies of an LLNDK library,
1250// it is subject to be double loaded. Such lib should be explicitly marked as double_loaded: true
1251// or as vndk-sp (vndk: { enabled: true, support_system_process: true}).
1252func checkDoubleLoadableLibries(ctx android.ModuleContext, from *Module, to *Module) {
1253 if _, ok := from.linker.(*libraryDecorator); !ok {
1254 return
1255 }
1256
1257 if inList(ctx.ModuleName(), llndkLibraries) ||
1258 (from.useVndk() && Bool(from.VendorProperties.Double_loadable)) {
1259 _, depIsLlndk := to.linker.(*llndkStubDecorator)
1260 depIsVndkSp := false
1261 if to.vndkdep != nil && to.vndkdep.isVndkSp() {
1262 depIsVndkSp = true
1263 }
1264 depIsVndk := false
1265 if to.vndkdep != nil && to.vndkdep.isVndk() {
1266 depIsVndk = true
1267 }
1268 depIsDoubleLoadable := Bool(to.VendorProperties.Double_loadable)
1269 if !depIsLlndk && !depIsVndkSp && !depIsDoubleLoadable && depIsVndk {
1270 ctx.ModuleErrorf("links VNDK library %q that isn't double_loadable.",
1271 ctx.OtherModuleName(to))
1272 }
1273 }
1274}
1275
Colin Crossc99deeb2016-04-11 15:06:20 -07001276// Convert dependencies to paths. Returns a PathDeps containing paths
Colin Cross635c3b02016-05-18 15:37:25 -07001277func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
Colin Crossca860ac2016-01-04 14:34:37 -08001278 var depPaths PathDeps
Colin Crossca860ac2016-01-04 14:34:37 -08001279
Jeff Gaston294356f2017-09-27 17:05:30 -07001280 directStaticDeps := []*Module{}
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001281 directSharedDeps := []*Module{}
Jeff Gaston294356f2017-09-27 17:05:30 -07001282
Colin Crossd11fcda2017-10-23 17:59:01 -07001283 ctx.VisitDirectDeps(func(dep android.Module) {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001284 depName := ctx.OtherModuleName(dep)
1285 depTag := ctx.OtherModuleDependencyTag(dep)
Dan Albert9e10cd42016-08-03 14:12:14 -07001286
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001287 ccDep, _ := dep.(*Module)
1288 if ccDep == nil {
1289 // handling for a few module types that aren't cc Module but that are also supported
1290 switch depTag {
Dan Willemsenb40aab62016-04-20 14:21:14 -07001291 case genSourceDepTag:
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001292 if genRule, ok := dep.(genrule.SourceFileGenerator); ok {
Dan Willemsenb40aab62016-04-20 14:21:14 -07001293 depPaths.GeneratedSources = append(depPaths.GeneratedSources,
1294 genRule.GeneratedSourceFiles()...)
1295 } else {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001296 ctx.ModuleErrorf("module %q is not a gensrcs or genrule", depName)
Dan Willemsenb40aab62016-04-20 14:21:14 -07001297 }
Colin Crosse90bfd12017-04-26 16:59:26 -07001298 // Support exported headers from a generated_sources dependency
1299 fallthrough
Dan Willemsenb3454ab2016-09-28 17:34:58 -07001300 case genHeaderDepTag, genHeaderExportDepTag:
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001301 if genRule, ok := dep.(genrule.SourceFileGenerator); ok {
Dan Willemsenb40aab62016-04-20 14:21:14 -07001302 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders,
Dan Willemsen9da9d492018-02-21 18:28:18 -08001303 genRule.GeneratedDeps()...)
Colin Cross5ed99c62016-11-22 12:55:55 -08001304 flags := includeDirsToFlags(genRule.GeneratedHeaderDirs())
Dan Willemsenb3454ab2016-09-28 17:34:58 -07001305 depPaths.Flags = append(depPaths.Flags, flags)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001306 if depTag == genHeaderExportDepTag {
Dan Willemsenb3454ab2016-09-28 17:34:58 -07001307 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags)
Dan Willemsen847dcc72016-09-29 12:13:36 -07001308 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps,
Dan Willemsen9da9d492018-02-21 18:28:18 -08001309 genRule.GeneratedDeps()...)
Jayant Chowdhary715cac32017-04-20 06:53:59 -07001310 // Add these re-exported flags to help header-abi-dumper to infer the abi exported by a library.
1311 c.sabi.Properties.ReexportedIncludeFlags = append(c.sabi.Properties.ReexportedIncludeFlags, flags)
1312
Dan Willemsenb3454ab2016-09-28 17:34:58 -07001313 }
Dan Willemsenb40aab62016-04-20 14:21:14 -07001314 } else {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001315 ctx.ModuleErrorf("module %q is not a genrule", depName)
Dan Willemsenb40aab62016-04-20 14:21:14 -07001316 }
Dan Willemsena0790e32018-10-12 00:24:23 -07001317 case linkerFlagsDepTag:
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001318 if genRule, ok := dep.(genrule.SourceFileGenerator); ok {
Dan Willemsenc77a0b32017-09-18 23:19:12 -07001319 files := genRule.GeneratedSourceFiles()
1320 if len(files) == 1 {
Dan Willemsena0790e32018-10-12 00:24:23 -07001321 depPaths.LinkerFlagsFile = android.OptionalPathForPath(files[0])
Dan Willemsenc77a0b32017-09-18 23:19:12 -07001322 } else if len(files) > 1 {
Dan Willemsena0790e32018-10-12 00:24:23 -07001323 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 -07001324 }
1325 } else {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001326 ctx.ModuleErrorf("module %q is not a genrule", depName)
Dan Willemsenc77a0b32017-09-18 23:19:12 -07001327 }
Colin Crossca860ac2016-01-04 14:34:37 -08001328 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001329 return
1330 }
1331
Colin Crossd11fcda2017-10-23 17:59:01 -07001332 if dep.Target().Os != ctx.Os() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001333 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1334 return
1335 }
Colin Crossd11fcda2017-10-23 17:59:01 -07001336 if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001337 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
Colin Crossa1ad8d12016-06-01 17:09:44 -07001338 return
1339 }
1340
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001341 // re-exporting flags
1342 if depTag == reuseObjTag {
1343 if l, ok := ccDep.compiler.(libraryInterface); ok {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001344 c.staticVariant = ccDep
Colin Crossbbc9f4d2017-05-03 16:24:55 -07001345 objs, flags, deps := l.reuseObjs()
Colin Cross10d22312017-05-03 11:01:58 -07001346 depPaths.Objs = depPaths.Objs.Append(objs)
1347 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Colin Crossbbc9f4d2017-05-03 16:24:55 -07001348 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps, deps...)
Colin Crossbba99042016-11-23 15:45:05 -08001349 return
1350 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001351 }
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001352 if t, ok := depTag.(dependencyTag); ok && t.library {
1353 if i, ok := ccDep.linker.(exportedFlagsProducer); ok {
Dan Willemsen76f08272016-07-09 00:14:08 -07001354 flags := i.exportedFlags()
Dan Willemsen847dcc72016-09-29 12:13:36 -07001355 deps := i.exportedFlagsDeps()
Dan Willemsen76f08272016-07-09 00:14:08 -07001356 depPaths.Flags = append(depPaths.Flags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -07001357 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders, deps...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001358
1359 if t.reexportFlags {
Dan Willemsen76f08272016-07-09 00:14:08 -07001360 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -07001361 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps, deps...)
Jayant Chowdhary715cac32017-04-20 06:53:59 -07001362 // 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 -07001363 // Re-exported shared library headers must be included as well since they can help us with type information
1364 // about template instantiations (instantiated from their headers).
1365 c.sabi.Properties.ReexportedIncludeFlags = append(c.sabi.Properties.ReexportedIncludeFlags, flags...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001366 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001367 }
Dan Willemsena96ff642016-06-07 12:34:45 -07001368
Logan Chienf3511742017-10-31 18:04:35 +08001369 checkLinkType(ctx, c, ccDep, t)
Jiyong Park5fb8c102018-04-09 12:03:06 +09001370 checkDoubleLoadableLibries(ctx, c, ccDep)
Colin Crossc99deeb2016-04-11 15:06:20 -07001371 }
1372
Colin Cross26c34ed2016-09-30 17:10:16 -07001373 var ptr *android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -07001374 var depPtr *android.Paths
Colin Crossc99deeb2016-04-11 15:06:20 -07001375
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001376 linkFile := ccDep.outputFile
Colin Cross26c34ed2016-09-30 17:10:16 -07001377 depFile := android.OptionalPath{}
1378
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001379 switch depTag {
Dan Albert914449f2016-06-17 16:45:24 -07001380 case ndkStubDepTag, sharedDepTag, sharedExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001381 ptr = &depPaths.SharedLibs
1382 depPtr = &depPaths.SharedLibsDeps
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001383 depFile = ccDep.linker.(libraryInterface).toc()
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001384 directSharedDeps = append(directSharedDeps, ccDep)
Dan Albert914449f2016-06-17 16:45:24 -07001385 case lateSharedDepTag, ndkLateStubDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001386 ptr = &depPaths.LateSharedLibs
1387 depPtr = &depPaths.LateSharedLibsDeps
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001388 depFile = ccDep.linker.(libraryInterface).toc()
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001389 case staticDepTag, staticExportDepTag:
Jeff Gaston294356f2017-09-27 17:05:30 -07001390 ptr = nil
1391 directStaticDeps = append(directStaticDeps, ccDep)
Colin Crossc99deeb2016-04-11 15:06:20 -07001392 case lateStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001393 ptr = &depPaths.LateStaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -07001394 case wholeStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001395 ptr = &depPaths.WholeStaticLibs
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001396 staticLib, ok := ccDep.linker.(libraryInterface)
Colin Crossb916a382016-07-29 17:28:03 -07001397 if !ok || !staticLib.static() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001398 ctx.ModuleErrorf("module %q not a static library", depName)
Colin Crossc99deeb2016-04-11 15:06:20 -07001399 return
1400 }
1401
1402 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001403 postfix := " (required by " + ctx.OtherModuleName(dep) + ")"
Colin Crossc99deeb2016-04-11 15:06:20 -07001404 for i := range missingDeps {
1405 missingDeps[i] += postfix
1406 }
1407 ctx.AddMissingDependencies(missingDeps)
1408 }
Dan Willemsen5cb580f2016-09-26 17:33:01 -07001409 depPaths.WholeStaticLibObjs = depPaths.WholeStaticLibObjs.Append(staticLib.objs())
Colin Cross5950f382016-12-13 12:50:57 -08001410 case headerDepTag:
1411 // Nothing
Colin Crossc99deeb2016-04-11 15:06:20 -07001412 case objDepTag:
Dan Willemsen5cb580f2016-09-26 17:33:01 -07001413 depPaths.Objs.objFiles = append(depPaths.Objs.objFiles, linkFile.Path())
Colin Crossc99deeb2016-04-11 15:06:20 -07001414 case crtBeginDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001415 depPaths.CrtBegin = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -07001416 case crtEndDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001417 depPaths.CrtEnd = linkFile
Dan Willemsena0790e32018-10-12 00:24:23 -07001418 case dynamicLinkerDepTag:
1419 depPaths.DynamicLinker = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -07001420 }
1421
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001422 switch depTag {
Dan Willemsen581341d2017-02-09 16:16:31 -08001423 case staticDepTag, staticExportDepTag, lateStaticDepTag:
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001424 staticLib, ok := ccDep.linker.(libraryInterface)
Dan Willemsen581341d2017-02-09 16:16:31 -08001425 if !ok || !staticLib.static() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001426 ctx.ModuleErrorf("module %q not a static library", depName)
Dan Willemsen581341d2017-02-09 16:16:31 -08001427 return
1428 }
1429
1430 // When combining coverage files for shared libraries and executables, coverage files
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001431 // in static libraries act as if they were whole static libraries. The same goes for
1432 // source based Abi dump files.
Dan Willemsen581341d2017-02-09 16:16:31 -08001433 depPaths.StaticLibObjs.coverageFiles = append(depPaths.StaticLibObjs.coverageFiles,
1434 staticLib.objs().coverageFiles...)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001435 depPaths.StaticLibObjs.sAbiDumpFiles = append(depPaths.StaticLibObjs.sAbiDumpFiles,
1436 staticLib.objs().sAbiDumpFiles...)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001437
Dan Willemsen581341d2017-02-09 16:16:31 -08001438 }
1439
Colin Cross26c34ed2016-09-30 17:10:16 -07001440 if ptr != nil {
Colin Crossce75d2c2016-10-06 16:12:58 -07001441 if !linkFile.Valid() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001442 ctx.ModuleErrorf("module %q missing output file", depName)
Colin Crossce75d2c2016-10-06 16:12:58 -07001443 return
1444 }
Colin Cross26c34ed2016-09-30 17:10:16 -07001445 *ptr = append(*ptr, linkFile.Path())
1446 }
1447
Colin Crossc99deeb2016-04-11 15:06:20 -07001448 if depPtr != nil {
Colin Cross26c34ed2016-09-30 17:10:16 -07001449 dep := depFile
1450 if !dep.Valid() {
1451 dep = linkFile
1452 }
1453 *depPtr = append(*depPtr, dep.Path())
Colin Crossca860ac2016-01-04 14:34:37 -08001454 }
Jiyong Park27b188b2017-07-18 13:23:39 +09001455
Logan Chien43d34c32017-12-20 01:17:32 +08001456 makeLibName := func(depName string) string {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001457 libName := strings.TrimSuffix(depName, llndkLibrarySuffix)
Jiyong Park374510b2018-03-19 18:23:01 +09001458 libName = strings.TrimSuffix(libName, vendorPublicLibrarySuffix)
Jiyong Park27b188b2017-07-18 13:23:39 +09001459 libName = strings.TrimPrefix(libName, "prebuilt_")
Jiyong Parkd5b18a52017-08-03 21:22:50 +09001460 isLLndk := inList(libName, llndkLibraries)
Jiyong Park374510b2018-03-19 18:23:01 +09001461 isVendorPublicLib := inList(libName, vendorPublicLibraries)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001462 bothVendorAndCoreVariantsExist := ccDep.hasVendorVariant() || isLLndk
1463 if c.useVndk() && bothVendorAndCoreVariantsExist {
1464 // The vendor module in Make will have been renamed to not conflict with the core
1465 // module, so update the dependency name here accordingly.
Logan Chien43d34c32017-12-20 01:17:32 +08001466 return libName + vendorSuffix
Jiyong Park374510b2018-03-19 18:23:01 +09001467 } else if (ctx.Platform() || ctx.ProductSpecific()) && isVendorPublicLib {
Logan Chien43d34c32017-12-20 01:17:32 +08001468 return libName + vendorPublicLibrarySuffix
Jiyong Parkf9332f12018-02-01 00:54:12 +09001469 } else if ccDep.inRecovery() && !ccDep.onlyInRecovery() {
1470 return libName + recoverySuffix
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001471 } else {
Logan Chien43d34c32017-12-20 01:17:32 +08001472 return libName
Jiyong Park27b188b2017-07-18 13:23:39 +09001473 }
Logan Chien43d34c32017-12-20 01:17:32 +08001474 }
1475
1476 // Export the shared libs to Make.
1477 switch depTag {
1478 case sharedDepTag, sharedExportDepTag, lateSharedDepTag:
Jiyong Park27b188b2017-07-18 13:23:39 +09001479 // Note: the order of libs in this list is not important because
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001480 // they merely serve as Make dependencies and do not affect this lib itself.
Logan Chien43d34c32017-12-20 01:17:32 +08001481 c.Properties.AndroidMkSharedLibs = append(
1482 c.Properties.AndroidMkSharedLibs, makeLibName(depName))
Jaewoong Jungeb05c2a2018-11-09 16:02:23 +00001483 case staticDepTag, staticExportDepTag, lateStaticDepTag:
1484 c.Properties.AndroidMkStaticLibs = append(
1485 c.Properties.AndroidMkStaticLibs, makeLibName(depName))
Logan Chien43d34c32017-12-20 01:17:32 +08001486 case runtimeDepTag:
1487 c.Properties.AndroidMkRuntimeLibs = append(
1488 c.Properties.AndroidMkRuntimeLibs, makeLibName(depName))
Jaewoong Jungeb05c2a2018-11-09 16:02:23 +00001489 case wholeStaticDepTag:
1490 c.Properties.AndroidMkWholeStaticLibs = append(
1491 c.Properties.AndroidMkWholeStaticLibs, makeLibName(depName))
Jiyong Park27b188b2017-07-18 13:23:39 +09001492 }
Colin Crossca860ac2016-01-04 14:34:37 -08001493 })
1494
Jeff Gaston294356f2017-09-27 17:05:30 -07001495 // use the ordered dependencies as this module's dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001496 depPaths.StaticLibs = append(depPaths.StaticLibs, orderStaticModuleDeps(c, directStaticDeps, directSharedDeps)...)
Jeff Gaston294356f2017-09-27 17:05:30 -07001497
Colin Crossdd84e052017-05-17 13:44:16 -07001498 // Dedup exported flags from dependencies
Colin Crossb6715442017-10-24 11:13:31 -07001499 depPaths.Flags = android.FirstUniqueStrings(depPaths.Flags)
Dan Willemsenfe92c962017-08-29 12:28:37 -07001500 depPaths.GeneratedHeaders = android.FirstUniquePaths(depPaths.GeneratedHeaders)
Colin Crossb6715442017-10-24 11:13:31 -07001501 depPaths.ReexportedFlags = android.FirstUniqueStrings(depPaths.ReexportedFlags)
Dan Willemsenfe92c962017-08-29 12:28:37 -07001502 depPaths.ReexportedFlagsDeps = android.FirstUniquePaths(depPaths.ReexportedFlagsDeps)
1503
1504 if c.sabi != nil {
Colin Crossb6715442017-10-24 11:13:31 -07001505 c.sabi.Properties.ReexportedIncludeFlags = android.FirstUniqueStrings(c.sabi.Properties.ReexportedIncludeFlags)
Dan Willemsenfe92c962017-08-29 12:28:37 -07001506 }
Colin Crossdd84e052017-05-17 13:44:16 -07001507
Colin Crossca860ac2016-01-04 14:34:37 -08001508 return depPaths
1509}
1510
1511func (c *Module) InstallInData() bool {
1512 if c.installer == nil {
1513 return false
1514 }
Vishwath Mohan1dd88392017-03-29 22:00:18 -07001515 return c.installer.inData()
1516}
1517
1518func (c *Module) InstallInSanitizerDir() bool {
1519 if c.installer == nil {
1520 return false
1521 }
1522 if c.sanitize != nil && c.sanitize.inSanitizerDir() {
Colin Cross94610402016-08-29 13:41:32 -07001523 return true
1524 }
Vishwath Mohan1dd88392017-03-29 22:00:18 -07001525 return c.installer.inSanitizerDir()
Colin Crossca860ac2016-01-04 14:34:37 -08001526}
1527
Jiyong Parkf9332f12018-02-01 00:54:12 +09001528func (c *Module) InstallInRecovery() bool {
1529 return c.inRecovery()
1530}
1531
Dan Willemsen4aa75ca2016-09-28 16:18:03 -07001532func (c *Module) HostToolPath() android.OptionalPath {
1533 if c.installer == nil {
1534 return android.OptionalPath{}
1535 }
1536 return c.installer.hostToolPath()
1537}
1538
Nan Zhangd4e641b2017-07-12 12:55:28 -07001539func (c *Module) IntermPathForModuleOut() android.OptionalPath {
1540 return c.outputFile
1541}
1542
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001543func (c *Module) Srcs() android.Paths {
1544 if c.outputFile.Valid() {
1545 return android.Paths{c.outputFile.Path()}
1546 }
1547 return android.Paths{}
1548}
1549
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001550func (c *Module) static() bool {
1551 if static, ok := c.linker.(interface {
1552 static() bool
1553 }); ok {
1554 return static.static()
1555 }
1556 return false
1557}
1558
Colin Crossb60190a2018-09-04 16:28:17 -07001559func (c *Module) getMakeLinkType() string {
1560 if c.useVndk() {
1561 if inList(c.Name(), vndkCoreLibraries) || inList(c.Name(), vndkSpLibraries) || inList(c.Name(), llndkLibraries) {
1562 if inList(c.Name(), vndkPrivateLibraries) {
1563 return "native:vndk_private"
1564 } else {
1565 return "native:vndk"
1566 }
1567 } else {
1568 return "native:vendor"
1569 }
1570 } else if c.inRecovery() {
1571 return "native:recovery"
1572 } else if c.Target().Os == android.Android && String(c.Properties.Sdk_version) != "" {
1573 return "native:ndk:none:none"
1574 // TODO(b/114741097): use the correct ndk stl once build errors have been fixed
1575 //family, link := getNdkStlFamilyAndLinkType(c)
1576 //return fmt.Sprintf("native:ndk:%s:%s", family, link)
1577 } else {
1578 return "native:platform"
1579 }
1580}
1581
Jiyong Park9d452992018-10-03 00:38:19 +09001582// Overrides ApexModule.IsInstallabeToApex()
1583// Only shared libraries are installable to APEX.
1584func (c *Module) IsInstallableToApex() bool {
1585 if shared, ok := c.linker.(interface {
1586 shared() bool
1587 }); ok {
1588 return shared.shared()
1589 }
1590 return false
1591}
1592
Colin Cross2ba19d92015-05-07 15:44:20 -07001593//
Colin Crosscfad1192015-11-02 16:43:11 -08001594// Defaults
1595//
Colin Crossca860ac2016-01-04 14:34:37 -08001596type Defaults struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001597 android.ModuleBase
Colin Cross1f44a3a2017-07-07 14:33:33 -07001598 android.DefaultsModuleBase
Jiyong Park9d452992018-10-03 00:38:19 +09001599 android.ApexModuleBase
Colin Crosscfad1192015-11-02 16:43:11 -08001600}
1601
Colin Cross635c3b02016-05-18 15:37:25 -07001602func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -08001603}
1604
Colin Cross1e676be2016-10-12 14:38:15 -07001605func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
1606}
1607
Colin Cross36242852017-06-23 15:06:31 -07001608func defaultsFactory() android.Module {
Colin Crosse1d764e2016-08-18 14:18:32 -07001609 return DefaultsFactory()
1610}
1611
Colin Cross36242852017-06-23 15:06:31 -07001612func DefaultsFactory(props ...interface{}) android.Module {
Colin Crossca860ac2016-01-04 14:34:37 -08001613 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -08001614
Colin Cross36242852017-06-23 15:06:31 -07001615 module.AddProperties(props...)
1616 module.AddProperties(
Colin Crossca860ac2016-01-04 14:34:37 -08001617 &BaseProperties{},
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001618 &VendorProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001619 &BaseCompilerProperties{},
1620 &BaseLinkerProperties{},
Chih-Hung Hsieh86814712018-05-23 18:30:46 -07001621 &MoreBaseLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -07001622 &LibraryProperties{},
Colin Cross919281a2016-04-05 16:42:05 -07001623 &FlagExporterProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001624 &BinaryLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -07001625 &TestProperties{},
1626 &TestBinaryProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001627 &StlProperties{},
Colin Cross16b23492016-01-06 14:41:07 -08001628 &SanitizeProperties{},
Colin Cross665dce92016-04-28 14:50:03 -07001629 &StripProperties{},
Dan Willemsen7424d612016-09-01 13:45:39 -07001630 &InstallerProperties{},
Dan Willemsena03cf6d2016-09-26 15:45:04 -07001631 &TidyProperties{},
Dan Willemsen581341d2017-02-09 16:16:31 -08001632 &CoverageProperties{},
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001633 &SAbiProperties{},
Justin Yun4b2382f2017-07-26 14:22:10 +09001634 &VndkProperties{},
Stephen Craneba090d12017-05-09 15:44:35 -07001635 &LTOProperties{},
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -07001636 &PgoProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08001637 &android.ProtoProperties{},
Colin Crosse1d764e2016-08-18 14:18:32 -07001638 )
Colin Crosscfad1192015-11-02 16:43:11 -08001639
Colin Cross1f44a3a2017-07-07 14:33:33 -07001640 android.InitDefaultsModule(module)
Jiyong Park9d452992018-10-03 00:38:19 +09001641 android.InitApexModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001642
1643 return module
Colin Crosscfad1192015-11-02 16:43:11 -08001644}
1645
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001646const (
1647 // coreMode is the variant used for framework-private libraries, or
1648 // SDK libraries. (which framework-private libraries can use)
1649 coreMode = "core"
1650
1651 // vendorMode is the variant used for /vendor code that compiles
1652 // against the VNDK.
1653 vendorMode = "vendor"
Jiyong Parkf9332f12018-02-01 00:54:12 +09001654
1655 recoveryMode = "recovery"
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001656)
1657
Jiyong Park6a43f042017-10-12 23:05:00 +09001658func squashVendorSrcs(m *Module) {
1659 if lib, ok := m.compiler.(*libraryDecorator); ok {
1660 lib.baseCompiler.Properties.Srcs = append(lib.baseCompiler.Properties.Srcs,
1661 lib.baseCompiler.Properties.Target.Vendor.Srcs...)
1662
1663 lib.baseCompiler.Properties.Exclude_srcs = append(lib.baseCompiler.Properties.Exclude_srcs,
1664 lib.baseCompiler.Properties.Target.Vendor.Exclude_srcs...)
1665 }
1666}
1667
Jiyong Parkf9332f12018-02-01 00:54:12 +09001668func squashRecoverySrcs(m *Module) {
1669 if lib, ok := m.compiler.(*libraryDecorator); ok {
1670 lib.baseCompiler.Properties.Srcs = append(lib.baseCompiler.Properties.Srcs,
1671 lib.baseCompiler.Properties.Target.Recovery.Srcs...)
1672
1673 lib.baseCompiler.Properties.Exclude_srcs = append(lib.baseCompiler.Properties.Exclude_srcs,
1674 lib.baseCompiler.Properties.Target.Recovery.Exclude_srcs...)
1675 }
1676}
1677
1678func imageMutator(mctx android.BottomUpMutatorContext) {
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001679 if mctx.Os() != android.Android {
1680 return
1681 }
1682
Jiyong Park7ed9de32018-10-15 22:25:07 +09001683 if g, ok := mctx.Module().(*genrule.Module); ok {
1684 if props, ok := g.Extra.(*GenruleExtraProperties); ok {
Jiyong Park3f736c92018-05-24 13:36:56 +09001685 var coreVariantNeeded bool = false
1686 var vendorVariantNeeded bool = false
1687 var recoveryVariantNeeded bool = false
Justin Yun71549282017-11-17 12:10:28 +09001688 if mctx.DeviceConfig().VndkVersion() == "" {
Jiyong Park3f736c92018-05-24 13:36:56 +09001689 coreVariantNeeded = true
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001690 } else if Bool(props.Vendor_available) {
Jiyong Park3f736c92018-05-24 13:36:56 +09001691 coreVariantNeeded = true
1692 vendorVariantNeeded = true
Jiyong Park2db76922017-11-08 16:03:48 +09001693 } else if mctx.SocSpecific() || mctx.DeviceSpecific() {
Jiyong Park3f736c92018-05-24 13:36:56 +09001694 vendorVariantNeeded = true
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001695 } else {
Jiyong Park3f736c92018-05-24 13:36:56 +09001696 coreVariantNeeded = true
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001697 }
Jiyong Park3f736c92018-05-24 13:36:56 +09001698 if Bool(props.Recovery_available) {
1699 recoveryVariantNeeded = true
1700 }
1701
Jiyong Park413cc742018-06-19 01:46:06 +09001702 if recoveryVariantNeeded {
Jiyong Park8d52f862018-07-07 18:02:07 +09001703 primaryArch := mctx.Config().DevicePrimaryArchType()
Jiyong Park7ed9de32018-10-15 22:25:07 +09001704 moduleArch := g.Target().Arch.ArchType
Jiyong Park8d52f862018-07-07 18:02:07 +09001705 if moduleArch != primaryArch {
Jiyong Park413cc742018-06-19 01:46:06 +09001706 recoveryVariantNeeded = false
1707 }
1708 }
1709
Jiyong Park3f736c92018-05-24 13:36:56 +09001710 var variants []string
1711 if coreVariantNeeded {
1712 variants = append(variants, coreMode)
1713 }
1714 if vendorVariantNeeded {
1715 variants = append(variants, vendorMode)
1716 }
1717 if recoveryVariantNeeded {
1718 variants = append(variants, recoveryMode)
1719 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09001720 mod := mctx.CreateVariations(variants...)
1721 for i, v := range variants {
1722 if v == recoveryMode {
1723 m := mod[i].(*genrule.Module)
1724 m.Extra.(*GenruleExtraProperties).InRecovery = true
1725 }
1726 }
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001727 }
1728 }
1729
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001730 m, ok := mctx.Module().(*Module)
1731 if !ok {
1732 return
1733 }
1734
1735 // Sanity check
Logan Chienf3511742017-10-31 18:04:35 +08001736 vendorSpecific := mctx.SocSpecific() || mctx.DeviceSpecific()
1737
1738 if m.VendorProperties.Vendor_available != nil && vendorSpecific {
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001739 mctx.PropertyErrorf("vendor_available",
Jiyong Park2db76922017-11-08 16:03:48 +09001740 "doesn't make sense at the same time as `vendor: true`, `proprietary: true`, or `device_specific:true`")
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001741 return
1742 }
Logan Chienf3511742017-10-31 18:04:35 +08001743
1744 if vndkdep := m.vndkdep; vndkdep != nil {
1745 if vndkdep.isVndk() {
1746 if vendorSpecific {
1747 if !vndkdep.isVndkExt() {
1748 mctx.PropertyErrorf("vndk",
1749 "must set `extends: \"...\"` to vndk extension")
1750 return
1751 }
1752 } else {
1753 if vndkdep.isVndkExt() {
1754 mctx.PropertyErrorf("vndk",
1755 "must set `vendor: true` to set `extends: %q`",
1756 m.getVndkExtendsModuleName())
1757 return
1758 }
1759 if m.VendorProperties.Vendor_available == nil {
1760 mctx.PropertyErrorf("vndk",
1761 "vendor_available must be set to either true or false when `vndk: {enabled: true}`")
1762 return
1763 }
1764 }
1765 } else {
1766 if vndkdep.isVndkSp() {
1767 mctx.PropertyErrorf("vndk",
1768 "must set `enabled: true` to set `support_system_process: true`")
1769 return
1770 }
1771 if vndkdep.isVndkExt() {
1772 mctx.PropertyErrorf("vndk",
1773 "must set `enabled: true` to set `extends: %q`",
1774 m.getVndkExtendsModuleName())
1775 return
1776 }
Justin Yun8effde42017-06-23 19:24:43 +09001777 }
1778 }
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001779
Jiyong Parkf9332f12018-02-01 00:54:12 +09001780 var coreVariantNeeded bool = false
1781 var vendorVariantNeeded bool = false
1782 var recoveryVariantNeeded bool = false
1783
Justin Yun71549282017-11-17 12:10:28 +09001784 if mctx.DeviceConfig().VndkVersion() == "" {
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001785 // If the device isn't compiling against the VNDK, we always
1786 // use the core mode.
Jiyong Parkf9332f12018-02-01 00:54:12 +09001787 coreVariantNeeded = true
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001788 } else if _, ok := m.linker.(*llndkStubDecorator); ok {
1789 // LL-NDK stubs only exist in the vendor variant, since the
1790 // real libraries will be used in the core variant.
Jiyong Parkf9332f12018-02-01 00:54:12 +09001791 vendorVariantNeeded = true
Jiyong Park2a454122017-10-19 15:59:33 +09001792 } else if _, ok := m.linker.(*llndkHeadersDecorator); ok {
1793 // ... and LL-NDK headers as well
Jiyong Parkf9332f12018-02-01 00:54:12 +09001794 vendorVariantNeeded = true
Justin Yun312ccb92018-01-23 12:07:46 +09001795 } else if _, ok := m.linker.(*vndkPrebuiltLibraryDecorator); ok {
Justin Yun71549282017-11-17 12:10:28 +09001796 // Make vendor variants only for the versions in BOARD_VNDK_VERSION and
1797 // PRODUCT_EXTRA_VNDK_VERSIONS.
Jiyong Parkf9332f12018-02-01 00:54:12 +09001798 vendorVariantNeeded = true
Logan Chienf3511742017-10-31 18:04:35 +08001799 } else if m.hasVendorVariant() && !vendorSpecific {
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001800 // This will be available in both /system and /vendor
Justin Yun8effde42017-06-23 19:24:43 +09001801 // or a /system directory that is available to vendor.
Jiyong Parkf9332f12018-02-01 00:54:12 +09001802 coreVariantNeeded = true
1803 vendorVariantNeeded = true
Logan Chienf3511742017-10-31 18:04:35 +08001804 } else if vendorSpecific && String(m.Properties.Sdk_version) == "" {
Jiyong Park2db76922017-11-08 16:03:48 +09001805 // This will be available in /vendor (or /odm) only
Jiyong Parkf9332f12018-02-01 00:54:12 +09001806 vendorVariantNeeded = true
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001807 } else {
1808 // This is either in /system (or similar: /data), or is a
1809 // modules built with the NDK. Modules built with the NDK
1810 // will be restricted using the existing link type checks.
Jiyong Parkf9332f12018-02-01 00:54:12 +09001811 coreVariantNeeded = true
1812 }
1813
1814 if Bool(m.Properties.Recovery_available) {
1815 recoveryVariantNeeded = true
1816 }
1817
1818 if m.ModuleBase.InstallInRecovery() {
1819 recoveryVariantNeeded = true
1820 coreVariantNeeded = false
1821 }
1822
Jiyong Park413cc742018-06-19 01:46:06 +09001823 if recoveryVariantNeeded {
Jiyong Park8d52f862018-07-07 18:02:07 +09001824 primaryArch := mctx.Config().DevicePrimaryArchType()
1825 moduleArch := m.Target().Arch.ArchType
1826 if moduleArch != primaryArch {
Jiyong Park413cc742018-06-19 01:46:06 +09001827 recoveryVariantNeeded = false
1828 }
1829 }
1830
Jiyong Parkf9332f12018-02-01 00:54:12 +09001831 var variants []string
1832 if coreVariantNeeded {
1833 variants = append(variants, coreMode)
1834 }
1835 if vendorVariantNeeded {
1836 variants = append(variants, vendorMode)
1837 }
1838 if recoveryVariantNeeded {
1839 variants = append(variants, recoveryMode)
1840 }
1841 mod := mctx.CreateVariations(variants...)
1842 for i, v := range variants {
1843 if v == vendorMode {
1844 m := mod[i].(*Module)
1845 m.Properties.UseVndk = true
1846 squashVendorSrcs(m)
1847 } else if v == recoveryMode {
1848 m := mod[i].(*Module)
1849 m.Properties.InRecovery = true
Jiyong Park5baac542018-08-28 09:55:37 +09001850 m.MakeAsPlatform()
Jiyong Parkf9332f12018-02-01 00:54:12 +09001851 squashRecoverySrcs(m)
1852 }
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001853 }
1854}
1855
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -07001856func getCurrentNdkPrebuiltVersion(ctx DepsContext) string {
Colin Cross6510f912017-11-29 00:27:14 -08001857 if ctx.Config().PlatformSdkVersionInt() > config.NdkMaxPrebuiltVersionInt {
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -07001858 return strconv.Itoa(config.NdkMaxPrebuiltVersionInt)
1859 }
Colin Cross6510f912017-11-29 00:27:14 -08001860 return ctx.Config().PlatformSdkVersion()
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -07001861}
1862
Colin Cross06a931b2015-10-28 17:23:31 -07001863var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07001864var BoolDefault = proptools.BoolDefault
Nan Zhang0007d812017-11-07 10:57:05 -08001865var BoolPtr = proptools.BoolPtr
1866var String = proptools.String
1867var StringPtr = proptools.StringPtr