-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added support to expose imports for python to Skylark. #2791
Changes from 4 commits
8d8eae9
e0d301b
98c25c3
01f59a7
ff966ff
a94b859
a8da0d6
1507e03
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -132,9 +133,12 @@ public void initBinary(List<Artifact> srcs) { | |
addPyExtraActionPseudoAction(); | ||
} | ||
|
||
public void addCommonTransitiveInfoProviders(RuleConfiguredTargetBuilder builder, | ||
PythonSemantics semantics, NestedSet<Artifact> filesToBuild) { | ||
|
||
public void addCommonTransitiveInfoProviders( | ||
RuleContext ruleContext, | ||
RuleConfiguredTargetBuilder builder, | ||
PythonSemantics semantics, | ||
NestedSet<Artifact> filesToBuild, | ||
NestedSet<PathFragment> imports) { | ||
builder | ||
.add( | ||
InstrumentedFilesProvider.class, | ||
|
@@ -145,7 +149,10 @@ 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) | ||
|
@@ -158,16 +165,39 @@ public void addCommonTransitiveInfoProviders(RuleConfiguredTargetBuilder builder | |
* addSkylarkTransitiveInfo(PYTHON_SKYLARK_PROVIDER_NAME, createSourceProvider(...)) | ||
*/ | ||
public static SkylarkClassObject createSourceProvider( | ||
NestedSet<Artifact> transitivePythonSources, boolean isUsingSharedLibrary) { | ||
NestedSet<Artifact> transitivePythonSources, | ||
boolean isUsingSharedLibrary, | ||
NestedSet<PathFragment> imports) { | ||
return NativeClassObjectConstructor.STRUCT.create( | ||
ImmutableMap.<String, Object>of( | ||
TRANSITIVE_PYTHON_SRCS, | ||
SkylarkNestedSet.of(Artifact.class, transitivePythonSources), | ||
IS_USING_SHARED_LIBRARY, | ||
isUsingSharedLibrary), | ||
isUsingSharedLibrary, | ||
IMPORTS, | ||
SkylarkNestedSet.of(String.class, | ||
makeImportStrings(imports )) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. super nit: unnecessary space after "imports" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
), | ||
"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<String> makeImportStrings( | ||
NestedSet<PathFragment> imports) { | ||
final NestedSetBuilder<String> builder = NestedSetBuilder.compileOrder(); | ||
|
||
for (PathFragment pathFragment : imports) { | ||
builder.add(pathFragment.toString()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This effectively negates any benefit of nested sets here, because you create a new, flat nested set with the strings instead of the carefully built-up nested set with import path fragments inherited from dependent targets. @laurentlb @vladmos , is there a way to expose a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry for late reply. We do not plan to expose There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or return a Iterable that lazily iterates a nested set when needed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry for my late reply. I will add it asap. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am sorry for not understanding what you are trying to suggest here. I think (I may be completely wrong) that you are suggesting the proposed change? @dslomov can you comment? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Friendly ping @dslomov :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please read https://docs.bazel.build/versions/master/skylark/depsets.html. What you are doing here is flattening the depset of path fragments to convert them to strings.
My preferred approach is (1) but I do not know how expensive it is to keep Strings instead of PathFragments. |
||
} | ||
|
||
return builder.build(); | ||
} | ||
|
||
public PythonVersion getDefaultPythonVersion() { | ||
return ruleContext.getRule() | ||
.isAttrDefined("default_python_version", Type.STRING) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you need
RuleContext
here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't exactly remember as this has been really old. I think I had to add it because rule context in pycommon was different from pylibrary/pybinary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can double check this and let you know.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed it.