blob: 65250475ca1d63630e509cfd5c9affa42a435373 [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 Crossc472d572015-03-17 15:06:21 -070057
Dan Willemsen490a8dc2016-06-06 18:22:19 -070058 ReexportSharedLibHeaders, ReexportStaticLibHeaders []string
59
Colin Cross81413472016-04-11 14:37:39 -070060 ObjFiles []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -070061
Dan Willemsenb40aab62016-04-20 14:21:14 -070062 GeneratedSources []string
63 GeneratedHeaders []string
64
Dan Willemsenb3454ab2016-09-28 17:34:58 -070065 ReexportGeneratedHeaders []string
66
Colin Cross97ba0732015-03-23 17:50:24 -070067 CrtBegin, CrtEnd string
Colin Crossc472d572015-03-17 15:06:21 -070068}
69
Colin Crossca860ac2016-01-04 14:34:37 -080070type PathDeps struct {
Colin Cross26c34ed2016-09-30 17:10:16 -070071 // Paths to .so files
72 SharedLibs, LateSharedLibs android.Paths
73 // Paths to the dependencies to use for .so files (.so.toc files)
74 SharedLibsDeps, LateSharedLibsDeps android.Paths
75 // Paths to .a files
Colin Cross635c3b02016-05-18 15:37:25 -070076 StaticLibs, LateStaticLibs, WholeStaticLibs android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -070077
Colin Cross26c34ed2016-09-30 17:10:16 -070078 // Paths to .o files
Dan Willemsen5cb580f2016-09-26 17:33:01 -070079 Objs Objects
80 WholeStaticLibObjs Objects
Dan Willemsen34cc69e2015-09-23 15:26:20 -070081
Colin Cross26c34ed2016-09-30 17:10:16 -070082 // Paths to generated source files
Colin Cross635c3b02016-05-18 15:37:25 -070083 GeneratedSources android.Paths
84 GeneratedHeaders android.Paths
Dan Willemsenb40aab62016-04-20 14:21:14 -070085
Dan Willemsen76f08272016-07-09 00:14:08 -070086 Flags, ReexportedFlags []string
Dan Willemsen847dcc72016-09-29 12:13:36 -070087 ReexportedFlagsDeps android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -070088
Colin Cross26c34ed2016-09-30 17:10:16 -070089 // Paths to crt*.o files
Colin Cross635c3b02016-05-18 15:37:25 -070090 CrtBegin, CrtEnd android.OptionalPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -070091}
92
Colin Crossca860ac2016-01-04 14:34:37 -080093type Flags struct {
Colin Cross28344522015-04-22 13:07:53 -070094 GlobalFlags []string // Flags that apply to C, C++, and assembly source files
95 AsFlags []string // Flags that apply to assembly source files
96 CFlags []string // Flags that apply to C and C++ source files
97 ConlyFlags []string // Flags that apply to C source files
98 CppFlags []string // Flags that apply to C++ source files
99 YaccFlags []string // Flags that apply to Yacc source files
Colin Cross0c461f12016-10-20 16:11:43 -0700100 protoFlags []string // Flags that apply to proto source files
Dan Willemsene1240db2016-11-03 14:28:51 -0700101 aidlFlags []string // Flags that apply to aidl source files
Colin Cross28344522015-04-22 13:07:53 -0700102 LdFlags []string // Flags that apply to linker command lines
Colin Cross16b23492016-01-06 14:41:07 -0800103 libFlags []string // Flags to add libraries early to the link order
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700104 TidyFlags []string // Flags that apply to clang-tidy
Colin Cross91e90042016-12-02 17:13:24 -0800105 YasmFlags []string // Flags that apply to yasm assembly source files
Colin Cross28344522015-04-22 13:07:53 -0700106
Colin Crossb98c8b02016-07-29 13:44:28 -0700107 Toolchain config.Toolchain
Colin Cross28344522015-04-22 13:07:53 -0700108 Clang bool
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700109 Tidy bool
Colin Crossca860ac2016-01-04 14:34:37 -0800110
111 RequiredInstructionSet string
Colin Cross16b23492016-01-06 14:41:07 -0800112 DynamicLinker string
113
Colin Cross635c3b02016-05-18 15:37:25 -0700114 CFlagsDeps android.Paths // Files depended on by compiler flags
Colin Cross18c0c5a2016-12-01 14:45:23 -0800115
116 GroupStaticLibs bool
Colin Crossc472d572015-03-17 15:06:21 -0700117}
118
Colin Cross81413472016-04-11 14:37:39 -0700119type ObjectLinkerProperties struct {
120 // names of other cc_object modules to link into this module using partial linking
121 Objs []string `android:"arch_variant"`
122}
123
Colin Crossca860ac2016-01-04 14:34:37 -0800124// Properties used to compile all C or C++ modules
125type BaseProperties struct {
126 // compile module with clang instead of gcc
127 Clang *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700128
129 // Minimum sdk version supported when compiling against the ndk
130 Sdk_version string
131
Dan Willemsend2ede872016-11-18 14:54:24 -0800132 // Whether to compile against the VNDK
133 Use_vndk bool
134
Colin Crossca860ac2016-01-04 14:34:37 -0800135 // don't insert default compiler flags into asflags, cflags,
136 // cppflags, conlyflags, ldflags, or include_dirs
137 No_default_compiler_flags *bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700138
139 AndroidMkSharedLibs []string `blueprint:"mutated"`
Colin Crossbc6fb162016-05-24 15:39:04 -0700140 HideFromMake bool `blueprint:"mutated"`
Colin Crossb0f28952016-09-19 16:46:53 -0700141 PreventInstall bool `blueprint:"mutated"`
Dan Willemsend2ede872016-11-18 14:54:24 -0800142 Vndk_version string `blueprint:"mutated"`
Colin Crossca860ac2016-01-04 14:34:37 -0800143}
144
Colin Crossca860ac2016-01-04 14:34:37 -0800145type UnusedProperties struct {
Colin Cross21b481b2016-04-15 16:27:17 -0700146 Native_coverage *bool
Colin Cross21b481b2016-04-15 16:27:17 -0700147 Tags []string
Colin Crosscfad1192015-11-02 16:43:11 -0800148}
149
Colin Crossca860ac2016-01-04 14:34:37 -0800150type ModuleContextIntf interface {
Colin Crossca860ac2016-01-04 14:34:37 -0800151 static() bool
152 staticBinary() bool
153 clang() bool
Colin Crossb98c8b02016-07-29 13:44:28 -0700154 toolchain() config.Toolchain
Colin Crossca860ac2016-01-04 14:34:37 -0800155 noDefaultCompilerFlags() bool
156 sdk() bool
157 sdkVersion() string
Dan Willemsend2ede872016-11-18 14:54:24 -0800158 vndk() bool
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700159 selectedStl() string
Colin Crossce75d2c2016-10-06 16:12:58 -0700160 baseModuleName() string
Colin Crossca860ac2016-01-04 14:34:37 -0800161}
162
163type ModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700164 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800165 ModuleContextIntf
166}
167
168type BaseModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700169 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800170 ModuleContextIntf
171}
172
Colin Crossca860ac2016-01-04 14:34:37 -0800173type feature interface {
174 begin(ctx BaseModuleContext)
175 deps(ctx BaseModuleContext, deps Deps) Deps
176 flags(ctx ModuleContext, flags Flags) Flags
177 props() []interface{}
178}
179
180type compiler interface {
Colin Cross42742b82016-08-01 13:20:05 -0700181 compilerInit(ctx BaseModuleContext)
182 compilerDeps(ctx BaseModuleContext, deps Deps) Deps
183 compilerFlags(ctx ModuleContext, flags Flags) Flags
184 compilerProps() []interface{}
185
Colin Cross76fada02016-07-27 10:31:13 -0700186 appendCflags([]string)
187 appendAsflags([]string)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700188 compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects
Colin Crossca860ac2016-01-04 14:34:37 -0800189}
190
191type linker interface {
Colin Cross42742b82016-08-01 13:20:05 -0700192 linkerInit(ctx BaseModuleContext)
193 linkerDeps(ctx BaseModuleContext, deps Deps) Deps
194 linkerFlags(ctx ModuleContext, flags Flags) Flags
195 linkerProps() []interface{}
196
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700197 link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path
Colin Cross76fada02016-07-27 10:31:13 -0700198 appendLdflags([]string)
Colin Crossca860ac2016-01-04 14:34:37 -0800199}
200
201type installer interface {
Colin Cross42742b82016-08-01 13:20:05 -0700202 installerProps() []interface{}
Colin Cross635c3b02016-05-18 15:37:25 -0700203 install(ctx ModuleContext, path android.Path)
Colin Crossca860ac2016-01-04 14:34:37 -0800204 inData() bool
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700205 hostToolPath() android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800206}
207
Colin Crossc99deeb2016-04-11 15:06:20 -0700208type dependencyTag struct {
209 blueprint.BaseDependencyTag
210 name string
211 library bool
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700212
213 reexportFlags bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700214}
215
216var (
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700217 sharedDepTag = dependencyTag{name: "shared", library: true}
218 sharedExportDepTag = dependencyTag{name: "shared", library: true, reexportFlags: true}
219 lateSharedDepTag = dependencyTag{name: "late shared", library: true}
220 staticDepTag = dependencyTag{name: "static", library: true}
221 staticExportDepTag = dependencyTag{name: "static", library: true, reexportFlags: true}
222 lateStaticDepTag = dependencyTag{name: "late static", library: true}
223 wholeStaticDepTag = dependencyTag{name: "whole static", library: true, reexportFlags: true}
224 genSourceDepTag = dependencyTag{name: "gen source"}
225 genHeaderDepTag = dependencyTag{name: "gen header"}
226 genHeaderExportDepTag = dependencyTag{name: "gen header", reexportFlags: true}
227 objDepTag = dependencyTag{name: "obj"}
228 crtBeginDepTag = dependencyTag{name: "crtbegin"}
229 crtEndDepTag = dependencyTag{name: "crtend"}
230 reuseObjTag = dependencyTag{name: "reuse objects"}
231 ndkStubDepTag = dependencyTag{name: "ndk stub", library: true}
232 ndkLateStubDepTag = dependencyTag{name: "ndk late stub", library: true}
Colin Crossc99deeb2016-04-11 15:06:20 -0700233)
234
Colin Crossca860ac2016-01-04 14:34:37 -0800235// Module contains the properties and members used by all C/C++ module types, and implements
236// the blueprint.Module interface. It delegates to compiler, linker, and installer interfaces
237// to construct the output file. Behavior can be customized with a Customizer interface
238type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700239 android.ModuleBase
240 android.DefaultableModule
Colin Crossc472d572015-03-17 15:06:21 -0700241
Colin Crossca860ac2016-01-04 14:34:37 -0800242 Properties BaseProperties
243 unused UnusedProperties
Colin Crossfa138792015-04-24 17:31:52 -0700244
Colin Crossca860ac2016-01-04 14:34:37 -0800245 // initialize before calling Init
Colin Cross635c3b02016-05-18 15:37:25 -0700246 hod android.HostOrDeviceSupported
247 multilib android.Multilib
Colin Crossc472d572015-03-17 15:06:21 -0700248
Colin Crossca860ac2016-01-04 14:34:37 -0800249 // delegates, initialize before calling Init
Colin Crossb4ce0ec2016-09-13 13:41:39 -0700250 features []feature
251 compiler compiler
252 linker linker
253 installer installer
254 stl *stl
255 sanitize *sanitize
Colin Cross16b23492016-01-06 14:41:07 -0800256
257 androidMkSharedLibDeps []string
Colin Cross74d1ec02015-04-28 13:30:13 -0700258
Colin Cross635c3b02016-05-18 15:37:25 -0700259 outputFile android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800260
Colin Crossb98c8b02016-07-29 13:44:28 -0700261 cachedToolchain config.Toolchain
Colin Crossb916a382016-07-29 17:28:03 -0700262
263 subAndroidMkOnce map[subAndroidMkProvider]bool
Colin Crossc472d572015-03-17 15:06:21 -0700264}
265
Colin Crossca860ac2016-01-04 14:34:37 -0800266func (c *Module) Init() (blueprint.Module, []interface{}) {
267 props := []interface{}{&c.Properties, &c.unused}
Colin Crossca860ac2016-01-04 14:34:37 -0800268 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700269 props = append(props, c.compiler.compilerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800270 }
271 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700272 props = append(props, c.linker.linkerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800273 }
274 if c.installer != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700275 props = append(props, c.installer.installerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800276 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700277 if c.stl != nil {
278 props = append(props, c.stl.props()...)
279 }
Colin Cross16b23492016-01-06 14:41:07 -0800280 if c.sanitize != nil {
281 props = append(props, c.sanitize.props()...)
282 }
Colin Crossca860ac2016-01-04 14:34:37 -0800283 for _, feature := range c.features {
284 props = append(props, feature.props()...)
285 }
Colin Crossc472d572015-03-17 15:06:21 -0700286
Colin Cross635c3b02016-05-18 15:37:25 -0700287 _, props = android.InitAndroidArchModule(c, c.hod, c.multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700288
Colin Cross635c3b02016-05-18 15:37:25 -0700289 return android.InitDefaultableModule(c, c, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700290}
291
Colin Crossb916a382016-07-29 17:28:03 -0700292// Returns true for dependency roots (binaries)
293// TODO(ccross): also handle dlopenable libraries
294func (c *Module) isDependencyRoot() bool {
295 if root, ok := c.linker.(interface {
296 isDependencyRoot() bool
297 }); ok {
298 return root.isDependencyRoot()
299 }
300 return false
301}
302
Colin Crossca860ac2016-01-04 14:34:37 -0800303type baseModuleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700304 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800305 moduleContextImpl
306}
307
308type moduleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700309 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800310 moduleContextImpl
311}
312
313type moduleContextImpl struct {
314 mod *Module
315 ctx BaseModuleContext
316}
317
Colin Crossca860ac2016-01-04 14:34:37 -0800318func (ctx *moduleContextImpl) clang() bool {
319 return ctx.mod.clang(ctx.ctx)
320}
321
Colin Crossb98c8b02016-07-29 13:44:28 -0700322func (ctx *moduleContextImpl) toolchain() config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800323 return ctx.mod.toolchain(ctx.ctx)
324}
325
326func (ctx *moduleContextImpl) static() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700327 if static, ok := ctx.mod.linker.(interface {
328 static() bool
329 }); ok {
330 return static.static()
Colin Crossca860ac2016-01-04 14:34:37 -0800331 }
Colin Crossb916a382016-07-29 17:28:03 -0700332 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800333}
334
335func (ctx *moduleContextImpl) staticBinary() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700336 if static, ok := ctx.mod.linker.(interface {
337 staticBinary() bool
338 }); ok {
339 return static.staticBinary()
Colin Crossca860ac2016-01-04 14:34:37 -0800340 }
Colin Crossb916a382016-07-29 17:28:03 -0700341 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800342}
343
344func (ctx *moduleContextImpl) noDefaultCompilerFlags() bool {
345 return Bool(ctx.mod.Properties.No_default_compiler_flags)
346}
347
348func (ctx *moduleContextImpl) sdk() bool {
Dan Willemsena96ff642016-06-07 12:34:45 -0700349 if ctx.ctx.Device() {
350 return ctx.mod.Properties.Sdk_version != ""
351 }
352 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800353}
354
355func (ctx *moduleContextImpl) sdkVersion() string {
Dan Willemsena96ff642016-06-07 12:34:45 -0700356 if ctx.ctx.Device() {
Dan Willemsend2ede872016-11-18 14:54:24 -0800357 if ctx.mod.Properties.Use_vndk {
358 return ctx.mod.Properties.Vndk_version
359 } else {
360 return ctx.mod.Properties.Sdk_version
361 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700362 }
363 return ""
Colin Crossca860ac2016-01-04 14:34:37 -0800364}
365
Dan Willemsend2ede872016-11-18 14:54:24 -0800366func (ctx *moduleContextImpl) vndk() bool {
367 if ctx.ctx.Device() {
368 return ctx.mod.Properties.Use_vndk
369 }
370 return false
371}
372
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700373func (ctx *moduleContextImpl) selectedStl() string {
374 if stl := ctx.mod.stl; stl != nil {
375 return stl.Properties.SelectedStl
376 }
377 return ""
378}
379
Colin Crossce75d2c2016-10-06 16:12:58 -0700380func (ctx *moduleContextImpl) baseModuleName() string {
381 return ctx.mod.ModuleBase.BaseModuleName()
382}
383
Colin Cross635c3b02016-05-18 15:37:25 -0700384func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800385 return &Module{
386 hod: hod,
387 multilib: multilib,
388 }
389}
390
Colin Cross635c3b02016-05-18 15:37:25 -0700391func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800392 module := newBaseModule(hod, multilib)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700393 module.features = []feature{
394 &tidyFeature{},
395 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700396 module.stl = &stl{}
Colin Cross16b23492016-01-06 14:41:07 -0800397 module.sanitize = &sanitize{}
Colin Crossca860ac2016-01-04 14:34:37 -0800398 return module
399}
400
Colin Crossce75d2c2016-10-06 16:12:58 -0700401func (c *Module) Prebuilt() *android.Prebuilt {
402 if p, ok := c.linker.(prebuiltLinkerInterface); ok {
403 return p.prebuilt()
404 }
405 return nil
406}
407
408func (c *Module) Name() string {
409 name := c.ModuleBase.Name()
410 if p, ok := c.linker.(prebuiltLinkerInterface); ok {
411 name = p.Name(name)
412 }
413 return name
414}
415
Colin Cross635c3b02016-05-18 15:37:25 -0700416func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800417 ctx := &moduleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700418 ModuleContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800419 moduleContextImpl: moduleContextImpl{
420 mod: c,
421 },
422 }
423 ctx.ctx = ctx
424
425 flags := Flags{
426 Toolchain: c.toolchain(ctx),
427 Clang: c.clang(ctx),
428 }
Colin Crossca860ac2016-01-04 14:34:37 -0800429 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700430 flags = c.compiler.compilerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800431 }
432 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700433 flags = c.linker.linkerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800434 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700435 if c.stl != nil {
436 flags = c.stl.flags(ctx, flags)
437 }
Colin Cross16b23492016-01-06 14:41:07 -0800438 if c.sanitize != nil {
439 flags = c.sanitize.flags(ctx, flags)
440 }
Colin Crossca860ac2016-01-04 14:34:37 -0800441 for _, feature := range c.features {
442 flags = feature.flags(ctx, flags)
443 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800444 if ctx.Failed() {
445 return
446 }
447
Colin Crossb98c8b02016-07-29 13:44:28 -0700448 flags.CFlags, _ = filterList(flags.CFlags, config.IllegalFlags)
449 flags.CppFlags, _ = filterList(flags.CppFlags, config.IllegalFlags)
450 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, config.IllegalFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800451
Colin Crossca860ac2016-01-04 14:34:37 -0800452 // Optimization to reduce size of build.ninja
453 // Replace the long list of flags for each file with a module-local variable
454 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
455 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
456 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
457 flags.CFlags = []string{"$cflags"}
458 flags.CppFlags = []string{"$cppflags"}
459 flags.AsFlags = []string{"$asflags"}
460
Colin Crossc99deeb2016-04-11 15:06:20 -0700461 deps := c.depsToPaths(ctx)
Colin Cross3f40fa42015-01-30 17:27:36 -0800462 if ctx.Failed() {
463 return
464 }
465
Dan Willemsen76f08272016-07-09 00:14:08 -0700466 flags.GlobalFlags = append(flags.GlobalFlags, deps.Flags...)
Colin Crossed9f8682015-03-18 17:17:35 -0700467
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700468 var objs Objects
Colin Crossca860ac2016-01-04 14:34:37 -0800469 if c.compiler != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700470 objs = c.compiler.compile(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -0800471 if ctx.Failed() {
472 return
473 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800474 }
475
Colin Crossca860ac2016-01-04 14:34:37 -0800476 if c.linker != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700477 outputFile := c.linker.link(ctx, flags, deps, objs)
Colin Crossca860ac2016-01-04 14:34:37 -0800478 if ctx.Failed() {
479 return
480 }
Colin Cross635c3b02016-05-18 15:37:25 -0700481 c.outputFile = android.OptionalPathForPath(outputFile)
Colin Crossce75d2c2016-10-06 16:12:58 -0700482 }
Colin Cross5049f022015-03-18 13:28:46 -0700483
Colin Crossce75d2c2016-10-06 16:12:58 -0700484 if c.installer != nil && !c.Properties.PreventInstall && c.outputFile.Valid() {
485 c.installer.install(ctx, c.outputFile.Path())
486 if ctx.Failed() {
487 return
Colin Crossca860ac2016-01-04 14:34:37 -0800488 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700489 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800490}
491
Colin Crossb98c8b02016-07-29 13:44:28 -0700492func (c *Module) toolchain(ctx BaseModuleContext) config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800493 if c.cachedToolchain == nil {
Colin Crossb98c8b02016-07-29 13:44:28 -0700494 c.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
Colin Cross3f40fa42015-01-30 17:27:36 -0800495 }
Colin Crossca860ac2016-01-04 14:34:37 -0800496 return c.cachedToolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800497}
498
Colin Crossca860ac2016-01-04 14:34:37 -0800499func (c *Module) begin(ctx BaseModuleContext) {
500 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700501 c.compiler.compilerInit(ctx)
Colin Cross21b9a242015-03-24 14:15:58 -0700502 }
Colin Crossca860ac2016-01-04 14:34:37 -0800503 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700504 c.linker.linkerInit(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800505 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700506 if c.stl != nil {
507 c.stl.begin(ctx)
508 }
Colin Cross16b23492016-01-06 14:41:07 -0800509 if c.sanitize != nil {
510 c.sanitize.begin(ctx)
511 }
Colin Crossca860ac2016-01-04 14:34:37 -0800512 for _, feature := range c.features {
513 feature.begin(ctx)
514 }
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700515 if ctx.sdk() {
Dan Willemsend2ede872016-11-18 14:54:24 -0800516 if ctx.vndk() {
517 ctx.PropertyErrorf("use_vndk",
518 "sdk_version and use_vndk cannot be used at the same time")
519 }
520
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700521 version, err := normalizeNdkApiLevel(ctx.sdkVersion(), ctx.Arch())
522 if err != nil {
523 ctx.PropertyErrorf("sdk_version", err.Error())
524 }
Dan Albert90f7a4d2016-11-08 14:34:24 -0800525 c.Properties.Sdk_version = version
Dan Willemsend2ede872016-11-18 14:54:24 -0800526 } else if ctx.vndk() {
527 version, err := normalizeNdkApiLevel(ctx.DeviceConfig().VndkVersion(), ctx.Arch())
528 if err != nil {
529 ctx.ModuleErrorf("Bad BOARD_VNDK_VERSION: %s", err.Error())
530 }
531 c.Properties.Vndk_version = version
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700532 }
Colin Crossca860ac2016-01-04 14:34:37 -0800533}
534
Colin Crossc99deeb2016-04-11 15:06:20 -0700535func (c *Module) deps(ctx BaseModuleContext) Deps {
536 deps := Deps{}
537
538 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700539 deps = c.compiler.compilerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700540 }
541 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700542 deps = c.linker.linkerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700543 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700544 if c.stl != nil {
545 deps = c.stl.deps(ctx, deps)
546 }
Colin Cross16b23492016-01-06 14:41:07 -0800547 if c.sanitize != nil {
548 deps = c.sanitize.deps(ctx, deps)
549 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700550 for _, feature := range c.features {
551 deps = feature.deps(ctx, deps)
552 }
553
554 deps.WholeStaticLibs = lastUniqueElements(deps.WholeStaticLibs)
555 deps.StaticLibs = lastUniqueElements(deps.StaticLibs)
556 deps.LateStaticLibs = lastUniqueElements(deps.LateStaticLibs)
557 deps.SharedLibs = lastUniqueElements(deps.SharedLibs)
558 deps.LateSharedLibs = lastUniqueElements(deps.LateSharedLibs)
559
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700560 for _, lib := range deps.ReexportSharedLibHeaders {
561 if !inList(lib, deps.SharedLibs) {
562 ctx.PropertyErrorf("export_shared_lib_headers", "Shared library not in shared_libs: '%s'", lib)
563 }
564 }
565
566 for _, lib := range deps.ReexportStaticLibHeaders {
567 if !inList(lib, deps.StaticLibs) {
568 ctx.PropertyErrorf("export_static_lib_headers", "Static library not in static_libs: '%s'", lib)
569 }
570 }
571
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700572 for _, gen := range deps.ReexportGeneratedHeaders {
573 if !inList(gen, deps.GeneratedHeaders) {
574 ctx.PropertyErrorf("export_generated_headers", "Generated header module not in generated_headers: '%s'", gen)
575 }
576 }
577
Colin Crossc99deeb2016-04-11 15:06:20 -0700578 return deps
579}
580
Dan Albert7e9d2952016-08-04 13:02:36 -0700581func (c *Module) beginMutator(actx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800582 ctx := &baseModuleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700583 BaseContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800584 moduleContextImpl: moduleContextImpl{
585 mod: c,
586 },
587 }
588 ctx.ctx = ctx
589
Colin Crossca860ac2016-01-04 14:34:37 -0800590 c.begin(ctx)
Dan Albert7e9d2952016-08-04 13:02:36 -0700591}
592
Colin Cross1e676be2016-10-12 14:38:15 -0700593func (c *Module) DepsMutator(actx android.BottomUpMutatorContext) {
594 if !c.Enabled() {
595 return
596 }
597
Dan Albert7e9d2952016-08-04 13:02:36 -0700598 ctx := &baseModuleContext{
599 BaseContext: actx,
600 moduleContextImpl: moduleContextImpl{
601 mod: c,
602 },
603 }
604 ctx.ctx = ctx
Colin Crossca860ac2016-01-04 14:34:37 -0800605
Colin Crossc99deeb2016-04-11 15:06:20 -0700606 deps := c.deps(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800607
Colin Crossb5bc4b42016-07-11 16:11:59 -0700608 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.SharedLibs...)
609 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.LateSharedLibs...)
Dan Willemsen72d39932016-07-08 23:23:48 -0700610
Dan Albert914449f2016-06-17 16:45:24 -0700611 variantNdkLibs := []string{}
612 variantLateNdkLibs := []string{}
Dan Willemsend2ede872016-11-18 14:54:24 -0800613 if ctx.sdk() || ctx.vndk() {
Dan Albert914449f2016-06-17 16:45:24 -0700614 version := ctx.sdkVersion()
Dan Willemsen72d39932016-07-08 23:23:48 -0700615
Dan Albert914449f2016-06-17 16:45:24 -0700616 // Rewrites the names of shared libraries into the names of the NDK
617 // libraries where appropriate. This returns two slices.
618 //
619 // The first is a list of non-variant shared libraries (either rewritten
620 // NDK libraries to the modules in prebuilts/ndk, or not rewritten
621 // because they are not NDK libraries).
622 //
623 // The second is a list of ndk_library modules. These need to be
624 // separated because they are a variation dependency and must be added
625 // in a different manner.
626 rewriteNdkLibs := func(list []string) ([]string, []string) {
627 variantLibs := []string{}
628 nonvariantLibs := []string{}
629 for _, entry := range list {
Dan Willemsen72d39932016-07-08 23:23:48 -0700630 if inList(entry, ndkPrebuiltSharedLibraries) {
Dan Albert914449f2016-06-17 16:45:24 -0700631 if !inList(entry, ndkMigratedLibs) {
632 nonvariantLibs = append(nonvariantLibs, entry+".ndk."+version)
633 } else {
634 variantLibs = append(variantLibs, entry+ndkLibrarySuffix)
635 }
636 } else {
637 nonvariantLibs = append(variantLibs, entry)
Dan Willemsen72d39932016-07-08 23:23:48 -0700638 }
639 }
Dan Albert914449f2016-06-17 16:45:24 -0700640 return nonvariantLibs, variantLibs
Dan Willemsen72d39932016-07-08 23:23:48 -0700641 }
642
Dan Albert914449f2016-06-17 16:45:24 -0700643 deps.SharedLibs, variantNdkLibs = rewriteNdkLibs(deps.SharedLibs)
644 deps.LateSharedLibs, variantLateNdkLibs = rewriteNdkLibs(deps.LateSharedLibs)
Dan Willemsen72d39932016-07-08 23:23:48 -0700645 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700646
647 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, wholeStaticDepTag,
648 deps.WholeStaticLibs...)
649
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700650 for _, lib := range deps.StaticLibs {
651 depTag := staticDepTag
652 if inList(lib, deps.ReexportStaticLibHeaders) {
653 depTag = staticExportDepTag
654 }
Colin Cross15a0d462016-07-14 14:49:58 -0700655 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700656 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700657
658 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, lateStaticDepTag,
659 deps.LateStaticLibs...)
660
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700661 for _, lib := range deps.SharedLibs {
662 depTag := sharedDepTag
663 if inList(lib, deps.ReexportSharedLibHeaders) {
664 depTag = sharedExportDepTag
665 }
Colin Cross15a0d462016-07-14 14:49:58 -0700666 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700667 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700668
669 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, lateSharedDepTag,
670 deps.LateSharedLibs...)
671
Colin Cross68861832016-07-08 10:41:41 -0700672 actx.AddDependency(c, genSourceDepTag, deps.GeneratedSources...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700673
674 for _, gen := range deps.GeneratedHeaders {
675 depTag := genHeaderDepTag
676 if inList(gen, deps.ReexportGeneratedHeaders) {
677 depTag = genHeaderExportDepTag
678 }
679 actx.AddDependency(c, depTag, gen)
680 }
Dan Willemsenb40aab62016-04-20 14:21:14 -0700681
Colin Cross68861832016-07-08 10:41:41 -0700682 actx.AddDependency(c, objDepTag, deps.ObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700683
684 if deps.CrtBegin != "" {
Colin Cross68861832016-07-08 10:41:41 -0700685 actx.AddDependency(c, crtBeginDepTag, deps.CrtBegin)
Colin Crossca860ac2016-01-04 14:34:37 -0800686 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700687 if deps.CrtEnd != "" {
Colin Cross68861832016-07-08 10:41:41 -0700688 actx.AddDependency(c, crtEndDepTag, deps.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700689 }
Dan Albert914449f2016-06-17 16:45:24 -0700690
691 version := ctx.sdkVersion()
692 actx.AddVariationDependencies([]blueprint.Variation{
693 {"ndk_api", version}, {"link", "shared"}}, ndkStubDepTag, variantNdkLibs...)
694 actx.AddVariationDependencies([]blueprint.Variation{
695 {"ndk_api", version}, {"link", "shared"}}, ndkLateStubDepTag, variantLateNdkLibs...)
Colin Cross6362e272015-10-29 15:25:03 -0700696}
Colin Cross21b9a242015-03-24 14:15:58 -0700697
Dan Albert7e9d2952016-08-04 13:02:36 -0700698func beginMutator(ctx android.BottomUpMutatorContext) {
699 if c, ok := ctx.Module().(*Module); ok && c.Enabled() {
700 c.beginMutator(ctx)
701 }
702}
703
Colin Crossca860ac2016-01-04 14:34:37 -0800704func (c *Module) clang(ctx BaseModuleContext) bool {
705 clang := Bool(c.Properties.Clang)
706
707 if c.Properties.Clang == nil {
708 if ctx.Host() {
709 clang = true
710 }
711
712 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
713 clang = true
714 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800715 }
Colin Cross28344522015-04-22 13:07:53 -0700716
Colin Crossca860ac2016-01-04 14:34:37 -0800717 if !c.toolchain(ctx).ClangSupported() {
718 clang = false
719 }
720
721 return clang
722}
723
Colin Crossc99deeb2016-04-11 15:06:20 -0700724// Convert dependencies to paths. Returns a PathDeps containing paths
Colin Cross635c3b02016-05-18 15:37:25 -0700725func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
Colin Crossca860ac2016-01-04 14:34:37 -0800726 var depPaths PathDeps
Colin Crossca860ac2016-01-04 14:34:37 -0800727
Dan Willemsena96ff642016-06-07 12:34:45 -0700728 // Whether a module can link to another module, taking into
729 // account NDK linking.
Dan Albert9e10cd42016-08-03 14:12:14 -0700730 checkLinkType := func(from, to *Module) {
Dan Willemsena96ff642016-06-07 12:34:45 -0700731 if from.Target().Os != android.Android {
732 // Host code is not restricted
Dan Albert9e10cd42016-08-03 14:12:14 -0700733 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700734 }
735 if from.Properties.Sdk_version == "" {
736 // Platform code can link to anything
Dan Albert9e10cd42016-08-03 14:12:14 -0700737 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700738 }
Colin Crossb916a382016-07-29 17:28:03 -0700739 if _, ok := to.linker.(*toolchainLibraryDecorator); ok {
Dan Willemsena96ff642016-06-07 12:34:45 -0700740 // These are always allowed
Dan Albert9e10cd42016-08-03 14:12:14 -0700741 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700742 }
743 if _, ok := to.linker.(*ndkPrebuiltLibraryLinker); ok {
744 // These are allowed, but don't set sdk_version
Dan Albert9e10cd42016-08-03 14:12:14 -0700745 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700746 }
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700747 if _, ok := to.linker.(*ndkPrebuiltStlLinker); ok {
748 // These are allowed, but don't set sdk_version
Dan Albert9e10cd42016-08-03 14:12:14 -0700749 return
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700750 }
Colin Crossb916a382016-07-29 17:28:03 -0700751 if _, ok := to.linker.(*stubDecorator); ok {
Dan Albert914449f2016-06-17 16:45:24 -0700752 // These aren't real libraries, but are the stub shared libraries that are included in
753 // the NDK.
Dan Albert9e10cd42016-08-03 14:12:14 -0700754 return
Dan Albert914449f2016-06-17 16:45:24 -0700755 }
Dan Albert9e10cd42016-08-03 14:12:14 -0700756 if to.Properties.Sdk_version == "" {
757 // NDK code linking to platform code is never okay.
758 ctx.ModuleErrorf("depends on non-NDK-built library %q",
759 ctx.OtherModuleName(to))
760 }
761
762 // All this point we know we have two NDK libraries, but we need to
763 // check that we're not linking against anything built against a higher
764 // API level, as it is only valid to link against older or equivalent
765 // APIs.
766
767 if from.Properties.Sdk_version == "current" {
768 // Current can link against anything.
769 return
770 } else if to.Properties.Sdk_version == "current" {
771 // Current can't be linked against by anything else.
772 ctx.ModuleErrorf("links %q built against newer API version %q",
773 ctx.OtherModuleName(to), "current")
774 }
775
776 fromApi, err := strconv.Atoi(from.Properties.Sdk_version)
777 if err != nil {
778 ctx.PropertyErrorf("sdk_version",
779 "Invalid sdk_version value (must be int): %q",
780 from.Properties.Sdk_version)
781 }
782 toApi, err := strconv.Atoi(to.Properties.Sdk_version)
783 if err != nil {
784 ctx.PropertyErrorf("sdk_version",
785 "Invalid sdk_version value (must be int): %q",
786 to.Properties.Sdk_version)
787 }
788
789 if toApi > fromApi {
790 ctx.ModuleErrorf("links %q built against newer API version %q",
791 ctx.OtherModuleName(to), to.Properties.Sdk_version)
792 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700793 }
794
Colin Crossc99deeb2016-04-11 15:06:20 -0700795 ctx.VisitDirectDeps(func(m blueprint.Module) {
796 name := ctx.OtherModuleName(m)
797 tag := ctx.OtherModuleDependencyTag(m)
Colin Crossca860ac2016-01-04 14:34:37 -0800798
Colin Cross635c3b02016-05-18 15:37:25 -0700799 a, _ := m.(android.Module)
Colin Crossc99deeb2016-04-11 15:06:20 -0700800 if a == nil {
801 ctx.ModuleErrorf("module %q not an android module", name)
802 return
Colin Crossca860ac2016-01-04 14:34:37 -0800803 }
Colin Crossca860ac2016-01-04 14:34:37 -0800804
Dan Willemsena96ff642016-06-07 12:34:45 -0700805 cc, _ := m.(*Module)
806 if cc == nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700807 switch tag {
Colin Cross635c3b02016-05-18 15:37:25 -0700808 case android.DefaultsDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700809 case genSourceDepTag:
810 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
811 depPaths.GeneratedSources = append(depPaths.GeneratedSources,
812 genRule.GeneratedSourceFiles()...)
813 } else {
814 ctx.ModuleErrorf("module %q is not a gensrcs or genrule", name)
815 }
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700816 case genHeaderDepTag, genHeaderExportDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700817 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
818 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders,
819 genRule.GeneratedSourceFiles()...)
Colin Cross5ed99c62016-11-22 12:55:55 -0800820 flags := includeDirsToFlags(genRule.GeneratedHeaderDirs())
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700821 depPaths.Flags = append(depPaths.Flags, flags)
822 if tag == genHeaderExportDepTag {
823 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700824 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps,
825 genRule.GeneratedSourceFiles()...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700826 }
Dan Willemsenb40aab62016-04-20 14:21:14 -0700827 } else {
828 ctx.ModuleErrorf("module %q is not a genrule", name)
829 }
830 default:
Colin Crossc99deeb2016-04-11 15:06:20 -0700831 ctx.ModuleErrorf("depends on non-cc module %q", name)
Colin Crossca860ac2016-01-04 14:34:37 -0800832 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700833 return
834 }
835
836 if !a.Enabled() {
837 ctx.ModuleErrorf("depends on disabled module %q", name)
838 return
839 }
840
Colin Crossa1ad8d12016-06-01 17:09:44 -0700841 if a.Target().Os != ctx.Os() {
842 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), name)
843 return
844 }
845
846 if a.Target().Arch.ArchType != ctx.Arch().ArchType {
847 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700848 return
849 }
850
Colin Crossc99deeb2016-04-11 15:06:20 -0700851 if tag == reuseObjTag {
Colin Crossbba99042016-11-23 15:45:05 -0800852 if l, ok := cc.compiler.(libraryInterface); ok {
853 depPaths.Objs = depPaths.Objs.Append(l.reuseObjs())
854 return
855 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700856 }
857
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700858 if t, ok := tag.(dependencyTag); ok && t.library {
Dan Willemsena96ff642016-06-07 12:34:45 -0700859 if i, ok := cc.linker.(exportedFlagsProducer); ok {
Dan Willemsen76f08272016-07-09 00:14:08 -0700860 flags := i.exportedFlags()
Dan Willemsen847dcc72016-09-29 12:13:36 -0700861 deps := i.exportedFlagsDeps()
Dan Willemsen76f08272016-07-09 00:14:08 -0700862 depPaths.Flags = append(depPaths.Flags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700863 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders, deps...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700864
865 if t.reexportFlags {
Dan Willemsen76f08272016-07-09 00:14:08 -0700866 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700867 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps, deps...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700868 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700869 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700870
Dan Albert9e10cd42016-08-03 14:12:14 -0700871 checkLinkType(c, cc)
Colin Crossc99deeb2016-04-11 15:06:20 -0700872 }
873
Colin Cross26c34ed2016-09-30 17:10:16 -0700874 var ptr *android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -0700875 var depPtr *android.Paths
Colin Crossc99deeb2016-04-11 15:06:20 -0700876
Colin Cross26c34ed2016-09-30 17:10:16 -0700877 linkFile := cc.outputFile
878 depFile := android.OptionalPath{}
879
Colin Crossc99deeb2016-04-11 15:06:20 -0700880 switch tag {
Dan Albert914449f2016-06-17 16:45:24 -0700881 case ndkStubDepTag, sharedDepTag, sharedExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700882 ptr = &depPaths.SharedLibs
883 depPtr = &depPaths.SharedLibsDeps
884 depFile = cc.linker.(libraryInterface).toc()
Dan Albert914449f2016-06-17 16:45:24 -0700885 case lateSharedDepTag, ndkLateStubDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700886 ptr = &depPaths.LateSharedLibs
887 depPtr = &depPaths.LateSharedLibsDeps
888 depFile = cc.linker.(libraryInterface).toc()
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700889 case staticDepTag, staticExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700890 ptr = &depPaths.StaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -0700891 case lateStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700892 ptr = &depPaths.LateStaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -0700893 case wholeStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700894 ptr = &depPaths.WholeStaticLibs
Colin Crossb916a382016-07-29 17:28:03 -0700895 staticLib, ok := cc.linker.(libraryInterface)
896 if !ok || !staticLib.static() {
Dan Willemsena96ff642016-06-07 12:34:45 -0700897 ctx.ModuleErrorf("module %q not a static library", name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700898 return
899 }
900
901 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
902 postfix := " (required by " + ctx.OtherModuleName(m) + ")"
903 for i := range missingDeps {
904 missingDeps[i] += postfix
905 }
906 ctx.AddMissingDependencies(missingDeps)
907 }
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700908 depPaths.WholeStaticLibObjs = depPaths.WholeStaticLibObjs.Append(staticLib.objs())
Colin Crossc99deeb2016-04-11 15:06:20 -0700909 case objDepTag:
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700910 depPaths.Objs.objFiles = append(depPaths.Objs.objFiles, linkFile.Path())
Colin Crossc99deeb2016-04-11 15:06:20 -0700911 case crtBeginDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700912 depPaths.CrtBegin = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -0700913 case crtEndDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700914 depPaths.CrtEnd = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -0700915 }
916
Colin Cross26c34ed2016-09-30 17:10:16 -0700917 if ptr != nil {
Colin Crossce75d2c2016-10-06 16:12:58 -0700918 if !linkFile.Valid() {
919 ctx.ModuleErrorf("module %q missing output file", name)
920 return
921 }
Colin Cross26c34ed2016-09-30 17:10:16 -0700922 *ptr = append(*ptr, linkFile.Path())
923 }
924
Colin Crossc99deeb2016-04-11 15:06:20 -0700925 if depPtr != nil {
Colin Cross26c34ed2016-09-30 17:10:16 -0700926 dep := depFile
927 if !dep.Valid() {
928 dep = linkFile
929 }
930 *depPtr = append(*depPtr, dep.Path())
Colin Crossca860ac2016-01-04 14:34:37 -0800931 }
932 })
933
934 return depPaths
935}
936
937func (c *Module) InstallInData() bool {
938 if c.installer == nil {
939 return false
940 }
Colin Cross94610402016-08-29 13:41:32 -0700941 if c.sanitize != nil && c.sanitize.inData() {
942 return true
943 }
Colin Crossca860ac2016-01-04 14:34:37 -0800944 return c.installer.inData()
945}
946
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700947func (c *Module) HostToolPath() android.OptionalPath {
948 if c.installer == nil {
949 return android.OptionalPath{}
950 }
951 return c.installer.hostToolPath()
952}
953
Colin Cross2ba19d92015-05-07 15:44:20 -0700954//
Colin Crosscfad1192015-11-02 16:43:11 -0800955// Defaults
956//
Colin Crossca860ac2016-01-04 14:34:37 -0800957type Defaults struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700958 android.ModuleBase
959 android.DefaultsModule
Colin Crosscfad1192015-11-02 16:43:11 -0800960}
961
Colin Cross635c3b02016-05-18 15:37:25 -0700962func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -0800963}
964
Colin Cross1e676be2016-10-12 14:38:15 -0700965func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
966}
967
Colin Crossca860ac2016-01-04 14:34:37 -0800968func defaultsFactory() (blueprint.Module, []interface{}) {
Colin Crosse1d764e2016-08-18 14:18:32 -0700969 return DefaultsFactory()
970}
971
972func DefaultsFactory(props ...interface{}) (blueprint.Module, []interface{}) {
Colin Crossca860ac2016-01-04 14:34:37 -0800973 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -0800974
Colin Crosse1d764e2016-08-18 14:18:32 -0700975 props = append(props,
Colin Crossca860ac2016-01-04 14:34:37 -0800976 &BaseProperties{},
977 &BaseCompilerProperties{},
978 &BaseLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -0700979 &LibraryProperties{},
Colin Cross919281a2016-04-05 16:42:05 -0700980 &FlagExporterProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -0800981 &BinaryLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -0700982 &TestProperties{},
983 &TestBinaryProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -0800984 &UnusedProperties{},
985 &StlProperties{},
Colin Cross16b23492016-01-06 14:41:07 -0800986 &SanitizeProperties{},
Colin Cross665dce92016-04-28 14:50:03 -0700987 &StripProperties{},
Dan Willemsen7424d612016-09-01 13:45:39 -0700988 &InstallerProperties{},
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700989 &TidyProperties{},
Colin Crosse1d764e2016-08-18 14:18:32 -0700990 )
Colin Crosscfad1192015-11-02 16:43:11 -0800991
Colin Crosse1d764e2016-08-18 14:18:32 -0700992 return android.InitDefaultsModule(module, module, props...)
Colin Crosscfad1192015-11-02 16:43:11 -0800993}
994
Colin Cross74d1ec02015-04-28 13:30:13 -0700995// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
996// modifies the slice contents in place, and returns a subslice of the original slice
997func lastUniqueElements(list []string) []string {
998 totalSkip := 0
999 for i := len(list) - 1; i >= totalSkip; i-- {
1000 skip := 0
1001 for j := i - 1; j >= totalSkip; j-- {
1002 if list[i] == list[j] {
1003 skip++
1004 } else {
1005 list[j+skip] = list[j]
1006 }
1007 }
1008 totalSkip += skip
1009 }
1010 return list[totalSkip:]
1011}
Colin Cross06a931b2015-10-28 17:23:31 -07001012
1013var Bool = proptools.Bool