blob: 4a7884b7407a3a346bb7b9e570788098d747b562 [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 }
Colin Cross2383f3b2018-02-06 14:40:13 -0800480
481 android.ExtractSourceDeps(ctx, library.Properties.Version_script)
482 android.ExtractSourceDeps(ctx, library.Properties.Unexported_symbols_list)
483 android.ExtractSourceDeps(ctx, library.Properties.Force_symbols_not_weak_list)
484 android.ExtractSourceDeps(ctx, library.Properties.Force_symbols_weak_list)
485 android.ExtractSourceDeps(ctx, library.Properties.Target.Vendor.Version_script)
486
Colin Cross4d9c2d12016-07-29 12:48:20 -0700487 return deps
488}
489
Colin Crossb916a382016-07-29 17:28:03 -0700490func (library *libraryDecorator) linkStatic(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700491 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700492
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700493 library.objects = deps.WholeStaticLibObjs.Copy()
494 library.objects = library.objects.Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700495
Colin Cross86803cf2018-02-15 14:12:26 -0800496 fileName := ctx.ModuleName() + library.MutatedProperties.VariantName + staticLibraryExtension
497 outputFile := android.PathForModuleOut(ctx, fileName)
Dan Willemsen581341d2017-02-09 16:16:31 -0800498 builderFlags := flagsToBuilderFlags(flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700499
Colin Cross86803cf2018-02-15 14:12:26 -0800500 if Bool(library.baseLinker.Properties.Use_version_lib) && ctx.Host() {
501 versionedOutputFile := outputFile
502 outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
503 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
504 }
505
Dan Willemsen581341d2017-02-09 16:16:31 -0800506 TransformObjToStaticLib(ctx, library.objects.objFiles, builderFlags, outputFile, objs.tidyFiles)
507
508 library.coverageOutputFile = TransformCoverageFilesToLib(ctx, library.objects, builderFlags,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800509 ctx.ModuleName()+library.MutatedProperties.VariantName)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700510
511 library.wholeStaticMissingDeps = ctx.GetMissingDependencies()
512
513 ctx.CheckbuildFile(outputFile)
514
515 return outputFile
516}
517
Colin Crossb916a382016-07-29 17:28:03 -0700518func (library *libraryDecorator) linkShared(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700519 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700520
521 var linkerDeps android.Paths
Pirama Arumuga Nainarada83ec2017-08-31 23:38:27 -0700522 linkerDeps = append(linkerDeps, flags.LdFlagsDeps...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700523
Colin Cross2383f3b2018-02-06 14:40:13 -0800524 versionScript := ctx.ExpandOptionalSource(library.Properties.Version_script, "version_script")
525 unexportedSymbols := ctx.ExpandOptionalSource(library.Properties.Unexported_symbols_list, "unexported_symbols_list")
526 forceNotWeakSymbols := ctx.ExpandOptionalSource(library.Properties.Force_symbols_not_weak_list, "force_symbols_not_weak_list")
527 forceWeakSymbols := ctx.ExpandOptionalSource(library.Properties.Force_symbols_weak_list, "force_symbols_weak_list")
Jiyong Park52d25bd2017-10-13 09:17:01 +0900528 if ctx.useVndk() && library.Properties.Target.Vendor.Version_script != nil {
Colin Cross2383f3b2018-02-06 14:40:13 -0800529 versionScript = ctx.ExpandOptionalSource(library.Properties.Target.Vendor.Version_script, "target.vendor.version_script")
Jiyong Park52d25bd2017-10-13 09:17:01 +0900530 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700531 if !ctx.Darwin() {
532 if versionScript.Valid() {
533 flags.LdFlags = append(flags.LdFlags, "-Wl,--version-script,"+versionScript.String())
534 linkerDeps = append(linkerDeps, versionScript.Path())
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000535 if library.sanitize.isSanitizerEnabled(cfi) {
Colin Cross1218a192018-03-28 14:50:27 -0700536 cfiExportsMap := android.PathForSource(ctx, cfiExportsMapPath)
Vishwath Mohanb743e9c2017-11-01 09:20:21 +0000537 flags.LdFlags = append(flags.LdFlags, "-Wl,--version-script,"+cfiExportsMap.String())
538 linkerDeps = append(linkerDeps, cfiExportsMap)
539 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700540 }
541 if unexportedSymbols.Valid() {
542 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
543 }
544 if forceNotWeakSymbols.Valid() {
545 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
546 }
547 if forceWeakSymbols.Valid() {
548 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
549 }
550 } else {
551 if versionScript.Valid() {
552 ctx.PropertyErrorf("version_script", "Not supported on Darwin")
553 }
554 if unexportedSymbols.Valid() {
555 flags.LdFlags = append(flags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
556 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
557 }
558 if forceNotWeakSymbols.Valid() {
559 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
560 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
561 }
562 if forceWeakSymbols.Valid() {
563 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
564 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
565 }
566 }
567
568 fileName := library.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
569 outputFile := android.PathForModuleOut(ctx, fileName)
570 ret := outputFile
571
572 builderFlags := flagsToBuilderFlags(flags)
573
Colin Crossd8f8d072017-04-04 13:00:15 -0700574 if !ctx.Darwin() && !ctx.Windows() {
Colin Cross89562dc2016-10-03 17:47:19 -0700575 // Optimize out relinking against shared libraries whose interface hasn't changed by
576 // depending on a table of contents file instead of the library itself.
577 tocPath := outputFile.RelPathString()
578 tocPath = pathtools.ReplaceExtension(tocPath, flags.Toolchain.ShlibSuffix()[1:]+".toc")
579 tocFile := android.PathForOutput(ctx, tocPath)
580 library.tocFile = android.OptionalPathForPath(tocFile)
581 TransformSharedObjectToToc(ctx, outputFile, tocFile, builderFlags)
582 }
583
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700584 if library.relocationPacker.needsPacking(ctx) {
585 packedOutputFile := outputFile
586 outputFile = android.PathForModuleOut(ctx, "unpacked", fileName)
587 library.relocationPacker.pack(ctx, outputFile, packedOutputFile, builderFlags)
588 }
589
Colin Cross4d9c2d12016-07-29 12:48:20 -0700590 if library.stripper.needsStrip(ctx) {
591 strippedOutputFile := outputFile
592 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
593 library.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
594 }
595
Colin Cross86803cf2018-02-15 14:12:26 -0800596 if Bool(library.baseLinker.Properties.Use_version_lib) && ctx.Host() {
597 versionedOutputFile := outputFile
598 outputFile = android.PathForModuleOut(ctx, "unversioned", fileName)
599 library.injectVersionSymbol(ctx, outputFile, versionedOutputFile)
600 }
601
Colin Cross4d9c2d12016-07-29 12:48:20 -0700602 sharedLibs := deps.SharedLibs
603 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
604
Colin Cross26c34ed2016-09-30 17:10:16 -0700605 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
606 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700607 linkerDeps = append(linkerDeps, objs.tidyFiles...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700608
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700609 TransformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700610 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
611 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile)
612
Dan Willemsen581341d2017-02-09 16:16:31 -0800613 objs.coverageFiles = append(objs.coverageFiles, deps.StaticLibObjs.coverageFiles...)
614 objs.coverageFiles = append(objs.coverageFiles, deps.WholeStaticLibObjs.coverageFiles...)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800615
616 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.StaticLibObjs.sAbiDumpFiles...)
617 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.WholeStaticLibObjs.sAbiDumpFiles...)
618
Dan Willemsen581341d2017-02-09 16:16:31 -0800619 library.coverageOutputFile = TransformCoverageFilesToLib(ctx, objs, builderFlags, library.getLibName(ctx))
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700620 library.linkSAbiDumpFiles(ctx, objs, fileName, ret)
Dan Willemsen581341d2017-02-09 16:16:31 -0800621
Colin Cross4d9c2d12016-07-29 12:48:20 -0700622 return ret
623}
624
Jayant Chowdhary6ab3d842017-06-26 12:52:58 -0700625func (library *libraryDecorator) linkSAbiDumpFiles(ctx ModuleContext, objs Objects, fileName string, soFile android.Path) {
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800626 //Also take into account object re-use.
Justin Yun8effde42017-06-23 19:24:43 +0900627 if len(objs.sAbiDumpFiles) > 0 && ctx.createVndkSourceAbiDump() {
Logan Chiena8f51582018-03-21 11:53:48 +0800628 vndkVersion := ctx.DeviceConfig().PlatformVndkVersion()
629 if ver := ctx.DeviceConfig().VndkVersion(); ver != "" && ver != "current" {
Logan Chienf3511742017-10-31 18:04:35 +0800630 vndkVersion = ver
631 }
632
633 refSourceDumpFile := android.PathForVndkRefAbiDump(ctx, vndkVersion, fileName, vndkVsNdk(ctx), true)
Jayant Chowdharya4fce192017-09-06 13:10:03 -0700634 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800635 var SourceAbiFlags []string
636 for _, dir := range exportIncludeDirs.Strings() {
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700637 SourceAbiFlags = append(SourceAbiFlags, "-I"+dir)
638 }
639 for _, reexportedInclude := range extractExportIncludesFromFlags(library.sabi.Properties.ReexportedIncludeFlags) {
640 SourceAbiFlags = append(SourceAbiFlags, reexportedInclude)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800641 }
642 exportedHeaderFlags := strings.Join(SourceAbiFlags, " ")
Jayant Chowdharydf344d52018-01-17 11:11:42 -0800643 library.sAbiOutputFile = TransformDumpToLinkedDump(ctx, objs.sAbiDumpFiles, soFile, fileName, exportedHeaderFlags)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800644 if refSourceDumpFile.Valid() {
Jayant Chowdhary715cac32017-04-20 06:53:59 -0700645 unzippedRefDump := UnzipRefDump(ctx, refSourceDumpFile.Path(), fileName)
Logan Chienf3511742017-10-31 18:04:35 +0800646 library.sAbiDiff = SourceAbiDiff(ctx, library.sAbiOutputFile.Path(),
647 unzippedRefDump, fileName, exportedHeaderFlags, ctx.isVndkExt())
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800648 }
649 }
650}
651
652func vndkVsNdk(ctx ModuleContext) bool {
Jiyong Parkab0fd5f2017-08-03 21:22:50 +0900653 if inList(ctx.baseModuleName(), llndkLibraries) {
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800654 return false
655 }
656 return true
657}
658
Colin Crossb916a382016-07-29 17:28:03 -0700659func (library *libraryDecorator) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700660 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700661
Colin Crossad59e752017-11-16 14:29:11 -0800662 objs = deps.Objs.Copy().Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700663 var out android.Path
Colin Crossa48ab5b2017-02-14 15:28:44 -0800664 if library.static() || library.header() {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700665 out = library.linkStatic(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700666 } else {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700667 out = library.linkShared(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700668 }
669
670 library.exportIncludes(ctx, "-I")
671 library.reexportFlags(deps.ReexportedFlags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700672 library.reexportDeps(deps.ReexportedFlagsDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700673
Nan Zhang0007d812017-11-07 10:57:05 -0800674 if Bool(library.Properties.Aidl.Export_aidl_headers) {
Dan Willemsene1240db2016-11-03 14:28:51 -0700675 if library.baseCompiler.hasSrcExt(".aidl") {
Colin Cross10d22312017-05-03 11:01:58 -0700676 flags := []string{
Dan Willemsene1240db2016-11-03 14:28:51 -0700677 "-I" + android.PathForModuleGen(ctx, "aidl").String(),
Colin Cross10d22312017-05-03 11:01:58 -0700678 }
679 library.reexportFlags(flags)
680 library.reuseExportedFlags = append(library.reuseExportedFlags, flags...)
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800681 library.reexportDeps(library.baseCompiler.pathDeps) // TODO: restrict to aidl deps
682 library.reuseExportedDeps = append(library.reuseExportedDeps, library.baseCompiler.pathDeps...)
Dan Willemsene1240db2016-11-03 14:28:51 -0700683 }
684 }
685
Nan Zhang0007d812017-11-07 10:57:05 -0800686 if Bool(library.Properties.Proto.Export_proto_headers) {
Dan Willemsene1240db2016-11-03 14:28:51 -0700687 if library.baseCompiler.hasSrcExt(".proto") {
Dan Willemsenab9f4262018-02-14 13:58:34 -0800688 includes := []string{}
689 if flags.ProtoRoot {
690 includes = append(includes, "-I"+android.ProtoSubDir(ctx).String())
Colin Cross10d22312017-05-03 11:01:58 -0700691 }
Dan Willemsenab9f4262018-02-14 13:58:34 -0800692 includes = append(includes, "-I"+android.ProtoDir(ctx).String())
693 library.reexportFlags(includes)
694 library.reuseExportedFlags = append(library.reuseExportedFlags, includes...)
Pirama Arumuga Nainarf231b192018-01-23 10:49:04 -0800695 library.reexportDeps(library.baseCompiler.pathDeps) // TODO: restrict to proto deps
696 library.reuseExportedDeps = append(library.reuseExportedDeps, library.baseCompiler.pathDeps...)
Colin Cross0c461f12016-10-20 16:11:43 -0700697 }
698 }
699
Colin Cross4d9c2d12016-07-29 12:48:20 -0700700 return out
701}
702
Colin Crossb916a382016-07-29 17:28:03 -0700703func (library *libraryDecorator) buildStatic() bool {
Colin Cross38b40df2018-04-10 16:14:46 -0700704 return library.MutatedProperties.BuildStatic && BoolDefault(library.Properties.Static.Enabled, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700705}
706
Colin Crossb916a382016-07-29 17:28:03 -0700707func (library *libraryDecorator) buildShared() bool {
Colin Cross38b40df2018-04-10 16:14:46 -0700708 return library.MutatedProperties.BuildShared && BoolDefault(library.Properties.Shared.Enabled, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700709}
710
Colin Crossb916a382016-07-29 17:28:03 -0700711func (library *libraryDecorator) getWholeStaticMissingDeps() []string {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700712 return library.wholeStaticMissingDeps
713}
714
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700715func (library *libraryDecorator) objs() Objects {
716 return library.objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700717}
718
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700719func (library *libraryDecorator) reuseObjs() (Objects, []string, android.Paths) {
720 return library.reuseObjects, library.reuseExportedFlags, library.reuseExportedDeps
Colin Cross4d9c2d12016-07-29 12:48:20 -0700721}
722
Colin Cross26c34ed2016-09-30 17:10:16 -0700723func (library *libraryDecorator) toc() android.OptionalPath {
724 return library.tocFile
725}
726
Colin Crossb916a382016-07-29 17:28:03 -0700727func (library *libraryDecorator) install(ctx ModuleContext, file android.Path) {
Colin Crossc43ae772017-04-14 15:42:53 -0700728 if library.shared() {
Justin Yun8fe12122017-12-07 17:18:15 +0900729 if ctx.Device() && ctx.useVndk() {
730 if ctx.isVndkSp() {
731 library.baseInstaller.subDir = "vndk-sp"
732 } else if ctx.isVndk() {
733 library.baseInstaller.subDir = "vndk"
734 }
Logan Chienf3511742017-10-31 18:04:35 +0800735
736 // Append a version to vndk or vndk-sp directories on the system partition.
737 if ctx.isVndk() && !ctx.isVndkExt() {
738 vndkVersion := ctx.DeviceConfig().PlatformVndkVersion()
739 if vndkVersion != "current" && vndkVersion != "" {
740 library.baseInstaller.subDir += "-" + vndkVersion
741 }
Justin Yun8effde42017-06-23 19:24:43 +0900742 }
743 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700744 library.baseInstaller.install(ctx, file)
745 }
Dan Albertf563d252017-10-13 00:29:00 -0700746
Dan Albert281f22b2017-12-13 15:03:47 -0800747 if Bool(library.Properties.Static_ndk_lib) && library.static() &&
Dan Albert7d1eecf2018-01-19 12:30:45 -0800748 !ctx.useVndk() && ctx.Device() &&
749 library.sanitize.isUnsanitizedVariant() {
Dan Albertf563d252017-10-13 00:29:00 -0700750 installPath := getNdkSysrootBase(ctx).Join(
Dan Albertea4b7b92018-04-25 16:05:30 -0700751 ctx, "usr/lib", config.NDKTriple(ctx.toolchain()), file.Base())
Dan Albertf563d252017-10-13 00:29:00 -0700752
753 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
754 Rule: android.Cp,
755 Description: "install " + installPath.Base(),
756 Output: installPath,
757 Input: file,
758 })
759
Colin Cross0875c522017-11-28 17:34:01 -0800760 library.ndkSysrootPath = installPath
Dan Albertf563d252017-10-13 00:29:00 -0700761 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700762}
763
Colin Crossb916a382016-07-29 17:28:03 -0700764func (library *libraryDecorator) static() bool {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800765 return library.MutatedProperties.VariantIsStatic
Colin Cross4d9c2d12016-07-29 12:48:20 -0700766}
767
Colin Crossa48ab5b2017-02-14 15:28:44 -0800768func (library *libraryDecorator) shared() bool {
769 return library.MutatedProperties.VariantIsShared
770}
771
772func (library *libraryDecorator) header() bool {
773 return !library.static() && !library.shared()
774}
775
776func (library *libraryDecorator) setStatic() {
777 library.MutatedProperties.VariantIsStatic = true
778 library.MutatedProperties.VariantIsShared = false
779}
780
781func (library *libraryDecorator) setShared() {
782 library.MutatedProperties.VariantIsStatic = false
783 library.MutatedProperties.VariantIsShared = true
Colin Crossb916a382016-07-29 17:28:03 -0700784}
785
Colin Crossab3b7322016-12-09 14:46:15 -0800786func (library *libraryDecorator) BuildOnlyStatic() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800787 library.MutatedProperties.BuildShared = false
Colin Crossab3b7322016-12-09 14:46:15 -0800788}
789
790func (library *libraryDecorator) BuildOnlyShared() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800791 library.MutatedProperties.BuildStatic = false
Colin Crossab3b7322016-12-09 14:46:15 -0800792}
793
Colin Cross5950f382016-12-13 12:50:57 -0800794func (library *libraryDecorator) HeaderOnly() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800795 library.MutatedProperties.BuildShared = false
796 library.MutatedProperties.BuildStatic = false
Colin Cross5950f382016-12-13 12:50:57 -0800797}
798
Colin Crossab3b7322016-12-09 14:46:15 -0800799func NewLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700800 module := newModule(hod, android.MultilibBoth)
801
Colin Crossb916a382016-07-29 17:28:03 -0700802 library := &libraryDecorator{
Colin Crossa48ab5b2017-02-14 15:28:44 -0800803 MutatedProperties: LibraryMutatedProperties{
Colin Crossab3b7322016-12-09 14:46:15 -0800804 BuildShared: true,
805 BuildStatic: true,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700806 },
Colin Crossb916a382016-07-29 17:28:03 -0700807 baseCompiler: NewBaseCompiler(),
808 baseLinker: NewBaseLinker(),
809 baseInstaller: NewBaseInstaller("lib", "lib64", InstallInSystem),
810 sanitize: module.sanitize,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800811 sabi: module.sabi,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700812 }
813
Colin Crossb916a382016-07-29 17:28:03 -0700814 module.compiler = library
815 module.linker = library
816 module.installer = library
817
818 return module, library
819}
820
Colin Cross10d22312017-05-03 11:01:58 -0700821// connects a shared library to a static library in order to reuse its .o files to avoid
822// compiling source files twice.
823func reuseStaticLibrary(mctx android.BottomUpMutatorContext, static, shared *Module) {
824 if staticCompiler, ok := static.compiler.(*libraryDecorator); ok {
825 sharedCompiler := shared.compiler.(*libraryDecorator)
826 if len(staticCompiler.Properties.Static.Cflags) == 0 &&
827 len(sharedCompiler.Properties.Shared.Cflags) == 0 {
828
829 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
830 sharedCompiler.baseCompiler.Properties.OriginalSrcs =
831 sharedCompiler.baseCompiler.Properties.Srcs
832 sharedCompiler.baseCompiler.Properties.Srcs = nil
833 sharedCompiler.baseCompiler.Properties.Generated_sources = nil
834 }
835 }
836}
837
Colin Crossb916a382016-07-29 17:28:03 -0700838func linkageMutator(mctx android.BottomUpMutatorContext) {
839 if m, ok := mctx.Module().(*Module); ok && m.linker != nil {
840 if library, ok := m.linker.(libraryInterface); ok {
841 var modules []blueprint.Module
842 if library.buildStatic() && library.buildShared() {
843 modules = mctx.CreateLocalVariations("static", "shared")
844 static := modules[0].(*Module)
845 shared := modules[1].(*Module)
846
Colin Crossa48ab5b2017-02-14 15:28:44 -0800847 static.linker.(libraryInterface).setStatic()
848 shared.linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700849
Colin Cross10d22312017-05-03 11:01:58 -0700850 reuseStaticLibrary(mctx, static, shared)
851
Colin Crossb916a382016-07-29 17:28:03 -0700852 } else if library.buildStatic() {
853 modules = mctx.CreateLocalVariations("static")
Colin Crossa48ab5b2017-02-14 15:28:44 -0800854 modules[0].(*Module).linker.(libraryInterface).setStatic()
Colin Crossb916a382016-07-29 17:28:03 -0700855 } else if library.buildShared() {
856 modules = mctx.CreateLocalVariations("shared")
Colin Crossa48ab5b2017-02-14 15:28:44 -0800857 modules[0].(*Module).linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700858 }
859 }
860 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700861}