blob: bac566f7647ae820847ca6e0019b2970449400dc [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 (
18 "strings"
19
20 "github.com/google/blueprint"
Colin Cross26c34ed2016-09-30 17:10:16 -070021 "github.com/google/blueprint/pathtools"
Colin Cross4d9c2d12016-07-29 12:48:20 -070022
Colin Cross4d9c2d12016-07-29 12:48:20 -070023 "android/soong/android"
24)
25
Colin Crossb916a382016-07-29 17:28:03 -070026type LibraryProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -070027 Static struct {
Colin Cross2f336352016-10-26 10:03:47 -070028 Srcs []string `android:"arch_variant"`
29 Cflags []string `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070030
Colin Cross4d9c2d12016-07-29 12:48:20 -070031 Enabled *bool `android:"arch_variant"`
32 Whole_static_libs []string `android:"arch_variant"`
33 Static_libs []string `android:"arch_variant"`
34 Shared_libs []string `android:"arch_variant"`
35 } `android:"arch_variant"`
36 Shared struct {
Colin Cross2f336352016-10-26 10:03:47 -070037 Srcs []string `android:"arch_variant"`
38 Cflags []string `android:"arch_variant"`
Colin Crossb916a382016-07-29 17:28:03 -070039
Colin Cross4d9c2d12016-07-29 12:48:20 -070040 Enabled *bool `android:"arch_variant"`
41 Whole_static_libs []string `android:"arch_variant"`
42 Static_libs []string `android:"arch_variant"`
43 Shared_libs []string `android:"arch_variant"`
44 } `android:"arch_variant"`
45
46 // local file name to pass to the linker as --version_script
47 Version_script *string `android:"arch_variant"`
48 // local file name to pass to the linker as -unexported_symbols_list
49 Unexported_symbols_list *string `android:"arch_variant"`
50 // local file name to pass to the linker as -force_symbols_not_weak_list
51 Force_symbols_not_weak_list *string `android:"arch_variant"`
52 // local file name to pass to the linker as -force_symbols_weak_list
53 Force_symbols_weak_list *string `android:"arch_variant"`
54
55 // rename host libraries to prevent overlap with system installed libraries
56 Unique_host_soname *bool
57
Dan Willemsene1240db2016-11-03 14:28:51 -070058 Aidl struct {
59 // export headers generated from .aidl sources
Nan Zhang0007d812017-11-07 10:57:05 -080060 Export_aidl_headers *bool
Dan Willemsene1240db2016-11-03 14:28:51 -070061 }
62
Colin Cross0c461f12016-10-20 16:11:43 -070063 Proto struct {
64 // export headers generated from .proto sources
Nan Zhang0007d812017-11-07 10:57:05 -080065 Export_proto_headers *bool
Colin Cross0c461f12016-10-20 16:11:43 -070066 }
Jiyong Park52d25bd2017-10-13 09:17:01 +090067 Target struct {
68 Vendor struct {
69 // version script for this vendor variant
70 Version_script *string `android:"arch_variant"`
71 }
72 }
Dan Albertf563d252017-10-13 00:29:00 -070073
Nan Zhang0007d812017-11-07 10:57:05 -080074 Static_ndk_lib *bool
Colin Crossa48ab5b2017-02-14 15:28:44 -080075}
Colin Cross0c461f12016-10-20 16:11:43 -070076
Colin Crossa48ab5b2017-02-14 15:28:44 -080077type LibraryMutatedProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -070078 VariantName string `blueprint:"mutated"`
Colin Crossb916a382016-07-29 17:28:03 -070079
80 // Build a static variant
81 BuildStatic bool `blueprint:"mutated"`
82 // Build a shared variant
83 BuildShared bool `blueprint:"mutated"`
84 // This variant is shared
85 VariantIsShared bool `blueprint:"mutated"`
86 // This variant is static
87 VariantIsStatic bool `blueprint:"mutated"`
88}
89
90type FlagExporterProperties struct {
91 // list of directories relative to the Blueprints file that will
Dan Willemsen273af7f2016-11-03 15:53:42 -070092 // be added to the include path (using -I) for this module and any module that links
Colin Cross5d195602017-10-17 16:15:50 -070093 // against this module. Directories listed in export_include_dirs do not need to be
94 // listed in local_include_dirs.
Colin Crossb916a382016-07-29 17:28:03 -070095 Export_include_dirs []string `android:"arch_variant"`
Dan Willemsen4416e5d2017-04-06 12:43:22 -070096
97 Target struct {
98 Vendor struct {
99 // list of exported include directories, like
100 // export_include_dirs, that will be applied to the
101 // vendor variant of this library. This will overwrite
102 // any other declarations.
Steven Morelandb21df8f2018-01-05 14:42:54 -0800103 Override_export_include_dirs []string
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700104 }
105 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700106}
107
108func init() {
Steven Morelandf9e62162017-11-02 17:00:50 -0700109 android.RegisterModuleType("cc_library_static", LibraryStaticFactory)
110 android.RegisterModuleType("cc_library_shared", LibrarySharedFactory)
111 android.RegisterModuleType("cc_library", LibraryFactory)
112 android.RegisterModuleType("cc_library_host_static", LibraryHostStaticFactory)
113 android.RegisterModuleType("cc_library_host_shared", LibraryHostSharedFactory)
114 android.RegisterModuleType("cc_library_headers", LibraryHeaderFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700115}
116
117// Module factory for combined static + shared libraries, device by default but with possible host
118// support
Steven Morelandf9e62162017-11-02 17:00:50 -0700119func LibraryFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800120 module, _ := NewLibrary(android.HostAndDeviceSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700121 return module.Init()
122}
123
124// Module factory for static libraries
Steven Morelandf9e62162017-11-02 17:00:50 -0700125func LibraryStaticFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800126 module, library := NewLibrary(android.HostAndDeviceSupported)
127 library.BuildOnlyStatic()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700128 return module.Init()
129}
130
131// Module factory for shared libraries
Steven Morelandf9e62162017-11-02 17:00:50 -0700132func LibrarySharedFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800133 module, library := NewLibrary(android.HostAndDeviceSupported)
134 library.BuildOnlyShared()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700135 return module.Init()
136}
137
138// Module factory for host static libraries
Steven Morelandf9e62162017-11-02 17:00:50 -0700139func LibraryHostStaticFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800140 module, library := NewLibrary(android.HostSupported)
141 library.BuildOnlyStatic()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700142 return module.Init()
143}
144
145// Module factory for host shared libraries
Steven Morelandf9e62162017-11-02 17:00:50 -0700146func LibraryHostSharedFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800147 module, library := NewLibrary(android.HostSupported)
148 library.BuildOnlyShared()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700149 return module.Init()
150}
151
Colin Cross5950f382016-12-13 12:50:57 -0800152// Module factory for header-only libraries
Steven Morelandf9e62162017-11-02 17:00:50 -0700153func LibraryHeaderFactory() android.Module {
Colin Cross5950f382016-12-13 12:50:57 -0800154 module, library := NewLibrary(android.HostAndDeviceSupported)
155 library.HeaderOnly()
156 return module.Init()
157}
158
Colin Cross4d9c2d12016-07-29 12:48:20 -0700159type flagExporter struct {
160 Properties FlagExporterProperties
161
Dan Willemsen847dcc72016-09-29 12:13:36 -0700162 flags []string
163 flagsDeps android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700164}
165
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700166func (f *flagExporter) exportedIncludes(ctx ModuleContext) android.Paths {
Steven Morelandb21df8f2018-01-05 14:42:54 -0800167 if ctx.useVndk() && f.Properties.Target.Vendor.Override_export_include_dirs != nil {
168 return android.PathsForModuleSrc(ctx, f.Properties.Target.Vendor.Override_export_include_dirs)
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700169 } else {
170 return android.PathsForModuleSrc(ctx, f.Properties.Export_include_dirs)
171 }
172}
173
Colin Cross4d9c2d12016-07-29 12:48:20 -0700174func (f *flagExporter) exportIncludes(ctx ModuleContext, inc string) {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700175 includeDirs := f.exportedIncludes(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700176 for _, dir := range includeDirs.Strings() {
177 f.flags = append(f.flags, inc+dir)
178 }
179}
180
181func (f *flagExporter) reexportFlags(flags []string) {
182 f.flags = append(f.flags, flags...)
183}
184
Dan Willemsen847dcc72016-09-29 12:13:36 -0700185func (f *flagExporter) reexportDeps(deps android.Paths) {
186 f.flagsDeps = append(f.flagsDeps, deps...)
187}
188
Colin Cross4d9c2d12016-07-29 12:48:20 -0700189func (f *flagExporter) exportedFlags() []string {
190 return f.flags
191}
192
Dan Willemsen847dcc72016-09-29 12:13:36 -0700193func (f *flagExporter) exportedFlagsDeps() android.Paths {
194 return f.flagsDeps
195}
196
Colin Cross4d9c2d12016-07-29 12:48:20 -0700197type exportedFlagsProducer interface {
198 exportedFlags() []string
Dan Willemsen847dcc72016-09-29 12:13:36 -0700199 exportedFlagsDeps() android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700200}
201
202var _ exportedFlagsProducer = (*flagExporter)(nil)
203
Colin Crossb916a382016-07-29 17:28:03 -0700204// libraryDecorator wraps baseCompiler, baseLinker and baseInstaller to provide library-specific
205// functionality: static vs. shared linkage, reusing object files for shared libraries
206type libraryDecorator struct {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800207 Properties LibraryProperties
208 MutatedProperties LibraryMutatedProperties
Colin Cross4d9c2d12016-07-29 12:48:20 -0700209
210 // For reusing static library objects for shared library
Colin Cross10d22312017-05-03 11:01:58 -0700211 reuseObjects Objects
212 reuseExportedFlags []string
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700213 reuseExportedDeps android.Paths
Colin Cross10d22312017-05-03 11:01:58 -0700214
Colin Cross26c34ed2016-09-30 17:10:16 -0700215 // table-of-contents file to optimize out relinking when possible
216 tocFile android.OptionalPath
Colin Cross4d9c2d12016-07-29 12:48:20 -0700217
Colin Cross4d9c2d12016-07-29 12:48:20 -0700218 flagExporter
219 stripper
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700220 relocationPacker
Colin Cross4d9c2d12016-07-29 12:48:20 -0700221
Colin Cross4d9c2d12016-07-29 12:48:20 -0700222 // If we're used as a whole_static_lib, our missing dependencies need
223 // to be given
224 wholeStaticMissingDeps []string
225
226 // For whole_static_libs
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700227 objects Objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700228
229 // Uses the module's name if empty, but can be overridden. Does not include
230 // shlib suffix.
231 libName string
Colin Crossb916a382016-07-29 17:28:03 -0700232
233 sanitize *sanitize
234
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800235 sabi *sabi
236
Dan Willemsen581341d2017-02-09 16:16:31 -0800237 // Output archive of gcno coverage information files
238 coverageOutputFile android.OptionalPath
239
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800240 // linked Source Abi Dump
241 sAbiOutputFile android.OptionalPath
242
243 // Source Abi Diff
244 sAbiDiff android.OptionalPath
245
Colin Cross0875c522017-11-28 17:34:01 -0800246 // Location of the static library in the sysroot. Empty if the library is
247 // not included in the NDK.
248 ndkSysrootPath android.Path
249
Colin Crossb916a382016-07-29 17:28:03 -0700250 // Decorated interafaces
251 *baseCompiler
252 *baseLinker
253 *baseInstaller
Colin Cross4d9c2d12016-07-29 12:48:20 -0700254}
255
Colin Crossb916a382016-07-29 17:28:03 -0700256func (library *libraryDecorator) linkerProps() []interface{} {
257 var props []interface{}
258 props = append(props, library.baseLinker.linkerProps()...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700259 return append(props,
260 &library.Properties,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800261 &library.MutatedProperties,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700262 &library.flagExporter.Properties,
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700263 &library.stripper.StripProperties,
264 &library.relocationPacker.Properties)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700265}
266
Colin Crossb916a382016-07-29 17:28:03 -0700267func (library *libraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross42742b82016-08-01 13:20:05 -0700268 flags = library.baseLinker.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700269
Colin Crossb916a382016-07-29 17:28:03 -0700270 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
271 // all code is position independent, and then those warnings get promoted to
272 // errors.
Colin Cross3edeee12017-04-04 12:59:48 -0700273 if !ctx.Windows() {
Colin Crossb916a382016-07-29 17:28:03 -0700274 flags.CFlags = append(flags.CFlags, "-fPIC")
275 }
276
277 if library.static() {
278 flags.CFlags = append(flags.CFlags, library.Properties.Static.Cflags...)
Colin Crossa48ab5b2017-02-14 15:28:44 -0800279 } else if library.shared() {
Colin Crossb916a382016-07-29 17:28:03 -0700280 flags.CFlags = append(flags.CFlags, library.Properties.Shared.Cflags...)
281 }
282
Colin Crossa48ab5b2017-02-14 15:28:44 -0800283 if library.shared() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700284 libName := library.getLibName(ctx)
285 // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead
286 sharedFlag := "-Wl,-shared"
287 if flags.Clang || ctx.Host() {
288 sharedFlag = "-shared"
289 }
290 var f []string
Dan Willemsen01a405a2016-06-13 17:19:03 -0700291 if ctx.toolchain().Bionic() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700292 f = append(f,
293 "-nostdlib",
294 "-Wl,--gc-sections",
295 )
296 }
297
298 if ctx.Darwin() {
299 f = append(f,
300 "-dynamiclib",
301 "-single_module",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700302 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
303 )
Colin Cross7863cf52016-10-20 10:47:21 -0700304 if ctx.Arch().ArchType == android.X86 {
305 f = append(f,
306 "-read_only_relocs suppress",
307 )
308 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700309 } else {
310 f = append(f,
311 sharedFlag,
312 "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix())
313 }
314
315 flags.LdFlags = append(f, flags.LdFlags...)
316 }
317
318 return flags
319}
320
Colin Crossf18e1102017-11-16 14:33:08 -0800321func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700322 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700323 if len(exportIncludeDirs) > 0 {
Colin Crossdad8c952017-04-26 14:55:27 -0700324 f := includeDirsToFlags(exportIncludeDirs)
325 flags.GlobalFlags = append(flags.GlobalFlags, f)
326 flags.YasmFlags = append(flags.YasmFlags, f)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700327 }
328
Colin Crossf18e1102017-11-16 14:33:08 -0800329 return library.baseCompiler.compilerFlags(ctx, flags, deps)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700330}
331
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700332func extractExportIncludesFromFlags(flags []string) []string {
333 // This method is used in the generation of rules which produce
334 // abi-dumps for source files. Exported headers are needed to infer the
335 // abi exported by a library and filter out the rest of the abi dumped
336 // from a source. We extract the include flags exported by a library.
337 // This includes the flags exported which are re-exported from static
338 // library dependencies, exported header library dependencies and
Jayant Chowdharyaf6eb712017-08-23 16:08:29 -0700339 // generated header dependencies. -isystem headers are not included
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700340 // since for bionic libraries, abi-filtering is taken care of by version
341 // scripts.
342 var exportedIncludes []string
343 for _, flag := range flags {
344 if strings.HasPrefix(flag, "-I") {
345 exportedIncludes = append(exportedIncludes, flag)
346 }
347 }
348 return exportedIncludes
349}
350
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700351func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Colin Cross5950f382016-12-13 12:50:57 -0800352 if !library.buildShared() && !library.buildStatic() {
353 if len(library.baseCompiler.Properties.Srcs) > 0 {
354 ctx.PropertyErrorf("srcs", "cc_library_headers must not have any srcs")
355 }
356 if len(library.Properties.Static.Srcs) > 0 {
357 ctx.PropertyErrorf("static.srcs", "cc_library_headers must not have any srcs")
358 }
359 if len(library.Properties.Shared.Srcs) > 0 {
360 ctx.PropertyErrorf("shared.srcs", "cc_library_headers must not have any srcs")
361 }
362 return Objects{}
363 }
Jayant Chowdhary2a966402017-08-07 14:09:45 -0700364 if ctx.createVndkSourceAbiDump() || library.sabi.Properties.CreateSAbiDumps {
Jayant Chowdharya4fce192017-09-06 13:10:03 -0700365 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800366 var SourceAbiFlags []string
367 for _, dir := range exportIncludeDirs.Strings() {
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700368 SourceAbiFlags = append(SourceAbiFlags, "-I"+dir)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800369 }
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700370 for _, reexportedInclude := range extractExportIncludesFromFlags(library.sabi.Properties.ReexportedIncludeFlags) {
371 SourceAbiFlags = append(SourceAbiFlags, reexportedInclude)
372 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800373 flags.SAbiFlags = SourceAbiFlags
374 total_length := len(library.baseCompiler.Properties.Srcs) + len(deps.GeneratedSources) + len(library.Properties.Shared.Srcs) +
375 len(library.Properties.Static.Srcs)
376 if total_length > 0 {
377 flags.SAbiDump = true
378 }
379 }
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700380 objs := library.baseCompiler.compile(ctx, flags, deps)
381 library.reuseObjects = objs
Colin Cross2f336352016-10-26 10:03:47 -0700382 buildFlags := flagsToBuilderFlags(flags)
Colin Crossb916a382016-07-29 17:28:03 -0700383
Colin Cross4d9c2d12016-07-29 12:48:20 -0700384 if library.static() {
Colin Cross2f336352016-10-26 10:03:47 -0700385 srcs := android.PathsForModuleSrc(ctx, library.Properties.Static.Srcs)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700386 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceStaticLibrary,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800387 srcs, library.baseCompiler.pathDeps, library.baseCompiler.cFlagsDeps))
Colin Crossa48ab5b2017-02-14 15:28:44 -0800388 } else if library.shared() {
Colin Cross2f336352016-10-26 10:03:47 -0700389 srcs := android.PathsForModuleSrc(ctx, library.Properties.Shared.Srcs)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700390 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceSharedLibrary,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800391 srcs, library.baseCompiler.pathDeps, library.baseCompiler.cFlagsDeps))
Colin Crossb916a382016-07-29 17:28:03 -0700392 }
393
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700394 return objs
Colin Crossb916a382016-07-29 17:28:03 -0700395}
396
397type libraryInterface interface {
398 getWholeStaticMissingDeps() []string
399 static() bool
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700400 objs() Objects
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700401 reuseObjs() (Objects, []string, android.Paths)
Colin Cross26c34ed2016-09-30 17:10:16 -0700402 toc() android.OptionalPath
Colin Crossb916a382016-07-29 17:28:03 -0700403
404 // Returns true if the build options for the module have selected a static or shared build
405 buildStatic() bool
406 buildShared() bool
407
408 // Sets whether a specific variant is static or shared
Colin Crossa48ab5b2017-02-14 15:28:44 -0800409 setStatic()
410 setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700411}
412
413func (library *libraryDecorator) getLibName(ctx ModuleContext) string {
414 name := library.libName
415 if name == "" {
Colin Crossce75d2c2016-10-06 16:12:58 -0700416 name = ctx.baseModuleName()
Colin Crossb916a382016-07-29 17:28:03 -0700417 }
418
Logan Chienf3511742017-10-31 18:04:35 +0800419 if ctx.isVndkExt() {
420 name = ctx.getVndkExtendsModuleName()
421 }
422
Colin Crossb916a382016-07-29 17:28:03 -0700423 if ctx.Host() && Bool(library.Properties.Unique_host_soname) {
424 if !strings.HasSuffix(name, "-host") {
425 name = name + "-host"
426 }
427 }
428
Colin Crossa48ab5b2017-02-14 15:28:44 -0800429 return name + library.MutatedProperties.VariantName
Colin Crossb916a382016-07-29 17:28:03 -0700430}
431
432func (library *libraryDecorator) linkerInit(ctx BaseModuleContext) {
433 location := InstallInSystem
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700434 if library.sanitize.inSanitizerDir() {
435 location = InstallInSanitizerDir
Colin Crossb916a382016-07-29 17:28:03 -0700436 }
437 library.baseInstaller.location = location
438
439 library.baseLinker.linkerInit(ctx)
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700440
441 library.relocationPacker.packingInit(ctx)
Colin Crossb916a382016-07-29 17:28:03 -0700442}
443
Colin Cross37047f12016-12-13 17:06:13 -0800444func (library *libraryDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700445 deps = library.baseLinker.linkerDeps(ctx, deps)
446
447 if library.static() {
448 deps.WholeStaticLibs = append(deps.WholeStaticLibs,
449 library.Properties.Static.Whole_static_libs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700450 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Static.Static_libs...)
451 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Static.Shared_libs...)
Colin Crossa48ab5b2017-02-14 15:28:44 -0800452 } else if library.shared() {
Dan Willemsen2e47b342016-11-17 01:02:25 -0800453 if ctx.toolchain().Bionic() && !Bool(library.baseLinker.Properties.Nocrt) {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700454 if !ctx.useSdk() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700455 deps.CrtBegin = "crtbegin_so"
456 deps.CrtEnd = "crtend_so"
457 } else {
Dan Albertebedf672016-11-08 15:06:22 -0800458 // TODO(danalbert): Add generation of crt objects.
459 // For `sdk_version: "current"`, we don't actually have a
460 // freshly generated set of CRT objects. Use the last stable
461 // version.
462 version := ctx.sdkVersion()
463 if version == "current" {
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -0700464 version = getCurrentNdkPrebuiltVersion(ctx)
Dan Albertebedf672016-11-08 15:06:22 -0800465 }
466 deps.CrtBegin = "ndk_crtbegin_so." + version
467 deps.CrtEnd = "ndk_crtend_so." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700468 }
469 }
470 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Shared.Whole_static_libs...)
471 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Shared.Static_libs...)
472 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Shared.Shared_libs...)
473 }
Jiyong Park52d25bd2017-10-13 09:17:01 +0900474 if ctx.useVndk() {
475 deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs)
476 deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Vendor.Exclude_shared_libs)
477 deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs)
478 }
Colin Cross2383f3b2018-02-06 14:40:13 -0800479
480 android.ExtractSourceDeps(ctx, library.Properties.Version_script)
481 android.ExtractSourceDeps(ctx, library.Properties.Unexported_symbols_list)
482 android.ExtractSourceDeps(ctx, library.Properties.Force_symbols_not_weak_list)
483 android.ExtractSourceDeps(ctx, library.Properties.Force_symbols_weak_list)
484 android.ExtractSourceDeps(ctx, library.Properties.Target.Vendor.Version_script)
485
Colin Cross4d9c2d12016-07-29 12:48:20 -0700486 return deps
487}
488
Colin Crossb916a382016-07-29 17:28:03 -0700489func (library *libraryDecorator) linkStatic(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700490 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700491
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700492 library.objects = deps.WholeStaticLibObjs.Copy()
493 library.objects = library.objects.Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700494
Colin Cross86803cf2018-02-15 14:12:26 -0800495 fileName := ctx.ModuleName() + library.MutatedProperties.VariantName + staticLibraryExtension
496 outputFile := android.PathForModuleOut(ctx, fileName)
Dan Willemsen581341d2017-02-09 16:16:31 -0800497 builderFlags := flagsToBuilderFlags(flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700498
Colin Cross86803cf2018-02-15 14:12:26 -0800499 if Bool(library.baseLinker.Properties.Use_version_lib) && ctx.Host() {
500 versionedOutputFile := outputFile
501 outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
502 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
503 }
504
Dan Willemsen581341d2017-02-09 16:16:31 -0800505 TransformObjToStaticLib(ctx, library.objects.objFiles, builderFlags, outputFile, objs.tidyFiles)
506
507 library.coverageOutputFile = TransformCoverageFilesToLib(ctx, library.objects, builderFlags,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800508 ctx.ModuleName()+library.MutatedProperties.VariantName)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700509
510 library.wholeStaticMissingDeps = ctx.GetMissingDependencies()
511
512 ctx.CheckbuildFile(outputFile)
513
514 return outputFile
515}
516
Colin Crossb916a382016-07-29 17:28:03 -0700517func (library *libraryDecorator) linkShared(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700518 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700519
520 var linkerDeps android.Paths
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700521 linkerDeps = append(linkerDeps, flags.LdFlagsDeps...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700522
Colin Cross2383f3b2018-02-06 14:40:13 -0800523 versionScript := ctx.ExpandOptionalSource(library.Properties.Version_script, "version_script")
524 unexportedSymbols := ctx.ExpandOptionalSource(library.Properties.Unexported_symbols_list, "unexported_symbols_list")
525 forceNotWeakSymbols := ctx.ExpandOptionalSource(library.Properties.Force_symbols_not_weak_list, "force_symbols_not_weak_list")
526 forceWeakSymbols := ctx.ExpandOptionalSource(library.Properties.Force_symbols_weak_list, "force_symbols_weak_list")
Jiyong Park52d25bd2017-10-13 09:17:01 +0900527 if ctx.useVndk() && library.Properties.Target.Vendor.Version_script != nil {
Colin Cross2383f3b2018-02-06 14:40:13 -0800528 versionScript = ctx.ExpandOptionalSource(library.Properties.Target.Vendor.Version_script, "target.vendor.version_script")
Jiyong Park52d25bd2017-10-13 09:17:01 +0900529 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700530 if !ctx.Darwin() {
531 if versionScript.Valid() {
532 flags.LdFlags = append(flags.LdFlags, "-Wl,--version-script,"+versionScript.String())
533 linkerDeps = append(linkerDeps, versionScript.Path())
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000534 if library.sanitize.isSanitizerEnabled(cfi) {
535 flags.LdFlags = append(flags.LdFlags, "-Wl,--version-script,"+cfiExportsMap.String())
536 linkerDeps = append(linkerDeps, cfiExportsMap)
537 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700538 }
539 if unexportedSymbols.Valid() {
540 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
541 }
542 if forceNotWeakSymbols.Valid() {
543 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
544 }
545 if forceWeakSymbols.Valid() {
546 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
547 }
548 } else {
549 if versionScript.Valid() {
550 ctx.PropertyErrorf("version_script", "Not supported on Darwin")
551 }
552 if unexportedSymbols.Valid() {
553 flags.LdFlags = append(flags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
554 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
555 }
556 if forceNotWeakSymbols.Valid() {
557 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
558 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
559 }
560 if forceWeakSymbols.Valid() {
561 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
562 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
563 }
564 }
565
566 fileName := library.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
567 outputFile := android.PathForModuleOut(ctx, fileName)
568 ret := outputFile
569
570 builderFlags := flagsToBuilderFlags(flags)
571
Colin Crossd8f8d072017-04-04 13:00:15 -0700572 if !ctx.Darwin() && !ctx.Windows() {
Colin Cross89562dc2016-10-03 17:47:19 -0700573 // Optimize out relinking against shared libraries whose interface hasn't changed by
574 // depending on a table of contents file instead of the library itself.
575 tocPath := outputFile.RelPathString()
576 tocPath = pathtools.ReplaceExtension(tocPath, flags.Toolchain.ShlibSuffix()[1:]+".toc")
577 tocFile := android.PathForOutput(ctx, tocPath)
578 library.tocFile = android.OptionalPathForPath(tocFile)
579 TransformSharedObjectToToc(ctx, outputFile, tocFile, builderFlags)
580 }
581
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700582 if library.relocationPacker.needsPacking(ctx) {
583 packedOutputFile := outputFile
584 outputFile = android.PathForModuleOut(ctx, "unpacked", fileName)
585 library.relocationPacker.pack(ctx, outputFile, packedOutputFile, builderFlags)
586 }
587
Colin Cross4d9c2d12016-07-29 12:48:20 -0700588 if library.stripper.needsStrip(ctx) {
589 strippedOutputFile := outputFile
590 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
591 library.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
592 }
593
Colin Cross86803cf2018-02-15 14:12:26 -0800594 if Bool(library.baseLinker.Properties.Use_version_lib) && ctx.Host() {
595 versionedOutputFile := outputFile
596 outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
597 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
598 }
599
Colin Cross4d9c2d12016-07-29 12:48:20 -0700600 sharedLibs := deps.SharedLibs
601 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
602
Dan Albertd015c4a2016-08-10 14:34:08 -0700603 // TODO(danalbert): Clean this up when soong supports prebuilts.
604 if strings.HasPrefix(ctx.selectedStl(), "ndk_libc++") {
Dan Albertc9460bb2017-12-20 15:51:30 -0800605 libDir := getNdkStlLibDir(ctx, "libc++")
Dan Albertd015c4a2016-08-10 14:34:08 -0700606
607 if strings.HasSuffix(ctx.selectedStl(), "_shared") {
608 deps.StaticLibs = append(deps.StaticLibs,
609 libDir.Join(ctx, "libandroid_support.a"))
610 } else {
611 deps.StaticLibs = append(deps.StaticLibs,
612 libDir.Join(ctx, "libc++abi.a"),
613 libDir.Join(ctx, "libandroid_support.a"))
614 }
615
616 if ctx.Arch().ArchType == android.Arm {
617 deps.StaticLibs = append(deps.StaticLibs,
618 libDir.Join(ctx, "libunwind.a"))
619 }
620 }
621
Colin Cross26c34ed2016-09-30 17:10:16 -0700622 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
623 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700624 linkerDeps = append(linkerDeps, objs.tidyFiles...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700625
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700626 TransformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700627 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
628 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile)
629
Dan Willemsen581341d2017-02-09 16:16:31 -0800630 objs.coverageFiles = append(objs.coverageFiles, deps.StaticLibObjs.coverageFiles...)
631 objs.coverageFiles = append(objs.coverageFiles, deps.WholeStaticLibObjs.coverageFiles...)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800632
633 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.StaticLibObjs.sAbiDumpFiles...)
634 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.WholeStaticLibObjs.sAbiDumpFiles...)
635
Dan Willemsen581341d2017-02-09 16:16:31 -0800636 library.coverageOutputFile = TransformCoverageFilesToLib(ctx, objs, builderFlags, library.getLibName(ctx))
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700637 library.linkSAbiDumpFiles(ctx, objs, fileName, ret)
Dan Willemsen581341d2017-02-09 16:16:31 -0800638
Colin Cross4d9c2d12016-07-29 12:48:20 -0700639 return ret
640}
641
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700642func (library *libraryDecorator) linkSAbiDumpFiles(ctx ModuleContext, objs Objects, fileName string, soFile android.Path) {
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800643 //Also take into account object re-use.
Justin Yun8effde42017-06-23 19:24:43 +0900644 if len(objs.sAbiDumpFiles) > 0 && ctx.createVndkSourceAbiDump() {
Logan Chiena8f51582018-03-21 11:53:48 +0800645 vndkVersion := ctx.DeviceConfig().PlatformVndkVersion()
646 if ver := ctx.DeviceConfig().VndkVersion(); ver != "" && ver != "current" {
Logan Chienf3511742017-10-31 18:04:35 +0800647 vndkVersion = ver
648 }
649
650 refSourceDumpFile := android.PathForVndkRefAbiDump(ctx, vndkVersion, fileName, vndkVsNdk(ctx), true)
Jayant Chowdharya4fce192017-09-06 13:10:03 -0700651 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800652 var SourceAbiFlags []string
653 for _, dir := range exportIncludeDirs.Strings() {
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700654 SourceAbiFlags = append(SourceAbiFlags, "-I"+dir)
655 }
656 for _, reexportedInclude := range extractExportIncludesFromFlags(library.sabi.Properties.ReexportedIncludeFlags) {
657 SourceAbiFlags = append(SourceAbiFlags, reexportedInclude)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800658 }
659 exportedHeaderFlags := strings.Join(SourceAbiFlags, " ")
Jayant Chowdharydf344d52018-01-17 11:11:42 -0800660 library.sAbiOutputFile = TransformDumpToLinkedDump(ctx, objs.sAbiDumpFiles, soFile, fileName, exportedHeaderFlags)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800661 if refSourceDumpFile.Valid() {
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700662 unzippedRefDump := UnzipRefDump(ctx, refSourceDumpFile.Path(), fileName)
Logan Chienf3511742017-10-31 18:04:35 +0800663 library.sAbiDiff = SourceAbiDiff(ctx, library.sAbiOutputFile.Path(),
664 unzippedRefDump, fileName, exportedHeaderFlags, ctx.isVndkExt())
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800665 }
666 }
667}
668
669func vndkVsNdk(ctx ModuleContext) bool {
Jiyong Parkab0fd5f2017-08-03 21:22:50 +0900670 if inList(ctx.baseModuleName(), llndkLibraries) {
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800671 return false
672 }
673 return true
674}
675
Colin Crossb916a382016-07-29 17:28:03 -0700676func (library *libraryDecorator) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700677 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700678
Colin Crossad59e752017-11-16 14:29:11 -0800679 objs = deps.Objs.Copy().Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700680 var out android.Path
Colin Crossa48ab5b2017-02-14 15:28:44 -0800681 if library.static() || library.header() {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700682 out = library.linkStatic(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700683 } else {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700684 out = library.linkShared(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700685 }
686
687 library.exportIncludes(ctx, "-I")
688 library.reexportFlags(deps.ReexportedFlags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700689 library.reexportDeps(deps.ReexportedFlagsDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700690
Nan Zhang0007d812017-11-07 10:57:05 -0800691 if Bool(library.Properties.Aidl.Export_aidl_headers) {
Dan Willemsene1240db2016-11-03 14:28:51 -0700692 if library.baseCompiler.hasSrcExt(".aidl") {
Colin Cross10d22312017-05-03 11:01:58 -0700693 flags := []string{
Dan Willemsene1240db2016-11-03 14:28:51 -0700694 "-I" + android.PathForModuleGen(ctx, "aidl").String(),
Colin Cross10d22312017-05-03 11:01:58 -0700695 }
696 library.reexportFlags(flags)
697 library.reuseExportedFlags = append(library.reuseExportedFlags, flags...)
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800698 library.reexportDeps(library.baseCompiler.pathDeps) // TODO: restrict to aidl deps
699 library.reuseExportedDeps = append(library.reuseExportedDeps, library.baseCompiler.pathDeps...)
Dan Willemsene1240db2016-11-03 14:28:51 -0700700 }
701 }
702
Nan Zhang0007d812017-11-07 10:57:05 -0800703 if Bool(library.Properties.Proto.Export_proto_headers) {
Dan Willemsene1240db2016-11-03 14:28:51 -0700704 if library.baseCompiler.hasSrcExt(".proto") {
Dan Willemsenab9f4262018-02-14 13:58:34 -0800705 includes := []string{}
706 if flags.ProtoRoot {
707 includes = append(includes, "-I"+android.ProtoSubDir(ctx).String())
Colin Cross10d22312017-05-03 11:01:58 -0700708 }
Dan Willemsenab9f4262018-02-14 13:58:34 -0800709 includes = append(includes, "-I"+android.ProtoDir(ctx).String())
710 library.reexportFlags(includes)
711 library.reuseExportedFlags = append(library.reuseExportedFlags, includes...)
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800712 library.reexportDeps(library.baseCompiler.pathDeps) // TODO: restrict to proto deps
713 library.reuseExportedDeps = append(library.reuseExportedDeps, library.baseCompiler.pathDeps...)
Colin Cross0c461f12016-10-20 16:11:43 -0700714 }
715 }
716
Colin Cross4d9c2d12016-07-29 12:48:20 -0700717 return out
718}
719
Colin Crossb916a382016-07-29 17:28:03 -0700720func (library *libraryDecorator) buildStatic() bool {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800721 return library.MutatedProperties.BuildStatic &&
Colin Cross4d9c2d12016-07-29 12:48:20 -0700722 (library.Properties.Static.Enabled == nil || *library.Properties.Static.Enabled)
723}
724
Colin Crossb916a382016-07-29 17:28:03 -0700725func (library *libraryDecorator) buildShared() bool {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800726 return library.MutatedProperties.BuildShared &&
Colin Cross4d9c2d12016-07-29 12:48:20 -0700727 (library.Properties.Shared.Enabled == nil || *library.Properties.Shared.Enabled)
728}
729
Colin Crossb916a382016-07-29 17:28:03 -0700730func (library *libraryDecorator) getWholeStaticMissingDeps() []string {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700731 return library.wholeStaticMissingDeps
732}
733
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700734func (library *libraryDecorator) objs() Objects {
735 return library.objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700736}
737
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700738func (library *libraryDecorator) reuseObjs() (Objects, []string, android.Paths) {
739 return library.reuseObjects, library.reuseExportedFlags, library.reuseExportedDeps
Colin Cross4d9c2d12016-07-29 12:48:20 -0700740}
741
Colin Cross26c34ed2016-09-30 17:10:16 -0700742func (library *libraryDecorator) toc() android.OptionalPath {
743 return library.tocFile
744}
745
Colin Crossb916a382016-07-29 17:28:03 -0700746func (library *libraryDecorator) install(ctx ModuleContext, file android.Path) {
Colin Crossc43ae772017-04-14 15:42:53 -0700747 if library.shared() {
Justin Yun8fe12122017-12-07 17:18:15 +0900748 if ctx.Device() && ctx.useVndk() {
749 if ctx.isVndkSp() {
750 library.baseInstaller.subDir = "vndk-sp"
751 } else if ctx.isVndk() {
752 library.baseInstaller.subDir = "vndk"
753 }
Logan Chienf3511742017-10-31 18:04:35 +0800754
755 // Append a version to vndk or vndk-sp directories on the system partition.
756 if ctx.isVndk() && !ctx.isVndkExt() {
757 vndkVersion := ctx.DeviceConfig().PlatformVndkVersion()
758 if vndkVersion != "current" && vndkVersion != "" {
759 library.baseInstaller.subDir += "-" + vndkVersion
760 }
Justin Yun8effde42017-06-23 19:24:43 +0900761 }
762 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700763 library.baseInstaller.install(ctx, file)
764 }
Dan Albertf563d252017-10-13 00:29:00 -0700765
Dan Albert281f22b2017-12-13 15:03:47 -0800766 if Bool(library.Properties.Static_ndk_lib) && library.static() &&
Dan Albert7d1eecf2018-01-19 12:30:45 -0800767 !ctx.useVndk() && ctx.Device() &&
768 library.sanitize.isUnsanitizedVariant() {
Dan Albertf563d252017-10-13 00:29:00 -0700769 installPath := getNdkSysrootBase(ctx).Join(
770 ctx, "usr/lib", ctx.toolchain().ClangTriple(), file.Base())
771
772 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
773 Rule: android.Cp,
774 Description: "install " + installPath.Base(),
775 Output: installPath,
776 Input: file,
777 })
778
Colin Cross0875c522017-11-28 17:34:01 -0800779 library.ndkSysrootPath = installPath
Dan Albertf563d252017-10-13 00:29:00 -0700780 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700781}
782
Colin Crossb916a382016-07-29 17:28:03 -0700783func (library *libraryDecorator) static() bool {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800784 return library.MutatedProperties.VariantIsStatic
Colin Cross4d9c2d12016-07-29 12:48:20 -0700785}
786
Colin Crossa48ab5b2017-02-14 15:28:44 -0800787func (library *libraryDecorator) shared() bool {
788 return library.MutatedProperties.VariantIsShared
789}
790
791func (library *libraryDecorator) header() bool {
792 return !library.static() && !library.shared()
793}
794
795func (library *libraryDecorator) setStatic() {
796 library.MutatedProperties.VariantIsStatic = true
797 library.MutatedProperties.VariantIsShared = false
798}
799
800func (library *libraryDecorator) setShared() {
801 library.MutatedProperties.VariantIsStatic = false
802 library.MutatedProperties.VariantIsShared = true
Colin Crossb916a382016-07-29 17:28:03 -0700803}
804
Colin Crossab3b7322016-12-09 14:46:15 -0800805func (library *libraryDecorator) BuildOnlyStatic() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800806 library.MutatedProperties.BuildShared = false
Colin Crossab3b7322016-12-09 14:46:15 -0800807}
808
809func (library *libraryDecorator) BuildOnlyShared() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800810 library.MutatedProperties.BuildStatic = false
Colin Crossab3b7322016-12-09 14:46:15 -0800811}
812
Colin Cross5950f382016-12-13 12:50:57 -0800813func (library *libraryDecorator) HeaderOnly() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800814 library.MutatedProperties.BuildShared = false
815 library.MutatedProperties.BuildStatic = false
Colin Cross5950f382016-12-13 12:50:57 -0800816}
817
Colin Crossab3b7322016-12-09 14:46:15 -0800818func NewLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700819 module := newModule(hod, android.MultilibBoth)
820
Colin Crossb916a382016-07-29 17:28:03 -0700821 library := &libraryDecorator{
Colin Crossa48ab5b2017-02-14 15:28:44 -0800822 MutatedProperties: LibraryMutatedProperties{
Colin Crossab3b7322016-12-09 14:46:15 -0800823 BuildShared: true,
824 BuildStatic: true,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700825 },
Colin Crossb916a382016-07-29 17:28:03 -0700826 baseCompiler: NewBaseCompiler(),
827 baseLinker: NewBaseLinker(),
828 baseInstaller: NewBaseInstaller("lib", "lib64", InstallInSystem),
829 sanitize: module.sanitize,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800830 sabi: module.sabi,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700831 }
832
Colin Crossb916a382016-07-29 17:28:03 -0700833 module.compiler = library
834 module.linker = library
835 module.installer = library
836
837 return module, library
838}
839
Colin Cross10d22312017-05-03 11:01:58 -0700840// connects a shared library to a static library in order to reuse its .o files to avoid
841// compiling source files twice.
842func reuseStaticLibrary(mctx android.BottomUpMutatorContext, static, shared *Module) {
843 if staticCompiler, ok := static.compiler.(*libraryDecorator); ok {
844 sharedCompiler := shared.compiler.(*libraryDecorator)
845 if len(staticCompiler.Properties.Static.Cflags) == 0 &&
846 len(sharedCompiler.Properties.Shared.Cflags) == 0 {
847
848 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
849 sharedCompiler.baseCompiler.Properties.OriginalSrcs =
850 sharedCompiler.baseCompiler.Properties.Srcs
851 sharedCompiler.baseCompiler.Properties.Srcs = nil
852 sharedCompiler.baseCompiler.Properties.Generated_sources = nil
853 }
854 }
855}
856
Colin Crossb916a382016-07-29 17:28:03 -0700857func linkageMutator(mctx android.BottomUpMutatorContext) {
858 if m, ok := mctx.Module().(*Module); ok && m.linker != nil {
859 if library, ok := m.linker.(libraryInterface); ok {
860 var modules []blueprint.Module
861 if library.buildStatic() && library.buildShared() {
862 modules = mctx.CreateLocalVariations("static", "shared")
863 static := modules[0].(*Module)
864 shared := modules[1].(*Module)
865
Colin Crossa48ab5b2017-02-14 15:28:44 -0800866 static.linker.(libraryInterface).setStatic()
867 shared.linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700868
Colin Cross10d22312017-05-03 11:01:58 -0700869 reuseStaticLibrary(mctx, static, shared)
870
Colin Crossb916a382016-07-29 17:28:03 -0700871 } else if library.buildStatic() {
872 modules = mctx.CreateLocalVariations("static")
Colin Crossa48ab5b2017-02-14 15:28:44 -0800873 modules[0].(*Module).linker.(libraryInterface).setStatic()
Colin Crossb916a382016-07-29 17:28:03 -0700874 } else if library.buildShared() {
875 modules = mctx.CreateLocalVariations("shared")
Colin Crossa48ab5b2017-02-14 15:28:44 -0800876 modules[0].(*Module).linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700877 }
878 }
879 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700880}