@@ -298,22 +298,21 @@ dependencies.""",
298
298
def _is_msvc_var (var ):
299
299
return var == "INCLUDE" or var == "LIB"
300
300
301
- def get_env_prelude (ctx , lib_name , data_dependencies , target_root ):
301
+ def get_env_prelude (ctx , installdir , data_dependencies ):
302
302
"""Generate a bash snippet containing environment variable definitions
303
303
304
304
Args:
305
305
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
307
307
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
309
308
310
309
Returns:
311
310
tuple: A list of environment variables to define in the build script and a dict
312
311
of environment variables
313
312
"""
314
313
env_snippet = [
315
314
"export EXT_BUILD_ROOT=##pwd##" ,
316
- "export INSTALLDIR=$$EXT_BUILD_ROOT$$/" + target_root + "/" + lib_name ,
315
+ "export INSTALLDIR=$$EXT_BUILD_ROOT$$/" + installdir ,
317
316
"export BUILD_TMPDIR=$$INSTALLDIR$$.build_tmpdir" ,
318
317
"export EXT_BUILD_DEPS=$$INSTALLDIR$$.ext_build_deps" ,
319
318
]
@@ -447,7 +446,8 @@ def cc_external_rule_impl(ctx, attrs):
447
446
# Also add legacy dependencies while they're still available
448
447
data_dependencies += ctx .attr .tools_deps + ctx .attr .additional_tools
449
448
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 )
451
451
452
452
postfix_script = [attrs .postfix_script ]
453
453
if not attrs .postfix_script :
@@ -491,7 +491,13 @@ def cc_external_rule_impl(ctx, attrs):
491
491
convert_shell_script (ctx , script_lines ),
492
492
"" ,
493
493
])
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
+ )
495
501
496
502
rule_outputs = outputs .declared_outputs + [installdir_copy .file ]
497
503
cc_toolchain = find_cpp_toolchain (ctx )
@@ -595,7 +601,7 @@ WrappedOutputs = provider(
595
601
)
596
602
597
603
# 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 ):
599
605
extension = script_extension (ctx )
600
606
build_log_file = ctx .actions .declare_file ("{}_foreign_cc/{}.log" .format (lib_name , configure_name ))
601
607
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 =
610
616
cleanup_on_success_function = create_function (
611
617
ctx ,
612
618
"cleanup_on_success" ,
613
- "rm -rf $BUILD_TMPDIR $ EXT_BUILD_DEPS" ,
619
+ "rm -rf $$ BUILD_TMPDIR$$ $$ EXT_BUILD_DEPS$$ " ,
614
620
)
615
621
cleanup_on_failure_function = create_function (
616
622
ctx ,
617
623
"cleanup_on_failure" ,
618
624
"\n " .join ([
619
625
"##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.\" " ,
622
626
"##echo## \" rules_foreign_cc: Printing build logs:\" " ,
623
627
"##echo## \" _____ BEGIN BUILD LOGS _____\" " ,
624
628
"##cat## $$BUILD_LOG$$" ,
625
629
"##echo## \" _____ END BUILD LOGS _____\" " ,
626
630
"##echo## \" rules_foreign_cc: Build wrapper script location: $$BUILD_WRAPPER_SCRIPT$$\" " ,
627
631
"##echo## \" rules_foreign_cc: Build script location: $$BUILD_SCRIPT$$\" " ,
628
632
"##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$$\" " ,
629
637
"##echo## \" \" " ,
630
638
]),
631
639
)
632
640
trap_function = "##cleanup_function## cleanup_on_success cleanup_on_failure"
633
641
634
642
build_command_lines = [
643
+ "##script_prelude##" ,
635
644
"##assert_script_errors##" ,
636
645
cleanup_on_success_function ,
637
646
cleanup_on_failure_function ,
638
647
# the call trap is defined inside, in a way how the shell function should be called
639
648
# see, for instance, linux_commands.bzl
640
649
trap_function ,
650
+ ] + env_prelude + [
641
651
"export BUILD_WRAPPER_SCRIPT=\" {}\" " .format (wrapper_script_file .path ),
642
652
"export BUILD_SCRIPT=\" {}\" " .format (build_script_file .path ),
643
653
"export BUILD_LOG=\" {}\" " .format (build_log_file .path ),
0 commit comments