Skip to content

Commit

Permalink
Fix #1161: set the CMAKE environment variables for prebuilt toolcha…
Browse files Browse the repository at this point in the history
…ins.

This commit is an attempt to fix #1161 by setting the `CMAKE` environment
variable (through [`TemplateVariableInfo`]).

[`TemplateVariableInfo`]: https://bazel.build/rules/lib/providers/TemplateVariableInfo

Since the CMake prebuilt packages yield multiple files, we cannot use `$(execpath)`
to get the path to the CMake binary, as we do for Ninja:

https://github.com/bazelbuild/rules_foreign_cc/blob/c2e097455d2bbf92b2ae71611d1261ba79eb8aa8/toolchains/prebuilt_toolchains.py#L449

To circumvent this issue, this commit adds a new attribute called `tools` to
the [`native_tool_toolchain`] rule. Through this attribute, extra targets
can be passed to [`_resolve_tool_path`] in order to be able to expand the path
to a single file using `$(execpath)`.

[`native_tool_toolchain`]: https://github.com/bazelbuild/rules_foreign_cc/blob/c2e097455d2bbf92b2ae71611d1261ba79eb8aa8/toolchains/native_tools/native_tools_toolchain.bzl#L40
[`_resolve_tool_path`]: https://github.com/bazelbuild/rules_foreign_cc/blob/c2e097455d2bbf92b2ae71611d1261ba79eb8aa8/toolchains/native_tools/native_tools_toolchain.bzl#L22
  • Loading branch information
thb-sb committed Feb 5, 2024
1 parent 4831827 commit 96d92b9
Show file tree
Hide file tree
Showing 3 changed files with 397 additions and 373 deletions.
18 changes: 14 additions & 4 deletions toolchains/native_tools/native_tools_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ ToolInfo = provider(
},
)

def _resolve_tool_path(ctx, path, target):
def _resolve_tool_path(ctx, path, target, tools):
"""
Resolve the path to a tool.
Expand All @@ -32,7 +32,7 @@ def _resolve_tool_path(ctx, path, target):
_, resolved_bash_command, _ = ctx.resolve_command(
command = path,
expand_locations = True,
tools = [target],
tools = tools + [target],
)

return resolved_bash_command[-1]
Expand All @@ -43,10 +43,10 @@ def _native_tool_toolchain_impl(ctx):
path = None
env = {}
if ctx.attr.target:
path = _resolve_tool_path(ctx, ctx.attr.path, ctx.attr.target)
path = _resolve_tool_path(ctx, ctx.attr.path, ctx.attr.target, ctx.attr.tools)

for k, v in ctx.attr.env.items():
env[k] = _resolve_tool_path(ctx, v, ctx.attr.target)
env[k] = _resolve_tool_path(ctx, v, ctx.attr.target, ctx.attr.tools)

else:
path = ctx.expand_location(ctx.attr.path)
Expand Down Expand Up @@ -88,6 +88,16 @@ native_tool_toolchain = rule(
),
allow_files = True,
),
"tools": attr.label_list(
mandatory = False,
cfg = "exec",
doc = (
"Additional tools." +
"If `target` expands to several files, `tools` can be used to " +
"isolate a specific file that can be used in `env`."
),
allow_files = True,
),
},
incompatible_use_toolchain_transition = True,
)
Loading

0 comments on commit 96d92b9

Please sign in to comment.