fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
4fa41238
rust-analyzer
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 ): 185plat_suffix = _CPU_ARCH_TO_BUILTIN_PLAT_SUFFIX [ cpu_arch ] 186 187if not plat_suffix : 188fail ( "CPU architecture \"{}\" is not supported by rules_rust" . format ( cpu_arch )) 189 190return [ "@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. 197return [] 198 199def system_to_constraints ( system ): 200sys_suffix = _SYSTEM_TO_BUILTIN_SYS_SUFFIX [ system ] 201 202if not sys_suffix : 203fail ( "System \"{}\" is not supported by rules_rust" . format ( sys_suffix )) 204 205return [ "@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 210Note that some ABI values require additional info to accurately match a set of constraints. 211 212Args: 213abi (str): The abi value to match constraints for 214arch (str, optional): The architecture for the associated ABI value. 215system (str, optional): The system for the associated ABI value. 216 217Returns: 218List: A list of labels to constraint values 219""" 220 221if abi == "sim" : 222if not system : 223fail ( "The ABI value {} is ambiguous. Please specify a system to match the right constraints." ) 224if not arch : 225fail ( "The ABI value {} is ambiguous. Please specify an architecture to match the right constraints." ) 226if system == "ios" : 227if abi == "sim" : 228return [ "@build_bazel_apple_support//constraints:simulator" ] 229elif arch == "aarch64" : # Only add device for archs that have both 230return [ "@build_bazel_apple_support//constraints:device" ] 231else : 232return [] 233 234# TODO(bazelbuild/platforms#38): Implement when C++ toolchain is more mature and we 235# figure out how they're doing this 236return [] 237 238def extra_ios_constraints ( triple ): 239"""Add constraints specific to iOS targets. 240 241Deprecated: Instead, use `abi_to_constraints` 242 243Args: 244triple: The full triple struct for the target 245 246Returns: 247A list of constraints to add to the target 248""" 249 250# TODO: Simplify if https://github.com/bazelbuild/bazel/issues/11454 is fixed 251if triple . system != "ios" : 252return [] 253 254return 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 261Args: 262target_triple (str): A platform triple. eg: `x86_64-unknown-linux-gnu` 263 264Returns: 265str: A system name 266""" 267if type ( target_triple ) == "string" : 268target_triple = triple ( target_triple ) 269return 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 276Args: 277target_triple (str): A platform triple. eg: `x86_64-unknown-linux-gnu` 278 279Returns: 280str: A cpu architecture 281""" 282if type ( target_triple ) == "string" : 283target_triple = triple ( target_triple ) 284return 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 291Args: 292target_triple (str): A platform triple. eg: `x86_64-unknown-linux-gnu` 293 294Returns: 295str: The triple's abi 296""" 297if type ( target_triple ) == "string" : 298target_triple = triple ( target_triple ) 299return target_triple . system 300 301def system_to_dylib_ext ( system ): 302return _SYSTEM_TO_DYLIB_EXT [ system ] 303 304def system_to_staticlib_ext ( system ): 305return _SYSTEM_TO_STATICLIB_EXT [ system ] 306 307def system_to_binary_ext ( system ): 308return _SYSTEM_TO_BINARY_EXT [ system ] 309 310def system_to_stdlib_linkflags ( system ): 311return _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 316Args: 317target_triple (str): A platform triple. eg: `x86_64-unknown-linux-gnu` 318 319Returns: 320list: A list of constraints (each represented by a list of strings) 321""" 322if target_triple == "wasm32-wasi" : 323return [ 324"@platforms//cpu:wasm32" , 325"@platforms//os:wasi" , 326] 327if target_triple == "wasm32-unknown-unknown" : 328return [ 329"@platforms//cpu:wasm32" , 330"@rules_rust//rust/platform/os:unknown" , 331] 332 333triple_struct = triple ( target_triple ) 334 335constraint_set = [] 336constraint_set += cpu_arch_to_constraints ( triple_struct . arch ) 337constraint_set += vendor_to_constraints ( triple_struct . vendor ) 338constraint_set += system_to_constraints ( triple_struct . system ) 339constraint_set += abi_to_constraints ( 340triple_struct . abi , 341arch = triple_struct . arch , 342system = triple_struct . system , 343) 344 345return constraint_set