external/zlib-1.2.11/CMakeLists.txt
author Sam Lantinga <slouken@libsdl.org>
Fri, 26 Oct 2018 14:58:01 -0700
changeset 617 d64228a395fc
parent 521 9ed2f7d4a251
permissions -rw-r--r--
Fixed webp library detection when cross-compiling for Windows
slouken@521
     1
cmake_minimum_required(VERSION 2.4.4)
slouken@521
     2
set(CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS ON)
slouken@521
     3
slouken@521
     4
project(zlib C)
slouken@521
     5
slouken@521
     6
set(VERSION "1.2.11")
slouken@521
     7
slouken@521
     8
option(ASM686 "Enable building i686 assembly implementation")
slouken@521
     9
option(AMD64 "Enable building amd64 assembly implementation")
slouken@521
    10
slouken@521
    11
set(INSTALL_BIN_DIR "${CMAKE_INSTALL_PREFIX}/bin" CACHE PATH "Installation directory for executables")
slouken@521
    12
set(INSTALL_LIB_DIR "${CMAKE_INSTALL_PREFIX}/lib" CACHE PATH "Installation directory for libraries")
slouken@521
    13
set(INSTALL_INC_DIR "${CMAKE_INSTALL_PREFIX}/include" CACHE PATH "Installation directory for headers")
slouken@521
    14
set(INSTALL_MAN_DIR "${CMAKE_INSTALL_PREFIX}/share/man" CACHE PATH "Installation directory for manual pages")
slouken@521
    15
set(INSTALL_PKGCONFIG_DIR "${CMAKE_INSTALL_PREFIX}/share/pkgconfig" CACHE PATH "Installation directory for pkgconfig (.pc) files")
slouken@521
    16
slouken@521
    17
include(CheckTypeSize)
slouken@521
    18
include(CheckFunctionExists)
slouken@521
    19
include(CheckIncludeFile)
slouken@521
    20
include(CheckCSourceCompiles)
slouken@521
    21
enable_testing()
slouken@521
    22
slouken@521
    23
check_include_file(sys/types.h HAVE_SYS_TYPES_H)
slouken@521
    24
check_include_file(stdint.h    HAVE_STDINT_H)
slouken@521
    25
check_include_file(stddef.h    HAVE_STDDEF_H)
slouken@521
    26
slouken@521
    27
#
slouken@521
    28
# Check to see if we have large file support
slouken@521
    29
#
slouken@521
    30
set(CMAKE_REQUIRED_DEFINITIONS -D_LARGEFILE64_SOURCE=1)
slouken@521
    31
# We add these other definitions here because CheckTypeSize.cmake
slouken@521
    32
# in CMake 2.4.x does not automatically do so and we want
slouken@521
    33
# compatibility with CMake 2.4.x.
slouken@521
    34
if(HAVE_SYS_TYPES_H)
slouken@521
    35
    list(APPEND CMAKE_REQUIRED_DEFINITIONS -DHAVE_SYS_TYPES_H)
slouken@521
    36
endif()
slouken@521
    37
if(HAVE_STDINT_H)
slouken@521
    38
    list(APPEND CMAKE_REQUIRED_DEFINITIONS -DHAVE_STDINT_H)
slouken@521
    39
endif()
slouken@521
    40
if(HAVE_STDDEF_H)
slouken@521
    41
    list(APPEND CMAKE_REQUIRED_DEFINITIONS -DHAVE_STDDEF_H)
slouken@521
    42
endif()
slouken@521
    43
check_type_size(off64_t OFF64_T)
slouken@521
    44
if(HAVE_OFF64_T)
slouken@521
    45
   add_definitions(-D_LARGEFILE64_SOURCE=1)
slouken@521
    46
endif()
slouken@521
    47
set(CMAKE_REQUIRED_DEFINITIONS) # clear variable
slouken@521
    48
slouken@521
    49
#
slouken@521
    50
# Check for fseeko
slouken@521
    51
#
slouken@521
    52
check_function_exists(fseeko HAVE_FSEEKO)
slouken@521
    53
if(NOT HAVE_FSEEKO)
slouken@521
    54
    add_definitions(-DNO_FSEEKO)
slouken@521
    55
