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) { |
zhangkun83 | da3c3f8 | 2015-03-11 18:03:31 -0700 | [diff] [blame] | 50 | } |
| 51 | clang(Clang) { |
zhangkun83 | da3c3f8 | 2015-03-11 18:03:31 -0700 | [diff] [blame] | 52 | } |
| 53 | } |
Kun Zhang | 221c534 | 2015-04-29 18:16:15 -0700 | [diff] [blame] | 54 | |
zhangkun83 | da3c3f8 | 2015-03-11 18:03:31 -0700 | [diff] [blame] | 55 | platforms { |
| 56 | x86_32 { |
Kun Zhang | 221c534 | 2015-04-29 18:16:15 -0700 | [diff] [blame] | 57 | architecture "x86" |
zhangkun83 | da3c3f8 | 2015-03-11 18:03:31 -0700 | [diff] [blame] | 58 | } |
| 59 | x86_64 { |
| 60 | architecture "x86_64" |
| 61 | } |
zhangkun83 | da3c3f8 | 2015-03-11 18:03:31 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | components { |
| 65 | java_plugin(NativeExecutableSpec) { |
Kun Zhang | 221c534 | 2015-04-29 18:16:15 -0700 | [diff] [blame] | 66 | 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 |
zhangkun83 | da3c3f8 | 2015-03-11 18:03:31 -0700 | [diff] [blame] | 70 | } |
| 71 | baseName "$protocPluginBaseName" |
| 72 | } |
Kun Zhang | 90706dc | 2015-04-09 13:11:31 -0700 | [diff] [blame] | 73 | } |
Eric Anderson | 46ce409 | 2016-01-26 12:13:06 -0800 | [diff] [blame] | 74 | |
| 75 | binaries { |
| 76 | all { |
| 77 | if (toolChain in Gcc || toolChain in Clang) { |
| 78 | cppCompiler.args "--std=c++0x" |
| 79 | addEnvArgs("CXXFLAGS", cppCompiler.args) |
| 80 | addEnvArgs("CPPFLAGS", cppCompiler.args) |
| 81 | if (osdetector.os == "osx") { |
| 82 | cppCompiler.args "-mmacosx-version-min=10.7", "-stdlib=libc++" |
| 83 | addLibraryIfNotLinked('protoc', linker.args) |
| 84 | addLibraryIfNotLinked('protobuf', linker.args) |
| 85 | } else if (osdetector.os == "windows") { |
| 86 | linker.args "-static", "-lprotoc", "-lprotobuf", "-static-libgcc", "-static-libstdc++", |
| 87 | "-s" |
| 88 | } else { |
| 89 | // Link protoc, protobuf, libgcc and libstdc++ statically. |
| 90 | // Link other (system) libraries dynamically. |
| 91 | // Clang under OSX doesn't support these options. |
| 92 | linker.args "-Wl,-Bstatic", "-lprotoc", "-lprotobuf", "-static-libgcc", |
| 93 | "-static-libstdc++", |
| 94 | "-Wl,-Bdynamic", "-lpthread", "-s" |
| 95 | } |
| 96 | addEnvArgs("LDFLAGS", linker.args) |
| 97 | } else if (toolChain in VisualCpp) { |
| 98 | cppCompiler.args "/EHsc", "/MT" |
| 99 | if (rootProject.hasProperty('vcProtobufInclude')) { |
| 100 | cppCompiler.args "/I${rootProject.vcProtobufInclude}" |
| 101 | } |
| 102 | linker.args "libprotobuf.lib", "libprotoc.lib" |
| 103 | if (rootProject.hasProperty('vcProtobufLibs')) { |
| 104 | linker.args "/LIBPATH:${rootProject.vcProtobufLibs}" |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | } |
zhangkun | 9de8e4b | 2015-01-15 10:29:05 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Kun Zhang | 287a27a | 2015-05-07 17:32:31 -0700 | [diff] [blame] | 111 | configurations { |
Eric Anderson | b22bcdf | 2016-03-29 14:13:28 -0700 | [diff] [blame^] | 112 | testLiteCompile |
Kun Zhang | 287a27a | 2015-05-07 17:32:31 -0700 | [diff] [blame] | 113 | testNanoCompile |
| 114 | } |
| 115 | |
Eric Anderson | fb28ad2 | 2015-01-29 15:00:58 -0800 | [diff] [blame] | 116 | dependencies { |
Eric Anderson | e23f899 | 2015-04-10 15:40:44 -0700 | [diff] [blame] | 117 | testCompile project(':grpc-protobuf'), |
| 118 | project(':grpc-stub') |
Eric Anderson | b22bcdf | 2016-03-29 14:13:28 -0700 | [diff] [blame^] | 119 | testLiteCompile project(':grpc-protobuf-lite'), |
| 120 | project(':grpc-stub') |
Kun Zhang | 287a27a | 2015-05-07 17:32:31 -0700 | [diff] [blame] | 121 | testNanoCompile project(':grpc-protobuf-nano'), |
| 122 | project(':grpc-stub') |
| 123 | } |
| 124 | |
| 125 | sourceSets { |
Eric Anderson | b22bcdf | 2016-03-29 14:13:28 -0700 | [diff] [blame^] | 126 | testLite {} |
Kun Zhang | 287a27a | 2015-05-07 17:32:31 -0700 | [diff] [blame] | 127 | testNano { |
| 128 | proto { |
| 129 | setSrcDirs(['src/test/proto']) |
Kun Zhang | 35f77ee | 2015-06-22 16:29:30 -0700 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
Eric Anderson | b22bcdf | 2016-03-29 14:13:28 -0700 | [diff] [blame^] | 134 | compileTestLiteJava { |
Eric Anderson | 99a6d8d | 2016-03-22 11:31:36 -0700 | [diff] [blame] | 135 | // Protobuf-generated Lite produces quite a few warnings. |
| 136 | it.options.compilerArgs.removeAll(["-Xlint:unchecked", "-Xlint:rawtypes"]) |
| 137 | } |
| 138 | |
Kun Zhang | 35f77ee | 2015-06-22 16:29:30 -0700 | [diff] [blame] | 139 | protobuf { |
| 140 | protoc { |
| 141 | if (project.hasProperty('protoc')) { |
| 142 | path = project.protoc |
| 143 | } else { |
| 144 | artifact = "com.google.protobuf:protoc:${protobufVersion}" |
| 145 | } |
| 146 | } |
| 147 | plugins { |
| 148 | grpc { |
| 149 | path = javaPluginPath |
| 150 | } |
| 151 | } |
| 152 | generateProtoTasks { |
| 153 | all()*.dependsOn 'java_pluginExecutable' |
| 154 | ofSourceSet('test')*.plugins { |
| 155 | grpc {} |
| 156 | } |
Eric Anderson | b22bcdf | 2016-03-29 14:13:28 -0700 | [diff] [blame^] | 157 | ofSourceSet('testLite')*.plugins { |
| 158 | grpc { |
| 159 | option 'lite' |
| 160 | } |
| 161 | } |
Kun Zhang | 35f77ee | 2015-06-22 16:29:30 -0700 | [diff] [blame] | 162 | ofSourceSet('testNano').each { task -> |
| 163 | task.builtins { |
Kun Zhang | 287a27a | 2015-05-07 17:32:31 -0700 | [diff] [blame] | 164 | remove java |
| 165 | javanano { |
| 166 | option 'ignore_services=true' |
| 167 | } |
| 168 | } |
Kun Zhang | 35f77ee | 2015-06-22 16:29:30 -0700 | [diff] [blame] | 169 | task.plugins { |
Kun Zhang | 287a27a | 2015-05-07 17:32:31 -0700 | [diff] [blame] | 170 | grpc { |
| 171 | option 'nano=true' |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | checkstyleTestNano { |
| 179 | source = fileTree(dir: "src/testNano", include: "**/*.java") |
Eric Anderson | fb28ad2 | 2015-01-29 15:00:58 -0800 | [diff] [blame] | 180 | } |
| 181 | |
Kun Zhang | bc347e3 | 2015-09-01 15:26:48 -0700 | [diff] [blame] | 182 | println "*** Building codegen requires Protobuf version ${protobufVersion}" |
| 183 | println "*** Please refer to https://github.com/grpc/grpc-java/blob/master/COMPILING.md#how-to-build-code-generation-plugin" |
| 184 | |
zhangkun83 | da3c3f8 | 2015-03-11 18:03:31 -0700 | [diff] [blame] | 185 | task buildArtifacts(type: Copy) { |
Kun Zhang | 221c534 | 2015-04-29 18:16:15 -0700 | [diff] [blame] | 186 | dependsOn 'java_pluginExecutable' |
Kun Zhang | 29784d1 | 2016-01-26 15:30:35 -0800 | [diff] [blame] | 187 | from("$buildDir/exe") { |
zhangkun83 | da3c3f8 | 2015-03-11 18:03:31 -0700 | [diff] [blame] | 188 | if (osdetector.os != 'windows') { |
| 189 | rename 'protoc-gen-grpc-java', '$0.exe' |
| 190 | } |
| 191 | } |
| 192 | into artifactStagingPath |
| 193 | } |
| 194 | |
| 195 | archivesBaseName = "$protocPluginBaseName" |
| 196 | |
| 197 | artifacts { |
Kun Zhang | 29784d1 | 2016-01-26 15:30:35 -0800 | [diff] [blame] | 198 | archives("$artifactStagingPath/java_plugin/${protocPluginBaseName}.exe" as File) { |
Kun Zhang | 221c534 | 2015-04-29 18:16:15 -0700 | [diff] [blame] | 199 | classifier osdetector.os + "-" + arch |
| 200 | type "exe" |
| 201 | extension "exe" |
| 202 | builtBy buildArtifacts |
zhangkun83 | da3c3f8 | 2015-03-11 18:03:31 -0700 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | |
| 206 | // Exe files are skipped by Maven by default. Override it. |
| 207 | // Also skip jar files that is generated by the java plugin. |
| 208 | [ |
| 209 | install.repositories.mavenInstaller, |
| 210 | uploadArchives.repositories.mavenDeployer, |
Eric Anderson | bf42913 | 2016-01-29 17:01:33 -0800 | [diff] [blame] | 211 | ]*.setFilter {artifact, file -> |
zhangkun83 | da3c3f8 | 2015-03-11 18:03:31 -0700 | [diff] [blame] | 212 | ! (file.getName().endsWith('jar') || file.getName().endsWith('jar.asc')) |
| 213 | } |
| 214 | |
| 215 | [ |
zhangkun83 | da3c3f8 | 2015-03-11 18:03:31 -0700 | [diff] [blame] | 216 | uploadArchives.repositories.mavenDeployer, |
| 217 | ]*.beforeDeployment { |
Kun Zhang | 221c534 | 2015-04-29 18:16:15 -0700 | [diff] [blame] | 218 | def ret = exec { |
| 219 | executable 'bash' |
| 220 | args 'check-artifact.sh', osdetector.os, arch |
| 221 | } |
| 222 | if (ret.exitValue != 0) { |
| 223 | throw new GradleException("check-artifact.sh exited with " + ret.exitValue) |
zhangkun83 | da3c3f8 | 2015-03-11 18:03:31 -0700 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | |
Eric Anderson | bf42913 | 2016-01-29 17:01:33 -0800 | [diff] [blame] | 227 | [ |
| 228 | install.repositories.mavenInstaller, |
| 229 | uploadArchives.repositories.mavenDeployer, |
| 230 | ]*.pom*.whenConfigured { pom -> |
| 231 | pom.project { |
| 232 | // This isn't any sort of Java archive artifact, and OSSRH doesn't enforce |
| 233 | // javadoc for 'pom' packages. 'exe' would be a more appropriate packaging |
| 234 | // value, but it isn't clear how that will be interpreted. In addition, |
| 235 | // 'pom' is typically the value used when building an exe with Maven. |
| 236 | packaging = "pom" |
| 237 | } |
| 238 | } |
| 239 | |
Eric Anderson | b22bcdf | 2016-03-29 14:13:28 -0700 | [diff] [blame^] | 240 | def configureTestTask(Task task, String dep, String extraPackage) { |
| 241 | test.dependsOn task |
Eric Anderson | 99a6d8d | 2016-03-22 11:31:36 -0700 | [diff] [blame] | 242 | task.dependsOn "generateTest${dep}Proto" |
Kun Zhang | 4bc2d6d | 2015-04-17 10:49:11 -0700 | [diff] [blame] | 243 | if (osdetector.os != 'windows') { |
Kun Zhang | 287a27a | 2015-05-07 17:32:31 -0700 | [diff] [blame] | 244 | task.executable "diff" |
Eric Anderson | b938ba5 | 2015-02-28 09:59:25 -0800 | [diff] [blame] | 245 | } else { |
Kun Zhang | 287a27a | 2015-05-07 17:32:31 -0700 | [diff] [blame] | 246 | task.executable "fc" |
Eric Anderson | b938ba5 | 2015-02-28 09:59:25 -0800 | [diff] [blame] | 247 | } |
| 248 | // File isn't found on Windows if last slash is forward-slash |
| 249 | def slash = System.getProperty("file.separator") |
Eric Anderson | 99a6d8d | 2016-03-22 11:31:36 -0700 | [diff] [blame] | 250 | 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^] | 251 | "$projectDir/src/test${dep}/golden/TestService.java.txt" |
zhangkun83 | 5e60785 | 2015-01-22 12:31:56 -0800 | [diff] [blame] | 252 | } |
Xiao Hang | dcff315 | 2015-02-20 15:03:06 -0800 | [diff] [blame] | 253 | |
Kun Zhang | 287a27a | 2015-05-07 17:32:31 -0700 | [diff] [blame] | 254 | task testGolden(type: Exec) |
Eric Anderson | 99a6d8d | 2016-03-22 11:31:36 -0700 | [diff] [blame] | 255 | task testLiteGolden(type: Exec) |
Kun Zhang | 287a27a | 2015-05-07 17:32:31 -0700 | [diff] [blame] | 256 | task testNanoGolden(type: Exec) |
Eric Anderson | b22bcdf | 2016-03-29 14:13:28 -0700 | [diff] [blame^] | 257 | configureTestTask(testGolden, '', '') |
| 258 | configureTestTask(testLiteGolden, 'Lite', '') |
| 259 | configureTestTask(testNanoGolden, 'Nano', '/nano') |