kusano 7d535a
cmake_minimum_required(VERSION 2.4.4)
kusano 7d535a
set(CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS ON)
kusano 7d535a
kusano 7d535a
project(zlib C)
kusano 7d535a
kusano 7d535a
set(VERSION "1.2.7")
kusano 7d535a
kusano 7d535a
set(INSTALL_BIN_DIR "${CMAKE_INSTALL_PREFIX}/bin" CACHE PATH "Installation directory for executables")
kusano 7d535a
set(INSTALL_LIB_DIR "${CMAKE_INSTALL_PREFIX}/lib" CACHE PATH "Installation directory for libraries")
kusano 7d535a
set(INSTALL_INC_DIR "${CMAKE_INSTALL_PREFIX}/include" CACHE PATH "Installation directory for headers")
kusano 7d535a
set(INSTALL_MAN_DIR "${CMAKE_INSTALL_PREFIX}/share/man" CACHE PATH "Installation directory for manual pages")
kusano 7d535a
set(INSTALL_PKGCONFIG_DIR "${CMAKE_INSTALL_PREFIX}/share/pkgconfig" CACHE PATH "Installation directory for pkgconfig (.pc) files")
kusano 7d535a
kusano 7d535a
include(CheckTypeSize)
kusano 7d535a
include(CheckFunctionExists)
kusano 7d535a
include(CheckIncludeFile)
kusano 7d535a
include(CheckCSourceCompiles)
kusano 7d535a
enable_testing()
kusano 7d535a
kusano 7d535a
check_include_file(sys/types.h HAVE_SYS_TYPES_H)
kusano 7d535a
check_include_file(stdint.h    HAVE_STDINT_H)
kusano 7d535a
check_include_file(stddef.h    HAVE_STDDEF_H)
kusano 7d535a
kusano 7d535a
#
kusano 7d535a
# Check to see if we have large file support
kusano 7d535a
#
kusano 7d535a
set(CMAKE_REQUIRED_DEFINITIONS -D_LARGEFILE64_SOURCE=1)
kusano 7d535a
# We add these other definitions here because CheckTypeSize.cmake
kusano 7d535a
# in CMake 2.4.x does not automatically do so and we want
kusano 7d535a
# compatibility with CMake 2.4.x.
kusano 7d535a
if(HAVE_SYS_TYPES_H)
kusano 7d535a
    list(APPEND CMAKE_REQUIRED_DEFINITIONS -DHAVE_SYS_TYPES_H)
kusano 7d535a
endif()
kusano 7d535a
if(HAVE_STDINT_H)
kusano 7d535a
    list(APPEND CMAKE_REQUIRED_DEFINITIONS -DHAVE_STDINT_H)
kusano 7d535a
endif()
kusano 7d535a
if(HAVE_STDDEF_H)
kusano 7d535a
    list(APPEND CMAKE_REQUIRED_DEFINITIONS -DHAVE_STDDEF_H)
kusano 7d535a
endif()
kusano 7d535a
check_type_size(off64_t OFF64_T)
kusano 7d535a
if(HAVE_OFF64_T)
kusano 7d535a
   add_definitions(-D_LARGEFILE64_SOURCE=1)
kusano 7d535a
endif()
kusano 7d535a
set(CMAKE_REQUIRED_DEFINITIONS) # clear variable
kusano 7d535a
kusano 7d535a
#
kusano 7d535a
# Check for fseeko
kusano 7d535a
#
kusano 7d535a
check_function_exists(fseeko HAVE_FSEEKO)
kusano 7d535a
if(NOT HAVE_FSEEKO)
kusano 7d535a
    add_definitions(-DNO_FSEEKO)
kusano 7d535a
endif()
kusano 7d535a
kusano 7d535a
#
kusano 7d535a
# Check for unistd.h
kusano 7d535a
#
kusano 7d535a
check_include_file(unistd.h Z_HAVE_UNISTD_H)
kusano 7d535a
kusano 7d535a
if(MSVC)
kusano 7d535a
    set(CMAKE_DEBUG_POSTFIX "d")
kusano 7d535a
    add_definitions(-D_CRT_SECURE_NO_DEPRECATE)
kusano 7d535a
    add_definitions(-D_CRT_NONSTDC_NO_DEPRECATE)
kusano 7d535a
    include_directories(${CMAKE_CURRENT_SOURCE_DIR})
kusano 7d535a
endif()
kusano 7d535a
kusano 7d535a
if(NOT CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_CURRENT_BINARY_DIR)
kusano 7d535a
    # If we're doing an out of source build and the user has a zconf.h
kusano 7d535a
    # in their source tree...
kusano 7d535a
    if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/zconf.h)
kusano 7d535a
        message(STATUS "Renaming")
