blob: d486db3f54019d37f0bbef8b6db3da8fe4a737f1 [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()
38 ctx.BottomUp("ndk_api", ndkApiMutator).Parallel()
39 ctx.BottomUp("test_per_src", testPerSrcMutator).Parallel()
40 ctx.BottomUp("begin", beginMutator).Parallel()
41 })
Colin Cross16b23492016-01-06 14:41:07 -080042
Colin Cross1e676be2016-10-12 14:38:15 -070043 android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
44 ctx.TopDown("asan_deps", sanitizerDepsMutator(asan))
45 ctx.BottomUp("asan", sanitizerMutator(asan)).Parallel()
Colin Cross16b23492016-01-06 14:41:07 -080046
Colin Cross1e676be2016-10-12 14:38:15 -070047 ctx.TopDown("tsan_deps", sanitizerDepsMutator(tsan))
48 ctx.BottomUp("tsan", sanitizerMutator(tsan)).Parallel()
Dan Willemsen581341d2017-02-09 16:16:31 -080049
50 ctx.BottomUp("coverage", coverageLinkingMutator).Parallel()
Colin Cross1e676be2016-10-12 14:38:15 -070051 })
Colin Crossb98c8b02016-07-29 13:44:28 -070052
53 pctx.Import("android/soong/cc/config")
Colin Cross463a90e2015-06-17 14:20:06 -070054}
55
Colin Crossca860ac2016-01-04 14:34:37 -080056type Deps struct {
57 SharedLibs, LateSharedLibs []string
58 StaticLibs, LateStaticLibs, WholeStaticLibs []string
Colin Cross5950f382016-12-13 12:50:57 -080059 HeaderLibs []string
Colin Crossc472d572015-03-17 15:06:21 -070060
Colin Cross5950f382016-12-13 12:50:57 -080061 ReexportSharedLibHeaders, ReexportStaticLibHeaders, ReexportHeaderLibHeaders []string
Dan Willemsen490a8dc2016-06-06 18:22:19 -070062
Colin Cross81413472016-04-11 14:37:39 -070063 ObjFiles []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -070064
Dan Willemsenb40aab62016-04-20 14:21:14 -070065 GeneratedSources []string
66 GeneratedHeaders []string
67
Dan Willemsenb3454ab2016-09-28 17:34:58 -070068 ReexportGeneratedHeaders []string
69
Colin Cross97ba0732015-03-23 17:50:24 -070070 CrtBegin, CrtEnd string
Colin Crossc472d572015-03-17 15:06:21 -070071}
72
Colin Crossca860ac2016-01-04 14:34:37 -080073type PathDeps struct {
Colin Cross26c34ed2016-09-30 17:10:16 -070074 // Paths to .so files
75 SharedLibs, LateSharedLibs android.Paths
76 // Paths to the dependencies to use for .so files (.so.toc files)
77 SharedLibsDeps, LateSharedLibsDeps android.Paths
78 // Paths to .a files
Colin Cross635c3b02016-05-18 15:37:25 -070079 StaticLibs, LateStaticLibs, WholeStaticLibs android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -070080
Colin Cross26c34ed2016-09-30 17:10:16 -070081 // Paths to .o files
Dan Willemsen5cb580f2016-09-26 17:33:01 -070082 Objs Objects
Dan Willemsen581341d2017-02-09 16:16:31 -080083 StaticLibObjs Objects
Dan Willemsen5cb580f2016-09-26 17:33:01 -070084 WholeStaticLibObjs Objects
Dan Willemsen34cc69e2015-09-23 15:26:20 -070085
Colin Cross26c34ed2016-09-30 17:10:16 -070086 // Paths to generated source files
Colin Cross635c3b02016-05-18 15:37:25 -070087 GeneratedSources android.Paths
88 GeneratedHeaders android.Paths
Dan Willemsenb40aab62016-04-20 14:21:14 -070089
Dan Willemsen76f08272016-07-09 00:14:08 -070090 Flags, ReexportedFlags []string
Dan Willemsen847dcc72016-09-29 12:13:36 -070091 ReexportedFlagsDeps android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -070092
Colin Cross26c34ed2016-09-30 17:10:16 -070093 // Paths to crt*.o files
Colin Cross635c3b02016-05-18 15:37:25 -070094 CrtBegin, CrtEnd android.OptionalPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -070095}
96
Colin Crossca860ac2016-01-04 14:34:37 -080097type Flags struct {
Colin Cross28344522015-04-22 13:07:53 -070098 GlobalFlags []string // Flags that apply to C, C++, and assembly source files
99 AsFlags []string // Flags that apply to assembly source files
100 CFlags []string // Flags that apply to C and C++ source files
101 ConlyFlags []string // Flags that apply to C source files
102 CppFlags []string // Flags that apply to C++ source files
103 YaccFlags []string // Flags that apply to Yacc source files
Colin Cross0c461f12016-10-20 16:11:43 -0700104 protoFlags []string // Flags that apply to proto source files
Dan Willemsene1240db2016-11-03 14:28:51 -0700105 aidlFlags []string // Flags that apply to aidl source files
Colin Cross28344522015-04-22 13:07:53 -0700106 LdFlags []string // Flags that apply to linker command lines
Colin Cross16b23492016-01-06 14:41:07 -0800107 libFlags []string // Flags to add libraries early to the link order
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700108 TidyFlags []string // Flags that apply to clang-tidy
Colin Cross91e90042016-12-02 17:13:24 -0800109 YasmFlags []string // Flags that apply to yasm assembly source files
Colin Cross28344522015-04-22 13:07:53 -0700110
Colin Crossb98c8b02016-07-29 13:44:28 -0700111 Toolchain config.Toolchain
Colin Cross28344522015-04-22 13:07:53 -0700112 Clang bool
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700113 Tidy bool
Dan Willemsen581341d2017-02-09 16:16:31 -0800114 Coverage bool
Colin Crossca860ac2016-01-04 14:34:37 -0800115
116 RequiredInstructionSet string
Colin Cross16b23492016-01-06 14:41:07 -0800117 DynamicLinker string
118
Colin Cross635c3b02016-05-18 15:37:25 -0700119 CFlagsDeps android.Paths // Files depended on by compiler flags
Colin Cross18c0c5a2016-12-01 14:45:23 -0800120
121 GroupStaticLibs bool
Colin Crossc472d572015-03-17 15:06:21 -0700122}
123
Colin Cross81413472016-04-11 14:37:39 -0700124type ObjectLinkerProperties struct {
125 // names of other cc_object modules to link into this module using partial linking
126 Objs []string `android:"arch_variant"`
127}
128
Colin Crossca860ac2016-01-04 14:34:37 -0800129// Properties used to compile all C or C++ modules
130type BaseProperties struct {
131 // compile module with clang instead of gcc
132 Clang *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700133
134 // Minimum sdk version supported when compiling against the ndk
135 Sdk_version string
136
Dan Willemsend2ede872016-11-18 14:54:24 -0800137 // Whether to compile against the VNDK
138 Use_vndk bool
139
Colin Crossca860ac2016-01-04 14:34:37 -0800140 // don't insert default compiler flags into asflags, cflags,
141 // cppflags, conlyflags, ldflags, or include_dirs
142 No_default_compiler_flags *bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700143
144 AndroidMkSharedLibs []string `blueprint:"mutated"`
Colin Crossbc6fb162016-05-24 15:39:04 -0700145 HideFromMake bool `blueprint:"mutated"`
Colin Crossb0f28952016-09-19 16:46:53 -0700146 PreventInstall bool `blueprint:"mutated"`
Dan Willemsend2ede872016-11-18 14:54:24 -0800147 Vndk_version string `blueprint:"mutated"`
Colin Crossca860ac2016-01-04 14:34:37 -0800148}
149
Colin Crossca860ac2016-01-04 14:34:37 -0800150type UnusedProperties struct {
Dan Willemsen581341d2017-02-09 16:16:31 -0800151 Tags []string
Colin Crosscfad1192015-11-02 16:43:11 -0800152}
153
Colin Crossca860ac2016-01-04 14:34:37 -0800154type ModuleContextIntf interface {
Colin Crossca860ac2016-01-04 14:34:37 -0800155 static() bool
156 staticBinary() bool
157 clang() bool
Colin Crossb98c8b02016-07-29 13:44:28 -0700158 toolchain() config.Toolchain
Colin Crossca860ac2016-01-04 14:34:37 -0800159 noDefaultCompilerFlags() bool
160 sdk() bool
161 sdkVersion() string
Dan Willemsend2ede872016-11-18 14:54:24 -0800162 vndk() bool
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700163 selectedStl() string
Colin Crossce75d2c2016-10-06 16:12:58 -0700164 baseModuleName() string
Colin Crossca860ac2016-01-04 14:34:37 -0800165}
166
167type ModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700168 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800169 ModuleContextIntf
170}
171
172type BaseModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700173 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800174 ModuleContextIntf
175}
176
Colin Cross37047f12016-12-13 17:06:13 -0800177type DepsContext interface {
178 android.BottomUpMutatorContext
179 ModuleContextIntf
180}
181
Colin Crossca860ac2016-01-04 14:34:37 -0800182type feature interface {
183 begin(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800184 deps(ctx DepsContext, deps Deps) Deps
Colin Crossca860ac2016-01-04 14:34:37 -0800185 flags(ctx ModuleContext, flags Flags) Flags
186 props() []interface{}
187}
188
189type compiler interface {
Colin Cross42742b82016-08-01 13:20:05 -0700190 compilerInit(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800191 compilerDeps(ctx DepsContext, deps Deps) Deps
Colin Cross42742b82016-08-01 13:20:05 -0700192 compilerFlags(ctx ModuleContext, flags Flags) Flags
193 compilerProps() []interface{}
194
Colin Cross76fada02016-07-27 10:31:13 -0700195 appendCflags([]string)
196 appendAsflags([]string)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700197 compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects
Colin Crossca860ac2016-01-04 14:34:37 -0800198}
199
200type linker interface {
Colin Cross42742b82016-08-01 13:20:05 -0700201 linkerInit(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800202 linkerDeps(ctx DepsContext, deps Deps) Deps
Colin Cross42742b82016-08-01 13:20:05 -0700203 linkerFlags(ctx ModuleContext, flags Flags) Flags
204 linkerProps() []interface{}
205
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700206 link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path
Colin Cross76fada02016-07-27 10:31:13 -0700207 appendLdflags([]string)
Colin Crossca860ac2016-01-04 14:34:37 -0800208}
209
210type installer interface {
Colin Cross42742b82016-08-01 13:20:05 -0700211 installerProps() []interface{}
Colin Cross635c3b02016-05-18 15:37:25 -0700212 install(ctx ModuleContext, path android.Path)
Colin Crossca860ac2016-01-04 14:34:37 -0800213 inData() bool
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700214 hostToolPath() android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800215}
216
Colin Crossc99deeb2016-04-11 15:06:20 -0700217type dependencyTag struct {
218 blueprint.BaseDependencyTag
219 name string
220 library bool
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700221
222 reexportFlags bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700223}
224
225var (
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700226 sharedDepTag = dependencyTag{name: "shared", library: true}
227 sharedExportDepTag = dependencyTag{name: "shared", library: true, reexportFlags: true}
228 lateSharedDepTag = dependencyTag{name: "late shared", library: true}
229 staticDepTag = dependencyTag{name: "static", library: true}
230 staticExportDepTag = dependencyTag{name: "static", library: true, reexportFlags: true}
231 lateStaticDepTag = dependencyTag{name: "late static", library: true}
232 wholeStaticDepTag = dependencyTag{name: "whole static", library: true, reexportFlags: true}
Colin Cross32ec36c2016-12-15 07:39:51 -0800233 headerDepTag = dependencyTag{name: "header", library: true}
234 headerExportDepTag = dependencyTag{name: "header", library: true, reexportFlags: true}
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700235 genSourceDepTag = dependencyTag{name: "gen source"}
236 genHeaderDepTag = dependencyTag{name: "gen header"}
237 genHeaderExportDepTag = dependencyTag{name: "gen header", reexportFlags: true}
238 objDepTag = dependencyTag{name: "obj"}
239 crtBeginDepTag = dependencyTag{name: "crtbegin"}
240 crtEndDepTag = dependencyTag{name: "crtend"}
241 reuseObjTag = dependencyTag{name: "reuse objects"}
242 ndkStubDepTag = dependencyTag{name: "ndk stub", library: true}
243 ndkLateStubDepTag = dependencyTag{name: "ndk late stub", library: true}
Colin Crossc99deeb2016-04-11 15:06:20 -0700244)
245
Colin Crossca860ac2016-01-04 14:34:37 -0800246// Module contains the properties and members used by all C/C++ module types, and implements
247// the blueprint.Module interface. It delegates to compiler, linker, and installer interfaces
248// to construct the output file. Behavior can be customized with a Customizer interface
249type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700250 android.ModuleBase
251 android.DefaultableModule
Colin Crossc472d572015-03-17 15:06:21 -0700252
Colin Crossca860ac2016-01-04 14:34:37 -0800253 Properties BaseProperties
254 unused UnusedProperties
Colin Crossfa138792015-04-24 17:31:52 -0700255
Colin Crossca860ac2016-01-04 14:34:37 -0800256 // initialize before calling Init
Colin Cross635c3b02016-05-18 15:37:25 -0700257 hod android.HostOrDeviceSupported
258 multilib android.Multilib
Colin Crossc472d572015-03-17 15:06:21 -0700259
Colin Crossca860ac2016-01-04 14:34:37 -0800260 // delegates, initialize before calling Init
Colin Crossb4ce0ec2016-09-13 13:41:39 -0700261 features []feature
262 compiler compiler
263 linker linker
264 installer installer
265 stl *stl
266 sanitize *sanitize
Dan Willemsen581341d2017-02-09 16:16:31 -0800267 coverage *coverage
Colin Cross16b23492016-01-06 14:41:07 -0800268
269 androidMkSharedLibDeps []string
Colin Cross74d1ec02015-04-28 13:30:13 -0700270
Colin Cross635c3b02016-05-18 15:37:25 -0700271 outputFile android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800272
Colin Crossb98c8b02016-07-29 13:44:28 -0700273 cachedToolchain config.Toolchain
Colin Crossb916a382016-07-29 17:28:03 -0700274
275 subAndroidMkOnce map[subAndroidMkProvider]bool
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800276
277 // Flags used to compile this module
278 flags Flags
Colin Crossc472d572015-03-17 15:06:21 -0700279}
280
Colin Crossca860ac2016-01-04 14:34:37 -0800281func (c *Module) Init() (blueprint.Module, []interface{}) {
282 props := []interface{}{&c.Properties, &c.unused}
Colin Crossca860ac2016-01-04 14:34:37 -0800283 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700284 props = append(props, c.compiler.compilerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800285 }
286 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700287 props = append(props, c.linker.linkerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800288 }
289 if c.installer != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700290 props = append(props, c.installer.installerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800291 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700292 if c.stl != nil {
293 props = append(props, c.stl.props()...)
294 }
Colin Cross16b23492016-01-06 14:41:07 -0800295 if c.sanitize != nil {
296 props = append(props, c.sanitize.props()...)
297 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800298 if c.coverage != nil {
299 props = append(props, c.coverage.props()...)
300 }
Colin Crossca860ac2016-01-04 14:34:37 -0800301 for _, feature := range c.features {
302 props = append(props, feature.props()...)
303 }
Colin Crossc472d572015-03-17 15:06:21 -0700304
Colin Cross635c3b02016-05-18 15:37:25 -0700305 _, props = android.InitAndroidArchModule(c, c.hod, c.multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700306
Colin Cross635c3b02016-05-18 15:37:25 -0700307 return android.InitDefaultableModule(c, c, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700308}
309
Colin Crossb916a382016-07-29 17:28:03 -0700310// Returns true for dependency roots (binaries)
311// TODO(ccross): also handle dlopenable libraries
312func (c *Module) isDependencyRoot() bool {
313 if root, ok := c.linker.(interface {
314 isDependencyRoot() bool
315 }); ok {
316 return root.isDependencyRoot()
317 }
318 return false
319}
320
Colin Crossca860ac2016-01-04 14:34:37 -0800321type baseModuleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700322 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800323 moduleContextImpl
324}
325
Colin Cross37047f12016-12-13 17:06:13 -0800326type depsContext struct {
327 android.BottomUpMutatorContext
328 moduleContextImpl
329}
330
Colin Crossca860ac2016-01-04 14:34:37 -0800331type moduleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700332 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800333 moduleContextImpl
334}
335
336type moduleContextImpl struct {
337 mod *Module
338 ctx BaseModuleContext
339}
340
Colin Crossca860ac2016-01-04 14:34:37 -0800341func (ctx *moduleContextImpl) clang() bool {
342 return ctx.mod.clang(ctx.ctx)
343}
344
Colin Crossb98c8b02016-07-29 13:44:28 -0700345func (ctx *moduleContextImpl) toolchain() config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800346 return ctx.mod.toolchain(ctx.ctx)
347}
348
349func (ctx *moduleContextImpl) static() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700350 if static, ok := ctx.mod.linker.(interface {
351 static() bool
352 }); ok {
353 return static.static()
Colin Crossca860ac2016-01-04 14:34:37 -0800354 }
Colin Crossb916a382016-07-29 17:28:03 -0700355 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800356}
357
358func (ctx *moduleContextImpl) staticBinary() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700359 if static, ok := ctx.mod.linker.(interface {
360 staticBinary() bool
361 }); ok {
362 return static.staticBinary()
Colin Crossca860ac2016-01-04 14:34:37 -0800363 }
Colin Crossb916a382016-07-29 17:28:03 -0700364 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800365}
366
367func (ctx *moduleContextImpl) noDefaultCompilerFlags() bool {
368 return Bool(ctx.mod.Properties.No_default_compiler_flags)
369}
370
371func (ctx *moduleContextImpl) sdk() bool {
Dan Willemsena96ff642016-06-07 12:34:45 -0700372 if ctx.ctx.Device() {
373 return ctx.mod.Properties.Sdk_version != ""
374 }
375 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800376}
377
378func (ctx *moduleContextImpl) sdkVersion() string {
Dan Willemsena96ff642016-06-07 12:34:45 -0700379 if ctx.ctx.Device() {
Dan Willemsend2ede872016-11-18 14:54:24 -0800380 if ctx.mod.Properties.Use_vndk {
381 return ctx.mod.Properties.Vndk_version
382 } else {
383 return ctx.mod.Properties.Sdk_version
384 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700385 }
386 return ""
Colin Crossca860ac2016-01-04 14:34:37 -0800387}
388
Dan Willemsend2ede872016-11-18 14:54:24 -0800389func (ctx *moduleContextImpl) vndk() bool {
390 if ctx.ctx.Device() {
391 return ctx.mod.Properties.Use_vndk
392 }
393 return false
394}
395
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700396func (ctx *moduleContextImpl) selectedStl() string {
397 if stl := ctx.mod.stl; stl != nil {
398 return stl.Properties.SelectedStl
399 }
400 return ""
401}
402
Colin Crossce75d2c2016-10-06 16:12:58 -0700403func (ctx *moduleContextImpl) baseModuleName() string {
404 return ctx.mod.ModuleBase.BaseModuleName()
405}
406
Colin Cross635c3b02016-05-18 15:37:25 -0700407func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800408 return &Module{
409 hod: hod,
410 multilib: multilib,
411 }
412}
413
Colin Cross635c3b02016-05-18 15:37:25 -0700414func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800415 module := newBaseModule(hod, multilib)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700416 module.features = []feature{
417 &tidyFeature{},
418 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700419 module.stl = &stl{}
Colin Cross16b23492016-01-06 14:41:07 -0800420 module.sanitize = &sanitize{}
Dan Willemsen581341d2017-02-09 16:16:31 -0800421 module.coverage = &coverage{}
Colin Crossca860ac2016-01-04 14:34:37 -0800422 return module
423}
424
Colin Crossce75d2c2016-10-06 16:12:58 -0700425func (c *Module) Prebuilt() *android.Prebuilt {
426 if p, ok := c.linker.(prebuiltLinkerInterface); ok {
427 return p.prebuilt()
428 }
429 return nil
430}
431
432func (c *Module) Name() string {
433 name := c.ModuleBase.Name()
434 if p, ok := c.linker.(prebuiltLinkerInterface); ok {
435 name = p.Name(name)
436 }
437 return name
438}
439
Colin Cross635c3b02016-05-18 15:37:25 -0700440func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800441 ctx := &moduleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700442 ModuleContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800443 moduleContextImpl: moduleContextImpl{
444 mod: c,
445 },
446 }
447 ctx.ctx = ctx
448
449 flags := Flags{
450 Toolchain: c.toolchain(ctx),
451 Clang: c.clang(ctx),
452 }
Colin Crossca860ac2016-01-04 14:34:37 -0800453 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700454 flags = c.compiler.compilerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800455 }
456 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700457 flags = c.linker.linkerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800458 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700459 if c.stl != nil {
460 flags = c.stl.flags(ctx, flags)
461 }
Colin Cross16b23492016-01-06 14:41:07 -0800462 if c.sanitize != nil {
463 flags = c.sanitize.flags(ctx, flags)
464 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800465 if c.coverage != nil {
466 flags = c.coverage.flags(ctx, flags)
467 }
Colin Crossca860ac2016-01-04 14:34:37 -0800468 for _, feature := range c.features {
469 flags = feature.flags(ctx, flags)
470 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800471 if ctx.Failed() {
472 return
473 }
474
Colin Crossb98c8b02016-07-29 13:44:28 -0700475 flags.CFlags, _ = filterList(flags.CFlags, config.IllegalFlags)
476 flags.CppFlags, _ = filterList(flags.CppFlags, config.IllegalFlags)
477 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, config.IllegalFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800478
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800479 deps := c.depsToPaths(ctx)
480 if ctx.Failed() {
481 return
482 }
483 flags.GlobalFlags = append(flags.GlobalFlags, deps.Flags...)
484 c.flags = flags
485
Colin Crossca860ac2016-01-04 14:34:37 -0800486 // Optimization to reduce size of build.ninja
487 // Replace the long list of flags for each file with a module-local variable
488 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
489 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
490 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
491 flags.CFlags = []string{"$cflags"}
492 flags.CppFlags = []string{"$cppflags"}
493 flags.AsFlags = []string{"$asflags"}
494
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700495 var objs Objects
Colin Crossca860ac2016-01-04 14:34:37 -0800496 if c.compiler != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700497 objs = c.compiler.compile(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -0800498 if ctx.Failed() {
499 return
500 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800501 }
502
Colin Crossca860ac2016-01-04 14:34:37 -0800503 if c.linker != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700504 outputFile := c.linker.link(ctx, flags, deps, objs)
Colin Crossca860ac2016-01-04 14:34:37 -0800505 if ctx.Failed() {
506 return
507 }
Colin Cross635c3b02016-05-18 15:37:25 -0700508 c.outputFile = android.OptionalPathForPath(outputFile)
Colin Crossce75d2c2016-10-06 16:12:58 -0700509 }
Colin Cross5049f022015-03-18 13:28:46 -0700510
Colin Crossce75d2c2016-10-06 16:12:58 -0700511 if c.installer != nil && !c.Properties.PreventInstall && c.outputFile.Valid() {
512 c.installer.install(ctx, c.outputFile.Path())
513 if ctx.Failed() {
514 return
Colin Crossca860ac2016-01-04 14:34:37 -0800515 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700516 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800517}
518
Colin Crossb98c8b02016-07-29 13:44:28 -0700519func (c *Module) toolchain(ctx BaseModuleContext) config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800520 if c.cachedToolchain == nil {
Colin Crossb98c8b02016-07-29 13:44:28 -0700521 c.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
Colin Cross3f40fa42015-01-30 17:27:36 -0800522 }
Colin Crossca860ac2016-01-04 14:34:37 -0800523 return c.cachedToolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800524}
525
Colin Crossca860ac2016-01-04 14:34:37 -0800526func (c *Module) begin(ctx BaseModuleContext) {
527 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700528 c.compiler.compilerInit(ctx)
Colin Cross21b9a242015-03-24 14:15:58 -0700529 }
Colin Crossca860ac2016-01-04 14:34:37 -0800530 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700531 c.linker.linkerInit(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800532 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700533 if c.stl != nil {
534 c.stl.begin(ctx)
535 }
Colin Cross16b23492016-01-06 14:41:07 -0800536 if c.sanitize != nil {
537 c.sanitize.begin(ctx)
538 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800539 if c.coverage != nil {
540 c.coverage.begin(ctx)
541 }
Colin Crossca860ac2016-01-04 14:34:37 -0800542 for _, feature := range c.features {
543 feature.begin(ctx)
544 }
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700545 if ctx.sdk() {
Dan Willemsend2ede872016-11-18 14:54:24 -0800546 if ctx.vndk() {
547 ctx.PropertyErrorf("use_vndk",
548 "sdk_version and use_vndk cannot be used at the same time")
549 }
550
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700551 version, err := normalizeNdkApiLevel(ctx.sdkVersion(), ctx.Arch())
552 if err != nil {
553 ctx.PropertyErrorf("sdk_version", err.Error())
554 }
Dan Albert90f7a4d2016-11-08 14:34:24 -0800555 c.Properties.Sdk_version = version
Dan Willemsend2ede872016-11-18 14:54:24 -0800556 } else if ctx.vndk() {
557 version, err := normalizeNdkApiLevel(ctx.DeviceConfig().VndkVersion(), ctx.Arch())
558 if err != nil {
559 ctx.ModuleErrorf("Bad BOARD_VNDK_VERSION: %s", err.Error())
560 }
561 c.Properties.Vndk_version = version
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700562 }
Colin Crossca860ac2016-01-04 14:34:37 -0800563}
564
Colin Cross37047f12016-12-13 17:06:13 -0800565func (c *Module) deps(ctx DepsContext) Deps {
Colin Crossc99deeb2016-04-11 15:06:20 -0700566 deps := Deps{}
567
568 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700569 deps = c.compiler.compilerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700570 }
571 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700572 deps = c.linker.linkerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700573 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700574 if c.stl != nil {
575 deps = c.stl.deps(ctx, deps)
576 }
Colin Cross16b23492016-01-06 14:41:07 -0800577 if c.sanitize != nil {
578 deps = c.sanitize.deps(ctx, deps)
579 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800580 if c.coverage != nil {
581 deps = c.coverage.deps(ctx, deps)
582 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700583 for _, feature := range c.features {
584 deps = feature.deps(ctx, deps)
585 }
586
587 deps.WholeStaticLibs = lastUniqueElements(deps.WholeStaticLibs)
588 deps.StaticLibs = lastUniqueElements(deps.StaticLibs)
589 deps.LateStaticLibs = lastUniqueElements(deps.LateStaticLibs)
590 deps.SharedLibs = lastUniqueElements(deps.SharedLibs)
591 deps.LateSharedLibs = lastUniqueElements(deps.LateSharedLibs)
Colin Cross5950f382016-12-13 12:50:57 -0800592 deps.HeaderLibs = lastUniqueElements(deps.HeaderLibs)
Colin Crossc99deeb2016-04-11 15:06:20 -0700593
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700594 for _, lib := range deps.ReexportSharedLibHeaders {
595 if !inList(lib, deps.SharedLibs) {
596 ctx.PropertyErrorf("export_shared_lib_headers", "Shared library not in shared_libs: '%s'", lib)
597 }
598 }
599
600 for _, lib := range deps.ReexportStaticLibHeaders {
601 if !inList(lib, deps.StaticLibs) {
602 ctx.PropertyErrorf("export_static_lib_headers", "Static library not in static_libs: '%s'", lib)
603 }
604 }
605
Colin Cross5950f382016-12-13 12:50:57 -0800606 for _, lib := range deps.ReexportHeaderLibHeaders {
607 if !inList(lib, deps.HeaderLibs) {
608 ctx.PropertyErrorf("export_header_lib_headers", "Header library not in header_libs: '%s'", lib)
609 }
610 }
611
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700612 for _, gen := range deps.ReexportGeneratedHeaders {
613 if !inList(gen, deps.GeneratedHeaders) {
614 ctx.PropertyErrorf("export_generated_headers", "Generated header module not in generated_headers: '%s'", gen)
615 }
616 }
617
Colin Crossc99deeb2016-04-11 15:06:20 -0700618 return deps
619}
620
Dan Albert7e9d2952016-08-04 13:02:36 -0700621func (c *Module) beginMutator(actx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800622 ctx := &baseModuleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700623 BaseContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800624 moduleContextImpl: moduleContextImpl{
625 mod: c,
626 },
627 }
628 ctx.ctx = ctx
629
Colin Crossca860ac2016-01-04 14:34:37 -0800630 c.begin(ctx)
Dan Albert7e9d2952016-08-04 13:02:36 -0700631}
632
Colin Cross1e676be2016-10-12 14:38:15 -0700633func (c *Module) DepsMutator(actx android.BottomUpMutatorContext) {
634 if !c.Enabled() {
635 return
636 }
637
Colin Cross37047f12016-12-13 17:06:13 -0800638 ctx := &depsContext{
639 BottomUpMutatorContext: actx,
Dan Albert7e9d2952016-08-04 13:02:36 -0700640 moduleContextImpl: moduleContextImpl{
641 mod: c,
642 },
643 }
644 ctx.ctx = ctx
Colin Crossca860ac2016-01-04 14:34:37 -0800645
Colin Crossc99deeb2016-04-11 15:06:20 -0700646 deps := c.deps(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800647
Colin Crossb5bc4b42016-07-11 16:11:59 -0700648 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.SharedLibs...)
649 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.LateSharedLibs...)
Dan Willemsen72d39932016-07-08 23:23:48 -0700650
Dan Albert914449f2016-06-17 16:45:24 -0700651 variantNdkLibs := []string{}
652 variantLateNdkLibs := []string{}
Dan Willemsend2ede872016-11-18 14:54:24 -0800653 if ctx.sdk() || ctx.vndk() {
Dan Albert914449f2016-06-17 16:45:24 -0700654 version := ctx.sdkVersion()
Dan Willemsen72d39932016-07-08 23:23:48 -0700655
Dan Albert914449f2016-06-17 16:45:24 -0700656 // Rewrites the names of shared libraries into the names of the NDK
657 // libraries where appropriate. This returns two slices.
658 //
659 // The first is a list of non-variant shared libraries (either rewritten
660 // NDK libraries to the modules in prebuilts/ndk, or not rewritten
661 // because they are not NDK libraries).
662 //
663 // The second is a list of ndk_library modules. These need to be
664 // separated because they are a variation dependency and must be added
665 // in a different manner.
666 rewriteNdkLibs := func(list []string) ([]string, []string) {
667 variantLibs := []string{}
668 nonvariantLibs := []string{}
669 for _, entry := range list {
Dan Willemsen72d39932016-07-08 23:23:48 -0700670 if inList(entry, ndkPrebuiltSharedLibraries) {
Dan Albert914449f2016-06-17 16:45:24 -0700671 if !inList(entry, ndkMigratedLibs) {
672 nonvariantLibs = append(nonvariantLibs, entry+".ndk."+version)
673 } else {
674 variantLibs = append(variantLibs, entry+ndkLibrarySuffix)
675 }
676 } else {
677 nonvariantLibs = append(variantLibs, entry)
Dan Willemsen72d39932016-07-08 23:23:48 -0700678 }
679 }
Dan Albert914449f2016-06-17 16:45:24 -0700680 return nonvariantLibs, variantLibs
Dan Willemsen72d39932016-07-08 23:23:48 -0700681 }
682
Dan Albert914449f2016-06-17 16:45:24 -0700683 deps.SharedLibs, variantNdkLibs = rewriteNdkLibs(deps.SharedLibs)
684 deps.LateSharedLibs, variantLateNdkLibs = rewriteNdkLibs(deps.LateSharedLibs)
Dan Willemsen72d39932016-07-08 23:23:48 -0700685 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700686
Colin Cross32ec36c2016-12-15 07:39:51 -0800687 for _, lib := range deps.HeaderLibs {
688 depTag := headerDepTag
689 if inList(lib, deps.ReexportHeaderLibHeaders) {
690 depTag = headerExportDepTag
691 }
692 actx.AddVariationDependencies(nil, depTag, lib)
693 }
Colin Cross5950f382016-12-13 12:50:57 -0800694
Colin Crossc99deeb2016-04-11 15:06:20 -0700695 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, wholeStaticDepTag,
696 deps.WholeStaticLibs...)
697
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700698 for _, lib := range deps.StaticLibs {
699 depTag := staticDepTag
700 if inList(lib, deps.ReexportStaticLibHeaders) {
701 depTag = staticExportDepTag
702 }
Colin Cross15a0d462016-07-14 14:49:58 -0700703 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700704 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700705
706 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, lateStaticDepTag,
707 deps.LateStaticLibs...)
708
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700709 for _, lib := range deps.SharedLibs {
710 depTag := sharedDepTag
711 if inList(lib, deps.ReexportSharedLibHeaders) {
712 depTag = sharedExportDepTag
713 }
Colin Cross15a0d462016-07-14 14:49:58 -0700714 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700715 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700716
717 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, lateSharedDepTag,
718 deps.LateSharedLibs...)
719
Colin Cross68861832016-07-08 10:41:41 -0700720 actx.AddDependency(c, genSourceDepTag, deps.GeneratedSources...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700721
722 for _, gen := range deps.GeneratedHeaders {
723 depTag := genHeaderDepTag
724 if inList(gen, deps.ReexportGeneratedHeaders) {
725 depTag = genHeaderExportDepTag
726 }
727 actx.AddDependency(c, depTag, gen)
728 }
Dan Willemsenb40aab62016-04-20 14:21:14 -0700729
Colin Cross68861832016-07-08 10:41:41 -0700730 actx.AddDependency(c, objDepTag, deps.ObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700731
732 if deps.CrtBegin != "" {
Colin Cross68861832016-07-08 10:41:41 -0700733 actx.AddDependency(c, crtBeginDepTag, deps.CrtBegin)
Colin Crossca860ac2016-01-04 14:34:37 -0800734 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700735 if deps.CrtEnd != "" {
Colin Cross68861832016-07-08 10:41:41 -0700736 actx.AddDependency(c, crtEndDepTag, deps.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700737 }
Dan Albert914449f2016-06-17 16:45:24 -0700738
739 version := ctx.sdkVersion()
740 actx.AddVariationDependencies([]blueprint.Variation{
741 {"ndk_api", version}, {"link", "shared"}}, ndkStubDepTag, variantNdkLibs...)
742 actx.AddVariationDependencies([]blueprint.Variation{
743 {"ndk_api", version}, {"link", "shared"}}, ndkLateStubDepTag, variantLateNdkLibs...)
Colin Cross6362e272015-10-29 15:25:03 -0700744}
Colin Cross21b9a242015-03-24 14:15:58 -0700745
Dan Albert7e9d2952016-08-04 13:02:36 -0700746func beginMutator(ctx android.BottomUpMutatorContext) {
747 if c, ok := ctx.Module().(*Module); ok && c.Enabled() {
748 c.beginMutator(ctx)
749 }
750}
751
Colin Crossca860ac2016-01-04 14:34:37 -0800752func (c *Module) clang(ctx BaseModuleContext) bool {
753 clang := Bool(c.Properties.Clang)
754
755 if c.Properties.Clang == nil {
756 if ctx.Host() {
757 clang = true
758 }
759
760 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
761 clang = true
762 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800763 }
Colin Cross28344522015-04-22 13:07:53 -0700764
Colin Crossca860ac2016-01-04 14:34:37 -0800765 if !c.toolchain(ctx).ClangSupported() {
766 clang = false
767 }
768
769 return clang
770}
771
Colin Crossc99deeb2016-04-11 15:06:20 -0700772// Convert dependencies to paths. Returns a PathDeps containing paths
Colin Cross635c3b02016-05-18 15:37:25 -0700773func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
Colin Crossca860ac2016-01-04 14:34:37 -0800774 var depPaths PathDeps
Colin Crossca860ac2016-01-04 14:34:37 -0800775
Dan Willemsena96ff642016-06-07 12:34:45 -0700776 // Whether a module can link to another module, taking into
777 // account NDK linking.
Dan Albert9e10cd42016-08-03 14:12:14 -0700778 checkLinkType := func(from, to *Module) {
Dan Willemsena96ff642016-06-07 12:34:45 -0700779 if from.Target().Os != android.Android {
780 // Host code is not restricted
Dan Albert9e10cd42016-08-03 14:12:14 -0700781 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700782 }
783 if from.Properties.Sdk_version == "" {
784 // Platform code can link to anything
Dan Albert9e10cd42016-08-03 14:12:14 -0700785 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700786 }
Colin Crossb916a382016-07-29 17:28:03 -0700787 if _, ok := to.linker.(*toolchainLibraryDecorator); ok {
Dan Willemsena96ff642016-06-07 12:34:45 -0700788 // These are always allowed
Dan Albert9e10cd42016-08-03 14:12:14 -0700789 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700790 }
791 if _, ok := to.linker.(*ndkPrebuiltLibraryLinker); ok {
792 // These are allowed, but don't set sdk_version
Dan Albert9e10cd42016-08-03 14:12:14 -0700793 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700794 }
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700795 if _, ok := to.linker.(*ndkPrebuiltStlLinker); ok {
796 // These are allowed, but don't set sdk_version
Dan Albert9e10cd42016-08-03 14:12:14 -0700797 return
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700798 }
Colin Crossb916a382016-07-29 17:28:03 -0700799 if _, ok := to.linker.(*stubDecorator); ok {
Dan Albert914449f2016-06-17 16:45:24 -0700800 // These aren't real libraries, but are the stub shared libraries that are included in
801 // the NDK.
Dan Albert9e10cd42016-08-03 14:12:14 -0700802 return
Dan Albert914449f2016-06-17 16:45:24 -0700803 }
Dan Albert9e10cd42016-08-03 14:12:14 -0700804 if to.Properties.Sdk_version == "" {
805 // NDK code linking to platform code is never okay.
806 ctx.ModuleErrorf("depends on non-NDK-built library %q",
807 ctx.OtherModuleName(to))
808 }
809
810 // All this point we know we have two NDK libraries, but we need to
811 // check that we're not linking against anything built against a higher
812 // API level, as it is only valid to link against older or equivalent
813 // APIs.
814
815 if from.Properties.Sdk_version == "current" {
816 // Current can link against anything.
817 return
818 } else if to.Properties.Sdk_version == "current" {
819 // Current can't be linked against by anything else.
820 ctx.ModuleErrorf("links %q built against newer API version %q",
821 ctx.OtherModuleName(to), "current")
822 }
823
824 fromApi, err := strconv.Atoi(from.Properties.Sdk_version)
825 if err != nil {
826 ctx.PropertyErrorf("sdk_version",
827 "Invalid sdk_version value (must be int): %q",
828 from.Properties.Sdk_version)
829 }
830 toApi, err := strconv.Atoi(to.Properties.Sdk_version)
831 if err != nil {
832 ctx.PropertyErrorf("sdk_version",
833 "Invalid sdk_version value (must be int): %q",
834 to.Properties.Sdk_version)
835 }
836
837 if toApi > fromApi {
838 ctx.ModuleErrorf("links %q built against newer API version %q",
839 ctx.OtherModuleName(to), to.Properties.Sdk_version)
840 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700841 }
842
Colin Crossc99deeb2016-04-11 15:06:20 -0700843 ctx.VisitDirectDeps(func(m blueprint.Module) {
844 name := ctx.OtherModuleName(m)
845 tag := ctx.OtherModuleDependencyTag(m)
Colin Crossca860ac2016-01-04 14:34:37 -0800846
Colin Cross635c3b02016-05-18 15:37:25 -0700847 a, _ := m.(android.Module)
Colin Crossc99deeb2016-04-11 15:06:20 -0700848 if a == nil {
849 ctx.ModuleErrorf("module %q not an android module", name)
850 return
Colin Crossca860ac2016-01-04 14:34:37 -0800851 }
Colin Crossca860ac2016-01-04 14:34:37 -0800852
Dan Willemsena96ff642016-06-07 12:34:45 -0700853 cc, _ := m.(*Module)
854 if cc == nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700855 switch tag {
Colin Cross068e0fe2016-12-13 15:23:47 -0800856 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700857 case genSourceDepTag:
858 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
859 depPaths.GeneratedSources = append(depPaths.GeneratedSources,
860 genRule.GeneratedSourceFiles()...)
861 } else {
862 ctx.ModuleErrorf("module %q is not a gensrcs or genrule", name)
863 }
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700864 case genHeaderDepTag, genHeaderExportDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700865 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
866 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders,
867 genRule.GeneratedSourceFiles()...)
Colin Cross5ed99c62016-11-22 12:55:55 -0800868 flags := includeDirsToFlags(genRule.GeneratedHeaderDirs())
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700869 depPaths.Flags = append(depPaths.Flags, flags)
870 if tag == genHeaderExportDepTag {
871 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700872 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps,
873 genRule.GeneratedSourceFiles()...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700874 }
Dan Willemsenb40aab62016-04-20 14:21:14 -0700875 } else {
876 ctx.ModuleErrorf("module %q is not a genrule", name)
877 }
878 default:
Colin Crossc99deeb2016-04-11 15:06:20 -0700879 ctx.ModuleErrorf("depends on non-cc module %q", name)
Colin Crossca860ac2016-01-04 14:34:37 -0800880 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700881 return
882 }
883
884 if !a.Enabled() {
Colin Crossa8f5e9a2016-12-13 12:51:11 -0800885 if ctx.AConfig().AllowMissingDependencies() {
886 ctx.AddMissingDependencies([]string{name})
887 } else {
888 ctx.ModuleErrorf("depends on disabled module %q", name)
889 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700890 return
891 }
892
Colin Crossa1ad8d12016-06-01 17:09:44 -0700893 if a.Target().Os != ctx.Os() {
894 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), name)
895 return
896 }
897
898 if a.Target().Arch.ArchType != ctx.Arch().ArchType {
899 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700900 return
901 }
902
Colin Crossc99deeb2016-04-11 15:06:20 -0700903 if tag == reuseObjTag {
Colin Crossbba99042016-11-23 15:45:05 -0800904 if l, ok := cc.compiler.(libraryInterface); ok {
905 depPaths.Objs = depPaths.Objs.Append(l.reuseObjs())
906 return
907 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700908 }
909
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700910 if t, ok := tag.(dependencyTag); ok && t.library {
Dan Willemsena96ff642016-06-07 12:34:45 -0700911 if i, ok := cc.linker.(exportedFlagsProducer); ok {
Dan Willemsen76f08272016-07-09 00:14:08 -0700912 flags := i.exportedFlags()
Dan Willemsen847dcc72016-09-29 12:13:36 -0700913 deps := i.exportedFlagsDeps()
Dan Willemsen76f08272016-07-09 00:14:08 -0700914 depPaths.Flags = append(depPaths.Flags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700915 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders, deps...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700916
917 if t.reexportFlags {
Dan Willemsen76f08272016-07-09 00:14:08 -0700918 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700919 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps, deps...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700920 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700921 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700922
Dan Albert9e10cd42016-08-03 14:12:14 -0700923 checkLinkType(c, cc)
Colin Crossc99deeb2016-04-11 15:06:20 -0700924 }
925
Colin Cross26c34ed2016-09-30 17:10:16 -0700926 var ptr *android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -0700927 var depPtr *android.Paths
Colin Crossc99deeb2016-04-11 15:06:20 -0700928
Colin Cross26c34ed2016-09-30 17:10:16 -0700929 linkFile := cc.outputFile
930 depFile := android.OptionalPath{}
931
Colin Crossc99deeb2016-04-11 15:06:20 -0700932 switch tag {
Dan Albert914449f2016-06-17 16:45:24 -0700933 case ndkStubDepTag, sharedDepTag, sharedExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700934 ptr = &depPaths.SharedLibs
935 depPtr = &depPaths.SharedLibsDeps
936 depFile = cc.linker.(libraryInterface).toc()
Dan Albert914449f2016-06-17 16:45:24 -0700937 case lateSharedDepTag, ndkLateStubDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700938 ptr = &depPaths.LateSharedLibs
939 depPtr = &depPaths.LateSharedLibsDeps
940 depFile = cc.linker.(libraryInterface).toc()
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700941 case staticDepTag, staticExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700942 ptr = &depPaths.StaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -0700943 case lateStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700944 ptr = &depPaths.LateStaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -0700945 case wholeStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700946 ptr = &depPaths.WholeStaticLibs
Colin Crossb916a382016-07-29 17:28:03 -0700947 staticLib, ok := cc.linker.(libraryInterface)
948 if !ok || !staticLib.static() {
Dan Willemsena96ff642016-06-07 12:34:45 -0700949 ctx.ModuleErrorf("module %q not a static library", name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700950 return
951 }
952
953 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
954 postfix := " (required by " + ctx.OtherModuleName(m) + ")"
955 for i := range missingDeps {
956 missingDeps[i] += postfix
957 }
958 ctx.AddMissingDependencies(missingDeps)
959 }
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700960 depPaths.WholeStaticLibObjs = depPaths.WholeStaticLibObjs.Append(staticLib.objs())
Colin Cross5950f382016-12-13 12:50:57 -0800961 case headerDepTag:
962 // Nothing
Colin Crossc99deeb2016-04-11 15:06:20 -0700963 case objDepTag:
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700964 depPaths.Objs.objFiles = append(depPaths.Objs.objFiles, linkFile.Path())
Colin Crossc99deeb2016-04-11 15:06:20 -0700965 case crtBeginDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700966 depPaths.CrtBegin = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -0700967 case crtEndDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700968 depPaths.CrtEnd = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -0700969 }
970
Dan Willemsen581341d2017-02-09 16:16:31 -0800971 switch tag {
972 case staticDepTag, staticExportDepTag, lateStaticDepTag:
973 staticLib, ok := cc.linker.(libraryInterface)
974 if !ok || !staticLib.static() {
975 ctx.ModuleErrorf("module %q not a static library", name)
976 return
977 }
978
979 // When combining coverage files for shared libraries and executables, coverage files
980 // in static libraries act as if they were whole static libraries.
981 depPaths.StaticLibObjs.coverageFiles = append(depPaths.StaticLibObjs.coverageFiles,
982 staticLib.objs().coverageFiles...)
983 }
984
Colin Cross26c34ed2016-09-30 17:10:16 -0700985 if ptr != nil {
Colin Crossce75d2c2016-10-06 16:12:58 -0700986 if !linkFile.Valid() {
987 ctx.ModuleErrorf("module %q missing output file", name)
988 return
989 }
Colin Cross26c34ed2016-09-30 17:10:16 -0700990 *ptr = append(*ptr, linkFile.Path())
991 }
992
Colin Crossc99deeb2016-04-11 15:06:20 -0700993 if depPtr != nil {
Colin Cross26c34ed2016-09-30 17:10:16 -0700994 dep := depFile
995 if !dep.Valid() {
996 dep = linkFile
997 }
998 *depPtr = append(*depPtr, dep.Path())
Colin Crossca860ac2016-01-04 14:34:37 -0800999 }
1000 })
1001
1002 return depPaths
1003}
1004
1005func (c *Module) InstallInData() bool {
1006 if c.installer == nil {
1007 return false
1008 }
Colin Cross94610402016-08-29 13:41:32 -07001009 if c.sanitize != nil && c.sanitize.inData() {
1010 return true
1011 }
Colin Crossca860ac2016-01-04 14:34:37 -08001012 return c.installer.inData()
1013}
1014
Dan Willemsen4aa75ca2016-09-28 16:18:03 -07001015func (c *Module) HostToolPath() android.OptionalPath {
1016 if c.installer == nil {
1017 return android.OptionalPath{}
1018 }
1019 return c.installer.hostToolPath()
1020}
1021
Colin Cross2ba19d92015-05-07 15:44:20 -07001022//
Colin Crosscfad1192015-11-02 16:43:11 -08001023// Defaults
1024//
Colin Crossca860ac2016-01-04 14:34:37 -08001025type Defaults struct {
Colin Cross635c3b02016-05-18 15:37:25 -07001026 android.ModuleBase
1027 android.DefaultsModule
Colin Crosscfad1192015-11-02 16:43:11 -08001028}
1029
Colin Cross635c3b02016-05-18 15:37:25 -07001030func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -08001031}
1032
Colin Cross1e676be2016-10-12 14:38:15 -07001033func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
1034}
1035
Colin Crossca860ac2016-01-04 14:34:37 -08001036func defaultsFactory() (blueprint.Module, []interface{}) {
Colin Crosse1d764e2016-08-18 14:18:32 -07001037 return DefaultsFactory()
1038}
1039
1040func DefaultsFactory(props ...interface{}) (blueprint.Module, []interface{}) {
Colin Crossca860ac2016-01-04 14:34:37 -08001041 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -08001042
Colin Crosse1d764e2016-08-18 14:18:32 -07001043 props = append(props,
Colin Crossca860ac2016-01-04 14:34:37 -08001044 &BaseProperties{},
1045 &BaseCompilerProperties{},
1046 &BaseLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -07001047 &LibraryProperties{},
Colin Cross919281a2016-04-05 16:42:05 -07001048 &FlagExporterProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001049 &BinaryLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -07001050 &TestProperties{},
1051 &TestBinaryProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001052 &UnusedProperties{},
1053 &StlProperties{},
Colin Cross16b23492016-01-06 14:41:07 -08001054 &SanitizeProperties{},
Colin Cross665dce92016-04-28 14:50:03 -07001055 &StripProperties{},
Dan Willemsen7424d612016-09-01 13:45:39 -07001056 &InstallerProperties{},
Dan Willemsena03cf6d2016-09-26 15:45:04 -07001057 &TidyProperties{},
Dan Willemsen581341d2017-02-09 16:16:31 -08001058 &CoverageProperties{},
Colin Crosse1d764e2016-08-18 14:18:32 -07001059 )
Colin Crosscfad1192015-11-02 16:43:11 -08001060
Colin Crosse1d764e2016-08-18 14:18:32 -07001061 return android.InitDefaultsModule(module, module, props...)
Colin Crosscfad1192015-11-02 16:43:11 -08001062}
1063
Colin Cross74d1ec02015-04-28 13:30:13 -07001064// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
1065// modifies the slice contents in place, and returns a subslice of the original slice
1066func lastUniqueElements(list []string) []string {
1067 totalSkip := 0
1068 for i := len(list) - 1; i >= totalSkip; i-- {
1069 skip := 0
1070 for j := i - 1; j >= totalSkip; j-- {
1071 if list[i] == list[j] {
1072 skip++
1073 } else {
1074 list[j+skip] = list[j]
1075 }
1076 }
1077 totalSkip += skip
1078 }
1079 return list[totalSkip:]
1080}
Colin Cross06a931b2015-10-28 17:23:31 -07001081
1082var Bool = proptools.Bool