blob: bd81648be9e7b0171e4c96c0e907159643957d91 [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
18## library target for the "gflags_nothreads-static" library target. Targets which
19## 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##
31## Example CMakeLists.txt of super-project which contains gflags source tree:
32## cmake_minimum_required(VERSION 2.8.12 FATAL_ERROR)
33##
34## project(Foo)
35##
36## add_subdirectory(gflags)
37##
38## add_executable(foo src/foo.cc)
39## target_link_libraries(foo gflags)
40##
41## Variables to configure the source files:
42## - GFLAGS_IS_A_DLL
43## - GFLAGS_NAMESPACE
44## - GFLAGS_ATTRIBUTE_UNUSED
45## - GFLAGS_INTTYPES_FORMAT
46##
47## Variables to configure the build:
48## - GFLAGS_SOVERSION
49## - GFLAGS_BUILD_SHARED_LIBS
50## - GFLAGS_BUILD_STATIC_LIBS
51## - GFLAGS_BUILD_gflags_LIB
52## - GFLAGS_BUILD_gflags_nothreads_LIB
53## - GFLAGS_BUILD_TESTING
54## - GFLAGS_BUILD_PACKAGING
55##
56## Variables to configure the installation:
57## - GFLAGS_INCLUDE_DIR
58## - GFLAGS_LIBRARY_INSTALL_DIR or LIB_INSTALL_DIR or LIB_SUFFIX
59## - GFLAGS_INSTALL_HEADERS
60## - GFLAGS_INSTALL_SHARED_LIBS
61## - GFLAGS_INSTALL_STATIC_LIBS
62
Andreas Schuh58345b12015-04-03 16:12:38 +010063cmake_minimum_required (VERSION 2.8.12 FATAL_ERROR)
unknown3d142b82014-03-19 15:27:08 +010064
Andreas Schuh7b5b0802015-03-24 14:41:10 +000065if (POLICY CMP0042)
66 cmake_policy (SET CMP0042 NEW)
67endif ()
68
unknown3d142b82014-03-19 15:27:08 +010069# ----------------------------------------------------------------------------
70# includes
71set (CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
unknown3d142b82014-03-19 15:27:08 +010072include (utils)
73
74# ----------------------------------------------------------------------------
75# package information
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +000076set (PACKAGE_NAME "gflags")
Andreas Schuh4b771462015-03-24 14:44:57 +000077set (PACKAGE_VERSION "2.2.0")
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +000078set (PACKAGE_STRING "${PACKAGE_NAME} ${PACKAGE_VERSION}")
79set (PACKAGE_TARNAME "${PACKAGE_NAME}-${PACKAGE_VERSION}")
Andreas Schuhabe4b102016-03-01 16:01:45 +000080set (PACKAGE_BUGREPORT "https://github.com/gflags/gflags/issues")
unknown3d142b82014-03-19 15:27:08 +010081
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +000082project (${PACKAGE_NAME} CXX)
Andreas Schuh51f1db72015-03-24 15:36:26 +000083if (CMAKE_VERSION VERSION_LESS 100)
84 # C language still needed because the following required CMake modules
85 # (or their dependencies, respectively) are not correctly handling
86 # the case where only CXX is enabled.
87 # - CheckTypeSize.cmake (fixed in CMake 3.1, cf. http://www.cmake.org/Bug/view.php?id=14056)
88 # - FindThreads.cmake (--> CheckIncludeFiles.cmake <--)
89 enable_language (C)
90endif ()
unknown3d142b82014-03-19 15:27:08 +010091
92version_numbers (
93 ${PACKAGE_VERSION}
94 PACKAGE_VERSION_MAJOR
95 PACKAGE_VERSION_MINOR
96 PACKAGE_VERSION_PATCH
97)
98
Andreas Schuh91e16ee2016-03-01 20:03:43 +000099# shared library ABI version number, can be overridden by package maintainers
100# using -DGFLAGS_SOVERSION=XXX on the command-line
101if (GFLAGS_SOVERSION)
102 set (PACKAGE_SOVERSION "${GFLAGS_SOVERSION}")
103else ()
104 set (PACKAGE_SOVERSION "${PACKAGE_VERSION_MAJOR}")
105endif ()
106
107# when gflags is included as subproject (e.g., as Git submodule/subtree) in the source
108# tree of a project that uses it, no variables should be added to the CMake cache;
109# users may set the non-cached variable GFLAGS_IS_SUBPROJECT before add_subdirectory(gflags)
110if (NOT DEFINED GFLAGS_IS_SUBPROJECT)
111 if ("^${CMAKE_SOURCE_DIR}$" STREQUAL "^${PROJECT_SOURCE_DIR}$")
112 set (GFLAGS_IS_SUBPROJECT FALSE)
113 else ()
114 set (GFLAGS_IS_SUBPROJECT TRUE)
115 endif ()
116endif ()
117
118# prefix for package variables in CMake configuration file
119string (TOUPPER "${PACKAGE_NAME}" PACKAGE_PREFIX)
Andreas Schuhbf889782014-05-01 20:16:16 +0100120
unknown3d142b82014-03-19 15:27:08 +0100121# ----------------------------------------------------------------------------
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000122# options
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000123
124# maintain binary backwards compatibility with gflags library version <= 2.0,
125# but at the same time enable the use of the preferred new "gflags" namespace
126gflags_define (STRING NAMESPACE "Name(s) of library namespace (separate multiple options by semicolon)" "google;${PACKAGE_NAME}" "${PACKAGE_NAME}")
127gflags_property (NAMESPACE ADVANCED TRUE)
128set (GFLAGS_NAMESPACE_SECONDARY "${NAMESPACE}")
Andreas Schuh659b4402014-05-02 14:56:58 +0100129list (REMOVE_DUPLICATES GFLAGS_NAMESPACE_SECONDARY)
130if (NOT GFLAGS_NAMESPACE_SECONDARY)
131 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 +0000132endif ()
Andreas Schuh659b4402014-05-02 14:56:58 +0100133foreach (ns IN LISTS GFLAGS_NAMESPACE_SECONDARY)
134 if (NOT ns MATCHES "^[a-zA-Z][a-zA-Z0-9_]*$")
135 message (FATAL_ERROR "GFLAGS_NAMESPACE contains invalid namespace identifier: ${ns}")
136 endif ()
137endforeach ()
138list (GET GFLAGS_NAMESPACE_SECONDARY 0 GFLAGS_NAMESPACE)
139list (REMOVE_AT GFLAGS_NAMESPACE_SECONDARY 0)
unknown3d142b82014-03-19 15:27:08 +0100140
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000141# cached build options when gflags is not a subproject, otherwise non-cached CMake variables
142# usage: gflags_define(BOOL <name> <doc> <default> [<subproject default>])
143gflags_define (BOOL BUILD_SHARED_LIBS "Request build of shared libraries." OFF OFF)
144gflags_define (BOOL BUILD_STATIC_LIBS "Request build of static libraries (default if BUILD_SHARED_LIBS is OFF)." OFF ON)
145gflags_define (BOOL BUILD_gflags_LIB "Request build of the multi-threaded gflags library." ON OFF)
146gflags_define (BOOL BUILD_gflags_nothreads_LIB "Request build of the single-threaded gflags library." ON ON)
147gflags_define (BOOL BUILD_PACKAGING "Enable build of distribution packages using CPack." OFF OFF)
148gflags_define (BOOL BUILD_TESTING "Enable build of the unit tests and their execution using CTest." OFF OFF)
149gflags_define (BOOL INSTALL_HEADERS "Request installation of headers and other development files." ON OFF)
150gflags_define (BOOL INSTALL_SHARED_LIBS "Request installation of shared libraries." ON ON)
151gflags_define (BOOL INSTALL_STATIC_LIBS "Request installation of static libraries." ON OFF)
unknown3d142b82014-03-19 15:27:08 +0100152
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000153gflags_property (BUILD_STATIC_LIBS ADVANCED TRUE)
154gflags_property (INSTALL_HEADERS ADVANCED TRUE)
155gflags_property (INSTALL_SHARED_LIBS ADVANCED TRUE)
156gflags_property (INSTALL_STATIC_LIBS ADVANCED TRUE)
157
158if (NOT GFLAGS_IS_SUBPROJECT)
159 foreach (varname IN ITEMS CMAKE_INSTALL_PREFIX)
160 gflags_property (${varname} ADVANCED FALSE)
161 endforeach ()
162 foreach (varname IN ITEMS CMAKE_CONFIGURATION_TYPES CMAKE_OSX_ARCHITECTURES CMAKE_OSX_DEPLOYMENT_TARGET CMAKE_OSX_SYSROOT)
163 gflags_property (${varname} ADVANCED TRUE)
164 endforeach ()
165 if (NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CXX_FLAGS)
166 gflags_set (CMAKE_BUILD_TYPE Release)
167 endif ()
168 if (CMAKE_CONFIGURATION_TYPES)
169 gflags_property (CMAKE_BUILD_TYPE STRINGS "${CMAKE_CONFIGURATION_TYPES}")
170 endif ()
171endif () # NOT GFLAGS_IS_SUBPROJECT
unknown3d142b82014-03-19 15:27:08 +0100172
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000173if (NOT BUILD_SHARED_LIBS AND NOT BUILD_STATIC_LIBS)
174 set (BUILD_STATIC_LIBS ON)
175endif ()
176if (NOT BUILD_gflags_LIB AND NOT BUILD_gflags_nothreads_LIB)
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000177 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 +0000178endif ()
179
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000180gflags_define (STRING INCLUDE_DIR "Name of include directory of installed header files relative to CMAKE_INSTALL_PREFIX/include/" "${PACKAGE_NAME}")
181gflags_property (INCLUDE_DIR ADVANCED TRUE)
182if (IS_ABSOLUTE INCLUDE_DIR)
183 message (FATAL_ERROR "[GFLAGS_]INCLUDE_DIR must be a path relative to CMAKE_INSTALL_PREFIX/include/")
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000184endif ()
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000185if (INCLUDE_DIR MATCHES "^\\.\\.[/\\]")
186 message (FATAL_ERROR "[GFLAGS_]INCLUDE_DIR must not start with parent directory reference (../)")
Andreas Schuh659b4402014-05-02 14:56:58 +0100187endif ()
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000188set (GFLAGS_INCLUDE_DIR "${INCLUDE_DIR}")
Andreas Schuh659b4402014-05-02 14:56:58 +0100189
unknown3d142b82014-03-19 15:27:08 +0100190# ----------------------------------------------------------------------------
191# system checks
192include (CheckTypeSize)
193include (CheckIncludeFileCXX)
194include (CheckCXXSymbolExists)
195
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000196if (WIN32 AND NOT CYGWIN)
197 set (OS_WINDOWS 1)
198else ()
199 set (OS_WINDOWS 0)
200endif ()
201
unknown3d142b82014-03-19 15:27:08 +0100202if (MSVC)
203 set (HAVE_SYS_TYPES_H 1)
204 set (HAVE_STDINT_H 1)
205 set (HAVE_STDDEF_H 1) # used by CheckTypeSize module
206 set (HAVE_INTTYPES_H 0)
207 set (HAVE_UNISTD_H 0)
208 set (HAVE_SYS_STAT_H 1)
Andreas Schuh92425782014-03-19 17:15:36 +0000209 set (HAVE_SHLWAPI_H 1)
unknown3d142b82014-03-19 15:27:08 +0100210else ()
211 foreach (fname IN ITEMS unistd stdint inttypes sys/types sys/stat fnmatch)
212 string (TOUPPER "${fname}" FNAME)
Andreas Schuh92425782014-03-19 17:15:36 +0000213 string (REPLACE "/" "_" FNAME "${FNAME}")
unknown3d142b82014-03-19 15:27:08 +0100214 if (NOT HAVE_${FNAME}_H)
215 check_include_file_cxx ("${fname}.h" HAVE_${FNAME}_H)
216 endif ()
217 endforeach ()
Andreas Schuhddc53572014-03-19 17:32:23 +0000218 # the following are used in #if directives not #ifdef
unknown3d142b82014-03-19 15:27:08 +0100219 bool_to_int (HAVE_STDINT_H)
220 bool_to_int (HAVE_SYS_TYPES_H)
221 bool_to_int (HAVE_INTTYPES_H)
Andreas Schuh92425782014-03-19 17:15:36 +0000222 if (NOT HAVE_FNMATCH_H AND OS_WINDOWS)
223 check_include_file_cxx ("shlwapi.h" HAVE_SHLWAPI_H)
224 endif ()
unknown3d142b82014-03-19 15:27:08 +0100225endif ()
226
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000227gflags_define (STRING INTTYPES_FORMAT "Format of integer types: \"C99\" (uint32_t), \"BSD\" (u_int32_t), \"VC7\" (__int32)" "")
228gflags_property (INTTYPES_FORMAT STRINGS "C99;BSD;VC7")
229gflags_property (INTTYPES_FORMAT ADVANCED TRUE)
230if (NOT INTTYPES_FORMAT)
unknown3d142b82014-03-19 15:27:08 +0100231 set (TYPES uint32_t u_int32_t)
232 if (MSVC)
233 list (INSERT TYPES 0 __int32)
234 endif ()
235 foreach (type IN LISTS TYPES)
236 check_type_size (${type} ${type} LANGUAGE CXX)
237 if (HAVE_${type})
238 break ()
239 endif ()
240 endforeach ()
241 if (HAVE_uint32_t)
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000242 gflags_set (INTTYPES_FORMAT C99)
unknown3d142b82014-03-19 15:27:08 +0100243 elseif (HAVE_u_int32_t)
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000244 gflags_set (INTTYPES_FORMAT BSD)
unknown3d142b82014-03-19 15:27:08 +0100245 elseif (HAVE___int32)
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000246 gflags_set (INTTYPES_FORMAT VC7)
unknown3d142b82014-03-19 15:27:08 +0100247 else ()
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000248 gflags_property (INTTYPES_FORMAT ADVANCED FALSE)
unknown3d142b82014-03-19 15:27:08 +0100249 message (FATAL_ERROR "Do not know how to define a 32-bit integer quantity on your system!"
250 " Neither uint32_t, u_int32_t, nor __int32 seem to be available."
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000251 " Set [GFLAGS_]INTTYPES_FORMAT to either C99, BSD, or VC7 and try again.")
unknown3d142b82014-03-19 15:27:08 +0100252 endif ()
253endif ()
254# use of special characters in strings to circumvent bug #0008226
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000255if ("^${INTTYPES_FORMAT}$" STREQUAL "^WIN$")
256 gflags_set (INTTYPES_FORMAT VC7)
unknown3d142b82014-03-19 15:27:08 +0100257endif ()
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000258if (NOT INTTYPES_FORMAT MATCHES "^(C99|BSD|VC7)$")
259 message (FATAL_ERROR "Invalid value for [GFLAGS_]INTTYPES_FORMAT! Choose one of \"C99\", \"BSD\", or \"VC7\"")
unknown3d142b82014-03-19 15:27:08 +0100260endif ()
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000261set (GFLAGS_INTTYPES_FORMAT "${INTTYPES_FORMAT}")
unknown3d142b82014-03-19 15:27:08 +0100262set (GFLAGS_INTTYPES_FORMAT_C99 0)
263set (GFLAGS_INTTYPES_FORMAT_BSD 0)
264set (GFLAGS_INTTYPES_FORMAT_VC7 0)
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000265set ("GFLAGS_INTTYPES_FORMAT_${INTTYPES_FORMAT}" 1)
unknown3d142b82014-03-19 15:27:08 +0100266
267if (MSVC)
268 set (HAVE_strtoll 0)
269 set (HAVE_strtoq 0)
270else ()
Andreas Schuh0b116eb2014-03-19 15:56:26 +0000271 check_cxx_symbol_exists (strtoll stdlib.h HAVE_STRTOLL)
272 if (NOT HAVE_STRTOLL)
273 check_cxx_symbol_exists (strtoq stdlib.h HAVE_STRTOQ)
274 endif ()
unknown3d142b82014-03-19 15:27:08 +0100275endif ()
276
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000277if (BUILD_gflags_LIB)
278 set (CMAKE_THREAD_PREFER_PTHREAD TRUE)
279 find_package (Threads)
280 if (Threads_FOUND AND CMAKE_USE_PTHREADS_INIT)
281 set (HAVE_PTHREAD 1)
282 check_type_size (pthread_rwlock_t RWLOCK LANGUAGE CXX)
283 else ()
284 set (HAVE_PTHREAD 0)
285 endif ()
286 if (UNIX AND NOT HAVE_PTHREAD)
287 if (CMAKE_HAVE_PTHREAD_H)
288 set (what "library")
289 else ()
290 set (what ".h file")
291 endif ()
292 message (FATAL_ERROR "Could not find pthread${what}. Check the log file"
293 "\n\t${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log"
294 "\nor disable the build of the multi-threaded gflags library (BUILD_gflags_LIB=OFF).")
295 endif ()
unknown3d142b82014-03-19 15:27:08 +0100296else ()
297 set (HAVE_PTHREAD 0)
298endif ()
299
unknown3d142b82014-03-19 15:27:08 +0100300# ----------------------------------------------------------------------------
301# source files - excluding root subdirectory and/or .in suffix
302set (PUBLIC_HDRS
303 "gflags.h"
304 "gflags_declare.h"
305 "gflags_completions.h"
306)
307
Andreas Schuh659b4402014-05-02 14:56:58 +0100308if (GFLAGS_NAMESPACE_SECONDARY)
309 set (INCLUDE_GFLAGS_NS_H "// Import gflags library symbols into alternative/deprecated namespace(s)")
310 foreach (ns IN LISTS GFLAGS_NAMESPACE_SECONDARY)
311 string (TOUPPER "${ns}" NS)
312 set (gflags_ns_h "${PROJECT_BINARY_DIR}/include/${GFLAGS_INCLUDE_DIR}/gflags_${ns}.h")
313 configure_file ("${PROJECT_SOURCE_DIR}/src/gflags_ns.h.in" "${gflags_ns_h}" @ONLY)
314 list (APPEND PUBLIC_HDRS "${gflags_ns_h}")
315 set (INCLUDE_GFLAGS_NS_H "${INCLUDE_GFLAGS_NS_H}\n#include \"gflags_${ns}.h\"")
316 endforeach ()
317else ()
318 set (INCLUDE_GFLAGS_NS_H)
319endif ()
320
unknown3d142b82014-03-19 15:27:08 +0100321set (PRIVATE_HDRS
322 "config.h"
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000323 "util.h"
324 "mutex.h"
unknown3d142b82014-03-19 15:27:08 +0100325)
326
327set (GFLAGS_SRCS
328 "gflags.cc"
329 "gflags_reporting.cc"
330 "gflags_completions.cc"
331)
332
Andreas Schuh41181662014-03-19 16:46:56 +0000333if (OS_WINDOWS)
unknown3d142b82014-03-19 15:27:08 +0100334 list (APPEND PRIVATE_HDRS "windows_port.h")
335 list (APPEND GFLAGS_SRCS "windows_port.cc")
336endif ()
337
338# ----------------------------------------------------------------------------
339# configure source files
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000340if (NOT DEFINED GFLAGS_ATTRIBUTE_UNUSED)
341 if (CMAKE_COMPILER_IS_GNUCXX)
342 set (GFLAGS_ATTRIBUTE_UNUSED "__attribute((unused))")
343 else ()
344 set (GFLAGS_ATTRIBUTE_UNUSED)
345 endif ()
unknown3d142b82014-03-19 15:27:08 +0100346endif ()
347
Andreas Schuh6040eac2014-03-25 15:29:56 +0000348# whenever we build a shared library (DLL on Windows), configure the public
349# headers of the API for use of this library rather than the optionally
350# also build statically linked library; users can override GFLAGS_DLL_DECL
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000351if (NOT DEFINED GFLAGS_IS_A_DLL)
352 if (BUILD_SHARED_LIBS)
353 set (GFLAGS_IS_A_DLL 1)
354 else ()
355 set (GFLAGS_IS_A_DLL 0)
356 endif ()
Andreas Schuh6040eac2014-03-25 15:29:56 +0000357endif ()
358
unknown3d142b82014-03-19 15:27:08 +0100359configure_headers (PUBLIC_HDRS ${PUBLIC_HDRS})
360configure_sources (PRIVATE_HDRS ${PRIVATE_HDRS})
361configure_sources (GFLAGS_SRCS ${GFLAGS_SRCS})
362
363# ----------------------------------------------------------------------------
364# output directories
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000365if (NOT GFLAGS_IS_SUBPROJECT)
366 set (CMAKE_RUNTIME_OUTPUT_DIRECTORY "bin")
367 set (CMAKE_LIBRARY_OUTPUT_DIRECTORY "lib")
368 set (CMAKE_ARCHIVE_OUTPUT_DIRECTORY "lib")
369endif ()
unknown3d142b82014-03-19 15:27:08 +0100370
371# ----------------------------------------------------------------------------
Andreas Schuh76c53b72015-03-24 14:44:12 +0000372# installation directories
373if (OS_WINDOWS)
374 set (RUNTIME_INSTALL_DIR Bin)
375 set (LIBRARY_INSTALL_DIR Lib)
376 set (INCLUDE_INSTALL_DIR Include)
377 set (CONFIG_INSTALL_DIR CMake)
378else ()
379 set (RUNTIME_INSTALL_DIR bin)
380 # The LIB_INSTALL_DIR and LIB_SUFFIX variables are used by the Fedora
381 # package maintainers. Also package maintainers of other distribution
382 # packages need to be able to specify the name of the library directory.
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000383 if (NOT GFLAGS_LIBRARY_INSTALL_DIR AND LIB_INSTALL_DIR)
384 set (GFLAGS_LIBRARY_INSTALL_DIR "${LIB_INSTALL_DIR}")
Andreas Schuh76c53b72015-03-24 14:44:12 +0000385 endif ()
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000386 gflags_define (PATH LIBRARY_INSTALL_DIR "Directory of installed libraries, e.g., \"lib64\"" "lib${LIB_SUFFIX}")
387 gflags_property (LIBRARY_INSTALL_DIR ADVANCED TRUE)
Andreas Schuh76c53b72015-03-24 14:44:12 +0000388 set (INCLUDE_INSTALL_DIR include)
389 set (CONFIG_INSTALL_DIR ${LIBRARY_INSTALL_DIR}/cmake/${PACKAGE_NAME})
390endif ()
391
392# ----------------------------------------------------------------------------
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000393# add library targets
394set (TARGETS)
395# static vs. shared
396foreach (TYPE IN ITEMS STATIC SHARED)
397 if (BUILD_${TYPE}_LIBS)
Arnaud Farbosd78b00d2015-11-05 12:03:37 -0800398 string (TOLOWER "${TYPE}" type)
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000399 # whether or not targets are a DLL
400 if (OS_WINDOWS AND "^${TYPE}$" STREQUAL "^SHARED$")
401 set (GFLAGS_IS_A_DLL 1)
402 else ()
403 set (GFLAGS_IS_A_DLL 0)
404 endif ()
Andreas Schuh6bd636c2016-02-29 17:58:46 +0000405 # filename suffix for static libraries on Windows
406 if (OS_WINDOWS AND "^${TYPE}$" STREQUAL "^STATIC$")
407 set (type_suffix "_${type}")
408 else ()
409 set (type_suffix "")
410 endif ()
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000411 # multi-threaded vs. single-threaded
412 foreach (opts IN ITEMS "" _nothreads)
413 if (BUILD_gflags${opts}_LIB)
414 add_library (gflags${opts}-${type} ${TYPE} ${GFLAGS_SRCS} ${PRIVATE_HDRS} ${PUBLIC_HDRS})
Andreas Schuh58345b12015-04-03 16:12:38 +0100415 set (include_dirs "$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/include>")
416 if (INSTALL_HEADERS)
417 list (APPEND include_dirs "$<INSTALL_INTERFACE:${INCLUDE_INSTALL_DIR}>")
Andreas Schuh76c53b72015-03-24 14:44:12 +0000418 endif ()
Andreas Schuh58345b12015-04-03 16:12:38 +0100419 target_include_directories (gflags${opts}-${type}
420 PUBLIC "${include_dirs}"
421 PRIVATE "${PROJECT_SOURCE_DIR}/src;${PROJECT_BINARY_DIR}/include/${GFLAGS_INCLUDE_DIR}"
422 )
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000423 if (opts MATCHES "nothreads")
Sam Cleggb572a722016-01-29 16:09:43 -0800424 set (defines "GFLAGS_IS_A_DLL=${GFLAGS_IS_A_DLL};NO_THREADS")
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000425 else ()
426 set (defines "GFLAGS_IS_A_DLL=${GFLAGS_IS_A_DLL}")
Andreas Schuhbf139ea2014-03-20 03:04:44 +0000427 if (CMAKE_USE_PTHREADS_INIT)
428 target_link_libraries (gflags${opts}-${type} ${CMAKE_THREAD_LIBS_INIT})
429 endif ()
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000430 endif ()
431 set_target_properties (
432 gflags${opts}-${type} PROPERTIES COMPILE_DEFINITIONS "${defines}"
Andreas Schuh6bd636c2016-02-29 17:58:46 +0000433 OUTPUT_NAME "gflags${opts}${type_suffix}"
Andreas Schuhcd7aece2014-05-02 11:12:05 +0100434 VERSION "${PACKAGE_VERSION}"
435 SOVERSION "${PACKAGE_SOVERSION}"
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000436 )
437 if (HAVE_SHLWAPI_H)
438 target_link_libraries (gflags${opts}-${type} shlwapi.lib)
439 endif ()
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000440 list (APPEND TARGETS gflags${opts}-${type})
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000441 # add convenience make target for build of both shared and static libraries
442 if (NOT GFLAGS_IS_SUBPROJECT)
443 if (NOT TARGET gflags${opts})
444 add_custom_target (gflags${opts})
445 endif ()
446 add_dependencies (gflags${opts} gflags${opts}-${type})
447 endif ()
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000448 endif ()
449 endforeach ()
Andreas Schuhddc53572014-03-19 17:32:23 +0000450 endif ()
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000451endforeach ()
unknown3d142b82014-03-19 15:27:08 +0100452
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000453# add ALIAS target for use in super-project, prefer static over shared, single-threaded over multi-threaded
454if (GFLAGS_IS_SUBPROJECT)
455 foreach (type IN ITEMS static shared)
456 foreach (opts IN ITEMS "_nothreads" "")
457 if (TARGET gflags${opts}-${type})
458 add_library (gflags ALIAS gflags${opts}-${type})
459 break ()
460 endif ()
461 endforeach ()
462 if (TARGET gflags)
463 break ()
464 endif ()
465 endforeach ()
466endif ()
467
unknown3d142b82014-03-19 15:27:08 +0100468# ----------------------------------------------------------------------------
Andreas Schuh76c53b72015-03-24 14:44:12 +0000469# installation rules
unknown3d142b82014-03-19 15:27:08 +0100470file (RELATIVE_PATH INSTALL_PREFIX_REL2CONFIG_DIR "${CMAKE_INSTALL_PREFIX}/${CONFIG_INSTALL_DIR}" "${CMAKE_INSTALL_PREFIX}")
471configure_file (cmake/config.cmake.in "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-config-install.cmake" @ONLY)
472configure_file (cmake/version.cmake.in "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-config-version.cmake" @ONLY)
473
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000474if (BUILD_SHARED_LIBS AND INSTALL_SHARED_LIBS)
475 foreach (opts IN ITEMS "" _nothreads)
476 if (BUILD_gflags${opts}_LIB)
477 install (TARGETS gflags${opts}-shared DESTINATION ${LIBRARY_INSTALL_DIR} EXPORT gflags-lib)
478 endif ()
479 endforeach ()
480endif ()
481if (BUILD_STATIC_LIBS AND INSTALL_STATIC_LIBS)
482 foreach (opts IN ITEMS "" _nothreads)
483 if (BUILD_gflags${opts}_LIB)
484 install (TARGETS gflags${opts}-static DESTINATION ${LIBRARY_INSTALL_DIR} EXPORT gflags-lib)
485 endif ()
486 endforeach ()
487endif ()
488
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000489if (INSTALL_HEADERS)
Andreas Schuhaff8ef82014-03-27 01:09:20 +0000490 install (FILES ${PUBLIC_HDRS} DESTINATION ${INCLUDE_INSTALL_DIR}/${GFLAGS_INCLUDE_DIR})
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000491 install (
492 FILES "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-config-install.cmake"
493 RENAME ${PACKAGE_NAME}-config.cmake
494 DESTINATION ${CONFIG_INSTALL_DIR}
495 )
496 install (
497 FILES "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-config-version.cmake"
498 DESTINATION ${CONFIG_INSTALL_DIR}
499 )
500 install (EXPORT gflags-lib DESTINATION ${CONFIG_INSTALL_DIR} FILE ${PACKAGE_NAME}-export.cmake)
501 if (UNIX)
502 install (PROGRAMS src/gflags_completions.sh DESTINATION ${RUNTIME_INSTALL_DIR})
503 endif ()
unknown3d142b82014-03-19 15:27:08 +0100504endif ()
505
506# ----------------------------------------------------------------------------
507# support direct use of build tree
508set (INSTALL_PREFIX_REL2CONFIG_DIR .)
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000509export (TARGETS ${TARGETS} FILE "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-export.cmake")
unknown3d142b82014-03-19 15:27:08 +0100510export (PACKAGE gflags)
511configure_file (cmake/config.cmake.in "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-config.cmake" @ONLY)
512
513# ----------------------------------------------------------------------------
514# testing - MUST follow the generation of the build tree config file
unknown3d142b82014-03-19 15:27:08 +0100515if (BUILD_TESTING)
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000516 include (CTest)
unknown3d142b82014-03-19 15:27:08 +0100517 enable_testing ()
518 add_subdirectory (test)
519endif ()
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000520
521# ----------------------------------------------------------------------------
522# packaging
523if (BUILD_PACKAGING)
524
525 if (NOT BUILD_SHARED_LIBS AND NOT INSTALL_HEADERS)
526 message (WARNING "Package will contain static libraries without headers!"
527 "\nRecommended options for generation of runtime package:"
528 "\n BUILD_SHARED_LIBS=ON"
529 "\n BUILD_STATIC_LIBS=OFF"
530 "\n INSTALL_HEADERS=OFF"
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000531 "\n INSTALL_SHARED_LIBS=ON"
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000532 "\nRecommended options for generation of development package:"
533 "\n BUILD_SHARED_LIBS=ON"
534 "\n BUILD_STATIC_LIBS=ON"
Andreas Schuh91e16ee2016-03-01 20:03:43 +0000535 "\n INSTALL_HEADERS=ON"
536 "\n INSTALL_SHARED_LIBS=ON"
537 "\n INSTALL_STATIC_LIBS=ON")
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000538 endif ()
539
540 # default package generators
541 if (APPLE)
542 set (PACKAGE_GENERATOR "PackageMaker")
543 set (PACKAGE_SOURCE_GENERATOR "TGZ;ZIP")
544 elseif (UNIX)
545 set (PACKAGE_GENERATOR "DEB;RPM")
546 set (PACKAGE_SOURCE_GENERATOR "TGZ;ZIP")
547 else ()
548 set (PACKAGE_GENERATOR "ZIP")
549 set (PACKAGE_SOURCE_GENERATOR "ZIP")
550 endif ()
551
552 # used package generators
553 set (CPACK_GENERATOR "${PACKAGE_GENERATOR}" CACHE STRING "List of binary package generators (CPack).")
554 set (CPACK_SOURCE_GENERATOR "${PACKAGE_SOURCE_GENERATOR}" CACHE STRING "List of source package generators (CPack).")
555 mark_as_advanced (CPACK_GENERATOR CPACK_SOURCE_GENERATOR)
556
Andreas Schuh64ac2e32015-03-24 16:21:44 +0000557 # some package generators (e.g., PackageMaker) do not allow .md extension
558 configure_file ("${CMAKE_CURRENT_LIST_DIR}/README.md" "${CMAKE_CURRENT_BINARY_DIR}/README.txt" COPYONLY)
559
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000560 # common package information
Andreas Schuhfc6e0792014-03-20 04:09:02 +0000561 set (CPACK_PACKAGE_VENDOR "Andreas Schuh")
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000562 set (CPACK_PACKAGE_CONTACT "google-gflags@googlegroups.com")
563 set (CPACK_PACKAGE_NAME "${PACKAGE_NAME}")
564 set (CPACK_PACKAGE_VERSION "${PACKAGE_VERSION}")
565 set (CPACK_PACKAGE_VERSION_MAJOR "${PACKAGE_VERSION_MAJOR}")
566 set (CPACK_PACKAGE_VERSION_MINOR "${PACKAGE_VERSION_MINOR}")
567 set (CPACK_PACKAGE_VERSION_PATCH "${PACKAGE_VERSION_PATCH}")
568 set (CPACK_PACKAGE_DESCRIPTION_SUMMARY "A commandline flags library that allows for distributed flags.")
Andreas Schuha819f0f2015-03-24 16:29:31 +0000569 set (CPACK_RESOURCE_FILE_WELCOME "${CMAKE_CURRENT_BINARY_DIR}/README.txt")
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000570 set (CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_LIST_DIR}/COPYING.txt")
Andreas Schuha819f0f2015-03-24 16:29:31 +0000571 set (CPACK_PACKAGE_DESCRIPTION_FILE "${CMAKE_CURRENT_BINARY_DIR}/README.txt")
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000572 set (CPACK_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}")
573 set (CPACK_OUTPUT_FILE_PREFIX packages)
574 set (CPACK_PACKAGE_RELOCATABLE TRUE)
575 set (CPACK_MONOLITHIC_INSTALL TRUE)
576
Andreas Schuhe8890f22014-03-20 04:20:15 +0000577 # RPM package information -- used in cmake/package.cmake.in also for DEB
Andreas Schuh37a9a902014-03-20 03:27:13 +0000578 set (CPACK_RPM_PACKAGE_GROUP "Development/Libraries")
579 set (CPACK_RPM_PACKAGE_LICENSE "BSD")
Andreas Schuhabe4b102016-03-01 16:01:45 +0000580 set (CPACK_RPM_PACKAGE_URL "http://gflags.github.io/gflags")
Andreas Schuh37a9a902014-03-20 03:27:13 +0000581 set (CPACK_RPM_CHANGELOG_FILE "${CMAKE_CURRENT_LIST_DIR}/ChangeLog.txt")
582
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000583 if (INSTALL_HEADERS)
Andreas Schuh2b8deaa2015-03-24 14:41:52 +0000584 set (CPACK_RESOURCE_FILE_README "${CMAKE_CURRENT_LIST_DIR}/doc/index.html")
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000585 else ()
586 set (CPACK_RESOURCE_FILE_README "${CMAKE_CURRENT_LIST_DIR}/cmake/README_runtime.txt")
587 endif ()
588
Andreas Schuhd6995592014-03-20 20:21:30 +0000589 # system/architecture
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000590 if (WINDOWS)
591 if (CMAKE_CL_64)
592 set (CPACK_SYSTEM_NAME "win64")
593 else ()
594 set (CPACK_SYSTEM_NAME "win32")
595 endif ()
Andreas Schuhd6995592014-03-20 20:21:30 +0000596 set (CPACK_PACKAGE_ARCHITECTURE)
Andreas Schuhef5c5472014-03-30 15:34:13 +0100597 elseif (APPLE)
598 set (CPACK_PACKAGE_ARCHITECTURE darwin)
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000599 else ()
600 string (TOLOWER "${CMAKE_SYSTEM_NAME}" CPACK_SYSTEM_NAME)
Andreas Schuhcf40f732014-03-20 20:56:05 +0000601 if (CMAKE_CXX_FLAGS MATCHES "-m32")
602 set (CPACK_PACKAGE_ARCHITECTURE i386)
603 else ()
604 execute_process (
605 COMMAND dpkg --print-architecture
606 RESULT_VARIABLE RV
607 OUTPUT_VARIABLE CPACK_PACKAGE_ARCHITECTURE
608 )
609 if (RV EQUAL 0)
610 string (STRIP "${CPACK_PACKAGE_ARCHITECTURE}" CPACK_PACKAGE_ARCHITECTURE)
611 else ()
612 execute_process (COMMAND uname -m OUTPUT_VARIABLE CPACK_PACKAGE_ARCHITECTURE)
613 if (CPACK_PACKAGE_ARCHITECTURE MATCHES "x86_64")
614 set (CPACK_PACKAGE_ARCHITECTURE amd64)
615 else ()
616 set (CPACK_PACKAGE_ARCHITECTURE i386)
617 endif ()
618 endif ()
619 endif ()
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000620 endif ()
621
622 # source package settings
623 set (CPACK_SOURCE_TOPLEVEL_TAG "source")
624 set (CPACK_SOURCE_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}")
625 set (CPACK_SOURCE_IGNORE_FILES "/\\\\.git/;\\\\.swp$;\\\\.#;/#;\\\\.*~;cscope\\\\.*;/[Bb]uild[.+-_a-zA-Z0-9]*/")
626
627 # default binary package settings
Andreas Schuhd6995592014-03-20 20:21:30 +0000628 set (CPACK_INCLUDE_TOPLEVEL_DIRECTORY TRUE)
629 set (CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}-${CPACK_SYSTEM_NAME}")
630 if (CPACK_PACKAGE_ARCHITECTURE)
631 set (CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_FILE_NAME}-${CPACK_PACKAGE_ARCHITECTURE}")
Andreas Schuh9ecc4ca2014-03-20 02:11:44 +0000632 endif ()
633
634 # generator specific configuration file
635 #
636 # allow package maintainers to use their own configuration file
637 # $ cmake -DCPACK_PROJECT_CONFIG_FILE:FILE=/path/to/package/config
638 if (NOT CPACK_PROJECT_CONFIG_FILE)
639 configure_file (
640 "${CMAKE_CURRENT_LIST_DIR}/cmake/package.cmake.in"
641 "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-package.cmake" @ONLY
642 )
643 set (CPACK_PROJECT_CONFIG_FILE "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-package.cmake")
644 endif ()
645
646 include (CPack)
647
648endif () # BUILD_PACKAGING