@@ -61,6 +61,10 @@ if (RT_SUPPORT_SANITIZERS)
6161 list (APPEND D_FLAGS -d-version=SupportSanitizers)
6262endif ()
6363
64+ if (PHOBOS_SYSTEM_ZLIB)
65+ message (STATUS "-- Building PHOBOS against system zlib" )
66+ endif ()
67+
6468# Auto-detect TARGET_SYSTEM from host
6569if ("${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 ()
254266endif ()
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 )
399411endif ()
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
406413set (variables
407414 CMAKE_C_FLAGS_DEBUG
@@ -412,6 +419,16 @@ foreach(variable ${variables})
412419 set (${variable} "${CMAKE_C_FLAGS_RELEASE} " )
413420endforeach ()
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.
417434macro (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 ()
0 commit comments