blob: dff38c84c8bfffb9082756180fef0e0d181c2021 [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 {
Dan Albertebedf672016-11-08 15:06:22 -0800338 // TODO(danalbert): Add generation of crt objects.
339 // For `sdk_version: "current"`, we don't actually have a
340 // freshly generated set of CRT objects. Use the last stable
341 // version.
342 version := ctx.sdkVersion()
343 if version == "current" {
344 version = ctx.AConfig().PlatformSdkVersion()
345 }
346 deps.CrtBegin = "ndk_crtbegin_so." + version
347 deps.CrtEnd = "ndk_crtend_so." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700348 }
349 }
350 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Shared.Whole_static_libs...)
351 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Shared.Static_libs...)
352 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Shared.Shared_libs...)
353 }
354
355 return deps
356}
357
Colin Crossb916a382016-07-29 17:28:03 -0700358func (library *libraryDecorator) linkStatic(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700359 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700360
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700361 library.objects = deps.WholeStaticLibObjs.Copy()
362 library.objects = library.objects.Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700363
364 outputFile := android.PathForModuleOut(ctx,
365 ctx.ModuleName()+library.Properties.VariantName+staticLibraryExtension)
366
367 if ctx.Darwin() {
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700368 TransformDarwinObjToStaticLib(ctx, library.objects.objFiles, flagsToBuilderFlags(flags), outputFile, objs.tidyFiles)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700369 } else {
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700370 TransformObjToStaticLib(ctx, library.objects.objFiles, flagsToBuilderFlags(flags), outputFile, objs.tidyFiles)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700371 }
372
373 library.wholeStaticMissingDeps = ctx.GetMissingDependencies()
374
375 ctx.CheckbuildFile(outputFile)
376
377 return outputFile
378}
379
Colin Crossb916a382016-07-29 17:28:03 -0700380func (library *libraryDecorator) linkShared(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700381 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700382
383 var linkerDeps android.Paths
384
385 versionScript := android.OptionalPathForModuleSrc(ctx, library.Properties.Version_script)
386 unexportedSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Unexported_symbols_list)
387 forceNotWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_not_weak_list)
388 forceWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_weak_list)
389 if !ctx.Darwin() {
390 if versionScript.Valid() {
391 flags.LdFlags = append(flags.LdFlags, "-Wl,--version-script,"+versionScript.String())
392 linkerDeps = append(linkerDeps, versionScript.Path())
393 }
394 if unexportedSymbols.Valid() {
395 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
396 }
397 if forceNotWeakSymbols.Valid() {
398 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
399 }
400 if forceWeakSymbols.Valid() {
401 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
402 }
403 } else {
404 if versionScript.Valid() {
405 ctx.PropertyErrorf("version_script", "Not supported on Darwin")
406 }
407 if unexportedSymbols.Valid() {
408 flags.LdFlags = append(flags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
409 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
410 }
411 if forceNotWeakSymbols.Valid() {
412 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
413 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
414 }
415 if forceWeakSymbols.Valid() {
416 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
417 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
418 }
419 }
420
421 fileName := library.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
422 outputFile := android.PathForModuleOut(ctx, fileName)
423 ret := outputFile
424
425 builderFlags := flagsToBuilderFlags(flags)
426
Colin Cross89562dc2016-10-03 17:47:19 -0700427 if !ctx.Darwin() {
428 // Optimize out relinking against shared libraries whose interface hasn't changed by
429 // depending on a table of contents file instead of the library itself.
430 tocPath := outputFile.RelPathString()
431 tocPath = pathtools.ReplaceExtension(tocPath, flags.Toolchain.ShlibSuffix()[1:]+".toc")
432 tocFile := android.PathForOutput(ctx, tocPath)
433 library.tocFile = android.OptionalPathForPath(tocFile)
434 TransformSharedObjectToToc(ctx, outputFile, tocFile, builderFlags)
435 }
436
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700437 if library.relocationPacker.needsPacking(ctx) {
438 packedOutputFile := outputFile
439 outputFile = android.PathForModuleOut(ctx, "unpacked", fileName)
440 library.relocationPacker.pack(ctx, outputFile, packedOutputFile, builderFlags)
441 }
442
Colin Cross4d9c2d12016-07-29 12:48:20 -0700443 if library.stripper.needsStrip(ctx) {
444 strippedOutputFile := outputFile
445 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
446 library.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
447 }
448
449 sharedLibs := deps.SharedLibs
450 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
451
Dan Albertd015c4a2016-08-10 14:34:08 -0700452 // TODO(danalbert): Clean this up when soong supports prebuilts.
453 if strings.HasPrefix(ctx.selectedStl(), "ndk_libc++") {
454 libDir := getNdkStlLibDir(ctx, flags.Toolchain, "libc++")
455
456 if strings.HasSuffix(ctx.selectedStl(), "_shared") {
457 deps.StaticLibs = append(deps.StaticLibs,
458 libDir.Join(ctx, "libandroid_support.a"))
459 } else {
460 deps.StaticLibs = append(deps.StaticLibs,
461 libDir.Join(ctx, "libc++abi.a"),
462 libDir.Join(ctx, "libandroid_support.a"))
463 }
464
465 if ctx.Arch().ArchType == android.Arm {
466 deps.StaticLibs = append(deps.StaticLibs,
467 libDir.Join(ctx, "libunwind.a"))
468 }
469 }
470
Colin Cross26c34ed2016-09-30 17:10:16 -0700471 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
472 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700473 linkerDeps = append(linkerDeps, objs.tidyFiles...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700474
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700475 TransformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700476 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
477 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile)
478
479 return ret
480}
481
Colin Crossb916a382016-07-29 17:28:03 -0700482func (library *libraryDecorator) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700483 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700484
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700485 objs = objs.Append(deps.Objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700486
487 var out android.Path
488 if library.static() {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700489 out = library.linkStatic(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700490 } else {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700491 out = library.linkShared(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700492 }
493
494 library.exportIncludes(ctx, "-I")
495 library.reexportFlags(deps.ReexportedFlags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700496 library.reexportDeps(deps.ReexportedFlagsDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700497
Dan Willemsene1a3ce32016-11-02 20:44:08 -0700498 if library.baseCompiler.hasSrcExt(".proto") {
Colin Cross0c461f12016-10-20 16:11:43 -0700499 if library.Properties.Proto.Export_proto_headers {
500 library.reexportFlags([]string{
501 "-I" + protoSubDir(ctx).String(),
502 "-I" + protoDir(ctx).String(),
503 })
504 library.reexportDeps(library.baseCompiler.deps) // TODO: restrict to proto deps
505 }
506 }
507
Colin Cross4d9c2d12016-07-29 12:48:20 -0700508 return out
509}
510
Colin Crossb916a382016-07-29 17:28:03 -0700511func (library *libraryDecorator) buildStatic() bool {
512 return library.Properties.BuildStatic &&
Colin Cross4d9c2d12016-07-29 12:48:20 -0700513 (library.Properties.Static.Enabled == nil || *library.Properties.Static.Enabled)
514}
515
Colin Crossb916a382016-07-29 17:28:03 -0700516func (library *libraryDecorator) buildShared() bool {
517 return library.Properties.BuildShared &&
Colin Cross4d9c2d12016-07-29 12:48:20 -0700518 (library.Properties.Shared.Enabled == nil || *library.Properties.Shared.Enabled)
519}
520
Colin Crossb916a382016-07-29 17:28:03 -0700521func (library *libraryDecorator) getWholeStaticMissingDeps() []string {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700522 return library.wholeStaticMissingDeps
523}
524
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700525func (library *libraryDecorator) objs() Objects {
526 return library.objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700527}
528
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700529func (library *libraryDecorator) reuseObjs() Objects {
530 return library.reuseObjects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700531}
532
Colin Cross26c34ed2016-09-30 17:10:16 -0700533func (library *libraryDecorator) toc() android.OptionalPath {
534 return library.tocFile
535}
536
Colin Crossb916a382016-07-29 17:28:03 -0700537func (library *libraryDecorator) install(ctx ModuleContext, file android.Path) {
538 if !ctx.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700539 library.baseInstaller.install(ctx, file)
540 }
541}
542
Colin Crossb916a382016-07-29 17:28:03 -0700543func (library *libraryDecorator) static() bool {
544 return library.Properties.VariantIsStatic
Colin Cross4d9c2d12016-07-29 12:48:20 -0700545}
546
Colin Crossb916a382016-07-29 17:28:03 -0700547func (library *libraryDecorator) setStatic(static bool) {
548 library.Properties.VariantIsStatic = static
549}
550
551func NewLibrary(hod android.HostOrDeviceSupported, shared, static bool) (*Module, *libraryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700552 module := newModule(hod, android.MultilibBoth)
553
Colin Crossb916a382016-07-29 17:28:03 -0700554 library := &libraryDecorator{
555 Properties: LibraryProperties{
556 BuildShared: shared,
557 BuildStatic: static,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700558 },
Colin Crossb916a382016-07-29 17:28:03 -0700559 baseCompiler: NewBaseCompiler(),
560 baseLinker: NewBaseLinker(),
561 baseInstaller: NewBaseInstaller("lib", "lib64", InstallInSystem),
562 sanitize: module.sanitize,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700563 }
564
Colin Crossb916a382016-07-29 17:28:03 -0700565 module.compiler = library
566 module.linker = library
567 module.installer = library
568
569 return module, library
570}
571
572func linkageMutator(mctx android.BottomUpMutatorContext) {
573 if m, ok := mctx.Module().(*Module); ok && m.linker != nil {
574 if library, ok := m.linker.(libraryInterface); ok {
575 var modules []blueprint.Module
576 if library.buildStatic() && library.buildShared() {
577 modules = mctx.CreateLocalVariations("static", "shared")
578 static := modules[0].(*Module)
579 shared := modules[1].(*Module)
580
581 static.linker.(libraryInterface).setStatic(true)
582 shared.linker.(libraryInterface).setStatic(false)
583
584 if staticCompiler, ok := static.compiler.(*libraryDecorator); ok {
585 sharedCompiler := shared.compiler.(*libraryDecorator)
586 if len(staticCompiler.Properties.Static.Cflags) == 0 &&
587 len(sharedCompiler.Properties.Shared.Cflags) == 0 {
588 // Optimize out compiling common .o files twice for static+shared libraries
589 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
590 sharedCompiler.baseCompiler.Properties.Srcs = nil
591 sharedCompiler.baseCompiler.Properties.Generated_sources = nil
592 }
593 }
594 } else if library.buildStatic() {
595 modules = mctx.CreateLocalVariations("static")
596 modules[0].(*Module).linker.(libraryInterface).setStatic(true)
597 } else if library.buildShared() {
598 modules = mctx.CreateLocalVariations("shared")
599 modules[0].(*Module).linker.(libraryInterface).setStatic(false)
600 }
601 }
602 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700603}