11cmake_minimum_required (VERSION 3.9)
22project (scala-native-bindgen)
33
4- set (USE_SHARED OFF )
4+ option (STATIC_LINKING "Statically link the executable" OFF )
55
66# Locate LLVMConfig.cmake
77find_package (LLVM REQUIRED CONFIG)
@@ -14,22 +14,11 @@ include_directories(SYSTEM ${LLVM_INCLUDE_DIRS})
1414message (STATUS "Using LLVM defs: ${LLVM_DEFINITIONS} " )
1515add_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-
3017message (STATUS "Using LLVM library directories: ${LLVM_LIBRARY_DIRS} " )
3118link_directories (${LLVM_LIBRARY_DIRS} )
3219
20+ add_compile_options (-fexceptions -std=c++11 -Wall -Wconversion -Werror)
21+
3322add_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+
8894set_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-
96100target_link_libraries (bindgen
97101 PRIVATE
98102 clangFrontend
0 commit comments