Skip to content

Commit f7652d7

Browse files
authored
fix: allow rules to consume tools env data (#1397)
1 parent fcd644e commit f7652d7

File tree

5 files changed

+41
-7
lines changed

5 files changed

+41
-7
lines changed

foreign_cc/built_tools/private/built_tools_framework.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def built_tool_rule_impl(ctx, script_lines, out_dir, mnemonic, additional_tools
112112

113113
root = detect_root(ctx.attr.srcs)
114114
lib_name = ctx.attr.name
115-
env_prelude = get_env_prelude(ctx, out_dir.path, [])
115+
env_prelude = get_env_prelude(ctx, out_dir.path, [], {})
116116

117117
cc_toolchain = find_cpp_toolchain(ctx)
118118

foreign_cc/cmake.bzl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,18 @@ load(
156156
load(
157157
"//toolchains/native_tools:tool_access.bzl",
158158
"get_cmake_data",
159+
"get_m4_data",
159160
"get_make_data",
160161
"get_ninja_data",
162+
"get_pkgconfig_data",
161163
)
162164

163165
def _cmake_impl(ctx):
164166
cmake_data = get_cmake_data(ctx)
167+
pkgconfig_data = get_pkgconfig_data(ctx)
168+
m4_data = get_m4_data(ctx)
165169

166-
tools_data = [cmake_data]
170+
tools_data = [cmake_data, pkgconfig_data, m4_data]
167171

168172
env = dict(ctx.attr.env)
169173

foreign_cc/configure.bzl

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,29 @@ load(
2121
"expand_locations_and_make_variables",
2222
)
2323
load("//foreign_cc/private:transitions.bzl", "foreign_cc_rule_variant")
24-
load("//toolchains/native_tools:tool_access.bzl", "get_make_data", "get_pkgconfig_data")
24+
load(
25+
"//toolchains/native_tools:tool_access.bzl",
26+
"get_autoconf_data",
27+
"get_automake_data",
28+
"get_m4_data",
29+
"get_make_data",
30+
"get_pkgconfig_data",
31+
)
2532

2633
def _configure_make(ctx):
2734
make_data = get_make_data(ctx)
2835
pkg_config_data = get_pkgconfig_data(ctx)
36+
autoconf_data = get_autoconf_data(ctx)
37+
automake_data = get_automake_data(ctx)
38+
m4_data = get_m4_data(ctx)
2939

30-
tools_data = [make_data, pkg_config_data]
40+
tools_data = [
41+
make_data,
42+
pkg_config_data,
43+
autoconf_data,
44+
automake_data,
45+
m4_data,
46+
]
3147

3248
if ctx.attr.autogen and not ctx.attr.configure_in_place:
3349
fail("`autogen` requires `configure_in_place = True`. Please update {}".format(

foreign_cc/private/framework.bzl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,13 +310,14 @@ dependencies.""",
310310
def _is_msvc_var(var):
311311
return var == "INCLUDE" or var == "LIB"
312312

313-
def get_env_prelude(ctx, installdir, data_dependencies):
313+
def get_env_prelude(ctx, installdir, data_dependencies, tools_env):
314314
"""Generate a bash snippet containing environment variable definitions
315315
316316
Args:
317317
ctx (ctx): The rule's context object
318318
installdir (str): The path from the root target's directory in the build output
319319
data_dependencies (list): A list of targets representing dependencies
320+
tools_env (dict): A dictionary of environment variables from toolchain
320321
321322
Returns:
322323
tuple: A list of environment variables to define in the build script and a dict
@@ -330,6 +331,7 @@ def get_env_prelude(ctx, installdir, data_dependencies):
330331
]
331332

332333
env = dict()
334+
env.update(tools_env)
333335

334336
# Add all environment variables from the cc_toolchain
335337
cc_toolchain = find_cpp_toolchain(ctx)
@@ -455,15 +457,18 @@ def cc_external_rule_impl(ctx, attrs):
455457
target_root = paths.dirname(installdir_copy.file.dirname)
456458

457459
data_dependencies = ctx.attr.data + ctx.attr.build_data + ctx.attr.toolchains
460+
tools_env = {}
458461
for tool in attrs.tools_data:
459462
if tool.target:
460463
data_dependencies.append(tool.target)
464+
if tool.env:
465+
tools_env.update(tool.env)
461466

462467
# Also add legacy dependencies while they're still available
463468
data_dependencies += ctx.attr.tools_deps + ctx.attr.additional_tools
464469

465470
installdir = target_root + "/" + lib_name
466-
env_prelude = get_env_prelude(ctx, installdir, data_dependencies)
471+
env_prelude = get_env_prelude(ctx, installdir, data_dependencies, tools_env)
467472

468473
if not attrs.postfix_script:
469474
postfix_script = []

toolchains/native_tools/tool_access.bzl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,22 @@ def _access_and_expect_label_copied(toolchain_type_, ctx):
4747
# This could be made more efficient by changing the
4848
# toolchain to provide the executable as a target
4949
cmd_file = tool_data
50+
tool_env = dict(tool_data.env)
51+
5052
for f in tool_data.target.files.to_list():
5153
if f.path.endswith("/" + tool_data.path):
5254
cmd_file = f
5355
break
56+
57+
# Environment vars for tools such as MAKE and CMAKE needs to be absolute
58+
# as they are used from build_tmpdir and not bazel's exec/sandbox root
59+
for k, v in tool_env.items():
60+
if v.endswith(tool_data.path):
61+
tool_env[k] = "$EXT_BUILD_ROOT/{}".format(cmd_file.path)
62+
5463
return struct(
5564
target = tool_data.target,
56-
env = tool_data.env,
65+
env = tool_env,
5766
# as the tool will be copied into tools directory
5867
path = "$EXT_BUILD_ROOT/{}".format(cmd_file.path),
5968
)

0 commit comments

Comments
 (0)