Skip to content

Commit 7729ee3

Browse files
authored
fix undefined variables in the wrap_outputs script (#1278)
1 parent 6e54230 commit 7729ee3

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

foreign_cc/built_tools/private/built_tools_framework.bzl

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,13 @@ def built_tool_rule_impl(ctx, script_lines, out_dir, mnemonic, additional_tools
7070

7171
root = detect_root(ctx.attr.srcs)
7272
lib_name = ctx.attr.name
73-
env_prelude = get_env_prelude(ctx, lib_name, [], "")
73+
env_prelude = get_env_prelude(ctx, out_dir.path, [])
7474

7575
cc_toolchain = find_cpp_toolchain(ctx)
7676

77-
script = env_prelude + [
77+
script = [
7878
"##script_prelude##",
79-
"export EXT_BUILD_ROOT=##pwd##",
80-
"export INSTALLDIR=$$EXT_BUILD_ROOT$$/{}".format(out_dir.path),
81-
"export BUILD_TMPDIR=$$INSTALLDIR$$.build_tmpdir",
79+
] + env_prelude + [
8280
"##rm_rf## $$INSTALLDIR$$",
8381
"##rm_rf## $$BUILD_TMPDIR$$",
8482
"##mkdirs## $$INSTALLDIR$$",
@@ -97,7 +95,13 @@ def built_tool_rule_impl(ctx, script_lines, out_dir, mnemonic, additional_tools
9795
"",
9896
])
9997

100-
wrapped_outputs = wrap_outputs(ctx, lib_name, mnemonic, script_text)
98+
wrapped_outputs = wrap_outputs(
99+
ctx,
100+
lib_name = lib_name,
101+
configure_name = mnemonic,
102+
env_prelude = env_prelude,
103+
script_text = script_text,
104+
)
101105

102106
tools = depset(
103107
[wrapped_outputs.wrapper_script_file, wrapped_outputs.script_file],

foreign_cc/private/framework.bzl

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -298,22 +298,21 @@ dependencies.""",
298298
def _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

Comments
 (0)