blob: aca19ae15112d9ea6514bc4b85152437de838dff [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
Kun Zhang0c2ea152015-04-17 12:40:42 -070048// 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.
53def 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
zhangkun83da3c3f82015-03-11 18:03:31 -070060model {
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 Zhang90706dc2015-04-09 13:11:31 -0700112 }
zhangkun9de8e4b2015-01-15 10:29:05 -0800113}
114
Eric Andersonfb28ad22015-01-29 15:00:58 -0800115dependencies {
Eric Andersone23f8992015-04-10 15:40:44 -0700116 testCompile project(':grpc-protobuf'),
117 project(':grpc-stub')
Eric Andersonfb28ad22015-01-29 15:00:58 -0800118}
119
zhangkun9de8e4b2015-01-15 10:29:05 -0800120binaries.all {
nmittlerf1299602015-01-30 14:55:38 -0800121 if (toolChain in Gcc || toolChain in Clang) {
Louis Ryanc42c8c42015-03-18 16:31:38 -0700122 cppCompiler.args "--std=c++0x"
zhangkun83da3c3f82015-03-11 18:03:31 -0700123 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 Zhang0c2ea152015-04-17 12:40:42 -0700127 addLibraryIfNotLinked('protoc', linker.args)
128 addLibraryIfNotLinked('protobuf', linker.args)
zhangkun83da3c3f82015-03-11 18:03:31 -0700129 } 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 Anderson2049e0d2015-01-29 12:41:51 -0800137 }
zhangkun83da3c3f82015-03-11 18:03:31 -0700138 addEnvArgs("LDFLAGS", linker.args)
Eric Andersonb938ba52015-02-28 09:59:25 -0800139 } 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 }
zhangkun9de8e4b2015-01-15 10:29:05 -0800148 }
149}
150
zhangkun83da3c3f82015-03-11 18:03:31 -0700151task 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
163archivesBaseName = "$protocPluginBaseName"
164
165artifacts {
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[
zhangkun83da3c3f82015-03-11 18:03:31 -0700186 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 Andersonb938ba52015-02-28 09:59:25 -0800199protobufCodeGenPlugins = ["java_plugin:$javaPluginPath"]
zhangkun9de8e4b2015-01-15 10:29:05 -0800200
zhangkun83da3c3f82015-03-11 18:03:31 -0700201generateTestProto.dependsOn 'local_archJava_pluginExecutable'
Eric Andersonb938ba52015-02-28 09:59:25 -0800202// Ignore test for the moment on Windows. It will be easier to run once the
203// gradle protobuf plugin can support nano.
204if (!Os.isFamily(Os.FAMILY_WINDOWS)) {
205 test.dependsOn('testGolden','testNanoGolden')
206}
Eric Andersonfb28ad22015-01-29 15:00:58 -0800207
208task testGolden(type: Exec, dependsOn: 'generateTestProto') {
Eric Andersonb938ba52015-02-28 09:59:25 -0800209 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 Andersonfb28ad22015-01-29 15:00:58 -0800217 "$projectDir/src/test/golden/TestService.java.txt"
zhangkun835e607852015-01-22 12:31:56 -0800218}
Xiao Hangdcff3152015-02-20 15:03:06 -0800219
zhangkun83da3c3f82015-03-11 18:03:31 -0700220task testNanoGolden(type: Exec, dependsOn: 'local_archJava_pluginExecutable') {
Jakob Buchgraberdf321fe2015-02-25 19:47:05 -0800221 doFirst {
222 temporaryDir.createNewFile();
223 }
224
zsurocking5902c6a2015-02-25 20:54:53 -0800225 environment 'TEST_TMP_DIR', temporaryDir
226 commandLine './src/test/run_nano_test.sh'
227}