fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
1cab691d
0.6.0
1"""Utility macros for use in rules_rust repository rules""" 2 3load ( "//rust:known_shas.bzl" , "FILE_KEY_TO_SHA" ) 4load ( 5"//rust/platform:triple_mappings.bzl" , 6"system_to_binary_ext" , 7"system_to_dylib_ext" , 8"system_to_staticlib_ext" , 9"system_to_stdlib_linkflags" , 10"triple_to_constraint_set" , 11"triple_to_system" , 12) 13 14DEFAULT_TOOLCHAIN_NAME_PREFIX = "toolchain_for" 15DEFAULT_STATIC_RUST_URL_TEMPLATES = [ "https://static.rust-lang.org/dist/{}.tar.gz" ] 16 17_build_file_for_compiler_template = """\ 18load("@rules_rust//rust:toolchain.bzl", "rust_toolchain") 19 20filegroup( 21name = "rustc", 22srcs = ["bin/rustc{binary_ext}"], 23visibility = ["//visibility:public"], 24) 25 26filegroup( 27name = "rustc_lib", 28srcs = glob( 29[ 30"bin/*{dylib_ext}", 31"lib/*{dylib_ext}", 32"lib/rustlib/{target_triple}/codegen-backends/*{dylib_ext}", 33"lib/rustlib/{target_triple}/bin/rust-lld{binary_ext}", 34"lib/rustlib/{target_triple}/lib/*{dylib_ext}", 35], 36allow_empty = True, 37), 38visibility = ["//visibility:public"], 39) 40 41filegroup( 42name = "rustdoc", 43srcs = ["bin/rustdoc{binary_ext}"], 44visibility = ["//visibility:public"], 45) 46""" 47 48def BUILD_for_compiler ( target_triple ): 49"""Emits a BUILD file the compiler archive. 50 51Args: 52target_triple (str): The triple of the target platform 53 54Returns: 55str: The contents of a BUILD file 56""" 57system = triple_to_system ( target_triple ) 58return _build_file_for_compiler_template . format ( 59binary_ext = system_to_binary_ext ( system ), 60staticlib_ext = system_to_staticlib_ext ( system ), 61dylib_ext = system_to_dylib_ext ( system ), 62target_triple = target_triple , 63) 64 65_build_file_for_cargo_template = """\ 66load("@rules_rust//rust:toolchain.bzl", "rust_toolchain") 67 68filegroup( 69name = "cargo", 70srcs = ["bin/cargo{binary_ext}"], 71visibility = ["//visibility:public"], 72)""" 73 74def BUILD_for_cargo ( target_triple ): 75"""Emits a BUILD file the cargo archive. 76 77Args: 78target_triple (str): The triple of the target platform 79 80Returns: 81str: The contents of a BUILD file 82""" 83system = triple_to_system ( target_triple ) 84return _build_file_for_cargo_template . format ( 85binary_ext = system_to_binary_ext ( system ), 86) 87 88_build_file_for_rustfmt_template = """\ 89load("@rules_rust//rust:toolchain.bzl", "rust_toolchain") 90 91filegroup( 92name = "rustfmt_bin", 93srcs = ["bin/rustfmt{binary_ext}"], 94visibility = ["//visibility:public"], 95) 96 97sh_binary( 98name = "rustfmt", 99srcs = [":rustfmt_bin"], 100visibility = ["//visibility:public"], 101) 102""" 103 104def BUILD_for_rustfmt ( target_triple ): 105"""Emits a BUILD file the rustfmt archive. 106 107Args: 108target_triple (str): The triple of the target platform 109 110Returns: 111str: The contents of a BUILD file 112""" 113system = triple_to_system ( target_triple ) 114return _build_file_for_rustfmt_template . format ( 115binary_ext = system_to_binary_ext ( system ), 116) 117 118_build_file_for_clippy_template = """\ 119load("@rules_rust//rust:toolchain.bzl", "rust_toolchain") 120 121filegroup( 122name = "clippy_driver_bin", 123srcs = ["bin/clippy-driver{binary_ext}"], 124visibility = ["//visibility:public"], 125) 126""" 127 128def BUILD_for_clippy ( target_triple ): 129"""Emits a BUILD file the clippy archive. 130 131Args: 132target_triple (str): The triple of the target platform 133 134Returns: 135str: The contents of a BUILD file 136""" 137system = triple_to_system ( target_triple ) 138return _build_file_for_clippy_template . format ( binary_ext = system_to_binary_ext ( system )) 139 140_build_file_for_stdlib_template = """\ 141load("@rules_rust//rust:toolchain.bzl", "rust_stdlib_filegroup") 142 143rust_stdlib_filegroup( 144name = "rust_std-{target_triple}", 145srcs = glob( 146[ 147"lib/rustlib/{target_triple}/lib/*.rlib", 148"lib/rustlib/{target_triple}/lib/*{dylib_ext}", 149"lib/rustlib/{target_triple}/lib/*{staticlib_ext}", 150"lib/rustlib/{target_triple}/lib/self-contained/**", 151], 152# Some patterns (e.g. `lib/*.a`) don't match anything, see https://github.com/bazelbuild/rules_rust/pull/245 153allow_empty = True, 154), 155visibility = ["//visibility:public"], 156) 157 158# For legacy support 159alias( 160name = "rust_lib-{target_triple}", 161actual = "rust_std-{target_triple}", 162visibility = ["//visibility:public"], 163) 164""" 165 166def BUILD_for_stdlib ( target_triple ): 167"""Emits a BUILD file the stdlib archive. 168 169Args: 170target_triple (str): The triple of the target platform 171 172Returns: 173str: The contents of a BUILD file 174""" 175system = triple_to_system ( target_triple ) 176return _build_file_for_stdlib_template . format ( 177binary_ext = system_to_binary_ext ( system ), 178staticlib_ext = system_to_staticlib_ext ( system ), 179dylib_ext = system_to_dylib_ext ( system ), 180target_triple = target_triple , 181) 182 183_build_file_for_rust_toolchain_template = """\ 184rust_toolchain( 185name = "{toolchain_name}_impl", 186rust_doc = "@{workspace_name}//:rustdoc", 187rust_std = "@{workspace_name}//:rust_std-{target_triple}", 188rustc = "@{workspace_name}//:rustc", 189rustfmt = {rustfmt_label}, 190cargo = "@{workspace_name}//:cargo", 191clippy_driver = "@{workspace_name}//:clippy_driver_bin", 192rustc_lib = "@{workspace_name}//:rustc_lib", 193rustc_srcs = {rustc_srcs}, 194binary_ext = "{binary_ext}", 195staticlib_ext = "{staticlib_ext}", 196dylib_ext = "{dylib_ext}", 197stdlib_linkflags = [{stdlib_linkflags}], 198os = "{system}", 199default_edition = "{default_edition}", 200exec_triple = "{exec_triple}", 201target_triple = "{target_triple}", 202visibility = ["//visibility:public"], 203) 204""" 205 206def BUILD_for_rust_toolchain ( 207workspace_name , 208name , 209exec_triple , 210target_triple , 211include_rustc_srcs , 212default_edition , 213include_rustfmt , 214stdlib_linkflags = None ): 215"""Emits a toolchain declaration to match an existing compiler and stdlib. 216 217Args: 218workspace_name (str): The name of the workspace that this toolchain resides in 219name (str): The name of the toolchain declaration 220exec_triple (str): The rust-style target that this compiler runs on 221target_triple (str): The rust-style target triple of the tool 222include_rustc_srcs (bool, optional): Whether to download rustc's src code. This is required in order to use rust-analyzer support. Defaults to False. 223default_edition (str): Default Rust edition. 224include_rustfmt (bool): Whether rustfmt is present in the toolchain. 225stdlib_linkflags (list, optional): Overriden flags needed for linking to rust 226stdlib, akin to BAZEL_LINKLIBS. Defaults to 227None. 228 229 230Returns: 231str: A rendered template of a `rust_toolchain` declaration 232""" 233system = triple_to_system ( target_triple ) 234if stdlib_linkflags == None : 235stdlib_linkflags = ", " . join ([ '"%s"' % x for x in system_to_stdlib_linkflags ( system )]) 236 237rustc_srcs = "None" 238if include_rustc_srcs : 239rustc_srcs = "\"@{workspace_name}//lib/rustlib/src:rustc_srcs\"" . format ( workspace_name = workspace_name ) 240rustfmt_label = "None" 241if include_rustfmt : 242rustfmt_label = "\"@{workspace_name}//:rustfmt_bin\"" . format ( workspace_name = workspace_name ) 243 244return _build_file_for_rust_toolchain_template . format ( 245toolchain_name = name , 246workspace_name = workspace_name , 247binary_ext = system_to_binary_ext ( system ), 248staticlib_ext = system_to_staticlib_ext ( system ), 249dylib_ext = system_to_dylib_ext ( system ), 250rustc_srcs = rustc_srcs , 251stdlib_linkflags = stdlib_linkflags , 252system = system , 253default_edition = default_edition , 254exec_triple = exec_triple , 255target_triple = target_triple , 256rustfmt_label = rustfmt_label , 257) 258 259_build_file_for_toolchain_template = """\ 260toolchain( 261name = "{name}", 262exec_compatible_with = {exec_constraint_sets_serialized}, 263target_compatible_with = {target_constraint_sets_serialized}, 264toolchain = "@{parent_workspace_name}//:{name}_impl", 265toolchain_type = "@rules_rust//rust:toolchain", 266) 267""" 268 269def BUILD_for_toolchain ( name , parent_workspace_name , exec_triple , target_triple ): 270return _build_file_for_toolchain_template . format ( 271name = name , 272exec_constraint_sets_serialized = serialized_constraint_set_from_triple ( exec_triple ), 273target_constraint_sets_serialized = serialized_constraint_set_from_triple ( target_triple ), 274parent_workspace_name = parent_workspace_name , 275) 276 277def load_rustfmt ( ctx ): 278"""Loads a rustfmt binary and yields corresponding BUILD for it 279 280Args: 281ctx (repository_ctx): The repository rule's context object 282 283Returns: 284str: The BUILD file contents for this rustfmt binary 285""" 286target_triple = ctx . attr . exec_triple 287 288load_arbitrary_tool ( 289ctx , 290iso_date = ctx . attr . iso_date , 291target_triple = target_triple , 292tool_name = "rustfmt" , 293tool_subdirectories = [ "rustfmt-preview" ], 294version = ctx . attr . rustfmt_version , 295) 296 297return BUILD_for_rustfmt ( target_triple ) 298 299def load_rust_compiler ( ctx ): 300"""Loads a rust compiler and yields corresponding BUILD for it 301 302Args: 303ctx (repository_ctx): A repository_ctx. 304 305Returns: 306str: The BUILD file contents for this compiler and compiler library 307""" 308 309target_triple = ctx . attr . exec_triple 310load_arbitrary_tool ( 311ctx , 312iso_date = ctx . attr . iso_date , 313target_triple = target_triple , 314tool_name = "rust" , 315tool_subdirectories = [ "rustc" , "clippy-preview" , "cargo" ], 316version = ctx . attr . version , 317) 318 319compiler_build_file = BUILD_for_compiler ( target_triple ) + BUILD_for_clippy ( target_triple ) + BUILD_for_cargo ( target_triple ) 320 321return compiler_build_file 322 323def should_include_rustc_srcs ( repository_ctx ): 324"""Determing whether or not to include rustc sources in the toolchain. 325 326Args: 327repository_ctx (repository_ctx): The repository rule's context object 328 329Returns: 330bool: Whether or not to include rustc source files in a `rustc_toolchain` 331""" 332 333# The environment variable will always take precedence over the attribute. 334include_rustc_srcs_env = repository_ctx . os . environ . get ( "RULES_RUST_TOOLCHAIN_INCLUDE_RUSTC_SRCS" ) 335if include_rustc_srcs_env != None : 336return include_rustc_srcs_env . lower () in [ "true" , "1" ] 337 338return getattr ( repository_ctx . attr , "include_rustc_srcs" , False ) 339 340def load_rust_src ( ctx ): 341"""Loads the rust source code. Used by the rust-analyzer rust-project.json generator. 342 343Args: 344ctx (ctx): A repository_ctx. 345""" 346tool_suburl = produce_tool_suburl ( "rust-src" , None , ctx . attr . version , ctx . attr . iso_date ) 347url = ctx . attr . urls [ 0 ]. format ( tool_suburl ) 348 349tool_path = produce_tool_path ( "rust-src" , None , ctx . attr . version ) 350archive_path = tool_path + _get_tool_extension ( ctx ) 351ctx . download ( 352url , 353output = archive_path , 354sha256 = ctx . attr . sha256s . get ( archive_path ) or FILE_KEY_TO_SHA . get ( archive_path ) or "" , 355auth = _make_auth_dict ( ctx , [ url ]), 356) 357ctx . extract ( 358archive_path , 359output = "lib/rustlib/src" , 360stripPrefix = "{}/rust-src/lib/rustlib/src/rust" . format ( tool_path ), 361) 362ctx . file ( 363"lib/rustlib/src/BUILD.bazel" , 364"""\ 365filegroup( 366name = "rustc_srcs", 367srcs = glob(["**/*"]), 368visibility = ["//visibility:public"], 369)""" , 370) 371 372def load_rust_stdlib ( ctx , target_triple ): 373"""Loads a rust standard library and yields corresponding BUILD for it 374 375Args: 376ctx (repository_ctx): A repository_ctx. 377target_triple (str): The rust-style target triple of the tool 378 379Returns: 380str: The BUILD file contents for this stdlib, and a toolchain decl to match 381""" 382 383load_arbitrary_tool ( 384ctx , 385iso_date = ctx . attr . iso_date , 386target_triple = target_triple , 387tool_name = "rust-std" , 388tool_subdirectories = [ "rust-std-{}" . format ( target_triple )], 389version = ctx . attr . version , 390) 391 392toolchain_prefix = ctx . attr . toolchain_name_prefix or DEFAULT_TOOLCHAIN_NAME_PREFIX 393stdlib_build_file = BUILD_for_stdlib ( target_triple ) 394 395stdlib_linkflags = None 396if "BAZEL_RUST_STDLIB_LINKFLAGS" in ctx . os . environ : 397stdlib_linkflags = ctx . os . environ [ "BAZEL_RUST_STDLIB_LINKFLAGS" ]. split ( ":" ) 398 399toolchain_build_file = BUILD_for_rust_toolchain ( 400name = "{toolchain_prefix}_{target_triple}" . format ( 401toolchain_prefix = toolchain_prefix , 402target_triple = target_triple , 403), 404exec_triple = ctx . attr . exec_triple , 405include_rustc_srcs = should_include_rustc_srcs ( ctx ), 406target_triple = target_triple , 407stdlib_linkflags = stdlib_linkflags , 408workspace_name = ctx . attr . name , 409default_edition = ctx . attr . edition , 410include_rustfmt = not ( not ctx . attr . rustfmt_version ), 411) 412 413return stdlib_build_file + toolchain_build_file 414 415def load_rustc_dev_nightly ( ctx , target_triple ): 416"""Loads the nightly rustc dev component 417 418Args: 419ctx: A repository_ctx. 420target_triple: The rust-style target triple of the tool 421""" 422 423subdir_name = "rustc-dev" 424if ctx . attr . iso_date < "2020-12-24" : 425subdir_name = "rustc-dev-{}" . format ( target_triple ) 426 427load_arbitrary_tool ( 428ctx , 429iso_date = ctx . attr . iso_date , 430target_triple = target_triple , 431tool_name = "rustc-dev" , 432tool_subdirectories = [ subdir_name ], 433version = ctx . attr . version , 434) 435 436def load_llvm_tools ( ctx , target_triple ): 437"""Loads the llvm tools 438 439Args: 440ctx: A repository_ctx. 441target_triple: The rust-style target triple of the tool 442""" 443load_arbitrary_tool ( 444ctx , 445iso_date = ctx . attr . iso_date , 446target_triple = target_triple , 447tool_name = "llvm-tools" , 448tool_subdirectories = [ "llvm-tools-preview" ], 449version = ctx . attr . version , 450) 451 452def check_version_valid ( version , iso_date , param_prefix = "" ): 453"""Verifies that the provided rust version and iso_date make sense. 454 455Args: 456version (str): The rustc version 457iso_date (str): The rustc nightly version's iso date 458param_prefix (str, optional): The name of the tool who's version is being checked. 459""" 460 461if not version and iso_date : 462fail ( "{param_prefix}iso_date must be paired with a {param_prefix}version" . format ( param_prefix = param_prefix )) 463 464if version in ( "beta" , "nightly" ) and not iso_date : 465fail ( "{param_prefix}iso_date must be specified if version is 'beta' or 'nightly'" . format ( param_prefix = param_prefix )) 466 467def serialized_constraint_set_from_triple ( target_triple ): 468"""Returns a string representing a set of constraints 469 470Args: 471target_triple (str): The target triple of the constraint set 472 473Returns: 474str: Formatted string representing the serialized constraint 475""" 476constraint_set = triple_to_constraint_set ( target_triple ) 477constraint_set_strs = [] 478for constraint in constraint_set : 479constraint_set_strs . append ( "\"{}\"" . format ( constraint )) 480return "[{}]" . format ( ", " . join ( constraint_set_strs )) 481 482def produce_tool_suburl ( tool_name , target_triple , version , iso_date = None ): 483"""Produces a fully qualified Rust tool name for URL 484 485Args: 486tool_name: The name of the tool per static.rust-lang.org 487target_triple: The rust-style target triple of the tool 488version: The version of the tool among "nightly", "beta', or an exact version. 489iso_date: The date of the tool (or None, if the version is a specific version). 490 491Returns: 492str: The fully qualified url path for the specified tool. 493""" 494path = produce_tool_path ( tool_name , target_triple , version ) 495return iso_date + "/" + path if ( iso_date and version in ( "beta" , "nightly" )) else path 496 497def produce_tool_path ( tool_name , target_triple , version ): 498"""Produces a qualified Rust tool name 499 500Args: 501tool_name: The name of the tool per static.rust-lang.org 502target_triple: The rust-style target triple of the tool 503version: The version of the tool among "nightly", "beta', or an exact version. 504 505Returns: 506str: The qualified path for the specified tool. 507""" 508if not tool_name : 509fail ( "No tool name was provided" ) 510if not version : 511fail ( "No tool version was provided" ) 512return "-" . join ([ e for e in [ tool_name , version , target_triple ] if e ]) 513 514def load_arbitrary_tool ( ctx , tool_name , tool_subdirectories , version , iso_date , target_triple , sha256 = "" ): 515"""Loads a Rust tool, downloads, and extracts into the common workspace. 516 517This function sources the tool from the Rust-lang static file server. The index is available at: 518- https://static.rust-lang.org/dist/channel-rust-stable.toml 519- https://static.rust-lang.org/dist/channel-rust-beta.toml 520- https://static.rust-lang.org/dist/channel-rust-nightly.toml 521 522Args: 523ctx (repository_ctx): A repository_ctx (no attrs required). 524tool_name (str): The name of the given tool per the archive naming. 525tool_subdirectories (str): The subdirectories of the tool files (at a level below the root directory of 526the archive). The root directory of the archive is expected to match 527$TOOL_NAME-$VERSION-$TARGET_TRIPLE. 528Example: 529tool_name 530| version 531| | target_triple 532v v v 533rust-1.39.0-x86_64-unknown-linux-gnu/clippy-preview 534.../rustc 535.../etc 536tool_subdirectories = ["clippy-preview", "rustc"] 537version (str): The version of the tool among "nightly", "beta', or an exact version. 538iso_date (str): The date of the tool (ignored if the version is a specific version). 539target_triple (str): The rust-style target triple of the tool 540sha256 (str, optional): The expected hash of hash of the Rust tool. Defaults to "". 541""" 542check_version_valid ( version , iso_date , param_prefix = tool_name + "_" ) 543 544# View the indices mentioned in the docstring to find the tool_suburl for a given 545# tool. 546tool_suburl = produce_tool_suburl ( tool_name , target_triple , version , iso_date ) 547urls = [] 548 549for url in getattr ( ctx . attr , "urls" , DEFAULT_STATIC_RUST_URL_TEMPLATES ): 550new_url = url . format ( tool_suburl ) 551if new_url not in urls : 552urls . append ( new_url ) 553 554tool_path = produce_tool_path ( tool_name , target_triple , version ) 555 556archive_path = tool_path + _get_tool_extension ( ctx ) 557ctx . download ( 558urls , 559output = archive_path , 560sha256 = getattr ( ctx . attr , "sha256s" , dict ()). get ( archive_path ) or 561FILE_KEY_TO_SHA . get ( archive_path ) or 562sha256 , 563auth = _make_auth_dict ( ctx , urls ), 564) 565for subdirectory in tool_subdirectories : 566ctx . extract ( 567archive_path , 568output = "" , 569stripPrefix = "{}/{}" . format ( tool_path , subdirectory ), 570) 571 572def _make_auth_dict ( ctx , urls ): 573auth = getattr ( ctx . attr , "auth" , {}) 574if not auth : 575return {} 576ret = {} 577for url in urls : 578ret [ url ] = auth 579return ret 580 581def _get_tool_extension ( ctx ): 582urls = getattr ( ctx . attr , "urls" , DEFAULT_STATIC_RUST_URL_TEMPLATES ) 583if urls [ 0 ][ - 7 :] == ".tar.gz" : 584return ".tar.gz" 585elif urls [ 0 ][ - 7 :] == ".tar.xz" : 586return ".tar.xz" 587else : 588return ""