CMakeLists.txt 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. ## CMake configuration file of gflags project
  2. ##
  3. ## This CMakeLists.txt defines some gflags specific configuration variables
  4. ## using the "gflags_define" utility macro. The default values of these variables
  5. ## can be overridden either on the CMake command-line using the -D option of
  6. ## the cmake command or in a super-project which includes the gflags source
  7. ## tree by setting the GFLAGS_<varname> CMake variables before adding the
  8. ## gflags source directory via CMake's "add_subdirectory" command. Only when
  9. ## the non-cached variable GFLAGS_IS_SUBPROJECT has a value equivalent to FALSE,
  10. ## these configuration variables are added to the CMake cache so they can be
  11. ## edited in the CMake GUI. By default, GFLAGS_IS_SUBPROJECT is set to TRUE when
  12. ## the CMAKE_SOURCE_DIR is not identical to the directory of this CMakeLists.txt
  13. ## file, i.e., the top-level directory of the gflags project source tree.
  14. ##
  15. ## When this project is a subproject (GFLAGS_IS_SUBPROJECT is TRUE), the default
  16. ## settings are such that only the static single-threaded library is built without
  17. ## installation of the gflags files. The "gflags::gflags" target is in this case an ALIAS
  18. ## library target for the "gflags_nothreads_static" library target. Targets which
  19. ## depend on the gflags library should link to the "gflags::gflags" library target.
  20. ##
  21. ## Example CMakeLists.txt of user project which requires separate gflags installation:
  22. ## cmake_minimum_required(VERSION 2.8.12 FATAL_ERROR)
  23. ##
  24. ## project(Foo)
  25. ##
  26. ## find_package(gflags REQUIRED)
  27. ##
  28. ## add_executable(foo src/foo.cc)
  29. ## target_link_libraries(foo gflags::gflags)
  30. ##
  31. ## Example CMakeLists.txt of user project which requires separate single-threaded static gflags installation:
  32. ## cmake_minimum_required(VERSION 2.8.12 FATAL_ERROR)
  33. ##
  34. ## project(Foo)
  35. ##
  36. ## find_package(gflags COMPONENTS nothreads_static)
  37. ##
  38. ## add_executable(foo src/foo.cc)
  39. ## target_link_libraries(foo gflags::gflags)
  40. ##
  41. ## Example CMakeLists.txt of super-project which contains gflags source tree:
  42. ## cmake_minimum_required(VERSION 2.8.12 FATAL_ERROR)
  43. ##
  44. ## project(Foo)
  45. ##
  46. ## add_subdirectory(gflags)
  47. ##
  48. ## add_executable(foo src/foo.cc)
  49. ## target_link_libraries(foo gflags::gflags)
  50. ##
  51. ## Variables to configure the source files:
  52. ## - GFLAGS_IS_A_DLL
  53. ## - GFLAGS_NAMESPACE
  54. ## - GFLAGS_ATTRIBUTE_UNUSED
  55. ## - GFLAGS_INTTYPES_FORMAT
  56. ##
  57. ## Variables to configure the build:
  58. ## - GFLAGS_SOVERSION
  59. ## - GFLAGS_BUILD_SHARED_LIBS
  60. ## - GFLAGS_BUILD_STATIC_LIBS
  61. ## - GFLAGS_BUILD_gflags_LIB
  62. ## - GFLAGS_BUILD_gflags_nothreads_LIB
  63. ## - GFLAGS_BUILD_TESTING
  64. ## - GFLAGS_BUILD_PACKAGING
  65. ##
  66. ## Variables to configure the installation:
  67. ## - GFLAGS_INCLUDE_DIR
  68. ## - GFLAGS_LIBRARY_INSTALL_DIR or LIB_INSTALL_DIR or LIB_SUFFIX
  69. ## - GFLAGS_INSTALL_HEADERS
  70. ## - GFLAGS_INSTALL_SHARED_LIBS
  71. ## - GFLAGS_INSTALL_STATIC_LIBS
  72. cmake_minimum_required (VERSION 3.0.2 FATAL_ERROR)
  73. if (POLICY CMP0042)
  74. cmake_policy (SET CMP0042 NEW)
  75. endif ()
  76. if (POLICY CMP0048)
  77. cmake_policy (SET CMP0048 NEW)
  78. endif ()
  79. if (POLICY CMP0063)
  80. cmake_policy (SET CMP0063 NEW)
  81. endif ()
  82. # ----------------------------------------------------------------------------
  83. # includes
  84. include ("${CMAKE_CURRENT_SOURCE_DIR}/cmake/utils.cmake")
  85. # ----------------------------------------------------------------------------
  86. # package information
  87. set (PACKAGE_NAME "gflags")
  88. set (PACKAGE_VERSION "2.2.2")
  89. set (PACKAGE_STRING "${PACKAGE_NAME} ${PACKAGE_VERSION}")
  90. set (PACKAGE_TARNAME "${PACKAGE_NAME}-${PACKAGE_VERSION}")
  91. set (PACKAGE_BUGREPORT "https://github.com/gflags/gflags/issues")
  92. set (PACKAGE_DESCRIPTION "A commandline flags library that allows for distributed flags.")
  93. set (PACKAGE_URL "http://gflags.github.io/gflags")
  94. project (${PACKAGE_NAME} VERSION ${PACKAGE_VERSION} LANGUAGES CXX)
  95. if (CMAKE_VERSION VERSION_LESS 3.4)
  96. # C language still needed because the following required CMake modules
  97. # (or their dependencies, respectively) are not correctly handling
  98. # the case where only CXX is enabled
  99. # - CheckTypeSize.cmake (fixed in CMake 3.1, cf. https://cmake.org/Bug/view.php?id=14056)
  100. # - FindThreads.cmake (fixed in CMake 3.4, cf. https://cmake.org/Bug/view.php?id=14905)
  101. enable_language (C)
  102. endif ()
  103. version_numbers (
  104. ${PACKAGE_VERSION}
  105. PACKAGE_VERSION_MAJOR
  106. PACKAGE_VERSION_MINOR
  107. PACKAGE_VERSION_PATCH
  108. )
  109. # shared library ABI version number, can be overridden by package maintainers
  110. # using -DGFLAGS_SOVERSION=XXX on the command-line
  111. if (GFLAGS_SOVERSION)
  112. set (PACKAGE_SOVERSION "${GFLAGS_SOVERSION}")
  113. else ()
  114. # TODO: Change default SOVERSION back to PACKAGE_VERSION_MAJOR with the
  115. # next increase of major version number (i.e., 3.0.0 -> SOVERSION 3)
  116. # The <major>.<minor> SOVERSION should be used for the 2.x releases
  117. # versions only which temporarily broke the API by changing the default
  118. # namespace from "google" to "gflags".
  119. set (PACKAGE_SOVERSION "${PACKAGE_VERSION_MAJOR}.${PACKAGE_VERSION_MINOR}")
  120. endif ()
  121. # when gflags is included as subproject (e.g., as Git submodule/subtree) in the source
  122. # tree of a project that uses it, no variables should be added to the CMake cache;
  123. # users may set the non-cached variable GFLAGS_IS_SUBPROJECT before add_subdirectory(gflags)
  124. if (NOT DEFINED GFLAGS_IS_SUBPROJECT)
  125. if ("^${CMAKE_SOURCE_DIR}$" STREQUAL "^${PROJECT_SOURCE_DIR}$")
  126. set (GFLAGS_IS_SUBPROJECT FALSE)
  127. else ()
  128. set (GFLAGS_IS_SUBPROJECT TRUE)
  129. endif ()
  130. endif ()
  131. # prefix for package variables in CMake configuration file
  132. string (TOUPPER "${PACKAGE_NAME}" PACKAGE_PREFIX)
  133. # convert file path on Windows with back slashes to path with forward slashes
  134. # otherwise this causes an issue with the cmake_install.cmake script
  135. file (TO_CMAKE_PATH "${CMAKE_INSTALL_PREFIX}" CMAKE_INSTALL_PREFIX)
  136. # ----------------------------------------------------------------------------
  137. # options
  138. # maintain binary backwards compatibility with gflags library version <= 2.0,
  139. # but at the same time enable the use of the preferred new "gflags" namespace
  140. gflags_define (STRING NAMESPACE "Name(s) of library namespace (separate multiple options by semicolon)" "google;${PACKAGE_NAME}" "${PACKAGE_NAME}")
  141. gflags_property (NAMESPACE ADVANCED TRUE)
  142. set (GFLAGS_NAMESPACE_SECONDARY "${NAMESPACE}")
  143. list (REMOVE_DUPLICATES GFLAGS_NAMESPACE_SECONDARY)
  144. if (NOT GFLAGS_NAMESPACE_SECONDARY)
  145. message (FATAL_ERROR "GFLAGS_NAMESPACE must be set to one (or more) valid C++ namespace identifier(s separated by semicolon \";\").")
  146. endif ()
  147. foreach (ns IN LISTS GFLAGS_NAMESPACE_SECONDARY)
  148. if (NOT ns MATCHES "^[a-zA-Z][a-zA-Z0-9_]*$")
  149. message (FATAL_ERROR "GFLAGS_NAMESPACE contains invalid namespace identifier: ${ns}")
  150. endif ()
  151. endforeach ()
  152. list (GET GFLAGS_NAMESPACE_SECONDARY 0 GFLAGS_NAMESPACE)
  153. list (REMOVE_AT GFLAGS_NAMESPACE_SECONDARY 0)
  154. # cached build options when gflags is not a subproject, otherwise non-cached CMake variables
  155. # usage: gflags_define(BOOL <name> <doc> <default> [<subproject default>])
  156. gflags_define (BOOL BUILD_SHARED_LIBS "Request build of shared libraries." OFF OFF)
  157. gflags_define (BOOL BUILD_STATIC_LIBS "Request build of static libraries (default if BUILD_SHARED_LIBS is OFF)." OFF ON)
  158. gflags_define (BOOL BUILD_gflags_LIB "Request build of the multi-threaded gflags library." ON OFF)
  159. gflags_define (BOOL BUILD_gflags_nothreads_LIB "Request build of the single-threaded gflags library." ON ON)
  160. gflags_define (BOOL BUILD_PACKAGING "Enable build of distribution packages using CPack." OFF OFF)
  161. gflags_define (BOOL BUILD_TESTING "Enable build of the unit tests and their execution using CTest." OFF OFF)
  162. gflags_define (BOOL INSTALL_HEADERS "Request installation of headers and other development files." ON OFF)
  163. gflags_define (BOOL INSTALL_SHARED_LIBS "Request installation of shared libraries." ON ON)
  164. gflags_define (BOOL INSTALL_STATIC_LIBS "Request installation of static libraries." ON OFF)
  165. gflags_define (BOOL REGISTER_BUILD_DIR "Request entry of build directory in CMake's package registry." OFF OFF)
  166. gflags_define (BOOL REGISTER_INSTALL_PREFIX "Request entry of installed package in CMake's package registry." ON OFF)
  167. gflags_property (BUILD_STATIC_LIBS ADVANCED TRUE)
  168. gflags_property (INSTALL_HEADERS ADVANCED TRUE)
  169. gflags_property (INSTALL_SHARED_LIBS ADVANCED TRUE)
  170. gflags_property (INSTALL_STATIC_LIBS ADVANCED TRUE)
  171. if (NOT GFLAGS_IS_SUBPROJECT)
  172. foreach (varname IN ITEMS CMAKE_INSTALL_PREFIX)
  173. gflags_property (${varname} ADVANCED FALSE)
  174. endforeach ()
  175. foreach (varname IN ITEMS CMAKE_CONFIGURATION_TYPES CMAKE_OSX_ARCHITECTURES CMAKE_OSX_DEPLOYMENT_TARGET CMAKE_OSX_SYSROOT)
  176. gflags_property (${varname} ADVANCED TRUE)
  177. endforeach ()
  178. if (NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CXX_FLAGS)
  179. gflags_set (CMAKE_BUILD_TYPE Release)
  180. endif ()
  181. if (CMAKE_CONFIGURATION_TYPES)
  182. gflags_property (CMAKE_BUILD_TYPE STRINGS "${CMAKE_CONFIGURATION_TYPES}")
  183. endif ()
  184. endif () # NOT GFLAGS_IS_SUBPROJECT
  185. if (NOT BUILD_SHARED_LIBS AND NOT BUILD_STATIC_LIBS)
  186. set (BUILD_STATIC_LIBS ON)
  187. endif ()
  188. if (NOT BUILD_gflags_LIB AND NOT BUILD_gflags_nothreads_LIB)
  189. message (FATAL_ERROR "At least one of [GFLAGS_]BUILD_gflags_LIB and [GFLAGS_]BUILD_gflags_nothreads_LIB must be ON.")
  190. endif ()
  191. gflags_define (STRING INCLUDE_DIR "Name of include directory of installed header files relative to CMAKE_INSTALL_PREFIX/include/" "${PACKAGE_NAME}")
  192. gflags_property (INCLUDE_DIR ADVANCED TRUE)
  193. file (TO_CMAKE_PATH "${INCLUDE_DIR}" INCLUDE_DIR)
  194. if (IS_ABSOLUTE INCLUDE_DIR)
  195. message (FATAL_ERROR "[GFLAGS_]INCLUDE_DIR must be a path relative to CMAKE_INSTALL_PREFIX/include/")
  196. endif ()
  197. if (INCLUDE_DIR MATCHES "^\\.\\.[/\\]")
  198. message (FATAL_ERROR "[GFLAGS_]INCLUDE_DIR must not start with parent directory reference (../)")
  199. endif ()
  200. set (GFLAGS_INCLUDE_DIR "${INCLUDE_DIR}")
  201. # ----------------------------------------------------------------------------
  202. # system checks
  203. include (CheckTypeSize)
  204. include (CheckIncludeFileCXX)
  205. include (CheckCXXSymbolExists)
  206. if (WIN32 AND NOT CYGWIN)
  207. set (OS_WINDOWS 1)
  208. else ()
  209. set (OS_WINDOWS 0)
  210. endif ()
  211. if (MSVC)
  212. set (HAVE_SYS_TYPES_H 1)
  213. set (HAVE_STDDEF_H 1) # used by CheckTypeSize module
  214. set (HAVE_UNISTD_H 0)
  215. set (HAVE_SYS_STAT_H 1)
  216. set (HAVE_SHLWAPI_H 1)
  217. if (MSVC_VERSION VERSION_LESS 1600)
  218. check_include_file_cxx ("stdint.h" HAVE_STDINT_H)
  219. bool_to_int (HAVE_STDINT_H) # used in #if directive
  220. else ()
  221. set (HAVE_STDINT_H 1)
  222. endif ()
  223. if (MSVC_VERSION VERSION_LESS 1800)
  224. check_include_file_cxx ("inttypes.h" HAVE_INTTYPES_H)
  225. bool_to_int (HAVE_INTTYPES_H) # used in #if directive
  226. else ()
  227. set (HAVE_INTTYPES_H 1)
  228. endif ()
  229. else ()
  230. foreach (fname IN ITEMS unistd stdint inttypes sys/types sys/stat fnmatch)
  231. string (TOUPPER "${fname}" FNAME)
  232. string (REPLACE "/" "_" FNAME "${FNAME}")
  233. if (NOT HAVE_${FNAME}_H)
  234. check_include_file_cxx ("${fname}.h" HAVE_${FNAME}_H)
  235. endif ()
  236. endforeach ()
  237. if (NOT HAVE_FNMATCH_H AND OS_WINDOWS)
  238. check_include_file_cxx ("shlwapi.h" HAVE_SHLWAPI_H)
  239. endif ()
  240. # the following are used in #if directives not #ifdef
  241. bool_to_int (HAVE_STDINT_H)
  242. bool_to_int (HAVE_SYS_TYPES_H)
  243. bool_to_int (HAVE_INTTYPES_H)
  244. endif ()
  245. gflags_define (STRING INTTYPES_FORMAT "Format of integer types: \"C99\" (uint32_t), \"BSD\" (u_int32_t), \"VC7\" (__int32)" "")
  246. gflags_property (INTTYPES_FORMAT STRINGS "C99;BSD;VC7")
  247. gflags_property (INTTYPES_FORMAT ADVANCED TRUE)
  248. if (NOT INTTYPES_FORMAT)
  249. set (TYPES uint32_t u_int32_t)
  250. if (MSVC)
  251. list (INSERT TYPES 0 __int32)
  252. endif ()
  253. foreach (type IN LISTS TYPES)
  254. check_type_size (${type} ${type} LANGUAGE CXX)
  255. if (HAVE_${type})
  256. break ()
  257. endif ()
  258. endforeach ()
  259. if (HAVE_uint32_t)
  260. gflags_set (INTTYPES_FORMAT C99)
  261. elseif (HAVE_u_int32_t)
  262. gflags_set (INTTYPES_FORMAT BSD)
  263. elseif (HAVE___int32)
  264. gflags_set (INTTYPES_FORMAT VC7)
  265. else ()
  266. gflags_property (INTTYPES_FORMAT ADVANCED FALSE)
  267. message (FATAL_ERROR "Do not know how to define a 32-bit integer quantity on your system!"
  268. " Neither uint32_t, u_int32_t, nor __int32 seem to be available."
  269. " Set [GFLAGS_]INTTYPES_FORMAT to either C99, BSD, or VC7 and try again.")
  270. endif ()
  271. endif ()
  272. # use of special characters in strings to circumvent bug #0008226
  273. if ("^${INTTYPES_FORMAT}$" STREQUAL "^WIN$")
  274. gflags_set (INTTYPES_FORMAT VC7)
  275. endif ()
  276. if (NOT INTTYPES_FORMAT MATCHES "^(C99|BSD|VC7)$")
  277. message (FATAL_ERROR "Invalid value for [GFLAGS_]INTTYPES_FORMAT! Choose one of \"C99\", \"BSD\", or \"VC7\"")
  278. endif ()
  279. set (GFLAGS_INTTYPES_FORMAT "${INTTYPES_FORMAT}")
  280. set (GFLAGS_INTTYPES_FORMAT_C99 0)
  281. set (GFLAGS_INTTYPES_FORMAT_BSD 0)
  282. set (GFLAGS_INTTYPES_FORMAT_VC7 0)
  283. set ("GFLAGS_INTTYPES_FORMAT_${INTTYPES_FORMAT}" 1)
  284. if (MSVC)
  285. set (HAVE_strtoll 0)
  286. set (HAVE_strtoq 0)
  287. else ()
  288. check_cxx_symbol_exists (strtoll stdlib.h HAVE_STRTOLL)
  289. if (NOT HAVE_STRTOLL)
  290. check_cxx_symbol_exists (strtoq stdlib.h HAVE_STRTOQ)
  291. endif ()
  292. endif ()
  293. if (BUILD_gflags_LIB)
  294. set (CMAKE_THREAD_PREFER_PTHREAD TRUE)
  295. find_package (Threads)
  296. if (Threads_FOUND AND CMAKE_USE_PTHREADS_INIT)
  297. set (HAVE_PTHREAD 1)
  298. check_type_size (pthread_rwlock_t RWLOCK LANGUAGE CXX)
  299. else ()
  300. set (HAVE_PTHREAD 0)
  301. endif ()
  302. if (UNIX AND NOT HAVE_PTHREAD)
  303. if (CMAKE_HAVE_PTHREAD_H)
  304. set (what "library")
  305. else ()
  306. set (what ".h file")
  307. endif ()
  308. message (FATAL_ERROR "Could not find pthread${what}. Check the log file"
  309. "\n\t${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log"
  310. "\nor disable the build of the multi-threaded gflags library (BUILD_gflags_LIB=OFF).")
  311. endif ()
  312. else ()
  313. set (HAVE_PTHREAD 0)
  314. endif ()
  315. # ----------------------------------------------------------------------------
  316. # source files - excluding root subdirectory and/or .in suffix
  317. set (PUBLIC_HDRS
  318. "gflags.h"
  319. "gflags_declare.h"
  320. "gflags_completions.h"
  321. )
  322. if (GFLAGS_NAMESPACE_SECONDARY)
  323. set (INCLUDE_GFLAGS_NS_H "// Import gflags library symbols into alternative/deprecated namespace(s)")
  324. foreach (ns IN LISTS GFLAGS_NAMESPACE_SECONDARY)
  325. string (TOUPPER "${ns}" NS)
  326. set (gflags_ns_h "${PROJECT_BINARY_DIR}/include/${GFLAGS_INCLUDE_DIR}/gflags_${ns}.h")
  327. configure_file ("${PROJECT_SOURCE_DIR}/src/gflags_ns.h.in" "${gflags_ns_h}" @ONLY)
  328. list (APPEND PUBLIC_HDRS "${gflags_ns_h}")
  329. set (INCLUDE_GFLAGS_NS_H "${INCLUDE_GFLAGS_NS_H}\n#include \"gflags_${ns}.h\"")
  330. endforeach ()
  331. else ()
  332. set (INCLUDE_GFLAGS_NS_H)
  333. endif ()
  334. set (PRIVATE_HDRS
  335. "defines.h"
  336. "config.h"
  337. "util.h"
  338. "mutex.h"
  339. )
  340. set (GFLAGS_SRCS
  341. "gflags.cc"
  342. "gflags_reporting.cc"
  343. "gflags_completions.cc"
  344. )
  345. if (OS_WINDOWS)
  346. list (APPEND PRIVATE_HDRS "windows_port.h")
  347. list (APPEND GFLAGS_SRCS "windows_port.cc")
  348. endif ()
  349. # ----------------------------------------------------------------------------
  350. # configure source files
  351. if (NOT DEFINED GFLAGS_ATTRIBUTE_UNUSED)
  352. if (CMAKE_COMPILER_IS_GNUCXX)
  353. set (GFLAGS_ATTRIBUTE_UNUSED "__attribute((unused))")
  354. else ()
  355. set (GFLAGS_ATTRIBUTE_UNUSED)
  356. endif ()
  357. endif ()
  358. # whenever we build a shared library (DLL on Windows), configure the public
  359. # headers of the API for use of this shared library rather than the optionally
  360. # also build statically linked library; users can override GFLAGS_DLL_DECL
  361. # in particular, this done by setting the INTERFACE_COMPILE_DEFINITIONS of
  362. # static libraries to include an empty definition for GFLAGS_DLL_DECL
  363. if (NOT DEFINED GFLAGS_IS_A_DLL)
  364. if (BUILD_SHARED_LIBS)
  365. set (GFLAGS_IS_A_DLL 1)
  366. else ()
  367. set (GFLAGS_IS_A_DLL 0)
  368. endif ()
  369. endif ()
  370. configure_headers (PUBLIC_HDRS ${PUBLIC_HDRS})
  371. configure_sources (PRIVATE_HDRS ${PRIVATE_HDRS})
  372. configure_sources (GFLAGS_SRCS ${GFLAGS_SRCS})
  373. # ----------------------------------------------------------------------------
  374. # output directories
  375. if (NOT GFLAGS_IS_SUBPROJECT)
  376. set (CMAKE_RUNTIME_OUTPUT_DIRECTORY "bin")
  377. set (CMAKE_LIBRARY_OUTPUT_DIRECTORY "lib")
  378. set (CMAKE_ARCHIVE_OUTPUT_DIRECTORY "lib")
  379. endif ()
  380. # Set postfixes for generated libraries based on buildtype.
  381. set(CMAKE_RELEASE_POSTFIX "")
  382. set(CMAKE_DEBUG_POSTFIX "_debug")
  383. # ----------------------------------------------------------------------------
  384. # installation directories
  385. if (OS_WINDOWS AND NOT MINGW)
  386. set (RUNTIME_INSTALL_DIR "bin")
  387. set (LIBRARY_INSTALL_DIR "lib")
  388. set (INCLUDE_INSTALL_DIR "include")
  389. set (CONFIG_INSTALL_DIR "lib/cmake/${PACKAGE_NAME}")
  390. set (PKGCONFIG_INSTALL_DIR)
  391. else ()
  392. set (RUNTIME_INSTALL_DIR bin)
  393. # The LIB_INSTALL_DIR and LIB_SUFFIX variables are used by the Fedora
  394. # package maintainers. Also package maintainers of other distribution
  395. # packages need to be able to specify the name of the library directory.
  396. if (NOT GFLAGS_LIBRARY_INSTALL_DIR AND LIB_INSTALL_DIR)
  397. set (GFLAGS_LIBRARY_INSTALL_DIR "${LIB_INSTALL_DIR}")
  398. endif ()
  399. gflags_define (PATH LIBRARY_INSTALL_DIR "Directory of installed libraries, e.g., \"lib64\"" "lib${LIB_SUFFIX}")
  400. gflags_property (LIBRARY_INSTALL_DIR ADVANCED TRUE)
  401. set (INCLUDE_INSTALL_DIR include)
  402. set (CONFIG_INSTALL_DIR ${LIBRARY_INSTALL_DIR}/cmake/${PACKAGE_NAME})
  403. set (PKGCONFIG_INSTALL_DIR ${LIBRARY_INSTALL_DIR}/pkgconfig)
  404. endif ()
  405. # ----------------------------------------------------------------------------
  406. # add library targets
  407. set (TARGETS)
  408. # static vs. shared
  409. foreach (TYPE IN ITEMS STATIC SHARED)
  410. if (BUILD_${TYPE}_LIBS)
  411. string (TOLOWER "${TYPE}" type)
  412. # whether or not targets are a DLL
  413. if (OS_WINDOWS AND "^${TYPE}$" STREQUAL "^SHARED$")
  414. set (GFLAGS_IS_A_DLL 1)
  415. else ()
  416. set (GFLAGS_IS_A_DLL 0)
  417. endif ()
  418. # filename suffix for static libraries on Windows for MSVC toolchain only
  419. if (OS_WINDOWS AND NOT MINGW AND "^${TYPE}$" STREQUAL "^STATIC$")
  420. set (type_suffix "_${type}")
  421. else ()
  422. set (type_suffix "")
  423. endif ()
  424. # multi-threaded vs. single-threaded
  425. foreach (opts IN ITEMS "" _nothreads)
  426. if (BUILD_gflags${opts}_LIB)
  427. set (target_name "gflags${opts}_${type}")
  428. add_library (${target_name} ${TYPE} ${GFLAGS_SRCS} ${PRIVATE_HDRS} ${PUBLIC_HDRS})
  429. set_target_properties (${target_name} PROPERTIES
  430. OUTPUT_NAME "gflags${opts}${type_suffix}"
  431. VERSION "${PACKAGE_VERSION}"
  432. SOVERSION "${PACKAGE_SOVERSION}"
  433. )
  434. set (include_dirs "$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/include>")
  435. if (INSTALL_HEADERS)
  436. list (APPEND include_dirs "$<INSTALL_INTERFACE:${INCLUDE_INSTALL_DIR}>")
  437. endif ()
  438. target_include_directories (${target_name}
  439. PUBLIC "${include_dirs}"
  440. PRIVATE "${PROJECT_SOURCE_DIR}/src;${PROJECT_BINARY_DIR}/include/${GFLAGS_INCLUDE_DIR}"
  441. )
  442. target_compile_definitions (${target_name} PUBLIC GFLAGS_IS_A_DLL=${GFLAGS_IS_A_DLL})
  443. if (opts MATCHES "nothreads")
  444. target_compile_definitions (${target_name} PRIVATE NO_THREADS)
  445. elseif (CMAKE_USE_PTHREADS_INIT)
  446. target_link_libraries (${target_name} ${CMAKE_THREAD_LIBS_INIT})
  447. endif ()
  448. if (HAVE_SHLWAPI_H)
  449. target_link_libraries (${target_name} shlwapi.lib)
  450. endif ()
  451. list (APPEND TARGETS ${target_name})
  452. # add convenience make target for build of both shared and static libraries
  453. if (NOT GFLAGS_IS_SUBPROJECT)
  454. if (NOT TARGET gflags${opts})
  455. add_custom_target (gflags${opts})
  456. endif ()
  457. add_dependencies (gflags${opts} ${target_name})
  458. endif ()
  459. endif ()
  460. endforeach ()
  461. endif ()
  462. endforeach ()
  463. # add ALIAS target for use in super-project, prefer static over shared, single-threaded over multi-threaded
  464. if (GFLAGS_IS_SUBPROJECT)
  465. foreach (type IN ITEMS static shared)
  466. foreach (opts IN ITEMS "_nothreads" "")
  467. if (TARGET gflags${opts}_${type})
  468. # Define "gflags" alias for super-projects treating targets of this library as part of their own project
  469. # (also for backwards compatibility with gflags 2.2.1 which only defined this alias)
  470. add_library (gflags ALIAS gflags${opts}_${type})
  471. # Define "gflags::gflags" alias for projects that support both find_package(gflags) and add_subdirectory(gflags)
  472. add_library (gflags::gflags ALIAS gflags${opts}_${type})
  473. break ()
  474. endif ()
  475. endforeach ()
  476. if (TARGET gflags::gflags)
  477. break ()
  478. endif ()
  479. endforeach ()
  480. endif ()
  481. # ----------------------------------------------------------------------------
  482. # installation rules
  483. set (EXPORT_NAME ${PACKAGE_NAME}-targets)
  484. file (RELATIVE_PATH INSTALL_PREFIX_REL2CONFIG_DIR "${CMAKE_INSTALL_PREFIX}/${CONFIG_INSTALL_DIR}" "${CMAKE_INSTALL_PREFIX}")
  485. configure_file (cmake/config.cmake.in "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-config-install.cmake" @ONLY)
  486. configure_file (cmake/version.cmake.in "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-config-version.cmake" @ONLY)
  487. if (BUILD_SHARED_LIBS AND INSTALL_SHARED_LIBS)
  488. foreach (opts IN ITEMS "" _nothreads)
  489. if (BUILD_gflags${opts}_LIB)
  490. install (TARGETS gflags${opts}_shared
  491. EXPORT ${EXPORT_NAME}
  492. RUNTIME DESTINATION ${RUNTIME_INSTALL_DIR}
  493. LIBRARY DESTINATION ${LIBRARY_INSTALL_DIR}
  494. ARCHIVE DESTINATION ${LIBRARY_INSTALL_DIR}
  495. )
  496. endif ()
  497. endforeach ()
  498. endif ()
  499. if (BUILD_STATIC_LIBS AND INSTALL_STATIC_LIBS)
  500. foreach (opts IN ITEMS "" _nothreads)
  501. if (BUILD_gflags${opts}_LIB)
  502. install (TARGETS gflags${opts}_static
  503. EXPORT ${EXPORT_NAME}
  504. RUNTIME DESTINATION ${RUNTIME_INSTALL_DIR}
  505. LIBRARY DESTINATION ${LIBRARY_INSTALL_DIR}
  506. ARCHIVE DESTINATION ${LIBRARY_INSTALL_DIR}
  507. )
  508. endif ()
  509. endforeach ()
  510. endif ()
  511. if (INSTALL_HEADERS)
  512. install (FILES ${PUBLIC_HDRS} DESTINATION ${INCLUDE_INSTALL_DIR}/${GFLAGS_INCLUDE_DIR})
  513. install (
  514. FILES "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-config-install.cmake"
  515. RENAME ${PACKAGE_NAME}-config.cmake
  516. DESTINATION ${CONFIG_INSTALL_DIR}
  517. )
  518. install (
  519. FILES "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-config-version.cmake"
  520. DESTINATION ${CONFIG_INSTALL_DIR}
  521. )
  522. install (
  523. EXPORT ${EXPORT_NAME}
  524. NAMESPACE ${PACKAGE_NAME}::
  525. DESTINATION ${CONFIG_INSTALL_DIR}
  526. )
  527. install (
  528. EXPORT ${EXPORT_NAME}
  529. FILE ${PACKAGE_NAME}-nonamespace-targets.cmake
  530. DESTINATION ${CONFIG_INSTALL_DIR}
  531. )
  532. if (UNIX)
  533. install (PROGRAMS src/gflags_completions.sh DESTINATION ${RUNTIME_INSTALL_DIR})
  534. endif ()
  535. if (PKGCONFIG_INSTALL_DIR)
  536. configure_file ("cmake/package.pc.in" "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}.pc" @ONLY)
  537. install (FILES "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}.pc" DESTINATION "${PKGCONFIG_INSTALL_DIR}")
  538. endif ()
  539. endif ()
  540. # ----------------------------------------------------------------------------
  541. # support direct use of build tree
  542. set (INSTALL_PREFIX_REL2CONFIG_DIR .)
  543. export (
  544. TARGETS ${TARGETS}
  545. NAMESPACE ${PACKAGE_NAME}::
  546. FILE "${PROJECT_BINARY_DIR}/${EXPORT_NAME}.cmake"
  547. )
  548. export (
  549. TARGETS ${TARGETS}
  550. FILE "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-nonamespace-targets.cmake"
  551. )
  552. if (REGISTER_BUILD_DIR)
  553. export (PACKAGE ${PACKAGE_NAME})
  554. endif ()
  555. if (REGISTER_INSTALL_PREFIX)
  556. register_gflags_package(${CONFIG_INSTALL_DIR})
  557. endif ()
  558. configure_file (cmake/config.cmake.in "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-config.cmake" @ONLY)
  559. # ----------------------------------------------------------------------------
  560. # testing - MUST follow the generation of the build tree config file
  561. if (BUILD_TESTING)
  562. include (CTest)
  563. add_subdirectory (test)
  564. endif ()
  565. # ----------------------------------------------------------------------------
  566. # packaging
  567. if (BUILD_PACKAGING)
  568. if (NOT BUILD_SHARED_LIBS AND NOT INSTALL_HEADERS)
  569. message (WARNING "Package will contain static libraries without headers!"
  570. "\nRecommended options for generation of runtime package:"
  571. "\n BUILD_SHARED_LIBS=ON"
  572. "\n BUILD_STATIC_LIBS=OFF"
  573. "\n INSTALL_HEADERS=OFF"
  574. "\n INSTALL_SHARED_LIBS=ON"
  575. "\nRecommended options for generation of development package:"
  576. "\n BUILD_SHARED_LIBS=ON"
  577. "\n BUILD_STATIC_LIBS=ON"
  578. "\n INSTALL_HEADERS=ON"
  579. "\n INSTALL_SHARED_LIBS=ON"
  580. "\n INSTALL_STATIC_LIBS=ON"
  581. )
  582. endif ()
  583. # default package generators
  584. if (APPLE)
  585. set (PACKAGE_GENERATOR "PackageMaker")
  586. set (PACKAGE_SOURCE_GENERATOR "TGZ;ZIP")
  587. elseif (UNIX)
  588. set (PACKAGE_GENERATOR "DEB;RPM")
  589. set (PACKAGE_SOURCE_GENERATOR "TGZ;ZIP")
  590. else ()
  591. set (PACKAGE_GENERATOR "ZIP")
  592. set (PACKAGE_SOURCE_GENERATOR "ZIP")
  593. endif ()
  594. # used package generators
  595. set (CPACK_GENERATOR "${PACKAGE_GENERATOR}" CACHE STRING "List of binary package generators (CPack).")
  596. set (CPACK_SOURCE_GENERATOR "${PACKAGE_SOURCE_GENERATOR}" CACHE STRING "List of source package generators (CPack).")
  597. mark_as_advanced (CPACK_GENERATOR CPACK_SOURCE_GENERATOR)
  598. # some package generators (e.g., PackageMaker) do not allow .md extension
  599. configure_file ("${CMAKE_CURRENT_LIST_DIR}/README.md" "${CMAKE_CURRENT_BINARY_DIR}/README.txt" COPYONLY)
  600. # common package information
  601. set (CPACK_PACKAGE_VENDOR "Andreas Schuh")
  602. set (CPACK_PACKAGE_CONTACT "google-gflags@googlegroups.com")
  603. set (CPACK_PACKAGE_NAME "${PACKAGE_NAME}")
  604. set (CPACK_PACKAGE_VERSION "${PACKAGE_VERSION}")
  605. set (CPACK_PACKAGE_VERSION_MAJOR "${PACKAGE_VERSION_MAJOR}")
  606. set (CPACK_PACKAGE_VERSION_MINOR "${PACKAGE_VERSION_MINOR}")
  607. set (CPACK_PACKAGE_VERSION_PATCH "${PACKAGE_VERSION_PATCH}")
  608. set (CPACK_PACKAGE_DESCRIPTION_SUMMARY "${PACKAGE_DESCRIPTION}")
  609. set (CPACK_RESOURCE_FILE_WELCOME "${CMAKE_CURRENT_BINARY_DIR}/README.txt")
  610. set (CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_LIST_DIR}/COPYING.txt")
  611. set (CPACK_PACKAGE_DESCRIPTION_FILE "${CMAKE_CURRENT_BINARY_DIR}/README.txt")
  612. set (CPACK_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}")
  613. set (CPACK_OUTPUT_FILE_PREFIX packages)
  614. set (CPACK_PACKAGE_RELOCATABLE TRUE)
  615. set (CPACK_MONOLITHIC_INSTALL TRUE)
  616. # RPM package information -- used in cmake/package.cmake.in also for DEB
  617. set (CPACK_RPM_PACKAGE_GROUP "Development/Libraries")
  618. set (CPACK_RPM_PACKAGE_LICENSE "BSD")
  619. set (CPACK_RPM_PACKAGE_URL "${PACKAGE_URL}")
  620. set (CPACK_RPM_CHANGELOG_FILE "${CMAKE_CURRENT_LIST_DIR}/ChangeLog.txt")
  621. if (INSTALL_HEADERS)
  622. set (CPACK_RESOURCE_FILE_README "${CMAKE_CURRENT_LIST_DIR}/doc/index.html")
  623. else ()
  624. set (CPACK_RESOURCE_FILE_README "${CMAKE_CURRENT_LIST_DIR}/cmake/README_runtime.txt")
  625. endif ()
  626. # system/architecture
  627. if (WINDOWS)
  628. if (CMAKE_CL_64)
  629. set (CPACK_SYSTEM_NAME "win64")
  630. else ()
  631. set (CPACK_SYSTEM_NAME "win32")
  632. endif ()
  633. set (CPACK_PACKAGE_ARCHITECTURE)
  634. elseif (APPLE)
  635. set (CPACK_PACKAGE_ARCHITECTURE darwin)
  636. else ()
  637. string (TOLOWER "${CMAKE_SYSTEM_NAME}" CPACK_SYSTEM_NAME)
  638. if (CMAKE_CXX_FLAGS MATCHES "-m32")
  639. set (CPACK_PACKAGE_ARCHITECTURE i386)
  640. else ()
  641. execute_process (
  642. COMMAND dpkg --print-architecture
  643. RESULT_VARIABLE RV
  644. OUTPUT_VARIABLE CPACK_PACKAGE_ARCHITECTURE
  645. )
  646. if (RV EQUAL 0)
  647. string (STRIP "${CPACK_PACKAGE_ARCHITECTURE}" CPACK_PACKAGE_ARCHITECTURE)
  648. else ()
  649. execute_process (COMMAND uname -m OUTPUT_VARIABLE CPACK_PACKAGE_ARCHITECTURE)
  650. if (CPACK_PACKAGE_ARCHITECTURE MATCHES "x86_64")
  651. set (CPACK_PACKAGE_ARCHITECTURE amd64)
  652. else ()
  653. set (CPACK_PACKAGE_ARCHITECTURE i386)
  654. endif ()
  655. endif ()
  656. endif ()
  657. endif ()
  658. # source package settings
  659. set (CPACK_SOURCE_TOPLEVEL_TAG "source")
  660. set (CPACK_SOURCE_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}")
  661. set (CPACK_SOURCE_IGNORE_FILES "/\\\\.git/;\\\\.swp$;\\\\.#;/#;\\\\.*~;cscope\\\\.*;/[Bb]uild[.+-_a-zA-Z0-9]*/")
  662. # default binary package settings
  663. set (CPACK_INCLUDE_TOPLEVEL_DIRECTORY TRUE)
  664. set (CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}-${CPACK_SYSTEM_NAME}")
  665. if (CPACK_PACKAGE_ARCHITECTURE)
  666. set (CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_FILE_NAME}-${CPACK_PACKAGE_ARCHITECTURE}")
  667. endif ()
  668. # generator specific configuration file
  669. #
  670. # allow package maintainers to use their own configuration file
  671. # $ cmake -DCPACK_PROJECT_CONFIG_FILE:FILE=/path/to/package/config
  672. if (NOT CPACK_PROJECT_CONFIG_FILE)
  673. configure_file (
  674. "${CMAKE_CURRENT_LIST_DIR}/cmake/package.cmake.in"
  675. "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-package.cmake" @ONLY
  676. )
  677. set (CPACK_PROJECT_CONFIG_FILE "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-package.cmake")
  678. endif ()
  679. include (CPack)
  680. endif () # BUILD_PACKAGING
  681. if (NOT GFLAGS_IS_SUBPROJECT AND NOT TARGET uninstall)
  682. configure_file (
  683. "${CMAKE_CURRENT_SOURCE_DIR}/cmake/cmake_uninstall.cmake.in"
  684. "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake" @ONLY
  685. )
  686. add_custom_target(uninstall COMMAND ${CMAKE_COMMAND} -P "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake")
  687. endif ()