blob: b4b89d2ac6f7530f00a60254c2da6dce6c8cebe4 [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) {
Yi Kongb5c34d72018-11-07 16:28:49 -0800689 if ctx.Darwin() {
690 builderFlags.stripUseGnuStrip = true
691 }
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
Jiyong Parkaf6d8952019-01-31 12:21:23 +0900743func (library *libraryDecorator) unstrippedOutputFilePath() android.Path {
744 return library.unstrippedOutputFile
745}
746
Logan Chien7eefdc42018-07-11 18:10:41 +0800747func getRefAbiDumpFile(ctx ModuleContext, vndkVersion, fileName string) android.Path {
Logan Chienf4b79c62018-08-02 02:27:02 +0800748 isLlndk := inList(ctx.baseModuleName(), llndkLibraries) || inList(ctx.baseModuleName(), ndkMigratedLibs)
Logan Chien7eefdc42018-07-11 18:10:41 +0800749
750 refAbiDumpTextFile := android.PathForVndkRefAbiDump(ctx, vndkVersion, fileName, isLlndk, false)
751 refAbiDumpGzipFile := android.PathForVndkRefAbiDump(ctx, vndkVersion, fileName, isLlndk, true)
752
753 if refAbiDumpTextFile.Valid() {
754 if refAbiDumpGzipFile.Valid() {
755 ctx.ModuleErrorf(
756 "Two reference ABI dump files are found: %q and %q. Please delete the stale one.",
757 refAbiDumpTextFile, refAbiDumpGzipFile)
758 return nil
759 }
760 return refAbiDumpTextFile.Path()
761 }
762 if refAbiDumpGzipFile.Valid() {
763 return UnzipRefDump(ctx, refAbiDumpGzipFile.Path(), fileName)
764 }
765 return nil
766}
767
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700768func (library *libraryDecorator) linkSAbiDumpFiles(ctx ModuleContext, objs Objects, fileName string, soFile android.Path) {
Logan Chien2f2b8902018-07-10 15:01:19 +0800769 if len(objs.sAbiDumpFiles) > 0 && ctx.shouldCreateVndkSourceAbiDump() {
Logan Chiena8f51582018-03-21 11:53:48 +0800770 vndkVersion := ctx.DeviceConfig().PlatformVndkVersion()
771 if ver := ctx.DeviceConfig().VndkVersion(); ver != "" && ver != "current" {
Logan Chienf3511742017-10-31 18:04:35 +0800772 vndkVersion = ver
773 }
774
Jayant Chowdharya4fce192017-09-06 13:10:03 -0700775 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800776 var SourceAbiFlags []string
777 for _, dir := range exportIncludeDirs.Strings() {
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700778 SourceAbiFlags = append(SourceAbiFlags, "-I"+dir)
779 }
780 for _, reexportedInclude := range extractExportIncludesFromFlags(library.sabi.Properties.ReexportedIncludeFlags) {
781 SourceAbiFlags = append(SourceAbiFlags, reexportedInclude)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800782 }
783 exportedHeaderFlags := strings.Join(SourceAbiFlags, " ")
Logan Chiene3d7a0d2019-01-17 00:18:02 +0800784 library.sAbiOutputFile = TransformDumpToLinkedDump(ctx, objs.sAbiDumpFiles, soFile, fileName, exportedHeaderFlags,
785 android.OptionalPathForModuleSrc(ctx, library.Properties.Header_abi_checker.Symbol_file),
786 library.Properties.Header_abi_checker.Exclude_symbol_versions,
787 library.Properties.Header_abi_checker.Exclude_symbol_tags)
Logan Chien2f2b8902018-07-10 15:01:19 +0800788
Logan Chien7eefdc42018-07-11 18:10:41 +0800789 refAbiDumpFile := getRefAbiDumpFile(ctx, vndkVersion, fileName)
790 if refAbiDumpFile != nil {
Logan Chienf3511742017-10-31 18:04:35 +0800791 library.sAbiDiff = SourceAbiDiff(ctx, library.sAbiOutputFile.Path(),
Logan Chien7eefdc42018-07-11 18:10:41 +0800792 refAbiDumpFile, fileName, exportedHeaderFlags, ctx.isVndkExt())
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800793 }
794 }
795}
796
Colin Crossb916a382016-07-29 17:28:03 -0700797func (library *libraryDecorator) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700798 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700799
Colin Crossad59e752017-11-16 14:29:11 -0800800 objs = deps.Objs.Copy().Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700801 var out android.Path
Colin Crossa48ab5b2017-02-14 15:28:44 -0800802 if library.static() || library.header() {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700803 out = library.linkStatic(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700804 } else {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700805 out = library.linkShared(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700806 }
807
808 library.exportIncludes(ctx, "-I")
809 library.reexportFlags(deps.ReexportedFlags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700810 library.reexportDeps(deps.ReexportedFlagsDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700811
Nan Zhang0007d812017-11-07 10:57:05 -0800812 if Bool(library.Properties.Aidl.Export_aidl_headers) {
Dan Willemsene1240db2016-11-03 14:28:51 -0700813 if library.baseCompiler.hasSrcExt(".aidl") {
Colin Cross10d22312017-05-03 11:01:58 -0700814 flags := []string{
Dan Willemsene1240db2016-11-03 14:28:51 -0700815 "-I" + android.PathForModuleGen(ctx, "aidl").String(),
Colin Cross10d22312017-05-03 11:01:58 -0700816 }
817 library.reexportFlags(flags)
818 library.reuseExportedFlags = append(library.reuseExportedFlags, flags...)
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800819 library.reexportDeps(library.baseCompiler.pathDeps) // TODO: restrict to aidl deps
820 library.reuseExportedDeps = append(library.reuseExportedDeps, library.baseCompiler.pathDeps...)
Dan Willemsene1240db2016-11-03 14:28:51 -0700821 }
822 }
823
Nan Zhang0007d812017-11-07 10:57:05 -0800824 if Bool(library.Properties.Proto.Export_proto_headers) {
Dan Willemsene1240db2016-11-03 14:28:51 -0700825 if library.baseCompiler.hasSrcExt(".proto") {
Dan Willemsenab9f4262018-02-14 13:58:34 -0800826 includes := []string{}
827 if flags.ProtoRoot {
828 includes = append(includes, "-I"+android.ProtoSubDir(ctx).String())
Colin Cross10d22312017-05-03 11:01:58 -0700829 }
Dan Willemsenab9f4262018-02-14 13:58:34 -0800830 includes = append(includes, "-I"+android.ProtoDir(ctx).String())
831 library.reexportFlags(includes)
832 library.reuseExportedFlags = append(library.reuseExportedFlags, includes...)
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800833 library.reexportDeps(library.baseCompiler.pathDeps) // TODO: restrict to proto deps
834 library.reuseExportedDeps = append(library.reuseExportedDeps, library.baseCompiler.pathDeps...)
Colin Cross0c461f12016-10-20 16:11:43 -0700835 }
836 }
837
Inseob Kim21f26902018-09-06 00:55:20 +0900838 if library.baseCompiler.hasSrcExt(".sysprop") {
839 flags := []string{
840 "-I" + android.PathForModuleGen(ctx, "sysprop", "include").String(),
841 }
842 library.reexportFlags(flags)
Sundong Ahn5b73f312018-12-19 14:39:29 +0900843 library.reexportDeps(library.baseCompiler.pathDeps)
Inseob Kim21f26902018-09-06 00:55:20 +0900844 library.reuseExportedFlags = append(library.reuseExportedFlags, flags...)
845 }
846
Jiyong Parkda732bd2018-11-02 18:23:15 +0900847 if library.buildStubs() {
848 library.reexportFlags([]string{"-D" + versioningMacroName(ctx.ModuleName()) + "=" + library.stubsVersion()})
849 }
850
Colin Cross4d9c2d12016-07-29 12:48:20 -0700851 return out
852}
853
Colin Crossb916a382016-07-29 17:28:03 -0700854func (library *libraryDecorator) buildStatic() bool {
Colin Cross38b40df2018-04-10 16:14:46 -0700855 return library.MutatedProperties.BuildStatic && BoolDefault(library.Properties.Static.Enabled, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700856}
857
Colin Crossb916a382016-07-29 17:28:03 -0700858func (library *libraryDecorator) buildShared() bool {
Colin Cross38b40df2018-04-10 16:14:46 -0700859 return library.MutatedProperties.BuildShared && BoolDefault(library.Properties.Shared.Enabled, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700860}
861
Colin Crossb916a382016-07-29 17:28:03 -0700862func (library *libraryDecorator) getWholeStaticMissingDeps() []string {
Colin Crossd2343a32018-04-30 14:50:01 -0700863 return append([]string(nil), library.wholeStaticMissingDeps...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700864}
865
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700866func (library *libraryDecorator) objs() Objects {
867 return library.objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700868}
869
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700870func (library *libraryDecorator) reuseObjs() (Objects, []string, android.Paths) {
871 return library.reuseObjects, library.reuseExportedFlags, library.reuseExportedDeps
Colin Cross4d9c2d12016-07-29 12:48:20 -0700872}
873
Colin Cross26c34ed2016-09-30 17:10:16 -0700874func (library *libraryDecorator) toc() android.OptionalPath {
875 return library.tocFile
876}
877
Colin Crossb916a382016-07-29 17:28:03 -0700878func (library *libraryDecorator) install(ctx ModuleContext, file android.Path) {
Colin Crossc43ae772017-04-14 15:42:53 -0700879 if library.shared() {
Justin Yun8fe12122017-12-07 17:18:15 +0900880 if ctx.Device() && ctx.useVndk() {
881 if ctx.isVndkSp() {
882 library.baseInstaller.subDir = "vndk-sp"
883 } else if ctx.isVndk() {
884 library.baseInstaller.subDir = "vndk"
885 }
Logan Chienf3511742017-10-31 18:04:35 +0800886
887 // Append a version to vndk or vndk-sp directories on the system partition.
888 if ctx.isVndk() && !ctx.isVndkExt() {
889 vndkVersion := ctx.DeviceConfig().PlatformVndkVersion()
890 if vndkVersion != "current" && vndkVersion != "" {
891 library.baseInstaller.subDir += "-" + vndkVersion
892 }
Justin Yun8effde42017-06-23 19:24:43 +0900893 }
Jiyong Park429660f2019-01-16 22:31:11 +0900894 } else if len(library.Properties.Stubs.Versions) > 0 && android.DirectlyInAnyApex(ctx, ctx.ModuleName()) {
895 // If a library in an APEX has stable versioned APIs, we basically don't need
896 // to have the platform variant of the library in /system partition because
897 // platform components can just use the lib from the APEX without fearing about
898 // compatibility. However, if the library is required for some early processes
899 // before the APEX is activated, the platform variant may also be required.
900 // In that case, it is installed to the subdirectory 'bootstrap' in order to
901 // be distinguished/isolated from other non-bootstrap libraries in /system/lib
902 // so that the bootstrap libraries are used only when the APEX isn't ready.
903 if !library.buildStubs() && ctx.Arch().Native {
904 library.baseInstaller.subDir = "bootstrap"
905 }
Justin Yun8effde42017-06-23 19:24:43 +0900906 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700907 library.baseInstaller.install(ctx, file)
908 }
Dan Albertf563d252017-10-13 00:29:00 -0700909
Dan Albert281f22b2017-12-13 15:03:47 -0800910 if Bool(library.Properties.Static_ndk_lib) && library.static() &&
Jiyong Parkf9332f12018-02-01 00:54:12 +0900911 !ctx.useVndk() && !ctx.inRecovery() && ctx.Device() &&
Jiyong Park7ed9de32018-10-15 22:25:07 +0900912 library.baseLinker.sanitize.isUnsanitizedVariant() &&
913 !library.buildStubs() {
Dan Albertf563d252017-10-13 00:29:00 -0700914 installPath := getNdkSysrootBase(ctx).Join(
Dan Albertea4b7b92018-04-25 16:05:30 -0700915 ctx, "usr/lib", config.NDKTriple(ctx.toolchain()), file.Base())
Dan Albertf563d252017-10-13 00:29:00 -0700916
917 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
918 Rule: android.Cp,
919 Description: "install " + installPath.Base(),
920 Output: installPath,
921 Input: file,
922 })
923
Colin Cross0875c522017-11-28 17:34:01 -0800924 library.ndkSysrootPath = installPath
Dan Albertf563d252017-10-13 00:29:00 -0700925 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700926}
927
Colin Crossb916a382016-07-29 17:28:03 -0700928func (library *libraryDecorator) static() bool {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800929 return library.MutatedProperties.VariantIsStatic
Colin Cross4d9c2d12016-07-29 12:48:20 -0700930}
931
Colin Crossa48ab5b2017-02-14 15:28:44 -0800932func (library *libraryDecorator) shared() bool {
933 return library.MutatedProperties.VariantIsShared
934}
935
936func (library *libraryDecorator) header() bool {
937 return !library.static() && !library.shared()
938}
939
940func (library *libraryDecorator) setStatic() {
941 library.MutatedProperties.VariantIsStatic = true
942 library.MutatedProperties.VariantIsShared = false
943}
944
945func (library *libraryDecorator) setShared() {
946 library.MutatedProperties.VariantIsStatic = false
947 library.MutatedProperties.VariantIsShared = true
Colin Crossb916a382016-07-29 17:28:03 -0700948}
949
Colin Crossab3b7322016-12-09 14:46:15 -0800950func (library *libraryDecorator) BuildOnlyStatic() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800951 library.MutatedProperties.BuildShared = false
Colin Crossab3b7322016-12-09 14:46:15 -0800952}
953
954func (library *libraryDecorator) BuildOnlyShared() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800955 library.MutatedProperties.BuildStatic = false
Colin Crossab3b7322016-12-09 14:46:15 -0800956}
957
Colin Cross5950f382016-12-13 12:50:57 -0800958func (library *libraryDecorator) HeaderOnly() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800959 library.MutatedProperties.BuildShared = false
960 library.MutatedProperties.BuildStatic = false
Colin Cross5950f382016-12-13 12:50:57 -0800961}
962
Jiyong Park7ed9de32018-10-15 22:25:07 +0900963func (library *libraryDecorator) buildStubs() bool {
964 return library.MutatedProperties.BuildStubs
965}
966
967func (library *libraryDecorator) stubsVersion() string {
968 return library.MutatedProperties.StubsVersion
969}
970
Colin Cross571cccf2019-02-04 11:22:08 -0800971var versioningMacroNamesListKey = android.NewOnceKey("versioningMacroNamesList")
972
Jiyong Parkda732bd2018-11-02 18:23:15 +0900973func versioningMacroNamesList(config android.Config) *map[string]string {
Colin Cross571cccf2019-02-04 11:22:08 -0800974 return config.Once(versioningMacroNamesListKey, func() interface{} {
Jiyong Parkda732bd2018-11-02 18:23:15 +0900975 m := make(map[string]string)
976 return &m
977 }).(*map[string]string)
978}
979
980// alphanumeric and _ characters are preserved.
981// other characters are all converted to _
982var charsNotForMacro = regexp.MustCompile("[^a-zA-Z0-9_]+")
983
984func versioningMacroName(moduleName string) string {
985 macroName := charsNotForMacro.ReplaceAllString(moduleName, "_")
986 macroName = strings.ToUpper(moduleName)
987 return "__" + macroName + "_API__"
988}
989
Colin Crossab3b7322016-12-09 14:46:15 -0800990func NewLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700991 module := newModule(hod, android.MultilibBoth)
992
Colin Crossb916a382016-07-29 17:28:03 -0700993 library := &libraryDecorator{
Colin Crossa48ab5b2017-02-14 15:28:44 -0800994 MutatedProperties: LibraryMutatedProperties{
Colin Crossab3b7322016-12-09 14:46:15 -0800995 BuildShared: true,
996 BuildStatic: true,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700997 },
Colin Crossb916a382016-07-29 17:28:03 -0700998 baseCompiler: NewBaseCompiler(),
Dan Albert61f32122018-07-26 14:00:24 -0700999 baseLinker: NewBaseLinker(module.sanitize),
Colin Crossb916a382016-07-29 17:28:03 -07001000 baseInstaller: NewBaseInstaller("lib", "lib64", InstallInSystem),
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -08001001 sabi: module.sabi,
Colin Cross4d9c2d12016-07-29 12:48:20 -07001002 }
1003
Colin Crossb916a382016-07-29 17:28:03 -07001004 module.compiler = library
1005 module.linker = library
1006 module.installer = library
1007
1008 return module, library
1009}
1010
Colin Cross10d22312017-05-03 11:01:58 -07001011// connects a shared library to a static library in order to reuse its .o files to avoid
1012// compiling source files twice.
1013func reuseStaticLibrary(mctx android.BottomUpMutatorContext, static, shared *Module) {
1014 if staticCompiler, ok := static.compiler.(*libraryDecorator); ok {
1015 sharedCompiler := shared.compiler.(*libraryDecorator)
Dan Willemsen3a26eef2018-12-03 15:25:46 -08001016
1017 // Check libraries in addition to cflags, since libraries may be exporting different
1018 // include directories.
Colin Cross10d22312017-05-03 11:01:58 -07001019 if len(staticCompiler.Properties.Static.Cflags) == 0 &&
Dan Willemsen3a26eef2018-12-03 15:25:46 -08001020 len(sharedCompiler.Properties.Shared.Cflags) == 0 &&
1021 len(staticCompiler.Properties.Static.Whole_static_libs) == 0 &&
1022 len(sharedCompiler.Properties.Shared.Whole_static_libs) == 0 &&
1023 len(staticCompiler.Properties.Static.Static_libs) == 0 &&
1024 len(sharedCompiler.Properties.Shared.Static_libs) == 0 &&
1025 len(staticCompiler.Properties.Static.Shared_libs) == 0 &&
1026 len(sharedCompiler.Properties.Shared.Shared_libs) == 0 &&
1027 staticCompiler.Properties.Static.System_shared_libs == nil &&
1028 sharedCompiler.Properties.Shared.System_shared_libs == nil {
Colin Cross10d22312017-05-03 11:01:58 -07001029
1030 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
1031 sharedCompiler.baseCompiler.Properties.OriginalSrcs =
1032 sharedCompiler.baseCompiler.Properties.Srcs
1033 sharedCompiler.baseCompiler.Properties.Srcs = nil
1034 sharedCompiler.baseCompiler.Properties.Generated_sources = nil
1035 }
1036 }
1037}
1038
Colin Crosse40b4ea2018-10-02 22:25:58 -07001039func LinkageMutator(mctx android.BottomUpMutatorContext) {
Colin Crossb916a382016-07-29 17:28:03 -07001040 if m, ok := mctx.Module().(*Module); ok && m.linker != nil {
1041 if library, ok := m.linker.(libraryInterface); ok {
1042 var modules []blueprint.Module
1043 if library.buildStatic() && library.buildShared() {
1044 modules = mctx.CreateLocalVariations("static", "shared")
1045 static := modules[0].(*Module)
1046 shared := modules[1].(*Module)
1047
Colin Crossa48ab5b2017-02-14 15:28:44 -08001048 static.linker.(libraryInterface).setStatic()
1049 shared.linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -07001050
Colin Cross10d22312017-05-03 11:01:58 -07001051 reuseStaticLibrary(mctx, static, shared)
1052
Colin Crossb916a382016-07-29 17:28:03 -07001053 } else if library.buildStatic() {
1054 modules = mctx.CreateLocalVariations("static")
Colin Crossa48ab5b2017-02-14 15:28:44 -08001055 modules[0].(*Module).linker.(libraryInterface).setStatic()
Colin Crossb916a382016-07-29 17:28:03 -07001056 } else if library.buildShared() {
1057 modules = mctx.CreateLocalVariations("shared")
Colin Crossa48ab5b2017-02-14 15:28:44 -08001058 modules[0].(*Module).linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -07001059 }
1060 }
1061 }
Colin Cross4d9c2d12016-07-29 12:48:20 -07001062}
Jiyong Park7ed9de32018-10-15 22:25:07 +09001063
Colin Cross571cccf2019-02-04 11:22:08 -08001064var stubVersionsKey = android.NewOnceKey("stubVersions")
1065
Jiyong Park25fc6a92018-11-18 18:02:45 +09001066// maps a module name to the list of stubs versions available for the module
1067func stubsVersionsFor(config android.Config) map[string][]string {
Colin Cross571cccf2019-02-04 11:22:08 -08001068 return config.Once(stubVersionsKey, func() interface{} {
Jiyong Park25fc6a92018-11-18 18:02:45 +09001069 return make(map[string][]string)
1070 }).(map[string][]string)
1071}
1072
1073var stubsVersionsLock sync.Mutex
1074
1075func latestStubsVersionFor(config android.Config, name string) string {
1076 versions, ok := stubsVersionsFor(config)[name]
1077 if ok && len(versions) > 0 {
1078 // the versions are alreay sorted in ascending order
1079 return versions[len(versions)-1]
1080 }
1081 return ""
1082}
1083
Jiyong Park7ed9de32018-10-15 22:25:07 +09001084// Version mutator splits a module into the mandatory non-stubs variant
Jiyong Park25fc6a92018-11-18 18:02:45 +09001085// (which is unnamed) and zero or more stubs variants.
1086func VersionMutator(mctx android.BottomUpMutatorContext) {
Jiyong Park7ed9de32018-10-15 22:25:07 +09001087 if m, ok := mctx.Module().(*Module); ok && !m.inRecovery() && m.linker != nil {
Jiyong Park25fc6a92018-11-18 18:02:45 +09001088 if library, ok := m.linker.(*libraryDecorator); ok && library.buildShared() &&
1089 len(library.Properties.Stubs.Versions) > 0 {
1090 versions := []string{}
Jiyong Park7ed9de32018-10-15 22:25:07 +09001091 for _, v := range library.Properties.Stubs.Versions {
Jiyong Park25fc6a92018-11-18 18:02:45 +09001092 if _, err := strconv.Atoi(v); err != nil {
1093 mctx.PropertyErrorf("versions", "%q is not a number", v)
1094 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09001095 versions = append(versions, v)
1096 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001097 sort.Slice(versions, func(i, j int) bool {
1098 left, _ := strconv.Atoi(versions[i])
1099 right, _ := strconv.Atoi(versions[j])
1100 return left < right
1101 })
1102
1103 // save the list of versions for later use
1104 copiedVersions := make([]string, len(versions))
1105 copy(copiedVersions, versions)
1106 stubsVersionsLock.Lock()
1107 defer stubsVersionsLock.Unlock()
1108 stubsVersionsFor(mctx.Config())[mctx.ModuleName()] = copiedVersions
1109
1110 // "" is for the non-stubs variant
Jiyong Park67883b32019-01-03 21:35:58 +09001111 versions = append([]string{""}, versions...)
Jiyong Park25fc6a92018-11-18 18:02:45 +09001112
Jiyong Park7ed9de32018-10-15 22:25:07 +09001113 modules := mctx.CreateVariations(versions...)
1114 for i, m := range modules {
1115 l := m.(*Module).linker.(*libraryDecorator)
Jiyong Park25fc6a92018-11-18 18:02:45 +09001116 if versions[i] != "" {
1117 l.MutatedProperties.BuildStubs = true
1118 l.MutatedProperties.StubsVersion = versions[i]
1119 m.(*Module).Properties.HideFromMake = true
Jiyong Park090d9df2018-12-11 02:47:16 +09001120 m.(*Module).sanitize = nil
1121 m.(*Module).stl = nil
Jiyong Park127d5652018-12-12 10:26:20 +09001122 m.(*Module).Properties.PreventInstall = true
Jiyong Park7ed9de32018-10-15 22:25:07 +09001123 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09001124 }
1125 } else {
1126 mctx.CreateVariations("")
1127 }
1128 return
1129 }
1130 if genrule, ok := mctx.Module().(*genrule.Module); ok {
1131 if props, ok := genrule.Extra.(*GenruleExtraProperties); ok && !props.InRecovery {
1132 mctx.CreateVariations("")
1133 return
1134 }
1135 }
1136}