blob: 4efa26172eae1d48879aad68232c53d9c6cd740a [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
Eric Andersonfb28ad22015-01-29 15:00:58 -080076dependencies {
Eric Andersone23f8992015-04-10 15:40:44 -070077 testCompile project(':grpc-protobuf'),
78 project(':grpc-stub')
Eric Andersonfb28ad22015-01-29 15:00:58 -080079}
80
zhangkun9de8e4b2015-01-15 10:29:05 -080081binaries.all {
nmittlerf1299602015-01-30 14:55:38 -080082 if (toolChain in Gcc || toolChain in Clang) {
Louis Ryanc42c8c42015-03-18 16:31:38 -070083 cppCompiler.args "--std=c++0x"
zhangkun83da3c3f82015-03-11 18:03:31 -070084 addEnvArgs("CXXFLAGS", cppCompiler.args)
85 addEnvArgs("CPPFLAGS", cppCompiler.args)
86 if (osdetector.os == "osx") {
87 cppCompiler.args "-mmacosx-version-min=10.7", "-stdlib=libc++"
Kun Zhang0c2ea152015-04-17 12:40:42 -070088 addLibraryIfNotLinked('protoc', linker.args)
89 addLibraryIfNotLinked('protobuf', linker.args)
zhangkun83da3c3f82015-03-11 18:03:31 -070090 } else if (osdetector.os == "windows") {
91 linker.args "-static", "-lprotoc", "-lprotobuf", "-static-libgcc", "-static-libstdc++", "-s"
92 } else {
93 // Link protoc, protobuf, libgcc and libstdc++ statically.
94 // Link other (system) libraries dynamically.
95 // Clang under OSX doesn't support these options.
96 linker.args "-Wl,-Bstatic", "-lprotoc", "-lprotobuf", "-static-libgcc", "-static-libstdc++",
97 "-Wl,-Bdynamic", "-lpthread", "-s"
Eric Anderson2049e0d2015-01-29 12:41:51 -080098 }
zhangkun83da3c3f82015-03-11 18:03:31 -070099 addEnvArgs("LDFLAGS", linker.args)
Eric Andersonb938ba52015-02-28 09:59:25 -0800100 } else if (toolChain in VisualCpp) {
101 cppCompiler.args "/EHsc", "/MD"
Kun Zhang2f749712015-05-06 13:10:28 -0700102 if (rootProject.hasProperty('vcProtobufInclude')) {
103 cppCompiler.args "/I${rootProject.vcProtobufInclude}"
Eric Andersonb938ba52015-02-28 09:59:25 -0800104 }
105 linker.args "libprotobuf.lib", "libprotoc.lib"
Kun Zhang2f749712015-05-06 13:10:28 -0700106 if (rootProject.hasProperty('vcProtobufLibs')) {
107 linker.args "/LIBPATH:${rootProject.vcProtobufLibs}"
Eric Andersonb938ba52015-02-28 09:59:25 -0800108 }
zhangkun9de8e4b2015-01-15 10:29:05 -0800109 }
110}
111
zhangkun83da3c3f82015-03-11 18:03:31 -0700112task buildArtifacts(type: Copy) {
Kun Zhang221c5342015-04-29 18:16:15 -0700113 dependsOn 'java_pluginExecutable'
zhangkun83da3c3f82015-03-11 18:03:31 -0700114 from("$buildDir/binaries") {
115 if (osdetector.os != 'windows') {
116 rename 'protoc-gen-grpc-java', '$0.exe'
117 }
118 }
119 into artifactStagingPath
120}
121
122archivesBaseName = "$protocPluginBaseName"
123
124artifacts {
Kun Zhang221c5342015-04-29 18:16:15 -0700125 archives("$artifactStagingPath/java_pluginExecutable/${protocPluginBaseName}.exe" as File) {
126 classifier osdetector.os + "-" + arch
127 type "exe"
128 extension "exe"
129 builtBy buildArtifacts
zhangkun83da3c3f82015-03-11 18:03:31 -0700130 }
131}
132
133// Exe files are skipped by Maven by default. Override it.
134// Also skip jar files that is generated by the java plugin.
135[
136 install.repositories.mavenInstaller,
137 uploadArchives.repositories.mavenDeployer,
138]*.addFilter('all') {artifact, file ->
139 ! (file.getName().endsWith('jar') || file.getName().endsWith('jar.asc'))
140}
141
142[
zhangkun83da3c3f82015-03-11 18:03:31 -0700143 uploadArchives.repositories.mavenDeployer,
144]*.beforeDeployment {
Kun Zhang221c5342015-04-29 18:16:15 -0700145 def ret = exec {
146 executable 'bash'
147 args 'check-artifact.sh', osdetector.os, arch
148 }
149 if (ret.exitValue != 0) {
150 throw new GradleException("check-artifact.sh exited with " + ret.exitValue)
zhangkun83da3c3f82015-03-11 18:03:31 -0700151 }
152}
153
Kun Zhangc5b94c72015-05-06 16:44:55 -0700154project.protocDep = "com.google.protobuf:protoc:${protobufVersion}"
Eric Andersonb938ba52015-02-28 09:59:25 -0800155protobufCodeGenPlugins = ["java_plugin:$javaPluginPath"]
zhangkun9de8e4b2015-01-15 10:29:05 -0800156
Kun Zhangaf188762015-04-20 09:46:46 -0700157project.afterEvaluate {
Kun Zhang221c5342015-04-29 18:16:15 -0700158 generateTestProto.dependsOn 'java_pluginExecutable'
Kun Zhangaf188762015-04-20 09:46:46 -0700159}
160
Eric Andersonb938ba52015-02-28 09:59:25 -0800161// Ignore test for the moment on Windows. It will be easier to run once the
162// gradle protobuf plugin can support nano.
Kun Zhang4bc2d6d2015-04-17 10:49:11 -0700163if (osdetector.os != 'windows') {
Eric Andersonb938ba52015-02-28 09:59:25 -0800164 test.dependsOn('testGolden','testNanoGolden')
165}
Eric Andersonfb28ad22015-01-29 15:00:58 -0800166
167task testGolden(type: Exec, dependsOn: 'generateTestProto') {
Kun Zhang4bc2d6d2015-04-17 10:49:11 -0700168 if (osdetector.os != 'windows') {
Eric Andersonb938ba52015-02-28 09:59:25 -0800169 executable "diff"
170 } else {
171 executable "fc"
172 }
173 // File isn't found on Windows if last slash is forward-slash
174 def slash = System.getProperty("file.separator")
175 args "$buildDir/generated-sources/test/io/grpc/testing/integration" + slash + "TestServiceGrpc.java",
Eric Andersonfb28ad22015-01-29 15:00:58 -0800176 "$projectDir/src/test/golden/TestService.java.txt"
zhangkun835e607852015-01-22 12:31:56 -0800177}
Xiao Hangdcff3152015-02-20 15:03:06 -0800178
Kun Zhang221c5342015-04-29 18:16:15 -0700179task testNanoGolden(type: Exec, dependsOn: 'java_pluginExecutable') {
Jakob Buchgraberdf321fe2015-02-25 19:47:05 -0800180 doFirst {
181 temporaryDir.createNewFile();
182 }
183
zsurocking5902c6a2015-02-25 20:54:53 -0800184 environment 'TEST_TMP_DIR', temporaryDir
185 commandLine './src/test/run_nano_test.sh'
186}