Skip to content

Commit

Permalink
Add ability to store environment variables in native_tools ToolInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
jsharpe committed Feb 20, 2022
1 parent 3220ac5 commit 10fbf67
Show file tree
Hide file tree
Showing 6 changed files with 212 additions and 21 deletions.
12 changes: 12 additions & 0 deletions toolchains/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ make_tool(

native_tool_toolchain(
name = "built_make",
env = select({
"@platforms//os:windows": {"MAKE": "$(execpath :make_tool)/bin/make.exe"},
"//conditions:default": {"MAKE": "$(execpath :make_tool)/bin/make"},
}),
path = select({
"@platforms//os:windows": "$(execpath :make_tool)/bin/make.exe",
"//conditions:default": "$(execpath :make_tool)/bin/make",
Expand Down Expand Up @@ -167,6 +171,10 @@ native_tool_toolchain(

native_tool_toolchain(
name = "preinstalled_ninja",
env = select({
"@platforms//os:windows": {"NINJA": "ninja.exe"},
"//conditions:default": {"NINJA": "ninja"},
}),
path = select({
"@platforms//os:windows": "ninja.exe",
"//conditions:default": "ninja",
Expand All @@ -181,6 +189,10 @@ ninja_tool(

native_tool_toolchain(
name = "built_ninja",
env = select({
"@platforms//os:windows": {"NINJA": "$(execpath :ninja_tool)/bin/ninja.exe"},
"//conditions:default": {"NINJA": "$(execpath :ninja_tool)/bin/ninja"},
}),
path = select({
"@platforms//os:windows": "$(execpath :ninja_tool)/bin/ninja.exe",
"//conditions:default": "$(execpath :ninja_tool)/bin/ninja",
Expand Down
9 changes: 9 additions & 0 deletions toolchains/native_tools/native_tools_toolchain.bzl
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
load("//foreign_cc/private:framework.bzl", "expand_locations")

# buildifier: disable=module-docstring
ToolInfo = provider(
doc = "Information about the native tool",
fields = {
"env": "Environment variables to set when using this tool e.g. M4",
"path": (
"Absolute path to the tool in case the tool is preinstalled on the machine. " +
"Relative path to the tool in case the tool is built as part of a build; the path should be relative " +
Expand All @@ -22,9 +25,12 @@ def _native_tool_toolchain_impl(ctx):
path = None
if ctx.attr.target:
path = ctx.expand_location(ctx.attr.path, targets = [ctx.attr.target])
env = expand_locations(ctx, ctx.attr.env, [ctx.attr.target])
else:
path = ctx.expand_location(ctx.attr.path)
env = expand_locations(ctx, ctx.attr.env, [])
return platform_common.ToolchainInfo(data = ToolInfo(
env = env,
path = path,
target = ctx.attr.target,
))
Expand All @@ -38,6 +44,9 @@ native_tool_toolchain = rule(
),
implementation = _native_tool_toolchain_impl,
attrs = {
"env": attr.string_dict(
doc = "Environment variables to be set when using this tool e.g. M4",
),
"path": attr.string(
mandatory = False,
doc = (
Expand Down
30 changes: 13 additions & 17 deletions toolchains/native_tools/tool_access.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,44 @@
rules_foreign_cc toolchains
"""

load(":native_tools_toolchain.bzl", "ToolInfo")

def access_tool(toolchain_type_, ctx, tool_name):
def access_tool(toolchain_type_, ctx):
"""A helper macro for getting the path to a build tool's executable
Args:
toolchain_type_ (str): The name of the toolchain type
ctx (ctx): The rule's context object
tool_name (str): The name of the tool to query
Returns:
ToolInfo: A provider containing information about the toolchain's executable
"""
tool_toolchain = ctx.toolchains[toolchain_type_]
if tool_toolchain:
return tool_toolchain.data
return ToolInfo(
path = tool_name,
target = None,
)
fail("No toolchain found for " + toolchain_type_)

def get_autoconf_data(ctx):
return _access_and_expect_label_copied(str(Label("//toolchains:autoconf_toolchain")), ctx, "autoconf")
return _access_and_expect_label_copied(str(Label("//toolchains:autoconf_toolchain")), ctx)

def get_automake_data(ctx):
return _access_and_expect_label_copied(str(Label("//toolchains:automake_toolchain")), ctx, "automake")
return _access_and_expect_label_copied(str(Label("//toolchains:automake_toolchain")), ctx)

def get_cmake_data(ctx):
return _access_and_expect_label_copied(str(Label("//toolchains:cmake_toolchain")), ctx, "cmake")
return _access_and_expect_label_copied(str(Label("//toolchains:cmake_toolchain")), ctx)

def get_m4_data(ctx):
return _access_and_expect_label_copied(str(Label("//toolchains:m4_toolchain")), ctx, "m4")
return _access_and_expect_label_copied(str(Label("//toolchains:m4_toolchain")), ctx)

def get_make_data(ctx):
return _access_and_expect_label_copied(str(Label("//toolchains:make_toolchain")), ctx, "make")
return _access_and_expect_label_copied(str(Label("//toolchains:make_toolchain")), ctx)

def get_ninja_data(ctx):
return _access_and_expect_label_copied(str(Label("//toolchains:ninja_toolchain")), ctx, "ninja")
return _access_and_expect_label_copied(str(Label("//toolchains:ninja_toolchain")), ctx)

def get_pkgconfig_data(ctx):
return _access_and_expect_label_copied(str(Label("//toolchains:pkgconfig_toolchain")), ctx, "pkg-config")
return _access_and_expect_label_copied(str(Label("//toolchains:pkgconfig_toolchain")), ctx)

def _access_and_expect_label_copied(toolchain_type_, ctx, tool_name):
tool_data = access_tool(toolchain_type_, ctx, tool_name)
def _access_and_expect_label_copied(toolchain_type_, ctx):
tool_data = access_tool(toolchain_type_, ctx)
if tool_data.target:
# This could be made more efficient by changing the
# toolchain to provide the executable as a target
Expand All @@ -56,11 +50,13 @@ def _access_and_expect_label_copied(toolchain_type_, ctx, tool_name):
break
return struct(
deps = [tool_data.target],
env = tool_data.env,
# as the tool will be copied into tools directory
path = "$EXT_BUILD_ROOT/{}".format(cmd_file.path),
)
else:
return struct(
deps = [],
env = tool_data.env,
path = tool_data.path,
)
Loading

0 comments on commit 10fbf67

Please sign in to comment.