Skip to content

Commit

Permalink
Detect musl in cmake and link with libunwind (#4645)
Browse files Browse the repository at this point in the history
  • Loading branch information
JohanEngelen authored May 17, 2024
1 parent 59c0c60 commit 4c2c8ca
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
17 changes: 16 additions & 1 deletion runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ set(RT_ARCHIVE_WITH_LDC ON CACHE STRING "Whet
set(RT_CFLAGS "" CACHE STRING "Runtime extra C compiler flags, separated by ' '")
set(LD_FLAGS "" CACHE STRING "Runtime extra C linker flags, separated by ' '")
set(C_SYSTEM_LIBS AUTO CACHE STRING "C system libraries for linking shared libraries and test runners, separated by ';'")
set(TARGET_SYSTEM AUTO CACHE STRING "Target OS/toolchain for cross-compilation (e.g., 'Linux;UNIX', 'Darwin;APPLE;UNIX', 'Windows;MSVC')")
set(TARGET_SYSTEM AUTO CACHE STRING "Target OS/toolchain for cross-compilation (e.g., 'Linux;UNIX', 'Linux;UNIX;musl', 'Darwin;APPLE;UNIX', 'Windows;MSVC')")
set(RT_SUPPORT_SANITIZERS OFF CACHE BOOL "Build runtime libraries with sanitizer support (e.g. for AddressSanitizer)")

set(CMAKE_INSTALL_LIBDIR ${CMAKE_INSTALL_PREFIX}/lib${LIB_SUFFIX})
Expand Down Expand Up @@ -73,6 +73,17 @@ if("${TARGET_SYSTEM}" STREQUAL "AUTO")
endif()
if(UNIX)
list(APPEND TARGET_SYSTEM "UNIX")

# Determines if host system uses musl libc
execute_process(COMMAND ldd /bin/ls OUTPUT_VARIABLE OUTPUT ERROR_VARIABLE OUTPUT RESULT_VARIABLE RETVAL)
if(NOT ${RETVAL})
if("${OUTPUT}" MATCHES "-musl-" )
if(NOT CMAKE_REQUIRED_QUIET)
message(STATUS "Detected musl libc")
endif()
list(APPEND TARGET_SYSTEM "musl")
endif()
endif()
endif()
endif()

Expand Down Expand Up @@ -122,6 +133,9 @@ if("${C_SYSTEM_LIBS}" STREQUAL "AUTO")
set(C_SYSTEM_LIBS m c)
elseif("${TARGET_SYSTEM}" MATCHES "Linux")
set(C_SYSTEM_LIBS m pthread rt dl)
if("${TARGET_SYSTEM}" MATCHES "musl")
list(APPEND C_SYSTEM_LIBS "unwind")
endif()
elseif("${TARGET_SYSTEM}" MATCHES "FreeBSD")
set(C_SYSTEM_LIBS m pthread execinfo z)
else()
Expand All @@ -137,6 +151,7 @@ message(STATUS "-- LDC runtime configuration:")
message(STATUS "-- - Building 32/64-bit libraries (MULTILIB): ${MULTILIB}")
message(STATUS "-- - Building shared libraries (BUILD_SHARED_LIBS): ${BUILD_SHARED_LIBS}")
message(STATUS "-- - Building LTO libraries (BUILD_LTO_LIBS): ${BUILD_LTO_LIBS}")
message(STATUS "-- - Linking shared libraries (and test runners) with (C_SYSTEM_LIBS): ${C_SYSTEM_LIBS}")

get_directory_property(PROJECT_PARENT_DIR DIRECTORY ${PROJECT_SOURCE_DIR} PARENT_DIRECTORY)
set(RUNTIME_DIR ${PROJECT_SOURCE_DIR}/druntime CACHE PATH "druntime root directory")
Expand Down
6 changes: 5 additions & 1 deletion runtime/DRuntimeIntegrationTests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ set(linkdl "")
if("${TARGET_SYSTEM}" MATCHES "Linux")
set(linkdl "LINKDL=-L-ldl")
endif()
set(linkunwind "")
if("${TARGET_SYSTEM}" MATCHES "musl")
set(linkunwind "LINKUNWIND=-L-lunwind")
endif()

get_subdirs(testnames ${PROJECT_SOURCE_DIR}/druntime/test)
if(${BUILD_SHARED_LIBS} STREQUAL "OFF")
Expand Down Expand Up @@ -74,7 +78,7 @@ foreach(name ${testnames})
COMMAND ${GNU_MAKE_BIN} -C ${PROJECT_SOURCE_DIR}/druntime/test/${name}
ROOT=${outdir} DMD=${LDMD_EXE_FULL} BUILD=${build}
DRUNTIME=${druntime_path_build} DRUNTIMESO=${shared_druntime_path_build}
SHARED=1 ${cflags_base} ${linkdl}
SHARED=1 ${cflags_base} ${linkdl} ${linkunwind}
)
set_tests_properties(${fullname} PROPERTIES DEPENDS clean-${fullname})
endforeach()
Expand Down
2 changes: 2 additions & 0 deletions runtime/druntime/test/common.mak
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ DMD:=
DRUNTIME:=
DRUNTIMESO:=
LINKDL:=
LINKUNWIND:=
QUIET:=
TIMELIMIT:=
PIC:=
Expand All @@ -25,6 +26,7 @@ ifeq (,$(findstring ldmd2,$(DMD)))
endif

LDL:=$(subst -L,,$(LINKDL)) # -ldl
LUNWIND:=$(subst -L,,$(LINKUNWIND)) # -lunwind
SRC:=src
GENERATED:=./generated
ROOT:=$(GENERATED)/$(OS)/$(BUILD)/$(MODEL)
Expand Down
8 changes: 4 additions & 4 deletions runtime/druntime/test/stdcpp/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -98,22 +98,22 @@ $(ROOT)/%_old.done: $(ROOT)/%_old$(DOTEXE)
$(ROOT)/%$(DOTEXE): $(SRC)/%.cpp $(SRC)/%_test.d
@mkdir -p $(dir $@)
$(QUIET)$(DMD) $(DFLAGS) -extern-std=c++98 -main -unittest -version=CoreUnittest -c -of=$(ROOT)/$*_d$(DOTOBJ) $(SRC)/$*_test.d
$(QUIET)$(CXX) $(CXXFLAGS_BASE) -std=c++98 -o $@ $< $(ROOT)/$*_d$(DOTOBJ) $(DRUNTIME) -lpthread $(LDL)
$(QUIET)$(CXX) $(CXXFLAGS_BASE) -std=c++98 -o $@ $< $(ROOT)/$*_d$(DOTOBJ) $(DRUNTIME) -lpthread $(LDL) $(LUNWIND)
# build C++11 tests
$(ROOT)/%_11$(DOTEXE): $(SRC)/%.cpp $(SRC)/%_test.d
@mkdir -p $(dir $@)
$(QUIET)$(DMD) $(DFLAGS) -extern-std=c++11 -main -unittest -version=CoreUnittest -c -of=$(ROOT)/$*_11_d$(DOTOBJ) $(SRC)/$*_test.d
$(QUIET)$(CXX) $(CXXFLAGS_BASE) -std=c++11 -o $@ $< $(ROOT)/$*_11_d$(DOTOBJ) $(DRUNTIME) -lpthread $(LDL)
$(QUIET)$(CXX) $(CXXFLAGS_BASE) -std=c++11 -o $@ $< $(ROOT)/$*_11_d$(DOTOBJ) $(DRUNTIME) -lpthread $(LDL) $(LUNWIND)
# build C++17 tests
$(ROOT)/%_17$(DOTEXE): $(SRC)/%.cpp $(SRC)/%_test.d
@mkdir -p $(dir $@)
$(QUIET)$(DMD) $(DFLAGS) -extern-std=c++17 -main -unittest -version=CoreUnittest -c -of=$(ROOT)/$*_17_d$(DOTOBJ) $(SRC)/$*_test.d
$(QUIET)$(CXX) $(CXXFLAGS_BASE) -std=c++17 -o $@ $< $(ROOT)/$*_17_d$(DOTOBJ) $(DRUNTIME) -lpthread $(LDL)
$(QUIET)$(CXX) $(CXXFLAGS_BASE) -std=c++17 -o $@ $< $(ROOT)/$*_17_d$(DOTOBJ) $(DRUNTIME) -lpthread $(LDL) $(LUNWIND)
# build libstdc++ _GLIBCXX_USE_CXX11_ABI=0 tests
$(ROOT)/%_old$(DOTEXE): $(SRC)/%.cpp $(SRC)/%_test.d
@mkdir -p $(dir $@)
$(QUIET)$(DMD) $(DFLAGS) -version=_GLIBCXX_USE_CXX98_ABI -main -unittest -version=CoreUnittest -c -of=$(ROOT)/$*_old_d$(DOTOBJ) $(SRC)/$*_test.d
$(QUIET)$(CXX) $(CXXFLAGS_BASE) -D_GLIBCXX_USE_CXX11_ABI=0 -o $@ $< $(ROOT)/$*_old_d$(DOTOBJ) $(DRUNTIME) -lpthread $(LDL)
$(QUIET)$(CXX) $(CXXFLAGS_BASE) -D_GLIBCXX_USE_CXX11_ABI=0 -o $@ $< $(ROOT)/$*_old_d$(DOTOBJ) $(DRUNTIME) -lpthread $(LDL) $(LUNWIND)

endif # end Posix

Expand Down

0 comments on commit 4c2c8ca

Please # to comment.