fork/rules_rust

git clone https://git.julia.blue/fork/rules_rust

cfrantzAdd support for riscv32 targets (#1323)0049ce38

0.5.0
9.0 KiB306 linesraw
1"""Helpers for constructing supported Rust platform triples"""
2
3load("//rust/platform:triple.bzl", "triple")
4
5# All T1 Platforms should be supported, but aren't, see inline notes.
6SUPPORTED_T1_PLATFORM_TRIPLES = [
7    "i686-apple-darwin",
8    "i686-pc-windows-msvc",
9    "i686-unknown-linux-gnu",
10    "x86_64-apple-darwin",
11    "x86_64-pc-windows-msvc",
12    "x86_64-unknown-linux-gnu",
13    # N.B. These "alternative" envs are not supported, as bazel cannot distinguish between them
14    # and others using existing @platforms// config_values
15    #
16    #"i686-pc-windows-gnu",
17    #"x86_64-pc-windows-gnu",
18]
19
20# Some T2 Platforms are supported, provided we have mappings to @platforms// entries.
21# See @rules_rust//rust/platform:triple_mappings.bzl for the complete list.
22SUPPORTED_T2_PLATFORM_TRIPLES = [
23    "aarch64-apple-darwin",
24    "aarch64-apple-ios",
25    "aarch64-apple-ios-sim",
26    "aarch64-linux-android",
27    "aarch64-unknown-linux-gnu",
28    "arm-unknown-linux-gnueabi",
29    "armv7-unknown-linux-gnueabi",
30    "i686-linux-android",
31    "i686-unknown-freebsd",
32    "powerpc-unknown-linux-gnu",
33    "s390x-unknown-linux-gnu",
34    "wasm32-unknown-unknown",
35    "wasm32-wasi",
36    "x86_64-apple-ios",
37    "x86_64-linux-android",
38    "x86_64-unknown-freebsd",
39    "riscv32imc-unknown-none-elf",
40]
41
42SUPPORTED_PLATFORM_TRIPLES = SUPPORTED_T1_PLATFORM_TRIPLES + SUPPORTED_T2_PLATFORM_TRIPLES
43
44# CPUs that map to a "@platforms//cpu entry
45_CPU_ARCH_TO_BUILTIN_PLAT_SUFFIX = {
46    "aarch64": "aarch64",
47    "arm": "arm",
48    "armv7": "armv7",
49    "armv7s": None,
50    "asmjs": None,
51    "i386": "i386",
52    "i586": None,
53    "i686": "x86_32",
54    "le32": None,
55    "mips": None,
56    "mipsel": None,
57    "powerpc": "ppc",
58    "powerpc64": None,
59    "powerpc64le": None,
60    "riscv32": "riscv32",
61    "riscv32imc": "riscv32",
62    "s390": None,
63    "s390x": "s390x",
64    "thumbv7m": "armv7",
65    "wasm32": None,
66    "x86_64": "x86_64",
67}
68
69# Systems that map to a "@platforms//os entry
70_SYSTEM_TO_BUILTIN_SYS_SUFFIX = {
71    "android": "android",
72    "bitrig": None,
73    "darwin": "osx",
74    "dragonfly": None,
75    "eabi": "none",
76    "emscripten": None,
77    "freebsd": "freebsd",
78    "ios": "ios",
79    "linux": "linux",
80    "nacl": None,
81    "netbsd": None,
82    "none": "none",
83    "openbsd": "openbsd",
84    "solaris": None,
85    "unknown": None,
86    "wasi": None,
87    "windows": "windows",
88}
89
90_SYSTEM_TO_BINARY_EXT = {
91    "android": "",
92    "darwin": "",
93    "eabi": "",
94    "emscripten": ".js",
95    "freebsd": "",
96    "ios": "",
97    "linux": "",
98    "none": "",
99    # This is currently a hack allowing us to have the proper
100    # generated extension for the wasm target, similarly to the
101    # windows target
102    "unknown": ".wasm",
103    "wasi": ".wasm",
104    "windows": ".exe",
105}
106
107_SYSTEM_TO_STATICLIB_EXT = {
108    "android": ".a",
109    "darwin": ".a",
110    "eabi": ".a",
111    "emscripten": ".js",
112    "freebsd": ".a",
113    "ios": ".a",
114    "linux": ".a",
115    "none": ".a",
116    "unknown": "",
117    "wasi": "",
118    "windows": ".lib",
119}
120
121_SYSTEM_TO_DYLIB_EXT = {
122    "android": ".so",
123    "darwin": ".dylib",
124    "eabi": ".so",
125    "emscripten": ".js",
126    "freebsd": ".so",
127    "ios": ".dylib",
128    "linux": ".so",
129    "none": ".so",
130    "unknown": ".wasm",
131    "wasi": ".wasm",
132    "windows": ".dll",
133}
134
135# See https://github.com/rust-lang/rust/blob/master/src/libstd/build.rs
136_SYSTEM_TO_STDLIB_LINKFLAGS = {
137    # NOTE: Rust stdlib `build.rs` treats android as a subset of linux, rust rules treat android
138    # as its own system.
139    "android": ["-ldl", "-llog", "-lgcc"],
140    "bitrig": [],
141    # TODO(gregbowyer): If rust stdlib is compiled for cloudabi with the backtrace feature it
142    # includes `-lunwind` but this might not actually be required.
143    # I am not sure which is the common configuration or how we encode it as a link flag.
144    "cloudabi": ["-lunwind", "-lc", "-lcompiler_rt"],
145    "darwin": ["-lSystem", "-lresolv"],
146    "dragonfly": ["-lpthread"],
147    "eabi": [],
148    "emscripten": [],
149    # TODO(bazelbuild/rules_cc#75):
150    #
151    # Right now bazel cc rules does not specify the exact flag setup needed for calling out system
152    # libs, that is we dont know given a toolchain if it should be, for example,
153    # `-lxxx` or `/Lxxx` or `xxx.lib` etc.
154    #
155    # We include the flag setup as they are _commonly seen_ on various platforms with a cc_rules
156    # style override for people doing things like gnu-mingw on windows.
157    #
158    # If you are reading this ... sorry! set the env var `BAZEL_RUST_STDLIB_LINKFLAGS` to
159    # what you need for your specific setup, for example like so
160    # `BAZEL_RUST_STDLIB_LINKFLAGS="-ladvapi32:-lws2_32:-luserenv"`
161    "freebsd": ["-lexecinfo", "-lpthread"],
162    "fuchsia": ["-lzircon", "-lfdio"],
163    "illumos": ["-lsocket", "-lposix4", "-lpthread", "-lresolv", "-lnsl", "-lumem"],
164    "ios": ["-lSystem", "-lobjc", "-Wl,-framework,Security", "-Wl,-framework,Foundation", "-lresolv"],
165    # TODO: This ignores musl. Longer term what does Bazel think about musl?
166    "linux": ["-ldl", "-lpthread"],
167    "nacl": [],
168    "netbsd": ["-lpthread", "-lrt"],
169    "none": [],
170    "openbsd": ["-lpthread"],
171    "solaris": ["-lsocket", "-lposix4", "-lpthread", "-lresolv"],
172    "unknown": [],
173    "uwp": ["ws2_32.lib"],
174    "wasi": [],
175    "windows": ["advapi32.lib", "ws2_32.lib", "userenv.lib", "Bcrypt.lib"],
176}
177
178def cpu_arch_to_constraints(cpu_arch):
179    plat_suffix = _CPU_ARCH_TO_BUILTIN_PLAT_SUFFIX[cpu_arch]
180
181    if not plat_suffix:
182        fail("CPU architecture \"{}\" is not supported by rules_rust".format(cpu_arch))
183
184    return ["@platforms//cpu:{}".format(plat_suffix)]
185
186def vendor_to_constraints(_vendor):
187    # TODO(acmcarther): Review:
188    #
189    # My current understanding is that vendors can't have a material impact on
190    # constraint sets.
191    return []
192
193def system_to_constraints(system):
194    sys_suffix = _SYSTEM_TO_BUILTIN_SYS_SUFFIX[system]
195
196    if not sys_suffix:
197        fail("System \"{}\" is not supported by rules_rust".format(sys_suffix))
198
199    return ["@platforms//os:{}".format(sys_suffix)]
200
201def abi_to_constraints(_abi):
202    # TODO(acmcarther): Implement when C++ toolchain is more mature and we
203    # figure out how they're doing this
204    return []
205
206def extra_ios_constriants(triple):
207    """Add constraints specific to iOS targets.
208
209    Args:
210        triple: The full triple struct for the target
211
212    Returns:
213        A list of constraints to add to the target
214    """
215
216    # TODO: Simplify if https://github.com/bazelbuild/bazel/issues/11454 is fixed
217    if triple.system != "ios":
218        return []
219    if triple.abi == "sim":
220        return ["@build_bazel_apple_support//constraints:simulator"]
221    elif triple.arch == "aarch64":  # Only add device for archs that have both
222        return ["@build_bazel_apple_support//constraints:device"]
223    else:
224        return []
225
226def triple_to_system(target_triple):
227    """Returns a system name for a given platform triple
228
229    **Deprecated**: Use triple() from triple.bzl directly.
230
231    Args:
232        target_triple (str): A platform triple. eg: `x86_64-unknown-linux-gnu`
233
234    Returns:
235        str: A system name
236    """
237    return triple(target_triple).system
238
239def triple_to_arch(target_triple):
240    """Returns a system architecture name for a given platform triple
241
242    **Deprecated**: Use triple() from triple.bzl directly.
243
244    Args:
245        target_triple (str): A platform triple. eg: `x86_64-unknown-linux-gnu`
246
247    Returns:
248        str: A cpu architecture
249    """
250    return triple(target_triple).arch
251
252def triple_to_abi(target_triple):
253    """Returns a system abi name for a given platform triple
254
255    **Deprecated**: Use triple() from triple.bzl directly.
256
257    Args:
258        target_triple (str): A platform triple. eg: `x86_64-unknown-linux-gnu`
259
260    Returns:
261        str: The triple's abi
262    """
263    return triple(target_triple).system
264
265def system_to_dylib_ext(system):
266    return _SYSTEM_TO_DYLIB_EXT[system]
267
268def system_to_staticlib_ext(system):
269    return _SYSTEM_TO_STATICLIB_EXT[system]
270
271def system_to_binary_ext(system):
272    return _SYSTEM_TO_BINARY_EXT[system]
273
274def system_to_stdlib_linkflags(system):
275    return _SYSTEM_TO_STDLIB_LINKFLAGS[system]
276
277def triple_to_constraint_set(target_triple):
278    """Returns a set of constraints for a given platform triple
279
280    Args:
281        target_triple (str): A platform triple. eg: `x86_64-unknown-linux-gnu`
282
283    Returns:
284        list: A list of constraints (each represented by a list of strings)
285    """
286    if target_triple == "wasm32-wasi":
287        return [
288            "@rules_rust//rust/platform/cpu:wasm32",
289            "@rules_rust//rust/platform/os:wasi",
290        ]
291    if target_triple == "wasm32-unknown-unknown":
292        return [
293            "@rules_rust//rust/platform/cpu:wasm32",
294            "@rules_rust//rust/platform/os:unknown",
295        ]
296
297    triple_struct = triple(target_triple)
298
299    constraint_set = []
300    constraint_set += cpu_arch_to_constraints(triple_struct.arch)
301    constraint_set += vendor_to_constraints(triple_struct.vendor)
302    constraint_set += system_to_constraints(triple_struct.system)
303    constraint_set += abi_to_constraints(triple_struct.abi)
304    constraint_set += extra_ios_constriants(triple_struct)
305
306    return constraint_set