blob: e2ec584920f738c8ba0bf8e7ee385c70fe82ab42 [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"
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -080024 "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
61 Export_aidl_headers bool
62 }
63
Colin Cross0c461f12016-10-20 16:11:43 -070064 Proto struct {
65 // export headers generated from .proto sources
66 Export_proto_headers bool
67 }
Colin Crossa48ab5b2017-02-14 15:28:44 -080068}
Colin Cross0c461f12016-10-20 16:11:43 -070069
Colin Crossa48ab5b2017-02-14 15:28:44 -080070type LibraryMutatedProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -070071 VariantName string `blueprint:"mutated"`
Colin Crossb916a382016-07-29 17:28:03 -070072
73 // Build a static variant
74 BuildStatic bool `blueprint:"mutated"`
75 // Build a shared variant
76 BuildShared bool `blueprint:"mutated"`
77 // This variant is shared
78 VariantIsShared bool `blueprint:"mutated"`
79 // This variant is static
80 VariantIsStatic bool `blueprint:"mutated"`
81}
82
83type FlagExporterProperties struct {
84 // list of directories relative to the Blueprints file that will
Dan Willemsen273af7f2016-11-03 15:53:42 -070085 // be added to the include path (using -I) for this module and any module that links
86 // against this module
Colin Crossb916a382016-07-29 17:28:03 -070087 Export_include_dirs []string `android:"arch_variant"`
Dan Willemsen4416e5d2017-04-06 12:43:22 -070088
89 Target struct {
90 Vendor struct {
91 // list of exported include directories, like
92 // export_include_dirs, that will be applied to the
93 // vendor variant of this library. This will overwrite
94 // any other declarations.
95 Export_include_dirs []string
96 }
97 }
Colin Cross4d9c2d12016-07-29 12:48:20 -070098}
99
100func init() {
Colin Cross798bfce2016-10-12 14:28:16 -0700101 android.RegisterModuleType("cc_library_static", libraryStaticFactory)
102 android.RegisterModuleType("cc_library_shared", librarySharedFactory)
103 android.RegisterModuleType("cc_library", libraryFactory)
104 android.RegisterModuleType("cc_library_host_static", libraryHostStaticFactory)
105 android.RegisterModuleType("cc_library_host_shared", libraryHostSharedFactory)
Colin Cross5950f382016-12-13 12:50:57 -0800106 android.RegisterModuleType("cc_library_headers", libraryHeaderFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700107}
108
109// Module factory for combined static + shared libraries, device by default but with possible host
110// support
111func libraryFactory() (blueprint.Module, []interface{}) {
Colin Crossab3b7322016-12-09 14:46:15 -0800112 module, _ := NewLibrary(android.HostAndDeviceSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700113 return module.Init()
114}
115
116// Module factory for static libraries
117func libraryStaticFactory() (blueprint.Module, []interface{}) {
Colin Crossab3b7322016-12-09 14:46:15 -0800118 module, library := NewLibrary(android.HostAndDeviceSupported)
119 library.BuildOnlyStatic()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700120 return module.Init()
121}
122
123// Module factory for shared libraries
124func librarySharedFactory() (blueprint.Module, []interface{}) {
Colin Crossab3b7322016-12-09 14:46:15 -0800125 module, library := NewLibrary(android.HostAndDeviceSupported)
126 library.BuildOnlyShared()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700127 return module.Init()
128}
129
130// Module factory for host static libraries
131func libraryHostStaticFactory() (blueprint.Module, []interface{}) {
Colin Crossab3b7322016-12-09 14:46:15 -0800132 module, library := NewLibrary(android.HostSupported)
133 library.BuildOnlyStatic()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700134 return module.Init()
135}
136
137// Module factory for host shared libraries
138func libraryHostSharedFactory() (blueprint.Module, []interface{}) {
Colin Crossab3b7322016-12-09 14:46:15 -0800139 module, library := NewLibrary(android.HostSupported)
140 library.BuildOnlyShared()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700141 return module.Init()
142}
143
Colin Cross5950f382016-12-13 12:50:57 -0800144// Module factory for header-only libraries
145func libraryHeaderFactory() (blueprint.Module, []interface{}) {
146 module, library := NewLibrary(android.HostAndDeviceSupported)
147 library.HeaderOnly()
148 return module.Init()
149}
150
Colin Cross4d9c2d12016-07-29 12:48:20 -0700151type flagExporter struct {
152 Properties FlagExporterProperties
153
Dan Willemsen847dcc72016-09-29 12:13:36 -0700154 flags []string
155 flagsDeps android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700156}
157
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700158func (f *flagExporter) exportedIncludes(ctx ModuleContext) android.Paths {
159 if ctx.Vendor() && f.Properties.Target.Vendor.Export_include_dirs != nil {
160 return android.PathsForModuleSrc(ctx, f.Properties.Target.Vendor.Export_include_dirs)
161 } else {
162 return android.PathsForModuleSrc(ctx, f.Properties.Export_include_dirs)
163 }
164}
165
Colin Cross4d9c2d12016-07-29 12:48:20 -0700166func (f *flagExporter) exportIncludes(ctx ModuleContext, inc string) {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700167 includeDirs := f.exportedIncludes(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700168 for _, dir := range includeDirs.Strings() {
169 f.flags = append(f.flags, inc+dir)
170 }
171}
172
173func (f *flagExporter) reexportFlags(flags []string) {
174 f.flags = append(f.flags, flags...)
175}
176
Dan Willemsen847dcc72016-09-29 12:13:36 -0700177func (f *flagExporter) reexportDeps(deps android.Paths) {
178 f.flagsDeps = append(f.flagsDeps, deps...)
179}
180
Colin Cross4d9c2d12016-07-29 12:48:20 -0700181func (f *flagExporter) exportedFlags() []string {
182 return f.flags
183}
184
Dan Willemsen847dcc72016-09-29 12:13:36 -0700185func (f *flagExporter) exportedFlagsDeps() android.Paths {
186 return f.flagsDeps
187}
188
Colin Cross4d9c2d12016-07-29 12:48:20 -0700189type exportedFlagsProducer interface {
190 exportedFlags() []string
Dan Willemsen847dcc72016-09-29 12:13:36 -0700191 exportedFlagsDeps() android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700192}
193
194var _ exportedFlagsProducer = (*flagExporter)(nil)
195
Colin Crossb916a382016-07-29 17:28:03 -0700196// libraryDecorator wraps baseCompiler, baseLinker and baseInstaller to provide library-specific
197// functionality: static vs. shared linkage, reusing object files for shared libraries
198type libraryDecorator struct {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800199 Properties LibraryProperties
200 MutatedProperties LibraryMutatedProperties
Colin Cross4d9c2d12016-07-29 12:48:20 -0700201
202 // For reusing static library objects for shared library
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700203 reuseObjects Objects
Colin Cross26c34ed2016-09-30 17:10:16 -0700204 // table-of-contents file to optimize out relinking when possible
205 tocFile android.OptionalPath
Colin Cross4d9c2d12016-07-29 12:48:20 -0700206
Colin Cross4d9c2d12016-07-29 12:48:20 -0700207 flagExporter
208 stripper
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700209 relocationPacker
Colin Cross4d9c2d12016-07-29 12:48:20 -0700210
Colin Cross4d9c2d12016-07-29 12:48:20 -0700211 // If we're used as a whole_static_lib, our missing dependencies need
212 // to be given
213 wholeStaticMissingDeps []string
214
215 // For whole_static_libs
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700216 objects Objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700217
218 // Uses the module's name if empty, but can be overridden. Does not include
219 // shlib suffix.
220 libName string
Colin Crossb916a382016-07-29 17:28:03 -0700221
222 sanitize *sanitize
223
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800224 sabi *sabi
225
Dan Willemsen581341d2017-02-09 16:16:31 -0800226 // Output archive of gcno coverage information files
227 coverageOutputFile android.OptionalPath
228
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800229 // linked Source Abi Dump
230 sAbiOutputFile android.OptionalPath
231
232 // Source Abi Diff
233 sAbiDiff android.OptionalPath
234
Colin Crossb916a382016-07-29 17:28:03 -0700235 // Decorated interafaces
236 *baseCompiler
237 *baseLinker
238 *baseInstaller
Colin Cross4d9c2d12016-07-29 12:48:20 -0700239}
240
Colin Crossb916a382016-07-29 17:28:03 -0700241func (library *libraryDecorator) linkerProps() []interface{} {
242 var props []interface{}
243 props = append(props, library.baseLinker.linkerProps()...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700244 return append(props,
245 &library.Properties,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800246 &library.MutatedProperties,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700247 &library.flagExporter.Properties,
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700248 &library.stripper.StripProperties,
249 &library.relocationPacker.Properties)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700250}
251
Colin Crossb916a382016-07-29 17:28:03 -0700252func (library *libraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross42742b82016-08-01 13:20:05 -0700253 flags = library.baseLinker.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700254
Colin Crossb916a382016-07-29 17:28:03 -0700255 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
256 // all code is position independent, and then those warnings get promoted to
257 // errors.
Colin Cross3edeee12017-04-04 12:59:48 -0700258 if !ctx.Windows() {
Colin Crossb916a382016-07-29 17:28:03 -0700259 flags.CFlags = append(flags.CFlags, "-fPIC")
260 }
261
262 if library.static() {
263 flags.CFlags = append(flags.CFlags, library.Properties.Static.Cflags...)
Colin Crossa48ab5b2017-02-14 15:28:44 -0800264 } else if library.shared() {
Colin Crossb916a382016-07-29 17:28:03 -0700265 flags.CFlags = append(flags.CFlags, library.Properties.Shared.Cflags...)
266 }
267
Colin Crossa48ab5b2017-02-14 15:28:44 -0800268 if library.shared() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700269 libName := library.getLibName(ctx)
270 // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead
271 sharedFlag := "-Wl,-shared"
272 if flags.Clang || ctx.Host() {
273 sharedFlag = "-shared"
274 }
275 var f []string
Dan Willemsen01a405a2016-06-13 17:19:03 -0700276 if ctx.toolchain().Bionic() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700277 f = append(f,
278 "-nostdlib",
279 "-Wl,--gc-sections",
280 )
281 }
282
283 if ctx.Darwin() {
284 f = append(f,
285 "-dynamiclib",
286 "-single_module",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700287 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
288 )
Colin Cross7863cf52016-10-20 10:47:21 -0700289 if ctx.Arch().ArchType == android.X86 {
290 f = append(f,
291 "-read_only_relocs suppress",
292 )
293 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700294 } else {
295 f = append(f,
296 sharedFlag,
297 "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix())
298 }
299
300 flags.LdFlags = append(f, flags.LdFlags...)
301 }
302
303 return flags
304}
305
Dan Willemsen273af7f2016-11-03 15:53:42 -0700306func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700307 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700308 if len(exportIncludeDirs) > 0 {
309 flags.GlobalFlags = append(flags.GlobalFlags, includeDirsToFlags(exportIncludeDirs))
310 }
311
312 return library.baseCompiler.compilerFlags(ctx, flags)
313}
314
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700315func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Colin Cross5950f382016-12-13 12:50:57 -0800316 if !library.buildShared() && !library.buildStatic() {
317 if len(library.baseCompiler.Properties.Srcs) > 0 {
318 ctx.PropertyErrorf("srcs", "cc_library_headers must not have any srcs")
319 }
320 if len(library.Properties.Static.Srcs) > 0 {
321 ctx.PropertyErrorf("static.srcs", "cc_library_headers must not have any srcs")
322 }
323 if len(library.Properties.Shared.Srcs) > 0 {
324 ctx.PropertyErrorf("shared.srcs", "cc_library_headers must not have any srcs")
325 }
326 return Objects{}
327 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800328 if ctx.createVndkSourceAbiDump() || (library.sabi.Properties.CreateSAbiDumps && ctx.Device()) {
329 exportIncludeDirs := android.PathsForModuleSrc(ctx, library.flagExporter.Properties.Export_include_dirs)
330 var SourceAbiFlags []string
331 for _, dir := range exportIncludeDirs.Strings() {
332 SourceAbiFlags = append(SourceAbiFlags, "-I "+dir)
333 }
Colin Cross5950f382016-12-13 12:50:57 -0800334
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800335 flags.SAbiFlags = SourceAbiFlags
336 total_length := len(library.baseCompiler.Properties.Srcs) + len(deps.GeneratedSources) + len(library.Properties.Shared.Srcs) +
337 len(library.Properties.Static.Srcs)
338 if total_length > 0 {
339 flags.SAbiDump = true
340 }
341 }
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700342 objs := library.baseCompiler.compile(ctx, flags, deps)
343 library.reuseObjects = objs
Colin Cross2f336352016-10-26 10:03:47 -0700344 buildFlags := flagsToBuilderFlags(flags)
Colin Crossb916a382016-07-29 17:28:03 -0700345
Colin Cross4d9c2d12016-07-29 12:48:20 -0700346 if library.static() {
Colin Cross2f336352016-10-26 10:03:47 -0700347 srcs := android.PathsForModuleSrc(ctx, library.Properties.Static.Srcs)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700348 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceStaticLibrary,
349 srcs, library.baseCompiler.deps))
Colin Crossa48ab5b2017-02-14 15:28:44 -0800350 } else if library.shared() {
Colin Cross2f336352016-10-26 10:03:47 -0700351 srcs := android.PathsForModuleSrc(ctx, library.Properties.Shared.Srcs)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700352 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceSharedLibrary,
353 srcs, library.baseCompiler.deps))
Colin Crossb916a382016-07-29 17:28:03 -0700354 }
355
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700356 return objs
Colin Crossb916a382016-07-29 17:28:03 -0700357}
358
359type libraryInterface interface {
360 getWholeStaticMissingDeps() []string
361 static() bool
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700362 objs() Objects
363 reuseObjs() Objects
Colin Cross26c34ed2016-09-30 17:10:16 -0700364 toc() android.OptionalPath
Colin Crossb916a382016-07-29 17:28:03 -0700365
366 // Returns true if the build options for the module have selected a static or shared build
367 buildStatic() bool
368 buildShared() bool
369
370 // Sets whether a specific variant is static or shared
Colin Crossa48ab5b2017-02-14 15:28:44 -0800371 setStatic()
372 setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700373}
374
375func (library *libraryDecorator) getLibName(ctx ModuleContext) string {
376 name := library.libName
377 if name == "" {
Colin Crossce75d2c2016-10-06 16:12:58 -0700378 name = ctx.baseModuleName()
Colin Crossb916a382016-07-29 17:28:03 -0700379 }
380
381 if ctx.Host() && Bool(library.Properties.Unique_host_soname) {
382 if !strings.HasSuffix(name, "-host") {
383 name = name + "-host"
384 }
385 }
386
Colin Crossa48ab5b2017-02-14 15:28:44 -0800387 return name + library.MutatedProperties.VariantName
Colin Crossb916a382016-07-29 17:28:03 -0700388}
389
390func (library *libraryDecorator) linkerInit(ctx BaseModuleContext) {
391 location := InstallInSystem
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700392 if library.sanitize.inSanitizerDir() {
393 location = InstallInSanitizerDir
Colin Crossb916a382016-07-29 17:28:03 -0700394 }
395 library.baseInstaller.location = location
396
397 library.baseLinker.linkerInit(ctx)
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700398
399 library.relocationPacker.packingInit(ctx)
Colin Crossb916a382016-07-29 17:28:03 -0700400}
401
Colin Cross37047f12016-12-13 17:06:13 -0800402func (library *libraryDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700403 deps = library.baseLinker.linkerDeps(ctx, deps)
404
405 if library.static() {
406 deps.WholeStaticLibs = append(deps.WholeStaticLibs,
407 library.Properties.Static.Whole_static_libs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700408 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Static.Static_libs...)
409 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Static.Shared_libs...)
Colin Crossa48ab5b2017-02-14 15:28:44 -0800410 } else if library.shared() {
Dan Willemsen2e47b342016-11-17 01:02:25 -0800411 if ctx.toolchain().Bionic() && !Bool(library.baseLinker.Properties.Nocrt) {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700412 if !ctx.sdk() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700413 deps.CrtBegin = "crtbegin_so"
414 deps.CrtEnd = "crtend_so"
415 } else {
Dan Albertebedf672016-11-08 15:06:22 -0800416 // TODO(danalbert): Add generation of crt objects.
417 // For `sdk_version: "current"`, we don't actually have a
418 // freshly generated set of CRT objects. Use the last stable
419 // version.
420 version := ctx.sdkVersion()
421 if version == "current" {
422 version = ctx.AConfig().PlatformSdkVersion()
423 }
424 deps.CrtBegin = "ndk_crtbegin_so." + version
425 deps.CrtEnd = "ndk_crtend_so." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700426 }
427 }
428 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Shared.Whole_static_libs...)
429 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Shared.Static_libs...)
430 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Shared.Shared_libs...)
431 }
432
433 return deps
434}
435
Colin Crossb916a382016-07-29 17:28:03 -0700436func (library *libraryDecorator) linkStatic(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700437 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700438
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700439 library.objects = deps.WholeStaticLibObjs.Copy()
440 library.objects = library.objects.Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700441
442 outputFile := android.PathForModuleOut(ctx,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800443 ctx.ModuleName()+library.MutatedProperties.VariantName+staticLibraryExtension)
Dan Willemsen581341d2017-02-09 16:16:31 -0800444 builderFlags := flagsToBuilderFlags(flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700445
Dan Willemsen581341d2017-02-09 16:16:31 -0800446 TransformObjToStaticLib(ctx, library.objects.objFiles, builderFlags, outputFile, objs.tidyFiles)
447
448 library.coverageOutputFile = TransformCoverageFilesToLib(ctx, library.objects, builderFlags,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800449 ctx.ModuleName()+library.MutatedProperties.VariantName)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700450
451 library.wholeStaticMissingDeps = ctx.GetMissingDependencies()
452
453 ctx.CheckbuildFile(outputFile)
454
455 return outputFile
456}
457
Colin Crossb916a382016-07-29 17:28:03 -0700458func (library *libraryDecorator) linkShared(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700459 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700460
461 var linkerDeps android.Paths
462
463 versionScript := android.OptionalPathForModuleSrc(ctx, library.Properties.Version_script)
464 unexportedSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Unexported_symbols_list)
465 forceNotWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_not_weak_list)
466 forceWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_weak_list)
467 if !ctx.Darwin() {
468 if versionScript.Valid() {
469 flags.LdFlags = append(flags.LdFlags, "-Wl,--version-script,"+versionScript.String())
470 linkerDeps = append(linkerDeps, versionScript.Path())
471 }
472 if unexportedSymbols.Valid() {
473 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
474 }
475 if forceNotWeakSymbols.Valid() {
476 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
477 }
478 if forceWeakSymbols.Valid() {
479 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
480 }
481 } else {
482 if versionScript.Valid() {
483 ctx.PropertyErrorf("version_script", "Not supported on Darwin")
484 }
485 if unexportedSymbols.Valid() {
486 flags.LdFlags = append(flags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
487 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
488 }
489 if forceNotWeakSymbols.Valid() {
490 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
491 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
492 }
493 if forceWeakSymbols.Valid() {
494 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
495 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
496 }
497 }
498
499 fileName := library.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
500 outputFile := android.PathForModuleOut(ctx, fileName)
501 ret := outputFile
502
503 builderFlags := flagsToBuilderFlags(flags)
504
Colin Crossd8f8d072017-04-04 13:00:15 -0700505 if !ctx.Darwin() && !ctx.Windows() {
Colin Cross89562dc2016-10-03 17:47:19 -0700506 // Optimize out relinking against shared libraries whose interface hasn't changed by
507 // depending on a table of contents file instead of the library itself.
508 tocPath := outputFile.RelPathString()
509 tocPath = pathtools.ReplaceExtension(tocPath, flags.Toolchain.ShlibSuffix()[1:]+".toc")
510 tocFile := android.PathForOutput(ctx, tocPath)
511 library.tocFile = android.OptionalPathForPath(tocFile)
512 TransformSharedObjectToToc(ctx, outputFile, tocFile, builderFlags)
513 }
514
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700515 if library.relocationPacker.needsPacking(ctx) {
516 packedOutputFile := outputFile
517 outputFile = android.PathForModuleOut(ctx, "unpacked", fileName)
518 library.relocationPacker.pack(ctx, outputFile, packedOutputFile, builderFlags)
519 }
520
Colin Cross4d9c2d12016-07-29 12:48:20 -0700521 if library.stripper.needsStrip(ctx) {
522 strippedOutputFile := outputFile
523 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
524 library.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
525 }
526
527 sharedLibs := deps.SharedLibs
528 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
529
Dan Albertd015c4a2016-08-10 14:34:08 -0700530 // TODO(danalbert): Clean this up when soong supports prebuilts.
531 if strings.HasPrefix(ctx.selectedStl(), "ndk_libc++") {
532 libDir := getNdkStlLibDir(ctx, flags.Toolchain, "libc++")
533
534 if strings.HasSuffix(ctx.selectedStl(), "_shared") {
535 deps.StaticLibs = append(deps.StaticLibs,
536 libDir.Join(ctx, "libandroid_support.a"))
537 } else {
538 deps.StaticLibs = append(deps.StaticLibs,
539 libDir.Join(ctx, "libc++abi.a"),
540 libDir.Join(ctx, "libandroid_support.a"))
541 }
542
543 if ctx.Arch().ArchType == android.Arm {
544 deps.StaticLibs = append(deps.StaticLibs,
545 libDir.Join(ctx, "libunwind.a"))
546 }
547 }
548
Colin Cross26c34ed2016-09-30 17:10:16 -0700549 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
550 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700551 linkerDeps = append(linkerDeps, objs.tidyFiles...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700552
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700553 TransformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700554 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
555 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile)
556
Dan Willemsen581341d2017-02-09 16:16:31 -0800557 objs.coverageFiles = append(objs.coverageFiles, deps.StaticLibObjs.coverageFiles...)
558 objs.coverageFiles = append(objs.coverageFiles, deps.WholeStaticLibObjs.coverageFiles...)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800559
560 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.StaticLibObjs.sAbiDumpFiles...)
561 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.WholeStaticLibObjs.sAbiDumpFiles...)
562
Dan Willemsen581341d2017-02-09 16:16:31 -0800563 library.coverageOutputFile = TransformCoverageFilesToLib(ctx, objs, builderFlags, library.getLibName(ctx))
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800564 library.linkSAbiDumpFiles(ctx, objs, fileName)
Dan Willemsen581341d2017-02-09 16:16:31 -0800565
Colin Cross4d9c2d12016-07-29 12:48:20 -0700566 return ret
567}
568
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800569func (library *libraryDecorator) linkSAbiDumpFiles(ctx ModuleContext, objs Objects, fileName string) {
570 //Also take into account object re-use.
571 if len(objs.sAbiDumpFiles) > 0 && ctx.createVndkSourceAbiDump() {
572 refSourceDumpFile := android.PathForVndkRefAbiDump(ctx, "current", fileName, vndkVsNdk(ctx), true)
573 versionScript := android.OptionalPathForModuleSrc(ctx, library.Properties.Version_script)
574 var symbolFile android.OptionalPath
575 if versionScript.Valid() {
576 symbolFile = versionScript
577 }
578 exportIncludeDirs := android.PathsForModuleSrc(ctx, library.flagExporter.Properties.Export_include_dirs)
579 var SourceAbiFlags []string
580 for _, dir := range exportIncludeDirs.Strings() {
581 SourceAbiFlags = append(SourceAbiFlags, "-I "+dir)
582 }
583 exportedHeaderFlags := strings.Join(SourceAbiFlags, " ")
584 library.sAbiOutputFile = TransformDumpToLinkedDump(ctx, objs.sAbiDumpFiles, symbolFile, "current", fileName, exportedHeaderFlags)
585 if refSourceDumpFile.Valid() {
586 library.sAbiDiff = SourceAbiDiff(ctx, library.sAbiOutputFile.Path(), refSourceDumpFile.Path(), fileName)
587 }
588 }
589}
590
591func vndkVsNdk(ctx ModuleContext) bool {
592 if inList(ctx.baseModuleName(), config.LLndkLibraries()) {
593 return false
594 }
595 return true
596}
597
Colin Crossb916a382016-07-29 17:28:03 -0700598func (library *libraryDecorator) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700599 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700600
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700601 objs = objs.Append(deps.Objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700602
603 var out android.Path
Colin Crossa48ab5b2017-02-14 15:28:44 -0800604 if library.static() || library.header() {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700605 out = library.linkStatic(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700606 } else {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700607 out = library.linkShared(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700608 }
609
610 library.exportIncludes(ctx, "-I")
611 library.reexportFlags(deps.ReexportedFlags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700612 library.reexportDeps(deps.ReexportedFlagsDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700613
Dan Willemsene1240db2016-11-03 14:28:51 -0700614 if library.Properties.Aidl.Export_aidl_headers {
615 if library.baseCompiler.hasSrcExt(".aidl") {
616 library.reexportFlags([]string{
617 "-I" + android.PathForModuleGen(ctx, "aidl").String(),
618 })
619 library.reexportDeps(library.baseCompiler.deps) // TODO: restrict to aidl deps
620 }
621 }
622
623 if library.Properties.Proto.Export_proto_headers {
624 if library.baseCompiler.hasSrcExt(".proto") {
Colin Cross0c461f12016-10-20 16:11:43 -0700625 library.reexportFlags([]string{
626 "-I" + protoSubDir(ctx).String(),
627 "-I" + protoDir(ctx).String(),
628 })
629 library.reexportDeps(library.baseCompiler.deps) // TODO: restrict to proto deps
630 }
631 }
632
Colin Cross4d9c2d12016-07-29 12:48:20 -0700633 return out
634}
635
Colin Crossb916a382016-07-29 17:28:03 -0700636func (library *libraryDecorator) buildStatic() bool {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800637 return library.MutatedProperties.BuildStatic &&
Colin Cross4d9c2d12016-07-29 12:48:20 -0700638 (library.Properties.Static.Enabled == nil || *library.Properties.Static.Enabled)
639}
640
Colin Crossb916a382016-07-29 17:28:03 -0700641func (library *libraryDecorator) buildShared() bool {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800642 return library.MutatedProperties.BuildShared &&
Colin Cross4d9c2d12016-07-29 12:48:20 -0700643 (library.Properties.Shared.Enabled == nil || *library.Properties.Shared.Enabled)
644}
645
Colin Crossb916a382016-07-29 17:28:03 -0700646func (library *libraryDecorator) getWholeStaticMissingDeps() []string {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700647 return library.wholeStaticMissingDeps
648}
649
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700650func (library *libraryDecorator) objs() Objects {
651 return library.objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700652}
653
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700654func (library *libraryDecorator) reuseObjs() Objects {
655 return library.reuseObjects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700656}
657
Colin Cross26c34ed2016-09-30 17:10:16 -0700658func (library *libraryDecorator) toc() android.OptionalPath {
659 return library.tocFile
660}
661
Colin Crossb916a382016-07-29 17:28:03 -0700662func (library *libraryDecorator) install(ctx ModuleContext, file android.Path) {
Colin Crossc43ae772017-04-14 15:42:53 -0700663 if library.shared() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700664 library.baseInstaller.install(ctx, file)
665 }
666}
667
Colin Crossb916a382016-07-29 17:28:03 -0700668func (library *libraryDecorator) static() bool {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800669 return library.MutatedProperties.VariantIsStatic
Colin Cross4d9c2d12016-07-29 12:48:20 -0700670}
671
Colin Crossa48ab5b2017-02-14 15:28:44 -0800672func (library *libraryDecorator) shared() bool {
673 return library.MutatedProperties.VariantIsShared
674}
675
676func (library *libraryDecorator) header() bool {
677 return !library.static() && !library.shared()
678}
679
680func (library *libraryDecorator) setStatic() {
681 library.MutatedProperties.VariantIsStatic = true
682 library.MutatedProperties.VariantIsShared = false
683}
684
685func (library *libraryDecorator) setShared() {
686 library.MutatedProperties.VariantIsStatic = false
687 library.MutatedProperties.VariantIsShared = true
Colin Crossb916a382016-07-29 17:28:03 -0700688}
689
Colin Crossab3b7322016-12-09 14:46:15 -0800690func (library *libraryDecorator) BuildOnlyStatic() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800691 library.MutatedProperties.BuildShared = false
Colin Crossab3b7322016-12-09 14:46:15 -0800692}
693
694func (library *libraryDecorator) BuildOnlyShared() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800695 library.MutatedProperties.BuildStatic = false
Colin Crossab3b7322016-12-09 14:46:15 -0800696}
697
Colin Cross5950f382016-12-13 12:50:57 -0800698func (library *libraryDecorator) HeaderOnly() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800699 library.MutatedProperties.BuildShared = false
700 library.MutatedProperties.BuildStatic = false
Colin Cross5950f382016-12-13 12:50:57 -0800701}
702
Colin Crossab3b7322016-12-09 14:46:15 -0800703func NewLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700704 module := newModule(hod, android.MultilibBoth)
705
Colin Crossb916a382016-07-29 17:28:03 -0700706 library := &libraryDecorator{
Colin Crossa48ab5b2017-02-14 15:28:44 -0800707 MutatedProperties: LibraryMutatedProperties{
Colin Crossab3b7322016-12-09 14:46:15 -0800708 BuildShared: true,
709 BuildStatic: true,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700710 },
Colin Crossb916a382016-07-29 17:28:03 -0700711 baseCompiler: NewBaseCompiler(),
712 baseLinker: NewBaseLinker(),
713 baseInstaller: NewBaseInstaller("lib", "lib64", InstallInSystem),
714 sanitize: module.sanitize,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800715 sabi: module.sabi,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700716 }
717
Colin Crossb916a382016-07-29 17:28:03 -0700718 module.compiler = library
719 module.linker = library
720 module.installer = library
721
722 return module, library
723}
724
725func linkageMutator(mctx android.BottomUpMutatorContext) {
726 if m, ok := mctx.Module().(*Module); ok && m.linker != nil {
727 if library, ok := m.linker.(libraryInterface); ok {
728 var modules []blueprint.Module
729 if library.buildStatic() && library.buildShared() {
730 modules = mctx.CreateLocalVariations("static", "shared")
731 static := modules[0].(*Module)
732 shared := modules[1].(*Module)
733
Colin Crossa48ab5b2017-02-14 15:28:44 -0800734 static.linker.(libraryInterface).setStatic()
735 shared.linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700736
737 if staticCompiler, ok := static.compiler.(*libraryDecorator); ok {
738 sharedCompiler := shared.compiler.(*libraryDecorator)
739 if len(staticCompiler.Properties.Static.Cflags) == 0 &&
740 len(sharedCompiler.Properties.Shared.Cflags) == 0 {
741 // Optimize out compiling common .o files twice for static+shared libraries
742 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
743 sharedCompiler.baseCompiler.Properties.Srcs = nil
744 sharedCompiler.baseCompiler.Properties.Generated_sources = nil
745 }
746 }
747 } else if library.buildStatic() {
748 modules = mctx.CreateLocalVariations("static")
Colin Crossa48ab5b2017-02-14 15:28:44 -0800749 modules[0].(*Module).linker.(libraryInterface).setStatic()
Colin Crossb916a382016-07-29 17:28:03 -0700750 } else if library.buildShared() {
751 modules = mctx.CreateLocalVariations("shared")
Colin Crossa48ab5b2017-02-14 15:28:44 -0800752 modules[0].(*Module).linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700753 }
754 }
755 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700756}