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

support xxx_opt flag on protoc #681

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import groovy.transform.CompileStatic
import groovy.transform.PackageScope
import org.gradle.api.Named
import org.gradle.api.file.FileCollection
import org.gradle.util.VersionNumber

/**
* Locates an executable that can either be found locally or downloaded from
Expand All @@ -50,6 +51,8 @@ class ExecutableLocator implements Named {
private FileCollection artifactFiles
private String simplifiedArtifactName

private VersionNumber version = VersionNumber.UNKNOWN

ExecutableLocator(String name) {
this.name = name
}
Expand All @@ -76,6 +79,10 @@ class ExecutableLocator implements Named {
this.artifact = null
}

void setVersion(VersionNumber version) {
this.version = version
}

String getArtifact() {
return artifact
}
Expand All @@ -84,6 +91,10 @@ class ExecutableLocator implements Named {
return path
}

VersionNumber getVersion() {
return version
}

@PackageScope
FileCollection getArtifactFiles() {
Preconditions.checkState(path == null, 'Not artifact based')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import org.gradle.api.tasks.PathSensitivity
import org.gradle.api.tasks.SkipWhenEmpty
import org.gradle.api.tasks.SourceSet
import org.gradle.api.tasks.TaskAction
import org.gradle.util.VersionNumber

import javax.annotation.Nullable
import javax.inject.Inject
Expand Down Expand Up @@ -165,7 +166,7 @@ public abstract class GenerateProtoTask extends DefaultTask {
// - Without options: --java_out=/path/to/output
// - With options: --java_out=option1,option2:/path/to/output
// This method generates the prefix out of the given options.
static String makeOptionsPrefix(List<String> options) {
static String assembleOptions(List<String> options) {
StringBuilder prefix = new StringBuilder()
if (!options.isEmpty()) {
options.each { option ->
Expand All @@ -174,7 +175,6 @@ public abstract class GenerateProtoTask extends DefaultTask {
}
prefix.append(option)
}
prefix.append(':')
}
return prefix.toString()
}
Expand Down Expand Up @@ -622,8 +622,16 @@ public abstract class GenerateProtoTask extends DefaultTask {

// Handle code generation built-ins
builtins.each { builtin ->
String outPrefix = makeOptionsPrefix(builtin.options)
baseCmd += "--${builtin.name}_out=${outPrefix}${getOutputDir(builtin)}".toString()
String options = assembleOptions(builtin.options)
if (toolsLocator.protoc.version == VersionNumber.UNKNOWN ||
VersionNumber.parse("3.2.0") >= toolsLocator.protoc.version) {
if (!options.isBlank())
options += ":"
baseCmd += "--${name}_out=${options}${getOutputDir(builtin)}".toString()
} else {
baseCmd += "--${builtin.name}_out=${getOutputDir(builtin)}".toString()
baseCmd += "--${builtin.name}_opt=$options".toString()
}
}

Map<String, ExecutableLocator> executableLocations = toolsLocator.plugins.asMap
Expand All @@ -636,8 +644,16 @@ public abstract class GenerateProtoTask extends DefaultTask {
} else {
logger.warn "protoc plugin '${name}' not defined. Trying to use 'protoc-gen-${name}' from system path"
}
String pluginOutPrefix = makeOptionsPrefix(plugin.options)
baseCmd += "--${name}_out=${pluginOutPrefix}${getOutputDir(plugin)}".toString()
String options = assembleOptions(plugin.options)
if (toolsLocator.protoc.version == VersionNumber.UNKNOWN ||
VersionNumber.parse("3.2.0") >= toolsLocator.protoc.version) {
if (!options.isBlank())
options += ":"
baseCmd += "--${name}_out=${options}${getOutputDir(plugin)}".toString()
} else {
baseCmd += "--${name}_out=${getOutputDir(plugin)}".toString()
baseCmd += "--${name}_opt=$options".toString()
}
}

if (generateDescriptorSet) {
Expand Down
18 changes: 18 additions & 0 deletions src/main/groovy/com/google/protobuf/gradle/ToolsLocator.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ import org.gradle.api.NamedDomainObjectContainer
import org.gradle.api.Project
import org.gradle.api.artifacts.Configuration
import org.gradle.api.artifacts.Dependency
import org.gradle.util.VersionNumber

import java.nio.file.Files
import java.nio.file.Paths

/**
* Holds locations of all external executables, i.e., protoc and plugins.
Expand Down Expand Up @@ -82,6 +86,7 @@ class ToolsLocator {
} else if (protoc.path == null) {
protoc.path = 'protoc'
}
protoc.version = determineProtocVersion(protoc)
for (ExecutableLocator pluginLocator in plugins) {
if (pluginLocator.artifact != null) {
resolveLocator(project, pluginLocator)
Expand Down Expand Up @@ -111,4 +116,17 @@ class ToolsLocator {
Dependency dep = project.dependencies.add(config.name, notation)
locator.resolve(config.fileCollection(dep), "$groupId:$artifact:$version".toString())
}

private VersionNumber determineProtocVersion(ExecutableLocator executableLocator) {
if (executableLocator.artifact != null) {
String[] parts = executableLocator.simplifiedArtifactName.split(":")
return VersionNumber.parse(parts[2])
} else if (executableLocator.path != null && Files.isExecutable(Paths.get(executableLocator.path))) {
Process process = new ProcessBuilder(executableLocator.path, "--version").start()
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))
String version = reader.readLine().replace("libprotoc ", "")
return VersionNumber.parse(version)
}
return VersionNumber.UNKNOWN
}
}