blob: df19de7a7285997c02f591d9da9686c73734ac53 [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
70 // don't link in crt_begin and crt_end. This flag should only be necessary for
71 // compiling crt or libc.
72 Nocrt *bool `android:"arch_variant"`
73}
74
Colin Crossb916a382016-07-29 17:28:03 -070075func NewBaseLinker() *baseLinker {
76 return &baseLinker{}
77}
78
Colin Cross4d9c2d12016-07-29 12:48:20 -070079// baseLinker provides support for shared_libs, static_libs, and whole_static_libs properties
80type baseLinker struct {
81 Properties BaseLinkerProperties
82 dynamicProperties struct {
Colin Crossb916a382016-07-29 17:28:03 -070083 RunPaths []string `blueprint:"mutated"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070084 }
85}
86
87func (linker *baseLinker) appendLdflags(flags []string) {
88 linker.Properties.Ldflags = append(linker.Properties.Ldflags, flags...)
89}
90
Colin Cross42742b82016-08-01 13:20:05 -070091func (linker *baseLinker) linkerInit(ctx BaseModuleContext) {
Colin Cross4d9c2d12016-07-29 12:48:20 -070092 if ctx.toolchain().Is64Bit() {
Colin Crossb916a382016-07-29 17:28:03 -070093 linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, "../lib64", "lib64")
Colin Cross4d9c2d12016-07-29 12:48:20 -070094 } else {
Colin Crossb916a382016-07-29 17:28:03 -070095 linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, "../lib", "lib")
Colin Cross4d9c2d12016-07-29 12:48:20 -070096 }
97}
98
Colin Cross42742b82016-08-01 13:20:05 -070099func (linker *baseLinker) linkerProps() []interface{} {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700100 return []interface{}{&linker.Properties, &linker.dynamicProperties}
101}
102
Colin Cross42742b82016-08-01 13:20:05 -0700103func (linker *baseLinker) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700104 deps.WholeStaticLibs = append(deps.WholeStaticLibs, linker.Properties.Whole_static_libs...)
105 deps.StaticLibs = append(deps.StaticLibs, linker.Properties.Static_libs...)
106 deps.SharedLibs = append(deps.SharedLibs, linker.Properties.Shared_libs...)
107
108 deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, linker.Properties.Export_static_lib_headers...)
109 deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, linker.Properties.Export_shared_lib_headers...)
110
111 if !ctx.sdk() && ctx.ModuleName() != "libcompiler_rt-extras" {
112 deps.LateStaticLibs = append(deps.LateStaticLibs, "libcompiler_rt-extras")
113 }
114
115 if ctx.Device() {
116 // libgcc and libatomic have to be last on the command line
117 deps.LateStaticLibs = append(deps.LateStaticLibs, "libatomic")
118 if !Bool(linker.Properties.No_libgcc) {
119 deps.LateStaticLibs = append(deps.LateStaticLibs, "libgcc")
120 }
121
Colin Crossb916a382016-07-29 17:28:03 -0700122 if !ctx.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700123 if linker.Properties.System_shared_libs != nil {
124 deps.LateSharedLibs = append(deps.LateSharedLibs,
125 linker.Properties.System_shared_libs...)
126 } else if !ctx.sdk() {
127 deps.LateSharedLibs = append(deps.LateSharedLibs, "libc", "libm")
128 }
129 }
130
131 if ctx.sdk() {
132 deps.SharedLibs = append(deps.SharedLibs,
133 "libc",
134 "libm",
135 )
136 }
137 }
138
139 return deps
140}
141
Colin Cross42742b82016-08-01 13:20:05 -0700142func (linker *baseLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700143 toolchain := ctx.toolchain()
144
Colin Cross4d9c2d12016-07-29 12:48:20 -0700145 if !ctx.noDefaultCompilerFlags() {
146 if ctx.Device() && !Bool(linker.Properties.Allow_undefined_symbols) {
147 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-undefined")
148 }
149
150 if flags.Clang {
151 flags.LdFlags = append(flags.LdFlags, toolchain.ClangLdflags())
152 } else {
153 flags.LdFlags = append(flags.LdFlags, toolchain.Ldflags())
154 }
155
156 if ctx.Host() {
157 CheckBadHostLdlibs(ctx, "host_ldlibs", linker.Properties.Host_ldlibs)
158
159 flags.LdFlags = append(flags.LdFlags, linker.Properties.Host_ldlibs...)
160 }
161 }
162
163 CheckBadLinkerFlags(ctx, "ldflags", linker.Properties.Ldflags)
164
165 flags.LdFlags = append(flags.LdFlags, linker.Properties.Ldflags...)
166
Colin Crossb916a382016-07-29 17:28:03 -0700167 if ctx.Host() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700168 rpath_prefix := `\$$ORIGIN/`
169 if ctx.Darwin() {
170 rpath_prefix = "@loader_path/"
171 }
172
173 for _, rpath := range linker.dynamicProperties.RunPaths {
174 flags.LdFlags = append(flags.LdFlags, "-Wl,-rpath,"+rpath_prefix+rpath)
175 }
176 }
177
178 if flags.Clang {
179 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainClangLdflags())
180 } else {
181 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainLdflags())
182 }
183
184 return flags
185}
186
Colin Crossb916a382016-07-29 17:28:03 -0700187func (linker *baseLinker) link(ctx ModuleContext,
188 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
189 panic(fmt.Errorf("baseLinker doesn't know how to link"))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700190}