blob: 4f9b5b2b4bc20550a4b5db6b0f30928fca312e93 [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"
Dan Albertea4b7b92018-04-25 16:05:30 -070024 "android/soong/cc/config"
Colin Cross4d9c2d12016-07-29 12:48:20 -070025)
26
Colin Crossb916a382016-07-29 17:28:03 -070027type LibraryProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -070028 Static struct {
Colin Cross2f336352016-10-26 10:03:47 -070029 Srcs []string `android:"arch_variant"`
30 Cflags []string `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070031
Colin Cross4d9c2d12016-07-29 12:48:20 -070032 Enabled *bool `android:"arch_variant"`
33 Whole_static_libs []string `android:"arch_variant"`
34 Static_libs []string `android:"arch_variant"`
35 Shared_libs []string `android:"arch_variant"`
36 } `android:"arch_variant"`
37 Shared struct {
Colin Cross2f336352016-10-26 10:03:47 -070038 Srcs []string `android:"arch_variant"`
39 Cflags []string `android:"arch_variant"`
Colin Crossb916a382016-07-29 17:28:03 -070040
Colin Cross4d9c2d12016-07-29 12:48:20 -070041 Enabled *bool `android:"arch_variant"`
42 Whole_static_libs []string `android:"arch_variant"`
43 Static_libs []string `android:"arch_variant"`
44 Shared_libs []string `android:"arch_variant"`
45 } `android:"arch_variant"`
46
Colin Cross4d9c2d12016-07-29 12:48:20 -070047 // local file name to pass to the linker as -unexported_symbols_list
48 Unexported_symbols_list *string `android:"arch_variant"`
49 // local file name to pass to the linker as -force_symbols_not_weak_list
50 Force_symbols_not_weak_list *string `android:"arch_variant"`
51 // local file name to pass to the linker as -force_symbols_weak_list
52 Force_symbols_weak_list *string `android:"arch_variant"`
53
54 // rename host libraries to prevent overlap with system installed libraries
55 Unique_host_soname *bool
56
Dan Willemsene1240db2016-11-03 14:28:51 -070057 Aidl struct {
58 // export headers generated from .aidl sources
Nan Zhang0007d812017-11-07 10:57:05 -080059 Export_aidl_headers *bool
Dan Willemsene1240db2016-11-03 14:28:51 -070060 }
61
Colin Cross0c461f12016-10-20 16:11:43 -070062 Proto struct {
63 // export headers generated from .proto sources
Nan Zhang0007d812017-11-07 10:57:05 -080064 Export_proto_headers *bool
Colin Cross0c461f12016-10-20 16:11:43 -070065 }
Dan Albertf563d252017-10-13 00:29:00 -070066
Nan Zhang0007d812017-11-07 10:57:05 -080067 Static_ndk_lib *bool
Colin Crossa48ab5b2017-02-14 15:28:44 -080068}
Colin Cross0c461f12016-10-20 16:11:43 -070069
Colin Crossa48ab5b2017-02-14 15:28:44 -080070type LibraryMutatedProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -070071 VariantName string `blueprint:"mutated"`
Colin Crossb916a382016-07-29 17:28:03 -070072
73 // Build a static variant
74 BuildStatic bool `blueprint:"mutated"`
75 // Build a shared variant
76 BuildShared bool `blueprint:"mutated"`
77 // This variant is shared
78 VariantIsShared bool `blueprint:"mutated"`
79 // This variant is static
80 VariantIsStatic bool `blueprint:"mutated"`
81}
82
83type FlagExporterProperties struct {
84 // list of directories relative to the Blueprints file that will
Dan Willemsen273af7f2016-11-03 15:53:42 -070085 // be added to the include path (using -I) for this module and any module that links
Colin Cross5d195602017-10-17 16:15:50 -070086 // against this module. Directories listed in export_include_dirs do not need to be
87 // listed in local_include_dirs.
Colin Crossb916a382016-07-29 17:28:03 -070088 Export_include_dirs []string `android:"arch_variant"`
Dan Willemsen4416e5d2017-04-06 12:43:22 -070089
90 Target struct {
91 Vendor struct {
92 // list of exported include directories, like
93 // export_include_dirs, that will be applied to the
94 // vendor variant of this library. This will overwrite
95 // any other declarations.
Steven Morelandb21df8f2018-01-05 14:42:54 -080096 Override_export_include_dirs []string
Dan Willemsen4416e5d2017-04-06 12:43:22 -070097 }
98 }
Colin Cross4d9c2d12016-07-29 12:48:20 -070099}
100
101func init() {
Steven Morelandf9e62162017-11-02 17:00:50 -0700102 android.RegisterModuleType("cc_library_static", LibraryStaticFactory)
103 android.RegisterModuleType("cc_library_shared", LibrarySharedFactory)
104 android.RegisterModuleType("cc_library", LibraryFactory)
105 android.RegisterModuleType("cc_library_host_static", LibraryHostStaticFactory)
106 android.RegisterModuleType("cc_library_host_shared", LibraryHostSharedFactory)
107 android.RegisterModuleType("cc_library_headers", LibraryHeaderFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700108}
109
110// Module factory for combined static + shared libraries, device by default but with possible host
111// support
Steven Morelandf9e62162017-11-02 17:00:50 -0700112func LibraryFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800113 module, _ := NewLibrary(android.HostAndDeviceSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700114 return module.Init()
115}
116
117// Module factory for static libraries
Steven Morelandf9e62162017-11-02 17:00:50 -0700118func LibraryStaticFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800119 module, library := NewLibrary(android.HostAndDeviceSupported)
120 library.BuildOnlyStatic()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700121 return module.Init()
122}
123
124// Module factory for shared libraries
Steven Morelandf9e62162017-11-02 17:00:50 -0700125func LibrarySharedFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800126 module, library := NewLibrary(android.HostAndDeviceSupported)
127 library.BuildOnlyShared()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700128 return module.Init()
129}
130
131// Module factory for host static libraries
Steven Morelandf9e62162017-11-02 17:00:50 -0700132func LibraryHostStaticFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800133 module, library := NewLibrary(android.HostSupported)
134 library.BuildOnlyStatic()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700135 return module.Init()
136}
137
138// Module factory for host shared libraries
Steven Morelandf9e62162017-11-02 17:00:50 -0700139func LibraryHostSharedFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800140 module, library := NewLibrary(android.HostSupported)
141 library.BuildOnlyShared()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700142 return module.Init()
143}
144
Colin Cross5950f382016-12-13 12:50:57 -0800145// Module factory for header-only libraries
Steven Morelandf9e62162017-11-02 17:00:50 -0700146func LibraryHeaderFactory() android.Module {
Colin Cross5950f382016-12-13 12:50:57 -0800147 module, library := NewLibrary(android.HostAndDeviceSupported)
148 library.HeaderOnly()
149 return module.Init()
150}
151
Colin Cross4d9c2d12016-07-29 12:48:20 -0700152type flagExporter struct {
153 Properties FlagExporterProperties
154
Dan Willemsen847dcc72016-09-29 12:13:36 -0700155 flags []string
156 flagsDeps android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700157}
158
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700159func (f *flagExporter) exportedIncludes(ctx ModuleContext) android.Paths {
Steven Morelandb21df8f2018-01-05 14:42:54 -0800160 if ctx.useVndk() && f.Properties.Target.Vendor.Override_export_include_dirs != nil {
161 return android.PathsForModuleSrc(ctx, f.Properties.Target.Vendor.Override_export_include_dirs)
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700162 } else {
163 return android.PathsForModuleSrc(ctx, f.Properties.Export_include_dirs)
164 }
165}
166
Colin Cross4d9c2d12016-07-29 12:48:20 -0700167func (f *flagExporter) exportIncludes(ctx ModuleContext, inc string) {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700168 includeDirs := f.exportedIncludes(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700169 for _, dir := range includeDirs.Strings() {
170 f.flags = append(f.flags, inc+dir)
171 }
172}
173
174func (f *flagExporter) reexportFlags(flags []string) {
175 f.flags = append(f.flags, flags...)
176}
177
Dan Willemsen847dcc72016-09-29 12:13:36 -0700178func (f *flagExporter) reexportDeps(deps android.Paths) {
179 f.flagsDeps = append(f.flagsDeps, deps...)
180}
181
Colin Cross4d9c2d12016-07-29 12:48:20 -0700182func (f *flagExporter) exportedFlags() []string {
183 return f.flags
184}
185
Dan Willemsen847dcc72016-09-29 12:13:36 -0700186func (f *flagExporter) exportedFlagsDeps() android.Paths {
187 return f.flagsDeps
188}
189
Colin Cross4d9c2d12016-07-29 12:48:20 -0700190type exportedFlagsProducer interface {
191 exportedFlags() []string
Dan Willemsen847dcc72016-09-29 12:13:36 -0700192 exportedFlagsDeps() android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700193}
194
195var _ exportedFlagsProducer = (*flagExporter)(nil)
196
Colin Crossb916a382016-07-29 17:28:03 -0700197// libraryDecorator wraps baseCompiler, baseLinker and baseInstaller to provide library-specific
198// functionality: static vs. shared linkage, reusing object files for shared libraries
199type libraryDecorator struct {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800200 Properties LibraryProperties
201 MutatedProperties LibraryMutatedProperties
Colin Cross4d9c2d12016-07-29 12:48:20 -0700202
203 // For reusing static library objects for shared library
Colin Cross10d22312017-05-03 11:01:58 -0700204 reuseObjects Objects
205 reuseExportedFlags []string
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700206 reuseExportedDeps android.Paths
Colin Cross10d22312017-05-03 11:01:58 -0700207
Colin Cross26c34ed2016-09-30 17:10:16 -0700208 // table-of-contents file to optimize out relinking when possible
209 tocFile android.OptionalPath
Colin Cross4d9c2d12016-07-29 12:48:20 -0700210
Colin Cross4d9c2d12016-07-29 12:48:20 -0700211 flagExporter
212 stripper
213
Colin Cross4d9c2d12016-07-29 12:48:20 -0700214 // If we're used as a whole_static_lib, our missing dependencies need
215 // to be given
216 wholeStaticMissingDeps []string
217
218 // For whole_static_libs
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700219 objects Objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700220
221 // Uses the module's name if empty, but can be overridden. Does not include
222 // shlib suffix.
223 libName string
Colin Crossb916a382016-07-29 17:28:03 -0700224
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800225 sabi *sabi
226
Dan Willemsen581341d2017-02-09 16:16:31 -0800227 // Output archive of gcno coverage information files
228 coverageOutputFile android.OptionalPath
229
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800230 // linked Source Abi Dump
231 sAbiOutputFile android.OptionalPath
232
233 // Source Abi Diff
234 sAbiDiff android.OptionalPath
235
Colin Cross0875c522017-11-28 17:34:01 -0800236 // Location of the static library in the sysroot. Empty if the library is
237 // not included in the NDK.
238 ndkSysrootPath android.Path
239
Colin Crossb60190a2018-09-04 16:28:17 -0700240 // Location of the linked, unstripped library for shared libraries
241 unstrippedOutputFile android.Path
242
Colin Crossb916a382016-07-29 17:28:03 -0700243 // Decorated interafaces
244 *baseCompiler
245 *baseLinker
246 *baseInstaller
Colin Cross4d9c2d12016-07-29 12:48:20 -0700247}
248
Colin Crossb916a382016-07-29 17:28:03 -0700249func (library *libraryDecorator) linkerProps() []interface{} {
250 var props []interface{}
251 props = append(props, library.baseLinker.linkerProps()...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700252 return append(props,
253 &library.Properties,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800254 &library.MutatedProperties,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700255 &library.flagExporter.Properties,
Colin Cross22f37952018-09-05 10:43:13 -0700256 &library.stripper.StripProperties)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700257}
258
Colin Crossb916a382016-07-29 17:28:03 -0700259func (library *libraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross42742b82016-08-01 13:20:05 -0700260 flags = library.baseLinker.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700261
Colin Crossb916a382016-07-29 17:28:03 -0700262 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
263 // all code is position independent, and then those warnings get promoted to
264 // errors.
Colin Cross3edeee12017-04-04 12:59:48 -0700265 if !ctx.Windows() {
Colin Crossb916a382016-07-29 17:28:03 -0700266 flags.CFlags = append(flags.CFlags, "-fPIC")
267 }
268
269 if library.static() {
270 flags.CFlags = append(flags.CFlags, library.Properties.Static.Cflags...)
Colin Crossa48ab5b2017-02-14 15:28:44 -0800271 } else if library.shared() {
Colin Crossb916a382016-07-29 17:28:03 -0700272 flags.CFlags = append(flags.CFlags, library.Properties.Shared.Cflags...)
273 }
274
Colin Crossa48ab5b2017-02-14 15:28:44 -0800275 if library.shared() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700276 libName := library.getLibName(ctx)
277 // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead
278 sharedFlag := "-Wl,-shared"
279 if flags.Clang || ctx.Host() {
280 sharedFlag = "-shared"
281 }
282 var f []string
Dan Willemsen01a405a2016-06-13 17:19:03 -0700283 if ctx.toolchain().Bionic() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700284 f = append(f,
285 "-nostdlib",
286 "-Wl,--gc-sections",
287 )
288 }
289
290 if ctx.Darwin() {
291 f = append(f,
292 "-dynamiclib",
293 "-single_module",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700294 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
295 )
Colin Cross7863cf52016-10-20 10:47:21 -0700296 if ctx.Arch().ArchType == android.X86 {
297 f = append(f,
298 "-read_only_relocs suppress",
299 )
300 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700301 } else {
302 f = append(f,
303 sharedFlag,
304 "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix())
305 }
306
307 flags.LdFlags = append(f, flags.LdFlags...)
308 }
309
310 return flags
311}
312
Colin Crossf18e1102017-11-16 14:33:08 -0800313func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700314 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700315 if len(exportIncludeDirs) > 0 {
Colin Crossdad8c952017-04-26 14:55:27 -0700316 f := includeDirsToFlags(exportIncludeDirs)
317 flags.GlobalFlags = append(flags.GlobalFlags, f)
318 flags.YasmFlags = append(flags.YasmFlags, f)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700319 }
320
Colin Crossf18e1102017-11-16 14:33:08 -0800321 return library.baseCompiler.compilerFlags(ctx, flags, deps)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700322}
323
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700324func extractExportIncludesFromFlags(flags []string) []string {
325 // This method is used in the generation of rules which produce
326 // abi-dumps for source files. Exported headers are needed to infer the
327 // abi exported by a library and filter out the rest of the abi dumped
328 // from a source. We extract the include flags exported by a library.
329 // This includes the flags exported which are re-exported from static
330 // library dependencies, exported header library dependencies and
Jayant Chowdharyaf6eb712017-08-23 16:08:29 -0700331 // generated header dependencies. -isystem headers are not included
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700332 // since for bionic libraries, abi-filtering is taken care of by version
333 // scripts.
334 var exportedIncludes []string
335 for _, flag := range flags {
336 if strings.HasPrefix(flag, "-I") {
337 exportedIncludes = append(exportedIncludes, flag)
338 }
339 }
340 return exportedIncludes
341}
342
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700343func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Colin Cross5950f382016-12-13 12:50:57 -0800344 if !library.buildShared() && !library.buildStatic() {
345 if len(library.baseCompiler.Properties.Srcs) > 0 {
346 ctx.PropertyErrorf("srcs", "cc_library_headers must not have any srcs")
347 }
348 if len(library.Properties.Static.Srcs) > 0 {
349 ctx.PropertyErrorf("static.srcs", "cc_library_headers must not have any srcs")
350 }
351 if len(library.Properties.Shared.Srcs) > 0 {
352 ctx.PropertyErrorf("shared.srcs", "cc_library_headers must not have any srcs")
353 }
354 return Objects{}
355 }
Logan Chien2f2b8902018-07-10 15:01:19 +0800356 if ctx.shouldCreateVndkSourceAbiDump() || library.sabi.Properties.CreateSAbiDumps {
Jayant Chowdharya4fce192017-09-06 13:10:03 -0700357 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800358 var SourceAbiFlags []string
359 for _, dir := range exportIncludeDirs.Strings() {
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700360 SourceAbiFlags = append(SourceAbiFlags, "-I"+dir)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800361 }
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700362 for _, reexportedInclude := range extractExportIncludesFromFlags(library.sabi.Properties.ReexportedIncludeFlags) {
363 SourceAbiFlags = append(SourceAbiFlags, reexportedInclude)
364 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800365 flags.SAbiFlags = SourceAbiFlags
366 total_length := len(library.baseCompiler.Properties.Srcs) + len(deps.GeneratedSources) + len(library.Properties.Shared.Srcs) +
367 len(library.Properties.Static.Srcs)
368 if total_length > 0 {
369 flags.SAbiDump = true
370 }
371 }
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700372 objs := library.baseCompiler.compile(ctx, flags, deps)
373 library.reuseObjects = objs
Colin Cross2f336352016-10-26 10:03:47 -0700374 buildFlags := flagsToBuilderFlags(flags)
Colin Crossb916a382016-07-29 17:28:03 -0700375
Colin Cross4d9c2d12016-07-29 12:48:20 -0700376 if library.static() {
Colin Cross2f336352016-10-26 10:03:47 -0700377 srcs := android.PathsForModuleSrc(ctx, library.Properties.Static.Srcs)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700378 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceStaticLibrary,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800379 srcs, library.baseCompiler.pathDeps, library.baseCompiler.cFlagsDeps))
Colin Crossa48ab5b2017-02-14 15:28:44 -0800380 } else if library.shared() {
Colin Cross2f336352016-10-26 10:03:47 -0700381 srcs := android.PathsForModuleSrc(ctx, library.Properties.Shared.Srcs)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700382 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceSharedLibrary,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800383 srcs, library.baseCompiler.pathDeps, library.baseCompiler.cFlagsDeps))
Colin Crossb916a382016-07-29 17:28:03 -0700384 }
385
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700386 return objs
Colin Crossb916a382016-07-29 17:28:03 -0700387}
388
389type libraryInterface interface {
390 getWholeStaticMissingDeps() []string
391 static() bool
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700392 objs() Objects
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700393 reuseObjs() (Objects, []string, android.Paths)
Colin Cross26c34ed2016-09-30 17:10:16 -0700394 toc() android.OptionalPath
Colin Crossb916a382016-07-29 17:28:03 -0700395
396 // Returns true if the build options for the module have selected a static or shared build
397 buildStatic() bool
398 buildShared() bool
399
400 // Sets whether a specific variant is static or shared
Colin Crossa48ab5b2017-02-14 15:28:44 -0800401 setStatic()
402 setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700403}
404
405func (library *libraryDecorator) getLibName(ctx ModuleContext) string {
406 name := library.libName
407 if name == "" {
Colin Crossce75d2c2016-10-06 16:12:58 -0700408 name = ctx.baseModuleName()
Colin Crossb916a382016-07-29 17:28:03 -0700409 }
410
Logan Chienf3511742017-10-31 18:04:35 +0800411 if ctx.isVndkExt() {
412 name = ctx.getVndkExtendsModuleName()
413 }
414
Colin Crossb916a382016-07-29 17:28:03 -0700415 if ctx.Host() && Bool(library.Properties.Unique_host_soname) {
416 if !strings.HasSuffix(name, "-host") {
417 name = name + "-host"
418 }
419 }
420
Colin Crossa48ab5b2017-02-14 15:28:44 -0800421 return name + library.MutatedProperties.VariantName
Colin Crossb916a382016-07-29 17:28:03 -0700422}
423
424func (library *libraryDecorator) linkerInit(ctx BaseModuleContext) {
425 location := InstallInSystem
Dan Albert61f32122018-07-26 14:00:24 -0700426 if library.baseLinker.sanitize.inSanitizerDir() {
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700427 location = InstallInSanitizerDir
Colin Crossb916a382016-07-29 17:28:03 -0700428 }
429 library.baseInstaller.location = location
430
431 library.baseLinker.linkerInit(ctx)
432}
433
Colin Cross37047f12016-12-13 17:06:13 -0800434func (library *libraryDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700435 deps = library.baseLinker.linkerDeps(ctx, deps)
436
437 if library.static() {
438 deps.WholeStaticLibs = append(deps.WholeStaticLibs,
439 library.Properties.Static.Whole_static_libs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700440 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Static.Static_libs...)
441 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Static.Shared_libs...)
Colin Crossa48ab5b2017-02-14 15:28:44 -0800442 } else if library.shared() {
Dan Willemsen2e47b342016-11-17 01:02:25 -0800443 if ctx.toolchain().Bionic() && !Bool(library.baseLinker.Properties.Nocrt) {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700444 if !ctx.useSdk() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700445 deps.CrtBegin = "crtbegin_so"
446 deps.CrtEnd = "crtend_so"
447 } else {
Dan Albertebedf672016-11-08 15:06:22 -0800448 // TODO(danalbert): Add generation of crt objects.
449 // For `sdk_version: "current"`, we don't actually have a
450 // freshly generated set of CRT objects. Use the last stable
451 // version.
452 version := ctx.sdkVersion()
453 if version == "current" {
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -0700454 version = getCurrentNdkPrebuiltVersion(ctx)
Dan Albertebedf672016-11-08 15:06:22 -0800455 }
456 deps.CrtBegin = "ndk_crtbegin_so." + version
457 deps.CrtEnd = "ndk_crtend_so." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700458 }
459 }
460 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Shared.Whole_static_libs...)
461 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Shared.Static_libs...)
462 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Shared.Shared_libs...)
463 }
Jiyong Park52d25bd2017-10-13 09:17:01 +0900464 if ctx.useVndk() {
465 deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs)
466 deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Vendor.Exclude_shared_libs)
467 deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs)
468 }
Jiyong Parkf9332f12018-02-01 00:54:12 +0900469 if ctx.inRecovery() {
470 deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Recovery.Exclude_static_libs)
471 deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Recovery.Exclude_shared_libs)
472 deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Recovery.Exclude_static_libs)
473 }
Colin Cross2383f3b2018-02-06 14:40:13 -0800474
Colin Cross2383f3b2018-02-06 14:40:13 -0800475 android.ExtractSourceDeps(ctx, library.Properties.Unexported_symbols_list)
476 android.ExtractSourceDeps(ctx, library.Properties.Force_symbols_not_weak_list)
477 android.ExtractSourceDeps(ctx, library.Properties.Force_symbols_weak_list)
Colin Cross2383f3b2018-02-06 14:40:13 -0800478
Colin Cross4d9c2d12016-07-29 12:48:20 -0700479 return deps
480}
481
Colin Crossb916a382016-07-29 17:28:03 -0700482func (library *libraryDecorator) linkStatic(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700483 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700484
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700485 library.objects = deps.WholeStaticLibObjs.Copy()
486 library.objects = library.objects.Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700487
Colin Cross86803cf2018-02-15 14:12:26 -0800488 fileName := ctx.ModuleName() + library.MutatedProperties.VariantName + staticLibraryExtension
489 outputFile := android.PathForModuleOut(ctx, fileName)
Dan Willemsen581341d2017-02-09 16:16:31 -0800490 builderFlags := flagsToBuilderFlags(flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700491
Colin Cross86803cf2018-02-15 14:12:26 -0800492 if Bool(library.baseLinker.Properties.Use_version_lib) && ctx.Host() {
493 versionedOutputFile := outputFile
494 outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
495 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
496 }
497
Dan Willemsen581341d2017-02-09 16:16:31 -0800498 TransformObjToStaticLib(ctx, library.objects.objFiles, builderFlags, outputFile, objs.tidyFiles)
499
500 library.coverageOutputFile = TransformCoverageFilesToLib(ctx, library.objects, builderFlags,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800501 ctx.ModuleName()+library.MutatedProperties.VariantName)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700502
503 library.wholeStaticMissingDeps = ctx.GetMissingDependencies()
504
505 ctx.CheckbuildFile(outputFile)
506
507 return outputFile
508}
509
Colin Crossb916a382016-07-29 17:28:03 -0700510func (library *libraryDecorator) linkShared(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700511 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700512
513 var linkerDeps android.Paths
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700514 linkerDeps = append(linkerDeps, flags.LdFlagsDeps...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700515
Colin Cross2383f3b2018-02-06 14:40:13 -0800516 unexportedSymbols := ctx.ExpandOptionalSource(library.Properties.Unexported_symbols_list, "unexported_symbols_list")
517 forceNotWeakSymbols := ctx.ExpandOptionalSource(library.Properties.Force_symbols_not_weak_list, "force_symbols_not_weak_list")
518 forceWeakSymbols := ctx.ExpandOptionalSource(library.Properties.Force_symbols_weak_list, "force_symbols_weak_list")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700519 if !ctx.Darwin() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700520 if unexportedSymbols.Valid() {
521 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
522 }
523 if forceNotWeakSymbols.Valid() {
524 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
525 }
526 if forceWeakSymbols.Valid() {
527 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
528 }
529 } else {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700530 if unexportedSymbols.Valid() {
531 flags.LdFlags = append(flags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
532 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
533 }
534 if forceNotWeakSymbols.Valid() {
535 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
536 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
537 }
538 if forceWeakSymbols.Valid() {
539 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
540 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
541 }
542 }
543
544 fileName := library.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
545 outputFile := android.PathForModuleOut(ctx, fileName)
546 ret := outputFile
547
548 builderFlags := flagsToBuilderFlags(flags)
549
Colin Crossb496cfd2018-09-10 16:50:05 -0700550 // Optimize out relinking against shared libraries whose interface hasn't changed by
551 // depending on a table of contents file instead of the library itself.
552 tocPath := outputFile.RelPathString()
553 tocPath = pathtools.ReplaceExtension(tocPath, flags.Toolchain.ShlibSuffix()[1:]+".toc")
554 tocFile := android.PathForOutput(ctx, tocPath)
555 library.tocFile = android.OptionalPathForPath(tocFile)
556 TransformSharedObjectToToc(ctx, outputFile, tocFile, builderFlags)
Colin Cross89562dc2016-10-03 17:47:19 -0700557
Colin Cross4d9c2d12016-07-29 12:48:20 -0700558 if library.stripper.needsStrip(ctx) {
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -0700559 // b/80093681, GNU strip/objcopy bug.
560 // Use llvm-{strip,objcopy} when clang lld is used.
561 builderFlags.stripUseLlvmStrip =
562 flags.Clang && library.baseLinker.useClangLld(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700563 strippedOutputFile := outputFile
564 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
565 library.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
566 }
567
Colin Crossb60190a2018-09-04 16:28:17 -0700568 library.unstrippedOutputFile = outputFile
569
Colin Cross86803cf2018-02-15 14:12:26 -0800570 if Bool(library.baseLinker.Properties.Use_version_lib) && ctx.Host() {
571 versionedOutputFile := outputFile
572 outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
573 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
574 }
575
Colin Cross4d9c2d12016-07-29 12:48:20 -0700576 sharedLibs := deps.SharedLibs
577 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
578
Colin Cross26c34ed2016-09-30 17:10:16 -0700579 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
580 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700581 linkerDeps = append(linkerDeps, objs.tidyFiles...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700582
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700583 TransformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700584 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
585 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile)
586
Dan Willemsen581341d2017-02-09 16:16:31 -0800587 objs.coverageFiles = append(objs.coverageFiles, deps.StaticLibObjs.coverageFiles...)
588 objs.coverageFiles = append(objs.coverageFiles, deps.WholeStaticLibObjs.coverageFiles...)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800589
590 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.StaticLibObjs.sAbiDumpFiles...)
591 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.WholeStaticLibObjs.sAbiDumpFiles...)
592
Dan Willemsen581341d2017-02-09 16:16:31 -0800593 library.coverageOutputFile = TransformCoverageFilesToLib(ctx, objs, builderFlags, library.getLibName(ctx))
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700594 library.linkSAbiDumpFiles(ctx, objs, fileName, ret)
Dan Willemsen581341d2017-02-09 16:16:31 -0800595
Colin Cross4d9c2d12016-07-29 12:48:20 -0700596 return ret
597}
598
Logan Chien7eefdc42018-07-11 18:10:41 +0800599func getRefAbiDumpFile(ctx ModuleContext, vndkVersion, fileName string) android.Path {
Logan Chienf4b79c62018-08-02 02:27:02 +0800600 isLlndk := inList(ctx.baseModuleName(), llndkLibraries) || inList(ctx.baseModuleName(), ndkMigratedLibs)
Logan Chien7eefdc42018-07-11 18:10:41 +0800601
602 refAbiDumpTextFile := android.PathForVndkRefAbiDump(ctx, vndkVersion, fileName, isLlndk, false)
603 refAbiDumpGzipFile := android.PathForVndkRefAbiDump(ctx, vndkVersion, fileName, isLlndk, true)
604
605 if refAbiDumpTextFile.Valid() {
606 if refAbiDumpGzipFile.Valid() {
607 ctx.ModuleErrorf(
608 "Two reference ABI dump files are found: %q and %q. Please delete the stale one.",
609 refAbiDumpTextFile, refAbiDumpGzipFile)
610 return nil
611 }
612 return refAbiDumpTextFile.Path()
613 }
614 if refAbiDumpGzipFile.Valid() {
615 return UnzipRefDump(ctx, refAbiDumpGzipFile.Path(), fileName)
616 }
617 return nil
618}
619
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700620func (library *libraryDecorator) linkSAbiDumpFiles(ctx ModuleContext, objs Objects, fileName string, soFile android.Path) {
Logan Chien2f2b8902018-07-10 15:01:19 +0800621 if len(objs.sAbiDumpFiles) > 0 && ctx.shouldCreateVndkSourceAbiDump() {
Logan Chiena8f51582018-03-21 11:53:48 +0800622 vndkVersion := ctx.DeviceConfig().PlatformVndkVersion()
623 if ver := ctx.DeviceConfig().VndkVersion(); ver != "" && ver != "current" {
Logan Chienf3511742017-10-31 18:04:35 +0800624 vndkVersion = ver
625 }
626
Jayant Chowdharya4fce192017-09-06 13:10:03 -0700627 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800628 var SourceAbiFlags []string
629 for _, dir := range exportIncludeDirs.Strings() {
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700630 SourceAbiFlags = append(SourceAbiFlags, "-I"+dir)
631 }
632 for _, reexportedInclude := range extractExportIncludesFromFlags(library.sabi.Properties.ReexportedIncludeFlags) {
633 SourceAbiFlags = append(SourceAbiFlags, reexportedInclude)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800634 }
635 exportedHeaderFlags := strings.Join(SourceAbiFlags, " ")
Jayant Chowdharydf344d52018-01-17 11:11:42 -0800636 library.sAbiOutputFile = TransformDumpToLinkedDump(ctx, objs.sAbiDumpFiles, soFile, fileName, exportedHeaderFlags)
Logan Chien2f2b8902018-07-10 15:01:19 +0800637
Logan Chien7eefdc42018-07-11 18:10:41 +0800638 refAbiDumpFile := getRefAbiDumpFile(ctx, vndkVersion, fileName)
639 if refAbiDumpFile != nil {
Logan Chienf3511742017-10-31 18:04:35 +0800640 library.sAbiDiff = SourceAbiDiff(ctx, library.sAbiOutputFile.Path(),
Logan Chien7eefdc42018-07-11 18:10:41 +0800641 refAbiDumpFile, fileName, exportedHeaderFlags, ctx.isVndkExt())
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800642 }
643 }
644}
645
Colin Crossb916a382016-07-29 17:28:03 -0700646func (library *libraryDecorator) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700647 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700648
Colin Crossad59e752017-11-16 14:29:11 -0800649 objs = deps.Objs.Copy().Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700650 var out android.Path
Colin Crossa48ab5b2017-02-14 15:28:44 -0800651 if library.static() || library.header() {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700652 out = library.linkStatic(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700653 } else {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700654 out = library.linkShared(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700655 }
656
657 library.exportIncludes(ctx, "-I")
658 library.reexportFlags(deps.ReexportedFlags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700659 library.reexportDeps(deps.ReexportedFlagsDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700660
Nan Zhang0007d812017-11-07 10:57:05 -0800661 if Bool(library.Properties.Aidl.Export_aidl_headers) {
Dan Willemsene1240db2016-11-03 14:28:51 -0700662 if library.baseCompiler.hasSrcExt(".aidl") {
Colin Cross10d22312017-05-03 11:01:58 -0700663 flags := []string{
Dan Willemsene1240db2016-11-03 14:28:51 -0700664 "-I" + android.PathForModuleGen(ctx, "aidl").String(),
Colin Cross10d22312017-05-03 11:01:58 -0700665 }
666 library.reexportFlags(flags)
667 library.reuseExportedFlags = append(library.reuseExportedFlags, flags...)
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800668 library.reexportDeps(library.baseCompiler.pathDeps) // TODO: restrict to aidl deps
669 library.reuseExportedDeps = append(library.reuseExportedDeps, library.baseCompiler.pathDeps...)
Dan Willemsene1240db2016-11-03 14:28:51 -0700670 }
671 }
672
Nan Zhang0007d812017-11-07 10:57:05 -0800673 if Bool(library.Properties.Proto.Export_proto_headers) {
Dan Willemsene1240db2016-11-03 14:28:51 -0700674 if library.baseCompiler.hasSrcExt(".proto") {
Dan Willemsenab9f4262018-02-14 13:58:34 -0800675 includes := []string{}
676 if flags.ProtoRoot {
677 includes = append(includes, "-I"+android.ProtoSubDir(ctx).String())
Colin Cross10d22312017-05-03 11:01:58 -0700678 }
Dan Willemsenab9f4262018-02-14 13:58:34 -0800679 includes = append(includes, "-I"+android.ProtoDir(ctx).String())
680 library.reexportFlags(includes)
681 library.reuseExportedFlags = append(library.reuseExportedFlags, includes...)
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800682 library.reexportDeps(library.baseCompiler.pathDeps) // TODO: restrict to proto deps
683 library.reuseExportedDeps = append(library.reuseExportedDeps, library.baseCompiler.pathDeps...)
Colin Cross0c461f12016-10-20 16:11:43 -0700684 }
685 }
686
Colin Cross4d9c2d12016-07-29 12:48:20 -0700687 return out
688}
689
Colin Crossb916a382016-07-29 17:28:03 -0700690func (library *libraryDecorator) buildStatic() bool {
Colin Cross38b40df2018-04-10 16:14:46 -0700691 return library.MutatedProperties.BuildStatic && BoolDefault(library.Properties.Static.Enabled, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700692}
693
Colin Crossb916a382016-07-29 17:28:03 -0700694func (library *libraryDecorator) buildShared() bool {
Colin Cross38b40df2018-04-10 16:14:46 -0700695 return library.MutatedProperties.BuildShared && BoolDefault(library.Properties.Shared.Enabled, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700696}
697
Colin Crossb916a382016-07-29 17:28:03 -0700698func (library *libraryDecorator) getWholeStaticMissingDeps() []string {
Colin Crossd2343a32018-04-30 14:50:01 -0700699 return append([]string(nil), library.wholeStaticMissingDeps...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700700}
701
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700702func (library *libraryDecorator) objs() Objects {
703 return library.objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700704}
705
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700706func (library *libraryDecorator) reuseObjs() (Objects, []string, android.Paths) {
707 return library.reuseObjects, library.reuseExportedFlags, library.reuseExportedDeps
Colin Cross4d9c2d12016-07-29 12:48:20 -0700708}
709
Colin Cross26c34ed2016-09-30 17:10:16 -0700710func (library *libraryDecorator) toc() android.OptionalPath {
711 return library.tocFile
712}
713
Colin Crossb916a382016-07-29 17:28:03 -0700714func (library *libraryDecorator) install(ctx ModuleContext, file android.Path) {
Colin Crossc43ae772017-04-14 15:42:53 -0700715 if library.shared() {
Justin Yun8fe12122017-12-07 17:18:15 +0900716 if ctx.Device() && ctx.useVndk() {
717 if ctx.isVndkSp() {
718 library.baseInstaller.subDir = "vndk-sp"
719 } else if ctx.isVndk() {
720 library.baseInstaller.subDir = "vndk"
721 }
Logan Chienf3511742017-10-31 18:04:35 +0800722
723 // Append a version to vndk or vndk-sp directories on the system partition.
724 if ctx.isVndk() && !ctx.isVndkExt() {
725 vndkVersion := ctx.DeviceConfig().PlatformVndkVersion()
726 if vndkVersion != "current" && vndkVersion != "" {
727 library.baseInstaller.subDir += "-" + vndkVersion
728 }
Justin Yun8effde42017-06-23 19:24:43 +0900729 }
730 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700731 library.baseInstaller.install(ctx, file)
732 }
Dan Albertf563d252017-10-13 00:29:00 -0700733
Dan Albert281f22b2017-12-13 15:03:47 -0800734 if Bool(library.Properties.Static_ndk_lib) && library.static() &&
Jiyong Parkf9332f12018-02-01 00:54:12 +0900735 !ctx.useVndk() && !ctx.inRecovery() && ctx.Device() &&
Dan Albert61f32122018-07-26 14:00:24 -0700736 library.baseLinker.sanitize.isUnsanitizedVariant() {
Dan Albertf563d252017-10-13 00:29:00 -0700737 installPath := getNdkSysrootBase(ctx).Join(
Dan Albertea4b7b92018-04-25 16:05:30 -0700738 ctx, "usr/lib", config.NDKTriple(ctx.toolchain()), file.Base())
Dan Albertf563d252017-10-13 00:29:00 -0700739
740 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
741 Rule: android.Cp,
742 Description: "install " + installPath.Base(),
743 Output: installPath,
744 Input: file,
745 })
746
Colin Cross0875c522017-11-28 17:34:01 -0800747 library.ndkSysrootPath = installPath
Dan Albertf563d252017-10-13 00:29:00 -0700748 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700749}
750
Colin Crossb916a382016-07-29 17:28:03 -0700751func (library *libraryDecorator) static() bool {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800752 return library.MutatedProperties.VariantIsStatic
Colin Cross4d9c2d12016-07-29 12:48:20 -0700753}
754
Colin Crossa48ab5b2017-02-14 15:28:44 -0800755func (library *libraryDecorator) shared() bool {
756 return library.MutatedProperties.VariantIsShared
757}
758
759func (library *libraryDecorator) header() bool {
760 return !library.static() && !library.shared()
761}
762
763func (library *libraryDecorator) setStatic() {
764 library.MutatedProperties.VariantIsStatic = true
765 library.MutatedProperties.VariantIsShared = false
766}
767
768func (library *libraryDecorator) setShared() {
769 library.MutatedProperties.VariantIsStatic = false
770 library.MutatedProperties.VariantIsShared = true
Colin Crossb916a382016-07-29 17:28:03 -0700771}
772
Colin Crossab3b7322016-12-09 14:46:15 -0800773func (library *libraryDecorator) BuildOnlyStatic() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800774 library.MutatedProperties.BuildShared = false
Colin Crossab3b7322016-12-09 14:46:15 -0800775}
776
777func (library *libraryDecorator) BuildOnlyShared() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800778 library.MutatedProperties.BuildStatic = false
Colin Crossab3b7322016-12-09 14:46:15 -0800779}
780
Colin Cross5950f382016-12-13 12:50:57 -0800781func (library *libraryDecorator) HeaderOnly() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800782 library.MutatedProperties.BuildShared = false
783 library.MutatedProperties.BuildStatic = false
Colin Cross5950f382016-12-13 12:50:57 -0800784}
785
Colin Crossab3b7322016-12-09 14:46:15 -0800786func NewLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700787 module := newModule(hod, android.MultilibBoth)
788
Colin Crossb916a382016-07-29 17:28:03 -0700789 library := &libraryDecorator{
Colin Crossa48ab5b2017-02-14 15:28:44 -0800790 MutatedProperties: LibraryMutatedProperties{
Colin Crossab3b7322016-12-09 14:46:15 -0800791 BuildShared: true,
792 BuildStatic: true,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700793 },
Colin Crossb916a382016-07-29 17:28:03 -0700794 baseCompiler: NewBaseCompiler(),
Dan Albert61f32122018-07-26 14:00:24 -0700795 baseLinker: NewBaseLinker(module.sanitize),
Colin Crossb916a382016-07-29 17:28:03 -0700796 baseInstaller: NewBaseInstaller("lib", "lib64", InstallInSystem),
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800797 sabi: module.sabi,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700798 }
799
Colin Crossb916a382016-07-29 17:28:03 -0700800 module.compiler = library
801 module.linker = library
802 module.installer = library
803
804 return module, library
805}
806
Colin Cross10d22312017-05-03 11:01:58 -0700807// connects a shared library to a static library in order to reuse its .o files to avoid
808// compiling source files twice.
809func reuseStaticLibrary(mctx android.BottomUpMutatorContext, static, shared *Module) {
810 if staticCompiler, ok := static.compiler.(*libraryDecorator); ok {
811 sharedCompiler := shared.compiler.(*libraryDecorator)
812 if len(staticCompiler.Properties.Static.Cflags) == 0 &&
813 len(sharedCompiler.Properties.Shared.Cflags) == 0 {
814
815 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
816 sharedCompiler.baseCompiler.Properties.OriginalSrcs =
817 sharedCompiler.baseCompiler.Properties.Srcs
818 sharedCompiler.baseCompiler.Properties.Srcs = nil
819 sharedCompiler.baseCompiler.Properties.Generated_sources = nil
820 }
821 }
822}
823
Colin Crosse40b4ea2018-10-02 22:25:58 -0700824func LinkageMutator(mctx android.BottomUpMutatorContext) {
Colin Crossb916a382016-07-29 17:28:03 -0700825 if m, ok := mctx.Module().(*Module); ok && m.linker != nil {
826 if library, ok := m.linker.(libraryInterface); ok {
827 var modules []blueprint.Module
828 if library.buildStatic() && library.buildShared() {
829 modules = mctx.CreateLocalVariations("static", "shared")
830 static := modules[0].(*Module)
831 shared := modules[1].(*Module)
832
Colin Crossa48ab5b2017-02-14 15:28:44 -0800833 static.linker.(libraryInterface).setStatic()
834 shared.linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700835
Colin Cross10d22312017-05-03 11:01:58 -0700836 reuseStaticLibrary(mctx, static, shared)
837
Colin Crossb916a382016-07-29 17:28:03 -0700838 } else if library.buildStatic() {
839 modules = mctx.CreateLocalVariations("static")
Colin Crossa48ab5b2017-02-14 15:28:44 -0800840 modules[0].(*Module).linker.(libraryInterface).setStatic()
Colin Crossb916a382016-07-29 17:28:03 -0700841 } else if library.buildShared() {
842 modules = mctx.CreateLocalVariations("shared")
Colin Crossa48ab5b2017-02-14 15:28:44 -0800843 modules[0].(*Module).linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700844 }
845 }
846 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700847}