diff --git a/eng/native/configurecompiler.cmake b/eng/native/configurecompiler.cmake index ea2ace1672750..c30f3ffef5e48 100644 --- a/eng/native/configurecompiler.cmake +++ b/eng/native/configurecompiler.cmake @@ -85,6 +85,7 @@ if (MSVC) #shrink pdb size set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /PDBCOMPRESS") + set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /INCREMENTAL:NO") set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /DEBUG") set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /DEBUGTYPE:CV,FIXUP") set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /IGNORE:4197,4013,4254,4070,4221") diff --git a/eng/native/functions.cmake b/eng/native/functions.cmake index 6629e926afacf..b1cb09d662043 100644 --- a/eng/native/functions.cmake +++ b/eng/native/functions.cmake @@ -221,7 +221,7 @@ function(preprocess_file inputFilename outputFilename) get_compile_definitions(PREPROCESS_DEFINITIONS) get_include_directories(PREPROCESS_INCLUDE_DIRECTORIES) get_source_file_property(SOURCE_FILE_DEFINITIONS ${inputFilename} COMPILE_DEFINITIONS) - + foreach(DEFINITION IN LISTS SOURCE_FILE_DEFINITIONS) list(APPEND PREPROCESS_DEFINITIONS -D${DEFINITION}) endforeach() @@ -508,7 +508,7 @@ function(install_static_library targetName destination component) if (WIN32) set_target_properties(${targetName} PROPERTIES COMPILE_PDB_NAME "${targetName}" - COMPILE_PDB_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}" + COMPILE_PDB_OUTPUT_DIRECTORY "$" ) install (FILES "$/${targetName}.pdb" DESTINATION ${destination} COMPONENT ${component}) endif() diff --git a/src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.Windows.targets b/src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.Windows.targets index 18c3c1f017a9f..603a255fc0ca0 100644 --- a/src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.Windows.targets +++ b/src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.Windows.targets @@ -46,6 +46,7 @@ The .NET Foundation licenses this file to you under the MIT license. + diff --git a/src/mono/CMakeLists.txt b/src/mono/CMakeLists.txt index ba41fa4d9a489..59d506591a316 100644 --- a/src/mono/CMakeLists.txt +++ b/src/mono/CMakeLists.txt @@ -249,7 +249,6 @@ elseif(CLR_CMAKE_HOST_OS STREQUAL "emscripten") set(DISABLE_SHARED_LIBS 1) # sys/random.h exists, but its not found set(HAVE_SYS_RANDOM_H 1) - set(INTERNAL_ZLIB 1) elseif(CLR_CMAKE_HOST_OS STREQUAL "wasi") set(HOST_WASI 1) add_definitions(-D_WASI_EMULATED_PROCESS_CLOCKS -D_WASI_EMULATED_SIGNAL -D_WASI_EMULATED_MMAN -DHOST_WASI) @@ -261,7 +260,6 @@ elseif(CLR_CMAKE_HOST_OS STREQUAL "wasi") add_compile_options(-Wno-unused-but-set-variable) set(ENABLE_PERFTRACING 0) set(DISABLE_SHARED_LIBS 1) - set(INTERNAL_ZLIB 1) set(DISABLE_EXECUTABLES 1) set(STATIC_COMPONENTS 1) elseif(CLR_CMAKE_HOST_OS STREQUAL "windows") @@ -270,7 +268,6 @@ elseif(CLR_CMAKE_HOST_OS STREQUAL "windows") set(HOST_NO_SYMLINKS 1) set(MONO_KEYWORD_THREAD "__declspec (thread)") set(MONO_ZERO_LEN_ARRAY 1) - set(INTERNAL_ZLIB 1) set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") # statically link VC runtime library add_compile_options($<$:/W4>) # set warning level 4 add_compile_options($<$:/WX>) # treat warnings as errors @@ -511,17 +508,9 @@ if ((HOST_MACCAT AND HOST_ARM64) OR (TARGET_MACCAT AND TARGET_ARM64)) set(TARGET_APPLE_MOBILE 1) endif() -# Decide if we need zlib, and if so whether we want the system zlib or the in-tree copy. +# Decide if we need zlib-ng. if(NOT DISABLE_EMBEDDED_PDB OR NOT DISABLE_LOG_PROFILER_GZ) - if(INTERNAL_ZLIB) - # defines ZLIB_SOURCES - include(${CLR_SRC_NATIVE_DIR}/external/zlib.cmake) - else() - # if we're not on a platform where we use the in-tree zlib, require system zlib - include(${CLR_SRC_NATIVE_DIR}/libs/System.IO.Compression.Native/extra_libs.cmake) - set(Z_LIBS) - append_extra_compression_libs(Z_LIBS) - endif() + include(${CLR_SRC_NATIVE_DIR}/external/zlib-ng.cmake) endif() ###################################### diff --git a/src/mono/cmake/config.h.in b/src/mono/cmake/config.h.in index 1fd699e761070..876ba60d4b8ce 100644 --- a/src/mono/cmake/config.h.in +++ b/src/mono/cmake/config.h.in @@ -183,9 +183,6 @@ /* Define to 1 if you have the header file. */ #cmakedefine HAVE_UNWIND_H 1 -/* Use in-tree zlib */ -#cmakedefine INTERNAL_ZLIB 1 - /* Define to 1 if you have the header file. */ #cmakedefine HAVE_POLL_H 1 diff --git a/src/mono/mono/metadata/CMakeLists.txt b/src/mono/mono/metadata/CMakeLists.txt index 773381d6c1c9a..2926655fdc1aa 100644 --- a/src/mono/mono/metadata/CMakeLists.txt +++ b/src/mono/mono/metadata/CMakeLists.txt @@ -5,7 +5,7 @@ project(metadata C) # if(DISABLE_ICALL_TABLES) add_library(mono-icall-table STATIC "icall-table.c") -target_link_libraries(mono-icall-table PRIVATE monoapi eglib_api) +target_link_libraries(mono-icall-table PRIVATE monoapi eglib_api zlibstatic) target_include_directories(mono-icall-table PRIVATE ${PROJECT_BINARY_DIR}/../.. ${PROJECT_SOURCE_DIR}/../.. ${PROJECT_SOURCE_DIR}/..) @@ -200,10 +200,10 @@ set(metadata_sources "${metadata_platform_sources};${metadata_common_sources};${ if(HOST_WIN32 AND NOT DISABLE_SHARED_LIBS) add_library(metadata_objects_shared OBJECT ${metadata_sources}) target_compile_definitions(metadata_objects_shared PRIVATE ${metadata_compile_definitions}) - target_link_libraries(metadata_objects_shared PRIVATE monoapi eglib_api utils_objects_shared) + target_link_libraries(metadata_objects_shared PRIVATE monoapi eglib_api utils_objects_shared zlibstatic) # note: metadata_objects is an object library, so this doesn't force linking with sgen, # it just adds the relevant include directories - which we need even with Boehm - target_link_libraries(metadata_objects_shared PRIVATE sgen_objects_shared) + target_link_libraries(metadata_objects_shared PRIVATE sgen_objects_shared zlibstatic) target_compile_definitions(metadata_objects_shared PRIVATE -DMONO_DLL_EXPORT) target_include_directories(metadata_objects_shared PRIVATE ${PROJECT_BINARY_DIR}/../.. ${PROJECT_SOURCE_DIR}/../.. @@ -212,10 +212,10 @@ endif() add_library(metadata_objects OBJECT ${metadata_sources}) target_compile_definitions(metadata_objects PRIVATE ${metadata_compile_definitions}) -target_link_libraries(metadata_objects PRIVATE monoapi eglib_api utils_objects) +target_link_libraries(metadata_objects PRIVATE monoapi eglib_api utils_objects zlibstatic) # note: metadata_objects is an object library, so this doesn't force linking with sgen, # it just adds the relevant include directories - which we need even with Boehm -target_link_libraries(metadata_objects PRIVATE sgen_objects) +target_link_libraries(metadata_objects PRIVATE sgen_objects zlibstatic) if(NOT HOST_WIN32) target_compile_definitions(metadata_objects PRIVATE -DMONO_DLL_EXPORT) endif() diff --git a/src/mono/mono/metadata/debug-mono-ppdb.c b/src/mono/mono/metadata/debug-mono-ppdb.c index bcbbb4c7f2be0..be4cb122e7838 100644 --- a/src/mono/mono/metadata/debug-mono-ppdb.c +++ b/src/mono/mono/metadata/debug-mono-ppdb.c @@ -30,12 +30,8 @@ #include #ifndef DISABLE_EMBEDDED_PDB -#ifdef INTERNAL_ZLIB -#include -#else #include #endif -#endif #include "debug-mono-ppdb.h" diff --git a/src/mono/mono/mini/CMakeLists.txt b/src/mono/mono/mini/CMakeLists.txt index b9bd9d426bc68..ba95682768609 100644 --- a/src/mono/mono/mini/CMakeLists.txt +++ b/src/mono/mono/mini/CMakeLists.txt @@ -272,7 +272,7 @@ endif() if(ENABLE_INTERP_LIB) add_library(mono-ee-interp STATIC "${interp_sources}") -target_link_libraries(mono-ee-interp PRIVATE monoapi eglib_api) +target_link_libraries(mono-ee-interp PRIVATE monoapi eglib_api zlibstatic) target_include_directories(mono-ee-interp PRIVATE ${PROJECT_BINARY_DIR}/../.. ${PROJECT_SOURCE_DIR}/../.. ${PROJECT_SOURCE_DIR}/..) @@ -322,19 +322,16 @@ elseif(NOT HOST_BROWSER AND NOT HOST_WASI) set(mini_sources "${mini_sources};${VERSION_FILE_PATH}") # this is generated by GenerateNativeVersionFile in Arcade endif() -set_source_files_properties(${ZLIB_SOURCES} PROPERTIES COMPILE_DEFINITIONS "${ZLIB_COMPILE_DEFINITIONS}") -set_source_files_properties(${ZLIB_SOURCES} PROPERTIES COMPILE_OPTIONS "${ZLIB_COMPILE_OPTIONS}") - -set(monosgen-sources "${mini_sources};${ZLIB_SOURCES}") +set(monosgen-sources "${mini_sources}") if(HOST_WIN32 AND NOT DISABLE_SHARED_LIBS) add_library(monosgen-objects_shared OBJECT "${monosgen-sources}") target_compile_definitions(monosgen-objects_shared PRIVATE -DMONO_DLL_EXPORT) - target_link_libraries (monosgen-objects_shared PRIVATE monoapi eglib_api utils_objects_shared sgen_objects_shared metadata_objects_shared) + target_link_libraries (monosgen-objects_shared PRIVATE monoapi eglib_api utils_objects_shared sgen_objects_shared metadata_objects_shared zlibstatic) endif() add_library(monosgen-objects OBJECT "${monosgen-sources}") -target_link_libraries (monosgen-objects PRIVATE monoapi eglib_api utils_objects sgen_objects metadata_objects) +target_link_libraries (monosgen-objects PRIVATE monoapi eglib_api utils_objects sgen_objects metadata_objects zlibstatic) if(NOT HOST_WIN32) target_compile_definitions(monosgen-objects PRIVATE -DMONO_DLL_EXPORT) endif() @@ -359,16 +356,16 @@ if(NOT DISABLE_SHARED_LIBS) target_compile_definitions(monosgen-shared PRIVATE -DMONO_DLL_EXPORT) # musl-libc implements ucontext in a different library on s390x if(CLR_CMAKE_TARGET_LINUX_MUSL AND TARGET_S390X) - target_link_libraries(monosgen-shared PRIVATE ucontext) + target_link_libraries(monosgen-shared PRIVATE ucontext zlibstatic) endif(CLR_CMAKE_TARGET_LINUX_MUSL AND TARGET_S390X) set_target_properties(monosgen-shared PROPERTIES OUTPUT_NAME ${MONO_SHARED_LIB_NAME}) if(MONO_SET_RPATH_ORIGIN) set_target_properties(monosgen-shared PROPERTIES INSTALL_RPATH "$ORIGIN") endif() if(HOST_WIN32) - target_link_libraries(monosgen-shared PRIVATE monoapi eglib_objects utils_objects_shared sgen_objects_shared metadata_objects_shared) + target_link_libraries(monosgen-shared PRIVATE monoapi eglib_objects utils_objects_shared sgen_objects_shared metadata_objects_shared zlibstatic) else() - target_link_libraries(monosgen-shared PRIVATE monoapi eglib_objects utils_objects sgen_objects metadata_objects) + target_link_libraries(monosgen-shared PRIVATE monoapi eglib_objects utils_objects sgen_objects metadata_objects zlibstatic) endif() target_include_directories (monosgen-shared PRIVATE monoapi) if(TARGET_WIN32) @@ -402,7 +399,7 @@ if(NOT DISABLE_SHARED_LIBS) install_with_stripped_symbols(monosgen-shared TARGETS lib) if(HOST_WIN32 AND TARGET_AMD64) add_library(monosgen-shared-dac SHARED "mini-windows-dlldac.c") - target_link_libraries(monosgen-shared-dac PRIVATE monoapi eglib_api) + target_link_libraries(monosgen-shared-dac PRIVATE monoapi eglib_api zlibstatic) set_target_properties(monosgen-shared-dac PROPERTIES OUTPUT_NAME ${MONO_SHARED_LIB_NAME}-dac) endif() @@ -424,7 +421,7 @@ if(NOT DISABLE_SHARED_LIBS) endif() add_library(${frameworkconfig} SHARED $) target_compile_definitions(${frameworkconfig} PRIVATE -DMONO_DLL_EXPORT) - target_link_libraries(${frameworkconfig} PRIVATE monoapi eglib_objects utils_objects sgen_objects metadata_objects) + target_link_libraries(${frameworkconfig} PRIVATE monoapi eglib_objects utils_objects sgen_objects metadata_objects zlibstatic) target_link_libraries(${frameworkconfig} PRIVATE ${OS_LIBS} ${LLVM_LIBS} ${Z_LIBS}) set_property(TARGET ${frameworkconfig} APPEND_STRING PROPERTY LINK_FLAGS " -Wl,-compatibility_version -Wl,2.0 -Wl,-current_version -Wl,2.0") @@ -541,14 +538,14 @@ if(NOT DISABLE_EXECUTABLES) set_target_properties(mono-sgen PROPERTIES INSTALL_RPATH "$ORIGIN") endif() endif() - target_link_libraries(mono-sgen PRIVATE monoapi eglib_api monosgen-static) + target_link_libraries(mono-sgen PRIVATE monoapi eglib_api monosgen-static zlibstatic) if (HOST_WASM) - target_link_libraries(mono-sgen PRIVATE mono-wasm-nosimd) + target_link_libraries(mono-sgen PRIVATE mono-wasm-nosimd zlibstatic) endif() target_link_libraries(mono-sgen PRIVATE ${OS_LIBS} ${LLVM_LIBS} ${Z_LIBS}) # musl-libc implements ucontext in a different library on s390x if(CLR_CMAKE_TARGET_LINUX_MUSL AND TARGET_S390X) - target_link_libraries(mono-sgen PRIVATE ucontext) + target_link_libraries(mono-sgen PRIVATE ucontex zlibstatic) endif(CLR_CMAKE_TARGET_LINUX_MUSL AND TARGET_S390X) if(NOT DISABLE_COMPONENTS AND STATIC_COMPONENTS AND NOT DISABLE_LINK_STATIC_COMPONENTS) # if components are built statically, link them into runtime. diff --git a/src/mono/mono/profiler/CMakeLists.txt b/src/mono/mono/profiler/CMakeLists.txt index 511c19dbe5a32..35a377c2f3056 100644 --- a/src/mono/mono/profiler/CMakeLists.txt +++ b/src/mono/mono/profiler/CMakeLists.txt @@ -12,11 +12,9 @@ include_directories( if(NOT DISABLE_LIBS) if(HOST_ANDROID OR HOST_IOS OR HOST_TVOS OR HOST_S390X) # Build the logging profiler only for certain platforms - set_source_files_properties(${ZLIB_SOURCES} PROPERTIES COMPILE_DEFINITIONS "${ZLIB_COMPILE_DEFINITIONS}") - set_source_files_properties(${ZLIB_SOURCES} PROPERTIES COMPILE_OPTIONS "${ZLIB_COMPILE_OPTIONS}") - add_library(mono-profiler-log SHARED helper.c log.c log-args.c ${ZLIB_SOURCES}) + add_library(mono-profiler-log SHARED helper.c log.c log-args.c) target_compile_definitions(mono-profiler-log PRIVATE -DMONO_DLL_EXPORT) - target_link_libraries(mono-profiler-log PRIVATE monosgen-shared monoapi eglib_objects) + target_link_libraries(mono-profiler-log PRIVATE monosgen-shared monoapi eglib_objects zlibstatic) if(HOST_ANDROID) target_link_libraries(mono-profiler-log PRIVATE log) endif() diff --git a/src/mono/mono/profiler/log.c b/src/mono/mono/profiler/log.c index 9d4304faae3ec..c02058402aeaf 100644 --- a/src/mono/mono/profiler/log.c +++ b/src/mono/mono/profiler/log.c @@ -72,12 +72,8 @@ #include #endif #ifndef DISABLE_LOG_PROFILER_GZ -#ifdef INTERNAL_ZLIB -#include -#else #include #endif -#endif #ifdef HOST_WIN32 #include diff --git a/src/mono/mono/profiler/mprof-report.c b/src/mono/mono/profiler/mprof-report.c index 35c88fe0a2fd9..c0db93ea0ca7c 100644 --- a/src/mono/mono/profiler/mprof-report.c +++ b/src/mono/mono/profiler/mprof-report.c @@ -23,12 +23,8 @@ #endif #include #ifndef DISABLE_LOG_PROFILER_GZ -#ifdef INTERNAL_ZLIB -#include -#else #include #endif -#endif #include #include #include diff --git a/src/native/external/zlib-ng.cmake b/src/native/external/zlib-ng.cmake new file mode 100644 index 0000000000000..6e916ef950a36 --- /dev/null +++ b/src/native/external/zlib-ng.cmake @@ -0,0 +1,22 @@ +include(FetchContent) + +FetchContent_Declare( + fetchzlibng + SOURCE_DIR "${CMAKE_CURRENT_LIST_DIR}/zlib-ng") + +set(ZLIB_COMPAT ON) +set(ZLIB_ENABLE_TESTS OFF) +set(ZLIBNG_ENABLE_TESTS OFF) +set(Z_PREFIX ON) + +add_compile_options(-Wno-error=unused-command-line-argument) # clang : error : argument unused during compilation: '-fno-semantic-interposition' + +add_compile_options($<$:/wd4127>) # warning C4127: conditional expression is constant +add_compile_options($<$:/wd4242>) # 'function': conversion from 'unsigned int' to 'Pos', possible loss of data, in various deflate_*.c files +add_compile_options($<$:/wd4244>) # 'function': conversion from 'unsigned int' to 'Pos', possible loss of data, in various deflate_*.c files +# TODO: DO we still need to disable /wd4131 ? +# add_compile_options($<$:/wd9025>) # Caused by zlib-ng/CMakeLists.txt:186 Command line error D9025: overriding '/W4' with '/W3' + +set(SKIP_INSTALL_ALL ON) +FetchContent_MakeAvailable(fetchzlibng) +set(SKIP_INSTALL_ALL OFF) \ No newline at end of file diff --git a/src/native/external/zlib-ng/CMakeLists.txt b/src/native/external/zlib-ng/CMakeLists.txt index 8bcfaf7ea23e4..61909e0e54946 100644 --- a/src/native/external/zlib-ng/CMakeLists.txt +++ b/src/native/external/zlib-ng/CMakeLists.txt @@ -183,8 +183,8 @@ elseif(MSVC) # (who'd use cmake from an IDE...) but checking for ICC before checking for MSVC should # avoid mistakes. # /Oi ? - set(WARNFLAGS /W3) - set(WARNFLAGS_MAINTAINER /W4) + # set(WARNFLAGS /W3) + #set(WARNFLAGS_MAINTAINER /W4) set(WARNFLAGS_DISABLE) if(BASEARCH_ARM_FOUND) add_definitions(-D_ARM_WINAPI_PARTITION_DESKTOP_SDK_AVAILABLE) @@ -462,29 +462,31 @@ endif() # # check for ptrdiff_t support # -check_c_source_compiles( - "#include - int main() { - ptrdiff_t *a; - (void)a; - return 0; - }" - HAVE_PTRDIFF_T -) -if(NOT HAVE_PTRDIFF_T) - set(NEED_PTRDIFF_T 1) - - check_type_size("void *" SIZEOF_DATA_PTR) - message(STATUS "sizeof(void *) is ${SIZEOF_DATA_PTR} bytes") - if(${SIZEOF_DATA_PTR} MATCHES "4") - set(PTRDIFF_TYPE "uint32_t") - elseif(${SIZEOF_DATA_PTR} MATCHES "8") - set(PTRDIFF_TYPE "uint64_t") - else() - message(FATAL_ERROR "sizeof(void *) is neither 32 nor 64 bit") - endif() -endif() +# TODO: SIZEOF_DATA_PTR is undefined when using MSVC because /INCREMENTAL:NO keeps getting overwritten to /INCREMENTAL by CMake. +# check_c_source_compiles( +# "#include +# int main() { +# ptrdiff_t *a; +# (void)a; +# return 0; +# }" +# HAVE_PTRDIFF_T +# ) +# if(NOT HAVE_PTRDIFF_T) +# set(NEED_PTRDIFF_T 1) + +# check_type_size("void *" SIZEOF_DATA_PTR) +# message(STATUS "sizeof(void *) is ${SIZEOF_DATA_PTR} bytes") + +# if(${SIZEOF_DATA_PTR} MATCHES "4") +# set(PTRDIFF_TYPE "uint32_t") +# elseif(${SIZEOF_DATA_PTR} MATCHES "8") +# set(PTRDIFF_TYPE "uint64_t") +# else() +# message(FATAL_ERROR "sizeof(void *) is neither 32 nor 64 bit") +# endif() +# endif() add_compile_options($<$:-DZLIB_DEBUG>) diff --git a/src/native/libs/Common/pal_utilities.h b/src/native/libs/Common/pal_utilities.h index 7b5fa63b6cac0..a1a57c8ed0e8c 100644 --- a/src/native/libs/Common/pal_utilities.h +++ b/src/native/libs/Common/pal_utilities.h @@ -12,7 +12,9 @@ #include #include #include -#include +#ifndef _WIN32 + #include +#endif #include #include @@ -69,6 +71,7 @@ inline static int ToFileDescriptorUnchecked(intptr_t fd) return (int)fd; } +#ifndef _WIN32 /** * Converts an intptr_t to a file descriptor. * intptr_t is the type used to marshal file descriptors so we can use SafeHandles effectively. @@ -86,6 +89,7 @@ static inline bool CheckInterrupted(ssize_t result) { return result < 0 && errno == EINTR; } +#endif inline static uint32_t Int32ToUint32(int32_t value) { diff --git a/src/native/libs/System.IO.Compression.Native/CMakeLists.txt b/src/native/libs/System.IO.Compression.Native/CMakeLists.txt index 089363cf5554e..d93dcc51168e5 100644 --- a/src/native/libs/System.IO.Compression.Native/CMakeLists.txt +++ b/src/native/libs/System.IO.Compression.Native/CMakeLists.txt @@ -1,11 +1,23 @@ project(System.IO.Compression.Native C) +include(FetchContent) include(${CMAKE_CURRENT_LIST_DIR}/extra_libs.cmake) +include(${CLR_SRC_NATIVE_DIR}/external/zlib-ng.cmake) + +configure_file( + ${CMAKE_CURRENT_SOURCE_DIR}/../Common/pal_config.h.in + ${CMAKE_CURRENT_BINARY_DIR}/pal_config.h) set(NATIVECOMPRESSION_SOURCES - pal_zlib.c + pal_zlib.c ) +if (HOST_WIN32 OR CLR_CMAKE_TARGET_WIN32) + list(APPEND NATIVECOMPRESSION_SOURCES "zlib_allocator_win.c") +else() + list(APPEND NATIVECOMPRESSION_SOURCES "zlib_allocator_unix.c") +endif() + if (NOT CLR_CMAKE_TARGET_BROWSER AND NOT CLR_CMAKE_TARGET_WASI) if (CLR_CMAKE_USE_SYSTEM_BROTLI) @@ -30,11 +42,8 @@ if (CLR_CMAKE_TARGET_UNIX OR CLR_CMAKE_TARGET_BROWSER OR CLR_CMAKE_TARGET_WASI) append_extra_compression_libs(NATIVE_LIBS_EXTRA) if (CLR_CMAKE_TARGET_BROWSER OR CLR_CMAKE_TARGET_WASI) - include(${CLR_SRC_NATIVE_DIR}/external/zlib.cmake) - add_definitions(-DINTERNAL_ZLIB) - set_source_files_properties(${ZLIB_SOURCES} PROPERTIES COMPILE_DEFINITIONS "${ZLIB_COMPILE_DEFINITIONS}") - set_source_files_properties(${ZLIB_SOURCES} PROPERTIES COMPILE_OPTIONS "${ZLIB_COMPILE_OPTIONS}") - set(NATIVECOMPRESSION_SOURCES ${ZLIB_SOURCES} ${NATIVECOMPRESSION_SOURCES}) + set(NATIVECOMPRESSION_SOURCES ${NATIVECOMPRESSION_SOURCES}) + list(APPEND NATIVE_LIBS_EXTRA zlibstatic) endif() # Disable implicit fallthrough warning for Zlib and Brotli @@ -64,6 +73,7 @@ if (CLR_CMAKE_TARGET_UNIX OR CLR_CMAKE_TARGET_BROWSER OR CLR_CMAKE_TARGET_WASI) target_link_libraries(System.IO.Compression.Native PRIVATE ${NATIVE_LIBS_EXTRA} + zlibstatic ) if (NOT CLR_CMAKE_TARGET_MACCATALYST AND NOT CLR_CMAKE_TARGET_IOS AND NOT CLR_CMAKE_TARGET_TVOS AND NOT CLR_CMAKE_TARGET_ANDROID AND NOT CLR_CMAKE_USE_SYSTEM_BROTLI) @@ -95,24 +105,17 @@ if (CLR_CMAKE_TARGET_UNIX OR CLR_CMAKE_TARGET_BROWSER OR CLR_CMAKE_TARGET_WASI) STATIC ${NATIVECOMPRESSION_SOURCES} ) - + target_link_libraries(System.IO.Compression.Native-Static + PRIVATE + zlibstatic + ) set_target_properties(System.IO.Compression.Native-Static PROPERTIES OUTPUT_NAME System.IO.Compression.Native CLEAN_DIRECT_OUTPUT 1) else () if (GEN_SHARED_LIB) include (GenerateExportHeader) endif () - if (CLR_CMAKE_HOST_ARCH_I386 OR CLR_CMAKE_HOST_ARCH_AMD64) - include(${CLR_SRC_NATIVE_DIR}/external/zlib-intel.cmake) - add_definitions(-DINTERNAL_ZLIB_INTEL) - else () - include(${CLR_SRC_NATIVE_DIR}/external/zlib.cmake) - endif () - - add_definitions(-DINTERNAL_ZLIB) - set_source_files_properties(${ZLIB_SOURCES} PROPERTIES COMPILE_DEFINITIONS "${ZLIB_COMPILE_DEFINITIONS}") - set_source_files_properties(${ZLIB_SOURCES} PROPERTIES COMPILE_OPTIONS "${ZLIB_COMPILE_OPTIONS}") - set(NATIVECOMPRESSION_SOURCES ${ZLIB_SOURCES} ${NATIVECOMPRESSION_SOURCES}) + set(NATIVECOMPRESSION_SOURCES ${NATIVECOMPRESSION_SOURCES}) if (GEN_SHARED_LIB) add_definitions(-DVER_FILEDESCRIPTION_STR="System.IO.Compression.Native") @@ -122,6 +125,10 @@ else () System.IO.Compression.Native.def ${VERSION_FILE_RC_PATH} ) + target_link_libraries(System.IO.Compression.Native + PRIVATE + zlibstatic + ) endif () if (NOT GEN_SHARED_LIB AND NOT CLR_CMAKE_TARGET_MACCATALYST AND NOT CLR_CMAKE_TARGET_IOS AND NOT CLR_CMAKE_TARGET_TVOS AND NOT CLR_CMAKE_TARGET_ANDROID AND NOT CLR_CMAKE_TARGET_BROWSER AND NOT CLR_CMAKE_TARGET_WASI) @@ -132,12 +139,20 @@ else () STATIC ${NATIVECOMPRESSION_SOURCES} ) + target_link_libraries(System.IO.Compression.Native-Static + PRIVATE + zlibstatic + ) if(STATIC_LIBS_ONLY) add_library(System.IO.Compression.Native.Aot STATIC ${NATIVECOMPRESSION_SOURCES} ) + target_link_libraries(System.IO.Compression.Native.Aot + PRIVATE + zlibstatic + ) set_target_properties(System.IO.Compression.Native.Aot PROPERTIES CLR_CONTROL_FLOW_GUARD OFF) set_target_properties(System.IO.Compression.Native.Aot PROPERTIES INTERPROCEDURAL_OPTIMIZATION OFF) @@ -145,6 +160,10 @@ else () STATIC ${NATIVECOMPRESSION_SOURCES} ) + target_link_libraries(System.IO.Compression.Native.Aot.GuardCF + PRIVATE + zlibstatic + ) set_target_properties(System.IO.Compression.Native.Aot.GuardCF PROPERTIES INTERPROCEDURAL_OPTIMIZATION OFF) endif() @@ -161,6 +180,7 @@ else () endif () if(STATIC_LIBS_ONLY) + install_static_library(zlibstatic aotsdk nativeaot) install_static_library(System.IO.Compression.Native.Aot aotsdk nativeaot) install_static_library(System.IO.Compression.Native.Aot.GuardCF aotsdk nativeaot) endif() diff --git a/src/native/libs/System.IO.Compression.Native/extra_libs.cmake b/src/native/libs/System.IO.Compression.Native/extra_libs.cmake index 78530ae98e8ff..cab0e5756a741 100644 --- a/src/native/libs/System.IO.Compression.Native/extra_libs.cmake +++ b/src/native/libs/System.IO.Compression.Native/extra_libs.cmake @@ -1,23 +1,8 @@ macro(append_extra_compression_libs NativeLibsExtra) - # TODO: remove the mono-style HOST_ variable checks once Mono is using eng/native/configureplatform.cmake to define the CLR_CMAKE_TARGET_ defines - if (CLR_CMAKE_TARGET_BROWSER OR HOST_BROWSER OR CLR_CMAKE_TARGET_WASI OR HOST_WASI) - # nothing special to link - elseif (CLR_CMAKE_TARGET_ANDROID OR HOST_ANDROID) - # need special case here since we want to link against libz.so but find_package() would resolve libz.a - set(ZLIB_LIBRARIES z) - elseif (CLR_CMAKE_TARGET_SUNOS OR HOST_SOLARIS) - set(ZLIB_LIBRARIES z m) - else () - find_package(ZLIB REQUIRED) - set(ZLIB_LIBRARIES ${ZLIB_LIBRARIES} m) - endif () - list(APPEND ${NativeLibsExtra} ${ZLIB_LIBRARIES}) - if (CLR_CMAKE_USE_SYSTEM_BROTLI) find_library(BROTLIDEC brotlidec REQUIRED) find_library(BROTLIENC brotlienc REQUIRED) - list(APPEND ${NativeLibsExtra} ${BROTLIDEC} ${BROTLIENC}) endif () endmacro() diff --git a/src/native/libs/System.IO.Compression.Native/pal_zlib.c b/src/native/libs/System.IO.Compression.Native/pal_zlib.c index bed37d1955f48..c814e927d83d0 100644 --- a/src/native/libs/System.IO.Compression.Native/pal_zlib.c +++ b/src/native/libs/System.IO.Compression.Native/pal_zlib.c @@ -5,19 +5,14 @@ #include #include "pal_zlib.h" -#ifdef INTERNAL_ZLIB - #ifdef _WIN32 - #define c_static_assert(e) static_assert((e),"") - #endif - #ifdef INTERNAL_ZLIB_INTEL - #include - #else - #include - #endif +#ifdef _WIN32 + #define c_static_assert(e) static_assert((e),"") + #include "../Common/pal_utilities.h" #else #include "pal_utilities.h" - #include #endif +#include +#include c_static_assert(PAL_Z_NOFLUSH == Z_NO_FLUSH); c_static_assert(PAL_Z_FINISH == Z_FINISH); @@ -44,6 +39,10 @@ Initializes the PAL_ZStream by creating and setting its underlying z_stream. static int32_t Init(PAL_ZStream* stream) { z_stream* zStream = (z_stream*)calloc(1, sizeof(z_stream)); + + zStream->zalloc = z_custom_calloc; + zStream->zfree = z_custom_cfree; + stream->internalState = zStream; if (zStream != NULL) diff --git a/src/native/libs/System.IO.Compression.Native/pal_zlib.h b/src/native/libs/System.IO.Compression.Native/pal_zlib.h index b317091b843f6..efb090b0631ae 100644 --- a/src/native/libs/System.IO.Compression.Native/pal_zlib.h +++ b/src/native/libs/System.IO.Compression.Native/pal_zlib.h @@ -5,12 +5,12 @@ #include #include #define FUNCTIONEXPORT - #define FUNCTIONCALLINGCONVENCTION WINAPI + #define FUNCTIONCALLINGCONVENTION WINAPI #else #include "pal_types.h" #include "pal_compiler.h" #define FUNCTIONEXPORT PALEXPORT - #define FUNCTIONCALLINGCONVENCTION + #define FUNCTIONCALLINGCONVENTION #endif /* @@ -84,7 +84,7 @@ Initializes the PAL_ZStream so the Deflate function can be invoked on it. Returns a PAL_ErrorCode indicating success or an error number on failure. */ -FUNCTIONEXPORT int32_t FUNCTIONCALLINGCONVENCTION CompressionNative_DeflateInit2_( +FUNCTIONEXPORT int32_t FUNCTIONCALLINGCONVENTION CompressionNative_DeflateInit2_( PAL_ZStream* stream, int32_t level, int32_t method, int32_t windowBits, int32_t memLevel, int32_t strategy); /* @@ -93,21 +93,21 @@ compressed bytes in nextOut. Returns a PAL_ErrorCode indicating success or an error number on failure. */ -FUNCTIONEXPORT int32_t FUNCTIONCALLINGCONVENCTION CompressionNative_Deflate(PAL_ZStream* stream, int32_t flush); +FUNCTIONEXPORT int32_t FUNCTIONCALLINGCONVENTION CompressionNative_Deflate(PAL_ZStream* stream, int32_t flush); /* All dynamically allocated data structures for this stream are freed. Returns a PAL_ErrorCode indicating success or an error number on failure. */ -FUNCTIONEXPORT int32_t FUNCTIONCALLINGCONVENCTION CompressionNative_DeflateEnd(PAL_ZStream* stream); +FUNCTIONEXPORT int32_t FUNCTIONCALLINGCONVENTION CompressionNative_DeflateEnd(PAL_ZStream* stream); /* Initializes the PAL_ZStream so the Inflate function can be invoked on it. Returns a PAL_ErrorCode indicating success or an error number on failure. */ -FUNCTIONEXPORT int32_t FUNCTIONCALLINGCONVENCTION CompressionNative_InflateInit2_(PAL_ZStream* stream, int32_t windowBits); +FUNCTIONEXPORT int32_t FUNCTIONCALLINGCONVENTION CompressionNative_InflateInit2_(PAL_ZStream* stream, int32_t windowBits); /* Inflates (uncompresses) the bytes in the PAL_ZStream's nextIn buffer and puts the @@ -115,14 +115,14 @@ uncompressed bytes in nextOut. Returns a PAL_ErrorCode indicating success or an error number on failure. */ -FUNCTIONEXPORT int32_t FUNCTIONCALLINGCONVENCTION CompressionNative_Inflate(PAL_ZStream* stream, int32_t flush); +FUNCTIONEXPORT int32_t FUNCTIONCALLINGCONVENTION CompressionNative_Inflate(PAL_ZStream* stream, int32_t flush); /* All dynamically allocated data structures for this stream are freed. Returns a PAL_ErrorCode indicating success or an error number on failure. */ -FUNCTIONEXPORT int32_t FUNCTIONCALLINGCONVENCTION CompressionNative_InflateEnd(PAL_ZStream* stream); +FUNCTIONEXPORT int32_t FUNCTIONCALLINGCONVENTION CompressionNative_InflateEnd(PAL_ZStream* stream); /* Update a running CRC-32 with the bytes buffer[0..len-1] and return the @@ -130,4 +130,4 @@ updated CRC-32. Returns the updated CRC-32. */ -FUNCTIONEXPORT uint32_t FUNCTIONCALLINGCONVENCTION CompressionNative_Crc32(uint32_t crc, uint8_t* buffer, int32_t len); +FUNCTIONEXPORT uint32_t FUNCTIONCALLINGCONVENTION CompressionNative_Crc32(uint32_t crc, uint8_t* buffer, int32_t len); diff --git a/src/native/libs/System.IO.Compression.Native/zlib_allocator.h b/src/native/libs/System.IO.Compression.Native/zlib_allocator.h new file mode 100644 index 0000000000000..f174f5f3472b5 --- /dev/null +++ b/src/native/libs/System.IO.Compression.Native/zlib_allocator.h @@ -0,0 +1,19 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +#ifdef _WIN32 + #include + #include + #define FUNCTIONEXPORT + #define FUNCTIONCALLINGCONVENTION WINAPI +#else + #include "pal_types.h" + #include "pal_compiler.h" + #define FUNCTIONEXPORT PALEXPORT + #define FUNCTIONCALLINGCONVENTION +#endif +#include // voidpf + +FUNCTIONEXPORT voidpf FUNCTIONCALLINGCONVENTION z_custom_calloc(voidpf opaque, unsigned items, unsigned size); + +FUNCTIONEXPORT void FUNCTIONCALLINGCONVENTION z_custom_cfree(voidpf opaque, voidpf ptr); \ No newline at end of file diff --git a/src/native/libs/System.IO.Compression.Native/zlib_allocator_unix.c b/src/native/libs/System.IO.Compression.Native/zlib_allocator_unix.c index 9eb4bbf267105..84572466d3995 100644 --- a/src/native/libs/System.IO.Compression.Native/zlib_allocator_unix.c +++ b/src/native/libs/System.IO.Compression.Native/zlib_allocator_unix.c @@ -1,9 +1,12 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +#define ZLIB_COMPAT 1 + #include #include -#include +#include +#include /* A custom allocator for zlib that provides some defense-in-depth over standard malloc / free. * (non-Windows version) @@ -70,7 +73,7 @@ static void WriteAllocCookieUnaligned(void* pDest, DOTNET_ALLOC_COOKIE vCookie) const size_t DOTNET_ALLOC_HEADER_COOKIE_SIZE_WITH_PADDING = (sizeof(DOTNET_ALLOC_COOKIE) + MEMORY_ALLOCATION_ALIGNMENT - 1) & ~((size_t)MEMORY_ALLOCATION_ALIGNMENT - 1); const size_t DOTNET_ALLOC_TRAILER_COOKIE_SIZE = sizeof(DOTNET_ALLOC_COOKIE); -voidpf ZLIB_INTERNAL zcalloc(opaque, items, size) +voidpf z_custom_calloc(opaque, items, size) voidpf opaque; unsigned items; unsigned size; @@ -79,7 +82,7 @@ voidpf ZLIB_INTERNAL zcalloc(opaque, items, size) // If initializing a fixed-size structure, zero the memory. bool fZeroMemory = (items == 1); - + size_t cbRequested; if (sizeof(items) + sizeof(size) <= sizeof(cbRequested)) { @@ -119,7 +122,7 @@ static void zcfree_trash_cookie(void* pCookie) memset(pCookie, 0, sizeof(DOTNET_ALLOC_COOKIE)); } -void ZLIB_INTERNAL zcfree(opaque, ptr) +void z_custom_cfree(opaque, ptr) voidpf opaque; voidpf ptr; { diff --git a/src/native/libs/System.IO.Compression.Native/zlib_allocator_win.c b/src/native/libs/System.IO.Compression.Native/zlib_allocator_win.c index 9bdf694495e68..1e766e7b2bdc0 100644 --- a/src/native/libs/System.IO.Compression.Native/zlib_allocator_win.c +++ b/src/native/libs/System.IO.Compression.Native/zlib_allocator_win.c @@ -1,17 +1,16 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +#define ZLIB_COMPAT 1 + #include #include #include #include #include /* _ASSERTE */ -#ifdef INTERNAL_ZLIB_INTEL -#include -#else -#include -#endif +#include +#include /* A custom allocator for zlib that provides some defense-in-depth over standard malloc / free. * (Windows-specific version) @@ -92,7 +91,7 @@ typedef struct _DOTNET_ALLOC_COOKIE const SIZE_T DOTNET_ALLOC_HEADER_COOKIE_SIZE_WITH_PADDING = (sizeof(DOTNET_ALLOC_COOKIE) + MEMORY_ALLOCATION_ALIGNMENT - 1) & ~((SIZE_T)MEMORY_ALLOCATION_ALIGNMENT - 1); const SIZE_T DOTNET_ALLOC_TRAILER_COOKIE_SIZE = sizeof(DOTNET_ALLOC_COOKIE); -voidpf ZLIB_INTERNAL zcalloc(opaque, items, size) +voidpf z_custom_calloc(opaque, items, size) voidpf opaque; unsigned items; unsigned size; @@ -150,7 +149,7 @@ void zcfree_cookie_check_failed() __fastfail(FAST_FAIL_HEAP_METADATA_CORRUPTION); } -void ZLIB_INTERNAL zcfree(opaque, ptr) +void z_custom_cfree(opaque, ptr) voidpf opaque; voidpf ptr; {