blob: 53345c53d8f8c5dd1eda8b24dae8994b08b50154 [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 Cross5049f022015-03-18 13:28:46 -070030 "android/soong/genrule"
Colin Cross3f40fa42015-01-30 17:27:36 -080031)
32
Colin Cross463a90e2015-06-17 14:20:06 -070033func init() {
Colin Crossca860ac2016-01-04 14:34:37 -080034 soong.RegisterModuleType("cc_defaults", defaultsFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070035
Colin Crossca860ac2016-01-04 14:34:37 -080036 soong.RegisterModuleType("toolchain_library", toolchainLibraryFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070037
38 // LinkageMutator must be registered after common.ArchMutator, but that is guaranteed by
39 // the Go initialization order because this package depends on common, so common's init
40 // functions will run first.
Colin Cross635c3b02016-05-18 15:37:25 -070041 android.RegisterBottomUpMutator("link", linkageMutator)
Dan Albert914449f2016-06-17 16:45:24 -070042 android.RegisterBottomUpMutator("ndk_api", ndkApiMutator)
Colin Cross635c3b02016-05-18 15:37:25 -070043 android.RegisterBottomUpMutator("test_per_src", testPerSrcMutator)
44 android.RegisterBottomUpMutator("deps", depsMutator)
Colin Cross16b23492016-01-06 14:41:07 -080045
Colin Cross635c3b02016-05-18 15:37:25 -070046 android.RegisterTopDownMutator("asan_deps", sanitizerDepsMutator(asan))
47 android.RegisterBottomUpMutator("asan", sanitizerMutator(asan))
Colin Cross16b23492016-01-06 14:41:07 -080048
Colin Cross635c3b02016-05-18 15:37:25 -070049 android.RegisterTopDownMutator("tsan_deps", sanitizerDepsMutator(tsan))
50 android.RegisterBottomUpMutator("tsan", sanitizerMutator(tsan))
Colin Cross463a90e2015-06-17 14:20:06 -070051}
52
Colin Crossca860ac2016-01-04 14:34:37 -080053type Deps struct {
54 SharedLibs, LateSharedLibs []string
55 StaticLibs, LateStaticLibs, WholeStaticLibs []string
Colin Crossc472d572015-03-17 15:06:21 -070056
Dan Willemsen490a8dc2016-06-06 18:22:19 -070057 ReexportSharedLibHeaders, ReexportStaticLibHeaders []string
58
Colin Cross81413472016-04-11 14:37:39 -070059 ObjFiles []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -070060
Dan Willemsenb40aab62016-04-20 14:21:14 -070061 GeneratedSources []string
62 GeneratedHeaders []string
63
Colin Cross97ba0732015-03-23 17:50:24 -070064 CrtBegin, CrtEnd string
Colin Crossc472d572015-03-17 15:06:21 -070065}
66
Colin Crossca860ac2016-01-04 14:34:37 -080067type PathDeps struct {
Colin Cross635c3b02016-05-18 15:37:25 -070068 SharedLibs, LateSharedLibs android.Paths
69 StaticLibs, LateStaticLibs, WholeStaticLibs android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -070070
Colin Cross635c3b02016-05-18 15:37:25 -070071 ObjFiles android.Paths
72 WholeStaticLibObjFiles android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -070073
Colin Cross635c3b02016-05-18 15:37:25 -070074 GeneratedSources android.Paths
75 GeneratedHeaders android.Paths
Dan Willemsenb40aab62016-04-20 14:21:14 -070076
Dan Willemsen76f08272016-07-09 00:14:08 -070077 Flags, ReexportedFlags []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -070078
Colin Cross635c3b02016-05-18 15:37:25 -070079 CrtBegin, CrtEnd android.OptionalPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -070080}
81
Colin Crossca860ac2016-01-04 14:34:37 -080082type Flags struct {
Colin Cross28344522015-04-22 13:07:53 -070083 GlobalFlags []string // Flags that apply to C, C++, and assembly source files
84 AsFlags []string // Flags that apply to assembly source files
85 CFlags []string // Flags that apply to C and C++ source files
86 ConlyFlags []string // Flags that apply to C source files
87 CppFlags []string // Flags that apply to C++ source files
88 YaccFlags []string // Flags that apply to Yacc source files
89 LdFlags []string // Flags that apply to linker command lines
Colin Cross16b23492016-01-06 14:41:07 -080090 libFlags []string // Flags to add libraries early to the link order
Colin Cross28344522015-04-22 13:07:53 -070091
92 Nocrt bool
93 Toolchain Toolchain
94 Clang bool
Colin Crossca860ac2016-01-04 14:34:37 -080095
96 RequiredInstructionSet string
Colin Cross16b23492016-01-06 14:41:07 -080097 DynamicLinker string
98
Colin Cross635c3b02016-05-18 15:37:25 -070099 CFlagsDeps android.Paths // Files depended on by compiler flags
Colin Crossc472d572015-03-17 15:06:21 -0700100}
101
Colin Cross81413472016-04-11 14:37:39 -0700102type ObjectLinkerProperties struct {
103 // names of other cc_object modules to link into this module using partial linking
104 Objs []string `android:"arch_variant"`
105}
106
Colin Crossca860ac2016-01-04 14:34:37 -0800107// Properties used to compile all C or C++ modules
108type BaseProperties struct {
109 // compile module with clang instead of gcc
110 Clang *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700111
112 // Minimum sdk version supported when compiling against the ndk
113 Sdk_version string
114
Colin Crossca860ac2016-01-04 14:34:37 -0800115 // don't insert default compiler flags into asflags, cflags,
116 // cppflags, conlyflags, ldflags, or include_dirs
117 No_default_compiler_flags *bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700118
119 AndroidMkSharedLibs []string `blueprint:"mutated"`
Colin Crossbc6fb162016-05-24 15:39:04 -0700120 HideFromMake bool `blueprint:"mutated"`
Colin Crossca860ac2016-01-04 14:34:37 -0800121}
122
Colin Crossca860ac2016-01-04 14:34:37 -0800123type UnusedProperties struct {
Colin Cross21b481b2016-04-15 16:27:17 -0700124 Native_coverage *bool
125 Required []string
Colin Cross21b481b2016-04-15 16:27:17 -0700126 Tags []string
Colin Crosscfad1192015-11-02 16:43:11 -0800127}
128
Colin Crossca860ac2016-01-04 14:34:37 -0800129type ModuleContextIntf interface {
Colin Crossca860ac2016-01-04 14:34:37 -0800130 static() bool
131 staticBinary() bool
132 clang() bool
133 toolchain() Toolchain
134 noDefaultCompilerFlags() bool
135 sdk() bool
136 sdkVersion() string
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700137 selectedStl() string
Colin Crossca860ac2016-01-04 14:34:37 -0800138}
139
140type ModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700141 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800142 ModuleContextIntf
143}
144
145type BaseModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700146 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800147 ModuleContextIntf
148}
149
Colin Cross76fada02016-07-27 10:31:13 -0700150type CustomizerFlagsContext interface {
151 BaseModuleContext
152 AppendCflags(...string)
153 AppendLdflags(...string)
154 AppendAsflags(...string)
155}
156
Colin Crossca860ac2016-01-04 14:34:37 -0800157type Customizer interface {
Colin Cross76fada02016-07-27 10:31:13 -0700158 Flags(CustomizerFlagsContext)
Colin Crossca860ac2016-01-04 14:34:37 -0800159 Properties() []interface{}
160}
161
162type feature interface {
163 begin(ctx BaseModuleContext)
164 deps(ctx BaseModuleContext, deps Deps) Deps
165 flags(ctx ModuleContext, flags Flags) Flags
166 props() []interface{}
167}
168
169type compiler interface {
170 feature
Colin Cross76fada02016-07-27 10:31:13 -0700171 appendCflags([]string)
172 appendAsflags([]string)
Colin Cross635c3b02016-05-18 15:37:25 -0700173 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -0800174}
175
176type linker interface {
177 feature
Colin Cross635c3b02016-05-18 15:37:25 -0700178 link(ctx ModuleContext, flags Flags, deps PathDeps, objFiles android.Paths) android.Path
Colin Cross76fada02016-07-27 10:31:13 -0700179 appendLdflags([]string)
Colin Crossc99deeb2016-04-11 15:06:20 -0700180 installable() bool
Colin Crossca860ac2016-01-04 14:34:37 -0800181}
182
183type installer interface {
184 props() []interface{}
Colin Cross635c3b02016-05-18 15:37:25 -0700185 install(ctx ModuleContext, path android.Path)
Colin Crossca860ac2016-01-04 14:34:37 -0800186 inData() bool
187}
188
Colin Crossc99deeb2016-04-11 15:06:20 -0700189type dependencyTag struct {
190 blueprint.BaseDependencyTag
191 name string
192 library bool
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700193
194 reexportFlags bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700195}
196
197var (
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700198 sharedDepTag = dependencyTag{name: "shared", library: true}
199 sharedExportDepTag = dependencyTag{name: "shared", library: true, reexportFlags: true}
200 lateSharedDepTag = dependencyTag{name: "late shared", library: true}
201 staticDepTag = dependencyTag{name: "static", library: true}
202 staticExportDepTag = dependencyTag{name: "static", library: true, reexportFlags: true}
203 lateStaticDepTag = dependencyTag{name: "late static", library: true}
204 wholeStaticDepTag = dependencyTag{name: "whole static", library: true, reexportFlags: true}
205 genSourceDepTag = dependencyTag{name: "gen source"}
206 genHeaderDepTag = dependencyTag{name: "gen header"}
207 objDepTag = dependencyTag{name: "obj"}
208 crtBeginDepTag = dependencyTag{name: "crtbegin"}
209 crtEndDepTag = dependencyTag{name: "crtend"}
210 reuseObjTag = dependencyTag{name: "reuse objects"}
Dan Albert914449f2016-06-17 16:45:24 -0700211 ndkStubDepTag = dependencyTag{name: "ndk stub", library: true}
212 ndkLateStubDepTag = dependencyTag{name: "ndk late stub", library: true}
Colin Crossc99deeb2016-04-11 15:06:20 -0700213)
214
Colin Crossca860ac2016-01-04 14:34:37 -0800215// Module contains the properties and members used by all C/C++ module types, and implements
216// the blueprint.Module interface. It delegates to compiler, linker, and installer interfaces
217// to construct the output file. Behavior can be customized with a Customizer interface
218type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700219 android.ModuleBase
220 android.DefaultableModule
Colin Crossc472d572015-03-17 15:06:21 -0700221
Colin Crossca860ac2016-01-04 14:34:37 -0800222 Properties BaseProperties
223 unused UnusedProperties
Colin Crossfa138792015-04-24 17:31:52 -0700224
Colin Crossca860ac2016-01-04 14:34:37 -0800225 // initialize before calling Init
Colin Cross635c3b02016-05-18 15:37:25 -0700226 hod android.HostOrDeviceSupported
227 multilib android.Multilib
Colin Crossc472d572015-03-17 15:06:21 -0700228
Colin Crossca860ac2016-01-04 14:34:37 -0800229 // delegates, initialize before calling Init
Colin Cross76fada02016-07-27 10:31:13 -0700230 Customizer Customizer
Colin Crossca860ac2016-01-04 14:34:37 -0800231 features []feature
232 compiler compiler
233 linker linker
234 installer installer
Colin Crossa8e07cc2016-04-04 15:07:06 -0700235 stl *stl
Colin Cross16b23492016-01-06 14:41:07 -0800236 sanitize *sanitize
237
238 androidMkSharedLibDeps []string
Colin Cross74d1ec02015-04-28 13:30:13 -0700239
Colin Cross635c3b02016-05-18 15:37:25 -0700240 outputFile android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800241
242 cachedToolchain Toolchain
Colin Crossc472d572015-03-17 15:06:21 -0700243}
244
Colin Crossca860ac2016-01-04 14:34:37 -0800245func (c *Module) Init() (blueprint.Module, []interface{}) {
246 props := []interface{}{&c.Properties, &c.unused}
Colin Cross76fada02016-07-27 10:31:13 -0700247 if c.Customizer != nil {
248 props = append(props, c.Customizer.Properties()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800249 }
250 if c.compiler != nil {
251 props = append(props, c.compiler.props()...)
252 }
253 if c.linker != nil {
254 props = append(props, c.linker.props()...)
255 }
256 if c.installer != nil {
257 props = append(props, c.installer.props()...)
258 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700259 if c.stl != nil {
260 props = append(props, c.stl.props()...)
261 }
Colin Cross16b23492016-01-06 14:41:07 -0800262 if c.sanitize != nil {
263 props = append(props, c.sanitize.props()...)
264 }
Colin Crossca860ac2016-01-04 14:34:37 -0800265 for _, feature := range c.features {
266 props = append(props, feature.props()...)
267 }
Colin Crossc472d572015-03-17 15:06:21 -0700268
Colin Cross635c3b02016-05-18 15:37:25 -0700269 _, props = android.InitAndroidArchModule(c, c.hod, c.multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700270
Colin Cross635c3b02016-05-18 15:37:25 -0700271 return android.InitDefaultableModule(c, c, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700272}
273
Colin Crossca860ac2016-01-04 14:34:37 -0800274type baseModuleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700275 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800276 moduleContextImpl
277}
278
279type moduleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700280 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800281 moduleContextImpl
282}
283
284type moduleContextImpl struct {
285 mod *Module
286 ctx BaseModuleContext
287}
288
Colin Cross76fada02016-07-27 10:31:13 -0700289func (ctx *moduleContextImpl) AppendCflags(flags ...string) {
290 CheckBadCompilerFlags(ctx.ctx, "", flags)
291 ctx.mod.compiler.appendCflags(flags)
292}
293
294func (ctx *moduleContextImpl) AppendAsflags(flags ...string) {
295 CheckBadCompilerFlags(ctx.ctx, "", flags)
296 ctx.mod.compiler.appendAsflags(flags)
297}
298
299func (ctx *moduleContextImpl) AppendLdflags(flags ...string) {
300 CheckBadLinkerFlags(ctx.ctx, "", flags)
301 ctx.mod.linker.appendLdflags(flags)
302}
303
Colin Crossca860ac2016-01-04 14:34:37 -0800304func (ctx *moduleContextImpl) clang() bool {
305 return ctx.mod.clang(ctx.ctx)
306}
307
308func (ctx *moduleContextImpl) toolchain() Toolchain {
309 return ctx.mod.toolchain(ctx.ctx)
310}
311
312func (ctx *moduleContextImpl) static() bool {
313 if ctx.mod.linker == nil {
314 panic(fmt.Errorf("static called on module %q with no linker", ctx.ctx.ModuleName()))
315 }
316 if linker, ok := ctx.mod.linker.(baseLinkerInterface); ok {
317 return linker.static()
318 } else {
319 panic(fmt.Errorf("static called on module %q that doesn't use base linker", ctx.ctx.ModuleName()))
320 }
321}
322
323func (ctx *moduleContextImpl) staticBinary() bool {
324 if ctx.mod.linker == nil {
325 panic(fmt.Errorf("staticBinary called on module %q with no linker", ctx.ctx.ModuleName()))
326 }
327 if linker, ok := ctx.mod.linker.(baseLinkerInterface); ok {
328 return linker.staticBinary()
329 } else {
330 panic(fmt.Errorf("staticBinary called on module %q that doesn't use base linker", ctx.ctx.ModuleName()))
331 }
332}
333
334func (ctx *moduleContextImpl) noDefaultCompilerFlags() bool {
335 return Bool(ctx.mod.Properties.No_default_compiler_flags)
336}
337
338func (ctx *moduleContextImpl) sdk() bool {
Dan Willemsena96ff642016-06-07 12:34:45 -0700339 if ctx.ctx.Device() {
340 return ctx.mod.Properties.Sdk_version != ""
341 }
342 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800343}
344
345func (ctx *moduleContextImpl) sdkVersion() string {
Dan Willemsena96ff642016-06-07 12:34:45 -0700346 if ctx.ctx.Device() {
347 return ctx.mod.Properties.Sdk_version
348 }
349 return ""
Colin Crossca860ac2016-01-04 14:34:37 -0800350}
351
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700352func (ctx *moduleContextImpl) selectedStl() string {
353 if stl := ctx.mod.stl; stl != nil {
354 return stl.Properties.SelectedStl
355 }
356 return ""
357}
358
Colin Cross635c3b02016-05-18 15:37:25 -0700359func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800360 return &Module{
361 hod: hod,
362 multilib: multilib,
363 }
364}
365
Colin Cross635c3b02016-05-18 15:37:25 -0700366func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800367 module := newBaseModule(hod, multilib)
Colin Crossa8e07cc2016-04-04 15:07:06 -0700368 module.stl = &stl{}
Colin Cross16b23492016-01-06 14:41:07 -0800369 module.sanitize = &sanitize{}
Colin Crossca860ac2016-01-04 14:34:37 -0800370 return module
371}
372
Colin Cross635c3b02016-05-18 15:37:25 -0700373func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800374 ctx := &moduleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700375 ModuleContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800376 moduleContextImpl: moduleContextImpl{
377 mod: c,
378 },
379 }
380 ctx.ctx = ctx
381
Colin Cross76fada02016-07-27 10:31:13 -0700382 if c.Customizer != nil {
383 c.Customizer.Flags(ctx)
384 }
385
Colin Crossca860ac2016-01-04 14:34:37 -0800386 flags := Flags{
387 Toolchain: c.toolchain(ctx),
388 Clang: c.clang(ctx),
389 }
Colin Crossca860ac2016-01-04 14:34:37 -0800390 if c.compiler != nil {
391 flags = c.compiler.flags(ctx, flags)
392 }
393 if c.linker != nil {
394 flags = c.linker.flags(ctx, flags)
395 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700396 if c.stl != nil {
397 flags = c.stl.flags(ctx, flags)
398 }
Colin Cross16b23492016-01-06 14:41:07 -0800399 if c.sanitize != nil {
400 flags = c.sanitize.flags(ctx, flags)
401 }
Colin Crossca860ac2016-01-04 14:34:37 -0800402 for _, feature := range c.features {
403 flags = feature.flags(ctx, flags)
404 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800405 if ctx.Failed() {
406 return
407 }
408
Colin Crossca860ac2016-01-04 14:34:37 -0800409 flags.CFlags, _ = filterList(flags.CFlags, illegalFlags)
410 flags.CppFlags, _ = filterList(flags.CppFlags, illegalFlags)
411 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, illegalFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800412
Colin Crossca860ac2016-01-04 14:34:37 -0800413 // Optimization to reduce size of build.ninja
414 // Replace the long list of flags for each file with a module-local variable
415 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
416 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
417 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
418 flags.CFlags = []string{"$cflags"}
419 flags.CppFlags = []string{"$cppflags"}
420 flags.AsFlags = []string{"$asflags"}
421
Colin Crossc99deeb2016-04-11 15:06:20 -0700422 deps := c.depsToPaths(ctx)
Colin Cross3f40fa42015-01-30 17:27:36 -0800423 if ctx.Failed() {
424 return
425 }
426
Dan Willemsen76f08272016-07-09 00:14:08 -0700427 flags.GlobalFlags = append(flags.GlobalFlags, deps.Flags...)
Colin Crossed9f8682015-03-18 17:17:35 -0700428
Colin Cross635c3b02016-05-18 15:37:25 -0700429 var objFiles android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -0800430 if c.compiler != nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700431 objFiles = c.compiler.compile(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -0800432 if ctx.Failed() {
433 return
434 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800435 }
436
Colin Crossca860ac2016-01-04 14:34:37 -0800437 if c.linker != nil {
438 outputFile := c.linker.link(ctx, flags, deps, objFiles)
439 if ctx.Failed() {
440 return
441 }
Colin Cross635c3b02016-05-18 15:37:25 -0700442 c.outputFile = android.OptionalPathForPath(outputFile)
Colin Cross5049f022015-03-18 13:28:46 -0700443
Colin Crossc99deeb2016-04-11 15:06:20 -0700444 if c.installer != nil && c.linker.installable() {
Colin Crossca860ac2016-01-04 14:34:37 -0800445 c.installer.install(ctx, outputFile)
446 if ctx.Failed() {
447 return
448 }
449 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700450 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800451}
452
Colin Crossca860ac2016-01-04 14:34:37 -0800453func (c *Module) toolchain(ctx BaseModuleContext) Toolchain {
454 if c.cachedToolchain == nil {
455 arch := ctx.Arch()
Colin Crossa1ad8d12016-06-01 17:09:44 -0700456 os := ctx.Os()
457 factory := toolchainFactories[os][arch.ArchType]
Colin Crossca860ac2016-01-04 14:34:37 -0800458 if factory == nil {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700459 ctx.ModuleErrorf("Toolchain not found for %s arch %q", os.String(), arch.String())
Colin Crossca860ac2016-01-04 14:34:37 -0800460 return nil
461 }
462 c.cachedToolchain = factory(arch)
Colin Cross3f40fa42015-01-30 17:27:36 -0800463 }
Colin Crossca860ac2016-01-04 14:34:37 -0800464 return c.cachedToolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800465}
466
Colin Crossca860ac2016-01-04 14:34:37 -0800467func (c *Module) begin(ctx BaseModuleContext) {
468 if c.compiler != nil {
469 c.compiler.begin(ctx)
Colin Cross21b9a242015-03-24 14:15:58 -0700470 }
Colin Crossca860ac2016-01-04 14:34:37 -0800471 if c.linker != nil {
472 c.linker.begin(ctx)
473 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700474 if c.stl != nil {
475 c.stl.begin(ctx)
476 }
Colin Cross16b23492016-01-06 14:41:07 -0800477 if c.sanitize != nil {
478 c.sanitize.begin(ctx)
479 }
Colin Crossca860ac2016-01-04 14:34:37 -0800480 for _, feature := range c.features {
481 feature.begin(ctx)
482 }
483}
484
Colin Crossc99deeb2016-04-11 15:06:20 -0700485func (c *Module) deps(ctx BaseModuleContext) Deps {
486 deps := Deps{}
487
488 if c.compiler != nil {
489 deps = c.compiler.deps(ctx, deps)
490 }
491 if c.linker != nil {
492 deps = c.linker.deps(ctx, deps)
493 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700494 if c.stl != nil {
495 deps = c.stl.deps(ctx, deps)
496 }
Colin Cross16b23492016-01-06 14:41:07 -0800497 if c.sanitize != nil {
498 deps = c.sanitize.deps(ctx, deps)
499 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700500 for _, feature := range c.features {
501 deps = feature.deps(ctx, deps)
502 }
503
504 deps.WholeStaticLibs = lastUniqueElements(deps.WholeStaticLibs)
505 deps.StaticLibs = lastUniqueElements(deps.StaticLibs)
506 deps.LateStaticLibs = lastUniqueElements(deps.LateStaticLibs)
507 deps.SharedLibs = lastUniqueElements(deps.SharedLibs)
508 deps.LateSharedLibs = lastUniqueElements(deps.LateSharedLibs)
509
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700510 for _, lib := range deps.ReexportSharedLibHeaders {
511 if !inList(lib, deps.SharedLibs) {
512 ctx.PropertyErrorf("export_shared_lib_headers", "Shared library not in shared_libs: '%s'", lib)
513 }
514 }
515
516 for _, lib := range deps.ReexportStaticLibHeaders {
517 if !inList(lib, deps.StaticLibs) {
518 ctx.PropertyErrorf("export_static_lib_headers", "Static library not in static_libs: '%s'", lib)
519 }
520 }
521
Colin Crossc99deeb2016-04-11 15:06:20 -0700522 return deps
523}
524
Colin Cross635c3b02016-05-18 15:37:25 -0700525func (c *Module) depsMutator(actx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800526 ctx := &baseModuleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700527 BaseContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800528 moduleContextImpl: moduleContextImpl{
529 mod: c,
530 },
531 }
532 ctx.ctx = ctx
533
Colin Crossca860ac2016-01-04 14:34:37 -0800534 c.begin(ctx)
535
Colin Crossc99deeb2016-04-11 15:06:20 -0700536 deps := c.deps(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800537
Colin Crossb5bc4b42016-07-11 16:11:59 -0700538 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.SharedLibs...)
539 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.LateSharedLibs...)
Dan Willemsen72d39932016-07-08 23:23:48 -0700540
Dan Albert914449f2016-06-17 16:45:24 -0700541 variantNdkLibs := []string{}
542 variantLateNdkLibs := []string{}
Dan Willemsen72d39932016-07-08 23:23:48 -0700543 if ctx.sdk() {
Dan Albert914449f2016-06-17 16:45:24 -0700544 version := ctx.sdkVersion()
Dan Willemsen72d39932016-07-08 23:23:48 -0700545
Dan Albert914449f2016-06-17 16:45:24 -0700546 // Rewrites the names of shared libraries into the names of the NDK
547 // libraries where appropriate. This returns two slices.
548 //
549 // The first is a list of non-variant shared libraries (either rewritten
550 // NDK libraries to the modules in prebuilts/ndk, or not rewritten
551 // because they are not NDK libraries).
552 //
553 // The second is a list of ndk_library modules. These need to be
554 // separated because they are a variation dependency and must be added
555 // in a different manner.
556 rewriteNdkLibs := func(list []string) ([]string, []string) {
557 variantLibs := []string{}
558 nonvariantLibs := []string{}
559 for _, entry := range list {
Dan Willemsen72d39932016-07-08 23:23:48 -0700560 if inList(entry, ndkPrebuiltSharedLibraries) {
Dan Albert914449f2016-06-17 16:45:24 -0700561 if !inList(entry, ndkMigratedLibs) {
562 nonvariantLibs = append(nonvariantLibs, entry+".ndk."+version)
563 } else {
564 variantLibs = append(variantLibs, entry+ndkLibrarySuffix)
565 }
566 } else {
567 nonvariantLibs = append(variantLibs, entry)
Dan Willemsen72d39932016-07-08 23:23:48 -0700568 }
569 }
Dan Albert914449f2016-06-17 16:45:24 -0700570 return nonvariantLibs, variantLibs
Dan Willemsen72d39932016-07-08 23:23:48 -0700571 }
572
Dan Albert914449f2016-06-17 16:45:24 -0700573 deps.SharedLibs, variantNdkLibs = rewriteNdkLibs(deps.SharedLibs)
574 deps.LateSharedLibs, variantLateNdkLibs = rewriteNdkLibs(deps.LateSharedLibs)
Dan Willemsen72d39932016-07-08 23:23:48 -0700575 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700576
577 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, wholeStaticDepTag,
578 deps.WholeStaticLibs...)
579
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700580 for _, lib := range deps.StaticLibs {
581 depTag := staticDepTag
582 if inList(lib, deps.ReexportStaticLibHeaders) {
583 depTag = staticExportDepTag
584 }
Colin Cross15a0d462016-07-14 14:49:58 -0700585 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700586 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700587
588 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, lateStaticDepTag,
589 deps.LateStaticLibs...)
590
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700591 for _, lib := range deps.SharedLibs {
592 depTag := sharedDepTag
593 if inList(lib, deps.ReexportSharedLibHeaders) {
594 depTag = sharedExportDepTag
595 }
Colin Cross15a0d462016-07-14 14:49:58 -0700596 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700597 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700598
599 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, lateSharedDepTag,
600 deps.LateSharedLibs...)
601
Colin Cross68861832016-07-08 10:41:41 -0700602 actx.AddDependency(c, genSourceDepTag, deps.GeneratedSources...)
603 actx.AddDependency(c, genHeaderDepTag, deps.GeneratedHeaders...)
Dan Willemsenb40aab62016-04-20 14:21:14 -0700604
Colin Cross68861832016-07-08 10:41:41 -0700605 actx.AddDependency(c, objDepTag, deps.ObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700606
607 if deps.CrtBegin != "" {
Colin Cross68861832016-07-08 10:41:41 -0700608 actx.AddDependency(c, crtBeginDepTag, deps.CrtBegin)
Colin Crossca860ac2016-01-04 14:34:37 -0800609 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700610 if deps.CrtEnd != "" {
Colin Cross68861832016-07-08 10:41:41 -0700611 actx.AddDependency(c, crtEndDepTag, deps.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700612 }
Dan Albert914449f2016-06-17 16:45:24 -0700613
614 version := ctx.sdkVersion()
615 actx.AddVariationDependencies([]blueprint.Variation{
616 {"ndk_api", version}, {"link", "shared"}}, ndkStubDepTag, variantNdkLibs...)
617 actx.AddVariationDependencies([]blueprint.Variation{
618 {"ndk_api", version}, {"link", "shared"}}, ndkLateStubDepTag, variantLateNdkLibs...)
Colin Cross6362e272015-10-29 15:25:03 -0700619}
Colin Cross21b9a242015-03-24 14:15:58 -0700620
Colin Cross635c3b02016-05-18 15:37:25 -0700621func depsMutator(ctx android.BottomUpMutatorContext) {
Dan Willemsen3f32f032016-07-11 14:36:48 -0700622 if c, ok := ctx.Module().(*Module); ok && c.Enabled() {
Colin Cross6362e272015-10-29 15:25:03 -0700623 c.depsMutator(ctx)
624 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800625}
626
Colin Crossca860ac2016-01-04 14:34:37 -0800627func (c *Module) clang(ctx BaseModuleContext) bool {
628 clang := Bool(c.Properties.Clang)
629
630 if c.Properties.Clang == nil {
631 if ctx.Host() {
632 clang = true
633 }
634
635 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
636 clang = true
637 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800638 }
Colin Cross28344522015-04-22 13:07:53 -0700639
Colin Crossca860ac2016-01-04 14:34:37 -0800640 if !c.toolchain(ctx).ClangSupported() {
641 clang = false
642 }
643
644 return clang
645}
646
Colin Crossc99deeb2016-04-11 15:06:20 -0700647// Convert dependencies to paths. Returns a PathDeps containing paths
Colin Cross635c3b02016-05-18 15:37:25 -0700648func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
Colin Crossca860ac2016-01-04 14:34:37 -0800649 var depPaths PathDeps
Colin Crossca860ac2016-01-04 14:34:37 -0800650
Dan Willemsena96ff642016-06-07 12:34:45 -0700651 // Whether a module can link to another module, taking into
652 // account NDK linking.
653 linkTypeOk := func(from, to *Module) bool {
654 if from.Target().Os != android.Android {
655 // Host code is not restricted
656 return true
657 }
658 if from.Properties.Sdk_version == "" {
659 // Platform code can link to anything
660 return true
661 }
662 if _, ok := to.linker.(*toolchainLibraryLinker); ok {
663 // These are always allowed
664 return true
665 }
666 if _, ok := to.linker.(*ndkPrebuiltLibraryLinker); ok {
667 // These are allowed, but don't set sdk_version
668 return true
669 }
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700670 if _, ok := to.linker.(*ndkPrebuiltStlLinker); ok {
671 // These are allowed, but don't set sdk_version
672 return true
673 }
Dan Albert914449f2016-06-17 16:45:24 -0700674 if _, ok := to.linker.(*stubLinker); ok {
675 // These aren't real libraries, but are the stub shared libraries that are included in
676 // the NDK.
677 return true
678 }
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700679 return to.Properties.Sdk_version != ""
Dan Willemsena96ff642016-06-07 12:34:45 -0700680 }
681
Colin Crossc99deeb2016-04-11 15:06:20 -0700682 ctx.VisitDirectDeps(func(m blueprint.Module) {
683 name := ctx.OtherModuleName(m)
684 tag := ctx.OtherModuleDependencyTag(m)
Colin Crossca860ac2016-01-04 14:34:37 -0800685
Colin Cross635c3b02016-05-18 15:37:25 -0700686 a, _ := m.(android.Module)
Colin Crossc99deeb2016-04-11 15:06:20 -0700687 if a == nil {
688 ctx.ModuleErrorf("module %q not an android module", name)
689 return
Colin Crossca860ac2016-01-04 14:34:37 -0800690 }
Colin Crossca860ac2016-01-04 14:34:37 -0800691
Dan Willemsena96ff642016-06-07 12:34:45 -0700692 cc, _ := m.(*Module)
693 if cc == nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700694 switch tag {
Colin Cross635c3b02016-05-18 15:37:25 -0700695 case android.DefaultsDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700696 case genSourceDepTag:
697 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
698 depPaths.GeneratedSources = append(depPaths.GeneratedSources,
699 genRule.GeneratedSourceFiles()...)
700 } else {
701 ctx.ModuleErrorf("module %q is not a gensrcs or genrule", name)
702 }
703 case genHeaderDepTag:
704 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
705 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders,
706 genRule.GeneratedSourceFiles()...)
Dan Willemsen76f08272016-07-09 00:14:08 -0700707 depPaths.Flags = append(depPaths.Flags,
Colin Cross635c3b02016-05-18 15:37:25 -0700708 includeDirsToFlags(android.Paths{genRule.GeneratedHeaderDir()}))
Dan Willemsenb40aab62016-04-20 14:21:14 -0700709 } else {
710 ctx.ModuleErrorf("module %q is not a genrule", name)
711 }
712 default:
Colin Crossc99deeb2016-04-11 15:06:20 -0700713 ctx.ModuleErrorf("depends on non-cc module %q", name)
Colin Crossca860ac2016-01-04 14:34:37 -0800714 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700715 return
716 }
717
718 if !a.Enabled() {
719 ctx.ModuleErrorf("depends on disabled module %q", name)
720 return
721 }
722
Colin Crossa1ad8d12016-06-01 17:09:44 -0700723 if a.Target().Os != ctx.Os() {
724 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), name)
725 return
726 }
727
728 if a.Target().Arch.ArchType != ctx.Arch().ArchType {
729 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700730 return
731 }
732
Dan Willemsena96ff642016-06-07 12:34:45 -0700733 if !cc.outputFile.Valid() {
Colin Crossc99deeb2016-04-11 15:06:20 -0700734 ctx.ModuleErrorf("module %q missing output file", name)
735 return
736 }
737
738 if tag == reuseObjTag {
739 depPaths.ObjFiles = append(depPaths.ObjFiles,
Dan Willemsena96ff642016-06-07 12:34:45 -0700740 cc.compiler.(*libraryCompiler).reuseObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700741 return
742 }
743
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700744 if t, ok := tag.(dependencyTag); ok && t.library {
Dan Willemsena96ff642016-06-07 12:34:45 -0700745 if i, ok := cc.linker.(exportedFlagsProducer); ok {
Dan Willemsen76f08272016-07-09 00:14:08 -0700746 flags := i.exportedFlags()
747 depPaths.Flags = append(depPaths.Flags, flags...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700748
749 if t.reexportFlags {
Dan Willemsen76f08272016-07-09 00:14:08 -0700750 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700751 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700752 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700753
754 if !linkTypeOk(c, cc) {
755 ctx.ModuleErrorf("depends on non-NDK-built library %q", name)
756 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700757 }
758
Colin Cross635c3b02016-05-18 15:37:25 -0700759 var depPtr *android.Paths
Colin Crossc99deeb2016-04-11 15:06:20 -0700760
761 switch tag {
Dan Albert914449f2016-06-17 16:45:24 -0700762 case ndkStubDepTag, sharedDepTag, sharedExportDepTag:
Colin Crossc99deeb2016-04-11 15:06:20 -0700763 depPtr = &depPaths.SharedLibs
Dan Albert914449f2016-06-17 16:45:24 -0700764 case lateSharedDepTag, ndkLateStubDepTag:
Colin Crossc99deeb2016-04-11 15:06:20 -0700765 depPtr = &depPaths.LateSharedLibs
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700766 case staticDepTag, staticExportDepTag:
Colin Crossc99deeb2016-04-11 15:06:20 -0700767 depPtr = &depPaths.StaticLibs
768 case lateStaticDepTag:
769 depPtr = &depPaths.LateStaticLibs
770 case wholeStaticDepTag:
771 depPtr = &depPaths.WholeStaticLibs
Colin Crossc7a38dc2016-07-12 13:13:09 -0700772 staticLib, _ := cc.linker.(libraryInterface)
Colin Crossc99deeb2016-04-11 15:06:20 -0700773 if staticLib == nil || !staticLib.static() {
Dan Willemsena96ff642016-06-07 12:34:45 -0700774 ctx.ModuleErrorf("module %q not a static library", name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700775 return
776 }
777
778 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
779 postfix := " (required by " + ctx.OtherModuleName(m) + ")"
780 for i := range missingDeps {
781 missingDeps[i] += postfix
782 }
783 ctx.AddMissingDependencies(missingDeps)
784 }
785 depPaths.WholeStaticLibObjFiles =
Colin Crossc7a38dc2016-07-12 13:13:09 -0700786 append(depPaths.WholeStaticLibObjFiles, staticLib.objs()...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700787 case objDepTag:
788 depPtr = &depPaths.ObjFiles
789 case crtBeginDepTag:
Dan Willemsena96ff642016-06-07 12:34:45 -0700790 depPaths.CrtBegin = cc.outputFile
Colin Crossc99deeb2016-04-11 15:06:20 -0700791 case crtEndDepTag:
Dan Willemsena96ff642016-06-07 12:34:45 -0700792 depPaths.CrtEnd = cc.outputFile
Colin Crossc99deeb2016-04-11 15:06:20 -0700793 default:
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700794 panic(fmt.Errorf("unknown dependency tag: %s", tag))
Colin Crossc99deeb2016-04-11 15:06:20 -0700795 }
796
797 if depPtr != nil {
Dan Willemsena96ff642016-06-07 12:34:45 -0700798 *depPtr = append(*depPtr, cc.outputFile.Path())
Colin Crossca860ac2016-01-04 14:34:37 -0800799 }
800 })
801
802 return depPaths
803}
804
805func (c *Module) InstallInData() bool {
806 if c.installer == nil {
807 return false
808 }
809 return c.installer.inData()
810}
811
Colin Cross2ba19d92015-05-07 15:44:20 -0700812//
Colin Crosscfad1192015-11-02 16:43:11 -0800813// Defaults
814//
Colin Crossca860ac2016-01-04 14:34:37 -0800815type Defaults struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700816 android.ModuleBase
817 android.DefaultsModule
Colin Crosscfad1192015-11-02 16:43:11 -0800818}
819
Colin Cross635c3b02016-05-18 15:37:25 -0700820func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -0800821}
822
Colin Crossca860ac2016-01-04 14:34:37 -0800823func defaultsFactory() (blueprint.Module, []interface{}) {
824 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -0800825
826 propertyStructs := []interface{}{
Colin Crossca860ac2016-01-04 14:34:37 -0800827 &BaseProperties{},
828 &BaseCompilerProperties{},
829 &BaseLinkerProperties{},
830 &LibraryCompilerProperties{},
Colin Cross919281a2016-04-05 16:42:05 -0700831 &FlagExporterProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -0800832 &LibraryLinkerProperties{},
833 &BinaryLinkerProperties{},
834 &TestLinkerProperties{},
835 &UnusedProperties{},
836 &StlProperties{},
Colin Cross16b23492016-01-06 14:41:07 -0800837 &SanitizeProperties{},
Colin Cross665dce92016-04-28 14:50:03 -0700838 &StripProperties{},
Colin Crosscfad1192015-11-02 16:43:11 -0800839 }
840
Colin Cross635c3b02016-05-18 15:37:25 -0700841 _, propertyStructs = android.InitAndroidArchModule(module, android.HostAndDeviceDefault,
842 android.MultilibDefault, propertyStructs...)
Colin Crosscfad1192015-11-02 16:43:11 -0800843
Colin Cross635c3b02016-05-18 15:37:25 -0700844 return android.InitDefaultsModule(module, module, propertyStructs...)
Colin Crosscfad1192015-11-02 16:43:11 -0800845}
846
847//
Colin Cross3f40fa42015-01-30 17:27:36 -0800848// Device libraries shipped with gcc
849//
850
Colin Crossca860ac2016-01-04 14:34:37 -0800851type toolchainLibraryLinker struct {
852 baseLinker
Colin Cross3f40fa42015-01-30 17:27:36 -0800853}
854
Colin Crossca860ac2016-01-04 14:34:37 -0800855var _ baseLinkerInterface = (*toolchainLibraryLinker)(nil)
856
857func (*toolchainLibraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross3f40fa42015-01-30 17:27:36 -0800858 // toolchain libraries can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -0800859 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -0800860}
861
Colin Crossca860ac2016-01-04 14:34:37 -0800862func (*toolchainLibraryLinker) buildStatic() bool {
863 return true
864}
Colin Cross3f40fa42015-01-30 17:27:36 -0800865
Colin Crossca860ac2016-01-04 14:34:37 -0800866func (*toolchainLibraryLinker) buildShared() bool {
867 return false
868}
869
870func toolchainLibraryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -0700871 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -0800872 module.compiler = &baseCompiler{}
873 module.linker = &toolchainLibraryLinker{}
Dan Willemsenfc9c28c2016-01-12 16:22:40 -0800874 module.Properties.Clang = proptools.BoolPtr(false)
Colin Crossca860ac2016-01-04 14:34:37 -0800875 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -0800876}
877
Colin Crossca860ac2016-01-04 14:34:37 -0800878func (library *toolchainLibraryLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -0700879 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -0800880
881 libName := ctx.ModuleName() + staticLibraryExtension
Colin Cross635c3b02016-05-18 15:37:25 -0700882 outputFile := android.PathForModuleOut(ctx, libName)
Colin Cross3f40fa42015-01-30 17:27:36 -0800883
Dan Willemsenfc9c28c2016-01-12 16:22:40 -0800884 if flags.Clang {
885 ctx.ModuleErrorf("toolchain_library must use GCC, not Clang")
886 }
887
Colin Crossca860ac2016-01-04 14:34:37 -0800888 CopyGccLib(ctx, libName, flagsToBuilderFlags(flags), outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -0800889
890 ctx.CheckbuildFile(outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -0800891
Colin Crossca860ac2016-01-04 14:34:37 -0800892 return outputFile
Dan Albertc403f7c2015-03-18 14:01:18 -0700893}
894
Colin Crossc99deeb2016-04-11 15:06:20 -0700895func (*toolchainLibraryLinker) installable() bool {
896 return false
897}
898
Colin Cross74d1ec02015-04-28 13:30:13 -0700899// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
900// modifies the slice contents in place, and returns a subslice of the original slice
901func lastUniqueElements(list []string) []string {
902 totalSkip := 0
903 for i := len(list) - 1; i >= totalSkip; i-- {
904 skip := 0
905 for j := i - 1; j >= totalSkip; j-- {
906 if list[i] == list[j] {
907 skip++
908 } else {
909 list[j+skip] = list[j]
910 }
911 }
912 totalSkip += skip
913 }
914 return list[totalSkip:]
915}
Colin Cross06a931b2015-10-28 17:23:31 -0700916
917var Bool = proptools.Bool