zhangkun | 9de8e4b | 2015-01-15 10:29:05 -0800 | [diff] [blame] | 1 | apply plugin: "cpp" |
Kun Zhang | 41940f7 | 2015-04-28 18:14:34 -0700 | [diff] [blame] | 2 | apply plugin: "com.google.protobuf" |
zhangkun | 9de8e4b | 2015-01-15 10:29:05 -0800 | [diff] [blame] | 3 | |
zhangkun83 | 5e60785 | 2015-01-22 12:31:56 -0800 | [diff] [blame] | 4 | description = 'The protoc plugin for gRPC Java' |
| 5 | |
Eric Anderson | fb28ad2 | 2015-01-29 15:00:58 -0800 | [diff] [blame] | 6 | buildscript { |
| 7 | repositories { |
| 8 | mavenCentral() |
zhangkun83 | da3c3f8 | 2015-03-11 18:03:31 -0700 | [diff] [blame] | 9 | mavenLocal() |
Eric Anderson | fb28ad2 | 2015-01-29 15:00:58 -0800 | [diff] [blame] | 10 | } |
| 11 | dependencies { |
Kun Zhang | 4bc2d6d | 2015-04-17 10:49:11 -0700 | [diff] [blame] | 12 | classpath libraries.protobuf_plugin |
Eric Anderson | fb28ad2 | 2015-01-29 15:00:58 -0800 | [diff] [blame] | 13 | } |
| 14 | } |
| 15 | |
zhangkun83 | da3c3f8 | 2015-03-11 18:03:31 -0700 | [diff] [blame] | 16 | def artifactStagingPath = "$buildDir/artifacts" as File |
zhangkun83 | da3c3f8 | 2015-03-11 18:03:31 -0700 | [diff] [blame] | 17 | // Adds space-delimited arguments from the environment variable env to the |
| 18 | // argList. |
| 19 | def 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 Zhang | 0c2ea15 | 2015-04-17 12:40:42 -0700 | [diff] [blame] | 26 | // 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. |
| 31 | def 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 Zhang | 2f74971 | 2015-05-06 13:10:28 -0700 | [diff] [blame] | 38 | def String arch = rootProject.hasProperty('targetArch') ? rootProject.targetArch : osdetector.arch |
| 39 | def boolean vcDisable = rootProject.hasProperty('vcDisable') ? rootProject.vcDisable : false |
Kun Zhang | 221c534 | 2015-04-29 18:16:15 -0700 | [diff] [blame] | 40 | |
zhangkun83 | da3c3f8 | 2015-03-11 18:03:31 -0700 | [diff] [blame] | 41 | model { |
| 42 | toolChains { |
Kun Zhang | 221c534 | 2015-04-29 18:16:15 -0700 | [diff] [blame] | 43 | // If you have both VC and Gcc installed, VC will be selected, unless you |
Kun Zhang | 2f74971 | 2015-05-06 13:10:28 -0700 | [diff] [blame] | 44 | // set 'vcDisable=true' |
| 45 | if (!vcDisable) { |
Kun Zhang | 221c534 | 2015-04-29 18:16:15 -0700 | [diff] [blame] | 46 | visualCpp(VisualCpp) { |
| 47 | } |
| 48 | } |
zhangkun83 | da3c3f8 | 2015-03-11 18:03:31 -0700 | [diff] [blame] | 49 | gcc(Gcc) { |
Nishidha | 862157a | 2016-10-26 03:48:10 +0530 | [diff] [blame] | 50 | target("ppcle_64") |
zhangkun83 | da3c3f8 | 2015-03-11 18:03:31 -0700 | [diff] [blame] | 51 | } |
| 52 | clang(Clang) { |
zhangkun83 | da3c3f8 | 2015-03-11 18:03:31 -0700 | [diff] [blame] | 53 | } |
| 54 | } |
Kun Zhang | 221c534 | 2015-04-29 18:16:15 -0700 | [diff] [blame] | 55 | |
zhangkun83 | da3c3f8 | 2015-03-11 18:03:31 -0700 | [diff] [blame] | 56 | platforms { |
| 57 | x86_32 { |
Kun Zhang | 221c534 | 2015-04-29 18:16:15 -0700 | [diff] [blame] | 58 | architecture "x86" |
zhangkun83 | da3c3f8 | 2015-03-11 18:03:31 -0700 | [diff] [blame] | 59 | } |
| 60 | x86_64 { |
| 61 | architecture "x86_64" |
| 62 | } |
Nishidha | 862157a | 2016-10-26 03:48:10 +0530 | [diff] [blame] | 63 | ppcle_64 { |
| 64 | architecture "ppcle_64" |
| 65 | } |
zhangkun83 | da3c3f8 | 2015-03-11 18:03:31 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | components { |
| 69 | java_plugin(NativeExecutableSpec) { |
Nishidha | 862157a | 2016-10-26 03:48:10 +0530 | [diff] [blame] | 70 | if (arch in ['x86_32', 'x86_64', 'ppcle_64']) { |
Kun Zhang | 221c534 | 2015-04-29 18:16:15 -0700 | [diff] [blame] | 71 | // 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 |
zhangkun83 | da3c3f8 | 2015-03-11 18:03:31 -0700 | [diff] [blame] | 74 | } |
| 75 | baseName "$protocPluginBaseName" |
| 76 | } |
Kun Zhang | 90706dc | 2015-04-09 13:11:31 -0700 | [diff] [blame] | 77 | } |
Eric Anderson | 46ce409 | 2016-01-26 12:13:06 -0800 | [diff] [blame] | 78 | |
| 79 | binaries { |
| 80 | all { |
| 81 | if (toolChain in Gcc || toolChain in Clang) { |
Lukasz Strzalkowski | 363e0f6 | 2016-04-11 19:34:15 +0200 | [diff] [blame] | 82 | cppCompiler.define("GRPC_VERSION", version) |
Eric Anderson | 46ce409 | 2016-01-26 12:13:06 -0800 | [diff] [blame] | 83 | 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 Strzalkowski | 363e0f6 | 2016-04-11 19:34:15 +0200 | [diff] [blame] | 103 | cppCompiler.define("GRPC_VERSION", version) |
Eric Anderson | 46ce409 | 2016-01-26 12:13:06 -0800 | [diff] [blame] | 104 | 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 | } |
zhangkun | 9de8e4b | 2015-01-15 10:29:05 -0800 | [diff] [blame] | 115 | } |
| 116 | |
Kun Zhang | 287a27a | 2015-05-07 17:32:31 -0700 | [diff] [blame] | 117 | configurations { |
Eric Anderson | b22bcdf | 2016-03-29 14:13:28 -0700 | [diff] [blame] | 118 | testLiteCompile |
Kun Zhang | 287a27a | 2015-05-07 17:32:31 -0700 | [diff] [blame] | 119 | testNanoCompile |
| 120 | } |
| 121 | |
Eric Anderson | fb28ad2 | 2015-01-29 15:00:58 -0800 | [diff] [blame] | 122 | dependencies { |
Eric Anderson | e23f899 | 2015-04-10 15:40:44 -0700 | [diff] [blame] | 123 | testCompile project(':grpc-protobuf'), |
| 124 | project(':grpc-stub') |
Eric Anderson | b22bcdf | 2016-03-29 14:13:28 -0700 | [diff] [blame] | 125 | testLiteCompile project(':grpc-protobuf-lite'), |
| 126 | project(':grpc-stub') |
Kun Zhang | 287a27a | 2015-05-07 17:32:31 -0700 | [diff] [blame] | 127 | testNanoCompile project(':grpc-protobuf-nano'), |
| 128 | project(':grpc-stub') |
| 129 | } |
| 130 | |
| 131 | sourceSets { |
Eric Anderson | dbef1af | 2016-05-23 12:05:41 -0700 | [diff] [blame] | 132 | testLite { |
| 133 | proto { |
| 134 | setSrcDirs(['src/test/proto']) |
| 135 | } |
| 136 | } |
Kun Zhang | 287a27a | 2015-05-07 17:32:31 -0700 | [diff] [blame] | 137 | testNano { |
| 138 | proto { |
| 139 | setSrcDirs(['src/test/proto']) |
Kun Zhang | 35f77ee | 2015-06-22 16:29:30 -0700 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
Eric Anderson | 5e1e883 | 2016-07-25 12:42:59 -0700 | [diff] [blame] | 144 | compileTestJava { |
Eric Anderson | 675080b | 2017-02-24 14:53:23 -0800 | [diff] [blame] | 145 | options.compilerArgs += ["-Xlint:-cast", "-Xep:MissingOverride:OFF", |
| 146 | "-Xep:ReferenceEquality:OFF", "-Xep:FunctionalInterfaceClash:OFF"] |
Eric Anderson | 5e1e883 | 2016-07-25 12:42:59 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Eric Anderson | b22bcdf | 2016-03-29 14:13:28 -0700 | [diff] [blame] | 149 | compileTestLiteJava { |
Eric Anderson | 99a6d8d | 2016-03-22 11:31:36 -0700 | [diff] [blame] | 150 | // Protobuf-generated Lite produces quite a few warnings. |
Eric Anderson | 675080b | 2017-02-24 14:53:23 -0800 | [diff] [blame] | 151 | options.compilerArgs += ["-Xlint:-rawtypes", "-Xlint:-unchecked", |
Carl Mastrangelo | ee12cc2 | 2017-03-22 22:09:04 -0700 | [diff] [blame] | 152 | "-Xep:MissingOverride:OFF", "-Xep:ReferenceEquality:OFF", |
| 153 | "-Xep:FallThrough:OFF"] |
Eric Anderson | 641cb35 | 2016-05-24 14:29:52 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | compileTestNanoJava { |
| 157 | options.compilerArgs = compileTestJava.options.compilerArgs |
Eric Anderson | 99a6d8d | 2016-03-22 11:31:36 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Kun Zhang | 35f77ee | 2015-06-22 16:29:30 -0700 | [diff] [blame] | 160 | protobuf { |
| 161 | protoc { |
| 162 | if (project.hasProperty('protoc')) { |
| 163 | path = project.protoc |
| 164 | } else { |
Carl Mastrangelo | 0fe2c5c | 2017-05-23 17:04:51 -0700 | [diff] [blame^] | 165 | artifact = "com.google.protobuf:protoc:${protocVersion}" |
Kun Zhang | 35f77ee | 2015-06-22 16:29:30 -0700 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | plugins { |
Eric Anderson | 5e1e883 | 2016-07-25 12:42:59 -0700 | [diff] [blame] | 169 | javalite { |
Eric Anderson | a8700a7 | 2016-07-29 11:57:41 -0700 | [diff] [blame] | 170 | if (project.hasProperty('protoc-gen-javalite')) { |
| 171 | path = project['protoc-gen-javalite'] |
| 172 | } else { |
Eric Anderson | b1d72e5 | 2016-09-27 17:15:12 -0700 | [diff] [blame] | 173 | artifact = libraries.protoc_lite |
Eric Anderson | a8700a7 | 2016-07-29 11:57:41 -0700 | [diff] [blame] | 174 | } |
Eric Anderson | 5e1e883 | 2016-07-25 12:42:59 -0700 | [diff] [blame] | 175 | } |
Kun Zhang | 35f77ee | 2015-06-22 16:29:30 -0700 | [diff] [blame] | 176 | grpc { |
| 177 | path = javaPluginPath |
| 178 | } |
| 179 | } |
| 180 | generateProtoTasks { |
Eric Anderson | 7fe8d8b | 2016-04-19 09:33:23 -0700 | [diff] [blame] | 181 | all().each { task -> |
| 182 | task.dependsOn 'java_pluginExecutable' |
| 183 | task.inputs.file javaPluginPath |
| 184 | } |
Kun Zhang | 35f77ee | 2015-06-22 16:29:30 -0700 | [diff] [blame] | 185 | ofSourceSet('test')*.plugins { |
| 186 | grpc {} |
| 187 | } |
Eric Anderson | 5e1e883 | 2016-07-25 12:42:59 -0700 | [diff] [blame] | 188 | ofSourceSet('testLite')*.each { task -> |
| 189 | task.builtins { |
| 190 | remove java |
| 191 | } |
| 192 | task.plugins { |
| 193 | javalite {} |
| 194 | grpc { |
| 195 | option 'lite' |
| 196 | } |
Eric Anderson | b22bcdf | 2016-03-29 14:13:28 -0700 | [diff] [blame] | 197 | } |
| 198 | } |
Kun Zhang | 35f77ee | 2015-06-22 16:29:30 -0700 | [diff] [blame] | 199 | ofSourceSet('testNano').each { task -> |
| 200 | task.builtins { |
Kun Zhang | 287a27a | 2015-05-07 17:32:31 -0700 | [diff] [blame] | 201 | remove java |
| 202 | javanano { |
| 203 | option 'ignore_services=true' |
| 204 | } |
| 205 | } |
Kun Zhang | 35f77ee | 2015-06-22 16:29:30 -0700 | [diff] [blame] | 206 | task.plugins { |
Kun Zhang | 287a27a | 2015-05-07 17:32:31 -0700 | [diff] [blame] | 207 | grpc { |
Eric Anderson | 0a01b3c | 2016-03-29 16:16:21 -0700 | [diff] [blame] | 208 | option 'nano' |
Kun Zhang | 287a27a | 2015-05-07 17:32:31 -0700 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | checkstyleTestNano { |
| 216 | source = fileTree(dir: "src/testNano", include: "**/*.java") |
Eric Anderson | fb28ad2 | 2015-01-29 15:00:58 -0800 | [diff] [blame] | 217 | } |
| 218 | |
Carl Mastrangelo | 0fe2c5c | 2017-05-23 17:04:51 -0700 | [diff] [blame^] | 219 | println "*** Building codegen requires Protobuf version ${protocVersion}" |
Kun Zhang | bc347e3 | 2015-09-01 15:26:48 -0700 | [diff] [blame] | 220 | println "*** Please refer to https://github.com/grpc/grpc-java/blob/master/COMPILING.md#how-to-build-code-generation-plugin" |
| 221 | |
zhangkun83 | da3c3f8 | 2015-03-11 18:03:31 -0700 | [diff] [blame] | 222 | task buildArtifacts(type: Copy) { |
Kun Zhang | 221c534 | 2015-04-29 18:16:15 -0700 | [diff] [blame] | 223 | dependsOn 'java_pluginExecutable' |
Kun Zhang | 29784d1 | 2016-01-26 15:30:35 -0800 | [diff] [blame] | 224 | from("$buildDir/exe") { |
zhangkun83 | da3c3f8 | 2015-03-11 18:03:31 -0700 | [diff] [blame] | 225 | if (osdetector.os != 'windows') { |
| 226 | rename 'protoc-gen-grpc-java', '$0.exe' |
| 227 | } |
| 228 | } |
| 229 | into artifactStagingPath |
| 230 | } |
| 231 | |
| 232 | archivesBaseName = "$protocPluginBaseName" |
| 233 | |
| 234 | artifacts { |
Kun Zhang | 29784d1 | 2016-01-26 15:30:35 -0800 | [diff] [blame] | 235 | archives("$artifactStagingPath/java_plugin/${protocPluginBaseName}.exe" as File) { |
Kun Zhang | 221c534 | 2015-04-29 18:16:15 -0700 | [diff] [blame] | 236 | classifier osdetector.os + "-" + arch |
| 237 | type "exe" |
| 238 | extension "exe" |
| 239 | builtBy buildArtifacts |
zhangkun83 | da3c3f8 | 2015-03-11 18:03:31 -0700 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | |
| 243 | // Exe files are skipped by Maven by default. Override it. |
| 244 | // Also skip jar files that is generated by the java plugin. |
| 245 | [ |
| 246 | install.repositories.mavenInstaller, |
| 247 | uploadArchives.repositories.mavenDeployer, |
Eric Anderson | bf42913 | 2016-01-29 17:01:33 -0800 | [diff] [blame] | 248 | ]*.setFilter {artifact, file -> |
zhangkun83 | da3c3f8 | 2015-03-11 18:03:31 -0700 | [diff] [blame] | 249 | ! (file.getName().endsWith('jar') || file.getName().endsWith('jar.asc')) |
| 250 | } |
| 251 | |
| 252 | [ |
zhangkun83 | da3c3f8 | 2015-03-11 18:03:31 -0700 | [diff] [blame] | 253 | uploadArchives.repositories.mavenDeployer, |
| 254 | ]*.beforeDeployment { |
Kun Zhang | 221c534 | 2015-04-29 18:16:15 -0700 | [diff] [blame] | 255 | def ret = exec { |
| 256 | executable 'bash' |
| 257 | args 'check-artifact.sh', osdetector.os, arch |
| 258 | } |
| 259 | if (ret.exitValue != 0) { |
| 260 | throw new GradleException("check-artifact.sh exited with " + ret.exitValue) |
zhangkun83 | da3c3f8 | 2015-03-11 18:03:31 -0700 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | |
Eric Anderson | bf42913 | 2016-01-29 17:01:33 -0800 | [diff] [blame] | 264 | [ |
| 265 | install.repositories.mavenInstaller, |
| 266 | uploadArchives.repositories.mavenDeployer, |
| 267 | ]*.pom*.whenConfigured { pom -> |
| 268 | pom.project { |
| 269 | // This isn't any sort of Java archive artifact, and OSSRH doesn't enforce |
| 270 | // javadoc for 'pom' packages. 'exe' would be a more appropriate packaging |
| 271 | // value, but it isn't clear how that will be interpreted. In addition, |
| 272 | // 'pom' is typically the value used when building an exe with Maven. |
| 273 | packaging = "pom" |
| 274 | } |
| 275 | } |
| 276 | |
Eric Anderson | b22bcdf | 2016-03-29 14:13:28 -0700 | [diff] [blame] | 277 | def configureTestTask(Task task, String dep, String extraPackage) { |
| 278 | test.dependsOn task |
Eric Anderson | 99a6d8d | 2016-03-22 11:31:36 -0700 | [diff] [blame] | 279 | task.dependsOn "generateTest${dep}Proto" |
Kun Zhang | 4bc2d6d | 2015-04-17 10:49:11 -0700 | [diff] [blame] | 280 | if (osdetector.os != 'windows') { |
Kun Zhang | 287a27a | 2015-05-07 17:32:31 -0700 | [diff] [blame] | 281 | task.executable "diff" |
Eric Anderson | b938ba5 | 2015-02-28 09:59:25 -0800 | [diff] [blame] | 282 | } else { |
Kun Zhang | 287a27a | 2015-05-07 17:32:31 -0700 | [diff] [blame] | 283 | task.executable "fc" |
Eric Anderson | b938ba5 | 2015-02-28 09:59:25 -0800 | [diff] [blame] | 284 | } |
| 285 | // File isn't found on Windows if last slash is forward-slash |
| 286 | def slash = System.getProperty("file.separator") |
Eric Anderson | 99a6d8d | 2016-03-22 11:31:36 -0700 | [diff] [blame] | 287 | task.args "$buildDir/generated/source/proto/test${dep}/grpc/io/grpc/testing/integration${extraPackage}${slash}TestServiceGrpc.java", |
Eric Anderson | b22bcdf | 2016-03-29 14:13:28 -0700 | [diff] [blame] | 288 | "$projectDir/src/test${dep}/golden/TestService.java.txt" |
zhangkun83 | 5e60785 | 2015-01-22 12:31:56 -0800 | [diff] [blame] | 289 | } |
Xiao Hang | dcff315 | 2015-02-20 15:03:06 -0800 | [diff] [blame] | 290 | |
Kun Zhang | 287a27a | 2015-05-07 17:32:31 -0700 | [diff] [blame] | 291 | task testGolden(type: Exec) |
Eric Anderson | 99a6d8d | 2016-03-22 11:31:36 -0700 | [diff] [blame] | 292 | task testLiteGolden(type: Exec) |
Kun Zhang | 287a27a | 2015-05-07 17:32:31 -0700 | [diff] [blame] | 293 | task testNanoGolden(type: Exec) |
Eric Anderson | b22bcdf | 2016-03-29 14:13:28 -0700 | [diff] [blame] | 294 | configureTestTask(testGolden, '', '') |
| 295 | configureTestTask(testLiteGolden, 'Lite', '') |
| 296 | configureTestTask(testNanoGolden, 'Nano', '/nano') |