fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
7465c1aa
0.7.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_llvm_tools = """\ 141filegroup( 142name = "llvm_cov_bin", 143srcs = ["lib/rustlib/{target_triple}/bin/llvm-cov{binary_ext}"], 144visibility = ["//visibility:public"], 145) 146 147filegroup( 148name = "llvm_profdata_bin", 149srcs = ["lib/rustlib/{target_triple}/bin/llvm-profdata{binary_ext}"], 150visibility = ["//visibility:public"], 151) 152""" 153 154def BUILD_for_llvm_tools ( target_triple ): 155"""Emits a BUILD file the llvm-tools binaries. 156 157Args: 158target_triple (str): The triple of the target platform 159 160Returns: 161str: The contents of a BUILD file 162""" 163system = triple_to_system ( target_triple ) 164return _build_file_for_llvm_tools . format ( 165binary_ext = system_to_binary_ext ( system ), 166target_triple = target_triple , 167) 168 169_build_file_for_stdlib_template = """\ 170load("@rules_rust//rust:toolchain.bzl", "rust_stdlib_filegroup") 171 172rust_stdlib_filegroup( 173name = "rust_std-{target_triple}", 174srcs = glob( 175[ 176"lib/rustlib/{target_triple}/lib/*.rlib", 177"lib/rustlib/{target_triple}/lib/*{dylib_ext}", 178"lib/rustlib/{target_triple}/lib/*{staticlib_ext}", 179"lib/rustlib/{target_triple}/lib/self-contained/**", 180], 181# Some patterns (e.g. `lib/*.a`) don't match anything, see https://github.com/bazelbuild/rules_rust/pull/245 182allow_empty = True, 183), 184visibility = ["//visibility:public"], 185) 186 187# For legacy support 188alias( 189name = "rust_lib-{target_triple}", 190actual = "rust_std-{target_triple}", 191visibility = ["//visibility:public"], 192) 193""" 194 195def BUILD_for_stdlib ( target_triple ): 196"""Emits a BUILD file the stdlib archive. 197 198Args: 199target_triple (str): The triple of the target platform 200 201Returns: 202str: The contents of a BUILD file 203""" 204system = triple_to_system ( target_triple ) 205return _build_file_for_stdlib_template . format ( 206binary_ext = system_to_binary_ext ( system ), 207staticlib_ext = system_to_staticlib_ext ( system ), 208dylib_ext = system_to_dylib_ext ( system ), 209target_triple = target_triple , 210) 211 212_build_file_for_rust_toolchain_template = """\ 213rust_toolchain( 214name = "{toolchain_name}_impl", 215rust_doc = "@{workspace_name}//:rustdoc", 216rust_std = "@{workspace_name}//:rust_std-{target_triple}", 217rustc = "@{workspace_name}//:rustc", 218rustfmt = {rustfmt_label}, 219cargo = "@{workspace_name}//:cargo", 220clippy_driver = "@{workspace_name}//:clippy_driver_bin", 221llvm_cov = {llvm_cov_label}, 222llvm_profdata = {llvm_profdata_label}, 223rustc_lib = "@{workspace_name}//:rustc_lib", 224rustc_srcs = {rustc_srcs}, 225binary_ext = "{binary_ext}", 226staticlib_ext = "{staticlib_ext}", 227dylib_ext = "{dylib_ext}", 228stdlib_linkflags = [{stdlib_linkflags}], 229os = "{system}", 230default_edition = "{default_edition}", 231exec_triple = "{exec_triple}", 232target_triple = "{target_triple}", 233visibility = ["//visibility:public"], 234) 235""" 236 237def BUILD_for_rust_toolchain ( 238workspace_name , 239name , 240exec_triple , 241target_triple , 242include_rustc_srcs , 243default_edition , 244include_rustfmt , 245include_llvm_tools , 246stdlib_linkflags = None ): 247"""Emits a toolchain declaration to match an existing compiler and stdlib. 248 249Args: 250workspace_name (str): The name of the workspace that this toolchain resides in 251name (str): The name of the toolchain declaration 252exec_triple (str): The rust-style target that this compiler runs on 253target_triple (str): The rust-style target triple of the tool 254include_rustc_srcs (bool, optional): Whether to download rustc's src code. This is required in order to use rust-analyzer support. Defaults to False. 255default_edition (str): Default Rust edition. 256include_rustfmt (bool): Whether rustfmt is present in the toolchain. 257include_llvm_tools (bool): Whether llvm-tools are present in the toolchain. 258stdlib_linkflags (list, optional): Overriden flags needed for linking to rust 259stdlib, akin to BAZEL_LINKLIBS. Defaults to 260None. 261 262 263Returns: 264str: A rendered template of a `rust_toolchain` declaration 265""" 266system = triple_to_system ( target_triple ) 267if stdlib_linkflags == None : 268stdlib_linkflags = ", " . join ([ '"%s"' % x for x in system_to_stdlib_linkflags ( system )]) 269 270rustc_srcs = "None" 271if include_rustc_srcs : 272rustc_srcs = "\"@{workspace_name}//lib/rustlib/src:rustc_srcs\"" . format ( workspace_name = workspace_name ) 273rustfmt_label = "None" 274if include_rustfmt : 275rustfmt_label = "\"@{workspace_name}//:rustfmt_bin\"" . format ( workspace_name = workspace_name ) 276llvm_cov_label = "None" 277llvm_profdata_label = "None" 278if include_llvm_tools : 279llvm_cov_label = "\"@{workspace_name}//:llvm_cov_bin\"" . format ( workspace_name = workspace_name ) 280llvm_profdata_label = "\"@{workspace_name}//:llvm_profdata_bin\"" . format ( workspace_name = workspace_name ) 281 282return _build_file_for_rust_toolchain_template . format ( 283toolchain_name = name , 284workspace_name = workspace_name , 285binary_ext = system_to_binary_ext ( system ), 286staticlib_ext = system_to_staticlib_ext ( system ), 287dylib_ext = system_to_dylib_ext ( system ), 288rustc_srcs = rustc_srcs , 289stdlib_linkflags = stdlib_linkflags , 290system = system , 291default_edition = default_edition , 292exec_triple = exec_triple , 293target_triple = target_triple , 294rustfmt_label = rustfmt_label , 295llvm_cov_label = llvm_cov_label , 296llvm_profdata_label = llvm_profdata_label , 297) 298 299_build_file_for_toolchain_template = """\ 300toolchain( 301name = "{name}", 302exec_compatible_with = {exec_constraint_sets_serialized}, 303target_compatible_with = {target_constraint_sets_serialized}, 304toolchain = "@{parent_workspace_name}//:{name}_impl", 305toolchain_type = "@rules_rust//rust:toolchain", 306) 307""" 308 309def BUILD_for_toolchain ( name , parent_workspace_name , exec_triple , target_triple ): 310return _build_file_for_toolchain_template . format ( 311name = name , 312exec_constraint_sets_serialized = serialized_constraint_set_from_triple ( exec_triple ), 313target_constraint_sets_serialized = serialized_constraint_set_from_triple ( target_triple ), 314parent_workspace_name = parent_workspace_name , 315) 316 317def load_rustfmt ( ctx ): 318"""Loads a rustfmt binary and yields corresponding BUILD for it 319 320Args: 321ctx (repository_ctx): The repository rule's context object 322 323Returns: 324str: The BUILD file contents for this rustfmt binary 325""" 326target_triple = ctx . attr . exec_triple 327 328load_arbitrary_tool ( 329ctx , 330iso_date = ctx . attr . iso_date , 331target_triple = target_triple , 332tool_name = "rustfmt" , 333tool_subdirectories = [ "rustfmt-preview" ], 334version = ctx . attr . rustfmt_version , 335) 336 337return BUILD_for_rustfmt ( target_triple ) 338 339def load_rust_compiler ( ctx ): 340"""Loads a rust compiler and yields corresponding BUILD for it 341 342Args: 343ctx (repository_ctx): A repository_ctx. 344 345Returns: 346str: The BUILD file contents for this compiler and compiler library 347""" 348 349target_triple = ctx . attr . exec_triple 350load_arbitrary_tool ( 351ctx , 352iso_date = ctx . attr . iso_date , 353target_triple = target_triple , 354tool_name = "rust" , 355tool_subdirectories = [ "rustc" , "clippy-preview" , "cargo" ], 356version = ctx . attr . version , 357) 358 359compiler_build_file = BUILD_for_compiler ( target_triple ) + BUILD_for_clippy ( target_triple ) + BUILD_for_cargo ( target_triple ) 360 361return compiler_build_file 362 363def should_include_rustc_srcs ( repository_ctx ): 364"""Determing whether or not to include rustc sources in the toolchain. 365 366Args: 367repository_ctx (repository_ctx): The repository rule's context object 368 369Returns: 370bool: Whether or not to include rustc source files in a `rustc_toolchain` 371""" 372 373# The environment variable will always take precedence over the attribute. 374include_rustc_srcs_env = repository_ctx . os . environ . get ( "RULES_RUST_TOOLCHAIN_INCLUDE_RUSTC_SRCS" ) 375if include_rustc_srcs_env != None : 376return include_rustc_srcs_env . lower () in [ "true" , "1" ] 377 378return getattr ( repository_ctx . attr , "include_rustc_srcs" , False ) 379 380def load_rust_src ( ctx ): 381"""Loads the rust source code. Used by the rust-analyzer rust-project.json generator. 382 383Args: 384ctx (ctx): A repository_ctx. 385""" 386tool_suburl = produce_tool_suburl ( "rust-src" , None , ctx . attr . version , ctx . attr . iso_date ) 387url = ctx . attr . urls [ 0 ]. format ( tool_suburl ) 388 389tool_path = produce_tool_path ( "rust-src" , None , ctx . attr . version ) 390archive_path = tool_path + _get_tool_extension ( ctx ) 391ctx . download ( 392url , 393output = archive_path , 394sha256 = ctx . attr . sha256s . get ( archive_path ) or FILE_KEY_TO_SHA . get ( archive_path ) or "" , 395auth = _make_auth_dict ( ctx , [ url ]), 396) 397ctx . extract ( 398archive_path , 399output = "lib/rustlib/src" , 400stripPrefix = "{}/rust-src/lib/rustlib/src/rust" . format ( tool_path ), 401) 402ctx . file ( 403"lib/rustlib/src/BUILD.bazel" , 404"""\ 405filegroup( 406name = "rustc_srcs", 407srcs = glob(["**/*"]), 408visibility = ["//visibility:public"], 409)""" , 410) 411 412def load_rust_stdlib ( ctx , target_triple , include_llvm_tools ): 413"""Loads a rust standard library and yields corresponding BUILD for it 414 415Args: 416ctx (repository_ctx): A repository_ctx. 417target_triple (str): The rust-style target triple of the tool 418include_llvm_tools (bool): Whether to include LLVM tools in the toolchain 419 420Returns: 421str: The BUILD file contents for this stdlib, and a toolchain decl to match 422""" 423 424load_arbitrary_tool ( 425ctx , 426iso_date = ctx . attr . iso_date , 427target_triple = target_triple , 428tool_name = "rust-std" , 429tool_subdirectories = [ "rust-std-{}" . format ( target_triple )], 430version = ctx . attr . version , 431) 432 433toolchain_prefix = ctx . attr . toolchain_name_prefix or DEFAULT_TOOLCHAIN_NAME_PREFIX 434stdlib_build_file = BUILD_for_stdlib ( target_triple ) 435 436stdlib_linkflags = None 437if "BAZEL_RUST_STDLIB_LINKFLAGS" in ctx . os . environ : 438stdlib_linkflags = ctx . os . environ [ "BAZEL_RUST_STDLIB_LINKFLAGS" ]. split ( ":" ) 439 440toolchain_build_file = BUILD_for_rust_toolchain ( 441name = "{toolchain_prefix}_{target_triple}" . format ( 442toolchain_prefix = toolchain_prefix , 443target_triple = target_triple , 444), 445exec_triple = ctx . attr . exec_triple , 446include_rustc_srcs = should_include_rustc_srcs ( ctx ), 447target_triple = target_triple , 448stdlib_linkflags = stdlib_linkflags , 449workspace_name = ctx . attr . name , 450default_edition = ctx . attr . edition , 451include_rustfmt = not ( not ctx . attr . rustfmt_version ), 452include_llvm_tools = include_llvm_tools , 453) 454 455return stdlib_build_file + toolchain_build_file 456 457def load_rustc_dev_nightly ( ctx , target_triple ): 458"""Loads the nightly rustc dev component 459 460Args: 461ctx: A repository_ctx. 462target_triple: The rust-style target triple of the tool 463""" 464 465subdir_name = "rustc-dev" 466if ctx . attr . iso_date < "2020-12-24" : 467subdir_name = "rustc-dev-{}" . format ( target_triple ) 468 469load_arbitrary_tool ( 470ctx , 471iso_date = ctx . attr . iso_date , 472target_triple = target_triple , 473tool_name = "rustc-dev" , 474tool_subdirectories = [ subdir_name ], 475version = ctx . attr . version , 476) 477 478def load_llvm_tools ( ctx , target_triple ): 479"""Loads the llvm tools 480 481Args: 482ctx: A repository_ctx. 483target_triple: The rust-style target triple of the tool 484""" 485load_arbitrary_tool ( 486ctx , 487iso_date = ctx . attr . iso_date , 488target_triple = target_triple , 489tool_name = "llvm-tools" , 490tool_subdirectories = [ "llvm-tools-preview" ], 491version = ctx . attr . version , 492) 493 494return BUILD_for_llvm_tools ( target_triple ) 495 496def check_version_valid ( version , iso_date , param_prefix = "" ): 497"""Verifies that the provided rust version and iso_date make sense. 498 499Args: 500version (str): The rustc version 501iso_date (str): The rustc nightly version's iso date 502param_prefix (str, optional): The name of the tool who's version is being checked. 503""" 504 505if not version and iso_date : 506fail ( "{param_prefix}iso_date must be paired with a {param_prefix}version" . format ( param_prefix = param_prefix )) 507 508if version in ( "beta" , "nightly" ) and not iso_date : 509fail ( "{param_prefix}iso_date must be specified if version is 'beta' or 'nightly'" . format ( param_prefix = param_prefix )) 510 511def serialized_constraint_set_from_triple ( target_triple ): 512"""Returns a string representing a set of constraints 513 514Args: 515target_triple (str): The target triple of the constraint set 516 517Returns: 518str: Formatted string representing the serialized constraint 519""" 520constraint_set = triple_to_constraint_set ( target_triple ) 521constraint_set_strs = [] 522for constraint in constraint_set : 523constraint_set_strs . append ( "\"{}\"" . format ( constraint )) 524return "[{}]" . format ( ", " . join ( constraint_set_strs )) 525 526def produce_tool_suburl ( tool_name , target_triple , version , iso_date = None ): 527"""Produces a fully qualified Rust tool name for URL 528 529Args: 530tool_name: The name of the tool per static.rust-lang.org 531target_triple: The rust-style target triple of the tool 532version: The version of the tool among "nightly", "beta', or an exact version. 533iso_date: The date of the tool (or None, if the version is a specific version). 534 535Returns: 536str: The fully qualified url path for the specified tool. 537""" 538path = produce_tool_path ( tool_name , target_triple , version ) 539return iso_date + "/" + path if ( iso_date and version in ( "beta" , "nightly" )) else path 540 541def produce_tool_path ( tool_name , target_triple , version ): 542"""Produces a qualified Rust tool name 543 544Args: 545tool_name: The name of the tool per static.rust-lang.org 546target_triple: The rust-style target triple of the tool 547version: The version of the tool among "nightly", "beta', or an exact version. 548 549Returns: 550str: The qualified path for the specified tool. 551""" 552if not tool_name : 553fail ( "No tool name was provided" ) 554if not version : 555fail ( "No tool version was provided" ) 556return "-" . join ([ e for e in [ tool_name , version , target_triple ] if e ]) 557 558def load_arbitrary_tool ( ctx , tool_name , tool_subdirectories , version , iso_date , target_triple , sha256 = "" ): 559"""Loads a Rust tool, downloads, and extracts into the common workspace. 560 561This function sources the tool from the Rust-lang static file server. The index is available at: 562- https://static.rust-lang.org/dist/channel-rust-stable.toml 563- https://static.rust-lang.org/dist/channel-rust-beta.toml 564- https://static.rust-lang.org/dist/channel-rust-nightly.toml 565 566Args: 567ctx (repository_ctx): A repository_ctx (no attrs required). 568tool_name (str): The name of the given tool per the archive naming. 569tool_subdirectories (str): The subdirectories of the tool files (at a level below the root directory of 570the archive). The root directory of the archive is expected to match 571$TOOL_NAME-$VERSION-$TARGET_TRIPLE. 572Example: 573tool_name 574| version 575| | target_triple 576v v v 577rust-1.39.0-x86_64-unknown-linux-gnu/clippy-preview 578.../rustc 579.../etc 580tool_subdirectories = ["clippy-preview", "rustc"] 581version (str): The version of the tool among "nightly", "beta', or an exact version. 582iso_date (str): The date of the tool (ignored if the version is a specific version). 583target_triple (str): The rust-style target triple of the tool 584sha256 (str, optional): The expected hash of hash of the Rust tool. Defaults to "". 585""" 586check_version_valid ( version , iso_date , param_prefix = tool_name + "_" ) 587 588# View the indices mentioned in the docstring to find the tool_suburl for a given 589# tool. 590tool_suburl = produce_tool_suburl ( tool_name , target_triple , version , iso_date ) 591urls = [] 592 593for url in getattr ( ctx . attr , "urls" , DEFAULT_STATIC_RUST_URL_TEMPLATES ): 594new_url = url . format ( tool_suburl ) 595if new_url not in urls : 596urls . append ( new_url ) 597 598tool_path = produce_tool_path ( tool_name , target_triple , version ) 599 600archive_path = tool_path + _get_tool_extension ( ctx ) 601ctx . download ( 602urls , 603output = archive_path , 604sha256 = getattr ( ctx . attr , "sha256s" , dict ()). get ( archive_path ) or 605FILE_KEY_TO_SHA . get ( archive_path ) or 606sha256 , 607auth = _make_auth_dict ( ctx , urls ), 608) 609for subdirectory in tool_subdirectories : 610ctx . extract ( 611archive_path , 612output = "" , 613stripPrefix = "{}/{}" . format ( tool_path , subdirectory ), 614) 615 616def _make_auth_dict ( ctx , urls ): 617auth = getattr ( ctx . attr , "auth" , {}) 618if not auth : 619return {} 620ret = {} 621for url in urls : 622ret [ url ] = auth 623return ret 624 625def _get_tool_extension ( ctx ): 626urls = getattr ( ctx . attr , "urls" , DEFAULT_STATIC_RUST_URL_TEMPLATES ) 627if urls [ 0 ][ - 7 :] == ".tar.gz" : 628return ".tar.gz" 629elif urls [ 0 ][ - 7 :] == ".tar.xz" : 630return ".tar.xz" 631else : 632return ""