fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
078c6908
0.11.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_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}, 218allocator_library = {allocator_library}, 219binary_ext = "{binary_ext}", 220staticlib_ext = "{staticlib_ext}", 221dylib_ext = "{dylib_ext}", 222stdlib_linkflags = [{stdlib_linkflags}], 223os = "{system}", 224default_edition = "{default_edition}", 225exec_triple = "{exec_triple}", 226target_triple = "{target_triple}", 227visibility = ["//visibility:public"], 228) 229""" 230 231def BUILD_for_rust_toolchain ( 232workspace_name , 233name , 234exec_triple , 235target_triple , 236include_rustc_srcs , 237allocator_library , 238default_edition , 239include_rustfmt , 240include_llvm_tools , 241stdlib_linkflags = None ): 242"""Emits a toolchain declaration to match an existing compiler and stdlib. 243 244Args: 245workspace_name (str): The name of the workspace that this toolchain resides in 246name (str): The name of the toolchain declaration 247exec_triple (str): The rust-style target that this compiler runs on 248target_triple (str): The rust-style target triple of the tool 249include_rustc_srcs (bool, optional): Whether to download rustc's src code. This is required in order to use rust-analyzer support. Defaults to False. 250allocator_library (str, optional): Target that provides allocator functions when rust_library targets are embedded in a cc_binary. 251default_edition (str): Default Rust edition. 252include_rustfmt (bool): Whether rustfmt is present in the toolchain. 253include_llvm_tools (bool): Whether llvm-tools are present in the toolchain. 254stdlib_linkflags (list, optional): Overriden flags needed for linking to rust 255stdlib, akin to BAZEL_LINKLIBS. Defaults to 256None. 257 258 259Returns: 260str: A rendered template of a `rust_toolchain` declaration 261""" 262system = triple_to_system ( target_triple ) 263if stdlib_linkflags == None : 264stdlib_linkflags = ", " . join ([ '"%s"' % x for x in system_to_stdlib_linkflags ( system )]) 265 266rustc_srcs = "None" 267if include_rustc_srcs : 268rustc_srcs = "\"@{workspace_name}//lib/rustlib/src:rustc_srcs\"" . format ( workspace_name = workspace_name ) 269rustfmt_label = "None" 270if include_rustfmt : 271rustfmt_label = "\"@{workspace_name}//:rustfmt_bin\"" . format ( workspace_name = workspace_name ) 272llvm_cov_label = "None" 273llvm_profdata_label = "None" 274if include_llvm_tools : 275llvm_cov_label = "\"@{workspace_name}//:llvm_cov_bin\"" . format ( workspace_name = workspace_name ) 276llvm_profdata_label = "\"@{workspace_name}//:llvm_profdata_bin\"" . format ( workspace_name = workspace_name ) 277allocator_library_label = "None" 278if allocator_library : 279allocator_library_label = "\"{allocator_library}\"" . format ( allocator_library = allocator_library ) 280 281return _build_file_for_rust_toolchain_template . format ( 282toolchain_name = name , 283workspace_name = workspace_name , 284binary_ext = system_to_binary_ext ( system ), 285staticlib_ext = system_to_staticlib_ext ( system ), 286dylib_ext = system_to_dylib_ext ( system ), 287rustc_srcs = rustc_srcs , 288allocator_library = allocator_library_label , 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 = "{toolchain}", 305toolchain_type = "{toolchain_type}", 306) 307""" 308 309def BUILD_for_toolchain ( 310name , 311toolchain , 312toolchain_type , 313target_compatible_with , 314exec_compatible_with ): 315return _build_file_for_toolchain_template . format ( 316name = name , 317exec_constraint_sets_serialized = exec_compatible_with , 318target_constraint_sets_serialized = target_compatible_with , 319toolchain = toolchain , 320toolchain_type = toolchain_type , 321) 322 323def load_rustfmt ( ctx ): 324"""Loads a rustfmt binary and yields corresponding BUILD for it 325 326Args: 327ctx (repository_ctx): The repository rule's context object 328 329Returns: 330str: The BUILD file contents for this rustfmt binary 331""" 332target_triple = ctx . attr . exec_triple 333 334load_arbitrary_tool ( 335ctx , 336iso_date = ctx . attr . iso_date , 337target_triple = target_triple , 338tool_name = "rustfmt" , 339tool_subdirectories = [ "rustfmt-preview" ], 340version = ctx . attr . rustfmt_version , 341) 342 343return BUILD_for_rustfmt ( target_triple ) 344 345def load_rust_compiler ( ctx ): 346"""Loads a rust compiler and yields corresponding BUILD for it 347 348Args: 349ctx (repository_ctx): A repository_ctx. 350 351Returns: 352str: The BUILD file contents for this compiler and compiler library 353""" 354 355target_triple = ctx . attr . exec_triple 356load_arbitrary_tool ( 357ctx , 358iso_date = ctx . attr . iso_date , 359target_triple = target_triple , 360tool_name = "rustc" , 361tool_subdirectories = [ "rustc" ], 362version = ctx . attr . version , 363) 364 365return BUILD_for_compiler ( target_triple ) 366 367def load_clippy ( ctx ): 368"""Loads Clippy and yields corresponding BUILD for it 369 370Args: 371ctx (repository_ctx): A repository_ctx. 372 373Returns: 374str: The BUILD file contents for Clippy 375""" 376 377target_triple = ctx . attr . exec_triple 378load_arbitrary_tool ( 379ctx , 380iso_date = ctx . attr . iso_date , 381target_triple = target_triple , 382tool_name = "clippy" , 383tool_subdirectories = [ "clippy-preview" ], 384version = ctx . attr . version , 385) 386 387return BUILD_for_clippy ( target_triple ) 388 389def load_cargo ( ctx ): 390"""Loads Cargo and yields corresponding BUILD for it 391 392Args: 393ctx (repository_ctx): A repository_ctx. 394 395Returns: 396str: The BUILD file contents for Cargo 397""" 398 399target_triple = ctx . attr . exec_triple 400load_arbitrary_tool ( 401ctx , 402iso_date = ctx . attr . iso_date , 403target_triple = target_triple , 404tool_name = "cargo" , 405tool_subdirectories = [ "cargo" ], 406version = ctx . attr . version , 407) 408 409return BUILD_for_cargo ( target_triple ) 410 411def should_include_rustc_srcs ( repository_ctx ): 412"""Determing whether or not to include rustc sources in the toolchain. 413 414Args: 415repository_ctx (repository_ctx): The repository rule's context object 416 417Returns: 418bool: Whether or not to include rustc source files in a `rustc_toolchain` 419""" 420 421# The environment variable will always take precedence over the attribute. 422include_rustc_srcs_env = repository_ctx . os . environ . get ( "RULES_RUST_TOOLCHAIN_INCLUDE_RUSTC_SRCS" ) 423if include_rustc_srcs_env != None : 424return include_rustc_srcs_env . lower () in [ "true" , "1" ] 425 426return getattr ( repository_ctx . attr , "include_rustc_srcs" , False ) 427 428def load_rust_src ( ctx , sha256 = "" ): 429"""Loads the rust source code. Used by the rust-analyzer rust-project.json generator. 430 431Args: 432ctx (ctx): A repository_ctx. 433sha256 (str): The sha256 value for the `rust-src` artifact 434""" 435tool_suburl = produce_tool_suburl ( "rust-src" , None , ctx . attr . version , ctx . attr . iso_date ) 436url = ctx . attr . urls [ 0 ]. format ( tool_suburl ) 437 438tool_path = produce_tool_path ( "rust-src" , None , ctx . attr . version ) 439archive_path = tool_path + _get_tool_extension ( ctx ) 440sha256 = sha256 or getattr ( ctx . attr , "sha256s" , {}). get ( archive_path ) or FILE_KEY_TO_SHA . get ( archive_path ) or "" 441ctx . download_and_extract ( 442url , 443output = "lib/rustlib/src" , 444sha256 = sha256 , 445auth = _make_auth_dict ( ctx , [ url ]), 446stripPrefix = "{}/rust-src/lib/rustlib/src/rust" . format ( tool_path ), 447) 448ctx . file ( 449"lib/rustlib/src/BUILD.bazel" , 450"""\ 451filegroup( 452name = "rustc_srcs", 453srcs = glob(["**/*"]), 454visibility = ["//visibility:public"], 455)""" , 456) 457 458_build_file_for_rust_analyzer_toolchain_template = """\ 459load("@rules_rust//rust:toolchain.bzl", "rust_analyzer_toolchain") 460 461rust_analyzer_toolchain( 462name = "{name}", 463rustc_srcs = "//lib/rustlib/src:rustc_srcs", 464visibility = ["//visibility:public"], 465) 466""" 467 468def BUILD_for_rust_analyzer_toolchain ( name ): 469return _build_file_for_rust_analyzer_toolchain_template . format ( 470name = name , 471) 472 473def load_rust_stdlib ( ctx , target_triple ): 474"""Loads a rust standard library and yields corresponding BUILD for it 475 476Args: 477ctx (repository_ctx): A repository_ctx. 478target_triple (str): The rust-style target triple of the tool 479 480Returns: 481str: The BUILD file contents for this stdlib 482""" 483 484load_arbitrary_tool ( 485ctx , 486iso_date = ctx . attr . iso_date , 487target_triple = target_triple , 488tool_name = "rust-std" , 489tool_subdirectories = [ "rust-std-{}" . format ( target_triple )], 490version = ctx . attr . version , 491) 492 493return BUILD_for_stdlib ( target_triple ) 494 495def load_rustc_dev_nightly ( ctx , target_triple ): 496"""Loads the nightly rustc dev component 497 498Args: 499ctx: A repository_ctx. 500target_triple: The rust-style target triple of the tool 501""" 502 503subdir_name = "rustc-dev" 504if ctx . attr . iso_date < "2020-12-24" : 505subdir_name = "rustc-dev-{}" . format ( target_triple ) 506 507load_arbitrary_tool ( 508ctx , 509iso_date = ctx . attr . iso_date , 510target_triple = target_triple , 511tool_name = "rustc-dev" , 512tool_subdirectories = [ subdir_name ], 513version = ctx . attr . version , 514) 515 516def load_llvm_tools ( ctx , target_triple ): 517"""Loads the llvm tools 518 519Args: 520ctx: A repository_ctx. 521target_triple: The rust-style target triple of the tool 522""" 523load_arbitrary_tool ( 524ctx , 525iso_date = ctx . attr . iso_date , 526target_triple = target_triple , 527tool_name = "llvm-tools" , 528tool_subdirectories = [ "llvm-tools-preview" ], 529version = ctx . attr . version , 530) 531 532return BUILD_for_llvm_tools ( target_triple ) 533 534def check_version_valid ( version , iso_date , param_prefix = "" ): 535"""Verifies that the provided rust version and iso_date make sense. 536 537Args: 538version (str): The rustc version 539iso_date (str): The rustc nightly version's iso date 540param_prefix (str, optional): The name of the tool who's version is being checked. 541""" 542 543if not version and iso_date : 544fail ( "{param_prefix}iso_date must be paired with a {param_prefix}version" . format ( param_prefix = param_prefix )) 545 546if version in ( "beta" , "nightly" ) and not iso_date : 547fail ( "{param_prefix}iso_date must be specified if version is 'beta' or 'nightly'" . format ( param_prefix = param_prefix )) 548 549def produce_tool_suburl ( tool_name , target_triple , version , iso_date = None ): 550"""Produces a fully qualified Rust tool name for URL 551 552Args: 553tool_name: The name of the tool per static.rust-lang.org 554target_triple: The rust-style target triple of the tool 555version: The version of the tool among "nightly", "beta', or an exact version. 556iso_date: The date of the tool (or None, if the version is a specific version). 557 558Returns: 559str: The fully qualified url path for the specified tool. 560""" 561path = produce_tool_path ( tool_name , target_triple , version ) 562return iso_date + "/" + path if ( iso_date and version in ( "beta" , "nightly" )) else path 563 564def produce_tool_path ( tool_name , target_triple , version ): 565"""Produces a qualified Rust tool name 566 567Args: 568tool_name: The name of the tool per static.rust-lang.org 569target_triple: The rust-style target triple of the tool 570version: The version of the tool among "nightly", "beta', or an exact version. 571 572Returns: 573str: The qualified path for the specified tool. 574""" 575if not tool_name : 576fail ( "No tool name was provided" ) 577if not version : 578fail ( "No tool version was provided" ) 579return "-" . join ([ e for e in [ tool_name , version , target_triple ] if e ]) 580 581def load_arbitrary_tool ( ctx , tool_name , tool_subdirectories , version , iso_date , target_triple , sha256 = "" ): 582"""Loads a Rust tool, downloads, and extracts into the common workspace. 583 584This function sources the tool from the Rust-lang static file server. The index is available at: 585- https://static.rust-lang.org/dist/channel-rust-stable.toml 586- https://static.rust-lang.org/dist/channel-rust-beta.toml 587- https://static.rust-lang.org/dist/channel-rust-nightly.toml 588 589Args: 590ctx (repository_ctx): A repository_ctx (no attrs required). 591tool_name (str): The name of the given tool per the archive naming. 592tool_subdirectories (str): The subdirectories of the tool files (at a level below the root directory of 593the archive). The root directory of the archive is expected to match 594$TOOL_NAME-$VERSION-$TARGET_TRIPLE. 595Example: 596tool_name 597| version 598| | target_triple 599v v v 600rust-1.39.0-x86_64-unknown-linux-gnu/clippy-preview 601.../rustc 602.../etc 603tool_subdirectories = ["clippy-preview", "rustc"] 604version (str): The version of the tool among "nightly", "beta', or an exact version. 605iso_date (str): The date of the tool (ignored if the version is a specific version). 606target_triple (str): The rust-style target triple of the tool 607sha256 (str, optional): The expected hash of hash of the Rust tool. Defaults to "". 608""" 609check_version_valid ( version , iso_date , param_prefix = tool_name + "_" ) 610 611# View the indices mentioned in the docstring to find the tool_suburl for a given 612# tool. 613tool_suburl = produce_tool_suburl ( tool_name , target_triple , version , iso_date ) 614urls = [] 615 616for url in getattr ( ctx . attr , "urls" , DEFAULT_STATIC_RUST_URL_TEMPLATES ): 617new_url = url . format ( tool_suburl ) 618if new_url not in urls : 619urls . append ( new_url ) 620 621tool_path = produce_tool_path ( tool_name , target_triple , version ) 622archive_path = tool_path + _get_tool_extension ( ctx ) 623sha256 = getattr ( ctx . attr , "sha256s" , dict ()). get ( archive_path ) or FILE_KEY_TO_SHA . get ( archive_path ) or sha256 624 625for subdirectory in tool_subdirectories : 626# As long as the sha256 value is consistent accross calls here the 627# cost of downloading an artifact is negated as by Bazel's caching. 628result = ctx . download_and_extract ( 629urls , 630sha256 = sha256 , 631auth = _make_auth_dict ( ctx , urls ), 632stripPrefix = "{}/{}" . format ( tool_path , subdirectory ), 633) 634 635# In the event no sha256 was provided, set it to the value of the first 636# downloaded item so subsequent downloads use a cached artifact. 637if not sha256 : 638sha256 = result . sha256 639 640def _make_auth_dict ( ctx , urls ): 641auth = getattr ( ctx . attr , "auth" , {}) 642if not auth : 643return {} 644ret = {} 645for url in urls : 646ret [ url ] = auth 647return ret 648 649def _get_tool_extension ( ctx ): 650urls = getattr ( ctx . attr , "urls" , DEFAULT_STATIC_RUST_URL_TEMPLATES ) 651if urls [ 0 ][ - 7 :] == ".tar.gz" : 652return ".tar.gz" 653elif urls [ 0 ][ - 7 :] == ".tar.xz" : 654return ".tar.xz" 655else : 656return ""