fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
1cc37c26
0.11.0
1# buildifier: disable=module-docstring 2load ( "@bazel_skylib//lib:paths.bzl" , "paths" ) 3load ( "@bazel_tools//tools/build_defs/cc:action_names.bzl" , "CPP_COMPILE_ACTION_NAME" , "C_COMPILE_ACTION_NAME" ) 4load ( "@bazel_tools//tools/cpp:toolchain_utils.bzl" , "find_cpp_toolchain" ) 5load ( "//rust:defs.bzl" , "rust_binary" , "rust_common" ) 6 7# buildifier: disable=bzl-visibility 8load ( "//rust/private:providers.bzl" , _DepInfo = "DepInfo" ) 9 10# buildifier: disable=bzl-visibility 11load ( "//rust/private:rustc.bzl" , "BuildInfo" , "get_compilation_mode_opts" , "get_linker_and_args" ) 12 13# buildifier: disable=bzl-visibility 14load ( "//rust/private:utils.bzl" , "dedent" , "expand_dict_value_locations" , "find_cc_toolchain" , "find_toolchain" , "name_to_crate_name" ) 15 16def strip_target ( elems ): 17"""Remove '-target xxx' from C(XX)FLAGS. 18 19The cpp toolchain adds '-target xxx' and '-isysroot xxx' to CFLAGS. If it is not stripped out before 20the CFLAGS are provided to build scripts, it can cause the build to take on the host architecture 21instead of the target architecture. 22 23Args: 24elems (list): A list of args 25 26Returns: 27list: the modified args 28""" 29skip_next = False 30out_elems = [] 31for elem in elems : 32if skip_next : 33skip_next = False 34 continue 35if elem == "-target" or elem == "-isysroot" : 36skip_next = True 37 continue 38out_elems . append ( elem ) 39return out_elems 40 41def get_cc_compile_args_and_env ( cc_toolchain , feature_configuration ): 42"""Gather cc environment variables from the given `cc_toolchain` 43 44Args: 45cc_toolchain (cc_toolchain): The current rule's `cc_toolchain`. 46feature_configuration (FeatureConfiguration): Class used to construct command lines from CROSSTOOL features. 47 48Returns: 49tuple: A tuple of the following items: 50- (sequence): A flattened C command line flags for given action. 51- (sequence): A flattened CXX command line flags for given action. 52- (dict): C environment variables to be set for given action. 53""" 54compile_variables = cc_common . create_compile_variables ( 55feature_configuration = feature_configuration , 56cc_toolchain = cc_toolchain , 57) 58cc_c_args = strip_target ( cc_common . get_memory_inefficient_command_line ( 59feature_configuration = feature_configuration , 60action_name = C_COMPILE_ACTION_NAME , 61variables = compile_variables , 62)) 63cc_cxx_args = strip_target ( cc_common . get_memory_inefficient_command_line ( 64feature_configuration = feature_configuration , 65action_name = CPP_COMPILE_ACTION_NAME , 66variables = compile_variables , 67)) 68cc_env = cc_common . get_environment_variables ( 69feature_configuration = feature_configuration , 70action_name = C_COMPILE_ACTION_NAME , 71variables = compile_variables , 72) 73return cc_c_args , cc_cxx_args , cc_env 74 75def _pwd_flags ( args ): 76"""Prefix execroot-relative paths of known arguments with ${pwd}. 77 78Args: 79args (list): List of tool arguments. 80 81Returns: 82list: The modified argument list. 83""" 84res = [] 85for arg in args : 86s , opt , path = arg . partition ( "--sysroot=" ) 87if s == "" and not paths . is_absolute ( path ): 88res . append ( "{}${{pwd}}/{}" . format ( opt , path )) 89else : 90res . append ( arg ) 91return res 92 93def _build_script_impl ( ctx ): 94"""The implementation for the `_build_script_run` rule. 95 96Args: 97ctx (ctx): The rules context object 98 99Returns: 100list: A list containing a BuildInfo provider 101""" 102script = ctx . executable . script 103toolchain = find_toolchain ( ctx ) 104out_dir = ctx . actions . declare_directory ( ctx . label . name + ".out_dir" ) 105env_out = ctx . actions . declare_file ( ctx . label . name + ".env" ) 106dep_env_out = ctx . actions . declare_file ( ctx . label . name + ".depenv" ) 107flags_out = ctx . actions . declare_file ( ctx . label . name + ".flags" ) 108link_flags = ctx . actions . declare_file ( ctx . label . name + ".linkflags" ) 109link_search_paths = ctx . actions . declare_file ( ctx . label . name + ".linksearchpaths" ) #rustc - link - search , propagated from transitive dependencies 110manifest_dir = "%s.runfiles/%s/%s" % ( script . path , ctx . label . workspace_name or ctx . workspace_name , ctx . label . package ) 111compilation_mode_opt_level = get_compilation_mode_opts ( ctx , toolchain ). opt_level 112 113streams = struct ( 114stdout = ctx . actions . declare_file ( ctx . label . name + ".stdout.log" ), 115stderr = ctx . actions . declare_file ( ctx . label . name + ".stderr.log" ), 116) 117 118pkg_name = _name_to_pkg_name ( ctx . label . name ) 119 120toolchain_tools = [ toolchain . all_files ] 121 122cc_toolchain = find_cpp_toolchain ( ctx ) 123 124# Start with the default shell env, which contains any --action_env 125# settings passed in on the command line. 126env = dict ( ctx . configuration . default_shell_env ) 127 128env . update ({ 129"CARGO_CRATE_NAME" : name_to_crate_name ( pkg_name ), 130"CARGO_MANIFEST_DIR" : manifest_dir , 131"CARGO_PKG_NAME" : pkg_name , 132"HOST" : toolchain . exec_triple , 133"NUM_JOBS" : "1" , 134"OPT_LEVEL" : compilation_mode_opt_level , 135"RUSTC" : toolchain . rustc . path , 136"TARGET" : toolchain . target_flag_value , 137# OUT_DIR is set by the runner itself, rather than on the action. 138}) 139 140# This isn't exactly right, but Bazel doesn't have exact views of "debug" and "release", so... 141env . update ({ 142"DEBUG" : { "dbg" : "true" , "fastbuild" : "true" , "opt" : "false" }. get ( ctx . var [ "COMPILATION_MODE" ], "true" ), 143"PROFILE" : { "dbg" : "debug" , "fastbuild" : "debug" , "opt" : "release" }. get ( ctx . var [ "COMPILATION_MODE" ], "unknown" ), 144}) 145 146if ctx . attr . version : 147version = ctx . attr . version . split ( "+" )[ 0 ]. split ( "." ) 148patch = version [ 2 ]. split ( "-" ) if len ( version ) > 2 else [ "" ] 149env [ "CARGO_PKG_VERSION_MAJOR" ] = version [ 0 ] 150env [ "CARGO_PKG_VERSION_MINOR" ] = version [ 1 ] if len ( version ) > 1 else "" 151env [ "CARGO_PKG_VERSION_PATCH" ] = patch [ 0 ] 152env [ "CARGO_PKG_VERSION_PRE" ] = patch [ 1 ] if len ( patch ) > 1 else "" 153env [ "CARGO_PKG_VERSION" ] = ctx . attr . version 154 155# Pull in env vars which may be required for the cc_toolchain to work (e.g. on OSX, the SDK version). 156# We hope that the linker env is sufficient for the whole cc_toolchain. 157cc_toolchain , feature_configuration = find_cc_toolchain ( ctx ) 158linker , link_args , linker_env = get_linker_and_args ( ctx , ctx . attr , cc_toolchain , feature_configuration , None ) 159env . update ( ** linker_env ) 160env [ "LD" ] = linker 161env [ "LDFLAGS" ] = " " . join ( _pwd_flags ( link_args )) 162 163# MSVC requires INCLUDE to be set 164cc_c_args , cc_cxx_args , cc_env = get_cc_compile_args_and_env ( cc_toolchain , feature_configuration ) 165include = cc_env . get ( "INCLUDE" ) 166if include : 167env [ "INCLUDE" ] = include 168 169if cc_toolchain : 170toolchain_tools . append ( cc_toolchain . all_files ) 171 172cc_executable = cc_toolchain . compiler_executable 173if cc_executable : 174env [ "CC" ] = cc_executable 175env [ "CXX" ] = cc_executable 176ar_executable = cc_toolchain . ar_executable 177if ar_executable : 178env [ "AR" ] = ar_executable 179 180# Populate CFLAGS and CXXFLAGS that cc-rs relies on when building from source, in particular 181# to determine the deployment target when building for apple platforms (`macosx-version-min` 182# for example, itself derived from the `macos_minimum_os` Bazel argument). 183env [ "CFLAGS" ] = " " . join ( _pwd_flags ( cc_c_args )) 184env [ "CXXFLAGS" ] = " " . join ( _pwd_flags ( cc_cxx_args )) 185 186# Inform build scripts of rustc flags 187# https://github.com/rust-lang/cargo/issues/9600 188env [ "CARGO_ENCODED_RUSTFLAGS" ] = "\\x1f ". join ([ 189# Allow build scripts to locate the generated sysroot 190"--sysroot=${{pwd}}/{}" . format ( toolchain . sysroot ), 191] + ctx . attr . rustc_flags ) 192 193for f in ctx . attr . crate_features : 194env [ "CARGO_FEATURE_" + f . upper (). replace ( "-" , "_" )] = "1" 195 196# Add environment variables from the Rust toolchain. 197env . update ( toolchain . env ) 198 199env . update ( expand_dict_value_locations ( 200ctx , 201ctx . attr . build_script_env , 202getattr ( ctx . attr , "data" , []) + 203getattr ( ctx . attr , "compile_data" , []) + 204getattr ( ctx . attr , "tools" , []), 205)) 206 207tools = depset ( 208direct = [ 209script , 210ctx . executable . _cargo_build_script_runner , 211] + ctx . files . data + ctx . files . tools + ([ toolchain . target_json ] if toolchain . target_json else []), 212transitive = toolchain_tools , 213) 214 215links = ctx . attr . links or "" 216 217# dep_env_file contains additional environment variables coming from 218# direct dependency sys-crates' build scripts. These need to be made 219# available to the current crate build script. 220# See https://doc.rust-lang.org/cargo/reference/build-scripts.html#-sys-packages 221# for details. 222args = ctx . actions . args () 223args . add_all ([ 224script . path , 225links , 226out_dir . path , 227env_out . path , 228flags_out . path , 229link_flags . path , 230link_search_paths . path , 231dep_env_out . path , 232streams . stdout . path , 233streams . stderr . path , 234]) 235build_script_inputs = [] 236for dep in ctx . attr . deps : 237if rust_common . dep_info in dep and dep [ rust_common . dep_info ]. dep_env : 238dep_env_file = dep [ rust_common . dep_info ]. dep_env 239args . add ( dep_env_file . path ) 240build_script_inputs . append ( dep_env_file ) 241for dep_build_info in dep [ rust_common . dep_info ]. transitive_build_infos . to_list (): 242build_script_inputs . append ( dep_build_info . out_dir ) 243 244ctx . actions . run ( 245executable = ctx . executable . _cargo_build_script_runner , 246arguments = [ args ], 247outputs = [ out_dir , env_out , flags_out , link_flags , link_search_paths , dep_env_out , streams . stdout , streams . stderr ], 248tools = tools , 249inputs = build_script_inputs , 250mnemonic = "CargoBuildScriptRun" , 251progress_message = "Running Cargo build script {}" . format ( pkg_name ), 252env = env , 253) 254 255return [ 256BuildInfo ( 257out_dir = out_dir , 258rustc_env = env_out , 259dep_env = dep_env_out , 260flags = flags_out , 261link_flags = link_flags , 262link_search_paths = link_search_paths , 263), 264OutputGroupInfo ( streams = depset ([ streams . stdout , streams . stderr ]), out_dir = depset ([ out_dir ])), 265] 266 267_build_script_run = rule ( 268doc = ( 269"A rule for running a crate's `build.rs` files to generate build information " + 270"which is then used to determine how to compile said crate." 271), 272implementation = _build_script_impl , 273attrs = { 274"build_script_env" : attr . string_dict ( 275doc = "Environment variables for build scripts." , 276), 277"crate_features" : attr . string_list ( 278doc = "The list of rust features that the build script should consider activated." , 279), 280"data" : attr . label_list ( 281doc = "Data required by the build script." , 282allow_files = True , 283), 284"deps" : attr . label_list ( 285doc = "The Rust dependencies of the crate" , 286providers = [ rust_common . dep_info ], 287), 288"links" : attr . string ( 289doc = "The name of the native library this crate links against." , 290), 291"rustc_flags" : attr . string_list ( 292doc = dedent ( """\ 293List of compiler flags passed to `rustc`. 294 295These strings are subject to Make variable expansion for predefined 296source/output path variables like `$location`, `$execpath`, and 297`$rootpath`. This expansion is useful if you wish to pass a generated 298file of arguments to rustc: `@$(location //package:target)`. 299""" ), 300), 301# The source of truth will be the `cargo_build_script` macro until stardoc 302# implements documentation inheritence. See https://github.com/bazelbuild/stardoc/issues/27 303"script" : attr . label ( 304doc = "The binary script to run, generally a `rust_binary` target." , 305executable = True , 306allow_files = True , 307mandatory = True , 308cfg = "exec" , 309), 310"tools" : attr . label_list ( 311doc = "Tools required by the build script." , 312allow_files = True , 313cfg = "exec" , 314), 315"version" : attr . string ( 316doc = "The semantic version (semver) of the crate" , 317), 318"_cargo_build_script_runner" : attr . label ( 319executable = True , 320allow_files = True , 321default = Label ( "//cargo/cargo_build_script_runner:cargo_build_script_runner" ), 322cfg = "exec" , 323), 324"_cc_toolchain" : attr . label ( 325default = Label ( "@bazel_tools//tools/cpp:current_cc_toolchain" ), 326), 327}, 328fragments = [ "cpp" ], 329toolchains = [ 330str ( Label ( "//rust:toolchain_type" )), 331"@bazel_tools//tools/cpp:toolchain_type" , 332], 333incompatible_use_toolchain_transition = True , 334) 335 336def cargo_build_script ( 337name , 338crate_features = [], 339version = None , 340deps = [], 341build_script_env = {}, 342data = [], 343tools = [], 344links = None , 345rustc_env = {}, 346rustc_flags = [], 347visibility = None , 348tags = None , 349** kwargs ): 350"""Compile and execute a rust build script to generate build attributes 351 352This rules take the same arguments as rust_binary. 353 354Example: 355 356Suppose you have a crate with a cargo build script `build.rs`: 357 358```output 359[workspace]/ 360hello_lib/ 361BUILD 362build.rs 363src/ 364lib.rs 365``` 366 367Then you want to use the build script in the following: 368 369`hello_lib/BUILD`: 370```python 371package(default_visibility = ["//visibility:public"]) 372 373load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library") 374load("@rules_rust//cargo:cargo_build_script.bzl", "cargo_build_script") 375 376# This will run the build script from the root of the workspace, and 377# collect the outputs. 378cargo_build_script( 379name = "build_script", 380srcs = ["build.rs"], 381# Optional environment variables passed during build.rs compilation 382rustc_env = { 383"CARGO_PKG_VERSION": "0.1.2", 384}, 385# Optional environment variables passed during build.rs execution. 386# Note that as the build script's working directory is not execroot, 387# execpath/location will return an absolute path, instead of a relative 388# one. 389build_script_env = { 390"SOME_TOOL_OR_FILE": "$(execpath @tool//:binary)" 391} 392# Optional data/tool dependencies 393data = ["@tool//:binary"], 394) 395 396rust_library( 397name = "hello_lib", 398srcs = [ 399"src/lib.rs", 400], 401deps = [":build_script"], 402) 403``` 404 405The `hello_lib` target will be build with the flags and the environment variables declared by the \ 406build script in addition to the file generated by it. 407 408Args: 409name (str): The name for the underlying rule. This should be the name of the package being compiled, optionally with a suffix of _build_script. 410crate_features (list, optional): A list of features to enable for the build script. 411version (str, optional): The semantic version (semver) of the crate. 412deps (list, optional): The dependencies of the crate. 413build_script_env (dict, optional): Environment variables for build scripts. 414data (list, optional): Files needed by the build script. 415tools (list, optional): Tools (executables) needed by the build script. 416links (str, optional): Name of the native library this crate links against. 417rustc_env (dict, optional): Environment variables to set in rustc when compiling the build script. 418rustc_flags (list, optional): List of compiler flags passed to `rustc`. 419visibility (list of label, optional): Visibility to apply to the generated build script output. 420tags: (list of str, optional): Tags to apply to the generated build script output. 421**kwargs: Forwards to the underlying `rust_binary` rule. 422""" 423 424# This duplicates the code in _build_script_impl because we need to make these available both when we invoke rustc (this code) and when we run the compiled build script (_build_script_impl). 425# https://github.com/bazelbuild/rules_rust/issues/661 will hopefully remove this duplication. 426rustc_env = dict ( rustc_env ) 427if "CARGO_PKG_NAME" not in rustc_env : 428rustc_env [ "CARGO_PKG_NAME" ] = _name_to_pkg_name ( name ) 429if "CARGO_CRATE_NAME" not in rustc_env : 430rustc_env [ "CARGO_CRATE_NAME" ] = name_to_crate_name ( _name_to_pkg_name ( name )) 431 432binary_tags = [ tag for tag in tags or []] 433if "manual" not in binary_tags : 434binary_tags . append ( "manual" ) 435 436rust_binary ( 437name = name + "_" , 438crate_features = crate_features , 439version = version , 440deps = deps , 441data = data , 442rustc_env = rustc_env , 443rustc_flags = rustc_flags , 444tags = binary_tags , 445** kwargs 446) 447_build_script_run ( 448name = name , 449script = ":{}_" . format ( name ), 450crate_features = crate_features , 451version = version , 452build_script_env = build_script_env , 453links = links , 454deps = deps , 455data = data , 456tools = tools , 457rustc_flags = rustc_flags , 458visibility = visibility , 459tags = tags , 460) 461 462def _name_to_pkg_name ( name ): 463if name . endswith ( "_build_script" ): 464return name [: - len ( "_build_script" )] 465return name 466 467def _cargo_dep_env_implementation ( ctx ): 468empty_file = ctx . actions . declare_file ( ctx . label . name + ".empty_file" ) 469empty_dir = ctx . actions . declare_directory ( ctx . label . name + ".empty_dir" ) 470ctx . actions . write ( 471output = empty_file , 472content = "" , 473) 474ctx . actions . run ( 475outputs = [ empty_dir ], 476executable = "true" , 477) 478 479build_infos = [] 480out_dir = ctx . file . out_dir 481if out_dir : 482if not out_dir . is_directory : 483fail ( "out_dir must be a directory artifact" ) 484 485# BuildInfos in this list are collected up for all transitive cargo_build_script 486# dependencies. This is important for any flags set in `dep_env` which reference this 487# `out_dir`. 488# 489# TLDR: This BuildInfo propagates up build script dependencies. 490build_infos . append ( BuildInfo ( 491dep_env = empty_file , 492flags = empty_file , 493link_flags = empty_file , 494link_search_paths = empty_file , 495out_dir = out_dir , 496rustc_env = empty_file , 497)) 498return [ 499DefaultInfo ( files = depset ( ctx . files . src )), 500# Parts of this BuildInfo is used when building all transitive dependencies 501# (cargo_build_script and otherwise), alongside the DepInfo. This is how other rules 502# identify this one as a valid dependency, but we don't otherwise have a use for it. 503# 504# TLDR: This BuildInfo propagates up normal (non build script) depenencies. 505# 506# In the future, we could consider setting rustc_env here, and also propagating dep_dir 507# so files in it can be referenced there. 508BuildInfo ( 509dep_env = empty_file , 510flags = empty_file , 511link_flags = empty_file , 512link_search_paths = empty_file , 513out_dir = empty_dir , 514rustc_env = empty_file , 515), 516# Information here is used directly by dependencies, and it is an error to have more than 517# one dependency which sets this. This is the main way to specify information from build 518# scripts, which is what we're looking to do. 519_DepInfo ( 520dep_env = ctx . file . src , 521direct_crates = depset (), 522link_search_path_files = depset (), 523transitive_build_infos = depset ( direct = build_infos ), 524transitive_crate_outputs = depset (), 525transitive_crates = depset (), 526transitive_noncrates = depset (), 527), 528] 529 530cargo_dep_env = rule ( 531implementation = _cargo_dep_env_implementation , 532doc = ( 533"A rule for generating variables for dependent `cargo_build_script`s " + 534"without a build script. This is useful for using Bazel rules instead " + 535"of a build script, while also generating configuration information " + 536"for build scripts which depend on this crate." 537), 538attrs = { 539"out_dir" : attr . label ( 540doc = dedent ( """\ 541Folder containing additional inputs when building all direct dependencies. 542 543This has the same effect as a `cargo_build_script` which prints 544puts files into `$OUT_DIR`, but without requiring a build script. 545""" ), 546allow_single_file = True , 547mandatory = False , 548), 549"src" : attr . label ( 550doc = dedent ( """\ 551 File containing additional environment variables to set for build scripts of direct dependencies. 552 553 This has the same effect as a `cargo_build_script` which prints 554 `cargo:VAR=VALUE` lines, but without requiring a build script. 555 556 This files should contain a single variable per line, of format 557 `NAME=value`, and newlines may be included in a value by ending a 558 line with a trailing back-slash (`\\\\`). 559 """), 560 allow_single_file = True, 561 mandatory = True, 562 ), 563 }, 564)