blob: 25872c6cb52e27a6f65bdf5ef745cf9320a8cfcf [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 }
Dan Albertf563d252017-10-13 00:29:00 -070073
74 Static_ndk_lib bool
Colin Crossa48ab5b2017-02-14 15:28:44 -080075}
Colin Cross0c461f12016-10-20 16:11:43 -070076
Colin Crossa48ab5b2017-02-14 15:28:44 -080077type LibraryMutatedProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -070078 VariantName string `blueprint:"mutated"`
Colin Crossb916a382016-07-29 17:28:03 -070079
80 // Build a static variant
81 BuildStatic bool `blueprint:"mutated"`
82 // Build a shared variant
83 BuildShared bool `blueprint:"mutated"`
84 // This variant is shared
85 VariantIsShared bool `blueprint:"mutated"`
86 // This variant is static
87 VariantIsStatic bool `blueprint:"mutated"`
Dan Albertf563d252017-10-13 00:29:00 -070088 // Location of the static library in the sysroot. Empty if the library is
89 // not included in the NDK.
90 NdkSysrootPath string `blueprint:"mutated"`
Colin Crossb916a382016-07-29 17:28:03 -070091}
92
93type FlagExporterProperties struct {
94 // list of directories relative to the Blueprints file that will
Dan Willemsen273af7f2016-11-03 15:53:42 -070095 // be added to the include path (using -I) for this module and any module that links
Colin Cross5d195602017-10-17 16:15:50 -070096 // against this module. Directories listed in export_include_dirs do not need to be
97 // listed in local_include_dirs.
Colin Crossb916a382016-07-29 17:28:03 -070098 Export_include_dirs []string `android:"arch_variant"`
Dan Willemsen4416e5d2017-04-06 12:43:22 -070099
100 Target struct {
101 Vendor struct {
102 // list of exported include directories, like
103 // export_include_dirs, that will be applied to the
104 // vendor variant of this library. This will overwrite
105 // any other declarations.
106 Export_include_dirs []string
107 }
108 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700109}
110
111func init() {
Colin Cross798bfce2016-10-12 14:28:16 -0700112 android.RegisterModuleType("cc_library_static", libraryStaticFactory)
113 android.RegisterModuleType("cc_library_shared", librarySharedFactory)
114 android.RegisterModuleType("cc_library", libraryFactory)
115 android.RegisterModuleType("cc_library_host_static", libraryHostStaticFactory)
116 android.RegisterModuleType("cc_library_host_shared", libraryHostSharedFactory)
Colin Cross5950f382016-12-13 12:50:57 -0800117 android.RegisterModuleType("cc_library_headers", libraryHeaderFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700118}
119
120// Module factory for combined static + shared libraries, device by default but with possible host
121// support
Colin Cross36242852017-06-23 15:06:31 -0700122func libraryFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800123 module, _ := NewLibrary(android.HostAndDeviceSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700124 return module.Init()
125}
126
127// Module factory for static libraries
Colin Cross36242852017-06-23 15:06:31 -0700128func libraryStaticFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800129 module, library := NewLibrary(android.HostAndDeviceSupported)
130 library.BuildOnlyStatic()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700131 return module.Init()
132}
133
134// Module factory for shared libraries
Colin Cross36242852017-06-23 15:06:31 -0700135func librarySharedFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800136 module, library := NewLibrary(android.HostAndDeviceSupported)
137 library.BuildOnlyShared()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700138 return module.Init()
139}
140
141// Module factory for host static libraries
Colin Cross36242852017-06-23 15:06:31 -0700142func libraryHostStaticFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800143 module, library := NewLibrary(android.HostSupported)
144 library.BuildOnlyStatic()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700145 return module.Init()
146}
147
148// Module factory for host shared libraries
Colin Cross36242852017-06-23 15:06:31 -0700149func libraryHostSharedFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800150 module, library := NewLibrary(android.HostSupported)
151 library.BuildOnlyShared()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700152 return module.Init()
153}
154
Colin Cross5950f382016-12-13 12:50:57 -0800155// Module factory for header-only libraries
Colin Cross36242852017-06-23 15:06:31 -0700156func libraryHeaderFactory() android.Module {
Colin Cross5950f382016-12-13 12:50:57 -0800157 module, library := NewLibrary(android.HostAndDeviceSupported)
158 library.HeaderOnly()
159 return module.Init()
160}
161
Colin Cross4d9c2d12016-07-29 12:48:20 -0700162type flagExporter struct {
163 Properties FlagExporterProperties
164
Dan Willemsen847dcc72016-09-29 12:13:36 -0700165 flags []string
166 flagsDeps android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700167}
168
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700169func (f *flagExporter) exportedIncludes(ctx ModuleContext) android.Paths {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700170 if ctx.useVndk() && f.Properties.Target.Vendor.Export_include_dirs != nil {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700171 return android.PathsForModuleSrc(ctx, f.Properties.Target.Vendor.Export_include_dirs)
172 } else {
173 return android.PathsForModuleSrc(ctx, f.Properties.Export_include_dirs)
174 }
175}
176
Colin Cross4d9c2d12016-07-29 12:48:20 -0700177func (f *flagExporter) exportIncludes(ctx ModuleContext, inc string) {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700178 includeDirs := f.exportedIncludes(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700179 for _, dir := range includeDirs.Strings() {
180 f.flags = append(f.flags, inc+dir)
181 }
182}
183
184func (f *flagExporter) reexportFlags(flags []string) {
185 f.flags = append(f.flags, flags...)
186}
187
Dan Willemsen847dcc72016-09-29 12:13:36 -0700188func (f *flagExporter) reexportDeps(deps android.Paths) {
189 f.flagsDeps = append(f.flagsDeps, deps...)
190}
191
Colin Cross4d9c2d12016-07-29 12:48:20 -0700192func (f *flagExporter) exportedFlags() []string {
193 return f.flags
194}
195
Dan Willemsen847dcc72016-09-29 12:13:36 -0700196func (f *flagExporter) exportedFlagsDeps() android.Paths {
197 return f.flagsDeps
198}
199
Colin Cross4d9c2d12016-07-29 12:48:20 -0700200type exportedFlagsProducer interface {
201 exportedFlags() []string
Dan Willemsen847dcc72016-09-29 12:13:36 -0700202 exportedFlagsDeps() android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700203}
204
205var _ exportedFlagsProducer = (*flagExporter)(nil)
206
Colin Crossb916a382016-07-29 17:28:03 -0700207// libraryDecorator wraps baseCompiler, baseLinker and baseInstaller to provide library-specific
208// functionality: static vs. shared linkage, reusing object files for shared libraries
209type libraryDecorator struct {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800210 Properties LibraryProperties
211 MutatedProperties LibraryMutatedProperties
Colin Cross4d9c2d12016-07-29 12:48:20 -0700212
213 // For reusing static library objects for shared library
Colin Cross10d22312017-05-03 11:01:58 -0700214 reuseObjects Objects
215 reuseExportedFlags []string
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700216 reuseExportedDeps android.Paths
Colin Cross10d22312017-05-03 11:01:58 -0700217
Colin Cross26c34ed2016-09-30 17:10:16 -0700218 // table-of-contents file to optimize out relinking when possible
219 tocFile android.OptionalPath
Colin Cross4d9c2d12016-07-29 12:48:20 -0700220
Colin Cross4d9c2d12016-07-29 12:48:20 -0700221 flagExporter
222 stripper
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700223 relocationPacker
Colin Cross4d9c2d12016-07-29 12:48:20 -0700224
Colin Cross4d9c2d12016-07-29 12:48:20 -0700225 // If we're used as a whole_static_lib, our missing dependencies need
226 // to be given
227 wholeStaticMissingDeps []string
228
229 // For whole_static_libs
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700230 objects Objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700231
232 // Uses the module's name if empty, but can be overridden. Does not include
233 // shlib suffix.
234 libName string
Colin Crossb916a382016-07-29 17:28:03 -0700235
236 sanitize *sanitize
237
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800238 sabi *sabi
239
Dan Willemsen581341d2017-02-09 16:16:31 -0800240 // Output archive of gcno coverage information files
241 coverageOutputFile android.OptionalPath
242
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800243 // linked Source Abi Dump
244 sAbiOutputFile android.OptionalPath
245
246 // Source Abi Diff
247 sAbiDiff android.OptionalPath
248
Colin Crossb916a382016-07-29 17:28:03 -0700249 // Decorated interafaces
250 *baseCompiler
251 *baseLinker
252 *baseInstaller
Colin Cross4d9c2d12016-07-29 12:48:20 -0700253}
254
Colin Crossb916a382016-07-29 17:28:03 -0700255func (library *libraryDecorator) linkerProps() []interface{} {
256 var props []interface{}
257 props = append(props, library.baseLinker.linkerProps()...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700258 return append(props,
259 &library.Properties,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800260 &library.MutatedProperties,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700261 &library.flagExporter.Properties,
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700262 &library.stripper.StripProperties,
263 &library.relocationPacker.Properties)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700264}
265
Colin Crossb916a382016-07-29 17:28:03 -0700266func (library *libraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross42742b82016-08-01 13:20:05 -0700267 flags = library.baseLinker.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700268
Colin Crossb916a382016-07-29 17:28:03 -0700269 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
270 // all code is position independent, and then those warnings get promoted to
271 // errors.
Colin Cross3edeee12017-04-04 12:59:48 -0700272 if !ctx.Windows() {
Colin Crossb916a382016-07-29 17:28:03 -0700273 flags.CFlags = append(flags.CFlags, "-fPIC")
274 }
275
276 if library.static() {
277 flags.CFlags = append(flags.CFlags, library.Properties.Static.Cflags...)
Colin Crossa48ab5b2017-02-14 15:28:44 -0800278 } else if library.shared() {
Colin Crossb916a382016-07-29 17:28:03 -0700279 flags.CFlags = append(flags.CFlags, library.Properties.Shared.Cflags...)
280 }
281
Colin Crossa48ab5b2017-02-14 15:28:44 -0800282 if library.shared() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700283 libName := library.getLibName(ctx)
284 // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead
285 sharedFlag := "-Wl,-shared"
286 if flags.Clang || ctx.Host() {
287 sharedFlag = "-shared"
288 }
289 var f []string
Dan Willemsen01a405a2016-06-13 17:19:03 -0700290 if ctx.toolchain().Bionic() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700291 f = append(f,
292 "-nostdlib",
293 "-Wl,--gc-sections",
294 )
295 }
296
297 if ctx.Darwin() {
298 f = append(f,
299 "-dynamiclib",
300 "-single_module",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700301 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
302 )
Colin Cross7863cf52016-10-20 10:47:21 -0700303 if ctx.Arch().ArchType == android.X86 {
304 f = append(f,
305 "-read_only_relocs suppress",
306 )
307 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700308 } else {
309 f = append(f,
310 sharedFlag,
311 "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix())
312 }
313
314 flags.LdFlags = append(f, flags.LdFlags...)
315 }
316
317 return flags
318}
319
Dan Willemsen273af7f2016-11-03 15:53:42 -0700320func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700321 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700322 if len(exportIncludeDirs) > 0 {
Colin Crossdad8c952017-04-26 14:55:27 -0700323 f := includeDirsToFlags(exportIncludeDirs)
324 flags.GlobalFlags = append(flags.GlobalFlags, f)
325 flags.YasmFlags = append(flags.YasmFlags, f)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700326 }
327
328 return library.baseCompiler.compilerFlags(ctx, flags)
329}
330
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700331func extractExportIncludesFromFlags(flags []string) []string {
332 // This method is used in the generation of rules which produce
333 // abi-dumps for source files. Exported headers are needed to infer the
334 // abi exported by a library and filter out the rest of the abi dumped
335 // from a source. We extract the include flags exported by a library.
336 // This includes the flags exported which are re-exported from static
337 // library dependencies, exported header library dependencies and
Jayant Chowdharyaf6eb712017-08-23 16:08:29 -0700338 // generated header dependencies. -isystem headers are not included
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700339 // since for bionic libraries, abi-filtering is taken care of by version
340 // scripts.
341 var exportedIncludes []string
342 for _, flag := range flags {
343 if strings.HasPrefix(flag, "-I") {
344 exportedIncludes = append(exportedIncludes, flag)
345 }
346 }
347 return exportedIncludes
348}
349
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700350func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Colin Cross5950f382016-12-13 12:50:57 -0800351 if !library.buildShared() && !library.buildStatic() {
352 if len(library.baseCompiler.Properties.Srcs) > 0 {
353 ctx.PropertyErrorf("srcs", "cc_library_headers must not have any srcs")
354 }
355 if len(library.Properties.Static.Srcs) > 0 {
356 ctx.PropertyErrorf("static.srcs", "cc_library_headers must not have any srcs")
357 }
358 if len(library.Properties.Shared.Srcs) > 0 {
359 ctx.PropertyErrorf("shared.srcs", "cc_library_headers must not have any srcs")
360 }
361 return Objects{}
362 }
Jayant Chowdhary2a966402017-08-07 14:09:45 -0700363 if ctx.createVndkSourceAbiDump() || library.sabi.Properties.CreateSAbiDumps {
Jayant Chowdharya4fce192017-09-06 13:10:03 -0700364 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800365 var SourceAbiFlags []string
366 for _, dir := range exportIncludeDirs.Strings() {
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700367 SourceAbiFlags = append(SourceAbiFlags, "-I"+dir)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800368 }
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700369 for _, reexportedInclude := range extractExportIncludesFromFlags(library.sabi.Properties.ReexportedIncludeFlags) {
370 SourceAbiFlags = append(SourceAbiFlags, reexportedInclude)
371 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800372 flags.SAbiFlags = SourceAbiFlags
373 total_length := len(library.baseCompiler.Properties.Srcs) + len(deps.GeneratedSources) + len(library.Properties.Shared.Srcs) +
374 len(library.Properties.Static.Srcs)
375 if total_length > 0 {
376 flags.SAbiDump = true
377 }
378 }
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700379 objs := library.baseCompiler.compile(ctx, flags, deps)
380 library.reuseObjects = objs
Colin Cross2f336352016-10-26 10:03:47 -0700381 buildFlags := flagsToBuilderFlags(flags)
Colin Crossb916a382016-07-29 17:28:03 -0700382
Colin Cross4d9c2d12016-07-29 12:48:20 -0700383 if library.static() {
Colin Cross2f336352016-10-26 10:03:47 -0700384 srcs := android.PathsForModuleSrc(ctx, library.Properties.Static.Srcs)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700385 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceStaticLibrary,
386 srcs, library.baseCompiler.deps))
Colin Crossa48ab5b2017-02-14 15:28:44 -0800387 } else if library.shared() {
Colin Cross2f336352016-10-26 10:03:47 -0700388 srcs := android.PathsForModuleSrc(ctx, library.Properties.Shared.Srcs)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700389 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceSharedLibrary,
390 srcs, library.baseCompiler.deps))
Colin Crossb916a382016-07-29 17:28:03 -0700391 }
392
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700393 return objs
Colin Crossb916a382016-07-29 17:28:03 -0700394}
395
396type libraryInterface interface {
397 getWholeStaticMissingDeps() []string
398 static() bool
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700399 objs() Objects
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700400 reuseObjs() (Objects, []string, android.Paths)
Colin Cross26c34ed2016-09-30 17:10:16 -0700401 toc() android.OptionalPath
Colin Crossb916a382016-07-29 17:28:03 -0700402
403 // Returns true if the build options for the module have selected a static or shared build
404 buildStatic() bool
405 buildShared() bool
406
407 // Sets whether a specific variant is static or shared
Colin Crossa48ab5b2017-02-14 15:28:44 -0800408 setStatic()
409 setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700410}
411
412func (library *libraryDecorator) getLibName(ctx ModuleContext) string {
413 name := library.libName
414 if name == "" {
Colin Crossce75d2c2016-10-06 16:12:58 -0700415 name = ctx.baseModuleName()
Colin Crossb916a382016-07-29 17:28:03 -0700416 }
417
418 if ctx.Host() && Bool(library.Properties.Unique_host_soname) {
419 if !strings.HasSuffix(name, "-host") {
420 name = name + "-host"
421 }
422 }
423
Colin Crossa48ab5b2017-02-14 15:28:44 -0800424 return name + library.MutatedProperties.VariantName
Colin Crossb916a382016-07-29 17:28:03 -0700425}
426
427func (library *libraryDecorator) linkerInit(ctx BaseModuleContext) {
428 location := InstallInSystem
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700429 if library.sanitize.inSanitizerDir() {
430 location = InstallInSanitizerDir
Colin Crossb916a382016-07-29 17:28:03 -0700431 }
432 library.baseInstaller.location = location
433
434 library.baseLinker.linkerInit(ctx)
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700435
436 library.relocationPacker.packingInit(ctx)
Colin Crossb916a382016-07-29 17:28:03 -0700437}
438
Colin Cross37047f12016-12-13 17:06:13 -0800439func (library *libraryDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700440 deps = library.baseLinker.linkerDeps(ctx, deps)
441
442 if library.static() {
443 deps.WholeStaticLibs = append(deps.WholeStaticLibs,
444 library.Properties.Static.Whole_static_libs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700445 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Static.Static_libs...)
446 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Static.Shared_libs...)
Colin Crossa48ab5b2017-02-14 15:28:44 -0800447 } else if library.shared() {
Dan Willemsen2e47b342016-11-17 01:02:25 -0800448 if ctx.toolchain().Bionic() && !Bool(library.baseLinker.Properties.Nocrt) {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700449 if !ctx.useSdk() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700450 deps.CrtBegin = "crtbegin_so"
451 deps.CrtEnd = "crtend_so"
452 } else {
Dan Albertebedf672016-11-08 15:06:22 -0800453 // TODO(danalbert): Add generation of crt objects.
454 // For `sdk_version: "current"`, we don't actually have a
455 // freshly generated set of CRT objects. Use the last stable
456 // version.
457 version := ctx.sdkVersion()
458 if version == "current" {
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -0700459 version = getCurrentNdkPrebuiltVersion(ctx)
Dan Albertebedf672016-11-08 15:06:22 -0800460 }
461 deps.CrtBegin = "ndk_crtbegin_so." + version
462 deps.CrtEnd = "ndk_crtend_so." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700463 }
464 }
465 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Shared.Whole_static_libs...)
466 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Shared.Static_libs...)
467 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Shared.Shared_libs...)
468 }
Jiyong Park52d25bd2017-10-13 09:17:01 +0900469 if ctx.useVndk() {
470 deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs)
471 deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Vendor.Exclude_shared_libs)
472 deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs)
473 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700474 return deps
475}
476
Colin Crossb916a382016-07-29 17:28:03 -0700477func (library *libraryDecorator) linkStatic(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700478 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700479
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700480 library.objects = deps.WholeStaticLibObjs.Copy()
481 library.objects = library.objects.Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700482
483 outputFile := android.PathForModuleOut(ctx,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800484 ctx.ModuleName()+library.MutatedProperties.VariantName+staticLibraryExtension)
Dan Willemsen581341d2017-02-09 16:16:31 -0800485 builderFlags := flagsToBuilderFlags(flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700486
Dan Willemsen581341d2017-02-09 16:16:31 -0800487 TransformObjToStaticLib(ctx, library.objects.objFiles, builderFlags, outputFile, objs.tidyFiles)
488
489 library.coverageOutputFile = TransformCoverageFilesToLib(ctx, library.objects, builderFlags,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800490 ctx.ModuleName()+library.MutatedProperties.VariantName)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700491
492 library.wholeStaticMissingDeps = ctx.GetMissingDependencies()
493
494 ctx.CheckbuildFile(outputFile)
495
496 return outputFile
497}
498
Colin Crossb916a382016-07-29 17:28:03 -0700499func (library *libraryDecorator) linkShared(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700500 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700501
502 var linkerDeps android.Paths
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700503 linkerDeps = append(linkerDeps, flags.LdFlagsDeps...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700504
505 versionScript := android.OptionalPathForModuleSrc(ctx, library.Properties.Version_script)
506 unexportedSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Unexported_symbols_list)
507 forceNotWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_not_weak_list)
508 forceWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_weak_list)
Jiyong Park52d25bd2017-10-13 09:17:01 +0900509 if ctx.useVndk() && library.Properties.Target.Vendor.Version_script != nil {
510 versionScript = android.OptionalPathForModuleSrc(ctx, library.Properties.Target.Vendor.Version_script)
511 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700512 if !ctx.Darwin() {
513 if versionScript.Valid() {
514 flags.LdFlags = append(flags.LdFlags, "-Wl,--version-script,"+versionScript.String())
515 linkerDeps = append(linkerDeps, versionScript.Path())
516 }
517 if unexportedSymbols.Valid() {
518 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
519 }
520 if forceNotWeakSymbols.Valid() {
521 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
522 }
523 if forceWeakSymbols.Valid() {
524 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
525 }
526 } else {
527 if versionScript.Valid() {
528 ctx.PropertyErrorf("version_script", "Not supported on Darwin")
529 }
530 if unexportedSymbols.Valid() {
531 flags.LdFlags = append(flags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
532 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
533 }
534 if forceNotWeakSymbols.Valid() {
535 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
536 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
537 }
538 if forceWeakSymbols.Valid() {
539 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
540 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
541 }
542 }
543
544 fileName := library.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
545 outputFile := android.PathForModuleOut(ctx, fileName)
546 ret := outputFile
547
548 builderFlags := flagsToBuilderFlags(flags)
549
Colin Crossd8f8d072017-04-04 13:00:15 -0700550 if !ctx.Darwin() && !ctx.Windows() {
Colin Cross89562dc2016-10-03 17:47:19 -0700551 // Optimize out relinking against shared libraries whose interface hasn't changed by
552 // depending on a table of contents file instead of the library itself.
553 tocPath := outputFile.RelPathString()
554 tocPath = pathtools.ReplaceExtension(tocPath, flags.Toolchain.ShlibSuffix()[1:]+".toc")
555 tocFile := android.PathForOutput(ctx, tocPath)
556 library.tocFile = android.OptionalPathForPath(tocFile)
557 TransformSharedObjectToToc(ctx, outputFile, tocFile, builderFlags)
558 }
559
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700560 if library.relocationPacker.needsPacking(ctx) {
561 packedOutputFile := outputFile
562 outputFile = android.PathForModuleOut(ctx, "unpacked", fileName)
563 library.relocationPacker.pack(ctx, outputFile, packedOutputFile, builderFlags)
564 }
565
Colin Cross4d9c2d12016-07-29 12:48:20 -0700566 if library.stripper.needsStrip(ctx) {
567 strippedOutputFile := outputFile
568 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
569 library.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
570 }
571
572 sharedLibs := deps.SharedLibs
573 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
574
Dan Albertd015c4a2016-08-10 14:34:08 -0700575 // TODO(danalbert): Clean this up when soong supports prebuilts.
576 if strings.HasPrefix(ctx.selectedStl(), "ndk_libc++") {
577 libDir := getNdkStlLibDir(ctx, flags.Toolchain, "libc++")
578
579 if strings.HasSuffix(ctx.selectedStl(), "_shared") {
580 deps.StaticLibs = append(deps.StaticLibs,
581 libDir.Join(ctx, "libandroid_support.a"))
582 } else {
583 deps.StaticLibs = append(deps.StaticLibs,
584 libDir.Join(ctx, "libc++abi.a"),
585 libDir.Join(ctx, "libandroid_support.a"))
586 }
587
588 if ctx.Arch().ArchType == android.Arm {
589 deps.StaticLibs = append(deps.StaticLibs,
590 libDir.Join(ctx, "libunwind.a"))
591 }
592 }
593
Colin Cross26c34ed2016-09-30 17:10:16 -0700594 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
595 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700596 linkerDeps = append(linkerDeps, objs.tidyFiles...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700597
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700598 TransformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700599 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
600 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile)
601
Dan Willemsen581341d2017-02-09 16:16:31 -0800602 objs.coverageFiles = append(objs.coverageFiles, deps.StaticLibObjs.coverageFiles...)
603 objs.coverageFiles = append(objs.coverageFiles, deps.WholeStaticLibObjs.coverageFiles...)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800604
605 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.StaticLibObjs.sAbiDumpFiles...)
606 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.WholeStaticLibObjs.sAbiDumpFiles...)
607
Dan Willemsen581341d2017-02-09 16:16:31 -0800608 library.coverageOutputFile = TransformCoverageFilesToLib(ctx, objs, builderFlags, library.getLibName(ctx))
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700609 library.linkSAbiDumpFiles(ctx, objs, fileName, ret)
Dan Willemsen581341d2017-02-09 16:16:31 -0800610
Colin Cross4d9c2d12016-07-29 12:48:20 -0700611 return ret
612}
613
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700614func (library *libraryDecorator) linkSAbiDumpFiles(ctx ModuleContext, objs Objects, fileName string, soFile android.Path) {
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800615 //Also take into account object re-use.
Justin Yun8effde42017-06-23 19:24:43 +0900616 if len(objs.sAbiDumpFiles) > 0 && ctx.createVndkSourceAbiDump() {
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800617 refSourceDumpFile := android.PathForVndkRefAbiDump(ctx, "current", fileName, vndkVsNdk(ctx), true)
618 versionScript := android.OptionalPathForModuleSrc(ctx, library.Properties.Version_script)
619 var symbolFile android.OptionalPath
620 if versionScript.Valid() {
621 symbolFile = versionScript
622 }
Jayant Chowdharya4fce192017-09-06 13:10:03 -0700623 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800624 var SourceAbiFlags []string
625 for _, dir := range exportIncludeDirs.Strings() {
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700626 SourceAbiFlags = append(SourceAbiFlags, "-I"+dir)
627 }
628 for _, reexportedInclude := range extractExportIncludesFromFlags(library.sabi.Properties.ReexportedIncludeFlags) {
629 SourceAbiFlags = append(SourceAbiFlags, reexportedInclude)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800630 }
631 exportedHeaderFlags := strings.Join(SourceAbiFlags, " ")
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700632 library.sAbiOutputFile = TransformDumpToLinkedDump(ctx, objs.sAbiDumpFiles, soFile, symbolFile, "current", fileName, exportedHeaderFlags)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800633 if refSourceDumpFile.Valid() {
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700634 unzippedRefDump := UnzipRefDump(ctx, refSourceDumpFile.Path(), fileName)
635 library.sAbiDiff = SourceAbiDiff(ctx, library.sAbiOutputFile.Path(), unzippedRefDump, fileName)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800636 }
637 }
638}
639
640func vndkVsNdk(ctx ModuleContext) bool {
Jiyong Parkab0fd5f2017-08-03 21:22:50 +0900641 if inList(ctx.baseModuleName(), llndkLibraries) {
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800642 return false
643 }
644 return true
645}
646
Colin Crossb916a382016-07-29 17:28:03 -0700647func (library *libraryDecorator) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700648 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700649
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700650 objs = objs.Append(deps.Objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700651 var out android.Path
Colin Crossa48ab5b2017-02-14 15:28:44 -0800652 if library.static() || library.header() {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700653 out = library.linkStatic(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700654 } else {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700655 out = library.linkShared(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700656 }
657
658 library.exportIncludes(ctx, "-I")
659 library.reexportFlags(deps.ReexportedFlags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700660 library.reexportDeps(deps.ReexportedFlagsDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700661
Dan Willemsene1240db2016-11-03 14:28:51 -0700662 if library.Properties.Aidl.Export_aidl_headers {
663 if library.baseCompiler.hasSrcExt(".aidl") {
Colin Cross10d22312017-05-03 11:01:58 -0700664 flags := []string{
Dan Willemsene1240db2016-11-03 14:28:51 -0700665 "-I" + android.PathForModuleGen(ctx, "aidl").String(),
Colin Cross10d22312017-05-03 11:01:58 -0700666 }
667 library.reexportFlags(flags)
668 library.reuseExportedFlags = append(library.reuseExportedFlags, flags...)
Dan Willemsene1240db2016-11-03 14:28:51 -0700669 library.reexportDeps(library.baseCompiler.deps) // TODO: restrict to aidl deps
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700670 library.reuseExportedDeps = append(library.reuseExportedDeps, library.baseCompiler.deps...)
Dan Willemsene1240db2016-11-03 14:28:51 -0700671 }
672 }
673
674 if library.Properties.Proto.Export_proto_headers {
675 if library.baseCompiler.hasSrcExt(".proto") {
Colin Cross10d22312017-05-03 11:01:58 -0700676 flags := []string{
Colin Cross38f794e2017-09-07 10:53:07 -0700677 "-I" + android.ProtoSubDir(ctx).String(),
678 "-I" + android.ProtoDir(ctx).String(),
Colin Cross10d22312017-05-03 11:01:58 -0700679 }
680 library.reexportFlags(flags)
681 library.reuseExportedFlags = append(library.reuseExportedFlags, flags...)
Colin Cross0c461f12016-10-20 16:11:43 -0700682 library.reexportDeps(library.baseCompiler.deps) // TODO: restrict to proto deps
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700683 library.reuseExportedDeps = append(library.reuseExportedDeps, library.baseCompiler.deps...)
Colin Cross0c461f12016-10-20 16:11:43 -0700684 }
685 }
686
Colin Cross4d9c2d12016-07-29 12:48:20 -0700687 return out
688}
689
Colin Crossb916a382016-07-29 17:28:03 -0700690func (library *libraryDecorator) buildStatic() bool {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800691 return library.MutatedProperties.BuildStatic &&
Colin Cross4d9c2d12016-07-29 12:48:20 -0700692 (library.Properties.Static.Enabled == nil || *library.Properties.Static.Enabled)
693}
694
Colin Crossb916a382016-07-29 17:28:03 -0700695func (library *libraryDecorator) buildShared() bool {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800696 return library.MutatedProperties.BuildShared &&
Colin Cross4d9c2d12016-07-29 12:48:20 -0700697 (library.Properties.Shared.Enabled == nil || *library.Properties.Shared.Enabled)
698}
699
Colin Crossb916a382016-07-29 17:28:03 -0700700func (library *libraryDecorator) getWholeStaticMissingDeps() []string {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700701 return library.wholeStaticMissingDeps
702}
703
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700704func (library *libraryDecorator) objs() Objects {
705 return library.objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700706}
707
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700708func (library *libraryDecorator) reuseObjs() (Objects, []string, android.Paths) {
709 return library.reuseObjects, library.reuseExportedFlags, library.reuseExportedDeps
Colin Cross4d9c2d12016-07-29 12:48:20 -0700710}
711
Colin Cross26c34ed2016-09-30 17:10:16 -0700712func (library *libraryDecorator) toc() android.OptionalPath {
713 return library.tocFile
714}
715
Colin Crossb916a382016-07-29 17:28:03 -0700716func (library *libraryDecorator) install(ctx ModuleContext, file android.Path) {
Colin Crossc43ae772017-04-14 15:42:53 -0700717 if library.shared() {
Justin Yun8effde42017-06-23 19:24:43 +0900718 if ctx.Device() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700719 if ctx.useVndk() {
Justin Yun8effde42017-06-23 19:24:43 +0900720 if ctx.isVndkSp() {
721 library.baseInstaller.subDir = "vndk-sp"
722 } else if ctx.isVndk() {
723 library.baseInstaller.subDir = "vndk"
724 }
725 }
726 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700727 library.baseInstaller.install(ctx, file)
728 }
Dan Albertf563d252017-10-13 00:29:00 -0700729
730 if library.Properties.Static_ndk_lib && library.static() {
731 installPath := getNdkSysrootBase(ctx).Join(
732 ctx, "usr/lib", ctx.toolchain().ClangTriple(), file.Base())
733
734 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
735 Rule: android.Cp,
736 Description: "install " + installPath.Base(),
737 Output: installPath,
738 Input: file,
739 })
740
741 library.MutatedProperties.NdkSysrootPath = installPath.String()
742 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700743}
744
Colin Crossb916a382016-07-29 17:28:03 -0700745func (library *libraryDecorator) static() bool {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800746 return library.MutatedProperties.VariantIsStatic
Colin Cross4d9c2d12016-07-29 12:48:20 -0700747}
748
Colin Crossa48ab5b2017-02-14 15:28:44 -0800749func (library *libraryDecorator) shared() bool {
750 return library.MutatedProperties.VariantIsShared
751}
752
753func (library *libraryDecorator) header() bool {
754 return !library.static() && !library.shared()
755}
756
757func (library *libraryDecorator) setStatic() {
758 library.MutatedProperties.VariantIsStatic = true
759 library.MutatedProperties.VariantIsShared = false
760}
761
762func (library *libraryDecorator) setShared() {
763 library.MutatedProperties.VariantIsStatic = false
764 library.MutatedProperties.VariantIsShared = true
Colin Crossb916a382016-07-29 17:28:03 -0700765}
766
Colin Crossab3b7322016-12-09 14:46:15 -0800767func (library *libraryDecorator) BuildOnlyStatic() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800768 library.MutatedProperties.BuildShared = false
Colin Crossab3b7322016-12-09 14:46:15 -0800769}
770
771func (library *libraryDecorator) BuildOnlyShared() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800772 library.MutatedProperties.BuildStatic = false
Colin Crossab3b7322016-12-09 14:46:15 -0800773}
774
Colin Cross5950f382016-12-13 12:50:57 -0800775func (library *libraryDecorator) HeaderOnly() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800776 library.MutatedProperties.BuildShared = false
777 library.MutatedProperties.BuildStatic = false
Colin Cross5950f382016-12-13 12:50:57 -0800778}
779
Colin Crossab3b7322016-12-09 14:46:15 -0800780func NewLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700781 module := newModule(hod, android.MultilibBoth)
782
Colin Crossb916a382016-07-29 17:28:03 -0700783 library := &libraryDecorator{
Colin Crossa48ab5b2017-02-14 15:28:44 -0800784 MutatedProperties: LibraryMutatedProperties{
Colin Crossab3b7322016-12-09 14:46:15 -0800785 BuildShared: true,
786 BuildStatic: true,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700787 },
Colin Crossb916a382016-07-29 17:28:03 -0700788 baseCompiler: NewBaseCompiler(),
789 baseLinker: NewBaseLinker(),
790 baseInstaller: NewBaseInstaller("lib", "lib64", InstallInSystem),
791 sanitize: module.sanitize,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800792 sabi: module.sabi,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700793 }
794
Colin Crossb916a382016-07-29 17:28:03 -0700795 module.compiler = library
796 module.linker = library
797 module.installer = library
798
799 return module, library
800}
801
Colin Cross10d22312017-05-03 11:01:58 -0700802// connects a shared library to a static library in order to reuse its .o files to avoid
803// compiling source files twice.
804func reuseStaticLibrary(mctx android.BottomUpMutatorContext, static, shared *Module) {
805 if staticCompiler, ok := static.compiler.(*libraryDecorator); ok {
806 sharedCompiler := shared.compiler.(*libraryDecorator)
807 if len(staticCompiler.Properties.Static.Cflags) == 0 &&
808 len(sharedCompiler.Properties.Shared.Cflags) == 0 {
809
810 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
811 sharedCompiler.baseCompiler.Properties.OriginalSrcs =
812 sharedCompiler.baseCompiler.Properties.Srcs
813 sharedCompiler.baseCompiler.Properties.Srcs = nil
814 sharedCompiler.baseCompiler.Properties.Generated_sources = nil
815 }
816 }
817}
818
Colin Crossb916a382016-07-29 17:28:03 -0700819func linkageMutator(mctx android.BottomUpMutatorContext) {
820 if m, ok := mctx.Module().(*Module); ok && m.linker != nil {
821 if library, ok := m.linker.(libraryInterface); ok {
822 var modules []blueprint.Module
823 if library.buildStatic() && library.buildShared() {
824 modules = mctx.CreateLocalVariations("static", "shared")
825 static := modules[0].(*Module)
826 shared := modules[1].(*Module)
827
Colin Crossa48ab5b2017-02-14 15:28:44 -0800828 static.linker.(libraryInterface).setStatic()
829 shared.linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700830
Colin Cross10d22312017-05-03 11:01:58 -0700831 reuseStaticLibrary(mctx, static, shared)
832
Colin Crossb916a382016-07-29 17:28:03 -0700833 } else if library.buildStatic() {
834 modules = mctx.CreateLocalVariations("static")
Colin Crossa48ab5b2017-02-14 15:28:44 -0800835 modules[0].(*Module).linker.(libraryInterface).setStatic()
Colin Crossb916a382016-07-29 17:28:03 -0700836 } else if library.buildShared() {
837 modules = mctx.CreateLocalVariations("shared")
Colin Crossa48ab5b2017-02-14 15:28:44 -0800838 modules[0].(*Module).linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700839 }
840 }
841 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700842}