blob: 8bf1467839345dae803ab03dd2e4a5808fb954bc [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()
49 })
Colin Crossb98c8b02016-07-29 13:44:28 -070050
51 pctx.Import("android/soong/cc/config")
Colin Cross463a90e2015-06-17 14:20:06 -070052}
53
Colin Crossca860ac2016-01-04 14:34:37 -080054type Deps struct {
55 SharedLibs, LateSharedLibs []string
56 StaticLibs, LateStaticLibs, WholeStaticLibs []string
Colin Cross5950f382016-12-13 12:50:57 -080057 HeaderLibs []string
Colin Crossc472d572015-03-17 15:06:21 -070058
Colin Cross5950f382016-12-13 12:50:57 -080059 ReexportSharedLibHeaders, ReexportStaticLibHeaders, ReexportHeaderLibHeaders []string
Dan Willemsen490a8dc2016-06-06 18:22:19 -070060
Colin Cross81413472016-04-11 14:37:39 -070061 ObjFiles []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -070062
Dan Willemsenb40aab62016-04-20 14:21:14 -070063 GeneratedSources []string
64 GeneratedHeaders []string
65
Dan Willemsenb3454ab2016-09-28 17:34:58 -070066 ReexportGeneratedHeaders []string
67
Colin Cross97ba0732015-03-23 17:50:24 -070068 CrtBegin, CrtEnd string
Colin Crossc472d572015-03-17 15:06:21 -070069}
70
Colin Crossca860ac2016-01-04 14:34:37 -080071type PathDeps struct {
Colin Cross26c34ed2016-09-30 17:10:16 -070072 // Paths to .so files
73 SharedLibs, LateSharedLibs android.Paths
74 // Paths to the dependencies to use for .so files (.so.toc files)
75 SharedLibsDeps, LateSharedLibsDeps android.Paths
76 // Paths to .a files
Colin Cross635c3b02016-05-18 15:37:25 -070077 StaticLibs, LateStaticLibs, WholeStaticLibs android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -070078
Colin Cross26c34ed2016-09-30 17:10:16 -070079 // Paths to .o files
Dan Willemsen5cb580f2016-09-26 17:33:01 -070080 Objs Objects
81 WholeStaticLibObjs Objects
Dan Willemsen34cc69e2015-09-23 15:26:20 -070082
Colin Cross26c34ed2016-09-30 17:10:16 -070083 // Paths to generated source files
Colin Cross635c3b02016-05-18 15:37:25 -070084 GeneratedSources android.Paths
85 GeneratedHeaders android.Paths
Dan Willemsenb40aab62016-04-20 14:21:14 -070086
Dan Willemsen76f08272016-07-09 00:14:08 -070087 Flags, ReexportedFlags []string
Dan Willemsen847dcc72016-09-29 12:13:36 -070088 ReexportedFlagsDeps android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -070089
Colin Cross26c34ed2016-09-30 17:10:16 -070090 // Paths to crt*.o files
Colin Cross635c3b02016-05-18 15:37:25 -070091 CrtBegin, CrtEnd android.OptionalPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -070092}
93
Colin Crossca860ac2016-01-04 14:34:37 -080094type Flags struct {
Colin Cross28344522015-04-22 13:07:53 -070095 GlobalFlags []string // Flags that apply to C, C++, and assembly source files
96 AsFlags []string // Flags that apply to assembly source files
97 CFlags []string // Flags that apply to C and C++ source files
98 ConlyFlags []string // Flags that apply to C source files
99 CppFlags []string // Flags that apply to C++ source files
100 YaccFlags []string // Flags that apply to Yacc source files
Colin Cross0c461f12016-10-20 16:11:43 -0700101 protoFlags []string // Flags that apply to proto source files
Dan Willemsene1240db2016-11-03 14:28:51 -0700102 aidlFlags []string // Flags that apply to aidl source files
Colin Cross28344522015-04-22 13:07:53 -0700103 LdFlags []string // Flags that apply to linker command lines
Colin Cross16b23492016-01-06 14:41:07 -0800104 libFlags []string // Flags to add libraries early to the link order
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700105 TidyFlags []string // Flags that apply to clang-tidy
Colin Cross91e90042016-12-02 17:13:24 -0800106 YasmFlags []string // Flags that apply to yasm assembly source files
Colin Cross28344522015-04-22 13:07:53 -0700107
Colin Crossb98c8b02016-07-29 13:44:28 -0700108 Toolchain config.Toolchain
Colin Cross28344522015-04-22 13:07:53 -0700109 Clang bool
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700110 Tidy bool
Colin Crossca860ac2016-01-04 14:34:37 -0800111
112 RequiredInstructionSet string
Colin Cross16b23492016-01-06 14:41:07 -0800113 DynamicLinker string
114
Colin Cross635c3b02016-05-18 15:37:25 -0700115 CFlagsDeps android.Paths // Files depended on by compiler flags
Colin Cross18c0c5a2016-12-01 14:45:23 -0800116
117 GroupStaticLibs bool
Colin Crossc472d572015-03-17 15:06:21 -0700118}
119
Colin Cross81413472016-04-11 14:37:39 -0700120type ObjectLinkerProperties struct {
121 // names of other cc_object modules to link into this module using partial linking
122 Objs []string `android:"arch_variant"`
123}
124
Colin Crossca860ac2016-01-04 14:34:37 -0800125// Properties used to compile all C or C++ modules
126type BaseProperties struct {
127 // compile module with clang instead of gcc
128 Clang *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700129
130 // Minimum sdk version supported when compiling against the ndk
131 Sdk_version string
132
Dan Willemsend2ede872016-11-18 14:54:24 -0800133 // Whether to compile against the VNDK
134 Use_vndk bool
135
Colin Crossca860ac2016-01-04 14:34:37 -0800136 // don't insert default compiler flags into asflags, cflags,
137 // cppflags, conlyflags, ldflags, or include_dirs
138 No_default_compiler_flags *bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700139
140 AndroidMkSharedLibs []string `blueprint:"mutated"`
Colin Crossbc6fb162016-05-24 15:39:04 -0700141 HideFromMake bool `blueprint:"mutated"`
Colin Crossb0f28952016-09-19 16:46:53 -0700142 PreventInstall bool `blueprint:"mutated"`
Dan Willemsend2ede872016-11-18 14:54:24 -0800143 Vndk_version string `blueprint:"mutated"`
Colin Crossca860ac2016-01-04 14:34:37 -0800144}
145
Colin Crossca860ac2016-01-04 14:34:37 -0800146type UnusedProperties struct {
Colin Cross21b481b2016-04-15 16:27:17 -0700147 Native_coverage *bool
Colin Cross21b481b2016-04-15 16:27:17 -0700148 Tags []string
Colin Crosscfad1192015-11-02 16:43:11 -0800149}
150
Colin Crossca860ac2016-01-04 14:34:37 -0800151type ModuleContextIntf interface {
Colin Crossca860ac2016-01-04 14:34:37 -0800152 static() bool
153 staticBinary() bool
154 clang() bool
Colin Crossb98c8b02016-07-29 13:44:28 -0700155 toolchain() config.Toolchain
Colin Crossca860ac2016-01-04 14:34:37 -0800156 noDefaultCompilerFlags() bool
157 sdk() bool
158 sdkVersion() string
Dan Willemsend2ede872016-11-18 14:54:24 -0800159 vndk() bool
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700160 selectedStl() string
Colin Crossce75d2c2016-10-06 16:12:58 -0700161 baseModuleName() string
Colin Crossca860ac2016-01-04 14:34:37 -0800162}
163
164type ModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700165 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800166 ModuleContextIntf
167}
168
169type BaseModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700170 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800171 ModuleContextIntf
172}
173
Colin Cross37047f12016-12-13 17:06:13 -0800174type DepsContext interface {
175 android.BottomUpMutatorContext
176 ModuleContextIntf
177}
178
Colin Crossca860ac2016-01-04 14:34:37 -0800179type feature interface {
180 begin(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800181 deps(ctx DepsContext, deps Deps) Deps
Colin Crossca860ac2016-01-04 14:34:37 -0800182 flags(ctx ModuleContext, flags Flags) Flags
183 props() []interface{}
184}
185
186type compiler interface {
Colin Cross42742b82016-08-01 13:20:05 -0700187 compilerInit(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800188 compilerDeps(ctx DepsContext, deps Deps) Deps
Colin Cross42742b82016-08-01 13:20:05 -0700189 compilerFlags(ctx ModuleContext, flags Flags) Flags
190 compilerProps() []interface{}
191
Colin Cross76fada02016-07-27 10:31:13 -0700192 appendCflags([]string)
193 appendAsflags([]string)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700194 compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects
Colin Crossca860ac2016-01-04 14:34:37 -0800195}
196
197type linker interface {
Colin Cross42742b82016-08-01 13:20:05 -0700198 linkerInit(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800199 linkerDeps(ctx DepsContext, deps Deps) Deps
Colin Cross42742b82016-08-01 13:20:05 -0700200 linkerFlags(ctx ModuleContext, flags Flags) Flags
201 linkerProps() []interface{}
202
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700203 link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path
Colin Cross76fada02016-07-27 10:31:13 -0700204 appendLdflags([]string)
Colin Crossca860ac2016-01-04 14:34:37 -0800205}
206
207type installer interface {
Colin Cross42742b82016-08-01 13:20:05 -0700208 installerProps() []interface{}
Colin Cross635c3b02016-05-18 15:37:25 -0700209 install(ctx ModuleContext, path android.Path)
Colin Crossca860ac2016-01-04 14:34:37 -0800210 inData() bool
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700211 hostToolPath() android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800212}
213
Colin Crossc99deeb2016-04-11 15:06:20 -0700214type dependencyTag struct {
215 blueprint.BaseDependencyTag
216 name string
217 library bool
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700218
219 reexportFlags bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700220}
221
222var (
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700223 sharedDepTag = dependencyTag{name: "shared", library: true}
224 sharedExportDepTag = dependencyTag{name: "shared", library: true, reexportFlags: true}
225 lateSharedDepTag = dependencyTag{name: "late shared", library: true}
226 staticDepTag = dependencyTag{name: "static", library: true}
227 staticExportDepTag = dependencyTag{name: "static", library: true, reexportFlags: true}
228 lateStaticDepTag = dependencyTag{name: "late static", library: true}
229 wholeStaticDepTag = dependencyTag{name: "whole static", library: true, reexportFlags: true}
Colin Cross32ec36c2016-12-15 07:39:51 -0800230 headerDepTag = dependencyTag{name: "header", library: true}
231 headerExportDepTag = dependencyTag{name: "header", library: true, reexportFlags: true}
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700232 genSourceDepTag = dependencyTag{name: "gen source"}
233 genHeaderDepTag = dependencyTag{name: "gen header"}
234 genHeaderExportDepTag = dependencyTag{name: "gen header", reexportFlags: true}
235 objDepTag = dependencyTag{name: "obj"}
236 crtBeginDepTag = dependencyTag{name: "crtbegin"}
237 crtEndDepTag = dependencyTag{name: "crtend"}
238 reuseObjTag = dependencyTag{name: "reuse objects"}
239 ndkStubDepTag = dependencyTag{name: "ndk stub", library: true}
240 ndkLateStubDepTag = dependencyTag{name: "ndk late stub", library: true}
Colin Crossc99deeb2016-04-11 15:06:20 -0700241)
242
Colin Crossca860ac2016-01-04 14:34:37 -0800243// Module contains the properties and members used by all C/C++ module types, and implements
244// the blueprint.Module interface. It delegates to compiler, linker, and installer interfaces
245// to construct the output file. Behavior can be customized with a Customizer interface
246type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700247 android.ModuleBase
248 android.DefaultableModule
Colin Crossc472d572015-03-17 15:06:21 -0700249
Colin Crossca860ac2016-01-04 14:34:37 -0800250 Properties BaseProperties
251 unused UnusedProperties
Colin Crossfa138792015-04-24 17:31:52 -0700252
Colin Crossca860ac2016-01-04 14:34:37 -0800253 // initialize before calling Init
Colin Cross635c3b02016-05-18 15:37:25 -0700254 hod android.HostOrDeviceSupported
255 multilib android.Multilib
Colin Crossc472d572015-03-17 15:06:21 -0700256
Colin Crossca860ac2016-01-04 14:34:37 -0800257 // delegates, initialize before calling Init
Colin Crossb4ce0ec2016-09-13 13:41:39 -0700258 features []feature
259 compiler compiler
260 linker linker
261 installer installer
262 stl *stl
263 sanitize *sanitize
Colin Cross16b23492016-01-06 14:41:07 -0800264
265 androidMkSharedLibDeps []string
Colin Cross74d1ec02015-04-28 13:30:13 -0700266
Colin Cross635c3b02016-05-18 15:37:25 -0700267 outputFile android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800268
Colin Crossb98c8b02016-07-29 13:44:28 -0700269 cachedToolchain config.Toolchain
Colin Crossb916a382016-07-29 17:28:03 -0700270
271 subAndroidMkOnce map[subAndroidMkProvider]bool
Colin Crossc472d572015-03-17 15:06:21 -0700272}
273
Colin Crossca860ac2016-01-04 14:34:37 -0800274func (c *Module) Init() (blueprint.Module, []interface{}) {
275 props := []interface{}{&c.Properties, &c.unused}
Colin Crossca860ac2016-01-04 14:34:37 -0800276 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700277 props = append(props, c.compiler.compilerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800278 }
279 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700280 props = append(props, c.linker.linkerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800281 }
282 if c.installer != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700283 props = append(props, c.installer.installerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800284 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700285 if c.stl != nil {
286 props = append(props, c.stl.props()...)
287 }
Colin Cross16b23492016-01-06 14:41:07 -0800288 if c.sanitize != nil {
289 props = append(props, c.sanitize.props()...)
290 }
Colin Crossca860ac2016-01-04 14:34:37 -0800291 for _, feature := range c.features {
292 props = append(props, feature.props()...)
293 }
Colin Crossc472d572015-03-17 15:06:21 -0700294
Colin Cross635c3b02016-05-18 15:37:25 -0700295 _, props = android.InitAndroidArchModule(c, c.hod, c.multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700296
Colin Cross635c3b02016-05-18 15:37:25 -0700297 return android.InitDefaultableModule(c, c, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700298}
299
Colin Crossb916a382016-07-29 17:28:03 -0700300// Returns true for dependency roots (binaries)
301// TODO(ccross): also handle dlopenable libraries
302func (c *Module) isDependencyRoot() bool {
303 if root, ok := c.linker.(interface {
304 isDependencyRoot() bool
305 }); ok {
306 return root.isDependencyRoot()
307 }
308 return false
309}
310
Colin Crossca860ac2016-01-04 14:34:37 -0800311type baseModuleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700312 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800313 moduleContextImpl
314}
315
Colin Cross37047f12016-12-13 17:06:13 -0800316type depsContext struct {
317 android.BottomUpMutatorContext
318 moduleContextImpl
319}
320
Colin Crossca860ac2016-01-04 14:34:37 -0800321type moduleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700322 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800323 moduleContextImpl
324}
325
326type moduleContextImpl struct {
327 mod *Module
328 ctx BaseModuleContext
329}
330
Colin Crossca860ac2016-01-04 14:34:37 -0800331func (ctx *moduleContextImpl) clang() bool {
332 return ctx.mod.clang(ctx.ctx)
333}
334
Colin Crossb98c8b02016-07-29 13:44:28 -0700335func (ctx *moduleContextImpl) toolchain() config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800336 return ctx.mod.toolchain(ctx.ctx)
337}
338
339func (ctx *moduleContextImpl) static() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700340 if static, ok := ctx.mod.linker.(interface {
341 static() bool
342 }); ok {
343 return static.static()
Colin Crossca860ac2016-01-04 14:34:37 -0800344 }
Colin Crossb916a382016-07-29 17:28:03 -0700345 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800346}
347
348func (ctx *moduleContextImpl) staticBinary() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700349 if static, ok := ctx.mod.linker.(interface {
350 staticBinary() bool
351 }); ok {
352 return static.staticBinary()
Colin Crossca860ac2016-01-04 14:34:37 -0800353 }
Colin Crossb916a382016-07-29 17:28:03 -0700354 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800355}
356
357func (ctx *moduleContextImpl) noDefaultCompilerFlags() bool {
358 return Bool(ctx.mod.Properties.No_default_compiler_flags)
359}
360
361func (ctx *moduleContextImpl) sdk() bool {
Dan Willemsena96ff642016-06-07 12:34:45 -0700362 if ctx.ctx.Device() {
363 return ctx.mod.Properties.Sdk_version != ""
364 }
365 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800366}
367
368func (ctx *moduleContextImpl) sdkVersion() string {
Dan Willemsena96ff642016-06-07 12:34:45 -0700369 if ctx.ctx.Device() {
Dan Willemsend2ede872016-11-18 14:54:24 -0800370 if ctx.mod.Properties.Use_vndk {
371 return ctx.mod.Properties.Vndk_version
372 } else {
373 return ctx.mod.Properties.Sdk_version
374 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700375 }
376 return ""
Colin Crossca860ac2016-01-04 14:34:37 -0800377}
378
Dan Willemsend2ede872016-11-18 14:54:24 -0800379func (ctx *moduleContextImpl) vndk() bool {
380 if ctx.ctx.Device() {
381 return ctx.mod.Properties.Use_vndk
382 }
383 return false
384}
385
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700386func (ctx *moduleContextImpl) selectedStl() string {
387 if stl := ctx.mod.stl; stl != nil {
388 return stl.Properties.SelectedStl
389 }
390 return ""
391}
392
Colin Crossce75d2c2016-10-06 16:12:58 -0700393func (ctx *moduleContextImpl) baseModuleName() string {
394 return ctx.mod.ModuleBase.BaseModuleName()
395}
396
Colin Cross635c3b02016-05-18 15:37:25 -0700397func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800398 return &Module{
399 hod: hod,
400 multilib: multilib,
401 }
402}
403
Colin Cross635c3b02016-05-18 15:37:25 -0700404func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800405 module := newBaseModule(hod, multilib)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700406 module.features = []feature{
407 &tidyFeature{},
408 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700409 module.stl = &stl{}
Colin Cross16b23492016-01-06 14:41:07 -0800410 module.sanitize = &sanitize{}
Colin Crossca860ac2016-01-04 14:34:37 -0800411 return module
412}
413
Colin Crossce75d2c2016-10-06 16:12:58 -0700414func (c *Module) Prebuilt() *android.Prebuilt {
415 if p, ok := c.linker.(prebuiltLinkerInterface); ok {
416 return p.prebuilt()
417 }
418 return nil
419}
420
421func (c *Module) Name() string {
422 name := c.ModuleBase.Name()
423 if p, ok := c.linker.(prebuiltLinkerInterface); ok {
424 name = p.Name(name)
425 }
426 return name
427}
428
Colin Cross635c3b02016-05-18 15:37:25 -0700429func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800430 ctx := &moduleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700431 ModuleContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800432 moduleContextImpl: moduleContextImpl{
433 mod: c,
434 },
435 }
436 ctx.ctx = ctx
437
438 flags := Flags{
439 Toolchain: c.toolchain(ctx),
440 Clang: c.clang(ctx),
441 }
Colin Crossca860ac2016-01-04 14:34:37 -0800442 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700443 flags = c.compiler.compilerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800444 }
445 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700446 flags = c.linker.linkerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800447 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700448 if c.stl != nil {
449 flags = c.stl.flags(ctx, flags)
450 }
Colin Cross16b23492016-01-06 14:41:07 -0800451 if c.sanitize != nil {
452 flags = c.sanitize.flags(ctx, flags)
453 }
Colin Crossca860ac2016-01-04 14:34:37 -0800454 for _, feature := range c.features {
455 flags = feature.flags(ctx, flags)
456 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800457 if ctx.Failed() {
458 return
459 }
460
Colin Crossb98c8b02016-07-29 13:44:28 -0700461 flags.CFlags, _ = filterList(flags.CFlags, config.IllegalFlags)
462 flags.CppFlags, _ = filterList(flags.CppFlags, config.IllegalFlags)
463 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, config.IllegalFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800464
Colin Crossca860ac2016-01-04 14:34:37 -0800465 // Optimization to reduce size of build.ninja
466 // Replace the long list of flags for each file with a module-local variable
467 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
468 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
469 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
470 flags.CFlags = []string{"$cflags"}
471 flags.CppFlags = []string{"$cppflags"}
472 flags.AsFlags = []string{"$asflags"}
473
Colin Crossc99deeb2016-04-11 15:06:20 -0700474 deps := c.depsToPaths(ctx)
Colin Cross3f40fa42015-01-30 17:27:36 -0800475 if ctx.Failed() {
476 return
477 }
478
Dan Willemsen76f08272016-07-09 00:14:08 -0700479 flags.GlobalFlags = append(flags.GlobalFlags, deps.Flags...)
Colin Crossed9f8682015-03-18 17:17:35 -0700480
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700481 var objs Objects
Colin Crossca860ac2016-01-04 14:34:37 -0800482 if c.compiler != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700483 objs = c.compiler.compile(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -0800484 if ctx.Failed() {
485 return
486 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800487 }
488
Colin Crossca860ac2016-01-04 14:34:37 -0800489 if c.linker != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700490 outputFile := c.linker.link(ctx, flags, deps, objs)
Colin Crossca860ac2016-01-04 14:34:37 -0800491 if ctx.Failed() {
492 return
493 }
Colin Cross635c3b02016-05-18 15:37:25 -0700494 c.outputFile = android.OptionalPathForPath(outputFile)
Colin Crossce75d2c2016-10-06 16:12:58 -0700495 }
Colin Cross5049f022015-03-18 13:28:46 -0700496
Colin Crossce75d2c2016-10-06 16:12:58 -0700497 if c.installer != nil && !c.Properties.PreventInstall && c.outputFile.Valid() {
498 c.installer.install(ctx, c.outputFile.Path())
499 if ctx.Failed() {
500 return
Colin Crossca860ac2016-01-04 14:34:37 -0800501 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700502 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800503}
504
Colin Crossb98c8b02016-07-29 13:44:28 -0700505func (c *Module) toolchain(ctx BaseModuleContext) config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800506 if c.cachedToolchain == nil {
Colin Crossb98c8b02016-07-29 13:44:28 -0700507 c.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
Colin Cross3f40fa42015-01-30 17:27:36 -0800508 }
Colin Crossca860ac2016-01-04 14:34:37 -0800509 return c.cachedToolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800510}
511
Colin Crossca860ac2016-01-04 14:34:37 -0800512func (c *Module) begin(ctx BaseModuleContext) {
513 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700514 c.compiler.compilerInit(ctx)
Colin Cross21b9a242015-03-24 14:15:58 -0700515 }
Colin Crossca860ac2016-01-04 14:34:37 -0800516 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700517 c.linker.linkerInit(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800518 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700519 if c.stl != nil {
520 c.stl.begin(ctx)
521 }
Colin Cross16b23492016-01-06 14:41:07 -0800522 if c.sanitize != nil {
523 c.sanitize.begin(ctx)
524 }
Colin Crossca860ac2016-01-04 14:34:37 -0800525 for _, feature := range c.features {
526 feature.begin(ctx)
527 }
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700528 if ctx.sdk() {
Dan Willemsend2ede872016-11-18 14:54:24 -0800529 if ctx.vndk() {
530 ctx.PropertyErrorf("use_vndk",
531 "sdk_version and use_vndk cannot be used at the same time")
532 }
533
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700534 version, err := normalizeNdkApiLevel(ctx.sdkVersion(), ctx.Arch())
535 if err != nil {
536 ctx.PropertyErrorf("sdk_version", err.Error())
537 }
Dan Albert90f7a4d2016-11-08 14:34:24 -0800538 c.Properties.Sdk_version = version
Dan Willemsend2ede872016-11-18 14:54:24 -0800539 } else if ctx.vndk() {
540 version, err := normalizeNdkApiLevel(ctx.DeviceConfig().VndkVersion(), ctx.Arch())
541 if err != nil {
542 ctx.ModuleErrorf("Bad BOARD_VNDK_VERSION: %s", err.Error())
543 }
544 c.Properties.Vndk_version = version
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700545 }
Colin Crossca860ac2016-01-04 14:34:37 -0800546}
547
Colin Cross37047f12016-12-13 17:06:13 -0800548func (c *Module) deps(ctx DepsContext) Deps {
Colin Crossc99deeb2016-04-11 15:06:20 -0700549 deps := Deps{}
550
551 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700552 deps = c.compiler.compilerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700553 }
554 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700555 deps = c.linker.linkerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700556 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700557 if c.stl != nil {
558 deps = c.stl.deps(ctx, deps)
559 }
Colin Cross16b23492016-01-06 14:41:07 -0800560 if c.sanitize != nil {
561 deps = c.sanitize.deps(ctx, deps)
562 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700563 for _, feature := range c.features {
564 deps = feature.deps(ctx, deps)
565 }
566
567 deps.WholeStaticLibs = lastUniqueElements(deps.WholeStaticLibs)
568 deps.StaticLibs = lastUniqueElements(deps.StaticLibs)
569 deps.LateStaticLibs = lastUniqueElements(deps.LateStaticLibs)
570 deps.SharedLibs = lastUniqueElements(deps.SharedLibs)
571 deps.LateSharedLibs = lastUniqueElements(deps.LateSharedLibs)
Colin Cross5950f382016-12-13 12:50:57 -0800572 deps.HeaderLibs = lastUniqueElements(deps.HeaderLibs)
Colin Crossc99deeb2016-04-11 15:06:20 -0700573
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700574 for _, lib := range deps.ReexportSharedLibHeaders {
575 if !inList(lib, deps.SharedLibs) {
576 ctx.PropertyErrorf("export_shared_lib_headers", "Shared library not in shared_libs: '%s'", lib)
577 }
578 }
579
580 for _, lib := range deps.ReexportStaticLibHeaders {
581 if !inList(lib, deps.StaticLibs) {
582 ctx.PropertyErrorf("export_static_lib_headers", "Static library not in static_libs: '%s'", lib)
583 }
584 }
585
Colin Cross5950f382016-12-13 12:50:57 -0800586 for _, lib := range deps.ReexportHeaderLibHeaders {
587 if !inList(lib, deps.HeaderLibs) {
588 ctx.PropertyErrorf("export_header_lib_headers", "Header library not in header_libs: '%s'", lib)
589 }
590 }
591
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700592 for _, gen := range deps.ReexportGeneratedHeaders {
593 if !inList(gen, deps.GeneratedHeaders) {
594 ctx.PropertyErrorf("export_generated_headers", "Generated header module not in generated_headers: '%s'", gen)
595 }
596 }
597
Colin Crossc99deeb2016-04-11 15:06:20 -0700598 return deps
599}
600
Dan Albert7e9d2952016-08-04 13:02:36 -0700601func (c *Module) beginMutator(actx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800602 ctx := &baseModuleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700603 BaseContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800604 moduleContextImpl: moduleContextImpl{
605 mod: c,
606 },
607 }
608 ctx.ctx = ctx
609
Colin Crossca860ac2016-01-04 14:34:37 -0800610 c.begin(ctx)
Dan Albert7e9d2952016-08-04 13:02:36 -0700611}
612
Colin Cross1e676be2016-10-12 14:38:15 -0700613func (c *Module) DepsMutator(actx android.BottomUpMutatorContext) {
614 if !c.Enabled() {
615 return
616 }
617
Colin Cross37047f12016-12-13 17:06:13 -0800618 ctx := &depsContext{
619 BottomUpMutatorContext: actx,
Dan Albert7e9d2952016-08-04 13:02:36 -0700620 moduleContextImpl: moduleContextImpl{
621 mod: c,
622 },
623 }
624 ctx.ctx = ctx
Colin Crossca860ac2016-01-04 14:34:37 -0800625
Colin Crossc99deeb2016-04-11 15:06:20 -0700626 deps := c.deps(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800627
Colin Crossb5bc4b42016-07-11 16:11:59 -0700628 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.SharedLibs...)
629 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.LateSharedLibs...)
Dan Willemsen72d39932016-07-08 23:23:48 -0700630
Dan Albert914449f2016-06-17 16:45:24 -0700631 variantNdkLibs := []string{}
632 variantLateNdkLibs := []string{}
Dan Willemsend2ede872016-11-18 14:54:24 -0800633 if ctx.sdk() || ctx.vndk() {
Dan Albert914449f2016-06-17 16:45:24 -0700634 version := ctx.sdkVersion()
Dan Willemsen72d39932016-07-08 23:23:48 -0700635
Dan Albert914449f2016-06-17 16:45:24 -0700636 // Rewrites the names of shared libraries into the names of the NDK
637 // libraries where appropriate. This returns two slices.
638 //
639 // The first is a list of non-variant shared libraries (either rewritten
640 // NDK libraries to the modules in prebuilts/ndk, or not rewritten
641 // because they are not NDK libraries).
642 //
643 // The second is a list of ndk_library modules. These need to be
644 // separated because they are a variation dependency and must be added
645 // in a different manner.
646 rewriteNdkLibs := func(list []string) ([]string, []string) {
647 variantLibs := []string{}
648 nonvariantLibs := []string{}
649 for _, entry := range list {
Dan Willemsen72d39932016-07-08 23:23:48 -0700650 if inList(entry, ndkPrebuiltSharedLibraries) {
Dan Albert914449f2016-06-17 16:45:24 -0700651 if !inList(entry, ndkMigratedLibs) {
652 nonvariantLibs = append(nonvariantLibs, entry+".ndk."+version)
653 } else {
654 variantLibs = append(variantLibs, entry+ndkLibrarySuffix)
655 }
656 } else {
657 nonvariantLibs = append(variantLibs, entry)
Dan Willemsen72d39932016-07-08 23:23:48 -0700658 }
659 }
Dan Albert914449f2016-06-17 16:45:24 -0700660 return nonvariantLibs, variantLibs
Dan Willemsen72d39932016-07-08 23:23:48 -0700661 }
662
Dan Albert914449f2016-06-17 16:45:24 -0700663 deps.SharedLibs, variantNdkLibs = rewriteNdkLibs(deps.SharedLibs)
664 deps.LateSharedLibs, variantLateNdkLibs = rewriteNdkLibs(deps.LateSharedLibs)
Dan Willemsen72d39932016-07-08 23:23:48 -0700665 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700666
Colin Cross32ec36c2016-12-15 07:39:51 -0800667 for _, lib := range deps.HeaderLibs {
668 depTag := headerDepTag
669 if inList(lib, deps.ReexportHeaderLibHeaders) {
670 depTag = headerExportDepTag
671 }
672 actx.AddVariationDependencies(nil, depTag, lib)
673 }
Colin Cross5950f382016-12-13 12:50:57 -0800674
Colin Crossc99deeb2016-04-11 15:06:20 -0700675 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, wholeStaticDepTag,
676 deps.WholeStaticLibs...)
677
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700678 for _, lib := range deps.StaticLibs {
679 depTag := staticDepTag
680 if inList(lib, deps.ReexportStaticLibHeaders) {
681 depTag = staticExportDepTag
682 }
Colin Cross15a0d462016-07-14 14:49:58 -0700683 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700684 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700685
686 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, lateStaticDepTag,
687 deps.LateStaticLibs...)
688
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700689 for _, lib := range deps.SharedLibs {
690 depTag := sharedDepTag
691 if inList(lib, deps.ReexportSharedLibHeaders) {
692 depTag = sharedExportDepTag
693 }
Colin Cross15a0d462016-07-14 14:49:58 -0700694 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700695 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700696
697 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, lateSharedDepTag,
698 deps.LateSharedLibs...)
699
Colin Cross68861832016-07-08 10:41:41 -0700700 actx.AddDependency(c, genSourceDepTag, deps.GeneratedSources...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700701
702 for _, gen := range deps.GeneratedHeaders {
703 depTag := genHeaderDepTag
704 if inList(gen, deps.ReexportGeneratedHeaders) {
705 depTag = genHeaderExportDepTag
706 }
707 actx.AddDependency(c, depTag, gen)
708 }
Dan Willemsenb40aab62016-04-20 14:21:14 -0700709
Colin Cross68861832016-07-08 10:41:41 -0700710 actx.AddDependency(c, objDepTag, deps.ObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700711
712 if deps.CrtBegin != "" {
Colin Cross68861832016-07-08 10:41:41 -0700713 actx.AddDependency(c, crtBeginDepTag, deps.CrtBegin)
Colin Crossca860ac2016-01-04 14:34:37 -0800714 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700715 if deps.CrtEnd != "" {
Colin Cross68861832016-07-08 10:41:41 -0700716 actx.AddDependency(c, crtEndDepTag, deps.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700717 }
Dan Albert914449f2016-06-17 16:45:24 -0700718
719 version := ctx.sdkVersion()
720 actx.AddVariationDependencies([]blueprint.Variation{
721 {"ndk_api", version}, {"link", "shared"}}, ndkStubDepTag, variantNdkLibs...)
722 actx.AddVariationDependencies([]blueprint.Variation{
723 {"ndk_api", version}, {"link", "shared"}}, ndkLateStubDepTag, variantLateNdkLibs...)
Colin Cross6362e272015-10-29 15:25:03 -0700724}
Colin Cross21b9a242015-03-24 14:15:58 -0700725
Dan Albert7e9d2952016-08-04 13:02:36 -0700726func beginMutator(ctx android.BottomUpMutatorContext) {
727 if c, ok := ctx.Module().(*Module); ok && c.Enabled() {
728 c.beginMutator(ctx)
729 }
730}
731
Colin Crossca860ac2016-01-04 14:34:37 -0800732func (c *Module) clang(ctx BaseModuleContext) bool {
733 clang := Bool(c.Properties.Clang)
734
735 if c.Properties.Clang == nil {
736 if ctx.Host() {
737 clang = true
738 }
739
740 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
741 clang = true
742 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800743 }
Colin Cross28344522015-04-22 13:07:53 -0700744
Colin Crossca860ac2016-01-04 14:34:37 -0800745 if !c.toolchain(ctx).ClangSupported() {
746 clang = false
747 }
748
749 return clang
750}
751
Colin Crossc99deeb2016-04-11 15:06:20 -0700752// Convert dependencies to paths. Returns a PathDeps containing paths
Colin Cross635c3b02016-05-18 15:37:25 -0700753func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
Colin Crossca860ac2016-01-04 14:34:37 -0800754 var depPaths PathDeps
Colin Crossca860ac2016-01-04 14:34:37 -0800755
Dan Willemsena96ff642016-06-07 12:34:45 -0700756 // Whether a module can link to another module, taking into
757 // account NDK linking.
Dan Albert9e10cd42016-08-03 14:12:14 -0700758 checkLinkType := func(from, to *Module) {
Dan Willemsena96ff642016-06-07 12:34:45 -0700759 if from.Target().Os != android.Android {
760 // Host code is not restricted
Dan Albert9e10cd42016-08-03 14:12:14 -0700761 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700762 }
763 if from.Properties.Sdk_version == "" {
764 // Platform code can link to anything
Dan Albert9e10cd42016-08-03 14:12:14 -0700765 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700766 }
Colin Crossb916a382016-07-29 17:28:03 -0700767 if _, ok := to.linker.(*toolchainLibraryDecorator); ok {
Dan Willemsena96ff642016-06-07 12:34:45 -0700768 // These are always allowed
Dan Albert9e10cd42016-08-03 14:12:14 -0700769 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700770 }
771 if _, ok := to.linker.(*ndkPrebuiltLibraryLinker); ok {
772 // These are allowed, but don't set sdk_version
Dan Albert9e10cd42016-08-03 14:12:14 -0700773 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700774 }
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700775 if _, ok := to.linker.(*ndkPrebuiltStlLinker); ok {
776 // These are allowed, but don't set sdk_version
Dan Albert9e10cd42016-08-03 14:12:14 -0700777 return
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700778 }
Colin Crossb916a382016-07-29 17:28:03 -0700779 if _, ok := to.linker.(*stubDecorator); ok {
Dan Albert914449f2016-06-17 16:45:24 -0700780 // These aren't real libraries, but are the stub shared libraries that are included in
781 // the NDK.
Dan Albert9e10cd42016-08-03 14:12:14 -0700782 return
Dan Albert914449f2016-06-17 16:45:24 -0700783 }
Dan Albert9e10cd42016-08-03 14:12:14 -0700784 if to.Properties.Sdk_version == "" {
785 // NDK code linking to platform code is never okay.
786 ctx.ModuleErrorf("depends on non-NDK-built library %q",
787 ctx.OtherModuleName(to))
788 }
789
790 // All this point we know we have two NDK libraries, but we need to
791 // check that we're not linking against anything built against a higher
792 // API level, as it is only valid to link against older or equivalent
793 // APIs.
794
795 if from.Properties.Sdk_version == "current" {
796 // Current can link against anything.
797 return
798 } else if to.Properties.Sdk_version == "current" {
799 // Current can't be linked against by anything else.
800 ctx.ModuleErrorf("links %q built against newer API version %q",
801 ctx.OtherModuleName(to), "current")
802 }
803
804 fromApi, err := strconv.Atoi(from.Properties.Sdk_version)
805 if err != nil {
806 ctx.PropertyErrorf("sdk_version",
807 "Invalid sdk_version value (must be int): %q",
808 from.Properties.Sdk_version)
809 }
810 toApi, err := strconv.Atoi(to.Properties.Sdk_version)
811 if err != nil {
812 ctx.PropertyErrorf("sdk_version",
813 "Invalid sdk_version value (must be int): %q",
814 to.Properties.Sdk_version)
815 }
816
817 if toApi > fromApi {
818 ctx.ModuleErrorf("links %q built against newer API version %q",
819 ctx.OtherModuleName(to), to.Properties.Sdk_version)
820 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700821 }
822
Colin Crossc99deeb2016-04-11 15:06:20 -0700823 ctx.VisitDirectDeps(func(m blueprint.Module) {
824 name := ctx.OtherModuleName(m)
825 tag := ctx.OtherModuleDependencyTag(m)
Colin Crossca860ac2016-01-04 14:34:37 -0800826
Colin Cross635c3b02016-05-18 15:37:25 -0700827 a, _ := m.(android.Module)
Colin Crossc99deeb2016-04-11 15:06:20 -0700828 if a == nil {
829 ctx.ModuleErrorf("module %q not an android module", name)
830 return
Colin Crossca860ac2016-01-04 14:34:37 -0800831 }
Colin Crossca860ac2016-01-04 14:34:37 -0800832
Dan Willemsena96ff642016-06-07 12:34:45 -0700833 cc, _ := m.(*Module)
834 if cc == nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700835 switch tag {
Colin Cross068e0fe2016-12-13 15:23:47 -0800836 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700837 case genSourceDepTag:
838 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
839 depPaths.GeneratedSources = append(depPaths.GeneratedSources,
840 genRule.GeneratedSourceFiles()...)
841 } else {
842 ctx.ModuleErrorf("module %q is not a gensrcs or genrule", name)
843 }
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700844 case genHeaderDepTag, genHeaderExportDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700845 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
846 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders,
847 genRule.GeneratedSourceFiles()...)
Colin Cross5ed99c62016-11-22 12:55:55 -0800848 flags := includeDirsToFlags(genRule.GeneratedHeaderDirs())
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700849 depPaths.Flags = append(depPaths.Flags, flags)
850 if tag == genHeaderExportDepTag {
851 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700852 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps,
853 genRule.GeneratedSourceFiles()...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700854 }
Dan Willemsenb40aab62016-04-20 14:21:14 -0700855 } else {
856 ctx.ModuleErrorf("module %q is not a genrule", name)
857 }
858 default:
Colin Crossc99deeb2016-04-11 15:06:20 -0700859 ctx.ModuleErrorf("depends on non-cc module %q", name)
Colin Crossca860ac2016-01-04 14:34:37 -0800860 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700861 return
862 }
863
864 if !a.Enabled() {
Colin Crossa8f5e9a2016-12-13 12:51:11 -0800865 if ctx.AConfig().AllowMissingDependencies() {
866 ctx.AddMissingDependencies([]string{name})
867 } else {
868 ctx.ModuleErrorf("depends on disabled module %q", name)
869 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700870 return
871 }
872
Colin Crossa1ad8d12016-06-01 17:09:44 -0700873 if a.Target().Os != ctx.Os() {
874 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), name)
875 return
876 }
877
878 if a.Target().Arch.ArchType != ctx.Arch().ArchType {
879 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700880 return
881 }
882
Colin Crossc99deeb2016-04-11 15:06:20 -0700883 if tag == reuseObjTag {
Colin Crossbba99042016-11-23 15:45:05 -0800884 if l, ok := cc.compiler.(libraryInterface); ok {
885 depPaths.Objs = depPaths.Objs.Append(l.reuseObjs())
886 return
887 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700888 }
889
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700890 if t, ok := tag.(dependencyTag); ok && t.library {
Dan Willemsena96ff642016-06-07 12:34:45 -0700891 if i, ok := cc.linker.(exportedFlagsProducer); ok {
Dan Willemsen76f08272016-07-09 00:14:08 -0700892 flags := i.exportedFlags()
Dan Willemsen847dcc72016-09-29 12:13:36 -0700893 deps := i.exportedFlagsDeps()
Dan Willemsen76f08272016-07-09 00:14:08 -0700894 depPaths.Flags = append(depPaths.Flags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700895 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders, deps...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700896
897 if t.reexportFlags {
Dan Willemsen76f08272016-07-09 00:14:08 -0700898 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700899 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps, deps...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700900 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700901 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700902
Dan Albert9e10cd42016-08-03 14:12:14 -0700903 checkLinkType(c, cc)
Colin Crossc99deeb2016-04-11 15:06:20 -0700904 }
905
Colin Cross26c34ed2016-09-30 17:10:16 -0700906 var ptr *android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -0700907 var depPtr *android.Paths
Colin Crossc99deeb2016-04-11 15:06:20 -0700908
Colin Cross26c34ed2016-09-30 17:10:16 -0700909 linkFile := cc.outputFile
910 depFile := android.OptionalPath{}
911
Colin Crossc99deeb2016-04-11 15:06:20 -0700912 switch tag {
Dan Albert914449f2016-06-17 16:45:24 -0700913 case ndkStubDepTag, sharedDepTag, sharedExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700914 ptr = &depPaths.SharedLibs
915 depPtr = &depPaths.SharedLibsDeps
916 depFile = cc.linker.(libraryInterface).toc()
Dan Albert914449f2016-06-17 16:45:24 -0700917 case lateSharedDepTag, ndkLateStubDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700918 ptr = &depPaths.LateSharedLibs
919 depPtr = &depPaths.LateSharedLibsDeps
920 depFile = cc.linker.(libraryInterface).toc()
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700921 case staticDepTag, staticExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700922 ptr = &depPaths.StaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -0700923 case lateStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700924 ptr = &depPaths.LateStaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -0700925 case wholeStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700926 ptr = &depPaths.WholeStaticLibs
Colin Crossb916a382016-07-29 17:28:03 -0700927 staticLib, ok := cc.linker.(libraryInterface)
928 if !ok || !staticLib.static() {
Dan Willemsena96ff642016-06-07 12:34:45 -0700929 ctx.ModuleErrorf("module %q not a static library", name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700930 return
931 }
932
933 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
934 postfix := " (required by " + ctx.OtherModuleName(m) + ")"
935 for i := range missingDeps {
936 missingDeps[i] += postfix
937 }
938 ctx.AddMissingDependencies(missingDeps)
939 }
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700940 depPaths.WholeStaticLibObjs = depPaths.WholeStaticLibObjs.Append(staticLib.objs())
Colin Cross5950f382016-12-13 12:50:57 -0800941 case headerDepTag:
942 // Nothing
Colin Crossc99deeb2016-04-11 15:06:20 -0700943 case objDepTag:
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700944 depPaths.Objs.objFiles = append(depPaths.Objs.objFiles, linkFile.Path())
Colin Crossc99deeb2016-04-11 15:06:20 -0700945 case crtBeginDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700946 depPaths.CrtBegin = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -0700947 case crtEndDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700948 depPaths.CrtEnd = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -0700949 }
950
Colin Cross26c34ed2016-09-30 17:10:16 -0700951 if ptr != nil {
Colin Crossce75d2c2016-10-06 16:12:58 -0700952 if !linkFile.Valid() {
953 ctx.ModuleErrorf("module %q missing output file", name)
954 return
955 }
Colin Cross26c34ed2016-09-30 17:10:16 -0700956 *ptr = append(*ptr, linkFile.Path())
957 }
958
Colin Crossc99deeb2016-04-11 15:06:20 -0700959 if depPtr != nil {
Colin Cross26c34ed2016-09-30 17:10:16 -0700960 dep := depFile
961 if !dep.Valid() {
962 dep = linkFile
963 }
964 *depPtr = append(*depPtr, dep.Path())
Colin Crossca860ac2016-01-04 14:34:37 -0800965 }
966 })
967
968 return depPaths
969}
970
971func (c *Module) InstallInData() bool {
972 if c.installer == nil {
973 return false
974 }
Colin Cross94610402016-08-29 13:41:32 -0700975 if c.sanitize != nil && c.sanitize.inData() {
976 return true
977 }
Colin Crossca860ac2016-01-04 14:34:37 -0800978 return c.installer.inData()
979}
980
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700981func (c *Module) HostToolPath() android.OptionalPath {
982 if c.installer == nil {
983 return android.OptionalPath{}
984 }
985 return c.installer.hostToolPath()
986}
987
Colin Cross2ba19d92015-05-07 15:44:20 -0700988//
Colin Crosscfad1192015-11-02 16:43:11 -0800989// Defaults
990//
Colin Crossca860ac2016-01-04 14:34:37 -0800991type Defaults struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700992 android.ModuleBase
993 android.DefaultsModule
Colin Crosscfad1192015-11-02 16:43:11 -0800994}
995
Colin Cross635c3b02016-05-18 15:37:25 -0700996func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -0800997}
998
Colin Cross1e676be2016-10-12 14:38:15 -0700999func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
1000}
1001
Colin Crossca860ac2016-01-04 14:34:37 -08001002func defaultsFactory() (blueprint.Module, []interface{}) {
Colin Crosse1d764e2016-08-18 14:18:32 -07001003 return DefaultsFactory()
1004}
1005
1006func DefaultsFactory(props ...interface{}) (blueprint.Module, []interface{}) {
Colin Crossca860ac2016-01-04 14:34:37 -08001007 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -08001008
Colin Crosse1d764e2016-08-18 14:18:32 -07001009 props = append(props,
Colin Crossca860ac2016-01-04 14:34:37 -08001010 &BaseProperties{},
1011 &BaseCompilerProperties{},
1012 &BaseLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -07001013 &LibraryProperties{},
Colin Cross919281a2016-04-05 16:42:05 -07001014 &FlagExporterProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001015 &BinaryLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -07001016 &TestProperties{},
1017 &TestBinaryProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001018 &UnusedProperties{},
1019 &StlProperties{},
Colin Cross16b23492016-01-06 14:41:07 -08001020 &SanitizeProperties{},
Colin Cross665dce92016-04-28 14:50:03 -07001021 &StripProperties{},
Dan Willemsen7424d612016-09-01 13:45:39 -07001022 &InstallerProperties{},
Dan Willemsena03cf6d2016-09-26 15:45:04 -07001023 &TidyProperties{},
Colin Crosse1d764e2016-08-18 14:18:32 -07001024 )
Colin Crosscfad1192015-11-02 16:43:11 -08001025
Colin Crosse1d764e2016-08-18 14:18:32 -07001026 return android.InitDefaultsModule(module, module, props...)
Colin Crosscfad1192015-11-02 16:43:11 -08001027}
1028
Colin Cross74d1ec02015-04-28 13:30:13 -07001029// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
1030// modifies the slice contents in place, and returns a subslice of the original slice
1031func lastUniqueElements(list []string) []string {
1032 totalSkip := 0
1033 for i := len(list) - 1; i >= totalSkip; i-- {
1034 skip := 0
1035 for j := i - 1; j >= totalSkip; j-- {
1036 if list[i] == list[j] {
1037 skip++
1038 } else {
1039 list[j+skip] = list[j]
1040 }
1041 }
1042 totalSkip += skip
1043 }
1044 return list[totalSkip:]
1045}
Colin Cross06a931b2015-10-28 17:23:31 -07001046
1047var Bool = proptools.Bool