Moved more toolchain related rules into `./toolchains` (#541)

* Moved more toolchain related rules into `./toolchains`

* Updated toolchain locations in rules
This commit is contained in:
UebelAndre 2021-03-11 08:52:36 -08:00 committed by GitHub
parent c309ec1319
commit 43a43353ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
30 changed files with 469 additions and 317 deletions

View File

@ -15,6 +15,7 @@ bzl_library(
deps = [
":bazel_tools_bzl_srcs",
"//for_workspace:bzl_srcs",
"//foreign_cc:bzl_srcs",
"//toolchains:bzl_srcs",
"//tools/build_defs:bzl_srcs",
],

View File

@ -261,7 +261,7 @@ Rule for building Make. Invokes configure script and make install.
native_tool_toolchain(<a href="#native_tool_toolchain-name">name</a>, <a href="#native_tool_toolchain-path">path</a>, <a href="#native_tool_toolchain-target">target</a>)
</pre>
Rule for defining the toolchain data of the native tools (cmake, ninja), to be used by rules_foreign_cc with toolchain types `@rules_foreign_cc//tools/build_defs:cmake_toolchain` and `@rules_foreign_cc//tools/build_defs:ninja_toolchain`.
Rule for defining the toolchain data of the native tools (cmake, ninja), to be used by rules_foreign_cc with toolchain types `@rules_foreign_cc//toolchains:cmake_toolchain` and `@rules_foreign_cc//toolchains:ninja_toolchain`.
**ATTRIBUTES**
@ -487,7 +487,7 @@ Call this function from the WORKSPACE file to initialize rules_foreign_cc de
| Name | Description | Default Value |
| :------------- | :------------- | :------------- |
| <a id="rules_foreign_cc_dependencies-native_tools_toolchains"></a>native_tools_toolchains | pass the toolchains for toolchain types '@rules_foreign_cc//tools/build_defs:cmake_toolchain' and '@rules_foreign_cc//tools/build_defs:ninja_toolchain' with the needed platform constraints. If you do not pass anything, registered default toolchains will be selected (see below). | <code>[]</code> |
| <a id="rules_foreign_cc_dependencies-native_tools_toolchains"></a>native_tools_toolchains | pass the toolchains for toolchain types '@rules_foreign_cc//toolchains:cmake_toolchain' and '@rules_foreign_cc//toolchains:ninja_toolchain' with the needed platform constraints. If you do not pass anything, registered default toolchains will be selected (see below). | <code>[]</code> |
| <a id="rules_foreign_cc_dependencies-register_default_tools"></a>register_default_tools | If True, the cmake and ninja toolchains, calling corresponding preinstalled binaries by name (cmake, ninja) will be registered after 'native_tools_toolchains' without any platform constraints. The default is True. | <code>True</code> |
| <a id="rules_foreign_cc_dependencies-cmake_version"></a>cmake_version | The target version of the cmake toolchain if <code>register_default_tools</code> or <code>register_built_tools</code> is set to <code>True</code>. | <code>"3.19.6"</code> |
| <a id="rules_foreign_cc_dependencies-make_version"></a>make_version | The target version of the default make toolchain if <code>register_built_tools</code> is set to <code>True</code>. | <code>"4.3"</code> |

View File

@ -1,9 +1,14 @@
"""A module exporting symbols for Stardoc generation."""
load("@rules_foreign_cc//:workspace_definitions.bzl", _rules_foreign_cc_dependencies = "rules_foreign_cc_dependencies")
load("@rules_foreign_cc//for_workspace:cmake_build.bzl", _cmake_tool = "cmake_tool")
load("@rules_foreign_cc//for_workspace:make_build.bzl", _make_tool = "make_tool")
load("@rules_foreign_cc//for_workspace:ninja_build.bzl", _ninja_tool = "ninja_tool")
load("@rules_foreign_cc//foreign_cc/built_tools:cmake_build.bzl", _cmake_tool = "cmake_tool")
load("@rules_foreign_cc//foreign_cc/built_tools:make_build.bzl", _make_tool = "make_tool")
load("@rules_foreign_cc//foreign_cc/built_tools:ninja_build.bzl", _ninja_tool = "ninja_tool")
load(
"@rules_foreign_cc//toolchains/native_tools:native_tools_toolchain.bzl",
_ToolInfo = "ToolInfo",
_native_tool_toolchain = "native_tool_toolchain",
)
load("@rules_foreign_cc//tools/build_defs:boost_build.bzl", _boost_build = "boost_build")
load("@rules_foreign_cc//tools/build_defs:cmake.bzl", _cmake = "cmake")
load("@rules_foreign_cc//tools/build_defs:configure.bzl", _configure_make = "configure_make")
@ -17,11 +22,6 @@ load(
)
load("@rules_foreign_cc//tools/build_defs:make.bzl", _make = "make")
load("@rules_foreign_cc//tools/build_defs:ninja.bzl", _ninja = "ninja")
load(
"@rules_foreign_cc//tools/build_defs/native_tools:native_tools_toolchain.bzl",
_ToolInfo = "ToolInfo",
_native_tool_toolchain = "native_tool_toolchain",
)
# Rules Foreign CC symbols
boost_build = _boost_build

View File

@ -5,7 +5,6 @@ exports_files(
"make_build.bzl",
"cmake_build.bzl",
"ninja_build.bzl",
"install_ws_dependency.bzl",
],
)

View File

@ -1,48 +1,5 @@
""" Rule for building CMake from sources. """
""" This module is deprecated and has been moved to `//toolchains/built_tools/...` """
load("@rules_foreign_cc//tools/build_defs:detect_root.bzl", "detect_root")
load("@rules_foreign_cc//tools/build_defs:shell_script_helper.bzl", "convert_shell_script")
load("//foreign_cc/built_tools:cmake_build.bzl", _cmake_tool = "cmake_tool")
def _cmake_tool(ctx):
root = detect_root(ctx.attr.cmake_srcs)
cmake = ctx.actions.declare_directory("cmake")
script = [
"export BUILD_DIR=##pwd##",
"export BUILD_TMPDIR=$${BUILD_DIR}$$.build_tmpdir",
"##copy_dir_contents_to_dir## ./{} $BUILD_TMPDIR".format(root),
"##mkdirs## " + cmake.path,
"cd $$BUILD_TMPDIR$$",
"./bootstrap --prefix=install",
"make install",
"##copy_dir_contents_to_dir## ./install $BUILD_DIR/" + cmake.path,
"cd $$BUILD_DIR$$",
]
script_text = convert_shell_script(ctx, script)
ctx.actions.run_shell(
mnemonic = "BootstrapCMake",
inputs = ctx.attr.cmake_srcs.files,
outputs = [cmake],
tools = [],
use_default_shell_env = True,
command = script_text,
execution_requirements = {"block-network": ""},
)
return [DefaultInfo(files = depset([cmake]))]
cmake_tool = rule(
doc = "Rule for building CMake. Invokes bootstrap script and make install.",
attrs = {
"cmake_srcs": attr.label(mandatory = True),
},
host_fragments = ["cpp"],
output_to_genfiles = True,
implementation = _cmake_tool,
toolchains = [
"@rules_foreign_cc//tools/build_defs/shell_toolchain/toolchains:shell_commands",
"@bazel_tools//tools/cpp:toolchain_type",
],
)
cmake_tool = _cmake_tool

View File

@ -1,62 +1,5 @@
""" Rule for building GNU Make from sources. """
""" This module is deprecated and has been moved to `//toolchains/built_tools/...` """
load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain")
load(
"@rules_foreign_cc//tools/build_defs:run_shell_file_utils.bzl",
"fictive_file_in_genroot",
)
load("@rules_foreign_cc//tools/build_defs:shell_script_helper.bzl", "convert_shell_script")
load("//tools/build_defs:detect_root.bzl", "detect_root")
load("//foreign_cc/built_tools:make_build.bzl", _make_tool = "make_tool")
def _make_tool(ctx):
root = detect_root(ctx.attr.make_srcs)
cc_toolchain = find_cpp_toolchain(ctx)
# we need this fictive file in the root to get the path of the root in the script
empty = fictive_file_in_genroot(ctx.actions, ctx.label.name)
make = ctx.actions.declare_directory("make")
script = [
"export EXT_BUILD_ROOT=##pwd##",
"export INSTALLDIR=$$EXT_BUILD_ROOT$$/" + empty.file.dirname + "/" + ctx.attr.name,
"export BUILD_TMPDIR=$$INSTALLDIR$$.build_tmpdir",
"##mkdirs## $$BUILD_TMPDIR$$",
"##copy_dir_contents_to_dir## ./{} $BUILD_TMPDIR".format(root),
"cd $$BUILD_TMPDIR$$",
"./configure --disable-dependency-tracking --prefix=$$EXT_BUILD_ROOT$$/{}".format(make.path),
"./build.sh",
"./make install",
empty.script,
]
script_text = convert_shell_script(ctx, script)
ctx.actions.run_shell(
mnemonic = "BootstrapMake",
inputs = ctx.attr.make_srcs.files,
outputs = [make, empty.file],
tools = cc_toolchain.all_files,
use_default_shell_env = True,
command = script_text,
execution_requirements = {"block-network": ""},
)
return [DefaultInfo(files = depset([make]))]
make_tool = rule(
doc = "Rule for building Make. Invokes configure script and make install.",
attrs = {
"make_srcs": attr.label(
doc = "target with the Make sources",
mandatory = True,
),
"_cc_toolchain": attr.label(default = Label("@bazel_tools//tools/cpp:current_cc_toolchain")),
},
host_fragments = ["cpp"],
output_to_genfiles = True,
implementation = _make_tool,
toolchains = [
"@rules_foreign_cc//tools/build_defs/shell_toolchain/toolchains:shell_commands",
"@bazel_tools//tools/cpp:toolchain_type",
],
)
make_tool = _make_tool

View File

@ -1,42 +1,5 @@
""" Rule for building Ninja from sources. """
""" This module is deprecated and has been moved to `//toolchains/built_tools/...` """
load("@rules_foreign_cc//tools/build_defs:shell_script_helper.bzl", "convert_shell_script")
load("//tools/build_defs:detect_root.bzl", "detect_root")
load("//foreign_cc/built_tools:ninja_build.bzl", _ninja_tool = "ninja_tool")
def _ninja_tool(ctx):
root = detect_root(ctx.attr.ninja_srcs)
ninja = ctx.actions.declare_directory("ninja")
script = [
"##mkdirs## " + ninja.path,
"##copy_dir_contents_to_dir## ./{} {}".format(root, ninja.path),
"cd " + ninja.path,
"./configure.py --bootstrap",
]
script_text = convert_shell_script(ctx, script)
ctx.actions.run_shell(
mnemonic = "BootstrapNinja",
inputs = ctx.attr.ninja_srcs.files,
outputs = [ninja],
tools = [],
use_default_shell_env = True,
command = script_text,
execution_requirements = {"block-network": ""},
)
return [DefaultInfo(files = depset([ninja]))]
ninja_tool = rule(
doc = "Rule for building Ninja. Invokes configure script and make install.",
attrs = {
"ninja_srcs": attr.label(mandatory = True),
},
host_fragments = ["cpp"],
output_to_genfiles = True,
implementation = _ninja_tool,
toolchains = [
"@rules_foreign_cc//tools/build_defs/shell_toolchain/toolchains:shell_commands",
"@bazel_tools//tools/cpp:toolchain_type",
],
)
ninja_tool = _ninja_tool

View File

@ -6,6 +6,9 @@ load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
def repositories():
"""Declare repositories used by `rules_foreign_cc`"""
# buildifier: disable=print
print("This macro is deprecated and will soon be removed")
maybe(
http_archive,
name = "bazel_skylib",

10
foreign_cc/BUILD.bazel Normal file
View File

@ -0,0 +1,10 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
bzl_library(
name = "bzl_srcs",
srcs = glob(["**/*.bzl"]),
visibility = ["//:__subpackages__"],
deps = [
"//foreign_cc/built_tools:bzl_srcs",
],
)

View File

@ -0,0 +1,7 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
bzl_library(
name = "bzl_srcs",
srcs = glob(["**/*.bzl"]),
visibility = ["//:__subpackages__"],
)

View File

@ -0,0 +1,48 @@
""" Rule for building CMake from sources. """
load("@rules_foreign_cc//tools/build_defs:detect_root.bzl", "detect_root")
load("@rules_foreign_cc//tools/build_defs:shell_script_helper.bzl", "convert_shell_script")
def _cmake_tool(ctx):
root = detect_root(ctx.attr.cmake_srcs)
cmake = ctx.actions.declare_directory("cmake")
script = [
"export BUILD_DIR=##pwd##",
"export BUILD_TMPDIR=$${BUILD_DIR}$$.build_tmpdir",
"##copy_dir_contents_to_dir## ./{} $BUILD_TMPDIR".format(root),
"##mkdirs## " + cmake.path,
"cd $$BUILD_TMPDIR$$",
"./bootstrap --prefix=install",
"make install",
"##copy_dir_contents_to_dir## ./install $BUILD_DIR/" + cmake.path,
"cd $$BUILD_DIR$$",
]
script_text = convert_shell_script(ctx, script)
ctx.actions.run_shell(
mnemonic = "BootstrapCMake",
inputs = ctx.attr.cmake_srcs.files,
outputs = [cmake],
tools = [],
use_default_shell_env = True,
command = script_text,
execution_requirements = {"block-network": ""},
)
return [DefaultInfo(files = depset([cmake]))]
cmake_tool = rule(
doc = "Rule for building CMake. Invokes bootstrap script and make install.",
attrs = {
"cmake_srcs": attr.label(mandatory = True),
},
host_fragments = ["cpp"],
output_to_genfiles = True,
implementation = _cmake_tool,
toolchains = [
"@rules_foreign_cc//tools/build_defs/shell_toolchain/toolchains:shell_commands",
"@bazel_tools//tools/cpp:toolchain_type",
],
)

View File

@ -0,0 +1,62 @@
""" Rule for building GNU Make from sources. """
load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain")
load(
"@rules_foreign_cc//tools/build_defs:run_shell_file_utils.bzl",
"fictive_file_in_genroot",
)
load("@rules_foreign_cc//tools/build_defs:shell_script_helper.bzl", "convert_shell_script")
load("//tools/build_defs:detect_root.bzl", "detect_root")
def _make_tool(ctx):
root = detect_root(ctx.attr.make_srcs)
cc_toolchain = find_cpp_toolchain(ctx)
# we need this fictive file in the root to get the path of the root in the script
empty = fictive_file_in_genroot(ctx.actions, ctx.label.name)
make = ctx.actions.declare_directory("make")
script = [
"export EXT_BUILD_ROOT=##pwd##",
"export INSTALLDIR=$$EXT_BUILD_ROOT$$/" + empty.file.dirname + "/" + ctx.attr.name,
"export BUILD_TMPDIR=$$INSTALLDIR$$.build_tmpdir",
"##mkdirs## $$BUILD_TMPDIR$$",
"##copy_dir_contents_to_dir## ./{} $BUILD_TMPDIR".format(root),
"cd $$BUILD_TMPDIR$$",
"./configure --disable-dependency-tracking --prefix=$$EXT_BUILD_ROOT$$/{}".format(make.path),
"./build.sh",
"./make install",
empty.script,
]
script_text = convert_shell_script(ctx, script)
ctx.actions.run_shell(
mnemonic = "BootstrapMake",
inputs = ctx.attr.make_srcs.files,
outputs = [make, empty.file],
tools = cc_toolchain.all_files,
use_default_shell_env = True,
command = script_text,
execution_requirements = {"block-network": ""},
)
return [DefaultInfo(files = depset([make]))]
make_tool = rule(
doc = "Rule for building Make. Invokes configure script and make install.",
attrs = {
"make_srcs": attr.label(
doc = "target with the Make sources",
mandatory = True,
),
"_cc_toolchain": attr.label(default = Label("@bazel_tools//tools/cpp:current_cc_toolchain")),
},
host_fragments = ["cpp"],
output_to_genfiles = True,
implementation = _make_tool,
toolchains = [
"@rules_foreign_cc//tools/build_defs/shell_toolchain/toolchains:shell_commands",
"@bazel_tools//tools/cpp:toolchain_type",
],
)

View File

@ -0,0 +1,42 @@
""" Rule for building Ninja from sources. """
load("@rules_foreign_cc//tools/build_defs:shell_script_helper.bzl", "convert_shell_script")
load("//tools/build_defs:detect_root.bzl", "detect_root")
def _ninja_tool(ctx):
root = detect_root(ctx.attr.ninja_srcs)
ninja = ctx.actions.declare_directory("ninja")
script = [
"##mkdirs## " + ninja.path,
"##copy_dir_contents_to_dir## ./{} {}".format(root, ninja.path),
"cd " + ninja.path,
"./configure.py --bootstrap",
]
script_text = convert_shell_script(ctx, script)
ctx.actions.run_shell(
mnemonic = "BootstrapNinja",
inputs = ctx.attr.ninja_srcs.files,
outputs = [ninja],
tools = [],
use_default_shell_env = True,
command = script_text,
execution_requirements = {"block-network": ""},
)
return [DefaultInfo(files = depset([ninja]))]
ninja_tool = rule(
doc = "Rule for building Ninja. Invokes configure script and make install.",
attrs = {
"ninja_srcs": attr.label(mandatory = True),
},
host_fragments = ["cpp"],
output_to_genfiles = True,
implementation = _ninja_tool,
toolchains = [
"@rules_foreign_cc//tools/build_defs/shell_toolchain/toolchains:shell_commands",
"@bazel_tools//tools/cpp:toolchain_type",
],
)

View File

@ -1,13 +1,68 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("//for_workspace:cmake_build.bzl", "cmake_tool")
load("//for_workspace:make_build.bzl", "make_tool")
load("//for_workspace:ninja_build.bzl", "ninja_tool")
load("//tools/build_defs/native_tools:native_tools_toolchain.bzl", "native_tool_toolchain")
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/native_tools:native_tools_toolchain.bzl", "native_tool_toolchain")
package(default_visibility = ["//visibility:public"])
toolchain_type(
name = "cmake_toolchain",
)
toolchain_type(
name = "ninja_toolchain",
)
toolchain_type(
name = "make_toolchain",
)
toolchain(
name = "built_cmake_toolchain",
toolchain = ":built_cmake",
toolchain_type = ":cmake_toolchain",
)
toolchain(
name = "built_ninja_toolchain",
toolchain = ":built_ninja",
toolchain_type = ":ninja_toolchain",
)
toolchain(
name = "built_make_toolchain",
toolchain = ":built_make",
toolchain_type = ":make_toolchain",
)
# Preinstalled cmake will always be the default, if toolchain with more exact constraints
# is not defined before; registered from workspace_definitions.bzl#rules_foreign_cc_dependencies
toolchain(
name = "preinstalled_cmake_toolchain",
toolchain = ":preinstalled_cmake",
toolchain_type = ":cmake_toolchain",
)
# Preinstalled ninja will always be the default, if toolchain with more exact constraints
# is not defined before; registered from workspace_definitions.bzl#rules_foreign_cc_dependencies
toolchain(
name = "preinstalled_ninja_toolchain",
toolchain = ":preinstalled_ninja",
toolchain_type = ":ninja_toolchain",
)
# Preinstalled make will always be the default, if toolchain with more exact constraints
# is not defined before; registered from workspace_definitions.bzl#rules_foreign_cc_dependencies
toolchain(
name = "preinstalled_make_toolchain",
toolchain = ":preinstalled_make",
toolchain_type = ":make_toolchain",
)
native_tool_toolchain(
name = "preinstalled_make",
path = "make",
visibility = ["//visibility:public"],
)
make_tool(
@ -20,13 +75,11 @@ native_tool_toolchain(
name = "built_make",
path = "$(execpath :make_tool)/bin/make",
target = ":make_tool",
visibility = ["//visibility:public"],
)
native_tool_toolchain(
name = "preinstalled_cmake",
path = "cmake",
visibility = ["//visibility:public"],
)
cmake_tool(
@ -39,13 +92,11 @@ native_tool_toolchain(
name = "built_cmake",
path = "$(execpath :cmake_tool)/bin/cmake",
target = ":cmake_tool",
visibility = ["//visibility:public"],
)
native_tool_toolchain(
name = "preinstalled_ninja",
path = "ninja",
visibility = ["//visibility:public"],
)
ninja_tool(
@ -58,11 +109,13 @@ native_tool_toolchain(
name = "built_ninja",
path = "$(execpath :ninja_tool)/ninja",
target = ":ninja_tool",
visibility = ["//visibility:public"],
)
bzl_library(
name = "bzl_srcs",
srcs = glob(["**/*.bzl"]),
visibility = ["//visibility:public"],
visibility = ["//:__subpackages__"],
deps = [
"//toolchains/native_tools:bzl_srcs",
],
)

View File

@ -23,7 +23,7 @@ def built_toolchains(cmake_version, make_version, ninja_version):
def _cmake_toolchain(version):
native.register_toolchains(
"@rules_foreign_cc//tools/build_defs:built_cmake_toolchain",
"@rules_foreign_cc//toolchains:built_cmake_toolchain",
)
if version == "3.19.6":
maybe(
@ -42,7 +42,7 @@ def _cmake_toolchain(version):
def _make_toolchain(version):
native.register_toolchains(
"@rules_foreign_cc//tools/build_defs:built_make_toolchain",
"@rules_foreign_cc//toolchains:built_make_toolchain",
)
if version == "4.3":
maybe(
@ -61,7 +61,7 @@ def _make_toolchain(version):
def _ninja_toolchain(version):
native.register_toolchains(
"@rules_foreign_cc//tools/build_defs:built_ninja_toolchain",
"@rules_foreign_cc//toolchains:built_ninja_toolchain",
)
if version == "1.10.2":
maybe(

View File

@ -0,0 +1,7 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
bzl_library(
name = "bzl_srcs",
srcs = glob(["**/*.bzl"]),
visibility = ["//visibility:public"],
)

View File

@ -0,0 +1,59 @@
# buildifier: disable=module-docstring
ToolInfo = provider(
doc = "Information about the native tool",
fields = {
"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 " +
"to the bazel-genfiles, i.e. it should start with the name of the top directory of the built tree " +
"artifact. (Please see the example `//examples:built_cmake_toolchain`)"
),
"target": (
"If the tool is preinstalled, must be None. " +
"If the tool is built as part of the build, the corresponding build target, which should produce " +
"the tree artifact with the binary to call."
),
},
)
def _native_tool_toolchain_impl(ctx):
if not ctx.attr.path and not ctx.attr.target:
fail("Either path or target (and path) should be defined for the tool.")
path = None
if ctx.attr.target:
path = ctx.expand_location(ctx.attr.path, targets = [ctx.attr.target])
else:
path = ctx.expand_location(ctx.attr.path)
return platform_common.ToolchainInfo(data = ToolInfo(
path = path,
target = ctx.attr.target,
))
native_tool_toolchain = rule(
doc = (
"Rule for defining the toolchain data of the native tools (cmake, ninja), " +
"to be used by rules_foreign_cc with toolchain types " +
"`@rules_foreign_cc//toolchains:cmake_toolchain` and " +
"`@rules_foreign_cc//toolchains:ninja_toolchain`."
),
implementation = _native_tool_toolchain_impl,
attrs = {
"path": attr.string(
mandatory = False,
doc = (
"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 to the bazel-genfiles, i.e. it should start with the name of the top directory " +
"of the built tree artifact. (Please see the example `//examples:built_cmake_toolchain`)"
),
),
"target": attr.label(
mandatory = False,
doc = (
"If the tool is preinstalled, must be None. " +
"If the tool is built as part of the build, the corresponding build target, " +
"which should produce the tree artifact with the binary to call."
),
),
},
)

View File

@ -0,0 +1,54 @@
"""A module defining convienence methoods for accessing build tools from
rules_foreign_cc toolchains
"""
load(":native_tools_toolchain.bzl", "ToolInfo")
def access_tool(toolchain_type_, ctx, tool_name):
"""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,
)
def get_cmake_data(ctx):
return _access_and_expect_label_copied("@rules_foreign_cc//toolchains:cmake_toolchain", ctx, "cmake")
def get_ninja_data(ctx):
return _access_and_expect_label_copied("@rules_foreign_cc//toolchains:ninja_toolchain", ctx, "ninja")
def get_make_data(ctx):
return _access_and_expect_label_copied("@rules_foreign_cc//toolchains:make_toolchain", ctx, "make")
def _access_and_expect_label_copied(toolchain_type_, ctx, tool_name):
tool_data = access_tool(toolchain_type_, ctx, tool_name)
if tool_data.target:
# This could be made more efficient by changing the
# toolchain to provide the executable as a target
cmd_file = tool_data
for f in tool_data.target.files.to_list():
if f.path.endswith("/" + tool_data.path):
cmd_file = f
break
return struct(
deps = [tool_data.target],
# as the tool will be copied into tools directory
path = "$EXT_BUILD_ROOT/{}".format(cmd_file.path),
)
else:
return struct(
deps = [],
path = tool_data.path,
)

View File

@ -9,7 +9,7 @@ load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
load("@rules_foreign_cc//toolchains:prebuilt_toolchains_repository.bzl", "prebuilt_toolchains_repository")
_CMAKE_BUILD_FILE = """\
load("@rules_foreign_cc//tools/build_defs/native_tools:native_tools_toolchain.bzl", "native_tool_toolchain")
load("@rules_foreign_cc//toolchains/native_tools:native_tools_toolchain.bzl", "native_tool_toolchain")
package(default_visibility = ["//visibility:public"])
@ -36,7 +36,7 @@ native_tool_toolchain(
"""
_NINJA_BUILD_FILE = """\
load("@rules_foreign_cc//tools/build_defs/native_tools:native_tools_toolchain.bzl", "native_tool_toolchain")
load("@rules_foreign_cc//toolchains/native_tools:native_tools_toolchain.bzl", "native_tool_toolchain")
package(default_visibility = ["//visibility:public"])
@ -62,6 +62,7 @@ def prebuilt_toolchains(cmake_version, ninja_version):
"""
_cmake_toolchains(cmake_version)
_ninja_toolchains(ninja_version)
_make_toolchains()
def _cmake_toolchains(version):
if "3.19.6" == version:
@ -1056,3 +1057,7 @@ def _ninja_toolchains(version):
return
fail("Unsupported version: " + str(version))
def _make_toolchains():
# There are currently no prebuilt make binaries
pass

View File

@ -114,7 +114,7 @@ load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
load("@rules_foreign_cc//toolchains:prebuilt_toolchains_repository.bzl", "prebuilt_toolchains_repository")
_CMAKE_BUILD_FILE = \"\"\"\\
load("@rules_foreign_cc//tools/build_defs/native_tools:native_tools_toolchain.bzl", "native_tool_toolchain")
load("@rules_foreign_cc//toolchains/native_tools:native_tools_toolchain.bzl", "native_tool_toolchain")
package(default_visibility = ["//visibility:public"])
@ -141,7 +141,7 @@ native_tool_toolchain(
\"\"\"
_NINJA_BUILD_FILE = \"\"\"\\
load("@rules_foreign_cc//tools/build_defs/native_tools:native_tools_toolchain.bzl", "native_tool_toolchain")
load("@rules_foreign_cc//toolchains/native_tools:native_tools_toolchain.bzl", "native_tool_toolchain")
package(default_visibility = ["//visibility:public"])
@ -366,7 +366,7 @@ def get_make_definitions() -> str:
"""
return indent(
"native.register_toolchains(\"@rules_foreign_cc//tools/build_defs:preinstalled_make_toolchain\")",
"# There are currently no prebuilt make binaries\npass",
" " * 4)

View File

@ -1,7 +1,7 @@
"""A module defining a repository rule for housing toolchain definitions"""
_BUILD_FILE = """\
load("@rules_foreign_cc//tools/build_defs/native_tools:native_tools_toolchain.bzl", "native_tool_toolchain")
load("@rules_foreign_cc//toolchains/native_tools:native_tools_toolchain.bzl", "native_tool_toolchain")
{toolchains}
"""
@ -11,7 +11,7 @@ toolchain(
name = "{repo}_toolchain",
exec_compatible_with = {exec_compatible_with},
toolchain = "@{repo}//:{tool}_tool",
toolchain_type = "@rules_foreign_cc//tools/build_defs:{tool}_toolchain",
toolchain_type = "@rules_foreign_cc//toolchains:{tool}_toolchain",
)
"""

View File

@ -13,7 +13,7 @@ prebuilt_toolchains = _prebuilt_toolchains
def preinstalled_toolchains():
"""Register toolchains for various build tools expected to be installed on the exec host"""
native.register_toolchains(
"@rules_foreign_cc//tools/build_defs:preinstalled_cmake_toolchain",
"@rules_foreign_cc//tools/build_defs:preinstalled_make_toolchain",
"@rules_foreign_cc//tools/build_defs:preinstalled_ninja_toolchain",
"@rules_foreign_cc//toolchains:preinstalled_cmake_toolchain",
"@rules_foreign_cc//toolchains:preinstalled_make_toolchain",
"@rules_foreign_cc//toolchains:preinstalled_ninja_toolchain",
)

View File

@ -1,60 +1,66 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
toolchain_type(
alias(
name = "cmake_toolchain",
actual = "//toolchains:cmake_toolchain",
deprecation = "This target has been moved to `@rules_foreign_cc//toolchains/...`",
visibility = ["//visibility:public"],
)
toolchain_type(
alias(
name = "ninja_toolchain",
actual = "//toolchains:ninja_toolchain",
deprecation = "This target has been moved to `@rules_foreign_cc//toolchains/...`",
visibility = ["//visibility:public"],
)
toolchain_type(
alias(
name = "make_toolchain",
actual = "//toolchains:make_toolchain",
deprecation = "This target has been moved to `@rules_foreign_cc//toolchains/...`",
visibility = ["//visibility:public"],
)
toolchain(
alias(
name = "built_cmake_toolchain",
toolchain = "@rules_foreign_cc//toolchains:built_cmake",
toolchain_type = "@rules_foreign_cc//tools/build_defs:cmake_toolchain",
actual = "//toolchains:built_cmake_toolchain",
deprecation = "This target has been moved to `@rules_foreign_cc//toolchains/...`",
)
toolchain(
alias(
name = "built_ninja_toolchain",
toolchain = "@rules_foreign_cc//toolchains:built_ninja",
toolchain_type = "@rules_foreign_cc//tools/build_defs:ninja_toolchain",
actual = "//toolchains:built_ninja_toolchain",
deprecation = "This target has been moved to `@rules_foreign_cc//toolchains/...`",
)
toolchain(
alias(
name = "built_make_toolchain",
toolchain = "@rules_foreign_cc//toolchains:built_make",
toolchain_type = "@rules_foreign_cc//tools/build_defs:make_toolchain",
actual = "//toolchains:built_make_toolchain",
deprecation = "This target has been moved to `@rules_foreign_cc//toolchains/...`",
)
# Preinstalled cmake will always be the default, if toolchain with more exact constraints
# is not defined before; registered from workspace_definitions.bzl#rules_foreign_cc_dependencies
toolchain(
alias(
name = "preinstalled_cmake_toolchain",
toolchain = "@rules_foreign_cc//toolchains:preinstalled_cmake",
toolchain_type = "@rules_foreign_cc//tools/build_defs:cmake_toolchain",
actual = "//toolchains:preinstalled_cmake_toolchain",
deprecation = "This target has been moved to `@rules_foreign_cc//toolchains/...`",
)
# Preinstalled ninja will always be the default, if toolchain with more exact constraints
# is not defined before; registered from workspace_definitions.bzl#rules_foreign_cc_dependencies
toolchain(
alias(
name = "preinstalled_ninja_toolchain",
toolchain = "@rules_foreign_cc//toolchains:preinstalled_ninja",
toolchain_type = "@rules_foreign_cc//tools/build_defs:ninja_toolchain",
actual = "//toolchains:preinstalled_ninja_toolchain",
deprecation = "This target has been moved to `@rules_foreign_cc//toolchains/...`",
)
# Preinstalled make will always be the default, if toolchain with more exact constraints
# is not defined before; registered from workspace_definitions.bzl#rules_foreign_cc_dependencies
toolchain(
alias(
name = "preinstalled_make_toolchain",
toolchain = "@rules_foreign_cc//toolchains:preinstalled_make",
toolchain_type = "@rules_foreign_cc//tools/build_defs:make_toolchain",
actual = "//toolchains:preinstalled_make_toolchain",
deprecation = "This target has been moved to `@rules_foreign_cc//toolchains/...`",
)
bzl_library(

View File

@ -1,6 +1,12 @@
""" Defines the rule for building external library with CMake
"""
load(
"//toolchains/native_tools:tool_access.bzl",
"get_cmake_data",
"get_make_data",
"get_ninja_data",
)
load(
"//tools/build_defs:cc_toolchain_util.bzl",
"get_flags_info",
@ -17,12 +23,6 @@ load(
"cc_external_rule_impl",
"create_attrs",
)
load(
"//tools/build_defs/native_tools:tool_access.bzl",
"get_cmake_data",
"get_make_data",
"get_ninja_data",
)
load(":cmake_script.bzl", "create_cmake_script")
def _cmake_impl(ctx):
@ -155,9 +155,9 @@ cmake = rule(
output_to_genfiles = True,
implementation = _cmake_impl,
toolchains = [
"@rules_foreign_cc//tools/build_defs:cmake_toolchain",
"@rules_foreign_cc//tools/build_defs:ninja_toolchain",
"@rules_foreign_cc//tools/build_defs:make_toolchain",
"@rules_foreign_cc//toolchains:cmake_toolchain",
"@rules_foreign_cc//toolchains:ninja_toolchain",
"@rules_foreign_cc//toolchains:make_toolchain",
"@rules_foreign_cc//tools/build_defs/shell_toolchain/toolchains:shell_commands",
"@bazel_tools//tools/cpp:toolchain_type",
],

View File

@ -1,5 +1,6 @@
# buildifier: disable=module-docstring
load("@rules_foreign_cc//tools/build_defs:shell_script_helper.bzl", "os_name")
load("//toolchains/native_tools:tool_access.bzl", "get_make_data")
load(
"//tools/build_defs:cc_toolchain_util.bzl",
"get_flags_info",
@ -16,7 +17,6 @@ load(
"cc_external_rule_impl",
"create_attrs",
)
load("//tools/build_defs/native_tools:tool_access.bzl", "get_make_data")
load(":configure_script.bzl", "create_configure_script")
def _configure_make(ctx):
@ -179,7 +179,7 @@ configure_make = rule(
output_to_genfiles = True,
implementation = _configure_make,
toolchains = [
"@rules_foreign_cc//tools/build_defs:make_toolchain",
"@rules_foreign_cc//toolchains:make_toolchain",
"@rules_foreign_cc//tools/build_defs/shell_toolchain/toolchains:shell_commands",
"@bazel_tools//tools/cpp:toolchain_type",
],

View File

@ -1,4 +1,5 @@
# buildifier: disable=module-docstring
load("//toolchains/native_tools:tool_access.bzl", "get_make_data")
load(
"//tools/build_defs:cc_toolchain_util.bzl",
"get_flags_info",
@ -14,7 +15,6 @@ load(
"cc_external_rule_impl",
"create_attrs",
)
load("//tools/build_defs/native_tools:tool_access.bzl", "get_make_data")
load(":configure_script.bzl", "create_make_script")
def _make(ctx):
@ -122,7 +122,7 @@ make = rule(
output_to_genfiles = True,
implementation = _make,
toolchains = [
"@rules_foreign_cc//tools/build_defs:make_toolchain",
"@rules_foreign_cc//toolchains:make_toolchain",
"@rules_foreign_cc//tools/build_defs/shell_toolchain/toolchains:shell_commands",
"@bazel_tools//tools/cpp:toolchain_type",
],

View File

@ -1,68 +1,9 @@
# buildifier: disable=module-docstring
ToolInfo = provider(
doc = "Information about the native tool",
fields = {
"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 " +
"to the bazel-genfiles, i.e. it should start with the name of the top directory of the built tree " +
"artifact. (Please see the example `//examples:built_cmake_toolchain`)"
),
"target": (
"If the tool is preinstalled, must be None. " +
"If the tool is built as part of the build, the corresponding build target, which should produce " +
"the tree artifact with the binary to call."
),
},
)
"""This module has been moved to `//toolchains/native_tools:native_tools_toolchains.bzl`.
This file will be removed at some point in the future
"""
def _native_tool_toolchain_impl(ctx):
if not ctx.attr.path and not ctx.attr.target:
fail("Either path or target (and path) should be defined for the tool.")
path = None
if ctx.attr.target:
path = ctx.expand_location(ctx.attr.path, targets = [ctx.attr.target])
else:
path = ctx.expand_location(ctx.attr.path)
return platform_common.ToolchainInfo(data = ToolInfo(
path = path,
target = ctx.attr.target,
))
load("//toolchains/native_tools:native_tools_toolchain.bzl", _native_tool_toolchain = "native_tool_toolchain")
load("//toolchains/native_tools:tool_access.bzl", _access_tool = "access_tool")
native_tool_toolchain = rule(
doc = (
"Rule for defining the toolchain data of the native tools (cmake, ninja), " +
"to be used by rules_foreign_cc with toolchain types " +
"`@rules_foreign_cc//tools/build_defs:cmake_toolchain` and " +
"`@rules_foreign_cc//tools/build_defs:ninja_toolchain`."
),
implementation = _native_tool_toolchain_impl,
attrs = {
"path": attr.string(
mandatory = False,
doc = (
"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 to the bazel-genfiles, i.e. it should start with the name of the top directory " +
"of the built tree artifact. (Please see the example `//examples:built_cmake_toolchain`)"
),
),
"target": attr.label(
mandatory = False,
doc = (
"If the tool is preinstalled, must be None. " +
"If the tool is built as part of the build, the corresponding build target, " +
"which should produce the tree artifact with the binary to call."
),
),
},
)
def access_tool(toolchain_type_, ctx, tool_name):
tool_toolchain = ctx.toolchains[toolchain_type_]
if tool_toolchain:
return tool_toolchain.data
return ToolInfo(
path = tool_name,
target = None,
)
native_tool_toolchain = _native_tool_toolchain
access_tool = _access_tool

View File

@ -1,32 +1,14 @@
# buildifier: disable=module-docstring
load(":native_tools_toolchain.bzl", "access_tool")
"""This module has been moved to `//toolchains/native_tools:native_tools_toolchains.bzl`.
This file will be removed at some point in the future
"""
def get_cmake_data(ctx):
return _access_and_expect_label_copied("@rules_foreign_cc//tools/build_defs:cmake_toolchain", ctx, "cmake")
load(
"//toolchains/native_tools:tool_access.bzl",
_get_cmake_data = "get_cmake_data",
_get_make_data = "get_make_data",
_get_ninja_data = "get_ninja_data",
)
def get_ninja_data(ctx):
return _access_and_expect_label_copied("@rules_foreign_cc//tools/build_defs:ninja_toolchain", ctx, "ninja")
def get_make_data(ctx):
return _access_and_expect_label_copied("@rules_foreign_cc//tools/build_defs:make_toolchain", ctx, "make")
def _access_and_expect_label_copied(toolchain_type_, ctx, tool_name):
tool_data = access_tool(toolchain_type_, ctx, tool_name)
if tool_data.target:
# This could be made more efficient by changing the
# toolchain to provide the executable as a target
cmd_file = tool_data
for f in tool_data.target.files.to_list():
if f.path.endswith("/" + tool_data.path):
cmd_file = f
break
return struct(
deps = [tool_data.target],
# as the tool will be copied into tools directory
path = "$EXT_BUILD_ROOT/{}".format(cmd_file.path),
)
else:
return struct(
deps = [],
path = tool_data.path,
)
get_cmake_data = _get_cmake_data
get_make_data = _get_make_data
get_ninja_data = _get_ninja_data

View File

@ -1,5 +1,6 @@
"""A module defining the `ninja` rule. A rule for building projects using the Ninja build tool"""
load("//toolchains/native_tools:tool_access.bzl", "get_ninja_data")
load(
"//tools/build_defs:detect_root.bzl",
"detect_root",
@ -10,7 +11,6 @@ load(
"cc_external_rule_impl",
"create_attrs",
)
load("//tools/build_defs/native_tools:tool_access.bzl", "get_ninja_data")
def _ninja_impl(ctx):
"""The implementation of the `ninja` rule
@ -117,7 +117,7 @@ ninja = rule(
output_to_genfiles = True,
implementation = _ninja_impl,
toolchains = [
"@rules_foreign_cc//tools/build_defs:ninja_toolchain",
"@rules_foreign_cc//toolchains:ninja_toolchain",
"@rules_foreign_cc//tools/build_defs/shell_toolchain/toolchains:shell_commands",
"@bazel_tools//tools/cpp:toolchain_type",
],

View File

@ -1,6 +1,7 @@
"""A module for defining WORKSPACE dependencies required for rules_foreign_cc"""
load("//for_workspace:repositories.bzl", "repositories")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
load("//toolchains:toolchains.bzl", "built_toolchains", "prebuilt_toolchains", "preinstalled_toolchains")
load(
"//tools/build_defs/shell_toolchain/toolchains:ws_defs.bzl",
@ -24,8 +25,8 @@ def rules_foreign_cc_dependencies(
Args:
native_tools_toolchains: pass the toolchains for toolchain types
'@rules_foreign_cc//tools/build_defs:cmake_toolchain' and
'@rules_foreign_cc//tools/build_defs:ninja_toolchain' with the needed platform constraints.
'@rules_foreign_cc//toolchains:cmake_toolchain' and
'@rules_foreign_cc//toolchains:ninja_toolchain' with the needed platform constraints.
If you do not pass anything, registered default toolchains will be selected (see below).
register_default_tools: If True, the cmake and ninja toolchains, calling corresponding
@ -56,7 +57,6 @@ def rules_foreign_cc_dependencies(
This value is needed since register_toolchains() is called for these toolchains.
Please refer to example in @rules_foreign_cc//toolchain_examples.
"""
repositories()
shell_toolchain_workspace_initalization(
additional_shell_toolchain_mappings,
@ -77,3 +77,13 @@ def rules_foreign_cc_dependencies(
if register_preinstalled_tools:
preinstalled_toolchains()
maybe(
http_archive,
name = "bazel_skylib",
sha256 = "1c531376ac7e5a180e0237938a2536de0c54d93f5c278634818e0efc952dd56c",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.0.3/bazel-skylib-1.0.3.tar.gz",
"https://github.com/bazelbuild/bazel-skylib/releases/download/1.0.3/bazel-skylib-1.0.3.tar.gz",
],
)