blob: 2fafaa294224d679312bf8aded6216d78331f8de [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
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700200 useSdk() bool
Colin Crossca860ac2016-01-04 14:34:37 -0800201 sdkVersion() string
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700202 useVndk() 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 Gastonaf3cc2d2017-09-27 17:01:44 -0700385func (c *Module) useVndk() 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 Gastonaf3cc2d2017-09-27 17:01:44 -0700417func (ctx *moduleContext) InstallOnVendorPartition() bool {
418 return ctx.ModuleContext.InstallOnVendorPartition() || (ctx.mod.useVndk() && !ctx.mod.isVndk())
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700419}
420
Colin Crossca860ac2016-01-04 14:34:37 -0800421type moduleContextImpl struct {
422 mod *Module
423 ctx BaseModuleContext
424}
425
Colin Crossca860ac2016-01-04 14:34:37 -0800426func (ctx *moduleContextImpl) clang() bool {
427 return ctx.mod.clang(ctx.ctx)
428}
429
Colin Crossb98c8b02016-07-29 13:44:28 -0700430func (ctx *moduleContextImpl) toolchain() config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800431 return ctx.mod.toolchain(ctx.ctx)
432}
433
434func (ctx *moduleContextImpl) static() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700435 if static, ok := ctx.mod.linker.(interface {
436 static() bool
437 }); ok {
438 return static.static()
Colin Crossca860ac2016-01-04 14:34:37 -0800439 }
Colin Crossb916a382016-07-29 17:28:03 -0700440 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800441}
442
443func (ctx *moduleContextImpl) staticBinary() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700444 if static, ok := ctx.mod.linker.(interface {
445 staticBinary() bool
446 }); ok {
447 return static.staticBinary()
Colin Crossca860ac2016-01-04 14:34:37 -0800448 }
Colin Crossb916a382016-07-29 17:28:03 -0700449 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800450}
451
452func (ctx *moduleContextImpl) noDefaultCompilerFlags() bool {
453 return Bool(ctx.mod.Properties.No_default_compiler_flags)
454}
455
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700456func (ctx *moduleContextImpl) useSdk() bool {
457 if ctx.ctx.Device() && !ctx.useVndk() {
Dan Willemsena96ff642016-06-07 12:34:45 -0700458 return ctx.mod.Properties.Sdk_version != ""
459 }
460 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800461}
462
463func (ctx *moduleContextImpl) sdkVersion() string {
Dan Willemsena96ff642016-06-07 12:34:45 -0700464 if ctx.ctx.Device() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700465 if ctx.useVndk() {
Dan Willemsen11b26142017-03-19 18:30:37 -0700466 return "current"
Dan Willemsend2ede872016-11-18 14:54:24 -0800467 } else {
468 return ctx.mod.Properties.Sdk_version
469 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700470 }
471 return ""
Colin Crossca860ac2016-01-04 14:34:37 -0800472}
473
Justin Yun8effde42017-06-23 19:24:43 +0900474func (ctx *moduleContextImpl) isVndk() bool {
475 return ctx.mod.isVndk()
476}
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700477func (ctx *moduleContextImpl) useVndk() bool {
478 return ctx.mod.useVndk()
479}
Justin Yun8effde42017-06-23 19:24:43 +0900480
481func (ctx *moduleContextImpl) isVndkSp() bool {
482 if vndk := ctx.mod.vndkdep; vndk != nil {
483 return vndk.isVndkSp()
484 }
485 return false
486}
487
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800488// Create source abi dumps if the module belongs to the list of VndkLibraries.
489func (ctx *moduleContextImpl) createVndkSourceAbiDump() bool {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700490 return ctx.ctx.Device() && ((ctx.useVndk() && ctx.isVndk()) || inList(ctx.baseModuleName(), llndkLibraries))
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800491}
492
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700493func (ctx *moduleContextImpl) selectedStl() string {
494 if stl := ctx.mod.stl; stl != nil {
495 return stl.Properties.SelectedStl
496 }
497 return ""
498}
499
Colin Crossce75d2c2016-10-06 16:12:58 -0700500func (ctx *moduleContextImpl) baseModuleName() string {
501 return ctx.mod.ModuleBase.BaseModuleName()
502}
503
Colin Cross635c3b02016-05-18 15:37:25 -0700504func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800505 return &Module{
506 hod: hod,
507 multilib: multilib,
508 }
509}
510
Colin Cross635c3b02016-05-18 15:37:25 -0700511func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800512 module := newBaseModule(hod, multilib)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700513 module.features = []feature{
514 &tidyFeature{},
515 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700516 module.stl = &stl{}
Colin Cross16b23492016-01-06 14:41:07 -0800517 module.sanitize = &sanitize{}
Dan Willemsen581341d2017-02-09 16:16:31 -0800518 module.coverage = &coverage{}
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800519 module.sabi = &sabi{}
Justin Yun8effde42017-06-23 19:24:43 +0900520 module.vndkdep = &vndkdep{}
Stephen Craneba090d12017-05-09 15:44:35 -0700521 module.lto = &lto{}
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700522 module.pgo = &pgo{}
Colin Crossca860ac2016-01-04 14:34:37 -0800523 return module
524}
525
Colin Crossce75d2c2016-10-06 16:12:58 -0700526func (c *Module) Prebuilt() *android.Prebuilt {
527 if p, ok := c.linker.(prebuiltLinkerInterface); ok {
528 return p.prebuilt()
529 }
530 return nil
531}
532
533func (c *Module) Name() string {
534 name := c.ModuleBase.Name()
Dan Willemsen01a90592017-04-07 15:21:13 -0700535 if p, ok := c.linker.(interface {
536 Name(string) string
537 }); ok {
Colin Crossce75d2c2016-10-06 16:12:58 -0700538 name = p.Name(name)
539 }
540 return name
541}
542
Colin Cross635c3b02016-05-18 15:37:25 -0700543func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700544
Colin Crossca860ac2016-01-04 14:34:37 -0800545 ctx := &moduleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700546 ModuleContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800547 moduleContextImpl: moduleContextImpl{
548 mod: c,
549 },
550 }
551 ctx.ctx = ctx
552
553 flags := Flags{
554 Toolchain: c.toolchain(ctx),
555 Clang: c.clang(ctx),
556 }
Colin Crossca860ac2016-01-04 14:34:37 -0800557 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700558 flags = c.compiler.compilerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800559 }
560 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700561 flags = c.linker.linkerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800562 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700563 if c.stl != nil {
564 flags = c.stl.flags(ctx, flags)
565 }
Colin Cross16b23492016-01-06 14:41:07 -0800566 if c.sanitize != nil {
567 flags = c.sanitize.flags(ctx, flags)
568 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800569 if c.coverage != nil {
570 flags = c.coverage.flags(ctx, flags)
571 }
Stephen Craneba090d12017-05-09 15:44:35 -0700572 if c.lto != nil {
573 flags = c.lto.flags(ctx, flags)
574 }
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700575 if c.pgo != nil {
576 flags = c.pgo.flags(ctx, flags)
577 }
Colin Crossca860ac2016-01-04 14:34:37 -0800578 for _, feature := range c.features {
579 flags = feature.flags(ctx, flags)
580 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800581 if ctx.Failed() {
582 return
583 }
584
Colin Crossb98c8b02016-07-29 13:44:28 -0700585 flags.CFlags, _ = filterList(flags.CFlags, config.IllegalFlags)
586 flags.CppFlags, _ = filterList(flags.CppFlags, config.IllegalFlags)
587 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, config.IllegalFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800588
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800589 deps := c.depsToPaths(ctx)
590 if ctx.Failed() {
591 return
592 }
593 flags.GlobalFlags = append(flags.GlobalFlags, deps.Flags...)
594 c.flags = flags
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700595 // We need access to all the flags seen by a source file.
596 if c.sabi != nil {
597 flags = c.sabi.flags(ctx, flags)
598 }
Colin Crossca860ac2016-01-04 14:34:37 -0800599 // Optimization to reduce size of build.ninja
600 // Replace the long list of flags for each file with a module-local variable
601 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
602 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
603 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
604 flags.CFlags = []string{"$cflags"}
605 flags.CppFlags = []string{"$cppflags"}
606 flags.AsFlags = []string{"$asflags"}
607
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700608 var objs Objects
Colin Crossca860ac2016-01-04 14:34:37 -0800609 if c.compiler != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700610 objs = c.compiler.compile(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -0800611 if ctx.Failed() {
612 return
613 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800614 }
615
Colin Crossca860ac2016-01-04 14:34:37 -0800616 if c.linker != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700617 outputFile := c.linker.link(ctx, flags, deps, objs)
Colin Crossca860ac2016-01-04 14:34:37 -0800618 if ctx.Failed() {
619 return
620 }
Colin Cross635c3b02016-05-18 15:37:25 -0700621 c.outputFile = android.OptionalPathForPath(outputFile)
Colin Crossce75d2c2016-10-06 16:12:58 -0700622 }
Colin Cross5049f022015-03-18 13:28:46 -0700623
Colin Crossce75d2c2016-10-06 16:12:58 -0700624 if c.installer != nil && !c.Properties.PreventInstall && c.outputFile.Valid() {
625 c.installer.install(ctx, c.outputFile.Path())
626 if ctx.Failed() {
627 return
Colin Crossca860ac2016-01-04 14:34:37 -0800628 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700629 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800630}
631
Colin Crossb98c8b02016-07-29 13:44:28 -0700632func (c *Module) toolchain(ctx BaseModuleContext) config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800633 if c.cachedToolchain == nil {
Colin Crossb98c8b02016-07-29 13:44:28 -0700634 c.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
Colin Cross3f40fa42015-01-30 17:27:36 -0800635 }
Colin Crossca860ac2016-01-04 14:34:37 -0800636 return c.cachedToolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800637}
638
Colin Crossca860ac2016-01-04 14:34:37 -0800639func (c *Module) begin(ctx BaseModuleContext) {
640 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700641 c.compiler.compilerInit(ctx)
Colin Cross21b9a242015-03-24 14:15:58 -0700642 }
Colin Crossca860ac2016-01-04 14:34:37 -0800643 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700644 c.linker.linkerInit(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800645 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700646 if c.stl != nil {
647 c.stl.begin(ctx)
648 }
Colin Cross16b23492016-01-06 14:41:07 -0800649 if c.sanitize != nil {
650 c.sanitize.begin(ctx)
651 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800652 if c.coverage != nil {
653 c.coverage.begin(ctx)
654 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800655 if c.sabi != nil {
656 c.sabi.begin(ctx)
657 }
Justin Yun8effde42017-06-23 19:24:43 +0900658 if c.vndkdep != nil {
659 c.vndkdep.begin(ctx)
660 }
Stephen Craneba090d12017-05-09 15:44:35 -0700661 if c.lto != nil {
662 c.lto.begin(ctx)
663 }
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700664 if c.pgo != nil {
665 c.pgo.begin(ctx)
666 }
Colin Crossca860ac2016-01-04 14:34:37 -0800667 for _, feature := range c.features {
668 feature.begin(ctx)
669 }
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700670 if ctx.useSdk() {
Dan Albertf5415d72017-08-17 16:19:59 -0700671 version, err := normalizeNdkApiLevel(ctx, ctx.sdkVersion(), ctx.Arch())
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700672 if err != nil {
673 ctx.PropertyErrorf("sdk_version", err.Error())
674 }
Dan Albert90f7a4d2016-11-08 14:34:24 -0800675 c.Properties.Sdk_version = version
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700676 }
Colin Crossca860ac2016-01-04 14:34:37 -0800677}
678
Colin Cross37047f12016-12-13 17:06:13 -0800679func (c *Module) deps(ctx DepsContext) Deps {
Colin Crossc99deeb2016-04-11 15:06:20 -0700680 deps := Deps{}
681
682 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700683 deps = c.compiler.compilerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700684 }
Pirama Arumuga Nainar49b53d52017-10-04 16:47:29 -0700685 // Add the PGO dependency (the clang_rt.profile runtime library), which
686 // sometimes depends on symbols from libgcc, before libgcc gets added
687 // in linkerDeps().
688 if c.pgo != nil {
689 deps = c.pgo.deps(ctx, deps)
690 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700691 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700692 deps = c.linker.linkerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700693 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700694 if c.stl != nil {
695 deps = c.stl.deps(ctx, deps)
696 }
Colin Cross16b23492016-01-06 14:41:07 -0800697 if c.sanitize != nil {
698 deps = c.sanitize.deps(ctx, deps)
699 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800700 if c.coverage != nil {
701 deps = c.coverage.deps(ctx, deps)
702 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800703 if c.sabi != nil {
704 deps = c.sabi.deps(ctx, deps)
705 }
Justin Yun8effde42017-06-23 19:24:43 +0900706 if c.vndkdep != nil {
707 deps = c.vndkdep.deps(ctx, deps)
708 }
Stephen Craneba090d12017-05-09 15:44:35 -0700709 if c.lto != nil {
710 deps = c.lto.deps(ctx, deps)
711 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700712 for _, feature := range c.features {
713 deps = feature.deps(ctx, deps)
714 }
715
716 deps.WholeStaticLibs = lastUniqueElements(deps.WholeStaticLibs)
717 deps.StaticLibs = lastUniqueElements(deps.StaticLibs)
718 deps.LateStaticLibs = lastUniqueElements(deps.LateStaticLibs)
719 deps.SharedLibs = lastUniqueElements(deps.SharedLibs)
720 deps.LateSharedLibs = lastUniqueElements(deps.LateSharedLibs)
Colin Cross5950f382016-12-13 12:50:57 -0800721 deps.HeaderLibs = lastUniqueElements(deps.HeaderLibs)
Colin Crossc99deeb2016-04-11 15:06:20 -0700722
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700723 for _, lib := range deps.ReexportSharedLibHeaders {
724 if !inList(lib, deps.SharedLibs) {
725 ctx.PropertyErrorf("export_shared_lib_headers", "Shared library not in shared_libs: '%s'", lib)
726 }
727 }
728
729 for _, lib := range deps.ReexportStaticLibHeaders {
730 if !inList(lib, deps.StaticLibs) {
731 ctx.PropertyErrorf("export_static_lib_headers", "Static library not in static_libs: '%s'", lib)
732 }
733 }
734
Colin Cross5950f382016-12-13 12:50:57 -0800735 for _, lib := range deps.ReexportHeaderLibHeaders {
736 if !inList(lib, deps.HeaderLibs) {
737 ctx.PropertyErrorf("export_header_lib_headers", "Header library not in header_libs: '%s'", lib)
738 }
739 }
740
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700741 for _, gen := range deps.ReexportGeneratedHeaders {
742 if !inList(gen, deps.GeneratedHeaders) {
743 ctx.PropertyErrorf("export_generated_headers", "Generated header module not in generated_headers: '%s'", gen)
744 }
745 }
746
Colin Crossc99deeb2016-04-11 15:06:20 -0700747 return deps
748}
749
Dan Albert7e9d2952016-08-04 13:02:36 -0700750func (c *Module) beginMutator(actx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800751 ctx := &baseModuleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700752 BaseContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800753 moduleContextImpl: moduleContextImpl{
754 mod: c,
755 },
756 }
757 ctx.ctx = ctx
758
Colin Crossca860ac2016-01-04 14:34:37 -0800759 c.begin(ctx)
Dan Albert7e9d2952016-08-04 13:02:36 -0700760}
761
Colin Cross1e676be2016-10-12 14:38:15 -0700762func (c *Module) DepsMutator(actx android.BottomUpMutatorContext) {
763 if !c.Enabled() {
764 return
765 }
766
Colin Cross37047f12016-12-13 17:06:13 -0800767 ctx := &depsContext{
768 BottomUpMutatorContext: actx,
Dan Albert7e9d2952016-08-04 13:02:36 -0700769 moduleContextImpl: moduleContextImpl{
770 mod: c,
771 },
772 }
773 ctx.ctx = ctx
Colin Crossca860ac2016-01-04 14:34:37 -0800774
Colin Crossc99deeb2016-04-11 15:06:20 -0700775 deps := c.deps(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800776
Dan Albert914449f2016-06-17 16:45:24 -0700777 variantNdkLibs := []string{}
778 variantLateNdkLibs := []string{}
Dan Willemsenb916b802017-03-19 13:44:32 -0700779 if ctx.Os() == android.Android {
Dan Albert914449f2016-06-17 16:45:24 -0700780 version := ctx.sdkVersion()
Dan Willemsen72d39932016-07-08 23:23:48 -0700781
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700782 // rewriteNdkLibs takes a list of names of shared libraries and scans it for three types
783 // of names:
Dan Albert914449f2016-06-17 16:45:24 -0700784 //
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700785 // 1. Name of an NDK library that refers to a prebuilt module.
786 // For each of these, it adds the name of the prebuilt module (which will be in
787 // prebuilts/ndk) to the list of nonvariant libs.
788 // 2. Name of an NDK library that refers to an ndk_library module.
789 // For each of these, it adds the name of the ndk_library module to the list of
790 // variant libs.
791 // 3. Anything else (so anything that isn't an NDK library).
792 // It adds these to the nonvariantLibs list.
Dan Albert914449f2016-06-17 16:45:24 -0700793 //
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700794 // The caller can then know to add the variantLibs dependencies differently from the
795 // nonvariantLibs
796 rewriteNdkLibs := func(list []string) (nonvariantLibs []string, variantLibs []string) {
797 variantLibs = []string{}
798 nonvariantLibs = []string{}
Dan Albert914449f2016-06-17 16:45:24 -0700799 for _, entry := range list {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700800 if ctx.useSdk() && inList(entry, ndkPrebuiltSharedLibraries) {
Dan Albert914449f2016-06-17 16:45:24 -0700801 if !inList(entry, ndkMigratedLibs) {
802 nonvariantLibs = append(nonvariantLibs, entry+".ndk."+version)
803 } else {
804 variantLibs = append(variantLibs, entry+ndkLibrarySuffix)
805 }
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700806 } else if ctx.useVndk() && inList(entry, llndkLibraries) {
Dan Willemsenb916b802017-03-19 13:44:32 -0700807 nonvariantLibs = append(nonvariantLibs, entry+llndkLibrarySuffix)
Dan Albert914449f2016-06-17 16:45:24 -0700808 } else {
Dan Willemsen7cbf5f82017-03-28 00:08:30 -0700809 nonvariantLibs = append(nonvariantLibs, entry)
Dan Willemsen72d39932016-07-08 23:23:48 -0700810 }
811 }
Dan Albert914449f2016-06-17 16:45:24 -0700812 return nonvariantLibs, variantLibs
Dan Willemsen72d39932016-07-08 23:23:48 -0700813 }
814
Dan Albert914449f2016-06-17 16:45:24 -0700815 deps.SharedLibs, variantNdkLibs = rewriteNdkLibs(deps.SharedLibs)
816 deps.LateSharedLibs, variantLateNdkLibs = rewriteNdkLibs(deps.LateSharedLibs)
Jiyong Park4c35af02017-07-05 13:41:55 +0900817 deps.ReexportSharedLibHeaders, _ = rewriteNdkLibs(deps.ReexportSharedLibHeaders)
Dan Willemsen72d39932016-07-08 23:23:48 -0700818 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700819
Colin Cross32ec36c2016-12-15 07:39:51 -0800820 for _, lib := range deps.HeaderLibs {
821 depTag := headerDepTag
822 if inList(lib, deps.ReexportHeaderLibHeaders) {
823 depTag = headerExportDepTag
824 }
825 actx.AddVariationDependencies(nil, depTag, lib)
826 }
Colin Cross5950f382016-12-13 12:50:57 -0800827
Colin Crossc99deeb2016-04-11 15:06:20 -0700828 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, wholeStaticDepTag,
829 deps.WholeStaticLibs...)
830
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700831 for _, lib := range deps.StaticLibs {
832 depTag := staticDepTag
833 if inList(lib, deps.ReexportStaticLibHeaders) {
834 depTag = staticExportDepTag
835 }
Colin Cross15a0d462016-07-14 14:49:58 -0700836 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700837 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700838
839 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, lateStaticDepTag,
840 deps.LateStaticLibs...)
841
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700842 for _, lib := range deps.SharedLibs {
843 depTag := sharedDepTag
844 if inList(lib, deps.ReexportSharedLibHeaders) {
845 depTag = sharedExportDepTag
846 }
Colin Cross15a0d462016-07-14 14:49:58 -0700847 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700848 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700849
850 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, lateSharedDepTag,
851 deps.LateSharedLibs...)
852
Colin Cross68861832016-07-08 10:41:41 -0700853 actx.AddDependency(c, genSourceDepTag, deps.GeneratedSources...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700854
855 for _, gen := range deps.GeneratedHeaders {
856 depTag := genHeaderDepTag
857 if inList(gen, deps.ReexportGeneratedHeaders) {
858 depTag = genHeaderExportDepTag
859 }
860 actx.AddDependency(c, depTag, gen)
861 }
Dan Willemsenb40aab62016-04-20 14:21:14 -0700862
Colin Cross68861832016-07-08 10:41:41 -0700863 actx.AddDependency(c, objDepTag, deps.ObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700864
865 if deps.CrtBegin != "" {
Colin Cross68861832016-07-08 10:41:41 -0700866 actx.AddDependency(c, crtBeginDepTag, deps.CrtBegin)
Colin Crossca860ac2016-01-04 14:34:37 -0800867 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700868 if deps.CrtEnd != "" {
Colin Cross68861832016-07-08 10:41:41 -0700869 actx.AddDependency(c, crtEndDepTag, deps.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700870 }
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700871 if deps.LinkerScript != "" {
872 actx.AddDependency(c, linkerScriptDepTag, deps.LinkerScript)
873 }
Dan Albert914449f2016-06-17 16:45:24 -0700874
875 version := ctx.sdkVersion()
876 actx.AddVariationDependencies([]blueprint.Variation{
877 {"ndk_api", version}, {"link", "shared"}}, ndkStubDepTag, variantNdkLibs...)
878 actx.AddVariationDependencies([]blueprint.Variation{
879 {"ndk_api", version}, {"link", "shared"}}, ndkLateStubDepTag, variantLateNdkLibs...)
Colin Cross6362e272015-10-29 15:25:03 -0700880}
Colin Cross21b9a242015-03-24 14:15:58 -0700881
Dan Albert7e9d2952016-08-04 13:02:36 -0700882func beginMutator(ctx android.BottomUpMutatorContext) {
883 if c, ok := ctx.Module().(*Module); ok && c.Enabled() {
884 c.beginMutator(ctx)
885 }
886}
887
Colin Crossca860ac2016-01-04 14:34:37 -0800888func (c *Module) clang(ctx BaseModuleContext) bool {
889 clang := Bool(c.Properties.Clang)
890
891 if c.Properties.Clang == nil {
892 if ctx.Host() {
893 clang = true
894 }
895
896 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
897 clang = true
898 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800899 }
Colin Cross28344522015-04-22 13:07:53 -0700900
Colin Crossca860ac2016-01-04 14:34:37 -0800901 if !c.toolchain(ctx).ClangSupported() {
902 clang = false
903 }
904
905 return clang
906}
907
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700908// Whether a module can link to another module, taking into
909// account NDK linking.
910func checkLinkType(ctx android.ModuleContext, from *Module, to *Module) {
911 if from.Target().Os != android.Android {
912 // Host code is not restricted
913 return
914 }
915 if from.Properties.UseVndk {
916 // Though vendor code is limited by the vendor mutator,
917 // each vendor-available module needs to check
918 // link-type for VNDK.
919 if from.vndkdep != nil {
920 from.vndkdep.vndkCheckLinkType(ctx, to)
921 }
922 return
923 }
924 if from.Properties.Sdk_version == "" {
925 // Platform code can link to anything
926 return
927 }
928 if _, ok := to.linker.(*toolchainLibraryDecorator); ok {
929 // These are always allowed
930 return
931 }
932 if _, ok := to.linker.(*ndkPrebuiltLibraryLinker); ok {
933 // These are allowed, but they don't set sdk_version
934 return
935 }
936 if _, ok := to.linker.(*ndkPrebuiltStlLinker); ok {
937 // These are allowed, but they don't set sdk_version
938 return
939 }
940 if _, ok := to.linker.(*stubDecorator); ok {
941 // These aren't real libraries, but are the stub shared libraries that are included in
942 // the NDK.
943 return
944 }
945 if to.Properties.Sdk_version == "" {
946 // NDK code linking to platform code is never okay.
947 ctx.ModuleErrorf("depends on non-NDK-built library %q",
948 ctx.OtherModuleName(to))
949 }
950
951 // At this point we know we have two NDK libraries, but we need to
952 // check that we're not linking against anything built against a higher
953 // API level, as it is only valid to link against older or equivalent
954 // APIs.
955
956 if from.Properties.Sdk_version == "current" {
957 // Current can link against anything.
958 return
959 } else if to.Properties.Sdk_version == "current" {
960 // Current can't be linked against by anything else.
961 ctx.ModuleErrorf("links %q built against newer API version %q",
962 ctx.OtherModuleName(to), "current")
963 }
964
965 fromApi, err := strconv.Atoi(from.Properties.Sdk_version)
966 if err != nil {
967 ctx.PropertyErrorf("sdk_version",
968 "Invalid sdk_version value (must be int): %q",
969 from.Properties.Sdk_version)
970 }
971 toApi, err := strconv.Atoi(to.Properties.Sdk_version)
972 if err != nil {
973 ctx.PropertyErrorf("sdk_version",
974 "Invalid sdk_version value (must be int): %q",
975 to.Properties.Sdk_version)
976 }
977
978 if toApi > fromApi {
979 ctx.ModuleErrorf("links %q built against newer API version %q",
980 ctx.OtherModuleName(to), to.Properties.Sdk_version)
981 }
982}
983
Colin Crossc99deeb2016-04-11 15:06:20 -0700984// Convert dependencies to paths. Returns a PathDeps containing paths
Colin Cross635c3b02016-05-18 15:37:25 -0700985func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
Colin Crossca860ac2016-01-04 14:34:37 -0800986 var depPaths PathDeps
Colin Crossca860ac2016-01-04 14:34:37 -0800987
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700988 ctx.VisitDirectDeps(func(dep blueprint.Module) {
989 depName := ctx.OtherModuleName(dep)
990 depTag := ctx.OtherModuleDependencyTag(dep)
Dan Albert9e10cd42016-08-03 14:12:14 -0700991
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700992 aDep, _ := dep.(android.Module)
993 if aDep == nil {
994 ctx.ModuleErrorf("module %q not an android module", depName)
Colin Crossc99deeb2016-04-11 15:06:20 -0700995 return
Colin Crossca860ac2016-01-04 14:34:37 -0800996 }
Colin Crossca860ac2016-01-04 14:34:37 -0800997
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700998 ccDep, _ := dep.(*Module)
999 if ccDep == nil {
1000 // handling for a few module types that aren't cc Module but that are also supported
1001 switch depTag {
Colin Cross068e0fe2016-12-13 15:23:47 -08001002 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -07001003 // Nothing to do
Dan Willemsenb40aab62016-04-20 14:21:14 -07001004 case genSourceDepTag:
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001005 if genRule, ok := dep.(genrule.SourceFileGenerator); ok {
Dan Willemsenb40aab62016-04-20 14:21:14 -07001006 depPaths.GeneratedSources = append(depPaths.GeneratedSources,
1007 genRule.GeneratedSourceFiles()...)
1008 } else {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001009 ctx.ModuleErrorf("module %q is not a gensrcs or genrule", depName)
Dan Willemsenb40aab62016-04-20 14:21:14 -07001010 }
Colin Crosse90bfd12017-04-26 16:59:26 -07001011 // Support exported headers from a generated_sources dependency
1012 fallthrough
Dan Willemsenb3454ab2016-09-28 17:34:58 -07001013 case genHeaderDepTag, genHeaderExportDepTag:
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001014 if genRule, ok := dep.(genrule.SourceFileGenerator); ok {
Dan Willemsenb40aab62016-04-20 14:21:14 -07001015 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders,
1016 genRule.GeneratedSourceFiles()...)
Colin Cross5ed99c62016-11-22 12:55:55 -08001017 flags := includeDirsToFlags(genRule.GeneratedHeaderDirs())
Dan Willemsenb3454ab2016-09-28 17:34:58 -07001018 depPaths.Flags = append(depPaths.Flags, flags)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001019 if depTag == genHeaderExportDepTag {
Dan Willemsenb3454ab2016-09-28 17:34:58 -07001020 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags)
Dan Willemsen847dcc72016-09-29 12:13:36 -07001021 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps,
1022 genRule.GeneratedSourceFiles()...)
Jayant Chowdhary715cac32017-04-20 06:53:59 -07001023 // Add these re-exported flags to help header-abi-dumper to infer the abi exported by a library.
1024 c.sabi.Properties.ReexportedIncludeFlags = append(c.sabi.Properties.ReexportedIncludeFlags, flags)
1025
Dan Willemsenb3454ab2016-09-28 17:34:58 -07001026 }
Dan Willemsenb40aab62016-04-20 14:21:14 -07001027 } else {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001028 ctx.ModuleErrorf("module %q is not a genrule", depName)
Dan Willemsenb40aab62016-04-20 14:21:14 -07001029 }
Dan Willemsenc77a0b32017-09-18 23:19:12 -07001030 case linkerScriptDepTag:
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001031 if genRule, ok := dep.(genrule.SourceFileGenerator); ok {
Dan Willemsenc77a0b32017-09-18 23:19:12 -07001032 files := genRule.GeneratedSourceFiles()
1033 if len(files) == 1 {
1034 depPaths.LinkerScript = android.OptionalPathForPath(files[0])
1035 } else if len(files) > 1 {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001036 ctx.ModuleErrorf("module %q can only generate a single file if used for a linker script", depName)
Dan Willemsenc77a0b32017-09-18 23:19:12 -07001037 }
1038 } else {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001039 ctx.ModuleErrorf("module %q is not a genrule", depName)
Dan Willemsenc77a0b32017-09-18 23:19:12 -07001040 }
Dan Willemsenb40aab62016-04-20 14:21:14 -07001041 default:
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001042 ctx.ModuleErrorf("depends on non-cc module %q", depName)
Colin Crossca860ac2016-01-04 14:34:37 -08001043 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001044 return
1045 }
1046
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001047 // some validation
1048 if !aDep.Enabled() {
Colin Crossa8f5e9a2016-12-13 12:51:11 -08001049 if ctx.AConfig().AllowMissingDependencies() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001050 ctx.AddMissingDependencies([]string{depName})
Colin Crossa8f5e9a2016-12-13 12:51:11 -08001051 } else {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001052 ctx.ModuleErrorf("depends on disabled module %q", depName)
Colin Crossa8f5e9a2016-12-13 12:51:11 -08001053 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001054 return
1055 }
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001056 if aDep.Target().Os != ctx.Os() {
1057 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1058 return
1059 }
1060 if aDep.Target().Arch.ArchType != ctx.Arch().ArchType {
1061 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
Colin Crossa1ad8d12016-06-01 17:09:44 -07001062 return
1063 }
1064
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001065 // re-exporting flags
1066 if depTag == reuseObjTag {
1067 if l, ok := ccDep.compiler.(libraryInterface); ok {
Colin Crossbbc9f4d2017-05-03 16:24:55 -07001068 objs, flags, deps := l.reuseObjs()
Colin Cross10d22312017-05-03 11:01:58 -07001069 depPaths.Objs = depPaths.Objs.Append(objs)
1070 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Colin Crossbbc9f4d2017-05-03 16:24:55 -07001071 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps, deps...)
Colin Crossbba99042016-11-23 15:45:05 -08001072 return
1073 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001074 }
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001075 if t, ok := depTag.(dependencyTag); ok && t.library {
1076 if i, ok := ccDep.linker.(exportedFlagsProducer); ok {
Dan Willemsen76f08272016-07-09 00:14:08 -07001077 flags := i.exportedFlags()
Dan Willemsen847dcc72016-09-29 12:13:36 -07001078 deps := i.exportedFlagsDeps()
Dan Willemsen76f08272016-07-09 00:14:08 -07001079 depPaths.Flags = append(depPaths.Flags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -07001080 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders, deps...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001081
1082 if t.reexportFlags {
Dan Willemsen76f08272016-07-09 00:14:08 -07001083 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -07001084 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps, deps...)
Jayant Chowdhary715cac32017-04-20 06:53:59 -07001085 // 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 -07001086 // Re-exported shared library headers must be included as well since they can help us with type information
1087 // about template instantiations (instantiated from their headers).
1088 c.sabi.Properties.ReexportedIncludeFlags = append(c.sabi.Properties.ReexportedIncludeFlags, flags...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001089 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001090 }
Dan Willemsena96ff642016-06-07 12:34:45 -07001091
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001092 checkLinkType(ctx, c, ccDep)
Colin Crossc99deeb2016-04-11 15:06:20 -07001093 }
1094
Colin Cross26c34ed2016-09-30 17:10:16 -07001095 var ptr *android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -07001096 var depPtr *android.Paths
Colin Crossc99deeb2016-04-11 15:06:20 -07001097
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001098 linkFile := ccDep.outputFile
Colin Cross26c34ed2016-09-30 17:10:16 -07001099 depFile := android.OptionalPath{}
1100
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001101 switch depTag {
Dan Albert914449f2016-06-17 16:45:24 -07001102 case ndkStubDepTag, sharedDepTag, sharedExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001103 ptr = &depPaths.SharedLibs
1104 depPtr = &depPaths.SharedLibsDeps
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001105 depFile = ccDep.linker.(libraryInterface).toc()
Dan Albert914449f2016-06-17 16:45:24 -07001106 case lateSharedDepTag, ndkLateStubDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001107 ptr = &depPaths.LateSharedLibs
1108 depPtr = &depPaths.LateSharedLibsDeps
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001109 depFile = ccDep.linker.(libraryInterface).toc()
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001110 case staticDepTag, staticExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001111 ptr = &depPaths.StaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -07001112 case lateStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001113 ptr = &depPaths.LateStaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -07001114 case wholeStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001115 ptr = &depPaths.WholeStaticLibs
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001116 staticLib, ok := ccDep.linker.(libraryInterface)
Colin Crossb916a382016-07-29 17:28:03 -07001117 if !ok || !staticLib.static() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001118 ctx.ModuleErrorf("module %q not a static library", depName)
Colin Crossc99deeb2016-04-11 15:06:20 -07001119 return
1120 }
1121
1122 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001123 postfix := " (required by " + ctx.OtherModuleName(dep) + ")"
Colin Crossc99deeb2016-04-11 15:06:20 -07001124 for i := range missingDeps {
1125 missingDeps[i] += postfix
1126 }
1127 ctx.AddMissingDependencies(missingDeps)
1128 }
Dan Willemsen5cb580f2016-09-26 17:33:01 -07001129 depPaths.WholeStaticLibObjs = depPaths.WholeStaticLibObjs.Append(staticLib.objs())
Colin Cross5950f382016-12-13 12:50:57 -08001130 case headerDepTag:
1131 // Nothing
Colin Crossc99deeb2016-04-11 15:06:20 -07001132 case objDepTag:
Dan Willemsen5cb580f2016-09-26 17:33:01 -07001133 depPaths.Objs.objFiles = append(depPaths.Objs.objFiles, linkFile.Path())
Colin Crossc99deeb2016-04-11 15:06:20 -07001134 case crtBeginDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001135 depPaths.CrtBegin = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -07001136 case crtEndDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001137 depPaths.CrtEnd = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -07001138 }
1139
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001140 switch depTag {
Dan Willemsen581341d2017-02-09 16:16:31 -08001141 case staticDepTag, staticExportDepTag, lateStaticDepTag:
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001142 staticLib, ok := ccDep.linker.(libraryInterface)
Dan Willemsen581341d2017-02-09 16:16:31 -08001143 if !ok || !staticLib.static() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001144 ctx.ModuleErrorf("module %q not a static library", depName)
Dan Willemsen581341d2017-02-09 16:16:31 -08001145 return
1146 }
1147
1148 // When combining coverage files for shared libraries and executables, coverage files
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001149 // in static libraries act as if they were whole static libraries. The same goes for
1150 // source based Abi dump files.
Dan Willemsen581341d2017-02-09 16:16:31 -08001151 depPaths.StaticLibObjs.coverageFiles = append(depPaths.StaticLibObjs.coverageFiles,
1152 staticLib.objs().coverageFiles...)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001153 depPaths.StaticLibObjs.sAbiDumpFiles = append(depPaths.StaticLibObjs.sAbiDumpFiles,
1154 staticLib.objs().sAbiDumpFiles...)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001155
Dan Willemsen581341d2017-02-09 16:16:31 -08001156 }
1157
Colin Cross26c34ed2016-09-30 17:10:16 -07001158 if ptr != nil {
Colin Crossce75d2c2016-10-06 16:12:58 -07001159 if !linkFile.Valid() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001160 ctx.ModuleErrorf("module %q missing output file", depName)
Colin Crossce75d2c2016-10-06 16:12:58 -07001161 return
1162 }
Colin Cross26c34ed2016-09-30 17:10:16 -07001163 *ptr = append(*ptr, linkFile.Path())
1164 }
1165
Colin Crossc99deeb2016-04-11 15:06:20 -07001166 if depPtr != nil {
Colin Cross26c34ed2016-09-30 17:10:16 -07001167 dep := depFile
1168 if !dep.Valid() {
1169 dep = linkFile
1170 }
1171 *depPtr = append(*depPtr, dep.Path())
Colin Crossca860ac2016-01-04 14:34:37 -08001172 }
Jiyong Park27b188b2017-07-18 13:23:39 +09001173
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001174 // Export the shared libs to Make.
1175 switch depTag {
Jiyong Park27b188b2017-07-18 13:23:39 +09001176 case sharedDepTag, sharedExportDepTag, lateSharedDepTag:
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001177 libName := strings.TrimSuffix(depName, llndkLibrarySuffix)
Jiyong Park27b188b2017-07-18 13:23:39 +09001178 libName = strings.TrimPrefix(libName, "prebuilt_")
Jiyong Parkd5b18a52017-08-03 21:22:50 +09001179 isLLndk := inList(libName, llndkLibraries)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001180 var makeLibName string
1181 bothVendorAndCoreVariantsExist := ccDep.hasVendorVariant() || isLLndk
1182 if c.useVndk() && bothVendorAndCoreVariantsExist {
1183 // The vendor module in Make will have been renamed to not conflict with the core
1184 // module, so update the dependency name here accordingly.
1185 makeLibName = libName + vendorSuffix
1186 } else {
1187 makeLibName = libName
Jiyong Park27b188b2017-07-18 13:23:39 +09001188 }
1189 // Note: the order of libs in this list is not important because
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001190 // they merely serve as Make dependencies and do not affect this lib itself.
1191 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, makeLibName)
Jiyong Park27b188b2017-07-18 13:23:39 +09001192 }
Colin Crossca860ac2016-01-04 14:34:37 -08001193 })
1194
Colin Crossdd84e052017-05-17 13:44:16 -07001195 // Dedup exported flags from dependencies
1196 depPaths.Flags = firstUniqueElements(depPaths.Flags)
Dan Willemsenfe92c962017-08-29 12:28:37 -07001197 depPaths.GeneratedHeaders = android.FirstUniquePaths(depPaths.GeneratedHeaders)
1198 depPaths.ReexportedFlags = firstUniqueElements(depPaths.ReexportedFlags)
1199 depPaths.ReexportedFlagsDeps = android.FirstUniquePaths(depPaths.ReexportedFlagsDeps)
1200
1201 if c.sabi != nil {
1202 c.sabi.Properties.ReexportedIncludeFlags = firstUniqueElements(c.sabi.Properties.ReexportedIncludeFlags)
1203 }
Colin Crossdd84e052017-05-17 13:44:16 -07001204
Colin Crossca860ac2016-01-04 14:34:37 -08001205 return depPaths
1206}
1207
1208func (c *Module) InstallInData() bool {
1209 if c.installer == nil {
1210 return false
1211 }
Vishwath Mohan1dd88392017-03-29 22:00:18 -07001212 return c.installer.inData()
1213}
1214
1215func (c *Module) InstallInSanitizerDir() bool {
1216 if c.installer == nil {
1217 return false
1218 }
1219 if c.sanitize != nil && c.sanitize.inSanitizerDir() {
Colin Cross94610402016-08-29 13:41:32 -07001220 return true
1221 }
Vishwath Mohan1dd88392017-03-29 22:00:18 -07001222 return c.installer.inSanitizerDir()
Colin Crossca860ac2016-01-04 14:34:37 -08001223}
1224
Dan Willemsen4aa75ca2016-09-28 16:18:03 -07001225func (c *Module) HostToolPath() android.OptionalPath {
1226 if c.installer == nil {
1227 return android.OptionalPath{}
1228 }
1229 return c.installer.hostToolPath()
1230}
1231
Nan Zhangd4e641b2017-07-12 12:55:28 -07001232func (c *Module) IntermPathForModuleOut() android.OptionalPath {
1233 return c.outputFile
1234}
1235
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001236func (c *Module) Srcs() android.Paths {
1237 if c.outputFile.Valid() {
1238 return android.Paths{c.outputFile.Path()}
1239 }
1240 return android.Paths{}
1241}
1242
Colin Cross2ba19d92015-05-07 15:44:20 -07001243//
Colin Crosscfad1192015-11-02 16:43:11 -08001244// Defaults
1245//
Colin Crossca860ac2016-01-04 14:34:37 -08001246type Defaults struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001247 android.ModuleBase
Colin Cross1f44a3a2017-07-07 14:33:33 -07001248 android.DefaultsModuleBase
Colin Crosscfad1192015-11-02 16:43:11 -08001249}
1250
Colin Cross635c3b02016-05-18 15:37:25 -07001251func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -08001252}
1253
Colin Cross1e676be2016-10-12 14:38:15 -07001254func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
1255}
1256
Colin Cross36242852017-06-23 15:06:31 -07001257func defaultsFactory() android.Module {
Colin Crosse1d764e2016-08-18 14:18:32 -07001258 return DefaultsFactory()
1259}
1260
Colin Cross36242852017-06-23 15:06:31 -07001261func DefaultsFactory(props ...interface{}) android.Module {
Colin Crossca860ac2016-01-04 14:34:37 -08001262 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -08001263
Colin Cross36242852017-06-23 15:06:31 -07001264 module.AddProperties(props...)
1265 module.AddProperties(
Colin Crossca860ac2016-01-04 14:34:37 -08001266 &BaseProperties{},
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001267 &VendorProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001268 &BaseCompilerProperties{},
1269 &BaseLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -07001270 &LibraryProperties{},
Colin Cross919281a2016-04-05 16:42:05 -07001271 &FlagExporterProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001272 &BinaryLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -07001273 &TestProperties{},
1274 &TestBinaryProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001275 &UnusedProperties{},
1276 &StlProperties{},
Colin Cross16b23492016-01-06 14:41:07 -08001277 &SanitizeProperties{},
Colin Cross665dce92016-04-28 14:50:03 -07001278 &StripProperties{},
Dan Willemsen7424d612016-09-01 13:45:39 -07001279 &InstallerProperties{},
Dan Willemsena03cf6d2016-09-26 15:45:04 -07001280 &TidyProperties{},
Dan Willemsen581341d2017-02-09 16:16:31 -08001281 &CoverageProperties{},
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001282 &SAbiProperties{},
Justin Yun4b2382f2017-07-26 14:22:10 +09001283 &VndkProperties{},
Stephen Craneba090d12017-05-09 15:44:35 -07001284 &LTOProperties{},
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -07001285 &PgoProperties{},
Colin Crosse1d764e2016-08-18 14:18:32 -07001286 )
Colin Crosscfad1192015-11-02 16:43:11 -08001287
Colin Cross1f44a3a2017-07-07 14:33:33 -07001288 android.InitDefaultsModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001289
1290 return module
Colin Crosscfad1192015-11-02 16:43:11 -08001291}
1292
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001293const (
1294 // coreMode is the variant used for framework-private libraries, or
1295 // SDK libraries. (which framework-private libraries can use)
1296 coreMode = "core"
1297
1298 // vendorMode is the variant used for /vendor code that compiles
1299 // against the VNDK.
1300 vendorMode = "vendor"
1301)
1302
Jiyong Park6a43f042017-10-12 23:05:00 +09001303func squashVendorSrcs(m *Module) {
1304 if lib, ok := m.compiler.(*libraryDecorator); ok {
1305 lib.baseCompiler.Properties.Srcs = append(lib.baseCompiler.Properties.Srcs,
1306 lib.baseCompiler.Properties.Target.Vendor.Srcs...)
1307
1308 lib.baseCompiler.Properties.Exclude_srcs = append(lib.baseCompiler.Properties.Exclude_srcs,
1309 lib.baseCompiler.Properties.Target.Vendor.Exclude_srcs...)
1310 }
1311}
1312
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001313func vendorMutator(mctx android.BottomUpMutatorContext) {
1314 if mctx.Os() != android.Android {
1315 return
1316 }
1317
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001318 if genrule, ok := mctx.Module().(*genrule.Module); ok {
1319 if props, ok := genrule.Extra.(*VendorProperties); ok {
1320 if !mctx.DeviceConfig().CompileVndk() {
1321 mctx.CreateVariations(coreMode)
1322 } else if Bool(props.Vendor_available) {
1323 mctx.CreateVariations(coreMode, vendorMode)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001324 } else if mctx.InstallOnVendorPartition() {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001325 mctx.CreateVariations(vendorMode)
1326 } else {
1327 mctx.CreateVariations(coreMode)
1328 }
1329 }
1330 }
1331
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001332 m, ok := mctx.Module().(*Module)
1333 if !ok {
1334 return
1335 }
1336
1337 // Sanity check
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001338 if m.VendorProperties.Vendor_available != nil && mctx.InstallOnVendorPartition() {
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001339 mctx.PropertyErrorf("vendor_available",
1340 "doesn't make sense at the same time as `vendor: true` or `proprietary: true`")
1341 return
1342 }
Justin Yun8effde42017-06-23 19:24:43 +09001343 if vndk := m.vndkdep; vndk != nil {
Jiyong Park82e2bf32017-08-16 14:05:54 +09001344 if vndk.isVndk() && m.VendorProperties.Vendor_available == nil {
Justin Yun8effde42017-06-23 19:24:43 +09001345 mctx.PropertyErrorf("vndk",
Jiyong Park82e2bf32017-08-16 14:05:54 +09001346 "vendor_available must be set to either true or false when `vndk: {enabled: true}`")
Justin Yun8effde42017-06-23 19:24:43 +09001347 return
1348 }
1349 if !vndk.isVndk() && vndk.isVndkSp() {
1350 mctx.PropertyErrorf("vndk",
1351 "must set `enabled: true` to set `support_system_process: true`")
1352 return
1353 }
1354 }
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001355
1356 if !mctx.DeviceConfig().CompileVndk() {
1357 // If the device isn't compiling against the VNDK, we always
1358 // use the core mode.
1359 mctx.CreateVariations(coreMode)
1360 } else if _, ok := m.linker.(*llndkStubDecorator); ok {
1361 // LL-NDK stubs only exist in the vendor variant, since the
1362 // real libraries will be used in the core variant.
1363 mctx.CreateVariations(vendorMode)
Jiyong Park82e2bf32017-08-16 14:05:54 +09001364 } else if m.hasVendorVariant() {
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001365 // This will be available in both /system and /vendor
Justin Yun8effde42017-06-23 19:24:43 +09001366 // or a /system directory that is available to vendor.
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001367 mod := mctx.CreateVariations(coreMode, vendorMode)
Jiyong Park6a43f042017-10-12 23:05:00 +09001368 vendor := mod[1].(*Module)
1369 vendor.Properties.UseVndk = true
1370 squashVendorSrcs(vendor)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001371 } else if mctx.InstallOnVendorPartition() && m.Properties.Sdk_version == "" {
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001372 // This will be available in /vendor only
1373 mod := mctx.CreateVariations(vendorMode)
Jiyong Park6a43f042017-10-12 23:05:00 +09001374 vendor := mod[0].(*Module)
1375 vendor.Properties.UseVndk = true
1376 squashVendorSrcs(vendor)
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001377 } else {
1378 // This is either in /system (or similar: /data), or is a
1379 // modules built with the NDK. Modules built with the NDK
1380 // will be restricted using the existing link type checks.
1381 mctx.CreateVariations(coreMode)
1382 }
1383}
1384
Colin Crossdd84e052017-05-17 13:44:16 -07001385// firstUniqueElements returns all unique elements of a slice, keeping the first copy of each
1386// modifies the slice contents in place, and returns a subslice of the original slice
1387func firstUniqueElements(list []string) []string {
1388 k := 0
1389outer:
1390 for i := 0; i < len(list); i++ {
1391 for j := 0; j < k; j++ {
1392 if list[i] == list[j] {
1393 continue outer
1394 }
1395 }
1396 list[k] = list[i]
1397 k++
1398 }
1399 return list[:k]
1400}
1401
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001402// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each.
1403// It modifies the slice contents in place, and returns a subslice of the original slice
Colin Cross74d1ec02015-04-28 13:30:13 -07001404func lastUniqueElements(list []string) []string {
1405 totalSkip := 0
1406 for i := len(list) - 1; i >= totalSkip; i-- {
1407 skip := 0
1408 for j := i - 1; j >= totalSkip; j-- {
1409 if list[i] == list[j] {
1410 skip++
1411 } else {
1412 list[j+skip] = list[j]
1413 }
1414 }
1415 totalSkip += skip
1416 }
1417 return list[totalSkip:]
1418}
Colin Cross06a931b2015-10-28 17:23:31 -07001419
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -07001420func getCurrentNdkPrebuiltVersion(ctx DepsContext) string {
1421 if ctx.AConfig().PlatformSdkVersionInt() > config.NdkMaxPrebuiltVersionInt {
1422 return strconv.Itoa(config.NdkMaxPrebuiltVersionInt)
1423 }
1424 return ctx.AConfig().PlatformSdkVersion()
1425}
1426
Colin Cross06a931b2015-10-28 17:23:31 -07001427var Bool = proptools.Bool