fork/rules_rust

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

UebelAndreAdded update known shas to include T1-T2 triples (#1824)4fa41238

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