blob: f8a8225b7087778de28067cb44a3c98b88f56e2a [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 Cross1e676be2016-10-12 14:38:15 -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()
42 ctx.BottomUp("begin", beginMutator).Parallel()
43 })
Colin Cross16b23492016-01-06 14:41:07 -080044
Colin Cross1e676be2016-10-12 14:38:15 -070045 android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
46 ctx.TopDown("asan_deps", sanitizerDepsMutator(asan))
47 ctx.BottomUp("asan", sanitizerMutator(asan)).Parallel()
Colin Cross16b23492016-01-06 14:41:07 -080048
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000049 ctx.TopDown("cfi_deps", sanitizerDepsMutator(cfi))
50 ctx.BottomUp("cfi", sanitizerMutator(cfi)).Parallel()
51
Colin Cross1e676be2016-10-12 14:38:15 -070052 ctx.TopDown("tsan_deps", sanitizerDepsMutator(tsan))
53 ctx.BottomUp("tsan", sanitizerMutator(tsan)).Parallel()
Dan Willemsen581341d2017-02-09 16:16:31 -080054
Colin Cross6b753602018-06-21 13:03:07 -070055 ctx.TopDown("sanitize_runtime_deps", sanitizerRuntimeDepsMutator)
Ivan Lozano30c5db22018-02-21 15:49:20 -080056
Pirama Arumuga Nainar0b882f02018-04-23 22:44:39 +000057 ctx.BottomUp("coverage", coverageLinkingMutator).Parallel()
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -080058 ctx.TopDown("vndk_deps", sabiDepsMutator)
Stephen Craneba090d12017-05-09 15:44:35 -070059
60 ctx.TopDown("lto_deps", ltoDepsMutator)
61 ctx.BottomUp("lto", ltoMutator).Parallel()
Colin Cross1e676be2016-10-12 14:38:15 -070062 })
Colin Crossb98c8b02016-07-29 13:44:28 -070063
64 pctx.Import("android/soong/cc/config")
Colin Cross463a90e2015-06-17 14:20:06 -070065}
66
Colin Crossca860ac2016-01-04 14:34:37 -080067type Deps struct {
68 SharedLibs, LateSharedLibs []string
69 StaticLibs, LateStaticLibs, WholeStaticLibs []string
Colin Cross5950f382016-12-13 12:50:57 -080070 HeaderLibs []string
Logan Chien43d34c32017-12-20 01:17:32 +080071 RuntimeLibs []string
Colin Crossc472d572015-03-17 15:06:21 -070072
Colin Cross5950f382016-12-13 12:50:57 -080073 ReexportSharedLibHeaders, ReexportStaticLibHeaders, ReexportHeaderLibHeaders []string
Dan Willemsen490a8dc2016-06-06 18:22:19 -070074
Colin Cross81413472016-04-11 14:37:39 -070075 ObjFiles []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -070076
Dan Willemsenb40aab62016-04-20 14:21:14 -070077 GeneratedSources []string
78 GeneratedHeaders []string
79
Dan Willemsenb3454ab2016-09-28 17:34:58 -070080 ReexportGeneratedHeaders []string
81
Colin Cross97ba0732015-03-23 17:50:24 -070082 CrtBegin, CrtEnd string
Dan Willemsenc77a0b32017-09-18 23:19:12 -070083 LinkerScript string
Colin Crossc472d572015-03-17 15:06:21 -070084}
85
Colin Crossca860ac2016-01-04 14:34:37 -080086type PathDeps struct {
Colin Cross26c34ed2016-09-30 17:10:16 -070087 // Paths to .so files
88 SharedLibs, LateSharedLibs android.Paths
89 // Paths to the dependencies to use for .so files (.so.toc files)
90 SharedLibsDeps, LateSharedLibsDeps android.Paths
91 // Paths to .a files
Colin Cross635c3b02016-05-18 15:37:25 -070092 StaticLibs, LateStaticLibs, WholeStaticLibs android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -070093
Colin Cross26c34ed2016-09-30 17:10:16 -070094 // Paths to .o files
Dan Willemsen5cb580f2016-09-26 17:33:01 -070095 Objs Objects
Dan Willemsen581341d2017-02-09 16:16:31 -080096 StaticLibObjs Objects
Dan Willemsen5cb580f2016-09-26 17:33:01 -070097 WholeStaticLibObjs Objects
Dan Willemsen34cc69e2015-09-23 15:26:20 -070098
Colin Cross26c34ed2016-09-30 17:10:16 -070099 // Paths to generated source files
Colin Cross635c3b02016-05-18 15:37:25 -0700100 GeneratedSources android.Paths
101 GeneratedHeaders android.Paths
Dan Willemsenb40aab62016-04-20 14:21:14 -0700102
Dan Willemsen76f08272016-07-09 00:14:08 -0700103 Flags, ReexportedFlags []string
Dan Willemsen847dcc72016-09-29 12:13:36 -0700104 ReexportedFlagsDeps android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700105
Colin Cross26c34ed2016-09-30 17:10:16 -0700106 // Paths to crt*.o files
Colin Cross635c3b02016-05-18 15:37:25 -0700107 CrtBegin, CrtEnd android.OptionalPath
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700108 LinkerScript android.OptionalPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700109}
110
Colin Crossca860ac2016-01-04 14:34:37 -0800111type Flags struct {
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700112 GlobalFlags []string // Flags that apply to C, C++, and assembly source files
113 ArFlags []string // Flags that apply to ar
114 AsFlags []string // Flags that apply to assembly source files
115 CFlags []string // Flags that apply to C and C++ source files
116 ToolingCFlags []string // Flags that apply to C and C++ source files parsed by clang LibTooling tools
117 ConlyFlags []string // Flags that apply to C source files
118 CppFlags []string // Flags that apply to C++ source files
119 ToolingCppFlags []string // Flags that apply to C++ source files parsed by clang LibTooling tools
120 YaccFlags []string // Flags that apply to Yacc source files
121 protoFlags []string // Flags that apply to proto source files
Joe Onorato09e94ab2017-11-18 18:23:14 -0800122 protoOutParams []string // Flags that modify the output of proto generated files
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700123 aidlFlags []string // Flags that apply to aidl source files
124 rsFlags []string // Flags that apply to renderscript source files
125 LdFlags []string // Flags that apply to linker command lines
126 libFlags []string // Flags to add libraries early to the link order
127 TidyFlags []string // Flags that apply to clang-tidy
128 SAbiFlags []string // Flags that apply to header-abi-dumper
129 YasmFlags []string // Flags that apply to yasm assembly source files
Colin Cross28344522015-04-22 13:07:53 -0700130
Colin Crossc3199482017-03-30 15:03:04 -0700131 // Global include flags that apply to C, C++, and assembly source files
132 // These must be after any module include flags, which will be in GlobalFlags.
133 SystemIncludeFlags []string
134
Colin Crossb98c8b02016-07-29 13:44:28 -0700135 Toolchain config.Toolchain
Colin Cross28344522015-04-22 13:07:53 -0700136 Clang bool
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700137 Tidy bool
Dan Willemsen581341d2017-02-09 16:16:31 -0800138 Coverage bool
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800139 SAbiDump bool
Dan Willemsenab9f4262018-02-14 13:58:34 -0800140 ProtoRoot bool
Colin Crossca860ac2016-01-04 14:34:37 -0800141
142 RequiredInstructionSet string
Colin Cross16b23492016-01-06 14:41:07 -0800143 DynamicLinker string
144
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700145 CFlagsDeps android.Paths // Files depended on by compiler flags
146 LdFlagsDeps android.Paths // Files depended on by linker flags
Colin Cross18c0c5a2016-12-01 14:45:23 -0800147
148 GroupStaticLibs bool
Zhizhou Yang51be6322018-02-08 18:32:11 -0800149 ArGoldPlugin bool // Whether LLVM gold plugin option is passed to llvm-ar
Colin Crossc472d572015-03-17 15:06:21 -0700150}
151
Colin Cross81413472016-04-11 14:37:39 -0700152type ObjectLinkerProperties struct {
153 // names of other cc_object modules to link into this module using partial linking
154 Objs []string `android:"arch_variant"`
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700155
156 // if set, add an extra objcopy --prefix-symbols= step
Nan Zhang0007d812017-11-07 10:57:05 -0800157 Prefix_symbols *string
Colin Cross81413472016-04-11 14:37:39 -0700158}
159
Colin Crossca860ac2016-01-04 14:34:37 -0800160// Properties used to compile all C or C++ modules
161type BaseProperties struct {
162 // compile module with clang instead of gcc
163 Clang *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700164
165 // Minimum sdk version supported when compiling against the ndk
Nan Zhang0007d812017-11-07 10:57:05 -0800166 Sdk_version *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700167
Logan Chien43d34c32017-12-20 01:17:32 +0800168 AndroidMkSharedLibs []string `blueprint:"mutated"`
169 AndroidMkRuntimeLibs []string `blueprint:"mutated"`
170 HideFromMake bool `blueprint:"mutated"`
171 PreventInstall bool `blueprint:"mutated"`
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700172
173 UseVndk bool `blueprint:"mutated"`
Colin Cross5beccee2017-12-07 15:28:59 -0800174
175 // *.logtags files, to combine together in order to generate the /system/etc/event-log-tags
176 // file
177 Logtags []string
Jiyong Parkf9332f12018-02-01 00:54:12 +0900178
179 // Make this module available when building for recovery
180 Recovery_available *bool
181
182 InRecovery bool `blueprint:"mutated"`
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700183}
184
185type VendorProperties struct {
Jiyong Park82e2bf32017-08-16 14:05:54 +0900186 // whether this module should be allowed to be directly depended by other
187 // modules with `vendor: true`, `proprietary: true`, or `vendor_available:true`.
188 // If set to true, two variants will be built separately, one like
189 // normal, and the other limited to the set of libraries and headers
190 // that are exposed to /vendor modules.
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700191 //
192 // The vendor variant may be used with a different (newer) /system,
193 // so it shouldn't have any unversioned runtime dependencies, or
194 // make assumptions about the system that may not be true in the
195 // future.
196 //
Jiyong Park82e2bf32017-08-16 14:05:54 +0900197 // If set to false, this module becomes inaccessible from /vendor modules.
198 //
199 // Default value is true when vndk: {enabled: true} or vendor: true.
200 //
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700201 // Nothing happens if BOARD_VNDK_VERSION isn't set in the BoardConfig.mk
202 Vendor_available *bool
Jiyong Park5fb8c102018-04-09 12:03:06 +0900203
204 // whether this module is capable of being loaded with other instance
205 // (possibly an older version) of the same module in the same process.
206 // Currently, a shared library that is a member of VNDK (vndk: {enabled: true})
207 // can be double loaded in a vendor process if the library is also a
208 // (direct and indirect) dependency of an LLNDK library. Such libraries must be
209 // explicitly marked as `double_loadable: true` by the owner, or the dependency
210 // from the LLNDK lib should be cut if the lib is not designed to be double loaded.
211 Double_loadable *bool
Colin Crossca860ac2016-01-04 14:34:37 -0800212}
213
Colin Crossca860ac2016-01-04 14:34:37 -0800214type ModuleContextIntf interface {
Colin Crossca860ac2016-01-04 14:34:37 -0800215 static() bool
216 staticBinary() bool
217 clang() bool
Colin Crossb98c8b02016-07-29 13:44:28 -0700218 toolchain() config.Toolchain
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700219 useSdk() bool
Colin Crossca860ac2016-01-04 14:34:37 -0800220 sdkVersion() string
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700221 useVndk() bool
Justin Yun8effde42017-06-23 19:24:43 +0900222 isVndk() bool
223 isVndkSp() bool
Logan Chienf3511742017-10-31 18:04:35 +0800224 isVndkExt() bool
Jiyong Parkf9332f12018-02-01 00:54:12 +0900225 inRecovery() bool
Logan Chien2f2b8902018-07-10 15:01:19 +0800226 shouldCreateVndkSourceAbiDump() bool
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700227 selectedStl() string
Colin Crossce75d2c2016-10-06 16:12:58 -0700228 baseModuleName() string
Logan Chienf3511742017-10-31 18:04:35 +0800229 getVndkExtendsModuleName() string
Yi Kong7e53c572018-02-14 18:16:12 +0800230 isPgoCompile() bool
Colin Crossca860ac2016-01-04 14:34:37 -0800231}
232
233type ModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700234 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800235 ModuleContextIntf
236}
237
238type BaseModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700239 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800240 ModuleContextIntf
241}
242
Colin Cross37047f12016-12-13 17:06:13 -0800243type DepsContext interface {
244 android.BottomUpMutatorContext
245 ModuleContextIntf
246}
247
Colin Crossca860ac2016-01-04 14:34:37 -0800248type feature interface {
249 begin(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800250 deps(ctx DepsContext, deps Deps) Deps
Colin Crossca860ac2016-01-04 14:34:37 -0800251 flags(ctx ModuleContext, flags Flags) Flags
252 props() []interface{}
253}
254
255type compiler interface {
Colin Cross42742b82016-08-01 13:20:05 -0700256 compilerInit(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800257 compilerDeps(ctx DepsContext, deps Deps) Deps
Colin Crossf18e1102017-11-16 14:33:08 -0800258 compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags
Colin Cross42742b82016-08-01 13:20:05 -0700259 compilerProps() []interface{}
260
Colin Cross76fada02016-07-27 10:31:13 -0700261 appendCflags([]string)
262 appendAsflags([]string)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700263 compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects
Colin Crossca860ac2016-01-04 14:34:37 -0800264}
265
266type linker interface {
Colin Cross42742b82016-08-01 13:20:05 -0700267 linkerInit(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800268 linkerDeps(ctx DepsContext, deps Deps) Deps
Colin Cross42742b82016-08-01 13:20:05 -0700269 linkerFlags(ctx ModuleContext, flags Flags) Flags
270 linkerProps() []interface{}
271
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700272 link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path
Colin Cross76fada02016-07-27 10:31:13 -0700273 appendLdflags([]string)
Colin Crossca860ac2016-01-04 14:34:37 -0800274}
275
276type installer interface {
Colin Cross42742b82016-08-01 13:20:05 -0700277 installerProps() []interface{}
Colin Cross635c3b02016-05-18 15:37:25 -0700278 install(ctx ModuleContext, path android.Path)
Colin Crossca860ac2016-01-04 14:34:37 -0800279 inData() bool
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700280 inSanitizerDir() bool
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700281 hostToolPath() android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800282}
283
Colin Crossc99deeb2016-04-11 15:06:20 -0700284type dependencyTag struct {
285 blueprint.BaseDependencyTag
286 name string
287 library bool
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700288
289 reexportFlags bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700290}
291
292var (
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700293 sharedDepTag = dependencyTag{name: "shared", library: true}
294 sharedExportDepTag = dependencyTag{name: "shared", library: true, reexportFlags: true}
295 lateSharedDepTag = dependencyTag{name: "late shared", library: true}
296 staticDepTag = dependencyTag{name: "static", library: true}
297 staticExportDepTag = dependencyTag{name: "static", library: true, reexportFlags: true}
298 lateStaticDepTag = dependencyTag{name: "late static", library: true}
299 wholeStaticDepTag = dependencyTag{name: "whole static", library: true, reexportFlags: true}
Colin Cross32ec36c2016-12-15 07:39:51 -0800300 headerDepTag = dependencyTag{name: "header", library: true}
301 headerExportDepTag = dependencyTag{name: "header", library: true, reexportFlags: true}
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700302 genSourceDepTag = dependencyTag{name: "gen source"}
303 genHeaderDepTag = dependencyTag{name: "gen header"}
304 genHeaderExportDepTag = dependencyTag{name: "gen header", reexportFlags: true}
305 objDepTag = dependencyTag{name: "obj"}
306 crtBeginDepTag = dependencyTag{name: "crtbegin"}
307 crtEndDepTag = dependencyTag{name: "crtend"}
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700308 linkerScriptDepTag = dependencyTag{name: "linker script"}
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700309 reuseObjTag = dependencyTag{name: "reuse objects"}
310 ndkStubDepTag = dependencyTag{name: "ndk stub", library: true}
311 ndkLateStubDepTag = dependencyTag{name: "ndk late stub", library: true}
Logan Chienf3511742017-10-31 18:04:35 +0800312 vndkExtDepTag = dependencyTag{name: "vndk extends", library: true}
Logan Chien43d34c32017-12-20 01:17:32 +0800313 runtimeDepTag = dependencyTag{name: "runtime lib"}
Colin Crossc99deeb2016-04-11 15:06:20 -0700314)
315
Colin Crossca860ac2016-01-04 14:34:37 -0800316// Module contains the properties and members used by all C/C++ module types, and implements
317// the blueprint.Module interface. It delegates to compiler, linker, and installer interfaces
318// to construct the output file. Behavior can be customized with a Customizer interface
319type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700320 android.ModuleBase
Colin Cross1f44a3a2017-07-07 14:33:33 -0700321 android.DefaultableModuleBase
Colin Crossc472d572015-03-17 15:06:21 -0700322
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700323 Properties BaseProperties
324 VendorProperties VendorProperties
Colin Crossfa138792015-04-24 17:31:52 -0700325
Colin Crossca860ac2016-01-04 14:34:37 -0800326 // initialize before calling Init
Colin Cross635c3b02016-05-18 15:37:25 -0700327 hod android.HostOrDeviceSupported
328 multilib android.Multilib
Colin Crossc472d572015-03-17 15:06:21 -0700329
Colin Crossca860ac2016-01-04 14:34:37 -0800330 // delegates, initialize before calling Init
Colin Crossb4ce0ec2016-09-13 13:41:39 -0700331 features []feature
332 compiler compiler
333 linker linker
334 installer installer
335 stl *stl
336 sanitize *sanitize
Dan Willemsen581341d2017-02-09 16:16:31 -0800337 coverage *coverage
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800338 sabi *sabi
Justin Yun8effde42017-06-23 19:24:43 +0900339 vndkdep *vndkdep
Stephen Craneba090d12017-05-09 15:44:35 -0700340 lto *lto
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700341 pgo *pgo
Colin Cross16b23492016-01-06 14:41:07 -0800342
343 androidMkSharedLibDeps []string
Colin Cross74d1ec02015-04-28 13:30:13 -0700344
Colin Cross635c3b02016-05-18 15:37:25 -0700345 outputFile android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800346
Colin Crossb98c8b02016-07-29 13:44:28 -0700347 cachedToolchain config.Toolchain
Colin Crossb916a382016-07-29 17:28:03 -0700348
349 subAndroidMkOnce map[subAndroidMkProvider]bool
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800350
351 // Flags used to compile this module
352 flags Flags
Jeff Gaston294356f2017-09-27 17:05:30 -0700353
354 // 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 -0800355 // line invocation. depsInLinkOrder stores the proper ordering of all of the transitive
Jeff Gaston294356f2017-09-27 17:05:30 -0700356 // deps of this module
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -0800357 depsInLinkOrder android.Paths
358
359 // only non-nil when this is a shared library that reuses the objects of a static library
360 staticVariant *Module
Colin Crossc472d572015-03-17 15:06:21 -0700361}
362
Colin Cross36242852017-06-23 15:06:31 -0700363func (c *Module) Init() android.Module {
Dan Willemsenf923f2b2018-05-09 13:45:03 -0700364 c.AddProperties(&c.Properties, &c.VendorProperties)
Colin Crossca860ac2016-01-04 14:34:37 -0800365 if c.compiler != nil {
Colin Cross36242852017-06-23 15:06:31 -0700366 c.AddProperties(c.compiler.compilerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800367 }
368 if c.linker != nil {
Colin Cross36242852017-06-23 15:06:31 -0700369 c.AddProperties(c.linker.linkerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800370 }
371 if c.installer != nil {
Colin Cross36242852017-06-23 15:06:31 -0700372 c.AddProperties(c.installer.installerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800373 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700374 if c.stl != nil {
Colin Cross36242852017-06-23 15:06:31 -0700375 c.AddProperties(c.stl.props()...)
Colin Crossa8e07cc2016-04-04 15:07:06 -0700376 }
Colin Cross16b23492016-01-06 14:41:07 -0800377 if c.sanitize != nil {
Colin Cross36242852017-06-23 15:06:31 -0700378 c.AddProperties(c.sanitize.props()...)
Colin Cross16b23492016-01-06 14:41:07 -0800379 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800380 if c.coverage != nil {
Colin Cross36242852017-06-23 15:06:31 -0700381 c.AddProperties(c.coverage.props()...)
Dan Willemsen581341d2017-02-09 16:16:31 -0800382 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800383 if c.sabi != nil {
Colin Cross36242852017-06-23 15:06:31 -0700384 c.AddProperties(c.sabi.props()...)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800385 }
Justin Yun8effde42017-06-23 19:24:43 +0900386 if c.vndkdep != nil {
387 c.AddProperties(c.vndkdep.props()...)
388 }
Stephen Craneba090d12017-05-09 15:44:35 -0700389 if c.lto != nil {
390 c.AddProperties(c.lto.props()...)
391 }
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700392 if c.pgo != nil {
393 c.AddProperties(c.pgo.props()...)
394 }
Colin Crossca860ac2016-01-04 14:34:37 -0800395 for _, feature := range c.features {
Colin Cross36242852017-06-23 15:06:31 -0700396 c.AddProperties(feature.props()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800397 }
Colin Crossc472d572015-03-17 15:06:21 -0700398
Colin Cross36242852017-06-23 15:06:31 -0700399 android.InitAndroidArchModule(c, c.hod, c.multilib)
Colin Crossc472d572015-03-17 15:06:21 -0700400
Colin Cross1f44a3a2017-07-07 14:33:33 -0700401 android.InitDefaultableModule(c)
Colin Cross36242852017-06-23 15:06:31 -0700402
403 return c
Colin Crossc472d572015-03-17 15:06:21 -0700404}
405
Colin Crossb916a382016-07-29 17:28:03 -0700406// Returns true for dependency roots (binaries)
407// TODO(ccross): also handle dlopenable libraries
408func (c *Module) isDependencyRoot() bool {
409 if root, ok := c.linker.(interface {
410 isDependencyRoot() bool
411 }); ok {
412 return root.isDependencyRoot()
413 }
414 return false
415}
416
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700417func (c *Module) useVndk() bool {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700418 return c.Properties.UseVndk
419}
420
Justin Yun8effde42017-06-23 19:24:43 +0900421func (c *Module) isVndk() bool {
Logan Chienf3511742017-10-31 18:04:35 +0800422 if vndkdep := c.vndkdep; vndkdep != nil {
423 return vndkdep.isVndk()
Justin Yun8effde42017-06-23 19:24:43 +0900424 }
425 return false
426}
427
Yi Kong7e53c572018-02-14 18:16:12 +0800428func (c *Module) isPgoCompile() bool {
429 if pgo := c.pgo; pgo != nil {
430 return pgo.Properties.PgoCompile
431 }
432 return false
433}
434
Logan Chienf3511742017-10-31 18:04:35 +0800435func (c *Module) isVndkSp() bool {
436 if vndkdep := c.vndkdep; vndkdep != nil {
437 return vndkdep.isVndkSp()
438 }
439 return false
440}
441
442func (c *Module) isVndkExt() bool {
443 if vndkdep := c.vndkdep; vndkdep != nil {
444 return vndkdep.isVndkExt()
445 }
446 return false
447}
448
449func (c *Module) getVndkExtendsModuleName() string {
450 if vndkdep := c.vndkdep; vndkdep != nil {
451 return vndkdep.getVndkExtendsModuleName()
452 }
453 return ""
454}
455
Jiyong Park82e2bf32017-08-16 14:05:54 +0900456// Returns true only when this module is configured to have core and vendor
457// variants.
458func (c *Module) hasVendorVariant() bool {
459 return c.isVndk() || Bool(c.VendorProperties.Vendor_available)
460}
461
Jiyong Parkf9332f12018-02-01 00:54:12 +0900462func (c *Module) inRecovery() bool {
463 return c.Properties.InRecovery || c.ModuleBase.InstallInRecovery()
464}
465
466func (c *Module) onlyInRecovery() bool {
467 return c.ModuleBase.InstallInRecovery()
468}
469
Colin Crossca860ac2016-01-04 14:34:37 -0800470type baseModuleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700471 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800472 moduleContextImpl
473}
474
Colin Cross37047f12016-12-13 17:06:13 -0800475type depsContext struct {
476 android.BottomUpMutatorContext
477 moduleContextImpl
478}
479
Colin Crossca860ac2016-01-04 14:34:37 -0800480type moduleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700481 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800482 moduleContextImpl
483}
484
Jiyong Park2db76922017-11-08 16:03:48 +0900485func (ctx *moduleContext) SocSpecific() bool {
486 return ctx.ModuleContext.SocSpecific() ||
487 (ctx.mod.hasVendorVariant() && ctx.mod.useVndk() && !ctx.mod.isVndk())
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700488}
489
Colin Crossca860ac2016-01-04 14:34:37 -0800490type moduleContextImpl struct {
491 mod *Module
492 ctx BaseModuleContext
493}
494
Colin Crossca860ac2016-01-04 14:34:37 -0800495func (ctx *moduleContextImpl) clang() bool {
496 return ctx.mod.clang(ctx.ctx)
497}
498
Colin Crossb98c8b02016-07-29 13:44:28 -0700499func (ctx *moduleContextImpl) toolchain() config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800500 return ctx.mod.toolchain(ctx.ctx)
501}
502
503func (ctx *moduleContextImpl) static() bool {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000504 return ctx.mod.static()
Colin Crossca860ac2016-01-04 14:34:37 -0800505}
506
507func (ctx *moduleContextImpl) staticBinary() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700508 if static, ok := ctx.mod.linker.(interface {
509 staticBinary() bool
510 }); ok {
511 return static.staticBinary()
Colin Crossca860ac2016-01-04 14:34:37 -0800512 }
Colin Crossb916a382016-07-29 17:28:03 -0700513 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800514}
515
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700516func (ctx *moduleContextImpl) useSdk() bool {
Jiyong Parkf9332f12018-02-01 00:54:12 +0900517 if ctx.ctx.Device() && !ctx.useVndk() && !ctx.inRecovery() {
Nan Zhang0007d812017-11-07 10:57:05 -0800518 return String(ctx.mod.Properties.Sdk_version) != ""
Dan Willemsena96ff642016-06-07 12:34:45 -0700519 }
520 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800521}
522
523func (ctx *moduleContextImpl) sdkVersion() string {
Dan Willemsena96ff642016-06-07 12:34:45 -0700524 if ctx.ctx.Device() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700525 if ctx.useVndk() {
Justin Yun732aa6a2018-03-23 17:43:47 +0900526 vndk_ver := ctx.ctx.DeviceConfig().VndkVersion()
Ryan Prichard05206112018-03-26 21:25:27 -0700527 if vndk_ver == "current" {
Justin Yun732aa6a2018-03-23 17:43:47 +0900528 platform_vndk_ver := ctx.ctx.DeviceConfig().PlatformVndkVersion()
529 if inList(platform_vndk_ver, ctx.ctx.Config().PlatformVersionCombinedCodenames()) {
530 return "current"
531 }
532 return platform_vndk_ver
533 }
534 return vndk_ver
Dan Willemsend2ede872016-11-18 14:54:24 -0800535 }
Justin Yun732aa6a2018-03-23 17:43:47 +0900536 return String(ctx.mod.Properties.Sdk_version)
Dan Willemsena96ff642016-06-07 12:34:45 -0700537 }
538 return ""
Colin Crossca860ac2016-01-04 14:34:37 -0800539}
540
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700541func (ctx *moduleContextImpl) useVndk() bool {
542 return ctx.mod.useVndk()
543}
Justin Yun8effde42017-06-23 19:24:43 +0900544
Logan Chienf3511742017-10-31 18:04:35 +0800545func (ctx *moduleContextImpl) isVndk() bool {
546 return ctx.mod.isVndk()
547}
548
Yi Kong7e53c572018-02-14 18:16:12 +0800549func (ctx *moduleContextImpl) isPgoCompile() bool {
550 return ctx.mod.isPgoCompile()
551}
552
Justin Yun8effde42017-06-23 19:24:43 +0900553func (ctx *moduleContextImpl) isVndkSp() bool {
Logan Chienf3511742017-10-31 18:04:35 +0800554 return ctx.mod.isVndkSp()
555}
556
557func (ctx *moduleContextImpl) isVndkExt() bool {
558 return ctx.mod.isVndkExt()
Justin Yun8effde42017-06-23 19:24:43 +0900559}
560
Jiyong Parkf9332f12018-02-01 00:54:12 +0900561func (ctx *moduleContextImpl) inRecovery() bool {
562 return ctx.mod.inRecovery()
563}
564
Logan Chien2f2b8902018-07-10 15:01:19 +0800565// Check whether ABI dumps should be created for this module.
566func (ctx *moduleContextImpl) shouldCreateVndkSourceAbiDump() bool {
567 if ctx.ctx.Config().IsEnvTrue("SKIP_ABI_CHECKS") {
568 return false
Jayant Chowdharyea0a2e12018-03-01 19:12:16 -0800569 }
Logan Chien2f2b8902018-07-10 15:01:19 +0800570 if sanitize := ctx.mod.sanitize; sanitize != nil {
571 if !sanitize.isVariantOnProductionDevice() {
572 return false
573 }
574 }
575 if !ctx.ctx.Device() {
576 // Host modules do not need ABI dumps.
577 return false
578 }
579 if inList(ctx.baseModuleName(), llndkLibraries) {
580 return true
581 }
582 if ctx.useVndk() && ctx.isVndk() {
583 // Return true if this is VNDK-core, VNDK-SP, or VNDK-Ext and this is not
584 // VNDK-private.
585 return Bool(ctx.mod.VendorProperties.Vendor_available) || ctx.isVndkExt()
586 }
587 return false
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800588}
589
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700590func (ctx *moduleContextImpl) selectedStl() string {
591 if stl := ctx.mod.stl; stl != nil {
592 return stl.Properties.SelectedStl
593 }
594 return ""
595}
596
Colin Crossce75d2c2016-10-06 16:12:58 -0700597func (ctx *moduleContextImpl) baseModuleName() string {
598 return ctx.mod.ModuleBase.BaseModuleName()
599}
600
Logan Chienf3511742017-10-31 18:04:35 +0800601func (ctx *moduleContextImpl) getVndkExtendsModuleName() string {
602 return ctx.mod.getVndkExtendsModuleName()
603}
604
Colin Cross635c3b02016-05-18 15:37:25 -0700605func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800606 return &Module{
607 hod: hod,
608 multilib: multilib,
609 }
610}
611
Colin Cross635c3b02016-05-18 15:37:25 -0700612func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800613 module := newBaseModule(hod, multilib)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700614 module.features = []feature{
615 &tidyFeature{},
616 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700617 module.stl = &stl{}
Colin Cross16b23492016-01-06 14:41:07 -0800618 module.sanitize = &sanitize{}
Dan Willemsen581341d2017-02-09 16:16:31 -0800619 module.coverage = &coverage{}
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800620 module.sabi = &sabi{}
Justin Yun8effde42017-06-23 19:24:43 +0900621 module.vndkdep = &vndkdep{}
Stephen Craneba090d12017-05-09 15:44:35 -0700622 module.lto = &lto{}
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700623 module.pgo = &pgo{}
Colin Crossca860ac2016-01-04 14:34:37 -0800624 return module
625}
626
Colin Crossce75d2c2016-10-06 16:12:58 -0700627func (c *Module) Prebuilt() *android.Prebuilt {
628 if p, ok := c.linker.(prebuiltLinkerInterface); ok {
629 return p.prebuilt()
630 }
631 return nil
632}
633
634func (c *Module) Name() string {
635 name := c.ModuleBase.Name()
Dan Willemsen01a90592017-04-07 15:21:13 -0700636 if p, ok := c.linker.(interface {
637 Name(string) string
638 }); ok {
Colin Crossce75d2c2016-10-06 16:12:58 -0700639 name = p.Name(name)
640 }
641 return name
642}
643
Jeff Gaston294356f2017-09-27 17:05:30 -0700644// orderDeps reorders dependencies into a list such that if module A depends on B, then
645// A will precede B in the resultant list.
646// This is convenient for passing into a linker.
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -0800647// Note that directSharedDeps should be the analogous static library for each shared lib dep
648func 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 -0700649 // If A depends on B, then
650 // Every list containing A will also contain B later in the list
651 // So, after concatenating all lists, the final instance of B will have come from the same
652 // original list as the final instance of A
653 // So, the final instance of B will be later in the concatenation than the final A
654 // So, keeping only the final instance of A and of B ensures that A is earlier in the output
655 // list than B
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -0800656 for _, dep := range directStaticDeps {
Jeff Gaston294356f2017-09-27 17:05:30 -0700657 orderedAllDeps = append(orderedAllDeps, dep)
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -0800658 orderedAllDeps = append(orderedAllDeps, allTransitiveDeps[dep]...)
659 }
660 for _, dep := range directSharedDeps {
661 orderedAllDeps = append(orderedAllDeps, dep)
662 orderedAllDeps = append(orderedAllDeps, allTransitiveDeps[dep]...)
Jeff Gaston294356f2017-09-27 17:05:30 -0700663 }
664
Colin Crossb6715442017-10-24 11:13:31 -0700665 orderedAllDeps = android.LastUniquePaths(orderedAllDeps)
Jeff Gaston294356f2017-09-27 17:05:30 -0700666
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -0800667 // We don't want to add any new dependencies into directStaticDeps (to allow the caller to
Jeff Gaston294356f2017-09-27 17:05:30 -0700668 // intentionally exclude or replace any unwanted transitive dependencies), so we limit the
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -0800669 // resultant list to only what the caller has chosen to include in directStaticDeps
670 _, orderedDeclaredDeps = android.FilterPathList(orderedAllDeps, directStaticDeps)
Jeff Gaston294356f2017-09-27 17:05:30 -0700671
672 return orderedAllDeps, orderedDeclaredDeps
673}
674
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -0800675func orderStaticModuleDeps(module *Module, staticDeps []*Module, sharedDeps []*Module) (results []android.Path) {
676 // convert Module to Path
677 allTransitiveDeps := make(map[android.Path][]android.Path, len(staticDeps))
678 staticDepFiles := []android.Path{}
679 for _, dep := range staticDeps {
680 allTransitiveDeps[dep.outputFile.Path()] = dep.depsInLinkOrder
681 staticDepFiles = append(staticDepFiles, dep.outputFile.Path())
Jeff Gaston294356f2017-09-27 17:05:30 -0700682 }
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -0800683 sharedDepFiles := []android.Path{}
684 for _, sharedDep := range sharedDeps {
685 staticAnalogue := sharedDep.staticVariant
686 if staticAnalogue != nil {
687 allTransitiveDeps[staticAnalogue.outputFile.Path()] = staticAnalogue.depsInLinkOrder
688 sharedDepFiles = append(sharedDepFiles, staticAnalogue.outputFile.Path())
689 }
Jeff Gaston294356f2017-09-27 17:05:30 -0700690 }
691
692 // reorder the dependencies based on transitive dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -0800693 module.depsInLinkOrder, results = orderDeps(staticDepFiles, sharedDepFiles, allTransitiveDeps)
Jeff Gaston294356f2017-09-27 17:05:30 -0700694
695 return results
Jeff Gaston294356f2017-09-27 17:05:30 -0700696}
697
Colin Cross635c3b02016-05-18 15:37:25 -0700698func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800699 ctx := &moduleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700700 ModuleContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800701 moduleContextImpl: moduleContextImpl{
702 mod: c,
703 },
704 }
705 ctx.ctx = ctx
706
Colin Crossf18e1102017-11-16 14:33:08 -0800707 deps := c.depsToPaths(ctx)
708 if ctx.Failed() {
709 return
710 }
711
Colin Crossca860ac2016-01-04 14:34:37 -0800712 flags := Flags{
713 Toolchain: c.toolchain(ctx),
714 Clang: c.clang(ctx),
715 }
Colin Crossca860ac2016-01-04 14:34:37 -0800716 if c.compiler != nil {
Colin Crossf18e1102017-11-16 14:33:08 -0800717 flags = c.compiler.compilerFlags(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -0800718 }
719 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700720 flags = c.linker.linkerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800721 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700722 if c.stl != nil {
723 flags = c.stl.flags(ctx, flags)
724 }
Colin Cross16b23492016-01-06 14:41:07 -0800725 if c.sanitize != nil {
726 flags = c.sanitize.flags(ctx, flags)
727 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800728 if c.coverage != nil {
729 flags = c.coverage.flags(ctx, flags)
730 }
Stephen Craneba090d12017-05-09 15:44:35 -0700731 if c.lto != nil {
732 flags = c.lto.flags(ctx, flags)
733 }
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700734 if c.pgo != nil {
735 flags = c.pgo.flags(ctx, flags)
736 }
Colin Crossca860ac2016-01-04 14:34:37 -0800737 for _, feature := range c.features {
738 flags = feature.flags(ctx, flags)
739 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800740 if ctx.Failed() {
741 return
742 }
743
Colin Crossb98c8b02016-07-29 13:44:28 -0700744 flags.CFlags, _ = filterList(flags.CFlags, config.IllegalFlags)
745 flags.CppFlags, _ = filterList(flags.CppFlags, config.IllegalFlags)
746 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, config.IllegalFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800747
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800748 flags.GlobalFlags = append(flags.GlobalFlags, deps.Flags...)
749 c.flags = flags
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700750 // We need access to all the flags seen by a source file.
751 if c.sabi != nil {
752 flags = c.sabi.flags(ctx, flags)
753 }
Colin Crossca860ac2016-01-04 14:34:37 -0800754 // Optimization to reduce size of build.ninja
755 // Replace the long list of flags for each file with a module-local variable
756 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
757 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
758 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
759 flags.CFlags = []string{"$cflags"}
760 flags.CppFlags = []string{"$cppflags"}
761 flags.AsFlags = []string{"$asflags"}
762
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700763 var objs Objects
Colin Crossca860ac2016-01-04 14:34:37 -0800764 if c.compiler != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700765 objs = c.compiler.compile(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -0800766 if ctx.Failed() {
767 return
768 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800769 }
770
Colin Crossca860ac2016-01-04 14:34:37 -0800771 if c.linker != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700772 outputFile := c.linker.link(ctx, flags, deps, objs)
Colin Crossca860ac2016-01-04 14:34:37 -0800773 if ctx.Failed() {
774 return
775 }
Colin Cross635c3b02016-05-18 15:37:25 -0700776 c.outputFile = android.OptionalPathForPath(outputFile)
Colin Crossce75d2c2016-10-06 16:12:58 -0700777 }
Colin Cross5049f022015-03-18 13:28:46 -0700778
Colin Crossce75d2c2016-10-06 16:12:58 -0700779 if c.installer != nil && !c.Properties.PreventInstall && c.outputFile.Valid() {
780 c.installer.install(ctx, c.outputFile.Path())
781 if ctx.Failed() {
782 return
Colin Crossca860ac2016-01-04 14:34:37 -0800783 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700784 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800785}
786
Colin Crossb98c8b02016-07-29 13:44:28 -0700787func (c *Module) toolchain(ctx BaseModuleContext) config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800788 if c.cachedToolchain == nil {
Colin Crossb98c8b02016-07-29 13:44:28 -0700789 c.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
Colin Cross3f40fa42015-01-30 17:27:36 -0800790 }
Colin Crossca860ac2016-01-04 14:34:37 -0800791 return c.cachedToolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800792}
793
Colin Crossca860ac2016-01-04 14:34:37 -0800794func (c *Module) begin(ctx BaseModuleContext) {
795 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700796 c.compiler.compilerInit(ctx)
Colin Cross21b9a242015-03-24 14:15:58 -0700797 }
Colin Crossca860ac2016-01-04 14:34:37 -0800798 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700799 c.linker.linkerInit(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800800 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700801 if c.stl != nil {
802 c.stl.begin(ctx)
803 }
Colin Cross16b23492016-01-06 14:41:07 -0800804 if c.sanitize != nil {
805 c.sanitize.begin(ctx)
806 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800807 if c.coverage != nil {
808 c.coverage.begin(ctx)
809 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800810 if c.sabi != nil {
811 c.sabi.begin(ctx)
812 }
Justin Yun8effde42017-06-23 19:24:43 +0900813 if c.vndkdep != nil {
814 c.vndkdep.begin(ctx)
815 }
Stephen Craneba090d12017-05-09 15:44:35 -0700816 if c.lto != nil {
817 c.lto.begin(ctx)
818 }
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700819 if c.pgo != nil {
820 c.pgo.begin(ctx)
821 }
Colin Crossca860ac2016-01-04 14:34:37 -0800822 for _, feature := range c.features {
823 feature.begin(ctx)
824 }
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700825 if ctx.useSdk() {
Dan Albertf5415d72017-08-17 16:19:59 -0700826 version, err := normalizeNdkApiLevel(ctx, ctx.sdkVersion(), ctx.Arch())
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700827 if err != nil {
828 ctx.PropertyErrorf("sdk_version", err.Error())
829 }
Nan Zhang0007d812017-11-07 10:57:05 -0800830 c.Properties.Sdk_version = StringPtr(version)
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700831 }
Colin Crossca860ac2016-01-04 14:34:37 -0800832}
833
Colin Cross37047f12016-12-13 17:06:13 -0800834func (c *Module) deps(ctx DepsContext) Deps {
Colin Crossc99deeb2016-04-11 15:06:20 -0700835 deps := Deps{}
836
837 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700838 deps = c.compiler.compilerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700839 }
Pirama Arumuga Nainar0b882f02018-04-23 22:44:39 +0000840 // Add the PGO dependency (the clang_rt.profile runtime library), which
841 // sometimes depends on symbols from libgcc, before libgcc gets added
842 // in linkerDeps().
Pirama Arumuga Nainar49b53d52017-10-04 16:47:29 -0700843 if c.pgo != nil {
844 deps = c.pgo.deps(ctx, deps)
845 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700846 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700847 deps = c.linker.linkerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700848 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700849 if c.stl != nil {
850 deps = c.stl.deps(ctx, deps)
851 }
Colin Cross16b23492016-01-06 14:41:07 -0800852 if c.sanitize != nil {
853 deps = c.sanitize.deps(ctx, deps)
854 }
Pirama Arumuga Nainar0b882f02018-04-23 22:44:39 +0000855 if c.coverage != nil {
856 deps = c.coverage.deps(ctx, deps)
857 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800858 if c.sabi != nil {
859 deps = c.sabi.deps(ctx, deps)
860 }
Justin Yun8effde42017-06-23 19:24:43 +0900861 if c.vndkdep != nil {
862 deps = c.vndkdep.deps(ctx, deps)
863 }
Stephen Craneba090d12017-05-09 15:44:35 -0700864 if c.lto != nil {
865 deps = c.lto.deps(ctx, deps)
866 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700867 for _, feature := range c.features {
868 deps = feature.deps(ctx, deps)
869 }
870
Colin Crossb6715442017-10-24 11:13:31 -0700871 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
872 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
873 deps.LateStaticLibs = android.LastUniqueStrings(deps.LateStaticLibs)
874 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
875 deps.LateSharedLibs = android.LastUniqueStrings(deps.LateSharedLibs)
876 deps.HeaderLibs = android.LastUniqueStrings(deps.HeaderLibs)
Logan Chien43d34c32017-12-20 01:17:32 +0800877 deps.RuntimeLibs = android.LastUniqueStrings(deps.RuntimeLibs)
Colin Crossc99deeb2016-04-11 15:06:20 -0700878
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700879 for _, lib := range deps.ReexportSharedLibHeaders {
880 if !inList(lib, deps.SharedLibs) {
881 ctx.PropertyErrorf("export_shared_lib_headers", "Shared library not in shared_libs: '%s'", lib)
882 }
883 }
884
885 for _, lib := range deps.ReexportStaticLibHeaders {
886 if !inList(lib, deps.StaticLibs) {
887 ctx.PropertyErrorf("export_static_lib_headers", "Static library not in static_libs: '%s'", lib)
888 }
889 }
890
Colin Cross5950f382016-12-13 12:50:57 -0800891 for _, lib := range deps.ReexportHeaderLibHeaders {
892 if !inList(lib, deps.HeaderLibs) {
893 ctx.PropertyErrorf("export_header_lib_headers", "Header library not in header_libs: '%s'", lib)
894 }
895 }
896
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700897 for _, gen := range deps.ReexportGeneratedHeaders {
898 if !inList(gen, deps.GeneratedHeaders) {
899 ctx.PropertyErrorf("export_generated_headers", "Generated header module not in generated_headers: '%s'", gen)
900 }
901 }
902
Colin Crossc99deeb2016-04-11 15:06:20 -0700903 return deps
904}
905
Dan Albert7e9d2952016-08-04 13:02:36 -0700906func (c *Module) beginMutator(actx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800907 ctx := &baseModuleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700908 BaseContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800909 moduleContextImpl: moduleContextImpl{
910 mod: c,
911 },
912 }
913 ctx.ctx = ctx
914
Colin Crossca860ac2016-01-04 14:34:37 -0800915 c.begin(ctx)
Dan Albert7e9d2952016-08-04 13:02:36 -0700916}
917
Colin Cross1e676be2016-10-12 14:38:15 -0700918func (c *Module) DepsMutator(actx android.BottomUpMutatorContext) {
919 if !c.Enabled() {
920 return
921 }
922
Colin Cross37047f12016-12-13 17:06:13 -0800923 ctx := &depsContext{
924 BottomUpMutatorContext: actx,
Dan Albert7e9d2952016-08-04 13:02:36 -0700925 moduleContextImpl: moduleContextImpl{
926 mod: c,
927 },
928 }
929 ctx.ctx = ctx
Colin Crossca860ac2016-01-04 14:34:37 -0800930
Colin Crossc99deeb2016-04-11 15:06:20 -0700931 deps := c.deps(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800932
Dan Albert914449f2016-06-17 16:45:24 -0700933 variantNdkLibs := []string{}
934 variantLateNdkLibs := []string{}
Dan Willemsenb916b802017-03-19 13:44:32 -0700935 if ctx.Os() == android.Android {
Dan Albert914449f2016-06-17 16:45:24 -0700936 version := ctx.sdkVersion()
Dan Willemsen72d39932016-07-08 23:23:48 -0700937
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700938 // rewriteNdkLibs takes a list of names of shared libraries and scans it for three types
939 // of names:
Dan Albert914449f2016-06-17 16:45:24 -0700940 //
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700941 // 1. Name of an NDK library that refers to a prebuilt module.
942 // For each of these, it adds the name of the prebuilt module (which will be in
943 // prebuilts/ndk) to the list of nonvariant libs.
944 // 2. Name of an NDK library that refers to an ndk_library module.
945 // For each of these, it adds the name of the ndk_library module to the list of
946 // variant libs.
947 // 3. Anything else (so anything that isn't an NDK library).
948 // It adds these to the nonvariantLibs list.
Dan Albert914449f2016-06-17 16:45:24 -0700949 //
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700950 // The caller can then know to add the variantLibs dependencies differently from the
951 // nonvariantLibs
952 rewriteNdkLibs := func(list []string) (nonvariantLibs []string, variantLibs []string) {
953 variantLibs = []string{}
954 nonvariantLibs = []string{}
Dan Albert914449f2016-06-17 16:45:24 -0700955 for _, entry := range list {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700956 if ctx.useSdk() && inList(entry, ndkPrebuiltSharedLibraries) {
Dan Albert914449f2016-06-17 16:45:24 -0700957 if !inList(entry, ndkMigratedLibs) {
958 nonvariantLibs = append(nonvariantLibs, entry+".ndk."+version)
959 } else {
960 variantLibs = append(variantLibs, entry+ndkLibrarySuffix)
961 }
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700962 } else if ctx.useVndk() && inList(entry, llndkLibraries) {
Dan Willemsenb916b802017-03-19 13:44:32 -0700963 nonvariantLibs = append(nonvariantLibs, entry+llndkLibrarySuffix)
Jiyong Park374510b2018-03-19 18:23:01 +0900964 } else if (ctx.Platform() || ctx.ProductSpecific()) && inList(entry, vendorPublicLibraries) {
965 vendorPublicLib := entry + vendorPublicLibrarySuffix
966 if actx.OtherModuleExists(vendorPublicLib) {
967 nonvariantLibs = append(nonvariantLibs, vendorPublicLib)
968 } else {
969 // This can happen if vendor_public_library module is defined in a
970 // namespace that isn't visible to the current module. In that case,
971 // link to the original library.
972 nonvariantLibs = append(nonvariantLibs, entry)
973 }
Dan Albert914449f2016-06-17 16:45:24 -0700974 } else {
Dan Willemsen7cbf5f82017-03-28 00:08:30 -0700975 nonvariantLibs = append(nonvariantLibs, entry)
Dan Willemsen72d39932016-07-08 23:23:48 -0700976 }
977 }
Dan Albert914449f2016-06-17 16:45:24 -0700978 return nonvariantLibs, variantLibs
Dan Willemsen72d39932016-07-08 23:23:48 -0700979 }
980
Dan Albert914449f2016-06-17 16:45:24 -0700981 deps.SharedLibs, variantNdkLibs = rewriteNdkLibs(deps.SharedLibs)
982 deps.LateSharedLibs, variantLateNdkLibs = rewriteNdkLibs(deps.LateSharedLibs)
Jiyong Park4c35af02017-07-05 13:41:55 +0900983 deps.ReexportSharedLibHeaders, _ = rewriteNdkLibs(deps.ReexportSharedLibHeaders)
Dan Willemsen72d39932016-07-08 23:23:48 -0700984 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700985
Colin Cross32ec36c2016-12-15 07:39:51 -0800986 for _, lib := range deps.HeaderLibs {
987 depTag := headerDepTag
988 if inList(lib, deps.ReexportHeaderLibHeaders) {
989 depTag = headerExportDepTag
990 }
991 actx.AddVariationDependencies(nil, depTag, lib)
992 }
Colin Cross5950f382016-12-13 12:50:57 -0800993
Dan Willemsen59339a22018-07-22 21:18:45 -0700994 actx.AddVariationDependencies([]blueprint.Variation{
995 {Mutator: "link", Variation: "static"},
996 }, wholeStaticDepTag, deps.WholeStaticLibs...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700997
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700998 for _, lib := range deps.StaticLibs {
999 depTag := staticDepTag
1000 if inList(lib, deps.ReexportStaticLibHeaders) {
1001 depTag = staticExportDepTag
1002 }
Dan Willemsen59339a22018-07-22 21:18:45 -07001003 actx.AddVariationDependencies([]blueprint.Variation{
1004 {Mutator: "link", Variation: "static"},
1005 }, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001006 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001007
Dan Willemsen59339a22018-07-22 21:18:45 -07001008 actx.AddVariationDependencies([]blueprint.Variation{
1009 {Mutator: "link", Variation: "static"},
1010 }, lateStaticDepTag, deps.LateStaticLibs...)
Colin Crossc99deeb2016-04-11 15:06:20 -07001011
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001012 for _, lib := range deps.SharedLibs {
1013 depTag := sharedDepTag
1014 if inList(lib, deps.ReexportSharedLibHeaders) {
1015 depTag = sharedExportDepTag
1016 }
Dan Willemsen59339a22018-07-22 21:18:45 -07001017 actx.AddVariationDependencies([]blueprint.Variation{
1018 {Mutator: "link", Variation: "shared"},
1019 }, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001020 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001021
Dan Willemsen59339a22018-07-22 21:18:45 -07001022 actx.AddVariationDependencies([]blueprint.Variation{
1023 {Mutator: "link", Variation: "shared"},
1024 }, lateSharedDepTag, deps.LateSharedLibs...)
Colin Crossc99deeb2016-04-11 15:06:20 -07001025
Dan Willemsen59339a22018-07-22 21:18:45 -07001026 actx.AddVariationDependencies([]blueprint.Variation{
1027 {Mutator: "link", Variation: "shared"},
1028 }, runtimeDepTag, deps.RuntimeLibs...)
Logan Chien43d34c32017-12-20 01:17:32 +08001029
Colin Cross68861832016-07-08 10:41:41 -07001030 actx.AddDependency(c, genSourceDepTag, deps.GeneratedSources...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -07001031
1032 for _, gen := range deps.GeneratedHeaders {
1033 depTag := genHeaderDepTag
1034 if inList(gen, deps.ReexportGeneratedHeaders) {
1035 depTag = genHeaderExportDepTag
1036 }
1037 actx.AddDependency(c, depTag, gen)
1038 }
Dan Willemsenb40aab62016-04-20 14:21:14 -07001039
Colin Cross68861832016-07-08 10:41:41 -07001040 actx.AddDependency(c, objDepTag, deps.ObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -07001041
1042 if deps.CrtBegin != "" {
Colin Cross68861832016-07-08 10:41:41 -07001043 actx.AddDependency(c, crtBeginDepTag, deps.CrtBegin)
Colin Crossca860ac2016-01-04 14:34:37 -08001044 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001045 if deps.CrtEnd != "" {
Colin Cross68861832016-07-08 10:41:41 -07001046 actx.AddDependency(c, crtEndDepTag, deps.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -07001047 }
Dan Willemsenc77a0b32017-09-18 23:19:12 -07001048 if deps.LinkerScript != "" {
1049 actx.AddDependency(c, linkerScriptDepTag, deps.LinkerScript)
1050 }
Dan Albert914449f2016-06-17 16:45:24 -07001051
1052 version := ctx.sdkVersion()
1053 actx.AddVariationDependencies([]blueprint.Variation{
Dan Willemsen59339a22018-07-22 21:18:45 -07001054 {Mutator: "ndk_api", Variation: version},
1055 {Mutator: "link", Variation: "shared"},
1056 }, ndkStubDepTag, variantNdkLibs...)
Dan Albert914449f2016-06-17 16:45:24 -07001057 actx.AddVariationDependencies([]blueprint.Variation{
Dan Willemsen59339a22018-07-22 21:18:45 -07001058 {Mutator: "ndk_api", Variation: version},
1059 {Mutator: "link", Variation: "shared"},
1060 }, ndkLateStubDepTag, variantLateNdkLibs...)
Logan Chienf3511742017-10-31 18:04:35 +08001061
1062 if vndkdep := c.vndkdep; vndkdep != nil {
1063 if vndkdep.isVndkExt() {
1064 baseModuleMode := vendorMode
1065 if actx.DeviceConfig().VndkVersion() == "" {
1066 baseModuleMode = coreMode
1067 }
1068 actx.AddVariationDependencies([]blueprint.Variation{
Dan Willemsen59339a22018-07-22 21:18:45 -07001069 {Mutator: "image", Variation: baseModuleMode},
1070 {Mutator: "link", Variation: "shared"},
1071 }, vndkExtDepTag, vndkdep.getVndkExtendsModuleName())
Logan Chienf3511742017-10-31 18:04:35 +08001072 }
1073 }
Colin Cross6362e272015-10-29 15:25:03 -07001074}
Colin Cross21b9a242015-03-24 14:15:58 -07001075
Dan Albert7e9d2952016-08-04 13:02:36 -07001076func beginMutator(ctx android.BottomUpMutatorContext) {
1077 if c, ok := ctx.Module().(*Module); ok && c.Enabled() {
1078 c.beginMutator(ctx)
1079 }
1080}
1081
Colin Crossca860ac2016-01-04 14:34:37 -08001082func (c *Module) clang(ctx BaseModuleContext) bool {
1083 clang := Bool(c.Properties.Clang)
1084
1085 if c.Properties.Clang == nil {
Stephen Hines6ea0f812018-01-11 11:56:19 -08001086 clang = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001087 }
Colin Cross28344522015-04-22 13:07:53 -07001088
Colin Crossca860ac2016-01-04 14:34:37 -08001089 if !c.toolchain(ctx).ClangSupported() {
1090 clang = false
1091 }
1092
1093 return clang
1094}
1095
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001096// Whether a module can link to another module, taking into
1097// account NDK linking.
Logan Chienf3511742017-10-31 18:04:35 +08001098func checkLinkType(ctx android.ModuleContext, from *Module, to *Module, tag dependencyTag) {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001099 if from.Target().Os != android.Android {
1100 // Host code is not restricted
1101 return
1102 }
1103 if from.Properties.UseVndk {
1104 // Though vendor code is limited by the vendor mutator,
1105 // each vendor-available module needs to check
1106 // link-type for VNDK.
1107 if from.vndkdep != nil {
Logan Chienf3511742017-10-31 18:04:35 +08001108 from.vndkdep.vndkCheckLinkType(ctx, to, tag)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001109 }
1110 return
1111 }
Nan Zhang0007d812017-11-07 10:57:05 -08001112 if String(from.Properties.Sdk_version) == "" {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001113 // Platform code can link to anything
1114 return
1115 }
Jiyong Parkf9332f12018-02-01 00:54:12 +09001116 if from.inRecovery() {
1117 // Recovery code is not NDK
1118 return
1119 }
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001120 if _, ok := to.linker.(*toolchainLibraryDecorator); ok {
1121 // These are always allowed
1122 return
1123 }
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001124 if _, ok := to.linker.(*ndkPrebuiltStlLinker); ok {
1125 // These are allowed, but they don't set sdk_version
1126 return
1127 }
1128 if _, ok := to.linker.(*stubDecorator); ok {
1129 // These aren't real libraries, but are the stub shared libraries that are included in
1130 // the NDK.
1131 return
1132 }
Nan Zhang0007d812017-11-07 10:57:05 -08001133 if String(to.Properties.Sdk_version) == "" {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001134 // NDK code linking to platform code is never okay.
1135 ctx.ModuleErrorf("depends on non-NDK-built library %q",
1136 ctx.OtherModuleName(to))
1137 }
1138
1139 // At this point we know we have two NDK libraries, but we need to
1140 // check that we're not linking against anything built against a higher
1141 // API level, as it is only valid to link against older or equivalent
1142 // APIs.
1143
Inseob Kim01a28722018-04-11 09:48:45 +09001144 // Current can link against anything.
1145 if String(from.Properties.Sdk_version) != "current" {
1146 // Otherwise we need to check.
1147 if String(to.Properties.Sdk_version) == "current" {
1148 // Current can't be linked against by anything else.
1149 ctx.ModuleErrorf("links %q built against newer API version %q",
1150 ctx.OtherModuleName(to), "current")
1151 } else {
1152 fromApi, err := strconv.Atoi(String(from.Properties.Sdk_version))
1153 if err != nil {
1154 ctx.PropertyErrorf("sdk_version",
Inseob Kim34b22832018-04-11 10:13:16 +09001155 "Invalid sdk_version value (must be int or current): %q",
Inseob Kim01a28722018-04-11 09:48:45 +09001156 String(from.Properties.Sdk_version))
1157 }
1158 toApi, err := strconv.Atoi(String(to.Properties.Sdk_version))
1159 if err != nil {
1160 ctx.PropertyErrorf("sdk_version",
Inseob Kim34b22832018-04-11 10:13:16 +09001161 "Invalid sdk_version value (must be int or current): %q",
Inseob Kim01a28722018-04-11 09:48:45 +09001162 String(to.Properties.Sdk_version))
1163 }
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001164
Inseob Kim01a28722018-04-11 09:48:45 +09001165 if toApi > fromApi {
1166 ctx.ModuleErrorf("links %q built against newer API version %q",
1167 ctx.OtherModuleName(to), String(to.Properties.Sdk_version))
1168 }
1169 }
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001170 }
Dan Albert202fe492017-12-15 13:56:59 -08001171
1172 // Also check that the two STL choices are compatible.
1173 fromStl := from.stl.Properties.SelectedStl
1174 toStl := to.stl.Properties.SelectedStl
1175 if fromStl == "" || toStl == "" {
1176 // Libraries that don't use the STL are unrestricted.
Inseob Kimda2171a2018-04-11 15:41:38 +09001177 } else if fromStl == "ndk_system" || toStl == "ndk_system" {
Dan Albert202fe492017-12-15 13:56:59 -08001178 // We can be permissive with the system "STL" since it is only the C++
1179 // ABI layer, but in the future we should make sure that everyone is
1180 // using either libc++ or nothing.
Inseob Kimda2171a2018-04-11 15:41:38 +09001181 } else if getNdkStlFamily(ctx, from) != getNdkStlFamily(ctx, to) {
Dan Albert202fe492017-12-15 13:56:59 -08001182 ctx.ModuleErrorf("uses %q and depends on %q which uses incompatible %q",
1183 from.stl.Properties.SelectedStl, ctx.OtherModuleName(to),
1184 to.stl.Properties.SelectedStl)
1185 }
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001186}
1187
Jiyong Park5fb8c102018-04-09 12:03:06 +09001188// Tests whether the dependent library is okay to be double loaded inside a single process.
1189// If a library is a member of VNDK and at the same time dependencies of an LLNDK library,
1190// it is subject to be double loaded. Such lib should be explicitly marked as double_loaded: true
1191// or as vndk-sp (vndk: { enabled: true, support_system_process: true}).
1192func checkDoubleLoadableLibries(ctx android.ModuleContext, from *Module, to *Module) {
1193 if _, ok := from.linker.(*libraryDecorator); !ok {
1194 return
1195 }
1196
1197 if inList(ctx.ModuleName(), llndkLibraries) ||
1198 (from.useVndk() && Bool(from.VendorProperties.Double_loadable)) {
1199 _, depIsLlndk := to.linker.(*llndkStubDecorator)
1200 depIsVndkSp := false
1201 if to.vndkdep != nil && to.vndkdep.isVndkSp() {
1202 depIsVndkSp = true
1203 }
1204 depIsVndk := false
1205 if to.vndkdep != nil && to.vndkdep.isVndk() {
1206 depIsVndk = true
1207 }
1208 depIsDoubleLoadable := Bool(to.VendorProperties.Double_loadable)
1209 if !depIsLlndk && !depIsVndkSp && !depIsDoubleLoadable && depIsVndk {
1210 ctx.ModuleErrorf("links VNDK library %q that isn't double_loadable.",
1211 ctx.OtherModuleName(to))
1212 }
1213 }
1214}
1215
Colin Crossc99deeb2016-04-11 15:06:20 -07001216// Convert dependencies to paths. Returns a PathDeps containing paths
Colin Cross635c3b02016-05-18 15:37:25 -07001217func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
Colin Crossca860ac2016-01-04 14:34:37 -08001218 var depPaths PathDeps
Colin Crossca860ac2016-01-04 14:34:37 -08001219
Jeff Gaston294356f2017-09-27 17:05:30 -07001220 directStaticDeps := []*Module{}
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001221 directSharedDeps := []*Module{}
Jeff Gaston294356f2017-09-27 17:05:30 -07001222
Colin Crossd11fcda2017-10-23 17:59:01 -07001223 ctx.VisitDirectDeps(func(dep android.Module) {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001224 depName := ctx.OtherModuleName(dep)
1225 depTag := ctx.OtherModuleDependencyTag(dep)
Dan Albert9e10cd42016-08-03 14:12:14 -07001226
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001227 ccDep, _ := dep.(*Module)
1228 if ccDep == nil {
1229 // handling for a few module types that aren't cc Module but that are also supported
1230 switch depTag {
Dan Willemsenb40aab62016-04-20 14:21:14 -07001231 case genSourceDepTag:
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001232 if genRule, ok := dep.(genrule.SourceFileGenerator); ok {
Dan Willemsenb40aab62016-04-20 14:21:14 -07001233 depPaths.GeneratedSources = append(depPaths.GeneratedSources,
1234 genRule.GeneratedSourceFiles()...)
1235 } else {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001236 ctx.ModuleErrorf("module %q is not a gensrcs or genrule", depName)
Dan Willemsenb40aab62016-04-20 14:21:14 -07001237 }
Colin Crosse90bfd12017-04-26 16:59:26 -07001238 // Support exported headers from a generated_sources dependency
1239 fallthrough
Dan Willemsenb3454ab2016-09-28 17:34:58 -07001240 case genHeaderDepTag, genHeaderExportDepTag:
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001241 if genRule, ok := dep.(genrule.SourceFileGenerator); ok {
Dan Willemsenb40aab62016-04-20 14:21:14 -07001242 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders,
Dan Willemsen9da9d492018-02-21 18:28:18 -08001243 genRule.GeneratedDeps()...)
Colin Cross5ed99c62016-11-22 12:55:55 -08001244 flags := includeDirsToFlags(genRule.GeneratedHeaderDirs())
Dan Willemsenb3454ab2016-09-28 17:34:58 -07001245 depPaths.Flags = append(depPaths.Flags, flags)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001246 if depTag == genHeaderExportDepTag {
Dan Willemsenb3454ab2016-09-28 17:34:58 -07001247 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags)
Dan Willemsen847dcc72016-09-29 12:13:36 -07001248 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps,
Dan Willemsen9da9d492018-02-21 18:28:18 -08001249 genRule.GeneratedDeps()...)
Jayant Chowdhary715cac32017-04-20 06:53:59 -07001250 // Add these re-exported flags to help header-abi-dumper to infer the abi exported by a library.
1251 c.sabi.Properties.ReexportedIncludeFlags = append(c.sabi.Properties.ReexportedIncludeFlags, flags)
1252
Dan Willemsenb3454ab2016-09-28 17:34:58 -07001253 }
Dan Willemsenb40aab62016-04-20 14:21:14 -07001254 } else {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001255 ctx.ModuleErrorf("module %q is not a genrule", depName)
Dan Willemsenb40aab62016-04-20 14:21:14 -07001256 }
Dan Willemsenc77a0b32017-09-18 23:19:12 -07001257 case linkerScriptDepTag:
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001258 if genRule, ok := dep.(genrule.SourceFileGenerator); ok {
Dan Willemsenc77a0b32017-09-18 23:19:12 -07001259 files := genRule.GeneratedSourceFiles()
1260 if len(files) == 1 {
1261 depPaths.LinkerScript = android.OptionalPathForPath(files[0])
1262 } else if len(files) > 1 {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001263 ctx.ModuleErrorf("module %q can only generate a single file if used for a linker script", depName)
Dan Willemsenc77a0b32017-09-18 23:19:12 -07001264 }
1265 } else {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001266 ctx.ModuleErrorf("module %q is not a genrule", depName)
Dan Willemsenc77a0b32017-09-18 23:19:12 -07001267 }
Colin Crossca860ac2016-01-04 14:34:37 -08001268 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001269 return
1270 }
1271
Colin Crossd11fcda2017-10-23 17:59:01 -07001272 if dep.Target().Os != ctx.Os() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001273 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1274 return
1275 }
Colin Crossd11fcda2017-10-23 17:59:01 -07001276 if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001277 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
Colin Crossa1ad8d12016-06-01 17:09:44 -07001278 return
1279 }
1280
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001281 // re-exporting flags
1282 if depTag == reuseObjTag {
1283 if l, ok := ccDep.compiler.(libraryInterface); ok {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001284 c.staticVariant = ccDep
Colin Crossbbc9f4d2017-05-03 16:24:55 -07001285 objs, flags, deps := l.reuseObjs()
Colin Cross10d22312017-05-03 11:01:58 -07001286 depPaths.Objs = depPaths.Objs.Append(objs)
1287 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Colin Crossbbc9f4d2017-05-03 16:24:55 -07001288 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps, deps...)
Colin Crossbba99042016-11-23 15:45:05 -08001289 return
1290 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001291 }
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001292 if t, ok := depTag.(dependencyTag); ok && t.library {
1293 if i, ok := ccDep.linker.(exportedFlagsProducer); ok {
Dan Willemsen76f08272016-07-09 00:14:08 -07001294 flags := i.exportedFlags()
Dan Willemsen847dcc72016-09-29 12:13:36 -07001295 deps := i.exportedFlagsDeps()
Dan Willemsen76f08272016-07-09 00:14:08 -07001296 depPaths.Flags = append(depPaths.Flags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -07001297 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders, deps...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001298
1299 if t.reexportFlags {
Dan Willemsen76f08272016-07-09 00:14:08 -07001300 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -07001301 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps, deps...)
Jayant Chowdhary715cac32017-04-20 06:53:59 -07001302 // 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 -07001303 // Re-exported shared library headers must be included as well since they can help us with type information
1304 // about template instantiations (instantiated from their headers).
1305 c.sabi.Properties.ReexportedIncludeFlags = append(c.sabi.Properties.ReexportedIncludeFlags, flags...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001306 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001307 }
Dan Willemsena96ff642016-06-07 12:34:45 -07001308
Logan Chienf3511742017-10-31 18:04:35 +08001309 checkLinkType(ctx, c, ccDep, t)
Jiyong Park5fb8c102018-04-09 12:03:06 +09001310 checkDoubleLoadableLibries(ctx, c, ccDep)
Colin Crossc99deeb2016-04-11 15:06:20 -07001311 }
1312
Colin Cross26c34ed2016-09-30 17:10:16 -07001313 var ptr *android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -07001314 var depPtr *android.Paths
Colin Crossc99deeb2016-04-11 15:06:20 -07001315
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001316 linkFile := ccDep.outputFile
Colin Cross26c34ed2016-09-30 17:10:16 -07001317 depFile := android.OptionalPath{}
1318
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001319 switch depTag {
Dan Albert914449f2016-06-17 16:45:24 -07001320 case ndkStubDepTag, sharedDepTag, sharedExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001321 ptr = &depPaths.SharedLibs
1322 depPtr = &depPaths.SharedLibsDeps
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001323 depFile = ccDep.linker.(libraryInterface).toc()
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001324 directSharedDeps = append(directSharedDeps, ccDep)
Dan Albert914449f2016-06-17 16:45:24 -07001325 case lateSharedDepTag, ndkLateStubDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001326 ptr = &depPaths.LateSharedLibs
1327 depPtr = &depPaths.LateSharedLibsDeps
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001328 depFile = ccDep.linker.(libraryInterface).toc()
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001329 case staticDepTag, staticExportDepTag:
Jeff Gaston294356f2017-09-27 17:05:30 -07001330 ptr = nil
1331 directStaticDeps = append(directStaticDeps, ccDep)
Colin Crossc99deeb2016-04-11 15:06:20 -07001332 case lateStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001333 ptr = &depPaths.LateStaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -07001334 case wholeStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001335 ptr = &depPaths.WholeStaticLibs
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001336 staticLib, ok := ccDep.linker.(libraryInterface)
Colin Crossb916a382016-07-29 17:28:03 -07001337 if !ok || !staticLib.static() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001338 ctx.ModuleErrorf("module %q not a static library", depName)
Colin Crossc99deeb2016-04-11 15:06:20 -07001339 return
1340 }
1341
1342 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001343 postfix := " (required by " + ctx.OtherModuleName(dep) + ")"
Colin Crossc99deeb2016-04-11 15:06:20 -07001344 for i := range missingDeps {
1345 missingDeps[i] += postfix
1346 }
1347 ctx.AddMissingDependencies(missingDeps)
1348 }
Dan Willemsen5cb580f2016-09-26 17:33:01 -07001349 depPaths.WholeStaticLibObjs = depPaths.WholeStaticLibObjs.Append(staticLib.objs())
Colin Cross5950f382016-12-13 12:50:57 -08001350 case headerDepTag:
1351 // Nothing
Colin Crossc99deeb2016-04-11 15:06:20 -07001352 case objDepTag:
Dan Willemsen5cb580f2016-09-26 17:33:01 -07001353 depPaths.Objs.objFiles = append(depPaths.Objs.objFiles, linkFile.Path())
Colin Crossc99deeb2016-04-11 15:06:20 -07001354 case crtBeginDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001355 depPaths.CrtBegin = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -07001356 case crtEndDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001357 depPaths.CrtEnd = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -07001358 }
1359
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001360 switch depTag {
Dan Willemsen581341d2017-02-09 16:16:31 -08001361 case staticDepTag, staticExportDepTag, lateStaticDepTag:
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001362 staticLib, ok := ccDep.linker.(libraryInterface)
Dan Willemsen581341d2017-02-09 16:16:31 -08001363 if !ok || !staticLib.static() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001364 ctx.ModuleErrorf("module %q not a static library", depName)
Dan Willemsen581341d2017-02-09 16:16:31 -08001365 return
1366 }
1367
1368 // When combining coverage files for shared libraries and executables, coverage files
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001369 // in static libraries act as if they were whole static libraries. The same goes for
1370 // source based Abi dump files.
Dan Willemsen581341d2017-02-09 16:16:31 -08001371 depPaths.StaticLibObjs.coverageFiles = append(depPaths.StaticLibObjs.coverageFiles,
1372 staticLib.objs().coverageFiles...)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001373 depPaths.StaticLibObjs.sAbiDumpFiles = append(depPaths.StaticLibObjs.sAbiDumpFiles,
1374 staticLib.objs().sAbiDumpFiles...)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001375
Dan Willemsen581341d2017-02-09 16:16:31 -08001376 }
1377
Colin Cross26c34ed2016-09-30 17:10:16 -07001378 if ptr != nil {
Colin Crossce75d2c2016-10-06 16:12:58 -07001379 if !linkFile.Valid() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001380 ctx.ModuleErrorf("module %q missing output file", depName)
Colin Crossce75d2c2016-10-06 16:12:58 -07001381 return
1382 }
Colin Cross26c34ed2016-09-30 17:10:16 -07001383 *ptr = append(*ptr, linkFile.Path())
1384 }
1385
Colin Crossc99deeb2016-04-11 15:06:20 -07001386 if depPtr != nil {
Colin Cross26c34ed2016-09-30 17:10:16 -07001387 dep := depFile
1388 if !dep.Valid() {
1389 dep = linkFile
1390 }
1391 *depPtr = append(*depPtr, dep.Path())
Colin Crossca860ac2016-01-04 14:34:37 -08001392 }
Jiyong Park27b188b2017-07-18 13:23:39 +09001393
Logan Chien43d34c32017-12-20 01:17:32 +08001394 makeLibName := func(depName string) string {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001395 libName := strings.TrimSuffix(depName, llndkLibrarySuffix)
Jiyong Park374510b2018-03-19 18:23:01 +09001396 libName = strings.TrimSuffix(libName, vendorPublicLibrarySuffix)
Jiyong Park27b188b2017-07-18 13:23:39 +09001397 libName = strings.TrimPrefix(libName, "prebuilt_")
Jiyong Parkd5b18a52017-08-03 21:22:50 +09001398 isLLndk := inList(libName, llndkLibraries)
Jiyong Park374510b2018-03-19 18:23:01 +09001399 isVendorPublicLib := inList(libName, vendorPublicLibraries)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001400 bothVendorAndCoreVariantsExist := ccDep.hasVendorVariant() || isLLndk
1401 if c.useVndk() && bothVendorAndCoreVariantsExist {
1402 // The vendor module in Make will have been renamed to not conflict with the core
1403 // module, so update the dependency name here accordingly.
Logan Chien43d34c32017-12-20 01:17:32 +08001404 return libName + vendorSuffix
Jiyong Park374510b2018-03-19 18:23:01 +09001405 } else if (ctx.Platform() || ctx.ProductSpecific()) && isVendorPublicLib {
Logan Chien43d34c32017-12-20 01:17:32 +08001406 return libName + vendorPublicLibrarySuffix
Jiyong Parkf9332f12018-02-01 00:54:12 +09001407 } else if ccDep.inRecovery() && !ccDep.onlyInRecovery() {
1408 return libName + recoverySuffix
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001409 } else {
Logan Chien43d34c32017-12-20 01:17:32 +08001410 return libName
Jiyong Park27b188b2017-07-18 13:23:39 +09001411 }
Logan Chien43d34c32017-12-20 01:17:32 +08001412 }
1413
1414 // Export the shared libs to Make.
1415 switch depTag {
1416 case sharedDepTag, sharedExportDepTag, lateSharedDepTag:
Jiyong Park27b188b2017-07-18 13:23:39 +09001417 // Note: the order of libs in this list is not important because
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001418 // they merely serve as Make dependencies and do not affect this lib itself.
Logan Chien43d34c32017-12-20 01:17:32 +08001419 c.Properties.AndroidMkSharedLibs = append(
1420 c.Properties.AndroidMkSharedLibs, makeLibName(depName))
1421 case runtimeDepTag:
1422 c.Properties.AndroidMkRuntimeLibs = append(
1423 c.Properties.AndroidMkRuntimeLibs, makeLibName(depName))
Jiyong Park27b188b2017-07-18 13:23:39 +09001424 }
Colin Crossca860ac2016-01-04 14:34:37 -08001425 })
1426
Jeff Gaston294356f2017-09-27 17:05:30 -07001427 // use the ordered dependencies as this module's dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001428 depPaths.StaticLibs = append(depPaths.StaticLibs, orderStaticModuleDeps(c, directStaticDeps, directSharedDeps)...)
Jeff Gaston294356f2017-09-27 17:05:30 -07001429
Colin Crossdd84e052017-05-17 13:44:16 -07001430 // Dedup exported flags from dependencies
Colin Crossb6715442017-10-24 11:13:31 -07001431 depPaths.Flags = android.FirstUniqueStrings(depPaths.Flags)
Dan Willemsenfe92c962017-08-29 12:28:37 -07001432 depPaths.GeneratedHeaders = android.FirstUniquePaths(depPaths.GeneratedHeaders)
Colin Crossb6715442017-10-24 11:13:31 -07001433 depPaths.ReexportedFlags = android.FirstUniqueStrings(depPaths.ReexportedFlags)
Dan Willemsenfe92c962017-08-29 12:28:37 -07001434 depPaths.ReexportedFlagsDeps = android.FirstUniquePaths(depPaths.ReexportedFlagsDeps)
1435
1436 if c.sabi != nil {
Colin Crossb6715442017-10-24 11:13:31 -07001437 c.sabi.Properties.ReexportedIncludeFlags = android.FirstUniqueStrings(c.sabi.Properties.ReexportedIncludeFlags)
Dan Willemsenfe92c962017-08-29 12:28:37 -07001438 }
Colin Crossdd84e052017-05-17 13:44:16 -07001439
Colin Crossca860ac2016-01-04 14:34:37 -08001440 return depPaths
1441}
1442
1443func (c *Module) InstallInData() bool {
1444 if c.installer == nil {
1445 return false
1446 }
Vishwath Mohan1dd88392017-03-29 22:00:18 -07001447 return c.installer.inData()
1448}
1449
1450func (c *Module) InstallInSanitizerDir() bool {
1451 if c.installer == nil {
1452 return false
1453 }
1454 if c.sanitize != nil && c.sanitize.inSanitizerDir() {
Colin Cross94610402016-08-29 13:41:32 -07001455 return true
1456 }
Vishwath Mohan1dd88392017-03-29 22:00:18 -07001457 return c.installer.inSanitizerDir()
Colin Crossca860ac2016-01-04 14:34:37 -08001458}
1459
Jiyong Parkf9332f12018-02-01 00:54:12 +09001460func (c *Module) InstallInRecovery() bool {
1461 return c.inRecovery()
1462}
1463
Dan Willemsen4aa75ca2016-09-28 16:18:03 -07001464func (c *Module) HostToolPath() android.OptionalPath {
1465 if c.installer == nil {
1466 return android.OptionalPath{}
1467 }
1468 return c.installer.hostToolPath()
1469}
1470
Nan Zhangd4e641b2017-07-12 12:55:28 -07001471func (c *Module) IntermPathForModuleOut() android.OptionalPath {
1472 return c.outputFile
1473}
1474
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001475func (c *Module) Srcs() android.Paths {
1476 if c.outputFile.Valid() {
1477 return android.Paths{c.outputFile.Path()}
1478 }
1479 return android.Paths{}
1480}
1481
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001482func (c *Module) static() bool {
1483 if static, ok := c.linker.(interface {
1484 static() bool
1485 }); ok {
1486 return static.static()
1487 }
1488 return false
1489}
1490
Colin Cross2ba19d92015-05-07 15:44:20 -07001491//
Colin Crosscfad1192015-11-02 16:43:11 -08001492// Defaults
1493//
Colin Crossca860ac2016-01-04 14:34:37 -08001494type Defaults struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001495 android.ModuleBase
Colin Cross1f44a3a2017-07-07 14:33:33 -07001496 android.DefaultsModuleBase
Colin Crosscfad1192015-11-02 16:43:11 -08001497}
1498
Colin Cross635c3b02016-05-18 15:37:25 -07001499func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -08001500}
1501
Colin Cross1e676be2016-10-12 14:38:15 -07001502func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
1503}
1504
Colin Cross36242852017-06-23 15:06:31 -07001505func defaultsFactory() android.Module {
Colin Crosse1d764e2016-08-18 14:18:32 -07001506 return DefaultsFactory()
1507}
1508
Colin Cross36242852017-06-23 15:06:31 -07001509func DefaultsFactory(props ...interface{}) android.Module {
Colin Crossca860ac2016-01-04 14:34:37 -08001510 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -08001511
Colin Cross36242852017-06-23 15:06:31 -07001512 module.AddProperties(props...)
1513 module.AddProperties(
Colin Crossca860ac2016-01-04 14:34:37 -08001514 &BaseProperties{},
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001515 &VendorProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001516 &BaseCompilerProperties{},
1517 &BaseLinkerProperties{},
Chih-Hung Hsieh86814712018-05-23 18:30:46 -07001518 &MoreBaseLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -07001519 &LibraryProperties{},
Colin Cross919281a2016-04-05 16:42:05 -07001520 &FlagExporterProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001521 &BinaryLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -07001522 &TestProperties{},
1523 &TestBinaryProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001524 &StlProperties{},
Colin Cross16b23492016-01-06 14:41:07 -08001525 &SanitizeProperties{},
Colin Cross665dce92016-04-28 14:50:03 -07001526 &StripProperties{},
Dan Willemsen7424d612016-09-01 13:45:39 -07001527 &InstallerProperties{},
Dan Willemsena03cf6d2016-09-26 15:45:04 -07001528 &TidyProperties{},
Dan Willemsen581341d2017-02-09 16:16:31 -08001529 &CoverageProperties{},
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001530 &SAbiProperties{},
Justin Yun4b2382f2017-07-26 14:22:10 +09001531 &VndkProperties{},
Stephen Craneba090d12017-05-09 15:44:35 -07001532 &LTOProperties{},
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -07001533 &PgoProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08001534 &android.ProtoProperties{},
Colin Crosse1d764e2016-08-18 14:18:32 -07001535 )
Colin Crosscfad1192015-11-02 16:43:11 -08001536
Colin Cross1f44a3a2017-07-07 14:33:33 -07001537 android.InitDefaultsModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001538
1539 return module
Colin Crosscfad1192015-11-02 16:43:11 -08001540}
1541
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001542const (
1543 // coreMode is the variant used for framework-private libraries, or
1544 // SDK libraries. (which framework-private libraries can use)
1545 coreMode = "core"
1546
1547 // vendorMode is the variant used for /vendor code that compiles
1548 // against the VNDK.
1549 vendorMode = "vendor"
Jiyong Parkf9332f12018-02-01 00:54:12 +09001550
1551 recoveryMode = "recovery"
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001552)
1553
Jiyong Park6a43f042017-10-12 23:05:00 +09001554func squashVendorSrcs(m *Module) {
1555 if lib, ok := m.compiler.(*libraryDecorator); ok {
1556 lib.baseCompiler.Properties.Srcs = append(lib.baseCompiler.Properties.Srcs,
1557 lib.baseCompiler.Properties.Target.Vendor.Srcs...)
1558
1559 lib.baseCompiler.Properties.Exclude_srcs = append(lib.baseCompiler.Properties.Exclude_srcs,
1560 lib.baseCompiler.Properties.Target.Vendor.Exclude_srcs...)
1561 }
1562}
1563
Jiyong Parkf9332f12018-02-01 00:54:12 +09001564func squashRecoverySrcs(m *Module) {
1565 if lib, ok := m.compiler.(*libraryDecorator); ok {
1566 lib.baseCompiler.Properties.Srcs = append(lib.baseCompiler.Properties.Srcs,
1567 lib.baseCompiler.Properties.Target.Recovery.Srcs...)
1568
1569 lib.baseCompiler.Properties.Exclude_srcs = append(lib.baseCompiler.Properties.Exclude_srcs,
1570 lib.baseCompiler.Properties.Target.Recovery.Exclude_srcs...)
1571 }
1572}
1573
1574func imageMutator(mctx android.BottomUpMutatorContext) {
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001575 if mctx.Os() != android.Android {
1576 return
1577 }
1578
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001579 if genrule, ok := mctx.Module().(*genrule.Module); ok {
Jiyong Park3f736c92018-05-24 13:36:56 +09001580 if props, ok := genrule.Extra.(*GenruleExtraProperties); ok {
1581 var coreVariantNeeded bool = false
1582 var vendorVariantNeeded bool = false
1583 var recoveryVariantNeeded bool = false
Justin Yun71549282017-11-17 12:10:28 +09001584 if mctx.DeviceConfig().VndkVersion() == "" {
Jiyong Park3f736c92018-05-24 13:36:56 +09001585 coreVariantNeeded = true
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001586 } else if Bool(props.Vendor_available) {
Jiyong Park3f736c92018-05-24 13:36:56 +09001587 coreVariantNeeded = true
1588 vendorVariantNeeded = true
Jiyong Park2db76922017-11-08 16:03:48 +09001589 } else if mctx.SocSpecific() || mctx.DeviceSpecific() {
Jiyong Park3f736c92018-05-24 13:36:56 +09001590 vendorVariantNeeded = true
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001591 } else {
Jiyong Park3f736c92018-05-24 13:36:56 +09001592 coreVariantNeeded = true
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001593 }
Jiyong Park3f736c92018-05-24 13:36:56 +09001594 if Bool(props.Recovery_available) {
1595 recoveryVariantNeeded = true
1596 }
1597
Jiyong Park413cc742018-06-19 01:46:06 +09001598 if recoveryVariantNeeded {
Jiyong Park8d52f862018-07-07 18:02:07 +09001599 primaryArch := mctx.Config().DevicePrimaryArchType()
1600 moduleArch := genrule.Target().Arch.ArchType
1601 if moduleArch != primaryArch {
Jiyong Park413cc742018-06-19 01:46:06 +09001602 recoveryVariantNeeded = false
1603 }
1604 }
1605
Jiyong Park3f736c92018-05-24 13:36:56 +09001606 var variants []string
1607 if coreVariantNeeded {
1608 variants = append(variants, coreMode)
1609 }
1610 if vendorVariantNeeded {
1611 variants = append(variants, vendorMode)
1612 }
1613 if recoveryVariantNeeded {
1614 variants = append(variants, recoveryMode)
1615 }
1616 mctx.CreateVariations(variants...)
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001617 }
1618 }
1619
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001620 m, ok := mctx.Module().(*Module)
1621 if !ok {
1622 return
1623 }
1624
1625 // Sanity check
Logan Chienf3511742017-10-31 18:04:35 +08001626 vendorSpecific := mctx.SocSpecific() || mctx.DeviceSpecific()
1627
1628 if m.VendorProperties.Vendor_available != nil && vendorSpecific {
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001629 mctx.PropertyErrorf("vendor_available",
Jiyong Park2db76922017-11-08 16:03:48 +09001630 "doesn't make sense at the same time as `vendor: true`, `proprietary: true`, or `device_specific:true`")
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001631 return
1632 }
Logan Chienf3511742017-10-31 18:04:35 +08001633
1634 if vndkdep := m.vndkdep; vndkdep != nil {
1635 if vndkdep.isVndk() {
1636 if vendorSpecific {
1637 if !vndkdep.isVndkExt() {
1638 mctx.PropertyErrorf("vndk",
1639 "must set `extends: \"...\"` to vndk extension")
1640 return
1641 }
1642 } else {
1643 if vndkdep.isVndkExt() {
1644 mctx.PropertyErrorf("vndk",
1645 "must set `vendor: true` to set `extends: %q`",
1646 m.getVndkExtendsModuleName())
1647 return
1648 }
1649 if m.VendorProperties.Vendor_available == nil {
1650 mctx.PropertyErrorf("vndk",
1651 "vendor_available must be set to either true or false when `vndk: {enabled: true}`")
1652 return
1653 }
1654 }
1655 } else {
1656 if vndkdep.isVndkSp() {
1657 mctx.PropertyErrorf("vndk",
1658 "must set `enabled: true` to set `support_system_process: true`")
1659 return
1660 }
1661 if vndkdep.isVndkExt() {
1662 mctx.PropertyErrorf("vndk",
1663 "must set `enabled: true` to set `extends: %q`",
1664 m.getVndkExtendsModuleName())
1665 return
1666 }
Justin Yun8effde42017-06-23 19:24:43 +09001667 }
1668 }
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001669
Jiyong Parkf9332f12018-02-01 00:54:12 +09001670 var coreVariantNeeded bool = false
1671 var vendorVariantNeeded bool = false
1672 var recoveryVariantNeeded bool = false
1673
Justin Yun71549282017-11-17 12:10:28 +09001674 if mctx.DeviceConfig().VndkVersion() == "" {
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001675 // If the device isn't compiling against the VNDK, we always
1676 // use the core mode.
Jiyong Parkf9332f12018-02-01 00:54:12 +09001677 coreVariantNeeded = true
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001678 } else if _, ok := m.linker.(*llndkStubDecorator); ok {
1679 // LL-NDK stubs only exist in the vendor variant, since the
1680 // real libraries will be used in the core variant.
Jiyong Parkf9332f12018-02-01 00:54:12 +09001681 vendorVariantNeeded = true
Jiyong Park2a454122017-10-19 15:59:33 +09001682 } else if _, ok := m.linker.(*llndkHeadersDecorator); ok {
1683 // ... and LL-NDK headers as well
Jiyong Parkf9332f12018-02-01 00:54:12 +09001684 vendorVariantNeeded = true
Justin Yun312ccb92018-01-23 12:07:46 +09001685 } else if _, ok := m.linker.(*vndkPrebuiltLibraryDecorator); ok {
Justin Yun71549282017-11-17 12:10:28 +09001686 // Make vendor variants only for the versions in BOARD_VNDK_VERSION and
1687 // PRODUCT_EXTRA_VNDK_VERSIONS.
Jiyong Parkf9332f12018-02-01 00:54:12 +09001688 vendorVariantNeeded = true
Logan Chienf3511742017-10-31 18:04:35 +08001689 } else if m.hasVendorVariant() && !vendorSpecific {
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001690 // This will be available in both /system and /vendor
Justin Yun8effde42017-06-23 19:24:43 +09001691 // or a /system directory that is available to vendor.
Jiyong Parkf9332f12018-02-01 00:54:12 +09001692 coreVariantNeeded = true
1693 vendorVariantNeeded = true
Logan Chienf3511742017-10-31 18:04:35 +08001694 } else if vendorSpecific && String(m.Properties.Sdk_version) == "" {
Jiyong Park2db76922017-11-08 16:03:48 +09001695 // This will be available in /vendor (or /odm) only
Jiyong Parkf9332f12018-02-01 00:54:12 +09001696 vendorVariantNeeded = true
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001697 } else {
1698 // This is either in /system (or similar: /data), or is a
1699 // modules built with the NDK. Modules built with the NDK
1700 // will be restricted using the existing link type checks.
Jiyong Parkf9332f12018-02-01 00:54:12 +09001701 coreVariantNeeded = true
1702 }
1703
1704 if Bool(m.Properties.Recovery_available) {
1705 recoveryVariantNeeded = true
1706 }
1707
1708 if m.ModuleBase.InstallInRecovery() {
1709 recoveryVariantNeeded = true
1710 coreVariantNeeded = false
1711 }
1712
Jiyong Park413cc742018-06-19 01:46:06 +09001713 if recoveryVariantNeeded {
Jiyong Park8d52f862018-07-07 18:02:07 +09001714 primaryArch := mctx.Config().DevicePrimaryArchType()
1715 moduleArch := m.Target().Arch.ArchType
1716 if moduleArch != primaryArch {
Jiyong Park413cc742018-06-19 01:46:06 +09001717 recoveryVariantNeeded = false
1718 }
1719 }
1720
Jiyong Parkf9332f12018-02-01 00:54:12 +09001721 var variants []string
1722 if coreVariantNeeded {
1723 variants = append(variants, coreMode)
1724 }
1725 if vendorVariantNeeded {
1726 variants = append(variants, vendorMode)
1727 }
1728 if recoveryVariantNeeded {
1729 variants = append(variants, recoveryMode)
1730 }
1731 mod := mctx.CreateVariations(variants...)
1732 for i, v := range variants {
1733 if v == vendorMode {
1734 m := mod[i].(*Module)
1735 m.Properties.UseVndk = true
1736 squashVendorSrcs(m)
1737 } else if v == recoveryMode {
1738 m := mod[i].(*Module)
1739 m.Properties.InRecovery = true
1740 squashRecoverySrcs(m)
1741 }
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001742 }
1743}
1744
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -07001745func getCurrentNdkPrebuiltVersion(ctx DepsContext) string {
Colin Cross6510f912017-11-29 00:27:14 -08001746 if ctx.Config().PlatformSdkVersionInt() > config.NdkMaxPrebuiltVersionInt {
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -07001747 return strconv.Itoa(config.NdkMaxPrebuiltVersionInt)
1748 }
Colin Cross6510f912017-11-29 00:27:14 -08001749 return ctx.Config().PlatformSdkVersion()
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -07001750}
1751
Colin Cross06a931b2015-10-28 17:23:31 -07001752var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07001753var BoolDefault = proptools.BoolDefault
Nan Zhang0007d812017-11-07 10:57:05 -08001754var BoolPtr = proptools.BoolPtr
1755var String = proptools.String
1756var StringPtr = proptools.StringPtr