blob: df4e5cf24e46b625905079c9b8738af8f4627d08 [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 Cross18c0c5a2016-12-01 14:45:23 -0800113
114 GroupStaticLibs bool
Colin Crossc472d572015-03-17 15:06:21 -0700115}
116
Colin Cross81413472016-04-11 14:37:39 -0700117type ObjectLinkerProperties struct {
118 // names of other cc_object modules to link into this module using partial linking
119 Objs []string `android:"arch_variant"`
120}
121
Colin Crossca860ac2016-01-04 14:34:37 -0800122// Properties used to compile all C or C++ modules
123type BaseProperties struct {
124 // compile module with clang instead of gcc
125 Clang *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700126
127 // Minimum sdk version supported when compiling against the ndk
128 Sdk_version string
129
Dan Willemsend2ede872016-11-18 14:54:24 -0800130 // Whether to compile against the VNDK
131 Use_vndk bool
132
Colin Crossca860ac2016-01-04 14:34:37 -0800133 // don't insert default compiler flags into asflags, cflags,
134 // cppflags, conlyflags, ldflags, or include_dirs
135 No_default_compiler_flags *bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700136
137 AndroidMkSharedLibs []string `blueprint:"mutated"`
Colin Crossbc6fb162016-05-24 15:39:04 -0700138 HideFromMake bool `blueprint:"mutated"`
Colin Crossb0f28952016-09-19 16:46:53 -0700139 PreventInstall bool `blueprint:"mutated"`
Dan Willemsend2ede872016-11-18 14:54:24 -0800140 Vndk_version string `blueprint:"mutated"`
Colin Crossca860ac2016-01-04 14:34:37 -0800141}
142
Colin Crossca860ac2016-01-04 14:34:37 -0800143type UnusedProperties struct {
Colin Cross21b481b2016-04-15 16:27:17 -0700144 Native_coverage *bool
Colin Cross21b481b2016-04-15 16:27:17 -0700145 Tags []string
Colin Crosscfad1192015-11-02 16:43:11 -0800146}
147
Colin Crossca860ac2016-01-04 14:34:37 -0800148type ModuleContextIntf interface {
Colin Crossca860ac2016-01-04 14:34:37 -0800149 static() bool
150 staticBinary() bool
151 clang() bool
Colin Crossb98c8b02016-07-29 13:44:28 -0700152 toolchain() config.Toolchain
Colin Crossca860ac2016-01-04 14:34:37 -0800153 noDefaultCompilerFlags() bool
154 sdk() bool
155 sdkVersion() string
Dan Willemsend2ede872016-11-18 14:54:24 -0800156 vndk() bool
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700157 selectedStl() string
Colin Crossce75d2c2016-10-06 16:12:58 -0700158 baseModuleName() string
Colin Crossca860ac2016-01-04 14:34:37 -0800159}
160
161type ModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700162 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800163 ModuleContextIntf
164}
165
166type BaseModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700167 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800168 ModuleContextIntf
169}
170
Colin Crossca860ac2016-01-04 14:34:37 -0800171type feature interface {
172 begin(ctx BaseModuleContext)
173 deps(ctx BaseModuleContext, deps Deps) Deps
174 flags(ctx ModuleContext, flags Flags) Flags
175 props() []interface{}
176}
177
178type compiler interface {
Colin Cross42742b82016-08-01 13:20:05 -0700179 compilerInit(ctx BaseModuleContext)
180 compilerDeps(ctx BaseModuleContext, deps Deps) Deps
181 compilerFlags(ctx ModuleContext, flags Flags) Flags
182 compilerProps() []interface{}
183
Colin Cross76fada02016-07-27 10:31:13 -0700184 appendCflags([]string)
185 appendAsflags([]string)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700186 compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects
Colin Crossca860ac2016-01-04 14:34:37 -0800187}
188
189type linker interface {
Colin Cross42742b82016-08-01 13:20:05 -0700190 linkerInit(ctx BaseModuleContext)
191 linkerDeps(ctx BaseModuleContext, deps Deps) Deps
192 linkerFlags(ctx ModuleContext, flags Flags) Flags
193 linkerProps() []interface{}
194
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700195 link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path
Colin Cross76fada02016-07-27 10:31:13 -0700196 appendLdflags([]string)
Colin Crossca860ac2016-01-04 14:34:37 -0800197}
198
199type installer interface {
Colin Cross42742b82016-08-01 13:20:05 -0700200 installerProps() []interface{}
Colin Cross635c3b02016-05-18 15:37:25 -0700201 install(ctx ModuleContext, path android.Path)
Colin Crossca860ac2016-01-04 14:34:37 -0800202 inData() bool
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700203 hostToolPath() android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800204}
205
Colin Crossc99deeb2016-04-11 15:06:20 -0700206type dependencyTag struct {
207 blueprint.BaseDependencyTag
208 name string
209 library bool
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700210
211 reexportFlags bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700212}
213
214var (
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700215 sharedDepTag = dependencyTag{name: "shared", library: true}
216 sharedExportDepTag = dependencyTag{name: "shared", library: true, reexportFlags: true}
217 lateSharedDepTag = dependencyTag{name: "late shared", library: true}
218 staticDepTag = dependencyTag{name: "static", library: true}
219 staticExportDepTag = dependencyTag{name: "static", library: true, reexportFlags: true}
220 lateStaticDepTag = dependencyTag{name: "late static", library: true}
221 wholeStaticDepTag = dependencyTag{name: "whole static", library: true, reexportFlags: true}
222 genSourceDepTag = dependencyTag{name: "gen source"}
223 genHeaderDepTag = dependencyTag{name: "gen header"}
224 genHeaderExportDepTag = dependencyTag{name: "gen header", reexportFlags: true}
225 objDepTag = dependencyTag{name: "obj"}
226 crtBeginDepTag = dependencyTag{name: "crtbegin"}
227 crtEndDepTag = dependencyTag{name: "crtend"}
228 reuseObjTag = dependencyTag{name: "reuse objects"}
229 ndkStubDepTag = dependencyTag{name: "ndk stub", library: true}
230 ndkLateStubDepTag = dependencyTag{name: "ndk late stub", library: true}
Colin Crossc99deeb2016-04-11 15:06:20 -0700231)
232
Colin Crossca860ac2016-01-04 14:34:37 -0800233// Module contains the properties and members used by all C/C++ module types, and implements
234// the blueprint.Module interface. It delegates to compiler, linker, and installer interfaces
235// to construct the output file. Behavior can be customized with a Customizer interface
236type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700237 android.ModuleBase
238 android.DefaultableModule
Colin Crossc472d572015-03-17 15:06:21 -0700239
Colin Crossca860ac2016-01-04 14:34:37 -0800240 Properties BaseProperties
241 unused UnusedProperties
Colin Crossfa138792015-04-24 17:31:52 -0700242
Colin Crossca860ac2016-01-04 14:34:37 -0800243 // initialize before calling Init
Colin Cross635c3b02016-05-18 15:37:25 -0700244 hod android.HostOrDeviceSupported
245 multilib android.Multilib
Colin Crossc472d572015-03-17 15:06:21 -0700246
Colin Crossca860ac2016-01-04 14:34:37 -0800247 // delegates, initialize before calling Init
Colin Crossb4ce0ec2016-09-13 13:41:39 -0700248 features []feature
249 compiler compiler
250 linker linker
251 installer installer
252 stl *stl
253 sanitize *sanitize
Colin Cross16b23492016-01-06 14:41:07 -0800254
255 androidMkSharedLibDeps []string
Colin Cross74d1ec02015-04-28 13:30:13 -0700256
Colin Cross635c3b02016-05-18 15:37:25 -0700257 outputFile android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800258
Colin Crossb98c8b02016-07-29 13:44:28 -0700259 cachedToolchain config.Toolchain
Colin Crossb916a382016-07-29 17:28:03 -0700260
261 subAndroidMkOnce map[subAndroidMkProvider]bool
Colin Crossc472d572015-03-17 15:06:21 -0700262}
263
Colin Crossca860ac2016-01-04 14:34:37 -0800264func (c *Module) Init() (blueprint.Module, []interface{}) {
265 props := []interface{}{&c.Properties, &c.unused}
Colin Crossca860ac2016-01-04 14:34:37 -0800266 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700267 props = append(props, c.compiler.compilerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800268 }
269 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700270 props = append(props, c.linker.linkerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800271 }
272 if c.installer != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700273 props = append(props, c.installer.installerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800274 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700275 if c.stl != nil {
276 props = append(props, c.stl.props()...)
277 }
Colin Cross16b23492016-01-06 14:41:07 -0800278 if c.sanitize != nil {
279 props = append(props, c.sanitize.props()...)
280 }
Colin Crossca860ac2016-01-04 14:34:37 -0800281 for _, feature := range c.features {
282 props = append(props, feature.props()...)
283 }
Colin Crossc472d572015-03-17 15:06:21 -0700284
Colin Cross635c3b02016-05-18 15:37:25 -0700285 _, props = android.InitAndroidArchModule(c, c.hod, c.multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700286
Colin Cross635c3b02016-05-18 15:37:25 -0700287 return android.InitDefaultableModule(c, c, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700288}
289
Colin Crossb916a382016-07-29 17:28:03 -0700290// Returns true for dependency roots (binaries)
291// TODO(ccross): also handle dlopenable libraries
292func (c *Module) isDependencyRoot() bool {
293 if root, ok := c.linker.(interface {
294 isDependencyRoot() bool
295 }); ok {
296 return root.isDependencyRoot()
297 }
298 return false
299}
300
Colin Crossca860ac2016-01-04 14:34:37 -0800301type baseModuleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700302 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800303 moduleContextImpl
304}
305
306type moduleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700307 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800308 moduleContextImpl
309}
310
311type moduleContextImpl struct {
312 mod *Module
313 ctx BaseModuleContext
314}
315
Colin Crossca860ac2016-01-04 14:34:37 -0800316func (ctx *moduleContextImpl) clang() bool {
317 return ctx.mod.clang(ctx.ctx)
318}
319
Colin Crossb98c8b02016-07-29 13:44:28 -0700320func (ctx *moduleContextImpl) toolchain() config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800321 return ctx.mod.toolchain(ctx.ctx)
322}
323
324func (ctx *moduleContextImpl) static() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700325 if static, ok := ctx.mod.linker.(interface {
326 static() bool
327 }); ok {
328 return static.static()
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) staticBinary() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700334 if static, ok := ctx.mod.linker.(interface {
335 staticBinary() bool
336 }); ok {
337 return static.staticBinary()
Colin Crossca860ac2016-01-04 14:34:37 -0800338 }
Colin Crossb916a382016-07-29 17:28:03 -0700339 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800340}
341
342func (ctx *moduleContextImpl) noDefaultCompilerFlags() bool {
343 return Bool(ctx.mod.Properties.No_default_compiler_flags)
344}
345
346func (ctx *moduleContextImpl) sdk() bool {
Dan Willemsena96ff642016-06-07 12:34:45 -0700347 if ctx.ctx.Device() {
348 return ctx.mod.Properties.Sdk_version != ""
349 }
350 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800351}
352
353func (ctx *moduleContextImpl) sdkVersion() string {
Dan Willemsena96ff642016-06-07 12:34:45 -0700354 if ctx.ctx.Device() {
Dan Willemsend2ede872016-11-18 14:54:24 -0800355 if ctx.mod.Properties.Use_vndk {
356 return ctx.mod.Properties.Vndk_version
357 } else {
358 return ctx.mod.Properties.Sdk_version
359 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700360 }
361 return ""
Colin Crossca860ac2016-01-04 14:34:37 -0800362}
363
Dan Willemsend2ede872016-11-18 14:54:24 -0800364func (ctx *moduleContextImpl) vndk() bool {
365 if ctx.ctx.Device() {
366 return ctx.mod.Properties.Use_vndk
367 }
368 return false
369}
370
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700371func (ctx *moduleContextImpl) selectedStl() string {
372 if stl := ctx.mod.stl; stl != nil {
373 return stl.Properties.SelectedStl
374 }
375 return ""
376}
377
Colin Crossce75d2c2016-10-06 16:12:58 -0700378func (ctx *moduleContextImpl) baseModuleName() string {
379 return ctx.mod.ModuleBase.BaseModuleName()
380}
381
Colin Cross635c3b02016-05-18 15:37:25 -0700382func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800383 return &Module{
384 hod: hod,
385 multilib: multilib,
386 }
387}
388
Colin Cross635c3b02016-05-18 15:37:25 -0700389func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800390 module := newBaseModule(hod, multilib)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700391 module.features = []feature{
392 &tidyFeature{},
393 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700394 module.stl = &stl{}
Colin Cross16b23492016-01-06 14:41:07 -0800395 module.sanitize = &sanitize{}
Colin Crossca860ac2016-01-04 14:34:37 -0800396 return module
397}
398
Colin Crossce75d2c2016-10-06 16:12:58 -0700399func (c *Module) Prebuilt() *android.Prebuilt {
400 if p, ok := c.linker.(prebuiltLinkerInterface); ok {
401 return p.prebuilt()
402 }
403 return nil
404}
405
406func (c *Module) Name() string {
407 name := c.ModuleBase.Name()
408 if p, ok := c.linker.(prebuiltLinkerInterface); ok {
409 name = p.Name(name)
410 }
411 return name
412}
413
Colin Cross635c3b02016-05-18 15:37:25 -0700414func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800415 ctx := &moduleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700416 ModuleContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800417 moduleContextImpl: moduleContextImpl{
418 mod: c,
419 },
420 }
421 ctx.ctx = ctx
422
423 flags := Flags{
424 Toolchain: c.toolchain(ctx),
425 Clang: c.clang(ctx),
426 }
Colin Crossca860ac2016-01-04 14:34:37 -0800427 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700428 flags = c.compiler.compilerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800429 }
430 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700431 flags = c.linker.linkerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800432 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700433 if c.stl != nil {
434 flags = c.stl.flags(ctx, flags)
435 }
Colin Cross16b23492016-01-06 14:41:07 -0800436 if c.sanitize != nil {
437 flags = c.sanitize.flags(ctx, flags)
438 }
Colin Crossca860ac2016-01-04 14:34:37 -0800439 for _, feature := range c.features {
440 flags = feature.flags(ctx, flags)
441 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800442 if ctx.Failed() {
443 return
444 }
445
Colin Crossb98c8b02016-07-29 13:44:28 -0700446 flags.CFlags, _ = filterList(flags.CFlags, config.IllegalFlags)
447 flags.CppFlags, _ = filterList(flags.CppFlags, config.IllegalFlags)
448 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, config.IllegalFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800449
Colin Crossca860ac2016-01-04 14:34:37 -0800450 // Optimization to reduce size of build.ninja
451 // Replace the long list of flags for each file with a module-local variable
452 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
453 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
454 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
455 flags.CFlags = []string{"$cflags"}
456 flags.CppFlags = []string{"$cppflags"}
457 flags.AsFlags = []string{"$asflags"}
458
Colin Crossc99deeb2016-04-11 15:06:20 -0700459 deps := c.depsToPaths(ctx)
Colin Cross3f40fa42015-01-30 17:27:36 -0800460 if ctx.Failed() {
461 return
462 }
463
Dan Willemsen76f08272016-07-09 00:14:08 -0700464 flags.GlobalFlags = append(flags.GlobalFlags, deps.Flags...)
Colin Crossed9f8682015-03-18 17:17:35 -0700465
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700466 var objs Objects
Colin Crossca860ac2016-01-04 14:34:37 -0800467 if c.compiler != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700468 objs = c.compiler.compile(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -0800469 if ctx.Failed() {
470 return
471 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800472 }
473
Colin Crossca860ac2016-01-04 14:34:37 -0800474 if c.linker != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700475 outputFile := c.linker.link(ctx, flags, deps, objs)
Colin Crossca860ac2016-01-04 14:34:37 -0800476 if ctx.Failed() {
477 return
478 }
Colin Cross635c3b02016-05-18 15:37:25 -0700479 c.outputFile = android.OptionalPathForPath(outputFile)
Colin Crossce75d2c2016-10-06 16:12:58 -0700480 }
Colin Cross5049f022015-03-18 13:28:46 -0700481
Colin Crossce75d2c2016-10-06 16:12:58 -0700482 if c.installer != nil && !c.Properties.PreventInstall && c.outputFile.Valid() {
483 c.installer.install(ctx, c.outputFile.Path())
484 if ctx.Failed() {
485 return
Colin Crossca860ac2016-01-04 14:34:37 -0800486 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700487 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800488}
489
Colin Crossb98c8b02016-07-29 13:44:28 -0700490func (c *Module) toolchain(ctx BaseModuleContext) config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800491 if c.cachedToolchain == nil {
Colin Crossb98c8b02016-07-29 13:44:28 -0700492 c.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
Colin Cross3f40fa42015-01-30 17:27:36 -0800493 }
Colin Crossca860ac2016-01-04 14:34:37 -0800494 return c.cachedToolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800495}
496
Colin Crossca860ac2016-01-04 14:34:37 -0800497func (c *Module) begin(ctx BaseModuleContext) {
498 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700499 c.compiler.compilerInit(ctx)
Colin Cross21b9a242015-03-24 14:15:58 -0700500 }
Colin Crossca860ac2016-01-04 14:34:37 -0800501 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700502 c.linker.linkerInit(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800503 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700504 if c.stl != nil {
505 c.stl.begin(ctx)
506 }
Colin Cross16b23492016-01-06 14:41:07 -0800507 if c.sanitize != nil {
508 c.sanitize.begin(ctx)
509 }
Colin Crossca860ac2016-01-04 14:34:37 -0800510 for _, feature := range c.features {
511 feature.begin(ctx)
512 }
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700513 if ctx.sdk() {
Dan Willemsend2ede872016-11-18 14:54:24 -0800514 if ctx.vndk() {
515 ctx.PropertyErrorf("use_vndk",
516 "sdk_version and use_vndk cannot be used at the same time")
517 }
518
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700519 version, err := normalizeNdkApiLevel(ctx.sdkVersion(), ctx.Arch())
520 if err != nil {
521 ctx.PropertyErrorf("sdk_version", err.Error())
522 }
Dan Albert90f7a4d2016-11-08 14:34:24 -0800523 c.Properties.Sdk_version = version
Dan Willemsend2ede872016-11-18 14:54:24 -0800524 } else if ctx.vndk() {
525 version, err := normalizeNdkApiLevel(ctx.DeviceConfig().VndkVersion(), ctx.Arch())
526 if err != nil {
527 ctx.ModuleErrorf("Bad BOARD_VNDK_VERSION: %s", err.Error())
528 }
529 c.Properties.Vndk_version = version
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700530 }
Colin Crossca860ac2016-01-04 14:34:37 -0800531}
532
Colin Crossc99deeb2016-04-11 15:06:20 -0700533func (c *Module) deps(ctx BaseModuleContext) Deps {
534 deps := Deps{}
535
536 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700537 deps = c.compiler.compilerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700538 }
539 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700540 deps = c.linker.linkerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700541 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700542 if c.stl != nil {
543 deps = c.stl.deps(ctx, deps)
544 }
Colin Cross16b23492016-01-06 14:41:07 -0800545 if c.sanitize != nil {
546 deps = c.sanitize.deps(ctx, deps)
547 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700548 for _, feature := range c.features {
549 deps = feature.deps(ctx, deps)
550 }
551
552 deps.WholeStaticLibs = lastUniqueElements(deps.WholeStaticLibs)
553 deps.StaticLibs = lastUniqueElements(deps.StaticLibs)
554 deps.LateStaticLibs = lastUniqueElements(deps.LateStaticLibs)
555 deps.SharedLibs = lastUniqueElements(deps.SharedLibs)
556 deps.LateSharedLibs = lastUniqueElements(deps.LateSharedLibs)
557
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700558 for _, lib := range deps.ReexportSharedLibHeaders {
559 if !inList(lib, deps.SharedLibs) {
560 ctx.PropertyErrorf("export_shared_lib_headers", "Shared library not in shared_libs: '%s'", lib)
561 }
562 }
563
564 for _, lib := range deps.ReexportStaticLibHeaders {
565 if !inList(lib, deps.StaticLibs) {
566 ctx.PropertyErrorf("export_static_lib_headers", "Static library not in static_libs: '%s'", lib)
567 }
568 }
569
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700570 for _, gen := range deps.ReexportGeneratedHeaders {
571 if !inList(gen, deps.GeneratedHeaders) {
572 ctx.PropertyErrorf("export_generated_headers", "Generated header module not in generated_headers: '%s'", gen)
573 }
574 }
575
Colin Crossc99deeb2016-04-11 15:06:20 -0700576 return deps
577}
578
Dan Albert7e9d2952016-08-04 13:02:36 -0700579func (c *Module) beginMutator(actx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800580 ctx := &baseModuleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700581 BaseContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800582 moduleContextImpl: moduleContextImpl{
583 mod: c,
584 },
585 }
586 ctx.ctx = ctx
587
Colin Crossca860ac2016-01-04 14:34:37 -0800588 c.begin(ctx)
Dan Albert7e9d2952016-08-04 13:02:36 -0700589}
590
Colin Cross1e676be2016-10-12 14:38:15 -0700591func (c *Module) DepsMutator(actx android.BottomUpMutatorContext) {
592 if !c.Enabled() {
593 return
594 }
595
Dan Albert7e9d2952016-08-04 13:02:36 -0700596 ctx := &baseModuleContext{
597 BaseContext: actx,
598 moduleContextImpl: moduleContextImpl{
599 mod: c,
600 },
601 }
602 ctx.ctx = ctx
Colin Crossca860ac2016-01-04 14:34:37 -0800603
Colin Crossc99deeb2016-04-11 15:06:20 -0700604 deps := c.deps(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800605
Colin Crossb5bc4b42016-07-11 16:11:59 -0700606 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.SharedLibs...)
607 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.LateSharedLibs...)
Dan Willemsen72d39932016-07-08 23:23:48 -0700608
Dan Albert914449f2016-06-17 16:45:24 -0700609 variantNdkLibs := []string{}
610 variantLateNdkLibs := []string{}
Dan Willemsend2ede872016-11-18 14:54:24 -0800611 if ctx.sdk() || ctx.vndk() {
Dan Albert914449f2016-06-17 16:45:24 -0700612 version := ctx.sdkVersion()
Dan Willemsen72d39932016-07-08 23:23:48 -0700613
Dan Albert914449f2016-06-17 16:45:24 -0700614 // Rewrites the names of shared libraries into the names of the NDK
615 // libraries where appropriate. This returns two slices.
616 //
617 // The first is a list of non-variant shared libraries (either rewritten
618 // NDK libraries to the modules in prebuilts/ndk, or not rewritten
619 // because they are not NDK libraries).
620 //
621 // The second is a list of ndk_library modules. These need to be
622 // separated because they are a variation dependency and must be added
623 // in a different manner.
624 rewriteNdkLibs := func(list []string) ([]string, []string) {
625 variantLibs := []string{}
626 nonvariantLibs := []string{}
627 for _, entry := range list {
Dan Willemsen72d39932016-07-08 23:23:48 -0700628 if inList(entry, ndkPrebuiltSharedLibraries) {
Dan Albert914449f2016-06-17 16:45:24 -0700629 if !inList(entry, ndkMigratedLibs) {
630 nonvariantLibs = append(nonvariantLibs, entry+".ndk."+version)
631 } else {
632 variantLibs = append(variantLibs, entry+ndkLibrarySuffix)
633 }
634 } else {
635 nonvariantLibs = append(variantLibs, entry)
Dan Willemsen72d39932016-07-08 23:23:48 -0700636 }
637 }
Dan Albert914449f2016-06-17 16:45:24 -0700638 return nonvariantLibs, variantLibs
Dan Willemsen72d39932016-07-08 23:23:48 -0700639 }
640
Dan Albert914449f2016-06-17 16:45:24 -0700641 deps.SharedLibs, variantNdkLibs = rewriteNdkLibs(deps.SharedLibs)
642 deps.LateSharedLibs, variantLateNdkLibs = rewriteNdkLibs(deps.LateSharedLibs)
Dan Willemsen72d39932016-07-08 23:23:48 -0700643 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700644
645 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, wholeStaticDepTag,
646 deps.WholeStaticLibs...)
647
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700648 for _, lib := range deps.StaticLibs {
649 depTag := staticDepTag
650 if inList(lib, deps.ReexportStaticLibHeaders) {
651 depTag = staticExportDepTag
652 }
Colin Cross15a0d462016-07-14 14:49:58 -0700653 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700654 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700655
656 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, lateStaticDepTag,
657 deps.LateStaticLibs...)
658
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700659 for _, lib := range deps.SharedLibs {
660 depTag := sharedDepTag
661 if inList(lib, deps.ReexportSharedLibHeaders) {
662 depTag = sharedExportDepTag
663 }
Colin Cross15a0d462016-07-14 14:49:58 -0700664 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700665 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700666
667 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, lateSharedDepTag,
668 deps.LateSharedLibs...)
669
Colin Cross68861832016-07-08 10:41:41 -0700670 actx.AddDependency(c, genSourceDepTag, deps.GeneratedSources...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700671
672 for _, gen := range deps.GeneratedHeaders {
673 depTag := genHeaderDepTag
674 if inList(gen, deps.ReexportGeneratedHeaders) {
675 depTag = genHeaderExportDepTag
676 }
677 actx.AddDependency(c, depTag, gen)
678 }
Dan Willemsenb40aab62016-04-20 14:21:14 -0700679
Colin Cross68861832016-07-08 10:41:41 -0700680 actx.AddDependency(c, objDepTag, deps.ObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700681
682 if deps.CrtBegin != "" {
Colin Cross68861832016-07-08 10:41:41 -0700683 actx.AddDependency(c, crtBeginDepTag, deps.CrtBegin)
Colin Crossca860ac2016-01-04 14:34:37 -0800684 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700685 if deps.CrtEnd != "" {
Colin Cross68861832016-07-08 10:41:41 -0700686 actx.AddDependency(c, crtEndDepTag, deps.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700687 }
Dan Albert914449f2016-06-17 16:45:24 -0700688
689 version := ctx.sdkVersion()
690 actx.AddVariationDependencies([]blueprint.Variation{
691 {"ndk_api", version}, {"link", "shared"}}, ndkStubDepTag, variantNdkLibs...)
692 actx.AddVariationDependencies([]blueprint.Variation{
693 {"ndk_api", version}, {"link", "shared"}}, ndkLateStubDepTag, variantLateNdkLibs...)
Colin Cross6362e272015-10-29 15:25:03 -0700694}
Colin Cross21b9a242015-03-24 14:15:58 -0700695
Dan Albert7e9d2952016-08-04 13:02:36 -0700696func beginMutator(ctx android.BottomUpMutatorContext) {
697 if c, ok := ctx.Module().(*Module); ok && c.Enabled() {
698 c.beginMutator(ctx)
699 }
700}
701
Colin Crossca860ac2016-01-04 14:34:37 -0800702func (c *Module) clang(ctx BaseModuleContext) bool {
703 clang := Bool(c.Properties.Clang)
704
705 if c.Properties.Clang == nil {
706 if ctx.Host() {
707 clang = true
708 }
709
710 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
711 clang = true
712 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800713 }
Colin Cross28344522015-04-22 13:07:53 -0700714
Colin Crossca860ac2016-01-04 14:34:37 -0800715 if !c.toolchain(ctx).ClangSupported() {
716 clang = false
717 }
718
719 return clang
720}
721
Colin Crossc99deeb2016-04-11 15:06:20 -0700722// Convert dependencies to paths. Returns a PathDeps containing paths
Colin Cross635c3b02016-05-18 15:37:25 -0700723func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
Colin Crossca860ac2016-01-04 14:34:37 -0800724 var depPaths PathDeps
Colin Crossca860ac2016-01-04 14:34:37 -0800725
Dan Willemsena96ff642016-06-07 12:34:45 -0700726 // Whether a module can link to another module, taking into
727 // account NDK linking.
Dan Albert9e10cd42016-08-03 14:12:14 -0700728 checkLinkType := func(from, to *Module) {
Dan Willemsena96ff642016-06-07 12:34:45 -0700729 if from.Target().Os != android.Android {
730 // Host code is not restricted
Dan Albert9e10cd42016-08-03 14:12:14 -0700731 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700732 }
733 if from.Properties.Sdk_version == "" {
734 // Platform code can link to anything
Dan Albert9e10cd42016-08-03 14:12:14 -0700735 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700736 }
Colin Crossb916a382016-07-29 17:28:03 -0700737 if _, ok := to.linker.(*toolchainLibraryDecorator); ok {
Dan Willemsena96ff642016-06-07 12:34:45 -0700738 // These are always allowed
Dan Albert9e10cd42016-08-03 14:12:14 -0700739 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700740 }
741 if _, ok := to.linker.(*ndkPrebuiltLibraryLinker); ok {
742 // These are allowed, but don't set sdk_version
Dan Albert9e10cd42016-08-03 14:12:14 -0700743 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700744 }
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700745 if _, ok := to.linker.(*ndkPrebuiltStlLinker); ok {
746 // These are allowed, but don't set sdk_version
Dan Albert9e10cd42016-08-03 14:12:14 -0700747 return
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700748 }
Colin Crossb916a382016-07-29 17:28:03 -0700749 if _, ok := to.linker.(*stubDecorator); ok {
Dan Albert914449f2016-06-17 16:45:24 -0700750 // These aren't real libraries, but are the stub shared libraries that are included in
751 // the NDK.
Dan Albert9e10cd42016-08-03 14:12:14 -0700752 return
Dan Albert914449f2016-06-17 16:45:24 -0700753 }
Dan Albert9e10cd42016-08-03 14:12:14 -0700754 if to.Properties.Sdk_version == "" {
755 // NDK code linking to platform code is never okay.
756 ctx.ModuleErrorf("depends on non-NDK-built library %q",
757 ctx.OtherModuleName(to))
758 }
759
760 // All this point we know we have two NDK libraries, but we need to
761 // check that we're not linking against anything built against a higher
762 // API level, as it is only valid to link against older or equivalent
763 // APIs.
764
765 if from.Properties.Sdk_version == "current" {
766 // Current can link against anything.
767 return
768 } else if to.Properties.Sdk_version == "current" {
769 // Current can't be linked against by anything else.
770 ctx.ModuleErrorf("links %q built against newer API version %q",
771 ctx.OtherModuleName(to), "current")
772 }
773
774 fromApi, err := strconv.Atoi(from.Properties.Sdk_version)
775 if err != nil {
776 ctx.PropertyErrorf("sdk_version",
777 "Invalid sdk_version value (must be int): %q",
778 from.Properties.Sdk_version)
779 }
780 toApi, err := strconv.Atoi(to.Properties.Sdk_version)
781 if err != nil {
782 ctx.PropertyErrorf("sdk_version",
783 "Invalid sdk_version value (must be int): %q",
784 to.Properties.Sdk_version)
785 }
786
787 if toApi > fromApi {
788 ctx.ModuleErrorf("links %q built against newer API version %q",
789 ctx.OtherModuleName(to), to.Properties.Sdk_version)
790 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700791 }
792
Colin Crossc99deeb2016-04-11 15:06:20 -0700793 ctx.VisitDirectDeps(func(m blueprint.Module) {
794 name := ctx.OtherModuleName(m)
795 tag := ctx.OtherModuleDependencyTag(m)
Colin Crossca860ac2016-01-04 14:34:37 -0800796
Colin Cross635c3b02016-05-18 15:37:25 -0700797 a, _ := m.(android.Module)
Colin Crossc99deeb2016-04-11 15:06:20 -0700798 if a == nil {
799 ctx.ModuleErrorf("module %q not an android module", name)
800 return
Colin Crossca860ac2016-01-04 14:34:37 -0800801 }
Colin Crossca860ac2016-01-04 14:34:37 -0800802
Dan Willemsena96ff642016-06-07 12:34:45 -0700803 cc, _ := m.(*Module)
804 if cc == nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700805 switch tag {
Colin Cross635c3b02016-05-18 15:37:25 -0700806 case android.DefaultsDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700807 case genSourceDepTag:
808 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
809 depPaths.GeneratedSources = append(depPaths.GeneratedSources,
810 genRule.GeneratedSourceFiles()...)
811 } else {
812 ctx.ModuleErrorf("module %q is not a gensrcs or genrule", name)
813 }
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700814 case genHeaderDepTag, genHeaderExportDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700815 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
816 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders,
817 genRule.GeneratedSourceFiles()...)
Colin Cross5ed99c62016-11-22 12:55:55 -0800818 flags := includeDirsToFlags(genRule.GeneratedHeaderDirs())
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700819 depPaths.Flags = append(depPaths.Flags, flags)
820 if tag == genHeaderExportDepTag {
821 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700822 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps,
823 genRule.GeneratedSourceFiles()...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700824 }
Dan Willemsenb40aab62016-04-20 14:21:14 -0700825 } else {
826 ctx.ModuleErrorf("module %q is not a genrule", name)
827 }
828 default:
Colin Crossc99deeb2016-04-11 15:06:20 -0700829 ctx.ModuleErrorf("depends on non-cc module %q", name)
Colin Crossca860ac2016-01-04 14:34:37 -0800830 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700831 return
832 }
833
834 if !a.Enabled() {
835 ctx.ModuleErrorf("depends on disabled module %q", name)
836 return
837 }
838
Colin Crossa1ad8d12016-06-01 17:09:44 -0700839 if a.Target().Os != ctx.Os() {
840 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), name)
841 return
842 }
843
844 if a.Target().Arch.ArchType != ctx.Arch().ArchType {
845 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700846 return
847 }
848
Colin Crossc99deeb2016-04-11 15:06:20 -0700849 if tag == reuseObjTag {
Colin Crossbba99042016-11-23 15:45:05 -0800850 if l, ok := cc.compiler.(libraryInterface); ok {
851 depPaths.Objs = depPaths.Objs.Append(l.reuseObjs())
852 return
853 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700854 }
855
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700856 if t, ok := tag.(dependencyTag); ok && t.library {
Dan Willemsena96ff642016-06-07 12:34:45 -0700857 if i, ok := cc.linker.(exportedFlagsProducer); ok {
Dan Willemsen76f08272016-07-09 00:14:08 -0700858 flags := i.exportedFlags()
Dan Willemsen847dcc72016-09-29 12:13:36 -0700859 deps := i.exportedFlagsDeps()
Dan Willemsen76f08272016-07-09 00:14:08 -0700860 depPaths.Flags = append(depPaths.Flags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700861 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders, deps...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700862
863 if t.reexportFlags {
Dan Willemsen76f08272016-07-09 00:14:08 -0700864 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700865 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps, deps...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700866 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700867 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700868
Dan Albert9e10cd42016-08-03 14:12:14 -0700869 checkLinkType(c, cc)
Colin Crossc99deeb2016-04-11 15:06:20 -0700870 }
871
Colin Cross26c34ed2016-09-30 17:10:16 -0700872 var ptr *android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -0700873 var depPtr *android.Paths
Colin Crossc99deeb2016-04-11 15:06:20 -0700874
Colin Cross26c34ed2016-09-30 17:10:16 -0700875 linkFile := cc.outputFile
876 depFile := android.OptionalPath{}
877
Colin Crossc99deeb2016-04-11 15:06:20 -0700878 switch tag {
Dan Albert914449f2016-06-17 16:45:24 -0700879 case ndkStubDepTag, sharedDepTag, sharedExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700880 ptr = &depPaths.SharedLibs
881 depPtr = &depPaths.SharedLibsDeps
882 depFile = cc.linker.(libraryInterface).toc()
Dan Albert914449f2016-06-17 16:45:24 -0700883 case lateSharedDepTag, ndkLateStubDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700884 ptr = &depPaths.LateSharedLibs
885 depPtr = &depPaths.LateSharedLibsDeps
886 depFile = cc.linker.(libraryInterface).toc()
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700887 case staticDepTag, staticExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700888 ptr = &depPaths.StaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -0700889 case lateStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700890 ptr = &depPaths.LateStaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -0700891 case wholeStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700892 ptr = &depPaths.WholeStaticLibs
Colin Crossb916a382016-07-29 17:28:03 -0700893 staticLib, ok := cc.linker.(libraryInterface)
894 if !ok || !staticLib.static() {
Dan Willemsena96ff642016-06-07 12:34:45 -0700895 ctx.ModuleErrorf("module %q not a static library", name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700896 return
897 }
898
899 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
900 postfix := " (required by " + ctx.OtherModuleName(m) + ")"
901 for i := range missingDeps {
902 missingDeps[i] += postfix
903 }
904 ctx.AddMissingDependencies(missingDeps)
905 }
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700906 depPaths.WholeStaticLibObjs = depPaths.WholeStaticLibObjs.Append(staticLib.objs())
Colin Crossc99deeb2016-04-11 15:06:20 -0700907 case objDepTag:
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700908 depPaths.Objs.objFiles = append(depPaths.Objs.objFiles, linkFile.Path())
Colin Crossc99deeb2016-04-11 15:06:20 -0700909 case crtBeginDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700910 depPaths.CrtBegin = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -0700911 case crtEndDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700912 depPaths.CrtEnd = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -0700913 }
914
Colin Cross26c34ed2016-09-30 17:10:16 -0700915 if ptr != nil {
Colin Crossce75d2c2016-10-06 16:12:58 -0700916 if !linkFile.Valid() {
917 ctx.ModuleErrorf("module %q missing output file", name)
918 return
919 }
Colin Cross26c34ed2016-09-30 17:10:16 -0700920 *ptr = append(*ptr, linkFile.Path())
921 }
922
Colin Crossc99deeb2016-04-11 15:06:20 -0700923 if depPtr != nil {
Colin Cross26c34ed2016-09-30 17:10:16 -0700924 dep := depFile
925 if !dep.Valid() {
926 dep = linkFile
927 }
928 *depPtr = append(*depPtr, dep.Path())
Colin Crossca860ac2016-01-04 14:34:37 -0800929 }
930 })
931
932 return depPaths
933}
934
935func (c *Module) InstallInData() bool {
936 if c.installer == nil {
937 return false
938 }
Colin Cross94610402016-08-29 13:41:32 -0700939 if c.sanitize != nil && c.sanitize.inData() {
940 return true
941 }
Colin Crossca860ac2016-01-04 14:34:37 -0800942 return c.installer.inData()
943}
944
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700945func (c *Module) HostToolPath() android.OptionalPath {
946 if c.installer == nil {
947 return android.OptionalPath{}
948 }
949 return c.installer.hostToolPath()
950}
951
Colin Cross2ba19d92015-05-07 15:44:20 -0700952//
Colin Crosscfad1192015-11-02 16:43:11 -0800953// Defaults
954//
Colin Crossca860ac2016-01-04 14:34:37 -0800955type Defaults struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700956 android.ModuleBase
957 android.DefaultsModule
Colin Crosscfad1192015-11-02 16:43:11 -0800958}
959
Colin Cross635c3b02016-05-18 15:37:25 -0700960func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -0800961}
962
Colin Cross1e676be2016-10-12 14:38:15 -0700963func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
964}
965
Colin Crossca860ac2016-01-04 14:34:37 -0800966func defaultsFactory() (blueprint.Module, []interface{}) {
Colin Crosse1d764e2016-08-18 14:18:32 -0700967 return DefaultsFactory()
968}
969
970func DefaultsFactory(props ...interface{}) (blueprint.Module, []interface{}) {
Colin Crossca860ac2016-01-04 14:34:37 -0800971 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -0800972
Colin Crosse1d764e2016-08-18 14:18:32 -0700973 props = append(props,
Colin Crossca860ac2016-01-04 14:34:37 -0800974 &BaseProperties{},
975 &BaseCompilerProperties{},
976 &BaseLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -0700977 &LibraryProperties{},
Colin Cross919281a2016-04-05 16:42:05 -0700978 &FlagExporterProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -0800979 &BinaryLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -0700980 &TestProperties{},
981 &TestBinaryProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -0800982 &UnusedProperties{},
983 &StlProperties{},
Colin Cross16b23492016-01-06 14:41:07 -0800984 &SanitizeProperties{},
Colin Cross665dce92016-04-28 14:50:03 -0700985 &StripProperties{},
Dan Willemsen7424d612016-09-01 13:45:39 -0700986 &InstallerProperties{},
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700987 &TidyProperties{},
Colin Crosse1d764e2016-08-18 14:18:32 -0700988 )
Colin Crosscfad1192015-11-02 16:43:11 -0800989
Colin Crosse1d764e2016-08-18 14:18:32 -0700990 return android.InitDefaultsModule(module, module, props...)
Colin Crosscfad1192015-11-02 16:43:11 -0800991}
992
Colin Cross74d1ec02015-04-28 13:30:13 -0700993// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
994// modifies the slice contents in place, and returns a subslice of the original slice
995func lastUniqueElements(list []string) []string {
996 totalSkip := 0
997 for i := len(list) - 1; i >= totalSkip; i-- {
998 skip := 0
999 for j := i - 1; j >= totalSkip; j-- {
1000 if list[i] == list[j] {
1001 skip++
1002 } else {
1003 list[j+skip] = list[j]
1004 }
1005 }
1006 totalSkip += skip
1007 }
1008 return list[totalSkip:]
1009}
Colin Cross06a931b2015-10-28 17:23:31 -07001010
1011var Bool = proptools.Bool