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