blob: 5da36dc669be82b3c5504d29edd3b134608c6f61 [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 }
Logan Chien2f2b8902018-07-10 15:01:19 +0800365 if ctx.shouldCreateVndkSourceAbiDump() || 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) {
Chih-Hung Hsieh30485c92018-06-04 10:37:43 -0700596 // b/80093681, GNU strip/objcopy bug.
597 // Use llvm-{strip,objcopy} when clang lld is used.
598 builderFlags.stripUseLlvmStrip =
599 flags.Clang && library.baseLinker.useClangLld(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700600 strippedOutputFile := outputFile
601 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
602 library.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
603 }
604
Colin Cross86803cf2018-02-15 14:12:26 -0800605 if Bool(library.baseLinker.Properties.Use_version_lib) && ctx.Host() {
606 versionedOutputFile := outputFile
607 outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
608 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
609 }
610
Colin Cross4d9c2d12016-07-29 12:48:20 -0700611 sharedLibs := deps.SharedLibs
612 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
613
Colin Cross26c34ed2016-09-30 17:10:16 -0700614 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
615 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700616 linkerDeps = append(linkerDeps, objs.tidyFiles...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700617
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700618 TransformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700619 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
620 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile)
621
Dan Willemsen581341d2017-02-09 16:16:31 -0800622 objs.coverageFiles = append(objs.coverageFiles, deps.StaticLibObjs.coverageFiles...)
623 objs.coverageFiles = append(objs.coverageFiles, deps.WholeStaticLibObjs.coverageFiles...)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800624
625 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.StaticLibObjs.sAbiDumpFiles...)
626 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.WholeStaticLibObjs.sAbiDumpFiles...)
627
Dan Willemsen581341d2017-02-09 16:16:31 -0800628 library.coverageOutputFile = TransformCoverageFilesToLib(ctx, objs, builderFlags, library.getLibName(ctx))
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700629 library.linkSAbiDumpFiles(ctx, objs, fileName, ret)
Dan Willemsen581341d2017-02-09 16:16:31 -0800630
Colin Cross4d9c2d12016-07-29 12:48:20 -0700631 return ret
632}
633
Logan Chien7eefdc42018-07-11 18:10:41 +0800634func getRefAbiDumpFile(ctx ModuleContext, vndkVersion, fileName string) android.Path {
635 isLlndk := inList(ctx.baseModuleName(), llndkLibraries)
636
637 refAbiDumpTextFile := android.PathForVndkRefAbiDump(ctx, vndkVersion, fileName, isLlndk, false)
638 refAbiDumpGzipFile := android.PathForVndkRefAbiDump(ctx, vndkVersion, fileName, isLlndk, true)
639
640 if refAbiDumpTextFile.Valid() {
641 if refAbiDumpGzipFile.Valid() {
642 ctx.ModuleErrorf(
643 "Two reference ABI dump files are found: %q and %q. Please delete the stale one.",
644 refAbiDumpTextFile, refAbiDumpGzipFile)
645 return nil
646 }
647 return refAbiDumpTextFile.Path()
648 }
649 if refAbiDumpGzipFile.Valid() {
650 return UnzipRefDump(ctx, refAbiDumpGzipFile.Path(), fileName)
651 }
652 return nil
653}
654
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700655func (library *libraryDecorator) linkSAbiDumpFiles(ctx ModuleContext, objs Objects, fileName string, soFile android.Path) {
Logan Chien2f2b8902018-07-10 15:01:19 +0800656 if len(objs.sAbiDumpFiles) > 0 && ctx.shouldCreateVndkSourceAbiDump() {
Logan Chiena8f51582018-03-21 11:53:48 +0800657 vndkVersion := ctx.DeviceConfig().PlatformVndkVersion()
658 if ver := ctx.DeviceConfig().VndkVersion(); ver != "" && ver != "current" {
Logan Chienf3511742017-10-31 18:04:35 +0800659 vndkVersion = ver
660 }
661
Jayant Chowdharya4fce192017-09-06 13:10:03 -0700662 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800663 var SourceAbiFlags []string
664 for _, dir := range exportIncludeDirs.Strings() {
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700665 SourceAbiFlags = append(SourceAbiFlags, "-I"+dir)
666 }
667 for _, reexportedInclude := range extractExportIncludesFromFlags(library.sabi.Properties.ReexportedIncludeFlags) {
668 SourceAbiFlags = append(SourceAbiFlags, reexportedInclude)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800669 }
670 exportedHeaderFlags := strings.Join(SourceAbiFlags, " ")
Jayant Chowdharydf344d52018-01-17 11:11:42 -0800671 library.sAbiOutputFile = TransformDumpToLinkedDump(ctx, objs.sAbiDumpFiles, soFile, fileName, exportedHeaderFlags)
Logan Chien2f2b8902018-07-10 15:01:19 +0800672
Logan Chien7eefdc42018-07-11 18:10:41 +0800673 refAbiDumpFile := getRefAbiDumpFile(ctx, vndkVersion, fileName)
674 if refAbiDumpFile != nil {
Logan Chienf3511742017-10-31 18:04:35 +0800675 library.sAbiDiff = SourceAbiDiff(ctx, library.sAbiOutputFile.Path(),
Logan Chien7eefdc42018-07-11 18:10:41 +0800676 refAbiDumpFile, fileName, exportedHeaderFlags, ctx.isVndkExt())
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800677 }
678 }
679}
680
Colin Crossb916a382016-07-29 17:28:03 -0700681func (library *libraryDecorator) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700682 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700683
Colin Crossad59e752017-11-16 14:29:11 -0800684 objs = deps.Objs.Copy().Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700685 var out android.Path
Colin Crossa48ab5b2017-02-14 15:28:44 -0800686 if library.static() || library.header() {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700687 out = library.linkStatic(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700688 } else {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700689 out = library.linkShared(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700690 }
691
692 library.exportIncludes(ctx, "-I")
693 library.reexportFlags(deps.ReexportedFlags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700694 library.reexportDeps(deps.ReexportedFlagsDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700695
Nan Zhang0007d812017-11-07 10:57:05 -0800696 if Bool(library.Properties.Aidl.Export_aidl_headers) {
Dan Willemsene1240db2016-11-03 14:28:51 -0700697 if library.baseCompiler.hasSrcExt(".aidl") {
Colin Cross10d22312017-05-03 11:01:58 -0700698 flags := []string{
Dan Willemsene1240db2016-11-03 14:28:51 -0700699 "-I" + android.PathForModuleGen(ctx, "aidl").String(),
Colin Cross10d22312017-05-03 11:01:58 -0700700 }
701 library.reexportFlags(flags)
702 library.reuseExportedFlags = append(library.reuseExportedFlags, flags...)
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800703 library.reexportDeps(library.baseCompiler.pathDeps) // TODO: restrict to aidl deps
704 library.reuseExportedDeps = append(library.reuseExportedDeps, library.baseCompiler.pathDeps...)
Dan Willemsene1240db2016-11-03 14:28:51 -0700705 }
706 }
707
Nan Zhang0007d812017-11-07 10:57:05 -0800708 if Bool(library.Properties.Proto.Export_proto_headers) {
Dan Willemsene1240db2016-11-03 14:28:51 -0700709 if library.baseCompiler.hasSrcExt(".proto") {
Dan Willemsenab9f4262018-02-14 13:58:34 -0800710 includes := []string{}
711 if flags.ProtoRoot {
712 includes = append(includes, "-I"+android.ProtoSubDir(ctx).String())
Colin Cross10d22312017-05-03 11:01:58 -0700713 }
Dan Willemsenab9f4262018-02-14 13:58:34 -0800714 includes = append(includes, "-I"+android.ProtoDir(ctx).String())
715 library.reexportFlags(includes)
716 library.reuseExportedFlags = append(library.reuseExportedFlags, includes...)
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800717 library.reexportDeps(library.baseCompiler.pathDeps) // TODO: restrict to proto deps
718 library.reuseExportedDeps = append(library.reuseExportedDeps, library.baseCompiler.pathDeps...)
Colin Cross0c461f12016-10-20 16:11:43 -0700719 }
720 }
721
Colin Cross4d9c2d12016-07-29 12:48:20 -0700722 return out
723}
724
Colin Crossb916a382016-07-29 17:28:03 -0700725func (library *libraryDecorator) buildStatic() bool {
Colin Cross38b40df2018-04-10 16:14:46 -0700726 return library.MutatedProperties.BuildStatic && BoolDefault(library.Properties.Static.Enabled, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700727}
728
Colin Crossb916a382016-07-29 17:28:03 -0700729func (library *libraryDecorator) buildShared() bool {
Colin Cross38b40df2018-04-10 16:14:46 -0700730 return library.MutatedProperties.BuildShared && BoolDefault(library.Properties.Shared.Enabled, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700731}
732
Colin Crossb916a382016-07-29 17:28:03 -0700733func (library *libraryDecorator) getWholeStaticMissingDeps() []string {
Colin Crossd2343a32018-04-30 14:50:01 -0700734 return append([]string(nil), library.wholeStaticMissingDeps...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700735}
736
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700737func (library *libraryDecorator) objs() Objects {
738 return library.objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700739}
740
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700741func (library *libraryDecorator) reuseObjs() (Objects, []string, android.Paths) {
742 return library.reuseObjects, library.reuseExportedFlags, library.reuseExportedDeps
Colin Cross4d9c2d12016-07-29 12:48:20 -0700743}
744
Colin Cross26c34ed2016-09-30 17:10:16 -0700745func (library *libraryDecorator) toc() android.OptionalPath {
746 return library.tocFile
747}
748
Colin Crossb916a382016-07-29 17:28:03 -0700749func (library *libraryDecorator) install(ctx ModuleContext, file android.Path) {
Colin Crossc43ae772017-04-14 15:42:53 -0700750 if library.shared() {
Justin Yun8fe12122017-12-07 17:18:15 +0900751 if ctx.Device() && ctx.useVndk() {
752 if ctx.isVndkSp() {
753 library.baseInstaller.subDir = "vndk-sp"
754 } else if ctx.isVndk() {
755 library.baseInstaller.subDir = "vndk"
756 }
Logan Chienf3511742017-10-31 18:04:35 +0800757
758 // Append a version to vndk or vndk-sp directories on the system partition.
759 if ctx.isVndk() && !ctx.isVndkExt() {
760 vndkVersion := ctx.DeviceConfig().PlatformVndkVersion()
761 if vndkVersion != "current" && vndkVersion != "" {
762 library.baseInstaller.subDir += "-" + vndkVersion
763 }
Justin Yun8effde42017-06-23 19:24:43 +0900764 }
765 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700766 library.baseInstaller.install(ctx, file)
767 }
Dan Albertf563d252017-10-13 00:29:00 -0700768
Dan Albert281f22b2017-12-13 15:03:47 -0800769 if Bool(library.Properties.Static_ndk_lib) && library.static() &&
Jiyong Parkf9332f12018-02-01 00:54:12 +0900770 !ctx.useVndk() && !ctx.inRecovery() && ctx.Device() &&
Dan Albert7d1eecf2018-01-19 12:30:45 -0800771 library.sanitize.isUnsanitizedVariant() {
Dan Albertf563d252017-10-13 00:29:00 -0700772 installPath := getNdkSysrootBase(ctx).Join(
Dan Albertea4b7b92018-04-25 16:05:30 -0700773 ctx, "usr/lib", config.NDKTriple(ctx.toolchain()), file.Base())
Dan Albertf563d252017-10-13 00:29:00 -0700774
775 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
776 Rule: android.Cp,
777 Description: "install " + installPath.Base(),
778 Output: installPath,
779 Input: file,
780 })
781
Colin Cross0875c522017-11-28 17:34:01 -0800782 library.ndkSysrootPath = installPath
Dan Albertf563d252017-10-13 00:29:00 -0700783 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700784}
785
Colin Crossb916a382016-07-29 17:28:03 -0700786func (library *libraryDecorator) static() bool {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800787 return library.MutatedProperties.VariantIsStatic
Colin Cross4d9c2d12016-07-29 12:48:20 -0700788}
789
Colin Crossa48ab5b2017-02-14 15:28:44 -0800790func (library *libraryDecorator) shared() bool {
791 return library.MutatedProperties.VariantIsShared
792}
793
794func (library *libraryDecorator) header() bool {
795 return !library.static() && !library.shared()
796}
797
798func (library *libraryDecorator) setStatic() {
799 library.MutatedProperties.VariantIsStatic = true
800 library.MutatedProperties.VariantIsShared = false
801}
802
803func (library *libraryDecorator) setShared() {
804 library.MutatedProperties.VariantIsStatic = false
805 library.MutatedProperties.VariantIsShared = true
Colin Crossb916a382016-07-29 17:28:03 -0700806}
807
Colin Crossab3b7322016-12-09 14:46:15 -0800808func (library *libraryDecorator) BuildOnlyStatic() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800809 library.MutatedProperties.BuildShared = false
Colin Crossab3b7322016-12-09 14:46:15 -0800810}
811
812func (library *libraryDecorator) BuildOnlyShared() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800813 library.MutatedProperties.BuildStatic = false
Colin Crossab3b7322016-12-09 14:46:15 -0800814}
815
Colin Cross5950f382016-12-13 12:50:57 -0800816func (library *libraryDecorator) HeaderOnly() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800817 library.MutatedProperties.BuildShared = false
818 library.MutatedProperties.BuildStatic = false
Colin Cross5950f382016-12-13 12:50:57 -0800819}
820
Colin Crossab3b7322016-12-09 14:46:15 -0800821func NewLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700822 module := newModule(hod, android.MultilibBoth)
823
Colin Crossb916a382016-07-29 17:28:03 -0700824 library := &libraryDecorator{
Colin Crossa48ab5b2017-02-14 15:28:44 -0800825 MutatedProperties: LibraryMutatedProperties{
Colin Crossab3b7322016-12-09 14:46:15 -0800826 BuildShared: true,
827 BuildStatic: true,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700828 },
Colin Crossb916a382016-07-29 17:28:03 -0700829 baseCompiler: NewBaseCompiler(),
830 baseLinker: NewBaseLinker(),
831 baseInstaller: NewBaseInstaller("lib", "lib64", InstallInSystem),
832 sanitize: module.sanitize,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800833 sabi: module.sabi,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700834 }
835
Colin Crossb916a382016-07-29 17:28:03 -0700836 module.compiler = library
837 module.linker = library
838 module.installer = library
839
840 return module, library
841}
842
Colin Cross10d22312017-05-03 11:01:58 -0700843// connects a shared library to a static library in order to reuse its .o files to avoid
844// compiling source files twice.
845func reuseStaticLibrary(mctx android.BottomUpMutatorContext, static, shared *Module) {
846 if staticCompiler, ok := static.compiler.(*libraryDecorator); ok {
847 sharedCompiler := shared.compiler.(*libraryDecorator)
848 if len(staticCompiler.Properties.Static.Cflags) == 0 &&
849 len(sharedCompiler.Properties.Shared.Cflags) == 0 {
850
851 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
852 sharedCompiler.baseCompiler.Properties.OriginalSrcs =
853 sharedCompiler.baseCompiler.Properties.Srcs
854 sharedCompiler.baseCompiler.Properties.Srcs = nil
855 sharedCompiler.baseCompiler.Properties.Generated_sources = nil
856 }
857 }
858}
859
Colin Crossb916a382016-07-29 17:28:03 -0700860func linkageMutator(mctx android.BottomUpMutatorContext) {
861 if m, ok := mctx.Module().(*Module); ok && m.linker != nil {
862 if library, ok := m.linker.(libraryInterface); ok {
863 var modules []blueprint.Module
864 if library.buildStatic() && library.buildShared() {
865 modules = mctx.CreateLocalVariations("static", "shared")
866 static := modules[0].(*Module)
867 shared := modules[1].(*Module)
868
Colin Crossa48ab5b2017-02-14 15:28:44 -0800869 static.linker.(libraryInterface).setStatic()
870 shared.linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700871
Colin Cross10d22312017-05-03 11:01:58 -0700872 reuseStaticLibrary(mctx, static, shared)
873
Colin Crossb916a382016-07-29 17:28:03 -0700874 } else if library.buildStatic() {
875 modules = mctx.CreateLocalVariations("static")
Colin Crossa48ab5b2017-02-14 15:28:44 -0800876 modules[0].(*Module).linker.(libraryInterface).setStatic()
Colin Crossb916a382016-07-29 17:28:03 -0700877 } else if library.buildShared() {
878 modules = mctx.CreateLocalVariations("shared")
Colin Crossa48ab5b2017-02-14 15:28:44 -0800879 modules[0].(*Module).linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700880 }
881 }
882 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700883}