Support building phobos against a system copy of zlib

This is achieved by linking the dynamic variant of phobos against zlib
and having the compiler add -lz when linking an application that
embeds the static variant of phobos.

Signed-off-by: Andrei Horodniceanu <a.horodniceanu@proton.me>
This commit is contained in:
Andrei Horodniceanu 2024-08-27 12:38:24 +03:00
parent 0cc531ee92
commit c2b350366a
No known key found for this signature in database
GPG key ID: D4D2730BE6398547
3 changed files with 47 additions and 17 deletions

View file

@ -149,6 +149,9 @@ set(LDC_ENABLE_ASSERTIONS "${LLVM_ENABLE_ASSERTIONS}" CACHE BOOL "Enable LDC ass
# Allow user to specify mimalloc.o location, to be linked with `ldc2` only
set(ALTERNATIVE_MALLOC_O "" CACHE STRING "If specified, adds ALTERNATIVE_MALLOC_O object file to LDC link, to override the CRT malloc.")
# Most linux distributions have a policy of not bundling dependencies like zlib
set(PHOBOS_SYSTEM_ZLIB OFF CACHE BOOL "Use system zlib instead of Phobos' vendored version")
if(D_VERSION EQUAL 1)
message(FATAL_ERROR "D version 1 is no longer supported.
Please consider using D version 2 or checkout the 'd1' git branch for the last version supporting D version 1.")
@ -290,6 +293,9 @@ if(SANITIZE)
endif()
endif()
append("${SANITIZE_CXXFLAGS}" LDC_CXXFLAGS)
if(PHOBOS_SYSTEM_ZLIB)
append("-DPHOBOS_SYSTEM_ZLIB" LDC_CXXFLAGS)
endif()
# LLVM_CXXFLAGS may contain -Werror which causes compile errors with dmd source
string(REPLACE "-Werror " "" LLVM_CXXFLAGS ${LLVM_CXXFLAGS})
if (UNIX AND NOT "${LLVM_LDFLAGS}" STREQUAL "")

View file

@ -583,6 +583,10 @@ void ArgsBuilder::build(llvm::StringRef outputPath,
for (const auto &name : defaultLibNames) {
args.push_back("-l" + name);
}
#ifdef PHOBOS_SYSTEM_ZLIB
if (!defaultLibNames.empty() && !linkAgainstSharedDefaultLibs())
args.push_back("-lz");
#endif
// libs added via pragma(lib, libname)
for (auto ls : global.params.linkswitches) {

View file

@ -61,6 +61,10 @@ if (RT_SUPPORT_SANITIZERS)
list(APPEND D_FLAGS -d-version=SupportSanitizers)
endif()
if(PHOBOS_SYSTEM_ZLIB)
message(STATUS "-- Building PHOBOS against system zlib")
endif()
# Auto-detect TARGET_SYSTEM from host
if("${TARGET_SYSTEM}" STREQUAL "AUTO")
set(TARGET_SYSTEM ${CMAKE_SYSTEM_NAME})
@ -243,14 +247,22 @@ if(PHOBOS2_DIR)
list(REMOVE_ITEM PHOBOS2_D ${PHOBOS2_D_WINDOWS})
endif()
# Phobos C parts
file(GLOB_RECURSE PHOBOS2_C ${PHOBOS2_DIR}/etc/*.c)
# remove zlib test modules
list(REMOVE_ITEM PHOBOS2_C
${PHOBOS2_DIR}/etc/c/zlib/test/example.c
${PHOBOS2_DIR}/etc/c/zlib/test/infcover.c
${PHOBOS2_DIR}/etc/c/zlib/test/minigzip.c
)
if(PHOBOS_SYSTEM_ZLIB)
find_package(ZLIB REQUIRED)
else()
# Phobos C parts
file(GLOB_RECURSE PHOBOS2_C ${PHOBOS2_DIR}/etc/*.c)
# remove zlib test modules
list(REMOVE_ITEM PHOBOS2_C
${PHOBOS2_DIR}/etc/c/zlib/test/example.c
${PHOBOS2_DIR}/etc/c/zlib/test/infcover.c
${PHOBOS2_DIR}/etc/c/zlib/test/minigzip.c
)
CHECK_INCLUDE_FILE(unistd.h HAVE_UNISTD_H)
if (HAVE_UNISTD_H)
append("-DHAVE_UNISTD_H" CMAKE_C_FLAGS)
endif()
endif()
endif()
#
@ -397,11 +409,6 @@ if("${TARGET_SYSTEM}" MATCHES "MSVC")
# warning C4996: zlib uses 'deprecated' POSIX names
append("/wd4100 /wd4127 /wd4131 /wd4206 /wd4244 /wd4245 /wd4267 /wd4996" CMAKE_C_FLAGS_RELEASE)
endif()
CHECK_INCLUDE_FILE(unistd.h HAVE_UNISTD_H)
if (HAVE_UNISTD_H)
# Needed for zlib
append("-DHAVE_UNISTD_H" CMAKE_C_FLAGS)
endif()
# 2) Set all other CMAKE_C_FLAGS variants to CMAKE_C_FLAGS_RELEASE
set(variables
CMAKE_C_FLAGS_DEBUG
@ -412,6 +419,16 @@ foreach(variable ${variables})
set(${variable} "${CMAKE_C_FLAGS_RELEASE}")
endforeach()
function(link_zlib phobos_target library_type)
if(PHOBOS_SYSTEM_ZLIB)
if(${library_type} STREQUAL "SHARED")
target_link_libraries(${phobos_target} ZLIB::ZLIB)
endif()
else()
target_sources(${phobos_target} PRIVATE ${PHOBOS2_C})
endif()
endfunction()
# Compiles the given D modules to object files, and if enabled, bitcode files.
# The paths of the output files are appended to outlist_o and outlist_bc, respectively.
macro(dc src_files src_basedir d_flags output_basedir emit_bc all_at_once single_obj_name outlist_o outlist_bc)
@ -638,8 +655,8 @@ macro(build_runtime_libs druntime_o druntime_bc phobos2_o phobos2_bc c_flags ld_
list(APPEND ${outlist_targets} druntime-ldc${target_suffix})
if(PHOBOS2_DIR)
add_library(phobos2-ldc${target_suffix} ${library_type}
${phobos2_o} ${PHOBOS2_C})
add_library(phobos2-ldc${target_suffix} ${library_type} ${phobos2_o})
link_zlib(phobos2-ldc${target_suffix} ${library_type})
set_common_library_properties(phobos2-ldc${target_suffix}
phobos2-ldc${lib_suffix} ${output_path}
"${c_flags}" "${ld_flags}" ${is_shared}
@ -669,8 +686,8 @@ macro(build_runtime_libs druntime_o druntime_bc phobos2_o phobos2_bc c_flags ld_
"${c_flags}" "${ld_flags}" OFF
)
add_library(phobos2-ldc-lto${target_suffix} STATIC
${phobos2_bc} ${PHOBOS2_C})
add_library(phobos2-ldc-lto${target_suffix} STATIC ${phobos2_bc})
link_zlib(phobos2-ldc-lto${target_suffix} STATIC)
set_common_library_properties(phobos2-ldc-lto${target_suffix}
phobos2-ldc-lto${lib_suffix} ${output_path}
"${c_flags}" "${ld_flags}" OFF
@ -1004,6 +1021,9 @@ function(build_test_runners name_suffix path_suffix d_flags linkflags is_shared)
LINK_FLAGS ${linkflags}
LINK_DEPENDS ${tested_lib_path}
)
if(PHOBOS_SYSTEM_ZLIB AND "${is_shared}" STREQUAL "OFF")
target_link_libraries(${phobos_name} ZLIB::ZLIB)
endif()
add_test(build-${phobos_name} "${CMAKE_COMMAND}" --build ${CMAKE_BINARY_DIR} --target ${phobos_name})
set(_GLOBAL_TESTRUNNERS "${_GLOBAL_TESTRUNNERS};${phobos_name}" CACHE INTERNAL "")
endif()