Skip to content

Commit d7e07fc

Browse files
authored
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.
1 parent 95b150d commit d7e07fc

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

python/private/common/common.bzl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,9 @@ def create_cc_details_struct(
153153
cc_info_for_self_link,
154154
cc_info_with_extra_link_time_libraries,
155155
extra_runfiles,
156-
cc_toolchain):
156+
cc_toolchain,
157+
feature_config,
158+
**kwargs):
157159
"""Creates a CcDetails struct.
158160
159161
Args:
@@ -170,6 +172,12 @@ def create_cc_details_struct(
170172
part of `cc_info_with_extra_link_time_libraries`; should be added to
171173
runfiles.
172174
cc_toolchain: CcToolchain that should be used when building.
175+
feature_config: struct from cc_configure_features(); see
176+
//python/private/common:py_executable.bzl%cc_configure_features.
177+
**kwargs: Additional keys/values to set in the returned struct. This is to
178+
facilitate extensions with less patching. Any added fields should
179+
pick names that are unlikely to collide if the CcDetails API has
180+
additional fields added.
173181
174182
Returns:
175183
A `CcDetails` struct.
@@ -180,6 +188,8 @@ def create_cc_details_struct(
180188
cc_info_with_extra_link_time_libraries = cc_info_with_extra_link_time_libraries,
181189
extra_runfiles = extra_runfiles,
182190
cc_toolchain = cc_toolchain,
191+
feature_config = feature_config,
192+
**kwargs
183193
)
184194

185195
def create_executable_result_struct(*, extra_files_to_build, output_groups, extra_runfiles = None):

python/private/common/py_executable.bzl

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -554,15 +554,7 @@ def _get_native_deps_details(ctx, *, semantics, cc_details, is_test):
554554

555555
dso = ctx.actions.declare_file(semantics.get_native_deps_dso_name(ctx))
556556
share_native_deps = py_internal.share_native_deps(ctx)
557-
cc_feature_config = cc_configure_features(
558-
ctx,
559-
cc_toolchain = cc_details.cc_toolchain,
560-
# See b/171276569#comment18: this feature string is just to allow
561-
# Google's RBE to know the link action is for the Python case so it can
562-
# take special actions (though as of Jun 2022, no special action is
563-
# taken).
564-
extra_features = ["native_deps_link"],
565-
)
557+
cc_feature_config = cc_details.feature_config
566558
if share_native_deps:
567559
linked_lib = _create_shared_native_deps_dso(
568560
ctx,
@@ -921,19 +913,27 @@ def create_base_executable_rule(*, attrs, fragments = [], **kwargs):
921913
**kwargs
922914
)
923915

924-
def cc_configure_features(ctx, *, cc_toolchain, extra_features):
916+
def cc_configure_features(
917+
ctx,
918+
*,
919+
cc_toolchain,
920+
extra_features,
921+
linking_mode = "static_linking_mode"):
925922
"""Configure C++ features for Python purposes.
926923
927924
Args:
928925
ctx: Rule ctx
929926
cc_toolchain: The CcToolchain the target is using.
930927
extra_features: list of strings; additional features to request be
931928
enabled.
929+
linking_mode: str; either "static_linking_mode" or
930+
"dynamic_linking_mode". Specifies the linking mode feature for
931+
C++ linking.
932932
933933
Returns:
934934
struct of the feature configuration and all requested features.
935935
"""
936-
requested_features = ["static_linking_mode"]
936+
requested_features = [linking_mode]
937937
requested_features.extend(extra_features)
938938
requested_features.extend(ctx.features)
939939
if "legacy_whole_archive" not in ctx.disabled_features:

python/private/common/py_executable_bazel.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,7 @@ def _get_cc_details_for_binary(ctx, extra_deps):
560560
extra_runfiles = ctx.runfiles(),
561561
# Though the rules require the CcToolchain, it isn't actually used.
562562
cc_toolchain = None,
563+
feature_config = None,
563564
)
564565

565566
def _get_interpreter_path(ctx, *, runtime, flag_interpreter_path):

0 commit comments

Comments
 (0)