kusano 7d535a
        message(STATUS "    ${CMAKE_CURRENT_SOURCE_DIR}/zconf.h")
kusano 7d535a
        message(STATUS "to 'zconf.h.included' because this file is included with zlib")
kusano 7d535a
        message(STATUS "but CMake generates it automatically in the build directory.")
kusano 7d535a
        file(RENAME ${CMAKE_CURRENT_SOURCE_DIR}/zconf.h ${CMAKE_CURRENT_SOURCE_DIR}/zconf.h.included)
kusano 7d535a
  endif()
kusano 7d535a
endif()
kusano 7d535a
kusano 7d535a
set(ZLIB_PC ${CMAKE_CURRENT_BINARY_DIR}/zlib.pc)
kusano 7d535a
configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/zlib.pc.cmakein
kusano 7d535a
		${ZLIB_PC} @ONLY)
kusano 7d535a
configure_file(	${CMAKE_CURRENT_SOURCE_DIR}/zconf.h.cmakein
kusano 7d535a
		${CMAKE_CURRENT_BINARY_DIR}/zconf.h @ONLY)
kusano 7d535a
include_directories(${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_SOURCE_DIR})
kusano 7d535a
kusano 7d535a
kusano 7d535a
#============================================================================
kusano 7d535a
# zlib
kusano 7d535a
#============================================================================
kusano 7d535a
kusano 7d535a
set(ZLIB_PUBLIC_HDRS
kusano 7d535a
    ${CMAKE_CURRENT_BINARY_DIR}/zconf.h
kusano 7d535a
    zlib.h
kusano 7d535a
)
kusano 7d535a
set(ZLIB_PRIVATE_HDRS
kusano 7d535a
    crc32.h
kusano 7d535a
    deflate.h
kusano 7d535a
    gzguts.h
kusano 7d535a
    inffast.h
kusano 7d535a
    inffixed.h
kusano 7d535a
    inflate.h
kusano 7d535a
    inftrees.h
kusano 7d535a
    trees.h
kusano 7d535a
    zutil.h
kusano 7d535a
)
kusano 7d535a
set(ZLIB_SRCS
kusano 7d535a
    adler32.c
kusano 7d535a
    compress.c
kusano 7d535a
    crc32.c
kusano 7d535a
    deflate.c
kusano 7d535a
    gzclose.c
kusano 7d535a
    gzlib.c
kusano 7d535a
    gzread.c
kusano 7d535a
    gzwrite.c
kusano 7d535a
    inflate.c
kusano 7d535a
    infback.c
kusano 7d535a
    inftrees.c
kusano 7d535a
    inffast.c
kusano 7d535a
    trees.c
kusano 7d535a
    uncompr.c
kusano 7d535a
    zutil.c
kusano 7d535a
)
kusano 7d535a
kusano 7d535a
if(NOT MINGW)
kusano 7d535a
    set(ZLIB_SRCS ${ZLIB_SRCS}
kusano 7d535a
        win32/zlib1.rc # If present will override custom build rule below.
kusano 7d535a
    )
kusano 7d535a
endif()
kusano 7d535a
kusano 7d535a
# parse the full version number from zlib.h and include in ZLIB_FULL_VERSION
kusano 7d535a
file(READ ${CMAKE_CURRENT_SOURCE_DIR}/zlib.h _zlib_h_contents)
kusano 7d535a
string(REGEX REPLACE ".*#define[ \t]+ZLIB_VERSION[ \t]+\"([-0-9A-Za-z.]+)\".*"
kusano 7d535a
    "\\1" ZLIB_FULL_VERSION ${_zlib_h_contents})
kusano 7d535a
kusano 7d535a
if(MINGW)
kusano 7d535a
    # This gets us DLL resource information when compiling on MinGW.
kusano 7d535a
    if(NOT CMAKE_RC_COMPILER)
kusano 7d535a
        SET(CMAKE_RC_COMPILER windres.exe)
kusano 7d535a
    endif()
kusano 7d535a
kusano 7d535a
    add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/zlib1rc.obj
kusano 7d535a
                       COMMAND ${CMAKE_RC_COMPILER}
kusano 7d535a
                            -D GCC_WINDRES
kusano 7d535a
                            -I ${CMAKE_CURRENT_SOURCE_DIR}
kusano 7d535a
                            -I ${CMAKE_CURRENT_BINARY_DIR}
kusano 7d535a
                            -o ${CMAKE_CURRENT_BINARY_DIR}/zlib1rc.obj
kusano 7d535a
                            -i ${CMAKE_CURRENT_SOURCE_DIR}/win32/zlib1.rc)
kusano 7d535a
    set(ZLIB_SRCS ${ZLIB_SRCS} ${CMAKE_CURRENT_BINARY_DIR}/zlib1rc.obj)
