blob: 62d02c59705d89548784eaec37e159e09e65505a [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)
46 android.RegisterBottomUpMutator("deps", depsMutator)
Colin Cross16b23492016-01-06 14:41:07 -080047
Colin Cross635c3b02016-05-18 15:37:25 -070048 android.RegisterTopDownMutator("asan_deps", sanitizerDepsMutator(asan))
49 android.RegisterBottomUpMutator("asan", sanitizerMutator(asan))
Colin Cross16b23492016-01-06 14:41:07 -080050
Colin Cross635c3b02016-05-18 15:37:25 -070051 android.RegisterTopDownMutator("tsan_deps", sanitizerDepsMutator(tsan))
52 android.RegisterBottomUpMutator("tsan", sanitizerMutator(tsan))
Colin Crossb98c8b02016-07-29 13:44:28 -070053
54 pctx.Import("android/soong/cc/config")
Colin Cross463a90e2015-06-17 14:20:06 -070055}
56
Colin Crossca860ac2016-01-04 14:34:37 -080057type Deps struct {
58 SharedLibs, LateSharedLibs []string
59 StaticLibs, LateStaticLibs, WholeStaticLibs []string
Colin Crossc472d572015-03-17 15:06:21 -070060
Dan Willemsen490a8dc2016-06-06 18:22:19 -070061 ReexportSharedLibHeaders, ReexportStaticLibHeaders []string
62
Colin Cross81413472016-04-11 14:37:39 -070063 ObjFiles []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -070064
Dan Willemsenb40aab62016-04-20 14:21:14 -070065 GeneratedSources []string
66 GeneratedHeaders []string
67
Colin Cross97ba0732015-03-23 17:50:24 -070068 CrtBegin, CrtEnd string
Colin Crossc472d572015-03-17 15:06:21 -070069}
70
Colin Crossca860ac2016-01-04 14:34:37 -080071type PathDeps struct {
Colin Cross635c3b02016-05-18 15:37:25 -070072 SharedLibs, LateSharedLibs android.Paths
73 StaticLibs, LateStaticLibs, WholeStaticLibs android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -070074
Colin Cross635c3b02016-05-18 15:37:25 -070075 ObjFiles android.Paths
76 WholeStaticLibObjFiles android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -070077
Colin Cross635c3b02016-05-18 15:37:25 -070078 GeneratedSources android.Paths
79 GeneratedHeaders android.Paths
Dan Willemsenb40aab62016-04-20 14:21:14 -070080
Dan Willemsen76f08272016-07-09 00:14:08 -070081 Flags, ReexportedFlags []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -070082
Colin Cross635c3b02016-05-18 15:37:25 -070083 CrtBegin, CrtEnd android.OptionalPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -070084}
85
Colin Crossca860ac2016-01-04 14:34:37 -080086type Flags struct {
Colin Cross28344522015-04-22 13:07:53 -070087 GlobalFlags []string // Flags that apply to C, C++, and assembly source files
88 AsFlags []string // Flags that apply to assembly source files
89 CFlags []string // Flags that apply to C and C++ source files
90 ConlyFlags []string // Flags that apply to C source files
91 CppFlags []string // Flags that apply to C++ source files
92 YaccFlags []string // Flags that apply to Yacc source files
93 LdFlags []string // Flags that apply to linker command lines
Colin Cross16b23492016-01-06 14:41:07 -080094 libFlags []string // Flags to add libraries early to the link order
Colin Cross28344522015-04-22 13:07:53 -070095
96 Nocrt bool
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
Colin Cross635c3b02016-05-18 15:37:25 -0700530func (c *Module) depsMutator(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)
540
Colin Crossc99deeb2016-04-11 15:06:20 -0700541 deps := c.deps(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800542
Colin Crossb5bc4b42016-07-11 16:11:59 -0700543 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.SharedLibs...)
544 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.LateSharedLibs...)
Dan Willemsen72d39932016-07-08 23:23:48 -0700545
Dan Albert914449f2016-06-17 16:45:24 -0700546 variantNdkLibs := []string{}
547 variantLateNdkLibs := []string{}
Dan Willemsen72d39932016-07-08 23:23:48 -0700548 if ctx.sdk() {
Dan Albert914449f2016-06-17 16:45:24 -0700549 version := ctx.sdkVersion()
Dan Willemsen72d39932016-07-08 23:23:48 -0700550
Dan Albert914449f2016-06-17 16:45:24 -0700551 // Rewrites the names of shared libraries into the names of the NDK
552 // libraries where appropriate. This returns two slices.
553 //
554 // The first is a list of non-variant shared libraries (either rewritten
555 // NDK libraries to the modules in prebuilts/ndk, or not rewritten
556 // because they are not NDK libraries).
557 //
558 // The second is a list of ndk_library modules. These need to be
559 // separated because they are a variation dependency and must be added
560 // in a different manner.
561 rewriteNdkLibs := func(list []string) ([]string, []string) {
562 variantLibs := []string{}
563 nonvariantLibs := []string{}
564 for _, entry := range list {
Dan Willemsen72d39932016-07-08 23:23:48 -0700565 if inList(entry, ndkPrebuiltSharedLibraries) {
Dan Albert914449f2016-06-17 16:45:24 -0700566 if !inList(entry, ndkMigratedLibs) {
567 nonvariantLibs = append(nonvariantLibs, entry+".ndk."+version)
568 } else {
569 variantLibs = append(variantLibs, entry+ndkLibrarySuffix)
570 }
571 } else {
572 nonvariantLibs = append(variantLibs, entry)
Dan Willemsen72d39932016-07-08 23:23:48 -0700573 }
574 }
Dan Albert914449f2016-06-17 16:45:24 -0700575 return nonvariantLibs, variantLibs
Dan Willemsen72d39932016-07-08 23:23:48 -0700576 }
577
Dan Albert914449f2016-06-17 16:45:24 -0700578 deps.SharedLibs, variantNdkLibs = rewriteNdkLibs(deps.SharedLibs)
579 deps.LateSharedLibs, variantLateNdkLibs = rewriteNdkLibs(deps.LateSharedLibs)
Dan Willemsen72d39932016-07-08 23:23:48 -0700580 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700581
582 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, wholeStaticDepTag,
583 deps.WholeStaticLibs...)
584
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700585 for _, lib := range deps.StaticLibs {
586 depTag := staticDepTag
587 if inList(lib, deps.ReexportStaticLibHeaders) {
588 depTag = staticExportDepTag
589 }
Colin Cross15a0d462016-07-14 14:49:58 -0700590 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700591 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700592
593 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, lateStaticDepTag,
594 deps.LateStaticLibs...)
595
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700596 for _, lib := range deps.SharedLibs {
597 depTag := sharedDepTag
598 if inList(lib, deps.ReexportSharedLibHeaders) {
599 depTag = sharedExportDepTag
600 }
Colin Cross15a0d462016-07-14 14:49:58 -0700601 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700602 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700603
604 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, lateSharedDepTag,
605 deps.LateSharedLibs...)
606
Colin Cross68861832016-07-08 10:41:41 -0700607 actx.AddDependency(c, genSourceDepTag, deps.GeneratedSources...)
608 actx.AddDependency(c, genHeaderDepTag, deps.GeneratedHeaders...)
Dan Willemsenb40aab62016-04-20 14:21:14 -0700609
Colin Cross68861832016-07-08 10:41:41 -0700610 actx.AddDependency(c, objDepTag, deps.ObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700611
612 if deps.CrtBegin != "" {
Colin Cross68861832016-07-08 10:41:41 -0700613 actx.AddDependency(c, crtBeginDepTag, deps.CrtBegin)
Colin Crossca860ac2016-01-04 14:34:37 -0800614 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700615 if deps.CrtEnd != "" {
Colin Cross68861832016-07-08 10:41:41 -0700616 actx.AddDependency(c, crtEndDepTag, deps.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700617 }
Dan Albert914449f2016-06-17 16:45:24 -0700618
619 version := ctx.sdkVersion()
620 actx.AddVariationDependencies([]blueprint.Variation{
621 {"ndk_api", version}, {"link", "shared"}}, ndkStubDepTag, variantNdkLibs...)
622 actx.AddVariationDependencies([]blueprint.Variation{
623 {"ndk_api", version}, {"link", "shared"}}, ndkLateStubDepTag, variantLateNdkLibs...)
Colin Cross6362e272015-10-29 15:25:03 -0700624}
Colin Cross21b9a242015-03-24 14:15:58 -0700625
Colin Cross635c3b02016-05-18 15:37:25 -0700626func depsMutator(ctx android.BottomUpMutatorContext) {
Dan Willemsen3f32f032016-07-11 14:36:48 -0700627 if c, ok := ctx.Module().(*Module); ok && c.Enabled() {
Colin Cross6362e272015-10-29 15:25:03 -0700628 c.depsMutator(ctx)
629 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800630}
631
Colin Crossca860ac2016-01-04 14:34:37 -0800632func (c *Module) clang(ctx BaseModuleContext) bool {
633 clang := Bool(c.Properties.Clang)
634
635 if c.Properties.Clang == nil {
636 if ctx.Host() {
637 clang = true
638 }
639
640 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
641 clang = true
642 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800643 }
Colin Cross28344522015-04-22 13:07:53 -0700644
Colin Crossca860ac2016-01-04 14:34:37 -0800645 if !c.toolchain(ctx).ClangSupported() {
646 clang = false
647 }
648
649 return clang
650}
651
Colin Crossc99deeb2016-04-11 15:06:20 -0700652// Convert dependencies to paths. Returns a PathDeps containing paths
Colin Cross635c3b02016-05-18 15:37:25 -0700653func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
Colin Crossca860ac2016-01-04 14:34:37 -0800654 var depPaths PathDeps
Colin Crossca860ac2016-01-04 14:34:37 -0800655
Dan Willemsena96ff642016-06-07 12:34:45 -0700656 // Whether a module can link to another module, taking into
657 // account NDK linking.
Dan Albert9e10cd42016-08-03 14:12:14 -0700658 checkLinkType := func(from, to *Module) {
Dan Willemsena96ff642016-06-07 12:34:45 -0700659 if from.Target().Os != android.Android {
660 // Host code is not restricted
Dan Albert9e10cd42016-08-03 14:12:14 -0700661 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700662 }
663 if from.Properties.Sdk_version == "" {
664 // Platform code can link to anything
Dan Albert9e10cd42016-08-03 14:12:14 -0700665 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700666 }
667 if _, ok := to.linker.(*toolchainLibraryLinker); ok {
668 // These are always allowed
Dan Albert9e10cd42016-08-03 14:12:14 -0700669 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700670 }
671 if _, ok := to.linker.(*ndkPrebuiltLibraryLinker); ok {
672 // These are allowed, but don't set sdk_version
Dan Albert9e10cd42016-08-03 14:12:14 -0700673 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700674 }
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700675 if _, ok := to.linker.(*ndkPrebuiltStlLinker); ok {
676 // These are allowed, but don't set sdk_version
Dan Albert9e10cd42016-08-03 14:12:14 -0700677 return
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700678 }
Dan Albert914449f2016-06-17 16:45:24 -0700679 if _, ok := to.linker.(*stubLinker); ok {
680 // These aren't real libraries, but are the stub shared libraries that are included in
681 // the NDK.
Dan Albert9e10cd42016-08-03 14:12:14 -0700682 return
Dan Albert914449f2016-06-17 16:45:24 -0700683 }
Dan Albert9e10cd42016-08-03 14:12:14 -0700684 if to.Properties.Sdk_version == "" {
685 // NDK code linking to platform code is never okay.
686 ctx.ModuleErrorf("depends on non-NDK-built library %q",
687 ctx.OtherModuleName(to))
688 }
689
690 // All this point we know we have two NDK libraries, but we need to
691 // check that we're not linking against anything built against a higher
692 // API level, as it is only valid to link against older or equivalent
693 // APIs.
694
695 if from.Properties.Sdk_version == "current" {
696 // Current can link against anything.
697 return
698 } else if to.Properties.Sdk_version == "current" {
699 // Current can't be linked against by anything else.
700 ctx.ModuleErrorf("links %q built against newer API version %q",
701 ctx.OtherModuleName(to), "current")
702 }
703
704 fromApi, err := strconv.Atoi(from.Properties.Sdk_version)
705 if err != nil {
706 ctx.PropertyErrorf("sdk_version",
707 "Invalid sdk_version value (must be int): %q",
708 from.Properties.Sdk_version)
709 }
710 toApi, err := strconv.Atoi(to.Properties.Sdk_version)
711 if err != nil {
712 ctx.PropertyErrorf("sdk_version",
713 "Invalid sdk_version value (must be int): %q",
714 to.Properties.Sdk_version)
715 }
716
717 if toApi > fromApi {
718 ctx.ModuleErrorf("links %q built against newer API version %q",
719 ctx.OtherModuleName(to), to.Properties.Sdk_version)
720 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700721 }
722
Colin Crossc99deeb2016-04-11 15:06:20 -0700723 ctx.VisitDirectDeps(func(m blueprint.Module) {
724 name := ctx.OtherModuleName(m)
725 tag := ctx.OtherModuleDependencyTag(m)
Colin Crossca860ac2016-01-04 14:34:37 -0800726
Colin Cross635c3b02016-05-18 15:37:25 -0700727 a, _ := m.(android.Module)
Colin Crossc99deeb2016-04-11 15:06:20 -0700728 if a == nil {
729 ctx.ModuleErrorf("module %q not an android module", name)
730 return
Colin Crossca860ac2016-01-04 14:34:37 -0800731 }
Colin Crossca860ac2016-01-04 14:34:37 -0800732
Dan Willemsena96ff642016-06-07 12:34:45 -0700733 cc, _ := m.(*Module)
734 if cc == nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700735 switch tag {
Colin Cross635c3b02016-05-18 15:37:25 -0700736 case android.DefaultsDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700737 case genSourceDepTag:
738 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
739 depPaths.GeneratedSources = append(depPaths.GeneratedSources,
740 genRule.GeneratedSourceFiles()...)
741 } else {
742 ctx.ModuleErrorf("module %q is not a gensrcs or genrule", name)
743 }
744 case genHeaderDepTag:
745 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
746 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders,
747 genRule.GeneratedSourceFiles()...)
Dan Willemsen76f08272016-07-09 00:14:08 -0700748 depPaths.Flags = append(depPaths.Flags,
Colin Cross635c3b02016-05-18 15:37:25 -0700749 includeDirsToFlags(android.Paths{genRule.GeneratedHeaderDir()}))
Dan Willemsenb40aab62016-04-20 14:21:14 -0700750 } else {
751 ctx.ModuleErrorf("module %q is not a genrule", name)
752 }
753 default:
Colin Crossc99deeb2016-04-11 15:06:20 -0700754 ctx.ModuleErrorf("depends on non-cc module %q", name)
Colin Crossca860ac2016-01-04 14:34:37 -0800755 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700756 return
757 }
758
759 if !a.Enabled() {
760 ctx.ModuleErrorf("depends on disabled module %q", name)
761 return
762 }
763
Colin Crossa1ad8d12016-06-01 17:09:44 -0700764 if a.Target().Os != ctx.Os() {
765 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), name)
766 return
767 }
768
769 if a.Target().Arch.ArchType != ctx.Arch().ArchType {
770 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700771 return
772 }
773
Dan Willemsena96ff642016-06-07 12:34:45 -0700774 if !cc.outputFile.Valid() {
Colin Crossc99deeb2016-04-11 15:06:20 -0700775 ctx.ModuleErrorf("module %q missing output file", name)
776 return
777 }
778
779 if tag == reuseObjTag {
780 depPaths.ObjFiles = append(depPaths.ObjFiles,
Dan Willemsena96ff642016-06-07 12:34:45 -0700781 cc.compiler.(*libraryCompiler).reuseObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700782 return
783 }
784
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700785 if t, ok := tag.(dependencyTag); ok && t.library {
Dan Willemsena96ff642016-06-07 12:34:45 -0700786 if i, ok := cc.linker.(exportedFlagsProducer); ok {
Dan Willemsen76f08272016-07-09 00:14:08 -0700787 flags := i.exportedFlags()
788 depPaths.Flags = append(depPaths.Flags, flags...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700789
790 if t.reexportFlags {
Dan Willemsen76f08272016-07-09 00:14:08 -0700791 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700792 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700793 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700794
Dan Albert9e10cd42016-08-03 14:12:14 -0700795 checkLinkType(c, cc)
Colin Crossc99deeb2016-04-11 15:06:20 -0700796 }
797
Colin Cross635c3b02016-05-18 15:37:25 -0700798 var depPtr *android.Paths
Colin Crossc99deeb2016-04-11 15:06:20 -0700799
800 switch tag {
Dan Albert914449f2016-06-17 16:45:24 -0700801 case ndkStubDepTag, sharedDepTag, sharedExportDepTag:
Colin Crossc99deeb2016-04-11 15:06:20 -0700802 depPtr = &depPaths.SharedLibs
Dan Albert914449f2016-06-17 16:45:24 -0700803 case lateSharedDepTag, ndkLateStubDepTag:
Colin Crossc99deeb2016-04-11 15:06:20 -0700804 depPtr = &depPaths.LateSharedLibs
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700805 case staticDepTag, staticExportDepTag:
Colin Crossc99deeb2016-04-11 15:06:20 -0700806 depPtr = &depPaths.StaticLibs
807 case lateStaticDepTag:
808 depPtr = &depPaths.LateStaticLibs
809 case wholeStaticDepTag:
810 depPtr = &depPaths.WholeStaticLibs
Colin Crossc7a38dc2016-07-12 13:13:09 -0700811 staticLib, _ := cc.linker.(libraryInterface)
Colin Crossc99deeb2016-04-11 15:06:20 -0700812 if staticLib == nil || !staticLib.static() {
Dan Willemsena96ff642016-06-07 12:34:45 -0700813 ctx.ModuleErrorf("module %q not a static library", name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700814 return
815 }
816
817 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
818 postfix := " (required by " + ctx.OtherModuleName(m) + ")"
819 for i := range missingDeps {
820 missingDeps[i] += postfix
821 }
822 ctx.AddMissingDependencies(missingDeps)
823 }
824 depPaths.WholeStaticLibObjFiles =
Colin Crossc7a38dc2016-07-12 13:13:09 -0700825 append(depPaths.WholeStaticLibObjFiles, staticLib.objs()...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700826 case objDepTag:
827 depPtr = &depPaths.ObjFiles
828 case crtBeginDepTag:
Dan Willemsena96ff642016-06-07 12:34:45 -0700829 depPaths.CrtBegin = cc.outputFile
Colin Crossc99deeb2016-04-11 15:06:20 -0700830 case crtEndDepTag:
Dan Willemsena96ff642016-06-07 12:34:45 -0700831 depPaths.CrtEnd = cc.outputFile
Colin Crossc99deeb2016-04-11 15:06:20 -0700832 default:
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700833 panic(fmt.Errorf("unknown dependency tag: %s", tag))
Colin Crossc99deeb2016-04-11 15:06:20 -0700834 }
835
836 if depPtr != nil {
Dan Willemsena96ff642016-06-07 12:34:45 -0700837 *depPtr = append(*depPtr, cc.outputFile.Path())
Colin Crossca860ac2016-01-04 14:34:37 -0800838 }
839 })
840
841 return depPaths
842}
843
844func (c *Module) InstallInData() bool {
845 if c.installer == nil {
846 return false
847 }
848 return c.installer.inData()
849}
850
Colin Cross2ba19d92015-05-07 15:44:20 -0700851//
Colin Crosscfad1192015-11-02 16:43:11 -0800852// Defaults
853//
Colin Crossca860ac2016-01-04 14:34:37 -0800854type Defaults struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700855 android.ModuleBase
856 android.DefaultsModule
Colin Crosscfad1192015-11-02 16:43:11 -0800857}
858
Colin Cross635c3b02016-05-18 15:37:25 -0700859func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -0800860}
861
Colin Crossca860ac2016-01-04 14:34:37 -0800862func defaultsFactory() (blueprint.Module, []interface{}) {
863 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -0800864
865 propertyStructs := []interface{}{
Colin Crossca860ac2016-01-04 14:34:37 -0800866 &BaseProperties{},
867 &BaseCompilerProperties{},
868 &BaseLinkerProperties{},
869 &LibraryCompilerProperties{},
Colin Cross919281a2016-04-05 16:42:05 -0700870 &FlagExporterProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -0800871 &LibraryLinkerProperties{},
872 &BinaryLinkerProperties{},
873 &TestLinkerProperties{},
874 &UnusedProperties{},
875 &StlProperties{},
Colin Cross16b23492016-01-06 14:41:07 -0800876 &SanitizeProperties{},
Colin Cross665dce92016-04-28 14:50:03 -0700877 &StripProperties{},
Colin Crosscfad1192015-11-02 16:43:11 -0800878 }
879
Colin Cross635c3b02016-05-18 15:37:25 -0700880 _, propertyStructs = android.InitAndroidArchModule(module, android.HostAndDeviceDefault,
881 android.MultilibDefault, propertyStructs...)
Colin Crosscfad1192015-11-02 16:43:11 -0800882
Colin Cross635c3b02016-05-18 15:37:25 -0700883 return android.InitDefaultsModule(module, module, propertyStructs...)
Colin Crosscfad1192015-11-02 16:43:11 -0800884}
885
886//
Colin Cross3f40fa42015-01-30 17:27:36 -0800887// Device libraries shipped with gcc
888//
889
Colin Crossca860ac2016-01-04 14:34:37 -0800890type toolchainLibraryLinker struct {
891 baseLinker
Colin Cross3f40fa42015-01-30 17:27:36 -0800892}
893
Colin Crossca860ac2016-01-04 14:34:37 -0800894var _ baseLinkerInterface = (*toolchainLibraryLinker)(nil)
895
Colin Cross42742b82016-08-01 13:20:05 -0700896func (*toolchainLibraryLinker) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross3f40fa42015-01-30 17:27:36 -0800897 // toolchain libraries can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -0800898 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -0800899}
900
Colin Crossca860ac2016-01-04 14:34:37 -0800901func (*toolchainLibraryLinker) buildStatic() bool {
902 return true
903}
Colin Cross3f40fa42015-01-30 17:27:36 -0800904
Colin Crossca860ac2016-01-04 14:34:37 -0800905func (*toolchainLibraryLinker) buildShared() bool {
906 return false
907}
908
909func toolchainLibraryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -0700910 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -0800911 module.compiler = &baseCompiler{}
912 module.linker = &toolchainLibraryLinker{}
Dan Willemsenfc9c28c2016-01-12 16:22:40 -0800913 module.Properties.Clang = proptools.BoolPtr(false)
Colin Crossca860ac2016-01-04 14:34:37 -0800914 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -0800915}
916
Colin Crossca860ac2016-01-04 14:34:37 -0800917func (library *toolchainLibraryLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -0700918 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -0800919
920 libName := ctx.ModuleName() + staticLibraryExtension
Colin Cross635c3b02016-05-18 15:37:25 -0700921 outputFile := android.PathForModuleOut(ctx, libName)
Colin Cross3f40fa42015-01-30 17:27:36 -0800922
Dan Willemsenfc9c28c2016-01-12 16:22:40 -0800923 if flags.Clang {
924 ctx.ModuleErrorf("toolchain_library must use GCC, not Clang")
925 }
926
Colin Crossca860ac2016-01-04 14:34:37 -0800927 CopyGccLib(ctx, libName, flagsToBuilderFlags(flags), outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -0800928
929 ctx.CheckbuildFile(outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -0800930
Colin Crossca860ac2016-01-04 14:34:37 -0800931 return outputFile
Dan Albertc403f7c2015-03-18 14:01:18 -0700932}
933
Colin Crossc99deeb2016-04-11 15:06:20 -0700934func (*toolchainLibraryLinker) installable() bool {
935 return false
936}
937
Colin Cross74d1ec02015-04-28 13:30:13 -0700938// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
939// modifies the slice contents in place, and returns a subslice of the original slice
940func lastUniqueElements(list []string) []string {
941 totalSkip := 0
942 for i := len(list) - 1; i >= totalSkip; i-- {
943 skip := 0
944 for j := i - 1; j >= totalSkip; j-- {
945 if list[i] == list[j] {
946 skip++
947 } else {
948 list[j+skip] = list[j]
949 }
950 }
951 totalSkip += skip
952 }
953 return list[totalSkip:]
954}
Colin Cross06a931b2015-10-28 17:23:31 -0700955
956var Bool = proptools.Bool