blob: 23e4f44cdfde03b5233440fc3ad1319757aea0ca [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
Nan Zhang0007d812017-11-07 10:57:05 -080060 Export_aidl_headers *bool
Dan Willemsene1240db2016-11-03 14:28:51 -070061 }
62
Colin Cross0c461f12016-10-20 16:11:43 -070063 Proto struct {
64 // export headers generated from .proto sources
Nan Zhang0007d812017-11-07 10:57:05 -080065 Export_proto_headers *bool
Colin Cross0c461f12016-10-20 16:11:43 -070066 }
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
Nan Zhang0007d812017-11-07 10:57:05 -080074 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() {
Steven Morelandf9e62162017-11-02 17:00:50 -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)
117 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
Steven Morelandf9e62162017-11-02 17:00:50 -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
Steven Morelandf9e62162017-11-02 17:00:50 -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
Steven Morelandf9e62162017-11-02 17:00:50 -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
Steven Morelandf9e62162017-11-02 17:00:50 -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
Steven Morelandf9e62162017-11-02 17:00:50 -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
Steven Morelandf9e62162017-11-02 17:00:50 -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())
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000516 if library.sanitize.isSanitizerEnabled(cfi) {
517 flags.LdFlags = append(flags.LdFlags, "-Wl,--version-script,"+cfiExportsMap.String())
518 linkerDeps = append(linkerDeps, cfiExportsMap)
519 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700520 }
521 if unexportedSymbols.Valid() {
522 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
523 }
524 if forceNotWeakSymbols.Valid() {
525 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
526 }
527 if forceWeakSymbols.Valid() {
528 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
529 }
530 } else {
531 if versionScript.Valid() {
532 ctx.PropertyErrorf("version_script", "Not supported on Darwin")
533 }
534 if unexportedSymbols.Valid() {
535 flags.LdFlags = append(flags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
536 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
537 }
538 if forceNotWeakSymbols.Valid() {
539 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
540 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
541 }
542 if forceWeakSymbols.Valid() {
543 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
544 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
545 }
546 }
547
548 fileName := library.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
549 outputFile := android.PathForModuleOut(ctx, fileName)
550 ret := outputFile
551
552 builderFlags := flagsToBuilderFlags(flags)
553
Colin Crossd8f8d072017-04-04 13:00:15 -0700554 if !ctx.Darwin() && !ctx.Windows() {
Colin Cross89562dc2016-10-03 17:47:19 -0700555 // Optimize out relinking against shared libraries whose interface hasn't changed by
556 // depending on a table of contents file instead of the library itself.
557 tocPath := outputFile.RelPathString()
558 tocPath = pathtools.ReplaceExtension(tocPath, flags.Toolchain.ShlibSuffix()[1:]+".toc")
559 tocFile := android.PathForOutput(ctx, tocPath)
560 library.tocFile = android.OptionalPathForPath(tocFile)
561 TransformSharedObjectToToc(ctx, outputFile, tocFile, builderFlags)
562 }
563
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700564 if library.relocationPacker.needsPacking(ctx) {
565 packedOutputFile := outputFile
566 outputFile = android.PathForModuleOut(ctx, "unpacked", fileName)
567 library.relocationPacker.pack(ctx, outputFile, packedOutputFile, builderFlags)
568 }
569
Colin Cross4d9c2d12016-07-29 12:48:20 -0700570 if library.stripper.needsStrip(ctx) {
571 strippedOutputFile := outputFile
572 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
573 library.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
574 }
575
576 sharedLibs := deps.SharedLibs
577 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
578
Dan Albertd015c4a2016-08-10 14:34:08 -0700579 // TODO(danalbert): Clean this up when soong supports prebuilts.
580 if strings.HasPrefix(ctx.selectedStl(), "ndk_libc++") {
581 libDir := getNdkStlLibDir(ctx, flags.Toolchain, "libc++")
582
583 if strings.HasSuffix(ctx.selectedStl(), "_shared") {
584 deps.StaticLibs = append(deps.StaticLibs,
585 libDir.Join(ctx, "libandroid_support.a"))
586 } else {
587 deps.StaticLibs = append(deps.StaticLibs,
588 libDir.Join(ctx, "libc++abi.a"),
589 libDir.Join(ctx, "libandroid_support.a"))
590 }
591
592 if ctx.Arch().ArchType == android.Arm {
593 deps.StaticLibs = append(deps.StaticLibs,
594 libDir.Join(ctx, "libunwind.a"))
595 }
596 }
597
Colin Cross26c34ed2016-09-30 17:10:16 -0700598 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
599 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700600 linkerDeps = append(linkerDeps, objs.tidyFiles...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700601
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700602 TransformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700603 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
604 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile)
605
Dan Willemsen581341d2017-02-09 16:16:31 -0800606 objs.coverageFiles = append(objs.coverageFiles, deps.StaticLibObjs.coverageFiles...)
607 objs.coverageFiles = append(objs.coverageFiles, deps.WholeStaticLibObjs.coverageFiles...)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800608
609 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.StaticLibObjs.sAbiDumpFiles...)
610 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.WholeStaticLibObjs.sAbiDumpFiles...)
611
Dan Willemsen581341d2017-02-09 16:16:31 -0800612 library.coverageOutputFile = TransformCoverageFilesToLib(ctx, objs, builderFlags, library.getLibName(ctx))
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700613 library.linkSAbiDumpFiles(ctx, objs, fileName, ret)
Dan Willemsen581341d2017-02-09 16:16:31 -0800614
Colin Cross4d9c2d12016-07-29 12:48:20 -0700615 return ret
616}
617
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700618func (library *libraryDecorator) linkSAbiDumpFiles(ctx ModuleContext, objs Objects, fileName string, soFile android.Path) {
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800619 //Also take into account object re-use.
Justin Yun8effde42017-06-23 19:24:43 +0900620 if len(objs.sAbiDumpFiles) > 0 && ctx.createVndkSourceAbiDump() {
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800621 refSourceDumpFile := android.PathForVndkRefAbiDump(ctx, "current", fileName, vndkVsNdk(ctx), true)
622 versionScript := android.OptionalPathForModuleSrc(ctx, library.Properties.Version_script)
623 var symbolFile android.OptionalPath
624 if versionScript.Valid() {
625 symbolFile = versionScript
626 }
Jayant Chowdharya4fce192017-09-06 13:10:03 -0700627 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800628 var SourceAbiFlags []string
629 for _, dir := range exportIncludeDirs.Strings() {
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700630 SourceAbiFlags = append(SourceAbiFlags, "-I"+dir)
631 }
632 for _, reexportedInclude := range extractExportIncludesFromFlags(library.sabi.Properties.ReexportedIncludeFlags) {
633 SourceAbiFlags = append(SourceAbiFlags, reexportedInclude)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800634 }
635 exportedHeaderFlags := strings.Join(SourceAbiFlags, " ")
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700636 library.sAbiOutputFile = TransformDumpToLinkedDump(ctx, objs.sAbiDumpFiles, soFile, symbolFile, "current", fileName, exportedHeaderFlags)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800637 if refSourceDumpFile.Valid() {
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700638 unzippedRefDump := UnzipRefDump(ctx, refSourceDumpFile.Path(), fileName)
639 library.sAbiDiff = SourceAbiDiff(ctx, library.sAbiOutputFile.Path(), unzippedRefDump, fileName)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800640 }
641 }
642}
643
644func vndkVsNdk(ctx ModuleContext) bool {
Jiyong Parkab0fd5f2017-08-03 21:22:50 +0900645 if inList(ctx.baseModuleName(), llndkLibraries) {
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800646 return false
647 }
648 return true
649}
650
Colin Crossb916a382016-07-29 17:28:03 -0700651func (library *libraryDecorator) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700652 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700653
Colin Crossad59e752017-11-16 14:29:11 -0800654 objs = deps.Objs.Copy().Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700655 var out android.Path
Colin Crossa48ab5b2017-02-14 15:28:44 -0800656 if library.static() || library.header() {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700657 out = library.linkStatic(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700658 } else {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700659 out = library.linkShared(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700660 }
661
662 library.exportIncludes(ctx, "-I")
663 library.reexportFlags(deps.ReexportedFlags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700664 library.reexportDeps(deps.ReexportedFlagsDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700665
Nan Zhang0007d812017-11-07 10:57:05 -0800666 if Bool(library.Properties.Aidl.Export_aidl_headers) {
Dan Willemsene1240db2016-11-03 14:28:51 -0700667 if library.baseCompiler.hasSrcExt(".aidl") {
Colin Cross10d22312017-05-03 11:01:58 -0700668 flags := []string{
Dan Willemsene1240db2016-11-03 14:28:51 -0700669 "-I" + android.PathForModuleGen(ctx, "aidl").String(),
Colin Cross10d22312017-05-03 11:01:58 -0700670 }
671 library.reexportFlags(flags)
672 library.reuseExportedFlags = append(library.reuseExportedFlags, flags...)
Dan Willemsene1240db2016-11-03 14:28:51 -0700673 library.reexportDeps(library.baseCompiler.deps) // TODO: restrict to aidl deps
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700674 library.reuseExportedDeps = append(library.reuseExportedDeps, library.baseCompiler.deps...)
Dan Willemsene1240db2016-11-03 14:28:51 -0700675 }
676 }
677
Nan Zhang0007d812017-11-07 10:57:05 -0800678 if Bool(library.Properties.Proto.Export_proto_headers) {
Dan Willemsene1240db2016-11-03 14:28:51 -0700679 if library.baseCompiler.hasSrcExt(".proto") {
Colin Cross10d22312017-05-03 11:01:58 -0700680 flags := []string{
Colin Cross38f794e2017-09-07 10:53:07 -0700681 "-I" + android.ProtoSubDir(ctx).String(),
682 "-I" + android.ProtoDir(ctx).String(),
Colin Cross10d22312017-05-03 11:01:58 -0700683 }
684 library.reexportFlags(flags)
685 library.reuseExportedFlags = append(library.reuseExportedFlags, flags...)
Colin Cross0c461f12016-10-20 16:11:43 -0700686 library.reexportDeps(library.baseCompiler.deps) // TODO: restrict to proto deps
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700687 library.reuseExportedDeps = append(library.reuseExportedDeps, library.baseCompiler.deps...)
Colin Cross0c461f12016-10-20 16:11:43 -0700688 }
689 }
690
Colin Cross4d9c2d12016-07-29 12:48:20 -0700691 return out
692}
693
Colin Crossb916a382016-07-29 17:28:03 -0700694func (library *libraryDecorator) buildStatic() bool {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800695 return library.MutatedProperties.BuildStatic &&
Colin Cross4d9c2d12016-07-29 12:48:20 -0700696 (library.Properties.Static.Enabled == nil || *library.Properties.Static.Enabled)
697}
698
Colin Crossb916a382016-07-29 17:28:03 -0700699func (library *libraryDecorator) buildShared() bool {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800700 return library.MutatedProperties.BuildShared &&
Colin Cross4d9c2d12016-07-29 12:48:20 -0700701 (library.Properties.Shared.Enabled == nil || *library.Properties.Shared.Enabled)
702}
703
Colin Crossb916a382016-07-29 17:28:03 -0700704func (library *libraryDecorator) getWholeStaticMissingDeps() []string {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700705 return library.wholeStaticMissingDeps
706}
707
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700708func (library *libraryDecorator) objs() Objects {
709 return library.objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700710}
711
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700712func (library *libraryDecorator) reuseObjs() (Objects, []string, android.Paths) {
713 return library.reuseObjects, library.reuseExportedFlags, library.reuseExportedDeps
Colin Cross4d9c2d12016-07-29 12:48:20 -0700714}
715
Colin Cross26c34ed2016-09-30 17:10:16 -0700716func (library *libraryDecorator) toc() android.OptionalPath {
717 return library.tocFile
718}
719
Colin Crossb916a382016-07-29 17:28:03 -0700720func (library *libraryDecorator) install(ctx ModuleContext, file android.Path) {
Colin Crossc43ae772017-04-14 15:42:53 -0700721 if library.shared() {
Justin Yun8effde42017-06-23 19:24:43 +0900722 if ctx.Device() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700723 if ctx.useVndk() {
Justin Yun8effde42017-06-23 19:24:43 +0900724 if ctx.isVndkSp() {
725 library.baseInstaller.subDir = "vndk-sp"
726 } else if ctx.isVndk() {
727 library.baseInstaller.subDir = "vndk"
728 }
729 }
730 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700731 library.baseInstaller.install(ctx, file)
732 }
Dan Albertf563d252017-10-13 00:29:00 -0700733
Nan Zhang0007d812017-11-07 10:57:05 -0800734 if Bool(library.Properties.Static_ndk_lib) && library.static() {
Dan Albertf563d252017-10-13 00:29:00 -0700735 installPath := getNdkSysrootBase(ctx).Join(
736 ctx, "usr/lib", ctx.toolchain().ClangTriple(), file.Base())
737
738 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
739 Rule: android.Cp,
740 Description: "install " + installPath.Base(),
741 Output: installPath,
742 Input: file,
743 })
744
745 library.MutatedProperties.NdkSysrootPath = installPath.String()
746 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700747}
748
Colin Crossb916a382016-07-29 17:28:03 -0700749func (library *libraryDecorator) static() bool {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800750 return library.MutatedProperties.VariantIsStatic
Colin Cross4d9c2d12016-07-29 12:48:20 -0700751}
752
Colin Crossa48ab5b2017-02-14 15:28:44 -0800753func (library *libraryDecorator) shared() bool {
754 return library.MutatedProperties.VariantIsShared
755}
756
757func (library *libraryDecorator) header() bool {
758 return !library.static() && !library.shared()
759}
760
761func (library *libraryDecorator) setStatic() {
762 library.MutatedProperties.VariantIsStatic = true
763 library.MutatedProperties.VariantIsShared = false
764}
765
766func (library *libraryDecorator) setShared() {
767 library.MutatedProperties.VariantIsStatic = false
768 library.MutatedProperties.VariantIsShared = true
Colin Crossb916a382016-07-29 17:28:03 -0700769}
770
Colin Crossab3b7322016-12-09 14:46:15 -0800771func (library *libraryDecorator) BuildOnlyStatic() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800772 library.MutatedProperties.BuildShared = false
Colin Crossab3b7322016-12-09 14:46:15 -0800773}
774
775func (library *libraryDecorator) BuildOnlyShared() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800776 library.MutatedProperties.BuildStatic = false
Colin Crossab3b7322016-12-09 14:46:15 -0800777}
778
Colin Cross5950f382016-12-13 12:50:57 -0800779func (library *libraryDecorator) HeaderOnly() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800780 library.MutatedProperties.BuildShared = false
781 library.MutatedProperties.BuildStatic = false
Colin Cross5950f382016-12-13 12:50:57 -0800782}
783
Colin Crossab3b7322016-12-09 14:46:15 -0800784func NewLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700785 module := newModule(hod, android.MultilibBoth)
786
Colin Crossb916a382016-07-29 17:28:03 -0700787 library := &libraryDecorator{
Colin Crossa48ab5b2017-02-14 15:28:44 -0800788 MutatedProperties: LibraryMutatedProperties{
Colin Crossab3b7322016-12-09 14:46:15 -0800789 BuildShared: true,
790 BuildStatic: true,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700791 },
Colin Crossb916a382016-07-29 17:28:03 -0700792 baseCompiler: NewBaseCompiler(),
793 baseLinker: NewBaseLinker(),
794 baseInstaller: NewBaseInstaller("lib", "lib64", InstallInSystem),
795 sanitize: module.sanitize,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800796 sabi: module.sabi,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700797 }
798
Colin Crossb916a382016-07-29 17:28:03 -0700799 module.compiler = library
800 module.linker = library
801 module.installer = library
802
803 return module, library
804}
805
Colin Cross10d22312017-05-03 11:01:58 -0700806// connects a shared library to a static library in order to reuse its .o files to avoid
807// compiling source files twice.
808func reuseStaticLibrary(mctx android.BottomUpMutatorContext, static, shared *Module) {
809 if staticCompiler, ok := static.compiler.(*libraryDecorator); ok {
810 sharedCompiler := shared.compiler.(*libraryDecorator)
811 if len(staticCompiler.Properties.Static.Cflags) == 0 &&
812 len(sharedCompiler.Properties.Shared.Cflags) == 0 {
813
814 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
815 sharedCompiler.baseCompiler.Properties.OriginalSrcs =
816 sharedCompiler.baseCompiler.Properties.Srcs
817 sharedCompiler.baseCompiler.Properties.Srcs = nil
818 sharedCompiler.baseCompiler.Properties.Generated_sources = nil
819 }
820 }
821}
822
Colin Crossb916a382016-07-29 17:28:03 -0700823func linkageMutator(mctx android.BottomUpMutatorContext) {
824 if m, ok := mctx.Module().(*Module); ok && m.linker != nil {
825 if library, ok := m.linker.(libraryInterface); ok {
826 var modules []blueprint.Module
827 if library.buildStatic() && library.buildShared() {
828 modules = mctx.CreateLocalVariations("static", "shared")
829 static := modules[0].(*Module)
830 shared := modules[1].(*Module)
831
Colin Crossa48ab5b2017-02-14 15:28:44 -0800832 static.linker.(libraryInterface).setStatic()
833 shared.linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700834
Colin Cross10d22312017-05-03 11:01:58 -0700835 reuseStaticLibrary(mctx, static, shared)
836
Colin Crossb916a382016-07-29 17:28:03 -0700837 } else if library.buildStatic() {
838 modules = mctx.CreateLocalVariations("static")
Colin Crossa48ab5b2017-02-14 15:28:44 -0800839 modules[0].(*Module).linker.(libraryInterface).setStatic()
Colin Crossb916a382016-07-29 17:28:03 -0700840 } else if library.buildShared() {
841 modules = mctx.CreateLocalVariations("shared")
Colin Crossa48ab5b2017-02-14 15:28:44 -0800842 modules[0].(*Module).linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700843 }
844 }
845 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700846}