blob: 68f9ea3ebc04d3c710fbaf536e9acc0a0a5fecb1 [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 ()
114 set (PACKAGE_SOVERSION "${PACKAGE_VERSION_MAJOR}")
115endif ()
116
117# when gflags is included as subproject (e.g., as Git submodule/subtree) in the source
118# tree of a project that uses it, no variables should be added to the CMake cache;
119# users may set the non-cached variable GFLAGS_IS_SUBPROJECT before add_subdirectory(gflags)
120if (NOT DEFINED GFLAGS_IS_SUBPROJECT)
121 if ("^${CMAKE_SOURCE_DIR}$" STREQUAL "^${PROJECT_SOURCE_DIR}$")
122 set (GFLAGS_IS_SUBPROJECT FALSE)
123 else ()
124 set (GFLAGS_IS_SUBPROJECT TRUE)
125 endif ()
126endif ()
127
128# prefix for package variables in CMake configuration file
129string (TOUPPER "${PACKAGE_NAME}" PACKAGE_PREFIX)
Andreas Schuhbf889782014-05-01 20:16:16 +0100130
unknown3d142b82014-03-19 15:27:08 +0100131# ----------------------------------------------------------------------------
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000132# options
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000133
134# maintain binary backwards compatibility with gflags library version <= 2.0,
135# but at the same time enable the use of the preferred new "gflags" namespace
136gflags_define (STRING NAMESPACE "Name(s) of library namespace (separate multiple options by semicolon)" "google;${PACKAGE_NAME}" "${PACKAGE_NAME}")
137gflags_property (NAMESPACE ADVANCED TRUE)
138set (GFLAGS_NAMESPACE_SECONDARY "${NAMESPACE}")
Andreas Schuh659b4402014-05-02 14:56:58 +0100139list (REMOVE_DUPLICATES GFLAGS_NAMESPACE_SECONDARY)
140if (NOT GFLAGS_NAMESPACE_SECONDARY)
141 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 +0000142endif ()
Andreas Schuh659b4402014-05-02 14:56:58 +0100143foreach (ns IN LISTS GFLAGS_NAMESPACE_SECONDARY)
144 if (NOT ns MATCHES "^[a-zA-Z][a-zA-Z0-9_]*$")
145 message (FATAL_ERROR "GFLAGS_NAMESPACE contains invalid namespace identifier: ${ns}")
146 endif ()
147endforeach ()
148list (GET GFLAGS_NAMESPACE_SECONDARY 0 GFLAGS_NAMESPACE)
149list (REMOVE_AT GFLAGS_NAMESPACE_SECONDARY 0)
unknown3d142b82014-03-19 15:27:08 +0100150
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000151# cached build options when gflags is not a subproject, otherwise non-cached CMake variables
152# usage: gflags_define(BOOL <name> <doc> <default> [<subproject default>])
153gflags_define (BOOL BUILD_SHARED_LIBS "Request build of shared libraries." OFF OFF)
154gflags_define (BOOL BUILD_STATIC_LIBS "Request build of static libraries (default if BUILD_SHARED_LIBS is OFF)." OFF ON)
155gflags_define (BOOL BUILD_gflags_LIB "Request build of the multi-threaded gflags library." ON OFF)
156gflags_define (BOOL BUILD_gflags_nothreads_LIB "Request build of the single-threaded gflags library." ON ON)
157gflags_define (BOOL BUILD_PACKAGING "Enable build of distribution packages using CPack." OFF OFF)
158gflags_define (BOOL BUILD_TESTING "Enable build of the unit tests and their execution using CTest." OFF OFF)
159gflags_define (BOOL INSTALL_HEADERS "Request installation of headers and other development files." ON OFF)
160gflags_define (BOOL INSTALL_SHARED_LIBS "Request installation of shared libraries." ON ON)
161gflags_define (BOOL INSTALL_STATIC_LIBS "Request installation of static libraries." ON OFF)
unknown3d142b82014-03-19 15:27:08 +0100162
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000163gflags_property (BUILD_STATIC_LIBS ADVANCED TRUE)
164gflags_property (INSTALL_HEADERS ADVANCED TRUE)
165gflags_property (INSTALL_SHARED_LIBS ADVANCED TRUE)
166gflags_property (INSTALL_STATIC_LIBS ADVANCED TRUE)
167
168if (NOT GFLAGS_IS_SUBPROJECT)
169 foreach (varname IN ITEMS CMAKE_INSTALL_PREFIX)
170 gflags_property (${varname} ADVANCED FALSE)
171 endforeach ()
172 foreach (varname IN ITEMS CMAKE_CONFIGURATION_TYPES CMAKE_OSX_ARCHITECTURES CMAKE_OSX_DEPLOYMENT_TARGET CMAKE_OSX_SYSROOT)
173 gflags_property (${varname} ADVANCED TRUE)
174 endforeach ()
175 if (NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CXX_FLAGS)
176 gflags_set (CMAKE_BUILD_TYPE Release)
177 endif ()
178 if (CMAKE_CONFIGURATION_TYPES)
179 gflags_property (CMAKE_BUILD_TYPE STRINGS "${CMAKE_CONFIGURATION_TYPES}")
180 endif ()
181endif () # NOT GFLAGS_IS_SUBPROJECT
unknown3d142b82014-03-19 15:27:08 +0100182
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000183if (NOT BUILD_SHARED_LIBS AND NOT BUILD_STATIC_LIBS)
184 set (BUILD_STATIC_LIBS ON)
185endif ()
186if (NOT BUILD_gflags_LIB AND NOT BUILD_gflags_nothreads_LIB)
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000187 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 +0000188endif ()
189
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000190gflags_define (STRING INCLUDE_DIR "Name of include directory of installed header files relative to CMAKE_INSTALL_PREFIX/include/" "${PACKAGE_NAME}")
191gflags_property (INCLUDE_DIR ADVANCED TRUE)
192if (IS_ABSOLUTE INCLUDE_DIR)
193 message (FATAL_ERROR "[GFLAGS_]INCLUDE_DIR must be a path relative to CMAKE_INSTALL_PREFIX/include/")
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000194endif ()
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000195if (INCLUDE_DIR MATCHES "^\\.\\.[/\\]")
196 message (FATAL_ERROR "[GFLAGS_]INCLUDE_DIR must not start with parent directory reference (../)")
Andreas Schuh659b4402014-05-02 14:56:58 +0100197endif ()
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000198set (GFLAGS_INCLUDE_DIR "${INCLUDE_DIR}")
Andreas Schuh659b4402014-05-02 14:56:58 +0100199
unknown3d142b82014-03-19 15:27:08 +0100200# ----------------------------------------------------------------------------
201# system checks
202include (CheckTypeSize)
203include (CheckIncludeFileCXX)
204include (CheckCXXSymbolExists)
205
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000206if (WIN32 AND NOT CYGWIN)
207 set (OS_WINDOWS 1)
208else ()
209 set (OS_WINDOWS 0)
210endif ()
211
unknown3d142b82014-03-19 15:27:08 +0100212if (MSVC)
213 set (HAVE_SYS_TYPES_H 1)
214 set (HAVE_STDINT_H 1)
215 set (HAVE_STDDEF_H 1) # used by CheckTypeSize module
216 set (HAVE_INTTYPES_H 0)
217 set (HAVE_UNISTD_H 0)
218 set (HAVE_SYS_STAT_H 1)
Andreas Schuh92425782014-03-19 17:15:36 +0000219 set (HAVE_SHLWAPI_H 1)
unknown3d142b82014-03-19 15:27:08 +0100220else ()
221 foreach (fname IN ITEMS unistd stdint inttypes sys/types sys/stat fnmatch)
222 string (TOUPPER "${fname}" FNAME)
Andreas Schuh92425782014-03-19 17:15:36 +0000223 string (REPLACE "/" "_" FNAME "${FNAME}")
unknown3d142b82014-03-19 15:27:08 +0100224 if (NOT HAVE_${FNAME}_H)
225 check_include_file_cxx ("${fname}.h" HAVE_${FNAME}_H)
226 endif ()
227 endforeach ()
Andreas Schuhddc53572014-03-19 17:32:23 +0000228 # the following are used in #if directives not #ifdef
unknown3d142b82014-03-19 15:27:08 +0100229 bool_to_int (HAVE_STDINT_H)
230 bool_to_int (HAVE_SYS_TYPES_H)
231 bool_to_int (HAVE_INTTYPES_H)
Andreas Schuh92425782014-03-19 17:15:36 +0000232 if (NOT HAVE_FNMATCH_H AND OS_WINDOWS)
233 check_include_file_cxx ("shlwapi.h" HAVE_SHLWAPI_H)
234 endif ()
unknown3d142b82014-03-19 15:27:08 +0100235endif ()
236
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000237gflags_define (STRING INTTYPES_FORMAT "Format of integer types: \"C99\" (uint32_t), \"BSD\" (u_int32_t), \"VC7\" (__int32)" "")
238gflags_property (INTTYPES_FORMAT STRINGS "C99;BSD;VC7")
239gflags_property (INTTYPES_FORMAT ADVANCED TRUE)
240if (NOT INTTYPES_FORMAT)
unknown3d142b82014-03-19 15:27:08 +0100241 set (TYPES uint32_t u_int32_t)
242 if (MSVC)
243 list (INSERT TYPES 0 __int32)
244 endif ()
245 foreach (type IN LISTS TYPES)
246 check_type_size (${type} ${type} LANGUAGE CXX)
247 if (HAVE_${type})
248 break ()
249 endif ()
250 endforeach ()
251 if (HAVE_uint32_t)
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000252 gflags_set (INTTYPES_FORMAT C99)
unknown3d142b82014-03-19 15:27:08 +0100253 elseif (HAVE_u_int32_t)
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000254 gflags_set (INTTYPES_FORMAT BSD)
unknown3d142b82014-03-19 15:27:08 +0100255 elseif (HAVE___int32)
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000256 gflags_set (INTTYPES_FORMAT VC7)
unknown3d142b82014-03-19 15:27:08 +0100257 else ()
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000258 gflags_property (INTTYPES_FORMAT ADVANCED FALSE)
unknown3d142b82014-03-19 15:27:08 +0100259 message (FATAL_ERROR "Do not know how to define a 32-bit integer quantity on your system!"
260 " Neither uint32_t, u_int32_t, nor __int32 seem to be available."
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000261 " Set [GFLAGS_]INTTYPES_FORMAT to either C99, BSD, or VC7 and try again.")
unknown3d142b82014-03-19 15:27:08 +0100262 endif ()
263endif ()
264# use of special characters in strings to circumvent bug #0008226
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000265if ("^${INTTYPES_FORMAT}$" STREQUAL "^WIN$")
266 gflags_set (INTTYPES_FORMAT VC7)
unknown3d142b82014-03-19 15:27:08 +0100267endif ()
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000268if (NOT INTTYPES_FORMAT MATCHES "^(C99|BSD|VC7)$")
269 message (FATAL_ERROR "Invalid value for [GFLAGS_]INTTYPES_FORMAT! Choose one of \"C99\", \"BSD\", or \"VC7\"")
unknown3d142b82014-03-19 15:27:08 +0100270endif ()
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000271set (GFLAGS_INTTYPES_FORMAT "${INTTYPES_FORMAT}")
unknown3d142b82014-03-19 15:27:08 +0100272set (GFLAGS_INTTYPES_FORMAT_C99 0)
273set (GFLAGS_INTTYPES_FORMAT_BSD 0)
274set (GFLAGS_INTTYPES_FORMAT_VC7 0)
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000275set ("GFLAGS_INTTYPES_FORMAT_${INTTYPES_FORMAT}" 1)
unknown3d142b82014-03-19 15:27:08 +0100276
277if (MSVC)
278 set (HAVE_strtoll 0)
279 set (HAVE_strtoq 0)
280else ()
Andreas Schuh0b116eb2014-03-19 15:56:26 +0000281 check_cxx_symbol_exists (strtoll stdlib.h HAVE_STRTOLL)
282 if (NOT HAVE_STRTOLL)
283 check_cxx_symbol_exists (strtoq stdlib.h HAVE_STRTOQ)
284 endif ()
unknown3d142b82014-03-19 15:27:08 +0100285endif ()
286
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000287if (BUILD_gflags_LIB)
288 set (CMAKE_THREAD_PREFER_PTHREAD TRUE)
289 find_package (Threads)
290 if (Threads_FOUND AND CMAKE_USE_PTHREADS_INIT)
291 set (HAVE_PTHREAD 1)
292 check_type_size (pthread_rwlock_t RWLOCK LANGUAGE CXX)
293 else ()
294 set (HAVE_PTHREAD 0)
295 endif ()
296 if (UNIX AND NOT HAVE_PTHREAD)
297 if (CMAKE_HAVE_PTHREAD_H)
298 set (what "library")
299 else ()
300 set (what ".h file")
301 endif ()
302 message (FATAL_ERROR "Could not find pthread${what}. Check the log file"
303 "\n\t${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log"
304 "\nor disable the build of the multi-threaded gflags library (BUILD_gflags_LIB=OFF).")
305 endif ()
unknown3d142b82014-03-19 15:27:08 +0100306else ()
307 set (HAVE_PTHREAD 0)
308endif ()
309
unknown3d142b82014-03-19 15:27:08 +0100310# ----------------------------------------------------------------------------
311# source files - excluding root subdirectory and/or .in suffix
312set (PUBLIC_HDRS
313 "gflags.h"
314 "gflags_declare.h"
315 "gflags_completions.h"
316)
317
Andreas Schuh659b4402014-05-02 14:56:58 +0100318if (GFLAGS_NAMESPACE_SECONDARY)
319 set (INCLUDE_GFLAGS_NS_H "// Import gflags library symbols into alternative/deprecated namespace(s)")
320 foreach (ns IN LISTS GFLAGS_NAMESPACE_SECONDARY)
321 string (TOUPPER "${ns}" NS)
322 set (gflags_ns_h "${PROJECT_BINARY_DIR}/include/${GFLAGS_INCLUDE_DIR}/gflags_${ns}.h")
323 configure_file ("${PROJECT_SOURCE_DIR}/src/gflags_ns.h.in" "${gflags_ns_h}" @ONLY)
324 list (APPEND PUBLIC_HDRS "${gflags_ns_h}")
325 set (INCLUDE_GFLAGS_NS_H "${INCLUDE_GFLAGS_NS_H}\n#include \"gflags_${ns}.h\"")
326 endforeach ()
327else ()
328 set (INCLUDE_GFLAGS_NS_H)
329endif ()
330
unknown3d142b82014-03-19 15:27:08 +0100331set (PRIVATE_HDRS
332 "config.h"
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000333 "util.h"
334 "mutex.h"
unknown3d142b82014-03-19 15:27:08 +0100335)
336
337set (GFLAGS_SRCS
338 "gflags.cc"
339 "gflags_reporting.cc"
340 "gflags_completions.cc"
341)
342
Andreas Schuh41181662014-03-19 16:46:56 +0000343if (OS_WINDOWS)
unknown3d142b82014-03-19 15:27:08 +0100344 list (APPEND PRIVATE_HDRS "windows_port.h")
345 list (APPEND GFLAGS_SRCS "windows_port.cc")
346endif ()
347
348# ----------------------------------------------------------------------------
349# configure source files
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000350if (NOT DEFINED GFLAGS_ATTRIBUTE_UNUSED)
351 if (CMAKE_COMPILER_IS_GNUCXX)
352 set (GFLAGS_ATTRIBUTE_UNUSED "__attribute((unused))")
353 else ()
354 set (GFLAGS_ATTRIBUTE_UNUSED)
355 endif ()
unknown3d142b82014-03-19 15:27:08 +0100356endif ()
357
Andreas Schuh6040eac2014-03-25 15:29:56 +0000358# whenever we build a shared library (DLL on Windows), configure the public
359# headers of the API for use of this library rather than the optionally
360# also build statically linked library; users can override GFLAGS_DLL_DECL
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000361if (NOT DEFINED GFLAGS_IS_A_DLL)
362 if (BUILD_SHARED_LIBS)
363 set (GFLAGS_IS_A_DLL 1)
364 else ()
365 set (GFLAGS_IS_A_DLL 0)
366 endif ()
Andreas Schuh6040eac2014-03-25 15:29:56 +0000367endif ()
368
unknown3d142b82014-03-19 15:27:08 +0100369configure_headers (PUBLIC_HDRS ${PUBLIC_HDRS})
370configure_sources (PRIVATE_HDRS ${PRIVATE_HDRS})
371configure_sources (GFLAGS_SRCS ${GFLAGS_SRCS})
372
373# ----------------------------------------------------------------------------
374# output directories
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000375if (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")
379endif ()
unknown3d142b82014-03-19 15:27:08 +0100380
381# ----------------------------------------------------------------------------
Andreas Schuh76c53b72015-03-24 14:44:12 +0000382# installation directories
383if (OS_WINDOWS)
384 set (RUNTIME_INSTALL_DIR Bin)
385 set (LIBRARY_INSTALL_DIR Lib)
386 set (INCLUDE_INSTALL_DIR Include)
387 set (CONFIG_INSTALL_DIR CMake)
388else ()
389 set (RUNTIME_INSTALL_DIR bin)
390 # The LIB_INSTALL_DIR and LIB_SUFFIX variables are used by the Fedora
391 # package maintainers. Also package maintainers of other distribution
392 # packages need to be able to specify the name of the library directory.
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000393 if (NOT GFLAGS_LIBRARY_INSTALL_DIR AND LIB_INSTALL_DIR)
394 set (GFLAGS_LIBRARY_INSTALL_DIR "${LIB_INSTALL_DIR}")
Andreas Schuh76c53b72015-03-24 14:44:12 +0000395 endif ()
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000396 gflags_define (PATH LIBRARY_INSTALL_DIR "Directory of installed libraries, e.g., \"lib64\"" "lib${LIB_SUFFIX}")
397 gflags_property (LIBRARY_INSTALL_DIR ADVANCED TRUE)
Andreas Schuh76c53b72015-03-24 14:44:12 +0000398 set (INCLUDE_INSTALL_DIR include)
399 set (CONFIG_INSTALL_DIR ${LIBRARY_INSTALL_DIR}/cmake/${PACKAGE_NAME})
400endif ()
401
402# ----------------------------------------------------------------------------
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000403# add library targets
404set (TARGETS)
405# static vs. shared
406foreach (TYPE IN ITEMS STATIC SHARED)
407 if (BUILD_${TYPE}_LIBS)
Arnaud Farbosd78b00d2015-11-05 12:03:37 -0800408 string (TOLOWER "${TYPE}" type)
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000409 # whether or not targets are a DLL
410 if (OS_WINDOWS AND "^${TYPE}$" STREQUAL "^SHARED$")
411 set (GFLAGS_IS_A_DLL 1)
412 else ()
413 set (GFLAGS_IS_A_DLL 0)
414 endif ()
Andreas Schuh6bd636c2016-02-29 17:58:46 +0000415 # filename suffix for static libraries on Windows
416 if (OS_WINDOWS AND "^${TYPE}$" STREQUAL "^STATIC$")
417 set (type_suffix "_${type}")
418 else ()
419 set (type_suffix "")
420 endif ()
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000421 # multi-threaded vs. single-threaded
422 foreach (opts IN ITEMS "" _nothreads)
423 if (BUILD_gflags${opts}_LIB)
Andreas Schuh7a21c162016-03-01 21:04:10 +0000424 add_library (gflags${opts}_${type} ${TYPE} ${GFLAGS_SRCS} ${PRIVATE_HDRS} ${PUBLIC_HDRS})
Andreas Schuh58345b12015-04-03 16:12:38 +0100425 set (include_dirs "$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/include>")
426 if (INSTALL_HEADERS)
427 list (APPEND include_dirs "$<INSTALL_INTERFACE:${INCLUDE_INSTALL_DIR}>")
Andreas Schuh76c53b72015-03-24 14:44:12 +0000428 endif ()
Andreas Schuh7a21c162016-03-01 21:04:10 +0000429 target_include_directories (gflags${opts}_${type}
Andreas Schuh58345b12015-04-03 16:12:38 +0100430 PUBLIC "${include_dirs}"
431 PRIVATE "${PROJECT_SOURCE_DIR}/src;${PROJECT_BINARY_DIR}/include/${GFLAGS_INCLUDE_DIR}"
432 )
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000433 if (opts MATCHES "nothreads")
Sam Cleggb572a722016-01-29 16:09:43 -0800434 set (defines "GFLAGS_IS_A_DLL=${GFLAGS_IS_A_DLL};NO_THREADS")
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000435 else ()
436 set (defines "GFLAGS_IS_A_DLL=${GFLAGS_IS_A_DLL}")
Andreas Schuhbf139ea2014-03-20 03:04:44 +0000437 if (CMAKE_USE_PTHREADS_INIT)
Andreas Schuh7a21c162016-03-01 21:04:10 +0000438 target_link_libraries (gflags${opts}_${type} ${CMAKE_THREAD_LIBS_INIT})
Andreas Schuhbf139ea2014-03-20 03:04:44 +0000439 endif ()
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000440 endif ()
441 set_target_properties (
Andreas Schuh7a21c162016-03-01 21:04:10 +0000442 gflags${opts}_${type} PROPERTIES COMPILE_DEFINITIONS "${defines}"
Andreas Schuh6bd636c2016-02-29 17:58:46 +0000443 OUTPUT_NAME "gflags${opts}${type_suffix}"
Andreas Schuhcd7aece2014-05-02 11:12:05 +0100444 VERSION "${PACKAGE_VERSION}"
445 SOVERSION "${PACKAGE_SOVERSION}"
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000446 )
447 if (HAVE_SHLWAPI_H)
Andreas Schuh7a21c162016-03-01 21:04:10 +0000448 target_link_libraries (gflags${opts}_${type} shlwapi.lib)
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000449 endif ()
Andreas Schuh7a21c162016-03-01 21:04:10 +0000450 list (APPEND TARGETS gflags${opts}_${type})
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000451 # add convenience make target for build of both shared and static libraries
452 if (NOT GFLAGS_IS_SUBPROJECT)
453 if (NOT TARGET gflags${opts})
454 add_custom_target (gflags${opts})
455 endif ()
Andreas Schuh7a21c162016-03-01 21:04:10 +0000456 add_dependencies (gflags${opts} gflags${opts}_${type})
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000457 endif ()
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000458 endif ()
459 endforeach ()
Andreas Schuhddc53572014-03-19 17:32:23 +0000460 endif ()
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000461endforeach ()
unknown3d142b82014-03-19 15:27:08 +0100462
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000463# add ALIAS target for use in super-project, prefer static over shared, single-threaded over multi-threaded
464if (GFLAGS_IS_SUBPROJECT)
465 foreach (type IN ITEMS static shared)
466 foreach (opts IN ITEMS "_nothreads" "")
Andreas Schuh7a21c162016-03-01 21:04:10 +0000467 if (TARGET gflags${opts}_${type})
468 add_library (gflags ALIAS gflags${opts}_${type})
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000469 break ()
470 endif ()
471 endforeach ()
472 if (TARGET gflags)
473 break ()
474 endif ()
475 endforeach ()
476endif ()
477
unknown3d142b82014-03-19 15:27:08 +0100478# ----------------------------------------------------------------------------
Andreas Schuh76c53b72015-03-24 14:44:12 +0000479# installation rules
unknown3d142b82014-03-19 15:27:08 +0100480file (RELATIVE_PATH INSTALL_PREFIX_REL2CONFIG_DIR "${CMAKE_INSTALL_PREFIX}/${CONFIG_INSTALL_DIR}" "${CMAKE_INSTALL_PREFIX}")
481configure_file (cmake/config.cmake.in "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-config-install.cmake" @ONLY)
482configure_file (cmake/version.cmake.in "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-config-version.cmake" @ONLY)
483
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000484if (BUILD_SHARED_LIBS AND INSTALL_SHARED_LIBS)
485 foreach (opts IN ITEMS "" _nothreads)
486 if (BUILD_gflags${opts}_LIB)
Andreas Schuh7a21c162016-03-01 21:04:10 +0000487 install (TARGETS gflags${opts}_shared DESTINATION ${LIBRARY_INSTALL_DIR} EXPORT gflags-lib)
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000488 endif ()
489 endforeach ()
490endif ()
491if (BUILD_STATIC_LIBS AND INSTALL_STATIC_LIBS)
492 foreach (opts IN ITEMS "" _nothreads)
493 if (BUILD_gflags${opts}_LIB)
Andreas Schuh7a21c162016-03-01 21:04:10 +0000494 install (TARGETS gflags${opts}_static DESTINATION ${LIBRARY_INSTALL_DIR} EXPORT gflags-lib)
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000495 endif ()
496 endforeach ()
497endif ()
498
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000499if (INSTALL_HEADERS)
Andreas Schuhaff8ef82014-03-27 01:09:20 +0000500 install (FILES ${PUBLIC_HDRS} DESTINATION ${INCLUDE_INSTALL_DIR}/${GFLAGS_INCLUDE_DIR})
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000501 install (
502 FILES "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-config-install.cmake"
503 RENAME ${PACKAGE_NAME}-config.cmake
504 DESTINATION ${CONFIG_INSTALL_DIR}
505 )
506 install (
507 FILES "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-config-version.cmake"
508 DESTINATION ${CONFIG_INSTALL_DIR}
509 )
Andreas Schuh7a21c162016-03-01 21:04:10 +0000510 install (EXPORT gflags-lib DESTINATION ${CONFIG_INSTALL_DIR} FILE ${PACKAGE_NAME}-targets.cmake)
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000511 if (UNIX)
512 install (PROGRAMS src/gflags_completions.sh DESTINATION ${RUNTIME_INSTALL_DIR})
513 endif ()
unknown3d142b82014-03-19 15:27:08 +0100514endif ()
515
516# ----------------------------------------------------------------------------
517# support direct use of build tree
518set (INSTALL_PREFIX_REL2CONFIG_DIR .)
Andreas Schuh7a21c162016-03-01 21:04:10 +0000519export (TARGETS ${TARGETS} FILE "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-targets.cmake")
unknown3d142b82014-03-19 15:27:08 +0100520export (PACKAGE gflags)
521configure_file (cmake/config.cmake.in "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-config.cmake" @ONLY)
522
523# ----------------------------------------------------------------------------
524# testing - MUST follow the generation of the build tree config file
unknown3d142b82014-03-19 15:27:08 +0100525if (BUILD_TESTING)
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000526 include (CTest)
unknown3d142b82014-03-19 15:27:08 +0100527 enable_testing ()
528 add_subdirectory (test)
529endif ()
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000530
531# ----------------------------------------------------------------------------
532# packaging
533if (BUILD_PACKAGING)
534
535 if (NOT BUILD_SHARED_LIBS AND NOT INSTALL_HEADERS)
536 message (WARNING "Package will contain static libraries without headers!"
537 "\nRecommended options for generation of runtime package:"
538 "\n BUILD_SHARED_LIBS=ON"
539 "\n BUILD_STATIC_LIBS=OFF"
540 "\n INSTALL_HEADERS=OFF"
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000541 "\n INSTALL_SHARED_LIBS=ON"
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000542 "\nRecommended options for generation of development package:"
543 "\n BUILD_SHARED_LIBS=ON"
544 "\n BUILD_STATIC_LIBS=ON"
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000545 "\n INSTALL_HEADERS=ON"
546 "\n INSTALL_SHARED_LIBS=ON"
547 "\n INSTALL_STATIC_LIBS=ON")
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000548 endif ()
549
550 # default package generators
551 if (APPLE)
552 set (PACKAGE_GENERATOR "PackageMaker")
553 set (PACKAGE_SOURCE_GENERATOR "TGZ;ZIP")
554 elseif (UNIX)
555 set (PACKAGE_GENERATOR "DEB;RPM")
556 set (PACKAGE_SOURCE_GENERATOR "TGZ;ZIP")
557 else ()
558 set (PACKAGE_GENERATOR "ZIP")
559 set (PACKAGE_SOURCE_GENERATOR "ZIP")
560 endif ()
561
562 # used package generators
563 set (CPACK_GENERATOR "${PACKAGE_GENERATOR}" CACHE STRING "List of binary package generators (CPack).")
564 set (CPACK_SOURCE_GENERATOR "${PACKAGE_SOURCE_GENERATOR}" CACHE STRING "List of source package generators (CPack).")
565 mark_as_advanced (CPACK_GENERATOR CPACK_SOURCE_GENERATOR)
566
Andreas Schuh64ac2e32015-03-24 16:21:44 +0000567 # some package generators (e.g., PackageMaker) do not allow .md extension
568 configure_file ("${CMAKE_CURRENT_LIST_DIR}/README.md" "${CMAKE_CURRENT_BINARY_DIR}/README.txt" COPYONLY)
569
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000570 # common package information
Andreas Schuhfc6e0792014-03-20 04:09:02 +0000571 set (CPACK_PACKAGE_VENDOR "Andreas Schuh")
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000572 set (CPACK_PACKAGE_CONTACT "google-gflags@googlegroups.com")
573 set (CPACK_PACKAGE_NAME "${PACKAGE_NAME}")
574 set (CPACK_PACKAGE_VERSION "${PACKAGE_VERSION}")
575 set (CPACK_PACKAGE_VERSION_MAJOR "${PACKAGE_VERSION_MAJOR}")
576 set (CPACK_PACKAGE_VERSION_MINOR "${PACKAGE_VERSION_MINOR}")
577 set (CPACK_PACKAGE_VERSION_PATCH "${PACKAGE_VERSION_PATCH}")
578 set (CPACK_PACKAGE_DESCRIPTION_SUMMARY "A commandline flags library that allows for distributed flags.")
Andreas Schuha819f0f2015-03-24 16:29:31 +0000579 set (CPACK_RESOURCE_FILE_WELCOME "${CMAKE_CURRENT_BINARY_DIR}/README.txt")
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000580 set (CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_LIST_DIR}/COPYING.txt")
Andreas Schuha819f0f2015-03-24 16:29:31 +0000581 set (CPACK_PACKAGE_DESCRIPTION_FILE "${CMAKE_CURRENT_BINARY_DIR}/README.txt")
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000582 set (CPACK_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}")
583 set (CPACK_OUTPUT_FILE_PREFIX packages)
584 set (CPACK_PACKAGE_RELOCATABLE TRUE)
585 set (CPACK_MONOLITHIC_INSTALL TRUE)
586
Andreas Schuhe8890f22014-03-20 04:20:15 +0000587 # RPM package information -- used in cmake/package.cmake.in also for DEB
Andreas Schuh37a9a902014-03-20 03:27:13 +0000588 set (CPACK_RPM_PACKAGE_GROUP "Development/Libraries")
589 set (CPACK_RPM_PACKAGE_LICENSE "BSD")
Andreas Schuhabe4b102016-03-01 16:01:45 +0000590 set (CPACK_RPM_PACKAGE_URL "http://gflags.github.io/gflags")
Andreas Schuh37a9a902014-03-20 03:27:13 +0000591 set (CPACK_RPM_CHANGELOG_FILE "${CMAKE_CURRENT_LIST_DIR}/ChangeLog.txt")
592
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000593 if (INSTALL_HEADERS)
Andreas Schuh2b8deaa2015-03-24 14:41:52 +0000594 set (CPACK_RESOURCE_FILE_README "${CMAKE_CURRENT_LIST_DIR}/doc/index.html")
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000595 else ()
596 set (CPACK_RESOURCE_FILE_README "${CMAKE_CURRENT_LIST_DIR}/cmake/README_runtime.txt")
597 endif ()
598
Andreas Schuhd6995592014-03-20 20:21:30 +0000599 # system/architecture
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000600 if (WINDOWS)
601 if (CMAKE_CL_64)
602 set (CPACK_SYSTEM_NAME "win64")
603 else ()
604 set (CPACK_SYSTEM_NAME "win32")
605 endif ()
Andreas Schuhd6995592014-03-20 20:21:30 +0000606 set (CPACK_PACKAGE_ARCHITECTURE)
Andreas Schuhef5c5472014-03-30 15:34:13 +0100607 elseif (APPLE)
608 set (CPACK_PACKAGE_ARCHITECTURE darwin)
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000609 else ()
610 string (TOLOWER "${CMAKE_SYSTEM_NAME}" CPACK_SYSTEM_NAME)
Andreas Schuhcf40f732014-03-20 20:56:05 +0000611 if (CMAKE_CXX_FLAGS MATCHES "-m32")
612 set (CPACK_PACKAGE_ARCHITECTURE i386)
613 else ()
614 execute_process (
615 COMMAND dpkg --print-architecture
616 RESULT_VARIABLE RV
617 OUTPUT_VARIABLE CPACK_PACKAGE_ARCHITECTURE
618 )
619 if (RV EQUAL 0)
620 string (STRIP "${CPACK_PACKAGE_ARCHITECTURE}" CPACK_PACKAGE_ARCHITECTURE)
621 else ()
622 execute_process (COMMAND uname -m OUTPUT_VARIABLE CPACK_PACKAGE_ARCHITECTURE)
623 if (CPACK_PACKAGE_ARCHITECTURE MATCHES "x86_64")
624 set (CPACK_PACKAGE_ARCHITECTURE amd64)
625 else ()
626 set (CPACK_PACKAGE_ARCHITECTURE i386)
627 endif ()
628 endif ()
629 endif ()
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000630 endif ()
631
632 # source package settings
633 set (CPACK_SOURCE_TOPLEVEL_TAG "source")
634 set (CPACK_SOURCE_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}")
635 set (CPACK_SOURCE_IGNORE_FILES "/\\\\.git/;\\\\.swp$;\\\\.#;/#;\\\\.*~;cscope\\\\.*;/[Bb]uild[.+-_a-zA-Z0-9]*/")
636
637 # default binary package settings
Andreas Schuhd6995592014-03-20 20:21:30 +0000638 set (CPACK_INCLUDE_TOPLEVEL_DIRECTORY TRUE)
639 set (CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}-${CPACK_SYSTEM_NAME}")
640 if (CPACK_PACKAGE_ARCHITECTURE)
641 set (CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_FILE_NAME}-${CPACK_PACKAGE_ARCHITECTURE}")
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000642 endif ()
643
644 # generator specific configuration file
645 #
646 # allow package maintainers to use their own configuration file
647 # $ cmake -DCPACK_PROJECT_CONFIG_FILE:FILE=/path/to/package/config
648 if (NOT CPACK_PROJECT_CONFIG_FILE)
649 configure_file (
650 "${CMAKE_CURRENT_LIST_DIR}/cmake/package.cmake.in"
651 "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-package.cmake" @ONLY
652 )
653 set (CPACK_PROJECT_CONFIG_FILE "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-package.cmake")
654 endif ()
655
656 include (CPack)
657
658endif () # BUILD_PACKAGING