blob: 5fb522a56b17979f516f48f10498fd8253d41d5f [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
Dan Willemsen273af7f2016-11-03 15:53:42 -070077 // be added to the include path (using -I) for this module and any module that links
78 // against this module
Colin Crossb916a382016-07-29 17:28:03 -070079 Export_include_dirs []string `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070080}
81
82func init() {
Colin Cross798bfce2016-10-12 14:28:16 -070083 android.RegisterModuleType("cc_library_static", libraryStaticFactory)
84 android.RegisterModuleType("cc_library_shared", librarySharedFactory)
85 android.RegisterModuleType("cc_library", libraryFactory)
86 android.RegisterModuleType("cc_library_host_static", libraryHostStaticFactory)
87 android.RegisterModuleType("cc_library_host_shared", libraryHostSharedFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -070088}
89
90// Module factory for combined static + shared libraries, device by default but with possible host
91// support
92func libraryFactory() (blueprint.Module, []interface{}) {
Colin Crossb916a382016-07-29 17:28:03 -070093 module, _ := NewLibrary(android.HostAndDeviceSupported, true, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -070094 return module.Init()
95}
96
97// Module factory for static libraries
98func libraryStaticFactory() (blueprint.Module, []interface{}) {
Colin Crossb916a382016-07-29 17:28:03 -070099 module, _ := NewLibrary(android.HostAndDeviceSupported, false, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700100 return module.Init()
101}
102
103// Module factory for shared libraries
104func librarySharedFactory() (blueprint.Module, []interface{}) {
Colin Crossb916a382016-07-29 17:28:03 -0700105 module, _ := NewLibrary(android.HostAndDeviceSupported, true, false)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700106 return module.Init()
107}
108
109// Module factory for host static libraries
110func libraryHostStaticFactory() (blueprint.Module, []interface{}) {
Colin Crossb916a382016-07-29 17:28:03 -0700111 module, _ := NewLibrary(android.HostSupported, false, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700112 return module.Init()
113}
114
115// Module factory for host shared libraries
116func libraryHostSharedFactory() (blueprint.Module, []interface{}) {
Colin Crossb916a382016-07-29 17:28:03 -0700117 module, _ := NewLibrary(android.HostSupported, true, false)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700118 return module.Init()
119}
120
121type flagExporter struct {
122 Properties FlagExporterProperties
123
Dan Willemsen847dcc72016-09-29 12:13:36 -0700124 flags []string
125 flagsDeps android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700126}
127
128func (f *flagExporter) exportIncludes(ctx ModuleContext, inc string) {
129 includeDirs := android.PathsForModuleSrc(ctx, f.Properties.Export_include_dirs)
130 for _, dir := range includeDirs.Strings() {
131 f.flags = append(f.flags, inc+dir)
132 }
133}
134
135func (f *flagExporter) reexportFlags(flags []string) {
136 f.flags = append(f.flags, flags...)
137}
138
Dan Willemsen847dcc72016-09-29 12:13:36 -0700139func (f *flagExporter) reexportDeps(deps android.Paths) {
140 f.flagsDeps = append(f.flagsDeps, deps...)
141}
142
Colin Cross4d9c2d12016-07-29 12:48:20 -0700143func (f *flagExporter) exportedFlags() []string {
144 return f.flags
145}
146
Dan Willemsen847dcc72016-09-29 12:13:36 -0700147func (f *flagExporter) exportedFlagsDeps() android.Paths {
148 return f.flagsDeps
149}
150
Colin Cross4d9c2d12016-07-29 12:48:20 -0700151type exportedFlagsProducer interface {
152 exportedFlags() []string
Dan Willemsen847dcc72016-09-29 12:13:36 -0700153 exportedFlagsDeps() android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700154}
155
156var _ exportedFlagsProducer = (*flagExporter)(nil)
157
Colin Crossb916a382016-07-29 17:28:03 -0700158// libraryDecorator wraps baseCompiler, baseLinker and baseInstaller to provide library-specific
159// functionality: static vs. shared linkage, reusing object files for shared libraries
160type libraryDecorator struct {
161 Properties LibraryProperties
Colin Cross4d9c2d12016-07-29 12:48:20 -0700162
163 // For reusing static library objects for shared library
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700164 reuseObjects Objects
Colin Cross26c34ed2016-09-30 17:10:16 -0700165 // table-of-contents file to optimize out relinking when possible
166 tocFile android.OptionalPath
Colin Cross4d9c2d12016-07-29 12:48:20 -0700167
Colin Cross4d9c2d12016-07-29 12:48:20 -0700168 flagExporter
169 stripper
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700170 relocationPacker
Colin Cross4d9c2d12016-07-29 12:48:20 -0700171
Colin Cross4d9c2d12016-07-29 12:48:20 -0700172 // If we're used as a whole_static_lib, our missing dependencies need
173 // to be given
174 wholeStaticMissingDeps []string
175
176 // For whole_static_libs
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700177 objects Objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700178
179 // Uses the module's name if empty, but can be overridden. Does not include
180 // shlib suffix.
181 libName string
Colin Crossb916a382016-07-29 17:28:03 -0700182
183 sanitize *sanitize
184
185 // Decorated interafaces
186 *baseCompiler
187 *baseLinker
188 *baseInstaller
Colin Cross4d9c2d12016-07-29 12:48:20 -0700189}
190
Colin Crossb916a382016-07-29 17:28:03 -0700191func (library *libraryDecorator) linkerProps() []interface{} {
192 var props []interface{}
193 props = append(props, library.baseLinker.linkerProps()...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700194 return append(props,
195 &library.Properties,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700196 &library.flagExporter.Properties,
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700197 &library.stripper.StripProperties,
198 &library.relocationPacker.Properties)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700199}
200
Colin Crossb916a382016-07-29 17:28:03 -0700201func (library *libraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross42742b82016-08-01 13:20:05 -0700202 flags = library.baseLinker.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700203
Colin Crossb916a382016-07-29 17:28:03 -0700204 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
205 // all code is position independent, and then those warnings get promoted to
206 // errors.
207 if ctx.Os() != android.Windows {
208 flags.CFlags = append(flags.CFlags, "-fPIC")
209 }
210
211 if library.static() {
212 flags.CFlags = append(flags.CFlags, library.Properties.Static.Cflags...)
213 } else {
214 flags.CFlags = append(flags.CFlags, library.Properties.Shared.Cflags...)
215 }
216
Colin Cross4d9c2d12016-07-29 12:48:20 -0700217 if !library.static() {
218 libName := library.getLibName(ctx)
219 // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead
220 sharedFlag := "-Wl,-shared"
221 if flags.Clang || ctx.Host() {
222 sharedFlag = "-shared"
223 }
224 var f []string
225 if ctx.Device() {
226 f = append(f,
227 "-nostdlib",
228 "-Wl,--gc-sections",
229 )
230 }
231
232 if ctx.Darwin() {
233 f = append(f,
234 "-dynamiclib",
235 "-single_module",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700236 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
237 )
Colin Cross7863cf52016-10-20 10:47:21 -0700238 if ctx.Arch().ArchType == android.X86 {
239 f = append(f,
240 "-read_only_relocs suppress",
241 )
242 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700243 } else {
244 f = append(f,
245 sharedFlag,
246 "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix())
247 }
248
249 flags.LdFlags = append(f, flags.LdFlags...)
250 }
251
252 return flags
253}
254
Dan Willemsen273af7f2016-11-03 15:53:42 -0700255func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
256 exportIncludeDirs := android.PathsForModuleSrc(ctx, library.flagExporter.Properties.Export_include_dirs)
257 if len(exportIncludeDirs) > 0 {
258 flags.GlobalFlags = append(flags.GlobalFlags, includeDirsToFlags(exportIncludeDirs))
259 }
260
261 return library.baseCompiler.compilerFlags(ctx, flags)
262}
263
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700264func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
265 objs := library.baseCompiler.compile(ctx, flags, deps)
266 library.reuseObjects = objs
Colin Cross2f336352016-10-26 10:03:47 -0700267 buildFlags := flagsToBuilderFlags(flags)
Colin Crossb916a382016-07-29 17:28:03 -0700268
Colin Cross4d9c2d12016-07-29 12:48:20 -0700269 if library.static() {
Colin Cross2f336352016-10-26 10:03:47 -0700270 srcs := android.PathsForModuleSrc(ctx, library.Properties.Static.Srcs)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700271 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceStaticLibrary,
272 srcs, library.baseCompiler.deps))
Colin Crossb916a382016-07-29 17:28:03 -0700273 } else {
Colin Cross2f336352016-10-26 10:03:47 -0700274 srcs := android.PathsForModuleSrc(ctx, library.Properties.Shared.Srcs)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700275 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceSharedLibrary,
276 srcs, library.baseCompiler.deps))
Colin Crossb916a382016-07-29 17:28:03 -0700277 }
278
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700279 return objs
Colin Crossb916a382016-07-29 17:28:03 -0700280}
281
282type libraryInterface interface {
283 getWholeStaticMissingDeps() []string
284 static() bool
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700285 objs() Objects
286 reuseObjs() Objects
Colin Cross26c34ed2016-09-30 17:10:16 -0700287 toc() android.OptionalPath
Colin Crossb916a382016-07-29 17:28:03 -0700288
289 // Returns true if the build options for the module have selected a static or shared build
290 buildStatic() bool
291 buildShared() bool
292
293 // Sets whether a specific variant is static or shared
294 setStatic(bool)
295}
296
297func (library *libraryDecorator) getLibName(ctx ModuleContext) string {
298 name := library.libName
299 if name == "" {
Colin Crossce75d2c2016-10-06 16:12:58 -0700300 name = ctx.baseModuleName()
Colin Crossb916a382016-07-29 17:28:03 -0700301 }
302
303 if ctx.Host() && Bool(library.Properties.Unique_host_soname) {
304 if !strings.HasSuffix(name, "-host") {
305 name = name + "-host"
306 }
307 }
308
309 return name + library.Properties.VariantName
310}
311
312func (library *libraryDecorator) linkerInit(ctx BaseModuleContext) {
313 location := InstallInSystem
314 if library.sanitize.inData() {
315 location = InstallInData
316 }
317 library.baseInstaller.location = location
318
319 library.baseLinker.linkerInit(ctx)
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700320
321 library.relocationPacker.packingInit(ctx)
Colin Crossb916a382016-07-29 17:28:03 -0700322}
323
324func (library *libraryDecorator) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
325 deps = library.baseLinker.linkerDeps(ctx, deps)
326
327 if library.static() {
328 deps.WholeStaticLibs = append(deps.WholeStaticLibs,
329 library.Properties.Static.Whole_static_libs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700330 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Static.Static_libs...)
331 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Static.Shared_libs...)
332 } else {
333 if ctx.Device() && !Bool(library.baseLinker.Properties.Nocrt) {
334 if !ctx.sdk() {
335 deps.CrtBegin = "crtbegin_so"
336 deps.CrtEnd = "crtend_so"
337 } else {
338 deps.CrtBegin = "ndk_crtbegin_so." + ctx.sdkVersion()
339 deps.CrtEnd = "ndk_crtend_so." + ctx.sdkVersion()
340 }
341 }
342 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Shared.Whole_static_libs...)
343 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Shared.Static_libs...)
344 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Shared.Shared_libs...)
345 }
346
347 return deps
348}
349
Colin Crossb916a382016-07-29 17:28:03 -0700350func (library *libraryDecorator) linkStatic(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700351 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700352
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700353 library.objects = deps.WholeStaticLibObjs.Copy()
354 library.objects = library.objects.Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700355
356 outputFile := android.PathForModuleOut(ctx,
357 ctx.ModuleName()+library.Properties.VariantName+staticLibraryExtension)
358
359 if ctx.Darwin() {
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700360 TransformDarwinObjToStaticLib(ctx, library.objects.objFiles, flagsToBuilderFlags(flags), outputFile, objs.tidyFiles)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700361 } else {
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700362 TransformObjToStaticLib(ctx, library.objects.objFiles, flagsToBuilderFlags(flags), outputFile, objs.tidyFiles)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700363 }
364
365 library.wholeStaticMissingDeps = ctx.GetMissingDependencies()
366
367 ctx.CheckbuildFile(outputFile)
368
369 return outputFile
370}
371
Colin Crossb916a382016-07-29 17:28:03 -0700372func (library *libraryDecorator) linkShared(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700373 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700374
375 var linkerDeps android.Paths
376
377 versionScript := android.OptionalPathForModuleSrc(ctx, library.Properties.Version_script)
378 unexportedSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Unexported_symbols_list)
379 forceNotWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_not_weak_list)
380 forceWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_weak_list)
381 if !ctx.Darwin() {
382 if versionScript.Valid() {
383 flags.LdFlags = append(flags.LdFlags, "-Wl,--version-script,"+versionScript.String())
384 linkerDeps = append(linkerDeps, versionScript.Path())
385 }
386 if unexportedSymbols.Valid() {
387 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
388 }
389 if forceNotWeakSymbols.Valid() {
390 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
391 }
392 if forceWeakSymbols.Valid() {
393 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
394 }
395 } else {
396 if versionScript.Valid() {
397 ctx.PropertyErrorf("version_script", "Not supported on Darwin")
398 }
399 if unexportedSymbols.Valid() {
400 flags.LdFlags = append(flags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
401 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
402 }
403 if forceNotWeakSymbols.Valid() {
404 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
405 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
406 }
407 if forceWeakSymbols.Valid() {
408 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
409 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
410 }
411 }
412
413 fileName := library.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
414 outputFile := android.PathForModuleOut(ctx, fileName)
415 ret := outputFile
416
417 builderFlags := flagsToBuilderFlags(flags)
418
Colin Cross89562dc2016-10-03 17:47:19 -0700419 if !ctx.Darwin() {
420 // Optimize out relinking against shared libraries whose interface hasn't changed by
421 // depending on a table of contents file instead of the library itself.
422 tocPath := outputFile.RelPathString()
423 tocPath = pathtools.ReplaceExtension(tocPath, flags.Toolchain.ShlibSuffix()[1:]+".toc")
424 tocFile := android.PathForOutput(ctx, tocPath)
425 library.tocFile = android.OptionalPathForPath(tocFile)
426 TransformSharedObjectToToc(ctx, outputFile, tocFile, builderFlags)
427 }
428
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700429 if library.relocationPacker.needsPacking(ctx) {
430 packedOutputFile := outputFile
431 outputFile = android.PathForModuleOut(ctx, "unpacked", fileName)
432 library.relocationPacker.pack(ctx, outputFile, packedOutputFile, builderFlags)
433 }
434
Colin Cross4d9c2d12016-07-29 12:48:20 -0700435 if library.stripper.needsStrip(ctx) {
436 strippedOutputFile := outputFile
437 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
438 library.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
439 }
440
441 sharedLibs := deps.SharedLibs
442 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
443
Dan Albertd015c4a2016-08-10 14:34:08 -0700444 // TODO(danalbert): Clean this up when soong supports prebuilts.
445 if strings.HasPrefix(ctx.selectedStl(), "ndk_libc++") {
446 libDir := getNdkStlLibDir(ctx, flags.Toolchain, "libc++")
447
448 if strings.HasSuffix(ctx.selectedStl(), "_shared") {
449 deps.StaticLibs = append(deps.StaticLibs,
450 libDir.Join(ctx, "libandroid_support.a"))
451 } else {
452 deps.StaticLibs = append(deps.StaticLibs,
453 libDir.Join(ctx, "libc++abi.a"),
454 libDir.Join(ctx, "libandroid_support.a"))
455 }
456
457 if ctx.Arch().ArchType == android.Arm {
458 deps.StaticLibs = append(deps.StaticLibs,
459 libDir.Join(ctx, "libunwind.a"))
460 }
461 }
462
Colin Cross26c34ed2016-09-30 17:10:16 -0700463 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
464 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700465 linkerDeps = append(linkerDeps, objs.tidyFiles...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700466
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700467 TransformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700468 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
469 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile)
470
471 return ret
472}
473
Colin Crossb916a382016-07-29 17:28:03 -0700474func (library *libraryDecorator) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700475 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700476
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700477 objs = objs.Append(deps.Objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700478
479 var out android.Path
480 if library.static() {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700481 out = library.linkStatic(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700482 } else {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700483 out = library.linkShared(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700484 }
485
486 library.exportIncludes(ctx, "-I")
487 library.reexportFlags(deps.ReexportedFlags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700488 library.reexportDeps(deps.ReexportedFlagsDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700489
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700490 if library.baseCompiler.hasSrcExt(".proto") {
Colin Cross0c461f12016-10-20 16:11:43 -0700491 if library.Properties.Proto.Export_proto_headers {
492 library.reexportFlags([]string{
493 "-I" + protoSubDir(ctx).String(),
494 "-I" + protoDir(ctx).String(),
495 })
496 library.reexportDeps(library.baseCompiler.deps) // TODO: restrict to proto deps
497 }
498 }
499
Colin Cross4d9c2d12016-07-29 12:48:20 -0700500 return out
501}
502
Colin Crossb916a382016-07-29 17:28:03 -0700503func (library *libraryDecorator) buildStatic() bool {
504 return library.Properties.BuildStatic &&
Colin Cross4d9c2d12016-07-29 12:48:20 -0700505 (library.Properties.Static.Enabled == nil || *library.Properties.Static.Enabled)
506}
507
Colin Crossb916a382016-07-29 17:28:03 -0700508func (library *libraryDecorator) buildShared() bool {
509 return library.Properties.BuildShared &&
Colin Cross4d9c2d12016-07-29 12:48:20 -0700510 (library.Properties.Shared.Enabled == nil || *library.Properties.Shared.Enabled)
511}
512
Colin Crossb916a382016-07-29 17:28:03 -0700513func (library *libraryDecorator) getWholeStaticMissingDeps() []string {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700514 return library.wholeStaticMissingDeps
515}
516
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700517func (library *libraryDecorator) objs() Objects {
518 return library.objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700519}
520
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700521func (library *libraryDecorator) reuseObjs() Objects {
522 return library.reuseObjects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700523}
524
Colin Cross26c34ed2016-09-30 17:10:16 -0700525func (library *libraryDecorator) toc() android.OptionalPath {
526 return library.tocFile
527}
528
Colin Crossb916a382016-07-29 17:28:03 -0700529func (library *libraryDecorator) install(ctx ModuleContext, file android.Path) {
530 if !ctx.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700531 library.baseInstaller.install(ctx, file)
532 }
533}
534
Colin Crossb916a382016-07-29 17:28:03 -0700535func (library *libraryDecorator) static() bool {
536 return library.Properties.VariantIsStatic
Colin Cross4d9c2d12016-07-29 12:48:20 -0700537}
538
Colin Crossb916a382016-07-29 17:28:03 -0700539func (library *libraryDecorator) setStatic(static bool) {
540 library.Properties.VariantIsStatic = static
541}
542
543func NewLibrary(hod android.HostOrDeviceSupported, shared, static bool) (*Module, *libraryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700544 module := newModule(hod, android.MultilibBoth)
545
Colin Crossb916a382016-07-29 17:28:03 -0700546 library := &libraryDecorator{
547 Properties: LibraryProperties{
548 BuildShared: shared,
549 BuildStatic: static,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700550 },
Colin Crossb916a382016-07-29 17:28:03 -0700551 baseCompiler: NewBaseCompiler(),
552 baseLinker: NewBaseLinker(),
553 baseInstaller: NewBaseInstaller("lib", "lib64", InstallInSystem),
554 sanitize: module.sanitize,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700555 }
556
Colin Crossb916a382016-07-29 17:28:03 -0700557 module.compiler = library
558 module.linker = library
559 module.installer = library
560
561 return module, library
562}
563
564func linkageMutator(mctx android.BottomUpMutatorContext) {
565 if m, ok := mctx.Module().(*Module); ok && m.linker != nil {
566 if library, ok := m.linker.(libraryInterface); ok {
567 var modules []blueprint.Module
568 if library.buildStatic() && library.buildShared() {
569 modules = mctx.CreateLocalVariations("static", "shared")
570 static := modules[0].(*Module)
571 shared := modules[1].(*Module)
572
573 static.linker.(libraryInterface).setStatic(true)
574 shared.linker.(libraryInterface).setStatic(false)
575
576 if staticCompiler, ok := static.compiler.(*libraryDecorator); ok {
577 sharedCompiler := shared.compiler.(*libraryDecorator)
578 if len(staticCompiler.Properties.Static.Cflags) == 0 &&
579 len(sharedCompiler.Properties.Shared.Cflags) == 0 {
580 // Optimize out compiling common .o files twice for static+shared libraries
581 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
582 sharedCompiler.baseCompiler.Properties.Srcs = nil
583 sharedCompiler.baseCompiler.Properties.Generated_sources = nil
584 }
585 }
586 } else if library.buildStatic() {
587 modules = mctx.CreateLocalVariations("static")
588 modules[0].(*Module).linker.(libraryInterface).setStatic(true)
589 } else if library.buildShared() {
590 modules = mctx.CreateLocalVariations("shared")
591 modules[0].(*Module).linker.(libraryInterface).setStatic(false)
592 }
593 }
594 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700595}