From a30714f161e7ccfd86eaffb32eefc843e160dcbe Mon Sep 17 00:00:00 2001 From: Cloud Han Date: Tue, 28 Jan 2025 11:48:03 +0800 Subject: [PATCH 1/2] feat: make sysroot a feature --- cuda/private/actions/compile.bzl | 1 + cuda/private/actions/dlink.bzl | 1 + cuda/private/cuda_helper.bzl | 21 +++++++++++++++++++-- cuda/private/toolchain_configs/clang.bzl | 1 + cuda/private/toolchain_configs/nvcc.bzl | 20 ++++++++++++++++++++ 5 files changed, 42 insertions(+), 2 deletions(-) diff --git a/cuda/private/actions/compile.bzl b/cuda/private/actions/compile.bzl index 5129bdac..3091465a 100644 --- a/cuda/private/actions/compile.bzl +++ b/cuda/private/actions/compile.bzl @@ -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, diff --git a/cuda/private/actions/dlink.bzl b/cuda/private/actions/dlink.bzl index 412cf4da..eb4ab3f7 100644 --- a/cuda/private/actions/dlink.bzl +++ b/cuda/private/actions/dlink.bzl @@ -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, diff --git a/cuda/private/cuda_helper.bzl b/cuda/private/cuda_helper.bzl index 1d5855c4..edf398bd 100644 --- a/cuda/private/cuda_helper.bzl +++ b/cuda/private/cuda_helper.bzl @@ -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 = [], @@ -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 <...>`. @@ -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, @@ -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]) @@ -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, @@ -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, @@ -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. @@ -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, @@ -441,6 +449,7 @@ def _create_compile_variables( ptxas_flags = ptxas_flags, use_pic = use_pic, use_rdc = use_rdc, + **optional_variables ) # buildifier: disable=unused-variable @@ -448,6 +457,7 @@ def _create_device_link_variables( cuda_toolchain, feature_configuration, cuda_archs_info, + sysroot = None, output_file = None, host_compiler = None, host_compile_flags = [], @@ -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. @@ -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, @@ -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): diff --git a/cuda/private/toolchain_configs/clang.bzl b/cuda/private/toolchain_configs/clang.bzl index 844677c0..9c339538 100644 --- a/cuda/private/toolchain_configs/clang.bzl +++ b/cuda/private/toolchain_configs/clang.bzl @@ -205,6 +205,7 @@ def _impl(ctx): flag_set( actions = [ ACTION_NAMES.cuda_compile, + ACTION_NAMES.device_link, ], flag_groups = [ flag_group( diff --git a/cuda/private/toolchain_configs/nvcc.bzl b/cuda/private/toolchain_configs/nvcc.bzl index d2f0c50c..c93f05a6 100644 --- a/cuda/private/toolchain_configs/nvcc.bzl +++ b/cuda/private/toolchain_configs/nvcc.bzl @@ -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, @@ -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, From c7750500d9d7d527b6a735a69ae0e5623aa68334 Mon Sep 17 00:00:00 2001 From: Cloud Han Date: Sun, 2 Feb 2025 16:41:21 +0800 Subject: [PATCH 2/2] [DONT MERGE] test: test sysroot flag gen --- .github/workflows/utilities-tests.yaml | 1 + MODULE.bazel | 5 ++ WORKSPACE.bazel | 5 ++ tests/flag/BUILD.bazel | 69 +++++++++++++++++++++++ tests/flag/flag_test_toolchain_config.bzl | 32 +++++++++++ tests/flag/flag_validation_test.bzl | 3 + 6 files changed, 115 insertions(+) create mode 100644 tests/flag/flag_test_toolchain_config.bzl diff --git a/.github/workflows/utilities-tests.yaml b/.github/workflows/utilities-tests.yaml index 4d3e4269..b14c0162 100644 --- a/.github/workflows/utilities-tests.yaml +++ b/.github/workflows/utilities-tests.yaml @@ -48,5 +48,6 @@ jobs: method: network - run: bazelisk test -- //tests/... + - run: bazelisk test --platforms=//tests/flag:flag-test-platform -- //tests/... - run: bazelisk shutdown diff --git a/MODULE.bazel b/MODULE.bazel index 25c9943e..4ca778e5 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -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", +) diff --git a/WORKSPACE.bazel b/WORKSPACE.bazel index 96301f69..fa275480 100644 --- a/WORKSPACE.bazel +++ b/WORKSPACE.bazel @@ -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", +) diff --git a/tests/flag/BUILD.bazel b/tests/flag/BUILD.bazel index 45ba5d1a..d65e74c2 100644 --- a/tests/flag/BUILD.bazel +++ b/tests/flag/BUILD.bazel @@ -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", @@ -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", +) diff --git a/tests/flag/flag_test_toolchain_config.bzl b/tests/flag/flag_test_toolchain_config.bzl new file mode 100644 index 00000000..43ba6dee --- /dev/null +++ b/tests/flag/flag_test_toolchain_config.bzl @@ -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], +) diff --git a/tests/flag/flag_validation_test.bzl b/tests/flag/flag_validation_test.bzl index 774fc9f7..6e3db08c 100644 --- a/tests/flag/flag_validation_test.bzl +++ b/tests/flag/flag_validation_test.bzl @@ -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)