blob: c83e89a3131707c54f128ff6d3606485d80ca107 [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
47 // local file name to pass to the linker as --version_script
48 Version_script *string `android:"arch_variant"`
49 // local file name to pass to the linker as -unexported_symbols_list
50 Unexported_symbols_list *string `android:"arch_variant"`
51 // local file name to pass to the linker as -force_symbols_not_weak_list
52 Force_symbols_not_weak_list *string `android:"arch_variant"`
53 // local file name to pass to the linker as -force_symbols_weak_list
54 Force_symbols_weak_list *string `android:"arch_variant"`
55
56 // rename host libraries to prevent overlap with system installed libraries
57 Unique_host_soname *bool
58
Dan Willemsene1240db2016-11-03 14:28:51 -070059 Aidl struct {
60 // export headers generated from .aidl sources
Nan Zhang0007d812017-11-07 10:57:05 -080061 Export_aidl_headers *bool
Dan Willemsene1240db2016-11-03 14:28:51 -070062 }
63
Colin Cross0c461f12016-10-20 16:11:43 -070064 Proto struct {
65 // export headers generated from .proto sources
Nan Zhang0007d812017-11-07 10:57:05 -080066 Export_proto_headers *bool
Colin Cross0c461f12016-10-20 16:11:43 -070067 }
Jiyong Park52d25bd2017-10-13 09:17:01 +090068 Target struct {
69 Vendor struct {
70 // version script for this vendor variant
71 Version_script *string `android:"arch_variant"`
72 }
73 }
Dan Albertf563d252017-10-13 00:29:00 -070074
Nan Zhang0007d812017-11-07 10:57:05 -080075 Static_ndk_lib *bool
Colin Crossa48ab5b2017-02-14 15:28:44 -080076}
Colin Cross0c461f12016-10-20 16:11:43 -070077
Colin Crossa48ab5b2017-02-14 15:28:44 -080078type LibraryMutatedProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -070079 VariantName string `blueprint:"mutated"`
Colin Crossb916a382016-07-29 17:28:03 -070080
81 // Build a static variant
82 BuildStatic bool `blueprint:"mutated"`
83 // Build a shared variant
84 BuildShared bool `blueprint:"mutated"`
85 // This variant is shared
86 VariantIsShared bool `blueprint:"mutated"`
87 // This variant is static
88 VariantIsStatic bool `blueprint:"mutated"`
89}
90
91type FlagExporterProperties struct {
92 // list of directories relative to the Blueprints file that will
Dan Willemsen273af7f2016-11-03 15:53:42 -070093 // be added to the include path (using -I) for this module and any module that links
Colin Cross5d195602017-10-17 16:15:50 -070094 // against this module. Directories listed in export_include_dirs do not need to be
95 // listed in local_include_dirs.
Colin Crossb916a382016-07-29 17:28:03 -070096 Export_include_dirs []string `android:"arch_variant"`
Dan Willemsen4416e5d2017-04-06 12:43:22 -070097
98 Target struct {
99 Vendor struct {
100 // list of exported include directories, like
101 // export_include_dirs, that will be applied to the
102 // vendor variant of this library. This will overwrite
103 // any other declarations.
Steven Morelandb21df8f2018-01-05 14:42:54 -0800104 Override_export_include_dirs []string
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700105 }
106 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700107}
108
109func init() {
Steven Morelandf9e62162017-11-02 17:00:50 -0700110 android.RegisterModuleType("cc_library_static", LibraryStaticFactory)
111 android.RegisterModuleType("cc_library_shared", LibrarySharedFactory)
112 android.RegisterModuleType("cc_library", LibraryFactory)
113 android.RegisterModuleType("cc_library_host_static", LibraryHostStaticFactory)
114 android.RegisterModuleType("cc_library_host_shared", LibraryHostSharedFactory)
115 android.RegisterModuleType("cc_library_headers", LibraryHeaderFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700116}
117
118// Module factory for combined static + shared libraries, device by default but with possible host
119// support
Steven Morelandf9e62162017-11-02 17:00:50 -0700120func LibraryFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800121 module, _ := NewLibrary(android.HostAndDeviceSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700122 return module.Init()
123}
124
125// Module factory for static libraries
Steven Morelandf9e62162017-11-02 17:00:50 -0700126func LibraryStaticFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800127 module, library := NewLibrary(android.HostAndDeviceSupported)
128 library.BuildOnlyStatic()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700129 return module.Init()
130}
131
132// Module factory for shared libraries
Steven Morelandf9e62162017-11-02 17:00:50 -0700133func LibrarySharedFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800134 module, library := NewLibrary(android.HostAndDeviceSupported)
135 library.BuildOnlyShared()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700136 return module.Init()
137}
138
139// Module factory for host static libraries
Steven Morelandf9e62162017-11-02 17:00:50 -0700140func LibraryHostStaticFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800141 module, library := NewLibrary(android.HostSupported)
142 library.BuildOnlyStatic()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700143 return module.Init()
144}
145
146// Module factory for host shared libraries
Steven Morelandf9e62162017-11-02 17:00:50 -0700147func LibraryHostSharedFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800148 module, library := NewLibrary(android.HostSupported)
149 library.BuildOnlyShared()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700150 return module.Init()
151}
152
Colin Cross5950f382016-12-13 12:50:57 -0800153// Module factory for header-only libraries
Steven Morelandf9e62162017-11-02 17:00:50 -0700154func LibraryHeaderFactory() android.Module {
Colin Cross5950f382016-12-13 12:50:57 -0800155 module, library := NewLibrary(android.HostAndDeviceSupported)
156 library.HeaderOnly()
157 return module.Init()
158}
159
Colin Cross4d9c2d12016-07-29 12:48:20 -0700160type flagExporter struct {
161 Properties FlagExporterProperties
162
Dan Willemsen847dcc72016-09-29 12:13:36 -0700163 flags []string
164 flagsDeps android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700165}
166
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700167func (f *flagExporter) exportedIncludes(ctx ModuleContext) android.Paths {
Steven Morelandb21df8f2018-01-05 14:42:54 -0800168 if ctx.useVndk() && f.Properties.Target.Vendor.Override_export_include_dirs != nil {
169 return android.PathsForModuleSrc(ctx, f.Properties.Target.Vendor.Override_export_include_dirs)
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700170 } else {
171 return android.PathsForModuleSrc(ctx, f.Properties.Export_include_dirs)
172 }
173}
174
Colin Cross4d9c2d12016-07-29 12:48:20 -0700175func (f *flagExporter) exportIncludes(ctx ModuleContext, inc string) {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700176 includeDirs := f.exportedIncludes(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700177 for _, dir := range includeDirs.Strings() {
178 f.flags = append(f.flags, inc+dir)
179 }
180}
181
182func (f *flagExporter) reexportFlags(flags []string) {
183 f.flags = append(f.flags, flags...)
184}
185
Dan Willemsen847dcc72016-09-29 12:13:36 -0700186func (f *flagExporter) reexportDeps(deps android.Paths) {
187 f.flagsDeps = append(f.flagsDeps, deps...)
188}
189
Colin Cross4d9c2d12016-07-29 12:48:20 -0700190func (f *flagExporter) exportedFlags() []string {
191 return f.flags
192}
193
Dan Willemsen847dcc72016-09-29 12:13:36 -0700194func (f *flagExporter) exportedFlagsDeps() android.Paths {
195 return f.flagsDeps
196}
197
Colin Cross4d9c2d12016-07-29 12:48:20 -0700198type exportedFlagsProducer interface {
199 exportedFlags() []string
Dan Willemsen847dcc72016-09-29 12:13:36 -0700200 exportedFlagsDeps() android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700201}
202
203var _ exportedFlagsProducer = (*flagExporter)(nil)
204
Colin Crossb916a382016-07-29 17:28:03 -0700205// libraryDecorator wraps baseCompiler, baseLinker and baseInstaller to provide library-specific
206// functionality: static vs. shared linkage, reusing object files for shared libraries
207type libraryDecorator struct {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800208 Properties LibraryProperties
209 MutatedProperties LibraryMutatedProperties
Colin Cross4d9c2d12016-07-29 12:48:20 -0700210
211 // For reusing static library objects for shared library
Colin Cross10d22312017-05-03 11:01:58 -0700212 reuseObjects Objects
213 reuseExportedFlags []string
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700214 reuseExportedDeps android.Paths
Colin Cross10d22312017-05-03 11:01:58 -0700215
Colin Cross26c34ed2016-09-30 17:10:16 -0700216 // table-of-contents file to optimize out relinking when possible
217 tocFile android.OptionalPath
Colin Cross4d9c2d12016-07-29 12:48:20 -0700218
Colin Cross4d9c2d12016-07-29 12:48:20 -0700219 flagExporter
220 stripper
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700221 relocationPacker
Colin Cross4d9c2d12016-07-29 12:48:20 -0700222
Colin Cross4d9c2d12016-07-29 12:48:20 -0700223 // If we're used as a whole_static_lib, our missing dependencies need
224 // to be given
225 wholeStaticMissingDeps []string
226
227 // For whole_static_libs
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700228 objects Objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700229
230 // Uses the module's name if empty, but can be overridden. Does not include
231 // shlib suffix.
232 libName string
Colin Crossb916a382016-07-29 17:28:03 -0700233
234 sanitize *sanitize
235
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800236 sabi *sabi
237
Dan Willemsen581341d2017-02-09 16:16:31 -0800238 // Output archive of gcno coverage information files
239 coverageOutputFile android.OptionalPath
240
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800241 // linked Source Abi Dump
242 sAbiOutputFile android.OptionalPath
243
244 // Source Abi Diff
245 sAbiDiff android.OptionalPath
246
Colin Cross0875c522017-11-28 17:34:01 -0800247 // Location of the static library in the sysroot. Empty if the library is
248 // not included in the NDK.
249 ndkSysrootPath android.Path
250
Colin Crossb916a382016-07-29 17:28:03 -0700251 // Decorated interafaces
252 *baseCompiler
253 *baseLinker
254 *baseInstaller
Colin Cross4d9c2d12016-07-29 12:48:20 -0700255}
256
Colin Crossb916a382016-07-29 17:28:03 -0700257func (library *libraryDecorator) linkerProps() []interface{} {
258 var props []interface{}
259 props = append(props, library.baseLinker.linkerProps()...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700260 return append(props,
261 &library.Properties,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800262 &library.MutatedProperties,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700263 &library.flagExporter.Properties,
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700264 &library.stripper.StripProperties,
265 &library.relocationPacker.Properties)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700266}
267
Colin Crossb916a382016-07-29 17:28:03 -0700268func (library *libraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross42742b82016-08-01 13:20:05 -0700269 flags = library.baseLinker.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700270
Colin Crossb916a382016-07-29 17:28:03 -0700271 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
272 // all code is position independent, and then those warnings get promoted to
273 // errors.
Colin Cross3edeee12017-04-04 12:59:48 -0700274 if !ctx.Windows() {
Colin Crossb916a382016-07-29 17:28:03 -0700275 flags.CFlags = append(flags.CFlags, "-fPIC")
276 }
277
278 if library.static() {
279 flags.CFlags = append(flags.CFlags, library.Properties.Static.Cflags...)
Colin Crossa48ab5b2017-02-14 15:28:44 -0800280 } else if library.shared() {
Colin Crossb916a382016-07-29 17:28:03 -0700281 flags.CFlags = append(flags.CFlags, library.Properties.Shared.Cflags...)
282 }
283
Colin Crossa48ab5b2017-02-14 15:28:44 -0800284 if library.shared() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700285 libName := library.getLibName(ctx)
286 // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead
287 sharedFlag := "-Wl,-shared"
288 if flags.Clang || ctx.Host() {
289 sharedFlag = "-shared"
290 }
291 var f []string
Dan Willemsen01a405a2016-06-13 17:19:03 -0700292 if ctx.toolchain().Bionic() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700293 f = append(f,
294 "-nostdlib",
295 "-Wl,--gc-sections",
296 )
297 }
298
299 if ctx.Darwin() {
300 f = append(f,
301 "-dynamiclib",
302 "-single_module",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700303 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
304 )
Colin Cross7863cf52016-10-20 10:47:21 -0700305 if ctx.Arch().ArchType == android.X86 {
306 f = append(f,
307 "-read_only_relocs suppress",
308 )
309 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700310 } else {
311 f = append(f,
312 sharedFlag,
313 "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix())
314 }
315
316 flags.LdFlags = append(f, flags.LdFlags...)
317 }
318
319 return flags
320}
321
Colin Crossf18e1102017-11-16 14:33:08 -0800322func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700323 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700324 if len(exportIncludeDirs) > 0 {
Colin Crossdad8c952017-04-26 14:55:27 -0700325 f := includeDirsToFlags(exportIncludeDirs)
326 flags.GlobalFlags = append(flags.GlobalFlags, f)
327 flags.YasmFlags = append(flags.YasmFlags, f)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700328 }
329
Colin Crossf18e1102017-11-16 14:33:08 -0800330 return library.baseCompiler.compilerFlags(ctx, flags, deps)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700331}
332
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700333func extractExportIncludesFromFlags(flags []string) []string {
334 // This method is used in the generation of rules which produce
335 // abi-dumps for source files. Exported headers are needed to infer the
336 // abi exported by a library and filter out the rest of the abi dumped
337 // from a source. We extract the include flags exported by a library.
338 // This includes the flags exported which are re-exported from static
339 // library dependencies, exported header library dependencies and
Jayant Chowdharyaf6eb712017-08-23 16:08:29 -0700340 // generated header dependencies. -isystem headers are not included
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700341 // since for bionic libraries, abi-filtering is taken care of by version
342 // scripts.
343 var exportedIncludes []string
344 for _, flag := range flags {
345 if strings.HasPrefix(flag, "-I") {
346 exportedIncludes = append(exportedIncludes, flag)
347 }
348 }
349 return exportedIncludes
350}
351
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700352func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Colin Cross5950f382016-12-13 12:50:57 -0800353 if !library.buildShared() && !library.buildStatic() {
354 if len(library.baseCompiler.Properties.Srcs) > 0 {
355 ctx.PropertyErrorf("srcs", "cc_library_headers must not have any srcs")
356 }
357 if len(library.Properties.Static.Srcs) > 0 {
358 ctx.PropertyErrorf("static.srcs", "cc_library_headers must not have any srcs")
359 }
360 if len(library.Properties.Shared.Srcs) > 0 {
361 ctx.PropertyErrorf("shared.srcs", "cc_library_headers must not have any srcs")
362 }
363 return Objects{}
364 }
Jayant Chowdhary2a966402017-08-07 14:09:45 -0700365 if ctx.createVndkSourceAbiDump() || library.sabi.Properties.CreateSAbiDumps {
Jayant Chowdharya4fce192017-09-06 13:10:03 -0700366 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800367 var SourceAbiFlags []string
368 for _, dir := range exportIncludeDirs.Strings() {
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700369 SourceAbiFlags = append(SourceAbiFlags, "-I"+dir)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800370 }
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700371 for _, reexportedInclude := range extractExportIncludesFromFlags(library.sabi.Properties.ReexportedIncludeFlags) {
372 SourceAbiFlags = append(SourceAbiFlags, reexportedInclude)
373 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800374 flags.SAbiFlags = SourceAbiFlags
375 total_length := len(library.baseCompiler.Properties.Srcs) + len(deps.GeneratedSources) + len(library.Properties.Shared.Srcs) +
376 len(library.Properties.Static.Srcs)
377 if total_length > 0 {
378 flags.SAbiDump = true
379 }
380 }
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700381 objs := library.baseCompiler.compile(ctx, flags, deps)
382 library.reuseObjects = objs
Colin Cross2f336352016-10-26 10:03:47 -0700383 buildFlags := flagsToBuilderFlags(flags)
Colin Crossb916a382016-07-29 17:28:03 -0700384
Colin Cross4d9c2d12016-07-29 12:48:20 -0700385 if library.static() {
Colin Cross2f336352016-10-26 10:03:47 -0700386 srcs := android.PathsForModuleSrc(ctx, library.Properties.Static.Srcs)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700387 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceStaticLibrary,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800388 srcs, library.baseCompiler.pathDeps, library.baseCompiler.cFlagsDeps))
Colin Crossa48ab5b2017-02-14 15:28:44 -0800389 } else if library.shared() {
Colin Cross2f336352016-10-26 10:03:47 -0700390 srcs := android.PathsForModuleSrc(ctx, library.Properties.Shared.Srcs)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700391 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceSharedLibrary,
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800392 srcs, library.baseCompiler.pathDeps, library.baseCompiler.cFlagsDeps))
Colin Crossb916a382016-07-29 17:28:03 -0700393 }
394
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700395 return objs
Colin Crossb916a382016-07-29 17:28:03 -0700396}
397
398type libraryInterface interface {
399 getWholeStaticMissingDeps() []string
400 static() bool
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700401 objs() Objects
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700402 reuseObjs() (Objects, []string, android.Paths)
Colin Cross26c34ed2016-09-30 17:10:16 -0700403 toc() android.OptionalPath
Colin Crossb916a382016-07-29 17:28:03 -0700404
405 // Returns true if the build options for the module have selected a static or shared build
406 buildStatic() bool
407 buildShared() bool
408
409 // Sets whether a specific variant is static or shared
Colin Crossa48ab5b2017-02-14 15:28:44 -0800410 setStatic()
411 setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700412}
413
414func (library *libraryDecorator) getLibName(ctx ModuleContext) string {
415 name := library.libName
416 if name == "" {
Colin Crossce75d2c2016-10-06 16:12:58 -0700417 name = ctx.baseModuleName()
Colin Crossb916a382016-07-29 17:28:03 -0700418 }
419
Logan Chienf3511742017-10-31 18:04:35 +0800420 if ctx.isVndkExt() {
421 name = ctx.getVndkExtendsModuleName()
422 }
423
Colin Crossb916a382016-07-29 17:28:03 -0700424 if ctx.Host() && Bool(library.Properties.Unique_host_soname) {
425 if !strings.HasSuffix(name, "-host") {
426 name = name + "-host"
427 }
428 }
429
Colin Crossa48ab5b2017-02-14 15:28:44 -0800430 return name + library.MutatedProperties.VariantName
Colin Crossb916a382016-07-29 17:28:03 -0700431}
432
433func (library *libraryDecorator) linkerInit(ctx BaseModuleContext) {
434 location := InstallInSystem
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700435 if library.sanitize.inSanitizerDir() {
436 location = InstallInSanitizerDir
Colin Crossb916a382016-07-29 17:28:03 -0700437 }
438 library.baseInstaller.location = location
439
440 library.baseLinker.linkerInit(ctx)
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700441
442 library.relocationPacker.packingInit(ctx)
Colin Crossb916a382016-07-29 17:28:03 -0700443}
444
Colin Cross37047f12016-12-13 17:06:13 -0800445func (library *libraryDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700446 deps = library.baseLinker.linkerDeps(ctx, deps)
447
448 if library.static() {
449 deps.WholeStaticLibs = append(deps.WholeStaticLibs,
450 library.Properties.Static.Whole_static_libs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700451 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Static.Static_libs...)
452 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Static.Shared_libs...)
Colin Crossa48ab5b2017-02-14 15:28:44 -0800453 } else if library.shared() {
Dan Willemsen2e47b342016-11-17 01:02:25 -0800454 if ctx.toolchain().Bionic() && !Bool(library.baseLinker.Properties.Nocrt) {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700455 if !ctx.useSdk() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700456 deps.CrtBegin = "crtbegin_so"
457 deps.CrtEnd = "crtend_so"
458 } else {
Dan Albertebedf672016-11-08 15:06:22 -0800459 // TODO(danalbert): Add generation of crt objects.
460 // For `sdk_version: "current"`, we don't actually have a
461 // freshly generated set of CRT objects. Use the last stable
462 // version.
463 version := ctx.sdkVersion()
464 if version == "current" {
Jayant Chowdhary6e8115a2017-05-09 10:21:52 -0700465 version = getCurrentNdkPrebuiltVersion(ctx)
Dan Albertebedf672016-11-08 15:06:22 -0800466 }
467 deps.CrtBegin = "ndk_crtbegin_so." + version
468 deps.CrtEnd = "ndk_crtend_so." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700469 }
470 }
471 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Shared.Whole_static_libs...)
472 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Shared.Static_libs...)
473 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Shared.Shared_libs...)
474 }
Jiyong Park52d25bd2017-10-13 09:17:01 +0900475 if ctx.useVndk() {
476 deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs)
477 deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Vendor.Exclude_shared_libs)
478 deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Vendor.Exclude_static_libs)
479 }
Jiyong Parkf9332f12018-02-01 00:54:12 +0900480 if ctx.inRecovery() {
481 deps.WholeStaticLibs = removeListFromList(deps.WholeStaticLibs, library.baseLinker.Properties.Target.Recovery.Exclude_static_libs)
482 deps.SharedLibs = removeListFromList(deps.SharedLibs, library.baseLinker.Properties.Target.Recovery.Exclude_shared_libs)
483 deps.StaticLibs = removeListFromList(deps.StaticLibs, library.baseLinker.Properties.Target.Recovery.Exclude_static_libs)
484 }
Colin Cross2383f3b2018-02-06 14:40:13 -0800485
486 android.ExtractSourceDeps(ctx, library.Properties.Version_script)
487 android.ExtractSourceDeps(ctx, library.Properties.Unexported_symbols_list)
488 android.ExtractSourceDeps(ctx, library.Properties.Force_symbols_not_weak_list)
489 android.ExtractSourceDeps(ctx, library.Properties.Force_symbols_weak_list)
490 android.ExtractSourceDeps(ctx, library.Properties.Target.Vendor.Version_script)
491
Colin Cross4d9c2d12016-07-29 12:48:20 -0700492 return deps
493}
494
Colin Crossb916a382016-07-29 17:28:03 -0700495func (library *libraryDecorator) linkStatic(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700496 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700497
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700498 library.objects = deps.WholeStaticLibObjs.Copy()
499 library.objects = library.objects.Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700500
Colin Cross86803cf2018-02-15 14:12:26 -0800501 fileName := ctx.ModuleName() + library.MutatedProperties.VariantName + staticLibraryExtension
502 outputFile := android.PathForModuleOut(ctx, fileName)
Dan Willemsen581341d2017-02-09 16:16:31 -0800503 builderFlags := flagsToBuilderFlags(flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700504
Colin Cross86803cf2018-02-15 14:12:26 -0800505 if Bool(library.baseLinker.Properties.Use_version_lib) && ctx.Host() {
506 versionedOutputFile := outputFile
507 outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
508 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
509 }
510
Dan Willemsen581341d2017-02-09 16:16:31 -0800511 TransformObjToStaticLib(ctx, library.objects.objFiles, builderFlags, outputFile, objs.tidyFiles)
512
513 library.coverageOutputFile = TransformCoverageFilesToLib(ctx, library.objects, builderFlags,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800514 ctx.ModuleName()+library.MutatedProperties.VariantName)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700515
516 library.wholeStaticMissingDeps = ctx.GetMissingDependencies()
517
518 ctx.CheckbuildFile(outputFile)
519
520 return outputFile
521}
522
Colin Crossb916a382016-07-29 17:28:03 -0700523func (library *libraryDecorator) linkShared(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700524 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700525
526 var linkerDeps android.Paths
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700527 linkerDeps = append(linkerDeps, flags.LdFlagsDeps...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700528
Colin Cross2383f3b2018-02-06 14:40:13 -0800529 versionScript := ctx.ExpandOptionalSource(library.Properties.Version_script, "version_script")
530 unexportedSymbols := ctx.ExpandOptionalSource(library.Properties.Unexported_symbols_list, "unexported_symbols_list")
531 forceNotWeakSymbols := ctx.ExpandOptionalSource(library.Properties.Force_symbols_not_weak_list, "force_symbols_not_weak_list")
532 forceWeakSymbols := ctx.ExpandOptionalSource(library.Properties.Force_symbols_weak_list, "force_symbols_weak_list")
Jiyong Park52d25bd2017-10-13 09:17:01 +0900533 if ctx.useVndk() && library.Properties.Target.Vendor.Version_script != nil {
Colin Cross2383f3b2018-02-06 14:40:13 -0800534 versionScript = ctx.ExpandOptionalSource(library.Properties.Target.Vendor.Version_script, "target.vendor.version_script")
Jiyong Park52d25bd2017-10-13 09:17:01 +0900535 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700536 if !ctx.Darwin() {
537 if versionScript.Valid() {
538 flags.LdFlags = append(flags.LdFlags, "-Wl,--version-script,"+versionScript.String())
539 linkerDeps = append(linkerDeps, versionScript.Path())
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000540 if library.sanitize.isSanitizerEnabled(cfi) {
Colin Cross1218a192018-03-28 14:50:27 -0700541 cfiExportsMap := android.PathForSource(ctx, cfiExportsMapPath)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000542 flags.LdFlags = append(flags.LdFlags, "-Wl,--version-script,"+cfiExportsMap.String())
543 linkerDeps = append(linkerDeps, cfiExportsMap)
544 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700545 }
546 if unexportedSymbols.Valid() {
547 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
548 }
549 if forceNotWeakSymbols.Valid() {
550 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
551 }
552 if forceWeakSymbols.Valid() {
553 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
554 }
555 } else {
556 if versionScript.Valid() {
557 ctx.PropertyErrorf("version_script", "Not supported on Darwin")
558 }
559 if unexportedSymbols.Valid() {
560 flags.LdFlags = append(flags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
561 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
562 }
563 if forceNotWeakSymbols.Valid() {
564 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
565 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
566 }
567 if forceWeakSymbols.Valid() {
568 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
569 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
570 }
571 }
572
573 fileName := library.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
574 outputFile := android.PathForModuleOut(ctx, fileName)
575 ret := outputFile
576
577 builderFlags := flagsToBuilderFlags(flags)
578
Colin Crossd8f8d072017-04-04 13:00:15 -0700579 if !ctx.Darwin() && !ctx.Windows() {
Colin Cross89562dc2016-10-03 17:47:19 -0700580 // Optimize out relinking against shared libraries whose interface hasn't changed by
581 // depending on a table of contents file instead of the library itself.
582 tocPath := outputFile.RelPathString()
583 tocPath = pathtools.ReplaceExtension(tocPath, flags.Toolchain.ShlibSuffix()[1:]+".toc")
584 tocFile := android.PathForOutput(ctx, tocPath)
585 library.tocFile = android.OptionalPathForPath(tocFile)
586 TransformSharedObjectToToc(ctx, outputFile, tocFile, builderFlags)
587 }
588
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700589 if library.relocationPacker.needsPacking(ctx) {
590 packedOutputFile := outputFile
591 outputFile = android.PathForModuleOut(ctx, "unpacked", fileName)
592 library.relocationPacker.pack(ctx, outputFile, packedOutputFile, builderFlags)
593 }
594
Colin Cross4d9c2d12016-07-29 12:48:20 -0700595 if library.stripper.needsStrip(ctx) {
596 strippedOutputFile := outputFile
597 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
598 library.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
599 }
600
Colin Cross86803cf2018-02-15 14:12:26 -0800601 if Bool(library.baseLinker.Properties.Use_version_lib) && ctx.Host() {
602 versionedOutputFile := outputFile
603 outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
604 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
605 }
606
Colin Cross4d9c2d12016-07-29 12:48:20 -0700607 sharedLibs := deps.SharedLibs
608 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
609
Colin Cross26c34ed2016-09-30 17:10:16 -0700610 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
611 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700612 linkerDeps = append(linkerDeps, objs.tidyFiles...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700613
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700614 TransformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700615 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
616 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile)
617
Dan Willemsen581341d2017-02-09 16:16:31 -0800618 objs.coverageFiles = append(objs.coverageFiles, deps.StaticLibObjs.coverageFiles...)
619 objs.coverageFiles = append(objs.coverageFiles, deps.WholeStaticLibObjs.coverageFiles...)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800620
621 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.StaticLibObjs.sAbiDumpFiles...)
622 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.WholeStaticLibObjs.sAbiDumpFiles...)
623
Dan Willemsen581341d2017-02-09 16:16:31 -0800624 library.coverageOutputFile = TransformCoverageFilesToLib(ctx, objs, builderFlags, library.getLibName(ctx))
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700625 library.linkSAbiDumpFiles(ctx, objs, fileName, ret)
Dan Willemsen581341d2017-02-09 16:16:31 -0800626
Colin Cross4d9c2d12016-07-29 12:48:20 -0700627 return ret
628}
629
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700630func (library *libraryDecorator) linkSAbiDumpFiles(ctx ModuleContext, objs Objects, fileName string, soFile android.Path) {
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800631 //Also take into account object re-use.
Justin Yun8effde42017-06-23 19:24:43 +0900632 if len(objs.sAbiDumpFiles) > 0 && ctx.createVndkSourceAbiDump() {
Logan Chiena8f51582018-03-21 11:53:48 +0800633 vndkVersion := ctx.DeviceConfig().PlatformVndkVersion()
634 if ver := ctx.DeviceConfig().VndkVersion(); ver != "" && ver != "current" {
Logan Chienf3511742017-10-31 18:04:35 +0800635 vndkVersion = ver
636 }
637
638 refSourceDumpFile := android.PathForVndkRefAbiDump(ctx, vndkVersion, fileName, vndkVsNdk(ctx), true)
Jayant Chowdharya4fce192017-09-06 13:10:03 -0700639 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800640 var SourceAbiFlags []string
641 for _, dir := range exportIncludeDirs.Strings() {
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700642 SourceAbiFlags = append(SourceAbiFlags, "-I"+dir)
643 }
644 for _, reexportedInclude := range extractExportIncludesFromFlags(library.sabi.Properties.ReexportedIncludeFlags) {
645 SourceAbiFlags = append(SourceAbiFlags, reexportedInclude)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800646 }
647 exportedHeaderFlags := strings.Join(SourceAbiFlags, " ")
Jayant Chowdharydf344d52018-01-17 11:11:42 -0800648 library.sAbiOutputFile = TransformDumpToLinkedDump(ctx, objs.sAbiDumpFiles, soFile, fileName, exportedHeaderFlags)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800649 if refSourceDumpFile.Valid() {
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700650 unzippedRefDump := UnzipRefDump(ctx, refSourceDumpFile.Path(), fileName)
Logan Chienf3511742017-10-31 18:04:35 +0800651 library.sAbiDiff = SourceAbiDiff(ctx, library.sAbiOutputFile.Path(),
652 unzippedRefDump, fileName, exportedHeaderFlags, ctx.isVndkExt())
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800653 }
654 }
655}
656
657func vndkVsNdk(ctx ModuleContext) bool {
Jiyong Parkab0fd5f2017-08-03 21:22:50 +0900658 if inList(ctx.baseModuleName(), llndkLibraries) {
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800659 return false
660 }
661 return true
662}
663
Colin Crossb916a382016-07-29 17:28:03 -0700664func (library *libraryDecorator) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700665 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700666
Colin Crossad59e752017-11-16 14:29:11 -0800667 objs = deps.Objs.Copy().Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700668 var out android.Path
Colin Crossa48ab5b2017-02-14 15:28:44 -0800669 if library.static() || library.header() {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700670 out = library.linkStatic(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700671 } else {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700672 out = library.linkShared(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700673 }
674
675 library.exportIncludes(ctx, "-I")
676 library.reexportFlags(deps.ReexportedFlags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700677 library.reexportDeps(deps.ReexportedFlagsDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700678
Nan Zhang0007d812017-11-07 10:57:05 -0800679 if Bool(library.Properties.Aidl.Export_aidl_headers) {
Dan Willemsene1240db2016-11-03 14:28:51 -0700680 if library.baseCompiler.hasSrcExt(".aidl") {
Colin Cross10d22312017-05-03 11:01:58 -0700681 flags := []string{
Dan Willemsene1240db2016-11-03 14:28:51 -0700682 "-I" + android.PathForModuleGen(ctx, "aidl").String(),
Colin Cross10d22312017-05-03 11:01:58 -0700683 }
684 library.reexportFlags(flags)
685 library.reuseExportedFlags = append(library.reuseExportedFlags, flags...)
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800686 library.reexportDeps(library.baseCompiler.pathDeps) // TODO: restrict to aidl deps
687 library.reuseExportedDeps = append(library.reuseExportedDeps, library.baseCompiler.pathDeps...)
Dan Willemsene1240db2016-11-03 14:28:51 -0700688 }
689 }
690
Nan Zhang0007d812017-11-07 10:57:05 -0800691 if Bool(library.Properties.Proto.Export_proto_headers) {
Dan Willemsene1240db2016-11-03 14:28:51 -0700692 if library.baseCompiler.hasSrcExt(".proto") {
Dan Willemsenab9f4262018-02-14 13:58:34 -0800693 includes := []string{}
694 if flags.ProtoRoot {
695 includes = append(includes, "-I"+android.ProtoSubDir(ctx).String())
Colin Cross10d22312017-05-03 11:01:58 -0700696 }
Dan Willemsenab9f4262018-02-14 13:58:34 -0800697 includes = append(includes, "-I"+android.ProtoDir(ctx).String())
698 library.reexportFlags(includes)
699 library.reuseExportedFlags = append(library.reuseExportedFlags, includes...)
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800700 library.reexportDeps(library.baseCompiler.pathDeps) // TODO: restrict to proto deps
701 library.reuseExportedDeps = append(library.reuseExportedDeps, library.baseCompiler.pathDeps...)
Colin Cross0c461f12016-10-20 16:11:43 -0700702 }
703 }
704
Colin Cross4d9c2d12016-07-29 12:48:20 -0700705 return out
706}
707
Colin Crossb916a382016-07-29 17:28:03 -0700708func (library *libraryDecorator) buildStatic() bool {
Colin Cross38b40df2018-04-10 16:14:46 -0700709 return library.MutatedProperties.BuildStatic && BoolDefault(library.Properties.Static.Enabled, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700710}
711
Colin Crossb916a382016-07-29 17:28:03 -0700712func (library *libraryDecorator) buildShared() bool {
Colin Cross38b40df2018-04-10 16:14:46 -0700713 return library.MutatedProperties.BuildShared && BoolDefault(library.Properties.Shared.Enabled, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700714}
715
Colin Crossb916a382016-07-29 17:28:03 -0700716func (library *libraryDecorator) getWholeStaticMissingDeps() []string {
Colin Crossd2343a32018-04-30 14:50:01 -0700717 return append([]string(nil), library.wholeStaticMissingDeps...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700718}
719
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700720func (library *libraryDecorator) objs() Objects {
721 return library.objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700722}
723
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700724func (library *libraryDecorator) reuseObjs() (Objects, []string, android.Paths) {
725 return library.reuseObjects, library.reuseExportedFlags, library.reuseExportedDeps
Colin Cross4d9c2d12016-07-29 12:48:20 -0700726}
727
Colin Cross26c34ed2016-09-30 17:10:16 -0700728func (library *libraryDecorator) toc() android.OptionalPath {
729 return library.tocFile
730}
731
Colin Crossb916a382016-07-29 17:28:03 -0700732func (library *libraryDecorator) install(ctx ModuleContext, file android.Path) {
Colin Crossc43ae772017-04-14 15:42:53 -0700733 if library.shared() {
Justin Yun8fe12122017-12-07 17:18:15 +0900734 if ctx.Device() && ctx.useVndk() {
735 if ctx.isVndkSp() {
736 library.baseInstaller.subDir = "vndk-sp"
737 } else if ctx.isVndk() {
738 library.baseInstaller.subDir = "vndk"
739 }
Logan Chienf3511742017-10-31 18:04:35 +0800740
741 // Append a version to vndk or vndk-sp directories on the system partition.
742 if ctx.isVndk() && !ctx.isVndkExt() {
743 vndkVersion := ctx.DeviceConfig().PlatformVndkVersion()
744 if vndkVersion != "current" && vndkVersion != "" {
745 library.baseInstaller.subDir += "-" + vndkVersion
746 }
Justin Yun8effde42017-06-23 19:24:43 +0900747 }
748 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700749 library.baseInstaller.install(ctx, file)
750 }
Dan Albertf563d252017-10-13 00:29:00 -0700751
Dan Albert281f22b2017-12-13 15:03:47 -0800752 if Bool(library.Properties.Static_ndk_lib) && library.static() &&
Jiyong Parkf9332f12018-02-01 00:54:12 +0900753 !ctx.useVndk() && !ctx.inRecovery() && ctx.Device() &&
Dan Albert7d1eecf2018-01-19 12:30:45 -0800754 library.sanitize.isUnsanitizedVariant() {
Dan Albertf563d252017-10-13 00:29:00 -0700755 installPath := getNdkSysrootBase(ctx).Join(
Dan Albertea4b7b92018-04-25 16:05:30 -0700756 ctx, "usr/lib", config.NDKTriple(ctx.toolchain()), file.Base())
Dan Albertf563d252017-10-13 00:29:00 -0700757
758 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
759 Rule: android.Cp,
760 Description: "install " + installPath.Base(),
761 Output: installPath,
762 Input: file,
763 })
764
Colin Cross0875c522017-11-28 17:34:01 -0800765 library.ndkSysrootPath = installPath
Dan Albertf563d252017-10-13 00:29:00 -0700766 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700767}
768
Colin Crossb916a382016-07-29 17:28:03 -0700769func (library *libraryDecorator) static() bool {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800770 return library.MutatedProperties.VariantIsStatic
Colin Cross4d9c2d12016-07-29 12:48:20 -0700771}
772
Colin Crossa48ab5b2017-02-14 15:28:44 -0800773func (library *libraryDecorator) shared() bool {
774 return library.MutatedProperties.VariantIsShared
775}
776
777func (library *libraryDecorator) header() bool {
778 return !library.static() && !library.shared()
779}
780
781func (library *libraryDecorator) setStatic() {
782 library.MutatedProperties.VariantIsStatic = true
783 library.MutatedProperties.VariantIsShared = false
784}
785
786func (library *libraryDecorator) setShared() {
787 library.MutatedProperties.VariantIsStatic = false
788 library.MutatedProperties.VariantIsShared = true
Colin Crossb916a382016-07-29 17:28:03 -0700789}
790
Colin Crossab3b7322016-12-09 14:46:15 -0800791func (library *libraryDecorator) BuildOnlyStatic() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800792 library.MutatedProperties.BuildShared = false
Colin Crossab3b7322016-12-09 14:46:15 -0800793}
794
795func (library *libraryDecorator) BuildOnlyShared() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800796 library.MutatedProperties.BuildStatic = false
Colin Crossab3b7322016-12-09 14:46:15 -0800797}
798
Colin Cross5950f382016-12-13 12:50:57 -0800799func (library *libraryDecorator) HeaderOnly() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800800 library.MutatedProperties.BuildShared = false
801 library.MutatedProperties.BuildStatic = false
Colin Cross5950f382016-12-13 12:50:57 -0800802}
803
Colin Crossab3b7322016-12-09 14:46:15 -0800804func NewLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700805 module := newModule(hod, android.MultilibBoth)
806
Colin Crossb916a382016-07-29 17:28:03 -0700807 library := &libraryDecorator{
Colin Crossa48ab5b2017-02-14 15:28:44 -0800808 MutatedProperties: LibraryMutatedProperties{
Colin Crossab3b7322016-12-09 14:46:15 -0800809 BuildShared: true,
810 BuildStatic: true,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700811 },
Colin Crossb916a382016-07-29 17:28:03 -0700812 baseCompiler: NewBaseCompiler(),
813 baseLinker: NewBaseLinker(),
814 baseInstaller: NewBaseInstaller("lib", "lib64", InstallInSystem),
815 sanitize: module.sanitize,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800816 sabi: module.sabi,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700817 }
818
Colin Crossb916a382016-07-29 17:28:03 -0700819 module.compiler = library
820 module.linker = library
821 module.installer = library
822
823 return module, library
824}
825
Colin Cross10d22312017-05-03 11:01:58 -0700826// connects a shared library to a static library in order to reuse its .o files to avoid
827// compiling source files twice.
828func reuseStaticLibrary(mctx android.BottomUpMutatorContext, static, shared *Module) {
829 if staticCompiler, ok := static.compiler.(*libraryDecorator); ok {
830 sharedCompiler := shared.compiler.(*libraryDecorator)
831 if len(staticCompiler.Properties.Static.Cflags) == 0 &&
832 len(sharedCompiler.Properties.Shared.Cflags) == 0 {
833
834 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
835 sharedCompiler.baseCompiler.Properties.OriginalSrcs =
836 sharedCompiler.baseCompiler.Properties.Srcs
837 sharedCompiler.baseCompiler.Properties.Srcs = nil
838 sharedCompiler.baseCompiler.Properties.Generated_sources = nil
839 }
840 }
841}
842
Colin Crossb916a382016-07-29 17:28:03 -0700843func linkageMutator(mctx android.BottomUpMutatorContext) {
844 if m, ok := mctx.Module().(*Module); ok && m.linker != nil {
845 if library, ok := m.linker.(libraryInterface); ok {
846 var modules []blueprint.Module
847 if library.buildStatic() && library.buildShared() {
848 modules = mctx.CreateLocalVariations("static", "shared")
849 static := modules[0].(*Module)
850 shared := modules[1].(*Module)
851
Colin Crossa48ab5b2017-02-14 15:28:44 -0800852 static.linker.(libraryInterface).setStatic()
853 shared.linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700854
Colin Cross10d22312017-05-03 11:01:58 -0700855 reuseStaticLibrary(mctx, static, shared)
856
Colin Crossb916a382016-07-29 17:28:03 -0700857 } else if library.buildStatic() {
858 modules = mctx.CreateLocalVariations("static")
Colin Crossa48ab5b2017-02-14 15:28:44 -0800859 modules[0].(*Module).linker.(libraryInterface).setStatic()
Colin Crossb916a382016-07-29 17:28:03 -0700860 } else if library.buildShared() {
861 modules = mctx.CreateLocalVariations("shared")
Colin Crossa48ab5b2017-02-14 15:28:44 -0800862 modules[0].(*Module).linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700863 }
864 }
865 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700866}