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