Migrate batch 3 of LinkBuildVariablesTest to Starlark

Migrated tests:
- testOutputExecpath
- testOutputExecpathIsNotExposedWhenThinLtoIndexing
- testIsCcTestLinkActionBuildVariable
- testStripBinariesIsEnabledWhenStripModeIsAlwaysNoMatterWhat

PiperOrigin-RevId: 955728051
Change-Id: I78ab09149983c91c665719ec92436dc0349f4500
diff --git a/tests/cc/common/link_build_variables_test.bzl b/tests/cc/common/link_build_variables_test.bzl
index cc1baeb..d688aa3 100644
--- a/tests/cc/common/link_build_variables_test.bzl
+++ b/tests/cc/common/link_build_variables_test.bzl
@@ -5,6 +5,7 @@
 load("@rules_testing//lib:util.bzl", "util")
 load("//cc:cc_binary.bzl", "cc_binary")
 load("//cc:cc_library.bzl", "cc_library")
+load("//cc:cc_test.bzl", "cc_test")
 load("//tests/cc/testutil:cc_analysis_test.bzl", "cc_analysis_test")
 load("//tests/cc/testutil:link_action_subject.bzl", "link_action_subject")
 
@@ -15,6 +16,13 @@
         srcs = ["a.cc"],
     )
 
+def _cc_binary_setup(name):
+    util.helper_target(
+        cc_binary,
+        name = name + "_bin",
+        srcs = ["a.cc"],
+    )
+
 def _cc_library_nodeps_dynamic_library_action(env, target):
     action_subject = env.expect.that_target(target).action_generating("{package}/lib{name}.so")
     return link_action_subject.new(action_subject.actual, action_subject.meta)
@@ -24,11 +32,7 @@
     return link_action_subject.new(action_subject.actual, action_subject.meta)
 
 def _test_force_pic_build_variable(name):
