blob: 0ba7088b775f087d116a82c6ad6f998863520ed2 [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"
24)
25
Colin Crossb916a382016-07-29 17:28:03 -070026type LibraryProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -070027 Static struct {
Colin Cross2f336352016-10-26 10:03:47 -070028 Srcs []string `android:"arch_variant"`
29 Cflags []string `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070030
Colin Cross4d9c2d12016-07-29 12:48:20 -070031 Enabled *bool `android:"arch_variant"`
32 Whole_static_libs []string `android:"arch_variant"`
33 Static_libs []string `android:"arch_variant"`
34 Shared_libs []string `android:"arch_variant"`
35 } `android:"arch_variant"`
36 Shared struct {
Colin Cross2f336352016-10-26 10:03:47 -070037 Srcs []string `android:"arch_variant"`
38 Cflags []string `android:"arch_variant"`
Colin Crossb916a382016-07-29 17:28:03 -070039
Colin Cross4d9c2d12016-07-29 12:48:20 -070040 Enabled *bool `android:"arch_variant"`
41 Whole_static_libs []string `android:"arch_variant"`
42 Static_libs []string `android:"arch_variant"`
43 Shared_libs []string `android:"arch_variant"`
44 } `android:"arch_variant"`
45
46 // local file name to pass to the linker as --version_script
47 Version_script *string `android:"arch_variant"`
48 // local file name to pass to the linker as -unexported_symbols_list
49 Unexported_symbols_list *string `android:"arch_variant"`
50 // local file name to pass to the linker as -force_symbols_not_weak_list
51 Force_symbols_not_weak_list *string `android:"arch_variant"`
52 // local file name to pass to the linker as -force_symbols_weak_list
53 Force_symbols_weak_list *string `android:"arch_variant"`
54
55 // rename host libraries to prevent overlap with system installed libraries
56 Unique_host_soname *bool
57
Dan Willemsene1240db2016-11-03 14:28:51 -070058 Aidl struct {
59 // export headers generated from .aidl sources
60 Export_aidl_headers bool
61 }
62
Colin Cross0c461f12016-10-20 16:11:43 -070063 Proto struct {
64 // export headers generated from .proto sources
65 Export_proto_headers bool
66 }
Colin Crossa48ab5b2017-02-14 15:28:44 -080067}
Colin Cross0c461f12016-10-20 16:11:43 -070068
Colin Crossa48ab5b2017-02-14 15:28:44 -080069type LibraryMutatedProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -070070 VariantName string `blueprint:"mutated"`
Colin Crossb916a382016-07-29 17:28:03 -070071
72 // Build a static variant
73 BuildStatic bool `blueprint:"mutated"`
74 // Build a shared variant
75 BuildShared bool `blueprint:"mutated"`
76 // This variant is shared
77 VariantIsShared bool `blueprint:"mutated"`
78 // This variant is static
79 VariantIsStatic bool `blueprint:"mutated"`
80}
81
82type FlagExporterProperties struct {
83 // list of directories relative to the Blueprints file that will
Dan Willemsen273af7f2016-11-03 15:53:42 -070084 // be added to the include path (using -I) for this module and any module that links
85 // against this module
Colin Crossb916a382016-07-29 17:28:03 -070086 Export_include_dirs []string `android:"arch_variant"`
Dan Willemsen4416e5d2017-04-06 12:43:22 -070087
88 Target struct {
89 Vendor struct {
90 // list of exported include directories, like
91 // export_include_dirs, that will be applied to the
92 // vendor variant of this library. This will overwrite
93 // any other declarations.
94 Export_include_dirs []string
95 }
96 }
Colin Cross4d9c2d12016-07-29 12:48:20 -070097}
98
99func init() {
Colin Cross798bfce2016-10-12 14:28:16 -0700100 android.RegisterModuleType("cc_library_static", libraryStaticFactory)
101 android.RegisterModuleType("cc_library_shared", librarySharedFactory)
102 android.RegisterModuleType("cc_library", libraryFactory)
103 android.RegisterModuleType("cc_library_host_static", libraryHostStaticFactory)
104 android.RegisterModuleType("cc_library_host_shared", libraryHostSharedFactory)
Colin Cross5950f382016-12-13 12:50:57 -0800105 android.RegisterModuleType("cc_library_headers", libraryHeaderFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700106}
107
108// Module factory for combined static + shared libraries, device by default but with possible host
109// support
110func libraryFactory() (blueprint.Module, []interface{}) {
Colin Crossab3b7322016-12-09 14:46:15 -0800111 module, _ := NewLibrary(android.HostAndDeviceSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700112 return module.Init()
113}
114
115// Module factory for static libraries
116func libraryStaticFactory() (blueprint.Module, []interface{}) {
Colin Crossab3b7322016-12-09 14:46:15 -0800117 module, library := NewLibrary(android.HostAndDeviceSupported)
118 library.BuildOnlyStatic()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700119 return module.Init()
120}
121
122// Module factory for shared libraries
123func librarySharedFactory() (blueprint.Module, []interface{}) {
Colin Crossab3b7322016-12-09 14:46:15 -0800124 module, library := NewLibrary(android.HostAndDeviceSupported)
125 library.BuildOnlyShared()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700126 return module.Init()
127}
128
129// Module factory for host static libraries
130func libraryHostStaticFactory() (blueprint.Module, []interface{}) {
Colin Crossab3b7322016-12-09 14:46:15 -0800131 module, library := NewLibrary(android.HostSupported)
132 library.BuildOnlyStatic()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700133 return module.Init()
134}
135
136// Module factory for host shared libraries
137func libraryHostSharedFactory() (blueprint.Module, []interface{}) {
Colin Crossab3b7322016-12-09 14:46:15 -0800138 module, library := NewLibrary(android.HostSupported)
139 library.BuildOnlyShared()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700140 return module.Init()
141}
142
Colin Cross5950f382016-12-13 12:50:57 -0800143// Module factory for header-only libraries
144func libraryHeaderFactory() (blueprint.Module, []interface{}) {
145 module, library := NewLibrary(android.HostAndDeviceSupported)
146 library.HeaderOnly()
147 return module.Init()
148}
149
Colin Cross4d9c2d12016-07-29 12:48:20 -0700150type flagExporter struct {
151 Properties FlagExporterProperties
152
Dan Willemsen847dcc72016-09-29 12:13:36 -0700153 flags []string
154 flagsDeps android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700155}
156
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700157func (f *flagExporter) exportedIncludes(ctx ModuleContext) android.Paths {
158 if ctx.Vendor() && f.Properties.Target.Vendor.Export_include_dirs != nil {
159 return android.PathsForModuleSrc(ctx, f.Properties.Target.Vendor.Export_include_dirs)
160 } else {
161 return android.PathsForModuleSrc(ctx, f.Properties.Export_include_dirs)
162 }
163}
164
Colin Cross4d9c2d12016-07-29 12:48:20 -0700165func (f *flagExporter) exportIncludes(ctx ModuleContext, inc string) {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700166 includeDirs := f.exportedIncludes(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700167 for _, dir := range includeDirs.Strings() {
168 f.flags = append(f.flags, inc+dir)
169 }
170}
171
172func (f *flagExporter) reexportFlags(flags []string) {
173 f.flags = append(f.flags, flags...)
174}
175
Dan Willemsen847dcc72016-09-29 12:13:36 -0700176func (f *flagExporter) reexportDeps(deps android.Paths) {
177 f.flagsDeps = append(f.flagsDeps, deps...)
178}
179
Colin Cross4d9c2d12016-07-29 12:48:20 -0700180func (f *flagExporter) exportedFlags() []string {
181 return f.flags
182}
183
Dan Willemsen847dcc72016-09-29 12:13:36 -0700184func (f *flagExporter) exportedFlagsDeps() android.Paths {
185 return f.flagsDeps
186}
187
Colin Cross4d9c2d12016-07-29 12:48:20 -0700188type exportedFlagsProducer interface {
189 exportedFlags() []string
Dan Willemsen847dcc72016-09-29 12:13:36 -0700190 exportedFlagsDeps() android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700191}
192
193var _ exportedFlagsProducer = (*flagExporter)(nil)
194
Colin Crossb916a382016-07-29 17:28:03 -0700195// libraryDecorator wraps baseCompiler, baseLinker and baseInstaller to provide library-specific
196// functionality: static vs. shared linkage, reusing object files for shared libraries
197type libraryDecorator struct {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800198 Properties LibraryProperties
199 MutatedProperties LibraryMutatedProperties
Colin Cross4d9c2d12016-07-29 12:48:20 -0700200
201 // For reusing static library objects for shared library
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700202 reuseObjects Objects
Colin Cross26c34ed2016-09-30 17:10:16 -0700203 // table-of-contents file to optimize out relinking when possible
204 tocFile android.OptionalPath
Colin Cross4d9c2d12016-07-29 12:48:20 -0700205
Colin Cross4d9c2d12016-07-29 12:48:20 -0700206 flagExporter
207 stripper
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700208 relocationPacker
Colin Cross4d9c2d12016-07-29 12:48:20 -0700209
Colin Cross4d9c2d12016-07-29 12:48:20 -0700210 // If we're used as a whole_static_lib, our missing dependencies need
211 // to be given
212 wholeStaticMissingDeps []string
213
214 // For whole_static_libs
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700215 objects Objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700216
217 // Uses the module's name if empty, but can be overridden. Does not include
218 // shlib suffix.
219 libName string
Colin Crossb916a382016-07-29 17:28:03 -0700220
221 sanitize *sanitize
222
Dan Willemsen581341d2017-02-09 16:16:31 -0800223 // Output archive of gcno coverage information files
224 coverageOutputFile android.OptionalPath
225
Colin Crossb916a382016-07-29 17:28:03 -0700226 // Decorated interafaces
227 *baseCompiler
228 *baseLinker
229 *baseInstaller
Colin Cross4d9c2d12016-07-29 12:48:20 -0700230}
231
Colin Crossb916a382016-07-29 17:28:03 -0700232func (library *libraryDecorator) linkerProps() []interface{} {
233 var props []interface{}
234 props = append(props, library.baseLinker.linkerProps()...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700235 return append(props,
236 &library.Properties,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800237 &library.MutatedProperties,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700238 &library.flagExporter.Properties,
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700239 &library.stripper.StripProperties,
240 &library.relocationPacker.Properties)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700241}
242
Colin Crossb916a382016-07-29 17:28:03 -0700243func (library *libraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross42742b82016-08-01 13:20:05 -0700244 flags = library.baseLinker.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700245
Colin Crossb916a382016-07-29 17:28:03 -0700246 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
247 // all code is position independent, and then those warnings get promoted to
248 // errors.
Colin Cross3edeee12017-04-04 12:59:48 -0700249 if !ctx.Windows() {
Colin Crossb916a382016-07-29 17:28:03 -0700250 flags.CFlags = append(flags.CFlags, "-fPIC")
251 }
252
253 if library.static() {
254 flags.CFlags = append(flags.CFlags, library.Properties.Static.Cflags...)
Colin Crossa48ab5b2017-02-14 15:28:44 -0800255 } else if library.shared() {
Colin Crossb916a382016-07-29 17:28:03 -0700256 flags.CFlags = append(flags.CFlags, library.Properties.Shared.Cflags...)
257 }
258
Colin Crossa48ab5b2017-02-14 15:28:44 -0800259 if library.shared() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700260 libName := library.getLibName(ctx)
261 // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead
262 sharedFlag := "-Wl,-shared"
263 if flags.Clang || ctx.Host() {
264 sharedFlag = "-shared"
265 }
266 var f []string
Dan Willemsen01a405a2016-06-13 17:19:03 -0700267 if ctx.toolchain().Bionic() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700268 f = append(f,
269 "-nostdlib",
270 "-Wl,--gc-sections",
271 )
272 }
273
274 if ctx.Darwin() {
275 f = append(f,
276 "-dynamiclib",
277 "-single_module",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700278 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
279 )
Colin Cross7863cf52016-10-20 10:47:21 -0700280 if ctx.Arch().ArchType == android.X86 {
281 f = append(f,
282 "-read_only_relocs suppress",
283 )
284 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700285 } else {
286 f = append(f,
287 sharedFlag,
288 "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix())
289 }
290
291 flags.LdFlags = append(f, flags.LdFlags...)
292 }
293
294 return flags
295}
296
Dan Willemsen273af7f2016-11-03 15:53:42 -0700297func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700298 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700299 if len(exportIncludeDirs) > 0 {
300 flags.GlobalFlags = append(flags.GlobalFlags, includeDirsToFlags(exportIncludeDirs))
301 }
302
303 return library.baseCompiler.compilerFlags(ctx, flags)
304}
305
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700306func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Colin Cross5950f382016-12-13 12:50:57 -0800307 if !library.buildShared() && !library.buildStatic() {
308 if len(library.baseCompiler.Properties.Srcs) > 0 {
309 ctx.PropertyErrorf("srcs", "cc_library_headers must not have any srcs")
310 }
311 if len(library.Properties.Static.Srcs) > 0 {
312 ctx.PropertyErrorf("static.srcs", "cc_library_headers must not have any srcs")
313 }
314 if len(library.Properties.Shared.Srcs) > 0 {
315 ctx.PropertyErrorf("shared.srcs", "cc_library_headers must not have any srcs")
316 }
317 return Objects{}
318 }
319
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700320 objs := library.baseCompiler.compile(ctx, flags, deps)
321 library.reuseObjects = objs
Colin Cross2f336352016-10-26 10:03:47 -0700322 buildFlags := flagsToBuilderFlags(flags)
Colin Crossb916a382016-07-29 17:28:03 -0700323
Colin Cross4d9c2d12016-07-29 12:48:20 -0700324 if library.static() {
Colin Cross2f336352016-10-26 10:03:47 -0700325 srcs := android.PathsForModuleSrc(ctx, library.Properties.Static.Srcs)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700326 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceStaticLibrary,
327 srcs, library.baseCompiler.deps))
Colin Crossa48ab5b2017-02-14 15:28:44 -0800328 } else if library.shared() {
Colin Cross2f336352016-10-26 10:03:47 -0700329 srcs := android.PathsForModuleSrc(ctx, library.Properties.Shared.Srcs)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700330 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceSharedLibrary,
331 srcs, library.baseCompiler.deps))
Colin Crossb916a382016-07-29 17:28:03 -0700332 }
333
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700334 return objs
Colin Crossb916a382016-07-29 17:28:03 -0700335}
336
337type libraryInterface interface {
338 getWholeStaticMissingDeps() []string
339 static() bool
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700340 objs() Objects
341 reuseObjs() Objects
Colin Cross26c34ed2016-09-30 17:10:16 -0700342 toc() android.OptionalPath
Colin Crossb916a382016-07-29 17:28:03 -0700343
344 // Returns true if the build options for the module have selected a static or shared build
345 buildStatic() bool
346 buildShared() bool
347
348 // Sets whether a specific variant is static or shared
Colin Crossa48ab5b2017-02-14 15:28:44 -0800349 setStatic()
350 setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700351}
352
353func (library *libraryDecorator) getLibName(ctx ModuleContext) string {
354 name := library.libName
355 if name == "" {
Colin Crossce75d2c2016-10-06 16:12:58 -0700356 name = ctx.baseModuleName()
Colin Crossb916a382016-07-29 17:28:03 -0700357 }
358
359 if ctx.Host() && Bool(library.Properties.Unique_host_soname) {
360 if !strings.HasSuffix(name, "-host") {
361 name = name + "-host"
362 }
363 }
364
Colin Crossa48ab5b2017-02-14 15:28:44 -0800365 return name + library.MutatedProperties.VariantName
Colin Crossb916a382016-07-29 17:28:03 -0700366}
367
368func (library *libraryDecorator) linkerInit(ctx BaseModuleContext) {
369 location := InstallInSystem
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700370 if library.sanitize.inSanitizerDir() {
371 location = InstallInSanitizerDir
Colin Crossb916a382016-07-29 17:28:03 -0700372 }
373 library.baseInstaller.location = location
374
375 library.baseLinker.linkerInit(ctx)
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700376
377 library.relocationPacker.packingInit(ctx)
Colin Crossb916a382016-07-29 17:28:03 -0700378}
379
Colin Cross37047f12016-12-13 17:06:13 -0800380func (library *libraryDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700381 deps = library.baseLinker.linkerDeps(ctx, deps)
382
383 if library.static() {
384 deps.WholeStaticLibs = append(deps.WholeStaticLibs,
385 library.Properties.Static.Whole_static_libs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700386 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Static.Static_libs...)
387 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Static.Shared_libs...)
Colin Crossa48ab5b2017-02-14 15:28:44 -0800388 } else if library.shared() {
Dan Willemsen2e47b342016-11-17 01:02:25 -0800389 if ctx.toolchain().Bionic() && !Bool(library.baseLinker.Properties.Nocrt) {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700390 if !ctx.sdk() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700391 deps.CrtBegin = "crtbegin_so"
392 deps.CrtEnd = "crtend_so"
393 } else {
Dan Albertebedf672016-11-08 15:06:22 -0800394 // TODO(danalbert): Add generation of crt objects.
395 // For `sdk_version: "current"`, we don't actually have a
396 // freshly generated set of CRT objects. Use the last stable
397 // version.
398 version := ctx.sdkVersion()
399 if version == "current" {
400 version = ctx.AConfig().PlatformSdkVersion()
401 }
402 deps.CrtBegin = "ndk_crtbegin_so." + version
403 deps.CrtEnd = "ndk_crtend_so." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700404 }
405 }
406 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Shared.Whole_static_libs...)
407 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Shared.Static_libs...)
408 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Shared.Shared_libs...)
409 }
410
411 return deps
412}
413
Colin Crossb916a382016-07-29 17:28:03 -0700414func (library *libraryDecorator) linkStatic(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700415 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700416
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700417 library.objects = deps.WholeStaticLibObjs.Copy()
418 library.objects = library.objects.Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700419
420 outputFile := android.PathForModuleOut(ctx,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800421 ctx.ModuleName()+library.MutatedProperties.VariantName+staticLibraryExtension)
Dan Willemsen581341d2017-02-09 16:16:31 -0800422 builderFlags := flagsToBuilderFlags(flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700423
Dan Willemsen581341d2017-02-09 16:16:31 -0800424 TransformObjToStaticLib(ctx, library.objects.objFiles, builderFlags, outputFile, objs.tidyFiles)
425
426 library.coverageOutputFile = TransformCoverageFilesToLib(ctx, library.objects, builderFlags,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800427 ctx.ModuleName()+library.MutatedProperties.VariantName)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700428
429 library.wholeStaticMissingDeps = ctx.GetMissingDependencies()
430
431 ctx.CheckbuildFile(outputFile)
432
433 return outputFile
434}
435
Colin Crossb916a382016-07-29 17:28:03 -0700436func (library *libraryDecorator) linkShared(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
439 var linkerDeps android.Paths
440
441 versionScript := android.OptionalPathForModuleSrc(ctx, library.Properties.Version_script)
442 unexportedSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Unexported_symbols_list)
443 forceNotWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_not_weak_list)
444 forceWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_weak_list)
445 if !ctx.Darwin() {
446 if versionScript.Valid() {
447 flags.LdFlags = append(flags.LdFlags, "-Wl,--version-script,"+versionScript.String())
448 linkerDeps = append(linkerDeps, versionScript.Path())
449 }
450 if unexportedSymbols.Valid() {
451 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
452 }
453 if forceNotWeakSymbols.Valid() {
454 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
455 }
456 if forceWeakSymbols.Valid() {
457 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
458 }
459 } else {
460 if versionScript.Valid() {
461 ctx.PropertyErrorf("version_script", "Not supported on Darwin")
462 }
463 if unexportedSymbols.Valid() {
464 flags.LdFlags = append(flags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
465 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
466 }
467 if forceNotWeakSymbols.Valid() {
468 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
469 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
470 }
471 if forceWeakSymbols.Valid() {
472 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
473 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
474 }
475 }
476
477 fileName := library.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
478 outputFile := android.PathForModuleOut(ctx, fileName)
479 ret := outputFile
480
481 builderFlags := flagsToBuilderFlags(flags)
482
Colin Crossd8f8d072017-04-04 13:00:15 -0700483 if !ctx.Darwin() && !ctx.Windows() {
Colin Cross89562dc2016-10-03 17:47:19 -0700484 // Optimize out relinking against shared libraries whose interface hasn't changed by
485 // depending on a table of contents file instead of the library itself.
486 tocPath := outputFile.RelPathString()
487 tocPath = pathtools.ReplaceExtension(tocPath, flags.Toolchain.ShlibSuffix()[1:]+".toc")
488 tocFile := android.PathForOutput(ctx, tocPath)
489 library.tocFile = android.OptionalPathForPath(tocFile)
490 TransformSharedObjectToToc(ctx, outputFile, tocFile, builderFlags)
491 }
492
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700493 if library.relocationPacker.needsPacking(ctx) {
494 packedOutputFile := outputFile
495 outputFile = android.PathForModuleOut(ctx, "unpacked", fileName)
496 library.relocationPacker.pack(ctx, outputFile, packedOutputFile, builderFlags)
497 }
498
Colin Cross4d9c2d12016-07-29 12:48:20 -0700499 if library.stripper.needsStrip(ctx) {
500 strippedOutputFile := outputFile
501 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
502 library.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
503 }
504
505 sharedLibs := deps.SharedLibs
506 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
507
Dan Albertd015c4a2016-08-10 14:34:08 -0700508 // TODO(danalbert): Clean this up when soong supports prebuilts.
509 if strings.HasPrefix(ctx.selectedStl(), "ndk_libc++") {
510 libDir := getNdkStlLibDir(ctx, flags.Toolchain, "libc++")
511
512 if strings.HasSuffix(ctx.selectedStl(), "_shared") {
513 deps.StaticLibs = append(deps.StaticLibs,
514 libDir.Join(ctx, "libandroid_support.a"))
515 } else {
516 deps.StaticLibs = append(deps.StaticLibs,
517 libDir.Join(ctx, "libc++abi.a"),
518 libDir.Join(ctx, "libandroid_support.a"))
519 }
520
521 if ctx.Arch().ArchType == android.Arm {
522 deps.StaticLibs = append(deps.StaticLibs,
523 libDir.Join(ctx, "libunwind.a"))
524 }
525 }
526
Colin Cross26c34ed2016-09-30 17:10:16 -0700527 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
528 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700529 linkerDeps = append(linkerDeps, objs.tidyFiles...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700530
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700531 TransformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700532 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
533 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile)
534
Dan Willemsen581341d2017-02-09 16:16:31 -0800535 objs.coverageFiles = append(objs.coverageFiles, deps.StaticLibObjs.coverageFiles...)
536 objs.coverageFiles = append(objs.coverageFiles, deps.WholeStaticLibObjs.coverageFiles...)
537 library.coverageOutputFile = TransformCoverageFilesToLib(ctx, objs, builderFlags, library.getLibName(ctx))
538
Colin Cross4d9c2d12016-07-29 12:48:20 -0700539 return ret
540}
541
Colin Crossb916a382016-07-29 17:28:03 -0700542func (library *libraryDecorator) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700543 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700544
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700545 objs = objs.Append(deps.Objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700546
547 var out android.Path
Colin Crossa48ab5b2017-02-14 15:28:44 -0800548 if library.static() || library.header() {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700549 out = library.linkStatic(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700550 } else {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700551 out = library.linkShared(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700552 }
553
554 library.exportIncludes(ctx, "-I")
555 library.reexportFlags(deps.ReexportedFlags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700556 library.reexportDeps(deps.ReexportedFlagsDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700557
Dan Willemsene1240db2016-11-03 14:28:51 -0700558 if library.Properties.Aidl.Export_aidl_headers {
559 if library.baseCompiler.hasSrcExt(".aidl") {
560 library.reexportFlags([]string{
561 "-I" + android.PathForModuleGen(ctx, "aidl").String(),
562 })
563 library.reexportDeps(library.baseCompiler.deps) // TODO: restrict to aidl deps
564 }
565 }
566
567 if library.Properties.Proto.Export_proto_headers {
568 if library.baseCompiler.hasSrcExt(".proto") {
Colin Cross0c461f12016-10-20 16:11:43 -0700569 library.reexportFlags([]string{
570 "-I" + protoSubDir(ctx).String(),
571 "-I" + protoDir(ctx).String(),
572 })
573 library.reexportDeps(library.baseCompiler.deps) // TODO: restrict to proto deps
574 }
575 }
576
Colin Cross4d9c2d12016-07-29 12:48:20 -0700577 return out
578}
579
Colin Crossb916a382016-07-29 17:28:03 -0700580func (library *libraryDecorator) buildStatic() bool {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800581 return library.MutatedProperties.BuildStatic &&
Colin Cross4d9c2d12016-07-29 12:48:20 -0700582 (library.Properties.Static.Enabled == nil || *library.Properties.Static.Enabled)
583}
584
Colin Crossb916a382016-07-29 17:28:03 -0700585func (library *libraryDecorator) buildShared() bool {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800586 return library.MutatedProperties.BuildShared &&
Colin Cross4d9c2d12016-07-29 12:48:20 -0700587 (library.Properties.Shared.Enabled == nil || *library.Properties.Shared.Enabled)
588}
589
Colin Crossb916a382016-07-29 17:28:03 -0700590func (library *libraryDecorator) getWholeStaticMissingDeps() []string {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700591 return library.wholeStaticMissingDeps
592}
593
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700594func (library *libraryDecorator) objs() Objects {
595 return library.objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700596}
597
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700598func (library *libraryDecorator) reuseObjs() Objects {
599 return library.reuseObjects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700600}
601
Colin Cross26c34ed2016-09-30 17:10:16 -0700602func (library *libraryDecorator) toc() android.OptionalPath {
603 return library.tocFile
604}
605
Colin Crossb916a382016-07-29 17:28:03 -0700606func (library *libraryDecorator) install(ctx ModuleContext, file android.Path) {
607 if !ctx.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700608 library.baseInstaller.install(ctx, file)
609 }
610}
611
Colin Crossb916a382016-07-29 17:28:03 -0700612func (library *libraryDecorator) static() bool {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800613 return library.MutatedProperties.VariantIsStatic
Colin Cross4d9c2d12016-07-29 12:48:20 -0700614}
615
Colin Crossa48ab5b2017-02-14 15:28:44 -0800616func (library *libraryDecorator) shared() bool {
617 return library.MutatedProperties.VariantIsShared
618}
619
620func (library *libraryDecorator) header() bool {
621 return !library.static() && !library.shared()
622}
623
624func (library *libraryDecorator) setStatic() {
625 library.MutatedProperties.VariantIsStatic = true
626 library.MutatedProperties.VariantIsShared = false
627}
628
629func (library *libraryDecorator) setShared() {
630 library.MutatedProperties.VariantIsStatic = false
631 library.MutatedProperties.VariantIsShared = true
Colin Crossb916a382016-07-29 17:28:03 -0700632}
633
Colin Crossab3b7322016-12-09 14:46:15 -0800634func (library *libraryDecorator) BuildOnlyStatic() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800635 library.MutatedProperties.BuildShared = false
Colin Crossab3b7322016-12-09 14:46:15 -0800636}
637
638func (library *libraryDecorator) BuildOnlyShared() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800639 library.MutatedProperties.BuildStatic = false
Colin Crossab3b7322016-12-09 14:46:15 -0800640}
641
Colin Cross5950f382016-12-13 12:50:57 -0800642func (library *libraryDecorator) HeaderOnly() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800643 library.MutatedProperties.BuildShared = false
644 library.MutatedProperties.BuildStatic = false
Colin Cross5950f382016-12-13 12:50:57 -0800645}
646
Colin Crossab3b7322016-12-09 14:46:15 -0800647func NewLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700648 module := newModule(hod, android.MultilibBoth)
649
Colin Crossb916a382016-07-29 17:28:03 -0700650 library := &libraryDecorator{
Colin Crossa48ab5b2017-02-14 15:28:44 -0800651 MutatedProperties: LibraryMutatedProperties{
Colin Crossab3b7322016-12-09 14:46:15 -0800652 BuildShared: true,
653 BuildStatic: true,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700654 },
Colin Crossb916a382016-07-29 17:28:03 -0700655 baseCompiler: NewBaseCompiler(),
656 baseLinker: NewBaseLinker(),
657 baseInstaller: NewBaseInstaller("lib", "lib64", InstallInSystem),
658 sanitize: module.sanitize,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700659 }
660
Colin Crossb916a382016-07-29 17:28:03 -0700661 module.compiler = library
662 module.linker = library
663 module.installer = library
664
665 return module, library
666}
667
668func linkageMutator(mctx android.BottomUpMutatorContext) {
669 if m, ok := mctx.Module().(*Module); ok && m.linker != nil {
670 if library, ok := m.linker.(libraryInterface); ok {
671 var modules []blueprint.Module
672 if library.buildStatic() && library.buildShared() {
673 modules = mctx.CreateLocalVariations("static", "shared")
674 static := modules[0].(*Module)
675 shared := modules[1].(*Module)
676
Colin Crossa48ab5b2017-02-14 15:28:44 -0800677 static.linker.(libraryInterface).setStatic()
678 shared.linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700679
680 if staticCompiler, ok := static.compiler.(*libraryDecorator); ok {
681 sharedCompiler := shared.compiler.(*libraryDecorator)
682 if len(staticCompiler.Properties.Static.Cflags) == 0 &&
683 len(sharedCompiler.Properties.Shared.Cflags) == 0 {
684 // Optimize out compiling common .o files twice for static+shared libraries
685 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
686 sharedCompiler.baseCompiler.Properties.Srcs = nil
687 sharedCompiler.baseCompiler.Properties.Generated_sources = nil
688 }
689 }
690 } else if library.buildStatic() {
691 modules = mctx.CreateLocalVariations("static")
Colin Crossa48ab5b2017-02-14 15:28:44 -0800692 modules[0].(*Module).linker.(libraryInterface).setStatic()
Colin Crossb916a382016-07-29 17:28:03 -0700693 } else if library.buildShared() {
694 modules = mctx.CreateLocalVariations("shared")
Colin Crossa48ab5b2017-02-14 15:28:44 -0800695 modules[0].(*Module).linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700696 }
697 }
698 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700699}