Move tests from Bazel's CcBinarySplitFunctionsTest to rules_cc. A number of the original tests are redundant - they only really verify that features from the package function and those declared on targets are correctly propagated. Such tests have not been preserved here. PiperOrigin-RevId: 960787284 Change-Id: I4b297e8b66e1e40f6c24e9a40ebe5420af1b8c28
diff --git a/tests/cc/common/BUILD b/tests/cc/common/BUILD index e3ff362..c194b3f 100644 --- a/tests/cc/common/BUILD +++ b/tests/cc/common/BUILD
@@ -1,5 +1,6 @@ load(":cc_bad_dependencies_test.bzl", "cc_bad_dependencies_tests") load(":cc_binary_configured_target_tests.bzl", "cc_binary_configured_target_tests") +load(":cc_binary_split_functions_test.bzl", "cc_binary_split_functions_tests") load(":cc_binary_thin_lto_tests.bzl", "cc_binary_thin_lto_tests") load(":cc_common_test.bzl", "cc_common_tests") load(":cc_dwp_test.bzl", "cc_dwp_tests") @@ -40,3 +41,5 @@ cc_bad_dependencies_tests(name = "cc_bad_dependencies_tests") cc_output_groups_tests(name = "cc_output_groups_tests") + +cc_binary_split_functions_tests(name = "cc_binary_split_functions_tests")
diff --git a/tests/cc/common/cc_binary_split_functions_test.bzl b/tests/cc/common/cc_binary_split_functions_test.bzl new file mode 100644 index 0000000..3163e3f --- /dev/null +++ b/tests/cc/common/cc_binary_split_functions_test.bzl
@@ -0,0 +1,301 @@ +"""Tests for cc_binary with split functions.""" + +load("@rules_testing//lib:analysis_test.bzl", "test_suite") +load("@rules_testing//lib:truth.bzl", "matching") +load("@rules_testing//lib:util.bzl", "util") +load("//cc:cc_binary.bzl", "cc_binary") +load("//tests/cc/testutil:cc_analysis_test.bzl", "cc_analysis_test") + +REQUIRED_TOOLCHAIN_FEATURES = [ + "fsafdo", + "thin_lto", + "supports_start_end_lib", + "enable_fdo_split_functions", + "fdo_optimize", + "fdo_split_functions", + "split_functions", +] + +FSPLIT_MACHINE_FUNCTIONS_FLAG = "-fsplit-machine-functions" + +def _test_implicit_split_functions(name, **kwargs): + util.helper_target( + cc_binary, + name = name + "/bin", + srcs = ["bin.cc"], + ) + + cc_analysis_test( + name = name, + impl = _test_implicit_split_functions_impl, + targets = { + "fdo": name + "/bin", + "fsafdo": name + "/bin", + }, + attrs = { + "fdo": { + "@config_settings": { + "//command_line_option:fdo_optimize": "/profile.zip", + }, + }, + "fsafdo": { + "@config_settings": { + "//command_line_option:fdo_optimize": "/profile.afdo", + }, + }, + }, + config_settings = { + "//command_line_option:compilation_mode": "opt", + }, + with_features = REQUIRED_TOOLCHAIN_FEATURES, + test_features = [ + "fdo_split_functions", + "thin_lto", + ], + **kwargs + ) + +def _test_implicit_split_functions_impl(env, targets): + assert_fdo_lto_action = env.expect.that_target(targets.fdo).action_generating( + "{package}/{test_name}/bin.lto/{bindir}/{package}/_objs/{name}/bin.o", + ) + assert_fdo_lto_action.mnemonic().equals("CcLtoBackendCompile") + assert_fdo_lto_action.argv().contains(FSPLIT_MACHINE_FUNCTIONS_FLAG) + assert_fdo_lto_action.argv().contains("-DBUILD_MFS_ENABLED=1") + + assert_fsafdo_lto_action = env.expect.that_target(targets.fdo).action_generating( + "{package}/{test_name}/bin.lto/{bindir}/{package}/_objs/{name}/bin.o", + ) + assert_fsafdo_lto_action.mnemonic().equals("CcLtoBackendCompile") + assert_fsafdo_lto_action.argv().contains(FSPLIT_MACHINE_FUNCTIONS_FLAG) + assert_fsafdo_lto_action.argv().contains("-DBUILD_MFS_ENABLED=1") + +def _test_no_implicit_split_functions(name, **kwargs): + util.helper_target( + cc_binary, + name = name + "/bin", + srcs = ["bin.cc"], + ) + + cc_analysis_test( + name = name, + impl = _test_no_implicit_split_functions_impl, + targets = { + "fdo": name + "/bin", + "fsafdo": name + "/bin", + }, + attrs = { + "fdo": { + "@config_settings": { + "//command_line_option:fdo_optimize": "/profile.zip", + }, + }, + "fsafdo": { + "@config_settings": { + "//command_line_option:fdo_optimize": "/profile.afdo", + }, + }, + }, + config_settings = { + "//command_line_option:compilation_mode": "opt", + }, + with_features = REQUIRED_TOOLCHAIN_FEATURES, + test_features = [ + "-fdo_split_functions", + "thin_lto", + ], + **kwargs + ) + +def _test_no_implicit_split_functions_impl(env, targets): + assert_fdo_lto_action = env.expect.that_target(targets.fdo).action_generating( + "{package}/{test_name}/bin.lto/{bindir}/{package}/_objs/{name}/bin.o", + ) + assert_fdo_lto_action.mnemonic().equals("CcLtoBackendCompile") + assert_fdo_lto_action.not_contains_arg(FSPLIT_MACHINE_FUNCTIONS_FLAG) + + assert_fsafdo_lto_action = env.expect.that_target(targets.fdo).action_generating( + "{package}/{test_name}/bin.lto/{bindir}/{package}/_objs/{name}/bin.o", + ) + assert_fsafdo_lto_action.mnemonic().equals("CcLtoBackendCompile") + assert_fsafdo_lto_action.not_contains_arg(FSPLIT_MACHINE_FUNCTIONS_FLAG) + +def _test_implicit_split_functions_disabled(name, **kwargs): + util.helper_target( + cc_binary, + name = name + "/bin", + srcs = ["bin.cc"], + ) + + cc_analysis_test( + name = name, + impl = _test_implicit_split_functions_disabled_impl, + targets = { + "fdo": name + "/bin", + "fsafdo": name + "/bin", + }, + attrs = { + "fdo": { + "@config_settings": { + "//command_line_option:fdo_optimize": "/profile.zip", + }, + }, + "fsafdo": { + "@config_settings": { + "//command_line_option:fdo_optimize": "/profile.afdo", + }, + }, + }, + config_settings = { + "//command_line_option:compilation_mode": "opt", + }, + with_features = REQUIRED_TOOLCHAIN_FEATURES, + test_features = [ + "-split_functions", + "fdo_split_functions", + "thin_lto", + ], + **kwargs + ) + +def _test_implicit_split_functions_disabled_impl(env, targets): + assert_fdo_lto_action = env.expect.that_target(targets.fdo).action_generating( + "{package}/{test_name}/bin.lto/{bindir}/{package}/_objs/{name}/bin.o", + ) + assert_fdo_lto_action.mnemonic().equals("CcLtoBackendCompile") + assert_fdo_lto_action.not_contains_arg(FSPLIT_MACHINE_FUNCTIONS_FLAG) + + assert_fsafdo_lto_action = env.expect.that_target(targets.fdo).action_generating( + "{package}/{test_name}/bin.lto/{bindir}/{package}/_objs/{name}/bin.o", + ) + assert_fsafdo_lto_action.mnemonic().equals("CcLtoBackendCompile") + assert_fsafdo_lto_action.not_contains_arg(FSPLIT_MACHINE_FUNCTIONS_FLAG) + +def _test_propeller_optimize_disables_implicit_split_functions(name, **kwargs): + util.helper_target( + cc_binary, + name = name + "/bin", + srcs = ["bin.cc"], + ) + + cc_analysis_test( + name = name, + impl = _test_propeller_optimize_disables_implicit_split_functions_impl, + targets = { + "fdo": name + "/bin", + "fsafdo": name + "/bin", + }, + attrs = { + "fdo": { + "@config_settings": { + "//command_line_option:fdo_optimize": "/profile.zip", + }, + }, + "fsafdo": { + "@config_settings": { + "//command_line_option:fdo_optimize": "/profile.afdo", + }, + }, + }, + config_settings = { + "//command_line_option:compilation_mode": "opt", + "//command_line_option:propeller_optimize_absolute_cc_profile": "/tmp/cc.txt", + }, + with_features = REQUIRED_TOOLCHAIN_FEATURES, + test_features = [ + "fdo_split_functions", + "thin_lto", + ], + **kwargs + ) + +def _test_propeller_optimize_disables_implicit_split_functions_impl(env, targets): + assert_fdo_lto_action = env.expect.that_target(targets.fdo).action_generating( + "{package}/{test_name}/bin.lto/{bindir}/{package}/_objs/{name}/bin.o", + ) + assert_fdo_lto_action.mnemonic().equals("CcLtoBackendCompile") + assert_fdo_lto_action.argv().contains_predicate( + matching.str_matches("-fbasic-block-sections=list=*cc.txt"), + ) + + # Depending on the Bazel version, it may be enabled via the "type" flag or the "enabled" flag. + assert_fdo_lto_action.argv().contains_predicate( + matching.custom( + "'-DBUILD_PROPELLER_ENABLED=1' or '-DBUILD_PROPELLER_TYPE=\"full\"'", + lambda arg: arg in ["-DBUILD_PROPELLER_ENABLED=1", "-DBUILD_PROPELLER_TYPE=\"full\""], + ), + ) + assert_fdo_lto_action.not_contains_arg(FSPLIT_MACHINE_FUNCTIONS_FLAG) + assert_fdo_lto_action.not_contains_arg("-DBUILD_PROPELLER_TYPE=\"split\"") + + assert_fsafdo_lto_action = env.expect.that_target(targets.fdo).action_generating( + "{package}/{test_name}/bin.lto/{bindir}/{package}/_objs/{name}/bin.o", + ) + assert_fsafdo_lto_action.mnemonic().equals("CcLtoBackendCompile") + assert_fsafdo_lto_action.argv().contains_predicate( + matching.str_matches("-fbasic-block-sections=list=*cc.txt"), + ) + assert_fsafdo_lto_action.argv().contains_predicate( + matching.custom( + "'-DBUILD_PROPELLER_ENABLED=1' or '-DBUILD_PROPELLER_TYPE=\"full\"'", + lambda arg: arg in ["-DBUILD_PROPELLER_ENABLED=1", "-DBUILD_PROPELLER_TYPE=\"full\""], + ), + ) + assert_fsafdo_lto_action.not_contains_arg(FSPLIT_MACHINE_FUNCTIONS_FLAG) + assert_fsafdo_lto_action.not_contains_arg("-DBUILD_PROPELLER_TYPE=\"split\"") + +def _test_propeller_optimize_with_explicit_split_functions(name, **kwargs): + util.helper_target( + cc_binary, + name = name + "/bin", + srcs = ["bin.cc"], + ) + + cc_analysis_test( + name = name, + impl = _test_propeller_optimize_with_explicit_split_functions_impl, + target = name + "/bin", + config_settings = { + "//command_line_option:compilation_mode": "opt", + "//command_line_option:fdo_optimize": "/profile.zip", + "//command_line_option:propeller_optimize_absolute_cc_profile": "/tmp/cc.txt", + }, + with_features = REQUIRED_TOOLCHAIN_FEATURES, + test_features = [ + "split_functions", + "fdo_split_functions", + "thin_lto", + ], + **kwargs + ) + +def _test_propeller_optimize_with_explicit_split_functions_impl(env, target): + assert_fdo_lto_action = env.expect.that_target(target).action_generating( + "{package}/{test_name}/bin.lto/{bindir}/{package}/_objs/{name}/bin.o", + ) + assert_fdo_lto_action.mnemonic().equals("CcLtoBackendCompile") + assert_fdo_lto_action.argv().contains_predicate( + matching.str_matches("-fbasic-block-sections=list=*cc.txt"), + ) + assert_fdo_lto_action.argv().contains_predicate( + matching.custom( + "'-DBUILD_PROPELLER_ENABLED=1' or '-DBUILD_PROPELLER_TYPE=\"full\"'", + lambda arg: arg in ["-DBUILD_PROPELLER_ENABLED=1", "-DBUILD_PROPELLER_TYPE=\"full\""], + ), + ) + assert_fdo_lto_action.argv().contains_at_least([ + "-DBUILD_MFS_ENABLED=1", + FSPLIT_MACHINE_FUNCTIONS_FLAG, + ]) + +def cc_binary_split_functions_tests(name): + test_suite( + name = name, + tests = [ + _test_implicit_split_functions, + _test_no_implicit_split_functions, + _test_implicit_split_functions_disabled, + _test_propeller_optimize_disables_implicit_split_functions, + _test_propeller_optimize_with_explicit_split_functions, + ], + )