Skip to content
This repository was archived by the owner on Mar 8, 2020. It is now read-only.

Commit bc7f973

Browse files
authored
Merge pull request #84 from bzz/migrate-to-sdk-v3
Migrate to SDK.v3 - protocol.v2 with decode
2 parents 387b287 + f023d7c commit bc7f973

23 files changed

+746
-708
lines changed

.clang-format

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
BasedOnStyle: Google
2+
AllowShortIfStatementsOnASingleLine: true

.travis.yml

Lines changed: 50 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,66 @@
11
dist: trusty
2-
sudo: false
3-
42
language: scala
5-
services:
6-
- docker
7-
83
jdk:
94
- openjdk8
10-
115
scala:
126
- 2.11.11
137

8+
services:
9+
- docker
10+
1411
cache:
1512
directories:
1613
- $HOME/.ivy2
1714
- $HOME/.sbt
1815

16+
stages:
17+
- name: test
18+
- name: release
19+
if: tag IS present
1920

20-
before_install:
21-
- docker run --privileged -d -p 9432:9432 --name bblfsh bblfsh/bblfshd
22-
- docker exec -it bblfsh bblfshctl driver install --recommended
23-
- sudo apt-get update
24-
- sudo apt-get install -y --no-install-recommends clang g++ gcc gcc-multilib libc6-dev libc6-dev-i386 mingw-w64 patch xz-utils libxml2-dev
25-
26-
env:
27-
global:
28-
- OSXCROSS_PATH="$HOME/osxcross"
29-
- OSXCROSS_REV=3034f7149716d815bc473d0a7b35d17e4cf175aa
30-
- SDK_VERSION=10.11
31-
- DARWIN_VERSION=15
32-
- OSX_VERSION_MIN=10.6
33-
- OSXCROSS_SDK_URL="https://s3.dockerproject.org/darwin/v2/MacOSX10.11.sdk.tar.xz"
3421

35-
install:
36-
- mkdir -p /tmp/osxcross
37-
- cd /tmp/osxcross
38-
- curl -sSL "https://codeload.github.com/tpoechtrager/osxcross/tar.gz/${OSXCROSS_REV}" | tar -C /tmp/osxcross --strip=1 -xzf -
39-
- curl -s -S -L -o tarballs/MacOSX${SDK_VERSION}.sdk.tar.xz ${OSXCROSS_SDK_URL}
40-
- UNATTENDED=yes ./build.sh >/dev/null
41-
- mv target "${OSXCROSS_PATH}"
42-
- curl -S -L "https://github.com/karalabe/xgo/blob/647f256c447ee20f9bf13ebc42e612d55994a383/docker/base/patch.tar.xz?raw=true" | xz -dc - | tar -xf -
43-
- mv v1 "${OSXCROSS_PATH}/SDK/MacOSX${SDK_VERSION}.sdk/usr/include/c++/v1"
44-
- rm -rf /tmp/osxcross "${OSXCROSS_PATH}/SDK/MacOSX${SDK_VERSION}.sdk/usr/share/man"
22+
jobs:
23+
include:
24+
- name: 'All tests'
25+
stage: test
26+
if: tag IS present # TODO(bzz): enable on PRs as soon as migrated to V2
27+
install: &test_setup_anchor
28+
- docker run --privileged -d -p 9432:9432 --name bblfsh bblfsh/bblfshd
29+
- docker exec -it bblfsh bblfshctl driver install --recommended
30+
- sudo apt-get update
31+
- sudo apt-get install -y --no-install-recommends clang g++ gcc gcc-multilib libc6-dev libc6-dev-i386 mingw-w64 patch xz-utils
32+
script:
33+
- ./sbt assembly test
34+
after_failure: &failure_logs_anchor
35+
- docker logs bblfsh
4536

