Skip to content

Commit 64989ef

Browse files
authored
Merge pull request #54 from kornilova-l/end-to-end
End-to-end testing
2 parents f5d3494 + 375964e commit 64989ef

31 files changed

+289
-115
lines changed

.travis.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,14 @@ matrix:
1515
- env: TEST_ENV=ubuntu-18.04-llvm-dev
1616
- env: TEST_ENV=ubuntu-18.04-llvm-6.0
1717
- env: TEST_ENV=ubuntu-18.04-llvm-5.0
18+
19+
before_cache:
20+
# See https://www.scala-sbt.org/1.0/docs/Travis-CI-with-sbt.html
21+
# Tricks to avoid unnecessary cache updates
22+
- find $HOME/.sbt -name "*.lock" | xargs rm
23+
- find $HOME/.ivy2 -name "ivydata-*.properties" | xargs rm
24+
25+
cache:
26+
directories:
27+
- $HOME/.ivy2/cache
28+
- $HOME/.sbt/boot

Dockerfile

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,31 @@ ARG UBUNTU_VERSION=18.04
22
FROM ubuntu:$UBUNTU_VERSION
33

44
RUN set -x \
5+
&& : Remove pre-bundled libunwind \
6+
&& find /usr -name "*libunwind*" -delete \
57
&& apt update \
6-
&& apt install -y apt-transport-https gnupg2 ca-certificates \
8+
&& apt install -y --no-install-recommends apt-transport-https gnupg2 ca-certificates \
79
&& echo "deb https://dl.bintray.com/sbt/debian /" > /etc/apt/sources.list.d/sbt.list \
810
&& apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 2EE0EA64E40A89B84B2DF73499E82A75642AC823 \
911
&& apt update \
10-
&& apt install -y curl build-essential openjdk-8-jdk-headless sbt cmake make clang-format-5.0 \
12+
&& apt install -y --no-install-recommends \
13+
g++ openjdk-8-jdk-headless sbt cmake make curl git \
14+
libgc-dev libunwind8-dev libre2-dev \
1115
&& rm -rf /var/lib/apt/lists/*
1216

1317
ARG LLVM_VERSION=6.0
1418
ENV LLVM_VERSION=$LLVM_VERSION
1519
# LLVM dev versions do not have a "-x.y" version suffix.
1620
ARG LLVM_DEB_COMPONENT=-$LLVM_VERSION
1721
RUN set -x \
18-
&& curl https://apt.llvm.org/llvm-snapshot.gpg.key | apt-key add - \
1922
&& . /etc/lsb-release \
2023
&& echo "deb https://apt.llvm.org/$DISTRIB_CODENAME/ llvm-toolchain-$DISTRIB_CODENAME$LLVM_DEB_COMPONENT main" > /etc/apt/sources.list.d/llvm.list \
24+
&& apt-key adv --fetch-keys https://apt.llvm.org/llvm-snapshot.gpg.key \
2125
&& apt update \
22-
&& apt install -y clang-$LLVM_VERSION libclang-$LLVM_VERSION-dev \
26+
&& apt install -y --no-install-recommends \
27+
clang-$LLVM_VERSION clang-format-$LLVM_VERSION \
28+
libclang-$LLVM_VERSION-dev llvm-$LLVM_VERSION-dev \
2329
&& rm -rf /var/lib/apt/lists/*
2430

31+
ENV PATH=$PATH:/usr/lib/llvm-$LLVM_VERSION/bin
2532
WORKDIR /src

bindgen/Main.cpp

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ int main(int argc, char *argv[]) {
3030
llvm::cl::opt<std::string> Package(
3131
"package", llvm::cl::cat(Category),
3232
llvm::cl::desc("Package name of generated Scala file"));
33+
llvm::cl::opt<bool> NoLinkName(
34+
"no-link", llvm::cl::cat(Category),
35+
llvm::cl::desc("Library does not require linking"));
36+
llvm::cl::opt<std::string> LinkName(
37+
"link", llvm::cl::cat(Category),
38+
llvm::cl::desc("Library to link with, e.g. -luv for libuv"));
3339
clang::tooling::CommonOptionsParser op(argc, (const char **)argv, Category);
3440
clang::tooling::ClangTool Tool(op.getCompilations(),
3541
op.getSourcePathList());
@@ -42,23 +48,33 @@ int main(int argc, char *argv[]) {
4248
return -1;
4349
}
4450

51+
auto linkName = LinkName.getValue();
52+
if (linkName.empty()) {
53+
linkName = libName;
54+
}
55+
if (NoLinkName.getValue()) {
56+
linkName = "";
57+
}
58+
59+
auto objectName = libName;
60+
if (objectName == "native") {
61+
/* there are at most 3 objects in the file.
62+
* All of them will have distinct names. */
63+
objectName = "nativeLib";
64+
}
65+
4566
auto stdhead = StdHeaders.getValue();
4667
if (!stdhead.empty()) {
4768
headerMan.LoadConfig(stdhead);
4869
}
4970

