blob: efaf440865669d9295f5f728a7ba0580b2916cbc [file] [log] [blame]
Colin Cross4d9c2d12016-07-29 12:48:20 -07001// Copyright 2016 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package cc
16
17import (
18 "strings"
19
20 "github.com/google/blueprint"
Colin Cross26c34ed2016-09-30 17:10:16 -070021 "github.com/google/blueprint/pathtools"
Colin Cross4d9c2d12016-07-29 12:48:20 -070022
Colin Cross4d9c2d12016-07-29 12:48:20 -070023 "android/soong/android"
24)
25
Colin Crossb916a382016-07-29 17:28:03 -070026type LibraryProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -070027 Static struct {
Colin Cross2f336352016-10-26 10:03:47 -070028 Srcs []string `android:"arch_variant"`
29 Cflags []string `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070030
Colin Cross4d9c2d12016-07-29 12:48:20 -070031 Enabled *bool `android:"arch_variant"`
32 Whole_static_libs []string `android:"arch_variant"`
33 Static_libs []string `android:"arch_variant"`
34 Shared_libs []string `android:"arch_variant"`
35 } `android:"arch_variant"`
36 Shared struct {
Colin Cross2f336352016-10-26 10:03:47 -070037 Srcs []string `android:"arch_variant"`
38 Cflags []string `android:"arch_variant"`
Colin Crossb916a382016-07-29 17:28:03 -070039
Colin Cross4d9c2d12016-07-29 12:48:20 -070040 Enabled *bool `android:"arch_variant"`
41 Whole_static_libs []string `android:"arch_variant"`
42 Static_libs []string `android:"arch_variant"`
43 Shared_libs []string `android:"arch_variant"`
44 } `android:"arch_variant"`
45
46 // local file name to pass to the linker as --version_script
47 Version_script *string `android:"arch_variant"`
48 // local file name to pass to the linker as -unexported_symbols_list
49 Unexported_symbols_list *string `android:"arch_variant"`
50 // local file name to pass to the linker as -force_symbols_not_weak_list
51 Force_symbols_not_weak_list *string `android:"arch_variant"`
52 // local file name to pass to the linker as -force_symbols_weak_list
53 Force_symbols_weak_list *string `android:"arch_variant"`
54
55 // rename host libraries to prevent overlap with system installed libraries
56 Unique_host_soname *bool
57
Dan Willemsene1240db2016-11-03 14:28:51 -070058 Aidl struct {
59 // export headers generated from .aidl sources
60 Export_aidl_headers bool
61 }
62
Colin Cross0c461f12016-10-20 16:11:43 -070063 Proto struct {
64 // export headers generated from .proto sources
65 Export_proto_headers bool
66 }
67
Colin Cross4d9c2d12016-07-29 12:48:20 -070068 VariantName string `blueprint:"mutated"`
Colin Crossb916a382016-07-29 17:28:03 -070069
70 // Build a static variant
71 BuildStatic bool `blueprint:"mutated"`
72 // Build a shared variant
73 BuildShared bool `blueprint:"mutated"`
74 // This variant is shared
75 VariantIsShared bool `blueprint:"mutated"`
76 // This variant is static
77 VariantIsStatic bool `blueprint:"mutated"`
78}
79
80type FlagExporterProperties struct {
81 // list of directories relative to the Blueprints file that will
Dan Willemsen273af7f2016-11-03 15:53:42 -070082 // be added to the include path (using -I) for this module and any module that links
83 // against this module
Colin Crossb916a382016-07-29 17:28:03 -070084 Export_include_dirs []string `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070085}
86
87func init() {
Colin Cross798bfce2016-10-12 14:28:16 -070088 android.RegisterModuleType("cc_library_static", libraryStaticFactory)
89 android.RegisterModuleType("cc_library_shared", librarySharedFactory)
90 android.RegisterModuleType("cc_library", libraryFactory)
91 android.RegisterModuleType("cc_library_host_static", libraryHostStaticFactory)
92 android.RegisterModuleType("cc_library_host_shared", libraryHostSharedFactory)
Colin Cross5950f382016-12-13 12:50:57 -080093 android.RegisterModuleType("cc_library_headers", libraryHeaderFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -070094}
95
96// Module factory for combined static + shared libraries, device by default but with possible host
97// support
98func libraryFactory() (blueprint.Module, []interface{}) {
Colin Crossab3b7322016-12-09 14:46:15 -080099 module, _ := NewLibrary(android.HostAndDeviceSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700100 return module.Init()
101}
102
103// Module factory for static libraries
104func libraryStaticFactory() (blueprint.Module, []interface{}) {
Colin Crossab3b7322016-12-09 14:46:15 -0800105 module, library := NewLibrary(android.HostAndDeviceSupported)
106 library.BuildOnlyStatic()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700107 return module.Init()
108}
109
110// Module factory for shared libraries
111func librarySharedFactory() (blueprint.Module, []interface{}) {
Colin Crossab3b7322016-12-09 14:46:15 -0800112 module, library := NewLibrary(android.HostAndDeviceSupported)
113 library.BuildOnlyShared()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700114 return module.Init()
115}
116
117// Module factory for host static libraries
118func libraryHostStaticFactory() (blueprint.Module, []interface{}) {
Colin Crossab3b7322016-12-09 14:46:15 -0800119 module, library := NewLibrary(android.HostSupported)
120 library.BuildOnlyStatic()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700121 return module.Init()
122}
123
124// Module factory for host shared libraries
125func libraryHostSharedFactory() (blueprint.Module, []interface{}) {
Colin Crossab3b7322016-12-09 14:46:15 -0800126 module, library := NewLibrary(android.HostSupported)
127 library.BuildOnlyShared()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700128 return module.Init()
129}
130
Colin Cross5950f382016-12-13 12:50:57 -0800131// Module factory for header-only libraries
132func libraryHeaderFactory() (blueprint.Module, []interface{}) {
133 module, library := NewLibrary(android.HostAndDeviceSupported)
134 library.HeaderOnly()
135 return module.Init()
136}
137
Colin Cross4d9c2d12016-07-29 12:48:20 -0700138type flagExporter struct {
139 Properties FlagExporterProperties
140
Dan Willemsen847dcc72016-09-29 12:13:36 -0700141 flags []string
142 flagsDeps android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700143}
144
145func (f *flagExporter) exportIncludes(ctx ModuleContext, inc string) {
146 includeDirs := android.PathsForModuleSrc(ctx, f.Properties.Export_include_dirs)
147 for _, dir := range includeDirs.Strings() {
148 f.flags = append(f.flags, inc+dir)
149 }
150}
151
152func (f *flagExporter) reexportFlags(flags []string) {
153 f.flags = append(f.flags, flags...)
154}
155
Dan Willemsen847dcc72016-09-29 12:13:36 -0700156func (f *flagExporter) reexportDeps(deps android.Paths) {
157 f.flagsDeps = append(f.flagsDeps, deps...)
158}
159
Colin Cross4d9c2d12016-07-29 12:48:20 -0700160func (f *flagExporter) exportedFlags() []string {
161 return f.flags
162}
163
Dan Willemsen847dcc72016-09-29 12:13:36 -0700164func (f *flagExporter) exportedFlagsDeps() android.Paths {
165 return f.flagsDeps
166}
167
Colin Cross4d9c2d12016-07-29 12:48:20 -0700168type exportedFlagsProducer interface {
169 exportedFlags() []string
Dan Willemsen847dcc72016-09-29 12:13:36 -0700170 exportedFlagsDeps() android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700171}
172
173var _ exportedFlagsProducer = (*flagExporter)(nil)
174
Colin Crossb916a382016-07-29 17:28:03 -0700175// libraryDecorator wraps baseCompiler, baseLinker and baseInstaller to provide library-specific
176// functionality: static vs. shared linkage, reusing object files for shared libraries
177type libraryDecorator struct {
178 Properties LibraryProperties
Colin Cross4d9c2d12016-07-29 12:48:20 -0700179
180 // For reusing static library objects for shared library
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700181 reuseObjects Objects
Colin Cross26c34ed2016-09-30 17:10:16 -0700182 // table-of-contents file to optimize out relinking when possible
183 tocFile android.OptionalPath
Colin Cross4d9c2d12016-07-29 12:48:20 -0700184
Colin Cross4d9c2d12016-07-29 12:48:20 -0700185 flagExporter
186 stripper
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700187 relocationPacker
Colin Cross4d9c2d12016-07-29 12:48:20 -0700188
Colin Cross4d9c2d12016-07-29 12:48:20 -0700189 // If we're used as a whole_static_lib, our missing dependencies need
190 // to be given
191 wholeStaticMissingDeps []string
192
193 // For whole_static_libs
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700194 objects Objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700195
196 // Uses the module's name if empty, but can be overridden. Does not include
197 // shlib suffix.
198 libName string
Colin Crossb916a382016-07-29 17:28:03 -0700199
200 sanitize *sanitize
201
Dan Willemsen581341d2017-02-09 16:16:31 -0800202 // Output archive of gcno coverage information files
203 coverageOutputFile android.OptionalPath
204
Colin Crossb916a382016-07-29 17:28:03 -0700205 // Decorated interafaces
206 *baseCompiler
207 *baseLinker
208 *baseInstaller
Colin Cross4d9c2d12016-07-29 12:48:20 -0700209}
210
Colin Crossb916a382016-07-29 17:28:03 -0700211func (library *libraryDecorator) linkerProps() []interface{} {
212 var props []interface{}
213 props = append(props, library.baseLinker.linkerProps()...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700214 return append(props,
215 &library.Properties,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700216 &library.flagExporter.Properties,
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700217 &library.stripper.StripProperties,
218 &library.relocationPacker.Properties)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700219}
220
Colin Crossb916a382016-07-29 17:28:03 -0700221func (library *libraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross42742b82016-08-01 13:20:05 -0700222 flags = library.baseLinker.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700223
Colin Crossb916a382016-07-29 17:28:03 -0700224 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
225 // all code is position independent, and then those warnings get promoted to
226 // errors.
227 if ctx.Os() != android.Windows {
228 flags.CFlags = append(flags.CFlags, "-fPIC")
229 }
230
231 if library.static() {
232 flags.CFlags = append(flags.CFlags, library.Properties.Static.Cflags...)
233 } else {
234 flags.CFlags = append(flags.CFlags, library.Properties.Shared.Cflags...)
235 }
236
Colin Cross4d9c2d12016-07-29 12:48:20 -0700237 if !library.static() {
238 libName := library.getLibName(ctx)
239 // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead
240 sharedFlag := "-Wl,-shared"
241 if flags.Clang || ctx.Host() {
242 sharedFlag = "-shared"
243 }
244 var f []string
245 if ctx.Device() {
246 f = append(f,
247 "-nostdlib",
248 "-Wl,--gc-sections",
249 )
250 }
251
252 if ctx.Darwin() {
253 f = append(f,
254 "-dynamiclib",
255 "-single_module",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700256 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
257 )
Colin Cross7863cf52016-10-20 10:47:21 -0700258 if ctx.Arch().ArchType == android.X86 {
259 f = append(f,
260 "-read_only_relocs suppress",
261 )
262 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700263 } else {
264 f = append(f,
265 sharedFlag,
266 "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix())
267 }
268
269 flags.LdFlags = append(f, flags.LdFlags...)
270 }
271
272 return flags
273}
274
Dan Willemsen273af7f2016-11-03 15:53:42 -0700275func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
276 exportIncludeDirs := android.PathsForModuleSrc(ctx, library.flagExporter.Properties.Export_include_dirs)
277 if len(exportIncludeDirs) > 0 {
278 flags.GlobalFlags = append(flags.GlobalFlags, includeDirsToFlags(exportIncludeDirs))
279 }
280
281 return library.baseCompiler.compilerFlags(ctx, flags)
282}
283
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700284func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Colin Cross5950f382016-12-13 12:50:57 -0800285 if !library.buildShared() && !library.buildStatic() {
286 if len(library.baseCompiler.Properties.Srcs) > 0 {
287 ctx.PropertyErrorf("srcs", "cc_library_headers must not have any srcs")
288 }
289 if len(library.Properties.Static.Srcs) > 0 {
290 ctx.PropertyErrorf("static.srcs", "cc_library_headers must not have any srcs")
291 }
292 if len(library.Properties.Shared.Srcs) > 0 {
293 ctx.PropertyErrorf("shared.srcs", "cc_library_headers must not have any srcs")
294 }
295 return Objects{}
296 }
297
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700298 objs := library.baseCompiler.compile(ctx, flags, deps)
299 library.reuseObjects = objs
Colin Cross2f336352016-10-26 10:03:47 -0700300 buildFlags := flagsToBuilderFlags(flags)
Colin Crossb916a382016-07-29 17:28:03 -0700301
Colin Cross4d9c2d12016-07-29 12:48:20 -0700302 if library.static() {
Colin Cross2f336352016-10-26 10:03:47 -0700303 srcs := android.PathsForModuleSrc(ctx, library.Properties.Static.Srcs)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700304 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceStaticLibrary,
305 srcs, library.baseCompiler.deps))
Colin Crossb916a382016-07-29 17:28:03 -0700306 } else {
Colin Cross2f336352016-10-26 10:03:47 -0700307 srcs := android.PathsForModuleSrc(ctx, library.Properties.Shared.Srcs)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700308 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceSharedLibrary,
309 srcs, library.baseCompiler.deps))
Colin Crossb916a382016-07-29 17:28:03 -0700310 }
311
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700312 return objs
Colin Crossb916a382016-07-29 17:28:03 -0700313}
314
315type libraryInterface interface {
316 getWholeStaticMissingDeps() []string
317 static() bool
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700318 objs() Objects
319 reuseObjs() Objects
Colin Cross26c34ed2016-09-30 17:10:16 -0700320 toc() android.OptionalPath
Colin Crossb916a382016-07-29 17:28:03 -0700321
322 // Returns true if the build options for the module have selected a static or shared build
323 buildStatic() bool
324 buildShared() bool
325
326 // Sets whether a specific variant is static or shared
327 setStatic(bool)
328}
329
330func (library *libraryDecorator) getLibName(ctx ModuleContext) string {
331 name := library.libName
332 if name == "" {
Colin Crossce75d2c2016-10-06 16:12:58 -0700333 name = ctx.baseModuleName()
Colin Crossb916a382016-07-29 17:28:03 -0700334 }
335
336 if ctx.Host() && Bool(library.Properties.Unique_host_soname) {
337 if !strings.HasSuffix(name, "-host") {
338 name = name + "-host"
339 }
340 }
341
342 return name + library.Properties.VariantName
343}
344
345func (library *libraryDecorator) linkerInit(ctx BaseModuleContext) {
346 location := InstallInSystem
347 if library.sanitize.inData() {
348 location = InstallInData
349 }
350 library.baseInstaller.location = location
351
352 library.baseLinker.linkerInit(ctx)
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700353
354 library.relocationPacker.packingInit(ctx)
Colin Crossb916a382016-07-29 17:28:03 -0700355}
356
Colin Cross37047f12016-12-13 17:06:13 -0800357func (library *libraryDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700358 deps = library.baseLinker.linkerDeps(ctx, deps)
359
360 if library.static() {
361 deps.WholeStaticLibs = append(deps.WholeStaticLibs,
362 library.Properties.Static.Whole_static_libs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700363 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Static.Static_libs...)
364 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Static.Shared_libs...)
365 } else {
Dan Willemsen2e47b342016-11-17 01:02:25 -0800366 if ctx.toolchain().Bionic() && !Bool(library.baseLinker.Properties.Nocrt) {
Dan Willemsend2ede872016-11-18 14:54:24 -0800367 if !ctx.sdk() && !ctx.vndk() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700368 deps.CrtBegin = "crtbegin_so"
369 deps.CrtEnd = "crtend_so"
370 } else {
Dan Albertebedf672016-11-08 15:06:22 -0800371 // TODO(danalbert): Add generation of crt objects.
372 // For `sdk_version: "current"`, we don't actually have a
373 // freshly generated set of CRT objects. Use the last stable
374 // version.
375 version := ctx.sdkVersion()
376 if version == "current" {
377 version = ctx.AConfig().PlatformSdkVersion()
378 }
379 deps.CrtBegin = "ndk_crtbegin_so." + version
380 deps.CrtEnd = "ndk_crtend_so." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700381 }
382 }
383 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Shared.Whole_static_libs...)
384 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Shared.Static_libs...)
385 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Shared.Shared_libs...)
386 }
387
388 return deps
389}
390
Colin Crossb916a382016-07-29 17:28:03 -0700391func (library *libraryDecorator) linkStatic(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700392 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700393
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700394 library.objects = deps.WholeStaticLibObjs.Copy()
395 library.objects = library.objects.Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700396
397 outputFile := android.PathForModuleOut(ctx,
398 ctx.ModuleName()+library.Properties.VariantName+staticLibraryExtension)
Dan Willemsen581341d2017-02-09 16:16:31 -0800399 builderFlags := flagsToBuilderFlags(flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700400
Dan Willemsen581341d2017-02-09 16:16:31 -0800401 TransformObjToStaticLib(ctx, library.objects.objFiles, builderFlags, outputFile, objs.tidyFiles)
402
403 library.coverageOutputFile = TransformCoverageFilesToLib(ctx, library.objects, builderFlags,
404 ctx.ModuleName()+library.Properties.VariantName)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700405
406 library.wholeStaticMissingDeps = ctx.GetMissingDependencies()
407
408 ctx.CheckbuildFile(outputFile)
409
410 return outputFile
411}
412
Colin Crossb916a382016-07-29 17:28:03 -0700413func (library *libraryDecorator) linkShared(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700414 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700415
416 var linkerDeps android.Paths
417
418 versionScript := android.OptionalPathForModuleSrc(ctx, library.Properties.Version_script)
419 unexportedSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Unexported_symbols_list)
420 forceNotWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_not_weak_list)
421 forceWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_weak_list)
422 if !ctx.Darwin() {
423 if versionScript.Valid() {
424 flags.LdFlags = append(flags.LdFlags, "-Wl,--version-script,"+versionScript.String())
425 linkerDeps = append(linkerDeps, versionScript.Path())
426 }
427 if unexportedSymbols.Valid() {
428 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
429 }
430 if forceNotWeakSymbols.Valid() {
431 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
432 }
433 if forceWeakSymbols.Valid() {
434 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
435 }
436 } else {
437 if versionScript.Valid() {
438 ctx.PropertyErrorf("version_script", "Not supported on Darwin")
439 }
440 if unexportedSymbols.Valid() {
441 flags.LdFlags = append(flags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
442 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
443 }
444 if forceNotWeakSymbols.Valid() {
445 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
446 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
447 }
448 if forceWeakSymbols.Valid() {
449 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
450 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
451 }
452 }
453
454 fileName := library.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
455 outputFile := android.PathForModuleOut(ctx, fileName)
456 ret := outputFile
457
458 builderFlags := flagsToBuilderFlags(flags)
459
Colin Cross89562dc2016-10-03 17:47:19 -0700460 if !ctx.Darwin() {
461 // Optimize out relinking against shared libraries whose interface hasn't changed by
462 // depending on a table of contents file instead of the library itself.
463 tocPath := outputFile.RelPathString()
464 tocPath = pathtools.ReplaceExtension(tocPath, flags.Toolchain.ShlibSuffix()[1:]+".toc")
465 tocFile := android.PathForOutput(ctx, tocPath)
466 library.tocFile = android.OptionalPathForPath(tocFile)
467 TransformSharedObjectToToc(ctx, outputFile, tocFile, builderFlags)
468 }
469
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700470 if library.relocationPacker.needsPacking(ctx) {
471 packedOutputFile := outputFile
472 outputFile = android.PathForModuleOut(ctx, "unpacked", fileName)
473 library.relocationPacker.pack(ctx, outputFile, packedOutputFile, builderFlags)
474 }
475
Colin Cross4d9c2d12016-07-29 12:48:20 -0700476 if library.stripper.needsStrip(ctx) {
477 strippedOutputFile := outputFile
478 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
479 library.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
480 }
481
482 sharedLibs := deps.SharedLibs
483 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
484
Dan Albertd015c4a2016-08-10 14:34:08 -0700485 // TODO(danalbert): Clean this up when soong supports prebuilts.
486 if strings.HasPrefix(ctx.selectedStl(), "ndk_libc++") {
487 libDir := getNdkStlLibDir(ctx, flags.Toolchain, "libc++")
488
489 if strings.HasSuffix(ctx.selectedStl(), "_shared") {
490 deps.StaticLibs = append(deps.StaticLibs,
491 libDir.Join(ctx, "libandroid_support.a"))
492 } else {
493 deps.StaticLibs = append(deps.StaticLibs,
494 libDir.Join(ctx, "libc++abi.a"),
495 libDir.Join(ctx, "libandroid_support.a"))
496 }
497
498 if ctx.Arch().ArchType == android.Arm {
499 deps.StaticLibs = append(deps.StaticLibs,
500 libDir.Join(ctx, "libunwind.a"))
501 }
502 }
503
Colin Cross26c34ed2016-09-30 17:10:16 -0700504 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
505 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700506 linkerDeps = append(linkerDeps, objs.tidyFiles...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700507
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700508 TransformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700509 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
510 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile)
511
Dan Willemsen581341d2017-02-09 16:16:31 -0800512 objs.coverageFiles = append(objs.coverageFiles, deps.StaticLibObjs.coverageFiles...)
513 objs.coverageFiles = append(objs.coverageFiles, deps.WholeStaticLibObjs.coverageFiles...)
514 library.coverageOutputFile = TransformCoverageFilesToLib(ctx, objs, builderFlags, library.getLibName(ctx))
515
Colin Cross4d9c2d12016-07-29 12:48:20 -0700516 return ret
517}
518
Colin Crossb916a382016-07-29 17:28:03 -0700519func (library *libraryDecorator) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700520 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700521
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700522 objs = objs.Append(deps.Objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700523
524 var out android.Path
525 if library.static() {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700526 out = library.linkStatic(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700527 } else {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700528 out = library.linkShared(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700529 }
530
531 library.exportIncludes(ctx, "-I")
532 library.reexportFlags(deps.ReexportedFlags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700533 library.reexportDeps(deps.ReexportedFlagsDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700534
Dan Willemsene1240db2016-11-03 14:28:51 -0700535 if library.Properties.Aidl.Export_aidl_headers {
536 if library.baseCompiler.hasSrcExt(".aidl") {
537 library.reexportFlags([]string{
538 "-I" + android.PathForModuleGen(ctx, "aidl").String(),
539 })
540 library.reexportDeps(library.baseCompiler.deps) // TODO: restrict to aidl deps
541 }
542 }
543
544 if library.Properties.Proto.Export_proto_headers {
545 if library.baseCompiler.hasSrcExt(".proto") {
Colin Cross0c461f12016-10-20 16:11:43 -0700546 library.reexportFlags([]string{
547 "-I" + protoSubDir(ctx).String(),
548 "-I" + protoDir(ctx).String(),
549 })
550 library.reexportDeps(library.baseCompiler.deps) // TODO: restrict to proto deps
551 }
552 }
553
Colin Cross4d9c2d12016-07-29 12:48:20 -0700554 return out
555}
556
Colin Crossb916a382016-07-29 17:28:03 -0700557func (library *libraryDecorator) buildStatic() bool {
558 return library.Properties.BuildStatic &&
Colin Cross4d9c2d12016-07-29 12:48:20 -0700559 (library.Properties.Static.Enabled == nil || *library.Properties.Static.Enabled)
560}
561
Colin Crossb916a382016-07-29 17:28:03 -0700562func (library *libraryDecorator) buildShared() bool {
563 return library.Properties.BuildShared &&
Colin Cross4d9c2d12016-07-29 12:48:20 -0700564 (library.Properties.Shared.Enabled == nil || *library.Properties.Shared.Enabled)
565}
566
Colin Crossb916a382016-07-29 17:28:03 -0700567func (library *libraryDecorator) getWholeStaticMissingDeps() []string {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700568 return library.wholeStaticMissingDeps
569}
570
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700571func (library *libraryDecorator) objs() Objects {
572 return library.objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700573}
574
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700575func (library *libraryDecorator) reuseObjs() Objects {
576 return library.reuseObjects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700577}
578
Colin Cross26c34ed2016-09-30 17:10:16 -0700579func (library *libraryDecorator) toc() android.OptionalPath {
580 return library.tocFile
581}
582
Colin Crossb916a382016-07-29 17:28:03 -0700583func (library *libraryDecorator) install(ctx ModuleContext, file android.Path) {
584 if !ctx.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700585 library.baseInstaller.install(ctx, file)
586 }
587}
588
Colin Crossb916a382016-07-29 17:28:03 -0700589func (library *libraryDecorator) static() bool {
590 return library.Properties.VariantIsStatic
Colin Cross4d9c2d12016-07-29 12:48:20 -0700591}
592
Colin Crossb916a382016-07-29 17:28:03 -0700593func (library *libraryDecorator) setStatic(static bool) {
594 library.Properties.VariantIsStatic = static
595}
596
Colin Crossab3b7322016-12-09 14:46:15 -0800597func (library *libraryDecorator) BuildOnlyStatic() {
598 library.Properties.BuildShared = false
599}
600
601func (library *libraryDecorator) BuildOnlyShared() {
602 library.Properties.BuildStatic = false
603}
604
Colin Cross5950f382016-12-13 12:50:57 -0800605func (library *libraryDecorator) HeaderOnly() {
606 library.Properties.BuildShared = false
607 library.Properties.BuildStatic = false
608}
609
Colin Crossab3b7322016-12-09 14:46:15 -0800610func NewLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700611 module := newModule(hod, android.MultilibBoth)
612
Colin Crossb916a382016-07-29 17:28:03 -0700613 library := &libraryDecorator{
614 Properties: LibraryProperties{
Colin Crossab3b7322016-12-09 14:46:15 -0800615 BuildShared: true,
616 BuildStatic: true,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700617 },
Colin Crossb916a382016-07-29 17:28:03 -0700618 baseCompiler: NewBaseCompiler(),
619 baseLinker: NewBaseLinker(),
620 baseInstaller: NewBaseInstaller("lib", "lib64", InstallInSystem),
621 sanitize: module.sanitize,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700622 }
623
Colin Crossb916a382016-07-29 17:28:03 -0700624 module.compiler = library
625 module.linker = library
626 module.installer = library
627
628 return module, library
629}
630
631func linkageMutator(mctx android.BottomUpMutatorContext) {
632 if m, ok := mctx.Module().(*Module); ok && m.linker != nil {
633 if library, ok := m.linker.(libraryInterface); ok {
634 var modules []blueprint.Module
635 if library.buildStatic() && library.buildShared() {
636 modules = mctx.CreateLocalVariations("static", "shared")
637 static := modules[0].(*Module)
638 shared := modules[1].(*Module)
639
640 static.linker.(libraryInterface).setStatic(true)
641 shared.linker.(libraryInterface).setStatic(false)
642
643 if staticCompiler, ok := static.compiler.(*libraryDecorator); ok {
644 sharedCompiler := shared.compiler.(*libraryDecorator)
645 if len(staticCompiler.Properties.Static.Cflags) == 0 &&
646 len(sharedCompiler.Properties.Shared.Cflags) == 0 {
647 // Optimize out compiling common .o files twice for static+shared libraries
648 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
649 sharedCompiler.baseCompiler.Properties.Srcs = nil
650 sharedCompiler.baseCompiler.Properties.Generated_sources = nil
651 }
652 }
653 } else if library.buildStatic() {
654 modules = mctx.CreateLocalVariations("static")
655 modules[0].(*Module).linker.(libraryInterface).setStatic(true)
656 } else if library.buildShared() {
657 modules = mctx.CreateLocalVariations("shared")
658 modules[0].(*Module).linker.(libraryInterface).setStatic(false)
659 }
660 }
661 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700662}