blob: fa77d346a9a9f357cedff5bf0cd835b8cfdf37a9 [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 }
Eric Anderson46ce4092016-01-26 12:13:06 -080074
75 binaries {
76 all {
77 if (toolChain in Gcc || toolChain in Clang) {
78 cppCompiler.args "--std=c++0x"
79 addEnvArgs("CXXFLAGS", cppCompiler.args)
80 addEnvArgs("CPPFLAGS", cppCompiler.args)
81 if (osdetector.os == "osx") {
82 cppCompiler.args "-mmacosx-version-min=10.7", "-stdlib=libc++"
83 addLibraryIfNotLinked('protoc', linker.args)
84 addLibraryIfNotLinked('protobuf', linker.args)
85 } else if (osdetector.os == "windows") {
86 linker.args "-static", "-lprotoc", "-lprotobuf", "-static-libgcc", "-static-libstdc++",
87 "-s"
88 } else {
89 // Link protoc, protobuf, libgcc and libstdc++ statically.
90 // Link other (system) libraries dynamically.
91 // Clang under OSX doesn't support these options.
92 linker.args "-Wl,-Bstatic", "-lprotoc", "-lprotobuf", "-static-libgcc",
93 "-static-libstdc++",
94 "-Wl,-Bdynamic", "-lpthread", "-s"
95 }
96 addEnvArgs("LDFLAGS", linker.args)
97 } else if (toolChain in VisualCpp) {
98 cppCompiler.args "/EHsc", "/MT"
99 if (rootProject.hasProperty('vcProtobufInclude')) {
100 cppCompiler.args "/I${rootProject.vcProtobufInclude}"
101 }
102 linker.args "libprotobuf.lib", "libprotoc.lib"
103 if (rootProject.hasProperty('vcProtobufLibs')) {
104 linker.args "/LIBPATH:${rootProject.vcProtobufLibs}"
105 }
106 }
107 }
108 }
zhangkun9de8e4b2015-01-15 10:29:05 -0800109}
110
Kun Zhang287a27a2015-05-07 17:32:31 -0700111configurations {
112 testNanoCompile
113}
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')
Kun Zhang287a27a2015-05-07 17:32:31 -0700118 testNanoCompile project(':grpc-protobuf-nano'),
119 project(':grpc-stub')
120}
121
122sourceSets {
Kun Zhang287a27a2015-05-07 17:32:31 -0700123 testNano {
124 proto {
125 setSrcDirs(['src/test/proto'])
Kun Zhang35f77ee2015-06-22 16:29:30 -0700126 }
127 }
128}
129
130protobuf {
131 protoc {
132 if (project.hasProperty('protoc')) {
133 path = project.protoc
134 } else {
135 artifact = "com.google.protobuf:protoc:${protobufVersion}"
136 }
137 }
138 plugins {
139 grpc {
140 path = javaPluginPath
141 }
142 }
143 generateProtoTasks {
144 all()*.dependsOn 'java_pluginExecutable'
145 ofSourceSet('test')*.plugins {
146 grpc {}
147 }
148 ofSourceSet('testNano').each { task ->
149 task.builtins {
Kun Zhang287a27a2015-05-07 17:32:31 -0700150 remove java
151 javanano {
152 option 'ignore_services=true'
153 }
154 }
Kun Zhang35f77ee2015-06-22 16:29:30 -0700155 task.plugins {
Kun Zhang287a27a2015-05-07 17:32:31 -0700156 grpc {
157 option 'nano=true'
158 }
159 }
160 }
161 }
162}
163
164checkstyleTestNano {
165 source = fileTree(dir: "src/testNano", include: "**/*.java")
Eric Andersonfb28ad22015-01-29 15:00:58 -0800166}
167
Kun Zhangbc347e32015-09-01 15:26:48 -0700168println "*** Building codegen requires Protobuf version ${protobufVersion}"
169println "*** Please refer to https://github.com/grpc/grpc-java/blob/master/COMPILING.md#how-to-build-code-generation-plugin"
170
zhangkun83da3c3f82015-03-11 18:03:31 -0700171task buildArtifacts(type: Copy) {
Kun Zhang221c5342015-04-29 18:16:15 -0700172 dependsOn 'java_pluginExecutable'
zhangkun83da3c3f82015-03-11 18:03:31 -0700173 from("$buildDir/binaries") {
174 if (osdetector.os != 'windows') {
175 rename 'protoc-gen-grpc-java', '$0.exe'
176 }
177 }
178 into artifactStagingPath
179}
180
181archivesBaseName = "$protocPluginBaseName"
182
183artifacts {
Kun Zhang221c5342015-04-29 18:16:15 -0700184 archives("$artifactStagingPath/java_pluginExecutable/${protocPluginBaseName}.exe" as File) {
185 classifier osdetector.os + "-" + arch
186 type "exe"
187 extension "exe"
188 builtBy buildArtifacts
zhangkun83da3c3f82015-03-11 18:03:31 -0700189 }
190}
191
192// Exe files are skipped by Maven by default. Override it.
193// Also skip jar files that is generated by the java plugin.
194[
195 install.repositories.mavenInstaller,
196 uploadArchives.repositories.mavenDeployer,
197]*.addFilter('all') {artifact, file ->
198 ! (file.getName().endsWith('jar') || file.getName().endsWith('jar.asc'))
199}
200
201[
zhangkun83da3c3f82015-03-11 18:03:31 -0700202 uploadArchives.repositories.mavenDeployer,
203]*.beforeDeployment {
Kun Zhang221c5342015-04-29 18:16:15 -0700204 def ret = exec {
205 executable 'bash'
206 args 'check-artifact.sh', osdetector.os, arch
207 }
208 if (ret.exitValue != 0) {
209 throw new GradleException("check-artifact.sh exited with " + ret.exitValue)
zhangkun83da3c3f82015-03-11 18:03:31 -0700210 }
211}
212
Kun Zhang287a27a2015-05-07 17:32:31 -0700213test.dependsOn('testGolden', 'testNanoGolden')
Eric Andersonfb28ad22015-01-29 15:00:58 -0800214
Xudong Maf7f57b72015-09-24 11:16:07 -0700215def configureTestTask(Task task, String suffix, String extraPackage) {
Kun Zhang287a27a2015-05-07 17:32:31 -0700216 task.dependsOn "generateTest${suffix}Proto"
Kun Zhang4bc2d6d2015-04-17 10:49:11 -0700217 if (osdetector.os != 'windows') {
Kun Zhang287a27a2015-05-07 17:32:31 -0700218 task.executable "diff"
Eric Andersonb938ba52015-02-28 09:59:25 -0800219 } else {
Kun Zhang287a27a2015-05-07 17:32:31 -0700220 task.executable "fc"
Eric Andersonb938ba52015-02-28 09:59:25 -0800221 }
222 // File isn't found on Windows if last slash is forward-slash
223 def slash = System.getProperty("file.separator")
Xudong Maf7f57b72015-09-24 11:16:07 -0700224 task.args "$buildDir/generated/source/proto/test${suffix}/grpc/io/grpc/testing/integration${extraPackage}${slash}TestServiceGrpc.java",
Kun Zhang287a27a2015-05-07 17:32:31 -0700225 "$projectDir/src/test/golden/TestService${suffix}.java.txt"
zhangkun835e607852015-01-22 12:31:56 -0800226}
Xiao Hangdcff3152015-02-20 15:03:06 -0800227
Kun Zhang287a27a2015-05-07 17:32:31 -0700228task testGolden(type: Exec)
229task testNanoGolden(type: Exec)
Xudong Maf7f57b72015-09-24 11:16:07 -0700230configureTestTask(testGolden, '', '')
231configureTestTask(testNanoGolden, 'Nano', '/nano')