refactor(internal): allow setting linking mode for internal cc_details APIs (#1982) This upstreams a Google patch to control how py_binary links C++ dependencies. This just extends the internal helper functions a bit to allow specifying some of the otherwise hard coded values and passing in additional args to the cc_details struct.
diff --git a/python/private/common/common.bzl b/python/private/common/common.bzl index 0ac9187..5559ccd 100644 --- a/python/private/common/common.bzl +++ b/python/private/common/common.bzl
@@ -153,7 +153,9 @@ cc_info_for_self_link, cc_info_with_extra_link_time_libraries, extra_runfiles, - cc_toolchain): + cc_toolchain, + feature_config, + **kwargs): """Creates a CcDetails struct. Args: @@ -170,6 +172,12 @@ part of `cc_info_with_extra_link_time_libraries`; should be added to runfiles. cc_toolchain: CcToolchain that should be used when building. + feature_config: struct from cc_configure_features(); see + //python/private/common:py_executable.bzl%cc_configure_features. + **kwargs: Additional keys/values to set in the returned struct. This is to + facilitate extensions with less patching. Any added fields should + pick names that are unlikely to collide if the CcDetails API has + additional fields added. Returns: A `CcDetails` struct. @@ -180,6 +188,8 @@ cc_info_with_extra_link_time_libraries = cc_info_with_extra_link_time_libraries, extra_runfiles = extra_runfiles, cc_toolchain = cc_toolchain, + feature_config = feature_config, + **kwargs ) def create_executable_result_struct(*, extra_files_to_build, output_groups, extra_runfiles = None):
diff --git a/python/private/common/py_executable.bzl b/python/private/common/py_executable.bzl index cbe7ccf..6b75b41 100644 --- a/python/private/common/py_executable.bzl +++ b/python/private/common/py_executable.bzl
@@ -554,15 +554,7 @@ dso = ctx.actions.declare_file(semantics.get_native_deps_dso_name(ctx)) share_native_deps = py_internal.share_native_deps(ctx) - cc_feature_config = cc_configure_features( - ctx, - cc_toolchain = cc_details.cc_toolchain, - # See b/171276569#comment18: this feature string is just to allow - # Google's RBE to know the link action is for the Python case so it can - # take special actions (though as of Jun 2022, no special action is - # taken). - extra_features = ["native_deps_link"], - ) + cc_feature_config = cc_details.feature_config if share_native_deps: linked_lib = _create_shared_native_deps_dso( ctx, @@ -921,7 +913,12 @@ **kwargs ) -def cc_configure_features(ctx, *, cc_toolchain, extra_features): +def cc_configure_features( + ctx, + *, + cc_toolchain, + extra_features, + linking_mode = "static_linking_mode"): """Configure C++ features for Python purposes. Args: @@ -929,11 +926,14 @@ cc_toolchain: The CcToolchain the target is using. extra_features: list of strings; additional features to request be enabled. + linking_mode: str; either "static_linking_mode" or + "dynamic_linking_mode". Specifies the linking mode feature for + C++ linking. Returns: struct of the feature configuration and all requested features. """ - requested_features = ["static_linking_mode"] + requested_features = [linking_mode] requested_features.extend(extra_features) requested_features.extend(ctx.features) if "legacy_whole_archive" not in ctx.disabled_features:
diff --git a/python/private/common/py_executable_bazel.bzl b/python/private/common/py_executable_bazel.bzl index 361e075..a0cfeba 100644 --- a/python/private/common/py_executable_bazel.bzl +++ b/python/private/common/py_executable_bazel.bzl
@@ -560,6 +560,7 @@ extra_runfiles = ctx.runfiles(), # Though the rules require the CcToolchain, it isn't actually used. cc_toolchain = None, + feature_config = None, ) def _get_interpreter_path(ctx, *, runtime, flag_interpreter_path):