blob: c01b3e7421a6f7cad3d662d0aced504375b90ffb [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"
Jiyong Park25fc6a92018-11-18 18:02:45 +090019 "sort"
20 "strconv"
Colin Cross4d9c2d12016-07-29 12:48:20 -070021 "strings"
Jiyong Parkda732bd2018-11-02 18:23:15 +090022 "sync"
Colin Cross4d9c2d12016-07-29 12:48:20 -070023
24 "github.com/google/blueprint"
Colin Cross26c34ed2016-09-30 17:10:16 -070025 "github.com/google/blueprint/pathtools"
Colin Cross4d9c2d12016-07-29 12:48:20 -070026
Colin Cross4d9c2d12016-07-29 12:48:20 -070027 "android/soong/android"
Dan Albertea4b7b92018-04-25 16:05:30 -070028 "android/soong/cc/config"
Jiyong Park7ed9de32018-10-15 22:25:07 +090029 "android/soong/genrule"
Colin Cross4d9c2d12016-07-29 12:48:20 -070030)
31
Colin Crosseefe9a32019-01-22 14:41:08 -080032type StaticSharedLibraryProperties struct {
33 Srcs []string `android:"arch_variant"`
34 Cflags []string `android:"arch_variant"`
35
36 Enabled *bool `android:"arch_variant"`
37 Whole_static_libs []string `android:"arch_variant"`
38 Static_libs []string `android:"arch_variant"`
39 Shared_libs []string `android:"arch_variant"`
40 System_shared_libs []string `android:"arch_variant"`
41
42 Export_shared_lib_headers []string `android:"arch_variant"`
43 Export_static_lib_headers []string `android:"arch_variant"`
44}
45
Colin Crossb916a382016-07-29 17:28:03 -070046type LibraryProperties struct {
Colin Crosseefe9a32019-01-22 14:41:08 -080047 Static StaticSharedLibraryProperties `android:"arch_variant"`
48 Shared StaticSharedLibraryProperties `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070049
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 }
dimitryd95964a2018-11-07 13:43:34 +010080
81 // set the name of the output
82 Stem *string `android:"arch_variant"`
83
84 // Names of modules to be overridden. Listed modules can only be other shared libraries
85 // (in Make or Soong).
86 // This does not completely prevent installation of the overridden libraries, but if both
87 // binaries would be installed by default (in PRODUCT_PACKAGES) the other library will be removed
88 // from PRODUCT_PACKAGES.
89 Overrides []string
Logan Chiene3d7a0d2019-01-17 00:18:02 +080090
91 // Properties for ABI compatibility checker
92 Header_abi_checker struct {
93 // Path to a symbol file that specifies the symbols to be included in the generated
94 // ABI dump file
95 Symbol_file *string
96
97 // Symbol versions that should be ignored from the symbol file
98 Exclude_symbol_versions []string
99
100 // Symbol tags that should be ignored from the symbol file
101 Exclude_symbol_tags []string
102 }
Colin Crossa48ab5b2017-02-14 15:28:44 -0800103}
Colin Cross0c461f12016-10-20 16:11:43 -0700104
Colin Crossa48ab5b2017-02-14 15:28:44 -0800105type LibraryMutatedProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700106 VariantName string `blueprint:"mutated"`
Colin Crossb916a382016-07-29 17:28:03 -0700107
108 // Build a static variant
109 BuildStatic bool `blueprint:"mutated"`
110 // Build a shared variant
111 BuildShared bool `blueprint:"mutated"`
112 // This variant is shared
113 VariantIsShared bool `blueprint:"mutated"`
114 // This variant is static
115 VariantIsStatic bool `blueprint:"mutated"`
Jiyong Park7ed9de32018-10-15 22:25:07 +0900116
117 // This variant is a stubs lib
118 BuildStubs bool `blueprint:"mutated"`
119 // Version of the stubs lib
120 StubsVersion string `blueprint:"mutated"`
Colin Crossb916a382016-07-29 17:28:03 -0700121}
122
123type FlagExporterProperties struct {
124 // list of directories relative to the Blueprints file that will
Dan Willemsen273af7f2016-11-03 15:53:42 -0700125 // be added to the include path (using -I) for this module and any module that links
Colin Cross5d195602017-10-17 16:15:50 -0700126 // against this module. Directories listed in export_include_dirs do not need to be
127 // listed in local_include_dirs.
Colin Crossb916a382016-07-29 17:28:03 -0700128 Export_include_dirs []string `android:"arch_variant"`
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700129
130 Target struct {
131 Vendor struct {
132 // list of exported include directories, like
133 // export_include_dirs, that will be applied to the
134 // vendor variant of this library. This will overwrite
135 // any other declarations.
Steven Morelandb21df8f2018-01-05 14:42:54 -0800136 Override_export_include_dirs []string
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700137 }
138 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700139}
140
141func init() {
Steven Morelandf9e62162017-11-02 17:00:50 -0700142 android.RegisterModuleType("cc_library_static", LibraryStaticFactory)
143 android.RegisterModuleType("cc_library_shared", LibrarySharedFactory)
144 android.RegisterModuleType("cc_library", LibraryFactory)
145 android.RegisterModuleType("cc_library_host_static", LibraryHostStaticFactory)
146 android.RegisterModuleType("cc_library_host_shared", LibraryHostSharedFactory)
147 android.RegisterModuleType("cc_library_headers", LibraryHeaderFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700148}
149
150// Module factory for combined static + shared libraries, device by default but with possible host
151// support
Steven Morelandf9e62162017-11-02 17:00:50 -0700152func LibraryFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800153 module, _ := NewLibrary(android.HostAndDeviceSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700154 return module.Init()
155}
156
157// Module factory for static libraries
Steven Morelandf9e62162017-11-02 17:00:50 -0700158func LibraryStaticFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800159 module, library := NewLibrary(android.HostAndDeviceSupported)
160 library.BuildOnlyStatic()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700161 return module.Init()
162}
163
164// Module factory for shared libraries
Steven Morelandf9e62162017-11-02 17:00:50 -0700165func LibrarySharedFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800166 module, library := NewLibrary(android.HostAndDeviceSupported)
167 library.BuildOnlyShared()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700168 return module.Init()
169}
170
171// Module factory for host static libraries
Steven Morelandf9e62162017-11-02 17:00:50 -0700172func LibraryHostStaticFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800173 module, library := NewLibrary(android.HostSupported)
174 library.BuildOnlyStatic()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700175 return module.Init()
176}
177
178// Module factory for host shared libraries
Steven Morelandf9e62162017-11-02 17:00:50 -0700179func LibraryHostSharedFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800180 module, library := NewLibrary(android.HostSupported)
181 library.BuildOnlyShared()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700182 return module.Init()
183}
184
Colin Cross5950f382016-12-13 12:50:57 -0800185// Module factory for header-only libraries
Steven Morelandf9e62162017-11-02 17:00:50 -0700186func LibraryHeaderFactory() android.Module {
Colin Cross5950f382016-12-13 12:50:57 -0800187 module, library := NewLibrary(android.HostAndDeviceSupported)
188 library.HeaderOnly()
189 return module.Init()
190}
191
Colin Cross4d9c2d12016-07-29 12:48:20 -0700192type flagExporter struct {
193 Properties FlagExporterProperties
194
Dan Willemsen847dcc72016-09-29 12:13:36 -0700195 flags []string
196 flagsDeps android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700197}
198
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700199func (f *flagExporter) exportedIncludes(ctx ModuleContext) android.Paths {
Steven Morelandb21df8f2018-01-05 14:42:54 -0800200 if ctx.useVndk() && f.Properties.Target.Vendor.Override_export_include_dirs != nil {
201 return android.PathsForModuleSrc(ctx, f.Properties.Target.Vendor.Override_export_include_dirs)
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700202 } else {
203 return android.PathsForModuleSrc(ctx, f.Properties.Export_include_dirs)
204 }
205}
206
Colin Cross4d9c2d12016-07-29 12:48:20 -0700207func (f *flagExporter) exportIncludes(ctx ModuleContext, inc string) {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700208 includeDirs := f.exportedIncludes(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700209 for _, dir := range includeDirs.Strings() {
210 f.flags = append(f.flags, inc+dir)
211 }
212}
213
214func (f *flagExporter) reexportFlags(flags []string) {
215 f.flags = append(f.flags, flags...)
216}
217
Dan Willemsen847dcc72016-09-29 12:13:36 -0700218func (f *flagExporter) reexportDeps(deps android.Paths) {
219 f.flagsDeps = append(f.flagsDeps, deps...)
220}
221
Colin Cross4d9c2d12016-07-29 12:48:20 -0700222func (f *flagExporter) exportedFlags() []string {
223 return f.flags
224}
225
Dan Willemsen847dcc72016-09-29 12:13:36 -0700226func (f *flagExporter) exportedFlagsDeps() android.Paths {
227 return f.flagsDeps
228}
229
Colin Cross4d9c2d12016-07-29 12:48:20 -0700230type exportedFlagsProducer interface {
231 exportedFlags() []string
Dan Willemsen847dcc72016-09-29 12:13:36 -0700232 exportedFlagsDeps() android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700233}
234
235var _ exportedFlagsProducer = (*flagExporter)(nil)
236
Colin Crossb916a382016-07-29 17:28:03 -0700237// libraryDecorator wraps baseCompiler, baseLinker and baseInstaller to provide library-specific
238// functionality: static vs. shared linkage, reusing object files for shared libraries
239type libraryDecorator struct {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800240 Properties LibraryProperties
241 MutatedProperties LibraryMutatedProperties
Colin Cross4d9c2d12016-07-29 12:48:20 -0700242
243 // For reusing static library objects for shared library
Colin Cross10d22312017-05-03 11:01:58 -0700244 reuseObjects Objects
245 reuseExportedFlags []string
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700246 reuseExportedDeps android.Paths
Colin Cross10d22312017-05-03 11:01:58 -0700247
Colin Cross26c34ed2016-09-30 17:10:16 -0700248 // table-of-contents file to optimize out relinking when possible
249 tocFile android.OptionalPath
Colin Cross4d9c2d12016-07-29 12:48:20 -0700250
Colin Cross4d9c2d12016-07-29 12:48:20 -0700251 flagExporter
252 stripper
253
Colin Cross4d9c2d12016-07-29 12:48:20 -0700254 // If we're used as a whole_static_lib, our missing dependencies need
255 // to be given
256 wholeStaticMissingDeps []string
257
258 // For whole_static_libs
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700259 objects Objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700260
261 // Uses the module's name if empty, but can be overridden. Does not include
262 // shlib suffix.
263 libName string
Colin Crossb916a382016-07-29 17:28:03 -0700264
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800265 sabi *sabi
266
Dan Willemsen581341d2017-02-09 16:16:31 -0800267 // Output archive of gcno coverage information files
268 coverageOutputFile android.OptionalPath
269
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800270 // linked Source Abi Dump
271 sAbiOutputFile android.OptionalPath
272
273 // Source Abi Diff
274 sAbiDiff android.OptionalPath
275
Colin Cross0875c522017-11-28 17:34:01 -0800276 // Location of the static library in the sysroot. Empty if the library is
277 // not included in the NDK.
278 ndkSysrootPath android.Path
279
Colin Crossb60190a2018-09-04 16:28:17 -0700280 // Location of the linked, unstripped library for shared libraries
281 unstrippedOutputFile android.Path
282
Dan Willemsen569edc52018-11-19 09:33:29 -0800283 // Location of the file that should be copied to dist dir when requested
284 distFile android.OptionalPath
285
Jiyong Park7ed9de32018-10-15 22:25:07 +0900286 versionScriptPath android.ModuleGenPath
287
Colin Crossb916a382016-07-29 17:28:03 -0700288 // Decorated interafaces
289 *baseCompiler
290 *baseLinker
291 *baseInstaller
Colin Cross4d9c2d12016-07-29 12:48:20 -0700292}
293
Colin Crossb916a382016-07-29 17:28:03 -0700294func (library *libraryDecorator) linkerProps() []interface{} {
295 var props []interface{}
296 props = append(props, library.baseLinker.linkerProps()...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700297 return append(props,
298 &library.Properties,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800299 &library.MutatedProperties,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700300 &library.flagExporter.Properties,
Colin Cross22f37952018-09-05 10:43:13 -0700301 &library.stripper.StripProperties)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700302}
303
Colin Crossb916a382016-07-29 17:28:03 -0700304func (library *libraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross42742b82016-08-01 13:20:05 -0700305 flags = library.baseLinker.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700306
Colin Crossb916a382016-07-29 17:28:03 -0700307 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
308 // all code is position independent, and then those warnings get promoted to
309 // errors.
Colin Cross3edeee12017-04-04 12:59:48 -0700310 if !ctx.Windows() {
Colin Crossb916a382016-07-29 17:28:03 -0700311 flags.CFlags = append(flags.CFlags, "-fPIC")
312 }
313
314 if library.static() {
315 flags.CFlags = append(flags.CFlags, library.Properties.Static.Cflags...)
Colin Crossa48ab5b2017-02-14 15:28:44 -0800316 } else if library.shared() {
Colin Crossb916a382016-07-29 17:28:03 -0700317 flags.CFlags = append(flags.CFlags, library.Properties.Shared.Cflags...)
318 }
319
Colin Crossa48ab5b2017-02-14 15:28:44 -0800320 if library.shared() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700321 libName := library.getLibName(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700322 var f []string
Dan Willemsen01a405a2016-06-13 17:19:03 -0700323 if ctx.toolchain().Bionic() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700324 f = append(f,
325 "-nostdlib",
326 "-Wl,--gc-sections",
327 )
328 }
329
330 if ctx.Darwin() {
331 f = append(f,
332 "-dynamiclib",
333 "-single_module",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700334 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
335 )
Colin Cross7863cf52016-10-20 10:47:21 -0700336 if ctx.Arch().ArchType == android.X86 {
337 f = append(f,
338 "-read_only_relocs suppress",
339 )
340 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700341 } else {
342 f = append(f,
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700343 "-shared",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700344 "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix())
345 }
346
347 flags.LdFlags = append(f, flags.LdFlags...)
348 }
349
350 return flags
351}
352
Colin Crossf18e1102017-11-16 14:33:08 -0800353func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700354 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700355 if len(exportIncludeDirs) > 0 {
Colin Crossdad8c952017-04-26 14:55:27 -0700356 f := includeDirsToFlags(exportIncludeDirs)
357 flags.GlobalFlags = append(flags.GlobalFlags, f)
358 flags.YasmFlags = append(flags.YasmFlags, f)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700359 }
360
Jiyong Park7ed9de32018-10-15 22:25:07 +0900361 flags = library.baseCompiler.compilerFlags(ctx, flags, deps)
362 if library.buildStubs() {
Jiyong Park64379952018-12-13 18:37:29 +0900363 // Remove -include <file> when compiling stubs. Otherwise, the force included
364 // headers might cause conflicting types error with the symbols in the
365 // generated stubs source code. e.g.
366 // double acos(double); // in header
367 // void acos() {} // in the generated source code
368 removeInclude := func(flags []string) []string {
369 ret := flags[:0]
370 for _, f := range flags {
371 if strings.HasPrefix(f, "-include ") {
372 continue
373 }
374 ret = append(ret, f)
375 }
376 return ret
377 }
378 flags.GlobalFlags = removeInclude(flags.GlobalFlags)
379 flags.CFlags = removeInclude(flags.CFlags)
380
Jiyong Park7ed9de32018-10-15 22:25:07 +0900381 flags = addStubLibraryCompilerFlags(flags)
382 }
383 return flags
Dan Willemsen273af7f2016-11-03 15:53:42 -0700384}
385
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700386func extractExportIncludesFromFlags(flags []string) []string {
387 // This method is used in the generation of rules which produce
388 // abi-dumps for source files. Exported headers are needed to infer the
389 // abi exported by a library and filter out the rest of the abi dumped
390 // from a source. We extract the include flags exported by a library.
391 // This includes the flags exported which are re-exported from static
392 // library dependencies, exported header library dependencies and
Jayant Chowdharyaf6eb712017-08-23 16:08:29 -0700393 // generated header dependencies. -isystem headers are not included
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700394 // since for bionic libraries, abi-filtering is taken care of by version
395 // scripts.
396 var exportedIncludes []string
397 for _, flag := range flags {
398 if strings.HasPrefix(flag, "-I") {
399 exportedIncludes = append(exportedIncludes, flag)
400 }
401 }
402 return exportedIncludes
403}
404
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700405func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Jiyong Park7ed9de32018-10-15 22:25:07 +0900406 if library.buildStubs() {
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900407 objs, versionScript := compileStubLibrary(ctx, flags, String(library.Properties.Stubs.Symbol_file), library.MutatedProperties.StubsVersion, "--apex")
Jiyong Park7ed9de32018-10-15 22:25:07 +0900408 library.versionScriptPath = versionScript
409 return objs
410 }
411
Colin Cross5950f382016-12-13 12:50:57 -0800412 if !library.buildShared() && !library.buildStatic() {
413 if len(library.baseCompiler.Properties.Srcs) > 0 {
414 ctx.PropertyErrorf("srcs", "cc_library_headers must not have any srcs")
415 }
416 if len(library.Properties.Static.Srcs) > 0 {
417 ctx.PropertyErrorf("static.srcs", "cc_library_headers must not have any srcs")
418 }
419 if len(library.Properties.Shared.Srcs) > 0 {
420 ctx.PropertyErrorf("shared.srcs", "cc_library_headers must not have any srcs")
421 }
422 return Objects{}
423 }
Logan Chien2f2b8902018-07-10 15:01:19 +0800424 if ctx.shouldCreateVndkSourceAbiDump() || library.sabi.Properties.CreateSAbiDumps {
Jayant Chowdharya4fce192017-09-06 13:10:03 -0700425 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800426 var SourceAbiFlags []string
427 for _, dir := range exportIncludeDirs.Strings() {
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700428 SourceAbiFlags = append(SourceAbiFlags, "-I"+dir)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800429 }
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700430 for _, reexportedInclude := range extractExportIncludesFromFlags(library.sabi.Properties.ReexportedIncludeFlags) {
431 SourceAbiFlags = append(SourceAbiFlags, reexportedInclude)
432 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800433 flags.SAbiFlags = SourceAbiFlags
434 total_length := len(library.baseCompiler.Properties.Srcs) + len(deps.GeneratedSources) + len(library.Properties.Shared.Srcs) +
435 len(library.Properties.Static.Srcs)
436 if total_length > 0 {
437 flags.SAbiDump = true
438 }
439 }
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700440 objs := library.baseCompiler.compile(ctx, flags, deps)
441 library.reuseObjects = objs
Colin Cross2f336352016-10-26 10:03:47 -0700442 buildFlags := flagsToBuilderFlags(flags)
Colin Crossb916a382016-07-29 17:28:03 -0700443
Colin Cross4d9c2d12016-07-29 12:48:20 -0700444 if library.static() {
dimitry0345ad82018-12-05 16:28:14 +0100445 srcs := ctx.ExpandSources(library.Properties.Static.Srcs, nil)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700446 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceStaticLibrary,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800447 srcs, library.baseCompiler.pathDeps, library.baseCompiler.cFlagsDeps))
Colin Crossa48ab5b2017-02-14 15:28:44 -0800448 } else if library.shared() {
dimitry0345ad82018-12-05 16:28:14 +0100449 srcs := ctx.ExpandSources(library.Properties.Shared.Srcs, nil)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700450 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceSharedLibrary,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800451 srcs, library.baseCompiler.pathDeps, library.baseCompiler.cFlagsDeps))
Colin Crossb916a382016-07-29 17:28:03 -0700452 }
453
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700454 return objs
Colin Crossb916a382016-07-29 17:28:03 -0700455}
456
457type libraryInterface interface {
458 getWholeStaticMissingDeps() []string
459 static() bool
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700460 objs() Objects
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700461 reuseObjs() (Objects, []string, android.Paths)
Colin Cross26c34ed2016-09-30 17:10:16 -0700462 toc() android.OptionalPath
Colin Crossb916a382016-07-29 17:28:03 -0700463
464 // Returns true if the build options for the module have selected a static or shared build
465 buildStatic() bool
466 buildShared() bool
467
468 // Sets whether a specific variant is static or shared
Colin Crossa48ab5b2017-02-14 15:28:44 -0800469 setStatic()
470 setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700471}
472
473func (library *libraryDecorator) getLibName(ctx ModuleContext) string {
474 name := library.libName
475 if name == "" {
dimitryd95964a2018-11-07 13:43:34 +0100476 name = String(library.Properties.Stem)
477 if name == "" {
478 name = ctx.baseModuleName()
479 }
Colin Crossb916a382016-07-29 17:28:03 -0700480 }
481
Logan Chienf3511742017-10-31 18:04:35 +0800482 if ctx.isVndkExt() {
483 name = ctx.getVndkExtendsModuleName()
484 }
485
Colin Crossb916a382016-07-29 17:28:03 -0700486 if ctx.Host() && Bool(library.Properties.Unique_host_soname) {
487 if !strings.HasSuffix(name, "-host") {
488 name = name + "-host"
489 }
490 }
491
Colin Crossa48ab5b2017-02-14 15:28:44 -0800492 return name + library.MutatedProperties.VariantName
Colin Crossb916a382016-07-29 17:28:03 -0700493}
494
Jiyong Parkda732bd2018-11-02 18:23:15 +0900495var versioningMacroNamesListMutex sync.Mutex
496
Colin Crossb916a382016-07-29 17:28:03 -0700497func (library *libraryDecorator) linkerInit(ctx BaseModuleContext) {
498 location := InstallInSystem
Dan Albert61f32122018-07-26 14:00:24 -0700499 if library.baseLinker.sanitize.inSanitizerDir() {
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700500 location = InstallInSanitizerDir
Colin Crossb916a382016-07-29 17:28:03 -0700501 }
502 library.baseInstaller.location = location
Colin Crossb916a382016-07-29 17:28:03 -0700503 library.baseLinker.linkerInit(ctx)
Jiyong Park7ed9de32018-10-15 22:25:07 +0900504 // Let baseLinker know whether this variant is for stubs or not, so that
505 // it can omit things that are not required for linking stubs.
506 library.baseLinker.dynamicProperties.BuildStubs = library.buildStubs()
Jiyong Parkda732bd2018-11-02 18:23:15 +0900507
508 if library.buildStubs() {
509 macroNames := versioningMacroNamesList(ctx.Config())
510 myName := versioningMacroName(ctx.ModuleName())
511 versioningMacroNamesListMutex.Lock()
512 defer versioningMacroNamesListMutex.Unlock()
513 if (*macroNames)[myName] == "" {
514 (*macroNames)[myName] = ctx.ModuleName()
515 } else if (*macroNames)[myName] != ctx.ModuleName() {
516 ctx.ModuleErrorf("Macro name %q for versioning conflicts with macro name from module %q ", myName, (*macroNames)[myName])
517 }
518 }
Colin Crossb916a382016-07-29 17:28:03 -0700519}
520
dimitry0345ad82018-12-05 16:28:14 +0100521func (library *libraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
522 deps = library.baseCompiler.compilerDeps(ctx, deps)
523
524 if library.static() {
525 android.ExtractSourcesDeps(ctx, library.Properties.Static.Srcs)
526 } else if library.shared() {
527 android.ExtractSourcesDeps(ctx, library.Properties.Shared.Srcs)
528 }
529
530 return deps
531}
532
Colin Cross37047f12016-12-13 17:06:13 -0800533func (library *libraryDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Dan Willemsen3a26eef2018-12-03 15:25:46 -0800534 if library.static() {
535 if library.Properties.Static.System_shared_libs != nil {
536 library.baseLinker.Properties.System_shared_libs = library.Properties.Static.System_shared_libs
537 }
538 } else if library.shared() {
539 if library.Properties.Shared.System_shared_libs != nil {
540 library.baseLinker.Properties.System_shared_libs = library.Properties.Shared.System_shared_libs
541 }
542 }
543
Colin Crossb916a382016-07-29 17:28:03 -0700544 deps = library.baseLinker.linkerDeps(ctx, deps)
545
546 if library.static() {
547 deps.WholeStaticLibs = append(deps.WholeStaticLibs,
548 library.Properties.Static.Whole_static_libs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700549 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Static.Static_libs...)
550 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Static.Shared_libs...)
Colin Crosseefe9a32019-01-22 14:41:08 -0800551
552 deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, library.Properties.Static.Export_shared_lib_headers...)
553 deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, library.Properties.Static.Export_static_lib_headers...)
Colin Crossa48ab5b2017-02-14 15:28:44 -0800554 } else if library.shared() {
Dan Willemsen2e47b342016-11-17 01:02:25 -0800555 if ctx.toolchain().Bionic() && !Bool(library.baseLinker.Properties.Nocrt) {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700556 if !ctx.useSdk() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700557 deps.CrtBegin = "crtbegin_so"
558 deps.CrtEnd = "crtend_so"
559 } else {
Dan Albertebedf672016-11-08 15:06:22 -0800560 // TODO(danalbert): Add generation of crt objects.
561 // For `sdk_version: "current"`, we don't actually have a
562 // freshly generated set of CRT objects. Use the last stable
563 // version.
564 version := ctx.sdkVersion()
565 if version == "current" {
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -0700566 version = getCurrentNdkPrebuiltVersion(ctx)
Dan Albertebedf672016-11-08 15:06:22 -0800567 }
568 deps.CrtBegin = "ndk_crtbegin_so." + version
569 deps.CrtEnd = "ndk_crtend_so." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700570 }
571 }
572 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Shared.Whole_static_libs...)
573 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Shared.Static_libs...)
574 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Shared.Shared_libs...)
Colin Crosseefe9a32019-01-22 14:41:08 -0800575
576 deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, library.Properties.Shared.Export_shared_lib_headers...)
577 deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, library.Properties.Shared.Export_static_lib_headers...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700578 }
Jiyong Park52d25bd2017-10-13 09:17:01 +0900579 if ctx.useVndk() {
580 deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs)
581 deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Vendor.Exclude_shared_libs)
582 deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs)
Victor Chang51271c12019-01-30 16:02:22 +0000583 deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, library.baseLinker.Properties.Target.Vendor.Exclude_shared_libs)
584 deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs)
Jiyong Park52d25bd2017-10-13 09:17:01 +0900585 }
Jiyong Parkf9332f12018-02-01 00:54:12 +0900586 if ctx.inRecovery() {
587 deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Recovery.Exclude_static_libs)
588 deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Recovery.Exclude_shared_libs)
589 deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Recovery.Exclude_static_libs)
Victor Chang51271c12019-01-30 16:02:22 +0000590 deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, library.baseLinker.Properties.Target.Recovery.Exclude_shared_libs)
591 deps.ReexportStaticLibHeaders = removeListFromList(deps.ReexportStaticLibHeaders, library.baseLinker.Properties.Target.Recovery.Exclude_static_libs)
Jiyong Parkf9332f12018-02-01 00:54:12 +0900592 }
Colin Cross2383f3b2018-02-06 14:40:13 -0800593
Colin Cross2383f3b2018-02-06 14:40:13 -0800594 android.ExtractSourceDeps(ctx, library.Properties.Unexported_symbols_list)
595 android.ExtractSourceDeps(ctx, library.Properties.Force_symbols_not_weak_list)
596 android.ExtractSourceDeps(ctx, library.Properties.Force_symbols_weak_list)
Colin Cross2383f3b2018-02-06 14:40:13 -0800597
Colin Cross4d9c2d12016-07-29 12:48:20 -0700598 return deps
599}
600
Colin Crossb916a382016-07-29 17:28:03 -0700601func (library *libraryDecorator) linkStatic(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700602 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700603
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700604 library.objects = deps.WholeStaticLibObjs.Copy()
605 library.objects = library.objects.Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700606
Colin Cross86803cf2018-02-15 14:12:26 -0800607 fileName := ctx.ModuleName() + library.MutatedProperties.VariantName + staticLibraryExtension
608 outputFile := android.PathForModuleOut(ctx, fileName)
Dan Willemsen581341d2017-02-09 16:16:31 -0800609 builderFlags := flagsToBuilderFlags(flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700610
Dan Willemsen569edc52018-11-19 09:33:29 -0800611 if Bool(library.baseLinker.Properties.Use_version_lib) {
612 if ctx.Host() {
613 versionedOutputFile := outputFile
614 outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
615 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
616 } else {
617 versionedOutputFile := android.PathForModuleOut(ctx, "versioned", fileName)
618 library.distFile = android.OptionalPathForPath(versionedOutputFile)
619 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
620 }
Colin Cross86803cf2018-02-15 14:12:26 -0800621 }
622
Dan Willemsen581341d2017-02-09 16:16:31 -0800623 TransformObjToStaticLib(ctx, library.objects.objFiles, builderFlags, outputFile, objs.tidyFiles)
624
625 library.coverageOutputFile = TransformCoverageFilesToLib(ctx, library.objects, builderFlags,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800626 ctx.ModuleName()+library.MutatedProperties.VariantName)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700627
628 library.wholeStaticMissingDeps = ctx.GetMissingDependencies()
629
630 ctx.CheckbuildFile(outputFile)
631
632 return outputFile
633}
634
Colin Crossb916a382016-07-29 17:28:03 -0700635func (library *libraryDecorator) linkShared(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700636 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700637
638 var linkerDeps android.Paths
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700639 linkerDeps = append(linkerDeps, flags.LdFlagsDeps...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700640
Colin Cross2383f3b2018-02-06 14:40:13 -0800641 unexportedSymbols := ctx.ExpandOptionalSource(library.Properties.Unexported_symbols_list, "unexported_symbols_list")
642 forceNotWeakSymbols := ctx.ExpandOptionalSource(library.Properties.Force_symbols_not_weak_list, "force_symbols_not_weak_list")
643 forceWeakSymbols := ctx.ExpandOptionalSource(library.Properties.Force_symbols_weak_list, "force_symbols_weak_list")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700644 if !ctx.Darwin() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700645 if unexportedSymbols.Valid() {
646 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
647 }
648 if forceNotWeakSymbols.Valid() {
649 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
650 }
651 if forceWeakSymbols.Valid() {
652 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
653 }
654 } else {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700655 if unexportedSymbols.Valid() {
656 flags.LdFlags = append(flags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
657 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
658 }
659 if forceNotWeakSymbols.Valid() {
660 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
661 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
662 }
663 if forceWeakSymbols.Valid() {
664 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
665 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
666 }
667 }
Jiyong Parkc1e7f482019-01-12 13:39:10 +0900668 if library.buildStubs() {
669 linkerScriptFlags := "-Wl,--version-script," + library.versionScriptPath.String()
670 flags.LdFlags = append(flags.LdFlags, linkerScriptFlags)
671 linkerDeps = append(linkerDeps, library.versionScriptPath)
672 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700673
674 fileName := library.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
675 outputFile := android.PathForModuleOut(ctx, fileName)
676 ret := outputFile
677
678 builderFlags := flagsToBuilderFlags(flags)
679
Colin Crossb496cfd2018-09-10 16:50:05 -0700680 // Optimize out relinking against shared libraries whose interface hasn't changed by
681 // depending on a table of contents file instead of the library itself.
682 tocPath := outputFile.RelPathString()
683 tocPath = pathtools.ReplaceExtension(tocPath, flags.Toolchain.ShlibSuffix()[1:]+".toc")
684 tocFile := android.PathForOutput(ctx, tocPath)
685 library.tocFile = android.OptionalPathForPath(tocFile)
686 TransformSharedObjectToToc(ctx, outputFile, tocFile, builderFlags)
Colin Cross89562dc2016-10-03 17:47:19 -0700687
Colin Cross4d9c2d12016-07-29 12:48:20 -0700688 if library.stripper.needsStrip(ctx) {
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -0700689 // b/80093681, GNU strip/objcopy bug.
690 // Use llvm-{strip,objcopy} when clang lld is used.
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700691 builderFlags.stripUseLlvmStrip = library.baseLinker.useClangLld(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700692 strippedOutputFile := outputFile
693 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
694 library.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
695 }
696
Colin Crossb60190a2018-09-04 16:28:17 -0700697 library.unstrippedOutputFile = outputFile
698
Dan Willemsen569edc52018-11-19 09:33:29 -0800699 if Bool(library.baseLinker.Properties.Use_version_lib) {
700 if ctx.Host() {
701 versionedOutputFile := outputFile
702 outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
703 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
704 } else {
705 versionedOutputFile := android.PathForModuleOut(ctx, "versioned", fileName)
706 library.distFile = android.OptionalPathForPath(versionedOutputFile)
707
708 if library.stripper.needsStrip(ctx) {
709 out := android.PathForModuleOut(ctx, "versioned-stripped", fileName)
710 library.distFile = android.OptionalPathForPath(out)
711 library.stripper.strip(ctx, versionedOutputFile, out, builderFlags)
712 }
713
714 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
715 }
Colin Cross86803cf2018-02-15 14:12:26 -0800716 }
717
Jiyong Park64a44f22019-01-18 14:37:08 +0900718 sharedLibs := deps.EarlySharedLibs
719 sharedLibs = append(sharedLibs, deps.SharedLibs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700720 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
721
Jiyong Park64a44f22019-01-18 14:37:08 +0900722 linkerDeps = append(linkerDeps, deps.EarlySharedLibsDeps...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700723 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
724 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700725 linkerDeps = append(linkerDeps, objs.tidyFiles...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700726
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700727 TransformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700728 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
729 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile)
730
Dan Willemsen581341d2017-02-09 16:16:31 -0800731 objs.coverageFiles = append(objs.coverageFiles, deps.StaticLibObjs.coverageFiles...)
732 objs.coverageFiles = append(objs.coverageFiles, deps.WholeStaticLibObjs.coverageFiles...)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800733
734 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.StaticLibObjs.sAbiDumpFiles...)
735 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.WholeStaticLibObjs.sAbiDumpFiles...)
736
Dan Willemsen581341d2017-02-09 16:16:31 -0800737 library.coverageOutputFile = TransformCoverageFilesToLib(ctx, objs, builderFlags, library.getLibName(ctx))
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700738 library.linkSAbiDumpFiles(ctx, objs, fileName, ret)
Dan Willemsen581341d2017-02-09 16:16:31 -0800739
Colin Cross4d9c2d12016-07-29 12:48:20 -0700740 return ret
741}
742
Logan Chien7eefdc42018-07-11 18:10:41 +0800743func getRefAbiDumpFile(ctx ModuleContext, vndkVersion, fileName string) android.Path {
Logan Chienf4b79c62018-08-02 02:27:02 +0800744 isLlndk := inList(ctx.baseModuleName(), llndkLibraries) || inList(ctx.baseModuleName(), ndkMigratedLibs)
Logan Chien7eefdc42018-07-11 18:10:41 +0800745
746 refAbiDumpTextFile := android.PathForVndkRefAbiDump(ctx, vndkVersion, fileName, isLlndk, false)
747 refAbiDumpGzipFile := android.PathForVndkRefAbiDump(ctx, vndkVersion, fileName, isLlndk, true)
748
749 if refAbiDumpTextFile.Valid() {
750 if refAbiDumpGzipFile.Valid() {
751 ctx.ModuleErrorf(
752 "Two reference ABI dump files are found: %q and %q. Please delete the stale one.",
753 refAbiDumpTextFile, refAbiDumpGzipFile)
754 return nil
755 }
756 return refAbiDumpTextFile.Path()
757 }
758 if refAbiDumpGzipFile.Valid() {
759 return UnzipRefDump(ctx, refAbiDumpGzipFile.Path(), fileName)
760 }
761 return nil
762}
763
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700764func (library *libraryDecorator) linkSAbiDumpFiles(ctx ModuleContext, objs Objects, fileName string, soFile android.Path) {
Logan Chien2f2b8902018-07-10 15:01:19 +0800765 if len(objs.sAbiDumpFiles) > 0 && ctx.shouldCreateVndkSourceAbiDump() {
Logan Chiena8f51582018-03-21 11:53:48 +0800766 vndkVersion := ctx.DeviceConfig().PlatformVndkVersion()
767 if ver := ctx.DeviceConfig().VndkVersion(); ver != "" && ver != "current" {
Logan Chienf3511742017-10-31 18:04:35 +0800768 vndkVersion = ver
769 }
770
Jayant Chowdharya4fce192017-09-06 13:10:03 -0700771 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800772 var SourceAbiFlags []string
773 for _, dir := range exportIncludeDirs.Strings() {
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700774 SourceAbiFlags = append(SourceAbiFlags, "-I"+dir)
775 }
776 for _, reexportedInclude := range extractExportIncludesFromFlags(library.sabi.Properties.ReexportedIncludeFlags) {
777 SourceAbiFlags = append(SourceAbiFlags, reexportedInclude)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800778 }
779 exportedHeaderFlags := strings.Join(SourceAbiFlags, " ")
Logan Chiene3d7a0d2019-01-17 00:18:02 +0800780 library.sAbiOutputFile = TransformDumpToLinkedDump(ctx, objs.sAbiDumpFiles, soFile, fileName, exportedHeaderFlags,
781 android.OptionalPathForModuleSrc(ctx, library.Properties.Header_abi_checker.Symbol_file),
782 library.Properties.Header_abi_checker.Exclude_symbol_versions,
783 library.Properties.Header_abi_checker.Exclude_symbol_tags)
Logan Chien2f2b8902018-07-10 15:01:19 +0800784
Logan Chien7eefdc42018-07-11 18:10:41 +0800785 refAbiDumpFile := getRefAbiDumpFile(ctx, vndkVersion, fileName)
786 if refAbiDumpFile != nil {
Logan Chienf3511742017-10-31 18:04:35 +0800787 library.sAbiDiff = SourceAbiDiff(ctx, library.sAbiOutputFile.Path(),
Logan Chien7eefdc42018-07-11 18:10:41 +0800788 refAbiDumpFile, fileName, exportedHeaderFlags, ctx.isVndkExt())
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800789 }
790 }
791}
792
Colin Crossb916a382016-07-29 17:28:03 -0700793func (library *libraryDecorator) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700794 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700795
Colin Crossad59e752017-11-16 14:29:11 -0800796 objs = deps.Objs.Copy().Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700797 var out android.Path
Colin Crossa48ab5b2017-02-14 15:28:44 -0800798 if library.static() || library.header() {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700799 out = library.linkStatic(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700800 } else {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700801 out = library.linkShared(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700802 }
803
804 library.exportIncludes(ctx, "-I")
805 library.reexportFlags(deps.ReexportedFlags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700806 library.reexportDeps(deps.ReexportedFlagsDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700807
Nan Zhang0007d812017-11-07 10:57:05 -0800808 if Bool(library.Properties.Aidl.Export_aidl_headers) {
Dan Willemsene1240db2016-11-03 14:28:51 -0700809 if library.baseCompiler.hasSrcExt(".aidl") {
Colin Cross10d22312017-05-03 11:01:58 -0700810 flags := []string{
Dan Willemsene1240db2016-11-03 14:28:51 -0700811 "-I" + android.PathForModuleGen(ctx, "aidl").String(),
Colin Cross10d22312017-05-03 11:01:58 -0700812 }
813 library.reexportFlags(flags)
814 library.reuseExportedFlags = append(library.reuseExportedFlags, flags...)
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800815 library.reexportDeps(library.baseCompiler.pathDeps) // TODO: restrict to aidl deps
816 library.reuseExportedDeps = append(library.reuseExportedDeps, library.baseCompiler.pathDeps...)
Dan Willemsene1240db2016-11-03 14:28:51 -0700817 }
818 }
819
Nan Zhang0007d812017-11-07 10:57:05 -0800820 if Bool(library.Properties.Proto.Export_proto_headers) {
Dan Willemsene1240db2016-11-03 14:28:51 -0700821 if library.baseCompiler.hasSrcExt(".proto") {
Dan Willemsenab9f4262018-02-14 13:58:34 -0800822 includes := []string{}
823 if flags.ProtoRoot {
824 includes = append(includes, "-I"+android.ProtoSubDir(ctx).String())
Colin Cross10d22312017-05-03 11:01:58 -0700825 }
Dan Willemsenab9f4262018-02-14 13:58:34 -0800826 includes = append(includes, "-I"+android.ProtoDir(ctx).String())
827 library.reexportFlags(includes)
828 library.reuseExportedFlags = append(library.reuseExportedFlags, includes...)
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800829 library.reexportDeps(library.baseCompiler.pathDeps) // TODO: restrict to proto deps
830 library.reuseExportedDeps = append(library.reuseExportedDeps, library.baseCompiler.pathDeps...)
Colin Cross0c461f12016-10-20 16:11:43 -0700831 }
832 }
833
Inseob Kim21f26902018-09-06 00:55:20 +0900834 if library.baseCompiler.hasSrcExt(".sysprop") {
835 flags := []string{
836 "-I" + android.PathForModuleGen(ctx, "sysprop", "include").String(),
837 }
838 library.reexportFlags(flags)
Sundong Ahn5b73f312018-12-19 14:39:29 +0900839 library.reexportDeps(library.baseCompiler.pathDeps)
Inseob Kim21f26902018-09-06 00:55:20 +0900840 library.reuseExportedFlags = append(library.reuseExportedFlags, flags...)
841 }
842
Jiyong Parkda732bd2018-11-02 18:23:15 +0900843 if library.buildStubs() {
844 library.reexportFlags([]string{"-D" + versioningMacroName(ctx.ModuleName()) + "=" + library.stubsVersion()})
845 }
846
Colin Cross4d9c2d12016-07-29 12:48:20 -0700847 return out
848}
849
Colin Crossb916a382016-07-29 17:28:03 -0700850func (library *libraryDecorator) buildStatic() bool {
Colin Cross38b40df2018-04-10 16:14:46 -0700851 return library.MutatedProperties.BuildStatic && BoolDefault(library.Properties.Static.Enabled, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700852}
853
Colin Crossb916a382016-07-29 17:28:03 -0700854func (library *libraryDecorator) buildShared() bool {
Colin Cross38b40df2018-04-10 16:14:46 -0700855 return library.MutatedProperties.BuildShared && BoolDefault(library.Properties.Shared.Enabled, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700856}
857
Colin Crossb916a382016-07-29 17:28:03 -0700858func (library *libraryDecorator) getWholeStaticMissingDeps() []string {
Colin Crossd2343a32018-04-30 14:50:01 -0700859 return append([]string(nil), library.wholeStaticMissingDeps...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700860}
861
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700862func (library *libraryDecorator) objs() Objects {
863 return library.objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700864}
865
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700866func (library *libraryDecorator) reuseObjs() (Objects, []string, android.Paths) {
867 return library.reuseObjects, library.reuseExportedFlags, library.reuseExportedDeps
Colin Cross4d9c2d12016-07-29 12:48:20 -0700868}
869
Colin Cross26c34ed2016-09-30 17:10:16 -0700870func (library *libraryDecorator) toc() android.OptionalPath {
871 return library.tocFile
872}
873
Colin Crossb916a382016-07-29 17:28:03 -0700874func (library *libraryDecorator) install(ctx ModuleContext, file android.Path) {
Colin Crossc43ae772017-04-14 15:42:53 -0700875 if library.shared() {
Justin Yun8fe12122017-12-07 17:18:15 +0900876 if ctx.Device() && ctx.useVndk() {
877 if ctx.isVndkSp() {
878 library.baseInstaller.subDir = "vndk-sp"
879 } else if ctx.isVndk() {
880 library.baseInstaller.subDir = "vndk"
881 }
Logan Chienf3511742017-10-31 18:04:35 +0800882
883 // Append a version to vndk or vndk-sp directories on the system partition.
884 if ctx.isVndk() && !ctx.isVndkExt() {
885 vndkVersion := ctx.DeviceConfig().PlatformVndkVersion()
886 if vndkVersion != "current" && vndkVersion != "" {
887 library.baseInstaller.subDir += "-" + vndkVersion
888 }
Justin Yun8effde42017-06-23 19:24:43 +0900889 }
Jiyong Park429660f2019-01-16 22:31:11 +0900890 } else if len(library.Properties.Stubs.Versions) > 0 && android.DirectlyInAnyApex(ctx, ctx.ModuleName()) {
891 // If a library in an APEX has stable versioned APIs, we basically don't need
892 // to have the platform variant of the library in /system partition because
893 // platform components can just use the lib from the APEX without fearing about
894 // compatibility. However, if the library is required for some early processes
895 // before the APEX is activated, the platform variant may also be required.
896 // In that case, it is installed to the subdirectory 'bootstrap' in order to
897 // be distinguished/isolated from other non-bootstrap libraries in /system/lib
898 // so that the bootstrap libraries are used only when the APEX isn't ready.
899 if !library.buildStubs() && ctx.Arch().Native {
900 library.baseInstaller.subDir = "bootstrap"
901 }
Justin Yun8effde42017-06-23 19:24:43 +0900902 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700903 library.baseInstaller.install(ctx, file)
904 }
Dan Albertf563d252017-10-13 00:29:00 -0700905
Dan Albert281f22b2017-12-13 15:03:47 -0800906 if Bool(library.Properties.Static_ndk_lib) && library.static() &&
Jiyong Parkf9332f12018-02-01 00:54:12 +0900907 !ctx.useVndk() && !ctx.inRecovery() && ctx.Device() &&
Jiyong Park7ed9de32018-10-15 22:25:07 +0900908 library.baseLinker.sanitize.isUnsanitizedVariant() &&
909 !library.buildStubs() {
Dan Albertf563d252017-10-13 00:29:00 -0700910 installPath := getNdkSysrootBase(ctx).Join(
Dan Albertea4b7b92018-04-25 16:05:30 -0700911 ctx, "usr/lib", config.NDKTriple(ctx.toolchain()), file.Base())
Dan Albertf563d252017-10-13 00:29:00 -0700912
913 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
914 Rule: android.Cp,
915 Description: "install " + installPath.Base(),
916 Output: installPath,
917 Input: file,
918 })
919
Colin Cross0875c522017-11-28 17:34:01 -0800920 library.ndkSysrootPath = installPath
Dan Albertf563d252017-10-13 00:29:00 -0700921 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700922}
923
Colin Crossb916a382016-07-29 17:28:03 -0700924func (library *libraryDecorator) static() bool {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800925 return library.MutatedProperties.VariantIsStatic
Colin Cross4d9c2d12016-07-29 12:48:20 -0700926}
927
Colin Crossa48ab5b2017-02-14 15:28:44 -0800928func (library *libraryDecorator) shared() bool {
929 return library.MutatedProperties.VariantIsShared
930}
931
932func (library *libraryDecorator) header() bool {
933 return !library.static() && !library.shared()
934}
935
936func (library *libraryDecorator) setStatic() {
937 library.MutatedProperties.VariantIsStatic = true
938 library.MutatedProperties.VariantIsShared = false
939}
940
941func (library *libraryDecorator) setShared() {
942 library.MutatedProperties.VariantIsStatic = false
943 library.MutatedProperties.VariantIsShared = true
Colin Crossb916a382016-07-29 17:28:03 -0700944}
945
Colin Crossab3b7322016-12-09 14:46:15 -0800946func (library *libraryDecorator) BuildOnlyStatic() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800947 library.MutatedProperties.BuildShared = false
Colin Crossab3b7322016-12-09 14:46:15 -0800948}
949
950func (library *libraryDecorator) BuildOnlyShared() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800951 library.MutatedProperties.BuildStatic = false
Colin Crossab3b7322016-12-09 14:46:15 -0800952}
953
Colin Cross5950f382016-12-13 12:50:57 -0800954func (library *libraryDecorator) HeaderOnly() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800955 library.MutatedProperties.BuildShared = false
956 library.MutatedProperties.BuildStatic = false
Colin Cross5950f382016-12-13 12:50:57 -0800957}
958
Jiyong Park7ed9de32018-10-15 22:25:07 +0900959func (library *libraryDecorator) buildStubs() bool {
960 return library.MutatedProperties.BuildStubs
961}
962
963func (library *libraryDecorator) stubsVersion() string {
964 return library.MutatedProperties.StubsVersion
965}
966
Jiyong Parkda732bd2018-11-02 18:23:15 +0900967func versioningMacroNamesList(config android.Config) *map[string]string {
968 return config.Once("versioningMacroNamesList", func() interface{} {
969 m := make(map[string]string)
970 return &m
971 }).(*map[string]string)
972}
973
974// alphanumeric and _ characters are preserved.
975// other characters are all converted to _
976var charsNotForMacro = regexp.MustCompile("[^a-zA-Z0-9_]+")
977
978func versioningMacroName(moduleName string) string {
979 macroName := charsNotForMacro.ReplaceAllString(moduleName, "_")
980 macroName = strings.ToUpper(moduleName)
981 return "__" + macroName + "_API__"
982}
983
Colin Crossab3b7322016-12-09 14:46:15 -0800984func NewLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700985 module := newModule(hod, android.MultilibBoth)
986
Colin Crossb916a382016-07-29 17:28:03 -0700987 library := &libraryDecorator{
Colin Crossa48ab5b2017-02-14 15:28:44 -0800988 MutatedProperties: LibraryMutatedProperties{
Colin Crossab3b7322016-12-09 14:46:15 -0800989 BuildShared: true,
990 BuildStatic: true,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700991 },
Colin Crossb916a382016-07-29 17:28:03 -0700992 baseCompiler: NewBaseCompiler(),
Dan Albert61f32122018-07-26 14:00:24 -0700993 baseLinker: NewBaseLinker(module.sanitize),
Colin Crossb916a382016-07-29 17:28:03 -0700994 baseInstaller: NewBaseInstaller("lib", "lib64", InstallInSystem),
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800995 sabi: module.sabi,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700996 }
997
Colin Crossb916a382016-07-29 17:28:03 -0700998 module.compiler = library
999 module.linker = library
1000 module.installer = library
1001
1002 return module, library
1003}
1004
Colin Cross10d22312017-05-03 11:01:58 -07001005// connects a shared library to a static library in order to reuse its .o files to avoid
1006// compiling source files twice.
1007func reuseStaticLibrary(mctx android.BottomUpMutatorContext, static, shared *Module) {
1008 if staticCompiler, ok := static.compiler.(*libraryDecorator); ok {
1009 sharedCompiler := shared.compiler.(*libraryDecorator)
Dan Willemsen3a26eef2018-12-03 15:25:46 -08001010
1011 // Check libraries in addition to cflags, since libraries may be exporting different
1012 // include directories.
Colin Cross10d22312017-05-03 11:01:58 -07001013 if len(staticCompiler.Properties.Static.Cflags) == 0 &&
Dan Willemsen3a26eef2018-12-03 15:25:46 -08001014 len(sharedCompiler.Properties.Shared.Cflags) == 0 &&
1015 len(staticCompiler.Properties.Static.Whole_static_libs) == 0 &&
1016 len(sharedCompiler.Properties.Shared.Whole_static_libs) == 0 &&
1017 len(staticCompiler.Properties.Static.Static_libs) == 0 &&
1018 len(sharedCompiler.Properties.Shared.Static_libs) == 0 &&
1019 len(staticCompiler.Properties.Static.Shared_libs) == 0 &&
1020 len(sharedCompiler.Properties.Shared.Shared_libs) == 0 &&
1021 staticCompiler.Properties.Static.System_shared_libs == nil &&
1022 sharedCompiler.Properties.Shared.System_shared_libs == nil {
Colin Cross10d22312017-05-03 11:01:58 -07001023
1024 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
1025 sharedCompiler.baseCompiler.Properties.OriginalSrcs =
1026 sharedCompiler.baseCompiler.Properties.Srcs
1027 sharedCompiler.baseCompiler.Properties.Srcs = nil
1028 sharedCompiler.baseCompiler.Properties.Generated_sources = nil
1029 }
1030 }
1031}
1032
Colin Crosse40b4ea2018-10-02 22:25:58 -07001033func LinkageMutator(mctx android.BottomUpMutatorContext) {
Colin Crossb916a382016-07-29 17:28:03 -07001034 if m, ok := mctx.Module().(*Module); ok && m.linker != nil {
1035 if library, ok := m.linker.(libraryInterface); ok {
1036 var modules []blueprint.Module
1037 if library.buildStatic() && library.buildShared() {
1038 modules = mctx.CreateLocalVariations("static", "shared")
1039 static := modules[0].(*Module)
1040 shared := modules[1].(*Module)
1041
Colin Crossa48ab5b2017-02-14 15:28:44 -08001042 static.linker.(libraryInterface).setStatic()
1043 shared.linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -07001044
Colin Cross10d22312017-05-03 11:01:58 -07001045 reuseStaticLibrary(mctx, static, shared)
1046
Colin Crossb916a382016-07-29 17:28:03 -07001047 } else if library.buildStatic() {
1048 modules = mctx.CreateLocalVariations("static")
Colin Crossa48ab5b2017-02-14 15:28:44 -08001049 modules[0].(*Module).linker.(libraryInterface).setStatic()
Colin Crossb916a382016-07-29 17:28:03 -07001050 } else if library.buildShared() {
1051 modules = mctx.CreateLocalVariations("shared")
Colin Crossa48ab5b2017-02-14 15:28:44 -08001052 modules[0].(*Module).linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -07001053 }
1054 }
1055 }
Colin Cross4d9c2d12016-07-29 12:48:20 -07001056}
Jiyong Park7ed9de32018-10-15 22:25:07 +09001057
Jiyong Park25fc6a92018-11-18 18:02:45 +09001058// maps a module name to the list of stubs versions available for the module
1059func stubsVersionsFor(config android.Config) map[string][]string {
1060 return config.Once("stubVersions", func() interface{} {
1061 return make(map[string][]string)
1062 }).(map[string][]string)
1063}
1064
1065var stubsVersionsLock sync.Mutex
1066
1067func latestStubsVersionFor(config android.Config, name string) string {
1068 versions, ok := stubsVersionsFor(config)[name]
1069 if ok && len(versions) > 0 {
1070 // the versions are alreay sorted in ascending order
1071 return versions[len(versions)-1]
1072 }
1073 return ""
1074}
1075
Jiyong Park7ed9de32018-10-15 22:25:07 +09001076// Version mutator splits a module into the mandatory non-stubs variant
Jiyong Park25fc6a92018-11-18 18:02:45 +09001077// (which is unnamed) and zero or more stubs variants.
1078func VersionMutator(mctx android.BottomUpMutatorContext) {
Jiyong Park7ed9de32018-10-15 22:25:07 +09001079 if m, ok := mctx.Module().(*Module); ok && !m.inRecovery() && m.linker != nil {
Jiyong Park25fc6a92018-11-18 18:02:45 +09001080 if library, ok := m.linker.(*libraryDecorator); ok && library.buildShared() &&
1081 len(library.Properties.Stubs.Versions) > 0 {
1082 versions := []string{}
Jiyong Park7ed9de32018-10-15 22:25:07 +09001083 for _, v := range library.Properties.Stubs.Versions {
Jiyong Park25fc6a92018-11-18 18:02:45 +09001084 if _, err := strconv.Atoi(v); err != nil {
1085 mctx.PropertyErrorf("versions", "%q is not a number", v)
1086 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09001087 versions = append(versions, v)
1088 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001089 sort.Slice(versions, func(i, j int) bool {
1090 left, _ := strconv.Atoi(versions[i])
1091 right, _ := strconv.Atoi(versions[j])
1092 return left < right
1093 })
1094
1095 // save the list of versions for later use
1096 copiedVersions := make([]string, len(versions))
1097 copy(copiedVersions, versions)
1098 stubsVersionsLock.Lock()
1099 defer stubsVersionsLock.Unlock()
1100 stubsVersionsFor(mctx.Config())[mctx.ModuleName()] = copiedVersions
1101
1102 // "" is for the non-stubs variant
Jiyong Park67883b32019-01-03 21:35:58 +09001103 versions = append([]string{""}, versions...)
Jiyong Park25fc6a92018-11-18 18:02:45 +09001104
Jiyong Park7ed9de32018-10-15 22:25:07 +09001105 modules := mctx.CreateVariations(versions...)
1106 for i, m := range modules {
1107 l := m.(*Module).linker.(*libraryDecorator)
Jiyong Park25fc6a92018-11-18 18:02:45 +09001108 if versions[i] != "" {
1109 l.MutatedProperties.BuildStubs = true
1110 l.MutatedProperties.StubsVersion = versions[i]
1111 m.(*Module).Properties.HideFromMake = true
Jiyong Park090d9df2018-12-11 02:47:16 +09001112 m.(*Module).sanitize = nil
1113 m.(*Module).stl = nil
Jiyong Park127d5652018-12-12 10:26:20 +09001114 m.(*Module).Properties.PreventInstall = true
Jiyong Park7ed9de32018-10-15 22:25:07 +09001115 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09001116 }
1117 } else {
1118 mctx.CreateVariations("")
1119 }
1120 return
1121 }
1122 if genrule, ok := mctx.Module().(*genrule.Module); ok {
1123 if props, ok := genrule.Extra.(*GenruleExtraProperties); ok && !props.InRecovery {
1124 mctx.CreateVariations("")
1125 return
1126 }
1127 }
1128}