blob: 86e3269868321735b7545d6679dc9458aa89072f [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 {
Colin Crossdad8c952017-04-26 14:55:27 -0700309 f := includeDirsToFlags(exportIncludeDirs)
310 flags.GlobalFlags = append(flags.GlobalFlags, f)
311 flags.YasmFlags = append(flags.YasmFlags, f)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700312 }
313
314 return library.baseCompiler.compilerFlags(ctx, flags)
315}
316
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700317func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Colin Cross5950f382016-12-13 12:50:57 -0800318 if !library.buildShared() && !library.buildStatic() {
319 if len(library.baseCompiler.Properties.Srcs) > 0 {
320 ctx.PropertyErrorf("srcs", "cc_library_headers must not have any srcs")
321 }
322 if len(library.Properties.Static.Srcs) > 0 {
323 ctx.PropertyErrorf("static.srcs", "cc_library_headers must not have any srcs")
324 }
325 if len(library.Properties.Shared.Srcs) > 0 {
326 ctx.PropertyErrorf("shared.srcs", "cc_library_headers must not have any srcs")
327 }
328 return Objects{}
329 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800330 if ctx.createVndkSourceAbiDump() || (library.sabi.Properties.CreateSAbiDumps && ctx.Device()) {
331 exportIncludeDirs := android.PathsForModuleSrc(ctx, library.flagExporter.Properties.Export_include_dirs)
332 var SourceAbiFlags []string
333 for _, dir := range exportIncludeDirs.Strings() {
334 SourceAbiFlags = append(SourceAbiFlags, "-I "+dir)
335 }
Colin Cross5950f382016-12-13 12:50:57 -0800336
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800337 flags.SAbiFlags = SourceAbiFlags
338 total_length := len(library.baseCompiler.Properties.Srcs) + len(deps.GeneratedSources) + len(library.Properties.Shared.Srcs) +
339 len(library.Properties.Static.Srcs)
340 if total_length > 0 {
341 flags.SAbiDump = true
342 }
343 }
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700344 objs := library.baseCompiler.compile(ctx, flags, deps)
345 library.reuseObjects = objs
Colin Cross2f336352016-10-26 10:03:47 -0700346 buildFlags := flagsToBuilderFlags(flags)
Colin Crossb916a382016-07-29 17:28:03 -0700347
Colin Cross4d9c2d12016-07-29 12:48:20 -0700348 if library.static() {
Colin Cross2f336352016-10-26 10:03:47 -0700349 srcs := android.PathsForModuleSrc(ctx, library.Properties.Static.Srcs)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700350 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceStaticLibrary,
351 srcs, library.baseCompiler.deps))
Colin Crossa48ab5b2017-02-14 15:28:44 -0800352 } else if library.shared() {
Colin Cross2f336352016-10-26 10:03:47 -0700353 srcs := android.PathsForModuleSrc(ctx, library.Properties.Shared.Srcs)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700354 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceSharedLibrary,
355 srcs, library.baseCompiler.deps))
Colin Crossb916a382016-07-29 17:28:03 -0700356 }
357
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700358 return objs
Colin Crossb916a382016-07-29 17:28:03 -0700359}
360
361type libraryInterface interface {
362 getWholeStaticMissingDeps() []string
363 static() bool
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700364 objs() Objects
365 reuseObjs() Objects
Colin Cross26c34ed2016-09-30 17:10:16 -0700366 toc() android.OptionalPath
Colin Crossb916a382016-07-29 17:28:03 -0700367
368 // Returns true if the build options for the module have selected a static or shared build
369 buildStatic() bool
370 buildShared() bool
371
372 // Sets whether a specific variant is static or shared
Colin Crossa48ab5b2017-02-14 15:28:44 -0800373 setStatic()
374 setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700375}
376
377func (library *libraryDecorator) getLibName(ctx ModuleContext) string {
378 name := library.libName
379 if name == "" {
Colin Crossce75d2c2016-10-06 16:12:58 -0700380 name = ctx.baseModuleName()
Colin Crossb916a382016-07-29 17:28:03 -0700381 }
382
383 if ctx.Host() && Bool(library.Properties.Unique_host_soname) {
384 if !strings.HasSuffix(name, "-host") {
385 name = name + "-host"
386 }
387 }
388
Colin Crossa48ab5b2017-02-14 15:28:44 -0800389 return name + library.MutatedProperties.VariantName
Colin Crossb916a382016-07-29 17:28:03 -0700390}
391
392func (library *libraryDecorator) linkerInit(ctx BaseModuleContext) {
393 location := InstallInSystem
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700394 if library.sanitize.inSanitizerDir() {
395 location = InstallInSanitizerDir
Colin Crossb916a382016-07-29 17:28:03 -0700396 }
397 library.baseInstaller.location = location
398
399 library.baseLinker.linkerInit(ctx)
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700400
401 library.relocationPacker.packingInit(ctx)
Colin Crossb916a382016-07-29 17:28:03 -0700402}
403
Colin Cross37047f12016-12-13 17:06:13 -0800404func (library *libraryDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700405 deps = library.baseLinker.linkerDeps(ctx, deps)
406
407 if library.static() {
408 deps.WholeStaticLibs = append(deps.WholeStaticLibs,
409 library.Properties.Static.Whole_static_libs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700410 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Static.Static_libs...)
411 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Static.Shared_libs...)
Colin Crossa48ab5b2017-02-14 15:28:44 -0800412 } else if library.shared() {
Dan Willemsen2e47b342016-11-17 01:02:25 -0800413 if ctx.toolchain().Bionic() && !Bool(library.baseLinker.Properties.Nocrt) {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700414 if !ctx.sdk() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700415 deps.CrtBegin = "crtbegin_so"
416 deps.CrtEnd = "crtend_so"
417 } else {
Dan Albertebedf672016-11-08 15:06:22 -0800418 // TODO(danalbert): Add generation of crt objects.
419 // For `sdk_version: "current"`, we don't actually have a
420 // freshly generated set of CRT objects. Use the last stable
421 // version.
422 version := ctx.sdkVersion()
423 if version == "current" {
424 version = ctx.AConfig().PlatformSdkVersion()
425 }
426 deps.CrtBegin = "ndk_crtbegin_so." + version
427 deps.CrtEnd = "ndk_crtend_so." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700428 }
429 }
430 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Shared.Whole_static_libs...)
431 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Shared.Static_libs...)
432 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Shared.Shared_libs...)
433 }
434
435 return deps
436}
437
Colin Crossb916a382016-07-29 17:28:03 -0700438func (library *libraryDecorator) linkStatic(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700439 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700440
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700441 library.objects = deps.WholeStaticLibObjs.Copy()
442 library.objects = library.objects.Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700443
444 outputFile := android.PathForModuleOut(ctx,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800445 ctx.ModuleName()+library.MutatedProperties.VariantName+staticLibraryExtension)
Dan Willemsen581341d2017-02-09 16:16:31 -0800446 builderFlags := flagsToBuilderFlags(flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700447
Dan Willemsen581341d2017-02-09 16:16:31 -0800448 TransformObjToStaticLib(ctx, library.objects.objFiles, builderFlags, outputFile, objs.tidyFiles)
449
450 library.coverageOutputFile = TransformCoverageFilesToLib(ctx, library.objects, builderFlags,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800451 ctx.ModuleName()+library.MutatedProperties.VariantName)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700452
453 library.wholeStaticMissingDeps = ctx.GetMissingDependencies()
454
455 ctx.CheckbuildFile(outputFile)
456
457 return outputFile
458}
459
Colin Crossb916a382016-07-29 17:28:03 -0700460func (library *libraryDecorator) linkShared(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700461 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700462
463 var linkerDeps android.Paths
464
465 versionScript := android.OptionalPathForModuleSrc(ctx, library.Properties.Version_script)
466 unexportedSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Unexported_symbols_list)
467 forceNotWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_not_weak_list)
468 forceWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_weak_list)
469 if !ctx.Darwin() {
470 if versionScript.Valid() {
471 flags.LdFlags = append(flags.LdFlags, "-Wl,--version-script,"+versionScript.String())
472 linkerDeps = append(linkerDeps, versionScript.Path())
473 }
474 if unexportedSymbols.Valid() {
475 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
476 }
477 if forceNotWeakSymbols.Valid() {
478 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
479 }
480 if forceWeakSymbols.Valid() {
481 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
482 }
483 } else {
484 if versionScript.Valid() {
485 ctx.PropertyErrorf("version_script", "Not supported on Darwin")
486 }
487 if unexportedSymbols.Valid() {
488 flags.LdFlags = append(flags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
489 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
490 }
491 if forceNotWeakSymbols.Valid() {
492 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
493 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
494 }
495 if forceWeakSymbols.Valid() {
496 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
497 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
498 }
499 }
500
501 fileName := library.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
502 outputFile := android.PathForModuleOut(ctx, fileName)
503 ret := outputFile
504
505 builderFlags := flagsToBuilderFlags(flags)
506
Colin Crossd8f8d072017-04-04 13:00:15 -0700507 if !ctx.Darwin() && !ctx.Windows() {
Colin Cross89562dc2016-10-03 17:47:19 -0700508 // Optimize out relinking against shared libraries whose interface hasn't changed by
509 // depending on a table of contents file instead of the library itself.
510 tocPath := outputFile.RelPathString()
511 tocPath = pathtools.ReplaceExtension(tocPath, flags.Toolchain.ShlibSuffix()[1:]+".toc")
512 tocFile := android.PathForOutput(ctx, tocPath)
513 library.tocFile = android.OptionalPathForPath(tocFile)
514 TransformSharedObjectToToc(ctx, outputFile, tocFile, builderFlags)
515 }
516
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700517 if library.relocationPacker.needsPacking(ctx) {
518 packedOutputFile := outputFile
519 outputFile = android.PathForModuleOut(ctx, "unpacked", fileName)
520 library.relocationPacker.pack(ctx, outputFile, packedOutputFile, builderFlags)
521 }
522
Colin Cross4d9c2d12016-07-29 12:48:20 -0700523 if library.stripper.needsStrip(ctx) {
524 strippedOutputFile := outputFile
525 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
526 library.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
527 }
528
529 sharedLibs := deps.SharedLibs
530 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
531
Dan Albertd015c4a2016-08-10 14:34:08 -0700532 // TODO(danalbert): Clean this up when soong supports prebuilts.
533 if strings.HasPrefix(ctx.selectedStl(), "ndk_libc++") {
534 libDir := getNdkStlLibDir(ctx, flags.Toolchain, "libc++")
535
536 if strings.HasSuffix(ctx.selectedStl(), "_shared") {
537 deps.StaticLibs = append(deps.StaticLibs,
538 libDir.Join(ctx, "libandroid_support.a"))
539 } else {
540 deps.StaticLibs = append(deps.StaticLibs,
541 libDir.Join(ctx, "libc++abi.a"),
542 libDir.Join(ctx, "libandroid_support.a"))
543 }
544
545 if ctx.Arch().ArchType == android.Arm {
546 deps.StaticLibs = append(deps.StaticLibs,
547 libDir.Join(ctx, "libunwind.a"))
548 }
549 }
550
Colin Cross26c34ed2016-09-30 17:10:16 -0700551 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
552 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700553 linkerDeps = append(linkerDeps, objs.tidyFiles...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700554
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700555 TransformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700556 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
557 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile)
558
Dan Willemsen581341d2017-02-09 16:16:31 -0800559 objs.coverageFiles = append(objs.coverageFiles, deps.StaticLibObjs.coverageFiles...)
560 objs.coverageFiles = append(objs.coverageFiles, deps.WholeStaticLibObjs.coverageFiles...)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800561
562 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.StaticLibObjs.sAbiDumpFiles...)
563 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.WholeStaticLibObjs.sAbiDumpFiles...)
564
Dan Willemsen581341d2017-02-09 16:16:31 -0800565 library.coverageOutputFile = TransformCoverageFilesToLib(ctx, objs, builderFlags, library.getLibName(ctx))
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800566 library.linkSAbiDumpFiles(ctx, objs, fileName)
Dan Willemsen581341d2017-02-09 16:16:31 -0800567
Colin Cross4d9c2d12016-07-29 12:48:20 -0700568 return ret
569}
570
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800571func (library *libraryDecorator) linkSAbiDumpFiles(ctx ModuleContext, objs Objects, fileName string) {
572 //Also take into account object re-use.
573 if len(objs.sAbiDumpFiles) > 0 && ctx.createVndkSourceAbiDump() {
574 refSourceDumpFile := android.PathForVndkRefAbiDump(ctx, "current", fileName, vndkVsNdk(ctx), true)
575 versionScript := android.OptionalPathForModuleSrc(ctx, library.Properties.Version_script)
576 var symbolFile android.OptionalPath
577 if versionScript.Valid() {
578 symbolFile = versionScript
579 }
580 exportIncludeDirs := android.PathsForModuleSrc(ctx, library.flagExporter.Properties.Export_include_dirs)
581 var SourceAbiFlags []string
582 for _, dir := range exportIncludeDirs.Strings() {
583 SourceAbiFlags = append(SourceAbiFlags, "-I "+dir)
584 }
585 exportedHeaderFlags := strings.Join(SourceAbiFlags, " ")
586 library.sAbiOutputFile = TransformDumpToLinkedDump(ctx, objs.sAbiDumpFiles, symbolFile, "current", fileName, exportedHeaderFlags)
587 if refSourceDumpFile.Valid() {
588 library.sAbiDiff = SourceAbiDiff(ctx, library.sAbiOutputFile.Path(), refSourceDumpFile.Path(), fileName)
589 }
590 }
591}
592
593func vndkVsNdk(ctx ModuleContext) bool {
594 if inList(ctx.baseModuleName(), config.LLndkLibraries()) {
595 return false
596 }
597 return true
598}
599
Colin Crossb916a382016-07-29 17:28:03 -0700600func (library *libraryDecorator) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700601 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700602
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700603 objs = objs.Append(deps.Objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700604
605 var out android.Path
Colin Crossa48ab5b2017-02-14 15:28:44 -0800606 if library.static() || library.header() {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700607 out = library.linkStatic(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700608 } else {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700609 out = library.linkShared(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700610 }
611
612 library.exportIncludes(ctx, "-I")
613 library.reexportFlags(deps.ReexportedFlags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700614 library.reexportDeps(deps.ReexportedFlagsDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700615
Dan Willemsene1240db2016-11-03 14:28:51 -0700616 if library.Properties.Aidl.Export_aidl_headers {
617 if library.baseCompiler.hasSrcExt(".aidl") {
618 library.reexportFlags([]string{
619 "-I" + android.PathForModuleGen(ctx, "aidl").String(),
620 })
621 library.reexportDeps(library.baseCompiler.deps) // TODO: restrict to aidl deps
622 }
623 }
624
625 if library.Properties.Proto.Export_proto_headers {
626 if library.baseCompiler.hasSrcExt(".proto") {
Colin Cross0c461f12016-10-20 16:11:43 -0700627 library.reexportFlags([]string{
628 "-I" + protoSubDir(ctx).String(),
629 "-I" + protoDir(ctx).String(),
630 })
631 library.reexportDeps(library.baseCompiler.deps) // TODO: restrict to proto deps
632 }
633 }
634
Colin Cross4d9c2d12016-07-29 12:48:20 -0700635 return out
636}
637
Colin Crossb916a382016-07-29 17:28:03 -0700638func (library *libraryDecorator) buildStatic() bool {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800639 return library.MutatedProperties.BuildStatic &&
Colin Cross4d9c2d12016-07-29 12:48:20 -0700640 (library.Properties.Static.Enabled == nil || *library.Properties.Static.Enabled)
641}
642
Colin Crossb916a382016-07-29 17:28:03 -0700643func (library *libraryDecorator) buildShared() bool {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800644 return library.MutatedProperties.BuildShared &&
Colin Cross4d9c2d12016-07-29 12:48:20 -0700645 (library.Properties.Shared.Enabled == nil || *library.Properties.Shared.Enabled)
646}
647
Colin Crossb916a382016-07-29 17:28:03 -0700648func (library *libraryDecorator) getWholeStaticMissingDeps() []string {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700649 return library.wholeStaticMissingDeps
650}
651
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700652func (library *libraryDecorator) objs() Objects {
653 return library.objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700654}
655
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700656func (library *libraryDecorator) reuseObjs() Objects {
657 return library.reuseObjects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700658}
659
Colin Cross26c34ed2016-09-30 17:10:16 -0700660func (library *libraryDecorator) toc() android.OptionalPath {
661 return library.tocFile
662}
663
Colin Crossb916a382016-07-29 17:28:03 -0700664func (library *libraryDecorator) install(ctx ModuleContext, file android.Path) {
Colin Crossc43ae772017-04-14 15:42:53 -0700665 if library.shared() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700666 library.baseInstaller.install(ctx, file)
667 }
668}
669
Colin Crossb916a382016-07-29 17:28:03 -0700670func (library *libraryDecorator) static() bool {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800671 return library.MutatedProperties.VariantIsStatic
Colin Cross4d9c2d12016-07-29 12:48:20 -0700672}
673
Colin Crossa48ab5b2017-02-14 15:28:44 -0800674func (library *libraryDecorator) shared() bool {
675 return library.MutatedProperties.VariantIsShared
676}
677
678func (library *libraryDecorator) header() bool {
679 return !library.static() && !library.shared()
680}
681
682func (library *libraryDecorator) setStatic() {
683 library.MutatedProperties.VariantIsStatic = true
684 library.MutatedProperties.VariantIsShared = false
685}
686
687func (library *libraryDecorator) setShared() {
688 library.MutatedProperties.VariantIsStatic = false
689 library.MutatedProperties.VariantIsShared = true
Colin Crossb916a382016-07-29 17:28:03 -0700690}
691
Colin Crossab3b7322016-12-09 14:46:15 -0800692func (library *libraryDecorator) BuildOnlyStatic() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800693 library.MutatedProperties.BuildShared = false
Colin Crossab3b7322016-12-09 14:46:15 -0800694}
695
696func (library *libraryDecorator) BuildOnlyShared() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800697 library.MutatedProperties.BuildStatic = false
Colin Crossab3b7322016-12-09 14:46:15 -0800698}
699
Colin Cross5950f382016-12-13 12:50:57 -0800700func (library *libraryDecorator) HeaderOnly() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800701 library.MutatedProperties.BuildShared = false
702 library.MutatedProperties.BuildStatic = false
Colin Cross5950f382016-12-13 12:50:57 -0800703}
704
Colin Crossab3b7322016-12-09 14:46:15 -0800705func NewLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700706 module := newModule(hod, android.MultilibBoth)
707
Colin Crossb916a382016-07-29 17:28:03 -0700708 library := &libraryDecorator{
Colin Crossa48ab5b2017-02-14 15:28:44 -0800709 MutatedProperties: LibraryMutatedProperties{
Colin Crossab3b7322016-12-09 14:46:15 -0800710 BuildShared: true,
711 BuildStatic: true,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700712 },
Colin Crossb916a382016-07-29 17:28:03 -0700713 baseCompiler: NewBaseCompiler(),
714 baseLinker: NewBaseLinker(),
715 baseInstaller: NewBaseInstaller("lib", "lib64", InstallInSystem),
716 sanitize: module.sanitize,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800717 sabi: module.sabi,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700718 }
719
Colin Crossb916a382016-07-29 17:28:03 -0700720 module.compiler = library
721 module.linker = library
722 module.installer = library
723
724 return module, library
725}
726
727func linkageMutator(mctx android.BottomUpMutatorContext) {
728 if m, ok := mctx.Module().(*Module); ok && m.linker != nil {
729 if library, ok := m.linker.(libraryInterface); ok {
730 var modules []blueprint.Module
731 if library.buildStatic() && library.buildShared() {
732 modules = mctx.CreateLocalVariations("static", "shared")
733 static := modules[0].(*Module)
734 shared := modules[1].(*Module)
735
Colin Crossa48ab5b2017-02-14 15:28:44 -0800736 static.linker.(libraryInterface).setStatic()
737 shared.linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700738
739 if staticCompiler, ok := static.compiler.(*libraryDecorator); ok {
740 sharedCompiler := shared.compiler.(*libraryDecorator)
741 if len(staticCompiler.Properties.Static.Cflags) == 0 &&
742 len(sharedCompiler.Properties.Shared.Cflags) == 0 {
743 // Optimize out compiling common .o files twice for static+shared libraries
744 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
745 sharedCompiler.baseCompiler.Properties.Srcs = nil
746 sharedCompiler.baseCompiler.Properties.Generated_sources = nil
747 }
748 }
749 } else if library.buildStatic() {
750 modules = mctx.CreateLocalVariations("static")
Colin Crossa48ab5b2017-02-14 15:28:44 -0800751 modules[0].(*Module).linker.(libraryInterface).setStatic()
Colin Crossb916a382016-07-29 17:28:03 -0700752 } else if library.buildShared() {
753 modules = mctx.CreateLocalVariations("shared")
Colin Crossa48ab5b2017-02-14 15:28:44 -0800754 modules[0].(*Module).linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700755 }
756 }
757 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700758}