Skip to content

Commit 8ce59f3

Browse files
authored
Merge branch 'master' into output-name
2 parents 61a5804 + f54579a commit 8ce59f3

File tree

13 files changed

+228
-56
lines changed

13 files changed

+228
-56
lines changed

.travis.yml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ compiler: gcc
44

55
script:
66
- docker-compose build $TEST_ENV
7-
- docker run --rm -ti scala-native-bindgen:$TEST_ENV /usr/include/ctype.h -name ctype --
8-
- docker-compose run --rm sbt-test
7+
- docker-compose run $TEST_ENV
98

109
matrix:
1110
include:
12-
- env: TEST_ENV=ubuntu-16.04-llvm-dev
13-
- env: TEST_ENV=ubuntu-16.04-llvm-6.0
14-
- env: TEST_ENV=ubuntu-16.04-llvm-5.0
11+
- env: TEST_ENV=ubuntu-18.04-llvm-dev
12+
- env: TEST_ENV=ubuntu-18.04-llvm-6.0
13+
- env: TEST_ENV=ubuntu-18.04-llvm-5.0

CONTRIBUTING.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Guide for contributors
2+
3+
Scala Native Bindgen follows Scala Native's [contributing guidelines].
4+
Larger contributions should always begin by creating an issue to
5+
ensure that it is properly scoped.
6+
7+
Important to note is that all contributors must have signed the [Scala CLA].
8+
9+
[contributing guidelines]: http://www.scala-native.org/en/latest/contrib/contributing.html
10+
[Scala CLA]: https://www.lightbend.com/contribute/cla/scala
11+
12+
## Developer Workflow
13+
14+
Build the `scalaBindgen` tool:
15+
16+
```sh
17+
mkdir target
18+
cd target
19+
cmake ..
20+
make
21+
22+
# Alternatively rerun on change
23+
watchman-make -p '*.cpp' '*.h' --run 'make -C target'
24+
```
25+
26+
In another terminal, run the test suite:
27+
28+
```sh
29+
cd tests
30+
sbt ~test
31+
```
32+
33+
## Coding Guidelines
34+
35+
The C++ tool is built on Clang and Libtooling and should respect the conventions of
36+
LLVM and Clang tools. The code itself should adhere to the [LLVM Coding Standards],
37+
specifically:
38+
39+
- For code generation and error reporting use `llvm::outs()` and `llvm::errs()`.
40+
- [Use `\n` instead of `std::endl`](https://llvm.org/docs/CodingStandards.html#avoid-std-endl)
41+
and remember to flush when reporting errors.
42+
43+
[LLVM Coding Standards]: https://llvm.org/docs/CodingStandards.html

Dockerfile

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,25 @@
1-
ARG UBUNTU_VERSION=16.04
1+
ARG UBUNTU_VERSION=18.04
22
FROM ubuntu:$UBUNTU_VERSION
33

44
RUN set -x \
55
&& apt update \
6-
&& apt install -y apt-transport-https \
6+
&& apt install -y apt-transport-https gnupg2 ca-certificates \
77
&& echo "deb https://dl.bintray.com/sbt/debian /" > /etc/apt/sources.list.d/sbt.list \
88
&& apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 2EE0EA64E40A89B84B2DF73499E82A75642AC823 \
99
&& apt update \
10-
&& apt install -y curl build-essential openjdk-8-jdk-headless sbt \
10+
&& apt install -y curl build-essential openjdk-8-jdk-headless sbt cmake make \
1111
&& rm -rf /var/lib/apt/lists/*
1212

13-
WORKDIR /cmake
14-
ARG CMAKE_ARCHIVE=cmake-3.11.2-Linux-x86_64
15-
RUN curl https://cmake.org/files/v3.11/$CMAKE_ARCHIVE.tar.gz | tar zxf - \
16-
&& for i in bin share; do \
17-
cp -r /cmake/$CMAKE_ARCHIVE/$i/* /usr/$i/; \
18-
done \
19-
&& rm -rf /cmake
20-
2113
ARG LLVM_VERSION=6.0
14+
ENV LLVM_VERSION=$LLVM_VERSION
2215
# LLVM dev versions do not have a "-x.y" version suffix.
2316
ARG LLVM_DEB_COMPONENT=-$LLVM_VERSION
2417
RUN set -x \
2518
&& curl https://apt.llvm.org/llvm-snapshot.gpg.key | apt-key add - \
2619
&& . /etc/lsb-release \
2720
&& echo "deb https://apt.llvm.org/$DISTRIB_CODENAME/ llvm-toolchain-$DISTRIB_CODENAME$LLVM_DEB_COMPONENT main" > /etc/apt/sources.list.d/llvm.list \
2821
&& apt update \
29-
&& apt install -y clang-$LLVM_VERSION libclang-$LLVM_VERSION-dev make \
22+
&& apt install -y clang-$LLVM_VERSION libclang-$LLVM_VERSION-dev \
3023
&& rm -rf /var/lib/apt/lists/*
3124

32-
WORKDIR /src/target
33-
COPY . /src
34-
RUN cmake .. && make VERBOSE=1
35-
36-
ENTRYPOINT ["/src/target/scala-native-bindgen"]
25+
WORKDIR /src

NEWS.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Release notes
2+
3+
## master
4+
5+
Improvements:
6+
7+
- Save results from AST visitor in an intermediate representation. [#6], [#10]
8+
- Add struct alloc helper. [mrRosset#3], [mrRosset#4]
9+
- Simplify the build to link against system installed Clang libraries. [mrRosset#2]
10+
- Use docker-compose to run tests against multiple LLVM versions on Travis. [#8], [#9]
11+
- Migrate the test suite to Scala and sbt. [#17]
12+
- Align code with LLVM Coding Standards. [#23]
13+
14+
[mrRosset#2]: https://github.com/mrRosset/scala-native-bindgen/pull/2
15+
[mrRosset#3]: https://github.com/mrRosset/scala-native-bindgen/issues/3
16+
[mrRosset#4]: https://github.com/mrRosset/scala-native-bindgen/pull/4
17+
[#6]: https://github.com/kornilova-l/scala-native-bindgen/issues/6
18+
[#8]: https://github.com/kornilova-l/scala-native-bindgen/pull/8
19+
[#9]: https://github.com/kornilova-l/scala-native-bindgen/issues/9
20+
[#10]: https://github.com/kornilova-l/scala-native-bindgen/pull/10
21+
[#17]: https://github.com/kornilova-l/scala-native-bindgen/pull/17
22+
23+
Based on work by [Marius Rosset][@mrRosset] done as part of a project at [EPFL].
24+
25+
[@mrRosset]: https://github.com/mrRosset
26+
[EPFL]: https://www.epfl.ch/

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,24 @@ cd target
2525
cmake ..
2626
make
2727
./scala-native-bindgen /usr/include/ctype.h -name ctype --
28+
```
29+
30+
Alternatively, you can use [docker-compose] to build and test the program:
31+
32+
```sh
33+
# Build the docker image with LLVM version 6.0.
34+
docker-compose build ubuntu-18.04-llvm-6.0
35+
# Build the bindgen tool and run the tests.
36+
docker-compose run --rm ubuntu-18.04-llvm-6.0
37+
# Run the bindgen tool inside the container.
38+
docker-compose run --rm ubuntu-18.04-llvm-6.0 target/scalaBindgen -name union tests/samples/Union.h --
2839
```
2940

3041
[CMake]: https://cmake.org/
3142
[LLVM]: https://llvm.org/
3243
[Clang]: https://clang.llvm.org/
3344
[Scala Native setup guide]: http://www.scala-native.org/en/latest/user/setup.html
45+
[docker-compose]: https://docs.docker.com/compose/
3446

3547
## Testing
3648

TypeTranslator.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ std::string TypeTranslator::Translate(const clang::QualType& qtpe, const std::st
153153
return TranslatePointer(tpe->getAs<clang::PointerType>()->getPointeeType(), avoid);
154154

155155
} else if(qtpe->isStructureType() || qtpe->isUnionType()){
156-
return TranslateStructOrUnion(qtpe);
156+
return handleReservedWords(TranslateStructOrUnion(qtpe));
157157

158158
} else if(qtpe->isEnumeralType()){
159159
return TranslateEnum(qtpe);
@@ -166,10 +166,10 @@ std::string TypeTranslator::Translate(const clang::QualType& qtpe, const std::st
166166

167167
auto found = typeMap.find(qtpe.getUnqualifiedType().getAsString());
168168
if(found != typeMap.end()){
169-
return found->second;
169+
return handleReservedWords(found->second);
170170
} else {
171171
//TODO: Properly handle non-default types
172-
return qtpe.getUnqualifiedType().getAsString();
172+
return handleReservedWords(qtpe.getUnqualifiedType().getAsString());
173173
}
174174
}
175175

Utils.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@ static std::array<std::string, 39> reserved_words = {"abstract", "case", "catch"
6868
"protected", "return", "sealed", "super", "this", "throw", "trait", "try",
6969
"true", "type", "val", "var", "while", "with", "yield"};
7070

71-
inline std::string handleReservedWords(std::string name){
71+
inline std::string handleReservedWords(std::string name, std::string suffix = "") {
7272
auto found = std::find(reserved_words.begin(), reserved_words.end(), name);
73-
if(found != reserved_words.end()){
74-
return "`" + name + "`";
75-
} else{
76-
return name;
73+
if (found != reserved_words.end()) {
74+
return "`" + name + suffix + "`";
75+
} else {
76+
return name + suffix;
7777
}
7878
}
7979

docker-compose.yml

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,42 @@
11
version: '3'
22

33
services:
4-
ubuntu-16.04-llvm-dev:
5-
image: scala-native-bindgen:ubuntu-16.04-llvm-dev
4+
ubuntu-18.04-llvm-dev:
5+
image: scalabindgen/scala-native-bindgen:ubuntu-18.04-llvm-dev
66
build:
77
context: .
88
args:
9-
- UBUNTU_VERSION=16.04
9+
- UBUNTU_VERSION=18.04
1010
- LLVM_VERSION=7
1111
- LLVM_DEB_COMPONENT=
12+
command: scripts/test.sh
13+
volumes:
14+
- .:/src
15+
- ${HOME}/.ivy2:/root/.ivy2
16+
- ${HOME}/.sbt:/root/.sbt
1217

13-
ubuntu-16.04-llvm-6.0:
14-
image: scala-native-bindgen:ubuntu-16.04-llvm-6.0
18+
ubuntu-18.04-llvm-6.0:
19+
image: scalabindgen/scala-native-bindgen:ubuntu-18.04-llvm-6.0
1520
build:
1621
context: .
1722
args:
18-
- UBUNTU_VERSION=16.04
23+
- UBUNTU_VERSION=18.04
1924
- LLVM_VERSION=6.0
25+
command: scripts/test.sh
26+
volumes:
27+
- .:/src
28+
- ${HOME}/.ivy2:/root/.ivy2
29+
- ${HOME}/.sbt:/root/.sbt
2030

21-
ubuntu-16.04-llvm-5.0:
22-
image: scala-native-bindgen:ubuntu-16.04-llvm-5.0
31+
ubuntu-18.04-llvm-5.0:
32+
image: scalabindgen/scala-native-bindgen:ubuntu-18.04-llvm-5.0
2333
build:
2434
context: .
2535
args:
26-
- UBUNTU_VERSION=16.04
36+
- UBUNTU_VERSION=18.04
2737
- LLVM_VERSION=5.0
28-
29-
sbt-test:
30-
image: scala-native-bindgen:${TEST_ENV}
31-
entrypoint:
32-
- sh
33-
- -c
34-
- |
35-
cd /src/tests
36-
sbt compile test
38+
command: scripts/test.sh
3739
volumes:
40+
- .:/src
3841
- ${HOME}/.ivy2:/root/.ivy2
3942
- ${HOME}/.sbt:/root/.sbt

ir/Struct.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,11 @@ std::string Struct::generateHelperClass() const {
4646
int fieldIndex = 0;
4747
for (const auto &field : fields) {
4848
if (!field.getName().empty()) {
49-
std::string fname = handleReservedWords(field.getName());
49+
std::string getter = handleReservedWords(field.getName());
50+
std::string setter = handleReservedWords(field.getName(), "_=");
5051
std::string ftype = field.getType();
51-
s << " def " << fname << ": " << ftype << " = !p._" << std::to_string(fieldIndex + 1) << "\n"
52-
<< " def " << fname << "_=(value: " + ftype + "):Unit = !p._" << std::to_string(fieldIndex + 1)
52+
s << " def " << getter << ": " << ftype << " = !p._" << std::to_string(fieldIndex + 1) << "\n"
53+
<< " def " << setter << "(value: " + ftype + "):Unit = !p._" << std::to_string(fieldIndex + 1)
5354
<< " = value\n";
5455
}
5556
fieldIndex++;
@@ -81,13 +82,14 @@ std::string Union::generateHelperClass() const {
8182
<< "(val p: native.Ptr[union_" << name << "]) extends AnyVal {\n";
8283
for (const auto &field : fields) {
8384
if (!field.getName().empty()) {
84-
std::string fname = handleReservedWords(field.getName());
85+
std::string getter = handleReservedWords(field.getName());
86+
std::string setter = handleReservedWords(field.getName(), "_=");
8587
std::string ftype = field.getType();
86-
s << " def " << fname
88+
s << " def " << getter
8789
<< ": native.Ptr[" << ftype << "] = p.cast[native.Ptr[" << ftype << "]]\n";
8890

89-
s << " def " << fname
90-
<< "_=(value: " << ftype << "): Unit = !p.cast[native.Ptr[" << ftype << "]] = value\n";
91+
s << " def " << setter
92+
<< "(value: " << ftype << "): Unit = !p.cast[native.Ptr[" << ftype << "]] = value\n";
9193
}
9294
}
9395
s << " }\n";

scripts/test.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env bash
2+
3+
# Bash strict mode
4+
# http://redsymbol.net/articles/unofficial-bash-strict-mode/
5+
set -euo pipefail
6+
IFS=$'\n\t'
7+
8+
if [[ ! -e target/.llvm-version ]] || [[ "$(<target/.llvm-version)" != "${LLVM_VERSION:-}" ]]; then
9+
rm -rf target
10+
mkdir -p target
11+
echo "${LLVM_VERSION:-}" > target/.llvm-version
12+
(cd target && cmake ..)
13+
make -C target
14+
fi
15+
16+
cd tests
17+
sbt "${@:-test}"

0 commit comments

Comments
 (0)