blob: d04485d71ba2dfa3de29a5a8ebdf13d0ce6dcc48 [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
Evgenii Stepanovd97a6e92018-08-02 16:19:13 -070049 ctx.TopDown("hwasan_deps", sanitizerDepsMutator(hwasan))
50 ctx.BottomUp("hwasan", sanitizerMutator(hwasan)).Parallel()
51
Vishwath Mohanb743e9c2017-11-01 09:20:21 +000052 ctx.TopDown("cfi_deps", sanitizerDepsMutator(cfi))
53 ctx.BottomUp("cfi", sanitizerMutator(cfi)).Parallel()
54
Colin Cross1e676be2016-10-12 14:38:15 -070055 ctx.TopDown("tsan_deps", sanitizerDepsMutator(tsan))
56 ctx.BottomUp("tsan", sanitizerMutator(tsan)).Parallel()
Dan Willemsen581341d2017-02-09 16:16:31 -080057
Colin Cross6b753602018-06-21 13:03:07 -070058 ctx.TopDown("sanitize_runtime_deps", sanitizerRuntimeDepsMutator)
Ivan Lozano30c5db22018-02-21 15:49:20 -080059
Pirama Arumuga Nainar0b882f02018-04-23 22:44:39 +000060 ctx.BottomUp("coverage", coverageLinkingMutator).Parallel()
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -080061 ctx.TopDown("vndk_deps", sabiDepsMutator)
Stephen Craneba090d12017-05-09 15:44:35 -070062
63 ctx.TopDown("lto_deps", ltoDepsMutator)
64 ctx.BottomUp("lto", ltoMutator).Parallel()
Colin Cross1e676be2016-10-12 14:38:15 -070065 })
Colin Crossb98c8b02016-07-29 13:44:28 -070066
67 pctx.Import("android/soong/cc/config")
Colin Cross463a90e2015-06-17 14:20:06 -070068}
69
Colin Crossca860ac2016-01-04 14:34:37 -080070type Deps struct {
71 SharedLibs, LateSharedLibs []string
72 StaticLibs, LateStaticLibs, WholeStaticLibs []string
Colin Cross5950f382016-12-13 12:50:57 -080073 HeaderLibs []string
Logan Chien43d34c32017-12-20 01:17:32 +080074 RuntimeLibs []string
Colin Crossc472d572015-03-17 15:06:21 -070075
Colin Cross5950f382016-12-13 12:50:57 -080076 ReexportSharedLibHeaders, ReexportStaticLibHeaders, ReexportHeaderLibHeaders []string
Dan Willemsen490a8dc2016-06-06 18:22:19 -070077
Colin Cross81413472016-04-11 14:37:39 -070078 ObjFiles []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -070079
Dan Willemsenb40aab62016-04-20 14:21:14 -070080 GeneratedSources []string
81 GeneratedHeaders []string
82
Dan Willemsenb3454ab2016-09-28 17:34:58 -070083 ReexportGeneratedHeaders []string
84
Colin Cross97ba0732015-03-23 17:50:24 -070085 CrtBegin, CrtEnd string
Dan Willemsenc77a0b32017-09-18 23:19:12 -070086 LinkerScript string
Colin Crossc472d572015-03-17 15:06:21 -070087}
88
Colin Crossca860ac2016-01-04 14:34:37 -080089type PathDeps struct {
Colin Cross26c34ed2016-09-30 17:10:16 -070090 // Paths to .so files
91 SharedLibs, LateSharedLibs android.Paths
92 // Paths to the dependencies to use for .so files (.so.toc files)
93 SharedLibsDeps, LateSharedLibsDeps android.Paths
94 // Paths to .a files
Colin Cross635c3b02016-05-18 15:37:25 -070095 StaticLibs, LateStaticLibs, WholeStaticLibs android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -070096
Colin Cross26c34ed2016-09-30 17:10:16 -070097 // Paths to .o files
Dan Willemsen5cb580f2016-09-26 17:33:01 -070098 Objs Objects
Dan Willemsen581341d2017-02-09 16:16:31 -080099 StaticLibObjs Objects
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700100 WholeStaticLibObjs Objects
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700101
Colin Cross26c34ed2016-09-30 17:10:16 -0700102 // Paths to generated source files
Colin Cross635c3b02016-05-18 15:37:25 -0700103 GeneratedSources android.Paths
104 GeneratedHeaders android.Paths
Dan Willemsenb40aab62016-04-20 14:21:14 -0700105
Dan Willemsen76f08272016-07-09 00:14:08 -0700106 Flags, ReexportedFlags []string
Dan Willemsen847dcc72016-09-29 12:13:36 -0700107 ReexportedFlagsDeps android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700108
Colin Cross26c34ed2016-09-30 17:10:16 -0700109 // Paths to crt*.o files
Colin Cross635c3b02016-05-18 15:37:25 -0700110 CrtBegin, CrtEnd android.OptionalPath
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700111 LinkerScript android.OptionalPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700112}
113
Colin Crossca860ac2016-01-04 14:34:37 -0800114type Flags struct {
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700115 GlobalFlags []string // Flags that apply to C, C++, and assembly source files
116 ArFlags []string // Flags that apply to ar
117 AsFlags []string // Flags that apply to assembly source files
118 CFlags []string // Flags that apply to C and C++ source files
119 ToolingCFlags []string // Flags that apply to C and C++ source files parsed by clang LibTooling tools
120 ConlyFlags []string // Flags that apply to C source files
121 CppFlags []string // Flags that apply to C++ source files
122 ToolingCppFlags []string // Flags that apply to C++ source files parsed by clang LibTooling tools
123 YaccFlags []string // Flags that apply to Yacc source files
124 protoFlags []string // Flags that apply to proto source files
Joe Onorato09e94ab2017-11-18 18:23:14 -0800125 protoOutParams []string // Flags that modify the output of proto generated files
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700126 aidlFlags []string // Flags that apply to aidl source files
127 rsFlags []string // Flags that apply to renderscript source files
128 LdFlags []string // Flags that apply to linker command lines
129 libFlags []string // Flags to add libraries early to the link order
130 TidyFlags []string // Flags that apply to clang-tidy
131 SAbiFlags []string // Flags that apply to header-abi-dumper
132 YasmFlags []string // Flags that apply to yasm assembly source files
Colin Cross28344522015-04-22 13:07:53 -0700133
Colin Crossc3199482017-03-30 15:03:04 -0700134 // Global include flags that apply to C, C++, and assembly source files
135 // These must be after any module include flags, which will be in GlobalFlags.
136 SystemIncludeFlags []string
137
Colin Crossb98c8b02016-07-29 13:44:28 -0700138 Toolchain config.Toolchain
Colin Cross28344522015-04-22 13:07:53 -0700139 Clang bool
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700140 Tidy bool
Dan Willemsen581341d2017-02-09 16:16:31 -0800141 Coverage bool
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800142 SAbiDump bool
Dan Willemsenab9f4262018-02-14 13:58:34 -0800143 ProtoRoot bool
Colin Crossca860ac2016-01-04 14:34:37 -0800144
145 RequiredInstructionSet string
Colin Cross16b23492016-01-06 14:41:07 -0800146 DynamicLinker string
147
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700148 CFlagsDeps android.Paths // Files depended on by compiler flags
149 LdFlagsDeps android.Paths // Files depended on by linker flags
Colin Cross18c0c5a2016-12-01 14:45:23 -0800150
151 GroupStaticLibs bool
Zhizhou Yang51be6322018-02-08 18:32:11 -0800152 ArGoldPlugin bool // Whether LLVM gold plugin option is passed to llvm-ar
Colin Crossc472d572015-03-17 15:06:21 -0700153}
154
Colin Cross81413472016-04-11 14:37:39 -0700155type ObjectLinkerProperties struct {
156 // names of other cc_object modules to link into this module using partial linking
157 Objs []string `android:"arch_variant"`
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700158
159 // if set, add an extra objcopy --prefix-symbols= step
Nan Zhang0007d812017-11-07 10:57:05 -0800160 Prefix_symbols *string
Colin Cross81413472016-04-11 14:37:39 -0700161}
162
Colin Crossca860ac2016-01-04 14:34:37 -0800163// Properties used to compile all C or C++ modules
164type BaseProperties struct {
Dan Willemsen742a5452018-07-23 17:19:36 -0700165 // Deprecated. true is the default, false is invalid.
Colin Crossca860ac2016-01-04 14:34:37 -0800166 Clang *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700167
Dan Willemsen742a5452018-07-23 17:19:36 -0700168 // Some internals still need GCC (toolchain_library)
169 Gcc bool `blueprint:"mutated"`
170
Colin Cross7d5136f2015-05-11 13:39:40 -0700171 // Minimum sdk version supported when compiling against the ndk
Nan Zhang0007d812017-11-07 10:57:05 -0800172 Sdk_version *string
Colin Cross7d5136f2015-05-11 13:39:40 -0700173
Logan Chien43d34c32017-12-20 01:17:32 +0800174 AndroidMkSharedLibs []string `blueprint:"mutated"`
175 AndroidMkRuntimeLibs []string `blueprint:"mutated"`
176 HideFromMake bool `blueprint:"mutated"`
177 PreventInstall bool `blueprint:"mutated"`
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700178
179 UseVndk bool `blueprint:"mutated"`
Colin Cross5beccee2017-12-07 15:28:59 -0800180
181 // *.logtags files, to combine together in order to generate the /system/etc/event-log-tags
182 // file
183 Logtags []string
Jiyong Parkf9332f12018-02-01 00:54:12 +0900184
185 // Make this module available when building for recovery
186 Recovery_available *bool
187
188 InRecovery bool `blueprint:"mutated"`
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700189}
190
191type VendorProperties struct {
Jiyong Park82e2bf32017-08-16 14:05:54 +0900192 // whether this module should be allowed to be directly depended by other
193 // modules with `vendor: true`, `proprietary: true`, or `vendor_available:true`.
194 // If set to true, two variants will be built separately, one like
195 // normal, and the other limited to the set of libraries and headers
196 // that are exposed to /vendor modules.
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700197 //
198 // The vendor variant may be used with a different (newer) /system,
199 // so it shouldn't have any unversioned runtime dependencies, or
200 // make assumptions about the system that may not be true in the
201 // future.
202 //
Jiyong Park82e2bf32017-08-16 14:05:54 +0900203 // If set to false, this module becomes inaccessible from /vendor modules.
204 //
205 // Default value is true when vndk: {enabled: true} or vendor: true.
206 //
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700207 // Nothing happens if BOARD_VNDK_VERSION isn't set in the BoardConfig.mk
208 Vendor_available *bool
Jiyong Park5fb8c102018-04-09 12:03:06 +0900209
210 // whether this module is capable of being loaded with other instance
211 // (possibly an older version) of the same module in the same process.
212 // Currently, a shared library that is a member of VNDK (vndk: {enabled: true})
213 // can be double loaded in a vendor process if the library is also a
214 // (direct and indirect) dependency of an LLNDK library. Such libraries must be
215 // explicitly marked as `double_loadable: true` by the owner, or the dependency
216 // from the LLNDK lib should be cut if the lib is not designed to be double loaded.
217 Double_loadable *bool
Colin Crossca860ac2016-01-04 14:34:37 -0800218}
219
Colin Crossca860ac2016-01-04 14:34:37 -0800220type ModuleContextIntf interface {
Colin Crossca860ac2016-01-04 14:34:37 -0800221 static() bool
222 staticBinary() bool
223 clang() bool
Colin Crossb98c8b02016-07-29 13:44:28 -0700224 toolchain() config.Toolchain
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700225 useSdk() bool
Colin Crossca860ac2016-01-04 14:34:37 -0800226 sdkVersion() string
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700227 useVndk() bool
Justin Yun8effde42017-06-23 19:24:43 +0900228 isVndk() bool
229 isVndkSp() bool
Logan Chienf3511742017-10-31 18:04:35 +0800230 isVndkExt() bool
Jiyong Parkf9332f12018-02-01 00:54:12 +0900231 inRecovery() bool
Logan Chien2f2b8902018-07-10 15:01:19 +0800232 shouldCreateVndkSourceAbiDump() bool
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700233 selectedStl() string
Colin Crossce75d2c2016-10-06 16:12:58 -0700234 baseModuleName() string
Logan Chienf3511742017-10-31 18:04:35 +0800235 getVndkExtendsModuleName() string
Yi Kong7e53c572018-02-14 18:16:12 +0800236 isPgoCompile() bool
Colin Crossca860ac2016-01-04 14:34:37 -0800237}
238
239type ModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700240 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800241 ModuleContextIntf
242}
243
244type BaseModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700245 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800246 ModuleContextIntf
247}
248
Colin Cross37047f12016-12-13 17:06:13 -0800249type DepsContext interface {
250 android.BottomUpMutatorContext
251 ModuleContextIntf
252}
253
Colin Crossca860ac2016-01-04 14:34:37 -0800254type feature interface {
255 begin(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800256 deps(ctx DepsContext, deps Deps) Deps
Colin Crossca860ac2016-01-04 14:34:37 -0800257 flags(ctx ModuleContext, flags Flags) Flags
258 props() []interface{}
259}
260
261type compiler interface {
Colin Cross42742b82016-08-01 13:20:05 -0700262 compilerInit(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800263 compilerDeps(ctx DepsContext, deps Deps) Deps
Colin Crossf18e1102017-11-16 14:33:08 -0800264 compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags
Colin Cross42742b82016-08-01 13:20:05 -0700265 compilerProps() []interface{}
266
Colin Cross76fada02016-07-27 10:31:13 -0700267 appendCflags([]string)
268 appendAsflags([]string)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700269 compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects
Colin Crossca860ac2016-01-04 14:34:37 -0800270}
271
272type linker interface {
Colin Cross42742b82016-08-01 13:20:05 -0700273 linkerInit(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800274 linkerDeps(ctx DepsContext, deps Deps) Deps
Colin Cross42742b82016-08-01 13:20:05 -0700275 linkerFlags(ctx ModuleContext, flags Flags) Flags
276 linkerProps() []interface{}
277
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700278 link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path
Colin Cross76fada02016-07-27 10:31:13 -0700279 appendLdflags([]string)
Colin Crossca860ac2016-01-04 14:34:37 -0800280}
281
282type installer interface {
Colin Cross42742b82016-08-01 13:20:05 -0700283 installerProps() []interface{}
Colin Cross635c3b02016-05-18 15:37:25 -0700284 install(ctx ModuleContext, path android.Path)
Colin Crossca860ac2016-01-04 14:34:37 -0800285 inData() bool
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700286 inSanitizerDir() bool
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700287 hostToolPath() android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800288}
289
Colin Crossc99deeb2016-04-11 15:06:20 -0700290type dependencyTag struct {
291 blueprint.BaseDependencyTag
292 name string
293 library bool
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700294
295 reexportFlags bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700296}
297
298var (
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700299 sharedDepTag = dependencyTag{name: "shared", library: true}
300 sharedExportDepTag = dependencyTag{name: "shared", library: true, reexportFlags: true}
301 lateSharedDepTag = dependencyTag{name: "late shared", library: true}
302 staticDepTag = dependencyTag{name: "static", library: true}
303 staticExportDepTag = dependencyTag{name: "static", library: true, reexportFlags: true}
304 lateStaticDepTag = dependencyTag{name: "late static", library: true}
305 wholeStaticDepTag = dependencyTag{name: "whole static", library: true, reexportFlags: true}
Colin Cross32ec36c2016-12-15 07:39:51 -0800306 headerDepTag = dependencyTag{name: "header", library: true}
307 headerExportDepTag = dependencyTag{name: "header", library: true, reexportFlags: true}
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700308 genSourceDepTag = dependencyTag{name: "gen source"}
309 genHeaderDepTag = dependencyTag{name: "gen header"}
310 genHeaderExportDepTag = dependencyTag{name: "gen header", reexportFlags: true}
311 objDepTag = dependencyTag{name: "obj"}
312 crtBeginDepTag = dependencyTag{name: "crtbegin"}
313 crtEndDepTag = dependencyTag{name: "crtend"}
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700314 linkerScriptDepTag = dependencyTag{name: "linker script"}
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700315 reuseObjTag = dependencyTag{name: "reuse objects"}
316 ndkStubDepTag = dependencyTag{name: "ndk stub", library: true}
317 ndkLateStubDepTag = dependencyTag{name: "ndk late stub", library: true}
Logan Chienf3511742017-10-31 18:04:35 +0800318 vndkExtDepTag = dependencyTag{name: "vndk extends", library: true}
Logan Chien43d34c32017-12-20 01:17:32 +0800319 runtimeDepTag = dependencyTag{name: "runtime lib"}
Colin Crossc99deeb2016-04-11 15:06:20 -0700320)
321
Colin Crossca860ac2016-01-04 14:34:37 -0800322// Module contains the properties and members used by all C/C++ module types, and implements
323// the blueprint.Module interface. It delegates to compiler, linker, and installer interfaces
324// to construct the output file. Behavior can be customized with a Customizer interface
325type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700326 android.ModuleBase
Colin Cross1f44a3a2017-07-07 14:33:33 -0700327 android.DefaultableModuleBase
Colin Crossc472d572015-03-17 15:06:21 -0700328
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700329 Properties BaseProperties
330 VendorProperties VendorProperties
Colin Crossfa138792015-04-24 17:31:52 -0700331
Colin Crossca860ac2016-01-04 14:34:37 -0800332 // initialize before calling Init
Colin Cross635c3b02016-05-18 15:37:25 -0700333 hod android.HostOrDeviceSupported
334 multilib android.Multilib
Colin Crossc472d572015-03-17 15:06:21 -0700335
Colin Crossca860ac2016-01-04 14:34:37 -0800336 // delegates, initialize before calling Init
Colin Crossb4ce0ec2016-09-13 13:41:39 -0700337 features []feature
338 compiler compiler
339 linker linker
340 installer installer
341 stl *stl
342 sanitize *sanitize
Dan Willemsen581341d2017-02-09 16:16:31 -0800343 coverage *coverage
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800344 sabi *sabi
Justin Yun8effde42017-06-23 19:24:43 +0900345 vndkdep *vndkdep
Stephen Craneba090d12017-05-09 15:44:35 -0700346 lto *lto
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700347 pgo *pgo
Colin Cross16b23492016-01-06 14:41:07 -0800348
349 androidMkSharedLibDeps []string
Colin Cross74d1ec02015-04-28 13:30:13 -0700350
Colin Cross635c3b02016-05-18 15:37:25 -0700351 outputFile android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800352
Colin Crossb98c8b02016-07-29 13:44:28 -0700353 cachedToolchain config.Toolchain
Colin Crossb916a382016-07-29 17:28:03 -0700354
355 subAndroidMkOnce map[subAndroidMkProvider]bool
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800356
357 // Flags used to compile this module
358 flags Flags
Jeff Gaston294356f2017-09-27 17:05:30 -0700359
360 // 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 -0800361 // line invocation. depsInLinkOrder stores the proper ordering of all of the transitive
Jeff Gaston294356f2017-09-27 17:05:30 -0700362 // deps of this module
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -0800363 depsInLinkOrder android.Paths
364
365 // only non-nil when this is a shared library that reuses the objects of a static library
366 staticVariant *Module
Colin Crossc472d572015-03-17 15:06:21 -0700367}
368
Jiyong Parkc20eee32018-09-05 22:36:17 +0900369func (c *Module) OutputFile() android.OptionalPath {
370 return c.outputFile
371}
372
Colin Cross36242852017-06-23 15:06:31 -0700373func (c *Module) Init() android.Module {
Dan Willemsenf923f2b2018-05-09 13:45:03 -0700374 c.AddProperties(&c.Properties, &c.VendorProperties)
Colin Crossca860ac2016-01-04 14:34:37 -0800375 if c.compiler != nil {
Colin Cross36242852017-06-23 15:06:31 -0700376 c.AddProperties(c.compiler.compilerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800377 }
378 if c.linker != nil {
Colin Cross36242852017-06-23 15:06:31 -0700379 c.AddProperties(c.linker.linkerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800380 }
381 if c.installer != nil {
Colin Cross36242852017-06-23 15:06:31 -0700382 c.AddProperties(c.installer.installerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800383 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700384 if c.stl != nil {
Colin Cross36242852017-06-23 15:06:31 -0700385 c.AddProperties(c.stl.props()...)
Colin Crossa8e07cc2016-04-04 15:07:06 -0700386 }
Colin Cross16b23492016-01-06 14:41:07 -0800387 if c.sanitize != nil {
Colin Cross36242852017-06-23 15:06:31 -0700388 c.AddProperties(c.sanitize.props()...)
Colin Cross16b23492016-01-06 14:41:07 -0800389 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800390 if c.coverage != nil {
Colin Cross36242852017-06-23 15:06:31 -0700391 c.AddProperties(c.coverage.props()...)
Dan Willemsen581341d2017-02-09 16:16:31 -0800392 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800393 if c.sabi != nil {
Colin Cross36242852017-06-23 15:06:31 -0700394 c.AddProperties(c.sabi.props()...)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800395 }
Justin Yun8effde42017-06-23 19:24:43 +0900396 if c.vndkdep != nil {
397 c.AddProperties(c.vndkdep.props()...)
398 }
Stephen Craneba090d12017-05-09 15:44:35 -0700399 if c.lto != nil {
400 c.AddProperties(c.lto.props()...)
401 }
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700402 if c.pgo != nil {
403 c.AddProperties(c.pgo.props()...)
404 }
Colin Crossca860ac2016-01-04 14:34:37 -0800405 for _, feature := range c.features {
Colin Cross36242852017-06-23 15:06:31 -0700406 c.AddProperties(feature.props()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800407 }
Colin Crossc472d572015-03-17 15:06:21 -0700408
Colin Crossa9d8bee2018-10-02 13:59:46 -0700409 c.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
410 switch class {
411 case android.Device:
412 return ctx.Config().DevicePrefer32BitExecutables()
413 case android.HostCross:
414 // Windows builds always prefer 32-bit
415 return true
416 default:
417 return false
418 }
419 })
Colin Cross36242852017-06-23 15:06:31 -0700420 android.InitAndroidArchModule(c, c.hod, c.multilib)
Colin Crossc472d572015-03-17 15:06:21 -0700421
Colin Cross1f44a3a2017-07-07 14:33:33 -0700422 android.InitDefaultableModule(c)
Colin Cross36242852017-06-23 15:06:31 -0700423
424 return c
Colin Crossc472d572015-03-17 15:06:21 -0700425}
426
Colin Crossb916a382016-07-29 17:28:03 -0700427// Returns true for dependency roots (binaries)
428// TODO(ccross): also handle dlopenable libraries
429func (c *Module) isDependencyRoot() bool {
430 if root, ok := c.linker.(interface {
431 isDependencyRoot() bool
432 }); ok {
433 return root.isDependencyRoot()
434 }
435 return false
436}
437
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700438func (c *Module) useVndk() bool {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700439 return c.Properties.UseVndk
440}
441
Justin Yun8effde42017-06-23 19:24:43 +0900442func (c *Module) isVndk() bool {
Logan Chienf3511742017-10-31 18:04:35 +0800443 if vndkdep := c.vndkdep; vndkdep != nil {
444 return vndkdep.isVndk()
Justin Yun8effde42017-06-23 19:24:43 +0900445 }
446 return false
447}
448
Yi Kong7e53c572018-02-14 18:16:12 +0800449func (c *Module) isPgoCompile() bool {
450 if pgo := c.pgo; pgo != nil {
451 return pgo.Properties.PgoCompile
452 }
453 return false
454}
455
Logan Chienf3511742017-10-31 18:04:35 +0800456func (c *Module) isVndkSp() bool {
457 if vndkdep := c.vndkdep; vndkdep != nil {
458 return vndkdep.isVndkSp()
459 }
460 return false
461}
462
463func (c *Module) isVndkExt() bool {
464 if vndkdep := c.vndkdep; vndkdep != nil {
465 return vndkdep.isVndkExt()
466 }
467 return false
468}
469
470func (c *Module) getVndkExtendsModuleName() string {
471 if vndkdep := c.vndkdep; vndkdep != nil {
472 return vndkdep.getVndkExtendsModuleName()
473 }
474 return ""
475}
476
Jiyong Park82e2bf32017-08-16 14:05:54 +0900477// Returns true only when this module is configured to have core and vendor
478// variants.
479func (c *Module) hasVendorVariant() bool {
480 return c.isVndk() || Bool(c.VendorProperties.Vendor_available)
481}
482
Jiyong Parkf9332f12018-02-01 00:54:12 +0900483func (c *Module) inRecovery() bool {
484 return c.Properties.InRecovery || c.ModuleBase.InstallInRecovery()
485}
486
487func (c *Module) onlyInRecovery() bool {
488 return c.ModuleBase.InstallInRecovery()
489}
490
Colin Crossca860ac2016-01-04 14:34:37 -0800491type baseModuleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700492 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800493 moduleContextImpl
494}
495
Colin Cross37047f12016-12-13 17:06:13 -0800496type depsContext struct {
497 android.BottomUpMutatorContext
498 moduleContextImpl
499}
500
Colin Crossca860ac2016-01-04 14:34:37 -0800501type moduleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700502 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800503 moduleContextImpl
504}
505
Jiyong Park2db76922017-11-08 16:03:48 +0900506func (ctx *moduleContext) SocSpecific() bool {
507 return ctx.ModuleContext.SocSpecific() ||
508 (ctx.mod.hasVendorVariant() && ctx.mod.useVndk() && !ctx.mod.isVndk())
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700509}
510
Colin Crossca860ac2016-01-04 14:34:37 -0800511type moduleContextImpl struct {
512 mod *Module
513 ctx BaseModuleContext
514}
515
Colin Crossca860ac2016-01-04 14:34:37 -0800516func (ctx *moduleContextImpl) clang() bool {
517 return ctx.mod.clang(ctx.ctx)
518}
519
Colin Crossb98c8b02016-07-29 13:44:28 -0700520func (ctx *moduleContextImpl) toolchain() config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800521 return ctx.mod.toolchain(ctx.ctx)
522}
523
524func (ctx *moduleContextImpl) static() bool {
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000525 return ctx.mod.static()
Colin Crossca860ac2016-01-04 14:34:37 -0800526}
527
528func (ctx *moduleContextImpl) staticBinary() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700529 if static, ok := ctx.mod.linker.(interface {
530 staticBinary() bool
531 }); ok {
532 return static.staticBinary()
Colin Crossca860ac2016-01-04 14:34:37 -0800533 }
Colin Crossb916a382016-07-29 17:28:03 -0700534 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800535}
536
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700537func (ctx *moduleContextImpl) useSdk() bool {
Jiyong Parkf9332f12018-02-01 00:54:12 +0900538 if ctx.ctx.Device() && !ctx.useVndk() && !ctx.inRecovery() {
Nan Zhang0007d812017-11-07 10:57:05 -0800539 return String(ctx.mod.Properties.Sdk_version) != ""
Dan Willemsena96ff642016-06-07 12:34:45 -0700540 }
541 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800542}
543
544func (ctx *moduleContextImpl) sdkVersion() string {
Dan Willemsena96ff642016-06-07 12:34:45 -0700545 if ctx.ctx.Device() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700546 if ctx.useVndk() {
Justin Yun732aa6a2018-03-23 17:43:47 +0900547 vndk_ver := ctx.ctx.DeviceConfig().VndkVersion()
Ryan Prichard05206112018-03-26 21:25:27 -0700548 if vndk_ver == "current" {
Justin Yun732aa6a2018-03-23 17:43:47 +0900549 platform_vndk_ver := ctx.ctx.DeviceConfig().PlatformVndkVersion()
550 if inList(platform_vndk_ver, ctx.ctx.Config().PlatformVersionCombinedCodenames()) {
551 return "current"
552 }
553 return platform_vndk_ver
554 }
555 return vndk_ver
Dan Willemsend2ede872016-11-18 14:54:24 -0800556 }
Justin Yun732aa6a2018-03-23 17:43:47 +0900557 return String(ctx.mod.Properties.Sdk_version)
Dan Willemsena96ff642016-06-07 12:34:45 -0700558 }
559 return ""
Colin Crossca860ac2016-01-04 14:34:37 -0800560}
561
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700562func (ctx *moduleContextImpl) useVndk() bool {
563 return ctx.mod.useVndk()
564}
Justin Yun8effde42017-06-23 19:24:43 +0900565
Logan Chienf3511742017-10-31 18:04:35 +0800566func (ctx *moduleContextImpl) isVndk() bool {
567 return ctx.mod.isVndk()
568}
569
Yi Kong7e53c572018-02-14 18:16:12 +0800570func (ctx *moduleContextImpl) isPgoCompile() bool {
571 return ctx.mod.isPgoCompile()
572}
573
Justin Yun8effde42017-06-23 19:24:43 +0900574func (ctx *moduleContextImpl) isVndkSp() bool {
Logan Chienf3511742017-10-31 18:04:35 +0800575 return ctx.mod.isVndkSp()
576}
577
578func (ctx *moduleContextImpl) isVndkExt() bool {
579 return ctx.mod.isVndkExt()
Justin Yun8effde42017-06-23 19:24:43 +0900580}
581
Jiyong Parkf9332f12018-02-01 00:54:12 +0900582func (ctx *moduleContextImpl) inRecovery() bool {
583 return ctx.mod.inRecovery()
584}
585
Logan Chien2f2b8902018-07-10 15:01:19 +0800586// Check whether ABI dumps should be created for this module.
587func (ctx *moduleContextImpl) shouldCreateVndkSourceAbiDump() bool {
588 if ctx.ctx.Config().IsEnvTrue("SKIP_ABI_CHECKS") {
589 return false
Jayant Chowdharyea0a2e12018-03-01 19:12:16 -0800590 }
Logan Chien2f2b8902018-07-10 15:01:19 +0800591 if sanitize := ctx.mod.sanitize; sanitize != nil {
592 if !sanitize.isVariantOnProductionDevice() {
593 return false
594 }
595 }
596 if !ctx.ctx.Device() {
597 // Host modules do not need ABI dumps.
598 return false
599 }
600 if inList(ctx.baseModuleName(), llndkLibraries) {
601 return true
602 }
Logan Chienf4b79c62018-08-02 02:27:02 +0800603 if inList(ctx.baseModuleName(), ndkMigratedLibs) {
604 return true
605 }
Logan Chien2f2b8902018-07-10 15:01:19 +0800606 if ctx.useVndk() && ctx.isVndk() {
607 // Return true if this is VNDK-core, VNDK-SP, or VNDK-Ext and this is not
608 // VNDK-private.
609 return Bool(ctx.mod.VendorProperties.Vendor_available) || ctx.isVndkExt()
610 }
611 return false
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800612}
613
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700614func (ctx *moduleContextImpl) selectedStl() string {
615 if stl := ctx.mod.stl; stl != nil {
616 return stl.Properties.SelectedStl
617 }
618 return ""
619}
620
Colin Crossce75d2c2016-10-06 16:12:58 -0700621func (ctx *moduleContextImpl) baseModuleName() string {
622 return ctx.mod.ModuleBase.BaseModuleName()
623}
624
Logan Chienf3511742017-10-31 18:04:35 +0800625func (ctx *moduleContextImpl) getVndkExtendsModuleName() string {
626 return ctx.mod.getVndkExtendsModuleName()
627}
628
Colin Cross635c3b02016-05-18 15:37:25 -0700629func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800630 return &Module{
631 hod: hod,
632 multilib: multilib,
633 }
634}
635
Colin Cross635c3b02016-05-18 15:37:25 -0700636func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800637 module := newBaseModule(hod, multilib)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700638 module.features = []feature{
639 &tidyFeature{},
640 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700641 module.stl = &stl{}
Colin Cross16b23492016-01-06 14:41:07 -0800642 module.sanitize = &sanitize{}
Dan Willemsen581341d2017-02-09 16:16:31 -0800643 module.coverage = &coverage{}
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800644 module.sabi = &sabi{}
Justin Yun8effde42017-06-23 19:24:43 +0900645 module.vndkdep = &vndkdep{}
Stephen Craneba090d12017-05-09 15:44:35 -0700646 module.lto = &lto{}
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700647 module.pgo = &pgo{}
Colin Crossca860ac2016-01-04 14:34:37 -0800648 return module
649}
650
Colin Crossce75d2c2016-10-06 16:12:58 -0700651func (c *Module) Prebuilt() *android.Prebuilt {
652 if p, ok := c.linker.(prebuiltLinkerInterface); ok {
653 return p.prebuilt()
654 }
655 return nil
656}
657
658func (c *Module) Name() string {
659 name := c.ModuleBase.Name()
Dan Willemsen01a90592017-04-07 15:21:13 -0700660 if p, ok := c.linker.(interface {
661 Name(string) string
662 }); ok {
Colin Crossce75d2c2016-10-06 16:12:58 -0700663 name = p.Name(name)
664 }
665 return name
666}
667
Jeff Gaston294356f2017-09-27 17:05:30 -0700668// orderDeps reorders dependencies into a list such that if module A depends on B, then
669// A will precede B in the resultant list.
670// This is convenient for passing into a linker.
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -0800671// Note that directSharedDeps should be the analogous static library for each shared lib dep
672func 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 -0700673 // If A depends on B, then
674 // Every list containing A will also contain B later in the list
675 // So, after concatenating all lists, the final instance of B will have come from the same
676 // original list as the final instance of A
677 // So, the final instance of B will be later in the concatenation than the final A
678 // So, keeping only the final instance of A and of B ensures that A is earlier in the output
679 // list than B
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -0800680 for _, dep := range directStaticDeps {
Jeff Gaston294356f2017-09-27 17:05:30 -0700681 orderedAllDeps = append(orderedAllDeps, dep)
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -0800682 orderedAllDeps = append(orderedAllDeps, allTransitiveDeps[dep]...)
683 }
684 for _, dep := range directSharedDeps {
685 orderedAllDeps = append(orderedAllDeps, dep)
686 orderedAllDeps = append(orderedAllDeps, allTransitiveDeps[dep]...)
Jeff Gaston294356f2017-09-27 17:05:30 -0700687 }
688
Colin Crossb6715442017-10-24 11:13:31 -0700689 orderedAllDeps = android.LastUniquePaths(orderedAllDeps)
Jeff Gaston294356f2017-09-27 17:05:30 -0700690
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -0800691 // We don't want to add any new dependencies into directStaticDeps (to allow the caller to
Jeff Gaston294356f2017-09-27 17:05:30 -0700692 // intentionally exclude or replace any unwanted transitive dependencies), so we limit the
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -0800693 // resultant list to only what the caller has chosen to include in directStaticDeps
694 _, orderedDeclaredDeps = android.FilterPathList(orderedAllDeps, directStaticDeps)
Jeff Gaston294356f2017-09-27 17:05:30 -0700695
696 return orderedAllDeps, orderedDeclaredDeps
697}
698
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -0800699func orderStaticModuleDeps(module *Module, staticDeps []*Module, sharedDeps []*Module) (results []android.Path) {
700 // convert Module to Path
701 allTransitiveDeps := make(map[android.Path][]android.Path, len(staticDeps))
702 staticDepFiles := []android.Path{}
703 for _, dep := range staticDeps {
704 allTransitiveDeps[dep.outputFile.Path()] = dep.depsInLinkOrder
705 staticDepFiles = append(staticDepFiles, dep.outputFile.Path())
Jeff Gaston294356f2017-09-27 17:05:30 -0700706 }
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -0800707 sharedDepFiles := []android.Path{}
708 for _, sharedDep := range sharedDeps {
709 staticAnalogue := sharedDep.staticVariant
710 if staticAnalogue != nil {
711 allTransitiveDeps[staticAnalogue.outputFile.Path()] = staticAnalogue.depsInLinkOrder
712 sharedDepFiles = append(sharedDepFiles, staticAnalogue.outputFile.Path())
713 }
Jeff Gaston294356f2017-09-27 17:05:30 -0700714 }
715
716 // reorder the dependencies based on transitive dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -0800717 module.depsInLinkOrder, results = orderDeps(staticDepFiles, sharedDepFiles, allTransitiveDeps)
Jeff Gaston294356f2017-09-27 17:05:30 -0700718
719 return results
Jeff Gaston294356f2017-09-27 17:05:30 -0700720}
721
Colin Cross635c3b02016-05-18 15:37:25 -0700722func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800723 ctx := &moduleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700724 ModuleContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800725 moduleContextImpl: moduleContextImpl{
726 mod: c,
727 },
728 }
729 ctx.ctx = ctx
730
Colin Crossf18e1102017-11-16 14:33:08 -0800731 deps := c.depsToPaths(ctx)
732 if ctx.Failed() {
733 return
734 }
735
Colin Crossca860ac2016-01-04 14:34:37 -0800736 flags := Flags{
737 Toolchain: c.toolchain(ctx),
738 Clang: c.clang(ctx),
739 }
Colin Crossca860ac2016-01-04 14:34:37 -0800740 if c.compiler != nil {
Colin Crossf18e1102017-11-16 14:33:08 -0800741 flags = c.compiler.compilerFlags(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -0800742 }
743 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700744 flags = c.linker.linkerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800745 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700746 if c.stl != nil {
747 flags = c.stl.flags(ctx, flags)
748 }
Colin Cross16b23492016-01-06 14:41:07 -0800749 if c.sanitize != nil {
750 flags = c.sanitize.flags(ctx, flags)
751 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800752 if c.coverage != nil {
753 flags = c.coverage.flags(ctx, flags)
754 }
Stephen Craneba090d12017-05-09 15:44:35 -0700755 if c.lto != nil {
756 flags = c.lto.flags(ctx, flags)
757 }
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700758 if c.pgo != nil {
759 flags = c.pgo.flags(ctx, flags)
760 }
Colin Crossca860ac2016-01-04 14:34:37 -0800761 for _, feature := range c.features {
762 flags = feature.flags(ctx, flags)
763 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800764 if ctx.Failed() {
765 return
766 }
767
Colin Crossb98c8b02016-07-29 13:44:28 -0700768 flags.CFlags, _ = filterList(flags.CFlags, config.IllegalFlags)
769 flags.CppFlags, _ = filterList(flags.CppFlags, config.IllegalFlags)
770 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, config.IllegalFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800771
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800772 flags.GlobalFlags = append(flags.GlobalFlags, deps.Flags...)
773 c.flags = flags
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700774 // We need access to all the flags seen by a source file.
775 if c.sabi != nil {
776 flags = c.sabi.flags(ctx, flags)
777 }
Colin Crossca860ac2016-01-04 14:34:37 -0800778 // Optimization to reduce size of build.ninja
779 // Replace the long list of flags for each file with a module-local variable
780 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
781 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
782 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
783 flags.CFlags = []string{"$cflags"}
784 flags.CppFlags = []string{"$cppflags"}
785 flags.AsFlags = []string{"$asflags"}
786
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700787 var objs Objects
Colin Crossca860ac2016-01-04 14:34:37 -0800788 if c.compiler != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700789 objs = c.compiler.compile(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -0800790 if ctx.Failed() {
791 return
792 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800793 }
794
Colin Crossca860ac2016-01-04 14:34:37 -0800795 if c.linker != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700796 outputFile := c.linker.link(ctx, flags, deps, objs)
Colin Crossca860ac2016-01-04 14:34:37 -0800797 if ctx.Failed() {
798 return
799 }
Colin Cross635c3b02016-05-18 15:37:25 -0700800 c.outputFile = android.OptionalPathForPath(outputFile)
Colin Crossce75d2c2016-10-06 16:12:58 -0700801 }
Colin Cross5049f022015-03-18 13:28:46 -0700802
Colin Crossce75d2c2016-10-06 16:12:58 -0700803 if c.installer != nil && !c.Properties.PreventInstall && c.outputFile.Valid() {
804 c.installer.install(ctx, c.outputFile.Path())
805 if ctx.Failed() {
806 return
Colin Crossca860ac2016-01-04 14:34:37 -0800807 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700808 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800809}
810
Colin Crossb98c8b02016-07-29 13:44:28 -0700811func (c *Module) toolchain(ctx BaseModuleContext) config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800812 if c.cachedToolchain == nil {
Colin Crossb98c8b02016-07-29 13:44:28 -0700813 c.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
Colin Cross3f40fa42015-01-30 17:27:36 -0800814 }
Colin Crossca860ac2016-01-04 14:34:37 -0800815 return c.cachedToolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800816}
817
Colin Crossca860ac2016-01-04 14:34:37 -0800818func (c *Module) begin(ctx BaseModuleContext) {
819 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700820 c.compiler.compilerInit(ctx)
Colin Cross21b9a242015-03-24 14:15:58 -0700821 }
Colin Crossca860ac2016-01-04 14:34:37 -0800822 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700823 c.linker.linkerInit(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800824 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700825 if c.stl != nil {
826 c.stl.begin(ctx)
827 }
Colin Cross16b23492016-01-06 14:41:07 -0800828 if c.sanitize != nil {
829 c.sanitize.begin(ctx)
830 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800831 if c.coverage != nil {
832 c.coverage.begin(ctx)
833 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800834 if c.sabi != nil {
835 c.sabi.begin(ctx)
836 }
Justin Yun8effde42017-06-23 19:24:43 +0900837 if c.vndkdep != nil {
838 c.vndkdep.begin(ctx)
839 }
Stephen Craneba090d12017-05-09 15:44:35 -0700840 if c.lto != nil {
841 c.lto.begin(ctx)
842 }
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700843 if c.pgo != nil {
844 c.pgo.begin(ctx)
845 }
Colin Crossca860ac2016-01-04 14:34:37 -0800846 for _, feature := range c.features {
847 feature.begin(ctx)
848 }
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700849 if ctx.useSdk() {
Dan Albertf5415d72017-08-17 16:19:59 -0700850 version, err := normalizeNdkApiLevel(ctx, ctx.sdkVersion(), ctx.Arch())
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700851 if err != nil {
852 ctx.PropertyErrorf("sdk_version", err.Error())
853 }
Nan Zhang0007d812017-11-07 10:57:05 -0800854 c.Properties.Sdk_version = StringPtr(version)
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700855 }
Colin Crossca860ac2016-01-04 14:34:37 -0800856}
857
Colin Cross37047f12016-12-13 17:06:13 -0800858func (c *Module) deps(ctx DepsContext) Deps {
Colin Crossc99deeb2016-04-11 15:06:20 -0700859 deps := Deps{}
860
861 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700862 deps = c.compiler.compilerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700863 }
Pirama Arumuga Nainar0b882f02018-04-23 22:44:39 +0000864 // Add the PGO dependency (the clang_rt.profile runtime library), which
865 // sometimes depends on symbols from libgcc, before libgcc gets added
866 // in linkerDeps().
Pirama Arumuga Nainar49b53d52017-10-04 16:47:29 -0700867 if c.pgo != nil {
868 deps = c.pgo.deps(ctx, deps)
869 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700870 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700871 deps = c.linker.linkerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700872 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700873 if c.stl != nil {
874 deps = c.stl.deps(ctx, deps)
875 }
Colin Cross16b23492016-01-06 14:41:07 -0800876 if c.sanitize != nil {
877 deps = c.sanitize.deps(ctx, deps)
878 }
Pirama Arumuga Nainar0b882f02018-04-23 22:44:39 +0000879 if c.coverage != nil {
880 deps = c.coverage.deps(ctx, deps)
881 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800882 if c.sabi != nil {
883 deps = c.sabi.deps(ctx, deps)
884 }
Justin Yun8effde42017-06-23 19:24:43 +0900885 if c.vndkdep != nil {
886 deps = c.vndkdep.deps(ctx, deps)
887 }
Stephen Craneba090d12017-05-09 15:44:35 -0700888 if c.lto != nil {
889 deps = c.lto.deps(ctx, deps)
890 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700891 for _, feature := range c.features {
892 deps = feature.deps(ctx, deps)
893 }
894
Colin Crossb6715442017-10-24 11:13:31 -0700895 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
896 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
897 deps.LateStaticLibs = android.LastUniqueStrings(deps.LateStaticLibs)
898 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
899 deps.LateSharedLibs = android.LastUniqueStrings(deps.LateSharedLibs)
900 deps.HeaderLibs = android.LastUniqueStrings(deps.HeaderLibs)
Logan Chien43d34c32017-12-20 01:17:32 +0800901 deps.RuntimeLibs = android.LastUniqueStrings(deps.RuntimeLibs)
Colin Crossc99deeb2016-04-11 15:06:20 -0700902
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700903 for _, lib := range deps.ReexportSharedLibHeaders {
904 if !inList(lib, deps.SharedLibs) {
905 ctx.PropertyErrorf("export_shared_lib_headers", "Shared library not in shared_libs: '%s'", lib)
906 }
907 }
908
909 for _, lib := range deps.ReexportStaticLibHeaders {
910 if !inList(lib, deps.StaticLibs) {
911 ctx.PropertyErrorf("export_static_lib_headers", "Static library not in static_libs: '%s'", lib)
912 }
913 }
914
Colin Cross5950f382016-12-13 12:50:57 -0800915 for _, lib := range deps.ReexportHeaderLibHeaders {
916 if !inList(lib, deps.HeaderLibs) {
917 ctx.PropertyErrorf("export_header_lib_headers", "Header library not in header_libs: '%s'", lib)
918 }
919 }
920
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700921 for _, gen := range deps.ReexportGeneratedHeaders {
922 if !inList(gen, deps.GeneratedHeaders) {
923 ctx.PropertyErrorf("export_generated_headers", "Generated header module not in generated_headers: '%s'", gen)
924 }
925 }
926
Colin Crossc99deeb2016-04-11 15:06:20 -0700927 return deps
928}
929
Dan Albert7e9d2952016-08-04 13:02:36 -0700930func (c *Module) beginMutator(actx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800931 ctx := &baseModuleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700932 BaseContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800933 moduleContextImpl: moduleContextImpl{
934 mod: c,
935 },
936 }
937 ctx.ctx = ctx
938
Colin Crossca860ac2016-01-04 14:34:37 -0800939 c.begin(ctx)
Dan Albert7e9d2952016-08-04 13:02:36 -0700940}
941
Colin Cross1e676be2016-10-12 14:38:15 -0700942func (c *Module) DepsMutator(actx android.BottomUpMutatorContext) {
Colin Cross37047f12016-12-13 17:06:13 -0800943 ctx := &depsContext{
944 BottomUpMutatorContext: actx,
Dan Albert7e9d2952016-08-04 13:02:36 -0700945 moduleContextImpl: moduleContextImpl{
946 mod: c,
947 },
948 }
949 ctx.ctx = ctx
Colin Crossca860ac2016-01-04 14:34:37 -0800950
Colin Crossc99deeb2016-04-11 15:06:20 -0700951 deps := c.deps(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800952
Dan Albert914449f2016-06-17 16:45:24 -0700953 variantNdkLibs := []string{}
954 variantLateNdkLibs := []string{}
Dan Willemsenb916b802017-03-19 13:44:32 -0700955 if ctx.Os() == android.Android {
Dan Albert914449f2016-06-17 16:45:24 -0700956 version := ctx.sdkVersion()
Dan Willemsen72d39932016-07-08 23:23:48 -0700957
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700958 // rewriteNdkLibs takes a list of names of shared libraries and scans it for three types
959 // of names:
Dan Albert914449f2016-06-17 16:45:24 -0700960 //
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700961 // 1. Name of an NDK library that refers to a prebuilt module.
962 // For each of these, it adds the name of the prebuilt module (which will be in
963 // prebuilts/ndk) to the list of nonvariant libs.
964 // 2. Name of an NDK library that refers to an ndk_library module.
965 // For each of these, it adds the name of the ndk_library module to the list of
966 // variant libs.
967 // 3. Anything else (so anything that isn't an NDK library).
968 // It adds these to the nonvariantLibs list.
Dan Albert914449f2016-06-17 16:45:24 -0700969 //
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700970 // The caller can then know to add the variantLibs dependencies differently from the
971 // nonvariantLibs
972 rewriteNdkLibs := func(list []string) (nonvariantLibs []string, variantLibs []string) {
973 variantLibs = []string{}
974 nonvariantLibs = []string{}
Dan Albert914449f2016-06-17 16:45:24 -0700975 for _, entry := range list {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700976 if ctx.useSdk() && inList(entry, ndkPrebuiltSharedLibraries) {
Dan Albert914449f2016-06-17 16:45:24 -0700977 if !inList(entry, ndkMigratedLibs) {
978 nonvariantLibs = append(nonvariantLibs, entry+".ndk."+version)
979 } else {
980 variantLibs = append(variantLibs, entry+ndkLibrarySuffix)
981 }
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700982 } else if ctx.useVndk() && inList(entry, llndkLibraries) {
Dan Willemsenb916b802017-03-19 13:44:32 -0700983 nonvariantLibs = append(nonvariantLibs, entry+llndkLibrarySuffix)
Jiyong Park374510b2018-03-19 18:23:01 +0900984 } else if (ctx.Platform() || ctx.ProductSpecific()) && inList(entry, vendorPublicLibraries) {
985 vendorPublicLib := entry + vendorPublicLibrarySuffix
986 if actx.OtherModuleExists(vendorPublicLib) {
987 nonvariantLibs = append(nonvariantLibs, vendorPublicLib)
988 } else {
989 // This can happen if vendor_public_library module is defined in a
990 // namespace that isn't visible to the current module. In that case,
991 // link to the original library.
992 nonvariantLibs = append(nonvariantLibs, entry)
993 }
Dan Albert914449f2016-06-17 16:45:24 -0700994 } else {
Dan Willemsen7cbf5f82017-03-28 00:08:30 -0700995 nonvariantLibs = append(nonvariantLibs, entry)
Dan Willemsen72d39932016-07-08 23:23:48 -0700996 }
997 }
Dan Albert914449f2016-06-17 16:45:24 -0700998 return nonvariantLibs, variantLibs
Dan Willemsen72d39932016-07-08 23:23:48 -0700999 }
1000
Dan Albert914449f2016-06-17 16:45:24 -07001001 deps.SharedLibs, variantNdkLibs = rewriteNdkLibs(deps.SharedLibs)
1002 deps.LateSharedLibs, variantLateNdkLibs = rewriteNdkLibs(deps.LateSharedLibs)
Jiyong Park4c35af02017-07-05 13:41:55 +09001003 deps.ReexportSharedLibHeaders, _ = rewriteNdkLibs(deps.ReexportSharedLibHeaders)
Dan Willemsen72d39932016-07-08 23:23:48 -07001004 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001005
Colin Cross32ec36c2016-12-15 07:39:51 -08001006 for _, lib := range deps.HeaderLibs {
1007 depTag := headerDepTag
1008 if inList(lib, deps.ReexportHeaderLibHeaders) {
1009 depTag = headerExportDepTag
1010 }
1011 actx.AddVariationDependencies(nil, depTag, lib)
1012 }
Colin Cross5950f382016-12-13 12:50:57 -08001013
Dan Willemsen59339a22018-07-22 21:18:45 -07001014 actx.AddVariationDependencies([]blueprint.Variation{
1015 {Mutator: "link", Variation: "static"},
1016 }, wholeStaticDepTag, deps.WholeStaticLibs...)
Colin Crossc99deeb2016-04-11 15:06:20 -07001017
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001018 for _, lib := range deps.StaticLibs {
1019 depTag := staticDepTag
1020 if inList(lib, deps.ReexportStaticLibHeaders) {
1021 depTag = staticExportDepTag
1022 }
Dan Willemsen59339a22018-07-22 21:18:45 -07001023 actx.AddVariationDependencies([]blueprint.Variation{
1024 {Mutator: "link", Variation: "static"},
1025 }, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001026 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001027
Dan Willemsen59339a22018-07-22 21:18:45 -07001028 actx.AddVariationDependencies([]blueprint.Variation{
1029 {Mutator: "link", Variation: "static"},
1030 }, lateStaticDepTag, deps.LateStaticLibs...)
Colin Crossc99deeb2016-04-11 15:06:20 -07001031
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001032 for _, lib := range deps.SharedLibs {
1033 depTag := sharedDepTag
1034 if inList(lib, deps.ReexportSharedLibHeaders) {
1035 depTag = sharedExportDepTag
1036 }
Dan Willemsen59339a22018-07-22 21:18:45 -07001037 actx.AddVariationDependencies([]blueprint.Variation{
1038 {Mutator: "link", Variation: "shared"},
1039 }, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001040 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001041
Dan Willemsen59339a22018-07-22 21:18:45 -07001042 actx.AddVariationDependencies([]blueprint.Variation{
1043 {Mutator: "link", Variation: "shared"},
1044 }, lateSharedDepTag, deps.LateSharedLibs...)
Colin Crossc99deeb2016-04-11 15:06:20 -07001045
Dan Willemsen59339a22018-07-22 21:18:45 -07001046 actx.AddVariationDependencies([]blueprint.Variation{
1047 {Mutator: "link", Variation: "shared"},
1048 }, runtimeDepTag, deps.RuntimeLibs...)
Logan Chien43d34c32017-12-20 01:17:32 +08001049
Colin Cross68861832016-07-08 10:41:41 -07001050 actx.AddDependency(c, genSourceDepTag, deps.GeneratedSources...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -07001051
1052 for _, gen := range deps.GeneratedHeaders {
1053 depTag := genHeaderDepTag
1054 if inList(gen, deps.ReexportGeneratedHeaders) {
1055 depTag = genHeaderExportDepTag
1056 }
1057 actx.AddDependency(c, depTag, gen)
1058 }
Dan Willemsenb40aab62016-04-20 14:21:14 -07001059
Colin Cross42d48b72018-08-29 14:10:52 -07001060 actx.AddVariationDependencies(nil, objDepTag, deps.ObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -07001061
1062 if deps.CrtBegin != "" {
Colin Cross42d48b72018-08-29 14:10:52 -07001063 actx.AddVariationDependencies(nil, crtBeginDepTag, deps.CrtBegin)
Colin Crossca860ac2016-01-04 14:34:37 -08001064 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001065 if deps.CrtEnd != "" {
Colin Cross42d48b72018-08-29 14:10:52 -07001066 actx.AddVariationDependencies(nil, crtEndDepTag, deps.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -07001067 }
Dan Willemsenc77a0b32017-09-18 23:19:12 -07001068 if deps.LinkerScript != "" {
1069 actx.AddDependency(c, linkerScriptDepTag, deps.LinkerScript)
1070 }
Dan Albert914449f2016-06-17 16:45:24 -07001071
1072 version := ctx.sdkVersion()
1073 actx.AddVariationDependencies([]blueprint.Variation{
Dan Willemsen59339a22018-07-22 21:18:45 -07001074 {Mutator: "ndk_api", Variation: version},
1075 {Mutator: "link", Variation: "shared"},
1076 }, ndkStubDepTag, variantNdkLibs...)
Dan Albert914449f2016-06-17 16:45:24 -07001077 actx.AddVariationDependencies([]blueprint.Variation{
Dan Willemsen59339a22018-07-22 21:18:45 -07001078 {Mutator: "ndk_api", Variation: version},
1079 {Mutator: "link", Variation: "shared"},
1080 }, ndkLateStubDepTag, variantLateNdkLibs...)
Logan Chienf3511742017-10-31 18:04:35 +08001081
1082 if vndkdep := c.vndkdep; vndkdep != nil {
1083 if vndkdep.isVndkExt() {
1084 baseModuleMode := vendorMode
1085 if actx.DeviceConfig().VndkVersion() == "" {
1086 baseModuleMode = coreMode
1087 }
1088 actx.AddVariationDependencies([]blueprint.Variation{
Dan Willemsen59339a22018-07-22 21:18:45 -07001089 {Mutator: "image", Variation: baseModuleMode},
1090 {Mutator: "link", Variation: "shared"},
1091 }, vndkExtDepTag, vndkdep.getVndkExtendsModuleName())
Logan Chienf3511742017-10-31 18:04:35 +08001092 }
1093 }
Colin Cross6362e272015-10-29 15:25:03 -07001094}
Colin Cross21b9a242015-03-24 14:15:58 -07001095
Dan Albert7e9d2952016-08-04 13:02:36 -07001096func beginMutator(ctx android.BottomUpMutatorContext) {
1097 if c, ok := ctx.Module().(*Module); ok && c.Enabled() {
1098 c.beginMutator(ctx)
1099 }
1100}
1101
Colin Crossca860ac2016-01-04 14:34:37 -08001102func (c *Module) clang(ctx BaseModuleContext) bool {
Dan Willemsen742a5452018-07-23 17:19:36 -07001103 if c.Properties.Clang != nil && *c.Properties.Clang == false {
1104 ctx.PropertyErrorf("clang", "false (GCC) is no longer supported")
Colin Cross3f40fa42015-01-30 17:27:36 -08001105 }
Colin Cross28344522015-04-22 13:07:53 -07001106
Dan Willemsen742a5452018-07-23 17:19:36 -07001107 return !c.Properties.Gcc
Colin Crossca860ac2016-01-04 14:34:37 -08001108}
1109
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001110// Whether a module can link to another module, taking into
1111// account NDK linking.
Logan Chienf3511742017-10-31 18:04:35 +08001112func checkLinkType(ctx android.ModuleContext, from *Module, to *Module, tag dependencyTag) {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001113 if from.Target().Os != android.Android {
1114 // Host code is not restricted
1115 return
1116 }
1117 if from.Properties.UseVndk {
1118 // Though vendor code is limited by the vendor mutator,
1119 // each vendor-available module needs to check
1120 // link-type for VNDK.
1121 if from.vndkdep != nil {
Logan Chienf3511742017-10-31 18:04:35 +08001122 from.vndkdep.vndkCheckLinkType(ctx, to, tag)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001123 }
1124 return
1125 }
Nan Zhang0007d812017-11-07 10:57:05 -08001126 if String(from.Properties.Sdk_version) == "" {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001127 // Platform code can link to anything
1128 return
1129 }
Jiyong Parkf9332f12018-02-01 00:54:12 +09001130 if from.inRecovery() {
1131 // Recovery code is not NDK
1132 return
1133 }
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001134 if _, ok := to.linker.(*toolchainLibraryDecorator); ok {
1135 // These are always allowed
1136 return
1137 }
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001138 if _, ok := to.linker.(*ndkPrebuiltStlLinker); ok {
1139 // These are allowed, but they don't set sdk_version
1140 return
1141 }
1142 if _, ok := to.linker.(*stubDecorator); ok {
1143 // These aren't real libraries, but are the stub shared libraries that are included in
1144 // the NDK.
1145 return
1146 }
Nan Zhang0007d812017-11-07 10:57:05 -08001147 if String(to.Properties.Sdk_version) == "" {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001148 // NDK code linking to platform code is never okay.
1149 ctx.ModuleErrorf("depends on non-NDK-built library %q",
1150 ctx.OtherModuleName(to))
1151 }
1152
1153 // At this point we know we have two NDK libraries, but we need to
1154 // check that we're not linking against anything built against a higher
1155 // API level, as it is only valid to link against older or equivalent
1156 // APIs.
1157
Inseob Kim01a28722018-04-11 09:48:45 +09001158 // Current can link against anything.
1159 if String(from.Properties.Sdk_version) != "current" {
1160 // Otherwise we need to check.
1161 if String(to.Properties.Sdk_version) == "current" {
1162 // Current can't be linked against by anything else.
1163 ctx.ModuleErrorf("links %q built against newer API version %q",
1164 ctx.OtherModuleName(to), "current")
1165 } else {
1166 fromApi, err := strconv.Atoi(String(from.Properties.Sdk_version))
1167 if err != nil {
1168 ctx.PropertyErrorf("sdk_version",
Inseob Kim34b22832018-04-11 10:13:16 +09001169 "Invalid sdk_version value (must be int or current): %q",
Inseob Kim01a28722018-04-11 09:48:45 +09001170 String(from.Properties.Sdk_version))
1171 }
1172 toApi, err := strconv.Atoi(String(to.Properties.Sdk_version))
1173 if err != nil {
1174 ctx.PropertyErrorf("sdk_version",
Inseob Kim34b22832018-04-11 10:13:16 +09001175 "Invalid sdk_version value (must be int or current): %q",
Inseob Kim01a28722018-04-11 09:48:45 +09001176 String(to.Properties.Sdk_version))
1177 }
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001178
Inseob Kim01a28722018-04-11 09:48:45 +09001179 if toApi > fromApi {
1180 ctx.ModuleErrorf("links %q built against newer API version %q",
1181 ctx.OtherModuleName(to), String(to.Properties.Sdk_version))
1182 }
1183 }
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001184 }
Dan Albert202fe492017-12-15 13:56:59 -08001185
1186 // Also check that the two STL choices are compatible.
1187 fromStl := from.stl.Properties.SelectedStl
1188 toStl := to.stl.Properties.SelectedStl
1189 if fromStl == "" || toStl == "" {
1190 // Libraries that don't use the STL are unrestricted.
Inseob Kimda2171a2018-04-11 15:41:38 +09001191 } else if fromStl == "ndk_system" || toStl == "ndk_system" {
Dan Albert202fe492017-12-15 13:56:59 -08001192 // We can be permissive with the system "STL" since it is only the C++
1193 // ABI layer, but in the future we should make sure that everyone is
1194 // using either libc++ or nothing.
Colin Crossb60190a2018-09-04 16:28:17 -07001195 } else if getNdkStlFamily(from) != getNdkStlFamily(to) {
Dan Albert202fe492017-12-15 13:56:59 -08001196 ctx.ModuleErrorf("uses %q and depends on %q which uses incompatible %q",
1197 from.stl.Properties.SelectedStl, ctx.OtherModuleName(to),
1198 to.stl.Properties.SelectedStl)
1199 }
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001200}
1201
Jiyong Park5fb8c102018-04-09 12:03:06 +09001202// Tests whether the dependent library is okay to be double loaded inside a single process.
1203// If a library is a member of VNDK and at the same time dependencies of an LLNDK library,
1204// it is subject to be double loaded. Such lib should be explicitly marked as double_loaded: true
1205// or as vndk-sp (vndk: { enabled: true, support_system_process: true}).
1206func checkDoubleLoadableLibries(ctx android.ModuleContext, from *Module, to *Module) {
1207 if _, ok := from.linker.(*libraryDecorator); !ok {
1208 return
1209 }
1210
1211 if inList(ctx.ModuleName(), llndkLibraries) ||
1212 (from.useVndk() && Bool(from.VendorProperties.Double_loadable)) {
1213 _, depIsLlndk := to.linker.(*llndkStubDecorator)
1214 depIsVndkSp := false
1215 if to.vndkdep != nil && to.vndkdep.isVndkSp() {
1216 depIsVndkSp = true
1217 }
1218 depIsVndk := false
1219 if to.vndkdep != nil && to.vndkdep.isVndk() {
1220 depIsVndk = true
1221 }
1222 depIsDoubleLoadable := Bool(to.VendorProperties.Double_loadable)
1223 if !depIsLlndk && !depIsVndkSp && !depIsDoubleLoadable && depIsVndk {
1224 ctx.ModuleErrorf("links VNDK library %q that isn't double_loadable.",
1225 ctx.OtherModuleName(to))
1226 }
1227 }
1228}
1229
Colin Crossc99deeb2016-04-11 15:06:20 -07001230// Convert dependencies to paths. Returns a PathDeps containing paths
Colin Cross635c3b02016-05-18 15:37:25 -07001231func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
Colin Crossca860ac2016-01-04 14:34:37 -08001232 var depPaths PathDeps
Colin Crossca860ac2016-01-04 14:34:37 -08001233
Jeff Gaston294356f2017-09-27 17:05:30 -07001234 directStaticDeps := []*Module{}
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001235 directSharedDeps := []*Module{}
Jeff Gaston294356f2017-09-27 17:05:30 -07001236
Colin Crossd11fcda2017-10-23 17:59:01 -07001237 ctx.VisitDirectDeps(func(dep android.Module) {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001238 depName := ctx.OtherModuleName(dep)
1239 depTag := ctx.OtherModuleDependencyTag(dep)
Dan Albert9e10cd42016-08-03 14:12:14 -07001240
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001241 ccDep, _ := dep.(*Module)
1242 if ccDep == nil {
1243 // handling for a few module types that aren't cc Module but that are also supported
1244 switch depTag {
Dan Willemsenb40aab62016-04-20 14:21:14 -07001245 case genSourceDepTag:
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001246 if genRule, ok := dep.(genrule.SourceFileGenerator); ok {
Dan Willemsenb40aab62016-04-20 14:21:14 -07001247 depPaths.GeneratedSources = append(depPaths.GeneratedSources,
1248 genRule.GeneratedSourceFiles()...)
1249 } else {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001250 ctx.ModuleErrorf("module %q is not a gensrcs or genrule", depName)
Dan Willemsenb40aab62016-04-20 14:21:14 -07001251 }
Colin Crosse90bfd12017-04-26 16:59:26 -07001252 // Support exported headers from a generated_sources dependency
1253 fallthrough
Dan Willemsenb3454ab2016-09-28 17:34:58 -07001254 case genHeaderDepTag, genHeaderExportDepTag:
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001255 if genRule, ok := dep.(genrule.SourceFileGenerator); ok {
Dan Willemsenb40aab62016-04-20 14:21:14 -07001256 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders,
Dan Willemsen9da9d492018-02-21 18:28:18 -08001257 genRule.GeneratedDeps()...)
Colin Cross5ed99c62016-11-22 12:55:55 -08001258 flags := includeDirsToFlags(genRule.GeneratedHeaderDirs())
Dan Willemsenb3454ab2016-09-28 17:34:58 -07001259 depPaths.Flags = append(depPaths.Flags, flags)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001260 if depTag == genHeaderExportDepTag {
Dan Willemsenb3454ab2016-09-28 17:34:58 -07001261 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags)
Dan Willemsen847dcc72016-09-29 12:13:36 -07001262 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps,
Dan Willemsen9da9d492018-02-21 18:28:18 -08001263 genRule.GeneratedDeps()...)
Jayant Chowdhary715cac32017-04-20 06:53:59 -07001264 // Add these re-exported flags to help header-abi-dumper to infer the abi exported by a library.
1265 c.sabi.Properties.ReexportedIncludeFlags = append(c.sabi.Properties.ReexportedIncludeFlags, flags)
1266
Dan Willemsenb3454ab2016-09-28 17:34:58 -07001267 }
Dan Willemsenb40aab62016-04-20 14:21:14 -07001268 } else {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001269 ctx.ModuleErrorf("module %q is not a genrule", depName)
Dan Willemsenb40aab62016-04-20 14:21:14 -07001270 }
Dan Willemsenc77a0b32017-09-18 23:19:12 -07001271 case linkerScriptDepTag:
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001272 if genRule, ok := dep.(genrule.SourceFileGenerator); ok {
Dan Willemsenc77a0b32017-09-18 23:19:12 -07001273 files := genRule.GeneratedSourceFiles()
1274 if len(files) == 1 {
1275 depPaths.LinkerScript = android.OptionalPathForPath(files[0])
1276 } else if len(files) > 1 {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001277 ctx.ModuleErrorf("module %q can only generate a single file if used for a linker script", depName)
Dan Willemsenc77a0b32017-09-18 23:19:12 -07001278 }
1279 } else {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001280 ctx.ModuleErrorf("module %q is not a genrule", depName)
Dan Willemsenc77a0b32017-09-18 23:19:12 -07001281 }
Colin Crossca860ac2016-01-04 14:34:37 -08001282 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001283 return
1284 }
1285
Colin Crossd11fcda2017-10-23 17:59:01 -07001286 if dep.Target().Os != ctx.Os() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001287 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1288 return
1289 }
Colin Crossd11fcda2017-10-23 17:59:01 -07001290 if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001291 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
Colin Crossa1ad8d12016-06-01 17:09:44 -07001292 return
1293 }
1294
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001295 // re-exporting flags
1296 if depTag == reuseObjTag {
1297 if l, ok := ccDep.compiler.(libraryInterface); ok {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001298 c.staticVariant = ccDep
Colin Crossbbc9f4d2017-05-03 16:24:55 -07001299 objs, flags, deps := l.reuseObjs()
Colin Cross10d22312017-05-03 11:01:58 -07001300 depPaths.Objs = depPaths.Objs.Append(objs)
1301 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Colin Crossbbc9f4d2017-05-03 16:24:55 -07001302 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps, deps...)
Colin Crossbba99042016-11-23 15:45:05 -08001303 return
1304 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001305 }
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001306 if t, ok := depTag.(dependencyTag); ok && t.library {
1307 if i, ok := ccDep.linker.(exportedFlagsProducer); ok {
Dan Willemsen76f08272016-07-09 00:14:08 -07001308 flags := i.exportedFlags()
Dan Willemsen847dcc72016-09-29 12:13:36 -07001309 deps := i.exportedFlagsDeps()
Dan Willemsen76f08272016-07-09 00:14:08 -07001310 depPaths.Flags = append(depPaths.Flags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -07001311 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders, deps...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001312
1313 if t.reexportFlags {
Dan Willemsen76f08272016-07-09 00:14:08 -07001314 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -07001315 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps, deps...)
Jayant Chowdhary715cac32017-04-20 06:53:59 -07001316 // 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 -07001317 // Re-exported shared library headers must be included as well since they can help us with type information
1318 // about template instantiations (instantiated from their headers).
1319 c.sabi.Properties.ReexportedIncludeFlags = append(c.sabi.Properties.ReexportedIncludeFlags, flags...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001320 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001321 }
Dan Willemsena96ff642016-06-07 12:34:45 -07001322
Logan Chienf3511742017-10-31 18:04:35 +08001323 checkLinkType(ctx, c, ccDep, t)
Jiyong Park5fb8c102018-04-09 12:03:06 +09001324 checkDoubleLoadableLibries(ctx, c, ccDep)
Colin Crossc99deeb2016-04-11 15:06:20 -07001325 }
1326
Colin Cross26c34ed2016-09-30 17:10:16 -07001327 var ptr *android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -07001328 var depPtr *android.Paths
Colin Crossc99deeb2016-04-11 15:06:20 -07001329
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001330 linkFile := ccDep.outputFile
Colin Cross26c34ed2016-09-30 17:10:16 -07001331 depFile := android.OptionalPath{}
1332
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001333 switch depTag {
Dan Albert914449f2016-06-17 16:45:24 -07001334 case ndkStubDepTag, sharedDepTag, sharedExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001335 ptr = &depPaths.SharedLibs
1336 depPtr = &depPaths.SharedLibsDeps
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001337 depFile = ccDep.linker.(libraryInterface).toc()
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001338 directSharedDeps = append(directSharedDeps, ccDep)
Dan Albert914449f2016-06-17 16:45:24 -07001339 case lateSharedDepTag, ndkLateStubDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001340 ptr = &depPaths.LateSharedLibs
1341 depPtr = &depPaths.LateSharedLibsDeps
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001342 depFile = ccDep.linker.(libraryInterface).toc()
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001343 case staticDepTag, staticExportDepTag:
Jeff Gaston294356f2017-09-27 17:05:30 -07001344 ptr = nil
1345 directStaticDeps = append(directStaticDeps, ccDep)
Colin Crossc99deeb2016-04-11 15:06:20 -07001346 case lateStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001347 ptr = &depPaths.LateStaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -07001348 case wholeStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001349 ptr = &depPaths.WholeStaticLibs
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001350 staticLib, ok := ccDep.linker.(libraryInterface)
Colin Crossb916a382016-07-29 17:28:03 -07001351 if !ok || !staticLib.static() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001352 ctx.ModuleErrorf("module %q not a static library", depName)
Colin Crossc99deeb2016-04-11 15:06:20 -07001353 return
1354 }
1355
1356 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001357 postfix := " (required by " + ctx.OtherModuleName(dep) + ")"
Colin Crossc99deeb2016-04-11 15:06:20 -07001358 for i := range missingDeps {
1359 missingDeps[i] += postfix
1360 }
1361 ctx.AddMissingDependencies(missingDeps)
1362 }
Dan Willemsen5cb580f2016-09-26 17:33:01 -07001363 depPaths.WholeStaticLibObjs = depPaths.WholeStaticLibObjs.Append(staticLib.objs())
Colin Cross5950f382016-12-13 12:50:57 -08001364 case headerDepTag:
1365 // Nothing
Colin Crossc99deeb2016-04-11 15:06:20 -07001366 case objDepTag:
Dan Willemsen5cb580f2016-09-26 17:33:01 -07001367 depPaths.Objs.objFiles = append(depPaths.Objs.objFiles, linkFile.Path())
Colin Crossc99deeb2016-04-11 15:06:20 -07001368 case crtBeginDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001369 depPaths.CrtBegin = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -07001370 case crtEndDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001371 depPaths.CrtEnd = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -07001372 }
1373
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001374 switch depTag {
Dan Willemsen581341d2017-02-09 16:16:31 -08001375 case staticDepTag, staticExportDepTag, lateStaticDepTag:
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001376 staticLib, ok := ccDep.linker.(libraryInterface)
Dan Willemsen581341d2017-02-09 16:16:31 -08001377 if !ok || !staticLib.static() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001378 ctx.ModuleErrorf("module %q not a static library", depName)
Dan Willemsen581341d2017-02-09 16:16:31 -08001379 return
1380 }
1381
1382 // When combining coverage files for shared libraries and executables, coverage files
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001383 // in static libraries act as if they were whole static libraries. The same goes for
1384 // source based Abi dump files.
Dan Willemsen581341d2017-02-09 16:16:31 -08001385 depPaths.StaticLibObjs.coverageFiles = append(depPaths.StaticLibObjs.coverageFiles,
1386 staticLib.objs().coverageFiles...)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001387 depPaths.StaticLibObjs.sAbiDumpFiles = append(depPaths.StaticLibObjs.sAbiDumpFiles,
1388 staticLib.objs().sAbiDumpFiles...)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001389
Dan Willemsen581341d2017-02-09 16:16:31 -08001390 }
1391
Colin Cross26c34ed2016-09-30 17:10:16 -07001392 if ptr != nil {
Colin Crossce75d2c2016-10-06 16:12:58 -07001393 if !linkFile.Valid() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001394 ctx.ModuleErrorf("module %q missing output file", depName)
Colin Crossce75d2c2016-10-06 16:12:58 -07001395 return
1396 }
Colin Cross26c34ed2016-09-30 17:10:16 -07001397 *ptr = append(*ptr, linkFile.Path())
1398 }
1399
Colin Crossc99deeb2016-04-11 15:06:20 -07001400 if depPtr != nil {
Colin Cross26c34ed2016-09-30 17:10:16 -07001401 dep := depFile
1402 if !dep.Valid() {
1403 dep = linkFile
1404 }
1405 *depPtr = append(*depPtr, dep.Path())
Colin Crossca860ac2016-01-04 14:34:37 -08001406 }
Jiyong Park27b188b2017-07-18 13:23:39 +09001407
Logan Chien43d34c32017-12-20 01:17:32 +08001408 makeLibName := func(depName string) string {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001409 libName := strings.TrimSuffix(depName, llndkLibrarySuffix)
Jiyong Park374510b2018-03-19 18:23:01 +09001410 libName = strings.TrimSuffix(libName, vendorPublicLibrarySuffix)
Jiyong Park27b188b2017-07-18 13:23:39 +09001411 libName = strings.TrimPrefix(libName, "prebuilt_")
Jiyong Parkd5b18a52017-08-03 21:22:50 +09001412 isLLndk := inList(libName, llndkLibraries)
Jiyong Park374510b2018-03-19 18:23:01 +09001413 isVendorPublicLib := inList(libName, vendorPublicLibraries)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001414 bothVendorAndCoreVariantsExist := ccDep.hasVendorVariant() || isLLndk
1415 if c.useVndk() && bothVendorAndCoreVariantsExist {
1416 // The vendor module in Make will have been renamed to not conflict with the core
1417 // module, so update the dependency name here accordingly.
Logan Chien43d34c32017-12-20 01:17:32 +08001418 return libName + vendorSuffix
Jiyong Park374510b2018-03-19 18:23:01 +09001419 } else if (ctx.Platform() || ctx.ProductSpecific()) && isVendorPublicLib {
Logan Chien43d34c32017-12-20 01:17:32 +08001420 return libName + vendorPublicLibrarySuffix
Jiyong Parkf9332f12018-02-01 00:54:12 +09001421 } else if ccDep.inRecovery() && !ccDep.onlyInRecovery() {
1422 return libName + recoverySuffix
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001423 } else {
Logan Chien43d34c32017-12-20 01:17:32 +08001424 return libName
Jiyong Park27b188b2017-07-18 13:23:39 +09001425 }
Logan Chien43d34c32017-12-20 01:17:32 +08001426 }
1427
1428 // Export the shared libs to Make.
1429 switch depTag {
1430 case sharedDepTag, sharedExportDepTag, lateSharedDepTag:
Jiyong Park27b188b2017-07-18 13:23:39 +09001431 // Note: the order of libs in this list is not important because
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001432 // they merely serve as Make dependencies and do not affect this lib itself.
Logan Chien43d34c32017-12-20 01:17:32 +08001433 c.Properties.AndroidMkSharedLibs = append(
1434 c.Properties.AndroidMkSharedLibs, makeLibName(depName))
1435 case runtimeDepTag:
1436 c.Properties.AndroidMkRuntimeLibs = append(
1437 c.Properties.AndroidMkRuntimeLibs, makeLibName(depName))
Jiyong Park27b188b2017-07-18 13:23:39 +09001438 }
Colin Crossca860ac2016-01-04 14:34:37 -08001439 })
1440
Jeff Gaston294356f2017-09-27 17:05:30 -07001441 // use the ordered dependencies as this module's dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08001442 depPaths.StaticLibs = append(depPaths.StaticLibs, orderStaticModuleDeps(c, directStaticDeps, directSharedDeps)...)
Jeff Gaston294356f2017-09-27 17:05:30 -07001443
Colin Crossdd84e052017-05-17 13:44:16 -07001444 // Dedup exported flags from dependencies
Colin Crossb6715442017-10-24 11:13:31 -07001445 depPaths.Flags = android.FirstUniqueStrings(depPaths.Flags)
Dan Willemsenfe92c962017-08-29 12:28:37 -07001446 depPaths.GeneratedHeaders = android.FirstUniquePaths(depPaths.GeneratedHeaders)
Colin Crossb6715442017-10-24 11:13:31 -07001447 depPaths.ReexportedFlags = android.FirstUniqueStrings(depPaths.ReexportedFlags)
Dan Willemsenfe92c962017-08-29 12:28:37 -07001448 depPaths.ReexportedFlagsDeps = android.FirstUniquePaths(depPaths.ReexportedFlagsDeps)
1449
1450 if c.sabi != nil {
Colin Crossb6715442017-10-24 11:13:31 -07001451 c.sabi.Properties.ReexportedIncludeFlags = android.FirstUniqueStrings(c.sabi.Properties.ReexportedIncludeFlags)
Dan Willemsenfe92c962017-08-29 12:28:37 -07001452 }
Colin Crossdd84e052017-05-17 13:44:16 -07001453
Colin Crossca860ac2016-01-04 14:34:37 -08001454 return depPaths
1455}
1456
1457func (c *Module) InstallInData() bool {
1458 if c.installer == nil {
1459 return false
1460 }
Vishwath Mohan1dd88392017-03-29 22:00:18 -07001461 return c.installer.inData()
1462}
1463
1464func (c *Module) InstallInSanitizerDir() bool {
1465 if c.installer == nil {
1466 return false
1467 }
1468 if c.sanitize != nil && c.sanitize.inSanitizerDir() {
Colin Cross94610402016-08-29 13:41:32 -07001469 return true
1470 }
Vishwath Mohan1dd88392017-03-29 22:00:18 -07001471 return c.installer.inSanitizerDir()
Colin Crossca860ac2016-01-04 14:34:37 -08001472}
1473
Jiyong Parkf9332f12018-02-01 00:54:12 +09001474func (c *Module) InstallInRecovery() bool {
1475 return c.inRecovery()
1476}
1477
Dan Willemsen4aa75ca2016-09-28 16:18:03 -07001478func (c *Module) HostToolPath() android.OptionalPath {
1479 if c.installer == nil {
1480 return android.OptionalPath{}
1481 }
1482 return c.installer.hostToolPath()
1483}
1484
Nan Zhangd4e641b2017-07-12 12:55:28 -07001485func (c *Module) IntermPathForModuleOut() android.OptionalPath {
1486 return c.outputFile
1487}
1488
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001489func (c *Module) Srcs() android.Paths {
1490 if c.outputFile.Valid() {
1491 return android.Paths{c.outputFile.Path()}
1492 }
1493 return android.Paths{}
1494}
1495
Vishwath Mohanb743e9c2017-11-01 09:20:21 +00001496func (c *Module) static() bool {
1497 if static, ok := c.linker.(interface {
1498 static() bool
1499 }); ok {
1500 return static.static()
1501 }
1502 return false
1503}
1504
Colin Crossb60190a2018-09-04 16:28:17 -07001505func (c *Module) getMakeLinkType() string {
1506 if c.useVndk() {
1507 if inList(c.Name(), vndkCoreLibraries) || inList(c.Name(), vndkSpLibraries) || inList(c.Name(), llndkLibraries) {
1508 if inList(c.Name(), vndkPrivateLibraries) {
1509 return "native:vndk_private"
1510 } else {
1511 return "native:vndk"
1512 }
1513 } else {
1514 return "native:vendor"
1515 }
1516 } else if c.inRecovery() {
1517 return "native:recovery"
1518 } else if c.Target().Os == android.Android && String(c.Properties.Sdk_version) != "" {
1519 return "native:ndk:none:none"
1520 // TODO(b/114741097): use the correct ndk stl once build errors have been fixed
1521 //family, link := getNdkStlFamilyAndLinkType(c)
1522 //return fmt.Sprintf("native:ndk:%s:%s", family, link)
1523 } else {
1524 return "native:platform"
1525 }
1526}
1527
Colin Cross2ba19d92015-05-07 15:44:20 -07001528//
Colin Crosscfad1192015-11-02 16:43:11 -08001529// Defaults
1530//
Colin Crossca860ac2016-01-04 14:34:37 -08001531type Defaults struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001532 android.ModuleBase
Colin Cross1f44a3a2017-07-07 14:33:33 -07001533 android.DefaultsModuleBase
Colin Crosscfad1192015-11-02 16:43:11 -08001534}
1535
Colin Cross635c3b02016-05-18 15:37:25 -07001536func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -08001537}
1538
Colin Cross1e676be2016-10-12 14:38:15 -07001539func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
1540}
1541
Colin Cross36242852017-06-23 15:06:31 -07001542func defaultsFactory() android.Module {
Colin Crosse1d764e2016-08-18 14:18:32 -07001543 return DefaultsFactory()
1544}
1545
Colin Cross36242852017-06-23 15:06:31 -07001546func DefaultsFactory(props ...interface{}) android.Module {
Colin Crossca860ac2016-01-04 14:34:37 -08001547 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -08001548
Colin Cross36242852017-06-23 15:06:31 -07001549 module.AddProperties(props...)
1550 module.AddProperties(
Colin Crossca860ac2016-01-04 14:34:37 -08001551 &BaseProperties{},
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001552 &VendorProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001553 &BaseCompilerProperties{},
1554 &BaseLinkerProperties{},
Chih-Hung Hsieh86814712018-05-23 18:30:46 -07001555 &MoreBaseLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -07001556 &LibraryProperties{},
Colin Cross919281a2016-04-05 16:42:05 -07001557 &FlagExporterProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001558 &BinaryLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -07001559 &TestProperties{},
1560 &TestBinaryProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001561 &StlProperties{},
Colin Cross16b23492016-01-06 14:41:07 -08001562 &SanitizeProperties{},
Colin Cross665dce92016-04-28 14:50:03 -07001563 &StripProperties{},
Dan Willemsen7424d612016-09-01 13:45:39 -07001564 &InstallerProperties{},
Dan Willemsena03cf6d2016-09-26 15:45:04 -07001565 &TidyProperties{},
Dan Willemsen581341d2017-02-09 16:16:31 -08001566 &CoverageProperties{},
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001567 &SAbiProperties{},
Justin Yun4b2382f2017-07-26 14:22:10 +09001568 &VndkProperties{},
Stephen Craneba090d12017-05-09 15:44:35 -07001569 &LTOProperties{},
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -07001570 &PgoProperties{},
Dan Willemsen6424d172018-03-08 13:27:59 -08001571 &android.ProtoProperties{},
Colin Crosse1d764e2016-08-18 14:18:32 -07001572 )
Colin Crosscfad1192015-11-02 16:43:11 -08001573
Colin Cross1f44a3a2017-07-07 14:33:33 -07001574 android.InitDefaultsModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001575
1576 return module
Colin Crosscfad1192015-11-02 16:43:11 -08001577}
1578
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001579const (
1580 // coreMode is the variant used for framework-private libraries, or
1581 // SDK libraries. (which framework-private libraries can use)
1582 coreMode = "core"
1583
1584 // vendorMode is the variant used for /vendor code that compiles
1585 // against the VNDK.
1586 vendorMode = "vendor"
Jiyong Parkf9332f12018-02-01 00:54:12 +09001587
1588 recoveryMode = "recovery"
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001589)
1590
Jiyong Park6a43f042017-10-12 23:05:00 +09001591func squashVendorSrcs(m *Module) {
1592 if lib, ok := m.compiler.(*libraryDecorator); ok {
1593 lib.baseCompiler.Properties.Srcs = append(lib.baseCompiler.Properties.Srcs,
1594 lib.baseCompiler.Properties.Target.Vendor.Srcs...)
1595
1596 lib.baseCompiler.Properties.Exclude_srcs = append(lib.baseCompiler.Properties.Exclude_srcs,
1597 lib.baseCompiler.Properties.Target.Vendor.Exclude_srcs...)
1598 }
1599}
1600
Jiyong Parkf9332f12018-02-01 00:54:12 +09001601func squashRecoverySrcs(m *Module) {
1602 if lib, ok := m.compiler.(*libraryDecorator); ok {
1603 lib.baseCompiler.Properties.Srcs = append(lib.baseCompiler.Properties.Srcs,
1604 lib.baseCompiler.Properties.Target.Recovery.Srcs...)
1605
1606 lib.baseCompiler.Properties.Exclude_srcs = append(lib.baseCompiler.Properties.Exclude_srcs,
1607 lib.baseCompiler.Properties.Target.Recovery.Exclude_srcs...)
1608 }
1609}
1610
1611func imageMutator(mctx android.BottomUpMutatorContext) {
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001612 if mctx.Os() != android.Android {
1613 return
1614 }
1615
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001616 if genrule, ok := mctx.Module().(*genrule.Module); ok {
Jiyong Park3f736c92018-05-24 13:36:56 +09001617 if props, ok := genrule.Extra.(*GenruleExtraProperties); ok {
1618 var coreVariantNeeded bool = false
1619 var vendorVariantNeeded bool = false
1620 var recoveryVariantNeeded bool = false
Justin Yun71549282017-11-17 12:10:28 +09001621 if mctx.DeviceConfig().VndkVersion() == "" {
Jiyong Park3f736c92018-05-24 13:36:56 +09001622 coreVariantNeeded = true
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001623 } else if Bool(props.Vendor_available) {
Jiyong Park3f736c92018-05-24 13:36:56 +09001624 coreVariantNeeded = true
1625 vendorVariantNeeded = true
Jiyong Park2db76922017-11-08 16:03:48 +09001626 } else if mctx.SocSpecific() || mctx.DeviceSpecific() {
Jiyong Park3f736c92018-05-24 13:36:56 +09001627 vendorVariantNeeded = true
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001628 } else {
Jiyong Park3f736c92018-05-24 13:36:56 +09001629 coreVariantNeeded = true
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001630 }
Jiyong Park3f736c92018-05-24 13:36:56 +09001631 if Bool(props.Recovery_available) {
1632 recoveryVariantNeeded = true
1633 }
1634
Jiyong Park413cc742018-06-19 01:46:06 +09001635 if recoveryVariantNeeded {
Jiyong Park8d52f862018-07-07 18:02:07 +09001636 primaryArch := mctx.Config().DevicePrimaryArchType()
1637 moduleArch := genrule.Target().Arch.ArchType
1638 if moduleArch != primaryArch {
Jiyong Park413cc742018-06-19 01:46:06 +09001639 recoveryVariantNeeded = false
1640 }
1641 }
1642
Jiyong Park3f736c92018-05-24 13:36:56 +09001643 var variants []string
1644 if coreVariantNeeded {
1645 variants = append(variants, coreMode)
1646 }
1647 if vendorVariantNeeded {
1648 variants = append(variants, vendorMode)
1649 }
1650 if recoveryVariantNeeded {
1651 variants = append(variants, recoveryMode)
1652 }
1653 mctx.CreateVariations(variants...)
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001654 }
1655 }
1656
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001657 m, ok := mctx.Module().(*Module)
1658 if !ok {
1659 return
1660 }
1661
1662 // Sanity check
Logan Chienf3511742017-10-31 18:04:35 +08001663 vendorSpecific := mctx.SocSpecific() || mctx.DeviceSpecific()
1664
1665 if m.VendorProperties.Vendor_available != nil && vendorSpecific {
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001666 mctx.PropertyErrorf("vendor_available",
Jiyong Park2db76922017-11-08 16:03:48 +09001667 "doesn't make sense at the same time as `vendor: true`, `proprietary: true`, or `device_specific:true`")
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001668 return
1669 }
Logan Chienf3511742017-10-31 18:04:35 +08001670
1671 if vndkdep := m.vndkdep; vndkdep != nil {
1672 if vndkdep.isVndk() {
1673 if vendorSpecific {
1674 if !vndkdep.isVndkExt() {
1675 mctx.PropertyErrorf("vndk",
1676 "must set `extends: \"...\"` to vndk extension")
1677 return
1678 }
1679 } else {
1680 if vndkdep.isVndkExt() {
1681 mctx.PropertyErrorf("vndk",
1682 "must set `vendor: true` to set `extends: %q`",
1683 m.getVndkExtendsModuleName())
1684 return
1685 }
1686 if m.VendorProperties.Vendor_available == nil {
1687 mctx.PropertyErrorf("vndk",
1688 "vendor_available must be set to either true or false when `vndk: {enabled: true}`")
1689 return
1690 }
1691 }
1692 } else {
1693 if vndkdep.isVndkSp() {
1694 mctx.PropertyErrorf("vndk",
1695 "must set `enabled: true` to set `support_system_process: true`")
1696 return
1697 }
1698 if vndkdep.isVndkExt() {
1699 mctx.PropertyErrorf("vndk",
1700 "must set `enabled: true` to set `extends: %q`",
1701 m.getVndkExtendsModuleName())
1702 return
1703 }
Justin Yun8effde42017-06-23 19:24:43 +09001704 }
1705 }
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001706
Jiyong Parkf9332f12018-02-01 00:54:12 +09001707 var coreVariantNeeded bool = false
1708 var vendorVariantNeeded bool = false
1709 var recoveryVariantNeeded bool = false
1710
Justin Yun71549282017-11-17 12:10:28 +09001711 if mctx.DeviceConfig().VndkVersion() == "" {
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001712 // If the device isn't compiling against the VNDK, we always
1713 // use the core mode.
Jiyong Parkf9332f12018-02-01 00:54:12 +09001714 coreVariantNeeded = true
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001715 } else if _, ok := m.linker.(*llndkStubDecorator); ok {
1716 // LL-NDK stubs only exist in the vendor variant, since the
1717 // real libraries will be used in the core variant.
Jiyong Parkf9332f12018-02-01 00:54:12 +09001718 vendorVariantNeeded = true
Jiyong Park2a454122017-10-19 15:59:33 +09001719 } else if _, ok := m.linker.(*llndkHeadersDecorator); ok {
1720 // ... and LL-NDK headers as well
Jiyong Parkf9332f12018-02-01 00:54:12 +09001721 vendorVariantNeeded = true
Justin Yun312ccb92018-01-23 12:07:46 +09001722 } else if _, ok := m.linker.(*vndkPrebuiltLibraryDecorator); ok {
Justin Yun71549282017-11-17 12:10:28 +09001723 // Make vendor variants only for the versions in BOARD_VNDK_VERSION and
1724 // PRODUCT_EXTRA_VNDK_VERSIONS.
Jiyong Parkf9332f12018-02-01 00:54:12 +09001725 vendorVariantNeeded = true
Logan Chienf3511742017-10-31 18:04:35 +08001726 } else if m.hasVendorVariant() && !vendorSpecific {
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001727 // This will be available in both /system and /vendor
Justin Yun8effde42017-06-23 19:24:43 +09001728 // or a /system directory that is available to vendor.
Jiyong Parkf9332f12018-02-01 00:54:12 +09001729 coreVariantNeeded = true
1730 vendorVariantNeeded = true
Logan Chienf3511742017-10-31 18:04:35 +08001731 } else if vendorSpecific && String(m.Properties.Sdk_version) == "" {
Jiyong Park2db76922017-11-08 16:03:48 +09001732 // This will be available in /vendor (or /odm) only
Jiyong Parkf9332f12018-02-01 00:54:12 +09001733 vendorVariantNeeded = true
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001734 } else {
1735 // This is either in /system (or similar: /data), or is a
1736 // modules built with the NDK. Modules built with the NDK
1737 // will be restricted using the existing link type checks.
Jiyong Parkf9332f12018-02-01 00:54:12 +09001738 coreVariantNeeded = true
1739 }
1740
1741 if Bool(m.Properties.Recovery_available) {
1742 recoveryVariantNeeded = true
1743 }
1744
1745 if m.ModuleBase.InstallInRecovery() {
1746 recoveryVariantNeeded = true
1747 coreVariantNeeded = false
1748 }
1749
Jiyong Park413cc742018-06-19 01:46:06 +09001750 if recoveryVariantNeeded {
Jiyong Park8d52f862018-07-07 18:02:07 +09001751 primaryArch := mctx.Config().DevicePrimaryArchType()
1752 moduleArch := m.Target().Arch.ArchType
1753 if moduleArch != primaryArch {
Jiyong Park413cc742018-06-19 01:46:06 +09001754 recoveryVariantNeeded = false
1755 }
1756 }
1757
Jiyong Parkf9332f12018-02-01 00:54:12 +09001758 var variants []string
1759 if coreVariantNeeded {
1760 variants = append(variants, coreMode)
1761 }
1762 if vendorVariantNeeded {
1763 variants = append(variants, vendorMode)
1764 }
1765 if recoveryVariantNeeded {
1766 variants = append(variants, recoveryMode)
1767 }
1768 mod := mctx.CreateVariations(variants...)
1769 for i, v := range variants {
1770 if v == vendorMode {
1771 m := mod[i].(*Module)
1772 m.Properties.UseVndk = true
1773 squashVendorSrcs(m)
1774 } else if v == recoveryMode {
1775 m := mod[i].(*Module)
1776 m.Properties.InRecovery = true
Jiyong Park5baac542018-08-28 09:55:37 +09001777 m.MakeAsPlatform()
Jiyong Parkf9332f12018-02-01 00:54:12 +09001778 squashRecoverySrcs(m)
1779 }
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001780 }
1781}
1782
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -07001783func getCurrentNdkPrebuiltVersion(ctx DepsContext) string {
Colin Cross6510f912017-11-29 00:27:14 -08001784 if ctx.Config().PlatformSdkVersionInt() > config.NdkMaxPrebuiltVersionInt {
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -07001785 return strconv.Itoa(config.NdkMaxPrebuiltVersionInt)
1786 }
Colin Cross6510f912017-11-29 00:27:14 -08001787 return ctx.Config().PlatformSdkVersion()
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -07001788}
1789
Colin Cross06a931b2015-10-28 17:23:31 -07001790var Bool = proptools.Bool
Colin Cross38b40df2018-04-10 16:14:46 -07001791var BoolDefault = proptools.BoolDefault
Nan Zhang0007d812017-11-07 10:57:05 -08001792var BoolPtr = proptools.BoolPtr
1793var String = proptools.String
1794var StringPtr = proptools.StringPtr