blob: 5385eaa6d035e159773ca2abfba04e25d63f4d67 [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 {
173 feature
Colin Cross76fada02016-07-27 10:31:13 -0700174 appendCflags([]string)
175 appendAsflags([]string)
Colin Cross635c3b02016-05-18 15:37:25 -0700176 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -0800177}
178
179type linker interface {
180 feature
Colin Cross635c3b02016-05-18 15:37:25 -0700181 link(ctx ModuleContext, flags Flags, deps PathDeps, objFiles android.Paths) android.Path
Colin Cross76fada02016-07-27 10:31:13 -0700182 appendLdflags([]string)
Colin Crossc99deeb2016-04-11 15:06:20 -0700183 installable() bool
Colin Crossca860ac2016-01-04 14:34:37 -0800184}
185
186type installer interface {
187 props() []interface{}
Colin Cross635c3b02016-05-18 15:37:25 -0700188 install(ctx ModuleContext, path android.Path)
Colin Crossca860ac2016-01-04 14:34:37 -0800189 inData() bool
190}
191
Colin Crossc99deeb2016-04-11 15:06:20 -0700192type dependencyTag struct {
193 blueprint.BaseDependencyTag
194 name string
195 library bool
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700196
197 reexportFlags bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700198}
199
200var (
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700201 sharedDepTag = dependencyTag{name: "shared", library: true}
202 sharedExportDepTag = dependencyTag{name: "shared", library: true, reexportFlags: true}
203 lateSharedDepTag = dependencyTag{name: "late shared", library: true}
204 staticDepTag = dependencyTag{name: "static", library: true}
205 staticExportDepTag = dependencyTag{name: "static", library: true, reexportFlags: true}
206 lateStaticDepTag = dependencyTag{name: "late static", library: true}
207 wholeStaticDepTag = dependencyTag{name: "whole static", library: true, reexportFlags: true}
208 genSourceDepTag = dependencyTag{name: "gen source"}
209 genHeaderDepTag = dependencyTag{name: "gen header"}
210 objDepTag = dependencyTag{name: "obj"}
211 crtBeginDepTag = dependencyTag{name: "crtbegin"}
212 crtEndDepTag = dependencyTag{name: "crtend"}
213 reuseObjTag = dependencyTag{name: "reuse objects"}
Dan Albert914449f2016-06-17 16:45:24 -0700214 ndkStubDepTag = dependencyTag{name: "ndk stub", library: true}
215 ndkLateStubDepTag = dependencyTag{name: "ndk late stub", library: true}
Colin Crossc99deeb2016-04-11 15:06:20 -0700216)
217
Colin Crossca860ac2016-01-04 14:34:37 -0800218// Module contains the properties and members used by all C/C++ module types, and implements
219// the blueprint.Module interface. It delegates to compiler, linker, and installer interfaces
220// to construct the output file. Behavior can be customized with a Customizer interface
221type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700222 android.ModuleBase
223 android.DefaultableModule
Colin Crossc472d572015-03-17 15:06:21 -0700224
Colin Crossca860ac2016-01-04 14:34:37 -0800225 Properties BaseProperties
226 unused UnusedProperties
Colin Crossfa138792015-04-24 17:31:52 -0700227
Colin Crossca860ac2016-01-04 14:34:37 -0800228 // initialize before calling Init
Colin Cross635c3b02016-05-18 15:37:25 -0700229 hod android.HostOrDeviceSupported
230 multilib android.Multilib
Colin Crossc472d572015-03-17 15:06:21 -0700231
Colin Crossca860ac2016-01-04 14:34:37 -0800232 // delegates, initialize before calling Init
Colin Cross76fada02016-07-27 10:31:13 -0700233 Customizer Customizer
Colin Crossca860ac2016-01-04 14:34:37 -0800234 features []feature
235 compiler compiler
236 linker linker
237 installer installer
Colin Crossa8e07cc2016-04-04 15:07:06 -0700238 stl *stl
Colin Cross16b23492016-01-06 14:41:07 -0800239 sanitize *sanitize
240
241 androidMkSharedLibDeps []string
Colin Cross74d1ec02015-04-28 13:30:13 -0700242
Colin Cross635c3b02016-05-18 15:37:25 -0700243 outputFile android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800244
Colin Crossb98c8b02016-07-29 13:44:28 -0700245 cachedToolchain config.Toolchain
Colin Crossc472d572015-03-17 15:06:21 -0700246}
247
Colin Crossca860ac2016-01-04 14:34:37 -0800248func (c *Module) Init() (blueprint.Module, []interface{}) {
249 props := []interface{}{&c.Properties, &c.unused}
Colin Cross76fada02016-07-27 10:31:13 -0700250 if c.Customizer != nil {
251 props = append(props, c.Customizer.Properties()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800252 }
253 if c.compiler != nil {
254 props = append(props, c.compiler.props()...)
255 }
256 if c.linker != nil {
257 props = append(props, c.linker.props()...)
258 }
259 if c.installer != nil {
260 props = append(props, c.installer.props()...)
261 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700262 if c.stl != nil {
263 props = append(props, c.stl.props()...)
264 }
Colin Cross16b23492016-01-06 14:41:07 -0800265 if c.sanitize != nil {
266 props = append(props, c.sanitize.props()...)
267 }
Colin Crossca860ac2016-01-04 14:34:37 -0800268 for _, feature := range c.features {
269 props = append(props, feature.props()...)
270 }
Colin Crossc472d572015-03-17 15:06:21 -0700271
Colin Cross635c3b02016-05-18 15:37:25 -0700272 _, props = android.InitAndroidArchModule(c, c.hod, c.multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700273
Colin Cross635c3b02016-05-18 15:37:25 -0700274 return android.InitDefaultableModule(c, c, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700275}
276
Colin Crossca860ac2016-01-04 14:34:37 -0800277type baseModuleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700278 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800279 moduleContextImpl
280}
281
282type moduleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700283 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800284 moduleContextImpl
285}
286
287type moduleContextImpl struct {
288 mod *Module
289 ctx BaseModuleContext
290}
291
Colin Cross76fada02016-07-27 10:31:13 -0700292func (ctx *moduleContextImpl) AppendCflags(flags ...string) {
293 CheckBadCompilerFlags(ctx.ctx, "", flags)
294 ctx.mod.compiler.appendCflags(flags)
295}
296
297func (ctx *moduleContextImpl) AppendAsflags(flags ...string) {
298 CheckBadCompilerFlags(ctx.ctx, "", flags)
299 ctx.mod.compiler.appendAsflags(flags)
300}
301
302func (ctx *moduleContextImpl) AppendLdflags(flags ...string) {
303 CheckBadLinkerFlags(ctx.ctx, "", flags)
304 ctx.mod.linker.appendLdflags(flags)
305}
306
Colin Crossca860ac2016-01-04 14:34:37 -0800307func (ctx *moduleContextImpl) clang() bool {
308 return ctx.mod.clang(ctx.ctx)
309}
310
Colin Crossb98c8b02016-07-29 13:44:28 -0700311func (ctx *moduleContextImpl) toolchain() config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800312 return ctx.mod.toolchain(ctx.ctx)
313}
314
315func (ctx *moduleContextImpl) static() bool {
316 if ctx.mod.linker == nil {
317 panic(fmt.Errorf("static called on module %q with no linker", ctx.ctx.ModuleName()))
318 }
319 if linker, ok := ctx.mod.linker.(baseLinkerInterface); ok {
320 return linker.static()
321 } else {
322 panic(fmt.Errorf("static called on module %q that doesn't use base linker", ctx.ctx.ModuleName()))
323 }
324}
325
326func (ctx *moduleContextImpl) staticBinary() bool {
327 if ctx.mod.linker == nil {
328 panic(fmt.Errorf("staticBinary called on module %q with no linker", ctx.ctx.ModuleName()))
329 }
330 if linker, ok := ctx.mod.linker.(baseLinkerInterface); ok {
331 return linker.staticBinary()
332 } else {
333 panic(fmt.Errorf("staticBinary called on module %q that doesn't use base linker", ctx.ctx.ModuleName()))
334 }
335}
336
337func (ctx *moduleContextImpl) noDefaultCompilerFlags() bool {
338 return Bool(ctx.mod.Properties.No_default_compiler_flags)
339}
340
341func (ctx *moduleContextImpl) sdk() bool {
Dan Willemsena96ff642016-06-07 12:34:45 -0700342 if ctx.ctx.Device() {
343 return ctx.mod.Properties.Sdk_version != ""
344 }
345 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800346}
347
348func (ctx *moduleContextImpl) sdkVersion() string {
Dan Willemsena96ff642016-06-07 12:34:45 -0700349 if ctx.ctx.Device() {
350 return ctx.mod.Properties.Sdk_version
351 }
352 return ""
Colin Crossca860ac2016-01-04 14:34:37 -0800353}
354
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700355func (ctx *moduleContextImpl) selectedStl() string {
356 if stl := ctx.mod.stl; stl != nil {
357 return stl.Properties.SelectedStl
358 }
359 return ""
360}
361
Colin Cross635c3b02016-05-18 15:37:25 -0700362func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800363 return &Module{
364 hod: hod,
365 multilib: multilib,
366 }
367}
368
Colin Cross635c3b02016-05-18 15:37:25 -0700369func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800370 module := newBaseModule(hod, multilib)
Colin Crossa8e07cc2016-04-04 15:07:06 -0700371 module.stl = &stl{}
Colin Cross16b23492016-01-06 14:41:07 -0800372 module.sanitize = &sanitize{}
Colin Crossca860ac2016-01-04 14:34:37 -0800373 return module
374}
375
Colin Cross635c3b02016-05-18 15:37:25 -0700376func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800377 ctx := &moduleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700378 ModuleContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800379 moduleContextImpl: moduleContextImpl{
380 mod: c,
381 },
382 }
383 ctx.ctx = ctx
384
Colin Cross76fada02016-07-27 10:31:13 -0700385 if c.Customizer != nil {
386 c.Customizer.Flags(ctx)
387 }
388
Colin Crossca860ac2016-01-04 14:34:37 -0800389 flags := Flags{
390 Toolchain: c.toolchain(ctx),
391 Clang: c.clang(ctx),
392 }
Colin Crossca860ac2016-01-04 14:34:37 -0800393 if c.compiler != nil {
394 flags = c.compiler.flags(ctx, flags)
395 }
396 if c.linker != nil {
397 flags = c.linker.flags(ctx, flags)
398 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700399 if c.stl != nil {
400 flags = c.stl.flags(ctx, flags)
401 }
Colin Cross16b23492016-01-06 14:41:07 -0800402 if c.sanitize != nil {
403 flags = c.sanitize.flags(ctx, flags)
404 }
Colin Crossca860ac2016-01-04 14:34:37 -0800405 for _, feature := range c.features {
406 flags = feature.flags(ctx, flags)
407 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800408 if ctx.Failed() {
409 return
410 }
411
Colin Crossb98c8b02016-07-29 13:44:28 -0700412 flags.CFlags, _ = filterList(flags.CFlags, config.IllegalFlags)
413 flags.CppFlags, _ = filterList(flags.CppFlags, config.IllegalFlags)
414 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, config.IllegalFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800415
Colin Crossca860ac2016-01-04 14:34:37 -0800416 // Optimization to reduce size of build.ninja
417 // Replace the long list of flags for each file with a module-local variable
418 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
419 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
420 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
421 flags.CFlags = []string{"$cflags"}
422 flags.CppFlags = []string{"$cppflags"}
423 flags.AsFlags = []string{"$asflags"}
424
Colin Crossc99deeb2016-04-11 15:06:20 -0700425 deps := c.depsToPaths(ctx)
Colin Cross3f40fa42015-01-30 17:27:36 -0800426 if ctx.Failed() {
427 return
428 }
429
Dan Willemsen76f08272016-07-09 00:14:08 -0700430 flags.GlobalFlags = append(flags.GlobalFlags, deps.Flags...)
Colin Crossed9f8682015-03-18 17:17:35 -0700431
Colin Cross635c3b02016-05-18 15:37:25 -0700432 var objFiles android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -0800433 if c.compiler != nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700434 objFiles = c.compiler.compile(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -0800435 if ctx.Failed() {
436 return
437 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800438 }
439
Colin Crossca860ac2016-01-04 14:34:37 -0800440 if c.linker != nil {
441 outputFile := c.linker.link(ctx, flags, deps, objFiles)
442 if ctx.Failed() {
443 return
444 }
Colin Cross635c3b02016-05-18 15:37:25 -0700445 c.outputFile = android.OptionalPathForPath(outputFile)
Colin Cross5049f022015-03-18 13:28:46 -0700446
Colin Crossc99deeb2016-04-11 15:06:20 -0700447 if c.installer != nil && c.linker.installable() {
Colin Crossca860ac2016-01-04 14:34:37 -0800448 c.installer.install(ctx, outputFile)
449 if ctx.Failed() {
450 return
451 }
452 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700453 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800454}
455
Colin Crossb98c8b02016-07-29 13:44:28 -0700456func (c *Module) toolchain(ctx BaseModuleContext) config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800457 if c.cachedToolchain == nil {
Colin Crossb98c8b02016-07-29 13:44:28 -0700458 c.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
Colin Cross3f40fa42015-01-30 17:27:36 -0800459 }
Colin Crossca860ac2016-01-04 14:34:37 -0800460 return c.cachedToolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800461}
462
Colin Crossca860ac2016-01-04 14:34:37 -0800463func (c *Module) begin(ctx BaseModuleContext) {
464 if c.compiler != nil {
465 c.compiler.begin(ctx)
Colin Cross21b9a242015-03-24 14:15:58 -0700466 }
Colin Crossca860ac2016-01-04 14:34:37 -0800467 if c.linker != nil {
468 c.linker.begin(ctx)
469 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700470 if c.stl != nil {
471 c.stl.begin(ctx)
472 }
Colin Cross16b23492016-01-06 14:41:07 -0800473 if c.sanitize != nil {
474 c.sanitize.begin(ctx)
475 }
Colin Crossca860ac2016-01-04 14:34:37 -0800476 for _, feature := range c.features {
477 feature.begin(ctx)
478 }
479}
480
Colin Crossc99deeb2016-04-11 15:06:20 -0700481func (c *Module) deps(ctx BaseModuleContext) Deps {
482 deps := Deps{}
483
484 if c.compiler != nil {
485 deps = c.compiler.deps(ctx, deps)
486 }
487 if c.linker != nil {
488 deps = c.linker.deps(ctx, deps)
489 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700490 if c.stl != nil {
491 deps = c.stl.deps(ctx, deps)
492 }
Colin Cross16b23492016-01-06 14:41:07 -0800493 if c.sanitize != nil {
494 deps = c.sanitize.deps(ctx, deps)
495 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700496 for _, feature := range c.features {
497 deps = feature.deps(ctx, deps)
498 }
499
500 deps.WholeStaticLibs = lastUniqueElements(deps.WholeStaticLibs)
501 deps.StaticLibs = lastUniqueElements(deps.StaticLibs)
502 deps.LateStaticLibs = lastUniqueElements(deps.LateStaticLibs)
503 deps.SharedLibs = lastUniqueElements(deps.SharedLibs)
504 deps.LateSharedLibs = lastUniqueElements(deps.LateSharedLibs)
505
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700506 for _, lib := range deps.ReexportSharedLibHeaders {
507 if !inList(lib, deps.SharedLibs) {
508 ctx.PropertyErrorf("export_shared_lib_headers", "Shared library not in shared_libs: '%s'", lib)
509 }
510 }
511
512 for _, lib := range deps.ReexportStaticLibHeaders {
513 if !inList(lib, deps.StaticLibs) {
514 ctx.PropertyErrorf("export_static_lib_headers", "Static library not in static_libs: '%s'", lib)
515 }
516 }
517
Colin Crossc99deeb2016-04-11 15:06:20 -0700518 return deps
519}
520
Colin Cross635c3b02016-05-18 15:37:25 -0700521func (c *Module) depsMutator(actx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800522 ctx := &baseModuleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700523 BaseContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800524 moduleContextImpl: moduleContextImpl{
525 mod: c,
526 },
527 }
528 ctx.ctx = ctx
529
Colin Crossca860ac2016-01-04 14:34:37 -0800530 c.begin(ctx)
531
Colin Crossc99deeb2016-04-11 15:06:20 -0700532 deps := c.deps(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800533
Colin Crossb5bc4b42016-07-11 16:11:59 -0700534 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.SharedLibs...)
535 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.LateSharedLibs...)
Dan Willemsen72d39932016-07-08 23:23:48 -0700536
Dan Albert914449f2016-06-17 16:45:24 -0700537 variantNdkLibs := []string{}
538 variantLateNdkLibs := []string{}
Dan Willemsen72d39932016-07-08 23:23:48 -0700539 if ctx.sdk() {
Dan Albert914449f2016-06-17 16:45:24 -0700540 version := ctx.sdkVersion()
Dan Willemsen72d39932016-07-08 23:23:48 -0700541
Dan Albert914449f2016-06-17 16:45:24 -0700542 // Rewrites the names of shared libraries into the names of the NDK
543 // libraries where appropriate. This returns two slices.
544 //
545 // The first is a list of non-variant shared libraries (either rewritten
546 // NDK libraries to the modules in prebuilts/ndk, or not rewritten
547 // because they are not NDK libraries).
548 //
549 // The second is a list of ndk_library modules. These need to be
550 // separated because they are a variation dependency and must be added
551 // in a different manner.
552 rewriteNdkLibs := func(list []string) ([]string, []string) {
553 variantLibs := []string{}
554 nonvariantLibs := []string{}
555 for _, entry := range list {
Dan Willemsen72d39932016-07-08 23:23:48 -0700556 if inList(entry, ndkPrebuiltSharedLibraries) {
Dan Albert914449f2016-06-17 16:45:24 -0700557 if !inList(entry, ndkMigratedLibs) {
558 nonvariantLibs = append(nonvariantLibs, entry+".ndk."+version)
559 } else {
560 variantLibs = append(variantLibs, entry+ndkLibrarySuffix)
561 }
562 } else {
563 nonvariantLibs = append(variantLibs, entry)
Dan Willemsen72d39932016-07-08 23:23:48 -0700564 }
565 }
Dan Albert914449f2016-06-17 16:45:24 -0700566 return nonvariantLibs, variantLibs
Dan Willemsen72d39932016-07-08 23:23:48 -0700567 }
568
Dan Albert914449f2016-06-17 16:45:24 -0700569 deps.SharedLibs, variantNdkLibs = rewriteNdkLibs(deps.SharedLibs)
570 deps.LateSharedLibs, variantLateNdkLibs = rewriteNdkLibs(deps.LateSharedLibs)
Dan Willemsen72d39932016-07-08 23:23:48 -0700571 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700572
573 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, wholeStaticDepTag,
574 deps.WholeStaticLibs...)
575
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700576 for _, lib := range deps.StaticLibs {
577 depTag := staticDepTag
578 if inList(lib, deps.ReexportStaticLibHeaders) {
579 depTag = staticExportDepTag
580 }
Colin Cross15a0d462016-07-14 14:49:58 -0700581 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700582 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700583
584 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, lateStaticDepTag,
585 deps.LateStaticLibs...)
586
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700587 for _, lib := range deps.SharedLibs {
588 depTag := sharedDepTag
589 if inList(lib, deps.ReexportSharedLibHeaders) {
590 depTag = sharedExportDepTag
591 }
Colin Cross15a0d462016-07-14 14:49:58 -0700592 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700593 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700594
595 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, lateSharedDepTag,
596 deps.LateSharedLibs...)
597
Colin Cross68861832016-07-08 10:41:41 -0700598 actx.AddDependency(c, genSourceDepTag, deps.GeneratedSources...)
599 actx.AddDependency(c, genHeaderDepTag, deps.GeneratedHeaders...)
Dan Willemsenb40aab62016-04-20 14:21:14 -0700600
Colin Cross68861832016-07-08 10:41:41 -0700601 actx.AddDependency(c, objDepTag, deps.ObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700602
603 if deps.CrtBegin != "" {
Colin Cross68861832016-07-08 10:41:41 -0700604 actx.AddDependency(c, crtBeginDepTag, deps.CrtBegin)
Colin Crossca860ac2016-01-04 14:34:37 -0800605 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700606 if deps.CrtEnd != "" {
Colin Cross68861832016-07-08 10:41:41 -0700607 actx.AddDependency(c, crtEndDepTag, deps.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700608 }
Dan Albert914449f2016-06-17 16:45:24 -0700609
610 version := ctx.sdkVersion()
611 actx.AddVariationDependencies([]blueprint.Variation{
612 {"ndk_api", version}, {"link", "shared"}}, ndkStubDepTag, variantNdkLibs...)
613 actx.AddVariationDependencies([]blueprint.Variation{
614 {"ndk_api", version}, {"link", "shared"}}, ndkLateStubDepTag, variantLateNdkLibs...)
Colin Cross6362e272015-10-29 15:25:03 -0700615}
Colin Cross21b9a242015-03-24 14:15:58 -0700616
Colin Cross635c3b02016-05-18 15:37:25 -0700617func depsMutator(ctx android.BottomUpMutatorContext) {
Dan Willemsen3f32f032016-07-11 14:36:48 -0700618 if c, ok := ctx.Module().(*Module); ok && c.Enabled() {
Colin Cross6362e272015-10-29 15:25:03 -0700619 c.depsMutator(ctx)
620 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800621}
622
Colin Crossca860ac2016-01-04 14:34:37 -0800623func (c *Module) clang(ctx BaseModuleContext) bool {
624 clang := Bool(c.Properties.Clang)
625
626 if c.Properties.Clang == nil {
627 if ctx.Host() {
628 clang = true
629 }
630
631 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
632 clang = true
633 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800634 }
Colin Cross28344522015-04-22 13:07:53 -0700635
Colin Crossca860ac2016-01-04 14:34:37 -0800636 if !c.toolchain(ctx).ClangSupported() {
637 clang = false
638 }
639
640 return clang
641}
642
Colin Crossc99deeb2016-04-11 15:06:20 -0700643// Convert dependencies to paths. Returns a PathDeps containing paths
Colin Cross635c3b02016-05-18 15:37:25 -0700644func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
Colin Crossca860ac2016-01-04 14:34:37 -0800645 var depPaths PathDeps
Colin Crossca860ac2016-01-04 14:34:37 -0800646
Dan Willemsena96ff642016-06-07 12:34:45 -0700647 // Whether a module can link to another module, taking into
648 // account NDK linking.
649 linkTypeOk := func(from, to *Module) bool {
650 if from.Target().Os != android.Android {
651 // Host code is not restricted
652 return true
653 }
654 if from.Properties.Sdk_version == "" {
655 // Platform code can link to anything
656 return true
657 }
658 if _, ok := to.linker.(*toolchainLibraryLinker); ok {
659 // These are always allowed
660 return true
661 }
662 if _, ok := to.linker.(*ndkPrebuiltLibraryLinker); ok {
663 // These are allowed, but don't set sdk_version
664 return true
665 }
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700666 if _, ok := to.linker.(*ndkPrebuiltStlLinker); ok {
667 // These are allowed, but don't set sdk_version
668 return true
669 }
Dan Albert914449f2016-06-17 16:45:24 -0700670 if _, ok := to.linker.(*stubLinker); ok {
671 // These aren't real libraries, but are the stub shared libraries that are included in
672 // the NDK.
673 return true
674 }
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700675 return to.Properties.Sdk_version != ""
Dan Willemsena96ff642016-06-07 12:34:45 -0700676 }
677
Colin Crossc99deeb2016-04-11 15:06:20 -0700678 ctx.VisitDirectDeps(func(m blueprint.Module) {
679 name := ctx.OtherModuleName(m)
680 tag := ctx.OtherModuleDependencyTag(m)
Colin Crossca860ac2016-01-04 14:34:37 -0800681
Colin Cross635c3b02016-05-18 15:37:25 -0700682 a, _ := m.(android.Module)
Colin Crossc99deeb2016-04-11 15:06:20 -0700683 if a == nil {
684 ctx.ModuleErrorf("module %q not an android module", name)
685 return
Colin Crossca860ac2016-01-04 14:34:37 -0800686 }
Colin Crossca860ac2016-01-04 14:34:37 -0800687
Dan Willemsena96ff642016-06-07 12:34:45 -0700688 cc, _ := m.(*Module)
689 if cc == nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700690 switch tag {
Colin Cross635c3b02016-05-18 15:37:25 -0700691 case android.DefaultsDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700692 case genSourceDepTag:
693 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
694 depPaths.GeneratedSources = append(depPaths.GeneratedSources,
695 genRule.GeneratedSourceFiles()...)
696 } else {
697 ctx.ModuleErrorf("module %q is not a gensrcs or genrule", name)
698 }
699 case genHeaderDepTag:
700 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
701 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders,
702 genRule.GeneratedSourceFiles()...)
Dan Willemsen76f08272016-07-09 00:14:08 -0700703 depPaths.Flags = append(depPaths.Flags,
Colin Cross635c3b02016-05-18 15:37:25 -0700704 includeDirsToFlags(android.Paths{genRule.GeneratedHeaderDir()}))
Dan Willemsenb40aab62016-04-20 14:21:14 -0700705 } else {
706 ctx.ModuleErrorf("module %q is not a genrule", name)
707 }
708 default:
Colin Crossc99deeb2016-04-11 15:06:20 -0700709 ctx.ModuleErrorf("depends on non-cc module %q", name)
Colin Crossca860ac2016-01-04 14:34:37 -0800710 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700711 return
712 }
713
714 if !a.Enabled() {
715 ctx.ModuleErrorf("depends on disabled module %q", name)
716 return
717 }
718
Colin Crossa1ad8d12016-06-01 17:09:44 -0700719 if a.Target().Os != ctx.Os() {
720 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), name)
721 return
722 }
723
724 if a.Target().Arch.ArchType != ctx.Arch().ArchType {
725 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700726 return
727 }
728
Dan Willemsena96ff642016-06-07 12:34:45 -0700729 if !cc.outputFile.Valid() {
Colin Crossc99deeb2016-04-11 15:06:20 -0700730 ctx.ModuleErrorf("module %q missing output file", name)
731 return
732 }
733
734 if tag == reuseObjTag {
735 depPaths.ObjFiles = append(depPaths.ObjFiles,
Dan Willemsena96ff642016-06-07 12:34:45 -0700736 cc.compiler.(*libraryCompiler).reuseObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700737 return
738 }
739
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700740 if t, ok := tag.(dependencyTag); ok && t.library {
Dan Willemsena96ff642016-06-07 12:34:45 -0700741 if i, ok := cc.linker.(exportedFlagsProducer); ok {
Dan Willemsen76f08272016-07-09 00:14:08 -0700742 flags := i.exportedFlags()
743 depPaths.Flags = append(depPaths.Flags, flags...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700744
745 if t.reexportFlags {
Dan Willemsen76f08272016-07-09 00:14:08 -0700746 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700747 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700748 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700749
750 if !linkTypeOk(c, cc) {
751 ctx.ModuleErrorf("depends on non-NDK-built library %q", name)
752 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700753 }
754
Colin Cross635c3b02016-05-18 15:37:25 -0700755 var depPtr *android.Paths
Colin Crossc99deeb2016-04-11 15:06:20 -0700756
757 switch tag {
Dan Albert914449f2016-06-17 16:45:24 -0700758 case ndkStubDepTag, sharedDepTag, sharedExportDepTag:
Colin Crossc99deeb2016-04-11 15:06:20 -0700759 depPtr = &depPaths.SharedLibs
Dan Albert914449f2016-06-17 16:45:24 -0700760 case lateSharedDepTag, ndkLateStubDepTag:
Colin Crossc99deeb2016-04-11 15:06:20 -0700761 depPtr = &depPaths.LateSharedLibs
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700762 case staticDepTag, staticExportDepTag:
Colin Crossc99deeb2016-04-11 15:06:20 -0700763 depPtr = &depPaths.StaticLibs
764 case lateStaticDepTag:
765 depPtr = &depPaths.LateStaticLibs
766 case wholeStaticDepTag:
767 depPtr = &depPaths.WholeStaticLibs
Colin Crossc7a38dc2016-07-12 13:13:09 -0700768 staticLib, _ := cc.linker.(libraryInterface)
Colin Crossc99deeb2016-04-11 15:06:20 -0700769 if staticLib == nil || !staticLib.static() {
Dan Willemsena96ff642016-06-07 12:34:45 -0700770 ctx.ModuleErrorf("module %q not a static library", name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700771 return
772 }
773
774 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
775 postfix := " (required by " + ctx.OtherModuleName(m) + ")"
776 for i := range missingDeps {
777 missingDeps[i] += postfix
778 }
779 ctx.AddMissingDependencies(missingDeps)
780 }
781 depPaths.WholeStaticLibObjFiles =
Colin Crossc7a38dc2016-07-12 13:13:09 -0700782 append(depPaths.WholeStaticLibObjFiles, staticLib.objs()...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700783 case objDepTag:
784 depPtr = &depPaths.ObjFiles
785 case crtBeginDepTag:
Dan Willemsena96ff642016-06-07 12:34:45 -0700786 depPaths.CrtBegin = cc.outputFile
Colin Crossc99deeb2016-04-11 15:06:20 -0700787 case crtEndDepTag:
Dan Willemsena96ff642016-06-07 12:34:45 -0700788 depPaths.CrtEnd = cc.outputFile
Colin Crossc99deeb2016-04-11 15:06:20 -0700789 default:
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700790 panic(fmt.Errorf("unknown dependency tag: %s", tag))
Colin Crossc99deeb2016-04-11 15:06:20 -0700791 }
792
793 if depPtr != nil {
Dan Willemsena96ff642016-06-07 12:34:45 -0700794 *depPtr = append(*depPtr, cc.outputFile.Path())
Colin Crossca860ac2016-01-04 14:34:37 -0800795 }
796 })
797
798 return depPaths
799}
800
801func (c *Module) InstallInData() bool {
802 if c.installer == nil {
803 return false
804 }
805 return c.installer.inData()
806}
807
Colin Cross2ba19d92015-05-07 15:44:20 -0700808//
Colin Crosscfad1192015-11-02 16:43:11 -0800809// Defaults
810//
Colin Crossca860ac2016-01-04 14:34:37 -0800811type Defaults struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700812 android.ModuleBase
813 android.DefaultsModule
Colin Crosscfad1192015-11-02 16:43:11 -0800814}
815
Colin Cross635c3b02016-05-18 15:37:25 -0700816func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -0800817}
818
Colin Crossca860ac2016-01-04 14:34:37 -0800819func defaultsFactory() (blueprint.Module, []interface{}) {
820 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -0800821
822 propertyStructs := []interface{}{
Colin Crossca860ac2016-01-04 14:34:37 -0800823 &BaseProperties{},
824 &BaseCompilerProperties{},
825 &BaseLinkerProperties{},
826 &LibraryCompilerProperties{},
Colin Cross919281a2016-04-05 16:42:05 -0700827 &FlagExporterProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -0800828 &LibraryLinkerProperties{},
829 &BinaryLinkerProperties{},
830 &TestLinkerProperties{},
831 &UnusedProperties{},
832 &StlProperties{},
Colin Cross16b23492016-01-06 14:41:07 -0800833 &SanitizeProperties{},
Colin Cross665dce92016-04-28 14:50:03 -0700834 &StripProperties{},
Colin Crosscfad1192015-11-02 16:43:11 -0800835 }
836
Colin Cross635c3b02016-05-18 15:37:25 -0700837 _, propertyStructs = android.InitAndroidArchModule(module, android.HostAndDeviceDefault,
838 android.MultilibDefault, propertyStructs...)
Colin Crosscfad1192015-11-02 16:43:11 -0800839
Colin Cross635c3b02016-05-18 15:37:25 -0700840 return android.InitDefaultsModule(module, module, propertyStructs...)
Colin Crosscfad1192015-11-02 16:43:11 -0800841}
842
843//
Colin Cross3f40fa42015-01-30 17:27:36 -0800844// Device libraries shipped with gcc
845//
846
Colin Crossca860ac2016-01-04 14:34:37 -0800847type toolchainLibraryLinker struct {
848 baseLinker
Colin Cross3f40fa42015-01-30 17:27:36 -0800849}
850
Colin Crossca860ac2016-01-04 14:34:37 -0800851var _ baseLinkerInterface = (*toolchainLibraryLinker)(nil)
852
853func (*toolchainLibraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross3f40fa42015-01-30 17:27:36 -0800854 // toolchain libraries can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -0800855 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -0800856}
857
Colin Crossca860ac2016-01-04 14:34:37 -0800858func (*toolchainLibraryLinker) buildStatic() bool {
859 return true
860}
Colin Cross3f40fa42015-01-30 17:27:36 -0800861
Colin Crossca860ac2016-01-04 14:34:37 -0800862func (*toolchainLibraryLinker) buildShared() bool {
863 return false
864}
865
866func toolchainLibraryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -0700867 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -0800868 module.compiler = &baseCompiler{}
869 module.linker = &toolchainLibraryLinker{}
Dan Willemsenfc9c28c2016-01-12 16:22:40 -0800870 module.Properties.Clang = proptools.BoolPtr(false)
Colin Crossca860ac2016-01-04 14:34:37 -0800871 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -0800872}
873
Colin Crossca860ac2016-01-04 14:34:37 -0800874func (library *toolchainLibraryLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -0700875 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -0800876
877 libName := ctx.ModuleName() + staticLibraryExtension
Colin Cross635c3b02016-05-18 15:37:25 -0700878 outputFile := android.PathForModuleOut(ctx, libName)
Colin Cross3f40fa42015-01-30 17:27:36 -0800879
Dan Willemsenfc9c28c2016-01-12 16:22:40 -0800880 if flags.Clang {
881 ctx.ModuleErrorf("toolchain_library must use GCC, not Clang")
882 }
883
Colin Crossca860ac2016-01-04 14:34:37 -0800884 CopyGccLib(ctx, libName, flagsToBuilderFlags(flags), outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -0800885
886 ctx.CheckbuildFile(outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -0800887
Colin Crossca860ac2016-01-04 14:34:37 -0800888 return outputFile
Dan Albertc403f7c2015-03-18 14:01:18 -0700889}
890
Colin Crossc99deeb2016-04-11 15:06:20 -0700891func (*toolchainLibraryLinker) installable() bool {
892 return false
893}
894
Colin Cross74d1ec02015-04-28 13:30:13 -0700895// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
896// modifies the slice contents in place, and returns a subslice of the original slice
897func lastUniqueElements(list []string) []string {
898 totalSkip := 0
899 for i := len(list) - 1; i >= totalSkip; i-- {
900 skip := 0
901 for j := i - 1; j >= totalSkip; j-- {
902 if list[i] == list[j] {
903 skip++
904 } else {
905 list[j+skip] = list[j]
906 }
907 }
908 totalSkip += skip
909 }
910 return list[totalSkip:]
911}
Colin Cross06a931b2015-10-28 17:23:31 -0700912
913var Bool = proptools.Bool