endif()
slouken@521
    56
slouken@521
    57
#
slouken@521
    58
# Check for unistd.h
slouken@521
    59
#
slouken@521
    60
check_include_file(unistd.h Z_HAVE_UNISTD_H)
slouken@521
    61
slouken@521
    62
if(MSVC)
slouken@521
    63
    set(CMAKE_DEBUG_POSTFIX "d")
slouken@521
    64
    add_definitions(-D_CRT_SECURE_NO_DEPRECATE)
slouken@521
    65
    add_definitions(-D_CRT_NONSTDC_NO_DEPRECATE)
slouken@521
    66
    include_directories(${CMAKE_CURRENT_SOURCE_DIR})
slouken@521
    67
endif()
slouken@521
    68
slouken@521
    69
if(NOT CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_CURRENT_BINARY_DIR)
slouken@521
    70
    # If we're doing an out of source build and the user has a zconf.h
slouken@521
    71
    # in their source tree...
slouken@521
    72
    if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/zconf.h)
slouken@521
    73
        message(STATUS "Renaming")
slouken@521
    74
        message(STATUS "    ${CMAKE_CURRENT_SOURCE_DIR}/zconf.h")
slouken@521
    75
        message(STATUS "to 'zconf.h.included' because this file is included with zlib")
slouken@521
    76
        message(STATUS "but CMake generates it automatically in the build directory.")
slouken@521
    77
        file(RENAME ${CMAKE_CURRENT_SOURCE_DIR}/zconf.h ${CMAKE_CURRENT_SOURCE_DIR}/zconf.h.included)
slouken@521
    78
  endif()
slouken@521
    79
endif()
slouken@521
    80
slouken@521
    81
set(ZLIB_PC ${CMAKE_CURRENT_BINARY_DIR}/zlib.pc)
slouken@521
    82
configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/zlib.pc.cmakein
slouken@521
    83
		${ZLIB_PC} @ONLY)
slouken@521
    84
configure_file(	${CMAKE_CURRENT_SOURCE_DIR}/zconf.h.cmakein
slouken@521
    85
		${CMAKE_CURRENT_BINARY_DIR}/zconf.h @ONLY)
slouken@521
    86
include_directories(${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_SOURCE_DIR})
slouken@521
    87
slouken@521
    88
slouken@521
    89
#============================================================================
slouken@521
    90
# zlib
slouken@521
    91
#============================================================================
slouken@521
    92
slouken@521
    93
set(ZLIB_PUBLIC_HDRS
slouken@521
    94
    ${CMAKE_CURRENT_BINARY_DIR}/zconf.h
slouken@521
    95
    zlib.h
slouken@521
    96
)
slouken@521
    97
set(ZLIB_PRIVATE_HDRS
slouken@521
    98
    crc32.h
slouken@521
    99
    deflate.h
slouken@521
   100
    gzguts.h
slouken@521
   101
    inffast.h
slouken@521
   102
    inffixed.h
slouken@521
   103
    inflate.h
slouken@521
   104
    inftrees.h
slouken@521
   105
    trees.h
slouken@521
   106
    zutil.h
slouken@521
   107
)
slouken@521
   108
set(ZLIB_SRCS
slouken@521
   109
    adler32.c
slouken@521
   110
    compress.c
slouken@521
   111
    crc32.c
slouken@521
   112
    deflate.c
slouken@521
   113
    gzclose.c
slouken@521
   114
    gzlib.c
slouken@521
   115
    gzread.c
slouken@521
   116
    gzwrite.c
slouken@521
   117
    inflate.c
slouken@521
   118
    infback.c
slouken@521
   119
    inftrees.c
slouken@521
   120
    inffast.c
slouken@521
   121
    trees.c
slouken@521
   122
    uncompr.c
slouken@521
   123
    zutil.c
slouken@521
   124
)
slouken@521
   125
slouken@521
   126
if(NOT MINGW)
slouken@521
   127
    set(ZLIB_DLL_SRCS
slouken@521
   128
        win32/zlib1.rc # If present will override custom build rule below.
slouken@521
   129
    )
slouken@521
   130
endif()
slouken@521
   131
slouken@521
   132
