Skip to content

Commit a344255

Browse files
authored
Merge pull request #75 from kornilova-l/static-binary
Fix #33: Configure CMake to build statically linked binary
2 parents 4d30963 + 5b3df08 commit a344255

File tree

7 files changed

+41
-35
lines changed

7 files changed

+41
-35
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ matrix:
2727
script: scripts/docker-deploy.sh latest
2828
on:
2929
branch: master
30-
- env: TEST_ENV=ubuntu-18.04-llvm-dev
3130
- env: TEST_ENV=ubuntu-18.04-llvm-5.0
3231

3332
before_cache:

Dockerfile

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,13 @@ RUN set -x \
1111
&& apt update \
1212
&& apt install -y --no-install-recommends \
1313
g++ openjdk-8-jdk-headless sbt cmake make curl git \
14+
zlib1g-dev \
1415
libgc-dev libunwind8-dev libre2-dev \
1516
&& rm -rf /var/lib/apt/lists/*
1617

1718
ARG LLVM_VERSION=6.0
1819
ENV LLVM_VERSION=$LLVM_VERSION
19-
# LLVM dev versions do not have a "-x.y" version suffix.
20-
ARG LLVM_DEB_COMPONENT=-$LLVM_VERSION
2120
RUN set -x \
22-
&& . /etc/lsb-release \
23-
&& 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 \
2521
&& apt update \
2622
&& apt install -y --no-install-recommends \
2723
clang-$LLVM_VERSION clang-format-$LLVM_VERSION \

bindgen/CMakeLists.txt

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,35 @@
11
cmake_minimum_required(VERSION 3.9)
22
project(scala-native-bindgen)
33

4-
# Locate $LLVM_PATH/lib/cmake/clang/ClangConfig.cmake
5-
find_package(Clang REQUIRED CONFIG)
4+
set(USE_SHARED OFF)
5+
6+
# Locate LLVMConfig.cmake
7+
find_package(LLVM REQUIRED CONFIG)
68
message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
79
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
810

11+
message(STATUS "Using LLVM include dirs: ${LLVM_INCLUDE_DIRS}")
912
include_directories(SYSTEM ${LLVM_INCLUDE_DIRS})
13+
14+
message(STATUS "Using LLVM defs: ${LLVM_DEFINITIONS}")
1015
add_definitions(${LLVM_DEFINITIONS})
1116

1217
add_compile_options(-fexceptions -std=c++11 -Wall -Wconversion -Werror)
1318

19+
if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
20+
# macOS does not guarantee backwards compatible system calls and therefore
21+
# discourages statically linked binaries. Instead add -L/usr/lib before the
22+
# LLVM LDFLAGS to link against the dynamic system libc++ instead of the one
23+
# from LLVM.
24+
set(BINDGEN_LINK_FLAG "-L/usr/lib")
25+
else()
26+
set(BINDGEN_LINK_FLAG "-static")
27+
endif()
28+
message(STATUS "Using link flag: ${BINDGEN_LINK_FLAG}")
29+
30+
message(STATUS "Using LLVM library directories: ${LLVM_LIBRARY_DIRS}")
31+
link_directories(${LLVM_LIBRARY_DIRS})
32+
1433
add_executable(bindgen
1534
Main.cpp
1635
visitor/ScalaFrontendAction.h
@@ -69,10 +88,23 @@ add_executable(bindgen
6988
set_target_properties(bindgen
7089
PROPERTIES
7190
OUTPUT_NAME scala-native-bindgen
91+
LINK_FLAGS "${BINDGEN_LINK_FLAG}"
7292
)
7393

94+
llvm_map_components_to_libnames(LLVM_LIBS support core irreader object option profiledata)
95+
7496
target_link_libraries(bindgen
7597
PRIVATE
7698
clangFrontend
7799
clangTooling
100+
clangSerialization
101+
clangDriver
102+
clangParse
103+
clangSema
104+
clangAnalysis
105+
clangEdit
106+
clangAST
107+
clangLex
108+
clangBasic
109+
${LLVM_LIBS}
78110
)

bindgen/Dockerfile

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,8 @@ RUN set -x \
1111
&& cmake .. \
1212
&& make
1313

14-
ARG UBUNTU_VERSION=18.04
15-
FROM ubuntu:$UBUNTU_VERSION
16-
17-
ARG LLVM_VERSION=6.0
18-
ENV LLVM_VERSION=$LLVM_VERSION
19-
RUN set -x \
20-
&& apt update \
21-
&& apt install -y --no-install-recommends libclang1-$LLVM_VERSION \
22-
&& rm -rf /var/lib/apt/lists/*
14+
FROM scratch
2315

24-
COPY --from=builder /src/target/scala-native-bindgen /usr/bin/scala-native-bindgen
16+
COPY --from=builder /src/target/scala-native-bindgen /scala-native-bindgen
2517
WORKDIR /src
26-
ENTRYPOINT ["scala-native-bindgen"]
18+
ENTRYPOINT ["/scala-native-bindgen"]

bindgen/Main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include "visitor/ScalaFrontendActionFactory.h"
33
#include <clang/Tooling/CommonOptionsParser.h>
44

5-
int main(int argc, char *argv[]) {
5+
int main(int argc, const char *argv[]) {
66
llvm::cl::OptionCategory Category("Binding Generator");
77
llvm::cl::extrahelp CommonHelp(
88
clang::tooling::CommonOptionsParser::HelpMessage);
@@ -37,7 +37,7 @@ int main(int argc, char *argv[]) {
3737
llvm::cl::opt<std::string> LinkName(
3838
"link", llvm::cl::cat(Category),
3939
llvm::cl::desc("Library to link with, e.g. -luv for libuv"));
40-
clang::tooling::CommonOptionsParser op(argc, (const char **)argv, Category);
40+
clang::tooling::CommonOptionsParser op(argc, argv, Category);
4141
clang::tooling::ClangTool Tool(op.getCompilations(),
4242
op.getSourcePathList());
4343

bindgen/visitor/TreeVisitor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class TreeVisitor : public clang::RecursiveASTVisitor<TreeVisitor> {
2626
TreeVisitor(clang::CompilerInstance *CI, IR &ir)
2727
: astContext(&(CI->getASTContext())), typeTranslator(astContext, ir),
2828
cycleDetection(typeTranslator), ir(ir) {}
29+
virtual ~TreeVisitor() {}
2930

3031
virtual bool VisitFunctionDecl(clang::FunctionDecl *func);
3132
virtual bool VisitTypedefDecl(clang::TypedefDecl *tpdef);

docker-compose.yml

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,6 @@ services:
1010
- LLVM_VERSION=6.0
1111
entrypoint: [scala-native-bindgen]
1212

13-
ubuntu-18.04-llvm-dev:
14-
image: scalabindgen/scala-native-bindgen-builder:ubuntu-18.04-llvm-dev
15-
build:
16-
context: .
17-
args:
18-
- UBUNTU_VERSION=18.04
19-
- LLVM_VERSION=7
20-
- LLVM_DEB_COMPONENT=
21-
command: scripts/test.sh
22-
volumes:
23-
- .:/src
24-
- ${HOME}/.ivy2:/root/.ivy2
25-
- ${HOME}/.sbt:/root/.sbt
26-
2713
ubuntu-18.04-llvm-6.0:
2814
image: scalabindgen/scala-native-bindgen-builder:ubuntu-18.04-llvm-6.0
2915
build:

0 commit comments

Comments
 (0)