@@ -298,22 +298,21 @@ dependencies.""",
298298def _is_msvc_var (var ):
299299 return var == "INCLUDE" or var == "LIB"
300300
301- def get_env_prelude (ctx , lib_name , data_dependencies , target_root ):
301+ def get_env_prelude (ctx , installdir , data_dependencies ):
302302 """Generate a bash snippet containing environment variable definitions
303303
304304 Args:
305305 ctx (ctx): The rule's context object
306- lib_name (str): The name of the target being built
306+ installdir (str): The path from the root target's directory in the build output
307307 data_dependencies (list): A list of targets representing dependencies
308- target_root (str): The path from the root target's directory in the build output
309308
310309 Returns:
311310 tuple: A list of environment variables to define in the build script and a dict
312311 of environment variables
313312 """
314313 env_snippet = [
315314 "export EXT_BUILD_ROOT=##pwd##" ,
316- "export INSTALLDIR=$$EXT_BUILD_ROOT$$/" + target_root + "/" + lib_name ,
315+ "export INSTALLDIR=$$EXT_BUILD_ROOT$$/" + installdir ,
317316 "export BUILD_TMPDIR=$$INSTALLDIR$$.build_tmpdir" ,
318317 "export EXT_BUILD_DEPS=$$INSTALLDIR$$.ext_build_deps" ,
319318 ]
@@ -447,7 +446,8 @@ def cc_external_rule_impl(ctx, attrs):
447446 # Also add legacy dependencies while they're still available
448447 data_dependencies += ctx .attr .tools_deps + ctx .attr .additional_tools
449448
450- env_prelude = get_env_prelude (ctx , lib_name , data_dependencies , target_root )
449+ installdir = target_root + "/" + lib_name
450+ env_prelude = get_env_prelude (ctx , installdir , data_dependencies )
451451
452452 postfix_script = [attrs .postfix_script ]
453453 if not attrs .postfix_script :
@@ -491,7 +491,13 @@ def cc_external_rule_impl(ctx, attrs):
491491 convert_shell_script (ctx , script_lines ),
492492 "" ,
493493 ])
494- wrapped_outputs = wrap_outputs (ctx , lib_name , attrs .configure_name , script_text )
494+ wrapped_outputs = wrap_outputs (
495+ ctx ,
496+ lib_name = lib_name ,
497+ configure_name = attrs .configure_name ,
498+ script_text = script_text ,
499+ env_prelude = env_prelude ,
500+ )
495501
496502 rule_outputs = outputs .declared_outputs + [installdir_copy .file ]
497503 cc_toolchain = find_cpp_toolchain (ctx )
@@ -595,7 +601,7 @@ WrappedOutputs = provider(
595601)
596602
597603# buildifier: disable=function-docstring
598- def wrap_outputs (ctx , lib_name , configure_name , script_text , build_script_file = None ):
604+ def wrap_outputs (ctx , lib_name , configure_name , script_text , env_prelude , build_script_file = None ):
599605 extension = script_extension (ctx )
600606 build_log_file = ctx .actions .declare_file ("{}_foreign_cc/{}.log" .format (lib_name , configure_name ))
601607 build_script_file = ctx .actions .declare_file ("{}_foreign_cc/build_script{}" .format (lib_name , extension ))
@@ -610,34 +616,38 @@ def wrap_outputs(ctx, lib_name, configure_name, script_text, build_script_file =
610616 cleanup_on_success_function = create_function (
611617 ctx ,
612618 "cleanup_on_success" ,
613- "rm -rf $BUILD_TMPDIR $ EXT_BUILD_DEPS" ,
619+ "rm -rf $$ BUILD_TMPDIR$$ $$ EXT_BUILD_DEPS$$ " ,
614620 )
615621 cleanup_on_failure_function = create_function (
616622 ctx ,
617623 "cleanup_on_failure" ,
618624 "\n " .join ([
619625 "##echo## \" rules_foreign_cc: Build failed!\" " ,
620- "##echo## \" rules_foreign_cc: Keeping temp build directory $$BUILD_TMPDIR$$ and dependencies directory $$EXT_BUILD_DEPS$$ for debug.\" " ,
621- "##echo## \" rules_foreign_cc: Please note that the directories inside a sandbox are still cleaned unless you specify '--sandbox_debug' Bazel command line flag.\" " ,
622626 "##echo## \" rules_foreign_cc: Printing build logs:\" " ,
623627 "##echo## \" _____ BEGIN BUILD LOGS _____\" " ,
624628 "##cat## $$BUILD_LOG$$" ,
625629 "##echo## \" _____ END BUILD LOGS _____\" " ,
626630 "##echo## \" rules_foreign_cc: Build wrapper script location: $$BUILD_WRAPPER_SCRIPT$$\" " ,
627631 "##echo## \" rules_foreign_cc: Build script location: $$BUILD_SCRIPT$$\" " ,
628632 "##echo## \" rules_foreign_cc: Build log location: $$BUILD_LOG$$\" " ,
633+ "##echo## \" rules_foreign_cc: Keeping these below directories for debug, but note that the directories inside a sandbox\" " ,
634+ "##echo## \" rules_foreign_cc: are still cleaned unless you specify the '--sandbox_debug' Bazel command line flag.\" " ,
635+ "##echo## \" rules_foreign_cc: Build Dir: $$BUILD_TMPDIR$$\" " ,
636+ "##echo## \" rules_foreign_cc: Deps Dir: $$EXT_BUILD_DEPS$$\" " ,
629637 "##echo## \" \" " ,
630638 ]),
631639 )
632640 trap_function = "##cleanup_function## cleanup_on_success cleanup_on_failure"
633641
634642 build_command_lines = [
643+ "##script_prelude##" ,
635644 "##assert_script_errors##" ,
636645 cleanup_on_success_function ,
637646 cleanup_on_failure_function ,
638647 # the call trap is defined inside, in a way how the shell function should be called
639648 # see, for instance, linux_commands.bzl
640649 trap_function ,
650+ ] + env_prelude + [
641651 "export BUILD_WRAPPER_SCRIPT=\" {}\" " .format (wrapper_script_file .path ),
642652 "export BUILD_SCRIPT=\" {}\" " .format (build_script_file .path ),
643653 "export BUILD_LOG=\" {}\" " .format (build_log_file .path ),
0 commit comments