zhangkun | 9de8e4b | 2015-01-15 10:29:05 -0800 | [diff] [blame] | 1 | apply plugin: "cpp" |
Eric Anderson | fb28ad2 | 2015-01-29 15:00:58 -0800 | [diff] [blame] | 2 | apply plugin: "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 | // When there is only one platform available, Gradle doesn't create a directory |
| 17 | // for the sole platform. In order to keep the script simple, we intentionally |
| 18 | // always build the 'local_arch' even though it's duplicate with one of the |
| 19 | // targetArchs, so that we always have at least two platforms. |
| 20 | def targetArchs = ['local_arch'] as HashSet |
| 21 | |
| 22 | def artifactStagingPath = "$buildDir/artifacts" as File |
| 23 | def artifactPath = { arch -> |
| 24 | return "$artifactStagingPath/java_pluginExecutable/" + arch + "/${protocPluginBaseName}.exe" |
| 25 | } |
| 26 | |
| 27 | |
| 28 | if (System.env.TARGET_ARCHS != null) { |
| 29 | def archs = System.env.TARGET_ARCHS.split(' +') |
| 30 | targetArchs.addAll(archs) |
| 31 | } else { |
| 32 | targetArchs.add(osdetector.arch) |
| 33 | } |
| 34 | |
| 35 | // Adds space-delimited arguments from the environment variable env to the |
| 36 | // argList. |
| 37 | def addEnvArgs = { env, argList -> |
| 38 | def value = System.getenv(env) |
| 39 | if (value != null) { |
| 40 | value.split(' +').each() { it -> argList.add(it) } |
| 41 | } |
| 42 | } |
| 43 | |
Kun Zhang | 0c2ea15 | 2015-04-17 12:40:42 -0700 | [diff] [blame] | 44 | // Adds corresponding "-l" option to the argList if libName is not found in |
| 45 | // LDFLAGS. This is only used for Mac because when building for uploadArchives |
| 46 | // artifacts, we add the ".a" files directly to LDFLAGS and without "-l" in |
| 47 | // order to get statically linked, otherwise we add the libraries through "-l" |
| 48 | // so that they can be searched for in default search paths. |
| 49 | def addLibraryIfNotLinked = { libName, argList -> |
| 50 | def ldflags = System.env.LDFLAGS |
| 51 | if (ldflags == null || !ldflags.contains('lib' + libName + '.a')) { |
| 52 | argList.add('-l' + libName) |
| 53 | } |
| 54 | } |
| 55 | |
zhangkun83 | da3c3f8 | 2015-03-11 18:03:31 -0700 | [diff] [blame] | 56 | model { |
| 57 | toolChains { |
| 58 | gcc(Gcc) { |
| 59 | target("x86_64") { |
| 60 | cppCompiler.withArguments { args -> |
| 61 | args << "-m64" |
| 62 | } |
| 63 | linker.withArguments { args -> |
| 64 | args << "-m64" |
| 65 | } |
| 66 | } |
| 67 | target("x86_32") { |
| 68 | cppCompiler.withArguments { args -> |
| 69 | args << "-m32" |
| 70 | } |
| 71 | linker.withArguments { args -> |
| 72 | args << "-m32" |
| 73 | } |
| 74 | } |
| 75 | target('local_arch') { } |
| 76 | } |
| 77 | clang(Clang) { |
| 78 | target("x86_64") { |
| 79 | cppCompiler.withArguments { args -> |
| 80 | args << "-m64" |
| 81 | } |
| 82 | linker.withArguments { args -> |
| 83 | args << "-m64" |
| 84 | } |
| 85 | } |
| 86 | target('local_arch') { } |
| 87 | } |
| 88 | } |
| 89 | platforms { |
| 90 | x86_32 { |
| 91 | architecture "x86_32" |
| 92 | } |
| 93 | x86_64 { |
| 94 | architecture "x86_64" |
| 95 | } |
| 96 | local_arch { |
| 97 | architecture 'local_arch' |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | components { |
| 102 | java_plugin(NativeExecutableSpec) { |
| 103 | targetArchs.each { |
| 104 | targetPlatform it |
| 105 | } |
| 106 | baseName "$protocPluginBaseName" |
| 107 | } |
Kun Zhang | 90706dc | 2015-04-09 13:11:31 -0700 | [diff] [blame] | 108 | } |
zhangkun | 9de8e4b | 2015-01-15 10:29:05 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Eric Anderson | fb28ad2 | 2015-01-29 15:00:58 -0800 | [diff] [blame] | 111 | dependencies { |
Eric Anderson | e23f899 | 2015-04-10 15:40:44 -0700 | [diff] [blame] | 112 | testCompile project(':grpc-protobuf'), |
| 113 | project(':grpc-stub') |
Eric Anderson | fb28ad2 | 2015-01-29 15:00:58 -0800 | [diff] [blame] | 114 | } |
| 115 | |
zhangkun | 9de8e4b | 2015-01-15 10:29:05 -0800 | [diff] [blame] | 116 | binaries.all { |
nmittler | f129960 | 2015-01-30 14:55:38 -0800 | [diff] [blame] | 117 | if (toolChain in Gcc || toolChain in Clang) { |
Louis Ryan | c42c8c4 | 2015-03-18 16:31:38 -0700 | [diff] [blame] | 118 | cppCompiler.args "--std=c++0x" |
zhangkun83 | da3c3f8 | 2015-03-11 18:03:31 -0700 | [diff] [blame] | 119 | addEnvArgs("CXXFLAGS", cppCompiler.args) |
| 120 | addEnvArgs("CPPFLAGS", cppCompiler.args) |
| 121 | if (osdetector.os == "osx") { |
| 122 | cppCompiler.args "-mmacosx-version-min=10.7", "-stdlib=libc++" |
Kun Zhang | 0c2ea15 | 2015-04-17 12:40:42 -0700 | [diff] [blame] | 123 | addLibraryIfNotLinked('protoc', linker.args) |
| 124 | addLibraryIfNotLinked('protobuf', linker.args) |
zhangkun83 | da3c3f8 | 2015-03-11 18:03:31 -0700 | [diff] [blame] | 125 | } else if (osdetector.os == "windows") { |
| 126 | linker.args "-static", "-lprotoc", "-lprotobuf", "-static-libgcc", "-static-libstdc++", "-s" |
| 127 | } else { |
| 128 | // Link protoc, protobuf, libgcc and libstdc++ statically. |
| 129 | // Link other (system) libraries dynamically. |
| 130 | // Clang under OSX doesn't support these options. |
| 131 | linker.args "-Wl,-Bstatic", "-lprotoc", "-lprotobuf", "-static-libgcc", "-static-libstdc++", |
| 132 | "-Wl,-Bdynamic", "-lpthread", "-s" |
Eric Anderson | 2049e0d | 2015-01-29 12:41:51 -0800 | [diff] [blame] | 133 | } |
zhangkun83 | da3c3f8 | 2015-03-11 18:03:31 -0700 | [diff] [blame] | 134 | addEnvArgs("LDFLAGS", linker.args) |
Eric Anderson | b938ba5 | 2015-02-28 09:59:25 -0800 | [diff] [blame] | 135 | } else if (toolChain in VisualCpp) { |
| 136 | cppCompiler.args "/EHsc", "/MD" |
| 137 | if (rootProject.hasProperty('protobuf.include')) { |
| 138 | cppCompiler.args "/I" + rootProject.properties['protobuf.include'] |
| 139 | } |
| 140 | linker.args "libprotobuf.lib", "libprotoc.lib" |
| 141 | if (rootProject.hasProperty('protobuf.libs')) { |
| 142 | linker.args "/LIBPATH:" + rootProject.properties['protobuf.libs'] |
| 143 | } |
zhangkun | 9de8e4b | 2015-01-15 10:29:05 -0800 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
zhangkun83 | da3c3f8 | 2015-03-11 18:03:31 -0700 | [diff] [blame] | 147 | task buildArtifacts(type: Copy) { |
| 148 | targetArchs.each { |
| 149 | dependsOn it + 'Java_pluginExecutable' |
| 150 | } |
| 151 | from("$buildDir/binaries") { |
| 152 | if (osdetector.os != 'windows') { |
| 153 | rename 'protoc-gen-grpc-java', '$0.exe' |
| 154 | } |
| 155 | } |
| 156 | into artifactStagingPath |
| 157 | } |
| 158 | |
| 159 | archivesBaseName = "$protocPluginBaseName" |
| 160 | |
| 161 | artifacts { |
| 162 | for (arch in (targetArchs - 'local_arch')) { |
| 163 | archives(artifactPath(arch) as File) { |
| 164 | classifier osdetector.os + "-" + arch |
| 165 | type "exe" |
| 166 | extension "exe" |
| 167 | builtBy buildArtifacts |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | // Exe files are skipped by Maven by default. Override it. |
| 173 | // Also skip jar files that is generated by the java plugin. |
| 174 | [ |
| 175 | install.repositories.mavenInstaller, |
| 176 | uploadArchives.repositories.mavenDeployer, |
| 177 | ]*.addFilter('all') {artifact, file -> |
| 178 | ! (file.getName().endsWith('jar') || file.getName().endsWith('jar.asc')) |
| 179 | } |
| 180 | |
| 181 | [ |
zhangkun83 | da3c3f8 | 2015-03-11 18:03:31 -0700 | [diff] [blame] | 182 | uploadArchives.repositories.mavenDeployer, |
| 183 | ]*.beforeDeployment { |
| 184 | for (arch in (targetArchs - 'local_arch')) { |
| 185 | def ret = exec { |
| 186 | executable 'bash' |
| 187 | args 'check-artifact.sh', osdetector.os, arch |
| 188 | } |
| 189 | if (ret.exitValue != 0) { |
| 190 | throw new GradleException("check-artifact.sh exited with " + ret.exitValue) |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
Eric Anderson | b938ba5 | 2015-02-28 09:59:25 -0800 | [diff] [blame] | 195 | protobufCodeGenPlugins = ["java_plugin:$javaPluginPath"] |
zhangkun | 9de8e4b | 2015-01-15 10:29:05 -0800 | [diff] [blame] | 196 | |
Kun Zhang | af18876 | 2015-04-20 09:46:46 -0700 | [diff] [blame] | 197 | project.afterEvaluate { |
| 198 | generateTestProto.dependsOn 'local_archJava_pluginExecutable' |
| 199 | } |
| 200 | |
Eric Anderson | b938ba5 | 2015-02-28 09:59:25 -0800 | [diff] [blame] | 201 | // Ignore test for the moment on Windows. It will be easier to run once the |
| 202 | // gradle protobuf plugin can support nano. |
Kun Zhang | 4bc2d6d | 2015-04-17 10:49:11 -0700 | [diff] [blame] | 203 | if (osdetector.os != 'windows') { |
Eric Anderson | b938ba5 | 2015-02-28 09:59:25 -0800 | [diff] [blame] | 204 | test.dependsOn('testGolden','testNanoGolden') |
| 205 | } |
Eric Anderson | fb28ad2 | 2015-01-29 15:00:58 -0800 | [diff] [blame] | 206 | |
| 207 | task testGolden(type: Exec, dependsOn: 'generateTestProto') { |
Kun Zhang | 4bc2d6d | 2015-04-17 10:49:11 -0700 | [diff] [blame] | 208 | if (osdetector.os != 'windows') { |
Eric Anderson | b938ba5 | 2015-02-28 09:59:25 -0800 | [diff] [blame] | 209 | executable "diff" |
| 210 | } else { |
| 211 | executable "fc" |
| 212 | } |
| 213 | // File isn't found on Windows if last slash is forward-slash |
| 214 | def slash = System.getProperty("file.separator") |
| 215 | args "$buildDir/generated-sources/test/io/grpc/testing/integration" + slash + "TestServiceGrpc.java", |
Eric Anderson | fb28ad2 | 2015-01-29 15:00:58 -0800 | [diff] [blame] | 216 | "$projectDir/src/test/golden/TestService.java.txt" |
zhangkun83 | 5e60785 | 2015-01-22 12:31:56 -0800 | [diff] [blame] | 217 | } |
Xiao Hang | dcff315 | 2015-02-20 15:03:06 -0800 | [diff] [blame] | 218 | |
zhangkun83 | da3c3f8 | 2015-03-11 18:03:31 -0700 | [diff] [blame] | 219 | task testNanoGolden(type: Exec, dependsOn: 'local_archJava_pluginExecutable') { |
Jakob Buchgraber | df321fe | 2015-02-25 19:47:05 -0800 | [diff] [blame] | 220 | doFirst { |
| 221 | temporaryDir.createNewFile(); |
| 222 | } |
| 223 | |
zsurocking | 5902c6a | 2015-02-25 20:54:53 -0800 | [diff] [blame] | 224 | environment 'TEST_TMP_DIR', temporaryDir |
| 225 | commandLine './src/test/run_nano_test.sh' |
| 226 | } |