Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package cc |
| 16 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 17 | import ( |
| 18 | "android/soong/android" |
| 19 | "fmt" |
| 20 | ) |
| 21 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 22 | // 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 | |
| 25 | type 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 Willemsen | b3454ab | 2016-09-28 17:34:58 -0700 | [diff] [blame^] | 70 | // 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 Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 74 | // 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 Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 79 | func NewBaseLinker() *baseLinker { |
| 80 | return &baseLinker{} |
| 81 | } |
| 82 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 83 | // baseLinker provides support for shared_libs, static_libs, and whole_static_libs properties |
| 84 | type baseLinker struct { |
| 85 | Properties BaseLinkerProperties |
| 86 | dynamicProperties struct { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 87 | RunPaths []string `blueprint:"mutated"` |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
| 91 | func (linker *baseLinker) appendLdflags(flags []string) { |
| 92 | linker.Properties.Ldflags = append(linker.Properties.Ldflags, flags...) |
| 93 | } |
| 94 | |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 95 | func (linker *baseLinker) linkerInit(ctx BaseModuleContext) { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 96 | if ctx.toolchain().Is64Bit() { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 97 | linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, "../lib64", "lib64") |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 98 | } else { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 99 | linker.dynamicProperties.RunPaths = append(linker.dynamicProperties.RunPaths, "../lib", "lib") |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 103 | func (linker *baseLinker) linkerProps() []interface{} { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 104 | return []interface{}{&linker.Properties, &linker.dynamicProperties} |
| 105 | } |
| 106 | |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 107 | func (linker *baseLinker) linkerDeps(ctx BaseModuleContext, deps Deps) Deps { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 108 | 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 Willemsen | b3454ab | 2016-09-28 17:34:58 -0700 | [diff] [blame^] | 114 | deps.ReexportGeneratedHeaders = append(deps.ReexportGeneratedHeaders, linker.Properties.Export_generated_headers...) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 115 | |
Dan Albert | 83705c8 | 2016-09-14 16:47:18 -0700 | [diff] [blame] | 116 | if ctx.ModuleName() != "libcompiler_rt-extras" { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 117 | 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 Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 127 | if !ctx.static() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 128 | 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 Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 147 | func (linker *baseLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 148 | toolchain := ctx.toolchain() |
| 149 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 150 | 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 Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 172 | if ctx.Host() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 173 | 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 Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 192 | func (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 Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 195 | } |