blob: 3824b451ebe4594347473836bc489ca7470f9005 [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 (
Dan Albert9e10cd42016-08-03 14:12:14 -070022 "strconv"
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 Cross635c3b02016-05-18 15:37:25 -070028 "android/soong/android"
Colin Crossb98c8b02016-07-29 13:44:28 -070029 "android/soong/cc/config"
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 Cross798bfce2016-10-12 14:28:16 -070034 android.RegisterModuleType("cc_defaults", defaultsFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070035
Colin Cross1e676be2016-10-12 14:38:15 -070036 android.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
37 ctx.BottomUp("link", linkageMutator).Parallel()
38 ctx.BottomUp("ndk_api", ndkApiMutator).Parallel()
39 ctx.BottomUp("test_per_src", testPerSrcMutator).Parallel()
40 ctx.BottomUp("begin", beginMutator).Parallel()
41 })
Colin Cross16b23492016-01-06 14:41:07 -080042
Colin Cross1e676be2016-10-12 14:38:15 -070043 android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
44 ctx.TopDown("asan_deps", sanitizerDepsMutator(asan))
45 ctx.BottomUp("asan", sanitizerMutator(asan)).Parallel()
Colin Cross16b23492016-01-06 14:41:07 -080046
Colin Cross1e676be2016-10-12 14:38:15 -070047 ctx.TopDown("tsan_deps", sanitizerDepsMutator(tsan))
48 ctx.BottomUp("tsan", sanitizerMutator(tsan)).Parallel()
49 })
Colin Crossb98c8b02016-07-29 13:44:28 -070050
51 pctx.Import("android/soong/cc/config")
Colin Cross463a90e2015-06-17 14:20:06 -070052}
53
Colin Crossca860ac2016-01-04 14:34:37 -080054type Deps struct {
55 SharedLibs, LateSharedLibs []string
56 StaticLibs, LateStaticLibs, WholeStaticLibs []string
Colin Cross5950f382016-12-13 12:50:57 -080057 HeaderLibs []string
Colin Crossc472d572015-03-17 15:06:21 -070058
Colin Cross5950f382016-12-13 12:50:57 -080059 ReexportSharedLibHeaders, ReexportStaticLibHeaders, ReexportHeaderLibHeaders []string
Dan Willemsen490a8dc2016-06-06 18:22:19 -070060
Colin Cross81413472016-04-11 14:37:39 -070061 ObjFiles []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -070062
Dan Willemsenb40aab62016-04-20 14:21:14 -070063 GeneratedSources []string
64 GeneratedHeaders []string
65
Dan Willemsenb3454ab2016-09-28 17:34:58 -070066 ReexportGeneratedHeaders []string
67
Colin Cross97ba0732015-03-23 17:50:24 -070068 CrtBegin, CrtEnd string
Colin Crossc472d572015-03-17 15:06:21 -070069}
70
Colin Crossca860ac2016-01-04 14:34:37 -080071type PathDeps struct {
Colin Cross26c34ed2016-09-30 17:10:16 -070072 // Paths to .so files
73 SharedLibs, LateSharedLibs android.Paths
74 // Paths to the dependencies to use for .so files (.so.toc files)
75 SharedLibsDeps, LateSharedLibsDeps android.Paths
76 // Paths to .a files
Colin Cross635c3b02016-05-18 15:37:25 -070077 StaticLibs, LateStaticLibs, WholeStaticLibs android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -070078
Colin Cross26c34ed2016-09-30 17:10:16 -070079 // Paths to .o files
Dan Willemsen5cb580f2016-09-26 17:33:01 -070080 Objs Objects
81 WholeStaticLibObjs Objects
Dan Willemsen34cc69e2015-09-23 15:26:20 -070082
Colin Cross26c34ed2016-09-30 17:10:16 -070083 // Paths to generated source files
Colin Cross635c3b02016-05-18 15:37:25 -070084 GeneratedSources android.Paths
85 GeneratedHeaders android.Paths
Dan Willemsenb40aab62016-04-20 14:21:14 -070086
Dan Willemsen76f08272016-07-09 00:14:08 -070087 Flags, ReexportedFlags []string
Dan Willemsen847dcc72016-09-29 12:13:36 -070088 ReexportedFlagsDeps android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -070089
Colin Cross26c34ed2016-09-30 17:10:16 -070090 // Paths to crt*.o files
Colin Cross635c3b02016-05-18 15:37:25 -070091 CrtBegin, CrtEnd android.OptionalPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -070092}
93
Colin Crossca860ac2016-01-04 14:34:37 -080094type Flags struct {
Colin Cross28344522015-04-22 13:07:53 -070095 GlobalFlags []string // Flags that apply to C, C++, and assembly source files
96 AsFlags []string // Flags that apply to assembly source files
97 CFlags []string // Flags that apply to C and C++ source files
98 ConlyFlags []string // Flags that apply to C source files
99 CppFlags []string // Flags that apply to C++ source files
100 YaccFlags []string // Flags that apply to Yacc source files
Colin Cross0c461f12016-10-20 16:11:43 -0700101 protoFlags []string // Flags that apply to proto source files
Dan Willemsene1240db2016-11-03 14:28:51 -0700102 aidlFlags []string // Flags that apply to aidl source files
Colin Cross28344522015-04-22 13:07:53 -0700103 LdFlags []string // Flags that apply to linker command lines
Colin Cross16b23492016-01-06 14:41:07 -0800104 libFlags []string // Flags to add libraries early to the link order
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700105 TidyFlags []string // Flags that apply to clang-tidy
Colin Cross91e90042016-12-02 17:13:24 -0800106 YasmFlags []string // Flags that apply to yasm assembly source files
Colin Cross28344522015-04-22 13:07:53 -0700107
Colin Crossb98c8b02016-07-29 13:44:28 -0700108 Toolchain config.Toolchain
Colin Cross28344522015-04-22 13:07:53 -0700109 Clang bool
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700110 Tidy bool
Colin Crossca860ac2016-01-04 14:34:37 -0800111
112 RequiredInstructionSet string
Colin Cross16b23492016-01-06 14:41:07 -0800113 DynamicLinker string
114
Colin Cross635c3b02016-05-18 15:37:25 -0700115 CFlagsDeps android.Paths // Files depended on by compiler flags
Colin Cross18c0c5a2016-12-01 14:45:23 -0800116
117 GroupStaticLibs bool
Colin Crossc472d572015-03-17 15:06:21 -0700118}
119
Colin Cross81413472016-04-11 14:37:39 -0700120type ObjectLinkerProperties struct {
121 // names of other cc_object modules to link into this module using partial linking
122 Objs []string `android:"arch_variant"`
123}
124
Colin Crossca860ac2016-01-04 14:34:37 -0800125// Properties used to compile all C or C++ modules
126type BaseProperties struct {
127 // compile module with clang instead of gcc
128 Clang *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700129
130 // Minimum sdk version supported when compiling against the ndk
131 Sdk_version string
132
Dan Willemsend2ede872016-11-18 14:54:24 -0800133 // Whether to compile against the VNDK
134 Use_vndk bool
135
Colin Crossca860ac2016-01-04 14:34:37 -0800136 // don't insert default compiler flags into asflags, cflags,
137 // cppflags, conlyflags, ldflags, or include_dirs
138 No_default_compiler_flags *bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700139
140 AndroidMkSharedLibs []string `blueprint:"mutated"`
Colin Crossbc6fb162016-05-24 15:39:04 -0700141 HideFromMake bool `blueprint:"mutated"`
Colin Crossb0f28952016-09-19 16:46:53 -0700142 PreventInstall bool `blueprint:"mutated"`
Dan Willemsend2ede872016-11-18 14:54:24 -0800143 Vndk_version string `blueprint:"mutated"`
Colin Crossca860ac2016-01-04 14:34:37 -0800144}
145
Colin Crossca860ac2016-01-04 14:34:37 -0800146type UnusedProperties struct {
Colin Cross21b481b2016-04-15 16:27:17 -0700147 Native_coverage *bool
Colin Cross21b481b2016-04-15 16:27:17 -0700148 Tags []string
Colin Crosscfad1192015-11-02 16:43:11 -0800149}
150
Colin Crossca860ac2016-01-04 14:34:37 -0800151type ModuleContextIntf interface {
Colin Crossca860ac2016-01-04 14:34:37 -0800152 static() bool
153 staticBinary() bool
154 clang() bool
Colin Crossb98c8b02016-07-29 13:44:28 -0700155 toolchain() config.Toolchain
Colin Crossca860ac2016-01-04 14:34:37 -0800156 noDefaultCompilerFlags() bool
157 sdk() bool
158 sdkVersion() string
Dan Willemsend2ede872016-11-18 14:54:24 -0800159 vndk() bool
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700160 selectedStl() string
Colin Crossce75d2c2016-10-06 16:12:58 -0700161 baseModuleName() string
Colin Crossca860ac2016-01-04 14:34:37 -0800162}
163
164type ModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700165 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800166 ModuleContextIntf
167}
168
169type BaseModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700170 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800171 ModuleContextIntf
172}
173
Colin Cross37047f12016-12-13 17:06:13 -0800174type DepsContext interface {
175 android.BottomUpMutatorContext
176 ModuleContextIntf
177}
178
Colin Crossca860ac2016-01-04 14:34:37 -0800179type feature interface {
180 begin(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800181 deps(ctx DepsContext, deps Deps) Deps
Colin Crossca860ac2016-01-04 14:34:37 -0800182 flags(ctx ModuleContext, flags Flags) Flags
183 props() []interface{}
184}
185
186type compiler interface {
Colin Cross42742b82016-08-01 13:20:05 -0700187 compilerInit(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800188 compilerDeps(ctx DepsContext, deps Deps) Deps
Colin Cross42742b82016-08-01 13:20:05 -0700189 compilerFlags(ctx ModuleContext, flags Flags) Flags
190 compilerProps() []interface{}
191
Colin Cross76fada02016-07-27 10:31:13 -0700192 appendCflags([]string)
193 appendAsflags([]string)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700194 compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects
Colin Crossca860ac2016-01-04 14:34:37 -0800195}
196
197type linker interface {
Colin Cross42742b82016-08-01 13:20:05 -0700198 linkerInit(ctx BaseModuleContext)
Colin Cross37047f12016-12-13 17:06:13 -0800199 linkerDeps(ctx DepsContext, deps Deps) Deps
Colin Cross42742b82016-08-01 13:20:05 -0700200 linkerFlags(ctx ModuleContext, flags Flags) Flags
201 linkerProps() []interface{}
202
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700203 link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path
Colin Cross76fada02016-07-27 10:31:13 -0700204 appendLdflags([]string)
Colin Crossca860ac2016-01-04 14:34:37 -0800205}
206
207type installer interface {
Colin Cross42742b82016-08-01 13:20:05 -0700208 installerProps() []interface{}
Colin Cross635c3b02016-05-18 15:37:25 -0700209 install(ctx ModuleContext, path android.Path)
Colin Crossca860ac2016-01-04 14:34:37 -0800210 inData() bool
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700211 hostToolPath() android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800212}
213
Colin Crossc99deeb2016-04-11 15:06:20 -0700214type dependencyTag struct {
215 blueprint.BaseDependencyTag
216 name string
217 library bool
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700218
219 reexportFlags bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700220}
221
222var (
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700223 sharedDepTag = dependencyTag{name: "shared", library: true}
224 sharedExportDepTag = dependencyTag{name: "shared", library: true, reexportFlags: true}
225 lateSharedDepTag = dependencyTag{name: "late shared", library: true}
226 staticDepTag = dependencyTag{name: "static", library: true}
227 staticExportDepTag = dependencyTag{name: "static", library: true, reexportFlags: true}
228 lateStaticDepTag = dependencyTag{name: "late static", library: true}
229 wholeStaticDepTag = dependencyTag{name: "whole static", library: true, reexportFlags: true}
Colin Cross32ec36c2016-12-15 07:39:51 -0800230 headerDepTag = dependencyTag{name: "header", library: true}
231 headerExportDepTag = dependencyTag{name: "header", library: true, reexportFlags: true}
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700232 genSourceDepTag = dependencyTag{name: "gen source"}
233 genHeaderDepTag = dependencyTag{name: "gen header"}
234 genHeaderExportDepTag = dependencyTag{name: "gen header", reexportFlags: true}
235 objDepTag = dependencyTag{name: "obj"}
236 crtBeginDepTag = dependencyTag{name: "crtbegin"}
237 crtEndDepTag = dependencyTag{name: "crtend"}
238 reuseObjTag = dependencyTag{name: "reuse objects"}
239 ndkStubDepTag = dependencyTag{name: "ndk stub", library: true}
240 ndkLateStubDepTag = dependencyTag{name: "ndk late stub", library: true}
Colin Crossc99deeb2016-04-11 15:06:20 -0700241)
242
Colin Crossca860ac2016-01-04 14:34:37 -0800243// Module contains the properties and members used by all C/C++ module types, and implements
244// the blueprint.Module interface. It delegates to compiler, linker, and installer interfaces
245// to construct the output file. Behavior can be customized with a Customizer interface
246type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700247 android.ModuleBase
248 android.DefaultableModule
Colin Crossc472d572015-03-17 15:06:21 -0700249
Colin Crossca860ac2016-01-04 14:34:37 -0800250 Properties BaseProperties
251 unused UnusedProperties
Colin Crossfa138792015-04-24 17:31:52 -0700252
Colin Crossca860ac2016-01-04 14:34:37 -0800253 // initialize before calling Init
Colin Cross635c3b02016-05-18 15:37:25 -0700254 hod android.HostOrDeviceSupported
255 multilib android.Multilib
Colin Crossc472d572015-03-17 15:06:21 -0700256
Colin Crossca860ac2016-01-04 14:34:37 -0800257 // delegates, initialize before calling Init
Colin Crossb4ce0ec2016-09-13 13:41:39 -0700258 features []feature
259 compiler compiler
260 linker linker
261 installer installer
262 stl *stl
263 sanitize *sanitize
Colin Cross16b23492016-01-06 14:41:07 -0800264
265 androidMkSharedLibDeps []string
Colin Cross74d1ec02015-04-28 13:30:13 -0700266
Colin Cross635c3b02016-05-18 15:37:25 -0700267 outputFile android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800268
Colin Crossb98c8b02016-07-29 13:44:28 -0700269 cachedToolchain config.Toolchain
Colin Crossb916a382016-07-29 17:28:03 -0700270
271 subAndroidMkOnce map[subAndroidMkProvider]bool
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800272
273 // Flags used to compile this module
274 flags Flags
Colin Crossc472d572015-03-17 15:06:21 -0700275}
276
Colin Crossca860ac2016-01-04 14:34:37 -0800277func (c *Module) Init() (blueprint.Module, []interface{}) {
278 props := []interface{}{&c.Properties, &c.unused}
Colin Crossca860ac2016-01-04 14:34:37 -0800279 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700280 props = append(props, c.compiler.compilerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800281 }
282 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700283 props = append(props, c.linker.linkerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800284 }
285 if c.installer != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700286 props = append(props, c.installer.installerProps()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800287 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700288 if c.stl != nil {
289 props = append(props, c.stl.props()...)
290 }
Colin Cross16b23492016-01-06 14:41:07 -0800291 if c.sanitize != nil {
292 props = append(props, c.sanitize.props()...)
293 }
Colin Crossca860ac2016-01-04 14:34:37 -0800294 for _, feature := range c.features {
295 props = append(props, feature.props()...)
296 }
Colin Crossc472d572015-03-17 15:06:21 -0700297
Colin Cross635c3b02016-05-18 15:37:25 -0700298 _, props = android.InitAndroidArchModule(c, c.hod, c.multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700299
Colin Cross635c3b02016-05-18 15:37:25 -0700300 return android.InitDefaultableModule(c, c, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700301}
302
Colin Crossb916a382016-07-29 17:28:03 -0700303// Returns true for dependency roots (binaries)
304// TODO(ccross): also handle dlopenable libraries
305func (c *Module) isDependencyRoot() bool {
306 if root, ok := c.linker.(interface {
307 isDependencyRoot() bool
308 }); ok {
309 return root.isDependencyRoot()
310 }
311 return false
312}
313
Colin Crossca860ac2016-01-04 14:34:37 -0800314type baseModuleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700315 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800316 moduleContextImpl
317}
318
Colin Cross37047f12016-12-13 17:06:13 -0800319type depsContext struct {
320 android.BottomUpMutatorContext
321 moduleContextImpl
322}
323
Colin Crossca860ac2016-01-04 14:34:37 -0800324type moduleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700325 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800326 moduleContextImpl
327}
328
329type moduleContextImpl struct {
330 mod *Module
331 ctx BaseModuleContext
332}
333
Colin Crossca860ac2016-01-04 14:34:37 -0800334func (ctx *moduleContextImpl) clang() bool {
335 return ctx.mod.clang(ctx.ctx)
336}
337
Colin Crossb98c8b02016-07-29 13:44:28 -0700338func (ctx *moduleContextImpl) toolchain() config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800339 return ctx.mod.toolchain(ctx.ctx)
340}
341
342func (ctx *moduleContextImpl) static() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700343 if static, ok := ctx.mod.linker.(interface {
344 static() bool
345 }); ok {
346 return static.static()
Colin Crossca860ac2016-01-04 14:34:37 -0800347 }
Colin Crossb916a382016-07-29 17:28:03 -0700348 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800349}
350
351func (ctx *moduleContextImpl) staticBinary() bool {
Colin Crossb916a382016-07-29 17:28:03 -0700352 if static, ok := ctx.mod.linker.(interface {
353 staticBinary() bool
354 }); ok {
355 return static.staticBinary()
Colin Crossca860ac2016-01-04 14:34:37 -0800356 }
Colin Crossb916a382016-07-29 17:28:03 -0700357 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800358}
359
360func (ctx *moduleContextImpl) noDefaultCompilerFlags() bool {
361 return Bool(ctx.mod.Properties.No_default_compiler_flags)
362}
363
364func (ctx *moduleContextImpl) sdk() bool {
Dan Willemsena96ff642016-06-07 12:34:45 -0700365 if ctx.ctx.Device() {
366 return ctx.mod.Properties.Sdk_version != ""
367 }
368 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800369}
370
371func (ctx *moduleContextImpl) sdkVersion() string {
Dan Willemsena96ff642016-06-07 12:34:45 -0700372 if ctx.ctx.Device() {
Dan Willemsend2ede872016-11-18 14:54:24 -0800373 if ctx.mod.Properties.Use_vndk {
374 return ctx.mod.Properties.Vndk_version
375 } else {
376 return ctx.mod.Properties.Sdk_version
377 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700378 }
379 return ""
Colin Crossca860ac2016-01-04 14:34:37 -0800380}
381
Dan Willemsend2ede872016-11-18 14:54:24 -0800382func (ctx *moduleContextImpl) vndk() bool {
383 if ctx.ctx.Device() {
384 return ctx.mod.Properties.Use_vndk
385 }
386 return false
387}
388
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700389func (ctx *moduleContextImpl) selectedStl() string {
390 if stl := ctx.mod.stl; stl != nil {
391 return stl.Properties.SelectedStl
392 }
393 return ""
394}
395
Colin Crossce75d2c2016-10-06 16:12:58 -0700396func (ctx *moduleContextImpl) baseModuleName() string {
397 return ctx.mod.ModuleBase.BaseModuleName()
398}
399
Colin Cross635c3b02016-05-18 15:37:25 -0700400func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800401 return &Module{
402 hod: hod,
403 multilib: multilib,
404 }
405}
406
Colin Cross635c3b02016-05-18 15:37:25 -0700407func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800408 module := newBaseModule(hod, multilib)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700409 module.features = []feature{
410 &tidyFeature{},
411 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700412 module.stl = &stl{}
Colin Cross16b23492016-01-06 14:41:07 -0800413 module.sanitize = &sanitize{}
Colin Crossca860ac2016-01-04 14:34:37 -0800414 return module
415}
416
Colin Crossce75d2c2016-10-06 16:12:58 -0700417func (c *Module) Prebuilt() *android.Prebuilt {
418 if p, ok := c.linker.(prebuiltLinkerInterface); ok {
419 return p.prebuilt()
420 }
421 return nil
422}
423
424func (c *Module) Name() string {
425 name := c.ModuleBase.Name()
426 if p, ok := c.linker.(prebuiltLinkerInterface); ok {
427 name = p.Name(name)
428 }
429 return name
430}
431
Colin Cross635c3b02016-05-18 15:37:25 -0700432func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800433 ctx := &moduleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700434 ModuleContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800435 moduleContextImpl: moduleContextImpl{
436 mod: c,
437 },
438 }
439 ctx.ctx = ctx
440
441 flags := Flags{
442 Toolchain: c.toolchain(ctx),
443 Clang: c.clang(ctx),
444 }
Colin Crossca860ac2016-01-04 14:34:37 -0800445 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700446 flags = c.compiler.compilerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800447 }
448 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700449 flags = c.linker.linkerFlags(ctx, flags)
Colin Crossca860ac2016-01-04 14:34:37 -0800450 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700451 if c.stl != nil {
452 flags = c.stl.flags(ctx, flags)
453 }
Colin Cross16b23492016-01-06 14:41:07 -0800454 if c.sanitize != nil {
455 flags = c.sanitize.flags(ctx, flags)
456 }
Colin Crossca860ac2016-01-04 14:34:37 -0800457 for _, feature := range c.features {
458 flags = feature.flags(ctx, flags)
459 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800460 if ctx.Failed() {
461 return
462 }
463
Colin Crossb98c8b02016-07-29 13:44:28 -0700464 flags.CFlags, _ = filterList(flags.CFlags, config.IllegalFlags)
465 flags.CppFlags, _ = filterList(flags.CppFlags, config.IllegalFlags)
466 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, config.IllegalFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800467
Fabien Sanglardd61f1f42017-01-10 16:21:22 -0800468 deps := c.depsToPaths(ctx)
469 if ctx.Failed() {
470 return
471 }
472 flags.GlobalFlags = append(flags.GlobalFlags, deps.Flags...)
473 c.flags = flags
474
Colin Crossca860ac2016-01-04 14:34:37 -0800475 // Optimization to reduce size of build.ninja
476 // Replace the long list of flags for each file with a module-local variable
477 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
478 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
479 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
480 flags.CFlags = []string{"$cflags"}
481 flags.CppFlags = []string{"$cppflags"}
482 flags.AsFlags = []string{"$asflags"}
483
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700484 var objs Objects
Colin Crossca860ac2016-01-04 14:34:37 -0800485 if c.compiler != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700486 objs = c.compiler.compile(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -0800487 if ctx.Failed() {
488 return
489 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800490 }
491
Colin Crossca860ac2016-01-04 14:34:37 -0800492 if c.linker != nil {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700493 outputFile := c.linker.link(ctx, flags, deps, objs)
Colin Crossca860ac2016-01-04 14:34:37 -0800494 if ctx.Failed() {
495 return
496 }
Colin Cross635c3b02016-05-18 15:37:25 -0700497 c.outputFile = android.OptionalPathForPath(outputFile)
Colin Crossce75d2c2016-10-06 16:12:58 -0700498 }
Colin Cross5049f022015-03-18 13:28:46 -0700499
Colin Crossce75d2c2016-10-06 16:12:58 -0700500 if c.installer != nil && !c.Properties.PreventInstall && c.outputFile.Valid() {
501 c.installer.install(ctx, c.outputFile.Path())
502 if ctx.Failed() {
503 return
Colin Crossca860ac2016-01-04 14:34:37 -0800504 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700505 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800506}
507
Colin Crossb98c8b02016-07-29 13:44:28 -0700508func (c *Module) toolchain(ctx BaseModuleContext) config.Toolchain {
Colin Crossca860ac2016-01-04 14:34:37 -0800509 if c.cachedToolchain == nil {
Colin Crossb98c8b02016-07-29 13:44:28 -0700510 c.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
Colin Cross3f40fa42015-01-30 17:27:36 -0800511 }
Colin Crossca860ac2016-01-04 14:34:37 -0800512 return c.cachedToolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800513}
514
Colin Crossca860ac2016-01-04 14:34:37 -0800515func (c *Module) begin(ctx BaseModuleContext) {
516 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700517 c.compiler.compilerInit(ctx)
Colin Cross21b9a242015-03-24 14:15:58 -0700518 }
Colin Crossca860ac2016-01-04 14:34:37 -0800519 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700520 c.linker.linkerInit(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800521 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700522 if c.stl != nil {
523 c.stl.begin(ctx)
524 }
Colin Cross16b23492016-01-06 14:41:07 -0800525 if c.sanitize != nil {
526 c.sanitize.begin(ctx)
527 }
Colin Crossca860ac2016-01-04 14:34:37 -0800528 for _, feature := range c.features {
529 feature.begin(ctx)
530 }
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700531 if ctx.sdk() {
Dan Willemsend2ede872016-11-18 14:54:24 -0800532 if ctx.vndk() {
533 ctx.PropertyErrorf("use_vndk",
534 "sdk_version and use_vndk cannot be used at the same time")
535 }
536
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700537 version, err := normalizeNdkApiLevel(ctx.sdkVersion(), ctx.Arch())
538 if err != nil {
539 ctx.PropertyErrorf("sdk_version", err.Error())
540 }
Dan Albert90f7a4d2016-11-08 14:34:24 -0800541 c.Properties.Sdk_version = version
Dan Willemsend2ede872016-11-18 14:54:24 -0800542 } else if ctx.vndk() {
543 version, err := normalizeNdkApiLevel(ctx.DeviceConfig().VndkVersion(), ctx.Arch())
544 if err != nil {
545 ctx.ModuleErrorf("Bad BOARD_VNDK_VERSION: %s", err.Error())
546 }
547 c.Properties.Vndk_version = version
Dan Albert7fa7b2e2016-08-05 16:37:52 -0700548 }
Colin Crossca860ac2016-01-04 14:34:37 -0800549}
550
Colin Cross37047f12016-12-13 17:06:13 -0800551func (c *Module) deps(ctx DepsContext) Deps {
Colin Crossc99deeb2016-04-11 15:06:20 -0700552 deps := Deps{}
553
554 if c.compiler != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700555 deps = c.compiler.compilerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700556 }
557 if c.linker != nil {
Colin Cross42742b82016-08-01 13:20:05 -0700558 deps = c.linker.linkerDeps(ctx, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -0700559 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700560 if c.stl != nil {
561 deps = c.stl.deps(ctx, deps)
562 }
Colin Cross16b23492016-01-06 14:41:07 -0800563 if c.sanitize != nil {
564 deps = c.sanitize.deps(ctx, deps)
565 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700566 for _, feature := range c.features {
567 deps = feature.deps(ctx, deps)
568 }
569
570 deps.WholeStaticLibs = lastUniqueElements(deps.WholeStaticLibs)
571 deps.StaticLibs = lastUniqueElements(deps.StaticLibs)
572 deps.LateStaticLibs = lastUniqueElements(deps.LateStaticLibs)
573 deps.SharedLibs = lastUniqueElements(deps.SharedLibs)
574 deps.LateSharedLibs = lastUniqueElements(deps.LateSharedLibs)
Colin Cross5950f382016-12-13 12:50:57 -0800575 deps.HeaderLibs = lastUniqueElements(deps.HeaderLibs)
Colin Crossc99deeb2016-04-11 15:06:20 -0700576
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700577 for _, lib := range deps.ReexportSharedLibHeaders {
578 if !inList(lib, deps.SharedLibs) {
579 ctx.PropertyErrorf("export_shared_lib_headers", "Shared library not in shared_libs: '%s'", lib)
580 }
581 }
582
583 for _, lib := range deps.ReexportStaticLibHeaders {
584 if !inList(lib, deps.StaticLibs) {
585 ctx.PropertyErrorf("export_static_lib_headers", "Static library not in static_libs: '%s'", lib)
586 }
587 }
588
Colin Cross5950f382016-12-13 12:50:57 -0800589 for _, lib := range deps.ReexportHeaderLibHeaders {
590 if !inList(lib, deps.HeaderLibs) {
591 ctx.PropertyErrorf("export_header_lib_headers", "Header library not in header_libs: '%s'", lib)
592 }
593 }
594
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700595 for _, gen := range deps.ReexportGeneratedHeaders {
596 if !inList(gen, deps.GeneratedHeaders) {
597 ctx.PropertyErrorf("export_generated_headers", "Generated header module not in generated_headers: '%s'", gen)
598 }
599 }
600
Colin Crossc99deeb2016-04-11 15:06:20 -0700601 return deps
602}
603
Dan Albert7e9d2952016-08-04 13:02:36 -0700604func (c *Module) beginMutator(actx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800605 ctx := &baseModuleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700606 BaseContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800607 moduleContextImpl: moduleContextImpl{
608 mod: c,
609 },
610 }
611 ctx.ctx = ctx
612
Colin Crossca860ac2016-01-04 14:34:37 -0800613 c.begin(ctx)
Dan Albert7e9d2952016-08-04 13:02:36 -0700614}
615
Colin Cross1e676be2016-10-12 14:38:15 -0700616func (c *Module) DepsMutator(actx android.BottomUpMutatorContext) {
617 if !c.Enabled() {
618 return
619 }
620
Colin Cross37047f12016-12-13 17:06:13 -0800621 ctx := &depsContext{
622 BottomUpMutatorContext: actx,
Dan Albert7e9d2952016-08-04 13:02:36 -0700623 moduleContextImpl: moduleContextImpl{
624 mod: c,
625 },
626 }
627 ctx.ctx = ctx
Colin Crossca860ac2016-01-04 14:34:37 -0800628
Colin Crossc99deeb2016-04-11 15:06:20 -0700629 deps := c.deps(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800630
Colin Crossb5bc4b42016-07-11 16:11:59 -0700631 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.SharedLibs...)
632 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.LateSharedLibs...)
Dan Willemsen72d39932016-07-08 23:23:48 -0700633
Dan Albert914449f2016-06-17 16:45:24 -0700634 variantNdkLibs := []string{}
635 variantLateNdkLibs := []string{}
Dan Willemsend2ede872016-11-18 14:54:24 -0800636 if ctx.sdk() || ctx.vndk() {
Dan Albert914449f2016-06-17 16:45:24 -0700637 version := ctx.sdkVersion()
Dan Willemsen72d39932016-07-08 23:23:48 -0700638
Dan Albert914449f2016-06-17 16:45:24 -0700639 // Rewrites the names of shared libraries into the names of the NDK
640 // libraries where appropriate. This returns two slices.
641 //
642 // The first is a list of non-variant shared libraries (either rewritten
643 // NDK libraries to the modules in prebuilts/ndk, or not rewritten
644 // because they are not NDK libraries).
645 //
646 // The second is a list of ndk_library modules. These need to be
647 // separated because they are a variation dependency and must be added
648 // in a different manner.
649 rewriteNdkLibs := func(list []string) ([]string, []string) {
650 variantLibs := []string{}
651 nonvariantLibs := []string{}
652 for _, entry := range list {
Dan Willemsen72d39932016-07-08 23:23:48 -0700653 if inList(entry, ndkPrebuiltSharedLibraries) {
Dan Albert914449f2016-06-17 16:45:24 -0700654 if !inList(entry, ndkMigratedLibs) {
655 nonvariantLibs = append(nonvariantLibs, entry+".ndk."+version)
656 } else {
657 variantLibs = append(variantLibs, entry+ndkLibrarySuffix)
658 }
659 } else {
660 nonvariantLibs = append(variantLibs, entry)
Dan Willemsen72d39932016-07-08 23:23:48 -0700661 }
662 }
Dan Albert914449f2016-06-17 16:45:24 -0700663 return nonvariantLibs, variantLibs
Dan Willemsen72d39932016-07-08 23:23:48 -0700664 }
665
Dan Albert914449f2016-06-17 16:45:24 -0700666 deps.SharedLibs, variantNdkLibs = rewriteNdkLibs(deps.SharedLibs)
667 deps.LateSharedLibs, variantLateNdkLibs = rewriteNdkLibs(deps.LateSharedLibs)
Dan Willemsen72d39932016-07-08 23:23:48 -0700668 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700669
Colin Cross32ec36c2016-12-15 07:39:51 -0800670 for _, lib := range deps.HeaderLibs {
671 depTag := headerDepTag
672 if inList(lib, deps.ReexportHeaderLibHeaders) {
673 depTag = headerExportDepTag
674 }
675 actx.AddVariationDependencies(nil, depTag, lib)
676 }
Colin Cross5950f382016-12-13 12:50:57 -0800677
Colin Crossc99deeb2016-04-11 15:06:20 -0700678 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, wholeStaticDepTag,
679 deps.WholeStaticLibs...)
680
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700681 for _, lib := range deps.StaticLibs {
682 depTag := staticDepTag
683 if inList(lib, deps.ReexportStaticLibHeaders) {
684 depTag = staticExportDepTag
685 }
Colin Cross15a0d462016-07-14 14:49:58 -0700686 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700687 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700688
689 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, lateStaticDepTag,
690 deps.LateStaticLibs...)
691
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700692 for _, lib := range deps.SharedLibs {
693 depTag := sharedDepTag
694 if inList(lib, deps.ReexportSharedLibHeaders) {
695 depTag = sharedExportDepTag
696 }
Colin Cross15a0d462016-07-14 14:49:58 -0700697 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700698 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700699
700 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, lateSharedDepTag,
701 deps.LateSharedLibs...)
702
Colin Cross68861832016-07-08 10:41:41 -0700703 actx.AddDependency(c, genSourceDepTag, deps.GeneratedSources...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700704
705 for _, gen := range deps.GeneratedHeaders {
706 depTag := genHeaderDepTag
707 if inList(gen, deps.ReexportGeneratedHeaders) {
708 depTag = genHeaderExportDepTag
709 }
710 actx.AddDependency(c, depTag, gen)
711 }
Dan Willemsenb40aab62016-04-20 14:21:14 -0700712
Colin Cross68861832016-07-08 10:41:41 -0700713 actx.AddDependency(c, objDepTag, deps.ObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700714
715 if deps.CrtBegin != "" {
Colin Cross68861832016-07-08 10:41:41 -0700716 actx.AddDependency(c, crtBeginDepTag, deps.CrtBegin)
Colin Crossca860ac2016-01-04 14:34:37 -0800717 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700718 if deps.CrtEnd != "" {
Colin Cross68861832016-07-08 10:41:41 -0700719 actx.AddDependency(c, crtEndDepTag, deps.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700720 }
Dan Albert914449f2016-06-17 16:45:24 -0700721
722 version := ctx.sdkVersion()
723 actx.AddVariationDependencies([]blueprint.Variation{
724 {"ndk_api", version}, {"link", "shared"}}, ndkStubDepTag, variantNdkLibs...)
725 actx.AddVariationDependencies([]blueprint.Variation{
726 {"ndk_api", version}, {"link", "shared"}}, ndkLateStubDepTag, variantLateNdkLibs...)
Colin Cross6362e272015-10-29 15:25:03 -0700727}
Colin Cross21b9a242015-03-24 14:15:58 -0700728
Dan Albert7e9d2952016-08-04 13:02:36 -0700729func beginMutator(ctx android.BottomUpMutatorContext) {
730 if c, ok := ctx.Module().(*Module); ok && c.Enabled() {
731 c.beginMutator(ctx)
732 }
733}
734
Colin Crossca860ac2016-01-04 14:34:37 -0800735func (c *Module) clang(ctx BaseModuleContext) bool {
736 clang := Bool(c.Properties.Clang)
737
738 if c.Properties.Clang == nil {
739 if ctx.Host() {
740 clang = true
741 }
742
743 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
744 clang = true
745 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800746 }
Colin Cross28344522015-04-22 13:07:53 -0700747
Colin Crossca860ac2016-01-04 14:34:37 -0800748 if !c.toolchain(ctx).ClangSupported() {
749 clang = false
750 }
751
752 return clang
753}
754
Colin Crossc99deeb2016-04-11 15:06:20 -0700755// Convert dependencies to paths. Returns a PathDeps containing paths
Colin Cross635c3b02016-05-18 15:37:25 -0700756func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
Colin Crossca860ac2016-01-04 14:34:37 -0800757 var depPaths PathDeps
Colin Crossca860ac2016-01-04 14:34:37 -0800758
Dan Willemsena96ff642016-06-07 12:34:45 -0700759 // Whether a module can link to another module, taking into
760 // account NDK linking.
Dan Albert9e10cd42016-08-03 14:12:14 -0700761 checkLinkType := func(from, to *Module) {
Dan Willemsena96ff642016-06-07 12:34:45 -0700762 if from.Target().Os != android.Android {
763 // Host code is not restricted
Dan Albert9e10cd42016-08-03 14:12:14 -0700764 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700765 }
766 if from.Properties.Sdk_version == "" {
767 // Platform code can link to anything
Dan Albert9e10cd42016-08-03 14:12:14 -0700768 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700769 }
Colin Crossb916a382016-07-29 17:28:03 -0700770 if _, ok := to.linker.(*toolchainLibraryDecorator); ok {
Dan Willemsena96ff642016-06-07 12:34:45 -0700771 // These are always allowed
Dan Albert9e10cd42016-08-03 14:12:14 -0700772 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700773 }
774 if _, ok := to.linker.(*ndkPrebuiltLibraryLinker); ok {
775 // These are allowed, but don't set sdk_version
Dan Albert9e10cd42016-08-03 14:12:14 -0700776 return
Dan Willemsena96ff642016-06-07 12:34:45 -0700777 }
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700778 if _, ok := to.linker.(*ndkPrebuiltStlLinker); ok {
779 // These are allowed, but don't set sdk_version
Dan Albert9e10cd42016-08-03 14:12:14 -0700780 return
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700781 }
Colin Crossb916a382016-07-29 17:28:03 -0700782 if _, ok := to.linker.(*stubDecorator); ok {
Dan Albert914449f2016-06-17 16:45:24 -0700783 // These aren't real libraries, but are the stub shared libraries that are included in
784 // the NDK.
Dan Albert9e10cd42016-08-03 14:12:14 -0700785 return
Dan Albert914449f2016-06-17 16:45:24 -0700786 }
Dan Albert9e10cd42016-08-03 14:12:14 -0700787 if to.Properties.Sdk_version == "" {
788 // NDK code linking to platform code is never okay.
789 ctx.ModuleErrorf("depends on non-NDK-built library %q",
790 ctx.OtherModuleName(to))
791 }
792
793 // All this point we know we have two NDK libraries, but we need to
794 // check that we're not linking against anything built against a higher
795 // API level, as it is only valid to link against older or equivalent
796 // APIs.
797
798 if from.Properties.Sdk_version == "current" {
799 // Current can link against anything.
800 return
801 } else if to.Properties.Sdk_version == "current" {
802 // Current can't be linked against by anything else.
803 ctx.ModuleErrorf("links %q built against newer API version %q",
804 ctx.OtherModuleName(to), "current")
805 }
806
807 fromApi, err := strconv.Atoi(from.Properties.Sdk_version)
808 if err != nil {
809 ctx.PropertyErrorf("sdk_version",
810 "Invalid sdk_version value (must be int): %q",
811 from.Properties.Sdk_version)
812 }
813 toApi, err := strconv.Atoi(to.Properties.Sdk_version)
814 if err != nil {
815 ctx.PropertyErrorf("sdk_version",
816 "Invalid sdk_version value (must be int): %q",
817 to.Properties.Sdk_version)
818 }
819
820 if toApi > fromApi {
821 ctx.ModuleErrorf("links %q built against newer API version %q",
822 ctx.OtherModuleName(to), to.Properties.Sdk_version)
823 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700824 }
825
Colin Crossc99deeb2016-04-11 15:06:20 -0700826 ctx.VisitDirectDeps(func(m blueprint.Module) {
827 name := ctx.OtherModuleName(m)
828 tag := ctx.OtherModuleDependencyTag(m)
Colin Crossca860ac2016-01-04 14:34:37 -0800829
Colin Cross635c3b02016-05-18 15:37:25 -0700830 a, _ := m.(android.Module)
Colin Crossc99deeb2016-04-11 15:06:20 -0700831 if a == nil {
832 ctx.ModuleErrorf("module %q not an android module", name)
833 return
Colin Crossca860ac2016-01-04 14:34:37 -0800834 }
Colin Crossca860ac2016-01-04 14:34:37 -0800835
Dan Willemsena96ff642016-06-07 12:34:45 -0700836 cc, _ := m.(*Module)
837 if cc == nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700838 switch tag {
Colin Cross068e0fe2016-12-13 15:23:47 -0800839 case android.DefaultsDepTag, android.SourceDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700840 case genSourceDepTag:
841 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
842 depPaths.GeneratedSources = append(depPaths.GeneratedSources,
843 genRule.GeneratedSourceFiles()...)
844 } else {
845 ctx.ModuleErrorf("module %q is not a gensrcs or genrule", name)
846 }
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700847 case genHeaderDepTag, genHeaderExportDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700848 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
849 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders,
850 genRule.GeneratedSourceFiles()...)
Colin Cross5ed99c62016-11-22 12:55:55 -0800851 flags := includeDirsToFlags(genRule.GeneratedHeaderDirs())
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700852 depPaths.Flags = append(depPaths.Flags, flags)
853 if tag == genHeaderExportDepTag {
854 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700855 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps,
856 genRule.GeneratedSourceFiles()...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700857 }
Dan Willemsenb40aab62016-04-20 14:21:14 -0700858 } else {
859 ctx.ModuleErrorf("module %q is not a genrule", name)
860 }
861 default:
Colin Crossc99deeb2016-04-11 15:06:20 -0700862 ctx.ModuleErrorf("depends on non-cc module %q", name)
Colin Crossca860ac2016-01-04 14:34:37 -0800863 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700864 return
865 }
866
867 if !a.Enabled() {
Colin Crossa8f5e9a2016-12-13 12:51:11 -0800868 if ctx.AConfig().AllowMissingDependencies() {
869 ctx.AddMissingDependencies([]string{name})
870 } else {
871 ctx.ModuleErrorf("depends on disabled module %q", name)
872 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700873 return
874 }
875
Colin Crossa1ad8d12016-06-01 17:09:44 -0700876 if a.Target().Os != ctx.Os() {
877 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), name)
878 return
879 }
880
881 if a.Target().Arch.ArchType != ctx.Arch().ArchType {
882 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700883 return
884 }
885
Colin Crossc99deeb2016-04-11 15:06:20 -0700886 if tag == reuseObjTag {
Colin Crossbba99042016-11-23 15:45:05 -0800887 if l, ok := cc.compiler.(libraryInterface); ok {
888 depPaths.Objs = depPaths.Objs.Append(l.reuseObjs())
889 return
890 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700891 }
892
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700893 if t, ok := tag.(dependencyTag); ok && t.library {
Dan Willemsena96ff642016-06-07 12:34:45 -0700894 if i, ok := cc.linker.(exportedFlagsProducer); ok {
Dan Willemsen76f08272016-07-09 00:14:08 -0700895 flags := i.exportedFlags()
Dan Willemsen847dcc72016-09-29 12:13:36 -0700896 deps := i.exportedFlagsDeps()
Dan Willemsen76f08272016-07-09 00:14:08 -0700897 depPaths.Flags = append(depPaths.Flags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700898 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders, deps...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700899
900 if t.reexportFlags {
Dan Willemsen76f08272016-07-09 00:14:08 -0700901 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700902 depPaths.ReexportedFlagsDeps = append(depPaths.ReexportedFlagsDeps, deps...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700903 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700904 }
Dan Willemsena96ff642016-06-07 12:34:45 -0700905
Dan Albert9e10cd42016-08-03 14:12:14 -0700906 checkLinkType(c, cc)
Colin Crossc99deeb2016-04-11 15:06:20 -0700907 }
908
Colin Cross26c34ed2016-09-30 17:10:16 -0700909 var ptr *android.Paths
Colin Cross635c3b02016-05-18 15:37:25 -0700910 var depPtr *android.Paths
Colin Crossc99deeb2016-04-11 15:06:20 -0700911
Colin Cross26c34ed2016-09-30 17:10:16 -0700912 linkFile := cc.outputFile
913 depFile := android.OptionalPath{}
914
Colin Crossc99deeb2016-04-11 15:06:20 -0700915 switch tag {
Dan Albert914449f2016-06-17 16:45:24 -0700916 case ndkStubDepTag, sharedDepTag, sharedExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700917 ptr = &depPaths.SharedLibs
918 depPtr = &depPaths.SharedLibsDeps
919 depFile = cc.linker.(libraryInterface).toc()
Dan Albert914449f2016-06-17 16:45:24 -0700920 case lateSharedDepTag, ndkLateStubDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700921 ptr = &depPaths.LateSharedLibs
922 depPtr = &depPaths.LateSharedLibsDeps
923 depFile = cc.linker.(libraryInterface).toc()
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700924 case staticDepTag, staticExportDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700925 ptr = &depPaths.StaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -0700926 case lateStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700927 ptr = &depPaths.LateStaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -0700928 case wholeStaticDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700929 ptr = &depPaths.WholeStaticLibs
Colin Crossb916a382016-07-29 17:28:03 -0700930 staticLib, ok := cc.linker.(libraryInterface)
931 if !ok || !staticLib.static() {
Dan Willemsena96ff642016-06-07 12:34:45 -0700932 ctx.ModuleErrorf("module %q not a static library", name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700933 return
934 }
935
936 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
937 postfix := " (required by " + ctx.OtherModuleName(m) + ")"
938 for i := range missingDeps {
939 missingDeps[i] += postfix
940 }
941 ctx.AddMissingDependencies(missingDeps)
942 }
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700943 depPaths.WholeStaticLibObjs = depPaths.WholeStaticLibObjs.Append(staticLib.objs())
Colin Cross5950f382016-12-13 12:50:57 -0800944 case headerDepTag:
945 // Nothing
Colin Crossc99deeb2016-04-11 15:06:20 -0700946 case objDepTag:
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700947 depPaths.Objs.objFiles = append(depPaths.Objs.objFiles, linkFile.Path())
Colin Crossc99deeb2016-04-11 15:06:20 -0700948 case crtBeginDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700949 depPaths.CrtBegin = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -0700950 case crtEndDepTag:
Colin Cross26c34ed2016-09-30 17:10:16 -0700951 depPaths.CrtEnd = linkFile
Colin Crossc99deeb2016-04-11 15:06:20 -0700952 }
953
Colin Cross26c34ed2016-09-30 17:10:16 -0700954 if ptr != nil {
Colin Crossce75d2c2016-10-06 16:12:58 -0700955 if !linkFile.Valid() {
956 ctx.ModuleErrorf("module %q missing output file", name)
957 return
958 }
Colin Cross26c34ed2016-09-30 17:10:16 -0700959 *ptr = append(*ptr, linkFile.Path())
960 }
961
Colin Crossc99deeb2016-04-11 15:06:20 -0700962 if depPtr != nil {
Colin Cross26c34ed2016-09-30 17:10:16 -0700963 dep := depFile
964 if !dep.Valid() {
965 dep = linkFile
966 }
967 *depPtr = append(*depPtr, dep.Path())
Colin Crossca860ac2016-01-04 14:34:37 -0800968 }
969 })
970
971 return depPaths
972}
973
974func (c *Module) InstallInData() bool {
975 if c.installer == nil {
976 return false
977 }
Colin Cross94610402016-08-29 13:41:32 -0700978 if c.sanitize != nil && c.sanitize.inData() {
979 return true
980 }
Colin Crossca860ac2016-01-04 14:34:37 -0800981 return c.installer.inData()
982}
983
Dan Willemsen4aa75ca2016-09-28 16:18:03 -0700984func (c *Module) HostToolPath() android.OptionalPath {
985 if c.installer == nil {
986 return android.OptionalPath{}
987 }
988 return c.installer.hostToolPath()
989}
990
Colin Cross2ba19d92015-05-07 15:44:20 -0700991//
Colin Crosscfad1192015-11-02 16:43:11 -0800992// Defaults
993//
Colin Crossca860ac2016-01-04 14:34:37 -0800994type Defaults struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700995 android.ModuleBase
996 android.DefaultsModule
Colin Crosscfad1192015-11-02 16:43:11 -0800997}
998
Colin Cross635c3b02016-05-18 15:37:25 -0700999func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -08001000}
1001
Colin Cross1e676be2016-10-12 14:38:15 -07001002func (d *Defaults) DepsMutator(ctx android.BottomUpMutatorContext) {
1003}
1004
Colin Crossca860ac2016-01-04 14:34:37 -08001005func defaultsFactory() (blueprint.Module, []interface{}) {
Colin Crosse1d764e2016-08-18 14:18:32 -07001006 return DefaultsFactory()
1007}
1008
1009func DefaultsFactory(props ...interface{}) (blueprint.Module, []interface{}) {
Colin Crossca860ac2016-01-04 14:34:37 -08001010 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -08001011
Colin Crosse1d764e2016-08-18 14:18:32 -07001012 props = append(props,
Colin Crossca860ac2016-01-04 14:34:37 -08001013 &BaseProperties{},
1014 &BaseCompilerProperties{},
1015 &BaseLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -07001016 &LibraryProperties{},
Colin Cross919281a2016-04-05 16:42:05 -07001017 &FlagExporterProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001018 &BinaryLinkerProperties{},
Colin Crossb916a382016-07-29 17:28:03 -07001019 &TestProperties{},
1020 &TestBinaryProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08001021 &UnusedProperties{},
1022 &StlProperties{},
Colin Cross16b23492016-01-06 14:41:07 -08001023 &SanitizeProperties{},
Colin Cross665dce92016-04-28 14:50:03 -07001024 &StripProperties{},
Dan Willemsen7424d612016-09-01 13:45:39 -07001025 &InstallerProperties{},
Dan Willemsena03cf6d2016-09-26 15:45:04 -07001026 &TidyProperties{},
Colin Crosse1d764e2016-08-18 14:18:32 -07001027 )
Colin Crosscfad1192015-11-02 16:43:11 -08001028
Colin Crosse1d764e2016-08-18 14:18:32 -07001029 return android.InitDefaultsModule(module, module, props...)
Colin Crosscfad1192015-11-02 16:43:11 -08001030}
1031
Colin Cross74d1ec02015-04-28 13:30:13 -07001032// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
1033// modifies the slice contents in place, and returns a subslice of the original slice
1034func lastUniqueElements(list []string) []string {
1035 totalSkip := 0
1036 for i := len(list) - 1; i >= totalSkip; i-- {
1037 skip := 0
1038 for j := i - 1; j >= totalSkip; j-- {
1039 if list[i] == list[j] {
1040 skip++
1041 } else {
1042 list[j+skip] = list[j]
1043 }
1044 }
1045 totalSkip += skip
1046 }
1047 return list[totalSkip:]
1048}
Colin Cross06a931b2015-10-28 17:23:31 -07001049
1050var Bool = proptools.Bool