fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
ba6af156
main
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 8The source of truth is: 9https://doc.rust-lang.org/nightly/rustc/platform-support.html 10 11Args: 12std (bool, optional): Whether or not platform has a standard library artifact. 13host_tools (bool, optional): Whether or not platform has host tools artifacts. 14 15Returns: 16struct: The parameters above. 17""" 18return struct ( 19std = std , 20host_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 = { 79triple : support 80for triple , support in _T3_PLATFORM_TRIPLES . items () 81if support . std 82} 83 84SUPPORTED_PLATFORM_TRIPLES = sorted ( 85list ( SUPPORTED_T1_PLATFORM_TRIPLES . keys ()) + 86list ( SUPPORTED_T2_PLATFORM_TRIPLES . keys ()) + 87list ( 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 = ( 94list ( SUPPORTED_T1_PLATFORM_TRIPLES . keys ()) + 95list ( SUPPORTED_T2_PLATFORM_TRIPLES . keys ()) + 96list ( _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 267Args: 268cpu_arch (str): The architecture to match constraints for 269system (str, optional): The system for the associated ABI value. 270 271Returns: 272List: A list of labels to constraint values 273""" 274if cpu_arch not in _CPU_ARCH_TO_BUILTIN_PLAT_SUFFIX : 275fail ( "CPU architecture \"{}\ " is not supported by rules_rust" . format ( cpu_arch )) 276 277plat_suffix = _CPU_ARCH_TO_BUILTIN_PLAT_SUFFIX [ cpu_arch ] 278 279# Patch armv7e-m to mf if hardfloat abi is selected 280if plat_suffix == "armv7e-m" and system == "eabihf" : 281plat_suffix = "armv7e-mf" 282 283return [ "@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. 290return [] 291 292def system_to_constraints ( system ): 293if system not in _SYSTEM_TO_BUILTIN_SYS_SUFFIX : 294fail ( "System \"{}\ " is not supported by rules_rust" . format ( system )) 295 296sys_suffix = _SYSTEM_TO_BUILTIN_SYS_SUFFIX [ system ] 297 298return [ "@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 303Note that some ABI values require additional info to accurately match a set of constraints. 304 305Args: 306abi (str): The abi value to match constraints for 307arch (str, optional): The architecture for the associated ABI value. 308system (str, optional): The system for the associated ABI value. 309 310Returns: 311List: A list of labels to constraint values 312""" 313 314all_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 322if system in [ "ios" , "watchos" ]: 323if arch == "x86_64" or abi == "sim" : 324all_abi_constraints . append ( "@build_bazel_apple_support//constraints:simulator" ) 325else : 326all_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 330return 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 337Args: 338target_triple (str): A platform triple. eg: `x86_64-unknown-linux-gnu` 339 340Returns: 341str: A system name 342""" 343if type ( target_triple ) == "string" : 344target_triple = triple ( target_triple ) 345return 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 352Args: 353target_triple (str): A platform triple. eg: `x86_64-unknown-linux-gnu` 354 355Returns: 356str: A cpu architecture 357""" 358if type ( target_triple ) == "string" : 359target_triple = triple ( target_triple ) 360return 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 367Args: 368target_triple (str): A platform triple. eg: `x86_64-unknown-linux-gnu` 369 370Returns: 371str: The triple's abi 372""" 373if type ( target_triple ) == "string" : 374target_triple = triple ( target_triple ) 375return target_triple . system 376 377def system_to_dylib_ext ( system ): 378return _SYSTEM_TO_DYLIB_EXT [ system ] 379 380def system_to_staticlib_ext ( system ): 381return _SYSTEM_TO_STATICLIB_EXT [ system ] 382 383def system_to_binary_ext ( system ): 384return _SYSTEM_TO_BINARY_EXT [ system ] 385 386def system_to_stdlib_linkflags ( system ): 387return _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 392Args: 393target_triple (str): A platform triple. eg: `x86_64-unknown-linux-gnu` 394 395Returns: 396list: A list of constraints (each represented by a list of strings) 397""" 398if target_triple in "wasm32-wasi" : 399return [ 400"@platforms//cpu:wasm32" , 401"@platforms//os:wasi" , 402] 403if target_triple == "wasm32-wasip1" : 404return [ 405"@platforms//cpu:wasm32" , 406"@platforms//os:wasi" , 407] 408if target_triple == "wasm32-unknown-unknown" : 409return [ 410"@platforms//cpu:wasm32" , 411"@platforms//os:none" , 412] 413if target_triple == "wasm64-unknown-unknown" : 414return [ 415"@platforms//cpu:wasm64" , 416"@platforms//os:none" , 417] 418 419triple_struct = triple ( target_triple ) 420 421constraint_set = [] 422constraint_set += cpu_arch_to_constraints ( 423triple_struct . arch , 424system = triple_struct . system , 425) 426constraint_set += vendor_to_constraints ( triple_struct . vendor ) 427constraint_set += system_to_constraints ( triple_struct . system ) 428constraint_set += abi_to_constraints ( 429triple_struct . abi , 430arch = triple_struct . arch , 431system = triple_struct . system , 432) 433 434return constraint_set