blob: 529a3297277326b6ca3d498248431ac6ef308340 [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
Colin Cross635c3b02016-05-18 15:37:25 -070079 ObjFiles android.Paths
80 WholeStaticLibObjFiles android.Paths
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
100 LdFlags []string // Flags that apply to linker command lines
Colin Cross16b23492016-01-06 14:41:07 -0800101 libFlags []string // Flags to add libraries early to the link order
Colin Cross28344522015-04-22 13:07:53 -0700102
Colin Crossb98c8b02016-07-29 13:44:28 -0700103 Toolchain config.Toolchain
Colin Cross28344522015-04-22 13:07:53 -0700104 Clang bool
Colin Crossca860ac2016-01-04 14:34:37 -0800105
106 RequiredInstructionSet string
Colin Cross16b23492016-01-06 14:41:07 -0800107 DynamicLinker string
108
Colin Cross635c3b02016-05-18 15:37:25 -0700109 CFlagsDeps android.Paths // Files depended on by compiler flags
Colin Crossc472d572015-03-17 15:06:21 -0700110}
111
Colin Cross81413472016-04-11 14:37:39 -0700112type ObjectLinkerProperties struct {
113 // names of other cc_object modules to link into this module using partial linking
114 Objs []string `android:"arch_variant"`
115}
116
Colin Crossca860ac2016-01-04 14:34:37 -0800117// Properties used to compile all C or C++ modules
118type BaseProperties struct {
119 // compile module with clang instead of gcc
120 Clang *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700121
122 // Minimum sdk version supported when compiling against the ndk
123 Sdk_version string
124
Colin Crossca860ac2016-01-04 14:34:37 -0800125 // don't insert default compiler flags into asflags, cflags,
126 // cppflags, conlyflags, ldflags, or include_dirs
127 No_default_compiler_flags *bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700128
129 AndroidMkSharedLibs []string `blueprint:"mutated"`
Colin Crossbc6fb162016-05-24 15:39:04 -0700130 HideFromMake bool `blueprint:"mutated"`
Colin Crossb0f28952016-09-19 16:46:53 -0700131 PreventInstall bool `blueprint:"mutated"`
Colin Crossca860ac2016-01-04 14:34:37 -0800132}
133
Colin Crossca860ac2016-01-04 14:34:37 -0800134type UnusedProperties struct {
Colin Cross21b481b2016-04-15 16:27:17 -0700135 Native_coverage *bool
Colin Cross21b481b2016-04-15 16:27:17 -0700136 Tags []string
Colin Crosscfad1192015-11-02 16:43:11 -0800137}
138
Colin Crossca860ac2016-01-04 14:34:37 -0800139type ModuleContextIntf interface {
Colin Crossca860ac2016-01-04 14:34:37 -0800140 static() bool
141 staticBinary() bool
142 clang() bool
Colin Crossb98c8b02016-07-29 13:44:28 -0700143 toolchain() config.Toolchain
Colin Crossca860ac2016-01-04 14:34:37 -0800144 noDefaultCompilerFlags() bool
145 sdk() bool
146 sdkVersion() string
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700147 selectedStl() string
Colin Crossce75d2c2016-10-06 16:12:58 -0700148 baseModuleName() string
Colin Crossca860ac2016-01-04 14:34:37 -0800149}
150
151type ModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700152 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800153 ModuleContextIntf
154}
155
156type BaseModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700157 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800158 ModuleContextIntf
159}
160
Colin Crossca860ac2016-01-04 14:34:37 -0800161type feature interface {
162 begin(ctx BaseModuleContext)
163 deps(ctx BaseModuleContext, deps Deps) Deps
164 flags(ctx ModuleContext, flags Flags) Flags
165 props() []interface{}
166}
167
168type compiler interface {
Colin Cross42742b82016-08-01 13:20:05 -0700169 compilerInit(ctx BaseModuleContext)
170 compilerDeps(ctx BaseModuleContext, deps Deps) Deps
171 compilerFlags(ctx ModuleContext, flags Flags) Flags
172 compilerProps() []interface{}
173
Colin Cross76fada02016-07-27 10:31:13 -0700174 appendCflags([]string)
175 appendAsflags([]string)
Colin Cross635c3b02016-05-18 15:37:25 -0700176 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -0800177}
178
179type linker interface {
Colin Cross42742b82016-08-01 13:20:05 -0700180 linkerInit(ctx BaseModuleContext)
181 linkerDeps(ctx BaseModuleContext, deps Deps) Deps
182 linkerFlags(ctx ModuleContext, flags Flags) Flags
183 linkerProps() []interface{}
184
Colin Cross635c3b02016-05-18 15:37:25 -0700185 link(ctx ModuleContext, flags Flags, deps PathDeps, objFiles android.Paths) android.Path
Colin Cross76fada02016-07-27 10:31:13 -0700186 appendLdflags([]string)
Colin Crossca860ac2016-01-04 14:34:37 -0800187}
188
189type installer interface {
Colin Cross42742b82016-08-01 13:20:05 -0700190 installerProps() []interface{}
Colin Cross635c3b02016-05-18 15:37:25 -0700191 install(ctx ModuleContext, path android.Path)
Colin Crossca860ac2016-01-04 14:34:37 -0800192 inData() bool
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700193 hostToolPath() android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800194}
195
Colin Crossc99deeb2016-04-11 15:06:20 -0700196type dependencyTag struct {
197 blueprint.BaseDependencyTag
198 name string
199 library bool
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700200
201 reexportFlags bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700202}
203
204var (
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700205 sharedDepTag = dependencyTag{name: "shared", library: true}
206 sharedExportDepTag = dependencyTag{name: "shared", library: true, reexportFlags: true}
207 lateSharedDepTag = dependencyTag{name: "late shared", library: true}
208 staticDepTag = dependencyTag{name: "static", library: true}
209 staticExportDepTag = dependencyTag{name: "static", library: true, reexportFlags: true}
210 lateStaticDepTag = dependencyTag{name: "late static", library: true}
211 wholeStaticDepTag = dependencyTag{name: "whole static", library: true, reexportFlags: true}
212 genSourceDepTag = dependencyTag{name: "gen source"}
213 genHeaderDepTag = dependencyTag{name: "gen header"}
214 genHeaderExportDepTag = dependencyTag{name: "gen header", reexportFlags: true}
215 objDepTag = dependencyTag{name: "obj"}
216 crtBeginDepTag = dependencyTag{name: "crtbegin"}
217 crtEndDepTag = dependencyTag{name: "crtend"}
218 reuseObjTag = dependencyTag{name: "reuse objects"}
219 ndkStubDepTag = dependencyTag{name: "ndk stub", library: true}
220 ndkLateStubDepTag = dependencyTag{name: "ndk late stub", library: true}
Colin Crossc99deeb2016-04-11 15:06:20 -0700221)
222
Colin Crossca860ac2016-01-04 14:34:37 -0800223// Module contains the properties and members used by all C/C++ module types, and implements
224// the blueprint.Module interface. It delegates to compiler, linker, and installer interfaces
225// to construct the output file. Behavior can be customized with a Customizer interface
226type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700227 android.ModuleBase
228 android.DefaultableModule
Colin Crossc472d572015-03-17 15:06:21 -0700229
Colin Crossca860ac2016-01-04 14:34:37 -0800230 Properties BaseProperties
231 unused UnusedProperties
Colin Crossfa138792015-04-24 17:31:52 -0700232
Colin Crossca860ac2016-01-04 14:34:37 -0800233 // initialize before calling Init
Colin Cross635c3b02016-05-18 15:37:25 -0700234 hod android.HostOrDeviceSupported
235 multilib android.Multilib
Colin Crossc472d572015-03-17 15:06:21 -0700236
Colin Crossca860ac2016-01-04 14:34:37 -0800237 // delegates, initialize before calling Init
Colin Crossb4ce0ec2016-09-13 13:41:39 -0700238 features []feature
239 compiler compiler
240 linker linker
241 installer installer
242 stl *stl
243 sanitize *sanitize
Colin Cross16b23492016-01-06 14:41:07 -0800244
245 androidMkSharedLibDeps []string
Colin Cross74d1ec02015-04-28 13:30:13 -0700246
Colin Cross635c3b02016-05-18 15:37:25 -0700247 outputFile android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800248
Colin Crossb98c8b02016-07-29 13:44:28 -0700249 cachedToolchain config.Toolchain
Colin Crossb916a382016-07-29 17:28:03 -0700250
251 subAndroidMkOnce map[subAndroidMkProvider]bool
Colin Crossc472d572015-03-17 15:06:21 -0700252}
253
Colin Crossca860ac2016-01-04 14:34:37 -0800254func (c *Module) Init() (blueprint.Module, []interface{}) {
255 props := []interface{}{&c.Properties, &c.unused}
Colin Crossca860ac2016-01-04 14:34:37 -0800256 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700257 props = append(props, c.compiler.compilerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800258 }
259 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700260 props = append(props, c.linker.linkerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800261 }
262 if c.installer != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700263 props = append(props, c.installer.installerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800264 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700265 if c.stl != nil {
266 props = append(props, c.stl.props()...)
267 }
Colin Cross16b23492016-01-06 14:41:07 -0800268 if c.sanitize != nil {
269 props = append(props, c.sanitize.props()...)
270 }
Colin Crossca860ac2016-01-04 14:34:37 -0800271 for _, feature := range c.features {
272 props = append(props, feature.props()...)
273 }
Colin Crossc472d572015-03-17 15:06:21 -0700274
Colin Cross635c3b02016-05-18 15:37:25 -0700275 _, props = android.InitAndroidArchModule(c, c.hod, c.multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700276
Colin Cross635c3b02016-05-18 15:37:25 -0700277 return android.InitDefaultableModule(c, c, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700278}
279
Colin Crossb916a382016-07-29 17:28:03 -0700280// Returns true for dependency roots (binaries)
281// TODO(ccross): also handle dlopenable libraries
282func (c *Module) isDependencyRoot() bool {
283 if root, ok := c.linker.(interface {
284 isDependencyRoot() bool
285 }); ok {
286 return root.isDependencyRoot()
287 }
288 return false
289}
290
Colin Crossca860ac2016-01-04 14:34:37 -0800291type baseModuleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700292 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800293 moduleContextImpl
294}
295
296type moduleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700297 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800298 moduleContextImpl
299}
300
301type moduleContextImpl struct {
302 mod *Module
303 ctx BaseModuleContext
304}
305
Colin Crossca860ac2016-01-04 14:34:37 -0800306func (ctx *moduleContextImpl) clang() bool {
307 return ctx.mod.clang(ctx.ctx)
308}
309
Colin Crossb98c8b02016-07-29 13:44:28 -0700310func (ctx *moduleContextImpl) toolchain() config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800311 return ctx.mod.toolchain(ctx.ctx)
312}
313
314func (ctx *moduleContextImpl) static() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700315 if static, ok := ctx.mod.linker.(interface {
316 static() bool
317 }); ok {
318 return static.static()
Colin Crossca860ac2016-01-04 14:34:37 -0800319 }
Colin Crossb916a382016-07-29 17:28:03 -0700320 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800321}
322
323func (ctx *moduleContextImpl) staticBinary() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700324 if static, ok := ctx.mod.linker.(interface {
325 staticBinary() bool
326 }); ok {
327 return static.staticBinary()
Colin Crossca860ac2016-01-04 14:34:37 -0800328 }
Colin Crossb916a382016-07-29 17:28:03 -0700329 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800330}
331
332func (ctx *moduleContextImpl) noDefaultCompilerFlags() bool {
333 return Bool(ctx.mod.Properties.No_default_compiler_flags)
334}
335
336func (ctx *moduleContextImpl) sdk() bool {
Dan Willemsena96ff642016-06-07 12:34:45 -0700337 if ctx.ctx.Device() {
338 return ctx.mod.Properties.Sdk_version != ""
339 }
340 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800341}
342
343func (ctx *moduleContextImpl) sdkVersion() string {
Dan Willemsena96ff642016-06-07 12:34:45 -0700344 if ctx.ctx.Device() {
345 return ctx.mod.Properties.Sdk_version
346 }
347 return ""
Colin Crossca860ac2016-01-04 14:34:37 -0800348}
349
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700350func (ctx *moduleContextImpl) selectedStl() string {
351 if stl := ctx.mod.stl; stl != nil {
352 return stl.Properties.SelectedStl
353 }
354 return ""
355}
356
Colin Crossce75d2c2016-10-06 16:12:58 -0700357func (ctx *moduleContextImpl) baseModuleName() string {
358 return ctx.mod.ModuleBase.BaseModuleName()
359}
360
Colin Cross635c3b02016-05-18 15:37:25 -0700361func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800362 return &Module{
363 hod: hod,
364 multilib: multilib,
365 }
366}
367
Colin Cross635c3b02016-05-18 15:37:25 -0700368func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800369 module := newBaseModule(hod, multilib)
Colin Crossa8e07cc2016-04-04 15:07:06 -0700370 module.stl = &stl{}
Colin Cross16b23492016-01-06 14:41:07 -0800371 module.sanitize = &sanitize{}
Colin Crossca860ac2016-01-04 14:34:37 -0800372 return module
373}
374
Colin Crossce75d2c2016-10-06 16:12:58 -0700375func (c *Module) Prebuilt() *android.Prebuilt {
376 if p, ok := c.linker.(prebuiltLinkerInterface); ok {
377 return p.prebuilt()
378 }
379 return nil
380}
381
382func (c *Module) Name() string {
383 name := c.ModuleBase.Name()
384 if p, ok := c.linker.(prebuiltLinkerInterface); ok {
385 name = p.Name(name)
386 }
387 return name
388}
389
Colin Cross635c3b02016-05-18 15:37:25 -0700390func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800391 ctx := &moduleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700392 ModuleContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800393 moduleContextImpl: moduleContextImpl{
394 mod: c,
395 },
396 }
397 ctx.ctx = ctx
398
399 flags := Flags{
400 Toolchain: c.toolchain(ctx),
401 Clang: c.clang(ctx),
402 }
Colin Crossca860ac2016-01-04 14:34:37 -0800403 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700404 flags = c.compiler.compilerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800405 }
406 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700407 flags = c.linker.linkerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800408 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700409 if c.stl != nil {
410 flags = c.stl.flags(ctx, flags)
411 }
Colin Cross16b23492016-01-06 14:41:07 -0800412 if c.sanitize != nil {
413 flags = c.sanitize.flags(ctx, flags)
414 }
Colin Crossca860ac2016-01-04 14:34:37 -0800415 for _, feature := range c.features {
416 flags = feature.flags(ctx, flags)
417 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800418 if ctx.Failed() {
419 return
420 }
421
Colin Crossb98c8b02016-07-29 13:44:28 -0700422 flags.CFlags, _ = filterList(flags.CFlags, config.IllegalFlags)
423 flags.CppFlags, _ = filterList(flags.CppFlags, config.IllegalFlags)
424 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, config.IllegalFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800425
Colin Crossca860ac2016-01-04 14:34:37 -0800426 // Optimization to reduce size of build.ninja
427 // Replace the long list of flags for each file with a module-local variable
428 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
429 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
430 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
431 flags.CFlags = []string{"$cflags"}
432 flags.CppFlags = []string{"$cppflags"}
433 flags.AsFlags = []string{"$asflags"}
434
Colin Crossc99deeb2016-04-11 15:06:20 -0700435 deps := c.depsToPaths(ctx)
Colin Cross3f40fa42015-01-30 17:27:36 -0800436 if ctx.Failed() {
437 return
438 }
439
Dan Willemsen76f08272016-07-09 00:14:08 -0700440 flags.GlobalFlags = append(flags.GlobalFlags, deps.Flags...)
Colin Crossed9f8682015-03-18 17:17:35 -0700441
Colin Cross635c3b02016-05-18 15:37:25 -0700442 var objFiles android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -0800443 if c.compiler != nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700444 objFiles = c.compiler.compile(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -0800445 if ctx.Failed() {
446 return
447 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800448 }
449
Colin Crossca860ac2016-01-04 14:34:37 -0800450 if c.linker != nil {
451 outputFile := c.linker.link(ctx, flags, deps, objFiles)
452 if ctx.Failed() {
453 return
454 }
Colin Cross635c3b02016-05-18 15:37:25 -0700455 c.outputFile = android.OptionalPathForPath(outputFile)
Colin Crossce75d2c2016-10-06 16:12:58 -0700456 }
Colin Cross5049f022015-03-18 13:28:46 -0700457
Colin Crossce75d2c2016-10-06 16:12:58 -0700458 if c.installer != nil && !c.Properties.PreventInstall && c.outputFile.Valid() {
459 c.installer.install(ctx, c.outputFile.Path())
460 if ctx.Failed() {
461 return
Colin Crossca860ac2016-01-04 14:34:37 -0800462 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700463 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800464}
465
Colin Crossb98c8b02016-07-29 13:44:28 -0700466func (c *Module) toolchain(ctx BaseModuleContext) config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800467 if c.cachedToolchain == nil {
Colin Crossb98c8b02016-07-29 13:44:28 -0700468 c.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
Colin Cross3f40fa42015-01-30 17:27:36 -0800469 }
Colin Crossca860ac2016-01-04 14:34:37 -0800470 return c.cachedToolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800471}
472
Colin Crossca860ac2016-01-04 14:34:37 -0800473func (c *Module) begin(ctx BaseModuleContext) {
474 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700475 c.compiler.compilerInit(ctx)
Colin Cross21b9a242015-03-24 14:15:58 -0700476 }
Colin Crossca860ac2016-01-04 14:34:37 -0800477 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700478 c.linker.linkerInit(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800479 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700480 if c.stl != nil {
481 c.stl.begin(ctx)
482 }
Colin Cross16b23492016-01-06 14:41:07 -0800483 if c.sanitize != nil {
484 c.sanitize.begin(ctx)
485 }
Colin Crossca860ac2016-01-04 14:34:37 -0800486 for _, feature := range c.features {
487 feature.begin(ctx)
488 }
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700489 if ctx.sdk() {
490 version, err := normalizeNdkApiLevel(ctx.sdkVersion(), ctx.Arch())
491 if err != nil {
492 ctx.PropertyErrorf("sdk_version", err.Error())
493 }
494 c.Properties.Sdk_version = strconv.Itoa(version)
495 }
Colin Crossca860ac2016-01-04 14:34:37 -0800496}
497
Colin Crossc99deeb2016-04-11 15:06:20 -0700498func (c *Module) deps(ctx BaseModuleContext) Deps {
499 deps := Deps{}
500
501 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700502 deps = c.compiler.compilerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700503 }
504 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700505 deps = c.linker.linkerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700506 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700507 if c.stl != nil {
508 deps = c.stl.deps(ctx, deps)
509 }
Colin Cross16b23492016-01-06 14:41:07 -0800510 if c.sanitize != nil {
511 deps = c.sanitize.deps(ctx, deps)
512 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700513 for _, feature := range c.features {
514 deps = feature.deps(ctx, deps)
515 }
516
517 deps.WholeStaticLibs = lastUniqueElements(deps.WholeStaticLibs)
518 deps.StaticLibs = lastUniqueElements(deps.StaticLibs)
519 deps.LateStaticLibs = lastUniqueElements(deps.LateStaticLibs)
520 deps.SharedLibs = lastUniqueElements(deps.SharedLibs)
521 deps.LateSharedLibs = lastUniqueElements(deps.LateSharedLibs)
522
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700523 for _, lib := range deps.ReexportSharedLibHeaders {
524 if !inList(lib, deps.SharedLibs) {
525 ctx.PropertyErrorf("export_shared_lib_headers", "Shared library not in shared_libs: '%s'", lib)
526 }
527 }
528
529 for _, lib := range deps.ReexportStaticLibHeaders {
530 if !inList(lib, deps.StaticLibs) {
531 ctx.PropertyErrorf("export_static_lib_headers", "Static library not in static_libs: '%s'", lib)
532 }
533 }
534
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700535 for _, gen := range deps.ReexportGeneratedHeaders {
536 if !inList(gen, deps.GeneratedHeaders) {
537 ctx.PropertyErrorf("export_generated_headers", "Generated header module not in generated_headers: '%s'", gen)
538 }
539 }
540
Colin Crossc99deeb2016-04-11 15:06:20 -0700541 return deps
542}
543
Dan Albert7e9d2952016-08-04 13:02:36 -0700544func (c *Module) beginMutator(actx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800545 ctx := &baseModuleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700546 BaseContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800547 moduleContextImpl: moduleContextImpl{
548 mod: c,
549 },
550 }
551 ctx.ctx = ctx
552
Colin Crossca860ac2016-01-04 14:34:37 -0800553 c.begin(ctx)
Dan Albert7e9d2952016-08-04 13:02:36 -0700554}
555
Colin Cross1e676be2016-10-12 14:38:15 -0700556func (c *Module) DepsMutator(actx android.BottomUpMutatorContext) {
557 if !c.Enabled() {
558 return
559 }
560
Dan Albert7e9d2952016-08-04 13:02:36 -0700561 ctx := &baseModuleContext{
562 BaseContext: actx,
563 moduleContextImpl: moduleContextImpl{
564 mod: c,
565 },
566 }
567 ctx.ctx = ctx
Colin Crossca860ac2016-01-04 14:34:37 -0800568
Colin Crossc99deeb2016-04-11 15:06:20 -0700569 deps := c.deps(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800570
Colin Crossb5bc4b42016-07-11 16:11:59 -0700571 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.SharedLibs...)
572 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.LateSharedLibs...)
Dan Willemsen72d39932016-07-08 23:23:48 -0700573
Dan Albert914449f2016-06-17 16:45:24 -0700574 variantNdkLibs := []string{}
575 variantLateNdkLibs := []string{}
Dan Willemsen72d39932016-07-08 23:23:48 -0700576 if ctx.sdk() {
Dan Albert914449f2016-06-17 16:45:24 -0700577 version := ctx.sdkVersion()
Dan Willemsen72d39932016-07-08 23:23:48 -0700578
Dan Albert914449f2016-06-17 16:45:24 -0700579 // Rewrites the names of shared libraries into the names of the NDK
580 // libraries where appropriate. This returns two slices.
581 //
582 // The first is a list of non-variant shared libraries (either rewritten
583 // NDK libraries to the modules in prebuilts/ndk, or not rewritten
584 // because they are not NDK libraries).
585 //
586 // The second is a list of ndk_library modules. These need to be
587 // separated because they are a variation dependency and must be added
588 // in a different manner.
589 rewriteNdkLibs := func(list []string) ([]string, []string) {
590 variantLibs := []string{}
591 nonvariantLibs := []string{}
592 for _, entry := range list {
Dan Willemsen72d39932016-07-08 23:23:48 -0700593 if inList(entry, ndkPrebuiltSharedLibraries) {
Dan Albert914449f2016-06-17 16:45:24 -0700594 if !inList(entry, ndkMigratedLibs) {
595 nonvariantLibs = append(nonvariantLibs, entry+".ndk."+version)
596 } else {
597 variantLibs = append(variantLibs, entry+ndkLibrarySuffix)
598 }
599 } else {
600 nonvariantLibs = append(variantLibs, entry)
Dan Willemsen72d39932016-07-08 23:23:48 -0700601 }
602 }
Dan Albert914449f2016-06-17 16:45:24 -0700603 return nonvariantLibs, variantLibs
Dan Willemsen72d39932016-07-08 23:23:48 -0700604 }
605
Dan Albert914449f2016-06-17 16:45:24 -0700606 deps.SharedLibs, variantNdkLibs = rewriteNdkLibs(deps.SharedLibs)
607 deps.LateSharedLibs, variantLateNdkLibs = rewriteNdkLibs(deps.LateSharedLibs)
Dan Willemsen72d39932016-07-08 23:23:48 -0700608 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700609
610 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, wholeStaticDepTag,
611 deps.WholeStaticLibs...)
612
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700613 for _, lib := range deps.StaticLibs {
614 depTag := staticDepTag
615 if inList(lib, deps.ReexportStaticLibHeaders) {
616 depTag = staticExportDepTag
617 }
Colin Cross15a0d462016-07-14 14:49:58 -0700618 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700619 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700620
621 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, lateStaticDepTag,
622 deps.LateStaticLibs...)
623
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700624 for _, lib := range deps.SharedLibs {
625 depTag := sharedDepTag
626 if inList(lib, deps.ReexportSharedLibHeaders) {
627 depTag = sharedExportDepTag
628 }
Colin Cross15a0d462016-07-14 14:49:58 -0700629 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700630 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700631
632 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, lateSharedDepTag,
633 deps.LateSharedLibs...)
634
Colin Cross68861832016-07-08 10:41:41 -0700635 actx.AddDependency(c, genSourceDepTag, deps.GeneratedSources...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700636
637 for _, gen := range deps.GeneratedHeaders {
638 depTag := genHeaderDepTag
639 if inList(gen, deps.ReexportGeneratedHeaders) {
640 depTag = genHeaderExportDepTag
641 }
642 actx.AddDependency(c, depTag, gen)
643 }
Dan Willemsenb40aab62016-04-20 14:21:14 -0700644
Colin Cross68861832016-07-08 10:41:41 -0700645 actx.AddDependency(c, objDepTag, deps.ObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700646
647 if deps.CrtBegin != "" {
Colin Cross68861832016-07-08 10:41:41 -0700648 actx.AddDependency(c, crtBeginDepTag, deps.CrtBegin)
Colin Crossca860ac2016-01-04 14:34:37 -0800649 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700650 if deps.CrtEnd != "" {
Colin Cross68861832016-07-08 10:41:41 -0700651 actx.AddDependency(c, crtEndDepTag, deps.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700652 }
Dan Albert914449f2016-06-17 16:45:24 -0700653
654 version := ctx.sdkVersion()
655 actx.AddVariationDependencies([]blueprint.Variation{
656 {"ndk_api", version}, {"link", "shared"}}, ndkStubDepTag, variantNdkLibs...)
657 actx.AddVariationDependencies([]blueprint.Variation{
658 {"ndk_api", version}, {"link", "shared"}}, ndkLateStubDepTag, variantLateNdkLibs...)
Colin Cross6362e272015-10-29 15:25:03 -0700659}
Colin Cross21b9a242015-03-24 14:15:58 -0700660
Dan Albert7e9d2952016-08-04 13:02:36 -0700661func beginMutator(ctx android.BottomUpMutatorContext) {
662 if c, ok := ctx.Module().(*Module); ok && c.Enabled() {
663 c.beginMutator(ctx)
664 }
665}
666
Colin Crossca860ac2016-01-04 14:34:37 -0800667func (c *Module) clang(ctx BaseModuleContext) bool {
668 clang := Bool(c.Properties.Clang)
669
670 if c.Properties.Clang == nil {
671 if ctx.Host() {
672 clang = true
673 }
674
675 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
676 clang = true
677 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800678 }
Colin Cross28344522015-04-22 13:07:53 -0700679
Colin Crossca860ac2016-01-04 14:34:37 -0800680 if !c.toolchain(ctx).ClangSupported() {
681 clang = false
682 }
683
684 return clang
685}
686
Colin Crossc99deeb2016-04-11 15:06:20 -0700687// Convert dependencies to paths. Returns a PathDeps containing paths
Colin Cross635c3b02016-05-18 15:37:25 -0700688func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
Colin Crossca860ac2016-01-04 14:34:37 -0800689 var depPaths PathDeps
Colin Crossca860ac2016-01-04 14:34:37 -0800690
Dan Willemsena96ff642016-06-07 12:34:45 -0700691 // Whether a module can link to another module, taking into
692 // account NDK linking.
Dan Albert9e10cd42016-08-03 14:12:14 -0700693 checkLinkType := func(from, to *Module) {
Dan Willemsena96ff642016-06-07 12:34:45 -0700694 if from.Target().Os != android.Android {
695 // Host code is not restricted
Dan Albert9e10cd42016-08-03 14:12:14 -0700696 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700697 }
698 if from.Properties.Sdk_version == "" {
699 // Platform code can link to anything
Dan Albert9e10cd42016-08-03 14:12:14 -0700700 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700701 }
Colin Crossb916a382016-07-29 17:28:03 -0700702 if _, ok := to.linker.(*toolchainLibraryDecorator); ok {
Dan Willemsena96ff642016-06-07 12:34:45 -0700703 // These are always allowed
Dan Albert9e10cd42016-08-03 14:12:14 -0700704 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700705 }
706 if _, ok := to.linker.(*ndkPrebuiltLibraryLinker); ok {
707 // These are allowed, but don't set sdk_version
Dan Albert9e10cd42016-08-03 14:12:14 -0700708 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700709 }
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700710 if _, ok := to.linker.(*ndkPrebuiltStlLinker); ok {
711 // These are allowed, but don't set sdk_version
Dan Albert9e10cd42016-08-03 14:12:14 -0700712 return
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700713 }
Colin Crossb916a382016-07-29 17:28:03 -0700714 if _, ok := to.linker.(*stubDecorator); ok {
Dan Albert914449f2016-06-17 16:45:24 -0700715 // These aren't real libraries, but are the stub shared libraries that are included in
716 // the NDK.
Dan Albert9e10cd42016-08-03 14:12:14 -0700717 return
Dan Albert914449f2016-06-17 16:45:24 -0700718 }
Dan Albert9e10cd42016-08-03 14:12:14 -0700719 if to.Properties.Sdk_version == "" {
720 // NDK code linking to platform code is never okay.
721 ctx.ModuleErrorf("depends on non-NDK-built library %q",
722 ctx.OtherModuleName(to))
723 }
724
725 // All this point we know we have two NDK libraries, but we need to
726 // check that we're not linking against anything built against a higher
727 // API level, as it is only valid to link against older or equivalent
728 // APIs.
729
730 if from.Properties.Sdk_version == "current" {
731 // Current can link against anything.
732 return
733 } else if to.Properties.Sdk_version == "current" {
734 // Current can't be linked against by anything else.
735 ctx.ModuleErrorf("links %q built against newer API version %q",
736 ctx.OtherModuleName(to), "current")
737 }
738
739 fromApi, err := strconv.Atoi(from.Properties.Sdk_version)
740 if err != nil {
741 ctx.PropertyErrorf("sdk_version",
742 "Invalid sdk_version value (must be int): %q",
743 from.Properties.Sdk_version)
744 }
745 toApi, err := strconv.Atoi(to.Properties.Sdk_version)
746 if err != nil {
747 ctx.PropertyErrorf("sdk_version",
748 "Invalid sdk_version value (must be int): %q",
749 to.Properties.Sdk_version)
750 }
751
752 if toApi > fromApi {
753 ctx.ModuleErrorf("links %q built against newer API version %q",
754 ctx.OtherModuleName(to), to.Properties.Sdk_version)
755 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700756 }
757
Colin Crossc99deeb2016-04-11 15:06:20 -0700758 ctx.VisitDirectDeps(func(m blueprint.Module) {
759 name := ctx.OtherModuleName(m)
760 tag := ctx.OtherModuleDependencyTag(m)
Colin Crossca860ac2016-01-04 14:34:37 -0800761
Colin Cross635c3b02016-05-18 15:37:25 -0700762 a, _ := m.(android.Module)
Colin Crossc99deeb2016-04-11 15:06:20 -0700763 if a == nil {
764 ctx.ModuleErrorf("module %q not an android module", name)
765 return
Colin Crossca860ac2016-01-04 14:34:37 -0800766 }
Colin Crossca860ac2016-01-04 14:34:37 -0800767
Dan Willemsena96ff642016-06-07 12:34:45 -0700768 cc, _ := m.(*Module)
769 if cc == nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700770 switch tag {
Colin Cross635c3b02016-05-18 15:37:25 -0700771 case android.DefaultsDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700772 case genSourceDepTag:
773 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
774 depPaths.GeneratedSources = append(depPaths.GeneratedSources,
775 genRule.GeneratedSourceFiles()...)
776 } else {
777 ctx.ModuleErrorf("module %q is not a gensrcs or genrule", name)
778 }
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700779 case genHeaderDepTag, genHeaderExportDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700780 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
781 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders,
782 genRule.GeneratedSourceFiles()...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700783 flags := includeDirsToFlags(android.Paths{genRule.GeneratedHeaderDir()})
784 depPaths.Flags = append(depPaths.Flags, flags)
785 if tag == genHeaderExportDepTag {
786 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700787 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps,
788 genRule.GeneratedSourceFiles()...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700789 }
Dan Willemsenb40aab62016-04-20 14:21:14 -0700790 } else {
791 ctx.ModuleErrorf("module %q is not a genrule", name)
792 }
793 default:
Colin Crossc99deeb2016-04-11 15:06:20 -0700794 ctx.ModuleErrorf("depends on non-cc module %q", name)
Colin Crossca860ac2016-01-04 14:34:37 -0800795 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700796 return
797 }
798
799 if !a.Enabled() {
800 ctx.ModuleErrorf("depends on disabled module %q", name)
801 return
802 }
803
Colin Crossa1ad8d12016-06-01 17:09:44 -0700804 if a.Target().Os != ctx.Os() {
805 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), name)
806 return
807 }
808
809 if a.Target().Arch.ArchType != ctx.Arch().ArchType {
810 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700811 return
812 }
813
Colin Crossc99deeb2016-04-11 15:06:20 -0700814 if tag == reuseObjTag {
815 depPaths.ObjFiles = append(depPaths.ObjFiles,
Colin Crossb916a382016-07-29 17:28:03 -0700816 cc.compiler.(libraryInterface).reuseObjs()...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700817 return
818 }
819
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700820 if t, ok := tag.(dependencyTag); ok && t.library {
Dan Willemsena96ff642016-06-07 12:34:45 -0700821 if i, ok := cc.linker.(exportedFlagsProducer); ok {
Dan Willemsen76f08272016-07-09 00:14:08 -0700822 flags := i.exportedFlags()
Dan Willemsen847dcc72016-09-29 12:13:36 -0700823 deps := i.exportedFlagsDeps()
Dan Willemsen76f08272016-07-09 00:14:08 -0700824 depPaths.Flags = append(depPaths.Flags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700825 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders, deps...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700826
827 if t.reexportFlags {
Dan Willemsen76f08272016-07-09 00:14:08 -0700828 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700829 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps, deps...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700830 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700831 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700832
Dan Albert9e10cd42016-08-03 14:12:14 -0700833 checkLinkType(c, cc)
Colin Crossc99deeb2016-04-11 15:06:20 -0700834 }
835
Colin Cross26c34ed2016-09-30 17:10:16 -0700836 var ptr *android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -0700837 var depPtr *android.Paths
Colin Crossc99deeb2016-04-11 15:06:20 -0700838
Colin Cross26c34ed2016-09-30 17:10:16 -0700839 linkFile := cc.outputFile
840 depFile := android.OptionalPath{}
841
Colin Crossc99deeb2016-04-11 15:06:20 -0700842 switch tag {
Dan Albert914449f2016-06-17 16:45:24 -0700843 case ndkStubDepTag, sharedDepTag, sharedExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700844 ptr = &depPaths.SharedLibs
845 depPtr = &depPaths.SharedLibsDeps
846 depFile = cc.linker.(libraryInterface).toc()
Dan Albert914449f2016-06-17 16:45:24 -0700847 case lateSharedDepTag, ndkLateStubDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700848 ptr = &depPaths.LateSharedLibs
849 depPtr = &depPaths.LateSharedLibsDeps
850 depFile = cc.linker.(libraryInterface).toc()
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700851 case staticDepTag, staticExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700852 ptr = &depPaths.StaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -0700853 case lateStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700854 ptr = &depPaths.LateStaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -0700855 case wholeStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700856 ptr = &depPaths.WholeStaticLibs
Colin Crossb916a382016-07-29 17:28:03 -0700857 staticLib, ok := cc.linker.(libraryInterface)
858 if !ok || !staticLib.static() {
Dan Willemsena96ff642016-06-07 12:34:45 -0700859 ctx.ModuleErrorf("module %q not a static library", name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700860 return
861 }
862
863 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
864 postfix := " (required by " + ctx.OtherModuleName(m) + ")"
865 for i := range missingDeps {
866 missingDeps[i] += postfix
867 }
868 ctx.AddMissingDependencies(missingDeps)
869 }
870 depPaths.WholeStaticLibObjFiles =
Colin Crossc7a38dc2016-07-12 13:13:09 -0700871 append(depPaths.WholeStaticLibObjFiles, staticLib.objs()...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700872 case objDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700873 ptr = &depPaths.ObjFiles
Colin Crossc99deeb2016-04-11 15:06:20 -0700874 case crtBeginDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700875 depPaths.CrtBegin = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -0700876 case crtEndDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700877 depPaths.CrtEnd = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -0700878 }
879
Colin Cross26c34ed2016-09-30 17:10:16 -0700880 if ptr != nil {
Colin Crossce75d2c2016-10-06 16:12:58 -0700881 if !linkFile.Valid() {
882 ctx.ModuleErrorf("module %q missing output file", name)
883 return
884 }
Colin Cross26c34ed2016-09-30 17:10:16 -0700885 *ptr = append(*ptr, linkFile.Path())
886 }
887
Colin Crossc99deeb2016-04-11 15:06:20 -0700888 if depPtr != nil {
Colin Cross26c34ed2016-09-30 17:10:16 -0700889 dep := depFile
890 if !dep.Valid() {
891 dep = linkFile
892 }
893 *depPtr = append(*depPtr, dep.Path())
Colin Crossca860ac2016-01-04 14:34:37 -0800894 }
895 })
896
897 return depPaths
898}
899
900func (c *Module) InstallInData() bool {
901 if c.installer == nil {
902 return false
903 }
Colin Cross94610402016-08-29 13:41:32 -0700904 if c.sanitize != nil && c.sanitize.inData() {
905 return true
906 }
Colin Crossca860ac2016-01-04 14:34:37 -0800907 return c.installer.inData()
908}
909
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700910func (c *Module) HostToolPath() android.OptionalPath {
911 if c.installer == nil {
912 return android.OptionalPath{}
913 }
914 return c.installer.hostToolPath()
915}
916
Colin Cross2ba19d92015-05-07 15:44:20 -0700917//
Colin Crosscfad1192015-11-02 16:43:11 -0800918// Defaults
919//
Colin Crossca860ac2016-01-04 14:34:37 -0800920type Defaults struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700921 android.ModuleBase
922 android.DefaultsModule
Colin Crosscfad1192015-11-02 16:43:11 -0800923}
924
Colin Cross635c3b02016-05-18 15:37:25 -0700925func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -0800926}
927
Colin Cross1e676be2016-10-12 14:38:15 -0700928func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
929}
930
Colin Crossca860ac2016-01-04 14:34:37 -0800931func defaultsFactory() (blueprint.Module, []interface{}) {
Colin Crosse1d764e2016-08-18 14:18:32 -0700932 return DefaultsFactory()
933}
934
935func DefaultsFactory(props ...interface{}) (blueprint.Module, []interface{}) {
Colin Crossca860ac2016-01-04 14:34:37 -0800936 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -0800937
Colin Crosse1d764e2016-08-18 14:18:32 -0700938 props = append(props,
Colin Crossca860ac2016-01-04 14:34:37 -0800939 &BaseProperties{},
940 &BaseCompilerProperties{},
941 &BaseLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -0700942 &LibraryProperties{},
Colin Cross919281a2016-04-05 16:42:05 -0700943 &FlagExporterProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -0800944 &BinaryLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -0700945 &TestProperties{},
946 &TestBinaryProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -0800947 &UnusedProperties{},
948 &StlProperties{},
Colin Cross16b23492016-01-06 14:41:07 -0800949 &SanitizeProperties{},
Colin Cross665dce92016-04-28 14:50:03 -0700950 &StripProperties{},
Dan Willemsen7424d612016-09-01 13:45:39 -0700951 &InstallerProperties{},
Colin Crosse1d764e2016-08-18 14:18:32 -0700952 )
Colin Crosscfad1192015-11-02 16:43:11 -0800953
Colin Crosse1d764e2016-08-18 14:18:32 -0700954 return android.InitDefaultsModule(module, module, props...)
Colin Crosscfad1192015-11-02 16:43:11 -0800955}
956
Colin Cross74d1ec02015-04-28 13:30:13 -0700957// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
958// modifies the slice contents in place, and returns a subslice of the original slice
959func lastUniqueElements(list []string) []string {
960 totalSkip := 0
961 for i := len(list) - 1; i >= totalSkip; i-- {
962 skip := 0
963 for j := i - 1; j >= totalSkip; j-- {
964 if list[i] == list[j] {
965 skip++
966 } else {
967 list[j+skip] = list[j]
968 }
969 }
970 totalSkip += skip
971 }
972 return list[totalSkip:]
973}
Colin Cross06a931b2015-10-28 17:23:31 -0700974
975var Bool = proptools.Bool