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