blob: 365ff0d0e2466cfacd62ab1dd5f97d7254021ecb [file] [log] [blame]
zhangkun9de8e4b2015-01-15 10:29:05 -08001apply plugin: "cpp"
zhangkun83da3c3f82015-03-11 18:03:31 -07002apply plugin: "osdetector"
Eric Andersonfb28ad22015-01-29 15:00:58 -08003apply plugin: "protobuf"
zhangkun9de8e4b2015-01-15 10:29:05 -08004
Eric Andersonb938ba52015-02-28 09:59:25 -08005import org.apache.tools.ant.taskdefs.condition.Os
6
zhangkun835e607852015-01-22 12:31:56 -08007description = 'The protoc plugin for gRPC Java'
8
Eric Andersonfb28ad22015-01-29 15:00:58 -08009buildscript {
10 repositories {
11 mavenCentral()
zhangkun83da3c3f82015-03-11 18:03:31 -070012 mavenLocal()
Eric Andersonfb28ad22015-01-29 15:00:58 -080013 }
14 dependencies {
zhangkun83da3c3f82015-03-11 18:03:31 -070015 classpath libraries.protobuf_plugin,
16 'com.google.gradle:osdetector-gradle-plugin:1.2.1'
Eric Andersonfb28ad22015-01-29 15:00:58 -080017 }
18}
19
zhangkun83da3c3f82015-03-11 18:03:31 -070020// 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.
24def targetArchs = ['local_arch'] as HashSet
25
26def artifactStagingPath = "$buildDir/artifacts" as File
27def artifactPath = { arch ->
28 return "$artifactStagingPath/java_pluginExecutable/" + arch + "/${protocPluginBaseName}.exe"
29}
30
31
32if (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.
41def addEnvArgs = { env, argList ->
42 def value = System.getenv(env)
43 if (value != null) {
44 value.split(' +').each() { it -> argList.add(it) }
45 }
46}
47
48model {
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 Zhang90706dc2015-04-09 13:11:31 -0700100 }
zhangkun9de8e4b2015-01-15 10:29:05 -0800101}
102
Eric Andersonfb28ad22015-01-29 15:00:58 -0800103dependencies {
Eric Andersone23f8992015-04-10 15:40:44 -0700104 testCompile project(':grpc-protobuf'),
105 project(':grpc-stub')
Eric Andersonfb28ad22015-01-29 15:00:58 -0800106}
107
zhangkun9de8e4b2015-01-15 10:29:05 -0800108binaries.all {
nmittlerf1299602015-01-30 14:55:38 -0800109 if (toolChain in Gcc || toolChain in Clang) {
Louis Ryanc42c8c42015-03-18 16:31:38 -0700110 cppCompiler.args "--std=c++0x"
zhangkun83da3c3f82015-03-11 18:03:31 -0700111 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 Anderson2049e0d2015-01-29 12:41:51 -0800123 }
zhangkun83da3c3f82015-03-11 18:03:31 -0700124 addEnvArgs("LDFLAGS", linker.args)
Eric Andersonb938ba52015-02-28 09:59:25 -0800125 } 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 }
zhangkun9de8e4b2015-01-15 10:29:05 -0800134 }
135}
136
zhangkun83da3c3f82015-03-11 18:03:31 -0700137task 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
149archivesBaseName = "$protocPluginBaseName"
150
151artifacts {
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 Andersonb938ba52015-02-28 09:59:25 -0800186protobufCodeGenPlugins = ["java_plugin:$javaPluginPath"]
zhangkun9de8e4b2015-01-15 10:29:05 -0800187
zhangkun83da3c3f82015-03-11 18:03:31 -0700188generateTestProto.dependsOn 'local_archJava_pluginExecutable'
Eric Andersonb938ba52015-02-28 09:59:25 -0800189// Ignore test for the moment on Windows. It will be easier to run once the
190// gradle protobuf plugin can support nano.
191if (!Os.isFamily(Os.FAMILY_WINDOWS)) {
192 test.dependsOn('testGolden','testNanoGolden')
193}
Eric Andersonfb28ad22015-01-29 15:00:58 -0800194
195task testGolden(type: Exec, dependsOn: 'generateTestProto') {
Eric Andersonb938ba52015-02-28 09:59:25 -0800196 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 Andersonfb28ad22015-01-29 15:00:58 -0800204 "$projectDir/src/test/golden/TestService.java.txt"
zhangkun835e607852015-01-22 12:31:56 -0800205}
Xiao Hangdcff3152015-02-20 15:03:06 -0800206
zhangkun83da3c3f82015-03-11 18:03:31 -0700207task testNanoGolden(type: Exec, dependsOn: 'local_archJava_pluginExecutable') {
Jakob Buchgraberdf321fe2015-02-25 19:47:05 -0800208 doFirst {
209 temporaryDir.createNewFile();
210 }
211
zsurocking5902c6a2015-02-25 20:54:53 -0800212 environment 'TEST_TMP_DIR', temporaryDir
213 commandLine './src/test/run_nano_test.sh'
214}