Skip to content
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

Link only generated Java and Kotlin to compilation task by default #375

Merged
merged 6 commits into from
Sep 30, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ files (``*.proto``) in your project. There are two pieces of its job:
2. It adds the generated Java source files to the input of the corresponding
Java compilation unit (_sourceSet_ in a Java project; _variant_ in an
Android project), so that they can be compiled along with your Java sources.
- Note if you are generating non-Java/Kotlin source files, they will not be
included for compilation automatically, you will need to add them to sources
for language-specific compilations. See details in [this section](#default-outputs).
voidzcy marked this conversation as resolved.
Show resolved Hide resolved

For more information about the Protobuf Compiler, please refer to
[Google Developers Site](https://developers.google.com/protocol-buffers/docs/reference/java-generated?csw=1).
Expand Down Expand Up @@ -330,6 +333,8 @@ protobuf {
}
```

Note the generated Python code will not be included for compilation, you will
need to add them as sources to Python's compilation tasks manually.
See [this section](#change-where-files-are-generated) for details about where the code will be generated.


Expand Down
17 changes: 2 additions & 15 deletions src/main/groovy/com/google/protobuf/gradle/ProtobufPlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ import org.gradle.api.attributes.LibraryElements
import org.gradle.api.file.FileCollection
import org.gradle.api.file.SourceDirectorySet
import org.gradle.api.internal.file.FileResolver
import org.gradle.api.logging.LogLevel
import org.gradle.api.plugins.AppliedPlugin
import org.gradle.api.tasks.SourceSet

Expand All @@ -65,7 +64,6 @@ class ProtobufPlugin implements Plugin<Project> {
'android-library',
]

private static final String USER_LANG_PROP = 'protobufGradlePluginAdditionalLanguages'
private static final List<String> SUPPORTED_LANGUAGES = [
'java',
'kotlin',
Expand Down Expand Up @@ -115,20 +113,9 @@ class ProtobufPlugin implements Plugin<Project> {
}
}

private static List<String> getLanguages(Project project) {
List<String> additionalLanguages = []
if (project.hasProperty(USER_LANG_PROP)) {
additionalLanguages = (List<String>) project.property(USER_LANG_PROP)
project.logger.log(
LogLevel.WARN,
"protobuf plugin is now using additional unsupported languages: " + additionalLanguages)
}
return SUPPORTED_LANGUAGES + additionalLanguages
}

private static void linkGenerateProtoTasksToTask(Task task, GenerateProtoTask genProtoTask) {
task.dependsOn(genProtoTask)
task.source genProtoTask.getOutputSourceDirectorySet()
task.source genProtoTask.getOutputSourceDirectorySet().include("**/*.java", "**/*.kt")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this fix the "no source files" error?

Copy link
Collaborator Author

@voidzcy voidzcy Jul 15, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applying the Java/Kotlin filter can avoid feeding non-Java/Kotlin source files to Java compilation task, which avoids task input change that triggers the task to run. If there are no Java/Kotlin source files, Java compilation tasks should not run (you should be able to see something like compileJava NO-SOURCE).

Copy link
Collaborator

@ejona86 ejona86 Jul 15, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We put each language in its own folder. Why don't we put each language in its own SourceDirectorySet?

Copy link
Collaborator Author

@voidzcy voidzcy Jul 15, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each SourceSet can have multiple compile tasks as for different languages (see SourceSet's getComileTaskName(lang) API). However, each SourceSet has only one GenerateProtoTask, which generates code for all languages configured. So for each SourceSet, we are hooking the output of its GenerateProtoTask as the input to each of its compile tasks (for each language). The output of a GenerateProtoTask is a single SourceDirectorySet that contains generated code for plugin/builtin in separated directories. We are not able to map plugins/builtins to individual languages as they do not contain language information.

}

private void doApply() {
Expand Down Expand Up @@ -469,7 +456,7 @@ class ProtobufPlugin implements Plugin<Project> {
} else {
project.sourceSets.each { SourceSet sourceSet ->
project.protobuf.generateProtoTasks.ofSourceSet(sourceSet.name).each { GenerateProtoTask genProtoTask ->
getLanguages(project).each { String lang ->
SUPPORTED_LANGUAGES.each { String lang ->
linkGenerateProtoTasksToTaskName(sourceSet.getCompileTaskName(lang), genProtoTask)
}
}
Expand Down