if(CMAKE_COMPILER_IS_GNUCC)
slouken@521
   133
    if(ASM686)
slouken@521
   134
        set(ZLIB_ASMS contrib/asm686/match.S)
slouken@521
   135
    elseif (AMD64)
slouken@521
   136
        set(ZLIB_ASMS contrib/amd64/amd64-match.S)
slouken@521
   137
    endif ()
slouken@521
   138
slouken@521
   139
	if(ZLIB_ASMS)
slouken@521
   140
		add_definitions(-DASMV)
slouken@521
   141
		set_source_files_properties(${ZLIB_ASMS} PROPERTIES LANGUAGE C COMPILE_FLAGS -DNO_UNDERLINE)
slouken@521
   142
	endif()
slouken@521
   143
endif()
slouken@521
   144
slouken@521
   145
if(MSVC)
slouken@521
   146
    if(ASM686)
slouken@521
   147
		ENABLE_LANGUAGE(ASM_MASM)
slouken@521
   148
        set(ZLIB_ASMS
slouken@521
   149
			contrib/masmx86/inffas32.asm
slouken@521
   150
			contrib/masmx86/match686.asm
slouken@521
   151
		)
slouken@521
   152
    elseif (AMD64)
slouken@521
   153
		ENABLE_LANGUAGE(ASM_MASM)
slouken@521
   154
        set(ZLIB_ASMS
slouken@521
   155
			contrib/masmx64/gvmat64.asm
slouken@521
   156
			contrib/masmx64/inffasx64.asm
slouken@521
   157
		)
slouken@521
   158
    endif()
slouken@521
   159
slouken@521
   160
	if(ZLIB_ASMS)
slouken@521
   161
		add_definitions(-DASMV -DASMINF)
slouken@521
   162
	endif()
slouken@521
   163
endif()
slouken@521
   164
slouken@521
   165
# parse the full version number from zlib.h and include in ZLIB_FULL_VERSION
slouken@521
   166
file(READ ${CMAKE_CURRENT_SOURCE_DIR}/zlib.h _zlib_h_contents)
slouken@521
   167
string(REGEX REPLACE ".*#define[ \t]+ZLIB_VERSION[ \t]+\"([-0-9A-Za-z.]+)\".*"
slouken@521
   168
    "\\1" ZLIB_FULL_VERSION ${_zlib_h_contents})
slouken@521
   169
slouken@521
   170
if(MINGW)
slouken@521
   171
    # This gets us DLL resource information when compiling on MinGW.
slouken@521
   172
    if(NOT CMAKE_RC_COMPILER)
slouken@521
   173
        set(CMAKE_RC_COMPILER windres.exe)
slouken@521
   174
    endif()
slouken@521
   175
slouken@521
   176
    add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/zlib1rc.obj
slouken@521
   177
                       COMMAND ${CMAKE_RC_COMPILER}
slouken@521
   178
                            -D GCC_WINDRES
slouken@521
   179
                            -I ${CMAKE_CURRENT_SOURCE_DIR}
slouken@521
   180
                            -I ${CMAKE_CURRENT_BINARY_DIR}
slouken@521
   181
                            -o ${CMAKE_CURRENT_BINARY_DIR}/zlib1rc.obj
slouken@521
   182
                            -i ${CMAKE_CURRENT_SOURCE_DIR}/win32/zlib1.rc)
slouken@521
   183
    set(ZLIB_DLL_SRCS ${CMAKE_CURRENT_BINARY_DIR}/zlib1rc.obj)
slouken@521
   184
endif(MINGW)
slouken@521
   185
slouken@521
   186
add_library(zlib SHARED ${ZLIB_SRCS} ${ZLIB_ASMS} ${ZLIB_DLL_SRCS} ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HDRS})
slouken@521
   187
add_library(zlibstatic STATIC ${ZLIB_SRCS} ${ZLIB_ASMS} ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HDRS})
slouken@521
   188
set_target_properties(zlib PROPERTIES DEFINE_SYMBOL ZLIB_DLL)
slouken@521
   189
set_target_properties(zlib PROPERTIES SOVERSION 1)
slouken@521
   190
slouken@521
   191
if(NOT CYGWIN)
slouken@521
   192
    # This property causes shared libraries on Linux to have the full version
