blob: 738b4e1528264cc3bcb2cdbcceb6378cbee43370 [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 {
Kun Zhang287a27a2015-05-07 17:32:31 -070088 testNano {
89 proto {
90 setSrcDirs(['src/test/proto'])
Kun Zhang35f77ee2015-06-22 16:29:30 -070091 }
92 }
93}
94
95protobuf {
96 protoc {
97 if (project.hasProperty('protoc')) {
98 path = project.protoc
99 } else {
100 artifact = "com.google.protobuf:protoc:${protobufVersion}"
101 }
102 }
103 plugins {
104 grpc {
105 path = javaPluginPath
106 }
107 }
108 generateProtoTasks {
109 all()*.dependsOn 'java_pluginExecutable'
110 ofSourceSet('test')*.plugins {
111 grpc {}
112 }
113 ofSourceSet('testNano').each { task ->
114 task.builtins {
Kun Zhang287a27a2015-05-07 17:32:31 -0700115 remove java
116 javanano {
117 option 'ignore_services=true'
118 }
119 }
Kun Zhang35f77ee2015-06-22 16:29:30 -0700120 task.plugins {
Kun Zhang287a27a2015-05-07 17:32:31 -0700121 grpc {
122 option 'nano=true'
123 }
124 }
125 }
126 }
127}
128
129checkstyleTestNano {
130 source = fileTree(dir: "src/testNano", include: "**/*.java")
Eric Andersonfb28ad22015-01-29 15:00:58 -0800131}
132
Kun Zhangbc347e32015-09-01 15:26:48 -0700133println "*** Building codegen requires Protobuf version ${protobufVersion}"
134println "*** Please refer to https://github.com/grpc/grpc-java/blob/master/COMPILING.md#how-to-build-code-generation-plugin"
135
zhangkun9de8e4b2015-01-15 10:29:05 -0800136binaries.all {
nmittlerf1299602015-01-30 14:55:38 -0800137 if (toolChain in Gcc || toolChain in Clang) {
Louis Ryanc42c8c42015-03-18 16:31:38 -0700138 cppCompiler.args "--std=c++0x"
zhangkun83da3c3f82015-03-11 18:03:31 -0700139 addEnvArgs("CXXFLAGS", cppCompiler.args)
140 addEnvArgs("CPPFLAGS", cppCompiler.args)
141 if (osdetector.os == "osx") {
142 cppCompiler.args "-mmacosx-version-min=10.7", "-stdlib=libc++"
Kun Zhang0c2ea152015-04-17 12:40:42 -0700143 addLibraryIfNotLinked('protoc', linker.args)
144 addLibraryIfNotLinked('protobuf', linker.args)
zhangkun83da3c3f82015-03-11 18:03:31 -0700145 } else if (osdetector.os == "windows") {
146 linker.args "-static", "-lprotoc", "-lprotobuf", "-static-libgcc", "-static-libstdc++", "-s"
147 } else {
148 // Link protoc, protobuf, libgcc and libstdc++ statically.
149 // Link other (system) libraries dynamically.
150 // Clang under OSX doesn't support these options.
151 linker.args "-Wl,-Bstatic", "-lprotoc", "-lprotobuf", "-static-libgcc", "-static-libstdc++",
152 "-Wl,-Bdynamic", "-lpthread", "-s"
Eric Anderson2049e0d2015-01-29 12:41:51 -0800153 }
zhangkun83da3c3f82015-03-11 18:03:31 -0700154 addEnvArgs("LDFLAGS", linker.args)
Eric Andersonb938ba52015-02-28 09:59:25 -0800155 } else if (toolChain in VisualCpp) {
156 cppCompiler.args "/EHsc", "/MD"
Kun Zhang2f749712015-05-06 13:10:28 -0700157 if (rootProject.hasProperty('vcProtobufInclude')) {
158 cppCompiler.args "/I${rootProject.vcProtobufInclude}"
Eric Andersonb938ba52015-02-28 09:59:25 -0800159 }
160 linker.args "libprotobuf.lib", "libprotoc.lib"
Kun Zhang2f749712015-05-06 13:10:28 -0700161 if (rootProject.hasProperty('vcProtobufLibs')) {
162 linker.args "/LIBPATH:${rootProject.vcProtobufLibs}"
Eric Andersonb938ba52015-02-28 09:59:25 -0800163 }
zhangkun9de8e4b2015-01-15 10:29:05 -0800164 }
165}
166
zhangkun83da3c3f82015-03-11 18:03:31 -0700167task buildArtifacts(type: Copy) {
Kun Zhang221c5342015-04-29 18:16:15 -0700168 dependsOn 'java_pluginExecutable'
zhangkun83da3c3f82015-03-11 18:03:31 -0700169 from("$buildDir/binaries") {
170 if (osdetector.os != 'windows') {
171 rename 'protoc-gen-grpc-java', '$0.exe'
172 }
173 }
174 into artifactStagingPath
175}
176
177archivesBaseName = "$protocPluginBaseName"
178
179artifacts {
Kun Zhang221c5342015-04-29 18:16:15 -0700180 archives("$artifactStagingPath/java_pluginExecutable/${protocPluginBaseName}.exe" as File) {
181 classifier osdetector.os + "-" + arch
182 type "exe"
183 extension "exe"
184 builtBy buildArtifacts
zhangkun83da3c3f82015-03-11 18:03:31 -0700185 }
186}
187
188// Exe files are skipped by Maven by default. Override it.
189// Also skip jar files that is generated by the java plugin.
190[
191 install.repositories.mavenInstaller,
192 uploadArchives.repositories.mavenDeployer,
193]*.addFilter('all') {artifact, file ->
194 ! (file.getName().endsWith('jar') || file.getName().endsWith('jar.asc'))
195}
196
197[
zhangkun83da3c3f82015-03-11 18:03:31 -0700198 uploadArchives.repositories.mavenDeployer,
199]*.beforeDeployment {
Kun Zhang221c5342015-04-29 18:16:15 -0700200 def ret = exec {
201 executable 'bash'
202 args 'check-artifact.sh', osdetector.os, arch
203 }
204 if (ret.exitValue != 0) {
205 throw new GradleException("check-artifact.sh exited with " + ret.exitValue)
zhangkun83da3c3f82015-03-11 18:03:31 -0700206 }
207}
208
Kun Zhang287a27a2015-05-07 17:32:31 -0700209test.dependsOn('testGolden', 'testNanoGolden')
Eric Andersonfb28ad22015-01-29 15:00:58 -0800210
Kun Zhang287a27a2015-05-07 17:32:31 -0700211def configureTestTask(Task task, String suffix) {
212 task.dependsOn "generateTest${suffix}Proto"
Kun Zhang4bc2d6d2015-04-17 10:49:11 -0700213 if (osdetector.os != 'windows') {
Kun Zhang287a27a2015-05-07 17:32:31 -0700214 task.executable "diff"
Eric Andersonb938ba52015-02-28 09:59:25 -0800215 } else {
Kun Zhang287a27a2015-05-07 17:32:31 -0700216 task.executable "fc"
Eric Andersonb938ba52015-02-28 09:59:25 -0800217 }
218 // File isn't found on Windows if last slash is forward-slash
219 def slash = System.getProperty("file.separator")
Kun Zhang35f77ee2015-06-22 16:29:30 -0700220 task.args "$buildDir/generated/source/proto/test${suffix}/grpc/io/grpc/testing/integration${slash}TestServiceGrpc.java",
Kun Zhang287a27a2015-05-07 17:32:31 -0700221 "$projectDir/src/test/golden/TestService${suffix}.java.txt"
zhangkun835e607852015-01-22 12:31:56 -0800222}
Xiao Hangdcff3152015-02-20 15:03:06 -0800223
Kun Zhang287a27a2015-05-07 17:32:31 -0700224task testGolden(type: Exec)
225task testNanoGolden(type: Exec)
226configureTestTask(testGolden, '')
227configureTestTask(testNanoGolden, 'Nano')