blob: 138ba43b0fc593f4eb076bd2b295ec57dc5d9b87 [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)
583 }
Jiyong Parkf9332f12018-02-01 00:54:12 +0900584 if ctx.inRecovery() {
585 deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Recovery.Exclude_static_libs)
586 deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Recovery.Exclude_shared_libs)
587 deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Recovery.Exclude_static_libs)
588 }
Colin Cross2383f3b2018-02-06 14:40:13 -0800589
Colin Cross2383f3b2018-02-06 14:40:13 -0800590 android.ExtractSourceDeps(ctx, library.Properties.Unexported_symbols_list)
591 android.ExtractSourceDeps(ctx, library.Properties.Force_symbols_not_weak_list)
592 android.ExtractSourceDeps(ctx, library.Properties.Force_symbols_weak_list)
Colin Cross2383f3b2018-02-06 14:40:13 -0800593
Colin Cross4d9c2d12016-07-29 12:48:20 -0700594 return deps
595}
596
Colin Crossb916a382016-07-29 17:28:03 -0700597func (library *libraryDecorator) linkStatic(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700598 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700599
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700600 library.objects = deps.WholeStaticLibObjs.Copy()
601 library.objects = library.objects.Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700602
Colin Cross86803cf2018-02-15 14:12:26 -0800603 fileName := ctx.ModuleName() + library.MutatedProperties.VariantName + staticLibraryExtension
604 outputFile := android.PathForModuleOut(ctx, fileName)
Dan Willemsen581341d2017-02-09 16:16:31 -0800605 builderFlags := flagsToBuilderFlags(flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700606
Dan Willemsen569edc52018-11-19 09:33:29 -0800607 if Bool(library.baseLinker.Properties.Use_version_lib) {
608 if ctx.Host() {
609 versionedOutputFile := outputFile
610 outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
611 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
612 } else {
613 versionedOutputFile := android.PathForModuleOut(ctx, "versioned", fileName)
614 library.distFile = android.OptionalPathForPath(versionedOutputFile)
615 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
616 }
Colin Cross86803cf2018-02-15 14:12:26 -0800617 }
618
Dan Willemsen581341d2017-02-09 16:16:31 -0800619 TransformObjToStaticLib(ctx, library.objects.objFiles, builderFlags, outputFile, objs.tidyFiles)
620
621 library.coverageOutputFile = TransformCoverageFilesToLib(ctx, library.objects, builderFlags,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800622 ctx.ModuleName()+library.MutatedProperties.VariantName)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700623
624 library.wholeStaticMissingDeps = ctx.GetMissingDependencies()
625
626 ctx.CheckbuildFile(outputFile)
627
628 return outputFile
629}
630
Colin Crossb916a382016-07-29 17:28:03 -0700631func (library *libraryDecorator) linkShared(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700632 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700633
634 var linkerDeps android.Paths
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700635 linkerDeps = append(linkerDeps, flags.LdFlagsDeps...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700636
Colin Cross2383f3b2018-02-06 14:40:13 -0800637 unexportedSymbols := ctx.ExpandOptionalSource(library.Properties.Unexported_symbols_list, "unexported_symbols_list")
638 forceNotWeakSymbols := ctx.ExpandOptionalSource(library.Properties.Force_symbols_not_weak_list, "force_symbols_not_weak_list")
639 forceWeakSymbols := ctx.ExpandOptionalSource(library.Properties.Force_symbols_weak_list, "force_symbols_weak_list")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700640 if !ctx.Darwin() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700641 if unexportedSymbols.Valid() {
642 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
643 }
644 if forceNotWeakSymbols.Valid() {
645 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
646 }
647 if forceWeakSymbols.Valid() {
648 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
649 }
650 } else {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700651 if unexportedSymbols.Valid() {
652 flags.LdFlags = append(flags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
653 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
654 }
655 if forceNotWeakSymbols.Valid() {
656 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
657 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
658 }
659 if forceWeakSymbols.Valid() {
660 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
661 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
662 }
663 }
Jiyong Parkc1e7f482019-01-12 13:39:10 +0900664 if library.buildStubs() {
665 linkerScriptFlags := "-Wl,--version-script," + library.versionScriptPath.String()
666 flags.LdFlags = append(flags.LdFlags, linkerScriptFlags)
667 linkerDeps = append(linkerDeps, library.versionScriptPath)
668 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700669
670 fileName := library.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
671 outputFile := android.PathForModuleOut(ctx, fileName)
672 ret := outputFile
673
674 builderFlags := flagsToBuilderFlags(flags)
675
Colin Crossb496cfd2018-09-10 16:50:05 -0700676 // Optimize out relinking against shared libraries whose interface hasn't changed by
677 // depending on a table of contents file instead of the library itself.
678 tocPath := outputFile.RelPathString()
679 tocPath = pathtools.ReplaceExtension(tocPath, flags.Toolchain.ShlibSuffix()[1:]+".toc")
680 tocFile := android.PathForOutput(ctx, tocPath)
681 library.tocFile = android.OptionalPathForPath(tocFile)
682 TransformSharedObjectToToc(ctx, outputFile, tocFile, builderFlags)
Colin Cross89562dc2016-10-03 17:47:19 -0700683
Colin Cross4d9c2d12016-07-29 12:48:20 -0700684 if library.stripper.needsStrip(ctx) {
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -0700685 // b/80093681, GNU strip/objcopy bug.
686 // Use llvm-{strip,objcopy} when clang lld is used.
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700687 builderFlags.stripUseLlvmStrip = library.baseLinker.useClangLld(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700688 strippedOutputFile := outputFile
689 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
690 library.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
691 }
692
Colin Crossb60190a2018-09-04 16:28:17 -0700693 library.unstrippedOutputFile = outputFile
694
Dan Willemsen569edc52018-11-19 09:33:29 -0800695 if Bool(library.baseLinker.Properties.Use_version_lib) {
696 if ctx.Host() {
697 versionedOutputFile := outputFile
698 outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
699 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
700 } else {
701 versionedOutputFile := android.PathForModuleOut(ctx, "versioned", fileName)
702 library.distFile = android.OptionalPathForPath(versionedOutputFile)
703
704 if library.stripper.needsStrip(ctx) {
705 out := android.PathForModuleOut(ctx, "versioned-stripped", fileName)
706 library.distFile = android.OptionalPathForPath(out)
707 library.stripper.strip(ctx, versionedOutputFile, out, builderFlags)
708 }
709
710 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
711 }
Colin Cross86803cf2018-02-15 14:12:26 -0800712 }
713
Jiyong Park64a44f22019-01-18 14:37:08 +0900714 sharedLibs := deps.EarlySharedLibs
715 sharedLibs = append(sharedLibs, deps.SharedLibs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700716 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
717
Jiyong Park64a44f22019-01-18 14:37:08 +0900718 linkerDeps = append(linkerDeps, deps.EarlySharedLibsDeps...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700719 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
720 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700721 linkerDeps = append(linkerDeps, objs.tidyFiles...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700722
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700723 TransformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700724 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
725 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile)
726
Dan Willemsen581341d2017-02-09 16:16:31 -0800727 objs.coverageFiles = append(objs.coverageFiles, deps.StaticLibObjs.coverageFiles...)
728 objs.coverageFiles = append(objs.coverageFiles, deps.WholeStaticLibObjs.coverageFiles...)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800729
730 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.StaticLibObjs.sAbiDumpFiles...)
731 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.WholeStaticLibObjs.sAbiDumpFiles...)
732
Dan Willemsen581341d2017-02-09 16:16:31 -0800733 library.coverageOutputFile = TransformCoverageFilesToLib(ctx, objs, builderFlags, library.getLibName(ctx))
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700734 library.linkSAbiDumpFiles(ctx, objs, fileName, ret)
Dan Willemsen581341d2017-02-09 16:16:31 -0800735
Colin Cross4d9c2d12016-07-29 12:48:20 -0700736 return ret
737}
738
Logan Chien7eefdc42018-07-11 18:10:41 +0800739func getRefAbiDumpFile(ctx ModuleContext, vndkVersion, fileName string) android.Path {
Logan Chienf4b79c62018-08-02 02:27:02 +0800740 isLlndk := inList(ctx.baseModuleName(), llndkLibraries) || inList(ctx.baseModuleName(), ndkMigratedLibs)
Logan Chien7eefdc42018-07-11 18:10:41 +0800741
742 refAbiDumpTextFile := android.PathForVndkRefAbiDump(ctx, vndkVersion, fileName, isLlndk, false)
743 refAbiDumpGzipFile := android.PathForVndkRefAbiDump(ctx, vndkVersion, fileName, isLlndk, true)
744
745 if refAbiDumpTextFile.Valid() {
746 if refAbiDumpGzipFile.Valid() {
747 ctx.ModuleErrorf(
748 "Two reference ABI dump files are found: %q and %q. Please delete the stale one.",
749 refAbiDumpTextFile, refAbiDumpGzipFile)
750 return nil
751 }
752 return refAbiDumpTextFile.Path()
753 }
754 if refAbiDumpGzipFile.Valid() {
755 return UnzipRefDump(ctx, refAbiDumpGzipFile.Path(), fileName)
756 }
757 return nil
758}
759
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700760func (library *libraryDecorator) linkSAbiDumpFiles(ctx ModuleContext, objs Objects, fileName string, soFile android.Path) {
Logan Chien2f2b8902018-07-10 15:01:19 +0800761 if len(objs.sAbiDumpFiles) > 0 && ctx.shouldCreateVndkSourceAbiDump() {
Logan Chiena8f51582018-03-21 11:53:48 +0800762 vndkVersion := ctx.DeviceConfig().PlatformVndkVersion()
763 if ver := ctx.DeviceConfig().VndkVersion(); ver != "" && ver != "current" {
Logan Chienf3511742017-10-31 18:04:35 +0800764 vndkVersion = ver
765 }
766
Jayant Chowdharya4fce192017-09-06 13:10:03 -0700767 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800768 var SourceAbiFlags []string
769 for _, dir := range exportIncludeDirs.Strings() {
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700770 SourceAbiFlags = append(SourceAbiFlags, "-I"+dir)
771 }
772 for _, reexportedInclude := range extractExportIncludesFromFlags(library.sabi.Properties.ReexportedIncludeFlags) {
773 SourceAbiFlags = append(SourceAbiFlags, reexportedInclude)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800774 }
775 exportedHeaderFlags := strings.Join(SourceAbiFlags, " ")
Logan Chiene3d7a0d2019-01-17 00:18:02 +0800776 library.sAbiOutputFile = TransformDumpToLinkedDump(ctx, objs.sAbiDumpFiles, soFile, fileName, exportedHeaderFlags,
777 android.OptionalPathForModuleSrc(ctx, library.Properties.Header_abi_checker.Symbol_file),
778 library.Properties.Header_abi_checker.Exclude_symbol_versions,
779 library.Properties.Header_abi_checker.Exclude_symbol_tags)
Logan Chien2f2b8902018-07-10 15:01:19 +0800780
Logan Chien7eefdc42018-07-11 18:10:41 +0800781 refAbiDumpFile := getRefAbiDumpFile(ctx, vndkVersion, fileName)
782 if refAbiDumpFile != nil {
Logan Chienf3511742017-10-31 18:04:35 +0800783 library.sAbiDiff = SourceAbiDiff(ctx, library.sAbiOutputFile.Path(),
Logan Chien7eefdc42018-07-11 18:10:41 +0800784 refAbiDumpFile, fileName, exportedHeaderFlags, ctx.isVndkExt())
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800785 }
786 }
787}
788
Colin Crossb916a382016-07-29 17:28:03 -0700789func (library *libraryDecorator) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700790 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700791
Colin Crossad59e752017-11-16 14:29:11 -0800792 objs = deps.Objs.Copy().Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700793 var out android.Path
Colin Crossa48ab5b2017-02-14 15:28:44 -0800794 if library.static() || library.header() {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700795 out = library.linkStatic(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700796 } else {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700797 out = library.linkShared(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700798 }
799
800 library.exportIncludes(ctx, "-I")
801 library.reexportFlags(deps.ReexportedFlags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700802 library.reexportDeps(deps.ReexportedFlagsDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700803
Nan Zhang0007d812017-11-07 10:57:05 -0800804 if Bool(library.Properties.Aidl.Export_aidl_headers) {
Dan Willemsene1240db2016-11-03 14:28:51 -0700805 if library.baseCompiler.hasSrcExt(".aidl") {
Colin Cross10d22312017-05-03 11:01:58 -0700806 flags := []string{
Dan Willemsene1240db2016-11-03 14:28:51 -0700807 "-I" + android.PathForModuleGen(ctx, "aidl").String(),
Colin Cross10d22312017-05-03 11:01:58 -0700808 }
809 library.reexportFlags(flags)
810 library.reuseExportedFlags = append(library.reuseExportedFlags, flags...)
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800811 library.reexportDeps(library.baseCompiler.pathDeps) // TODO: restrict to aidl deps
812 library.reuseExportedDeps = append(library.reuseExportedDeps, library.baseCompiler.pathDeps...)
Dan Willemsene1240db2016-11-03 14:28:51 -0700813 }
814 }
815
Nan Zhang0007d812017-11-07 10:57:05 -0800816 if Bool(library.Properties.Proto.Export_proto_headers) {
Dan Willemsene1240db2016-11-03 14:28:51 -0700817 if library.baseCompiler.hasSrcExt(".proto") {
Dan Willemsenab9f4262018-02-14 13:58:34 -0800818 includes := []string{}
819 if flags.ProtoRoot {
820 includes = append(includes, "-I"+android.ProtoSubDir(ctx).String())
Colin Cross10d22312017-05-03 11:01:58 -0700821 }
Dan Willemsenab9f4262018-02-14 13:58:34 -0800822 includes = append(includes, "-I"+android.ProtoDir(ctx).String())
823 library.reexportFlags(includes)
824 library.reuseExportedFlags = append(library.reuseExportedFlags, includes...)
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800825 library.reexportDeps(library.baseCompiler.pathDeps) // TODO: restrict to proto deps
826 library.reuseExportedDeps = append(library.reuseExportedDeps, library.baseCompiler.pathDeps...)
Colin Cross0c461f12016-10-20 16:11:43 -0700827 }
828 }
829
Inseob Kim21f26902018-09-06 00:55:20 +0900830 if library.baseCompiler.hasSrcExt(".sysprop") {
831 flags := []string{
832 "-I" + android.PathForModuleGen(ctx, "sysprop", "include").String(),
833 }
834 library.reexportFlags(flags)
Sundong Ahn5b73f312018-12-19 14:39:29 +0900835 library.reexportDeps(library.baseCompiler.pathDeps)
Inseob Kim21f26902018-09-06 00:55:20 +0900836 library.reuseExportedFlags = append(library.reuseExportedFlags, flags...)
837 }
838
Jiyong Parkda732bd2018-11-02 18:23:15 +0900839 if library.buildStubs() {
840 library.reexportFlags([]string{"-D" + versioningMacroName(ctx.ModuleName()) + "=" + library.stubsVersion()})
841 }
842
Colin Cross4d9c2d12016-07-29 12:48:20 -0700843 return out
844}
845
Colin Crossb916a382016-07-29 17:28:03 -0700846func (library *libraryDecorator) buildStatic() bool {
Colin Cross38b40df2018-04-10 16:14:46 -0700847 return library.MutatedProperties.BuildStatic && BoolDefault(library.Properties.Static.Enabled, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700848}
849
Colin Crossb916a382016-07-29 17:28:03 -0700850func (library *libraryDecorator) buildShared() bool {
Colin Cross38b40df2018-04-10 16:14:46 -0700851 return library.MutatedProperties.BuildShared && BoolDefault(library.Properties.Shared.Enabled, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700852}
853
Colin Crossb916a382016-07-29 17:28:03 -0700854func (library *libraryDecorator) getWholeStaticMissingDeps() []string {
Colin Crossd2343a32018-04-30 14:50:01 -0700855 return append([]string(nil), library.wholeStaticMissingDeps...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700856}
857
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700858func (library *libraryDecorator) objs() Objects {
859 return library.objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700860}
861
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700862func (library *libraryDecorator) reuseObjs() (Objects, []string, android.Paths) {
863 return library.reuseObjects, library.reuseExportedFlags, library.reuseExportedDeps
Colin Cross4d9c2d12016-07-29 12:48:20 -0700864}
865
Colin Cross26c34ed2016-09-30 17:10:16 -0700866func (library *libraryDecorator) toc() android.OptionalPath {
867 return library.tocFile
868}
869
Colin Crossb916a382016-07-29 17:28:03 -0700870func (library *libraryDecorator) install(ctx ModuleContext, file android.Path) {
Colin Crossc43ae772017-04-14 15:42:53 -0700871 if library.shared() {
Justin Yun8fe12122017-12-07 17:18:15 +0900872 if ctx.Device() && ctx.useVndk() {
873 if ctx.isVndkSp() {
874 library.baseInstaller.subDir = "vndk-sp"
875 } else if ctx.isVndk() {
876 library.baseInstaller.subDir = "vndk"
877 }
Logan Chienf3511742017-10-31 18:04:35 +0800878
879 // Append a version to vndk or vndk-sp directories on the system partition.
880 if ctx.isVndk() && !ctx.isVndkExt() {
881 vndkVersion := ctx.DeviceConfig().PlatformVndkVersion()
882 if vndkVersion != "current" && vndkVersion != "" {
883 library.baseInstaller.subDir += "-" + vndkVersion
884 }
Justin Yun8effde42017-06-23 19:24:43 +0900885 }
886 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700887 library.baseInstaller.install(ctx, file)
888 }
Dan Albertf563d252017-10-13 00:29:00 -0700889
Dan Albert281f22b2017-12-13 15:03:47 -0800890 if Bool(library.Properties.Static_ndk_lib) && library.static() &&
Jiyong Parkf9332f12018-02-01 00:54:12 +0900891 !ctx.useVndk() && !ctx.inRecovery() && ctx.Device() &&
Jiyong Park7ed9de32018-10-15 22:25:07 +0900892 library.baseLinker.sanitize.isUnsanitizedVariant() &&
893 !library.buildStubs() {
Dan Albertf563d252017-10-13 00:29:00 -0700894 installPath := getNdkSysrootBase(ctx).Join(
Dan Albertea4b7b92018-04-25 16:05:30 -0700895 ctx, "usr/lib", config.NDKTriple(ctx.toolchain()), file.Base())
Dan Albertf563d252017-10-13 00:29:00 -0700896
897 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
898 Rule: android.Cp,
899 Description: "install " + installPath.Base(),
900 Output: installPath,
901 Input: file,
902 })
903
Colin Cross0875c522017-11-28 17:34:01 -0800904 library.ndkSysrootPath = installPath
Dan Albertf563d252017-10-13 00:29:00 -0700905 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700906}
907
Colin Crossb916a382016-07-29 17:28:03 -0700908func (library *libraryDecorator) static() bool {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800909 return library.MutatedProperties.VariantIsStatic
Colin Cross4d9c2d12016-07-29 12:48:20 -0700910}
911
Colin Crossa48ab5b2017-02-14 15:28:44 -0800912func (library *libraryDecorator) shared() bool {
913 return library.MutatedProperties.VariantIsShared
914}
915
916func (library *libraryDecorator) header() bool {
917 return !library.static() && !library.shared()
918}
919
920func (library *libraryDecorator) setStatic() {
921 library.MutatedProperties.VariantIsStatic = true
922 library.MutatedProperties.VariantIsShared = false
923}
924
925func (library *libraryDecorator) setShared() {
926 library.MutatedProperties.VariantIsStatic = false
927 library.MutatedProperties.VariantIsShared = true
Colin Crossb916a382016-07-29 17:28:03 -0700928}
929
Colin Crossab3b7322016-12-09 14:46:15 -0800930func (library *libraryDecorator) BuildOnlyStatic() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800931 library.MutatedProperties.BuildShared = false
Colin Crossab3b7322016-12-09 14:46:15 -0800932}
933
934func (library *libraryDecorator) BuildOnlyShared() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800935 library.MutatedProperties.BuildStatic = false
Colin Crossab3b7322016-12-09 14:46:15 -0800936}
937
Colin Cross5950f382016-12-13 12:50:57 -0800938func (library *libraryDecorator) HeaderOnly() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800939 library.MutatedProperties.BuildShared = false
940 library.MutatedProperties.BuildStatic = false
Colin Cross5950f382016-12-13 12:50:57 -0800941}
942
Jiyong Park7ed9de32018-10-15 22:25:07 +0900943func (library *libraryDecorator) buildStubs() bool {
944 return library.MutatedProperties.BuildStubs
945}
946
947func (library *libraryDecorator) stubsVersion() string {
948 return library.MutatedProperties.StubsVersion
949}
950
Jiyong Parkda732bd2018-11-02 18:23:15 +0900951func versioningMacroNamesList(config android.Config) *map[string]string {
952 return config.Once("versioningMacroNamesList", func() interface{} {
953 m := make(map[string]string)
954 return &m
955 }).(*map[string]string)
956}
957
958// alphanumeric and _ characters are preserved.
959// other characters are all converted to _
960var charsNotForMacro = regexp.MustCompile("[^a-zA-Z0-9_]+")
961
962func versioningMacroName(moduleName string) string {
963 macroName := charsNotForMacro.ReplaceAllString(moduleName, "_")
964 macroName = strings.ToUpper(moduleName)
965 return "__" + macroName + "_API__"
966}
967
Colin Crossab3b7322016-12-09 14:46:15 -0800968func NewLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700969 module := newModule(hod, android.MultilibBoth)
970
Colin Crossb916a382016-07-29 17:28:03 -0700971 library := &libraryDecorator{
Colin Crossa48ab5b2017-02-14 15:28:44 -0800972 MutatedProperties: LibraryMutatedProperties{
Colin Crossab3b7322016-12-09 14:46:15 -0800973 BuildShared: true,
974 BuildStatic: true,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700975 },
Colin Crossb916a382016-07-29 17:28:03 -0700976 baseCompiler: NewBaseCompiler(),
Dan Albert61f32122018-07-26 14:00:24 -0700977 baseLinker: NewBaseLinker(module.sanitize),
Colin Crossb916a382016-07-29 17:28:03 -0700978 baseInstaller: NewBaseInstaller("lib", "lib64", InstallInSystem),
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800979 sabi: module.sabi,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700980 }
981
Colin Crossb916a382016-07-29 17:28:03 -0700982 module.compiler = library
983 module.linker = library
984 module.installer = library
985
986 return module, library
987}
988
Colin Cross10d22312017-05-03 11:01:58 -0700989// connects a shared library to a static library in order to reuse its .o files to avoid
990// compiling source files twice.
991func reuseStaticLibrary(mctx android.BottomUpMutatorContext, static, shared *Module) {
992 if staticCompiler, ok := static.compiler.(*libraryDecorator); ok {
993 sharedCompiler := shared.compiler.(*libraryDecorator)
Dan Willemsen3a26eef2018-12-03 15:25:46 -0800994
995 // Check libraries in addition to cflags, since libraries may be exporting different
996 // include directories.
Colin Cross10d22312017-05-03 11:01:58 -0700997 if len(staticCompiler.Properties.Static.Cflags) == 0 &&
Dan Willemsen3a26eef2018-12-03 15:25:46 -0800998 len(sharedCompiler.Properties.Shared.Cflags) == 0 &&
999 len(staticCompiler.Properties.Static.Whole_static_libs) == 0 &&
1000 len(sharedCompiler.Properties.Shared.Whole_static_libs) == 0 &&
1001 len(staticCompiler.Properties.Static.Static_libs) == 0 &&
1002 len(sharedCompiler.Properties.Shared.Static_libs) == 0 &&
1003 len(staticCompiler.Properties.Static.Shared_libs) == 0 &&
1004 len(sharedCompiler.Properties.Shared.Shared_libs) == 0 &&
1005 staticCompiler.Properties.Static.System_shared_libs == nil &&
1006 sharedCompiler.Properties.Shared.System_shared_libs == nil {
Colin Cross10d22312017-05-03 11:01:58 -07001007
1008 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
1009 sharedCompiler.baseCompiler.Properties.OriginalSrcs =
1010 sharedCompiler.baseCompiler.Properties.Srcs
1011 sharedCompiler.baseCompiler.Properties.Srcs = nil
1012 sharedCompiler.baseCompiler.Properties.Generated_sources = nil
1013 }
1014 }
1015}
1016
Colin Crosse40b4ea2018-10-02 22:25:58 -07001017func LinkageMutator(mctx android.BottomUpMutatorContext) {
Colin Crossb916a382016-07-29 17:28:03 -07001018 if m, ok := mctx.Module().(*Module); ok && m.linker != nil {
1019 if library, ok := m.linker.(libraryInterface); ok {
1020 var modules []blueprint.Module
1021 if library.buildStatic() && library.buildShared() {
1022 modules = mctx.CreateLocalVariations("static", "shared")
1023 static := modules[0].(*Module)
1024 shared := modules[1].(*Module)
1025
Colin Crossa48ab5b2017-02-14 15:28:44 -08001026 static.linker.(libraryInterface).setStatic()
1027 shared.linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -07001028
Colin Cross10d22312017-05-03 11:01:58 -07001029 reuseStaticLibrary(mctx, static, shared)
1030
Colin Crossb916a382016-07-29 17:28:03 -07001031 } else if library.buildStatic() {
1032 modules = mctx.CreateLocalVariations("static")
Colin Crossa48ab5b2017-02-14 15:28:44 -08001033 modules[0].(*Module).linker.(libraryInterface).setStatic()
Colin Crossb916a382016-07-29 17:28:03 -07001034 } else if library.buildShared() {
1035 modules = mctx.CreateLocalVariations("shared")
Colin Crossa48ab5b2017-02-14 15:28:44 -08001036 modules[0].(*Module).linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -07001037 }
1038 }
1039 }
Colin Cross4d9c2d12016-07-29 12:48:20 -07001040}
Jiyong Park7ed9de32018-10-15 22:25:07 +09001041
Jiyong Park25fc6a92018-11-18 18:02:45 +09001042// maps a module name to the list of stubs versions available for the module
1043func stubsVersionsFor(config android.Config) map[string][]string {
1044 return config.Once("stubVersions", func() interface{} {
1045 return make(map[string][]string)
1046 }).(map[string][]string)
1047}
1048
1049var stubsVersionsLock sync.Mutex
1050
1051func latestStubsVersionFor(config android.Config, name string) string {
1052 versions, ok := stubsVersionsFor(config)[name]
1053 if ok && len(versions) > 0 {
1054 // the versions are alreay sorted in ascending order
1055 return versions[len(versions)-1]
1056 }
1057 return ""
1058}
1059
Jiyong Park7ed9de32018-10-15 22:25:07 +09001060// Version mutator splits a module into the mandatory non-stubs variant
Jiyong Park25fc6a92018-11-18 18:02:45 +09001061// (which is unnamed) and zero or more stubs variants.
1062func VersionMutator(mctx android.BottomUpMutatorContext) {
Jiyong Park7ed9de32018-10-15 22:25:07 +09001063 if m, ok := mctx.Module().(*Module); ok && !m.inRecovery() && m.linker != nil {
Jiyong Park25fc6a92018-11-18 18:02:45 +09001064 if library, ok := m.linker.(*libraryDecorator); ok && library.buildShared() &&
1065 len(library.Properties.Stubs.Versions) > 0 {
1066 versions := []string{}
Jiyong Park7ed9de32018-10-15 22:25:07 +09001067 for _, v := range library.Properties.Stubs.Versions {
Jiyong Park25fc6a92018-11-18 18:02:45 +09001068 if _, err := strconv.Atoi(v); err != nil {
1069 mctx.PropertyErrorf("versions", "%q is not a number", v)
1070 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09001071 versions = append(versions, v)
1072 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001073 sort.Slice(versions, func(i, j int) bool {
1074 left, _ := strconv.Atoi(versions[i])
1075 right, _ := strconv.Atoi(versions[j])
1076 return left < right
1077 })
1078
1079 // save the list of versions for later use
1080 copiedVersions := make([]string, len(versions))
1081 copy(copiedVersions, versions)
1082 stubsVersionsLock.Lock()
1083 defer stubsVersionsLock.Unlock()
1084 stubsVersionsFor(mctx.Config())[mctx.ModuleName()] = copiedVersions
1085
1086 // "" is for the non-stubs variant
Jiyong Park67883b32019-01-03 21:35:58 +09001087 versions = append([]string{""}, versions...)
Jiyong Park25fc6a92018-11-18 18:02:45 +09001088
Jiyong Park7ed9de32018-10-15 22:25:07 +09001089 modules := mctx.CreateVariations(versions...)
1090 for i, m := range modules {
1091 l := m.(*Module).linker.(*libraryDecorator)
Jiyong Park25fc6a92018-11-18 18:02:45 +09001092 if versions[i] != "" {
1093 l.MutatedProperties.BuildStubs = true
1094 l.MutatedProperties.StubsVersion = versions[i]
1095 m.(*Module).Properties.HideFromMake = true
Jiyong Park090d9df2018-12-11 02:47:16 +09001096 m.(*Module).sanitize = nil
1097 m.(*Module).stl = nil
Jiyong Park127d5652018-12-12 10:26:20 +09001098 m.(*Module).Properties.PreventInstall = true
Jiyong Park7ed9de32018-10-15 22:25:07 +09001099 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09001100 }
1101 } else {
1102 mctx.CreateVariations("")
1103 }
1104 return
1105 }
1106 if genrule, ok := mctx.Module().(*genrule.Module); ok {
1107 if props, ok := genrule.Extra.(*GenruleExtraProperties); ok && !props.InRecovery {
1108 mctx.CreateVariations("")
1109 return
1110 }
1111 }
1112}