Skip to content

Commit

Permalink
Implement flag enabling Starlark implementation of C++ linking support
Browse files Browse the repository at this point in the history
Most of the linking code is in Starlark, including a large chunk of LTO support. What's not in Starlark:
- conversion of LibraryToLink into LegacyLinkerInputs (causes a regression)
- conversion of collectLibrariesToLink (causes a regression)
- compilation of LTO artifacts (needs rewrite of C++ compilation support first)

PiperOrigin-RevId: 723497031
Change-Id: I9cd75a9c11de9eb82b277757267bcaeaecfd7034
  • Loading branch information
comius authored and copybara-github committed Feb 5, 2025
1 parent ed34bb3 commit 22d277f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1011,4 +1011,13 @@ public boolean getProtoProfile(StarlarkThread thread) throws EvalException {
CcModule.checkPrivateStarlarkificationAllowlist(thread);
return cppOptions.protoProfile;
}

@StarlarkMethod(
name = "experimental_starlark_linking",
documented = false,
useStarlarkThread = true)
public boolean experimentalStarlarkLinking(StarlarkThread thread) throws EvalException {
CcModule.checkPrivateStarlarkificationAllowlist(thread);
return cppOptions.experimentalStarlarkLinking;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1078,6 +1078,17 @@ public Label getMemProfProfileLabel() {
+ " toolchain() resolution to choose a test runner.")
public boolean experimentalPlatformCcTest;

@Option(
name = "experimental_starlark_linking",
defaultValue = "false",
documentationCategory = OptionDocumentationCategory.UNDOCUMENTED,
effectTags = {
OptionEffectTag.LOADING_AND_ANALYSIS,
},
metadataTags = {OptionMetadataTag.EXPERIMENTAL},
help = "If enabled, a Starlark version of linking is used.")
public boolean experimentalStarlarkLinking;

/** See {@link #targetLibcTopLabel} documentation. * */
@Override
public FragmentOptions getNormalized() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ bazel_fragments["CppOptions"] = fragment(
"//command_line_option:experimental_platform_cc_test",
"//command_line_option:experimental_save_feature_state",
"//command_line_option:experimental_use_llvm_covmap",
"//command_line_option:experimental_starlark_linking",
],
outputs = [
"//command_line_option:crosstool_top",
Expand Down
14 changes: 12 additions & 2 deletions src/main/starlark/builtins_bzl/common/cc/cc_common.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ load(
)
load(":common/cc/cc_info.bzl", "CcInfo")
load(":common/cc/cc_shared_library_hint_info.bzl", "CcSharedLibraryHintInfo")
load(":common/cc/link/create_linking_context_from_compilation_outputs.bzl", "create_linking_context_from_compilation_outputs")
load(":common/cc/link/link.bzl", "link")
load(":common/cc/link/link_build_variables.bzl", "create_link_variables")

cc_common_internal = _builtins.internal.cc_common
Expand Down Expand Up @@ -126,7 +128,11 @@ def _link(
if emit_interface_shared_library == _UNBOUND:
emit_interface_shared_library = False

return cc_common_internal.link(
if cc_toolchain._cpp_configuration.experimental_starlark_linking():
link_func = link
else:
link_func = cc_common_internal.link
return link_func(
actions = actions,
name = name,
feature_configuration = feature_configuration,
Expand Down Expand Up @@ -570,7 +576,11 @@ def _create_linking_context_from_compilation_outputs(
if test_only_target == _UNBOUND:
test_only_target = False

return cc_common_internal.create_linking_context_from_compilation_outputs(
if cc_toolchain._cpp_configuration.experimental_starlark_linking():
linking_func = create_linking_context_from_compilation_outputs
else:
linking_func = cc_common_internal.create_linking_context_from_compilation_outputs
return linking_func(
actions = actions,
name = name,
feature_configuration = feature_configuration,
Expand Down

0 comments on commit 22d277f

Please sign in to comment.