blob: 2c5f22f9ca75e052345b1076e2ced60850241d9e [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
Colin Cross89562dc2016-10-03 17:47:19 -0700407 if !ctx.Darwin() {
408 // Optimize out relinking against shared libraries whose interface hasn't changed by
409 // depending on a table of contents file instead of the library itself.
410 tocPath := outputFile.RelPathString()
411 tocPath = pathtools.ReplaceExtension(tocPath, flags.Toolchain.ShlibSuffix()[1:]+".toc")
412 tocFile := android.PathForOutput(ctx, tocPath)
413 library.tocFile = android.OptionalPathForPath(tocFile)
414 TransformSharedObjectToToc(ctx, outputFile, tocFile, builderFlags)
415 }
416
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700417 if library.relocationPacker.needsPacking(ctx) {
418 packedOutputFile := outputFile
419 outputFile = android.PathForModuleOut(ctx, "unpacked", fileName)
420 library.relocationPacker.pack(ctx, outputFile, packedOutputFile, builderFlags)
421 }
422
Colin Cross4d9c2d12016-07-29 12:48:20 -0700423 if library.stripper.needsStrip(ctx) {
424 strippedOutputFile := outputFile
425 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
426 library.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
427 }
428
429 sharedLibs := deps.SharedLibs
430 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
431
Dan Albertd015c4a2016-08-10 14:34:08 -0700432 // TODO(danalbert): Clean this up when soong supports prebuilts.
433 if strings.HasPrefix(ctx.selectedStl(), "ndk_libc++") {
434 libDir := getNdkStlLibDir(ctx, flags.Toolchain, "libc++")
435
436 if strings.HasSuffix(ctx.selectedStl(), "_shared") {
437 deps.StaticLibs = append(deps.StaticLibs,
438 libDir.Join(ctx, "libandroid_support.a"))
439 } else {
440 deps.StaticLibs = append(deps.StaticLibs,
441 libDir.Join(ctx, "libc++abi.a"),
442 libDir.Join(ctx, "libandroid_support.a"))
443 }
444
445 if ctx.Arch().ArchType == android.Arm {
446 deps.StaticLibs = append(deps.StaticLibs,
447 libDir.Join(ctx, "libunwind.a"))
448 }
449 }
450
Colin Cross26c34ed2016-09-30 17:10:16 -0700451 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
452 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
453
Colin Cross4d9c2d12016-07-29 12:48:20 -0700454 TransformObjToDynamicBinary(ctx, objFiles, sharedLibs,
455 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
456 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile)
457
458 return ret
459}
460
Colin Crossb916a382016-07-29 17:28:03 -0700461func (library *libraryDecorator) link(ctx ModuleContext,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700462 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
463
464 objFiles = append(objFiles, deps.ObjFiles...)
465
466 var out android.Path
467 if library.static() {
468 out = library.linkStatic(ctx, flags, deps, objFiles)
469 } else {
470 out = library.linkShared(ctx, flags, deps, objFiles)
471 }
472
473 library.exportIncludes(ctx, "-I")
474 library.reexportFlags(deps.ReexportedFlags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700475 library.reexportDeps(deps.ReexportedFlagsDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700476
477 return out
478}
479
Colin Crossb916a382016-07-29 17:28:03 -0700480func (library *libraryDecorator) buildStatic() bool {
481 return library.Properties.BuildStatic &&
Colin Cross4d9c2d12016-07-29 12:48:20 -0700482 (library.Properties.Static.Enabled == nil || *library.Properties.Static.Enabled)
483}
484
Colin Crossb916a382016-07-29 17:28:03 -0700485func (library *libraryDecorator) buildShared() bool {
486 return library.Properties.BuildShared &&
Colin Cross4d9c2d12016-07-29 12:48:20 -0700487 (library.Properties.Shared.Enabled == nil || *library.Properties.Shared.Enabled)
488}
489
Colin Crossb916a382016-07-29 17:28:03 -0700490func (library *libraryDecorator) getWholeStaticMissingDeps() []string {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700491 return library.wholeStaticMissingDeps
492}
493
Colin Crossb916a382016-07-29 17:28:03 -0700494func (library *libraryDecorator) objs() android.Paths {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700495 return library.objFiles
496}
497
Colin Crossb916a382016-07-29 17:28:03 -0700498func (library *libraryDecorator) reuseObjs() android.Paths {
499 return library.reuseObjFiles
Colin Cross4d9c2d12016-07-29 12:48:20 -0700500}
501
Colin Cross26c34ed2016-09-30 17:10:16 -0700502func (library *libraryDecorator) toc() android.OptionalPath {
503 return library.tocFile
504}
505
Colin Crossb916a382016-07-29 17:28:03 -0700506func (library *libraryDecorator) install(ctx ModuleContext, file android.Path) {
507 if !ctx.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700508 library.baseInstaller.install(ctx, file)
509 }
510}
511
Colin Crossb916a382016-07-29 17:28:03 -0700512func (library *libraryDecorator) static() bool {
513 return library.Properties.VariantIsStatic
Colin Cross4d9c2d12016-07-29 12:48:20 -0700514}
515
Colin Crossb916a382016-07-29 17:28:03 -0700516func (library *libraryDecorator) setStatic(static bool) {
517 library.Properties.VariantIsStatic = static
518}
519
520func NewLibrary(hod android.HostOrDeviceSupported, shared, static bool) (*Module, *libraryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700521 module := newModule(hod, android.MultilibBoth)
522
Colin Crossb916a382016-07-29 17:28:03 -0700523 library := &libraryDecorator{
524 Properties: LibraryProperties{
525 BuildShared: shared,
526 BuildStatic: static,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700527 },
Colin Crossb916a382016-07-29 17:28:03 -0700528 baseCompiler: NewBaseCompiler(),
529 baseLinker: NewBaseLinker(),
530 baseInstaller: NewBaseInstaller("lib", "lib64", InstallInSystem),
531 sanitize: module.sanitize,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700532 }
533
Colin Crossb916a382016-07-29 17:28:03 -0700534 module.compiler = library
535 module.linker = library
536 module.installer = library
537
538 return module, library
539}
540
541func linkageMutator(mctx android.BottomUpMutatorContext) {
542 if m, ok := mctx.Module().(*Module); ok && m.linker != nil {
543 if library, ok := m.linker.(libraryInterface); ok {
544 var modules []blueprint.Module
545 if library.buildStatic() && library.buildShared() {
546 modules = mctx.CreateLocalVariations("static", "shared")
547 static := modules[0].(*Module)
548 shared := modules[1].(*Module)
549
550 static.linker.(libraryInterface).setStatic(true)
551 shared.linker.(libraryInterface).setStatic(false)
552
553 if staticCompiler, ok := static.compiler.(*libraryDecorator); ok {
554 sharedCompiler := shared.compiler.(*libraryDecorator)
555 if len(staticCompiler.Properties.Static.Cflags) == 0 &&
556 len(sharedCompiler.Properties.Shared.Cflags) == 0 {
557 // Optimize out compiling common .o files twice for static+shared libraries
558 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
559 sharedCompiler.baseCompiler.Properties.Srcs = nil
560 sharedCompiler.baseCompiler.Properties.Generated_sources = nil
561 }
562 }
563 } else if library.buildStatic() {
564 modules = mctx.CreateLocalVariations("static")
565 modules[0].(*Module).linker.(libraryInterface).setStatic(true)
566 } else if library.buildShared() {
567 modules = mctx.CreateLocalVariations("shared")
568 modules[0].(*Module).linker.(libraryInterface).setStatic(false)
569 }
570 }
571 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700572}