blob: 99a9b4886c25351a48a91701fed47a2e0751026c [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
Colin Cross0c461f12016-10-20 16:11:43 -070058 Proto struct {
59 // export headers generated from .proto sources
60 Export_proto_headers bool
61 }
62
Colin Cross4d9c2d12016-07-29 12:48:20 -070063 VariantName string `blueprint:"mutated"`
Colin Crossb916a382016-07-29 17:28:03 -070064
65 // Build a static variant
66 BuildStatic bool `blueprint:"mutated"`
67 // Build a shared variant
68 BuildShared bool `blueprint:"mutated"`
69 // This variant is shared
70 VariantIsShared bool `blueprint:"mutated"`
71 // This variant is static
72 VariantIsStatic bool `blueprint:"mutated"`
73}
74
75type FlagExporterProperties struct {
76 // list of directories relative to the Blueprints file that will
77 // be added to the include path using -I for any module that links against this module
78 Export_include_dirs []string `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070079}
80
81func init() {
Colin Cross798bfce2016-10-12 14:28:16 -070082 android.RegisterModuleType("cc_library_static", libraryStaticFactory)
83 android.RegisterModuleType("cc_library_shared", librarySharedFactory)
84 android.RegisterModuleType("cc_library", libraryFactory)
85 android.RegisterModuleType("cc_library_host_static", libraryHostStaticFactory)
86 android.RegisterModuleType("cc_library_host_shared", libraryHostSharedFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -070087}
88
89// Module factory for combined static + shared libraries, device by default but with possible host
90// support
91func libraryFactory() (blueprint.Module, []interface{}) {
Colin Crossb916a382016-07-29 17:28:03 -070092 module, _ := NewLibrary(android.HostAndDeviceSupported, true, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -070093 return module.Init()
94}
95
96// Module factory for static libraries
97func libraryStaticFactory() (blueprint.Module, []interface{}) {
Colin Crossb916a382016-07-29 17:28:03 -070098 module, _ := NewLibrary(android.HostAndDeviceSupported, false, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -070099 return module.Init()
100}
101
102// Module factory for shared libraries
103func librarySharedFactory() (blueprint.Module, []interface{}) {
Colin Crossb916a382016-07-29 17:28:03 -0700104 module, _ := NewLibrary(android.HostAndDeviceSupported, true, false)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700105 return module.Init()
106}
107
108// Module factory for host static libraries
109func libraryHostStaticFactory() (blueprint.Module, []interface{}) {
Colin Crossb916a382016-07-29 17:28:03 -0700110 module, _ := NewLibrary(android.HostSupported, false, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700111 return module.Init()
112}
113
114// Module factory for host shared libraries
115func libraryHostSharedFactory() (blueprint.Module, []interface{}) {
Colin Crossb916a382016-07-29 17:28:03 -0700116 module, _ := NewLibrary(android.HostSupported, true, false)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700117 return module.Init()
118}
119
120type flagExporter struct {
121 Properties FlagExporterProperties
122
Dan Willemsen847dcc72016-09-29 12:13:36 -0700123 flags []string
124 flagsDeps android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700125}
126
127func (f *flagExporter) exportIncludes(ctx ModuleContext, inc string) {
128 includeDirs := android.PathsForModuleSrc(ctx, f.Properties.Export_include_dirs)
129 for _, dir := range includeDirs.Strings() {
130 f.flags = append(f.flags, inc+dir)
131 }
132}
133
134func (f *flagExporter) reexportFlags(flags []string) {
135 f.flags = append(f.flags, flags...)
136}
137
Dan Willemsen847dcc72016-09-29 12:13:36 -0700138func (f *flagExporter) reexportDeps(deps android.Paths) {
139 f.flagsDeps = append(f.flagsDeps, deps...)
140}
141
Colin Cross4d9c2d12016-07-29 12:48:20 -0700142func (f *flagExporter) exportedFlags() []string {
143 return f.flags
144}
145
Dan Willemsen847dcc72016-09-29 12:13:36 -0700146func (f *flagExporter) exportedFlagsDeps() android.Paths {
147 return f.flagsDeps
148}
149
Colin Cross4d9c2d12016-07-29 12:48:20 -0700150type exportedFlagsProducer interface {
151 exportedFlags() []string
Dan Willemsen847dcc72016-09-29 12:13:36 -0700152 exportedFlagsDeps() android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700153}
154
155var _ exportedFlagsProducer = (*flagExporter)(nil)
156
Colin Crossb916a382016-07-29 17:28:03 -0700157// libraryDecorator wraps baseCompiler, baseLinker and baseInstaller to provide library-specific
158// functionality: static vs. shared linkage, reusing object files for shared libraries
159type libraryDecorator struct {
160 Properties LibraryProperties
Colin Cross4d9c2d12016-07-29 12:48:20 -0700161
162 // For reusing static library objects for shared library
163 reuseObjFiles android.Paths
Colin Cross26c34ed2016-09-30 17:10:16 -0700164 // table-of-contents file to optimize out relinking when possible
165 tocFile android.OptionalPath
Colin Cross4d9c2d12016-07-29 12:48:20 -0700166
Colin Cross4d9c2d12016-07-29 12:48:20 -0700167 flagExporter
168 stripper
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700169 relocationPacker
Colin Cross4d9c2d12016-07-29 12:48:20 -0700170
Colin Cross4d9c2d12016-07-29 12:48:20 -0700171 // If we're used as a whole_static_lib, our missing dependencies need
172 // to be given
173 wholeStaticMissingDeps []string
174
175 // For whole_static_libs
176 objFiles android.Paths
177
178 // Uses the module's name if empty, but can be overridden. Does not include
179 // shlib suffix.
180 libName string
Colin Crossb916a382016-07-29 17:28:03 -0700181
182 sanitize *sanitize
183
184 // Decorated interafaces
185 *baseCompiler
186 *baseLinker
187 *baseInstaller
Colin Cross4d9c2d12016-07-29 12:48:20 -0700188}
189
Colin Crossb916a382016-07-29 17:28:03 -0700190func (library *libraryDecorator) linkerProps() []interface{} {
191 var props []interface{}
192 props = append(props, library.baseLinker.linkerProps()...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700193 return append(props,
194 &library.Properties,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700195 &library.flagExporter.Properties,
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700196 &library.stripper.StripProperties,
197 &library.relocationPacker.Properties)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700198}
199
Colin Crossb916a382016-07-29 17:28:03 -0700200func (library *libraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross42742b82016-08-01 13:20:05 -0700201 flags = library.baseLinker.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700202
Colin Crossb916a382016-07-29 17:28:03 -0700203 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
204 // all code is position independent, and then those warnings get promoted to
205 // errors.
206 if ctx.Os() != android.Windows {
207 flags.CFlags = append(flags.CFlags, "-fPIC")
208 }
209
210 if library.static() {
211 flags.CFlags = append(flags.CFlags, library.Properties.Static.Cflags...)
212 } else {
213 flags.CFlags = append(flags.CFlags, library.Properties.Shared.Cflags...)
214 }
215
Colin Cross4d9c2d12016-07-29 12:48:20 -0700216 if !library.static() {
217 libName := library.getLibName(ctx)
218 // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead
219 sharedFlag := "-Wl,-shared"
220 if flags.Clang || ctx.Host() {
221 sharedFlag = "-shared"
222 }
223 var f []string
224 if ctx.Device() {
225 f = append(f,
226 "-nostdlib",
227 "-Wl,--gc-sections",
228 )
229 }
230
231 if ctx.Darwin() {
232 f = append(f,
233 "-dynamiclib",
234 "-single_module",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700235 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
236 )
Colin Cross7863cf52016-10-20 10:47:21 -0700237 if ctx.Arch().ArchType == android.X86 {
238 f = append(f,
239 "-read_only_relocs suppress",
240 )
241 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700242 } else {
243 f = append(f,
244 sharedFlag,
245 "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix())
246 }
247
248 flags.LdFlags = append(f, flags.LdFlags...)
249 }
250
251 return flags
252}
253
Colin Crossb916a382016-07-29 17:28:03 -0700254func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths {
255 var objFiles android.Paths
256
257 objFiles = library.baseCompiler.compile(ctx, flags, deps)
258 library.reuseObjFiles = objFiles
Colin Cross2f336352016-10-26 10:03:47 -0700259 buildFlags := flagsToBuilderFlags(flags)
Colin Crossb916a382016-07-29 17:28:03 -0700260
Colin Cross4d9c2d12016-07-29 12:48:20 -0700261 if library.static() {
Colin Cross2f336352016-10-26 10:03:47 -0700262 srcs := android.PathsForModuleSrc(ctx, library.Properties.Static.Srcs)
263 objFiles = append(objFiles, compileObjs(ctx, buildFlags, android.DeviceStaticLibrary,
264 srcs, library.baseCompiler.deps)...)
Colin Crossb916a382016-07-29 17:28:03 -0700265 } else {
Colin Cross2f336352016-10-26 10:03:47 -0700266 srcs := android.PathsForModuleSrc(ctx, library.Properties.Shared.Srcs)
267 objFiles = append(objFiles, compileObjs(ctx, buildFlags, android.DeviceSharedLibrary,
268 srcs, library.baseCompiler.deps)...)
Colin Crossb916a382016-07-29 17:28:03 -0700269 }
270
271 return objFiles
272}
273
274type libraryInterface interface {
275 getWholeStaticMissingDeps() []string
276 static() bool
277 objs() android.Paths
278 reuseObjs() android.Paths
Colin Cross26c34ed2016-09-30 17:10:16 -0700279 toc() android.OptionalPath
Colin Crossb916a382016-07-29 17:28:03 -0700280
281 // Returns true if the build options for the module have selected a static or shared build
282 buildStatic() bool
283 buildShared() bool
284
285 // Sets whether a specific variant is static or shared
286 setStatic(bool)
287}
288
289func (library *libraryDecorator) getLibName(ctx ModuleContext) string {
290 name := library.libName
291 if name == "" {
Colin Crossce75d2c2016-10-06 16:12:58 -0700292 name = ctx.baseModuleName()
Colin Crossb916a382016-07-29 17:28:03 -0700293 }
294
295 if ctx.Host() && Bool(library.Properties.Unique_host_soname) {
296 if !strings.HasSuffix(name, "-host") {
297 name = name + "-host"
298 }
299 }
300
301 return name + library.Properties.VariantName
302}
303
304func (library *libraryDecorator) linkerInit(ctx BaseModuleContext) {
305 location := InstallInSystem
306 if library.sanitize.inData() {
307 location = InstallInData
308 }
309 library.baseInstaller.location = location
310
311 library.baseLinker.linkerInit(ctx)
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700312
313 library.relocationPacker.packingInit(ctx)
Colin Crossb916a382016-07-29 17:28:03 -0700314}
315
316func (library *libraryDecorator) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
317 deps = library.baseLinker.linkerDeps(ctx, deps)
318
319 if library.static() {
320 deps.WholeStaticLibs = append(deps.WholeStaticLibs,
321 library.Properties.Static.Whole_static_libs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700322 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Static.Static_libs...)
323 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Static.Shared_libs...)
324 } else {
325 if ctx.Device() && !Bool(library.baseLinker.Properties.Nocrt) {
326 if !ctx.sdk() {
327 deps.CrtBegin = "crtbegin_so"
328 deps.CrtEnd = "crtend_so"
329 } else {
330 deps.CrtBegin = "ndk_crtbegin_so." + ctx.sdkVersion()
331 deps.CrtEnd = "ndk_crtend_so." + ctx.sdkVersion()
332 }
333 }
334 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Shared.Whole_static_libs...)
335 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Shared.Static_libs...)
336 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Shared.Shared_libs...)
337 }
338
339 return deps
340}
341
Colin Crossb916a382016-07-29 17:28:03 -0700342func (library *libraryDecorator) linkStatic(ctx ModuleContext,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700343 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
344
345 library.objFiles = append(android.Paths{}, deps.WholeStaticLibObjFiles...)
346 library.objFiles = append(library.objFiles, objFiles...)
347
348 outputFile := android.PathForModuleOut(ctx,
349 ctx.ModuleName()+library.Properties.VariantName+staticLibraryExtension)
350
351 if ctx.Darwin() {
352 TransformDarwinObjToStaticLib(ctx, library.objFiles, flagsToBuilderFlags(flags), outputFile)
353 } else {
354 TransformObjToStaticLib(ctx, library.objFiles, flagsToBuilderFlags(flags), outputFile)
355 }
356
357 library.wholeStaticMissingDeps = ctx.GetMissingDependencies()
358
359 ctx.CheckbuildFile(outputFile)
360
361 return outputFile
362}
363
Colin Crossb916a382016-07-29 17:28:03 -0700364func (library *libraryDecorator) linkShared(ctx ModuleContext,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700365 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
366
367 var linkerDeps android.Paths
368
369 versionScript := android.OptionalPathForModuleSrc(ctx, library.Properties.Version_script)
370 unexportedSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Unexported_symbols_list)
371 forceNotWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_not_weak_list)
372 forceWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_weak_list)
373 if !ctx.Darwin() {
374 if versionScript.Valid() {
375 flags.LdFlags = append(flags.LdFlags, "-Wl,--version-script,"+versionScript.String())
376 linkerDeps = append(linkerDeps, versionScript.Path())
377 }
378 if unexportedSymbols.Valid() {
379 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
380 }
381 if forceNotWeakSymbols.Valid() {
382 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
383 }
384 if forceWeakSymbols.Valid() {
385 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
386 }
387 } else {
388 if versionScript.Valid() {
389 ctx.PropertyErrorf("version_script", "Not supported on Darwin")
390 }
391 if unexportedSymbols.Valid() {
392 flags.LdFlags = append(flags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
393 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
394 }
395 if forceNotWeakSymbols.Valid() {
396 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
397 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
398 }
399 if forceWeakSymbols.Valid() {
400 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
401 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
402 }
403 }
404
405 fileName := library.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
406 outputFile := android.PathForModuleOut(ctx, fileName)
407 ret := outputFile
408
409 builderFlags := flagsToBuilderFlags(flags)
410
Colin Cross89562dc2016-10-03 17:47:19 -0700411 if !ctx.Darwin() {
412 // Optimize out relinking against shared libraries whose interface hasn't changed by
413 // depending on a table of contents file instead of the library itself.
414 tocPath := outputFile.RelPathString()
415 tocPath = pathtools.ReplaceExtension(tocPath, flags.Toolchain.ShlibSuffix()[1:]+".toc")
416 tocFile := android.PathForOutput(ctx, tocPath)
417 library.tocFile = android.OptionalPathForPath(tocFile)
418 TransformSharedObjectToToc(ctx, outputFile, tocFile, builderFlags)
419 }
420
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700421 if library.relocationPacker.needsPacking(ctx) {
422 packedOutputFile := outputFile
423 outputFile = android.PathForModuleOut(ctx, "unpacked", fileName)
424 library.relocationPacker.pack(ctx, outputFile, packedOutputFile, builderFlags)
425 }
426
Colin Cross4d9c2d12016-07-29 12:48:20 -0700427 if library.stripper.needsStrip(ctx) {
428 strippedOutputFile := outputFile
429 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
430 library.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
431 }
432
433 sharedLibs := deps.SharedLibs
434 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
435
Dan Albertd015c4a2016-08-10 14:34:08 -0700436 // TODO(danalbert): Clean this up when soong supports prebuilts.
437 if strings.HasPrefix(ctx.selectedStl(), "ndk_libc++") {
438 libDir := getNdkStlLibDir(ctx, flags.Toolchain, "libc++")
439
440 if strings.HasSuffix(ctx.selectedStl(), "_shared") {
441 deps.StaticLibs = append(deps.StaticLibs,
442 libDir.Join(ctx, "libandroid_support.a"))
443 } else {
444 deps.StaticLibs = append(deps.StaticLibs,
445 libDir.Join(ctx, "libc++abi.a"),
446 libDir.Join(ctx, "libandroid_support.a"))
447 }
448
449 if ctx.Arch().ArchType == android.Arm {
450 deps.StaticLibs = append(deps.StaticLibs,
451 libDir.Join(ctx, "libunwind.a"))
452 }
453 }
454
Colin Cross26c34ed2016-09-30 17:10:16 -0700455 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
456 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
457
Colin Cross4d9c2d12016-07-29 12:48:20 -0700458 TransformObjToDynamicBinary(ctx, objFiles, sharedLibs,
459 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
460 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile)
461
462 return ret
463}
464
Colin Crossb916a382016-07-29 17:28:03 -0700465func (library *libraryDecorator) link(ctx ModuleContext,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700466 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
467
468 objFiles = append(objFiles, deps.ObjFiles...)
469
470 var out android.Path
471 if library.static() {
472 out = library.linkStatic(ctx, flags, deps, objFiles)
473 } else {
474 out = library.linkShared(ctx, flags, deps, objFiles)
475 }
476
477 library.exportIncludes(ctx, "-I")
478 library.reexportFlags(deps.ReexportedFlags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700479 library.reexportDeps(deps.ReexportedFlagsDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700480
Colin Cross0c461f12016-10-20 16:11:43 -0700481 if library.baseCompiler.hasProto() {
482 if library.Properties.Proto.Export_proto_headers {
483 library.reexportFlags([]string{
484 "-I" + protoSubDir(ctx).String(),
485 "-I" + protoDir(ctx).String(),
486 })
487 library.reexportDeps(library.baseCompiler.deps) // TODO: restrict to proto deps
488 }
489 }
490
Colin Cross4d9c2d12016-07-29 12:48:20 -0700491 return out
492}
493
Colin Crossb916a382016-07-29 17:28:03 -0700494func (library *libraryDecorator) buildStatic() bool {
495 return library.Properties.BuildStatic &&
Colin Cross4d9c2d12016-07-29 12:48:20 -0700496 (library.Properties.Static.Enabled == nil || *library.Properties.Static.Enabled)
497}
498
Colin Crossb916a382016-07-29 17:28:03 -0700499func (library *libraryDecorator) buildShared() bool {
500 return library.Properties.BuildShared &&
Colin Cross4d9c2d12016-07-29 12:48:20 -0700501 (library.Properties.Shared.Enabled == nil || *library.Properties.Shared.Enabled)
502}
503
Colin Crossb916a382016-07-29 17:28:03 -0700504func (library *libraryDecorator) getWholeStaticMissingDeps() []string {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700505 return library.wholeStaticMissingDeps
506}
507
Colin Crossb916a382016-07-29 17:28:03 -0700508func (library *libraryDecorator) objs() android.Paths {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700509 return library.objFiles
510}
511
Colin Crossb916a382016-07-29 17:28:03 -0700512func (library *libraryDecorator) reuseObjs() android.Paths {
513 return library.reuseObjFiles
Colin Cross4d9c2d12016-07-29 12:48:20 -0700514}
515
Colin Cross26c34ed2016-09-30 17:10:16 -0700516func (library *libraryDecorator) toc() android.OptionalPath {
517 return library.tocFile
518}
519
Colin Crossb916a382016-07-29 17:28:03 -0700520func (library *libraryDecorator) install(ctx ModuleContext, file android.Path) {
521 if !ctx.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700522 library.baseInstaller.install(ctx, file)
523 }
524}
525
Colin Crossb916a382016-07-29 17:28:03 -0700526func (library *libraryDecorator) static() bool {
527 return library.Properties.VariantIsStatic
Colin Cross4d9c2d12016-07-29 12:48:20 -0700528}
529
Colin Crossb916a382016-07-29 17:28:03 -0700530func (library *libraryDecorator) setStatic(static bool) {
531 library.Properties.VariantIsStatic = static
532}
533
534func NewLibrary(hod android.HostOrDeviceSupported, shared, static bool) (*Module, *libraryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700535 module := newModule(hod, android.MultilibBoth)
536
Colin Crossb916a382016-07-29 17:28:03 -0700537 library := &libraryDecorator{
538 Properties: LibraryProperties{
539 BuildShared: shared,
540 BuildStatic: static,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700541 },
Colin Crossb916a382016-07-29 17:28:03 -0700542 baseCompiler: NewBaseCompiler(),
543 baseLinker: NewBaseLinker(),
544 baseInstaller: NewBaseInstaller("lib", "lib64", InstallInSystem),
545 sanitize: module.sanitize,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700546 }
547
Colin Crossb916a382016-07-29 17:28:03 -0700548 module.compiler = library
549 module.linker = library
550 module.installer = library
551
552 return module, library
553}
554
555func linkageMutator(mctx android.BottomUpMutatorContext) {
556 if m, ok := mctx.Module().(*Module); ok && m.linker != nil {
557 if library, ok := m.linker.(libraryInterface); ok {
558 var modules []blueprint.Module
559 if library.buildStatic() && library.buildShared() {
560 modules = mctx.CreateLocalVariations("static", "shared")
561 static := modules[0].(*Module)
562 shared := modules[1].(*Module)
563
564 static.linker.(libraryInterface).setStatic(true)
565 shared.linker.(libraryInterface).setStatic(false)
566
567 if staticCompiler, ok := static.compiler.(*libraryDecorator); ok {
568 sharedCompiler := shared.compiler.(*libraryDecorator)
569 if len(staticCompiler.Properties.Static.Cflags) == 0 &&
570 len(sharedCompiler.Properties.Shared.Cflags) == 0 {
571 // Optimize out compiling common .o files twice for static+shared libraries
572 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
573 sharedCompiler.baseCompiler.Properties.Srcs = nil
574 sharedCompiler.baseCompiler.Properties.Generated_sources = nil
575 }
576 }
577 } else if library.buildStatic() {
578 modules = mctx.CreateLocalVariations("static")
579 modules[0].(*Module).linker.(libraryInterface).setStatic(true)
580 } else if library.buildShared() {
581 modules = mctx.CreateLocalVariations("shared")
582 modules[0].(*Module).linker.(libraryInterface).setStatic(false)
583 }
584 }
585 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700586}