blob: 73efb9eabf49ecbd74f82e1e16472af82eaab4d8 [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
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700163 reuseObjects Objects
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
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700176 objects Objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700177
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
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700254func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
255 objs := library.baseCompiler.compile(ctx, flags, deps)
256 library.reuseObjects = objs
Colin Cross2f336352016-10-26 10:03:47 -0700257 buildFlags := flagsToBuilderFlags(flags)
Colin Crossb916a382016-07-29 17:28:03 -0700258
Colin Cross4d9c2d12016-07-29 12:48:20 -0700259 if library.static() {
Colin Cross2f336352016-10-26 10:03:47 -0700260 srcs := android.PathsForModuleSrc(ctx, library.Properties.Static.Srcs)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700261 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceStaticLibrary,
262 srcs, library.baseCompiler.deps))
Colin Crossb916a382016-07-29 17:28:03 -0700263 } else {
Colin Cross2f336352016-10-26 10:03:47 -0700264 srcs := android.PathsForModuleSrc(ctx, library.Properties.Shared.Srcs)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700265 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceSharedLibrary,
266 srcs, library.baseCompiler.deps))
Colin Crossb916a382016-07-29 17:28:03 -0700267 }
268
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700269 return objs
Colin Crossb916a382016-07-29 17:28:03 -0700270}
271
272type libraryInterface interface {
273 getWholeStaticMissingDeps() []string
274 static() bool
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700275 objs() Objects
276 reuseObjs() Objects
Colin Cross26c34ed2016-09-30 17:10:16 -0700277 toc() android.OptionalPath
Colin Crossb916a382016-07-29 17:28:03 -0700278
279 // Returns true if the build options for the module have selected a static or shared build
280 buildStatic() bool
281 buildShared() bool
282
283 // Sets whether a specific variant is static or shared
284 setStatic(bool)
285}
286
287func (library *libraryDecorator) getLibName(ctx ModuleContext) string {
288 name := library.libName
289 if name == "" {
Colin Crossce75d2c2016-10-06 16:12:58 -0700290 name = ctx.baseModuleName()
Colin Crossb916a382016-07-29 17:28:03 -0700291 }
292
293 if ctx.Host() && Bool(library.Properties.Unique_host_soname) {
294 if !strings.HasSuffix(name, "-host") {
295 name = name + "-host"
296 }
297 }
298
299 return name + library.Properties.VariantName
300}
301
302func (library *libraryDecorator) linkerInit(ctx BaseModuleContext) {
303 location := InstallInSystem
304 if library.sanitize.inData() {
305 location = InstallInData
306 }
307 library.baseInstaller.location = location
308
309 library.baseLinker.linkerInit(ctx)
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700310
311 library.relocationPacker.packingInit(ctx)
Colin Crossb916a382016-07-29 17:28:03 -0700312}
313
314func (library *libraryDecorator) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
315 deps = library.baseLinker.linkerDeps(ctx, deps)
316
317 if library.static() {
318 deps.WholeStaticLibs = append(deps.WholeStaticLibs,
319 library.Properties.Static.Whole_static_libs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700320 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Static.Static_libs...)
321 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Static.Shared_libs...)
322 } else {
323 if ctx.Device() && !Bool(library.baseLinker.Properties.Nocrt) {
324 if !ctx.sdk() {
325 deps.CrtBegin = "crtbegin_so"
326 deps.CrtEnd = "crtend_so"
327 } else {
328 deps.CrtBegin = "ndk_crtbegin_so." + ctx.sdkVersion()
329 deps.CrtEnd = "ndk_crtend_so." + ctx.sdkVersion()
330 }
331 }
332 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Shared.Whole_static_libs...)
333 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Shared.Static_libs...)
334 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Shared.Shared_libs...)
335 }
336
337 return deps
338}
339
Colin Crossb916a382016-07-29 17:28:03 -0700340func (library *libraryDecorator) linkStatic(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700341 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700342
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700343 library.objects = deps.WholeStaticLibObjs.Copy()
344 library.objects = library.objects.Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700345
346 outputFile := android.PathForModuleOut(ctx,
347 ctx.ModuleName()+library.Properties.VariantName+staticLibraryExtension)
348
349 if ctx.Darwin() {
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700350 TransformDarwinObjToStaticLib(ctx, library.objects.objFiles, flagsToBuilderFlags(flags), outputFile, objs.tidyFiles)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700351 } else {
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700352 TransformObjToStaticLib(ctx, library.objects.objFiles, flagsToBuilderFlags(flags), outputFile, objs.tidyFiles)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700353 }
354
355 library.wholeStaticMissingDeps = ctx.GetMissingDependencies()
356
357 ctx.CheckbuildFile(outputFile)
358
359 return outputFile
360}
361
Colin Crossb916a382016-07-29 17:28:03 -0700362func (library *libraryDecorator) linkShared(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700363 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700364
365 var linkerDeps android.Paths
366
367 versionScript := android.OptionalPathForModuleSrc(ctx, library.Properties.Version_script)
368 unexportedSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Unexported_symbols_list)
369 forceNotWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_not_weak_list)
370 forceWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_weak_list)
371 if !ctx.Darwin() {
372 if versionScript.Valid() {
373 flags.LdFlags = append(flags.LdFlags, "-Wl,--version-script,"+versionScript.String())
374 linkerDeps = append(linkerDeps, versionScript.Path())
375 }
376 if unexportedSymbols.Valid() {
377 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
378 }
379 if forceNotWeakSymbols.Valid() {
380 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
381 }
382 if forceWeakSymbols.Valid() {
383 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
384 }
385 } else {
386 if versionScript.Valid() {
387 ctx.PropertyErrorf("version_script", "Not supported on Darwin")
388 }
389 if unexportedSymbols.Valid() {
390 flags.LdFlags = append(flags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
391 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
392 }
393 if forceNotWeakSymbols.Valid() {
394 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
395 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
396 }
397 if forceWeakSymbols.Valid() {
398 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
399 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
400 }
401 }
402
403 fileName := library.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
404 outputFile := android.PathForModuleOut(ctx, fileName)
405 ret := outputFile
406
407 builderFlags := flagsToBuilderFlags(flags)
408
Colin Cross89562dc2016-10-03 17:47:19 -0700409 if !ctx.Darwin() {
410 // Optimize out relinking against shared libraries whose interface hasn't changed by
411 // depending on a table of contents file instead of the library itself.
412 tocPath := outputFile.RelPathString()
413 tocPath = pathtools.ReplaceExtension(tocPath, flags.Toolchain.ShlibSuffix()[1:]+".toc")
414 tocFile := android.PathForOutput(ctx, tocPath)
415 library.tocFile = android.OptionalPathForPath(tocFile)
416 TransformSharedObjectToToc(ctx, outputFile, tocFile, builderFlags)
417 }
418
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700419 if library.relocationPacker.needsPacking(ctx) {
420 packedOutputFile := outputFile
421 outputFile = android.PathForModuleOut(ctx, "unpacked", fileName)
422 library.relocationPacker.pack(ctx, outputFile, packedOutputFile, builderFlags)
423 }
424
Colin Cross4d9c2d12016-07-29 12:48:20 -0700425 if library.stripper.needsStrip(ctx) {
426 strippedOutputFile := outputFile
427 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
428 library.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
429 }
430
431 sharedLibs := deps.SharedLibs
432 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
433
Dan Albertd015c4a2016-08-10 14:34:08 -0700434 // TODO(danalbert): Clean this up when soong supports prebuilts.
435 if strings.HasPrefix(ctx.selectedStl(), "ndk_libc++") {
436 libDir := getNdkStlLibDir(ctx, flags.Toolchain, "libc++")
437
438 if strings.HasSuffix(ctx.selectedStl(), "_shared") {
439 deps.StaticLibs = append(deps.StaticLibs,
440 libDir.Join(ctx, "libandroid_support.a"))
441 } else {
442 deps.StaticLibs = append(deps.StaticLibs,
443 libDir.Join(ctx, "libc++abi.a"),
444 libDir.Join(ctx, "libandroid_support.a"))
445 }
446
447 if ctx.Arch().ArchType == android.Arm {
448 deps.StaticLibs = append(deps.StaticLibs,
449 libDir.Join(ctx, "libunwind.a"))
450 }
451 }
452
Colin Cross26c34ed2016-09-30 17:10:16 -0700453 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
454 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700455 linkerDeps = append(linkerDeps, objs.tidyFiles...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700456
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700457 TransformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700458 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
459 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile)
460
461 return ret
462}
463
Colin Crossb916a382016-07-29 17:28:03 -0700464func (library *libraryDecorator) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700465 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700466
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700467 objs = objs.Append(deps.Objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700468
469 var out android.Path
470 if library.static() {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700471 out = library.linkStatic(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700472 } else {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700473 out = library.linkShared(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700474 }
475
476 library.exportIncludes(ctx, "-I")
477 library.reexportFlags(deps.ReexportedFlags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700478 library.reexportDeps(deps.ReexportedFlagsDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700479
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700480 if library.baseCompiler.hasSrcExt(".proto") {
Colin Cross0c461f12016-10-20 16:11:43 -0700481 if library.Properties.Proto.Export_proto_headers {
482 library.reexportFlags([]string{
483 "-I" + protoSubDir(ctx).String(),
484 "-I" + protoDir(ctx).String(),
485 })
486 library.reexportDeps(library.baseCompiler.deps) // TODO: restrict to proto deps
487 }
488 }
489
Colin Cross4d9c2d12016-07-29 12:48:20 -0700490 return out
491}
492
Colin Crossb916a382016-07-29 17:28:03 -0700493func (library *libraryDecorator) buildStatic() bool {
494 return library.Properties.BuildStatic &&
Colin Cross4d9c2d12016-07-29 12:48:20 -0700495 (library.Properties.Static.Enabled == nil || *library.Properties.Static.Enabled)
496}
497
Colin Crossb916a382016-07-29 17:28:03 -0700498func (library *libraryDecorator) buildShared() bool {
499 return library.Properties.BuildShared &&
Colin Cross4d9c2d12016-07-29 12:48:20 -0700500 (library.Properties.Shared.Enabled == nil || *library.Properties.Shared.Enabled)
501}
502
Colin Crossb916a382016-07-29 17:28:03 -0700503func (library *libraryDecorator) getWholeStaticMissingDeps() []string {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700504 return library.wholeStaticMissingDeps
505}
506
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700507func (library *libraryDecorator) objs() Objects {
508 return library.objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700509}
510
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700511func (library *libraryDecorator) reuseObjs() Objects {
512 return library.reuseObjects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700513}
514
Colin Cross26c34ed2016-09-30 17:10:16 -0700515func (library *libraryDecorator) toc() android.OptionalPath {
516 return library.tocFile
517}
518
Colin Crossb916a382016-07-29 17:28:03 -0700519func (library *libraryDecorator) install(ctx ModuleContext, file android.Path) {
520 if !ctx.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700521 library.baseInstaller.install(ctx, file)
522 }
523}
524
Colin Crossb916a382016-07-29 17:28:03 -0700525func (library *libraryDecorator) static() bool {
526 return library.Properties.VariantIsStatic
Colin Cross4d9c2d12016-07-29 12:48:20 -0700527}
528
Colin Crossb916a382016-07-29 17:28:03 -0700529func (library *libraryDecorator) setStatic(static bool) {
530 library.Properties.VariantIsStatic = static
531}
532
533func NewLibrary(hod android.HostOrDeviceSupported, shared, static bool) (*Module, *libraryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700534 module := newModule(hod, android.MultilibBoth)
535
Colin Crossb916a382016-07-29 17:28:03 -0700536 library := &libraryDecorator{
537 Properties: LibraryProperties{
538 BuildShared: shared,
539 BuildStatic: static,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700540 },
Colin Crossb916a382016-07-29 17:28:03 -0700541 baseCompiler: NewBaseCompiler(),
542 baseLinker: NewBaseLinker(),
543 baseInstaller: NewBaseInstaller("lib", "lib64", InstallInSystem),
544 sanitize: module.sanitize,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700545 }
546
Colin Crossb916a382016-07-29 17:28:03 -0700547 module.compiler = library
548 module.linker = library
549 module.installer = library
550
551 return module, library
552}
553
554func linkageMutator(mctx android.BottomUpMutatorContext) {
555 if m, ok := mctx.Module().(*Module); ok && m.linker != nil {
556 if library, ok := m.linker.(libraryInterface); ok {
557 var modules []blueprint.Module
558 if library.buildStatic() && library.buildShared() {
559 modules = mctx.CreateLocalVariations("static", "shared")
560 static := modules[0].(*Module)
561 shared := modules[1].(*Module)
562
563 static.linker.(libraryInterface).setStatic(true)
564 shared.linker.(libraryInterface).setStatic(false)
565
566 if staticCompiler, ok := static.compiler.(*libraryDecorator); ok {
567 sharedCompiler := shared.compiler.(*libraryDecorator)
568 if len(staticCompiler.Properties.Static.Cflags) == 0 &&
569 len(sharedCompiler.Properties.Shared.Cflags) == 0 {
570 // Optimize out compiling common .o files twice for static+shared libraries
571 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
572 sharedCompiler.baseCompiler.Properties.Srcs = nil
573 sharedCompiler.baseCompiler.Properties.Generated_sources = nil
574 }
575 }
576 } else if library.buildStatic() {
577 modules = mctx.CreateLocalVariations("static")
578 modules[0].(*Module).linker.(libraryInterface).setStatic(true)
579 } else if library.buildShared() {
580 modules = mctx.CreateLocalVariations("shared")
581 modules[0].(*Module).linker.(libraryInterface).setStatic(false)
582 }
583 }
584 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700585}