blob: 4c02e9e2995ee5fa441859d7342fc32b065953e0 [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 Park6a43f042017-10-12 23:05:00 +090037 ctx.BottomUp("image", vendorMutator).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
Colin Cross1e676be2016-10-12 14:38:15 -070049 ctx.TopDown("tsan_deps", sanitizerDepsMutator(tsan))
50 ctx.BottomUp("tsan", sanitizerMutator(tsan)).Parallel()
Dan Willemsen581341d2017-02-09 16:16:31 -080051
52 ctx.BottomUp("coverage", coverageLinkingMutator).Parallel()
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -080053 ctx.TopDown("vndk_deps", sabiDepsMutator)
Stephen Craneba090d12017-05-09 15:44:35 -070054
55 ctx.TopDown("lto_deps", ltoDepsMutator)
56 ctx.BottomUp("lto", ltoMutator).Parallel()
Colin Cross1e676be2016-10-12 14:38:15 -070057 })
Colin Crossb98c8b02016-07-29 13:44:28 -070058
59 pctx.Import("android/soong/cc/config")
Colin Cross463a90e2015-06-17 14:20:06 -070060}
61
Colin Crossca860ac2016-01-04 14:34:37 -080062type Deps struct {
63 SharedLibs, LateSharedLibs []string
64 StaticLibs, LateStaticLibs, WholeStaticLibs []string
Colin Cross5950f382016-12-13 12:50:57 -080065 HeaderLibs []string
Colin Crossc472d572015-03-17 15:06:21 -070066
Colin Cross5950f382016-12-13 12:50:57 -080067 ReexportSharedLibHeaders, ReexportStaticLibHeaders, ReexportHeaderLibHeaders []string
Dan Willemsen490a8dc2016-06-06 18:22:19 -070068
Colin Cross81413472016-04-11 14:37:39 -070069 ObjFiles []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -070070
Dan Willemsenb40aab62016-04-20 14:21:14 -070071 GeneratedSources []string
72 GeneratedHeaders []string
73
Dan Willemsenb3454ab2016-09-28 17:34:58 -070074 ReexportGeneratedHeaders []string
75
Colin Cross97ba0732015-03-23 17:50:24 -070076 CrtBegin, CrtEnd string
Dan Willemsenc77a0b32017-09-18 23:19:12 -070077 LinkerScript string
Colin Crossc472d572015-03-17 15:06:21 -070078}
79
Colin Crossca860ac2016-01-04 14:34:37 -080080type PathDeps struct {
Colin Cross26c34ed2016-09-30 17:10:16 -070081 // Paths to .so files
82 SharedLibs, LateSharedLibs android.Paths
83 // Paths to the dependencies to use for .so files (.so.toc files)
84 SharedLibsDeps, LateSharedLibsDeps android.Paths
85 // Paths to .a files
Colin Cross635c3b02016-05-18 15:37:25 -070086 StaticLibs, LateStaticLibs, WholeStaticLibs android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -070087
Colin Cross26c34ed2016-09-30 17:10:16 -070088 // Paths to .o files
Dan Willemsen5cb580f2016-09-26 17:33:01 -070089 Objs Objects
Dan Willemsen581341d2017-02-09 16:16:31 -080090 StaticLibObjs Objects
Dan Willemsen5cb580f2016-09-26 17:33:01 -070091 WholeStaticLibObjs Objects
Dan Willemsen34cc69e2015-09-23 15:26:20 -070092
Colin Cross26c34ed2016-09-30 17:10:16 -070093 // Paths to generated source files
Colin Cross635c3b02016-05-18 15:37:25 -070094 GeneratedSources android.Paths
95 GeneratedHeaders android.Paths
Dan Willemsenb40aab62016-04-20 14:21:14 -070096
Dan Willemsen76f08272016-07-09 00:14:08 -070097 Flags, ReexportedFlags []string
Dan Willemsen847dcc72016-09-29 12:13:36 -070098 ReexportedFlagsDeps android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -070099
Colin Cross26c34ed2016-09-30 17:10:16 -0700100 // Paths to crt*.o files
Colin Cross635c3b02016-05-18 15:37:25 -0700101 CrtBegin, CrtEnd android.OptionalPath
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700102 LinkerScript android.OptionalPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700103}
104
Colin Crossca860ac2016-01-04 14:34:37 -0800105type Flags struct {
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700106 GlobalFlags []string // Flags that apply to C, C++, and assembly source files
107 ArFlags []string // Flags that apply to ar
108 AsFlags []string // Flags that apply to assembly source files
109 CFlags []string // Flags that apply to C and C++ source files
110 ToolingCFlags []string // Flags that apply to C and C++ source files parsed by clang LibTooling tools
111 ConlyFlags []string // Flags that apply to C source files
112 CppFlags []string // Flags that apply to C++ source files
113 ToolingCppFlags []string // Flags that apply to C++ source files parsed by clang LibTooling tools
114 YaccFlags []string // Flags that apply to Yacc source files
115 protoFlags []string // Flags that apply to proto source files
116 aidlFlags []string // Flags that apply to aidl source files
117 rsFlags []string // Flags that apply to renderscript source files
118 LdFlags []string // Flags that apply to linker command lines
119 libFlags []string // Flags to add libraries early to the link order
120 TidyFlags []string // Flags that apply to clang-tidy
121 SAbiFlags []string // Flags that apply to header-abi-dumper
122 YasmFlags []string // Flags that apply to yasm assembly source files
Colin Cross28344522015-04-22 13:07:53 -0700123
Colin Crossc3199482017-03-30 15:03:04 -0700124 // Global include flags that apply to C, C++, and assembly source files
125 // These must be after any module include flags, which will be in GlobalFlags.
126 SystemIncludeFlags []string
127
Colin Crossb98c8b02016-07-29 13:44:28 -0700128 Toolchain config.Toolchain
Colin Cross28344522015-04-22 13:07:53 -0700129 Clang bool
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700130 Tidy bool
Dan Willemsen581341d2017-02-09 16:16:31 -0800131 Coverage bool
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800132 SAbiDump bool
Colin Crossca860ac2016-01-04 14:34:37 -0800133
134 RequiredInstructionSet string
Colin Cross16b23492016-01-06 14:41:07 -0800135 DynamicLinker string
136
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700137 CFlagsDeps android.Paths // Files depended on by compiler flags
138 LdFlagsDeps android.Paths // Files depended on by linker flags
Colin Cross18c0c5a2016-12-01 14:45:23 -0800139
140 GroupStaticLibs bool
Colin Crossc472d572015-03-17 15:06:21 -0700141}
142
Colin Cross81413472016-04-11 14:37:39 -0700143type ObjectLinkerProperties struct {
144 // names of other cc_object modules to link into this module using partial linking
145 Objs []string `android:"arch_variant"`
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700146
147 // if set, add an extra objcopy --prefix-symbols= step
148 Prefix_symbols string
Colin Cross81413472016-04-11 14:37:39 -0700149}
150
Colin Crossca860ac2016-01-04 14:34:37 -0800151// Properties used to compile all C or C++ modules
152type BaseProperties struct {
153 // compile module with clang instead of gcc
154 Clang *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700155
156 // Minimum sdk version supported when compiling against the ndk
157 Sdk_version string
158
Colin Crossca860ac2016-01-04 14:34:37 -0800159 // don't insert default compiler flags into asflags, cflags,
160 // cppflags, conlyflags, ldflags, or include_dirs
161 No_default_compiler_flags *bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700162
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700163 AndroidMkSharedLibs []string `blueprint:"mutated"`
164 HideFromMake bool `blueprint:"mutated"`
165 PreventInstall bool `blueprint:"mutated"`
166
167 UseVndk bool `blueprint:"mutated"`
168}
169
170type VendorProperties struct {
Jiyong Park82e2bf32017-08-16 14:05:54 +0900171 // whether this module should be allowed to be directly depended by other
172 // modules with `vendor: true`, `proprietary: true`, or `vendor_available:true`.
173 // If set to true, two variants will be built separately, one like
174 // normal, and the other limited to the set of libraries and headers
175 // that are exposed to /vendor modules.
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700176 //
177 // The vendor variant may be used with a different (newer) /system,
178 // so it shouldn't have any unversioned runtime dependencies, or
179 // make assumptions about the system that may not be true in the
180 // future.
181 //
Jiyong Park82e2bf32017-08-16 14:05:54 +0900182 // If set to false, this module becomes inaccessible from /vendor modules.
183 //
184 // Default value is true when vndk: {enabled: true} or vendor: true.
185 //
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700186 // Nothing happens if BOARD_VNDK_VERSION isn't set in the BoardConfig.mk
187 Vendor_available *bool
Colin Crossca860ac2016-01-04 14:34:37 -0800188}
189
Colin Crossca860ac2016-01-04 14:34:37 -0800190type UnusedProperties struct {
Dan Willemsen581341d2017-02-09 16:16:31 -0800191 Tags []string
Colin Crosscfad1192015-11-02 16:43:11 -0800192}
193
Colin Crossca860ac2016-01-04 14:34:37 -0800194type ModuleContextIntf interface {
Colin Crossca860ac2016-01-04 14:34:37 -0800195 static() bool
196 staticBinary() bool
197 clang() bool
Colin Crossb98c8b02016-07-29 13:44:28 -0700198 toolchain() config.Toolchain
Colin Crossca860ac2016-01-04 14:34:37 -0800199 noDefaultCompilerFlags() bool
200 sdk() bool
201 sdkVersion() string
Jeff Gaston7b6118b2017-10-04 21:07:42 +0000202 vndk() bool
Justin Yun8effde42017-06-23 19:24:43 +0900203 isVndk() bool
204 isVndkSp() bool
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800205 createVndkSourceAbiDump() bool
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700206 selectedStl() string
Colin Crossce75d2c2016-10-06 16:12:58 -0700207 baseModuleName() string
Colin Crossca860ac2016-01-04 14:34:37 -0800208}
209
210type ModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700211 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800212 ModuleContextIntf
213}
214
215type BaseModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700216 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800217 ModuleContextIntf
218}
219
Colin Cross37047f12016-12-13 17:06:13 -0800220type DepsContext interface {
221 android.BottomUpMutatorContext
222 ModuleContextIntf
223}
224
Colin Crossca860ac2016-01-04 14:34:37 -0800225type feature interface {
226 begin(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800227 deps(ctx DepsContext, deps Deps) Deps
Colin Crossca860ac2016-01-04 14:34:37 -0800228 flags(ctx ModuleContext, flags Flags) Flags
229 props() []interface{}
230}
231
232type compiler interface {
Colin Cross42742b82016-08-01 13:20:05 -0700233 compilerInit(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800234 compilerDeps(ctx DepsContext, deps Deps) Deps
Colin Cross42742b82016-08-01 13:20:05 -0700235 compilerFlags(ctx ModuleContext, flags Flags) Flags
236 compilerProps() []interface{}
237
Colin Cross76fada02016-07-27 10:31:13 -0700238 appendCflags([]string)
239 appendAsflags([]string)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700240 compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects
Colin Crossca860ac2016-01-04 14:34:37 -0800241}
242
243type linker interface {
Colin Cross42742b82016-08-01 13:20:05 -0700244 linkerInit(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800245 linkerDeps(ctx DepsContext, deps Deps) Deps
Colin Cross42742b82016-08-01 13:20:05 -0700246 linkerFlags(ctx ModuleContext, flags Flags) Flags
247 linkerProps() []interface{}
248
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700249 link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path
Colin Cross76fada02016-07-27 10:31:13 -0700250 appendLdflags([]string)
Colin Crossca860ac2016-01-04 14:34:37 -0800251}
252
253type installer interface {
Colin Cross42742b82016-08-01 13:20:05 -0700254 installerProps() []interface{}
Colin Cross635c3b02016-05-18 15:37:25 -0700255 install(ctx ModuleContext, path android.Path)
Colin Crossca860ac2016-01-04 14:34:37 -0800256 inData() bool
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700257 inSanitizerDir() bool
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700258 hostToolPath() android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800259}
260
Colin Crossc99deeb2016-04-11 15:06:20 -0700261type dependencyTag struct {
262 blueprint.BaseDependencyTag
263 name string
264 library bool
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700265
266 reexportFlags bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700267}
268
269var (
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700270 sharedDepTag = dependencyTag{name: "shared", library: true}
271 sharedExportDepTag = dependencyTag{name: "shared", library: true, reexportFlags: true}
272 lateSharedDepTag = dependencyTag{name: "late shared", library: true}
273 staticDepTag = dependencyTag{name: "static", library: true}
274 staticExportDepTag = dependencyTag{name: "static", library: true, reexportFlags: true}
275 lateStaticDepTag = dependencyTag{name: "late static", library: true}
276 wholeStaticDepTag = dependencyTag{name: "whole static", library: true, reexportFlags: true}
Colin Cross32ec36c2016-12-15 07:39:51 -0800277 headerDepTag = dependencyTag{name: "header", library: true}
278 headerExportDepTag = dependencyTag{name: "header", library: true, reexportFlags: true}
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700279 genSourceDepTag = dependencyTag{name: "gen source"}
280 genHeaderDepTag = dependencyTag{name: "gen header"}
281 genHeaderExportDepTag = dependencyTag{name: "gen header", reexportFlags: true}
282 objDepTag = dependencyTag{name: "obj"}
283 crtBeginDepTag = dependencyTag{name: "crtbegin"}
284 crtEndDepTag = dependencyTag{name: "crtend"}
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700285 linkerScriptDepTag = dependencyTag{name: "linker script"}
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700286 reuseObjTag = dependencyTag{name: "reuse objects"}
287 ndkStubDepTag = dependencyTag{name: "ndk stub", library: true}
288 ndkLateStubDepTag = dependencyTag{name: "ndk late stub", library: true}
Colin Crossc99deeb2016-04-11 15:06:20 -0700289)
290
Colin Crossca860ac2016-01-04 14:34:37 -0800291// Module contains the properties and members used by all C/C++ module types, and implements
292// the blueprint.Module interface. It delegates to compiler, linker, and installer interfaces
293// to construct the output file. Behavior can be customized with a Customizer interface
294type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700295 android.ModuleBase
Colin Cross1f44a3a2017-07-07 14:33:33 -0700296 android.DefaultableModuleBase
Colin Crossc472d572015-03-17 15:06:21 -0700297
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700298 Properties BaseProperties
299 VendorProperties VendorProperties
300 unused UnusedProperties
Colin Crossfa138792015-04-24 17:31:52 -0700301
Colin Crossca860ac2016-01-04 14:34:37 -0800302 // initialize before calling Init
Colin Cross635c3b02016-05-18 15:37:25 -0700303 hod android.HostOrDeviceSupported
304 multilib android.Multilib
Colin Crossc472d572015-03-17 15:06:21 -0700305
Colin Crossca860ac2016-01-04 14:34:37 -0800306 // delegates, initialize before calling Init
Colin Crossb4ce0ec2016-09-13 13:41:39 -0700307 features []feature
308 compiler compiler
309 linker linker
310 installer installer
311 stl *stl
312 sanitize *sanitize
Dan Willemsen581341d2017-02-09 16:16:31 -0800313 coverage *coverage
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800314 sabi *sabi
Justin Yun8effde42017-06-23 19:24:43 +0900315 vndkdep *vndkdep
Stephen Craneba090d12017-05-09 15:44:35 -0700316 lto *lto
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700317 pgo *pgo
Colin Cross16b23492016-01-06 14:41:07 -0800318
319 androidMkSharedLibDeps []string
Colin Cross74d1ec02015-04-28 13:30:13 -0700320
Colin Cross635c3b02016-05-18 15:37:25 -0700321 outputFile android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800322
Colin Crossb98c8b02016-07-29 13:44:28 -0700323 cachedToolchain config.Toolchain
Colin Crossb916a382016-07-29 17:28:03 -0700324
325 subAndroidMkOnce map[subAndroidMkProvider]bool
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800326
327 // Flags used to compile this module
328 flags Flags
Colin Crossc472d572015-03-17 15:06:21 -0700329}
330
Colin Cross36242852017-06-23 15:06:31 -0700331func (c *Module) Init() android.Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700332 c.AddProperties(&c.Properties, &c.VendorProperties, &c.unused)
Colin Crossca860ac2016-01-04 14:34:37 -0800333 if c.compiler != nil {
Colin Cross36242852017-06-23 15:06:31 -0700334 c.AddProperties(c.compiler.compilerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800335 }
336 if c.linker != nil {
Colin Cross36242852017-06-23 15:06:31 -0700337 c.AddProperties(c.linker.linkerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800338 }
339 if c.installer != nil {
Colin Cross36242852017-06-23 15:06:31 -0700340 c.AddProperties(c.installer.installerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800341 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700342 if c.stl != nil {
Colin Cross36242852017-06-23 15:06:31 -0700343 c.AddProperties(c.stl.props()...)
Colin Crossa8e07cc2016-04-04 15:07:06 -0700344 }
Colin Cross16b23492016-01-06 14:41:07 -0800345 if c.sanitize != nil {
Colin Cross36242852017-06-23 15:06:31 -0700346 c.AddProperties(c.sanitize.props()...)
Colin Cross16b23492016-01-06 14:41:07 -0800347 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800348 if c.coverage != nil {
Colin Cross36242852017-06-23 15:06:31 -0700349 c.AddProperties(c.coverage.props()...)
Dan Willemsen581341d2017-02-09 16:16:31 -0800350 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800351 if c.sabi != nil {
Colin Cross36242852017-06-23 15:06:31 -0700352 c.AddProperties(c.sabi.props()...)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800353 }
Justin Yun8effde42017-06-23 19:24:43 +0900354 if c.vndkdep != nil {
355 c.AddProperties(c.vndkdep.props()...)
356 }
Stephen Craneba090d12017-05-09 15:44:35 -0700357 if c.lto != nil {
358 c.AddProperties(c.lto.props()...)
359 }
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700360 if c.pgo != nil {
361 c.AddProperties(c.pgo.props()...)
362 }
Colin Crossca860ac2016-01-04 14:34:37 -0800363 for _, feature := range c.features {
Colin Cross36242852017-06-23 15:06:31 -0700364 c.AddProperties(feature.props()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800365 }
Colin Crossc472d572015-03-17 15:06:21 -0700366
Colin Cross36242852017-06-23 15:06:31 -0700367 android.InitAndroidArchModule(c, c.hod, c.multilib)
Colin Crossc472d572015-03-17 15:06:21 -0700368
Colin Cross1f44a3a2017-07-07 14:33:33 -0700369 android.InitDefaultableModule(c)
Colin Cross36242852017-06-23 15:06:31 -0700370
371 return c
Colin Crossc472d572015-03-17 15:06:21 -0700372}
373
Colin Crossb916a382016-07-29 17:28:03 -0700374// Returns true for dependency roots (binaries)
375// TODO(ccross): also handle dlopenable libraries
376func (c *Module) isDependencyRoot() bool {
377 if root, ok := c.linker.(interface {
378 isDependencyRoot() bool
379 }); ok {
380 return root.isDependencyRoot()
381 }
382 return false
383}
384
Jeff Gaston7b6118b2017-10-04 21:07:42 +0000385func (c *Module) vndk() bool {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700386 return c.Properties.UseVndk
387}
388
Justin Yun8effde42017-06-23 19:24:43 +0900389func (c *Module) isVndk() bool {
390 if c.vndkdep != nil {
391 return c.vndkdep.isVndk()
392 }
393 return false
394}
395
Jiyong Park82e2bf32017-08-16 14:05:54 +0900396// Returns true only when this module is configured to have core and vendor
397// variants.
398func (c *Module) hasVendorVariant() bool {
399 return c.isVndk() || Bool(c.VendorProperties.Vendor_available)
400}
401
Colin Crossca860ac2016-01-04 14:34:37 -0800402type baseModuleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700403 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800404 moduleContextImpl
405}
406
Colin Cross37047f12016-12-13 17:06:13 -0800407type depsContext struct {
408 android.BottomUpMutatorContext
409 moduleContextImpl
410}
411
Colin Crossca860ac2016-01-04 14:34:37 -0800412type moduleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700413 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800414 moduleContextImpl
415}
416
Jeff Gaston7b6118b2017-10-04 21:07:42 +0000417// Vendor returns true for vendor modules excluding VNDK libraries so that
418// they get installed onto the correct partition
419func (ctx *moduleContext) Vendor() bool {
420 return ctx.ModuleContext.Vendor() || (ctx.mod.vndk() && !ctx.mod.isVndk())
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700421}
422
Colin Crossca860ac2016-01-04 14:34:37 -0800423type moduleContextImpl struct {
424 mod *Module
425 ctx BaseModuleContext
426}
427
Colin Crossca860ac2016-01-04 14:34:37 -0800428func (ctx *moduleContextImpl) clang() bool {
429 return ctx.mod.clang(ctx.ctx)
430}
431
Colin Crossb98c8b02016-07-29 13:44:28 -0700432func (ctx *moduleContextImpl) toolchain() config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800433 return ctx.mod.toolchain(ctx.ctx)
434}
435
436func (ctx *moduleContextImpl) static() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700437 if static, ok := ctx.mod.linker.(interface {
438 static() bool
439 }); ok {
440 return static.static()
Colin Crossca860ac2016-01-04 14:34:37 -0800441 }
Colin Crossb916a382016-07-29 17:28:03 -0700442 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800443}
444
445func (ctx *moduleContextImpl) staticBinary() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700446 if static, ok := ctx.mod.linker.(interface {
447 staticBinary() bool
448 }); ok {
449 return static.staticBinary()
Colin Crossca860ac2016-01-04 14:34:37 -0800450 }
Colin Crossb916a382016-07-29 17:28:03 -0700451 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800452}
453
454func (ctx *moduleContextImpl) noDefaultCompilerFlags() bool {
455 return Bool(ctx.mod.Properties.No_default_compiler_flags)
456}
457
458func (ctx *moduleContextImpl) sdk() bool {
Jeff Gaston7b6118b2017-10-04 21:07:42 +0000459 if ctx.ctx.Device() && !ctx.vndk() {
Dan Willemsena96ff642016-06-07 12:34:45 -0700460 return ctx.mod.Properties.Sdk_version != ""
461 }
462 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800463}
464
465func (ctx *moduleContextImpl) sdkVersion() string {
Dan Willemsena96ff642016-06-07 12:34:45 -0700466 if ctx.ctx.Device() {
Jeff Gaston7b6118b2017-10-04 21:07:42 +0000467 if ctx.vndk() {
Dan Willemsen11b26142017-03-19 18:30:37 -0700468 return "current"
Dan Willemsend2ede872016-11-18 14:54:24 -0800469 } else {
470 return ctx.mod.Properties.Sdk_version
471 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700472 }
473 return ""
Colin Crossca860ac2016-01-04 14:34:37 -0800474}
475
Jeff Gaston7b6118b2017-10-04 21:07:42 +0000476func (ctx *moduleContextImpl) vndk() bool {
477 return ctx.mod.vndk()
478}
479
Justin Yun8effde42017-06-23 19:24:43 +0900480func (ctx *moduleContextImpl) isVndk() bool {
481 return ctx.mod.isVndk()
482}
483
484func (ctx *moduleContextImpl) isVndkSp() bool {
485 if vndk := ctx.mod.vndkdep; vndk != nil {
486 return vndk.isVndkSp()
487 }
488 return false
489}
490
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800491// Create source abi dumps if the module belongs to the list of VndkLibraries.
492func (ctx *moduleContextImpl) createVndkSourceAbiDump() bool {
Jeff Gaston7b6118b2017-10-04 21:07:42 +0000493 return ctx.ctx.Device() && ((ctx.vndk() && ctx.isVndk()) || inList(ctx.baseModuleName(), llndkLibraries))
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800494}
495
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700496func (ctx *moduleContextImpl) selectedStl() string {
497 if stl := ctx.mod.stl; stl != nil {
498 return stl.Properties.SelectedStl
499 }
500 return ""
501}
502
Colin Crossce75d2c2016-10-06 16:12:58 -0700503func (ctx *moduleContextImpl) baseModuleName() string {
504 return ctx.mod.ModuleBase.BaseModuleName()
505}
506
Colin Cross635c3b02016-05-18 15:37:25 -0700507func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800508 return &Module{
509 hod: hod,
510 multilib: multilib,
511 }
512}
513
Colin Cross635c3b02016-05-18 15:37:25 -0700514func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800515 module := newBaseModule(hod, multilib)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700516 module.features = []feature{
517 &tidyFeature{},
518 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700519 module.stl = &stl{}
Colin Cross16b23492016-01-06 14:41:07 -0800520 module.sanitize = &sanitize{}
Dan Willemsen581341d2017-02-09 16:16:31 -0800521 module.coverage = &coverage{}
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800522 module.sabi = &sabi{}
Justin Yun8effde42017-06-23 19:24:43 +0900523 module.vndkdep = &vndkdep{}
Stephen Craneba090d12017-05-09 15:44:35 -0700524 module.lto = &lto{}
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700525 module.pgo = &pgo{}
Colin Crossca860ac2016-01-04 14:34:37 -0800526 return module
527}
528
Colin Crossce75d2c2016-10-06 16:12:58 -0700529func (c *Module) Prebuilt() *android.Prebuilt {
530 if p, ok := c.linker.(prebuiltLinkerInterface); ok {
531 return p.prebuilt()
532 }
533 return nil
534}
535
536func (c *Module) Name() string {
537 name := c.ModuleBase.Name()
Dan Willemsen01a90592017-04-07 15:21:13 -0700538 if p, ok := c.linker.(interface {
539 Name(string) string
540 }); ok {
Colin Crossce75d2c2016-10-06 16:12:58 -0700541 name = p.Name(name)
542 }
543 return name
544}
545
Colin Cross635c3b02016-05-18 15:37:25 -0700546func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800547 ctx := &moduleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700548 ModuleContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800549 moduleContextImpl: moduleContextImpl{
550 mod: c,
551 },
552 }
553 ctx.ctx = ctx
554
555 flags := Flags{
556 Toolchain: c.toolchain(ctx),
557 Clang: c.clang(ctx),
558 }
Colin Crossca860ac2016-01-04 14:34:37 -0800559 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700560 flags = c.compiler.compilerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800561 }
562 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700563 flags = c.linker.linkerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800564 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700565 if c.stl != nil {
566 flags = c.stl.flags(ctx, flags)
567 }
Colin Cross16b23492016-01-06 14:41:07 -0800568 if c.sanitize != nil {
569 flags = c.sanitize.flags(ctx, flags)
570 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800571 if c.coverage != nil {
572 flags = c.coverage.flags(ctx, flags)
573 }
Stephen Craneba090d12017-05-09 15:44:35 -0700574 if c.lto != nil {
575 flags = c.lto.flags(ctx, flags)
576 }
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700577 if c.pgo != nil {
578 flags = c.pgo.flags(ctx, flags)
579 }
Colin Crossca860ac2016-01-04 14:34:37 -0800580 for _, feature := range c.features {
581 flags = feature.flags(ctx, flags)
582 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800583 if ctx.Failed() {
584 return
585 }
586
Colin Crossb98c8b02016-07-29 13:44:28 -0700587 flags.CFlags, _ = filterList(flags.CFlags, config.IllegalFlags)
588 flags.CppFlags, _ = filterList(flags.CppFlags, config.IllegalFlags)
589 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, config.IllegalFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800590
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800591 deps := c.depsToPaths(ctx)
592 if ctx.Failed() {
593 return
594 }
595 flags.GlobalFlags = append(flags.GlobalFlags, deps.Flags...)
596 c.flags = flags
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700597 // We need access to all the flags seen by a source file.
598 if c.sabi != nil {
599 flags = c.sabi.flags(ctx, flags)
600 }
Colin Crossca860ac2016-01-04 14:34:37 -0800601 // Optimization to reduce size of build.ninja
602 // Replace the long list of flags for each file with a module-local variable
603 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
604 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
605 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
606 flags.CFlags = []string{"$cflags"}
607 flags.CppFlags = []string{"$cppflags"}
608 flags.AsFlags = []string{"$asflags"}
609
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700610 var objs Objects
Colin Crossca860ac2016-01-04 14:34:37 -0800611 if c.compiler != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700612 objs = c.compiler.compile(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -0800613 if ctx.Failed() {
614 return
615 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800616 }
617
Colin Crossca860ac2016-01-04 14:34:37 -0800618 if c.linker != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700619 outputFile := c.linker.link(ctx, flags, deps, objs)
Colin Crossca860ac2016-01-04 14:34:37 -0800620 if ctx.Failed() {
621 return
622 }
Colin Cross635c3b02016-05-18 15:37:25 -0700623 c.outputFile = android.OptionalPathForPath(outputFile)
Colin Crossce75d2c2016-10-06 16:12:58 -0700624 }
Colin Cross5049f022015-03-18 13:28:46 -0700625
Colin Crossce75d2c2016-10-06 16:12:58 -0700626 if c.installer != nil && !c.Properties.PreventInstall && c.outputFile.Valid() {
627 c.installer.install(ctx, c.outputFile.Path())
628 if ctx.Failed() {
629 return
Colin Crossca860ac2016-01-04 14:34:37 -0800630 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700631 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800632}
633
Colin Crossb98c8b02016-07-29 13:44:28 -0700634func (c *Module) toolchain(ctx BaseModuleContext) config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800635 if c.cachedToolchain == nil {
Colin Crossb98c8b02016-07-29 13:44:28 -0700636 c.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
Colin Cross3f40fa42015-01-30 17:27:36 -0800637 }
Colin Crossca860ac2016-01-04 14:34:37 -0800638 return c.cachedToolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800639}
640
Colin Crossca860ac2016-01-04 14:34:37 -0800641func (c *Module) begin(ctx BaseModuleContext) {
642 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700643 c.compiler.compilerInit(ctx)
Colin Cross21b9a242015-03-24 14:15:58 -0700644 }
Colin Crossca860ac2016-01-04 14:34:37 -0800645 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700646 c.linker.linkerInit(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800647 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700648 if c.stl != nil {
649 c.stl.begin(ctx)
650 }
Colin Cross16b23492016-01-06 14:41:07 -0800651 if c.sanitize != nil {
652 c.sanitize.begin(ctx)
653 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800654 if c.coverage != nil {
655 c.coverage.begin(ctx)
656 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800657 if c.sabi != nil {
658 c.sabi.begin(ctx)
659 }
Justin Yun8effde42017-06-23 19:24:43 +0900660 if c.vndkdep != nil {
661 c.vndkdep.begin(ctx)
662 }
Stephen Craneba090d12017-05-09 15:44:35 -0700663 if c.lto != nil {
664 c.lto.begin(ctx)
665 }
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700666 if c.pgo != nil {
667 c.pgo.begin(ctx)
668 }
Colin Crossca860ac2016-01-04 14:34:37 -0800669 for _, feature := range c.features {
670 feature.begin(ctx)
671 }
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700672 if ctx.sdk() {
Dan Albertf5415d72017-08-17 16:19:59 -0700673 version, err := normalizeNdkApiLevel(ctx, ctx.sdkVersion(), ctx.Arch())
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700674 if err != nil {
675 ctx.PropertyErrorf("sdk_version", err.Error())
676 }
Dan Albert90f7a4d2016-11-08 14:34:24 -0800677 c.Properties.Sdk_version = version
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700678 }
Colin Crossca860ac2016-01-04 14:34:37 -0800679}
680
Colin Cross37047f12016-12-13 17:06:13 -0800681func (c *Module) deps(ctx DepsContext) Deps {
Colin Crossc99deeb2016-04-11 15:06:20 -0700682 deps := Deps{}
683
684 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700685 deps = c.compiler.compilerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700686 }
Pirama Arumuga Nainar49b53d52017-10-04 16:47:29 -0700687 // Add the PGO dependency (the clang_rt.profile runtime library), which
688 // sometimes depends on symbols from libgcc, before libgcc gets added
689 // in linkerDeps().
690 if c.pgo != nil {
691 deps = c.pgo.deps(ctx, deps)
692 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700693 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700694 deps = c.linker.linkerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700695 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700696 if c.stl != nil {
697 deps = c.stl.deps(ctx, deps)
698 }
Colin Cross16b23492016-01-06 14:41:07 -0800699 if c.sanitize != nil {
700 deps = c.sanitize.deps(ctx, deps)
701 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800702 if c.coverage != nil {
703 deps = c.coverage.deps(ctx, deps)
704 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800705 if c.sabi != nil {
706 deps = c.sabi.deps(ctx, deps)
707 }
Justin Yun8effde42017-06-23 19:24:43 +0900708 if c.vndkdep != nil {
709 deps = c.vndkdep.deps(ctx, deps)
710 }
Stephen Craneba090d12017-05-09 15:44:35 -0700711 if c.lto != nil {
712 deps = c.lto.deps(ctx, deps)
713 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700714 for _, feature := range c.features {
715 deps = feature.deps(ctx, deps)
716 }
717
718 deps.WholeStaticLibs = lastUniqueElements(deps.WholeStaticLibs)
719 deps.StaticLibs = lastUniqueElements(deps.StaticLibs)
720 deps.LateStaticLibs = lastUniqueElements(deps.LateStaticLibs)
721 deps.SharedLibs = lastUniqueElements(deps.SharedLibs)
722 deps.LateSharedLibs = lastUniqueElements(deps.LateSharedLibs)
Colin Cross5950f382016-12-13 12:50:57 -0800723 deps.HeaderLibs = lastUniqueElements(deps.HeaderLibs)
Colin Crossc99deeb2016-04-11 15:06:20 -0700724
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700725 for _, lib := range deps.ReexportSharedLibHeaders {
726 if !inList(lib, deps.SharedLibs) {
727 ctx.PropertyErrorf("export_shared_lib_headers", "Shared library not in shared_libs: '%s'", lib)
728 }
729 }
730
731 for _, lib := range deps.ReexportStaticLibHeaders {
732 if !inList(lib, deps.StaticLibs) {
733 ctx.PropertyErrorf("export_static_lib_headers", "Static library not in static_libs: '%s'", lib)
734 }
735 }
736
Colin Cross5950f382016-12-13 12:50:57 -0800737 for _, lib := range deps.ReexportHeaderLibHeaders {
738 if !inList(lib, deps.HeaderLibs) {
739 ctx.PropertyErrorf("export_header_lib_headers", "Header library not in header_libs: '%s'", lib)
740 }
741 }
742
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700743 for _, gen := range deps.ReexportGeneratedHeaders {
744 if !inList(gen, deps.GeneratedHeaders) {
745 ctx.PropertyErrorf("export_generated_headers", "Generated header module not in generated_headers: '%s'", gen)
746 }
747 }
748
Colin Crossc99deeb2016-04-11 15:06:20 -0700749 return deps
750}
751
Dan Albert7e9d2952016-08-04 13:02:36 -0700752func (c *Module) beginMutator(actx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800753 ctx := &baseModuleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700754 BaseContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800755 moduleContextImpl: moduleContextImpl{
756 mod: c,
757 },
758 }
759 ctx.ctx = ctx
760
Colin Crossca860ac2016-01-04 14:34:37 -0800761 c.begin(ctx)
Dan Albert7e9d2952016-08-04 13:02:36 -0700762}
763
Colin Cross1e676be2016-10-12 14:38:15 -0700764func (c *Module) DepsMutator(actx android.BottomUpMutatorContext) {
765 if !c.Enabled() {
766 return
767 }
768
Colin Cross37047f12016-12-13 17:06:13 -0800769 ctx := &depsContext{
770 BottomUpMutatorContext: actx,
Dan Albert7e9d2952016-08-04 13:02:36 -0700771 moduleContextImpl: moduleContextImpl{
772 mod: c,
773 },
774 }
775 ctx.ctx = ctx
Colin Crossca860ac2016-01-04 14:34:37 -0800776
Colin Crossc99deeb2016-04-11 15:06:20 -0700777 deps := c.deps(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800778
Dan Albert914449f2016-06-17 16:45:24 -0700779 variantNdkLibs := []string{}
780 variantLateNdkLibs := []string{}
Dan Willemsenb916b802017-03-19 13:44:32 -0700781 if ctx.Os() == android.Android {
Dan Albert914449f2016-06-17 16:45:24 -0700782 version := ctx.sdkVersion()
Dan Willemsen72d39932016-07-08 23:23:48 -0700783
Jeff Gaston7b6118b2017-10-04 21:07:42 +0000784 // Rewrites the names of shared libraries into the names of the NDK
785 // libraries where appropriate. This returns two slices.
Dan Albert914449f2016-06-17 16:45:24 -0700786 //
Jeff Gaston7b6118b2017-10-04 21:07:42 +0000787 // The first is a list of non-variant shared libraries (either rewritten
788 // NDK libraries to the modules in prebuilts/ndk, or not rewritten
789 // because they are not NDK libraries).
Dan Albert914449f2016-06-17 16:45:24 -0700790 //
Jeff Gaston7b6118b2017-10-04 21:07:42 +0000791 // The second is a list of ndk_library modules. These need to be
792 // separated because they are a variation dependency and must be added
793 // in a different manner.
794 rewriteNdkLibs := func(list []string) ([]string, []string) {
795 variantLibs := []string{}
796 nonvariantLibs := []string{}
Dan Albert914449f2016-06-17 16:45:24 -0700797 for _, entry := range list {
Dan Willemsenb916b802017-03-19 13:44:32 -0700798 if ctx.sdk() && inList(entry, ndkPrebuiltSharedLibraries) {
Dan Albert914449f2016-06-17 16:45:24 -0700799 if !inList(entry, ndkMigratedLibs) {
800 nonvariantLibs = append(nonvariantLibs, entry+".ndk."+version)
801 } else {
802 variantLibs = append(variantLibs, entry+ndkLibrarySuffix)
803 }
Jeff Gaston7b6118b2017-10-04 21:07:42 +0000804 } else if ctx.vndk() && inList(entry, llndkLibraries) {
Dan Willemsenb916b802017-03-19 13:44:32 -0700805 nonvariantLibs = append(nonvariantLibs, entry+llndkLibrarySuffix)
Dan Albert914449f2016-06-17 16:45:24 -0700806 } else {
Dan Willemsen7cbf5f82017-03-28 00:08:30 -0700807 nonvariantLibs = append(nonvariantLibs, entry)
Dan Willemsen72d39932016-07-08 23:23:48 -0700808 }
809 }
Dan Albert914449f2016-06-17 16:45:24 -0700810 return nonvariantLibs, variantLibs
Dan Willemsen72d39932016-07-08 23:23:48 -0700811 }
812
Dan Albert914449f2016-06-17 16:45:24 -0700813 deps.SharedLibs, variantNdkLibs = rewriteNdkLibs(deps.SharedLibs)
814 deps.LateSharedLibs, variantLateNdkLibs = rewriteNdkLibs(deps.LateSharedLibs)
Jiyong Park4c35af02017-07-05 13:41:55 +0900815 deps.ReexportSharedLibHeaders, _ = rewriteNdkLibs(deps.ReexportSharedLibHeaders)
Dan Willemsen72d39932016-07-08 23:23:48 -0700816 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700817
Colin Cross32ec36c2016-12-15 07:39:51 -0800818 for _, lib := range deps.HeaderLibs {
819 depTag := headerDepTag
820 if inList(lib, deps.ReexportHeaderLibHeaders) {
821 depTag = headerExportDepTag
822 }
823 actx.AddVariationDependencies(nil, depTag, lib)
824 }
Colin Cross5950f382016-12-13 12:50:57 -0800825
Colin Crossc99deeb2016-04-11 15:06:20 -0700826 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, wholeStaticDepTag,
827 deps.WholeStaticLibs...)
828
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700829 for _, lib := range deps.StaticLibs {
830 depTag := staticDepTag
831 if inList(lib, deps.ReexportStaticLibHeaders) {
832 depTag = staticExportDepTag
833 }
Colin Cross15a0d462016-07-14 14:49:58 -0700834 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700835 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700836
837 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, lateStaticDepTag,
838 deps.LateStaticLibs...)
839
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700840 for _, lib := range deps.SharedLibs {
841 depTag := sharedDepTag
842 if inList(lib, deps.ReexportSharedLibHeaders) {
843 depTag = sharedExportDepTag
844 }
Colin Cross15a0d462016-07-14 14:49:58 -0700845 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700846 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700847
848 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, lateSharedDepTag,
849 deps.LateSharedLibs...)
850
Colin Cross68861832016-07-08 10:41:41 -0700851 actx.AddDependency(c, genSourceDepTag, deps.GeneratedSources...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700852
853 for _, gen := range deps.GeneratedHeaders {
854 depTag := genHeaderDepTag
855 if inList(gen, deps.ReexportGeneratedHeaders) {
856 depTag = genHeaderExportDepTag
857 }
858 actx.AddDependency(c, depTag, gen)
859 }
Dan Willemsenb40aab62016-04-20 14:21:14 -0700860
Colin Cross68861832016-07-08 10:41:41 -0700861 actx.AddDependency(c, objDepTag, deps.ObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700862
863 if deps.CrtBegin != "" {
Colin Cross68861832016-07-08 10:41:41 -0700864 actx.AddDependency(c, crtBeginDepTag, deps.CrtBegin)
Colin Crossca860ac2016-01-04 14:34:37 -0800865 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700866 if deps.CrtEnd != "" {
Colin Cross68861832016-07-08 10:41:41 -0700867 actx.AddDependency(c, crtEndDepTag, deps.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700868 }
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700869 if deps.LinkerScript != "" {
870 actx.AddDependency(c, linkerScriptDepTag, deps.LinkerScript)
871 }
Dan Albert914449f2016-06-17 16:45:24 -0700872
873 version := ctx.sdkVersion()
874 actx.AddVariationDependencies([]blueprint.Variation{
875 {"ndk_api", version}, {"link", "shared"}}, ndkStubDepTag, variantNdkLibs...)
876 actx.AddVariationDependencies([]blueprint.Variation{
877 {"ndk_api", version}, {"link", "shared"}}, ndkLateStubDepTag, variantLateNdkLibs...)
Colin Cross6362e272015-10-29 15:25:03 -0700878}
Colin Cross21b9a242015-03-24 14:15:58 -0700879
Dan Albert7e9d2952016-08-04 13:02:36 -0700880func beginMutator(ctx android.BottomUpMutatorContext) {
881 if c, ok := ctx.Module().(*Module); ok && c.Enabled() {
882 c.beginMutator(ctx)
883 }
884}
885
Colin Crossca860ac2016-01-04 14:34:37 -0800886func (c *Module) clang(ctx BaseModuleContext) bool {
887 clang := Bool(c.Properties.Clang)
888
889 if c.Properties.Clang == nil {
890 if ctx.Host() {
891 clang = true
892 }
893
894 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
895 clang = true
896 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800897 }
Colin Cross28344522015-04-22 13:07:53 -0700898
Colin Crossca860ac2016-01-04 14:34:37 -0800899 if !c.toolchain(ctx).ClangSupported() {
900 clang = false
901 }
902
903 return clang
904}
905
Colin Crossc99deeb2016-04-11 15:06:20 -0700906// Convert dependencies to paths. Returns a PathDeps containing paths
Colin Cross635c3b02016-05-18 15:37:25 -0700907func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
Colin Crossca860ac2016-01-04 14:34:37 -0800908 var depPaths PathDeps
Colin Crossca860ac2016-01-04 14:34:37 -0800909
Jeff Gaston7b6118b2017-10-04 21:07:42 +0000910 // Whether a module can link to another module, taking into
911 // account NDK linking.
912 checkLinkType := func(from, to *Module) {
913 if from.Target().Os != android.Android {
914 // Host code is not restricted
915 return
916 }
917 if from.Properties.UseVndk {
918 // Though vendor code is limited by the vendor mutator,
919 // each vendor-available module needs to check
920 // link-type for VNDK.
921 if from.vndkdep != nil {
922 from.vndkdep.vndkCheckLinkType(ctx, to)
923 }
924 return
925 }
926 if from.Properties.Sdk_version == "" {
927 // Platform code can link to anything
928 return
929 }
930 if _, ok := to.linker.(*toolchainLibraryDecorator); ok {
931 // These are always allowed
932 return
933 }
934 if _, ok := to.linker.(*ndkPrebuiltLibraryLinker); ok {
935 // These are allowed, but don't set sdk_version
936 return
937 }
938 if _, ok := to.linker.(*ndkPrebuiltStlLinker); ok {
939 // These are allowed, but don't set sdk_version
940 return
941 }
942 if _, ok := to.linker.(*stubDecorator); ok {
943 // These aren't real libraries, but are the stub shared libraries that are included in
944 // the NDK.
945 return
946 }
947 if to.Properties.Sdk_version == "" {
948 // NDK code linking to platform code is never okay.
949 ctx.ModuleErrorf("depends on non-NDK-built library %q",
950 ctx.OtherModuleName(to))
951 }
Dan Albert9e10cd42016-08-03 14:12:14 -0700952
Jeff Gaston7b6118b2017-10-04 21:07:42 +0000953 // All this point we know we have two NDK libraries, but we need to
954 // check that we're not linking against anything built against a higher
955 // API level, as it is only valid to link against older or equivalent
956 // APIs.
957
958 if from.Properties.Sdk_version == "current" {
959 // Current can link against anything.
960 return
961 } else if to.Properties.Sdk_version == "current" {
962 // Current can't be linked against by anything else.
963 ctx.ModuleErrorf("links %q built against newer API version %q",
964 ctx.OtherModuleName(to), "current")
965 }
966
967 fromApi, err := strconv.Atoi(from.Properties.Sdk_version)
968 if err != nil {
969 ctx.PropertyErrorf("sdk_version",
970 "Invalid sdk_version value (must be int): %q",
971 from.Properties.Sdk_version)
972 }
973 toApi, err := strconv.Atoi(to.Properties.Sdk_version)
974 if err != nil {
975 ctx.PropertyErrorf("sdk_version",
976 "Invalid sdk_version value (must be int): %q",
977 to.Properties.Sdk_version)
978 }
979
980 if toApi > fromApi {
981 ctx.ModuleErrorf("links %q built against newer API version %q",
982 ctx.OtherModuleName(to), to.Properties.Sdk_version)
983 }
984 }
985
986 ctx.VisitDirectDeps(func(m blueprint.Module) {
987 name := ctx.OtherModuleName(m)
988 tag := ctx.OtherModuleDependencyTag(m)
989
990 a, _ := m.(android.Module)
991 if a == nil {
992 ctx.ModuleErrorf("module %q not an android module", name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700993 return
Colin Crossca860ac2016-01-04 14:34:37 -0800994 }
Colin Crossca860ac2016-01-04 14:34:37 -0800995
Jeff Gaston7b6118b2017-10-04 21:07:42 +0000996 cc, _ := m.(*Module)
997 if cc == nil {
998 switch tag {
Colin Cross068e0fe2016-12-13 15:23:47 -0800999 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -07001000 // Nothing to do
Dan Willemsenb40aab62016-04-20 14:21:14 -07001001 case genSourceDepTag:
Jeff Gaston7b6118b2017-10-04 21:07:42 +00001002 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
Dan Willemsenb40aab62016-04-20 14:21:14 -07001003 depPaths.GeneratedSources = append(depPaths.GeneratedSources,
1004 genRule.GeneratedSourceFiles()...)
1005 } else {
Jeff Gaston7b6118b2017-10-04 21:07:42 +00001006 ctx.ModuleErrorf("module %q is not a gensrcs or genrule", name)
Dan Willemsenb40aab62016-04-20 14:21:14 -07001007 }
Colin Crosse90bfd12017-04-26 16:59:26 -07001008 // Support exported headers from a generated_sources dependency
1009 fallthrough
Dan Willemsenb3454ab2016-09-28 17:34:58 -07001010 case genHeaderDepTag, genHeaderExportDepTag:
Jeff Gaston7b6118b2017-10-04 21:07:42 +00001011 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
Dan Willemsenb40aab62016-04-20 14:21:14 -07001012 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders,
1013 genRule.GeneratedSourceFiles()...)
Colin Cross5ed99c62016-11-22 12:55:55 -08001014 flags := includeDirsToFlags(genRule.GeneratedHeaderDirs())
Dan Willemsenb3454ab2016-09-28 17:34:58 -07001015 depPaths.Flags = append(depPaths.Flags, flags)
Jeff Gaston7b6118b2017-10-04 21:07:42 +00001016 if tag == genHeaderExportDepTag {
Dan Willemsenb3454ab2016-09-28 17:34:58 -07001017 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags)
Dan Willemsen847dcc72016-09-29 12:13:36 -07001018 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps,
1019 genRule.GeneratedSourceFiles()...)
Jayant Chowdhary715cac32017-04-20 06:53:59 -07001020 // Add these re-exported flags to help header-abi-dumper to infer the abi exported by a library.
1021 c.sabi.Properties.ReexportedIncludeFlags = append(c.sabi.Properties.ReexportedIncludeFlags, flags)
1022
Dan Willemsenb3454ab2016-09-28 17:34:58 -07001023 }
Dan Willemsenb40aab62016-04-20 14:21:14 -07001024 } else {
Jeff Gaston7b6118b2017-10-04 21:07:42 +00001025 ctx.ModuleErrorf("module %q is not a genrule", name)
Dan Willemsenb40aab62016-04-20 14:21:14 -07001026 }
Dan Willemsenc77a0b32017-09-18 23:19:12 -07001027 case linkerScriptDepTag:
Jeff Gaston7b6118b2017-10-04 21:07:42 +00001028 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
Dan Willemsenc77a0b32017-09-18 23:19:12 -07001029 files := genRule.GeneratedSourceFiles()
1030 if len(files) == 1 {
1031 depPaths.LinkerScript = android.OptionalPathForPath(files[0])
1032 } else if len(files) > 1 {
Jeff Gaston7b6118b2017-10-04 21:07:42 +00001033 ctx.ModuleErrorf("module %q can only generate a single file if used for a linker script", name)
Dan Willemsenc77a0b32017-09-18 23:19:12 -07001034 }
1035 } else {
Jeff Gaston7b6118b2017-10-04 21:07:42 +00001036 ctx.ModuleErrorf("module %q is not a genrule", name)
Dan Willemsenc77a0b32017-09-18 23:19:12 -07001037 }
Dan Willemsenb40aab62016-04-20 14:21:14 -07001038 default:
Jeff Gaston7b6118b2017-10-04 21:07:42 +00001039 ctx.ModuleErrorf("depends on non-cc module %q", name)
Colin Crossca860ac2016-01-04 14:34:37 -08001040 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001041 return
1042 }
1043
Jeff Gaston7b6118b2017-10-04 21:07:42 +00001044 if !a.Enabled() {
Colin Crossa8f5e9a2016-12-13 12:51:11 -08001045 if ctx.AConfig().AllowMissingDependencies() {
Jeff Gaston7b6118b2017-10-04 21:07:42 +00001046 ctx.AddMissingDependencies([]string{name})
Colin Crossa8f5e9a2016-12-13 12:51:11 -08001047 } else {
Jeff Gaston7b6118b2017-10-04 21:07:42 +00001048 ctx.ModuleErrorf("depends on disabled module %q", name)
Colin Crossa8f5e9a2016-12-13 12:51:11 -08001049 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001050 return
1051 }
Jeff Gaston7b6118b2017-10-04 21:07:42 +00001052
1053 if a.Target().Os != ctx.Os() {
1054 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), name)
Colin Crossa1ad8d12016-06-01 17:09:44 -07001055 return
1056 }
1057
Jeff Gaston7b6118b2017-10-04 21:07:42 +00001058 if a.Target().Arch.ArchType != ctx.Arch().ArchType {
1059 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), name)
1060 return
1061 }
1062
1063 if tag == reuseObjTag {
1064 if l, ok := cc.compiler.(libraryInterface); ok {
Colin Crossbbc9f4d2017-05-03 16:24:55 -07001065 objs, flags, deps := l.reuseObjs()
Colin Cross10d22312017-05-03 11:01:58 -07001066 depPaths.Objs = depPaths.Objs.Append(objs)
1067 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Colin Crossbbc9f4d2017-05-03 16:24:55 -07001068 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps, deps...)
Colin Crossbba99042016-11-23 15:45:05 -08001069 return
1070 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001071 }
Jeff Gaston7b6118b2017-10-04 21:07:42 +00001072
1073 if t, ok := tag.(dependencyTag); ok && t.library {
1074 if i, ok := cc.linker.(exportedFlagsProducer); ok {
Dan Willemsen76f08272016-07-09 00:14:08 -07001075 flags := i.exportedFlags()
Dan Willemsen847dcc72016-09-29 12:13:36 -07001076 deps := i.exportedFlagsDeps()
Dan Willemsen76f08272016-07-09 00:14:08 -07001077 depPaths.Flags = append(depPaths.Flags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -07001078 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders, deps...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001079
1080 if t.reexportFlags {
Dan Willemsen76f08272016-07-09 00:14:08 -07001081 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -07001082 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps, deps...)
Jayant Chowdhary715cac32017-04-20 06:53:59 -07001083 // 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 -07001084 // Re-exported shared library headers must be included as well since they can help us with type information
1085 // about template instantiations (instantiated from their headers).
1086 c.sabi.Properties.ReexportedIncludeFlags = append(c.sabi.Properties.ReexportedIncludeFlags, flags...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001087 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001088 }
Dan Willemsena96ff642016-06-07 12:34:45 -07001089
Jeff Gaston7b6118b2017-10-04 21:07:42 +00001090 checkLinkType(c, cc)
Colin Crossc99deeb2016-04-11 15:06:20 -07001091 }
1092
Colin Cross26c34ed2016-09-30 17:10:16 -07001093 var ptr *android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -07001094 var depPtr *android.Paths
Colin Crossc99deeb2016-04-11 15:06:20 -07001095
Jeff Gaston7b6118b2017-10-04 21:07:42 +00001096 linkFile := cc.outputFile
Colin Cross26c34ed2016-09-30 17:10:16 -07001097 depFile := android.OptionalPath{}
1098
Jeff Gaston7b6118b2017-10-04 21:07:42 +00001099 switch tag {
Dan Albert914449f2016-06-17 16:45:24 -07001100 case ndkStubDepTag, sharedDepTag, sharedExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001101 ptr = &depPaths.SharedLibs
1102 depPtr = &depPaths.SharedLibsDeps
Jeff Gaston7b6118b2017-10-04 21:07:42 +00001103 depFile = cc.linker.(libraryInterface).toc()
Dan Albert914449f2016-06-17 16:45:24 -07001104 case lateSharedDepTag, ndkLateStubDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001105 ptr = &depPaths.LateSharedLibs
1106 depPtr = &depPaths.LateSharedLibsDeps
Jeff Gaston7b6118b2017-10-04 21:07:42 +00001107 depFile = cc.linker.(libraryInterface).toc()
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001108 case staticDepTag, staticExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001109 ptr = &depPaths.StaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -07001110 case lateStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001111 ptr = &depPaths.LateStaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -07001112 case wholeStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001113 ptr = &depPaths.WholeStaticLibs
Jeff Gaston7b6118b2017-10-04 21:07:42 +00001114 staticLib, ok := cc.linker.(libraryInterface)
Colin Crossb916a382016-07-29 17:28:03 -07001115 if !ok || !staticLib.static() {
Jeff Gaston7b6118b2017-10-04 21:07:42 +00001116 ctx.ModuleErrorf("module %q not a static library", name)
Colin Crossc99deeb2016-04-11 15:06:20 -07001117 return
1118 }
1119
1120 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
Jeff Gaston7b6118b2017-10-04 21:07:42 +00001121 postfix := " (required by " + ctx.OtherModuleName(m) + ")"
Colin Crossc99deeb2016-04-11 15:06:20 -07001122 for i := range missingDeps {
1123 missingDeps[i] += postfix
1124 }
1125 ctx.AddMissingDependencies(missingDeps)
1126 }
Dan Willemsen5cb580f2016-09-26 17:33:01 -07001127 depPaths.WholeStaticLibObjs = depPaths.WholeStaticLibObjs.Append(staticLib.objs())
Colin Cross5950f382016-12-13 12:50:57 -08001128 case headerDepTag:
1129 // Nothing
Colin Crossc99deeb2016-04-11 15:06:20 -07001130 case objDepTag:
Dan Willemsen5cb580f2016-09-26 17:33:01 -07001131 depPaths.Objs.objFiles = append(depPaths.Objs.objFiles, linkFile.Path())
Colin Crossc99deeb2016-04-11 15:06:20 -07001132 case crtBeginDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001133 depPaths.CrtBegin = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -07001134 case crtEndDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001135 depPaths.CrtEnd = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -07001136 }
1137
Jeff Gaston7b6118b2017-10-04 21:07:42 +00001138 switch tag {
Dan Willemsen581341d2017-02-09 16:16:31 -08001139 case staticDepTag, staticExportDepTag, lateStaticDepTag:
Jeff Gaston7b6118b2017-10-04 21:07:42 +00001140 staticLib, ok := cc.linker.(libraryInterface)
Dan Willemsen581341d2017-02-09 16:16:31 -08001141 if !ok || !staticLib.static() {
Jeff Gaston7b6118b2017-10-04 21:07:42 +00001142 ctx.ModuleErrorf("module %q not a static library", name)
Dan Willemsen581341d2017-02-09 16:16:31 -08001143 return
1144 }
1145
1146 // When combining coverage files for shared libraries and executables, coverage files
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001147 // in static libraries act as if they were whole static libraries. The same goes for
1148 // source based Abi dump files.
Dan Willemsen581341d2017-02-09 16:16:31 -08001149 depPaths.StaticLibObjs.coverageFiles = append(depPaths.StaticLibObjs.coverageFiles,
1150 staticLib.objs().coverageFiles...)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001151 depPaths.StaticLibObjs.sAbiDumpFiles = append(depPaths.StaticLibObjs.sAbiDumpFiles,
1152 staticLib.objs().sAbiDumpFiles...)
Dan Willemsen581341d2017-02-09 16:16:31 -08001153 }
1154
Colin Cross26c34ed2016-09-30 17:10:16 -07001155 if ptr != nil {
Colin Crossce75d2c2016-10-06 16:12:58 -07001156 if !linkFile.Valid() {
Jeff Gaston7b6118b2017-10-04 21:07:42 +00001157 ctx.ModuleErrorf("module %q missing output file", name)
Colin Crossce75d2c2016-10-06 16:12:58 -07001158 return
1159 }
Colin Cross26c34ed2016-09-30 17:10:16 -07001160 *ptr = append(*ptr, linkFile.Path())
1161 }
1162
Colin Crossc99deeb2016-04-11 15:06:20 -07001163 if depPtr != nil {
Colin Cross26c34ed2016-09-30 17:10:16 -07001164 dep := depFile
1165 if !dep.Valid() {
1166 dep = linkFile
1167 }
1168 *depPtr = append(*depPtr, dep.Path())
Colin Crossca860ac2016-01-04 14:34:37 -08001169 }
Jiyong Park27b188b2017-07-18 13:23:39 +09001170
Jeff Gaston7b6118b2017-10-04 21:07:42 +00001171 // Export the shared libs to the make world. In doing so, .vendor suffix
1172 // is added if the lib has both core and vendor variants and this module
Jiyong Park82e2bf32017-08-16 14:05:54 +09001173 // is building against vndk. This is because the vendor variant will
Jeff Gaston7b6118b2017-10-04 21:07:42 +00001174 // have .vendor suffix in its name in the make world. However, if the
1175 // lib is a vendor-only lib or this lib is not building against vndk,
1176 // then the suffix is not added.
1177 switch tag {
Jiyong Park27b188b2017-07-18 13:23:39 +09001178 case sharedDepTag, sharedExportDepTag, lateSharedDepTag:
Jeff Gaston7b6118b2017-10-04 21:07:42 +00001179 libName := strings.TrimSuffix(name, llndkLibrarySuffix)
Jiyong Park27b188b2017-07-18 13:23:39 +09001180 libName = strings.TrimPrefix(libName, "prebuilt_")
Jiyong Parkd5b18a52017-08-03 21:22:50 +09001181 isLLndk := inList(libName, llndkLibraries)
Jiyong Park82e2bf32017-08-16 14:05:54 +09001182 if c.vndk() && (cc.hasVendorVariant() || isLLndk) {
Jeff Gaston7b6118b2017-10-04 21:07:42 +00001183 libName += vendorSuffix
Jiyong Park27b188b2017-07-18 13:23:39 +09001184 }
1185 // Note: the order of libs in this list is not important because
Jeff Gaston7b6118b2017-10-04 21:07:42 +00001186 // they merely serve as dependencies in the make world and do not
1187 // affect this lib itself.
1188 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, libName)
Jiyong Park27b188b2017-07-18 13:23:39 +09001189 }
Colin Crossca860ac2016-01-04 14:34:37 -08001190 })
1191
Colin Crossdd84e052017-05-17 13:44:16 -07001192 // Dedup exported flags from dependencies
1193 depPaths.Flags = firstUniqueElements(depPaths.Flags)
Dan Willemsenfe92c962017-08-29 12:28:37 -07001194 depPaths.GeneratedHeaders = android.FirstUniquePaths(depPaths.GeneratedHeaders)
1195 depPaths.ReexportedFlags = firstUniqueElements(depPaths.ReexportedFlags)
1196 depPaths.ReexportedFlagsDeps = android.FirstUniquePaths(depPaths.ReexportedFlagsDeps)
1197
1198 if c.sabi != nil {
1199 c.sabi.Properties.ReexportedIncludeFlags = firstUniqueElements(c.sabi.Properties.ReexportedIncludeFlags)
1200 }
Colin Crossdd84e052017-05-17 13:44:16 -07001201
Colin Crossca860ac2016-01-04 14:34:37 -08001202 return depPaths
1203}
1204
1205func (c *Module) InstallInData() bool {
1206 if c.installer == nil {
1207 return false
1208 }
Vishwath Mohan1dd88392017-03-29 22:00:18 -07001209 return c.installer.inData()
1210}
1211
1212func (c *Module) InstallInSanitizerDir() bool {
1213 if c.installer == nil {
1214 return false
1215 }
1216 if c.sanitize != nil && c.sanitize.inSanitizerDir() {
Colin Cross94610402016-08-29 13:41:32 -07001217 return true
1218 }
Vishwath Mohan1dd88392017-03-29 22:00:18 -07001219 return c.installer.inSanitizerDir()
Colin Crossca860ac2016-01-04 14:34:37 -08001220}
1221
Dan Willemsen4aa75ca2016-09-28 16:18:03 -07001222func (c *Module) HostToolPath() android.OptionalPath {
1223 if c.installer == nil {
1224 return android.OptionalPath{}
1225 }
1226 return c.installer.hostToolPath()
1227}
1228
Nan Zhangd4e641b2017-07-12 12:55:28 -07001229func (c *Module) IntermPathForModuleOut() android.OptionalPath {
1230 return c.outputFile
1231}
1232
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001233func (c *Module) Srcs() android.Paths {
1234 if c.outputFile.Valid() {
1235 return android.Paths{c.outputFile.Path()}
1236 }
1237 return android.Paths{}
1238}
1239
Colin Cross2ba19d92015-05-07 15:44:20 -07001240//
Colin Crosscfad1192015-11-02 16:43:11 -08001241// Defaults
1242//
Colin Crossca860ac2016-01-04 14:34:37 -08001243type Defaults struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001244 android.ModuleBase
Colin Cross1f44a3a2017-07-07 14:33:33 -07001245 android.DefaultsModuleBase
Colin Crosscfad1192015-11-02 16:43:11 -08001246}
1247
Colin Cross635c3b02016-05-18 15:37:25 -07001248func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -08001249}
1250
Colin Cross1e676be2016-10-12 14:38:15 -07001251func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
1252}
1253
Colin Cross36242852017-06-23 15:06:31 -07001254func defaultsFactory() android.Module {
Colin Crosse1d764e2016-08-18 14:18:32 -07001255 return DefaultsFactory()
1256}
1257
Colin Cross36242852017-06-23 15:06:31 -07001258func DefaultsFactory(props ...interface{}) android.Module {
Colin Crossca860ac2016-01-04 14:34:37 -08001259 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -08001260
Colin Cross36242852017-06-23 15:06:31 -07001261 module.AddProperties(props...)
1262 module.AddProperties(
Colin Crossca860ac2016-01-04 14:34:37 -08001263 &BaseProperties{},
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001264 &VendorProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001265 &BaseCompilerProperties{},
1266 &BaseLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -07001267 &LibraryProperties{},
Colin Cross919281a2016-04-05 16:42:05 -07001268 &FlagExporterProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001269 &BinaryLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -07001270 &TestProperties{},
1271 &TestBinaryProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001272 &UnusedProperties{},
1273 &StlProperties{},
Colin Cross16b23492016-01-06 14:41:07 -08001274 &SanitizeProperties{},
Colin Cross665dce92016-04-28 14:50:03 -07001275 &StripProperties{},
Dan Willemsen7424d612016-09-01 13:45:39 -07001276 &InstallerProperties{},
Dan Willemsena03cf6d2016-09-26 15:45:04 -07001277 &TidyProperties{},
Dan Willemsen581341d2017-02-09 16:16:31 -08001278 &CoverageProperties{},
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001279 &SAbiProperties{},
Justin Yun4b2382f2017-07-26 14:22:10 +09001280 &VndkProperties{},
Stephen Craneba090d12017-05-09 15:44:35 -07001281 &LTOProperties{},
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -07001282 &PgoProperties{},
Colin Crosse1d764e2016-08-18 14:18:32 -07001283 )
Colin Crosscfad1192015-11-02 16:43:11 -08001284
Colin Cross1f44a3a2017-07-07 14:33:33 -07001285 android.InitDefaultsModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001286
1287 return module
Colin Crosscfad1192015-11-02 16:43:11 -08001288}
1289
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001290const (
1291 // coreMode is the variant used for framework-private libraries, or
1292 // SDK libraries. (which framework-private libraries can use)
1293 coreMode = "core"
1294
1295 // vendorMode is the variant used for /vendor code that compiles
1296 // against the VNDK.
1297 vendorMode = "vendor"
1298)
1299
Jiyong Park6a43f042017-10-12 23:05:00 +09001300func squashVendorSrcs(m *Module) {
1301 if lib, ok := m.compiler.(*libraryDecorator); ok {
1302 lib.baseCompiler.Properties.Srcs = append(lib.baseCompiler.Properties.Srcs,
1303 lib.baseCompiler.Properties.Target.Vendor.Srcs...)
1304
1305 lib.baseCompiler.Properties.Exclude_srcs = append(lib.baseCompiler.Properties.Exclude_srcs,
1306 lib.baseCompiler.Properties.Target.Vendor.Exclude_srcs...)
1307 }
1308}
1309
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001310func vendorMutator(mctx android.BottomUpMutatorContext) {
1311 if mctx.Os() != android.Android {
1312 return
1313 }
1314
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001315 if genrule, ok := mctx.Module().(*genrule.Module); ok {
1316 if props, ok := genrule.Extra.(*VendorProperties); ok {
1317 if !mctx.DeviceConfig().CompileVndk() {
1318 mctx.CreateVariations(coreMode)
1319 } else if Bool(props.Vendor_available) {
1320 mctx.CreateVariations(coreMode, vendorMode)
1321 } else if mctx.Vendor() {
1322 mctx.CreateVariations(vendorMode)
1323 } else {
1324 mctx.CreateVariations(coreMode)
1325 }
1326 }
1327 }
1328
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001329 m, ok := mctx.Module().(*Module)
1330 if !ok {
1331 return
1332 }
1333
1334 // Sanity check
Jiyong Park82e2bf32017-08-16 14:05:54 +09001335 if m.VendorProperties.Vendor_available != nil && mctx.Vendor() {
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001336 mctx.PropertyErrorf("vendor_available",
1337 "doesn't make sense at the same time as `vendor: true` or `proprietary: true`")
1338 return
1339 }
Justin Yun8effde42017-06-23 19:24:43 +09001340 if vndk := m.vndkdep; vndk != nil {
Jiyong Park82e2bf32017-08-16 14:05:54 +09001341 if vndk.isVndk() && m.VendorProperties.Vendor_available == nil {
Justin Yun8effde42017-06-23 19:24:43 +09001342 mctx.PropertyErrorf("vndk",
Jiyong Park82e2bf32017-08-16 14:05:54 +09001343 "vendor_available must be set to either true or false when `vndk: {enabled: true}`")
Justin Yun8effde42017-06-23 19:24:43 +09001344 return
1345 }
1346 if !vndk.isVndk() && vndk.isVndkSp() {
1347 mctx.PropertyErrorf("vndk",
1348 "must set `enabled: true` to set `support_system_process: true`")
1349 return
1350 }
1351 }
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001352
1353 if !mctx.DeviceConfig().CompileVndk() {
1354 // If the device isn't compiling against the VNDK, we always
1355 // use the core mode.
1356 mctx.CreateVariations(coreMode)
1357 } else if _, ok := m.linker.(*llndkStubDecorator); ok {
1358 // LL-NDK stubs only exist in the vendor variant, since the
1359 // real libraries will be used in the core variant.
1360 mctx.CreateVariations(vendorMode)
Jiyong Park82e2bf32017-08-16 14:05:54 +09001361 } else if m.hasVendorVariant() {
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001362 // This will be available in both /system and /vendor
Justin Yun8effde42017-06-23 19:24:43 +09001363 // or a /system directory that is available to vendor.
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001364 mod := mctx.CreateVariations(coreMode, vendorMode)
Jiyong Park6a43f042017-10-12 23:05:00 +09001365 vendor := mod[1].(*Module)
1366 vendor.Properties.UseVndk = true
1367 squashVendorSrcs(vendor)
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001368 } else if mctx.Vendor() && m.Properties.Sdk_version == "" {
1369 // This will be available in /vendor only
1370 mod := mctx.CreateVariations(vendorMode)
Jiyong Park6a43f042017-10-12 23:05:00 +09001371 vendor := mod[0].(*Module)
1372 vendor.Properties.UseVndk = true
1373 squashVendorSrcs(vendor)
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001374 } else {
1375 // This is either in /system (or similar: /data), or is a
1376 // modules built with the NDK. Modules built with the NDK
1377 // will be restricted using the existing link type checks.
1378 mctx.CreateVariations(coreMode)
1379 }
1380}
1381
Colin Crossdd84e052017-05-17 13:44:16 -07001382// firstUniqueElements returns all unique elements of a slice, keeping the first copy of each
1383// modifies the slice contents in place, and returns a subslice of the original slice
1384func firstUniqueElements(list []string) []string {
1385 k := 0
1386outer:
1387 for i := 0; i < len(list); i++ {
1388 for j := 0; j < k; j++ {
1389 if list[i] == list[j] {
1390 continue outer
1391 }
1392 }
1393 list[k] = list[i]
1394 k++
1395 }
1396 return list[:k]
1397}
1398
Jeff Gaston7b6118b2017-10-04 21:07:42 +00001399// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
1400// modifies the slice contents in place, and returns a subslice of the original slice
Colin Cross74d1ec02015-04-28 13:30:13 -07001401func lastUniqueElements(list []string) []string {
1402 totalSkip := 0
1403 for i := len(list) - 1; i >= totalSkip; i-- {
1404 skip := 0
1405 for j := i - 1; j >= totalSkip; j-- {
1406 if list[i] == list[j] {
1407 skip++
1408 } else {
1409 list[j+skip] = list[j]
1410 }
1411 }
1412 totalSkip += skip
1413 }
1414 return list[totalSkip:]
1415}
Colin Cross06a931b2015-10-28 17:23:31 -07001416
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -07001417func getCurrentNdkPrebuiltVersion(ctx DepsContext) string {
1418 if ctx.AConfig().PlatformSdkVersionInt() > config.NdkMaxPrebuiltVersionInt {
1419 return strconv.Itoa(config.NdkMaxPrebuiltVersionInt)
1420 }
1421 return ctx.AConfig().PlatformSdkVersion()
1422}
1423
Colin Cross06a931b2015-10-28 17:23:31 -07001424var Bool = proptools.Bool