blob: 1434f2cca253259108bc0b343f7d5e5eaf78dc8e [file] [log] [blame]
Colin Cross4d9c2d12016-07-29 12:48:20 -07001// Copyright 2016 Google Inc. All rights reserved.
2//
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
17import (
18 "strings"
19
20 "github.com/google/blueprint"
Colin Cross26c34ed2016-09-30 17:10:16 -070021 "github.com/google/blueprint/pathtools"
Colin Cross4d9c2d12016-07-29 12:48:20 -070022
Colin Cross4d9c2d12016-07-29 12:48:20 -070023 "android/soong/android"
24)
25
Colin Crossb916a382016-07-29 17:28:03 -070026type LibraryProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -070027 Static struct {
Colin Cross2f336352016-10-26 10:03:47 -070028 Srcs []string `android:"arch_variant"`
29 Cflags []string `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070030
Colin Cross4d9c2d12016-07-29 12:48:20 -070031 Enabled *bool `android:"arch_variant"`
32 Whole_static_libs []string `android:"arch_variant"`
33 Static_libs []string `android:"arch_variant"`
34 Shared_libs []string `android:"arch_variant"`
35 } `android:"arch_variant"`
36 Shared struct {
Colin Cross2f336352016-10-26 10:03:47 -070037 Srcs []string `android:"arch_variant"`
38 Cflags []string `android:"arch_variant"`
Colin Crossb916a382016-07-29 17:28:03 -070039
Colin Cross4d9c2d12016-07-29 12:48:20 -070040 Enabled *bool `android:"arch_variant"`
41 Whole_static_libs []string `android:"arch_variant"`
42 Static_libs []string `android:"arch_variant"`
43 Shared_libs []string `android:"arch_variant"`
44 } `android:"arch_variant"`
45
46 // local file name to pass to the linker as --version_script
47 Version_script *string `android:"arch_variant"`
48 // local file name to pass to the linker as -unexported_symbols_list
49 Unexported_symbols_list *string `android:"arch_variant"`
50 // local file name to pass to the linker as -force_symbols_not_weak_list
51 Force_symbols_not_weak_list *string `android:"arch_variant"`
52 // local file name to pass to the linker as -force_symbols_weak_list
53 Force_symbols_weak_list *string `android:"arch_variant"`
54
55 // rename host libraries to prevent overlap with system installed libraries
56 Unique_host_soname *bool
57
Dan Willemsene1240db2016-11-03 14:28:51 -070058 Aidl struct {
59 // export headers generated from .aidl sources
60 Export_aidl_headers bool
61 }
62
Colin Cross0c461f12016-10-20 16:11:43 -070063 Proto struct {
64 // export headers generated from .proto sources
65 Export_proto_headers bool
66 }
Jiyong Park52d25bd2017-10-13 09:17:01 +090067 Target struct {
68 Vendor struct {
69 // version script for this vendor variant
70 Version_script *string `android:"arch_variant"`
71 }
72 }
Colin Crossa48ab5b2017-02-14 15:28:44 -080073}
Colin Cross0c461f12016-10-20 16:11:43 -070074
Colin Crossa48ab5b2017-02-14 15:28:44 -080075type LibraryMutatedProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -070076 VariantName string `blueprint:"mutated"`
Colin Crossb916a382016-07-29 17:28:03 -070077
78 // Build a static variant
79 BuildStatic bool `blueprint:"mutated"`
80 // Build a shared variant
81 BuildShared bool `blueprint:"mutated"`
82 // This variant is shared
83 VariantIsShared bool `blueprint:"mutated"`
84 // This variant is static
85 VariantIsStatic bool `blueprint:"mutated"`
86}
87
88type FlagExporterProperties struct {
89 // list of directories relative to the Blueprints file that will
Dan Willemsen273af7f2016-11-03 15:53:42 -070090 // be added to the include path (using -I) for this module and any module that links
Colin Cross5d195602017-10-17 16:15:50 -070091 // against this module. Directories listed in export_include_dirs do not need to be
92 // listed in local_include_dirs.
Colin Crossb916a382016-07-29 17:28:03 -070093 Export_include_dirs []string `android:"arch_variant"`
Dan Willemsen4416e5d2017-04-06 12:43:22 -070094
95 Target struct {
96 Vendor struct {
97 // list of exported include directories, like
98 // export_include_dirs, that will be applied to the
99 // vendor variant of this library. This will overwrite
100 // any other declarations.
101 Export_include_dirs []string
102 }
103 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700104}
105
106func init() {
Colin Cross798bfce2016-10-12 14:28:16 -0700107 android.RegisterModuleType("cc_library_static", libraryStaticFactory)
108 android.RegisterModuleType("cc_library_shared", librarySharedFactory)
109 android.RegisterModuleType("cc_library", libraryFactory)
110 android.RegisterModuleType("cc_library_host_static", libraryHostStaticFactory)
111 android.RegisterModuleType("cc_library_host_shared", libraryHostSharedFactory)
Colin Cross5950f382016-12-13 12:50:57 -0800112 android.RegisterModuleType("cc_library_headers", libraryHeaderFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700113}
114
115// Module factory for combined static + shared libraries, device by default but with possible host
116// support
Colin Cross36242852017-06-23 15:06:31 -0700117func libraryFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800118 module, _ := NewLibrary(android.HostAndDeviceSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700119 return module.Init()
120}
121
122// Module factory for static libraries
Colin Cross36242852017-06-23 15:06:31 -0700123func libraryStaticFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800124 module, library := NewLibrary(android.HostAndDeviceSupported)
125 library.BuildOnlyStatic()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700126 return module.Init()
127}
128
129// Module factory for shared libraries
Colin Cross36242852017-06-23 15:06:31 -0700130func librarySharedFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800131 module, library := NewLibrary(android.HostAndDeviceSupported)
132 library.BuildOnlyShared()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700133 return module.Init()
134}
135
136// Module factory for host static libraries
Colin Cross36242852017-06-23 15:06:31 -0700137func libraryHostStaticFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800138 module, library := NewLibrary(android.HostSupported)
139 library.BuildOnlyStatic()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700140 return module.Init()
141}
142
143// Module factory for host shared libraries
Colin Cross36242852017-06-23 15:06:31 -0700144func libraryHostSharedFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800145 module, library := NewLibrary(android.HostSupported)
146 library.BuildOnlyShared()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700147 return module.Init()
148}
149
Colin Cross5950f382016-12-13 12:50:57 -0800150// Module factory for header-only libraries
Colin Cross36242852017-06-23 15:06:31 -0700151func libraryHeaderFactory() android.Module {
Colin Cross5950f382016-12-13 12:50:57 -0800152 module, library := NewLibrary(android.HostAndDeviceSupported)
153 library.HeaderOnly()
154 return module.Init()
155}
156
Colin Cross4d9c2d12016-07-29 12:48:20 -0700157type flagExporter struct {
158 Properties FlagExporterProperties
159
Dan Willemsen847dcc72016-09-29 12:13:36 -0700160 flags []string
161 flagsDeps android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700162}
163
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700164func (f *flagExporter) exportedIncludes(ctx ModuleContext) android.Paths {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700165 if ctx.useVndk() && f.Properties.Target.Vendor.Export_include_dirs != nil {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700166 return android.PathsForModuleSrc(ctx, f.Properties.Target.Vendor.Export_include_dirs)
167 } else {
168 return android.PathsForModuleSrc(ctx, f.Properties.Export_include_dirs)
169 }
170}
171
Colin Cross4d9c2d12016-07-29 12:48:20 -0700172func (f *flagExporter) exportIncludes(ctx ModuleContext, inc string) {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700173 includeDirs := f.exportedIncludes(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700174 for _, dir := range includeDirs.Strings() {
175 f.flags = append(f.flags, inc+dir)
176 }
177}
178
179func (f *flagExporter) reexportFlags(flags []string) {
180 f.flags = append(f.flags, flags...)
181}
182
Dan Willemsen847dcc72016-09-29 12:13:36 -0700183func (f *flagExporter) reexportDeps(deps android.Paths) {
184 f.flagsDeps = append(f.flagsDeps, deps...)
185}
186
Colin Cross4d9c2d12016-07-29 12:48:20 -0700187func (f *flagExporter) exportedFlags() []string {
188 return f.flags
189}
190
Dan Willemsen847dcc72016-09-29 12:13:36 -0700191func (f *flagExporter) exportedFlagsDeps() android.Paths {
192 return f.flagsDeps
193}
194
Colin Cross4d9c2d12016-07-29 12:48:20 -0700195type exportedFlagsProducer interface {
196 exportedFlags() []string
Dan Willemsen847dcc72016-09-29 12:13:36 -0700197 exportedFlagsDeps() android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700198}
199
200var _ exportedFlagsProducer = (*flagExporter)(nil)
201
Colin Crossb916a382016-07-29 17:28:03 -0700202// libraryDecorator wraps baseCompiler, baseLinker and baseInstaller to provide library-specific
203// functionality: static vs. shared linkage, reusing object files for shared libraries
204type libraryDecorator struct {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800205 Properties LibraryProperties
206 MutatedProperties LibraryMutatedProperties
Colin Cross4d9c2d12016-07-29 12:48:20 -0700207
208 // For reusing static library objects for shared library
Colin Cross10d22312017-05-03 11:01:58 -0700209 reuseObjects Objects
210 reuseExportedFlags []string
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700211 reuseExportedDeps android.Paths
Colin Cross10d22312017-05-03 11:01:58 -0700212
Colin Cross26c34ed2016-09-30 17:10:16 -0700213 // table-of-contents file to optimize out relinking when possible
214 tocFile android.OptionalPath
Colin Cross4d9c2d12016-07-29 12:48:20 -0700215
Colin Cross4d9c2d12016-07-29 12:48:20 -0700216 flagExporter
217 stripper
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700218 relocationPacker
Colin Cross4d9c2d12016-07-29 12:48:20 -0700219
Colin Cross4d9c2d12016-07-29 12:48:20 -0700220 // If we're used as a whole_static_lib, our missing dependencies need
221 // to be given
222 wholeStaticMissingDeps []string
223
224 // For whole_static_libs
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700225 objects Objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700226
227 // Uses the module's name if empty, but can be overridden. Does not include
228 // shlib suffix.
229 libName string
Colin Crossb916a382016-07-29 17:28:03 -0700230
231 sanitize *sanitize
232
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800233 sabi *sabi
234
Dan Willemsen581341d2017-02-09 16:16:31 -0800235 // Output archive of gcno coverage information files
236 coverageOutputFile android.OptionalPath
237
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800238 // linked Source Abi Dump
239 sAbiOutputFile android.OptionalPath
240
241 // Source Abi Diff
242 sAbiDiff android.OptionalPath
243
Colin Crossb916a382016-07-29 17:28:03 -0700244 // Decorated interafaces
245 *baseCompiler
246 *baseLinker
247 *baseInstaller
Colin Cross4d9c2d12016-07-29 12:48:20 -0700248}
249
Colin Crossb916a382016-07-29 17:28:03 -0700250func (library *libraryDecorator) linkerProps() []interface{} {
251 var props []interface{}
252 props = append(props, library.baseLinker.linkerProps()...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700253 return append(props,
254 &library.Properties,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800255 &library.MutatedProperties,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700256 &library.flagExporter.Properties,
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700257 &library.stripper.StripProperties,
258 &library.relocationPacker.Properties)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700259}
260
Colin Crossb916a382016-07-29 17:28:03 -0700261func (library *libraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross42742b82016-08-01 13:20:05 -0700262 flags = library.baseLinker.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700263
Colin Crossb916a382016-07-29 17:28:03 -0700264 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
265 // all code is position independent, and then those warnings get promoted to
266 // errors.
Colin Cross3edeee12017-04-04 12:59:48 -0700267 if !ctx.Windows() {
Colin Crossb916a382016-07-29 17:28:03 -0700268 flags.CFlags = append(flags.CFlags, "-fPIC")
269 }
270
271 if library.static() {
272 flags.CFlags = append(flags.CFlags, library.Properties.Static.Cflags...)
Colin Crossa48ab5b2017-02-14 15:28:44 -0800273 } else if library.shared() {
Colin Crossb916a382016-07-29 17:28:03 -0700274 flags.CFlags = append(flags.CFlags, library.Properties.Shared.Cflags...)
275 }
276
Colin Crossa48ab5b2017-02-14 15:28:44 -0800277 if library.shared() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700278 libName := library.getLibName(ctx)
279 // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead
280 sharedFlag := "-Wl,-shared"
281 if flags.Clang || ctx.Host() {
282 sharedFlag = "-shared"
283 }
284 var f []string
Dan Willemsen01a405a2016-06-13 17:19:03 -0700285 if ctx.toolchain().Bionic() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700286 f = append(f,
287 "-nostdlib",
288 "-Wl,--gc-sections",
289 )
290 }
291
292 if ctx.Darwin() {
293 f = append(f,
294 "-dynamiclib",
295 "-single_module",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700296 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
297 )
Colin Cross7863cf52016-10-20 10:47:21 -0700298 if ctx.Arch().ArchType == android.X86 {
299 f = append(f,
300 "-read_only_relocs suppress",
301 )
302 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700303 } else {
304 f = append(f,
305 sharedFlag,
306 "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix())
307 }
308
309 flags.LdFlags = append(f, flags.LdFlags...)
310 }
311
312 return flags
313}
314
Dan Willemsen273af7f2016-11-03 15:53:42 -0700315func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700316 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700317 if len(exportIncludeDirs) > 0 {
Colin Crossdad8c952017-04-26 14:55:27 -0700318 f := includeDirsToFlags(exportIncludeDirs)
319 flags.GlobalFlags = append(flags.GlobalFlags, f)
320 flags.YasmFlags = append(flags.YasmFlags, f)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700321 }
322
323 return library.baseCompiler.compilerFlags(ctx, flags)
324}
325
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700326func extractExportIncludesFromFlags(flags []string) []string {
327 // This method is used in the generation of rules which produce
328 // abi-dumps for source files. Exported headers are needed to infer the
329 // abi exported by a library and filter out the rest of the abi dumped
330 // from a source. We extract the include flags exported by a library.
331 // This includes the flags exported which are re-exported from static
332 // library dependencies, exported header library dependencies and
Jayant Chowdharyaf6eb712017-08-23 16:08:29 -0700333 // generated header dependencies. -isystem headers are not included
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700334 // since for bionic libraries, abi-filtering is taken care of by version
335 // scripts.
336 var exportedIncludes []string
337 for _, flag := range flags {
338 if strings.HasPrefix(flag, "-I") {
339 exportedIncludes = append(exportedIncludes, flag)
340 }
341 }
342 return exportedIncludes
343}
344
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700345func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Colin Cross5950f382016-12-13 12:50:57 -0800346 if !library.buildShared() && !library.buildStatic() {
347 if len(library.baseCompiler.Properties.Srcs) > 0 {
348 ctx.PropertyErrorf("srcs", "cc_library_headers must not have any srcs")
349 }
350 if len(library.Properties.Static.Srcs) > 0 {
351 ctx.PropertyErrorf("static.srcs", "cc_library_headers must not have any srcs")
352 }
353 if len(library.Properties.Shared.Srcs) > 0 {
354 ctx.PropertyErrorf("shared.srcs", "cc_library_headers must not have any srcs")
355 }
356 return Objects{}
357 }
Jayant Chowdhary2a966402017-08-07 14:09:45 -0700358 if ctx.createVndkSourceAbiDump() || library.sabi.Properties.CreateSAbiDumps {
Jayant Chowdharya4fce192017-09-06 13:10:03 -0700359 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800360 var SourceAbiFlags []string
361 for _, dir := range exportIncludeDirs.Strings() {
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700362 SourceAbiFlags = append(SourceAbiFlags, "-I"+dir)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800363 }
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700364 for _, reexportedInclude := range extractExportIncludesFromFlags(library.sabi.Properties.ReexportedIncludeFlags) {
365 SourceAbiFlags = append(SourceAbiFlags, reexportedInclude)
366 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800367 flags.SAbiFlags = SourceAbiFlags
368 total_length := len(library.baseCompiler.Properties.Srcs) + len(deps.GeneratedSources) + len(library.Properties.Shared.Srcs) +
369 len(library.Properties.Static.Srcs)
370 if total_length > 0 {
371 flags.SAbiDump = true
372 }
373 }
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700374 objs := library.baseCompiler.compile(ctx, flags, deps)
375 library.reuseObjects = objs
Colin Cross2f336352016-10-26 10:03:47 -0700376 buildFlags := flagsToBuilderFlags(flags)
Colin Crossb916a382016-07-29 17:28:03 -0700377
Colin Cross4d9c2d12016-07-29 12:48:20 -0700378 if library.static() {
Colin Cross2f336352016-10-26 10:03:47 -0700379 srcs := android.PathsForModuleSrc(ctx, library.Properties.Static.Srcs)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700380 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceStaticLibrary,
381 srcs, library.baseCompiler.deps))
Colin Crossa48ab5b2017-02-14 15:28:44 -0800382 } else if library.shared() {
Colin Cross2f336352016-10-26 10:03:47 -0700383 srcs := android.PathsForModuleSrc(ctx, library.Properties.Shared.Srcs)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700384 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceSharedLibrary,
385 srcs, library.baseCompiler.deps))
Colin Crossb916a382016-07-29 17:28:03 -0700386 }
387
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700388 return objs
Colin Crossb916a382016-07-29 17:28:03 -0700389}
390
391type libraryInterface interface {
392 getWholeStaticMissingDeps() []string
393 static() bool
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700394 objs() Objects
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700395 reuseObjs() (Objects, []string, android.Paths)
Colin Cross26c34ed2016-09-30 17:10:16 -0700396 toc() android.OptionalPath
Colin Crossb916a382016-07-29 17:28:03 -0700397
398 // Returns true if the build options for the module have selected a static or shared build
399 buildStatic() bool
400 buildShared() bool
401
402 // Sets whether a specific variant is static or shared
Colin Crossa48ab5b2017-02-14 15:28:44 -0800403 setStatic()
404 setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700405}
406
407func (library *libraryDecorator) getLibName(ctx ModuleContext) string {
408 name := library.libName
409 if name == "" {
Colin Crossce75d2c2016-10-06 16:12:58 -0700410 name = ctx.baseModuleName()
Colin Crossb916a382016-07-29 17:28:03 -0700411 }
412
413 if ctx.Host() && Bool(library.Properties.Unique_host_soname) {
414 if !strings.HasSuffix(name, "-host") {
415 name = name + "-host"
416 }
417 }
418
Colin Crossa48ab5b2017-02-14 15:28:44 -0800419 return name + library.MutatedProperties.VariantName
Colin Crossb916a382016-07-29 17:28:03 -0700420}
421
422func (library *libraryDecorator) linkerInit(ctx BaseModuleContext) {
423 location := InstallInSystem
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700424 if library.sanitize.inSanitizerDir() {
425 location = InstallInSanitizerDir
Colin Crossb916a382016-07-29 17:28:03 -0700426 }
427 library.baseInstaller.location = location
428
429 library.baseLinker.linkerInit(ctx)
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700430
431 library.relocationPacker.packingInit(ctx)
Colin Crossb916a382016-07-29 17:28:03 -0700432}
433
Colin Cross37047f12016-12-13 17:06:13 -0800434func (library *libraryDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700435 deps = library.baseLinker.linkerDeps(ctx, deps)
436
437 if library.static() {
438 deps.WholeStaticLibs = append(deps.WholeStaticLibs,
439 library.Properties.Static.Whole_static_libs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700440 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Static.Static_libs...)
441 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Static.Shared_libs...)
Colin Crossa48ab5b2017-02-14 15:28:44 -0800442 } else if library.shared() {
Dan Willemsen2e47b342016-11-17 01:02:25 -0800443 if ctx.toolchain().Bionic() && !Bool(library.baseLinker.Properties.Nocrt) {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700444 if !ctx.useSdk() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700445 deps.CrtBegin = "crtbegin_so"
446 deps.CrtEnd = "crtend_so"
447 } else {
Dan Albertebedf672016-11-08 15:06:22 -0800448 // TODO(danalbert): Add generation of crt objects.
449 // For `sdk_version: "current"`, we don't actually have a
450 // freshly generated set of CRT objects. Use the last stable
451 // version.
452 version := ctx.sdkVersion()
453 if version == "current" {
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -0700454 version = getCurrentNdkPrebuiltVersion(ctx)
Dan Albertebedf672016-11-08 15:06:22 -0800455 }
456 deps.CrtBegin = "ndk_crtbegin_so." + version
457 deps.CrtEnd = "ndk_crtend_so." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700458 }
459 }
460 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Shared.Whole_static_libs...)
461 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Shared.Static_libs...)
462 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Shared.Shared_libs...)
463 }
Jiyong Park52d25bd2017-10-13 09:17:01 +0900464 if ctx.useVndk() {
465 deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs)
466 deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Vendor.Exclude_shared_libs)
467 deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs)
468 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700469 return deps
470}
471
Colin Crossb916a382016-07-29 17:28:03 -0700472func (library *libraryDecorator) linkStatic(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700473 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700474
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700475 library.objects = deps.WholeStaticLibObjs.Copy()
476 library.objects = library.objects.Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700477
478 outputFile := android.PathForModuleOut(ctx,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800479 ctx.ModuleName()+library.MutatedProperties.VariantName+staticLibraryExtension)
Dan Willemsen581341d2017-02-09 16:16:31 -0800480 builderFlags := flagsToBuilderFlags(flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700481
Dan Willemsen581341d2017-02-09 16:16:31 -0800482 TransformObjToStaticLib(ctx, library.objects.objFiles, builderFlags, outputFile, objs.tidyFiles)
483
484 library.coverageOutputFile = TransformCoverageFilesToLib(ctx, library.objects, builderFlags,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800485 ctx.ModuleName()+library.MutatedProperties.VariantName)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700486
487 library.wholeStaticMissingDeps = ctx.GetMissingDependencies()
488
489 ctx.CheckbuildFile(outputFile)
490
491 return outputFile
492}
493
Colin Crossb916a382016-07-29 17:28:03 -0700494func (library *libraryDecorator) linkShared(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700495 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700496
497 var linkerDeps android.Paths
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700498 linkerDeps = append(linkerDeps, flags.LdFlagsDeps...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700499
500 versionScript := android.OptionalPathForModuleSrc(ctx, library.Properties.Version_script)
501 unexportedSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Unexported_symbols_list)
502 forceNotWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_not_weak_list)
503 forceWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_weak_list)
Jiyong Park52d25bd2017-10-13 09:17:01 +0900504 if ctx.useVndk() && library.Properties.Target.Vendor.Version_script != nil {
505 versionScript = android.OptionalPathForModuleSrc(ctx, library.Properties.Target.Vendor.Version_script)
506 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700507 if !ctx.Darwin() {
508 if versionScript.Valid() {
509 flags.LdFlags = append(flags.LdFlags, "-Wl,--version-script,"+versionScript.String())
510 linkerDeps = append(linkerDeps, versionScript.Path())
511 }
512 if unexportedSymbols.Valid() {
513 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
514 }
515 if forceNotWeakSymbols.Valid() {
516 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
517 }
518 if forceWeakSymbols.Valid() {
519 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
520 }
521 } else {
522 if versionScript.Valid() {
523 ctx.PropertyErrorf("version_script", "Not supported on Darwin")
524 }
525 if unexportedSymbols.Valid() {
526 flags.LdFlags = append(flags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
527 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
528 }
529 if forceNotWeakSymbols.Valid() {
530 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
531 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
532 }
533 if forceWeakSymbols.Valid() {
534 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
535 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
536 }
537 }
538
539 fileName := library.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
540 outputFile := android.PathForModuleOut(ctx, fileName)
541 ret := outputFile
542
543 builderFlags := flagsToBuilderFlags(flags)
544
Colin Crossd8f8d072017-04-04 13:00:15 -0700545 if !ctx.Darwin() && !ctx.Windows() {
Colin Cross89562dc2016-10-03 17:47:19 -0700546 // Optimize out relinking against shared libraries whose interface hasn't changed by
547 // depending on a table of contents file instead of the library itself.
548 tocPath := outputFile.RelPathString()
549 tocPath = pathtools.ReplaceExtension(tocPath, flags.Toolchain.ShlibSuffix()[1:]+".toc")
550 tocFile := android.PathForOutput(ctx, tocPath)
551 library.tocFile = android.OptionalPathForPath(tocFile)
552 TransformSharedObjectToToc(ctx, outputFile, tocFile, builderFlags)
553 }
554
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700555 if library.relocationPacker.needsPacking(ctx) {
556 packedOutputFile := outputFile
557 outputFile = android.PathForModuleOut(ctx, "unpacked", fileName)
558 library.relocationPacker.pack(ctx, outputFile, packedOutputFile, builderFlags)
559 }
560
Colin Cross4d9c2d12016-07-29 12:48:20 -0700561 if library.stripper.needsStrip(ctx) {
562 strippedOutputFile := outputFile
563 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
564 library.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
565 }
566
567 sharedLibs := deps.SharedLibs
568 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
569
Dan Albertd015c4a2016-08-10 14:34:08 -0700570 // TODO(danalbert): Clean this up when soong supports prebuilts.
571 if strings.HasPrefix(ctx.selectedStl(), "ndk_libc++") {
572 libDir := getNdkStlLibDir(ctx, flags.Toolchain, "libc++")
573
574 if strings.HasSuffix(ctx.selectedStl(), "_shared") {
575 deps.StaticLibs = append(deps.StaticLibs,
576 libDir.Join(ctx, "libandroid_support.a"))
577 } else {
578 deps.StaticLibs = append(deps.StaticLibs,
579 libDir.Join(ctx, "libc++abi.a"),
580 libDir.Join(ctx, "libandroid_support.a"))
581 }
582
583 if ctx.Arch().ArchType == android.Arm {
584 deps.StaticLibs = append(deps.StaticLibs,
585 libDir.Join(ctx, "libunwind.a"))
586 }
587 }
588
Colin Cross26c34ed2016-09-30 17:10:16 -0700589 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
590 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700591 linkerDeps = append(linkerDeps, objs.tidyFiles...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700592
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700593 TransformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700594 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
595 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile)
596
Dan Willemsen581341d2017-02-09 16:16:31 -0800597 objs.coverageFiles = append(objs.coverageFiles, deps.StaticLibObjs.coverageFiles...)
598 objs.coverageFiles = append(objs.coverageFiles, deps.WholeStaticLibObjs.coverageFiles...)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800599
600 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.StaticLibObjs.sAbiDumpFiles...)
601 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.WholeStaticLibObjs.sAbiDumpFiles...)
602
Dan Willemsen581341d2017-02-09 16:16:31 -0800603 library.coverageOutputFile = TransformCoverageFilesToLib(ctx, objs, builderFlags, library.getLibName(ctx))
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700604 library.linkSAbiDumpFiles(ctx, objs, fileName, ret)
Dan Willemsen581341d2017-02-09 16:16:31 -0800605
Colin Cross4d9c2d12016-07-29 12:48:20 -0700606 return ret
607}
608
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700609func (library *libraryDecorator) linkSAbiDumpFiles(ctx ModuleContext, objs Objects, fileName string, soFile android.Path) {
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800610 //Also take into account object re-use.
Justin Yun8effde42017-06-23 19:24:43 +0900611 if len(objs.sAbiDumpFiles) > 0 && ctx.createVndkSourceAbiDump() {
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800612 refSourceDumpFile := android.PathForVndkRefAbiDump(ctx, "current", fileName, vndkVsNdk(ctx), true)
613 versionScript := android.OptionalPathForModuleSrc(ctx, library.Properties.Version_script)
614 var symbolFile android.OptionalPath
615 if versionScript.Valid() {
616 symbolFile = versionScript
617 }
Jayant Chowdharya4fce192017-09-06 13:10:03 -0700618 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800619 var SourceAbiFlags []string
620 for _, dir := range exportIncludeDirs.Strings() {
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700621 SourceAbiFlags = append(SourceAbiFlags, "-I"+dir)
622 }
623 for _, reexportedInclude := range extractExportIncludesFromFlags(library.sabi.Properties.ReexportedIncludeFlags) {
624 SourceAbiFlags = append(SourceAbiFlags, reexportedInclude)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800625 }
626 exportedHeaderFlags := strings.Join(SourceAbiFlags, " ")
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700627 library.sAbiOutputFile = TransformDumpToLinkedDump(ctx, objs.sAbiDumpFiles, soFile, symbolFile, "current", fileName, exportedHeaderFlags)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800628 if refSourceDumpFile.Valid() {
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700629 unzippedRefDump := UnzipRefDump(ctx, refSourceDumpFile.Path(), fileName)
630 library.sAbiDiff = SourceAbiDiff(ctx, library.sAbiOutputFile.Path(), unzippedRefDump, fileName)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800631 }
632 }
633}
634
635func vndkVsNdk(ctx ModuleContext) bool {
Jiyong Parkab0fd5f2017-08-03 21:22:50 +0900636 if inList(ctx.baseModuleName(), llndkLibraries) {
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800637 return false
638 }
639 return true
640}
641
Colin Crossb916a382016-07-29 17:28:03 -0700642func (library *libraryDecorator) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700643 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700644
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700645 objs = objs.Append(deps.Objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700646 var out android.Path
Colin Crossa48ab5b2017-02-14 15:28:44 -0800647 if library.static() || library.header() {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700648 out = library.linkStatic(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700649 } else {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700650 out = library.linkShared(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700651 }
652
653 library.exportIncludes(ctx, "-I")
654 library.reexportFlags(deps.ReexportedFlags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700655 library.reexportDeps(deps.ReexportedFlagsDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700656
Dan Willemsene1240db2016-11-03 14:28:51 -0700657 if library.Properties.Aidl.Export_aidl_headers {
658 if library.baseCompiler.hasSrcExt(".aidl") {
Colin Cross10d22312017-05-03 11:01:58 -0700659 flags := []string{
Dan Willemsene1240db2016-11-03 14:28:51 -0700660 "-I" + android.PathForModuleGen(ctx, "aidl").String(),
Colin Cross10d22312017-05-03 11:01:58 -0700661 }
662 library.reexportFlags(flags)
663 library.reuseExportedFlags = append(library.reuseExportedFlags, flags...)
Dan Willemsene1240db2016-11-03 14:28:51 -0700664 library.reexportDeps(library.baseCompiler.deps) // TODO: restrict to aidl deps
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700665 library.reuseExportedDeps = append(library.reuseExportedDeps, library.baseCompiler.deps...)
Dan Willemsene1240db2016-11-03 14:28:51 -0700666 }
667 }
668
669 if library.Properties.Proto.Export_proto_headers {
670 if library.baseCompiler.hasSrcExt(".proto") {
Colin Cross10d22312017-05-03 11:01:58 -0700671 flags := []string{
Colin Cross38f794e2017-09-07 10:53:07 -0700672 "-I" + android.ProtoSubDir(ctx).String(),
673 "-I" + android.ProtoDir(ctx).String(),
Colin Cross10d22312017-05-03 11:01:58 -0700674 }
675 library.reexportFlags(flags)
676 library.reuseExportedFlags = append(library.reuseExportedFlags, flags...)
Colin Cross0c461f12016-10-20 16:11:43 -0700677 library.reexportDeps(library.baseCompiler.deps) // TODO: restrict to proto deps
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700678 library.reuseExportedDeps = append(library.reuseExportedDeps, library.baseCompiler.deps...)
Colin Cross0c461f12016-10-20 16:11:43 -0700679 }
680 }
681
Colin Cross4d9c2d12016-07-29 12:48:20 -0700682 return out
683}
684
Colin Crossb916a382016-07-29 17:28:03 -0700685func (library *libraryDecorator) buildStatic() bool {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800686 return library.MutatedProperties.BuildStatic &&
Colin Cross4d9c2d12016-07-29 12:48:20 -0700687 (library.Properties.Static.Enabled == nil || *library.Properties.Static.Enabled)
688}
689
Colin Crossb916a382016-07-29 17:28:03 -0700690func (library *libraryDecorator) buildShared() bool {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800691 return library.MutatedProperties.BuildShared &&
Colin Cross4d9c2d12016-07-29 12:48:20 -0700692 (library.Properties.Shared.Enabled == nil || *library.Properties.Shared.Enabled)
693}
694
Colin Crossb916a382016-07-29 17:28:03 -0700695func (library *libraryDecorator) getWholeStaticMissingDeps() []string {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700696 return library.wholeStaticMissingDeps
697}
698
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700699func (library *libraryDecorator) objs() Objects {
700 return library.objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700701}
702
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700703func (library *libraryDecorator) reuseObjs() (Objects, []string, android.Paths) {
704 return library.reuseObjects, library.reuseExportedFlags, library.reuseExportedDeps
Colin Cross4d9c2d12016-07-29 12:48:20 -0700705}
706
Colin Cross26c34ed2016-09-30 17:10:16 -0700707func (library *libraryDecorator) toc() android.OptionalPath {
708 return library.tocFile
709}
710
Colin Crossb916a382016-07-29 17:28:03 -0700711func (library *libraryDecorator) install(ctx ModuleContext, file android.Path) {
Colin Crossc43ae772017-04-14 15:42:53 -0700712 if library.shared() {
Justin Yun8effde42017-06-23 19:24:43 +0900713 if ctx.Device() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700714 if ctx.useVndk() {
Justin Yun8effde42017-06-23 19:24:43 +0900715 if ctx.isVndkSp() {
716 library.baseInstaller.subDir = "vndk-sp"
717 } else if ctx.isVndk() {
718 library.baseInstaller.subDir = "vndk"
719 }
720 }
721 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700722 library.baseInstaller.install(ctx, file)
723 }
724}
725
Colin Crossb916a382016-07-29 17:28:03 -0700726func (library *libraryDecorator) static() bool {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800727 return library.MutatedProperties.VariantIsStatic
Colin Cross4d9c2d12016-07-29 12:48:20 -0700728}
729
Colin Crossa48ab5b2017-02-14 15:28:44 -0800730func (library *libraryDecorator) shared() bool {
731 return library.MutatedProperties.VariantIsShared
732}
733
734func (library *libraryDecorator) header() bool {
735 return !library.static() && !library.shared()
736}
737
738func (library *libraryDecorator) setStatic() {
739 library.MutatedProperties.VariantIsStatic = true
740 library.MutatedProperties.VariantIsShared = false
741}
742
743func (library *libraryDecorator) setShared() {
744 library.MutatedProperties.VariantIsStatic = false
745 library.MutatedProperties.VariantIsShared = true
Colin Crossb916a382016-07-29 17:28:03 -0700746}
747
Colin Crossab3b7322016-12-09 14:46:15 -0800748func (library *libraryDecorator) BuildOnlyStatic() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800749 library.MutatedProperties.BuildShared = false
Colin Crossab3b7322016-12-09 14:46:15 -0800750}
751
752func (library *libraryDecorator) BuildOnlyShared() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800753 library.MutatedProperties.BuildStatic = false
Colin Crossab3b7322016-12-09 14:46:15 -0800754}
755
Colin Cross5950f382016-12-13 12:50:57 -0800756func (library *libraryDecorator) HeaderOnly() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800757 library.MutatedProperties.BuildShared = false
758 library.MutatedProperties.BuildStatic = false
Colin Cross5950f382016-12-13 12:50:57 -0800759}
760
Colin Crossab3b7322016-12-09 14:46:15 -0800761func NewLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700762 module := newModule(hod, android.MultilibBoth)
763
Colin Crossb916a382016-07-29 17:28:03 -0700764 library := &libraryDecorator{
Colin Crossa48ab5b2017-02-14 15:28:44 -0800765 MutatedProperties: LibraryMutatedProperties{
Colin Crossab3b7322016-12-09 14:46:15 -0800766 BuildShared: true,
767 BuildStatic: true,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700768 },
Colin Crossb916a382016-07-29 17:28:03 -0700769 baseCompiler: NewBaseCompiler(),
770 baseLinker: NewBaseLinker(),
771 baseInstaller: NewBaseInstaller("lib", "lib64", InstallInSystem),
772 sanitize: module.sanitize,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800773 sabi: module.sabi,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700774 }
775
Colin Crossb916a382016-07-29 17:28:03 -0700776 module.compiler = library
777 module.linker = library
778 module.installer = library
779
780 return module, library
781}
782
Colin Cross10d22312017-05-03 11:01:58 -0700783// connects a shared library to a static library in order to reuse its .o files to avoid
784// compiling source files twice.
785func reuseStaticLibrary(mctx android.BottomUpMutatorContext, static, shared *Module) {
786 if staticCompiler, ok := static.compiler.(*libraryDecorator); ok {
787 sharedCompiler := shared.compiler.(*libraryDecorator)
788 if len(staticCompiler.Properties.Static.Cflags) == 0 &&
789 len(sharedCompiler.Properties.Shared.Cflags) == 0 {
790
791 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
792 sharedCompiler.baseCompiler.Properties.OriginalSrcs =
793 sharedCompiler.baseCompiler.Properties.Srcs
794 sharedCompiler.baseCompiler.Properties.Srcs = nil
795 sharedCompiler.baseCompiler.Properties.Generated_sources = nil
796 }
797 }
798}
799
Colin Crossb916a382016-07-29 17:28:03 -0700800func linkageMutator(mctx android.BottomUpMutatorContext) {
801 if m, ok := mctx.Module().(*Module); ok && m.linker != nil {
802 if library, ok := m.linker.(libraryInterface); ok {
803 var modules []blueprint.Module
804 if library.buildStatic() && library.buildShared() {
805 modules = mctx.CreateLocalVariations("static", "shared")
806 static := modules[0].(*Module)
807 shared := modules[1].(*Module)
808
Colin Crossa48ab5b2017-02-14 15:28:44 -0800809 static.linker.(libraryInterface).setStatic()
810 shared.linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700811
Colin Cross10d22312017-05-03 11:01:58 -0700812 reuseStaticLibrary(mctx, static, shared)
813
Colin Crossb916a382016-07-29 17:28:03 -0700814 } else if library.buildStatic() {
815 modules = mctx.CreateLocalVariations("static")
Colin Crossa48ab5b2017-02-14 15:28:44 -0800816 modules[0].(*Module).linker.(libraryInterface).setStatic()
Colin Crossb916a382016-07-29 17:28:03 -0700817 } else if library.buildShared() {
818 modules = mctx.CreateLocalVariations("shared")
Colin Crossa48ab5b2017-02-14 15:28:44 -0800819 modules[0].(*Module).linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700820 }
821 }
822 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700823}