blob: 4d7c422190e75e56ba673c22a34f21f5d8021c10 [file] [log] [blame]
Ivan Lozanoffee3342019-08-27 12:03:00 -07001// 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
15package config
16
17import (
18 "strings"
19
20 "android/soong/android"
21)
22
23var (
Jeff Vander Stoep6e97a7b2020-07-15 21:34:45 +020024 LinuxRustFlags = []string{}
Colin Crosse32f0932022-01-23 20:48:36 -080025 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 Stoep6e97a7b2020-07-15 21:34:45 +020032 LinuxRustLinkFlags = []string{
ThiƩbaud Weksteen682c9d72020-08-31 10:06:16 +020033 "-B${cc_config.ClangBin}",
Jeff Vander Stoep6e97a7b2020-07-15 21:34:45 +020034 "-fuse-ld=lld",
Ivan Lozano6d45a982020-09-09 09:08:44 -040035 "-Wl,--undefined-version",
Colin Crosse32f0932022-01-23 20:48:36 -080036 }
37 LinuxRustGlibcLinkFlags = []string{
Colin Cross748c8c72021-09-10 09:55:54 -070038 "--sysroot ${cc_config.LinuxGccRoot}/sysroot",
Jeff Vander Stoep6e97a7b2020-07-15 21:34:45 +020039 }
Colin Crosse32f0932022-01-23 20:48:36 -080040 LinuxRustMuslLinkFlags = []string{
41 "--sysroot /dev/null",
42 "-nodefaultlibs",
43 "-nostdlib",
44 "-Wl,--no-dynamic-linker",
Colin Crosse32f0932022-01-23 20:48:36 -080045 }
Ivan Lozanoffee3342019-08-27 12:03:00 -070046 linuxX86Rustflags = []string{}
47 linuxX86Linkflags = []string{}
48 linuxX8664Rustflags = []string{}
49 linuxX8664Linkflags = []string{}
50)
51
52func init() {
Colin Crosse32f0932022-01-23 20:48:36 -080053 registerToolchainFactory(android.Linux, android.X86_64, linuxGlibcX8664ToolchainFactory)
54 registerToolchainFactory(android.Linux, android.X86, linuxGlibcX86ToolchainFactory)
Ivan Lozanoffee3342019-08-27 12:03:00 -070055
Colin Crosse32f0932022-01-23 20:48:36 -080056 registerToolchainFactory(android.LinuxMusl, android.X86_64, linuxMuslX8664ToolchainFactory)
57 registerToolchainFactory(android.LinuxMusl, android.X86, linuxMuslX86ToolchainFactory)
Colin Cross528d67e2021-07-23 22:23:07 +000058
Ivan Lozanoffee3342019-08-27 12:03:00 -070059 pctx.StaticVariable("LinuxToolchainRustFlags", strings.Join(LinuxRustFlags, " "))
Colin Crosse32f0932022-01-23 20:48:36 -080060 pctx.StaticVariable("LinuxMuslToolchainRustFlags", strings.Join(LinuxMuslRustFlags, " "))
Ivan Lozanoffee3342019-08-27 12:03:00 -070061 pctx.StaticVariable("LinuxToolchainLinkFlags", strings.Join(LinuxRustLinkFlags, " "))
Colin Crosse32f0932022-01-23 20:48:36 -080062 pctx.StaticVariable("LinuxGlibcToolchainLinkFlags", strings.Join(LinuxRustGlibcLinkFlags, " "))
63 pctx.StaticVariable("LinuxMuslToolchainLinkFlags", strings.Join(LinuxRustMuslLinkFlags, " "))
Ivan Lozanoffee3342019-08-27 12:03:00 -070064 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 Crosse32f0932022-01-23 20:48:36 -080071// Base 64-bit linux rust toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -070072type toolchainLinuxX8664 struct {
73 toolchain64Bit
Ivan Lozanoffee3342019-08-27 12:03:00 -070074}
75
76func (toolchainLinuxX8664) Supported() bool {
77 return true
78}
79
Ivan Lozanof1c84332019-09-20 11:00:37 -070080func (toolchainLinuxX8664) Bionic() bool {
81 return false
82}
83
Ivan Lozanoffee3342019-08-27 12:03:00 -070084func (t *toolchainLinuxX8664) Name() string {
85 return "x86_64"
86}
87
Ivan Lozanoffee3342019-08-27 12:03:00 -070088func (t *toolchainLinuxX8664) ToolchainLinkFlags() string {
Ivan Lozano6d45a982020-09-09 09:08:44 -040089 // Prepend the lld flags from cc_config so we stay in sync with cc
Colin Cross0523ba22021-07-14 18:45:05 -070090 return "${cc_config.LinuxLldflags} ${cc_config.LinuxX8664Lldflags} " +
Ivan Lozano6d45a982020-09-09 09:08:44 -040091 "${config.LinuxToolchainLinkFlags} ${config.LinuxToolchainX8664LinkFlags}"
Ivan Lozanoffee3342019-08-27 12:03:00 -070092}
93
94func (t *toolchainLinuxX8664) ToolchainRustFlags() string {
95 return "${config.LinuxToolchainRustFlags} ${config.LinuxToolchainX8664RustFlags}"
96}
97
Colin Crosse32f0932022-01-23 20:48:36 -080098// Specialization of the 64-bit linux rust toolchain for glibc. Adds the gnu rust triple and
99// sysroot linker flags.
100type toolchainLinuxGlibcX8664 struct {
101 toolchainLinuxX8664
102}
103
104func (t *toolchainLinuxX8664) RustTriple() string {
105 return "x86_64-unknown-linux-gnu"
106}
107
108func (t *toolchainLinuxGlibcX8664) ToolchainLinkFlags() string {
109 return t.toolchainLinuxX8664.ToolchainLinkFlags() + " " + "${config.LinuxGlibcToolchainLinkFlags}"
110}
111
112func 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.
118type toolchainLinuxMuslX8664 struct {
119 toolchainLinuxX8664
120}
121
122func (t *toolchainLinuxMuslX8664) RustTriple() string {
123 return "x86_64-unknown-linux-musl"
124}
125
126func (t *toolchainLinuxMuslX8664) ToolchainLinkFlags() string {
127 return t.toolchainLinuxX8664.ToolchainLinkFlags() + " " + "${config.LinuxMuslToolchainLinkFlags}"
128}
129
130func (t *toolchainLinuxMuslX8664) ToolchainRustFlags() string {
131 return t.toolchainLinuxX8664.ToolchainRustFlags() + " " + "${config.LinuxMuslToolchainRustFlags}"
132}
133
134func linuxMuslX8664ToolchainFactory(arch android.Arch) Toolchain {
135 return toolchainLinuxMuslX8664Singleton
136}
137
138// Base 32-bit linux rust toolchain
139type toolchainLinuxX86 struct {
140 toolchain32Bit
Ivan Lozanoffee3342019-08-27 12:03:00 -0700141}
142
143func (toolchainLinuxX86) Supported() bool {
144 return true
145}
146
Ivan Lozanof1c84332019-09-20 11:00:37 -0700147func (toolchainLinuxX86) Bionic() bool {
148 return false
149}
150
Ivan Lozanoffee3342019-08-27 12:03:00 -0700151func (t *toolchainLinuxX86) Name() string {
152 return "x86"
153}
154
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500155func (toolchainLinuxX86) LibclangRuntimeLibraryArch() string {
156 return "i386"
157}
158
159func (toolchainLinuxX8664) LibclangRuntimeLibraryArch() string {
160 return "x86_64"
161}
162
Ivan Lozanoffee3342019-08-27 12:03:00 -0700163func (t *toolchainLinuxX86) ToolchainLinkFlags() string {
Ivan Lozano6d45a982020-09-09 09:08:44 -0400164 // Prepend the lld flags from cc_config so we stay in sync with cc
Colin Cross0523ba22021-07-14 18:45:05 -0700165 return "${cc_config.LinuxLldflags} ${cc_config.LinuxX86Lldflags} " +
Ivan Lozano6d45a982020-09-09 09:08:44 -0400166 "${config.LinuxToolchainLinkFlags} ${config.LinuxToolchainX86LinkFlags}"
Ivan Lozanoffee3342019-08-27 12:03:00 -0700167}
168
169func (t *toolchainLinuxX86) ToolchainRustFlags() string {
170 return "${config.LinuxToolchainRustFlags} ${config.LinuxToolchainX86RustFlags}"
171}
172
Colin Crosse32f0932022-01-23 20:48:36 -0800173// Specialization of the 32-bit linux rust toolchain for glibc. Adds the gnu rust triple and
174// sysroot linker flags.
175type toolchainLinuxGlibcX86 struct {
176 toolchainLinuxX86
Ivan Lozanoffee3342019-08-27 12:03:00 -0700177}
178
Colin Crosse32f0932022-01-23 20:48:36 -0800179func (t *toolchainLinuxGlibcX86) RustTriple() string {
180 return "i686-unknown-linux-gnu"
181}
182
183func (t *toolchainLinuxGlibcX86) ToolchainLinkFlags() string {
184 return t.toolchainLinuxX86.ToolchainLinkFlags() + " " + "${config.LinuxGlibcToolchainLinkFlags}"
185}
186
187func 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.
193type toolchainLinuxMuslX86 struct {
194 toolchainLinuxX86
195}
196
197func (t *toolchainLinuxMuslX86) RustTriple() string {
198 return "i686-unknown-linux-musl"
199}
200
201func (t *toolchainLinuxMuslX86) ToolchainLinkFlags() string {
202 return t.toolchainLinuxX86.ToolchainLinkFlags() + " " + "${config.LinuxMuslToolchainLinkFlags}"
203}
204
205func (t *toolchainLinuxMuslX86) ToolchainRustFlags() string {
206 return t.toolchainLinuxX86.ToolchainRustFlags() + " " + "${config.LinuxMuslToolchainRustFlags}"
207}
208
209func linuxMuslX86ToolchainFactory(arch android.Arch) Toolchain {
210 return toolchainLinuxMuslX86Singleton
211}
212
213var toolchainLinuxGlibcX8664Singleton Toolchain = &toolchainLinuxGlibcX8664{}
214var toolchainLinuxGlibcX86Singleton Toolchain = &toolchainLinuxGlibcX86{}
215var toolchainLinuxMuslX8664Singleton Toolchain = &toolchainLinuxMuslX8664{}
216var toolchainLinuxMuslX86Singleton Toolchain = &toolchainLinuxMuslX86{}