slouken@521
   193
    # encoded into their final filename.  We disable this on Cygwin because
slouken@521
   194
    # it causes cygz-${ZLIB_FULL_VERSION}.dll to be created when cygz.dll
slouken@521
   195
    # seems to be the default.
slouken@521
   196
    #
slouken@521
   197
    # This has no effect with MSVC, on that platform the version info for
slouken@521
   198
    # the DLL comes from the resource file win32/zlib1.rc
slouken@521
   199
    set_target_properties(zlib PROPERTIES VERSION ${ZLIB_FULL_VERSION})
slouken@521
   200
endif()
slouken@521
   201
slouken@521
   202
if(UNIX)
slouken@521
   203
    # On unix-like platforms the library is almost always called libz
slouken@521
   204
   set_target_properties(zlib zlibstatic PROPERTIES OUTPUT_NAME z)
slouken@521
   205
   if(NOT APPLE)
slouken@521
   206
     set_target_properties(zlib PROPERTIES LINK_FLAGS "-Wl,--version-script,\"${CMAKE_CURRENT_SOURCE_DIR}/zlib.map\"")
slouken@521
   207
   endif()
slouken@521
   208
elseif(BUILD_SHARED_LIBS AND WIN32)
slouken@521
   209
    # Creates zlib1.dll when building shared library version
slouken@521
   210
    set_target_properties(zlib PROPERTIES SUFFIX "1.dll")
slouken@521
   211
endif()
slouken@521
   212
slouken@521
   213
if(NOT SKIP_INSTALL_LIBRARIES AND NOT SKIP_INSTALL_ALL )
slouken@521
   214
    install(TARGETS zlib zlibstatic
slouken@521
   215
        RUNTIME DESTINATION "${INSTALL_BIN_DIR}"
slouken@521
   216
        ARCHIVE DESTINATION "${INSTALL_LIB_DIR}"
slouken@521
   217
        LIBRARY DESTINATION "${INSTALL_LIB_DIR}" )
slouken@521
   218
endif()
slouken@521
   219
if(NOT SKIP_INSTALL_HEADERS AND NOT SKIP_INSTALL_ALL )
slouken@521
   220
    install(FILES ${ZLIB_PUBLIC_HDRS} DESTINATION "${INSTALL_INC_DIR}")
slouken@521
   221
endif()
slouken@521
   222
if(NOT SKIP_INSTALL_FILES AND NOT SKIP_INSTALL_ALL )
slouken@521
   223
    install(FILES zlib.3 DESTINATION "${INSTALL_MAN_DIR}/man3")
slouken@521
   224
endif()
slouken@521
   225
if(NOT SKIP_INSTALL_FILES AND NOT SKIP_INSTALL_ALL )
slouken@521
   226
    install(FILES ${ZLIB_PC} DESTINATION "${INSTALL_PKGCONFIG_DIR}")
slouken@521
   227
endif()
slouken@521
   228
slouken@521
   229
#============================================================================
slouken@521
   230
# Example binaries
slouken@521
   231
#============================================================================
slouken@521
   232
slouken@521
   233
add_executable(example test/example.c)
slouken@521
   234
target_link_libraries(example zlib)
slouken@521
   235
add_test(example example)
slouken@521
   236
slouken@521
   237
add_executable(minigzip test/minigzip.c)
slouken@521
   238
target_link_libraries(minigzip zlib)
slouken@521
   239
slouken@521
   240
if(HAVE_OFF64_T)
slouken@521
   241
    add_executable(example64 test/example.c)
slouken@521
   242
    target_link_libraries(example64 zlib)
slouken@521
   243
    set_target_properties(example64 PROPERTIES COMPILE_FLAGS "-D_FILE_OFFSET_BITS=64")
slouken@521
   244
    add_test(example64 example64)
slouken@521
   245
slouken@521
   246
    add_executable(minigzip64 test/minigzip.c)
slouken@521
   247
    target_link_libraries(minigzip64 zlib)
slouken@521
   248
    set_target_properties(minigzip64 PROPERTIES COMPILE_FLAGS "-D_FILE_OFFSET_BITS=64")
slouken@521
   249
endif()