blob: 28572c69ad36e8fa487a43affa30a971c35163b1 [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"`
79}
80
Colin Crossb916a382016-07-29 17:28:03 -070081func NewBaseLinker() *baseLinker {
82 return &baseLinker{}
83}
84
Colin Cross4d9c2d12016-07-29 12:48:20 -070085// baseLinker provides support for shared_libs, static_libs, and whole_static_libs properties
86type baseLinker struct {
87 Properties BaseLinkerProperties
88 dynamicProperties struct {
Colin Crossb916a382016-07-29 17:28:03 -070089 RunPaths []string `blueprint:"mutated"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070090 }
91}
92
93func (linker *baseLinker) appendLdflags(flags []string) {
94 linker.Properties.Ldflags = append(linker.Properties.Ldflags, flags...)
95}
96
Colin Cross42742b82016-08-01 13:20:05 -070097func (linker *baseLinker) linkerInit(ctx BaseModuleContext) {
Colin Cross4d9c2d12016-07-29 12:48:20 -070098 if ctx.toolchain().Is64Bit() {
Colin Crossb916a382016-07-29 17:28:03 -070099 linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, "../lib64", "lib64")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700100 } else {
Colin Crossb916a382016-07-29 17:28:03 -0700101 linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, "../lib", "lib")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700102 }
103}
104
Colin Cross42742b82016-08-01 13:20:05 -0700105func (linker *baseLinker) linkerProps() []interface{} {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700106 return []interface{}{&linker.Properties, &linker.dynamicProperties}
107}
108
Colin Cross42742b82016-08-01 13:20:05 -0700109func (linker *baseLinker) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700110 deps.WholeStaticLibs = append(deps.WholeStaticLibs, linker.Properties.Whole_static_libs...)
111 deps.StaticLibs = append(deps.StaticLibs, linker.Properties.Static_libs...)
112 deps.SharedLibs = append(deps.SharedLibs, linker.Properties.Shared_libs...)
113
114 deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, linker.Properties.Export_static_lib_headers...)
115 deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, linker.Properties.Export_shared_lib_headers...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700116 deps.ReexportGeneratedHeaders = append(deps.ReexportGeneratedHeaders, linker.Properties.Export_generated_headers...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700117
Dan Albert83705c82016-09-14 16:47:18 -0700118 if ctx.ModuleName() != "libcompiler_rt-extras" {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700119 deps.LateStaticLibs = append(deps.LateStaticLibs, "libcompiler_rt-extras")
120 }
121
122 if ctx.Device() {
123 // libgcc and libatomic have to be last on the command line
124 deps.LateStaticLibs = append(deps.LateStaticLibs, "libatomic")
125 if !Bool(linker.Properties.No_libgcc) {
126 deps.LateStaticLibs = append(deps.LateStaticLibs, "libgcc")
127 }
128
Colin Crossb916a382016-07-29 17:28:03 -0700129 if !ctx.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700130 if linker.Properties.System_shared_libs != nil {
131 deps.LateSharedLibs = append(deps.LateSharedLibs,
132 linker.Properties.System_shared_libs...)
133 } else if !ctx.sdk() {
134 deps.LateSharedLibs = append(deps.LateSharedLibs, "libc", "libm")
135 }
136 }
137
138 if ctx.sdk() {
139 deps.SharedLibs = append(deps.SharedLibs,
140 "libc",
141 "libm",
142 )
143 }
144 }
145
146 return deps
147}
148
Colin Cross42742b82016-08-01 13:20:05 -0700149func (linker *baseLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700150 toolchain := ctx.toolchain()
151
Colin Cross4d9c2d12016-07-29 12:48:20 -0700152 if !ctx.noDefaultCompilerFlags() {
Colin Cross46974e22016-10-20 12:41:14 -0700153 if Bool(linker.Properties.Allow_undefined_symbols) {
154 if ctx.Darwin() {
155 // darwin defaults to treating undefined symbols as errors
156 flags.LdFlags = append(flags.LdFlags, "-Wl,-undefined,dynamic_lookup")
157 }
158 } else if !ctx.Darwin() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700159 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-undefined")
160 }
161
162 if flags.Clang {
163 flags.LdFlags = append(flags.LdFlags, toolchain.ClangLdflags())
164 } else {
165 flags.LdFlags = append(flags.LdFlags, toolchain.Ldflags())
166 }
167
168 if ctx.Host() {
169 CheckBadHostLdlibs(ctx, "host_ldlibs", linker.Properties.Host_ldlibs)
170
171 flags.LdFlags = append(flags.LdFlags, linker.Properties.Host_ldlibs...)
172 }
173 }
174
175 CheckBadLinkerFlags(ctx, "ldflags", linker.Properties.Ldflags)
176
Colin Cross4b963f82016-09-29 14:06:02 -0700177 flags.LdFlags = append(flags.LdFlags, proptools.NinjaAndShellEscape(linker.Properties.Ldflags)...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700178
Colin Crossb916a382016-07-29 17:28:03 -0700179 if ctx.Host() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700180 rpath_prefix := `\$$ORIGIN/`
181 if ctx.Darwin() {
182 rpath_prefix = "@loader_path/"
183 }
184
185 for _, rpath := range linker.dynamicProperties.RunPaths {
186 flags.LdFlags = append(flags.LdFlags, "-Wl,-rpath,"+rpath_prefix+rpath)
187 }
188 }
189
190 if flags.Clang {
191 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainClangLdflags())
192 } else {
193 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainLdflags())
194 }
195
196 return flags
197}
198
Colin Crossb916a382016-07-29 17:28:03 -0700199func (linker *baseLinker) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700200 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Crossb916a382016-07-29 17:28:03 -0700201 panic(fmt.Errorf("baseLinker doesn't know how to link"))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700202}