blob: 14cdb90b975846210fad64f8a34986a0f94f3c3d [file] [log] [blame]
Andreas Schuh91e16ee2016-03-01 20:03:43 +00001## 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" target is in this case an ALIAS
Andreas Schuh7a21c162016-03-01 21:04:10 +000018## library target for the "gflags_nothreads_static" library target. Targets which
Andreas Schuh91e16ee2016-03-01 20:03:43 +000019## depend on the gflags library should link to the "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)
30##
Andreas Schuh7a21c162016-03-01 21:04:10 +000031## 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)
40##
Andreas Schuh91e16ee2016-03-01 20:03:43 +000041## 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)
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
Andreas Schuh58345b12015-04-03 16:12:38 +010073cmake_minimum_required (VERSION 2.8.12 FATAL_ERROR)
unknown3d142b82014-03-19 15:27:08 +010074
Andreas Schuh7b5b0802015-03-24 14:41:10 +000075if (POLICY CMP0042)
76 cmake_policy (SET CMP0042 NEW)
77endif ()
78
unknown3d142b82014-03-19 15:27:08 +010079# ----------------------------------------------------------------------------
80# includes
81set (CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
unknown3d142b82014-03-19 15:27:08 +010082include (utils)
83
84# ----------------------------------------------------------------------------
85# package information
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +000086set (PACKAGE_NAME "gflags")
Andreas Schuh4b771462015-03-24 14:44:57 +000087set (PACKAGE_VERSION "2.2.0")
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +000088set (PACKAGE_STRING "${PACKAGE_NAME} ${PACKAGE_VERSION}")
89set (PACKAGE_TARNAME "${PACKAGE_NAME}-${PACKAGE_VERSION}")
Andreas Schuhabe4b102016-03-01 16:01:45 +000090set (PACKAGE_BUGREPORT "https://github.com/gflags/gflags/issues")
unknown3d142b82014-03-19 15:27:08 +010091
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +000092project (${PACKAGE_NAME} CXX)
Andreas Schuh51f1db72015-03-24 15:36:26 +000093if (CMAKE_VERSION VERSION_LESS 100)
94 # C language still needed because the following required CMake modules
95 # (or their dependencies, respectively) are not correctly handling
96 # the case where only CXX is enabled.
97 # - CheckTypeSize.cmake (fixed in CMake 3.1, cf. http://www.cmake.org/Bug/view.php?id=14056)
98 # - FindThreads.cmake (--> CheckIncludeFiles.cmake <--)
99 enable_language (C)
100endif ()
unknown3d142b82014-03-19 15:27:08 +0100101
102version_numbers (
103 ${PACKAGE_VERSION}
104 PACKAGE_VERSION_MAJOR
105 PACKAGE_VERSION_MINOR
106 PACKAGE_VERSION_PATCH
107)
108
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000109# shared library ABI version number, can be overridden by package maintainers
110# using -DGFLAGS_SOVERSION=XXX on the command-line
111if (GFLAGS_SOVERSION)
112 set (PACKAGE_SOVERSION "${GFLAGS_SOVERSION}")
113else ()
Andreas Schuh45341952016-03-04 10:11:39 +0000114 # 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}")
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000120endif ()
121
122# when gflags is included as subproject (e.g., as Git submodule/subtree) in the source
123# tree of a project that uses it, no variables should be added to the CMake cache;
124# users may set the non-cached variable GFLAGS_IS_SUBPROJECT before add_subdirectory(gflags)
125if (NOT DEFINED GFLAGS_IS_SUBPROJECT)
126 if ("^${CMAKE_SOURCE_DIR}$" STREQUAL "^${PROJECT_SOURCE_DIR}$")
127 set (GFLAGS_IS_SUBPROJECT FALSE)
128 else ()
129 set (GFLAGS_IS_SUBPROJECT TRUE)
130 endif ()
131endif ()
132
133# prefix for package variables in CMake configuration file
134string (TOUPPER "${PACKAGE_NAME}" PACKAGE_PREFIX)
Andreas Schuhbf889782014-05-01 20:16:16 +0100135
unknown3d142b82014-03-19 15:27:08 +0100136# ----------------------------------------------------------------------------
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000137# options
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000138
139# maintain binary backwards compatibility with gflags library version <= 2.0,
140# but at the same time enable the use of the preferred new "gflags" namespace
141gflags_define (STRING NAMESPACE "Name(s) of library namespace (separate multiple options by semicolon)" "google;${PACKAGE_NAME}" "${PACKAGE_NAME}")
142gflags_property (NAMESPACE ADVANCED TRUE)
143set (GFLAGS_NAMESPACE_SECONDARY "${NAMESPACE}")
Andreas Schuh659b4402014-05-02 14:56:58 +0100144list (REMOVE_DUPLICATES GFLAGS_NAMESPACE_SECONDARY)
145if (NOT GFLAGS_NAMESPACE_SECONDARY)
146 message (FATAL_ERROR "GFLAGS_NAMESPACE must be set to one (or more) valid C++ namespace identifier(s separated by semicolon \";\").")
Andreas Schuhaff8ef82014-03-27 01:09:20 +0000147endif ()
Andreas Schuh659b4402014-05-02 14:56:58 +0100148foreach (ns IN LISTS GFLAGS_NAMESPACE_SECONDARY)
149 if (NOT ns MATCHES "^[a-zA-Z][a-zA-Z0-9_]*$")
150 message (FATAL_ERROR "GFLAGS_NAMESPACE contains invalid namespace identifier: ${ns}")
151 endif ()
152endforeach ()
153list (GET GFLAGS_NAMESPACE_SECONDARY 0 GFLAGS_NAMESPACE)
154list (REMOVE_AT GFLAGS_NAMESPACE_SECONDARY 0)
unknown3d142b82014-03-19 15:27:08 +0100155
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000156# cached build options when gflags is not a subproject, otherwise non-cached CMake variables
157# usage: gflags_define(BOOL <name> <doc> <default> [<subproject default>])
158gflags_define (BOOL BUILD_SHARED_LIBS "Request build of shared libraries." OFF OFF)
159gflags_define (BOOL BUILD_STATIC_LIBS "Request build of static libraries (default if BUILD_SHARED_LIBS is OFF)." OFF ON)
160gflags_define (BOOL BUILD_gflags_LIB "Request build of the multi-threaded gflags library." ON OFF)
161gflags_define (BOOL BUILD_gflags_nothreads_LIB "Request build of the single-threaded gflags library." ON ON)
162gflags_define (BOOL BUILD_PACKAGING "Enable build of distribution packages using CPack." OFF OFF)
163gflags_define (BOOL BUILD_TESTING "Enable build of the unit tests and their execution using CTest." OFF OFF)
164gflags_define (BOOL INSTALL_HEADERS "Request installation of headers and other development files." ON OFF)
165gflags_define (BOOL INSTALL_SHARED_LIBS "Request installation of shared libraries." ON ON)
166gflags_define (BOOL INSTALL_STATIC_LIBS "Request installation of static libraries." ON OFF)
unknown3d142b82014-03-19 15:27:08 +0100167
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000168gflags_property (BUILD_STATIC_LIBS ADVANCED TRUE)
169gflags_property (INSTALL_HEADERS ADVANCED TRUE)
170gflags_property (INSTALL_SHARED_LIBS ADVANCED TRUE)
171gflags_property (INSTALL_STATIC_LIBS ADVANCED TRUE)
172
173if (NOT GFLAGS_IS_SUBPROJECT)
174 foreach (varname IN ITEMS CMAKE_INSTALL_PREFIX)
175 gflags_property (${varname} ADVANCED FALSE)
176 endforeach ()
177 foreach (varname IN ITEMS CMAKE_CONFIGURATION_TYPES CMAKE_OSX_ARCHITECTURES CMAKE_OSX_DEPLOYMENT_TARGET CMAKE_OSX_SYSROOT)
178 gflags_property (${varname} ADVANCED TRUE)
179 endforeach ()
180 if (NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CXX_FLAGS)
181 gflags_set (CMAKE_BUILD_TYPE Release)
182 endif ()
183 if (CMAKE_CONFIGURATION_TYPES)
184 gflags_property (CMAKE_BUILD_TYPE STRINGS "${CMAKE_CONFIGURATION_TYPES}")
185 endif ()
186endif () # NOT GFLAGS_IS_SUBPROJECT
unknown3d142b82014-03-19 15:27:08 +0100187
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000188if (NOT BUILD_SHARED_LIBS AND NOT BUILD_STATIC_LIBS)
189 set (BUILD_STATIC_LIBS ON)
190endif ()
191if (NOT BUILD_gflags_LIB AND NOT BUILD_gflags_nothreads_LIB)
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000192 message (FATAL_ERROR "At least one of [GFLAGS_]BUILD_gflags_LIB and [GFLAGS_]BUILD_gflags_nothreads_LIB must be ON.")
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000193endif ()
194
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000195gflags_define (STRING INCLUDE_DIR "Name of include directory of installed header files relative to CMAKE_INSTALL_PREFIX/include/" "${PACKAGE_NAME}")
196gflags_property (INCLUDE_DIR ADVANCED TRUE)
197if (IS_ABSOLUTE INCLUDE_DIR)
198 message (FATAL_ERROR "[GFLAGS_]INCLUDE_DIR must be a path relative to CMAKE_INSTALL_PREFIX/include/")
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000199endif ()
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000200if (INCLUDE_DIR MATCHES "^\\.\\.[/\\]")
201 message (FATAL_ERROR "[GFLAGS_]INCLUDE_DIR must not start with parent directory reference (../)")
Andreas Schuh659b4402014-05-02 14:56:58 +0100202endif ()
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000203set (GFLAGS_INCLUDE_DIR "${INCLUDE_DIR}")
Andreas Schuh659b4402014-05-02 14:56:58 +0100204
unknown3d142b82014-03-19 15:27:08 +0100205# ----------------------------------------------------------------------------
206# system checks
207include (CheckTypeSize)
208include (CheckIncludeFileCXX)
209include (CheckCXXSymbolExists)
210
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000211if (WIN32 AND NOT CYGWIN)
212 set (OS_WINDOWS 1)
213else ()
214 set (OS_WINDOWS 0)
215endif ()
216
unknown3d142b82014-03-19 15:27:08 +0100217if (MSVC)
218 set (HAVE_SYS_TYPES_H 1)
219 set (HAVE_STDINT_H 1)
220 set (HAVE_STDDEF_H 1) # used by CheckTypeSize module
221 set (HAVE_INTTYPES_H 0)
222 set (HAVE_UNISTD_H 0)
223 set (HAVE_SYS_STAT_H 1)
Andreas Schuh92425782014-03-19 17:15:36 +0000224 set (HAVE_SHLWAPI_H 1)
unknown3d142b82014-03-19 15:27:08 +0100225else ()
226 foreach (fname IN ITEMS unistd stdint inttypes sys/types sys/stat fnmatch)
227 string (TOUPPER "${fname}" FNAME)
Andreas Schuh92425782014-03-19 17:15:36 +0000228 string (REPLACE "/" "_" FNAME "${FNAME}")
unknown3d142b82014-03-19 15:27:08 +0100229 if (NOT HAVE_${FNAME}_H)
230 check_include_file_cxx ("${fname}.h" HAVE_${FNAME}_H)
231 endif ()
232 endforeach ()
Andreas Schuhddc53572014-03-19 17:32:23 +0000233 # the following are used in #if directives not #ifdef
unknown3d142b82014-03-19 15:27:08 +0100234 bool_to_int (HAVE_STDINT_H)
235 bool_to_int (HAVE_SYS_TYPES_H)
236 bool_to_int (HAVE_INTTYPES_H)
Andreas Schuh92425782014-03-19 17:15:36 +0000237 if (NOT HAVE_FNMATCH_H AND OS_WINDOWS)
238 check_include_file_cxx ("shlwapi.h" HAVE_SHLWAPI_H)
239 endif ()
unknown3d142b82014-03-19 15:27:08 +0100240endif ()
241
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000242gflags_define (STRING INTTYPES_FORMAT "Format of integer types: \"C99\" (uint32_t), \"BSD\" (u_int32_t), \"VC7\" (__int32)" "")
243gflags_property (INTTYPES_FORMAT STRINGS "C99;BSD;VC7")
244gflags_property (INTTYPES_FORMAT ADVANCED TRUE)
245if (NOT INTTYPES_FORMAT)
unknown3d142b82014-03-19 15:27:08 +0100246 set (TYPES uint32_t u_int32_t)
247 if (MSVC)
248 list (INSERT TYPES 0 __int32)
249 endif ()
250 foreach (type IN LISTS TYPES)
251 check_type_size (${type} ${type} LANGUAGE CXX)
252 if (HAVE_${type})
253 break ()
254 endif ()
255 endforeach ()
256 if (HAVE_uint32_t)
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000257 gflags_set (INTTYPES_FORMAT C99)
unknown3d142b82014-03-19 15:27:08 +0100258 elseif (HAVE_u_int32_t)
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000259 gflags_set (INTTYPES_FORMAT BSD)
unknown3d142b82014-03-19 15:27:08 +0100260 elseif (HAVE___int32)
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000261 gflags_set (INTTYPES_FORMAT VC7)
unknown3d142b82014-03-19 15:27:08 +0100262 else ()
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000263 gflags_property (INTTYPES_FORMAT ADVANCED FALSE)
unknown3d142b82014-03-19 15:27:08 +0100264 message (FATAL_ERROR "Do not know how to define a 32-bit integer quantity on your system!"
265 " Neither uint32_t, u_int32_t, nor __int32 seem to be available."
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000266 " Set [GFLAGS_]INTTYPES_FORMAT to either C99, BSD, or VC7 and try again.")
unknown3d142b82014-03-19 15:27:08 +0100267 endif ()
268endif ()
269# use of special characters in strings to circumvent bug #0008226
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000270if ("^${INTTYPES_FORMAT}$" STREQUAL "^WIN$")
271 gflags_set (INTTYPES_FORMAT VC7)
unknown3d142b82014-03-19 15:27:08 +0100272endif ()
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000273if (NOT INTTYPES_FORMAT MATCHES "^(C99|BSD|VC7)$")
274 message (FATAL_ERROR "Invalid value for [GFLAGS_]INTTYPES_FORMAT! Choose one of \"C99\", \"BSD\", or \"VC7\"")
unknown3d142b82014-03-19 15:27:08 +0100275endif ()
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000276set (GFLAGS_INTTYPES_FORMAT "${INTTYPES_FORMAT}")
unknown3d142b82014-03-19 15:27:08 +0100277set (GFLAGS_INTTYPES_FORMAT_C99 0)
278set (GFLAGS_INTTYPES_FORMAT_BSD 0)
279set (GFLAGS_INTTYPES_FORMAT_VC7 0)
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000280set ("GFLAGS_INTTYPES_FORMAT_${INTTYPES_FORMAT}" 1)
unknown3d142b82014-03-19 15:27:08 +0100281
282if (MSVC)
283 set (HAVE_strtoll 0)
284 set (HAVE_strtoq 0)
285else ()
Andreas Schuh0b116eb2014-03-19 15:56:26 +0000286 check_cxx_symbol_exists (strtoll stdlib.h HAVE_STRTOLL)
287 if (NOT HAVE_STRTOLL)
288 check_cxx_symbol_exists (strtoq stdlib.h HAVE_STRTOQ)
289 endif ()
unknown3d142b82014-03-19 15:27:08 +0100290endif ()
291
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000292if (BUILD_gflags_LIB)
293 set (CMAKE_THREAD_PREFER_PTHREAD TRUE)
294 find_package (Threads)
295 if (Threads_FOUND AND CMAKE_USE_PTHREADS_INIT)
296 set (HAVE_PTHREAD 1)
297 check_type_size (pthread_rwlock_t RWLOCK LANGUAGE CXX)
298 else ()
299 set (HAVE_PTHREAD 0)
300 endif ()
301 if (UNIX AND NOT HAVE_PTHREAD)
302 if (CMAKE_HAVE_PTHREAD_H)
303 set (what "library")
304 else ()
305 set (what ".h file")
306 endif ()
307 message (FATAL_ERROR "Could not find pthread${what}. Check the log file"
308 "\n\t${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log"
309 "\nor disable the build of the multi-threaded gflags library (BUILD_gflags_LIB=OFF).")
310 endif ()
unknown3d142b82014-03-19 15:27:08 +0100311else ()
312 set (HAVE_PTHREAD 0)
313endif ()
314
unknown3d142b82014-03-19 15:27:08 +0100315# ----------------------------------------------------------------------------
316# source files - excluding root subdirectory and/or .in suffix
317set (PUBLIC_HDRS
318 "gflags.h"
319 "gflags_declare.h"
320 "gflags_completions.h"
321)
322
Andreas Schuh659b4402014-05-02 14:56:58 +0100323if (GFLAGS_NAMESPACE_SECONDARY)
324 set (INCLUDE_GFLAGS_NS_H "// Import gflags library symbols into alternative/deprecated namespace(s)")
325 foreach (ns IN LISTS GFLAGS_NAMESPACE_SECONDARY)
326 string (TOUPPER "${ns}" NS)
327 set (gflags_ns_h "${PROJECT_BINARY_DIR}/include/${GFLAGS_INCLUDE_DIR}/gflags_${ns}.h")
328 configure_file ("${PROJECT_SOURCE_DIR}/src/gflags_ns.h.in" "${gflags_ns_h}" @ONLY)
329 list (APPEND PUBLIC_HDRS "${gflags_ns_h}")
330 set (INCLUDE_GFLAGS_NS_H "${INCLUDE_GFLAGS_NS_H}\n#include \"gflags_${ns}.h\"")
331 endforeach ()
332else ()
333 set (INCLUDE_GFLAGS_NS_H)
334endif ()
335
unknown3d142b82014-03-19 15:27:08 +0100336set (PRIVATE_HDRS
337 "config.h"
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000338 "util.h"
339 "mutex.h"
unknown3d142b82014-03-19 15:27:08 +0100340)
341
342set (GFLAGS_SRCS
343 "gflags.cc"
344 "gflags_reporting.cc"
345 "gflags_completions.cc"
346)
347
Andreas Schuh41181662014-03-19 16:46:56 +0000348if (OS_WINDOWS)
unknown3d142b82014-03-19 15:27:08 +0100349 list (APPEND PRIVATE_HDRS "windows_port.h")
350 list (APPEND GFLAGS_SRCS "windows_port.cc")
351endif ()
352
353# ----------------------------------------------------------------------------
354# configure source files
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000355if (NOT DEFINED GFLAGS_ATTRIBUTE_UNUSED)
356 if (CMAKE_COMPILER_IS_GNUCXX)
357 set (GFLAGS_ATTRIBUTE_UNUSED "__attribute((unused))")
358 else ()
359 set (GFLAGS_ATTRIBUTE_UNUSED)
360 endif ()
unknown3d142b82014-03-19 15:27:08 +0100361endif ()
362
Andreas Schuh6040eac2014-03-25 15:29:56 +0000363# whenever we build a shared library (DLL on Windows), configure the public
364# headers of the API for use of this library rather than the optionally
365# also build statically linked library; users can override GFLAGS_DLL_DECL
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000366if (NOT DEFINED GFLAGS_IS_A_DLL)
367 if (BUILD_SHARED_LIBS)
368 set (GFLAGS_IS_A_DLL 1)
369 else ()
370 set (GFLAGS_IS_A_DLL 0)
371 endif ()
Andreas Schuh6040eac2014-03-25 15:29:56 +0000372endif ()
373
unknown3d142b82014-03-19 15:27:08 +0100374configure_headers (PUBLIC_HDRS ${PUBLIC_HDRS})
375configure_sources (PRIVATE_HDRS ${PRIVATE_HDRS})
376configure_sources (GFLAGS_SRCS ${GFLAGS_SRCS})
377
378# ----------------------------------------------------------------------------
379# output directories
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000380if (NOT GFLAGS_IS_SUBPROJECT)
381 set (CMAKE_RUNTIME_OUTPUT_DIRECTORY "bin")
382 set (CMAKE_LIBRARY_OUTPUT_DIRECTORY "lib")
383 set (CMAKE_ARCHIVE_OUTPUT_DIRECTORY "lib")
384endif ()
unknown3d142b82014-03-19 15:27:08 +0100385
386# ----------------------------------------------------------------------------
Andreas Schuh76c53b72015-03-24 14:44:12 +0000387# installation directories
388if (OS_WINDOWS)
389 set (RUNTIME_INSTALL_DIR Bin)
390 set (LIBRARY_INSTALL_DIR Lib)
391 set (INCLUDE_INSTALL_DIR Include)
392 set (CONFIG_INSTALL_DIR CMake)
393else ()
394 set (RUNTIME_INSTALL_DIR bin)
395 # The LIB_INSTALL_DIR and LIB_SUFFIX variables are used by the Fedora
396 # package maintainers. Also package maintainers of other distribution
397 # packages need to be able to specify the name of the library directory.
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000398 if (NOT GFLAGS_LIBRARY_INSTALL_DIR AND LIB_INSTALL_DIR)
399 set (GFLAGS_LIBRARY_INSTALL_DIR "${LIB_INSTALL_DIR}")
Andreas Schuh76c53b72015-03-24 14:44:12 +0000400 endif ()
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000401 gflags_define (PATH LIBRARY_INSTALL_DIR "Directory of installed libraries, e.g., \"lib64\"" "lib${LIB_SUFFIX}")
402 gflags_property (LIBRARY_INSTALL_DIR ADVANCED TRUE)
Andreas Schuh76c53b72015-03-24 14:44:12 +0000403 set (INCLUDE_INSTALL_DIR include)
404 set (CONFIG_INSTALL_DIR ${LIBRARY_INSTALL_DIR}/cmake/${PACKAGE_NAME})
405endif ()
406
407# ----------------------------------------------------------------------------
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000408# add library targets
409set (TARGETS)
410# static vs. shared
411foreach (TYPE IN ITEMS STATIC SHARED)
412 if (BUILD_${TYPE}_LIBS)
Arnaud Farbosd78b00d2015-11-05 12:03:37 -0800413 string (TOLOWER "${TYPE}" type)
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000414 # whether or not targets are a DLL
415 if (OS_WINDOWS AND "^${TYPE}$" STREQUAL "^SHARED$")
416 set (GFLAGS_IS_A_DLL 1)
417 else ()
418 set (GFLAGS_IS_A_DLL 0)
419 endif ()
Andreas Schuh6bd636c2016-02-29 17:58:46 +0000420 # filename suffix for static libraries on Windows
421 if (OS_WINDOWS AND "^${TYPE}$" STREQUAL "^STATIC$")
422 set (type_suffix "_${type}")
423 else ()
424 set (type_suffix "")
425 endif ()
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000426 # multi-threaded vs. single-threaded
427 foreach (opts IN ITEMS "" _nothreads)
428 if (BUILD_gflags${opts}_LIB)
Andreas Schuh7a21c162016-03-01 21:04:10 +0000429 add_library (gflags${opts}_${type} ${TYPE} ${GFLAGS_SRCS} ${PRIVATE_HDRS} ${PUBLIC_HDRS})
Andreas Schuh58345b12015-04-03 16:12:38 +0100430 set (include_dirs "$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/include>")
431 if (INSTALL_HEADERS)
432 list (APPEND include_dirs "$<INSTALL_INTERFACE:${INCLUDE_INSTALL_DIR}>")
Andreas Schuh76c53b72015-03-24 14:44:12 +0000433 endif ()
Andreas Schuh7a21c162016-03-01 21:04:10 +0000434 target_include_directories (gflags${opts}_${type}
Andreas Schuh58345b12015-04-03 16:12:38 +0100435 PUBLIC "${include_dirs}"
436 PRIVATE "${PROJECT_SOURCE_DIR}/src;${PROJECT_BINARY_DIR}/include/${GFLAGS_INCLUDE_DIR}"
437 )
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000438 if (opts MATCHES "nothreads")
Sam Cleggb572a722016-01-29 16:09:43 -0800439 set (defines "GFLAGS_IS_A_DLL=${GFLAGS_IS_A_DLL};NO_THREADS")
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000440 else ()
441 set (defines "GFLAGS_IS_A_DLL=${GFLAGS_IS_A_DLL}")
Andreas Schuhbf139ea2014-03-20 03:04:44 +0000442 if (CMAKE_USE_PTHREADS_INIT)
Andreas Schuh7a21c162016-03-01 21:04:10 +0000443 target_link_libraries (gflags${opts}_${type} ${CMAKE_THREAD_LIBS_INIT})
Andreas Schuhbf139ea2014-03-20 03:04:44 +0000444 endif ()
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000445 endif ()
446 set_target_properties (
Andreas Schuh7a21c162016-03-01 21:04:10 +0000447 gflags${opts}_${type} PROPERTIES COMPILE_DEFINITIONS "${defines}"
Andreas Schuh6bd636c2016-02-29 17:58:46 +0000448 OUTPUT_NAME "gflags${opts}${type_suffix}"
Andreas Schuhcd7aece2014-05-02 11:12:05 +0100449 VERSION "${PACKAGE_VERSION}"
450 SOVERSION "${PACKAGE_SOVERSION}"
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000451 )
452 if (HAVE_SHLWAPI_H)
Andreas Schuh7a21c162016-03-01 21:04:10 +0000453 target_link_libraries (gflags${opts}_${type} shlwapi.lib)
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000454 endif ()
Andreas Schuh7a21c162016-03-01 21:04:10 +0000455 list (APPEND TARGETS gflags${opts}_${type})
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000456 # add convenience make target for build of both shared and static libraries
457 if (NOT GFLAGS_IS_SUBPROJECT)
458 if (NOT TARGET gflags${opts})
459 add_custom_target (gflags${opts})
460 endif ()
Andreas Schuh7a21c162016-03-01 21:04:10 +0000461 add_dependencies (gflags${opts} gflags${opts}_${type})
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000462 endif ()
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000463 endif ()
464 endforeach ()
Andreas Schuhddc53572014-03-19 17:32:23 +0000465 endif ()
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000466endforeach ()
unknown3d142b82014-03-19 15:27:08 +0100467
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000468# add ALIAS target for use in super-project, prefer static over shared, single-threaded over multi-threaded
469if (GFLAGS_IS_SUBPROJECT)
470 foreach (type IN ITEMS static shared)
471 foreach (opts IN ITEMS "_nothreads" "")
Andreas Schuh7a21c162016-03-01 21:04:10 +0000472 if (TARGET gflags${opts}_${type})
473 add_library (gflags ALIAS gflags${opts}_${type})
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000474 break ()
475 endif ()
476 endforeach ()
477 if (TARGET gflags)
478 break ()
479 endif ()
480 endforeach ()
481endif ()
482
unknown3d142b82014-03-19 15:27:08 +0100483# ----------------------------------------------------------------------------
Andreas Schuh76c53b72015-03-24 14:44:12 +0000484# installation rules
unknown3d142b82014-03-19 15:27:08 +0100485file (RELATIVE_PATH INSTALL_PREFIX_REL2CONFIG_DIR "${CMAKE_INSTALL_PREFIX}/${CONFIG_INSTALL_DIR}" "${CMAKE_INSTALL_PREFIX}")
486configure_file (cmake/config.cmake.in "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-config-install.cmake" @ONLY)
487configure_file (cmake/version.cmake.in "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-config-version.cmake" @ONLY)
488
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000489if (BUILD_SHARED_LIBS AND INSTALL_SHARED_LIBS)
490 foreach (opts IN ITEMS "" _nothreads)
491 if (BUILD_gflags${opts}_LIB)
Andreas Schuh7a21c162016-03-01 21:04:10 +0000492 install (TARGETS gflags${opts}_shared DESTINATION ${LIBRARY_INSTALL_DIR} EXPORT gflags-lib)
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000493 endif ()
494 endforeach ()
495endif ()
496if (BUILD_STATIC_LIBS AND INSTALL_STATIC_LIBS)
497 foreach (opts IN ITEMS "" _nothreads)
498 if (BUILD_gflags${opts}_LIB)
Andreas Schuh7a21c162016-03-01 21:04:10 +0000499 install (TARGETS gflags${opts}_static DESTINATION ${LIBRARY_INSTALL_DIR} EXPORT gflags-lib)
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000500 endif ()
501 endforeach ()
502endif ()
503
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000504if (INSTALL_HEADERS)
Andreas Schuhaff8ef82014-03-27 01:09:20 +0000505 install (FILES ${PUBLIC_HDRS} DESTINATION ${INCLUDE_INSTALL_DIR}/${GFLAGS_INCLUDE_DIR})
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000506 install (
507 FILES "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-config-install.cmake"
508 RENAME ${PACKAGE_NAME}-config.cmake
509 DESTINATION ${CONFIG_INSTALL_DIR}
510 )
511 install (
512 FILES "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-config-version.cmake"
513 DESTINATION ${CONFIG_INSTALL_DIR}
514 )
Andreas Schuh7a21c162016-03-01 21:04:10 +0000515 install (EXPORT gflags-lib DESTINATION ${CONFIG_INSTALL_DIR} FILE ${PACKAGE_NAME}-targets.cmake)
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000516 if (UNIX)
517 install (PROGRAMS src/gflags_completions.sh DESTINATION ${RUNTIME_INSTALL_DIR})
518 endif ()
unknown3d142b82014-03-19 15:27:08 +0100519endif ()
520
521# ----------------------------------------------------------------------------
522# support direct use of build tree
523set (INSTALL_PREFIX_REL2CONFIG_DIR .)
Andreas Schuh7a21c162016-03-01 21:04:10 +0000524export (TARGETS ${TARGETS} FILE "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-targets.cmake")
unknown3d142b82014-03-19 15:27:08 +0100525export (PACKAGE gflags)
526configure_file (cmake/config.cmake.in "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-config.cmake" @ONLY)
527
528# ----------------------------------------------------------------------------
529# testing - MUST follow the generation of the build tree config file
unknown3d142b82014-03-19 15:27:08 +0100530if (BUILD_TESTING)
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000531 include (CTest)
unknown3d142b82014-03-19 15:27:08 +0100532 enable_testing ()
533 add_subdirectory (test)
534endif ()
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000535
536# ----------------------------------------------------------------------------
537# packaging
538if (BUILD_PACKAGING)
539
540 if (NOT BUILD_SHARED_LIBS AND NOT INSTALL_HEADERS)
541 message (WARNING "Package will contain static libraries without headers!"
542 "\nRecommended options for generation of runtime package:"
543 "\n BUILD_SHARED_LIBS=ON"
544 "\n BUILD_STATIC_LIBS=OFF"
545 "\n INSTALL_HEADERS=OFF"
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000546 "\n INSTALL_SHARED_LIBS=ON"
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000547 "\nRecommended options for generation of development package:"
548 "\n BUILD_SHARED_LIBS=ON"
549 "\n BUILD_STATIC_LIBS=ON"
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000550 "\n INSTALL_HEADERS=ON"
551 "\n INSTALL_SHARED_LIBS=ON"
552 "\n INSTALL_STATIC_LIBS=ON")
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000553 endif ()
554
555 # default package generators
556 if (APPLE)
557 set (PACKAGE_GENERATOR "PackageMaker")
558 set (PACKAGE_SOURCE_GENERATOR "TGZ;ZIP")
559 elseif (UNIX)
560 set (PACKAGE_GENERATOR "DEB;RPM")
561 set (PACKAGE_SOURCE_GENERATOR "TGZ;ZIP")
562 else ()
563 set (PACKAGE_GENERATOR "ZIP")
564 set (PACKAGE_SOURCE_GENERATOR "ZIP")
565 endif ()
566
567 # used package generators
568 set (CPACK_GENERATOR "${PACKAGE_GENERATOR}" CACHE STRING "List of binary package generators (CPack).")
569 set (CPACK_SOURCE_GENERATOR "${PACKAGE_SOURCE_GENERATOR}" CACHE STRING "List of source package generators (CPack).")
570 mark_as_advanced (CPACK_GENERATOR CPACK_SOURCE_GENERATOR)
571
Andreas Schuh64ac2e32015-03-24 16:21:44 +0000572 # some package generators (e.g., PackageMaker) do not allow .md extension
573 configure_file ("${CMAKE_CURRENT_LIST_DIR}/README.md" "${CMAKE_CURRENT_BINARY_DIR}/README.txt" COPYONLY)
574
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000575 # common package information
Andreas Schuhfc6e0792014-03-20 04:09:02 +0000576 set (CPACK_PACKAGE_VENDOR "Andreas Schuh")
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000577 set (CPACK_PACKAGE_CONTACT "google-gflags@googlegroups.com")
578 set (CPACK_PACKAGE_NAME "${PACKAGE_NAME}")
579 set (CPACK_PACKAGE_VERSION "${PACKAGE_VERSION}")
580 set (CPACK_PACKAGE_VERSION_MAJOR "${PACKAGE_VERSION_MAJOR}")
581 set (CPACK_PACKAGE_VERSION_MINOR "${PACKAGE_VERSION_MINOR}")
582 set (CPACK_PACKAGE_VERSION_PATCH "${PACKAGE_VERSION_PATCH}")
583 set (CPACK_PACKAGE_DESCRIPTION_SUMMARY "A commandline flags library that allows for distributed flags.")
Andreas Schuha819f0f2015-03-24 16:29:31 +0000584 set (CPACK_RESOURCE_FILE_WELCOME "${CMAKE_CURRENT_BINARY_DIR}/README.txt")
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000585 set (CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_LIST_DIR}/COPYING.txt")
Andreas Schuha819f0f2015-03-24 16:29:31 +0000586 set (CPACK_PACKAGE_DESCRIPTION_FILE "${CMAKE_CURRENT_BINARY_DIR}/README.txt")
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000587 set (CPACK_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}")
588 set (CPACK_OUTPUT_FILE_PREFIX packages)
589 set (CPACK_PACKAGE_RELOCATABLE TRUE)
590 set (CPACK_MONOLITHIC_INSTALL TRUE)
591
Andreas Schuhe8890f22014-03-20 04:20:15 +0000592 # RPM package information -- used in cmake/package.cmake.in also for DEB
Andreas Schuh37a9a902014-03-20 03:27:13 +0000593 set (CPACK_RPM_PACKAGE_GROUP "Development/Libraries")
594 set (CPACK_RPM_PACKAGE_LICENSE "BSD")
Andreas Schuhabe4b102016-03-01 16:01:45 +0000595 set (CPACK_RPM_PACKAGE_URL "http://gflags.github.io/gflags")
Andreas Schuh37a9a902014-03-20 03:27:13 +0000596 set (CPACK_RPM_CHANGELOG_FILE "${CMAKE_CURRENT_LIST_DIR}/ChangeLog.txt")
597
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000598 if (INSTALL_HEADERS)
Andreas Schuh2b8deaa2015-03-24 14:41:52 +0000599 set (CPACK_RESOURCE_FILE_README "${CMAKE_CURRENT_LIST_DIR}/doc/index.html")
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000600 else ()
601 set (CPACK_RESOURCE_FILE_README "${CMAKE_CURRENT_LIST_DIR}/cmake/README_runtime.txt")
602 endif ()
603
Andreas Schuhd6995592014-03-20 20:21:30 +0000604 # system/architecture
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000605 if (WINDOWS)
606 if (CMAKE_CL_64)
607 set (CPACK_SYSTEM_NAME "win64")
608 else ()
609 set (CPACK_SYSTEM_NAME "win32")
610 endif ()
Andreas Schuhd6995592014-03-20 20:21:30 +0000611 set (CPACK_PACKAGE_ARCHITECTURE)
Andreas Schuhef5c5472014-03-30 15:34:13 +0100612 elseif (APPLE)
613 set (CPACK_PACKAGE_ARCHITECTURE darwin)
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000614 else ()
615 string (TOLOWER "${CMAKE_SYSTEM_NAME}" CPACK_SYSTEM_NAME)
Andreas Schuhcf40f732014-03-20 20:56:05 +0000616 if (CMAKE_CXX_FLAGS MATCHES "-m32")
617 set (CPACK_PACKAGE_ARCHITECTURE i386)
618 else ()
619 execute_process (
620 COMMAND dpkg --print-architecture
621 RESULT_VARIABLE RV
622 OUTPUT_VARIABLE CPACK_PACKAGE_ARCHITECTURE
623 )
624 if (RV EQUAL 0)
625 string (STRIP "${CPACK_PACKAGE_ARCHITECTURE}" CPACK_PACKAGE_ARCHITECTURE)
626 else ()
627 execute_process (COMMAND uname -m OUTPUT_VARIABLE CPACK_PACKAGE_ARCHITECTURE)
628 if (CPACK_PACKAGE_ARCHITECTURE MATCHES "x86_64")
629 set (CPACK_PACKAGE_ARCHITECTURE amd64)
630 else ()
631 set (CPACK_PACKAGE_ARCHITECTURE i386)
632 endif ()
633 endif ()
634 endif ()
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000635 endif ()
636
637 # source package settings
638 set (CPACK_SOURCE_TOPLEVEL_TAG "source")
639 set (CPACK_SOURCE_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}")
640 set (CPACK_SOURCE_IGNORE_FILES "/\\\\.git/;\\\\.swp$;\\\\.#;/#;\\\\.*~;cscope\\\\.*;/[Bb]uild[.+-_a-zA-Z0-9]*/")
641
642 # default binary package settings
Andreas Schuhd6995592014-03-20 20:21:30 +0000643 set (CPACK_INCLUDE_TOPLEVEL_DIRECTORY TRUE)
644 set (CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}-${CPACK_SYSTEM_NAME}")
645 if (CPACK_PACKAGE_ARCHITECTURE)
646 set (CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_FILE_NAME}-${CPACK_PACKAGE_ARCHITECTURE}")
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000647 endif ()
648
649 # generator specific configuration file
650 #
651 # allow package maintainers to use their own configuration file
652 # $ cmake -DCPACK_PROJECT_CONFIG_FILE:FILE=/path/to/package/config
653 if (NOT CPACK_PROJECT_CONFIG_FILE)
654 configure_file (
655 "${CMAKE_CURRENT_LIST_DIR}/cmake/package.cmake.in"
656 "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-package.cmake" @ONLY
657 )
658 set (CPACK_PROJECT_CONFIG_FILE "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-package.cmake")
659 endif ()
660
661 include (CPack)
662
663endif () # BUILD_PACKAGING