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 | } |
zhangkun | 9de8e4b | 2015-01-15 10:29:05 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Eric Anderson | fb28ad2 | 2015-01-29 15:00:58 -0800 | [diff] [blame] | 76 | dependencies { |
Eric Anderson | e23f899 | 2015-04-10 15:40:44 -0700 | [diff] [blame] | 77 | testCompile project(':grpc-protobuf'), |
| 78 | project(':grpc-stub') |
Eric Anderson | fb28ad2 | 2015-01-29 15:00:58 -0800 | [diff] [blame] | 79 | } |
| 80 | |
zhangkun | 9de8e4b | 2015-01-15 10:29:05 -0800 | [diff] [blame] | 81 | binaries.all { |
nmittler | f129960 | 2015-01-30 14:55:38 -0800 | [diff] [blame] | 82 | if (toolChain in Gcc || toolChain in Clang) { |
Louis Ryan | c42c8c4 | 2015-03-18 16:31:38 -0700 | [diff] [blame] | 83 | cppCompiler.args "--std=c++0x" |
zhangkun83 | da3c3f8 | 2015-03-11 18:03:31 -0700 | [diff] [blame] | 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++" |
Kun Zhang | 0c2ea15 | 2015-04-17 12:40:42 -0700 | [diff] [blame] | 88 | addLibraryIfNotLinked('protoc', linker.args) |
| 89 | addLibraryIfNotLinked('protobuf', linker.args) |
zhangkun83 | da3c3f8 | 2015-03-11 18:03:31 -0700 | [diff] [blame] | 90 | } else if (osdetector.os == "windows") { |
| 91 | linker.args "-static", "-lprotoc", "-lprotobuf", "-static-libgcc", "-static-libstdc++", "-s" |
| 92 | } else { |
| 93 | // Link protoc, protobuf, libgcc and libstdc++ statically. |
| 94 | // Link other (system) libraries dynamically. |
| 95 | // Clang under OSX doesn't support these options. |
| 96 | linker.args "-Wl,-Bstatic", "-lprotoc", "-lprotobuf", "-static-libgcc", "-static-libstdc++", |
| 97 | "-Wl,-Bdynamic", "-lpthread", "-s" |
Eric Anderson | 2049e0d | 2015-01-29 12:41:51 -0800 | [diff] [blame] | 98 | } |
zhangkun83 | da3c3f8 | 2015-03-11 18:03:31 -0700 | [diff] [blame] | 99 | addEnvArgs("LDFLAGS", linker.args) |
Eric Anderson | b938ba5 | 2015-02-28 09:59:25 -0800 | [diff] [blame] | 100 | } else if (toolChain in VisualCpp) { |
| 101 | cppCompiler.args "/EHsc", "/MD" |
Kun Zhang | 2f74971 | 2015-05-06 13:10:28 -0700 | [diff] [blame^] | 102 | if (rootProject.hasProperty('vcProtobufInclude')) { |
| 103 | cppCompiler.args "/I${rootProject.vcProtobufInclude}" |
Eric Anderson | b938ba5 | 2015-02-28 09:59:25 -0800 | [diff] [blame] | 104 | } |
| 105 | linker.args "libprotobuf.lib", "libprotoc.lib" |
Kun Zhang | 2f74971 | 2015-05-06 13:10:28 -0700 | [diff] [blame^] | 106 | if (rootProject.hasProperty('vcProtobufLibs')) { |
| 107 | linker.args "/LIBPATH:${rootProject.vcProtobufLibs}" |
Eric Anderson | b938ba5 | 2015-02-28 09:59:25 -0800 | [diff] [blame] | 108 | } |
zhangkun | 9de8e4b | 2015-01-15 10:29:05 -0800 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
zhangkun83 | da3c3f8 | 2015-03-11 18:03:31 -0700 | [diff] [blame] | 112 | task buildArtifacts(type: Copy) { |
Kun Zhang | 221c534 | 2015-04-29 18:16:15 -0700 | [diff] [blame] | 113 | dependsOn 'java_pluginExecutable' |
zhangkun83 | da3c3f8 | 2015-03-11 18:03:31 -0700 | [diff] [blame] | 114 | from("$buildDir/binaries") { |
| 115 | if (osdetector.os != 'windows') { |
| 116 | rename 'protoc-gen-grpc-java', '$0.exe' |
| 117 | } |
| 118 | } |
| 119 | into artifactStagingPath |
| 120 | } |
| 121 | |
| 122 | archivesBaseName = "$protocPluginBaseName" |
| 123 | |
| 124 | artifacts { |
Kun Zhang | 221c534 | 2015-04-29 18:16:15 -0700 | [diff] [blame] | 125 | archives("$artifactStagingPath/java_pluginExecutable/${protocPluginBaseName}.exe" as File) { |
| 126 | classifier osdetector.os + "-" + arch |
| 127 | type "exe" |
| 128 | extension "exe" |
| 129 | builtBy buildArtifacts |
zhangkun83 | da3c3f8 | 2015-03-11 18:03:31 -0700 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
| 133 | // Exe files are skipped by Maven by default. Override it. |
| 134 | // Also skip jar files that is generated by the java plugin. |
| 135 | [ |
| 136 | install.repositories.mavenInstaller, |
| 137 | uploadArchives.repositories.mavenDeployer, |
| 138 | ]*.addFilter('all') {artifact, file -> |
| 139 | ! (file.getName().endsWith('jar') || file.getName().endsWith('jar.asc')) |
| 140 | } |
| 141 | |
| 142 | [ |
zhangkun83 | da3c3f8 | 2015-03-11 18:03:31 -0700 | [diff] [blame] | 143 | uploadArchives.repositories.mavenDeployer, |
| 144 | ]*.beforeDeployment { |
Kun Zhang | 221c534 | 2015-04-29 18:16:15 -0700 | [diff] [blame] | 145 | def ret = exec { |
| 146 | executable 'bash' |
| 147 | args 'check-artifact.sh', osdetector.os, arch |
| 148 | } |
| 149 | if (ret.exitValue != 0) { |
| 150 | throw new GradleException("check-artifact.sh exited with " + ret.exitValue) |
zhangkun83 | da3c3f8 | 2015-03-11 18:03:31 -0700 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
Kun Zhang | c5b94c7 | 2015-05-06 16:44:55 -0700 | [diff] [blame] | 154 | project.protocDep = "com.google.protobuf:protoc:${protobufVersion}" |
Eric Anderson | b938ba5 | 2015-02-28 09:59:25 -0800 | [diff] [blame] | 155 | protobufCodeGenPlugins = ["java_plugin:$javaPluginPath"] |
zhangkun | 9de8e4b | 2015-01-15 10:29:05 -0800 | [diff] [blame] | 156 | |
Kun Zhang | af18876 | 2015-04-20 09:46:46 -0700 | [diff] [blame] | 157 | project.afterEvaluate { |
Kun Zhang | 221c534 | 2015-04-29 18:16:15 -0700 | [diff] [blame] | 158 | generateTestProto.dependsOn 'java_pluginExecutable' |
Kun Zhang | af18876 | 2015-04-20 09:46:46 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Eric Anderson | b938ba5 | 2015-02-28 09:59:25 -0800 | [diff] [blame] | 161 | // Ignore test for the moment on Windows. It will be easier to run once the |
| 162 | // gradle protobuf plugin can support nano. |
Kun Zhang | 4bc2d6d | 2015-04-17 10:49:11 -0700 | [diff] [blame] | 163 | if (osdetector.os != 'windows') { |
Eric Anderson | b938ba5 | 2015-02-28 09:59:25 -0800 | [diff] [blame] | 164 | test.dependsOn('testGolden','testNanoGolden') |
| 165 | } |
Eric Anderson | fb28ad2 | 2015-01-29 15:00:58 -0800 | [diff] [blame] | 166 | |
| 167 | task testGolden(type: Exec, dependsOn: 'generateTestProto') { |
Kun Zhang | 4bc2d6d | 2015-04-17 10:49:11 -0700 | [diff] [blame] | 168 | if (osdetector.os != 'windows') { |
Eric Anderson | b938ba5 | 2015-02-28 09:59:25 -0800 | [diff] [blame] | 169 | executable "diff" |
| 170 | } else { |
| 171 | executable "fc" |
| 172 | } |
| 173 | // File isn't found on Windows if last slash is forward-slash |
| 174 | def slash = System.getProperty("file.separator") |
| 175 | args "$buildDir/generated-sources/test/io/grpc/testing/integration" + slash + "TestServiceGrpc.java", |
Eric Anderson | fb28ad2 | 2015-01-29 15:00:58 -0800 | [diff] [blame] | 176 | "$projectDir/src/test/golden/TestService.java.txt" |
zhangkun83 | 5e60785 | 2015-01-22 12:31:56 -0800 | [diff] [blame] | 177 | } |
Xiao Hang | dcff315 | 2015-02-20 15:03:06 -0800 | [diff] [blame] | 178 | |
Kun Zhang | 221c534 | 2015-04-29 18:16:15 -0700 | [diff] [blame] | 179 | task testNanoGolden(type: Exec, dependsOn: 'java_pluginExecutable') { |
Jakob Buchgraber | df321fe | 2015-02-25 19:47:05 -0800 | [diff] [blame] | 180 | doFirst { |
| 181 | temporaryDir.createNewFile(); |
| 182 | } |
| 183 | |
zsurocking | 5902c6a | 2015-02-25 20:54:53 -0800 | [diff] [blame] | 184 | environment 'TEST_TMP_DIR', temporaryDir |
| 185 | commandLine './src/test/run_nano_test.sh' |
| 186 | } |