Skip to content

Commit b36bc77

Browse files
committed
Static build fixes
1 parent 37d6009 commit b36bc77

9 files changed

Lines changed: 137 additions & 43 deletions

File tree

dynadjust/CMakeLists.txt

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,19 @@ if(BUILD_TESTING)
6060
endif()
6161

6262
# Set output directories for all platforms to use ../bin/
63-
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/../bin)
64-
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/../bin)
65-
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/../bin)
63+
# Allow override via -DCMAKE_RUNTIME_OUTPUT_DIRECTORY=... on the command line.
64+
if(NOT DEFINED CACHE{CMAKE_RUNTIME_OUTPUT_DIRECTORY})
65+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/../bin)
66+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/../bin)
67+
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/../bin)
68+
endif()
6669

6770
# For multi-config generators (Visual Studio, Xcode)
6871
foreach(OUTPUTCONFIG ${CMAKE_CONFIGURATION_TYPES})
6972
string(TOUPPER ${OUTPUTCONFIG} OUTPUTCONFIG_UPPER)
70-
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_${OUTPUTCONFIG_UPPER} ${CMAKE_SOURCE_DIR}/../bin)
71-
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_${OUTPUTCONFIG_UPPER} ${CMAKE_SOURCE_DIR}/../bin)
72-
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${OUTPUTCONFIG_UPPER} ${CMAKE_SOURCE_DIR}/../bin)
73+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_${OUTPUTCONFIG_UPPER} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
74+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_${OUTPUTCONFIG_UPPER} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
75+
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${OUTPUTCONFIG_UPPER} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
7376
endforeach()
7477

7578
if (BUILD_STATIC)
@@ -189,8 +192,9 @@ else()
189192
endif()
190193
message(STATUS "Found XercesC (${XercesC_INCLUDE_DIRS} ${XercesC_LIBRARIES})")
191194

192-
# Static builds need ICU for Xerces transcoding (glibc iconv can't dlopen gconv modules in static binaries)
193-
if(BUILD_STATIC AND UNIX AND NOT APPLE)
195+
# Static builds need ICU for Xerces transcoding (glibc iconv can't dlopen gconv modules in static binaries).
196+
# musl builds don't need ICU — musl's iconv is built-in and works in static binaries.
197+
if(BUILD_STATIC AND UNIX AND NOT APPLE AND NOT MUSL_BUILD)
194198
find_package(ICU COMPONENTS uc data)
195199
if(ICU_FOUND)
196200
message(STATUS "Found ICU ${ICU_VERSION} for static Xerces transcoding")
@@ -608,6 +612,23 @@ if(BUILD_STATIC)
608612
endif()
609613
endforeach()
610614

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

dynadjust/cmake/StaticBuildOptimizations.cmake

Lines changed: 60 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,67 @@ function(optimize_static_target TARGET_NAME)
99

1010
# Platform-specific optimizations
1111
if(UNIX AND NOT APPLE)
12-
# Linux-specific optimizations
13-
target_link_options(${TARGET_NAME} PRIVATE
14-
-static
15-
-Wl,--gc-sections # Remove unused sections
16-
-Wl,--as-needed # Only link libraries that are actually used
17-
-Wl,-O2 # Optimize at link time
18-
-Wl,--strip-all # Strip all symbols
19-
)
12+
if(MUSL_BUILD)
13+
target_link_options(${TARGET_NAME} PRIVATE
14+
-static
15+
-Wl,--gc-sections
16+
-Wl,--as-needed
17+
-Wl,-O2
18+
-Wl,--strip-all
19+
)
20+
target_link_libraries(${TARGET_NAME} PRIVATE pthread m)
21+
else()
22+
target_link_options(${TARGET_NAME} PRIVATE
23+
-static-libgcc
24+
-static-libstdc++
25+
-Wl,--as-needed
26+
-Wl,-O2
27+
-Wl,--strip-all
28+
# glibc 2.34+ merged libpthread into libc; libpthread.a is an
29+
# empty stub. libstdc++.a references ALL pthread functions via
30+
# weak aliases (__gthrw_), and glibc defines them as WEAK (W)
31+
# in libc.a. LLD will not pull a weak-defined archive member to
32+
# satisfy a weak-undefined reference, leaving every pthread
33+
# symbol at address 0 and crashing at runtime.
34+
# Fix: add a strong undefined reference (-u) for each symbol.
35+
# This forces LLD to pull in each object from libc.a, making
36+
# the weak-defined symbol available for the weak refs to bind to.
37+
-Wl,-u,pthread_once
38+
-Wl,-u,pthread_getspecific
39+
-Wl,-u,pthread_setspecific
40+
-Wl,-u,pthread_create
41+
-Wl,-u,pthread_join
42+
-Wl,-u,pthread_equal
43+
-Wl,-u,pthread_self
44+
-Wl,-u,pthread_detach
45+
-Wl,-u,pthread_cancel
46+
-Wl,-u,pthread_mutex_lock
47+
-Wl,-u,pthread_mutex_trylock
48+
-Wl,-u,pthread_mutex_unlock
49+
-Wl,-u,pthread_mutex_init
50+
-Wl,-u,pthread_mutex_destroy
51+
-Wl,-u,pthread_cond_init
52+
-Wl,-u,pthread_cond_broadcast
53+
-Wl,-u,pthread_cond_signal
54+
-Wl,-u,pthread_cond_wait
55+
-Wl,-u,pthread_cond_timedwait
56+
-Wl,-u,pthread_cond_destroy
57+
-Wl,-u,pthread_key_create
58+
-Wl,-u,pthread_key_delete
59+
-Wl,-u,pthread_mutexattr_init
60+
-Wl,-u,pthread_mutexattr_settype
61+
-Wl,-u,pthread_mutexattr_destroy
62+
-Wl,-u,pthread_attr_init
63+
-Wl,-u,pthread_attr_destroy
64+
-Wl,-u,pthread_attr_setdetachstate
65+
-Wl,-u,pthread_exit
66+
-Wl,-u,__pthread_key_create
67+
)
68+
target_link_libraries(${TARGET_NAME} PRIVATE pthread m dl)
69+
endif()
2070
target_compile_options(${TARGET_NAME} PRIVATE
21-
-ffunction-sections # Put each function in its own section
22-
-fdata-sections # Put each data item in its own section
71+
-ffunction-sections
72+
-fdata-sections
2373
)
2474
elseif(APPLE)
2575
# macOS-specific optimizations

