blob: 2c4c25059d1a6b696e892f3841afa136f5a0dedd [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"
20)
21
Colin Cross4d9c2d12016-07-29 12:48:20 -070022// This file contains the basic functionality for linking against static libraries and shared
23// libraries. Final linking into libraries or executables is handled in library.go, binary.go, etc.
24
25type BaseLinkerProperties struct {
26 // list of modules whose object files should be linked into this module
27 // in their entirety. For static library modules, all of the .o files from the intermediate
28 // directory of the dependency will be linked into this modules .a file. For a shared library,
29 // the dependency's .a file will be linked into this module using -Wl,--whole-archive.
30 Whole_static_libs []string `android:"arch_variant,variant_prepend"`
31
32 // list of modules that should be statically linked into this module.
33 Static_libs []string `android:"arch_variant,variant_prepend"`
34
35 // list of modules that should be dynamically linked into this module.
36 Shared_libs []string `android:"arch_variant"`
37
38 // list of module-specific flags that will be used for all link steps
39 Ldflags []string `android:"arch_variant"`
40
41 // don't insert default compiler flags into asflags, cflags,
42 // cppflags, conlyflags, ldflags, or include_dirs
43 No_default_compiler_flags *bool
44
45 // list of system libraries that will be dynamically linked to
46 // shared library and executable modules. If unset, generally defaults to libc
47 // and libm. Set to [] to prevent linking against libc and libm.
48 System_shared_libs []string
49
50 // allow the module to contain undefined symbols. By default,
51 // modules cannot contain undefined symbols that are not satisified by their immediate
52 // dependencies. Set this flag to true to remove --no-undefined from the linker flags.
53 // This flag should only be necessary for compiling low-level libraries like libc.
54 Allow_undefined_symbols *bool
55
56 // don't link in libgcc.a
57 No_libgcc *bool
58
59 // -l arguments to pass to linker for host-provided shared libraries
60 Host_ldlibs []string `android:"arch_variant"`
61
62 // list of shared libraries to re-export include directories from. Entries must be
63 // present in shared_libs.
64 Export_shared_lib_headers []string `android:"arch_variant"`
65
66 // list of static libraries to re-export include directories from. Entries must be
67 // present in static_libs.
68 Export_static_lib_headers []string `android:"arch_variant"`
69
Dan Willemsenb3454ab2016-09-28 17:34:58 -070070 // list of generated headers to re-export include directories from. Entries must be
71 // present in generated_headers.
72 Export_generated_headers []string `android:"arch_variant"`
73
Colin Cross4d9c2d12016-07-29 12:48:20 -070074 // don't link in crt_begin and crt_end. This flag should only be necessary for
75 // compiling crt or libc.
76 Nocrt *bool `android:"arch_variant"`
77}
78
Colin Crossb916a382016-07-29 17:28:03 -070079func NewBaseLinker() *baseLinker {
80 return &baseLinker{}
81}
82
Colin Cross4d9c2d12016-07-29 12:48:20 -070083// baseLinker provides support for shared_libs, static_libs, and whole_static_libs properties
84type baseLinker struct {
85 Properties BaseLinkerProperties
86 dynamicProperties struct {
Colin Crossb916a382016-07-29 17:28:03 -070087 RunPaths []string `blueprint:"mutated"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070088 }
89}
90
91func (linker *baseLinker) appendLdflags(flags []string) {
92 linker.Properties.Ldflags = append(linker.Properties.Ldflags, flags...)
93}
94
Colin Cross42742b82016-08-01 13:20:05 -070095func (linker *baseLinker) linkerInit(ctx BaseModuleContext) {
Colin Cross4d9c2d12016-07-29 12:48:20 -070096 if ctx.toolchain().Is64Bit() {
Colin Crossb916a382016-07-29 17:28:03 -070097 linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, "../lib64", "lib64")
Colin Cross4d9c2d12016-07-29 12:48:20 -070098 } else {
Colin Crossb916a382016-07-29 17:28:03 -070099 linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, "../lib", "lib")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700100 }
101}
102
Colin Cross42742b82016-08-01 13:20:05 -0700103func (linker *baseLinker) linkerProps() []interface{} {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700104 return []interface{}{&linker.Properties, &linker.dynamicProperties}
105}
106
Colin Cross42742b82016-08-01 13:20:05 -0700107func (linker *baseLinker) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700108 deps.WholeStaticLibs = append(deps.WholeStaticLibs, linker.Properties.Whole_static_libs...)
109 deps.StaticLibs = append(deps.StaticLibs, linker.Properties.Static_libs...)
110 deps.SharedLibs = append(deps.SharedLibs, linker.Properties.Shared_libs...)
111
112 deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, linker.Properties.Export_static_lib_headers...)
113 deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, linker.Properties.Export_shared_lib_headers...)
Dan Willemsenb3454ab2016-09-28 17:34:58 -0700114 deps.ReexportGeneratedHeaders = append(deps.ReexportGeneratedHeaders, linker.Properties.Export_generated_headers...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700115
Dan Albert83705c82016-09-14 16:47:18 -0700116 if ctx.ModuleName() != "libcompiler_rt-extras" {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700117 deps.LateStaticLibs = append(deps.LateStaticLibs, "libcompiler_rt-extras")
118 }
119
120 if ctx.Device() {
121 // libgcc and libatomic have to be last on the command line
122 deps.LateStaticLibs = append(deps.LateStaticLibs, "libatomic")
123 if !Bool(linker.Properties.No_libgcc) {
124 deps.LateStaticLibs = append(deps.LateStaticLibs, "libgcc")
125 }
126
Colin Crossb916a382016-07-29 17:28:03 -0700127 if !ctx.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700128 if linker.Properties.System_shared_libs != nil {
129 deps.LateSharedLibs = append(deps.LateSharedLibs,
130 linker.Properties.System_shared_libs...)
131 } else if !ctx.sdk() {
132 deps.LateSharedLibs = append(deps.LateSharedLibs, "libc", "libm")
133 }
134 }
135
136 if ctx.sdk() {
137 deps.SharedLibs = append(deps.SharedLibs,
138 "libc",
139 "libm",
140 )
141 }
142 }
143
144 return deps
145}
146
Colin Cross42742b82016-08-01 13:20:05 -0700147func (linker *baseLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700148 toolchain := ctx.toolchain()
149
Colin Cross4d9c2d12016-07-29 12:48:20 -0700150 if !ctx.noDefaultCompilerFlags() {
151 if ctx.Device() && !Bool(linker.Properties.Allow_undefined_symbols) {
152 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-undefined")
153 }
154
155 if flags.Clang {
156 flags.LdFlags = append(flags.LdFlags, toolchain.ClangLdflags())
157 } else {
158 flags.LdFlags = append(flags.LdFlags, toolchain.Ldflags())
159 }
160
161 if ctx.Host() {
162 CheckBadHostLdlibs(ctx, "host_ldlibs", linker.Properties.Host_ldlibs)
163
164 flags.LdFlags = append(flags.LdFlags, linker.Properties.Host_ldlibs...)
165 }
166 }
167
168 CheckBadLinkerFlags(ctx, "ldflags", linker.Properties.Ldflags)
169
170 flags.LdFlags = append(flags.LdFlags, linker.Properties.Ldflags...)
171
Colin Crossb916a382016-07-29 17:28:03 -0700172 if ctx.Host() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700173 rpath_prefix := `\$$ORIGIN/`
174 if ctx.Darwin() {
175 rpath_prefix = "@loader_path/"
176 }
177
178 for _, rpath := range linker.dynamicProperties.RunPaths {
179 flags.LdFlags = append(flags.LdFlags, "-Wl,-rpath,"+rpath_prefix+rpath)
180 }
181 }
182
183 if flags.Clang {
184 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainClangLdflags())
185 } else {
186 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainLdflags())
187 }
188
189 return flags
190}
191
Colin Crossb916a382016-07-29 17:28:03 -0700192func (linker *baseLinker) link(ctx ModuleContext,
193 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
194 panic(fmt.Errorf("baseLinker doesn't know how to link"))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700195}