blob: 07d9aa8026c74df39463c6341f159a28fff4b646 [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
Colin Cross5950f382016-12-13 12:50:57 -080040 // list of modules that should only provide headers for this module.
41 Header_libs []string `android:"arch_variant,variant_prepend"`
42
Colin Cross4d9c2d12016-07-29 12:48:20 -070043 // list of module-specific flags that will be used for all link steps
44 Ldflags []string `android:"arch_variant"`
45
46 // don't insert default compiler flags into asflags, cflags,
47 // cppflags, conlyflags, ldflags, or include_dirs
48 No_default_compiler_flags *bool
49
50 // list of system libraries that will be dynamically linked to
51 // shared library and executable modules. If unset, generally defaults to libc
52 // and libm. Set to [] to prevent linking against libc and libm.
53 System_shared_libs []string
54
55 // allow the module to contain undefined symbols. By default,
56 // modules cannot contain undefined symbols that are not satisified by their immediate
57 // dependencies. Set this flag to true to remove --no-undefined from the linker flags.
58 // This flag should only be necessary for compiling low-level libraries like libc.
Colin Crossbe360ae2016-12-08 09:45:21 -080059 Allow_undefined_symbols *bool `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070060
61 // don't link in libgcc.a
62 No_libgcc *bool
63
64 // -l arguments to pass to linker for host-provided shared libraries
65 Host_ldlibs []string `android:"arch_variant"`
66
67 // list of shared libraries to re-export include directories from. Entries must be
68 // present in shared_libs.
69 Export_shared_lib_headers []string `android:"arch_variant"`
70
71 // list of static libraries to re-export include directories from. Entries must be
72 // present in static_libs.
73 Export_static_lib_headers []string `android:"arch_variant"`
74
Colin Cross5950f382016-12-13 12:50:57 -080075 // list of header libraries to re-export include directories from. Entries must be
76 // present in header_libs.
77 Export_header_lib_headers []string `android:"arch_variant"`
78
Dan Willemsenb3454ab2016-09-28 17:34:58 -070079 // list of generated headers to re-export include directories from. Entries must be
80 // present in generated_headers.
81 Export_generated_headers []string `android:"arch_variant"`
82
Colin Cross4d9c2d12016-07-29 12:48:20 -070083 // don't link in crt_begin and crt_end. This flag should only be necessary for
84 // compiling crt or libc.
85 Nocrt *bool `android:"arch_variant"`
Colin Cross18c0c5a2016-12-01 14:45:23 -080086
87 // group static libraries. This can resolve missing symbols issues with interdependencies
88 // between static libraries, but it is generally better to order them correctly instead.
89 Group_static_libs *bool `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070090}
91
Colin Crossb916a382016-07-29 17:28:03 -070092func NewBaseLinker() *baseLinker {
93 return &baseLinker{}
94}
95
Colin Cross4d9c2d12016-07-29 12:48:20 -070096// baseLinker provides support for shared_libs, static_libs, and whole_static_libs properties
97type baseLinker struct {
98 Properties BaseLinkerProperties
99 dynamicProperties struct {
Colin Crossb916a382016-07-29 17:28:03 -0700100 RunPaths []string `blueprint:"mutated"`
Colin Cross4d9c2d12016-07-29 12:48:20 -0700101 }
102}
103
104func (linker *baseLinker) appendLdflags(flags []string) {
105 linker.Properties.Ldflags = append(linker.Properties.Ldflags, flags...)
106}
107
Colin Cross42742b82016-08-01 13:20:05 -0700108func (linker *baseLinker) linkerInit(ctx BaseModuleContext) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700109 if ctx.toolchain().Is64Bit() {
Colin Crossb916a382016-07-29 17:28:03 -0700110 linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, "../lib64", "lib64")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700111 } else {
Colin Crossb916a382016-07-29 17:28:03 -0700112 linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, "../lib", "lib")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700113 }
114}
115
Colin Cross42742b82016-08-01 13:20:05 -0700116func (linker *baseLinker) linkerProps() []interface{} {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700117 return []interface{}{&linker.Properties, &linker.dynamicProperties}
118}
119
Colin Cross42742b82016-08-01 13:20:05 -0700120func (linker *baseLinker) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700121 deps.WholeStaticLibs = append(deps.WholeStaticLibs, linker.Properties.Whole_static_libs...)
Colin Cross5950f382016-12-13 12:50:57 -0800122 deps.HeaderLibs = append(deps.HeaderLibs, linker.Properties.Header_libs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700123 deps.StaticLibs = append(deps.StaticLibs, linker.Properties.Static_libs...)
124 deps.SharedLibs = append(deps.SharedLibs, linker.Properties.Shared_libs...)
125
Colin Cross5950f382016-12-13 12:50:57 -0800126 deps.ReexportHeaderLibHeaders = append(deps.ReexportHeaderLibHeaders, linker.Properties.Export_header_lib_headers...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700127 deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, linker.Properties.Export_static_lib_headers...)
128 deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, linker.Properties.Export_shared_lib_headers...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700129 deps.ReexportGeneratedHeaders = append(deps.ReexportGeneratedHeaders, linker.Properties.Export_generated_headers...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700130
Dan Albert83705c82016-09-14 16:47:18 -0700131 if ctx.ModuleName() != "libcompiler_rt-extras" {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700132 deps.LateStaticLibs = append(deps.LateStaticLibs, "libcompiler_rt-extras")
133 }
134
Dan Willemsen2e47b342016-11-17 01:02:25 -0800135 if ctx.toolchain().Bionic() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700136 // libgcc and libatomic have to be last on the command line
137 deps.LateStaticLibs = append(deps.LateStaticLibs, "libatomic")
138 if !Bool(linker.Properties.No_libgcc) {
139 deps.LateStaticLibs = append(deps.LateStaticLibs, "libgcc")
140 }
141
Colin Crossb916a382016-07-29 17:28:03 -0700142 if !ctx.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700143 if linker.Properties.System_shared_libs != nil {
144 deps.LateSharedLibs = append(deps.LateSharedLibs,
145 linker.Properties.System_shared_libs...)
146 } else if !ctx.sdk() {
147 deps.LateSharedLibs = append(deps.LateSharedLibs, "libc", "libm")
148 }
149 }
150
151 if ctx.sdk() {
152 deps.SharedLibs = append(deps.SharedLibs,
153 "libc",
154 "libm",
155 )
156 }
157 }
158
Josh Gao7bd4c5c2017-02-23 17:52:24 -0800159 if ctx.Os() == android.Windows {
160 deps.LateStaticLibs = append(deps.LateStaticLibs, "libwinpthread")
161 }
162
Colin Cross4d9c2d12016-07-29 12:48:20 -0700163 return deps
164}
165
Colin Cross42742b82016-08-01 13:20:05 -0700166func (linker *baseLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700167 toolchain := ctx.toolchain()
168
Colin Cross4d9c2d12016-07-29 12:48:20 -0700169 if !ctx.noDefaultCompilerFlags() {
Colin Cross46974e22016-10-20 12:41:14 -0700170 if Bool(linker.Properties.Allow_undefined_symbols) {
171 if ctx.Darwin() {
172 // darwin defaults to treating undefined symbols as errors
173 flags.LdFlags = append(flags.LdFlags, "-Wl,-undefined,dynamic_lookup")
174 }
175 } else if !ctx.Darwin() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700176 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-undefined")
177 }
178
179 if flags.Clang {
180 flags.LdFlags = append(flags.LdFlags, toolchain.ClangLdflags())
181 } else {
182 flags.LdFlags = append(flags.LdFlags, toolchain.Ldflags())
183 }
184
Dan Willemsen2e47b342016-11-17 01:02:25 -0800185 if !ctx.toolchain().Bionic() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700186 CheckBadHostLdlibs(ctx, "host_ldlibs", linker.Properties.Host_ldlibs)
187
188 flags.LdFlags = append(flags.LdFlags, linker.Properties.Host_ldlibs...)
189 }
190 }
191
192 CheckBadLinkerFlags(ctx, "ldflags", linker.Properties.Ldflags)
193
Colin Cross4b963f82016-09-29 14:06:02 -0700194 flags.LdFlags = append(flags.LdFlags, proptools.NinjaAndShellEscape(linker.Properties.Ldflags)...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700195
Colin Crossb916a382016-07-29 17:28:03 -0700196 if ctx.Host() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700197 rpath_prefix := `\$$ORIGIN/`
198 if ctx.Darwin() {
199 rpath_prefix = "@loader_path/"
200 }
201
202 for _, rpath := range linker.dynamicProperties.RunPaths {
203 flags.LdFlags = append(flags.LdFlags, "-Wl,-rpath,"+rpath_prefix+rpath)
204 }
205 }
206
207 if flags.Clang {
208 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainClangLdflags())
209 } else {
210 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainLdflags())
211 }
212
Colin Cross18c0c5a2016-12-01 14:45:23 -0800213 if Bool(linker.Properties.Group_static_libs) {
214 flags.GroupStaticLibs = true
215 }
216
Colin Cross4d9c2d12016-07-29 12:48:20 -0700217 return flags
218}
219
Colin Crossb916a382016-07-29 17:28:03 -0700220func (linker *baseLinker) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700221 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Crossb916a382016-07-29 17:28:03 -0700222 panic(fmt.Errorf("baseLinker doesn't know how to link"))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700223}