blob: 12a866bb775d1fbe8fab03a4ab385a1d43536921 [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
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700205 reuseExportedDeps android.Paths
Colin Cross10d22312017-05-03 11:01:58 -0700206
Colin Cross26c34ed2016-09-30 17:10:16 -0700207 // table-of-contents file to optimize out relinking when possible
208 tocFile android.OptionalPath
Colin Cross4d9c2d12016-07-29 12:48:20 -0700209
Colin Cross4d9c2d12016-07-29 12:48:20 -0700210 flagExporter
211 stripper
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700212 relocationPacker
Colin Cross4d9c2d12016-07-29 12:48:20 -0700213
Colin Cross4d9c2d12016-07-29 12:48:20 -0700214 // If we're used as a whole_static_lib, our missing dependencies need
215 // to be given
216 wholeStaticMissingDeps []string
217
218 // For whole_static_libs
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700219 objects Objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700220
221 // Uses the module's name if empty, but can be overridden. Does not include
222 // shlib suffix.
223 libName string
Colin Crossb916a382016-07-29 17:28:03 -0700224
225 sanitize *sanitize
226
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800227 sabi *sabi
228
Dan Willemsen581341d2017-02-09 16:16:31 -0800229 // Output archive of gcno coverage information files
230 coverageOutputFile android.OptionalPath
231
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800232 // linked Source Abi Dump
233 sAbiOutputFile android.OptionalPath
234
235 // Source Abi Diff
236 sAbiDiff android.OptionalPath
237
Colin Crossb916a382016-07-29 17:28:03 -0700238 // Decorated interafaces
239 *baseCompiler
240 *baseLinker
241 *baseInstaller
Colin Cross4d9c2d12016-07-29 12:48:20 -0700242}
243
Colin Crossb916a382016-07-29 17:28:03 -0700244func (library *libraryDecorator) linkerProps() []interface{} {
245 var props []interface{}
246 props = append(props, library.baseLinker.linkerProps()...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700247 return append(props,
248 &library.Properties,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800249 &library.MutatedProperties,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700250 &library.flagExporter.Properties,
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700251 &library.stripper.StripProperties,
252 &library.relocationPacker.Properties)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700253}
254
Colin Crossb916a382016-07-29 17:28:03 -0700255func (library *libraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross42742b82016-08-01 13:20:05 -0700256 flags = library.baseLinker.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700257
Colin Crossb916a382016-07-29 17:28:03 -0700258 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
259 // all code is position independent, and then those warnings get promoted to
260 // errors.
Colin Cross3edeee12017-04-04 12:59:48 -0700261 if !ctx.Windows() {
Colin Crossb916a382016-07-29 17:28:03 -0700262 flags.CFlags = append(flags.CFlags, "-fPIC")
263 }
264
265 if library.static() {
266 flags.CFlags = append(flags.CFlags, library.Properties.Static.Cflags...)
Colin Crossa48ab5b2017-02-14 15:28:44 -0800267 } else if library.shared() {
Colin Crossb916a382016-07-29 17:28:03 -0700268 flags.CFlags = append(flags.CFlags, library.Properties.Shared.Cflags...)
269 }
270
Colin Crossa48ab5b2017-02-14 15:28:44 -0800271 if library.shared() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700272 libName := library.getLibName(ctx)
273 // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead
274 sharedFlag := "-Wl,-shared"
275 if flags.Clang || ctx.Host() {
276 sharedFlag = "-shared"
277 }
278 var f []string
Dan Willemsen01a405a2016-06-13 17:19:03 -0700279 if ctx.toolchain().Bionic() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700280 f = append(f,
281 "-nostdlib",
282 "-Wl,--gc-sections",
283 )
284 }
285
286 if ctx.Darwin() {
287 f = append(f,
288 "-dynamiclib",
289 "-single_module",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700290 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
291 )
Colin Cross7863cf52016-10-20 10:47:21 -0700292 if ctx.Arch().ArchType == android.X86 {
293 f = append(f,
294 "-read_only_relocs suppress",
295 )
296 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700297 } else {
298 f = append(f,
299 sharedFlag,
300 "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix())
301 }
302
303 flags.LdFlags = append(f, flags.LdFlags...)
304 }
305
306 return flags
307}
308
Dan Willemsen273af7f2016-11-03 15:53:42 -0700309func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700310 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700311 if len(exportIncludeDirs) > 0 {
Colin Crossdad8c952017-04-26 14:55:27 -0700312 f := includeDirsToFlags(exportIncludeDirs)
313 flags.GlobalFlags = append(flags.GlobalFlags, f)
314 flags.YasmFlags = append(flags.YasmFlags, f)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700315 }
316
317 return library.baseCompiler.compilerFlags(ctx, flags)
318}
319
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700320func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Colin Cross5950f382016-12-13 12:50:57 -0800321 if !library.buildShared() && !library.buildStatic() {
322 if len(library.baseCompiler.Properties.Srcs) > 0 {
323 ctx.PropertyErrorf("srcs", "cc_library_headers must not have any srcs")
324 }
325 if len(library.Properties.Static.Srcs) > 0 {
326 ctx.PropertyErrorf("static.srcs", "cc_library_headers must not have any srcs")
327 }
328 if len(library.Properties.Shared.Srcs) > 0 {
329 ctx.PropertyErrorf("shared.srcs", "cc_library_headers must not have any srcs")
330 }
331 return Objects{}
332 }
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800333 if ctx.createVndkSourceAbiDump() || (library.sabi.Properties.CreateSAbiDumps && ctx.Device()) {
334 exportIncludeDirs := android.PathsForModuleSrc(ctx, library.flagExporter.Properties.Export_include_dirs)
335 var SourceAbiFlags []string
336 for _, dir := range exportIncludeDirs.Strings() {
337 SourceAbiFlags = append(SourceAbiFlags, "-I "+dir)
338 }
Colin Cross5950f382016-12-13 12:50:57 -0800339
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800340 flags.SAbiFlags = SourceAbiFlags
341 total_length := len(library.baseCompiler.Properties.Srcs) + len(deps.GeneratedSources) + len(library.Properties.Shared.Srcs) +
342 len(library.Properties.Static.Srcs)
343 if total_length > 0 {
344 flags.SAbiDump = true
345 }
346 }
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700347 objs := library.baseCompiler.compile(ctx, flags, deps)
348 library.reuseObjects = objs
Colin Cross2f336352016-10-26 10:03:47 -0700349 buildFlags := flagsToBuilderFlags(flags)
Colin Crossb916a382016-07-29 17:28:03 -0700350
Colin Cross4d9c2d12016-07-29 12:48:20 -0700351 if library.static() {
Colin Cross2f336352016-10-26 10:03:47 -0700352 srcs := android.PathsForModuleSrc(ctx, library.Properties.Static.Srcs)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700353 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceStaticLibrary,
354 srcs, library.baseCompiler.deps))
Colin Crossa48ab5b2017-02-14 15:28:44 -0800355 } else if library.shared() {
Colin Cross2f336352016-10-26 10:03:47 -0700356 srcs := android.PathsForModuleSrc(ctx, library.Properties.Shared.Srcs)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700357 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceSharedLibrary,
358 srcs, library.baseCompiler.deps))
Colin Crossb916a382016-07-29 17:28:03 -0700359 }
360
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700361 return objs
Colin Crossb916a382016-07-29 17:28:03 -0700362}
363
364type libraryInterface interface {
365 getWholeStaticMissingDeps() []string
366 static() bool
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700367 objs() Objects
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700368 reuseObjs() (Objects, []string, android.Paths)
Colin Cross26c34ed2016-09-30 17:10:16 -0700369 toc() android.OptionalPath
Colin Crossb916a382016-07-29 17:28:03 -0700370
371 // Returns true if the build options for the module have selected a static or shared build
372 buildStatic() bool
373 buildShared() bool
374
375 // Sets whether a specific variant is static or shared
Colin Crossa48ab5b2017-02-14 15:28:44 -0800376 setStatic()
377 setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700378}
379
380func (library *libraryDecorator) getLibName(ctx ModuleContext) string {
381 name := library.libName
382 if name == "" {
Colin Crossce75d2c2016-10-06 16:12:58 -0700383 name = ctx.baseModuleName()
Colin Crossb916a382016-07-29 17:28:03 -0700384 }
385
386 if ctx.Host() && Bool(library.Properties.Unique_host_soname) {
387 if !strings.HasSuffix(name, "-host") {
388 name = name + "-host"
389 }
390 }
391
Colin Crossa48ab5b2017-02-14 15:28:44 -0800392 return name + library.MutatedProperties.VariantName
Colin Crossb916a382016-07-29 17:28:03 -0700393}
394
395func (library *libraryDecorator) linkerInit(ctx BaseModuleContext) {
396 location := InstallInSystem
Vishwath Mohan1dd88392017-03-29 22:00:18 -0700397 if library.sanitize.inSanitizerDir() {
398 location = InstallInSanitizerDir
Colin Crossb916a382016-07-29 17:28:03 -0700399 }
400 library.baseInstaller.location = location
401
402 library.baseLinker.linkerInit(ctx)
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700403
404 library.relocationPacker.packingInit(ctx)
Colin Crossb916a382016-07-29 17:28:03 -0700405}
406
Colin Cross37047f12016-12-13 17:06:13 -0800407func (library *libraryDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700408 deps = library.baseLinker.linkerDeps(ctx, deps)
409
410 if library.static() {
411 deps.WholeStaticLibs = append(deps.WholeStaticLibs,
412 library.Properties.Static.Whole_static_libs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700413 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Static.Static_libs...)
414 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Static.Shared_libs...)
Colin Crossa48ab5b2017-02-14 15:28:44 -0800415 } else if library.shared() {
Dan Willemsen2e47b342016-11-17 01:02:25 -0800416 if ctx.toolchain().Bionic() && !Bool(library.baseLinker.Properties.Nocrt) {
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700417 if !ctx.sdk() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700418 deps.CrtBegin = "crtbegin_so"
419 deps.CrtEnd = "crtend_so"
420 } else {
Dan Albertebedf672016-11-08 15:06:22 -0800421 // TODO(danalbert): Add generation of crt objects.
422 // For `sdk_version: "current"`, we don't actually have a
423 // freshly generated set of CRT objects. Use the last stable
424 // version.
425 version := ctx.sdkVersion()
426 if version == "current" {
427 version = ctx.AConfig().PlatformSdkVersion()
428 }
429 deps.CrtBegin = "ndk_crtbegin_so." + version
430 deps.CrtEnd = "ndk_crtend_so." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700431 }
432 }
433 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Shared.Whole_static_libs...)
434 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Shared.Static_libs...)
435 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Shared.Shared_libs...)
436 }
437
438 return deps
439}
440
Colin Crossb916a382016-07-29 17:28:03 -0700441func (library *libraryDecorator) linkStatic(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700442 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700443
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700444 library.objects = deps.WholeStaticLibObjs.Copy()
445 library.objects = library.objects.Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700446
447 outputFile := android.PathForModuleOut(ctx,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800448 ctx.ModuleName()+library.MutatedProperties.VariantName+staticLibraryExtension)
Dan Willemsen581341d2017-02-09 16:16:31 -0800449 builderFlags := flagsToBuilderFlags(flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700450
Dan Willemsen581341d2017-02-09 16:16:31 -0800451 TransformObjToStaticLib(ctx, library.objects.objFiles, builderFlags, outputFile, objs.tidyFiles)
452
453 library.coverageOutputFile = TransformCoverageFilesToLib(ctx, library.objects, builderFlags,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800454 ctx.ModuleName()+library.MutatedProperties.VariantName)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700455
456 library.wholeStaticMissingDeps = ctx.GetMissingDependencies()
457
458 ctx.CheckbuildFile(outputFile)
459
460 return outputFile
461}
462
Colin Crossb916a382016-07-29 17:28:03 -0700463func (library *libraryDecorator) linkShared(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700464 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700465
466 var linkerDeps android.Paths
467
468 versionScript := android.OptionalPathForModuleSrc(ctx, library.Properties.Version_script)
469 unexportedSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Unexported_symbols_list)
470 forceNotWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_not_weak_list)
471 forceWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_weak_list)
472 if !ctx.Darwin() {
473 if versionScript.Valid() {
474 flags.LdFlags = append(flags.LdFlags, "-Wl,--version-script,"+versionScript.String())
475 linkerDeps = append(linkerDeps, versionScript.Path())
476 }
477 if unexportedSymbols.Valid() {
478 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
479 }
480 if forceNotWeakSymbols.Valid() {
481 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
482 }
483 if forceWeakSymbols.Valid() {
484 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
485 }
486 } else {
487 if versionScript.Valid() {
488 ctx.PropertyErrorf("version_script", "Not supported on Darwin")
489 }
490 if unexportedSymbols.Valid() {
491 flags.LdFlags = append(flags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
492 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
493 }
494 if forceNotWeakSymbols.Valid() {
495 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
496 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
497 }
498 if forceWeakSymbols.Valid() {
499 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
500 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
501 }
502 }
503
504 fileName := library.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
505 outputFile := android.PathForModuleOut(ctx, fileName)
506 ret := outputFile
507
508 builderFlags := flagsToBuilderFlags(flags)
509
Colin Crossd8f8d072017-04-04 13:00:15 -0700510 if !ctx.Darwin() && !ctx.Windows() {
Colin Cross89562dc2016-10-03 17:47:19 -0700511 // Optimize out relinking against shared libraries whose interface hasn't changed by
512 // depending on a table of contents file instead of the library itself.
513 tocPath := outputFile.RelPathString()
514 tocPath = pathtools.ReplaceExtension(tocPath, flags.Toolchain.ShlibSuffix()[1:]+".toc")
515 tocFile := android.PathForOutput(ctx, tocPath)
516 library.tocFile = android.OptionalPathForPath(tocFile)
517 TransformSharedObjectToToc(ctx, outputFile, tocFile, builderFlags)
518 }
519
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700520 if library.relocationPacker.needsPacking(ctx) {
521 packedOutputFile := outputFile
522 outputFile = android.PathForModuleOut(ctx, "unpacked", fileName)
523 library.relocationPacker.pack(ctx, outputFile, packedOutputFile, builderFlags)
524 }
525
Colin Cross4d9c2d12016-07-29 12:48:20 -0700526 if library.stripper.needsStrip(ctx) {
527 strippedOutputFile := outputFile
528 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
529 library.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
530 }
531
532 sharedLibs := deps.SharedLibs
533 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
534
Dan Albertd015c4a2016-08-10 14:34:08 -0700535 // TODO(danalbert): Clean this up when soong supports prebuilts.
536 if strings.HasPrefix(ctx.selectedStl(), "ndk_libc++") {
537 libDir := getNdkStlLibDir(ctx, flags.Toolchain, "libc++")
538
539 if strings.HasSuffix(ctx.selectedStl(), "_shared") {
540 deps.StaticLibs = append(deps.StaticLibs,
541 libDir.Join(ctx, "libandroid_support.a"))
542 } else {
543 deps.StaticLibs = append(deps.StaticLibs,
544 libDir.Join(ctx, "libc++abi.a"),
545 libDir.Join(ctx, "libandroid_support.a"))
546 }
547
548 if ctx.Arch().ArchType == android.Arm {
549 deps.StaticLibs = append(deps.StaticLibs,
550 libDir.Join(ctx, "libunwind.a"))
551 }
552 }
553
Colin Cross26c34ed2016-09-30 17:10:16 -0700554 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
555 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700556 linkerDeps = append(linkerDeps, objs.tidyFiles...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700557
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700558 TransformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700559 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
560 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile)
561
Dan Willemsen581341d2017-02-09 16:16:31 -0800562 objs.coverageFiles = append(objs.coverageFiles, deps.StaticLibObjs.coverageFiles...)
563 objs.coverageFiles = append(objs.coverageFiles, deps.WholeStaticLibObjs.coverageFiles...)
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800564
565 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.StaticLibObjs.sAbiDumpFiles...)
566 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.WholeStaticLibObjs.sAbiDumpFiles...)
567
Dan Willemsen581341d2017-02-09 16:16:31 -0800568 library.coverageOutputFile = TransformCoverageFilesToLib(ctx, objs, builderFlags, library.getLibName(ctx))
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800569 library.linkSAbiDumpFiles(ctx, objs, fileName)
Dan Willemsen581341d2017-02-09 16:16:31 -0800570
Colin Cross4d9c2d12016-07-29 12:48:20 -0700571 return ret
572}
573
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800574func (library *libraryDecorator) linkSAbiDumpFiles(ctx ModuleContext, objs Objects, fileName string) {
575 //Also take into account object re-use.
576 if len(objs.sAbiDumpFiles) > 0 && ctx.createVndkSourceAbiDump() {
577 refSourceDumpFile := android.PathForVndkRefAbiDump(ctx, "current", fileName, vndkVsNdk(ctx), true)
578 versionScript := android.OptionalPathForModuleSrc(ctx, library.Properties.Version_script)
579 var symbolFile android.OptionalPath
580 if versionScript.Valid() {
581 symbolFile = versionScript
582 }
583 exportIncludeDirs := android.PathsForModuleSrc(ctx, library.flagExporter.Properties.Export_include_dirs)
584 var SourceAbiFlags []string
585 for _, dir := range exportIncludeDirs.Strings() {
586 SourceAbiFlags = append(SourceAbiFlags, "-I "+dir)
587 }
588 exportedHeaderFlags := strings.Join(SourceAbiFlags, " ")
589 library.sAbiOutputFile = TransformDumpToLinkedDump(ctx, objs.sAbiDumpFiles, symbolFile, "current", fileName, exportedHeaderFlags)
590 if refSourceDumpFile.Valid() {
591 library.sAbiDiff = SourceAbiDiff(ctx, library.sAbiOutputFile.Path(), refSourceDumpFile.Path(), fileName)
592 }
593 }
594}
595
596func vndkVsNdk(ctx ModuleContext) bool {
597 if inList(ctx.baseModuleName(), config.LLndkLibraries()) {
598 return false
599 }
600 return true
601}
602
Colin Crossb916a382016-07-29 17:28:03 -0700603func (library *libraryDecorator) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700604 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700605
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700606 objs = objs.Append(deps.Objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700607
608 var out android.Path
Colin Crossa48ab5b2017-02-14 15:28:44 -0800609 if library.static() || library.header() {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700610 out = library.linkStatic(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700611 } else {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700612 out = library.linkShared(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700613 }
614
615 library.exportIncludes(ctx, "-I")
616 library.reexportFlags(deps.ReexportedFlags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700617 library.reexportDeps(deps.ReexportedFlagsDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700618
Dan Willemsene1240db2016-11-03 14:28:51 -0700619 if library.Properties.Aidl.Export_aidl_headers {
620 if library.baseCompiler.hasSrcExt(".aidl") {
Colin Cross10d22312017-05-03 11:01:58 -0700621 flags := []string{
Dan Willemsene1240db2016-11-03 14:28:51 -0700622 "-I" + android.PathForModuleGen(ctx, "aidl").String(),
Colin Cross10d22312017-05-03 11:01:58 -0700623 }
624 library.reexportFlags(flags)
625 library.reuseExportedFlags = append(library.reuseExportedFlags, flags...)
Dan Willemsene1240db2016-11-03 14:28:51 -0700626 library.reexportDeps(library.baseCompiler.deps) // TODO: restrict to aidl deps
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700627 library.reuseExportedDeps = append(library.reuseExportedDeps, library.baseCompiler.deps...)
Dan Willemsene1240db2016-11-03 14:28:51 -0700628 }
629 }
630
631 if library.Properties.Proto.Export_proto_headers {
632 if library.baseCompiler.hasSrcExt(".proto") {
Colin Cross10d22312017-05-03 11:01:58 -0700633 flags := []string{
Colin Cross0c461f12016-10-20 16:11:43 -0700634 "-I" + protoSubDir(ctx).String(),
635 "-I" + protoDir(ctx).String(),
Colin Cross10d22312017-05-03 11:01:58 -0700636 }
637 library.reexportFlags(flags)
638 library.reuseExportedFlags = append(library.reuseExportedFlags, flags...)
Colin Cross0c461f12016-10-20 16:11:43 -0700639 library.reexportDeps(library.baseCompiler.deps) // TODO: restrict to proto deps
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700640 library.reuseExportedDeps = append(library.reuseExportedDeps, library.baseCompiler.deps...)
Colin Cross0c461f12016-10-20 16:11:43 -0700641 }
642 }
643
Colin Cross4d9c2d12016-07-29 12:48:20 -0700644 return out
645}
646
Colin Crossb916a382016-07-29 17:28:03 -0700647func (library *libraryDecorator) buildStatic() bool {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800648 return library.MutatedProperties.BuildStatic &&
Colin Cross4d9c2d12016-07-29 12:48:20 -0700649 (library.Properties.Static.Enabled == nil || *library.Properties.Static.Enabled)
650}
651
Colin Crossb916a382016-07-29 17:28:03 -0700652func (library *libraryDecorator) buildShared() bool {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800653 return library.MutatedProperties.BuildShared &&
Colin Cross4d9c2d12016-07-29 12:48:20 -0700654 (library.Properties.Shared.Enabled == nil || *library.Properties.Shared.Enabled)
655}
656
Colin Crossb916a382016-07-29 17:28:03 -0700657func (library *libraryDecorator) getWholeStaticMissingDeps() []string {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700658 return library.wholeStaticMissingDeps
659}
660
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700661func (library *libraryDecorator) objs() Objects {
662 return library.objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700663}
664
Colin Crossbbc9f4d2017-05-03 16:24:55 -0700665func (library *libraryDecorator) reuseObjs() (Objects, []string, android.Paths) {
666 return library.reuseObjects, library.reuseExportedFlags, library.reuseExportedDeps
Colin Cross4d9c2d12016-07-29 12:48:20 -0700667}
668
Colin Cross26c34ed2016-09-30 17:10:16 -0700669func (library *libraryDecorator) toc() android.OptionalPath {
670 return library.tocFile
671}
672
Colin Crossb916a382016-07-29 17:28:03 -0700673func (library *libraryDecorator) install(ctx ModuleContext, file android.Path) {
Colin Crossc43ae772017-04-14 15:42:53 -0700674 if library.shared() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700675 library.baseInstaller.install(ctx, file)
676 }
677}
678
Colin Crossb916a382016-07-29 17:28:03 -0700679func (library *libraryDecorator) static() bool {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800680 return library.MutatedProperties.VariantIsStatic
Colin Cross4d9c2d12016-07-29 12:48:20 -0700681}
682
Colin Crossa48ab5b2017-02-14 15:28:44 -0800683func (library *libraryDecorator) shared() bool {
684 return library.MutatedProperties.VariantIsShared
685}
686
687func (library *libraryDecorator) header() bool {
688 return !library.static() && !library.shared()
689}
690
691func (library *libraryDecorator) setStatic() {
692 library.MutatedProperties.VariantIsStatic = true
693 library.MutatedProperties.VariantIsShared = false
694}
695
696func (library *libraryDecorator) setShared() {
697 library.MutatedProperties.VariantIsStatic = false
698 library.MutatedProperties.VariantIsShared = true
Colin Crossb916a382016-07-29 17:28:03 -0700699}
700
Colin Crossab3b7322016-12-09 14:46:15 -0800701func (library *libraryDecorator) BuildOnlyStatic() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800702 library.MutatedProperties.BuildShared = false
Colin Crossab3b7322016-12-09 14:46:15 -0800703}
704
705func (library *libraryDecorator) BuildOnlyShared() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800706 library.MutatedProperties.BuildStatic = false
Colin Crossab3b7322016-12-09 14:46:15 -0800707}
708
Colin Cross5950f382016-12-13 12:50:57 -0800709func (library *libraryDecorator) HeaderOnly() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800710 library.MutatedProperties.BuildShared = false
711 library.MutatedProperties.BuildStatic = false
Colin Cross5950f382016-12-13 12:50:57 -0800712}
713
Colin Crossab3b7322016-12-09 14:46:15 -0800714func NewLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700715 module := newModule(hod, android.MultilibBoth)
716
Colin Crossb916a382016-07-29 17:28:03 -0700717 library := &libraryDecorator{
Colin Crossa48ab5b2017-02-14 15:28:44 -0800718 MutatedProperties: LibraryMutatedProperties{
Colin Crossab3b7322016-12-09 14:46:15 -0800719 BuildShared: true,
720 BuildStatic: true,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700721 },
Colin Crossb916a382016-07-29 17:28:03 -0700722 baseCompiler: NewBaseCompiler(),
723 baseLinker: NewBaseLinker(),
724 baseInstaller: NewBaseInstaller("lib", "lib64", InstallInSystem),
725 sanitize: module.sanitize,
Jayant Chowdhary3e231fd2017-02-08 13:45:53 -0800726 sabi: module.sabi,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700727 }
728
Colin Crossb916a382016-07-29 17:28:03 -0700729 module.compiler = library
730 module.linker = library
731 module.installer = library
732
733 return module, library
734}
735
Colin Cross10d22312017-05-03 11:01:58 -0700736// connects a shared library to a static library in order to reuse its .o files to avoid
737// compiling source files twice.
738func reuseStaticLibrary(mctx android.BottomUpMutatorContext, static, shared *Module) {
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
744 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
745 sharedCompiler.baseCompiler.Properties.OriginalSrcs =
746 sharedCompiler.baseCompiler.Properties.Srcs
747 sharedCompiler.baseCompiler.Properties.Srcs = nil
748 sharedCompiler.baseCompiler.Properties.Generated_sources = nil
749 }
750 }
751}
752
Colin Crossb916a382016-07-29 17:28:03 -0700753func linkageMutator(mctx android.BottomUpMutatorContext) {
754 if m, ok := mctx.Module().(*Module); ok && m.linker != nil {
755 if library, ok := m.linker.(libraryInterface); ok {
756 var modules []blueprint.Module
757 if library.buildStatic() && library.buildShared() {
758 modules = mctx.CreateLocalVariations("static", "shared")
759 static := modules[0].(*Module)
760 shared := modules[1].(*Module)
761
Colin Crossa48ab5b2017-02-14 15:28:44 -0800762 static.linker.(libraryInterface).setStatic()
763 shared.linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700764
Colin Cross10d22312017-05-03 11:01:58 -0700765 reuseStaticLibrary(mctx, static, shared)
766
Colin Crossb916a382016-07-29 17:28:03 -0700767 } else if library.buildStatic() {
768 modules = mctx.CreateLocalVariations("static")
Colin Crossa48ab5b2017-02-14 15:28:44 -0800769 modules[0].(*Module).linker.(libraryInterface).setStatic()
Colin Crossb916a382016-07-29 17:28:03 -0700770 } else if library.buildShared() {
771 modules = mctx.CreateLocalVariations("shared")
Colin Crossa48ab5b2017-02-14 15:28:44 -0800772 modules[0].(*Module).linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700773 }
774 }
775 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700776}