blob: bdd8e79f9d96207e92026a6ebc5ffd882ce034ef [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 Crossb916a382016-07-29 17:28:03 -070098 module, _ := NewLibrary(android.HostAndDeviceSupported, true, true)
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 Crossb916a382016-07-29 17:28:03 -0700104 module, _ := NewLibrary(android.HostAndDeviceSupported, false, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700105 return module.Init()
106}
107
108// Module factory for shared libraries
109func librarySharedFactory() (blueprint.Module, []interface{}) {
Colin Crossb916a382016-07-29 17:28:03 -0700110 module, _ := NewLibrary(android.HostAndDeviceSupported, true, false)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700111 return module.Init()
112}
113
114// Module factory for host static libraries
115func libraryHostStaticFactory() (blueprint.Module, []interface{}) {
Colin Crossb916a382016-07-29 17:28:03 -0700116 module, _ := NewLibrary(android.HostSupported, false, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700117 return module.Init()
118}
119
120// Module factory for host shared libraries
121func libraryHostSharedFactory() (blueprint.Module, []interface{}) {
Colin Crossb916a382016-07-29 17:28:03 -0700122 module, _ := NewLibrary(android.HostSupported, true, false)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700123 return module.Init()
124}
125
126type flagExporter struct {
127 Properties FlagExporterProperties
128
Dan Willemsen847dcc72016-09-29 12:13:36 -0700129 flags []string
130 flagsDeps android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700131}
132
133func (f *flagExporter) exportIncludes(ctx ModuleContext, inc string) {
134 includeDirs := android.PathsForModuleSrc(ctx, f.Properties.Export_include_dirs)
135 for _, dir := range includeDirs.Strings() {
136 f.flags = append(f.flags, inc+dir)
137 }
138}
139
140func (f *flagExporter) reexportFlags(flags []string) {
141 f.flags = append(f.flags, flags...)
142}
143
Dan Willemsen847dcc72016-09-29 12:13:36 -0700144func (f *flagExporter) reexportDeps(deps android.Paths) {
145 f.flagsDeps = append(f.flagsDeps, deps...)
146}
147
Colin Cross4d9c2d12016-07-29 12:48:20 -0700148func (f *flagExporter) exportedFlags() []string {
149 return f.flags
150}
151
Dan Willemsen847dcc72016-09-29 12:13:36 -0700152func (f *flagExporter) exportedFlagsDeps() android.Paths {
153 return f.flagsDeps
154}
155
Colin Cross4d9c2d12016-07-29 12:48:20 -0700156type exportedFlagsProducer interface {
157 exportedFlags() []string
Dan Willemsen847dcc72016-09-29 12:13:36 -0700158 exportedFlagsDeps() android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700159}
160
161var _ exportedFlagsProducer = (*flagExporter)(nil)
162
Colin Crossb916a382016-07-29 17:28:03 -0700163// libraryDecorator wraps baseCompiler, baseLinker and baseInstaller to provide library-specific
164// functionality: static vs. shared linkage, reusing object files for shared libraries
165type libraryDecorator struct {
166 Properties LibraryProperties
Colin Cross4d9c2d12016-07-29 12:48:20 -0700167
168 // For reusing static library objects for shared library
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700169 reuseObjects Objects
Colin Cross26c34ed2016-09-30 17:10:16 -0700170 // table-of-contents file to optimize out relinking when possible
171 tocFile android.OptionalPath
Colin Cross4d9c2d12016-07-29 12:48:20 -0700172
Colin Cross4d9c2d12016-07-29 12:48:20 -0700173 flagExporter
174 stripper
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700175 relocationPacker
Colin Cross4d9c2d12016-07-29 12:48:20 -0700176
Colin Cross4d9c2d12016-07-29 12:48:20 -0700177 // If we're used as a whole_static_lib, our missing dependencies need
178 // to be given
179 wholeStaticMissingDeps []string
180
181 // For whole_static_libs
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700182 objects Objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700183
184 // Uses the module's name if empty, but can be overridden. Does not include
185 // shlib suffix.
186 libName string
Colin Crossb916a382016-07-29 17:28:03 -0700187
188 sanitize *sanitize
189
190 // Decorated interafaces
191 *baseCompiler
192 *baseLinker
193 *baseInstaller
Colin Cross4d9c2d12016-07-29 12:48:20 -0700194}
195
Colin Crossb916a382016-07-29 17:28:03 -0700196func (library *libraryDecorator) linkerProps() []interface{} {
197 var props []interface{}
198 props = append(props, library.baseLinker.linkerProps()...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700199 return append(props,
200 &library.Properties,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700201 &library.flagExporter.Properties,
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700202 &library.stripper.StripProperties,
203 &library.relocationPacker.Properties)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700204}
205
Colin Crossb916a382016-07-29 17:28:03 -0700206func (library *libraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross42742b82016-08-01 13:20:05 -0700207 flags = library.baseLinker.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700208
Colin Crossb916a382016-07-29 17:28:03 -0700209 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
210 // all code is position independent, and then those warnings get promoted to
211 // errors.
212 if ctx.Os() != android.Windows {
213 flags.CFlags = append(flags.CFlags, "-fPIC")
214 }
215
216 if library.static() {
217 flags.CFlags = append(flags.CFlags, library.Properties.Static.Cflags...)
218 } else {
219 flags.CFlags = append(flags.CFlags, library.Properties.Shared.Cflags...)
220 }
221
Colin Cross4d9c2d12016-07-29 12:48:20 -0700222 if !library.static() {
223 libName := library.getLibName(ctx)
224 // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead
225 sharedFlag := "-Wl,-shared"
226 if flags.Clang || ctx.Host() {
227 sharedFlag = "-shared"
228 }
229 var f []string
230 if ctx.Device() {
231 f = append(f,
232 "-nostdlib",
233 "-Wl,--gc-sections",
234 )
235 }
236
237 if ctx.Darwin() {
238 f = append(f,
239 "-dynamiclib",
240 "-single_module",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700241 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
242 )
Colin Cross7863cf52016-10-20 10:47:21 -0700243 if ctx.Arch().ArchType == android.X86 {
244 f = append(f,
245 "-read_only_relocs suppress",
246 )
247 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700248 } else {
249 f = append(f,
250 sharedFlag,
251 "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix())
252 }
253
254 flags.LdFlags = append(f, flags.LdFlags...)
255 }
256
257 return flags
258}
259
Dan Willemsen273af7f2016-11-03 15:53:42 -0700260func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
261 exportIncludeDirs := android.PathsForModuleSrc(ctx, library.flagExporter.Properties.Export_include_dirs)
262 if len(exportIncludeDirs) > 0 {
263 flags.GlobalFlags = append(flags.GlobalFlags, includeDirsToFlags(exportIncludeDirs))
264 }
265
266 return library.baseCompiler.compilerFlags(ctx, flags)
267}
268
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700269func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
270 objs := library.baseCompiler.compile(ctx, flags, deps)
271 library.reuseObjects = objs
Colin Cross2f336352016-10-26 10:03:47 -0700272 buildFlags := flagsToBuilderFlags(flags)
Colin Crossb916a382016-07-29 17:28:03 -0700273
Colin Cross4d9c2d12016-07-29 12:48:20 -0700274 if library.static() {
Colin Cross2f336352016-10-26 10:03:47 -0700275 srcs := android.PathsForModuleSrc(ctx, library.Properties.Static.Srcs)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700276 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceStaticLibrary,
277 srcs, library.baseCompiler.deps))
Colin Crossb916a382016-07-29 17:28:03 -0700278 } else {
Colin Cross2f336352016-10-26 10:03:47 -0700279 srcs := android.PathsForModuleSrc(ctx, library.Properties.Shared.Srcs)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700280 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceSharedLibrary,
281 srcs, library.baseCompiler.deps))
Colin Crossb916a382016-07-29 17:28:03 -0700282 }
283
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700284 return objs
Colin Crossb916a382016-07-29 17:28:03 -0700285}
286
287type libraryInterface interface {
288 getWholeStaticMissingDeps() []string
289 static() bool
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700290 objs() Objects
291 reuseObjs() Objects
Colin Cross26c34ed2016-09-30 17:10:16 -0700292 toc() android.OptionalPath
Colin Crossb916a382016-07-29 17:28:03 -0700293
294 // Returns true if the build options for the module have selected a static or shared build
295 buildStatic() bool
296 buildShared() bool
297
298 // Sets whether a specific variant is static or shared
299 setStatic(bool)
300}
301
302func (library *libraryDecorator) getLibName(ctx ModuleContext) string {
303 name := library.libName
304 if name == "" {
Colin Crossce75d2c2016-10-06 16:12:58 -0700305 name = ctx.baseModuleName()
Colin Crossb916a382016-07-29 17:28:03 -0700306 }
307
308 if ctx.Host() && Bool(library.Properties.Unique_host_soname) {
309 if !strings.HasSuffix(name, "-host") {
310 name = name + "-host"
311 }
312 }
313
314 return name + library.Properties.VariantName
315}
316
317func (library *libraryDecorator) linkerInit(ctx BaseModuleContext) {
318 location := InstallInSystem
319 if library.sanitize.inData() {
320 location = InstallInData
321 }
322 library.baseInstaller.location = location
323
324 library.baseLinker.linkerInit(ctx)
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700325
326 library.relocationPacker.packingInit(ctx)
Colin Crossb916a382016-07-29 17:28:03 -0700327}
328
329func (library *libraryDecorator) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
330 deps = library.baseLinker.linkerDeps(ctx, deps)
331
332 if library.static() {
333 deps.WholeStaticLibs = append(deps.WholeStaticLibs,
334 library.Properties.Static.Whole_static_libs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700335 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Static.Static_libs...)
336 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Static.Shared_libs...)
337 } else {
Dan Willemsen2e47b342016-11-17 01:02:25 -0800338 if ctx.toolchain().Bionic() && !Bool(library.baseLinker.Properties.Nocrt) {
Dan Willemsend2ede872016-11-18 14:54:24 -0800339 if !ctx.sdk() && !ctx.vndk() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700340 deps.CrtBegin = "crtbegin_so"
341 deps.CrtEnd = "crtend_so"
342 } else {
Dan Albertebedf672016-11-08 15:06:22 -0800343 // TODO(danalbert): Add generation of crt objects.
344 // For `sdk_version: "current"`, we don't actually have a
345 // freshly generated set of CRT objects. Use the last stable
346 // version.
347 version := ctx.sdkVersion()
348 if version == "current" {
349 version = ctx.AConfig().PlatformSdkVersion()
350 }
351 deps.CrtBegin = "ndk_crtbegin_so." + version
352 deps.CrtEnd = "ndk_crtend_so." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700353 }
354 }
355 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Shared.Whole_static_libs...)
356 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Shared.Static_libs...)
357 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Shared.Shared_libs...)
358 }
359
360 return deps
361}
362
Colin Crossb916a382016-07-29 17:28:03 -0700363func (library *libraryDecorator) linkStatic(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700364 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700365
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700366 library.objects = deps.WholeStaticLibObjs.Copy()
367 library.objects = library.objects.Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700368
369 outputFile := android.PathForModuleOut(ctx,
370 ctx.ModuleName()+library.Properties.VariantName+staticLibraryExtension)
371
372 if ctx.Darwin() {
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700373 TransformDarwinObjToStaticLib(ctx, library.objects.objFiles, flagsToBuilderFlags(flags), outputFile, objs.tidyFiles)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700374 } else {
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700375 TransformObjToStaticLib(ctx, library.objects.objFiles, flagsToBuilderFlags(flags), outputFile, objs.tidyFiles)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700376 }
377
378 library.wholeStaticMissingDeps = ctx.GetMissingDependencies()
379
380 ctx.CheckbuildFile(outputFile)
381
382 return outputFile
383}
384
Colin Crossb916a382016-07-29 17:28:03 -0700385func (library *libraryDecorator) linkShared(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700386 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700387
388 var linkerDeps android.Paths
389
390 versionScript := android.OptionalPathForModuleSrc(ctx, library.Properties.Version_script)
391 unexportedSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Unexported_symbols_list)
392 forceNotWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_not_weak_list)
393 forceWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_weak_list)
394 if !ctx.Darwin() {
395 if versionScript.Valid() {
396 flags.LdFlags = append(flags.LdFlags, "-Wl,--version-script,"+versionScript.String())
397 linkerDeps = append(linkerDeps, versionScript.Path())
398 }
399 if unexportedSymbols.Valid() {
400 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
401 }
402 if forceNotWeakSymbols.Valid() {
403 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
404 }
405 if forceWeakSymbols.Valid() {
406 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
407 }
408 } else {
409 if versionScript.Valid() {
410 ctx.PropertyErrorf("version_script", "Not supported on Darwin")
411 }
412 if unexportedSymbols.Valid() {
413 flags.LdFlags = append(flags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
414 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
415 }
416 if forceNotWeakSymbols.Valid() {
417 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
418 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
419 }
420 if forceWeakSymbols.Valid() {
421 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
422 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
423 }
424 }
425
426 fileName := library.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
427 outputFile := android.PathForModuleOut(ctx, fileName)
428 ret := outputFile
429
430 builderFlags := flagsToBuilderFlags(flags)
431
Colin Cross89562dc2016-10-03 17:47:19 -0700432 if !ctx.Darwin() {
433 // Optimize out relinking against shared libraries whose interface hasn't changed by
434 // depending on a table of contents file instead of the library itself.
435 tocPath := outputFile.RelPathString()
436 tocPath = pathtools.ReplaceExtension(tocPath, flags.Toolchain.ShlibSuffix()[1:]+".toc")
437 tocFile := android.PathForOutput(ctx, tocPath)
438 library.tocFile = android.OptionalPathForPath(tocFile)
439 TransformSharedObjectToToc(ctx, outputFile, tocFile, builderFlags)
440 }
441
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700442 if library.relocationPacker.needsPacking(ctx) {
443 packedOutputFile := outputFile
444 outputFile = android.PathForModuleOut(ctx, "unpacked", fileName)
445 library.relocationPacker.pack(ctx, outputFile, packedOutputFile, builderFlags)
446 }
447
Colin Cross4d9c2d12016-07-29 12:48:20 -0700448 if library.stripper.needsStrip(ctx) {
449 strippedOutputFile := outputFile
450 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
451 library.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
452 }
453
454 sharedLibs := deps.SharedLibs
455 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
456
Dan Albertd015c4a2016-08-10 14:34:08 -0700457 // TODO(danalbert): Clean this up when soong supports prebuilts.
458 if strings.HasPrefix(ctx.selectedStl(), "ndk_libc++") {
459 libDir := getNdkStlLibDir(ctx, flags.Toolchain, "libc++")
460
461 if strings.HasSuffix(ctx.selectedStl(), "_shared") {
462 deps.StaticLibs = append(deps.StaticLibs,
463 libDir.Join(ctx, "libandroid_support.a"))
464 } else {
465 deps.StaticLibs = append(deps.StaticLibs,
466 libDir.Join(ctx, "libc++abi.a"),
467 libDir.Join(ctx, "libandroid_support.a"))
468 }
469
470 if ctx.Arch().ArchType == android.Arm {
471 deps.StaticLibs = append(deps.StaticLibs,
472 libDir.Join(ctx, "libunwind.a"))
473 }
474 }
475
Colin Cross26c34ed2016-09-30 17:10:16 -0700476 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
477 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700478 linkerDeps = append(linkerDeps, objs.tidyFiles...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700479
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700480 TransformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700481 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
482 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile)
483
484 return ret
485}
486
Colin Crossb916a382016-07-29 17:28:03 -0700487func (library *libraryDecorator) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700488 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700489
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700490 objs = objs.Append(deps.Objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700491
492 var out android.Path
493 if library.static() {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700494 out = library.linkStatic(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700495 } else {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700496 out = library.linkShared(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700497 }
498
499 library.exportIncludes(ctx, "-I")
500 library.reexportFlags(deps.ReexportedFlags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700501 library.reexportDeps(deps.ReexportedFlagsDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700502
Dan Willemsene1240db2016-11-03 14:28:51 -0700503 if library.Properties.Aidl.Export_aidl_headers {
504 if library.baseCompiler.hasSrcExt(".aidl") {
505 library.reexportFlags([]string{
506 "-I" + android.PathForModuleGen(ctx, "aidl").String(),
507 })
508 library.reexportDeps(library.baseCompiler.deps) // TODO: restrict to aidl deps
509 }
510 }
511
512 if library.Properties.Proto.Export_proto_headers {
513 if library.baseCompiler.hasSrcExt(".proto") {
Colin Cross0c461f12016-10-20 16:11:43 -0700514 library.reexportFlags([]string{
515 "-I" + protoSubDir(ctx).String(),
516 "-I" + protoDir(ctx).String(),
517 })
518 library.reexportDeps(library.baseCompiler.deps) // TODO: restrict to proto deps
519 }
520 }
521
Colin Cross4d9c2d12016-07-29 12:48:20 -0700522 return out
523}
524
Colin Crossb916a382016-07-29 17:28:03 -0700525func (library *libraryDecorator) buildStatic() bool {
526 return library.Properties.BuildStatic &&
Colin Cross4d9c2d12016-07-29 12:48:20 -0700527 (library.Properties.Static.Enabled == nil || *library.Properties.Static.Enabled)
528}
529
Colin Crossb916a382016-07-29 17:28:03 -0700530func (library *libraryDecorator) buildShared() bool {
531 return library.Properties.BuildShared &&
Colin Cross4d9c2d12016-07-29 12:48:20 -0700532 (library.Properties.Shared.Enabled == nil || *library.Properties.Shared.Enabled)
533}
534
Colin Crossb916a382016-07-29 17:28:03 -0700535func (library *libraryDecorator) getWholeStaticMissingDeps() []string {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700536 return library.wholeStaticMissingDeps
537}
538
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700539func (library *libraryDecorator) objs() Objects {
540 return library.objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700541}
542
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700543func (library *libraryDecorator) reuseObjs() Objects {
544 return library.reuseObjects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700545}
546
Colin Cross26c34ed2016-09-30 17:10:16 -0700547func (library *libraryDecorator) toc() android.OptionalPath {
548 return library.tocFile
549}
550
Colin Crossb916a382016-07-29 17:28:03 -0700551func (library *libraryDecorator) install(ctx ModuleContext, file android.Path) {
552 if !ctx.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700553 library.baseInstaller.install(ctx, file)
554 }
555}
556
Colin Crossb916a382016-07-29 17:28:03 -0700557func (library *libraryDecorator) static() bool {
558 return library.Properties.VariantIsStatic
Colin Cross4d9c2d12016-07-29 12:48:20 -0700559}
560
Colin Crossb916a382016-07-29 17:28:03 -0700561func (library *libraryDecorator) setStatic(static bool) {
562 library.Properties.VariantIsStatic = static
563}
564
565func NewLibrary(hod android.HostOrDeviceSupported, shared, static bool) (*Module, *libraryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700566 module := newModule(hod, android.MultilibBoth)
567
Colin Crossb916a382016-07-29 17:28:03 -0700568 library := &libraryDecorator{
569 Properties: LibraryProperties{
570 BuildShared: shared,
571 BuildStatic: static,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700572 },
Colin Crossb916a382016-07-29 17:28:03 -0700573 baseCompiler: NewBaseCompiler(),
574 baseLinker: NewBaseLinker(),
575 baseInstaller: NewBaseInstaller("lib", "lib64", InstallInSystem),
576 sanitize: module.sanitize,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700577 }
578
Colin Crossb916a382016-07-29 17:28:03 -0700579 module.compiler = library
580 module.linker = library
581 module.installer = library
582
583 return module, library
584}
585
586func linkageMutator(mctx android.BottomUpMutatorContext) {
587 if m, ok := mctx.Module().(*Module); ok && m.linker != nil {
588 if library, ok := m.linker.(libraryInterface); ok {
589 var modules []blueprint.Module
590 if library.buildStatic() && library.buildShared() {
591 modules = mctx.CreateLocalVariations("static", "shared")
592 static := modules[0].(*Module)
593 shared := modules[1].(*Module)
594
595 static.linker.(libraryInterface).setStatic(true)
596 shared.linker.(libraryInterface).setStatic(false)
597
598 if staticCompiler, ok := static.compiler.(*libraryDecorator); ok {
599 sharedCompiler := shared.compiler.(*libraryDecorator)
600 if len(staticCompiler.Properties.Static.Cflags) == 0 &&
601 len(sharedCompiler.Properties.Shared.Cflags) == 0 {
602 // Optimize out compiling common .o files twice for static+shared libraries
603 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
604 sharedCompiler.baseCompiler.Properties.Srcs = nil
605 sharedCompiler.baseCompiler.Properties.Generated_sources = nil
606 }
607 }
608 } else if library.buildStatic() {
609 modules = mctx.CreateLocalVariations("static")
610 modules[0].(*Module).linker.(libraryInterface).setStatic(true)
611 } else if library.buildShared() {
612 modules = mctx.CreateLocalVariations("shared")
613 modules[0].(*Module).linker.(libraryInterface).setStatic(false)
614 }
615 }
616 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700617}