46-
script:
47-
- cd $TRAVIS_BUILD_DIR
48-
- ./sbt assembly test
49-
- if [[ -z "$TRAVIS_TAG" ]]; then echo "Skipping this build for non-tag builds."; exit 0; fi
50-
- ./sbt publishLocal
51-
- openssl aes-256-cbc -K $encrypted_97aef7f4ae04_key -iv $encrypted_97aef7f4ae04_iv -in key.asc.enc -out key.asc -d
52-
- gpg --no-default-keyring --primary-keyring ./project/.gnupg/pubring.gpg --secret-keyring ./project/.gnupg/secring.gpg --keyring ./project/.gnupg/pubring.gpg --fingerprint --import key.asc
53-
- ./sbt publishSigned
54-
- ./sbt sonatypeRelease
37+
- name: 'V2: passing tests' # TODO(#83): remove, after both tests sets converge
38+
install: *test_setup_anchor
39+
script:
40+
- sudo apt-get install -y binutils
41+
- ./sbt assembly
42+
- ./sbt "testOnly *Close* *ClientVersion* *SupportedLanguages*"
43+
- ./sbt "testOnly org.bblfsh.client.v2.BblfshClientParseTest -- -z \"Decoded UAST after parsing\""
44+
- ./sbt "testOnly org.bblfsh.client.v2.BblfshClientParseTest -- -z \"Decoded UAST RootNode\""
45+
after_failure: *failure_logs_anchor
5546

56-
after_failure:
57-
- docker logs bblfsh
47+
- name: 'Cross-compile, release & publish to Sonatype'
48+
stage: release
49+
before_install:
50+
- mkdir -p /tmp/osxcross
51+
- cd /tmp/osxcross
52+
- curl -sSL "https://codeload.github.com/tpoechtrager/osxcross/tar.gz/${OSXCROSS_REV}" | tar -C /tmp/osxcross --strip=1 -xzf -
53+
- curl -s -S -L -o tarballs/MacOSX${SDK_VERSION}.sdk.tar.xz ${OSXCROSS_SDK_URL}
54+
- UNATTENDED=yes ./build.sh >/dev/null
55+
- mv target "${OSXCROSS_PATH}"
56+
- curl -S -L "https://github.com/karalabe/xgo/blob/647f256c447ee20f9bf13ebc42e612d55994a383/docker/base/patch.tar.xz?raw=true" | xz -dc - | tar -xf -
57+
- mv v1 "${OSXCROSS_PATH}/SDK/MacOSX${SDK_VERSION}.sdk/usr/include/c++/v1"
58+
- rm -rf /tmp/osxcross "${OSXCROSS_PATH}/SDK/MacOSX${SDK_VERSION}.sdk/usr/share/man"
59+
script:
60+
- cd $TRAVIS_BUILD_DIR
61+
- ./sbt assembly
62+
- ./sbt publishLocal
63+
- openssl aes-256-cbc -K $encrypted_97aef7f4ae04_key -iv $encrypted_97aef7f4ae04_iv -in key.asc.enc -out key.asc -d
64+
- gpg --no-default-keyring --primary-keyring ./project/.gnupg/pubring.gpg --secret-keyring ./project/.gnupg/secring.gpg --keyring ./project/.gnupg/pubring.gpg --fingerprint --import key.asc
65+
- ./sbt publishSigned
66+
- ./sbt sonatypeRelease

build.sbt

Lines changed: 57 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import sys.process._
1+
22

33
name := "bblfsh-client"
44
organization := "org.bblfsh"
5-
version := "1.11.0"
5+
version := "2.0.0-SNAPSHOT"
66

77
scalaVersion := "2.11.11"
8-
val libuastVersion = "1.9.3"
9-
val sdkVersion = "v1.17.0"
10-
val sdkMajor = "v1"
8+
val libuastVersion = "3.3.1"
9+
val sdkMajor = "v3"
10+
val sdkVersion = s"${sdkMajor}.1.0"
1111
val protoDir = "src/main/proto"
1212

