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