unknown | 3d142b8 | 2014-03-19 15:27:08 +0100 | [diff] [blame] | 1 | cmake_minimum_required(VERSION 2.8.4 FATAL_ERROR) |
| 2 | |
| 3 | if (WIN32 AND NOT CYGWIN) |
Andreas Schuh | 4118166 | 2014-03-19 16:46:56 +0000 | [diff] [blame] | 4 | set (OS_WINDOWS 1) |
unknown | 3d142b8 | 2014-03-19 15:27:08 +0100 | [diff] [blame] | 5 | else () |
Andreas Schuh | 4118166 | 2014-03-19 16:46:56 +0000 | [diff] [blame] | 6 | set (OS_WINDOWS 0) |
unknown | 3d142b8 | 2014-03-19 15:27:08 +0100 | [diff] [blame] | 7 | endif () |
| 8 | |
| 9 | # ---------------------------------------------------------------------------- |
| 10 | # includes |
| 11 | set (CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake") |
| 12 | |
| 13 | include (utils) |
| 14 | |
| 15 | # ---------------------------------------------------------------------------- |
| 16 | # package information |
| 17 | set (PROJECT_NAME "gflags") |
| 18 | set (PACKAGE_NAME "${PROJECT_NAME}") |
| 19 | set (PACKAGE_VERSION "2.1.0") |
| 20 | set (PACKAGE_STRING "${PROJECT_NAME} ${PACKAGE_VERSION}") |
| 21 | set (PACKAGE_TARNAME "${PROJECT_NAME}-${PACKAGE_VERSION}") |
| 22 | set (PACKAGE_BUGREPORT "https://code.google.com/p/gflags/issues/") |
| 23 | |
| 24 | project (${PROJECT_NAME} CXX) |
| 25 | |
| 26 | version_numbers ( |
| 27 | ${PACKAGE_VERSION} |
| 28 | PACKAGE_VERSION_MAJOR |
| 29 | PACKAGE_VERSION_MINOR |
| 30 | PACKAGE_VERSION_PATCH |
| 31 | ) |
| 32 | |
| 33 | # ---------------------------------------------------------------------------- |
| 34 | # configure options |
| 35 | option (BUILD_SHARED_LIBS "Request build of shared libraries." OFF) |
| 36 | |
Andreas Schuh | 4118166 | 2014-03-19 16:46:56 +0000 | [diff] [blame] | 37 | if (OS_WINDOWS AND BUILD_SHARED_LIBS) |
unknown | 3d142b8 | 2014-03-19 15:27:08 +0100 | [diff] [blame] | 38 | set (GFLAGS_IS_A_DLL 1) |
| 39 | else () |
| 40 | set (GFLAGS_IS_A_DLL 0) |
| 41 | endif () |
| 42 | |
| 43 | option (BUILD_gflags_LIB "Request build of the multi-threaded gflags library." ON) |
| 44 | option (BUILD_gflags_nothreads_LIB "Request build of the single-threaded gflags library." ON) |
| 45 | |
| 46 | if (NOT BUILD_gflags_LIB AND NOT BUILD_gflags_nothreads_LIB) |
| 47 | message (FATAL_ERROR "At least one of BUILD_gflags_LIB and BUILD_gflags_nothreads_LIB must be ON.") |
| 48 | endif () |
| 49 | |
| 50 | option (BUILD_NEGATIVE_COMPILATION_TESTS "Request addition of negative compilation tests." OFF) |
| 51 | mark_as_advanced (BUILD_NEGATIVE_COMPILATION_TESTS) |
| 52 | |
| 53 | set (GFLAGS_NAMESPACE "gflags" CACHE STRING "C++ namespace identifier of gflags library.") |
| 54 | mark_as_advanced (GFLAGS_NAMESPACE) |
| 55 | |
| 56 | mark_as_advanced (CLEAR CMAKE_INSTALL_PREFIX) |
| 57 | mark_as_advanced (CMAKE_CONFIGURATION_TYPES) |
| 58 | if (NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CXX_FLAGS AND NOT CMAKE_C_FLAGS) |
| 59 | set ( |
| 60 | CMAKE_BUILD_TYPE "Release" |
| 61 | CACHE STRING "Choose the type of build, options are: None (CMAKE_C_FLAGS and CMAKE_CXX_FLAGS used) Debug Release RelWithDebInfo MinSizeRel." |
| 62 | FORCE |
| 63 | ) |
| 64 | endif () |
| 65 | |
| 66 | if (APPLE) |
| 67 | mark_as_advanced(CMAKE_OSX_ARCHITECTURES |
| 68 | CMAKE_OSX_DEPLOYMENT_TARGET |
| 69 | CMAKE_OSX_SYSROOT) |
| 70 | endif () |
| 71 | |
| 72 | # ---------------------------------------------------------------------------- |
| 73 | # system checks |
| 74 | include (CheckTypeSize) |
| 75 | include (CheckIncludeFileCXX) |
| 76 | include (CheckCXXSymbolExists) |
| 77 | |
| 78 | if (MSVC) |
| 79 | set (HAVE_SYS_TYPES_H 1) |
| 80 | set (HAVE_STDINT_H 1) |
| 81 | set (HAVE_STDDEF_H 1) # used by CheckTypeSize module |
| 82 | set (HAVE_INTTYPES_H 0) |
| 83 | set (HAVE_UNISTD_H 0) |
| 84 | set (HAVE_SYS_STAT_H 1) |
| 85 | check_include_file_cxx ("shlwapi.h" HAVE_SHLWAPI_H) |
| 86 | else () |
| 87 | foreach (fname IN ITEMS unistd stdint inttypes sys/types sys/stat fnmatch) |
| 88 | string (TOUPPER "${fname}" FNAME) |
| 89 | string (REGEX REPLACE "/" "_" FNAME "${FNAME}") |
| 90 | if (NOT HAVE_${FNAME}_H) |
| 91 | check_include_file_cxx ("${fname}.h" HAVE_${FNAME}_H) |
| 92 | endif () |
| 93 | endforeach () |
| 94 | # the following are used in #if not #ifdef |
| 95 | bool_to_int (HAVE_STDINT_H) |
| 96 | bool_to_int (HAVE_SYS_TYPES_H) |
| 97 | bool_to_int (HAVE_INTTYPES_H) |
| 98 | endif () |
| 99 | |
| 100 | set (GFLAGS_INTTYPES_FORMAT "" CACHE STRING "Format of integer types: \"C99\" (uint32_t), \"BSD\" (u_int32_t), \"VC7\" (__int32)") |
| 101 | set_property (CACHE GFLAGS_INTTYPES_FORMAT PROPERTY STRINGS "C99;BSD;VC7") |
| 102 | mark_as_advanced (GFLAGS_INTTYPES_FORMAT) |
| 103 | if (NOT GFLAGS_INTTYPES_FORMAT) |
| 104 | set (TYPES uint32_t u_int32_t) |
| 105 | if (MSVC) |
| 106 | list (INSERT TYPES 0 __int32) |
| 107 | endif () |
| 108 | foreach (type IN LISTS TYPES) |
| 109 | check_type_size (${type} ${type} LANGUAGE CXX) |
| 110 | if (HAVE_${type}) |
| 111 | break () |
| 112 | endif () |
| 113 | endforeach () |
| 114 | if (HAVE_uint32_t) |
| 115 | set_property (CACHE GFLAGS_INTTYPES_FORMAT PROPERTY VALUE C99) |
| 116 | elseif (HAVE_u_int32_t) |
| 117 | set_property (CACHE GFLAGS_INTTYPES_FORMAT PROPERTY VALUE BSD) |
| 118 | elseif (HAVE___int32) |
| 119 | set_property (CACHE GFLAGS_INTTYPES_FORMAT PROPERTY VALUE VC7) |
| 120 | else () |
| 121 | mark_as_advanced (CLEAR GFLAGS_INTTYPES_FORMAT) |
| 122 | message (FATAL_ERROR "Do not know how to define a 32-bit integer quantity on your system!" |
| 123 | " Neither uint32_t, u_int32_t, nor __int32 seem to be available." |
| 124 | " Set GFLAGS_INTTYPES_FORMAT to either C99, BSD, or VC7 and try again.") |
| 125 | endif () |
| 126 | endif () |
| 127 | # use of special characters in strings to circumvent bug #0008226 |
| 128 | if ("^${GFLAGS_INTTYPES_FORMAT}$" STREQUAL "^WIN$") |
| 129 | set_property (CACHE GFLAGS_INTTYPES_FORMAT PROPERTY VALUE VC7) |
| 130 | endif () |
| 131 | if (NOT GFLAGS_INTTYPES_FORMAT MATCHES "^(C99|BSD|VC7)$") |
| 132 | message (FATAL_ERROR "Invalid value for GFLAGS_INTTYPES_FORMAT! Choose one of \"C99\", \"BSD\", or \"VC7\"") |
| 133 | endif () |
| 134 | set (GFLAGS_INTTYPES_FORMAT_C99 0) |
| 135 | set (GFLAGS_INTTYPES_FORMAT_BSD 0) |
| 136 | set (GFLAGS_INTTYPES_FORMAT_VC7 0) |
| 137 | set ("GFLAGS_INTTYPES_FORMAT_${GFLAGS_INTTYPES_FORMAT}" 1) |
| 138 | |
| 139 | if (MSVC) |
| 140 | set (HAVE_strtoll 0) |
| 141 | set (HAVE_strtoq 0) |
| 142 | else () |
Andreas Schuh | 0b116eb | 2014-03-19 15:56:26 +0000 | [diff] [blame] | 143 | check_cxx_symbol_exists (strtoll stdlib.h HAVE_STRTOLL) |
| 144 | if (NOT HAVE_STRTOLL) |
| 145 | check_cxx_symbol_exists (strtoq stdlib.h HAVE_STRTOQ) |
| 146 | endif () |
unknown | 3d142b8 | 2014-03-19 15:27:08 +0100 | [diff] [blame] | 147 | endif () |
| 148 | |
| 149 | set (CMAKE_THREAD_PREFER_PTHREAD TRUE) |
| 150 | find_package (ThreadsCXX) |
| 151 | if (Threads_FOUND AND CMAKE_USE_PTHREADS_INIT) |
| 152 | set (HAVE_PTHREAD 1) |
| 153 | check_type_size (pthread_rwlock_t RWLOCK LANGUAGE CXX) |
| 154 | else () |
| 155 | set (HAVE_PTHREAD 0) |
| 156 | endif () |
| 157 | |
| 158 | if (UNIX AND NOT HAVE_PTHREAD AND BUILD_gflags_LIB) |
| 159 | if (CMAKE_HAVE_PTHREAD_H) |
| 160 | set (what "library") |
| 161 | else () |
| 162 | set (what ".h file") |
| 163 | endif () |
| 164 | message (FATAL_ERROR "Could not find pthread${what}. Check the log file" |
| 165 | "\n\t${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log" |
| 166 | "\nor disable the build of the multi-threaded gflags library (BUILD_gflags_LIB=OFF).") |
| 167 | endif () |
| 168 | |
| 169 | # ---------------------------------------------------------------------------- |
| 170 | # source files - excluding root subdirectory and/or .in suffix |
| 171 | set (PUBLIC_HDRS |
| 172 | "gflags.h" |
| 173 | "gflags_declare.h" |
| 174 | "gflags_completions.h" |
| 175 | ) |
| 176 | |
| 177 | set (PRIVATE_HDRS |
| 178 | "config.h" |
| 179 | ) |
| 180 | |
| 181 | set (GFLAGS_SRCS |
| 182 | "gflags.cc" |
| 183 | "gflags_reporting.cc" |
| 184 | "gflags_completions.cc" |
| 185 | ) |
| 186 | |
Andreas Schuh | 4118166 | 2014-03-19 16:46:56 +0000 | [diff] [blame] | 187 | if (OS_WINDOWS) |
unknown | 3d142b8 | 2014-03-19 15:27:08 +0100 | [diff] [blame] | 188 | list (APPEND PRIVATE_HDRS "windows_port.h") |
| 189 | list (APPEND GFLAGS_SRCS "windows_port.cc") |
| 190 | endif () |
| 191 | |
| 192 | # ---------------------------------------------------------------------------- |
| 193 | # configure source files |
| 194 | if (CMAKE_COMPILER_IS_GNUCXX) |
| 195 | set (GFLAGS_ATTRIBUTE_UNUSED "__attribute((unused))") |
| 196 | else () |
| 197 | set (GFLAGS_ATTRIBUTE_UNUSED) |
| 198 | endif () |
| 199 | |
| 200 | configure_headers (PUBLIC_HDRS ${PUBLIC_HDRS}) |
| 201 | configure_sources (PRIVATE_HDRS ${PRIVATE_HDRS}) |
| 202 | configure_sources (GFLAGS_SRCS ${GFLAGS_SRCS}) |
| 203 | |
| 204 | # ---------------------------------------------------------------------------- |
| 205 | # output directories |
| 206 | set (CMAKE_RUNTIME_OUTPUT_DIRECTORY "bin") |
| 207 | set (CMAKE_LIBRARY_OUTPUT_DIRECTORY "lib") |
| 208 | set (CMAKE_ARCHIVE_OUTPUT_DIRECTORY "lib") |
| 209 | |
| 210 | # ---------------------------------------------------------------------------- |
| 211 | # add library target |
| 212 | include_directories ("${PROJECT_SOURCE_DIR}/src") |
| 213 | include_directories ("${PROJECT_BINARY_DIR}/include") |
| 214 | include_directories ("${PROJECT_BINARY_DIR}/include/${GFLAGS_NAMESPACE}") |
| 215 | |
| 216 | set (LIB_TARGETS) |
| 217 | if (BUILD_gflags_LIB) |
| 218 | add_library (gflags ${GFLAGS_SRCS} ${PRIVATE_HDRS} ${PUBLIC_HDRS}) |
| 219 | list (APPEND LIB_TARGETS gflags) |
| 220 | endif () |
| 221 | if (BUILD_gflags_nothreads_LIB) |
| 222 | add_library (gflags_nothreads ${GFLAGS_SRCS} ${PRIVATE_HDRS} ${PUBLIC_HDRS}) |
| 223 | set_target_properties (gflags_nothreads PROPERTIES COMPILE_DEFINITIONS NO_THREADS) |
| 224 | list (APPEND LIB_TARGETS gflags_nothreads) |
| 225 | endif () |
| 226 | |
| 227 | # ---------------------------------------------------------------------------- |
| 228 | # installation |
Andreas Schuh | 4118166 | 2014-03-19 16:46:56 +0000 | [diff] [blame] | 229 | if (OS_WINDOWS) |
unknown | 3d142b8 | 2014-03-19 15:27:08 +0100 | [diff] [blame] | 230 | set (RUNTIME_INSTALL_DIR Bin) |
| 231 | set (LIBRARY_INSTALL_DIR Lib) |
| 232 | set (INCLUDE_INSTALL_DIR Include) |
| 233 | set (CONFIG_INSTALL_DIR CMake) |
| 234 | else () |
| 235 | set (RUNTIME_INSTALL_DIR bin) |
| 236 | set (LIBRARY_INSTALL_DIR lib) |
| 237 | set (INCLUDE_INSTALL_DIR include) |
| 238 | set (CONFIG_INSTALL_DIR lib/cmake/${PACKAGE_NAME}) |
| 239 | endif () |
| 240 | |
| 241 | install (TARGETS ${LIB_TARGETS} DESTINATION ${LIBRARY_INSTALL_DIR} EXPORT gflags-lib) |
| 242 | install (FILES ${PUBLIC_HDRS} DESTINATION ${INCLUDE_INSTALL_DIR}/${GFLAGS_NAMESPACE}) |
| 243 | |
| 244 | file (RELATIVE_PATH INSTALL_PREFIX_REL2CONFIG_DIR "${CMAKE_INSTALL_PREFIX}/${CONFIG_INSTALL_DIR}" "${CMAKE_INSTALL_PREFIX}") |
| 245 | configure_file (cmake/config.cmake.in "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-config-install.cmake" @ONLY) |
| 246 | configure_file (cmake/version.cmake.in "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-config-version.cmake" @ONLY) |
| 247 | |
| 248 | install ( |
| 249 | FILES "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-config-install.cmake" |
| 250 | RENAME ${PACKAGE_NAME}-config.cmake |
| 251 | DESTINATION ${CONFIG_INSTALL_DIR} |
| 252 | ) |
| 253 | |
| 254 | install ( |
| 255 | FILES "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-config-version.cmake" |
| 256 | DESTINATION ${CONFIG_INSTALL_DIR} |
| 257 | ) |
| 258 | |
| 259 | install (EXPORT gflags-lib DESTINATION ${CONFIG_INSTALL_DIR} FILE ${PACKAGE_NAME}-export.cmake) |
| 260 | |
| 261 | if (UNIX) |
| 262 | install (PROGRAMS src/gflags_completions.sh DESTINATION ${RUNTIME_INSTALL_DIR}) |
| 263 | endif () |
| 264 | |
| 265 | # ---------------------------------------------------------------------------- |
| 266 | # support direct use of build tree |
| 267 | set (INSTALL_PREFIX_REL2CONFIG_DIR .) |
| 268 | export (TARGETS ${LIB_TARGETS} FILE "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-export.cmake") |
| 269 | export (PACKAGE gflags) |
| 270 | configure_file (cmake/config.cmake.in "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-config.cmake" @ONLY) |
| 271 | |
| 272 | # ---------------------------------------------------------------------------- |
| 273 | # testing - MUST follow the generation of the build tree config file |
| 274 | include (CTest) |
| 275 | if (BUILD_TESTING) |
| 276 | enable_testing () |
| 277 | add_subdirectory (test) |
| 278 | endif () |