Skip to content

Commit 131b6d8

Browse files
authored
Merge pull request #354 from GeoscienceAustralia/dr-341
Static build fixes
2 parents a90fbce + 346cb9e commit 131b6d8

9 files changed

Lines changed: 74 additions & 36 deletions

File tree

dynadjust/CMakeLists.txt

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ endif()
7373
# For multi-config generators (Visual Studio, Xcode)
7474
foreach(OUTPUTCONFIG ${CMAKE_CONFIGURATION_TYPES})
7575
string(TOUPPER ${OUTPUTCONFIG} OUTPUTCONFIG_UPPER)
76-
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_${OUTPUTCONFIG_UPPER} ${CMAKE_SOURCE_DIR}/../bin)
77-
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_${OUTPUTCONFIG_UPPER} ${CMAKE_SOURCE_DIR}/../bin)
78-
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${OUTPUTCONFIG_UPPER} ${CMAKE_SOURCE_DIR}/../bin)
76+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_${OUTPUTCONFIG_UPPER} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
77+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_${OUTPUTCONFIG_UPPER} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
78+
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${OUTPUTCONFIG_UPPER} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
7979
endforeach()
8080

8181
if (BUILD_STATIC)
@@ -617,6 +617,23 @@ if(BUILD_STATIC)
617617
endif()
618618
endforeach()
619619

620+
# For MKL builds, add static MKL libraries (MKL::MKL is an imported
621+
# target that the generic .a finder above cannot resolve)
622+
if(USE_MKL AND MKL_FOUND AND DEFINED ENV{MKLROOT})
623+
set(MKL_LIB_DIR "$ENV{MKLROOT}/lib")
624+
if(ILP64)
625+
set(MKL_INTERFACE_LIB "${MKL_LIB_DIR}/libmkl_intel_ilp64.a")
626+
else()
627+
set(MKL_INTERFACE_LIB "${MKL_LIB_DIR}/libmkl_intel_lp64.a")
628+
endif()
629+
list(APPEND STATIC_LIBS_LIST
630+
"-Wl,--start-group"
631+
"${MKL_INTERFACE_LIB}"
632+
"${MKL_LIB_DIR}/libmkl_sequential.a"
633+
"${MKL_LIB_DIR}/libmkl_core.a"
634+
"-Wl,--end-group")
635+
endif()
636+
620637
message("")
621638
message(STATUS "STATIC_LIBS_LIST=${STATIC_LIBS_LIST}")
622639

dynadjust/cmake/StaticBuildOptimizations.cmake

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,56 @@ function(optimize_static_target TARGET_NAME)
99

1010
# Platform-specific optimizations
1111
if(UNIX AND NOT APPLE)
12-
# Linux-specific optimizations
1312
target_link_options(${TARGET_NAME} PRIVATE
14-
-static
15-
-pthread
16-
# Clang + static glibc: libstdc++'s gthr-posix.h uses __weakref__ aliases
17-
# for pthread functions. GCC resolves these at link time, but Clang leaves
18-
# them as null, causing segfaults when std::thread calls through them.
19-
# Force the linker to pull in the real symbols from libc.a.
13+
-static-libgcc
14+
-static-libstdc++
15+
-Wl,--as-needed
16+
-Wl,-O2
17+
-Wl,--strip-all
18+
# glibc 2.34+ merged libpthread into libc; libpthread.a is an
19+
# empty stub. libstdc++.a references ALL pthread functions via
20+
# weak aliases (__gthrw_), and glibc defines them as WEAK (W)
21+
# in libc.a. LLD will not pull a weak-defined archive member to
22+
# satisfy a weak-undefined reference, leaving every pthread
23+
# symbol at address 0 and crashing at runtime.
24+
# Fix: add a strong undefined reference (-u) for each symbol.
25+
# This forces LLD to pull in each object from libc.a, making
26+
# the weak-defined symbol available for the weak refs to bind to.
27+
-Wl,-u,pthread_once
28+
-Wl,-u,pthread_getspecific
29+
-Wl,-u,pthread_setspecific
2030
-Wl,-u,pthread_create
2131
-Wl,-u,pthread_join
22-
-Wl,-u,pthread_cancel
32+
-Wl,-u,pthread_equal
33+
-Wl,-u,pthread_self
2334
-Wl,-u,pthread_detach
35+
-Wl,-u,pthread_cancel
2436
-Wl,-u,pthread_mutex_lock
37+
-Wl,-u,pthread_mutex_trylock
2538
-Wl,-u,pthread_mutex_unlock
26-
-Wl,-u,pthread_once
39+
-Wl,-u,pthread_mutex_init
40+
-Wl,-u,pthread_mutex_destroy
41+
-Wl,-u,pthread_cond_init
42+
-Wl,-u,pthread_cond_broadcast
43+
-Wl,-u,pthread_cond_signal
44+
-Wl,-u,pthread_cond_wait
45+
-Wl,-u,pthread_cond_timedwait
46+
-Wl,-u,pthread_cond_destroy
2747
-Wl,-u,pthread_key_create
28-
-Wl,--gc-sections # Remove unused sections
29-
-Wl,--as-needed # Only link libraries that are actually used
30-
-Wl,-O2 # Optimize at link time
31-
-Wl,--strip-all # Strip all symbols
48+
-Wl,-u,pthread_key_delete
49+
-Wl,-u,pthread_mutexattr_init
50+
-Wl,-u,pthread_mutexattr_settype
51+
-Wl,-u,pthread_mutexattr_destroy
52+
-Wl,-u,pthread_attr_init
53+
-Wl,-u,pthread_attr_destroy
54+
-Wl,-u,pthread_attr_setdetachstate
55+
-Wl,-u,pthread_exit
56+
-Wl,-u,__pthread_key_create
3257
)
58+
target_link_libraries(${TARGET_NAME} PRIVATE pthread m dl)
3359
target_compile_options(${TARGET_NAME} PRIVATE
34-
-ffunction-sections # Put each function in its own section
35-
-fdata-sections # Put each data item in its own section
60+
-ffunction-sections
61+
-fdata-sections
3662
)
3763
elseif(APPLE)
3864
# macOS-specific optimizations

