diff --git a/src/main/java/com/google/devtools/build/lib/rules/python/PyBinary.java b/src/main/java/com/google/devtools/build/lib/rules/python/PyBinary.java index e0706923f2f409..182545f4c066e9 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/python/PyBinary.java +++ b/src/main/java/com/google/devtools/build/lib/rules/python/PyBinary.java @@ -117,7 +117,7 @@ static RuleConfiguredTargetBuilder init(RuleContext ruleContext, PythonSemantics RuleConfiguredTargetBuilder builder = new RuleConfiguredTargetBuilder(ruleContext); - common.addCommonTransitiveInfoProviders(builder, semantics, common.getFilesToBuild()); + common.addCommonTransitiveInfoProviders(builder, semantics, common.getFilesToBuild(), imports); semantics.postInitBinary(ruleContext, runfilesSupport, common); return builder diff --git a/src/main/java/com/google/devtools/build/lib/rules/python/PyCommon.java b/src/main/java/com/google/devtools/build/lib/rules/python/PyCommon.java index d8da9b8bf05684..1f14d026d1464d 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/python/PyCommon.java +++ b/src/main/java/com/google/devtools/build/lib/rules/python/PyCommon.java @@ -69,6 +69,7 @@ public final class PyCommon { public static final String PYTHON_SKYLARK_PROVIDER_NAME = "py"; public static final String TRANSITIVE_PYTHON_SRCS = "transitive_sources"; public static final String IS_USING_SHARED_LIBRARY = "uses_shared_libraries"; + public static final String IMPORTS = "imports"; private static final LocalMetadataCollector METADATA_COLLECTOR = new LocalMetadataCollector() { @Override @@ -154,7 +155,7 @@ public Artifact getPythonZipArtifact(Artifact executable) { } public void addCommonTransitiveInfoProviders(RuleConfiguredTargetBuilder builder, - PythonSemantics semantics, NestedSet filesToBuild) { + PythonSemantics semantics, NestedSet filesToBuild, NestedSet imports) { builder .add( @@ -166,7 +167,7 @@ public void addCommonTransitiveInfoProviders(RuleConfiguredTargetBuilder builder filesToBuild)) .addSkylarkTransitiveInfo( PYTHON_SKYLARK_PROVIDER_NAME, - createSourceProvider(this.transitivePythonSources, usesSharedLibraries())) + createSourceProvider(transitivePythonSources, usesSharedLibraries(), imports)) // Python targets are not really compilable. The best we can do is make sure that all // generated source files are ready. .addOutputGroup(OutputGroupProvider.FILES_TO_COMPILE, transitivePythonSources) @@ -179,16 +180,37 @@ public void addCommonTransitiveInfoProviders(RuleConfiguredTargetBuilder builder *

addSkylarkTransitiveInfo(PYTHON_SKYLARK_PROVIDER_NAME, createSourceProvider(...)) */ public static Info createSourceProvider( - NestedSet transitivePythonSources, boolean isUsingSharedLibrary) { + NestedSet transitivePythonSources, boolean isUsingSharedLibrary, + NestedSet imports) { return NativeProvider.STRUCT.create( ImmutableMap.of( TRANSITIVE_PYTHON_SRCS, SkylarkNestedSet.of(Artifact.class, transitivePythonSources), IS_USING_SHARED_LIBRARY, - isUsingSharedLibrary), + isUsingSharedLibrary, + IMPORTS, + SkylarkNestedSet.of(String.class, makeImportStrings(imports)) + ), "No such attribute '%s'"); } + /** + * Converts {@link NestedSet} of {@link PathFragment} to + * {@link NestedSet} of {@link String} + * @param imports - {@link NestedSet} of {@link PathFragment} + * @return - {@link NestedSet} of {@link String} + */ + public static NestedSet makeImportStrings( + NestedSet imports) { + final NestedSetBuilder builder = NestedSetBuilder.compileOrder(); + + for (PathFragment pathFragment : imports) { + builder.add(pathFragment.toString()); + } + + return builder.build(); + } + public PythonVersion getDefaultPythonVersion() { return ruleContext.getRule() .isAttrDefined("default_python_version", Type.STRING) diff --git a/src/main/java/com/google/devtools/build/lib/rules/python/PyLibrary.java b/src/main/java/com/google/devtools/build/lib/rules/python/PyLibrary.java index adc0ccae37ab99..ef8721d790e8c7 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/python/PyLibrary.java +++ b/src/main/java/com/google/devtools/build/lib/rules/python/PyLibrary.java @@ -88,7 +88,7 @@ protected void collect(CcLinkParams.Builder builder, boolean linkingStatically, runfilesBuilder.addRunfiles(ruleContext, RunfilesProvider.DEFAULT_RUNFILES); RuleConfiguredTargetBuilder builder = new RuleConfiguredTargetBuilder(ruleContext); - common.addCommonTransitiveInfoProviders(builder, semantics, filesToBuild); + common.addCommonTransitiveInfoProviders(builder, semantics, filesToBuild, imports); return builder .setFilesToBuild(filesToBuild) .add(RunfilesProvider.class, RunfilesProvider.simple(runfilesBuilder.build()))