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