blob: c9b7c564e19e16ad234a0a655a574bb681719050 [file] [log] [blame]
zhangkun9de8e4b2015-01-15 10:29:05 -08001apply plugin: "cpp"
Kun Zhang41940f72015-04-28 18:14:34 -07002apply plugin: "com.google.protobuf"
zhangkun9de8e4b2015-01-15 10:29:05 -08003
zhangkun835e607852015-01-22 12:31:56 -08004description = 'The protoc plugin for gRPC Java'
5
Eric Andersonfb28ad22015-01-29 15:00:58 -08006buildscript {
7 repositories {
8 mavenCentral()
zhangkun83da3c3f82015-03-11 18:03:31 -07009 mavenLocal()
Eric Andersonfb28ad22015-01-29 15:00:58 -080010 }
11 dependencies {
Kun Zhang4bc2d6d2015-04-17 10:49:11 -070012 classpath libraries.protobuf_plugin
Eric Andersonfb28ad22015-01-29 15:00:58 -080013 }
14}
15
zhangkun83da3c3f82015-03-11 18:03:31 -070016def artifactStagingPath = "$buildDir/artifacts" as File
zhangkun83da3c3f82015-03-11 18:03:31 -070017// Adds space-delimited arguments from the environment variable env to the
18// argList.
19def addEnvArgs = { env, argList ->
20 def value = System.getenv(env)
21 if (value != null) {
22 value.split(' +').each() { it -> argList.add(it) }
23 }
24}
25
Kun Zhang0c2ea152015-04-17 12:40:42 -070026// Adds corresponding "-l" option to the argList if libName is not found in
27// LDFLAGS. This is only used for Mac because when building for uploadArchives
28// artifacts, we add the ".a" files directly to LDFLAGS and without "-l" in
29// order to get statically linked, otherwise we add the libraries through "-l"
30// so that they can be searched for in default search paths.
31def addLibraryIfNotLinked = { libName, argList ->
32 def ldflags = System.env.LDFLAGS
33 if (ldflags == null || !ldflags.contains('lib' + libName + '.a')) {
34 argList.add('-l' + libName)
35 }
36}
37
Kun Zhang2f749712015-05-06 13:10:28 -070038def String arch = rootProject.hasProperty('targetArch') ? rootProject.targetArch : osdetector.arch
39def boolean vcDisable = rootProject.hasProperty('vcDisable') ? rootProject.vcDisable : false
Kun Zhang221c5342015-04-29 18:16:15 -070040
zhangkun83da3c3f82015-03-11 18:03:31 -070041model {
42 toolChains {
Kun Zhang221c5342015-04-29 18:16:15 -070043 // If you have both VC and Gcc installed, VC will be selected, unless you
Kun Zhang2f749712015-05-06 13:10:28 -070044 // set 'vcDisable=true'
45 if (!vcDisable) {
Kun Zhang221c5342015-04-29 18:16:15 -070046 visualCpp(VisualCpp) {
47 }
48 }
zhangkun83da3c3f82015-03-11 18:03:31 -070049 gcc(Gcc) {
zhangkun83da3c3f82015-03-11 18:03:31 -070050 }
51 clang(Clang) {
zhangkun83da3c3f82015-03-11 18:03:31 -070052 }
53 }
Kun Zhang221c5342015-04-29 18:16:15 -070054
zhangkun83da3c3f82015-03-11 18:03:31 -070055 platforms {
56 x86_32 {
Kun Zhang221c5342015-04-29 18:16:15 -070057 architecture "x86"
zhangkun83da3c3f82015-03-11 18:03:31 -070058 }
59 x86_64 {
60 architecture "x86_64"
61 }
zhangkun83da3c3f82015-03-11 18:03:31 -070062 }
63
64 components {
65 java_plugin(NativeExecutableSpec) {
Kun Zhang221c5342015-04-29 18:16:15 -070066 if (arch in ['x86_32', 'x86_64']) {
67 // If arch is not within the defined platforms, we do not specify the
68 // targetPlatform so that Gradle will choose what is appropriate.
69 targetPlatform arch
zhangkun83da3c3f82015-03-11 18:03:31 -070070 }
71 baseName "$protocPluginBaseName"
72 }
Kun Zhang90706dc2015-04-09 13:11:31 -070073 }
Eric Anderson46ce4092016-01-26 12:13:06 -080074
75 binaries {
76 all {
77 if (toolChain in Gcc || toolChain in Clang) {
Lukasz Strzalkowski363e0f62016-04-11 19:34:15 +020078 cppCompiler.define("GRPC_VERSION", version)
Eric Anderson46ce4092016-01-26 12:13:06 -080079 cppCompiler.args "--std=c++0x"
80 addEnvArgs("CXXFLAGS", cppCompiler.args)
81 addEnvArgs("CPPFLAGS", cppCompiler.args)
82 if (osdetector.os == "osx") {
83 cppCompiler.args "-mmacosx-version-min=10.7", "-stdlib=libc++"
84 addLibraryIfNotLinked('protoc', linker.args)
85 addLibraryIfNotLinked('protobuf', linker.args)
86 } else if (osdetector.os == "windows") {
87 linker.args "-static", "-lprotoc", "-lprotobuf", "-static-libgcc", "-static-libstdc++",
88 "-s"
89 } else {
90 // Link protoc, protobuf, libgcc and libstdc++ statically.
91 // Link other (system) libraries dynamically.
92 // Clang under OSX doesn't support these options.
93 linker.args "-Wl,-Bstatic", "-lprotoc", "-lprotobuf", "-static-libgcc",
94 "-static-libstdc++",
95 "-Wl,-Bdynamic", "-lpthread", "-s"
96 }
97 addEnvArgs("LDFLAGS", linker.args)
98 } else if (toolChain in VisualCpp) {
Lukasz Strzalkowski363e0f62016-04-11 19:34:15 +020099 cppCompiler.define("GRPC_VERSION", version)
Eric Anderson46ce4092016-01-26 12:13:06 -0800100 cppCompiler.args "/EHsc", "/MT"
101 if (rootProject.hasProperty('vcProtobufInclude')) {
102 cppCompiler.args "/I${rootProject.vcProtobufInclude}"
103 }
104 linker.args "libprotobuf.lib", "libprotoc.lib"
105 if (rootProject.hasProperty('vcProtobufLibs')) {
106 linker.args "/LIBPATH:${rootProject.vcProtobufLibs}"
107 }
108 }
109 }
110 }
zhangkun9de8e4b2015-01-15 10:29:05 -0800111}
112
Kun Zhang287a27a2015-05-07 17:32:31 -0700113configurations {
Eric Andersonb22bcdf2016-03-29 14:13:28 -0700114 testLiteCompile
Kun Zhang287a27a2015-05-07 17:32:31 -0700115 testNanoCompile
116}
117
Eric Andersonfb28ad22015-01-29 15:00:58 -0800118dependencies {
Eric Andersone23f8992015-04-10 15:40:44 -0700119 testCompile project(':grpc-protobuf'),
120 project(':grpc-stub')
Eric Andersonb22bcdf2016-03-29 14:13:28 -0700121 testLiteCompile project(':grpc-protobuf-lite'),
122 project(':grpc-stub')
Kun Zhang287a27a2015-05-07 17:32:31 -0700123 testNanoCompile project(':grpc-protobuf-nano'),
124 project(':grpc-stub')
125}
126
127sourceSets {
Eric Andersonb22bcdf2016-03-29 14:13:28 -0700128 testLite {}
Kun Zhang287a27a2015-05-07 17:32:31 -0700129 testNano {
130 proto {
131 setSrcDirs(['src/test/proto'])
Kun Zhang35f77ee2015-06-22 16:29:30 -0700132 }
133 }
134}
135
Eric Andersonb22bcdf2016-03-29 14:13:28 -0700136compileTestLiteJava {
Eric Anderson99a6d8d2016-03-22 11:31:36 -0700137 // Protobuf-generated Lite produces quite a few warnings.
Eric Anderson641cb352016-05-24 14:29:52 -0700138 options.compilerArgs = compileTestJava.options.compilerArgs +
139 ["-Xlint:-unchecked", "-Xlint:-rawtypes"]
140}
141
142compileTestNanoJava {
143 options.compilerArgs = compileTestJava.options.compilerArgs
Eric Anderson99a6d8d2016-03-22 11:31:36 -0700144}
145
Kun Zhang35f77ee2015-06-22 16:29:30 -0700146protobuf {
147 protoc {
148 if (project.hasProperty('protoc')) {
149 path = project.protoc
150 } else {
151 artifact = "com.google.protobuf:protoc:${protobufVersion}"
152 }
153 }
154 plugins {
155 grpc {
156 path = javaPluginPath
157 }
158 }
159 generateProtoTasks {
Eric Anderson7fe8d8b2016-04-19 09:33:23 -0700160 all().each { task ->
161 task.dependsOn 'java_pluginExecutable'
162 task.inputs.file javaPluginPath
163 }
Kun Zhang35f77ee2015-06-22 16:29:30 -0700164 ofSourceSet('test')*.plugins {
165 grpc {}
166 }
Eric Andersonb22bcdf2016-03-29 14:13:28 -0700167 ofSourceSet('testLite')*.plugins {
168 grpc {
169 option 'lite'
170 }
171 }
Kun Zhang35f77ee2015-06-22 16:29:30 -0700172 ofSourceSet('testNano').each { task ->
173 task.builtins {
Kun Zhang287a27a2015-05-07 17:32:31 -0700174 remove java
175 javanano {
176 option 'ignore_services=true'
177 }
178 }
Kun Zhang35f77ee2015-06-22 16:29:30 -0700179 task.plugins {
Kun Zhang287a27a2015-05-07 17:32:31 -0700180 grpc {
Eric Anderson0a01b3c2016-03-29 16:16:21 -0700181 option 'nano'
Kun Zhang287a27a2015-05-07 17:32:31 -0700182 }
183 }
184 }
185 }
186}
187
188checkstyleTestNano {
189 source = fileTree(dir: "src/testNano", include: "**/*.java")
Eric Andersonfb28ad22015-01-29 15:00:58 -0800190}
191
Kun Zhangbc347e32015-09-01 15:26:48 -0700192println "*** Building codegen requires Protobuf version ${protobufVersion}"
193println "*** Please refer to https://github.com/grpc/grpc-java/blob/master/COMPILING.md#how-to-build-code-generation-plugin"
194
zhangkun83da3c3f82015-03-11 18:03:31 -0700195task buildArtifacts(type: Copy) {
Kun Zhang221c5342015-04-29 18:16:15 -0700196 dependsOn 'java_pluginExecutable'
Kun Zhang29784d12016-01-26 15:30:35 -0800197 from("$buildDir/exe") {
zhangkun83da3c3f82015-03-11 18:03:31 -0700198 if (osdetector.os != 'windows') {
199 rename 'protoc-gen-grpc-java', '$0.exe'
200 }
201 }
202 into artifactStagingPath
203}
204
205archivesBaseName = "$protocPluginBaseName"
206
207artifacts {
Kun Zhang29784d12016-01-26 15:30:35 -0800208 archives("$artifactStagingPath/java_plugin/${protocPluginBaseName}.exe" as File) {
Kun Zhang221c5342015-04-29 18:16:15 -0700209 classifier osdetector.os + "-" + arch
210 type "exe"
211 extension "exe"
212 builtBy buildArtifacts
zhangkun83da3c3f82015-03-11 18:03:31 -0700213 }
214}
215
216// Exe files are skipped by Maven by default. Override it.
217// Also skip jar files that is generated by the java plugin.
218[
219 install.repositories.mavenInstaller,
220 uploadArchives.repositories.mavenDeployer,
Eric Andersonbf429132016-01-29 17:01:33 -0800221]*.setFilter {artifact, file ->
zhangkun83da3c3f82015-03-11 18:03:31 -0700222 ! (file.getName().endsWith('jar') || file.getName().endsWith('jar.asc'))
223}
224
225[
zhangkun83da3c3f82015-03-11 18:03:31 -0700226 uploadArchives.repositories.mavenDeployer,
227]*.beforeDeployment {
Kun Zhang221c5342015-04-29 18:16:15 -0700228 def ret = exec {
229 executable 'bash'
230 args 'check-artifact.sh', osdetector.os, arch
231 }
232 if (ret.exitValue != 0) {
233 throw new GradleException("check-artifact.sh exited with " + ret.exitValue)
zhangkun83da3c3f82015-03-11 18:03:31 -0700234 }
235}
236
Eric Andersonbf429132016-01-29 17:01:33 -0800237[
238 install.repositories.mavenInstaller,
239 uploadArchives.repositories.mavenDeployer,
240]*.pom*.whenConfigured { pom ->
241 pom.project {
242 // This isn't any sort of Java archive artifact, and OSSRH doesn't enforce
243 // javadoc for 'pom' packages. 'exe' would be a more appropriate packaging
244 // value, but it isn't clear how that will be interpreted. In addition,
245 // 'pom' is typically the value used when building an exe with Maven.
246 packaging = "pom"
247 }
248}
249
Eric Andersonb22bcdf2016-03-29 14:13:28 -0700250def configureTestTask(Task task, String dep, String extraPackage) {
251 test.dependsOn task
Eric Anderson99a6d8d2016-03-22 11:31:36 -0700252 task.dependsOn "generateTest${dep}Proto"
Kun Zhang4bc2d6d2015-04-17 10:49:11 -0700253 if (osdetector.os != 'windows') {
Kun Zhang287a27a2015-05-07 17:32:31 -0700254 task.executable "diff"
Eric Andersonb938ba52015-02-28 09:59:25 -0800255 } else {
Kun Zhang287a27a2015-05-07 17:32:31 -0700256 task.executable "fc"
Eric Andersonb938ba52015-02-28 09:59:25 -0800257 }
258 // File isn't found on Windows if last slash is forward-slash
259 def slash = System.getProperty("file.separator")
Eric Anderson99a6d8d2016-03-22 11:31:36 -0700260 task.args "$buildDir/generated/source/proto/test${dep}/grpc/io/grpc/testing/integration${extraPackage}${slash}TestServiceGrpc.java",
Eric Andersonb22bcdf2016-03-29 14:13:28 -0700261 "$projectDir/src/test${dep}/golden/TestService.java.txt"
zhangkun835e607852015-01-22 12:31:56 -0800262}
Xiao Hangdcff3152015-02-20 15:03:06 -0800263
Kun Zhang287a27a2015-05-07 17:32:31 -0700264task testGolden(type: Exec)
Eric Anderson99a6d8d2016-03-22 11:31:36 -0700265task testLiteGolden(type: Exec)
Kun Zhang287a27a2015-05-07 17:32:31 -0700266task testNanoGolden(type: Exec)
Eric Andersonb22bcdf2016-03-29 14:13:28 -0700267configureTestTask(testGolden, '', '')
268configureTestTask(testLiteGolden, 'Lite', '')
269configureTestTask(testNanoGolden, 'Nano', '/nano')