blob: 15582cf71708615c63efe843741053c098f17875 [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
Andreas Schuh18469832016-11-24 23:15:34 +0000136# convert file path on Windows with back slashes to path with forward slashes
137# otherwise this causes an issue with the cmake_install.cmake script
138file (TO_CMAKE_PATH "${CMAKE_INSTALL_PREFIX}" CMAKE_INSTALL_PREFIX)
139
unknown3d142b82014-03-19 15:27:08 +0100140# ----------------------------------------------------------------------------
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000141# options
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000142
143# maintain binary backwards compatibility with gflags library version <= 2.0,
144# but at the same time enable the use of the preferred new "gflags" namespace
145gflags_define (STRING NAMESPACE "Name(s) of library namespace (separate multiple options by semicolon)" "google;${PACKAGE_NAME}" "${PACKAGE_NAME}")
146gflags_property (NAMESPACE ADVANCED TRUE)
147set (GFLAGS_NAMESPACE_SECONDARY "${NAMESPACE}")
Andreas Schuh659b4402014-05-02 14:56:58 +0100148list (REMOVE_DUPLICATES GFLAGS_NAMESPACE_SECONDARY)
149if (NOT GFLAGS_NAMESPACE_SECONDARY)
150 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 +0000151endif ()
Andreas Schuh659b4402014-05-02 14:56:58 +0100152foreach (ns IN LISTS GFLAGS_NAMESPACE_SECONDARY)
153 if (NOT ns MATCHES "^[a-zA-Z][a-zA-Z0-9_]*$")
154 message (FATAL_ERROR "GFLAGS_NAMESPACE contains invalid namespace identifier: ${ns}")
155 endif ()
156endforeach ()
157list (GET GFLAGS_NAMESPACE_SECONDARY 0 GFLAGS_NAMESPACE)
158list (REMOVE_AT GFLAGS_NAMESPACE_SECONDARY 0)
unknown3d142b82014-03-19 15:27:08 +0100159
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000160# cached build options when gflags is not a subproject, otherwise non-cached CMake variables
161# usage: gflags_define(BOOL <name> <doc> <default> [<subproject default>])
162gflags_define (BOOL BUILD_SHARED_LIBS "Request build of shared libraries." OFF OFF)
163gflags_define (BOOL BUILD_STATIC_LIBS "Request build of static libraries (default if BUILD_SHARED_LIBS is OFF)." OFF ON)
164gflags_define (BOOL BUILD_gflags_LIB "Request build of the multi-threaded gflags library." ON OFF)
165gflags_define (BOOL BUILD_gflags_nothreads_LIB "Request build of the single-threaded gflags library." ON ON)
166gflags_define (BOOL BUILD_PACKAGING "Enable build of distribution packages using CPack." OFF OFF)
167gflags_define (BOOL BUILD_TESTING "Enable build of the unit tests and their execution using CTest." OFF OFF)
168gflags_define (BOOL INSTALL_HEADERS "Request installation of headers and other development files." ON OFF)
169gflags_define (BOOL INSTALL_SHARED_LIBS "Request installation of shared libraries." ON ON)
170gflags_define (BOOL INSTALL_STATIC_LIBS "Request installation of static libraries." ON OFF)
Andreas Schuh3886da52016-11-24 22:36:48 +0000171gflags_define (BOOL REGISTER_BUILD_DIR "Request entry of build directory in CMake's package registry." OFF OFF)
172gflags_define (BOOL REGISTER_INSTALL_PREFIX "Request entry of installed package in CMake's package registry." ON OFF)
unknown3d142b82014-03-19 15:27:08 +0100173
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000174gflags_property (BUILD_STATIC_LIBS ADVANCED TRUE)
175gflags_property (INSTALL_HEADERS ADVANCED TRUE)
176gflags_property (INSTALL_SHARED_LIBS ADVANCED TRUE)
177gflags_property (INSTALL_STATIC_LIBS ADVANCED TRUE)
178
179if (NOT GFLAGS_IS_SUBPROJECT)
180 foreach (varname IN ITEMS CMAKE_INSTALL_PREFIX)
181 gflags_property (${varname} ADVANCED FALSE)
182 endforeach ()
183 foreach (varname IN ITEMS CMAKE_CONFIGURATION_TYPES CMAKE_OSX_ARCHITECTURES CMAKE_OSX_DEPLOYMENT_TARGET CMAKE_OSX_SYSROOT)
184 gflags_property (${varname} ADVANCED TRUE)
185 endforeach ()
186 if (NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CXX_FLAGS)
187 gflags_set (CMAKE_BUILD_TYPE Release)
188 endif ()
189 if (CMAKE_CONFIGURATION_TYPES)
190 gflags_property (CMAKE_BUILD_TYPE STRINGS "${CMAKE_CONFIGURATION_TYPES}")
191 endif ()
192endif () # NOT GFLAGS_IS_SUBPROJECT
unknown3d142b82014-03-19 15:27:08 +0100193
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000194if (NOT BUILD_SHARED_LIBS AND NOT BUILD_STATIC_LIBS)
195 set (BUILD_STATIC_LIBS ON)
196endif ()
197if (NOT BUILD_gflags_LIB AND NOT BUILD_gflags_nothreads_LIB)
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000198 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 +0000199endif ()
200
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000201gflags_define (STRING INCLUDE_DIR "Name of include directory of installed header files relative to CMAKE_INSTALL_PREFIX/include/" "${PACKAGE_NAME}")
202gflags_property (INCLUDE_DIR ADVANCED TRUE)
Andreas Schuh18469832016-11-24 23:15:34 +0000203file (TO_CMAKE_PATH "${INCLUDE_DIR}" INCLUDE_DIR)
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000204if (IS_ABSOLUTE INCLUDE_DIR)
205 message (FATAL_ERROR "[GFLAGS_]INCLUDE_DIR must be a path relative to CMAKE_INSTALL_PREFIX/include/")
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000206endif ()
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000207if (INCLUDE_DIR MATCHES "^\\.\\.[/\\]")
208 message (FATAL_ERROR "[GFLAGS_]INCLUDE_DIR must not start with parent directory reference (../)")
Andreas Schuh659b4402014-05-02 14:56:58 +0100209endif ()
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000210set (GFLAGS_INCLUDE_DIR "${INCLUDE_DIR}")
Andreas Schuh659b4402014-05-02 14:56:58 +0100211
unknown3d142b82014-03-19 15:27:08 +0100212# ----------------------------------------------------------------------------
213# system checks
214include (CheckTypeSize)
215include (CheckIncludeFileCXX)
216include (CheckCXXSymbolExists)
217
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000218if (WIN32 AND NOT CYGWIN)
219 set (OS_WINDOWS 1)
220else ()
221 set (OS_WINDOWS 0)
222endif ()
223
unknown3d142b82014-03-19 15:27:08 +0100224if (MSVC)
225 set (HAVE_SYS_TYPES_H 1)
226 set (HAVE_STDINT_H 1)
227 set (HAVE_STDDEF_H 1) # used by CheckTypeSize module
228 set (HAVE_INTTYPES_H 0)
229 set (HAVE_UNISTD_H 0)
230 set (HAVE_SYS_STAT_H 1)
Andreas Schuh92425782014-03-19 17:15:36 +0000231 set (HAVE_SHLWAPI_H 1)
unknown3d142b82014-03-19 15:27:08 +0100232else ()
233 foreach (fname IN ITEMS unistd stdint inttypes sys/types sys/stat fnmatch)
234 string (TOUPPER "${fname}" FNAME)
Andreas Schuh92425782014-03-19 17:15:36 +0000235 string (REPLACE "/" "_" FNAME "${FNAME}")
unknown3d142b82014-03-19 15:27:08 +0100236 if (NOT HAVE_${FNAME}_H)
237 check_include_file_cxx ("${fname}.h" HAVE_${FNAME}_H)
238 endif ()
239 endforeach ()
Andreas Schuhddc53572014-03-19 17:32:23 +0000240 # the following are used in #if directives not #ifdef
unknown3d142b82014-03-19 15:27:08 +0100241 bool_to_int (HAVE_STDINT_H)
242 bool_to_int (HAVE_SYS_TYPES_H)
243 bool_to_int (HAVE_INTTYPES_H)
Andreas Schuh92425782014-03-19 17:15:36 +0000244 if (NOT HAVE_FNMATCH_H AND OS_WINDOWS)
245 check_include_file_cxx ("shlwapi.h" HAVE_SHLWAPI_H)
246 endif ()
unknown3d142b82014-03-19 15:27:08 +0100247endif ()
248
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000249gflags_define (STRING INTTYPES_FORMAT "Format of integer types: \"C99\" (uint32_t), \"BSD\" (u_int32_t), \"VC7\" (__int32)" "")
250gflags_property (INTTYPES_FORMAT STRINGS "C99;BSD;VC7")
251gflags_property (INTTYPES_FORMAT ADVANCED TRUE)
252if (NOT INTTYPES_FORMAT)
unknown3d142b82014-03-19 15:27:08 +0100253 set (TYPES uint32_t u_int32_t)
254 if (MSVC)
255 list (INSERT TYPES 0 __int32)
256 endif ()
257 foreach (type IN LISTS TYPES)
258 check_type_size (${type} ${type} LANGUAGE CXX)
259 if (HAVE_${type})
260 break ()
261 endif ()
262 endforeach ()
263 if (HAVE_uint32_t)
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000264 gflags_set (INTTYPES_FORMAT C99)
unknown3d142b82014-03-19 15:27:08 +0100265 elseif (HAVE_u_int32_t)
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000266 gflags_set (INTTYPES_FORMAT BSD)
unknown3d142b82014-03-19 15:27:08 +0100267 elseif (HAVE___int32)
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000268 gflags_set (INTTYPES_FORMAT VC7)
unknown3d142b82014-03-19 15:27:08 +0100269 else ()
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000270 gflags_property (INTTYPES_FORMAT ADVANCED FALSE)
unknown3d142b82014-03-19 15:27:08 +0100271 message (FATAL_ERROR "Do not know how to define a 32-bit integer quantity on your system!"
272 " Neither uint32_t, u_int32_t, nor __int32 seem to be available."
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000273 " Set [GFLAGS_]INTTYPES_FORMAT to either C99, BSD, or VC7 and try again.")
unknown3d142b82014-03-19 15:27:08 +0100274 endif ()
275endif ()
276# use of special characters in strings to circumvent bug #0008226
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000277if ("^${INTTYPES_FORMAT}$" STREQUAL "^WIN$")
278 gflags_set (INTTYPES_FORMAT VC7)
unknown3d142b82014-03-19 15:27:08 +0100279endif ()
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000280if (NOT INTTYPES_FORMAT MATCHES "^(C99|BSD|VC7)$")
281 message (FATAL_ERROR "Invalid value for [GFLAGS_]INTTYPES_FORMAT! Choose one of \"C99\", \"BSD\", or \"VC7\"")
unknown3d142b82014-03-19 15:27:08 +0100282endif ()
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000283set (GFLAGS_INTTYPES_FORMAT "${INTTYPES_FORMAT}")
unknown3d142b82014-03-19 15:27:08 +0100284set (GFLAGS_INTTYPES_FORMAT_C99 0)
285set (GFLAGS_INTTYPES_FORMAT_BSD 0)
286set (GFLAGS_INTTYPES_FORMAT_VC7 0)
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000287set ("GFLAGS_INTTYPES_FORMAT_${INTTYPES_FORMAT}" 1)
unknown3d142b82014-03-19 15:27:08 +0100288
289if (MSVC)
290 set (HAVE_strtoll 0)
291 set (HAVE_strtoq 0)
292else ()
Andreas Schuh0b116eb2014-03-19 15:56:26 +0000293 check_cxx_symbol_exists (strtoll stdlib.h HAVE_STRTOLL)
294 if (NOT HAVE_STRTOLL)
295 check_cxx_symbol_exists (strtoq stdlib.h HAVE_STRTOQ)
296 endif ()
unknown3d142b82014-03-19 15:27:08 +0100297endif ()
298
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000299if (BUILD_gflags_LIB)
300 set (CMAKE_THREAD_PREFER_PTHREAD TRUE)
301 find_package (Threads)
302 if (Threads_FOUND AND CMAKE_USE_PTHREADS_INIT)
303 set (HAVE_PTHREAD 1)
304 check_type_size (pthread_rwlock_t RWLOCK LANGUAGE CXX)
305 else ()
306 set (HAVE_PTHREAD 0)
307 endif ()
308 if (UNIX AND NOT HAVE_PTHREAD)
309 if (CMAKE_HAVE_PTHREAD_H)
310 set (what "library")
311 else ()
312 set (what ".h file")
313 endif ()
314 message (FATAL_ERROR "Could not find pthread${what}. Check the log file"
315 "\n\t${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log"
316 "\nor disable the build of the multi-threaded gflags library (BUILD_gflags_LIB=OFF).")
317 endif ()
unknown3d142b82014-03-19 15:27:08 +0100318else ()
319 set (HAVE_PTHREAD 0)
320endif ()
321
unknown3d142b82014-03-19 15:27:08 +0100322# ----------------------------------------------------------------------------
323# source files - excluding root subdirectory and/or .in suffix
324set (PUBLIC_HDRS
325 "gflags.h"
326 "gflags_declare.h"
327 "gflags_completions.h"
328)
329
Andreas Schuh659b4402014-05-02 14:56:58 +0100330if (GFLAGS_NAMESPACE_SECONDARY)
331 set (INCLUDE_GFLAGS_NS_H "// Import gflags library symbols into alternative/deprecated namespace(s)")
332 foreach (ns IN LISTS GFLAGS_NAMESPACE_SECONDARY)
333 string (TOUPPER "${ns}" NS)
334 set (gflags_ns_h "${PROJECT_BINARY_DIR}/include/${GFLAGS_INCLUDE_DIR}/gflags_${ns}.h")
335 configure_file ("${PROJECT_SOURCE_DIR}/src/gflags_ns.h.in" "${gflags_ns_h}" @ONLY)
336 list (APPEND PUBLIC_HDRS "${gflags_ns_h}")
337 set (INCLUDE_GFLAGS_NS_H "${INCLUDE_GFLAGS_NS_H}\n#include \"gflags_${ns}.h\"")
338 endforeach ()
339else ()
340 set (INCLUDE_GFLAGS_NS_H)
341endif ()
342
unknown3d142b82014-03-19 15:27:08 +0100343set (PRIVATE_HDRS
344 "config.h"
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000345 "util.h"
346 "mutex.h"
unknown3d142b82014-03-19 15:27:08 +0100347)
348
349set (GFLAGS_SRCS
350 "gflags.cc"
351 "gflags_reporting.cc"
352 "gflags_completions.cc"
353)
354
Andreas Schuh41181662014-03-19 16:46:56 +0000355if (OS_WINDOWS)
unknown3d142b82014-03-19 15:27:08 +0100356 list (APPEND PRIVATE_HDRS "windows_port.h")
357 list (APPEND GFLAGS_SRCS "windows_port.cc")
358endif ()
359
360# ----------------------------------------------------------------------------
361# configure source files
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000362if (NOT DEFINED GFLAGS_ATTRIBUTE_UNUSED)
363 if (CMAKE_COMPILER_IS_GNUCXX)
364 set (GFLAGS_ATTRIBUTE_UNUSED "__attribute((unused))")
365 else ()
366 set (GFLAGS_ATTRIBUTE_UNUSED)
367 endif ()
unknown3d142b82014-03-19 15:27:08 +0100368endif ()
369
Andreas Schuh6040eac2014-03-25 15:29:56 +0000370# whenever we build a shared library (DLL on Windows), configure the public
371# headers of the API for use of this library rather than the optionally
372# also build statically linked library; users can override GFLAGS_DLL_DECL
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000373if (NOT DEFINED GFLAGS_IS_A_DLL)
374 if (BUILD_SHARED_LIBS)
375 set (GFLAGS_IS_A_DLL 1)
376 else ()
377 set (GFLAGS_IS_A_DLL 0)
378 endif ()
Andreas Schuh6040eac2014-03-25 15:29:56 +0000379endif ()
380
unknown3d142b82014-03-19 15:27:08 +0100381configure_headers (PUBLIC_HDRS ${PUBLIC_HDRS})
382configure_sources (PRIVATE_HDRS ${PRIVATE_HDRS})
383configure_sources (GFLAGS_SRCS ${GFLAGS_SRCS})
384
385# ----------------------------------------------------------------------------
386# output directories
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000387if (NOT GFLAGS_IS_SUBPROJECT)
388 set (CMAKE_RUNTIME_OUTPUT_DIRECTORY "bin")
389 set (CMAKE_LIBRARY_OUTPUT_DIRECTORY "lib")
390 set (CMAKE_ARCHIVE_OUTPUT_DIRECTORY "lib")
391endif ()
unknown3d142b82014-03-19 15:27:08 +0100392
393# ----------------------------------------------------------------------------
Andreas Schuh76c53b72015-03-24 14:44:12 +0000394# installation directories
395if (OS_WINDOWS)
396 set (RUNTIME_INSTALL_DIR Bin)
397 set (LIBRARY_INSTALL_DIR Lib)
398 set (INCLUDE_INSTALL_DIR Include)
399 set (CONFIG_INSTALL_DIR CMake)
400else ()
401 set (RUNTIME_INSTALL_DIR bin)
402 # The LIB_INSTALL_DIR and LIB_SUFFIX variables are used by the Fedora
403 # package maintainers. Also package maintainers of other distribution
404 # packages need to be able to specify the name of the library directory.
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000405 if (NOT GFLAGS_LIBRARY_INSTALL_DIR AND LIB_INSTALL_DIR)
406 set (GFLAGS_LIBRARY_INSTALL_DIR "${LIB_INSTALL_DIR}")
Andreas Schuh76c53b72015-03-24 14:44:12 +0000407 endif ()
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000408 gflags_define (PATH LIBRARY_INSTALL_DIR "Directory of installed libraries, e.g., \"lib64\"" "lib${LIB_SUFFIX}")
409 gflags_property (LIBRARY_INSTALL_DIR ADVANCED TRUE)
Andreas Schuh76c53b72015-03-24 14:44:12 +0000410 set (INCLUDE_INSTALL_DIR include)
411 set (CONFIG_INSTALL_DIR ${LIBRARY_INSTALL_DIR}/cmake/${PACKAGE_NAME})
412endif ()
413
414# ----------------------------------------------------------------------------
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000415# add library targets
416set (TARGETS)
417# static vs. shared
418foreach (TYPE IN ITEMS STATIC SHARED)
419 if (BUILD_${TYPE}_LIBS)
Arnaud Farbosd78b00d2015-11-05 12:03:37 -0800420 string (TOLOWER "${TYPE}" type)
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000421 # whether or not targets are a DLL
422 if (OS_WINDOWS AND "^${TYPE}$" STREQUAL "^SHARED$")
423 set (GFLAGS_IS_A_DLL 1)
424 else ()
425 set (GFLAGS_IS_A_DLL 0)
426 endif ()
Andreas Schuh6bd636c2016-02-29 17:58:46 +0000427 # filename suffix for static libraries on Windows
428 if (OS_WINDOWS AND "^${TYPE}$" STREQUAL "^STATIC$")
429 set (type_suffix "_${type}")
430 else ()
431 set (type_suffix "")
432 endif ()
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000433 # multi-threaded vs. single-threaded
434 foreach (opts IN ITEMS "" _nothreads)
435 if (BUILD_gflags${opts}_LIB)
Andreas Schuh7a21c162016-03-01 21:04:10 +0000436 add_library (gflags${opts}_${type} ${TYPE} ${GFLAGS_SRCS} ${PRIVATE_HDRS} ${PUBLIC_HDRS})
Andreas Schuh58345b12015-04-03 16:12:38 +0100437 set (include_dirs "$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/include>")
438 if (INSTALL_HEADERS)
439 list (APPEND include_dirs "$<INSTALL_INTERFACE:${INCLUDE_INSTALL_DIR}>")
Andreas Schuh76c53b72015-03-24 14:44:12 +0000440 endif ()
Andreas Schuh7a21c162016-03-01 21:04:10 +0000441 target_include_directories (gflags${opts}_${type}
Andreas Schuh58345b12015-04-03 16:12:38 +0100442 PUBLIC "${include_dirs}"
443 PRIVATE "${PROJECT_SOURCE_DIR}/src;${PROJECT_BINARY_DIR}/include/${GFLAGS_INCLUDE_DIR}"
444 )
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000445 if (opts MATCHES "nothreads")
Sam Cleggb572a722016-01-29 16:09:43 -0800446 set (defines "GFLAGS_IS_A_DLL=${GFLAGS_IS_A_DLL};NO_THREADS")
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000447 else ()
448 set (defines "GFLAGS_IS_A_DLL=${GFLAGS_IS_A_DLL}")
Andreas Schuhbf139ea2014-03-20 03:04:44 +0000449 if (CMAKE_USE_PTHREADS_INIT)
Andreas Schuh7a21c162016-03-01 21:04:10 +0000450 target_link_libraries (gflags${opts}_${type} ${CMAKE_THREAD_LIBS_INIT})
Andreas Schuhbf139ea2014-03-20 03:04:44 +0000451 endif ()
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000452 endif ()
453 set_target_properties (
Andreas Schuh7a21c162016-03-01 21:04:10 +0000454 gflags${opts}_${type} PROPERTIES COMPILE_DEFINITIONS "${defines}"
Andreas Schuh6bd636c2016-02-29 17:58:46 +0000455 OUTPUT_NAME "gflags${opts}${type_suffix}"
Andreas Schuhcd7aece2014-05-02 11:12:05 +0100456 VERSION "${PACKAGE_VERSION}"
457 SOVERSION "${PACKAGE_SOVERSION}"
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000458 )
459 if (HAVE_SHLWAPI_H)
Andreas Schuh7a21c162016-03-01 21:04:10 +0000460 target_link_libraries (gflags${opts}_${type} shlwapi.lib)
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000461 endif ()
Andreas Schuh7a21c162016-03-01 21:04:10 +0000462 list (APPEND TARGETS gflags${opts}_${type})
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000463 # add convenience make target for build of both shared and static libraries
464 if (NOT GFLAGS_IS_SUBPROJECT)
465 if (NOT TARGET gflags${opts})
466 add_custom_target (gflags${opts})
467 endif ()
Andreas Schuh7a21c162016-03-01 21:04:10 +0000468 add_dependencies (gflags${opts} gflags${opts}_${type})
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000469 endif ()
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000470 endif ()
471 endforeach ()
Andreas Schuhddc53572014-03-19 17:32:23 +0000472 endif ()
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000473endforeach ()
unknown3d142b82014-03-19 15:27:08 +0100474
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000475# add ALIAS target for use in super-project, prefer static over shared, single-threaded over multi-threaded
476if (GFLAGS_IS_SUBPROJECT)
477 foreach (type IN ITEMS static shared)
478 foreach (opts IN ITEMS "_nothreads" "")
Andreas Schuh7a21c162016-03-01 21:04:10 +0000479 if (TARGET gflags${opts}_${type})
480 add_library (gflags ALIAS gflags${opts}_${type})
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000481 break ()
482 endif ()
483 endforeach ()
484 if (TARGET gflags)
485 break ()
486 endif ()
487 endforeach ()
488endif ()
489
unknown3d142b82014-03-19 15:27:08 +0100490# ----------------------------------------------------------------------------
Andreas Schuh76c53b72015-03-24 14:44:12 +0000491# installation rules
Andreas Schuh3064f632016-11-24 21:49:41 +0000492set (EXPORT_NAME ${PACKAGE_NAME}-targets)
unknown3d142b82014-03-19 15:27:08 +0100493file (RELATIVE_PATH INSTALL_PREFIX_REL2CONFIG_DIR "${CMAKE_INSTALL_PREFIX}/${CONFIG_INSTALL_DIR}" "${CMAKE_INSTALL_PREFIX}")
494configure_file (cmake/config.cmake.in "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-config-install.cmake" @ONLY)
495configure_file (cmake/version.cmake.in "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-config-version.cmake" @ONLY)
496
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000497if (BUILD_SHARED_LIBS AND INSTALL_SHARED_LIBS)
498 foreach (opts IN ITEMS "" _nothreads)
499 if (BUILD_gflags${opts}_LIB)
Andreas Schuh3064f632016-11-24 21:49:41 +0000500 install (TARGETS gflags${opts}_shared DESTINATION ${LIBRARY_INSTALL_DIR} EXPORT ${EXPORT_NAME})
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000501 endif ()
502 endforeach ()
503endif ()
504if (BUILD_STATIC_LIBS AND INSTALL_STATIC_LIBS)
505 foreach (opts IN ITEMS "" _nothreads)
506 if (BUILD_gflags${opts}_LIB)
Andreas Schuh3064f632016-11-24 21:49:41 +0000507 install (TARGETS gflags${opts}_static DESTINATION ${LIBRARY_INSTALL_DIR} EXPORT ${EXPORT_NAME})
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000508 endif ()
509 endforeach ()
510endif ()
511
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000512if (INSTALL_HEADERS)
Andreas Schuhaff8ef82014-03-27 01:09:20 +0000513 install (FILES ${PUBLIC_HDRS} DESTINATION ${INCLUDE_INSTALL_DIR}/${GFLAGS_INCLUDE_DIR})
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000514 install (
515 FILES "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-config-install.cmake"
516 RENAME ${PACKAGE_NAME}-config.cmake
517 DESTINATION ${CONFIG_INSTALL_DIR}
518 )
519 install (
520 FILES "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-config-version.cmake"
521 DESTINATION ${CONFIG_INSTALL_DIR}
522 )
Andreas Schuh3064f632016-11-24 21:49:41 +0000523 install (EXPORT ${EXPORT_NAME} DESTINATION ${CONFIG_INSTALL_DIR})
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000524 if (UNIX)
525 install (PROGRAMS src/gflags_completions.sh DESTINATION ${RUNTIME_INSTALL_DIR})
526 endif ()
unknown3d142b82014-03-19 15:27:08 +0100527endif ()
528
529# ----------------------------------------------------------------------------
530# support direct use of build tree
531set (INSTALL_PREFIX_REL2CONFIG_DIR .)
Andreas Schuh3064f632016-11-24 21:49:41 +0000532export (TARGETS ${TARGETS} FILE "${PROJECT_BINARY_DIR}/${EXPORT_NAME}.cmake")
Andreas Schuh3886da52016-11-24 22:36:48 +0000533if (REGISTER_BUILD_DIR)
534 export (PACKAGE ${PACKAGE_NAME})
535endif ()
536if (REGISTER_INSTALL_PREFIX)
537 register_gflags_package(${CONFIG_INSTALL_DIR})
538endif ()
unknown3d142b82014-03-19 15:27:08 +0100539configure_file (cmake/config.cmake.in "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-config.cmake" @ONLY)
540
541# ----------------------------------------------------------------------------
542# testing - MUST follow the generation of the build tree config file
unknown3d142b82014-03-19 15:27:08 +0100543if (BUILD_TESTING)
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000544 include (CTest)
unknown3d142b82014-03-19 15:27:08 +0100545 enable_testing ()
546 add_subdirectory (test)
547endif ()
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000548
549# ----------------------------------------------------------------------------
550# packaging
551if (BUILD_PACKAGING)
552
553 if (NOT BUILD_SHARED_LIBS AND NOT INSTALL_HEADERS)
554 message (WARNING "Package will contain static libraries without headers!"
555 "\nRecommended options for generation of runtime package:"
556 "\n BUILD_SHARED_LIBS=ON"
557 "\n BUILD_STATIC_LIBS=OFF"
558 "\n INSTALL_HEADERS=OFF"
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000559 "\n INSTALL_SHARED_LIBS=ON"
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000560 "\nRecommended options for generation of development package:"
561 "\n BUILD_SHARED_LIBS=ON"
562 "\n BUILD_STATIC_LIBS=ON"
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000563 "\n INSTALL_HEADERS=ON"
564 "\n INSTALL_SHARED_LIBS=ON"
565 "\n INSTALL_STATIC_LIBS=ON")
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000566 endif ()
567
568 # default package generators
569 if (APPLE)
570 set (PACKAGE_GENERATOR "PackageMaker")
571 set (PACKAGE_SOURCE_GENERATOR "TGZ;ZIP")
572 elseif (UNIX)
573 set (PACKAGE_GENERATOR "DEB;RPM")
574 set (PACKAGE_SOURCE_GENERATOR "TGZ;ZIP")
575 else ()
576 set (PACKAGE_GENERATOR "ZIP")
577 set (PACKAGE_SOURCE_GENERATOR "ZIP")
578 endif ()
579
580 # used package generators
581 set (CPACK_GENERATOR "${PACKAGE_GENERATOR}" CACHE STRING "List of binary package generators (CPack).")
582 set (CPACK_SOURCE_GENERATOR "${PACKAGE_SOURCE_GENERATOR}" CACHE STRING "List of source package generators (CPack).")
583 mark_as_advanced (CPACK_GENERATOR CPACK_SOURCE_GENERATOR)
584
Andreas Schuh64ac2e32015-03-24 16:21:44 +0000585 # some package generators (e.g., PackageMaker) do not allow .md extension
586 configure_file ("${CMAKE_CURRENT_LIST_DIR}/README.md" "${CMAKE_CURRENT_BINARY_DIR}/README.txt" COPYONLY)
587
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000588 # common package information
Andreas Schuhfc6e0792014-03-20 04:09:02 +0000589 set (CPACK_PACKAGE_VENDOR "Andreas Schuh")
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000590 set (CPACK_PACKAGE_CONTACT "google-gflags@googlegroups.com")
591 set (CPACK_PACKAGE_NAME "${PACKAGE_NAME}")
592 set (CPACK_PACKAGE_VERSION "${PACKAGE_VERSION}")
593 set (CPACK_PACKAGE_VERSION_MAJOR "${PACKAGE_VERSION_MAJOR}")
594 set (CPACK_PACKAGE_VERSION_MINOR "${PACKAGE_VERSION_MINOR}")
595 set (CPACK_PACKAGE_VERSION_PATCH "${PACKAGE_VERSION_PATCH}")
596 set (CPACK_PACKAGE_DESCRIPTION_SUMMARY "A commandline flags library that allows for distributed flags.")
Andreas Schuha819f0f2015-03-24 16:29:31 +0000597 set (CPACK_RESOURCE_FILE_WELCOME "${CMAKE_CURRENT_BINARY_DIR}/README.txt")
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000598 set (CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_LIST_DIR}/COPYING.txt")
Andreas Schuha819f0f2015-03-24 16:29:31 +0000599 set (CPACK_PACKAGE_DESCRIPTION_FILE "${CMAKE_CURRENT_BINARY_DIR}/README.txt")
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000600 set (CPACK_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}")
601 set (CPACK_OUTPUT_FILE_PREFIX packages)
602 set (CPACK_PACKAGE_RELOCATABLE TRUE)
603 set (CPACK_MONOLITHIC_INSTALL TRUE)
604
Andreas Schuhe8890f22014-03-20 04:20:15 +0000605 # RPM package information -- used in cmake/package.cmake.in also for DEB
Andreas Schuh37a9a902014-03-20 03:27:13 +0000606 set (CPACK_RPM_PACKAGE_GROUP "Development/Libraries")
607 set (CPACK_RPM_PACKAGE_LICENSE "BSD")
Andreas Schuhabe4b102016-03-01 16:01:45 +0000608 set (CPACK_RPM_PACKAGE_URL "http://gflags.github.io/gflags")
Andreas Schuh37a9a902014-03-20 03:27:13 +0000609 set (CPACK_RPM_CHANGELOG_FILE "${CMAKE_CURRENT_LIST_DIR}/ChangeLog.txt")
610
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000611 if (INSTALL_HEADERS)
Andreas Schuh2b8deaa2015-03-24 14:41:52 +0000612 set (CPACK_RESOURCE_FILE_README "${CMAKE_CURRENT_LIST_DIR}/doc/index.html")
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000613 else ()
614 set (CPACK_RESOURCE_FILE_README "${CMAKE_CURRENT_LIST_DIR}/cmake/README_runtime.txt")
615 endif ()
616
Andreas Schuhd6995592014-03-20 20:21:30 +0000617 # system/architecture
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000618 if (WINDOWS)
619 if (CMAKE_CL_64)
620 set (CPACK_SYSTEM_NAME "win64")
621 else ()
622 set (CPACK_SYSTEM_NAME "win32")
623 endif ()
Andreas Schuhd6995592014-03-20 20:21:30 +0000624 set (CPACK_PACKAGE_ARCHITECTURE)
Andreas Schuhef5c5472014-03-30 15:34:13 +0100625 elseif (APPLE)
626 set (CPACK_PACKAGE_ARCHITECTURE darwin)
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000627 else ()
628 string (TOLOWER "${CMAKE_SYSTEM_NAME}" CPACK_SYSTEM_NAME)
Andreas Schuhcf40f732014-03-20 20:56:05 +0000629 if (CMAKE_CXX_FLAGS MATCHES "-m32")
630 set (CPACK_PACKAGE_ARCHITECTURE i386)
631 else ()
632 execute_process (
633 COMMAND dpkg --print-architecture
634 RESULT_VARIABLE RV
635 OUTPUT_VARIABLE CPACK_PACKAGE_ARCHITECTURE
636 )
637 if (RV EQUAL 0)
638 string (STRIP "${CPACK_PACKAGE_ARCHITECTURE}" CPACK_PACKAGE_ARCHITECTURE)
639 else ()
640 execute_process (COMMAND uname -m OUTPUT_VARIABLE CPACK_PACKAGE_ARCHITECTURE)
641 if (CPACK_PACKAGE_ARCHITECTURE MATCHES "x86_64")
642 set (CPACK_PACKAGE_ARCHITECTURE amd64)
643 else ()
644 set (CPACK_PACKAGE_ARCHITECTURE i386)
645 endif ()
646 endif ()
647 endif ()
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000648 endif ()
649
650 # source package settings
651 set (CPACK_SOURCE_TOPLEVEL_TAG "source")
652 set (CPACK_SOURCE_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}")
653 set (CPACK_SOURCE_IGNORE_FILES "/\\\\.git/;\\\\.swp$;\\\\.#;/#;\\\\.*~;cscope\\\\.*;/[Bb]uild[.+-_a-zA-Z0-9]*/")
654
655 # default binary package settings
Andreas Schuhd6995592014-03-20 20:21:30 +0000656 set (CPACK_INCLUDE_TOPLEVEL_DIRECTORY TRUE)
657 set (CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}-${CPACK_SYSTEM_NAME}")
658 if (CPACK_PACKAGE_ARCHITECTURE)
659 set (CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_FILE_NAME}-${CPACK_PACKAGE_ARCHITECTURE}")
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000660 endif ()
661
662 # generator specific configuration file
663 #
664 # allow package maintainers to use their own configuration file
665 # $ cmake -DCPACK_PROJECT_CONFIG_FILE:FILE=/path/to/package/config
666 if (NOT CPACK_PROJECT_CONFIG_FILE)
667 configure_file (
668 "${CMAKE_CURRENT_LIST_DIR}/cmake/package.cmake.in"
669 "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-package.cmake" @ONLY
670 )
671 set (CPACK_PROJECT_CONFIG_FILE "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-package.cmake")
672 endif ()
673
674 include (CPack)
675
676endif () # BUILD_PACKAGING