enh: Set GFLAGS_IS_A_DLL in INTERFACE_COMPILE_DEFINITIONS
[platform/upstream/gflags.git] / CMakeLists.txt
1 ## CMake configuration file of gflags project
2 ##
3 ## This CMakeLists.txt defines some gflags specific configuration variables
4 ## using the "gflags_define" utility macro. The default values of these variables
5 ## can be overridden either on the CMake command-line using the -D option of
6 ## the cmake command or in a super-project which includes the gflags source
7 ## tree by setting the GFLAGS_<varname> CMake variables before adding the
8 ## gflags source directory via CMake's "add_subdirectory" command. Only when
9 ## the non-cached variable GFLAGS_IS_SUBPROJECT has a value equivalent to FALSE,
10 ## these configuration variables are added to the CMake cache so they can be
11 ## edited in the CMake GUI. By default, GFLAGS_IS_SUBPROJECT is set to TRUE when
12 ## the CMAKE_SOURCE_DIR is not identical to the directory of this CMakeLists.txt
13 ## file, i.e., the top-level directory of the gflags project source tree.
14 ##
15 ## When this project is a subproject (GFLAGS_IS_SUBPROJECT is TRUE), the default
16 ## settings are such that only the static single-threaded library is built without
17 ## installation of the gflags files. The "gflags" 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 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 ##
41 ## Example CMakeLists.txt of super-project which contains gflags source tree:
42 ##   cmake_minimum_required(VERSION 2.8.12 FATAL_ERROR)
43 ##
44 ##   project(Foo)
45 ##
46 ##   add_subdirectory(gflags)
47 ##
48 ##   add_executable(foo src/foo.cc)
49 ##   target_link_libraries(foo gflags)
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
73 cmake_minimum_required (VERSION 2.8.12 FATAL_ERROR)
74
75 if (POLICY CMP0042)
76   cmake_policy (SET CMP0042 NEW)
77 endif ()
78
79 # ----------------------------------------------------------------------------
80 # includes
81 set (CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
82 include (utils)
83
84 # ----------------------------------------------------------------------------
85 # package information
86 set (PACKAGE_NAME      "gflags")
87 set (PACKAGE_VERSION   "2.2.0")
88 set (PACKAGE_STRING    "${PACKAGE_NAME} ${PACKAGE_VERSION}")
89 set (PACKAGE_TARNAME   "${PACKAGE_NAME}-${PACKAGE_VERSION}")
90 set (PACKAGE_BUGREPORT "https://github.com/gflags/gflags/issues")
91
92 project (${PACKAGE_NAME} CXX)
93 if (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)
100 endif ()
101
102 version_numbers (
103   ${PACKAGE_VERSION}
104     PACKAGE_VERSION_MAJOR
105     PACKAGE_VERSION_MINOR
106     PACKAGE_VERSION_PATCH
107 )
108
109 # shared library ABI version number, can be overridden by package maintainers
110 # using -DGFLAGS_SOVERSION=XXX on the command-line
111 if (GFLAGS_SOVERSION)
112   set (PACKAGE_SOVERSION "${GFLAGS_SOVERSION}")
113 else ()
114   # TODO: Change default SOVERSION back to PACKAGE_VERSION_MAJOR with the
115   #       next increase of major version number (i.e., 3.0.0 -> SOVERSION 3)
116   #       The <major>.<minor> SOVERSION should be used for the 2.x releases
117   #       versions only which temporarily broke the API by changing the default
118   #       namespace from "google" to "gflags".
119   set (PACKAGE_SOVERSION "${PACKAGE_VERSION_MAJOR}.${PACKAGE_VERSION_MINOR}")
120 endif ()
121
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)
125 if (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 ()
131 endif ()
132
133 # prefix for package variables in CMake configuration file
134 string (TOUPPER "${PACKAGE_NAME}" PACKAGE_PREFIX)
135
136 # 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
138 file (TO_CMAKE_PATH "${CMAKE_INSTALL_PREFIX}" CMAKE_INSTALL_PREFIX)
139
140 # ----------------------------------------------------------------------------
141 # options
142
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
145 gflags_define (STRING NAMESPACE "Name(s) of library namespace (separate multiple options by semicolon)" "google;${PACKAGE_NAME}" "${PACKAGE_NAME}")
146 gflags_property (NAMESPACE ADVANCED TRUE)
147 set (GFLAGS_NAMESPACE_SECONDARY "${NAMESPACE}")
148 list (REMOVE_DUPLICATES GFLAGS_NAMESPACE_SECONDARY)
149 if (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 \";\").")
151 endif ()
152 foreach (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 ()
156 endforeach ()
157 list (GET       GFLAGS_NAMESPACE_SECONDARY 0 GFLAGS_NAMESPACE)
158 list (REMOVE_AT GFLAGS_NAMESPACE_SECONDARY 0)
159
160 # cached build options when gflags is not a subproject, otherwise non-cached CMake variables
161 # usage: gflags_define(BOOL <name> <doc> <default> [<subproject default>])
162 gflags_define (BOOL BUILD_SHARED_LIBS          "Request build of shared libraries."                                       OFF OFF)
163 gflags_define (BOOL BUILD_STATIC_LIBS          "Request build of static libraries (default if BUILD_SHARED_LIBS is OFF)." OFF ON)
164 gflags_define (BOOL BUILD_gflags_LIB           "Request build of the multi-threaded gflags library."                      ON  OFF)
165 gflags_define (BOOL BUILD_gflags_nothreads_LIB "Request build of the single-threaded gflags library."                     ON  ON)
166 gflags_define (BOOL BUILD_PACKAGING            "Enable build of distribution packages using CPack."                       OFF OFF)
167 gflags_define (BOOL BUILD_TESTING              "Enable build of the unit tests and their execution using CTest."          OFF OFF)
168 gflags_define (BOOL INSTALL_HEADERS            "Request installation of headers and other development files."             ON  OFF)
169 gflags_define (BOOL INSTALL_SHARED_LIBS        "Request installation of shared libraries."                                ON  ON)
170 gflags_define (BOOL INSTALL_STATIC_LIBS        "Request installation of static libraries."                                ON  OFF)
171 gflags_define (BOOL REGISTER_BUILD_DIR         "Request entry of build directory in CMake's package registry."            OFF OFF)
172 gflags_define (BOOL REGISTER_INSTALL_PREFIX    "Request entry of installed package in CMake's package registry."          ON  OFF)
173
174 gflags_property (BUILD_STATIC_LIBS   ADVANCED TRUE)
175 gflags_property (INSTALL_HEADERS     ADVANCED TRUE)
176 gflags_property (INSTALL_SHARED_LIBS ADVANCED TRUE)
177 gflags_property (INSTALL_STATIC_LIBS ADVANCED TRUE)
178
179 if (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 ()
192 endif () # NOT GFLAGS_IS_SUBPROJECT
193
194 if (NOT BUILD_SHARED_LIBS AND NOT BUILD_STATIC_LIBS)
195   set (BUILD_STATIC_LIBS ON)
196 endif ()
197 if (NOT BUILD_gflags_LIB AND NOT BUILD_gflags_nothreads_LIB)
198   message (FATAL_ERROR "At least one of [GFLAGS_]BUILD_gflags_LIB and [GFLAGS_]BUILD_gflags_nothreads_LIB must be ON.")
199 endif ()
200
201 gflags_define (STRING INCLUDE_DIR "Name of include directory of installed header files relative to CMAKE_INSTALL_PREFIX/include/" "${PACKAGE_NAME}")
202 gflags_property (INCLUDE_DIR ADVANCED TRUE)
203 file (TO_CMAKE_PATH "${INCLUDE_DIR}" INCLUDE_DIR)
204 if (IS_ABSOLUTE INCLUDE_DIR)
205   message (FATAL_ERROR "[GFLAGS_]INCLUDE_DIR must be a path relative to CMAKE_INSTALL_PREFIX/include/")
206 endif ()
207 if (INCLUDE_DIR MATCHES "^\\.\\.[/\\]")
208   message (FATAL_ERROR "[GFLAGS_]INCLUDE_DIR must not start with parent directory reference (../)")
209 endif ()
210 set (GFLAGS_INCLUDE_DIR "${INCLUDE_DIR}")
211
212 # ----------------------------------------------------------------------------
213 # system checks
214 include (CheckTypeSize)
215 include (CheckIncludeFileCXX)
216 include (CheckCXXSymbolExists)
217
218 if (WIN32 AND NOT CYGWIN)
219   set (OS_WINDOWS 1)
220 else ()
221   set (OS_WINDOWS 0)
222 endif ()
223
224 if (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)
231   set (HAVE_SHLWAPI_H   1)
232 else ()
233   foreach (fname IN ITEMS unistd stdint inttypes sys/types sys/stat fnmatch)
234     string (TOUPPER "${fname}" FNAME)
235     string (REPLACE "/" "_" FNAME "${FNAME}")
236     if (NOT HAVE_${FNAME}_H)
237       check_include_file_cxx ("${fname}.h" HAVE_${FNAME}_H)
238     endif ()
239   endforeach ()
240   # the following are used in #if directives not #ifdef
241   bool_to_int (HAVE_STDINT_H)
242   bool_to_int (HAVE_SYS_TYPES_H)
243   bool_to_int (HAVE_INTTYPES_H)
244   if (NOT HAVE_FNMATCH_H AND OS_WINDOWS)
245     check_include_file_cxx ("shlwapi.h" HAVE_SHLWAPI_H)
246   endif ()
247 endif ()
248
249 gflags_define (STRING INTTYPES_FORMAT "Format of integer types: \"C99\" (uint32_t), \"BSD\" (u_int32_t), \"VC7\" (__int32)" "")
250 gflags_property (INTTYPES_FORMAT STRINGS "C99;BSD;VC7")
251 gflags_property (INTTYPES_FORMAT ADVANCED TRUE)
252 if (NOT INTTYPES_FORMAT)
253   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)
264     gflags_set (INTTYPES_FORMAT C99)
265   elseif (HAVE_u_int32_t)
266     gflags_set (INTTYPES_FORMAT BSD)
267   elseif (HAVE___int32)
268     gflags_set (INTTYPES_FORMAT VC7)
269   else ()
270     gflags_property (INTTYPES_FORMAT ADVANCED FALSE)
271     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."
273                          " Set [GFLAGS_]INTTYPES_FORMAT to either C99, BSD, or VC7 and try again.")
274   endif ()
275 endif ()
276 # use of special characters in strings to circumvent bug #0008226
277 if ("^${INTTYPES_FORMAT}$" STREQUAL "^WIN$")
278   gflags_set (INTTYPES_FORMAT VC7)
279 endif ()
280 if (NOT INTTYPES_FORMAT MATCHES "^(C99|BSD|VC7)$")
281   message (FATAL_ERROR "Invalid value for [GFLAGS_]INTTYPES_FORMAT! Choose one of \"C99\", \"BSD\", or \"VC7\"")
282 endif ()
283 set (GFLAGS_INTTYPES_FORMAT "${INTTYPES_FORMAT}")
284 set (GFLAGS_INTTYPES_FORMAT_C99 0)
285 set (GFLAGS_INTTYPES_FORMAT_BSD 0)
286 set (GFLAGS_INTTYPES_FORMAT_VC7 0)
287 set ("GFLAGS_INTTYPES_FORMAT_${INTTYPES_FORMAT}" 1)
288
289 if (MSVC)
290   set (HAVE_strtoll 0)
291   set (HAVE_strtoq  0)
292 else ()
293   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 ()
297 endif ()
298
299 if (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 ()
318 else ()
319   set (HAVE_PTHREAD 0)
320 endif ()
321
322 # ----------------------------------------------------------------------------
323 # source files - excluding root subdirectory and/or .in suffix
324 set (PUBLIC_HDRS
325   "gflags.h"
326   "gflags_declare.h"
327   "gflags_completions.h"
328 )
329
330 if (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 ()
339 else ()
340   set (INCLUDE_GFLAGS_NS_H)
341 endif ()
342
343 set (PRIVATE_HDRS
344   "config.h"
345   "util.h"
346   "mutex.h"
347 )
348
349 set (GFLAGS_SRCS
350   "gflags.cc"
351   "gflags_reporting.cc"
352   "gflags_completions.cc"
353 )
354
355 if (OS_WINDOWS)
356   list (APPEND PRIVATE_HDRS "windows_port.h")
357   list (APPEND GFLAGS_SRCS  "windows_port.cc")
358 endif ()
359
360 # ----------------------------------------------------------------------------
361 # configure source files
362 if (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 ()
368 endif ()
369
370 # whenever we build a shared library (DLL on Windows), configure the public
371 # headers of the API for use of this shared library rather than the optionally
372 # also build statically linked library; users can override GFLAGS_DLL_DECL
373 # in particular, this done by setting the INTERFACE_COMPILE_DEFINITIONS of
374 # static libraries to include an empty definition for GFLAGS_DLL_DECL
375 if (NOT DEFINED GFLAGS_IS_A_DLL)
376   if (BUILD_SHARED_LIBS)
377     set (GFLAGS_IS_A_DLL 1)
378   else ()
379     set (GFLAGS_IS_A_DLL 0)
380   endif ()
381 endif ()
382
383 configure_headers (PUBLIC_HDRS  ${PUBLIC_HDRS})
384 configure_sources (PRIVATE_HDRS ${PRIVATE_HDRS})
385 configure_sources (GFLAGS_SRCS  ${GFLAGS_SRCS})
386
387 # ----------------------------------------------------------------------------
388 # output directories
389 if (NOT GFLAGS_IS_SUBPROJECT)
390   set (CMAKE_RUNTIME_OUTPUT_DIRECTORY "bin")
391   set (CMAKE_LIBRARY_OUTPUT_DIRECTORY "lib")
392   set (CMAKE_ARCHIVE_OUTPUT_DIRECTORY "lib")
393 endif ()
394
395 # ----------------------------------------------------------------------------
396 # installation directories
397 if (OS_WINDOWS)
398   set (RUNTIME_INSTALL_DIR Bin)
399   set (LIBRARY_INSTALL_DIR Lib)
400   set (INCLUDE_INSTALL_DIR Include)
401   set (CONFIG_INSTALL_DIR  CMake)
402 else ()
403   set (RUNTIME_INSTALL_DIR bin)
404   # The LIB_INSTALL_DIR and LIB_SUFFIX variables are used by the Fedora
405   # package maintainers. Also package maintainers of other distribution
406   # packages need to be able to specify the name of the library directory.
407   if (NOT GFLAGS_LIBRARY_INSTALL_DIR AND LIB_INSTALL_DIR)
408     set (GFLAGS_LIBRARY_INSTALL_DIR "${LIB_INSTALL_DIR}")
409   endif ()
410   gflags_define (PATH LIBRARY_INSTALL_DIR "Directory of installed libraries, e.g., \"lib64\"" "lib${LIB_SUFFIX}")
411   gflags_property (LIBRARY_INSTALL_DIR ADVANCED TRUE)
412   set (INCLUDE_INSTALL_DIR include)
413   set (CONFIG_INSTALL_DIR  ${LIBRARY_INSTALL_DIR}/cmake/${PACKAGE_NAME})
414 endif ()
415
416 # ----------------------------------------------------------------------------
417 # add library targets
418 set (TARGETS)
419 # static vs. shared
420 foreach (TYPE IN ITEMS STATIC SHARED)
421   if (BUILD_${TYPE}_LIBS)
422     string (TOLOWER "${TYPE}" type)
423     # whether or not targets are a DLL
424     if (OS_WINDOWS AND "^${TYPE}$" STREQUAL "^SHARED$")
425       set (GFLAGS_IS_A_DLL 1)
426     else ()
427       set (GFLAGS_IS_A_DLL 0)
428     endif ()
429     # filename suffix for static libraries on Windows
430     if (OS_WINDOWS AND "^${TYPE}$" STREQUAL "^STATIC$")
431       set (type_suffix "_${type}")
432     else ()
433       set (type_suffix "")
434     endif ()
435     # multi-threaded vs. single-threaded
436     foreach (opts IN ITEMS "" _nothreads)
437       if (BUILD_gflags${opts}_LIB)
438         set (target_name "gflags${opts}_${type}")
439         add_library (${target_name} ${TYPE} ${GFLAGS_SRCS} ${PRIVATE_HDRS} ${PUBLIC_HDRS})
440         set_target_properties (${target_name} PROPERTIES
441           OUTPUT_NAME "gflags${opts}${type_suffix}"
442           VERSION     "${PACKAGE_VERSION}"
443           SOVERSION   "${PACKAGE_SOVERSION}"
444         )
445         set (include_dirs "$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/include>")
446         if (INSTALL_HEADERS)
447           list (APPEND include_dirs "$<INSTALL_INTERFACE:${INCLUDE_INSTALL_DIR}>")
448         endif ()
449         target_include_directories (${target_name}
450           PUBLIC  "${include_dirs}"
451           PRIVATE "${PROJECT_SOURCE_DIR}/src;${PROJECT_BINARY_DIR}/include/${GFLAGS_INCLUDE_DIR}"
452         )
453         target_compile_definitions (${target_name} PUBLIC GFLAGS_IS_A_DLL=${GFLAGS_IS_A_DLL})
454         if (opts MATCHES "nothreads")
455           target_compile_definitions (${target_name} PRIVATE NO_THREADS)
456         elseif (CMAKE_USE_PTHREADS_INIT)
457           target_link_libraries (${target_name} ${CMAKE_THREAD_LIBS_INIT})
458         endif ()
459         if (HAVE_SHLWAPI_H)
460           target_link_libraries (${target_name} shlwapi.lib)
461         endif ()
462         list (APPEND TARGETS ${target_name})
463         # 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 ()
468           add_dependencies (gflags${opts} ${target_name})
469         endif ()
470       endif ()
471     endforeach ()
472   endif ()
473 endforeach ()
474
475 # add ALIAS target for use in super-project, prefer static over shared, single-threaded over multi-threaded
476 if (GFLAGS_IS_SUBPROJECT)
477   foreach (type IN ITEMS static shared)
478     foreach (opts IN ITEMS "_nothreads" "")
479       if (TARGET gflags${opts}_${type})
480         add_library (gflags ALIAS gflags${opts}_${type})
481         break ()
482       endif ()
483     endforeach ()
484     if (TARGET gflags)
485        break ()
486     endif ()
487   endforeach ()
488 endif ()
489
490 # ----------------------------------------------------------------------------
491 # installation rules
492 set (EXPORT_NAME ${PACKAGE_NAME}-targets)
493 file (RELATIVE_PATH INSTALL_PREFIX_REL2CONFIG_DIR "${CMAKE_INSTALL_PREFIX}/${CONFIG_INSTALL_DIR}" "${CMAKE_INSTALL_PREFIX}")
494 configure_file (cmake/config.cmake.in  "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-config-install.cmake" @ONLY)
495 configure_file (cmake/version.cmake.in "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-config-version.cmake" @ONLY)
496
497 if (BUILD_SHARED_LIBS AND INSTALL_SHARED_LIBS)
498   foreach (opts IN ITEMS "" _nothreads)
499     if (BUILD_gflags${opts}_LIB)
500       install (TARGETS gflags${opts}_shared DESTINATION ${LIBRARY_INSTALL_DIR} EXPORT ${EXPORT_NAME})
501     endif ()
502   endforeach ()
503 endif ()
504 if (BUILD_STATIC_LIBS AND INSTALL_STATIC_LIBS)
505   foreach (opts IN ITEMS "" _nothreads)
506     if (BUILD_gflags${opts}_LIB)
507       install (TARGETS gflags${opts}_static DESTINATION ${LIBRARY_INSTALL_DIR} EXPORT ${EXPORT_NAME})
508     endif ()
509   endforeach ()
510 endif ()
511
512 if (INSTALL_HEADERS)
513   install (FILES ${PUBLIC_HDRS} DESTINATION ${INCLUDE_INSTALL_DIR}/${GFLAGS_INCLUDE_DIR})
514   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   )
523   install (EXPORT ${EXPORT_NAME} DESTINATION ${CONFIG_INSTALL_DIR})
524   if (UNIX)
525     install (PROGRAMS src/gflags_completions.sh DESTINATION ${RUNTIME_INSTALL_DIR})
526   endif ()
527 endif ()
528
529 # ----------------------------------------------------------------------------
530 # support direct use of build tree
531 set (INSTALL_PREFIX_REL2CONFIG_DIR .)
532 export (TARGETS ${TARGETS} FILE "${PROJECT_BINARY_DIR}/${EXPORT_NAME}.cmake")
533 if (REGISTER_BUILD_DIR)
534   export (PACKAGE ${PACKAGE_NAME})
535 endif ()
536 if (REGISTER_INSTALL_PREFIX)
537   register_gflags_package(${CONFIG_INSTALL_DIR})
538 endif ()
539 configure_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
543 if (BUILD_TESTING)
544   include (CTest)
545   enable_testing ()
546   add_subdirectory (test)
547 endif ()
548
549 # ----------------------------------------------------------------------------
550 # packaging
551 if (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"
559                      "\n  INSTALL_SHARED_LIBS=ON"
560                      "\nRecommended options for generation of development package:"
561                      "\n  BUILD_SHARED_LIBS=ON"
562                      "\n  BUILD_STATIC_LIBS=ON"
563                      "\n  INSTALL_HEADERS=ON"
564                      "\n  INSTALL_SHARED_LIBS=ON"
565                      "\n  INSTALL_STATIC_LIBS=ON")
566   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
585   # 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
588   # common package information
589   set (CPACK_PACKAGE_VENDOR              "Andreas Schuh")
590   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.")
597   set (CPACK_RESOURCE_FILE_WELCOME       "${CMAKE_CURRENT_BINARY_DIR}/README.txt")
598   set (CPACK_RESOURCE_FILE_LICENSE       "${CMAKE_CURRENT_LIST_DIR}/COPYING.txt")
599   set (CPACK_PACKAGE_DESCRIPTION_FILE    "${CMAKE_CURRENT_BINARY_DIR}/README.txt")
600   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
605   # RPM package information -- used in cmake/package.cmake.in also for DEB
606   set (CPACK_RPM_PACKAGE_GROUP   "Development/Libraries")
607   set (CPACK_RPM_PACKAGE_LICENSE "BSD")
608   set (CPACK_RPM_PACKAGE_URL     "http://gflags.github.io/gflags")
609   set (CPACK_RPM_CHANGELOG_FILE  "${CMAKE_CURRENT_LIST_DIR}/ChangeLog.txt")
610
611   if (INSTALL_HEADERS)
612     set (CPACK_RESOURCE_FILE_README "${CMAKE_CURRENT_LIST_DIR}/doc/index.html")
613   else ()
614     set (CPACK_RESOURCE_FILE_README "${CMAKE_CURRENT_LIST_DIR}/cmake/README_runtime.txt")
615   endif ()
616
617   # system/architecture
618   if (WINDOWS)
619     if (CMAKE_CL_64)
620       set (CPACK_SYSTEM_NAME "win64")
621     else ()
622       set (CPACK_SYSTEM_NAME "win32")
623     endif ()
624     set (CPACK_PACKAGE_ARCHITECTURE)
625   elseif (APPLE)
626     set (CPACK_PACKAGE_ARCHITECTURE darwin)
627   else ()
628     string (TOLOWER "${CMAKE_SYSTEM_NAME}" CPACK_SYSTEM_NAME)
629     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 ()
648   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
656   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}")
660   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
676 endif () # BUILD_PACKAGING