blob: 4433cdd1b7ef4d9a0a212b42ad69a85583cba49e [file] [log] [blame]
zhangkun9de8e4b2015-01-15 10:29:05 -08001apply plugin: "cpp"
Kun Zhang41940f72015-04-28 18:14:34 -07002apply plugin: "com.google.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 -070016def artifactStagingPath = "$buildDir/artifacts" as File
zhangkun83da3c3f82015-03-11 18:03:31 -070017// Adds space-delimited arguments from the environment variable env to the
18// argList.
19def addEnvArgs = { env, argList ->
20 def value = System.getenv(env)
21 if (value != null) {
22 value.split(' +').each() { it -> argList.add(it) }
23 }
24}
25
Kun Zhang0c2ea152015-04-17 12:40:42 -070026// Adds corresponding "-l" option to the argList if libName is not found in
27// LDFLAGS. This is only used for Mac because when building for uploadArchives
28// artifacts, we add the ".a" files directly to LDFLAGS and without "-l" in
29// order to get statically linked, otherwise we add the libraries through "-l"
30// so that they can be searched for in default search paths.
31def addLibraryIfNotLinked = { libName, argList ->
32 def ldflags = System.env.LDFLAGS
33 if (ldflags == null || !ldflags.contains('lib' + libName + '.a')) {
34 argList.add('-l' + libName)
35 }
36}
37
Kun Zhang2f749712015-05-06 13:10:28 -070038def String arch = rootProject.hasProperty('targetArch') ? rootProject.targetArch : osdetector.arch
39def boolean vcDisable = rootProject.hasProperty('vcDisable') ? rootProject.vcDisable : false
Kun Zhang221c5342015-04-29 18:16:15 -070040
zhangkun83da3c3f82015-03-11 18:03:31 -070041model {
42 toolChains {
Kun Zhang221c5342015-04-29 18:16:15 -070043 // If you have both VC and Gcc installed, VC will be selected, unless you
Kun Zhang2f749712015-05-06 13:10:28 -070044 // set 'vcDisable=true'
45 if (!vcDisable) {
Kun Zhang221c5342015-04-29 18:16:15 -070046 visualCpp(VisualCpp) {
47 }
48 }
zhangkun83da3c3f82015-03-11 18:03:31 -070049 gcc(Gcc) {
zhangkun83da3c3f82015-03-11 18:03:31 -070050 }
51 clang(Clang) {
zhangkun83da3c3f82015-03-11 18:03:31 -070052 }
53 }
Kun Zhang221c5342015-04-29 18:16:15 -070054
zhangkun83da3c3f82015-03-11 18:03:31 -070055 platforms {
56 x86_32 {
Kun Zhang221c5342015-04-29 18:16:15 -070057 architecture "x86"
zhangkun83da3c3f82015-03-11 18:03:31 -070058 }
59 x86_64 {
60 architecture "x86_64"
61 }
zhangkun83da3c3f82015-03-11 18:03:31 -070062 }
63
64 components {
65 java_plugin(NativeExecutableSpec) {
Kun Zhang221c5342015-04-29 18:16:15 -070066 if (arch in ['x86_32', 'x86_64']) {
67 // If arch is not within the defined platforms, we do not specify the
68 // targetPlatform so that Gradle will choose what is appropriate.
69 targetPlatform arch
zhangkun83da3c3f82015-03-11 18:03:31 -070070 }
71 baseName "$protocPluginBaseName"
72 }
Kun Zhang90706dc2015-04-09 13:11:31 -070073 }
zhangkun9de8e4b2015-01-15 10:29:05 -080074}
75
Kun Zhang287a27a2015-05-07 17:32:31 -070076configurations {
77 testNanoCompile
78}
79
Eric Andersonfb28ad22015-01-29 15:00:58 -080080dependencies {
Eric Andersone23f8992015-04-10 15:40:44 -070081 testCompile project(':grpc-protobuf'),
82 project(':grpc-stub')
Kun Zhang287a27a2015-05-07 17:32:31 -070083 testNanoCompile project(':grpc-protobuf-nano'),
84 project(':grpc-stub')
85}
86
87sourceSets {
88 test {
89 proto {
90 plugins {
91 grpc { }
92 }
93 }
94 }
95 testNano {
96 proto {
97 setSrcDirs(['src/test/proto'])
98 builtins {
99 remove java
100 javanano {
101 option 'ignore_services=true'
102 }
103 }
104 plugins {
105 grpc {
106 option 'nano=true'
107 }
108 }
109 }
110 }
111}
112
113checkstyleTestNano {
114 source = fileTree(dir: "src/testNano", include: "**/*.java")
Eric Andersonfb28ad22015-01-29 15:00:58 -0800115}
116
zhangkun9de8e4b2015-01-15 10:29:05 -0800117binaries.all {
nmittlerf1299602015-01-30 14:55:38 -0800118 if (toolChain in Gcc || toolChain in Clang) {
Louis Ryanc42c8c42015-03-18 16:31:38 -0700119 cppCompiler.args "--std=c++0x"
zhangkun83da3c3f82015-03-11 18:03:31 -0700120 addEnvArgs("CXXFLAGS", cppCompiler.args)
121 addEnvArgs("CPPFLAGS", cppCompiler.args)
122 if (osdetector.os == "osx") {
123 cppCompiler.args "-mmacosx-version-min=10.7", "-stdlib=libc++"
Kun Zhang0c2ea152015-04-17 12:40:42 -0700124 addLibraryIfNotLinked('protoc', linker.args)
125 addLibraryIfNotLinked('protobuf', linker.args)
zhangkun83da3c3f82015-03-11 18:03:31 -0700126 } else if (osdetector.os == "windows") {
127 linker.args "-static", "-lprotoc", "-lprotobuf", "-static-libgcc", "-static-libstdc++", "-s"
128 } else {
129 // Link protoc, protobuf, libgcc and libstdc++ statically.
130 // Link other (system) libraries dynamically.
131 // Clang under OSX doesn't support these options.
132 linker.args "-Wl,-Bstatic", "-lprotoc", "-lprotobuf", "-static-libgcc", "-static-libstdc++",
133 "-Wl,-Bdynamic", "-lpthread", "-s"
Eric Anderson2049e0d2015-01-29 12:41:51 -0800134 }
zhangkun83da3c3f82015-03-11 18:03:31 -0700135 addEnvArgs("LDFLAGS", linker.args)
Eric Andersonb938ba52015-02-28 09:59:25 -0800136 } else if (toolChain in VisualCpp) {
137 cppCompiler.args "/EHsc", "/MD"
Kun Zhang2f749712015-05-06 13:10:28 -0700138 if (rootProject.hasProperty('vcProtobufInclude')) {
139 cppCompiler.args "/I${rootProject.vcProtobufInclude}"
Eric Andersonb938ba52015-02-28 09:59:25 -0800140 }
141 linker.args "libprotobuf.lib", "libprotoc.lib"
Kun Zhang2f749712015-05-06 13:10:28 -0700142 if (rootProject.hasProperty('vcProtobufLibs')) {
143 linker.args "/LIBPATH:${rootProject.vcProtobufLibs}"
Eric Andersonb938ba52015-02-28 09:59:25 -0800144 }
zhangkun9de8e4b2015-01-15 10:29:05 -0800145 }
146}
147
zhangkun83da3c3f82015-03-11 18:03:31 -0700148task buildArtifacts(type: Copy) {
Kun Zhang221c5342015-04-29 18:16:15 -0700149 dependsOn 'java_pluginExecutable'
zhangkun83da3c3f82015-03-11 18:03:31 -0700150 from("$buildDir/binaries") {
151 if (osdetector.os != 'windows') {
152 rename 'protoc-gen-grpc-java', '$0.exe'
153 }
154 }
155 into artifactStagingPath
156}
157
158archivesBaseName = "$protocPluginBaseName"
159
160artifacts {
Kun Zhang221c5342015-04-29 18:16:15 -0700161 archives("$artifactStagingPath/java_pluginExecutable/${protocPluginBaseName}.exe" as File) {
162 classifier osdetector.os + "-" + arch
163 type "exe"
164 extension "exe"
165 builtBy buildArtifacts
zhangkun83da3c3f82015-03-11 18:03:31 -0700166 }
167}
168
169// Exe files are skipped by Maven by default. Override it.
170// Also skip jar files that is generated by the java plugin.
171[
172 install.repositories.mavenInstaller,
173 uploadArchives.repositories.mavenDeployer,
174]*.addFilter('all') {artifact, file ->
175 ! (file.getName().endsWith('jar') || file.getName().endsWith('jar.asc'))
176}
177
178[
zhangkun83da3c3f82015-03-11 18:03:31 -0700179 uploadArchives.repositories.mavenDeployer,
180]*.beforeDeployment {
Kun Zhang221c5342015-04-29 18:16:15 -0700181 def ret = exec {
182 executable 'bash'
183 args 'check-artifact.sh', osdetector.os, arch
184 }
185 if (ret.exitValue != 0) {
186 throw new GradleException("check-artifact.sh exited with " + ret.exitValue)
zhangkun83da3c3f82015-03-11 18:03:31 -0700187 }
188}
189
Kun Zhangc5b94c72015-05-06 16:44:55 -0700190project.protocDep = "com.google.protobuf:protoc:${protobufVersion}"
Kun Zhang287a27a2015-05-07 17:32:31 -0700191protobufCodeGenPlugins = ["grpc:$javaPluginPath"]
zhangkun9de8e4b2015-01-15 10:29:05 -0800192
Kun Zhangaf188762015-04-20 09:46:46 -0700193project.afterEvaluate {
Kun Zhang287a27a2015-05-07 17:32:31 -0700194 [generateTestProto, generateTestNanoProto]*.dependsOn 'java_pluginExecutable'
Kun Zhangaf188762015-04-20 09:46:46 -0700195}
196
Kun Zhang287a27a2015-05-07 17:32:31 -0700197test.dependsOn('testGolden', 'testNanoGolden')
Eric Andersonfb28ad22015-01-29 15:00:58 -0800198
Kun Zhang287a27a2015-05-07 17:32:31 -0700199def configureTestTask(Task task, String suffix) {
200 task.dependsOn "generateTest${suffix}Proto"
Kun Zhang4bc2d6d2015-04-17 10:49:11 -0700201 if (osdetector.os != 'windows') {
Kun Zhang287a27a2015-05-07 17:32:31 -0700202 task.executable "diff"
Eric Andersonb938ba52015-02-28 09:59:25 -0800203 } else {
Kun Zhang287a27a2015-05-07 17:32:31 -0700204 task.executable "fc"
Eric Andersonb938ba52015-02-28 09:59:25 -0800205 }
206 // File isn't found on Windows if last slash is forward-slash
207 def slash = System.getProperty("file.separator")
Kun Zhang287a27a2015-05-07 17:32:31 -0700208 task.args "$buildDir/generated-sources/test${suffix}/io/grpc/testing/integration" + slash + "TestServiceGrpc.java",
209 "$projectDir/src/test/golden/TestService${suffix}.java.txt"
zhangkun835e607852015-01-22 12:31:56 -0800210}
Xiao Hangdcff3152015-02-20 15:03:06 -0800211
Kun Zhang287a27a2015-05-07 17:32:31 -0700212task testGolden(type: Exec)
213task testNanoGolden(type: Exec)
214configureTestTask(testGolden, '')
215configureTestTask(testNanoGolden, 'Nano')