dynadjust/dynadjust/dnaadjustwrapper/CMakeLists.txt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,13 @@ if(BUILD_STATIC)
8686
"-framework Accelerate"
8787
"-framework CoreFoundation")
8888
else()
89-
target_link_options(${STATIC_TARGET_NAME} PRIVATE
90-
-static)
91-
89+
if(MUSL_BUILD)
90+
target_link_options(${STATIC_TARGET_NAME} PRIVATE -static)
91+
else()
92+
target_link_options(${STATIC_TARGET_NAME} PRIVATE
93+
-static-libgcc
94+
-static-libstdc++)
95+
endif()
9296
endif()
9397
elseif(WIN32)
9498
# On Windows with MSVC

dynadjust/dynadjust/dnadiff/CMakeLists.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,12 @@ 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+
if(MUSL_BUILD)
28+
target_link_options(${STATIC_TARGET_NAME} PRIVATE -static)
29+
else()
30+
target_link_options(${STATIC_TARGET_NAME} PRIVATE -static-libgcc -static-libstdc++)
31+
endif()
32+
target_link_libraries(${STATIC_TARGET_NAME} pthread)
2933
endif()
3034
endif()
3135

dynadjust/dynadjust/dnageoidwrapper/CMakeLists.txt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,13 @@ if(BUILD_STATIC)
8383
#CURL::libcurl
8484
"-framework CoreFoundation")
8585
else()
86-
target_link_options(${STATIC_TARGET_NAME} PRIVATE
87-
-static)
88-
86+
if(MUSL_BUILD)
87+
target_link_options(${STATIC_TARGET_NAME} PRIVATE -static)
88+
else()
89+
target_link_options(${STATIC_TARGET_NAME} PRIVATE
90+
-static-libgcc
91+
-static-libstdc++)
92+
endif()
8993
endif()
9094
elseif(WIN32)
9195
# On Windows with MSVC

dynadjust/dynadjust/dnaplotwrapper/CMakeLists.txt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,13 @@ if(BUILD_STATIC)
8282
"-framework Accelerate"
8383
"-framework CoreFoundation")
8484
else()
85-
target_link_options(${STATIC_TARGET_NAME} PRIVATE
86-
-static)
87-
85+
if(MUSL_BUILD)
86+
target_link_options(${STATIC_TARGET_NAME} PRIVATE -static)
87+
else()
88+
target_link_options(${STATIC_TARGET_NAME} PRIVATE
89+
-static-libgcc
90+
-static-libstdc++)
91+
endif()
8892
endif()
8993
elseif(WIN32)
9094
# On Windows with MSVC

dynadjust/dynadjust/dnareftranwrapper/CMakeLists.txt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,13 @@ if(BUILD_STATIC)
8484
#CURL::libcurl
8585
"-framework CoreFoundation")
8686
else()
87-
target_link_options(${STATIC_TARGET_NAME} PRIVATE
88-
-static)
89-
87+
if(MUSL_BUILD)
88+
target_link_options(${STATIC_TARGET_NAME} PRIVATE -static)
89+
else()
90+
target_link_options(${STATIC_TARGET_NAME} PRIVATE
91+
-static-libgcc
92+
-static-libstdc++)
93+
endif()
9094
endif()
9195
elseif(WIN32)
9296
# On Windows with MSVC

dynadjust/dynadjust/dnasegmentwrapper/CMakeLists.txt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,13 @@ if(BUILD_STATIC)
8989
#CURL::libcurl
9090
"-framework CoreFoundation")
9191
else()
92-
target_link_options(${STATIC_TARGET_NAME} PRIVATE
93-
-static)
94-
92+
if(MUSL_BUILD)
93+
target_link_options(${STATIC_TARGET_NAME} PRIVATE -static)
94+
else()
95+
target_link_options(${STATIC_TARGET_NAME} PRIVATE
96+
-static-libgcc
97+
-static-libstdc++)
98+
endif()
9599
endif()
96100
elseif(WIN32)
97101
# On Windows with MSVC

dynadjust/dynadjust/dynadjust/CMakeLists.txt

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,13 @@ if(BUILD_STATIC)
7070
#CURL::libcurl
7171
"-framework CoreFoundation")
7272
else()
73-
# On Linux/Unix, we can use full static linking
74-
target_link_options(${STATIC_TARGET_NAME} PRIVATE
75-
-Wl,-Bstatic
76-
-fno-builtin
77-
-static-libgcc
78-
-static-libstdc++
79-
-static)
80-
73+
if(MUSL_BUILD)
74+
target_link_options(${STATIC_TARGET_NAME} PRIVATE -static)
75+
else()
76+
target_link_options(${STATIC_TARGET_NAME} PRIVATE
77+
-static-libgcc
78+
-static-libstdc++)
79+
endif()
8180
endif()
8281
elseif(WIN32)
8382
# On Windows with MSVC

0 commit comments

Comments
 (0)