kusano 7d535a
endif(MINGW)
kusano 7d535a
kusano 7d535a
add_library(zlib SHARED ${ZLIB_SRCS} ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HDRS})
kusano 7d535a
add_library(zlibstatic STATIC ${ZLIB_SRCS} ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HDRS})
kusano 7d535a
set_target_properties(zlib PROPERTIES DEFINE_SYMBOL ZLIB_DLL)
kusano 7d535a
set_target_properties(zlib PROPERTIES SOVERSION 1)
kusano 7d535a
kusano 7d535a
if(NOT CYGWIN)
kusano 7d535a
    # This property causes shared libraries on Linux to have the full version
kusano 7d535a
    # encoded into their final filename.  We disable this on Cygwin because
kusano 7d535a
    # it causes cygz-${ZLIB_FULL_VERSION}.dll to be created when cygz.dll
kusano 7d535a
    # seems to be the default.
kusano 7d535a
    #
kusano 7d535a
    # This has no effect with MSVC, on that platform the version info for
kusano 7d535a
    # the DLL comes from the resource file win32/zlib1.rc
kusano 7d535a
    set_target_properties(zlib PROPERTIES VERSION ${ZLIB_FULL_VERSION})
kusano 7d535a
endif()
kusano 7d535a
kusano 7d535a
if(UNIX)
kusano 7d535a
    # On unix-like platforms the library is almost always called libz
kusano 7d535a
   set_target_properties(zlib zlibstatic PROPERTIES OUTPUT_NAME z)
kusano 7d535a
   set_target_properties(zlib PROPERTIES LINK_FLAGS "-Wl,--version-script,${CMAKE_CURRENT_SOURCE_DIR}/zlib.map")
kusano 7d535a
elseif(BUILD_SHARED_LIBS AND WIN32)
kusano 7d535a
    # Creates zlib1.dll when building shared library version
kusano 7d535a
    set_target_properties(zlib PROPERTIES SUFFIX "1.dll")
kusano 7d535a
endif()
kusano 7d535a
kusano 7d535a
if(NOT SKIP_INSTALL_LIBRARIES AND NOT SKIP_INSTALL_ALL )
kusano 7d535a
    install(TARGETS zlib zlibstatic
kusano 7d535a
        RUNTIME DESTINATION "${INSTALL_BIN_DIR}"
kusano 7d535a
        ARCHIVE DESTINATION "${INSTALL_LIB_DIR}"
kusano 7d535a
        LIBRARY DESTINATION "${INSTALL_LIB_DIR}" )
kusano 7d535a
endif()
kusano 7d535a
if(NOT SKIP_INSTALL_HEADERS AND NOT SKIP_INSTALL_ALL )
kusano 7d535a
    install(FILES ${ZLIB_PUBLIC_HDRS} DESTINATION "${INSTALL_INC_DIR}")
kusano 7d535a
endif()
kusano 7d535a
if(NOT SKIP_INSTALL_FILES AND NOT SKIP_INSTALL_ALL )
kusano 7d535a
    install(FILES zlib.3 DESTINATION "${INSTALL_MAN_DIR}/man3")
kusano 7d535a
endif()
kusano 7d535a
if(NOT SKIP_INSTALL_FILES AND NOT SKIP_INSTALL_ALL )
kusano 7d535a
    install(FILES ${ZLIB_PC} DESTINATION "${INSTALL_PKGCONFIG_DIR}")
kusano 7d535a
endif()
kusano 7d535a
kusano 7d535a
#============================================================================
kusano 7d535a
# Example binaries
kusano 7d535a
#============================================================================
kusano 7d535a
kusano 7d535a
add_executable(example test/example.c)
kusano 7d535a
target_link_libraries(example zlib)
kusano 7d535a
add_test(example example)
kusano 7d535a
kusano 7d535a
add_executable(minigzip test/minigzip.c)
kusano 7d535a
target_link_libraries(minigzip zlib)
kusano 7d535a
kusano 7d535a
if(HAVE_OFF64_T)
kusano 7d535a
    add_executable(example64 test/example.c)
kusano 7d535a
    target_link_libraries(example64 zlib)
kusano 7d535a
    set_target_properties(example64 PROPERTIES COMPILE_FLAGS "-D_FILE_OFFSET_BITS=64")
kusano 7d535a
    add_test(example64 example64)
kusano 7d535a
kusano 7d535a
    add_executable(minigzip64 test/minigzip.c)
kusano 7d535a
    target_link_libraries(minigzip64 zlib)
kusano 7d535a
    set_target_properties(minigzip64 PROPERTIES COMPILE_FLAGS "-D_FILE_OFFSET_BITS=64")
kusano 7d535a
endif()