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_DEBUG_POSTFIX d)
52 set (CMAKE_THREAD_PREFER_PTHREAD 1)
55 find_package (gflags 2.2.0)
58 set (HAVE_LIB_GFLAGS 1)
59 determine_gflags_namespace (gflags_NAMESPACE)
64 find_package (Threads)
67 check_include_file (dlfcn.h HAVE_DLFCN_H)
68 check_include_file (execinfo.h HAVE_EXECINFO_H)
69 check_include_file (glob.h HAVE_GLOB_H)
70 check_include_file (inttypes.h HAVE_INTTYPES_H)
71 check_include_file (libunwind.h HAVE_LIBUNWIND_H)
72 check_include_file (memory.h HAVE_MEMORY_H)
73 check_include_file (pwd.h HAVE_PWD_H)
74 check_include_file (stdint.h HAVE_STDINT_H)
75 check_include_file (stdlib.h HAVE_STDLIB_H)
76 check_include_file (string.h HAVE_STRING_H)
77 check_include_file (strings.h HAVE_STRINGS_H)
78 check_include_file (sys/stat.h HAVE_SYS_STAT_H)
79 check_include_file (sys/syscall.h HAVE_SYS_SYSCALL_H)
80 check_include_file (sys/time.h HAVE_SYS_TIME_H)
81 check_include_file (sys/types.h HAVE_SYS_TYPES_H)
82 check_include_file (sys/utsname.h HAVE_SYS_UTSNAME_H)
83 check_include_file (syscall.h HAVE_SYSCALL_H)
84 check_include_file (syslog.h HAVE_SYSLOG_H)
85 check_include_file (ucontext.h HAVE_UCONTEXT_H)
86 check_include_file (unistd.h HAVE_UNISTD_H)
87 check_include_file (unwind.h HAVE_UNWIND_H)
88 check_include_file (pwd.h HAVE_PWD_H)
90 check_include_file_cxx ("ext/hash_map" HAVE_EXT_HASH_MAP)
91 check_include_file_cxx ("ext/hash_set" HAVE_EXT_HASH_SET)
92 check_include_file_cxx ("ext/slist" HAVE_EXT_SLIST)
93 check_include_file_cxx ("tr1/unordered_map" HAVE_TR1_UNORDERED_MAP)
94 check_include_file_cxx ("tr1/unordered_set" HAVE_TR1_UNORDERED_SET)
95 check_include_file_cxx ("unordered_map" HAVE_UNORDERED_MAP)
96 check_include_file_cxx ("unordered_set" HAVE_UNORDERED_SET)
98 check_type_size ("unsigned __int16" HAVE___UINT16)
99 check_type_size (u_int16_t HAVE_U_INT16_T)
100 check_type_size (uint16_t HAVE_UINT16_T)
102 check_function_exists (dladdr HAVE_DLADDR)
103 check_function_exists (fcntl HAVE_FCNTL)
104 check_function_exists (pread HAVE_PREAD)
105 check_function_exists (pwrite HAVE_PWRITE)
106 check_function_exists (sigaction HAVE_SIGACTION)
107 check_function_exists (sigaltstack HAVE_SIGALSTACK)
109 # NOTE gcc does not fail if you pass a non-existent -Wno-* option as an
110 # argument. However, it will happily fail if you pass the corresponding -W*
111 # option. So, we check whether options that disable warnings exist by testing
112 # the availability of the corresponding option that enables the warning. This
113 # eliminates the need to check for compiler for several (mainly Clang) options.
115 check_cxx_compiler_flag (-Wdeprecated HAVE_NO_DEPRECATED)
116 check_cxx_compiler_flag (-Wunnamed-type-template-args
117 HAVE_NO_UNNAMED_TYPE_TEMPLATE_ARGS)
119 # NOTE: Cannot use check_function_exists here since >=vc-14.0 can define
120 # snprintf as an inline function
121 check_symbol_exists (snprintf stdio.h HAVE_SNPRINTF)
123 check_library_exists (unwind get_static_proc_name "" HAVE_LIB_UNWIND)
125 find_library (UNWIND_LIBRARY NAMES unwind DOC "unwind library")
126 mark_as_advanced (UNWIND_LIBRARY)
128 check_c_source_compiles ("
130 static void foo(void) __attribute__ ((unused));
131 int main(void) { return 0; }
132 " HAVE___ATTRIBUTE__)
134 check_c_source_compiles ("
136 static void foo(void) __attribute__ ((visibility(\"default\")));
137 int main(void) { return 0; }
138 " HAVE___ATTRIBUTE__VISIBILITY_DEFAULT)
140 check_c_source_compiles ("
142 static void foo(void) __attribute__ ((visibility(\"hidden\")));
143 int main(void) { return 0; }
144 " HAVE___ATTRIBUTE__VISIBILITY_HIDDEN)
146 check_c_source_compiles ("
147 int main(void) { if (__builtin_expect(0, 0)) return 1; return 0; }
148 " HAVE___BUILTIN_EXPECT)
150 check_c_source_compiles ("
153 int a; if (__sync_val_compare_and_swap(&a, 0, 1)) return 1; return 0;
155 " HAVE___SYNC_VAL_COMPARE_AND_SWAP)
157 check_c_source_compiles ("
158 #define _XOPEN_SOURCE 500
163 pthread_rwlock_init(&l, NULL);
164 pthread_rwlock_rdlock(&l);
169 check_c_source_compiles ("
170 __declspec(selectany) int a;
171 int main(void) { return 0; }
174 check_cxx_source_compiles ("
176 vector<int> t; int main() { }
179 check_cxx_source_compiles ("
181 std::vector<int> t; int main() { }
184 check_cxx_source_compiles ("
186 std::ostream& operator<<(std::ostream&, struct s);
189 " HAVE_USING_OPERATOR)
191 check_cxx_source_compiles ("
192 namespace Outer { namespace Inner { int i = 0; }}
193 using namespace Outer::Inner;;
194 int main() { return i; }
197 check_cxx_source_compiles ("
202 check_cxx_source_compiles ("
203 __declspec(thread) int tls;
207 check_cxx_source_compiles ("
208 thread_local int tls;
213 # Cygwin does not support the thread attribute. Don't bother.
215 set (GLOG_THREAD_LOCAL_STORAGE "__thread")
216 elseif (HAVE_MSVC_TLS)
217 set (GLOG_THREAD_LOCAL_STORAGE "__declspec(thread)")
218 elseif (HAVE_CXX11_TLS)
219 set (GLOG_THREAD_LOCAL_STORAGE thread_local)
228 "uc_regs->gregs[PT_NIP]"
243 set (_PC_HEADERS ucontext.h signal.h)
245 if (HAVE_UCONTEXT_H AND NOT PC_FROM_UCONTEXT)
246 foreach (_PC_FIELD ${_PC_FIELDS})
247 foreach (_PC_HEADER ${_PC_HEADERS})
249 ${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/uctfield.c)
250 file (WRITE ${_TMP} "
251 #define _GNU_SOURCE 1
252 #include <${_PC_HEADER}>
256 return u.${_PC_FIELD} == 0;
259 try_compile (HAVE_PC_FROM_UCONTEXT ${CMAKE_CURRENT_BINARY_DIR} ${_TMP}
260 COMPILE_DEFINITIONS _GNU_SOURCE=1)
262 if (HAVE_PC_FROM_UCONTEXT)
263 set (PC_FROM_UCONTEXT ${_PC_FIELD} CACHE)
264 endif (HAVE_PC_FROM_UCONTEXT)
265 endforeach (_PC_HEADER)
266 endforeach (_PC_FIELD)
267 endif (HAVE_UCONTEXT_H AND NOT PC_FROM_UCONTEXT)
269 if (STL_STD_NAMESPACE)
270 set (STL_NAMESPACE std)
271 else (STL_STD_NAMESPACE)
272 set (STL_NAMESPACE "")
273 endif (STL_STD_NAMESPACE)
275 set (GOOGLE_NAMESPACE google)
276 set (_START_GOOGLE_NAMESPACE_ "namespace ${GOOGLE_NAMESPACE} {")
277 set (_END_GOOGLE_NAMESPACE_ "}")
280 set (ac_cv_have___uint16 1)
282 set (ac_cv_have___uint16 0)
283 endif (HAVE___UINT16)
286 set (ac_cv_have_inttypes_h 1)
287 else (HAVE_INTTYPES_H)
288 set (ac_cv_have_inttypes_h 0)
289 endif (HAVE_INTTYPES_H)
292 set (ac_cv_have_libgflags 1)
293 else (HAVE_LIB_GFLAGS)
294 set (ac_cv_have_libgflags 0)
295 endif (HAVE_LIB_GFLAGS)
298 set (ac_cv_have_stdint_h 1)
300 set (ac_cv_have_stdint_h 0)
301 endif (HAVE_STDINT_H)
303 if (HAVE_SYS_TYPES_H)
304 set (ac_cv_have_systypes_h 1)
305 else (HAVE_SYS_TYPES_H)
306 set (ac_cv_have_systypes_h 0)
307 endif (HAVE_SYS_TYPES_H)
310 set (ac_cv_have_u_int16_t 1)
311 else (HAVE_U_INT16_T)
312 set (ac_cv_have_u_int16_t 0)
313 endif (HAVE_U_INT16_T)
316 set (ac_cv_have_uint16_t 1)
318 set (ac_cv_have_uint16_t 0)
319 endif (HAVE_UINT16_T)
322 set (ac_cv_have_unistd_h 1)
324 set (ac_cv_have_unistd_h 0)
325 endif (HAVE_UNISTD_H)
327 set (ac_google_namespace ${GOOGLE_NAMESPACE})
328 set (ac_google_end_namespace ${_END_GOOGLE_NAMESPACE_})
329 set (ac_google_start_namespace ${_START_GOOGLE_NAMESPACE_})
331 if (HAVE___ATTRIBUTE__)
332 set (ac_cv___attribute___noreturn "__attribute__((noreturn))")
333 set (ac_cv___attribute___noinline "__attribute__((noinline))")
334 elseif (HAVE___DECLSPEC)
335 set (ac_cv___attribute___noreturn "__declspec(noreturn)")
336 #set (ac_cv___attribute___noinline "__declspec(noinline)")
337 endif (HAVE___ATTRIBUTE__)
339 if (HAVE___BUILTIN_EXPECT)
340 set (ac_cv_have___builtin_expect 1)
341 else (HAVE___BUILTIN_EXPECT)
342 set (ac_cv_have___builtin_expect 0)
343 endif (HAVE___BUILTIN_EXPECT)
345 if (HAVE_USING_OPERATOR)
346 set (ac_cv_cxx_using_operator 1)
347 else (HAVE_USING_OPERATOR)
348 set (ac_cv_cxx_using_operator 0)
349 endif (HAVE_USING_OPERATOR)
351 set (SIZEOF_VOID_P ${CMAKE_SIZEOF_VOID_P})
353 if (WITH_THREADS AND Threads_FOUND)
354 if (CMAKE_USE_PTHREADS_INIT)
356 endif (CMAKE_USE_PTHREADS_INIT)
357 else (WITH_THREADS AND Threads_FOUND)
359 endif (WITH_THREADS AND Threads_FOUND)
361 set (TEST_SRC_DIR \"${CMAKE_CURRENT_SOURCE_DIR}\")
363 configure_file (src/config.h.cmake.in config.h)
364 configure_file (src/glog/logging.h.in glog/logging.h @ONLY)
365 configure_file (src/glog/raw_logging.h.in glog/raw_logging.h @ONLY)
366 configure_file (src/glog/stl_logging.h.in glog/stl_logging.h @ONLY)
367 configure_file (src/glog/vlog_is_on.h.in glog/vlog_is_on.h @ONLY)
369 set (CMAKE_CXX_VISIBILITY_PRESET default)
370 set (CMAKE_VISIBILITY_INLINES_HIDDEN 1)
373 ${CMAKE_CURRENT_BINARY_DIR}/config.h
374 ${CMAKE_CURRENT_BINARY_DIR}/glog/logging.h
375 ${CMAKE_CURRENT_BINARY_DIR}/glog/raw_logging.h
376 ${CMAKE_CURRENT_BINARY_DIR}/glog/stl_logging.h
377 ${CMAKE_CURRENT_BINARY_DIR}/glog/vlog_is_on.h
378 src/glog/log_severity.h
383 src/base/commandlineflags.h
384 src/base/googleinit.h
397 if (HAVE_PTHREAD OR WIN32)
398 list (APPEND GLOG_SRCS src/signalhandler.cc)
399 endif (HAVE_PTHREAD OR WIN32)
402 list (APPEND GLOG_SRCS
408 add_compile_options ($<$<BOOL:${HAVE_NO_UNNAMED_TYPE_TEMPLATE_ARGS}>:-Wno-unnamed-type-template-args>)
414 set_target_properties (glog PROPERTIES POSITION_INDEPENDENT_CODE ON)
417 target_link_libraries (glog PUBLIC ${UNWIND_LIBRARY})
418 endif (UNWIND_LIBRARY)
421 target_link_libraries (glog PUBLIC ${CMAKE_THREAD_LIBS_INIT})
424 if (WIN32 AND HAVE_SNPRINTF)
425 set_property (SOURCE src/windows/port.cc APPEND PROPERTY COMPILE_DEFINITIONS
427 endif (WIN32 AND HAVE_SNPRINTF)
430 target_link_libraries (glog PUBLIC gflags)
432 if (NOT BUILD_SHARED_LIBS)
433 # Don't use __declspec(dllexport|dllimport) if this is a static build
434 targeT_compile_definitions (glog PUBLIC GFLAGS_DLL_DECLARE_FLAG= GFLAGS_DLL_DEFINE_FLAG=)
435 endif (NOT BUILD_SHARED_LIBS)
438 set_target_properties (glog PROPERTIES VERSION ${GLOG_MAJOR_VERSION})
439 set_target_properties (glog PROPERTIES SOVERSION ${GLOG_VERSION})
442 target_compile_definitions (glog PUBLIC GLOG_NO_ABBREVIATED_SEVERITIES)
445 set_target_properties (glog PROPERTIES PUBLIC_HEADER "${GLOG_PUBLIC_H}")
447 set (_glog_CMake_BINDIR ${CMAKE_INSTALL_BINDIR})
448 set (_glog_CMake_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR})
449 set (_glog_CMake_LIBDIR ${CMAKE_INSTALL_LIBDIR})
450 set (_glog_CMake_INSTALLDIR ${_glog_CMake_LIBDIR}/cmake/glog)
452 target_include_directories (glog BEFORE PUBLIC
453 "$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>"
454 "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>"
455 "$<INSTALL_INTERFACE:${_glog_CMake_INCLUDE_DIR}>"
456 PRIVATE ${CMAKE_CURRENT_BINARY_DIR}
457 PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src)
460 target_include_directories (glog PUBLIC
461 "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/windows>"
462 PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src/windows)
465 set_target_properties (glog PROPERTIES DEFINE_SYMBOL LIBGLOG_EXPORTS)
467 if (NOT BUILD_SHARED_LIBS)
468 target_compile_definitions (glog PUBLIC GOOGLE_GLOG_DLL_DECL=)
469 else (NOT BUILD_SHARED_LIBS)
470 target_compile_definitions (glog PRIVATE GOOGLE_GLOG_IS_A_DLL=1)
472 if (HAVE___ATTRIBUTE__VISIBILITY_DEFAULT)
473 set (_EXPORT "__attribute__((visibility(\"default\")))")
475 elseif (HAVE___DECLSPEC)
476 set (_EXPORT "__declspec(dllexport)")
477 set (_IMPORT "__declspec(dllimport)")
478 endif (HAVE___ATTRIBUTE__VISIBILITY_DEFAULT)
480 target_compile_definitions (glog PRIVATE
481 "GOOGLE_GLOG_DLL_DECL=${_EXPORT}")
482 target_compile_definitions (glog INTERFACE
483 "GOOGLE_GLOG_DLL_DECL=${_IMPORT}")
484 target_compile_definitions (glog INTERFACE
485 "GOOGLE_GLOG_DLL_DECL_FOR_UNITTESTS=${_IMPORT}")
486 endif (NOT BUILD_SHARED_LIBS)
489 set (HAVE_STACKTRACE 1)
490 endif (HAVE_EXECINFO_H)
493 set (HAVE_STACKTRACE 1)
494 set (HAVE_SYMBOLIZE 1)
497 if (UNIX OR (APPLE AND HAVE_DLADDR))
498 set (HAVE_SYMBOLIZE 1)
499 endif (UNIX OR (APPLE AND HAVE_DLADDR))
504 add_executable (logging_unittest
505 src/logging_unittest.cc
508 target_link_libraries (logging_unittest PRIVATE glog)
510 add_executable (stl_logging_unittest
511 src/stl_logging_unittest.cc
514 target_link_libraries (stl_logging_unittest PRIVATE glog)
516 if (HAVE_NO_DEPRECATED)
517 set_property (TARGET stl_logging_unittest APPEND PROPERTY COMPILE_OPTIONS
519 endif (HAVE_NO_DEPRECATED)
521 if (HAVE_UNORDERED_MAP AND HAVE_UNORDERED_SET)
522 target_compile_definitions (stl_logging_unittest PRIVATE
523 GLOG_STL_LOGGING_FOR_UNORDERED)
524 endif (HAVE_UNORDERED_MAP AND HAVE_UNORDERED_SET)
526 if (HAVE_TR1_UNORDERED_MAP AND HAVE_TR1_UNORDERED_SET)
527 target_compile_definitions (stl_logging_unittest PRIVATE
528 GLOG_STL_LOGGING_FOR_TR1_UNORDERED)
529 endif (HAVE_TR1_UNORDERED_MAP AND HAVE_TR1_UNORDERED_SET)
531 if (HAVE_EXT_HASH_MAP AND HAVE_EXT_HASH_SET)
532 target_compile_definitions (stl_logging_unittest PRIVATE
533 GLOG_STL_LOGGING_FOR_EXT_HASH)
534 endif (HAVE_EXT_HASH_MAP AND HAVE_EXT_HASH_SET)
537 target_compile_definitions (stl_logging_unittest PRIVATE
538 GLOG_STL_LOGGING_FOR_EXT_SLIST)
539 endif (HAVE_EXT_SLIST)
542 add_executable (symbolize_unittest
543 src/symbolize_unittest.cc
546 target_link_libraries (symbolize_unittest PRIVATE glog)
547 endif (HAVE_SYMBOLIZE)
549 add_executable (demangle_unittest
550 src/demangle_unittest.cc
553 target_link_libraries (demangle_unittest PRIVATE glog)
556 add_executable (stacktrace_unittest
557 src/stacktrace_unittest.cc
560 target_link_libraries (stacktrace_unittest PRIVATE glog)
561 endif (HAVE_STACKTRACE)
563 add_executable (utilities_unittest
564 src/utilities_unittest.cc
567 target_link_libraries (utilities_unittest PRIVATE glog)
569 if (HAVE_STACKTRACE AND HAVE_SYMBOLIZE)
570 add_executable (signalhandler_unittest
571 src/signalhandler_unittest.cc
574 target_link_libraries (signalhandler_unittest PRIVATE glog)
575 endif (HAVE_STACKTRACE AND HAVE_SYMBOLIZE)
577 add_test (NAME demangle COMMAND demangle_unittest)
578 add_test (NAME logging COMMAND logging_unittest)
580 if (TARGET signalhandler_unittest)
581 add_test (NAME signalhandler COMMAND signalhandler_unittest)
582 endif (TARGET signalhandler_unittest)
584 if (TARGET stacktrace_unittest)
585 add_test (NAME stacktrace COMMAND stacktrace_unittest)
586 endif (TARGET stacktrace_unittest)
588 add_test (NAME stl_logging COMMAND stl_logging_unittest)
590 if (TARGET symbolize_unittest)
591 add_test (NAME symbolize COMMAND symbolize_unittest)
592 endif (TARGET symbolize_unittest)
593 endif (BUILD_TESTING)
595 install (TARGETS glog
597 RUNTIME DESTINATION ${_glog_CMake_BINDIR}
598 PUBLIC_HEADER DESTINATION ${_glog_CMake_INCLUDE_DIR}/glog
599 LIBRARY DESTINATION ${_glog_CMake_LIBDIR}
600 ARCHIVE DESTINATION ${_glog_CMake_LIBDIR})
603 set (gflags_DEPENDENCY "find_dependency (gflags ${gflags_VERSION})")
606 configure_package_config_file (glog-config.cmake.in
607 ${CMAKE_CURRENT_BINARY_DIR}/glog-config.cmake
608 INSTALL_DESTINATION ${_glog_CMake_INSTALLDIR}
609 NO_CHECK_REQUIRED_COMPONENTS_MACRO)
611 write_basic_package_version_file (glog-config-version.cmake VERSION
612 ${GLOG_VERSION} COMPATIBILITY SameMajorVersion)
614 export (TARGETS glog NAMESPACE glog:: FILE glog-targets.cmake)
615 export (PACKAGE glog)
618 ${CMAKE_CURRENT_BINARY_DIR}/glog-config.cmake
619 ${CMAKE_CURRENT_BINARY_DIR}/glog-config-version.cmake
620 DESTINATION ${_glog_CMake_INSTALLDIR})
622 install (EXPORT glog-targets NAMESPACE glog:: DESTINATION
623 ${_glog_CMake_INSTALLDIR})