blob: f7194e46d1360f29f08ed561f30f127f0043cca0 [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"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070087}
88
89func init() {
Colin Cross798bfce2016-10-12 14:28:16 -070090 android.RegisterModuleType("cc_library_static", libraryStaticFactory)
91 android.RegisterModuleType("cc_library_shared", librarySharedFactory)
92 android.RegisterModuleType("cc_library", libraryFactory)
93 android.RegisterModuleType("cc_library_host_static", libraryHostStaticFactory)
94 android.RegisterModuleType("cc_library_host_shared", libraryHostSharedFactory)
Colin Cross5950f382016-12-13 12:50:57 -080095 android.RegisterModuleType("cc_library_headers", libraryHeaderFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -070096}
97
98// Module factory for combined static + shared libraries, device by default but with possible host
99// support
100func libraryFactory() (blueprint.Module, []interface{}) {
Colin Crossab3b7322016-12-09 14:46:15 -0800101 module, _ := NewLibrary(android.HostAndDeviceSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700102 return module.Init()
103}
104
105// Module factory for static libraries
106func libraryStaticFactory() (blueprint.Module, []interface{}) {
Colin Crossab3b7322016-12-09 14:46:15 -0800107 module, library := NewLibrary(android.HostAndDeviceSupported)
108 library.BuildOnlyStatic()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700109 return module.Init()
110}
111
112// Module factory for shared libraries
113func librarySharedFactory() (blueprint.Module, []interface{}) {
Colin Crossab3b7322016-12-09 14:46:15 -0800114 module, library := NewLibrary(android.HostAndDeviceSupported)
115 library.BuildOnlyShared()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700116 return module.Init()
117}
118
119// Module factory for host static libraries
120func libraryHostStaticFactory() (blueprint.Module, []interface{}) {
Colin Crossab3b7322016-12-09 14:46:15 -0800121 module, library := NewLibrary(android.HostSupported)
122 library.BuildOnlyStatic()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700123 return module.Init()
124}
125
126// Module factory for host shared libraries
127func libraryHostSharedFactory() (blueprint.Module, []interface{}) {
Colin Crossab3b7322016-12-09 14:46:15 -0800128 module, library := NewLibrary(android.HostSupported)
129 library.BuildOnlyShared()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700130 return module.Init()
131}
132
Colin Cross5950f382016-12-13 12:50:57 -0800133// Module factory for header-only libraries
134func libraryHeaderFactory() (blueprint.Module, []interface{}) {
135 module, library := NewLibrary(android.HostAndDeviceSupported)
136 library.HeaderOnly()
137 return module.Init()
138}
139
Colin Cross4d9c2d12016-07-29 12:48:20 -0700140type flagExporter struct {
141 Properties FlagExporterProperties
142
Dan Willemsen847dcc72016-09-29 12:13:36 -0700143 flags []string
144 flagsDeps android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700145}
146
147func (f *flagExporter) exportIncludes(ctx ModuleContext, inc string) {
148 includeDirs := android.PathsForModuleSrc(ctx, f.Properties.Export_include_dirs)
149 for _, dir := range includeDirs.Strings() {
150 f.flags = append(f.flags, inc+dir)
151 }
152}
153
154func (f *flagExporter) reexportFlags(flags []string) {
155 f.flags = append(f.flags, flags...)
156}
157
Dan Willemsen847dcc72016-09-29 12:13:36 -0700158func (f *flagExporter) reexportDeps(deps android.Paths) {
159 f.flagsDeps = append(f.flagsDeps, deps...)
160}
161
Colin Cross4d9c2d12016-07-29 12:48:20 -0700162func (f *flagExporter) exportedFlags() []string {
163 return f.flags
164}
165
Dan Willemsen847dcc72016-09-29 12:13:36 -0700166func (f *flagExporter) exportedFlagsDeps() android.Paths {
167 return f.flagsDeps
168}
169
Colin Cross4d9c2d12016-07-29 12:48:20 -0700170type exportedFlagsProducer interface {
171 exportedFlags() []string
Dan Willemsen847dcc72016-09-29 12:13:36 -0700172 exportedFlagsDeps() android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700173}
174
175var _ exportedFlagsProducer = (*flagExporter)(nil)
176
Colin Crossb916a382016-07-29 17:28:03 -0700177// libraryDecorator wraps baseCompiler, baseLinker and baseInstaller to provide library-specific
178// functionality: static vs. shared linkage, reusing object files for shared libraries
179type libraryDecorator struct {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800180 Properties LibraryProperties
181 MutatedProperties LibraryMutatedProperties
Colin Cross4d9c2d12016-07-29 12:48:20 -0700182
183 // For reusing static library objects for shared library
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700184 reuseObjects Objects
Colin Cross26c34ed2016-09-30 17:10:16 -0700185 // table-of-contents file to optimize out relinking when possible
186 tocFile android.OptionalPath
Colin Cross4d9c2d12016-07-29 12:48:20 -0700187
Colin Cross4d9c2d12016-07-29 12:48:20 -0700188 flagExporter
189 stripper
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700190 relocationPacker
Colin Cross4d9c2d12016-07-29 12:48:20 -0700191
Colin Cross4d9c2d12016-07-29 12:48:20 -0700192 // If we're used as a whole_static_lib, our missing dependencies need
193 // to be given
194 wholeStaticMissingDeps []string
195
196 // For whole_static_libs
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700197 objects Objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700198
199 // Uses the module's name if empty, but can be overridden. Does not include
200 // shlib suffix.
201 libName string
Colin Crossb916a382016-07-29 17:28:03 -0700202
203 sanitize *sanitize
204
Dan Willemsen581341d2017-02-09 16:16:31 -0800205 // Output archive of gcno coverage information files
206 coverageOutputFile android.OptionalPath
207
Colin Crossb916a382016-07-29 17:28:03 -0700208 // Decorated interafaces
209 *baseCompiler
210 *baseLinker
211 *baseInstaller
Colin Cross4d9c2d12016-07-29 12:48:20 -0700212}
213
Colin Crossb916a382016-07-29 17:28:03 -0700214func (library *libraryDecorator) linkerProps() []interface{} {
215 var props []interface{}
216 props = append(props, library.baseLinker.linkerProps()...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700217 return append(props,
218 &library.Properties,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800219 &library.MutatedProperties,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700220 &library.flagExporter.Properties,
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700221 &library.stripper.StripProperties,
222 &library.relocationPacker.Properties)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700223}
224
Colin Crossb916a382016-07-29 17:28:03 -0700225func (library *libraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross42742b82016-08-01 13:20:05 -0700226 flags = library.baseLinker.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700227
Colin Crossb916a382016-07-29 17:28:03 -0700228 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
229 // all code is position independent, and then those warnings get promoted to
230 // errors.
231 if ctx.Os() != android.Windows {
232 flags.CFlags = append(flags.CFlags, "-fPIC")
233 }
234
235 if library.static() {
236 flags.CFlags = append(flags.CFlags, library.Properties.Static.Cflags...)
Colin Crossa48ab5b2017-02-14 15:28:44 -0800237 } else if library.shared() {
Colin Crossb916a382016-07-29 17:28:03 -0700238 flags.CFlags = append(flags.CFlags, library.Properties.Shared.Cflags...)
239 }
240
Colin Crossa48ab5b2017-02-14 15:28:44 -0800241 if library.shared() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700242 libName := library.getLibName(ctx)
243 // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead
244 sharedFlag := "-Wl,-shared"
245 if flags.Clang || ctx.Host() {
246 sharedFlag = "-shared"
247 }
248 var f []string
249 if ctx.Device() {
250 f = append(f,
251 "-nostdlib",
252 "-Wl,--gc-sections",
253 )
254 }
255
256 if ctx.Darwin() {
257 f = append(f,
258 "-dynamiclib",
259 "-single_module",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700260 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
261 )
Colin Cross7863cf52016-10-20 10:47:21 -0700262 if ctx.Arch().ArchType == android.X86 {
263 f = append(f,
264 "-read_only_relocs suppress",
265 )
266 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700267 } else {
268 f = append(f,
269 sharedFlag,
270 "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix())
271 }
272
273 flags.LdFlags = append(f, flags.LdFlags...)
274 }
275
276 return flags
277}
278
Dan Willemsen273af7f2016-11-03 15:53:42 -0700279func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
280 exportIncludeDirs := android.PathsForModuleSrc(ctx, library.flagExporter.Properties.Export_include_dirs)
281 if len(exportIncludeDirs) > 0 {
282 flags.GlobalFlags = append(flags.GlobalFlags, includeDirsToFlags(exportIncludeDirs))
283 }
284
285 return library.baseCompiler.compilerFlags(ctx, flags)
286}
287
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700288func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Colin Cross5950f382016-12-13 12:50:57 -0800289 if !library.buildShared() && !library.buildStatic() {
290 if len(library.baseCompiler.Properties.Srcs) > 0 {
291 ctx.PropertyErrorf("srcs", "cc_library_headers must not have any srcs")
292 }
293 if len(library.Properties.Static.Srcs) > 0 {
294 ctx.PropertyErrorf("static.srcs", "cc_library_headers must not have any srcs")
295 }
296 if len(library.Properties.Shared.Srcs) > 0 {
297 ctx.PropertyErrorf("shared.srcs", "cc_library_headers must not have any srcs")
298 }
299 return Objects{}
300 }
301
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700302 objs := library.baseCompiler.compile(ctx, flags, deps)
303 library.reuseObjects = objs
Colin Cross2f336352016-10-26 10:03:47 -0700304 buildFlags := flagsToBuilderFlags(flags)
Colin Crossb916a382016-07-29 17:28:03 -0700305
Colin Cross4d9c2d12016-07-29 12:48:20 -0700306 if library.static() {
Colin Cross2f336352016-10-26 10:03:47 -0700307 srcs := android.PathsForModuleSrc(ctx, library.Properties.Static.Srcs)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700308 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceStaticLibrary,
309 srcs, library.baseCompiler.deps))
Colin Crossa48ab5b2017-02-14 15:28:44 -0800310 } else if library.shared() {
Colin Cross2f336352016-10-26 10:03:47 -0700311 srcs := android.PathsForModuleSrc(ctx, library.Properties.Shared.Srcs)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700312 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceSharedLibrary,
313 srcs, library.baseCompiler.deps))
Colin Crossb916a382016-07-29 17:28:03 -0700314 }
315
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700316 return objs
Colin Crossb916a382016-07-29 17:28:03 -0700317}
318
319type libraryInterface interface {
320 getWholeStaticMissingDeps() []string
321 static() bool
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700322 objs() Objects
323 reuseObjs() Objects
Colin Cross26c34ed2016-09-30 17:10:16 -0700324 toc() android.OptionalPath
Colin Crossb916a382016-07-29 17:28:03 -0700325
326 // Returns true if the build options for the module have selected a static or shared build
327 buildStatic() bool
328 buildShared() bool
329
330 // Sets whether a specific variant is static or shared
Colin Crossa48ab5b2017-02-14 15:28:44 -0800331 setStatic()
332 setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700333}
334
335func (library *libraryDecorator) getLibName(ctx ModuleContext) string {
336 name := library.libName
337 if name == "" {
Colin Crossce75d2c2016-10-06 16:12:58 -0700338 name = ctx.baseModuleName()
Colin Crossb916a382016-07-29 17:28:03 -0700339 }
340
341 if ctx.Host() && Bool(library.Properties.Unique_host_soname) {
342 if !strings.HasSuffix(name, "-host") {
343 name = name + "-host"
344 }
345 }
346
Colin Crossa48ab5b2017-02-14 15:28:44 -0800347 return name + library.MutatedProperties.VariantName
Colin Crossb916a382016-07-29 17:28:03 -0700348}
349
350func (library *libraryDecorator) linkerInit(ctx BaseModuleContext) {
351 location := InstallInSystem
352 if library.sanitize.inData() {
353 location = InstallInData
354 }
355 library.baseInstaller.location = location
356
357 library.baseLinker.linkerInit(ctx)
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700358
359 library.relocationPacker.packingInit(ctx)
Colin Crossb916a382016-07-29 17:28:03 -0700360}
361
Colin Cross37047f12016-12-13 17:06:13 -0800362func (library *libraryDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700363 deps = library.baseLinker.linkerDeps(ctx, deps)
364
365 if library.static() {
366 deps.WholeStaticLibs = append(deps.WholeStaticLibs,
367 library.Properties.Static.Whole_static_libs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700368 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Static.Static_libs...)
369 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Static.Shared_libs...)
Colin Crossa48ab5b2017-02-14 15:28:44 -0800370 } else if library.shared() {
Dan Willemsen2e47b342016-11-17 01:02:25 -0800371 if ctx.toolchain().Bionic() && !Bool(library.baseLinker.Properties.Nocrt) {
Dan Willemsend2ede872016-11-18 14:54:24 -0800372 if !ctx.sdk() && !ctx.vndk() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700373 deps.CrtBegin = "crtbegin_so"
374 deps.CrtEnd = "crtend_so"
375 } else {
Dan Albertebedf672016-11-08 15:06:22 -0800376 // TODO(danalbert): Add generation of crt objects.
377 // For `sdk_version: "current"`, we don't actually have a
378 // freshly generated set of CRT objects. Use the last stable
379 // version.
380 version := ctx.sdkVersion()
381 if version == "current" {
382 version = ctx.AConfig().PlatformSdkVersion()
383 }
384 deps.CrtBegin = "ndk_crtbegin_so." + version
385 deps.CrtEnd = "ndk_crtend_so." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700386 }
387 }
388 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Shared.Whole_static_libs...)
389 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Shared.Static_libs...)
390 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Shared.Shared_libs...)
391 }
392
393 return deps
394}
395
Colin Crossb916a382016-07-29 17:28:03 -0700396func (library *libraryDecorator) linkStatic(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700397 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700398
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700399 library.objects = deps.WholeStaticLibObjs.Copy()
400 library.objects = library.objects.Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700401
402 outputFile := android.PathForModuleOut(ctx,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800403 ctx.ModuleName()+library.MutatedProperties.VariantName+staticLibraryExtension)
Dan Willemsen581341d2017-02-09 16:16:31 -0800404 builderFlags := flagsToBuilderFlags(flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700405
Dan Willemsen581341d2017-02-09 16:16:31 -0800406 TransformObjToStaticLib(ctx, library.objects.objFiles, builderFlags, outputFile, objs.tidyFiles)
407
408 library.coverageOutputFile = TransformCoverageFilesToLib(ctx, library.objects, builderFlags,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800409 ctx.ModuleName()+library.MutatedProperties.VariantName)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700410
411 library.wholeStaticMissingDeps = ctx.GetMissingDependencies()
412
413 ctx.CheckbuildFile(outputFile)
414
415 return outputFile
416}
417
Colin Crossb916a382016-07-29 17:28:03 -0700418func (library *libraryDecorator) linkShared(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700419 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700420
421 var linkerDeps android.Paths
422
423 versionScript := android.OptionalPathForModuleSrc(ctx, library.Properties.Version_script)
424 unexportedSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Unexported_symbols_list)
425 forceNotWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_not_weak_list)
426 forceWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_weak_list)
427 if !ctx.Darwin() {
428 if versionScript.Valid() {
429 flags.LdFlags = append(flags.LdFlags, "-Wl,--version-script,"+versionScript.String())
430 linkerDeps = append(linkerDeps, versionScript.Path())
431 }
432 if unexportedSymbols.Valid() {
433 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
434 }
435 if forceNotWeakSymbols.Valid() {
436 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
437 }
438 if forceWeakSymbols.Valid() {
439 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
440 }
441 } else {
442 if versionScript.Valid() {
443 ctx.PropertyErrorf("version_script", "Not supported on Darwin")
444 }
445 if unexportedSymbols.Valid() {
446 flags.LdFlags = append(flags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
447 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
448 }
449 if forceNotWeakSymbols.Valid() {
450 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
451 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
452 }
453 if forceWeakSymbols.Valid() {
454 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
455 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
456 }
457 }
458
459 fileName := library.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
460 outputFile := android.PathForModuleOut(ctx, fileName)
461 ret := outputFile
462
463 builderFlags := flagsToBuilderFlags(flags)
464
Colin Cross89562dc2016-10-03 17:47:19 -0700465 if !ctx.Darwin() {
466 // Optimize out relinking against shared libraries whose interface hasn't changed by
467 // depending on a table of contents file instead of the library itself.
468 tocPath := outputFile.RelPathString()
469 tocPath = pathtools.ReplaceExtension(tocPath, flags.Toolchain.ShlibSuffix()[1:]+".toc")
470 tocFile := android.PathForOutput(ctx, tocPath)
471 library.tocFile = android.OptionalPathForPath(tocFile)
472 TransformSharedObjectToToc(ctx, outputFile, tocFile, builderFlags)
473 }
474
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700475 if library.relocationPacker.needsPacking(ctx) {
476 packedOutputFile := outputFile
477 outputFile = android.PathForModuleOut(ctx, "unpacked", fileName)
478 library.relocationPacker.pack(ctx, outputFile, packedOutputFile, builderFlags)
479 }
480
Colin Cross4d9c2d12016-07-29 12:48:20 -0700481 if library.stripper.needsStrip(ctx) {
482 strippedOutputFile := outputFile
483 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
484 library.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
485 }
486
487 sharedLibs := deps.SharedLibs
488 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
489
Dan Albertd015c4a2016-08-10 14:34:08 -0700490 // TODO(danalbert): Clean this up when soong supports prebuilts.
491 if strings.HasPrefix(ctx.selectedStl(), "ndk_libc++") {
492 libDir := getNdkStlLibDir(ctx, flags.Toolchain, "libc++")
493
494 if strings.HasSuffix(ctx.selectedStl(), "_shared") {
495 deps.StaticLibs = append(deps.StaticLibs,
496 libDir.Join(ctx, "libandroid_support.a"))
497 } else {
498 deps.StaticLibs = append(deps.StaticLibs,
499 libDir.Join(ctx, "libc++abi.a"),
500 libDir.Join(ctx, "libandroid_support.a"))
501 }
502
503 if ctx.Arch().ArchType == android.Arm {
504 deps.StaticLibs = append(deps.StaticLibs,
505 libDir.Join(ctx, "libunwind.a"))
506 }
507 }
508
Colin Cross26c34ed2016-09-30 17:10:16 -0700509 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
510 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700511 linkerDeps = append(linkerDeps, objs.tidyFiles...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700512
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700513 TransformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700514 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
515 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile)
516
Dan Willemsen581341d2017-02-09 16:16:31 -0800517 objs.coverageFiles = append(objs.coverageFiles, deps.StaticLibObjs.coverageFiles...)
518 objs.coverageFiles = append(objs.coverageFiles, deps.WholeStaticLibObjs.coverageFiles...)
519 library.coverageOutputFile = TransformCoverageFilesToLib(ctx, objs, builderFlags, library.getLibName(ctx))
520
Colin Cross4d9c2d12016-07-29 12:48:20 -0700521 return ret
522}
523
Colin Crossb916a382016-07-29 17:28:03 -0700524func (library *libraryDecorator) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700525 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700526
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700527 objs = objs.Append(deps.Objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700528
529 var out android.Path
Colin Crossa48ab5b2017-02-14 15:28:44 -0800530 if library.static() || library.header() {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700531 out = library.linkStatic(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700532 } else {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700533 out = library.linkShared(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700534 }
535
536 library.exportIncludes(ctx, "-I")
537 library.reexportFlags(deps.ReexportedFlags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700538 library.reexportDeps(deps.ReexportedFlagsDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700539
Dan Willemsene1240db2016-11-03 14:28:51 -0700540 if library.Properties.Aidl.Export_aidl_headers {
541 if library.baseCompiler.hasSrcExt(".aidl") {
542 library.reexportFlags([]string{
543 "-I" + android.PathForModuleGen(ctx, "aidl").String(),
544 })
545 library.reexportDeps(library.baseCompiler.deps) // TODO: restrict to aidl deps
546 }
547 }
548
549 if library.Properties.Proto.Export_proto_headers {
550 if library.baseCompiler.hasSrcExt(".proto") {
Colin Cross0c461f12016-10-20 16:11:43 -0700551 library.reexportFlags([]string{
552 "-I" + protoSubDir(ctx).String(),
553 "-I" + protoDir(ctx).String(),
554 })
555 library.reexportDeps(library.baseCompiler.deps) // TODO: restrict to proto deps
556 }
557 }
558
Colin Cross4d9c2d12016-07-29 12:48:20 -0700559 return out
560}
561
Colin Crossb916a382016-07-29 17:28:03 -0700562func (library *libraryDecorator) buildStatic() bool {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800563 return library.MutatedProperties.BuildStatic &&
Colin Cross4d9c2d12016-07-29 12:48:20 -0700564 (library.Properties.Static.Enabled == nil || *library.Properties.Static.Enabled)
565}
566
Colin Crossb916a382016-07-29 17:28:03 -0700567func (library *libraryDecorator) buildShared() bool {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800568 return library.MutatedProperties.BuildShared &&
Colin Cross4d9c2d12016-07-29 12:48:20 -0700569 (library.Properties.Shared.Enabled == nil || *library.Properties.Shared.Enabled)
570}
571
Colin Crossb916a382016-07-29 17:28:03 -0700572func (library *libraryDecorator) getWholeStaticMissingDeps() []string {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700573 return library.wholeStaticMissingDeps
574}
575
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700576func (library *libraryDecorator) objs() Objects {
577 return library.objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700578}
579
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700580func (library *libraryDecorator) reuseObjs() Objects {
581 return library.reuseObjects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700582}
583
Colin Cross26c34ed2016-09-30 17:10:16 -0700584func (library *libraryDecorator) toc() android.OptionalPath {
585 return library.tocFile
586}
587
Colin Crossb916a382016-07-29 17:28:03 -0700588func (library *libraryDecorator) install(ctx ModuleContext, file android.Path) {
589 if !ctx.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700590 library.baseInstaller.install(ctx, file)
591 }
592}
593
Colin Crossb916a382016-07-29 17:28:03 -0700594func (library *libraryDecorator) static() bool {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800595 return library.MutatedProperties.VariantIsStatic
Colin Cross4d9c2d12016-07-29 12:48:20 -0700596}
597
Colin Crossa48ab5b2017-02-14 15:28:44 -0800598func (library *libraryDecorator) shared() bool {
599 return library.MutatedProperties.VariantIsShared
600}
601
602func (library *libraryDecorator) header() bool {
603 return !library.static() && !library.shared()
604}
605
606func (library *libraryDecorator) setStatic() {
607 library.MutatedProperties.VariantIsStatic = true
608 library.MutatedProperties.VariantIsShared = false
609}
610
611func (library *libraryDecorator) setShared() {
612 library.MutatedProperties.VariantIsStatic = false
613 library.MutatedProperties.VariantIsShared = true
Colin Crossb916a382016-07-29 17:28:03 -0700614}
615
Colin Crossab3b7322016-12-09 14:46:15 -0800616func (library *libraryDecorator) BuildOnlyStatic() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800617 library.MutatedProperties.BuildShared = false
Colin Crossab3b7322016-12-09 14:46:15 -0800618}
619
620func (library *libraryDecorator) BuildOnlyShared() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800621 library.MutatedProperties.BuildStatic = false
Colin Crossab3b7322016-12-09 14:46:15 -0800622}
623
Colin Cross5950f382016-12-13 12:50:57 -0800624func (library *libraryDecorator) HeaderOnly() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800625 library.MutatedProperties.BuildShared = false
626 library.MutatedProperties.BuildStatic = false
Colin Cross5950f382016-12-13 12:50:57 -0800627}
628
Colin Crossab3b7322016-12-09 14:46:15 -0800629func NewLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700630 module := newModule(hod, android.MultilibBoth)
631
Colin Crossb916a382016-07-29 17:28:03 -0700632 library := &libraryDecorator{
Colin Crossa48ab5b2017-02-14 15:28:44 -0800633 MutatedProperties: LibraryMutatedProperties{
Colin Crossab3b7322016-12-09 14:46:15 -0800634 BuildShared: true,
635 BuildStatic: true,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700636 },
Colin Crossb916a382016-07-29 17:28:03 -0700637 baseCompiler: NewBaseCompiler(),
638 baseLinker: NewBaseLinker(),
639 baseInstaller: NewBaseInstaller("lib", "lib64", InstallInSystem),
640 sanitize: module.sanitize,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700641 }
642
Colin Crossb916a382016-07-29 17:28:03 -0700643 module.compiler = library
644 module.linker = library
645 module.installer = library
646
647 return module, library
648}
649
650func linkageMutator(mctx android.BottomUpMutatorContext) {
651 if m, ok := mctx.Module().(*Module); ok && m.linker != nil {
652 if library, ok := m.linker.(libraryInterface); ok {
653 var modules []blueprint.Module
654 if library.buildStatic() && library.buildShared() {
655 modules = mctx.CreateLocalVariations("static", "shared")
656 static := modules[0].(*Module)
657 shared := modules[1].(*Module)
658
Colin Crossa48ab5b2017-02-14 15:28:44 -0800659 static.linker.(libraryInterface).setStatic()
660 shared.linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700661
662 if staticCompiler, ok := static.compiler.(*libraryDecorator); ok {
663 sharedCompiler := shared.compiler.(*libraryDecorator)
664 if len(staticCompiler.Properties.Static.Cflags) == 0 &&
665 len(sharedCompiler.Properties.Shared.Cflags) == 0 {
666 // Optimize out compiling common .o files twice for static+shared libraries
667 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
668 sharedCompiler.baseCompiler.Properties.Srcs = nil
669 sharedCompiler.baseCompiler.Properties.Generated_sources = nil
670 }
671 }
672 } else if library.buildStatic() {
673 modules = mctx.CreateLocalVariations("static")
Colin Crossa48ab5b2017-02-14 15:28:44 -0800674 modules[0].(*Module).linker.(libraryInterface).setStatic()
Colin Crossb916a382016-07-29 17:28:03 -0700675 } else if library.buildShared() {
676 modules = mctx.CreateLocalVariations("shared")
Colin Crossa48ab5b2017-02-14 15:28:44 -0800677 modules[0].(*Module).linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700678 }
679 }
680 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700681}