5071
locations.clear();
5172

52-
ScalaFrontendActionFactory actionFactory(libName);
73+
IR ir(libName, linkName, objectName, Package.getValue());
74+
ScalaFrontendActionFactory actionFactory(ir);
5375

5476
int result = Tool.run(&actionFactory);
5577

56-
IR ir = actionFactory.getIntermediateRepresentation();
57-
58-
if (!Package.empty()) {
59-
ir.setPackageName(Package.getValue());
60-
}
61-
6278
auto printLoc = PrintHeadersLocation.getValue();
6379
if (printLoc) {
6480
for (const auto &location : locations) {

bindgen/ir/IR.cpp

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
#include "IR.h"
22
#include "../Utils.h"
33

4-
IR::IR(std::string libName) : libName(std::move(libName)) {
5-
if (this->libName == "native") {
6-
/* there are at most 3 objects in the file.
7-
* All of them will have distinct names. */
8-
libObjectName = "nativeLib";
9-
} else {
10-
libObjectName = this->libName;
11-
}
12-
}
4+
IR::IR(std::string libName, std::string linkName, std::string objectName,
5+
std::string packageName)
6+
: libName(std::move(libName)), linkName(std::move(linkName)),
7+
objectName(std::move(objectName)), packageName(packageName) {}
138

149
void IR::addFunction(std::string name, std::vector<Parameter> parameters,
1510
std::string retType, bool isVariadic) {
@@ -54,12 +49,15 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const IR &ir) {
5449
<< "import scala.scalanative.native._\n\n";
5550
}
5651

57-
std::string libObjName = handleReservedWords(ir.libObjectName);
52+
std::string objectName = handleReservedWords(ir.objectName);
5853

5954
if (!ir.libObjEmpty()) {
60-
s << "@native.link(\"" << ir.libName << "\")\n"
61-
<< "@native.extern\n"
62-
<< "object " << libObjName << " {\n";
55+
if (!ir.linkName.empty()) {
56+
s << "@native.link(\"" << ir.linkName << "\")\n";
57+
}
58+
59+
s << "@native.extern\n"
60+
<< "object " << objectName << " {\n";
6361

6462
for (const auto &typeDef : ir.typeDefs) {
6563
s << typeDef;
@@ -73,7 +71,7 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const IR &ir) {
7371
}
7472

7573
if (!ir.enums.empty() || ir.hasHelperMethods()) {
76-
s << "import " << libObjName << "._\n\n";
74+
s << "import " << objectName << "._\n\n";
7775
}
7876

7977
if (!ir.enums.empty()) {
@@ -207,10 +205,6 @@ bool IR::typeIsUsedOnlyInTypeDefs(std::string type) {
207205
isTypeUsed(unions, type));
208206
}
209207

210-
void IR::setPackageName(std::string packageName) {
211-
this->packageName = std::move(packageName);
212-
}
213-
214208
void IR::setScalaNames() {
215209
/* Renaming according to Scala naming conventions
216210
* should happen here */

bindgen/ir/IR.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
*/
1212
class IR {
1313
public:
14-
explicit IR(std::string libName);
14+
explicit IR(std::string libName, std::string linkName,
15+
std::string objectName, std::string packageName);
1516

1617
void addFunction(std::string name, std::vector<Parameter> parameters,
1718
std::string, bool isVariadic);
@@ -39,8 +40,6 @@ class IR {
3940

4041
void generate(const std::string &excludePrefix);
4142

42-
void setPackageName(std::string packageName);
43-
4443
private:
4544
/**
4645
* Generates type defs for enums, structs and unions
@@ -105,8 +104,9 @@ class IR {
105104

106105
bool existsFunctionWithName(std::string functionName);
107106

108-
std::string libName; // name of the library
109-
std::string libObjectName; // name of Scala object
107+
std::string libName; // name of the library
108+
std::string linkName; // name of the library to link with
109+
std::string objectName; // name of Scala object
110110
std::vector<Function> functions;
111111
std::vector<TypeDef> typeDefs;
112112
std::vector<Struct> structs;

bindgen/visitor/ScalaFrontendAction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "ScalaFrontendAction.h"
22

3-
ScalaFrontendAction::ScalaFrontendAction(IR *ir) : ir(ir) {}
3+
ScalaFrontendAction::ScalaFrontendAction(IR &ir) : ir(ir) {}
44

55
std::unique_ptr<clang::ASTConsumer>
66
ScalaFrontendAction::CreateASTConsumer(clang::CompilerInstance &CI,

bindgen/visitor/ScalaFrontendAction.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
*/
1313
class ScalaFrontendAction : public clang::ASTFrontendAction {
1414
public:
15-
explicit ScalaFrontendAction(IR *ir);
15+
explicit ScalaFrontendAction(IR &ir);
1616

1717
std::unique_ptr<clang::ASTConsumer>
1818
CreateASTConsumer(clang::CompilerInstance &CI,
1919
clang::StringRef file) override;
2020

2121
private:
22-
IR *ir;
22+
IR &ir;
2323
};
2424

2525
#endif // SCALA_NATIVE_BINDGEN_SCALAFRONTENDACTION_H
Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
#include "ScalaFrontendActionFactory.h"
22
#include "ScalaFrontendAction.h"
33

4-
ScalaFrontendActionFactory::ScalaFrontendActionFactory(std::string libName)
5-
: libName(libName), ir(libName) {}
4+
ScalaFrontendActionFactory::ScalaFrontendActionFactory(IR &ir) : ir(ir) {}
65

76
clang::FrontendAction *ScalaFrontendActionFactory::create() {
87
if (!ir.libObjEmpty() || ir.hasEnums()) {
98
llvm::errs() << "IR is not empty. Please use new instance of "
109
"ScalaFrontendActionFactory.\n";
1110
llvm::errs().flush();
1211
}
13-
return new ScalaFrontendAction(
14-
&ir); // instance will be deleted by LibTooling
15-
}
16-
17-
const IR &ScalaFrontendActionFactory::getIntermediateRepresentation() const {
18-
return ir;
12+
return new ScalaFrontendAction(ir);
1913
}

bindgen/visitor/ScalaFrontendActionFactory.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,12 @@
1313
class ScalaFrontendActionFactory
1414
: public clang::tooling::FrontendActionFactory {
1515
public:
16-
explicit ScalaFrontendActionFactory(std::string libName);
16+
explicit ScalaFrontendActionFactory(IR &ir);
1717

1818
clang::FrontendAction *create() override;
1919

20-
const IR &getIntermediateRepresentation() const;
21-
2220
private:
23-
std::string libName;
24-
25-
IR ir;
21+
IR &ir;
2622
};
2723

2824
#endif // SCALA_NATIVE_BINDGEN_SCALAFRONTENDACTIONFACTORY_H

bindgen/visitor/TreeConsumer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class TreeConsumer : public clang::ASTConsumer {
1818
clang::SourceManager &smanager;
1919

2020
public:
21-
TreeConsumer(clang::CompilerInstance *CI, IR *ir)
21+
TreeConsumer(clang::CompilerInstance *CI, IR &ir)
2222
: visitor(CI, ir), smanager(CI->getASTContext().getSourceManager()) {}
2323

2424
bool HandleTopLevelDecl(clang::DeclGroupRef DG) override {

0 commit comments

Comments
 (0)