forked from bblfsh/scala-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sbt
250 lines (190 loc) · 8.03 KB
/
build.sbt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
name := "bblfsh-client"
organization := "org.bblfsh"
git.useGitDescribe := true
enablePlugins(GitVersioning)
scalaVersion := "2.11.11"
val libuastVersion = "3.4.2"
val sdkMajor = "v3"
val sdkVersion = s"${sdkMajor}.1.0"
val protoDir = "src/main/proto"
mainClass in Compile := Some("org.bblfsh.client.cli.ScalaClientCLI")
target in assembly := file("build")
// Where the .h files will be generated by javah from the sbt-jni plugin
target in javah := file("src/main/native")
libraryDependencies ++= Seq(
"org.rogach" %% "scallop" % "3.0.3",
"commons-io" % "commons-io" % "2.5",
"com.thesamet.scalapb" %% "scalapb-runtime" % scalapb.compiler.Version.scalapbVersion % "protobuf",
"com.thesamet.scalapb" %% "scalapb-runtime-grpc" % scalapb.compiler.Version.scalapbVersion,
"io.grpc" % "grpc-netty" % scalapb.compiler.Version.grpcJavaVersion,
"org.scalatest" %% "scalatest" % "3.0.1" % "test"
)
assemblyMergeStrategy in assembly := {
case "META-INF/io.netty.versions.properties" => MergeStrategy.last
case x =>
val oldStrategy = (assemblyMergeStrategy in assembly).value
oldStrategy(x)
}
test in assembly := {}
PB.targets in Compile := Seq(
scalapb.gen() -> (sourceManaged in Compile).value
)
PB.protoSources in Compile := Seq(file(protoDir))
sonatypeProfileName := "org.bblfsh"
// pom settings for sonatype
homepage := Some(url("https://github.com/bblfsh/scala-client"))
scmInfo := Some(ScmInfo(url("https://github.com/bblfsh/scala-client"),
"git@github.com:bblfsh/scala-client.git"))
developers += Developer("juanjux",
"Juanjo Álvarez",
"juanjo@sourced.tech",
url("https://github.com/juanjux"))
developers += Developer("bzz",
"Alexander Bezzubov",
"Alex@sourced.tech",
url("https://github.com/bzz"))
licenses += ("Apache-2.0", url("http://www.apache.org/licenses/LICENSE-2.0"))
pomIncludeRepository := (_ => false)
crossPaths := false
publishMavenStyle := true
exportJars := true
val SONATYPE_USERNAME = scala.util.Properties.envOrElse("SONATYPE_USERNAME", "NOT_SET")
val SONATYPE_PASSWORD = scala.util.Properties.envOrElse("SONATYPE_PASSWORD", "NOT_SET")
credentials += Credentials(
"Sonatype Nexus Repository Manager",
"oss.sonatype.org",
SONATYPE_USERNAME,
SONATYPE_PASSWORD)
val SONATYPE_PASSPHRASE = scala.util.Properties.envOrElse("SONATYPE_PASSPHRASE", "not set")
val JAVA_HOME = scala.util.Properties.envOrElse("JAVA_HOME", "/usr/lib/jvm/java-8-openjdk-amd64")
val CPP_FLAGS = "-shared -Wall -fPIC -O2 -std=c++11"
val GCC_FLAGS = "-Wl,-Bsymbolic"
useGpg := false
pgpSecretRing := baseDirectory.value / "project" / ".gnupg" / "secring.gpg"
pgpPublicRing := baseDirectory.value / "project" / ".gnupg" / "pubring.gpg"
pgpPassphrase := Some(SONATYPE_PASSPHRASE.toArray)
isSnapshot := version.value endsWith "SNAPSHOT"
publishTo := {
val nexus = "https://oss.sonatype.org/"
if (isSnapshot.value)
Some("snapshots" at nexus + "content/repositories/snapshots")
else
Some("releases" at nexus + "service/local/staging/deploy/maven2")
}
val getProtoFiles = TaskKey[Unit]("getProtoFiles", "Retrieve protobuf files")
getProtoFiles := {
import sys.process._
println(s"Downloading and installing SDK$sdkMajor protocol buffer files...")
val bblfshProto = s"${protoDir}/github.com/bblfsh"
val sdkProto = s"${bblfshProto}/sdk/${sdkMajor}"
s"mkdir -p ${sdkProto}/protocol" !
s"mkdir -p ${sdkProto}/uast/role" !
val unzip_dir = "sdk-" + sdkVersion.substring(1)
s"curl -SL https://github.com/bblfsh/sdk/archive/${sdkVersion}.tar.gz" #| "tar xz" #&&
s"cp ${unzip_dir}/protocol/driver.proto ${sdkProto}/protocol/" #&&
s"cp ${unzip_dir}/uast/role/generated.proto ${sdkProto}/uast/role" #&&
s"rm -rf ${unzip_dir}" !
println("Done unpacking SDK")
}
val getLibuast = TaskKey[Unit]("getLibuast", "Retrieve libuast")
getLibuast := {
val os = if (System.getProperty("os.name").toLowerCase.contains("mac os x")) "darwin" else "linux"
downloadUnpackLibuast(os)
}
def downloadUnpackLibuast(os: String) = {
import sys.process._
val ghUrl = "https://github.com/bblfsh/libuast"
val binaryReleaseUrl = s"${ghUrl}/releases/download/v${libuastVersion}/libuast-${os}-amd64.tar.gz"
println(s"Downloading libuast binary from ${binaryReleaseUrl}")
s"curl -sL ${binaryReleaseUrl} -o libuast-bin.tar.gz" #&&
"tar xzf libuast-bin.tar.gz" #&&
s"mv ${os}-amd64 libuast" #&&
"mkdir -p src/main/resources" #&&
"rm -rf src/main/resources/libuast" #&&
"mv libuast src/main/resources" #&&
"rm -f src/main/resources/libuast/libuast.so" #&& // always a static build
"rm -f src/main/resources/libuast/libuast.dylib" #&&
"rm libuast-bin.tar.gz" !
"find src/main/resources"!
"nm src/main/resources/libuast/libuast.a" #| "grep -c UastDecode"!
"nm src/main/resources/libuast/libuast.a" #| "wc -l"!
println(s"Done unpacking libuast for ${os}")
}
val compileScalaLibuast = TaskKey[Unit]("compileScalaLibuast", "Compile libScalaUast JNI library")
compileScalaLibuast := {
import sys.process._
println("Compiling libuast bindings...")
"mkdir -p ./src/main/resources/lib/" !
val nativeSourceFiles = "src/main/native/org_bblfsh_client_v2_libuast_Libuast.cc " +
"src/main/native/jni_utils.cc "
compileUnix(nativeSourceFiles)
crossCompileMacOS(nativeSourceFiles)
}
def compileUnix(sourceFiles: String) = {
import sys.process._
val osName = System.getProperty("os.name").toLowerCase()
if (osName.contains("mac os x")) {
val cmd:String = "g++" + " " + GCC_FLAGS + " " + CPP_FLAGS + " " +
"-I/usr/include " +
"-I" + JAVA_HOME + "/include/ " +
"-I" + JAVA_HOME + "/include/darwin " +
"-Isrc/main/resources/libuast " +
"-o src/main/resources/lib/libscalauast.dylib " + // sic, must be in the classpath for the test
sourceFiles +
"src/main/resources/libuast/libuast.a "
checkedProcess(cmd, "macOS build")
} else {
val cmd:String = "g++" + " " + GCC_FLAGS + " " + CPP_FLAGS + " " +
"-I/usr/include " +
"-I" + JAVA_HOME + "/include/ " +
"-I" + JAVA_HOME + "/include/linux " +
"-Isrc/main/resources/libuast " +
"-o src/main/resources/lib/libscalauast.so " +
sourceFiles +
"src/main/resources/libuast/libuast.a "
checkedProcess(cmd, "Linux build")
"nm src/main/resources/lib/libscalauast.so" #| "grep -c UastDecode"!
}
}
def crossCompileMacOS(sourceFiles: String): Unit = {
val osName = System.getProperty("os.name").toLowerCase()
if (osName.contains("mac os x")) {
println("Skipping cross-compilation for macOS on macOS")
return
}
val osxHome = System.getenv("OSXCROSS_PATH")
// This is defined in .travis.yml
val sdkVersion = System.getenv("SDK_VERSION")
if (osxHome == null || osxHome.isEmpty) {
println("OSXCROSS_PATH variable not defined, not cross-compiling for macOS")
return
}
if (sdkVersion == null || sdkVersion.isEmpty) {
println("SDK_VERSION variable not defined, it should be to cross-compile for macOS")
return
}
downloadUnpackLibuast("darwin")
val cmd = osxHome + "/bin/o64-clang++-libc++" + " " + CPP_FLAGS + " " +
"-I" + osxHome + s"/SDK/MacOSX${sdkVersion}.sdk/usr/include/ " +
"-I" + JAVA_HOME + "/include " +
"-I" + JAVA_HOME + "/include/linux " +
"-Isrc/main/resources/libuast " +
"-o src/main/resources/lib/libscalauast.dylib " +
sourceFiles +
"src/main/resources/libuast/libuast.a "
checkedProcess(cmd, "macOS cross-compile build")
}
def checkedProcess(cmd: String, name: String) {
import sys.process._
println(cmd)
val out = cmd !
if (out != 0) {
throw new IllegalStateException(name + " failed (see previous messages)")
}
}
cleanFiles ++= Seq(
baseDirectory.value / "src/main/resources/libuast",
baseDirectory.value / "src/main/resources/lib",
baseDirectory.value / s"${protoDir}/github.com/bblfsh"
)
mainClass := Def.sequential(getProtoFiles, getLibuast, compileScalaLibuast, (mainClass in Compile)).value