Skip to content

Commit

Permalink
python: port PyCcLinkParamsProvider to Starlark and enable it.
Browse files Browse the repository at this point in the history
Because providers are based on identity, and there are Java tests that need
to reference the provider, it's hard to split up the changes, so it's all
in a single change.

The Java class is kept to make usage by the Java tests easier; it isn't
actually used outside of tests.

Work towards bazelbuild#15897

PiperOrigin-RevId: 523182610
Change-Id: I6e07cf55ba08bb87c5ccb6cd37b473ed6aeeb760
  • Loading branch information
rickeylev authored and fweikert committed May 25, 2023
1 parent 4595e6e commit b2c6405
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@
import com.google.devtools.build.lib.rules.proto.BazelProtoLibraryRule;
import com.google.devtools.build.lib.rules.proto.ProtoConfiguration;
import com.google.devtools.build.lib.rules.proto.ProtoLangToolchainRule;
import com.google.devtools.build.lib.rules.python.PyCcLinkParamsProvider;
import com.google.devtools.build.lib.rules.python.PyInfo;
import com.google.devtools.build.lib.rules.python.PyRuleClasses.PySymlink;
import com.google.devtools.build.lib.rules.python.PyRuntimeRule;
Expand Down Expand Up @@ -476,8 +475,7 @@ public void init(ConfiguredRuleClassProvider.Builder builder) {
PyInfo.PROVIDER,
PyStarlarkTransitions.INSTANCE,
new GoogleLegacyStubs.PyWrapCcHelper(),
new GoogleLegacyStubs.PyWrapCcInfoProvider(),
PyCcLinkParamsProvider.PROVIDER));
new GoogleLegacyStubs.PyWrapCcInfoProvider()));

builder.addSymlinkDefinition(PySymlink.PY2);
builder.addSymlinkDefinition(PySymlink.PY3);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import com.google.devtools.build.lib.starlarkbuildapi.cpp.CompilationInfoApi;
import com.google.devtools.build.lib.starlarkbuildapi.cpp.CppConfigurationApi;
import com.google.devtools.build.lib.starlarkbuildapi.cpp.FeatureConfigurationApi;
import com.google.devtools.build.lib.starlarkbuildapi.cpp.PyCcLinkParamsProviderApi;
import com.google.devtools.build.lib.starlarkbuildapi.cpp.PyWrapCcHelperApi;
import com.google.devtools.build.lib.starlarkbuildapi.cpp.PyWrapCcInfoApi;
import com.google.devtools.build.lib.starlarkbuildapi.cpp.WrapCcHelperApi;
Expand Down Expand Up @@ -210,16 +209,4 @@ public void repr(Printer printer) {
printer.append("<unknown object>");
}
}

