Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 1 | // Copyright 2019 The Android Open Source Project |
| 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 config |
| 16 | |
| 17 | import ( |
| 18 | "strings" |
| 19 | |
| 20 | "android/soong/android" |
| 21 | ) |
| 22 | |
| 23 | var ( |
Jeff Vander Stoep | 6e97a7b | 2020-07-15 21:34:45 +0200 | [diff] [blame] | 24 | LinuxRustFlags = []string{} |
Colin Cross | e32f093 | 2022-01-23 20:48:36 -0800 | [diff] [blame] | 25 | LinuxMuslRustFlags = []string{ |
| 26 | // disable rustc's builtin fallbacks for crt objects |
| 27 | "-C link_self_contained=no", |
| 28 | // force rustc to use a dynamic musl libc |
| 29 | "-C target-feature=-crt-static", |
| 30 | "-Z link-native-libraries=no", |
| 31 | } |
Jeff Vander Stoep | 6e97a7b | 2020-07-15 21:34:45 +0200 | [diff] [blame] | 32 | LinuxRustLinkFlags = []string{ |
ThiƩbaud Weksteen | 682c9d7 | 2020-08-31 10:06:16 +0200 | [diff] [blame] | 33 | "-B${cc_config.ClangBin}", |
Jeff Vander Stoep | 6e97a7b | 2020-07-15 21:34:45 +0200 | [diff] [blame] | 34 | "-fuse-ld=lld", |
Ivan Lozano | 6d45a98 | 2020-09-09 09:08:44 -0400 | [diff] [blame] | 35 | "-Wl,--undefined-version", |
Colin Cross | e32f093 | 2022-01-23 20:48:36 -0800 | [diff] [blame] | 36 | } |
| 37 | LinuxRustGlibcLinkFlags = []string{ |
Colin Cross | 748c8c7 | 2021-09-10 09:55:54 -0700 | [diff] [blame] | 38 | "--sysroot ${cc_config.LinuxGccRoot}/sysroot", |
Jeff Vander Stoep | 6e97a7b | 2020-07-15 21:34:45 +0200 | [diff] [blame] | 39 | } |
Colin Cross | e32f093 | 2022-01-23 20:48:36 -0800 | [diff] [blame] | 40 | LinuxRustMuslLinkFlags = []string{ |
| 41 | "--sysroot /dev/null", |
| 42 | "-nodefaultlibs", |
| 43 | "-nostdlib", |
| 44 | "-Wl,--no-dynamic-linker", |
Colin Cross | e32f093 | 2022-01-23 20:48:36 -0800 | [diff] [blame] | 45 | } |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 46 | linuxX86Rustflags = []string{} |
| 47 | linuxX86Linkflags = []string{} |
| 48 | linuxX8664Rustflags = []string{} |
| 49 | linuxX8664Linkflags = []string{} |
| 50 | ) |
| 51 | |
| 52 | func init() { |
Colin Cross | e32f093 | 2022-01-23 20:48:36 -0800 | [diff] [blame] | 53 | registerToolchainFactory(android.Linux, android.X86_64, linuxGlibcX8664ToolchainFactory) |
| 54 | registerToolchainFactory(android.Linux, android.X86, linuxGlibcX86ToolchainFactory) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 55 | |
Colin Cross | e32f093 | 2022-01-23 20:48:36 -0800 | [diff] [blame] | 56 | registerToolchainFactory(android.LinuxMusl, android.X86_64, linuxMuslX8664ToolchainFactory) |
| 57 | registerToolchainFactory(android.LinuxMusl, android.X86, linuxMuslX86ToolchainFactory) |
Colin Cross | 528d67e | 2021-07-23 22:23:07 +0000 | [diff] [blame] | 58 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 59 | pctx.StaticVariable("LinuxToolchainRustFlags", strings.Join(LinuxRustFlags, " ")) |
Colin Cross | e32f093 | 2022-01-23 20:48:36 -0800 | [diff] [blame] | 60 | pctx.StaticVariable("LinuxMuslToolchainRustFlags", strings.Join(LinuxMuslRustFlags, " ")) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 61 | pctx.StaticVariable("LinuxToolchainLinkFlags", strings.Join(LinuxRustLinkFlags, " ")) |
Colin Cross | e32f093 | 2022-01-23 20:48:36 -0800 | [diff] [blame] | 62 | pctx.StaticVariable("LinuxGlibcToolchainLinkFlags", strings.Join(LinuxRustGlibcLinkFlags, " ")) |
| 63 | pctx.StaticVariable("LinuxMuslToolchainLinkFlags", strings.Join(LinuxRustMuslLinkFlags, " ")) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 64 | pctx.StaticVariable("LinuxToolchainX86RustFlags", strings.Join(linuxX86Rustflags, " ")) |
| 65 | pctx.StaticVariable("LinuxToolchainX86LinkFlags", strings.Join(linuxX86Linkflags, " ")) |
| 66 | pctx.StaticVariable("LinuxToolchainX8664RustFlags", strings.Join(linuxX8664Rustflags, " ")) |
| 67 | pctx.StaticVariable("LinuxToolchainX8664LinkFlags", strings.Join(linuxX8664Linkflags, " ")) |
| 68 | |
| 69 | } |
| 70 | |
Colin Cross | e32f093 | 2022-01-23 20:48:36 -0800 | [diff] [blame] | 71 | // Base 64-bit linux rust toolchain |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 72 | type toolchainLinuxX8664 struct { |
| 73 | toolchain64Bit |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | func (toolchainLinuxX8664) Supported() bool { |
| 77 | return true |
| 78 | } |
| 79 | |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 80 | func (toolchainLinuxX8664) Bionic() bool { |
| 81 | return false |
| 82 | } |
| 83 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 84 | func (t *toolchainLinuxX8664) Name() string { |
| 85 | return "x86_64" |
| 86 | } |
| 87 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 88 | func (t *toolchainLinuxX8664) ToolchainLinkFlags() string { |
Ivan Lozano | 6d45a98 | 2020-09-09 09:08:44 -0400 | [diff] [blame] | 89 | // Prepend the lld flags from cc_config so we stay in sync with cc |
Colin Cross | 0523ba2 | 2021-07-14 18:45:05 -0700 | [diff] [blame] | 90 | return "${cc_config.LinuxLldflags} ${cc_config.LinuxX8664Lldflags} " + |
Ivan Lozano | 6d45a98 | 2020-09-09 09:08:44 -0400 | [diff] [blame] | 91 | "${config.LinuxToolchainLinkFlags} ${config.LinuxToolchainX8664LinkFlags}" |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | func (t *toolchainLinuxX8664) ToolchainRustFlags() string { |
| 95 | return "${config.LinuxToolchainRustFlags} ${config.LinuxToolchainX8664RustFlags}" |
| 96 | } |
| 97 | |
Colin Cross | e32f093 | 2022-01-23 20:48:36 -0800 | [diff] [blame] | 98 | // Specialization of the 64-bit linux rust toolchain for glibc. Adds the gnu rust triple and |
| 99 | // sysroot linker flags. |
| 100 | type toolchainLinuxGlibcX8664 struct { |
| 101 | toolchainLinuxX8664 |
| 102 | } |
| 103 | |
| 104 | func (t *toolchainLinuxX8664) RustTriple() string { |
| 105 | return "x86_64-unknown-linux-gnu" |
| 106 | } |
| 107 | |
| 108 | func (t *toolchainLinuxGlibcX8664) ToolchainLinkFlags() string { |
| 109 | return t.toolchainLinuxX8664.ToolchainLinkFlags() + " " + "${config.LinuxGlibcToolchainLinkFlags}" |
| 110 | } |
| 111 | |
| 112 | func linuxGlibcX8664ToolchainFactory(arch android.Arch) Toolchain { |
| 113 | return toolchainLinuxGlibcX8664Singleton |
| 114 | } |
| 115 | |
| 116 | // Specialization of the 64-bit linux rust toolchain for musl. Adds the musl rust triple and |
| 117 | // linker flags to avoid using the host sysroot. |
| 118 | type toolchainLinuxMuslX8664 struct { |
| 119 | toolchainLinuxX8664 |
| 120 | } |
| 121 | |
| 122 | func (t *toolchainLinuxMuslX8664) RustTriple() string { |
| 123 | return "x86_64-unknown-linux-musl" |
| 124 | } |
| 125 | |
| 126 | func (t *toolchainLinuxMuslX8664) ToolchainLinkFlags() string { |
| 127 | return t.toolchainLinuxX8664.ToolchainLinkFlags() + " " + "${config.LinuxMuslToolchainLinkFlags}" |
| 128 | } |
| 129 | |
| 130 | func (t *toolchainLinuxMuslX8664) ToolchainRustFlags() string { |
| 131 | return t.toolchainLinuxX8664.ToolchainRustFlags() + " " + "${config.LinuxMuslToolchainRustFlags}" |
| 132 | } |
| 133 | |
| 134 | func linuxMuslX8664ToolchainFactory(arch android.Arch) Toolchain { |
| 135 | return toolchainLinuxMuslX8664Singleton |
| 136 | } |
| 137 | |
| 138 | // Base 32-bit linux rust toolchain |
| 139 | type toolchainLinuxX86 struct { |
| 140 | toolchain32Bit |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | func (toolchainLinuxX86) Supported() bool { |
| 144 | return true |
| 145 | } |
| 146 | |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 147 | func (toolchainLinuxX86) Bionic() bool { |
| 148 | return false |
| 149 | } |
| 150 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 151 | func (t *toolchainLinuxX86) Name() string { |
| 152 | return "x86" |
| 153 | } |
| 154 | |
Ivan Lozano | 6cd99e6 | 2020-02-11 08:24:25 -0500 | [diff] [blame] | 155 | func (toolchainLinuxX86) LibclangRuntimeLibraryArch() string { |
| 156 | return "i386" |
| 157 | } |
| 158 | |
| 159 | func (toolchainLinuxX8664) LibclangRuntimeLibraryArch() string { |
| 160 | return "x86_64" |
| 161 | } |
| 162 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 163 | func (t *toolchainLinuxX86) ToolchainLinkFlags() string { |
Ivan Lozano | 6d45a98 | 2020-09-09 09:08:44 -0400 | [diff] [blame] | 164 | // Prepend the lld flags from cc_config so we stay in sync with cc |
Colin Cross | 0523ba2 | 2021-07-14 18:45:05 -0700 | [diff] [blame] | 165 | return "${cc_config.LinuxLldflags} ${cc_config.LinuxX86Lldflags} " + |
Ivan Lozano | 6d45a98 | 2020-09-09 09:08:44 -0400 | [diff] [blame] | 166 | "${config.LinuxToolchainLinkFlags} ${config.LinuxToolchainX86LinkFlags}" |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | func (t *toolchainLinuxX86) ToolchainRustFlags() string { |
| 170 | return "${config.LinuxToolchainRustFlags} ${config.LinuxToolchainX86RustFlags}" |
| 171 | } |
| 172 | |
Colin Cross | e32f093 | 2022-01-23 20:48:36 -0800 | [diff] [blame] | 173 | // Specialization of the 32-bit linux rust toolchain for glibc. Adds the gnu rust triple and |
| 174 | // sysroot linker flags. |
| 175 | type toolchainLinuxGlibcX86 struct { |
| 176 | toolchainLinuxX86 |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Colin Cross | e32f093 | 2022-01-23 20:48:36 -0800 | [diff] [blame] | 179 | func (t *toolchainLinuxGlibcX86) RustTriple() string { |
| 180 | return "i686-unknown-linux-gnu" |
| 181 | } |
| 182 | |
| 183 | func (t *toolchainLinuxGlibcX86) ToolchainLinkFlags() string { |
| 184 | return t.toolchainLinuxX86.ToolchainLinkFlags() + " " + "${config.LinuxGlibcToolchainLinkFlags}" |
| 185 | } |
| 186 | |
| 187 | func linuxGlibcX86ToolchainFactory(arch android.Arch) Toolchain { |
| 188 | return toolchainLinuxGlibcX86Singleton |
| 189 | } |
| 190 | |
| 191 | // Specialization of the 32-bit linux rust toolchain for musl. Adds the musl rust triple and |
| 192 | // linker flags to avoid using the host sysroot. |
| 193 | type toolchainLinuxMuslX86 struct { |
| 194 | toolchainLinuxX86 |
| 195 | } |
| 196 | |
| 197 | func (t *toolchainLinuxMuslX86) RustTriple() string { |
| 198 | return "i686-unknown-linux-musl" |
| 199 | } |
| 200 | |
| 201 | func (t *toolchainLinuxMuslX86) ToolchainLinkFlags() string { |
| 202 | return t.toolchainLinuxX86.ToolchainLinkFlags() + " " + "${config.LinuxMuslToolchainLinkFlags}" |
| 203 | } |
| 204 | |
| 205 | func (t *toolchainLinuxMuslX86) ToolchainRustFlags() string { |
| 206 | return t.toolchainLinuxX86.ToolchainRustFlags() + " " + "${config.LinuxMuslToolchainRustFlags}" |
| 207 | } |
| 208 | |
| 209 | func linuxMuslX86ToolchainFactory(arch android.Arch) Toolchain { |
| 210 | return toolchainLinuxMuslX86Singleton |
| 211 | } |
| 212 | |
| 213 | var toolchainLinuxGlibcX8664Singleton Toolchain = &toolchainLinuxGlibcX8664{} |
| 214 | var toolchainLinuxGlibcX86Singleton Toolchain = &toolchainLinuxGlibcX86{} |
| 215 | var toolchainLinuxMuslX8664Singleton Toolchain = &toolchainLinuxMuslX8664{} |
| 216 | var toolchainLinuxMuslX86Singleton Toolchain = &toolchainLinuxMuslX86{} |