7cf93a4e86508d5d1de0ae5671908da046a3e127
[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 # ----------------------------------------------------------------------------
137 # options
138
139 # maintain binary backwards compatibility with gflags library version <= 2.0,
140 # but at the same time enable the use of the preferred new "gflags" namespace
141 gflags_define (STRING NAMESPACE "Name(s) of library namespace (separate multiple options by semicolon)" "google;${PACKAGE_NAME}" "${PACKAGE_NAME}")
142 gflags_property (NAMESPACE ADVANCED TRUE)
143 set (GFLAGS_NAMESPACE_SECONDARY "${NAMESPACE}")
144 list (REMOVE_DUPLICATES GFLAGS_NAMESPACE_SECONDARY)
145 if (NOT GFLAGS_NAMESPACE_SECONDARY)
146   message (FATAL_ERROR "GFLAGS_NAMESPACE must be set to one (or more) valid C++ namespace identifier(s separated by semicolon \";\").")
147 endif ()
148 foreach (ns IN LISTS GFLAGS_NAMESPACE_SECONDARY)
149   if (NOT ns MATCHES "^[a-zA-Z][a-zA-Z0-9_]*$")
150     message (FATAL_ERROR "GFLAGS_NAMESPACE contains invalid namespace identifier: ${ns}")
151   endif ()
152 endforeach ()
153 list (GET       GFLAGS_NAMESPACE_SECONDARY 0 GFLAGS_NAMESPACE)
154 list (REMOVE_AT GFLAGS_NAMESPACE_SECONDARY 0)
155
156 # cached build options when gflags is not a subproject, otherwise non-cached CMake variables
157 # usage: gflags_define(BOOL <name> <doc> <default> [<subproject default>])
158 gflags_define (BOOL BUILD_SHARED_LIBS          "Request build of shared libraries."                                       OFF OFF)
159 gflags_define (BOOL BUILD_STATIC_LIBS          "Request build of static libraries (default if BUILD_SHARED_LIBS is OFF)." OFF ON)
160 gflags_define (BOOL BUILD_gflags_LIB           "Request build of the multi-threaded gflags library."                      ON  OFF)
161 gflags_define (BOOL BUILD_gflags_nothreads_LIB "Request build of the single-threaded gflags library."                     ON  ON)
162 gflags_define (BOOL BUILD_PACKAGING            "Enable build of distribution packages using CPack."                       OFF OFF)
163 gflags_define (BOOL BUILD_TESTING              "Enable build of the unit tests and their execution using CTest."          OFF OFF)
164 gflags_define (BOOL INSTALL_HEADERS            "Request installation of headers and other development files."             ON  OFF)
165 gflags_define (BOOL INSTALL_SHARED_LIBS        "Request installation of shared libraries."                                ON  ON)
166 gflags_define (BOOL INSTALL_STATIC_LIBS        "Request installation of static libraries."                                ON  OFF)
167
168 gflags_property (BUILD_STATIC_LIBS   ADVANCED TRUE)
169 gflags_property (INSTALL_HEADERS     ADVANCED TRUE)
170 gflags_property (INSTALL_SHARED_LIBS ADVANCED TRUE)
171 gflags_property (INSTALL_STATIC_LIBS ADVANCED TRUE)
172
173 if (NOT GFLAGS_IS_SUBPROJECT)
174   foreach (varname IN ITEMS CMAKE_INSTALL_PREFIX)
175     gflags_property (${varname} ADVANCED FALSE)
176   endforeach ()
177   foreach (varname IN ITEMS CMAKE_CONFIGURATION_TYPES CMAKE_OSX_ARCHITECTURES CMAKE_OSX_DEPLOYMENT_TARGET CMAKE_OSX_SYSROOT)
178     gflags_property (${varname} ADVANCED TRUE)
179   endforeach ()
180   if (NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CXX_FLAGS)
181     gflags_set (CMAKE_BUILD_TYPE Release)
182   endif ()
183   if (CMAKE_CONFIGURATION_TYPES)
184     gflags_property (CMAKE_BUILD_TYPE STRINGS "${CMAKE_CONFIGURATION_TYPES}")
185   endif ()
186 endif () # NOT GFLAGS_IS_SUBPROJECT
187
188 if (NOT BUILD_SHARED_LIBS AND NOT BUILD_STATIC_LIBS)
189   set (BUILD_STATIC_LIBS ON)
190 endif ()
191 if (NOT BUILD_gflags_LIB AND NOT BUILD_gflags_nothreads_LIB)
192   message (FATAL_ERROR "At least one of [GFLAGS_]BUILD_gflags_LIB and [GFLAGS_]BUILD_gflags_nothreads_LIB must be ON.")
193 endif ()
194
195 gflags_define (STRING INCLUDE_DIR "Name of include directory of installed header files relative to CMAKE_INSTALL_PREFIX/include/" "${PACKAGE_NAME}")
196 gflags_property (INCLUDE_DIR ADVANCED TRUE)
197 if (IS_ABSOLUTE INCLUDE_DIR)
198   message (FATAL_ERROR "[GFLAGS_]INCLUDE_DIR must be a path relative to CMAKE_INSTALL_PREFIX/include/")
199 endif ()
200 if (INCLUDE_DIR MATCHES "^\\.\\.[/\\]")
201   message (FATAL_ERROR "[GFLAGS_]INCLUDE_DIR must not start with parent directory reference (../)")
202 endif ()
203 set (GFLAGS_INCLUDE_DIR "${INCLUDE_DIR}")
204
205 # ----------------------------------------------------------------------------
206 # system checks
207 include (CheckTypeSize)
208 include (CheckIncludeFileCXX)
209 include (CheckCXXSymbolExists)
210
211 if (WIN32 AND NOT CYGWIN)
212   set (OS_WINDOWS 1)
213 else ()
214   set (OS_WINDOWS 0)
215 endif ()
216
217 if (MSVC)
218   set (HAVE_SYS_TYPES_H 1)
219   set (HAVE_STDINT_H    1)
220   set (HAVE_STDDEF_H    1) # used by CheckTypeSize module
221   set (HAVE_INTTYPES_H  0)
222   set (HAVE_UNISTD_H    0)
223   set (HAVE_SYS_STAT_H  1)
224   set (HAVE_SHLWAPI_H   1)
225 else ()
226   foreach (fname IN ITEMS unistd stdint inttypes sys/types sys/stat fnmatch)
227     string (TOUPPER "${fname}" FNAME)
228     string (REPLACE "/" "_" FNAME "${FNAME}")
229     if (NOT HAVE_${FNAME}_H)
230       check_include_file_cxx ("${fname}.h" HAVE_${FNAME}_H)
231     endif ()
232   endforeach ()
233   # the following are used in #if directives not #ifdef
234   bool_to_int (HAVE_STDINT_H)
235   bool_to_int (HAVE_SYS_TYPES_H)
236   bool_to_int (HAVE_INTTYPES_H)
237   if (NOT HAVE_FNMATCH_H AND OS_WINDOWS)
238     check_include_file_cxx ("shlwapi.h" HAVE_SHLWAPI_H)
239   endif ()
240 endif ()
241
242 gflags_define (STRING INTTYPES_FORMAT "Format of integer types: \"C99\" (uint32_t), \"BSD\" (u_int32_t), \"VC7\" (__int32)" "")
243 gflags_property (INTTYPES_FORMAT STRINGS "C99;BSD;VC7")
244 gflags_property (INTTYPES_FORMAT ADVANCED TRUE)
245 if (NOT INTTYPES_FORMAT)
246   set (TYPES uint32_t u_int32_t)
247   if (MSVC)
248     list (INSERT TYPES 0 __int32)
249   endif ()
250   foreach (type IN LISTS TYPES)
251     check_type_size (${type} ${type} LANGUAGE CXX)
252     if (HAVE_${type})
253       break ()
254     endif ()
255   endforeach ()
256   if (HAVE_uint32_t)
257     gflags_set (INTTYPES_FORMAT C99)
258   elseif (HAVE_u_int32_t)
259     gflags_set (INTTYPES_FORMAT BSD)
260   elseif (HAVE___int32)
261     gflags_set (INTTYPES_FORMAT VC7)
262   else ()
263     gflags_property (INTTYPES_FORMAT ADVANCED FALSE)
264     message (FATAL_ERROR "Do not know how to define a 32-bit integer quantity on your system!"
265                          " Neither uint32_t, u_int32_t, nor __int32 seem to be available."
266                          " Set [GFLAGS_]INTTYPES_FORMAT to either C99, BSD, or VC7 and try again.")
267   endif ()
268 endif ()
269 # use of special characters in strings to circumvent bug #0008226
270 if ("^${INTTYPES_FORMAT}$" STREQUAL "^WIN$")
271   gflags_set (INTTYPES_FORMAT VC7)
272 endif ()
273 if (NOT INTTYPES_FORMAT MATCHES "^(C99|BSD|VC7)$")
274   message (FATAL_ERROR "Invalid value for [GFLAGS_]INTTYPES_FORMAT! Choose one of \"C99\", \"BSD\", or \"VC7\"")
275 endif ()
276 set (GFLAGS_INTTYPES_FORMAT "${INTTYPES_FORMAT}")
277 set (GFLAGS_INTTYPES_FORMAT_C99 0)
278 set (GFLAGS_INTTYPES_FORMAT_BSD 0)
279 set (GFLAGS_INTTYPES_FORMAT_VC7 0)
280 set ("GFLAGS_INTTYPES_FORMAT_${INTTYPES_FORMAT}" 1)
281
282 if (MSVC)
283   set (HAVE_strtoll 0)
284   set (HAVE_strtoq  0)
285 else ()
286   check_cxx_symbol_exists (strtoll stdlib.h HAVE_STRTOLL)
287   if (NOT HAVE_STRTOLL)
288     check_cxx_symbol_exists (strtoq stdlib.h HAVE_STRTOQ)
289   endif ()
290 endif ()
291
292 if (BUILD_gflags_LIB)
293   set (CMAKE_THREAD_PREFER_PTHREAD TRUE)
294   find_package (Threads)
295   if (Threads_FOUND AND CMAKE_USE_PTHREADS_INIT)
296     set (HAVE_PTHREAD 1)
297     check_type_size (pthread_rwlock_t RWLOCK LANGUAGE CXX)
298   else ()
299     set (HAVE_PTHREAD 0)
300   endif ()
301   if (UNIX AND NOT HAVE_PTHREAD)
302     if (CMAKE_HAVE_PTHREAD_H)
303       set (what "library")
304     else ()
305       set (what ".h file")
306     endif ()
307     message (FATAL_ERROR "Could not find pthread${what}. Check the log file"
308                          "\n\t${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log"
309                          "\nor disable the build of the multi-threaded gflags library (BUILD_gflags_LIB=OFF).")
310   endif ()
311 else ()
312   set (HAVE_PTHREAD 0)
313 endif ()
314
315 # ----------------------------------------------------------------------------
316 # source files - excluding root subdirectory and/or .in suffix
317 set (PUBLIC_HDRS
318   "gflags.h"
319   "gflags_declare.h"
320   "gflags_completions.h"
321 )
322
323 if (GFLAGS_NAMESPACE_SECONDARY)
324   set (INCLUDE_GFLAGS_NS_H "// Import gflags library symbols into alternative/deprecated namespace(s)")
325   foreach (ns IN LISTS GFLAGS_NAMESPACE_SECONDARY)
326     string (TOUPPER "${ns}" NS)
327     set (gflags_ns_h "${PROJECT_BINARY_DIR}/include/${GFLAGS_INCLUDE_DIR}/gflags_${ns}.h")
328     configure_file ("${PROJECT_SOURCE_DIR}/src/gflags_ns.h.in" "${gflags_ns_h}" @ONLY)
329     list (APPEND PUBLIC_HDRS "${gflags_ns_h}")
330     set (INCLUDE_GFLAGS_NS_H "${INCLUDE_GFLAGS_NS_H}\n#include \"gflags_${ns}.h\"")
331   endforeach ()
332 else ()
333   set (INCLUDE_GFLAGS_NS_H)
334 endif ()
335
336 set (PRIVATE_HDRS
337   "config.h"
338   "util.h"
339   "mutex.h"
340 )
341
342 set (GFLAGS_SRCS
343   "gflags.cc"
344   "gflags_reporting.cc"
345   "gflags_completions.cc"
346 )
347
348 if (OS_WINDOWS)
349   list (APPEND PRIVATE_HDRS "windows_port.h")
350   list (APPEND GFLAGS_SRCS  "windows_port.cc")
351 endif ()
352
353 # ----------------------------------------------------------------------------
354 # configure source files
355 if (NOT DEFINED GFLAGS_ATTRIBUTE_UNUSED)
356   if (CMAKE_COMPILER_IS_GNUCXX)
357     set (GFLAGS_ATTRIBUTE_UNUSED "__attribute((unused))")
358   else ()
359     set (GFLAGS_ATTRIBUTE_UNUSED)
360   endif ()
361 endif ()
362
363 # whenever we build a shared library (DLL on Windows), configure the public
364 # headers of the API for use of this library rather than the optionally
365 # also build statically linked library; users can override GFLAGS_DLL_DECL
366 if (NOT DEFINED GFLAGS_IS_A_DLL)
367   if (BUILD_SHARED_LIBS)
368     set (GFLAGS_IS_A_DLL 1)
369   else ()
370     set (GFLAGS_IS_A_DLL 0)
371   endif ()
372 endif ()
373
374 configure_headers (PUBLIC_HDRS  ${PUBLIC_HDRS})
375 configure_sources (PRIVATE_HDRS ${PRIVATE_HDRS})
376 configure_sources (GFLAGS_SRCS  ${GFLAGS_SRCS})
377
378 # ----------------------------------------------------------------------------
379 # output directories
380 if (NOT GFLAGS_IS_SUBPROJECT)
381   set (CMAKE_RUNTIME_OUTPUT_DIRECTORY "bin")
382   set (CMAKE_LIBRARY_OUTPUT_DIRECTORY "lib")
383   set (CMAKE_ARCHIVE_OUTPUT_DIRECTORY "lib")
384 endif ()
385
386 # ----------------------------------------------------------------------------
387 # installation directories
388 if (OS_WINDOWS)
389   set (RUNTIME_INSTALL_DIR Bin)
390   set (LIBRARY_INSTALL_DIR Lib)
391   set (INCLUDE_INSTALL_DIR Include)
392   set (CONFIG_INSTALL_DIR  CMake)
393 else ()
394   set (RUNTIME_INSTALL_DIR bin)
395   # The LIB_INSTALL_DIR and LIB_SUFFIX variables are used by the Fedora
396   # package maintainers. Also package maintainers of other distribution
397   # packages need to be able to specify the name of the library directory.
398   if (NOT GFLAGS_LIBRARY_INSTALL_DIR AND LIB_INSTALL_DIR)
399     set (GFLAGS_LIBRARY_INSTALL_DIR "${LIB_INSTALL_DIR}")
400   endif ()
401   gflags_define (PATH LIBRARY_INSTALL_DIR "Directory of installed libraries, e.g., \"lib64\"" "lib${LIB_SUFFIX}")
402   gflags_property (LIBRARY_INSTALL_DIR ADVANCED TRUE)
403   set (INCLUDE_INSTALL_DIR include)
404   set (CONFIG_INSTALL_DIR  ${LIBRARY_INSTALL_DIR}/cmake/${PACKAGE_NAME})
405 endif ()
406
407 # ----------------------------------------------------------------------------
408 # add library targets
409 set (TARGETS)
410 # static vs. shared
411 foreach (TYPE IN ITEMS STATIC SHARED)
412   if (BUILD_${TYPE}_LIBS)
413     string (TOLOWER "${TYPE}" type)
414     # whether or not targets are a DLL
415     if (OS_WINDOWS AND "^${TYPE}$" STREQUAL "^SHARED$")
416       set (GFLAGS_IS_A_DLL 1)
417     else ()
418       set (GFLAGS_IS_A_DLL 0)
419     endif ()
420     # filename suffix for static libraries on Windows
421     if (OS_WINDOWS AND "^${TYPE}$" STREQUAL "^STATIC$")
422       set (type_suffix "_${type}")
423     else ()
424       set (type_suffix "")
425     endif ()
426     # multi-threaded vs. single-threaded
427     foreach (opts IN ITEMS "" _nothreads)
428       if (BUILD_gflags${opts}_LIB)
429         add_library (gflags${opts}_${type} ${TYPE} ${GFLAGS_SRCS} ${PRIVATE_HDRS} ${PUBLIC_HDRS})
430         set (include_dirs "$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/include>")
431         if (INSTALL_HEADERS)
432           list (APPEND include_dirs "$<INSTALL_INTERFACE:${INCLUDE_INSTALL_DIR}>")
433         endif ()
434         target_include_directories (gflags${opts}_${type}
435           PUBLIC  "${include_dirs}"
436           PRIVATE "${PROJECT_SOURCE_DIR}/src;${PROJECT_BINARY_DIR}/include/${GFLAGS_INCLUDE_DIR}"
437         )
438         if (opts MATCHES "nothreads")
439           set (defines "GFLAGS_IS_A_DLL=${GFLAGS_IS_A_DLL};NO_THREADS")
440         else ()
441           set (defines "GFLAGS_IS_A_DLL=${GFLAGS_IS_A_DLL}")
442           if (CMAKE_USE_PTHREADS_INIT)
443             target_link_libraries (gflags${opts}_${type} ${CMAKE_THREAD_LIBS_INIT})
444           endif ()
445         endif ()
446         set_target_properties (
447           gflags${opts}_${type} PROPERTIES COMPILE_DEFINITIONS "${defines}"
448                                            OUTPUT_NAME         "gflags${opts}${type_suffix}"
449                                            VERSION             "${PACKAGE_VERSION}"
450                                            SOVERSION           "${PACKAGE_SOVERSION}"
451         )
452         if (HAVE_SHLWAPI_H)
453           target_link_libraries (gflags${opts}_${type} shlwapi.lib)
454         endif ()
455         list (APPEND TARGETS gflags${opts}_${type})
456         # add convenience make target for build of both shared and static libraries
457         if (NOT GFLAGS_IS_SUBPROJECT)
458           if (NOT TARGET gflags${opts})
459             add_custom_target (gflags${opts})
460           endif ()
461           add_dependencies (gflags${opts} gflags${opts}_${type})
462         endif ()
463       endif ()
464     endforeach ()
465   endif ()
466 endforeach ()
467
468 # add ALIAS target for use in super-project, prefer static over shared, single-threaded over multi-threaded
469 if (GFLAGS_IS_SUBPROJECT)
470   foreach (type IN ITEMS static shared)
471     foreach (opts IN ITEMS "_nothreads" "")
472       if (TARGET gflags${opts}_${type})
473         add_library (gflags ALIAS gflags${opts}_${type})
474         break ()
475       endif ()
476     endforeach ()
477     if (TARGET gflags)
478        break ()
479     endif ()
480   endforeach ()
481 endif ()
482
483 # ----------------------------------------------------------------------------
484 # installation rules
485 set (EXPORT_NAME ${PACKAGE_NAME}-targets)
486 file (RELATIVE_PATH INSTALL_PREFIX_REL2CONFIG_DIR "${CMAKE_INSTALL_PREFIX}/${CONFIG_INSTALL_DIR}" "${CMAKE_INSTALL_PREFIX}")
487 configure_file (cmake/config.cmake.in  "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-config-install.cmake" @ONLY)
488 configure_file (cmake/version.cmake.in "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-config-version.cmake" @ONLY)
489
490 if (BUILD_SHARED_LIBS AND INSTALL_SHARED_LIBS)
491   foreach (opts IN ITEMS "" _nothreads)
492     if (BUILD_gflags${opts}_LIB)
493       install (TARGETS gflags${opts}_shared DESTINATION ${LIBRARY_INSTALL_DIR} EXPORT ${EXPORT_NAME})
494     endif ()
495   endforeach ()
496 endif ()
497 if (BUILD_STATIC_LIBS AND INSTALL_STATIC_LIBS)
498   foreach (opts IN ITEMS "" _nothreads)
499     if (BUILD_gflags${opts}_LIB)
500       install (TARGETS gflags${opts}_static DESTINATION ${LIBRARY_INSTALL_DIR} EXPORT ${EXPORT_NAME})
501     endif ()
502   endforeach ()
503 endif ()
504
505 if (INSTALL_HEADERS)
506   install (FILES ${PUBLIC_HDRS} DESTINATION ${INCLUDE_INSTALL_DIR}/${GFLAGS_INCLUDE_DIR})
507   install (
508     FILES "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-config-install.cmake"
509     RENAME ${PACKAGE_NAME}-config.cmake
510     DESTINATION ${CONFIG_INSTALL_DIR}
511   )
512   install (
513     FILES "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-config-version.cmake"
514     DESTINATION ${CONFIG_INSTALL_DIR}
515   )
516   install (EXPORT ${EXPORT_NAME} DESTINATION ${CONFIG_INSTALL_DIR})
517   if (UNIX)
518     install (PROGRAMS src/gflags_completions.sh DESTINATION ${RUNTIME_INSTALL_DIR})
519   endif ()
520 endif ()
521
522 # ----------------------------------------------------------------------------
523 # support direct use of build tree
524 set (INSTALL_PREFIX_REL2CONFIG_DIR .)
525 export (TARGETS ${TARGETS} FILE "${PROJECT_BINARY_DIR}/${EXPORT_NAME}.cmake")
526 export (PACKAGE gflags)
527 configure_file (cmake/config.cmake.in "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-config.cmake" @ONLY)
528
529 # ----------------------------------------------------------------------------
530 # testing - MUST follow the generation of the build tree config file
531 if (BUILD_TESTING)
532   include (CTest)
533   enable_testing ()
534   add_subdirectory (test)
535 endif ()
536
537 # ----------------------------------------------------------------------------
538 # packaging
539 if (BUILD_PACKAGING)
540
541   if (NOT BUILD_SHARED_LIBS AND NOT INSTALL_HEADERS)
542     message (WARNING "Package will contain static libraries without headers!"
543                      "\nRecommended options for generation of runtime package:"
544                      "\n  BUILD_SHARED_LIBS=ON"
545                      "\n  BUILD_STATIC_LIBS=OFF"
546                      "\n  INSTALL_HEADERS=OFF"
547                      "\n  INSTALL_SHARED_LIBS=ON"
548                      "\nRecommended options for generation of development package:"
549                      "\n  BUILD_SHARED_LIBS=ON"
550                      "\n  BUILD_STATIC_LIBS=ON"
551                      "\n  INSTALL_HEADERS=ON"
552                      "\n  INSTALL_SHARED_LIBS=ON"
553                      "\n  INSTALL_STATIC_LIBS=ON")
554   endif ()
555
556   # default package generators
557   if (APPLE)
558     set (PACKAGE_GENERATOR        "PackageMaker")
559     set (PACKAGE_SOURCE_GENERATOR "TGZ;ZIP")
560   elseif (UNIX)
561     set (PACKAGE_GENERATOR        "DEB;RPM")
562     set (PACKAGE_SOURCE_GENERATOR "TGZ;ZIP")
563   else ()
564     set (PACKAGE_GENERATOR        "ZIP")
565     set (PACKAGE_SOURCE_GENERATOR "ZIP")
566   endif ()
567
568   # used package generators
569   set (CPACK_GENERATOR        "${PACKAGE_GENERATOR}"        CACHE STRING "List of binary package generators (CPack).")
570   set (CPACK_SOURCE_GENERATOR "${PACKAGE_SOURCE_GENERATOR}" CACHE STRING "List of source package generators (CPack).")
571   mark_as_advanced (CPACK_GENERATOR CPACK_SOURCE_GENERATOR)
572
573   # some package generators (e.g., PackageMaker) do not allow .md extension
574   configure_file ("${CMAKE_CURRENT_LIST_DIR}/README.md" "${CMAKE_CURRENT_BINARY_DIR}/README.txt" COPYONLY)
575
576   # common package information
577   set (CPACK_PACKAGE_VENDOR              "Andreas Schuh")
578   set (CPACK_PACKAGE_CONTACT             "google-gflags@googlegroups.com")
579   set (CPACK_PACKAGE_NAME                "${PACKAGE_NAME}")
580   set (CPACK_PACKAGE_VERSION             "${PACKAGE_VERSION}")
581   set (CPACK_PACKAGE_VERSION_MAJOR       "${PACKAGE_VERSION_MAJOR}")
582   set (CPACK_PACKAGE_VERSION_MINOR       "${PACKAGE_VERSION_MINOR}")
583   set (CPACK_PACKAGE_VERSION_PATCH       "${PACKAGE_VERSION_PATCH}")
584   set (CPACK_PACKAGE_DESCRIPTION_SUMMARY "A commandline flags library that allows for distributed flags.")
585   set (CPACK_RESOURCE_FILE_WELCOME       "${CMAKE_CURRENT_BINARY_DIR}/README.txt")
586   set (CPACK_RESOURCE_FILE_LICENSE       "${CMAKE_CURRENT_LIST_DIR}/COPYING.txt")
587   set (CPACK_PACKAGE_DESCRIPTION_FILE    "${CMAKE_CURRENT_BINARY_DIR}/README.txt")
588   set (CPACK_INSTALL_PREFIX              "${CMAKE_INSTALL_PREFIX}")
589   set (CPACK_OUTPUT_FILE_PREFIX          packages)
590   set (CPACK_PACKAGE_RELOCATABLE         TRUE)
591   set (CPACK_MONOLITHIC_INSTALL          TRUE)
592
593   # RPM package information -- used in cmake/package.cmake.in also for DEB
594   set (CPACK_RPM_PACKAGE_GROUP   "Development/Libraries")
595   set (CPACK_RPM_PACKAGE_LICENSE "BSD")
596   set (CPACK_RPM_PACKAGE_URL     "http://gflags.github.io/gflags")
597   set (CPACK_RPM_CHANGELOG_FILE  "${CMAKE_CURRENT_LIST_DIR}/ChangeLog.txt")
598
599   if (INSTALL_HEADERS)
600     set (CPACK_RESOURCE_FILE_README "${CMAKE_CURRENT_LIST_DIR}/doc/index.html")
601   else ()
602     set (CPACK_RESOURCE_FILE_README "${CMAKE_CURRENT_LIST_DIR}/cmake/README_runtime.txt")
603   endif ()
604
605   # system/architecture
606   if (WINDOWS)
607     if (CMAKE_CL_64)
608       set (CPACK_SYSTEM_NAME "win64")
609     else ()
610       set (CPACK_SYSTEM_NAME "win32")
611     endif ()
612     set (CPACK_PACKAGE_ARCHITECTURE)
613   elseif (APPLE)
614     set (CPACK_PACKAGE_ARCHITECTURE darwin)
615   else ()
616     string (TOLOWER "${CMAKE_SYSTEM_NAME}" CPACK_SYSTEM_NAME)
617     if (CMAKE_CXX_FLAGS MATCHES "-m32")
618       set (CPACK_PACKAGE_ARCHITECTURE i386)
619     else ()
620       execute_process (
621         COMMAND         dpkg --print-architecture
622         RESULT_VARIABLE RV
623         OUTPUT_VARIABLE CPACK_PACKAGE_ARCHITECTURE
624       )
625       if (RV EQUAL 0)
626               string (STRIP "${CPACK_PACKAGE_ARCHITECTURE}" CPACK_PACKAGE_ARCHITECTURE)
627       else ()
628         execute_process (COMMAND uname -m OUTPUT_VARIABLE CPACK_PACKAGE_ARCHITECTURE)
629         if (CPACK_PACKAGE_ARCHITECTURE MATCHES "x86_64")
630                 set (CPACK_PACKAGE_ARCHITECTURE amd64)
631         else ()
632           set (CPACK_PACKAGE_ARCHITECTURE i386)
633         endif ()
634       endif ()
635     endif ()
636   endif ()
637
638   # source package settings
639   set (CPACK_SOURCE_TOPLEVEL_TAG      "source")
640   set (CPACK_SOURCE_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}")
641   set (CPACK_SOURCE_IGNORE_FILES      "/\\\\.git/;\\\\.swp$;\\\\.#;/#;\\\\.*~;cscope\\\\.*;/[Bb]uild[.+-_a-zA-Z0-9]*/")
642
643   # default binary package settings
644   set (CPACK_INCLUDE_TOPLEVEL_DIRECTORY TRUE)
645   set (CPACK_PACKAGE_FILE_NAME          "${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}-${CPACK_SYSTEM_NAME}")
646   if (CPACK_PACKAGE_ARCHITECTURE)
647     set (CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_FILE_NAME}-${CPACK_PACKAGE_ARCHITECTURE}")
648   endif ()
649
650   # generator specific configuration file
651   #
652   # allow package maintainers to use their own configuration file
653   # $ cmake -DCPACK_PROJECT_CONFIG_FILE:FILE=/path/to/package/config
654   if (NOT CPACK_PROJECT_CONFIG_FILE)
655     configure_file (
656       "${CMAKE_CURRENT_LIST_DIR}/cmake/package.cmake.in"
657       "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-package.cmake" @ONLY
658     )
659     set (CPACK_PROJECT_CONFIG_FILE "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-package.cmake")
660   endif ()
661
662   include (CPack)
663
664 endif () # BUILD_PACKAGING