blob: 8b8fe023753994b019de7246987a501f7ddd24a1 [file] [log] [blame]
Colin Cross4d9c2d12016-07-29 12:48:20 -07001// Copyright 2016 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package cc
16
17import (
Jiyong Parkda732bd2018-11-02 18:23:15 +090018 "regexp"
Colin Cross4d9c2d12016-07-29 12:48:20 -070019 "strings"
Jiyong Parkda732bd2018-11-02 18:23:15 +090020 "sync"
Colin Cross4d9c2d12016-07-29 12:48:20 -070021
22 "github.com/google/blueprint"
Colin Cross26c34ed2016-09-30 17:10:16 -070023 "github.com/google/blueprint/pathtools"
Colin Cross4d9c2d12016-07-29 12:48:20 -070024
Colin Cross4d9c2d12016-07-29 12:48:20 -070025 "android/soong/android"
Dan Albertea4b7b92018-04-25 16:05:30 -070026 "android/soong/cc/config"
Jiyong Park7ed9de32018-10-15 22:25:07 +090027 "android/soong/genrule"
Colin Cross4d9c2d12016-07-29 12:48:20 -070028)
29
Colin Crossb916a382016-07-29 17:28:03 -070030type LibraryProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -070031 Static struct {
Colin Cross2f336352016-10-26 10:03:47 -070032 Srcs []string `android:"arch_variant"`
33 Cflags []string `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070034
Dan Willemsen3a26eef2018-12-03 15:25:46 -080035 Enabled *bool `android:"arch_variant"`
36 Whole_static_libs []string `android:"arch_variant"`
37 Static_libs []string `android:"arch_variant"`
38 Shared_libs []string `android:"arch_variant"`
39 System_shared_libs []string `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070040 } `android:"arch_variant"`
41 Shared struct {
Colin Cross2f336352016-10-26 10:03:47 -070042 Srcs []string `android:"arch_variant"`
43 Cflags []string `android:"arch_variant"`
Colin Crossb916a382016-07-29 17:28:03 -070044
Dan Willemsen3a26eef2018-12-03 15:25:46 -080045 Enabled *bool `android:"arch_variant"`
46 Whole_static_libs []string `android:"arch_variant"`
47 Static_libs []string `android:"arch_variant"`
48 Shared_libs []string `android:"arch_variant"`
49 System_shared_libs []string `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070050 } `android:"arch_variant"`
51
Colin Cross4d9c2d12016-07-29 12:48:20 -070052 // local file name to pass to the linker as -unexported_symbols_list
53 Unexported_symbols_list *string `android:"arch_variant"`
54 // local file name to pass to the linker as -force_symbols_not_weak_list
55 Force_symbols_not_weak_list *string `android:"arch_variant"`
56 // local file name to pass to the linker as -force_symbols_weak_list
57 Force_symbols_weak_list *string `android:"arch_variant"`
58
59 // rename host libraries to prevent overlap with system installed libraries
60 Unique_host_soname *bool
61
Dan Willemsene1240db2016-11-03 14:28:51 -070062 Aidl struct {
63 // export headers generated from .aidl sources
Nan Zhang0007d812017-11-07 10:57:05 -080064 Export_aidl_headers *bool
Dan Willemsene1240db2016-11-03 14:28:51 -070065 }
66
Colin Cross0c461f12016-10-20 16:11:43 -070067 Proto struct {
68 // export headers generated from .proto sources
Nan Zhang0007d812017-11-07 10:57:05 -080069 Export_proto_headers *bool
Colin Cross0c461f12016-10-20 16:11:43 -070070 }
Dan Albertf563d252017-10-13 00:29:00 -070071
Nan Zhang0007d812017-11-07 10:57:05 -080072 Static_ndk_lib *bool
Jiyong Park7ed9de32018-10-15 22:25:07 +090073
74 Stubs struct {
75 // Relative path to the symbol map. The symbol map provides the list of
76 // symbols that are exported for stubs variant of this library.
77 Symbol_file *string
78
79 // List versions to generate stubs libs for.
80 Versions []string
81 }
dimitryd95964a2018-11-07 13:43:34 +010082
83 // set the name of the output
84 Stem *string `android:"arch_variant"`
85
86 // Names of modules to be overridden. Listed modules can only be other shared libraries
87 // (in Make or Soong).
88 // This does not completely prevent installation of the overridden libraries, but if both
89 // binaries would be installed by default (in PRODUCT_PACKAGES) the other library will be removed
90 // from PRODUCT_PACKAGES.
91 Overrides []string
Colin Crossa48ab5b2017-02-14 15:28:44 -080092}
Colin Cross0c461f12016-10-20 16:11:43 -070093
Colin Crossa48ab5b2017-02-14 15:28:44 -080094type LibraryMutatedProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -070095 VariantName string `blueprint:"mutated"`
Colin Crossb916a382016-07-29 17:28:03 -070096
97 // Build a static variant
98 BuildStatic bool `blueprint:"mutated"`
99 // Build a shared variant
100 BuildShared bool `blueprint:"mutated"`
101 // This variant is shared
102 VariantIsShared bool `blueprint:"mutated"`
103 // This variant is static
104 VariantIsStatic bool `blueprint:"mutated"`
Jiyong Park7ed9de32018-10-15 22:25:07 +0900105
106 // This variant is a stubs lib
107 BuildStubs bool `blueprint:"mutated"`
108 // Version of the stubs lib
109 StubsVersion string `blueprint:"mutated"`
Colin Crossb916a382016-07-29 17:28:03 -0700110}
111
112type FlagExporterProperties struct {
113 // list of directories relative to the Blueprints file that will
Dan Willemsen273af7f2016-11-03 15:53:42 -0700114 // be added to the include path (using -I) for this module and any module that links
Colin Cross5d195602017-10-17 16:15:50 -0700115 // against this module. Directories listed in export_include_dirs do not need to be
116 // listed in local_include_dirs.
Colin Crossb916a382016-07-29 17:28:03 -0700117 Export_include_dirs []string `android:"arch_variant"`
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700118
119 Target struct {
120 Vendor struct {
121 // list of exported include directories, like
122 // export_include_dirs, that will be applied to the
123 // vendor variant of this library. This will overwrite
124 // any other declarations.
Steven Morelandb21df8f2018-01-05 14:42:54 -0800125 Override_export_include_dirs []string
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700126 }
127 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700128}
129
130func init() {
Steven Morelandf9e62162017-11-02 17:00:50 -0700131 android.RegisterModuleType("cc_library_static", LibraryStaticFactory)
132 android.RegisterModuleType("cc_library_shared", LibrarySharedFactory)
133 android.RegisterModuleType("cc_library", LibraryFactory)
134 android.RegisterModuleType("cc_library_host_static", LibraryHostStaticFactory)
135 android.RegisterModuleType("cc_library_host_shared", LibraryHostSharedFactory)
136 android.RegisterModuleType("cc_library_headers", LibraryHeaderFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700137}
138
139// Module factory for combined static + shared libraries, device by default but with possible host
140// support
Steven Morelandf9e62162017-11-02 17:00:50 -0700141func LibraryFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800142 module, _ := NewLibrary(android.HostAndDeviceSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700143 return module.Init()
144}
145
146// Module factory for static libraries
Steven Morelandf9e62162017-11-02 17:00:50 -0700147func LibraryStaticFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800148 module, library := NewLibrary(android.HostAndDeviceSupported)
149 library.BuildOnlyStatic()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700150 return module.Init()
151}
152
153// Module factory for shared libraries
Steven Morelandf9e62162017-11-02 17:00:50 -0700154func LibrarySharedFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800155 module, library := NewLibrary(android.HostAndDeviceSupported)
156 library.BuildOnlyShared()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700157 return module.Init()
158}
159
160// Module factory for host static libraries
Steven Morelandf9e62162017-11-02 17:00:50 -0700161func LibraryHostStaticFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800162 module, library := NewLibrary(android.HostSupported)
163 library.BuildOnlyStatic()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700164 return module.Init()
165}
166
167// Module factory for host shared libraries
Steven Morelandf9e62162017-11-02 17:00:50 -0700168func LibraryHostSharedFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800169 module, library := NewLibrary(android.HostSupported)
170 library.BuildOnlyShared()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700171 return module.Init()
172}
173
Colin Cross5950f382016-12-13 12:50:57 -0800174// Module factory for header-only libraries
Steven Morelandf9e62162017-11-02 17:00:50 -0700175func LibraryHeaderFactory() android.Module {
Colin Cross5950f382016-12-13 12:50:57 -0800176 module, library := NewLibrary(android.HostAndDeviceSupported)
177 library.HeaderOnly()
178 return module.Init()
179}
180
Colin Cross4d9c2d12016-07-29 12:48:20 -0700181type flagExporter struct {
182 Properties FlagExporterProperties
183
Dan Willemsen847dcc72016-09-29 12:13:36 -0700184 flags []string
185 flagsDeps android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700186}
187
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700188func (f *flagExporter) exportedIncludes(ctx ModuleContext) android.Paths {
Steven Morelandb21df8f2018-01-05 14:42:54 -0800189 if ctx.useVndk() && f.Properties.Target.Vendor.Override_export_include_dirs != nil {
190 return android.PathsForModuleSrc(ctx, f.Properties.Target.Vendor.Override_export_include_dirs)
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700191 } else {
192 return android.PathsForModuleSrc(ctx, f.Properties.Export_include_dirs)
193 }
194}
195
Colin Cross4d9c2d12016-07-29 12:48:20 -0700196func (f *flagExporter) exportIncludes(ctx ModuleContext, inc string) {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700197 includeDirs := f.exportedIncludes(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700198 for _, dir := range includeDirs.Strings() {
199 f.flags = append(f.flags, inc+dir)
200 }
201}
202
203func (f *flagExporter) reexportFlags(flags []string) {
204 f.flags = append(f.flags, flags...)
205}
206
Dan Willemsen847dcc72016-09-29 12:13:36 -0700207func (f *flagExporter) reexportDeps(deps android.Paths) {
208 f.flagsDeps = append(f.flagsDeps, deps...)
209}
210
Colin Cross4d9c2d12016-07-29 12:48:20 -0700211func (f *flagExporter) exportedFlags() []string {
212 return f.flags
213}
214
Dan Willemsen847dcc72016-09-29 12:13:36 -0700215func (f *flagExporter) exportedFlagsDeps() android.Paths {
216 return f.flagsDeps
217}
218
Colin Cross4d9c2d12016-07-29 12:48:20 -0700219type exportedFlagsProducer interface {
220 exportedFlags() []string
Dan Willemsen847dcc72016-09-29 12:13:36 -0700221 exportedFlagsDeps() android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700222}
223
224var _ exportedFlagsProducer = (*flagExporter)(nil)
225
Colin Crossb916a382016-07-29 17:28:03 -0700226// libraryDecorator wraps baseCompiler, baseLinker and baseInstaller to provide library-specific
227// functionality: static vs. shared linkage, reusing object files for shared libraries
228type libraryDecorator struct {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800229 Properties LibraryProperties
230 MutatedProperties LibraryMutatedProperties
Colin Cross4d9c2d12016-07-29 12:48:20 -0700231
232 // For reusing static library objects for shared library
Colin Cross10d22312017-05-03 11:01:58 -0700233 reuseObjects Objects
234 reuseExportedFlags []string
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700235 reuseExportedDeps android.Paths
Colin Cross10d22312017-05-03 11:01:58 -0700236
Colin Cross26c34ed2016-09-30 17:10:16 -0700237 // table-of-contents file to optimize out relinking when possible
238 tocFile android.OptionalPath
Colin Cross4d9c2d12016-07-29 12:48:20 -0700239
Colin Cross4d9c2d12016-07-29 12:48:20 -0700240 flagExporter
241 stripper
242
Colin Cross4d9c2d12016-07-29 12:48:20 -0700243 // If we're used as a whole_static_lib, our missing dependencies need
244 // to be given
245 wholeStaticMissingDeps []string
246
247 // For whole_static_libs
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700248 objects Objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700249
250 // Uses the module's name if empty, but can be overridden. Does not include
251 // shlib suffix.
252 libName string
Colin Crossb916a382016-07-29 17:28:03 -0700253
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800254 sabi *sabi
255
Dan Willemsen581341d2017-02-09 16:16:31 -0800256 // Output archive of gcno coverage information files
257 coverageOutputFile android.OptionalPath
258
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800259 // linked Source Abi Dump
260 sAbiOutputFile android.OptionalPath
261
262 // Source Abi Diff
263 sAbiDiff android.OptionalPath
264
Colin Cross0875c522017-11-28 17:34:01 -0800265 // Location of the static library in the sysroot. Empty if the library is
266 // not included in the NDK.
267 ndkSysrootPath android.Path
268
Colin Crossb60190a2018-09-04 16:28:17 -0700269 // Location of the linked, unstripped library for shared libraries
270 unstrippedOutputFile android.Path
271
Dan Willemsen569edc52018-11-19 09:33:29 -0800272 // Location of the file that should be copied to dist dir when requested
273 distFile android.OptionalPath
274
Jiyong Park7ed9de32018-10-15 22:25:07 +0900275 versionScriptPath android.ModuleGenPath
276
Colin Crossb916a382016-07-29 17:28:03 -0700277 // Decorated interafaces
278 *baseCompiler
279 *baseLinker
280 *baseInstaller
Colin Cross4d9c2d12016-07-29 12:48:20 -0700281}
282
Colin Crossb916a382016-07-29 17:28:03 -0700283func (library *libraryDecorator) linkerProps() []interface{} {
284 var props []interface{}
285 props = append(props, library.baseLinker.linkerProps()...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700286 return append(props,
287 &library.Properties,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800288 &library.MutatedProperties,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700289 &library.flagExporter.Properties,
Colin Cross22f37952018-09-05 10:43:13 -0700290 &library.stripper.StripProperties)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700291}
292
Colin Crossb916a382016-07-29 17:28:03 -0700293func (library *libraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross42742b82016-08-01 13:20:05 -0700294 flags = library.baseLinker.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700295
Colin Crossb916a382016-07-29 17:28:03 -0700296 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
297 // all code is position independent, and then those warnings get promoted to
298 // errors.
Colin Cross3edeee12017-04-04 12:59:48 -0700299 if !ctx.Windows() {
Colin Crossb916a382016-07-29 17:28:03 -0700300 flags.CFlags = append(flags.CFlags, "-fPIC")
301 }
302
303 if library.static() {
304 flags.CFlags = append(flags.CFlags, library.Properties.Static.Cflags...)
Colin Crossa48ab5b2017-02-14 15:28:44 -0800305 } else if library.shared() {
Colin Crossb916a382016-07-29 17:28:03 -0700306 flags.CFlags = append(flags.CFlags, library.Properties.Shared.Cflags...)
307 }
308
Colin Crossa48ab5b2017-02-14 15:28:44 -0800309 if library.shared() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700310 libName := library.getLibName(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700311 var f []string
Dan Willemsen01a405a2016-06-13 17:19:03 -0700312 if ctx.toolchain().Bionic() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700313 f = append(f,
314 "-nostdlib",
315 "-Wl,--gc-sections",
316 )
317 }
318
319 if ctx.Darwin() {
320 f = append(f,
321 "-dynamiclib",
322 "-single_module",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700323 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
324 )
Colin Cross7863cf52016-10-20 10:47:21 -0700325 if ctx.Arch().ArchType == android.X86 {
326 f = append(f,
327 "-read_only_relocs suppress",
328 )
329 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700330 } else {
331 f = append(f,
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700332 "-shared",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700333 "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix())
334 }
335
336 flags.LdFlags = append(f, flags.LdFlags...)
337 }
338
339 return flags
340}
341
Colin Crossf18e1102017-11-16 14:33:08 -0800342func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700343 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700344 if len(exportIncludeDirs) > 0 {
Colin Crossdad8c952017-04-26 14:55:27 -0700345 f := includeDirsToFlags(exportIncludeDirs)
346 flags.GlobalFlags = append(flags.GlobalFlags, f)
347 flags.YasmFlags = append(flags.YasmFlags, f)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700348 }
349
Jiyong Park7ed9de32018-10-15 22:25:07 +0900350 flags = library.baseCompiler.compilerFlags(ctx, flags, deps)
351 if library.buildStubs() {
352 flags = addStubLibraryCompilerFlags(flags)
353 }
354 return flags
Dan Willemsen273af7f2016-11-03 15:53:42 -0700355}
356
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700357func extractExportIncludesFromFlags(flags []string) []string {
358 // This method is used in the generation of rules which produce
359 // abi-dumps for source files. Exported headers are needed to infer the
360 // abi exported by a library and filter out the rest of the abi dumped
361 // from a source. We extract the include flags exported by a library.
362 // This includes the flags exported which are re-exported from static
363 // library dependencies, exported header library dependencies and
Jayant Chowdharyaf6eb712017-08-23 16:08:29 -0700364 // generated header dependencies. -isystem headers are not included
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700365 // since for bionic libraries, abi-filtering is taken care of by version
366 // scripts.
367 var exportedIncludes []string
368 for _, flag := range flags {
369 if strings.HasPrefix(flag, "-I") {
370 exportedIncludes = append(exportedIncludes, flag)
371 }
372 }
373 return exportedIncludes
374}
375
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700376func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Jiyong Park7ed9de32018-10-15 22:25:07 +0900377 if library.buildStubs() {
378 objs, versionScript := compileStubLibrary(ctx, flags, String(library.Properties.Stubs.Symbol_file), library.MutatedProperties.StubsVersion, "")
379 library.versionScriptPath = versionScript
380 return objs
381 }
382
Colin Cross5950f382016-12-13 12:50:57 -0800383 if !library.buildShared() && !library.buildStatic() {
384 if len(library.baseCompiler.Properties.Srcs) > 0 {
385 ctx.PropertyErrorf("srcs", "cc_library_headers must not have any srcs")
386 }
387 if len(library.Properties.Static.Srcs) > 0 {
388 ctx.PropertyErrorf("static.srcs", "cc_library_headers must not have any srcs")
389 }
390 if len(library.Properties.Shared.Srcs) > 0 {
391 ctx.PropertyErrorf("shared.srcs", "cc_library_headers must not have any srcs")
392 }
393 return Objects{}
394 }
Logan Chien2f2b8902018-07-10 15:01:19 +0800395 if ctx.shouldCreateVndkSourceAbiDump() || library.sabi.Properties.CreateSAbiDumps {
Jayant Chowdharya4fce192017-09-06 13:10:03 -0700396 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800397 var SourceAbiFlags []string
398 for _, dir := range exportIncludeDirs.Strings() {
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700399 SourceAbiFlags = append(SourceAbiFlags, "-I"+dir)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800400 }
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700401 for _, reexportedInclude := range extractExportIncludesFromFlags(library.sabi.Properties.ReexportedIncludeFlags) {
402 SourceAbiFlags = append(SourceAbiFlags, reexportedInclude)
403 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800404 flags.SAbiFlags = SourceAbiFlags
405 total_length := len(library.baseCompiler.Properties.Srcs) + len(deps.GeneratedSources) + len(library.Properties.Shared.Srcs) +
406 len(library.Properties.Static.Srcs)
407 if total_length > 0 {
408 flags.SAbiDump = true
409 }
410 }
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700411 objs := library.baseCompiler.compile(ctx, flags, deps)
412 library.reuseObjects = objs
Colin Cross2f336352016-10-26 10:03:47 -0700413 buildFlags := flagsToBuilderFlags(flags)
Colin Crossb916a382016-07-29 17:28:03 -0700414
Colin Cross4d9c2d12016-07-29 12:48:20 -0700415 if library.static() {
Colin Cross2f336352016-10-26 10:03:47 -0700416 srcs := android.PathsForModuleSrc(ctx, library.Properties.Static.Srcs)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700417 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceStaticLibrary,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800418 srcs, library.baseCompiler.pathDeps, library.baseCompiler.cFlagsDeps))
Colin Crossa48ab5b2017-02-14 15:28:44 -0800419 } else if library.shared() {
Colin Cross2f336352016-10-26 10:03:47 -0700420 srcs := android.PathsForModuleSrc(ctx, library.Properties.Shared.Srcs)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700421 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceSharedLibrary,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800422 srcs, library.baseCompiler.pathDeps, library.baseCompiler.cFlagsDeps))
Colin Crossb916a382016-07-29 17:28:03 -0700423 }
424
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700425 return objs
Colin Crossb916a382016-07-29 17:28:03 -0700426}
427
428type libraryInterface interface {
429 getWholeStaticMissingDeps() []string
430 static() bool
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700431 objs() Objects
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700432 reuseObjs() (Objects, []string, android.Paths)
Colin Cross26c34ed2016-09-30 17:10:16 -0700433 toc() android.OptionalPath
Colin Crossb916a382016-07-29 17:28:03 -0700434
435 // Returns true if the build options for the module have selected a static or shared build
436 buildStatic() bool
437 buildShared() bool
438
439 // Sets whether a specific variant is static or shared
Colin Crossa48ab5b2017-02-14 15:28:44 -0800440 setStatic()
441 setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700442}
443
444func (library *libraryDecorator) getLibName(ctx ModuleContext) string {
445 name := library.libName
446 if name == "" {
dimitryd95964a2018-11-07 13:43:34 +0100447 name = String(library.Properties.Stem)
448 if name == "" {
449 name = ctx.baseModuleName()
450 }
Colin Crossb916a382016-07-29 17:28:03 -0700451 }
452
Logan Chienf3511742017-10-31 18:04:35 +0800453 if ctx.isVndkExt() {
454 name = ctx.getVndkExtendsModuleName()
455 }
456
Colin Crossb916a382016-07-29 17:28:03 -0700457 if ctx.Host() && Bool(library.Properties.Unique_host_soname) {
458 if !strings.HasSuffix(name, "-host") {
459 name = name + "-host"
460 }
461 }
462
Colin Crossa48ab5b2017-02-14 15:28:44 -0800463 return name + library.MutatedProperties.VariantName
Colin Crossb916a382016-07-29 17:28:03 -0700464}
465
Jiyong Parkda732bd2018-11-02 18:23:15 +0900466var versioningMacroNamesListMutex sync.Mutex
467
Colin Crossb916a382016-07-29 17:28:03 -0700468func (library *libraryDecorator) linkerInit(ctx BaseModuleContext) {
469 location := InstallInSystem
Dan Albert61f32122018-07-26 14:00:24 -0700470 if library.baseLinker.sanitize.inSanitizerDir() {
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700471 location = InstallInSanitizerDir
Colin Crossb916a382016-07-29 17:28:03 -0700472 }
473 library.baseInstaller.location = location
Colin Crossb916a382016-07-29 17:28:03 -0700474 library.baseLinker.linkerInit(ctx)
Jiyong Park7ed9de32018-10-15 22:25:07 +0900475 // Let baseLinker know whether this variant is for stubs or not, so that
476 // it can omit things that are not required for linking stubs.
477 library.baseLinker.dynamicProperties.BuildStubs = library.buildStubs()
Jiyong Parkda732bd2018-11-02 18:23:15 +0900478
479 if library.buildStubs() {
480 macroNames := versioningMacroNamesList(ctx.Config())
481 myName := versioningMacroName(ctx.ModuleName())
482 versioningMacroNamesListMutex.Lock()
483 defer versioningMacroNamesListMutex.Unlock()
484 if (*macroNames)[myName] == "" {
485 (*macroNames)[myName] = ctx.ModuleName()
486 } else if (*macroNames)[myName] != ctx.ModuleName() {
487 ctx.ModuleErrorf("Macro name %q for versioning conflicts with macro name from module %q ", myName, (*macroNames)[myName])
488 }
489 }
Colin Crossb916a382016-07-29 17:28:03 -0700490}
491
Colin Cross37047f12016-12-13 17:06:13 -0800492func (library *libraryDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Dan Willemsen3a26eef2018-12-03 15:25:46 -0800493 if library.static() {
494 if library.Properties.Static.System_shared_libs != nil {
495 library.baseLinker.Properties.System_shared_libs = library.Properties.Static.System_shared_libs
496 }
497 } else if library.shared() {
498 if library.Properties.Shared.System_shared_libs != nil {
499 library.baseLinker.Properties.System_shared_libs = library.Properties.Shared.System_shared_libs
500 }
501 }
502
Colin Crossb916a382016-07-29 17:28:03 -0700503 deps = library.baseLinker.linkerDeps(ctx, deps)
504
505 if library.static() {
506 deps.WholeStaticLibs = append(deps.WholeStaticLibs,
507 library.Properties.Static.Whole_static_libs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700508 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Static.Static_libs...)
509 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Static.Shared_libs...)
Colin Crossa48ab5b2017-02-14 15:28:44 -0800510 } else if library.shared() {
Dan Willemsen2e47b342016-11-17 01:02:25 -0800511 if ctx.toolchain().Bionic() && !Bool(library.baseLinker.Properties.Nocrt) {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700512 if !ctx.useSdk() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700513 deps.CrtBegin = "crtbegin_so"
514 deps.CrtEnd = "crtend_so"
515 } else {
Dan Albertebedf672016-11-08 15:06:22 -0800516 // TODO(danalbert): Add generation of crt objects.
517 // For `sdk_version: "current"`, we don't actually have a
518 // freshly generated set of CRT objects. Use the last stable
519 // version.
520 version := ctx.sdkVersion()
521 if version == "current" {
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -0700522 version = getCurrentNdkPrebuiltVersion(ctx)
Dan Albertebedf672016-11-08 15:06:22 -0800523 }
524 deps.CrtBegin = "ndk_crtbegin_so." + version
525 deps.CrtEnd = "ndk_crtend_so." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700526 }
527 }
528 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Shared.Whole_static_libs...)
529 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Shared.Static_libs...)
530 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Shared.Shared_libs...)
531 }
Jiyong Park52d25bd2017-10-13 09:17:01 +0900532 if ctx.useVndk() {
533 deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs)
534 deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Vendor.Exclude_shared_libs)
535 deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs)
536 }
Jiyong Parkf9332f12018-02-01 00:54:12 +0900537 if ctx.inRecovery() {
538 deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Recovery.Exclude_static_libs)
539 deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Recovery.Exclude_shared_libs)
540 deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Recovery.Exclude_static_libs)
541 }
Colin Cross2383f3b2018-02-06 14:40:13 -0800542
Colin Cross2383f3b2018-02-06 14:40:13 -0800543 android.ExtractSourceDeps(ctx, library.Properties.Unexported_symbols_list)
544 android.ExtractSourceDeps(ctx, library.Properties.Force_symbols_not_weak_list)
545 android.ExtractSourceDeps(ctx, library.Properties.Force_symbols_weak_list)
Colin Cross2383f3b2018-02-06 14:40:13 -0800546
Colin Cross4d9c2d12016-07-29 12:48:20 -0700547 return deps
548}
549
Colin Crossb916a382016-07-29 17:28:03 -0700550func (library *libraryDecorator) linkStatic(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700551 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700552
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700553 library.objects = deps.WholeStaticLibObjs.Copy()
554 library.objects = library.objects.Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700555
Colin Cross86803cf2018-02-15 14:12:26 -0800556 fileName := ctx.ModuleName() + library.MutatedProperties.VariantName + staticLibraryExtension
557 outputFile := android.PathForModuleOut(ctx, fileName)
Dan Willemsen581341d2017-02-09 16:16:31 -0800558 builderFlags := flagsToBuilderFlags(flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700559
Dan Willemsen569edc52018-11-19 09:33:29 -0800560 if Bool(library.baseLinker.Properties.Use_version_lib) {
561 if ctx.Host() {
562 versionedOutputFile := outputFile
563 outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
564 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
565 } else {
566 versionedOutputFile := android.PathForModuleOut(ctx, "versioned", fileName)
567 library.distFile = android.OptionalPathForPath(versionedOutputFile)
568 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
569 }
Colin Cross86803cf2018-02-15 14:12:26 -0800570 }
571
Dan Willemsen581341d2017-02-09 16:16:31 -0800572 TransformObjToStaticLib(ctx, library.objects.objFiles, builderFlags, outputFile, objs.tidyFiles)
573
574 library.coverageOutputFile = TransformCoverageFilesToLib(ctx, library.objects, builderFlags,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800575 ctx.ModuleName()+library.MutatedProperties.VariantName)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700576
577 library.wholeStaticMissingDeps = ctx.GetMissingDependencies()
578
579 ctx.CheckbuildFile(outputFile)
580
581 return outputFile
582}
583
Colin Crossb916a382016-07-29 17:28:03 -0700584func (library *libraryDecorator) linkShared(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700585 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700586
587 var linkerDeps android.Paths
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700588 linkerDeps = append(linkerDeps, flags.LdFlagsDeps...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700589
Colin Cross2383f3b2018-02-06 14:40:13 -0800590 unexportedSymbols := ctx.ExpandOptionalSource(library.Properties.Unexported_symbols_list, "unexported_symbols_list")
591 forceNotWeakSymbols := ctx.ExpandOptionalSource(library.Properties.Force_symbols_not_weak_list, "force_symbols_not_weak_list")
592 forceWeakSymbols := ctx.ExpandOptionalSource(library.Properties.Force_symbols_weak_list, "force_symbols_weak_list")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700593 if !ctx.Darwin() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700594 if unexportedSymbols.Valid() {
595 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
596 }
597 if forceNotWeakSymbols.Valid() {
598 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
599 }
600 if forceWeakSymbols.Valid() {
601 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
602 }
603 } else {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700604 if unexportedSymbols.Valid() {
605 flags.LdFlags = append(flags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
606 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
607 }
608 if forceNotWeakSymbols.Valid() {
609 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
610 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
611 }
612 if forceWeakSymbols.Valid() {
613 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
614 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
615 }
616 }
617
618 fileName := library.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
619 outputFile := android.PathForModuleOut(ctx, fileName)
620 ret := outputFile
621
622 builderFlags := flagsToBuilderFlags(flags)
623
Colin Crossb496cfd2018-09-10 16:50:05 -0700624 // Optimize out relinking against shared libraries whose interface hasn't changed by
625 // depending on a table of contents file instead of the library itself.
626 tocPath := outputFile.RelPathString()
627 tocPath = pathtools.ReplaceExtension(tocPath, flags.Toolchain.ShlibSuffix()[1:]+".toc")
628 tocFile := android.PathForOutput(ctx, tocPath)
629 library.tocFile = android.OptionalPathForPath(tocFile)
630 TransformSharedObjectToToc(ctx, outputFile, tocFile, builderFlags)
Colin Cross89562dc2016-10-03 17:47:19 -0700631
Colin Cross4d9c2d12016-07-29 12:48:20 -0700632 if library.stripper.needsStrip(ctx) {
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -0700633 // b/80093681, GNU strip/objcopy bug.
634 // Use llvm-{strip,objcopy} when clang lld is used.
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700635 builderFlags.stripUseLlvmStrip = library.baseLinker.useClangLld(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700636 strippedOutputFile := outputFile
637 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
638 library.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
639 }
640
Colin Crossb60190a2018-09-04 16:28:17 -0700641 library.unstrippedOutputFile = outputFile
642
Dan Willemsen569edc52018-11-19 09:33:29 -0800643 if Bool(library.baseLinker.Properties.Use_version_lib) {
644 if ctx.Host() {
645 versionedOutputFile := outputFile
646 outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
647 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
648 } else {
649 versionedOutputFile := android.PathForModuleOut(ctx, "versioned", fileName)
650 library.distFile = android.OptionalPathForPath(versionedOutputFile)
651
652 if library.stripper.needsStrip(ctx) {
653 out := android.PathForModuleOut(ctx, "versioned-stripped", fileName)
654 library.distFile = android.OptionalPathForPath(out)
655 library.stripper.strip(ctx, versionedOutputFile, out, builderFlags)
656 }
657
658 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
659 }
Colin Cross86803cf2018-02-15 14:12:26 -0800660 }
661
Colin Cross4d9c2d12016-07-29 12:48:20 -0700662 sharedLibs := deps.SharedLibs
663 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
664
Colin Cross26c34ed2016-09-30 17:10:16 -0700665 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
666 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700667 linkerDeps = append(linkerDeps, objs.tidyFiles...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700668
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700669 TransformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700670 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
671 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile)
672
Dan Willemsen581341d2017-02-09 16:16:31 -0800673 objs.coverageFiles = append(objs.coverageFiles, deps.StaticLibObjs.coverageFiles...)
674 objs.coverageFiles = append(objs.coverageFiles, deps.WholeStaticLibObjs.coverageFiles...)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800675
676 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.StaticLibObjs.sAbiDumpFiles...)
677 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.WholeStaticLibObjs.sAbiDumpFiles...)
678
Dan Willemsen581341d2017-02-09 16:16:31 -0800679 library.coverageOutputFile = TransformCoverageFilesToLib(ctx, objs, builderFlags, library.getLibName(ctx))
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700680 library.linkSAbiDumpFiles(ctx, objs, fileName, ret)
Dan Willemsen581341d2017-02-09 16:16:31 -0800681
Colin Cross4d9c2d12016-07-29 12:48:20 -0700682 return ret
683}
684
Logan Chien7eefdc42018-07-11 18:10:41 +0800685func getRefAbiDumpFile(ctx ModuleContext, vndkVersion, fileName string) android.Path {
Logan Chienf4b79c62018-08-02 02:27:02 +0800686 isLlndk := inList(ctx.baseModuleName(), llndkLibraries) || inList(ctx.baseModuleName(), ndkMigratedLibs)
Logan Chien7eefdc42018-07-11 18:10:41 +0800687
688 refAbiDumpTextFile := android.PathForVndkRefAbiDump(ctx, vndkVersion, fileName, isLlndk, false)
689 refAbiDumpGzipFile := android.PathForVndkRefAbiDump(ctx, vndkVersion, fileName, isLlndk, true)
690
691 if refAbiDumpTextFile.Valid() {
692 if refAbiDumpGzipFile.Valid() {
693 ctx.ModuleErrorf(
694 "Two reference ABI dump files are found: %q and %q. Please delete the stale one.",
695 refAbiDumpTextFile, refAbiDumpGzipFile)
696 return nil
697 }
698 return refAbiDumpTextFile.Path()
699 }
700 if refAbiDumpGzipFile.Valid() {
701 return UnzipRefDump(ctx, refAbiDumpGzipFile.Path(), fileName)
702 }
703 return nil
704}
705
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700706func (library *libraryDecorator) linkSAbiDumpFiles(ctx ModuleContext, objs Objects, fileName string, soFile android.Path) {
Logan Chien2f2b8902018-07-10 15:01:19 +0800707 if len(objs.sAbiDumpFiles) > 0 && ctx.shouldCreateVndkSourceAbiDump() {
Logan Chiena8f51582018-03-21 11:53:48 +0800708 vndkVersion := ctx.DeviceConfig().PlatformVndkVersion()
709 if ver := ctx.DeviceConfig().VndkVersion(); ver != "" && ver != "current" {
Logan Chienf3511742017-10-31 18:04:35 +0800710 vndkVersion = ver
711 }
712
Jayant Chowdharya4fce192017-09-06 13:10:03 -0700713 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800714 var SourceAbiFlags []string
715 for _, dir := range exportIncludeDirs.Strings() {
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700716 SourceAbiFlags = append(SourceAbiFlags, "-I"+dir)
717 }
718 for _, reexportedInclude := range extractExportIncludesFromFlags(library.sabi.Properties.ReexportedIncludeFlags) {
719 SourceAbiFlags = append(SourceAbiFlags, reexportedInclude)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800720 }
721 exportedHeaderFlags := strings.Join(SourceAbiFlags, " ")
Jayant Chowdharydf344d52018-01-17 11:11:42 -0800722 library.sAbiOutputFile = TransformDumpToLinkedDump(ctx, objs.sAbiDumpFiles, soFile, fileName, exportedHeaderFlags)
Logan Chien2f2b8902018-07-10 15:01:19 +0800723
Logan Chien7eefdc42018-07-11 18:10:41 +0800724 refAbiDumpFile := getRefAbiDumpFile(ctx, vndkVersion, fileName)
725 if refAbiDumpFile != nil {
Logan Chienf3511742017-10-31 18:04:35 +0800726 library.sAbiDiff = SourceAbiDiff(ctx, library.sAbiOutputFile.Path(),
Logan Chien7eefdc42018-07-11 18:10:41 +0800727 refAbiDumpFile, fileName, exportedHeaderFlags, ctx.isVndkExt())
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800728 }
729 }
730}
731
Colin Crossb916a382016-07-29 17:28:03 -0700732func (library *libraryDecorator) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700733 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700734
Colin Crossad59e752017-11-16 14:29:11 -0800735 objs = deps.Objs.Copy().Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700736 var out android.Path
Colin Crossa48ab5b2017-02-14 15:28:44 -0800737 if library.static() || library.header() {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700738 out = library.linkStatic(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700739 } else {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700740 out = library.linkShared(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700741 }
742
743 library.exportIncludes(ctx, "-I")
744 library.reexportFlags(deps.ReexportedFlags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700745 library.reexportDeps(deps.ReexportedFlagsDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700746
Nan Zhang0007d812017-11-07 10:57:05 -0800747 if Bool(library.Properties.Aidl.Export_aidl_headers) {
Dan Willemsene1240db2016-11-03 14:28:51 -0700748 if library.baseCompiler.hasSrcExt(".aidl") {
Colin Cross10d22312017-05-03 11:01:58 -0700749 flags := []string{
Dan Willemsene1240db2016-11-03 14:28:51 -0700750 "-I" + android.PathForModuleGen(ctx, "aidl").String(),
Colin Cross10d22312017-05-03 11:01:58 -0700751 }
752 library.reexportFlags(flags)
753 library.reuseExportedFlags = append(library.reuseExportedFlags, flags...)
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800754 library.reexportDeps(library.baseCompiler.pathDeps) // TODO: restrict to aidl deps
755 library.reuseExportedDeps = append(library.reuseExportedDeps, library.baseCompiler.pathDeps...)
Dan Willemsene1240db2016-11-03 14:28:51 -0700756 }
757 }
758
Nan Zhang0007d812017-11-07 10:57:05 -0800759 if Bool(library.Properties.Proto.Export_proto_headers) {
Dan Willemsene1240db2016-11-03 14:28:51 -0700760 if library.baseCompiler.hasSrcExt(".proto") {
Dan Willemsenab9f4262018-02-14 13:58:34 -0800761 includes := []string{}
762 if flags.ProtoRoot {
763 includes = append(includes, "-I"+android.ProtoSubDir(ctx).String())
Colin Cross10d22312017-05-03 11:01:58 -0700764 }
Dan Willemsenab9f4262018-02-14 13:58:34 -0800765 includes = append(includes, "-I"+android.ProtoDir(ctx).String())
766 library.reexportFlags(includes)
767 library.reuseExportedFlags = append(library.reuseExportedFlags, includes...)
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800768 library.reexportDeps(library.baseCompiler.pathDeps) // TODO: restrict to proto deps
769 library.reuseExportedDeps = append(library.reuseExportedDeps, library.baseCompiler.pathDeps...)
Colin Cross0c461f12016-10-20 16:11:43 -0700770 }
771 }
772
Inseob Kim21f26902018-09-06 00:55:20 +0900773 if library.baseCompiler.hasSrcExt(".sysprop") {
774 flags := []string{
775 "-I" + android.PathForModuleGen(ctx, "sysprop", "include").String(),
776 }
777 library.reexportFlags(flags)
778 library.reuseExportedFlags = append(library.reuseExportedFlags, flags...)
779 }
780
Jiyong Parkda732bd2018-11-02 18:23:15 +0900781 if library.buildStubs() {
782 library.reexportFlags([]string{"-D" + versioningMacroName(ctx.ModuleName()) + "=" + library.stubsVersion()})
783 }
784
Colin Cross4d9c2d12016-07-29 12:48:20 -0700785 return out
786}
787
Colin Crossb916a382016-07-29 17:28:03 -0700788func (library *libraryDecorator) buildStatic() bool {
Colin Cross38b40df2018-04-10 16:14:46 -0700789 return library.MutatedProperties.BuildStatic && BoolDefault(library.Properties.Static.Enabled, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700790}
791
Colin Crossb916a382016-07-29 17:28:03 -0700792func (library *libraryDecorator) buildShared() bool {
Colin Cross38b40df2018-04-10 16:14:46 -0700793 return library.MutatedProperties.BuildShared && BoolDefault(library.Properties.Shared.Enabled, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700794}
795
Colin Crossb916a382016-07-29 17:28:03 -0700796func (library *libraryDecorator) getWholeStaticMissingDeps() []string {
Colin Crossd2343a32018-04-30 14:50:01 -0700797 return append([]string(nil), library.wholeStaticMissingDeps...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700798}
799
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700800func (library *libraryDecorator) objs() Objects {
801 return library.objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700802}
803
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700804func (library *libraryDecorator) reuseObjs() (Objects, []string, android.Paths) {
805 return library.reuseObjects, library.reuseExportedFlags, library.reuseExportedDeps
Colin Cross4d9c2d12016-07-29 12:48:20 -0700806}
807
Colin Cross26c34ed2016-09-30 17:10:16 -0700808func (library *libraryDecorator) toc() android.OptionalPath {
809 return library.tocFile
810}
811
Colin Crossb916a382016-07-29 17:28:03 -0700812func (library *libraryDecorator) install(ctx ModuleContext, file android.Path) {
Colin Crossc43ae772017-04-14 15:42:53 -0700813 if library.shared() {
Justin Yun8fe12122017-12-07 17:18:15 +0900814 if ctx.Device() && ctx.useVndk() {
815 if ctx.isVndkSp() {
816 library.baseInstaller.subDir = "vndk-sp"
817 } else if ctx.isVndk() {
818 library.baseInstaller.subDir = "vndk"
819 }
Logan Chienf3511742017-10-31 18:04:35 +0800820
821 // Append a version to vndk or vndk-sp directories on the system partition.
822 if ctx.isVndk() && !ctx.isVndkExt() {
823 vndkVersion := ctx.DeviceConfig().PlatformVndkVersion()
824 if vndkVersion != "current" && vndkVersion != "" {
825 library.baseInstaller.subDir += "-" + vndkVersion
826 }
Justin Yun8effde42017-06-23 19:24:43 +0900827 }
828 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700829 library.baseInstaller.install(ctx, file)
830 }
Dan Albertf563d252017-10-13 00:29:00 -0700831
Dan Albert281f22b2017-12-13 15:03:47 -0800832 if Bool(library.Properties.Static_ndk_lib) && library.static() &&
Jiyong Parkf9332f12018-02-01 00:54:12 +0900833 !ctx.useVndk() && !ctx.inRecovery() && ctx.Device() &&
Jiyong Park7ed9de32018-10-15 22:25:07 +0900834 library.baseLinker.sanitize.isUnsanitizedVariant() &&
835 !library.buildStubs() {
Dan Albertf563d252017-10-13 00:29:00 -0700836 installPath := getNdkSysrootBase(ctx).Join(
Dan Albertea4b7b92018-04-25 16:05:30 -0700837 ctx, "usr/lib", config.NDKTriple(ctx.toolchain()), file.Base())
Dan Albertf563d252017-10-13 00:29:00 -0700838
839 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
840 Rule: android.Cp,
841 Description: "install " + installPath.Base(),
842 Output: installPath,
843 Input: file,
844 })
845
Colin Cross0875c522017-11-28 17:34:01 -0800846 library.ndkSysrootPath = installPath
Dan Albertf563d252017-10-13 00:29:00 -0700847 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700848}
849
Colin Crossb916a382016-07-29 17:28:03 -0700850func (library *libraryDecorator) static() bool {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800851 return library.MutatedProperties.VariantIsStatic
Colin Cross4d9c2d12016-07-29 12:48:20 -0700852}
853
Colin Crossa48ab5b2017-02-14 15:28:44 -0800854func (library *libraryDecorator) shared() bool {
855 return library.MutatedProperties.VariantIsShared
856}
857
858func (library *libraryDecorator) header() bool {
859 return !library.static() && !library.shared()
860}
861
862func (library *libraryDecorator) setStatic() {
863 library.MutatedProperties.VariantIsStatic = true
864 library.MutatedProperties.VariantIsShared = false
865}
866
867func (library *libraryDecorator) setShared() {
868 library.MutatedProperties.VariantIsStatic = false
869 library.MutatedProperties.VariantIsShared = true
Colin Crossb916a382016-07-29 17:28:03 -0700870}
871
Colin Crossab3b7322016-12-09 14:46:15 -0800872func (library *libraryDecorator) BuildOnlyStatic() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800873 library.MutatedProperties.BuildShared = false
Colin Crossab3b7322016-12-09 14:46:15 -0800874}
875
876func (library *libraryDecorator) BuildOnlyShared() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800877 library.MutatedProperties.BuildStatic = false
Colin Crossab3b7322016-12-09 14:46:15 -0800878}
879
Colin Cross5950f382016-12-13 12:50:57 -0800880func (library *libraryDecorator) HeaderOnly() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800881 library.MutatedProperties.BuildShared = false
882 library.MutatedProperties.BuildStatic = false
Colin Cross5950f382016-12-13 12:50:57 -0800883}
884
Jiyong Park7ed9de32018-10-15 22:25:07 +0900885func (library *libraryDecorator) buildStubs() bool {
886 return library.MutatedProperties.BuildStubs
887}
888
889func (library *libraryDecorator) stubsVersion() string {
890 return library.MutatedProperties.StubsVersion
891}
892
Jiyong Parkda732bd2018-11-02 18:23:15 +0900893func versioningMacroNamesList(config android.Config) *map[string]string {
894 return config.Once("versioningMacroNamesList", func() interface{} {
895 m := make(map[string]string)
896 return &m
897 }).(*map[string]string)
898}
899
900// alphanumeric and _ characters are preserved.
901// other characters are all converted to _
902var charsNotForMacro = regexp.MustCompile("[^a-zA-Z0-9_]+")
903
904func versioningMacroName(moduleName string) string {
905 macroName := charsNotForMacro.ReplaceAllString(moduleName, "_")
906 macroName = strings.ToUpper(moduleName)
907 return "__" + macroName + "_API__"
908}
909
Colin Crossab3b7322016-12-09 14:46:15 -0800910func NewLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700911 module := newModule(hod, android.MultilibBoth)
912
Colin Crossb916a382016-07-29 17:28:03 -0700913 library := &libraryDecorator{
Colin Crossa48ab5b2017-02-14 15:28:44 -0800914 MutatedProperties: LibraryMutatedProperties{
Colin Crossab3b7322016-12-09 14:46:15 -0800915 BuildShared: true,
916 BuildStatic: true,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700917 },
Colin Crossb916a382016-07-29 17:28:03 -0700918 baseCompiler: NewBaseCompiler(),
Dan Albert61f32122018-07-26 14:00:24 -0700919 baseLinker: NewBaseLinker(module.sanitize),
Colin Crossb916a382016-07-29 17:28:03 -0700920 baseInstaller: NewBaseInstaller("lib", "lib64", InstallInSystem),
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800921 sabi: module.sabi,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700922 }
923
Colin Crossb916a382016-07-29 17:28:03 -0700924 module.compiler = library
925 module.linker = library
926 module.installer = library
927
928 return module, library
929}
930
Colin Cross10d22312017-05-03 11:01:58 -0700931// connects a shared library to a static library in order to reuse its .o files to avoid
932// compiling source files twice.
933func reuseStaticLibrary(mctx android.BottomUpMutatorContext, static, shared *Module) {
934 if staticCompiler, ok := static.compiler.(*libraryDecorator); ok {
935 sharedCompiler := shared.compiler.(*libraryDecorator)
Dan Willemsen3a26eef2018-12-03 15:25:46 -0800936
937 // Check libraries in addition to cflags, since libraries may be exporting different
938 // include directories.
Colin Cross10d22312017-05-03 11:01:58 -0700939 if len(staticCompiler.Properties.Static.Cflags) == 0 &&
Dan Willemsen3a26eef2018-12-03 15:25:46 -0800940 len(sharedCompiler.Properties.Shared.Cflags) == 0 &&
941 len(staticCompiler.Properties.Static.Whole_static_libs) == 0 &&
942 len(sharedCompiler.Properties.Shared.Whole_static_libs) == 0 &&
943 len(staticCompiler.Properties.Static.Static_libs) == 0 &&
944 len(sharedCompiler.Properties.Shared.Static_libs) == 0 &&
945 len(staticCompiler.Properties.Static.Shared_libs) == 0 &&
946 len(sharedCompiler.Properties.Shared.Shared_libs) == 0 &&
947 staticCompiler.Properties.Static.System_shared_libs == nil &&
948 sharedCompiler.Properties.Shared.System_shared_libs == nil {
Colin Cross10d22312017-05-03 11:01:58 -0700949
950 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
951 sharedCompiler.baseCompiler.Properties.OriginalSrcs =
952 sharedCompiler.baseCompiler.Properties.Srcs
953 sharedCompiler.baseCompiler.Properties.Srcs = nil
954 sharedCompiler.baseCompiler.Properties.Generated_sources = nil
955 }
956 }
957}
958
Colin Crosse40b4ea2018-10-02 22:25:58 -0700959func LinkageMutator(mctx android.BottomUpMutatorContext) {
Colin Crossb916a382016-07-29 17:28:03 -0700960 if m, ok := mctx.Module().(*Module); ok && m.linker != nil {
961 if library, ok := m.linker.(libraryInterface); ok {
962 var modules []blueprint.Module
963 if library.buildStatic() && library.buildShared() {
964 modules = mctx.CreateLocalVariations("static", "shared")
965 static := modules[0].(*Module)
966 shared := modules[1].(*Module)
967
Colin Crossa48ab5b2017-02-14 15:28:44 -0800968 static.linker.(libraryInterface).setStatic()
969 shared.linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700970
Colin Cross10d22312017-05-03 11:01:58 -0700971 reuseStaticLibrary(mctx, static, shared)
972
Colin Crossb916a382016-07-29 17:28:03 -0700973 } else if library.buildStatic() {
974 modules = mctx.CreateLocalVariations("static")
Colin Crossa48ab5b2017-02-14 15:28:44 -0800975 modules[0].(*Module).linker.(libraryInterface).setStatic()
Colin Crossb916a382016-07-29 17:28:03 -0700976 } else if library.buildShared() {
977 modules = mctx.CreateLocalVariations("shared")
Colin Crossa48ab5b2017-02-14 15:28:44 -0800978 modules[0].(*Module).linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700979 }
980 }
981 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700982}
Jiyong Park7ed9de32018-10-15 22:25:07 +0900983
984// Version mutator splits a module into the mandatory non-stubs variant
985// (which is named "impl") and zero or more stubs variants.
986func versionMutator(mctx android.BottomUpMutatorContext) {
987 if mctx.Os() != android.Android {
988 return
989 }
990
991 if m, ok := mctx.Module().(*Module); ok && !m.inRecovery() && m.linker != nil {
992 if library, ok := m.linker.(*libraryDecorator); ok && library.buildShared() {
993 versions := []string{""}
994 for _, v := range library.Properties.Stubs.Versions {
995 versions = append(versions, v)
996 }
997 modules := mctx.CreateVariations(versions...)
998 for i, m := range modules {
999 l := m.(*Module).linker.(*libraryDecorator)
1000 if i == 0 {
1001 l.MutatedProperties.BuildStubs = false
1002 continue
1003 }
1004 // Mark that this variant is for stubs.
1005 l.MutatedProperties.BuildStubs = true
1006 l.MutatedProperties.StubsVersion = versions[i]
1007 m.(*Module).Properties.HideFromMake = true
1008 }
1009 } else {
1010 mctx.CreateVariations("")
1011 }
1012 return
1013 }
1014 if genrule, ok := mctx.Module().(*genrule.Module); ok {
1015 if props, ok := genrule.Extra.(*GenruleExtraProperties); ok && !props.InRecovery {
1016 mctx.CreateVariations("")
1017 return
1018 }
1019 }
1020}