fork/rules_rust

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

UebelAndreDrop references to `wasm-wasi` (#3027)ba6af156

main
14.6 KiB434 linesraw
1"""Helpers for constructing supported Rust platform triples"""
2
3load("//rust/platform:triple.bzl", "triple")
4
5def _support(*, std = False, host_tools = False):
6    """Identify the type of support an associated platform triple has.
7
8    The source of truth is:
9    https://doc.rust-lang.org/nightly/rustc/platform-support.html
10
11    Args:
12        std (bool, optional): Whether or not platform has a standard library artifact.
13        host_tools (bool, optional): Whether or not platform has host tools artifacts.
14
15    Returns:
16        struct: The parameters above.
17    """
18    return struct(
19        std = std,
20        host_tools = host_tools,
21    )
22
23# All T1 Platforms should be supported, but aren't, see inline notes.
24SUPPORTED_T1_PLATFORM_TRIPLES = {
25    "aarch64-apple-darwin": _support(std = True, host_tools = True),
26    "aarch64-unknown-linux-gnu": _support(std = True, host_tools = True),
27    "aarch64-unknown-nixos-gnu": _support(std = True, host_tools = True),  # Same as `aarch64-unknown-linux-gnu` but with `@platforms//os:nixos`.
28    "i686-apple-darwin": _support(std = True, host_tools = True),
29    "i686-pc-windows-msvc": _support(std = True, host_tools = True),
30    "i686-unknown-linux-gnu": _support(std = True, host_tools = True),
31    "x86_64-apple-darwin": _support(std = True, host_tools = True),
32    "x86_64-pc-windows-msvc": _support(std = True, host_tools = True),
33    "x86_64-unknown-linux-gnu": _support(std = True, host_tools = True),
34    "x86_64-unknown-nixos-gnu": _support(std = True, host_tools = True),  # Same as `x86_64-unknown-linux-gnu` but with `@platforms//os:nixos`.
35    # N.B. These "alternative" envs are not supported, as bazel cannot distinguish between them
36    # and others using existing @platforms// config_values
37    #
38    #"i686-pc-windows-gnu",
39    #"x86_64-pc-windows-gnu",
40}
41
42# Some T2 Platforms are supported, provided we have mappings to `@platforms//...` entries.
43# See `@rules_rust//rust/platform:triple_mappings.bzl` for the complete list.
44SUPPORTED_T2_PLATFORM_TRIPLES = {
45    "aarch64-apple-ios": _support(std = True, host_tools = False),
46    "aarch64-apple-ios-sim": _support(std = True, host_tools = False),
47    "aarch64-linux-android": _support(std = True, host_tools = False),
48    "aarch64-pc-windows-msvc": _support(std = True, host_tools = True),
49    "aarch64-unknown-fuchsia": _support(std = True, host_tools = False),
50    "arm-unknown-linux-gnueabi": _support(std = True, host_tools = True),
51    "armv7-linux-androideabi": _support(std = True, host_tools = False),
52    "armv7-unknown-linux-gnueabi": _support(std = True, host_tools = True),
53    "i686-linux-android": _support(std = True, host_tools = False),
54    "i686-unknown-freebsd": _support(std = True, host_tools = False),
55    "powerpc-unknown-linux-gnu": _support(std = True, host_tools = True),
56    "riscv32imc-unknown-none-elf": _support(std = True, host_tools = False),
57    "riscv64gc-unknown-none-elf": _support(std = True, host_tools = False),
58    "s390x-unknown-linux-gnu": _support(std = True, host_tools = True),
59    "thumbv7em-none-eabi": _support(std = True, host_tools = False),
60    "thumbv8m.main-none-eabi": _support(std = True, host_tools = False),
61    "wasm32-unknown-unknown": _support(std = True, host_tools = False),
62    "wasm32-wasip1": _support(std = True, host_tools = False),
63    "x86_64-apple-ios": _support(std = True, host_tools = False),
64    "x86_64-linux-android": _support(std = True, host_tools = False),
65    "x86_64-unknown-freebsd": _support(std = True, host_tools = True),
66    "x86_64-unknown-fuchsia": _support(std = True, host_tools = False),
67    "x86_64-unknown-none": _support(std = True, host_tools = False),
68}
69
70_T3_PLATFORM_TRIPLES = {
71    "aarch64-unknown-nto-qnx710": _support(std = True, host_tools = False),
72    "wasm64-unknown-unknown": _support(std = False, host_tools = False),
73}
74
75# The only T3 triples that are supported are ones with at least a stdlib
76# artifact. However, it can be useful to know of additional triples so
77# this list exists separate from the full list above.
78SUPPORTED_T3_PLATFORM_TRIPLES = {
79    triple: support
80    for triple, support in _T3_PLATFORM_TRIPLES.items()
81    if support.std
82}
83
84SUPPORTED_PLATFORM_TRIPLES = sorted(
85    list(SUPPORTED_T1_PLATFORM_TRIPLES.keys()) +
86    list(SUPPORTED_T2_PLATFORM_TRIPLES.keys()) +
87    list(SUPPORTED_T3_PLATFORM_TRIPLES.keys()),
88)
89
90# Represents all platform triples `rules_rust` is configured to handle in some way.
91# Note that with T3 platforms some artifacts may not be available which can lead to
92# failures in the analysis phase. This list should be used sparingly.
93ALL_PLATFORM_TRIPLES = (
94    list(SUPPORTED_T1_PLATFORM_TRIPLES.keys()) +
95    list(SUPPORTED_T2_PLATFORM_TRIPLES.keys()) +
96    list(_T3_PLATFORM_TRIPLES.keys())
97)
98
99# CPUs that map to a `@platforms//cpu` entry
100_CPU_ARCH_TO_BUILTIN_PLAT_SUFFIX = {
101    "aarch64": "aarch64",
102    "arm": "arm",
103    "arm64e": "arm64e",
104    "armv7": "armv7",
105    "armv7s": None,
106    "asmjs": None,
107    "i386": "i386",
108    "i586": None,
109    "i686": "x86_32",
110    "le32": None,
111    "mips": None,
112    "mipsel": None,
113    "powerpc": "ppc",
114    "powerpc64": None,
115    "powerpc64le": None,
116    "riscv32": "riscv32",
117    "riscv32imc": "riscv32",
118    "riscv64": "riscv64",
119    "riscv64gc": "riscv64",
120    "s390": None,
121    "s390x": "s390x",
122    "thumbv6m": "armv6-m",
123    "thumbv7em": "armv7e-m",
124    "thumbv7m": "armv7-m",
125    "thumbv8m.main": "armv8-m",
126    "wasm32": "wasm32",
127    "wasm64": "wasm64",
128    "x86_64": "x86_64",
129}
130
131# Systems that map to a "@platforms//os entry
132_SYSTEM_TO_BUILTIN_SYS_SUFFIX = {
133    "android": "android",
134    "bitrig": None,
135    "darwin": "osx",
136    "dragonfly": None,
137    "eabi": "none",
138    "eabihf": "none",
139    "emscripten": None,
140    "freebsd": "freebsd",
141    "fuchsia": "fuchsia",
142    "ios": "ios",
143    "linux": "linux",
144    "nacl": None,
145    "netbsd": None,
146    "nixos": "nixos",
147    "none": "none",
148    "nto": "qnx",
149    "openbsd": "openbsd",
150    "solaris": None,
151    "unknown": None,
152    "wasi": None,
153    "wasip1": None,
154    "windows": "windows",
155}
156
157_SYSTEM_TO_BINARY_EXT = {
158    "android": "",
159    "darwin": "",
160    "eabi": "",
161    "eabihf": "",
162    "emscripten": ".js",
163    "freebsd": "",
164    "fuchsia": "",
165    "ios": "",
166    "linux": "",
167    "nixos": "",
168    "none": "",
169    "nto": "",
170    # This is currently a hack allowing us to have the proper
171    # generated extension for the wasm target, similarly to the
172    # windows target
173    "unknown": ".wasm",
174    "wasi": ".wasm",
175    "wasip1": ".wasm",
176    "windows": ".exe",
177}
178
179_SYSTEM_TO_STATICLIB_EXT = {
180    "android": ".a",
181    "darwin": ".a",
182    "eabi": ".a",
183    "eabihf": ".a",
184    "emscripten": ".js",
185    "freebsd": ".a",
186    "fuchsia": ".a",
187    "ios": ".a",
188    "linux": ".a",
189    "nixos": ".a",
190    "none": ".a",
191    "nto": ".a",
192    "unknown": "",
193    "wasi": "",
194    "wasip1": "",
195    "windows": ".lib",
196}
197
198_SYSTEM_TO_DYLIB_EXT = {
199    "android": ".so",
200    "darwin": ".dylib",
201    "eabi": ".so",
202    "eabihf": ".so",
203    "emscripten": ".js",
204    "freebsd": ".so",
205    "fuchsia": ".so",
206    "ios": ".dylib",
207    "linux": ".so",
208    "nixos": ".so",
209    "none": ".so",
210    "nto": ".a",
211    "unknown": ".wasm",
212    "wasi": ".wasm",
213    "wasip1": ".wasm",
214    "windows": ".dll",
215}
216
217# See https://github.com/rust-lang/rust/blob/master/src/libstd/build.rs
218_SYSTEM_TO_STDLIB_LINKFLAGS = {
219    # NOTE: Rust stdlib `build.rs` treats android as a subset of linux, rust rules treat android
220    # as its own system.
221    "android": ["-ldl", "-llog"],
222    "bitrig": [],
223    # TODO(gregbowyer): If rust stdlib is compiled for cloudabi with the backtrace feature it
224    # includes `-lunwind` but this might not actually be required.
225    # I am not sure which is the common configuration or how we encode it as a link flag.
226    "cloudabi": ["-lunwind", "-lc", "-lcompiler_rt"],
227    "darwin": ["-lSystem", "-lresolv"],
228    "dragonfly": ["-lpthread"],
229    "eabi": [],
230    "eabihf": [],
231    "emscripten": [],
232    # TODO(bazelbuild/rules_cc#75):
233    #
234    # Right now bazel cc rules does not specify the exact flag setup needed for calling out system
235    # libs, that is we dont know given a toolchain if it should be, for example,
236    # `-lxxx` or `/Lxxx` or `xxx.lib` etc.
237    #
238    # We include the flag setup as they are _commonly seen_ on various platforms with a cc_rules
239    # style override for people doing things like gnu-mingw on windows.
240    #
241    # If you are reading this ... sorry! set the env var `BAZEL_RUST_STDLIB_LINKFLAGS` to
242    # what you need for your specific setup, for example like so
243    # `BAZEL_RUST_STDLIB_LINKFLAGS="-ladvapi32:-lws2_32:-luserenv"`
244    "freebsd": ["-lexecinfo", "-lpthread"],
245    "fuchsia": ["-lzircon", "-lfdio"],
246    "illumos": ["-lsocket", "-lposix4", "-lpthread", "-lresolv", "-lnsl", "-lumem"],
247    "ios": ["-lSystem", "-lobjc", "-Wl,-framework,Security", "-Wl,-framework,Foundation", "-lresolv"],
248    # TODO: This ignores musl. Longer term what does Bazel think about musl?
249    "linux": ["-ldl", "-lpthread"],
250    "nacl": [],
251    "netbsd": ["-lpthread", "-lrt"],
252    "nixos": ["-ldl", "-lpthread"],  # Same as `linux`.
253    "none": [],
254    "nto": [],
255    "openbsd": ["-lpthread"],
256    "solaris": ["-lsocket", "-lposix4", "-lpthread", "-lresolv"],
257    "unknown": [],
258    "uwp": ["ws2_32.lib"],
259    "wasi": [],
260    "wasip1": [],
261    "windows": ["advapi32.lib", "ws2_32.lib", "userenv.lib", "Bcrypt.lib"],
262}
263
264def cpu_arch_to_constraints(cpu_arch, *, system = None):
265    """Returns a list of contraint values which represents a triple's CPU.
266
267    Args:
268        cpu_arch (str): The architecture to match constraints for
269        system (str, optional): The system for the associated ABI value.
270
271    Returns:
272        List: A list of labels to constraint values
273    """
274    if cpu_arch not in _CPU_ARCH_TO_BUILTIN_PLAT_SUFFIX:
275        fail("CPU architecture \"{}\" is not supported by rules_rust".format(cpu_arch))
276
277    plat_suffix = _CPU_ARCH_TO_BUILTIN_PLAT_SUFFIX[cpu_arch]
278
279    # Patch armv7e-m to mf if hardfloat abi is selected
280    if plat_suffix == "armv7e-m" and system == "eabihf":
281        plat_suffix = "armv7e-mf"
282
283    return ["@platforms//cpu:{}".format(plat_suffix)]
284
285def vendor_to_constraints(_vendor):
286    # TODO(acmcarther): Review:
287    #
288    # My current understanding is that vendors can't have a material impact on
289    # constraint sets.
290    return []
291
292def system_to_constraints(system):
293    if system not in _SYSTEM_TO_BUILTIN_SYS_SUFFIX:
294        fail("System \"{}\" is not supported by rules_rust".format(system))
295
296    sys_suffix = _SYSTEM_TO_BUILTIN_SYS_SUFFIX[system]
297
298    return ["@platforms//os:{}".format(sys_suffix)]
299
300def abi_to_constraints(abi, *, arch = None, system = None):
301    """Return a list of constraint values which represents a triple's ABI.
302
303    Note that some ABI values require additional info to accurately match a set of constraints.
304
305    Args:
306        abi (str): The abi value to match constraints for
307        arch (str, optional): The architecture for the associated ABI value.
308        system (str, optional): The system for the associated ABI value.
309
310    Returns:
311        List: A list of labels to constraint values
312    """
313
314    all_abi_constraints = []
315
316    # add constraints for MUSL static compilation and linking
317    # to separate the MUSL from the non-MUSL toolchain on x86_64
318    # if abi == "musl" and system == "linux" and arch == "x86_64":
319    # all_abi_constraints.append("//rust/platform/constraints:musl_on")
320
321    # add constraints for iOS + watchOS simulator and device triples
322    if system in ["ios", "watchos"]:
323        if arch == "x86_64" or abi == "sim":
324            all_abi_constraints.append("@build_bazel_apple_support//constraints:simulator")
325        else:
326            all_abi_constraints.append("@build_bazel_apple_support//constraints:device")
327
328    # TODO(bazelbuild/platforms#38): Implement when C++ toolchain is more mature and we
329    # figure out how they're doing this
330    return all_abi_constraints
331
332def triple_to_system(target_triple):
333    """Returns a system name for a given platform triple
334
335    **Deprecated**: Use triple() from triple.bzl directly.
336
337    Args:
338        target_triple (str): A platform triple. eg: `x86_64-unknown-linux-gnu`
339
340    Returns:
341        str: A system name
342    """
343    if type(target_triple) == "string":
344        target_triple = triple(target_triple)
345    return target_triple.system
346
347def triple_to_arch(target_triple):
348    """Returns a system architecture name for a given platform triple
349
350    **Deprecated**: Use triple() from triple.bzl directly.
351
352    Args:
353        target_triple (str): A platform triple. eg: `x86_64-unknown-linux-gnu`
354
355    Returns:
356        str: A cpu architecture
357    """
358    if type(target_triple) == "string":
359        target_triple = triple(target_triple)
360    return target_triple.arch
361
362def triple_to_abi(target_triple):
363    """Returns a system abi name for a given platform triple
364
365    **Deprecated**: Use triple() from triple.bzl directly.
366
367    Args:
368        target_triple (str): A platform triple. eg: `x86_64-unknown-linux-gnu`
369
370    Returns:
371        str: The triple's abi
372    """
373    if type(target_triple) == "string":
374        target_triple = triple(target_triple)
375    return target_triple.system
376
377def system_to_dylib_ext(system):
378    return _SYSTEM_TO_DYLIB_EXT[system]
379
380def system_to_staticlib_ext(system):
381    return _SYSTEM_TO_STATICLIB_EXT[system]
382
383def system_to_binary_ext(system):
384    return _SYSTEM_TO_BINARY_EXT[system]
385
386def system_to_stdlib_linkflags(system):
387    return _SYSTEM_TO_STDLIB_LINKFLAGS[system]
388
389def triple_to_constraint_set(target_triple):
390    """Returns a set of constraints for a given platform triple
391
392    Args:
393        target_triple (str): A platform triple. eg: `x86_64-unknown-linux-gnu`
394
395    Returns:
396        list: A list of constraints (each represented by a list of strings)
397    """
398    if target_triple in "wasm32-wasi":
399        return [
400            "@platforms//cpu:wasm32",
401            "@platforms//os:wasi",
402        ]
403    if target_triple == "wasm32-wasip1":
404        return [
405            "@platforms//cpu:wasm32",
406            "@platforms//os:wasi",
407        ]
408    if target_triple == "wasm32-unknown-unknown":
409        return [
410            "@platforms//cpu:wasm32",
411            "@platforms//os:none",
412        ]
413    if target_triple == "wasm64-unknown-unknown":
414        return [
415            "@platforms//cpu:wasm64",
416            "@platforms//os:none",
417        ]
418
419    triple_struct = triple(target_triple)
420
421    constraint_set = []
422    constraint_set += cpu_arch_to_constraints(
423        triple_struct.arch,
424        system = triple_struct.system,
425    )
426    constraint_set += vendor_to_constraints(triple_struct.vendor)
427    constraint_set += system_to_constraints(triple_struct.system)
428    constraint_set += abi_to_constraints(
429        triple_struct.abi,
430        arch = triple_struct.arch,
431        system = triple_struct.system,
432    )
433
434    return constraint_set