blob: 5a7afe19edf777ed7b5df48aa42b52dce754a8d0 [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
23 "android/soong"
24 "android/soong/android"
25)
26
Colin Crossb916a382016-07-29 17:28:03 -070027type LibraryProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -070028 Static struct {
29 Srcs []string `android:"arch_variant"`
30 Exclude_srcs []string `android:"arch_variant"`
31 Cflags []string `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070032
Colin Cross4d9c2d12016-07-29 12:48:20 -070033 Enabled *bool `android:"arch_variant"`
34 Whole_static_libs []string `android:"arch_variant"`
35 Static_libs []string `android:"arch_variant"`
36 Shared_libs []string `android:"arch_variant"`
37 } `android:"arch_variant"`
38 Shared struct {
Colin Crossb916a382016-07-29 17:28:03 -070039 Srcs []string `android:"arch_variant"`
40 Exclude_srcs []string `android:"arch_variant"`
41 Cflags []string `android:"arch_variant"`
42
Colin Cross4d9c2d12016-07-29 12:48:20 -070043 Enabled *bool `android:"arch_variant"`
44 Whole_static_libs []string `android:"arch_variant"`
45 Static_libs []string `android:"arch_variant"`
46 Shared_libs []string `android:"arch_variant"`
47 } `android:"arch_variant"`
48
49 // local file name to pass to the linker as --version_script
50 Version_script *string `android:"arch_variant"`
51 // local file name to pass to the linker as -unexported_symbols_list
52 Unexported_symbols_list *string `android:"arch_variant"`
53 // local file name to pass to the linker as -force_symbols_not_weak_list
54 Force_symbols_not_weak_list *string `android:"arch_variant"`
55 // local file name to pass to the linker as -force_symbols_weak_list
56 Force_symbols_weak_list *string `android:"arch_variant"`
57
58 // rename host libraries to prevent overlap with system installed libraries
59 Unique_host_soname *bool
60
61 VariantName string `blueprint:"mutated"`
Colin Crossb916a382016-07-29 17:28:03 -070062
63 // Build a static variant
64 BuildStatic bool `blueprint:"mutated"`
65 // Build a shared variant
66 BuildShared bool `blueprint:"mutated"`
67 // This variant is shared
68 VariantIsShared bool `blueprint:"mutated"`
69 // This variant is static
70 VariantIsStatic bool `blueprint:"mutated"`
71}
72
73type FlagExporterProperties struct {
74 // list of directories relative to the Blueprints file that will
75 // be added to the include path using -I for any module that links against this module
76 Export_include_dirs []string `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070077}
78
79func init() {
80 soong.RegisterModuleType("cc_library_static", libraryStaticFactory)
81 soong.RegisterModuleType("cc_library_shared", librarySharedFactory)
82 soong.RegisterModuleType("cc_library", libraryFactory)
83 soong.RegisterModuleType("cc_library_host_static", libraryHostStaticFactory)
84 soong.RegisterModuleType("cc_library_host_shared", libraryHostSharedFactory)
85}
86
87// Module factory for combined static + shared libraries, device by default but with possible host
88// support
89func libraryFactory() (blueprint.Module, []interface{}) {
Colin Crossb916a382016-07-29 17:28:03 -070090 module, _ := NewLibrary(android.HostAndDeviceSupported, true, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -070091 return module.Init()
92}
93
94// Module factory for static libraries
95func libraryStaticFactory() (blueprint.Module, []interface{}) {
Colin Crossb916a382016-07-29 17:28:03 -070096 module, _ := NewLibrary(android.HostAndDeviceSupported, false, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -070097 return module.Init()
98}
99
100// Module factory for shared libraries
101func librarySharedFactory() (blueprint.Module, []interface{}) {
Colin Crossb916a382016-07-29 17:28:03 -0700102 module, _ := NewLibrary(android.HostAndDeviceSupported, true, false)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700103 return module.Init()
104}
105
106// Module factory for host static libraries
107func libraryHostStaticFactory() (blueprint.Module, []interface{}) {
Colin Crossb916a382016-07-29 17:28:03 -0700108 module, _ := NewLibrary(android.HostSupported, false, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700109 return module.Init()
110}
111
112// Module factory for host shared libraries
113func libraryHostSharedFactory() (blueprint.Module, []interface{}) {
Colin Crossb916a382016-07-29 17:28:03 -0700114 module, _ := NewLibrary(android.HostSupported, true, false)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700115 return module.Init()
116}
117
118type flagExporter struct {
119 Properties FlagExporterProperties
120
Dan Willemsen847dcc72016-09-29 12:13:36 -0700121 flags []string
122 flagsDeps android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700123}
124
125func (f *flagExporter) exportIncludes(ctx ModuleContext, inc string) {
126 includeDirs := android.PathsForModuleSrc(ctx, f.Properties.Export_include_dirs)
127 for _, dir := range includeDirs.Strings() {
128 f.flags = append(f.flags, inc+dir)
129 }
130}
131
132func (f *flagExporter) reexportFlags(flags []string) {
133 f.flags = append(f.flags, flags...)
134}
135
Dan Willemsen847dcc72016-09-29 12:13:36 -0700136func (f *flagExporter) reexportDeps(deps android.Paths) {
137 f.flagsDeps = append(f.flagsDeps, deps...)
138}
139
Colin Cross4d9c2d12016-07-29 12:48:20 -0700140func (f *flagExporter) exportedFlags() []string {
141 return f.flags
142}
143
Dan Willemsen847dcc72016-09-29 12:13:36 -0700144func (f *flagExporter) exportedFlagsDeps() android.Paths {
145 return f.flagsDeps
146}
147
Colin Cross4d9c2d12016-07-29 12:48:20 -0700148type exportedFlagsProducer interface {
149 exportedFlags() []string
Dan Willemsen847dcc72016-09-29 12:13:36 -0700150 exportedFlagsDeps() android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700151}
152
153var _ exportedFlagsProducer = (*flagExporter)(nil)
154
Colin Crossb916a382016-07-29 17:28:03 -0700155// libraryDecorator wraps baseCompiler, baseLinker and baseInstaller to provide library-specific
156// functionality: static vs. shared linkage, reusing object files for shared libraries
157type libraryDecorator struct {
158 Properties LibraryProperties
Colin Cross4d9c2d12016-07-29 12:48:20 -0700159
160 // For reusing static library objects for shared library
161 reuseObjFiles android.Paths
Colin Cross26c34ed2016-09-30 17:10:16 -0700162 // table-of-contents file to optimize out relinking when possible
163 tocFile android.OptionalPath
Colin Cross4d9c2d12016-07-29 12:48:20 -0700164
Colin Cross4d9c2d12016-07-29 12:48:20 -0700165 flagExporter
166 stripper
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700167 relocationPacker
Colin Cross4d9c2d12016-07-29 12:48:20 -0700168
Colin Cross4d9c2d12016-07-29 12:48:20 -0700169 // If we're used as a whole_static_lib, our missing dependencies need
170 // to be given
171 wholeStaticMissingDeps []string
172
173 // For whole_static_libs
174 objFiles android.Paths
175
176 // Uses the module's name if empty, but can be overridden. Does not include
177 // shlib suffix.
178 libName string
Colin Crossb916a382016-07-29 17:28:03 -0700179
180 sanitize *sanitize
181
182 // Decorated interafaces
183 *baseCompiler
184 *baseLinker
185 *baseInstaller
Colin Cross4d9c2d12016-07-29 12:48:20 -0700186}
187
Colin Crossb916a382016-07-29 17:28:03 -0700188func (library *libraryDecorator) linkerProps() []interface{} {
189 var props []interface{}
190 props = append(props, library.baseLinker.linkerProps()...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700191 return append(props,
192 &library.Properties,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700193 &library.flagExporter.Properties,
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700194 &library.stripper.StripProperties,
195 &library.relocationPacker.Properties)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700196}
197
Colin Crossb916a382016-07-29 17:28:03 -0700198func (library *libraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross42742b82016-08-01 13:20:05 -0700199 flags = library.baseLinker.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700200
Colin Crossb916a382016-07-29 17:28:03 -0700201 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
202 // all code is position independent, and then those warnings get promoted to
203 // errors.
204 if ctx.Os() != android.Windows {
205 flags.CFlags = append(flags.CFlags, "-fPIC")
206 }
207
208 if library.static() {
209 flags.CFlags = append(flags.CFlags, library.Properties.Static.Cflags...)
210 } else {
211 flags.CFlags = append(flags.CFlags, library.Properties.Shared.Cflags...)
212 }
213
Colin Cross4d9c2d12016-07-29 12:48:20 -0700214 if !library.static() {
215 libName := library.getLibName(ctx)
216 // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead
217 sharedFlag := "-Wl,-shared"
218 if flags.Clang || ctx.Host() {
219 sharedFlag = "-shared"
220 }
221 var f []string
222 if ctx.Device() {
223 f = append(f,
224 "-nostdlib",
225 "-Wl,--gc-sections",
226 )
227 }
228
229 if ctx.Darwin() {
230 f = append(f,
231 "-dynamiclib",
232 "-single_module",
Colin Cross7d82ab72016-08-25 16:54:53 -0700233 "-read_only_relocs suppress",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700234 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
235 )
236 } else {
237 f = append(f,
238 sharedFlag,
239 "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix())
240 }
241
242 flags.LdFlags = append(f, flags.LdFlags...)
243 }
244
245 return flags
246}
247
Colin Crossb916a382016-07-29 17:28:03 -0700248func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths {
249 var objFiles android.Paths
250
251 objFiles = library.baseCompiler.compile(ctx, flags, deps)
252 library.reuseObjFiles = objFiles
253
254 pathDeps := deps.GeneratedHeaders
255 pathDeps = append(pathDeps, ndkPathDeps(ctx)...)
256
Colin Cross4d9c2d12016-07-29 12:48:20 -0700257 if library.static() {
Colin Crossb916a382016-07-29 17:28:03 -0700258 objFiles = append(objFiles, compileObjs(ctx, flags, android.DeviceStaticLibrary,
259 library.Properties.Static.Srcs, library.Properties.Static.Exclude_srcs,
260 nil, pathDeps)...)
261 } else {
262 objFiles = append(objFiles, compileObjs(ctx, flags, android.DeviceSharedLibrary,
263 library.Properties.Shared.Srcs, library.Properties.Shared.Exclude_srcs,
264 nil, pathDeps)...)
265 }
266
267 return objFiles
268}
269
270type libraryInterface interface {
271 getWholeStaticMissingDeps() []string
272 static() bool
273 objs() android.Paths
274 reuseObjs() android.Paths
Colin Cross26c34ed2016-09-30 17:10:16 -0700275 toc() android.OptionalPath
Colin Crossb916a382016-07-29 17:28:03 -0700276
277 // Returns true if the build options for the module have selected a static or shared build
278 buildStatic() bool
279 buildShared() bool
280
281 // Sets whether a specific variant is static or shared
282 setStatic(bool)
283}
284
285func (library *libraryDecorator) getLibName(ctx ModuleContext) string {
286 name := library.libName
287 if name == "" {
288 name = ctx.ModuleName()
289 }
290
291 if ctx.Host() && Bool(library.Properties.Unique_host_soname) {
292 if !strings.HasSuffix(name, "-host") {
293 name = name + "-host"
294 }
295 }
296
297 return name + library.Properties.VariantName
298}
299
300func (library *libraryDecorator) linkerInit(ctx BaseModuleContext) {
301 location := InstallInSystem
302 if library.sanitize.inData() {
303 location = InstallInData
304 }
305 library.baseInstaller.location = location
306
307 library.baseLinker.linkerInit(ctx)
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700308
309 library.relocationPacker.packingInit(ctx)
Colin Crossb916a382016-07-29 17:28:03 -0700310}
311
312func (library *libraryDecorator) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
313 deps = library.baseLinker.linkerDeps(ctx, deps)
314
315 if library.static() {
316 deps.WholeStaticLibs = append(deps.WholeStaticLibs,
317 library.Properties.Static.Whole_static_libs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700318 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Static.Static_libs...)
319 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Static.Shared_libs...)
320 } else {
321 if ctx.Device() && !Bool(library.baseLinker.Properties.Nocrt) {
322 if !ctx.sdk() {
323 deps.CrtBegin = "crtbegin_so"
324 deps.CrtEnd = "crtend_so"
325 } else {
326 deps.CrtBegin = "ndk_crtbegin_so." + ctx.sdkVersion()
327 deps.CrtEnd = "ndk_crtend_so." + ctx.sdkVersion()
328 }
329 }
330 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Shared.Whole_static_libs...)
331 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Shared.Static_libs...)
332 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Shared.Shared_libs...)
333 }
334
335 return deps
336}
337
Colin Crossb916a382016-07-29 17:28:03 -0700338func (library *libraryDecorator) linkStatic(ctx ModuleContext,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700339 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
340
341 library.objFiles = append(android.Paths{}, deps.WholeStaticLibObjFiles...)
342 library.objFiles = append(library.objFiles, objFiles...)
343
344 outputFile := android.PathForModuleOut(ctx,
345 ctx.ModuleName()+library.Properties.VariantName+staticLibraryExtension)
346
347 if ctx.Darwin() {
348 TransformDarwinObjToStaticLib(ctx, library.objFiles, flagsToBuilderFlags(flags), outputFile)
349 } else {
350 TransformObjToStaticLib(ctx, library.objFiles, flagsToBuilderFlags(flags), outputFile)
351 }
352
353 library.wholeStaticMissingDeps = ctx.GetMissingDependencies()
354
355 ctx.CheckbuildFile(outputFile)
356
357 return outputFile
358}
359
Colin Crossb916a382016-07-29 17:28:03 -0700360func (library *libraryDecorator) linkShared(ctx ModuleContext,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700361 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
362
363 var linkerDeps android.Paths
364
365 versionScript := android.OptionalPathForModuleSrc(ctx, library.Properties.Version_script)
366 unexportedSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Unexported_symbols_list)
367 forceNotWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_not_weak_list)
368 forceWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_weak_list)
369 if !ctx.Darwin() {
370 if versionScript.Valid() {
371 flags.LdFlags = append(flags.LdFlags, "-Wl,--version-script,"+versionScript.String())
372 linkerDeps = append(linkerDeps, versionScript.Path())
373 }
374 if unexportedSymbols.Valid() {
375 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
376 }
377 if forceNotWeakSymbols.Valid() {
378 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
379 }
380 if forceWeakSymbols.Valid() {
381 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
382 }
383 } else {
384 if versionScript.Valid() {
385 ctx.PropertyErrorf("version_script", "Not supported on Darwin")
386 }
387 if unexportedSymbols.Valid() {
388 flags.LdFlags = append(flags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
389 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
390 }
391 if forceNotWeakSymbols.Valid() {
392 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
393 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
394 }
395 if forceWeakSymbols.Valid() {
396 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
397 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
398 }
399 }
400
401 fileName := library.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
402 outputFile := android.PathForModuleOut(ctx, fileName)
403 ret := outputFile
404
405 builderFlags := flagsToBuilderFlags(flags)
406
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700407 if library.relocationPacker.needsPacking(ctx) {
408 packedOutputFile := outputFile
409 outputFile = android.PathForModuleOut(ctx, "unpacked", fileName)
410 library.relocationPacker.pack(ctx, outputFile, packedOutputFile, builderFlags)
411 }
412
Colin Cross4d9c2d12016-07-29 12:48:20 -0700413 if library.stripper.needsStrip(ctx) {
414 strippedOutputFile := outputFile
415 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
416 library.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
417 }
418
419 sharedLibs := deps.SharedLibs
420 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
421
Dan Albertd015c4a2016-08-10 14:34:08 -0700422 // TODO(danalbert): Clean this up when soong supports prebuilts.
423 if strings.HasPrefix(ctx.selectedStl(), "ndk_libc++") {
424 libDir := getNdkStlLibDir(ctx, flags.Toolchain, "libc++")
425
426 if strings.HasSuffix(ctx.selectedStl(), "_shared") {
427 deps.StaticLibs = append(deps.StaticLibs,
428 libDir.Join(ctx, "libandroid_support.a"))
429 } else {
430 deps.StaticLibs = append(deps.StaticLibs,
431 libDir.Join(ctx, "libc++abi.a"),
432 libDir.Join(ctx, "libandroid_support.a"))
433 }
434
435 if ctx.Arch().ArchType == android.Arm {
436 deps.StaticLibs = append(deps.StaticLibs,
437 libDir.Join(ctx, "libunwind.a"))
438 }
439 }
440
Colin Cross26c34ed2016-09-30 17:10:16 -0700441 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
442 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
443
Colin Cross4d9c2d12016-07-29 12:48:20 -0700444 TransformObjToDynamicBinary(ctx, objFiles, sharedLibs,
445 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
446 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile)
447
Colin Cross26c34ed2016-09-30 17:10:16 -0700448 if ctx.Device() {
449 // For device targets, optimize out relinking against shared
450 // libraries whose interface hasn't changed by depending on
451 // a table of contents file instead of the library itself.
452 // For host targets, the library might be part of a host tool
453 // that is run during the build, use the library directly so
454 // that the timestamp of the binary changes whenever a library
455 // changes and any necessary tools get re-run.
Colin Cross4d676422016-10-04 09:35:23 -0700456 tocPath := outputFile.RelPathString()
Colin Cross26c34ed2016-09-30 17:10:16 -0700457 tocPath = pathtools.ReplaceExtension(tocPath, flags.Toolchain.ShlibSuffix()[1:]+".toc")
458 tocFile := android.PathForOutput(ctx, tocPath)
459 library.tocFile = android.OptionalPathForPath(tocFile)
460 TransformSharedObjectToToc(ctx, outputFile, tocFile, builderFlags)
461 }
462
Colin Cross4d9c2d12016-07-29 12:48:20 -0700463 return ret
464}
465
Colin Crossb916a382016-07-29 17:28:03 -0700466func (library *libraryDecorator) link(ctx ModuleContext,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700467 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
468
469 objFiles = append(objFiles, deps.ObjFiles...)
470
471 var out android.Path
472 if library.static() {
473 out = library.linkStatic(ctx, flags, deps, objFiles)
474 } else {
475 out = library.linkShared(ctx, flags, deps, objFiles)
476 }
477
478 library.exportIncludes(ctx, "-I")
479 library.reexportFlags(deps.ReexportedFlags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700480 library.reexportDeps(deps.ReexportedFlagsDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700481
482 return out
483}
484
Colin Crossb916a382016-07-29 17:28:03 -0700485func (library *libraryDecorator) buildStatic() bool {
486 return library.Properties.BuildStatic &&
Colin Cross4d9c2d12016-07-29 12:48:20 -0700487 (library.Properties.Static.Enabled == nil || *library.Properties.Static.Enabled)
488}
489
Colin Crossb916a382016-07-29 17:28:03 -0700490func (library *libraryDecorator) buildShared() bool {
491 return library.Properties.BuildShared &&
Colin Cross4d9c2d12016-07-29 12:48:20 -0700492 (library.Properties.Shared.Enabled == nil || *library.Properties.Shared.Enabled)
493}
494
Colin Crossb916a382016-07-29 17:28:03 -0700495func (library *libraryDecorator) getWholeStaticMissingDeps() []string {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700496 return library.wholeStaticMissingDeps
497}
498
Colin Crossb916a382016-07-29 17:28:03 -0700499func (library *libraryDecorator) objs() android.Paths {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700500 return library.objFiles
501}
502
Colin Crossb916a382016-07-29 17:28:03 -0700503func (library *libraryDecorator) reuseObjs() android.Paths {
504 return library.reuseObjFiles
Colin Cross4d9c2d12016-07-29 12:48:20 -0700505}
506
Colin Cross26c34ed2016-09-30 17:10:16 -0700507func (library *libraryDecorator) toc() android.OptionalPath {
508 return library.tocFile
509}
510
Colin Crossb916a382016-07-29 17:28:03 -0700511func (library *libraryDecorator) install(ctx ModuleContext, file android.Path) {
512 if !ctx.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700513 library.baseInstaller.install(ctx, file)
514 }
515}
516
Colin Crossb916a382016-07-29 17:28:03 -0700517func (library *libraryDecorator) static() bool {
518 return library.Properties.VariantIsStatic
Colin Cross4d9c2d12016-07-29 12:48:20 -0700519}
520
Colin Crossb916a382016-07-29 17:28:03 -0700521func (library *libraryDecorator) setStatic(static bool) {
522 library.Properties.VariantIsStatic = static
523}
524
525func NewLibrary(hod android.HostOrDeviceSupported, shared, static bool) (*Module, *libraryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700526 module := newModule(hod, android.MultilibBoth)
527
Colin Crossb916a382016-07-29 17:28:03 -0700528 library := &libraryDecorator{
529 Properties: LibraryProperties{
530 BuildShared: shared,
531 BuildStatic: static,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700532 },
Colin Crossb916a382016-07-29 17:28:03 -0700533 baseCompiler: NewBaseCompiler(),
534 baseLinker: NewBaseLinker(),
535 baseInstaller: NewBaseInstaller("lib", "lib64", InstallInSystem),
536 sanitize: module.sanitize,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700537 }
538
Colin Crossb916a382016-07-29 17:28:03 -0700539 module.compiler = library
540 module.linker = library
541 module.installer = library
542
543 return module, library
544}
545
546func linkageMutator(mctx android.BottomUpMutatorContext) {
547 if m, ok := mctx.Module().(*Module); ok && m.linker != nil {
548 if library, ok := m.linker.(libraryInterface); ok {
549 var modules []blueprint.Module
550 if library.buildStatic() && library.buildShared() {
551 modules = mctx.CreateLocalVariations("static", "shared")
552 static := modules[0].(*Module)
553 shared := modules[1].(*Module)
554
555 static.linker.(libraryInterface).setStatic(true)
556 shared.linker.(libraryInterface).setStatic(false)
557
558 if staticCompiler, ok := static.compiler.(*libraryDecorator); ok {
559 sharedCompiler := shared.compiler.(*libraryDecorator)
560 if len(staticCompiler.Properties.Static.Cflags) == 0 &&
561 len(sharedCompiler.Properties.Shared.Cflags) == 0 {
562 // Optimize out compiling common .o files twice for static+shared libraries
563 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
564 sharedCompiler.baseCompiler.Properties.Srcs = nil
565 sharedCompiler.baseCompiler.Properties.Generated_sources = nil
566 }
567 }
568 } else if library.buildStatic() {
569 modules = mctx.CreateLocalVariations("static")
570 modules[0].(*Module).linker.(libraryInterface).setStatic(true)
571 } else if library.buildShared() {
572 modules = mctx.CreateLocalVariations("shared")
573 modules[0].(*Module).linker.(libraryInterface).setStatic(false)
574 }
575 }
576 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700577}