Skip to content

wip(test): sysroot feature #322

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions .github/workflows/utilities-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,6 @@ jobs:
method: network

- run: bazelisk test -- //tests/...
- run: bazelisk test --platforms=//tests/flag:flag-test-platform -- //tests/...

- run: bazelisk shutdown
5 changes: 5 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ register_toolchains(
"@local_cuda//toolchain/clang:clang-local-toolchain",
"@local_cuda//toolchain/disabled:disabled-local-toolchain",
)

# FIXME: we must not register dummy test toolchain!
register_toolchains(
"//tests/flag:flag-test-toolchain",
)
5 changes: 5 additions & 0 deletions WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,8 @@ register_detected_cuda_toolchains()
load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")

bazel_skylib_workspace()

# FIXME: we must not register dummy test toolchain!
register_toolchains(
"//tests/flag:flag-test-toolchain",
)
1 change: 1 addition & 0 deletions cuda/private/actions/compile.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def compile(
cuda_toolchain,
cuda_feature_config,
common.cuda_archs_info,
common.sysroot,
source_file = src.path,
output_file = obj_file.path,
host_compiler = host_compiler,
Expand Down
1 change: 1 addition & 0 deletions cuda/private/actions/dlink.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def _compiler_device_link(
cuda_toolchain,
cuda_feature_config,
common.cuda_archs_info,
common.sysroot,
output_file = obj_file.path,
host_compiler = host_compiler,
host_compile_flags = common.host_compile_flags,
Expand Down
21 changes: 19 additions & 2 deletions cuda/private/cuda_helper.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ def _get_cuda_archs_info(ctx):

def _create_common_info(
cuda_archs_info = None,
sysroot = None,
includes = [],
quote_includes = [],
system_includes = [],
Expand All @@ -194,6 +195,7 @@ def _create_common_info(

Args:
cuda_archs_info: `CudaArchsInfo`.
sysroot: The `sysroot`.
includes: include paths. Can be used with `#include <...>` and `#include "..."`.
quote_includes: include paths. Can be used with `#include "..."`.
system_includes: include paths. Can be used with `#include <...>`.
Expand All @@ -212,6 +214,7 @@ def _create_common_info(
"""
return struct(
cuda_archs_info = cuda_archs_info,
sysroot = sysroot,
includes = includes,
quote_includes = quote_includes,
system_includes = system_includes,
Expand Down Expand Up @@ -279,8 +282,6 @@ def _create_common(ctx):
host_defines = []
host_local_defines = [i for i in attr.host_local_defines]
host_compile_flags = attr._default_host_copts[BuildSettingInfo].value + [i for i in attr.host_copts]
if cc_toolchain.sysroot:
host_compile_flags.append("--sysroot={}".format(cc_toolchain.sysroot))
host_link_flags = []
if hasattr(attr, "host_linkopts"):
host_link_flags.extend([i for i in attr.host_linkopts])
Expand All @@ -295,6 +296,7 @@ def _create_common(ctx):

return _create_common_info(
cuda_archs_info = _get_cuda_archs_info(ctx),
sysroot = getattr(cc_toolchain, "sysroot", None),
includes = includes,
quote_includes = quote_includes,
system_includes = system_includes,
Expand Down Expand Up @@ -388,6 +390,7 @@ def _create_compile_variables(
cuda_toolchain,
feature_configuration,
cuda_archs_info,
sysroot = None,
source_file = None,
output_file = None,
host_compiler = None,
Expand All @@ -407,6 +410,7 @@ def _create_compile_variables(
cuda_toolchain: cuda_toolchain for which we are creating build variables.
feature_configuration: Feature configuration to be queried.
cuda_archs_info: `CudaArchsInfo`
sysroot: The `sysroot`.
source_file: source file for the compilation.
output_file: output file of the compilation.
host_compiler: host compiler path.
Expand All @@ -425,6 +429,10 @@ def _create_compile_variables(
if not use_rdc:
use_rdc = _check_must_enforce_rdc(arch_specs = arch_specs)

optional_variables = {}
if sysroot != None:
optional_variables["sysroot"] = sysroot

return struct(
arch_specs = arch_specs,
use_arch_native = len(arch_specs) == 0,
Expand All @@ -441,13 +449,15 @@ def _create_compile_variables(
ptxas_flags = ptxas_flags,
use_pic = use_pic,
use_rdc = use_rdc,
**optional_variables
)

# buildifier: disable=unused-variable
def _create_device_link_variables(
cuda_toolchain,
feature_configuration,
cuda_archs_info,
sysroot = None,
output_file = None,
host_compiler = None,
host_compile_flags = [],
Expand All @@ -459,6 +469,7 @@ def _create_device_link_variables(
cuda_toolchain: cuda_toolchain for which we are creating build variables.
feature_configuration: Feature configuration to be queried.
cuda_archs_info: `CudaArchsInfo`
sysroot: The `sysroot`.
output_file: output file of the device linking.
host_compiler: host compiler path.
host_compile_flags: flags pass to host compiler.
Expand All @@ -477,6 +488,11 @@ def _create_device_link_variables(
if stage2_arch.lto:
use_dlto = True
break

optional_variables = {}
if sysroot != None:
optional_variables["sysroot"] = sysroot

return struct(
arch_specs = arch_specs,
use_arch_native = len(arch_specs) == 0,
Expand All @@ -486,6 +502,7 @@ def _create_device_link_variables(
user_link_flags = user_link_flags,
use_dlto = use_dlto,
use_pic = use_pic,
**optional_variables
)

def _get_all_unsupported_features(ctx, cuda_toolchain, unsupported_features):
Expand Down
1 change: 1 addition & 0 deletions cuda/private/toolchain_configs/clang.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ def _impl(ctx):
flag_set(
actions = [
ACTION_NAMES.cuda_compile,
ACTION_NAMES.device_link,
],
flag_groups = [
flag_group(
Expand Down
20 changes: 20 additions & 0 deletions cuda/private/toolchain_configs/nvcc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,25 @@ def _impl(ctx):
provides = ["compilation_mode"],
)

sysroot_feature = feature(
name = "sysroot",
enabled = True,
flag_sets = [
flag_set(
actions = [
ACTION_NAMES.cuda_compile,
ACTION_NAMES.device_link,
],
flag_groups = [
flag_group(
flags = ["-Xcompiler", "--sysroot=%{sysroot}"],
expand_if_available = "sysroot",
),
],
),
],
)

ptxas_flags_feature = feature(
name = "ptxas_flags",
enabled = True,
Expand Down Expand Up @@ -498,6 +517,7 @@ def _impl(ctx):
dbg_feature,
opt_feature,
fastbuild_feature,
sysroot_feature,
ptxas_flags_feature,
compiler_input_flags_feature,
compiler_output_flags_feature,
Expand Down
69 changes: 69 additions & 0 deletions tests/flag/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,18 @@ load(
"cuda_library_compute60_sm61_flag_test",
"cuda_library_compute61_sm61_flag_test",
"cuda_library_flag_test",
"cuda_library_platform_flag_test",
"cuda_library_sm61_flag_test",
"cuda_library_sm90a_flag_test",
"cuda_library_sm90a_sm90_flag_test",
"num_actions_test",
)
load(":flag_test_toolchain_config.bzl", "flag_test_toolchain_config")

filegroup(
name = "empty",
srcs = [],
)

num_actions_test(
name = "cuda_library_num_actions_test",
Expand Down Expand Up @@ -348,3 +355,65 @@ cuda_library_sm90a_sm90_flag_test(
],
target_under_test = "@rules_cuda_examples//basic:kernel",
)

cc_toolchain(
name = "flag-test-compiler",
all_files = ":empty",
compiler_files = ":empty",
dwp_files = ":empty",
linker_files = ":empty",
objcopy_files = ":empty",
strip_files = ":empty",
supports_param_files = 1,
toolchain_config = ":flag_test_toolchain_config",
)

toolchain(
name = "flag-test-toolchain",
exec_compatible_with = [
"@platforms//cpu:x86_64",
"@platforms//os:linux",
],
target_compatible_with = [
"@platforms//cpu:aarch64",
"@platforms//os:linux",
],
toolchain = ":flag-test-compiler",
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
)

flag_test_toolchain_config(
name = "flag_test_toolchain_config",
)

platform(
name = "flag-test-platform",
constraint_values = [
"@platforms//cpu:aarch64",
"@platforms//os:linux",
],
)

cuda_library_flag_test(
name = "cuda_library_no_sysroot_flag_test",
action_mnemonic = "CudaCompile",
not_contain_flags = ["--sysroot=/sysroot/for/flag/test"],
output_name = "kernel.pic.o",
target_compatible_with = [
"@platforms//cpu:x86_64",
"@platforms//os:linux",
],
target_under_test = "@rules_cuda_examples//basic:kernel",
)

cuda_library_platform_flag_test(
name = "cuda_library_has_sysroot_flag_test",
action_mnemonic = "CudaCompile",
contain_flags = ["--sysroot=/sysroot/for/flag/test"],
output_name = "kernel.pic.o",
target_compatible_with = [
"@platforms//cpu:aarch64",
"@platforms//os:linux",
],
target_under_test = "@rules_cuda_examples//basic:kernel",
)
32 changes: 32 additions & 0 deletions tests/flag/flag_test_toolchain_config.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
load("@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl", "tool_path")

def _impl(ctx):
return cc_common.create_cc_toolchain_config_info(
ctx = ctx,
toolchain_identifier = "flag-test-toolchain",
host_system_name = "local",
target_system_name = "aarch64-linux-gnu",
target_cpu = "aarch64",
compiler = "gcc",
target_libc = "glibc-2.2.2",
# abi_version = "gcc",
# abi_libc_version = abi_libc_version,
cc_target_os = None,
builtin_sysroot = "/sysroot/for/flag/test",
tool_paths = [
tool_path(name = "gcc", path = "/usr/bin/aarch64-linux-gnu-gcc"),
tool_path(name = "ld", path = "/usr/bin/aarch64-linux-gnu-ld"),
tool_path(name = "ar", path = "/usr/bin/aarch64-linux-gnu-ar"),
tool_path(name = "cpp", path = "/usr/bin/aarch64-linux-gnu-g++"),
tool_path(name = "strip", path = "/usr/bin/aarch64-linux-gnu-strip"),
tool_path(name = "nm", path = "/usr/bin/aarch64-linux-gnu-nm"),
tool_path(name = "objdump", path = "/usr/bin/aarch64-linux-gnu-objdump"),
tool_path(name = "objcopy", path = "/usr/bin/aarch64-linux-gnu-objcopy"),
],
)

flag_test_toolchain_config = rule(
implementation = _impl,
attrs = {},
provides = [CcToolchainConfigInfo],
)
3 changes: 3 additions & 0 deletions tests/flag/flag_validation_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,6 @@ cuda_library_sm90a_sm90_flag_test = _create_cuda_library_flag_test(config_settin
cuda_library_compute60_flag_test = _create_cuda_library_flag_test(config_settings_compute60)
cuda_library_compute60_sm61_flag_test = _create_cuda_library_flag_test(config_settings_compute60_sm61)
cuda_library_compute61_sm61_flag_test = _create_cuda_library_flag_test(config_settings_compute61_sm61)

config_settings_platform_flag_test = {"//command_line_option:platforms": _rules_cuda_target("tests/flag:flag-test-platform")}
cuda_library_platform_flag_test = _create_cuda_library_flag_test(config_settings_platform_flag_test)
Loading