Skip to content

Commit

Permalink
refactor: replace accesses to cc_toolchain.compiler_executable with c…
Browse files Browse the repository at this point in the history
…alls to cc_toolchain.get_tool_for_action() (#274)

According to the Bazel documentation, `compiler_executable` is going to be phased out. It is already an issue to use it for people who are trying to work with the new `rules_cc` implementation of the cc rules.
  • Loading branch information
hypdeb authored Sep 11, 2024
1 parent d028c35 commit 4dd02f0
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 8 deletions.
9 changes: 8 additions & 1 deletion cuda/private/actions/compile.bzl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
load("@bazel_tools//tools/build_defs/cc:action_names.bzl", CC_ACTION_NAMES = "ACTION_NAMES")
load("//cuda/private:action_names.bzl", "ACTION_NAMES")
load("//cuda/private:cuda_helper.bzl", "cuda_helper")
load("//cuda/private:rules/common.bzl", "ALLOW_CUDA_SRCS")
Expand Down Expand Up @@ -33,7 +34,13 @@ def compile(
An compiled object `File`.
"""
actions = ctx.actions
host_compiler = cc_toolchain.compiler_executable
cc_feature_configuration = cc_common.configure_features(
ctx = ctx,
cc_toolchain = cc_toolchain,
requested_features = ctx.features,
unsupported_features = ctx.disabled_features,
)
host_compiler = cc_common.get_tool_for_action(feature_configuration = cc_feature_configuration, action_name = CC_ACTION_NAMES.cpp_compile)
cuda_compiler = cuda_toolchain.compiler_executable

cuda_feature_config = cuda_helper.configure_features(ctx, cuda_toolchain, requested_features = [ACTION_NAMES.cuda_compile])
Expand Down
9 changes: 8 additions & 1 deletion cuda/private/actions/dlink.bzl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
""

load("@bazel_tools//tools/build_defs/cc:action_names.bzl", CC_ACTION_NAMES = "ACTION_NAMES")
load("//cuda/private:action_names.bzl", "ACTION_NAMES")
load("//cuda/private:actions/compile.bzl", "compile")
load("//cuda/private:cuda_helper.bzl", "cuda_helper")
Expand Down Expand Up @@ -55,7 +56,13 @@ def _compiler_device_link(
fail("device link is only meaningful on building relocatable device code")

actions = ctx.actions
host_compiler = cc_toolchain.compiler_executable
cc_feature_configuration = cc_common.configure_features(
ctx = ctx,
cc_toolchain = cc_toolchain,
requested_features = ctx.features,
unsupported_features = ctx.disabled_features,
)
host_compiler = cc_common.get_tool_for_action(feature_configuration = cc_feature_configuration, action_name = CC_ACTION_NAMES.cpp_compile)
cuda_compiler = cuda_toolchain.compiler_executable

artifact_category_name = cuda_helper.get_artifact_category_from_action(ACTION_NAMES.device_link, pic, rdc)
Expand Down
11 changes: 10 additions & 1 deletion cuda/private/toolchain_configs/clang.bzl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("@bazel_skylib//lib:paths.bzl", "paths")
load("@bazel_tools//tools/build_defs/cc:action_names.bzl", CC_ACTION_NAMES = "ACTION_NAMES")
load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain")
load("//cuda/private:action_names.bzl", "ACTION_NAMES")
load("//cuda/private:artifact_categories.bzl", "ARTIFACT_CATEGORIES")
Expand Down Expand Up @@ -52,6 +53,13 @@ def _impl(ctx):
]

cc_toolchain = find_cpp_toolchain(ctx)
cc_feature_configuration = cc_common.configure_features(
ctx = ctx,
cc_toolchain = cc_toolchain,
requested_features = ctx.features,
unsupported_features = ctx.disabled_features,
)
host_compiler = cc_common.get_tool_for_action(feature_configuration = cc_feature_configuration, action_name = CC_ACTION_NAMES.cpp_compile)

clang_compile_env_feature = feature(
name = "clang_compile_env",
Expand All @@ -64,7 +72,7 @@ def _impl(ctx):
],
env_entries = [
env_entry("INCLUDE", ";".join(cc_toolchain.built_in_include_directories)),
env_entry("PATH", paths.dirname(cc_toolchain.compiler_executable) + ";C:/Windows/system32"),
env_entry("PATH", paths.dirname(host_compiler) + ";C:/Windows/system32"),
],
),
],
Expand Down Expand Up @@ -509,4 +517,5 @@ cuda_toolchain_config = rule(
},
provides = [CudaToolchainConfigInfo],
toolchains = use_cpp_toolchain(),
fragments = ["cpp"],
)
13 changes: 11 additions & 2 deletions cuda/private/toolchain_configs/nvcc.bzl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("@bazel_skylib//lib:paths.bzl", "paths")
load("@bazel_tools//tools/build_defs/cc:action_names.bzl", CC_ACTION_NAMES = "ACTION_NAMES")
load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain")
load("//cuda/private:action_names.bzl", "ACTION_NAMES")
load("//cuda/private:artifact_categories.bzl", "ARTIFACT_CATEGORIES")
Expand Down Expand Up @@ -42,13 +43,20 @@ def _impl(ctx):
]

cc_toolchain = find_cpp_toolchain(ctx)
cc_feature_configuration = cc_common.configure_features(
ctx = ctx,
cc_toolchain = cc_toolchain,
requested_features = ctx.features,
unsupported_features = ctx.disabled_features,
)
host_compiler = cc_common.get_tool_for_action(feature_configuration = cc_feature_configuration, action_name = CC_ACTION_NAMES.cpp_compile)

nvcc_compile_env_feature = feature(
name = "nvcc_compile_env",
env_sets = [
env_set(
actions = [ACTION_NAMES.cuda_compile],
env_entries = [env_entry("PATH", paths.dirname(cc_toolchain.compiler_executable))],
env_entries = [env_entry("PATH", paths.dirname(host_compiler))],
),
],
)
Expand All @@ -58,7 +66,7 @@ def _impl(ctx):
env_sets = [
env_set(
actions = [ACTION_NAMES.device_link],
env_entries = [env_entry("PATH", paths.dirname(cc_toolchain.compiler_executable))],
env_entries = [env_entry("PATH", paths.dirname(host_compiler))],
),
],
)
Expand Down Expand Up @@ -519,4 +527,5 @@ cuda_toolchain_config = rule(
},
provides = [CudaToolchainConfigInfo],
toolchains = use_cpp_toolchain(),
fragments = ["cpp"],
)
13 changes: 10 additions & 3 deletions cuda/private/toolchain_configs/nvcc_msvc.bzl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("@bazel_skylib//lib:paths.bzl", "paths")
load("@bazel_tools//tools/build_defs/cc:action_names.bzl", CC_ACTION_NAMES = "ACTION_NAMES")
load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain")
load("//cuda/private:action_names.bzl", "ACTION_NAMES")
load("//cuda/private:artifact_categories.bzl", "ARTIFACT_CATEGORIES")
Expand Down Expand Up @@ -43,6 +44,13 @@ def _impl(ctx):
]

cc_toolchain = find_cpp_toolchain(ctx)
cc_feature_configuration = cc_common.configure_features(
ctx = ctx,
cc_toolchain = cc_toolchain,
requested_features = ctx.features,
unsupported_features = ctx.disabled_features,
)
host_compiler = cc_common.get_tool_for_action(feature_configuration = cc_feature_configuration, action_name = CC_ACTION_NAMES.cpp_compile)

nvcc_compile_env_feature = feature(
name = "nvcc_compile_env",
Expand All @@ -54,7 +62,7 @@ def _impl(ctx):
],
env_entries = [
env_entry("INCLUDE", ";".join(cc_toolchain.built_in_include_directories)),
env_entry("PATH", paths.dirname(cc_toolchain.compiler_executable) + ";C:/Windows/system32"),
env_entry("PATH", paths.dirname(host_compiler) + ";C:/Windows/system32"),
env_entry("TEMP", ctx.attr.msvc_env_tmp),
env_entry("TMP", ctx.attr.msvc_env_tmp),
],
Expand Down Expand Up @@ -430,8 +438,6 @@ def _impl(ctx):
],
)

static_link_msvcrt_feature = feature(name = "static_link_msvcrt")

static_link_msvcrt_debug_feature = feature(
name = "static_link_msvcrt_debug",
flag_sets = [
Expand Down Expand Up @@ -609,4 +615,5 @@ cuda_toolchain_config = rule(
},
provides = [CudaToolchainConfigInfo],
toolchains = use_cpp_toolchain(),
fragments = ["cpp"],
)

0 comments on commit 4dd02f0

Please sign in to comment.