Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add current_*_toolchain rules to allow passing current toolchain inst… #843

Merged
merged 5 commits into from
Mar 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions foreign_cc/built_tools/cmake_build.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def cmake_tool(name, srcs, **kwargs):
tags = ["manual"] + kwargs.pop("tags", [])

configure_make(
name = "_build_{}".format(name),
name = "{}.build".format(name),
configure_command = "bootstrap",
configure_options = ["--", "-DCMAKE_MAKE_PROGRAM=$$MAKE$$"],
# On macOS at least -DDEBUG gets set for a fastbuild
Expand All @@ -24,7 +24,7 @@ def cmake_tool(name, srcs, **kwargs):

native.filegroup(
name = name,
srcs = ["_build_{}".format(name)],
srcs = ["{}.build".format(name)],
output_group = "gen_dir",
tags = tags,
)
41 changes: 41 additions & 0 deletions toolchains/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("//foreign_cc/built_tools:cmake_build.bzl", "cmake_tool")
load("//foreign_cc/built_tools:make_build.bzl", "make_tool")
load("//foreign_cc/built_tools:ninja_build.bzl", "ninja_tool")
load("//toolchains:toolchains.bzl", "current_autoconf_toolchain", "current_automake_toolchain", "current_cmake_toolchain", "current_m4_toolchain", "current_make_toolchain", "current_ninja_toolchain", "current_pkgconfig_toolchain")
load("//toolchains/native_tools:native_tools_toolchain.bzl", "native_tool_toolchain")

package(default_visibility = ["//visibility:public"])
Expand Down Expand Up @@ -34,6 +35,34 @@ toolchain_type(
name = "automake_toolchain",
)

current_cmake_toolchain(
name = "current_cmake_toolchain",
)

current_ninja_toolchain(
name = "current_ninja_toolchain",
)

current_m4_toolchain(
name = "current_m4_toolchain",
)

current_pkgconfig_toolchain(
name = "current_pkgconfig_toolchain",
)

current_make_toolchain(
name = "current_make_toolchain",
)

current_automake_toolchain(
name = "current_automake_toolchain",
)

current_autoconf_toolchain(
name = "current_autoconf_toolchain",
)

toolchain(
name = "built_cmake_toolchain",
toolchain = ":built_cmake",
Expand Down Expand Up @@ -106,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 @@ -138,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 @@ -152,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
12 changes: 12 additions & 0 deletions toolchains/native_tools/native_tools_toolchain.bzl
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
"""Rules for building native build tools such as ninja, make or cmake"""

# buildifier: disable=bzl-visibility
load("//foreign_cc/private:framework.bzl", "expand_locations_and_make_variables")

# 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 +28,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_and_make_variables(ctx, ctx.attr.env, "env", [ctx.attr.target])
else:
path = ctx.expand_location(ctx.attr.path)
env = expand_locations_and_make_variables(ctx, ctx.attr.env, "env", [])
return platform_common.ToolchainInfo(data = ToolInfo(
env = env,
path = path,
target = ctx.attr.target,
))
Expand All @@ -38,6 +47,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