blob: 9062b00275f1c931657d791569f5b9ba333617fa [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
Colin Cross28344522015-04-22 13:07:53 -0700101 LdFlags []string // Flags that apply to linker command lines
Colin Cross16b23492016-01-06 14:41:07 -0800102 libFlags []string // Flags to add libraries early to the link order
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700103 TidyFlags []string // Flags that apply to clang-tidy
Colin Cross28344522015-04-22 13:07:53 -0700104
Colin Crossb98c8b02016-07-29 13:44:28 -0700105 Toolchain config.Toolchain
Colin Cross28344522015-04-22 13:07:53 -0700106 Clang bool
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700107 Tidy bool
Colin Crossca860ac2016-01-04 14:34:37 -0800108
109 RequiredInstructionSet string
Colin Cross16b23492016-01-06 14:41:07 -0800110 DynamicLinker string
111
Colin Cross635c3b02016-05-18 15:37:25 -0700112 CFlagsDeps android.Paths // Files depended on by compiler flags
Colin Crossc472d572015-03-17 15:06:21 -0700113}
114
Colin Cross81413472016-04-11 14:37:39 -0700115type ObjectLinkerProperties struct {
116 // names of other cc_object modules to link into this module using partial linking
117 Objs []string `android:"arch_variant"`
118}
119
Colin Crossca860ac2016-01-04 14:34:37 -0800120// Properties used to compile all C or C++ modules
121type BaseProperties struct {
122 // compile module with clang instead of gcc
123 Clang *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700124
125 // Minimum sdk version supported when compiling against the ndk
126 Sdk_version string
127
Dan Willemsend2ede872016-11-18 14:54:24 -0800128 // Whether to compile against the VNDK
129 Use_vndk bool
130
Colin Crossca860ac2016-01-04 14:34:37 -0800131 // don't insert default compiler flags into asflags, cflags,
132 // cppflags, conlyflags, ldflags, or include_dirs
133 No_default_compiler_flags *bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700134
135 AndroidMkSharedLibs []string `blueprint:"mutated"`
Colin Crossbc6fb162016-05-24 15:39:04 -0700136 HideFromMake bool `blueprint:"mutated"`
Colin Crossb0f28952016-09-19 16:46:53 -0700137 PreventInstall bool `blueprint:"mutated"`
Dan Willemsend2ede872016-11-18 14:54:24 -0800138 Vndk_version string `blueprint:"mutated"`
Colin Crossca860ac2016-01-04 14:34:37 -0800139}
140
Colin Crossca860ac2016-01-04 14:34:37 -0800141type UnusedProperties struct {
Colin Cross21b481b2016-04-15 16:27:17 -0700142 Native_coverage *bool
Colin Cross21b481b2016-04-15 16:27:17 -0700143 Tags []string
Colin Crosscfad1192015-11-02 16:43:11 -0800144}
145
Colin Crossca860ac2016-01-04 14:34:37 -0800146type ModuleContextIntf interface {
Colin Crossca860ac2016-01-04 14:34:37 -0800147 static() bool
148 staticBinary() bool
149 clang() bool
Colin Crossb98c8b02016-07-29 13:44:28 -0700150 toolchain() config.Toolchain
Colin Crossca860ac2016-01-04 14:34:37 -0800151 noDefaultCompilerFlags() bool
152 sdk() bool
153 sdkVersion() string
Dan Willemsend2ede872016-11-18 14:54:24 -0800154 vndk() bool
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700155 selectedStl() string
Colin Crossce75d2c2016-10-06 16:12:58 -0700156 baseModuleName() string
Colin Crossca860ac2016-01-04 14:34:37 -0800157}
158
159type ModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700160 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800161 ModuleContextIntf
162}
163
164type BaseModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700165 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800166 ModuleContextIntf
167}
168
Colin Crossca860ac2016-01-04 14:34:37 -0800169type feature interface {
170 begin(ctx BaseModuleContext)
171 deps(ctx BaseModuleContext, deps Deps) Deps
172 flags(ctx ModuleContext, flags Flags) Flags
173 props() []interface{}
174}
175
176type compiler interface {
Colin Cross42742b82016-08-01 13:20:05 -0700177 compilerInit(ctx BaseModuleContext)
178 compilerDeps(ctx BaseModuleContext, deps Deps) Deps
179 compilerFlags(ctx ModuleContext, flags Flags) Flags
180 compilerProps() []interface{}
181
Colin Cross76fada02016-07-27 10:31:13 -0700182 appendCflags([]string)
183 appendAsflags([]string)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700184 compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects
Colin Crossca860ac2016-01-04 14:34:37 -0800185}
186
187type linker interface {
Colin Cross42742b82016-08-01 13:20:05 -0700188 linkerInit(ctx BaseModuleContext)
189 linkerDeps(ctx BaseModuleContext, deps Deps) Deps
190 linkerFlags(ctx ModuleContext, flags Flags) Flags
191 linkerProps() []interface{}
192
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700193 link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path
Colin Cross76fada02016-07-27 10:31:13 -0700194 appendLdflags([]string)
Colin Crossca860ac2016-01-04 14:34:37 -0800195}
196
197type installer interface {
Colin Cross42742b82016-08-01 13:20:05 -0700198 installerProps() []interface{}
Colin Cross635c3b02016-05-18 15:37:25 -0700199 install(ctx ModuleContext, path android.Path)
Colin Crossca860ac2016-01-04 14:34:37 -0800200 inData() bool
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700201 hostToolPath() android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800202}
203
Colin Crossc99deeb2016-04-11 15:06:20 -0700204type dependencyTag struct {
205 blueprint.BaseDependencyTag
206 name string
207 library bool
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700208
209 reexportFlags bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700210}
211
212var (
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700213 sharedDepTag = dependencyTag{name: "shared", library: true}
214 sharedExportDepTag = dependencyTag{name: "shared", library: true, reexportFlags: true}
215 lateSharedDepTag = dependencyTag{name: "late shared", library: true}
216 staticDepTag = dependencyTag{name: "static", library: true}
217 staticExportDepTag = dependencyTag{name: "static", library: true, reexportFlags: true}
218 lateStaticDepTag = dependencyTag{name: "late static", library: true}
219 wholeStaticDepTag = dependencyTag{name: "whole static", library: true, reexportFlags: true}
220 genSourceDepTag = dependencyTag{name: "gen source"}
221 genHeaderDepTag = dependencyTag{name: "gen header"}
222 genHeaderExportDepTag = dependencyTag{name: "gen header", reexportFlags: true}
223 objDepTag = dependencyTag{name: "obj"}
224 crtBeginDepTag = dependencyTag{name: "crtbegin"}
225 crtEndDepTag = dependencyTag{name: "crtend"}
226 reuseObjTag = dependencyTag{name: "reuse objects"}
227 ndkStubDepTag = dependencyTag{name: "ndk stub", library: true}
228 ndkLateStubDepTag = dependencyTag{name: "ndk late stub", library: true}
Colin Crossc99deeb2016-04-11 15:06:20 -0700229)
230
Colin Crossca860ac2016-01-04 14:34:37 -0800231// Module contains the properties and members used by all C/C++ module types, and implements
232// the blueprint.Module interface. It delegates to compiler, linker, and installer interfaces
233// to construct the output file. Behavior can be customized with a Customizer interface
234type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700235 android.ModuleBase
236 android.DefaultableModule
Colin Crossc472d572015-03-17 15:06:21 -0700237
Colin Crossca860ac2016-01-04 14:34:37 -0800238 Properties BaseProperties
239 unused UnusedProperties
Colin Crossfa138792015-04-24 17:31:52 -0700240
Colin Crossca860ac2016-01-04 14:34:37 -0800241 // initialize before calling Init
Colin Cross635c3b02016-05-18 15:37:25 -0700242 hod android.HostOrDeviceSupported
243 multilib android.Multilib
Colin Crossc472d572015-03-17 15:06:21 -0700244
Colin Crossca860ac2016-01-04 14:34:37 -0800245 // delegates, initialize before calling Init
Colin Crossb4ce0ec2016-09-13 13:41:39 -0700246 features []feature
247 compiler compiler
248 linker linker
249 installer installer
250 stl *stl
251 sanitize *sanitize
Colin Cross16b23492016-01-06 14:41:07 -0800252
253 androidMkSharedLibDeps []string
Colin Cross74d1ec02015-04-28 13:30:13 -0700254
Colin Cross635c3b02016-05-18 15:37:25 -0700255 outputFile android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800256
Colin Crossb98c8b02016-07-29 13:44:28 -0700257 cachedToolchain config.Toolchain
Colin Crossb916a382016-07-29 17:28:03 -0700258
259 subAndroidMkOnce map[subAndroidMkProvider]bool
Colin Crossc472d572015-03-17 15:06:21 -0700260}
261
Colin Crossca860ac2016-01-04 14:34:37 -0800262func (c *Module) Init() (blueprint.Module, []interface{}) {
263 props := []interface{}{&c.Properties, &c.unused}
Colin Crossca860ac2016-01-04 14:34:37 -0800264 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700265 props = append(props, c.compiler.compilerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800266 }
267 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700268 props = append(props, c.linker.linkerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800269 }
270 if c.installer != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700271 props = append(props, c.installer.installerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800272 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700273 if c.stl != nil {
274 props = append(props, c.stl.props()...)
275 }
Colin Cross16b23492016-01-06 14:41:07 -0800276 if c.sanitize != nil {
277 props = append(props, c.sanitize.props()...)
278 }
Colin Crossca860ac2016-01-04 14:34:37 -0800279 for _, feature := range c.features {
280 props = append(props, feature.props()...)
281 }
Colin Crossc472d572015-03-17 15:06:21 -0700282
Colin Cross635c3b02016-05-18 15:37:25 -0700283 _, props = android.InitAndroidArchModule(c, c.hod, c.multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700284
Colin Cross635c3b02016-05-18 15:37:25 -0700285 return android.InitDefaultableModule(c, c, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700286}
287
Colin Crossb916a382016-07-29 17:28:03 -0700288// Returns true for dependency roots (binaries)
289// TODO(ccross): also handle dlopenable libraries
290func (c *Module) isDependencyRoot() bool {
291 if root, ok := c.linker.(interface {
292 isDependencyRoot() bool
293 }); ok {
294 return root.isDependencyRoot()
295 }
296 return false
297}
298
Colin Crossca860ac2016-01-04 14:34:37 -0800299type baseModuleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700300 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800301 moduleContextImpl
302}
303
304type moduleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700305 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800306 moduleContextImpl
307}
308
309type moduleContextImpl struct {
310 mod *Module
311 ctx BaseModuleContext
312}
313
Colin Crossca860ac2016-01-04 14:34:37 -0800314func (ctx *moduleContextImpl) clang() bool {
315 return ctx.mod.clang(ctx.ctx)
316}
317
Colin Crossb98c8b02016-07-29 13:44:28 -0700318func (ctx *moduleContextImpl) toolchain() config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800319 return ctx.mod.toolchain(ctx.ctx)
320}
321
322func (ctx *moduleContextImpl) static() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700323 if static, ok := ctx.mod.linker.(interface {
324 static() bool
325 }); ok {
326 return static.static()
Colin Crossca860ac2016-01-04 14:34:37 -0800327 }
Colin Crossb916a382016-07-29 17:28:03 -0700328 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800329}
330
331func (ctx *moduleContextImpl) staticBinary() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700332 if static, ok := ctx.mod.linker.(interface {
333 staticBinary() bool
334 }); ok {
335 return static.staticBinary()
Colin Crossca860ac2016-01-04 14:34:37 -0800336 }
Colin Crossb916a382016-07-29 17:28:03 -0700337 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800338}
339
340func (ctx *moduleContextImpl) noDefaultCompilerFlags() bool {
341 return Bool(ctx.mod.Properties.No_default_compiler_flags)
342}
343
344func (ctx *moduleContextImpl) sdk() bool {
Dan Willemsena96ff642016-06-07 12:34:45 -0700345 if ctx.ctx.Device() {
346 return ctx.mod.Properties.Sdk_version != ""
347 }
348 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800349}
350
351func (ctx *moduleContextImpl) sdkVersion() string {
Dan Willemsena96ff642016-06-07 12:34:45 -0700352 if ctx.ctx.Device() {
Dan Willemsend2ede872016-11-18 14:54:24 -0800353 if ctx.mod.Properties.Use_vndk {
354 return ctx.mod.Properties.Vndk_version
355 } else {
356 return ctx.mod.Properties.Sdk_version
357 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700358 }
359 return ""
Colin Crossca860ac2016-01-04 14:34:37 -0800360}
361
Dan Willemsend2ede872016-11-18 14:54:24 -0800362func (ctx *moduleContextImpl) vndk() bool {
363 if ctx.ctx.Device() {
364 return ctx.mod.Properties.Use_vndk
365 }
366 return false
367}
368
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700369func (ctx *moduleContextImpl) selectedStl() string {
370 if stl := ctx.mod.stl; stl != nil {
371 return stl.Properties.SelectedStl
372 }
373 return ""
374}
375
Colin Crossce75d2c2016-10-06 16:12:58 -0700376func (ctx *moduleContextImpl) baseModuleName() string {
377 return ctx.mod.ModuleBase.BaseModuleName()
378}
379
Colin Cross635c3b02016-05-18 15:37:25 -0700380func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800381 return &Module{
382 hod: hod,
383 multilib: multilib,
384 }
385}
386
Colin Cross635c3b02016-05-18 15:37:25 -0700387func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800388 module := newBaseModule(hod, multilib)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700389 module.features = []feature{
390 &tidyFeature{},
391 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700392 module.stl = &stl{}
Colin Cross16b23492016-01-06 14:41:07 -0800393 module.sanitize = &sanitize{}
Colin Crossca860ac2016-01-04 14:34:37 -0800394 return module
395}
396
Colin Crossce75d2c2016-10-06 16:12:58 -0700397func (c *Module) Prebuilt() *android.Prebuilt {
398 if p, ok := c.linker.(prebuiltLinkerInterface); ok {
399 return p.prebuilt()
400 }
401 return nil
402}
403
404func (c *Module) Name() string {
405 name := c.ModuleBase.Name()
406 if p, ok := c.linker.(prebuiltLinkerInterface); ok {
407 name = p.Name(name)
408 }
409 return name
410}
411
Colin Cross635c3b02016-05-18 15:37:25 -0700412func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800413 ctx := &moduleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700414 ModuleContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800415 moduleContextImpl: moduleContextImpl{
416 mod: c,
417 },
418 }
419 ctx.ctx = ctx
420
421 flags := Flags{
422 Toolchain: c.toolchain(ctx),
423 Clang: c.clang(ctx),
424 }
Colin Crossca860ac2016-01-04 14:34:37 -0800425 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700426 flags = c.compiler.compilerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800427 }
428 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700429 flags = c.linker.linkerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800430 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700431 if c.stl != nil {
432 flags = c.stl.flags(ctx, flags)
433 }
Colin Cross16b23492016-01-06 14:41:07 -0800434 if c.sanitize != nil {
435 flags = c.sanitize.flags(ctx, flags)
436 }
Colin Crossca860ac2016-01-04 14:34:37 -0800437 for _, feature := range c.features {
438 flags = feature.flags(ctx, flags)
439 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800440 if ctx.Failed() {
441 return
442 }
443
Colin Crossb98c8b02016-07-29 13:44:28 -0700444 flags.CFlags, _ = filterList(flags.CFlags, config.IllegalFlags)
445 flags.CppFlags, _ = filterList(flags.CppFlags, config.IllegalFlags)
446 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, config.IllegalFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800447
Colin Crossca860ac2016-01-04 14:34:37 -0800448 // Optimization to reduce size of build.ninja
449 // Replace the long list of flags for each file with a module-local variable
450 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
451 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
452 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
453 flags.CFlags = []string{"$cflags"}
454 flags.CppFlags = []string{"$cppflags"}
455 flags.AsFlags = []string{"$asflags"}
456
Colin Crossc99deeb2016-04-11 15:06:20 -0700457 deps := c.depsToPaths(ctx)
Colin Cross3f40fa42015-01-30 17:27:36 -0800458 if ctx.Failed() {
459 return
460 }
461
Dan Willemsen76f08272016-07-09 00:14:08 -0700462 flags.GlobalFlags = append(flags.GlobalFlags, deps.Flags...)
Colin Crossed9f8682015-03-18 17:17:35 -0700463
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700464 var objs Objects
Colin Crossca860ac2016-01-04 14:34:37 -0800465 if c.compiler != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700466 objs = c.compiler.compile(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -0800467 if ctx.Failed() {
468 return
469 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800470 }
471
Colin Crossca860ac2016-01-04 14:34:37 -0800472 if c.linker != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700473 outputFile := c.linker.link(ctx, flags, deps, objs)
Colin Crossca860ac2016-01-04 14:34:37 -0800474 if ctx.Failed() {
475 return
476 }
Colin Cross635c3b02016-05-18 15:37:25 -0700477 c.outputFile = android.OptionalPathForPath(outputFile)
Colin Crossce75d2c2016-10-06 16:12:58 -0700478 }
Colin Cross5049f022015-03-18 13:28:46 -0700479
Colin Crossce75d2c2016-10-06 16:12:58 -0700480 if c.installer != nil && !c.Properties.PreventInstall && c.outputFile.Valid() {
481 c.installer.install(ctx, c.outputFile.Path())
482 if ctx.Failed() {
483 return
Colin Crossca860ac2016-01-04 14:34:37 -0800484 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700485 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800486}
487
Colin Crossb98c8b02016-07-29 13:44:28 -0700488func (c *Module) toolchain(ctx BaseModuleContext) config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800489 if c.cachedToolchain == nil {
Colin Crossb98c8b02016-07-29 13:44:28 -0700490 c.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
Colin Cross3f40fa42015-01-30 17:27:36 -0800491 }
Colin Crossca860ac2016-01-04 14:34:37 -0800492 return c.cachedToolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800493}
494
Colin Crossca860ac2016-01-04 14:34:37 -0800495func (c *Module) begin(ctx BaseModuleContext) {
496 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700497 c.compiler.compilerInit(ctx)
Colin Cross21b9a242015-03-24 14:15:58 -0700498 }
Colin Crossca860ac2016-01-04 14:34:37 -0800499 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700500 c.linker.linkerInit(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800501 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700502 if c.stl != nil {
503 c.stl.begin(ctx)
504 }
Colin Cross16b23492016-01-06 14:41:07 -0800505 if c.sanitize != nil {
506 c.sanitize.begin(ctx)
507 }
Colin Crossca860ac2016-01-04 14:34:37 -0800508 for _, feature := range c.features {
509 feature.begin(ctx)
510 }
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700511 if ctx.sdk() {
Dan Willemsend2ede872016-11-18 14:54:24 -0800512 if ctx.vndk() {
513 ctx.PropertyErrorf("use_vndk",
514 "sdk_version and use_vndk cannot be used at the same time")
515 }
516
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700517 version, err := normalizeNdkApiLevel(ctx.sdkVersion(), ctx.Arch())
518 if err != nil {
519 ctx.PropertyErrorf("sdk_version", err.Error())
520 }
Dan Albert90f7a4d2016-11-08 14:34:24 -0800521 c.Properties.Sdk_version = version
Dan Willemsend2ede872016-11-18 14:54:24 -0800522 } else if ctx.vndk() {
523 version, err := normalizeNdkApiLevel(ctx.DeviceConfig().VndkVersion(), ctx.Arch())
524 if err != nil {
525 ctx.ModuleErrorf("Bad BOARD_VNDK_VERSION: %s", err.Error())
526 }
527 c.Properties.Vndk_version = version
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700528 }
Colin Crossca860ac2016-01-04 14:34:37 -0800529}
530
Colin Crossc99deeb2016-04-11 15:06:20 -0700531func (c *Module) deps(ctx BaseModuleContext) Deps {
532 deps := Deps{}
533
534 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700535 deps = c.compiler.compilerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700536 }
537 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700538 deps = c.linker.linkerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700539 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700540 if c.stl != nil {
541 deps = c.stl.deps(ctx, deps)
542 }
Colin Cross16b23492016-01-06 14:41:07 -0800543 if c.sanitize != nil {
544 deps = c.sanitize.deps(ctx, deps)
545 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700546 for _, feature := range c.features {
547 deps = feature.deps(ctx, deps)
548 }
549
550 deps.WholeStaticLibs = lastUniqueElements(deps.WholeStaticLibs)
551 deps.StaticLibs = lastUniqueElements(deps.StaticLibs)
552 deps.LateStaticLibs = lastUniqueElements(deps.LateStaticLibs)
553 deps.SharedLibs = lastUniqueElements(deps.SharedLibs)
554 deps.LateSharedLibs = lastUniqueElements(deps.LateSharedLibs)
555
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700556 for _, lib := range deps.ReexportSharedLibHeaders {
557 if !inList(lib, deps.SharedLibs) {
558 ctx.PropertyErrorf("export_shared_lib_headers", "Shared library not in shared_libs: '%s'", lib)
559 }
560 }
561
562 for _, lib := range deps.ReexportStaticLibHeaders {
563 if !inList(lib, deps.StaticLibs) {
564 ctx.PropertyErrorf("export_static_lib_headers", "Static library not in static_libs: '%s'", lib)
565 }
566 }
567
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700568 for _, gen := range deps.ReexportGeneratedHeaders {
569 if !inList(gen, deps.GeneratedHeaders) {
570 ctx.PropertyErrorf("export_generated_headers", "Generated header module not in generated_headers: '%s'", gen)
571 }
572 }
573
Colin Crossc99deeb2016-04-11 15:06:20 -0700574 return deps
575}
576
Dan Albert7e9d2952016-08-04 13:02:36 -0700577func (c *Module) beginMutator(actx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800578 ctx := &baseModuleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700579 BaseContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800580 moduleContextImpl: moduleContextImpl{
581 mod: c,
582 },
583 }
584 ctx.ctx = ctx
585
Colin Crossca860ac2016-01-04 14:34:37 -0800586 c.begin(ctx)
Dan Albert7e9d2952016-08-04 13:02:36 -0700587}
588
Colin Cross1e676be2016-10-12 14:38:15 -0700589func (c *Module) DepsMutator(actx android.BottomUpMutatorContext) {
590 if !c.Enabled() {
591 return
592 }
593
Dan Albert7e9d2952016-08-04 13:02:36 -0700594 ctx := &baseModuleContext{
595 BaseContext: actx,
596 moduleContextImpl: moduleContextImpl{
597 mod: c,
598 },
599 }
600 ctx.ctx = ctx
Colin Crossca860ac2016-01-04 14:34:37 -0800601
Colin Crossc99deeb2016-04-11 15:06:20 -0700602 deps := c.deps(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800603
Colin Crossb5bc4b42016-07-11 16:11:59 -0700604 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.SharedLibs...)
605 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.LateSharedLibs...)
Dan Willemsen72d39932016-07-08 23:23:48 -0700606
Dan Albert914449f2016-06-17 16:45:24 -0700607 variantNdkLibs := []string{}
608 variantLateNdkLibs := []string{}
Dan Willemsend2ede872016-11-18 14:54:24 -0800609 if ctx.sdk() || ctx.vndk() {
Dan Albert914449f2016-06-17 16:45:24 -0700610 version := ctx.sdkVersion()
Dan Willemsen72d39932016-07-08 23:23:48 -0700611
Dan Albert914449f2016-06-17 16:45:24 -0700612 // Rewrites the names of shared libraries into the names of the NDK
613 // libraries where appropriate. This returns two slices.
614 //
615 // The first is a list of non-variant shared libraries (either rewritten
616 // NDK libraries to the modules in prebuilts/ndk, or not rewritten
617 // because they are not NDK libraries).
618 //
619 // The second is a list of ndk_library modules. These need to be
620 // separated because they are a variation dependency and must be added
621 // in a different manner.
622 rewriteNdkLibs := func(list []string) ([]string, []string) {
623 variantLibs := []string{}
624 nonvariantLibs := []string{}
625 for _, entry := range list {
Dan Willemsen72d39932016-07-08 23:23:48 -0700626 if inList(entry, ndkPrebuiltSharedLibraries) {
Dan Albert914449f2016-06-17 16:45:24 -0700627 if !inList(entry, ndkMigratedLibs) {
628 nonvariantLibs = append(nonvariantLibs, entry+".ndk."+version)
629 } else {
630 variantLibs = append(variantLibs, entry+ndkLibrarySuffix)
631 }
632 } else {
633 nonvariantLibs = append(variantLibs, entry)
Dan Willemsen72d39932016-07-08 23:23:48 -0700634 }
635 }
Dan Albert914449f2016-06-17 16:45:24 -0700636 return nonvariantLibs, variantLibs
Dan Willemsen72d39932016-07-08 23:23:48 -0700637 }
638
Dan Albert914449f2016-06-17 16:45:24 -0700639 deps.SharedLibs, variantNdkLibs = rewriteNdkLibs(deps.SharedLibs)
640 deps.LateSharedLibs, variantLateNdkLibs = rewriteNdkLibs(deps.LateSharedLibs)
Dan Willemsen72d39932016-07-08 23:23:48 -0700641 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700642
643 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, wholeStaticDepTag,
644 deps.WholeStaticLibs...)
645
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700646 for _, lib := range deps.StaticLibs {
647 depTag := staticDepTag
648 if inList(lib, deps.ReexportStaticLibHeaders) {
649 depTag = staticExportDepTag
650 }
Colin Cross15a0d462016-07-14 14:49:58 -0700651 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700652 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700653
654 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, lateStaticDepTag,
655 deps.LateStaticLibs...)
656
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700657 for _, lib := range deps.SharedLibs {
658 depTag := sharedDepTag
659 if inList(lib, deps.ReexportSharedLibHeaders) {
660 depTag = sharedExportDepTag
661 }
Colin Cross15a0d462016-07-14 14:49:58 -0700662 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700663 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700664
665 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, lateSharedDepTag,
666 deps.LateSharedLibs...)
667
Colin Cross68861832016-07-08 10:41:41 -0700668 actx.AddDependency(c, genSourceDepTag, deps.GeneratedSources...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700669
670 for _, gen := range deps.GeneratedHeaders {
671 depTag := genHeaderDepTag
672 if inList(gen, deps.ReexportGeneratedHeaders) {
673 depTag = genHeaderExportDepTag
674 }
675 actx.AddDependency(c, depTag, gen)
676 }
Dan Willemsenb40aab62016-04-20 14:21:14 -0700677
Colin Cross68861832016-07-08 10:41:41 -0700678 actx.AddDependency(c, objDepTag, deps.ObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700679
680 if deps.CrtBegin != "" {
Colin Cross68861832016-07-08 10:41:41 -0700681 actx.AddDependency(c, crtBeginDepTag, deps.CrtBegin)
Colin Crossca860ac2016-01-04 14:34:37 -0800682 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700683 if deps.CrtEnd != "" {
Colin Cross68861832016-07-08 10:41:41 -0700684 actx.AddDependency(c, crtEndDepTag, deps.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700685 }
Dan Albert914449f2016-06-17 16:45:24 -0700686
687 version := ctx.sdkVersion()
688 actx.AddVariationDependencies([]blueprint.Variation{
689 {"ndk_api", version}, {"link", "shared"}}, ndkStubDepTag, variantNdkLibs...)
690 actx.AddVariationDependencies([]blueprint.Variation{
691 {"ndk_api", version}, {"link", "shared"}}, ndkLateStubDepTag, variantLateNdkLibs...)
Colin Cross6362e272015-10-29 15:25:03 -0700692}
Colin Cross21b9a242015-03-24 14:15:58 -0700693
Dan Albert7e9d2952016-08-04 13:02:36 -0700694func beginMutator(ctx android.BottomUpMutatorContext) {
695 if c, ok := ctx.Module().(*Module); ok && c.Enabled() {
696 c.beginMutator(ctx)
697 }
698}
699
Colin Crossca860ac2016-01-04 14:34:37 -0800700func (c *Module) clang(ctx BaseModuleContext) bool {
701 clang := Bool(c.Properties.Clang)
702
703 if c.Properties.Clang == nil {
704 if ctx.Host() {
705 clang = true
706 }
707
708 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
709 clang = true
710 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800711 }
Colin Cross28344522015-04-22 13:07:53 -0700712
Colin Crossca860ac2016-01-04 14:34:37 -0800713 if !c.toolchain(ctx).ClangSupported() {
714 clang = false
715 }
716
717 return clang
718}
719
Colin Crossc99deeb2016-04-11 15:06:20 -0700720// Convert dependencies to paths. Returns a PathDeps containing paths
Colin Cross635c3b02016-05-18 15:37:25 -0700721func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
Colin Crossca860ac2016-01-04 14:34:37 -0800722 var depPaths PathDeps
Colin Crossca860ac2016-01-04 14:34:37 -0800723
Dan Willemsena96ff642016-06-07 12:34:45 -0700724 // Whether a module can link to another module, taking into
725 // account NDK linking.
Dan Albert9e10cd42016-08-03 14:12:14 -0700726 checkLinkType := func(from, to *Module) {
Dan Willemsena96ff642016-06-07 12:34:45 -0700727 if from.Target().Os != android.Android {
728 // Host code is not restricted
Dan Albert9e10cd42016-08-03 14:12:14 -0700729 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700730 }
731 if from.Properties.Sdk_version == "" {
732 // Platform code can link to anything
Dan Albert9e10cd42016-08-03 14:12:14 -0700733 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700734 }
Colin Crossb916a382016-07-29 17:28:03 -0700735 if _, ok := to.linker.(*toolchainLibraryDecorator); ok {
Dan Willemsena96ff642016-06-07 12:34:45 -0700736 // These are always allowed
Dan Albert9e10cd42016-08-03 14:12:14 -0700737 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700738 }
739 if _, ok := to.linker.(*ndkPrebuiltLibraryLinker); ok {
740 // These are allowed, but don't set sdk_version
Dan Albert9e10cd42016-08-03 14:12:14 -0700741 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700742 }
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700743 if _, ok := to.linker.(*ndkPrebuiltStlLinker); ok {
744 // These are allowed, but don't set sdk_version
Dan Albert9e10cd42016-08-03 14:12:14 -0700745 return
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700746 }
Colin Crossb916a382016-07-29 17:28:03 -0700747 if _, ok := to.linker.(*stubDecorator); ok {
Dan Albert914449f2016-06-17 16:45:24 -0700748 // These aren't real libraries, but are the stub shared libraries that are included in
749 // the NDK.
Dan Albert9e10cd42016-08-03 14:12:14 -0700750 return
Dan Albert914449f2016-06-17 16:45:24 -0700751 }
Dan Albert9e10cd42016-08-03 14:12:14 -0700752 if to.Properties.Sdk_version == "" {
753 // NDK code linking to platform code is never okay.
754 ctx.ModuleErrorf("depends on non-NDK-built library %q",
755 ctx.OtherModuleName(to))
756 }
757
758 // All this point we know we have two NDK libraries, but we need to
759 // check that we're not linking against anything built against a higher
760 // API level, as it is only valid to link against older or equivalent
761 // APIs.
762
763 if from.Properties.Sdk_version == "current" {
764 // Current can link against anything.
765 return
766 } else if to.Properties.Sdk_version == "current" {
767 // Current can't be linked against by anything else.
768 ctx.ModuleErrorf("links %q built against newer API version %q",
769 ctx.OtherModuleName(to), "current")
770 }
771
772 fromApi, err := strconv.Atoi(from.Properties.Sdk_version)
773 if err != nil {
774 ctx.PropertyErrorf("sdk_version",
775 "Invalid sdk_version value (must be int): %q",
776 from.Properties.Sdk_version)
777 }
778 toApi, err := strconv.Atoi(to.Properties.Sdk_version)
779 if err != nil {
780 ctx.PropertyErrorf("sdk_version",
781 "Invalid sdk_version value (must be int): %q",
782 to.Properties.Sdk_version)
783 }
784
785 if toApi > fromApi {
786 ctx.ModuleErrorf("links %q built against newer API version %q",
787 ctx.OtherModuleName(to), to.Properties.Sdk_version)
788 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700789 }
790
Colin Crossc99deeb2016-04-11 15:06:20 -0700791 ctx.VisitDirectDeps(func(m blueprint.Module) {
792 name := ctx.OtherModuleName(m)
793 tag := ctx.OtherModuleDependencyTag(m)
Colin Crossca860ac2016-01-04 14:34:37 -0800794
Colin Cross635c3b02016-05-18 15:37:25 -0700795 a, _ := m.(android.Module)
Colin Crossc99deeb2016-04-11 15:06:20 -0700796 if a == nil {
797 ctx.ModuleErrorf("module %q not an android module", name)
798 return
Colin Crossca860ac2016-01-04 14:34:37 -0800799 }
Colin Crossca860ac2016-01-04 14:34:37 -0800800
Dan Willemsena96ff642016-06-07 12:34:45 -0700801 cc, _ := m.(*Module)
802 if cc == nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700803 switch tag {
Colin Cross635c3b02016-05-18 15:37:25 -0700804 case android.DefaultsDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700805 case genSourceDepTag:
806 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
807 depPaths.GeneratedSources = append(depPaths.GeneratedSources,
808 genRule.GeneratedSourceFiles()...)
809 } else {
810 ctx.ModuleErrorf("module %q is not a gensrcs or genrule", name)
811 }
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700812 case genHeaderDepTag, genHeaderExportDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700813 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
814 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders,
815 genRule.GeneratedSourceFiles()...)
Colin Cross5ed99c62016-11-22 12:55:55 -0800816 flags := includeDirsToFlags(genRule.GeneratedHeaderDirs())
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700817 depPaths.Flags = append(depPaths.Flags, flags)
818 if tag == genHeaderExportDepTag {
819 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700820 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps,
821 genRule.GeneratedSourceFiles()...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700822 }
Dan Willemsenb40aab62016-04-20 14:21:14 -0700823 } else {
824 ctx.ModuleErrorf("module %q is not a genrule", name)
825 }
826 default:
Colin Crossc99deeb2016-04-11 15:06:20 -0700827 ctx.ModuleErrorf("depends on non-cc module %q", name)
Colin Crossca860ac2016-01-04 14:34:37 -0800828 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700829 return
830 }
831
832 if !a.Enabled() {
833 ctx.ModuleErrorf("depends on disabled module %q", name)
834 return
835 }
836
Colin Crossa1ad8d12016-06-01 17:09:44 -0700837 if a.Target().Os != ctx.Os() {
838 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), name)
839 return
840 }
841
842 if a.Target().Arch.ArchType != ctx.Arch().ArchType {
843 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700844 return
845 }
846
Colin Crossc99deeb2016-04-11 15:06:20 -0700847 if tag == reuseObjTag {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700848 depPaths.Objs = depPaths.Objs.Append(cc.compiler.(libraryInterface).reuseObjs())
Colin Crossc99deeb2016-04-11 15:06:20 -0700849 return
850 }
851
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700852 if t, ok := tag.(dependencyTag); ok && t.library {
Dan Willemsena96ff642016-06-07 12:34:45 -0700853 if i, ok := cc.linker.(exportedFlagsProducer); ok {
Dan Willemsen76f08272016-07-09 00:14:08 -0700854 flags := i.exportedFlags()
Dan Willemsen847dcc72016-09-29 12:13:36 -0700855 deps := i.exportedFlagsDeps()
Dan Willemsen76f08272016-07-09 00:14:08 -0700856 depPaths.Flags = append(depPaths.Flags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700857 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders, deps...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700858
859 if t.reexportFlags {
Dan Willemsen76f08272016-07-09 00:14:08 -0700860 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700861 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps, deps...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700862 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700863 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700864
Dan Albert9e10cd42016-08-03 14:12:14 -0700865 checkLinkType(c, cc)
Colin Crossc99deeb2016-04-11 15:06:20 -0700866 }
867
Colin Cross26c34ed2016-09-30 17:10:16 -0700868 var ptr *android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -0700869 var depPtr *android.Paths
Colin Crossc99deeb2016-04-11 15:06:20 -0700870
Colin Cross26c34ed2016-09-30 17:10:16 -0700871 linkFile := cc.outputFile
872 depFile := android.OptionalPath{}
873
Colin Crossc99deeb2016-04-11 15:06:20 -0700874 switch tag {
Dan Albert914449f2016-06-17 16:45:24 -0700875 case ndkStubDepTag, sharedDepTag, sharedExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700876 ptr = &depPaths.SharedLibs
877 depPtr = &depPaths.SharedLibsDeps
878 depFile = cc.linker.(libraryInterface).toc()
Dan Albert914449f2016-06-17 16:45:24 -0700879 case lateSharedDepTag, ndkLateStubDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700880 ptr = &depPaths.LateSharedLibs
881 depPtr = &depPaths.LateSharedLibsDeps
882 depFile = cc.linker.(libraryInterface).toc()
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700883 case staticDepTag, staticExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700884 ptr = &depPaths.StaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -0700885 case lateStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700886 ptr = &depPaths.LateStaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -0700887 case wholeStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700888 ptr = &depPaths.WholeStaticLibs
Colin Crossb916a382016-07-29 17:28:03 -0700889 staticLib, ok := cc.linker.(libraryInterface)
890 if !ok || !staticLib.static() {
Dan Willemsena96ff642016-06-07 12:34:45 -0700891 ctx.ModuleErrorf("module %q not a static library", name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700892 return
893 }
894
895 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
896 postfix := " (required by " + ctx.OtherModuleName(m) + ")"
897 for i := range missingDeps {
898 missingDeps[i] += postfix
899 }
900 ctx.AddMissingDependencies(missingDeps)
901 }
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700902 depPaths.WholeStaticLibObjs = depPaths.WholeStaticLibObjs.Append(staticLib.objs())
Colin Crossc99deeb2016-04-11 15:06:20 -0700903 case objDepTag:
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700904 depPaths.Objs.objFiles = append(depPaths.Objs.objFiles, linkFile.Path())
Colin Crossc99deeb2016-04-11 15:06:20 -0700905 case crtBeginDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700906 depPaths.CrtBegin = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -0700907 case crtEndDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700908 depPaths.CrtEnd = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -0700909 }
910
Colin Cross26c34ed2016-09-30 17:10:16 -0700911 if ptr != nil {
Colin Crossce75d2c2016-10-06 16:12:58 -0700912 if !linkFile.Valid() {
913 ctx.ModuleErrorf("module %q missing output file", name)
914 return
915 }
Colin Cross26c34ed2016-09-30 17:10:16 -0700916 *ptr = append(*ptr, linkFile.Path())
917 }
918
Colin Crossc99deeb2016-04-11 15:06:20 -0700919 if depPtr != nil {
Colin Cross26c34ed2016-09-30 17:10:16 -0700920 dep := depFile
921 if !dep.Valid() {
922 dep = linkFile
923 }
924 *depPtr = append(*depPtr, dep.Path())
Colin Crossca860ac2016-01-04 14:34:37 -0800925 }
926 })
927
928 return depPaths
929}
930
931func (c *Module) InstallInData() bool {
932 if c.installer == nil {
933 return false
934 }
Colin Cross94610402016-08-29 13:41:32 -0700935 if c.sanitize != nil && c.sanitize.inData() {
936 return true
937 }
Colin Crossca860ac2016-01-04 14:34:37 -0800938 return c.installer.inData()
939}
940
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700941func (c *Module) HostToolPath() android.OptionalPath {
942 if c.installer == nil {
943 return android.OptionalPath{}
944 }
945 return c.installer.hostToolPath()
946}
947
Colin Cross2ba19d92015-05-07 15:44:20 -0700948//
Colin Crosscfad1192015-11-02 16:43:11 -0800949// Defaults
950//
Colin Crossca860ac2016-01-04 14:34:37 -0800951type Defaults struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700952 android.ModuleBase
953 android.DefaultsModule
Colin Crosscfad1192015-11-02 16:43:11 -0800954}
955
Colin Cross635c3b02016-05-18 15:37:25 -0700956func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -0800957}
958
Colin Cross1e676be2016-10-12 14:38:15 -0700959func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
960}
961
Colin Crossca860ac2016-01-04 14:34:37 -0800962func defaultsFactory() (blueprint.Module, []interface{}) {
Colin Crosse1d764e2016-08-18 14:18:32 -0700963 return DefaultsFactory()
964}
965
966func DefaultsFactory(props ...interface{}) (blueprint.Module, []interface{}) {
Colin Crossca860ac2016-01-04 14:34:37 -0800967 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -0800968
Colin Crosse1d764e2016-08-18 14:18:32 -0700969 props = append(props,
Colin Crossca860ac2016-01-04 14:34:37 -0800970 &BaseProperties{},
971 &BaseCompilerProperties{},
972 &BaseLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -0700973 &LibraryProperties{},
Colin Cross919281a2016-04-05 16:42:05 -0700974 &FlagExporterProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -0800975 &BinaryLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -0700976 &TestProperties{},
977 &TestBinaryProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -0800978 &UnusedProperties{},
979 &StlProperties{},
Colin Cross16b23492016-01-06 14:41:07 -0800980 &SanitizeProperties{},
Colin Cross665dce92016-04-28 14:50:03 -0700981 &StripProperties{},
Dan Willemsen7424d612016-09-01 13:45:39 -0700982 &InstallerProperties{},
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700983 &TidyProperties{},
Colin Crosse1d764e2016-08-18 14:18:32 -0700984 )
Colin Crosscfad1192015-11-02 16:43:11 -0800985
Colin Crosse1d764e2016-08-18 14:18:32 -0700986 return android.InitDefaultsModule(module, module, props...)
Colin Crosscfad1192015-11-02 16:43:11 -0800987}
988
Colin Cross74d1ec02015-04-28 13:30:13 -0700989// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
990// modifies the slice contents in place, and returns a subslice of the original slice
991func lastUniqueElements(list []string) []string {
992 totalSkip := 0
993 for i := len(list) - 1; i >= totalSkip; i-- {
994 skip := 0
995 for j := i - 1; j >= totalSkip; j-- {
996 if list[i] == list[j] {
997 skip++
998 } else {
999 list[j+skip] = list[j]
1000 }
1001 }
1002 totalSkip += skip
1003 }
1004 return list[totalSkip:]
1005}
Colin Cross06a931b2015-10-28 17:23:31 -07001006
1007var Bool = proptools.Bool