Skip to content

Commit

Permalink
CMake update. (#25)
Browse files Browse the repository at this point in the history
* Fix architecture macro on MSVC.

Signed-off-by: Ningfei Li <ningfei.li@gmail.com>

* Update '.gitignore'.

Also remove 'zconf.h', it will be generated during building.

* Add 'build' subdir to .gitignore

* Fix architecture macro on MSVC.

* Update CMakeLists.txt

Clean up.
Add option to set runtime (useful for MSVC).
Add 'SSE4.2' and 'AVX' option.

Signed-off-by: Ningfei Li <ningfei.li@gmail.com>

* Define '_LARGEFILE64_SOURCE' by default in CMakeLists.txt.

Also remove checking fseeko.

Signed-off-by: Ningfei Li <ningfei.li@gmail.com>

* Set 'CMAKE_MACOSX_RPATH' to TRUE.

Signed-off-by: Ningfei Li <ningfei.li@gmail.com>

* Update SSE4.2 and PCLMUL setting in CMakeLists.txt

Signed-off-by: Ningfei Li <ningfei.li@gmail.com>

* Add 'HAVE_HIDDEN' to CMakeLists.txt

Signed-off-by: Ningfei Li <ningfei.li@gmail.com>

* Fix building dll on MSVC.

Signed-off-by: Ningfei Li <ningfei.li@gmail.com>

* Fix zlib.pc file generation.

Signed-off-by: Ningfei Li <ningfei.li@gmail.com>

* Refine cmake settings.

* Change cmake function to lowercase.

* Fix zlib.pc

* Fix crc32_pclmul_le_16 type.

* Set POSITION_INDEPENDENT_CODE flag to ON by default.

* Replace GPL CRC with BSD CRC (zlib-ng/zlib-ng#42), for validation see https://github.com/neurolabusc/simd_crc

* Westmere detection

* Update configure for Westmere (InsightSoftwareConsortium/ITK#416)

* use cpu_has_pclmul() to autodetect CPU hardware (InsightSoftwareConsortium/ITK#416)

* remove gpl code

* Improve support for compiling using Windows (https://github.com/ningfei/zlib)

* Import ucm.cmake from https://github.com/ningfei/zlib

* crc32_simd as separate file (#18)

* atomic and SKIP_CPUID_CHECK (#18)

* Suppress some MSVC warnings.

* Remove unused code

* Fix ucm_set_runtime when only C is enabled for the project.

* Removed configured header file.

* Unify zconf.h template.

* Only allow compiler to use clmul instructions to crc_simd unit (intel/zlib#25)

* Atomic does not compile on Ubuntu 14.04

* Allow "cmake -DSKIP_CPUID_CHECK=ON .." to not check SIMD CRC support on execution.

* Restore Windows compilation using "nmake -f win32\Makefile.msc"

* Do not set SOVERSION on Cygwin.

* Fix file permission.

* Refine PCLMUL CMake option.

* zconf.h required for Windows nmake

* Do not set visibility flag on Cygwin.

* Fix compiling dll resource.

* Fix compiling using MinGW.

SSE4.2 and PCLMUL are also supported.

* Fix zconf.h for Windows.

* support crc intrinsics for ARM CPUs

* Add gzip -k option to minigzip (to aid benchmarks)

* Support Intel and ARMv8 optimization in CMake.

* Clean up.

* Fix compiling on Apple M1.

check_c_compiler_flag detects SSE2, SSE3, SSE42 and PCLMUL but compiling
fails. Workaround fix, need further investigation.

* Restore MSVC warnings.

Co-authored-by: neurolabusc <rorden@sc.edu>
Co-authored-by: Chris Rorden <rorden@mailbox.sc.edu>
  • Loading branch information
3 people authored Dec 2, 2020
1 parent 6931277 commit 4a94dad
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 567 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
/minigzip64
/minigzipsh
/zlib.pc
/zconf.h.included

.DS_Store

Expand Down
101 changes: 64 additions & 37 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ option(BUILD_EXAMPLES "Build examples" OFF)
option(SKIP_CPUID_CHECK "Assume CPU supports fast CRC" OFF)

if(SKIP_CPUID_CHECK)
add_definitions(-DSKIP_CPUID_CHECK)
add_definitions(-DSKIP_CPUID_CHECK)
endif()

# Set -fPIC option
Expand Down Expand Up @@ -66,11 +66,34 @@ endif()

# Compiler dependent flags
include (CheckCCompilerFlag)
if(UNIX)
check_c_compiler_flag(-msse4.2 HAS_SSE42)
if(HAS_SSE42)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -msse4.2")
add_definitions(-DHAS_SSE42)
if(UNIX OR MINGW)
check_c_compiler_flag(-march=armv8-a+crc ARM_CRC)
if(ARM_CRC)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=armv8-a+crc")
else()
check_c_compiler_flag(-msse2 HAS_SSE2)
if(HAS_SSE2)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -msse2")
add_definitions(-DHAS_SSE2)
endif()

check_c_compiler_flag(-mssse3 HAS_SSSE3)
if(HAS_SSSE3)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mssse3")
add_definitions(-DHAS_SSSE3)
endif()

check_c_compiler_flag(-msse4.2 HAS_SSE42)
if(HAS_SSE42)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -msse4.2")
add_definitions(-DHAS_SSE42)
endif()

check_c_compiler_flag(-mpclmul HAS_PCLMUL)
if(HAS_PCLMUL)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mpclmul")
add_definitions(-DHAS_PCLMUL)
endif()
endif()
elseif(MSVC)
set(CMAKE_DEBUG_POSTFIX "d")
Expand Down Expand Up @@ -120,45 +143,49 @@ set(ZLIB_SRCS
zutil.c
)

# append "crc_simd.c" and compile with "mpclmul" if supported by compiler
if(UNIX)
check_c_compiler_flag(-mpclmul HAS_PCLMUL)
if(HAS_PCLMUL)
set(ENABLE_ASSEMBLY "PCLMUL" CACHE STRING "Choose assembly implementation.")
set_property(CACHE ENABLE_ASSEMBLY PROPERTY STRINGS "OFF;PCLMUL")

if("${ENABLE_ASSEMBLY}" STREQUAL "PCLMUL")
#set(ZLIB_ASMS contrib/amd64/crc32-pclmul_asm.S)
#set_source_files_properties(${ZLIB_ASMS} PROPERTIES LANGUAGE C COMPILE_FLAGS -DNO_UNDERLINE)
#set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mpclmul")
set_source_files_properties(crc32_simd.c PROPERTIES COMPILE_FLAGS -mpclmul)
list(APPEND ZLIB_SRCS crc32_simd.c)
add_definitions(-DHAS_PCLMUL)
endif()
if(UNIX OR MINGW)
# append "inffast_chunk.c" and "adler32_simd.c" for ARMv8 CPU
if(ARM_CRC)
list(APPEND ZLIB_SRCS inffast_chunk.c adler32_simd.c)
add_definitions(-DINFLATE_CHUNK_SIMD_NEON)
add_definitions(-DINFLATE_CHUNK_READ_64LE)
add_definitions(-DADLER32_SIMD_NEON)
endif()

# append "inffast_chunk.c" and compile with "sse2" if supported by compiler
if(HAS_SSE2)
list(APPEND ZLIB_SRCS inffast_chunk.c)
add_definitions(-DINFLATE_CHUNK_SIMD_SSE2)
add_definitions(-DINFLATE_CHUNK_READ_64LE)
endif()
endif()

# support crc intrinsics for ARM CPUs
if(UNIX)
check_c_compiler_flag(-march=armv8-a+crc HAS_CRC)
if(HAS_CRC)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=armv8-a+crc")
# append "adler32_simd.c" and compile with "ssse3" if supported by compiler
if(HAS_SSSE3)
list(APPEND ZLIB_SRCS adler32_simd.c)
add_definitions(-DADLER32_SIMD_SSSE3)
endif()

# append "crc_simd.c" and compile with "pclmul" if supported by compiler
if(HAS_PCLMUL)
list(APPEND ZLIB_SRCS crc32_simd.c)
endif()
endif()

if(BUILD_SHARED_LIBS)
# Visibility
check_c_compiler_flag(-fvisibility=hidden HAVE_HIDDEN)
if(HAVE_HIDDEN)
add_definitions(-DHAVE_HIDDEN)
if(UNIX AND NOT CYGWIN)
check_c_compiler_flag(-fvisibility=hidden HAVE_HIDDEN)
if(HAVE_HIDDEN)
add_definitions(-DHAVE_HIDDEN)
endif()
endif()

# DLL resource setting
if(NOT MINGW)
if(MSVC)
set(ZLIB_DLL_SRCS
win32/zlib1.rc # If present will override custom build rule below.
)
else()
elseif(MINGW OR CYGWIN)
# This gets us DLL resource information when compiling on MinGW.
if(NOT CMAKE_RC_COMPILER)
set(CMAKE_RC_COMPILER windres.exe)
Expand All @@ -174,9 +201,8 @@ if(BUILD_SHARED_LIBS)
set(ZLIB_DLL_SRCS ${CMAKE_CURRENT_BINARY_DIR}/zlib1rc.obj)
endif()

add_library(zlib SHARED ${ZLIB_SRCS} ${ZLIB_ASMS} ${ZLIB_DLL_SRCS} ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HDRS})
add_library(zlib SHARED ${ZLIB_SRCS} ${ZLIB_DLL_SRCS} ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HDRS})
set_target_properties(zlib PROPERTIES DEFINE_SYMBOL ZLIB_DLL)
set_target_properties(zlib PROPERTIES SOVERSION 1)

if(NOT CYGWIN)
# This property causes shared libraries on Linux to have the full version
Expand All @@ -186,10 +212,11 @@ if(BUILD_SHARED_LIBS)
#
# This has no effect with MSVC, on that platform the version info for
# the DLL comes from the resource file win32/zlib1.rc
set_target_properties(zlib PROPERTIES SOVERSION 1)
set_target_properties(zlib PROPERTIES VERSION ${ZLIB_VERSION})
endif()

if(UNIX)
if(UNIX OR MINGW)
# On unix-like platforms the library is almost always called libz
set_target_properties(zlib PROPERTIES OUTPUT_NAME z)
if(NOT APPLE)
Expand All @@ -200,8 +227,8 @@ if(BUILD_SHARED_LIBS)
set_target_properties(zlib PROPERTIES SUFFIX "1.dll")
endif()
else()
add_library(zlib STATIC ${ZLIB_SRCS} ${ZLIB_ASMS} ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HDRS})
if(UNIX)
add_library(zlib STATIC ${ZLIB_SRCS} ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HDRS})
if(UNIX OR MINGW)
set_target_properties(zlib PROPERTIES OUTPUT_NAME z)
endif()
#============================================================================
Expand Down
4 changes: 2 additions & 2 deletions crc32.c
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ local unsigned long crc32_generic(crc, buf, len)
#define PCLMUL_ALIGN_MASK 15

#if defined(__GNUC__)
#if __GNUC__ < 5
#if __GNUC__ < 5
int cpu_has_pclmul = -1; //e.g. gcc 4.8.4 https://stackoverflow.com/questions/20326604/stdatomic-h-in-gcc-4-8
#else
_Atomic int cpu_has_pclmul = -1; //global: will be 0 or 1 after first test
Expand Down Expand Up @@ -553,4 +553,4 @@ uLong ZEXPORT crc32_combine64(crc1, crc2, len2)
return crc32_combine_(crc1, crc2, len2);
}

#endif
#endif
Empty file modified crc32_simd.c
100755 → 100644
Empty file.
Empty file modified crc32_simd.h
100755 → 100644
Empty file.
Empty file modified deflate.c
100755 → 100644
Empty file.
2 changes: 1 addition & 1 deletion test/minigzip.c
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ int main(argc, argv)
else if (strcmp(*argv, "-h") == 0)
outmode[3] = 'h';
else if (strcmp(*argv, "-k") == 0)
keep = 1;
keep = 1;
else if (strcmp(*argv, "-r") == 0)
outmode[3] = 'R';
else if ((*argv)[0] == '-' && (*argv)[1] >= '1' && (*argv)[1] <= '9' &&
Expand Down
24 changes: 12 additions & 12 deletions ucm.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ macro(ucm_set_runtime)
# - for example if with MSVC and the user has removed the flags - here we just switch/replace them
if("${ARG_STATIC}")
foreach(flags ${flags_configs})
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR CMAKE_C_COMPILER_ID MATCHES "GNU")
if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 4.4.7) # option "-static-libstdc++" available since GCC 4.5
if(NOT ${flags} MATCHES "-static-libstdc\\+\\+")
set(${flags} "${${flags}} -static-libstdc++")
Expand All @@ -226,7 +226,7 @@ macro(ucm_set_runtime)
endforeach()
elseif("${ARG_DYNAMIC}")
foreach(flags ${flags_configs})
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR CMAKE_C_COMPILER_ID MATCHES "GNU")
if(${flags} MATCHES "-static-libstdc\\+\\+")
string(REGEX REPLACE "-static-libstdc\\+\\+" "" ${flags} "${${flags}}")
endif()
Expand Down Expand Up @@ -556,13 +556,13 @@ macro(ucm_add_target)
set(do_unity FALSE)
endif()

# inform the developer that the current target might benefit from a unity build
if(NOT ARG_UNITY AND ${UCM_UNITY_BUILD})
ucm_count_sources(${ARG_SOURCES} RESULT num_sources)
if(${num_sources} GREATER 1)
message(AUTHOR_WARNING "Target '${ARG_NAME}' may benefit from a unity build.\nIt has ${num_sources} sources - enable with UNITY flag")
endif()
endif()
# inform the developer that the current target might benefit from a unity build
if(NOT ARG_UNITY AND ${UCM_UNITY_BUILD})
ucm_count_sources(${ARG_SOURCES} RESULT num_sources)
if(${num_sources} GREATER 1)
message(AUTHOR_WARNING "Target '${ARG_NAME}' may benefit from a unity build.\nIt has ${num_sources} sources - enable with UNITY flag")
endif()
endif()

# prepare for the unity build
set(orig_target ${ARG_NAME})
Expand All @@ -587,9 +587,9 @@ macro(ucm_add_target)
# set the number of unity cpp files to be used for the unity target
if(NOT "${ARG_CPP_PER_UNITY}" STREQUAL "")
set_property(TARGET ${orig_target} PROPERTY COTIRE_UNITY_SOURCE_MAXIMUM_NUMBER_OF_INCLUDES "${ARG_CPP_PER_UNITY}")
else()
set_property(TARGET ${orig_target} PROPERTY COTIRE_UNITY_SOURCE_MAXIMUM_NUMBER_OF_INCLUDES "100")
endif()
else()
set_property(TARGET ${orig_target} PROPERTY COTIRE_UNITY_SOURCE_MAXIMUM_NUMBER_OF_INCLUDES "100")
endif()

if(NOT "${ARG_PCH_FILE}" STREQUAL "")
set_target_properties(${orig_target} PROPERTIES COTIRE_CXX_PREFIX_HEADER_INIT "${ARG_PCH_FILE}")
Expand Down
4 changes: 2 additions & 2 deletions zconf.h
Original file line number Diff line number Diff line change
Expand Up @@ -449,10 +449,10 @@ typedef uLong FAR uLongf;
#endif
#ifndef Z_SOLO
# if defined(Z_HAVE_UNISTD_H) || defined(_LARGEFILE64_SOURCE)
# ifdef _MSC_VER
# ifdef _WIN32 /* _MSC_VER doesn't work for some reason when building dll*/
# include <io.h>
# else
# include <unistd.h> /* for SEEK_*, off_t, and _LFS64_LARGEFILE */
# include <unistd.h> /* for SEEK_*, off_t, and _LFS64_LARGEFILE */
# endif
# ifdef VMS
# include <unixio.h> /* for off_t */
Expand Down
4 changes: 2 additions & 2 deletions zconf.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -449,10 +449,10 @@ typedef uLong FAR uLongf;
#endif
#ifndef Z_SOLO
# if defined(Z_HAVE_UNISTD_H) || defined(_LARGEFILE64_SOURCE)
# ifdef _MSC_VER
# ifdef _WIN32 /* _MSC_VER doesn't work for some reason when building dll*/
# include <io.h>
# else
# include <unistd.h> /* for SEEK_*, off_t, and _LFS64_LARGEFILE */
# include <unistd.h> /* for SEEK_*, off_t, and _LFS64_LARGEFILE */
# endif
# ifdef VMS
# include <unixio.h> /* for off_t */
Expand Down
Loading

0 comments on commit 4a94dad

Please sign in to comment.