Skip to content

Commit d562736

Browse files
authored
Merge pull request ldc-developers#4742 from the-horo/zlib
Support building phobos against a system copy of zlib
2 parents 0cc531e + 4859d83 commit d562736

File tree

6 files changed

+56
-22
lines changed

6 files changed

+56
-22
lines changed

.github/workflows/supported_llvm_versions.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
host_dc: ldc-1.19.0
2121
# FIXME: no usable official package available yet
2222
llvm_version: https://github.com/ldc-developers/llvm-project/releases/download/ldc-v18.1.8/llvm-18.1.8-linux-x86_64.tar.xz
23-
cmake_flags: -DRT_SUPPORT_SANITIZERS=ON
23+
cmake_flags: -DRT_SUPPORT_SANITIZERS=ON -DPHOBOS_SYSTEM_ZLIB=ON
2424
- job_name: macOS 14, LLVM 17, latest LDC beta
2525
os: macos-14
2626
host_dc: ldc-beta
@@ -63,13 +63,13 @@ jobs:
6363
python3 -m pip install --user lit
6464
fi
6565
python3 -c "import lit.main; lit.main.main();" --version . | head -n 1
66-
- name: 'Linux: Install gdb, llvm-symbolizer and libzstd'
66+
- name: 'Linux: Install gdb, llvm-symbolizer, libzstd, zlib1g-dev'
6767
if: runner.os == 'Linux'
6868
run: |
6969
set -eux
7070
sudo apt-get update
7171
# Don't use latest gdb v10+ from Ubuntu toolchain PPA with regressions, use official v9
72-
sudo apt-get install gdb=9.1-0ubuntu1 llvm libzstd-dev
72+
sudo apt-get install gdb=9.1-0ubuntu1 llvm libzstd-dev zlib1g-dev
7373
7474
- name: Try to restore cached LLVM
7575
uses: actions/cache@v4

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- LLVM for prebuilt packages bumped to v18.1.8 (incl. macOS arm64). (#4712)
66
- Android: NDK for prebuilt package bumped from r26d to r27. (#4711)
77
- ldc2.conf: %%ldcconfigpath%% placeholder added - specifies the directory where current configuration file is located. (#4717)
8+
- Add support for building against a system copy of zlib through `-DPHOBOS_SYSTEM_ZLIB=ON`. (#4742)
89

910
#### Platform support
1011

@@ -923,7 +924,7 @@
923924
- Misc. debuginfo issues, incl. adaptations to internal LLVM 5.0 changes: (#2315)
924925
- `ref` parameters and closure parameters declared with wrong address and hence potentially showing garbage.
925926
- Win64: parameters > 64 bit passed by value showing garbage.
926-
- Win64: debuginfos for closure and nested variables now finally available starting with LLVM 5.0.
927+
- Win64: debuginfos for closure and nested variables now finally available starting with LLVM 5.0.
927928
- LLVM error `Global variable initializer type does not match global variable type!` for `T.init` with explicit initializers for dominated members in nested unions. (#2108)
928929
- Inconsistent handling of lvalue slicees wrt. visible side-effects of slice lower/upper bound expressions. (#1433)
929930
- Misc. dcompute issues. (#2195, #2215)

CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ set(LDC_ENABLE_ASSERTIONS "${LLVM_ENABLE_ASSERTIONS}" CACHE BOOL "Enable LDC ass
149149
# Allow user to specify mimalloc.o location, to be linked with `ldc2` only
150150
set(ALTERNATIVE_MALLOC_O "" CACHE STRING "If specified, adds ALTERNATIVE_MALLOC_O object file to LDC link, to override the CRT malloc.")
151151

152+
# Most linux distributions have a policy of not bundling dependencies like zlib
153+
set(PHOBOS_SYSTEM_ZLIB OFF CACHE BOOL "Use system zlib instead of Phobos' vendored version")
154+
152155
if(D_VERSION EQUAL 1)
153156
message(FATAL_ERROR "D version 1 is no longer supported.
154157
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)
290293
endif()
291294
endif()
292295
append("${SANITIZE_CXXFLAGS}" LDC_CXXFLAGS)
296+
if(PHOBOS_SYSTEM_ZLIB)
297+
append("-DPHOBOS_SYSTEM_ZLIB" LDC_CXXFLAGS)
298+
endif()
293299
# LLVM_CXXFLAGS may contain -Werror which causes compile errors with dmd source
294300
string(REPLACE "-Werror " "" LLVM_CXXFLAGS ${LLVM_CXXFLAGS})
295301
if (UNIX AND NOT "${LLVM_LDFLAGS}" STREQUAL "")

driver/linker-gcc.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,10 @@ void ArgsBuilder::build(llvm::StringRef outputPath,
583583
for (const auto &name : defaultLibNames) {
584584
args.push_back("-l" + name);
585585
}
586+
#ifdef PHOBOS_SYSTEM_ZLIB
587+
if (!defaultLibNames.empty() && !linkAgainstSharedDefaultLibs())
588+
args.push_back("-lz");
589+
#endif
586590

587591
// libs added via pragma(lib, libname)
588592
for (auto ls : global.params.linkswitches) {

runtime/CMakeLists.txt

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ if (RT_SUPPORT_SANITIZERS)
6161
list(APPEND D_FLAGS -d-version=SupportSanitizers)
6262
endif()
6363

64+
if(PHOBOS_SYSTEM_ZLIB)
65+
message(STATUS "-- Building PHOBOS against system zlib")
66+
endif()
67+
6468
# Auto-detect TARGET_SYSTEM from host
6569
if("${TARGET_SYSTEM}" STREQUAL "AUTO")
6670
set(TARGET_SYSTEM ${CMAKE_SYSTEM_NAME})
@@ -243,14 +247,22 @@ if(PHOBOS2_DIR)
243247
list(REMOVE_ITEM PHOBOS2_D ${PHOBOS2_D_WINDOWS})
244248
endif()
245249

246-
# Phobos C parts
247-
file(GLOB_RECURSE PHOBOS2_C ${PHOBOS2_DIR}/etc/*.c)
248-
# remove zlib test modules
249-
list(REMOVE_ITEM PHOBOS2_C
250-
${PHOBOS2_DIR}/etc/c/zlib/test/example.c
251-
${PHOBOS2_DIR}/etc/c/zlib/test/infcover.c
252-
${PHOBOS2_DIR}/etc/c/zlib/test/minigzip.c
253-
)
250+
if(PHOBOS_SYSTEM_ZLIB)
251+
find_package(ZLIB REQUIRED)
252+
else()
253+
# Phobos C parts
254+
file(GLOB_RECURSE PHOBOS2_C ${PHOBOS2_DIR}/etc/*.c)
255+
# remove zlib test modules
256+
list(REMOVE_ITEM PHOBOS2_C
257+
${PHOBOS2_DIR}/etc/c/zlib/test/example.c
258+
${PHOBOS2_DIR}/etc/c/zlib/test/infcover.c
259+
${PHOBOS2_DIR}/etc/c/zlib/test/minigzip.c
260+
)
261+
CHECK_INCLUDE_FILE(unistd.h HAVE_UNISTD_H)
262+
if (HAVE_UNISTD_H)
263+
append("-DHAVE_UNISTD_H" CMAKE_C_FLAGS)
264+
endif()
265+
endif()
254266
endif()
255267

256268
#
@@ -397,11 +409,6 @@ if("${TARGET_SYSTEM}" MATCHES "MSVC")
397409
# warning C4996: zlib uses 'deprecated' POSIX names
398410
append("/wd4100 /wd4127 /wd4131 /wd4206 /wd4244 /wd4245 /wd4267 /wd4996" CMAKE_C_FLAGS_RELEASE)
399411
endif()
400-
CHECK_INCLUDE_FILE(unistd.h HAVE_UNISTD_H)
401-
if (HAVE_UNISTD_H)
402-
# Needed for zlib
403-
append("-DHAVE_UNISTD_H" CMAKE_C_FLAGS)
404-
endif()
405412
# 2) Set all other CMAKE_C_FLAGS variants to CMAKE_C_FLAGS_RELEASE
406413
set(variables
407414
CMAKE_C_FLAGS_DEBUG
@@ -412,6 +419,16 @@ foreach(variable ${variables})
412419
set(${variable} "${CMAKE_C_FLAGS_RELEASE}")
413420
endforeach()
414421

422+
function(link_zlib phobos_target library_type)
423+
if(PHOBOS_SYSTEM_ZLIB)
424+
if(${library_type} STREQUAL "SHARED")
425+
target_link_libraries(${phobos_target} ZLIB::ZLIB)
426+
endif()
427+
else()
428+
target_sources(${phobos_target} PRIVATE ${PHOBOS2_C})
429+
endif()
430+
endfunction()
431+
415432
# Compiles the given D modules to object files, and if enabled, bitcode files.
416433
# The paths of the output files are appended to outlist_o and outlist_bc, respectively.
417434
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_
638655
list(APPEND ${outlist_targets} druntime-ldc${target_suffix})
639656

640657
if(PHOBOS2_DIR)
641-
add_library(phobos2-ldc${target_suffix} ${library_type}
642-
${phobos2_o} ${PHOBOS2_C})
658+
add_library(phobos2-ldc${target_suffix} ${library_type} ${phobos2_o})
659+
link_zlib(phobos2-ldc${target_suffix} ${library_type})
643660
set_common_library_properties(phobos2-ldc${target_suffix}
644661
phobos2-ldc${lib_suffix} ${output_path}
645662
"${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_
669686
"${c_flags}" "${ld_flags}" OFF
670687
)
671688

672-
add_library(phobos2-ldc-lto${target_suffix} STATIC
673-
${phobos2_bc} ${PHOBOS2_C})
689+
add_library(phobos2-ldc-lto${target_suffix} STATIC ${phobos2_bc})
690+
link_zlib(phobos2-ldc-lto${target_suffix} STATIC)
674691
set_common_library_properties(phobos2-ldc-lto${target_suffix}
675692
phobos2-ldc-lto${lib_suffix} ${output_path}
676693
"${c_flags}" "${ld_flags}" OFF
@@ -1004,6 +1021,9 @@ function(build_test_runners name_suffix path_suffix d_flags linkflags is_shared)
10041021
LINK_FLAGS ${linkflags}
10051022
LINK_DEPENDS ${tested_lib_path}
10061023
)
1024+
if(PHOBOS_SYSTEM_ZLIB AND "${is_shared}" STREQUAL "OFF")
1025+
target_link_libraries(${phobos_name} ZLIB::ZLIB)
1026+
endif()
10071027
add_test(build-${phobos_name} "${CMAKE_COMMAND}" --build ${CMAKE_BINARY_DIR} --target ${phobos_name})
10081028
set(_GLOBAL_TESTRUNNERS "${_GLOBAL_TESTRUNNERS};${phobos_name}" CACHE INTERNAL "")
10091029
endif()

runtime/ldc-build-runtime.d.in

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ struct Config {
2020
string[] cFlags;
2121
string[] linkerFlags;
2222
uint numBuildJobs;
23+
bool systemZlib;
2324
string[string] cmakeVars;
2425
}
2526

@@ -160,6 +161,7 @@ void runCMake() {
160161
if (config.dFlags.length) args ~= "-DD_EXTRA_FLAGS=" ~ config.dFlags.join(";");
161162
if (config.cFlags.length) args ~= "-DRT_CFLAGS=" ~ config.cFlags.join(" ");
162163
if (config.linkerFlags.length) args ~= "-DLD_FLAGS=" ~ config.linkerFlags.join(" ");
164+
if (config.systemZlib) args ~= "-DPHOBOS_SYSTEM_ZLIB=ON";
163165

164166
foreach (pair; config.cmakeVars.byPair)
165167
args ~= "-D" ~ pair[0] ~ '=' ~ pair[1];
@@ -324,7 +326,8 @@ void parseCommandLine(string[] args) {
324326
"dFlags", "Extra LDC flags for the D modules (separated by ';')", &config.dFlags,
325327
"cFlags", "Extra C/ASM compiler flags for the handful of C/ASM files (separated by ';')", &config.cFlags,
326328
"linkerFlags", "Extra C linker flags for shared libraries and testrunner executables (separated by ';')", &config.linkerFlags,
327-
"j", "Number of parallel build jobs", &config.numBuildJobs
329+
"j", "Number of parallel build jobs", &config.numBuildJobs,
330+
"systemZlib", "Use system zlib instead of Phobos' vendored version", &config.systemZlib,
328331
);
329332

330333
// getopt() has removed all consumed args from `args`

0 commit comments

Comments
 (0)