blob: cdbe43e5a3e0b54180bfe9e1062784a303d4af6a [file] [log] [blame]
Colin Cross5049f022015-03-18 13:28:46 -07001// Copyright 2015 Google Inc. All rights reserved.
Colin Cross3f40fa42015-01-30 17:27:36 -08002//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package cc
16
17// This file contains the module types for compiling C/C++ for Android, and converts the properties
18// into the flags and filenames necessary to pass to the compiler. The final creation of the rules
19// is handled in builder.go
20
21import (
Dan Albert9e10cd42016-08-03 14:12:14 -070022 "strconv"
Colin Cross3f40fa42015-01-30 17:27:36 -080023 "strings"
24
Colin Cross97ba0732015-03-23 17:50:24 -070025 "github.com/google/blueprint"
Colin Cross06a931b2015-10-28 17:23:31 -070026 "github.com/google/blueprint/proptools"
Colin Cross97ba0732015-03-23 17:50:24 -070027
Colin Cross635c3b02016-05-18 15:37:25 -070028 "android/soong/android"
Colin Crossb98c8b02016-07-29 13:44:28 -070029 "android/soong/cc/config"
Colin Cross5049f022015-03-18 13:28:46 -070030 "android/soong/genrule"
Colin Cross3f40fa42015-01-30 17:27:36 -080031)
32
Colin Cross463a90e2015-06-17 14:20:06 -070033func init() {
Colin Cross798bfce2016-10-12 14:28:16 -070034 android.RegisterModuleType("cc_defaults", defaultsFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070035
Colin Cross1e676be2016-10-12 14:38:15 -070036 android.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
Jiyong Park6a43f042017-10-12 23:05:00 +090037 ctx.BottomUp("image", vendorMutator).Parallel()
Colin Cross1e676be2016-10-12 14:38:15 -070038 ctx.BottomUp("link", linkageMutator).Parallel()
Jiyong Parkd5b18a52017-08-03 21:22:50 +090039 ctx.BottomUp("vndk", vndkMutator).Parallel()
Colin Cross1e676be2016-10-12 14:38:15 -070040 ctx.BottomUp("ndk_api", ndkApiMutator).Parallel()
41 ctx.BottomUp("test_per_src", testPerSrcMutator).Parallel()
42 ctx.BottomUp("begin", beginMutator).Parallel()
43 })
Colin Cross16b23492016-01-06 14:41:07 -080044
Colin Cross1e676be2016-10-12 14:38:15 -070045 android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
46 ctx.TopDown("asan_deps", sanitizerDepsMutator(asan))
47 ctx.BottomUp("asan", sanitizerMutator(asan)).Parallel()
Colin Cross16b23492016-01-06 14:41:07 -080048
Colin Cross1e676be2016-10-12 14:38:15 -070049 ctx.TopDown("tsan_deps", sanitizerDepsMutator(tsan))
50 ctx.BottomUp("tsan", sanitizerMutator(tsan)).Parallel()
Dan Willemsen581341d2017-02-09 16:16:31 -080051
52 ctx.BottomUp("coverage", coverageLinkingMutator).Parallel()
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -080053 ctx.TopDown("vndk_deps", sabiDepsMutator)
Stephen Craneba090d12017-05-09 15:44:35 -070054
55 ctx.TopDown("lto_deps", ltoDepsMutator)
56 ctx.BottomUp("lto", ltoMutator).Parallel()
Colin Cross1e676be2016-10-12 14:38:15 -070057 })
Colin Crossb98c8b02016-07-29 13:44:28 -070058
59 pctx.Import("android/soong/cc/config")
Colin Cross463a90e2015-06-17 14:20:06 -070060}
61
Colin Crossca860ac2016-01-04 14:34:37 -080062type Deps struct {
63 SharedLibs, LateSharedLibs []string
64 StaticLibs, LateStaticLibs, WholeStaticLibs []string
Colin Cross5950f382016-12-13 12:50:57 -080065 HeaderLibs []string
Colin Crossc472d572015-03-17 15:06:21 -070066
Colin Cross5950f382016-12-13 12:50:57 -080067 ReexportSharedLibHeaders, ReexportStaticLibHeaders, ReexportHeaderLibHeaders []string
Dan Willemsen490a8dc2016-06-06 18:22:19 -070068
Colin Cross81413472016-04-11 14:37:39 -070069 ObjFiles []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -070070
Dan Willemsenb40aab62016-04-20 14:21:14 -070071 GeneratedSources []string
72 GeneratedHeaders []string
73
Dan Willemsenb3454ab2016-09-28 17:34:58 -070074 ReexportGeneratedHeaders []string
75
Colin Cross97ba0732015-03-23 17:50:24 -070076 CrtBegin, CrtEnd string
Dan Willemsenc77a0b32017-09-18 23:19:12 -070077 LinkerScript string
Colin Crossc472d572015-03-17 15:06:21 -070078}
79
Colin Crossca860ac2016-01-04 14:34:37 -080080type PathDeps struct {
Colin Cross26c34ed2016-09-30 17:10:16 -070081 // Paths to .so files
82 SharedLibs, LateSharedLibs android.Paths
83 // Paths to the dependencies to use for .so files (.so.toc files)
84 SharedLibsDeps, LateSharedLibsDeps android.Paths
85 // Paths to .a files
Colin Cross635c3b02016-05-18 15:37:25 -070086 StaticLibs, LateStaticLibs, WholeStaticLibs android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -070087
Colin Cross26c34ed2016-09-30 17:10:16 -070088 // Paths to .o files
Dan Willemsen5cb580f2016-09-26 17:33:01 -070089 Objs Objects
Dan Willemsen581341d2017-02-09 16:16:31 -080090 StaticLibObjs Objects
Dan Willemsen5cb580f2016-09-26 17:33:01 -070091 WholeStaticLibObjs Objects
Dan Willemsen34cc69e2015-09-23 15:26:20 -070092
Colin Cross26c34ed2016-09-30 17:10:16 -070093 // Paths to generated source files
Colin Cross635c3b02016-05-18 15:37:25 -070094 GeneratedSources android.Paths
95 GeneratedHeaders android.Paths
Dan Willemsenb40aab62016-04-20 14:21:14 -070096
Dan Willemsen76f08272016-07-09 00:14:08 -070097 Flags, ReexportedFlags []string
Dan Willemsen847dcc72016-09-29 12:13:36 -070098 ReexportedFlagsDeps android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -070099
Colin Cross26c34ed2016-09-30 17:10:16 -0700100 // Paths to crt*.o files
Colin Cross635c3b02016-05-18 15:37:25 -0700101 CrtBegin, CrtEnd android.OptionalPath
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700102 LinkerScript android.OptionalPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700103}
104
Colin Crossca860ac2016-01-04 14:34:37 -0800105type Flags struct {
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700106 GlobalFlags []string // Flags that apply to C, C++, and assembly source files
107 ArFlags []string // Flags that apply to ar
108 AsFlags []string // Flags that apply to assembly source files
109 CFlags []string // Flags that apply to C and C++ source files
110 ToolingCFlags []string // Flags that apply to C and C++ source files parsed by clang LibTooling tools
111 ConlyFlags []string // Flags that apply to C source files
112 CppFlags []string // Flags that apply to C++ source files
113 ToolingCppFlags []string // Flags that apply to C++ source files parsed by clang LibTooling tools
114 YaccFlags []string // Flags that apply to Yacc source files
115 protoFlags []string // Flags that apply to proto source files
116 aidlFlags []string // Flags that apply to aidl source files
117 rsFlags []string // Flags that apply to renderscript source files
118 LdFlags []string // Flags that apply to linker command lines
119 libFlags []string // Flags to add libraries early to the link order
120 TidyFlags []string // Flags that apply to clang-tidy
121 SAbiFlags []string // Flags that apply to header-abi-dumper
122 YasmFlags []string // Flags that apply to yasm assembly source files
Colin Cross28344522015-04-22 13:07:53 -0700123
Colin Crossc3199482017-03-30 15:03:04 -0700124 // Global include flags that apply to C, C++, and assembly source files
125 // These must be after any module include flags, which will be in GlobalFlags.
126 SystemIncludeFlags []string
127
Colin Crossb98c8b02016-07-29 13:44:28 -0700128 Toolchain config.Toolchain
Colin Cross28344522015-04-22 13:07:53 -0700129 Clang bool
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700130 Tidy bool
Dan Willemsen581341d2017-02-09 16:16:31 -0800131 Coverage bool
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800132 SAbiDump bool
Colin Crossca860ac2016-01-04 14:34:37 -0800133
134 RequiredInstructionSet string
Colin Cross16b23492016-01-06 14:41:07 -0800135 DynamicLinker string
136
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700137 CFlagsDeps android.Paths // Files depended on by compiler flags
138 LdFlagsDeps android.Paths // Files depended on by linker flags
Colin Cross18c0c5a2016-12-01 14:45:23 -0800139
140 GroupStaticLibs bool
Colin Crossc472d572015-03-17 15:06:21 -0700141}
142
Colin Cross81413472016-04-11 14:37:39 -0700143type ObjectLinkerProperties struct {
144 // names of other cc_object modules to link into this module using partial linking
145 Objs []string `android:"arch_variant"`
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700146
147 // if set, add an extra objcopy --prefix-symbols= step
148 Prefix_symbols string
Colin Cross81413472016-04-11 14:37:39 -0700149}
150
Colin Crossca860ac2016-01-04 14:34:37 -0800151// Properties used to compile all C or C++ modules
152type BaseProperties struct {
153 // compile module with clang instead of gcc
154 Clang *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700155
156 // Minimum sdk version supported when compiling against the ndk
157 Sdk_version string
158
Colin Crossca860ac2016-01-04 14:34:37 -0800159 // don't insert default compiler flags into asflags, cflags,
160 // cppflags, conlyflags, ldflags, or include_dirs
161 No_default_compiler_flags *bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700162
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700163 AndroidMkSharedLibs []string `blueprint:"mutated"`
164 HideFromMake bool `blueprint:"mutated"`
165 PreventInstall bool `blueprint:"mutated"`
166
167 UseVndk bool `blueprint:"mutated"`
168}
169
170type VendorProperties struct {
Jiyong Park82e2bf32017-08-16 14:05:54 +0900171 // whether this module should be allowed to be directly depended by other
172 // modules with `vendor: true`, `proprietary: true`, or `vendor_available:true`.
173 // If set to true, two variants will be built separately, one like
174 // normal, and the other limited to the set of libraries and headers
175 // that are exposed to /vendor modules.
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700176 //
177 // The vendor variant may be used with a different (newer) /system,
178 // so it shouldn't have any unversioned runtime dependencies, or
179 // make assumptions about the system that may not be true in the
180 // future.
181 //
Jiyong Park82e2bf32017-08-16 14:05:54 +0900182 // If set to false, this module becomes inaccessible from /vendor modules.
183 //
184 // Default value is true when vndk: {enabled: true} or vendor: true.
185 //
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700186 // Nothing happens if BOARD_VNDK_VERSION isn't set in the BoardConfig.mk
187 Vendor_available *bool
Colin Crossca860ac2016-01-04 14:34:37 -0800188}
189
Colin Crossca860ac2016-01-04 14:34:37 -0800190type UnusedProperties struct {
Dan Willemsen581341d2017-02-09 16:16:31 -0800191 Tags []string
Colin Crosscfad1192015-11-02 16:43:11 -0800192}
193
Colin Crossca860ac2016-01-04 14:34:37 -0800194type ModuleContextIntf interface {
Colin Crossca860ac2016-01-04 14:34:37 -0800195 static() bool
196 staticBinary() bool
197 clang() bool
Colin Crossb98c8b02016-07-29 13:44:28 -0700198 toolchain() config.Toolchain
Colin Crossca860ac2016-01-04 14:34:37 -0800199 noDefaultCompilerFlags() bool
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700200 useSdk() bool
Colin Crossca860ac2016-01-04 14:34:37 -0800201 sdkVersion() string
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700202 useVndk() bool
Justin Yun8effde42017-06-23 19:24:43 +0900203 isVndk() bool
204 isVndkSp() bool
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800205 createVndkSourceAbiDump() bool
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700206 selectedStl() string
Colin Crossce75d2c2016-10-06 16:12:58 -0700207 baseModuleName() string
Colin Crossca860ac2016-01-04 14:34:37 -0800208}
209
210type ModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700211 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800212 ModuleContextIntf
213}
214
215type BaseModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700216 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800217 ModuleContextIntf
218}
219
Colin Cross37047f12016-12-13 17:06:13 -0800220type DepsContext interface {
221 android.BottomUpMutatorContext
222 ModuleContextIntf
223}
224
Colin Crossca860ac2016-01-04 14:34:37 -0800225type feature interface {
226 begin(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800227 deps(ctx DepsContext, deps Deps) Deps
Colin Crossca860ac2016-01-04 14:34:37 -0800228 flags(ctx ModuleContext, flags Flags) Flags
229 props() []interface{}
230}
231
232type compiler interface {
Colin Cross42742b82016-08-01 13:20:05 -0700233 compilerInit(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800234 compilerDeps(ctx DepsContext, deps Deps) Deps
Colin Cross42742b82016-08-01 13:20:05 -0700235 compilerFlags(ctx ModuleContext, flags Flags) Flags
236 compilerProps() []interface{}
237
Colin Cross76fada02016-07-27 10:31:13 -0700238 appendCflags([]string)
239 appendAsflags([]string)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700240 compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects
Colin Crossca860ac2016-01-04 14:34:37 -0800241}
242
243type linker interface {
Colin Cross42742b82016-08-01 13:20:05 -0700244 linkerInit(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800245 linkerDeps(ctx DepsContext, deps Deps) Deps
Colin Cross42742b82016-08-01 13:20:05 -0700246 linkerFlags(ctx ModuleContext, flags Flags) Flags
247 linkerProps() []interface{}
248
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700249 link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path
Colin Cross76fada02016-07-27 10:31:13 -0700250 appendLdflags([]string)
Colin Crossca860ac2016-01-04 14:34:37 -0800251}
252
253type installer interface {
Colin Cross42742b82016-08-01 13:20:05 -0700254 installerProps() []interface{}
Colin Cross635c3b02016-05-18 15:37:25 -0700255 install(ctx ModuleContext, path android.Path)
Colin Crossca860ac2016-01-04 14:34:37 -0800256 inData() bool
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700257 inSanitizerDir() bool
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700258 hostToolPath() android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800259}
260
Colin Crossc99deeb2016-04-11 15:06:20 -0700261type dependencyTag struct {
262 blueprint.BaseDependencyTag
263 name string
264 library bool
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700265
266 reexportFlags bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700267}
268
269var (
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700270 sharedDepTag = dependencyTag{name: "shared", library: true}
271 sharedExportDepTag = dependencyTag{name: "shared", library: true, reexportFlags: true}
272 lateSharedDepTag = dependencyTag{name: "late shared", library: true}
273 staticDepTag = dependencyTag{name: "static", library: true}
274 staticExportDepTag = dependencyTag{name: "static", library: true, reexportFlags: true}
275 lateStaticDepTag = dependencyTag{name: "late static", library: true}
276 wholeStaticDepTag = dependencyTag{name: "whole static", library: true, reexportFlags: true}
Colin Cross32ec36c2016-12-15 07:39:51 -0800277 headerDepTag = dependencyTag{name: "header", library: true}
278 headerExportDepTag = dependencyTag{name: "header", library: true, reexportFlags: true}
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700279 genSourceDepTag = dependencyTag{name: "gen source"}
280 genHeaderDepTag = dependencyTag{name: "gen header"}
281 genHeaderExportDepTag = dependencyTag{name: "gen header", reexportFlags: true}
282 objDepTag = dependencyTag{name: "obj"}
283 crtBeginDepTag = dependencyTag{name: "crtbegin"}
284 crtEndDepTag = dependencyTag{name: "crtend"}
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700285 linkerScriptDepTag = dependencyTag{name: "linker script"}
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700286 reuseObjTag = dependencyTag{name: "reuse objects"}
287 ndkStubDepTag = dependencyTag{name: "ndk stub", library: true}
288 ndkLateStubDepTag = dependencyTag{name: "ndk late stub", library: true}
Colin Crossc99deeb2016-04-11 15:06:20 -0700289)
290
Colin Crossca860ac2016-01-04 14:34:37 -0800291// Module contains the properties and members used by all C/C++ module types, and implements
292// the blueprint.Module interface. It delegates to compiler, linker, and installer interfaces
293// to construct the output file. Behavior can be customized with a Customizer interface
294type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700295 android.ModuleBase
Colin Cross1f44a3a2017-07-07 14:33:33 -0700296 android.DefaultableModuleBase
Colin Crossc472d572015-03-17 15:06:21 -0700297
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700298 Properties BaseProperties
299 VendorProperties VendorProperties
300 unused UnusedProperties
Colin Crossfa138792015-04-24 17:31:52 -0700301
Colin Crossca860ac2016-01-04 14:34:37 -0800302 // initialize before calling Init
Colin Cross635c3b02016-05-18 15:37:25 -0700303 hod android.HostOrDeviceSupported
304 multilib android.Multilib
Colin Crossc472d572015-03-17 15:06:21 -0700305
Colin Crossca860ac2016-01-04 14:34:37 -0800306 // delegates, initialize before calling Init
Colin Crossb4ce0ec2016-09-13 13:41:39 -0700307 features []feature
308 compiler compiler
309 linker linker
310 installer installer
311 stl *stl
312 sanitize *sanitize
Dan Willemsen581341d2017-02-09 16:16:31 -0800313 coverage *coverage
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800314 sabi *sabi
Justin Yun8effde42017-06-23 19:24:43 +0900315 vndkdep *vndkdep
Stephen Craneba090d12017-05-09 15:44:35 -0700316 lto *lto
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700317 pgo *pgo
Colin Cross16b23492016-01-06 14:41:07 -0800318
319 androidMkSharedLibDeps []string
Colin Cross74d1ec02015-04-28 13:30:13 -0700320
Colin Cross635c3b02016-05-18 15:37:25 -0700321 outputFile android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800322
Colin Crossb98c8b02016-07-29 13:44:28 -0700323 cachedToolchain config.Toolchain
Colin Crossb916a382016-07-29 17:28:03 -0700324
325 subAndroidMkOnce map[subAndroidMkProvider]bool
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800326
327 // Flags used to compile this module
328 flags Flags
Jeff Gaston294356f2017-09-27 17:05:30 -0700329
330 // When calling a linker, if module A depends on module B, then A must precede B in its command
331 // line invocation. staticDepsInLinkOrder stores the proper ordering of all of the transitive
332 // deps of this module
333 staticDepsInLinkOrder android.Paths
Colin Crossc472d572015-03-17 15:06:21 -0700334}
335
Colin Cross36242852017-06-23 15:06:31 -0700336func (c *Module) Init() android.Module {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -0700337 c.AddProperties(&c.Properties, &c.VendorProperties, &c.unused)
Colin Crossca860ac2016-01-04 14:34:37 -0800338 if c.compiler != nil {
Colin Cross36242852017-06-23 15:06:31 -0700339 c.AddProperties(c.compiler.compilerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800340 }
341 if c.linker != nil {
Colin Cross36242852017-06-23 15:06:31 -0700342 c.AddProperties(c.linker.linkerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800343 }
344 if c.installer != nil {
Colin Cross36242852017-06-23 15:06:31 -0700345 c.AddProperties(c.installer.installerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800346 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700347 if c.stl != nil {
Colin Cross36242852017-06-23 15:06:31 -0700348 c.AddProperties(c.stl.props()...)
Colin Crossa8e07cc2016-04-04 15:07:06 -0700349 }
Colin Cross16b23492016-01-06 14:41:07 -0800350 if c.sanitize != nil {
Colin Cross36242852017-06-23 15:06:31 -0700351 c.AddProperties(c.sanitize.props()...)
Colin Cross16b23492016-01-06 14:41:07 -0800352 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800353 if c.coverage != nil {
Colin Cross36242852017-06-23 15:06:31 -0700354 c.AddProperties(c.coverage.props()...)
Dan Willemsen581341d2017-02-09 16:16:31 -0800355 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800356 if c.sabi != nil {
Colin Cross36242852017-06-23 15:06:31 -0700357 c.AddProperties(c.sabi.props()...)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800358 }
Justin Yun8effde42017-06-23 19:24:43 +0900359 if c.vndkdep != nil {
360 c.AddProperties(c.vndkdep.props()...)
361 }
Stephen Craneba090d12017-05-09 15:44:35 -0700362 if c.lto != nil {
363 c.AddProperties(c.lto.props()...)
364 }
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700365 if c.pgo != nil {
366 c.AddProperties(c.pgo.props()...)
367 }
Colin Crossca860ac2016-01-04 14:34:37 -0800368 for _, feature := range c.features {
Colin Cross36242852017-06-23 15:06:31 -0700369 c.AddProperties(feature.props()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800370 }
Colin Crossc472d572015-03-17 15:06:21 -0700371
Colin Cross36242852017-06-23 15:06:31 -0700372 android.InitAndroidArchModule(c, c.hod, c.multilib)
Colin Crossc472d572015-03-17 15:06:21 -0700373
Colin Cross1f44a3a2017-07-07 14:33:33 -0700374 android.InitDefaultableModule(c)
Colin Cross36242852017-06-23 15:06:31 -0700375
376 return c
Colin Crossc472d572015-03-17 15:06:21 -0700377}
378
Colin Crossb916a382016-07-29 17:28:03 -0700379// Returns true for dependency roots (binaries)
380// TODO(ccross): also handle dlopenable libraries
381func (c *Module) isDependencyRoot() bool {
382 if root, ok := c.linker.(interface {
383 isDependencyRoot() bool
384 }); ok {
385 return root.isDependencyRoot()
386 }
387 return false
388}
389
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700390func (c *Module) useVndk() bool {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700391 return c.Properties.UseVndk
392}
393
Justin Yun8effde42017-06-23 19:24:43 +0900394func (c *Module) isVndk() bool {
395 if c.vndkdep != nil {
396 return c.vndkdep.isVndk()
397 }
398 return false
399}
400
Jiyong Park82e2bf32017-08-16 14:05:54 +0900401// Returns true only when this module is configured to have core and vendor
402// variants.
403func (c *Module) hasVendorVariant() bool {
404 return c.isVndk() || Bool(c.VendorProperties.Vendor_available)
405}
406
Colin Crossca860ac2016-01-04 14:34:37 -0800407type baseModuleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700408 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800409 moduleContextImpl
410}
411
Colin Cross37047f12016-12-13 17:06:13 -0800412type depsContext struct {
413 android.BottomUpMutatorContext
414 moduleContextImpl
415}
416
Colin Crossca860ac2016-01-04 14:34:37 -0800417type moduleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700418 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800419 moduleContextImpl
420}
421
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700422func (ctx *moduleContext) InstallOnVendorPartition() bool {
423 return ctx.ModuleContext.InstallOnVendorPartition() || (ctx.mod.useVndk() && !ctx.mod.isVndk())
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700424}
425
Colin Crossca860ac2016-01-04 14:34:37 -0800426type moduleContextImpl struct {
427 mod *Module
428 ctx BaseModuleContext
429}
430
Colin Crossca860ac2016-01-04 14:34:37 -0800431func (ctx *moduleContextImpl) clang() bool {
432 return ctx.mod.clang(ctx.ctx)
433}
434
Colin Crossb98c8b02016-07-29 13:44:28 -0700435func (ctx *moduleContextImpl) toolchain() config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800436 return ctx.mod.toolchain(ctx.ctx)
437}
438
439func (ctx *moduleContextImpl) static() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700440 if static, ok := ctx.mod.linker.(interface {
441 static() bool
442 }); ok {
443 return static.static()
Colin Crossca860ac2016-01-04 14:34:37 -0800444 }
Colin Crossb916a382016-07-29 17:28:03 -0700445 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800446}
447
448func (ctx *moduleContextImpl) staticBinary() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700449 if static, ok := ctx.mod.linker.(interface {
450 staticBinary() bool
451 }); ok {
452 return static.staticBinary()
Colin Crossca860ac2016-01-04 14:34:37 -0800453 }
Colin Crossb916a382016-07-29 17:28:03 -0700454 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800455}
456
457func (ctx *moduleContextImpl) noDefaultCompilerFlags() bool {
458 return Bool(ctx.mod.Properties.No_default_compiler_flags)
459}
460
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700461func (ctx *moduleContextImpl) useSdk() bool {
462 if ctx.ctx.Device() && !ctx.useVndk() {
Dan Willemsena96ff642016-06-07 12:34:45 -0700463 return ctx.mod.Properties.Sdk_version != ""
464 }
465 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800466}
467
468func (ctx *moduleContextImpl) sdkVersion() string {
Dan Willemsena96ff642016-06-07 12:34:45 -0700469 if ctx.ctx.Device() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700470 if ctx.useVndk() {
Dan Willemsen11b26142017-03-19 18:30:37 -0700471 return "current"
Dan Willemsend2ede872016-11-18 14:54:24 -0800472 } else {
473 return ctx.mod.Properties.Sdk_version
474 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700475 }
476 return ""
Colin Crossca860ac2016-01-04 14:34:37 -0800477}
478
Justin Yun8effde42017-06-23 19:24:43 +0900479func (ctx *moduleContextImpl) isVndk() bool {
480 return ctx.mod.isVndk()
481}
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700482func (ctx *moduleContextImpl) useVndk() bool {
483 return ctx.mod.useVndk()
484}
Justin Yun8effde42017-06-23 19:24:43 +0900485
486func (ctx *moduleContextImpl) isVndkSp() bool {
487 if vndk := ctx.mod.vndkdep; vndk != nil {
488 return vndk.isVndkSp()
489 }
490 return false
491}
492
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800493// Create source abi dumps if the module belongs to the list of VndkLibraries.
494func (ctx *moduleContextImpl) createVndkSourceAbiDump() bool {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700495 return ctx.ctx.Device() && ((ctx.useVndk() && ctx.isVndk()) || inList(ctx.baseModuleName(), llndkLibraries))
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800496}
497
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700498func (ctx *moduleContextImpl) selectedStl() string {
499 if stl := ctx.mod.stl; stl != nil {
500 return stl.Properties.SelectedStl
501 }
502 return ""
503}
504
Colin Crossce75d2c2016-10-06 16:12:58 -0700505func (ctx *moduleContextImpl) baseModuleName() string {
506 return ctx.mod.ModuleBase.BaseModuleName()
507}
508
Colin Cross635c3b02016-05-18 15:37:25 -0700509func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800510 return &Module{
511 hod: hod,
512 multilib: multilib,
513 }
514}
515
Colin Cross635c3b02016-05-18 15:37:25 -0700516func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800517 module := newBaseModule(hod, multilib)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700518 module.features = []feature{
519 &tidyFeature{},
520 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700521 module.stl = &stl{}
Colin Cross16b23492016-01-06 14:41:07 -0800522 module.sanitize = &sanitize{}
Dan Willemsen581341d2017-02-09 16:16:31 -0800523 module.coverage = &coverage{}
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800524 module.sabi = &sabi{}
Justin Yun8effde42017-06-23 19:24:43 +0900525 module.vndkdep = &vndkdep{}
Stephen Craneba090d12017-05-09 15:44:35 -0700526 module.lto = &lto{}
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700527 module.pgo = &pgo{}
Colin Crossca860ac2016-01-04 14:34:37 -0800528 return module
529}
530
Colin Crossce75d2c2016-10-06 16:12:58 -0700531func (c *Module) Prebuilt() *android.Prebuilt {
532 if p, ok := c.linker.(prebuiltLinkerInterface); ok {
533 return p.prebuilt()
534 }
535 return nil
536}
537
538func (c *Module) Name() string {
539 name := c.ModuleBase.Name()
Dan Willemsen01a90592017-04-07 15:21:13 -0700540 if p, ok := c.linker.(interface {
541 Name(string) string
542 }); ok {
Colin Crossce75d2c2016-10-06 16:12:58 -0700543 name = p.Name(name)
544 }
545 return name
546}
547
Jeff Gaston294356f2017-09-27 17:05:30 -0700548// orderDeps reorders dependencies into a list such that if module A depends on B, then
549// A will precede B in the resultant list.
550// This is convenient for passing into a linker.
551func orderDeps(directDeps []android.Path, transitiveDeps map[android.Path][]android.Path) (orderedAllDeps []android.Path, orderedDeclaredDeps []android.Path) {
552 // If A depends on B, then
553 // Every list containing A will also contain B later in the list
554 // So, after concatenating all lists, the final instance of B will have come from the same
555 // original list as the final instance of A
556 // So, the final instance of B will be later in the concatenation than the final A
557 // So, keeping only the final instance of A and of B ensures that A is earlier in the output
558 // list than B
559 for _, dep := range directDeps {
560 orderedAllDeps = append(orderedAllDeps, dep)
561 orderedAllDeps = append(orderedAllDeps, transitiveDeps[dep]...)
562 }
563
Colin Crossb6715442017-10-24 11:13:31 -0700564 orderedAllDeps = android.LastUniquePaths(orderedAllDeps)
Jeff Gaston294356f2017-09-27 17:05:30 -0700565
566 // We don't want to add any new dependencies into directDeps (to allow the caller to
567 // intentionally exclude or replace any unwanted transitive dependencies), so we limit the
568 // resultant list to only what the caller has chosen to include in directDeps
569 _, orderedDeclaredDeps = android.FilterPathList(orderedAllDeps, directDeps)
570
571 return orderedAllDeps, orderedDeclaredDeps
572}
573
574func orderStaticModuleDeps(module *Module, deps []*Module) (results []android.Path) {
575 // make map of transitive dependencies
576 transitiveStaticDepNames := make(map[android.Path][]android.Path, len(deps))
577 for _, dep := range deps {
578 transitiveStaticDepNames[dep.outputFile.Path()] = dep.staticDepsInLinkOrder
579 }
580 // get the output file for each declared dependency
581 depFiles := []android.Path{}
582 for _, dep := range deps {
583 depFiles = append(depFiles, dep.outputFile.Path())
584 }
585
586 // reorder the dependencies based on transitive dependencies
587 module.staticDepsInLinkOrder, results = orderDeps(depFiles, transitiveStaticDepNames)
588
589 return results
590
591}
592
Colin Cross635c3b02016-05-18 15:37:25 -0700593func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700594
Colin Crossca860ac2016-01-04 14:34:37 -0800595 ctx := &moduleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700596 ModuleContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800597 moduleContextImpl: moduleContextImpl{
598 mod: c,
599 },
600 }
601 ctx.ctx = ctx
602
603 flags := Flags{
604 Toolchain: c.toolchain(ctx),
605 Clang: c.clang(ctx),
606 }
Colin Crossca860ac2016-01-04 14:34:37 -0800607 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700608 flags = c.compiler.compilerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800609 }
610 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700611 flags = c.linker.linkerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800612 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700613 if c.stl != nil {
614 flags = c.stl.flags(ctx, flags)
615 }
Colin Cross16b23492016-01-06 14:41:07 -0800616 if c.sanitize != nil {
617 flags = c.sanitize.flags(ctx, flags)
618 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800619 if c.coverage != nil {
620 flags = c.coverage.flags(ctx, flags)
621 }
Stephen Craneba090d12017-05-09 15:44:35 -0700622 if c.lto != nil {
623 flags = c.lto.flags(ctx, flags)
624 }
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700625 if c.pgo != nil {
626 flags = c.pgo.flags(ctx, flags)
627 }
Colin Crossca860ac2016-01-04 14:34:37 -0800628 for _, feature := range c.features {
629 flags = feature.flags(ctx, flags)
630 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800631 if ctx.Failed() {
632 return
633 }
634
Colin Crossb98c8b02016-07-29 13:44:28 -0700635 flags.CFlags, _ = filterList(flags.CFlags, config.IllegalFlags)
636 flags.CppFlags, _ = filterList(flags.CppFlags, config.IllegalFlags)
637 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, config.IllegalFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800638
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800639 deps := c.depsToPaths(ctx)
640 if ctx.Failed() {
641 return
642 }
643 flags.GlobalFlags = append(flags.GlobalFlags, deps.Flags...)
644 c.flags = flags
Jayant Chowdhary9677e8c2017-06-15 14:45:18 -0700645 // We need access to all the flags seen by a source file.
646 if c.sabi != nil {
647 flags = c.sabi.flags(ctx, flags)
648 }
Colin Crossca860ac2016-01-04 14:34:37 -0800649 // Optimization to reduce size of build.ninja
650 // Replace the long list of flags for each file with a module-local variable
651 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
652 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
653 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
654 flags.CFlags = []string{"$cflags"}
655 flags.CppFlags = []string{"$cppflags"}
656 flags.AsFlags = []string{"$asflags"}
657
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700658 var objs Objects
Colin Crossca860ac2016-01-04 14:34:37 -0800659 if c.compiler != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700660 objs = c.compiler.compile(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -0800661 if ctx.Failed() {
662 return
663 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800664 }
665
Colin Crossca860ac2016-01-04 14:34:37 -0800666 if c.linker != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700667 outputFile := c.linker.link(ctx, flags, deps, objs)
Colin Crossca860ac2016-01-04 14:34:37 -0800668 if ctx.Failed() {
669 return
670 }
Colin Cross635c3b02016-05-18 15:37:25 -0700671 c.outputFile = android.OptionalPathForPath(outputFile)
Colin Crossce75d2c2016-10-06 16:12:58 -0700672 }
Colin Cross5049f022015-03-18 13:28:46 -0700673
Colin Crossce75d2c2016-10-06 16:12:58 -0700674 if c.installer != nil && !c.Properties.PreventInstall && c.outputFile.Valid() {
675 c.installer.install(ctx, c.outputFile.Path())
676 if ctx.Failed() {
677 return
Colin Crossca860ac2016-01-04 14:34:37 -0800678 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700679 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800680}
681
Colin Crossb98c8b02016-07-29 13:44:28 -0700682func (c *Module) toolchain(ctx BaseModuleContext) config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800683 if c.cachedToolchain == nil {
Colin Crossb98c8b02016-07-29 13:44:28 -0700684 c.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
Colin Cross3f40fa42015-01-30 17:27:36 -0800685 }
Colin Crossca860ac2016-01-04 14:34:37 -0800686 return c.cachedToolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800687}
688
Colin Crossca860ac2016-01-04 14:34:37 -0800689func (c *Module) begin(ctx BaseModuleContext) {
690 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700691 c.compiler.compilerInit(ctx)
Colin Cross21b9a242015-03-24 14:15:58 -0700692 }
Colin Crossca860ac2016-01-04 14:34:37 -0800693 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700694 c.linker.linkerInit(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800695 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700696 if c.stl != nil {
697 c.stl.begin(ctx)
698 }
Colin Cross16b23492016-01-06 14:41:07 -0800699 if c.sanitize != nil {
700 c.sanitize.begin(ctx)
701 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800702 if c.coverage != nil {
703 c.coverage.begin(ctx)
704 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800705 if c.sabi != nil {
706 c.sabi.begin(ctx)
707 }
Justin Yun8effde42017-06-23 19:24:43 +0900708 if c.vndkdep != nil {
709 c.vndkdep.begin(ctx)
710 }
Stephen Craneba090d12017-05-09 15:44:35 -0700711 if c.lto != nil {
712 c.lto.begin(ctx)
713 }
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700714 if c.pgo != nil {
715 c.pgo.begin(ctx)
716 }
Colin Crossca860ac2016-01-04 14:34:37 -0800717 for _, feature := range c.features {
718 feature.begin(ctx)
719 }
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700720 if ctx.useSdk() {
Dan Albertf5415d72017-08-17 16:19:59 -0700721 version, err := normalizeNdkApiLevel(ctx, ctx.sdkVersion(), ctx.Arch())
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700722 if err != nil {
723 ctx.PropertyErrorf("sdk_version", err.Error())
724 }
Dan Albert90f7a4d2016-11-08 14:34:24 -0800725 c.Properties.Sdk_version = version
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700726 }
Colin Crossca860ac2016-01-04 14:34:37 -0800727}
728
Colin Cross37047f12016-12-13 17:06:13 -0800729func (c *Module) deps(ctx DepsContext) Deps {
Colin Crossc99deeb2016-04-11 15:06:20 -0700730 deps := Deps{}
731
732 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700733 deps = c.compiler.compilerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700734 }
Pirama Arumuga Nainar49b53d52017-10-04 16:47:29 -0700735 // Add the PGO dependency (the clang_rt.profile runtime library), which
736 // sometimes depends on symbols from libgcc, before libgcc gets added
737 // in linkerDeps().
738 if c.pgo != nil {
739 deps = c.pgo.deps(ctx, deps)
740 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700741 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700742 deps = c.linker.linkerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700743 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700744 if c.stl != nil {
745 deps = c.stl.deps(ctx, deps)
746 }
Colin Cross16b23492016-01-06 14:41:07 -0800747 if c.sanitize != nil {
748 deps = c.sanitize.deps(ctx, deps)
749 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800750 if c.coverage != nil {
751 deps = c.coverage.deps(ctx, deps)
752 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800753 if c.sabi != nil {
754 deps = c.sabi.deps(ctx, deps)
755 }
Justin Yun8effde42017-06-23 19:24:43 +0900756 if c.vndkdep != nil {
757 deps = c.vndkdep.deps(ctx, deps)
758 }
Stephen Craneba090d12017-05-09 15:44:35 -0700759 if c.lto != nil {
760 deps = c.lto.deps(ctx, deps)
761 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700762 for _, feature := range c.features {
763 deps = feature.deps(ctx, deps)
764 }
765
Colin Crossb6715442017-10-24 11:13:31 -0700766 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
767 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
768 deps.LateStaticLibs = android.LastUniqueStrings(deps.LateStaticLibs)
769 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
770 deps.LateSharedLibs = android.LastUniqueStrings(deps.LateSharedLibs)
771 deps.HeaderLibs = android.LastUniqueStrings(deps.HeaderLibs)
Colin Crossc99deeb2016-04-11 15:06:20 -0700772
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700773 for _, lib := range deps.ReexportSharedLibHeaders {
774 if !inList(lib, deps.SharedLibs) {
775 ctx.PropertyErrorf("export_shared_lib_headers", "Shared library not in shared_libs: '%s'", lib)
776 }
777 }
778
779 for _, lib := range deps.ReexportStaticLibHeaders {
780 if !inList(lib, deps.StaticLibs) {
781 ctx.PropertyErrorf("export_static_lib_headers", "Static library not in static_libs: '%s'", lib)
782 }
783 }
784
Colin Cross5950f382016-12-13 12:50:57 -0800785 for _, lib := range deps.ReexportHeaderLibHeaders {
786 if !inList(lib, deps.HeaderLibs) {
787 ctx.PropertyErrorf("export_header_lib_headers", "Header library not in header_libs: '%s'", lib)
788 }
789 }
790
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700791 for _, gen := range deps.ReexportGeneratedHeaders {
792 if !inList(gen, deps.GeneratedHeaders) {
793 ctx.PropertyErrorf("export_generated_headers", "Generated header module not in generated_headers: '%s'", gen)
794 }
795 }
796
Colin Crossc99deeb2016-04-11 15:06:20 -0700797 return deps
798}
799
Dan Albert7e9d2952016-08-04 13:02:36 -0700800func (c *Module) beginMutator(actx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800801 ctx := &baseModuleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700802 BaseContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800803 moduleContextImpl: moduleContextImpl{
804 mod: c,
805 },
806 }
807 ctx.ctx = ctx
808
Colin Crossca860ac2016-01-04 14:34:37 -0800809 c.begin(ctx)
Dan Albert7e9d2952016-08-04 13:02:36 -0700810}
811
Colin Cross1e676be2016-10-12 14:38:15 -0700812func (c *Module) DepsMutator(actx android.BottomUpMutatorContext) {
813 if !c.Enabled() {
814 return
815 }
816
Colin Cross37047f12016-12-13 17:06:13 -0800817 ctx := &depsContext{
818 BottomUpMutatorContext: actx,
Dan Albert7e9d2952016-08-04 13:02:36 -0700819 moduleContextImpl: moduleContextImpl{
820 mod: c,
821 },
822 }
823 ctx.ctx = ctx
Colin Crossca860ac2016-01-04 14:34:37 -0800824
Colin Crossc99deeb2016-04-11 15:06:20 -0700825 deps := c.deps(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800826
Dan Albert914449f2016-06-17 16:45:24 -0700827 variantNdkLibs := []string{}
828 variantLateNdkLibs := []string{}
Dan Willemsenb916b802017-03-19 13:44:32 -0700829 if ctx.Os() == android.Android {
Dan Albert914449f2016-06-17 16:45:24 -0700830 version := ctx.sdkVersion()
Dan Willemsen72d39932016-07-08 23:23:48 -0700831
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700832 // rewriteNdkLibs takes a list of names of shared libraries and scans it for three types
833 // of names:
Dan Albert914449f2016-06-17 16:45:24 -0700834 //
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700835 // 1. Name of an NDK library that refers to a prebuilt module.
836 // For each of these, it adds the name of the prebuilt module (which will be in
837 // prebuilts/ndk) to the list of nonvariant libs.
838 // 2. Name of an NDK library that refers to an ndk_library module.
839 // For each of these, it adds the name of the ndk_library module to the list of
840 // variant libs.
841 // 3. Anything else (so anything that isn't an NDK library).
842 // It adds these to the nonvariantLibs list.
Dan Albert914449f2016-06-17 16:45:24 -0700843 //
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700844 // The caller can then know to add the variantLibs dependencies differently from the
845 // nonvariantLibs
846 rewriteNdkLibs := func(list []string) (nonvariantLibs []string, variantLibs []string) {
847 variantLibs = []string{}
848 nonvariantLibs = []string{}
Dan Albert914449f2016-06-17 16:45:24 -0700849 for _, entry := range list {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700850 if ctx.useSdk() && inList(entry, ndkPrebuiltSharedLibraries) {
Dan Albert914449f2016-06-17 16:45:24 -0700851 if !inList(entry, ndkMigratedLibs) {
852 nonvariantLibs = append(nonvariantLibs, entry+".ndk."+version)
853 } else {
854 variantLibs = append(variantLibs, entry+ndkLibrarySuffix)
855 }
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700856 } else if ctx.useVndk() && inList(entry, llndkLibraries) {
Dan Willemsenb916b802017-03-19 13:44:32 -0700857 nonvariantLibs = append(nonvariantLibs, entry+llndkLibrarySuffix)
Dan Albert914449f2016-06-17 16:45:24 -0700858 } else {
Dan Willemsen7cbf5f82017-03-28 00:08:30 -0700859 nonvariantLibs = append(nonvariantLibs, entry)
Dan Willemsen72d39932016-07-08 23:23:48 -0700860 }
861 }
Dan Albert914449f2016-06-17 16:45:24 -0700862 return nonvariantLibs, variantLibs
Dan Willemsen72d39932016-07-08 23:23:48 -0700863 }
864
Dan Albert914449f2016-06-17 16:45:24 -0700865 deps.SharedLibs, variantNdkLibs = rewriteNdkLibs(deps.SharedLibs)
866 deps.LateSharedLibs, variantLateNdkLibs = rewriteNdkLibs(deps.LateSharedLibs)
Jiyong Park4c35af02017-07-05 13:41:55 +0900867 deps.ReexportSharedLibHeaders, _ = rewriteNdkLibs(deps.ReexportSharedLibHeaders)
Dan Willemsen72d39932016-07-08 23:23:48 -0700868 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700869
Colin Cross32ec36c2016-12-15 07:39:51 -0800870 for _, lib := range deps.HeaderLibs {
871 depTag := headerDepTag
872 if inList(lib, deps.ReexportHeaderLibHeaders) {
873 depTag = headerExportDepTag
874 }
875 actx.AddVariationDependencies(nil, depTag, lib)
876 }
Colin Cross5950f382016-12-13 12:50:57 -0800877
Colin Crossc99deeb2016-04-11 15:06:20 -0700878 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, wholeStaticDepTag,
879 deps.WholeStaticLibs...)
880
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700881 for _, lib := range deps.StaticLibs {
882 depTag := staticDepTag
883 if inList(lib, deps.ReexportStaticLibHeaders) {
884 depTag = staticExportDepTag
885 }
Colin Cross15a0d462016-07-14 14:49:58 -0700886 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700887 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700888
889 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, lateStaticDepTag,
890 deps.LateStaticLibs...)
891
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700892 for _, lib := range deps.SharedLibs {
893 depTag := sharedDepTag
894 if inList(lib, deps.ReexportSharedLibHeaders) {
895 depTag = sharedExportDepTag
896 }
Colin Cross15a0d462016-07-14 14:49:58 -0700897 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700898 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700899
900 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, lateSharedDepTag,
901 deps.LateSharedLibs...)
902
Colin Cross68861832016-07-08 10:41:41 -0700903 actx.AddDependency(c, genSourceDepTag, deps.GeneratedSources...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700904
905 for _, gen := range deps.GeneratedHeaders {
906 depTag := genHeaderDepTag
907 if inList(gen, deps.ReexportGeneratedHeaders) {
908 depTag = genHeaderExportDepTag
909 }
910 actx.AddDependency(c, depTag, gen)
911 }
Dan Willemsenb40aab62016-04-20 14:21:14 -0700912
Colin Cross68861832016-07-08 10:41:41 -0700913 actx.AddDependency(c, objDepTag, deps.ObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700914
915 if deps.CrtBegin != "" {
Colin Cross68861832016-07-08 10:41:41 -0700916 actx.AddDependency(c, crtBeginDepTag, deps.CrtBegin)
Colin Crossca860ac2016-01-04 14:34:37 -0800917 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700918 if deps.CrtEnd != "" {
Colin Cross68861832016-07-08 10:41:41 -0700919 actx.AddDependency(c, crtEndDepTag, deps.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700920 }
Dan Willemsenc77a0b32017-09-18 23:19:12 -0700921 if deps.LinkerScript != "" {
922 actx.AddDependency(c, linkerScriptDepTag, deps.LinkerScript)
923 }
Dan Albert914449f2016-06-17 16:45:24 -0700924
925 version := ctx.sdkVersion()
926 actx.AddVariationDependencies([]blueprint.Variation{
927 {"ndk_api", version}, {"link", "shared"}}, ndkStubDepTag, variantNdkLibs...)
928 actx.AddVariationDependencies([]blueprint.Variation{
929 {"ndk_api", version}, {"link", "shared"}}, ndkLateStubDepTag, variantLateNdkLibs...)
Colin Cross6362e272015-10-29 15:25:03 -0700930}
Colin Cross21b9a242015-03-24 14:15:58 -0700931
Dan Albert7e9d2952016-08-04 13:02:36 -0700932func beginMutator(ctx android.BottomUpMutatorContext) {
933 if c, ok := ctx.Module().(*Module); ok && c.Enabled() {
934 c.beginMutator(ctx)
935 }
936}
937
Colin Crossca860ac2016-01-04 14:34:37 -0800938func (c *Module) clang(ctx BaseModuleContext) bool {
939 clang := Bool(c.Properties.Clang)
940
941 if c.Properties.Clang == nil {
942 if ctx.Host() {
943 clang = true
944 }
945
946 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
947 clang = true
948 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800949 }
Colin Cross28344522015-04-22 13:07:53 -0700950
Colin Crossca860ac2016-01-04 14:34:37 -0800951 if !c.toolchain(ctx).ClangSupported() {
952 clang = false
953 }
954
955 return clang
956}
957
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700958// Whether a module can link to another module, taking into
959// account NDK linking.
960func checkLinkType(ctx android.ModuleContext, from *Module, to *Module) {
961 if from.Target().Os != android.Android {
962 // Host code is not restricted
963 return
964 }
965 if from.Properties.UseVndk {
966 // Though vendor code is limited by the vendor mutator,
967 // each vendor-available module needs to check
968 // link-type for VNDK.
969 if from.vndkdep != nil {
970 from.vndkdep.vndkCheckLinkType(ctx, to)
971 }
972 return
973 }
974 if from.Properties.Sdk_version == "" {
975 // Platform code can link to anything
976 return
977 }
978 if _, ok := to.linker.(*toolchainLibraryDecorator); ok {
979 // These are always allowed
980 return
981 }
982 if _, ok := to.linker.(*ndkPrebuiltLibraryLinker); ok {
983 // These are allowed, but they don't set sdk_version
984 return
985 }
986 if _, ok := to.linker.(*ndkPrebuiltStlLinker); ok {
987 // These are allowed, but they don't set sdk_version
988 return
989 }
990 if _, ok := to.linker.(*stubDecorator); ok {
991 // These aren't real libraries, but are the stub shared libraries that are included in
992 // the NDK.
993 return
994 }
995 if to.Properties.Sdk_version == "" {
996 // NDK code linking to platform code is never okay.
997 ctx.ModuleErrorf("depends on non-NDK-built library %q",
998 ctx.OtherModuleName(to))
999 }
1000
1001 // At this point we know we have two NDK libraries, but we need to
1002 // check that we're not linking against anything built against a higher
1003 // API level, as it is only valid to link against older or equivalent
1004 // APIs.
1005
1006 if from.Properties.Sdk_version == "current" {
1007 // Current can link against anything.
1008 return
1009 } else if to.Properties.Sdk_version == "current" {
1010 // Current can't be linked against by anything else.
1011 ctx.ModuleErrorf("links %q built against newer API version %q",
1012 ctx.OtherModuleName(to), "current")
1013 }
1014
1015 fromApi, err := strconv.Atoi(from.Properties.Sdk_version)
1016 if err != nil {
1017 ctx.PropertyErrorf("sdk_version",
1018 "Invalid sdk_version value (must be int): %q",
1019 from.Properties.Sdk_version)
1020 }
1021 toApi, err := strconv.Atoi(to.Properties.Sdk_version)
1022 if err != nil {
1023 ctx.PropertyErrorf("sdk_version",
1024 "Invalid sdk_version value (must be int): %q",
1025 to.Properties.Sdk_version)
1026 }
1027
1028 if toApi > fromApi {
1029 ctx.ModuleErrorf("links %q built against newer API version %q",
1030 ctx.OtherModuleName(to), to.Properties.Sdk_version)
1031 }
1032}
1033
Colin Crossc99deeb2016-04-11 15:06:20 -07001034// Convert dependencies to paths. Returns a PathDeps containing paths
Colin Cross635c3b02016-05-18 15:37:25 -07001035func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
Colin Crossca860ac2016-01-04 14:34:37 -08001036 var depPaths PathDeps
Colin Crossca860ac2016-01-04 14:34:37 -08001037
Jeff Gaston294356f2017-09-27 17:05:30 -07001038 directStaticDeps := []*Module{}
1039
Colin Crossd11fcda2017-10-23 17:59:01 -07001040 ctx.VisitDirectDeps(func(dep android.Module) {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001041 depName := ctx.OtherModuleName(dep)
1042 depTag := ctx.OtherModuleDependencyTag(dep)
Dan Albert9e10cd42016-08-03 14:12:14 -07001043
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001044 ccDep, _ := dep.(*Module)
1045 if ccDep == nil {
1046 // handling for a few module types that aren't cc Module but that are also supported
1047 switch depTag {
Colin Cross068e0fe2016-12-13 15:23:47 -08001048 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsend6ba0d52017-09-13 15:46:47 -07001049 // Nothing to do
Dan Willemsenb40aab62016-04-20 14:21:14 -07001050 case genSourceDepTag:
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001051 if genRule, ok := dep.(genrule.SourceFileGenerator); ok {
Dan Willemsenb40aab62016-04-20 14:21:14 -07001052 depPaths.GeneratedSources = append(depPaths.GeneratedSources,
1053 genRule.GeneratedSourceFiles()...)
1054 } else {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001055 ctx.ModuleErrorf("module %q is not a gensrcs or genrule", depName)
Dan Willemsenb40aab62016-04-20 14:21:14 -07001056 }
Colin Crosse90bfd12017-04-26 16:59:26 -07001057 // Support exported headers from a generated_sources dependency
1058 fallthrough
Dan Willemsenb3454ab2016-09-28 17:34:58 -07001059 case genHeaderDepTag, genHeaderExportDepTag:
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001060 if genRule, ok := dep.(genrule.SourceFileGenerator); ok {
Dan Willemsenb40aab62016-04-20 14:21:14 -07001061 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders,
1062 genRule.GeneratedSourceFiles()...)
Colin Cross5ed99c62016-11-22 12:55:55 -08001063 flags := includeDirsToFlags(genRule.GeneratedHeaderDirs())
Dan Willemsenb3454ab2016-09-28 17:34:58 -07001064 depPaths.Flags = append(depPaths.Flags, flags)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001065 if depTag == genHeaderExportDepTag {
Dan Willemsenb3454ab2016-09-28 17:34:58 -07001066 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags)
Dan Willemsen847dcc72016-09-29 12:13:36 -07001067 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps,
1068 genRule.GeneratedSourceFiles()...)
Jayant Chowdhary715cac32017-04-20 06:53:59 -07001069 // Add these re-exported flags to help header-abi-dumper to infer the abi exported by a library.
1070 c.sabi.Properties.ReexportedIncludeFlags = append(c.sabi.Properties.ReexportedIncludeFlags, flags)
1071
Dan Willemsenb3454ab2016-09-28 17:34:58 -07001072 }
Dan Willemsenb40aab62016-04-20 14:21:14 -07001073 } else {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001074 ctx.ModuleErrorf("module %q is not a genrule", depName)
Dan Willemsenb40aab62016-04-20 14:21:14 -07001075 }
Dan Willemsenc77a0b32017-09-18 23:19:12 -07001076 case linkerScriptDepTag:
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001077 if genRule, ok := dep.(genrule.SourceFileGenerator); ok {
Dan Willemsenc77a0b32017-09-18 23:19:12 -07001078 files := genRule.GeneratedSourceFiles()
1079 if len(files) == 1 {
1080 depPaths.LinkerScript = android.OptionalPathForPath(files[0])
1081 } else if len(files) > 1 {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001082 ctx.ModuleErrorf("module %q can only generate a single file if used for a linker script", depName)
Dan Willemsenc77a0b32017-09-18 23:19:12 -07001083 }
1084 } else {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001085 ctx.ModuleErrorf("module %q is not a genrule", depName)
Dan Willemsenc77a0b32017-09-18 23:19:12 -07001086 }
Dan Willemsenb40aab62016-04-20 14:21:14 -07001087 default:
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001088 ctx.ModuleErrorf("depends on non-cc module %q", depName)
Colin Crossca860ac2016-01-04 14:34:37 -08001089 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001090 return
1091 }
1092
Colin Crossd11fcda2017-10-23 17:59:01 -07001093 if dep.Target().Os != ctx.Os() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001094 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1095 return
1096 }
Colin Crossd11fcda2017-10-23 17:59:01 -07001097 if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001098 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
Colin Crossa1ad8d12016-06-01 17:09:44 -07001099 return
1100 }
1101
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001102 // re-exporting flags
1103 if depTag == reuseObjTag {
1104 if l, ok := ccDep.compiler.(libraryInterface); ok {
Colin Crossbbc9f4d2017-05-03 16:24:55 -07001105 objs, flags, deps := l.reuseObjs()
Colin Cross10d22312017-05-03 11:01:58 -07001106 depPaths.Objs = depPaths.Objs.Append(objs)
1107 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Colin Crossbbc9f4d2017-05-03 16:24:55 -07001108 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps, deps...)
Colin Crossbba99042016-11-23 15:45:05 -08001109 return
1110 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001111 }
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001112 if t, ok := depTag.(dependencyTag); ok && t.library {
1113 if i, ok := ccDep.linker.(exportedFlagsProducer); ok {
Dan Willemsen76f08272016-07-09 00:14:08 -07001114 flags := i.exportedFlags()
Dan Willemsen847dcc72016-09-29 12:13:36 -07001115 deps := i.exportedFlagsDeps()
Dan Willemsen76f08272016-07-09 00:14:08 -07001116 depPaths.Flags = append(depPaths.Flags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -07001117 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders, deps...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001118
1119 if t.reexportFlags {
Dan Willemsen76f08272016-07-09 00:14:08 -07001120 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -07001121 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps, deps...)
Jayant Chowdhary715cac32017-04-20 06:53:59 -07001122 // 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 -07001123 // Re-exported shared library headers must be included as well since they can help us with type information
1124 // about template instantiations (instantiated from their headers).
1125 c.sabi.Properties.ReexportedIncludeFlags = append(c.sabi.Properties.ReexportedIncludeFlags, flags...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001126 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001127 }
Dan Willemsena96ff642016-06-07 12:34:45 -07001128
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001129 checkLinkType(ctx, c, ccDep)
Colin Crossc99deeb2016-04-11 15:06:20 -07001130 }
1131
Colin Cross26c34ed2016-09-30 17:10:16 -07001132 var ptr *android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -07001133 var depPtr *android.Paths
Colin Crossc99deeb2016-04-11 15:06:20 -07001134
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001135 linkFile := ccDep.outputFile
Colin Cross26c34ed2016-09-30 17:10:16 -07001136 depFile := android.OptionalPath{}
1137
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001138 switch depTag {
Dan Albert914449f2016-06-17 16:45:24 -07001139 case ndkStubDepTag, sharedDepTag, sharedExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001140 ptr = &depPaths.SharedLibs
1141 depPtr = &depPaths.SharedLibsDeps
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001142 depFile = ccDep.linker.(libraryInterface).toc()
Dan Albert914449f2016-06-17 16:45:24 -07001143 case lateSharedDepTag, ndkLateStubDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001144 ptr = &depPaths.LateSharedLibs
1145 depPtr = &depPaths.LateSharedLibsDeps
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001146 depFile = ccDep.linker.(libraryInterface).toc()
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001147 case staticDepTag, staticExportDepTag:
Jeff Gaston294356f2017-09-27 17:05:30 -07001148 ptr = nil
1149 directStaticDeps = append(directStaticDeps, ccDep)
Colin Crossc99deeb2016-04-11 15:06:20 -07001150 case lateStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001151 ptr = &depPaths.LateStaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -07001152 case wholeStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001153 ptr = &depPaths.WholeStaticLibs
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001154 staticLib, ok := ccDep.linker.(libraryInterface)
Colin Crossb916a382016-07-29 17:28:03 -07001155 if !ok || !staticLib.static() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001156 ctx.ModuleErrorf("module %q not a static library", depName)
Colin Crossc99deeb2016-04-11 15:06:20 -07001157 return
1158 }
1159
1160 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001161 postfix := " (required by " + ctx.OtherModuleName(dep) + ")"
Colin Crossc99deeb2016-04-11 15:06:20 -07001162 for i := range missingDeps {
1163 missingDeps[i] += postfix
1164 }
1165 ctx.AddMissingDependencies(missingDeps)
1166 }
Dan Willemsen5cb580f2016-09-26 17:33:01 -07001167 depPaths.WholeStaticLibObjs = depPaths.WholeStaticLibObjs.Append(staticLib.objs())
Colin Cross5950f382016-12-13 12:50:57 -08001168 case headerDepTag:
1169 // Nothing
Colin Crossc99deeb2016-04-11 15:06:20 -07001170 case objDepTag:
Dan Willemsen5cb580f2016-09-26 17:33:01 -07001171 depPaths.Objs.objFiles = append(depPaths.Objs.objFiles, linkFile.Path())
Colin Crossc99deeb2016-04-11 15:06:20 -07001172 case crtBeginDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001173 depPaths.CrtBegin = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -07001174 case crtEndDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -07001175 depPaths.CrtEnd = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -07001176 }
1177
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001178 switch depTag {
Dan Willemsen581341d2017-02-09 16:16:31 -08001179 case staticDepTag, staticExportDepTag, lateStaticDepTag:
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001180 staticLib, ok := ccDep.linker.(libraryInterface)
Dan Willemsen581341d2017-02-09 16:16:31 -08001181 if !ok || !staticLib.static() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001182 ctx.ModuleErrorf("module %q not a static library", depName)
Dan Willemsen581341d2017-02-09 16:16:31 -08001183 return
1184 }
1185
1186 // When combining coverage files for shared libraries and executables, coverage files
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001187 // in static libraries act as if they were whole static libraries. The same goes for
1188 // source based Abi dump files.
Dan Willemsen581341d2017-02-09 16:16:31 -08001189 depPaths.StaticLibObjs.coverageFiles = append(depPaths.StaticLibObjs.coverageFiles,
1190 staticLib.objs().coverageFiles...)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001191 depPaths.StaticLibObjs.sAbiDumpFiles = append(depPaths.StaticLibObjs.sAbiDumpFiles,
1192 staticLib.objs().sAbiDumpFiles...)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001193
Dan Willemsen581341d2017-02-09 16:16:31 -08001194 }
1195
Colin Cross26c34ed2016-09-30 17:10:16 -07001196 if ptr != nil {
Colin Crossce75d2c2016-10-06 16:12:58 -07001197 if !linkFile.Valid() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001198 ctx.ModuleErrorf("module %q missing output file", depName)
Colin Crossce75d2c2016-10-06 16:12:58 -07001199 return
1200 }
Colin Cross26c34ed2016-09-30 17:10:16 -07001201 *ptr = append(*ptr, linkFile.Path())
1202 }
1203
Colin Crossc99deeb2016-04-11 15:06:20 -07001204 if depPtr != nil {
Colin Cross26c34ed2016-09-30 17:10:16 -07001205 dep := depFile
1206 if !dep.Valid() {
1207 dep = linkFile
1208 }
1209 *depPtr = append(*depPtr, dep.Path())
Colin Crossca860ac2016-01-04 14:34:37 -08001210 }
Jiyong Park27b188b2017-07-18 13:23:39 +09001211
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001212 // Export the shared libs to Make.
1213 switch depTag {
Jiyong Park27b188b2017-07-18 13:23:39 +09001214 case sharedDepTag, sharedExportDepTag, lateSharedDepTag:
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001215 libName := strings.TrimSuffix(depName, llndkLibrarySuffix)
Jiyong Park27b188b2017-07-18 13:23:39 +09001216 libName = strings.TrimPrefix(libName, "prebuilt_")
Jiyong Parkd5b18a52017-08-03 21:22:50 +09001217 isLLndk := inList(libName, llndkLibraries)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001218 var makeLibName string
1219 bothVendorAndCoreVariantsExist := ccDep.hasVendorVariant() || isLLndk
1220 if c.useVndk() && bothVendorAndCoreVariantsExist {
1221 // The vendor module in Make will have been renamed to not conflict with the core
1222 // module, so update the dependency name here accordingly.
1223 makeLibName = libName + vendorSuffix
1224 } else {
1225 makeLibName = libName
Jiyong Park27b188b2017-07-18 13:23:39 +09001226 }
1227 // Note: the order of libs in this list is not important because
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001228 // they merely serve as Make dependencies and do not affect this lib itself.
1229 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, makeLibName)
Jiyong Park27b188b2017-07-18 13:23:39 +09001230 }
Colin Crossca860ac2016-01-04 14:34:37 -08001231 })
1232
Jeff Gaston294356f2017-09-27 17:05:30 -07001233 // use the ordered dependencies as this module's dependencies
1234 depPaths.StaticLibs = append(depPaths.StaticLibs, orderStaticModuleDeps(c, directStaticDeps)...)
1235
Colin Crossdd84e052017-05-17 13:44:16 -07001236 // Dedup exported flags from dependencies
Colin Crossb6715442017-10-24 11:13:31 -07001237 depPaths.Flags = android.FirstUniqueStrings(depPaths.Flags)
Dan Willemsenfe92c962017-08-29 12:28:37 -07001238 depPaths.GeneratedHeaders = android.FirstUniquePaths(depPaths.GeneratedHeaders)
Colin Crossb6715442017-10-24 11:13:31 -07001239 depPaths.ReexportedFlags = android.FirstUniqueStrings(depPaths.ReexportedFlags)
Dan Willemsenfe92c962017-08-29 12:28:37 -07001240 depPaths.ReexportedFlagsDeps = android.FirstUniquePaths(depPaths.ReexportedFlagsDeps)
1241
1242 if c.sabi != nil {
Colin Crossb6715442017-10-24 11:13:31 -07001243 c.sabi.Properties.ReexportedIncludeFlags = android.FirstUniqueStrings(c.sabi.Properties.ReexportedIncludeFlags)
Dan Willemsenfe92c962017-08-29 12:28:37 -07001244 }
Colin Crossdd84e052017-05-17 13:44:16 -07001245
Colin Crossca860ac2016-01-04 14:34:37 -08001246 return depPaths
1247}
1248
1249func (c *Module) InstallInData() bool {
1250 if c.installer == nil {
1251 return false
1252 }
Vishwath Mohan1dd88392017-03-29 22:00:18 -07001253 return c.installer.inData()
1254}
1255
1256func (c *Module) InstallInSanitizerDir() bool {
1257 if c.installer == nil {
1258 return false
1259 }
1260 if c.sanitize != nil && c.sanitize.inSanitizerDir() {
Colin Cross94610402016-08-29 13:41:32 -07001261 return true
1262 }
Vishwath Mohan1dd88392017-03-29 22:00:18 -07001263 return c.installer.inSanitizerDir()
Colin Crossca860ac2016-01-04 14:34:37 -08001264}
1265
Dan Willemsen4aa75ca2016-09-28 16:18:03 -07001266func (c *Module) HostToolPath() android.OptionalPath {
1267 if c.installer == nil {
1268 return android.OptionalPath{}
1269 }
1270 return c.installer.hostToolPath()
1271}
1272
Nan Zhangd4e641b2017-07-12 12:55:28 -07001273func (c *Module) IntermPathForModuleOut() android.OptionalPath {
1274 return c.outputFile
1275}
1276
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001277func (c *Module) Srcs() android.Paths {
1278 if c.outputFile.Valid() {
1279 return android.Paths{c.outputFile.Path()}
1280 }
1281 return android.Paths{}
1282}
1283
Colin Cross2ba19d92015-05-07 15:44:20 -07001284//
Colin Crosscfad1192015-11-02 16:43:11 -08001285// Defaults
1286//
Colin Crossca860ac2016-01-04 14:34:37 -08001287type Defaults struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001288 android.ModuleBase
Colin Cross1f44a3a2017-07-07 14:33:33 -07001289 android.DefaultsModuleBase
Colin Crosscfad1192015-11-02 16:43:11 -08001290}
1291
Colin Cross635c3b02016-05-18 15:37:25 -07001292func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -08001293}
1294
Colin Cross1e676be2016-10-12 14:38:15 -07001295func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
1296}
1297
Colin Cross36242852017-06-23 15:06:31 -07001298func defaultsFactory() android.Module {
Colin Crosse1d764e2016-08-18 14:18:32 -07001299 return DefaultsFactory()
1300}
1301
Colin Cross36242852017-06-23 15:06:31 -07001302func DefaultsFactory(props ...interface{}) android.Module {
Colin Crossca860ac2016-01-04 14:34:37 -08001303 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -08001304
Colin Cross36242852017-06-23 15:06:31 -07001305 module.AddProperties(props...)
1306 module.AddProperties(
Colin Crossca860ac2016-01-04 14:34:37 -08001307 &BaseProperties{},
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001308 &VendorProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001309 &BaseCompilerProperties{},
1310 &BaseLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -07001311 &LibraryProperties{},
Colin Cross919281a2016-04-05 16:42:05 -07001312 &FlagExporterProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001313 &BinaryLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -07001314 &TestProperties{},
1315 &TestBinaryProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001316 &UnusedProperties{},
1317 &StlProperties{},
Colin Cross16b23492016-01-06 14:41:07 -08001318 &SanitizeProperties{},
Colin Cross665dce92016-04-28 14:50:03 -07001319 &StripProperties{},
Dan Willemsen7424d612016-09-01 13:45:39 -07001320 &InstallerProperties{},
Dan Willemsena03cf6d2016-09-26 15:45:04 -07001321 &TidyProperties{},
Dan Willemsen581341d2017-02-09 16:16:31 -08001322 &CoverageProperties{},
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001323 &SAbiProperties{},
Justin Yun4b2382f2017-07-26 14:22:10 +09001324 &VndkProperties{},
Stephen Craneba090d12017-05-09 15:44:35 -07001325 &LTOProperties{},
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -07001326 &PgoProperties{},
Colin Crosse1d764e2016-08-18 14:18:32 -07001327 )
Colin Crosscfad1192015-11-02 16:43:11 -08001328
Colin Cross1f44a3a2017-07-07 14:33:33 -07001329 android.InitDefaultsModule(module)
Colin Cross36242852017-06-23 15:06:31 -07001330
1331 return module
Colin Crosscfad1192015-11-02 16:43:11 -08001332}
1333
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001334const (
1335 // coreMode is the variant used for framework-private libraries, or
1336 // SDK libraries. (which framework-private libraries can use)
1337 coreMode = "core"
1338
1339 // vendorMode is the variant used for /vendor code that compiles
1340 // against the VNDK.
1341 vendorMode = "vendor"
1342)
1343
Jiyong Park6a43f042017-10-12 23:05:00 +09001344func squashVendorSrcs(m *Module) {
1345 if lib, ok := m.compiler.(*libraryDecorator); ok {
1346 lib.baseCompiler.Properties.Srcs = append(lib.baseCompiler.Properties.Srcs,
1347 lib.baseCompiler.Properties.Target.Vendor.Srcs...)
1348
1349 lib.baseCompiler.Properties.Exclude_srcs = append(lib.baseCompiler.Properties.Exclude_srcs,
1350 lib.baseCompiler.Properties.Target.Vendor.Exclude_srcs...)
1351 }
1352}
1353
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001354func vendorMutator(mctx android.BottomUpMutatorContext) {
1355 if mctx.Os() != android.Android {
1356 return
1357 }
1358
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001359 if genrule, ok := mctx.Module().(*genrule.Module); ok {
1360 if props, ok := genrule.Extra.(*VendorProperties); ok {
1361 if !mctx.DeviceConfig().CompileVndk() {
1362 mctx.CreateVariations(coreMode)
1363 } else if Bool(props.Vendor_available) {
1364 mctx.CreateVariations(coreMode, vendorMode)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001365 } else if mctx.InstallOnVendorPartition() {
Dan Willemsen3e5bdf22017-09-13 18:37:08 -07001366 mctx.CreateVariations(vendorMode)
1367 } else {
1368 mctx.CreateVariations(coreMode)
1369 }
1370 }
1371 }
1372
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001373 m, ok := mctx.Module().(*Module)
1374 if !ok {
1375 return
1376 }
1377
1378 // Sanity check
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001379 if m.VendorProperties.Vendor_available != nil && mctx.InstallOnVendorPartition() {
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001380 mctx.PropertyErrorf("vendor_available",
1381 "doesn't make sense at the same time as `vendor: true` or `proprietary: true`")
1382 return
1383 }
Justin Yun8effde42017-06-23 19:24:43 +09001384 if vndk := m.vndkdep; vndk != nil {
Jiyong Park82e2bf32017-08-16 14:05:54 +09001385 if vndk.isVndk() && m.VendorProperties.Vendor_available == nil {
Justin Yun8effde42017-06-23 19:24:43 +09001386 mctx.PropertyErrorf("vndk",
Jiyong Park82e2bf32017-08-16 14:05:54 +09001387 "vendor_available must be set to either true or false when `vndk: {enabled: true}`")
Justin Yun8effde42017-06-23 19:24:43 +09001388 return
1389 }
1390 if !vndk.isVndk() && vndk.isVndkSp() {
1391 mctx.PropertyErrorf("vndk",
1392 "must set `enabled: true` to set `support_system_process: true`")
1393 return
1394 }
1395 }
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001396
1397 if !mctx.DeviceConfig().CompileVndk() {
1398 // If the device isn't compiling against the VNDK, we always
1399 // use the core mode.
1400 mctx.CreateVariations(coreMode)
1401 } else if _, ok := m.linker.(*llndkStubDecorator); ok {
1402 // LL-NDK stubs only exist in the vendor variant, since the
1403 // real libraries will be used in the core variant.
1404 mctx.CreateVariations(vendorMode)
Jiyong Park2a454122017-10-19 15:59:33 +09001405 } else if _, ok := m.linker.(*llndkHeadersDecorator); ok {
1406 // ... and LL-NDK headers as well
1407 mctx.CreateVariations(vendorMode)
Jiyong Park82e2bf32017-08-16 14:05:54 +09001408 } else if m.hasVendorVariant() {
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001409 // This will be available in both /system and /vendor
Justin Yun8effde42017-06-23 19:24:43 +09001410 // or a /system directory that is available to vendor.
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001411 mod := mctx.CreateVariations(coreMode, vendorMode)
Jiyong Park6a43f042017-10-12 23:05:00 +09001412 vendor := mod[1].(*Module)
1413 vendor.Properties.UseVndk = true
1414 squashVendorSrcs(vendor)
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -07001415 } else if mctx.InstallOnVendorPartition() && m.Properties.Sdk_version == "" {
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001416 // This will be available in /vendor only
1417 mod := mctx.CreateVariations(vendorMode)
Jiyong Park6a43f042017-10-12 23:05:00 +09001418 vendor := mod[0].(*Module)
1419 vendor.Properties.UseVndk = true
1420 squashVendorSrcs(vendor)
Dan Willemsen4416e5d2017-04-06 12:43:22 -07001421 } else {
1422 // This is either in /system (or similar: /data), or is a
1423 // modules built with the NDK. Modules built with the NDK
1424 // will be restricted using the existing link type checks.
1425 mctx.CreateVariations(coreMode)
1426 }
1427}
1428
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -07001429func getCurrentNdkPrebuiltVersion(ctx DepsContext) string {
1430 if ctx.AConfig().PlatformSdkVersionInt() > config.NdkMaxPrebuiltVersionInt {
1431 return strconv.Itoa(config.NdkMaxPrebuiltVersionInt)
1432 }
1433 return ctx.AConfig().PlatformSdkVersion()
1434}
1435
Colin Cross06a931b2015-10-28 17:23:31 -07001436var Bool = proptools.Bool