1 cmake_minimum_required (VERSION 3.0)
4 cmake_policy (SET CMP0042 NEW)
8 cmake_policy (SET CMP0063 NEW)
15 set (GLOG_MAJOR_VERSION 0)
16 set (GLOG_MINOR_VERSION 3)
17 set (GLOG_PATCH_VERSION 5)
20 ${GLOG_MAJOR_VERSION}.${GLOG_MINOR_VERSION}.${GLOG_PATCH_VERSION})
22 set (CPACK_PACKAGE_NAME glog)
23 set (CPACK_PACKAGE_DESCRIPTION_SUMMARY "")
24 set (CPACK_PACKAGE_VERSION_MAJOR ${GLOG_MAJOR_VERSION})
25 set (CPACK_PACKAGE_VERSION_MINOR ${GLOG_MINOR_VERSION})
26 set (CPACK_PACKAGE_VERSION_PATCH ${GLOG_PATCH_VERSION})
27 set (CPACK_PACKAGE_VERSION ${GLOG_VERSION})
29 option (WITH_GFLAGS "Use gflags" ON)
30 option (WITH_THREADS "Enable multithreading support" ON)
31 option (WITH_TLS "Enable Thread Local Storage (TLS) support" ON)
33 list (APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
35 include (CheckCSourceCompiles)
36 include (CheckCXXCompilerFlag)
37 include (CheckCXXSourceCompiles)
38 include (CheckFunctionExists)
39 include (CheckIncludeFile)
40 include (CheckIncludeFileCXX)
41 include (CheckLibraryExists)
42 include (CheckStructHasMember)
43 include (CheckSymbolExists)
44 include (CheckTypeSize)
45 include (CMakePackageConfigHelpers)
48 include (DetermineGflagsNamespace)
49 include (GNUInstallDirs)
51 set (CMAKE_THREAD_PREFER_PTHREAD 1)
54 find_package (gflags 2.2.0)
57 set (HAVE_LIB_GFLAGS 1)
58 determine_gflags_namespace (gflags_NAMESPACE)
63 find_package (Threads)
66 check_include_file (dlfcn.h HAVE_DLFCN_H)
67 check_include_file (execinfo.h HAVE_EXECINFO_H)
68 check_include_file (glob.h HAVE_GLOB_H)
69 check_include_file (inttypes.h HAVE_INTTYPES_H)
70 check_include_file (libunwind.h HAVE_LIBUNWIND_H)
71 check_include_file (memory.h HAVE_MEMORY_H)
72 check_include_file (pwd.h HAVE_PWD_H)
73 check_include_file (stdint.h HAVE_STDINT_H)
74 check_include_file (stdlib.h HAVE_STDLIB_H)
75 check_include_file (string.h HAVE_STRING_H)
76 check_include_file (strings.h HAVE_STRINGS_H)
77 check_include_file (sys/stat.h HAVE_SYS_STAT_H)
78 check_include_file (sys/syscall.h HAVE_SYS_SYSCALL_H)
79 check_include_file (sys/time.h HAVE_SYS_TIME_H)
80 check_include_file (sys/types.h HAVE_SYS_TYPES_H)
81 check_include_file (sys/utsname.h HAVE_SYS_UTSNAME_H)
82 check_include_file (syscall.h HAVE_SYSCALL_H)
83 check_include_file (syslog.h HAVE_SYSLOG_H)
84 check_include_file (ucontext.h HAVE_UCONTEXT_H)
85 check_include_file (unistd.h HAVE_UNISTD_H)
86 check_include_file (unwind.h HAVE_UNWIND_H)
88 check_include_file_cxx ("ext/hash_map" HAVE_EXT_HASH_MAP)
89 check_include_file_cxx ("ext/hash_set" HAVE_EXT_HASH_SET)
90 check_include_file_cxx ("ext/slist" HAVE_EXT_SLIST)
91 check_include_file_cxx ("tr1/unordered_map" HAVE_TR1_UNORDERED_MAP)
92 check_include_file_cxx ("tr1/unordered_set" HAVE_TR1_UNORDERED_SET)
93 check_include_file_cxx ("unordered_map" HAVE_UNORDERED_MAP)
94 check_include_file_cxx ("unordered_set" HAVE_UNORDERED_SET)
96 check_type_size ("unsigned __int16" HAVE___UINT16)
97 check_type_size (u_int16_t HAVE_U_INT16_T)
98 check_type_size (uint16_t HAVE_UINT16_T)
100 check_function_exists (dladdr HAVE_DLADDR)
101 check_function_exists (fcntl HAVE_FCNTL)
102 check_function_exists (pread HAVE_PREAD)
103 check_function_exists (pwrite HAVE_PWRITE)
104 check_function_exists (sigaction HAVE_SIGACTION)
105 check_function_exists (sigaltstack HAVE_SIGALSTACK)
107 # NOTE gcc does not fail if you pass a non-existent -Wno-* option as an
108 # argument. However, it will happily fail if you pass the corresponding -W*
109 # option. So, we check whether options that disable warnings exist by testing
110 # the availability of the corresponding option that enables the warning. This
111 # eliminates the need to check for compiler for several (mainly Clang) options.
113 check_cxx_compiler_flag (-Wdeprecated HAVE_NO_DEPRECATED)
114 check_cxx_compiler_flag (-Wunnamed-type-template-args
115 HAVE_NO_UNNAMED_TYPE_TEMPLATE_ARGS)
117 # NOTE: Cannot use check_function_exists here since >=vc-14.0 can define
118 # snprintf as an inline function
119 check_symbol_exists (snprintf stdio.h HAVE_SNPRINTF)
121 check_library_exists (unwind get_static_proc_name "" HAVE_LIB_UNWIND)
123 find_library (UNWIND_LIBRARY NAMES unwind DOC "unwind library")
124 mark_as_advanced (UNWIND_LIBRARY)
126 check_c_source_compiles ("
128 static void foo(void) __attribute__ ((unused));
129 int main(void) { return 0; }
130 " HAVE___ATTRIBUTE__)
132 check_c_source_compiles ("
134 static void foo(void) __attribute__ ((visibility(\"default\")));
135 int main(void) { return 0; }
136 " HAVE___ATTRIBUTE__VISIBILITY_DEFAULT)
138 check_c_source_compiles ("
140 static void foo(void) __attribute__ ((visibility(\"hidden\")));
141 int main(void) { return 0; }
142 " HAVE___ATTRIBUTE__VISIBILITY_HIDDEN)
144 check_c_source_compiles ("
145 int main(void) { if (__builtin_expect(0, 0)) return 1; return 0; }
146 " HAVE___BUILTIN_EXPECT)
148 check_c_source_compiles ("
151 int a; if (__sync_val_compare_and_swap(&a, 0, 1)) return 1; return 0;
153 " HAVE___SYNC_VAL_COMPARE_AND_SWAP)
155 check_c_source_compiles ("
156 #define _XOPEN_SOURCE 500
161 pthread_rwlock_init(&l, NULL);
162 pthread_rwlock_rdlock(&l);
167 check_c_source_compiles ("
168 __declspec(selectany) int a;
169 int main(void) { return 0; }
172 check_cxx_source_compiles ("
174 vector<int> t; int main() { }
177 check_cxx_source_compiles ("
179 std::vector<int> t; int main() { }
182 check_cxx_source_compiles ("
184 std::ostream& operator<<(std::ostream&, struct s);
187 " HAVE_USING_OPERATOR)
189 check_cxx_source_compiles ("
190 namespace Outer { namespace Inner { int i = 0; }}
191 using namespace Outer::Inner;;
192 int main() { return i; }
195 check_cxx_source_compiles ("
196 __declspec(thread) int tls;
200 check_cxx_source_compiles ("
201 thread_local int tls;
205 check_cxx_source_compiles ("
206 __attribute__((thread)) int tls;
212 set (GLOG_THREAD_LOCAL_STORAGE "__attribute__((thread))")
213 elseif (HAVE_MSVC_TLS)
214 set (GLOG_THREAD_LOCAL_STORAGE "__declspec(thread)")
215 elseif (HAVE_CXX11_TLS)
216 set (GLOG_THREAD_LOCAL_STORAGE thread_local)
217 endif (HAVE_CYGWIN_TLS)
225 "uc_regs->gregs[PT_NIP]"
240 set (_PC_HEADERS ucontext.h signal.h)
242 if (HAVE_UCONTEXT_H AND NOT PC_FROM_UCONTEXT)
243 foreach (_PC_FIELD ${_PC_FIELDS})
244 foreach (_PC_HEADER ${_PC_HEADERS})
246 ${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/uctfield.c)
247 file (WRITE ${_TMP} "
248 #define _GNU_SOURCE 1
249 #include <${_PC_HEADER}>
253 return u.${_PC_FIELD} == 0;
256 try_compile (HAVE_PC_FROM_UCONTEXT ${CMAKE_CURRENT_BINARY_DIR} ${_TMP}
257 COMPILE_DEFINITIONS _GNU_SOURCE=1)
259 if (HAVE_PC_FROM_UCONTEXT)
260 set (PC_FROM_UCONTEXT ${_PC_FIELD} CACHE)
261 endif (HAVE_PC_FROM_UCONTEXT)
262 endforeach (_PC_HEADER)
263 endforeach (_PC_FIELD)
264 endif (HAVE_UCONTEXT_H AND NOT PC_FROM_UCONTEXT)
266 if (STL_STD_NAMESPACE)
267 set (STL_NAMESPACE std)
268 else (STL_STD_NAMESPACE)
269 set (STL_NAMESPACE "")
270 endif (STL_STD_NAMESPACE)
272 set (GOOGLE_NAMESPACE google)
273 set (_START_GOOGLE_NAMESPACE_ "namespace ${GOOGLE_NAMESPACE} {")
274 set (_END_GOOGLE_NAMESPACE_ "}")
277 set (ac_cv_have___uint16 1)
279 set (ac_cv_have___uint16 0)
280 endif (HAVE___UINT16)
283 set (ac_cv_have_inttypes_h 1)
284 else (HAVE_INTTYPES_H)
285 set (ac_cv_have_inttypes_h 0)
286 endif (HAVE_INTTYPES_H)
289 set (ac_cv_have_libgflags 1)
290 else (HAVE_LIB_GFLAGS)
291 set (ac_cv_have_libgflags 0)
292 endif (HAVE_LIB_GFLAGS)
295 set (ac_cv_have_stdint_h 1)
297 set (ac_cv_have_stdint_h 0)
298 endif (HAVE_STDINT_H)
300 if (HAVE_SYS_TYPES_H)
301 set (ac_cv_have_systypes_h 1)
302 else (HAVE_SYS_TYPES_H)
303 set (ac_cv_have_systypes_h 0)
304 endif (HAVE_SYS_TYPES_H)
307 set (ac_cv_have_u_int16_t 1)
308 else (HAVE_U_INT16_T)
309 set (ac_cv_have_u_int16_t 0)
310 endif (HAVE_U_INT16_T)
313 set (ac_cv_have_uint16_t 1)
315 set (ac_cv_have_uint16_t 0)
316 endif (HAVE_UINT16_T)
319 set (ac_cv_have_unistd_h 1)
321 set (ac_cv_have_unistd_h 0)
322 endif (HAVE_UNISTD_H)
324 set (ac_google_namespace ${GOOGLE_NAMESPACE})
325 set (ac_google_end_namespace ${_END_GOOGLE_NAMESPACE_})
326 set (ac_google_start_namespace ${_START_GOOGLE_NAMESPACE_})
328 if (HAVE___ATTRIBUTE__)
329 set (ac_cv___attribute___noreturn "__attribute__((noreturn))")
330 set (ac_cv___attribute___noinline "__attribute__((noinline))")
331 elseif (HAVE___DECLSPEC)
332 set (ac_cv___attribute___noreturn "__declspec(noreturn)")
333 #set (ac_cv___attribute___noinline "__declspec(noinline)")
334 endif (HAVE___ATTRIBUTE__)
336 if (HAVE___BUILTIN_EXPECT)
337 set (ac_cv_have___builtin_expect 1)
338 else (HAVE___BUILTIN_EXPECT)
339 set (ac_cv_have___builtin_expect 0)
340 endif (HAVE___BUILTIN_EXPECT)
342 if (HAVE_USING_OPERATOR)
343 set (ac_cv_cxx_using_operator 1)
344 else (HAVE_USING_OPERATOR)
345 set (ac_cv_cxx_using_operator 0)
346 endif (HAVE_USING_OPERATOR)
348 set (SIZEOF_VOID_P ${CMAKE_SIZEOF_VOID_P})
350 if (WITH_THREADS AND Threads_FOUND)
351 if (CMAKE_USE_PTHREADS_INIT)
353 endif (CMAKE_USE_PTHREADS_INIT)
354 else (WITH_THREADS AND Threads_FOUND)
356 endif (WITH_THREADS AND Threads_FOUND)
358 set (TEST_SRC_DIR \"${CMAKE_CURRENT_SOURCE_DIR}\")
360 configure_file (src/config.h.cmake.in config.h)
361 configure_file (src/glog/logging.h.in glog/logging.h @ONLY)
362 configure_file (src/glog/raw_logging.h.in glog/raw_logging.h @ONLY)
363 configure_file (src/glog/stl_logging.h.in glog/stl_logging.h @ONLY)
364 configure_file (src/glog/vlog_is_on.h.in glog/vlog_is_on.h @ONLY)
366 set (CMAKE_CXX_VISIBILITY_PRESET default)
367 set (CMAKE_VISIBILITY_INLINES_HIDDEN 1)
370 ${CMAKE_CURRENT_BINARY_DIR}/config.h
371 ${CMAKE_CURRENT_BINARY_DIR}/glog/logging.h
372 ${CMAKE_CURRENT_BINARY_DIR}/glog/raw_logging.h
373 ${CMAKE_CURRENT_BINARY_DIR}/glog/stl_logging.h
374 ${CMAKE_CURRENT_BINARY_DIR}/glog/vlog_is_on.h
375 src/glog/log_severity.h
380 src/base/commandlineflags.h
381 src/base/googleinit.h
394 if (HAVE_PTHREAD OR WIN32)
395 list (APPEND GLOG_SRCS src/signalhandler.cc)
396 endif (HAVE_PTHREAD OR WIN32)
399 list (APPEND GLOG_SRCS
405 add_compile_options ($<$<BOOL:${HAVE_NO_UNNAMED_TYPE_TEMPLATE_ARGS}>:-Wno-unnamed-type-template-args>)
411 set_target_properties (glog PROPERTIES POSITION_INDEPENDENT_CODE ON)
414 target_link_libraries (glog PUBLIC ${UNWIND_LIBRARY})
415 endif (UNWIND_LIBRARY)
418 target_link_libraries (glog PUBLIC ${CMAKE_THREAD_LIBS_INIT})
421 if (WIN32 AND HAVE_SNPRINTF)
422 set_property (SOURCE src/windows/port.cc APPEND PROPERTY COMPILE_DEFINITIONS
424 endif (WIN32 AND HAVE_SNPRINTF)
427 target_link_libraries (glog PUBLIC gflags)
429 if (NOT BUILD_SHARED_LIBS)
430 # Don't use __declspec(dllexport|dllimport) if this is a static build
431 targeT_compile_definitions (glog PUBLIC GFLAGS_DLL_DECLARE_FLAG= GFLAGS_DLL_DEFINE_FLAG=)
432 endif (NOT BUILD_SHARED_LIBS)
435 set_target_properties (glog PROPERTIES VERSION ${GLOG_MAJOR_VERSION})
436 set_target_properties (glog PROPERTIES SOVERSION ${GLOG_VERSION})
439 target_compile_definitions (glog PUBLIC GLOG_NO_ABBREVIATED_SEVERITIES)
442 set_target_properties (glog PROPERTIES PUBLIC_HEADER "${GLOG_PUBLIC_H}")
444 set (_glog_CMake_BINDIR ${CMAKE_INSTALL_BINDIR})
445 set (_glog_CMake_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR})
446 set (_glog_CMake_LIBDIR ${CMAKE_INSTALL_LIBDIR})
447 set (_glog_CMake_INSTALLDIR ${_glog_CMake_LIBDIR}/cmake/glog)
449 target_include_directories (glog BEFORE PUBLIC
450 "$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>"
451 "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>"
452 "$<INSTALL_INTERFACE:${_glog_CMake_INCLUDE_DIR}>"
453 PRIVATE ${CMAKE_CURRENT_BINARY_DIR}
454 PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src)
457 target_include_directories (glog PUBLIC
458 "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/windows>"
459 PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src/windows)
462 set_target_properties (glog PROPERTIES DEFINE_SYMBOL LIBGLOG_EXPORTS)
464 if (NOT BUILD_SHARED_LIBS)
465 target_compile_definitions (glog PUBLIC GOOGLE_GLOG_DLL_DECL=)
466 else (NOT BUILD_SHARED_LIBS)
467 target_compile_definitions (glog PRIVATE GOOGLE_GLOG_IS_A_DLL=1)
469 if (HAVE___ATTRIBUTE__VISIBILITY_DEFAULT)
470 set (_EXPORT "__attribute__((visibility(\"default\")))")
472 elseif (HAVE___DECLSPEC)
473 set (_EXPORT "__declspec(dllexport)")
474 set (_IMPORT "__declspec(dllimport)")
475 endif (HAVE___ATTRIBUTE__VISIBILITY_DEFAULT)
477 target_compile_definitions (glog PRIVATE
478 "GOOGLE_GLOG_DLL_DECL=${_EXPORT}")
479 target_compile_definitions (glog INTERFACE
480 "GOOGLE_GLOG_DLL_DECL=${_IMPORT}")
481 target_compile_definitions (glog INTERFACE
482 "GOOGLE_GLOG_DLL_DECL_FOR_UNITTESTS=${_IMPORT}")
483 endif (NOT BUILD_SHARED_LIBS)
486 set (HAVE_STACKTRACE 1)
487 endif (HAVE_EXECINFO_H)
490 set (HAVE_STACKTRACE 1)
491 set (HAVE_SYMBOLIZE 1)
494 if (UNIX OR (APPLE AND HAVE_DLADDR))
495 set (HAVE_SYMBOLIZE 1)
496 endif (UNIX OR (APPLE AND HAVE_DLADDR))
501 add_executable (logging_unittest
502 src/logging_unittest.cc
505 target_link_libraries (logging_unittest PRIVATE glog)
507 add_executable (stl_logging_unittest
508 src/stl_logging_unittest.cc
511 target_link_libraries (stl_logging_unittest PRIVATE glog)
513 if (HAVE_NO_DEPRECATED)
514 set_property (TARGET stl_logging_unittest APPEND PROPERTY COMPILE_OPTIONS
516 endif (HAVE_NO_DEPRECATED)
518 if (HAVE_UNORDERED_MAP AND HAVE_UNORDERED_SET)
519 target_compile_definitions (stl_logging_unittest PRIVATE
520 GLOG_STL_LOGGING_FOR_UNORDERED)
521 endif (HAVE_UNORDERED_MAP AND HAVE_UNORDERED_SET)
523 if (HAVE_TR1_UNORDERED_MAP AND HAVE_TR1_UNORDERED_SET)
524 target_compile_definitions (stl_logging_unittest PRIVATE
525 GLOG_STL_LOGGING_FOR_TR1_UNORDERED)
526 endif (HAVE_TR1_UNORDERED_MAP AND HAVE_TR1_UNORDERED_SET)
528 if (HAVE_EXT_HASH_MAP AND HAVE_EXT_HASH_SET)
529 target_compile_definitions (stl_logging_unittest PRIVATE
530 GLOG_STL_LOGGING_FOR_EXT_HASH)
531 endif (HAVE_EXT_HASH_MAP AND HAVE_EXT_HASH_SET)
534 target_compile_definitions (stl_logging_unittest PRIVATE
535 GLOG_STL_LOGGING_FOR_EXT_SLIST)
536 endif (HAVE_EXT_SLIST)
539 add_executable (symbolize_unittest
540 src/symbolize_unittest.cc
543 target_link_libraries (symbolize_unittest PRIVATE glog)
544 endif (HAVE_SYMBOLIZE)
546 add_executable (demangle_unittest
547 src/demangle_unittest.cc
550 target_link_libraries (demangle_unittest PRIVATE glog)
553 add_executable (stacktrace_unittest
554 src/stacktrace_unittest.cc
557 target_link_libraries (stacktrace_unittest PRIVATE glog)
558 endif (HAVE_STACKTRACE)
560 add_executable (utilities_unittest
561 src/utilities_unittest.cc
564 target_link_libraries (utilities_unittest PRIVATE glog)
566 if (HAVE_STACKTRACE AND HAVE_SYMBOLIZE)
567 add_executable (signalhandler_unittest
568 src/signalhandler_unittest.cc
571 target_link_libraries (signalhandler_unittest PRIVATE glog)
572 endif (HAVE_STACKTRACE AND HAVE_SYMBOLIZE)
574 add_test (NAME demangle COMMAND demangle_unittest)
575 add_test (NAME logging COMMAND logging_unittest)
577 if (TARGET signalhandler_unittest)
578 add_test (NAME signalhandler COMMAND signalhandler_unittest)
579 endif (TARGET signalhandler_unittest)
581 if (TARGET stacktrace_unittest)
582 add_test (NAME stacktrace COMMAND stacktrace_unittest)
583 endif (TARGET stacktrace_unittest)
585 add_test (NAME stl_logging COMMAND stl_logging_unittest)
587 if (TARGET symbolize_unittest)
588 add_test (NAME symbolize COMMAND symbolize_unittest)
589 endif (TARGET symbolize_unittest)
590 endif (BUILD_TESTING)
592 install (TARGETS glog
594 RUNTIME DESTINATION ${_glog_CMake_BINDIR}
595 PUBLIC_HEADER DESTINATION ${_glog_CMake_INCLUDE_DIR}/glog
596 LIBRARY DESTINATION ${_glog_CMake_LIBDIR}
597 ARCHIVE DESTINATION ${_glog_CMake_LIBDIR})
600 set (gflags_DEPENDENCY "find_dependency (gflags ${gflags_VERSION})")
603 configure_package_config_file (glog-config.cmake.in
604 ${CMAKE_CURRENT_BINARY_DIR}/glog-config.cmake
605 INSTALL_DESTINATION ${_glog_CMake_INSTALLDIR}
606 NO_CHECK_REQUIRED_COMPONENTS_MACRO)
608 write_basic_package_version_file (glog-config-version.cmake VERSION
609 ${GLOG_VERSION} COMPATIBILITY SameMajorVersion)
611 export (TARGETS glog NAMESPACE glog:: FILE glog-targets.cmake)
612 export (PACKAGE glog)
615 ${CMAKE_CURRENT_BINARY_DIR}/glog-config.cmake
616 ${CMAKE_CURRENT_BINARY_DIR}/glog-config-version.cmake
617 DESTINATION ${_glog_CMake_INSTALLDIR})
619 install (EXPORT glog-targets NAMESPACE glog:: DESTINATION
620 ${_glog_CMake_INSTALLDIR})