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