blob: aafe365ae6c727c60511dadb7b48495561124ee8 [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"
Dan Albertea4b7b92018-04-25 16:05:30 -070024 "android/soong/cc/config"
Jiyong Park7ed9de32018-10-15 22:25:07 +090025 "android/soong/genrule"
Colin Cross4d9c2d12016-07-29 12:48:20 -070026)
27
Colin Crossb916a382016-07-29 17:28:03 -070028type LibraryProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -070029 Static struct {
Colin Cross2f336352016-10-26 10:03:47 -070030 Srcs []string `android:"arch_variant"`
31 Cflags []string `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070032
Colin Cross4d9c2d12016-07-29 12:48:20 -070033 Enabled *bool `android:"arch_variant"`
34 Whole_static_libs []string `android:"arch_variant"`
35 Static_libs []string `android:"arch_variant"`
36 Shared_libs []string `android:"arch_variant"`
37 } `android:"arch_variant"`
38 Shared struct {
Colin Cross2f336352016-10-26 10:03:47 -070039 Srcs []string `android:"arch_variant"`
40 Cflags []string `android:"arch_variant"`
Colin Crossb916a382016-07-29 17:28:03 -070041
Colin Cross4d9c2d12016-07-29 12:48:20 -070042 Enabled *bool `android:"arch_variant"`
43 Whole_static_libs []string `android:"arch_variant"`
44 Static_libs []string `android:"arch_variant"`
45 Shared_libs []string `android:"arch_variant"`
46 } `android:"arch_variant"`
47
Colin Cross4d9c2d12016-07-29 12:48:20 -070048 // 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 }
Dan Albertf563d252017-10-13 00:29:00 -070067
Nan Zhang0007d812017-11-07 10:57:05 -080068 Static_ndk_lib *bool
Jiyong Park7ed9de32018-10-15 22:25:07 +090069
70 Stubs struct {
71 // Relative path to the symbol map. The symbol map provides the list of
72 // symbols that are exported for stubs variant of this library.
73 Symbol_file *string
74
75 // List versions to generate stubs libs for.
76 Versions []string
77 }
Colin Crossa48ab5b2017-02-14 15:28:44 -080078}
Colin Cross0c461f12016-10-20 16:11:43 -070079
Colin Crossa48ab5b2017-02-14 15:28:44 -080080type LibraryMutatedProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -070081 VariantName string `blueprint:"mutated"`
Colin Crossb916a382016-07-29 17:28:03 -070082
83 // Build a static variant
84 BuildStatic bool `blueprint:"mutated"`
85 // Build a shared variant
86 BuildShared bool `blueprint:"mutated"`
87 // This variant is shared
88 VariantIsShared bool `blueprint:"mutated"`
89 // This variant is static
90 VariantIsStatic bool `blueprint:"mutated"`
Jiyong Park7ed9de32018-10-15 22:25:07 +090091
92 // This variant is a stubs lib
93 BuildStubs bool `blueprint:"mutated"`
94 // Version of the stubs lib
95 StubsVersion string `blueprint:"mutated"`
Colin Crossb916a382016-07-29 17:28:03 -070096}
97
98type FlagExporterProperties struct {
99 // list of directories relative to the Blueprints file that will
Dan Willemsen273af7f2016-11-03 15:53:42 -0700100 // be added to the include path (using -I) for this module and any module that links
Colin Cross5d195602017-10-17 16:15:50 -0700101 // against this module. Directories listed in export_include_dirs do not need to be
102 // listed in local_include_dirs.
Colin Crossb916a382016-07-29 17:28:03 -0700103 Export_include_dirs []string `android:"arch_variant"`
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700104
105 Target struct {
106 Vendor struct {
107 // list of exported include directories, like
108 // export_include_dirs, that will be applied to the
109 // vendor variant of this library. This will overwrite
110 // any other declarations.
Steven Morelandb21df8f2018-01-05 14:42:54 -0800111 Override_export_include_dirs []string
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700112 }
113 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700114}
115
116func init() {
Steven Morelandf9e62162017-11-02 17:00:50 -0700117 android.RegisterModuleType("cc_library_static", LibraryStaticFactory)
118 android.RegisterModuleType("cc_library_shared", LibrarySharedFactory)
119 android.RegisterModuleType("cc_library", LibraryFactory)
120 android.RegisterModuleType("cc_library_host_static", LibraryHostStaticFactory)
121 android.RegisterModuleType("cc_library_host_shared", LibraryHostSharedFactory)
122 android.RegisterModuleType("cc_library_headers", LibraryHeaderFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700123}
124
125// Module factory for combined static + shared libraries, device by default but with possible host
126// support
Steven Morelandf9e62162017-11-02 17:00:50 -0700127func LibraryFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800128 module, _ := NewLibrary(android.HostAndDeviceSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700129 return module.Init()
130}
131
132// Module factory for static libraries
Steven Morelandf9e62162017-11-02 17:00:50 -0700133func LibraryStaticFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800134 module, library := NewLibrary(android.HostAndDeviceSupported)
135 library.BuildOnlyStatic()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700136 return module.Init()
137}
138
139// Module factory for shared libraries
Steven Morelandf9e62162017-11-02 17:00:50 -0700140func LibrarySharedFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800141 module, library := NewLibrary(android.HostAndDeviceSupported)
142 library.BuildOnlyShared()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700143 return module.Init()
144}
145
146// Module factory for host static libraries
Steven Morelandf9e62162017-11-02 17:00:50 -0700147func LibraryHostStaticFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800148 module, library := NewLibrary(android.HostSupported)
149 library.BuildOnlyStatic()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700150 return module.Init()
151}
152
153// Module factory for host shared libraries
Steven Morelandf9e62162017-11-02 17:00:50 -0700154func LibraryHostSharedFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800155 module, library := NewLibrary(android.HostSupported)
156 library.BuildOnlyShared()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700157 return module.Init()
158}
159
Colin Cross5950f382016-12-13 12:50:57 -0800160// Module factory for header-only libraries
Steven Morelandf9e62162017-11-02 17:00:50 -0700161func LibraryHeaderFactory() android.Module {
Colin Cross5950f382016-12-13 12:50:57 -0800162 module, library := NewLibrary(android.HostAndDeviceSupported)
163 library.HeaderOnly()
164 return module.Init()
165}
166
Colin Cross4d9c2d12016-07-29 12:48:20 -0700167type flagExporter struct {
168 Properties FlagExporterProperties
169
Dan Willemsen847dcc72016-09-29 12:13:36 -0700170 flags []string
171 flagsDeps android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700172}
173
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700174func (f *flagExporter) exportedIncludes(ctx ModuleContext) android.Paths {
Steven Morelandb21df8f2018-01-05 14:42:54 -0800175 if ctx.useVndk() && f.Properties.Target.Vendor.Override_export_include_dirs != nil {
176 return android.PathsForModuleSrc(ctx, f.Properties.Target.Vendor.Override_export_include_dirs)
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700177 } else {
178 return android.PathsForModuleSrc(ctx, f.Properties.Export_include_dirs)
179 }
180}
181
Colin Cross4d9c2d12016-07-29 12:48:20 -0700182func (f *flagExporter) exportIncludes(ctx ModuleContext, inc string) {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700183 includeDirs := f.exportedIncludes(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700184 for _, dir := range includeDirs.Strings() {
185 f.flags = append(f.flags, inc+dir)
186 }
187}
188
189func (f *flagExporter) reexportFlags(flags []string) {
190 f.flags = append(f.flags, flags...)
191}
192
Dan Willemsen847dcc72016-09-29 12:13:36 -0700193func (f *flagExporter) reexportDeps(deps android.Paths) {
194 f.flagsDeps = append(f.flagsDeps, deps...)
195}
196
Colin Cross4d9c2d12016-07-29 12:48:20 -0700197func (f *flagExporter) exportedFlags() []string {
198 return f.flags
199}
200
Dan Willemsen847dcc72016-09-29 12:13:36 -0700201func (f *flagExporter) exportedFlagsDeps() android.Paths {
202 return f.flagsDeps
203}
204
Colin Cross4d9c2d12016-07-29 12:48:20 -0700205type exportedFlagsProducer interface {
206 exportedFlags() []string
Dan Willemsen847dcc72016-09-29 12:13:36 -0700207 exportedFlagsDeps() android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700208}
209
210var _ exportedFlagsProducer = (*flagExporter)(nil)
211
Colin Crossb916a382016-07-29 17:28:03 -0700212// libraryDecorator wraps baseCompiler, baseLinker and baseInstaller to provide library-specific
213// functionality: static vs. shared linkage, reusing object files for shared libraries
214type libraryDecorator struct {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800215 Properties LibraryProperties
216 MutatedProperties LibraryMutatedProperties
Colin Cross4d9c2d12016-07-29 12:48:20 -0700217
218 // For reusing static library objects for shared library
Colin Cross10d22312017-05-03 11:01:58 -0700219 reuseObjects Objects
220 reuseExportedFlags []string
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700221 reuseExportedDeps android.Paths
Colin Cross10d22312017-05-03 11:01:58 -0700222
Colin Cross26c34ed2016-09-30 17:10:16 -0700223 // table-of-contents file to optimize out relinking when possible
224 tocFile android.OptionalPath
Colin Cross4d9c2d12016-07-29 12:48:20 -0700225
Colin Cross4d9c2d12016-07-29 12:48:20 -0700226 flagExporter
227 stripper
228
Colin Cross4d9c2d12016-07-29 12:48:20 -0700229 // If we're used as a whole_static_lib, our missing dependencies need
230 // to be given
231 wholeStaticMissingDeps []string
232
233 // For whole_static_libs
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700234 objects Objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700235
236 // Uses the module's name if empty, but can be overridden. Does not include
237 // shlib suffix.
238 libName string
Colin Crossb916a382016-07-29 17:28:03 -0700239
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800240 sabi *sabi
241
Dan Willemsen581341d2017-02-09 16:16:31 -0800242 // Output archive of gcno coverage information files
243 coverageOutputFile android.OptionalPath
244
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800245 // linked Source Abi Dump
246 sAbiOutputFile android.OptionalPath
247
248 // Source Abi Diff
249 sAbiDiff android.OptionalPath
250
Colin Cross0875c522017-11-28 17:34:01 -0800251 // Location of the static library in the sysroot. Empty if the library is
252 // not included in the NDK.
253 ndkSysrootPath android.Path
254
Colin Crossb60190a2018-09-04 16:28:17 -0700255 // Location of the linked, unstripped library for shared libraries
256 unstrippedOutputFile android.Path
257
Jiyong Park7ed9de32018-10-15 22:25:07 +0900258 versionScriptPath android.ModuleGenPath
259
Colin Crossb916a382016-07-29 17:28:03 -0700260 // Decorated interafaces
261 *baseCompiler
262 *baseLinker
263 *baseInstaller
Colin Cross4d9c2d12016-07-29 12:48:20 -0700264}
265
Colin Crossb916a382016-07-29 17:28:03 -0700266func (library *libraryDecorator) linkerProps() []interface{} {
267 var props []interface{}
268 props = append(props, library.baseLinker.linkerProps()...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700269 return append(props,
270 &library.Properties,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800271 &library.MutatedProperties,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700272 &library.flagExporter.Properties,
Colin Cross22f37952018-09-05 10:43:13 -0700273 &library.stripper.StripProperties)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700274}
275
Colin Crossb916a382016-07-29 17:28:03 -0700276func (library *libraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross42742b82016-08-01 13:20:05 -0700277 flags = library.baseLinker.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700278
Colin Crossb916a382016-07-29 17:28:03 -0700279 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
280 // all code is position independent, and then those warnings get promoted to
281 // errors.
Colin Cross3edeee12017-04-04 12:59:48 -0700282 if !ctx.Windows() {
Colin Crossb916a382016-07-29 17:28:03 -0700283 flags.CFlags = append(flags.CFlags, "-fPIC")
284 }
285
286 if library.static() {
287 flags.CFlags = append(flags.CFlags, library.Properties.Static.Cflags...)
Colin Crossa48ab5b2017-02-14 15:28:44 -0800288 } else if library.shared() {
Colin Crossb916a382016-07-29 17:28:03 -0700289 flags.CFlags = append(flags.CFlags, library.Properties.Shared.Cflags...)
290 }
291
Colin Crossa48ab5b2017-02-14 15:28:44 -0800292 if library.shared() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700293 libName := library.getLibName(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700294 var f []string
Dan Willemsen01a405a2016-06-13 17:19:03 -0700295 if ctx.toolchain().Bionic() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700296 f = append(f,
297 "-nostdlib",
298 "-Wl,--gc-sections",
299 )
300 }
301
302 if ctx.Darwin() {
303 f = append(f,
304 "-dynamiclib",
305 "-single_module",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700306 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
307 )
Colin Cross7863cf52016-10-20 10:47:21 -0700308 if ctx.Arch().ArchType == android.X86 {
309 f = append(f,
310 "-read_only_relocs suppress",
311 )
312 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700313 } else {
314 f = append(f,
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700315 "-shared",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700316 "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix())
317 }
318
319 flags.LdFlags = append(f, flags.LdFlags...)
320 }
321
322 return flags
323}
324
Colin Crossf18e1102017-11-16 14:33:08 -0800325func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700326 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700327 if len(exportIncludeDirs) > 0 {
Colin Crossdad8c952017-04-26 14:55:27 -0700328 f := includeDirsToFlags(exportIncludeDirs)
329 flags.GlobalFlags = append(flags.GlobalFlags, f)
330 flags.YasmFlags = append(flags.YasmFlags, f)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700331 }
332
Jiyong Park7ed9de32018-10-15 22:25:07 +0900333 flags = library.baseCompiler.compilerFlags(ctx, flags, deps)
334 if library.buildStubs() {
335 flags = addStubLibraryCompilerFlags(flags)
336 }
337 return flags
Dan Willemsen273af7f2016-11-03 15:53:42 -0700338}
339
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700340func extractExportIncludesFromFlags(flags []string) []string {
341 // This method is used in the generation of rules which produce
342 // abi-dumps for source files. Exported headers are needed to infer the
343 // abi exported by a library and filter out the rest of the abi dumped
344 // from a source. We extract the include flags exported by a library.
345 // This includes the flags exported which are re-exported from static
346 // library dependencies, exported header library dependencies and
Jayant Chowdharyaf6eb712017-08-23 16:08:29 -0700347 // generated header dependencies. -isystem headers are not included
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700348 // since for bionic libraries, abi-filtering is taken care of by version
349 // scripts.
350 var exportedIncludes []string
351 for _, flag := range flags {
352 if strings.HasPrefix(flag, "-I") {
353 exportedIncludes = append(exportedIncludes, flag)
354 }
355 }
356 return exportedIncludes
357}
358
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700359func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Jiyong Park7ed9de32018-10-15 22:25:07 +0900360 if library.buildStubs() {
361 objs, versionScript := compileStubLibrary(ctx, flags, String(library.Properties.Stubs.Symbol_file), library.MutatedProperties.StubsVersion, "")
362 library.versionScriptPath = versionScript
363 return objs
364 }
365
Colin Cross5950f382016-12-13 12:50:57 -0800366 if !library.buildShared() && !library.buildStatic() {
367 if len(library.baseCompiler.Properties.Srcs) > 0 {
368 ctx.PropertyErrorf("srcs", "cc_library_headers must not have any srcs")
369 }
370 if len(library.Properties.Static.Srcs) > 0 {
371 ctx.PropertyErrorf("static.srcs", "cc_library_headers must not have any srcs")
372 }
373 if len(library.Properties.Shared.Srcs) > 0 {
374 ctx.PropertyErrorf("shared.srcs", "cc_library_headers must not have any srcs")
375 }
376 return Objects{}
377 }
Logan Chien2f2b8902018-07-10 15:01:19 +0800378 if ctx.shouldCreateVndkSourceAbiDump() || library.sabi.Properties.CreateSAbiDumps {
Jayant Chowdharya4fce192017-09-06 13:10:03 -0700379 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800380 var SourceAbiFlags []string
381 for _, dir := range exportIncludeDirs.Strings() {
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700382 SourceAbiFlags = append(SourceAbiFlags, "-I"+dir)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800383 }
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700384 for _, reexportedInclude := range extractExportIncludesFromFlags(library.sabi.Properties.ReexportedIncludeFlags) {
385 SourceAbiFlags = append(SourceAbiFlags, reexportedInclude)
386 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800387 flags.SAbiFlags = SourceAbiFlags
388 total_length := len(library.baseCompiler.Properties.Srcs) + len(deps.GeneratedSources) + len(library.Properties.Shared.Srcs) +
389 len(library.Properties.Static.Srcs)
390 if total_length > 0 {
391 flags.SAbiDump = true
392 }
393 }
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700394 objs := library.baseCompiler.compile(ctx, flags, deps)
395 library.reuseObjects = objs
Colin Cross2f336352016-10-26 10:03:47 -0700396 buildFlags := flagsToBuilderFlags(flags)
Colin Crossb916a382016-07-29 17:28:03 -0700397
Colin Cross4d9c2d12016-07-29 12:48:20 -0700398 if library.static() {
Colin Cross2f336352016-10-26 10:03:47 -0700399 srcs := android.PathsForModuleSrc(ctx, library.Properties.Static.Srcs)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700400 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceStaticLibrary,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800401 srcs, library.baseCompiler.pathDeps, library.baseCompiler.cFlagsDeps))
Colin Crossa48ab5b2017-02-14 15:28:44 -0800402 } else if library.shared() {
Colin Cross2f336352016-10-26 10:03:47 -0700403 srcs := android.PathsForModuleSrc(ctx, library.Properties.Shared.Srcs)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700404 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceSharedLibrary,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800405 srcs, library.baseCompiler.pathDeps, library.baseCompiler.cFlagsDeps))
Colin Crossb916a382016-07-29 17:28:03 -0700406 }
407
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700408 return objs
Colin Crossb916a382016-07-29 17:28:03 -0700409}
410
411type libraryInterface interface {
412 getWholeStaticMissingDeps() []string
413 static() bool
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700414 objs() Objects
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700415 reuseObjs() (Objects, []string, android.Paths)
Colin Cross26c34ed2016-09-30 17:10:16 -0700416 toc() android.OptionalPath
Colin Crossb916a382016-07-29 17:28:03 -0700417
418 // Returns true if the build options for the module have selected a static or shared build
419 buildStatic() bool
420 buildShared() bool
421
422 // Sets whether a specific variant is static or shared
Colin Crossa48ab5b2017-02-14 15:28:44 -0800423 setStatic()
424 setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700425}
426
427func (library *libraryDecorator) getLibName(ctx ModuleContext) string {
428 name := library.libName
429 if name == "" {
Colin Crossce75d2c2016-10-06 16:12:58 -0700430 name = ctx.baseModuleName()
Colin Crossb916a382016-07-29 17:28:03 -0700431 }
432
Logan Chienf3511742017-10-31 18:04:35 +0800433 if ctx.isVndkExt() {
434 name = ctx.getVndkExtendsModuleName()
435 }
436
Colin Crossb916a382016-07-29 17:28:03 -0700437 if ctx.Host() && Bool(library.Properties.Unique_host_soname) {
438 if !strings.HasSuffix(name, "-host") {
439 name = name + "-host"
440 }
441 }
442
Colin Crossa48ab5b2017-02-14 15:28:44 -0800443 return name + library.MutatedProperties.VariantName
Colin Crossb916a382016-07-29 17:28:03 -0700444}
445
446func (library *libraryDecorator) linkerInit(ctx BaseModuleContext) {
447 location := InstallInSystem
Dan Albert61f32122018-07-26 14:00:24 -0700448 if library.baseLinker.sanitize.inSanitizerDir() {
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700449 location = InstallInSanitizerDir
Colin Crossb916a382016-07-29 17:28:03 -0700450 }
451 library.baseInstaller.location = location
Colin Crossb916a382016-07-29 17:28:03 -0700452 library.baseLinker.linkerInit(ctx)
Jiyong Park7ed9de32018-10-15 22:25:07 +0900453 // Let baseLinker know whether this variant is for stubs or not, so that
454 // it can omit things that are not required for linking stubs.
455 library.baseLinker.dynamicProperties.BuildStubs = library.buildStubs()
Colin Crossb916a382016-07-29 17:28:03 -0700456}
457
Colin Cross37047f12016-12-13 17:06:13 -0800458func (library *libraryDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700459 deps = library.baseLinker.linkerDeps(ctx, deps)
460
461 if library.static() {
462 deps.WholeStaticLibs = append(deps.WholeStaticLibs,
463 library.Properties.Static.Whole_static_libs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700464 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Static.Static_libs...)
465 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Static.Shared_libs...)
Colin Crossa48ab5b2017-02-14 15:28:44 -0800466 } else if library.shared() {
Dan Willemsen2e47b342016-11-17 01:02:25 -0800467 if ctx.toolchain().Bionic() && !Bool(library.baseLinker.Properties.Nocrt) {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700468 if !ctx.useSdk() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700469 deps.CrtBegin = "crtbegin_so"
470 deps.CrtEnd = "crtend_so"
471 } else {
Dan Albertebedf672016-11-08 15:06:22 -0800472 // TODO(danalbert): Add generation of crt objects.
473 // For `sdk_version: "current"`, we don't actually have a
474 // freshly generated set of CRT objects. Use the last stable
475 // version.
476 version := ctx.sdkVersion()
477 if version == "current" {
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -0700478 version = getCurrentNdkPrebuiltVersion(ctx)
Dan Albertebedf672016-11-08 15:06:22 -0800479 }
480 deps.CrtBegin = "ndk_crtbegin_so." + version
481 deps.CrtEnd = "ndk_crtend_so." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700482 }
483 }
484 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Shared.Whole_static_libs...)
485 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Shared.Static_libs...)
486 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Shared.Shared_libs...)
487 }
Jiyong Park52d25bd2017-10-13 09:17:01 +0900488 if ctx.useVndk() {
489 deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs)
490 deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Vendor.Exclude_shared_libs)
491 deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs)
492 }
Jiyong Parkf9332f12018-02-01 00:54:12 +0900493 if ctx.inRecovery() {
494 deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Recovery.Exclude_static_libs)
495 deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Recovery.Exclude_shared_libs)
496 deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Recovery.Exclude_static_libs)
497 }
Colin Cross2383f3b2018-02-06 14:40:13 -0800498
Colin Cross2383f3b2018-02-06 14:40:13 -0800499 android.ExtractSourceDeps(ctx, library.Properties.Unexported_symbols_list)
500 android.ExtractSourceDeps(ctx, library.Properties.Force_symbols_not_weak_list)
501 android.ExtractSourceDeps(ctx, library.Properties.Force_symbols_weak_list)
Colin Cross2383f3b2018-02-06 14:40:13 -0800502
Colin Cross4d9c2d12016-07-29 12:48:20 -0700503 return deps
504}
505
Colin Crossb916a382016-07-29 17:28:03 -0700506func (library *libraryDecorator) linkStatic(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700507 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700508
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700509 library.objects = deps.WholeStaticLibObjs.Copy()
510 library.objects = library.objects.Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700511
Colin Cross86803cf2018-02-15 14:12:26 -0800512 fileName := ctx.ModuleName() + library.MutatedProperties.VariantName + staticLibraryExtension
513 outputFile := android.PathForModuleOut(ctx, fileName)
Dan Willemsen581341d2017-02-09 16:16:31 -0800514 builderFlags := flagsToBuilderFlags(flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700515
Colin Cross86803cf2018-02-15 14:12:26 -0800516 if Bool(library.baseLinker.Properties.Use_version_lib) && ctx.Host() {
517 versionedOutputFile := outputFile
518 outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
519 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
520 }
521
Dan Willemsen581341d2017-02-09 16:16:31 -0800522 TransformObjToStaticLib(ctx, library.objects.objFiles, builderFlags, outputFile, objs.tidyFiles)
523
524 library.coverageOutputFile = TransformCoverageFilesToLib(ctx, library.objects, builderFlags,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800525 ctx.ModuleName()+library.MutatedProperties.VariantName)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700526
527 library.wholeStaticMissingDeps = ctx.GetMissingDependencies()
528
529 ctx.CheckbuildFile(outputFile)
530
531 return outputFile
532}
533
Colin Crossb916a382016-07-29 17:28:03 -0700534func (library *libraryDecorator) linkShared(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700535 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700536
537 var linkerDeps android.Paths
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700538 linkerDeps = append(linkerDeps, flags.LdFlagsDeps...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700539
Colin Cross2383f3b2018-02-06 14:40:13 -0800540 unexportedSymbols := ctx.ExpandOptionalSource(library.Properties.Unexported_symbols_list, "unexported_symbols_list")
541 forceNotWeakSymbols := ctx.ExpandOptionalSource(library.Properties.Force_symbols_not_weak_list, "force_symbols_not_weak_list")
542 forceWeakSymbols := ctx.ExpandOptionalSource(library.Properties.Force_symbols_weak_list, "force_symbols_weak_list")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700543 if !ctx.Darwin() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700544 if unexportedSymbols.Valid() {
545 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
546 }
547 if forceNotWeakSymbols.Valid() {
548 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
549 }
550 if forceWeakSymbols.Valid() {
551 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
552 }
553 } else {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700554 if unexportedSymbols.Valid() {
555 flags.LdFlags = append(flags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
556 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
557 }
558 if forceNotWeakSymbols.Valid() {
559 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
560 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
561 }
562 if forceWeakSymbols.Valid() {
563 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
564 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
565 }
566 }
567
568 fileName := library.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
569 outputFile := android.PathForModuleOut(ctx, fileName)
570 ret := outputFile
571
572 builderFlags := flagsToBuilderFlags(flags)
573
Colin Crossb496cfd2018-09-10 16:50:05 -0700574 // Optimize out relinking against shared libraries whose interface hasn't changed by
575 // depending on a table of contents file instead of the library itself.
576 tocPath := outputFile.RelPathString()
577 tocPath = pathtools.ReplaceExtension(tocPath, flags.Toolchain.ShlibSuffix()[1:]+".toc")
578 tocFile := android.PathForOutput(ctx, tocPath)
579 library.tocFile = android.OptionalPathForPath(tocFile)
580 TransformSharedObjectToToc(ctx, outputFile, tocFile, builderFlags)
Colin Cross89562dc2016-10-03 17:47:19 -0700581
Colin Cross4d9c2d12016-07-29 12:48:20 -0700582 if library.stripper.needsStrip(ctx) {
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -0700583 // b/80093681, GNU strip/objcopy bug.
584 // Use llvm-{strip,objcopy} when clang lld is used.
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700585 builderFlags.stripUseLlvmStrip = library.baseLinker.useClangLld(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700586 strippedOutputFile := outputFile
587 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
588 library.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
589 }
590
Colin Crossb60190a2018-09-04 16:28:17 -0700591 library.unstrippedOutputFile = outputFile
592
Colin Cross86803cf2018-02-15 14:12:26 -0800593 if Bool(library.baseLinker.Properties.Use_version_lib) && ctx.Host() {
594 versionedOutputFile := outputFile
595 outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
596 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
597 }
598
Colin Cross4d9c2d12016-07-29 12:48:20 -0700599 sharedLibs := deps.SharedLibs
600 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
601
Colin Cross26c34ed2016-09-30 17:10:16 -0700602 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
603 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700604 linkerDeps = append(linkerDeps, objs.tidyFiles...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700605
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700606 TransformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700607 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
608 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile)
609
Dan Willemsen581341d2017-02-09 16:16:31 -0800610 objs.coverageFiles = append(objs.coverageFiles, deps.StaticLibObjs.coverageFiles...)
611 objs.coverageFiles = append(objs.coverageFiles, deps.WholeStaticLibObjs.coverageFiles...)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800612
613 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.StaticLibObjs.sAbiDumpFiles...)
614 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.WholeStaticLibObjs.sAbiDumpFiles...)
615
Dan Willemsen581341d2017-02-09 16:16:31 -0800616 library.coverageOutputFile = TransformCoverageFilesToLib(ctx, objs, builderFlags, library.getLibName(ctx))
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700617 library.linkSAbiDumpFiles(ctx, objs, fileName, ret)
Dan Willemsen581341d2017-02-09 16:16:31 -0800618
Colin Cross4d9c2d12016-07-29 12:48:20 -0700619 return ret
620}
621
Logan Chien7eefdc42018-07-11 18:10:41 +0800622func getRefAbiDumpFile(ctx ModuleContext, vndkVersion, fileName string) android.Path {
Logan Chienf4b79c62018-08-02 02:27:02 +0800623 isLlndk := inList(ctx.baseModuleName(), llndkLibraries) || inList(ctx.baseModuleName(), ndkMigratedLibs)
Logan Chien7eefdc42018-07-11 18:10:41 +0800624
625 refAbiDumpTextFile := android.PathForVndkRefAbiDump(ctx, vndkVersion, fileName, isLlndk, false)
626 refAbiDumpGzipFile := android.PathForVndkRefAbiDump(ctx, vndkVersion, fileName, isLlndk, true)
627
628 if refAbiDumpTextFile.Valid() {
629 if refAbiDumpGzipFile.Valid() {
630 ctx.ModuleErrorf(
631 "Two reference ABI dump files are found: %q and %q. Please delete the stale one.",
632 refAbiDumpTextFile, refAbiDumpGzipFile)
633 return nil
634 }
635 return refAbiDumpTextFile.Path()
636 }
637 if refAbiDumpGzipFile.Valid() {
638 return UnzipRefDump(ctx, refAbiDumpGzipFile.Path(), fileName)
639 }
640 return nil
641}
642
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700643func (library *libraryDecorator) linkSAbiDumpFiles(ctx ModuleContext, objs Objects, fileName string, soFile android.Path) {
Logan Chien2f2b8902018-07-10 15:01:19 +0800644 if len(objs.sAbiDumpFiles) > 0 && ctx.shouldCreateVndkSourceAbiDump() {
Logan Chiena8f51582018-03-21 11:53:48 +0800645 vndkVersion := ctx.DeviceConfig().PlatformVndkVersion()
646 if ver := ctx.DeviceConfig().VndkVersion(); ver != "" && ver != "current" {
Logan Chienf3511742017-10-31 18:04:35 +0800647 vndkVersion = ver
648 }
649
Jayant Chowdharya4fce192017-09-06 13:10:03 -0700650 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800651 var SourceAbiFlags []string
652 for _, dir := range exportIncludeDirs.Strings() {
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700653 SourceAbiFlags = append(SourceAbiFlags, "-I"+dir)
654 }
655 for _, reexportedInclude := range extractExportIncludesFromFlags(library.sabi.Properties.ReexportedIncludeFlags) {
656 SourceAbiFlags = append(SourceAbiFlags, reexportedInclude)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800657 }
658 exportedHeaderFlags := strings.Join(SourceAbiFlags, " ")
Jayant Chowdharydf344d52018-01-17 11:11:42 -0800659 library.sAbiOutputFile = TransformDumpToLinkedDump(ctx, objs.sAbiDumpFiles, soFile, fileName, exportedHeaderFlags)
Logan Chien2f2b8902018-07-10 15:01:19 +0800660
Logan Chien7eefdc42018-07-11 18:10:41 +0800661 refAbiDumpFile := getRefAbiDumpFile(ctx, vndkVersion, fileName)
662 if refAbiDumpFile != nil {
Logan Chienf3511742017-10-31 18:04:35 +0800663 library.sAbiDiff = SourceAbiDiff(ctx, library.sAbiOutputFile.Path(),
Logan Chien7eefdc42018-07-11 18:10:41 +0800664 refAbiDumpFile, fileName, exportedHeaderFlags, ctx.isVndkExt())
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800665 }
666 }
667}
668
Colin Crossb916a382016-07-29 17:28:03 -0700669func (library *libraryDecorator) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700670 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700671
Colin Crossad59e752017-11-16 14:29:11 -0800672 objs = deps.Objs.Copy().Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700673 var out android.Path
Colin Crossa48ab5b2017-02-14 15:28:44 -0800674 if library.static() || library.header() {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700675 out = library.linkStatic(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700676 } else {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700677 out = library.linkShared(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700678 }
679
680 library.exportIncludes(ctx, "-I")
681 library.reexportFlags(deps.ReexportedFlags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700682 library.reexportDeps(deps.ReexportedFlagsDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700683
Nan Zhang0007d812017-11-07 10:57:05 -0800684 if Bool(library.Properties.Aidl.Export_aidl_headers) {
Dan Willemsene1240db2016-11-03 14:28:51 -0700685 if library.baseCompiler.hasSrcExt(".aidl") {
Colin Cross10d22312017-05-03 11:01:58 -0700686 flags := []string{
Dan Willemsene1240db2016-11-03 14:28:51 -0700687 "-I" + android.PathForModuleGen(ctx, "aidl").String(),
Colin Cross10d22312017-05-03 11:01:58 -0700688 }
689 library.reexportFlags(flags)
690 library.reuseExportedFlags = append(library.reuseExportedFlags, flags...)
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800691 library.reexportDeps(library.baseCompiler.pathDeps) // TODO: restrict to aidl deps
692 library.reuseExportedDeps = append(library.reuseExportedDeps, library.baseCompiler.pathDeps...)
Dan Willemsene1240db2016-11-03 14:28:51 -0700693 }
694 }
695
Nan Zhang0007d812017-11-07 10:57:05 -0800696 if Bool(library.Properties.Proto.Export_proto_headers) {
Dan Willemsene1240db2016-11-03 14:28:51 -0700697 if library.baseCompiler.hasSrcExt(".proto") {
Dan Willemsenab9f4262018-02-14 13:58:34 -0800698 includes := []string{}
699 if flags.ProtoRoot {
700 includes = append(includes, "-I"+android.ProtoSubDir(ctx).String())
Colin Cross10d22312017-05-03 11:01:58 -0700701 }
Dan Willemsenab9f4262018-02-14 13:58:34 -0800702 includes = append(includes, "-I"+android.ProtoDir(ctx).String())
703 library.reexportFlags(includes)
704 library.reuseExportedFlags = append(library.reuseExportedFlags, includes...)
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800705 library.reexportDeps(library.baseCompiler.pathDeps) // TODO: restrict to proto deps
706 library.reuseExportedDeps = append(library.reuseExportedDeps, library.baseCompiler.pathDeps...)
Colin Cross0c461f12016-10-20 16:11:43 -0700707 }
708 }
709
Inseob Kim21f26902018-09-06 00:55:20 +0900710 if library.baseCompiler.hasSrcExt(".sysprop") {
711 flags := []string{
712 "-I" + android.PathForModuleGen(ctx, "sysprop", "include").String(),
713 }
714 library.reexportFlags(flags)
715 library.reuseExportedFlags = append(library.reuseExportedFlags, flags...)
716 }
717
Colin Cross4d9c2d12016-07-29 12:48:20 -0700718 return out
719}
720
Colin Crossb916a382016-07-29 17:28:03 -0700721func (library *libraryDecorator) buildStatic() bool {
Colin Cross38b40df2018-04-10 16:14:46 -0700722 return library.MutatedProperties.BuildStatic && BoolDefault(library.Properties.Static.Enabled, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700723}
724
Colin Crossb916a382016-07-29 17:28:03 -0700725func (library *libraryDecorator) buildShared() bool {
Colin Cross38b40df2018-04-10 16:14:46 -0700726 return library.MutatedProperties.BuildShared && BoolDefault(library.Properties.Shared.Enabled, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700727}
728
Colin Crossb916a382016-07-29 17:28:03 -0700729func (library *libraryDecorator) getWholeStaticMissingDeps() []string {
Colin Crossd2343a32018-04-30 14:50:01 -0700730 return append([]string(nil), library.wholeStaticMissingDeps...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700731}
732
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700733func (library *libraryDecorator) objs() Objects {
734 return library.objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700735}
736
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700737func (library *libraryDecorator) reuseObjs() (Objects, []string, android.Paths) {
738 return library.reuseObjects, library.reuseExportedFlags, library.reuseExportedDeps
Colin Cross4d9c2d12016-07-29 12:48:20 -0700739}
740
Colin Cross26c34ed2016-09-30 17:10:16 -0700741func (library *libraryDecorator) toc() android.OptionalPath {
742 return library.tocFile
743}
744
Colin Crossb916a382016-07-29 17:28:03 -0700745func (library *libraryDecorator) install(ctx ModuleContext, file android.Path) {
Colin Crossc43ae772017-04-14 15:42:53 -0700746 if library.shared() {
Justin Yun8fe12122017-12-07 17:18:15 +0900747 if ctx.Device() && ctx.useVndk() {
748 if ctx.isVndkSp() {
749 library.baseInstaller.subDir = "vndk-sp"
750 } else if ctx.isVndk() {
751 library.baseInstaller.subDir = "vndk"
752 }
Logan Chienf3511742017-10-31 18:04:35 +0800753
754 // Append a version to vndk or vndk-sp directories on the system partition.
755 if ctx.isVndk() && !ctx.isVndkExt() {
756 vndkVersion := ctx.DeviceConfig().PlatformVndkVersion()
757 if vndkVersion != "current" && vndkVersion != "" {
758 library.baseInstaller.subDir += "-" + vndkVersion
759 }
Justin Yun8effde42017-06-23 19:24:43 +0900760 }
761 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700762 library.baseInstaller.install(ctx, file)
763 }
Dan Albertf563d252017-10-13 00:29:00 -0700764
Dan Albert281f22b2017-12-13 15:03:47 -0800765 if Bool(library.Properties.Static_ndk_lib) && library.static() &&
Jiyong Parkf9332f12018-02-01 00:54:12 +0900766 !ctx.useVndk() && !ctx.inRecovery() && ctx.Device() &&
Jiyong Park7ed9de32018-10-15 22:25:07 +0900767 library.baseLinker.sanitize.isUnsanitizedVariant() &&
768 !library.buildStubs() {
Dan Albertf563d252017-10-13 00:29:00 -0700769 installPath := getNdkSysrootBase(ctx).Join(
Dan Albertea4b7b92018-04-25 16:05:30 -0700770 ctx, "usr/lib", config.NDKTriple(ctx.toolchain()), file.Base())
Dan Albertf563d252017-10-13 00:29:00 -0700771
772 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
773 Rule: android.Cp,
774 Description: "install " + installPath.Base(),
775 Output: installPath,
776 Input: file,
777 })
778
Colin Cross0875c522017-11-28 17:34:01 -0800779 library.ndkSysrootPath = installPath
Dan Albertf563d252017-10-13 00:29:00 -0700780 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700781}
782
Colin Crossb916a382016-07-29 17:28:03 -0700783func (library *libraryDecorator) static() bool {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800784 return library.MutatedProperties.VariantIsStatic
Colin Cross4d9c2d12016-07-29 12:48:20 -0700785}
786
Colin Crossa48ab5b2017-02-14 15:28:44 -0800787func (library *libraryDecorator) shared() bool {
788 return library.MutatedProperties.VariantIsShared
789}
790
791func (library *libraryDecorator) header() bool {
792 return !library.static() && !library.shared()
793}
794
795func (library *libraryDecorator) setStatic() {
796 library.MutatedProperties.VariantIsStatic = true
797 library.MutatedProperties.VariantIsShared = false
798}
799
800func (library *libraryDecorator) setShared() {
801 library.MutatedProperties.VariantIsStatic = false
802 library.MutatedProperties.VariantIsShared = true
Colin Crossb916a382016-07-29 17:28:03 -0700803}
804
Colin Crossab3b7322016-12-09 14:46:15 -0800805func (library *libraryDecorator) BuildOnlyStatic() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800806 library.MutatedProperties.BuildShared = false
Colin Crossab3b7322016-12-09 14:46:15 -0800807}
808
809func (library *libraryDecorator) BuildOnlyShared() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800810 library.MutatedProperties.BuildStatic = false
Colin Crossab3b7322016-12-09 14:46:15 -0800811}
812
Colin Cross5950f382016-12-13 12:50:57 -0800813func (library *libraryDecorator) HeaderOnly() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800814 library.MutatedProperties.BuildShared = false
815 library.MutatedProperties.BuildStatic = false
Colin Cross5950f382016-12-13 12:50:57 -0800816}
817
Jiyong Park7ed9de32018-10-15 22:25:07 +0900818func (library *libraryDecorator) buildStubs() bool {
819 return library.MutatedProperties.BuildStubs
820}
821
822func (library *libraryDecorator) stubsVersion() string {
823 return library.MutatedProperties.StubsVersion
824}
825
Colin Crossab3b7322016-12-09 14:46:15 -0800826func NewLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700827 module := newModule(hod, android.MultilibBoth)
828
Colin Crossb916a382016-07-29 17:28:03 -0700829 library := &libraryDecorator{
Colin Crossa48ab5b2017-02-14 15:28:44 -0800830 MutatedProperties: LibraryMutatedProperties{
Colin Crossab3b7322016-12-09 14:46:15 -0800831 BuildShared: true,
832 BuildStatic: true,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700833 },
Colin Crossb916a382016-07-29 17:28:03 -0700834 baseCompiler: NewBaseCompiler(),
Dan Albert61f32122018-07-26 14:00:24 -0700835 baseLinker: NewBaseLinker(module.sanitize),
Colin Crossb916a382016-07-29 17:28:03 -0700836 baseInstaller: NewBaseInstaller("lib", "lib64", InstallInSystem),
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800837 sabi: module.sabi,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700838 }
839
Colin Crossb916a382016-07-29 17:28:03 -0700840 module.compiler = library
841 module.linker = library
842 module.installer = library
843
844 return module, library
845}
846
Colin Cross10d22312017-05-03 11:01:58 -0700847// connects a shared library to a static library in order to reuse its .o files to avoid
848// compiling source files twice.
849func reuseStaticLibrary(mctx android.BottomUpMutatorContext, static, shared *Module) {
850 if staticCompiler, ok := static.compiler.(*libraryDecorator); ok {
851 sharedCompiler := shared.compiler.(*libraryDecorator)
852 if len(staticCompiler.Properties.Static.Cflags) == 0 &&
853 len(sharedCompiler.Properties.Shared.Cflags) == 0 {
854
855 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
856 sharedCompiler.baseCompiler.Properties.OriginalSrcs =
857 sharedCompiler.baseCompiler.Properties.Srcs
858 sharedCompiler.baseCompiler.Properties.Srcs = nil
859 sharedCompiler.baseCompiler.Properties.Generated_sources = nil
860 }
861 }
862}
863
Colin Crosse40b4ea2018-10-02 22:25:58 -0700864func LinkageMutator(mctx android.BottomUpMutatorContext) {
Colin Crossb916a382016-07-29 17:28:03 -0700865 if m, ok := mctx.Module().(*Module); ok && m.linker != nil {
866 if library, ok := m.linker.(libraryInterface); ok {
867 var modules []blueprint.Module
868 if library.buildStatic() && library.buildShared() {
869 modules = mctx.CreateLocalVariations("static", "shared")
870 static := modules[0].(*Module)
871 shared := modules[1].(*Module)
872
Colin Crossa48ab5b2017-02-14 15:28:44 -0800873 static.linker.(libraryInterface).setStatic()
874 shared.linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700875
Colin Cross10d22312017-05-03 11:01:58 -0700876 reuseStaticLibrary(mctx, static, shared)
877
Colin Crossb916a382016-07-29 17:28:03 -0700878 } else if library.buildStatic() {
879 modules = mctx.CreateLocalVariations("static")
Colin Crossa48ab5b2017-02-14 15:28:44 -0800880 modules[0].(*Module).linker.(libraryInterface).setStatic()
Colin Crossb916a382016-07-29 17:28:03 -0700881 } else if library.buildShared() {
882 modules = mctx.CreateLocalVariations("shared")
Colin Crossa48ab5b2017-02-14 15:28:44 -0800883 modules[0].(*Module).linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700884 }
885 }
886 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700887}
Jiyong Park7ed9de32018-10-15 22:25:07 +0900888
889// Version mutator splits a module into the mandatory non-stubs variant
890// (which is named "impl") and zero or more stubs variants.
891func versionMutator(mctx android.BottomUpMutatorContext) {
892 if mctx.Os() != android.Android {
893 return
894 }
895
896 if m, ok := mctx.Module().(*Module); ok && !m.inRecovery() && m.linker != nil {
897 if library, ok := m.linker.(*libraryDecorator); ok && library.buildShared() {
898 versions := []string{""}
899 for _, v := range library.Properties.Stubs.Versions {
900 versions = append(versions, v)
901 }
902 modules := mctx.CreateVariations(versions...)
903 for i, m := range modules {
904 l := m.(*Module).linker.(*libraryDecorator)
905 if i == 0 {
906 l.MutatedProperties.BuildStubs = false
907 continue
908 }
909 // Mark that this variant is for stubs.
910 l.MutatedProperties.BuildStubs = true
911 l.MutatedProperties.StubsVersion = versions[i]
912 m.(*Module).Properties.HideFromMake = true
913 }
914 } else {
915 mctx.CreateVariations("")
916 }
917 return
918 }
919 if genrule, ok := mctx.Module().(*genrule.Module); ok {
920 if props, ok := genrule.Extra.(*GenruleExtraProperties); ok && !props.InRecovery {
921 mctx.CreateVariations("")
922 return
923 }
924 }
925}