blob: 86cd5da965b3d7dd4bee8bc725b1aa34f5965dec [file] [log] [blame]
Colin Cross4d9c2d12016-07-29 12:48:20 -07001// Copyright 2016 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package cc
16
17import (
Jiyong Parkda732bd2018-11-02 18:23:15 +090018 "regexp"
Jiyong Park25fc6a92018-11-18 18:02:45 +090019 "sort"
20 "strconv"
Colin Cross4d9c2d12016-07-29 12:48:20 -070021 "strings"
Jiyong Parkda732bd2018-11-02 18:23:15 +090022 "sync"
Colin Cross4d9c2d12016-07-29 12:48:20 -070023
24 "github.com/google/blueprint"
Colin Cross26c34ed2016-09-30 17:10:16 -070025 "github.com/google/blueprint/pathtools"
Colin Cross4d9c2d12016-07-29 12:48:20 -070026
Colin Cross4d9c2d12016-07-29 12:48:20 -070027 "android/soong/android"
Dan Albertea4b7b92018-04-25 16:05:30 -070028 "android/soong/cc/config"
Jiyong Park7ed9de32018-10-15 22:25:07 +090029 "android/soong/genrule"
Colin Cross4d9c2d12016-07-29 12:48:20 -070030)
31
Colin Crossb916a382016-07-29 17:28:03 -070032type LibraryProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -070033 Static struct {
Colin Cross2f336352016-10-26 10:03:47 -070034 Srcs []string `android:"arch_variant"`
35 Cflags []string `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070036
Dan Willemsen3a26eef2018-12-03 15:25:46 -080037 Enabled *bool `android:"arch_variant"`
38 Whole_static_libs []string `android:"arch_variant"`
39 Static_libs []string `android:"arch_variant"`
40 Shared_libs []string `android:"arch_variant"`
41 System_shared_libs []string `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070042 } `android:"arch_variant"`
43 Shared struct {
Colin Cross2f336352016-10-26 10:03:47 -070044 Srcs []string `android:"arch_variant"`
45 Cflags []string `android:"arch_variant"`
Colin Crossb916a382016-07-29 17:28:03 -070046
Dan Willemsen3a26eef2018-12-03 15:25:46 -080047 Enabled *bool `android:"arch_variant"`
48 Whole_static_libs []string `android:"arch_variant"`
49 Static_libs []string `android:"arch_variant"`
50 Shared_libs []string `android:"arch_variant"`
51 System_shared_libs []string `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070052 } `android:"arch_variant"`
53
Colin Cross4d9c2d12016-07-29 12:48:20 -070054 // local file name to pass to the linker as -unexported_symbols_list
55 Unexported_symbols_list *string `android:"arch_variant"`
56 // local file name to pass to the linker as -force_symbols_not_weak_list
57 Force_symbols_not_weak_list *string `android:"arch_variant"`
58 // local file name to pass to the linker as -force_symbols_weak_list
59 Force_symbols_weak_list *string `android:"arch_variant"`
60
61 // rename host libraries to prevent overlap with system installed libraries
62 Unique_host_soname *bool
63
Dan Willemsene1240db2016-11-03 14:28:51 -070064 Aidl struct {
65 // export headers generated from .aidl sources
Nan Zhang0007d812017-11-07 10:57:05 -080066 Export_aidl_headers *bool
Dan Willemsene1240db2016-11-03 14:28:51 -070067 }
68
Colin Cross0c461f12016-10-20 16:11:43 -070069 Proto struct {
70 // export headers generated from .proto sources
Nan Zhang0007d812017-11-07 10:57:05 -080071 Export_proto_headers *bool
Colin Cross0c461f12016-10-20 16:11:43 -070072 }
Dan Albertf563d252017-10-13 00:29:00 -070073
Nan Zhang0007d812017-11-07 10:57:05 -080074 Static_ndk_lib *bool
Jiyong Park7ed9de32018-10-15 22:25:07 +090075
76 Stubs struct {
77 // Relative path to the symbol map. The symbol map provides the list of
78 // symbols that are exported for stubs variant of this library.
79 Symbol_file *string
80
81 // List versions to generate stubs libs for.
82 Versions []string
83 }
dimitryd95964a2018-11-07 13:43:34 +010084
85 // set the name of the output
86 Stem *string `android:"arch_variant"`
87
88 // Names of modules to be overridden. Listed modules can only be other shared libraries
89 // (in Make or Soong).
90 // This does not completely prevent installation of the overridden libraries, but if both
91 // binaries would be installed by default (in PRODUCT_PACKAGES) the other library will be removed
92 // from PRODUCT_PACKAGES.
93 Overrides []string
Colin Crossa48ab5b2017-02-14 15:28:44 -080094}
Colin Cross0c461f12016-10-20 16:11:43 -070095
Colin Crossa48ab5b2017-02-14 15:28:44 -080096type LibraryMutatedProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -070097 VariantName string `blueprint:"mutated"`
Colin Crossb916a382016-07-29 17:28:03 -070098
99 // Build a static variant
100 BuildStatic bool `blueprint:"mutated"`
101 // Build a shared variant
102 BuildShared bool `blueprint:"mutated"`
103 // This variant is shared
104 VariantIsShared bool `blueprint:"mutated"`
105 // This variant is static
106 VariantIsStatic bool `blueprint:"mutated"`
Jiyong Park7ed9de32018-10-15 22:25:07 +0900107
108 // This variant is a stubs lib
109 BuildStubs bool `blueprint:"mutated"`
110 // Version of the stubs lib
111 StubsVersion string `blueprint:"mutated"`
Colin Crossb916a382016-07-29 17:28:03 -0700112}
113
114type FlagExporterProperties struct {
115 // list of directories relative to the Blueprints file that will
Dan Willemsen273af7f2016-11-03 15:53:42 -0700116 // be added to the include path (using -I) for this module and any module that links
Colin Cross5d195602017-10-17 16:15:50 -0700117 // against this module. Directories listed in export_include_dirs do not need to be
118 // listed in local_include_dirs.
Colin Crossb916a382016-07-29 17:28:03 -0700119 Export_include_dirs []string `android:"arch_variant"`
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700120
121 Target struct {
122 Vendor struct {
123 // list of exported include directories, like
124 // export_include_dirs, that will be applied to the
125 // vendor variant of this library. This will overwrite
126 // any other declarations.
Steven Morelandb21df8f2018-01-05 14:42:54 -0800127 Override_export_include_dirs []string
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700128 }
129 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700130}
131
132func init() {
Steven Morelandf9e62162017-11-02 17:00:50 -0700133 android.RegisterModuleType("cc_library_static", LibraryStaticFactory)
134 android.RegisterModuleType("cc_library_shared", LibrarySharedFactory)
135 android.RegisterModuleType("cc_library", LibraryFactory)
136 android.RegisterModuleType("cc_library_host_static", LibraryHostStaticFactory)
137 android.RegisterModuleType("cc_library_host_shared", LibraryHostSharedFactory)
138 android.RegisterModuleType("cc_library_headers", LibraryHeaderFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700139}
140
141// Module factory for combined static + shared libraries, device by default but with possible host
142// support
Steven Morelandf9e62162017-11-02 17:00:50 -0700143func LibraryFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800144 module, _ := NewLibrary(android.HostAndDeviceSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700145 return module.Init()
146}
147
148// Module factory for static libraries
Steven Morelandf9e62162017-11-02 17:00:50 -0700149func LibraryStaticFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800150 module, library := NewLibrary(android.HostAndDeviceSupported)
151 library.BuildOnlyStatic()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700152 return module.Init()
153}
154
155// Module factory for shared libraries
Steven Morelandf9e62162017-11-02 17:00:50 -0700156func LibrarySharedFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800157 module, library := NewLibrary(android.HostAndDeviceSupported)
158 library.BuildOnlyShared()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700159 return module.Init()
160}
161
162// Module factory for host static libraries
Steven Morelandf9e62162017-11-02 17:00:50 -0700163func LibraryHostStaticFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800164 module, library := NewLibrary(android.HostSupported)
165 library.BuildOnlyStatic()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700166 return module.Init()
167}
168
169// Module factory for host shared libraries
Steven Morelandf9e62162017-11-02 17:00:50 -0700170func LibraryHostSharedFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800171 module, library := NewLibrary(android.HostSupported)
172 library.BuildOnlyShared()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700173 return module.Init()
174}
175
Colin Cross5950f382016-12-13 12:50:57 -0800176// Module factory for header-only libraries
Steven Morelandf9e62162017-11-02 17:00:50 -0700177func LibraryHeaderFactory() android.Module {
Colin Cross5950f382016-12-13 12:50:57 -0800178 module, library := NewLibrary(android.HostAndDeviceSupported)
179 library.HeaderOnly()
180 return module.Init()
181}
182
Colin Cross4d9c2d12016-07-29 12:48:20 -0700183type flagExporter struct {
184 Properties FlagExporterProperties
185
Dan Willemsen847dcc72016-09-29 12:13:36 -0700186 flags []string
187 flagsDeps android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700188}
189
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700190func (f *flagExporter) exportedIncludes(ctx ModuleContext) android.Paths {
Steven Morelandb21df8f2018-01-05 14:42:54 -0800191 if ctx.useVndk() && f.Properties.Target.Vendor.Override_export_include_dirs != nil {
192 return android.PathsForModuleSrc(ctx, f.Properties.Target.Vendor.Override_export_include_dirs)
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700193 } else {
194 return android.PathsForModuleSrc(ctx, f.Properties.Export_include_dirs)
195 }
196}
197
Colin Cross4d9c2d12016-07-29 12:48:20 -0700198func (f *flagExporter) exportIncludes(ctx ModuleContext, inc string) {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700199 includeDirs := f.exportedIncludes(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700200 for _, dir := range includeDirs.Strings() {
201 f.flags = append(f.flags, inc+dir)
202 }
203}
204
205func (f *flagExporter) reexportFlags(flags []string) {
206 f.flags = append(f.flags, flags...)
207}
208
Dan Willemsen847dcc72016-09-29 12:13:36 -0700209func (f *flagExporter) reexportDeps(deps android.Paths) {
210 f.flagsDeps = append(f.flagsDeps, deps...)
211}
212
Colin Cross4d9c2d12016-07-29 12:48:20 -0700213func (f *flagExporter) exportedFlags() []string {
214 return f.flags
215}
216
Dan Willemsen847dcc72016-09-29 12:13:36 -0700217func (f *flagExporter) exportedFlagsDeps() android.Paths {
218 return f.flagsDeps
219}
220
Colin Cross4d9c2d12016-07-29 12:48:20 -0700221type exportedFlagsProducer interface {
222 exportedFlags() []string
Dan Willemsen847dcc72016-09-29 12:13:36 -0700223 exportedFlagsDeps() android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700224}
225
226var _ exportedFlagsProducer = (*flagExporter)(nil)
227
Colin Crossb916a382016-07-29 17:28:03 -0700228// libraryDecorator wraps baseCompiler, baseLinker and baseInstaller to provide library-specific
229// functionality: static vs. shared linkage, reusing object files for shared libraries
230type libraryDecorator struct {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800231 Properties LibraryProperties
232 MutatedProperties LibraryMutatedProperties
Colin Cross4d9c2d12016-07-29 12:48:20 -0700233
234 // For reusing static library objects for shared library
Colin Cross10d22312017-05-03 11:01:58 -0700235 reuseObjects Objects
236 reuseExportedFlags []string
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700237 reuseExportedDeps android.Paths
Colin Cross10d22312017-05-03 11:01:58 -0700238
Colin Cross26c34ed2016-09-30 17:10:16 -0700239 // table-of-contents file to optimize out relinking when possible
240 tocFile android.OptionalPath
Colin Cross4d9c2d12016-07-29 12:48:20 -0700241
Colin Cross4d9c2d12016-07-29 12:48:20 -0700242 flagExporter
243 stripper
244
Colin Cross4d9c2d12016-07-29 12:48:20 -0700245 // If we're used as a whole_static_lib, our missing dependencies need
246 // to be given
247 wholeStaticMissingDeps []string
248
249 // For whole_static_libs
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700250 objects Objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700251
252 // Uses the module's name if empty, but can be overridden. Does not include
253 // shlib suffix.
254 libName string
Colin Crossb916a382016-07-29 17:28:03 -0700255
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800256 sabi *sabi
257
Dan Willemsen581341d2017-02-09 16:16:31 -0800258 // Output archive of gcno coverage information files
259 coverageOutputFile android.OptionalPath
260
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800261 // linked Source Abi Dump
262 sAbiOutputFile android.OptionalPath
263
264 // Source Abi Diff
265 sAbiDiff android.OptionalPath
266
Colin Cross0875c522017-11-28 17:34:01 -0800267 // Location of the static library in the sysroot. Empty if the library is
268 // not included in the NDK.
269 ndkSysrootPath android.Path
270
Colin Crossb60190a2018-09-04 16:28:17 -0700271 // Location of the linked, unstripped library for shared libraries
272 unstrippedOutputFile android.Path
273
Dan Willemsen569edc52018-11-19 09:33:29 -0800274 // Location of the file that should be copied to dist dir when requested
275 distFile android.OptionalPath
276
Jiyong Park7ed9de32018-10-15 22:25:07 +0900277 versionScriptPath android.ModuleGenPath
278
Colin Crossb916a382016-07-29 17:28:03 -0700279 // Decorated interafaces
280 *baseCompiler
281 *baseLinker
282 *baseInstaller
Colin Cross4d9c2d12016-07-29 12:48:20 -0700283}
284
Colin Crossb916a382016-07-29 17:28:03 -0700285func (library *libraryDecorator) linkerProps() []interface{} {
286 var props []interface{}
287 props = append(props, library.baseLinker.linkerProps()...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700288 return append(props,
289 &library.Properties,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800290 &library.MutatedProperties,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700291 &library.flagExporter.Properties,
Colin Cross22f37952018-09-05 10:43:13 -0700292 &library.stripper.StripProperties)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700293}
294
Colin Crossb916a382016-07-29 17:28:03 -0700295func (library *libraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross42742b82016-08-01 13:20:05 -0700296 flags = library.baseLinker.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700297
Colin Crossb916a382016-07-29 17:28:03 -0700298 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
299 // all code is position independent, and then those warnings get promoted to
300 // errors.
Colin Cross3edeee12017-04-04 12:59:48 -0700301 if !ctx.Windows() {
Colin Crossb916a382016-07-29 17:28:03 -0700302 flags.CFlags = append(flags.CFlags, "-fPIC")
303 }
304
305 if library.static() {
306 flags.CFlags = append(flags.CFlags, library.Properties.Static.Cflags...)
Colin Crossa48ab5b2017-02-14 15:28:44 -0800307 } else if library.shared() {
Colin Crossb916a382016-07-29 17:28:03 -0700308 flags.CFlags = append(flags.CFlags, library.Properties.Shared.Cflags...)
309 }
310
Colin Crossa48ab5b2017-02-14 15:28:44 -0800311 if library.shared() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700312 libName := library.getLibName(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700313 var f []string
Dan Willemsen01a405a2016-06-13 17:19:03 -0700314 if ctx.toolchain().Bionic() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700315 f = append(f,
316 "-nostdlib",
317 "-Wl,--gc-sections",
318 )
319 }
320
321 if ctx.Darwin() {
322 f = append(f,
323 "-dynamiclib",
324 "-single_module",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700325 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
326 )
Colin Cross7863cf52016-10-20 10:47:21 -0700327 if ctx.Arch().ArchType == android.X86 {
328 f = append(f,
329 "-read_only_relocs suppress",
330 )
331 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700332 } else {
333 f = append(f,
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700334 "-shared",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700335 "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix())
336 }
337
338 flags.LdFlags = append(f, flags.LdFlags...)
339 }
340
341 return flags
342}
343
Colin Crossf18e1102017-11-16 14:33:08 -0800344func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700345 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700346 if len(exportIncludeDirs) > 0 {
Colin Crossdad8c952017-04-26 14:55:27 -0700347 f := includeDirsToFlags(exportIncludeDirs)
348 flags.GlobalFlags = append(flags.GlobalFlags, f)
349 flags.YasmFlags = append(flags.YasmFlags, f)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700350 }
351
Jiyong Park7ed9de32018-10-15 22:25:07 +0900352 flags = library.baseCompiler.compilerFlags(ctx, flags, deps)
353 if library.buildStubs() {
Jiyong Park64379952018-12-13 18:37:29 +0900354 // Remove -include <file> when compiling stubs. Otherwise, the force included
355 // headers might cause conflicting types error with the symbols in the
356 // generated stubs source code. e.g.
357 // double acos(double); // in header
358 // void acos() {} // in the generated source code
359 removeInclude := func(flags []string) []string {
360 ret := flags[:0]
361 for _, f := range flags {
362 if strings.HasPrefix(f, "-include ") {
363 continue
364 }
365 ret = append(ret, f)
366 }
367 return ret
368 }
369 flags.GlobalFlags = removeInclude(flags.GlobalFlags)
370 flags.CFlags = removeInclude(flags.CFlags)
371
Jiyong Park7ed9de32018-10-15 22:25:07 +0900372 flags = addStubLibraryCompilerFlags(flags)
373 }
374 return flags
Dan Willemsen273af7f2016-11-03 15:53:42 -0700375}
376
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700377func extractExportIncludesFromFlags(flags []string) []string {
378 // This method is used in the generation of rules which produce
379 // abi-dumps for source files. Exported headers are needed to infer the
380 // abi exported by a library and filter out the rest of the abi dumped
381 // from a source. We extract the include flags exported by a library.
382 // This includes the flags exported which are re-exported from static
383 // library dependencies, exported header library dependencies and
Jayant Chowdharyaf6eb712017-08-23 16:08:29 -0700384 // generated header dependencies. -isystem headers are not included
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700385 // since for bionic libraries, abi-filtering is taken care of by version
386 // scripts.
387 var exportedIncludes []string
388 for _, flag := range flags {
389 if strings.HasPrefix(flag, "-I") {
390 exportedIncludes = append(exportedIncludes, flag)
391 }
392 }
393 return exportedIncludes
394}
395
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700396func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Jiyong Park7ed9de32018-10-15 22:25:07 +0900397 if library.buildStubs() {
Jiyong Park3fd0baf2018-12-07 16:25:39 +0900398 objs, versionScript := compileStubLibrary(ctx, flags, String(library.Properties.Stubs.Symbol_file), library.MutatedProperties.StubsVersion, "--apex")
Jiyong Park7ed9de32018-10-15 22:25:07 +0900399 library.versionScriptPath = versionScript
400 return objs
401 }
402
Colin Cross5950f382016-12-13 12:50:57 -0800403 if !library.buildShared() && !library.buildStatic() {
404 if len(library.baseCompiler.Properties.Srcs) > 0 {
405 ctx.PropertyErrorf("srcs", "cc_library_headers must not have any srcs")
406 }
407 if len(library.Properties.Static.Srcs) > 0 {
408 ctx.PropertyErrorf("static.srcs", "cc_library_headers must not have any srcs")
409 }
410 if len(library.Properties.Shared.Srcs) > 0 {
411 ctx.PropertyErrorf("shared.srcs", "cc_library_headers must not have any srcs")
412 }
413 return Objects{}
414 }
Logan Chien2f2b8902018-07-10 15:01:19 +0800415 if ctx.shouldCreateVndkSourceAbiDump() || library.sabi.Properties.CreateSAbiDumps {
Jayant Chowdharya4fce192017-09-06 13:10:03 -0700416 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800417 var SourceAbiFlags []string
418 for _, dir := range exportIncludeDirs.Strings() {
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700419 SourceAbiFlags = append(SourceAbiFlags, "-I"+dir)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800420 }
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700421 for _, reexportedInclude := range extractExportIncludesFromFlags(library.sabi.Properties.ReexportedIncludeFlags) {
422 SourceAbiFlags = append(SourceAbiFlags, reexportedInclude)
423 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800424 flags.SAbiFlags = SourceAbiFlags
425 total_length := len(library.baseCompiler.Properties.Srcs) + len(deps.GeneratedSources) + len(library.Properties.Shared.Srcs) +
426 len(library.Properties.Static.Srcs)
427 if total_length > 0 {
428 flags.SAbiDump = true
429 }
430 }
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700431 objs := library.baseCompiler.compile(ctx, flags, deps)
432 library.reuseObjects = objs
Colin Cross2f336352016-10-26 10:03:47 -0700433 buildFlags := flagsToBuilderFlags(flags)
Colin Crossb916a382016-07-29 17:28:03 -0700434
Colin Cross4d9c2d12016-07-29 12:48:20 -0700435 if library.static() {
dimitry0345ad82018-12-05 16:28:14 +0100436 srcs := ctx.ExpandSources(library.Properties.Static.Srcs, nil)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700437 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceStaticLibrary,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800438 srcs, library.baseCompiler.pathDeps, library.baseCompiler.cFlagsDeps))
Colin Crossa48ab5b2017-02-14 15:28:44 -0800439 } else if library.shared() {
dimitry0345ad82018-12-05 16:28:14 +0100440 srcs := ctx.ExpandSources(library.Properties.Shared.Srcs, nil)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700441 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceSharedLibrary,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800442 srcs, library.baseCompiler.pathDeps, library.baseCompiler.cFlagsDeps))
Colin Crossb916a382016-07-29 17:28:03 -0700443 }
444
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700445 return objs
Colin Crossb916a382016-07-29 17:28:03 -0700446}
447
448type libraryInterface interface {
449 getWholeStaticMissingDeps() []string
450 static() bool
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700451 objs() Objects
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700452 reuseObjs() (Objects, []string, android.Paths)
Colin Cross26c34ed2016-09-30 17:10:16 -0700453 toc() android.OptionalPath
Colin Crossb916a382016-07-29 17:28:03 -0700454
455 // Returns true if the build options for the module have selected a static or shared build
456 buildStatic() bool
457 buildShared() bool
458
459 // Sets whether a specific variant is static or shared
Colin Crossa48ab5b2017-02-14 15:28:44 -0800460 setStatic()
461 setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700462}
463
464func (library *libraryDecorator) getLibName(ctx ModuleContext) string {
465 name := library.libName
466 if name == "" {
dimitryd95964a2018-11-07 13:43:34 +0100467 name = String(library.Properties.Stem)
468 if name == "" {
469 name = ctx.baseModuleName()
470 }
Colin Crossb916a382016-07-29 17:28:03 -0700471 }
472
Logan Chienf3511742017-10-31 18:04:35 +0800473 if ctx.isVndkExt() {
474 name = ctx.getVndkExtendsModuleName()
475 }
476
Colin Crossb916a382016-07-29 17:28:03 -0700477 if ctx.Host() && Bool(library.Properties.Unique_host_soname) {
478 if !strings.HasSuffix(name, "-host") {
479 name = name + "-host"
480 }
481 }
482
Colin Crossa48ab5b2017-02-14 15:28:44 -0800483 return name + library.MutatedProperties.VariantName
Colin Crossb916a382016-07-29 17:28:03 -0700484}
485
Jiyong Parkda732bd2018-11-02 18:23:15 +0900486var versioningMacroNamesListMutex sync.Mutex
487
Colin Crossb916a382016-07-29 17:28:03 -0700488func (library *libraryDecorator) linkerInit(ctx BaseModuleContext) {
489 location := InstallInSystem
Dan Albert61f32122018-07-26 14:00:24 -0700490 if library.baseLinker.sanitize.inSanitizerDir() {
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700491 location = InstallInSanitizerDir
Colin Crossb916a382016-07-29 17:28:03 -0700492 }
493 library.baseInstaller.location = location
Colin Crossb916a382016-07-29 17:28:03 -0700494 library.baseLinker.linkerInit(ctx)
Jiyong Park7ed9de32018-10-15 22:25:07 +0900495 // Let baseLinker know whether this variant is for stubs or not, so that
496 // it can omit things that are not required for linking stubs.
497 library.baseLinker.dynamicProperties.BuildStubs = library.buildStubs()
Jiyong Parkda732bd2018-11-02 18:23:15 +0900498
499 if library.buildStubs() {
500 macroNames := versioningMacroNamesList(ctx.Config())
501 myName := versioningMacroName(ctx.ModuleName())
502 versioningMacroNamesListMutex.Lock()
503 defer versioningMacroNamesListMutex.Unlock()
504 if (*macroNames)[myName] == "" {
505 (*macroNames)[myName] = ctx.ModuleName()
506 } else if (*macroNames)[myName] != ctx.ModuleName() {
507 ctx.ModuleErrorf("Macro name %q for versioning conflicts with macro name from module %q ", myName, (*macroNames)[myName])
508 }
509 }
Colin Crossb916a382016-07-29 17:28:03 -0700510}
511
dimitry0345ad82018-12-05 16:28:14 +0100512func (library *libraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
513 deps = library.baseCompiler.compilerDeps(ctx, deps)
514
515 if library.static() {
516 android.ExtractSourcesDeps(ctx, library.Properties.Static.Srcs)
517 } else if library.shared() {
518 android.ExtractSourcesDeps(ctx, library.Properties.Shared.Srcs)
519 }
520
521 return deps
522}
523
Colin Cross37047f12016-12-13 17:06:13 -0800524func (library *libraryDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Dan Willemsen3a26eef2018-12-03 15:25:46 -0800525 if library.static() {
526 if library.Properties.Static.System_shared_libs != nil {
527 library.baseLinker.Properties.System_shared_libs = library.Properties.Static.System_shared_libs
528 }
529 } else if library.shared() {
530 if library.Properties.Shared.System_shared_libs != nil {
531 library.baseLinker.Properties.System_shared_libs = library.Properties.Shared.System_shared_libs
532 }
533 }
534
Colin Crossb916a382016-07-29 17:28:03 -0700535 deps = library.baseLinker.linkerDeps(ctx, deps)
536
537 if library.static() {
538 deps.WholeStaticLibs = append(deps.WholeStaticLibs,
539 library.Properties.Static.Whole_static_libs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700540 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Static.Static_libs...)
541 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Static.Shared_libs...)
Colin Crossa48ab5b2017-02-14 15:28:44 -0800542 } else if library.shared() {
Dan Willemsen2e47b342016-11-17 01:02:25 -0800543 if ctx.toolchain().Bionic() && !Bool(library.baseLinker.Properties.Nocrt) {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700544 if !ctx.useSdk() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700545 deps.CrtBegin = "crtbegin_so"
546 deps.CrtEnd = "crtend_so"
547 } else {
Dan Albertebedf672016-11-08 15:06:22 -0800548 // TODO(danalbert): Add generation of crt objects.
549 // For `sdk_version: "current"`, we don't actually have a
550 // freshly generated set of CRT objects. Use the last stable
551 // version.
552 version := ctx.sdkVersion()
553 if version == "current" {
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -0700554 version = getCurrentNdkPrebuiltVersion(ctx)
Dan Albertebedf672016-11-08 15:06:22 -0800555 }
556 deps.CrtBegin = "ndk_crtbegin_so." + version
557 deps.CrtEnd = "ndk_crtend_so." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700558 }
559 }
560 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Shared.Whole_static_libs...)
561 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Shared.Static_libs...)
562 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Shared.Shared_libs...)
563 }
Jiyong Park52d25bd2017-10-13 09:17:01 +0900564 if ctx.useVndk() {
565 deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs)
566 deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Vendor.Exclude_shared_libs)
567 deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs)
568 }
Jiyong Parkf9332f12018-02-01 00:54:12 +0900569 if ctx.inRecovery() {
570 deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Recovery.Exclude_static_libs)
571 deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Recovery.Exclude_shared_libs)
572 deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Recovery.Exclude_static_libs)
573 }
Colin Cross2383f3b2018-02-06 14:40:13 -0800574
Colin Cross2383f3b2018-02-06 14:40:13 -0800575 android.ExtractSourceDeps(ctx, library.Properties.Unexported_symbols_list)
576 android.ExtractSourceDeps(ctx, library.Properties.Force_symbols_not_weak_list)
577 android.ExtractSourceDeps(ctx, library.Properties.Force_symbols_weak_list)
Colin Cross2383f3b2018-02-06 14:40:13 -0800578
Colin Cross4d9c2d12016-07-29 12:48:20 -0700579 return deps
580}
581
Colin Crossb916a382016-07-29 17:28:03 -0700582func (library *libraryDecorator) linkStatic(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700583 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700584
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700585 library.objects = deps.WholeStaticLibObjs.Copy()
586 library.objects = library.objects.Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700587
Colin Cross86803cf2018-02-15 14:12:26 -0800588 fileName := ctx.ModuleName() + library.MutatedProperties.VariantName + staticLibraryExtension
589 outputFile := android.PathForModuleOut(ctx, fileName)
Dan Willemsen581341d2017-02-09 16:16:31 -0800590 builderFlags := flagsToBuilderFlags(flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700591
Dan Willemsen569edc52018-11-19 09:33:29 -0800592 if Bool(library.baseLinker.Properties.Use_version_lib) {
593 if ctx.Host() {
594 versionedOutputFile := outputFile
595 outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
596 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
597 } else {
598 versionedOutputFile := android.PathForModuleOut(ctx, "versioned", fileName)
599 library.distFile = android.OptionalPathForPath(versionedOutputFile)
600 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
601 }
Colin Cross86803cf2018-02-15 14:12:26 -0800602 }
603
Dan Willemsen581341d2017-02-09 16:16:31 -0800604 TransformObjToStaticLib(ctx, library.objects.objFiles, builderFlags, outputFile, objs.tidyFiles)
605
606 library.coverageOutputFile = TransformCoverageFilesToLib(ctx, library.objects, builderFlags,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800607 ctx.ModuleName()+library.MutatedProperties.VariantName)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700608
609 library.wholeStaticMissingDeps = ctx.GetMissingDependencies()
610
611 ctx.CheckbuildFile(outputFile)
612
613 return outputFile
614}
615
Colin Crossb916a382016-07-29 17:28:03 -0700616func (library *libraryDecorator) linkShared(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700617 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700618
619 var linkerDeps android.Paths
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700620 linkerDeps = append(linkerDeps, flags.LdFlagsDeps...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700621
Colin Cross2383f3b2018-02-06 14:40:13 -0800622 unexportedSymbols := ctx.ExpandOptionalSource(library.Properties.Unexported_symbols_list, "unexported_symbols_list")
623 forceNotWeakSymbols := ctx.ExpandOptionalSource(library.Properties.Force_symbols_not_weak_list, "force_symbols_not_weak_list")
624 forceWeakSymbols := ctx.ExpandOptionalSource(library.Properties.Force_symbols_weak_list, "force_symbols_weak_list")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700625 if !ctx.Darwin() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700626 if unexportedSymbols.Valid() {
627 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
628 }
629 if forceNotWeakSymbols.Valid() {
630 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
631 }
632 if forceWeakSymbols.Valid() {
633 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
634 }
635 } else {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700636 if unexportedSymbols.Valid() {
637 flags.LdFlags = append(flags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
638 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
639 }
640 if forceNotWeakSymbols.Valid() {
641 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
642 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
643 }
644 if forceWeakSymbols.Valid() {
645 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
646 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
647 }
648 }
649
650 fileName := library.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
651 outputFile := android.PathForModuleOut(ctx, fileName)
652 ret := outputFile
653
654 builderFlags := flagsToBuilderFlags(flags)
655
Colin Crossb496cfd2018-09-10 16:50:05 -0700656 // Optimize out relinking against shared libraries whose interface hasn't changed by
657 // depending on a table of contents file instead of the library itself.
658 tocPath := outputFile.RelPathString()
659 tocPath = pathtools.ReplaceExtension(tocPath, flags.Toolchain.ShlibSuffix()[1:]+".toc")
660 tocFile := android.PathForOutput(ctx, tocPath)
661 library.tocFile = android.OptionalPathForPath(tocFile)
662 TransformSharedObjectToToc(ctx, outputFile, tocFile, builderFlags)
Colin Cross89562dc2016-10-03 17:47:19 -0700663
Colin Cross4d9c2d12016-07-29 12:48:20 -0700664 if library.stripper.needsStrip(ctx) {
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -0700665 // b/80093681, GNU strip/objcopy bug.
666 // Use llvm-{strip,objcopy} when clang lld is used.
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700667 builderFlags.stripUseLlvmStrip = library.baseLinker.useClangLld(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700668 strippedOutputFile := outputFile
669 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
670 library.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
671 }
672
Colin Crossb60190a2018-09-04 16:28:17 -0700673 library.unstrippedOutputFile = outputFile
674
Dan Willemsen569edc52018-11-19 09:33:29 -0800675 if Bool(library.baseLinker.Properties.Use_version_lib) {
676 if ctx.Host() {
677 versionedOutputFile := outputFile
678 outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
679 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
680 } else {
681 versionedOutputFile := android.PathForModuleOut(ctx, "versioned", fileName)
682 library.distFile = android.OptionalPathForPath(versionedOutputFile)
683
684 if library.stripper.needsStrip(ctx) {
685 out := android.PathForModuleOut(ctx, "versioned-stripped", fileName)
686 library.distFile = android.OptionalPathForPath(out)
687 library.stripper.strip(ctx, versionedOutputFile, out, builderFlags)
688 }
689
690 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
691 }
Colin Cross86803cf2018-02-15 14:12:26 -0800692 }
693
Colin Cross4d9c2d12016-07-29 12:48:20 -0700694 sharedLibs := deps.SharedLibs
695 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
696
Colin Cross26c34ed2016-09-30 17:10:16 -0700697 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
698 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700699 linkerDeps = append(linkerDeps, objs.tidyFiles...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700700
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700701 TransformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700702 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
703 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile)
704
Dan Willemsen581341d2017-02-09 16:16:31 -0800705 objs.coverageFiles = append(objs.coverageFiles, deps.StaticLibObjs.coverageFiles...)
706 objs.coverageFiles = append(objs.coverageFiles, deps.WholeStaticLibObjs.coverageFiles...)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800707
708 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.StaticLibObjs.sAbiDumpFiles...)
709 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.WholeStaticLibObjs.sAbiDumpFiles...)
710
Dan Willemsen581341d2017-02-09 16:16:31 -0800711 library.coverageOutputFile = TransformCoverageFilesToLib(ctx, objs, builderFlags, library.getLibName(ctx))
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700712 library.linkSAbiDumpFiles(ctx, objs, fileName, ret)
Dan Willemsen581341d2017-02-09 16:16:31 -0800713
Colin Cross4d9c2d12016-07-29 12:48:20 -0700714 return ret
715}
716
Logan Chien7eefdc42018-07-11 18:10:41 +0800717func getRefAbiDumpFile(ctx ModuleContext, vndkVersion, fileName string) android.Path {
Logan Chienf4b79c62018-08-02 02:27:02 +0800718 isLlndk := inList(ctx.baseModuleName(), llndkLibraries) || inList(ctx.baseModuleName(), ndkMigratedLibs)
Logan Chien7eefdc42018-07-11 18:10:41 +0800719
720 refAbiDumpTextFile := android.PathForVndkRefAbiDump(ctx, vndkVersion, fileName, isLlndk, false)
721 refAbiDumpGzipFile := android.PathForVndkRefAbiDump(ctx, vndkVersion, fileName, isLlndk, true)
722
723 if refAbiDumpTextFile.Valid() {
724 if refAbiDumpGzipFile.Valid() {
725 ctx.ModuleErrorf(
726 "Two reference ABI dump files are found: %q and %q. Please delete the stale one.",
727 refAbiDumpTextFile, refAbiDumpGzipFile)
728 return nil
729 }
730 return refAbiDumpTextFile.Path()
731 }
732 if refAbiDumpGzipFile.Valid() {
733 return UnzipRefDump(ctx, refAbiDumpGzipFile.Path(), fileName)
734 }
735 return nil
736}
737
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700738func (library *libraryDecorator) linkSAbiDumpFiles(ctx ModuleContext, objs Objects, fileName string, soFile android.Path) {
Logan Chien2f2b8902018-07-10 15:01:19 +0800739 if len(objs.sAbiDumpFiles) > 0 && ctx.shouldCreateVndkSourceAbiDump() {
Logan Chiena8f51582018-03-21 11:53:48 +0800740 vndkVersion := ctx.DeviceConfig().PlatformVndkVersion()
741 if ver := ctx.DeviceConfig().VndkVersion(); ver != "" && ver != "current" {
Logan Chienf3511742017-10-31 18:04:35 +0800742 vndkVersion = ver
743 }
744
Jayant Chowdharya4fce192017-09-06 13:10:03 -0700745 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800746 var SourceAbiFlags []string
747 for _, dir := range exportIncludeDirs.Strings() {
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700748 SourceAbiFlags = append(SourceAbiFlags, "-I"+dir)
749 }
750 for _, reexportedInclude := range extractExportIncludesFromFlags(library.sabi.Properties.ReexportedIncludeFlags) {
751 SourceAbiFlags = append(SourceAbiFlags, reexportedInclude)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800752 }
753 exportedHeaderFlags := strings.Join(SourceAbiFlags, " ")
Jayant Chowdharydf344d52018-01-17 11:11:42 -0800754 library.sAbiOutputFile = TransformDumpToLinkedDump(ctx, objs.sAbiDumpFiles, soFile, fileName, exportedHeaderFlags)
Logan Chien2f2b8902018-07-10 15:01:19 +0800755
Logan Chien7eefdc42018-07-11 18:10:41 +0800756 refAbiDumpFile := getRefAbiDumpFile(ctx, vndkVersion, fileName)
757 if refAbiDumpFile != nil {
Logan Chienf3511742017-10-31 18:04:35 +0800758 library.sAbiDiff = SourceAbiDiff(ctx, library.sAbiOutputFile.Path(),
Logan Chien7eefdc42018-07-11 18:10:41 +0800759 refAbiDumpFile, fileName, exportedHeaderFlags, ctx.isVndkExt())
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800760 }
761 }
762}
763
Colin Crossb916a382016-07-29 17:28:03 -0700764func (library *libraryDecorator) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700765 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700766
Colin Crossad59e752017-11-16 14:29:11 -0800767 objs = deps.Objs.Copy().Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700768 var out android.Path
Colin Crossa48ab5b2017-02-14 15:28:44 -0800769 if library.static() || library.header() {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700770 out = library.linkStatic(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700771 } else {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700772 out = library.linkShared(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700773 }
774
775 library.exportIncludes(ctx, "-I")
776 library.reexportFlags(deps.ReexportedFlags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700777 library.reexportDeps(deps.ReexportedFlagsDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700778
Nan Zhang0007d812017-11-07 10:57:05 -0800779 if Bool(library.Properties.Aidl.Export_aidl_headers) {
Dan Willemsene1240db2016-11-03 14:28:51 -0700780 if library.baseCompiler.hasSrcExt(".aidl") {
Colin Cross10d22312017-05-03 11:01:58 -0700781 flags := []string{
Dan Willemsene1240db2016-11-03 14:28:51 -0700782 "-I" + android.PathForModuleGen(ctx, "aidl").String(),
Colin Cross10d22312017-05-03 11:01:58 -0700783 }
784 library.reexportFlags(flags)
785 library.reuseExportedFlags = append(library.reuseExportedFlags, flags...)
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800786 library.reexportDeps(library.baseCompiler.pathDeps) // TODO: restrict to aidl deps
787 library.reuseExportedDeps = append(library.reuseExportedDeps, library.baseCompiler.pathDeps...)
Dan Willemsene1240db2016-11-03 14:28:51 -0700788 }
789 }
790
Nan Zhang0007d812017-11-07 10:57:05 -0800791 if Bool(library.Properties.Proto.Export_proto_headers) {
Dan Willemsene1240db2016-11-03 14:28:51 -0700792 if library.baseCompiler.hasSrcExt(".proto") {
Dan Willemsenab9f4262018-02-14 13:58:34 -0800793 includes := []string{}
794 if flags.ProtoRoot {
795 includes = append(includes, "-I"+android.ProtoSubDir(ctx).String())
Colin Cross10d22312017-05-03 11:01:58 -0700796 }
Dan Willemsenab9f4262018-02-14 13:58:34 -0800797 includes = append(includes, "-I"+android.ProtoDir(ctx).String())
798 library.reexportFlags(includes)
799 library.reuseExportedFlags = append(library.reuseExportedFlags, includes...)
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800800 library.reexportDeps(library.baseCompiler.pathDeps) // TODO: restrict to proto deps
801 library.reuseExportedDeps = append(library.reuseExportedDeps, library.baseCompiler.pathDeps...)
Colin Cross0c461f12016-10-20 16:11:43 -0700802 }
803 }
804
Inseob Kim21f26902018-09-06 00:55:20 +0900805 if library.baseCompiler.hasSrcExt(".sysprop") {
806 flags := []string{
807 "-I" + android.PathForModuleGen(ctx, "sysprop", "include").String(),
808 }
809 library.reexportFlags(flags)
810 library.reuseExportedFlags = append(library.reuseExportedFlags, flags...)
811 }
812
Jiyong Parkda732bd2018-11-02 18:23:15 +0900813 if library.buildStubs() {
814 library.reexportFlags([]string{"-D" + versioningMacroName(ctx.ModuleName()) + "=" + library.stubsVersion()})
815 }
816
Colin Cross4d9c2d12016-07-29 12:48:20 -0700817 return out
818}
819
Colin Crossb916a382016-07-29 17:28:03 -0700820func (library *libraryDecorator) buildStatic() bool {
Colin Cross38b40df2018-04-10 16:14:46 -0700821 return library.MutatedProperties.BuildStatic && BoolDefault(library.Properties.Static.Enabled, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700822}
823
Colin Crossb916a382016-07-29 17:28:03 -0700824func (library *libraryDecorator) buildShared() bool {
Colin Cross38b40df2018-04-10 16:14:46 -0700825 return library.MutatedProperties.BuildShared && BoolDefault(library.Properties.Shared.Enabled, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700826}
827
Colin Crossb916a382016-07-29 17:28:03 -0700828func (library *libraryDecorator) getWholeStaticMissingDeps() []string {
Colin Crossd2343a32018-04-30 14:50:01 -0700829 return append([]string(nil), library.wholeStaticMissingDeps...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700830}
831
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700832func (library *libraryDecorator) objs() Objects {
833 return library.objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700834}
835
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700836func (library *libraryDecorator) reuseObjs() (Objects, []string, android.Paths) {
837 return library.reuseObjects, library.reuseExportedFlags, library.reuseExportedDeps
Colin Cross4d9c2d12016-07-29 12:48:20 -0700838}
839
Colin Cross26c34ed2016-09-30 17:10:16 -0700840func (library *libraryDecorator) toc() android.OptionalPath {
841 return library.tocFile
842}
843
Colin Crossb916a382016-07-29 17:28:03 -0700844func (library *libraryDecorator) install(ctx ModuleContext, file android.Path) {
Colin Crossc43ae772017-04-14 15:42:53 -0700845 if library.shared() {
Justin Yun8fe12122017-12-07 17:18:15 +0900846 if ctx.Device() && ctx.useVndk() {
847 if ctx.isVndkSp() {
848 library.baseInstaller.subDir = "vndk-sp"
849 } else if ctx.isVndk() {
850 library.baseInstaller.subDir = "vndk"
851 }
Logan Chienf3511742017-10-31 18:04:35 +0800852
853 // Append a version to vndk or vndk-sp directories on the system partition.
854 if ctx.isVndk() && !ctx.isVndkExt() {
855 vndkVersion := ctx.DeviceConfig().PlatformVndkVersion()
856 if vndkVersion != "current" && vndkVersion != "" {
857 library.baseInstaller.subDir += "-" + vndkVersion
858 }
Justin Yun8effde42017-06-23 19:24:43 +0900859 }
860 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700861 library.baseInstaller.install(ctx, file)
862 }
Dan Albertf563d252017-10-13 00:29:00 -0700863
Dan Albert281f22b2017-12-13 15:03:47 -0800864 if Bool(library.Properties.Static_ndk_lib) && library.static() &&
Jiyong Parkf9332f12018-02-01 00:54:12 +0900865 !ctx.useVndk() && !ctx.inRecovery() && ctx.Device() &&
Jiyong Park7ed9de32018-10-15 22:25:07 +0900866 library.baseLinker.sanitize.isUnsanitizedVariant() &&
867 !library.buildStubs() {
Dan Albertf563d252017-10-13 00:29:00 -0700868 installPath := getNdkSysrootBase(ctx).Join(
Dan Albertea4b7b92018-04-25 16:05:30 -0700869 ctx, "usr/lib", config.NDKTriple(ctx.toolchain()), file.Base())
Dan Albertf563d252017-10-13 00:29:00 -0700870
871 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
872 Rule: android.Cp,
873 Description: "install " + installPath.Base(),
874 Output: installPath,
875 Input: file,
876 })
877
Colin Cross0875c522017-11-28 17:34:01 -0800878 library.ndkSysrootPath = installPath
Dan Albertf563d252017-10-13 00:29:00 -0700879 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700880}
881
Colin Crossb916a382016-07-29 17:28:03 -0700882func (library *libraryDecorator) static() bool {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800883 return library.MutatedProperties.VariantIsStatic
Colin Cross4d9c2d12016-07-29 12:48:20 -0700884}
885
Colin Crossa48ab5b2017-02-14 15:28:44 -0800886func (library *libraryDecorator) shared() bool {
887 return library.MutatedProperties.VariantIsShared
888}
889
890func (library *libraryDecorator) header() bool {
891 return !library.static() && !library.shared()
892}
893
894func (library *libraryDecorator) setStatic() {
895 library.MutatedProperties.VariantIsStatic = true
896 library.MutatedProperties.VariantIsShared = false
897}
898
899func (library *libraryDecorator) setShared() {
900 library.MutatedProperties.VariantIsStatic = false
901 library.MutatedProperties.VariantIsShared = true
Colin Crossb916a382016-07-29 17:28:03 -0700902}
903
Colin Crossab3b7322016-12-09 14:46:15 -0800904func (library *libraryDecorator) BuildOnlyStatic() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800905 library.MutatedProperties.BuildShared = false
Colin Crossab3b7322016-12-09 14:46:15 -0800906}
907
908func (library *libraryDecorator) BuildOnlyShared() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800909 library.MutatedProperties.BuildStatic = false
Colin Crossab3b7322016-12-09 14:46:15 -0800910}
911
Colin Cross5950f382016-12-13 12:50:57 -0800912func (library *libraryDecorator) HeaderOnly() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800913 library.MutatedProperties.BuildShared = false
914 library.MutatedProperties.BuildStatic = false
Colin Cross5950f382016-12-13 12:50:57 -0800915}
916
Jiyong Park7ed9de32018-10-15 22:25:07 +0900917func (library *libraryDecorator) buildStubs() bool {
918 return library.MutatedProperties.BuildStubs
919}
920
921func (library *libraryDecorator) stubsVersion() string {
922 return library.MutatedProperties.StubsVersion
923}
924
Jiyong Parkda732bd2018-11-02 18:23:15 +0900925func versioningMacroNamesList(config android.Config) *map[string]string {
926 return config.Once("versioningMacroNamesList", func() interface{} {
927 m := make(map[string]string)
928 return &m
929 }).(*map[string]string)
930}
931
932// alphanumeric and _ characters are preserved.
933// other characters are all converted to _
934var charsNotForMacro = regexp.MustCompile("[^a-zA-Z0-9_]+")
935
936func versioningMacroName(moduleName string) string {
937 macroName := charsNotForMacro.ReplaceAllString(moduleName, "_")
938 macroName = strings.ToUpper(moduleName)
939 return "__" + macroName + "_API__"
940}
941
Colin Crossab3b7322016-12-09 14:46:15 -0800942func NewLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700943 module := newModule(hod, android.MultilibBoth)
944
Colin Crossb916a382016-07-29 17:28:03 -0700945 library := &libraryDecorator{
Colin Crossa48ab5b2017-02-14 15:28:44 -0800946 MutatedProperties: LibraryMutatedProperties{
Colin Crossab3b7322016-12-09 14:46:15 -0800947 BuildShared: true,
948 BuildStatic: true,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700949 },
Colin Crossb916a382016-07-29 17:28:03 -0700950 baseCompiler: NewBaseCompiler(),
Dan Albert61f32122018-07-26 14:00:24 -0700951 baseLinker: NewBaseLinker(module.sanitize),
Colin Crossb916a382016-07-29 17:28:03 -0700952 baseInstaller: NewBaseInstaller("lib", "lib64", InstallInSystem),
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800953 sabi: module.sabi,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700954 }
955
Colin Crossb916a382016-07-29 17:28:03 -0700956 module.compiler = library
957 module.linker = library
958 module.installer = library
959
960 return module, library
961}
962
Colin Cross10d22312017-05-03 11:01:58 -0700963// connects a shared library to a static library in order to reuse its .o files to avoid
964// compiling source files twice.
965func reuseStaticLibrary(mctx android.BottomUpMutatorContext, static, shared *Module) {
966 if staticCompiler, ok := static.compiler.(*libraryDecorator); ok {
967 sharedCompiler := shared.compiler.(*libraryDecorator)
Dan Willemsen3a26eef2018-12-03 15:25:46 -0800968
969 // Check libraries in addition to cflags, since libraries may be exporting different
970 // include directories.
Colin Cross10d22312017-05-03 11:01:58 -0700971 if len(staticCompiler.Properties.Static.Cflags) == 0 &&
Dan Willemsen3a26eef2018-12-03 15:25:46 -0800972 len(sharedCompiler.Properties.Shared.Cflags) == 0 &&
973 len(staticCompiler.Properties.Static.Whole_static_libs) == 0 &&
974 len(sharedCompiler.Properties.Shared.Whole_static_libs) == 0 &&
975 len(staticCompiler.Properties.Static.Static_libs) == 0 &&
976 len(sharedCompiler.Properties.Shared.Static_libs) == 0 &&
977 len(staticCompiler.Properties.Static.Shared_libs) == 0 &&
978 len(sharedCompiler.Properties.Shared.Shared_libs) == 0 &&
979 staticCompiler.Properties.Static.System_shared_libs == nil &&
980 sharedCompiler.Properties.Shared.System_shared_libs == nil {
Colin Cross10d22312017-05-03 11:01:58 -0700981
982 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
983 sharedCompiler.baseCompiler.Properties.OriginalSrcs =
984 sharedCompiler.baseCompiler.Properties.Srcs
985 sharedCompiler.baseCompiler.Properties.Srcs = nil
986 sharedCompiler.baseCompiler.Properties.Generated_sources = nil
987 }
988 }
989}
990
Colin Crosse40b4ea2018-10-02 22:25:58 -0700991func LinkageMutator(mctx android.BottomUpMutatorContext) {
Colin Crossb916a382016-07-29 17:28:03 -0700992 if m, ok := mctx.Module().(*Module); ok && m.linker != nil {
993 if library, ok := m.linker.(libraryInterface); ok {
994 var modules []blueprint.Module
995 if library.buildStatic() && library.buildShared() {
996 modules = mctx.CreateLocalVariations("static", "shared")
997 static := modules[0].(*Module)
998 shared := modules[1].(*Module)
999
Colin Crossa48ab5b2017-02-14 15:28:44 -08001000 static.linker.(libraryInterface).setStatic()
1001 shared.linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -07001002
Colin Cross10d22312017-05-03 11:01:58 -07001003 reuseStaticLibrary(mctx, static, shared)
1004
Colin Crossb916a382016-07-29 17:28:03 -07001005 } else if library.buildStatic() {
1006 modules = mctx.CreateLocalVariations("static")
Colin Crossa48ab5b2017-02-14 15:28:44 -08001007 modules[0].(*Module).linker.(libraryInterface).setStatic()
Colin Crossb916a382016-07-29 17:28:03 -07001008 } else if library.buildShared() {
1009 modules = mctx.CreateLocalVariations("shared")
Colin Crossa48ab5b2017-02-14 15:28:44 -08001010 modules[0].(*Module).linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -07001011 }
1012 }
1013 }
Colin Cross4d9c2d12016-07-29 12:48:20 -07001014}
Jiyong Park7ed9de32018-10-15 22:25:07 +09001015
Jiyong Park25fc6a92018-11-18 18:02:45 +09001016// maps a module name to the list of stubs versions available for the module
1017func stubsVersionsFor(config android.Config) map[string][]string {
1018 return config.Once("stubVersions", func() interface{} {
1019 return make(map[string][]string)
1020 }).(map[string][]string)
1021}
1022
1023var stubsVersionsLock sync.Mutex
1024
1025func latestStubsVersionFor(config android.Config, name string) string {
1026 versions, ok := stubsVersionsFor(config)[name]
1027 if ok && len(versions) > 0 {
1028 // the versions are alreay sorted in ascending order
1029 return versions[len(versions)-1]
1030 }
1031 return ""
1032}
1033
Jiyong Park7ed9de32018-10-15 22:25:07 +09001034// Version mutator splits a module into the mandatory non-stubs variant
Jiyong Park25fc6a92018-11-18 18:02:45 +09001035// (which is unnamed) and zero or more stubs variants.
1036func VersionMutator(mctx android.BottomUpMutatorContext) {
Jiyong Park7ed9de32018-10-15 22:25:07 +09001037 if m, ok := mctx.Module().(*Module); ok && !m.inRecovery() && m.linker != nil {
Jiyong Park25fc6a92018-11-18 18:02:45 +09001038 if library, ok := m.linker.(*libraryDecorator); ok && library.buildShared() &&
1039 len(library.Properties.Stubs.Versions) > 0 {
1040 versions := []string{}
Jiyong Park7ed9de32018-10-15 22:25:07 +09001041 for _, v := range library.Properties.Stubs.Versions {
Jiyong Park25fc6a92018-11-18 18:02:45 +09001042 if _, err := strconv.Atoi(v); err != nil {
1043 mctx.PropertyErrorf("versions", "%q is not a number", v)
1044 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09001045 versions = append(versions, v)
1046 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001047 sort.Slice(versions, func(i, j int) bool {
1048 left, _ := strconv.Atoi(versions[i])
1049 right, _ := strconv.Atoi(versions[j])
1050 return left < right
1051 })
1052
1053 // save the list of versions for later use
1054 copiedVersions := make([]string, len(versions))
1055 copy(copiedVersions, versions)
1056 stubsVersionsLock.Lock()
1057 defer stubsVersionsLock.Unlock()
1058 stubsVersionsFor(mctx.Config())[mctx.ModuleName()] = copiedVersions
1059
1060 // "" is for the non-stubs variant
1061 versions = append(versions, "")
1062
Jiyong Park7ed9de32018-10-15 22:25:07 +09001063 modules := mctx.CreateVariations(versions...)
1064 for i, m := range modules {
1065 l := m.(*Module).linker.(*libraryDecorator)
Jiyong Park25fc6a92018-11-18 18:02:45 +09001066 if versions[i] != "" {
1067 l.MutatedProperties.BuildStubs = true
1068 l.MutatedProperties.StubsVersion = versions[i]
1069 m.(*Module).Properties.HideFromMake = true
Jiyong Park090d9df2018-12-11 02:47:16 +09001070 m.(*Module).sanitize = nil
1071 m.(*Module).stl = nil
Jiyong Park127d5652018-12-12 10:26:20 +09001072 m.(*Module).Properties.PreventInstall = true
Jiyong Park7ed9de32018-10-15 22:25:07 +09001073 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09001074 }
1075 } else {
1076 mctx.CreateVariations("")
1077 }
1078 return
1079 }
1080 if genrule, ok := mctx.Module().(*genrule.Module); ok {
1081 if props, ok := genrule.Extra.(*GenruleExtraProperties); ok && !props.InRecovery {
1082 mctx.CreateVariations("")
1083 return
1084 }
1085 }
1086}