blob: d25f728dc3fbf3dd0a2a8a74ef036cd9dbb9238b [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 Zhang221c5342015-04-29 18:16:15 -070038def String arch = osdetector.arch
39if (System.getProperty('arch')) {
40 arch = System.getProperty('arch')
41}
42
zhangkun83da3c3f82015-03-11 18:03:31 -070043model {
44 toolChains {
Kun Zhang221c5342015-04-29 18:16:15 -070045 // If you have both VC and Gcc installed, VC will be selected, unless you
46 // use '-Dvc.disable'
47 if (System.getProperty('vc.disable') == null) {
48 visualCpp(VisualCpp) {
49 }
50 }
zhangkun83da3c3f82015-03-11 18:03:31 -070051 gcc(Gcc) {
zhangkun83da3c3f82015-03-11 18:03:31 -070052 }
53 clang(Clang) {
zhangkun83da3c3f82015-03-11 18:03:31 -070054 }
55 }
Kun Zhang221c5342015-04-29 18:16:15 -070056
zhangkun83da3c3f82015-03-11 18:03:31 -070057 platforms {
58 x86_32 {
Kun Zhang221c5342015-04-29 18:16:15 -070059 architecture "x86"
zhangkun83da3c3f82015-03-11 18:03:31 -070060 }
61 x86_64 {
62 architecture "x86_64"
63 }
zhangkun83da3c3f82015-03-11 18:03:31 -070064 }
65
66 components {
67 java_plugin(NativeExecutableSpec) {
Kun Zhang221c5342015-04-29 18:16:15 -070068 if (arch in ['x86_32', 'x86_64']) {
69 // If arch is not within the defined platforms, we do not specify the
70 // targetPlatform so that Gradle will choose what is appropriate.
71 targetPlatform arch
zhangkun83da3c3f82015-03-11 18:03:31 -070072 }
73 baseName "$protocPluginBaseName"
74 }
Kun Zhang90706dc2015-04-09 13:11:31 -070075 }
zhangkun9de8e4b2015-01-15 10:29:05 -080076}
77
Eric Andersonfb28ad22015-01-29 15:00:58 -080078dependencies {
Eric Andersone23f8992015-04-10 15:40:44 -070079 testCompile project(':grpc-protobuf'),
80 project(':grpc-stub')
Eric Andersonfb28ad22015-01-29 15:00:58 -080081}
82
zhangkun9de8e4b2015-01-15 10:29:05 -080083binaries.all {
nmittlerf1299602015-01-30 14:55:38 -080084 if (toolChain in Gcc || toolChain in Clang) {
Louis Ryanc42c8c42015-03-18 16:31:38 -070085 cppCompiler.args "--std=c++0x"
zhangkun83da3c3f82015-03-11 18:03:31 -070086 addEnvArgs("CXXFLAGS", cppCompiler.args)
87 addEnvArgs("CPPFLAGS", cppCompiler.args)
88 if (osdetector.os == "osx") {
89 cppCompiler.args "-mmacosx-version-min=10.7", "-stdlib=libc++"
Kun Zhang0c2ea152015-04-17 12:40:42 -070090 addLibraryIfNotLinked('protoc', linker.args)
91 addLibraryIfNotLinked('protobuf', linker.args)
zhangkun83da3c3f82015-03-11 18:03:31 -070092 } else if (osdetector.os == "windows") {
93 linker.args "-static", "-lprotoc", "-lprotobuf", "-static-libgcc", "-static-libstdc++", "-s"
94 } else {
95 // Link protoc, protobuf, libgcc and libstdc++ statically.
96 // Link other (system) libraries dynamically.
97 // Clang under OSX doesn't support these options.
98 linker.args "-Wl,-Bstatic", "-lprotoc", "-lprotobuf", "-static-libgcc", "-static-libstdc++",
99 "-Wl,-Bdynamic", "-lpthread", "-s"
Eric Anderson2049e0d2015-01-29 12:41:51 -0800100 }
zhangkun83da3c3f82015-03-11 18:03:31 -0700101 addEnvArgs("LDFLAGS", linker.args)
Eric Andersonb938ba52015-02-28 09:59:25 -0800102 } else if (toolChain in VisualCpp) {
103 cppCompiler.args "/EHsc", "/MD"
Kun Zhang221c5342015-04-29 18:16:15 -0700104 if (rootProject.hasProperty('vc.protobuf.include')) {
105 cppCompiler.args "/I" + rootProject.properties['vc.protobuf.include']
Eric Andersonb938ba52015-02-28 09:59:25 -0800106 }
107 linker.args "libprotobuf.lib", "libprotoc.lib"
Kun Zhang221c5342015-04-29 18:16:15 -0700108 if (rootProject.hasProperty('vc.protobuf.libs')) {
109 linker.args "/LIBPATH:" + rootProject.properties['vc.protobuf.libs']
Eric Andersonb938ba52015-02-28 09:59:25 -0800110 }
zhangkun9de8e4b2015-01-15 10:29:05 -0800111 }
112}
113
zhangkun83da3c3f82015-03-11 18:03:31 -0700114task buildArtifacts(type: Copy) {
Kun Zhang221c5342015-04-29 18:16:15 -0700115 dependsOn 'java_pluginExecutable'
zhangkun83da3c3f82015-03-11 18:03:31 -0700116 from("$buildDir/binaries") {
117 if (osdetector.os != 'windows') {
118 rename 'protoc-gen-grpc-java', '$0.exe'
119 }
120 }
121 into artifactStagingPath
122}
123
124archivesBaseName = "$protocPluginBaseName"
125
126artifacts {
Kun Zhang221c5342015-04-29 18:16:15 -0700127 archives("$artifactStagingPath/java_pluginExecutable/${protocPluginBaseName}.exe" as File) {
128 classifier osdetector.os + "-" + arch
129 type "exe"
130 extension "exe"
131 builtBy buildArtifacts
zhangkun83da3c3f82015-03-11 18:03:31 -0700132 }
133}
134
135// Exe files are skipped by Maven by default. Override it.
136// Also skip jar files that is generated by the java plugin.
137[
138 install.repositories.mavenInstaller,
139 uploadArchives.repositories.mavenDeployer,
140]*.addFilter('all') {artifact, file ->
141 ! (file.getName().endsWith('jar') || file.getName().endsWith('jar.asc'))
142}
143
144[
zhangkun83da3c3f82015-03-11 18:03:31 -0700145 uploadArchives.repositories.mavenDeployer,
146]*.beforeDeployment {
Kun Zhang221c5342015-04-29 18:16:15 -0700147 def ret = exec {
148 executable 'bash'
149 args 'check-artifact.sh', osdetector.os, arch
150 }
151 if (ret.exitValue != 0) {
152 throw new GradleException("check-artifact.sh exited with " + ret.exitValue)
zhangkun83da3c3f82015-03-11 18:03:31 -0700153 }
154}
155
Eric Andersonb938ba52015-02-28 09:59:25 -0800156protobufCodeGenPlugins = ["java_plugin:$javaPluginPath"]
zhangkun9de8e4b2015-01-15 10:29:05 -0800157
Kun Zhangaf188762015-04-20 09:46:46 -0700158project.afterEvaluate {
Kun Zhang221c5342015-04-29 18:16:15 -0700159 generateTestProto.dependsOn 'java_pluginExecutable'
Kun Zhangaf188762015-04-20 09:46:46 -0700160}
161
Eric Andersonb938ba52015-02-28 09:59:25 -0800162// Ignore test for the moment on Windows. It will be easier to run once the
163// gradle protobuf plugin can support nano.
Kun Zhang4bc2d6d2015-04-17 10:49:11 -0700164if (osdetector.os != 'windows') {
Eric Andersonb938ba52015-02-28 09:59:25 -0800165 test.dependsOn('testGolden','testNanoGolden')
166}
Eric Andersonfb28ad22015-01-29 15:00:58 -0800167
168task testGolden(type: Exec, dependsOn: 'generateTestProto') {
Kun Zhang4bc2d6d2015-04-17 10:49:11 -0700169 if (osdetector.os != 'windows') {
Eric Andersonb938ba52015-02-28 09:59:25 -0800170 executable "diff"
171 } else {
172 executable "fc"
173 }
174 // File isn't found on Windows if last slash is forward-slash
175 def slash = System.getProperty("file.separator")
176 args "$buildDir/generated-sources/test/io/grpc/testing/integration" + slash + "TestServiceGrpc.java",
Eric Andersonfb28ad22015-01-29 15:00:58 -0800177 "$projectDir/src/test/golden/TestService.java.txt"
zhangkun835e607852015-01-22 12:31:56 -0800178}
Xiao Hangdcff3152015-02-20 15:03:06 -0800179
Kun Zhang221c5342015-04-29 18:16:15 -0700180task testNanoGolden(type: Exec, dependsOn: 'java_pluginExecutable') {
Jakob Buchgraberdf321fe2015-02-25 19:47:05 -0800181 doFirst {
182 temporaryDir.createNewFile();
183 }
184
zsurocking5902c6a2015-02-25 20:54:53 -0800185 environment 'TEST_TMP_DIR', temporaryDir
186 commandLine './src/test/run_nano_test.sh'
187}