blob: 09e5b50193cbc270f3ac031d6ffe474cfe46d566 [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 Crossb916a382016-07-29 17:28:03 -070032type LibraryProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -070033 Static struct {
Colin Cross2f336352016-10-26 10:03:47 -070034 Srcs []string `android:"arch_variant"`
35 Cflags []string `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070036
Dan Willemsen3a26eef2018-12-03 15:25:46 -080037 Enabled *bool `android:"arch_variant"`
38 Whole_static_libs []string `android:"arch_variant"`
39 Static_libs []string `android:"arch_variant"`
40 Shared_libs []string `android:"arch_variant"`
41 System_shared_libs []string `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070042 } `android:"arch_variant"`
43 Shared struct {
Colin Cross2f336352016-10-26 10:03:47 -070044 Srcs []string `android:"arch_variant"`
45 Cflags []string `android:"arch_variant"`
Colin Crossb916a382016-07-29 17:28:03 -070046
Dan Willemsen3a26eef2018-12-03 15:25:46 -080047 Enabled *bool `android:"arch_variant"`
48 Whole_static_libs []string `android:"arch_variant"`
49 Static_libs []string `android:"arch_variant"`
50 Shared_libs []string `android:"arch_variant"`
51 System_shared_libs []string `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070052 } `android:"arch_variant"`
53
Colin Cross4d9c2d12016-07-29 12:48:20 -070054 // local file name to pass to the linker as -unexported_symbols_list
55 Unexported_symbols_list *string `android:"arch_variant"`
56 // local file name to pass to the linker as -force_symbols_not_weak_list
57 Force_symbols_not_weak_list *string `android:"arch_variant"`
58 // local file name to pass to the linker as -force_symbols_weak_list
59 Force_symbols_weak_list *string `android:"arch_variant"`
60
61 // rename host libraries to prevent overlap with system installed libraries
62 Unique_host_soname *bool
63
Dan Willemsene1240db2016-11-03 14:28:51 -070064 Aidl struct {
65 // export headers generated from .aidl sources
Nan Zhang0007d812017-11-07 10:57:05 -080066 Export_aidl_headers *bool
Dan Willemsene1240db2016-11-03 14:28:51 -070067 }
68
Colin Cross0c461f12016-10-20 16:11:43 -070069 Proto struct {
70 // export headers generated from .proto sources
Nan Zhang0007d812017-11-07 10:57:05 -080071 Export_proto_headers *bool
Colin Cross0c461f12016-10-20 16:11:43 -070072 }
Dan Albertf563d252017-10-13 00:29:00 -070073
Nan Zhang0007d812017-11-07 10:57:05 -080074 Static_ndk_lib *bool
Jiyong Park7ed9de32018-10-15 22:25:07 +090075
76 Stubs struct {
77 // Relative path to the symbol map. The symbol map provides the list of
78 // symbols that are exported for stubs variant of this library.
79 Symbol_file *string
80
81 // List versions to generate stubs libs for.
82 Versions []string
83 }
dimitryd95964a2018-11-07 13:43:34 +010084
85 // set the name of the output
86 Stem *string `android:"arch_variant"`
87
88 // Names of modules to be overridden. Listed modules can only be other shared libraries
89 // (in Make or Soong).
90 // This does not completely prevent installation of the overridden libraries, but if both
91 // binaries would be installed by default (in PRODUCT_PACKAGES) the other library will be removed
92 // from PRODUCT_PACKAGES.
93 Overrides []string
Colin Crossa48ab5b2017-02-14 15:28:44 -080094}
Colin Cross0c461f12016-10-20 16:11:43 -070095
Colin Crossa48ab5b2017-02-14 15:28:44 -080096type LibraryMutatedProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -070097 VariantName string `blueprint:"mutated"`
Colin Crossb916a382016-07-29 17:28:03 -070098
99 // Build a static variant
100 BuildStatic bool `blueprint:"mutated"`
101 // Build a shared variant
102 BuildShared bool `blueprint:"mutated"`
103 // This variant is shared
104 VariantIsShared bool `blueprint:"mutated"`
105 // This variant is static
106 VariantIsStatic bool `blueprint:"mutated"`
Jiyong Park7ed9de32018-10-15 22:25:07 +0900107
108 // This variant is a stubs lib
109 BuildStubs bool `blueprint:"mutated"`
110 // Version of the stubs lib
111 StubsVersion string `blueprint:"mutated"`
Colin Crossb916a382016-07-29 17:28:03 -0700112}
113
114type FlagExporterProperties struct {
115 // list of directories relative to the Blueprints file that will
Dan Willemsen273af7f2016-11-03 15:53:42 -0700116 // be added to the include path (using -I) for this module and any module that links
Colin Cross5d195602017-10-17 16:15:50 -0700117 // against this module. Directories listed in export_include_dirs do not need to be
118 // listed in local_include_dirs.
Colin Crossb916a382016-07-29 17:28:03 -0700119 Export_include_dirs []string `android:"arch_variant"`
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700120
121 Target struct {
122 Vendor struct {
123 // list of exported include directories, like
124 // export_include_dirs, that will be applied to the
125 // vendor variant of this library. This will overwrite
126 // any other declarations.
Steven Morelandb21df8f2018-01-05 14:42:54 -0800127 Override_export_include_dirs []string
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700128 }
129 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700130}
131
132func init() {
Steven Morelandf9e62162017-11-02 17:00:50 -0700133 android.RegisterModuleType("cc_library_static", LibraryStaticFactory)
134 android.RegisterModuleType("cc_library_shared", LibrarySharedFactory)
135 android.RegisterModuleType("cc_library", LibraryFactory)
136 android.RegisterModuleType("cc_library_host_static", LibraryHostStaticFactory)
137 android.RegisterModuleType("cc_library_host_shared", LibraryHostSharedFactory)
138 android.RegisterModuleType("cc_library_headers", LibraryHeaderFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700139}
140
141// Module factory for combined static + shared libraries, device by default but with possible host
142// support
Steven Morelandf9e62162017-11-02 17:00:50 -0700143func LibraryFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800144 module, _ := NewLibrary(android.HostAndDeviceSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700145 return module.Init()
146}
147
148// Module factory for static libraries
Steven Morelandf9e62162017-11-02 17:00:50 -0700149func LibraryStaticFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800150 module, library := NewLibrary(android.HostAndDeviceSupported)
151 library.BuildOnlyStatic()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700152 return module.Init()
153}
154
155// Module factory for shared libraries
Steven Morelandf9e62162017-11-02 17:00:50 -0700156func LibrarySharedFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800157 module, library := NewLibrary(android.HostAndDeviceSupported)
158 library.BuildOnlyShared()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700159 return module.Init()
160}
161
162// Module factory for host static libraries
Steven Morelandf9e62162017-11-02 17:00:50 -0700163func LibraryHostStaticFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800164 module, library := NewLibrary(android.HostSupported)
165 library.BuildOnlyStatic()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700166 return module.Init()
167}
168
169// Module factory for host shared libraries
Steven Morelandf9e62162017-11-02 17:00:50 -0700170func LibraryHostSharedFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800171 module, library := NewLibrary(android.HostSupported)
172 library.BuildOnlyShared()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700173 return module.Init()
174}
175
Colin Cross5950f382016-12-13 12:50:57 -0800176// Module factory for header-only libraries
Steven Morelandf9e62162017-11-02 17:00:50 -0700177func LibraryHeaderFactory() android.Module {
Colin Cross5950f382016-12-13 12:50:57 -0800178 module, library := NewLibrary(android.HostAndDeviceSupported)
179 library.HeaderOnly()
180 return module.Init()
181}
182
Colin Cross4d9c2d12016-07-29 12:48:20 -0700183type flagExporter struct {
184 Properties FlagExporterProperties
185
Dan Willemsen847dcc72016-09-29 12:13:36 -0700186 flags []string
187 flagsDeps android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700188}
189
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700190func (f *flagExporter) exportedIncludes(ctx ModuleContext) android.Paths {
Steven Morelandb21df8f2018-01-05 14:42:54 -0800191 if ctx.useVndk() && f.Properties.Target.Vendor.Override_export_include_dirs != nil {
192 return android.PathsForModuleSrc(ctx, f.Properties.Target.Vendor.Override_export_include_dirs)
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700193 } else {
194 return android.PathsForModuleSrc(ctx, f.Properties.Export_include_dirs)
195 }
196}
197
Colin Cross4d9c2d12016-07-29 12:48:20 -0700198func (f *flagExporter) exportIncludes(ctx ModuleContext, inc string) {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700199 includeDirs := f.exportedIncludes(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700200 for _, dir := range includeDirs.Strings() {
201 f.flags = append(f.flags, inc+dir)
202 }
203}
204
205func (f *flagExporter) reexportFlags(flags []string) {
206 f.flags = append(f.flags, flags...)
207}
208
Dan Willemsen847dcc72016-09-29 12:13:36 -0700209func (f *flagExporter) reexportDeps(deps android.Paths) {
210 f.flagsDeps = append(f.flagsDeps, deps...)
211}
212
Colin Cross4d9c2d12016-07-29 12:48:20 -0700213func (f *flagExporter) exportedFlags() []string {
214 return f.flags
215}
216
Dan Willemsen847dcc72016-09-29 12:13:36 -0700217func (f *flagExporter) exportedFlagsDeps() android.Paths {
218 return f.flagsDeps
219}
220
Colin Cross4d9c2d12016-07-29 12:48:20 -0700221type exportedFlagsProducer interface {
222 exportedFlags() []string
Dan Willemsen847dcc72016-09-29 12:13:36 -0700223 exportedFlagsDeps() android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700224}
225
226var _ exportedFlagsProducer = (*flagExporter)(nil)
227
Colin Crossb916a382016-07-29 17:28:03 -0700228// libraryDecorator wraps baseCompiler, baseLinker and baseInstaller to provide library-specific
229// functionality: static vs. shared linkage, reusing object files for shared libraries
230type libraryDecorator struct {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800231 Properties LibraryProperties
232 MutatedProperties LibraryMutatedProperties
Colin Cross4d9c2d12016-07-29 12:48:20 -0700233
234 // For reusing static library objects for shared library
Colin Cross10d22312017-05-03 11:01:58 -0700235 reuseObjects Objects
236 reuseExportedFlags []string
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700237 reuseExportedDeps android.Paths
Colin Cross10d22312017-05-03 11:01:58 -0700238
Colin Cross26c34ed2016-09-30 17:10:16 -0700239 // table-of-contents file to optimize out relinking when possible
240 tocFile android.OptionalPath
Colin Cross4d9c2d12016-07-29 12:48:20 -0700241
Colin Cross4d9c2d12016-07-29 12:48:20 -0700242 flagExporter
243 stripper
244
Colin Cross4d9c2d12016-07-29 12:48:20 -0700245 // If we're used as a whole_static_lib, our missing dependencies need
246 // to be given
247 wholeStaticMissingDeps []string
248
249 // For whole_static_libs
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700250 objects Objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700251
252 // Uses the module's name if empty, but can be overridden. Does not include
253 // shlib suffix.
254 libName string
Colin Crossb916a382016-07-29 17:28:03 -0700255
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800256 sabi *sabi
257
Dan Willemsen581341d2017-02-09 16:16:31 -0800258 // Output archive of gcno coverage information files
259 coverageOutputFile android.OptionalPath
260
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800261 // linked Source Abi Dump
262 sAbiOutputFile android.OptionalPath
263
264 // Source Abi Diff
265 sAbiDiff android.OptionalPath
266
Colin Cross0875c522017-11-28 17:34:01 -0800267 // Location of the static library in the sysroot. Empty if the library is
268 // not included in the NDK.
269 ndkSysrootPath android.Path
270
Colin Crossb60190a2018-09-04 16:28:17 -0700271 // Location of the linked, unstripped library for shared libraries
272 unstrippedOutputFile android.Path
273
Dan Willemsen569edc52018-11-19 09:33:29 -0800274 // Location of the file that should be copied to dist dir when requested
275 distFile android.OptionalPath
276
Jiyong Park7ed9de32018-10-15 22:25:07 +0900277 versionScriptPath android.ModuleGenPath
278
Colin Crossb916a382016-07-29 17:28:03 -0700279 // Decorated interafaces
280 *baseCompiler
281 *baseLinker
282 *baseInstaller
Colin Cross4d9c2d12016-07-29 12:48:20 -0700283}
284
Colin Crossb916a382016-07-29 17:28:03 -0700285func (library *libraryDecorator) linkerProps() []interface{} {
286 var props []interface{}
287 props = append(props, library.baseLinker.linkerProps()...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700288 return append(props,
289 &library.Properties,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800290 &library.MutatedProperties,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700291 &library.flagExporter.Properties,
Colin Cross22f37952018-09-05 10:43:13 -0700292 &library.stripper.StripProperties)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700293}
294
Colin Crossb916a382016-07-29 17:28:03 -0700295func (library *libraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross42742b82016-08-01 13:20:05 -0700296 flags = library.baseLinker.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700297
Colin Crossb916a382016-07-29 17:28:03 -0700298 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
299 // all code is position independent, and then those warnings get promoted to
300 // errors.
Colin Cross3edeee12017-04-04 12:59:48 -0700301 if !ctx.Windows() {
Colin Crossb916a382016-07-29 17:28:03 -0700302 flags.CFlags = append(flags.CFlags, "-fPIC")
303 }
304
305 if library.static() {
306 flags.CFlags = append(flags.CFlags, library.Properties.Static.Cflags...)
Colin Crossa48ab5b2017-02-14 15:28:44 -0800307 } else if library.shared() {
Colin Crossb916a382016-07-29 17:28:03 -0700308 flags.CFlags = append(flags.CFlags, library.Properties.Shared.Cflags...)
309 }
310
Colin Crossa48ab5b2017-02-14 15:28:44 -0800311 if library.shared() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700312 libName := library.getLibName(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700313 var f []string
Dan Willemsen01a405a2016-06-13 17:19:03 -0700314 if ctx.toolchain().Bionic() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700315 f = append(f,
316 "-nostdlib",
317 "-Wl,--gc-sections",
318 )
319 }
320
321 if ctx.Darwin() {
322 f = append(f,
323 "-dynamiclib",
324 "-single_module",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700325 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
326 )
Colin Cross7863cf52016-10-20 10:47:21 -0700327 if ctx.Arch().ArchType == android.X86 {
328 f = append(f,
329 "-read_only_relocs suppress",
330 )
331 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700332 } else {
333 f = append(f,
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700334 "-shared",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700335 "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix())
336 }
337
338 flags.LdFlags = append(f, flags.LdFlags...)
339 }
340
341 return flags
342}
343
Colin Crossf18e1102017-11-16 14:33:08 -0800344func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700345 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700346 if len(exportIncludeDirs) > 0 {
Colin Crossdad8c952017-04-26 14:55:27 -0700347 f := includeDirsToFlags(exportIncludeDirs)
348 flags.GlobalFlags = append(flags.GlobalFlags, f)
349 flags.YasmFlags = append(flags.YasmFlags, f)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700350 }
351
Jiyong Park7ed9de32018-10-15 22:25:07 +0900352 flags = library.baseCompiler.compilerFlags(ctx, flags, deps)
353 if library.buildStubs() {
Jiyong Park64379952018-12-13 18:37:29 +0900354 // Remove -include <file> when compiling stubs. Otherwise, the force included
355 // headers might cause conflicting types error with the symbols in the
356 // generated stubs source code. e.g.
357 // double acos(double); // in header
358 // void acos() {} // in the generated source code
359 removeInclude := func(flags []string) []string {
360 ret := flags[:0]
361 for _, f := range flags {
362 if strings.HasPrefix(f, "-include ") {
363 continue
364 }
365 ret = append(ret, f)
366 }
367 return ret
368 }
369 flags.GlobalFlags = removeInclude(flags.GlobalFlags)
370 flags.CFlags = removeInclude(flags.CFlags)
371
Jiyong Park7ed9de32018-10-15 22:25:07 +0900372 flags = addStubLibraryCompilerFlags(flags)
373 }
374 return flags
Dan Willemsen273af7f2016-11-03 15:53:42 -0700375}
376
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700377func extractExportIncludesFromFlags(flags []string) []string {
378 // This method is used in the generation of rules which produce
379 // abi-dumps for source files. Exported headers are needed to infer the
380 // abi exported by a library and filter out the rest of the abi dumped
381 // from a source. We extract the include flags exported by a library.
382 // This includes the flags exported which are re-exported from static
383 // library dependencies, exported header library dependencies and
Jayant Chowdharyaf6eb712017-08-23 16:08:29 -0700384 // generated header dependencies. -isystem headers are not included
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700385 // since for bionic libraries, abi-filtering is taken care of by version
386 // scripts.
387 var exportedIncludes []string
388 for _, flag := range flags {
389 if strings.HasPrefix(flag, "-I") {
390 exportedIncludes = append(exportedIncludes, flag)
391 }
392 }
393 return exportedIncludes
394}
395
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700396func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Jiyong Park7ed9de32018-10-15 22:25:07 +0900397 if library.buildStubs() {
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900398 objs, versionScript := compileStubLibrary(ctx, flags, String(library.Properties.Stubs.Symbol_file), library.MutatedProperties.StubsVersion, "--apex")
Jiyong Park7ed9de32018-10-15 22:25:07 +0900399 library.versionScriptPath = versionScript
400 return objs
401 }
402
Colin Cross5950f382016-12-13 12:50:57 -0800403 if !library.buildShared() && !library.buildStatic() {
404 if len(library.baseCompiler.Properties.Srcs) > 0 {
405 ctx.PropertyErrorf("srcs", "cc_library_headers must not have any srcs")
406 }
407 if len(library.Properties.Static.Srcs) > 0 {
408 ctx.PropertyErrorf("static.srcs", "cc_library_headers must not have any srcs")
409 }
410 if len(library.Properties.Shared.Srcs) > 0 {
411 ctx.PropertyErrorf("shared.srcs", "cc_library_headers must not have any srcs")
412 }
413 return Objects{}
414 }
Logan Chien2f2b8902018-07-10 15:01:19 +0800415 if ctx.shouldCreateVndkSourceAbiDump() || library.sabi.Properties.CreateSAbiDumps {
Jayant Chowdharya4fce192017-09-06 13:10:03 -0700416 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800417 var SourceAbiFlags []string
418 for _, dir := range exportIncludeDirs.Strings() {
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700419 SourceAbiFlags = append(SourceAbiFlags, "-I"+dir)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800420 }
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700421 for _, reexportedInclude := range extractExportIncludesFromFlags(library.sabi.Properties.ReexportedIncludeFlags) {
422 SourceAbiFlags = append(SourceAbiFlags, reexportedInclude)
423 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800424 flags.SAbiFlags = SourceAbiFlags
425 total_length := len(library.baseCompiler.Properties.Srcs) + len(deps.GeneratedSources) + len(library.Properties.Shared.Srcs) +
426 len(library.Properties.Static.Srcs)
427 if total_length > 0 {
428 flags.SAbiDump = true
429 }
430 }
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700431 objs := library.baseCompiler.compile(ctx, flags, deps)
432 library.reuseObjects = objs
Colin Cross2f336352016-10-26 10:03:47 -0700433 buildFlags := flagsToBuilderFlags(flags)
Colin Crossb916a382016-07-29 17:28:03 -0700434
Colin Cross4d9c2d12016-07-29 12:48:20 -0700435 if library.static() {
dimitry0345ad82018-12-05 16:28:14 +0100436 srcs := ctx.ExpandSources(library.Properties.Static.Srcs, nil)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700437 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceStaticLibrary,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800438 srcs, library.baseCompiler.pathDeps, library.baseCompiler.cFlagsDeps))
Colin Crossa48ab5b2017-02-14 15:28:44 -0800439 } else if library.shared() {
dimitry0345ad82018-12-05 16:28:14 +0100440 srcs := ctx.ExpandSources(library.Properties.Shared.Srcs, nil)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700441 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceSharedLibrary,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800442 srcs, library.baseCompiler.pathDeps, library.baseCompiler.cFlagsDeps))
Colin Crossb916a382016-07-29 17:28:03 -0700443 }
444
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700445 return objs
Colin Crossb916a382016-07-29 17:28:03 -0700446}
447
448type libraryInterface interface {
449 getWholeStaticMissingDeps() []string
450 static() bool
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700451 objs() Objects
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700452 reuseObjs() (Objects, []string, android.Paths)
Colin Cross26c34ed2016-09-30 17:10:16 -0700453 toc() android.OptionalPath
Colin Crossb916a382016-07-29 17:28:03 -0700454
455 // Returns true if the build options for the module have selected a static or shared build
456 buildStatic() bool
457 buildShared() bool
458
459 // Sets whether a specific variant is static or shared
Colin Crossa48ab5b2017-02-14 15:28:44 -0800460 setStatic()
461 setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700462}
463
464func (library *libraryDecorator) getLibName(ctx ModuleContext) string {
465 name := library.libName
466 if name == "" {
dimitryd95964a2018-11-07 13:43:34 +0100467 name = String(library.Properties.Stem)
468 if name == "" {
469 name = ctx.baseModuleName()
470 }
Colin Crossb916a382016-07-29 17:28:03 -0700471 }
472
Logan Chienf3511742017-10-31 18:04:35 +0800473 if ctx.isVndkExt() {
474 name = ctx.getVndkExtendsModuleName()
475 }
476
Colin Crossb916a382016-07-29 17:28:03 -0700477 if ctx.Host() && Bool(library.Properties.Unique_host_soname) {
478 if !strings.HasSuffix(name, "-host") {
479 name = name + "-host"
480 }
481 }
482
Colin Crossa48ab5b2017-02-14 15:28:44 -0800483 return name + library.MutatedProperties.VariantName
Colin Crossb916a382016-07-29 17:28:03 -0700484}
485
Jiyong Parkda732bd2018-11-02 18:23:15 +0900486var versioningMacroNamesListMutex sync.Mutex
487
Colin Crossb916a382016-07-29 17:28:03 -0700488func (library *libraryDecorator) linkerInit(ctx BaseModuleContext) {
489 location := InstallInSystem
Dan Albert61f32122018-07-26 14:00:24 -0700490 if library.baseLinker.sanitize.inSanitizerDir() {
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700491 location = InstallInSanitizerDir
Colin Crossb916a382016-07-29 17:28:03 -0700492 }
493 library.baseInstaller.location = location
Colin Crossb916a382016-07-29 17:28:03 -0700494 library.baseLinker.linkerInit(ctx)
Jiyong Park7ed9de32018-10-15 22:25:07 +0900495 // Let baseLinker know whether this variant is for stubs or not, so that
496 // it can omit things that are not required for linking stubs.
497 library.baseLinker.dynamicProperties.BuildStubs = library.buildStubs()
Jiyong Parkda732bd2018-11-02 18:23:15 +0900498
499 if library.buildStubs() {
500 macroNames := versioningMacroNamesList(ctx.Config())
501 myName := versioningMacroName(ctx.ModuleName())
502 versioningMacroNamesListMutex.Lock()
503 defer versioningMacroNamesListMutex.Unlock()
504 if (*macroNames)[myName] == "" {
505 (*macroNames)[myName] = ctx.ModuleName()
506 } else if (*macroNames)[myName] != ctx.ModuleName() {
507 ctx.ModuleErrorf("Macro name %q for versioning conflicts with macro name from module %q ", myName, (*macroNames)[myName])
508 }
509 }
Colin Crossb916a382016-07-29 17:28:03 -0700510}
511
dimitry0345ad82018-12-05 16:28:14 +0100512func (library *libraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
513 deps = library.baseCompiler.compilerDeps(ctx, deps)
514
515 if library.static() {
516 android.ExtractSourcesDeps(ctx, library.Properties.Static.Srcs)
517 } else if library.shared() {
518 android.ExtractSourcesDeps(ctx, library.Properties.Shared.Srcs)
519 }
520
521 return deps
522}
523
Colin Cross37047f12016-12-13 17:06:13 -0800524func (library *libraryDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Dan Willemsen3a26eef2018-12-03 15:25:46 -0800525 if library.static() {
526 if library.Properties.Static.System_shared_libs != nil {
527 library.baseLinker.Properties.System_shared_libs = library.Properties.Static.System_shared_libs
528 }
529 } else if library.shared() {
530 if library.Properties.Shared.System_shared_libs != nil {
531 library.baseLinker.Properties.System_shared_libs = library.Properties.Shared.System_shared_libs
532 }
533 }
534
Colin Crossb916a382016-07-29 17:28:03 -0700535 deps = library.baseLinker.linkerDeps(ctx, deps)
536
537 if library.static() {
538 deps.WholeStaticLibs = append(deps.WholeStaticLibs,
539 library.Properties.Static.Whole_static_libs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700540 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Static.Static_libs...)
541 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Static.Shared_libs...)
Colin Crossa48ab5b2017-02-14 15:28:44 -0800542 } else if library.shared() {
Dan Willemsen2e47b342016-11-17 01:02:25 -0800543 if ctx.toolchain().Bionic() && !Bool(library.baseLinker.Properties.Nocrt) {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700544 if !ctx.useSdk() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700545 deps.CrtBegin = "crtbegin_so"
546 deps.CrtEnd = "crtend_so"
547 } else {
Dan Albertebedf672016-11-08 15:06:22 -0800548 // TODO(danalbert): Add generation of crt objects.
549 // For `sdk_version: "current"`, we don't actually have a
550 // freshly generated set of CRT objects. Use the last stable
551 // version.
552 version := ctx.sdkVersion()
553 if version == "current" {
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -0700554 version = getCurrentNdkPrebuiltVersion(ctx)
Dan Albertebedf672016-11-08 15:06:22 -0800555 }
556 deps.CrtBegin = "ndk_crtbegin_so." + version
557 deps.CrtEnd = "ndk_crtend_so." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700558 }
559 }
560 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Shared.Whole_static_libs...)
561 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Shared.Static_libs...)
562 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Shared.Shared_libs...)
563 }
Jiyong Park52d25bd2017-10-13 09:17:01 +0900564 if ctx.useVndk() {
565 deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs)
566 deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Vendor.Exclude_shared_libs)
567 deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs)
568 }
Jiyong Parkf9332f12018-02-01 00:54:12 +0900569 if ctx.inRecovery() {
570 deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Recovery.Exclude_static_libs)
571 deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Recovery.Exclude_shared_libs)
572 deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Recovery.Exclude_static_libs)
573 }
Colin Cross2383f3b2018-02-06 14:40:13 -0800574
Colin Cross2383f3b2018-02-06 14:40:13 -0800575 android.ExtractSourceDeps(ctx, library.Properties.Unexported_symbols_list)
576 android.ExtractSourceDeps(ctx, library.Properties.Force_symbols_not_weak_list)
577 android.ExtractSourceDeps(ctx, library.Properties.Force_symbols_weak_list)
Colin Cross2383f3b2018-02-06 14:40:13 -0800578
Colin Cross4d9c2d12016-07-29 12:48:20 -0700579 return deps
580}
581
Colin Crossb916a382016-07-29 17:28:03 -0700582func (library *libraryDecorator) linkStatic(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700583 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700584
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700585 library.objects = deps.WholeStaticLibObjs.Copy()
586 library.objects = library.objects.Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700587
Colin Cross86803cf2018-02-15 14:12:26 -0800588 fileName := ctx.ModuleName() + library.MutatedProperties.VariantName + staticLibraryExtension
589 outputFile := android.PathForModuleOut(ctx, fileName)
Dan Willemsen581341d2017-02-09 16:16:31 -0800590 builderFlags := flagsToBuilderFlags(flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700591
Dan Willemsen569edc52018-11-19 09:33:29 -0800592 if Bool(library.baseLinker.Properties.Use_version_lib) {
593 if ctx.Host() {
594 versionedOutputFile := outputFile
595 outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
596 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
597 } else {
598 versionedOutputFile := android.PathForModuleOut(ctx, "versioned", fileName)
599 library.distFile = android.OptionalPathForPath(versionedOutputFile)
600 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
601 }
Colin Cross86803cf2018-02-15 14:12:26 -0800602 }
603
Dan Willemsen581341d2017-02-09 16:16:31 -0800604 TransformObjToStaticLib(ctx, library.objects.objFiles, builderFlags, outputFile, objs.tidyFiles)
605
606 library.coverageOutputFile = TransformCoverageFilesToLib(ctx, library.objects, builderFlags,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800607 ctx.ModuleName()+library.MutatedProperties.VariantName)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700608
609 library.wholeStaticMissingDeps = ctx.GetMissingDependencies()
610
611 ctx.CheckbuildFile(outputFile)
612
613 return outputFile
614}
615
Colin Crossb916a382016-07-29 17:28:03 -0700616func (library *libraryDecorator) linkShared(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700617 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700618
619 var linkerDeps android.Paths
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700620 linkerDeps = append(linkerDeps, flags.LdFlagsDeps...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700621
Colin Cross2383f3b2018-02-06 14:40:13 -0800622 unexportedSymbols := ctx.ExpandOptionalSource(library.Properties.Unexported_symbols_list, "unexported_symbols_list")
623 forceNotWeakSymbols := ctx.ExpandOptionalSource(library.Properties.Force_symbols_not_weak_list, "force_symbols_not_weak_list")
624 forceWeakSymbols := ctx.ExpandOptionalSource(library.Properties.Force_symbols_weak_list, "force_symbols_weak_list")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700625 if !ctx.Darwin() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700626 if unexportedSymbols.Valid() {
627 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
628 }
629 if forceNotWeakSymbols.Valid() {
630 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
631 }
632 if forceWeakSymbols.Valid() {
633 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
634 }
635 } else {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700636 if unexportedSymbols.Valid() {
637 flags.LdFlags = append(flags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
638 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
639 }
640 if forceNotWeakSymbols.Valid() {
641 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
642 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
643 }
644 if forceWeakSymbols.Valid() {
645 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
646 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
647 }
648 }
Jiyong Parkc1e7f482019-01-12 13:39:10 +0900649 if library.buildStubs() {
650 linkerScriptFlags := "-Wl,--version-script," + library.versionScriptPath.String()
651 flags.LdFlags = append(flags.LdFlags, linkerScriptFlags)
652 linkerDeps = append(linkerDeps, library.versionScriptPath)
653 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700654
655 fileName := library.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
656 outputFile := android.PathForModuleOut(ctx, fileName)
657 ret := outputFile
658
659 builderFlags := flagsToBuilderFlags(flags)
660
Colin Crossb496cfd2018-09-10 16:50:05 -0700661 // Optimize out relinking against shared libraries whose interface hasn't changed by
662 // depending on a table of contents file instead of the library itself.
663 tocPath := outputFile.RelPathString()
664 tocPath = pathtools.ReplaceExtension(tocPath, flags.Toolchain.ShlibSuffix()[1:]+".toc")
665 tocFile := android.PathForOutput(ctx, tocPath)
666 library.tocFile = android.OptionalPathForPath(tocFile)
667 TransformSharedObjectToToc(ctx, outputFile, tocFile, builderFlags)
Colin Cross89562dc2016-10-03 17:47:19 -0700668
Colin Cross4d9c2d12016-07-29 12:48:20 -0700669 if library.stripper.needsStrip(ctx) {
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -0700670 // b/80093681, GNU strip/objcopy bug.
671 // Use llvm-{strip,objcopy} when clang lld is used.
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700672 builderFlags.stripUseLlvmStrip = library.baseLinker.useClangLld(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700673 strippedOutputFile := outputFile
674 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
675 library.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
676 }
677
Colin Crossb60190a2018-09-04 16:28:17 -0700678 library.unstrippedOutputFile = outputFile
679
Dan Willemsen569edc52018-11-19 09:33:29 -0800680 if Bool(library.baseLinker.Properties.Use_version_lib) {
681 if ctx.Host() {
682 versionedOutputFile := outputFile
683 outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
684 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
685 } else {
686 versionedOutputFile := android.PathForModuleOut(ctx, "versioned", fileName)
687 library.distFile = android.OptionalPathForPath(versionedOutputFile)
688
689 if library.stripper.needsStrip(ctx) {
690 out := android.PathForModuleOut(ctx, "versioned-stripped", fileName)
691 library.distFile = android.OptionalPathForPath(out)
692 library.stripper.strip(ctx, versionedOutputFile, out, builderFlags)
693 }
694
695 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
696 }
Colin Cross86803cf2018-02-15 14:12:26 -0800697 }
698
Jiyong Park64a44f22019-01-18 14:37:08 +0900699 sharedLibs := deps.EarlySharedLibs
700 sharedLibs = append(sharedLibs, deps.SharedLibs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700701 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
702
Jiyong Park64a44f22019-01-18 14:37:08 +0900703 linkerDeps = append(linkerDeps, deps.EarlySharedLibsDeps...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700704 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
705 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700706 linkerDeps = append(linkerDeps, objs.tidyFiles...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700707
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700708 TransformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700709 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
710 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile)
711
Dan Willemsen581341d2017-02-09 16:16:31 -0800712 objs.coverageFiles = append(objs.coverageFiles, deps.StaticLibObjs.coverageFiles...)
713 objs.coverageFiles = append(objs.coverageFiles, deps.WholeStaticLibObjs.coverageFiles...)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800714
715 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.StaticLibObjs.sAbiDumpFiles...)
716 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.WholeStaticLibObjs.sAbiDumpFiles...)
717
Dan Willemsen581341d2017-02-09 16:16:31 -0800718 library.coverageOutputFile = TransformCoverageFilesToLib(ctx, objs, builderFlags, library.getLibName(ctx))
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700719 library.linkSAbiDumpFiles(ctx, objs, fileName, ret)
Dan Willemsen581341d2017-02-09 16:16:31 -0800720
Colin Cross4d9c2d12016-07-29 12:48:20 -0700721 return ret
722}
723
Logan Chien7eefdc42018-07-11 18:10:41 +0800724func getRefAbiDumpFile(ctx ModuleContext, vndkVersion, fileName string) android.Path {
Logan Chienf4b79c62018-08-02 02:27:02 +0800725 isLlndk := inList(ctx.baseModuleName(), llndkLibraries) || inList(ctx.baseModuleName(), ndkMigratedLibs)
Logan Chien7eefdc42018-07-11 18:10:41 +0800726
727 refAbiDumpTextFile := android.PathForVndkRefAbiDump(ctx, vndkVersion, fileName, isLlndk, false)
728 refAbiDumpGzipFile := android.PathForVndkRefAbiDump(ctx, vndkVersion, fileName, isLlndk, true)
729
730 if refAbiDumpTextFile.Valid() {
731 if refAbiDumpGzipFile.Valid() {
732 ctx.ModuleErrorf(
733 "Two reference ABI dump files are found: %q and %q. Please delete the stale one.",
734 refAbiDumpTextFile, refAbiDumpGzipFile)
735 return nil
736 }
737 return refAbiDumpTextFile.Path()
738 }
739 if refAbiDumpGzipFile.Valid() {
740 return UnzipRefDump(ctx, refAbiDumpGzipFile.Path(), fileName)
741 }
742 return nil
743}
744
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700745func (library *libraryDecorator) linkSAbiDumpFiles(ctx ModuleContext, objs Objects, fileName string, soFile android.Path) {
Logan Chien2f2b8902018-07-10 15:01:19 +0800746 if len(objs.sAbiDumpFiles) > 0 && ctx.shouldCreateVndkSourceAbiDump() {
Logan Chiena8f51582018-03-21 11:53:48 +0800747 vndkVersion := ctx.DeviceConfig().PlatformVndkVersion()
748 if ver := ctx.DeviceConfig().VndkVersion(); ver != "" && ver != "current" {
Logan Chienf3511742017-10-31 18:04:35 +0800749 vndkVersion = ver
750 }
751
Jayant Chowdharya4fce192017-09-06 13:10:03 -0700752 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800753 var SourceAbiFlags []string
754 for _, dir := range exportIncludeDirs.Strings() {
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700755 SourceAbiFlags = append(SourceAbiFlags, "-I"+dir)
756 }
757 for _, reexportedInclude := range extractExportIncludesFromFlags(library.sabi.Properties.ReexportedIncludeFlags) {
758 SourceAbiFlags = append(SourceAbiFlags, reexportedInclude)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800759 }
760 exportedHeaderFlags := strings.Join(SourceAbiFlags, " ")
Jayant Chowdharydf344d52018-01-17 11:11:42 -0800761 library.sAbiOutputFile = TransformDumpToLinkedDump(ctx, objs.sAbiDumpFiles, soFile, fileName, exportedHeaderFlags)
Logan Chien2f2b8902018-07-10 15:01:19 +0800762
Logan Chien7eefdc42018-07-11 18:10:41 +0800763 refAbiDumpFile := getRefAbiDumpFile(ctx, vndkVersion, fileName)
764 if refAbiDumpFile != nil {
Logan Chienf3511742017-10-31 18:04:35 +0800765 library.sAbiDiff = SourceAbiDiff(ctx, library.sAbiOutputFile.Path(),
Logan Chien7eefdc42018-07-11 18:10:41 +0800766 refAbiDumpFile, fileName, exportedHeaderFlags, ctx.isVndkExt())
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800767 }
768 }
769}
770
Colin Crossb916a382016-07-29 17:28:03 -0700771func (library *libraryDecorator) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700772 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700773
Colin Crossad59e752017-11-16 14:29:11 -0800774 objs = deps.Objs.Copy().Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700775 var out android.Path
Colin Crossa48ab5b2017-02-14 15:28:44 -0800776 if library.static() || library.header() {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700777 out = library.linkStatic(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700778 } else {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700779 out = library.linkShared(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700780 }
781
782 library.exportIncludes(ctx, "-I")
783 library.reexportFlags(deps.ReexportedFlags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700784 library.reexportDeps(deps.ReexportedFlagsDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700785
Nan Zhang0007d812017-11-07 10:57:05 -0800786 if Bool(library.Properties.Aidl.Export_aidl_headers) {
Dan Willemsene1240db2016-11-03 14:28:51 -0700787 if library.baseCompiler.hasSrcExt(".aidl") {
Colin Cross10d22312017-05-03 11:01:58 -0700788 flags := []string{
Dan Willemsene1240db2016-11-03 14:28:51 -0700789 "-I" + android.PathForModuleGen(ctx, "aidl").String(),
Colin Cross10d22312017-05-03 11:01:58 -0700790 }
791 library.reexportFlags(flags)
792 library.reuseExportedFlags = append(library.reuseExportedFlags, flags...)
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800793 library.reexportDeps(library.baseCompiler.pathDeps) // TODO: restrict to aidl deps
794 library.reuseExportedDeps = append(library.reuseExportedDeps, library.baseCompiler.pathDeps...)
Dan Willemsene1240db2016-11-03 14:28:51 -0700795 }
796 }
797
Nan Zhang0007d812017-11-07 10:57:05 -0800798 if Bool(library.Properties.Proto.Export_proto_headers) {
Dan Willemsene1240db2016-11-03 14:28:51 -0700799 if library.baseCompiler.hasSrcExt(".proto") {
Dan Willemsenab9f4262018-02-14 13:58:34 -0800800 includes := []string{}
801 if flags.ProtoRoot {
802 includes = append(includes, "-I"+android.ProtoSubDir(ctx).String())
Colin Cross10d22312017-05-03 11:01:58 -0700803 }
Dan Willemsenab9f4262018-02-14 13:58:34 -0800804 includes = append(includes, "-I"+android.ProtoDir(ctx).String())
805 library.reexportFlags(includes)
806 library.reuseExportedFlags = append(library.reuseExportedFlags, includes...)
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800807 library.reexportDeps(library.baseCompiler.pathDeps) // TODO: restrict to proto deps
808 library.reuseExportedDeps = append(library.reuseExportedDeps, library.baseCompiler.pathDeps...)
Colin Cross0c461f12016-10-20 16:11:43 -0700809 }
810 }
811
Inseob Kim21f26902018-09-06 00:55:20 +0900812 if library.baseCompiler.hasSrcExt(".sysprop") {
813 flags := []string{
814 "-I" + android.PathForModuleGen(ctx, "sysprop", "include").String(),
815 }
816 library.reexportFlags(flags)
Sundong Ahn5b73f312018-12-19 14:39:29 +0900817 library.reexportDeps(library.baseCompiler.pathDeps)
Inseob Kim21f26902018-09-06 00:55:20 +0900818 library.reuseExportedFlags = append(library.reuseExportedFlags, flags...)
819 }
820
Jiyong Parkda732bd2018-11-02 18:23:15 +0900821 if library.buildStubs() {
822 library.reexportFlags([]string{"-D" + versioningMacroName(ctx.ModuleName()) + "=" + library.stubsVersion()})
823 }
824
Colin Cross4d9c2d12016-07-29 12:48:20 -0700825 return out
826}
827
Colin Crossb916a382016-07-29 17:28:03 -0700828func (library *libraryDecorator) buildStatic() bool {
Colin Cross38b40df2018-04-10 16:14:46 -0700829 return library.MutatedProperties.BuildStatic && BoolDefault(library.Properties.Static.Enabled, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700830}
831
Colin Crossb916a382016-07-29 17:28:03 -0700832func (library *libraryDecorator) buildShared() bool {
Colin Cross38b40df2018-04-10 16:14:46 -0700833 return library.MutatedProperties.BuildShared && BoolDefault(library.Properties.Shared.Enabled, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700834}
835
Colin Crossb916a382016-07-29 17:28:03 -0700836func (library *libraryDecorator) getWholeStaticMissingDeps() []string {
Colin Crossd2343a32018-04-30 14:50:01 -0700837 return append([]string(nil), library.wholeStaticMissingDeps...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700838}
839
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700840func (library *libraryDecorator) objs() Objects {
841 return library.objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700842}
843
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700844func (library *libraryDecorator) reuseObjs() (Objects, []string, android.Paths) {
845 return library.reuseObjects, library.reuseExportedFlags, library.reuseExportedDeps
Colin Cross4d9c2d12016-07-29 12:48:20 -0700846}
847
Colin Cross26c34ed2016-09-30 17:10:16 -0700848func (library *libraryDecorator) toc() android.OptionalPath {
849 return library.tocFile
850}
851
Colin Crossb916a382016-07-29 17:28:03 -0700852func (library *libraryDecorator) install(ctx ModuleContext, file android.Path) {
Colin Crossc43ae772017-04-14 15:42:53 -0700853 if library.shared() {
Justin Yun8fe12122017-12-07 17:18:15 +0900854 if ctx.Device() && ctx.useVndk() {
855 if ctx.isVndkSp() {
856 library.baseInstaller.subDir = "vndk-sp"
857 } else if ctx.isVndk() {
858 library.baseInstaller.subDir = "vndk"
859 }
Logan Chienf3511742017-10-31 18:04:35 +0800860
861 // Append a version to vndk or vndk-sp directories on the system partition.
862 if ctx.isVndk() && !ctx.isVndkExt() {
863 vndkVersion := ctx.DeviceConfig().PlatformVndkVersion()
864 if vndkVersion != "current" && vndkVersion != "" {
865 library.baseInstaller.subDir += "-" + vndkVersion
866 }
Justin Yun8effde42017-06-23 19:24:43 +0900867 }
868 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700869 library.baseInstaller.install(ctx, file)
870 }
Dan Albertf563d252017-10-13 00:29:00 -0700871
Dan Albert281f22b2017-12-13 15:03:47 -0800872 if Bool(library.Properties.Static_ndk_lib) && library.static() &&
Jiyong Parkf9332f12018-02-01 00:54:12 +0900873 !ctx.useVndk() && !ctx.inRecovery() && ctx.Device() &&
Jiyong Park7ed9de32018-10-15 22:25:07 +0900874 library.baseLinker.sanitize.isUnsanitizedVariant() &&
875 !library.buildStubs() {
Dan Albertf563d252017-10-13 00:29:00 -0700876 installPath := getNdkSysrootBase(ctx).Join(
Dan Albertea4b7b92018-04-25 16:05:30 -0700877 ctx, "usr/lib", config.NDKTriple(ctx.toolchain()), file.Base())
Dan Albertf563d252017-10-13 00:29:00 -0700878
879 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
880 Rule: android.Cp,
881 Description: "install " + installPath.Base(),
882 Output: installPath,
883 Input: file,
884 })
885
Colin Cross0875c522017-11-28 17:34:01 -0800886 library.ndkSysrootPath = installPath
Dan Albertf563d252017-10-13 00:29:00 -0700887 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700888}
889
Colin Crossb916a382016-07-29 17:28:03 -0700890func (library *libraryDecorator) static() bool {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800891 return library.MutatedProperties.VariantIsStatic
Colin Cross4d9c2d12016-07-29 12:48:20 -0700892}
893
Colin Crossa48ab5b2017-02-14 15:28:44 -0800894func (library *libraryDecorator) shared() bool {
895 return library.MutatedProperties.VariantIsShared
896}
897
898func (library *libraryDecorator) header() bool {
899 return !library.static() && !library.shared()
900}
901
902func (library *libraryDecorator) setStatic() {
903 library.MutatedProperties.VariantIsStatic = true
904 library.MutatedProperties.VariantIsShared = false
905}
906
907func (library *libraryDecorator) setShared() {
908 library.MutatedProperties.VariantIsStatic = false
909 library.MutatedProperties.VariantIsShared = true
Colin Crossb916a382016-07-29 17:28:03 -0700910}
911
Colin Crossab3b7322016-12-09 14:46:15 -0800912func (library *libraryDecorator) BuildOnlyStatic() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800913 library.MutatedProperties.BuildShared = false
Colin Crossab3b7322016-12-09 14:46:15 -0800914}
915
916func (library *libraryDecorator) BuildOnlyShared() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800917 library.MutatedProperties.BuildStatic = false
Colin Crossab3b7322016-12-09 14:46:15 -0800918}
919
Colin Cross5950f382016-12-13 12:50:57 -0800920func (library *libraryDecorator) HeaderOnly() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800921 library.MutatedProperties.BuildShared = false
922 library.MutatedProperties.BuildStatic = false
Colin Cross5950f382016-12-13 12:50:57 -0800923}
924
Jiyong Park7ed9de32018-10-15 22:25:07 +0900925func (library *libraryDecorator) buildStubs() bool {
926 return library.MutatedProperties.BuildStubs
927}
928
929func (library *libraryDecorator) stubsVersion() string {
930 return library.MutatedProperties.StubsVersion
931}
932
Jiyong Parkda732bd2018-11-02 18:23:15 +0900933func versioningMacroNamesList(config android.Config) *map[string]string {
934 return config.Once("versioningMacroNamesList", func() interface{} {
935 m := make(map[string]string)
936 return &m
937 }).(*map[string]string)
938}
939
940// alphanumeric and _ characters are preserved.
941// other characters are all converted to _
942var charsNotForMacro = regexp.MustCompile("[^a-zA-Z0-9_]+")
943
944func versioningMacroName(moduleName string) string {
945 macroName := charsNotForMacro.ReplaceAllString(moduleName, "_")
946 macroName = strings.ToUpper(moduleName)
947 return "__" + macroName + "_API__"
948}
949
Colin Crossab3b7322016-12-09 14:46:15 -0800950func NewLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700951 module := newModule(hod, android.MultilibBoth)
952
Colin Crossb916a382016-07-29 17:28:03 -0700953 library := &libraryDecorator{
Colin Crossa48ab5b2017-02-14 15:28:44 -0800954 MutatedProperties: LibraryMutatedProperties{
Colin Crossab3b7322016-12-09 14:46:15 -0800955 BuildShared: true,
956 BuildStatic: true,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700957 },
Colin Crossb916a382016-07-29 17:28:03 -0700958 baseCompiler: NewBaseCompiler(),
Dan Albert61f32122018-07-26 14:00:24 -0700959 baseLinker: NewBaseLinker(module.sanitize),
Colin Crossb916a382016-07-29 17:28:03 -0700960 baseInstaller: NewBaseInstaller("lib", "lib64", InstallInSystem),
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800961 sabi: module.sabi,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700962 }
963
Colin Crossb916a382016-07-29 17:28:03 -0700964 module.compiler = library
965 module.linker = library
966 module.installer = library
967
968 return module, library
969}
970
Colin Cross10d22312017-05-03 11:01:58 -0700971// connects a shared library to a static library in order to reuse its .o files to avoid
972// compiling source files twice.
973func reuseStaticLibrary(mctx android.BottomUpMutatorContext, static, shared *Module) {
974 if staticCompiler, ok := static.compiler.(*libraryDecorator); ok {
975 sharedCompiler := shared.compiler.(*libraryDecorator)
Dan Willemsen3a26eef2018-12-03 15:25:46 -0800976
977 // Check libraries in addition to cflags, since libraries may be exporting different
978 // include directories.
Colin Cross10d22312017-05-03 11:01:58 -0700979 if len(staticCompiler.Properties.Static.Cflags) == 0 &&
Dan Willemsen3a26eef2018-12-03 15:25:46 -0800980 len(sharedCompiler.Properties.Shared.Cflags) == 0 &&
981 len(staticCompiler.Properties.Static.Whole_static_libs) == 0 &&
982 len(sharedCompiler.Properties.Shared.Whole_static_libs) == 0 &&
983 len(staticCompiler.Properties.Static.Static_libs) == 0 &&
984 len(sharedCompiler.Properties.Shared.Static_libs) == 0 &&
985 len(staticCompiler.Properties.Static.Shared_libs) == 0 &&
986 len(sharedCompiler.Properties.Shared.Shared_libs) == 0 &&
987 staticCompiler.Properties.Static.System_shared_libs == nil &&
988 sharedCompiler.Properties.Shared.System_shared_libs == nil {
Colin Cross10d22312017-05-03 11:01:58 -0700989
990 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
991 sharedCompiler.baseCompiler.Properties.OriginalSrcs =
992 sharedCompiler.baseCompiler.Properties.Srcs
993 sharedCompiler.baseCompiler.Properties.Srcs = nil
994 sharedCompiler.baseCompiler.Properties.Generated_sources = nil
995 }
996 }
997}
998
Colin Crosse40b4ea2018-10-02 22:25:58 -0700999func LinkageMutator(mctx android.BottomUpMutatorContext) {
Colin Crossb916a382016-07-29 17:28:03 -07001000 if m, ok := mctx.Module().(*Module); ok && m.linker != nil {
1001 if library, ok := m.linker.(libraryInterface); ok {
1002 var modules []blueprint.Module
1003 if library.buildStatic() && library.buildShared() {
1004 modules = mctx.CreateLocalVariations("static", "shared")
1005 static := modules[0].(*Module)
1006 shared := modules[1].(*Module)
1007
Colin Crossa48ab5b2017-02-14 15:28:44 -08001008 static.linker.(libraryInterface).setStatic()
1009 shared.linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -07001010
Colin Cross10d22312017-05-03 11:01:58 -07001011 reuseStaticLibrary(mctx, static, shared)
1012
Colin Crossb916a382016-07-29 17:28:03 -07001013 } else if library.buildStatic() {
1014 modules = mctx.CreateLocalVariations("static")
Colin Crossa48ab5b2017-02-14 15:28:44 -08001015 modules[0].(*Module).linker.(libraryInterface).setStatic()
Colin Crossb916a382016-07-29 17:28:03 -07001016 } else if library.buildShared() {
1017 modules = mctx.CreateLocalVariations("shared")
Colin Crossa48ab5b2017-02-14 15:28:44 -08001018 modules[0].(*Module).linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -07001019 }
1020 }
1021 }
Colin Cross4d9c2d12016-07-29 12:48:20 -07001022}
Jiyong Park7ed9de32018-10-15 22:25:07 +09001023
Jiyong Park25fc6a92018-11-18 18:02:45 +09001024// maps a module name to the list of stubs versions available for the module
1025func stubsVersionsFor(config android.Config) map[string][]string {
1026 return config.Once("stubVersions", func() interface{} {
1027 return make(map[string][]string)
1028 }).(map[string][]string)
1029}
1030
1031var stubsVersionsLock sync.Mutex
1032
1033func latestStubsVersionFor(config android.Config, name string) string {
1034 versions, ok := stubsVersionsFor(config)[name]
1035 if ok && len(versions) > 0 {
1036 // the versions are alreay sorted in ascending order
1037 return versions[len(versions)-1]
1038 }
1039 return ""
1040}
1041
Jiyong Park7ed9de32018-10-15 22:25:07 +09001042// Version mutator splits a module into the mandatory non-stubs variant
Jiyong Park25fc6a92018-11-18 18:02:45 +09001043// (which is unnamed) and zero or more stubs variants.
1044func VersionMutator(mctx android.BottomUpMutatorContext) {
Jiyong Park7ed9de32018-10-15 22:25:07 +09001045 if m, ok := mctx.Module().(*Module); ok && !m.inRecovery() && m.linker != nil {
Jiyong Park25fc6a92018-11-18 18:02:45 +09001046 if library, ok := m.linker.(*libraryDecorator); ok && library.buildShared() &&
1047 len(library.Properties.Stubs.Versions) > 0 {
1048 versions := []string{}
Jiyong Park7ed9de32018-10-15 22:25:07 +09001049 for _, v := range library.Properties.Stubs.Versions {
Jiyong Park25fc6a92018-11-18 18:02:45 +09001050 if _, err := strconv.Atoi(v); err != nil {
1051 mctx.PropertyErrorf("versions", "%q is not a number", v)
1052 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09001053 versions = append(versions, v)
1054 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001055 sort.Slice(versions, func(i, j int) bool {
1056 left, _ := strconv.Atoi(versions[i])
1057 right, _ := strconv.Atoi(versions[j])
1058 return left < right
1059 })
1060
1061 // save the list of versions for later use
1062 copiedVersions := make([]string, len(versions))
1063 copy(copiedVersions, versions)
1064 stubsVersionsLock.Lock()
1065 defer stubsVersionsLock.Unlock()
1066 stubsVersionsFor(mctx.Config())[mctx.ModuleName()] = copiedVersions
1067
1068 // "" is for the non-stubs variant
Jiyong Park67883b32019-01-03 21:35:58 +09001069 versions = append([]string{""}, versions...)
Jiyong Park25fc6a92018-11-18 18:02:45 +09001070
Jiyong Park7ed9de32018-10-15 22:25:07 +09001071 modules := mctx.CreateVariations(versions...)
1072 for i, m := range modules {
1073 l := m.(*Module).linker.(*libraryDecorator)
Jiyong Park25fc6a92018-11-18 18:02:45 +09001074 if versions[i] != "" {
1075 l.MutatedProperties.BuildStubs = true
1076 l.MutatedProperties.StubsVersion = versions[i]
1077 m.(*Module).Properties.HideFromMake = true
Jiyong Park090d9df2018-12-11 02:47:16 +09001078 m.(*Module).sanitize = nil
1079 m.(*Module).stl = nil
Jiyong Park127d5652018-12-12 10:26:20 +09001080 m.(*Module).Properties.PreventInstall = true
Jiyong Park7ed9de32018-10-15 22:25:07 +09001081 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09001082 }
1083 } else {
1084 mctx.CreateVariations("")
1085 }
1086 return
1087 }
1088 if genrule, ok := mctx.Module().(*genrule.Module); ok {
1089 if props, ok := genrule.Extra.(*GenruleExtraProperties); ok && !props.InRecovery {
1090 mctx.CreateVariations("")
1091 return
1092 }
1093 }
1094}