1313
mainClass in Compile := Some("org.bblfsh.client.cli.ScalaClientCLI")
@@ -92,35 +92,48 @@ getProtoFiles := {
9292

9393
println(s"Downloading and installing SDK$sdkMajor protocol buffer files...")
9494

95-
val bblfshProto = f"$protoDir%s/gopkg.in/bblfsh"
96-
val sdkProto = f"$bblfshProto%s/sdk.$sdkMajor%s"
95+
val bblfshProto = s"${protoDir}/github.com/bblfsh"
96+
val sdkProto = s"${bblfshProto}/sdk/${sdkMajor}"
9797

98-
f"mkdir -p $sdkProto%s/protocol" !
98+
s"mkdir -p ${sdkProto}/protocol" !
9999

100-
f"mkdir -p $sdkProto%s/uast" !
100+
s"mkdir -p ${sdkProto}/uast/role" !
101101

102102
val unzip_dir = "sdk-" + sdkVersion.substring(1)
103103

104-
f"curl -SL https://github.com/bblfsh/sdk/archive/$sdkVersion%s.tar.gz" #| "tar xz" #&&
105-
f"cp $unzip_dir%s/protocol/generated.proto $sdkProto%s/protocol/" #&&
106-
f"cp $unzip_dir%s/uast/generated.proto $sdkProto%s/uast/" #&&
107-
f"rm -rf $unzip_dir%s" !
104+
s"curl -SL https://github.com/bblfsh/sdk/archive/${sdkVersion}.tar.gz" #| "tar xz" #&&
105+
s"cp ${unzip_dir}/protocol/driver.proto ${sdkProto}/protocol/" #&&
106+
s"cp ${unzip_dir}/uast/role/generated.proto ${sdkProto}/uast/role" #&&
107+
s"rm -rf ${unzip_dir}" !
108+
109+
println("Done unpacking SDK")
108110
}
109111

110112
val getLibuast = TaskKey[Unit]("getLibuast", "Retrieve libuast")
111113
getLibuast := {
112114
import sys.process._
113115

114-
val releaseUrl = s"https://github.com/bblfsh/libuast/archive/v$libuastVersion.tar.gz"
115-
println(s"Downloading libuast from $releaseUrl")
116+
val ghUrl = "https://github.com/bblfsh/libuast"
117+
val os = if (System.getProperty("os.name").toLowerCase.contains("mac os x")) "darwin" else "linux"
118+
val binaryReleaseUrl = s"${ghUrl}/releases/download/v${libuastVersion}/libuast-${os}-amd64.tar.gz"
119+
println(s"Downloading libuast binary from ${binaryReleaseUrl}")
120+
121+
s"curl -sL ${binaryReleaseUrl} -o libuast-bin.tar.gz" #&&
122+
"tar xzf libuast-bin.tar.gz" #&&
123+
s"mv ${os}-amd64 libuast" #&&
124+
"mkdir -p src/main/resources" #&&
125+
"rm -rf src/main/resources/libuast" #&&
126+
"mv libuast src/main/resources" #&&
127+
"rm src/main/resources/libuast/libuast.so" #&& // always a static build
128+
"rm libuast-bin.tar.gz" !
129+
130+
"find src/main/resources"!
116131

117-
f"curl -SL $releaseUrl -o libuast.tar.gz" #&&
118-
"tar zxf libuast.tar.gz" #&&
119-
f"mv libuast-$libuastVersion libuast" #&&
120-
"rm -rf src/libuast-native" #&&
121-
"mv libuast/src/ src/libuast-native" #&&
122-
"rm -rf libuast" #&&
123-
"rm libuast.tar.gz" !
132+
"nm src/main/resources/libuast/libuast.a" #| "grep -c UastDecode"!
133+
134+
"nm src/main/resources/libuast/libuast.a" #| "wc -l"!
135+
136+
println("Done unpacking libuast")
124137
}
125138

