2016-07-29 21:48:20 +02:00
|
|
|
// Copyright 2016 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package cc
|
|
|
|
|
2016-07-30 02:28:03 +02:00
|
|
|
import (
|
|
|
|
"android/soong/android"
|
|
|
|
"fmt"
|
2016-09-29 23:06:02 +02:00
|
|
|
|
|
|
|
"github.com/google/blueprint/proptools"
|
2016-07-30 02:28:03 +02:00
|
|
|
)
|
|
|
|
|
2016-07-29 21:48:20 +02:00
|
|
|
// This file contains the basic functionality for linking against static libraries and shared
|
|
|
|
// libraries. Final linking into libraries or executables is handled in library.go, binary.go, etc.
|
|
|
|
|
|
|
|
type BaseLinkerProperties struct {
|
|
|
|
// list of modules whose object files should be linked into this module
|
|
|
|
// in their entirety. For static library modules, all of the .o files from the intermediate
|
|
|
|
// directory of the dependency will be linked into this modules .a file. For a shared library,
|
|
|
|
// the dependency's .a file will be linked into this module using -Wl,--whole-archive.
|
|
|
|
Whole_static_libs []string `android:"arch_variant,variant_prepend"`
|
|
|
|
|
|
|
|
// list of modules that should be statically linked into this module.
|
|
|
|
Static_libs []string `android:"arch_variant,variant_prepend"`
|
|
|
|
|
|
|
|
// list of modules that should be dynamically linked into this module.
|
|
|
|
Shared_libs []string `android:"arch_variant"`
|
|
|
|
|
2016-12-13 21:50:57 +01:00
|
|
|
// list of modules that should only provide headers for this module.
|
|
|
|
Header_libs []string `android:"arch_variant,variant_prepend"`
|
|
|
|
|
2016-07-29 21:48:20 +02:00
|
|
|
// list of module-specific flags that will be used for all link steps
|
|
|
|
Ldflags []string `android:"arch_variant"`
|
|
|
|
|
|
|
|
// list of system libraries that will be dynamically linked to
|
2017-08-19 01:52:25 +02:00
|
|
|
// shared library and executable modules. If unset, generally defaults to libc,
|
|
|
|
// libm, and libdl. Set to [] to prevent linking against the defaults.
|
2016-07-29 21:48:20 +02:00
|
|
|
System_shared_libs []string
|
|
|
|
|
|
|
|
// allow the module to contain undefined symbols. By default,
|
|
|
|
// modules cannot contain undefined symbols that are not satisified by their immediate
|
|
|
|
// dependencies. Set this flag to true to remove --no-undefined from the linker flags.
|
|
|
|
// This flag should only be necessary for compiling low-level libraries like libc.
|
2016-12-08 18:45:21 +01:00
|
|
|
Allow_undefined_symbols *bool `android:"arch_variant"`
|
2016-07-29 21:48:20 +02:00
|
|
|
|
|
|
|
// don't link in libgcc.a
|
|
|
|
No_libgcc *bool
|
|
|
|
|
|
|
|
// -l arguments to pass to linker for host-provided shared libraries
|
|
|
|
Host_ldlibs []string `android:"arch_variant"`
|
|
|
|
|
|
|
|
// list of shared libraries to re-export include directories from. Entries must be
|
|
|
|
// present in shared_libs.
|
|
|
|
Export_shared_lib_headers []string `android:"arch_variant"`
|
|
|
|
|
|
|
|
// list of static libraries to re-export include directories from. Entries must be
|
|
|
|
// present in static_libs.
|
|
|
|
Export_static_lib_headers []string `android:"arch_variant"`
|
|
|
|
|
2016-12-13 21:50:57 +01:00
|
|
|
// list of header libraries to re-export include directories from. Entries must be
|
|
|
|
// present in header_libs.
|
|
|
|
Export_header_lib_headers []string `android:"arch_variant"`
|
|
|
|
|
2016-09-29 02:34:58 +02:00
|
|
|
// list of generated headers to re-export include directories from. Entries must be
|
|
|
|
// present in generated_headers.
|
|
|
|
Export_generated_headers []string `android:"arch_variant"`
|
|
|
|
|
2016-07-29 21:48:20 +02:00
|
|
|
// don't link in crt_begin and crt_end. This flag should only be necessary for
|
|
|
|
// compiling crt or libc.
|
|
|
|
Nocrt *bool `android:"arch_variant"`
|
2016-12-01 23:45:23 +01:00
|
|
|
|
|
|
|
// group static libraries. This can resolve missing symbols issues with interdependencies
|
|
|
|
// between static libraries, but it is generally better to order them correctly instead.
|
|
|
|
Group_static_libs *bool `android:"arch_variant"`
|
2017-06-16 05:03:55 +02:00
|
|
|
|
|
|
|
Target struct {
|
|
|
|
Vendor struct {
|
|
|
|
// list of shared libs that should not be used to build
|
|
|
|
// the vendor variant of the C/C++ module.
|
|
|
|
Exclude_shared_libs []string
|
|
|
|
}
|
|
|
|
}
|
2016-07-29 21:48:20 +02:00
|
|
|
}
|
|
|
|
|
2016-07-30 02:28:03 +02:00
|
|
|
func NewBaseLinker() *baseLinker {
|
|
|
|
return &baseLinker{}
|
|
|
|
}
|
|
|
|
|
2016-07-29 21:48:20 +02:00
|
|
|
// baseLinker provides support for shared_libs, static_libs, and whole_static_libs properties
|
|
|
|
type baseLinker struct {
|
|
|
|
Properties BaseLinkerProperties
|
|
|
|
dynamicProperties struct {
|
2016-07-30 02:28:03 +02:00
|
|
|
RunPaths []string `blueprint:"mutated"`
|
2016-07-29 21:48:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (linker *baseLinker) appendLdflags(flags []string) {
|
|
|
|
linker.Properties.Ldflags = append(linker.Properties.Ldflags, flags...)
|
|
|
|
}
|
|
|
|
|
2016-08-01 22:20:05 +02:00
|
|
|
func (linker *baseLinker) linkerInit(ctx BaseModuleContext) {
|
2016-07-29 21:48:20 +02:00
|
|
|
if ctx.toolchain().Is64Bit() {
|
2016-07-30 02:28:03 +02:00
|
|
|
linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, "../lib64", "lib64")
|
2016-07-29 21:48:20 +02:00
|
|
|
} else {
|
2016-07-30 02:28:03 +02:00
|
|
|
linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, "../lib", "lib")
|
2016-07-29 21:48:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-01 22:20:05 +02:00
|
|
|
func (linker *baseLinker) linkerProps() []interface{} {
|
2016-07-29 21:48:20 +02:00
|
|
|
return []interface{}{&linker.Properties, &linker.dynamicProperties}
|
|
|
|
}
|
|
|
|
|
2016-08-01 22:20:05 +02:00
|
|
|
func (linker *baseLinker) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
|
2016-07-29 21:48:20 +02:00
|
|
|
deps.WholeStaticLibs = append(deps.WholeStaticLibs, linker.Properties.Whole_static_libs...)
|
2016-12-13 21:50:57 +01:00
|
|
|
deps.HeaderLibs = append(deps.HeaderLibs, linker.Properties.Header_libs...)
|
2016-07-29 21:48:20 +02:00
|
|
|
deps.StaticLibs = append(deps.StaticLibs, linker.Properties.Static_libs...)
|
|
|
|
deps.SharedLibs = append(deps.SharedLibs, linker.Properties.Shared_libs...)
|
|
|
|
|
2016-12-13 21:50:57 +01:00
|
|
|
deps.ReexportHeaderLibHeaders = append(deps.ReexportHeaderLibHeaders, linker.Properties.Export_header_lib_headers...)
|
2016-07-29 21:48:20 +02:00
|
|
|
deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, linker.Properties.Export_static_lib_headers...)
|
|
|
|
deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, linker.Properties.Export_shared_lib_headers...)
|
2016-09-29 02:34:58 +02:00
|
|
|
deps.ReexportGeneratedHeaders = append(deps.ReexportGeneratedHeaders, linker.Properties.Export_generated_headers...)
|
2016-07-29 21:48:20 +02:00
|
|
|
|
2017-08-09 17:48:06 +02:00
|
|
|
if ctx.vndk() {
|
|
|
|
deps.SharedLibs = removeListFromList(deps.SharedLibs, linker.Properties.Target.Vendor.Exclude_shared_libs)
|
|
|
|
deps.ReexportSharedLibHeaders = removeListFromList(deps.ReexportSharedLibHeaders, linker.Properties.Target.Vendor.Exclude_shared_libs)
|
|
|
|
}
|
|
|
|
|
2016-09-15 01:47:18 +02:00
|
|
|
if ctx.ModuleName() != "libcompiler_rt-extras" {
|
2016-07-29 21:48:20 +02:00
|
|
|
deps.LateStaticLibs = append(deps.LateStaticLibs, "libcompiler_rt-extras")
|
|
|
|
}
|
|
|
|
|
2016-11-17 10:02:25 +01:00
|
|
|
if ctx.toolchain().Bionic() {
|
2016-07-29 21:48:20 +02:00
|
|
|
// libgcc and libatomic have to be last on the command line
|
|
|
|
deps.LateStaticLibs = append(deps.LateStaticLibs, "libatomic")
|
|
|
|
if !Bool(linker.Properties.No_libgcc) {
|
|
|
|
deps.LateStaticLibs = append(deps.LateStaticLibs, "libgcc")
|
|
|
|
}
|
|
|
|
|
2016-07-30 02:28:03 +02:00
|
|
|
if !ctx.static() {
|
2017-08-19 01:52:25 +02:00
|
|
|
systemSharedLibs := linker.Properties.System_shared_libs
|
|
|
|
if systemSharedLibs == nil {
|
|
|
|
systemSharedLibs = []string{"libc", "libm", "libdl"}
|
2017-06-27 03:13:56 +02:00
|
|
|
}
|
|
|
|
|
2017-08-19 01:52:25 +02:00
|
|
|
if inList("libdl", deps.SharedLibs) {
|
|
|
|
// If system_shared_libs has libc but not libdl, make sure shared_libs does not
|
|
|
|
// have libdl to avoid loading libdl before libc.
|
|
|
|
if inList("libc", systemSharedLibs) {
|
|
|
|
if !inList("libdl", systemSharedLibs) {
|
|
|
|
ctx.PropertyErrorf("shared_libs",
|
|
|
|
"libdl must be in system_shared_libs, not shared_libs")
|
|
|
|
}
|
|
|
|
_, deps.SharedLibs = removeFromList("libdl", deps.SharedLibs)
|
2017-06-27 03:13:56 +02:00
|
|
|
}
|
2016-07-29 21:48:20 +02:00
|
|
|
}
|
|
|
|
|
2017-08-19 01:52:25 +02:00
|
|
|
// If libc and libdl are both in system_shared_libs make sure libd comes after libc
|
|
|
|
// to avoid loading libdl before libc.
|
|
|
|
if inList("libdl", systemSharedLibs) && inList("libc", systemSharedLibs) &&
|
|
|
|
indexList("libdl", systemSharedLibs) < indexList("libc", systemSharedLibs) {
|
|
|
|
ctx.PropertyErrorf("system_shared_libs", "libdl must be after libc")
|
|
|
|
}
|
|
|
|
|
|
|
|
deps.LateSharedLibs = append(deps.LateSharedLibs, systemSharedLibs...)
|
|
|
|
} else if ctx.sdk() || ctx.vndk() {
|
2017-06-27 03:13:56 +02:00
|
|
|
deps.LateSharedLibs = append(deps.LateSharedLibs, "libc", "libm", "libdl")
|
Split /system and /vendor modules, allow multi-installation
Nothing changes if BOARD_VNDK_VERSION isn't set.
When the VNDK is enabled (BOARD_VNDK_VERSION in Make), this will split
/system and /vendor modules into two different variant spaces that can't
link to each other. There are a few interfaces between the two variant
spaces:
The `llndk_library` stubs will be available in the /vendor variant, but
won't be installed, so at runtime the /system variant will be used.
Setting `vendor_available: true` will split a module into both variants.
The /system (or "core") variant will compile just like today. The
/vendor ("vendor") variant will compile against everything else in the
vendor space (so LL-NDK instead of libc/liblog/etc). There will be two
copies of these libraries installed onto the final device.
Since the available runtime interfaces for vendor modules may be
reduced, and your dependencies may not expose their private interfaces,
we allow the vendor variants to reduce their compilation set, and export
a different set of headers:
cc_library {
name: "libfoo",
srcs: ["common.cpp", "private_impl.cpp"],
export_include_dirs: ["include"],
target: {
vendor: {
export_include_dirs: ["include_vndk"],
exclude_srcs: ["private_impl.cpp"],
srcs: ["vendor_only.cpp"],
},
},
}
So the "core" variant would compile with both "common.cpp" and
"private_impl.cpp", and export "include".
The "vendor" variant would compile "common.cpp" and "vendor_only.cpp",
and export "include_vndk".
Bug: 36426473
Bug: 36079834
Test: out/soong/build.ninja, out/soong/Android- only changes due to _core addition and
.llndk -> .vendor
Test: attempt to compile with BOARD_VNDK_VERSION:=current
Change-Id: Idef28764043bf6c33dc0d2e7e2026c38867ff769
2017-04-06 21:43:22 +02:00
|
|
|
}
|
2016-07-29 21:48:20 +02:00
|
|
|
}
|
|
|
|
|
2017-04-04 21:59:48 +02:00
|
|
|
if ctx.Windows() {
|
2017-02-24 02:52:24 +01:00
|
|
|
deps.LateStaticLibs = append(deps.LateStaticLibs, "libwinpthread")
|
|
|
|
}
|
|
|
|
|
2016-07-29 21:48:20 +02:00
|
|
|
return deps
|
|
|
|
}
|
|
|
|
|
2016-08-01 22:20:05 +02:00
|
|
|
func (linker *baseLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags {
|
2016-07-29 21:48:20 +02:00
|
|
|
toolchain := ctx.toolchain()
|
|
|
|
|
|
|
|
if !ctx.noDefaultCompilerFlags() {
|
2016-10-20 21:41:14 +02:00
|
|
|
if Bool(linker.Properties.Allow_undefined_symbols) {
|
|
|
|
if ctx.Darwin() {
|
|
|
|
// darwin defaults to treating undefined symbols as errors
|
|
|
|
flags.LdFlags = append(flags.LdFlags, "-Wl,-undefined,dynamic_lookup")
|
|
|
|
}
|
|
|
|
} else if !ctx.Darwin() {
|
2016-07-29 21:48:20 +02:00
|
|
|
flags.LdFlags = append(flags.LdFlags, "-Wl,--no-undefined")
|
|
|
|
}
|
|
|
|
|
|
|
|
if flags.Clang {
|
|
|
|
flags.LdFlags = append(flags.LdFlags, toolchain.ClangLdflags())
|
|
|
|
} else {
|
|
|
|
flags.LdFlags = append(flags.LdFlags, toolchain.Ldflags())
|
|
|
|
}
|
|
|
|
|
2016-11-17 10:02:25 +01:00
|
|
|
if !ctx.toolchain().Bionic() {
|
2016-07-29 21:48:20 +02:00
|
|
|
CheckBadHostLdlibs(ctx, "host_ldlibs", linker.Properties.Host_ldlibs)
|
|
|
|
|
|
|
|
flags.LdFlags = append(flags.LdFlags, linker.Properties.Host_ldlibs...)
|
2017-09-08 21:45:18 +02:00
|
|
|
|
|
|
|
if !ctx.Windows() {
|
|
|
|
// Add -ldl, -lpthread and -lrt to host builds to match the default behavior of device
|
|
|
|
// builds
|
|
|
|
flags.LdFlags = append(flags.LdFlags,
|
|
|
|
"-ldl",
|
|
|
|
"-lpthread",
|
|
|
|
)
|
|
|
|
if !ctx.Darwin() {
|
|
|
|
flags.LdFlags = append(flags.LdFlags, "-lrt")
|
|
|
|
}
|
|
|
|
}
|
2016-07-29 21:48:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CheckBadLinkerFlags(ctx, "ldflags", linker.Properties.Ldflags)
|
|
|
|
|
2016-09-29 23:06:02 +02:00
|
|
|
flags.LdFlags = append(flags.LdFlags, proptools.NinjaAndShellEscape(linker.Properties.Ldflags)...)
|
2016-07-29 21:48:20 +02:00
|
|
|
|
2016-07-30 02:28:03 +02:00
|
|
|
if ctx.Host() {
|
2016-07-29 21:48:20 +02:00
|
|
|
rpath_prefix := `\$$ORIGIN/`
|
|
|
|
if ctx.Darwin() {
|
|
|
|
rpath_prefix = "@loader_path/"
|
|
|
|
}
|
|
|
|
|
2017-09-19 07:45:15 +02:00
|
|
|
if !ctx.static() {
|
|
|
|
for _, rpath := range linker.dynamicProperties.RunPaths {
|
|
|
|
flags.LdFlags = append(flags.LdFlags, "-Wl,-rpath,"+rpath_prefix+rpath)
|
|
|
|
}
|
2016-07-29 21:48:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-11 22:23:57 +02:00
|
|
|
if ctx.sdk() && (ctx.Arch().ArchType != android.Mips && ctx.Arch().ArchType != android.Mips64) {
|
|
|
|
// The bionic linker now has support gnu style hashes (which are much faster!), but shipping
|
|
|
|
// to older devices requires the old style hash. Fortunately, we can build with both and
|
|
|
|
// it'll work anywhere.
|
|
|
|
// This is not currently supported on MIPS architectures.
|
|
|
|
flags.LdFlags = append(flags.LdFlags, "-Wl,--hash-style=both")
|
|
|
|
}
|
|
|
|
|
2016-07-29 21:48:20 +02:00
|
|
|
if flags.Clang {
|
|
|
|
flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainClangLdflags())
|
|
|
|
} else {
|
|
|
|
flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainLdflags())
|
|
|
|
}
|
|
|
|
|
2016-12-01 23:45:23 +01:00
|
|
|
if Bool(linker.Properties.Group_static_libs) {
|
|
|
|
flags.GroupStaticLibs = true
|
|
|
|
}
|
|
|
|
|
2016-07-29 21:48:20 +02:00
|
|
|
return flags
|
|
|
|
}
|
|
|
|
|
2016-07-30 02:28:03 +02:00
|
|
|
func (linker *baseLinker) link(ctx ModuleContext,
|
2016-09-27 02:33:01 +02:00
|
|
|
flags Flags, deps PathDeps, objs Objects) android.Path {
|
2016-07-30 02:28:03 +02:00
|
|
|
panic(fmt.Errorf("baseLinker doesn't know how to link"))
|
2016-07-29 21:48:20 +02:00
|
|
|
}
|