-    util.helper_target(
-        cc_binary,
-        name = name + "_bin",
-        srcs = ["a.cc"],
-    )
+    _cc_binary_setup(name)
 
     cc_analysis_test(
         name = name,
@@ -220,6 +224,138 @@
     action.argv().contains("--interface-library-output=ignored")
     action.argv().contains("--interface-library-builder=ignored")
 
+def _test_output_execpath(name):
+    _cc_library_setup(name)
+    cc_analysis_test(
+        name = name,
+        impl = _test_output_execpath_impl,
+        target = name + "_lib",
+        with_features = [
+            "supports_dynamic_linker",
+            "uses_output_execpath",
+        ],
+    )
+
+def _test_output_execpath_impl(env, target):
+    action = _cc_library_nodeps_dynamic_library_action(env, target)
+    action.argv().contains_predicate(
+        matching.custom(
+            "ends with libfoo.so",
+            lambda s: s.startswith("--output-execpath=") and s.endswith("lib" + target.label.name + ".so"),
+        ),
+    )
+
+def _test_output_execpath_is_not_exposed_when_thin_lto_indexing(name):
+    _cc_library_setup(name)
+    cc_analysis_test(
+        name = name,
+        impl = _test_output_execpath_is_not_exposed_when_thin_lto_indexing_impl,
+        target = name + "_lib",
+        with_features = [
+            "thin_lto",
+            "supports_pic",
+            "supports_interface_shared_libraries",
+            "supports_dynamic_linker",
+            "supports_start_end_lib",
+            "uses_output_execpath",
+        ],
+        config_settings = {
+            "//command_line_option:features": ["thin_lto"],
+        },
+    )
+
+def _test_output_execpath_is_not_exposed_when_thin_lto_indexing_impl(env, target):
+    # TODO(b/525692821): Consider using build graph traversal once available to find the LTO backend action.
+    action_subject = env.expect.that_target(target).action_generating(
+        "{package}/lib{name}.so-lto-final.params",
+    )
+    action = link_action_subject.new(action_subject.actual, action_subject.meta)
+    action.argv().not_contains_predicate(
+        matching.custom(
+            "starts with --output-execpath=",
+            lambda s: s.startswith("--output-execpath="),
+        ),
+    )
+
+def _test_is_cc_test_link_action_build_variable_for_test(name):
+    util.helper_target(
+        cc_test,
+        name = name + "_test_target",
+        srcs = ["a.cc"],
+    )
+    cc_analysis_test(
+        name = name,
+        impl = _test_is_cc_test_link_action_build_variable_test_impl,
+        target = name + "_test_target",
+        with_features = ["uses_is_cc_test"],
+    )
+
+def _test_is_cc_test_link_action_build_variable_test_impl(env, target):
+    action = link_action_subject.from_target(env, target)
+    action.argv().contains("--linkopt-is-cc-test")
+
+def _test_is_cc_test_link_action_build_variable_for_binary(name):
+    _cc_binary_setup(name)
+    cc_analysis_test(
+        name = name,
+        impl = _test_is_cc_test_link_action_build_variable_bin_impl,
+        target = name + "_bin",
+        with_features = ["uses_is_cc_test"],
+    )
+
+def _test_is_cc_test_link_action_build_variable_bin_impl(env, target):
+    action = link_action_subject.from_target(env, target)
+    action.argv().contains("--linkopt-is-not-cc-test")
+
+def _strip_test_present_impl(env, target):
+    action = link_action_subject.from_target(env, target)
+    action.argv().contains("--strip-debug-symbols")
+
+def _strip_test_absent_impl(env, target):
+    action = link_action_subject.from_target(env, target)
+    action.argv().not_contains("--strip-debug-symbols")
+
+def _strip_test_helper(name, strip, mode, should_have_strip):
+    _cc_binary_setup(name)
+    impl = _strip_test_present_impl if should_have_strip else _strip_test_absent_impl
+    cc_analysis_test(
+        name = name,
+        impl = impl,
+        target = name + "_bin",
+        with_features = ["uses_strip_debug_symbols"],
+        config_settings = {
+            "//command_line_option:strip": strip,
+            "//command_line_option:compilation_mode": mode,
+        },
+    )
+
+def _test_strip_always_opt(name):
+    _strip_test_helper(name, "always", "opt", True)
+
+def _test_strip_always_fastbuild(name):
+    _strip_test_helper(name, "always", "fastbuild", True)
+
+def _test_strip_always_dbg(name):
+    _strip_test_helper(name, "always", "dbg", True)
+
+def _test_strip_sometimes_opt(name):
+    _strip_test_helper(name, "sometimes", "opt", False)
+
+def _test_strip_sometimes_fastbuild(name):
+    _strip_test_helper(name, "sometimes", "fastbuild", True)
+
+def _test_strip_sometimes_dbg(name):
+    _strip_test_helper(name, "sometimes", "dbg", False)
+
+def _test_strip_never_opt(name):
+    _strip_test_helper(name, "never", "opt", False)
+
+def _test_strip_never_fastbuild(name):
+    _strip_test_helper(name, "never", "fastbuild", False)
+
+def _test_strip_never_dbg(name):
+    _strip_test_helper(name, "never", "dbg", False)
+
 def link_build_variables_tests(name):
     test_suite(
         name = name,
@@ -233,5 +369,18 @@
             _test_interface_library_building_variables_when_generation_possible,
             _test_interface_library_building_variables_when_generation_not_allowed,
             _test_no_ifso_building_when_thin_lto_indexing,
+            _test_output_execpath,
+            _test_output_execpath_is_not_exposed_when_thin_lto_indexing,
+            _test_is_cc_test_link_action_build_variable_for_test,
+            _test_is_cc_test_link_action_build_variable_for_binary,
+            _test_strip_always_opt,
+            _test_strip_always_fastbuild,
+            _test_strip_always_dbg,
+            _test_strip_sometimes_opt,
+            _test_strip_sometimes_fastbuild,
+            _test_strip_sometimes_dbg,
+            _test_strip_never_opt,
+            _test_strip_never_fastbuild,
+            _test_strip_never_dbg,
         ],
     )
