Skip to content

Commit 4866662

Browse files
committed
Fix #82: Make static linking optional
The executable is still linked against `.a` Clang libraries.
1 parent 93ed6e3 commit 4866662

File tree

3 files changed

+27
-17
lines changed

3 files changed

+27
-17
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ make
6666
./scala-native-bindgen /usr/include/ctype.h -name ctype --
6767
```
6868

69+
To build a statically linked executable pass `-DSTATIC_LINKING=ON` when invoking `cmake`:
70+
71+
```sh
72+
cmake -DSTATIC_LINKING=ON ..
73+
```
74+
6975
Alternatively, you can use [docker-compose] to build and test the program:
7076

7177
```sh

bindgen/CMakeLists.txt

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

4-
set(USE_SHARED OFF)
4+
option(STATIC_LINKING "Statically link the executable" OFF)
55

66
# Locate LLVMConfig.cmake
77
find_package(LLVM REQUIRED CONFIG)
@@ -14,22 +14,11 @@ include_directories(SYSTEM ${LLVM_INCLUDE_DIRS})
1414
message(STATUS "Using LLVM defs: ${LLVM_DEFINITIONS}")
1515
add_definitions(${LLVM_DEFINITIONS})
1616

17-
add_compile_options(-fexceptions -std=c++11 -Wall -Wconversion -Werror)
18-
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-
3017
message(STATUS "Using LLVM library directories: ${LLVM_LIBRARY_DIRS}")
3118
link_directories(${LLVM_LIBRARY_DIRS})
3219

20+
add_compile_options(-fexceptions -std=c++11 -Wall -Wconversion -Werror)
21+
3322
add_executable(bindgen
3423
Main.cpp
3524
visitor/ScalaFrontendAction.h
@@ -85,14 +74,29 @@ add_executable(bindgen
8574
ir/types/ArrayType.h
8675
)
8776

77+
if (STATIC_LINKING)
78+
set(USE_SHARED OFF)
79+
if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
80+
# macOS does not guarantee backwards compatible system calls and therefore
81+
# discourages statically linked binaries. Instead add -L/usr/lib before the
82+
# LLVM LDFLAGS to link against the dynamic system libc++ instead of the one
83+
# from LLVM.
84+
set(BINDGEN_LINK_FLAG "-L/usr/lib")
85+
else()
86+
set(BINDGEN_LINK_FLAG "-static")
87+
endif()
88+
llvm_map_components_to_libnames(LLVM_LIBS support core irreader object option profiledata)
89+
else()
90+
set(LLVM_LIBS LLVM)
91+
endif()
92+
message(STATUS "Using link flag: ${BINDGEN_LINK_FLAG}")
93+
8894
set_target_properties(bindgen
8995
PROPERTIES
9096
OUTPUT_NAME scala-native-bindgen
9197
LINK_FLAGS "${BINDGEN_LINK_FLAG}"
9298
)
9399

94-
llvm_map_components_to_libnames(LLVM_LIBS support core irreader object option profiledata)
95-
96100
target_link_libraries(bindgen
97101
PRIVATE
98102
clangFrontend

bindgen/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ COPY . /src
88
RUN set -x \
99
&& mkdir target \
1010
&& cd target \
11-
&& cmake .. \
11+
&& cmake -DSTATIC_LINKING=ON .. \
1212
&& make
1313

1414
FROM scratch

0 commit comments

Comments
 (0)