blob: 8af2b8c709725aff76a72198614ceae0398831c7 [file] [log] [blame]
Colin Cross5049f022015-03-18 13:28:46 -07001// Copyright 2015 Google Inc. All rights reserved.
Colin Cross3f40fa42015-01-30 17:27:36 -08002//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package cc
16
17// This file contains the module types for compiling C/C++ for Android, and converts the properties
18// into the flags and filenames necessary to pass to the compiler. The final creation of the rules
19// is handled in builder.go
20
21import (
Colin Cross3f40fa42015-01-30 17:27:36 -080022 "fmt"
Dan Albert9e10cd42016-08-03 14:12:14 -070023 "strconv"
Colin Cross3f40fa42015-01-30 17:27:36 -080024 "strings"
25
Colin Cross97ba0732015-03-23 17:50:24 -070026 "github.com/google/blueprint"
Colin Cross06a931b2015-10-28 17:23:31 -070027 "github.com/google/blueprint/proptools"
Colin Cross97ba0732015-03-23 17:50:24 -070028
Colin Cross463a90e2015-06-17 14:20:06 -070029 "android/soong"
Colin Cross635c3b02016-05-18 15:37:25 -070030 "android/soong/android"
Colin Crossb98c8b02016-07-29 13:44:28 -070031 "android/soong/cc/config"
Colin Cross5049f022015-03-18 13:28:46 -070032 "android/soong/genrule"
Colin Cross3f40fa42015-01-30 17:27:36 -080033)
34
Colin Cross463a90e2015-06-17 14:20:06 -070035func init() {
Colin Crossca860ac2016-01-04 14:34:37 -080036 soong.RegisterModuleType("cc_defaults", defaultsFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070037
Colin Crossca860ac2016-01-04 14:34:37 -080038 soong.RegisterModuleType("toolchain_library", toolchainLibraryFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070039
40 // LinkageMutator must be registered after common.ArchMutator, but that is guaranteed by
41 // the Go initialization order because this package depends on common, so common's init
42 // functions will run first.
Colin Cross635c3b02016-05-18 15:37:25 -070043 android.RegisterBottomUpMutator("link", linkageMutator)
Dan Albert914449f2016-06-17 16:45:24 -070044 android.RegisterBottomUpMutator("ndk_api", ndkApiMutator)
Colin Cross635c3b02016-05-18 15:37:25 -070045 android.RegisterBottomUpMutator("test_per_src", testPerSrcMutator)
Dan Albert7e9d2952016-08-04 13:02:36 -070046 android.RegisterBottomUpMutator("begin", beginMutator)
Colin Cross635c3b02016-05-18 15:37:25 -070047 android.RegisterBottomUpMutator("deps", depsMutator)
Colin Cross16b23492016-01-06 14:41:07 -080048
Colin Cross635c3b02016-05-18 15:37:25 -070049 android.RegisterTopDownMutator("asan_deps", sanitizerDepsMutator(asan))
50 android.RegisterBottomUpMutator("asan", sanitizerMutator(asan))
Colin Cross16b23492016-01-06 14:41:07 -080051
Colin Cross635c3b02016-05-18 15:37:25 -070052 android.RegisterTopDownMutator("tsan_deps", sanitizerDepsMutator(tsan))
53 android.RegisterBottomUpMutator("tsan", sanitizerMutator(tsan))
Colin Crossb98c8b02016-07-29 13:44:28 -070054
55 pctx.Import("android/soong/cc/config")
Colin Cross463a90e2015-06-17 14:20:06 -070056}
57
Colin Crossca860ac2016-01-04 14:34:37 -080058type Deps struct {
59 SharedLibs, LateSharedLibs []string
60 StaticLibs, LateStaticLibs, WholeStaticLibs []string
Colin Crossc472d572015-03-17 15:06:21 -070061
Dan Willemsen490a8dc2016-06-06 18:22:19 -070062 ReexportSharedLibHeaders, ReexportStaticLibHeaders []string
63
Colin Cross81413472016-04-11 14:37:39 -070064 ObjFiles []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -070065
Dan Willemsenb40aab62016-04-20 14:21:14 -070066 GeneratedSources []string
67 GeneratedHeaders []string
68
Colin Cross97ba0732015-03-23 17:50:24 -070069 CrtBegin, CrtEnd string
Colin Crossc472d572015-03-17 15:06:21 -070070}
71
Colin Crossca860ac2016-01-04 14:34:37 -080072type PathDeps struct {
Colin Cross635c3b02016-05-18 15:37:25 -070073 SharedLibs, LateSharedLibs android.Paths
74 StaticLibs, LateStaticLibs, WholeStaticLibs android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -070075
Colin Cross635c3b02016-05-18 15:37:25 -070076 ObjFiles android.Paths
77 WholeStaticLibObjFiles android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -070078
Colin Cross635c3b02016-05-18 15:37:25 -070079 GeneratedSources android.Paths
80 GeneratedHeaders android.Paths
Dan Willemsenb40aab62016-04-20 14:21:14 -070081
Dan Willemsen76f08272016-07-09 00:14:08 -070082 Flags, ReexportedFlags []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -070083
Colin Cross635c3b02016-05-18 15:37:25 -070084 CrtBegin, CrtEnd android.OptionalPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -070085}
86
Colin Crossca860ac2016-01-04 14:34:37 -080087type Flags struct {
Colin Cross28344522015-04-22 13:07:53 -070088 GlobalFlags []string // Flags that apply to C, C++, and assembly source files
89 AsFlags []string // Flags that apply to assembly source files
90 CFlags []string // Flags that apply to C and C++ source files
91 ConlyFlags []string // Flags that apply to C source files
92 CppFlags []string // Flags that apply to C++ source files
93 YaccFlags []string // Flags that apply to Yacc source files
94 LdFlags []string // Flags that apply to linker command lines
Colin Cross16b23492016-01-06 14:41:07 -080095 libFlags []string // Flags to add libraries early to the link order
Colin Cross28344522015-04-22 13:07:53 -070096
Colin Crossb98c8b02016-07-29 13:44:28 -070097 Toolchain config.Toolchain
Colin Cross28344522015-04-22 13:07:53 -070098 Clang bool
Colin Crossca860ac2016-01-04 14:34:37 -080099
100 RequiredInstructionSet string
Colin Cross16b23492016-01-06 14:41:07 -0800101 DynamicLinker string
102
Colin Cross635c3b02016-05-18 15:37:25 -0700103 CFlagsDeps android.Paths // Files depended on by compiler flags
Colin Crossc472d572015-03-17 15:06:21 -0700104}
105
Colin Cross81413472016-04-11 14:37:39 -0700106type ObjectLinkerProperties struct {
107 // names of other cc_object modules to link into this module using partial linking
108 Objs []string `android:"arch_variant"`
109}
110
Colin Crossca860ac2016-01-04 14:34:37 -0800111// Properties used to compile all C or C++ modules
112type BaseProperties struct {
113 // compile module with clang instead of gcc
114 Clang *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700115
116 // Minimum sdk version supported when compiling against the ndk
117 Sdk_version string
118
Colin Crossca860ac2016-01-04 14:34:37 -0800119 // don't insert default compiler flags into asflags, cflags,
120 // cppflags, conlyflags, ldflags, or include_dirs
121 No_default_compiler_flags *bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700122
123 AndroidMkSharedLibs []string `blueprint:"mutated"`
Colin Crossbc6fb162016-05-24 15:39:04 -0700124 HideFromMake bool `blueprint:"mutated"`
Colin Crossca860ac2016-01-04 14:34:37 -0800125}
126
Colin Crossca860ac2016-01-04 14:34:37 -0800127type UnusedProperties struct {
Colin Cross21b481b2016-04-15 16:27:17 -0700128 Native_coverage *bool
129 Required []string
Colin Cross21b481b2016-04-15 16:27:17 -0700130 Tags []string
Colin Crosscfad1192015-11-02 16:43:11 -0800131}
132
Colin Crossca860ac2016-01-04 14:34:37 -0800133type ModuleContextIntf interface {
Colin Crossca860ac2016-01-04 14:34:37 -0800134 static() bool
135 staticBinary() bool
136 clang() bool
Colin Crossb98c8b02016-07-29 13:44:28 -0700137 toolchain() config.Toolchain
Colin Crossca860ac2016-01-04 14:34:37 -0800138 noDefaultCompilerFlags() bool
139 sdk() bool
140 sdkVersion() string
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700141 selectedStl() string
Colin Crossca860ac2016-01-04 14:34:37 -0800142}
143
144type ModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700145 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800146 ModuleContextIntf
147}
148
149type BaseModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700150 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800151 ModuleContextIntf
152}
153
Colin Cross76fada02016-07-27 10:31:13 -0700154type CustomizerFlagsContext interface {
155 BaseModuleContext
156 AppendCflags(...string)
157 AppendLdflags(...string)
158 AppendAsflags(...string)
159}
160
Colin Crossca860ac2016-01-04 14:34:37 -0800161type Customizer interface {
Colin Cross76fada02016-07-27 10:31:13 -0700162 Flags(CustomizerFlagsContext)
Colin Crossca860ac2016-01-04 14:34:37 -0800163 Properties() []interface{}
164}
165
166type feature interface {
167 begin(ctx BaseModuleContext)
168 deps(ctx BaseModuleContext, deps Deps) Deps
169 flags(ctx ModuleContext, flags Flags) Flags
170 props() []interface{}
171}
172
173type compiler interface {
Colin Cross42742b82016-08-01 13:20:05 -0700174 compilerInit(ctx BaseModuleContext)
175 compilerDeps(ctx BaseModuleContext, deps Deps) Deps
176 compilerFlags(ctx ModuleContext, flags Flags) Flags
177 compilerProps() []interface{}
178
Colin Cross76fada02016-07-27 10:31:13 -0700179 appendCflags([]string)
180 appendAsflags([]string)
Colin Cross635c3b02016-05-18 15:37:25 -0700181 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -0800182}
183
184type linker interface {
Colin Cross42742b82016-08-01 13:20:05 -0700185 linkerInit(ctx BaseModuleContext)
186 linkerDeps(ctx BaseModuleContext, deps Deps) Deps
187 linkerFlags(ctx ModuleContext, flags Flags) Flags
188 linkerProps() []interface{}
189
Colin Cross635c3b02016-05-18 15:37:25 -0700190 link(ctx ModuleContext, flags Flags, deps PathDeps, objFiles android.Paths) android.Path
Colin Cross76fada02016-07-27 10:31:13 -0700191 appendLdflags([]string)
Colin Crossc99deeb2016-04-11 15:06:20 -0700192 installable() bool
Colin Crossca860ac2016-01-04 14:34:37 -0800193}
194
195type installer interface {
Colin Cross42742b82016-08-01 13:20:05 -0700196 installerProps() []interface{}
Colin Cross635c3b02016-05-18 15:37:25 -0700197 install(ctx ModuleContext, path android.Path)
Colin Crossca860ac2016-01-04 14:34:37 -0800198 inData() bool
199}
200
Colin Crossc99deeb2016-04-11 15:06:20 -0700201type dependencyTag struct {
202 blueprint.BaseDependencyTag
203 name string
204 library bool
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700205
206 reexportFlags bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700207}
208
209var (
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700210 sharedDepTag = dependencyTag{name: "shared", library: true}
211 sharedExportDepTag = dependencyTag{name: "shared", library: true, reexportFlags: true}
212 lateSharedDepTag = dependencyTag{name: "late shared", library: true}
213 staticDepTag = dependencyTag{name: "static", library: true}
214 staticExportDepTag = dependencyTag{name: "static", library: true, reexportFlags: true}
215 lateStaticDepTag = dependencyTag{name: "late static", library: true}
216 wholeStaticDepTag = dependencyTag{name: "whole static", library: true, reexportFlags: true}
217 genSourceDepTag = dependencyTag{name: "gen source"}
218 genHeaderDepTag = dependencyTag{name: "gen header"}
219 objDepTag = dependencyTag{name: "obj"}
220 crtBeginDepTag = dependencyTag{name: "crtbegin"}
221 crtEndDepTag = dependencyTag{name: "crtend"}
222 reuseObjTag = dependencyTag{name: "reuse objects"}
Dan Albert914449f2016-06-17 16:45:24 -0700223 ndkStubDepTag = dependencyTag{name: "ndk stub", library: true}
224 ndkLateStubDepTag = dependencyTag{name: "ndk late stub", library: true}
Colin Crossc99deeb2016-04-11 15:06:20 -0700225)
226
Colin Crossca860ac2016-01-04 14:34:37 -0800227// Module contains the properties and members used by all C/C++ module types, and implements
228// the blueprint.Module interface. It delegates to compiler, linker, and installer interfaces
229// to construct the output file. Behavior can be customized with a Customizer interface
230type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700231 android.ModuleBase
232 android.DefaultableModule
Colin Crossc472d572015-03-17 15:06:21 -0700233
Colin Crossca860ac2016-01-04 14:34:37 -0800234 Properties BaseProperties
235 unused UnusedProperties
Colin Crossfa138792015-04-24 17:31:52 -0700236
Colin Crossca860ac2016-01-04 14:34:37 -0800237 // initialize before calling Init
Colin Cross635c3b02016-05-18 15:37:25 -0700238 hod android.HostOrDeviceSupported
239 multilib android.Multilib
Colin Crossc472d572015-03-17 15:06:21 -0700240
Colin Crossca860ac2016-01-04 14:34:37 -0800241 // delegates, initialize before calling Init
Colin Cross76fada02016-07-27 10:31:13 -0700242 Customizer Customizer
Colin Crossca860ac2016-01-04 14:34:37 -0800243 features []feature
244 compiler compiler
245 linker linker
246 installer installer
Colin Crossa8e07cc2016-04-04 15:07:06 -0700247 stl *stl
Colin Cross16b23492016-01-06 14:41:07 -0800248 sanitize *sanitize
249
250 androidMkSharedLibDeps []string
Colin Cross74d1ec02015-04-28 13:30:13 -0700251
Colin Cross635c3b02016-05-18 15:37:25 -0700252 outputFile android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800253
Colin Crossb98c8b02016-07-29 13:44:28 -0700254 cachedToolchain config.Toolchain
Colin Crossc472d572015-03-17 15:06:21 -0700255}
256
Colin Crossca860ac2016-01-04 14:34:37 -0800257func (c *Module) Init() (blueprint.Module, []interface{}) {
258 props := []interface{}{&c.Properties, &c.unused}
Colin Cross76fada02016-07-27 10:31:13 -0700259 if c.Customizer != nil {
260 props = append(props, c.Customizer.Properties()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800261 }
262 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700263 props = append(props, c.compiler.compilerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800264 }
265 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700266 props = append(props, c.linker.linkerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800267 }
268 if c.installer != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700269 props = append(props, c.installer.installerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800270 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700271 if c.stl != nil {
272 props = append(props, c.stl.props()...)
273 }
Colin Cross16b23492016-01-06 14:41:07 -0800274 if c.sanitize != nil {
275 props = append(props, c.sanitize.props()...)
276 }
Colin Crossca860ac2016-01-04 14:34:37 -0800277 for _, feature := range c.features {
278 props = append(props, feature.props()...)
279 }
Colin Crossc472d572015-03-17 15:06:21 -0700280
Colin Cross635c3b02016-05-18 15:37:25 -0700281 _, props = android.InitAndroidArchModule(c, c.hod, c.multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700282
Colin Cross635c3b02016-05-18 15:37:25 -0700283 return android.InitDefaultableModule(c, c, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700284}
285
Colin Crossca860ac2016-01-04 14:34:37 -0800286type baseModuleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700287 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800288 moduleContextImpl
289}
290
291type moduleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700292 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800293 moduleContextImpl
294}
295
296type moduleContextImpl struct {
297 mod *Module
298 ctx BaseModuleContext
299}
300
Colin Cross76fada02016-07-27 10:31:13 -0700301func (ctx *moduleContextImpl) AppendCflags(flags ...string) {
302 CheckBadCompilerFlags(ctx.ctx, "", flags)
303 ctx.mod.compiler.appendCflags(flags)
304}
305
306func (ctx *moduleContextImpl) AppendAsflags(flags ...string) {
307 CheckBadCompilerFlags(ctx.ctx, "", flags)
308 ctx.mod.compiler.appendAsflags(flags)
309}
310
311func (ctx *moduleContextImpl) AppendLdflags(flags ...string) {
312 CheckBadLinkerFlags(ctx.ctx, "", flags)
313 ctx.mod.linker.appendLdflags(flags)
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 {
325 if ctx.mod.linker == nil {
326 panic(fmt.Errorf("static called on module %q with no linker", ctx.ctx.ModuleName()))
327 }
328 if linker, ok := ctx.mod.linker.(baseLinkerInterface); ok {
329 return linker.static()
330 } else {
331 panic(fmt.Errorf("static called on module %q that doesn't use base linker", ctx.ctx.ModuleName()))
332 }
333}
334
335func (ctx *moduleContextImpl) staticBinary() bool {
336 if ctx.mod.linker == nil {
337 panic(fmt.Errorf("staticBinary called on module %q with no linker", ctx.ctx.ModuleName()))
338 }
339 if linker, ok := ctx.mod.linker.(baseLinkerInterface); ok {
340 return linker.staticBinary()
341 } else {
342 panic(fmt.Errorf("staticBinary called on module %q that doesn't use base linker", ctx.ctx.ModuleName()))
343 }
344}
345
346func (ctx *moduleContextImpl) noDefaultCompilerFlags() bool {
347 return Bool(ctx.mod.Properties.No_default_compiler_flags)
348}
349
350func (ctx *moduleContextImpl) sdk() bool {
Dan Willemsena96ff642016-06-07 12:34:45 -0700351 if ctx.ctx.Device() {
352 return ctx.mod.Properties.Sdk_version != ""
353 }
354 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800355}
356
357func (ctx *moduleContextImpl) sdkVersion() string {
Dan Willemsena96ff642016-06-07 12:34:45 -0700358 if ctx.ctx.Device() {
359 return ctx.mod.Properties.Sdk_version
360 }
361 return ""
Colin Crossca860ac2016-01-04 14:34:37 -0800362}
363
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700364func (ctx *moduleContextImpl) selectedStl() string {
365 if stl := ctx.mod.stl; stl != nil {
366 return stl.Properties.SelectedStl
367 }
368 return ""
369}
370
Colin Cross635c3b02016-05-18 15:37:25 -0700371func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800372 return &Module{
373 hod: hod,
374 multilib: multilib,
375 }
376}
377
Colin Cross635c3b02016-05-18 15:37:25 -0700378func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800379 module := newBaseModule(hod, multilib)
Colin Crossa8e07cc2016-04-04 15:07:06 -0700380 module.stl = &stl{}
Colin Cross16b23492016-01-06 14:41:07 -0800381 module.sanitize = &sanitize{}
Colin Crossca860ac2016-01-04 14:34:37 -0800382 return module
383}
384
Colin Cross635c3b02016-05-18 15:37:25 -0700385func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800386 ctx := &moduleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700387 ModuleContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800388 moduleContextImpl: moduleContextImpl{
389 mod: c,
390 },
391 }
392 ctx.ctx = ctx
393
Colin Cross76fada02016-07-27 10:31:13 -0700394 if c.Customizer != nil {
395 c.Customizer.Flags(ctx)
396 }
397
Colin Crossca860ac2016-01-04 14:34:37 -0800398 flags := Flags{
399 Toolchain: c.toolchain(ctx),
400 Clang: c.clang(ctx),
401 }
Colin Crossca860ac2016-01-04 14:34:37 -0800402 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700403 flags = c.compiler.compilerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800404 }
405 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700406 flags = c.linker.linkerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800407 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700408 if c.stl != nil {
409 flags = c.stl.flags(ctx, flags)
410 }
Colin Cross16b23492016-01-06 14:41:07 -0800411 if c.sanitize != nil {
412 flags = c.sanitize.flags(ctx, flags)
413 }
Colin Crossca860ac2016-01-04 14:34:37 -0800414 for _, feature := range c.features {
415 flags = feature.flags(ctx, flags)
416 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800417 if ctx.Failed() {
418 return
419 }
420
Colin Crossb98c8b02016-07-29 13:44:28 -0700421 flags.CFlags, _ = filterList(flags.CFlags, config.IllegalFlags)
422 flags.CppFlags, _ = filterList(flags.CppFlags, config.IllegalFlags)
423 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, config.IllegalFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800424
Colin Crossca860ac2016-01-04 14:34:37 -0800425 // Optimization to reduce size of build.ninja
426 // Replace the long list of flags for each file with a module-local variable
427 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
428 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
429 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
430 flags.CFlags = []string{"$cflags"}
431 flags.CppFlags = []string{"$cppflags"}
432 flags.AsFlags = []string{"$asflags"}
433
Colin Crossc99deeb2016-04-11 15:06:20 -0700434 deps := c.depsToPaths(ctx)
Colin Cross3f40fa42015-01-30 17:27:36 -0800435 if ctx.Failed() {
436 return
437 }
438
Dan Willemsen76f08272016-07-09 00:14:08 -0700439 flags.GlobalFlags = append(flags.GlobalFlags, deps.Flags...)
Colin Crossed9f8682015-03-18 17:17:35 -0700440
Colin Cross635c3b02016-05-18 15:37:25 -0700441 var objFiles android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -0800442 if c.compiler != nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700443 objFiles = c.compiler.compile(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -0800444 if ctx.Failed() {
445 return
446 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800447 }
448
Colin Crossca860ac2016-01-04 14:34:37 -0800449 if c.linker != nil {
450 outputFile := c.linker.link(ctx, flags, deps, objFiles)
451 if ctx.Failed() {
452 return
453 }
Colin Cross635c3b02016-05-18 15:37:25 -0700454 c.outputFile = android.OptionalPathForPath(outputFile)
Colin Cross5049f022015-03-18 13:28:46 -0700455
Colin Crossc99deeb2016-04-11 15:06:20 -0700456 if c.installer != nil && c.linker.installable() {
Colin Crossca860ac2016-01-04 14:34:37 -0800457 c.installer.install(ctx, outputFile)
458 if ctx.Failed() {
459 return
460 }
461 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700462 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800463}
464
Colin Crossb98c8b02016-07-29 13:44:28 -0700465func (c *Module) toolchain(ctx BaseModuleContext) config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800466 if c.cachedToolchain == nil {
Colin Crossb98c8b02016-07-29 13:44:28 -0700467 c.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
Colin Cross3f40fa42015-01-30 17:27:36 -0800468 }
Colin Crossca860ac2016-01-04 14:34:37 -0800469 return c.cachedToolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800470}
471
Colin Crossca860ac2016-01-04 14:34:37 -0800472func (c *Module) begin(ctx BaseModuleContext) {
473 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700474 c.compiler.compilerInit(ctx)
Colin Cross21b9a242015-03-24 14:15:58 -0700475 }
Colin Crossca860ac2016-01-04 14:34:37 -0800476 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700477 c.linker.linkerInit(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800478 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700479 if c.stl != nil {
480 c.stl.begin(ctx)
481 }
Colin Cross16b23492016-01-06 14:41:07 -0800482 if c.sanitize != nil {
483 c.sanitize.begin(ctx)
484 }
Colin Crossca860ac2016-01-04 14:34:37 -0800485 for _, feature := range c.features {
486 feature.begin(ctx)
487 }
488}
489
Colin Crossc99deeb2016-04-11 15:06:20 -0700490func (c *Module) deps(ctx BaseModuleContext) Deps {
491 deps := Deps{}
492
493 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700494 deps = c.compiler.compilerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700495 }
496 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700497 deps = c.linker.linkerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700498 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700499 if c.stl != nil {
500 deps = c.stl.deps(ctx, deps)
501 }
Colin Cross16b23492016-01-06 14:41:07 -0800502 if c.sanitize != nil {
503 deps = c.sanitize.deps(ctx, deps)
504 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700505 for _, feature := range c.features {
506 deps = feature.deps(ctx, deps)
507 }
508
509 deps.WholeStaticLibs = lastUniqueElements(deps.WholeStaticLibs)
510 deps.StaticLibs = lastUniqueElements(deps.StaticLibs)
511 deps.LateStaticLibs = lastUniqueElements(deps.LateStaticLibs)
512 deps.SharedLibs = lastUniqueElements(deps.SharedLibs)
513 deps.LateSharedLibs = lastUniqueElements(deps.LateSharedLibs)
514
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700515 for _, lib := range deps.ReexportSharedLibHeaders {
516 if !inList(lib, deps.SharedLibs) {
517 ctx.PropertyErrorf("export_shared_lib_headers", "Shared library not in shared_libs: '%s'", lib)
518 }
519 }
520
521 for _, lib := range deps.ReexportStaticLibHeaders {
522 if !inList(lib, deps.StaticLibs) {
523 ctx.PropertyErrorf("export_static_lib_headers", "Static library not in static_libs: '%s'", lib)
524 }
525 }
526
Colin Crossc99deeb2016-04-11 15:06:20 -0700527 return deps
528}
529
Dan Albert7e9d2952016-08-04 13:02:36 -0700530func (c *Module) beginMutator(actx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800531 ctx := &baseModuleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700532 BaseContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800533 moduleContextImpl: moduleContextImpl{
534 mod: c,
535 },
536 }
537 ctx.ctx = ctx
538
Colin Crossca860ac2016-01-04 14:34:37 -0800539 c.begin(ctx)
Dan Albert7e9d2952016-08-04 13:02:36 -0700540}
541
542func (c *Module) depsMutator(actx android.BottomUpMutatorContext) {
543 ctx := &baseModuleContext{
544 BaseContext: actx,
545 moduleContextImpl: moduleContextImpl{
546 mod: c,
547 },
548 }
549 ctx.ctx = ctx
Colin Crossca860ac2016-01-04 14:34:37 -0800550
Colin Crossc99deeb2016-04-11 15:06:20 -0700551 deps := c.deps(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800552
Colin Crossb5bc4b42016-07-11 16:11:59 -0700553 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.SharedLibs...)
554 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.LateSharedLibs...)
Dan Willemsen72d39932016-07-08 23:23:48 -0700555
Dan Albert914449f2016-06-17 16:45:24 -0700556 variantNdkLibs := []string{}
557 variantLateNdkLibs := []string{}
Dan Willemsen72d39932016-07-08 23:23:48 -0700558 if ctx.sdk() {
Dan Albert914449f2016-06-17 16:45:24 -0700559 version := ctx.sdkVersion()
Dan Willemsen72d39932016-07-08 23:23:48 -0700560
Dan Albert914449f2016-06-17 16:45:24 -0700561 // Rewrites the names of shared libraries into the names of the NDK
562 // libraries where appropriate. This returns two slices.
563 //
564 // The first is a list of non-variant shared libraries (either rewritten
565 // NDK libraries to the modules in prebuilts/ndk, or not rewritten
566 // because they are not NDK libraries).
567 //
568 // The second is a list of ndk_library modules. These need to be
569 // separated because they are a variation dependency and must be added
570 // in a different manner.
571 rewriteNdkLibs := func(list []string) ([]string, []string) {
572 variantLibs := []string{}
573 nonvariantLibs := []string{}
574 for _, entry := range list {
Dan Willemsen72d39932016-07-08 23:23:48 -0700575 if inList(entry, ndkPrebuiltSharedLibraries) {
Dan Albert914449f2016-06-17 16:45:24 -0700576 if !inList(entry, ndkMigratedLibs) {
577 nonvariantLibs = append(nonvariantLibs, entry+".ndk."+version)
578 } else {
579 variantLibs = append(variantLibs, entry+ndkLibrarySuffix)
580 }
581 } else {
582 nonvariantLibs = append(variantLibs, entry)
Dan Willemsen72d39932016-07-08 23:23:48 -0700583 }
584 }
Dan Albert914449f2016-06-17 16:45:24 -0700585 return nonvariantLibs, variantLibs
Dan Willemsen72d39932016-07-08 23:23:48 -0700586 }
587
Dan Albert914449f2016-06-17 16:45:24 -0700588 deps.SharedLibs, variantNdkLibs = rewriteNdkLibs(deps.SharedLibs)
589 deps.LateSharedLibs, variantLateNdkLibs = rewriteNdkLibs(deps.LateSharedLibs)
Dan Willemsen72d39932016-07-08 23:23:48 -0700590 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700591
592 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, wholeStaticDepTag,
593 deps.WholeStaticLibs...)
594
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700595 for _, lib := range deps.StaticLibs {
596 depTag := staticDepTag
597 if inList(lib, deps.ReexportStaticLibHeaders) {
598 depTag = staticExportDepTag
599 }
Colin Cross15a0d462016-07-14 14:49:58 -0700600 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700601 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700602
603 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, lateStaticDepTag,
604 deps.LateStaticLibs...)
605
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700606 for _, lib := range deps.SharedLibs {
607 depTag := sharedDepTag
608 if inList(lib, deps.ReexportSharedLibHeaders) {
609 depTag = sharedExportDepTag
610 }
Colin Cross15a0d462016-07-14 14:49:58 -0700611 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700612 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700613
614 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, lateSharedDepTag,
615 deps.LateSharedLibs...)
616
Colin Cross68861832016-07-08 10:41:41 -0700617 actx.AddDependency(c, genSourceDepTag, deps.GeneratedSources...)
618 actx.AddDependency(c, genHeaderDepTag, deps.GeneratedHeaders...)
Dan Willemsenb40aab62016-04-20 14:21:14 -0700619
Colin Cross68861832016-07-08 10:41:41 -0700620 actx.AddDependency(c, objDepTag, deps.ObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700621
622 if deps.CrtBegin != "" {
Colin Cross68861832016-07-08 10:41:41 -0700623 actx.AddDependency(c, crtBeginDepTag, deps.CrtBegin)
Colin Crossca860ac2016-01-04 14:34:37 -0800624 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700625 if deps.CrtEnd != "" {
Colin Cross68861832016-07-08 10:41:41 -0700626 actx.AddDependency(c, crtEndDepTag, deps.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700627 }
Dan Albert914449f2016-06-17 16:45:24 -0700628
629 version := ctx.sdkVersion()
630 actx.AddVariationDependencies([]blueprint.Variation{
631 {"ndk_api", version}, {"link", "shared"}}, ndkStubDepTag, variantNdkLibs...)
632 actx.AddVariationDependencies([]blueprint.Variation{
633 {"ndk_api", version}, {"link", "shared"}}, ndkLateStubDepTag, variantLateNdkLibs...)
Colin Cross6362e272015-10-29 15:25:03 -0700634}
Colin Cross21b9a242015-03-24 14:15:58 -0700635
Dan Albert7e9d2952016-08-04 13:02:36 -0700636func beginMutator(ctx android.BottomUpMutatorContext) {
637 if c, ok := ctx.Module().(*Module); ok && c.Enabled() {
638 c.beginMutator(ctx)
639 }
640}
641
Colin Cross635c3b02016-05-18 15:37:25 -0700642func depsMutator(ctx android.BottomUpMutatorContext) {
Dan Willemsen3f32f032016-07-11 14:36:48 -0700643 if c, ok := ctx.Module().(*Module); ok && c.Enabled() {
Colin Cross6362e272015-10-29 15:25:03 -0700644 c.depsMutator(ctx)
645 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800646}
647
Colin Crossca860ac2016-01-04 14:34:37 -0800648func (c *Module) clang(ctx BaseModuleContext) bool {
649 clang := Bool(c.Properties.Clang)
650
651 if c.Properties.Clang == nil {
652 if ctx.Host() {
653 clang = true
654 }
655
656 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
657 clang = true
658 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800659 }
Colin Cross28344522015-04-22 13:07:53 -0700660
Colin Crossca860ac2016-01-04 14:34:37 -0800661 if !c.toolchain(ctx).ClangSupported() {
662 clang = false
663 }
664
665 return clang
666}
667
Colin Crossc99deeb2016-04-11 15:06:20 -0700668// Convert dependencies to paths. Returns a PathDeps containing paths
Colin Cross635c3b02016-05-18 15:37:25 -0700669func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
Colin Crossca860ac2016-01-04 14:34:37 -0800670 var depPaths PathDeps
Colin Crossca860ac2016-01-04 14:34:37 -0800671
Dan Willemsena96ff642016-06-07 12:34:45 -0700672 // Whether a module can link to another module, taking into
673 // account NDK linking.
Dan Albert9e10cd42016-08-03 14:12:14 -0700674 checkLinkType := func(from, to *Module) {
Dan Willemsena96ff642016-06-07 12:34:45 -0700675 if from.Target().Os != android.Android {
676 // Host code is not restricted
Dan Albert9e10cd42016-08-03 14:12:14 -0700677 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700678 }
679 if from.Properties.Sdk_version == "" {
680 // Platform code can link to anything
Dan Albert9e10cd42016-08-03 14:12:14 -0700681 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700682 }
683 if _, ok := to.linker.(*toolchainLibraryLinker); ok {
684 // These are always allowed
Dan Albert9e10cd42016-08-03 14:12:14 -0700685 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700686 }
687 if _, ok := to.linker.(*ndkPrebuiltLibraryLinker); ok {
688 // These are allowed, but don't set sdk_version
Dan Albert9e10cd42016-08-03 14:12:14 -0700689 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700690 }
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700691 if _, ok := to.linker.(*ndkPrebuiltStlLinker); ok {
692 // These are allowed, but don't set sdk_version
Dan Albert9e10cd42016-08-03 14:12:14 -0700693 return
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700694 }
Dan Albert914449f2016-06-17 16:45:24 -0700695 if _, ok := to.linker.(*stubLinker); ok {
696 // These aren't real libraries, but are the stub shared libraries that are included in
697 // the NDK.
Dan Albert9e10cd42016-08-03 14:12:14 -0700698 return
Dan Albert914449f2016-06-17 16:45:24 -0700699 }
Dan Albert9e10cd42016-08-03 14:12:14 -0700700 if to.Properties.Sdk_version == "" {
701 // NDK code linking to platform code is never okay.
702 ctx.ModuleErrorf("depends on non-NDK-built library %q",
703 ctx.OtherModuleName(to))
704 }
705
706 // All this point we know we have two NDK libraries, but we need to
707 // check that we're not linking against anything built against a higher
708 // API level, as it is only valid to link against older or equivalent
709 // APIs.
710
711 if from.Properties.Sdk_version == "current" {
712 // Current can link against anything.
713 return
714 } else if to.Properties.Sdk_version == "current" {
715 // Current can't be linked against by anything else.
716 ctx.ModuleErrorf("links %q built against newer API version %q",
717 ctx.OtherModuleName(to), "current")
718 }
719
720 fromApi, err := strconv.Atoi(from.Properties.Sdk_version)
721 if err != nil {
722 ctx.PropertyErrorf("sdk_version",
723 "Invalid sdk_version value (must be int): %q",
724 from.Properties.Sdk_version)
725 }
726 toApi, err := strconv.Atoi(to.Properties.Sdk_version)
727 if err != nil {
728 ctx.PropertyErrorf("sdk_version",
729 "Invalid sdk_version value (must be int): %q",
730 to.Properties.Sdk_version)
731 }
732
733 if toApi > fromApi {
734 ctx.ModuleErrorf("links %q built against newer API version %q",
735 ctx.OtherModuleName(to), to.Properties.Sdk_version)
736 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700737 }
738
Colin Crossc99deeb2016-04-11 15:06:20 -0700739 ctx.VisitDirectDeps(func(m blueprint.Module) {
740 name := ctx.OtherModuleName(m)
741 tag := ctx.OtherModuleDependencyTag(m)
Colin Crossca860ac2016-01-04 14:34:37 -0800742
Colin Cross635c3b02016-05-18 15:37:25 -0700743 a, _ := m.(android.Module)
Colin Crossc99deeb2016-04-11 15:06:20 -0700744 if a == nil {
745 ctx.ModuleErrorf("module %q not an android module", name)
746 return
Colin Crossca860ac2016-01-04 14:34:37 -0800747 }
Colin Crossca860ac2016-01-04 14:34:37 -0800748
Dan Willemsena96ff642016-06-07 12:34:45 -0700749 cc, _ := m.(*Module)
750 if cc == nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700751 switch tag {
Colin Cross635c3b02016-05-18 15:37:25 -0700752 case android.DefaultsDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700753 case genSourceDepTag:
754 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
755 depPaths.GeneratedSources = append(depPaths.GeneratedSources,
756 genRule.GeneratedSourceFiles()...)
757 } else {
758 ctx.ModuleErrorf("module %q is not a gensrcs or genrule", name)
759 }
760 case genHeaderDepTag:
761 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
762 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders,
763 genRule.GeneratedSourceFiles()...)
Dan Willemsen76f08272016-07-09 00:14:08 -0700764 depPaths.Flags = append(depPaths.Flags,
Colin Cross635c3b02016-05-18 15:37:25 -0700765 includeDirsToFlags(android.Paths{genRule.GeneratedHeaderDir()}))
Dan Willemsenb40aab62016-04-20 14:21:14 -0700766 } else {
767 ctx.ModuleErrorf("module %q is not a genrule", name)
768 }
769 default:
Colin Crossc99deeb2016-04-11 15:06:20 -0700770 ctx.ModuleErrorf("depends on non-cc module %q", name)
Colin Crossca860ac2016-01-04 14:34:37 -0800771 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700772 return
773 }
774
775 if !a.Enabled() {
776 ctx.ModuleErrorf("depends on disabled module %q", name)
777 return
778 }
779
Colin Crossa1ad8d12016-06-01 17:09:44 -0700780 if a.Target().Os != ctx.Os() {
781 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), name)
782 return
783 }
784
785 if a.Target().Arch.ArchType != ctx.Arch().ArchType {
786 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700787 return
788 }
789
Dan Willemsena96ff642016-06-07 12:34:45 -0700790 if !cc.outputFile.Valid() {
Colin Crossc99deeb2016-04-11 15:06:20 -0700791 ctx.ModuleErrorf("module %q missing output file", name)
792 return
793 }
794
795 if tag == reuseObjTag {
796 depPaths.ObjFiles = append(depPaths.ObjFiles,
Dan Willemsena96ff642016-06-07 12:34:45 -0700797 cc.compiler.(*libraryCompiler).reuseObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700798 return
799 }
800
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700801 if t, ok := tag.(dependencyTag); ok && t.library {
Dan Willemsena96ff642016-06-07 12:34:45 -0700802 if i, ok := cc.linker.(exportedFlagsProducer); ok {
Dan Willemsen76f08272016-07-09 00:14:08 -0700803 flags := i.exportedFlags()
804 depPaths.Flags = append(depPaths.Flags, flags...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700805
806 if t.reexportFlags {
Dan Willemsen76f08272016-07-09 00:14:08 -0700807 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700808 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700809 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700810
Dan Albert9e10cd42016-08-03 14:12:14 -0700811 checkLinkType(c, cc)
Colin Crossc99deeb2016-04-11 15:06:20 -0700812 }
813
Colin Cross635c3b02016-05-18 15:37:25 -0700814 var depPtr *android.Paths
Colin Crossc99deeb2016-04-11 15:06:20 -0700815
816 switch tag {
Dan Albert914449f2016-06-17 16:45:24 -0700817 case ndkStubDepTag, sharedDepTag, sharedExportDepTag:
Colin Crossc99deeb2016-04-11 15:06:20 -0700818 depPtr = &depPaths.SharedLibs
Dan Albert914449f2016-06-17 16:45:24 -0700819 case lateSharedDepTag, ndkLateStubDepTag:
Colin Crossc99deeb2016-04-11 15:06:20 -0700820 depPtr = &depPaths.LateSharedLibs
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700821 case staticDepTag, staticExportDepTag:
Colin Crossc99deeb2016-04-11 15:06:20 -0700822 depPtr = &depPaths.StaticLibs
823 case lateStaticDepTag:
824 depPtr = &depPaths.LateStaticLibs
825 case wholeStaticDepTag:
826 depPtr = &depPaths.WholeStaticLibs
Colin Crossc7a38dc2016-07-12 13:13:09 -0700827 staticLib, _ := cc.linker.(libraryInterface)
Colin Crossc99deeb2016-04-11 15:06:20 -0700828 if staticLib == nil || !staticLib.static() {
Dan Willemsena96ff642016-06-07 12:34:45 -0700829 ctx.ModuleErrorf("module %q not a static library", name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700830 return
831 }
832
833 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
834 postfix := " (required by " + ctx.OtherModuleName(m) + ")"
835 for i := range missingDeps {
836 missingDeps[i] += postfix
837 }
838 ctx.AddMissingDependencies(missingDeps)
839 }
840 depPaths.WholeStaticLibObjFiles =
Colin Crossc7a38dc2016-07-12 13:13:09 -0700841 append(depPaths.WholeStaticLibObjFiles, staticLib.objs()...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700842 case objDepTag:
843 depPtr = &depPaths.ObjFiles
844 case crtBeginDepTag:
Dan Willemsena96ff642016-06-07 12:34:45 -0700845 depPaths.CrtBegin = cc.outputFile
Colin Crossc99deeb2016-04-11 15:06:20 -0700846 case crtEndDepTag:
Dan Willemsena96ff642016-06-07 12:34:45 -0700847 depPaths.CrtEnd = cc.outputFile
Colin Crossc99deeb2016-04-11 15:06:20 -0700848 default:
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700849 panic(fmt.Errorf("unknown dependency tag: %s", tag))
Colin Crossc99deeb2016-04-11 15:06:20 -0700850 }
851
852 if depPtr != nil {
Dan Willemsena96ff642016-06-07 12:34:45 -0700853 *depPtr = append(*depPtr, cc.outputFile.Path())
Colin Crossca860ac2016-01-04 14:34:37 -0800854 }
855 })
856
857 return depPaths
858}
859
860func (c *Module) InstallInData() bool {
861 if c.installer == nil {
862 return false
863 }
864 return c.installer.inData()
865}
866
Colin Cross2ba19d92015-05-07 15:44:20 -0700867//
Colin Crosscfad1192015-11-02 16:43:11 -0800868// Defaults
869//
Colin Crossca860ac2016-01-04 14:34:37 -0800870type Defaults struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700871 android.ModuleBase
872 android.DefaultsModule
Colin Crosscfad1192015-11-02 16:43:11 -0800873}
874
Colin Cross635c3b02016-05-18 15:37:25 -0700875func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -0800876}
877
Colin Crossca860ac2016-01-04 14:34:37 -0800878func defaultsFactory() (blueprint.Module, []interface{}) {
879 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -0800880
881 propertyStructs := []interface{}{
Colin Crossca860ac2016-01-04 14:34:37 -0800882 &BaseProperties{},
883 &BaseCompilerProperties{},
884 &BaseLinkerProperties{},
885 &LibraryCompilerProperties{},
Colin Cross919281a2016-04-05 16:42:05 -0700886 &FlagExporterProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -0800887 &LibraryLinkerProperties{},
888 &BinaryLinkerProperties{},
889 &TestLinkerProperties{},
890 &UnusedProperties{},
891 &StlProperties{},
Colin Cross16b23492016-01-06 14:41:07 -0800892 &SanitizeProperties{},
Colin Cross665dce92016-04-28 14:50:03 -0700893 &StripProperties{},
Colin Crosscfad1192015-11-02 16:43:11 -0800894 }
895
Colin Cross635c3b02016-05-18 15:37:25 -0700896 _, propertyStructs = android.InitAndroidArchModule(module, android.HostAndDeviceDefault,
897 android.MultilibDefault, propertyStructs...)
Colin Crosscfad1192015-11-02 16:43:11 -0800898
Colin Cross635c3b02016-05-18 15:37:25 -0700899 return android.InitDefaultsModule(module, module, propertyStructs...)
Colin Crosscfad1192015-11-02 16:43:11 -0800900}
901
902//
Colin Cross3f40fa42015-01-30 17:27:36 -0800903// Device libraries shipped with gcc
904//
905
Colin Crossca860ac2016-01-04 14:34:37 -0800906type toolchainLibraryLinker struct {
907 baseLinker
Colin Cross3f40fa42015-01-30 17:27:36 -0800908}
909
Colin Crossca860ac2016-01-04 14:34:37 -0800910var _ baseLinkerInterface = (*toolchainLibraryLinker)(nil)
911
Colin Cross42742b82016-08-01 13:20:05 -0700912func (*toolchainLibraryLinker) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross3f40fa42015-01-30 17:27:36 -0800913 // toolchain libraries can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -0800914 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -0800915}
916
Colin Crossca860ac2016-01-04 14:34:37 -0800917func (*toolchainLibraryLinker) buildStatic() bool {
918 return true
919}
Colin Cross3f40fa42015-01-30 17:27:36 -0800920
Colin Crossca860ac2016-01-04 14:34:37 -0800921func (*toolchainLibraryLinker) buildShared() bool {
922 return false
923}
924
925func toolchainLibraryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -0700926 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -0800927 module.compiler = &baseCompiler{}
928 module.linker = &toolchainLibraryLinker{}
Dan Willemsenfc9c28c2016-01-12 16:22:40 -0800929 module.Properties.Clang = proptools.BoolPtr(false)
Colin Crossca860ac2016-01-04 14:34:37 -0800930 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -0800931}
932
Colin Crossca860ac2016-01-04 14:34:37 -0800933func (library *toolchainLibraryLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -0700934 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -0800935
936 libName := ctx.ModuleName() + staticLibraryExtension
Colin Cross635c3b02016-05-18 15:37:25 -0700937 outputFile := android.PathForModuleOut(ctx, libName)
Colin Cross3f40fa42015-01-30 17:27:36 -0800938
Dan Willemsenfc9c28c2016-01-12 16:22:40 -0800939 if flags.Clang {
940 ctx.ModuleErrorf("toolchain_library must use GCC, not Clang")
941 }
942
Colin Crossca860ac2016-01-04 14:34:37 -0800943 CopyGccLib(ctx, libName, flagsToBuilderFlags(flags), outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -0800944
945 ctx.CheckbuildFile(outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -0800946
Colin Crossca860ac2016-01-04 14:34:37 -0800947 return outputFile
Dan Albertc403f7c2015-03-18 14:01:18 -0700948}
949
Colin Crossc99deeb2016-04-11 15:06:20 -0700950func (*toolchainLibraryLinker) installable() bool {
951 return false
952}
953
Colin Cross74d1ec02015-04-28 13:30:13 -0700954// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
955// modifies the slice contents in place, and returns a subslice of the original slice
956func lastUniqueElements(list []string) []string {
957 totalSkip := 0
958 for i := len(list) - 1; i >= totalSkip; i-- {
959 skip := 0
960 for j := i - 1; j >= totalSkip; j-- {
961 if list[i] == list[j] {
962 skip++
963 } else {
964 list[j+skip] = list[j]
965 }
966 }
967 totalSkip += skip
968 }
969 return list[totalSkip:]
970}
Colin Cross06a931b2015-10-28 17:23:31 -0700971
972var Bool = proptools.Bool