126139
val compileScalaLibuast = TaskKey[Unit]("compileScalaLibuast", "Compile libScalaUast JNI library")
@@ -131,15 +144,11 @@ compileScalaLibuast := {
131144

132145
"mkdir -p ./src/main/resources/lib/" !
133146

134-
val sourceFiles = "src/main/scala/org/bblfsh/client/libuast/org_bblfsh_client_libuast_Libuast.cc " +
135-
"src/main/scala/org/bblfsh/client/libuast/jni_utils.cc " +
136-
"src/main/scala/org/bblfsh/client/libuast/nodeiface.cc " +
137-
"src/main/scala/org/bblfsh/client/libuast/memtracker.cc " +
138-
"src/libuast-native/uast.cc " +
139-
"src/libuast-native/roles.c "
147+
val nativeSourceFiles = "src/main/native/org_bblfsh_client_v2_libuast_Libuast.cc " +
148+
"src/main/native/jni_utils.cc "
140149

141-
compileUnix(sourceFiles)
142-
crossCompileMacOS(sourceFiles)
150+
compileUnix(nativeSourceFiles)
151+
crossCompileMacOS(nativeSourceFiles)
143152
}
144153

145154
def compileUnix(sourceFiles: String) = {
@@ -149,31 +158,32 @@ def compileUnix(sourceFiles: String) = {
149158
if (javaHome == null) {
150159
javaHome = "/usr/lib/jvm/java-8-openjdk-amd64"
151160
}
152-
val xml2Conf = "xml2-config --cflags --libs" !!
153161

154162
val osName = System.getProperty("os.name").toLowerCase()
155-
if (osName.contains("mac os x")) {
156-
val cmd:String = "g++ -shared -Wall -fPIC -O2 -std=c++11 " +
163+
if (osName.contains("mac os x")) { // TODO(bzz): change back to '-fPIC -O2' for release
164+
val cmd:String = "g++ -shared -Wall -g -std=c++11 " +
157165
"-I/usr/include " +
158166
"-I" + javaHome + "/include/ " +
159167
"-I" + javaHome + "/include/darwin " +
160-
"-Isrc/libuast-native/ " +
161-
"-o src/main/resources/lib/libscalauast.dylib " +
168+
"-Isrc/main/resources/libuast " +
169+
"-o src/main/resources/lib/libscalauast.dylib " + // sic, must be in the classpath for the test
162170
sourceFiles +
163-
xml2Conf + " "
171+
"src/main/resources/libuast/libuast.a "
164172

165173
checkedProcess(cmd, "macOS build")
166174
} else {
167175
val cmd:String = "g++ -shared -Wall -fPIC -O2 -std=c++11 " +
168176
"-I/usr/include " +
169177
"-I" + javaHome + "/include/ " +
170178
"-I" + javaHome + "/include/linux " +
171-
"-Isrc/libuast-native/ " +
179+
"-Isrc/main/resources/libuast " +
172180
"-o src/main/resources/lib/libscalauast.so " +
173181
sourceFiles +
174-
xml2Conf + " "
182+
"src/main/resources/libuast/libuast.a "
175183

176184
checkedProcess(cmd, "Linux build")
185+
186+
"nm src/main/resources/lib/libscalauast.so" #| "grep -c UastDecode"!
177187
}
178188
}
179189

@@ -183,22 +193,21 @@ def crossCompileMacOS(sourceFiles: String): Unit = {
183193
println("Skipping cross-compilation for macOS on macOS")
184194
return
185195
}
186-
val osxHome = System.getenv("OSXCROSS_PATH")
187196

197+
val osxHome = System.getenv("OSXCROSS_PATH")
188198
if (osxHome == null || osxHome.isEmpty) {
189199
println("OSXCROSS_PATH variable not defined, not cross-compiling for macOS")
190200
return
191201
}
192202

193203
val cmd = osxHome + "/bin/o64-clang++-libc++ -shared -Wall -fPIC -O2 -lxml2 -std=c++11 " +
194-
"-I" + osxHome + "/SDK/MacOSX10.11.sdk/usr/include/libxml2/ " +
195-
"-I" + osxHome + "/SDK/src/libuast-native/roles.c " +
196204
"-I" + osxHome + "/SDK/MacOSX10.11.sdk/usr/include/ " +
197205
"-I/usr/lib/jvm/java-8-openjdk-amd64/include " +
198206
"-I/usr/lib/jvm/java-8-openjdk-amd64/include/linux " +
199207
"-Isrc/libuast-native/ " +
200208
"-o src/main/resources/lib/libscalauast.dylib " +
201-
sourceFiles
209+
sourceFiles +
210+
"src/main/resources/libuast/libuast.a "
202211

203212
checkedProcess(cmd, "macOS cross-compile build")
204213
}
@@ -214,4 +223,10 @@ def checkedProcess(cmd: String, name: String) {
214223
}
215224
}
216225

