blob: dac51d6dd9b27d4f231c7bfda4f8801feffdfc53 [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
Colin Crossb916a382016-07-29 17:28:03 -070017import (
18 "android/soong/android"
19 "fmt"
Colin Cross4b963f82016-09-29 14:06:02 -070020
21 "github.com/google/blueprint/proptools"
Colin Crossb916a382016-07-29 17:28:03 -070022)
23
Colin Cross4d9c2d12016-07-29 12:48:20 -070024// This file contains the basic functionality for linking against static libraries and shared
25// libraries. Final linking into libraries or executables is handled in library.go, binary.go, etc.
26
27type BaseLinkerProperties struct {
28 // list of modules whose object files should be linked into this module
29 // in their entirety. For static library modules, all of the .o files from the intermediate
30 // directory of the dependency will be linked into this modules .a file. For a shared library,
31 // the dependency's .a file will be linked into this module using -Wl,--whole-archive.
32 Whole_static_libs []string `android:"arch_variant,variant_prepend"`
33
34 // list of modules that should be statically linked into this module.
35 Static_libs []string `android:"arch_variant,variant_prepend"`
36
37 // list of modules that should be dynamically linked into this module.
38 Shared_libs []string `android:"arch_variant"`
39
40 // list of module-specific flags that will be used for all link steps
41 Ldflags []string `android:"arch_variant"`
42
43 // don't insert default compiler flags into asflags, cflags,
44 // cppflags, conlyflags, ldflags, or include_dirs
45 No_default_compiler_flags *bool
46
47 // list of system libraries that will be dynamically linked to
48 // shared library and executable modules. If unset, generally defaults to libc
49 // and libm. Set to [] to prevent linking against libc and libm.
50 System_shared_libs []string
51
52 // allow the module to contain undefined symbols. By default,
53 // modules cannot contain undefined symbols that are not satisified by their immediate
54 // dependencies. Set this flag to true to remove --no-undefined from the linker flags.
55 // This flag should only be necessary for compiling low-level libraries like libc.
56 Allow_undefined_symbols *bool
57
58 // don't link in libgcc.a
59 No_libgcc *bool
60
61 // -l arguments to pass to linker for host-provided shared libraries
62 Host_ldlibs []string `android:"arch_variant"`
63
64 // list of shared libraries to re-export include directories from. Entries must be
65 // present in shared_libs.
66 Export_shared_lib_headers []string `android:"arch_variant"`
67
68 // list of static libraries to re-export include directories from. Entries must be
69 // present in static_libs.
70 Export_static_lib_headers []string `android:"arch_variant"`
71
Dan Willemsenb3454ab2016-09-28 17:34:58 -070072 // list of generated headers to re-export include directories from. Entries must be
73 // present in generated_headers.
74 Export_generated_headers []string `android:"arch_variant"`
75
Colin Cross4d9c2d12016-07-29 12:48:20 -070076 // don't link in crt_begin and crt_end. This flag should only be necessary for
77 // compiling crt or libc.
78 Nocrt *bool `android:"arch_variant"`
Colin Cross18c0c5a2016-12-01 14:45:23 -080079
80 // group static libraries. This can resolve missing symbols issues with interdependencies
81 // between static libraries, but it is generally better to order them correctly instead.
82 Group_static_libs *bool `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070083}
84
Colin Crossb916a382016-07-29 17:28:03 -070085func NewBaseLinker() *baseLinker {
86 return &baseLinker{}
87}
88
Colin Cross4d9c2d12016-07-29 12:48:20 -070089// baseLinker provides support for shared_libs, static_libs, and whole_static_libs properties
90type baseLinker struct {
91 Properties BaseLinkerProperties
92 dynamicProperties struct {
Colin Crossb916a382016-07-29 17:28:03 -070093 RunPaths []string `blueprint:"mutated"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070094 }
95}
96
97func (linker *baseLinker) appendLdflags(flags []string) {
98 linker.Properties.Ldflags = append(linker.Properties.Ldflags, flags...)
99}
100
Colin Cross42742b82016-08-01 13:20:05 -0700101func (linker *baseLinker) linkerInit(ctx BaseModuleContext) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700102 if ctx.toolchain().Is64Bit() {
Colin Crossb916a382016-07-29 17:28:03 -0700103 linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, "../lib64", "lib64")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700104 } else {
Colin Crossb916a382016-07-29 17:28:03 -0700105 linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, "../lib", "lib")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700106 }
107}
108
Colin Cross42742b82016-08-01 13:20:05 -0700109func (linker *baseLinker) linkerProps() []interface{} {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700110 return []interface{}{&linker.Properties, &linker.dynamicProperties}
111}
112
Colin Cross42742b82016-08-01 13:20:05 -0700113func (linker *baseLinker) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700114 deps.WholeStaticLibs = append(deps.WholeStaticLibs, linker.Properties.Whole_static_libs...)
115 deps.StaticLibs = append(deps.StaticLibs, linker.Properties.Static_libs...)
116 deps.SharedLibs = append(deps.SharedLibs, linker.Properties.Shared_libs...)
117
118 deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, linker.Properties.Export_static_lib_headers...)
119 deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, linker.Properties.Export_shared_lib_headers...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700120 deps.ReexportGeneratedHeaders = append(deps.ReexportGeneratedHeaders, linker.Properties.Export_generated_headers...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700121
Dan Albert83705c82016-09-14 16:47:18 -0700122 if ctx.ModuleName() != "libcompiler_rt-extras" {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700123 deps.LateStaticLibs = append(deps.LateStaticLibs, "libcompiler_rt-extras")
124 }
125
Dan Willemsen2e47b342016-11-17 01:02:25 -0800126 if ctx.toolchain().Bionic() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700127 // libgcc and libatomic have to be last on the command line
128 deps.LateStaticLibs = append(deps.LateStaticLibs, "libatomic")
129 if !Bool(linker.Properties.No_libgcc) {
130 deps.LateStaticLibs = append(deps.LateStaticLibs, "libgcc")
131 }
132
Colin Crossb916a382016-07-29 17:28:03 -0700133 if !ctx.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700134 if linker.Properties.System_shared_libs != nil {
135 deps.LateSharedLibs = append(deps.LateSharedLibs,
136 linker.Properties.System_shared_libs...)
137 } else if !ctx.sdk() {
138 deps.LateSharedLibs = append(deps.LateSharedLibs, "libc", "libm")
139 }
140 }
141
142 if ctx.sdk() {
143 deps.SharedLibs = append(deps.SharedLibs,
144 "libc",
145 "libm",
146 )
147 }
148 }
149
150 return deps
151}
152
Colin Cross42742b82016-08-01 13:20:05 -0700153func (linker *baseLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700154 toolchain := ctx.toolchain()
155
Colin Cross4d9c2d12016-07-29 12:48:20 -0700156 if !ctx.noDefaultCompilerFlags() {
Colin Cross46974e22016-10-20 12:41:14 -0700157 if Bool(linker.Properties.Allow_undefined_symbols) {
158 if ctx.Darwin() {
159 // darwin defaults to treating undefined symbols as errors
160 flags.LdFlags = append(flags.LdFlags, "-Wl,-undefined,dynamic_lookup")
161 }
162 } else if !ctx.Darwin() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700163 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-undefined")
164 }
165
166 if flags.Clang {
167 flags.LdFlags = append(flags.LdFlags, toolchain.ClangLdflags())
168 } else {
169 flags.LdFlags = append(flags.LdFlags, toolchain.Ldflags())
170 }
171
Dan Willemsen2e47b342016-11-17 01:02:25 -0800172 if !ctx.toolchain().Bionic() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700173 CheckBadHostLdlibs(ctx, "host_ldlibs", linker.Properties.Host_ldlibs)
174
175 flags.LdFlags = append(flags.LdFlags, linker.Properties.Host_ldlibs...)
176 }
177 }
178
179 CheckBadLinkerFlags(ctx, "ldflags", linker.Properties.Ldflags)
180
Colin Cross4b963f82016-09-29 14:06:02 -0700181 flags.LdFlags = append(flags.LdFlags, proptools.NinjaAndShellEscape(linker.Properties.Ldflags)...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700182
Colin Crossb916a382016-07-29 17:28:03 -0700183 if ctx.Host() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700184 rpath_prefix := `\$$ORIGIN/`
185 if ctx.Darwin() {
186 rpath_prefix = "@loader_path/"
187 }
188
189 for _, rpath := range linker.dynamicProperties.RunPaths {
190 flags.LdFlags = append(flags.LdFlags, "-Wl,-rpath,"+rpath_prefix+rpath)
191 }
192 }
193
194 if flags.Clang {
195 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainClangLdflags())
196 } else {
197 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainLdflags())
198 }
199
Colin Cross18c0c5a2016-12-01 14:45:23 -0800200 if Bool(linker.Properties.Group_static_libs) {
201 flags.GroupStaticLibs = true
202 }
203
Colin Cross4d9c2d12016-07-29 12:48:20 -0700204 return flags
205}
206
Colin Crossb916a382016-07-29 17:28:03 -0700207func (linker *baseLinker) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700208 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Crossb916a382016-07-29 17:28:03 -0700209 panic(fmt.Errorf("baseLinker doesn't know how to link"))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700210}