blob: f0ab72c2f0fc7733b62d7aa7bc8f79ccee09588e [file] [log] [blame]
zhangkun9de8e4b2015-01-15 10:29:05 -08001apply plugin: "cpp"
Eric Andersonfb28ad22015-01-29 15:00:58 -08002apply plugin: "protobuf"
zhangkun9de8e4b2015-01-15 10:29:05 -08003
zhangkun835e607852015-01-22 12:31:56 -08004description = 'The protoc plugin for gRPC Java'
5
Eric Andersonfb28ad22015-01-29 15:00:58 -08006buildscript {
7 repositories {
8 mavenCentral()
zhangkun83da3c3f82015-03-11 18:03:31 -07009 mavenLocal()
Eric Andersonfb28ad22015-01-29 15:00:58 -080010 }
11 dependencies {
Kun Zhang4bc2d6d2015-04-17 10:49:11 -070012 classpath libraries.protobuf_plugin
Eric Andersonfb28ad22015-01-29 15:00:58 -080013 }
14}
15
zhangkun83da3c3f82015-03-11 18:03:31 -070016// 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.
20def targetArchs = ['local_arch'] as HashSet
21
22def artifactStagingPath = "$buildDir/artifacts" as File
23def artifactPath = { arch ->
24 return "$artifactStagingPath/java_pluginExecutable/" + arch + "/${protocPluginBaseName}.exe"
25}
26
27
28if (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.
37def 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 Zhang0c2ea152015-04-17 12:40:42 -070044// 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.
49def 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
zhangkun83da3c3f82015-03-11 18:03:31 -070056model {
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 Zhang90706dc2015-04-09 13:11:31 -0700108 }
zhangkun9de8e4b2015-01-15 10:29:05 -0800109}
110
Eric Andersonfb28ad22015-01-29 15:00:58 -0800111dependencies {
Eric Andersone23f8992015-04-10 15:40:44 -0700112 testCompile project(':grpc-protobuf'),
113 project(':grpc-stub')
Eric Andersonfb28ad22015-01-29 15:00:58 -0800114}
115
zhangkun9de8e4b2015-01-15 10:29:05 -0800116binaries.all {
nmittlerf1299602015-01-30 14:55:38 -0800117 if (toolChain in Gcc || toolChain in Clang) {
Louis Ryanc42c8c42015-03-18 16:31:38 -0700118 cppCompiler.args "--std=c++0x"
zhangkun83da3c3f82015-03-11 18:03:31 -0700119 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 Zhang0c2ea152015-04-17 12:40:42 -0700123 addLibraryIfNotLinked('protoc', linker.args)
124 addLibraryIfNotLinked('protobuf', linker.args)
zhangkun83da3c3f82015-03-11 18:03:31 -0700125 } 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 Anderson2049e0d2015-01-29 12:41:51 -0800133 }
zhangkun83da3c3f82015-03-11 18:03:31 -0700134 addEnvArgs("LDFLAGS", linker.args)
Eric Andersonb938ba52015-02-28 09:59:25 -0800135 } 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 }
zhangkun9de8e4b2015-01-15 10:29:05 -0800144 }
145}
146
zhangkun83da3c3f82015-03-11 18:03:31 -0700147task 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
159archivesBaseName = "$protocPluginBaseName"
160
161artifacts {
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[
zhangkun83da3c3f82015-03-11 18:03:31 -0700182 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 Andersonb938ba52015-02-28 09:59:25 -0800195protobufCodeGenPlugins = ["java_plugin:$javaPluginPath"]
zhangkun9de8e4b2015-01-15 10:29:05 -0800196
zhangkun83da3c3f82015-03-11 18:03:31 -0700197generateTestProto.dependsOn 'local_archJava_pluginExecutable'
Eric Andersonb938ba52015-02-28 09:59:25 -0800198// Ignore test for the moment on Windows. It will be easier to run once the
199// gradle protobuf plugin can support nano.
Kun Zhang4bc2d6d2015-04-17 10:49:11 -0700200if (osdetector.os != 'windows') {
Eric Andersonb938ba52015-02-28 09:59:25 -0800201 test.dependsOn('testGolden','testNanoGolden')
202}
Eric Andersonfb28ad22015-01-29 15:00:58 -0800203
204task testGolden(type: Exec, dependsOn: 'generateTestProto') {
Kun Zhang4bc2d6d2015-04-17 10:49:11 -0700205 if (osdetector.os != 'windows') {
Eric Andersonb938ba52015-02-28 09:59:25 -0800206 executable "diff"
207 } else {
208 executable "fc"
209 }
210 // File isn't found on Windows if last slash is forward-slash
211 def slash = System.getProperty("file.separator")
212 args "$buildDir/generated-sources/test/io/grpc/testing/integration" + slash + "TestServiceGrpc.java",
Eric Andersonfb28ad22015-01-29 15:00:58 -0800213 "$projectDir/src/test/golden/TestService.java.txt"
zhangkun835e607852015-01-22 12:31:56 -0800214}
Xiao Hangdcff3152015-02-20 15:03:06 -0800215
zhangkun83da3c3f82015-03-11 18:03:31 -0700216task testNanoGolden(type: Exec, dependsOn: 'local_archJava_pluginExecutable') {
Jakob Buchgraberdf321fe2015-02-25 19:47:05 -0800217 doFirst {
218 temporaryDir.createNewFile();
219 }
220
zsurocking5902c6a2015-02-25 20:54:53 -0800221 environment 'TEST_TMP_DIR', temporaryDir
222 commandLine './src/test/run_nano_test.sh'
223}