blob: abaa6c49d43ca152f0a29be5a1de6302dfcaba3d [file] [log] [blame]
Colin Cross4d9c2d12016-07-29 12:48:20 -07001// Copyright 2016 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package cc
16
17import (
Jiyong Parkda732bd2018-11-02 18:23:15 +090018 "regexp"
Colin Cross4d9c2d12016-07-29 12:48:20 -070019 "strings"
Jiyong Parkda732bd2018-11-02 18:23:15 +090020 "sync"
Colin Cross4d9c2d12016-07-29 12:48:20 -070021
22 "github.com/google/blueprint"
Colin Cross26c34ed2016-09-30 17:10:16 -070023 "github.com/google/blueprint/pathtools"
Colin Cross4d9c2d12016-07-29 12:48:20 -070024
Colin Cross4d9c2d12016-07-29 12:48:20 -070025 "android/soong/android"
Dan Albertea4b7b92018-04-25 16:05:30 -070026 "android/soong/cc/config"
Jiyong Park7ed9de32018-10-15 22:25:07 +090027 "android/soong/genrule"
Colin Cross4d9c2d12016-07-29 12:48:20 -070028)
29
Colin Crossb916a382016-07-29 17:28:03 -070030type LibraryProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -070031 Static struct {
Colin Cross2f336352016-10-26 10:03:47 -070032 Srcs []string `android:"arch_variant"`
33 Cflags []string `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070034
Colin Cross4d9c2d12016-07-29 12:48:20 -070035 Enabled *bool `android:"arch_variant"`
36 Whole_static_libs []string `android:"arch_variant"`
37 Static_libs []string `android:"arch_variant"`
38 Shared_libs []string `android:"arch_variant"`
39 } `android:"arch_variant"`
40 Shared struct {
Colin Cross2f336352016-10-26 10:03:47 -070041 Srcs []string `android:"arch_variant"`
42 Cflags []string `android:"arch_variant"`
Colin Crossb916a382016-07-29 17:28:03 -070043
Colin Cross4d9c2d12016-07-29 12:48:20 -070044 Enabled *bool `android:"arch_variant"`
45 Whole_static_libs []string `android:"arch_variant"`
46 Static_libs []string `android:"arch_variant"`
47 Shared_libs []string `android:"arch_variant"`
48 } `android:"arch_variant"`
49
Colin Cross4d9c2d12016-07-29 12:48:20 -070050 // local file name to pass to the linker as -unexported_symbols_list
51 Unexported_symbols_list *string `android:"arch_variant"`
52 // local file name to pass to the linker as -force_symbols_not_weak_list
53 Force_symbols_not_weak_list *string `android:"arch_variant"`
54 // local file name to pass to the linker as -force_symbols_weak_list
55 Force_symbols_weak_list *string `android:"arch_variant"`
56
57 // rename host libraries to prevent overlap with system installed libraries
58 Unique_host_soname *bool
59
Dan Willemsene1240db2016-11-03 14:28:51 -070060 Aidl struct {
61 // export headers generated from .aidl sources
Nan Zhang0007d812017-11-07 10:57:05 -080062 Export_aidl_headers *bool
Dan Willemsene1240db2016-11-03 14:28:51 -070063 }
64
Colin Cross0c461f12016-10-20 16:11:43 -070065 Proto struct {
66 // export headers generated from .proto sources
Nan Zhang0007d812017-11-07 10:57:05 -080067 Export_proto_headers *bool
Colin Cross0c461f12016-10-20 16:11:43 -070068 }
Dan Albertf563d252017-10-13 00:29:00 -070069
Nan Zhang0007d812017-11-07 10:57:05 -080070 Static_ndk_lib *bool
Jiyong Park7ed9de32018-10-15 22:25:07 +090071
72 Stubs struct {
73 // Relative path to the symbol map. The symbol map provides the list of
74 // symbols that are exported for stubs variant of this library.
75 Symbol_file *string
76
77 // List versions to generate stubs libs for.
78 Versions []string
79 }
dimitryd95964a2018-11-07 13:43:34 +010080
81 // set the name of the output
82 Stem *string `android:"arch_variant"`
83
84 // Names of modules to be overridden. Listed modules can only be other shared libraries
85 // (in Make or Soong).
86 // This does not completely prevent installation of the overridden libraries, but if both
87 // binaries would be installed by default (in PRODUCT_PACKAGES) the other library will be removed
88 // from PRODUCT_PACKAGES.
89 Overrides []string
Colin Crossa48ab5b2017-02-14 15:28:44 -080090}
Colin Cross0c461f12016-10-20 16:11:43 -070091
Colin Crossa48ab5b2017-02-14 15:28:44 -080092type LibraryMutatedProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -070093 VariantName string `blueprint:"mutated"`
Colin Crossb916a382016-07-29 17:28:03 -070094
95 // Build a static variant
96 BuildStatic bool `blueprint:"mutated"`
97 // Build a shared variant
98 BuildShared bool `blueprint:"mutated"`
99 // This variant is shared
100 VariantIsShared bool `blueprint:"mutated"`
101 // This variant is static
102 VariantIsStatic bool `blueprint:"mutated"`
Jiyong Park7ed9de32018-10-15 22:25:07 +0900103
104 // This variant is a stubs lib
105 BuildStubs bool `blueprint:"mutated"`
106 // Version of the stubs lib
107 StubsVersion string `blueprint:"mutated"`
Colin Crossb916a382016-07-29 17:28:03 -0700108}
109
110type FlagExporterProperties struct {
111 // list of directories relative to the Blueprints file that will
Dan Willemsen273af7f2016-11-03 15:53:42 -0700112 // be added to the include path (using -I) for this module and any module that links
Colin Cross5d195602017-10-17 16:15:50 -0700113 // against this module. Directories listed in export_include_dirs do not need to be
114 // listed in local_include_dirs.
Colin Crossb916a382016-07-29 17:28:03 -0700115 Export_include_dirs []string `android:"arch_variant"`
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700116
117 Target struct {
118 Vendor struct {
119 // list of exported include directories, like
120 // export_include_dirs, that will be applied to the
121 // vendor variant of this library. This will overwrite
122 // any other declarations.
Steven Morelandb21df8f2018-01-05 14:42:54 -0800123 Override_export_include_dirs []string
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700124 }
125 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700126}
127
128func init() {
Steven Morelandf9e62162017-11-02 17:00:50 -0700129 android.RegisterModuleType("cc_library_static", LibraryStaticFactory)
130 android.RegisterModuleType("cc_library_shared", LibrarySharedFactory)
131 android.RegisterModuleType("cc_library", LibraryFactory)
132 android.RegisterModuleType("cc_library_host_static", LibraryHostStaticFactory)
133 android.RegisterModuleType("cc_library_host_shared", LibraryHostSharedFactory)
134 android.RegisterModuleType("cc_library_headers", LibraryHeaderFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700135}
136
137// Module factory for combined static + shared libraries, device by default but with possible host
138// support
Steven Morelandf9e62162017-11-02 17:00:50 -0700139func LibraryFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800140 module, _ := NewLibrary(android.HostAndDeviceSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700141 return module.Init()
142}
143
144// Module factory for static libraries
Steven Morelandf9e62162017-11-02 17:00:50 -0700145func LibraryStaticFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800146 module, library := NewLibrary(android.HostAndDeviceSupported)
147 library.BuildOnlyStatic()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700148 return module.Init()
149}
150
151// Module factory for shared libraries
Steven Morelandf9e62162017-11-02 17:00:50 -0700152func LibrarySharedFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800153 module, library := NewLibrary(android.HostAndDeviceSupported)
154 library.BuildOnlyShared()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700155 return module.Init()
156}
157
158// Module factory for host static libraries
Steven Morelandf9e62162017-11-02 17:00:50 -0700159func LibraryHostStaticFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800160 module, library := NewLibrary(android.HostSupported)
161 library.BuildOnlyStatic()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700162 return module.Init()
163}
164
165// Module factory for host shared libraries
Steven Morelandf9e62162017-11-02 17:00:50 -0700166func LibraryHostSharedFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800167 module, library := NewLibrary(android.HostSupported)
168 library.BuildOnlyShared()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700169 return module.Init()
170}
171
Colin Cross5950f382016-12-13 12:50:57 -0800172// Module factory for header-only libraries
Steven Morelandf9e62162017-11-02 17:00:50 -0700173func LibraryHeaderFactory() android.Module {
Colin Cross5950f382016-12-13 12:50:57 -0800174 module, library := NewLibrary(android.HostAndDeviceSupported)
175 library.HeaderOnly()
176 return module.Init()
177}
178
Colin Cross4d9c2d12016-07-29 12:48:20 -0700179type flagExporter struct {
180 Properties FlagExporterProperties
181
Dan Willemsen847dcc72016-09-29 12:13:36 -0700182 flags []string
183 flagsDeps android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700184}
185
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700186func (f *flagExporter) exportedIncludes(ctx ModuleContext) android.Paths {
Steven Morelandb21df8f2018-01-05 14:42:54 -0800187 if ctx.useVndk() && f.Properties.Target.Vendor.Override_export_include_dirs != nil {
188 return android.PathsForModuleSrc(ctx, f.Properties.Target.Vendor.Override_export_include_dirs)
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700189 } else {
190 return android.PathsForModuleSrc(ctx, f.Properties.Export_include_dirs)
191 }
192}
193
Colin Cross4d9c2d12016-07-29 12:48:20 -0700194func (f *flagExporter) exportIncludes(ctx ModuleContext, inc string) {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700195 includeDirs := f.exportedIncludes(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700196 for _, dir := range includeDirs.Strings() {
197 f.flags = append(f.flags, inc+dir)
198 }
199}
200
201func (f *flagExporter) reexportFlags(flags []string) {
202 f.flags = append(f.flags, flags...)
203}
204
Dan Willemsen847dcc72016-09-29 12:13:36 -0700205func (f *flagExporter) reexportDeps(deps android.Paths) {
206 f.flagsDeps = append(f.flagsDeps, deps...)
207}
208
Colin Cross4d9c2d12016-07-29 12:48:20 -0700209func (f *flagExporter) exportedFlags() []string {
210 return f.flags
211}
212
Dan Willemsen847dcc72016-09-29 12:13:36 -0700213func (f *flagExporter) exportedFlagsDeps() android.Paths {
214 return f.flagsDeps
215}
216
Colin Cross4d9c2d12016-07-29 12:48:20 -0700217type exportedFlagsProducer interface {
218 exportedFlags() []string
Dan Willemsen847dcc72016-09-29 12:13:36 -0700219 exportedFlagsDeps() android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700220}
221
222var _ exportedFlagsProducer = (*flagExporter)(nil)
223
Colin Crossb916a382016-07-29 17:28:03 -0700224// libraryDecorator wraps baseCompiler, baseLinker and baseInstaller to provide library-specific
225// functionality: static vs. shared linkage, reusing object files for shared libraries
226type libraryDecorator struct {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800227 Properties LibraryProperties
228 MutatedProperties LibraryMutatedProperties
Colin Cross4d9c2d12016-07-29 12:48:20 -0700229
230 // For reusing static library objects for shared library
Colin Cross10d22312017-05-03 11:01:58 -0700231 reuseObjects Objects
232 reuseExportedFlags []string
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700233 reuseExportedDeps android.Paths
Colin Cross10d22312017-05-03 11:01:58 -0700234
Colin Cross26c34ed2016-09-30 17:10:16 -0700235 // table-of-contents file to optimize out relinking when possible
236 tocFile android.OptionalPath
Colin Cross4d9c2d12016-07-29 12:48:20 -0700237
Colin Cross4d9c2d12016-07-29 12:48:20 -0700238 flagExporter
239 stripper
240
Colin Cross4d9c2d12016-07-29 12:48:20 -0700241 // If we're used as a whole_static_lib, our missing dependencies need
242 // to be given
243 wholeStaticMissingDeps []string
244
245 // For whole_static_libs
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700246 objects Objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700247
248 // Uses the module's name if empty, but can be overridden. Does not include
249 // shlib suffix.
250 libName string
Colin Crossb916a382016-07-29 17:28:03 -0700251
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800252 sabi *sabi
253
Dan Willemsen581341d2017-02-09 16:16:31 -0800254 // Output archive of gcno coverage information files
255 coverageOutputFile android.OptionalPath
256
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800257 // linked Source Abi Dump
258 sAbiOutputFile android.OptionalPath
259
260 // Source Abi Diff
261 sAbiDiff android.OptionalPath
262
Colin Cross0875c522017-11-28 17:34:01 -0800263 // Location of the static library in the sysroot. Empty if the library is
264 // not included in the NDK.
265 ndkSysrootPath android.Path
266
Colin Crossb60190a2018-09-04 16:28:17 -0700267 // Location of the linked, unstripped library for shared libraries
268 unstrippedOutputFile android.Path
269
Jiyong Park7ed9de32018-10-15 22:25:07 +0900270 versionScriptPath android.ModuleGenPath
271
Colin Crossb916a382016-07-29 17:28:03 -0700272 // Decorated interafaces
273 *baseCompiler
274 *baseLinker
275 *baseInstaller
Colin Cross4d9c2d12016-07-29 12:48:20 -0700276}
277
Colin Crossb916a382016-07-29 17:28:03 -0700278func (library *libraryDecorator) linkerProps() []interface{} {
279 var props []interface{}
280 props = append(props, library.baseLinker.linkerProps()...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700281 return append(props,
282 &library.Properties,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800283 &library.MutatedProperties,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700284 &library.flagExporter.Properties,
Colin Cross22f37952018-09-05 10:43:13 -0700285 &library.stripper.StripProperties)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700286}
287
Colin Crossb916a382016-07-29 17:28:03 -0700288func (library *libraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross42742b82016-08-01 13:20:05 -0700289 flags = library.baseLinker.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700290
Colin Crossb916a382016-07-29 17:28:03 -0700291 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
292 // all code is position independent, and then those warnings get promoted to
293 // errors.
Colin Cross3edeee12017-04-04 12:59:48 -0700294 if !ctx.Windows() {
Colin Crossb916a382016-07-29 17:28:03 -0700295 flags.CFlags = append(flags.CFlags, "-fPIC")
296 }
297
298 if library.static() {
299 flags.CFlags = append(flags.CFlags, library.Properties.Static.Cflags...)
Colin Crossa48ab5b2017-02-14 15:28:44 -0800300 } else if library.shared() {
Colin Crossb916a382016-07-29 17:28:03 -0700301 flags.CFlags = append(flags.CFlags, library.Properties.Shared.Cflags...)
302 }
303
Colin Crossa48ab5b2017-02-14 15:28:44 -0800304 if library.shared() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700305 libName := library.getLibName(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700306 var f []string
Dan Willemsen01a405a2016-06-13 17:19:03 -0700307 if ctx.toolchain().Bionic() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700308 f = append(f,
309 "-nostdlib",
310 "-Wl,--gc-sections",
311 )
312 }
313
314 if ctx.Darwin() {
315 f = append(f,
316 "-dynamiclib",
317 "-single_module",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700318 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
319 )
Colin Cross7863cf52016-10-20 10:47:21 -0700320 if ctx.Arch().ArchType == android.X86 {
321 f = append(f,
322 "-read_only_relocs suppress",
323 )
324 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700325 } else {
326 f = append(f,
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700327 "-shared",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700328 "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix())
329 }
330
331 flags.LdFlags = append(f, flags.LdFlags...)
332 }
333
334 return flags
335}
336
Colin Crossf18e1102017-11-16 14:33:08 -0800337func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700338 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700339 if len(exportIncludeDirs) > 0 {
Colin Crossdad8c952017-04-26 14:55:27 -0700340 f := includeDirsToFlags(exportIncludeDirs)
341 flags.GlobalFlags = append(flags.GlobalFlags, f)
342 flags.YasmFlags = append(flags.YasmFlags, f)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700343 }
344
Jiyong Park7ed9de32018-10-15 22:25:07 +0900345 flags = library.baseCompiler.compilerFlags(ctx, flags, deps)
346 if library.buildStubs() {
347 flags = addStubLibraryCompilerFlags(flags)
348 }
349 return flags
Dan Willemsen273af7f2016-11-03 15:53:42 -0700350}
351
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700352func extractExportIncludesFromFlags(flags []string) []string {
353 // This method is used in the generation of rules which produce
354 // abi-dumps for source files. Exported headers are needed to infer the
355 // abi exported by a library and filter out the rest of the abi dumped
356 // from a source. We extract the include flags exported by a library.
357 // This includes the flags exported which are re-exported from static
358 // library dependencies, exported header library dependencies and
Jayant Chowdharyaf6eb712017-08-23 16:08:29 -0700359 // generated header dependencies. -isystem headers are not included
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700360 // since for bionic libraries, abi-filtering is taken care of by version
361 // scripts.
362 var exportedIncludes []string
363 for _, flag := range flags {
364 if strings.HasPrefix(flag, "-I") {
365 exportedIncludes = append(exportedIncludes, flag)
366 }
367 }
368 return exportedIncludes
369}
370
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700371func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Jiyong Park7ed9de32018-10-15 22:25:07 +0900372 if library.buildStubs() {
373 objs, versionScript := compileStubLibrary(ctx, flags, String(library.Properties.Stubs.Symbol_file), library.MutatedProperties.StubsVersion, "")
374 library.versionScriptPath = versionScript
375 return objs
376 }
377
Colin Cross5950f382016-12-13 12:50:57 -0800378 if !library.buildShared() && !library.buildStatic() {
379 if len(library.baseCompiler.Properties.Srcs) > 0 {
380 ctx.PropertyErrorf("srcs", "cc_library_headers must not have any srcs")
381 }
382 if len(library.Properties.Static.Srcs) > 0 {
383 ctx.PropertyErrorf("static.srcs", "cc_library_headers must not have any srcs")
384 }
385 if len(library.Properties.Shared.Srcs) > 0 {
386 ctx.PropertyErrorf("shared.srcs", "cc_library_headers must not have any srcs")
387 }
388 return Objects{}
389 }
Logan Chien2f2b8902018-07-10 15:01:19 +0800390 if ctx.shouldCreateVndkSourceAbiDump() || library.sabi.Properties.CreateSAbiDumps {
Jayant Chowdharya4fce192017-09-06 13:10:03 -0700391 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800392 var SourceAbiFlags []string
393 for _, dir := range exportIncludeDirs.Strings() {
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700394 SourceAbiFlags = append(SourceAbiFlags, "-I"+dir)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800395 }
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700396 for _, reexportedInclude := range extractExportIncludesFromFlags(library.sabi.Properties.ReexportedIncludeFlags) {
397 SourceAbiFlags = append(SourceAbiFlags, reexportedInclude)
398 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800399 flags.SAbiFlags = SourceAbiFlags
400 total_length := len(library.baseCompiler.Properties.Srcs) + len(deps.GeneratedSources) + len(library.Properties.Shared.Srcs) +
401 len(library.Properties.Static.Srcs)
402 if total_length > 0 {
403 flags.SAbiDump = true
404 }
405 }
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700406 objs := library.baseCompiler.compile(ctx, flags, deps)
407 library.reuseObjects = objs
Colin Cross2f336352016-10-26 10:03:47 -0700408 buildFlags := flagsToBuilderFlags(flags)
Colin Crossb916a382016-07-29 17:28:03 -0700409
Colin Cross4d9c2d12016-07-29 12:48:20 -0700410 if library.static() {
Colin Cross2f336352016-10-26 10:03:47 -0700411 srcs := android.PathsForModuleSrc(ctx, library.Properties.Static.Srcs)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700412 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceStaticLibrary,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800413 srcs, library.baseCompiler.pathDeps, library.baseCompiler.cFlagsDeps))
Colin Crossa48ab5b2017-02-14 15:28:44 -0800414 } else if library.shared() {
Colin Cross2f336352016-10-26 10:03:47 -0700415 srcs := android.PathsForModuleSrc(ctx, library.Properties.Shared.Srcs)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700416 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceSharedLibrary,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800417 srcs, library.baseCompiler.pathDeps, library.baseCompiler.cFlagsDeps))
Colin Crossb916a382016-07-29 17:28:03 -0700418 }
419
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700420 return objs
Colin Crossb916a382016-07-29 17:28:03 -0700421}
422
423type libraryInterface interface {
424 getWholeStaticMissingDeps() []string
425 static() bool
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700426 objs() Objects
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700427 reuseObjs() (Objects, []string, android.Paths)
Colin Cross26c34ed2016-09-30 17:10:16 -0700428 toc() android.OptionalPath
Colin Crossb916a382016-07-29 17:28:03 -0700429
430 // Returns true if the build options for the module have selected a static or shared build
431 buildStatic() bool
432 buildShared() bool
433
434 // Sets whether a specific variant is static or shared
Colin Crossa48ab5b2017-02-14 15:28:44 -0800435 setStatic()
436 setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700437}
438
439func (library *libraryDecorator) getLibName(ctx ModuleContext) string {
440 name := library.libName
441 if name == "" {
dimitryd95964a2018-11-07 13:43:34 +0100442 name = String(library.Properties.Stem)
443 if name == "" {
444 name = ctx.baseModuleName()
445 }
Colin Crossb916a382016-07-29 17:28:03 -0700446 }
447
Logan Chienf3511742017-10-31 18:04:35 +0800448 if ctx.isVndkExt() {
449 name = ctx.getVndkExtendsModuleName()
450 }
451
Colin Crossb916a382016-07-29 17:28:03 -0700452 if ctx.Host() && Bool(library.Properties.Unique_host_soname) {
453 if !strings.HasSuffix(name, "-host") {
454 name = name + "-host"
455 }
456 }
457
Colin Crossa48ab5b2017-02-14 15:28:44 -0800458 return name + library.MutatedProperties.VariantName
Colin Crossb916a382016-07-29 17:28:03 -0700459}
460
Jiyong Parkda732bd2018-11-02 18:23:15 +0900461var versioningMacroNamesListMutex sync.Mutex
462
Colin Crossb916a382016-07-29 17:28:03 -0700463func (library *libraryDecorator) linkerInit(ctx BaseModuleContext) {
464 location := InstallInSystem
Dan Albert61f32122018-07-26 14:00:24 -0700465 if library.baseLinker.sanitize.inSanitizerDir() {
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700466 location = InstallInSanitizerDir
Colin Crossb916a382016-07-29 17:28:03 -0700467 }
468 library.baseInstaller.location = location
Colin Crossb916a382016-07-29 17:28:03 -0700469 library.baseLinker.linkerInit(ctx)
Jiyong Park7ed9de32018-10-15 22:25:07 +0900470 // Let baseLinker know whether this variant is for stubs or not, so that
471 // it can omit things that are not required for linking stubs.
472 library.baseLinker.dynamicProperties.BuildStubs = library.buildStubs()
Jiyong Parkda732bd2018-11-02 18:23:15 +0900473
474 if library.buildStubs() {
475 macroNames := versioningMacroNamesList(ctx.Config())
476 myName := versioningMacroName(ctx.ModuleName())
477 versioningMacroNamesListMutex.Lock()
478 defer versioningMacroNamesListMutex.Unlock()
479 if (*macroNames)[myName] == "" {
480 (*macroNames)[myName] = ctx.ModuleName()
481 } else if (*macroNames)[myName] != ctx.ModuleName() {
482 ctx.ModuleErrorf("Macro name %q for versioning conflicts with macro name from module %q ", myName, (*macroNames)[myName])
483 }
484 }
Colin Crossb916a382016-07-29 17:28:03 -0700485}
486
Colin Cross37047f12016-12-13 17:06:13 -0800487func (library *libraryDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700488 deps = library.baseLinker.linkerDeps(ctx, deps)
489
490 if library.static() {
491 deps.WholeStaticLibs = append(deps.WholeStaticLibs,
492 library.Properties.Static.Whole_static_libs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700493 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Static.Static_libs...)
494 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Static.Shared_libs...)
Colin Crossa48ab5b2017-02-14 15:28:44 -0800495 } else if library.shared() {
Dan Willemsen2e47b342016-11-17 01:02:25 -0800496 if ctx.toolchain().Bionic() && !Bool(library.baseLinker.Properties.Nocrt) {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700497 if !ctx.useSdk() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700498 deps.CrtBegin = "crtbegin_so"
499 deps.CrtEnd = "crtend_so"
500 } else {
Dan Albertebedf672016-11-08 15:06:22 -0800501 // TODO(danalbert): Add generation of crt objects.
502 // For `sdk_version: "current"`, we don't actually have a
503 // freshly generated set of CRT objects. Use the last stable
504 // version.
505 version := ctx.sdkVersion()
506 if version == "current" {
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -0700507 version = getCurrentNdkPrebuiltVersion(ctx)
Dan Albertebedf672016-11-08 15:06:22 -0800508 }
509 deps.CrtBegin = "ndk_crtbegin_so." + version
510 deps.CrtEnd = "ndk_crtend_so." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700511 }
512 }
513 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Shared.Whole_static_libs...)
514 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Shared.Static_libs...)
515 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Shared.Shared_libs...)
516 }
Jiyong Park52d25bd2017-10-13 09:17:01 +0900517 if ctx.useVndk() {
518 deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs)
519 deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Vendor.Exclude_shared_libs)
520 deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs)
521 }
Jiyong Parkf9332f12018-02-01 00:54:12 +0900522 if ctx.inRecovery() {
523 deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Recovery.Exclude_static_libs)
524 deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Recovery.Exclude_shared_libs)
525 deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Recovery.Exclude_static_libs)
526 }
Colin Cross2383f3b2018-02-06 14:40:13 -0800527
Colin Cross2383f3b2018-02-06 14:40:13 -0800528 android.ExtractSourceDeps(ctx, library.Properties.Unexported_symbols_list)
529 android.ExtractSourceDeps(ctx, library.Properties.Force_symbols_not_weak_list)
530 android.ExtractSourceDeps(ctx, library.Properties.Force_symbols_weak_list)
Colin Cross2383f3b2018-02-06 14:40:13 -0800531
Colin Cross4d9c2d12016-07-29 12:48:20 -0700532 return deps
533}
534
Colin Crossb916a382016-07-29 17:28:03 -0700535func (library *libraryDecorator) linkStatic(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700536 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700537
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700538 library.objects = deps.WholeStaticLibObjs.Copy()
539 library.objects = library.objects.Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700540
Colin Cross86803cf2018-02-15 14:12:26 -0800541 fileName := ctx.ModuleName() + library.MutatedProperties.VariantName + staticLibraryExtension
542 outputFile := android.PathForModuleOut(ctx, fileName)
Dan Willemsen581341d2017-02-09 16:16:31 -0800543 builderFlags := flagsToBuilderFlags(flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700544
Colin Cross86803cf2018-02-15 14:12:26 -0800545 if Bool(library.baseLinker.Properties.Use_version_lib) && ctx.Host() {
546 versionedOutputFile := outputFile
547 outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
548 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
549 }
550
Dan Willemsen581341d2017-02-09 16:16:31 -0800551 TransformObjToStaticLib(ctx, library.objects.objFiles, builderFlags, outputFile, objs.tidyFiles)
552
553 library.coverageOutputFile = TransformCoverageFilesToLib(ctx, library.objects, builderFlags,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800554 ctx.ModuleName()+library.MutatedProperties.VariantName)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700555
556 library.wholeStaticMissingDeps = ctx.GetMissingDependencies()
557
558 ctx.CheckbuildFile(outputFile)
559
560 return outputFile
561}
562
Colin Crossb916a382016-07-29 17:28:03 -0700563func (library *libraryDecorator) linkShared(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700564 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700565
566 var linkerDeps android.Paths
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700567 linkerDeps = append(linkerDeps, flags.LdFlagsDeps...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700568
Colin Cross2383f3b2018-02-06 14:40:13 -0800569 unexportedSymbols := ctx.ExpandOptionalSource(library.Properties.Unexported_symbols_list, "unexported_symbols_list")
570 forceNotWeakSymbols := ctx.ExpandOptionalSource(library.Properties.Force_symbols_not_weak_list, "force_symbols_not_weak_list")
571 forceWeakSymbols := ctx.ExpandOptionalSource(library.Properties.Force_symbols_weak_list, "force_symbols_weak_list")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700572 if !ctx.Darwin() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700573 if unexportedSymbols.Valid() {
574 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
575 }
576 if forceNotWeakSymbols.Valid() {
577 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
578 }
579 if forceWeakSymbols.Valid() {
580 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
581 }
582 } else {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700583 if unexportedSymbols.Valid() {
584 flags.LdFlags = append(flags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
585 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
586 }
587 if forceNotWeakSymbols.Valid() {
588 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
589 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
590 }
591 if forceWeakSymbols.Valid() {
592 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
593 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
594 }
595 }
596
597 fileName := library.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
598 outputFile := android.PathForModuleOut(ctx, fileName)
599 ret := outputFile
600
601 builderFlags := flagsToBuilderFlags(flags)
602
Colin Crossb496cfd2018-09-10 16:50:05 -0700603 // Optimize out relinking against shared libraries whose interface hasn't changed by
604 // depending on a table of contents file instead of the library itself.
605 tocPath := outputFile.RelPathString()
606 tocPath = pathtools.ReplaceExtension(tocPath, flags.Toolchain.ShlibSuffix()[1:]+".toc")
607 tocFile := android.PathForOutput(ctx, tocPath)
608 library.tocFile = android.OptionalPathForPath(tocFile)
609 TransformSharedObjectToToc(ctx, outputFile, tocFile, builderFlags)
Colin Cross89562dc2016-10-03 17:47:19 -0700610
Colin Cross4d9c2d12016-07-29 12:48:20 -0700611 if library.stripper.needsStrip(ctx) {
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -0700612 // b/80093681, GNU strip/objcopy bug.
613 // Use llvm-{strip,objcopy} when clang lld is used.
Dan Willemsen8536d6b2018-10-07 20:54:34 -0700614 builderFlags.stripUseLlvmStrip = library.baseLinker.useClangLld(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700615 strippedOutputFile := outputFile
616 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
617 library.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
618 }
619
Colin Crossb60190a2018-09-04 16:28:17 -0700620 library.unstrippedOutputFile = outputFile
621
Colin Cross86803cf2018-02-15 14:12:26 -0800622 if Bool(library.baseLinker.Properties.Use_version_lib) && ctx.Host() {
623 versionedOutputFile := outputFile
624 outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
625 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
626 }
627
Colin Cross4d9c2d12016-07-29 12:48:20 -0700628 sharedLibs := deps.SharedLibs
629 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
630
Colin Cross26c34ed2016-09-30 17:10:16 -0700631 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
632 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700633 linkerDeps = append(linkerDeps, objs.tidyFiles...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700634
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700635 TransformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700636 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
637 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile)
638
Dan Willemsen581341d2017-02-09 16:16:31 -0800639 objs.coverageFiles = append(objs.coverageFiles, deps.StaticLibObjs.coverageFiles...)
640 objs.coverageFiles = append(objs.coverageFiles, deps.WholeStaticLibObjs.coverageFiles...)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800641
642 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.StaticLibObjs.sAbiDumpFiles...)
643 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.WholeStaticLibObjs.sAbiDumpFiles...)
644
Dan Willemsen581341d2017-02-09 16:16:31 -0800645 library.coverageOutputFile = TransformCoverageFilesToLib(ctx, objs, builderFlags, library.getLibName(ctx))
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700646 library.linkSAbiDumpFiles(ctx, objs, fileName, ret)
Dan Willemsen581341d2017-02-09 16:16:31 -0800647
Colin Cross4d9c2d12016-07-29 12:48:20 -0700648 return ret
649}
650
Logan Chien7eefdc42018-07-11 18:10:41 +0800651func getRefAbiDumpFile(ctx ModuleContext, vndkVersion, fileName string) android.Path {
Logan Chienf4b79c62018-08-02 02:27:02 +0800652 isLlndk := inList(ctx.baseModuleName(), llndkLibraries) || inList(ctx.baseModuleName(), ndkMigratedLibs)
Logan Chien7eefdc42018-07-11 18:10:41 +0800653
654 refAbiDumpTextFile := android.PathForVndkRefAbiDump(ctx, vndkVersion, fileName, isLlndk, false)
655 refAbiDumpGzipFile := android.PathForVndkRefAbiDump(ctx, vndkVersion, fileName, isLlndk, true)
656
657 if refAbiDumpTextFile.Valid() {
658 if refAbiDumpGzipFile.Valid() {
659 ctx.ModuleErrorf(
660 "Two reference ABI dump files are found: %q and %q. Please delete the stale one.",
661 refAbiDumpTextFile, refAbiDumpGzipFile)
662 return nil
663 }
664 return refAbiDumpTextFile.Path()
665 }
666 if refAbiDumpGzipFile.Valid() {
667 return UnzipRefDump(ctx, refAbiDumpGzipFile.Path(), fileName)
668 }
669 return nil
670}
671
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700672func (library *libraryDecorator) linkSAbiDumpFiles(ctx ModuleContext, objs Objects, fileName string, soFile android.Path) {
Logan Chien2f2b8902018-07-10 15:01:19 +0800673 if len(objs.sAbiDumpFiles) > 0 && ctx.shouldCreateVndkSourceAbiDump() {
Logan Chiena8f51582018-03-21 11:53:48 +0800674 vndkVersion := ctx.DeviceConfig().PlatformVndkVersion()
675 if ver := ctx.DeviceConfig().VndkVersion(); ver != "" && ver != "current" {
Logan Chienf3511742017-10-31 18:04:35 +0800676 vndkVersion = ver
677 }
678
Jayant Chowdharya4fce192017-09-06 13:10:03 -0700679 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800680 var SourceAbiFlags []string
681 for _, dir := range exportIncludeDirs.Strings() {
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700682 SourceAbiFlags = append(SourceAbiFlags, "-I"+dir)
683 }
684 for _, reexportedInclude := range extractExportIncludesFromFlags(library.sabi.Properties.ReexportedIncludeFlags) {
685 SourceAbiFlags = append(SourceAbiFlags, reexportedInclude)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800686 }
687 exportedHeaderFlags := strings.Join(SourceAbiFlags, " ")
Jayant Chowdharydf344d52018-01-17 11:11:42 -0800688 library.sAbiOutputFile = TransformDumpToLinkedDump(ctx, objs.sAbiDumpFiles, soFile, fileName, exportedHeaderFlags)
Logan Chien2f2b8902018-07-10 15:01:19 +0800689
Logan Chien7eefdc42018-07-11 18:10:41 +0800690 refAbiDumpFile := getRefAbiDumpFile(ctx, vndkVersion, fileName)
691 if refAbiDumpFile != nil {
Logan Chienf3511742017-10-31 18:04:35 +0800692 library.sAbiDiff = SourceAbiDiff(ctx, library.sAbiOutputFile.Path(),
Logan Chien7eefdc42018-07-11 18:10:41 +0800693 refAbiDumpFile, fileName, exportedHeaderFlags, ctx.isVndkExt())
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800694 }
695 }
696}
697
Colin Crossb916a382016-07-29 17:28:03 -0700698func (library *libraryDecorator) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700699 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700700
Colin Crossad59e752017-11-16 14:29:11 -0800701 objs = deps.Objs.Copy().Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700702 var out android.Path
Colin Crossa48ab5b2017-02-14 15:28:44 -0800703 if library.static() || library.header() {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700704 out = library.linkStatic(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700705 } else {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700706 out = library.linkShared(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700707 }
708
709 library.exportIncludes(ctx, "-I")
710 library.reexportFlags(deps.ReexportedFlags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700711 library.reexportDeps(deps.ReexportedFlagsDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700712
Nan Zhang0007d812017-11-07 10:57:05 -0800713 if Bool(library.Properties.Aidl.Export_aidl_headers) {
Dan Willemsene1240db2016-11-03 14:28:51 -0700714 if library.baseCompiler.hasSrcExt(".aidl") {
Colin Cross10d22312017-05-03 11:01:58 -0700715 flags := []string{
Dan Willemsene1240db2016-11-03 14:28:51 -0700716 "-I" + android.PathForModuleGen(ctx, "aidl").String(),
Colin Cross10d22312017-05-03 11:01:58 -0700717 }
718 library.reexportFlags(flags)
719 library.reuseExportedFlags = append(library.reuseExportedFlags, flags...)
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800720 library.reexportDeps(library.baseCompiler.pathDeps) // TODO: restrict to aidl deps
721 library.reuseExportedDeps = append(library.reuseExportedDeps, library.baseCompiler.pathDeps...)
Dan Willemsene1240db2016-11-03 14:28:51 -0700722 }
723 }
724
Nan Zhang0007d812017-11-07 10:57:05 -0800725 if Bool(library.Properties.Proto.Export_proto_headers) {
Dan Willemsene1240db2016-11-03 14:28:51 -0700726 if library.baseCompiler.hasSrcExt(".proto") {
Dan Willemsenab9f4262018-02-14 13:58:34 -0800727 includes := []string{}
728 if flags.ProtoRoot {
729 includes = append(includes, "-I"+android.ProtoSubDir(ctx).String())
Colin Cross10d22312017-05-03 11:01:58 -0700730 }
Dan Willemsenab9f4262018-02-14 13:58:34 -0800731 includes = append(includes, "-I"+android.ProtoDir(ctx).String())
732 library.reexportFlags(includes)
733 library.reuseExportedFlags = append(library.reuseExportedFlags, includes...)
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800734 library.reexportDeps(library.baseCompiler.pathDeps) // TODO: restrict to proto deps
735 library.reuseExportedDeps = append(library.reuseExportedDeps, library.baseCompiler.pathDeps...)
Colin Cross0c461f12016-10-20 16:11:43 -0700736 }
737 }
738
Inseob Kim21f26902018-09-06 00:55:20 +0900739 if library.baseCompiler.hasSrcExt(".sysprop") {
740 flags := []string{
741 "-I" + android.PathForModuleGen(ctx, "sysprop", "include").String(),
742 }
743 library.reexportFlags(flags)
744 library.reuseExportedFlags = append(library.reuseExportedFlags, flags...)
745 }
746
Jiyong Parkda732bd2018-11-02 18:23:15 +0900747 if library.buildStubs() {
748 library.reexportFlags([]string{"-D" + versioningMacroName(ctx.ModuleName()) + "=" + library.stubsVersion()})
749 }
750
Colin Cross4d9c2d12016-07-29 12:48:20 -0700751 return out
752}
753
Colin Crossb916a382016-07-29 17:28:03 -0700754func (library *libraryDecorator) buildStatic() bool {
Colin Cross38b40df2018-04-10 16:14:46 -0700755 return library.MutatedProperties.BuildStatic && BoolDefault(library.Properties.Static.Enabled, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700756}
757
Colin Crossb916a382016-07-29 17:28:03 -0700758func (library *libraryDecorator) buildShared() bool {
Colin Cross38b40df2018-04-10 16:14:46 -0700759 return library.MutatedProperties.BuildShared && BoolDefault(library.Properties.Shared.Enabled, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700760}
761
Colin Crossb916a382016-07-29 17:28:03 -0700762func (library *libraryDecorator) getWholeStaticMissingDeps() []string {
Colin Crossd2343a32018-04-30 14:50:01 -0700763 return append([]string(nil), library.wholeStaticMissingDeps...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700764}
765
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700766func (library *libraryDecorator) objs() Objects {
767 return library.objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700768}
769
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700770func (library *libraryDecorator) reuseObjs() (Objects, []string, android.Paths) {
771 return library.reuseObjects, library.reuseExportedFlags, library.reuseExportedDeps
Colin Cross4d9c2d12016-07-29 12:48:20 -0700772}
773
Colin Cross26c34ed2016-09-30 17:10:16 -0700774func (library *libraryDecorator) toc() android.OptionalPath {
775 return library.tocFile
776}
777
Colin Crossb916a382016-07-29 17:28:03 -0700778func (library *libraryDecorator) install(ctx ModuleContext, file android.Path) {
Colin Crossc43ae772017-04-14 15:42:53 -0700779 if library.shared() {
Justin Yun8fe12122017-12-07 17:18:15 +0900780 if ctx.Device() && ctx.useVndk() {
781 if ctx.isVndkSp() {
782 library.baseInstaller.subDir = "vndk-sp"
783 } else if ctx.isVndk() {
784 library.baseInstaller.subDir = "vndk"
785 }
Logan Chienf3511742017-10-31 18:04:35 +0800786
787 // Append a version to vndk or vndk-sp directories on the system partition.
788 if ctx.isVndk() && !ctx.isVndkExt() {
789 vndkVersion := ctx.DeviceConfig().PlatformVndkVersion()
790 if vndkVersion != "current" && vndkVersion != "" {
791 library.baseInstaller.subDir += "-" + vndkVersion
792 }
Justin Yun8effde42017-06-23 19:24:43 +0900793 }
794 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700795 library.baseInstaller.install(ctx, file)
796 }
Dan Albertf563d252017-10-13 00:29:00 -0700797
Dan Albert281f22b2017-12-13 15:03:47 -0800798 if Bool(library.Properties.Static_ndk_lib) && library.static() &&
Jiyong Parkf9332f12018-02-01 00:54:12 +0900799 !ctx.useVndk() && !ctx.inRecovery() && ctx.Device() &&
Jiyong Park7ed9de32018-10-15 22:25:07 +0900800 library.baseLinker.sanitize.isUnsanitizedVariant() &&
801 !library.buildStubs() {
Dan Albertf563d252017-10-13 00:29:00 -0700802 installPath := getNdkSysrootBase(ctx).Join(
Dan Albertea4b7b92018-04-25 16:05:30 -0700803 ctx, "usr/lib", config.NDKTriple(ctx.toolchain()), file.Base())
Dan Albertf563d252017-10-13 00:29:00 -0700804
805 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
806 Rule: android.Cp,
807 Description: "install " + installPath.Base(),
808 Output: installPath,
809 Input: file,
810 })
811
Colin Cross0875c522017-11-28 17:34:01 -0800812 library.ndkSysrootPath = installPath
Dan Albertf563d252017-10-13 00:29:00 -0700813 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700814}
815
Colin Crossb916a382016-07-29 17:28:03 -0700816func (library *libraryDecorator) static() bool {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800817 return library.MutatedProperties.VariantIsStatic
Colin Cross4d9c2d12016-07-29 12:48:20 -0700818}
819
Colin Crossa48ab5b2017-02-14 15:28:44 -0800820func (library *libraryDecorator) shared() bool {
821 return library.MutatedProperties.VariantIsShared
822}
823
824func (library *libraryDecorator) header() bool {
825 return !library.static() && !library.shared()
826}
827
828func (library *libraryDecorator) setStatic() {
829 library.MutatedProperties.VariantIsStatic = true
830 library.MutatedProperties.VariantIsShared = false
831}
832
833func (library *libraryDecorator) setShared() {
834 library.MutatedProperties.VariantIsStatic = false
835 library.MutatedProperties.VariantIsShared = true
Colin Crossb916a382016-07-29 17:28:03 -0700836}
837
Colin Crossab3b7322016-12-09 14:46:15 -0800838func (library *libraryDecorator) BuildOnlyStatic() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800839 library.MutatedProperties.BuildShared = false
Colin Crossab3b7322016-12-09 14:46:15 -0800840}
841
842func (library *libraryDecorator) BuildOnlyShared() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800843 library.MutatedProperties.BuildStatic = false
Colin Crossab3b7322016-12-09 14:46:15 -0800844}
845
Colin Cross5950f382016-12-13 12:50:57 -0800846func (library *libraryDecorator) HeaderOnly() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800847 library.MutatedProperties.BuildShared = false
848 library.MutatedProperties.BuildStatic = false
Colin Cross5950f382016-12-13 12:50:57 -0800849}
850
Jiyong Park7ed9de32018-10-15 22:25:07 +0900851func (library *libraryDecorator) buildStubs() bool {
852 return library.MutatedProperties.BuildStubs
853}
854
855func (library *libraryDecorator) stubsVersion() string {
856 return library.MutatedProperties.StubsVersion
857}
858
Jiyong Parkda732bd2018-11-02 18:23:15 +0900859func versioningMacroNamesList(config android.Config) *map[string]string {
860 return config.Once("versioningMacroNamesList", func() interface{} {
861 m := make(map[string]string)
862 return &m
863 }).(*map[string]string)
864}
865
866// alphanumeric and _ characters are preserved.
867// other characters are all converted to _
868var charsNotForMacro = regexp.MustCompile("[^a-zA-Z0-9_]+")
869
870func versioningMacroName(moduleName string) string {
871 macroName := charsNotForMacro.ReplaceAllString(moduleName, "_")
872 macroName = strings.ToUpper(moduleName)
873 return "__" + macroName + "_API__"
874}
875
Colin Crossab3b7322016-12-09 14:46:15 -0800876func NewLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700877 module := newModule(hod, android.MultilibBoth)
878
Colin Crossb916a382016-07-29 17:28:03 -0700879 library := &libraryDecorator{
Colin Crossa48ab5b2017-02-14 15:28:44 -0800880 MutatedProperties: LibraryMutatedProperties{
Colin Crossab3b7322016-12-09 14:46:15 -0800881 BuildShared: true,
882 BuildStatic: true,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700883 },
Colin Crossb916a382016-07-29 17:28:03 -0700884 baseCompiler: NewBaseCompiler(),
Dan Albert61f32122018-07-26 14:00:24 -0700885 baseLinker: NewBaseLinker(module.sanitize),
Colin Crossb916a382016-07-29 17:28:03 -0700886 baseInstaller: NewBaseInstaller("lib", "lib64", InstallInSystem),
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800887 sabi: module.sabi,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700888 }
889
Colin Crossb916a382016-07-29 17:28:03 -0700890 module.compiler = library
891 module.linker = library
892 module.installer = library
893
894 return module, library
895}
896
Colin Cross10d22312017-05-03 11:01:58 -0700897// connects a shared library to a static library in order to reuse its .o files to avoid
898// compiling source files twice.
899func reuseStaticLibrary(mctx android.BottomUpMutatorContext, static, shared *Module) {
900 if staticCompiler, ok := static.compiler.(*libraryDecorator); ok {
901 sharedCompiler := shared.compiler.(*libraryDecorator)
902 if len(staticCompiler.Properties.Static.Cflags) == 0 &&
903 len(sharedCompiler.Properties.Shared.Cflags) == 0 {
904
905 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
906 sharedCompiler.baseCompiler.Properties.OriginalSrcs =
907 sharedCompiler.baseCompiler.Properties.Srcs
908 sharedCompiler.baseCompiler.Properties.Srcs = nil
909 sharedCompiler.baseCompiler.Properties.Generated_sources = nil
910 }
911 }
912}
913
Colin Crosse40b4ea2018-10-02 22:25:58 -0700914func LinkageMutator(mctx android.BottomUpMutatorContext) {
Colin Crossb916a382016-07-29 17:28:03 -0700915 if m, ok := mctx.Module().(*Module); ok && m.linker != nil {
916 if library, ok := m.linker.(libraryInterface); ok {
917 var modules []blueprint.Module
918 if library.buildStatic() && library.buildShared() {
919 modules = mctx.CreateLocalVariations("static", "shared")
920 static := modules[0].(*Module)
921 shared := modules[1].(*Module)
922
Colin Crossa48ab5b2017-02-14 15:28:44 -0800923 static.linker.(libraryInterface).setStatic()
924 shared.linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700925
Colin Cross10d22312017-05-03 11:01:58 -0700926 reuseStaticLibrary(mctx, static, shared)
927
Colin Crossb916a382016-07-29 17:28:03 -0700928 } else if library.buildStatic() {
929 modules = mctx.CreateLocalVariations("static")
Colin Crossa48ab5b2017-02-14 15:28:44 -0800930 modules[0].(*Module).linker.(libraryInterface).setStatic()
Colin Crossb916a382016-07-29 17:28:03 -0700931 } else if library.buildShared() {
932 modules = mctx.CreateLocalVariations("shared")
Colin Crossa48ab5b2017-02-14 15:28:44 -0800933 modules[0].(*Module).linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700934 }
935 }
936 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700937}
Jiyong Park7ed9de32018-10-15 22:25:07 +0900938
939// Version mutator splits a module into the mandatory non-stubs variant
940// (which is named "impl") and zero or more stubs variants.
941func versionMutator(mctx android.BottomUpMutatorContext) {
942 if mctx.Os() != android.Android {
943 return
944 }
945
946 if m, ok := mctx.Module().(*Module); ok && !m.inRecovery() && m.linker != nil {
947 if library, ok := m.linker.(*libraryDecorator); ok && library.buildShared() {
948 versions := []string{""}
949 for _, v := range library.Properties.Stubs.Versions {
950 versions = append(versions, v)
951 }
952 modules := mctx.CreateVariations(versions...)
953 for i, m := range modules {
954 l := m.(*Module).linker.(*libraryDecorator)
955 if i == 0 {
956 l.MutatedProperties.BuildStubs = false
957 continue
958 }
959 // Mark that this variant is for stubs.
960 l.MutatedProperties.BuildStubs = true
961 l.MutatedProperties.StubsVersion = versions[i]
962 m.(*Module).Properties.HideFromMake = true
963 }
964 } else {
965 mctx.CreateVariations("")
966 }
967 return
968 }
969 if genrule, ok := mctx.Module().(*genrule.Module); ok {
970 if props, ok := genrule.Extra.(*GenruleExtraProperties); ok && !props.InRecovery {
971 mctx.CreateVariations("")
972 return
973 }
974 }
975}