blob: 2fd423b6b8ff83d07359371c4f0aab43a0cbded8 [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() {
354 flags = addStubLibraryCompilerFlags(flags)
355 }
356 return flags
Dan Willemsen273af7f2016-11-03 15:53:42 -0700357}
358
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700359func extractExportIncludesFromFlags(flags []string) []string {
360 // This method is used in the generation of rules which produce
361 // abi-dumps for source files. Exported headers are needed to infer the
362 // abi exported by a library and filter out the rest of the abi dumped
363 // from a source. We extract the include flags exported by a library.
364 // This includes the flags exported which are re-exported from static
365 // library dependencies, exported header library dependencies and
Jayant Chowdharyaf6eb712017-08-23 16:08:29 -0700366 // generated header dependencies. -isystem headers are not included
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700367 // since for bionic libraries, abi-filtering is taken care of by version
368 // scripts.
369 var exportedIncludes []string
370 for _, flag := range flags {
371 if strings.HasPrefix(flag, "-I") {
372 exportedIncludes = append(exportedIncludes, flag)
373 }
374 }
375 return exportedIncludes
376}
377
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700378func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Jiyong Park7ed9de32018-10-15 22:25:07 +0900379 if library.buildStubs() {
380 objs, versionScript := compileStubLibrary(ctx, flags, String(library.Properties.Stubs.Symbol_file), library.MutatedProperties.StubsVersion, "")
381 library.versionScriptPath = versionScript
382 return objs
383 }
384
Colin Cross5950f382016-12-13 12:50:57 -0800385 if !library.buildShared() && !library.buildStatic() {
386 if len(library.baseCompiler.Properties.Srcs) > 0 {
387 ctx.PropertyErrorf("srcs", "cc_library_headers must not have any srcs")
388 }
389 if len(library.Properties.Static.Srcs) > 0 {
390 ctx.PropertyErrorf("static.srcs", "cc_library_headers must not have any srcs")
391 }
392 if len(library.Properties.Shared.Srcs) > 0 {
393 ctx.PropertyErrorf("shared.srcs", "cc_library_headers must not have any srcs")
394 }
395 return Objects{}
396 }
Logan Chien2f2b8902018-07-10 15:01:19 +0800397 if ctx.shouldCreateVndkSourceAbiDump() || library.sabi.Properties.CreateSAbiDumps {
Jayant Chowdharya4fce192017-09-06 13:10:03 -0700398 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800399 var SourceAbiFlags []string
400 for _, dir := range exportIncludeDirs.Strings() {
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700401 SourceAbiFlags = append(SourceAbiFlags, "-I"+dir)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800402 }
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700403 for _, reexportedInclude := range extractExportIncludesFromFlags(library.sabi.Properties.ReexportedIncludeFlags) {
404 SourceAbiFlags = append(SourceAbiFlags, reexportedInclude)
405 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800406 flags.SAbiFlags = SourceAbiFlags
407 total_length := len(library.baseCompiler.Properties.Srcs) + len(deps.GeneratedSources) + len(library.Properties.Shared.Srcs) +
408 len(library.Properties.Static.Srcs)
409 if total_length > 0 {
410 flags.SAbiDump = true
411 }
412 }
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700413 objs := library.baseCompiler.compile(ctx, flags, deps)
414 library.reuseObjects = objs
Colin Cross2f336352016-10-26 10:03:47 -0700415 buildFlags := flagsToBuilderFlags(flags)
Colin Crossb916a382016-07-29 17:28:03 -0700416
Colin Cross4d9c2d12016-07-29 12:48:20 -0700417 if library.static() {
dimitry0345ad82018-12-05 16:28:14 +0100418 srcs := ctx.ExpandSources(library.Properties.Static.Srcs, nil)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700419 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceStaticLibrary,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800420 srcs, library.baseCompiler.pathDeps, library.baseCompiler.cFlagsDeps))
Colin Crossa48ab5b2017-02-14 15:28:44 -0800421 } else if library.shared() {
dimitry0345ad82018-12-05 16:28:14 +0100422 srcs := ctx.ExpandSources(library.Properties.Shared.Srcs, nil)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700423 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceSharedLibrary,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800424 srcs, library.baseCompiler.pathDeps, library.baseCompiler.cFlagsDeps))
Colin Crossb916a382016-07-29 17:28:03 -0700425 }
426
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700427 return objs
Colin Crossb916a382016-07-29 17:28:03 -0700428}
429
430type libraryInterface interface {
431 getWholeStaticMissingDeps() []string
432 static() bool
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700433 objs() Objects
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700434 reuseObjs() (Objects, []string, android.Paths)
Colin Cross26c34ed2016-09-30 17:10:16 -0700435 toc() android.OptionalPath
Colin Crossb916a382016-07-29 17:28:03 -0700436
437 // Returns true if the build options for the module have selected a static or shared build
438 buildStatic() bool
439 buildShared() bool
440
441 // Sets whether a specific variant is static or shared
Colin Crossa48ab5b2017-02-14 15:28:44 -0800442 setStatic()
443 setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700444}
445
446func (library *libraryDecorator) getLibName(ctx ModuleContext) string {
447 name := library.libName
448 if name == "" {
dimitryd95964a2018-11-07 13:43:34 +0100449 name = String(library.Properties.Stem)
450 if name == "" {
451 name = ctx.baseModuleName()
452 }
Colin Crossb916a382016-07-29 17:28:03 -0700453 }
454
Logan Chienf3511742017-10-31 18:04:35 +0800455 if ctx.isVndkExt() {
456 name = ctx.getVndkExtendsModuleName()
457 }
458
Colin Crossb916a382016-07-29 17:28:03 -0700459 if ctx.Host() && Bool(library.Properties.Unique_host_soname) {
460 if !strings.HasSuffix(name, "-host") {
461 name = name + "-host"
462 }
463 }
464
Colin Crossa48ab5b2017-02-14 15:28:44 -0800465 return name + library.MutatedProperties.VariantName
Colin Crossb916a382016-07-29 17:28:03 -0700466}
467
Jiyong Parkda732bd2018-11-02 18:23:15 +0900468var versioningMacroNamesListMutex sync.Mutex
469
Colin Crossb916a382016-07-29 17:28:03 -0700470func (library *libraryDecorator) linkerInit(ctx BaseModuleContext) {
471 location := InstallInSystem
Dan Albert61f32122018-07-26 14:00:24 -0700472 if library.baseLinker.sanitize.inSanitizerDir() {
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700473 location = InstallInSanitizerDir
Colin Crossb916a382016-07-29 17:28:03 -0700474 }
475 library.baseInstaller.location = location
Colin Crossb916a382016-07-29 17:28:03 -0700476 library.baseLinker.linkerInit(ctx)
Jiyong Park7ed9de32018-10-15 22:25:07 +0900477 // Let baseLinker know whether this variant is for stubs or not, so that
478 // it can omit things that are not required for linking stubs.
479 library.baseLinker.dynamicProperties.BuildStubs = library.buildStubs()
Jiyong Parkda732bd2018-11-02 18:23:15 +0900480
481 if library.buildStubs() {
482 macroNames := versioningMacroNamesList(ctx.Config())
483 myName := versioningMacroName(ctx.ModuleName())
484 versioningMacroNamesListMutex.Lock()
485 defer versioningMacroNamesListMutex.Unlock()
486 if (*macroNames)[myName] == "" {
487 (*macroNames)[myName] = ctx.ModuleName()
488 } else if (*macroNames)[myName] != ctx.ModuleName() {
489 ctx.ModuleErrorf("Macro name %q for versioning conflicts with macro name from module %q ", myName, (*macroNames)[myName])
490 }
491 }
Colin Crossb916a382016-07-29 17:28:03 -0700492}
493
dimitry0345ad82018-12-05 16:28:14 +0100494func (library *libraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
495 deps = library.baseCompiler.compilerDeps(ctx, deps)
496
497 if library.static() {
498 android.ExtractSourcesDeps(ctx, library.Properties.Static.Srcs)
499 } else if library.shared() {
500 android.ExtractSourcesDeps(ctx, library.Properties.Shared.Srcs)
501 }
502
503 return deps
504}
505
Colin Cross37047f12016-12-13 17:06:13 -0800506func (library *libraryDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Dan Willemsen3a26eef2018-12-03 15:25:46 -0800507 if library.static() {
508 if library.Properties.Static.System_shared_libs != nil {
509 library.baseLinker.Properties.System_shared_libs = library.Properties.Static.System_shared_libs
510 }
511 } else if library.shared() {
512 if library.Properties.Shared.System_shared_libs != nil {
513 library.baseLinker.Properties.System_shared_libs = library.Properties.Shared.System_shared_libs
514 }
515 }
516
Colin Crossb916a382016-07-29 17:28:03 -0700517 deps = library.baseLinker.linkerDeps(ctx, deps)
518
519 if library.static() {
520 deps.WholeStaticLibs = append(deps.WholeStaticLibs,
521 library.Properties.Static.Whole_static_libs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700522 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Static.Static_libs...)
523 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Static.Shared_libs...)
Colin Crossa48ab5b2017-02-14 15:28:44 -0800524 } else if library.shared() {
Dan Willemsen2e47b342016-11-17 01:02:25 -0800525 if ctx.toolchain().Bionic() && !Bool(library.baseLinker.Properties.Nocrt) {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700526 if !ctx.useSdk() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700527 deps.CrtBegin = "crtbegin_so"
528 deps.CrtEnd = "crtend_so"
529 } else {
Dan Albertebedf672016-11-08 15:06:22 -0800530 // TODO(danalbert): Add generation of crt objects.
531 // For `sdk_version: "current"`, we don't actually have a
532 // freshly generated set of CRT objects. Use the last stable
533 // version.
534 version := ctx.sdkVersion()
535 if version == "current" {
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -0700536 version = getCurrentNdkPrebuiltVersion(ctx)
Dan Albertebedf672016-11-08 15:06:22 -0800537 }
538 deps.CrtBegin = "ndk_crtbegin_so." + version
539 deps.CrtEnd = "ndk_crtend_so." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700540 }
541 }
542 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Shared.Whole_static_libs...)
543 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Shared.Static_libs...)
544 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Shared.Shared_libs...)
545 }
Jiyong Park52d25bd2017-10-13 09:17:01 +0900546 if ctx.useVndk() {
547 deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs)
548 deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Vendor.Exclude_shared_libs)
549 deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs)
550 }
Jiyong Parkf9332f12018-02-01 00:54:12 +0900551 if ctx.inRecovery() {
552 deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Recovery.Exclude_static_libs)
553 deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Recovery.Exclude_shared_libs)
554 deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Recovery.Exclude_static_libs)
555 }
Colin Cross2383f3b2018-02-06 14:40:13 -0800556
Colin Cross2383f3b2018-02-06 14:40:13 -0800557 android.ExtractSourceDeps(ctx, library.Properties.Unexported_symbols_list)
558 android.ExtractSourceDeps(ctx, library.Properties.Force_symbols_not_weak_list)
559 android.ExtractSourceDeps(ctx, library.Properties.Force_symbols_weak_list)
Colin Cross2383f3b2018-02-06 14:40:13 -0800560
Colin Cross4d9c2d12016-07-29 12:48:20 -0700561 return deps
562}
563
Colin Crossb916a382016-07-29 17:28:03 -0700564func (library *libraryDecorator) linkStatic(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700565 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700566
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700567 library.objects = deps.WholeStaticLibObjs.Copy()
568 library.objects = library.objects.Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700569
Colin Cross86803cf2018-02-15 14:12:26 -0800570 fileName := ctx.ModuleName() + library.MutatedProperties.VariantName + staticLibraryExtension
571 outputFile := android.PathForModuleOut(ctx, fileName)
Dan Willemsen581341d2017-02-09 16:16:31 -0800572 builderFlags := flagsToBuilderFlags(flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700573
Dan Willemsen569edc52018-11-19 09:33:29 -0800574 if Bool(library.baseLinker.Properties.Use_version_lib) {
575 if ctx.Host() {
576 versionedOutputFile := outputFile
577 outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
578 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
579 } else {
580 versionedOutputFile := android.PathForModuleOut(ctx, "versioned", fileName)
581 library.distFile = android.OptionalPathForPath(versionedOutputFile)
582 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
583 }
Colin Cross86803cf2018-02-15 14:12:26 -0800584 }
585
Dan Willemsen581341d2017-02-09 16:16:31 -0800586 TransformObjToStaticLib(ctx, library.objects.objFiles, builderFlags, outputFile, objs.tidyFiles)
587
588 library.coverageOutputFile = TransformCoverageFilesToLib(ctx, library.objects, builderFlags,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800589 ctx.ModuleName()+library.MutatedProperties.VariantName)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700590
591 library.wholeStaticMissingDeps = ctx.GetMissingDependencies()
592
593 ctx.CheckbuildFile(outputFile)
594
595 return outputFile
596}
597
Colin Crossb916a382016-07-29 17:28:03 -0700598func (library *libraryDecorator) linkShared(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700599 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700600
601 var linkerDeps android.Paths
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700602 linkerDeps = append(linkerDeps, flags.LdFlagsDeps...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700603
Colin Cross2383f3b2018-02-06 14:40:13 -0800604 unexportedSymbols := ctx.ExpandOptionalSource(library.Properties.Unexported_symbols_list, "unexported_symbols_list")
605 forceNotWeakSymbols := ctx.ExpandOptionalSource(library.Properties.Force_symbols_not_weak_list, "force_symbols_not_weak_list")
606 forceWeakSymbols := ctx.ExpandOptionalSource(library.Properties.Force_symbols_weak_list, "force_symbols_weak_list")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700607 if !ctx.Darwin() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700608 if unexportedSymbols.Valid() {
609 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
610 }
611 if forceNotWeakSymbols.Valid() {
612 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
613 }
614 if forceWeakSymbols.Valid() {
615 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
616 }
617 } else {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700618 if unexportedSymbols.Valid() {
619 flags.LdFlags = append(flags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
620 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
621 }
622 if forceNotWeakSymbols.Valid() {
623 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
624 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
625 }
626 if forceWeakSymbols.Valid() {
627 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
628 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
629 }
630 }
631
632 fileName := library.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
633 outputFile := android.PathForModuleOut(ctx, fileName)
634 ret := outputFile
635
636 builderFlags := flagsToBuilderFlags(flags)
637
Colin Crossb496cfd2018-09-10 16:50:05 -0700638 // Optimize out relinking against shared libraries whose interface hasn't changed by
639 // depending on a table of contents file instead of the library itself.
640 tocPath := outputFile.RelPathString()
641 tocPath = pathtools.ReplaceExtension(tocPath, flags.Toolchain.ShlibSuffix()[1:]+".toc")
642 tocFile := android.PathForOutput(ctx, tocPath)
643 library.tocFile = android.OptionalPathForPath(tocFile)
644 TransformSharedObjectToToc(ctx, outputFile, tocFile, builderFlags)
Colin Cross89562dc2016-10-03 17:47:19 -0700645
Colin Cross4d9c2d12016-07-29 12:48:20 -0700646 if library.stripper.needsStrip(ctx) {
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -0700647 // b/80093681, GNU strip/objcopy bug.
648 // Use llvm-{strip,objcopy} when clang lld is used.
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700649 builderFlags.stripUseLlvmStrip = library.baseLinker.useClangLld(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700650 strippedOutputFile := outputFile
651 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
652 library.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
653 }
654
Colin Crossb60190a2018-09-04 16:28:17 -0700655 library.unstrippedOutputFile = outputFile
656
Dan Willemsen569edc52018-11-19 09:33:29 -0800657 if Bool(library.baseLinker.Properties.Use_version_lib) {
658 if ctx.Host() {
659 versionedOutputFile := outputFile
660 outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
661 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
662 } else {
663 versionedOutputFile := android.PathForModuleOut(ctx, "versioned", fileName)
664 library.distFile = android.OptionalPathForPath(versionedOutputFile)
665
666 if library.stripper.needsStrip(ctx) {
667 out := android.PathForModuleOut(ctx, "versioned-stripped", fileName)
668 library.distFile = android.OptionalPathForPath(out)
669 library.stripper.strip(ctx, versionedOutputFile, out, builderFlags)
670 }
671
672 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
673 }
Colin Cross86803cf2018-02-15 14:12:26 -0800674 }
675
Colin Cross4d9c2d12016-07-29 12:48:20 -0700676 sharedLibs := deps.SharedLibs
677 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
678
Colin Cross26c34ed2016-09-30 17:10:16 -0700679 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
680 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700681 linkerDeps = append(linkerDeps, objs.tidyFiles...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700682
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700683 TransformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700684 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
685 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile)
686
Dan Willemsen581341d2017-02-09 16:16:31 -0800687 objs.coverageFiles = append(objs.coverageFiles, deps.StaticLibObjs.coverageFiles...)
688 objs.coverageFiles = append(objs.coverageFiles, deps.WholeStaticLibObjs.coverageFiles...)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800689
690 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.StaticLibObjs.sAbiDumpFiles...)
691 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.WholeStaticLibObjs.sAbiDumpFiles...)
692
Dan Willemsen581341d2017-02-09 16:16:31 -0800693 library.coverageOutputFile = TransformCoverageFilesToLib(ctx, objs, builderFlags, library.getLibName(ctx))
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700694 library.linkSAbiDumpFiles(ctx, objs, fileName, ret)
Dan Willemsen581341d2017-02-09 16:16:31 -0800695
Colin Cross4d9c2d12016-07-29 12:48:20 -0700696 return ret
697}
698
Logan Chien7eefdc42018-07-11 18:10:41 +0800699func getRefAbiDumpFile(ctx ModuleContext, vndkVersion, fileName string) android.Path {
Logan Chienf4b79c62018-08-02 02:27:02 +0800700 isLlndk := inList(ctx.baseModuleName(), llndkLibraries) || inList(ctx.baseModuleName(), ndkMigratedLibs)
Logan Chien7eefdc42018-07-11 18:10:41 +0800701
702 refAbiDumpTextFile := android.PathForVndkRefAbiDump(ctx, vndkVersion, fileName, isLlndk, false)
703 refAbiDumpGzipFile := android.PathForVndkRefAbiDump(ctx, vndkVersion, fileName, isLlndk, true)
704
705 if refAbiDumpTextFile.Valid() {
706 if refAbiDumpGzipFile.Valid() {
707 ctx.ModuleErrorf(
708 "Two reference ABI dump files are found: %q and %q. Please delete the stale one.",
709 refAbiDumpTextFile, refAbiDumpGzipFile)
710 return nil
711 }
712 return refAbiDumpTextFile.Path()
713 }
714 if refAbiDumpGzipFile.Valid() {
715 return UnzipRefDump(ctx, refAbiDumpGzipFile.Path(), fileName)
716 }
717 return nil
718}
719
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700720func (library *libraryDecorator) linkSAbiDumpFiles(ctx ModuleContext, objs Objects, fileName string, soFile android.Path) {
Logan Chien2f2b8902018-07-10 15:01:19 +0800721 if len(objs.sAbiDumpFiles) > 0 && ctx.shouldCreateVndkSourceAbiDump() {
Logan Chiena8f51582018-03-21 11:53:48 +0800722 vndkVersion := ctx.DeviceConfig().PlatformVndkVersion()
723 if ver := ctx.DeviceConfig().VndkVersion(); ver != "" && ver != "current" {
Logan Chienf3511742017-10-31 18:04:35 +0800724 vndkVersion = ver
725 }
726
Jayant Chowdharya4fce192017-09-06 13:10:03 -0700727 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800728 var SourceAbiFlags []string
729 for _, dir := range exportIncludeDirs.Strings() {
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700730 SourceAbiFlags = append(SourceAbiFlags, "-I"+dir)
731 }
732 for _, reexportedInclude := range extractExportIncludesFromFlags(library.sabi.Properties.ReexportedIncludeFlags) {
733 SourceAbiFlags = append(SourceAbiFlags, reexportedInclude)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800734 }
735 exportedHeaderFlags := strings.Join(SourceAbiFlags, " ")
Jayant Chowdharydf344d52018-01-17 11:11:42 -0800736 library.sAbiOutputFile = TransformDumpToLinkedDump(ctx, objs.sAbiDumpFiles, soFile, fileName, exportedHeaderFlags)
Logan Chien2f2b8902018-07-10 15:01:19 +0800737
Logan Chien7eefdc42018-07-11 18:10:41 +0800738 refAbiDumpFile := getRefAbiDumpFile(ctx, vndkVersion, fileName)
739 if refAbiDumpFile != nil {
Logan Chienf3511742017-10-31 18:04:35 +0800740 library.sAbiDiff = SourceAbiDiff(ctx, library.sAbiOutputFile.Path(),
Logan Chien7eefdc42018-07-11 18:10:41 +0800741 refAbiDumpFile, fileName, exportedHeaderFlags, ctx.isVndkExt())
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800742 }
743 }
744}
745
Colin Crossb916a382016-07-29 17:28:03 -0700746func (library *libraryDecorator) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700747 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700748
Colin Crossad59e752017-11-16 14:29:11 -0800749 objs = deps.Objs.Copy().Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700750 var out android.Path
Colin Crossa48ab5b2017-02-14 15:28:44 -0800751 if library.static() || library.header() {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700752 out = library.linkStatic(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700753 } else {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700754 out = library.linkShared(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700755 }
756
757 library.exportIncludes(ctx, "-I")
758 library.reexportFlags(deps.ReexportedFlags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700759 library.reexportDeps(deps.ReexportedFlagsDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700760
Nan Zhang0007d812017-11-07 10:57:05 -0800761 if Bool(library.Properties.Aidl.Export_aidl_headers) {
Dan Willemsene1240db2016-11-03 14:28:51 -0700762 if library.baseCompiler.hasSrcExt(".aidl") {
Colin Cross10d22312017-05-03 11:01:58 -0700763 flags := []string{
Dan Willemsene1240db2016-11-03 14:28:51 -0700764 "-I" + android.PathForModuleGen(ctx, "aidl").String(),
Colin Cross10d22312017-05-03 11:01:58 -0700765 }
766 library.reexportFlags(flags)
767 library.reuseExportedFlags = append(library.reuseExportedFlags, flags...)
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800768 library.reexportDeps(library.baseCompiler.pathDeps) // TODO: restrict to aidl deps
769 library.reuseExportedDeps = append(library.reuseExportedDeps, library.baseCompiler.pathDeps...)
Dan Willemsene1240db2016-11-03 14:28:51 -0700770 }
771 }
772
Nan Zhang0007d812017-11-07 10:57:05 -0800773 if Bool(library.Properties.Proto.Export_proto_headers) {
Dan Willemsene1240db2016-11-03 14:28:51 -0700774 if library.baseCompiler.hasSrcExt(".proto") {
Dan Willemsenab9f4262018-02-14 13:58:34 -0800775 includes := []string{}
776 if flags.ProtoRoot {
777 includes = append(includes, "-I"+android.ProtoSubDir(ctx).String())
Colin Cross10d22312017-05-03 11:01:58 -0700778 }
Dan Willemsenab9f4262018-02-14 13:58:34 -0800779 includes = append(includes, "-I"+android.ProtoDir(ctx).String())
780 library.reexportFlags(includes)
781 library.reuseExportedFlags = append(library.reuseExportedFlags, includes...)
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800782 library.reexportDeps(library.baseCompiler.pathDeps) // TODO: restrict to proto deps
783 library.reuseExportedDeps = append(library.reuseExportedDeps, library.baseCompiler.pathDeps...)
Colin Cross0c461f12016-10-20 16:11:43 -0700784 }
785 }
786
Inseob Kim21f26902018-09-06 00:55:20 +0900787 if library.baseCompiler.hasSrcExt(".sysprop") {
788 flags := []string{
789 "-I" + android.PathForModuleGen(ctx, "sysprop", "include").String(),
790 }
791 library.reexportFlags(flags)
792 library.reuseExportedFlags = append(library.reuseExportedFlags, flags...)
793 }
794
Jiyong Parkda732bd2018-11-02 18:23:15 +0900795 if library.buildStubs() {
796 library.reexportFlags([]string{"-D" + versioningMacroName(ctx.ModuleName()) + "=" + library.stubsVersion()})
797 }
798
Colin Cross4d9c2d12016-07-29 12:48:20 -0700799 return out
800}
801
Colin Crossb916a382016-07-29 17:28:03 -0700802func (library *libraryDecorator) buildStatic() bool {
Colin Cross38b40df2018-04-10 16:14:46 -0700803 return library.MutatedProperties.BuildStatic && BoolDefault(library.Properties.Static.Enabled, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700804}
805
Colin Crossb916a382016-07-29 17:28:03 -0700806func (library *libraryDecorator) buildShared() bool {
Colin Cross38b40df2018-04-10 16:14:46 -0700807 return library.MutatedProperties.BuildShared && BoolDefault(library.Properties.Shared.Enabled, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700808}
809
Colin Crossb916a382016-07-29 17:28:03 -0700810func (library *libraryDecorator) getWholeStaticMissingDeps() []string {
Colin Crossd2343a32018-04-30 14:50:01 -0700811 return append([]string(nil), library.wholeStaticMissingDeps...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700812}
813
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700814func (library *libraryDecorator) objs() Objects {
815 return library.objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700816}
817
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700818func (library *libraryDecorator) reuseObjs() (Objects, []string, android.Paths) {
819 return library.reuseObjects, library.reuseExportedFlags, library.reuseExportedDeps
Colin Cross4d9c2d12016-07-29 12:48:20 -0700820}
821
Colin Cross26c34ed2016-09-30 17:10:16 -0700822func (library *libraryDecorator) toc() android.OptionalPath {
823 return library.tocFile
824}
825
Colin Crossb916a382016-07-29 17:28:03 -0700826func (library *libraryDecorator) install(ctx ModuleContext, file android.Path) {
Colin Crossc43ae772017-04-14 15:42:53 -0700827 if library.shared() {
Justin Yun8fe12122017-12-07 17:18:15 +0900828 if ctx.Device() && ctx.useVndk() {
829 if ctx.isVndkSp() {
830 library.baseInstaller.subDir = "vndk-sp"
831 } else if ctx.isVndk() {
832 library.baseInstaller.subDir = "vndk"
833 }
Logan Chienf3511742017-10-31 18:04:35 +0800834
835 // Append a version to vndk or vndk-sp directories on the system partition.
836 if ctx.isVndk() && !ctx.isVndkExt() {
837 vndkVersion := ctx.DeviceConfig().PlatformVndkVersion()
838 if vndkVersion != "current" && vndkVersion != "" {
839 library.baseInstaller.subDir += "-" + vndkVersion
840 }
Justin Yun8effde42017-06-23 19:24:43 +0900841 }
842 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700843 library.baseInstaller.install(ctx, file)
844 }
Dan Albertf563d252017-10-13 00:29:00 -0700845
Dan Albert281f22b2017-12-13 15:03:47 -0800846 if Bool(library.Properties.Static_ndk_lib) && library.static() &&
Jiyong Parkf9332f12018-02-01 00:54:12 +0900847 !ctx.useVndk() && !ctx.inRecovery() && ctx.Device() &&
Jiyong Park7ed9de32018-10-15 22:25:07 +0900848 library.baseLinker.sanitize.isUnsanitizedVariant() &&
849 !library.buildStubs() {
Dan Albertf563d252017-10-13 00:29:00 -0700850 installPath := getNdkSysrootBase(ctx).Join(
Dan Albertea4b7b92018-04-25 16:05:30 -0700851 ctx, "usr/lib", config.NDKTriple(ctx.toolchain()), file.Base())
Dan Albertf563d252017-10-13 00:29:00 -0700852
853 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
854 Rule: android.Cp,
855 Description: "install " + installPath.Base(),
856 Output: installPath,
857 Input: file,
858 })
859
Colin Cross0875c522017-11-28 17:34:01 -0800860 library.ndkSysrootPath = installPath
Dan Albertf563d252017-10-13 00:29:00 -0700861 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700862}
863
Colin Crossb916a382016-07-29 17:28:03 -0700864func (library *libraryDecorator) static() bool {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800865 return library.MutatedProperties.VariantIsStatic
Colin Cross4d9c2d12016-07-29 12:48:20 -0700866}
867
Colin Crossa48ab5b2017-02-14 15:28:44 -0800868func (library *libraryDecorator) shared() bool {
869 return library.MutatedProperties.VariantIsShared
870}
871
872func (library *libraryDecorator) header() bool {
873 return !library.static() && !library.shared()
874}
875
876func (library *libraryDecorator) setStatic() {
877 library.MutatedProperties.VariantIsStatic = true
878 library.MutatedProperties.VariantIsShared = false
879}
880
881func (library *libraryDecorator) setShared() {
882 library.MutatedProperties.VariantIsStatic = false
883 library.MutatedProperties.VariantIsShared = true
Colin Crossb916a382016-07-29 17:28:03 -0700884}
885
Colin Crossab3b7322016-12-09 14:46:15 -0800886func (library *libraryDecorator) BuildOnlyStatic() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800887 library.MutatedProperties.BuildShared = false
Colin Crossab3b7322016-12-09 14:46:15 -0800888}
889
890func (library *libraryDecorator) BuildOnlyShared() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800891 library.MutatedProperties.BuildStatic = false
Colin Crossab3b7322016-12-09 14:46:15 -0800892}
893
Colin Cross5950f382016-12-13 12:50:57 -0800894func (library *libraryDecorator) HeaderOnly() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800895 library.MutatedProperties.BuildShared = false
896 library.MutatedProperties.BuildStatic = false
Colin Cross5950f382016-12-13 12:50:57 -0800897}
898
Jiyong Park7ed9de32018-10-15 22:25:07 +0900899func (library *libraryDecorator) buildStubs() bool {
900 return library.MutatedProperties.BuildStubs
901}
902
903func (library *libraryDecorator) stubsVersion() string {
904 return library.MutatedProperties.StubsVersion
905}
906
Jiyong Parkda732bd2018-11-02 18:23:15 +0900907func versioningMacroNamesList(config android.Config) *map[string]string {
908 return config.Once("versioningMacroNamesList", func() interface{} {
909 m := make(map[string]string)
910 return &m
911 }).(*map[string]string)
912}
913
914// alphanumeric and _ characters are preserved.
915// other characters are all converted to _
916var charsNotForMacro = regexp.MustCompile("[^a-zA-Z0-9_]+")
917
918func versioningMacroName(moduleName string) string {
919 macroName := charsNotForMacro.ReplaceAllString(moduleName, "_")
920 macroName = strings.ToUpper(moduleName)
921 return "__" + macroName + "_API__"
922}
923
Colin Crossab3b7322016-12-09 14:46:15 -0800924func NewLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700925 module := newModule(hod, android.MultilibBoth)
926
Colin Crossb916a382016-07-29 17:28:03 -0700927 library := &libraryDecorator{
Colin Crossa48ab5b2017-02-14 15:28:44 -0800928 MutatedProperties: LibraryMutatedProperties{
Colin Crossab3b7322016-12-09 14:46:15 -0800929 BuildShared: true,
930 BuildStatic: true,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700931 },
Colin Crossb916a382016-07-29 17:28:03 -0700932 baseCompiler: NewBaseCompiler(),
Dan Albert61f32122018-07-26 14:00:24 -0700933 baseLinker: NewBaseLinker(module.sanitize),
Colin Crossb916a382016-07-29 17:28:03 -0700934 baseInstaller: NewBaseInstaller("lib", "lib64", InstallInSystem),
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800935 sabi: module.sabi,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700936 }
937
Colin Crossb916a382016-07-29 17:28:03 -0700938 module.compiler = library
939 module.linker = library
940 module.installer = library
941
942 return module, library
943}
944
Colin Cross10d22312017-05-03 11:01:58 -0700945// connects a shared library to a static library in order to reuse its .o files to avoid
946// compiling source files twice.
947func reuseStaticLibrary(mctx android.BottomUpMutatorContext, static, shared *Module) {
948 if staticCompiler, ok := static.compiler.(*libraryDecorator); ok {
949 sharedCompiler := shared.compiler.(*libraryDecorator)
Dan Willemsen3a26eef2018-12-03 15:25:46 -0800950
951 // Check libraries in addition to cflags, since libraries may be exporting different
952 // include directories.
Colin Cross10d22312017-05-03 11:01:58 -0700953 if len(staticCompiler.Properties.Static.Cflags) == 0 &&
Dan Willemsen3a26eef2018-12-03 15:25:46 -0800954 len(sharedCompiler.Properties.Shared.Cflags) == 0 &&
955 len(staticCompiler.Properties.Static.Whole_static_libs) == 0 &&
956 len(sharedCompiler.Properties.Shared.Whole_static_libs) == 0 &&
957 len(staticCompiler.Properties.Static.Static_libs) == 0 &&
958 len(sharedCompiler.Properties.Shared.Static_libs) == 0 &&
959 len(staticCompiler.Properties.Static.Shared_libs) == 0 &&
960 len(sharedCompiler.Properties.Shared.Shared_libs) == 0 &&
961 staticCompiler.Properties.Static.System_shared_libs == nil &&
962 sharedCompiler.Properties.Shared.System_shared_libs == nil {
Colin Cross10d22312017-05-03 11:01:58 -0700963
964 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
965 sharedCompiler.baseCompiler.Properties.OriginalSrcs =
966 sharedCompiler.baseCompiler.Properties.Srcs
967 sharedCompiler.baseCompiler.Properties.Srcs = nil
968 sharedCompiler.baseCompiler.Properties.Generated_sources = nil
969 }
970 }
971}
972
Colin Crosse40b4ea2018-10-02 22:25:58 -0700973func LinkageMutator(mctx android.BottomUpMutatorContext) {
Colin Crossb916a382016-07-29 17:28:03 -0700974 if m, ok := mctx.Module().(*Module); ok && m.linker != nil {
975 if library, ok := m.linker.(libraryInterface); ok {
976 var modules []blueprint.Module
977 if library.buildStatic() && library.buildShared() {
978 modules = mctx.CreateLocalVariations("static", "shared")
979 static := modules[0].(*Module)
980 shared := modules[1].(*Module)
981
Colin Crossa48ab5b2017-02-14 15:28:44 -0800982 static.linker.(libraryInterface).setStatic()
983 shared.linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700984
Colin Cross10d22312017-05-03 11:01:58 -0700985 reuseStaticLibrary(mctx, static, shared)
986
Colin Crossb916a382016-07-29 17:28:03 -0700987 } else if library.buildStatic() {
988 modules = mctx.CreateLocalVariations("static")
Colin Crossa48ab5b2017-02-14 15:28:44 -0800989 modules[0].(*Module).linker.(libraryInterface).setStatic()
Colin Crossb916a382016-07-29 17:28:03 -0700990 } else if library.buildShared() {
991 modules = mctx.CreateLocalVariations("shared")
Colin Crossa48ab5b2017-02-14 15:28:44 -0800992 modules[0].(*Module).linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700993 }
994 }
995 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700996}
Jiyong Park7ed9de32018-10-15 22:25:07 +0900997
Jiyong Park25fc6a92018-11-18 18:02:45 +0900998// maps a module name to the list of stubs versions available for the module
999func stubsVersionsFor(config android.Config) map[string][]string {
1000 return config.Once("stubVersions", func() interface{} {
1001 return make(map[string][]string)
1002 }).(map[string][]string)
1003}
1004
1005var stubsVersionsLock sync.Mutex
1006
1007func latestStubsVersionFor(config android.Config, name string) string {
1008 versions, ok := stubsVersionsFor(config)[name]
1009 if ok && len(versions) > 0 {
1010 // the versions are alreay sorted in ascending order
1011 return versions[len(versions)-1]
1012 }
1013 return ""
1014}
1015
Jiyong Park7ed9de32018-10-15 22:25:07 +09001016// Version mutator splits a module into the mandatory non-stubs variant
Jiyong Park25fc6a92018-11-18 18:02:45 +09001017// (which is unnamed) and zero or more stubs variants.
1018func VersionMutator(mctx android.BottomUpMutatorContext) {
Jiyong Park7ed9de32018-10-15 22:25:07 +09001019 if mctx.Os() != android.Android {
1020 return
1021 }
1022
1023 if m, ok := mctx.Module().(*Module); ok && !m.inRecovery() && m.linker != nil {
Jiyong Park25fc6a92018-11-18 18:02:45 +09001024 if library, ok := m.linker.(*libraryDecorator); ok && library.buildShared() &&
1025 len(library.Properties.Stubs.Versions) > 0 {
1026 versions := []string{}
Jiyong Park7ed9de32018-10-15 22:25:07 +09001027 for _, v := range library.Properties.Stubs.Versions {
Jiyong Park25fc6a92018-11-18 18:02:45 +09001028 if _, err := strconv.Atoi(v); err != nil {
1029 mctx.PropertyErrorf("versions", "%q is not a number", v)
1030 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09001031 versions = append(versions, v)
1032 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001033 sort.Slice(versions, func(i, j int) bool {
1034 left, _ := strconv.Atoi(versions[i])
1035 right, _ := strconv.Atoi(versions[j])
1036 return left < right
1037 })
1038
1039 // save the list of versions for later use
1040 copiedVersions := make([]string, len(versions))
1041 copy(copiedVersions, versions)
1042 stubsVersionsLock.Lock()
1043 defer stubsVersionsLock.Unlock()
1044 stubsVersionsFor(mctx.Config())[mctx.ModuleName()] = copiedVersions
1045
1046 // "" is for the non-stubs variant
1047 versions = append(versions, "")
1048
Jiyong Park7ed9de32018-10-15 22:25:07 +09001049 modules := mctx.CreateVariations(versions...)
1050 for i, m := range modules {
1051 l := m.(*Module).linker.(*libraryDecorator)
Jiyong Park25fc6a92018-11-18 18:02:45 +09001052 if versions[i] != "" {
1053 l.MutatedProperties.BuildStubs = true
1054 l.MutatedProperties.StubsVersion = versions[i]
1055 m.(*Module).Properties.HideFromMake = true
Jiyong Park090d9df2018-12-11 02:47:16 +09001056 m.(*Module).sanitize = nil
1057 m.(*Module).stl = nil
Jiyong Park7ed9de32018-10-15 22:25:07 +09001058 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09001059 }
1060 } else {
1061 mctx.CreateVariations("")
1062 }
1063 return
1064 }
1065 if genrule, ok := mctx.Module().(*genrule.Module); ok {
1066 if props, ok := genrule.Extra.(*GenruleExtraProperties); ok && !props.InRecovery {
1067 mctx.CreateVariations("")
1068 return
1069 }
1070 }
1071}