diff --git a/tests/cc/testutil/toolchains/cc_toolchain_config.bzl b/tests/cc/testutil/toolchains/cc_toolchain_config.bzl
index a4220db..5e63dc0 100644
--- a/tests/cc/testutil/toolchains/cc_toolchain_config.bzl
+++ b/tests/cc/testutil/toolchains/cc_toolchain_config.bzl
@@ -1240,6 +1240,69 @@
     ],
 )
 
+_uses_output_execpath_feature = feature(
+    name = FEATURE_NAMES.uses_output_execpath,
+    enabled = True,
+    flag_sets = [
+        flag_set(
+            actions = [
+                ACTION_NAMES.cpp_link_dynamic_library,
+                ACTION_NAMES.cpp_link_nodeps_dynamic_library,
+                ACTION_NAMES.cpp_link_executable,
+                "lto-index-for-executable",
+                "lto-index-for-dynamic-library",
+                "lto-index-for-nodeps-dynamic-library",
+            ],
+            flag_groups = [
+                flag_group(
+                    expand_if_available = "output_execpath",
+                    flags = ["--output-execpath=%{output_execpath}"],
+                ),
+            ],
+        ),
+    ],
+)
+
+_uses_is_cc_test_feature = feature(
+    name = FEATURE_NAMES.uses_is_cc_test,
+    enabled = True,
+    flag_sets = [
+        flag_set(
+            actions = [
+                ACTION_NAMES.cpp_link_executable,
+            ],
+            flag_groups = [
+                flag_group(
+                    expand_if_true = "is_cc_test",
+                    flags = ["--linkopt-is-cc-test"],
+                ),
+                flag_group(
+                    expand_if_false = "is_cc_test",
+                    flags = ["--linkopt-is-not-cc-test"],
+                ),
+            ],
+        ),
+    ],
+)
+
+_uses_strip_debug_symbols_feature = feature(
+    name = FEATURE_NAMES.uses_strip_debug_symbols,
+    enabled = True,
+    flag_sets = [
+        flag_set(
+            actions = [
+                ACTION_NAMES.cpp_link_executable,
+            ],
+            flag_groups = [
+                flag_group(
+                    expand_if_available = "strip_debug_symbols",
+                    flags = ["--strip-debug-symbols"],
+                ),
+            ],
+        ),
+    ],
+)
+
 _def_feature = feature(
     name = FEATURE_NAMES.def_feature,
     enabled = True,
@@ -1520,6 +1583,9 @@
     FEATURE_NAMES.runtime_library_search_directories: _runtime_library_search_directories_feature,
     FEATURE_NAMES.generate_submodules: _generate_submodules_feature,
     FEATURE_NAMES.uses_ifso_variables: _uses_ifso_variables_feature,
+    FEATURE_NAMES.uses_output_execpath: _uses_output_execpath_feature,
+    FEATURE_NAMES.uses_is_cc_test: _uses_is_cc_test_feature,
+    FEATURE_NAMES.uses_strip_debug_symbols: _uses_strip_debug_symbols_feature,
     FEATURE_NAMES.def_feature: _def_feature,
     FEATURE_NAMES.strip_debug_symbols: _strip_debug_symbols_feature,
     FEATURE_NAMES.disable_pbh: _disable_pbh_feature,
diff --git a/tests/cc/testutil/toolchains/features.bzl b/tests/cc/testutil/toolchains/features.bzl
index 9f6e93b..f3b0996 100644
--- a/tests/cc/testutil/toolchains/features.bzl
+++ b/tests/cc/testutil/toolchains/features.bzl
@@ -101,4 +101,7 @@
     llvm_profdata_env = "llvm_profdata_env",
     force_pic_flags = "force_pic_flags",
     libraries_to_link = "libraries_to_link",
+    uses_output_execpath = "uses_output_execpath",
+    uses_is_cc_test = "uses_is_cc_test",
+    uses_strip_debug_symbols = "uses_strip_debug_symbols",
 )