blob: 75292aa73d370d3dd9b9d1231f662bfcab4c89ac [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 (
Colin Cross3f40fa42015-01-30 17:27:36 -080022 "fmt"
Dan Albert9e10cd42016-08-03 14:12:14 -070023 "strconv"
Colin Cross3f40fa42015-01-30 17:27:36 -080024 "strings"
25
Colin Cross97ba0732015-03-23 17:50:24 -070026 "github.com/google/blueprint"
Colin Cross06a931b2015-10-28 17:23:31 -070027 "github.com/google/blueprint/proptools"
Colin Cross97ba0732015-03-23 17:50:24 -070028
Colin Cross463a90e2015-06-17 14:20:06 -070029 "android/soong"
Colin Cross635c3b02016-05-18 15:37:25 -070030 "android/soong/android"
Colin Crossb98c8b02016-07-29 13:44:28 -070031 "android/soong/cc/config"
Colin Cross5049f022015-03-18 13:28:46 -070032 "android/soong/genrule"
Colin Cross3f40fa42015-01-30 17:27:36 -080033)
34
Colin Cross463a90e2015-06-17 14:20:06 -070035func init() {
Colin Crossca860ac2016-01-04 14:34:37 -080036 soong.RegisterModuleType("cc_defaults", defaultsFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070037
Colin Cross463a90e2015-06-17 14:20:06 -070038 // LinkageMutator must be registered after common.ArchMutator, but that is guaranteed by
39 // the Go initialization order because this package depends on common, so common's init
40 // functions will run first.
Colin Crosse8a67a72016-08-07 21:17:54 -070041 android.RegisterBottomUpMutator("link", linkageMutator).Parallel()
42 android.RegisterBottomUpMutator("ndk_api", ndkApiMutator).Parallel()
43 android.RegisterBottomUpMutator("test_per_src", testPerSrcMutator).Parallel()
44 android.RegisterBottomUpMutator("begin", beginMutator).Parallel()
45 android.RegisterBottomUpMutator("deps", depsMutator).Parallel()
Colin Cross16b23492016-01-06 14:41:07 -080046
Colin Cross635c3b02016-05-18 15:37:25 -070047 android.RegisterTopDownMutator("asan_deps", sanitizerDepsMutator(asan))
Colin Crosse8a67a72016-08-07 21:17:54 -070048 android.RegisterBottomUpMutator("asan", sanitizerMutator(asan)).Parallel()
Colin Cross16b23492016-01-06 14:41:07 -080049
Colin Cross635c3b02016-05-18 15:37:25 -070050 android.RegisterTopDownMutator("tsan_deps", sanitizerDepsMutator(tsan))
Colin Crosse8a67a72016-08-07 21:17:54 -070051 android.RegisterBottomUpMutator("tsan", sanitizerMutator(tsan)).Parallel()
Colin Crossb98c8b02016-07-29 13:44:28 -070052
53 pctx.Import("android/soong/cc/config")
Colin Cross463a90e2015-06-17 14:20:06 -070054}
55
Colin Crossca860ac2016-01-04 14:34:37 -080056type Deps struct {
57 SharedLibs, LateSharedLibs []string
58 StaticLibs, LateStaticLibs, WholeStaticLibs []string
Colin Crossc472d572015-03-17 15:06:21 -070059
Dan Willemsen490a8dc2016-06-06 18:22:19 -070060 ReexportSharedLibHeaders, ReexportStaticLibHeaders []string
61
Colin Cross81413472016-04-11 14:37:39 -070062 ObjFiles []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -070063
Dan Willemsenb40aab62016-04-20 14:21:14 -070064 GeneratedSources []string
65 GeneratedHeaders []string
66
Dan Willemsenb3454ab2016-09-28 17:34:58 -070067 ReexportGeneratedHeaders []string
68
Colin Cross97ba0732015-03-23 17:50:24 -070069 CrtBegin, CrtEnd string
Colin Crossc472d572015-03-17 15:06:21 -070070}
71
Colin Crossca860ac2016-01-04 14:34:37 -080072type PathDeps struct {
Colin Cross26c34ed2016-09-30 17:10:16 -070073 // Paths to .so files
74 SharedLibs, LateSharedLibs android.Paths
75 // Paths to the dependencies to use for .so files (.so.toc files)
76 SharedLibsDeps, LateSharedLibsDeps android.Paths
77 // Paths to .a files
Colin Cross635c3b02016-05-18 15:37:25 -070078 StaticLibs, LateStaticLibs, WholeStaticLibs android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -070079
Colin Cross26c34ed2016-09-30 17:10:16 -070080 // Paths to .o files
Colin Cross635c3b02016-05-18 15:37:25 -070081 ObjFiles android.Paths
82 WholeStaticLibObjFiles android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -070083
Colin Cross26c34ed2016-09-30 17:10:16 -070084 // Paths to generated source files
Colin Cross635c3b02016-05-18 15:37:25 -070085 GeneratedSources android.Paths
86 GeneratedHeaders android.Paths
Dan Willemsenb40aab62016-04-20 14:21:14 -070087
Dan Willemsen76f08272016-07-09 00:14:08 -070088 Flags, ReexportedFlags []string
Dan Willemsen847dcc72016-09-29 12:13:36 -070089 ReexportedFlagsDeps android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -070090
Colin Cross26c34ed2016-09-30 17:10:16 -070091 // Paths to crt*.o files
Colin Cross635c3b02016-05-18 15:37:25 -070092 CrtBegin, CrtEnd android.OptionalPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -070093}
94
Colin Crossca860ac2016-01-04 14:34:37 -080095type Flags struct {
Colin Cross28344522015-04-22 13:07:53 -070096 GlobalFlags []string // Flags that apply to C, C++, and assembly source files
97 AsFlags []string // Flags that apply to assembly source files
98 CFlags []string // Flags that apply to C and C++ source files
99 ConlyFlags []string // Flags that apply to C source files
100 CppFlags []string // Flags that apply to C++ source files
101 YaccFlags []string // Flags that apply to Yacc source files
102 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
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
Colin Crossca860ac2016-01-04 14:34:37 -0800107
108 RequiredInstructionSet string
Colin Cross16b23492016-01-06 14:41:07 -0800109 DynamicLinker string
110
Colin Cross635c3b02016-05-18 15:37:25 -0700111 CFlagsDeps android.Paths // Files depended on by compiler flags
Colin Crossc472d572015-03-17 15:06:21 -0700112}
113
Colin Cross81413472016-04-11 14:37:39 -0700114type ObjectLinkerProperties struct {
115 // names of other cc_object modules to link into this module using partial linking
116 Objs []string `android:"arch_variant"`
117}
118
Colin Crossca860ac2016-01-04 14:34:37 -0800119// Properties used to compile all C or C++ modules
120type BaseProperties struct {
121 // compile module with clang instead of gcc
122 Clang *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700123
124 // Minimum sdk version supported when compiling against the ndk
125 Sdk_version string
126
Colin Crossca860ac2016-01-04 14:34:37 -0800127 // don't insert default compiler flags into asflags, cflags,
128 // cppflags, conlyflags, ldflags, or include_dirs
129 No_default_compiler_flags *bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700130
131 AndroidMkSharedLibs []string `blueprint:"mutated"`
Colin Crossbc6fb162016-05-24 15:39:04 -0700132 HideFromMake bool `blueprint:"mutated"`
Colin Crossb0f28952016-09-19 16:46:53 -0700133 PreventInstall bool `blueprint:"mutated"`
Colin Crossca860ac2016-01-04 14:34:37 -0800134}
135
Colin Crossca860ac2016-01-04 14:34:37 -0800136type UnusedProperties struct {
Colin Cross21b481b2016-04-15 16:27:17 -0700137 Native_coverage *bool
Colin Cross21b481b2016-04-15 16:27:17 -0700138 Tags []string
Colin Crosscfad1192015-11-02 16:43:11 -0800139}
140
Colin Crossca860ac2016-01-04 14:34:37 -0800141type ModuleContextIntf interface {
Colin Crossca860ac2016-01-04 14:34:37 -0800142 static() bool
143 staticBinary() bool
144 clang() bool
Colin Crossb98c8b02016-07-29 13:44:28 -0700145 toolchain() config.Toolchain
Colin Crossca860ac2016-01-04 14:34:37 -0800146 noDefaultCompilerFlags() bool
147 sdk() bool
148 sdkVersion() string
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700149 selectedStl() string
Colin Crossca860ac2016-01-04 14:34:37 -0800150}
151
152type ModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700153 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800154 ModuleContextIntf
155}
156
157type BaseModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700158 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800159 ModuleContextIntf
160}
161
Colin Crossca860ac2016-01-04 14:34:37 -0800162type feature interface {
163 begin(ctx BaseModuleContext)
164 deps(ctx BaseModuleContext, deps Deps) Deps
165 flags(ctx ModuleContext, flags Flags) Flags
166 props() []interface{}
167}
168
169type compiler interface {
Colin Cross42742b82016-08-01 13:20:05 -0700170 compilerInit(ctx BaseModuleContext)
171 compilerDeps(ctx BaseModuleContext, deps Deps) Deps
172 compilerFlags(ctx ModuleContext, flags Flags) Flags
173 compilerProps() []interface{}
174
Colin Cross76fada02016-07-27 10:31:13 -0700175 appendCflags([]string)
176 appendAsflags([]string)
Colin Cross635c3b02016-05-18 15:37:25 -0700177 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -0800178}
179
180type linker interface {
Colin Cross42742b82016-08-01 13:20:05 -0700181 linkerInit(ctx BaseModuleContext)
182 linkerDeps(ctx BaseModuleContext, deps Deps) Deps
183 linkerFlags(ctx ModuleContext, flags Flags) Flags
184 linkerProps() []interface{}
185
Colin Cross635c3b02016-05-18 15:37:25 -0700186 link(ctx ModuleContext, flags Flags, deps PathDeps, objFiles android.Paths) android.Path
Colin Cross76fada02016-07-27 10:31:13 -0700187 appendLdflags([]string)
Colin Crossca860ac2016-01-04 14:34:37 -0800188}
189
190type installer interface {
Colin Cross42742b82016-08-01 13:20:05 -0700191 installerProps() []interface{}
Colin Cross635c3b02016-05-18 15:37:25 -0700192 install(ctx ModuleContext, path android.Path)
Colin Crossca860ac2016-01-04 14:34:37 -0800193 inData() bool
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700194 hostToolPath() android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800195}
196
Colin Crossc99deeb2016-04-11 15:06:20 -0700197type dependencyTag struct {
198 blueprint.BaseDependencyTag
199 name string
200 library bool
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700201
202 reexportFlags bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700203}
204
205var (
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700206 sharedDepTag = dependencyTag{name: "shared", library: true}
207 sharedExportDepTag = dependencyTag{name: "shared", library: true, reexportFlags: true}
208 lateSharedDepTag = dependencyTag{name: "late shared", library: true}
209 staticDepTag = dependencyTag{name: "static", library: true}
210 staticExportDepTag = dependencyTag{name: "static", library: true, reexportFlags: true}
211 lateStaticDepTag = dependencyTag{name: "late static", library: true}
212 wholeStaticDepTag = dependencyTag{name: "whole static", library: true, reexportFlags: true}
213 genSourceDepTag = dependencyTag{name: "gen source"}
214 genHeaderDepTag = dependencyTag{name: "gen header"}
215 genHeaderExportDepTag = dependencyTag{name: "gen header", reexportFlags: true}
216 objDepTag = dependencyTag{name: "obj"}
217 crtBeginDepTag = dependencyTag{name: "crtbegin"}
218 crtEndDepTag = dependencyTag{name: "crtend"}
219 reuseObjTag = dependencyTag{name: "reuse objects"}
220 ndkStubDepTag = dependencyTag{name: "ndk stub", library: true}
221 ndkLateStubDepTag = dependencyTag{name: "ndk late stub", library: true}
Colin Crossc99deeb2016-04-11 15:06:20 -0700222)
223
Colin Crossca860ac2016-01-04 14:34:37 -0800224// Module contains the properties and members used by all C/C++ module types, and implements
225// the blueprint.Module interface. It delegates to compiler, linker, and installer interfaces
226// to construct the output file. Behavior can be customized with a Customizer interface
227type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700228 android.ModuleBase
229 android.DefaultableModule
Colin Crossc472d572015-03-17 15:06:21 -0700230
Colin Crossca860ac2016-01-04 14:34:37 -0800231 Properties BaseProperties
232 unused UnusedProperties
Colin Crossfa138792015-04-24 17:31:52 -0700233
Colin Crossca860ac2016-01-04 14:34:37 -0800234 // initialize before calling Init
Colin Cross635c3b02016-05-18 15:37:25 -0700235 hod android.HostOrDeviceSupported
236 multilib android.Multilib
Colin Crossc472d572015-03-17 15:06:21 -0700237
Colin Crossca860ac2016-01-04 14:34:37 -0800238 // delegates, initialize before calling Init
Colin Crossb4ce0ec2016-09-13 13:41:39 -0700239 features []feature
240 compiler compiler
241 linker linker
242 installer installer
243 stl *stl
244 sanitize *sanitize
Colin Cross16b23492016-01-06 14:41:07 -0800245
246 androidMkSharedLibDeps []string
Colin Cross74d1ec02015-04-28 13:30:13 -0700247
Colin Cross635c3b02016-05-18 15:37:25 -0700248 outputFile android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800249
Colin Crossb98c8b02016-07-29 13:44:28 -0700250 cachedToolchain config.Toolchain
Colin Crossb916a382016-07-29 17:28:03 -0700251
252 subAndroidMkOnce map[subAndroidMkProvider]bool
Colin Crossc472d572015-03-17 15:06:21 -0700253}
254
Colin Crossca860ac2016-01-04 14:34:37 -0800255func (c *Module) Init() (blueprint.Module, []interface{}) {
256 props := []interface{}{&c.Properties, &c.unused}
Colin Crossca860ac2016-01-04 14:34:37 -0800257 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700258 props = append(props, c.compiler.compilerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800259 }
260 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700261 props = append(props, c.linker.linkerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800262 }
263 if c.installer != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700264 props = append(props, c.installer.installerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800265 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700266 if c.stl != nil {
267 props = append(props, c.stl.props()...)
268 }
Colin Cross16b23492016-01-06 14:41:07 -0800269 if c.sanitize != nil {
270 props = append(props, c.sanitize.props()...)
271 }
Colin Crossca860ac2016-01-04 14:34:37 -0800272 for _, feature := range c.features {
273 props = append(props, feature.props()...)
274 }
Colin Crossc472d572015-03-17 15:06:21 -0700275
Colin Cross635c3b02016-05-18 15:37:25 -0700276 _, props = android.InitAndroidArchModule(c, c.hod, c.multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700277
Colin Cross635c3b02016-05-18 15:37:25 -0700278 return android.InitDefaultableModule(c, c, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700279}
280
Colin Crossb916a382016-07-29 17:28:03 -0700281// Returns true for dependency roots (binaries)
282// TODO(ccross): also handle dlopenable libraries
283func (c *Module) isDependencyRoot() bool {
284 if root, ok := c.linker.(interface {
285 isDependencyRoot() bool
286 }); ok {
287 return root.isDependencyRoot()
288 }
289 return false
290}
291
Colin Crossca860ac2016-01-04 14:34:37 -0800292type baseModuleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700293 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800294 moduleContextImpl
295}
296
297type moduleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700298 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800299 moduleContextImpl
300}
301
302type moduleContextImpl struct {
303 mod *Module
304 ctx BaseModuleContext
305}
306
Colin Crossca860ac2016-01-04 14:34:37 -0800307func (ctx *moduleContextImpl) clang() bool {
308 return ctx.mod.clang(ctx.ctx)
309}
310
Colin Crossb98c8b02016-07-29 13:44:28 -0700311func (ctx *moduleContextImpl) toolchain() config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800312 return ctx.mod.toolchain(ctx.ctx)
313}
314
315func (ctx *moduleContextImpl) static() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700316 if static, ok := ctx.mod.linker.(interface {
317 static() bool
318 }); ok {
319 return static.static()
Colin Crossca860ac2016-01-04 14:34:37 -0800320 }
Colin Crossb916a382016-07-29 17:28:03 -0700321 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800322}
323
324func (ctx *moduleContextImpl) staticBinary() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700325 if static, ok := ctx.mod.linker.(interface {
326 staticBinary() bool
327 }); ok {
328 return static.staticBinary()
Colin Crossca860ac2016-01-04 14:34:37 -0800329 }
Colin Crossb916a382016-07-29 17:28:03 -0700330 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800331}
332
333func (ctx *moduleContextImpl) noDefaultCompilerFlags() bool {
334 return Bool(ctx.mod.Properties.No_default_compiler_flags)
335}
336
337func (ctx *moduleContextImpl) sdk() bool {
Dan Willemsena96ff642016-06-07 12:34:45 -0700338 if ctx.ctx.Device() {
339 return ctx.mod.Properties.Sdk_version != ""
340 }
341 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800342}
343
344func (ctx *moduleContextImpl) sdkVersion() string {
Dan Willemsena96ff642016-06-07 12:34:45 -0700345 if ctx.ctx.Device() {
346 return ctx.mod.Properties.Sdk_version
347 }
348 return ""
Colin Crossca860ac2016-01-04 14:34:37 -0800349}
350
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700351func (ctx *moduleContextImpl) selectedStl() string {
352 if stl := ctx.mod.stl; stl != nil {
353 return stl.Properties.SelectedStl
354 }
355 return ""
356}
357
Colin Cross635c3b02016-05-18 15:37:25 -0700358func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800359 return &Module{
360 hod: hod,
361 multilib: multilib,
362 }
363}
364
Colin Cross635c3b02016-05-18 15:37:25 -0700365func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800366 module := newBaseModule(hod, multilib)
Colin Crossa8e07cc2016-04-04 15:07:06 -0700367 module.stl = &stl{}
Colin Cross16b23492016-01-06 14:41:07 -0800368 module.sanitize = &sanitize{}
Colin Crossca860ac2016-01-04 14:34:37 -0800369 return module
370}
371
Colin Cross635c3b02016-05-18 15:37:25 -0700372func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800373 ctx := &moduleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700374 ModuleContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800375 moduleContextImpl: moduleContextImpl{
376 mod: c,
377 },
378 }
379 ctx.ctx = ctx
380
381 flags := Flags{
382 Toolchain: c.toolchain(ctx),
383 Clang: c.clang(ctx),
384 }
Colin Crossca860ac2016-01-04 14:34:37 -0800385 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700386 flags = c.compiler.compilerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800387 }
388 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700389 flags = c.linker.linkerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800390 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700391 if c.stl != nil {
392 flags = c.stl.flags(ctx, flags)
393 }
Colin Cross16b23492016-01-06 14:41:07 -0800394 if c.sanitize != nil {
395 flags = c.sanitize.flags(ctx, flags)
396 }
Colin Crossca860ac2016-01-04 14:34:37 -0800397 for _, feature := range c.features {
398 flags = feature.flags(ctx, flags)
399 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800400 if ctx.Failed() {
401 return
402 }
403
Colin Crossb98c8b02016-07-29 13:44:28 -0700404 flags.CFlags, _ = filterList(flags.CFlags, config.IllegalFlags)
405 flags.CppFlags, _ = filterList(flags.CppFlags, config.IllegalFlags)
406 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, config.IllegalFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800407
Colin Crossca860ac2016-01-04 14:34:37 -0800408 // Optimization to reduce size of build.ninja
409 // Replace the long list of flags for each file with a module-local variable
410 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
411 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
412 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
413 flags.CFlags = []string{"$cflags"}
414 flags.CppFlags = []string{"$cppflags"}
415 flags.AsFlags = []string{"$asflags"}
416
Colin Crossc99deeb2016-04-11 15:06:20 -0700417 deps := c.depsToPaths(ctx)
Colin Cross3f40fa42015-01-30 17:27:36 -0800418 if ctx.Failed() {
419 return
420 }
421
Dan Willemsen76f08272016-07-09 00:14:08 -0700422 flags.GlobalFlags = append(flags.GlobalFlags, deps.Flags...)
Colin Crossed9f8682015-03-18 17:17:35 -0700423
Colin Cross635c3b02016-05-18 15:37:25 -0700424 var objFiles android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -0800425 if c.compiler != nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700426 objFiles = c.compiler.compile(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -0800427 if ctx.Failed() {
428 return
429 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800430 }
431
Colin Crossca860ac2016-01-04 14:34:37 -0800432 if c.linker != nil {
433 outputFile := c.linker.link(ctx, flags, deps, objFiles)
434 if ctx.Failed() {
435 return
436 }
Colin Cross635c3b02016-05-18 15:37:25 -0700437 c.outputFile = android.OptionalPathForPath(outputFile)
Colin Cross5049f022015-03-18 13:28:46 -0700438
Colin Crossb0f28952016-09-19 16:46:53 -0700439 if c.installer != nil && !c.Properties.PreventInstall {
Colin Crossca860ac2016-01-04 14:34:37 -0800440 c.installer.install(ctx, outputFile)
441 if ctx.Failed() {
442 return
443 }
444 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700445 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800446}
447
Colin Crossb98c8b02016-07-29 13:44:28 -0700448func (c *Module) toolchain(ctx BaseModuleContext) config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800449 if c.cachedToolchain == nil {
Colin Crossb98c8b02016-07-29 13:44:28 -0700450 c.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
Colin Cross3f40fa42015-01-30 17:27:36 -0800451 }
Colin Crossca860ac2016-01-04 14:34:37 -0800452 return c.cachedToolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800453}
454
Colin Crossca860ac2016-01-04 14:34:37 -0800455func (c *Module) begin(ctx BaseModuleContext) {
456 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700457 c.compiler.compilerInit(ctx)
Colin Cross21b9a242015-03-24 14:15:58 -0700458 }
Colin Crossca860ac2016-01-04 14:34:37 -0800459 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700460 c.linker.linkerInit(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800461 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700462 if c.stl != nil {
463 c.stl.begin(ctx)
464 }
Colin Cross16b23492016-01-06 14:41:07 -0800465 if c.sanitize != nil {
466 c.sanitize.begin(ctx)
467 }
Colin Crossca860ac2016-01-04 14:34:37 -0800468 for _, feature := range c.features {
469 feature.begin(ctx)
470 }
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700471 if ctx.sdk() {
472 version, err := normalizeNdkApiLevel(ctx.sdkVersion(), ctx.Arch())
473 if err != nil {
474 ctx.PropertyErrorf("sdk_version", err.Error())
475 }
476 c.Properties.Sdk_version = strconv.Itoa(version)
477 }
Colin Crossca860ac2016-01-04 14:34:37 -0800478}
479
Colin Crossc99deeb2016-04-11 15:06:20 -0700480func (c *Module) deps(ctx BaseModuleContext) Deps {
481 deps := Deps{}
482
483 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700484 deps = c.compiler.compilerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700485 }
486 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700487 deps = c.linker.linkerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700488 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700489 if c.stl != nil {
490 deps = c.stl.deps(ctx, deps)
491 }
Colin Cross16b23492016-01-06 14:41:07 -0800492 if c.sanitize != nil {
493 deps = c.sanitize.deps(ctx, deps)
494 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700495 for _, feature := range c.features {
496 deps = feature.deps(ctx, deps)
497 }
498
499 deps.WholeStaticLibs = lastUniqueElements(deps.WholeStaticLibs)
500 deps.StaticLibs = lastUniqueElements(deps.StaticLibs)
501 deps.LateStaticLibs = lastUniqueElements(deps.LateStaticLibs)
502 deps.SharedLibs = lastUniqueElements(deps.SharedLibs)
503 deps.LateSharedLibs = lastUniqueElements(deps.LateSharedLibs)
504
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700505 for _, lib := range deps.ReexportSharedLibHeaders {
506 if !inList(lib, deps.SharedLibs) {
507 ctx.PropertyErrorf("export_shared_lib_headers", "Shared library not in shared_libs: '%s'", lib)
508 }
509 }
510
511 for _, lib := range deps.ReexportStaticLibHeaders {
512 if !inList(lib, deps.StaticLibs) {
513 ctx.PropertyErrorf("export_static_lib_headers", "Static library not in static_libs: '%s'", lib)
514 }
515 }
516
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700517 for _, gen := range deps.ReexportGeneratedHeaders {
518 if !inList(gen, deps.GeneratedHeaders) {
519 ctx.PropertyErrorf("export_generated_headers", "Generated header module not in generated_headers: '%s'", gen)
520 }
521 }
522
Colin Crossc99deeb2016-04-11 15:06:20 -0700523 return deps
524}
525
Dan Albert7e9d2952016-08-04 13:02:36 -0700526func (c *Module) beginMutator(actx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800527 ctx := &baseModuleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700528 BaseContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800529 moduleContextImpl: moduleContextImpl{
530 mod: c,
531 },
532 }
533 ctx.ctx = ctx
534
Colin Crossca860ac2016-01-04 14:34:37 -0800535 c.begin(ctx)
Dan Albert7e9d2952016-08-04 13:02:36 -0700536}
537
538func (c *Module) depsMutator(actx android.BottomUpMutatorContext) {
539 ctx := &baseModuleContext{
540 BaseContext: actx,
541 moduleContextImpl: moduleContextImpl{
542 mod: c,
543 },
544 }
545 ctx.ctx = ctx
Colin Crossca860ac2016-01-04 14:34:37 -0800546
Colin Crossc99deeb2016-04-11 15:06:20 -0700547 deps := c.deps(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800548
Colin Crossb5bc4b42016-07-11 16:11:59 -0700549 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.SharedLibs...)
550 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.LateSharedLibs...)
Dan Willemsen72d39932016-07-08 23:23:48 -0700551
Dan Albert914449f2016-06-17 16:45:24 -0700552 variantNdkLibs := []string{}
553 variantLateNdkLibs := []string{}
Dan Willemsen72d39932016-07-08 23:23:48 -0700554 if ctx.sdk() {
Dan Albert914449f2016-06-17 16:45:24 -0700555 version := ctx.sdkVersion()
Dan Willemsen72d39932016-07-08 23:23:48 -0700556
Dan Albert914449f2016-06-17 16:45:24 -0700557 // Rewrites the names of shared libraries into the names of the NDK
558 // libraries where appropriate. This returns two slices.
559 //
560 // The first is a list of non-variant shared libraries (either rewritten
561 // NDK libraries to the modules in prebuilts/ndk, or not rewritten
562 // because they are not NDK libraries).
563 //
564 // The second is a list of ndk_library modules. These need to be
565 // separated because they are a variation dependency and must be added
566 // in a different manner.
567 rewriteNdkLibs := func(list []string) ([]string, []string) {
568 variantLibs := []string{}
569 nonvariantLibs := []string{}
570 for _, entry := range list {
Dan Willemsen72d39932016-07-08 23:23:48 -0700571 if inList(entry, ndkPrebuiltSharedLibraries) {
Dan Albert914449f2016-06-17 16:45:24 -0700572 if !inList(entry, ndkMigratedLibs) {
573 nonvariantLibs = append(nonvariantLibs, entry+".ndk."+version)
574 } else {
575 variantLibs = append(variantLibs, entry+ndkLibrarySuffix)
576 }
577 } else {
578 nonvariantLibs = append(variantLibs, entry)
Dan Willemsen72d39932016-07-08 23:23:48 -0700579 }
580 }
Dan Albert914449f2016-06-17 16:45:24 -0700581 return nonvariantLibs, variantLibs
Dan Willemsen72d39932016-07-08 23:23:48 -0700582 }
583
Dan Albert914449f2016-06-17 16:45:24 -0700584 deps.SharedLibs, variantNdkLibs = rewriteNdkLibs(deps.SharedLibs)
585 deps.LateSharedLibs, variantLateNdkLibs = rewriteNdkLibs(deps.LateSharedLibs)
Dan Willemsen72d39932016-07-08 23:23:48 -0700586 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700587
588 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, wholeStaticDepTag,
589 deps.WholeStaticLibs...)
590
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700591 for _, lib := range deps.StaticLibs {
592 depTag := staticDepTag
593 if inList(lib, deps.ReexportStaticLibHeaders) {
594 depTag = staticExportDepTag
595 }
Colin Cross15a0d462016-07-14 14:49:58 -0700596 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700597 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700598
599 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, lateStaticDepTag,
600 deps.LateStaticLibs...)
601
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700602 for _, lib := range deps.SharedLibs {
603 depTag := sharedDepTag
604 if inList(lib, deps.ReexportSharedLibHeaders) {
605 depTag = sharedExportDepTag
606 }
Colin Cross15a0d462016-07-14 14:49:58 -0700607 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700608 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700609
610 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, lateSharedDepTag,
611 deps.LateSharedLibs...)
612
Colin Cross68861832016-07-08 10:41:41 -0700613 actx.AddDependency(c, genSourceDepTag, deps.GeneratedSources...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700614
615 for _, gen := range deps.GeneratedHeaders {
616 depTag := genHeaderDepTag
617 if inList(gen, deps.ReexportGeneratedHeaders) {
618 depTag = genHeaderExportDepTag
619 }
620 actx.AddDependency(c, depTag, gen)
621 }
Dan Willemsenb40aab62016-04-20 14:21:14 -0700622
Colin Cross68861832016-07-08 10:41:41 -0700623 actx.AddDependency(c, objDepTag, deps.ObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700624
625 if deps.CrtBegin != "" {
Colin Cross68861832016-07-08 10:41:41 -0700626 actx.AddDependency(c, crtBeginDepTag, deps.CrtBegin)
Colin Crossca860ac2016-01-04 14:34:37 -0800627 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700628 if deps.CrtEnd != "" {
Colin Cross68861832016-07-08 10:41:41 -0700629 actx.AddDependency(c, crtEndDepTag, deps.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700630 }
Dan Albert914449f2016-06-17 16:45:24 -0700631
632 version := ctx.sdkVersion()
633 actx.AddVariationDependencies([]blueprint.Variation{
634 {"ndk_api", version}, {"link", "shared"}}, ndkStubDepTag, variantNdkLibs...)
635 actx.AddVariationDependencies([]blueprint.Variation{
636 {"ndk_api", version}, {"link", "shared"}}, ndkLateStubDepTag, variantLateNdkLibs...)
Colin Cross6362e272015-10-29 15:25:03 -0700637}
Colin Cross21b9a242015-03-24 14:15:58 -0700638
Dan Albert7e9d2952016-08-04 13:02:36 -0700639func beginMutator(ctx android.BottomUpMutatorContext) {
640 if c, ok := ctx.Module().(*Module); ok && c.Enabled() {
641 c.beginMutator(ctx)
642 }
643}
644
Colin Cross635c3b02016-05-18 15:37:25 -0700645func depsMutator(ctx android.BottomUpMutatorContext) {
Dan Willemsen3f32f032016-07-11 14:36:48 -0700646 if c, ok := ctx.Module().(*Module); ok && c.Enabled() {
Colin Cross6362e272015-10-29 15:25:03 -0700647 c.depsMutator(ctx)
648 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800649}
650
Colin Crossca860ac2016-01-04 14:34:37 -0800651func (c *Module) clang(ctx BaseModuleContext) bool {
652 clang := Bool(c.Properties.Clang)
653
654 if c.Properties.Clang == nil {
655 if ctx.Host() {
656 clang = true
657 }
658
659 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
660 clang = true
661 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800662 }
Colin Cross28344522015-04-22 13:07:53 -0700663
Colin Crossca860ac2016-01-04 14:34:37 -0800664 if !c.toolchain(ctx).ClangSupported() {
665 clang = false
666 }
667
668 return clang
669}
670
Colin Crossc99deeb2016-04-11 15:06:20 -0700671// Convert dependencies to paths. Returns a PathDeps containing paths
Colin Cross635c3b02016-05-18 15:37:25 -0700672func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
Colin Crossca860ac2016-01-04 14:34:37 -0800673 var depPaths PathDeps
Colin Crossca860ac2016-01-04 14:34:37 -0800674
Dan Willemsena96ff642016-06-07 12:34:45 -0700675 // Whether a module can link to another module, taking into
676 // account NDK linking.
Dan Albert9e10cd42016-08-03 14:12:14 -0700677 checkLinkType := func(from, to *Module) {
Dan Willemsena96ff642016-06-07 12:34:45 -0700678 if from.Target().Os != android.Android {
679 // Host code is not restricted
Dan Albert9e10cd42016-08-03 14:12:14 -0700680 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700681 }
682 if from.Properties.Sdk_version == "" {
683 // Platform code can link to anything
Dan Albert9e10cd42016-08-03 14:12:14 -0700684 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700685 }
Colin Crossb916a382016-07-29 17:28:03 -0700686 if _, ok := to.linker.(*toolchainLibraryDecorator); ok {
Dan Willemsena96ff642016-06-07 12:34:45 -0700687 // These are always allowed
Dan Albert9e10cd42016-08-03 14:12:14 -0700688 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700689 }
690 if _, ok := to.linker.(*ndkPrebuiltLibraryLinker); ok {
691 // These are allowed, but don't set sdk_version
Dan Albert9e10cd42016-08-03 14:12:14 -0700692 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700693 }
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700694 if _, ok := to.linker.(*ndkPrebuiltStlLinker); ok {
695 // These are allowed, but don't set sdk_version
Dan Albert9e10cd42016-08-03 14:12:14 -0700696 return
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700697 }
Colin Crossb916a382016-07-29 17:28:03 -0700698 if _, ok := to.linker.(*stubDecorator); ok {
Dan Albert914449f2016-06-17 16:45:24 -0700699 // These aren't real libraries, but are the stub shared libraries that are included in
700 // the NDK.
Dan Albert9e10cd42016-08-03 14:12:14 -0700701 return
Dan Albert914449f2016-06-17 16:45:24 -0700702 }
Dan Albert9e10cd42016-08-03 14:12:14 -0700703 if to.Properties.Sdk_version == "" {
704 // NDK code linking to platform code is never okay.
705 ctx.ModuleErrorf("depends on non-NDK-built library %q",
706 ctx.OtherModuleName(to))
707 }
708
709 // All this point we know we have two NDK libraries, but we need to
710 // check that we're not linking against anything built against a higher
711 // API level, as it is only valid to link against older or equivalent
712 // APIs.
713
714 if from.Properties.Sdk_version == "current" {
715 // Current can link against anything.
716 return
717 } else if to.Properties.Sdk_version == "current" {
718 // Current can't be linked against by anything else.
719 ctx.ModuleErrorf("links %q built against newer API version %q",
720 ctx.OtherModuleName(to), "current")
721 }
722
723 fromApi, err := strconv.Atoi(from.Properties.Sdk_version)
724 if err != nil {
725 ctx.PropertyErrorf("sdk_version",
726 "Invalid sdk_version value (must be int): %q",
727 from.Properties.Sdk_version)
728 }
729 toApi, err := strconv.Atoi(to.Properties.Sdk_version)
730 if err != nil {
731 ctx.PropertyErrorf("sdk_version",
732 "Invalid sdk_version value (must be int): %q",
733 to.Properties.Sdk_version)
734 }
735
736 if toApi > fromApi {
737 ctx.ModuleErrorf("links %q built against newer API version %q",
738 ctx.OtherModuleName(to), to.Properties.Sdk_version)
739 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700740 }
741
Colin Crossc99deeb2016-04-11 15:06:20 -0700742 ctx.VisitDirectDeps(func(m blueprint.Module) {
743 name := ctx.OtherModuleName(m)
744 tag := ctx.OtherModuleDependencyTag(m)
Colin Crossca860ac2016-01-04 14:34:37 -0800745
Colin Cross635c3b02016-05-18 15:37:25 -0700746 a, _ := m.(android.Module)
Colin Crossc99deeb2016-04-11 15:06:20 -0700747 if a == nil {
748 ctx.ModuleErrorf("module %q not an android module", name)
749 return
Colin Crossca860ac2016-01-04 14:34:37 -0800750 }
Colin Crossca860ac2016-01-04 14:34:37 -0800751
Dan Willemsena96ff642016-06-07 12:34:45 -0700752 cc, _ := m.(*Module)
753 if cc == nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700754 switch tag {
Colin Cross635c3b02016-05-18 15:37:25 -0700755 case android.DefaultsDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700756 case genSourceDepTag:
757 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
758 depPaths.GeneratedSources = append(depPaths.GeneratedSources,
759 genRule.GeneratedSourceFiles()...)
760 } else {
761 ctx.ModuleErrorf("module %q is not a gensrcs or genrule", name)
762 }
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700763 case genHeaderDepTag, genHeaderExportDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700764 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
765 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders,
766 genRule.GeneratedSourceFiles()...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700767 flags := includeDirsToFlags(android.Paths{genRule.GeneratedHeaderDir()})
768 depPaths.Flags = append(depPaths.Flags, flags)
769 if tag == genHeaderExportDepTag {
770 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700771 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps,
772 genRule.GeneratedSourceFiles()...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700773 }
Dan Willemsenb40aab62016-04-20 14:21:14 -0700774 } else {
775 ctx.ModuleErrorf("module %q is not a genrule", name)
776 }
777 default:
Colin Crossc99deeb2016-04-11 15:06:20 -0700778 ctx.ModuleErrorf("depends on non-cc module %q", name)
Colin Crossca860ac2016-01-04 14:34:37 -0800779 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700780 return
781 }
782
783 if !a.Enabled() {
784 ctx.ModuleErrorf("depends on disabled module %q", name)
785 return
786 }
787
Colin Crossa1ad8d12016-06-01 17:09:44 -0700788 if a.Target().Os != ctx.Os() {
789 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), name)
790 return
791 }
792
793 if a.Target().Arch.ArchType != ctx.Arch().ArchType {
794 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700795 return
796 }
797
Dan Willemsena96ff642016-06-07 12:34:45 -0700798 if !cc.outputFile.Valid() {
Colin Crossc99deeb2016-04-11 15:06:20 -0700799 ctx.ModuleErrorf("module %q missing output file", name)
800 return
801 }
802
803 if tag == reuseObjTag {
804 depPaths.ObjFiles = append(depPaths.ObjFiles,
Colin Crossb916a382016-07-29 17:28:03 -0700805 cc.compiler.(libraryInterface).reuseObjs()...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700806 return
807 }
808
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700809 if t, ok := tag.(dependencyTag); ok && t.library {
Dan Willemsena96ff642016-06-07 12:34:45 -0700810 if i, ok := cc.linker.(exportedFlagsProducer); ok {
Dan Willemsen76f08272016-07-09 00:14:08 -0700811 flags := i.exportedFlags()
Dan Willemsen847dcc72016-09-29 12:13:36 -0700812 deps := i.exportedFlagsDeps()
Dan Willemsen76f08272016-07-09 00:14:08 -0700813 depPaths.Flags = append(depPaths.Flags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700814 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders, deps...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700815
816 if t.reexportFlags {
Dan Willemsen76f08272016-07-09 00:14:08 -0700817 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700818 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps, deps...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700819 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700820 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700821
Dan Albert9e10cd42016-08-03 14:12:14 -0700822 checkLinkType(c, cc)
Colin Crossc99deeb2016-04-11 15:06:20 -0700823 }
824
Colin Cross26c34ed2016-09-30 17:10:16 -0700825 var ptr *android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -0700826 var depPtr *android.Paths
Colin Crossc99deeb2016-04-11 15:06:20 -0700827
Colin Cross26c34ed2016-09-30 17:10:16 -0700828 linkFile := cc.outputFile
829 depFile := android.OptionalPath{}
830
Colin Crossc99deeb2016-04-11 15:06:20 -0700831 switch tag {
Dan Albert914449f2016-06-17 16:45:24 -0700832 case ndkStubDepTag, sharedDepTag, sharedExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700833 ptr = &depPaths.SharedLibs
834 depPtr = &depPaths.SharedLibsDeps
835 depFile = cc.linker.(libraryInterface).toc()
Dan Albert914449f2016-06-17 16:45:24 -0700836 case lateSharedDepTag, ndkLateStubDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700837 ptr = &depPaths.LateSharedLibs
838 depPtr = &depPaths.LateSharedLibsDeps
839 depFile = cc.linker.(libraryInterface).toc()
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700840 case staticDepTag, staticExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700841 ptr = &depPaths.StaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -0700842 case lateStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700843 ptr = &depPaths.LateStaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -0700844 case wholeStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700845 ptr = &depPaths.WholeStaticLibs
Colin Crossb916a382016-07-29 17:28:03 -0700846 staticLib, ok := cc.linker.(libraryInterface)
847 if !ok || !staticLib.static() {
Dan Willemsena96ff642016-06-07 12:34:45 -0700848 ctx.ModuleErrorf("module %q not a static library", name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700849 return
850 }
851
852 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
853 postfix := " (required by " + ctx.OtherModuleName(m) + ")"
854 for i := range missingDeps {
855 missingDeps[i] += postfix
856 }
857 ctx.AddMissingDependencies(missingDeps)
858 }
859 depPaths.WholeStaticLibObjFiles =
Colin Crossc7a38dc2016-07-12 13:13:09 -0700860 append(depPaths.WholeStaticLibObjFiles, staticLib.objs()...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700861 case objDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700862 ptr = &depPaths.ObjFiles
Colin Crossc99deeb2016-04-11 15:06:20 -0700863 case crtBeginDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700864 depPaths.CrtBegin = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -0700865 case crtEndDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700866 depPaths.CrtEnd = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -0700867 default:
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700868 panic(fmt.Errorf("unknown dependency tag: %s", tag))
Colin Crossc99deeb2016-04-11 15:06:20 -0700869 }
870
Colin Cross26c34ed2016-09-30 17:10:16 -0700871 if ptr != nil {
872 *ptr = append(*ptr, linkFile.Path())
873 }
874
Colin Crossc99deeb2016-04-11 15:06:20 -0700875 if depPtr != nil {
Colin Cross26c34ed2016-09-30 17:10:16 -0700876 dep := depFile
877 if !dep.Valid() {
878 dep = linkFile
879 }
880 *depPtr = append(*depPtr, dep.Path())
Colin Crossca860ac2016-01-04 14:34:37 -0800881 }
882 })
883
884 return depPaths
885}
886
887func (c *Module) InstallInData() bool {
888 if c.installer == nil {
889 return false
890 }
Colin Cross94610402016-08-29 13:41:32 -0700891 if c.sanitize != nil && c.sanitize.inData() {
892 return true
893 }
Colin Crossca860ac2016-01-04 14:34:37 -0800894 return c.installer.inData()
895}
896
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700897func (c *Module) HostToolPath() android.OptionalPath {
898 if c.installer == nil {
899 return android.OptionalPath{}
900 }
901 return c.installer.hostToolPath()
902}
903
Colin Cross2ba19d92015-05-07 15:44:20 -0700904//
Colin Crosscfad1192015-11-02 16:43:11 -0800905// Defaults
906//
Colin Crossca860ac2016-01-04 14:34:37 -0800907type Defaults struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700908 android.ModuleBase
909 android.DefaultsModule
Colin Crosscfad1192015-11-02 16:43:11 -0800910}
911
Colin Cross635c3b02016-05-18 15:37:25 -0700912func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -0800913}
914
Colin Crossca860ac2016-01-04 14:34:37 -0800915func defaultsFactory() (blueprint.Module, []interface{}) {
Colin Crosse1d764e2016-08-18 14:18:32 -0700916 return DefaultsFactory()
917}
918
919func DefaultsFactory(props ...interface{}) (blueprint.Module, []interface{}) {
Colin Crossca860ac2016-01-04 14:34:37 -0800920 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -0800921
Colin Crosse1d764e2016-08-18 14:18:32 -0700922 props = append(props,
Colin Crossca860ac2016-01-04 14:34:37 -0800923 &BaseProperties{},
924 &BaseCompilerProperties{},
925 &BaseLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -0700926 &LibraryProperties{},
Colin Cross919281a2016-04-05 16:42:05 -0700927 &FlagExporterProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -0800928 &BinaryLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -0700929 &TestProperties{},
930 &TestBinaryProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -0800931 &UnusedProperties{},
932 &StlProperties{},
Colin Cross16b23492016-01-06 14:41:07 -0800933 &SanitizeProperties{},
Colin Cross665dce92016-04-28 14:50:03 -0700934 &StripProperties{},
Dan Willemsen7424d612016-09-01 13:45:39 -0700935 &InstallerProperties{},
Colin Crosse1d764e2016-08-18 14:18:32 -0700936 )
Colin Crosscfad1192015-11-02 16:43:11 -0800937
Colin Crosse1d764e2016-08-18 14:18:32 -0700938 _, props = android.InitAndroidArchModule(module, android.HostAndDeviceDefault,
939 android.MultilibDefault, props...)
Colin Crosscfad1192015-11-02 16:43:11 -0800940
Colin Crosse1d764e2016-08-18 14:18:32 -0700941 return android.InitDefaultsModule(module, module, props...)
Colin Crosscfad1192015-11-02 16:43:11 -0800942}
943
Colin Cross74d1ec02015-04-28 13:30:13 -0700944// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
945// modifies the slice contents in place, and returns a subslice of the original slice
946func lastUniqueElements(list []string) []string {
947 totalSkip := 0
948 for i := len(list) - 1; i >= totalSkip; i-- {
949 skip := 0
950 for j := i - 1; j >= totalSkip; j-- {
951 if list[i] == list[j] {
952 skip++
953 } else {
954 list[j+skip] = list[j]
955 }
956 }
957 totalSkip += skip
958 }
959 return list[totalSkip:]
960}
Colin Cross06a931b2015-10-28 17:23:31 -0700961
962var Bool = proptools.Bool