/**
* Fake no-op implementation of {@link PyCcLinkParamsProviderApi.Provider}. This implementation
* should be unreachable without (discouraged) use of --experimental_google_legacy_api.
*/
public static class PyCcLinkParamsProvider implements PyCcLinkParamsProviderApi.Provider {

@Override
public void repr(Printer printer) {
printer.append("<unknown object>");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ java_library(
"//src/main/java/com/google/devtools/build/lib/rules/cpp",
"//src/main/java/com/google/devtools/build/lib/skyframe/serialization/autocodec",
"//src/main/java/com/google/devtools/build/lib/skyframe/serialization/autocodec:serialization-constant",
"//src/main/java/com/google/devtools/build/lib/starlarkbuildapi/cpp",
"//src/main/java/com/google/devtools/build/lib/starlarkbuildapi/python",
"//src/main/java/com/google/devtools/build/lib/util",
"//src/main/java/com/google/devtools/build/lib/util:filetype",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
import com.google.devtools.build.lib.collect.nestedset.Order;
import com.google.devtools.build.lib.packages.StarlarkProvider;
import com.google.devtools.build.lib.rules.cpp.CcInfo;
import com.google.devtools.build.lib.util.Fingerprint;
import com.google.devtools.build.lib.util.OS;
import com.google.devtools.build.lib.vfs.PathFragment;
Expand Down Expand Up @@ -93,22 +92,6 @@ public boolean regexMatch(String subject, String pattern) {
return subject.matches(pattern);
}

@StarlarkMethod(
name = "new_py_cc_link_params_provider",
doc = "Creates a <code>PyCcLinkParamsProvder</code>.",
parameters = {
@Param(
name = "cc_info",
positional = false,
named = true,
defaultValue = "unbound",
doc = "The CcInfo whose linking context to propagate; other information is discarded"),
},
useStarlarkThread = false)
public PyCcLinkParamsProvider newPyCcLinkParamsProvider(CcInfo ccInfo) {
return new PyCcLinkParamsProvider(ccInfo);
}

@StarlarkMethod(
name = "get_legacy_external_runfiles",
doc = "Get the --legacy_external_runfiles flag value",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,51 @@
// limitations under the License.
package com.google.devtools.build.lib.rules.python;

import com.google.devtools.build.lib.actions.Artifact;
import com.google.common.annotations.VisibleForTesting;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
import com.google.devtools.build.lib.packages.BuiltinProvider;
import com.google.devtools.build.lib.packages.NativeInfo;
import com.google.devtools.build.lib.packages.Info;
import com.google.devtools.build.lib.packages.RuleClass.ConfiguredTargetFactory.RuleErrorException;
import com.google.devtools.build.lib.packages.StarlarkInfo;
import com.google.devtools.build.lib.packages.StarlarkProviderWrapper;
import com.google.devtools.build.lib.rules.cpp.CcInfo;
import com.google.devtools.build.lib.starlarkbuildapi.cpp.PyCcLinkParamsProviderApi;
import net.starlark.java.eval.EvalException;

/** A target that provides C++ libraries to be linked into Python targets. */
@VisibleForTesting
@Immutable
public final class PyCcLinkParamsProvider extends NativeInfo
implements PyCcLinkParamsProviderApi<Artifact> {
public final class PyCcLinkParamsProvider {
public static final Provider PROVIDER = new Provider();

private final CcInfo ccInfo;

public PyCcLinkParamsProvider(CcInfo ccInfo) {
this.ccInfo = CcInfo.builder().setCcLinkingContext(ccInfo.getCcLinkingContext()).build();
public PyCcLinkParamsProvider(StarlarkInfo info) throws EvalException {
this.ccInfo = info.getValue("cc_info", CcInfo.class);
}

@Override
public Provider getProvider() {
return PROVIDER;
}

@Override
public CcInfo getCcInfo() {
return ccInfo;
}

/** Provider class for {@link PyCcLinkParamsProvider} objects. */
public static class Provider extends BuiltinProvider<PyCcLinkParamsProvider>
implements PyCcLinkParamsProviderApi.Provider {
public static class Provider extends StarlarkProviderWrapper<PyCcLinkParamsProvider> {
private Provider() {
super("PyCcLinkParamsProvider", PyCcLinkParamsProvider.class);
super(
Label.parseCanonicalUnchecked("@_builtins//:common/python/providers.bzl"),
"PyCcLinkParamsProvider");
}

@Override
public PyCcLinkParamsProvider wrap(Info value) throws RuleErrorException {
try {
return new PyCcLinkParamsProvider((StarlarkInfo) value);
} catch (EvalException e) {
throw new RuleErrorException(e.getMessageWithStack());
}
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import com.google.devtools.build.lib.packages.semantics.BuildLanguageOptions;
import com.google.devtools.build.lib.starlarkbuildapi.core.Bootstrap;
import com.google.devtools.build.lib.starlarkbuildapi.core.ContextAndFlagGuardedValue;
import com.google.devtools.build.lib.starlarkbuildapi.cpp.PyCcLinkParamsProviderApi;
import com.google.devtools.build.lib.starlarkbuildapi.cpp.PyWrapCcHelperApi;
import com.google.devtools.build.lib.starlarkbuildapi.cpp.PyWrapCcInfoApi;
import com.google.devtools.build.lib.starlarkbuildapi.python.PyInfoApi.PyInfoProviderApi;
Expand All @@ -40,19 +39,16 @@ public class PyBootstrap implements Bootstrap {
private final PyStarlarkTransitionsApi pyStarlarkTransitionsApi;
private final PyWrapCcHelperApi<?, ?, ?, ?, ?, ?, ?, ?, ?> pyWrapCcHelper;
private final PyWrapCcInfoApi.Provider pyWrapCcInfoProvider;
private final PyCcLinkParamsProviderApi.Provider pyCcLinkInfoParamsInfoProvider;

public PyBootstrap(
PyInfoProviderApi pyInfoProviderApi,
PyStarlarkTransitionsApi pyStarlarkTransitionsApi,
PyWrapCcHelperApi<?, ?, ?, ?, ?, ?, ?, ?, ?> pyWrapCcHelper,
PyWrapCcInfoApi.Provider pyWrapCcInfoProvider,
PyCcLinkParamsProviderApi.Provider pyCcLinkInfoParamsInfoProvider) {
PyWrapCcInfoApi.Provider pyWrapCcInfoProvider) {
this.pyInfoProviderApi = pyInfoProviderApi;
this.pyStarlarkTransitionsApi = pyStarlarkTransitionsApi;
this.pyWrapCcHelper = pyWrapCcHelper;
this.pyWrapCcInfoProvider = pyWrapCcInfoProvider;
this.pyCcLinkInfoParamsInfoProvider = pyCcLinkInfoParamsInfoProvider;
}

@Override
Expand Down Expand Up @@ -88,7 +84,7 @@ public void addBindingsToBuilder(ImmutableMap.Builder<String, Object> builder) {
"PyCcLinkParamsProvider",
ContextAndFlagGuardedValue.onlyInAllowedReposOrWhenIncompatibleFlagIsFalse(
BuildLanguageOptions.INCOMPATIBLE_STOP_EXPORTING_LANGUAGE_MODULES,
pyCcLinkInfoParamsInfoProvider,
Starlark.NONE,
allowedRepositories));
}
}
5 changes: 1 addition & 4 deletions src/main/starlark/builtins_bzl/common/python/providers.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def _PyCcLinkParamsProvider_init(cc_info):
}

# buildifier: disable=name-conventions
StarlarkPyCcLinkParamsProvider, _unused_raw_py_cc_link_params_provider_ctor = provider(
PyCcLinkParamsProvider, _unused_raw_py_cc_link_params_provider_ctor = provider(
doc = ("Python-wrapper to forward CcInfo.linking_context. This is to " +
"allow Python targets to propagate C++ linking information, but " +
"without the Python target appearing to be a valid C++ rule dependency"),
Expand All @@ -208,6 +208,3 @@ StarlarkPyCcLinkParamsProvider, _unused_raw_py_cc_link_params_provider_ctor = pr
"cc_info": "A CcInfo instance; it has only linking_context set",
},
)

# TODO(b/203567235): Re-implement in Starlark
PyCcLinkParamsProvider = _builtins.toplevel.PyCcLinkParamsProvider # buildifier: disable=name-conventions
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ load(
)
load(
":common/python/providers.bzl",
"PyCcLinkParamsProvider",
"PyRuntimeInfo",
)
load(
Expand Down Expand Up @@ -758,7 +759,7 @@ def _create_providers(
# are cleaned up.
if cc_info:
providers.append(
_py_builtins.new_py_cc_link_params_provider(cc_info = cc_info),
PyCcLinkParamsProvider(cc_info = cc_info),
)

py_info, deps_transitive_sources = create_py_info(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,4 @@ py_internal = struct(
is_singleton_depset = _py_builtins.is_singleton_depset,
make_runfiles_respect_legacy_external_runfiles = _py_builtins.make_runfiles_respect_legacy_external_runfiles,
merge_runfiles_with_generated_inits_empty_files_supplier = _py_builtins.merge_runfiles_with_generated_inits_empty_files_supplier,
new_py_cc_link_params_provider = _py_builtins.new_py_cc_link_params_provider,
)
3 changes: 2 additions & 1 deletion src/main/starlark/builtins_bzl/common/python/py_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ load(
"filter_to_py_srcs",
"union_attrs",
)
load(":common/python/providers.bzl", "PyCcLinkParamsProvider")

_py_builtins = _builtins.internal.py_builtins

Expand Down Expand Up @@ -76,7 +77,7 @@ def py_library_impl(ctx, *, semantics):
DefaultInfo(files = output_sources, runfiles = runfiles),
py_info,
create_instrumented_files_info(ctx),
_py_builtins.new_py_cc_link_params_provider(cc_info = cc_info),
PyCcLinkParamsProvider(cc_info = cc_info),
create_output_group_info(py_info.transitive_sources, extra_groups = {}),
]

Expand Down

0 comments on commit b2c6405

Please sign in to comment.