blob: b4b70ed516f15567dfd2ee61173d887d201e3a0f [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) {
37 ctx.BottomUp("link", linkageMutator).Parallel()
Jiyong Parkd5b18a52017-08-03 21:22:50 +090038 ctx.BottomUp("vndk", vndkMutator).Parallel()
Dan Willemsen4416e5d2017-04-06 12:43:22 -070039 ctx.BottomUp("image", vendorMutator).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 {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700171 // whether this module should be allowed to install onto /vendor as
172 // well as /system. The two variants will be built separately, one
173 // like normal, and the other limited to the set of libraries and
174 // headers that are exposed to /vendor modules.
175 //
176 // The vendor variant may be used with a different (newer) /system,
177 // so it shouldn't have any unversioned runtime dependencies, or
178 // make assumptions about the system that may not be true in the
179 // future.
180 //
181 // Nothing happens if BOARD_VNDK_VERSION isn't set in the BoardConfig.mk
182 Vendor_available *bool
Colin Crossca860ac2016-01-04 14:34:37 -0800183}
184
Colin Crossca860ac2016-01-04 14:34:37 -0800185type UnusedProperties struct {
Dan Willemsen581341d2017-02-09 16:16:31 -0800186 Tags []string
Colin Crosscfad1192015-11-02 16:43:11 -0800187}
188
Colin Crossca860ac2016-01-04 14:34:37 -0800189type ModuleContextIntf interface {
Colin Crossca860ac2016-01-04 14:34:37 -0800190 static() bool
191 staticBinary() bool
192 clang() bool
Colin Crossb98c8b02016-07-29 13:44:28 -0700193 toolchain() config.Toolchain
Colin Crossca860ac2016-01-04 14:34:37 -0800194 noDefaultCompilerFlags() bool
195 sdk() bool
196 sdkVersion() string
Dan Willemsend2ede872016-11-18 14:54:24 -0800197 vndk() bool
Justin Yun8effde42017-06-23 19:24:43 +0900198 isVndk() bool
199 isVndkSp() bool
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800200 createVndkSourceAbiDump() bool
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700201 selectedStl() string
Colin Crossce75d2c2016-10-06 16:12:58 -0700202 baseModuleName() string
Colin Crossca860ac2016-01-04 14:34:37 -0800203}
204
205type ModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700206 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800207 ModuleContextIntf
208}
209
210type BaseModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700211 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800212 ModuleContextIntf
213}
214
Colin Cross37047f12016-12-13 17:06:13 -0800215type DepsContext interface {
216 android.BottomUpMutatorContext
217 ModuleContextIntf
218}
219
Colin Crossca860ac2016-01-04 14:34:37 -0800220type feature interface {
221 begin(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800222 deps(ctx DepsContext, deps Deps) Deps
Colin Crossca860ac2016-01-04 14:34:37 -0800223 flags(ctx ModuleContext, flags Flags) Flags
224 props() []interface{}
225}
226
227type compiler interface {
Colin Cross42742b82016-08-01 13:20:05 -0700228 compilerInit(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800229 compilerDeps(ctx DepsContext, deps Deps) Deps
Colin Cross42742b82016-08-01 13:20:05 -0700230 compilerFlags(ctx ModuleContext, flags Flags) Flags
231 compilerProps() []interface{}
232
Colin Cross76fada02016-07-27 10:31:13 -0700233 appendCflags([]string)
234 appendAsflags([]string)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700235 compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects
Colin Crossca860ac2016-01-04 14:34:37 -0800236}
237
238type linker interface {
Colin Cross42742b82016-08-01 13:20:05 -0700239 linkerInit(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800240 linkerDeps(ctx DepsContext, deps Deps) Deps
Colin Cross42742b82016-08-01 13:20:05 -0700241 linkerFlags(ctx ModuleContext, flags Flags) Flags
242 linkerProps() []interface{}
243
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700244 link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path
Colin Cross76fada02016-07-27 10:31:13 -0700245 appendLdflags([]string)
Colin Crossca860ac2016-01-04 14:34:37 -0800246}
247
248type installer interface {
Colin Cross42742b82016-08-01 13:20:05 -0700249 installerProps() []interface{}
Colin Cross635c3b02016-05-18 15:37:25 -0700250 install(ctx ModuleContext, path android.Path)
Colin Crossca860ac2016-01-04 14:34:37 -0800251 inData() bool
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700252 inSanitizerDir() bool
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700253 hostToolPath() android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800254}
255
Colin Crossc99deeb2016-04-11 15:06:20 -0700256type dependencyTag struct {
257 blueprint.BaseDependencyTag
258 name string
259 library bool
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700260
261 reexportFlags bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700262}
263
264var (
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700265 sharedDepTag = dependencyTag{name: "shared", library: true}
266 sharedExportDepTag = dependencyTag{name: "shared", library: true, reexportFlags: true}
267 lateSharedDepTag = dependencyTag{name: "late shared", library: true}
268 staticDepTag = dependencyTag{name: "static", library: true}
269 staticExportDepTag = dependencyTag{name: "static", library: true, reexportFlags: true}
270 lateStaticDepTag = dependencyTag{name: "late static", library: true}
271 wholeStaticDepTag = dependencyTag{name: "whole static", library: true, reexportFlags: true}
Colin Cross32ec36c2016-12-15 07:39:51 -0800272 headerDepTag = dependencyTag{name: "header", library: true}
273 headerExportDepTag = dependencyTag{name: "header", library: true, reexportFlags: true}
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700274 genSourceDepTag = dependencyTag{name: "gen source"}
275 genHeaderDepTag = dependencyTag{name: "gen header"}
276 genHeaderExportDepTag = dependencyTag{name: "gen header", reexportFlags: true}
277 objDepTag = dependencyTag{name: "obj"}
278 crtBeginDepTag = dependencyTag{name: "crtbegin"}
279 crtEndDepTag = dependencyTag{name: "crtend"}
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700280 linkerScriptDepTag = dependencyTag{name: "linker script"}
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700281 reuseObjTag = dependencyTag{name: "reuse objects"}
282 ndkStubDepTag = dependencyTag{name: "ndk stub", library: true}
283 ndkLateStubDepTag = dependencyTag{name: "ndk late stub", library: true}
Colin Crossc99deeb2016-04-11 15:06:20 -0700284)
285
Colin Crossca860ac2016-01-04 14:34:37 -0800286// Module contains the properties and members used by all C/C++ module types, and implements
287// the blueprint.Module interface. It delegates to compiler, linker, and installer interfaces
288// to construct the output file. Behavior can be customized with a Customizer interface
289type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700290 android.ModuleBase
Colin Cross1f44a3a2017-07-07 14:33:33 -0700291 android.DefaultableModuleBase
Colin Crossc472d572015-03-17 15:06:21 -0700292
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700293 Properties BaseProperties
294 VendorProperties VendorProperties
295 unused UnusedProperties
Colin Crossfa138792015-04-24 17:31:52 -0700296
Colin Crossca860ac2016-01-04 14:34:37 -0800297 // initialize before calling Init
Colin Cross635c3b02016-05-18 15:37:25 -0700298 hod android.HostOrDeviceSupported
299 multilib android.Multilib
Colin Crossc472d572015-03-17 15:06:21 -0700300
Colin Crossca860ac2016-01-04 14:34:37 -0800301 // delegates, initialize before calling Init
Colin Crossb4ce0ec2016-09-13 13:41:39 -0700302 features []feature
303 compiler compiler
304 linker linker
305 installer installer
306 stl *stl
307 sanitize *sanitize
Dan Willemsen581341d2017-02-09 16:16:31 -0800308 coverage *coverage
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800309 sabi *sabi
Justin Yun8effde42017-06-23 19:24:43 +0900310 vndkdep *vndkdep
Stephen Craneba090d12017-05-09 15:44:35 -0700311 lto *lto
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700312 pgo *pgo
Colin Cross16b23492016-01-06 14:41:07 -0800313
314 androidMkSharedLibDeps []string
Colin Cross74d1ec02015-04-28 13:30:13 -0700315
Colin Cross635c3b02016-05-18 15:37:25 -0700316 outputFile android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800317
Colin Crossb98c8b02016-07-29 13:44:28 -0700318 cachedToolchain config.Toolchain
Colin Crossb916a382016-07-29 17:28:03 -0700319
320 subAndroidMkOnce map[subAndroidMkProvider]bool
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800321
322 // Flags used to compile this module
323 flags Flags
Colin Crossc472d572015-03-17 15:06:21 -0700324}
325
Colin Cross36242852017-06-23 15:06:31 -0700326func (c *Module) Init() android.Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700327 c.AddProperties(&c.Properties, &c.VendorProperties, &c.unused)
Colin Crossca860ac2016-01-04 14:34:37 -0800328 if c.compiler != nil {
Colin Cross36242852017-06-23 15:06:31 -0700329 c.AddProperties(c.compiler.compilerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800330 }
331 if c.linker != nil {
Colin Cross36242852017-06-23 15:06:31 -0700332 c.AddProperties(c.linker.linkerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800333 }
334 if c.installer != nil {
Colin Cross36242852017-06-23 15:06:31 -0700335 c.AddProperties(c.installer.installerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800336 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700337 if c.stl != nil {
Colin Cross36242852017-06-23 15:06:31 -0700338 c.AddProperties(c.stl.props()...)
Colin Crossa8e07cc2016-04-04 15:07:06 -0700339 }
Colin Cross16b23492016-01-06 14:41:07 -0800340 if c.sanitize != nil {
Colin Cross36242852017-06-23 15:06:31 -0700341 c.AddProperties(c.sanitize.props()...)
Colin Cross16b23492016-01-06 14:41:07 -0800342 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800343 if c.coverage != nil {
Colin Cross36242852017-06-23 15:06:31 -0700344 c.AddProperties(c.coverage.props()...)
Dan Willemsen581341d2017-02-09 16:16:31 -0800345 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800346 if c.sabi != nil {
Colin Cross36242852017-06-23 15:06:31 -0700347 c.AddProperties(c.sabi.props()...)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800348 }
Justin Yun8effde42017-06-23 19:24:43 +0900349 if c.vndkdep != nil {
350 c.AddProperties(c.vndkdep.props()...)
351 }
Stephen Craneba090d12017-05-09 15:44:35 -0700352 if c.lto != nil {
353 c.AddProperties(c.lto.props()...)
354 }
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700355 if c.pgo != nil {
356 c.AddProperties(c.pgo.props()...)
357 }
Colin Crossca860ac2016-01-04 14:34:37 -0800358 for _, feature := range c.features {
Colin Cross36242852017-06-23 15:06:31 -0700359 c.AddProperties(feature.props()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800360 }
Colin Crossc472d572015-03-17 15:06:21 -0700361
Colin Cross36242852017-06-23 15:06:31 -0700362 android.InitAndroidArchModule(c, c.hod, c.multilib)
Colin Crossc472d572015-03-17 15:06:21 -0700363
Colin Cross1f44a3a2017-07-07 14:33:33 -0700364 android.InitDefaultableModule(c)
Colin Cross36242852017-06-23 15:06:31 -0700365
366 return c
Colin Crossc472d572015-03-17 15:06:21 -0700367}
368
Colin Crossb916a382016-07-29 17:28:03 -0700369// Returns true for dependency roots (binaries)
370// TODO(ccross): also handle dlopenable libraries
371func (c *Module) isDependencyRoot() bool {
372 if root, ok := c.linker.(interface {
373 isDependencyRoot() bool
374 }); ok {
375 return root.isDependencyRoot()
376 }
377 return false
378}
379
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700380func (c *Module) vndk() bool {
381 return c.Properties.UseVndk
382}
383
Justin Yun8effde42017-06-23 19:24:43 +0900384func (c *Module) isVndk() bool {
385 if c.vndkdep != nil {
386 return c.vndkdep.isVndk()
387 }
388 return false
389}
390
Colin Crossca860ac2016-01-04 14:34:37 -0800391type baseModuleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700392 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800393 moduleContextImpl
394}
395
Colin Cross37047f12016-12-13 17:06:13 -0800396type depsContext struct {
397 android.BottomUpMutatorContext
398 moduleContextImpl
399}
400
Colin Crossca860ac2016-01-04 14:34:37 -0800401type moduleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700402 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800403 moduleContextImpl
404}
405
Justin Yun8effde42017-06-23 19:24:43 +0900406// Vendor returns true for vendor modules excluding VNDK libraries so that
407// they get installed onto the correct partition
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700408func (ctx *moduleContext) Vendor() bool {
Justin Yun8effde42017-06-23 19:24:43 +0900409 return ctx.ModuleContext.Vendor() || (ctx.mod.vndk() && !ctx.mod.isVndk())
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700410}
411
Colin Crossca860ac2016-01-04 14:34:37 -0800412type moduleContextImpl struct {
413 mod *Module
414 ctx BaseModuleContext
415}
416
Colin Crossca860ac2016-01-04 14:34:37 -0800417func (ctx *moduleContextImpl) clang() bool {
418 return ctx.mod.clang(ctx.ctx)
419}
420
Colin Crossb98c8b02016-07-29 13:44:28 -0700421func (ctx *moduleContextImpl) toolchain() config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800422 return ctx.mod.toolchain(ctx.ctx)
423}
424
425func (ctx *moduleContextImpl) static() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700426 if static, ok := ctx.mod.linker.(interface {
427 static() bool
428 }); ok {
429 return static.static()
Colin Crossca860ac2016-01-04 14:34:37 -0800430 }
Colin Crossb916a382016-07-29 17:28:03 -0700431 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800432}
433
434func (ctx *moduleContextImpl) staticBinary() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700435 if static, ok := ctx.mod.linker.(interface {
436 staticBinary() bool
437 }); ok {
438 return static.staticBinary()
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) noDefaultCompilerFlags() bool {
444 return Bool(ctx.mod.Properties.No_default_compiler_flags)
445}
446
447func (ctx *moduleContextImpl) sdk() bool {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700448 if ctx.ctx.Device() && !ctx.vndk() {
Dan Willemsena96ff642016-06-07 12:34:45 -0700449 return ctx.mod.Properties.Sdk_version != ""
450 }
451 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800452}
453
454func (ctx *moduleContextImpl) sdkVersion() string {
Dan Willemsena96ff642016-06-07 12:34:45 -0700455 if ctx.ctx.Device() {
Dan Willemsen11b26142017-03-19 18:30:37 -0700456 if ctx.vndk() {
457 return "current"
Dan Willemsend2ede872016-11-18 14:54:24 -0800458 } else {
459 return ctx.mod.Properties.Sdk_version
460 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700461 }
462 return ""
Colin Crossca860ac2016-01-04 14:34:37 -0800463}
464
Dan Willemsend2ede872016-11-18 14:54:24 -0800465func (ctx *moduleContextImpl) vndk() bool {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700466 return ctx.mod.vndk()
Dan Willemsend2ede872016-11-18 14:54:24 -0800467}
468
Justin Yun8effde42017-06-23 19:24:43 +0900469func (ctx *moduleContextImpl) isVndk() bool {
470 return ctx.mod.isVndk()
471}
472
473func (ctx *moduleContextImpl) isVndkSp() bool {
474 if vndk := ctx.mod.vndkdep; vndk != nil {
475 return vndk.isVndkSp()
476 }
477 return false
478}
479
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800480// Create source abi dumps if the module belongs to the list of VndkLibraries.
481func (ctx *moduleContextImpl) createVndkSourceAbiDump() bool {
Jayant Chowdharya4fce192017-09-06 13:10:03 -0700482 return ctx.ctx.Device() && ((ctx.vndk() && ctx.isVndk()) || inList(ctx.baseModuleName(), llndkLibraries))
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800483}
484
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700485func (ctx *moduleContextImpl) selectedStl() string {
486 if stl := ctx.mod.stl; stl != nil {
487 return stl.Properties.SelectedStl
488 }
489 return ""
490}
491
Colin Crossce75d2c2016-10-06 16:12:58 -0700492func (ctx *moduleContextImpl) baseModuleName() string {
493 return ctx.mod.ModuleBase.BaseModuleName()
494}
495
Colin Cross635c3b02016-05-18 15:37:25 -0700496func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800497 return &Module{
498 hod: hod,
499 multilib: multilib,
500 }
501}
502
Colin Cross635c3b02016-05-18 15:37:25 -0700503func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800504 module := newBaseModule(hod, multilib)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700505 module.features = []feature{
506 &tidyFeature{},
507 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700508 module.stl = &stl{}
Colin Cross16b23492016-01-06 14:41:07 -0800509 module.sanitize = &sanitize{}
Dan Willemsen581341d2017-02-09 16:16:31 -0800510 module.coverage = &coverage{}
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800511 module.sabi = &sabi{}
Justin Yun8effde42017-06-23 19:24:43 +0900512 module.vndkdep = &vndkdep{}
Stephen Craneba090d12017-05-09 15:44:35 -0700513 module.lto = &lto{}
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700514 module.pgo = &pgo{}
Colin Crossca860ac2016-01-04 14:34:37 -0800515 return module
516}
517
Colin Crossce75d2c2016-10-06 16:12:58 -0700518func (c *Module) Prebuilt() *android.Prebuilt {
519 if p, ok := c.linker.(prebuiltLinkerInterface); ok {
520 return p.prebuilt()
521 }
522 return nil
523}
524
525func (c *Module) Name() string {
526 name := c.ModuleBase.Name()
Dan Willemsen01a90592017-04-07 15:21:13 -0700527 if p, ok := c.linker.(interface {
528 Name(string) string
529 }); ok {
Colin Crossce75d2c2016-10-06 16:12:58 -0700530 name = p.Name(name)
531 }
532 return name
533}
534
Colin Cross635c3b02016-05-18 15:37:25 -0700535func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800536 ctx := &moduleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700537 ModuleContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800538 moduleContextImpl: moduleContextImpl{
539 mod: c,
540 },
541 }
542 ctx.ctx = ctx
543
544 flags := Flags{
545 Toolchain: c.toolchain(ctx),
546 Clang: c.clang(ctx),
547 }
Colin Crossca860ac2016-01-04 14:34:37 -0800548 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700549 flags = c.compiler.compilerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800550 }
551 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700552 flags = c.linker.linkerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800553 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700554 if c.stl != nil {
555 flags = c.stl.flags(ctx, flags)
556 }
Colin Cross16b23492016-01-06 14:41:07 -0800557 if c.sanitize != nil {
558 flags = c.sanitize.flags(ctx, flags)
559 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800560 if c.coverage != nil {
561 flags = c.coverage.flags(ctx, flags)
562 }
Stephen Craneba090d12017-05-09 15:44:35 -0700563 if c.lto != nil {
564 flags = c.lto.flags(ctx, flags)
565 }
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700566 if c.pgo != nil {
567 flags = c.pgo.flags(ctx, flags)
568 }
Colin Crossca860ac2016-01-04 14:34:37 -0800569 for _, feature := range c.features {
570 flags = feature.flags(ctx, flags)
571 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800572 if ctx.Failed() {
573 return
574 }
575
Colin Crossb98c8b02016-07-29 13:44:28 -0700576 flags.CFlags, _ = filterList(flags.CFlags, config.IllegalFlags)
577 flags.CppFlags, _ = filterList(flags.CppFlags, config.IllegalFlags)
578 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, config.IllegalFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800579
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800580 deps := c.depsToPaths(ctx)
581 if ctx.Failed() {
582 return
583 }
584 flags.GlobalFlags = append(flags.GlobalFlags, deps.Flags...)
585 c.flags = flags
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700586 // We need access to all the flags seen by a source file.
587 if c.sabi != nil {
588 flags = c.sabi.flags(ctx, flags)
589 }
Colin Crossca860ac2016-01-04 14:34:37 -0800590 // Optimization to reduce size of build.ninja
591 // Replace the long list of flags for each file with a module-local variable
592 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
593 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
594 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
595 flags.CFlags = []string{"$cflags"}
596 flags.CppFlags = []string{"$cppflags"}
597 flags.AsFlags = []string{"$asflags"}
598
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700599 var objs Objects
Colin Crossca860ac2016-01-04 14:34:37 -0800600 if c.compiler != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700601 objs = c.compiler.compile(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -0800602 if ctx.Failed() {
603 return
604 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800605 }
606
Colin Crossca860ac2016-01-04 14:34:37 -0800607 if c.linker != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700608 outputFile := c.linker.link(ctx, flags, deps, objs)
Colin Crossca860ac2016-01-04 14:34:37 -0800609 if ctx.Failed() {
610 return
611 }
Colin Cross635c3b02016-05-18 15:37:25 -0700612 c.outputFile = android.OptionalPathForPath(outputFile)
Colin Crossce75d2c2016-10-06 16:12:58 -0700613 }
Colin Cross5049f022015-03-18 13:28:46 -0700614
Colin Crossce75d2c2016-10-06 16:12:58 -0700615 if c.installer != nil && !c.Properties.PreventInstall && c.outputFile.Valid() {
616 c.installer.install(ctx, c.outputFile.Path())
617 if ctx.Failed() {
618 return
Colin Crossca860ac2016-01-04 14:34:37 -0800619 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700620 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800621}
622
Colin Crossb98c8b02016-07-29 13:44:28 -0700623func (c *Module) toolchain(ctx BaseModuleContext) config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800624 if c.cachedToolchain == nil {
Colin Crossb98c8b02016-07-29 13:44:28 -0700625 c.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
Colin Cross3f40fa42015-01-30 17:27:36 -0800626 }
Colin Crossca860ac2016-01-04 14:34:37 -0800627 return c.cachedToolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800628}
629
Colin Crossca860ac2016-01-04 14:34:37 -0800630func (c *Module) begin(ctx BaseModuleContext) {
631 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700632 c.compiler.compilerInit(ctx)
Colin Cross21b9a242015-03-24 14:15:58 -0700633 }
Colin Crossca860ac2016-01-04 14:34:37 -0800634 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700635 c.linker.linkerInit(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800636 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700637 if c.stl != nil {
638 c.stl.begin(ctx)
639 }
Colin Cross16b23492016-01-06 14:41:07 -0800640 if c.sanitize != nil {
641 c.sanitize.begin(ctx)
642 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800643 if c.coverage != nil {
644 c.coverage.begin(ctx)
645 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800646 if c.sabi != nil {
647 c.sabi.begin(ctx)
648 }
Justin Yun8effde42017-06-23 19:24:43 +0900649 if c.vndkdep != nil {
650 c.vndkdep.begin(ctx)
651 }
Stephen Craneba090d12017-05-09 15:44:35 -0700652 if c.lto != nil {
653 c.lto.begin(ctx)
654 }
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700655 if c.pgo != nil {
656 c.pgo.begin(ctx)
657 }
Colin Crossca860ac2016-01-04 14:34:37 -0800658 for _, feature := range c.features {
659 feature.begin(ctx)
660 }
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700661 if ctx.sdk() {
Dan Albertf5415d72017-08-17 16:19:59 -0700662 version, err := normalizeNdkApiLevel(ctx, ctx.sdkVersion(), ctx.Arch())
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700663 if err != nil {
664 ctx.PropertyErrorf("sdk_version", err.Error())
665 }
Dan Albert90f7a4d2016-11-08 14:34:24 -0800666 c.Properties.Sdk_version = version
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700667 }
Colin Crossca860ac2016-01-04 14:34:37 -0800668}
669
Colin Cross37047f12016-12-13 17:06:13 -0800670func (c *Module) deps(ctx DepsContext) Deps {
Colin Crossc99deeb2016-04-11 15:06:20 -0700671 deps := Deps{}
672
673 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700674 deps = c.compiler.compilerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700675 }
676 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700677 deps = c.linker.linkerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700678 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700679 if c.stl != nil {
680 deps = c.stl.deps(ctx, deps)
681 }
Colin Cross16b23492016-01-06 14:41:07 -0800682 if c.sanitize != nil {
683 deps = c.sanitize.deps(ctx, deps)
684 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800685 if c.coverage != nil {
686 deps = c.coverage.deps(ctx, deps)
687 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800688 if c.sabi != nil {
689 deps = c.sabi.deps(ctx, deps)
690 }
Justin Yun8effde42017-06-23 19:24:43 +0900691 if c.vndkdep != nil {
692 deps = c.vndkdep.deps(ctx, deps)
693 }
Stephen Craneba090d12017-05-09 15:44:35 -0700694 if c.lto != nil {
695 deps = c.lto.deps(ctx, deps)
696 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700697 for _, feature := range c.features {
698 deps = feature.deps(ctx, deps)
699 }
700
701 deps.WholeStaticLibs = lastUniqueElements(deps.WholeStaticLibs)
702 deps.StaticLibs = lastUniqueElements(deps.StaticLibs)
703 deps.LateStaticLibs = lastUniqueElements(deps.LateStaticLibs)
704 deps.SharedLibs = lastUniqueElements(deps.SharedLibs)
705 deps.LateSharedLibs = lastUniqueElements(deps.LateSharedLibs)
Colin Cross5950f382016-12-13 12:50:57 -0800706 deps.HeaderLibs = lastUniqueElements(deps.HeaderLibs)
Colin Crossc99deeb2016-04-11 15:06:20 -0700707
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700708 for _, lib := range deps.ReexportSharedLibHeaders {
709 if !inList(lib, deps.SharedLibs) {
710 ctx.PropertyErrorf("export_shared_lib_headers", "Shared library not in shared_libs: '%s'", lib)
711 }
712 }
713
714 for _, lib := range deps.ReexportStaticLibHeaders {
715 if !inList(lib, deps.StaticLibs) {
716 ctx.PropertyErrorf("export_static_lib_headers", "Static library not in static_libs: '%s'", lib)
717 }
718 }
719
Colin Cross5950f382016-12-13 12:50:57 -0800720 for _, lib := range deps.ReexportHeaderLibHeaders {
721 if !inList(lib, deps.HeaderLibs) {
722 ctx.PropertyErrorf("export_header_lib_headers", "Header library not in header_libs: '%s'", lib)
723 }
724 }
725
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700726 for _, gen := range deps.ReexportGeneratedHeaders {
727 if !inList(gen, deps.GeneratedHeaders) {
728 ctx.PropertyErrorf("export_generated_headers", "Generated header module not in generated_headers: '%s'", gen)
729 }
730 }
731
Colin Crossc99deeb2016-04-11 15:06:20 -0700732 return deps
733}
734
Dan Albert7e9d2952016-08-04 13:02:36 -0700735func (c *Module) beginMutator(actx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800736 ctx := &baseModuleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700737 BaseContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800738 moduleContextImpl: moduleContextImpl{
739 mod: c,
740 },
741 }
742 ctx.ctx = ctx
743
Colin Crossca860ac2016-01-04 14:34:37 -0800744 c.begin(ctx)
Dan Albert7e9d2952016-08-04 13:02:36 -0700745}
746
Colin Cross1e676be2016-10-12 14:38:15 -0700747func (c *Module) DepsMutator(actx android.BottomUpMutatorContext) {
748 if !c.Enabled() {
749 return
750 }
751
Colin Cross37047f12016-12-13 17:06:13 -0800752 ctx := &depsContext{
753 BottomUpMutatorContext: actx,
Dan Albert7e9d2952016-08-04 13:02:36 -0700754 moduleContextImpl: moduleContextImpl{
755 mod: c,
756 },
757 }
758 ctx.ctx = ctx
Colin Crossca860ac2016-01-04 14:34:37 -0800759
Colin Crossc99deeb2016-04-11 15:06:20 -0700760 deps := c.deps(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800761
Dan Albert914449f2016-06-17 16:45:24 -0700762 variantNdkLibs := []string{}
763 variantLateNdkLibs := []string{}
Dan Willemsenb916b802017-03-19 13:44:32 -0700764 if ctx.Os() == android.Android {
Dan Albert914449f2016-06-17 16:45:24 -0700765 version := ctx.sdkVersion()
Dan Willemsen72d39932016-07-08 23:23:48 -0700766
Dan Albert914449f2016-06-17 16:45:24 -0700767 // Rewrites the names of shared libraries into the names of the NDK
768 // libraries where appropriate. This returns two slices.
769 //
770 // The first is a list of non-variant shared libraries (either rewritten
771 // NDK libraries to the modules in prebuilts/ndk, or not rewritten
772 // because they are not NDK libraries).
773 //
774 // The second is a list of ndk_library modules. These need to be
775 // separated because they are a variation dependency and must be added
776 // in a different manner.
777 rewriteNdkLibs := func(list []string) ([]string, []string) {
778 variantLibs := []string{}
779 nonvariantLibs := []string{}
780 for _, entry := range list {
Dan Willemsenb916b802017-03-19 13:44:32 -0700781 if ctx.sdk() && inList(entry, ndkPrebuiltSharedLibraries) {
Dan Albert914449f2016-06-17 16:45:24 -0700782 if !inList(entry, ndkMigratedLibs) {
783 nonvariantLibs = append(nonvariantLibs, entry+".ndk."+version)
784 } else {
785 variantLibs = append(variantLibs, entry+ndkLibrarySuffix)
786 }
Jiyong Parkd5b18a52017-08-03 21:22:50 +0900787 } else if ctx.vndk() && inList(entry, llndkLibraries) {
Dan Willemsenb916b802017-03-19 13:44:32 -0700788 nonvariantLibs = append(nonvariantLibs, entry+llndkLibrarySuffix)
Dan Albert914449f2016-06-17 16:45:24 -0700789 } else {
Dan Willemsen7cbf5f82017-03-28 00:08:30 -0700790 nonvariantLibs = append(nonvariantLibs, entry)
Dan Willemsen72d39932016-07-08 23:23:48 -0700791 }
792 }
Dan Albert914449f2016-06-17 16:45:24 -0700793 return nonvariantLibs, variantLibs
Dan Willemsen72d39932016-07-08 23:23:48 -0700794 }
795
Dan Albert914449f2016-06-17 16:45:24 -0700796 deps.SharedLibs, variantNdkLibs = rewriteNdkLibs(deps.SharedLibs)
797 deps.LateSharedLibs, variantLateNdkLibs = rewriteNdkLibs(deps.LateSharedLibs)
Jiyong Park4c35af02017-07-05 13:41:55 +0900798 deps.ReexportSharedLibHeaders, _ = rewriteNdkLibs(deps.ReexportSharedLibHeaders)
Dan Willemsen72d39932016-07-08 23:23:48 -0700799 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700800
Colin Cross32ec36c2016-12-15 07:39:51 -0800801 for _, lib := range deps.HeaderLibs {
802 depTag := headerDepTag
803 if inList(lib, deps.ReexportHeaderLibHeaders) {
804 depTag = headerExportDepTag
805 }
806 actx.AddVariationDependencies(nil, depTag, lib)
807 }
Colin Cross5950f382016-12-13 12:50:57 -0800808
Colin Crossc99deeb2016-04-11 15:06:20 -0700809 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, wholeStaticDepTag,
810 deps.WholeStaticLibs...)
811
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700812 for _, lib := range deps.StaticLibs {
813 depTag := staticDepTag
814 if inList(lib, deps.ReexportStaticLibHeaders) {
815 depTag = staticExportDepTag
816 }
Colin Cross15a0d462016-07-14 14:49:58 -0700817 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700818 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700819
820 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, lateStaticDepTag,
821 deps.LateStaticLibs...)
822
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700823 for _, lib := range deps.SharedLibs {
824 depTag := sharedDepTag
825 if inList(lib, deps.ReexportSharedLibHeaders) {
826 depTag = sharedExportDepTag
827 }
Colin Cross15a0d462016-07-14 14:49:58 -0700828 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700829 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700830
831 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, lateSharedDepTag,
832 deps.LateSharedLibs...)
833
Colin Cross68861832016-07-08 10:41:41 -0700834 actx.AddDependency(c, genSourceDepTag, deps.GeneratedSources...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700835
836 for _, gen := range deps.GeneratedHeaders {
837 depTag := genHeaderDepTag
838 if inList(gen, deps.ReexportGeneratedHeaders) {
839 depTag = genHeaderExportDepTag
840 }
841 actx.AddDependency(c, depTag, gen)
842 }
Dan Willemsenb40aab62016-04-20 14:21:14 -0700843
Colin Cross68861832016-07-08 10:41:41 -0700844 actx.AddDependency(c, objDepTag, deps.ObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700845
846 if deps.CrtBegin != "" {
Colin Cross68861832016-07-08 10:41:41 -0700847 actx.AddDependency(c, crtBeginDepTag, deps.CrtBegin)
Colin Crossca860ac2016-01-04 14:34:37 -0800848 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700849 if deps.CrtEnd != "" {
Colin Cross68861832016-07-08 10:41:41 -0700850 actx.AddDependency(c, crtEndDepTag, deps.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700851 }
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700852 if deps.LinkerScript != "" {
853 actx.AddDependency(c, linkerScriptDepTag, deps.LinkerScript)
854 }
Dan Albert914449f2016-06-17 16:45:24 -0700855
856 version := ctx.sdkVersion()
857 actx.AddVariationDependencies([]blueprint.Variation{
858 {"ndk_api", version}, {"link", "shared"}}, ndkStubDepTag, variantNdkLibs...)
859 actx.AddVariationDependencies([]blueprint.Variation{
860 {"ndk_api", version}, {"link", "shared"}}, ndkLateStubDepTag, variantLateNdkLibs...)
Colin Cross6362e272015-10-29 15:25:03 -0700861}
Colin Cross21b9a242015-03-24 14:15:58 -0700862
Dan Albert7e9d2952016-08-04 13:02:36 -0700863func beginMutator(ctx android.BottomUpMutatorContext) {
864 if c, ok := ctx.Module().(*Module); ok && c.Enabled() {
865 c.beginMutator(ctx)
866 }
867}
868
Colin Crossca860ac2016-01-04 14:34:37 -0800869func (c *Module) clang(ctx BaseModuleContext) bool {
870 clang := Bool(c.Properties.Clang)
871
872 if c.Properties.Clang == nil {
873 if ctx.Host() {
874 clang = true
875 }
876
877 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
878 clang = true
879 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800880 }
Colin Cross28344522015-04-22 13:07:53 -0700881
Colin Crossca860ac2016-01-04 14:34:37 -0800882 if !c.toolchain(ctx).ClangSupported() {
883 clang = false
884 }
885
886 return clang
887}
888
Colin Crossc99deeb2016-04-11 15:06:20 -0700889// Convert dependencies to paths. Returns a PathDeps containing paths
Colin Cross635c3b02016-05-18 15:37:25 -0700890func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
Colin Crossca860ac2016-01-04 14:34:37 -0800891 var depPaths PathDeps
Colin Crossca860ac2016-01-04 14:34:37 -0800892
Dan Willemsena96ff642016-06-07 12:34:45 -0700893 // Whether a module can link to another module, taking into
894 // account NDK linking.
Dan Albert9e10cd42016-08-03 14:12:14 -0700895 checkLinkType := func(from, to *Module) {
Dan Willemsena96ff642016-06-07 12:34:45 -0700896 if from.Target().Os != android.Android {
897 // Host code is not restricted
Dan Albert9e10cd42016-08-03 14:12:14 -0700898 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700899 }
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700900 if from.Properties.UseVndk {
Justin Yun8effde42017-06-23 19:24:43 +0900901 // Though vendor code is limited by the vendor mutator,
902 // each vendor-available module needs to check
903 // link-type for VNDK.
904 if from.vndkdep != nil {
905 from.vndkdep.vndkCheckLinkType(ctx, to)
906 }
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700907 return
908 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700909 if from.Properties.Sdk_version == "" {
910 // Platform code can link to anything
Dan Albert9e10cd42016-08-03 14:12:14 -0700911 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700912 }
Colin Crossb916a382016-07-29 17:28:03 -0700913 if _, ok := to.linker.(*toolchainLibraryDecorator); ok {
Dan Willemsena96ff642016-06-07 12:34:45 -0700914 // These are always allowed
Dan Albert9e10cd42016-08-03 14:12:14 -0700915 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700916 }
917 if _, ok := to.linker.(*ndkPrebuiltLibraryLinker); ok {
918 // These are allowed, but don't set sdk_version
Dan Albert9e10cd42016-08-03 14:12:14 -0700919 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700920 }
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700921 if _, ok := to.linker.(*ndkPrebuiltStlLinker); ok {
922 // These are allowed, but don't set sdk_version
Dan Albert9e10cd42016-08-03 14:12:14 -0700923 return
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700924 }
Colin Crossb916a382016-07-29 17:28:03 -0700925 if _, ok := to.linker.(*stubDecorator); ok {
Dan Albert914449f2016-06-17 16:45:24 -0700926 // These aren't real libraries, but are the stub shared libraries that are included in
927 // the NDK.
Dan Albert9e10cd42016-08-03 14:12:14 -0700928 return
Dan Albert914449f2016-06-17 16:45:24 -0700929 }
Dan Albert9e10cd42016-08-03 14:12:14 -0700930 if to.Properties.Sdk_version == "" {
931 // NDK code linking to platform code is never okay.
932 ctx.ModuleErrorf("depends on non-NDK-built library %q",
933 ctx.OtherModuleName(to))
934 }
935
936 // All this point we know we have two NDK libraries, but we need to
937 // check that we're not linking against anything built against a higher
938 // API level, as it is only valid to link against older or equivalent
939 // APIs.
940
941 if from.Properties.Sdk_version == "current" {
942 // Current can link against anything.
943 return
944 } else if to.Properties.Sdk_version == "current" {
945 // Current can't be linked against by anything else.
946 ctx.ModuleErrorf("links %q built against newer API version %q",
947 ctx.OtherModuleName(to), "current")
948 }
949
950 fromApi, err := strconv.Atoi(from.Properties.Sdk_version)
951 if err != nil {
952 ctx.PropertyErrorf("sdk_version",
953 "Invalid sdk_version value (must be int): %q",
954 from.Properties.Sdk_version)
955 }
956 toApi, err := strconv.Atoi(to.Properties.Sdk_version)
957 if err != nil {
958 ctx.PropertyErrorf("sdk_version",
959 "Invalid sdk_version value (must be int): %q",
960 to.Properties.Sdk_version)
961 }
962
963 if toApi > fromApi {
964 ctx.ModuleErrorf("links %q built against newer API version %q",
965 ctx.OtherModuleName(to), to.Properties.Sdk_version)
966 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700967 }
968
Colin Crossc99deeb2016-04-11 15:06:20 -0700969 ctx.VisitDirectDeps(func(m blueprint.Module) {
970 name := ctx.OtherModuleName(m)
971 tag := ctx.OtherModuleDependencyTag(m)
Colin Crossca860ac2016-01-04 14:34:37 -0800972
Colin Cross635c3b02016-05-18 15:37:25 -0700973 a, _ := m.(android.Module)
Colin Crossc99deeb2016-04-11 15:06:20 -0700974 if a == nil {
975 ctx.ModuleErrorf("module %q not an android module", name)
976 return
Colin Crossca860ac2016-01-04 14:34:37 -0800977 }
Colin Crossca860ac2016-01-04 14:34:37 -0800978
Dan Willemsena96ff642016-06-07 12:34:45 -0700979 cc, _ := m.(*Module)
980 if cc == nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700981 switch tag {
Colin Cross068e0fe2016-12-13 15:23:47 -0800982 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700983 // Nothing to do
Dan Willemsenb40aab62016-04-20 14:21:14 -0700984 case genSourceDepTag:
985 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
986 depPaths.GeneratedSources = append(depPaths.GeneratedSources,
987 genRule.GeneratedSourceFiles()...)
988 } else {
989 ctx.ModuleErrorf("module %q is not a gensrcs or genrule", name)
990 }
Colin Crosse90bfd12017-04-26 16:59:26 -0700991 // Support exported headers from a generated_sources dependency
992 fallthrough
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700993 case genHeaderDepTag, genHeaderExportDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700994 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
995 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders,
996 genRule.GeneratedSourceFiles()...)
Colin Cross5ed99c62016-11-22 12:55:55 -0800997 flags := includeDirsToFlags(genRule.GeneratedHeaderDirs())
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700998 depPaths.Flags = append(depPaths.Flags, flags)
999 if tag == genHeaderExportDepTag {
1000 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags)
Dan Willemsen847dcc72016-09-29 12:13:36 -07001001 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps,
1002 genRule.GeneratedSourceFiles()...)
Jayant Chowdhary715cac32017-04-20 06:53:59 -07001003 // Add these re-exported flags to help header-abi-dumper to infer the abi exported by a library.
1004 c.sabi.Properties.ReexportedIncludeFlags = append(c.sabi.Properties.ReexportedIncludeFlags, flags)
1005
Dan Willemsenb3454ab2016-09-28 17:34:58 -07001006 }
Dan Willemsenb40aab62016-04-20 14:21:14 -07001007 } else {
1008 ctx.ModuleErrorf("module %q is not a genrule", name)
1009 }
Dan Willemsenc77a0b32017-09-18 23:19:12 -07001010 case linkerScriptDepTag:
1011 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
1012 files := genRule.GeneratedSourceFiles()
1013 if len(files) == 1 {
1014 depPaths.LinkerScript = android.OptionalPathForPath(files[0])
1015 } else if len(files) > 1 {
1016 ctx.ModuleErrorf("module %q can only generate a single file if used for a linker script", name)
1017 }
1018 } else {
1019 ctx.ModuleErrorf("module %q is not a genrule", name)
1020 }
Dan Willemsenb40aab62016-04-20 14:21:14 -07001021 default:
Colin Crossc99deeb2016-04-11 15:06:20 -07001022 ctx.ModuleErrorf("depends on non-cc module %q", name)
Colin Crossca860ac2016-01-04 14:34:37 -08001023 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001024 return
1025 }
1026
1027 if !a.Enabled() {
Colin Crossa8f5e9a2016-12-13 12:51:11 -08001028 if ctx.AConfig().AllowMissingDependencies() {
1029 ctx.AddMissingDependencies([]string{name})
1030 } else {
1031 ctx.ModuleErrorf("depends on disabled module %q", name)
1032 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001033 return
1034 }
1035
Colin Crossa1ad8d12016-06-01 17:09:44 -07001036 if a.Target().Os != ctx.Os() {
1037 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), name)
1038 return
1039 }
1040
1041 if a.Target().Arch.ArchType != ctx.Arch().ArchType {
1042 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), name)
Colin Crossc99deeb2016-04-11 15:06:20 -07001043 return
1044 }
1045
Colin Crossc99deeb2016-04-11 15:06:20 -07001046 if tag == reuseObjTag {
Colin Crossbba99042016-11-23 15:45:05 -08001047 if l, ok := cc.compiler.(libraryInterface); ok {
Colin Crossbbc9f4d2017-05-03 16:24:55 -07001048 objs, flags, deps := l.reuseObjs()
Colin Cross10d22312017-05-03 11:01:58 -07001049 depPaths.Objs = depPaths.Objs.Append(objs)
1050 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Colin Crossbbc9f4d2017-05-03 16:24:55 -07001051 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps, deps...)
Colin Crossbba99042016-11-23 15:45:05 -08001052 return
1053 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001054 }
1055
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001056 if t, ok := tag.(dependencyTag); ok && t.library {
Dan Willemsena96ff642016-06-07 12:34:45 -07001057 if i, ok := cc.linker.(exportedFlagsProducer); ok {
Dan Willemsen76f08272016-07-09 00:14:08 -07001058 flags := i.exportedFlags()
Dan Willemsen847dcc72016-09-29 12:13:36 -07001059 deps := i.exportedFlagsDeps()
Dan Willemsen76f08272016-07-09 00:14:08 -07001060 depPaths.Flags = append(depPaths.Flags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -07001061 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders, deps...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001062
1063 if t.reexportFlags {
Dan Willemsen76f08272016-07-09 00:14:08 -07001064 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -07001065 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps, deps...)
Jayant Chowdhary715cac32017-04-20 06:53:59 -07001066 // 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 -07001067 // Re-exported shared library headers must be included as well since they can help us with type information
1068 // about template instantiations (instantiated from their headers).
1069 c.sabi.Properties.ReexportedIncludeFlags = append(c.sabi.Properties.ReexportedIncludeFlags, flags...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001070 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001071 }
Dan Willemsena96ff642016-06-07 12:34:45 -07001072
Dan Albert9e10cd42016-08-03 14:12:14 -07001073 checkLinkType(c, cc)
Colin Crossc99deeb2016-04-11 15:06:20 -07001074 }
1075
Colin Cross26c34ed2016-09-30 17:10:16 -07001076 var ptr *android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -07001077 var depPtr *android.Paths
Colin Crossc99deeb2016-04-11 15:06:20 -07001078
Colin Cross26c34ed2016-09-30 17:10:16 -07001079 linkFile := cc.outputFile
1080 depFile := android.OptionalPath{}
1081
Colin Crossc99deeb2016-04-11 15:06:20 -07001082 switch tag {
Dan Albert914449f2016-06-17 16:45:24 -07001083 case ndkStubDepTag, sharedDepTag, sharedExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001084 ptr = &depPaths.SharedLibs
1085 depPtr = &depPaths.SharedLibsDeps
1086 depFile = cc.linker.(libraryInterface).toc()
Dan Albert914449f2016-06-17 16:45:24 -07001087 case lateSharedDepTag, ndkLateStubDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001088 ptr = &depPaths.LateSharedLibs
1089 depPtr = &depPaths.LateSharedLibsDeps
1090 depFile = cc.linker.(libraryInterface).toc()
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001091 case staticDepTag, staticExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001092 ptr = &depPaths.StaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -07001093 case lateStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001094 ptr = &depPaths.LateStaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -07001095 case wholeStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001096 ptr = &depPaths.WholeStaticLibs
Colin Crossb916a382016-07-29 17:28:03 -07001097 staticLib, ok := cc.linker.(libraryInterface)
1098 if !ok || !staticLib.static() {
Dan Willemsena96ff642016-06-07 12:34:45 -07001099 ctx.ModuleErrorf("module %q not a static library", name)
Colin Crossc99deeb2016-04-11 15:06:20 -07001100 return
1101 }
1102
1103 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
1104 postfix := " (required by " + ctx.OtherModuleName(m) + ")"
1105 for i := range missingDeps {
1106 missingDeps[i] += postfix
1107 }
1108 ctx.AddMissingDependencies(missingDeps)
1109 }
Dan Willemsen5cb580f2016-09-26 17:33:01 -07001110 depPaths.WholeStaticLibObjs = depPaths.WholeStaticLibObjs.Append(staticLib.objs())
Colin Cross5950f382016-12-13 12:50:57 -08001111 case headerDepTag:
1112 // Nothing
Colin Crossc99deeb2016-04-11 15:06:20 -07001113 case objDepTag:
Dan Willemsen5cb580f2016-09-26 17:33:01 -07001114 depPaths.Objs.objFiles = append(depPaths.Objs.objFiles, linkFile.Path())
Colin Crossc99deeb2016-04-11 15:06:20 -07001115 case crtBeginDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001116 depPaths.CrtBegin = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -07001117 case crtEndDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001118 depPaths.CrtEnd = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -07001119 }
1120
Dan Willemsen581341d2017-02-09 16:16:31 -08001121 switch tag {
1122 case staticDepTag, staticExportDepTag, lateStaticDepTag:
1123 staticLib, ok := cc.linker.(libraryInterface)
1124 if !ok || !staticLib.static() {
1125 ctx.ModuleErrorf("module %q not a static library", name)
1126 return
1127 }
1128
1129 // When combining coverage files for shared libraries and executables, coverage files
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001130 // in static libraries act as if they were whole static libraries. The same goes for
1131 // source based Abi dump files.
Dan Willemsen581341d2017-02-09 16:16:31 -08001132 depPaths.StaticLibObjs.coverageFiles = append(depPaths.StaticLibObjs.coverageFiles,
1133 staticLib.objs().coverageFiles...)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001134 depPaths.StaticLibObjs.sAbiDumpFiles = append(depPaths.StaticLibObjs.sAbiDumpFiles,
1135 staticLib.objs().sAbiDumpFiles...)
Dan Willemsen581341d2017-02-09 16:16:31 -08001136 }
1137
Colin Cross26c34ed2016-09-30 17:10:16 -07001138 if ptr != nil {
Colin Crossce75d2c2016-10-06 16:12:58 -07001139 if !linkFile.Valid() {
1140 ctx.ModuleErrorf("module %q missing output file", name)
1141 return
1142 }
Colin Cross26c34ed2016-09-30 17:10:16 -07001143 *ptr = append(*ptr, linkFile.Path())
1144 }
1145
Colin Crossc99deeb2016-04-11 15:06:20 -07001146 if depPtr != nil {
Colin Cross26c34ed2016-09-30 17:10:16 -07001147 dep := depFile
1148 if !dep.Valid() {
1149 dep = linkFile
1150 }
1151 *depPtr = append(*depPtr, dep.Path())
Colin Crossca860ac2016-01-04 14:34:37 -08001152 }
Jiyong Park27b188b2017-07-18 13:23:39 +09001153
1154 // Export the shared libs to the make world. In doing so, .vendor suffix
1155 // is added if the lib has both core and vendor variants and this module
1156 // is building against vndk. This is because the vendor variant will be
1157 // have .vendor suffix in its name in the make world. However, if the
1158 // lib is a vendor-only lib or this lib is not building against vndk,
1159 // then the suffix is not added.
1160 switch tag {
1161 case sharedDepTag, sharedExportDepTag, lateSharedDepTag:
1162 libName := strings.TrimSuffix(name, llndkLibrarySuffix)
1163 libName = strings.TrimPrefix(libName, "prebuilt_")
Jiyong Parkd5b18a52017-08-03 21:22:50 +09001164 isLLndk := inList(libName, llndkLibraries)
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001165 if c.vndk() && (Bool(cc.VendorProperties.Vendor_available) || isLLndk) {
Jiyong Park27b188b2017-07-18 13:23:39 +09001166 libName += vendorSuffix
1167 }
1168 // Note: the order of libs in this list is not important because
1169 // they merely serve as dependencies in the make world and do not
1170 // affect this lib itself.
1171 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, libName)
1172 }
Colin Crossca860ac2016-01-04 14:34:37 -08001173 })
1174
Colin Crossdd84e052017-05-17 13:44:16 -07001175 // Dedup exported flags from dependencies
1176 depPaths.Flags = firstUniqueElements(depPaths.Flags)
Dan Willemsenfe92c962017-08-29 12:28:37 -07001177 depPaths.GeneratedHeaders = android.FirstUniquePaths(depPaths.GeneratedHeaders)
1178 depPaths.ReexportedFlags = firstUniqueElements(depPaths.ReexportedFlags)
1179 depPaths.ReexportedFlagsDeps = android.FirstUniquePaths(depPaths.ReexportedFlagsDeps)
1180
1181 if c.sabi != nil {
1182 c.sabi.Properties.ReexportedIncludeFlags = firstUniqueElements(c.sabi.Properties.ReexportedIncludeFlags)
1183 }
Colin Crossdd84e052017-05-17 13:44:16 -07001184
Colin Crossca860ac2016-01-04 14:34:37 -08001185 return depPaths
1186}
1187
1188func (c *Module) InstallInData() bool {
1189 if c.installer == nil {
1190 return false
1191 }
Vishwath Mohan1dd88392017-03-29 22:00:18 -07001192 return c.installer.inData()
1193}
1194
1195func (c *Module) InstallInSanitizerDir() bool {
1196 if c.installer == nil {
1197 return false
1198 }
1199 if c.sanitize != nil && c.sanitize.inSanitizerDir() {
Colin Cross94610402016-08-29 13:41:32 -07001200 return true
1201 }
Vishwath Mohan1dd88392017-03-29 22:00:18 -07001202 return c.installer.inSanitizerDir()
Colin Crossca860ac2016-01-04 14:34:37 -08001203}
1204
Dan Willemsen4aa75ca2016-09-28 16:18:03 -07001205func (c *Module) HostToolPath() android.OptionalPath {
1206 if c.installer == nil {
1207 return android.OptionalPath{}
1208 }
1209 return c.installer.hostToolPath()
1210}
1211
Nan Zhangd4e641b2017-07-12 12:55:28 -07001212func (c *Module) IntermPathForModuleOut() android.OptionalPath {
1213 return c.outputFile
1214}
1215
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001216func (c *Module) Srcs() android.Paths {
1217 if c.outputFile.Valid() {
1218 return android.Paths{c.outputFile.Path()}
1219 }
1220 return android.Paths{}
1221}
1222
Colin Cross2ba19d92015-05-07 15:44:20 -07001223//
Colin Crosscfad1192015-11-02 16:43:11 -08001224// Defaults
1225//
Colin Crossca860ac2016-01-04 14:34:37 -08001226type Defaults struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001227 android.ModuleBase
Colin Cross1f44a3a2017-07-07 14:33:33 -07001228 android.DefaultsModuleBase
Colin Crosscfad1192015-11-02 16:43:11 -08001229}
1230
Colin Cross635c3b02016-05-18 15:37:25 -07001231func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -08001232}
1233
Colin Cross1e676be2016-10-12 14:38:15 -07001234func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
1235}
1236
Colin Cross36242852017-06-23 15:06:31 -07001237func defaultsFactory() android.Module {
Colin Crosse1d764e2016-08-18 14:18:32 -07001238 return DefaultsFactory()
1239}
1240
Colin Cross36242852017-06-23 15:06:31 -07001241func DefaultsFactory(props ...interface{}) android.Module {
Colin Crossca860ac2016-01-04 14:34:37 -08001242 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -08001243
Colin Cross36242852017-06-23 15:06:31 -07001244 module.AddProperties(props...)
1245 module.AddProperties(
Colin Crossca860ac2016-01-04 14:34:37 -08001246 &BaseProperties{},
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001247 &VendorProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001248 &BaseCompilerProperties{},
1249 &BaseLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -07001250 &LibraryProperties{},
Colin Cross919281a2016-04-05 16:42:05 -07001251 &FlagExporterProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001252 &BinaryLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -07001253 &TestProperties{},
1254 &TestBinaryProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001255 &UnusedProperties{},
1256 &StlProperties{},
Colin Cross16b23492016-01-06 14:41:07 -08001257 &SanitizeProperties{},
Colin Cross665dce92016-04-28 14:50:03 -07001258 &StripProperties{},
Dan Willemsen7424d612016-09-01 13:45:39 -07001259 &InstallerProperties{},
Dan Willemsena03cf6d2016-09-26 15:45:04 -07001260 &TidyProperties{},
Dan Willemsen581341d2017-02-09 16:16:31 -08001261 &CoverageProperties{},
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001262 &SAbiProperties{},
Justin Yun4b2382f2017-07-26 14:22:10 +09001263 &VndkProperties{},
Stephen Craneba090d12017-05-09 15:44:35 -07001264 &LTOProperties{},
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -07001265 &PgoProperties{},
Colin Crosse1d764e2016-08-18 14:18:32 -07001266 )
Colin Crosscfad1192015-11-02 16:43:11 -08001267
Colin Cross1f44a3a2017-07-07 14:33:33 -07001268 android.InitDefaultsModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001269
1270 return module
Colin Crosscfad1192015-11-02 16:43:11 -08001271}
1272
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001273const (
1274 // coreMode is the variant used for framework-private libraries, or
1275 // SDK libraries. (which framework-private libraries can use)
1276 coreMode = "core"
1277
1278 // vendorMode is the variant used for /vendor code that compiles
1279 // against the VNDK.
1280 vendorMode = "vendor"
1281)
1282
1283func vendorMutator(mctx android.BottomUpMutatorContext) {
1284 if mctx.Os() != android.Android {
1285 return
1286 }
1287
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001288 if genrule, ok := mctx.Module().(*genrule.Module); ok {
1289 if props, ok := genrule.Extra.(*VendorProperties); ok {
1290 if !mctx.DeviceConfig().CompileVndk() {
1291 mctx.CreateVariations(coreMode)
1292 } else if Bool(props.Vendor_available) {
1293 mctx.CreateVariations(coreMode, vendorMode)
1294 } else if mctx.Vendor() {
1295 mctx.CreateVariations(vendorMode)
1296 } else {
1297 mctx.CreateVariations(coreMode)
1298 }
1299 }
1300 }
1301
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001302 m, ok := mctx.Module().(*Module)
1303 if !ok {
1304 return
1305 }
1306
1307 // Sanity check
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001308 if Bool(m.VendorProperties.Vendor_available) && mctx.Vendor() {
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001309 mctx.PropertyErrorf("vendor_available",
1310 "doesn't make sense at the same time as `vendor: true` or `proprietary: true`")
1311 return
1312 }
Justin Yun8effde42017-06-23 19:24:43 +09001313 if vndk := m.vndkdep; vndk != nil {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001314 if vndk.isVndk() && !Bool(m.VendorProperties.Vendor_available) {
Justin Yun8effde42017-06-23 19:24:43 +09001315 mctx.PropertyErrorf("vndk",
1316 "has to define `vendor_available: true` to enable vndk")
1317 return
1318 }
1319 if !vndk.isVndk() && vndk.isVndkSp() {
1320 mctx.PropertyErrorf("vndk",
1321 "must set `enabled: true` to set `support_system_process: true`")
1322 return
1323 }
1324 }
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001325
1326 if !mctx.DeviceConfig().CompileVndk() {
1327 // If the device isn't compiling against the VNDK, we always
1328 // use the core mode.
1329 mctx.CreateVariations(coreMode)
1330 } else if _, ok := m.linker.(*llndkStubDecorator); ok {
1331 // LL-NDK stubs only exist in the vendor variant, since the
1332 // real libraries will be used in the core variant.
1333 mctx.CreateVariations(vendorMode)
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001334 } else if Bool(m.VendorProperties.Vendor_available) {
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001335 // This will be available in both /system and /vendor
Justin Yun8effde42017-06-23 19:24:43 +09001336 // or a /system directory that is available to vendor.
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001337 mod := mctx.CreateVariations(coreMode, vendorMode)
1338 mod[1].(*Module).Properties.UseVndk = true
1339 } else if mctx.Vendor() && m.Properties.Sdk_version == "" {
1340 // This will be available in /vendor only
1341 mod := mctx.CreateVariations(vendorMode)
1342 mod[0].(*Module).Properties.UseVndk = true
1343 } else {
1344 // This is either in /system (or similar: /data), or is a
1345 // modules built with the NDK. Modules built with the NDK
1346 // will be restricted using the existing link type checks.
1347 mctx.CreateVariations(coreMode)
1348 }
1349}
1350
Colin Crossdd84e052017-05-17 13:44:16 -07001351// firstUniqueElements returns all unique elements of a slice, keeping the first copy of each
1352// modifies the slice contents in place, and returns a subslice of the original slice
1353func firstUniqueElements(list []string) []string {
1354 k := 0
1355outer:
1356 for i := 0; i < len(list); i++ {
1357 for j := 0; j < k; j++ {
1358 if list[i] == list[j] {
1359 continue outer
1360 }
1361 }
1362 list[k] = list[i]
1363 k++
1364 }
1365 return list[:k]
1366}
1367
Colin Cross74d1ec02015-04-28 13:30:13 -07001368// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
1369// modifies the slice contents in place, and returns a subslice of the original slice
1370func lastUniqueElements(list []string) []string {
1371 totalSkip := 0
1372 for i := len(list) - 1; i >= totalSkip; i-- {
1373 skip := 0
1374 for j := i - 1; j >= totalSkip; j-- {
1375 if list[i] == list[j] {
1376 skip++
1377 } else {
1378 list[j+skip] = list[j]
1379 }
1380 }
1381 totalSkip += skip
1382 }
1383 return list[totalSkip:]
1384}
Colin Cross06a931b2015-10-28 17:23:31 -07001385
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -07001386func getCurrentNdkPrebuiltVersion(ctx DepsContext) string {
1387 if ctx.AConfig().PlatformSdkVersionInt() > config.NdkMaxPrebuiltVersionInt {
1388 return strconv.Itoa(config.NdkMaxPrebuiltVersionInt)
1389 }
1390 return ctx.AConfig().PlatformSdkVersion()
1391}
1392
Colin Cross06a931b2015-10-28 17:23:31 -07001393var Bool = proptools.Bool