blob: 975f3253fe509641e58e5af1695def46ec54dd1c [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 (
Jiyong Parkda732bd2018-11-02 18:23:15 +090018 "regexp"
Colin Cross4d9c2d12016-07-29 12:48:20 -070019 "strings"
Jiyong Parkda732bd2018-11-02 18:23:15 +090020 "sync"
Colin Cross4d9c2d12016-07-29 12:48:20 -070021
22 "github.com/google/blueprint"
Colin Cross26c34ed2016-09-30 17:10:16 -070023 "github.com/google/blueprint/pathtools"
Colin Cross4d9c2d12016-07-29 12:48:20 -070024
Colin Cross4d9c2d12016-07-29 12:48:20 -070025 "android/soong/android"
Dan Albertea4b7b92018-04-25 16:05:30 -070026 "android/soong/cc/config"
Jiyong Park7ed9de32018-10-15 22:25:07 +090027 "android/soong/genrule"
Colin Cross4d9c2d12016-07-29 12:48:20 -070028)
29
Colin Crossb916a382016-07-29 17:28:03 -070030type LibraryProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -070031 Static struct {
Colin Cross2f336352016-10-26 10:03:47 -070032 Srcs []string `android:"arch_variant"`
33 Cflags []string `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070034
Colin Cross4d9c2d12016-07-29 12:48:20 -070035 Enabled *bool `android:"arch_variant"`
36 Whole_static_libs []string `android:"arch_variant"`
37 Static_libs []string `android:"arch_variant"`
38 Shared_libs []string `android:"arch_variant"`
39 } `android:"arch_variant"`
40 Shared struct {
Colin Cross2f336352016-10-26 10:03:47 -070041 Srcs []string `android:"arch_variant"`
42 Cflags []string `android:"arch_variant"`
Colin Crossb916a382016-07-29 17:28:03 -070043
Colin Cross4d9c2d12016-07-29 12:48:20 -070044 Enabled *bool `android:"arch_variant"`
45 Whole_static_libs []string `android:"arch_variant"`
46 Static_libs []string `android:"arch_variant"`
47 Shared_libs []string `android:"arch_variant"`
48 } `android:"arch_variant"`
49
Colin Cross4d9c2d12016-07-29 12:48:20 -070050 // local file name to pass to the linker as -unexported_symbols_list
51 Unexported_symbols_list *string `android:"arch_variant"`
52 // local file name to pass to the linker as -force_symbols_not_weak_list
53 Force_symbols_not_weak_list *string `android:"arch_variant"`
54 // local file name to pass to the linker as -force_symbols_weak_list
55 Force_symbols_weak_list *string `android:"arch_variant"`
56
57 // rename host libraries to prevent overlap with system installed libraries
58 Unique_host_soname *bool
59
Dan Willemsene1240db2016-11-03 14:28:51 -070060 Aidl struct {
61 // export headers generated from .aidl sources
Nan Zhang0007d812017-11-07 10:57:05 -080062 Export_aidl_headers *bool
Dan Willemsene1240db2016-11-03 14:28:51 -070063 }
64
Colin Cross0c461f12016-10-20 16:11:43 -070065 Proto struct {
66 // export headers generated from .proto sources
Nan Zhang0007d812017-11-07 10:57:05 -080067 Export_proto_headers *bool
Colin Cross0c461f12016-10-20 16:11:43 -070068 }
Dan Albertf563d252017-10-13 00:29:00 -070069
Nan Zhang0007d812017-11-07 10:57:05 -080070 Static_ndk_lib *bool
Jiyong Park7ed9de32018-10-15 22:25:07 +090071
72 Stubs struct {
73 // Relative path to the symbol map. The symbol map provides the list of
74 // symbols that are exported for stubs variant of this library.
75 Symbol_file *string
76
77 // List versions to generate stubs libs for.
78 Versions []string
79 }
Colin Crossa48ab5b2017-02-14 15:28:44 -080080}
Colin Cross0c461f12016-10-20 16:11:43 -070081
Colin Crossa48ab5b2017-02-14 15:28:44 -080082type LibraryMutatedProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -070083 VariantName string `blueprint:"mutated"`
Colin Crossb916a382016-07-29 17:28:03 -070084
85 // Build a static variant
86 BuildStatic bool `blueprint:"mutated"`
87 // Build a shared variant
88 BuildShared bool `blueprint:"mutated"`
89 // This variant is shared
90 VariantIsShared bool `blueprint:"mutated"`
91 // This variant is static
92 VariantIsStatic bool `blueprint:"mutated"`
Jiyong Park7ed9de32018-10-15 22:25:07 +090093
94 // This variant is a stubs lib
95 BuildStubs bool `blueprint:"mutated"`
96 // Version of the stubs lib
97 StubsVersion string `blueprint:"mutated"`
Colin Crossb916a382016-07-29 17:28:03 -070098}
99
100type FlagExporterProperties struct {
101 // list of directories relative to the Blueprints file that will
Dan Willemsen273af7f2016-11-03 15:53:42 -0700102 // be added to the include path (using -I) for this module and any module that links
Colin Cross5d195602017-10-17 16:15:50 -0700103 // against this module. Directories listed in export_include_dirs do not need to be
104 // listed in local_include_dirs.
Colin Crossb916a382016-07-29 17:28:03 -0700105 Export_include_dirs []string `android:"arch_variant"`
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700106
107 Target struct {
108 Vendor struct {
109 // list of exported include directories, like
110 // export_include_dirs, that will be applied to the
111 // vendor variant of this library. This will overwrite
112 // any other declarations.
Steven Morelandb21df8f2018-01-05 14:42:54 -0800113 Override_export_include_dirs []string
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700114 }
115 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700116}
117
118func init() {
Steven Morelandf9e62162017-11-02 17:00:50 -0700119 android.RegisterModuleType("cc_library_static", LibraryStaticFactory)
120 android.RegisterModuleType("cc_library_shared", LibrarySharedFactory)
121 android.RegisterModuleType("cc_library", LibraryFactory)
122 android.RegisterModuleType("cc_library_host_static", LibraryHostStaticFactory)
123 android.RegisterModuleType("cc_library_host_shared", LibraryHostSharedFactory)
124 android.RegisterModuleType("cc_library_headers", LibraryHeaderFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700125}
126
127// Module factory for combined static + shared libraries, device by default but with possible host
128// support
Steven Morelandf9e62162017-11-02 17:00:50 -0700129func LibraryFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800130 module, _ := NewLibrary(android.HostAndDeviceSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700131 return module.Init()
132}
133
134// Module factory for static libraries
Steven Morelandf9e62162017-11-02 17:00:50 -0700135func LibraryStaticFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800136 module, library := NewLibrary(android.HostAndDeviceSupported)
137 library.BuildOnlyStatic()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700138 return module.Init()
139}
140
141// Module factory for shared libraries
Steven Morelandf9e62162017-11-02 17:00:50 -0700142func LibrarySharedFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800143 module, library := NewLibrary(android.HostAndDeviceSupported)
144 library.BuildOnlyShared()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700145 return module.Init()
146}
147
148// Module factory for host static libraries
Steven Morelandf9e62162017-11-02 17:00:50 -0700149func LibraryHostStaticFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800150 module, library := NewLibrary(android.HostSupported)
151 library.BuildOnlyStatic()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700152 return module.Init()
153}
154
155// Module factory for host shared libraries
Steven Morelandf9e62162017-11-02 17:00:50 -0700156func LibraryHostSharedFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800157 module, library := NewLibrary(android.HostSupported)
158 library.BuildOnlyShared()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700159 return module.Init()
160}
161
Colin Cross5950f382016-12-13 12:50:57 -0800162// Module factory for header-only libraries
Steven Morelandf9e62162017-11-02 17:00:50 -0700163func LibraryHeaderFactory() android.Module {
Colin Cross5950f382016-12-13 12:50:57 -0800164 module, library := NewLibrary(android.HostAndDeviceSupported)
165 library.HeaderOnly()
166 return module.Init()
167}
168
Colin Cross4d9c2d12016-07-29 12:48:20 -0700169type flagExporter struct {
170 Properties FlagExporterProperties
171
Dan Willemsen847dcc72016-09-29 12:13:36 -0700172 flags []string
173 flagsDeps android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700174}
175
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700176func (f *flagExporter) exportedIncludes(ctx ModuleContext) android.Paths {
Steven Morelandb21df8f2018-01-05 14:42:54 -0800177 if ctx.useVndk() && f.Properties.Target.Vendor.Override_export_include_dirs != nil {
178 return android.PathsForModuleSrc(ctx, f.Properties.Target.Vendor.Override_export_include_dirs)
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700179 } else {
180 return android.PathsForModuleSrc(ctx, f.Properties.Export_include_dirs)
181 }
182}
183
Colin Cross4d9c2d12016-07-29 12:48:20 -0700184func (f *flagExporter) exportIncludes(ctx ModuleContext, inc string) {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700185 includeDirs := f.exportedIncludes(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700186 for _, dir := range includeDirs.Strings() {
187 f.flags = append(f.flags, inc+dir)
188 }
189}
190
191func (f *flagExporter) reexportFlags(flags []string) {
192 f.flags = append(f.flags, flags...)
193}
194
Dan Willemsen847dcc72016-09-29 12:13:36 -0700195func (f *flagExporter) reexportDeps(deps android.Paths) {
196 f.flagsDeps = append(f.flagsDeps, deps...)
197}
198
Colin Cross4d9c2d12016-07-29 12:48:20 -0700199func (f *flagExporter) exportedFlags() []string {
200 return f.flags
201}
202
Dan Willemsen847dcc72016-09-29 12:13:36 -0700203func (f *flagExporter) exportedFlagsDeps() android.Paths {
204 return f.flagsDeps
205}
206
Colin Cross4d9c2d12016-07-29 12:48:20 -0700207type exportedFlagsProducer interface {
208 exportedFlags() []string
Dan Willemsen847dcc72016-09-29 12:13:36 -0700209 exportedFlagsDeps() android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700210}
211
212var _ exportedFlagsProducer = (*flagExporter)(nil)
213
Colin Crossb916a382016-07-29 17:28:03 -0700214// libraryDecorator wraps baseCompiler, baseLinker and baseInstaller to provide library-specific
215// functionality: static vs. shared linkage, reusing object files for shared libraries
216type libraryDecorator struct {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800217 Properties LibraryProperties
218 MutatedProperties LibraryMutatedProperties
Colin Cross4d9c2d12016-07-29 12:48:20 -0700219
220 // For reusing static library objects for shared library
Colin Cross10d22312017-05-03 11:01:58 -0700221 reuseObjects Objects
222 reuseExportedFlags []string
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700223 reuseExportedDeps android.Paths
Colin Cross10d22312017-05-03 11:01:58 -0700224
Colin Cross26c34ed2016-09-30 17:10:16 -0700225 // table-of-contents file to optimize out relinking when possible
226 tocFile android.OptionalPath
Colin Cross4d9c2d12016-07-29 12:48:20 -0700227
Colin Cross4d9c2d12016-07-29 12:48:20 -0700228 flagExporter
229 stripper
230
Colin Cross4d9c2d12016-07-29 12:48:20 -0700231 // If we're used as a whole_static_lib, our missing dependencies need
232 // to be given
233 wholeStaticMissingDeps []string
234
235 // For whole_static_libs
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700236 objects Objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700237
238 // Uses the module's name if empty, but can be overridden. Does not include
239 // shlib suffix.
240 libName string
Colin Crossb916a382016-07-29 17:28:03 -0700241
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800242 sabi *sabi
243
Dan Willemsen581341d2017-02-09 16:16:31 -0800244 // Output archive of gcno coverage information files
245 coverageOutputFile android.OptionalPath
246
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800247 // linked Source Abi Dump
248 sAbiOutputFile android.OptionalPath
249
250 // Source Abi Diff
251 sAbiDiff android.OptionalPath
252
Colin Cross0875c522017-11-28 17:34:01 -0800253 // Location of the static library in the sysroot. Empty if the library is
254 // not included in the NDK.
255 ndkSysrootPath android.Path
256
Colin Crossb60190a2018-09-04 16:28:17 -0700257 // Location of the linked, unstripped library for shared libraries
258 unstrippedOutputFile android.Path
259
Jiyong Park7ed9de32018-10-15 22:25:07 +0900260 versionScriptPath android.ModuleGenPath
261
Colin Crossb916a382016-07-29 17:28:03 -0700262 // Decorated interafaces
263 *baseCompiler
264 *baseLinker
265 *baseInstaller
Colin Cross4d9c2d12016-07-29 12:48:20 -0700266}
267
Colin Crossb916a382016-07-29 17:28:03 -0700268func (library *libraryDecorator) linkerProps() []interface{} {
269 var props []interface{}
270 props = append(props, library.baseLinker.linkerProps()...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700271 return append(props,
272 &library.Properties,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800273 &library.MutatedProperties,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700274 &library.flagExporter.Properties,
Colin Cross22f37952018-09-05 10:43:13 -0700275 &library.stripper.StripProperties)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700276}
277
Colin Crossb916a382016-07-29 17:28:03 -0700278func (library *libraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross42742b82016-08-01 13:20:05 -0700279 flags = library.baseLinker.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700280
Colin Crossb916a382016-07-29 17:28:03 -0700281 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
282 // all code is position independent, and then those warnings get promoted to
283 // errors.
Colin Cross3edeee12017-04-04 12:59:48 -0700284 if !ctx.Windows() {
Colin Crossb916a382016-07-29 17:28:03 -0700285 flags.CFlags = append(flags.CFlags, "-fPIC")
286 }
287
288 if library.static() {
289 flags.CFlags = append(flags.CFlags, library.Properties.Static.Cflags...)
Colin Crossa48ab5b2017-02-14 15:28:44 -0800290 } else if library.shared() {
Colin Crossb916a382016-07-29 17:28:03 -0700291 flags.CFlags = append(flags.CFlags, library.Properties.Shared.Cflags...)
292 }
293
Colin Crossa48ab5b2017-02-14 15:28:44 -0800294 if library.shared() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700295 libName := library.getLibName(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700296 var f []string
Dan Willemsen01a405a2016-06-13 17:19:03 -0700297 if ctx.toolchain().Bionic() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700298 f = append(f,
299 "-nostdlib",
300 "-Wl,--gc-sections",
301 )
302 }
303
304 if ctx.Darwin() {
305 f = append(f,
306 "-dynamiclib",
307 "-single_module",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700308 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
309 )
Colin Cross7863cf52016-10-20 10:47:21 -0700310 if ctx.Arch().ArchType == android.X86 {
311 f = append(f,
312 "-read_only_relocs suppress",
313 )
314 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700315 } else {
316 f = append(f,
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700317 "-shared",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700318 "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix())
319 }
320
321 flags.LdFlags = append(f, flags.LdFlags...)
322 }
323
324 return flags
325}
326
Colin Crossf18e1102017-11-16 14:33:08 -0800327func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700328 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700329 if len(exportIncludeDirs) > 0 {
Colin Crossdad8c952017-04-26 14:55:27 -0700330 f := includeDirsToFlags(exportIncludeDirs)
331 flags.GlobalFlags = append(flags.GlobalFlags, f)
332 flags.YasmFlags = append(flags.YasmFlags, f)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700333 }
334
Jiyong Park7ed9de32018-10-15 22:25:07 +0900335 flags = library.baseCompiler.compilerFlags(ctx, flags, deps)
336 if library.buildStubs() {
337 flags = addStubLibraryCompilerFlags(flags)
338 }
339 return flags
Dan Willemsen273af7f2016-11-03 15:53:42 -0700340}
341
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700342func extractExportIncludesFromFlags(flags []string) []string {
343 // This method is used in the generation of rules which produce
344 // abi-dumps for source files. Exported headers are needed to infer the
345 // abi exported by a library and filter out the rest of the abi dumped
346 // from a source. We extract the include flags exported by a library.
347 // This includes the flags exported which are re-exported from static
348 // library dependencies, exported header library dependencies and
Jayant Chowdharyaf6eb712017-08-23 16:08:29 -0700349 // generated header dependencies. -isystem headers are not included
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700350 // since for bionic libraries, abi-filtering is taken care of by version
351 // scripts.
352 var exportedIncludes []string
353 for _, flag := range flags {
354 if strings.HasPrefix(flag, "-I") {
355 exportedIncludes = append(exportedIncludes, flag)
356 }
357 }
358 return exportedIncludes
359}
360
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700361func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Jiyong Park7ed9de32018-10-15 22:25:07 +0900362 if library.buildStubs() {
363 objs, versionScript := compileStubLibrary(ctx, flags, String(library.Properties.Stubs.Symbol_file), library.MutatedProperties.StubsVersion, "")
364 library.versionScriptPath = versionScript
365 return objs
366 }
367
Colin Cross5950f382016-12-13 12:50:57 -0800368 if !library.buildShared() && !library.buildStatic() {
369 if len(library.baseCompiler.Properties.Srcs) > 0 {
370 ctx.PropertyErrorf("srcs", "cc_library_headers must not have any srcs")
371 }
372 if len(library.Properties.Static.Srcs) > 0 {
373 ctx.PropertyErrorf("static.srcs", "cc_library_headers must not have any srcs")
374 }
375 if len(library.Properties.Shared.Srcs) > 0 {
376 ctx.PropertyErrorf("shared.srcs", "cc_library_headers must not have any srcs")
377 }
378 return Objects{}
379 }
Logan Chien2f2b8902018-07-10 15:01:19 +0800380 if ctx.shouldCreateVndkSourceAbiDump() || library.sabi.Properties.CreateSAbiDumps {
Jayant Chowdharya4fce192017-09-06 13:10:03 -0700381 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800382 var SourceAbiFlags []string
383 for _, dir := range exportIncludeDirs.Strings() {
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700384 SourceAbiFlags = append(SourceAbiFlags, "-I"+dir)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800385 }
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700386 for _, reexportedInclude := range extractExportIncludesFromFlags(library.sabi.Properties.ReexportedIncludeFlags) {
387 SourceAbiFlags = append(SourceAbiFlags, reexportedInclude)
388 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800389 flags.SAbiFlags = SourceAbiFlags
390 total_length := len(library.baseCompiler.Properties.Srcs) + len(deps.GeneratedSources) + len(library.Properties.Shared.Srcs) +
391 len(library.Properties.Static.Srcs)
392 if total_length > 0 {
393 flags.SAbiDump = true
394 }
395 }
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700396 objs := library.baseCompiler.compile(ctx, flags, deps)
397 library.reuseObjects = objs
Colin Cross2f336352016-10-26 10:03:47 -0700398 buildFlags := flagsToBuilderFlags(flags)
Colin Crossb916a382016-07-29 17:28:03 -0700399
Colin Cross4d9c2d12016-07-29 12:48:20 -0700400 if library.static() {
Colin Cross2f336352016-10-26 10:03:47 -0700401 srcs := android.PathsForModuleSrc(ctx, library.Properties.Static.Srcs)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700402 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceStaticLibrary,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800403 srcs, library.baseCompiler.pathDeps, library.baseCompiler.cFlagsDeps))
Colin Crossa48ab5b2017-02-14 15:28:44 -0800404 } else if library.shared() {
Colin Cross2f336352016-10-26 10:03:47 -0700405 srcs := android.PathsForModuleSrc(ctx, library.Properties.Shared.Srcs)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700406 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceSharedLibrary,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800407 srcs, library.baseCompiler.pathDeps, library.baseCompiler.cFlagsDeps))
Colin Crossb916a382016-07-29 17:28:03 -0700408 }
409
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700410 return objs
Colin Crossb916a382016-07-29 17:28:03 -0700411}
412
413type libraryInterface interface {
414 getWholeStaticMissingDeps() []string
415 static() bool
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700416 objs() Objects
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700417 reuseObjs() (Objects, []string, android.Paths)
Colin Cross26c34ed2016-09-30 17:10:16 -0700418 toc() android.OptionalPath
Colin Crossb916a382016-07-29 17:28:03 -0700419
420 // Returns true if the build options for the module have selected a static or shared build
421 buildStatic() bool
422 buildShared() bool
423
424 // Sets whether a specific variant is static or shared
Colin Crossa48ab5b2017-02-14 15:28:44 -0800425 setStatic()
426 setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700427}
428
429func (library *libraryDecorator) getLibName(ctx ModuleContext) string {
430 name := library.libName
431 if name == "" {
Colin Crossce75d2c2016-10-06 16:12:58 -0700432 name = ctx.baseModuleName()
Colin Crossb916a382016-07-29 17:28:03 -0700433 }
434
Logan Chienf3511742017-10-31 18:04:35 +0800435 if ctx.isVndkExt() {
436 name = ctx.getVndkExtendsModuleName()
437 }
438
Colin Crossb916a382016-07-29 17:28:03 -0700439 if ctx.Host() && Bool(library.Properties.Unique_host_soname) {
440 if !strings.HasSuffix(name, "-host") {
441 name = name + "-host"
442 }
443 }
444
Colin Crossa48ab5b2017-02-14 15:28:44 -0800445 return name + library.MutatedProperties.VariantName
Colin Crossb916a382016-07-29 17:28:03 -0700446}
447
Jiyong Parkda732bd2018-11-02 18:23:15 +0900448var versioningMacroNamesListMutex sync.Mutex
449
Colin Crossb916a382016-07-29 17:28:03 -0700450func (library *libraryDecorator) linkerInit(ctx BaseModuleContext) {
451 location := InstallInSystem
Dan Albert61f32122018-07-26 14:00:24 -0700452 if library.baseLinker.sanitize.inSanitizerDir() {
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700453 location = InstallInSanitizerDir
Colin Crossb916a382016-07-29 17:28:03 -0700454 }
455 library.baseInstaller.location = location
Colin Crossb916a382016-07-29 17:28:03 -0700456 library.baseLinker.linkerInit(ctx)
Jiyong Park7ed9de32018-10-15 22:25:07 +0900457 // Let baseLinker know whether this variant is for stubs or not, so that
458 // it can omit things that are not required for linking stubs.
459 library.baseLinker.dynamicProperties.BuildStubs = library.buildStubs()
Jiyong Parkda732bd2018-11-02 18:23:15 +0900460
461 if library.buildStubs() {
462 macroNames := versioningMacroNamesList(ctx.Config())
463 myName := versioningMacroName(ctx.ModuleName())
464 versioningMacroNamesListMutex.Lock()
465 defer versioningMacroNamesListMutex.Unlock()
466 if (*macroNames)[myName] == "" {
467 (*macroNames)[myName] = ctx.ModuleName()
468 } else if (*macroNames)[myName] != ctx.ModuleName() {
469 ctx.ModuleErrorf("Macro name %q for versioning conflicts with macro name from module %q ", myName, (*macroNames)[myName])
470 }
471 }
Colin Crossb916a382016-07-29 17:28:03 -0700472}
473
Colin Cross37047f12016-12-13 17:06:13 -0800474func (library *libraryDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700475 deps = library.baseLinker.linkerDeps(ctx, deps)
476
477 if library.static() {
478 deps.WholeStaticLibs = append(deps.WholeStaticLibs,
479 library.Properties.Static.Whole_static_libs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700480 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Static.Static_libs...)
481 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Static.Shared_libs...)
Colin Crossa48ab5b2017-02-14 15:28:44 -0800482 } else if library.shared() {
Dan Willemsen2e47b342016-11-17 01:02:25 -0800483 if ctx.toolchain().Bionic() && !Bool(library.baseLinker.Properties.Nocrt) {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700484 if !ctx.useSdk() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700485 deps.CrtBegin = "crtbegin_so"
486 deps.CrtEnd = "crtend_so"
487 } else {
Dan Albertebedf672016-11-08 15:06:22 -0800488 // TODO(danalbert): Add generation of crt objects.
489 // For `sdk_version: "current"`, we don't actually have a
490 // freshly generated set of CRT objects. Use the last stable
491 // version.
492 version := ctx.sdkVersion()
493 if version == "current" {
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -0700494 version = getCurrentNdkPrebuiltVersion(ctx)
Dan Albertebedf672016-11-08 15:06:22 -0800495 }
496 deps.CrtBegin = "ndk_crtbegin_so." + version
497 deps.CrtEnd = "ndk_crtend_so." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700498 }
499 }
500 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Shared.Whole_static_libs...)
501 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Shared.Static_libs...)
502 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Shared.Shared_libs...)
503 }
Jiyong Park52d25bd2017-10-13 09:17:01 +0900504 if ctx.useVndk() {
505 deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs)
506 deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Vendor.Exclude_shared_libs)
507 deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs)
508 }
Jiyong Parkf9332f12018-02-01 00:54:12 +0900509 if ctx.inRecovery() {
510 deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Recovery.Exclude_static_libs)
511 deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Recovery.Exclude_shared_libs)
512 deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Recovery.Exclude_static_libs)
513 }
Colin Cross2383f3b2018-02-06 14:40:13 -0800514
Colin Cross2383f3b2018-02-06 14:40:13 -0800515 android.ExtractSourceDeps(ctx, library.Properties.Unexported_symbols_list)
516 android.ExtractSourceDeps(ctx, library.Properties.Force_symbols_not_weak_list)
517 android.ExtractSourceDeps(ctx, library.Properties.Force_symbols_weak_list)
Colin Cross2383f3b2018-02-06 14:40:13 -0800518
Colin Cross4d9c2d12016-07-29 12:48:20 -0700519 return deps
520}
521
Colin Crossb916a382016-07-29 17:28:03 -0700522func (library *libraryDecorator) linkStatic(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700523 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700524
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700525 library.objects = deps.WholeStaticLibObjs.Copy()
526 library.objects = library.objects.Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700527
Colin Cross86803cf2018-02-15 14:12:26 -0800528 fileName := ctx.ModuleName() + library.MutatedProperties.VariantName + staticLibraryExtension
529 outputFile := android.PathForModuleOut(ctx, fileName)
Dan Willemsen581341d2017-02-09 16:16:31 -0800530 builderFlags := flagsToBuilderFlags(flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700531
Colin Cross86803cf2018-02-15 14:12:26 -0800532 if Bool(library.baseLinker.Properties.Use_version_lib) && ctx.Host() {
533 versionedOutputFile := outputFile
534 outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
535 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
536 }
537
Dan Willemsen581341d2017-02-09 16:16:31 -0800538 TransformObjToStaticLib(ctx, library.objects.objFiles, builderFlags, outputFile, objs.tidyFiles)
539
540 library.coverageOutputFile = TransformCoverageFilesToLib(ctx, library.objects, builderFlags,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800541 ctx.ModuleName()+library.MutatedProperties.VariantName)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700542
543 library.wholeStaticMissingDeps = ctx.GetMissingDependencies()
544
545 ctx.CheckbuildFile(outputFile)
546
547 return outputFile
548}
549
Colin Crossb916a382016-07-29 17:28:03 -0700550func (library *libraryDecorator) linkShared(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700551 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700552
553 var linkerDeps android.Paths
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700554 linkerDeps = append(linkerDeps, flags.LdFlagsDeps...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700555
Colin Cross2383f3b2018-02-06 14:40:13 -0800556 unexportedSymbols := ctx.ExpandOptionalSource(library.Properties.Unexported_symbols_list, "unexported_symbols_list")
557 forceNotWeakSymbols := ctx.ExpandOptionalSource(library.Properties.Force_symbols_not_weak_list, "force_symbols_not_weak_list")
558 forceWeakSymbols := ctx.ExpandOptionalSource(library.Properties.Force_symbols_weak_list, "force_symbols_weak_list")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700559 if !ctx.Darwin() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700560 if unexportedSymbols.Valid() {
561 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
562 }
563 if forceNotWeakSymbols.Valid() {
564 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
565 }
566 if forceWeakSymbols.Valid() {
567 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
568 }
569 } else {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700570 if unexportedSymbols.Valid() {
571 flags.LdFlags = append(flags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
572 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
573 }
574 if forceNotWeakSymbols.Valid() {
575 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
576 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
577 }
578 if forceWeakSymbols.Valid() {
579 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
580 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
581 }
582 }
583
584 fileName := library.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
585 outputFile := android.PathForModuleOut(ctx, fileName)
586 ret := outputFile
587
588 builderFlags := flagsToBuilderFlags(flags)
589
Colin Crossb496cfd2018-09-10 16:50:05 -0700590 // Optimize out relinking against shared libraries whose interface hasn't changed by
591 // depending on a table of contents file instead of the library itself.
592 tocPath := outputFile.RelPathString()
593 tocPath = pathtools.ReplaceExtension(tocPath, flags.Toolchain.ShlibSuffix()[1:]+".toc")
594 tocFile := android.PathForOutput(ctx, tocPath)
595 library.tocFile = android.OptionalPathForPath(tocFile)
596 TransformSharedObjectToToc(ctx, outputFile, tocFile, builderFlags)
Colin Cross89562dc2016-10-03 17:47:19 -0700597
Colin Cross4d9c2d12016-07-29 12:48:20 -0700598 if library.stripper.needsStrip(ctx) {
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -0700599 // b/80093681, GNU strip/objcopy bug.
600 // Use llvm-{strip,objcopy} when clang lld is used.
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700601 builderFlags.stripUseLlvmStrip = library.baseLinker.useClangLld(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700602 strippedOutputFile := outputFile
603 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
604 library.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
605 }
606
Colin Crossb60190a2018-09-04 16:28:17 -0700607 library.unstrippedOutputFile = outputFile
608
Colin Cross86803cf2018-02-15 14:12:26 -0800609 if Bool(library.baseLinker.Properties.Use_version_lib) && ctx.Host() {
610 versionedOutputFile := outputFile
611 outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
612 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
613 }
614
Colin Cross4d9c2d12016-07-29 12:48:20 -0700615 sharedLibs := deps.SharedLibs
616 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
617
Colin Cross26c34ed2016-09-30 17:10:16 -0700618 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
619 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700620 linkerDeps = append(linkerDeps, objs.tidyFiles...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700621
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700622 TransformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700623 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
624 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile)
625
Dan Willemsen581341d2017-02-09 16:16:31 -0800626 objs.coverageFiles = append(objs.coverageFiles, deps.StaticLibObjs.coverageFiles...)
627 objs.coverageFiles = append(objs.coverageFiles, deps.WholeStaticLibObjs.coverageFiles...)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800628
629 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.StaticLibObjs.sAbiDumpFiles...)
630 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.WholeStaticLibObjs.sAbiDumpFiles...)
631
Dan Willemsen581341d2017-02-09 16:16:31 -0800632 library.coverageOutputFile = TransformCoverageFilesToLib(ctx, objs, builderFlags, library.getLibName(ctx))
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700633 library.linkSAbiDumpFiles(ctx, objs, fileName, ret)
Dan Willemsen581341d2017-02-09 16:16:31 -0800634
Colin Cross4d9c2d12016-07-29 12:48:20 -0700635 return ret
636}
637
Logan Chien7eefdc42018-07-11 18:10:41 +0800638func getRefAbiDumpFile(ctx ModuleContext, vndkVersion, fileName string) android.Path {
Logan Chienf4b79c62018-08-02 02:27:02 +0800639 isLlndk := inList(ctx.baseModuleName(), llndkLibraries) || inList(ctx.baseModuleName(), ndkMigratedLibs)
Logan Chien7eefdc42018-07-11 18:10:41 +0800640
641 refAbiDumpTextFile := android.PathForVndkRefAbiDump(ctx, vndkVersion, fileName, isLlndk, false)
642 refAbiDumpGzipFile := android.PathForVndkRefAbiDump(ctx, vndkVersion, fileName, isLlndk, true)
643
644 if refAbiDumpTextFile.Valid() {
645 if refAbiDumpGzipFile.Valid() {
646 ctx.ModuleErrorf(
647 "Two reference ABI dump files are found: %q and %q. Please delete the stale one.",
648 refAbiDumpTextFile, refAbiDumpGzipFile)
649 return nil
650 }
651 return refAbiDumpTextFile.Path()
652 }
653 if refAbiDumpGzipFile.Valid() {
654 return UnzipRefDump(ctx, refAbiDumpGzipFile.Path(), fileName)
655 }
656 return nil
657}
658
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700659func (library *libraryDecorator) linkSAbiDumpFiles(ctx ModuleContext, objs Objects, fileName string, soFile android.Path) {
Logan Chien2f2b8902018-07-10 15:01:19 +0800660 if len(objs.sAbiDumpFiles) > 0 && ctx.shouldCreateVndkSourceAbiDump() {
Logan Chiena8f51582018-03-21 11:53:48 +0800661 vndkVersion := ctx.DeviceConfig().PlatformVndkVersion()
662 if ver := ctx.DeviceConfig().VndkVersion(); ver != "" && ver != "current" {
Logan Chienf3511742017-10-31 18:04:35 +0800663 vndkVersion = ver
664 }
665
Jayant Chowdharya4fce192017-09-06 13:10:03 -0700666 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800667 var SourceAbiFlags []string
668 for _, dir := range exportIncludeDirs.Strings() {
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700669 SourceAbiFlags = append(SourceAbiFlags, "-I"+dir)
670 }
671 for _, reexportedInclude := range extractExportIncludesFromFlags(library.sabi.Properties.ReexportedIncludeFlags) {
672 SourceAbiFlags = append(SourceAbiFlags, reexportedInclude)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800673 }
674 exportedHeaderFlags := strings.Join(SourceAbiFlags, " ")
Jayant Chowdharydf344d52018-01-17 11:11:42 -0800675 library.sAbiOutputFile = TransformDumpToLinkedDump(ctx, objs.sAbiDumpFiles, soFile, fileName, exportedHeaderFlags)
Logan Chien2f2b8902018-07-10 15:01:19 +0800676
Logan Chien7eefdc42018-07-11 18:10:41 +0800677 refAbiDumpFile := getRefAbiDumpFile(ctx, vndkVersion, fileName)
678 if refAbiDumpFile != nil {
Logan Chienf3511742017-10-31 18:04:35 +0800679 library.sAbiDiff = SourceAbiDiff(ctx, library.sAbiOutputFile.Path(),
Logan Chien7eefdc42018-07-11 18:10:41 +0800680 refAbiDumpFile, fileName, exportedHeaderFlags, ctx.isVndkExt())
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800681 }
682 }
683}
684
Colin Crossb916a382016-07-29 17:28:03 -0700685func (library *libraryDecorator) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700686 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700687
Colin Crossad59e752017-11-16 14:29:11 -0800688 objs = deps.Objs.Copy().Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700689 var out android.Path
Colin Crossa48ab5b2017-02-14 15:28:44 -0800690 if library.static() || library.header() {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700691 out = library.linkStatic(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700692 } else {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700693 out = library.linkShared(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700694 }
695
696 library.exportIncludes(ctx, "-I")
697 library.reexportFlags(deps.ReexportedFlags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700698 library.reexportDeps(deps.ReexportedFlagsDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700699
Nan Zhang0007d812017-11-07 10:57:05 -0800700 if Bool(library.Properties.Aidl.Export_aidl_headers) {
Dan Willemsene1240db2016-11-03 14:28:51 -0700701 if library.baseCompiler.hasSrcExt(".aidl") {
Colin Cross10d22312017-05-03 11:01:58 -0700702 flags := []string{
Dan Willemsene1240db2016-11-03 14:28:51 -0700703 "-I" + android.PathForModuleGen(ctx, "aidl").String(),
Colin Cross10d22312017-05-03 11:01:58 -0700704 }
705 library.reexportFlags(flags)
706 library.reuseExportedFlags = append(library.reuseExportedFlags, flags...)
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800707 library.reexportDeps(library.baseCompiler.pathDeps) // TODO: restrict to aidl deps
708 library.reuseExportedDeps = append(library.reuseExportedDeps, library.baseCompiler.pathDeps...)
Dan Willemsene1240db2016-11-03 14:28:51 -0700709 }
710 }
711
Nan Zhang0007d812017-11-07 10:57:05 -0800712 if Bool(library.Properties.Proto.Export_proto_headers) {
Dan Willemsene1240db2016-11-03 14:28:51 -0700713 if library.baseCompiler.hasSrcExt(".proto") {
Dan Willemsenab9f4262018-02-14 13:58:34 -0800714 includes := []string{}
715 if flags.ProtoRoot {
716 includes = append(includes, "-I"+android.ProtoSubDir(ctx).String())
Colin Cross10d22312017-05-03 11:01:58 -0700717 }
Dan Willemsenab9f4262018-02-14 13:58:34 -0800718 includes = append(includes, "-I"+android.ProtoDir(ctx).String())
719 library.reexportFlags(includes)
720 library.reuseExportedFlags = append(library.reuseExportedFlags, includes...)
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800721 library.reexportDeps(library.baseCompiler.pathDeps) // TODO: restrict to proto deps
722 library.reuseExportedDeps = append(library.reuseExportedDeps, library.baseCompiler.pathDeps...)
Colin Cross0c461f12016-10-20 16:11:43 -0700723 }
724 }
725
Inseob Kim21f26902018-09-06 00:55:20 +0900726 if library.baseCompiler.hasSrcExt(".sysprop") {
727 flags := []string{
728 "-I" + android.PathForModuleGen(ctx, "sysprop", "include").String(),
729 }
730 library.reexportFlags(flags)
731 library.reuseExportedFlags = append(library.reuseExportedFlags, flags...)
732 }
733
Jiyong Parkda732bd2018-11-02 18:23:15 +0900734 if library.buildStubs() {
735 library.reexportFlags([]string{"-D" + versioningMacroName(ctx.ModuleName()) + "=" + library.stubsVersion()})
736 }
737
Colin Cross4d9c2d12016-07-29 12:48:20 -0700738 return out
739}
740
Colin Crossb916a382016-07-29 17:28:03 -0700741func (library *libraryDecorator) buildStatic() bool {
Colin Cross38b40df2018-04-10 16:14:46 -0700742 return library.MutatedProperties.BuildStatic && BoolDefault(library.Properties.Static.Enabled, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700743}
744
Colin Crossb916a382016-07-29 17:28:03 -0700745func (library *libraryDecorator) buildShared() bool {
Colin Cross38b40df2018-04-10 16:14:46 -0700746 return library.MutatedProperties.BuildShared && BoolDefault(library.Properties.Shared.Enabled, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700747}
748
Colin Crossb916a382016-07-29 17:28:03 -0700749func (library *libraryDecorator) getWholeStaticMissingDeps() []string {
Colin Crossd2343a32018-04-30 14:50:01 -0700750 return append([]string(nil), library.wholeStaticMissingDeps...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700751}
752
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700753func (library *libraryDecorator) objs() Objects {
754 return library.objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700755}
756
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700757func (library *libraryDecorator) reuseObjs() (Objects, []string, android.Paths) {
758 return library.reuseObjects, library.reuseExportedFlags, library.reuseExportedDeps
Colin Cross4d9c2d12016-07-29 12:48:20 -0700759}
760
Colin Cross26c34ed2016-09-30 17:10:16 -0700761func (library *libraryDecorator) toc() android.OptionalPath {
762 return library.tocFile
763}
764
Colin Crossb916a382016-07-29 17:28:03 -0700765func (library *libraryDecorator) install(ctx ModuleContext, file android.Path) {
Colin Crossc43ae772017-04-14 15:42:53 -0700766 if library.shared() {
Justin Yun8fe12122017-12-07 17:18:15 +0900767 if ctx.Device() && ctx.useVndk() {
768 if ctx.isVndkSp() {
769 library.baseInstaller.subDir = "vndk-sp"
770 } else if ctx.isVndk() {
771 library.baseInstaller.subDir = "vndk"
772 }
Logan Chienf3511742017-10-31 18:04:35 +0800773
774 // Append a version to vndk or vndk-sp directories on the system partition.
775 if ctx.isVndk() && !ctx.isVndkExt() {
776 vndkVersion := ctx.DeviceConfig().PlatformVndkVersion()
777 if vndkVersion != "current" && vndkVersion != "" {
778 library.baseInstaller.subDir += "-" + vndkVersion
779 }
Justin Yun8effde42017-06-23 19:24:43 +0900780 }
781 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700782 library.baseInstaller.install(ctx, file)
783 }
Dan Albertf563d252017-10-13 00:29:00 -0700784
Dan Albert281f22b2017-12-13 15:03:47 -0800785 if Bool(library.Properties.Static_ndk_lib) && library.static() &&
Jiyong Parkf9332f12018-02-01 00:54:12 +0900786 !ctx.useVndk() && !ctx.inRecovery() && ctx.Device() &&
Jiyong Park7ed9de32018-10-15 22:25:07 +0900787 library.baseLinker.sanitize.isUnsanitizedVariant() &&
788 !library.buildStubs() {
Dan Albertf563d252017-10-13 00:29:00 -0700789 installPath := getNdkSysrootBase(ctx).Join(
Dan Albertea4b7b92018-04-25 16:05:30 -0700790 ctx, "usr/lib", config.NDKTriple(ctx.toolchain()), file.Base())
Dan Albertf563d252017-10-13 00:29:00 -0700791
792 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
793 Rule: android.Cp,
794 Description: "install " + installPath.Base(),
795 Output: installPath,
796 Input: file,
797 })
798
Colin Cross0875c522017-11-28 17:34:01 -0800799 library.ndkSysrootPath = installPath
Dan Albertf563d252017-10-13 00:29:00 -0700800 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700801}
802
Colin Crossb916a382016-07-29 17:28:03 -0700803func (library *libraryDecorator) static() bool {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800804 return library.MutatedProperties.VariantIsStatic
Colin Cross4d9c2d12016-07-29 12:48:20 -0700805}
806
Colin Crossa48ab5b2017-02-14 15:28:44 -0800807func (library *libraryDecorator) shared() bool {
808 return library.MutatedProperties.VariantIsShared
809}
810
811func (library *libraryDecorator) header() bool {
812 return !library.static() && !library.shared()
813}
814
815func (library *libraryDecorator) setStatic() {
816 library.MutatedProperties.VariantIsStatic = true
817 library.MutatedProperties.VariantIsShared = false
818}
819
820func (library *libraryDecorator) setShared() {
821 library.MutatedProperties.VariantIsStatic = false
822 library.MutatedProperties.VariantIsShared = true
Colin Crossb916a382016-07-29 17:28:03 -0700823}
824
Colin Crossab3b7322016-12-09 14:46:15 -0800825func (library *libraryDecorator) BuildOnlyStatic() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800826 library.MutatedProperties.BuildShared = false
Colin Crossab3b7322016-12-09 14:46:15 -0800827}
828
829func (library *libraryDecorator) BuildOnlyShared() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800830 library.MutatedProperties.BuildStatic = false
Colin Crossab3b7322016-12-09 14:46:15 -0800831}
832
Colin Cross5950f382016-12-13 12:50:57 -0800833func (library *libraryDecorator) HeaderOnly() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800834 library.MutatedProperties.BuildShared = false
835 library.MutatedProperties.BuildStatic = false
Colin Cross5950f382016-12-13 12:50:57 -0800836}
837
Jiyong Park7ed9de32018-10-15 22:25:07 +0900838func (library *libraryDecorator) buildStubs() bool {
839 return library.MutatedProperties.BuildStubs
840}
841
842func (library *libraryDecorator) stubsVersion() string {
843 return library.MutatedProperties.StubsVersion
844}
845
Jiyong Parkda732bd2018-11-02 18:23:15 +0900846func versioningMacroNamesList(config android.Config) *map[string]string {
847 return config.Once("versioningMacroNamesList", func() interface{} {
848 m := make(map[string]string)
849 return &m
850 }).(*map[string]string)
851}
852
853// alphanumeric and _ characters are preserved.
854// other characters are all converted to _
855var charsNotForMacro = regexp.MustCompile("[^a-zA-Z0-9_]+")
856
857func versioningMacroName(moduleName string) string {
858 macroName := charsNotForMacro.ReplaceAllString(moduleName, "_")
859 macroName = strings.ToUpper(moduleName)
860 return "__" + macroName + "_API__"
861}
862
Colin Crossab3b7322016-12-09 14:46:15 -0800863func NewLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700864 module := newModule(hod, android.MultilibBoth)
865
Colin Crossb916a382016-07-29 17:28:03 -0700866 library := &libraryDecorator{
Colin Crossa48ab5b2017-02-14 15:28:44 -0800867 MutatedProperties: LibraryMutatedProperties{
Colin Crossab3b7322016-12-09 14:46:15 -0800868 BuildShared: true,
869 BuildStatic: true,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700870 },
Colin Crossb916a382016-07-29 17:28:03 -0700871 baseCompiler: NewBaseCompiler(),
Dan Albert61f32122018-07-26 14:00:24 -0700872 baseLinker: NewBaseLinker(module.sanitize),
Colin Crossb916a382016-07-29 17:28:03 -0700873 baseInstaller: NewBaseInstaller("lib", "lib64", InstallInSystem),
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800874 sabi: module.sabi,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700875 }
876
Colin Crossb916a382016-07-29 17:28:03 -0700877 module.compiler = library
878 module.linker = library
879 module.installer = library
880
881 return module, library
882}
883
Colin Cross10d22312017-05-03 11:01:58 -0700884// connects a shared library to a static library in order to reuse its .o files to avoid
885// compiling source files twice.
886func reuseStaticLibrary(mctx android.BottomUpMutatorContext, static, shared *Module) {
887 if staticCompiler, ok := static.compiler.(*libraryDecorator); ok {
888 sharedCompiler := shared.compiler.(*libraryDecorator)
889 if len(staticCompiler.Properties.Static.Cflags) == 0 &&
890 len(sharedCompiler.Properties.Shared.Cflags) == 0 {
891
892 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
893 sharedCompiler.baseCompiler.Properties.OriginalSrcs =
894 sharedCompiler.baseCompiler.Properties.Srcs
895 sharedCompiler.baseCompiler.Properties.Srcs = nil
896 sharedCompiler.baseCompiler.Properties.Generated_sources = nil
897 }
898 }
899}
900
Colin Crosse40b4ea2018-10-02 22:25:58 -0700901func LinkageMutator(mctx android.BottomUpMutatorContext) {
Colin Crossb916a382016-07-29 17:28:03 -0700902 if m, ok := mctx.Module().(*Module); ok && m.linker != nil {
903 if library, ok := m.linker.(libraryInterface); ok {
904 var modules []blueprint.Module
905 if library.buildStatic() && library.buildShared() {
906 modules = mctx.CreateLocalVariations("static", "shared")
907 static := modules[0].(*Module)
908 shared := modules[1].(*Module)
909
Colin Crossa48ab5b2017-02-14 15:28:44 -0800910 static.linker.(libraryInterface).setStatic()
911 shared.linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700912
Colin Cross10d22312017-05-03 11:01:58 -0700913 reuseStaticLibrary(mctx, static, shared)
914
Colin Crossb916a382016-07-29 17:28:03 -0700915 } else if library.buildStatic() {
916 modules = mctx.CreateLocalVariations("static")
Colin Crossa48ab5b2017-02-14 15:28:44 -0800917 modules[0].(*Module).linker.(libraryInterface).setStatic()
Colin Crossb916a382016-07-29 17:28:03 -0700918 } else if library.buildShared() {
919 modules = mctx.CreateLocalVariations("shared")
Colin Crossa48ab5b2017-02-14 15:28:44 -0800920 modules[0].(*Module).linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700921 }
922 }
923 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700924}
Jiyong Park7ed9de32018-10-15 22:25:07 +0900925
926// Version mutator splits a module into the mandatory non-stubs variant
927// (which is named "impl") and zero or more stubs variants.
928func versionMutator(mctx android.BottomUpMutatorContext) {
929 if mctx.Os() != android.Android {
930 return
931 }
932
933 if m, ok := mctx.Module().(*Module); ok && !m.inRecovery() && m.linker != nil {
934 if library, ok := m.linker.(*libraryDecorator); ok && library.buildShared() {
935 versions := []string{""}
936 for _, v := range library.Properties.Stubs.Versions {
937 versions = append(versions, v)
938 }
939 modules := mctx.CreateVariations(versions...)
940 for i, m := range modules {
941 l := m.(*Module).linker.(*libraryDecorator)
942 if i == 0 {
943 l.MutatedProperties.BuildStubs = false
944 continue
945 }
946 // Mark that this variant is for stubs.
947 l.MutatedProperties.BuildStubs = true
948 l.MutatedProperties.StubsVersion = versions[i]
949 m.(*Module).Properties.HideFromMake = true
950 }
951 } else {
952 mctx.CreateVariations("")
953 }
954 return
955 }
956 if genrule, ok := mctx.Module().(*genrule.Module); ok {
957 if props, ok := genrule.Extra.(*GenruleExtraProperties); ok && !props.InRecovery {
958 mctx.CreateVariations("")
959 return
960 }
961 }
962}