Skip to content

Commit

Permalink
[DONT MERGE] test: test sysroot flag gen
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudhan committed Feb 2, 2025
1 parent 7a65f56 commit 307b6c2
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 15 deletions.
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
4 changes: 4 additions & 0 deletions WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ register_detected_cuda_toolchains()
load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")

bazel_skylib_workspace()

register_toolchains(
"//tests/flag:flag-test-toolchain",
)
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],
)
35 changes: 20 additions & 15 deletions tests/flag/flag_validation_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,21 @@ def cuda_library_flag_test_impl(ctx):

return analysistest.end(env)

def _rules_cuda_target(target):
# https://github.com/bazelbuild/bazel/issues/19286#issuecomment-1684325913
# must only apply to rules_cuda related labels when bzlmod is enabled
is_bzlmod_enabled = str(Label("//:invalid")).startswith("@@")
label_str = "@//" + target
if is_bzlmod_enabled:
return str(Label(label_str))
else:
return label_str

def _create_cuda_library_flag_test(*config_settings):
merged_config_settings = {}
for cs in config_settings:
for k, v in cs.items():
# https://github.com/bazelbuild/bazel/issues/19286#issuecomment-1684325913
# Wrapping all keys into str(Label(...)) should be a workaround with Bazel 6 and later.
# NOTE: //command_line_option will resolve to @@//command_line_option which is not correct.
# Only apply to cuda related labels when bzlmod is enabled
is_bzlmod_enabled = str(Label("//:invalid")).startswith("@@")
if is_bzlmod_enabled and "cuda" in k:
merged_config_settings[str(Label(k))] = v
else:
merged_config_settings[k] = v
merged_config_settings[k] = v
return analysistest.make(
cuda_library_flag_test_impl,
config_settings = merged_config_settings,
Expand Down Expand Up @@ -96,16 +98,19 @@ cuda_library_c_fastbuild_static_msvcrt_flag_test = _create_cuda_library_flag_tes
cuda_library_c_opt_static_msvcrt_flag_test = _create_cuda_library_flag_test(config_settings_opt, static_link_msvcrt)

# NOTE: @rules_cuda//cuda:archs does not work
config_settings_sm61 = {"@//cuda:archs": "sm_61"}
config_settings_compute60 = {"@//cuda:archs": "compute_60"}
config_settings_compute60_sm61 = {"@//cuda:archs": "compute_60,sm_61"}
config_settings_compute61_sm61 = {"@//cuda:archs": "compute_61,sm_61"}
config_settings_sm90a = {"@//cuda:archs": "sm_90a"}
config_settings_sm90a_sm90 = {"@//cuda:archs": "sm_90a,sm_90"}
config_settings_sm61 = {_rules_cuda_target("cuda:archs"): "sm_61"}
config_settings_compute60 = {_rules_cuda_target("cuda:archs"): "compute_60"}
config_settings_compute60_sm61 = {_rules_cuda_target("cuda:archs"): "compute_60,sm_61"}
config_settings_compute61_sm61 = {_rules_cuda_target("cuda:archs"): "compute_61,sm_61"}
config_settings_sm90a = {_rules_cuda_target("cuda:archs"): "sm_90a"}
config_settings_sm90a_sm90 = {_rules_cuda_target("cuda:archs"): "sm_90a,sm_90"}

cuda_library_sm61_flag_test = _create_cuda_library_flag_test(config_settings_sm61)
cuda_library_sm90a_flag_test = _create_cuda_library_flag_test(config_settings_sm90a)
cuda_library_sm90a_sm90_flag_test = _create_cuda_library_flag_test(config_settings_sm90a_sm90)
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)

0 comments on commit 307b6c2

Please sign in to comment.