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)
32 list (APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
34 include (CheckCSourceCompiles)
35 include (CheckCXXCompilerFlag)
36 include (CheckCXXSourceCompiles)
37 include (CheckFunctionExists)
38 include (CheckIncludeFile)
39 include (CheckIncludeFileCXX)
40 include (CheckLibraryExists)
41 include (CheckStructHasMember)
42 include (CheckSymbolExists)
43 include (CheckTypeSize)
44 include (CMakePackageConfigHelpers)
47 include (DetermineGflagsNamespace)
49 set (CMAKE_THREAD_PREFER_PTHREAD 1)
55 set (HAVE_LIB_GFLAGS 1)
56 determine_gflags_namespace (gflags_NAMESPACE)
61 find_package (Threads)
64 check_include_file (dlfcn.h HAVE_DLFCN_H)
65 check_include_file (execinfo.h HAVE_EXECINFO_H)
66 check_include_file (glob.h HAVE_GLOB_H)
67 check_include_file (inttypes.h HAVE_INTTYPES_H)
68 check_include_file (libunwind.h HAVE_LIBUNWIND_H)
69 check_include_file (memory.h HAVE_MEMORY_H)
70 check_include_file (pwd.h HAVE_PWD_H)
71 check_include_file (stdint.h HAVE_STDINT_H)
72 check_include_file (stdlib.h HAVE_STDLIB_H)
73 check_include_file (string.h HAVE_STRING_H)
74 check_include_file (strings.h HAVE_STRINGS_H)
75 check_include_file (sys/stat.h HAVE_SYS_STAT_H)
76 check_include_file (sys/syscall.h HAVE_SYS_SYSCALL_H)
77 check_include_file (sys/time.h HAVE_SYS_TIME_H)
78 check_include_file (sys/types.h HAVE_SYS_TYPES_H)
79 check_include_file (sys/utsname.h HAVE_SYS_UTSNAME_H)
80 check_include_file (syscall.h HAVE_SYSCALL_H)
81 check_include_file (syslog.h HAVE_SYSLOG_H)
82 check_include_file (ucontext.h HAVE_UCONTEXT_H)
83 check_include_file (unistd.h HAVE_UNISTD_H)
84 check_include_file (unwind.h HAVE_UNWIND_H)
86 check_include_file_cxx ("ext/hash_map" HAVE_EXT_HASH_MAP)
87 check_include_file_cxx ("ext/hash_set" HAVE_EXT_HASH_SET)
88 check_include_file_cxx ("ext/slist" HAVE_EXT_SLIST)
89 check_include_file_cxx ("tr1/unordered_map" HAVE_TR1_UNORDERED_MAP)
90 check_include_file_cxx ("tr1/unordered_set" HAVE_TR1_UNORDERED_SET)
91 check_include_file_cxx ("unordered_map" HAVE_UNORDERED_MAP)
92 check_include_file_cxx ("unordered_set" HAVE_UNORDERED_SET)
94 check_type_size ("unsigned __int16" HAVE___UINT16)
95 check_type_size (u_int16_t HAVE_U_INT16_T)
96 check_type_size (uint16_t HAVE_UINT16_T)
98 check_function_exists (dladdr HAVE_DLADDR)
99 check_function_exists (fcntl HAVE_FCNTL)
100 check_function_exists (pread HAVE_PREAD)
101 check_function_exists (pwrite HAVE_PWRITE)
102 check_function_exists (sigaction HAVE_SIGACTION)
103 check_function_exists (sigaltstack HAVE_SIGALSTACK)
105 # NOTE gcc does not fail if you pass a non-existent -Wno-* option as an
106 # argument. However, it will happily fail if you pass the corresponding -W*
107 # option. So, we check whether options that disable warnings exist by testing
108 # the availability of the corresponding option that enables the warning. This
109 # eliminates the need to check for compiler for several (mainly Clang) options.
111 check_cxx_compiler_flag (-Wdeprecated HAVE_NO_DEPRECATED)
112 check_cxx_compiler_flag (-Wunnamed-type-template-args
113 HAVE_NO_UNNAMED_TYPE_TEMPLATE_ARGS)
115 # NOTE: Cannot use check_function_exists here since >=vc-14.0 can define
116 # snprintf as an inline function
117 check_symbol_exists (snprintf stdio.h HAVE_SNPRINTF)
119 check_library_exists (unwind get_static_proc_name "" HAVE_LIB_UNWIND)
121 find_library (UNWIND_LIBRARY NAMES unwind DOC "unwind library")
122 mark_as_advanced (UNWIND_LIBRARY)
124 check_c_source_compiles ("
126 static void foo(void) __attribute__ ((unused));
127 int main(void) { return 0; }
128 " HAVE___ATTRIBUTE__)
130 check_c_source_compiles ("
132 static void foo(void) __attribute__ ((visibility(\"default\")));
133 int main(void) { return 0; }
134 " HAVE___ATTRIBUTE__VISIBILITY_DEFAULT)
136 check_c_source_compiles ("
138 static void foo(void) __attribute__ ((visibility(\"hidden\")));
139 int main(void) { return 0; }
140 " HAVE___ATTRIBUTE__VISIBILITY_HIDDEN)
142 check_c_source_compiles ("
143 int main(void) { if (__builtin_expect(0, 0)) return 1; return 0; }
144 " HAVE___BUILTIN_EXPECT)
146 check_c_source_compiles ("
149 int a; if (__sync_val_compare_and_swap(&a, 0, 1)) return 1; return 0;
151 " HAVE___SYNC_VAL_COMPARE_AND_SWAP)
153 check_c_source_compiles ("
154 #define _XOPEN_SOURCE 500
159 pthread_rwlock_init(&l, NULL);
160 pthread_rwlock_rdlock(&l);
165 check_c_source_compiles ("
166 __declspec(selectany) int a;
167 int main(void) { return 0; }
170 check_cxx_source_compiles ("
172 vector<int> t; int main() { }
175 check_cxx_source_compiles ("
177 std::vector<int> t; int main() { }
180 check_cxx_source_compiles ("
182 std::ostream& operator<<(std::ostream&, struct s);
185 " HAVE_USING_OPERATOR)
187 check_cxx_source_compiles ("
188 namespace Outer { namespace Inner { int i = 0; }}
189 using namespace Outer::Inner;;
190 int main() { return i; }
198 "uc_regs->gregs[PT_NIP]"
213 set (_PC_HEADERS ucontext.h signal.h)
215 if (HAVE_UCONTEXT_H AND NOT PC_FROM_UCONTEXT)
216 foreach (_PC_FIELD ${_PC_FIELDS})
217 foreach (_PC_HEADER ${_PC_HEADERS})
219 ${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/uctfield.c)
220 file (WRITE ${_TMP} "
221 #define _GNU_SOURCE 1
222 #include <${_PC_HEADER}>
226 return u.${_PC_FIELD} == 0;
229 try_compile (HAVE_PC_FROM_UCONTEXT ${CMAKE_CURRENT_BINARY_DIR} ${_TMP}
230 COMPILE_DEFINITIONS _GNU_SOURCE=1)
232 if (HAVE_PC_FROM_UCONTEXT)
233 set (PC_FROM_UCONTEXT ${_PC_FIELD} CACHE)
234 endif (HAVE_PC_FROM_UCONTEXT)
235 endforeach (_PC_HEADER)
236 endforeach (_PC_FIELD)
237 endif (HAVE_UCONTEXT_H AND NOT PC_FROM_UCONTEXT)
239 if (STL_STD_NAMESPACE)
240 set (STL_NAMESPACE std)
241 else (STL_STD_NAMESPACE)
242 set (STL_NAMESPACE "")
243 endif (STL_STD_NAMESPACE)
245 set (GOOGLE_NAMESPACE google)
246 set (_START_GOOGLE_NAMESPACE_ "namespace ${GOOGLE_NAMESPACE} {")
247 set (_END_GOOGLE_NAMESPACE_ "}")
250 set (ac_cv_have___uint16 1)
252 set (ac_cv_have___uint16 0)
253 endif (HAVE___UINT16)
256 set (ac_cv_have_inttypes_h 1)
257 else (HAVE_INTTYPES_H)
258 set (ac_cv_have_inttypes_h 0)
259 endif (HAVE_INTTYPES_H)
262 set (ac_cv_have_libgflags 1)
263 else (HAVE_LIB_GFLAGS)
264 set (ac_cv_have_libgflags 0)
265 endif (HAVE_LIB_GFLAGS)
268 set (ac_cv_have_stdint_h 1)
270 set (ac_cv_have_stdint_h 0)
271 endif (HAVE_STDINT_H)
273 if (HAVE_SYS_TYPES_H)
274 set (ac_cv_have_systypes_h 1)
275 else (HAVE_SYS_TYPES_H)
276 set (ac_cv_have_systypes_h 0)
277 endif (HAVE_SYS_TYPES_H)
280 set (ac_cv_have_u_int16_t 1)
281 else (HAVE_U_INT16_T)
282 set (ac_cv_have_u_int16_t 0)
283 endif (HAVE_U_INT16_T)
286 set (ac_cv_have_uint16_t 1)
288 set (ac_cv_have_uint16_t 0)
289 endif (HAVE_UINT16_T)
292 set (ac_cv_have_unistd_h 1)
294 set (ac_cv_have_unistd_h 0)
295 endif (HAVE_UNISTD_H)
297 set (ac_google_namespace ${GOOGLE_NAMESPACE})
298 set (ac_google_end_namespace ${_END_GOOGLE_NAMESPACE_})
299 set (ac_google_start_namespace ${_START_GOOGLE_NAMESPACE_})
301 if (HAVE___ATTRIBUTE__)
302 set (ac_cv___attribute___noreturn "__attribute__((noreturn))")
303 set (ac_cv___attribute___noinline "__attribute__((noinline))")
304 elseif (HAVE___DECLSPEC)
305 set (ac_cv___attribute___noreturn "__declspec(noreturn)")
306 #set (ac_cv___attribute___noinline "__declspec(noinline)")
307 endif (HAVE___ATTRIBUTE__)
309 if (HAVE___BUILTIN_EXPECT)
310 set (ac_cv_have___builtin_expect 1)
311 else (HAVE___BUILTIN_EXPECT)
312 set (ac_cv_have___builtin_expect 0)
313 endif (HAVE___BUILTIN_EXPECT)
315 if (HAVE_USING_OPERATOR)
316 set (ac_cv_cxx_using_operator 1)
317 else (HAVE_USING_OPERATOR)
318 set (ac_cv_cxx_using_operator 0)
319 endif (HAVE_USING_OPERATOR)
321 set (SIZEOF_VOID_P ${CMAKE_SIZEOF_VOID_P})
323 if (WITH_THREADS AND Threads_FOUND)
324 if (CMAKE_USE_PTHREADS_INIT)
326 endif (CMAKE_USE_PTHREADS_INIT)
327 else (WITH_THREADS AND Threads_FOUND)
329 endif (WITH_THREADS AND Threads_FOUND)
331 set (TEST_SRC_DIR \"${CMAKE_CURRENT_SOURCE_DIR}\")
333 configure_file (src/config.h.cmake.in config.h)
334 configure_file (src/glog/logging.h.in glog/logging.h @ONLY)
335 configure_file (src/glog/raw_logging.h.in glog/raw_logging.h @ONLY)
336 configure_file (src/glog/stl_logging.h.in glog/stl_logging.h @ONLY)
337 configure_file (src/glog/vlog_is_on.h.in glog/vlog_is_on.h @ONLY)
339 set (CMAKE_CXX_VISIBILITY_PRESET default)
340 set (CMAKE_VISIBILITY_INLINES_HIDDEN 1)
343 ${CMAKE_CURRENT_BINARY_DIR}/config.h
344 ${CMAKE_CURRENT_BINARY_DIR}/glog/logging.h
345 ${CMAKE_CURRENT_BINARY_DIR}/glog/raw_logging.h
346 ${CMAKE_CURRENT_BINARY_DIR}/glog/stl_logging.h
347 ${CMAKE_CURRENT_BINARY_DIR}/glog/vlog_is_on.h
348 src/glog/log_severity.h
353 src/base/commandlineflags.h
354 src/base/googleinit.h
368 list (APPEND GLOG_SRCS src/signalhandler.cc)
372 list (APPEND GLOG_SRCS
378 add_compile_options ($<$<BOOL:${HAVE_NO_UNNAMED_TYPE_TEMPLATE_ARGS}>:-Wno-unnamed-type-template-args>)
384 set_target_properties (glog PROPERTIES POSITION_INDEPENDENT_CODE ON)
387 target_link_libraries (glog PUBLIC ${UNWIND_LIBRARY})
388 endif (UNWIND_LIBRARY)
391 target_link_libraries (glog PUBLIC ${CMAKE_THREAD_LIBS_INIT})
394 if (WIN32 AND HAVE_SNPRINTF)
395 set_property (SOURCE src/windows/port.cc APPEND PROPERTY COMPILE_DEFINITIONS
397 endif (WIN32 AND HAVE_SNPRINTF)
400 target_include_directories (glog PUBLIC $<BUILD_INTERFACE:${gflags_INCLUDE_DIR}>)
401 target_link_libraries (glog PUBLIC ${gflags_LIBRARIES})
403 if (NOT BUILD_SHARED_LIBS)
404 # Don't use __declspec(dllexport|dllimport) if this is a static build
405 targeT_compile_definitions (glog PUBLIC GFLAGS_DLL_DECLARE_FLAG= GFLAGS_DLL_DEFINE_FLAG=)
406 endif (NOT BUILD_SHARED_LIBS)
409 set_target_properties (glog PROPERTIES VERSION ${GLOG_MAJOR_VERSION})
410 set_target_properties (glog PROPERTIES SOVERSION ${GLOG_VERSION})
413 target_compile_definitions (glog PUBLIC GLOG_NO_ABBREVIATED_SEVERITIES)
416 set_target_properties (glog PROPERTIES PUBLIC_HEADER "${GLOG_PUBLIC_H}")
418 target_include_directories (glog BEFORE PUBLIC
419 "$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>"
420 "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>"
421 "$<INSTALL_INTERFACE:include>"
422 PRIVATE ${CMAKE_CURRENT_BINARY_DIR}
423 PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src)
426 target_include_directories (glog PUBLIC
427 "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/windows>"
428 PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src/windows)
431 set_target_properties (glog PROPERTIES DEFINE_SYMBOL LIBGLOG_EXPORTS)
433 if (NOT BUILD_SHARED_LIBS)
434 target_compile_definitions (glog PUBLIC GOOGLE_GLOG_DLL_DECL=)
435 else (NOT BUILD_SHARED_LIBS)
436 target_compile_definitions (glog PRIVATE GOOGLE_GLOG_IS_A_DLL=1)
438 if (HAVE___ATTRIBUTE__VISIBILITY_DEFAULT)
439 set (_EXPORT "__attribute__((visibility(\"default\")))")
441 elseif (HAVE___DECLSPEC)
442 set (_EXPORT "__declspec(dllexport)")
443 set (_IMPORT "__declspec(dllimport)")
444 endif (HAVE___ATTRIBUTE__VISIBILITY_DEFAULT)
446 target_compile_definitions (glog PRIVATE
447 "GOOGLE_GLOG_DLL_DECL=${_EXPORT}")
448 target_compile_definitions (glog INTERFACE
449 "GOOGLE_GLOG_DLL_DECL=${_IMPORT}")
450 target_compile_definitions (glog INTERFACE
451 "GOOGLE_GLOG_DLL_DECL_FOR_UNITTESTS=${_IMPORT}")
452 endif (NOT BUILD_SHARED_LIBS)
455 set (HAVE_STACKTRACE 1)
456 endif (HAVE_EXECINFO_H)
459 set (HAVE_STACKTRACE 1)
460 set (HAVE_SYMBOLIZE 1)
463 if (UNIX OR (APPLE AND HAVE_DLADDR))
464 set (HAVE_SYMBOLIZE 1)
465 endif (UNIX OR (APPLE AND HAVE_DLADDR))
470 add_executable (logging_unittest
471 src/logging_unittest.cc
474 target_link_libraries (logging_unittest PRIVATE glog)
476 add_executable (stl_logging_unittest
477 src/stl_logging_unittest.cc
480 target_link_libraries (stl_logging_unittest PRIVATE glog)
482 if (HAVE_NO_DEPRECATED)
483 set_property (TARGET stl_logging_unittest APPEND PROPERTY COMPILE_OPTIONS
485 endif (HAVE_NO_DEPRECATED)
487 if (HAVE_UNORDERED_MAP AND HAVE_UNORDERED_SET)
488 target_compile_definitions (stl_logging_unittest PRIVATE
489 GLOG_STL_LOGGING_FOR_UNORDERED)
490 endif (HAVE_UNORDERED_MAP AND HAVE_UNORDERED_SET)
492 if (HAVE_TR1_UNORDERED_MAP AND HAVE_TR1_UNORDERED_SET)
493 target_compile_definitions (stl_logging_unittest PRIVATE
494 GLOG_STL_LOGGING_FOR_TR1_UNORDERED)
495 endif (HAVE_TR1_UNORDERED_MAP AND HAVE_TR1_UNORDERED_SET)
497 if (HAVE_EXT_HASH_MAP AND HAVE_EXT_HASH_SET)
498 target_compile_definitions (stl_logging_unittest PRIVATE
499 GLOG_STL_LOGGING_FOR_EXT_HASH)
500 endif (HAVE_EXT_HASH_MAP AND HAVE_EXT_HASH_SET)
503 target_compile_definitions (stl_logging_unittest PRIVATE
504 GLOG_STL_LOGGING_FOR_EXT_SLIST)
505 endif (HAVE_EXT_SLIST)
508 add_executable (symbolize_unittest
509 src/symbolize_unittest.cc
512 target_link_libraries (symbolize_unittest PRIVATE glog)
513 endif (HAVE_SYMBOLIZE)
515 add_executable (demangle_unittest
516 src/demangle_unittest.cc
519 target_link_libraries (demangle_unittest PRIVATE glog)
522 add_executable (stacktrace_unittest
523 src/stacktrace_unittest.cc
526 target_link_libraries (stacktrace_unittest PRIVATE glog)
527 endif (HAVE_STACKTRACE)
529 add_executable (utilities_unittest
530 src/utilities_unittest.cc
533 target_link_libraries (utilities_unittest PRIVATE glog)
535 if (HAVE_STACKTRACE AND HAVE_SYMBOLIZE)
536 add_executable (signalhandler_unittest
537 src/signalhandler_unittest.cc
540 target_link_libraries (signalhandler_unittest PRIVATE glog)
541 endif (HAVE_STACKTRACE AND HAVE_SYMBOLIZE)
543 add_test (NAME demangle COMMAND demangle_unittest)
544 add_test (NAME logging COMMAND logging_unittest)
546 if (TARGET signalhandler_unittest)
547 add_test (NAME signalhandler COMMAND signalhandler_unittest)
548 endif (TARGET signalhandler_unittest)
550 if (TARGET stacktrace_unittest)
551 add_test (NAME stacktrace COMMAND stacktrace_unittest)
552 endif (TARGET stacktrace_unittest)
554 add_test (NAME stl_logging COMMAND stl_logging_unittest)
556 if (TARGET symbolize_unittest)
557 add_test (NAME symbolize COMMAND symbolize_unittest)
558 endif (TARGET symbolize_unittest)
559 endif (BUILD_TESTING)
561 install (TARGETS glog
563 RUNTIME DESTINATION bin
564 PUBLIC_HEADER DESTINATION include/glog
565 LIBRARY DESTINATION lib
566 ARCHIVE DESTINATION lib)
569 set (gflags_DEPENDENCY "find_dependency (gflags ${gflags_VERSION})")
572 configure_package_config_file (glog-config.cmake.in
573 ${CMAKE_CURRENT_BINARY_DIR}/glog-config.cmake
574 INSTALL_DESTINATION lib/cmake/glog
575 NO_CHECK_REQUIRED_COMPONENTS_MACRO)
577 write_basic_package_version_file (glog-config-version.cmake VERSION
578 ${GLOG_VERSION} COMPATIBILITY SameMajorVersion)
580 export (TARGETS glog NAMESPACE glog:: FILE glog-targets.cmake)
581 export (PACKAGE glog)
584 ${CMAKE_CURRENT_BINARY_DIR}/glog-config.cmake
585 ${CMAKE_CURRENT_BINARY_DIR}/glog-config-version.cmake
586 DESTINATION lib/cmake/glog)
588 install (EXPORT glog-targets NAMESPACE glog:: DESTINATION lib/cmake/glog)