Skip to content

Commit

Permalink
Expand make variables in env
Browse files Browse the repository at this point in the history
  • Loading branch information
illicitonion committed Sep 20, 2021
1 parent 7da37f8 commit da14d17
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion foreign_cc/built_tools/private/built_tools_framework.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ load("//foreign_cc/private/framework:helpers.bzl", "convert_shell_script", "sheb
# Common attributes for all built_tool rules
FOREIGN_CC_BUILT_TOOLS_ATTRS = {
"env": attr.string_dict(
doc = "Environment variables to set during the build.",
doc = "Environment variables to set during the build. This attribute is subject to make variable substitution.",
default = {},
),
"srcs": attr.label(
Expand Down
3 changes: 2 additions & 1 deletion foreign_cc/cmake.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ load(
"cc_external_rule_impl",
"create_attrs",
"expand_locations",
"expand_make_variables",
)
load("//foreign_cc/private:transitions.bzl", "make_variant")
load(
Expand Down Expand Up @@ -262,7 +263,7 @@ def _create_configure_script(configureParameters):
root = root,
no_toolchain_file = no_toolchain_file,
user_cache = dict(ctx.attr.cache_entries),
user_env = expand_locations(ctx, ctx.attr.env, data),
user_env = expand_locations(ctx, expand_make_variables(ctx, "env", ctx.attr.env), data),
options = attrs.generate_args,
cmake_commands = cmake_commands,
cmake_prefix = prefix,
Expand Down
3 changes: 2 additions & 1 deletion foreign_cc/configure.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ load(
"cc_external_rule_impl",
"create_attrs",
"expand_locations",
"expand_make_variables",
)
load("//foreign_cc/private:transitions.bzl", "make_variant")
load("//foreign_cc/private/framework:platform.bzl", "os_name")
Expand Down Expand Up @@ -74,7 +75,7 @@ def _create_configure_script(configureParameters):
for arg in ctx.attr.args
])

user_env = expand_locations(ctx, ctx.attr.env, data)
user_env = expand_locations(ctx, expand_make_variables(ctx, "env", ctx.attr.env), data)

make_commands = []
prefix = "{} ".format(expand_locations(ctx, attrs.tool_prefix, data)) if attrs.tool_prefix else ""
Expand Down
19 changes: 19 additions & 0 deletions foreign_cc/private/framework.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ CC_EXTERNAL_RULE_ATTRIBUTES = {
"`$(execpath)` macros may be used to point at files which are listed as `data`, `deps`, or `build_data`, " +
"but unlike with other rules, these will be replaced with absolute paths to those files, " +
"because the build does not run in the exec root. " +
"This attribute is subject to make variable substitution. " +
"No other macros are supported." +
"Variables containing `PATH` (e.g. `PATH`, `LD_LIBRARY_PATH`, `CPATH`) entries will be prepended to the existing variable."
),
Expand Down Expand Up @@ -952,3 +953,21 @@ def expand_locations(ctx, expandable, data):
return ctx.expand_location(expandable.replace("$(execpath ", "$$EXT_BUILD_ROOT$$/$(execpath "), data)
else:
fail("Unsupported type: {}".format(type(expandable)))

def expand_make_variables(ctx, attr_name, data):
"""Expand make variables on a dictionary
See https://docs.bazel.build/versions/main/be/make-variables.html
Args:
ctx (ctx): The rule's context object
attr_name (str): Name of the attr of the rule being expanded
data (dict): Dict whose values to expand (must be dict[str, str])
Returns:
dict: An expanded dict of environment variables
"""
return {
k: ctx.expand_make_variables(attr_name, v, {})
for k, v in data.items()
}

0 comments on commit da14d17

Please sign in to comment.