Skip to content

Commit

Permalink
cmake: adjust to produce same results as configure/make; fixes madler…
Browse files Browse the repository at this point in the history
…#497

- default build type to Release
- set -DHIDDEN properly
- pass .c files to compiler in same order
- on linux/bsd, link libz.so with -lc explicitly (as Makefile.in does, but why?)
- output same libz.pc and zconf.h as Make
  • Loading branch information
dankegel committed May 26, 2020
1 parent 0af1b78 commit 182a4ee
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 24 deletions.
87 changes: 72 additions & 15 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ set(CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS ON)

project(zlib C)

# parse the full version number from zlib.h and include in ZLIB_FULL_VERSION
file(READ ${CMAKE_CURRENT_SOURCE_DIR}/zlib.h _zlib_h_contents)
string(REGEX REPLACE ".*#define[ \t]+ZLIB_VERSION[ \t]+\"([-0-9A-Za-z.]+)\".*"
"\\1" ZLIB_FULL_VERSION ${_zlib_h_contents})
# FIXME: parse VERSION from ZLIB_FULL_VERSION
set(VERSION "1.2.11.1")

option(ASM686 "Enable building i686 assembly implementation")
Expand All @@ -12,7 +17,16 @@ set(INSTALL_BIN_DIR "${CMAKE_INSTALL_PREFIX}/bin" CACHE PATH "Installation direc
set(INSTALL_LIB_DIR "${CMAKE_INSTALL_PREFIX}/lib" CACHE PATH "Installation directory for libraries")
set(INSTALL_INC_DIR "${CMAKE_INSTALL_PREFIX}/include" CACHE PATH "Installation directory for headers")
set(INSTALL_MAN_DIR "${CMAKE_INSTALL_PREFIX}/share/man" CACHE PATH "Installation directory for manual pages")
set(INSTALL_PKGCONFIG_DIR "${CMAKE_INSTALL_PREFIX}/share/pkgconfig" CACHE PATH "Installation directory for pkgconfig (.pc) files")
set(INSTALL_PKGCONFIG_DIR "${INSTALL_LIB_DIR}/pkgconfig" CACHE PATH "Installation directory for pkgconfig (.pc) files")

# Set a default build type if none was specified
set(default_build_type "Release")
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
message(STATUS "Setting build type to '${default_build_type}' as none was specified.")
set(CMAKE_BUILD_TYPE "${default_build_type}" CACHE STRING "Choose the type of build." FORCE)
# Set the possible values of build type for cmake-gui
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
endif()

include(CheckTypeSize)
include(CheckFunctionExists)
Expand All @@ -21,8 +35,35 @@ include(CheckCSourceCompiles)
enable_testing()

check_include_file(sys/types.h HAVE_SYS_TYPES_H)
check_include_file(stdarg.h HAVE_STDARG_H)
check_include_file(stdint.h HAVE_STDINT_H)
check_include_file(stddef.h HAVE_STDDEF_H)
check_include_file(unistd.h HAVE_UNISTD_H)

#
# Match configure's zconf.h exactly
#
if (HAVE_STDARG_H)
SET(ZCONF_STDARG_LINE "#if 1 /* was set to #if 1 by ./configure */")
else()
SET(ZCONF_STDARG_LINE "#ifdef HAVE_STDARG_H /* may be set to #if 1 by ./configure */")
endif()
if (HAVE_UNISTD_H)
SET(ZCONF_UNISTD_LINE "#if 1 /* was set to #if 1 by ./configure */")
else()
SET(ZCONF_UNISTD_LINE "#ifdef HAVE_UNISTD_H /* may be set to #if 1 by ./configure */")
endif()

#
# Check to see if compiler supports __attribute__((visibility ("hidden")))
#
check_c_source_compiles(
"int __attribute__((visibility (\"hidden\"))) foo; int main() { return foo; }"
HAVE_HIDDEN
)
if(HAVE_HIDDEN)
add_definitions(-DHAVE_HIDDEN)
endif()

#
# Check to see if we have large file support
Expand Down Expand Up @@ -57,7 +98,7 @@ endif()
#
# Check for unistd.h
#
check_include_file(unistd.h Z_HAVE_UNISTD_H)
check_include_file(unistd.h HAVE_UNISTD_H)

if(MSVC)
set(CMAKE_DEBUG_POSTFIX "d")
Expand All @@ -78,6 +119,19 @@ if(NOT CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_CURRENT_BINARY_DIR)
endif()
endif()

# Refer to prefix symbolically to ease relocation by end user,
# and match Makefile-generate .pc file exactly.
if(INSTALL_INC_DIR STREQUAL "${CMAKE_INSTALL_PREFIX}/include")
set(PC_INSTALL_INC_DIR "\${prefix}/include")
else()
set(PC_INSTALL_INC_DIR "${INSTALL_INC_DIR}")
endif()
if(INSTALL_LIB_DIR STREQUAL "${CMAKE_INSTALL_PREFIX}/lib")
set(PC_INSTALL_LIB_DIR "\${exec_prefix}/lib")
else()
set(PC_INSTALL_LIB_DIR "${INSTALL_LIB_DIR}")
endif()

set(ZLIB_PC ${CMAKE_CURRENT_BINARY_DIR}/zlib.pc)
configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/zlib.pc.cmakein
${ZLIB_PC} @ONLY)
Expand Down Expand Up @@ -105,22 +159,27 @@ set(ZLIB_PRIVATE_HDRS
trees.h
zutil.h
)

