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