blob: 822274a145d98e997ba533124edb495e8d37ac73 [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() {
Colin Cross2f336352016-10-26 10:03:47 -0700418 srcs := android.PathsForModuleSrc(ctx, library.Properties.Static.Srcs)
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() {
Colin Cross2f336352016-10-26 10:03:47 -0700422 srcs := android.PathsForModuleSrc(ctx, library.Properties.Shared.Srcs)
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
Colin Cross37047f12016-12-13 17:06:13 -0800494func (library *libraryDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Dan Willemsen3a26eef2018-12-03 15:25:46 -0800495 if library.static() {
496 if library.Properties.Static.System_shared_libs != nil {
497 library.baseLinker.Properties.System_shared_libs = library.Properties.Static.System_shared_libs
498 }
499 } else if library.shared() {
500 if library.Properties.Shared.System_shared_libs != nil {
501 library.baseLinker.Properties.System_shared_libs = library.Properties.Shared.System_shared_libs
502 }
503 }
504
Colin Crossb916a382016-07-29 17:28:03 -0700505 deps = library.baseLinker.linkerDeps(ctx, deps)
506
507 if library.static() {
508 deps.WholeStaticLibs = append(deps.WholeStaticLibs,
509 library.Properties.Static.Whole_static_libs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700510 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Static.Static_libs...)
511 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Static.Shared_libs...)
Colin Crossa48ab5b2017-02-14 15:28:44 -0800512 } else if library.shared() {
Dan Willemsen2e47b342016-11-17 01:02:25 -0800513 if ctx.toolchain().Bionic() && !Bool(library.baseLinker.Properties.Nocrt) {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700514 if !ctx.useSdk() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700515 deps.CrtBegin = "crtbegin_so"
516 deps.CrtEnd = "crtend_so"
517 } else {
Dan Albertebedf672016-11-08 15:06:22 -0800518 // TODO(danalbert): Add generation of crt objects.
519 // For `sdk_version: "current"`, we don't actually have a
520 // freshly generated set of CRT objects. Use the last stable
521 // version.
522 version := ctx.sdkVersion()
523 if version == "current" {
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -0700524 version = getCurrentNdkPrebuiltVersion(ctx)
Dan Albertebedf672016-11-08 15:06:22 -0800525 }
526 deps.CrtBegin = "ndk_crtbegin_so." + version
527 deps.CrtEnd = "ndk_crtend_so." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700528 }
529 }
530 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Shared.Whole_static_libs...)
531 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Shared.Static_libs...)
532 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Shared.Shared_libs...)
533 }
Jiyong Park52d25bd2017-10-13 09:17:01 +0900534 if ctx.useVndk() {
535 deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs)
536 deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Vendor.Exclude_shared_libs)
537 deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs)
538 }
Jiyong Parkf9332f12018-02-01 00:54:12 +0900539 if ctx.inRecovery() {
540 deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Recovery.Exclude_static_libs)
541 deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Recovery.Exclude_shared_libs)
542 deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Recovery.Exclude_static_libs)
543 }
Colin Cross2383f3b2018-02-06 14:40:13 -0800544
Colin Cross2383f3b2018-02-06 14:40:13 -0800545 android.ExtractSourceDeps(ctx, library.Properties.Unexported_symbols_list)
546 android.ExtractSourceDeps(ctx, library.Properties.Force_symbols_not_weak_list)
547 android.ExtractSourceDeps(ctx, library.Properties.Force_symbols_weak_list)
Colin Cross2383f3b2018-02-06 14:40:13 -0800548
Colin Cross4d9c2d12016-07-29 12:48:20 -0700549 return deps
550}
551
Colin Crossb916a382016-07-29 17:28:03 -0700552func (library *libraryDecorator) linkStatic(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700553 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700554
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700555 library.objects = deps.WholeStaticLibObjs.Copy()
556 library.objects = library.objects.Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700557
Colin Cross86803cf2018-02-15 14:12:26 -0800558 fileName := ctx.ModuleName() + library.MutatedProperties.VariantName + staticLibraryExtension
559 outputFile := android.PathForModuleOut(ctx, fileName)
Dan Willemsen581341d2017-02-09 16:16:31 -0800560 builderFlags := flagsToBuilderFlags(flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700561
Dan Willemsen569edc52018-11-19 09:33:29 -0800562 if Bool(library.baseLinker.Properties.Use_version_lib) {
563 if ctx.Host() {
564 versionedOutputFile := outputFile
565 outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
566 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
567 } else {
568 versionedOutputFile := android.PathForModuleOut(ctx, "versioned", fileName)
569 library.distFile = android.OptionalPathForPath(versionedOutputFile)
570 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
571 }
Colin Cross86803cf2018-02-15 14:12:26 -0800572 }
573
Dan Willemsen581341d2017-02-09 16:16:31 -0800574 TransformObjToStaticLib(ctx, library.objects.objFiles, builderFlags, outputFile, objs.tidyFiles)
575
576 library.coverageOutputFile = TransformCoverageFilesToLib(ctx, library.objects, builderFlags,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800577 ctx.ModuleName()+library.MutatedProperties.VariantName)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700578
579 library.wholeStaticMissingDeps = ctx.GetMissingDependencies()
580
581 ctx.CheckbuildFile(outputFile)
582
583 return outputFile
584}
585
Colin Crossb916a382016-07-29 17:28:03 -0700586func (library *libraryDecorator) linkShared(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700587 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700588
589 var linkerDeps android.Paths
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700590 linkerDeps = append(linkerDeps, flags.LdFlagsDeps...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700591
Colin Cross2383f3b2018-02-06 14:40:13 -0800592 unexportedSymbols := ctx.ExpandOptionalSource(library.Properties.Unexported_symbols_list, "unexported_symbols_list")
593 forceNotWeakSymbols := ctx.ExpandOptionalSource(library.Properties.Force_symbols_not_weak_list, "force_symbols_not_weak_list")
594 forceWeakSymbols := ctx.ExpandOptionalSource(library.Properties.Force_symbols_weak_list, "force_symbols_weak_list")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700595 if !ctx.Darwin() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700596 if unexportedSymbols.Valid() {
597 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
598 }
599 if forceNotWeakSymbols.Valid() {
600 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
601 }
602 if forceWeakSymbols.Valid() {
603 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
604 }
605 } else {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700606 if unexportedSymbols.Valid() {
607 flags.LdFlags = append(flags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
608 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
609 }
610 if forceNotWeakSymbols.Valid() {
611 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
612 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
613 }
614 if forceWeakSymbols.Valid() {
615 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
616 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
617 }
618 }
619
620 fileName := library.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
621 outputFile := android.PathForModuleOut(ctx, fileName)
622 ret := outputFile
623
624 builderFlags := flagsToBuilderFlags(flags)
625
Colin Crossb496cfd2018-09-10 16:50:05 -0700626 // Optimize out relinking against shared libraries whose interface hasn't changed by
627 // depending on a table of contents file instead of the library itself.
628 tocPath := outputFile.RelPathString()
629 tocPath = pathtools.ReplaceExtension(tocPath, flags.Toolchain.ShlibSuffix()[1:]+".toc")
630 tocFile := android.PathForOutput(ctx, tocPath)
631 library.tocFile = android.OptionalPathForPath(tocFile)
632 TransformSharedObjectToToc(ctx, outputFile, tocFile, builderFlags)
Colin Cross89562dc2016-10-03 17:47:19 -0700633
Colin Cross4d9c2d12016-07-29 12:48:20 -0700634 if library.stripper.needsStrip(ctx) {
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -0700635 // b/80093681, GNU strip/objcopy bug.
636 // Use llvm-{strip,objcopy} when clang lld is used.
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700637 builderFlags.stripUseLlvmStrip = library.baseLinker.useClangLld(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700638 strippedOutputFile := outputFile
639 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
640 library.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
641 }
642
Colin Crossb60190a2018-09-04 16:28:17 -0700643 library.unstrippedOutputFile = outputFile
644
Dan Willemsen569edc52018-11-19 09:33:29 -0800645 if Bool(library.baseLinker.Properties.Use_version_lib) {
646 if ctx.Host() {
647 versionedOutputFile := outputFile
648 outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
649 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
650 } else {
651 versionedOutputFile := android.PathForModuleOut(ctx, "versioned", fileName)
652 library.distFile = android.OptionalPathForPath(versionedOutputFile)
653
654 if library.stripper.needsStrip(ctx) {
655 out := android.PathForModuleOut(ctx, "versioned-stripped", fileName)
656 library.distFile = android.OptionalPathForPath(out)
657 library.stripper.strip(ctx, versionedOutputFile, out, builderFlags)
658 }
659
660 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
661 }
Colin Cross86803cf2018-02-15 14:12:26 -0800662 }
663
Colin Cross4d9c2d12016-07-29 12:48:20 -0700664 sharedLibs := deps.SharedLibs
665 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
666
Colin Cross26c34ed2016-09-30 17:10:16 -0700667 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
668 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700669 linkerDeps = append(linkerDeps, objs.tidyFiles...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700670
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700671 TransformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700672 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
673 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile)
674
Dan Willemsen581341d2017-02-09 16:16:31 -0800675 objs.coverageFiles = append(objs.coverageFiles, deps.StaticLibObjs.coverageFiles...)
676 objs.coverageFiles = append(objs.coverageFiles, deps.WholeStaticLibObjs.coverageFiles...)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800677
678 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.StaticLibObjs.sAbiDumpFiles...)
679 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.WholeStaticLibObjs.sAbiDumpFiles...)
680
Dan Willemsen581341d2017-02-09 16:16:31 -0800681 library.coverageOutputFile = TransformCoverageFilesToLib(ctx, objs, builderFlags, library.getLibName(ctx))
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700682 library.linkSAbiDumpFiles(ctx, objs, fileName, ret)
Dan Willemsen581341d2017-02-09 16:16:31 -0800683
Colin Cross4d9c2d12016-07-29 12:48:20 -0700684 return ret
685}
686
Logan Chien7eefdc42018-07-11 18:10:41 +0800687func getRefAbiDumpFile(ctx ModuleContext, vndkVersion, fileName string) android.Path {
Logan Chienf4b79c62018-08-02 02:27:02 +0800688 isLlndk := inList(ctx.baseModuleName(), llndkLibraries) || inList(ctx.baseModuleName(), ndkMigratedLibs)
Logan Chien7eefdc42018-07-11 18:10:41 +0800689
690 refAbiDumpTextFile := android.PathForVndkRefAbiDump(ctx, vndkVersion, fileName, isLlndk, false)
691 refAbiDumpGzipFile := android.PathForVndkRefAbiDump(ctx, vndkVersion, fileName, isLlndk, true)
692
693 if refAbiDumpTextFile.Valid() {
694 if refAbiDumpGzipFile.Valid() {
695 ctx.ModuleErrorf(
696 "Two reference ABI dump files are found: %q and %q. Please delete the stale one.",
697 refAbiDumpTextFile, refAbiDumpGzipFile)
698 return nil
699 }
700 return refAbiDumpTextFile.Path()
701 }
702 if refAbiDumpGzipFile.Valid() {
703 return UnzipRefDump(ctx, refAbiDumpGzipFile.Path(), fileName)
704 }
705 return nil
706}
707
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700708func (library *libraryDecorator) linkSAbiDumpFiles(ctx ModuleContext, objs Objects, fileName string, soFile android.Path) {
Logan Chien2f2b8902018-07-10 15:01:19 +0800709 if len(objs.sAbiDumpFiles) > 0 && ctx.shouldCreateVndkSourceAbiDump() {
Logan Chiena8f51582018-03-21 11:53:48 +0800710 vndkVersion := ctx.DeviceConfig().PlatformVndkVersion()
711 if ver := ctx.DeviceConfig().VndkVersion(); ver != "" && ver != "current" {
Logan Chienf3511742017-10-31 18:04:35 +0800712 vndkVersion = ver
713 }
714
Jayant Chowdharya4fce192017-09-06 13:10:03 -0700715 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800716 var SourceAbiFlags []string
717 for _, dir := range exportIncludeDirs.Strings() {
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700718 SourceAbiFlags = append(SourceAbiFlags, "-I"+dir)
719 }
720 for _, reexportedInclude := range extractExportIncludesFromFlags(library.sabi.Properties.ReexportedIncludeFlags) {
721 SourceAbiFlags = append(SourceAbiFlags, reexportedInclude)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800722 }
723 exportedHeaderFlags := strings.Join(SourceAbiFlags, " ")
Jayant Chowdharydf344d52018-01-17 11:11:42 -0800724 library.sAbiOutputFile = TransformDumpToLinkedDump(ctx, objs.sAbiDumpFiles, soFile, fileName, exportedHeaderFlags)
Logan Chien2f2b8902018-07-10 15:01:19 +0800725
Logan Chien7eefdc42018-07-11 18:10:41 +0800726 refAbiDumpFile := getRefAbiDumpFile(ctx, vndkVersion, fileName)
727 if refAbiDumpFile != nil {
Logan Chienf3511742017-10-31 18:04:35 +0800728 library.sAbiDiff = SourceAbiDiff(ctx, library.sAbiOutputFile.Path(),
Logan Chien7eefdc42018-07-11 18:10:41 +0800729 refAbiDumpFile, fileName, exportedHeaderFlags, ctx.isVndkExt())
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800730 }
731 }
732}
733
Colin Crossb916a382016-07-29 17:28:03 -0700734func (library *libraryDecorator) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700735 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700736
Colin Crossad59e752017-11-16 14:29:11 -0800737 objs = deps.Objs.Copy().Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700738 var out android.Path
Colin Crossa48ab5b2017-02-14 15:28:44 -0800739 if library.static() || library.header() {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700740 out = library.linkStatic(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700741 } else {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700742 out = library.linkShared(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700743 }
744
745 library.exportIncludes(ctx, "-I")
746 library.reexportFlags(deps.ReexportedFlags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700747 library.reexportDeps(deps.ReexportedFlagsDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700748
Nan Zhang0007d812017-11-07 10:57:05 -0800749 if Bool(library.Properties.Aidl.Export_aidl_headers) {
Dan Willemsene1240db2016-11-03 14:28:51 -0700750 if library.baseCompiler.hasSrcExt(".aidl") {
Colin Cross10d22312017-05-03 11:01:58 -0700751 flags := []string{
Dan Willemsene1240db2016-11-03 14:28:51 -0700752 "-I" + android.PathForModuleGen(ctx, "aidl").String(),
Colin Cross10d22312017-05-03 11:01:58 -0700753 }
754 library.reexportFlags(flags)
755 library.reuseExportedFlags = append(library.reuseExportedFlags, flags...)
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800756 library.reexportDeps(library.baseCompiler.pathDeps) // TODO: restrict to aidl deps
757 library.reuseExportedDeps = append(library.reuseExportedDeps, library.baseCompiler.pathDeps...)
Dan Willemsene1240db2016-11-03 14:28:51 -0700758 }
759 }
760
Nan Zhang0007d812017-11-07 10:57:05 -0800761 if Bool(library.Properties.Proto.Export_proto_headers) {
Dan Willemsene1240db2016-11-03 14:28:51 -0700762 if library.baseCompiler.hasSrcExt(".proto") {
Dan Willemsenab9f4262018-02-14 13:58:34 -0800763 includes := []string{}
764 if flags.ProtoRoot {
765 includes = append(includes, "-I"+android.ProtoSubDir(ctx).String())
Colin Cross10d22312017-05-03 11:01:58 -0700766 }
Dan Willemsenab9f4262018-02-14 13:58:34 -0800767 includes = append(includes, "-I"+android.ProtoDir(ctx).String())
768 library.reexportFlags(includes)
769 library.reuseExportedFlags = append(library.reuseExportedFlags, includes...)
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800770 library.reexportDeps(library.baseCompiler.pathDeps) // TODO: restrict to proto deps
771 library.reuseExportedDeps = append(library.reuseExportedDeps, library.baseCompiler.pathDeps...)
Colin Cross0c461f12016-10-20 16:11:43 -0700772 }
773 }
774
Inseob Kim21f26902018-09-06 00:55:20 +0900775 if library.baseCompiler.hasSrcExt(".sysprop") {
776 flags := []string{
777 "-I" + android.PathForModuleGen(ctx, "sysprop", "include").String(),
778 }
779 library.reexportFlags(flags)
780 library.reuseExportedFlags = append(library.reuseExportedFlags, flags...)
781 }
782
Jiyong Parkda732bd2018-11-02 18:23:15 +0900783 if library.buildStubs() {
784 library.reexportFlags([]string{"-D" + versioningMacroName(ctx.ModuleName()) + "=" + library.stubsVersion()})
785 }
786
Colin Cross4d9c2d12016-07-29 12:48:20 -0700787 return out
788}
789
Colin Crossb916a382016-07-29 17:28:03 -0700790func (library *libraryDecorator) buildStatic() bool {
Colin Cross38b40df2018-04-10 16:14:46 -0700791 return library.MutatedProperties.BuildStatic && BoolDefault(library.Properties.Static.Enabled, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700792}
793
Colin Crossb916a382016-07-29 17:28:03 -0700794func (library *libraryDecorator) buildShared() bool {
Colin Cross38b40df2018-04-10 16:14:46 -0700795 return library.MutatedProperties.BuildShared && BoolDefault(library.Properties.Shared.Enabled, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700796}
797
Colin Crossb916a382016-07-29 17:28:03 -0700798func (library *libraryDecorator) getWholeStaticMissingDeps() []string {
Colin Crossd2343a32018-04-30 14:50:01 -0700799 return append([]string(nil), library.wholeStaticMissingDeps...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700800}
801
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700802func (library *libraryDecorator) objs() Objects {
803 return library.objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700804}
805
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700806func (library *libraryDecorator) reuseObjs() (Objects, []string, android.Paths) {
807 return library.reuseObjects, library.reuseExportedFlags, library.reuseExportedDeps
Colin Cross4d9c2d12016-07-29 12:48:20 -0700808}
809
Colin Cross26c34ed2016-09-30 17:10:16 -0700810func (library *libraryDecorator) toc() android.OptionalPath {
811 return library.tocFile
812}
813
Colin Crossb916a382016-07-29 17:28:03 -0700814func (library *libraryDecorator) install(ctx ModuleContext, file android.Path) {
Colin Crossc43ae772017-04-14 15:42:53 -0700815 if library.shared() {
Justin Yun8fe12122017-12-07 17:18:15 +0900816 if ctx.Device() && ctx.useVndk() {
817 if ctx.isVndkSp() {
818 library.baseInstaller.subDir = "vndk-sp"
819 } else if ctx.isVndk() {
820 library.baseInstaller.subDir = "vndk"
821 }
Logan Chienf3511742017-10-31 18:04:35 +0800822
823 // Append a version to vndk or vndk-sp directories on the system partition.
824 if ctx.isVndk() && !ctx.isVndkExt() {
825 vndkVersion := ctx.DeviceConfig().PlatformVndkVersion()
826 if vndkVersion != "current" && vndkVersion != "" {
827 library.baseInstaller.subDir += "-" + vndkVersion
828 }
Justin Yun8effde42017-06-23 19:24:43 +0900829 }
830 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700831 library.baseInstaller.install(ctx, file)
832 }
Dan Albertf563d252017-10-13 00:29:00 -0700833
Dan Albert281f22b2017-12-13 15:03:47 -0800834 if Bool(library.Properties.Static_ndk_lib) && library.static() &&
Jiyong Parkf9332f12018-02-01 00:54:12 +0900835 !ctx.useVndk() && !ctx.inRecovery() && ctx.Device() &&
Jiyong Park7ed9de32018-10-15 22:25:07 +0900836 library.baseLinker.sanitize.isUnsanitizedVariant() &&
837 !library.buildStubs() {
Dan Albertf563d252017-10-13 00:29:00 -0700838 installPath := getNdkSysrootBase(ctx).Join(
Dan Albertea4b7b92018-04-25 16:05:30 -0700839 ctx, "usr/lib", config.NDKTriple(ctx.toolchain()), file.Base())
Dan Albertf563d252017-10-13 00:29:00 -0700840
841 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
842 Rule: android.Cp,
843 Description: "install " + installPath.Base(),
844 Output: installPath,
845 Input: file,
846 })
847
Colin Cross0875c522017-11-28 17:34:01 -0800848 library.ndkSysrootPath = installPath
Dan Albertf563d252017-10-13 00:29:00 -0700849 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700850}
851
Colin Crossb916a382016-07-29 17:28:03 -0700852func (library *libraryDecorator) static() bool {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800853 return library.MutatedProperties.VariantIsStatic
Colin Cross4d9c2d12016-07-29 12:48:20 -0700854}
855
Colin Crossa48ab5b2017-02-14 15:28:44 -0800856func (library *libraryDecorator) shared() bool {
857 return library.MutatedProperties.VariantIsShared
858}
859
860func (library *libraryDecorator) header() bool {
861 return !library.static() && !library.shared()
862}
863
864func (library *libraryDecorator) setStatic() {
865 library.MutatedProperties.VariantIsStatic = true
866 library.MutatedProperties.VariantIsShared = false
867}
868
869func (library *libraryDecorator) setShared() {
870 library.MutatedProperties.VariantIsStatic = false
871 library.MutatedProperties.VariantIsShared = true
Colin Crossb916a382016-07-29 17:28:03 -0700872}
873
Colin Crossab3b7322016-12-09 14:46:15 -0800874func (library *libraryDecorator) BuildOnlyStatic() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800875 library.MutatedProperties.BuildShared = false
Colin Crossab3b7322016-12-09 14:46:15 -0800876}
877
878func (library *libraryDecorator) BuildOnlyShared() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800879 library.MutatedProperties.BuildStatic = false
Colin Crossab3b7322016-12-09 14:46:15 -0800880}
881
Colin Cross5950f382016-12-13 12:50:57 -0800882func (library *libraryDecorator) HeaderOnly() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800883 library.MutatedProperties.BuildShared = false
884 library.MutatedProperties.BuildStatic = false
Colin Cross5950f382016-12-13 12:50:57 -0800885}
886
Jiyong Park7ed9de32018-10-15 22:25:07 +0900887func (library *libraryDecorator) buildStubs() bool {
888 return library.MutatedProperties.BuildStubs
889}
890
891func (library *libraryDecorator) stubsVersion() string {
892 return library.MutatedProperties.StubsVersion
893}
894
Jiyong Parkda732bd2018-11-02 18:23:15 +0900895func versioningMacroNamesList(config android.Config) *map[string]string {
896 return config.Once("versioningMacroNamesList", func() interface{} {
897 m := make(map[string]string)
898 return &m
899 }).(*map[string]string)
900}
901
902// alphanumeric and _ characters are preserved.
903// other characters are all converted to _
904var charsNotForMacro = regexp.MustCompile("[^a-zA-Z0-9_]+")
905
906func versioningMacroName(moduleName string) string {
907 macroName := charsNotForMacro.ReplaceAllString(moduleName, "_")
908 macroName = strings.ToUpper(moduleName)
909 return "__" + macroName + "_API__"
910}
911
Colin Crossab3b7322016-12-09 14:46:15 -0800912func NewLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700913 module := newModule(hod, android.MultilibBoth)
914
Colin Crossb916a382016-07-29 17:28:03 -0700915 library := &libraryDecorator{
Colin Crossa48ab5b2017-02-14 15:28:44 -0800916 MutatedProperties: LibraryMutatedProperties{
Colin Crossab3b7322016-12-09 14:46:15 -0800917 BuildShared: true,
918 BuildStatic: true,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700919 },
Colin Crossb916a382016-07-29 17:28:03 -0700920 baseCompiler: NewBaseCompiler(),
Dan Albert61f32122018-07-26 14:00:24 -0700921 baseLinker: NewBaseLinker(module.sanitize),
Colin Crossb916a382016-07-29 17:28:03 -0700922 baseInstaller: NewBaseInstaller("lib", "lib64", InstallInSystem),
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800923 sabi: module.sabi,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700924 }
925
Colin Crossb916a382016-07-29 17:28:03 -0700926 module.compiler = library
927 module.linker = library
928 module.installer = library
929
930 return module, library
931}
932
Colin Cross10d22312017-05-03 11:01:58 -0700933// connects a shared library to a static library in order to reuse its .o files to avoid
934// compiling source files twice.
935func reuseStaticLibrary(mctx android.BottomUpMutatorContext, static, shared *Module) {
936 if staticCompiler, ok := static.compiler.(*libraryDecorator); ok {
937 sharedCompiler := shared.compiler.(*libraryDecorator)
Dan Willemsen3a26eef2018-12-03 15:25:46 -0800938
939 // Check libraries in addition to cflags, since libraries may be exporting different
940 // include directories.
Colin Cross10d22312017-05-03 11:01:58 -0700941 if len(staticCompiler.Properties.Static.Cflags) == 0 &&
Dan Willemsen3a26eef2018-12-03 15:25:46 -0800942 len(sharedCompiler.Properties.Shared.Cflags) == 0 &&
943 len(staticCompiler.Properties.Static.Whole_static_libs) == 0 &&
944 len(sharedCompiler.Properties.Shared.Whole_static_libs) == 0 &&
945 len(staticCompiler.Properties.Static.Static_libs) == 0 &&
946 len(sharedCompiler.Properties.Shared.Static_libs) == 0 &&
947 len(staticCompiler.Properties.Static.Shared_libs) == 0 &&
948 len(sharedCompiler.Properties.Shared.Shared_libs) == 0 &&
949 staticCompiler.Properties.Static.System_shared_libs == nil &&
950 sharedCompiler.Properties.Shared.System_shared_libs == nil {
Colin Cross10d22312017-05-03 11:01:58 -0700951
952 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
953 sharedCompiler.baseCompiler.Properties.OriginalSrcs =
954 sharedCompiler.baseCompiler.Properties.Srcs
955 sharedCompiler.baseCompiler.Properties.Srcs = nil
956 sharedCompiler.baseCompiler.Properties.Generated_sources = nil
957 }
958 }
959}
960
Colin Crosse40b4ea2018-10-02 22:25:58 -0700961func LinkageMutator(mctx android.BottomUpMutatorContext) {
Colin Crossb916a382016-07-29 17:28:03 -0700962 if m, ok := mctx.Module().(*Module); ok && m.linker != nil {
963 if library, ok := m.linker.(libraryInterface); ok {
964 var modules []blueprint.Module
965 if library.buildStatic() && library.buildShared() {
966 modules = mctx.CreateLocalVariations("static", "shared")
967 static := modules[0].(*Module)
968 shared := modules[1].(*Module)
969
Colin Crossa48ab5b2017-02-14 15:28:44 -0800970 static.linker.(libraryInterface).setStatic()
971 shared.linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700972
Colin Cross10d22312017-05-03 11:01:58 -0700973 reuseStaticLibrary(mctx, static, shared)
974
Colin Crossb916a382016-07-29 17:28:03 -0700975 } else if library.buildStatic() {
976 modules = mctx.CreateLocalVariations("static")
Colin Crossa48ab5b2017-02-14 15:28:44 -0800977 modules[0].(*Module).linker.(libraryInterface).setStatic()
Colin Crossb916a382016-07-29 17:28:03 -0700978 } else if library.buildShared() {
979 modules = mctx.CreateLocalVariations("shared")
Colin Crossa48ab5b2017-02-14 15:28:44 -0800980 modules[0].(*Module).linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700981 }
982 }
983 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700984}
Jiyong Park7ed9de32018-10-15 22:25:07 +0900985
Jiyong Park25fc6a92018-11-18 18:02:45 +0900986// maps a module name to the list of stubs versions available for the module
987func stubsVersionsFor(config android.Config) map[string][]string {
988 return config.Once("stubVersions", func() interface{} {
989 return make(map[string][]string)
990 }).(map[string][]string)
991}
992
993var stubsVersionsLock sync.Mutex
994
995func latestStubsVersionFor(config android.Config, name string) string {
996 versions, ok := stubsVersionsFor(config)[name]
997 if ok && len(versions) > 0 {
998 // the versions are alreay sorted in ascending order
999 return versions[len(versions)-1]
1000 }
1001 return ""
1002}
1003
Jiyong Park7ed9de32018-10-15 22:25:07 +09001004// Version mutator splits a module into the mandatory non-stubs variant
Jiyong Park25fc6a92018-11-18 18:02:45 +09001005// (which is unnamed) and zero or more stubs variants.
1006func VersionMutator(mctx android.BottomUpMutatorContext) {
Jiyong Park7ed9de32018-10-15 22:25:07 +09001007 if mctx.Os() != android.Android {
1008 return
1009 }
1010
1011 if m, ok := mctx.Module().(*Module); ok && !m.inRecovery() && m.linker != nil {
Jiyong Park25fc6a92018-11-18 18:02:45 +09001012 if library, ok := m.linker.(*libraryDecorator); ok && library.buildShared() &&
1013 len(library.Properties.Stubs.Versions) > 0 {
1014 versions := []string{}
Jiyong Park7ed9de32018-10-15 22:25:07 +09001015 for _, v := range library.Properties.Stubs.Versions {
Jiyong Park25fc6a92018-11-18 18:02:45 +09001016 if _, err := strconv.Atoi(v); err != nil {
1017 mctx.PropertyErrorf("versions", "%q is not a number", v)
1018 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09001019 versions = append(versions, v)
1020 }
Jiyong Park25fc6a92018-11-18 18:02:45 +09001021 sort.Slice(versions, func(i, j int) bool {
1022 left, _ := strconv.Atoi(versions[i])
1023 right, _ := strconv.Atoi(versions[j])
1024 return left < right
1025 })
1026
1027 // save the list of versions for later use
1028 copiedVersions := make([]string, len(versions))
1029 copy(copiedVersions, versions)
1030 stubsVersionsLock.Lock()
1031 defer stubsVersionsLock.Unlock()
1032 stubsVersionsFor(mctx.Config())[mctx.ModuleName()] = copiedVersions
1033
1034 // "" is for the non-stubs variant
1035 versions = append(versions, "")
1036
Jiyong Park7ed9de32018-10-15 22:25:07 +09001037 modules := mctx.CreateVariations(versions...)
1038 for i, m := range modules {
1039 l := m.(*Module).linker.(*libraryDecorator)
Jiyong Park25fc6a92018-11-18 18:02:45 +09001040 if versions[i] != "" {
1041 l.MutatedProperties.BuildStubs = true
1042 l.MutatedProperties.StubsVersion = versions[i]
1043 m.(*Module).Properties.HideFromMake = true
Jiyong Park7ed9de32018-10-15 22:25:07 +09001044 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09001045 }
1046 } else {
1047 mctx.CreateVariations("")
1048 }
1049 return
1050 }
1051 if genrule, ok := mctx.Module().(*genrule.Module); ok {
1052 if props, ok := genrule.Extra.(*GenruleExtraProperties); ok && !props.InRecovery {
1053 mctx.CreateVariations("")
1054 return
1055 }
1056 }
1057}