fork/rules_rust

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

Gabriel FéronFix typo in function name (#1478)fce12226

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