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