blob: af58f9d0e5192992952ed16086327d9bcff0309e [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
Colin Cross635c3b02016-05-18 15:37:25 -0700137 CFlagsDeps android.Paths // Files depended on by compiler flags
Colin Cross18c0c5a2016-12-01 14:45:23 -0800138
139 GroupStaticLibs bool
Colin Crossc472d572015-03-17 15:06:21 -0700140}
141
Colin Cross81413472016-04-11 14:37:39 -0700142type ObjectLinkerProperties struct {
143 // names of other cc_object modules to link into this module using partial linking
144 Objs []string `android:"arch_variant"`
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700145
146 // if set, add an extra objcopy --prefix-symbols= step
147 Prefix_symbols string
Colin Cross81413472016-04-11 14:37:39 -0700148}
149
Colin Crossca860ac2016-01-04 14:34:37 -0800150// Properties used to compile all C or C++ modules
151type BaseProperties struct {
152 // compile module with clang instead of gcc
153 Clang *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700154
155 // Minimum sdk version supported when compiling against the ndk
156 Sdk_version string
157
Colin Crossca860ac2016-01-04 14:34:37 -0800158 // don't insert default compiler flags into asflags, cflags,
159 // cppflags, conlyflags, ldflags, or include_dirs
160 No_default_compiler_flags *bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700161
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700162 AndroidMkSharedLibs []string `blueprint:"mutated"`
163 HideFromMake bool `blueprint:"mutated"`
164 PreventInstall bool `blueprint:"mutated"`
165
166 UseVndk bool `blueprint:"mutated"`
167}
168
169type VendorProperties struct {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700170 // whether this module should be allowed to install onto /vendor as
171 // well as /system. The two variants will be built separately, one
172 // like normal, and the other limited to the set of libraries and
173 // headers that are exposed to /vendor modules.
174 //
175 // The vendor variant may be used with a different (newer) /system,
176 // so it shouldn't have any unversioned runtime dependencies, or
177 // make assumptions about the system that may not be true in the
178 // future.
179 //
180 // Nothing happens if BOARD_VNDK_VERSION isn't set in the BoardConfig.mk
181 Vendor_available *bool
Colin Crossca860ac2016-01-04 14:34:37 -0800182}
183
Colin Crossca860ac2016-01-04 14:34:37 -0800184type UnusedProperties struct {
Dan Willemsen581341d2017-02-09 16:16:31 -0800185 Tags []string
Colin Crosscfad1192015-11-02 16:43:11 -0800186}
187
Colin Crossca860ac2016-01-04 14:34:37 -0800188type ModuleContextIntf interface {
Colin Crossca860ac2016-01-04 14:34:37 -0800189 static() bool
190 staticBinary() bool
191 clang() bool
Colin Crossb98c8b02016-07-29 13:44:28 -0700192 toolchain() config.Toolchain
Colin Crossca860ac2016-01-04 14:34:37 -0800193 noDefaultCompilerFlags() bool
194 sdk() bool
195 sdkVersion() string
Dan Willemsend2ede872016-11-18 14:54:24 -0800196 vndk() bool
Justin Yun8effde42017-06-23 19:24:43 +0900197 isVndk() bool
198 isVndkSp() bool
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800199 createVndkSourceAbiDump() bool
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700200 selectedStl() string
Colin Crossce75d2c2016-10-06 16:12:58 -0700201 baseModuleName() string
Colin Crossca860ac2016-01-04 14:34:37 -0800202}
203
204type ModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700205 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800206 ModuleContextIntf
207}
208
209type BaseModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700210 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800211 ModuleContextIntf
212}
213
Colin Cross37047f12016-12-13 17:06:13 -0800214type DepsContext interface {
215 android.BottomUpMutatorContext
216 ModuleContextIntf
217}
218
Colin Crossca860ac2016-01-04 14:34:37 -0800219type feature interface {
220 begin(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800221 deps(ctx DepsContext, deps Deps) Deps
Colin Crossca860ac2016-01-04 14:34:37 -0800222 flags(ctx ModuleContext, flags Flags) Flags
223 props() []interface{}
224}
225
226type compiler interface {
Colin Cross42742b82016-08-01 13:20:05 -0700227 compilerInit(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800228 compilerDeps(ctx DepsContext, deps Deps) Deps
Colin Cross42742b82016-08-01 13:20:05 -0700229 compilerFlags(ctx ModuleContext, flags Flags) Flags
230 compilerProps() []interface{}
231
Colin Cross76fada02016-07-27 10:31:13 -0700232 appendCflags([]string)
233 appendAsflags([]string)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700234 compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects
Colin Crossca860ac2016-01-04 14:34:37 -0800235}
236
237type linker interface {
Colin Cross42742b82016-08-01 13:20:05 -0700238 linkerInit(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800239 linkerDeps(ctx DepsContext, deps Deps) Deps
Colin Cross42742b82016-08-01 13:20:05 -0700240 linkerFlags(ctx ModuleContext, flags Flags) Flags
241 linkerProps() []interface{}
242
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700243 link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path
Colin Cross76fada02016-07-27 10:31:13 -0700244 appendLdflags([]string)
Colin Crossca860ac2016-01-04 14:34:37 -0800245}
246
247type installer interface {
Colin Cross42742b82016-08-01 13:20:05 -0700248 installerProps() []interface{}
Colin Cross635c3b02016-05-18 15:37:25 -0700249 install(ctx ModuleContext, path android.Path)
Colin Crossca860ac2016-01-04 14:34:37 -0800250 inData() bool
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700251 inSanitizerDir() bool
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700252 hostToolPath() android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800253}
254
Colin Crossc99deeb2016-04-11 15:06:20 -0700255type dependencyTag struct {
256 blueprint.BaseDependencyTag
257 name string
258 library bool
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700259
260 reexportFlags bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700261}
262
263var (
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700264 sharedDepTag = dependencyTag{name: "shared", library: true}
265 sharedExportDepTag = dependencyTag{name: "shared", library: true, reexportFlags: true}
266 lateSharedDepTag = dependencyTag{name: "late shared", library: true}
267 staticDepTag = dependencyTag{name: "static", library: true}
268 staticExportDepTag = dependencyTag{name: "static", library: true, reexportFlags: true}
269 lateStaticDepTag = dependencyTag{name: "late static", library: true}
270 wholeStaticDepTag = dependencyTag{name: "whole static", library: true, reexportFlags: true}
Colin Cross32ec36c2016-12-15 07:39:51 -0800271 headerDepTag = dependencyTag{name: "header", library: true}
272 headerExportDepTag = dependencyTag{name: "header", library: true, reexportFlags: true}
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700273 genSourceDepTag = dependencyTag{name: "gen source"}
274 genHeaderDepTag = dependencyTag{name: "gen header"}
275 genHeaderExportDepTag = dependencyTag{name: "gen header", reexportFlags: true}
276 objDepTag = dependencyTag{name: "obj"}
277 crtBeginDepTag = dependencyTag{name: "crtbegin"}
278 crtEndDepTag = dependencyTag{name: "crtend"}
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700279 linkerScriptDepTag = dependencyTag{name: "linker script"}
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700280 reuseObjTag = dependencyTag{name: "reuse objects"}
281 ndkStubDepTag = dependencyTag{name: "ndk stub", library: true}
282 ndkLateStubDepTag = dependencyTag{name: "ndk late stub", library: true}
Colin Crossc99deeb2016-04-11 15:06:20 -0700283)
284
Colin Crossca860ac2016-01-04 14:34:37 -0800285// Module contains the properties and members used by all C/C++ module types, and implements
286// the blueprint.Module interface. It delegates to compiler, linker, and installer interfaces
287// to construct the output file. Behavior can be customized with a Customizer interface
288type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700289 android.ModuleBase
Colin Cross1f44a3a2017-07-07 14:33:33 -0700290 android.DefaultableModuleBase
Colin Crossc472d572015-03-17 15:06:21 -0700291
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700292 Properties BaseProperties
293 VendorProperties VendorProperties
294 unused UnusedProperties
Colin Crossfa138792015-04-24 17:31:52 -0700295
Colin Crossca860ac2016-01-04 14:34:37 -0800296 // initialize before calling Init
Colin Cross635c3b02016-05-18 15:37:25 -0700297 hod android.HostOrDeviceSupported
298 multilib android.Multilib
Colin Crossc472d572015-03-17 15:06:21 -0700299
Colin Crossca860ac2016-01-04 14:34:37 -0800300 // delegates, initialize before calling Init
Colin Crossb4ce0ec2016-09-13 13:41:39 -0700301 features []feature
302 compiler compiler
303 linker linker
304 installer installer
305 stl *stl
306 sanitize *sanitize
Dan Willemsen581341d2017-02-09 16:16:31 -0800307 coverage *coverage
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800308 sabi *sabi
Justin Yun8effde42017-06-23 19:24:43 +0900309 vndkdep *vndkdep
Stephen Craneba090d12017-05-09 15:44:35 -0700310 lto *lto
Colin Cross16b23492016-01-06 14:41:07 -0800311
312 androidMkSharedLibDeps []string
Colin Cross74d1ec02015-04-28 13:30:13 -0700313
Colin Cross635c3b02016-05-18 15:37:25 -0700314 outputFile android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800315
Colin Crossb98c8b02016-07-29 13:44:28 -0700316 cachedToolchain config.Toolchain
Colin Crossb916a382016-07-29 17:28:03 -0700317
318 subAndroidMkOnce map[subAndroidMkProvider]bool
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800319
320 // Flags used to compile this module
321 flags Flags
Colin Crossc472d572015-03-17 15:06:21 -0700322}
323
Colin Cross36242852017-06-23 15:06:31 -0700324func (c *Module) Init() android.Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700325 c.AddProperties(&c.Properties, &c.VendorProperties, &c.unused)
Colin Crossca860ac2016-01-04 14:34:37 -0800326 if c.compiler != nil {
Colin Cross36242852017-06-23 15:06:31 -0700327 c.AddProperties(c.compiler.compilerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800328 }
329 if c.linker != nil {
Colin Cross36242852017-06-23 15:06:31 -0700330 c.AddProperties(c.linker.linkerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800331 }
332 if c.installer != nil {
Colin Cross36242852017-06-23 15:06:31 -0700333 c.AddProperties(c.installer.installerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800334 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700335 if c.stl != nil {
Colin Cross36242852017-06-23 15:06:31 -0700336 c.AddProperties(c.stl.props()...)
Colin Crossa8e07cc2016-04-04 15:07:06 -0700337 }
Colin Cross16b23492016-01-06 14:41:07 -0800338 if c.sanitize != nil {
Colin Cross36242852017-06-23 15:06:31 -0700339 c.AddProperties(c.sanitize.props()...)
Colin Cross16b23492016-01-06 14:41:07 -0800340 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800341 if c.coverage != nil {
Colin Cross36242852017-06-23 15:06:31 -0700342 c.AddProperties(c.coverage.props()...)
Dan Willemsen581341d2017-02-09 16:16:31 -0800343 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800344 if c.sabi != nil {
Colin Cross36242852017-06-23 15:06:31 -0700345 c.AddProperties(c.sabi.props()...)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800346 }
Justin Yun8effde42017-06-23 19:24:43 +0900347 if c.vndkdep != nil {
348 c.AddProperties(c.vndkdep.props()...)
349 }
Stephen Craneba090d12017-05-09 15:44:35 -0700350 if c.lto != nil {
351 c.AddProperties(c.lto.props()...)
352 }
Colin Crossca860ac2016-01-04 14:34:37 -0800353 for _, feature := range c.features {
Colin Cross36242852017-06-23 15:06:31 -0700354 c.AddProperties(feature.props()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800355 }
Colin Crossc472d572015-03-17 15:06:21 -0700356
Colin Cross36242852017-06-23 15:06:31 -0700357 android.InitAndroidArchModule(c, c.hod, c.multilib)
Colin Crossc472d572015-03-17 15:06:21 -0700358
Colin Cross1f44a3a2017-07-07 14:33:33 -0700359 android.InitDefaultableModule(c)
Colin Cross36242852017-06-23 15:06:31 -0700360
361 return c
Colin Crossc472d572015-03-17 15:06:21 -0700362}
363
Colin Crossb916a382016-07-29 17:28:03 -0700364// Returns true for dependency roots (binaries)
365// TODO(ccross): also handle dlopenable libraries
366func (c *Module) isDependencyRoot() bool {
367 if root, ok := c.linker.(interface {
368 isDependencyRoot() bool
369 }); ok {
370 return root.isDependencyRoot()
371 }
372 return false
373}
374
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700375func (c *Module) vndk() bool {
376 return c.Properties.UseVndk
377}
378
Justin Yun8effde42017-06-23 19:24:43 +0900379func (c *Module) isVndk() bool {
380 if c.vndkdep != nil {
381 return c.vndkdep.isVndk()
382 }
383 return false
384}
385
Colin Crossca860ac2016-01-04 14:34:37 -0800386type baseModuleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700387 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800388 moduleContextImpl
389}
390
Colin Cross37047f12016-12-13 17:06:13 -0800391type depsContext struct {
392 android.BottomUpMutatorContext
393 moduleContextImpl
394}
395
Colin Crossca860ac2016-01-04 14:34:37 -0800396type moduleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700397 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800398 moduleContextImpl
399}
400
Justin Yun8effde42017-06-23 19:24:43 +0900401// Vendor returns true for vendor modules excluding VNDK libraries so that
402// they get installed onto the correct partition
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700403func (ctx *moduleContext) Vendor() bool {
Justin Yun8effde42017-06-23 19:24:43 +0900404 return ctx.ModuleContext.Vendor() || (ctx.mod.vndk() && !ctx.mod.isVndk())
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700405}
406
Colin Crossca860ac2016-01-04 14:34:37 -0800407type moduleContextImpl struct {
408 mod *Module
409 ctx BaseModuleContext
410}
411
Colin Crossca860ac2016-01-04 14:34:37 -0800412func (ctx *moduleContextImpl) clang() bool {
413 return ctx.mod.clang(ctx.ctx)
414}
415
Colin Crossb98c8b02016-07-29 13:44:28 -0700416func (ctx *moduleContextImpl) toolchain() config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800417 return ctx.mod.toolchain(ctx.ctx)
418}
419
420func (ctx *moduleContextImpl) static() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700421 if static, ok := ctx.mod.linker.(interface {
422 static() bool
423 }); ok {
424 return static.static()
Colin Crossca860ac2016-01-04 14:34:37 -0800425 }
Colin Crossb916a382016-07-29 17:28:03 -0700426 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800427}
428
429func (ctx *moduleContextImpl) staticBinary() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700430 if static, ok := ctx.mod.linker.(interface {
431 staticBinary() bool
432 }); ok {
433 return static.staticBinary()
Colin Crossca860ac2016-01-04 14:34:37 -0800434 }
Colin Crossb916a382016-07-29 17:28:03 -0700435 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800436}
437
438func (ctx *moduleContextImpl) noDefaultCompilerFlags() bool {
439 return Bool(ctx.mod.Properties.No_default_compiler_flags)
440}
441
442func (ctx *moduleContextImpl) sdk() bool {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700443 if ctx.ctx.Device() && !ctx.vndk() {
Dan Willemsena96ff642016-06-07 12:34:45 -0700444 return ctx.mod.Properties.Sdk_version != ""
445 }
446 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800447}
448
449func (ctx *moduleContextImpl) sdkVersion() string {
Dan Willemsena96ff642016-06-07 12:34:45 -0700450 if ctx.ctx.Device() {
Dan Willemsen11b26142017-03-19 18:30:37 -0700451 if ctx.vndk() {
452 return "current"
Dan Willemsend2ede872016-11-18 14:54:24 -0800453 } else {
454 return ctx.mod.Properties.Sdk_version
455 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700456 }
457 return ""
Colin Crossca860ac2016-01-04 14:34:37 -0800458}
459
Dan Willemsend2ede872016-11-18 14:54:24 -0800460func (ctx *moduleContextImpl) vndk() bool {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700461 return ctx.mod.vndk()
Dan Willemsend2ede872016-11-18 14:54:24 -0800462}
463
Justin Yun8effde42017-06-23 19:24:43 +0900464func (ctx *moduleContextImpl) isVndk() bool {
465 return ctx.mod.isVndk()
466}
467
468func (ctx *moduleContextImpl) isVndkSp() bool {
469 if vndk := ctx.mod.vndkdep; vndk != nil {
470 return vndk.isVndkSp()
471 }
472 return false
473}
474
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800475// Create source abi dumps if the module belongs to the list of VndkLibraries.
476func (ctx *moduleContextImpl) createVndkSourceAbiDump() bool {
Jayant Chowdharya4fce192017-09-06 13:10:03 -0700477 return ctx.ctx.Device() && ((ctx.vndk() && ctx.isVndk()) || inList(ctx.baseModuleName(), llndkLibraries))
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800478}
479
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700480func (ctx *moduleContextImpl) selectedStl() string {
481 if stl := ctx.mod.stl; stl != nil {
482 return stl.Properties.SelectedStl
483 }
484 return ""
485}
486
Colin Crossce75d2c2016-10-06 16:12:58 -0700487func (ctx *moduleContextImpl) baseModuleName() string {
488 return ctx.mod.ModuleBase.BaseModuleName()
489}
490
Colin Cross635c3b02016-05-18 15:37:25 -0700491func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800492 return &Module{
493 hod: hod,
494 multilib: multilib,
495 }
496}
497
Colin Cross635c3b02016-05-18 15:37:25 -0700498func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800499 module := newBaseModule(hod, multilib)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700500 module.features = []feature{
501 &tidyFeature{},
502 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700503 module.stl = &stl{}
Colin Cross16b23492016-01-06 14:41:07 -0800504 module.sanitize = &sanitize{}
Dan Willemsen581341d2017-02-09 16:16:31 -0800505 module.coverage = &coverage{}
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800506 module.sabi = &sabi{}
Justin Yun8effde42017-06-23 19:24:43 +0900507 module.vndkdep = &vndkdep{}
Stephen Craneba090d12017-05-09 15:44:35 -0700508 module.lto = &lto{}
Colin Crossca860ac2016-01-04 14:34:37 -0800509 return module
510}
511
Colin Crossce75d2c2016-10-06 16:12:58 -0700512func (c *Module) Prebuilt() *android.Prebuilt {
513 if p, ok := c.linker.(prebuiltLinkerInterface); ok {
514 return p.prebuilt()
515 }
516 return nil
517}
518
519func (c *Module) Name() string {
520 name := c.ModuleBase.Name()
Dan Willemsen01a90592017-04-07 15:21:13 -0700521 if p, ok := c.linker.(interface {
522 Name(string) string
523 }); ok {
Colin Crossce75d2c2016-10-06 16:12:58 -0700524 name = p.Name(name)
525 }
526 return name
527}
528
Colin Cross635c3b02016-05-18 15:37:25 -0700529func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800530 ctx := &moduleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700531 ModuleContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800532 moduleContextImpl: moduleContextImpl{
533 mod: c,
534 },
535 }
536 ctx.ctx = ctx
537
538 flags := Flags{
539 Toolchain: c.toolchain(ctx),
540 Clang: c.clang(ctx),
541 }
Colin Crossca860ac2016-01-04 14:34:37 -0800542 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700543 flags = c.compiler.compilerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800544 }
545 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700546 flags = c.linker.linkerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800547 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700548 if c.stl != nil {
549 flags = c.stl.flags(ctx, flags)
550 }
Colin Cross16b23492016-01-06 14:41:07 -0800551 if c.sanitize != nil {
552 flags = c.sanitize.flags(ctx, flags)
553 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800554 if c.coverage != nil {
555 flags = c.coverage.flags(ctx, flags)
556 }
Stephen Craneba090d12017-05-09 15:44:35 -0700557 if c.lto != nil {
558 flags = c.lto.flags(ctx, flags)
559 }
Colin Crossca860ac2016-01-04 14:34:37 -0800560 for _, feature := range c.features {
561 flags = feature.flags(ctx, flags)
562 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800563 if ctx.Failed() {
564 return
565 }
566
Colin Crossb98c8b02016-07-29 13:44:28 -0700567 flags.CFlags, _ = filterList(flags.CFlags, config.IllegalFlags)
568 flags.CppFlags, _ = filterList(flags.CppFlags, config.IllegalFlags)
569 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, config.IllegalFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800570
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800571 deps := c.depsToPaths(ctx)
572 if ctx.Failed() {
573 return
574 }
575 flags.GlobalFlags = append(flags.GlobalFlags, deps.Flags...)
576 c.flags = flags
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700577 // We need access to all the flags seen by a source file.
578 if c.sabi != nil {
579 flags = c.sabi.flags(ctx, flags)
580 }
Colin Crossca860ac2016-01-04 14:34:37 -0800581 // Optimization to reduce size of build.ninja
582 // Replace the long list of flags for each file with a module-local variable
583 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
584 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
585 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
586 flags.CFlags = []string{"$cflags"}
587 flags.CppFlags = []string{"$cppflags"}
588 flags.AsFlags = []string{"$asflags"}
589
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700590 var objs Objects
Colin Crossca860ac2016-01-04 14:34:37 -0800591 if c.compiler != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700592 objs = c.compiler.compile(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -0800593 if ctx.Failed() {
594 return
595 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800596 }
597
Colin Crossca860ac2016-01-04 14:34:37 -0800598 if c.linker != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700599 outputFile := c.linker.link(ctx, flags, deps, objs)
Colin Crossca860ac2016-01-04 14:34:37 -0800600 if ctx.Failed() {
601 return
602 }
Colin Cross635c3b02016-05-18 15:37:25 -0700603 c.outputFile = android.OptionalPathForPath(outputFile)
Colin Crossce75d2c2016-10-06 16:12:58 -0700604 }
Colin Cross5049f022015-03-18 13:28:46 -0700605
Colin Crossce75d2c2016-10-06 16:12:58 -0700606 if c.installer != nil && !c.Properties.PreventInstall && c.outputFile.Valid() {
607 c.installer.install(ctx, c.outputFile.Path())
608 if ctx.Failed() {
609 return
Colin Crossca860ac2016-01-04 14:34:37 -0800610 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700611 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800612}
613
Colin Crossb98c8b02016-07-29 13:44:28 -0700614func (c *Module) toolchain(ctx BaseModuleContext) config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800615 if c.cachedToolchain == nil {
Colin Crossb98c8b02016-07-29 13:44:28 -0700616 c.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
Colin Cross3f40fa42015-01-30 17:27:36 -0800617 }
Colin Crossca860ac2016-01-04 14:34:37 -0800618 return c.cachedToolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800619}
620
Colin Crossca860ac2016-01-04 14:34:37 -0800621func (c *Module) begin(ctx BaseModuleContext) {
622 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700623 c.compiler.compilerInit(ctx)
Colin Cross21b9a242015-03-24 14:15:58 -0700624 }
Colin Crossca860ac2016-01-04 14:34:37 -0800625 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700626 c.linker.linkerInit(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800627 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700628 if c.stl != nil {
629 c.stl.begin(ctx)
630 }
Colin Cross16b23492016-01-06 14:41:07 -0800631 if c.sanitize != nil {
632 c.sanitize.begin(ctx)
633 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800634 if c.coverage != nil {
635 c.coverage.begin(ctx)
636 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800637 if c.sabi != nil {
638 c.sabi.begin(ctx)
639 }
Justin Yun8effde42017-06-23 19:24:43 +0900640 if c.vndkdep != nil {
641 c.vndkdep.begin(ctx)
642 }
Stephen Craneba090d12017-05-09 15:44:35 -0700643 if c.lto != nil {
644 c.lto.begin(ctx)
645 }
Colin Crossca860ac2016-01-04 14:34:37 -0800646 for _, feature := range c.features {
647 feature.begin(ctx)
648 }
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700649 if ctx.sdk() {
Dan Albertf5415d72017-08-17 16:19:59 -0700650 version, err := normalizeNdkApiLevel(ctx, ctx.sdkVersion(), ctx.Arch())
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700651 if err != nil {
652 ctx.PropertyErrorf("sdk_version", err.Error())
653 }
Dan Albert90f7a4d2016-11-08 14:34:24 -0800654 c.Properties.Sdk_version = version
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700655 }
Colin Crossca860ac2016-01-04 14:34:37 -0800656}
657
Colin Cross37047f12016-12-13 17:06:13 -0800658func (c *Module) deps(ctx DepsContext) Deps {
Colin Crossc99deeb2016-04-11 15:06:20 -0700659 deps := Deps{}
660
661 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700662 deps = c.compiler.compilerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700663 }
664 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700665 deps = c.linker.linkerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700666 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700667 if c.stl != nil {
668 deps = c.stl.deps(ctx, deps)
669 }
Colin Cross16b23492016-01-06 14:41:07 -0800670 if c.sanitize != nil {
671 deps = c.sanitize.deps(ctx, deps)
672 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800673 if c.coverage != nil {
674 deps = c.coverage.deps(ctx, deps)
675 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800676 if c.sabi != nil {
677 deps = c.sabi.deps(ctx, deps)
678 }
Justin Yun8effde42017-06-23 19:24:43 +0900679 if c.vndkdep != nil {
680 deps = c.vndkdep.deps(ctx, deps)
681 }
Stephen Craneba090d12017-05-09 15:44:35 -0700682 if c.lto != nil {
683 deps = c.lto.deps(ctx, deps)
684 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700685 for _, feature := range c.features {
686 deps = feature.deps(ctx, deps)
687 }
688
689 deps.WholeStaticLibs = lastUniqueElements(deps.WholeStaticLibs)
690 deps.StaticLibs = lastUniqueElements(deps.StaticLibs)
691 deps.LateStaticLibs = lastUniqueElements(deps.LateStaticLibs)
692 deps.SharedLibs = lastUniqueElements(deps.SharedLibs)
693 deps.LateSharedLibs = lastUniqueElements(deps.LateSharedLibs)
Colin Cross5950f382016-12-13 12:50:57 -0800694 deps.HeaderLibs = lastUniqueElements(deps.HeaderLibs)
Colin Crossc99deeb2016-04-11 15:06:20 -0700695
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700696 for _, lib := range deps.ReexportSharedLibHeaders {
697 if !inList(lib, deps.SharedLibs) {
698 ctx.PropertyErrorf("export_shared_lib_headers", "Shared library not in shared_libs: '%s'", lib)
699 }
700 }
701
702 for _, lib := range deps.ReexportStaticLibHeaders {
703 if !inList(lib, deps.StaticLibs) {
704 ctx.PropertyErrorf("export_static_lib_headers", "Static library not in static_libs: '%s'", lib)
705 }
706 }
707
Colin Cross5950f382016-12-13 12:50:57 -0800708 for _, lib := range deps.ReexportHeaderLibHeaders {
709 if !inList(lib, deps.HeaderLibs) {
710 ctx.PropertyErrorf("export_header_lib_headers", "Header library not in header_libs: '%s'", lib)
711 }
712 }
713
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700714 for _, gen := range deps.ReexportGeneratedHeaders {
715 if !inList(gen, deps.GeneratedHeaders) {
716 ctx.PropertyErrorf("export_generated_headers", "Generated header module not in generated_headers: '%s'", gen)
717 }
718 }
719
Colin Crossc99deeb2016-04-11 15:06:20 -0700720 return deps
721}
722
Dan Albert7e9d2952016-08-04 13:02:36 -0700723func (c *Module) beginMutator(actx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800724 ctx := &baseModuleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700725 BaseContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800726 moduleContextImpl: moduleContextImpl{
727 mod: c,
728 },
729 }
730 ctx.ctx = ctx
731
Colin Crossca860ac2016-01-04 14:34:37 -0800732 c.begin(ctx)
Dan Albert7e9d2952016-08-04 13:02:36 -0700733}
734
Colin Cross1e676be2016-10-12 14:38:15 -0700735func (c *Module) DepsMutator(actx android.BottomUpMutatorContext) {
736 if !c.Enabled() {
737 return
738 }
739
Colin Cross37047f12016-12-13 17:06:13 -0800740 ctx := &depsContext{
741 BottomUpMutatorContext: actx,
Dan Albert7e9d2952016-08-04 13:02:36 -0700742 moduleContextImpl: moduleContextImpl{
743 mod: c,
744 },
745 }
746 ctx.ctx = ctx
Colin Crossca860ac2016-01-04 14:34:37 -0800747
Colin Crossc99deeb2016-04-11 15:06:20 -0700748 deps := c.deps(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800749
Dan Albert914449f2016-06-17 16:45:24 -0700750 variantNdkLibs := []string{}
751 variantLateNdkLibs := []string{}
Dan Willemsenb916b802017-03-19 13:44:32 -0700752 if ctx.Os() == android.Android {
Dan Albert914449f2016-06-17 16:45:24 -0700753 version := ctx.sdkVersion()
Dan Willemsen72d39932016-07-08 23:23:48 -0700754
Dan Albert914449f2016-06-17 16:45:24 -0700755 // Rewrites the names of shared libraries into the names of the NDK
756 // libraries where appropriate. This returns two slices.
757 //
758 // The first is a list of non-variant shared libraries (either rewritten
759 // NDK libraries to the modules in prebuilts/ndk, or not rewritten
760 // because they are not NDK libraries).
761 //
762 // The second is a list of ndk_library modules. These need to be
763 // separated because they are a variation dependency and must be added
764 // in a different manner.
765 rewriteNdkLibs := func(list []string) ([]string, []string) {
766 variantLibs := []string{}
767 nonvariantLibs := []string{}
768 for _, entry := range list {
Dan Willemsenb916b802017-03-19 13:44:32 -0700769 if ctx.sdk() && inList(entry, ndkPrebuiltSharedLibraries) {
Dan Albert914449f2016-06-17 16:45:24 -0700770 if !inList(entry, ndkMigratedLibs) {
771 nonvariantLibs = append(nonvariantLibs, entry+".ndk."+version)
772 } else {
773 variantLibs = append(variantLibs, entry+ndkLibrarySuffix)
774 }
Jiyong Parkd5b18a52017-08-03 21:22:50 +0900775 } else if ctx.vndk() && inList(entry, llndkLibraries) {
Dan Willemsenb916b802017-03-19 13:44:32 -0700776 nonvariantLibs = append(nonvariantLibs, entry+llndkLibrarySuffix)
Dan Albert914449f2016-06-17 16:45:24 -0700777 } else {
Dan Willemsen7cbf5f82017-03-28 00:08:30 -0700778 nonvariantLibs = append(nonvariantLibs, entry)
Dan Willemsen72d39932016-07-08 23:23:48 -0700779 }
780 }
Dan Albert914449f2016-06-17 16:45:24 -0700781 return nonvariantLibs, variantLibs
Dan Willemsen72d39932016-07-08 23:23:48 -0700782 }
783
Dan Albert914449f2016-06-17 16:45:24 -0700784 deps.SharedLibs, variantNdkLibs = rewriteNdkLibs(deps.SharedLibs)
785 deps.LateSharedLibs, variantLateNdkLibs = rewriteNdkLibs(deps.LateSharedLibs)
Jiyong Park4c35af02017-07-05 13:41:55 +0900786 deps.ReexportSharedLibHeaders, _ = rewriteNdkLibs(deps.ReexportSharedLibHeaders)
Dan Willemsen72d39932016-07-08 23:23:48 -0700787 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700788
Colin Cross32ec36c2016-12-15 07:39:51 -0800789 for _, lib := range deps.HeaderLibs {
790 depTag := headerDepTag
791 if inList(lib, deps.ReexportHeaderLibHeaders) {
792 depTag = headerExportDepTag
793 }
794 actx.AddVariationDependencies(nil, depTag, lib)
795 }
Colin Cross5950f382016-12-13 12:50:57 -0800796
Colin Crossc99deeb2016-04-11 15:06:20 -0700797 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, wholeStaticDepTag,
798 deps.WholeStaticLibs...)
799
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700800 for _, lib := range deps.StaticLibs {
801 depTag := staticDepTag
802 if inList(lib, deps.ReexportStaticLibHeaders) {
803 depTag = staticExportDepTag
804 }
Colin Cross15a0d462016-07-14 14:49:58 -0700805 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700806 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700807
808 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, lateStaticDepTag,
809 deps.LateStaticLibs...)
810
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700811 for _, lib := range deps.SharedLibs {
812 depTag := sharedDepTag
813 if inList(lib, deps.ReexportSharedLibHeaders) {
814 depTag = sharedExportDepTag
815 }
Colin Cross15a0d462016-07-14 14:49:58 -0700816 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700817 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700818
819 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, lateSharedDepTag,
820 deps.LateSharedLibs...)
821
Colin Cross68861832016-07-08 10:41:41 -0700822 actx.AddDependency(c, genSourceDepTag, deps.GeneratedSources...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700823
824 for _, gen := range deps.GeneratedHeaders {
825 depTag := genHeaderDepTag
826 if inList(gen, deps.ReexportGeneratedHeaders) {
827 depTag = genHeaderExportDepTag
828 }
829 actx.AddDependency(c, depTag, gen)
830 }
Dan Willemsenb40aab62016-04-20 14:21:14 -0700831
Colin Cross68861832016-07-08 10:41:41 -0700832 actx.AddDependency(c, objDepTag, deps.ObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700833
834 if deps.CrtBegin != "" {
Colin Cross68861832016-07-08 10:41:41 -0700835 actx.AddDependency(c, crtBeginDepTag, deps.CrtBegin)
Colin Crossca860ac2016-01-04 14:34:37 -0800836 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700837 if deps.CrtEnd != "" {
Colin Cross68861832016-07-08 10:41:41 -0700838 actx.AddDependency(c, crtEndDepTag, deps.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700839 }
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700840 if deps.LinkerScript != "" {
841 actx.AddDependency(c, linkerScriptDepTag, deps.LinkerScript)
842 }
Dan Albert914449f2016-06-17 16:45:24 -0700843
844 version := ctx.sdkVersion()
845 actx.AddVariationDependencies([]blueprint.Variation{
846 {"ndk_api", version}, {"link", "shared"}}, ndkStubDepTag, variantNdkLibs...)
847 actx.AddVariationDependencies([]blueprint.Variation{
848 {"ndk_api", version}, {"link", "shared"}}, ndkLateStubDepTag, variantLateNdkLibs...)
Colin Cross6362e272015-10-29 15:25:03 -0700849}
Colin Cross21b9a242015-03-24 14:15:58 -0700850
Dan Albert7e9d2952016-08-04 13:02:36 -0700851func beginMutator(ctx android.BottomUpMutatorContext) {
852 if c, ok := ctx.Module().(*Module); ok && c.Enabled() {
853 c.beginMutator(ctx)
854 }
855}
856
Colin Crossca860ac2016-01-04 14:34:37 -0800857func (c *Module) clang(ctx BaseModuleContext) bool {
858 clang := Bool(c.Properties.Clang)
859
860 if c.Properties.Clang == nil {
861 if ctx.Host() {
862 clang = true
863 }
864
865 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
866 clang = true
867 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800868 }
Colin Cross28344522015-04-22 13:07:53 -0700869
Colin Crossca860ac2016-01-04 14:34:37 -0800870 if !c.toolchain(ctx).ClangSupported() {
871 clang = false
872 }
873
874 return clang
875}
876
Colin Crossc99deeb2016-04-11 15:06:20 -0700877// Convert dependencies to paths. Returns a PathDeps containing paths
Colin Cross635c3b02016-05-18 15:37:25 -0700878func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
Colin Crossca860ac2016-01-04 14:34:37 -0800879 var depPaths PathDeps
Colin Crossca860ac2016-01-04 14:34:37 -0800880
Dan Willemsena96ff642016-06-07 12:34:45 -0700881 // Whether a module can link to another module, taking into
882 // account NDK linking.
Dan Albert9e10cd42016-08-03 14:12:14 -0700883 checkLinkType := func(from, to *Module) {
Dan Willemsena96ff642016-06-07 12:34:45 -0700884 if from.Target().Os != android.Android {
885 // Host code is not restricted
Dan Albert9e10cd42016-08-03 14:12:14 -0700886 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700887 }
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700888 if from.Properties.UseVndk {
Justin Yun8effde42017-06-23 19:24:43 +0900889 // Though vendor code is limited by the vendor mutator,
890 // each vendor-available module needs to check
891 // link-type for VNDK.
892 if from.vndkdep != nil {
893 from.vndkdep.vndkCheckLinkType(ctx, to)
894 }
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700895 return
896 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700897 if from.Properties.Sdk_version == "" {
898 // Platform code can link to anything
Dan Albert9e10cd42016-08-03 14:12:14 -0700899 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700900 }
Colin Crossb916a382016-07-29 17:28:03 -0700901 if _, ok := to.linker.(*toolchainLibraryDecorator); ok {
Dan Willemsena96ff642016-06-07 12:34:45 -0700902 // These are always allowed
Dan Albert9e10cd42016-08-03 14:12:14 -0700903 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700904 }
905 if _, ok := to.linker.(*ndkPrebuiltLibraryLinker); ok {
906 // These are allowed, but don't set sdk_version
Dan Albert9e10cd42016-08-03 14:12:14 -0700907 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700908 }
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700909 if _, ok := to.linker.(*ndkPrebuiltStlLinker); ok {
910 // These are allowed, but don't set sdk_version
Dan Albert9e10cd42016-08-03 14:12:14 -0700911 return
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700912 }
Colin Crossb916a382016-07-29 17:28:03 -0700913 if _, ok := to.linker.(*stubDecorator); ok {
Dan Albert914449f2016-06-17 16:45:24 -0700914 // These aren't real libraries, but are the stub shared libraries that are included in
915 // the NDK.
Dan Albert9e10cd42016-08-03 14:12:14 -0700916 return
Dan Albert914449f2016-06-17 16:45:24 -0700917 }
Dan Albert9e10cd42016-08-03 14:12:14 -0700918 if to.Properties.Sdk_version == "" {
919 // NDK code linking to platform code is never okay.
920 ctx.ModuleErrorf("depends on non-NDK-built library %q",
921 ctx.OtherModuleName(to))
922 }
923
924 // All this point we know we have two NDK libraries, but we need to
925 // check that we're not linking against anything built against a higher
926 // API level, as it is only valid to link against older or equivalent
927 // APIs.
928
929 if from.Properties.Sdk_version == "current" {
930 // Current can link against anything.
931 return
932 } else if to.Properties.Sdk_version == "current" {
933 // Current can't be linked against by anything else.
934 ctx.ModuleErrorf("links %q built against newer API version %q",
935 ctx.OtherModuleName(to), "current")
936 }
937
938 fromApi, err := strconv.Atoi(from.Properties.Sdk_version)
939 if err != nil {
940 ctx.PropertyErrorf("sdk_version",
941 "Invalid sdk_version value (must be int): %q",
942 from.Properties.Sdk_version)
943 }
944 toApi, err := strconv.Atoi(to.Properties.Sdk_version)
945 if err != nil {
946 ctx.PropertyErrorf("sdk_version",
947 "Invalid sdk_version value (must be int): %q",
948 to.Properties.Sdk_version)
949 }
950
951 if toApi > fromApi {
952 ctx.ModuleErrorf("links %q built against newer API version %q",
953 ctx.OtherModuleName(to), to.Properties.Sdk_version)
954 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700955 }
956
Colin Crossc99deeb2016-04-11 15:06:20 -0700957 ctx.VisitDirectDeps(func(m blueprint.Module) {
958 name := ctx.OtherModuleName(m)
959 tag := ctx.OtherModuleDependencyTag(m)
Colin Crossca860ac2016-01-04 14:34:37 -0800960
Colin Cross635c3b02016-05-18 15:37:25 -0700961 a, _ := m.(android.Module)
Colin Crossc99deeb2016-04-11 15:06:20 -0700962 if a == nil {
963 ctx.ModuleErrorf("module %q not an android module", name)
964 return
Colin Crossca860ac2016-01-04 14:34:37 -0800965 }
Colin Crossca860ac2016-01-04 14:34:37 -0800966
Dan Willemsena96ff642016-06-07 12:34:45 -0700967 cc, _ := m.(*Module)
968 if cc == nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700969 switch tag {
Colin Cross068e0fe2016-12-13 15:23:47 -0800970 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -0700971 // Nothing to do
Dan Willemsenb40aab62016-04-20 14:21:14 -0700972 case genSourceDepTag:
973 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
974 depPaths.GeneratedSources = append(depPaths.GeneratedSources,
975 genRule.GeneratedSourceFiles()...)
976 } else {
977 ctx.ModuleErrorf("module %q is not a gensrcs or genrule", name)
978 }
Colin Crosse90bfd12017-04-26 16:59:26 -0700979 // Support exported headers from a generated_sources dependency
980 fallthrough
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700981 case genHeaderDepTag, genHeaderExportDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700982 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
983 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders,
984 genRule.GeneratedSourceFiles()...)
Colin Cross5ed99c62016-11-22 12:55:55 -0800985 flags := includeDirsToFlags(genRule.GeneratedHeaderDirs())
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700986 depPaths.Flags = append(depPaths.Flags, flags)
987 if tag == genHeaderExportDepTag {
988 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700989 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps,
990 genRule.GeneratedSourceFiles()...)
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700991 // Add these re-exported flags to help header-abi-dumper to infer the abi exported by a library.
992 c.sabi.Properties.ReexportedIncludeFlags = append(c.sabi.Properties.ReexportedIncludeFlags, flags)
993
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700994 }
Dan Willemsenb40aab62016-04-20 14:21:14 -0700995 } else {
996 ctx.ModuleErrorf("module %q is not a genrule", name)
997 }
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700998 case linkerScriptDepTag:
999 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
1000 files := genRule.GeneratedSourceFiles()
1001 if len(files) == 1 {
1002 depPaths.LinkerScript = android.OptionalPathForPath(files[0])
1003 } else if len(files) > 1 {
1004 ctx.ModuleErrorf("module %q can only generate a single file if used for a linker script", name)
1005 }
1006 } else {
1007 ctx.ModuleErrorf("module %q is not a genrule", name)
1008 }
Dan Willemsenb40aab62016-04-20 14:21:14 -07001009 default:
Colin Crossc99deeb2016-04-11 15:06:20 -07001010 ctx.ModuleErrorf("depends on non-cc module %q", name)
Colin Crossca860ac2016-01-04 14:34:37 -08001011 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001012 return
1013 }
1014
1015 if !a.Enabled() {
Colin Crossa8f5e9a2016-12-13 12:51:11 -08001016 if ctx.AConfig().AllowMissingDependencies() {
1017 ctx.AddMissingDependencies([]string{name})
1018 } else {
1019 ctx.ModuleErrorf("depends on disabled module %q", name)
1020 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001021 return
1022 }
1023
Colin Crossa1ad8d12016-06-01 17:09:44 -07001024 if a.Target().Os != ctx.Os() {
1025 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), name)
1026 return
1027 }
1028
1029 if a.Target().Arch.ArchType != ctx.Arch().ArchType {
1030 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), name)
Colin Crossc99deeb2016-04-11 15:06:20 -07001031 return
1032 }
1033
Colin Crossc99deeb2016-04-11 15:06:20 -07001034 if tag == reuseObjTag {
Colin Crossbba99042016-11-23 15:45:05 -08001035 if l, ok := cc.compiler.(libraryInterface); ok {
Colin Crossbbc9f4d2017-05-03 16:24:55 -07001036 objs, flags, deps := l.reuseObjs()
Colin Cross10d22312017-05-03 11:01:58 -07001037 depPaths.Objs = depPaths.Objs.Append(objs)
1038 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Colin Crossbbc9f4d2017-05-03 16:24:55 -07001039 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps, deps...)
Colin Crossbba99042016-11-23 15:45:05 -08001040 return
1041 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001042 }
1043
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001044 if t, ok := tag.(dependencyTag); ok && t.library {
Dan Willemsena96ff642016-06-07 12:34:45 -07001045 if i, ok := cc.linker.(exportedFlagsProducer); ok {
Dan Willemsen76f08272016-07-09 00:14:08 -07001046 flags := i.exportedFlags()
Dan Willemsen847dcc72016-09-29 12:13:36 -07001047 deps := i.exportedFlagsDeps()
Dan Willemsen76f08272016-07-09 00:14:08 -07001048 depPaths.Flags = append(depPaths.Flags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -07001049 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders, deps...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001050
1051 if t.reexportFlags {
Dan Willemsen76f08272016-07-09 00:14:08 -07001052 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -07001053 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps, deps...)
Jayant Chowdhary715cac32017-04-20 06:53:59 -07001054 // 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 -07001055 // Re-exported shared library headers must be included as well since they can help us with type information
1056 // about template instantiations (instantiated from their headers).
1057 c.sabi.Properties.ReexportedIncludeFlags = append(c.sabi.Properties.ReexportedIncludeFlags, flags...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001058 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001059 }
Dan Willemsena96ff642016-06-07 12:34:45 -07001060
Dan Albert9e10cd42016-08-03 14:12:14 -07001061 checkLinkType(c, cc)
Colin Crossc99deeb2016-04-11 15:06:20 -07001062 }
1063
Colin Cross26c34ed2016-09-30 17:10:16 -07001064 var ptr *android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -07001065 var depPtr *android.Paths
Colin Crossc99deeb2016-04-11 15:06:20 -07001066
Colin Cross26c34ed2016-09-30 17:10:16 -07001067 linkFile := cc.outputFile
1068 depFile := android.OptionalPath{}
1069
Colin Crossc99deeb2016-04-11 15:06:20 -07001070 switch tag {
Dan Albert914449f2016-06-17 16:45:24 -07001071 case ndkStubDepTag, sharedDepTag, sharedExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001072 ptr = &depPaths.SharedLibs
1073 depPtr = &depPaths.SharedLibsDeps
1074 depFile = cc.linker.(libraryInterface).toc()
Dan Albert914449f2016-06-17 16:45:24 -07001075 case lateSharedDepTag, ndkLateStubDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001076 ptr = &depPaths.LateSharedLibs
1077 depPtr = &depPaths.LateSharedLibsDeps
1078 depFile = cc.linker.(libraryInterface).toc()
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001079 case staticDepTag, staticExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001080 ptr = &depPaths.StaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -07001081 case lateStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001082 ptr = &depPaths.LateStaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -07001083 case wholeStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001084 ptr = &depPaths.WholeStaticLibs
Colin Crossb916a382016-07-29 17:28:03 -07001085 staticLib, ok := cc.linker.(libraryInterface)
1086 if !ok || !staticLib.static() {
Dan Willemsena96ff642016-06-07 12:34:45 -07001087 ctx.ModuleErrorf("module %q not a static library", name)
Colin Crossc99deeb2016-04-11 15:06:20 -07001088 return
1089 }
1090
1091 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
1092 postfix := " (required by " + ctx.OtherModuleName(m) + ")"
1093 for i := range missingDeps {
1094 missingDeps[i] += postfix
1095 }
1096 ctx.AddMissingDependencies(missingDeps)
1097 }
Dan Willemsen5cb580f2016-09-26 17:33:01 -07001098 depPaths.WholeStaticLibObjs = depPaths.WholeStaticLibObjs.Append(staticLib.objs())
Colin Cross5950f382016-12-13 12:50:57 -08001099 case headerDepTag:
1100 // Nothing
Colin Crossc99deeb2016-04-11 15:06:20 -07001101 case objDepTag:
Dan Willemsen5cb580f2016-09-26 17:33:01 -07001102 depPaths.Objs.objFiles = append(depPaths.Objs.objFiles, linkFile.Path())
Colin Crossc99deeb2016-04-11 15:06:20 -07001103 case crtBeginDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001104 depPaths.CrtBegin = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -07001105 case crtEndDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001106 depPaths.CrtEnd = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -07001107 }
1108
Dan Willemsen581341d2017-02-09 16:16:31 -08001109 switch tag {
1110 case staticDepTag, staticExportDepTag, lateStaticDepTag:
1111 staticLib, ok := cc.linker.(libraryInterface)
1112 if !ok || !staticLib.static() {
1113 ctx.ModuleErrorf("module %q not a static library", name)
1114 return
1115 }
1116
1117 // When combining coverage files for shared libraries and executables, coverage files
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001118 // in static libraries act as if they were whole static libraries. The same goes for
1119 // source based Abi dump files.
Dan Willemsen581341d2017-02-09 16:16:31 -08001120 depPaths.StaticLibObjs.coverageFiles = append(depPaths.StaticLibObjs.coverageFiles,
1121 staticLib.objs().coverageFiles...)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001122 depPaths.StaticLibObjs.sAbiDumpFiles = append(depPaths.StaticLibObjs.sAbiDumpFiles,
1123 staticLib.objs().sAbiDumpFiles...)
Dan Willemsen581341d2017-02-09 16:16:31 -08001124 }
1125
Colin Cross26c34ed2016-09-30 17:10:16 -07001126 if ptr != nil {
Colin Crossce75d2c2016-10-06 16:12:58 -07001127 if !linkFile.Valid() {
1128 ctx.ModuleErrorf("module %q missing output file", name)
1129 return
1130 }
Colin Cross26c34ed2016-09-30 17:10:16 -07001131 *ptr = append(*ptr, linkFile.Path())
1132 }
1133
Colin Crossc99deeb2016-04-11 15:06:20 -07001134 if depPtr != nil {
Colin Cross26c34ed2016-09-30 17:10:16 -07001135 dep := depFile
1136 if !dep.Valid() {
1137 dep = linkFile
1138 }
1139 *depPtr = append(*depPtr, dep.Path())
Colin Crossca860ac2016-01-04 14:34:37 -08001140 }
Jiyong Park27b188b2017-07-18 13:23:39 +09001141
1142 // Export the shared libs to the make world. In doing so, .vendor suffix
1143 // is added if the lib has both core and vendor variants and this module
1144 // is building against vndk. This is because the vendor variant will be
1145 // have .vendor suffix in its name in the make world. However, if the
1146 // lib is a vendor-only lib or this lib is not building against vndk,
1147 // then the suffix is not added.
1148 switch tag {
1149 case sharedDepTag, sharedExportDepTag, lateSharedDepTag:
1150 libName := strings.TrimSuffix(name, llndkLibrarySuffix)
1151 libName = strings.TrimPrefix(libName, "prebuilt_")
Jiyong Parkd5b18a52017-08-03 21:22:50 +09001152 isLLndk := inList(libName, llndkLibraries)
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001153 if c.vndk() && (Bool(cc.VendorProperties.Vendor_available) || isLLndk) {
Jiyong Park27b188b2017-07-18 13:23:39 +09001154 libName += vendorSuffix
1155 }
1156 // Note: the order of libs in this list is not important because
1157 // they merely serve as dependencies in the make world and do not
1158 // affect this lib itself.
1159 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, libName)
1160 }
Colin Crossca860ac2016-01-04 14:34:37 -08001161 })
1162
Colin Crossdd84e052017-05-17 13:44:16 -07001163 // Dedup exported flags from dependencies
1164 depPaths.Flags = firstUniqueElements(depPaths.Flags)
Dan Willemsenfe92c962017-08-29 12:28:37 -07001165 depPaths.GeneratedHeaders = android.FirstUniquePaths(depPaths.GeneratedHeaders)
1166 depPaths.ReexportedFlags = firstUniqueElements(depPaths.ReexportedFlags)
1167 depPaths.ReexportedFlagsDeps = android.FirstUniquePaths(depPaths.ReexportedFlagsDeps)
1168
1169 if c.sabi != nil {
1170 c.sabi.Properties.ReexportedIncludeFlags = firstUniqueElements(c.sabi.Properties.ReexportedIncludeFlags)
1171 }
Colin Crossdd84e052017-05-17 13:44:16 -07001172
Colin Crossca860ac2016-01-04 14:34:37 -08001173 return depPaths
1174}
1175
1176func (c *Module) InstallInData() bool {
1177 if c.installer == nil {
1178 return false
1179 }
Vishwath Mohan1dd88392017-03-29 22:00:18 -07001180 return c.installer.inData()
1181}
1182
1183func (c *Module) InstallInSanitizerDir() bool {
1184 if c.installer == nil {
1185 return false
1186 }
1187 if c.sanitize != nil && c.sanitize.inSanitizerDir() {
Colin Cross94610402016-08-29 13:41:32 -07001188 return true
1189 }
Vishwath Mohan1dd88392017-03-29 22:00:18 -07001190 return c.installer.inSanitizerDir()
Colin Crossca860ac2016-01-04 14:34:37 -08001191}
1192
Dan Willemsen4aa75ca2016-09-28 16:18:03 -07001193func (c *Module) HostToolPath() android.OptionalPath {
1194 if c.installer == nil {
1195 return android.OptionalPath{}
1196 }
1197 return c.installer.hostToolPath()
1198}
1199
Nan Zhangd4e641b2017-07-12 12:55:28 -07001200func (c *Module) IntermPathForModuleOut() android.OptionalPath {
1201 return c.outputFile
1202}
1203
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001204func (c *Module) Srcs() android.Paths {
1205 if c.outputFile.Valid() {
1206 return android.Paths{c.outputFile.Path()}
1207 }
1208 return android.Paths{}
1209}
1210
Colin Cross2ba19d92015-05-07 15:44:20 -07001211//
Colin Crosscfad1192015-11-02 16:43:11 -08001212// Defaults
1213//
Colin Crossca860ac2016-01-04 14:34:37 -08001214type Defaults struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001215 android.ModuleBase
Colin Cross1f44a3a2017-07-07 14:33:33 -07001216 android.DefaultsModuleBase
Colin Crosscfad1192015-11-02 16:43:11 -08001217}
1218
Colin Cross635c3b02016-05-18 15:37:25 -07001219func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -08001220}
1221
Colin Cross1e676be2016-10-12 14:38:15 -07001222func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
1223}
1224
Colin Cross36242852017-06-23 15:06:31 -07001225func defaultsFactory() android.Module {
Colin Crosse1d764e2016-08-18 14:18:32 -07001226 return DefaultsFactory()
1227}
1228
Colin Cross36242852017-06-23 15:06:31 -07001229func DefaultsFactory(props ...interface{}) android.Module {
Colin Crossca860ac2016-01-04 14:34:37 -08001230 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -08001231
Colin Cross36242852017-06-23 15:06:31 -07001232 module.AddProperties(props...)
1233 module.AddProperties(
Colin Crossca860ac2016-01-04 14:34:37 -08001234 &BaseProperties{},
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001235 &VendorProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001236 &BaseCompilerProperties{},
1237 &BaseLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -07001238 &LibraryProperties{},
Colin Cross919281a2016-04-05 16:42:05 -07001239 &FlagExporterProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001240 &BinaryLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -07001241 &TestProperties{},
1242 &TestBinaryProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001243 &UnusedProperties{},
1244 &StlProperties{},
Colin Cross16b23492016-01-06 14:41:07 -08001245 &SanitizeProperties{},
Colin Cross665dce92016-04-28 14:50:03 -07001246 &StripProperties{},
Dan Willemsen7424d612016-09-01 13:45:39 -07001247 &InstallerProperties{},
Dan Willemsena03cf6d2016-09-26 15:45:04 -07001248 &TidyProperties{},
Dan Willemsen581341d2017-02-09 16:16:31 -08001249 &CoverageProperties{},
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001250 &SAbiProperties{},
Justin Yun4b2382f2017-07-26 14:22:10 +09001251 &VndkProperties{},
Stephen Craneba090d12017-05-09 15:44:35 -07001252 &LTOProperties{},
Colin Crosse1d764e2016-08-18 14:18:32 -07001253 )
Colin Crosscfad1192015-11-02 16:43:11 -08001254
Colin Cross1f44a3a2017-07-07 14:33:33 -07001255 android.InitDefaultsModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001256
1257 return module
Colin Crosscfad1192015-11-02 16:43:11 -08001258}
1259
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001260const (
1261 // coreMode is the variant used for framework-private libraries, or
1262 // SDK libraries. (which framework-private libraries can use)
1263 coreMode = "core"
1264
1265 // vendorMode is the variant used for /vendor code that compiles
1266 // against the VNDK.
1267 vendorMode = "vendor"
1268)
1269
1270func vendorMutator(mctx android.BottomUpMutatorContext) {
1271 if mctx.Os() != android.Android {
1272 return
1273 }
1274
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001275 if genrule, ok := mctx.Module().(*genrule.Module); ok {
1276 if props, ok := genrule.Extra.(*VendorProperties); ok {
1277 if !mctx.DeviceConfig().CompileVndk() {
1278 mctx.CreateVariations(coreMode)
1279 } else if Bool(props.Vendor_available) {
1280 mctx.CreateVariations(coreMode, vendorMode)
1281 } else if mctx.Vendor() {
1282 mctx.CreateVariations(vendorMode)
1283 } else {
1284 mctx.CreateVariations(coreMode)
1285 }
1286 }
1287 }
1288
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001289 m, ok := mctx.Module().(*Module)
1290 if !ok {
1291 return
1292 }
1293
1294 // Sanity check
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001295 if Bool(m.VendorProperties.Vendor_available) && mctx.Vendor() {
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001296 mctx.PropertyErrorf("vendor_available",
1297 "doesn't make sense at the same time as `vendor: true` or `proprietary: true`")
1298 return
1299 }
Justin Yun8effde42017-06-23 19:24:43 +09001300 if vndk := m.vndkdep; vndk != nil {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001301 if vndk.isVndk() && !Bool(m.VendorProperties.Vendor_available) {
Justin Yun8effde42017-06-23 19:24:43 +09001302 mctx.PropertyErrorf("vndk",
1303 "has to define `vendor_available: true` to enable vndk")
1304 return
1305 }
1306 if !vndk.isVndk() && vndk.isVndkSp() {
1307 mctx.PropertyErrorf("vndk",
1308 "must set `enabled: true` to set `support_system_process: true`")
1309 return
1310 }
1311 }
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001312
1313 if !mctx.DeviceConfig().CompileVndk() {
1314 // If the device isn't compiling against the VNDK, we always
1315 // use the core mode.
1316 mctx.CreateVariations(coreMode)
1317 } else if _, ok := m.linker.(*llndkStubDecorator); ok {
1318 // LL-NDK stubs only exist in the vendor variant, since the
1319 // real libraries will be used in the core variant.
1320 mctx.CreateVariations(vendorMode)
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001321 } else if Bool(m.VendorProperties.Vendor_available) {
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001322 // This will be available in both /system and /vendor
Justin Yun8effde42017-06-23 19:24:43 +09001323 // or a /system directory that is available to vendor.
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001324 mod := mctx.CreateVariations(coreMode, vendorMode)
1325 mod[1].(*Module).Properties.UseVndk = true
1326 } else if mctx.Vendor() && m.Properties.Sdk_version == "" {
1327 // This will be available in /vendor only
1328 mod := mctx.CreateVariations(vendorMode)
1329 mod[0].(*Module).Properties.UseVndk = true
1330 } else {
1331 // This is either in /system (or similar: /data), or is a
1332 // modules built with the NDK. Modules built with the NDK
1333 // will be restricted using the existing link type checks.
1334 mctx.CreateVariations(coreMode)
1335 }
1336}
1337
Colin Crossdd84e052017-05-17 13:44:16 -07001338// firstUniqueElements returns all unique elements of a slice, keeping the first copy of each
1339// modifies the slice contents in place, and returns a subslice of the original slice
1340func firstUniqueElements(list []string) []string {
1341 k := 0
1342outer:
1343 for i := 0; i < len(list); i++ {
1344 for j := 0; j < k; j++ {
1345 if list[i] == list[j] {
1346 continue outer
1347 }
1348 }
1349 list[k] = list[i]
1350 k++
1351 }
1352 return list[:k]
1353}
1354
Colin Cross74d1ec02015-04-28 13:30:13 -07001355// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
1356// modifies the slice contents in place, and returns a subslice of the original slice
1357func lastUniqueElements(list []string) []string {
1358 totalSkip := 0
1359 for i := len(list) - 1; i >= totalSkip; i-- {
1360 skip := 0
1361 for j := i - 1; j >= totalSkip; j-- {
1362 if list[i] == list[j] {
1363 skip++
1364 } else {
1365 list[j+skip] = list[j]
1366 }
1367 }
1368 totalSkip += skip
1369 }
1370 return list[totalSkip:]
1371}
Colin Cross06a931b2015-10-28 17:23:31 -07001372
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -07001373func getCurrentNdkPrebuiltVersion(ctx DepsContext) string {
1374 if ctx.AConfig().PlatformSdkVersionInt() > config.NdkMaxPrebuiltVersionInt {
1375 return strconv.Itoa(config.NdkMaxPrebuiltVersionInt)
1376 }
1377 return ctx.AConfig().PlatformSdkVersion()
1378}
1379
Colin Cross06a931b2015-10-28 17:23:31 -07001380var Bool = proptools.Bool