blob: 8c2fe6fee9b5ea0b4be7a130ff764d08d0c12d1a [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"
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 Cross463a90e2015-06-17 14:20:06 -070028 "android/soong"
Colin Cross635c3b02016-05-18 15:37:25 -070029 "android/soong/android"
Colin Crossb98c8b02016-07-29 13:44:28 -070030 "android/soong/cc/config"
Colin Cross5049f022015-03-18 13:28:46 -070031 "android/soong/genrule"
Colin Cross3f40fa42015-01-30 17:27:36 -080032)
33
Colin Cross463a90e2015-06-17 14:20:06 -070034func init() {
Colin Crossca860ac2016-01-04 14:34:37 -080035 soong.RegisterModuleType("cc_defaults", defaultsFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070036
Colin Crossca860ac2016-01-04 14:34:37 -080037 soong.RegisterModuleType("toolchain_library", toolchainLibraryFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070038
39 // LinkageMutator must be registered after common.ArchMutator, but that is guaranteed by
40 // the Go initialization order because this package depends on common, so common's init
41 // functions will run first.
Colin Cross635c3b02016-05-18 15:37:25 -070042 android.RegisterBottomUpMutator("link", linkageMutator)
Dan Albert914449f2016-06-17 16:45:24 -070043 android.RegisterBottomUpMutator("ndk_api", ndkApiMutator)
Colin Cross635c3b02016-05-18 15:37:25 -070044 android.RegisterBottomUpMutator("test_per_src", testPerSrcMutator)
45 android.RegisterBottomUpMutator("deps", depsMutator)
Colin Cross16b23492016-01-06 14:41:07 -080046
Colin Cross635c3b02016-05-18 15:37:25 -070047 android.RegisterTopDownMutator("asan_deps", sanitizerDepsMutator(asan))
48 android.RegisterBottomUpMutator("asan", sanitizerMutator(asan))
Colin Cross16b23492016-01-06 14:41:07 -080049
Colin Cross635c3b02016-05-18 15:37:25 -070050 android.RegisterTopDownMutator("tsan_deps", sanitizerDepsMutator(tsan))
51 android.RegisterBottomUpMutator("tsan", sanitizerMutator(tsan))
Colin Crossb98c8b02016-07-29 13:44:28 -070052
53 pctx.Import("android/soong/cc/config")
Colin Cross463a90e2015-06-17 14:20:06 -070054}
55
Colin Crossca860ac2016-01-04 14:34:37 -080056type Deps struct {
57 SharedLibs, LateSharedLibs []string
58 StaticLibs, LateStaticLibs, WholeStaticLibs []string
Colin Crossc472d572015-03-17 15:06:21 -070059
Dan Willemsen490a8dc2016-06-06 18:22:19 -070060 ReexportSharedLibHeaders, ReexportStaticLibHeaders []string
61
Colin Cross81413472016-04-11 14:37:39 -070062 ObjFiles []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -070063
Dan Willemsenb40aab62016-04-20 14:21:14 -070064 GeneratedSources []string
65 GeneratedHeaders []string
66
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 Cross635c3b02016-05-18 15:37:25 -070071 SharedLibs, LateSharedLibs android.Paths
72 StaticLibs, LateStaticLibs, WholeStaticLibs android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -070073
Colin Cross635c3b02016-05-18 15:37:25 -070074 ObjFiles android.Paths
75 WholeStaticLibObjFiles android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -070076
Colin Cross635c3b02016-05-18 15:37:25 -070077 GeneratedSources android.Paths
78 GeneratedHeaders android.Paths
Dan Willemsenb40aab62016-04-20 14:21:14 -070079
Dan Willemsen76f08272016-07-09 00:14:08 -070080 Flags, ReexportedFlags []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -070081
Colin Cross635c3b02016-05-18 15:37:25 -070082 CrtBegin, CrtEnd android.OptionalPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -070083}
84
Colin Crossca860ac2016-01-04 14:34:37 -080085type Flags struct {
Colin Cross28344522015-04-22 13:07:53 -070086 GlobalFlags []string // Flags that apply to C, C++, and assembly source files
87 AsFlags []string // Flags that apply to assembly source files
88 CFlags []string // Flags that apply to C and C++ source files
89 ConlyFlags []string // Flags that apply to C source files
90 CppFlags []string // Flags that apply to C++ source files
91 YaccFlags []string // Flags that apply to Yacc source files
92 LdFlags []string // Flags that apply to linker command lines
Colin Cross16b23492016-01-06 14:41:07 -080093 libFlags []string // Flags to add libraries early to the link order
Colin Cross28344522015-04-22 13:07:53 -070094
95 Nocrt bool
Colin Crossb98c8b02016-07-29 13:44:28 -070096 Toolchain config.Toolchain
Colin Cross28344522015-04-22 13:07:53 -070097 Clang bool
Colin Crossca860ac2016-01-04 14:34:37 -080098
99 RequiredInstructionSet string
Colin Cross16b23492016-01-06 14:41:07 -0800100 DynamicLinker string
101
Colin Cross635c3b02016-05-18 15:37:25 -0700102 CFlagsDeps android.Paths // Files depended on by compiler flags
Colin Crossc472d572015-03-17 15:06:21 -0700103}
104
Colin Cross81413472016-04-11 14:37:39 -0700105type ObjectLinkerProperties struct {
106 // names of other cc_object modules to link into this module using partial linking
107 Objs []string `android:"arch_variant"`
108}
109
Colin Crossca860ac2016-01-04 14:34:37 -0800110// Properties used to compile all C or C++ modules
111type BaseProperties struct {
112 // compile module with clang instead of gcc
113 Clang *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700114
115 // Minimum sdk version supported when compiling against the ndk
116 Sdk_version string
117
Colin Crossca860ac2016-01-04 14:34:37 -0800118 // don't insert default compiler flags into asflags, cflags,
119 // cppflags, conlyflags, ldflags, or include_dirs
120 No_default_compiler_flags *bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700121
122 AndroidMkSharedLibs []string `blueprint:"mutated"`
Colin Crossbc6fb162016-05-24 15:39:04 -0700123 HideFromMake bool `blueprint:"mutated"`
Colin Crossca860ac2016-01-04 14:34:37 -0800124}
125
Colin Crossca860ac2016-01-04 14:34:37 -0800126type UnusedProperties struct {
Colin Cross21b481b2016-04-15 16:27:17 -0700127 Native_coverage *bool
128 Required []string
Colin Cross21b481b2016-04-15 16:27:17 -0700129 Tags []string
Colin Crosscfad1192015-11-02 16:43:11 -0800130}
131
Colin Crossca860ac2016-01-04 14:34:37 -0800132type ModuleContextIntf interface {
Colin Crossca860ac2016-01-04 14:34:37 -0800133 static() bool
134 staticBinary() bool
135 clang() bool
Colin Crossb98c8b02016-07-29 13:44:28 -0700136 toolchain() config.Toolchain
Colin Crossca860ac2016-01-04 14:34:37 -0800137 noDefaultCompilerFlags() bool
138 sdk() bool
139 sdkVersion() string
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700140 selectedStl() string
Colin Crossca860ac2016-01-04 14:34:37 -0800141}
142
143type ModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700144 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800145 ModuleContextIntf
146}
147
148type BaseModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700149 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800150 ModuleContextIntf
151}
152
Colin Cross76fada02016-07-27 10:31:13 -0700153type CustomizerFlagsContext interface {
154 BaseModuleContext
155 AppendCflags(...string)
156 AppendLdflags(...string)
157 AppendAsflags(...string)
158}
159
Colin Crossca860ac2016-01-04 14:34:37 -0800160type Customizer interface {
Colin Cross76fada02016-07-27 10:31:13 -0700161 Flags(CustomizerFlagsContext)
Colin Crossca860ac2016-01-04 14:34:37 -0800162 Properties() []interface{}
163}
164
165type feature interface {
166 begin(ctx BaseModuleContext)
167 deps(ctx BaseModuleContext, deps Deps) Deps
168 flags(ctx ModuleContext, flags Flags) Flags
169 props() []interface{}
170}
171
172type compiler interface {
Colin Cross42742b82016-08-01 13:20:05 -0700173 compilerInit(ctx BaseModuleContext)
174 compilerDeps(ctx BaseModuleContext, deps Deps) Deps
175 compilerFlags(ctx ModuleContext, flags Flags) Flags
176 compilerProps() []interface{}
177
Colin Cross76fada02016-07-27 10:31:13 -0700178 appendCflags([]string)
179 appendAsflags([]string)
Colin Cross635c3b02016-05-18 15:37:25 -0700180 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -0800181}
182
183type linker interface {
Colin Cross42742b82016-08-01 13:20:05 -0700184 linkerInit(ctx BaseModuleContext)
185 linkerDeps(ctx BaseModuleContext, deps Deps) Deps
186 linkerFlags(ctx ModuleContext, flags Flags) Flags
187 linkerProps() []interface{}
188
Colin Cross635c3b02016-05-18 15:37:25 -0700189 link(ctx ModuleContext, flags Flags, deps PathDeps, objFiles android.Paths) android.Path
Colin Cross76fada02016-07-27 10:31:13 -0700190 appendLdflags([]string)
Colin Crossc99deeb2016-04-11 15:06:20 -0700191 installable() bool
Colin Crossca860ac2016-01-04 14:34:37 -0800192}
193
194type installer interface {
Colin Cross42742b82016-08-01 13:20:05 -0700195 installerProps() []interface{}
Colin Cross635c3b02016-05-18 15:37:25 -0700196 install(ctx ModuleContext, path android.Path)
Colin Crossca860ac2016-01-04 14:34:37 -0800197 inData() bool
198}
199
Colin Crossc99deeb2016-04-11 15:06:20 -0700200type dependencyTag struct {
201 blueprint.BaseDependencyTag
202 name string
203 library bool
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700204
205 reexportFlags bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700206}
207
208var (
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700209 sharedDepTag = dependencyTag{name: "shared", library: true}
210 sharedExportDepTag = dependencyTag{name: "shared", library: true, reexportFlags: true}
211 lateSharedDepTag = dependencyTag{name: "late shared", library: true}
212 staticDepTag = dependencyTag{name: "static", library: true}
213 staticExportDepTag = dependencyTag{name: "static", library: true, reexportFlags: true}
214 lateStaticDepTag = dependencyTag{name: "late static", library: true}
215 wholeStaticDepTag = dependencyTag{name: "whole static", library: true, reexportFlags: true}
216 genSourceDepTag = dependencyTag{name: "gen source"}
217 genHeaderDepTag = dependencyTag{name: "gen header"}
218 objDepTag = dependencyTag{name: "obj"}
219 crtBeginDepTag = dependencyTag{name: "crtbegin"}
220 crtEndDepTag = dependencyTag{name: "crtend"}
221 reuseObjTag = dependencyTag{name: "reuse objects"}
Dan Albert914449f2016-06-17 16:45:24 -0700222 ndkStubDepTag = dependencyTag{name: "ndk stub", library: true}
223 ndkLateStubDepTag = dependencyTag{name: "ndk late stub", library: true}
Colin Crossc99deeb2016-04-11 15:06:20 -0700224)
225
Colin Crossca860ac2016-01-04 14:34:37 -0800226// Module contains the properties and members used by all C/C++ module types, and implements
227// the blueprint.Module interface. It delegates to compiler, linker, and installer interfaces
228// to construct the output file. Behavior can be customized with a Customizer interface
229type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700230 android.ModuleBase
231 android.DefaultableModule
Colin Crossc472d572015-03-17 15:06:21 -0700232
Colin Crossca860ac2016-01-04 14:34:37 -0800233 Properties BaseProperties
234 unused UnusedProperties
Colin Crossfa138792015-04-24 17:31:52 -0700235
Colin Crossca860ac2016-01-04 14:34:37 -0800236 // initialize before calling Init
Colin Cross635c3b02016-05-18 15:37:25 -0700237 hod android.HostOrDeviceSupported
238 multilib android.Multilib
Colin Crossc472d572015-03-17 15:06:21 -0700239
Colin Crossca860ac2016-01-04 14:34:37 -0800240 // delegates, initialize before calling Init
Colin Cross76fada02016-07-27 10:31:13 -0700241 Customizer Customizer
Colin Crossca860ac2016-01-04 14:34:37 -0800242 features []feature
243 compiler compiler
244 linker linker
245 installer installer
Colin Crossa8e07cc2016-04-04 15:07:06 -0700246 stl *stl
Colin Cross16b23492016-01-06 14:41:07 -0800247 sanitize *sanitize
248
249 androidMkSharedLibDeps []string
Colin Cross74d1ec02015-04-28 13:30:13 -0700250
Colin Cross635c3b02016-05-18 15:37:25 -0700251 outputFile android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800252
Colin Crossb98c8b02016-07-29 13:44:28 -0700253 cachedToolchain config.Toolchain
Colin Crossc472d572015-03-17 15:06:21 -0700254}
255
Colin Crossca860ac2016-01-04 14:34:37 -0800256func (c *Module) Init() (blueprint.Module, []interface{}) {
257 props := []interface{}{&c.Properties, &c.unused}
Colin Cross76fada02016-07-27 10:31:13 -0700258 if c.Customizer != nil {
259 props = append(props, c.Customizer.Properties()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800260 }
261 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700262 props = append(props, c.compiler.compilerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800263 }
264 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700265 props = append(props, c.linker.linkerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800266 }
267 if c.installer != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700268 props = append(props, c.installer.installerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800269 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700270 if c.stl != nil {
271 props = append(props, c.stl.props()...)
272 }
Colin Cross16b23492016-01-06 14:41:07 -0800273 if c.sanitize != nil {
274 props = append(props, c.sanitize.props()...)
275 }
Colin Crossca860ac2016-01-04 14:34:37 -0800276 for _, feature := range c.features {
277 props = append(props, feature.props()...)
278 }
Colin Crossc472d572015-03-17 15:06:21 -0700279
Colin Cross635c3b02016-05-18 15:37:25 -0700280 _, props = android.InitAndroidArchModule(c, c.hod, c.multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700281
Colin Cross635c3b02016-05-18 15:37:25 -0700282 return android.InitDefaultableModule(c, c, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700283}
284
Colin Crossca860ac2016-01-04 14:34:37 -0800285type baseModuleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700286 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800287 moduleContextImpl
288}
289
290type moduleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700291 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800292 moduleContextImpl
293}
294
295type moduleContextImpl struct {
296 mod *Module
297 ctx BaseModuleContext
298}
299
Colin Cross76fada02016-07-27 10:31:13 -0700300func (ctx *moduleContextImpl) AppendCflags(flags ...string) {
301 CheckBadCompilerFlags(ctx.ctx, "", flags)
302 ctx.mod.compiler.appendCflags(flags)
303}
304
305func (ctx *moduleContextImpl) AppendAsflags(flags ...string) {
306 CheckBadCompilerFlags(ctx.ctx, "", flags)
307 ctx.mod.compiler.appendAsflags(flags)
308}
309
310func (ctx *moduleContextImpl) AppendLdflags(flags ...string) {
311 CheckBadLinkerFlags(ctx.ctx, "", flags)
312 ctx.mod.linker.appendLdflags(flags)
313}
314
Colin Crossca860ac2016-01-04 14:34:37 -0800315func (ctx *moduleContextImpl) clang() bool {
316 return ctx.mod.clang(ctx.ctx)
317}
318
Colin Crossb98c8b02016-07-29 13:44:28 -0700319func (ctx *moduleContextImpl) toolchain() config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800320 return ctx.mod.toolchain(ctx.ctx)
321}
322
323func (ctx *moduleContextImpl) static() bool {
324 if ctx.mod.linker == nil {
325 panic(fmt.Errorf("static called on module %q with no linker", ctx.ctx.ModuleName()))
326 }
327 if linker, ok := ctx.mod.linker.(baseLinkerInterface); ok {
328 return linker.static()
329 } else {
330 panic(fmt.Errorf("static called on module %q that doesn't use base linker", ctx.ctx.ModuleName()))
331 }
332}
333
334func (ctx *moduleContextImpl) staticBinary() bool {
335 if ctx.mod.linker == nil {
336 panic(fmt.Errorf("staticBinary called on module %q with no linker", ctx.ctx.ModuleName()))
337 }
338 if linker, ok := ctx.mod.linker.(baseLinkerInterface); ok {
339 return linker.staticBinary()
340 } else {
341 panic(fmt.Errorf("staticBinary called on module %q that doesn't use base linker", ctx.ctx.ModuleName()))
342 }
343}
344
345func (ctx *moduleContextImpl) noDefaultCompilerFlags() bool {
346 return Bool(ctx.mod.Properties.No_default_compiler_flags)
347}
348
349func (ctx *moduleContextImpl) sdk() bool {
Dan Willemsena96ff642016-06-07 12:34:45 -0700350 if ctx.ctx.Device() {
351 return ctx.mod.Properties.Sdk_version != ""
352 }
353 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800354}
355
356func (ctx *moduleContextImpl) sdkVersion() string {
Dan Willemsena96ff642016-06-07 12:34:45 -0700357 if ctx.ctx.Device() {
358 return ctx.mod.Properties.Sdk_version
359 }
360 return ""
Colin Crossca860ac2016-01-04 14:34:37 -0800361}
362
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700363func (ctx *moduleContextImpl) selectedStl() string {
364 if stl := ctx.mod.stl; stl != nil {
365 return stl.Properties.SelectedStl
366 }
367 return ""
368}
369
Colin Cross635c3b02016-05-18 15:37:25 -0700370func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800371 return &Module{
372 hod: hod,
373 multilib: multilib,
374 }
375}
376
Colin Cross635c3b02016-05-18 15:37:25 -0700377func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800378 module := newBaseModule(hod, multilib)
Colin Crossa8e07cc2016-04-04 15:07:06 -0700379 module.stl = &stl{}
Colin Cross16b23492016-01-06 14:41:07 -0800380 module.sanitize = &sanitize{}
Colin Crossca860ac2016-01-04 14:34:37 -0800381 return module
382}
383
Colin Cross635c3b02016-05-18 15:37:25 -0700384func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800385 ctx := &moduleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700386 ModuleContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800387 moduleContextImpl: moduleContextImpl{
388 mod: c,
389 },
390 }
391 ctx.ctx = ctx
392
Colin Cross76fada02016-07-27 10:31:13 -0700393 if c.Customizer != nil {
394 c.Customizer.Flags(ctx)
395 }
396
Colin Crossca860ac2016-01-04 14:34:37 -0800397 flags := Flags{
398 Toolchain: c.toolchain(ctx),
399 Clang: c.clang(ctx),
400 }
Colin Crossca860ac2016-01-04 14:34:37 -0800401 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700402 flags = c.compiler.compilerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800403 }
404 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700405 flags = c.linker.linkerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800406 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700407 if c.stl != nil {
408 flags = c.stl.flags(ctx, flags)
409 }
Colin Cross16b23492016-01-06 14:41:07 -0800410 if c.sanitize != nil {
411 flags = c.sanitize.flags(ctx, flags)
412 }
Colin Crossca860ac2016-01-04 14:34:37 -0800413 for _, feature := range c.features {
414 flags = feature.flags(ctx, flags)
415 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800416 if ctx.Failed() {
417 return
418 }
419
Colin Crossb98c8b02016-07-29 13:44:28 -0700420 flags.CFlags, _ = filterList(flags.CFlags, config.IllegalFlags)
421 flags.CppFlags, _ = filterList(flags.CppFlags, config.IllegalFlags)
422 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, config.IllegalFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800423
Colin Crossca860ac2016-01-04 14:34:37 -0800424 // Optimization to reduce size of build.ninja
425 // Replace the long list of flags for each file with a module-local variable
426 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
427 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
428 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
429 flags.CFlags = []string{"$cflags"}
430 flags.CppFlags = []string{"$cppflags"}
431 flags.AsFlags = []string{"$asflags"}
432
Colin Crossc99deeb2016-04-11 15:06:20 -0700433 deps := c.depsToPaths(ctx)
Colin Cross3f40fa42015-01-30 17:27:36 -0800434 if ctx.Failed() {
435 return
436 }
437
Dan Willemsen76f08272016-07-09 00:14:08 -0700438 flags.GlobalFlags = append(flags.GlobalFlags, deps.Flags...)
Colin Crossed9f8682015-03-18 17:17:35 -0700439
Colin Cross635c3b02016-05-18 15:37:25 -0700440 var objFiles android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -0800441 if c.compiler != nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700442 objFiles = c.compiler.compile(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -0800443 if ctx.Failed() {
444 return
445 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800446 }
447
Colin Crossca860ac2016-01-04 14:34:37 -0800448 if c.linker != nil {
449 outputFile := c.linker.link(ctx, flags, deps, objFiles)
450 if ctx.Failed() {
451 return
452 }
Colin Cross635c3b02016-05-18 15:37:25 -0700453 c.outputFile = android.OptionalPathForPath(outputFile)
Colin Cross5049f022015-03-18 13:28:46 -0700454
Colin Crossc99deeb2016-04-11 15:06:20 -0700455 if c.installer != nil && c.linker.installable() {
Colin Crossca860ac2016-01-04 14:34:37 -0800456 c.installer.install(ctx, outputFile)
457 if ctx.Failed() {
458 return
459 }
460 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700461 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800462}
463
Colin Crossb98c8b02016-07-29 13:44:28 -0700464func (c *Module) toolchain(ctx BaseModuleContext) config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800465 if c.cachedToolchain == nil {
Colin Crossb98c8b02016-07-29 13:44:28 -0700466 c.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
Colin Cross3f40fa42015-01-30 17:27:36 -0800467 }
Colin Crossca860ac2016-01-04 14:34:37 -0800468 return c.cachedToolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800469}
470
Colin Crossca860ac2016-01-04 14:34:37 -0800471func (c *Module) begin(ctx BaseModuleContext) {
472 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700473 c.compiler.compilerInit(ctx)
Colin Cross21b9a242015-03-24 14:15:58 -0700474 }
Colin Crossca860ac2016-01-04 14:34:37 -0800475 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700476 c.linker.linkerInit(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800477 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700478 if c.stl != nil {
479 c.stl.begin(ctx)
480 }
Colin Cross16b23492016-01-06 14:41:07 -0800481 if c.sanitize != nil {
482 c.sanitize.begin(ctx)
483 }
Colin Crossca860ac2016-01-04 14:34:37 -0800484 for _, feature := range c.features {
485 feature.begin(ctx)
486 }
487}
488
Colin Crossc99deeb2016-04-11 15:06:20 -0700489func (c *Module) deps(ctx BaseModuleContext) Deps {
490 deps := Deps{}
491
492 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700493 deps = c.compiler.compilerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700494 }
495 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700496 deps = c.linker.linkerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700497 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700498 if c.stl != nil {
499 deps = c.stl.deps(ctx, deps)
500 }
Colin Cross16b23492016-01-06 14:41:07 -0800501 if c.sanitize != nil {
502 deps = c.sanitize.deps(ctx, deps)
503 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700504 for _, feature := range c.features {
505 deps = feature.deps(ctx, deps)
506 }
507
508 deps.WholeStaticLibs = lastUniqueElements(deps.WholeStaticLibs)
509 deps.StaticLibs = lastUniqueElements(deps.StaticLibs)
510 deps.LateStaticLibs = lastUniqueElements(deps.LateStaticLibs)
511 deps.SharedLibs = lastUniqueElements(deps.SharedLibs)
512 deps.LateSharedLibs = lastUniqueElements(deps.LateSharedLibs)
513
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700514 for _, lib := range deps.ReexportSharedLibHeaders {
515 if !inList(lib, deps.SharedLibs) {
516 ctx.PropertyErrorf("export_shared_lib_headers", "Shared library not in shared_libs: '%s'", lib)
517 }
518 }
519
520 for _, lib := range deps.ReexportStaticLibHeaders {
521 if !inList(lib, deps.StaticLibs) {
522 ctx.PropertyErrorf("export_static_lib_headers", "Static library not in static_libs: '%s'", lib)
523 }
524 }
525
Colin Crossc99deeb2016-04-11 15:06:20 -0700526 return deps
527}
528
Colin Cross635c3b02016-05-18 15:37:25 -0700529func (c *Module) depsMutator(actx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800530 ctx := &baseModuleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700531 BaseContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800532 moduleContextImpl: moduleContextImpl{
533 mod: c,
534 },
535 }
536 ctx.ctx = ctx
537
Colin Crossca860ac2016-01-04 14:34:37 -0800538 c.begin(ctx)
539
Colin Crossc99deeb2016-04-11 15:06:20 -0700540 deps := c.deps(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800541
Colin Crossb5bc4b42016-07-11 16:11:59 -0700542 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.SharedLibs...)
543 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.LateSharedLibs...)
Dan Willemsen72d39932016-07-08 23:23:48 -0700544
Dan Albert914449f2016-06-17 16:45:24 -0700545 variantNdkLibs := []string{}
546 variantLateNdkLibs := []string{}
Dan Willemsen72d39932016-07-08 23:23:48 -0700547 if ctx.sdk() {
Dan Albert914449f2016-06-17 16:45:24 -0700548 version := ctx.sdkVersion()
Dan Willemsen72d39932016-07-08 23:23:48 -0700549
Dan Albert914449f2016-06-17 16:45:24 -0700550 // Rewrites the names of shared libraries into the names of the NDK
551 // libraries where appropriate. This returns two slices.
552 //
553 // The first is a list of non-variant shared libraries (either rewritten
554 // NDK libraries to the modules in prebuilts/ndk, or not rewritten
555 // because they are not NDK libraries).
556 //
557 // The second is a list of ndk_library modules. These need to be
558 // separated because they are a variation dependency and must be added
559 // in a different manner.
560 rewriteNdkLibs := func(list []string) ([]string, []string) {
561 variantLibs := []string{}
562 nonvariantLibs := []string{}
563 for _, entry := range list {
Dan Willemsen72d39932016-07-08 23:23:48 -0700564 if inList(entry, ndkPrebuiltSharedLibraries) {
Dan Albert914449f2016-06-17 16:45:24 -0700565 if !inList(entry, ndkMigratedLibs) {
566 nonvariantLibs = append(nonvariantLibs, entry+".ndk."+version)
567 } else {
568 variantLibs = append(variantLibs, entry+ndkLibrarySuffix)
569 }
570 } else {
571 nonvariantLibs = append(variantLibs, entry)
Dan Willemsen72d39932016-07-08 23:23:48 -0700572 }
573 }
Dan Albert914449f2016-06-17 16:45:24 -0700574 return nonvariantLibs, variantLibs
Dan Willemsen72d39932016-07-08 23:23:48 -0700575 }
576
Dan Albert914449f2016-06-17 16:45:24 -0700577 deps.SharedLibs, variantNdkLibs = rewriteNdkLibs(deps.SharedLibs)
578 deps.LateSharedLibs, variantLateNdkLibs = rewriteNdkLibs(deps.LateSharedLibs)
Dan Willemsen72d39932016-07-08 23:23:48 -0700579 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700580
581 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, wholeStaticDepTag,
582 deps.WholeStaticLibs...)
583
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700584 for _, lib := range deps.StaticLibs {
585 depTag := staticDepTag
586 if inList(lib, deps.ReexportStaticLibHeaders) {
587 depTag = staticExportDepTag
588 }
Colin Cross15a0d462016-07-14 14:49:58 -0700589 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700590 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700591
592 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, lateStaticDepTag,
593 deps.LateStaticLibs...)
594
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700595 for _, lib := range deps.SharedLibs {
596 depTag := sharedDepTag
597 if inList(lib, deps.ReexportSharedLibHeaders) {
598 depTag = sharedExportDepTag
599 }
Colin Cross15a0d462016-07-14 14:49:58 -0700600 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700601 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700602
603 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, lateSharedDepTag,
604 deps.LateSharedLibs...)
605
Colin Cross68861832016-07-08 10:41:41 -0700606 actx.AddDependency(c, genSourceDepTag, deps.GeneratedSources...)
607 actx.AddDependency(c, genHeaderDepTag, deps.GeneratedHeaders...)
Dan Willemsenb40aab62016-04-20 14:21:14 -0700608
Colin Cross68861832016-07-08 10:41:41 -0700609 actx.AddDependency(c, objDepTag, deps.ObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700610
611 if deps.CrtBegin != "" {
Colin Cross68861832016-07-08 10:41:41 -0700612 actx.AddDependency(c, crtBeginDepTag, deps.CrtBegin)
Colin Crossca860ac2016-01-04 14:34:37 -0800613 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700614 if deps.CrtEnd != "" {
Colin Cross68861832016-07-08 10:41:41 -0700615 actx.AddDependency(c, crtEndDepTag, deps.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700616 }
Dan Albert914449f2016-06-17 16:45:24 -0700617
618 version := ctx.sdkVersion()
619 actx.AddVariationDependencies([]blueprint.Variation{
620 {"ndk_api", version}, {"link", "shared"}}, ndkStubDepTag, variantNdkLibs...)
621 actx.AddVariationDependencies([]blueprint.Variation{
622 {"ndk_api", version}, {"link", "shared"}}, ndkLateStubDepTag, variantLateNdkLibs...)
Colin Cross6362e272015-10-29 15:25:03 -0700623}
Colin Cross21b9a242015-03-24 14:15:58 -0700624
Colin Cross635c3b02016-05-18 15:37:25 -0700625func depsMutator(ctx android.BottomUpMutatorContext) {
Dan Willemsen3f32f032016-07-11 14:36:48 -0700626 if c, ok := ctx.Module().(*Module); ok && c.Enabled() {
Colin Cross6362e272015-10-29 15:25:03 -0700627 c.depsMutator(ctx)
628 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800629}
630
Colin Crossca860ac2016-01-04 14:34:37 -0800631func (c *Module) clang(ctx BaseModuleContext) bool {
632 clang := Bool(c.Properties.Clang)
633
634 if c.Properties.Clang == nil {
635 if ctx.Host() {
636 clang = true
637 }
638
639 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
640 clang = true
641 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800642 }
Colin Cross28344522015-04-22 13:07:53 -0700643
Colin Crossca860ac2016-01-04 14:34:37 -0800644 if !c.toolchain(ctx).ClangSupported() {
645 clang = false
646 }
647
648 return clang
649}
650
Colin Crossc99deeb2016-04-11 15:06:20 -0700651// Convert dependencies to paths. Returns a PathDeps containing paths
Colin Cross635c3b02016-05-18 15:37:25 -0700652func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
Colin Crossca860ac2016-01-04 14:34:37 -0800653 var depPaths PathDeps
Colin Crossca860ac2016-01-04 14:34:37 -0800654
Dan Willemsena96ff642016-06-07 12:34:45 -0700655 // Whether a module can link to another module, taking into
656 // account NDK linking.
657 linkTypeOk := func(from, to *Module) bool {
658 if from.Target().Os != android.Android {
659 // Host code is not restricted
660 return true
661 }
662 if from.Properties.Sdk_version == "" {
663 // Platform code can link to anything
664 return true
665 }
666 if _, ok := to.linker.(*toolchainLibraryLinker); ok {
667 // These are always allowed
668 return true
669 }
670 if _, ok := to.linker.(*ndkPrebuiltLibraryLinker); ok {
671 // These are allowed, but don't set sdk_version
672 return true
673 }
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700674 if _, ok := to.linker.(*ndkPrebuiltStlLinker); ok {
675 // These are allowed, but don't set sdk_version
676 return true
677 }
Dan Albert914449f2016-06-17 16:45:24 -0700678 if _, ok := to.linker.(*stubLinker); ok {
679 // These aren't real libraries, but are the stub shared libraries that are included in
680 // the NDK.
681 return true
682 }
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700683 return to.Properties.Sdk_version != ""
Dan Willemsena96ff642016-06-07 12:34:45 -0700684 }
685
Colin Crossc99deeb2016-04-11 15:06:20 -0700686 ctx.VisitDirectDeps(func(m blueprint.Module) {
687 name := ctx.OtherModuleName(m)
688 tag := ctx.OtherModuleDependencyTag(m)
Colin Crossca860ac2016-01-04 14:34:37 -0800689
Colin Cross635c3b02016-05-18 15:37:25 -0700690 a, _ := m.(android.Module)
Colin Crossc99deeb2016-04-11 15:06:20 -0700691 if a == nil {
692 ctx.ModuleErrorf("module %q not an android module", name)
693 return
Colin Crossca860ac2016-01-04 14:34:37 -0800694 }
Colin Crossca860ac2016-01-04 14:34:37 -0800695
Dan Willemsena96ff642016-06-07 12:34:45 -0700696 cc, _ := m.(*Module)
697 if cc == nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700698 switch tag {
Colin Cross635c3b02016-05-18 15:37:25 -0700699 case android.DefaultsDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700700 case genSourceDepTag:
701 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
702 depPaths.GeneratedSources = append(depPaths.GeneratedSources,
703 genRule.GeneratedSourceFiles()...)
704 } else {
705 ctx.ModuleErrorf("module %q is not a gensrcs or genrule", name)
706 }
707 case genHeaderDepTag:
708 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
709 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders,
710 genRule.GeneratedSourceFiles()...)
Dan Willemsen76f08272016-07-09 00:14:08 -0700711 depPaths.Flags = append(depPaths.Flags,
Colin Cross635c3b02016-05-18 15:37:25 -0700712 includeDirsToFlags(android.Paths{genRule.GeneratedHeaderDir()}))
Dan Willemsenb40aab62016-04-20 14:21:14 -0700713 } else {
714 ctx.ModuleErrorf("module %q is not a genrule", name)
715 }
716 default:
Colin Crossc99deeb2016-04-11 15:06:20 -0700717 ctx.ModuleErrorf("depends on non-cc module %q", name)
Colin Crossca860ac2016-01-04 14:34:37 -0800718 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700719 return
720 }
721
722 if !a.Enabled() {
723 ctx.ModuleErrorf("depends on disabled module %q", name)
724 return
725 }
726
Colin Crossa1ad8d12016-06-01 17:09:44 -0700727 if a.Target().Os != ctx.Os() {
728 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), name)
729 return
730 }
731
732 if a.Target().Arch.ArchType != ctx.Arch().ArchType {
733 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700734 return
735 }
736
Dan Willemsena96ff642016-06-07 12:34:45 -0700737 if !cc.outputFile.Valid() {
Colin Crossc99deeb2016-04-11 15:06:20 -0700738 ctx.ModuleErrorf("module %q missing output file", name)
739 return
740 }
741
742 if tag == reuseObjTag {
743 depPaths.ObjFiles = append(depPaths.ObjFiles,
Dan Willemsena96ff642016-06-07 12:34:45 -0700744 cc.compiler.(*libraryCompiler).reuseObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700745 return
746 }
747
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700748 if t, ok := tag.(dependencyTag); ok && t.library {
Dan Willemsena96ff642016-06-07 12:34:45 -0700749 if i, ok := cc.linker.(exportedFlagsProducer); ok {
Dan Willemsen76f08272016-07-09 00:14:08 -0700750 flags := i.exportedFlags()
751 depPaths.Flags = append(depPaths.Flags, flags...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700752
753 if t.reexportFlags {
Dan Willemsen76f08272016-07-09 00:14:08 -0700754 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700755 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700756 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700757
758 if !linkTypeOk(c, cc) {
759 ctx.ModuleErrorf("depends on non-NDK-built library %q", name)
760 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700761 }
762
Colin Cross635c3b02016-05-18 15:37:25 -0700763 var depPtr *android.Paths
Colin Crossc99deeb2016-04-11 15:06:20 -0700764
765 switch tag {
Dan Albert914449f2016-06-17 16:45:24 -0700766 case ndkStubDepTag, sharedDepTag, sharedExportDepTag:
Colin Crossc99deeb2016-04-11 15:06:20 -0700767 depPtr = &depPaths.SharedLibs
Dan Albert914449f2016-06-17 16:45:24 -0700768 case lateSharedDepTag, ndkLateStubDepTag:
Colin Crossc99deeb2016-04-11 15:06:20 -0700769 depPtr = &depPaths.LateSharedLibs
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700770 case staticDepTag, staticExportDepTag:
Colin Crossc99deeb2016-04-11 15:06:20 -0700771 depPtr = &depPaths.StaticLibs
772 case lateStaticDepTag:
773 depPtr = &depPaths.LateStaticLibs
774 case wholeStaticDepTag:
775 depPtr = &depPaths.WholeStaticLibs
Colin Crossc7a38dc2016-07-12 13:13:09 -0700776 staticLib, _ := cc.linker.(libraryInterface)
Colin Crossc99deeb2016-04-11 15:06:20 -0700777 if staticLib == nil || !staticLib.static() {
Dan Willemsena96ff642016-06-07 12:34:45 -0700778 ctx.ModuleErrorf("module %q not a static library", name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700779 return
780 }
781
782 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
783 postfix := " (required by " + ctx.OtherModuleName(m) + ")"
784 for i := range missingDeps {
785 missingDeps[i] += postfix
786 }
787 ctx.AddMissingDependencies(missingDeps)
788 }
789 depPaths.WholeStaticLibObjFiles =
Colin Crossc7a38dc2016-07-12 13:13:09 -0700790 append(depPaths.WholeStaticLibObjFiles, staticLib.objs()...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700791 case objDepTag:
792 depPtr = &depPaths.ObjFiles
793 case crtBeginDepTag:
Dan Willemsena96ff642016-06-07 12:34:45 -0700794 depPaths.CrtBegin = cc.outputFile
Colin Crossc99deeb2016-04-11 15:06:20 -0700795 case crtEndDepTag:
Dan Willemsena96ff642016-06-07 12:34:45 -0700796 depPaths.CrtEnd = cc.outputFile
Colin Crossc99deeb2016-04-11 15:06:20 -0700797 default:
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700798 panic(fmt.Errorf("unknown dependency tag: %s", tag))
Colin Crossc99deeb2016-04-11 15:06:20 -0700799 }
800
801 if depPtr != nil {
Dan Willemsena96ff642016-06-07 12:34:45 -0700802 *depPtr = append(*depPtr, cc.outputFile.Path())
Colin Crossca860ac2016-01-04 14:34:37 -0800803 }
804 })
805
806 return depPaths
807}
808
809func (c *Module) InstallInData() bool {
810 if c.installer == nil {
811 return false
812 }
813 return c.installer.inData()
814}
815
Colin Cross2ba19d92015-05-07 15:44:20 -0700816//
Colin Crosscfad1192015-11-02 16:43:11 -0800817// Defaults
818//
Colin Crossca860ac2016-01-04 14:34:37 -0800819type Defaults struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700820 android.ModuleBase
821 android.DefaultsModule
Colin Crosscfad1192015-11-02 16:43:11 -0800822}
823
Colin Cross635c3b02016-05-18 15:37:25 -0700824func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -0800825}
826
Colin Crossca860ac2016-01-04 14:34:37 -0800827func defaultsFactory() (blueprint.Module, []interface{}) {
828 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -0800829
830 propertyStructs := []interface{}{
Colin Crossca860ac2016-01-04 14:34:37 -0800831 &BaseProperties{},
832 &BaseCompilerProperties{},
833 &BaseLinkerProperties{},
834 &LibraryCompilerProperties{},
Colin Cross919281a2016-04-05 16:42:05 -0700835 &FlagExporterProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -0800836 &LibraryLinkerProperties{},
837 &BinaryLinkerProperties{},
838 &TestLinkerProperties{},
839 &UnusedProperties{},
840 &StlProperties{},
Colin Cross16b23492016-01-06 14:41:07 -0800841 &SanitizeProperties{},
Colin Cross665dce92016-04-28 14:50:03 -0700842 &StripProperties{},
Colin Crosscfad1192015-11-02 16:43:11 -0800843 }
844
Colin Cross635c3b02016-05-18 15:37:25 -0700845 _, propertyStructs = android.InitAndroidArchModule(module, android.HostAndDeviceDefault,
846 android.MultilibDefault, propertyStructs...)
Colin Crosscfad1192015-11-02 16:43:11 -0800847
Colin Cross635c3b02016-05-18 15:37:25 -0700848 return android.InitDefaultsModule(module, module, propertyStructs...)
Colin Crosscfad1192015-11-02 16:43:11 -0800849}
850
851//
Colin Cross3f40fa42015-01-30 17:27:36 -0800852// Device libraries shipped with gcc
853//
854
Colin Crossca860ac2016-01-04 14:34:37 -0800855type toolchainLibraryLinker struct {
856 baseLinker
Colin Cross3f40fa42015-01-30 17:27:36 -0800857}
858
Colin Crossca860ac2016-01-04 14:34:37 -0800859var _ baseLinkerInterface = (*toolchainLibraryLinker)(nil)
860
Colin Cross42742b82016-08-01 13:20:05 -0700861func (*toolchainLibraryLinker) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross3f40fa42015-01-30 17:27:36 -0800862 // toolchain libraries can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -0800863 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -0800864}
865
Colin Crossca860ac2016-01-04 14:34:37 -0800866func (*toolchainLibraryLinker) buildStatic() bool {
867 return true
868}
Colin Cross3f40fa42015-01-30 17:27:36 -0800869
Colin Crossca860ac2016-01-04 14:34:37 -0800870func (*toolchainLibraryLinker) buildShared() bool {
871 return false
872}
873
874func toolchainLibraryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -0700875 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -0800876 module.compiler = &baseCompiler{}
877 module.linker = &toolchainLibraryLinker{}
Dan Willemsenfc9c28c2016-01-12 16:22:40 -0800878 module.Properties.Clang = proptools.BoolPtr(false)
Colin Crossca860ac2016-01-04 14:34:37 -0800879 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -0800880}
881
Colin Crossca860ac2016-01-04 14:34:37 -0800882func (library *toolchainLibraryLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -0700883 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -0800884
885 libName := ctx.ModuleName() + staticLibraryExtension
Colin Cross635c3b02016-05-18 15:37:25 -0700886 outputFile := android.PathForModuleOut(ctx, libName)
Colin Cross3f40fa42015-01-30 17:27:36 -0800887
Dan Willemsenfc9c28c2016-01-12 16:22:40 -0800888 if flags.Clang {
889 ctx.ModuleErrorf("toolchain_library must use GCC, not Clang")
890 }
891
Colin Crossca860ac2016-01-04 14:34:37 -0800892 CopyGccLib(ctx, libName, flagsToBuilderFlags(flags), outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -0800893
894 ctx.CheckbuildFile(outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -0800895
Colin Crossca860ac2016-01-04 14:34:37 -0800896 return outputFile
Dan Albertc403f7c2015-03-18 14:01:18 -0700897}
898
Colin Crossc99deeb2016-04-11 15:06:20 -0700899func (*toolchainLibraryLinker) installable() bool {
900 return false
901}
902
Colin Cross74d1ec02015-04-28 13:30:13 -0700903// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
904// modifies the slice contents in place, and returns a subslice of the original slice
905func lastUniqueElements(list []string) []string {
906 totalSkip := 0
907 for i := len(list) - 1; i >= totalSkip; i-- {
908 skip := 0
909 for j := i - 1; j >= totalSkip; j-- {
910 if list[i] == list[j] {
911 skip++
912 } else {
913 list[j+skip] = list[j]
914 }
915 }
916 totalSkip += skip
917 }
918 return list[totalSkip:]
919}
Colin Cross06a931b2015-10-28 17:23:31 -0700920
921var Bool = proptools.Bool