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