Skip to content

Commit

Permalink
Add parallel make toolchain wrapper
Browse files Browse the repository at this point in the history
This can be used by adding
`--extra_toolchains=@rules_foreign_cc//toolchains:built_make_parallel_toolchain` to your bazel command line

Addresses some of bazelbuild#329
  • Loading branch information
jsharpe committed Aug 3, 2022
1 parent 30fdd11 commit 4d4139d
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
4 changes: 2 additions & 2 deletions examples/WORKSPACE.bazel
Expand Up @@ -18,9 +18,9 @@ load("//deps:repositories.bzl", "repositories")

repositories()

load("//deps:deps_android.bzl", "deps_android")
#load("//deps:deps_android.bzl", "deps_android")

deps_android()
#deps_android()

load("//deps:deps_jvm_external.bzl", "deps_jvm_external")

Expand Down
6 changes: 6 additions & 0 deletions foreign_cc/private/framework.bzl
Expand Up @@ -483,6 +483,7 @@ def cc_external_rule_impl(ctx, attrs):
ctx.files.build_data +
legacy_tools +
cc_toolchain.all_files.to_list() +
inputs.tool_runfiles +
tool_runfiles +
[data[DefaultInfo].files_to_run for data in data_dependencies],
command = wrapped_outputs.wrapper_script_file.path,
Expand Down Expand Up @@ -784,6 +785,7 @@ InputFiles = provider(
"Files and directories with tools needed for configuration/building " +
"to be copied into the bin folder, which is added to the PATH"
),
tool_runfiles = "All files and directories that must be declared as runfiles for tools",
ext_build_dirs = (
"Directories with libraries, built by framework function. " +
"This directories should be copied into $EXT_BUILD_DEPS/lib-name as is, with all contents."
Expand Down Expand Up @@ -825,15 +827,18 @@ def _define_inputs(attrs):

tools_roots = []
tools_files = []
tool_runfiles = []
input_files = []
for tool in attrs.tools_deps:
tool_runfiles += tool[DefaultInfo].default_runfiles.files.to_list()
tool_root = detect_root(tool)
tools_roots.append(tool_root)
for file_list in tool.files.to_list():
tools_files += _list(file_list)

# TODO: Remove, `additional_tools` is deprecated.
for tool in attrs.additional_tools:
tool_runfiles += tool[DefaultInfo].default_runfiles.files.to_list()
for file_list in tool.files.to_list():
tools_files += _list(file_list)

Expand All @@ -853,6 +858,7 @@ def _define_inputs(attrs):
deps_compilation_info = cc_info_merged.compilation_context,
deps_linking_info = cc_info_merged.linking_context,
ext_build_dirs = ext_build_dirs,
tool_runfiles = tool_runfiles,
declared_inputs = filter_containing_dirs_from_inputs(attrs.lib_source.files.to_list()) +
bazel_libs +
tools_files +
Expand Down
19 changes: 19 additions & 0 deletions toolchains/BUILD.bazel
Expand Up @@ -81,6 +81,12 @@ toolchain(
toolchain_type = ":make_toolchain",
)

toolchain(
name = "built_make_parallel_toolchain",
toolchain = ":built_parallel_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(
Expand Down Expand Up @@ -146,6 +152,19 @@ native_tool_toolchain(
target = ":make_tool",
)

sh_binary(
name = "parallel_make",
srcs = ["parallel_make.sh"],
data = [":make_tool"],
deps = ["@bazel_tools//tools/bash/runfiles"],
)

native_tool_toolchain(
name = "built_parallel_make",
path = "parallel_make",
target = ":parallel_make",
)

native_tool_toolchain(
name = "preinstalled_cmake",
env = select({
Expand Down
19 changes: 19 additions & 0 deletions toolchains/parallel_make.sh
@@ -0,0 +1,19 @@
#!/bin/bash

pushd $EXT_BUILD_ROOT/external

# --- begin runfiles.bash initialization v2 ---
# Copy-pasted from the Bazel Bash runfiles library v2.
set -uo pipefail; f=bazel_tools/tools/bash/runfiles/runfiles.bash
source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
source "$0.runfiles/$f" 2>/dev/null || \
source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
{ echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
# --- end runfiles.bash initialization v2 ---

popd

NUMBER_OF_CPUS=$(nproc)
exec "$(rlocation rules_foreign_cc/toolchains/make/bin/make)" -j${NUMBER_OF_CPUS} -l${NUMBER_OF_CPUS} "$@"

0 comments on commit 4d4139d

Please sign in to comment.