# Note: keep these in the same order as in Makefile.in
# This allows libz.so to be identical between the two build systems.
set(ZLIB_SRCS
# Makefile.in: PIC_OBJZ
adler32.c
compress.c
crc32.c
deflate.c
gzclose.c
gzlib.c
gzread.c
gzwrite.c
inflate.c
infback.c
inftrees.c
inffast.c
inflate.c
inftrees.c
trees.c
uncompr.c
zutil.c
# Makefile.in: PIC_OBJG
compress.c
uncompr.c
gzclose.c
gzlib.c
gzread.c
gzwrite.c
)

if(NOT MINGW)
Expand Down Expand Up @@ -162,11 +221,6 @@ if(MSVC)
endif()
endif()

# parse the full version number from zlib.h and include in ZLIB_FULL_VERSION
file(READ ${CMAKE_CURRENT_SOURCE_DIR}/zlib.h _zlib_h_contents)
string(REGEX REPLACE ".*#define[ \t]+ZLIB_VERSION[ \t]+\"([-0-9A-Za-z.]+)\".*"
"\\1" ZLIB_FULL_VERSION ${_zlib_h_contents})

if(MINGW)
# This gets us DLL resource information when compiling on MinGW.
if(NOT CMAKE_RC_COMPILER)
Expand Down Expand Up @@ -204,6 +258,9 @@ if(UNIX)
set_target_properties(zlib zlibstatic PROPERTIES OUTPUT_NAME z)
if(NOT APPLE)
set_target_properties(zlib PROPERTIES LINK_FLAGS "-Wl,--version-script,\"${CMAKE_CURRENT_SOURCE_DIR}/zlib.map\"")
# Makefile.in uses -lc...
set(LDSHAREDLIBC -lc)
target_link_libraries(zlib ${LDSHAREDLIBC})
endif()
elseif(BUILD_SHARED_LIBS AND WIN32)
# Creates zlib1.dll when building shared library version
Expand Down
6 changes: 2 additions & 4 deletions zconf.h.cmakein
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

#ifndef ZCONF_H
#define ZCONF_H
#cmakedefine Z_PREFIX
#cmakedefine Z_HAVE_UNISTD_H

/*
* If you *really* need a unique prefix for all types and library functions,
Expand Down Expand Up @@ -433,11 +431,11 @@ typedef uLong FAR uLongf;
typedef unsigned long z_crc_t;
#endif

#ifdef HAVE_UNISTD_H /* may be set to #if 1 by ./configure */
@ZCONF_UNISTD_LINE@
# define Z_HAVE_UNISTD_H
#endif

#ifdef HAVE_STDARG_H /* may be set to #if 1 by ./configure */
@ZCONF_STDARG_LINE@
# define Z_HAVE_STDARG_H
#endif

Expand Down
10 changes: 5 additions & 5 deletions zlib.pc.cmakein
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
prefix=@CMAKE_INSTALL_PREFIX@
exec_prefix=@CMAKE_INSTALL_PREFIX@
libdir=@INSTALL_LIB_DIR@
sharedlibdir=@INSTALL_LIB_DIR@
includedir=@INSTALL_INC_DIR@
exec_prefix=${prefix}
libdir=@PC_INSTALL_LIB_DIR@
sharedlibdir=${libdir}
includedir=@PC_INSTALL_INC_DIR@

Name: zlib
Description: zlib compression library
Version: @VERSION@
Version: @ZLIB_FULL_VERSION@

Requires:
Libs: -L${libdir} -L${sharedlibdir} -lz
Expand Down

0 comments on commit 182a4ee

Please sign in to comment.