Skip to content

Commit

Permalink
[IWYU]: support for remote execution
Browse files Browse the repository at this point in the history
  • Loading branch information
storypku committed Jul 10, 2022
1 parent b9fcc50 commit ed669d8
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 15 deletions.
66 changes: 52 additions & 14 deletions bazel/iwyu/iwyu.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@ def _is_cpp_target(srcs):
def _is_cuda_target(srcs):
return any([src.extension in _CUDA_EXTENSIONS for src in srcs])

def _run_iwyu(ctx, iwyu_executable, flags, target, infile):
def _run_iwyu(
ctx,
iwyu_sh_wrapper,
iwyu_binary,
iwyu_mappings,
iwyu_options,
flags,
target,
infile):
compilation_context = target[CcInfo].compilation_context

inputs = depset(direct = [infile], transitive = [compilation_context.headers])
outfile = ctx.actions.declare_file(
"{}.{}.iwyu.txt".format(target.label.name, infile.basename),
)
Expand All @@ -31,11 +37,12 @@ def _run_iwyu(ctx, iwyu_executable, flags, target, infile):
args = ctx.actions.args()
args.add(outfile)

iwyu_options = ctx.attr._iwyu_opts[BuildSettingInfo].value
args.add_all(iwyu_options, before_each = "-Xiwyu")

iwyu_mappings = ctx.attr._iwyu_mappings.files.to_list()
args.add_all(["--mapping_file={}".format(m.path) for m in iwyu_mappings], before_each = "-Xiwyu")
args.add_all(
["--mapping_file={}".format(m.path) for m in iwyu_mappings],
before_each = "-Xiwyu",
)

args.add_all(flags)

Expand All @@ -59,12 +66,17 @@ def _run_iwyu(ctx, iwyu_executable, flags, target, infile):
# add source to check
args.add(infile)

inputs = depset(
direct = [infile] + iwyu_mappings,
transitive = [compilation_context.headers],
)

# https://github.com/bazelbuild/bazel/issues/5511
ctx.actions.run(
inputs = inputs,
outputs = [outfile],
arguments = [args],
executable = iwyu_executable,
executable = iwyu_sh_wrapper,
mnemonic = "iwyu",
progress_message = "Run include-what-you-use on {}".format(infile.short_path),
execution_requirements = {
Expand Down Expand Up @@ -95,8 +107,7 @@ def _toolchain_flags(ctx, is_cpp_target):
compile_variables = cc_common.create_compile_variables(
feature_configuration = feature_configuration,
cc_toolchain = cc_toolchain,
user_compile_flags = ctx.fragments.cpp.cxxopts +
ctx.fragments.cpp.copts,
user_compile_flags = ctx.fragments.cpp.cxxopts + ctx.fragments.cpp.copts,
add_legacy_cxx_options = True,
)
flags = cc_common.get_memory_inefficient_command_line(
Expand Down Expand Up @@ -126,7 +137,11 @@ def _safe_flags(flags):
"-fstack-usage",
]

return [flag for flag in flags if flag not in unsupported_flags and not flag.startswith("--sysroot")]
return [
flag
for flag in flags
if flag not in unsupported_flags and not flag.startswith("--sysroot")
]

def _iwyu_aspect_impl(target, ctx):
# Interested in C, C++, and CUDA (not-ready) targets only
Expand All @@ -139,7 +154,10 @@ def _iwyu_aspect_impl(target, ctx):
if len(srcs) == 0 or _is_cuda_target(srcs):
return []

iwyu_executable = ctx.attr._iwyu_executable.files_to_run
iwyu_sh_wrapper = ctx.attr._iwyu_sh_wrapper.files_to_run
iwyu_binary = ctx.file._iwyu_binary
iwyu_mappings = ctx.attr._iwyu_mappings.files.to_list()
iwyu_options = ctx.attr._iwyu_opts[BuildSettingInfo].value

is_cpp_target = _is_cpp_target(srcs)
toolchain_flags = _toolchain_flags(ctx, is_cpp_target)
Expand All @@ -150,7 +168,19 @@ def _iwyu_aspect_impl(target, ctx):

all_flags = _safe_flags(toolchain_flags + rule_flags)

outputs = [_run_iwyu(ctx, iwyu_executable, all_flags, target, src) for src in srcs]
outputs = [
_run_iwyu(
ctx,
iwyu_sh_wrapper,
iwyu_binary,
iwyu_mappings,
iwyu_options,
all_flags,
target,
src,
)
for src in srcs
]
return [
OutputGroupInfo(report = depset(direct = outputs)),
]
Expand All @@ -160,10 +190,18 @@ iwyu_aspect = aspect(
implementation = _iwyu_aspect_impl,
fragments = ["cpp"],
attrs = {
"_cc_toolchain": attr.label(default = Label("@bazel_tools//tools/cpp:current_cc_toolchain")),
"_iwyu_executable": attr.label(default = Label("//bazel/iwyu:run_iwyu")),
"_cc_toolchain": attr.label(
default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"),
),
"_iwyu_binary": attr.label(
executable = True,
cfg = "exec",
allow_single_file = True,
default = Label("@iwyu_prebuilt_pkg//:bin/include-what-you-use"),
),
"_iwyu_mappings": attr.label(default = Label("//:iwyu_mappings")),
"_iwyu_opts": attr.label(default = Label("//:iwyu_opts")),
"_iwyu_sh_wrapper": attr.label(default = Label("//bazel/iwyu:run_iwyu")),
},
toolchains = ["@bazel_tools//tools/cpp:toolchain_type"],
)
8 changes: 7 additions & 1 deletion bazel/iwyu/run_iwyu.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ set -u

readonly RED='\033[0;31m'
readonly RESET='\033[0m'
readonly IWYU_BINARY="external/iwyu_prebuilt_pkg/bin/include-what-you-use"

IWYU_BINARY="external/iwyu_prebuilt_pkg/bin/include-what-you-use"

# FIXME(storypku): The path above doesn't work for Remote execution
if [[ ! -x "${IWYU_BINARY}" ]]; then
IWYU_BINARY="include-what-you-use"
fi

function error() {
(echo >&2 -e "${RED}[ERROR]${RESET} $*")
Expand Down

0 comments on commit ed669d8

Please sign in to comment.