blob: 5ba1a977d12c5a20fc0e19b350798112c6ce6aa1 [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 Cross4d9c2d12016-07-29 12:48:20 -070093}
94
95// Module factory for combined static + shared libraries, device by default but with possible host
96// support
97func libraryFactory() (blueprint.Module, []interface{}) {
Colin Crossab3b7322016-12-09 14:46:15 -080098 module, _ := NewLibrary(android.HostAndDeviceSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -070099 return module.Init()
100}
101
102// Module factory for static libraries
103func libraryStaticFactory() (blueprint.Module, []interface{}) {
Colin Crossab3b7322016-12-09 14:46:15 -0800104 module, library := NewLibrary(android.HostAndDeviceSupported)
105 library.BuildOnlyStatic()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700106 return module.Init()
107}
108
109// Module factory for shared libraries
110func librarySharedFactory() (blueprint.Module, []interface{}) {
Colin Crossab3b7322016-12-09 14:46:15 -0800111 module, library := NewLibrary(android.HostAndDeviceSupported)
112 library.BuildOnlyShared()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700113 return module.Init()
114}
115
116// Module factory for host static libraries
117func libraryHostStaticFactory() (blueprint.Module, []interface{}) {
Colin Crossab3b7322016-12-09 14:46:15 -0800118 module, library := NewLibrary(android.HostSupported)
119 library.BuildOnlyStatic()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700120 return module.Init()
121}
122
123// Module factory for host shared libraries
124func libraryHostSharedFactory() (blueprint.Module, []interface{}) {
Colin Crossab3b7322016-12-09 14:46:15 -0800125 module, library := NewLibrary(android.HostSupported)
126 library.BuildOnlyShared()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700127 return module.Init()
128}
129
130type flagExporter struct {
131 Properties FlagExporterProperties
132
Dan Willemsen847dcc72016-09-29 12:13:36 -0700133 flags []string
134 flagsDeps android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700135}
136
137func (f *flagExporter) exportIncludes(ctx ModuleContext, inc string) {
138 includeDirs := android.PathsForModuleSrc(ctx, f.Properties.Export_include_dirs)
139 for _, dir := range includeDirs.Strings() {
140 f.flags = append(f.flags, inc+dir)
141 }
142}
143
144func (f *flagExporter) reexportFlags(flags []string) {
145 f.flags = append(f.flags, flags...)
146}
147
Dan Willemsen847dcc72016-09-29 12:13:36 -0700148func (f *flagExporter) reexportDeps(deps android.Paths) {
149 f.flagsDeps = append(f.flagsDeps, deps...)
150}
151
Colin Cross4d9c2d12016-07-29 12:48:20 -0700152func (f *flagExporter) exportedFlags() []string {
153 return f.flags
154}
155
Dan Willemsen847dcc72016-09-29 12:13:36 -0700156func (f *flagExporter) exportedFlagsDeps() android.Paths {
157 return f.flagsDeps
158}
159
Colin Cross4d9c2d12016-07-29 12:48:20 -0700160type exportedFlagsProducer interface {
161 exportedFlags() []string
Dan Willemsen847dcc72016-09-29 12:13:36 -0700162 exportedFlagsDeps() android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700163}
164
165var _ exportedFlagsProducer = (*flagExporter)(nil)
166
Colin Crossb916a382016-07-29 17:28:03 -0700167// libraryDecorator wraps baseCompiler, baseLinker and baseInstaller to provide library-specific
168// functionality: static vs. shared linkage, reusing object files for shared libraries
169type libraryDecorator struct {
170 Properties LibraryProperties
Colin Cross4d9c2d12016-07-29 12:48:20 -0700171
172 // For reusing static library objects for shared library
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700173 reuseObjects Objects
Colin Cross26c34ed2016-09-30 17:10:16 -0700174 // table-of-contents file to optimize out relinking when possible
175 tocFile android.OptionalPath
Colin Cross4d9c2d12016-07-29 12:48:20 -0700176
Colin Cross4d9c2d12016-07-29 12:48:20 -0700177 flagExporter
178 stripper
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700179 relocationPacker
Colin Cross4d9c2d12016-07-29 12:48:20 -0700180
Colin Cross4d9c2d12016-07-29 12:48:20 -0700181 // If we're used as a whole_static_lib, our missing dependencies need
182 // to be given
183 wholeStaticMissingDeps []string
184
185 // For whole_static_libs
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700186 objects Objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700187
188 // Uses the module's name if empty, but can be overridden. Does not include
189 // shlib suffix.
190 libName string
Colin Crossb916a382016-07-29 17:28:03 -0700191
192 sanitize *sanitize
193
194 // Decorated interafaces
195 *baseCompiler
196 *baseLinker
197 *baseInstaller
Colin Cross4d9c2d12016-07-29 12:48:20 -0700198}
199
Colin Crossb916a382016-07-29 17:28:03 -0700200func (library *libraryDecorator) linkerProps() []interface{} {
201 var props []interface{}
202 props = append(props, library.baseLinker.linkerProps()...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700203 return append(props,
204 &library.Properties,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700205 &library.flagExporter.Properties,
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700206 &library.stripper.StripProperties,
207 &library.relocationPacker.Properties)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700208}
209
Colin Crossb916a382016-07-29 17:28:03 -0700210func (library *libraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross42742b82016-08-01 13:20:05 -0700211 flags = library.baseLinker.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700212
Colin Crossb916a382016-07-29 17:28:03 -0700213 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
214 // all code is position independent, and then those warnings get promoted to
215 // errors.
216 if ctx.Os() != android.Windows {
217 flags.CFlags = append(flags.CFlags, "-fPIC")
218 }
219
220 if library.static() {
221 flags.CFlags = append(flags.CFlags, library.Properties.Static.Cflags...)
222 } else {
223 flags.CFlags = append(flags.CFlags, library.Properties.Shared.Cflags...)
224 }
225
Colin Cross4d9c2d12016-07-29 12:48:20 -0700226 if !library.static() {
227 libName := library.getLibName(ctx)
228 // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead
229 sharedFlag := "-Wl,-shared"
230 if flags.Clang || ctx.Host() {
231 sharedFlag = "-shared"
232 }
233 var f []string
234 if ctx.Device() {
235 f = append(f,
236 "-nostdlib",
237 "-Wl,--gc-sections",
238 )
239 }
240
241 if ctx.Darwin() {
242 f = append(f,
243 "-dynamiclib",
244 "-single_module",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700245 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
246 )
Colin Cross7863cf52016-10-20 10:47:21 -0700247 if ctx.Arch().ArchType == android.X86 {
248 f = append(f,
249 "-read_only_relocs suppress",
250 )
251 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700252 } else {
253 f = append(f,
254 sharedFlag,
255 "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix())
256 }
257
258 flags.LdFlags = append(f, flags.LdFlags...)
259 }
260
261 return flags
262}
263
Dan Willemsen273af7f2016-11-03 15:53:42 -0700264func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
265 exportIncludeDirs := android.PathsForModuleSrc(ctx, library.flagExporter.Properties.Export_include_dirs)
266 if len(exportIncludeDirs) > 0 {
267 flags.GlobalFlags = append(flags.GlobalFlags, includeDirsToFlags(exportIncludeDirs))
268 }
269
270 return library.baseCompiler.compilerFlags(ctx, flags)
271}
272
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700273func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
274 objs := library.baseCompiler.compile(ctx, flags, deps)
275 library.reuseObjects = objs
Colin Cross2f336352016-10-26 10:03:47 -0700276 buildFlags := flagsToBuilderFlags(flags)
Colin Crossb916a382016-07-29 17:28:03 -0700277
Colin Cross4d9c2d12016-07-29 12:48:20 -0700278 if library.static() {
Colin Cross2f336352016-10-26 10:03:47 -0700279 srcs := android.PathsForModuleSrc(ctx, library.Properties.Static.Srcs)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700280 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceStaticLibrary,
281 srcs, library.baseCompiler.deps))
Colin Crossb916a382016-07-29 17:28:03 -0700282 } else {
Colin Cross2f336352016-10-26 10:03:47 -0700283 srcs := android.PathsForModuleSrc(ctx, library.Properties.Shared.Srcs)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700284 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceSharedLibrary,
285 srcs, library.baseCompiler.deps))
Colin Crossb916a382016-07-29 17:28:03 -0700286 }
287
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700288 return objs
Colin Crossb916a382016-07-29 17:28:03 -0700289}
290
291type libraryInterface interface {
292 getWholeStaticMissingDeps() []string
293 static() bool
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700294 objs() Objects
295 reuseObjs() Objects
Colin Cross26c34ed2016-09-30 17:10:16 -0700296 toc() android.OptionalPath
Colin Crossb916a382016-07-29 17:28:03 -0700297
298 // Returns true if the build options for the module have selected a static or shared build
299 buildStatic() bool
300 buildShared() bool
301
302 // Sets whether a specific variant is static or shared
303 setStatic(bool)
304}
305
306func (library *libraryDecorator) getLibName(ctx ModuleContext) string {
307 name := library.libName
308 if name == "" {
Colin Crossce75d2c2016-10-06 16:12:58 -0700309 name = ctx.baseModuleName()
Colin Crossb916a382016-07-29 17:28:03 -0700310 }
311
312 if ctx.Host() && Bool(library.Properties.Unique_host_soname) {
313 if !strings.HasSuffix(name, "-host") {
314 name = name + "-host"
315 }
316 }
317
318 return name + library.Properties.VariantName
319}
320
321func (library *libraryDecorator) linkerInit(ctx BaseModuleContext) {
322 location := InstallInSystem
323 if library.sanitize.inData() {
324 location = InstallInData
325 }
326 library.baseInstaller.location = location
327
328 library.baseLinker.linkerInit(ctx)
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700329
330 library.relocationPacker.packingInit(ctx)
Colin Crossb916a382016-07-29 17:28:03 -0700331}
332
333func (library *libraryDecorator) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
334 deps = library.baseLinker.linkerDeps(ctx, deps)
335
336 if library.static() {
337 deps.WholeStaticLibs = append(deps.WholeStaticLibs,
338 library.Properties.Static.Whole_static_libs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700339 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Static.Static_libs...)
340 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Static.Shared_libs...)
341 } else {
Dan Willemsen2e47b342016-11-17 01:02:25 -0800342 if ctx.toolchain().Bionic() && !Bool(library.baseLinker.Properties.Nocrt) {
Dan Willemsend2ede872016-11-18 14:54:24 -0800343 if !ctx.sdk() && !ctx.vndk() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700344 deps.CrtBegin = "crtbegin_so"
345 deps.CrtEnd = "crtend_so"
346 } else {
Dan Albertebedf672016-11-08 15:06:22 -0800347 // TODO(danalbert): Add generation of crt objects.
348 // For `sdk_version: "current"`, we don't actually have a
349 // freshly generated set of CRT objects. Use the last stable
350 // version.
351 version := ctx.sdkVersion()
352 if version == "current" {
353 version = ctx.AConfig().PlatformSdkVersion()
354 }
355 deps.CrtBegin = "ndk_crtbegin_so." + version
356 deps.CrtEnd = "ndk_crtend_so." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700357 }
358 }
359 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Shared.Whole_static_libs...)
360 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Shared.Static_libs...)
361 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Shared.Shared_libs...)
362 }
363
364 return deps
365}
366
Colin Crossb916a382016-07-29 17:28:03 -0700367func (library *libraryDecorator) linkStatic(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700368 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700369
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700370 library.objects = deps.WholeStaticLibObjs.Copy()
371 library.objects = library.objects.Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700372
373 outputFile := android.PathForModuleOut(ctx,
374 ctx.ModuleName()+library.Properties.VariantName+staticLibraryExtension)
375
376 if ctx.Darwin() {
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700377 TransformDarwinObjToStaticLib(ctx, library.objects.objFiles, flagsToBuilderFlags(flags), outputFile, objs.tidyFiles)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700378 } else {
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700379 TransformObjToStaticLib(ctx, library.objects.objFiles, flagsToBuilderFlags(flags), outputFile, objs.tidyFiles)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700380 }
381
382 library.wholeStaticMissingDeps = ctx.GetMissingDependencies()
383
384 ctx.CheckbuildFile(outputFile)
385
386 return outputFile
387}
388
Colin Crossb916a382016-07-29 17:28:03 -0700389func (library *libraryDecorator) linkShared(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700390 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700391
392 var linkerDeps android.Paths
393
394 versionScript := android.OptionalPathForModuleSrc(ctx, library.Properties.Version_script)
395 unexportedSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Unexported_symbols_list)
396 forceNotWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_not_weak_list)
397 forceWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_weak_list)
398 if !ctx.Darwin() {
399 if versionScript.Valid() {
400 flags.LdFlags = append(flags.LdFlags, "-Wl,--version-script,"+versionScript.String())
401 linkerDeps = append(linkerDeps, versionScript.Path())
402 }
403 if unexportedSymbols.Valid() {
404 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
405 }
406 if forceNotWeakSymbols.Valid() {
407 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
408 }
409 if forceWeakSymbols.Valid() {
410 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
411 }
412 } else {
413 if versionScript.Valid() {
414 ctx.PropertyErrorf("version_script", "Not supported on Darwin")
415 }
416 if unexportedSymbols.Valid() {
417 flags.LdFlags = append(flags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
418 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
419 }
420 if forceNotWeakSymbols.Valid() {
421 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
422 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
423 }
424 if forceWeakSymbols.Valid() {
425 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
426 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
427 }
428 }
429
430 fileName := library.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
431 outputFile := android.PathForModuleOut(ctx, fileName)
432 ret := outputFile
433
434 builderFlags := flagsToBuilderFlags(flags)
435
Colin Cross89562dc2016-10-03 17:47:19 -0700436 if !ctx.Darwin() {
437 // Optimize out relinking against shared libraries whose interface hasn't changed by
438 // depending on a table of contents file instead of the library itself.
439 tocPath := outputFile.RelPathString()
440 tocPath = pathtools.ReplaceExtension(tocPath, flags.Toolchain.ShlibSuffix()[1:]+".toc")
441 tocFile := android.PathForOutput(ctx, tocPath)
442 library.tocFile = android.OptionalPathForPath(tocFile)
443 TransformSharedObjectToToc(ctx, outputFile, tocFile, builderFlags)
444 }
445
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700446 if library.relocationPacker.needsPacking(ctx) {
447 packedOutputFile := outputFile
448 outputFile = android.PathForModuleOut(ctx, "unpacked", fileName)
449 library.relocationPacker.pack(ctx, outputFile, packedOutputFile, builderFlags)
450 }
451
Colin Cross4d9c2d12016-07-29 12:48:20 -0700452 if library.stripper.needsStrip(ctx) {
453 strippedOutputFile := outputFile
454 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
455 library.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
456 }
457
458 sharedLibs := deps.SharedLibs
459 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
460
Dan Albertd015c4a2016-08-10 14:34:08 -0700461 // TODO(danalbert): Clean this up when soong supports prebuilts.
462 if strings.HasPrefix(ctx.selectedStl(), "ndk_libc++") {
463 libDir := getNdkStlLibDir(ctx, flags.Toolchain, "libc++")
464
465 if strings.HasSuffix(ctx.selectedStl(), "_shared") {
466 deps.StaticLibs = append(deps.StaticLibs,
467 libDir.Join(ctx, "libandroid_support.a"))
468 } else {
469 deps.StaticLibs = append(deps.StaticLibs,
470 libDir.Join(ctx, "libc++abi.a"),
471 libDir.Join(ctx, "libandroid_support.a"))
472 }
473
474 if ctx.Arch().ArchType == android.Arm {
475 deps.StaticLibs = append(deps.StaticLibs,
476 libDir.Join(ctx, "libunwind.a"))
477 }
478 }
479
Colin Cross26c34ed2016-09-30 17:10:16 -0700480 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
481 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700482 linkerDeps = append(linkerDeps, objs.tidyFiles...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700483
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700484 TransformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700485 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
486 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile)
487
488 return ret
489}
490
Colin Crossb916a382016-07-29 17:28:03 -0700491func (library *libraryDecorator) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700492 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700493
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700494 objs = objs.Append(deps.Objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700495
496 var out android.Path
497 if library.static() {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700498 out = library.linkStatic(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700499 } else {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700500 out = library.linkShared(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700501 }
502
503 library.exportIncludes(ctx, "-I")
504 library.reexportFlags(deps.ReexportedFlags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700505 library.reexportDeps(deps.ReexportedFlagsDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700506
Dan Willemsene1240db2016-11-03 14:28:51 -0700507 if library.Properties.Aidl.Export_aidl_headers {
508 if library.baseCompiler.hasSrcExt(".aidl") {
509 library.reexportFlags([]string{
510 "-I" + android.PathForModuleGen(ctx, "aidl").String(),
511 })
512 library.reexportDeps(library.baseCompiler.deps) // TODO: restrict to aidl deps
513 }
514 }
515
516 if library.Properties.Proto.Export_proto_headers {
517 if library.baseCompiler.hasSrcExt(".proto") {
Colin Cross0c461f12016-10-20 16:11:43 -0700518 library.reexportFlags([]string{
519 "-I" + protoSubDir(ctx).String(),
520 "-I" + protoDir(ctx).String(),
521 })
522 library.reexportDeps(library.baseCompiler.deps) // TODO: restrict to proto deps
523 }
524 }
525
Colin Cross4d9c2d12016-07-29 12:48:20 -0700526 return out
527}
528
Colin Crossb916a382016-07-29 17:28:03 -0700529func (library *libraryDecorator) buildStatic() bool {
530 return library.Properties.BuildStatic &&
Colin Cross4d9c2d12016-07-29 12:48:20 -0700531 (library.Properties.Static.Enabled == nil || *library.Properties.Static.Enabled)
532}
533
Colin Crossb916a382016-07-29 17:28:03 -0700534func (library *libraryDecorator) buildShared() bool {
535 return library.Properties.BuildShared &&
Colin Cross4d9c2d12016-07-29 12:48:20 -0700536 (library.Properties.Shared.Enabled == nil || *library.Properties.Shared.Enabled)
537}
538
Colin Crossb916a382016-07-29 17:28:03 -0700539func (library *libraryDecorator) getWholeStaticMissingDeps() []string {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700540 return library.wholeStaticMissingDeps
541}
542
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700543func (library *libraryDecorator) objs() Objects {
544 return library.objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700545}
546
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700547func (library *libraryDecorator) reuseObjs() Objects {
548 return library.reuseObjects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700549}
550
Colin Cross26c34ed2016-09-30 17:10:16 -0700551func (library *libraryDecorator) toc() android.OptionalPath {
552 return library.tocFile
553}
554
Colin Crossb916a382016-07-29 17:28:03 -0700555func (library *libraryDecorator) install(ctx ModuleContext, file android.Path) {
556 if !ctx.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700557 library.baseInstaller.install(ctx, file)
558 }
559}
560
Colin Crossb916a382016-07-29 17:28:03 -0700561func (library *libraryDecorator) static() bool {
562 return library.Properties.VariantIsStatic
Colin Cross4d9c2d12016-07-29 12:48:20 -0700563}
564
Colin Crossb916a382016-07-29 17:28:03 -0700565func (library *libraryDecorator) setStatic(static bool) {
566 library.Properties.VariantIsStatic = static
567}
568
Colin Crossab3b7322016-12-09 14:46:15 -0800569func (library *libraryDecorator) BuildOnlyStatic() {
570 library.Properties.BuildShared = false
571}
572
573func (library *libraryDecorator) BuildOnlyShared() {
574 library.Properties.BuildStatic = false
575}
576
577func NewLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700578 module := newModule(hod, android.MultilibBoth)
579
Colin Crossb916a382016-07-29 17:28:03 -0700580 library := &libraryDecorator{
581 Properties: LibraryProperties{
Colin Crossab3b7322016-12-09 14:46:15 -0800582 BuildShared: true,
583 BuildStatic: true,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700584 },
Colin Crossb916a382016-07-29 17:28:03 -0700585 baseCompiler: NewBaseCompiler(),
586 baseLinker: NewBaseLinker(),
587 baseInstaller: NewBaseInstaller("lib", "lib64", InstallInSystem),
588 sanitize: module.sanitize,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700589 }
590
Colin Crossb916a382016-07-29 17:28:03 -0700591 module.compiler = library
592 module.linker = library
593 module.installer = library
594
595 return module, library
596}
597
598func linkageMutator(mctx android.BottomUpMutatorContext) {
599 if m, ok := mctx.Module().(*Module); ok && m.linker != nil {
600 if library, ok := m.linker.(libraryInterface); ok {
601 var modules []blueprint.Module
602 if library.buildStatic() && library.buildShared() {
603 modules = mctx.CreateLocalVariations("static", "shared")
604 static := modules[0].(*Module)
605 shared := modules[1].(*Module)
606
607 static.linker.(libraryInterface).setStatic(true)
608 shared.linker.(libraryInterface).setStatic(false)
609
610 if staticCompiler, ok := static.compiler.(*libraryDecorator); ok {
611 sharedCompiler := shared.compiler.(*libraryDecorator)
612 if len(staticCompiler.Properties.Static.Cflags) == 0 &&
613 len(sharedCompiler.Properties.Shared.Cflags) == 0 {
614 // Optimize out compiling common .o files twice for static+shared libraries
615 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
616 sharedCompiler.baseCompiler.Properties.Srcs = nil
617 sharedCompiler.baseCompiler.Properties.Generated_sources = nil
618 }
619 }
620 } else if library.buildStatic() {
621 modules = mctx.CreateLocalVariations("static")
622 modules[0].(*Module).linker.(libraryInterface).setStatic(true)
623 } else if library.buildShared() {
624 modules = mctx.CreateLocalVariations("shared")
625 modules[0].(*Module).linker.(libraryInterface).setStatic(false)
626 }
627 }
628 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700629}