226+
cleanFiles ++= Seq(
227+
baseDirectory.value / "src/main/resources/libuast",
228+
baseDirectory.value / "src/main/resources/lib",
229+
baseDirectory.value / s"${protoDir}/github.com/bblfsh"
230+
)
231+
217232
mainClass := Def.sequential(getProtoFiles, getLibuast, compileScalaLibuast, (mainClass in Compile)).value

src/main/native/jni_utils.cc

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include "jni_utils.h"
2+
3+
// Class fully qualified names
4+
const char *CLS_NODE = "org/bblfsh/client/v2/Node";
5+
const char *CLS_CTX = "org/bblfsh/client/v2/Context";
6+
7+
extern JavaVM *jvm; // FIXME(bzz): double-check and document
8+
9+
JNIEnv *getJNIEnv() {
10+
JNIEnv *pEnv = NULL;
11+
12+
switch (jvm->GetEnv((void **)&pEnv, JNI_VERSION_1_8)) {
13+
case JNI_OK: // Thread is ready to use, nothing to do
14+
break;
15+
16+
case JNI_EDETACHED: // Thread is detached, need to attach
17+
jvm->AttachCurrentThread((void **)&pEnv, NULL);
18+
break;
19+
}
20+
21+
return pEnv;
22+
}
23+
24+
jobject NewJavaObject(JNIEnv *env, const char *className, const char *initSign,
25+
...) {
26+
jclass cls = env->FindClass(className);
27+
if (env->ExceptionOccurred() || !cls) {
28+
return NULL;
29+
}
30+
31+
jmethodID initId = env->GetMethodID(cls, "<init>", initSign);
32+
if (env->ExceptionOccurred() || !initId) {
33+
return NULL;
34+
}
35+
36+
va_list varargs;
37+
va_start(varargs, initSign);
38+
jobject instance = env->NewObjectV(cls, initId, varargs);
39+
va_end(varargs);
40+
if (env->ExceptionOccurred() || !instance) {
41+
return NULL;
42+
}
43+
44+
return instance;
45+
}
46+
47+
jfieldID getField(JNIEnv *env, jobject obj, const char *name) {
48+
jclass cls = env->GetObjectClass(obj);
49+
if (env->ExceptionOccurred() || !cls) {
50+
return nullptr;
51+
}
52+
53+
jfieldID jfid = env->GetFieldID(cls, name, "J");
54+
if (env->ExceptionOccurred() || !jfid) {
55+
return nullptr;
56+
}
57+
return jfid;
58+
}

src/main/native/jni_utils.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef _Included_org_bblfsh_client_libuast_Libuast_jni_utils
2+
#define _Included_org_bblfsh_client_libuast_Libuast_jni_utils
3+
4+
#include <jni.h>
5+
6+
extern const char *CLS_NODE;
7+
extern const char *CLS_CTX;
8+
9+
JNIEnv *getJNIEnv();
10+
jobject NewJavaObject(JNIEnv *, const char *, const char *, ...);
11+
jfieldID getField(JNIEnv *env, jobject obj, const char *name);
12+
13+
#endif

0 commit comments

Comments
 (0)