blob: 666c47ab24d65cf65b667105c6c0b6fe6359e898 [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
202 // Decorated interafaces
203 *baseCompiler
204 *baseLinker
205 *baseInstaller
Colin Cross4d9c2d12016-07-29 12:48:20 -0700206}
207
Colin Crossb916a382016-07-29 17:28:03 -0700208func (library *libraryDecorator) linkerProps() []interface{} {
209 var props []interface{}
210 props = append(props, library.baseLinker.linkerProps()...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700211 return append(props,
212 &library.Properties,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700213 &library.flagExporter.Properties,
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700214 &library.stripper.StripProperties,
215 &library.relocationPacker.Properties)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700216}
217
Colin Crossb916a382016-07-29 17:28:03 -0700218func (library *libraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross42742b82016-08-01 13:20:05 -0700219 flags = library.baseLinker.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700220
Colin Crossb916a382016-07-29 17:28:03 -0700221 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
222 // all code is position independent, and then those warnings get promoted to
223 // errors.
224 if ctx.Os() != android.Windows {
225 flags.CFlags = append(flags.CFlags, "-fPIC")
226 }
227
228 if library.static() {
229 flags.CFlags = append(flags.CFlags, library.Properties.Static.Cflags...)
230 } else {
231 flags.CFlags = append(flags.CFlags, library.Properties.Shared.Cflags...)
232 }
233
Colin Cross4d9c2d12016-07-29 12:48:20 -0700234 if !library.static() {
235 libName := library.getLibName(ctx)
236 // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead
237 sharedFlag := "-Wl,-shared"
238 if flags.Clang || ctx.Host() {
239 sharedFlag = "-shared"
240 }
241 var f []string
242 if ctx.Device() {
243 f = append(f,
244 "-nostdlib",
245 "-Wl,--gc-sections",
246 )
247 }
248
249 if ctx.Darwin() {
250 f = append(f,
251 "-dynamiclib",
252 "-single_module",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700253 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
254 )
Colin Cross7863cf52016-10-20 10:47:21 -0700255 if ctx.Arch().ArchType == android.X86 {
256 f = append(f,
257 "-read_only_relocs suppress",
258 )
259 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700260 } else {
261 f = append(f,
262 sharedFlag,
263 "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix())
264 }
265
266 flags.LdFlags = append(f, flags.LdFlags...)
267 }
268
269 return flags
270}
271
Dan Willemsen273af7f2016-11-03 15:53:42 -0700272func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
273 exportIncludeDirs := android.PathsForModuleSrc(ctx, library.flagExporter.Properties.Export_include_dirs)
274 if len(exportIncludeDirs) > 0 {
275 flags.GlobalFlags = append(flags.GlobalFlags, includeDirsToFlags(exportIncludeDirs))
276 }
277
278 return library.baseCompiler.compilerFlags(ctx, flags)
279}
280
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700281func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Colin Cross5950f382016-12-13 12:50:57 -0800282 if !library.buildShared() && !library.buildStatic() {
283 if len(library.baseCompiler.Properties.Srcs) > 0 {
284 ctx.PropertyErrorf("srcs", "cc_library_headers must not have any srcs")
285 }
286 if len(library.Properties.Static.Srcs) > 0 {
287 ctx.PropertyErrorf("static.srcs", "cc_library_headers must not have any srcs")
288 }
289 if len(library.Properties.Shared.Srcs) > 0 {
290 ctx.PropertyErrorf("shared.srcs", "cc_library_headers must not have any srcs")
291 }
292 return Objects{}
293 }
294
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700295 objs := library.baseCompiler.compile(ctx, flags, deps)
296 library.reuseObjects = objs
Colin Cross2f336352016-10-26 10:03:47 -0700297 buildFlags := flagsToBuilderFlags(flags)
Colin Crossb916a382016-07-29 17:28:03 -0700298
Colin Cross4d9c2d12016-07-29 12:48:20 -0700299 if library.static() {
Colin Cross2f336352016-10-26 10:03:47 -0700300 srcs := android.PathsForModuleSrc(ctx, library.Properties.Static.Srcs)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700301 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceStaticLibrary,
302 srcs, library.baseCompiler.deps))
Colin Crossb916a382016-07-29 17:28:03 -0700303 } else {
Colin Cross2f336352016-10-26 10:03:47 -0700304 srcs := android.PathsForModuleSrc(ctx, library.Properties.Shared.Srcs)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700305 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceSharedLibrary,
306 srcs, library.baseCompiler.deps))
Colin Crossb916a382016-07-29 17:28:03 -0700307 }
308
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700309 return objs
Colin Crossb916a382016-07-29 17:28:03 -0700310}
311
312type libraryInterface interface {
313 getWholeStaticMissingDeps() []string
314 static() bool
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700315 objs() Objects
316 reuseObjs() Objects
Colin Cross26c34ed2016-09-30 17:10:16 -0700317 toc() android.OptionalPath
Colin Crossb916a382016-07-29 17:28:03 -0700318
319 // Returns true if the build options for the module have selected a static or shared build
320 buildStatic() bool
321 buildShared() bool
322
323 // Sets whether a specific variant is static or shared
324 setStatic(bool)
325}
326
327func (library *libraryDecorator) getLibName(ctx ModuleContext) string {
328 name := library.libName
329 if name == "" {
Colin Crossce75d2c2016-10-06 16:12:58 -0700330 name = ctx.baseModuleName()
Colin Crossb916a382016-07-29 17:28:03 -0700331 }
332
333 if ctx.Host() && Bool(library.Properties.Unique_host_soname) {
334 if !strings.HasSuffix(name, "-host") {
335 name = name + "-host"
336 }
337 }
338
339 return name + library.Properties.VariantName
340}
341
342func (library *libraryDecorator) linkerInit(ctx BaseModuleContext) {
343 location := InstallInSystem
344 if library.sanitize.inData() {
345 location = InstallInData
346 }
347 library.baseInstaller.location = location
348
349 library.baseLinker.linkerInit(ctx)
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700350
351 library.relocationPacker.packingInit(ctx)
Colin Crossb916a382016-07-29 17:28:03 -0700352}
353
354func (library *libraryDecorator) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
355 deps = library.baseLinker.linkerDeps(ctx, deps)
356
357 if library.static() {
358 deps.WholeStaticLibs = append(deps.WholeStaticLibs,
359 library.Properties.Static.Whole_static_libs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700360 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Static.Static_libs...)
361 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Static.Shared_libs...)
362 } else {
Dan Willemsen2e47b342016-11-17 01:02:25 -0800363 if ctx.toolchain().Bionic() && !Bool(library.baseLinker.Properties.Nocrt) {
Dan Willemsend2ede872016-11-18 14:54:24 -0800364 if !ctx.sdk() && !ctx.vndk() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700365 deps.CrtBegin = "crtbegin_so"
366 deps.CrtEnd = "crtend_so"
367 } else {
Dan Albertebedf672016-11-08 15:06:22 -0800368 // TODO(danalbert): Add generation of crt objects.
369 // For `sdk_version: "current"`, we don't actually have a
370 // freshly generated set of CRT objects. Use the last stable
371 // version.
372 version := ctx.sdkVersion()
373 if version == "current" {
374 version = ctx.AConfig().PlatformSdkVersion()
375 }
376 deps.CrtBegin = "ndk_crtbegin_so." + version
377 deps.CrtEnd = "ndk_crtend_so." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700378 }
379 }
380 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Shared.Whole_static_libs...)
381 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Shared.Static_libs...)
382 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Shared.Shared_libs...)
383 }
384
385 return deps
386}
387
Colin Crossb916a382016-07-29 17:28:03 -0700388func (library *libraryDecorator) linkStatic(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700389 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700390
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700391 library.objects = deps.WholeStaticLibObjs.Copy()
392 library.objects = library.objects.Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700393
394 outputFile := android.PathForModuleOut(ctx,
395 ctx.ModuleName()+library.Properties.VariantName+staticLibraryExtension)
396
397 if ctx.Darwin() {
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700398 TransformDarwinObjToStaticLib(ctx, library.objects.objFiles, flagsToBuilderFlags(flags), outputFile, objs.tidyFiles)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700399 } else {
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700400 TransformObjToStaticLib(ctx, library.objects.objFiles, flagsToBuilderFlags(flags), outputFile, objs.tidyFiles)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700401 }
402
403 library.wholeStaticMissingDeps = ctx.GetMissingDependencies()
404
405 ctx.CheckbuildFile(outputFile)
406
407 return outputFile
408}
409
Colin Crossb916a382016-07-29 17:28:03 -0700410func (library *libraryDecorator) linkShared(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700411 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700412
413 var linkerDeps android.Paths
414
415 versionScript := android.OptionalPathForModuleSrc(ctx, library.Properties.Version_script)
416 unexportedSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Unexported_symbols_list)
417 forceNotWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_not_weak_list)
418 forceWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_weak_list)
419 if !ctx.Darwin() {
420 if versionScript.Valid() {
421 flags.LdFlags = append(flags.LdFlags, "-Wl,--version-script,"+versionScript.String())
422 linkerDeps = append(linkerDeps, versionScript.Path())
423 }
424 if unexportedSymbols.Valid() {
425 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
426 }
427 if forceNotWeakSymbols.Valid() {
428 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
429 }
430 if forceWeakSymbols.Valid() {
431 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
432 }
433 } else {
434 if versionScript.Valid() {
435 ctx.PropertyErrorf("version_script", "Not supported on Darwin")
436 }
437 if unexportedSymbols.Valid() {
438 flags.LdFlags = append(flags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
439 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
440 }
441 if forceNotWeakSymbols.Valid() {
442 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
443 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
444 }
445 if forceWeakSymbols.Valid() {
446 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
447 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
448 }
449 }
450
451 fileName := library.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
452 outputFile := android.PathForModuleOut(ctx, fileName)
453 ret := outputFile
454
455 builderFlags := flagsToBuilderFlags(flags)
456
Colin Cross89562dc2016-10-03 17:47:19 -0700457 if !ctx.Darwin() {
458 // Optimize out relinking against shared libraries whose interface hasn't changed by
459 // depending on a table of contents file instead of the library itself.
460 tocPath := outputFile.RelPathString()
461 tocPath = pathtools.ReplaceExtension(tocPath, flags.Toolchain.ShlibSuffix()[1:]+".toc")
462 tocFile := android.PathForOutput(ctx, tocPath)
463 library.tocFile = android.OptionalPathForPath(tocFile)
464 TransformSharedObjectToToc(ctx, outputFile, tocFile, builderFlags)
465 }
466
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700467 if library.relocationPacker.needsPacking(ctx) {
468 packedOutputFile := outputFile
469 outputFile = android.PathForModuleOut(ctx, "unpacked", fileName)
470 library.relocationPacker.pack(ctx, outputFile, packedOutputFile, builderFlags)
471 }
472
Colin Cross4d9c2d12016-07-29 12:48:20 -0700473 if library.stripper.needsStrip(ctx) {
474 strippedOutputFile := outputFile
475 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
476 library.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
477 }
478
479 sharedLibs := deps.SharedLibs
480 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
481
Dan Albertd015c4a2016-08-10 14:34:08 -0700482 // TODO(danalbert): Clean this up when soong supports prebuilts.
483 if strings.HasPrefix(ctx.selectedStl(), "ndk_libc++") {
484 libDir := getNdkStlLibDir(ctx, flags.Toolchain, "libc++")
485
486 if strings.HasSuffix(ctx.selectedStl(), "_shared") {
487 deps.StaticLibs = append(deps.StaticLibs,
488 libDir.Join(ctx, "libandroid_support.a"))
489 } else {
490 deps.StaticLibs = append(deps.StaticLibs,
491 libDir.Join(ctx, "libc++abi.a"),
492 libDir.Join(ctx, "libandroid_support.a"))
493 }
494
495 if ctx.Arch().ArchType == android.Arm {
496 deps.StaticLibs = append(deps.StaticLibs,
497 libDir.Join(ctx, "libunwind.a"))
498 }
499 }
500
Colin Cross26c34ed2016-09-30 17:10:16 -0700501 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
502 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700503 linkerDeps = append(linkerDeps, objs.tidyFiles...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700504
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700505 TransformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700506 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
507 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile)
508
509 return ret
510}
511
Colin Crossb916a382016-07-29 17:28:03 -0700512func (library *libraryDecorator) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700513 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700514
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700515 objs = objs.Append(deps.Objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700516
517 var out android.Path
518 if library.static() {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700519 out = library.linkStatic(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700520 } else {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700521 out = library.linkShared(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700522 }
523
524 library.exportIncludes(ctx, "-I")
525 library.reexportFlags(deps.ReexportedFlags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700526 library.reexportDeps(deps.ReexportedFlagsDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700527
Dan Willemsene1240db2016-11-03 14:28:51 -0700528 if library.Properties.Aidl.Export_aidl_headers {
529 if library.baseCompiler.hasSrcExt(".aidl") {
530 library.reexportFlags([]string{
531 "-I" + android.PathForModuleGen(ctx, "aidl").String(),
532 })
533 library.reexportDeps(library.baseCompiler.deps) // TODO: restrict to aidl deps
534 }
535 }
536
537 if library.Properties.Proto.Export_proto_headers {
538 if library.baseCompiler.hasSrcExt(".proto") {
Colin Cross0c461f12016-10-20 16:11:43 -0700539 library.reexportFlags([]string{
540 "-I" + protoSubDir(ctx).String(),
541 "-I" + protoDir(ctx).String(),
542 })
543 library.reexportDeps(library.baseCompiler.deps) // TODO: restrict to proto deps
544 }
545 }
546
Colin Cross4d9c2d12016-07-29 12:48:20 -0700547 return out
548}
549
Colin Crossb916a382016-07-29 17:28:03 -0700550func (library *libraryDecorator) buildStatic() bool {
551 return library.Properties.BuildStatic &&
Colin Cross4d9c2d12016-07-29 12:48:20 -0700552 (library.Properties.Static.Enabled == nil || *library.Properties.Static.Enabled)
553}
554
Colin Crossb916a382016-07-29 17:28:03 -0700555func (library *libraryDecorator) buildShared() bool {
556 return library.Properties.BuildShared &&
Colin Cross4d9c2d12016-07-29 12:48:20 -0700557 (library.Properties.Shared.Enabled == nil || *library.Properties.Shared.Enabled)
558}
559
Colin Crossb916a382016-07-29 17:28:03 -0700560func (library *libraryDecorator) getWholeStaticMissingDeps() []string {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700561 return library.wholeStaticMissingDeps
562}
563
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700564func (library *libraryDecorator) objs() Objects {
565 return library.objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700566}
567
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700568func (library *libraryDecorator) reuseObjs() Objects {
569 return library.reuseObjects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700570}
571
Colin Cross26c34ed2016-09-30 17:10:16 -0700572func (library *libraryDecorator) toc() android.OptionalPath {
573 return library.tocFile
574}
575
Colin Crossb916a382016-07-29 17:28:03 -0700576func (library *libraryDecorator) install(ctx ModuleContext, file android.Path) {
577 if !ctx.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700578 library.baseInstaller.install(ctx, file)
579 }
580}
581
Colin Crossb916a382016-07-29 17:28:03 -0700582func (library *libraryDecorator) static() bool {
583 return library.Properties.VariantIsStatic
Colin Cross4d9c2d12016-07-29 12:48:20 -0700584}
585
Colin Crossb916a382016-07-29 17:28:03 -0700586func (library *libraryDecorator) setStatic(static bool) {
587 library.Properties.VariantIsStatic = static
588}
589
Colin Crossab3b7322016-12-09 14:46:15 -0800590func (library *libraryDecorator) BuildOnlyStatic() {
591 library.Properties.BuildShared = false
592}
593
594func (library *libraryDecorator) BuildOnlyShared() {
595 library.Properties.BuildStatic = false
596}
597
Colin Cross5950f382016-12-13 12:50:57 -0800598func (library *libraryDecorator) HeaderOnly() {
599 library.Properties.BuildShared = false
600 library.Properties.BuildStatic = false
601}
602
Colin Crossab3b7322016-12-09 14:46:15 -0800603func NewLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700604 module := newModule(hod, android.MultilibBoth)
605
Colin Crossb916a382016-07-29 17:28:03 -0700606 library := &libraryDecorator{
607 Properties: LibraryProperties{
Colin Crossab3b7322016-12-09 14:46:15 -0800608 BuildShared: true,
609 BuildStatic: true,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700610 },
Colin Crossb916a382016-07-29 17:28:03 -0700611 baseCompiler: NewBaseCompiler(),
612 baseLinker: NewBaseLinker(),
613 baseInstaller: NewBaseInstaller("lib", "lib64", InstallInSystem),
614 sanitize: module.sanitize,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700615 }
616
Colin Crossb916a382016-07-29 17:28:03 -0700617 module.compiler = library
618 module.linker = library
619 module.installer = library
620
621 return module, library
622}
623
624func linkageMutator(mctx android.BottomUpMutatorContext) {
625 if m, ok := mctx.Module().(*Module); ok && m.linker != nil {
626 if library, ok := m.linker.(libraryInterface); ok {
627 var modules []blueprint.Module
628 if library.buildStatic() && library.buildShared() {
629 modules = mctx.CreateLocalVariations("static", "shared")
630 static := modules[0].(*Module)
631 shared := modules[1].(*Module)
632
633 static.linker.(libraryInterface).setStatic(true)
634 shared.linker.(libraryInterface).setStatic(false)
635
636 if staticCompiler, ok := static.compiler.(*libraryDecorator); ok {
637 sharedCompiler := shared.compiler.(*libraryDecorator)
638 if len(staticCompiler.Properties.Static.Cflags) == 0 &&
639 len(sharedCompiler.Properties.Shared.Cflags) == 0 {
640 // Optimize out compiling common .o files twice for static+shared libraries
641 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
642 sharedCompiler.baseCompiler.Properties.Srcs = nil
643 sharedCompiler.baseCompiler.Properties.Generated_sources = nil
644 }
645 }
646 } else if library.buildStatic() {
647 modules = mctx.CreateLocalVariations("static")
648 modules[0].(*Module).linker.(libraryInterface).setStatic(true)
649 } else if library.buildShared() {
650 modules = mctx.CreateLocalVariations("shared")
651 modules[0].(*Module).linker.(libraryInterface).setStatic(false)
652 }
653 }
654 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700655}