blob: 1a5de619df7c82d4826248732badaa33dd043dc9 [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
Colin Cross10d22312017-05-03 11:01:58 -0700203 reuseObjects Objects
204 reuseExportedFlags []string
205
Colin Cross26c34ed2016-09-30 17:10:16 -0700206 // table-of-contents file to optimize out relinking when possible
207 tocFile android.OptionalPath
Colin Cross4d9c2d12016-07-29 12:48:20 -0700208
Colin Cross4d9c2d12016-07-29 12:48:20 -0700209 flagExporter
210 stripper
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700211 relocationPacker
Colin Cross4d9c2d12016-07-29 12:48:20 -0700212
Colin Cross4d9c2d12016-07-29 12:48:20 -0700213 // If we're used as a whole_static_lib, our missing dependencies need
214 // to be given
215 wholeStaticMissingDeps []string
216
217 // For whole_static_libs
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700218 objects Objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700219
220 // Uses the module's name if empty, but can be overridden. Does not include
221 // shlib suffix.
222 libName string
Colin Crossb916a382016-07-29 17:28:03 -0700223
224 sanitize *sanitize
225
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800226 sabi *sabi
227
Dan Willemsen581341d2017-02-09 16:16:31 -0800228 // Output archive of gcno coverage information files
229 coverageOutputFile android.OptionalPath
230
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800231 // linked Source Abi Dump
232 sAbiOutputFile android.OptionalPath
233
234 // Source Abi Diff
235 sAbiDiff android.OptionalPath
236
Colin Crossb916a382016-07-29 17:28:03 -0700237 // Decorated interafaces
238 *baseCompiler
239 *baseLinker
240 *baseInstaller
Colin Cross4d9c2d12016-07-29 12:48:20 -0700241}
242
Colin Crossb916a382016-07-29 17:28:03 -0700243func (library *libraryDecorator) linkerProps() []interface{} {
244 var props []interface{}
245 props = append(props, library.baseLinker.linkerProps()...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700246 return append(props,
247 &library.Properties,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800248 &library.MutatedProperties,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700249 &library.flagExporter.Properties,
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700250 &library.stripper.StripProperties,
251 &library.relocationPacker.Properties)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700252}
253
Colin Crossb916a382016-07-29 17:28:03 -0700254func (library *libraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross42742b82016-08-01 13:20:05 -0700255 flags = library.baseLinker.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700256
Colin Crossb916a382016-07-29 17:28:03 -0700257 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
258 // all code is position independent, and then those warnings get promoted to
259 // errors.
Colin Cross3edeee12017-04-04 12:59:48 -0700260 if !ctx.Windows() {
Colin Crossb916a382016-07-29 17:28:03 -0700261 flags.CFlags = append(flags.CFlags, "-fPIC")
262 }
263
264 if library.static() {
265 flags.CFlags = append(flags.CFlags, library.Properties.Static.Cflags...)
Colin Crossa48ab5b2017-02-14 15:28:44 -0800266 } else if library.shared() {
Colin Crossb916a382016-07-29 17:28:03 -0700267 flags.CFlags = append(flags.CFlags, library.Properties.Shared.Cflags...)
268 }
269
Colin Crossa48ab5b2017-02-14 15:28:44 -0800270 if library.shared() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700271 libName := library.getLibName(ctx)
272 // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead
273 sharedFlag := "-Wl,-shared"
274 if flags.Clang || ctx.Host() {
275 sharedFlag = "-shared"
276 }
277 var f []string
Dan Willemsen01a405a2016-06-13 17:19:03 -0700278 if ctx.toolchain().Bionic() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700279 f = append(f,
280 "-nostdlib",
281 "-Wl,--gc-sections",
282 )
283 }
284
285 if ctx.Darwin() {
286 f = append(f,
287 "-dynamiclib",
288 "-single_module",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700289 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
290 )
Colin Cross7863cf52016-10-20 10:47:21 -0700291 if ctx.Arch().ArchType == android.X86 {
292 f = append(f,
293 "-read_only_relocs suppress",
294 )
295 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700296 } else {
297 f = append(f,
298 sharedFlag,
299 "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix())
300 }
301
302 flags.LdFlags = append(f, flags.LdFlags...)
303 }
304
305 return flags
306}
307
Dan Willemsen273af7f2016-11-03 15:53:42 -0700308func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700309 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700310 if len(exportIncludeDirs) > 0 {
Colin Crossdad8c952017-04-26 14:55:27 -0700311 f := includeDirsToFlags(exportIncludeDirs)
312 flags.GlobalFlags = append(flags.GlobalFlags, f)
313 flags.YasmFlags = append(flags.YasmFlags, f)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700314 }
315
316 return library.baseCompiler.compilerFlags(ctx, flags)
317}
318
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700319func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Colin Cross5950f382016-12-13 12:50:57 -0800320 if !library.buildShared() && !library.buildStatic() {
321 if len(library.baseCompiler.Properties.Srcs) > 0 {
322 ctx.PropertyErrorf("srcs", "cc_library_headers must not have any srcs")
323 }
324 if len(library.Properties.Static.Srcs) > 0 {
325 ctx.PropertyErrorf("static.srcs", "cc_library_headers must not have any srcs")
326 }
327 if len(library.Properties.Shared.Srcs) > 0 {
328 ctx.PropertyErrorf("shared.srcs", "cc_library_headers must not have any srcs")
329 }
330 return Objects{}
331 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800332 if ctx.createVndkSourceAbiDump() || (library.sabi.Properties.CreateSAbiDumps && ctx.Device()) {
333 exportIncludeDirs := android.PathsForModuleSrc(ctx, library.flagExporter.Properties.Export_include_dirs)
334 var SourceAbiFlags []string
335 for _, dir := range exportIncludeDirs.Strings() {
336 SourceAbiFlags = append(SourceAbiFlags, "-I "+dir)
337 }
Colin Cross5950f382016-12-13 12:50:57 -0800338
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800339 flags.SAbiFlags = SourceAbiFlags
340 total_length := len(library.baseCompiler.Properties.Srcs) + len(deps.GeneratedSources) + len(library.Properties.Shared.Srcs) +
341 len(library.Properties.Static.Srcs)
342 if total_length > 0 {
343 flags.SAbiDump = true
344 }
345 }
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700346 objs := library.baseCompiler.compile(ctx, flags, deps)
347 library.reuseObjects = objs
Colin Cross2f336352016-10-26 10:03:47 -0700348 buildFlags := flagsToBuilderFlags(flags)
Colin Crossb916a382016-07-29 17:28:03 -0700349
Colin Cross4d9c2d12016-07-29 12:48:20 -0700350 if library.static() {
Colin Cross2f336352016-10-26 10:03:47 -0700351 srcs := android.PathsForModuleSrc(ctx, library.Properties.Static.Srcs)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700352 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceStaticLibrary,
353 srcs, library.baseCompiler.deps))
Colin Crossa48ab5b2017-02-14 15:28:44 -0800354 } else if library.shared() {
Colin Cross2f336352016-10-26 10:03:47 -0700355 srcs := android.PathsForModuleSrc(ctx, library.Properties.Shared.Srcs)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700356 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceSharedLibrary,
357 srcs, library.baseCompiler.deps))
Colin Crossb916a382016-07-29 17:28:03 -0700358 }
359
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700360 return objs
Colin Crossb916a382016-07-29 17:28:03 -0700361}
362
363type libraryInterface interface {
364 getWholeStaticMissingDeps() []string
365 static() bool
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700366 objs() Objects
Colin Cross10d22312017-05-03 11:01:58 -0700367 reuseObjs() (Objects, []string)
Colin Cross26c34ed2016-09-30 17:10:16 -0700368 toc() android.OptionalPath
Colin Crossb916a382016-07-29 17:28:03 -0700369
370 // Returns true if the build options for the module have selected a static or shared build
371 buildStatic() bool
372 buildShared() bool
373
374 // Sets whether a specific variant is static or shared
Colin Crossa48ab5b2017-02-14 15:28:44 -0800375 setStatic()
376 setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700377}
378
379func (library *libraryDecorator) getLibName(ctx ModuleContext) string {
380 name := library.libName
381 if name == "" {
Colin Crossce75d2c2016-10-06 16:12:58 -0700382 name = ctx.baseModuleName()
Colin Crossb916a382016-07-29 17:28:03 -0700383 }
384
385 if ctx.Host() && Bool(library.Properties.Unique_host_soname) {
386 if !strings.HasSuffix(name, "-host") {
387 name = name + "-host"
388 }
389 }
390
Colin Crossa48ab5b2017-02-14 15:28:44 -0800391 return name + library.MutatedProperties.VariantName
Colin Crossb916a382016-07-29 17:28:03 -0700392}
393
394func (library *libraryDecorator) linkerInit(ctx BaseModuleContext) {
395 location := InstallInSystem
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700396 if library.sanitize.inSanitizerDir() {
397 location = InstallInSanitizerDir
Colin Crossb916a382016-07-29 17:28:03 -0700398 }
399 library.baseInstaller.location = location
400
401 library.baseLinker.linkerInit(ctx)
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700402
403 library.relocationPacker.packingInit(ctx)
Colin Crossb916a382016-07-29 17:28:03 -0700404}
405
Colin Cross37047f12016-12-13 17:06:13 -0800406func (library *libraryDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700407 deps = library.baseLinker.linkerDeps(ctx, deps)
408
409 if library.static() {
410 deps.WholeStaticLibs = append(deps.WholeStaticLibs,
411 library.Properties.Static.Whole_static_libs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700412 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Static.Static_libs...)
413 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Static.Shared_libs...)
Colin Crossa48ab5b2017-02-14 15:28:44 -0800414 } else if library.shared() {
Dan Willemsen2e47b342016-11-17 01:02:25 -0800415 if ctx.toolchain().Bionic() && !Bool(library.baseLinker.Properties.Nocrt) {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700416 if !ctx.sdk() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700417 deps.CrtBegin = "crtbegin_so"
418 deps.CrtEnd = "crtend_so"
419 } else {
Dan Albertebedf672016-11-08 15:06:22 -0800420 // TODO(danalbert): Add generation of crt objects.
421 // For `sdk_version: "current"`, we don't actually have a
422 // freshly generated set of CRT objects. Use the last stable
423 // version.
424 version := ctx.sdkVersion()
425 if version == "current" {
426 version = ctx.AConfig().PlatformSdkVersion()
427 }
428 deps.CrtBegin = "ndk_crtbegin_so." + version
429 deps.CrtEnd = "ndk_crtend_so." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700430 }
431 }
432 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Shared.Whole_static_libs...)
433 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Shared.Static_libs...)
434 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Shared.Shared_libs...)
435 }
436
437 return deps
438}
439
Colin Crossb916a382016-07-29 17:28:03 -0700440func (library *libraryDecorator) linkStatic(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700441 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700442
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700443 library.objects = deps.WholeStaticLibObjs.Copy()
444 library.objects = library.objects.Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700445
446 outputFile := android.PathForModuleOut(ctx,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800447 ctx.ModuleName()+library.MutatedProperties.VariantName+staticLibraryExtension)
Dan Willemsen581341d2017-02-09 16:16:31 -0800448 builderFlags := flagsToBuilderFlags(flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700449
Dan Willemsen581341d2017-02-09 16:16:31 -0800450 TransformObjToStaticLib(ctx, library.objects.objFiles, builderFlags, outputFile, objs.tidyFiles)
451
452 library.coverageOutputFile = TransformCoverageFilesToLib(ctx, library.objects, builderFlags,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800453 ctx.ModuleName()+library.MutatedProperties.VariantName)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700454
455 library.wholeStaticMissingDeps = ctx.GetMissingDependencies()
456
457 ctx.CheckbuildFile(outputFile)
458
459 return outputFile
460}
461
Colin Crossb916a382016-07-29 17:28:03 -0700462func (library *libraryDecorator) linkShared(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700463 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700464
465 var linkerDeps android.Paths
466
467 versionScript := android.OptionalPathForModuleSrc(ctx, library.Properties.Version_script)
468 unexportedSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Unexported_symbols_list)
469 forceNotWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_not_weak_list)
470 forceWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_weak_list)
471 if !ctx.Darwin() {
472 if versionScript.Valid() {
473 flags.LdFlags = append(flags.LdFlags, "-Wl,--version-script,"+versionScript.String())
474 linkerDeps = append(linkerDeps, versionScript.Path())
475 }
476 if unexportedSymbols.Valid() {
477 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
478 }
479 if forceNotWeakSymbols.Valid() {
480 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
481 }
482 if forceWeakSymbols.Valid() {
483 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
484 }
485 } else {
486 if versionScript.Valid() {
487 ctx.PropertyErrorf("version_script", "Not supported on Darwin")
488 }
489 if unexportedSymbols.Valid() {
490 flags.LdFlags = append(flags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
491 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
492 }
493 if forceNotWeakSymbols.Valid() {
494 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
495 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
496 }
497 if forceWeakSymbols.Valid() {
498 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
499 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
500 }
501 }
502
503 fileName := library.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
504 outputFile := android.PathForModuleOut(ctx, fileName)
505 ret := outputFile
506
507 builderFlags := flagsToBuilderFlags(flags)
508
Colin Crossd8f8d072017-04-04 13:00:15 -0700509 if !ctx.Darwin() && !ctx.Windows() {
Colin Cross89562dc2016-10-03 17:47:19 -0700510 // Optimize out relinking against shared libraries whose interface hasn't changed by
511 // depending on a table of contents file instead of the library itself.
512 tocPath := outputFile.RelPathString()
513 tocPath = pathtools.ReplaceExtension(tocPath, flags.Toolchain.ShlibSuffix()[1:]+".toc")
514 tocFile := android.PathForOutput(ctx, tocPath)
515 library.tocFile = android.OptionalPathForPath(tocFile)
516 TransformSharedObjectToToc(ctx, outputFile, tocFile, builderFlags)
517 }
518
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700519 if library.relocationPacker.needsPacking(ctx) {
520 packedOutputFile := outputFile
521 outputFile = android.PathForModuleOut(ctx, "unpacked", fileName)
522 library.relocationPacker.pack(ctx, outputFile, packedOutputFile, builderFlags)
523 }
524
Colin Cross4d9c2d12016-07-29 12:48:20 -0700525 if library.stripper.needsStrip(ctx) {
526 strippedOutputFile := outputFile
527 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
528 library.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
529 }
530
531 sharedLibs := deps.SharedLibs
532 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
533
Dan Albertd015c4a2016-08-10 14:34:08 -0700534 // TODO(danalbert): Clean this up when soong supports prebuilts.
535 if strings.HasPrefix(ctx.selectedStl(), "ndk_libc++") {
536 libDir := getNdkStlLibDir(ctx, flags.Toolchain, "libc++")
537
538 if strings.HasSuffix(ctx.selectedStl(), "_shared") {
539 deps.StaticLibs = append(deps.StaticLibs,
540 libDir.Join(ctx, "libandroid_support.a"))
541 } else {
542 deps.StaticLibs = append(deps.StaticLibs,
543 libDir.Join(ctx, "libc++abi.a"),
544 libDir.Join(ctx, "libandroid_support.a"))
545 }
546
547 if ctx.Arch().ArchType == android.Arm {
548 deps.StaticLibs = append(deps.StaticLibs,
549 libDir.Join(ctx, "libunwind.a"))
550 }
551 }
552
Colin Cross26c34ed2016-09-30 17:10:16 -0700553 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
554 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700555 linkerDeps = append(linkerDeps, objs.tidyFiles...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700556
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700557 TransformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700558 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
559 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile)
560
Dan Willemsen581341d2017-02-09 16:16:31 -0800561 objs.coverageFiles = append(objs.coverageFiles, deps.StaticLibObjs.coverageFiles...)
562 objs.coverageFiles = append(objs.coverageFiles, deps.WholeStaticLibObjs.coverageFiles...)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800563
564 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.StaticLibObjs.sAbiDumpFiles...)
565 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.WholeStaticLibObjs.sAbiDumpFiles...)
566
Dan Willemsen581341d2017-02-09 16:16:31 -0800567 library.coverageOutputFile = TransformCoverageFilesToLib(ctx, objs, builderFlags, library.getLibName(ctx))
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800568 library.linkSAbiDumpFiles(ctx, objs, fileName)
Dan Willemsen581341d2017-02-09 16:16:31 -0800569
Colin Cross4d9c2d12016-07-29 12:48:20 -0700570 return ret
571}
572
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800573func (library *libraryDecorator) linkSAbiDumpFiles(ctx ModuleContext, objs Objects, fileName string) {
574 //Also take into account object re-use.
575 if len(objs.sAbiDumpFiles) > 0 && ctx.createVndkSourceAbiDump() {
576 refSourceDumpFile := android.PathForVndkRefAbiDump(ctx, "current", fileName, vndkVsNdk(ctx), true)
577 versionScript := android.OptionalPathForModuleSrc(ctx, library.Properties.Version_script)
578 var symbolFile android.OptionalPath
579 if versionScript.Valid() {
580 symbolFile = versionScript
581 }
582 exportIncludeDirs := android.PathsForModuleSrc(ctx, library.flagExporter.Properties.Export_include_dirs)
583 var SourceAbiFlags []string
584 for _, dir := range exportIncludeDirs.Strings() {
585 SourceAbiFlags = append(SourceAbiFlags, "-I "+dir)
586 }
587 exportedHeaderFlags := strings.Join(SourceAbiFlags, " ")
588 library.sAbiOutputFile = TransformDumpToLinkedDump(ctx, objs.sAbiDumpFiles, symbolFile, "current", fileName, exportedHeaderFlags)
589 if refSourceDumpFile.Valid() {
590 library.sAbiDiff = SourceAbiDiff(ctx, library.sAbiOutputFile.Path(), refSourceDumpFile.Path(), fileName)
591 }
592 }
593}
594
595func vndkVsNdk(ctx ModuleContext) bool {
596 if inList(ctx.baseModuleName(), config.LLndkLibraries()) {
597 return false
598 }
599 return true
600}
601
Colin Crossb916a382016-07-29 17:28:03 -0700602func (library *libraryDecorator) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700603 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700604
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700605 objs = objs.Append(deps.Objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700606
607 var out android.Path
Colin Crossa48ab5b2017-02-14 15:28:44 -0800608 if library.static() || library.header() {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700609 out = library.linkStatic(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700610 } else {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700611 out = library.linkShared(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700612 }
613
614 library.exportIncludes(ctx, "-I")
615 library.reexportFlags(deps.ReexportedFlags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700616 library.reexportDeps(deps.ReexportedFlagsDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700617
Dan Willemsene1240db2016-11-03 14:28:51 -0700618 if library.Properties.Aidl.Export_aidl_headers {
619 if library.baseCompiler.hasSrcExt(".aidl") {
Colin Cross10d22312017-05-03 11:01:58 -0700620 flags := []string{
Dan Willemsene1240db2016-11-03 14:28:51 -0700621 "-I" + android.PathForModuleGen(ctx, "aidl").String(),
Colin Cross10d22312017-05-03 11:01:58 -0700622 }
623 library.reexportFlags(flags)
624 library.reuseExportedFlags = append(library.reuseExportedFlags, flags...)
Dan Willemsene1240db2016-11-03 14:28:51 -0700625 library.reexportDeps(library.baseCompiler.deps) // TODO: restrict to aidl deps
626 }
627 }
628
629 if library.Properties.Proto.Export_proto_headers {
630 if library.baseCompiler.hasSrcExt(".proto") {
Colin Cross10d22312017-05-03 11:01:58 -0700631 flags := []string{
Colin Cross0c461f12016-10-20 16:11:43 -0700632 "-I" + protoSubDir(ctx).String(),
633 "-I" + protoDir(ctx).String(),
Colin Cross10d22312017-05-03 11:01:58 -0700634 }
635 library.reexportFlags(flags)
636 library.reuseExportedFlags = append(library.reuseExportedFlags, flags...)
Colin Cross0c461f12016-10-20 16:11:43 -0700637 library.reexportDeps(library.baseCompiler.deps) // TODO: restrict to proto deps
638 }
639 }
640
Colin Cross4d9c2d12016-07-29 12:48:20 -0700641 return out
642}
643
Colin Crossb916a382016-07-29 17:28:03 -0700644func (library *libraryDecorator) buildStatic() bool {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800645 return library.MutatedProperties.BuildStatic &&
Colin Cross4d9c2d12016-07-29 12:48:20 -0700646 (library.Properties.Static.Enabled == nil || *library.Properties.Static.Enabled)
647}
648
Colin Crossb916a382016-07-29 17:28:03 -0700649func (library *libraryDecorator) buildShared() bool {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800650 return library.MutatedProperties.BuildShared &&
Colin Cross4d9c2d12016-07-29 12:48:20 -0700651 (library.Properties.Shared.Enabled == nil || *library.Properties.Shared.Enabled)
652}
653
Colin Crossb916a382016-07-29 17:28:03 -0700654func (library *libraryDecorator) getWholeStaticMissingDeps() []string {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700655 return library.wholeStaticMissingDeps
656}
657
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700658func (library *libraryDecorator) objs() Objects {
659 return library.objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700660}
661
Colin Cross10d22312017-05-03 11:01:58 -0700662func (library *libraryDecorator) reuseObjs() (Objects, []string) {
663 return library.reuseObjects, library.reuseExportedFlags
Colin Cross4d9c2d12016-07-29 12:48:20 -0700664}
665
Colin Cross26c34ed2016-09-30 17:10:16 -0700666func (library *libraryDecorator) toc() android.OptionalPath {
667 return library.tocFile
668}
669
Colin Crossb916a382016-07-29 17:28:03 -0700670func (library *libraryDecorator) install(ctx ModuleContext, file android.Path) {
Colin Crossc43ae772017-04-14 15:42:53 -0700671 if library.shared() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700672 library.baseInstaller.install(ctx, file)
673 }
674}
675
Colin Crossb916a382016-07-29 17:28:03 -0700676func (library *libraryDecorator) static() bool {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800677 return library.MutatedProperties.VariantIsStatic
Colin Cross4d9c2d12016-07-29 12:48:20 -0700678}
679
Colin Crossa48ab5b2017-02-14 15:28:44 -0800680func (library *libraryDecorator) shared() bool {
681 return library.MutatedProperties.VariantIsShared
682}
683
684func (library *libraryDecorator) header() bool {
685 return !library.static() && !library.shared()
686}
687
688func (library *libraryDecorator) setStatic() {
689 library.MutatedProperties.VariantIsStatic = true
690 library.MutatedProperties.VariantIsShared = false
691}
692
693func (library *libraryDecorator) setShared() {
694 library.MutatedProperties.VariantIsStatic = false
695 library.MutatedProperties.VariantIsShared = true
Colin Crossb916a382016-07-29 17:28:03 -0700696}
697
Colin Crossab3b7322016-12-09 14:46:15 -0800698func (library *libraryDecorator) BuildOnlyStatic() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800699 library.MutatedProperties.BuildShared = false
Colin Crossab3b7322016-12-09 14:46:15 -0800700}
701
702func (library *libraryDecorator) BuildOnlyShared() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800703 library.MutatedProperties.BuildStatic = false
Colin Crossab3b7322016-12-09 14:46:15 -0800704}
705
Colin Cross5950f382016-12-13 12:50:57 -0800706func (library *libraryDecorator) HeaderOnly() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800707 library.MutatedProperties.BuildShared = false
708 library.MutatedProperties.BuildStatic = false
Colin Cross5950f382016-12-13 12:50:57 -0800709}
710
Colin Crossab3b7322016-12-09 14:46:15 -0800711func NewLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700712 module := newModule(hod, android.MultilibBoth)
713
Colin Crossb916a382016-07-29 17:28:03 -0700714 library := &libraryDecorator{
Colin Crossa48ab5b2017-02-14 15:28:44 -0800715 MutatedProperties: LibraryMutatedProperties{
Colin Crossab3b7322016-12-09 14:46:15 -0800716 BuildShared: true,
717 BuildStatic: true,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700718 },
Colin Crossb916a382016-07-29 17:28:03 -0700719 baseCompiler: NewBaseCompiler(),
720 baseLinker: NewBaseLinker(),
721 baseInstaller: NewBaseInstaller("lib", "lib64", InstallInSystem),
722 sanitize: module.sanitize,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800723 sabi: module.sabi,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700724 }
725
Colin Crossb916a382016-07-29 17:28:03 -0700726 module.compiler = library
727 module.linker = library
728 module.installer = library
729
730 return module, library
731}
732
Colin Cross10d22312017-05-03 11:01:58 -0700733// connects a shared library to a static library in order to reuse its .o files to avoid
734// compiling source files twice.
735func reuseStaticLibrary(mctx android.BottomUpMutatorContext, static, shared *Module) {
736 if staticCompiler, ok := static.compiler.(*libraryDecorator); ok {
737 sharedCompiler := shared.compiler.(*libraryDecorator)
738 if len(staticCompiler.Properties.Static.Cflags) == 0 &&
739 len(sharedCompiler.Properties.Shared.Cflags) == 0 {
740
741 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
742 sharedCompiler.baseCompiler.Properties.OriginalSrcs =
743 sharedCompiler.baseCompiler.Properties.Srcs
744 sharedCompiler.baseCompiler.Properties.Srcs = nil
745 sharedCompiler.baseCompiler.Properties.Generated_sources = nil
746 }
747 }
748}
749
Colin Crossb916a382016-07-29 17:28:03 -0700750func linkageMutator(mctx android.BottomUpMutatorContext) {
751 if m, ok := mctx.Module().(*Module); ok && m.linker != nil {
752 if library, ok := m.linker.(libraryInterface); ok {
753 var modules []blueprint.Module
754 if library.buildStatic() && library.buildShared() {
755 modules = mctx.CreateLocalVariations("static", "shared")
756 static := modules[0].(*Module)
757 shared := modules[1].(*Module)
758
Colin Crossa48ab5b2017-02-14 15:28:44 -0800759 static.linker.(libraryInterface).setStatic()
760 shared.linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700761
Colin Cross10d22312017-05-03 11:01:58 -0700762 reuseStaticLibrary(mctx, static, shared)
763
Colin Crossb916a382016-07-29 17:28:03 -0700764 } else if library.buildStatic() {
765 modules = mctx.CreateLocalVariations("static")
Colin Crossa48ab5b2017-02-14 15:28:44 -0800766 modules[0].(*Module).linker.(libraryInterface).setStatic()
Colin Crossb916a382016-07-29 17:28:03 -0700767 } else if library.buildShared() {
768 modules = mctx.CreateLocalVariations("shared")
Colin Crossa48ab5b2017-02-14 15:28:44 -0800769 modules[0].(*Module).linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700770 }
771 }
772 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700773}