blob: ea523ca6a4b4e953ef1f9854ce46af21c3406274 [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
Colin Crossc472d572015-03-17 15:06:21 -070077}
78
Colin Crossca860ac2016-01-04 14:34:37 -080079type PathDeps struct {
Colin Cross26c34ed2016-09-30 17:10:16 -070080 // Paths to .so files
81 SharedLibs, LateSharedLibs android.Paths
82 // Paths to the dependencies to use for .so files (.so.toc files)
83 SharedLibsDeps, LateSharedLibsDeps android.Paths
84 // Paths to .a files
Colin Cross635c3b02016-05-18 15:37:25 -070085 StaticLibs, LateStaticLibs, WholeStaticLibs android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -070086
Colin Cross26c34ed2016-09-30 17:10:16 -070087 // Paths to .o files
Dan Willemsen5cb580f2016-09-26 17:33:01 -070088 Objs Objects
Dan Willemsen581341d2017-02-09 16:16:31 -080089 StaticLibObjs Objects
Dan Willemsen5cb580f2016-09-26 17:33:01 -070090 WholeStaticLibObjs Objects
Dan Willemsen34cc69e2015-09-23 15:26:20 -070091
Colin Cross26c34ed2016-09-30 17:10:16 -070092 // Paths to generated source files
Colin Cross635c3b02016-05-18 15:37:25 -070093 GeneratedSources android.Paths
94 GeneratedHeaders android.Paths
Dan Willemsenb40aab62016-04-20 14:21:14 -070095
Dan Willemsen76f08272016-07-09 00:14:08 -070096 Flags, ReexportedFlags []string
Dan Willemsen847dcc72016-09-29 12:13:36 -070097 ReexportedFlagsDeps android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -070098
Colin Cross26c34ed2016-09-30 17:10:16 -070099 // Paths to crt*.o files
Colin Cross635c3b02016-05-18 15:37:25 -0700100 CrtBegin, CrtEnd android.OptionalPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700101}
102
Colin Crossca860ac2016-01-04 14:34:37 -0800103type Flags struct {
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700104 GlobalFlags []string // Flags that apply to C, C++, and assembly source files
105 ArFlags []string // Flags that apply to ar
106 AsFlags []string // Flags that apply to assembly source files
107 CFlags []string // Flags that apply to C and C++ source files
108 ToolingCFlags []string // Flags that apply to C and C++ source files parsed by clang LibTooling tools
109 ConlyFlags []string // Flags that apply to C source files
110 CppFlags []string // Flags that apply to C++ source files
111 ToolingCppFlags []string // Flags that apply to C++ source files parsed by clang LibTooling tools
112 YaccFlags []string // Flags that apply to Yacc source files
113 protoFlags []string // Flags that apply to proto source files
114 aidlFlags []string // Flags that apply to aidl source files
115 rsFlags []string // Flags that apply to renderscript source files
116 LdFlags []string // Flags that apply to linker command lines
117 libFlags []string // Flags to add libraries early to the link order
118 TidyFlags []string // Flags that apply to clang-tidy
119 SAbiFlags []string // Flags that apply to header-abi-dumper
120 YasmFlags []string // Flags that apply to yasm assembly source files
Colin Cross28344522015-04-22 13:07:53 -0700121
Colin Crossc3199482017-03-30 15:03:04 -0700122 // Global include flags that apply to C, C++, and assembly source files
123 // These must be after any module include flags, which will be in GlobalFlags.
124 SystemIncludeFlags []string
125
Colin Crossb98c8b02016-07-29 13:44:28 -0700126 Toolchain config.Toolchain
Colin Cross28344522015-04-22 13:07:53 -0700127 Clang bool
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700128 Tidy bool
Dan Willemsen581341d2017-02-09 16:16:31 -0800129 Coverage bool
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800130 SAbiDump bool
Colin Crossca860ac2016-01-04 14:34:37 -0800131
132 RequiredInstructionSet string
Colin Cross16b23492016-01-06 14:41:07 -0800133 DynamicLinker string
134
Colin Cross635c3b02016-05-18 15:37:25 -0700135 CFlagsDeps android.Paths // Files depended on by compiler flags
Colin Cross18c0c5a2016-12-01 14:45:23 -0800136
137 GroupStaticLibs bool
Colin Crossc472d572015-03-17 15:06:21 -0700138}
139
Colin Cross81413472016-04-11 14:37:39 -0700140type ObjectLinkerProperties struct {
141 // names of other cc_object modules to link into this module using partial linking
142 Objs []string `android:"arch_variant"`
143}
144
Colin Crossca860ac2016-01-04 14:34:37 -0800145// Properties used to compile all C or C++ modules
146type BaseProperties struct {
147 // compile module with clang instead of gcc
148 Clang *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700149
150 // Minimum sdk version supported when compiling against the ndk
151 Sdk_version string
152
Colin Crossca860ac2016-01-04 14:34:37 -0800153 // don't insert default compiler flags into asflags, cflags,
154 // cppflags, conlyflags, ldflags, or include_dirs
155 No_default_compiler_flags *bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700156
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700157 AndroidMkSharedLibs []string `blueprint:"mutated"`
158 HideFromMake bool `blueprint:"mutated"`
159 PreventInstall bool `blueprint:"mutated"`
160
161 UseVndk bool `blueprint:"mutated"`
162}
163
164type VendorProperties struct {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700165 // whether this module should be allowed to install onto /vendor as
166 // well as /system. The two variants will be built separately, one
167 // like normal, and the other limited to the set of libraries and
168 // headers that are exposed to /vendor modules.
169 //
170 // The vendor variant may be used with a different (newer) /system,
171 // so it shouldn't have any unversioned runtime dependencies, or
172 // make assumptions about the system that may not be true in the
173 // future.
174 //
175 // Nothing happens if BOARD_VNDK_VERSION isn't set in the BoardConfig.mk
176 Vendor_available *bool
Colin Crossca860ac2016-01-04 14:34:37 -0800177}
178
Colin Crossca860ac2016-01-04 14:34:37 -0800179type UnusedProperties struct {
Dan Willemsen581341d2017-02-09 16:16:31 -0800180 Tags []string
Colin Crosscfad1192015-11-02 16:43:11 -0800181}
182
Colin Crossca860ac2016-01-04 14:34:37 -0800183type ModuleContextIntf interface {
Colin Crossca860ac2016-01-04 14:34:37 -0800184 static() bool
185 staticBinary() bool
186 clang() bool
Colin Crossb98c8b02016-07-29 13:44:28 -0700187 toolchain() config.Toolchain
Colin Crossca860ac2016-01-04 14:34:37 -0800188 noDefaultCompilerFlags() bool
189 sdk() bool
190 sdkVersion() string
Dan Willemsend2ede872016-11-18 14:54:24 -0800191 vndk() bool
Justin Yun8effde42017-06-23 19:24:43 +0900192 isVndk() bool
193 isVndkSp() bool
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800194 createVndkSourceAbiDump() bool
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700195 selectedStl() string
Colin Crossce75d2c2016-10-06 16:12:58 -0700196 baseModuleName() string
Colin Crossca860ac2016-01-04 14:34:37 -0800197}
198
199type ModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700200 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800201 ModuleContextIntf
202}
203
204type BaseModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700205 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800206 ModuleContextIntf
207}
208
Colin Cross37047f12016-12-13 17:06:13 -0800209type DepsContext interface {
210 android.BottomUpMutatorContext
211 ModuleContextIntf
212}
213
Colin Crossca860ac2016-01-04 14:34:37 -0800214type feature interface {
215 begin(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800216 deps(ctx DepsContext, deps Deps) Deps
Colin Crossca860ac2016-01-04 14:34:37 -0800217 flags(ctx ModuleContext, flags Flags) Flags
218 props() []interface{}
219}
220
221type compiler interface {
Colin Cross42742b82016-08-01 13:20:05 -0700222 compilerInit(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800223 compilerDeps(ctx DepsContext, deps Deps) Deps
Colin Cross42742b82016-08-01 13:20:05 -0700224 compilerFlags(ctx ModuleContext, flags Flags) Flags
225 compilerProps() []interface{}
226
Colin Cross76fada02016-07-27 10:31:13 -0700227 appendCflags([]string)
228 appendAsflags([]string)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700229 compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects
Colin Crossca860ac2016-01-04 14:34:37 -0800230}
231
232type linker interface {
Colin Cross42742b82016-08-01 13:20:05 -0700233 linkerInit(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800234 linkerDeps(ctx DepsContext, deps Deps) Deps
Colin Cross42742b82016-08-01 13:20:05 -0700235 linkerFlags(ctx ModuleContext, flags Flags) Flags
236 linkerProps() []interface{}
237
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700238 link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path
Colin Cross76fada02016-07-27 10:31:13 -0700239 appendLdflags([]string)
Colin Crossca860ac2016-01-04 14:34:37 -0800240}
241
242type installer interface {
Colin Cross42742b82016-08-01 13:20:05 -0700243 installerProps() []interface{}
Colin Cross635c3b02016-05-18 15:37:25 -0700244 install(ctx ModuleContext, path android.Path)
Colin Crossca860ac2016-01-04 14:34:37 -0800245 inData() bool
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700246 inSanitizerDir() bool
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700247 hostToolPath() android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800248}
249
Colin Crossc99deeb2016-04-11 15:06:20 -0700250type dependencyTag struct {
251 blueprint.BaseDependencyTag
252 name string
253 library bool
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700254
255 reexportFlags bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700256}
257
258var (
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700259 sharedDepTag = dependencyTag{name: "shared", library: true}
260 sharedExportDepTag = dependencyTag{name: "shared", library: true, reexportFlags: true}
261 lateSharedDepTag = dependencyTag{name: "late shared", library: true}
262 staticDepTag = dependencyTag{name: "static", library: true}
263 staticExportDepTag = dependencyTag{name: "static", library: true, reexportFlags: true}
264 lateStaticDepTag = dependencyTag{name: "late static", library: true}
265 wholeStaticDepTag = dependencyTag{name: "whole static", library: true, reexportFlags: true}
Colin Cross32ec36c2016-12-15 07:39:51 -0800266 headerDepTag = dependencyTag{name: "header", library: true}
267 headerExportDepTag = dependencyTag{name: "header", library: true, reexportFlags: true}
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700268 genSourceDepTag = dependencyTag{name: "gen source"}
269 genHeaderDepTag = dependencyTag{name: "gen header"}
270 genHeaderExportDepTag = dependencyTag{name: "gen header", reexportFlags: true}
271 objDepTag = dependencyTag{name: "obj"}
272 crtBeginDepTag = dependencyTag{name: "crtbegin"}
273 crtEndDepTag = dependencyTag{name: "crtend"}
274 reuseObjTag = dependencyTag{name: "reuse objects"}
275 ndkStubDepTag = dependencyTag{name: "ndk stub", library: true}
276 ndkLateStubDepTag = dependencyTag{name: "ndk late stub", library: true}
Colin Crossc99deeb2016-04-11 15:06:20 -0700277)
278
Colin Crossca860ac2016-01-04 14:34:37 -0800279// Module contains the properties and members used by all C/C++ module types, and implements
280// the blueprint.Module interface. It delegates to compiler, linker, and installer interfaces
281// to construct the output file. Behavior can be customized with a Customizer interface
282type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700283 android.ModuleBase
Colin Cross1f44a3a2017-07-07 14:33:33 -0700284 android.DefaultableModuleBase
Colin Crossc472d572015-03-17 15:06:21 -0700285
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700286 Properties BaseProperties
287 VendorProperties VendorProperties
288 unused UnusedProperties
Colin Crossfa138792015-04-24 17:31:52 -0700289
Colin Crossca860ac2016-01-04 14:34:37 -0800290 // initialize before calling Init
Colin Cross635c3b02016-05-18 15:37:25 -0700291 hod android.HostOrDeviceSupported
292 multilib android.Multilib
Colin Crossc472d572015-03-17 15:06:21 -0700293
Colin Crossca860ac2016-01-04 14:34:37 -0800294 // delegates, initialize before calling Init
Colin Crossb4ce0ec2016-09-13 13:41:39 -0700295 features []feature
296 compiler compiler
297 linker linker
298 installer installer
299 stl *stl
300 sanitize *sanitize
Dan Willemsen581341d2017-02-09 16:16:31 -0800301 coverage *coverage
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800302 sabi *sabi
Justin Yun8effde42017-06-23 19:24:43 +0900303 vndkdep *vndkdep
Stephen Craneba090d12017-05-09 15:44:35 -0700304 lto *lto
Colin Cross16b23492016-01-06 14:41:07 -0800305
306 androidMkSharedLibDeps []string
Colin Cross74d1ec02015-04-28 13:30:13 -0700307
Colin Cross635c3b02016-05-18 15:37:25 -0700308 outputFile android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800309
Colin Crossb98c8b02016-07-29 13:44:28 -0700310 cachedToolchain config.Toolchain
Colin Crossb916a382016-07-29 17:28:03 -0700311
312 subAndroidMkOnce map[subAndroidMkProvider]bool
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800313
314 // Flags used to compile this module
315 flags Flags
Colin Crossc472d572015-03-17 15:06:21 -0700316}
317
Colin Cross36242852017-06-23 15:06:31 -0700318func (c *Module) Init() android.Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700319 c.AddProperties(&c.Properties, &c.VendorProperties, &c.unused)
Colin Crossca860ac2016-01-04 14:34:37 -0800320 if c.compiler != nil {
Colin Cross36242852017-06-23 15:06:31 -0700321 c.AddProperties(c.compiler.compilerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800322 }
323 if c.linker != nil {
Colin Cross36242852017-06-23 15:06:31 -0700324 c.AddProperties(c.linker.linkerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800325 }
326 if c.installer != nil {
Colin Cross36242852017-06-23 15:06:31 -0700327 c.AddProperties(c.installer.installerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800328 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700329 if c.stl != nil {
Colin Cross36242852017-06-23 15:06:31 -0700330 c.AddProperties(c.stl.props()...)
Colin Crossa8e07cc2016-04-04 15:07:06 -0700331 }
Colin Cross16b23492016-01-06 14:41:07 -0800332 if c.sanitize != nil {
Colin Cross36242852017-06-23 15:06:31 -0700333 c.AddProperties(c.sanitize.props()...)
Colin Cross16b23492016-01-06 14:41:07 -0800334 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800335 if c.coverage != nil {
Colin Cross36242852017-06-23 15:06:31 -0700336 c.AddProperties(c.coverage.props()...)
Dan Willemsen581341d2017-02-09 16:16:31 -0800337 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800338 if c.sabi != nil {
Colin Cross36242852017-06-23 15:06:31 -0700339 c.AddProperties(c.sabi.props()...)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800340 }
Justin Yun8effde42017-06-23 19:24:43 +0900341 if c.vndkdep != nil {
342 c.AddProperties(c.vndkdep.props()...)
343 }
Stephen Craneba090d12017-05-09 15:44:35 -0700344 if c.lto != nil {
345 c.AddProperties(c.lto.props()...)
346 }
Colin Crossca860ac2016-01-04 14:34:37 -0800347 for _, feature := range c.features {
Colin Cross36242852017-06-23 15:06:31 -0700348 c.AddProperties(feature.props()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800349 }
Colin Crossc472d572015-03-17 15:06:21 -0700350
Colin Cross36242852017-06-23 15:06:31 -0700351 android.InitAndroidArchModule(c, c.hod, c.multilib)
Colin Crossc472d572015-03-17 15:06:21 -0700352
Colin Cross1f44a3a2017-07-07 14:33:33 -0700353 android.InitDefaultableModule(c)
Colin Cross36242852017-06-23 15:06:31 -0700354
355 return c
Colin Crossc472d572015-03-17 15:06:21 -0700356}
357
Colin Crossb916a382016-07-29 17:28:03 -0700358// Returns true for dependency roots (binaries)
359// TODO(ccross): also handle dlopenable libraries
360func (c *Module) isDependencyRoot() bool {
361 if root, ok := c.linker.(interface {
362 isDependencyRoot() bool
363 }); ok {
364 return root.isDependencyRoot()
365 }
366 return false
367}
368
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700369func (c *Module) vndk() bool {
370 return c.Properties.UseVndk
371}
372
Justin Yun8effde42017-06-23 19:24:43 +0900373func (c *Module) isVndk() bool {
374 if c.vndkdep != nil {
375 return c.vndkdep.isVndk()
376 }
377 return false
378}
379
Colin Crossca860ac2016-01-04 14:34:37 -0800380type baseModuleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700381 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800382 moduleContextImpl
383}
384
Colin Cross37047f12016-12-13 17:06:13 -0800385type depsContext struct {
386 android.BottomUpMutatorContext
387 moduleContextImpl
388}
389
Colin Crossca860ac2016-01-04 14:34:37 -0800390type moduleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700391 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800392 moduleContextImpl
393}
394
Justin Yun8effde42017-06-23 19:24:43 +0900395// Vendor returns true for vendor modules excluding VNDK libraries so that
396// they get installed onto the correct partition
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700397func (ctx *moduleContext) Vendor() bool {
Justin Yun8effde42017-06-23 19:24:43 +0900398 return ctx.ModuleContext.Vendor() || (ctx.mod.vndk() && !ctx.mod.isVndk())
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700399}
400
Colin Crossca860ac2016-01-04 14:34:37 -0800401type moduleContextImpl struct {
402 mod *Module
403 ctx BaseModuleContext
404}
405
Colin Crossca860ac2016-01-04 14:34:37 -0800406func (ctx *moduleContextImpl) clang() bool {
407 return ctx.mod.clang(ctx.ctx)
408}
409
Colin Crossb98c8b02016-07-29 13:44:28 -0700410func (ctx *moduleContextImpl) toolchain() config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800411 return ctx.mod.toolchain(ctx.ctx)
412}
413
414func (ctx *moduleContextImpl) static() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700415 if static, ok := ctx.mod.linker.(interface {
416 static() bool
417 }); ok {
418 return static.static()
Colin Crossca860ac2016-01-04 14:34:37 -0800419 }
Colin Crossb916a382016-07-29 17:28:03 -0700420 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800421}
422
423func (ctx *moduleContextImpl) staticBinary() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700424 if static, ok := ctx.mod.linker.(interface {
425 staticBinary() bool
426 }); ok {
427 return static.staticBinary()
Colin Crossca860ac2016-01-04 14:34:37 -0800428 }
Colin Crossb916a382016-07-29 17:28:03 -0700429 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800430}
431
432func (ctx *moduleContextImpl) noDefaultCompilerFlags() bool {
433 return Bool(ctx.mod.Properties.No_default_compiler_flags)
434}
435
436func (ctx *moduleContextImpl) sdk() bool {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700437 if ctx.ctx.Device() && !ctx.vndk() {
Dan Willemsena96ff642016-06-07 12:34:45 -0700438 return ctx.mod.Properties.Sdk_version != ""
439 }
440 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800441}
442
443func (ctx *moduleContextImpl) sdkVersion() string {
Dan Willemsena96ff642016-06-07 12:34:45 -0700444 if ctx.ctx.Device() {
Dan Willemsen11b26142017-03-19 18:30:37 -0700445 if ctx.vndk() {
446 return "current"
Dan Willemsend2ede872016-11-18 14:54:24 -0800447 } else {
448 return ctx.mod.Properties.Sdk_version
449 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700450 }
451 return ""
Colin Crossca860ac2016-01-04 14:34:37 -0800452}
453
Dan Willemsend2ede872016-11-18 14:54:24 -0800454func (ctx *moduleContextImpl) vndk() bool {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700455 return ctx.mod.vndk()
Dan Willemsend2ede872016-11-18 14:54:24 -0800456}
457
Justin Yun8effde42017-06-23 19:24:43 +0900458func (ctx *moduleContextImpl) isVndk() bool {
459 return ctx.mod.isVndk()
460}
461
462func (ctx *moduleContextImpl) isVndkSp() bool {
463 if vndk := ctx.mod.vndkdep; vndk != nil {
464 return vndk.isVndkSp()
465 }
466 return false
467}
468
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800469// Create source abi dumps if the module belongs to the list of VndkLibraries.
470func (ctx *moduleContextImpl) createVndkSourceAbiDump() bool {
Jayant Chowdharya4fce192017-09-06 13:10:03 -0700471 return ctx.ctx.Device() && ((ctx.vndk() && ctx.isVndk()) || inList(ctx.baseModuleName(), llndkLibraries))
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800472}
473
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700474func (ctx *moduleContextImpl) selectedStl() string {
475 if stl := ctx.mod.stl; stl != nil {
476 return stl.Properties.SelectedStl
477 }
478 return ""
479}
480
Colin Crossce75d2c2016-10-06 16:12:58 -0700481func (ctx *moduleContextImpl) baseModuleName() string {
482 return ctx.mod.ModuleBase.BaseModuleName()
483}
484
Colin Cross635c3b02016-05-18 15:37:25 -0700485func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800486 return &Module{
487 hod: hod,
488 multilib: multilib,
489 }
490}
491
Colin Cross635c3b02016-05-18 15:37:25 -0700492func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800493 module := newBaseModule(hod, multilib)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700494 module.features = []feature{
495 &tidyFeature{},
496 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700497 module.stl = &stl{}
Colin Cross16b23492016-01-06 14:41:07 -0800498 module.sanitize = &sanitize{}
Dan Willemsen581341d2017-02-09 16:16:31 -0800499 module.coverage = &coverage{}
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800500 module.sabi = &sabi{}
Justin Yun8effde42017-06-23 19:24:43 +0900501 module.vndkdep = &vndkdep{}
Stephen Craneba090d12017-05-09 15:44:35 -0700502 module.lto = &lto{}
Colin Crossca860ac2016-01-04 14:34:37 -0800503 return module
504}
505
Colin Crossce75d2c2016-10-06 16:12:58 -0700506func (c *Module) Prebuilt() *android.Prebuilt {
507 if p, ok := c.linker.(prebuiltLinkerInterface); ok {
508 return p.prebuilt()
509 }
510 return nil
511}
512
513func (c *Module) Name() string {
514 name := c.ModuleBase.Name()
Dan Willemsen01a90592017-04-07 15:21:13 -0700515 if p, ok := c.linker.(interface {
516 Name(string) string
517 }); ok {
Colin Crossce75d2c2016-10-06 16:12:58 -0700518 name = p.Name(name)
519 }
520 return name
521}
522
Colin Cross635c3b02016-05-18 15:37:25 -0700523func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800524 ctx := &moduleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700525 ModuleContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800526 moduleContextImpl: moduleContextImpl{
527 mod: c,
528 },
529 }
530 ctx.ctx = ctx
531
532 flags := Flags{
533 Toolchain: c.toolchain(ctx),
534 Clang: c.clang(ctx),
535 }
Colin Crossca860ac2016-01-04 14:34:37 -0800536 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700537 flags = c.compiler.compilerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800538 }
539 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700540 flags = c.linker.linkerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800541 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700542 if c.stl != nil {
543 flags = c.stl.flags(ctx, flags)
544 }
Colin Cross16b23492016-01-06 14:41:07 -0800545 if c.sanitize != nil {
546 flags = c.sanitize.flags(ctx, flags)
547 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800548 if c.coverage != nil {
549 flags = c.coverage.flags(ctx, flags)
550 }
Stephen Craneba090d12017-05-09 15:44:35 -0700551 if c.lto != nil {
552 flags = c.lto.flags(ctx, flags)
553 }
Colin Crossca860ac2016-01-04 14:34:37 -0800554 for _, feature := range c.features {
555 flags = feature.flags(ctx, flags)
556 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800557 if ctx.Failed() {
558 return
559 }
560
Colin Crossb98c8b02016-07-29 13:44:28 -0700561 flags.CFlags, _ = filterList(flags.CFlags, config.IllegalFlags)
562 flags.CppFlags, _ = filterList(flags.CppFlags, config.IllegalFlags)
563 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, config.IllegalFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800564
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800565 deps := c.depsToPaths(ctx)
566 if ctx.Failed() {
567 return
568 }
569 flags.GlobalFlags = append(flags.GlobalFlags, deps.Flags...)
570 c.flags = flags
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700571 // We need access to all the flags seen by a source file.
572 if c.sabi != nil {
573 flags = c.sabi.flags(ctx, flags)
574 }
Colin Crossca860ac2016-01-04 14:34:37 -0800575 // Optimization to reduce size of build.ninja
576 // Replace the long list of flags for each file with a module-local variable
577 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
578 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
579 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
580 flags.CFlags = []string{"$cflags"}
581 flags.CppFlags = []string{"$cppflags"}
582 flags.AsFlags = []string{"$asflags"}
583
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700584 var objs Objects
Colin Crossca860ac2016-01-04 14:34:37 -0800585 if c.compiler != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700586 objs = c.compiler.compile(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -0800587 if ctx.Failed() {
588 return
589 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800590 }
591
Colin Crossca860ac2016-01-04 14:34:37 -0800592 if c.linker != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700593 outputFile := c.linker.link(ctx, flags, deps, objs)
Colin Crossca860ac2016-01-04 14:34:37 -0800594 if ctx.Failed() {
595 return
596 }
Colin Cross635c3b02016-05-18 15:37:25 -0700597 c.outputFile = android.OptionalPathForPath(outputFile)
Colin Crossce75d2c2016-10-06 16:12:58 -0700598 }
Colin Cross5049f022015-03-18 13:28:46 -0700599
Colin Crossce75d2c2016-10-06 16:12:58 -0700600 if c.installer != nil && !c.Properties.PreventInstall && c.outputFile.Valid() {
601 c.installer.install(ctx, c.outputFile.Path())
602 if ctx.Failed() {
603 return
Colin Crossca860ac2016-01-04 14:34:37 -0800604 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700605 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800606}
607
Colin Crossb98c8b02016-07-29 13:44:28 -0700608func (c *Module) toolchain(ctx BaseModuleContext) config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800609 if c.cachedToolchain == nil {
Colin Crossb98c8b02016-07-29 13:44:28 -0700610 c.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
Colin Cross3f40fa42015-01-30 17:27:36 -0800611 }
Colin Crossca860ac2016-01-04 14:34:37 -0800612 return c.cachedToolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800613}
614
Colin Crossca860ac2016-01-04 14:34:37 -0800615func (c *Module) begin(ctx BaseModuleContext) {
616 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700617 c.compiler.compilerInit(ctx)
Colin Cross21b9a242015-03-24 14:15:58 -0700618 }
Colin Crossca860ac2016-01-04 14:34:37 -0800619 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700620 c.linker.linkerInit(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800621 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700622 if c.stl != nil {
623 c.stl.begin(ctx)
624 }
Colin Cross16b23492016-01-06 14:41:07 -0800625 if c.sanitize != nil {
626 c.sanitize.begin(ctx)
627 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800628 if c.coverage != nil {
629 c.coverage.begin(ctx)
630 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800631 if c.sabi != nil {
632 c.sabi.begin(ctx)
633 }
Justin Yun8effde42017-06-23 19:24:43 +0900634 if c.vndkdep != nil {
635 c.vndkdep.begin(ctx)
636 }
Stephen Craneba090d12017-05-09 15:44:35 -0700637 if c.lto != nil {
638 c.lto.begin(ctx)
639 }
Colin Crossca860ac2016-01-04 14:34:37 -0800640 for _, feature := range c.features {
641 feature.begin(ctx)
642 }
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700643 if ctx.sdk() {
Dan Albertf5415d72017-08-17 16:19:59 -0700644 version, err := normalizeNdkApiLevel(ctx, ctx.sdkVersion(), ctx.Arch())
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700645 if err != nil {
646 ctx.PropertyErrorf("sdk_version", err.Error())
647 }
Dan Albert90f7a4d2016-11-08 14:34:24 -0800648 c.Properties.Sdk_version = version
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700649 }
Colin Crossca860ac2016-01-04 14:34:37 -0800650}
651
Colin Cross37047f12016-12-13 17:06:13 -0800652func (c *Module) deps(ctx DepsContext) Deps {
Colin Crossc99deeb2016-04-11 15:06:20 -0700653 deps := Deps{}
654
655 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700656 deps = c.compiler.compilerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700657 }
658 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700659 deps = c.linker.linkerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700660 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700661 if c.stl != nil {
662 deps = c.stl.deps(ctx, deps)
663 }
Colin Cross16b23492016-01-06 14:41:07 -0800664 if c.sanitize != nil {
665 deps = c.sanitize.deps(ctx, deps)
666 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800667 if c.coverage != nil {
668 deps = c.coverage.deps(ctx, deps)
669 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800670 if c.sabi != nil {
671 deps = c.sabi.deps(ctx, deps)
672 }
Justin Yun8effde42017-06-23 19:24:43 +0900673 if c.vndkdep != nil {
674 deps = c.vndkdep.deps(ctx, deps)
675 }
Stephen Craneba090d12017-05-09 15:44:35 -0700676 if c.lto != nil {
677 deps = c.lto.deps(ctx, deps)
678 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700679 for _, feature := range c.features {
680 deps = feature.deps(ctx, deps)
681 }
682
683 deps.WholeStaticLibs = lastUniqueElements(deps.WholeStaticLibs)
684 deps.StaticLibs = lastUniqueElements(deps.StaticLibs)
685 deps.LateStaticLibs = lastUniqueElements(deps.LateStaticLibs)
686 deps.SharedLibs = lastUniqueElements(deps.SharedLibs)
687 deps.LateSharedLibs = lastUniqueElements(deps.LateSharedLibs)
Colin Cross5950f382016-12-13 12:50:57 -0800688 deps.HeaderLibs = lastUniqueElements(deps.HeaderLibs)
Colin Crossc99deeb2016-04-11 15:06:20 -0700689
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700690 for _, lib := range deps.ReexportSharedLibHeaders {
691 if !inList(lib, deps.SharedLibs) {
692 ctx.PropertyErrorf("export_shared_lib_headers", "Shared library not in shared_libs: '%s'", lib)
693 }
694 }
695
696 for _, lib := range deps.ReexportStaticLibHeaders {
697 if !inList(lib, deps.StaticLibs) {
698 ctx.PropertyErrorf("export_static_lib_headers", "Static library not in static_libs: '%s'", lib)
699 }
700 }
701
Colin Cross5950f382016-12-13 12:50:57 -0800702 for _, lib := range deps.ReexportHeaderLibHeaders {
703 if !inList(lib, deps.HeaderLibs) {
704 ctx.PropertyErrorf("export_header_lib_headers", "Header library not in header_libs: '%s'", lib)
705 }
706 }
707
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700708 for _, gen := range deps.ReexportGeneratedHeaders {
709 if !inList(gen, deps.GeneratedHeaders) {
710 ctx.PropertyErrorf("export_generated_headers", "Generated header module not in generated_headers: '%s'", gen)
711 }
712 }
713
Colin Crossc99deeb2016-04-11 15:06:20 -0700714 return deps
715}
716
Dan Albert7e9d2952016-08-04 13:02:36 -0700717func (c *Module) beginMutator(actx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800718 ctx := &baseModuleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700719 BaseContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800720 moduleContextImpl: moduleContextImpl{
721 mod: c,
722 },
723 }
724 ctx.ctx = ctx
725
Colin Crossca860ac2016-01-04 14:34:37 -0800726 c.begin(ctx)
Dan Albert7e9d2952016-08-04 13:02:36 -0700727}
728
Colin Cross1e676be2016-10-12 14:38:15 -0700729func (c *Module) DepsMutator(actx android.BottomUpMutatorContext) {
730 if !c.Enabled() {
731 return
732 }
733
Colin Cross37047f12016-12-13 17:06:13 -0800734 ctx := &depsContext{
735 BottomUpMutatorContext: actx,
Dan Albert7e9d2952016-08-04 13:02:36 -0700736 moduleContextImpl: moduleContextImpl{
737 mod: c,
738 },
739 }
740 ctx.ctx = ctx
Colin Crossca860ac2016-01-04 14:34:37 -0800741
Colin Crossc99deeb2016-04-11 15:06:20 -0700742 deps := c.deps(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800743
Dan Albert914449f2016-06-17 16:45:24 -0700744 variantNdkLibs := []string{}
745 variantLateNdkLibs := []string{}
Dan Willemsenb916b802017-03-19 13:44:32 -0700746 if ctx.Os() == android.Android {
Dan Albert914449f2016-06-17 16:45:24 -0700747 version := ctx.sdkVersion()
Dan Willemsen72d39932016-07-08 23:23:48 -0700748
Dan Albert914449f2016-06-17 16:45:24 -0700749 // Rewrites the names of shared libraries into the names of the NDK
750 // libraries where appropriate. This returns two slices.
751 //
752 // The first is a list of non-variant shared libraries (either rewritten
753 // NDK libraries to the modules in prebuilts/ndk, or not rewritten
754 // because they are not NDK libraries).
755 //
756 // The second is a list of ndk_library modules. These need to be
757 // separated because they are a variation dependency and must be added
758 // in a different manner.
759 rewriteNdkLibs := func(list []string) ([]string, []string) {
760 variantLibs := []string{}
761 nonvariantLibs := []string{}
762 for _, entry := range list {
Dan Willemsenb916b802017-03-19 13:44:32 -0700763 if ctx.sdk() && inList(entry, ndkPrebuiltSharedLibraries) {
Dan Albert914449f2016-06-17 16:45:24 -0700764 if !inList(entry, ndkMigratedLibs) {
765 nonvariantLibs = append(nonvariantLibs, entry+".ndk."+version)
766 } else {
767 variantLibs = append(variantLibs, entry+ndkLibrarySuffix)
768 }
Jiyong Parkd5b18a52017-08-03 21:22:50 +0900769 } else if ctx.vndk() && inList(entry, llndkLibraries) {
Dan Willemsenb916b802017-03-19 13:44:32 -0700770 nonvariantLibs = append(nonvariantLibs, entry+llndkLibrarySuffix)
Dan Albert914449f2016-06-17 16:45:24 -0700771 } else {
Dan Willemsen7cbf5f82017-03-28 00:08:30 -0700772 nonvariantLibs = append(nonvariantLibs, entry)
Dan Willemsen72d39932016-07-08 23:23:48 -0700773 }
774 }
Dan Albert914449f2016-06-17 16:45:24 -0700775 return nonvariantLibs, variantLibs
Dan Willemsen72d39932016-07-08 23:23:48 -0700776 }
777
Dan Albert914449f2016-06-17 16:45:24 -0700778 deps.SharedLibs, variantNdkLibs = rewriteNdkLibs(deps.SharedLibs)
779 deps.LateSharedLibs, variantLateNdkLibs = rewriteNdkLibs(deps.LateSharedLibs)
Jiyong Park4c35af02017-07-05 13:41:55 +0900780 deps.ReexportSharedLibHeaders, _ = rewriteNdkLibs(deps.ReexportSharedLibHeaders)
Dan Willemsen72d39932016-07-08 23:23:48 -0700781 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700782
Colin Cross32ec36c2016-12-15 07:39:51 -0800783 for _, lib := range deps.HeaderLibs {
784 depTag := headerDepTag
785 if inList(lib, deps.ReexportHeaderLibHeaders) {
786 depTag = headerExportDepTag
787 }
788 actx.AddVariationDependencies(nil, depTag, lib)
789 }
Colin Cross5950f382016-12-13 12:50:57 -0800790
Colin Crossc99deeb2016-04-11 15:06:20 -0700791 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, wholeStaticDepTag,
792 deps.WholeStaticLibs...)
793
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700794 for _, lib := range deps.StaticLibs {
795 depTag := staticDepTag
796 if inList(lib, deps.ReexportStaticLibHeaders) {
797 depTag = staticExportDepTag
798 }
Colin Cross15a0d462016-07-14 14:49:58 -0700799 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700800 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700801
802 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, lateStaticDepTag,
803 deps.LateStaticLibs...)
804
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700805 for _, lib := range deps.SharedLibs {
806 depTag := sharedDepTag
807 if inList(lib, deps.ReexportSharedLibHeaders) {
808 depTag = sharedExportDepTag
809 }
Colin Cross15a0d462016-07-14 14:49:58 -0700810 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700811 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700812
813 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, lateSharedDepTag,
814 deps.LateSharedLibs...)
815
Colin Cross68861832016-07-08 10:41:41 -0700816 actx.AddDependency(c, genSourceDepTag, deps.GeneratedSources...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700817
818 for _, gen := range deps.GeneratedHeaders {
819 depTag := genHeaderDepTag
820 if inList(gen, deps.ReexportGeneratedHeaders) {
821 depTag = genHeaderExportDepTag
822 }
823 actx.AddDependency(c, depTag, gen)
824 }
Dan Willemsenb40aab62016-04-20 14:21:14 -0700825
Colin Cross68861832016-07-08 10:41:41 -0700826 actx.AddDependency(c, objDepTag, deps.ObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700827
828 if deps.CrtBegin != "" {
Colin Cross68861832016-07-08 10:41:41 -0700829 actx.AddDependency(c, crtBeginDepTag, deps.CrtBegin)
Colin Crossca860ac2016-01-04 14:34:37 -0800830 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700831 if deps.CrtEnd != "" {
Colin Cross68861832016-07-08 10:41:41 -0700832 actx.AddDependency(c, crtEndDepTag, deps.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700833 }
Dan Albert914449f2016-06-17 16:45:24 -0700834
835 version := ctx.sdkVersion()
836 actx.AddVariationDependencies([]blueprint.Variation{
837 {"ndk_api", version}, {"link", "shared"}}, ndkStubDepTag, variantNdkLibs...)
838 actx.AddVariationDependencies([]blueprint.Variation{
839 {"ndk_api", version}, {"link", "shared"}}, ndkLateStubDepTag, variantLateNdkLibs...)
Colin Cross6362e272015-10-29 15:25:03 -0700840}
Colin Cross21b9a242015-03-24 14:15:58 -0700841
Dan Albert7e9d2952016-08-04 13:02:36 -0700842func beginMutator(ctx android.BottomUpMutatorContext) {
843 if c, ok := ctx.Module().(*Module); ok && c.Enabled() {
844 c.beginMutator(ctx)
845 }
846}
847
Colin Crossca860ac2016-01-04 14:34:37 -0800848func (c *Module) clang(ctx BaseModuleContext) bool {
849 clang := Bool(c.Properties.Clang)
850
851 if c.Properties.Clang == nil {
852 if ctx.Host() {
853 clang = true
854 }
855
856 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
857 clang = true
858 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800859 }
Colin Cross28344522015-04-22 13:07:53 -0700860
Colin Crossca860ac2016-01-04 14:34:37 -0800861 if !c.toolchain(ctx).ClangSupported() {
862 clang = false
863 }
864
865 return clang
866}
867
Colin Crossc99deeb2016-04-11 15:06:20 -0700868// Convert dependencies to paths. Returns a PathDeps containing paths
Colin Cross635c3b02016-05-18 15:37:25 -0700869func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
Colin Crossca860ac2016-01-04 14:34:37 -0800870 var depPaths PathDeps
Colin Crossca860ac2016-01-04 14:34:37 -0800871
Dan Willemsena96ff642016-06-07 12:34:45 -0700872 // Whether a module can link to another module, taking into
873 // account NDK linking.
Dan Albert9e10cd42016-08-03 14:12:14 -0700874 checkLinkType := func(from, to *Module) {
Dan Willemsena96ff642016-06-07 12:34:45 -0700875 if from.Target().Os != android.Android {
876 // Host code is not restricted
Dan Albert9e10cd42016-08-03 14:12:14 -0700877 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700878 }
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700879 if from.Properties.UseVndk {
Justin Yun8effde42017-06-23 19:24:43 +0900880 // Though vendor code is limited by the vendor mutator,
881 // each vendor-available module needs to check
882 // link-type for VNDK.
883 if from.vndkdep != nil {
884 from.vndkdep.vndkCheckLinkType(ctx, to)
885 }
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700886 return
887 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700888 if from.Properties.Sdk_version == "" {
889 // Platform code can link to anything
Dan Albert9e10cd42016-08-03 14:12:14 -0700890 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700891 }
Colin Crossb916a382016-07-29 17:28:03 -0700892 if _, ok := to.linker.(*toolchainLibraryDecorator); ok {
Dan Willemsena96ff642016-06-07 12:34:45 -0700893 // These are always allowed
Dan Albert9e10cd42016-08-03 14:12:14 -0700894 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700895 }
896 if _, ok := to.linker.(*ndkPrebuiltLibraryLinker); ok {
897 // These are allowed, but don't set sdk_version
Dan Albert9e10cd42016-08-03 14:12:14 -0700898 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700899 }
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700900 if _, ok := to.linker.(*ndkPrebuiltStlLinker); ok {
901 // These are allowed, but don't set sdk_version
Dan Albert9e10cd42016-08-03 14:12:14 -0700902 return
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700903 }
Colin Crossb916a382016-07-29 17:28:03 -0700904 if _, ok := to.linker.(*stubDecorator); ok {
Dan Albert914449f2016-06-17 16:45:24 -0700905 // These aren't real libraries, but are the stub shared libraries that are included in
906 // the NDK.
Dan Albert9e10cd42016-08-03 14:12:14 -0700907 return
Dan Albert914449f2016-06-17 16:45:24 -0700908 }
Dan Albert9e10cd42016-08-03 14:12:14 -0700909 if to.Properties.Sdk_version == "" {
910 // NDK code linking to platform code is never okay.
911 ctx.ModuleErrorf("depends on non-NDK-built library %q",
912 ctx.OtherModuleName(to))
913 }
914
915 // All this point we know we have two NDK libraries, but we need to
916 // check that we're not linking against anything built against a higher
917 // API level, as it is only valid to link against older or equivalent
918 // APIs.
919
920 if from.Properties.Sdk_version == "current" {
921 // Current can link against anything.
922 return
923 } else if to.Properties.Sdk_version == "current" {
924 // Current can't be linked against by anything else.
925 ctx.ModuleErrorf("links %q built against newer API version %q",
926 ctx.OtherModuleName(to), "current")
927 }
928
929 fromApi, err := strconv.Atoi(from.Properties.Sdk_version)
930 if err != nil {
931 ctx.PropertyErrorf("sdk_version",
932 "Invalid sdk_version value (must be int): %q",
933 from.Properties.Sdk_version)
934 }
935 toApi, err := strconv.Atoi(to.Properties.Sdk_version)
936 if err != nil {
937 ctx.PropertyErrorf("sdk_version",
938 "Invalid sdk_version value (must be int): %q",
939 to.Properties.Sdk_version)
940 }
941
942 if toApi > fromApi {
943 ctx.ModuleErrorf("links %q built against newer API version %q",
944 ctx.OtherModuleName(to), to.Properties.Sdk_version)
945 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700946 }
947
Colin Crossc99deeb2016-04-11 15:06:20 -0700948 ctx.VisitDirectDeps(func(m blueprint.Module) {
949 name := ctx.OtherModuleName(m)
950 tag := ctx.OtherModuleDependencyTag(m)
Colin Crossca860ac2016-01-04 14:34:37 -0800951
Colin Cross635c3b02016-05-18 15:37:25 -0700952 a, _ := m.(android.Module)
Colin Crossc99deeb2016-04-11 15:06:20 -0700953 if a == nil {
954 ctx.ModuleErrorf("module %q not an android module", name)
955 return
Colin Crossca860ac2016-01-04 14:34:37 -0800956 }
Colin Crossca860ac2016-01-04 14:34:37 -0800957
Dan Willemsena96ff642016-06-07 12:34:45 -0700958 cc, _ := m.(*Module)
959 if cc == nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700960 switch tag {
Colin Cross068e0fe2016-12-13 15:23:47 -0800961 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700962 // Nothing to do
Dan Willemsenb40aab62016-04-20 14:21:14 -0700963 case genSourceDepTag:
964 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
965 depPaths.GeneratedSources = append(depPaths.GeneratedSources,
966 genRule.GeneratedSourceFiles()...)
967 } else {
968 ctx.ModuleErrorf("module %q is not a gensrcs or genrule", name)
969 }
Colin Crosse90bfd12017-04-26 16:59:26 -0700970 // Support exported headers from a generated_sources dependency
971 fallthrough
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700972 case genHeaderDepTag, genHeaderExportDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700973 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
974 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders,
975 genRule.GeneratedSourceFiles()...)
Colin Cross5ed99c62016-11-22 12:55:55 -0800976 flags := includeDirsToFlags(genRule.GeneratedHeaderDirs())
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700977 depPaths.Flags = append(depPaths.Flags, flags)
978 if tag == genHeaderExportDepTag {
979 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700980 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps,
981 genRule.GeneratedSourceFiles()...)
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700982 // Add these re-exported flags to help header-abi-dumper to infer the abi exported by a library.
983 c.sabi.Properties.ReexportedIncludeFlags = append(c.sabi.Properties.ReexportedIncludeFlags, flags)
984
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700985 }
Dan Willemsenb40aab62016-04-20 14:21:14 -0700986 } else {
987 ctx.ModuleErrorf("module %q is not a genrule", name)
988 }
989 default:
Colin Crossc99deeb2016-04-11 15:06:20 -0700990 ctx.ModuleErrorf("depends on non-cc module %q", name)
Colin Crossca860ac2016-01-04 14:34:37 -0800991 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700992 return
993 }
994
995 if !a.Enabled() {
Colin Crossa8f5e9a2016-12-13 12:51:11 -0800996 if ctx.AConfig().AllowMissingDependencies() {
997 ctx.AddMissingDependencies([]string{name})
998 } else {
999 ctx.ModuleErrorf("depends on disabled module %q", name)
1000 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001001 return
1002 }
1003
Colin Crossa1ad8d12016-06-01 17:09:44 -07001004 if a.Target().Os != ctx.Os() {
1005 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), name)
1006 return
1007 }
1008
1009 if a.Target().Arch.ArchType != ctx.Arch().ArchType {
1010 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), name)
Colin Crossc99deeb2016-04-11 15:06:20 -07001011 return
1012 }
1013
Colin Crossc99deeb2016-04-11 15:06:20 -07001014 if tag == reuseObjTag {
Colin Crossbba99042016-11-23 15:45:05 -08001015 if l, ok := cc.compiler.(libraryInterface); ok {
Colin Crossbbc9f4d2017-05-03 16:24:55 -07001016 objs, flags, deps := l.reuseObjs()
Colin Cross10d22312017-05-03 11:01:58 -07001017 depPaths.Objs = depPaths.Objs.Append(objs)
1018 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Colin Crossbbc9f4d2017-05-03 16:24:55 -07001019 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps, deps...)
Colin Crossbba99042016-11-23 15:45:05 -08001020 return
1021 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001022 }
1023
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001024 if t, ok := tag.(dependencyTag); ok && t.library {
Dan Willemsena96ff642016-06-07 12:34:45 -07001025 if i, ok := cc.linker.(exportedFlagsProducer); ok {
Dan Willemsen76f08272016-07-09 00:14:08 -07001026 flags := i.exportedFlags()
Dan Willemsen847dcc72016-09-29 12:13:36 -07001027 deps := i.exportedFlagsDeps()
Dan Willemsen76f08272016-07-09 00:14:08 -07001028 depPaths.Flags = append(depPaths.Flags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -07001029 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders, deps...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001030
1031 if t.reexportFlags {
Dan Willemsen76f08272016-07-09 00:14:08 -07001032 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -07001033 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps, deps...)
Jayant Chowdhary715cac32017-04-20 06:53:59 -07001034 // 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 -07001035 // Re-exported shared library headers must be included as well since they can help us with type information
1036 // about template instantiations (instantiated from their headers).
1037 c.sabi.Properties.ReexportedIncludeFlags = append(c.sabi.Properties.ReexportedIncludeFlags, flags...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001038 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001039 }
Dan Willemsena96ff642016-06-07 12:34:45 -07001040
Dan Albert9e10cd42016-08-03 14:12:14 -07001041 checkLinkType(c, cc)
Colin Crossc99deeb2016-04-11 15:06:20 -07001042 }
1043
Colin Cross26c34ed2016-09-30 17:10:16 -07001044 var ptr *android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -07001045 var depPtr *android.Paths
Colin Crossc99deeb2016-04-11 15:06:20 -07001046
Colin Cross26c34ed2016-09-30 17:10:16 -07001047 linkFile := cc.outputFile
1048 depFile := android.OptionalPath{}
1049
Colin Crossc99deeb2016-04-11 15:06:20 -07001050 switch tag {
Dan Albert914449f2016-06-17 16:45:24 -07001051 case ndkStubDepTag, sharedDepTag, sharedExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001052 ptr = &depPaths.SharedLibs
1053 depPtr = &depPaths.SharedLibsDeps
1054 depFile = cc.linker.(libraryInterface).toc()
Dan Albert914449f2016-06-17 16:45:24 -07001055 case lateSharedDepTag, ndkLateStubDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001056 ptr = &depPaths.LateSharedLibs
1057 depPtr = &depPaths.LateSharedLibsDeps
1058 depFile = cc.linker.(libraryInterface).toc()
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001059 case staticDepTag, staticExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001060 ptr = &depPaths.StaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -07001061 case lateStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001062 ptr = &depPaths.LateStaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -07001063 case wholeStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001064 ptr = &depPaths.WholeStaticLibs
Colin Crossb916a382016-07-29 17:28:03 -07001065 staticLib, ok := cc.linker.(libraryInterface)
1066 if !ok || !staticLib.static() {
Dan Willemsena96ff642016-06-07 12:34:45 -07001067 ctx.ModuleErrorf("module %q not a static library", name)
Colin Crossc99deeb2016-04-11 15:06:20 -07001068 return
1069 }
1070
1071 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
1072 postfix := " (required by " + ctx.OtherModuleName(m) + ")"
1073 for i := range missingDeps {
1074 missingDeps[i] += postfix
1075 }
1076 ctx.AddMissingDependencies(missingDeps)
1077 }
Dan Willemsen5cb580f2016-09-26 17:33:01 -07001078 depPaths.WholeStaticLibObjs = depPaths.WholeStaticLibObjs.Append(staticLib.objs())
Colin Cross5950f382016-12-13 12:50:57 -08001079 case headerDepTag:
1080 // Nothing
Colin Crossc99deeb2016-04-11 15:06:20 -07001081 case objDepTag:
Dan Willemsen5cb580f2016-09-26 17:33:01 -07001082 depPaths.Objs.objFiles = append(depPaths.Objs.objFiles, linkFile.Path())
Colin Crossc99deeb2016-04-11 15:06:20 -07001083 case crtBeginDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001084 depPaths.CrtBegin = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -07001085 case crtEndDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001086 depPaths.CrtEnd = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -07001087 }
1088
Dan Willemsen581341d2017-02-09 16:16:31 -08001089 switch tag {
1090 case staticDepTag, staticExportDepTag, lateStaticDepTag:
1091 staticLib, ok := cc.linker.(libraryInterface)
1092 if !ok || !staticLib.static() {
1093 ctx.ModuleErrorf("module %q not a static library", name)
1094 return
1095 }
1096
1097 // When combining coverage files for shared libraries and executables, coverage files
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001098 // in static libraries act as if they were whole static libraries. The same goes for
1099 // source based Abi dump files.
Dan Willemsen581341d2017-02-09 16:16:31 -08001100 depPaths.StaticLibObjs.coverageFiles = append(depPaths.StaticLibObjs.coverageFiles,
1101 staticLib.objs().coverageFiles...)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001102 depPaths.StaticLibObjs.sAbiDumpFiles = append(depPaths.StaticLibObjs.sAbiDumpFiles,
1103 staticLib.objs().sAbiDumpFiles...)
Dan Willemsen581341d2017-02-09 16:16:31 -08001104 }
1105
Colin Cross26c34ed2016-09-30 17:10:16 -07001106 if ptr != nil {
Colin Crossce75d2c2016-10-06 16:12:58 -07001107 if !linkFile.Valid() {
1108 ctx.ModuleErrorf("module %q missing output file", name)
1109 return
1110 }
Colin Cross26c34ed2016-09-30 17:10:16 -07001111 *ptr = append(*ptr, linkFile.Path())
1112 }
1113
Colin Crossc99deeb2016-04-11 15:06:20 -07001114 if depPtr != nil {
Colin Cross26c34ed2016-09-30 17:10:16 -07001115 dep := depFile
1116 if !dep.Valid() {
1117 dep = linkFile
1118 }
1119 *depPtr = append(*depPtr, dep.Path())
Colin Crossca860ac2016-01-04 14:34:37 -08001120 }
Jiyong Park27b188b2017-07-18 13:23:39 +09001121
1122 // Export the shared libs to the make world. In doing so, .vendor suffix
1123 // is added if the lib has both core and vendor variants and this module
1124 // is building against vndk. This is because the vendor variant will be
1125 // have .vendor suffix in its name in the make world. However, if the
1126 // lib is a vendor-only lib or this lib is not building against vndk,
1127 // then the suffix is not added.
1128 switch tag {
1129 case sharedDepTag, sharedExportDepTag, lateSharedDepTag:
1130 libName := strings.TrimSuffix(name, llndkLibrarySuffix)
1131 libName = strings.TrimPrefix(libName, "prebuilt_")
Jiyong Parkd5b18a52017-08-03 21:22:50 +09001132 isLLndk := inList(libName, llndkLibraries)
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001133 if c.vndk() && (Bool(cc.VendorProperties.Vendor_available) || isLLndk) {
Jiyong Park27b188b2017-07-18 13:23:39 +09001134 libName += vendorSuffix
1135 }
1136 // Note: the order of libs in this list is not important because
1137 // they merely serve as dependencies in the make world and do not
1138 // affect this lib itself.
1139 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, libName)
1140 }
Colin Crossca860ac2016-01-04 14:34:37 -08001141 })
1142
Colin Crossdd84e052017-05-17 13:44:16 -07001143 // Dedup exported flags from dependencies
1144 depPaths.Flags = firstUniqueElements(depPaths.Flags)
Dan Willemsenfe92c962017-08-29 12:28:37 -07001145 depPaths.GeneratedHeaders = android.FirstUniquePaths(depPaths.GeneratedHeaders)
1146 depPaths.ReexportedFlags = firstUniqueElements(depPaths.ReexportedFlags)
1147 depPaths.ReexportedFlagsDeps = android.FirstUniquePaths(depPaths.ReexportedFlagsDeps)
1148
1149 if c.sabi != nil {
1150 c.sabi.Properties.ReexportedIncludeFlags = firstUniqueElements(c.sabi.Properties.ReexportedIncludeFlags)
1151 }
Colin Crossdd84e052017-05-17 13:44:16 -07001152
Colin Crossca860ac2016-01-04 14:34:37 -08001153 return depPaths
1154}
1155
1156func (c *Module) InstallInData() bool {
1157 if c.installer == nil {
1158 return false
1159 }
Vishwath Mohan1dd88392017-03-29 22:00:18 -07001160 return c.installer.inData()
1161}
1162
1163func (c *Module) InstallInSanitizerDir() bool {
1164 if c.installer == nil {
1165 return false
1166 }
1167 if c.sanitize != nil && c.sanitize.inSanitizerDir() {
Colin Cross94610402016-08-29 13:41:32 -07001168 return true
1169 }
Vishwath Mohan1dd88392017-03-29 22:00:18 -07001170 return c.installer.inSanitizerDir()
Colin Crossca860ac2016-01-04 14:34:37 -08001171}
1172
Dan Willemsen4aa75ca2016-09-28 16:18:03 -07001173func (c *Module) HostToolPath() android.OptionalPath {
1174 if c.installer == nil {
1175 return android.OptionalPath{}
1176 }
1177 return c.installer.hostToolPath()
1178}
1179
Nan Zhangd4e641b2017-07-12 12:55:28 -07001180func (c *Module) IntermPathForModuleOut() android.OptionalPath {
1181 return c.outputFile
1182}
1183
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001184func (c *Module) Srcs() android.Paths {
1185 if c.outputFile.Valid() {
1186 return android.Paths{c.outputFile.Path()}
1187 }
1188 return android.Paths{}
1189}
1190
Colin Cross2ba19d92015-05-07 15:44:20 -07001191//
Colin Crosscfad1192015-11-02 16:43:11 -08001192// Defaults
1193//
Colin Crossca860ac2016-01-04 14:34:37 -08001194type Defaults struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001195 android.ModuleBase
Colin Cross1f44a3a2017-07-07 14:33:33 -07001196 android.DefaultsModuleBase
Colin Crosscfad1192015-11-02 16:43:11 -08001197}
1198
Colin Cross635c3b02016-05-18 15:37:25 -07001199func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -08001200}
1201
Colin Cross1e676be2016-10-12 14:38:15 -07001202func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
1203}
1204
Colin Cross36242852017-06-23 15:06:31 -07001205func defaultsFactory() android.Module {
Colin Crosse1d764e2016-08-18 14:18:32 -07001206 return DefaultsFactory()
1207}
1208
Colin Cross36242852017-06-23 15:06:31 -07001209func DefaultsFactory(props ...interface{}) android.Module {
Colin Crossca860ac2016-01-04 14:34:37 -08001210 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -08001211
Colin Cross36242852017-06-23 15:06:31 -07001212 module.AddProperties(props...)
1213 module.AddProperties(
Colin Crossca860ac2016-01-04 14:34:37 -08001214 &BaseProperties{},
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001215 &VendorProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001216 &BaseCompilerProperties{},
1217 &BaseLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -07001218 &LibraryProperties{},
Colin Cross919281a2016-04-05 16:42:05 -07001219 &FlagExporterProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001220 &BinaryLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -07001221 &TestProperties{},
1222 &TestBinaryProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001223 &UnusedProperties{},
1224 &StlProperties{},
Colin Cross16b23492016-01-06 14:41:07 -08001225 &SanitizeProperties{},
Colin Cross665dce92016-04-28 14:50:03 -07001226 &StripProperties{},
Dan Willemsen7424d612016-09-01 13:45:39 -07001227 &InstallerProperties{},
Dan Willemsena03cf6d2016-09-26 15:45:04 -07001228 &TidyProperties{},
Dan Willemsen581341d2017-02-09 16:16:31 -08001229 &CoverageProperties{},
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001230 &SAbiProperties{},
Justin Yun4b2382f2017-07-26 14:22:10 +09001231 &VndkProperties{},
Stephen Craneba090d12017-05-09 15:44:35 -07001232 &LTOProperties{},
Colin Crosse1d764e2016-08-18 14:18:32 -07001233 )
Colin Crosscfad1192015-11-02 16:43:11 -08001234
Colin Cross1f44a3a2017-07-07 14:33:33 -07001235 android.InitDefaultsModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001236
1237 return module
Colin Crosscfad1192015-11-02 16:43:11 -08001238}
1239
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001240const (
1241 // coreMode is the variant used for framework-private libraries, or
1242 // SDK libraries. (which framework-private libraries can use)
1243 coreMode = "core"
1244
1245 // vendorMode is the variant used for /vendor code that compiles
1246 // against the VNDK.
1247 vendorMode = "vendor"
1248)
1249
1250func vendorMutator(mctx android.BottomUpMutatorContext) {
1251 if mctx.Os() != android.Android {
1252 return
1253 }
1254
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001255 if genrule, ok := mctx.Module().(*genrule.Module); ok {
1256 if props, ok := genrule.Extra.(*VendorProperties); ok {
1257 if !mctx.DeviceConfig().CompileVndk() {
1258 mctx.CreateVariations(coreMode)
1259 } else if Bool(props.Vendor_available) {
1260 mctx.CreateVariations(coreMode, vendorMode)
1261 } else if mctx.Vendor() {
1262 mctx.CreateVariations(vendorMode)
1263 } else {
1264 mctx.CreateVariations(coreMode)
1265 }
1266 }
1267 }
1268
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001269 m, ok := mctx.Module().(*Module)
1270 if !ok {
1271 return
1272 }
1273
1274 // Sanity check
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001275 if Bool(m.VendorProperties.Vendor_available) && mctx.Vendor() {
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001276 mctx.PropertyErrorf("vendor_available",
1277 "doesn't make sense at the same time as `vendor: true` or `proprietary: true`")
1278 return
1279 }
Justin Yun8effde42017-06-23 19:24:43 +09001280 if vndk := m.vndkdep; vndk != nil {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001281 if vndk.isVndk() && !Bool(m.VendorProperties.Vendor_available) {
Justin Yun8effde42017-06-23 19:24:43 +09001282 mctx.PropertyErrorf("vndk",
1283 "has to define `vendor_available: true` to enable vndk")
1284 return
1285 }
1286 if !vndk.isVndk() && vndk.isVndkSp() {
1287 mctx.PropertyErrorf("vndk",
1288 "must set `enabled: true` to set `support_system_process: true`")
1289 return
1290 }
1291 }
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001292
1293 if !mctx.DeviceConfig().CompileVndk() {
1294 // If the device isn't compiling against the VNDK, we always
1295 // use the core mode.
1296 mctx.CreateVariations(coreMode)
1297 } else if _, ok := m.linker.(*llndkStubDecorator); ok {
1298 // LL-NDK stubs only exist in the vendor variant, since the
1299 // real libraries will be used in the core variant.
1300 mctx.CreateVariations(vendorMode)
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001301 } else if Bool(m.VendorProperties.Vendor_available) {
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001302 // This will be available in both /system and /vendor
Justin Yun8effde42017-06-23 19:24:43 +09001303 // or a /system directory that is available to vendor.
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001304 mod := mctx.CreateVariations(coreMode, vendorMode)
1305 mod[1].(*Module).Properties.UseVndk = true
1306 } else if mctx.Vendor() && m.Properties.Sdk_version == "" {
1307 // This will be available in /vendor only
1308 mod := mctx.CreateVariations(vendorMode)
1309 mod[0].(*Module).Properties.UseVndk = true
1310 } else {
1311 // This is either in /system (or similar: /data), or is a
1312 // modules built with the NDK. Modules built with the NDK
1313 // will be restricted using the existing link type checks.
1314 mctx.CreateVariations(coreMode)
1315 }
1316}
1317
Colin Crossdd84e052017-05-17 13:44:16 -07001318// firstUniqueElements returns all unique elements of a slice, keeping the first copy of each
1319// modifies the slice contents in place, and returns a subslice of the original slice
1320func firstUniqueElements(list []string) []string {
1321 k := 0
1322outer:
1323 for i := 0; i < len(list); i++ {
1324 for j := 0; j < k; j++ {
1325 if list[i] == list[j] {
1326 continue outer
1327 }
1328 }
1329 list[k] = list[i]
1330 k++
1331 }
1332 return list[:k]
1333}
1334
Colin Cross74d1ec02015-04-28 13:30:13 -07001335// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
1336// modifies the slice contents in place, and returns a subslice of the original slice
1337func lastUniqueElements(list []string) []string {
1338 totalSkip := 0
1339 for i := len(list) - 1; i >= totalSkip; i-- {
1340 skip := 0
1341 for j := i - 1; j >= totalSkip; j-- {
1342 if list[i] == list[j] {
1343 skip++
1344 } else {
1345 list[j+skip] = list[j]
1346 }
1347 }
1348 totalSkip += skip
1349 }
1350 return list[totalSkip:]
1351}
Colin Cross06a931b2015-10-28 17:23:31 -07001352
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -07001353func getCurrentNdkPrebuiltVersion(ctx DepsContext) string {
1354 if ctx.AConfig().PlatformSdkVersionInt() > config.NdkMaxPrebuiltVersionInt {
1355 return strconv.Itoa(config.NdkMaxPrebuiltVersionInt)
1356 }
1357 return ctx.AConfig().PlatformSdkVersion()
1358}
1359
Colin Cross06a931b2015-10-28 17:23:31 -07001360var Bool = proptools.Bool