blob: a8a8423d0ea1dc6d10956a41ca5f23d8a96ddbf8 [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) {
Nishidha862157a2016-10-26 03:48:10 +053050 target("ppcle_64")
zhangkun83da3c3f82015-03-11 18:03:31 -070051 }
52 clang(Clang) {
zhangkun83da3c3f82015-03-11 18:03:31 -070053 }
54 }
Kun Zhang221c5342015-04-29 18:16:15 -070055
zhangkun83da3c3f82015-03-11 18:03:31 -070056 platforms {
57 x86_32 {
Kun Zhang221c5342015-04-29 18:16:15 -070058 architecture "x86"
zhangkun83da3c3f82015-03-11 18:03:31 -070059 }
60 x86_64 {
61 architecture "x86_64"
62 }
Nishidha862157a2016-10-26 03:48:10 +053063 ppcle_64 {
64 architecture "ppcle_64"
65 }
zhangkun83da3c3f82015-03-11 18:03:31 -070066 }
67
68 components {
69 java_plugin(NativeExecutableSpec) {
Nishidha862157a2016-10-26 03:48:10 +053070 if (arch in ['x86_32', 'x86_64', 'ppcle_64']) {
Kun Zhang221c5342015-04-29 18:16:15 -070071 // If arch is not within the defined platforms, we do not specify the
72 // targetPlatform so that Gradle will choose what is appropriate.
73 targetPlatform arch
zhangkun83da3c3f82015-03-11 18:03:31 -070074 }
75 baseName "$protocPluginBaseName"
76 }
Kun Zhang90706dc2015-04-09 13:11:31 -070077 }
Eric Anderson46ce4092016-01-26 12:13:06 -080078
79 binaries {
80 all {
81 if (toolChain in Gcc || toolChain in Clang) {
Lukasz Strzalkowski363e0f62016-04-11 19:34:15 +020082 cppCompiler.define("GRPC_VERSION", version)
Eric Anderson46ce4092016-01-26 12:13:06 -080083 cppCompiler.args "--std=c++0x"
84 addEnvArgs("CXXFLAGS", cppCompiler.args)
85 addEnvArgs("CPPFLAGS", cppCompiler.args)
86 if (osdetector.os == "osx") {
87 cppCompiler.args "-mmacosx-version-min=10.7", "-stdlib=libc++"
88 addLibraryIfNotLinked('protoc', linker.args)
89 addLibraryIfNotLinked('protobuf', linker.args)
90 } else if (osdetector.os == "windows") {
91 linker.args "-static", "-lprotoc", "-lprotobuf", "-static-libgcc", "-static-libstdc++",
92 "-s"
93 } else {
94 // Link protoc, protobuf, libgcc and libstdc++ statically.
95 // Link other (system) libraries dynamically.
96 // Clang under OSX doesn't support these options.
97 linker.args "-Wl,-Bstatic", "-lprotoc", "-lprotobuf", "-static-libgcc",
98 "-static-libstdc++",
99 "-Wl,-Bdynamic", "-lpthread", "-s"
100 }
101 addEnvArgs("LDFLAGS", linker.args)
102 } else if (toolChain in VisualCpp) {
Lukasz Strzalkowski363e0f62016-04-11 19:34:15 +0200103 cppCompiler.define("GRPC_VERSION", version)
Eric Anderson46ce4092016-01-26 12:13:06 -0800104 cppCompiler.args "/EHsc", "/MT"
105 if (rootProject.hasProperty('vcProtobufInclude')) {
106 cppCompiler.args "/I${rootProject.vcProtobufInclude}"
107 }
108 linker.args "libprotobuf.lib", "libprotoc.lib"
109 if (rootProject.hasProperty('vcProtobufLibs')) {
110 linker.args "/LIBPATH:${rootProject.vcProtobufLibs}"
111 }
112 }
113 }
114 }
zhangkun9de8e4b2015-01-15 10:29:05 -0800115}
116
Kun Zhang287a27a2015-05-07 17:32:31 -0700117configurations {
Eric Andersonb22bcdf2016-03-29 14:13:28 -0700118 testLiteCompile
Kun Zhang287a27a2015-05-07 17:32:31 -0700119 testNanoCompile
120}
121
Eric Andersonfb28ad22015-01-29 15:00:58 -0800122dependencies {
Eric Andersone23f8992015-04-10 15:40:44 -0700123 testCompile project(':grpc-protobuf'),
124 project(':grpc-stub')
Eric Andersonb22bcdf2016-03-29 14:13:28 -0700125 testLiteCompile project(':grpc-protobuf-lite'),
126 project(':grpc-stub')
Kun Zhang287a27a2015-05-07 17:32:31 -0700127 testNanoCompile project(':grpc-protobuf-nano'),
128 project(':grpc-stub')
129}
130
131sourceSets {
Eric Andersondbef1af2016-05-23 12:05:41 -0700132 testLite {
133 proto {
134 setSrcDirs(['src/test/proto'])
135 }
136 }
Kun Zhang287a27a2015-05-07 17:32:31 -0700137 testNano {
138 proto {
139 setSrcDirs(['src/test/proto'])
Kun Zhang35f77ee2015-06-22 16:29:30 -0700140 }
141 }
142}
143
Eric Anderson5e1e8832016-07-25 12:42:59 -0700144compileTestJava {
Eric Anderson675080b2017-02-24 14:53:23 -0800145 options.compilerArgs += ["-Xlint:-cast", "-Xep:MissingOverride:OFF",
146 "-Xep:ReferenceEquality:OFF", "-Xep:FunctionalInterfaceClash:OFF"]
Eric Anderson5e1e8832016-07-25 12:42:59 -0700147}
148
Eric Andersonb22bcdf2016-03-29 14:13:28 -0700149compileTestLiteJava {
Eric Anderson99a6d8d2016-03-22 11:31:36 -0700150 // Protobuf-generated Lite produces quite a few warnings.
Eric Anderson675080b2017-02-24 14:53:23 -0800151 options.compilerArgs += ["-Xlint:-rawtypes", "-Xlint:-unchecked",
152 "-Xep:MissingOverride:OFF", "-Xep:ReferenceEquality:OFF"]
Eric Anderson641cb352016-05-24 14:29:52 -0700153}
154
155compileTestNanoJava {
156 options.compilerArgs = compileTestJava.options.compilerArgs
Eric Anderson99a6d8d2016-03-22 11:31:36 -0700157}
158
Kun Zhang35f77ee2015-06-22 16:29:30 -0700159protobuf {
160 protoc {
161 if (project.hasProperty('protoc')) {
162 path = project.protoc
163 } else {
164 artifact = "com.google.protobuf:protoc:${protobufVersion}"
165 }
166 }
167 plugins {
Eric Anderson5e1e8832016-07-25 12:42:59 -0700168 javalite {
Eric Andersona8700a72016-07-29 11:57:41 -0700169 if (project.hasProperty('protoc-gen-javalite')) {
170 path = project['protoc-gen-javalite']
171 } else {
Eric Andersonb1d72e52016-09-27 17:15:12 -0700172 artifact = libraries.protoc_lite
Eric Andersona8700a72016-07-29 11:57:41 -0700173 }
Eric Anderson5e1e8832016-07-25 12:42:59 -0700174 }
Kun Zhang35f77ee2015-06-22 16:29:30 -0700175 grpc {
176 path = javaPluginPath
177 }
178 }
179 generateProtoTasks {
Eric Anderson7fe8d8b2016-04-19 09:33:23 -0700180 all().each { task ->
181 task.dependsOn 'java_pluginExecutable'
182 task.inputs.file javaPluginPath
183 }
Kun Zhang35f77ee2015-06-22 16:29:30 -0700184 ofSourceSet('test')*.plugins {
185 grpc {}
186 }
Eric Anderson5e1e8832016-07-25 12:42:59 -0700187 ofSourceSet('testLite')*.each { task ->
188 task.builtins {
189 remove java
190 }
191 task.plugins {
192 javalite {}
193 grpc {
194 option 'lite'
195 }
Eric Andersonb22bcdf2016-03-29 14:13:28 -0700196 }
197 }
Kun Zhang35f77ee2015-06-22 16:29:30 -0700198 ofSourceSet('testNano').each { task ->
199 task.builtins {
Kun Zhang287a27a2015-05-07 17:32:31 -0700200 remove java
201 javanano {
202 option 'ignore_services=true'
203 }
204 }
Kun Zhang35f77ee2015-06-22 16:29:30 -0700205 task.plugins {
Kun Zhang287a27a2015-05-07 17:32:31 -0700206 grpc {
Eric Anderson0a01b3c2016-03-29 16:16:21 -0700207 option 'nano'
Kun Zhang287a27a2015-05-07 17:32:31 -0700208 }
209 }
210 }
211 }
212}
213
214checkstyleTestNano {
215 source = fileTree(dir: "src/testNano", include: "**/*.java")
Eric Andersonfb28ad22015-01-29 15:00:58 -0800216}
217
Kun Zhangbc347e32015-09-01 15:26:48 -0700218println "*** Building codegen requires Protobuf version ${protobufVersion}"
219println "*** Please refer to https://github.com/grpc/grpc-java/blob/master/COMPILING.md#how-to-build-code-generation-plugin"
220
zhangkun83da3c3f82015-03-11 18:03:31 -0700221task buildArtifacts(type: Copy) {
Kun Zhang221c5342015-04-29 18:16:15 -0700222 dependsOn 'java_pluginExecutable'
Kun Zhang29784d12016-01-26 15:30:35 -0800223 from("$buildDir/exe") {
zhangkun83da3c3f82015-03-11 18:03:31 -0700224 if (osdetector.os != 'windows') {
225 rename 'protoc-gen-grpc-java', '$0.exe'
226 }
227 }
228 into artifactStagingPath
229}
230
231archivesBaseName = "$protocPluginBaseName"
232
233artifacts {
Kun Zhang29784d12016-01-26 15:30:35 -0800234 archives("$artifactStagingPath/java_plugin/${protocPluginBaseName}.exe" as File) {
Kun Zhang221c5342015-04-29 18:16:15 -0700235 classifier osdetector.os + "-" + arch
236 type "exe"
237 extension "exe"
238 builtBy buildArtifacts
zhangkun83da3c3f82015-03-11 18:03:31 -0700239 }
240}
241
242// Exe files are skipped by Maven by default. Override it.
243// Also skip jar files that is generated by the java plugin.
244[
245 install.repositories.mavenInstaller,
246 uploadArchives.repositories.mavenDeployer,
Eric Andersonbf429132016-01-29 17:01:33 -0800247]*.setFilter {artifact, file ->
zhangkun83da3c3f82015-03-11 18:03:31 -0700248 ! (file.getName().endsWith('jar') || file.getName().endsWith('jar.asc'))
249}
250
251[
zhangkun83da3c3f82015-03-11 18:03:31 -0700252 uploadArchives.repositories.mavenDeployer,
253]*.beforeDeployment {
Kun Zhang221c5342015-04-29 18:16:15 -0700254 def ret = exec {
255 executable 'bash'
256 args 'check-artifact.sh', osdetector.os, arch
257 }
258 if (ret.exitValue != 0) {
259 throw new GradleException("check-artifact.sh exited with " + ret.exitValue)
zhangkun83da3c3f82015-03-11 18:03:31 -0700260 }
261}
262
Eric Andersonbf429132016-01-29 17:01:33 -0800263[
264 install.repositories.mavenInstaller,
265 uploadArchives.repositories.mavenDeployer,
266]*.pom*.whenConfigured { pom ->
267 pom.project {
268 // This isn't any sort of Java archive artifact, and OSSRH doesn't enforce
269 // javadoc for 'pom' packages. 'exe' would be a more appropriate packaging
270 // value, but it isn't clear how that will be interpreted. In addition,
271 // 'pom' is typically the value used when building an exe with Maven.
272 packaging = "pom"
273 }
274}
275
Eric Andersonb22bcdf2016-03-29 14:13:28 -0700276def configureTestTask(Task task, String dep, String extraPackage) {
277 test.dependsOn task
Eric Anderson99a6d8d2016-03-22 11:31:36 -0700278 task.dependsOn "generateTest${dep}Proto"
Kun Zhang4bc2d6d2015-04-17 10:49:11 -0700279 if (osdetector.os != 'windows') {
Kun Zhang287a27a2015-05-07 17:32:31 -0700280 task.executable "diff"
Eric Andersonb938ba52015-02-28 09:59:25 -0800281 } else {
Kun Zhang287a27a2015-05-07 17:32:31 -0700282 task.executable "fc"
Eric Andersonb938ba52015-02-28 09:59:25 -0800283 }
284 // File isn't found on Windows if last slash is forward-slash
285 def slash = System.getProperty("file.separator")
Eric Anderson99a6d8d2016-03-22 11:31:36 -0700286 task.args "$buildDir/generated/source/proto/test${dep}/grpc/io/grpc/testing/integration${extraPackage}${slash}TestServiceGrpc.java",
Eric Andersonb22bcdf2016-03-29 14:13:28 -0700287 "$projectDir/src/test${dep}/golden/TestService.java.txt"
zhangkun835e607852015-01-22 12:31:56 -0800288}
Xiao Hangdcff3152015-02-20 15:03:06 -0800289
Kun Zhang287a27a2015-05-07 17:32:31 -0700290task testGolden(type: Exec)
Eric Anderson99a6d8d2016-03-22 11:31:36 -0700291task testLiteGolden(type: Exec)
Kun Zhang287a27a2015-05-07 17:32:31 -0700292task testNanoGolden(type: Exec)
Eric Andersonb22bcdf2016-03-29 14:13:28 -0700293configureTestTask(testGolden, '', '')
294configureTestTask(testLiteGolden, 'Lite', '')
295configureTestTask(testNanoGolden, 'Nano', '/nano')