fork/rules_rust
git clone https://git.julia.blue/fork/rules_rust
d58a8e77
main
1"""Rules for Cargo build scripts (`build.rs` files)""" 2 3load ( "@bazel_skylib//lib:paths.bzl" , "paths" ) 4load ( "@bazel_skylib//rules:common_settings.bzl" , "BuildSettingInfo" ) 5load ( "@bazel_tools//tools/cpp:toolchain_utils.bzl" , "find_cpp_toolchain" ) 6load ( "@rules_cc//cc:action_names.bzl" , "ACTION_NAMES" ) 7load ( "//rust:defs.bzl" , "rust_common" ) 8load ( "//rust:rust_common.bzl" , "BuildInfo" ) 9 10# buildifier: disable=bzl-visibility 11load ( 12"//rust/private:rustc.bzl" , 13"get_compilation_mode_opts" , 14"get_linker_and_args" , 15) 16 17# buildifier: disable=bzl-visibility 18load ( 19"//rust/private:utils.bzl" , 20"dedent" , 21"expand_dict_value_locations" , 22"find_cc_toolchain" , 23"find_toolchain" , 24_name_to_crate_name = "name_to_crate_name" , 25) 26 27# Reexport for cargo_build_script_wrapper.bzl 28name_to_crate_name = _name_to_crate_name 29 30CargoBuildScriptRunfilesInfo = provider ( 31doc = "Info about a `cargo_build_script.script` target." , 32fields = { 33"data" : "List[Target]: The raw `cargo_build_script_runfiles.data` attribute." , 34"tools" : "List[Target]: The raw `cargo_build_script_runfiles.tools` attribute." , 35}, 36) 37 38def _cargo_build_script_runfiles_impl ( ctx ): 39script = ctx . executable . script 40 41is_windows = script . extension == "exe" 42exe = ctx . actions . declare_file ( "{}{}" . format ( ctx . label . name , ".exe" if is_windows else "" )) 43 44# Avoid the following issue on Windows when using builds-without-the-bytes. 45# https://github.com/bazelbuild/bazel/issues/21747 46if is_windows : 47args = ctx . actions . args () 48args . add ( script ) 49args . add ( exe ) 50 51ctx . actions . run ( 52executable = ctx . executable . _copy_file , 53arguments = [ args ], 54inputs = [ script ], 55outputs = [ exe ], 56) 57else : 58ctx . actions . symlink ( 59output = exe , 60target_file = script , 61is_executable = True , 62) 63 64# Tools are ommitted here because they should be within the `script` 65# attribute's runfiles. 66runfiles = ctx . runfiles ( files = ctx . files . data ) 67 68return [ 69DefaultInfo ( 70files = depset ([ exe ]), 71runfiles = runfiles . merge ( ctx . attr . script [ DefaultInfo ]. default_runfiles ), 72executable = exe , 73), 74CargoBuildScriptRunfilesInfo ( 75data = ctx . attr . data , 76tools = ctx . attr . tools , 77), 78] 79 80cargo_build_script_runfiles = rule ( 81doc = """\ 82A rule for producing `cargo_build_script.script` with proper runfiles. 83 84This rule ensure's the executable for `cargo_build_script` has properly formed runfiles with `cfg=target` and 85`cfg=exec` files. This is a challenge becuase had the script binary been directly consumed, it would have been 86in either configuration which would have been incorrect for either the `tools` (exec) or `data` (target) attributes. 87This is solved here by consuming the script as exec and creating a symlink to consumers of this rule can consume 88with `cfg=target` and still get an exec compatible binary. 89 90This rule may not be necessary if it becomes possible to construct runfiles trees within a rule for an action as 91we'd be able to build the correct runfiles tree and configure the script runner to run the script in the new runfiles 92directory: 93https://github.com/bazelbuild/bazel/issues/15486 94""" , 95implementation = _cargo_build_script_runfiles_impl , 96attrs = { 97"data" : attr . label_list ( 98doc = "Data required by the build script." , 99allow_files = True , 100), 101"script" : attr . label ( 102doc = "The binary script to run, generally a `rust_binary` target." , 103executable = True , 104mandatory = True , 105providers = [ rust_common . crate_info ], 106cfg = "exec" , 107), 108"tools" : attr . label_list ( 109doc = "Tools required by the build script." , 110allow_files = True , 111cfg = "exec" , 112), 113"_copy_file" : attr . label ( 114cfg = "exec" , 115executable = True , 116default = Label ( "//cargo/private:copy_file" ), 117), 118}, 119executable = True , 120) 121 122def get_cc_compile_args_and_env ( cc_toolchain , feature_configuration ): 123"""Gather cc environment variables from the given `cc_toolchain` 124 125Args: 126cc_toolchain (cc_toolchain): The current rule's `cc_toolchain`. 127feature_configuration (FeatureConfiguration): Class used to construct command lines from CROSSTOOL features. 128 129Returns: 130tuple: A tuple of the following items: 131- (sequence): A flattened C command line flags for given action. 132- (sequence): A flattened CXX command line flags for given action. 133- (dict): C environment variables to be set for given action. 134""" 135compile_variables = cc_common . create_compile_variables ( 136feature_configuration = feature_configuration , 137cc_toolchain = cc_toolchain , 138) 139cc_c_args = cc_common . get_memory_inefficient_command_line ( 140feature_configuration = feature_configuration , 141action_name = ACTION_NAMES . c_compile , 142variables = compile_variables , 143) 144cc_cxx_args = cc_common . get_memory_inefficient_command_line ( 145feature_configuration = feature_configuration , 146action_name = ACTION_NAMES . cpp_compile , 147variables = compile_variables , 148) 149cc_env = cc_common . get_environment_variables ( 150feature_configuration = feature_configuration , 151action_name = ACTION_NAMES . c_compile , 152variables = compile_variables , 153) 154return cc_c_args , cc_cxx_args , cc_env 155 156def _pwd_flags_sysroot ( args ): 157"""Prefix execroot-relative paths of known arguments with ${pwd}. 158 159Args: 160args (list): List of tool arguments. 161 162Returns: 163list: The modified argument list. 164""" 165res = [] 166for arg in args : 167s , opt , path = arg . partition ( "--sysroot=" ) 168if s == "" and not paths . is_absolute ( path ): 169res . append ( "{}${{pwd}}/{}" . format ( opt , path )) 170else : 171res . append ( arg ) 172return res 173 174def _pwd_flags_isystem ( args ): 175"""Prefix execroot-relative paths of known arguments with ${pwd}. 176 177Args: 178args (list): List of tool arguments. 179 180Returns: 181list: The modified argument list. 182""" 183res = [] 184fix_next_arg = False 185for arg in args : 186if fix_next_arg and not paths . is_absolute ( arg ): 187res . append ( "${{pwd}}/{}" . format ( arg )) 188else : 189res . append ( arg ) 190 191fix_next_arg = arg == "-isystem" 192 193return res 194 195def _pwd_flags_fsanitize_ignorelist ( args ): 196"""Prefix execroot-relative paths of known arguments with ${pwd}. 197 198Args: 199args (list): List of tool arguments. 200 201Returns: 202list: The modified argument list. 203""" 204res = [] 205for arg in args : 206s , opt , path = arg . partition ( "-fsanitize-ignorelist=" ) 207if s == "" and not paths . is_absolute ( path ): 208res . append ( "{}${{pwd}}/{}" . format ( opt , path )) 209else : 210res . append ( arg ) 211return res 212 213def _pwd_flags ( args ): 214return _pwd_flags_fsanitize_ignorelist ( _pwd_flags_isystem ( _pwd_flags_sysroot ( args ))) 215 216def _feature_enabled ( ctx , feature_name , default = False ): 217"""Check if a feature is enabled. 218 219If the feature is explicitly enabled or disabled, return accordingly. 220 221In the case where the feature is not explicitly enabled or disabled, return the default value. 222 223Args: 224ctx: The context object. 225feature_name: The name of the feature. 226default: The default value to return if the feature is not explicitly enabled or disabled. 227 228Returns: 229Boolean defining whether the feature is enabled. 230""" 231if feature_name in ctx . disabled_features : 232return False 233 234if feature_name in ctx . features : 235return True 236 237return default 238 239def _rlocationpath ( file , workspace_name ): 240if file . short_path . startswith ( "../" ): 241return file . short_path [ len ( "../" ):] 242 243return "{}/{}" . format ( workspace_name , file . short_path ) 244 245def _create_runfiles_dir ( ctx , script , retain_list ): 246"""Create a runfiles directory to represent `CARGO_MANIFEST_DIR`. 247 248Due to the inability to forcibly generate runfiles directories for use as inputs 249to actions, this function creates a custom runfiles directory that can more 250consistently be relied upon as an input. For more details see: 251https://github.com/bazelbuild/bazel/issues/15486 252 253If runfiles directories can ever be more directly treated as an input this function 254can be retired. 255 256Args: 257ctx (ctx): The rule's context object 258script (Target): The `cargo_build_script.script` target. 259retain_list (list): A list of strings to keep in generated runfiles directories. 260 261Returns: 262Tuple[File, Depset[File], Args]: 263- The output directory to be created. 264- Runfile inputs needed by the action. 265- The args required to create the directory. 266""" 267runfiles_dir = ctx . actions . declare_directory ( "{}.cargo_runfiles" . format ( ctx . label . name )) 268 269# External repos always fall into the `../` branch of `_rlocationpath`. 270workspace_name = ctx . workspace_name 271 272def _runfiles_map ( file ): 273return "{}={}" . format ( file . path , _rlocationpath ( file , workspace_name )) 274 275runfiles = script [ DefaultInfo ]. default_runfiles 276 277args = ctx . actions . args () 278args . use_param_file ( "--cargo_manifest_args=@%s" , use_always = True ) 279args . add ( runfiles_dir . path ) 280args . add ( "," . join ( retain_list )) 281args . add_all ( runfiles . files , map_each = _runfiles_map , allow_closure = True ) 282 283return runfiles_dir , runfiles . files , args 284 285def _cargo_build_script_impl ( ctx ): 286"""The implementation for the `cargo_build_script` rule. 287 288Args: 289ctx (ctx): The rules context object 290 291Returns: 292list: A list containing a BuildInfo provider 293""" 294script = ctx . executable . script 295script_info = ctx . attr . script [ CargoBuildScriptRunfilesInfo ] 296toolchain = find_toolchain ( ctx ) 297out_dir = ctx . actions . declare_directory ( ctx . label . name + ".out_dir" ) 298env_out = ctx . actions . declare_file ( ctx . label . name + ".env" ) 299dep_env_out = ctx . actions . declare_file ( ctx . label . name + ".depenv" ) 300flags_out = ctx . actions . declare_file ( ctx . label . name + ".flags" ) 301link_flags = ctx . actions . declare_file ( ctx . label . name + ".linkflags" ) 302link_search_paths = ctx . actions . declare_file ( ctx . label . name + ".linksearchpaths" ) #rustc - link - search , propagated from transitive dependencies 303compilation_mode_opt_level = get_compilation_mode_opts ( ctx , toolchain ). opt_level 304 305script_tools = [] 306script_data = [] 307for target in script_info . data : 308script_data . append ( target [ DefaultInfo ]. files ) 309script_data . append ( target [ DefaultInfo ]. default_runfiles . files ) 310for target in script_info . tools : 311script_tools . append ( target [ DefaultInfo ]. files ) 312script_tools . append ( target [ DefaultInfo ]. default_runfiles . files ) 313 314workspace_name = ctx . label . workspace_name 315if not workspace_name : 316workspace_name = ctx . workspace_name 317 318extra_args = [] 319extra_inputs = [] 320extra_output = [] 321 322# Relying on runfiles directories is unreliable when passing data to 323# dependent actions. Instead, an explicit directory should be created 324# until more reliable functionality is implemented in Bazel: 325# https://github.com/bazelbuild/bazel/issues/15486 326incompatible_runfiles_cargo_manifest_dir = ctx . attr . _incompatible_runfiles_cargo_manifest_dir [ BuildSettingInfo ]. value 327if not incompatible_runfiles_cargo_manifest_dir : 328script_data . append ( ctx . attr . script [ DefaultInfo ]. default_runfiles . files ) 329manifest_dir = "{}.runfiles/{}/{}" . format ( script . path , workspace_name , ctx . label . package ) 330else : 331runfiles_dir , runfiles_inputs , runfiles_args = _create_runfiles_dir ( 332ctx = ctx , 333script = ctx . attr . script , 334retain_list = ctx . attr . _cargo_manifest_dir_filename_suffixes_to_retain [ BuildSettingInfo ]. value , 335) 336manifest_dir = "{}/{}/{}" . format ( runfiles_dir . path , workspace_name , ctx . label . package ) 337extra_args . append ( runfiles_args ) 338extra_inputs . append ( runfiles_inputs ) 339extra_output = [ runfiles_dir ] 340 341pkg_name = ctx . attr . pkg_name 342if pkg_name == "" : 343pkg_name = name_to_pkg_name ( ctx . label . name ) 344 345toolchain_tools = [ toolchain . all_files ] 346 347cc_toolchain = find_cpp_toolchain ( ctx ) 348 349env = dict ({}) 350 351# If enabled, start with the default shell env, which contains any --action_env 352# settings passed in on the command line and defaults like $PATH. 353if ctx . attr . use_default_shell_env : 354env . update ( ctx . configuration . default_shell_env ) 355 356env . update ({ 357"CARGO_CRATE_NAME" : name_to_crate_name ( pkg_name ), 358"CARGO_MANIFEST_DIR" : manifest_dir , 359"CARGO_PKG_NAME" : pkg_name , 360"HOST" : toolchain . exec_triple . str , 361"NUM_JOBS" : "1" , 362"OPT_LEVEL" : compilation_mode_opt_level , 363"RUSTC" : toolchain . rustc . path , 364"TARGET" : toolchain . target_flag_value , 365# OUT_DIR is set by the runner itself, rather than on the action. 366}) 367 368# This isn't exactly right, but Bazel doesn't have exact views of "debug" and "release", so... 369env . update ({ 370"DEBUG" : { "dbg" : "true" , "fastbuild" : "true" , "opt" : "false" }. get ( ctx . var [ "COMPILATION_MODE" ], "true" ), 371"PROFILE" : { "dbg" : "debug" , "fastbuild" : "debug" , "opt" : "release" }. get ( ctx . var [ "COMPILATION_MODE" ], "unknown" ), 372}) 373 374if ctx . attr . version : 375version = ctx . attr . version . split ( "+" )[ 0 ]. split ( "." ) 376patch = version [ 2 ]. split ( "-" ) if len ( version ) > 2 else [ "" ] 377env [ "CARGO_PKG_VERSION_MAJOR" ] = version [ 0 ] 378env [ "CARGO_PKG_VERSION_MINOR" ] = version [ 1 ] if len ( version ) > 1 else "" 379env [ "CARGO_PKG_VERSION_PATCH" ] = patch [ 0 ] 380env [ "CARGO_PKG_VERSION_PRE" ] = patch [ 1 ] if len ( patch ) > 1 else "" 381env [ "CARGO_PKG_VERSION" ] = ctx . attr . version 382 383# Pull in env vars which may be required for the cc_toolchain to work (e.g. on OSX, the SDK version). 384# We hope that the linker env is sufficient for the whole cc_toolchain. 385cc_toolchain , feature_configuration = find_cc_toolchain ( ctx ) 386linker , link_args , linker_env = get_linker_and_args ( ctx , "bin" , cc_toolchain , feature_configuration , None ) 387env . update ( ** linker_env ) 388env [ "LD" ] = linker 389env [ "LDFLAGS" ] = " " . join ( _pwd_flags ( link_args )) 390 391# MSVC requires INCLUDE to be set 392cc_c_args , cc_cxx_args , cc_env = get_cc_compile_args_and_env ( cc_toolchain , feature_configuration ) 393include = cc_env . get ( "INCLUDE" ) 394if include : 395env [ "INCLUDE" ] = include 396 397if cc_toolchain : 398toolchain_tools . append ( cc_toolchain . all_files ) 399 400env [ "CC" ] = cc_common . get_tool_for_action ( 401feature_configuration = feature_configuration , 402action_name = ACTION_NAMES . c_compile , 403) 404env [ "CXX" ] = cc_common . get_tool_for_action ( 405feature_configuration = feature_configuration , 406action_name = ACTION_NAMES . cpp_compile , 407) 408env [ "AR" ] = cc_common . get_tool_for_action ( 409feature_configuration = feature_configuration , 410action_name = ACTION_NAMES . cpp_link_static_library , 411) 412 413# Populate CFLAGS and CXXFLAGS that cc-rs relies on when building from source, in particular 414# to determine the deployment target when building for apple platforms (`macosx-version-min` 415# for example, itself derived from the `macos_minimum_os` Bazel argument). 416env [ "CFLAGS" ] = " " . join ( _pwd_flags ( cc_c_args )) 417env [ "CXXFLAGS" ] = " " . join ( _pwd_flags ( cc_cxx_args )) 418 419# Inform build scripts of rustc flags 420# https://github.com/rust-lang/cargo/issues/9600 421env [ "CARGO_ENCODED_RUSTFLAGS" ] = "\\x1f ". join ([ 422# Allow build scripts to locate the generated sysroot 423"--sysroot=${{pwd}}/{}" . format ( toolchain . sysroot ), 424] + ctx . attr . rustc_flags ) 425 426for f in ctx . attr . crate_features : 427env [ "CARGO_FEATURE_" + f . upper (). replace ( "-" , "_" )] = "1" 428 429links = ctx . attr . links or "" 430if links : 431env [ "CARGO_MANIFEST_LINKS" ] = links 432 433# Add environment variables from the Rust toolchain. 434env . update ( toolchain . env ) 435 436known_variables = {} 437 438# Gather data from the `toolchains` attribute. 439for target in ctx . attr . toolchains : 440if DefaultInfo in target : 441toolchain_tools . extend ([ 442target [ DefaultInfo ]. files , 443target [ DefaultInfo ]. default_runfiles . files , 444]) 445if platform_common . ToolchainInfo in target : 446all_files = getattr ( target [ platform_common . ToolchainInfo ], "all_files" , depset ([])) 447if type ( all_files ) == "list" : 448all_files = depset ( all_files ) 449toolchain_tools . append ( all_files ) 450if platform_common . TemplateVariableInfo in target : 451variables = getattr ( target [ platform_common . TemplateVariableInfo ], "variables" , depset ([])) 452known_variables . update ( variables ) 453 454_merge_env_dict ( env , expand_dict_value_locations ( 455ctx , 456ctx . attr . build_script_env , 457getattr ( ctx . attr , "data" , []) + 458getattr ( ctx . attr , "compile_data" , []) + 459getattr ( ctx . attr , "tools" , []) + 460script_info . data + 461script_info . tools , 462known_variables , 463)) 464 465tools = depset ( 466direct = [ 467script , 468ctx . executable . _cargo_build_script_runner , 469] + ([ toolchain . target_json ] if toolchain . target_json else []), 470transitive = script_data + script_tools + toolchain_tools , 471) 472 473# dep_env_file contains additional environment variables coming from 474# direct dependency sys-crates' build scripts. These need to be made 475# available to the current crate build script. 476# See https://doc.rust-lang.org/cargo/reference/build-scripts.html#-sys-packages 477# for details. 478args = ctx . actions . args () 479args . add ( script , format = "--script=%s" ) 480args . add ( links , format = "--links=%s" ) 481args . add ( out_dir . path , format = "--out_dir=%s" ) 482args . add ( env_out , format = "--env_out=%s" ) 483args . add ( flags_out , format = "--flags_out=%s" ) 484args . add ( link_flags , format = "--link_flags=%s" ) 485args . add ( link_search_paths , format = "--link_search_paths=%s" ) 486args . add ( dep_env_out , format = "--dep_env_out=%s" ) 487args . add ( ctx . attr . rundir , format = "--rundir=%s" ) 488 489output_groups = { 490"out_dir" : depset ([ out_dir ]), 491} 492 493debug_std_streams_output_group = ctx . attr . _debug_std_streams_output_group [ BuildSettingInfo ]. value 494if debug_std_streams_output_group : 495debug_stdout = ctx . actions . declare_file ( ctx . label . name + ".stdout.log" ) 496debug_stderr = ctx . actions . declare_file ( ctx . label . name + ".stderr.log" ) 497args . add ( debug_stdout , format = "--stdout=%s" ) 498args . add ( debug_stderr , format = "--stderr=%s" ) 499extra_output . append ( debug_stdout ) 500extra_output . append ( debug_stderr ) 501output_groups [ "streams" ] = depset ([ debug_stdout , debug_stderr ]) 502 503build_script_inputs = [] 504 505for dep in ctx . attr . link_deps : 506if rust_common . dep_info in dep and dep [ rust_common . dep_info ]. dep_env : 507dep_env_file = dep [ rust_common . dep_info ]. dep_env 508args . add ( dep_env_file . path , format = "--input_dep_env_path=%s" ) 509build_script_inputs . append ( dep_env_file ) 510for dep_build_info in dep [ rust_common . dep_info ]. transitive_build_infos . to_list (): 511build_script_inputs . append ( dep_build_info . out_dir ) 512 513for dep in ctx . attr . deps : 514for dep_build_info in dep [ rust_common . dep_info ]. transitive_build_infos . to_list (): 515build_script_inputs . append ( dep_build_info . out_dir ) 516 517experimental_symlink_execroot = ctx . attr . _experimental_symlink_execroot [ BuildSettingInfo ]. value or \ 518_feature_enabled ( ctx , "symlink-exec-root" ) 519 520if experimental_symlink_execroot : 521env [ "RULES_RUST_SYMLINK_EXEC_ROOT" ] = "1" 522 523ctx . actions . run ( 524executable = ctx . executable . _cargo_build_script_runner , 525arguments = [ args ] + extra_args , 526outputs = [ 527out_dir , 528env_out , 529flags_out , 530link_flags , 531link_search_paths , 532dep_env_out , 533] + extra_output , 534tools = tools , 535inputs = depset ( build_script_inputs , transitive = extra_inputs ), 536mnemonic = "CargoBuildScriptRun" , 537progress_message = "Running Cargo build script {}" . format ( pkg_name ), 538env = env , 539toolchain = None , 540# If enabled, sets the $PATH environment variable so tools like `cmake` 541# can probe $PATH for helper tools. Defaults to `True`. 542use_default_shell_env = ctx . attr . use_default_shell_env , 543) 544 545return [ 546# Although this isn't used anywhere, without this, `bazel build`'ing 547# the cargo_build_script label won't actually run the build script 548# since bazel is lazy. 549DefaultInfo ( files = depset ([ out_dir ])), 550BuildInfo ( 551out_dir = out_dir , 552rustc_env = env_out , 553dep_env = dep_env_out , 554flags = flags_out , 555linker_flags = link_flags , 556link_search_paths = link_search_paths , 557compile_data = depset ( extra_output , transitive = script_data ), 558), 559OutputGroupInfo ( 560** output_groups 561), 562] 563 564cargo_build_script = rule ( 565doc = ( 566"A rule for running a crate's `build.rs` files to generate build information " + 567"which is then used to determine how to compile said crate." 568), 569implementation = _cargo_build_script_impl , 570attrs = { 571"build_script_env" : attr . string_dict ( 572doc = "Environment variables for build scripts." , 573), 574"crate_features" : attr . string_list ( 575doc = "The list of rust features that the build script should consider activated." , 576), 577"deps" : attr . label_list ( 578doc = "The Rust build-dependencies of the crate" , 579providers = [ rust_common . dep_info ], 580cfg = "exec" , 581), 582"link_deps" : attr . label_list ( 583doc = dedent ( """\ 584The subset of the Rust (normal) dependencies of the crate that 585have the links attribute and therefore provide environment 586variables to this build script. 587""" ), 588providers = [ rust_common . dep_info ], 589), 590"links" : attr . string ( 591doc = "The name of the native library this crate links against." , 592), 593"pkg_name" : attr . string ( 594doc = "The name of package being compiled, if not derived from `name`." , 595), 596"rundir" : attr . string ( 597default = "" , 598doc = dedent ( """\ 599A directory to cd to before the cargo_build_script is run. 600 601This should be a pathrelative to the exec root. The default behaviour (and the 602behaviour if rundir is set to the empty string) is to change to the relative 603path corresponding to the cargo manifest directory, which replicates the 604normal behaviour of cargo so it is easy to write compatible build scripts. 605 606If set to `.`, the cargo build script will run in the exec root. 607""" ), 608), 609"rustc_flags" : attr . string_list ( 610doc = dedent ( """\ 611List of compiler flags passed to `rustc`. 612 613These strings are subject to Make variable expansion for predefined 614source/output path variables like `$location`, `$execpath`, and 615`$rootpath`. This expansion is useful if you wish to pass a generated 616file of arguments to rustc: `@$(location //package:target)`. 617""" ), 618), 619"script" : attr . label ( 620doc = "The binary script to run, generally a `rust_binary` target." , 621executable = True , 622mandatory = True , 623cfg = "target" , 624providers = [ CargoBuildScriptRunfilesInfo ], 625), 626"tools" : attr . label_list ( 627doc = "Tools required by the build script." , 628allow_files = True , 629cfg = "exec" , 630), 631"use_default_shell_env" : attr . bool ( 632doc = dedent ( """\ 633Whether or not to include the default shell environment for the build 634script action. By default Bazel's `default_shell_env` is set for build 635script actions so crates like `cmake` can probe $PATH to find tools. 636""" ), 637default = True , 638), 639"version" : attr . string ( 640doc = "The semantic version (semver) of the crate" , 641), 642"_cargo_build_script_runner" : attr . label ( 643executable = True , 644allow_files = True , 645default = Label ( "//cargo/cargo_build_script_runner:cargo_build_script_runner" ), 646cfg = "exec" , 647), 648"_cargo_manifest_dir_filename_suffixes_to_retain" : attr . label ( 649default = Label ( "//cargo/settings:cargo_manifest_dir_filename_suffixes_to_retain" ), 650), 651"_cc_toolchain" : attr . label ( 652default = Label ( "@bazel_tools//tools/cpp:current_cc_toolchain" ), 653), 654"_debug_std_streams_output_group" : attr . label ( 655default = Label ( "//cargo/settings:debug_std_streams_output_group" ), 656), 657"_experimental_symlink_execroot" : attr . label ( 658default = Label ( "//cargo/settings:experimental_symlink_execroot" ), 659), 660"_incompatible_runfiles_cargo_manifest_dir" : attr . label ( 661default = Label ( "//cargo/settings:incompatible_runfiles_cargo_manifest_dir" ), 662), 663}, 664fragments = [ "cpp" ], 665toolchains = [ 666str ( Label ( "//rust:toolchain_type" )), 667"@bazel_tools//tools/cpp:toolchain_type" , 668], 669) 670 671def _merge_env_dict ( prefix_dict , suffix_dict ): 672"""Merges suffix_dict into prefix_dict, appending rather than replacing certain env vars.""" 673for key in [ "CFLAGS" , "CXXFLAGS" , "LDFLAGS" ]: 674if key in prefix_dict and key in suffix_dict and prefix_dict [ key ]: 675prefix_dict [ key ] += " " + suffix_dict . pop ( key ) 676prefix_dict . update ( suffix_dict ) 677 678def name_to_pkg_name ( name ): 679"""Sanitize the name of cargo_build_script targets. 680 681Args: 682name (str): The name value pass to the `cargo_build_script` wrapper. 683 684Returns: 685str: A cleaned up name for a build script target. 686""" 687if name . endswith ( "_bs" ): 688return name [: - len ( "_bs" )] 689return name