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