dynadjust/dynadjust/dnaadjustwrapper/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ if(BUILD_STATIC)
8787
"-framework CoreFoundation")
8888
else()
8989
target_link_options(${STATIC_TARGET_NAME} PRIVATE
90-
-static)
91-
90+
-static-libgcc
91+
-static-libstdc++)
9292
endif()
9393
elseif(WIN32)
9494
# On Windows with MSVC

dynadjust/dynadjust/dnadiff/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ else()
2424
# Link statically
2525
if(UNIX)
2626
if(NOT APPLE)
27-
SET_TARGET_PROPERTIES(${STATIC_TARGET_NAME} PROPERTIES LINK_FLAGS "-static")
28-
target_link_libraries (${STATIC_TARGET_NAME} pthread)
27+
target_link_options(${STATIC_TARGET_NAME} PRIVATE -static-libgcc -static-libstdc++)
28+
target_link_libraries(${STATIC_TARGET_NAME} pthread)
2929
endif()
3030
endif()
3131

dynadjust/dynadjust/dnageoidwrapper/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ if(BUILD_STATIC)
8484
"-framework CoreFoundation")
8585
else()
8686
target_link_options(${STATIC_TARGET_NAME} PRIVATE
87-
-static)
88-
87+
-static-libgcc
88+
-static-libstdc++)
8989
endif()
9090
elseif(WIN32)
9191
# On Windows with MSVC

dynadjust/dynadjust/dnaplotwrapper/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ if(BUILD_STATIC)
8383
"-framework CoreFoundation")
8484
else()
8585
target_link_options(${STATIC_TARGET_NAME} PRIVATE
86-
-static)
87-
86+
-static-libgcc
87+
-static-libstdc++)
8888
endif()
8989
elseif(WIN32)
9090
# On Windows with MSVC

dynadjust/dynadjust/dnareftranwrapper/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ if(BUILD_STATIC)
8585
"-framework CoreFoundation")
8686
else()
8787
target_link_options(${STATIC_TARGET_NAME} PRIVATE
88-
-static)
89-
88+
-static-libgcc
89+
-static-libstdc++)
9090
endif()
9191
elseif(WIN32)
9292
# On Windows with MSVC

dynadjust/dynadjust/dnasegmentwrapper/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ if(BUILD_STATIC)
9090
"-framework CoreFoundation")
9191
else()
9292
target_link_options(${STATIC_TARGET_NAME} PRIVATE
93-
-static)
94-
93+
-static-libgcc
94+
-static-libstdc++)
9595
endif()
9696
elseif(WIN32)
9797
# On Windows with MSVC

dynadjust/dynadjust/dynadjust/CMakeLists.txt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,9 @@ if(BUILD_STATIC)
7070
#CURL::libcurl
7171
"-framework CoreFoundation")
7272
else()
73-
# On Linux/Unix, we can use full static linking
7473
target_link_options(${STATIC_TARGET_NAME} PRIVATE
75-
-Wl,-Bstatic
76-
-fno-builtin
7774
-static-libgcc
78-
-static-libstdc++
79-
-static)
80-
75+
-static-libstdc++)
8176
endif()
8277
elseif(WIN32)
8378
# On Windows with MSVC

0 commit comments

Comments
 (0)