CMakeLists.txt 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  1. cmake_minimum_required (VERSION 3.16)
  2. project (glog
  3. VERSION 0.7.0
  4. DESCRIPTION "C++ implementation of the Google logging module"
  5. HOMEPAGE_URL https://github.com/google/glog
  6. LANGUAGES CXX
  7. )
  8. set (CPACK_PACKAGE_NAME glog)
  9. set (CPACK_PACKAGE_DESCRIPTION_SUMMARY "Google logging library")
  10. set (CPACK_PACKAGE_VERSION_MAJOR ${PROJECT_VERSION_MAJOR})
  11. set (CPACK_PACKAGE_VERSION_MINOR ${PROJECT_VERSION_MINOR})
  12. set (CPACK_PACKAGE_VERSION_PATCH ${PROJECT_VERSION_PATCH})
  13. set (CPACK_PACKAGE_VERSION ${PROJECT_VERSION})
  14. list (APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
  15. include (CheckCXXCompilerFlag)
  16. include (CheckCXXSourceCompiles)
  17. include (CheckCXXSourceRuns)
  18. include (CheckCXXSymbolExists)
  19. include (CheckFunctionExists)
  20. include (CheckIncludeFileCXX)
  21. include (CheckLibraryExists)
  22. include (CheckStructHasMember)
  23. include (CheckTypeSize)
  24. include (CMakeDependentOption)
  25. include (CMakePackageConfigHelpers)
  26. include (CMakePushCheckState)
  27. include (CPack)
  28. include (CTest)
  29. include (DetermineGflagsNamespace)
  30. include (GenerateExportHeader)
  31. include (GetCacheVariables)
  32. include (GNUInstallDirs)
  33. option (BUILD_SHARED_LIBS "Build shared libraries" ON)
  34. option (PRINT_UNSYMBOLIZED_STACK_TRACES
  35. "Print file offsets in traces instead of symbolizing" OFF)
  36. option (WITH_GFLAGS "Use gflags" ON)
  37. option (WITH_GTEST "Use Google Test" ON)
  38. option (WITH_PKGCONFIG "Enable pkg-config support" ON)
  39. option (WITH_SYMBOLIZE "Enable symbolize module" ON)
  40. option (WITH_THREADS "Enable multithreading support" ON)
  41. option (WITH_TLS "Enable Thread Local Storage (TLS) support" ON)
  42. option (WITH_UNWIND "Enable libunwind support" ON)
  43. cmake_dependent_option (WITH_GMOCK "Use Google Mock" ON WITH_GTEST OFF)
  44. set (WITH_FUZZING none CACHE STRING "Fuzzing engine")
  45. set_property (CACHE WITH_FUZZING PROPERTY STRINGS none libfuzzer ossfuzz)
  46. if (NOT WITH_UNWIND)
  47. set (CMAKE_DISABLE_FIND_PACKAGE_Unwind ON)
  48. endif (NOT WITH_UNWIND)
  49. if (NOT WITH_GTEST)
  50. set (CMAKE_DISABLE_FIND_PACKAGE_GTest ON)
  51. endif (NOT WITH_GTEST)
  52. if (NOT WITH_THREADS)
  53. set (CMAKE_DISABLE_FIND_PACKAGE_Threads ON)
  54. endif (NOT WITH_THREADS)
  55. set (CMAKE_C_VISIBILITY_PRESET hidden)
  56. set (CMAKE_CXX_VISIBILITY_PRESET hidden)
  57. set (CMAKE_POSITION_INDEPENDENT_CODE ON)
  58. set (CMAKE_VISIBILITY_INLINES_HIDDEN ON)
  59. set (CMAKE_DEBUG_POSTFIX d)
  60. set (CMAKE_THREAD_PREFER_PTHREAD 1)
  61. find_package (GTest NO_MODULE)
  62. if (GTest_FOUND)
  63. set (HAVE_LIB_GTEST 1)
  64. endif (GTest_FOUND)
  65. if (WITH_GMOCK AND TARGET GTest::gmock)
  66. set (HAVE_LIB_GMOCK 1)
  67. endif (WITH_GMOCK AND TARGET GTest::gmock)
  68. if (WITH_GFLAGS)
  69. find_package (gflags 2.2.2)
  70. if (gflags_FOUND)
  71. set (HAVE_LIB_GFLAGS 1)
  72. determine_gflags_namespace (gflags_NAMESPACE)
  73. endif (gflags_FOUND)
  74. endif (WITH_GFLAGS)
  75. find_package (Threads)
  76. find_package (Unwind)
  77. if (Unwind_FOUND)
  78. set (HAVE_LIB_UNWIND 1)
  79. else (Unwind_FOUND)
  80. # Check whether linking actually succeeds. ARM toolchains of LLVM unwind
  81. # implementation do not necessarily provide the _Unwind_Backtrace function
  82. # which causes the previous check to succeed but the linking to fail.
  83. check_cxx_symbol_exists (_Unwind_Backtrace unwind.h HAVE__UNWIND_BACKTRACE)
  84. check_cxx_symbol_exists (_Unwind_GetIP unwind.h HAVE__UNWIND_GETIP)
  85. endif (Unwind_FOUND)
  86. check_include_file_cxx (dlfcn.h HAVE_DLFCN_H)
  87. check_include_file_cxx (glob.h HAVE_GLOB_H)
  88. check_include_file_cxx (memory.h HAVE_MEMORY_H)
  89. check_include_file_cxx (pwd.h HAVE_PWD_H)
  90. check_include_file_cxx (strings.h HAVE_STRINGS_H)
  91. check_include_file_cxx (sys/stat.h HAVE_SYS_STAT_H)
  92. check_include_file_cxx (sys/syscall.h HAVE_SYS_SYSCALL_H)
  93. check_include_file_cxx (sys/time.h HAVE_SYS_TIME_H)
  94. check_include_file_cxx (sys/types.h HAVE_SYS_TYPES_H)
  95. check_include_file_cxx (sys/utsname.h HAVE_SYS_UTSNAME_H)
  96. check_include_file_cxx (sys/wait.h HAVE_SYS_WAIT_H)
  97. check_include_file_cxx (syscall.h HAVE_SYSCALL_H)
  98. check_include_file_cxx (syslog.h HAVE_SYSLOG_H)
  99. check_include_file_cxx (ucontext.h HAVE_UCONTEXT_H)
  100. check_include_file_cxx (unistd.h HAVE_UNISTD_H)
  101. check_type_size (mode_t HAVE_MODE_T LANGUAGE CXX)
  102. check_type_size (ssize_t HAVE_SSIZE_T LANGUAGE CXX)
  103. check_function_exists (dladdr HAVE_DLADDR)
  104. check_function_exists (fcntl HAVE_FCNTL)
  105. check_function_exists (pread HAVE_PREAD)
  106. check_function_exists (pwrite HAVE_PWRITE)
  107. check_function_exists (sigaction HAVE_SIGACTION)
  108. check_function_exists (sigaltstack HAVE_SIGALTSTACK)
  109. check_cxx_symbol_exists (backtrace execinfo.h HAVE_EXECINFO_BACKTRACE)
  110. check_cxx_symbol_exists (backtrace_symbols execinfo.h
  111. HAVE_EXECINFO_BACKTRACE_SYMBOLS)
  112. # NOTE gcc does not fail if you pass a non-existent -Wno-* option as an
  113. # argument. However, it will happily fail if you pass the corresponding -W*
  114. # option. So, we check whether options that disable warnings exist by testing
  115. # the availability of the corresponding option that enables the warning. This
  116. # eliminates the need to check for compiler for several (mainly Clang) options.
  117. check_cxx_compiler_flag (-Wdeprecated HAVE_NO_DEPRECATED)
  118. check_cxx_compiler_flag (-Wunnamed-type-template-args
  119. HAVE_NO_UNNAMED_TYPE_TEMPLATE_ARGS)
  120. cmake_push_check_state (RESET)
  121. if (Threads_FOUND)
  122. set (CMAKE_REQUIRED_LIBRARIES Threads::Threads)
  123. endif (Threads_FOUND)
  124. check_cxx_symbol_exists (pthread_threadid_np "pthread.h" HAVE_PTHREAD_THREADID_NP)
  125. cmake_pop_check_state ()
  126. # NOTE: Cannot use check_function_exists here since >=vc-14.0 can define
  127. # snprintf as an inline function
  128. check_cxx_symbol_exists (snprintf cstdio HAVE_SNPRINTF)
  129. check_library_exists (dbghelp UnDecorateSymbolName "" HAVE_DBGHELP)
  130. check_cxx_source_compiles ("
  131. #include <cstdlib>
  132. static void foo(void) __attribute__ ((unused));
  133. int main(void) { return 0; }
  134. " HAVE___ATTRIBUTE__)
  135. check_cxx_source_compiles ("
  136. #include <cstdlib>
  137. static void foo(void) __attribute__ ((visibility(\"default\")));
  138. int main(void) { return 0; }
  139. " HAVE___ATTRIBUTE__VISIBILITY_DEFAULT)
  140. check_cxx_source_compiles ("
  141. #include <cstdlib>
  142. static void foo(void) __attribute__ ((visibility(\"hidden\")));
  143. int main(void) { return 0; }
  144. " HAVE___ATTRIBUTE__VISIBILITY_HIDDEN)
  145. check_cxx_source_compiles ("
  146. int main(void) { if (__builtin_expect(0, 0)) return 1; return 0; }
  147. " HAVE___BUILTIN_EXPECT)
  148. check_cxx_source_compiles ("
  149. int main(void)
  150. {
  151. int a; if (__sync_val_compare_and_swap(&a, 0, 1)) return 1; return 0;
  152. }
  153. " HAVE___SYNC_VAL_COMPARE_AND_SWAP)
  154. if (Threads_FOUND)
  155. cmake_push_check_state (RESET)
  156. set (CMAKE_REQUIRED_LIBRARIES Threads::Threads)
  157. check_cxx_source_compiles ("
  158. #define _XOPEN_SOURCE 500
  159. #include <pthread.h>
  160. int main(void)
  161. {
  162. pthread_rwlock_t l;
  163. pthread_rwlock_init(&l, NULL);
  164. pthread_rwlock_rdlock(&l);
  165. return 0;
  166. }
  167. " HAVE_RWLOCK)
  168. cmake_pop_check_state ()
  169. endif (Threads_FOUND)
  170. check_cxx_source_compiles ("
  171. __declspec(selectany) int a;
  172. int main(void) { return 0; }
  173. " HAVE___DECLSPEC)
  174. if (WITH_TLS)
  175. set (GLOG_THREAD_LOCAL_STORAGE 1)
  176. endif (WITH_TLS)
  177. set (_PC_FIELDS
  178. "gregs[REG_PC]"
  179. "gregs[REG_EIP]"
  180. "gregs[REG_RIP]"
  181. "sc_ip"
  182. "uc_regs->gregs[PT_NIP]"
  183. "gregs[R15]"
  184. "arm_pc"
  185. "mc_eip"
  186. "mc_rip"
  187. "__gregs[REG_EIP]"
  188. "__gregs[REG_RIP]"
  189. "ss.eip"
  190. "__ss.__eip"
  191. "ss.rip"
  192. "__ss.__rip"
  193. "ss.srr0"
  194. "__ss.__srr0"
  195. )
  196. set (_PC_HEADERS ucontext.h signal.h)
  197. if (HAVE_UCONTEXT_H AND NOT PC_FROM_UCONTEXT)
  198. foreach (_PC_FIELD ${_PC_FIELDS})
  199. foreach (_PC_HEADER ${_PC_HEADERS})
  200. set (_TMP
  201. ${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/uctfield.cpp)
  202. file (WRITE ${_TMP} "
  203. #define _GNU_SOURCE 1
  204. #include <${_PC_HEADER}>
  205. int main(void)
  206. {
  207. ucontext_t u;
  208. return u.${_PC_FIELD} == 0;
  209. }
  210. ")
  211. try_compile (HAVE_PC_FROM_UCONTEXT ${CMAKE_CURRENT_BINARY_DIR} ${_TMP}
  212. COMPILE_DEFINITIONS _GNU_SOURCE=1)
  213. if (HAVE_PC_FROM_UCONTEXT)
  214. set (PC_FROM_UCONTEXT ${_PC_FIELD} CACHE)
  215. endif (HAVE_PC_FROM_UCONTEXT)
  216. endforeach (_PC_HEADER)
  217. endforeach (_PC_FIELD)
  218. endif (HAVE_UCONTEXT_H AND NOT PC_FROM_UCONTEXT)
  219. set (GOOGLE_NAMESPACE google)
  220. set (_START_GOOGLE_NAMESPACE_ "namespace ${GOOGLE_NAMESPACE} {")
  221. set (_END_GOOGLE_NAMESPACE_ "}")
  222. set (ac_cv_have_glog_export 1)
  223. if (HAVE_LIB_GFLAGS)
  224. set (ac_cv_have_libgflags 1)
  225. else (HAVE_LIB_GFLAGS)
  226. set (ac_cv_have_libgflags 0)
  227. endif (HAVE_LIB_GFLAGS)
  228. if (HAVE_SYS_TYPES_H)
  229. set (ac_cv_have_systypes_h 1)
  230. else (HAVE_SYS_TYPES_H)
  231. set (ac_cv_have_systypes_h 0)
  232. endif (HAVE_SYS_TYPES_H)
  233. if (HAVE_SSIZE_T)
  234. set (ac_cv_have_ssize_t 1)
  235. else (HAVE_SSIZE_T)
  236. set (ac_cv_have_ssize_t 0)
  237. endif (HAVE_SSIZE_T)
  238. if (HAVE_MODE_T)
  239. set (ac_cv_have_mode_t 1)
  240. else (HAVE_MODE_T)
  241. set (ac_cv_have_mode_t 0)
  242. endif (HAVE_MODE_T)
  243. if (HAVE_UNISTD_H)
  244. set (ac_cv_have_unistd_h 1)
  245. else (HAVE_UNISTD_H)
  246. set (ac_cv_have_unistd_h 0)
  247. endif (HAVE_UNISTD_H)
  248. set (ac_google_namespace ${GOOGLE_NAMESPACE})
  249. set (ac_google_end_namespace ${_END_GOOGLE_NAMESPACE_})
  250. set (ac_google_start_namespace ${_START_GOOGLE_NAMESPACE_})
  251. if (HAVE___ATTRIBUTE__)
  252. set (ac_cv___attribute___noinline "__attribute__((noinline))")
  253. set (ac_cv___attribute___printf_4_5 "__attribute__((__format__(__printf__, 4, 5)))")
  254. elseif (HAVE___DECLSPEC)
  255. #set (ac_cv___attribute___noinline "__declspec(noinline)")
  256. endif (HAVE___ATTRIBUTE__)
  257. if (HAVE___BUILTIN_EXPECT)
  258. set (ac_cv_have___builtin_expect 1)
  259. else (HAVE___BUILTIN_EXPECT)
  260. set (ac_cv_have___builtin_expect 0)
  261. endif (HAVE___BUILTIN_EXPECT)
  262. if (HAVE_EXECINFO_BACKTRACE AND HAVE_EXECINFO_BACKTRACE_SYMBOLS)
  263. set (HAVE_STACKTRACE 1)
  264. endif (HAVE_EXECINFO_BACKTRACE AND HAVE_EXECINFO_BACKTRACE_SYMBOLS)
  265. if (WITH_SYMBOLIZE)
  266. if (WIN32 OR CYGWIN)
  267. cmake_push_check_state (RESET)
  268. set (CMAKE_REQUIRED_LIBRARIES DbgHelp)
  269. check_cxx_source_runs ([=[
  270. #include <windows.h>
  271. #include <dbghelp.h>
  272. #include <cstdlib>
  273. void foobar() { }
  274. int main()
  275. {
  276. HANDLE process = GetCurrentProcess();
  277. if (!SymInitialize(process, NULL, TRUE))
  278. return EXIT_FAILURE;
  279. char buf[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
  280. SYMBOL_INFO *symbol = reinterpret_cast<SYMBOL_INFO *>(buf);
  281. symbol->SizeOfStruct = sizeof(SYMBOL_INFO);
  282. symbol->MaxNameLen = MAX_SYM_NAME;
  283. void* const pc = reinterpret_cast<void*>(&foobar);
  284. BOOL ret = SymFromAddr(process, reinterpret_cast<DWORD64>(pc), 0, symbol);
  285. return ret ? EXIT_SUCCESS : EXIT_FAILURE;
  286. }
  287. ]=] HAVE_SYMBOLIZE)
  288. cmake_pop_check_state ()
  289. if (HAVE_SYMBOLIZE)
  290. set (HAVE_STACKTRACE 1)
  291. endif (HAVE_SYMBOLIZE)
  292. elseif (UNIX)
  293. cmake_push_check_state (RESET)
  294. check_cxx_source_compiles ([=[
  295. int main()
  296. {
  297. #ifndef __ELF__
  298. #error __ELF__ not defined
  299. #endif
  300. }
  301. ]=] HAVE_SYMBOLIZE)
  302. cmake_pop_check_state ()
  303. elseif (APPLE AND HAVE_DLADDR)
  304. set (HAVE_SYMBOLIZE 1)
  305. endif (WIN32 OR CYGWIN)
  306. endif (WITH_SYMBOLIZE)
  307. # CMake manages symbolize availability. The definition is necessary only when
  308. # building the library.
  309. add_compile_definitions (GLOG_NO_SYMBOLIZE_DETECTION)
  310. check_cxx_source_compiles ("
  311. #include <cstdlib>
  312. #include <ctime>
  313. int main()
  314. {
  315. time_t timep;
  316. struct tm result;
  317. localtime_r(&timep, &result);
  318. return EXIT_SUCCESS;
  319. }
  320. " HAVE_LOCALTIME_R)
  321. set (SIZEOF_VOID_P ${CMAKE_SIZEOF_VOID_P})
  322. if (WITH_THREADS AND Threads_FOUND)
  323. if (CMAKE_USE_PTHREADS_INIT)
  324. set (HAVE_PTHREAD 1)
  325. endif (CMAKE_USE_PTHREADS_INIT)
  326. else (WITH_THREADS AND Threads_FOUND)
  327. set (NO_THREADS 1)
  328. endif (WITH_THREADS AND Threads_FOUND)
  329. # fopen/open on Cygwin can not handle unix-type paths like /home/....
  330. # therefore we translate TEST_SRC_DIR to windows-path.
  331. if (CYGWIN)
  332. execute_process (COMMAND cygpath.exe -m ${CMAKE_CURRENT_SOURCE_DIR}
  333. OUTPUT_STRIP_TRAILING_WHITESPACE
  334. OUTPUT_VARIABLE TEST_SRC_DIR)
  335. set (TEST_SRC_DIR \"${TEST_SRC_DIR}\")
  336. else (CYGWIN)
  337. set (TEST_SRC_DIR \"${CMAKE_CURRENT_SOURCE_DIR}\")
  338. endif (CYGWIN)
  339. configure_file (src/config.h.cmake.in config.h)
  340. configure_file (src/glog/logging.h.in glog/logging.h @ONLY)
  341. configure_file (src/glog/raw_logging.h.in glog/raw_logging.h @ONLY)
  342. configure_file (src/glog/stl_logging.h.in glog/stl_logging.h @ONLY)
  343. configure_file (src/glog/vlog_is_on.h.in glog/vlog_is_on.h @ONLY)
  344. add_compile_options ($<$<AND:$<BOOL:${HAVE_NO_UNNAMED_TYPE_TEMPLATE_ARGS}>,$<NOT:$<CXX_COMPILER_ID:GNU>>>:-Wno-unnamed-type-template-args>)
  345. set (_glog_CMake_BINDIR ${CMAKE_INSTALL_BINDIR})
  346. set (_glog_CMake_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR})
  347. set (_glog_CMake_LIBDIR ${CMAKE_INSTALL_LIBDIR})
  348. set (_glog_CMake_INSTALLDIR ${_glog_CMake_LIBDIR}/cmake/glog)
  349. set (_glog_CMake_DIR glog/cmake)
  350. set (_glog_CMake_DATADIR ${CMAKE_INSTALL_DATAROOTDIR}/${_glog_CMake_DIR})
  351. set (_glog_BINARY_CMake_DATADIR
  352. ${CMAKE_CURRENT_BINARY_DIR}/${_glog_CMake_DATADIR})
  353. # Add additional CMake find modules here.
  354. set (_glog_CMake_MODULES)
  355. if (Unwind_FOUND)
  356. # Copy the module only if libunwind is actually used.
  357. list (APPEND _glog_CMake_MODULES ${CMAKE_CURRENT_SOURCE_DIR}/cmake/FindUnwind.cmake)
  358. endif (Unwind_FOUND)
  359. # Generate file name for each module in the binary directory
  360. foreach (_file ${_glog_CMake_MODULES})
  361. get_filename_component (_module "${_file}" NAME)
  362. list (APPEND _glog_BINARY_CMake_MODULES
  363. ${_glog_BINARY_CMake_DATADIR}/${_module})
  364. endforeach (_file)
  365. if (_glog_CMake_MODULES)
  366. # Copy modules to binary directory during the build
  367. add_custom_command (OUTPUT ${_glog_BINARY_CMake_MODULES}
  368. COMMAND ${CMAKE_COMMAND} -E make_directory
  369. ${_glog_BINARY_CMake_DATADIR}
  370. COMMAND ${CMAKE_COMMAND} -E copy ${_glog_CMake_MODULES}
  371. ${_glog_BINARY_CMake_DATADIR}
  372. DEPENDS ${_glog_CMake_MODULES}
  373. COMMENT "Copying find modules..."
  374. )
  375. endif (_glog_CMake_MODULES)
  376. set (GLOG_PUBLIC_H
  377. ${CMAKE_CURRENT_BINARY_DIR}/glog/export.h
  378. ${CMAKE_CURRENT_BINARY_DIR}/glog/logging.h
  379. ${CMAKE_CURRENT_BINARY_DIR}/glog/raw_logging.h
  380. ${CMAKE_CURRENT_BINARY_DIR}/glog/stl_logging.h
  381. ${CMAKE_CURRENT_BINARY_DIR}/glog/vlog_is_on.h
  382. src/glog/log_severity.h
  383. src/glog/platform.h
  384. )
  385. set (GLOG_SRCS
  386. ${GLOG_PUBLIC_H}
  387. src/base/commandlineflags.h
  388. src/base/googleinit.h
  389. src/base/mutex.h
  390. src/demangle.cc
  391. src/demangle.h
  392. src/logging.cc
  393. src/raw_logging.cc
  394. src/symbolize.cc
  395. src/symbolize.h
  396. src/utilities.cc
  397. src/utilities.h
  398. src/vlog_is_on.cc
  399. )
  400. if (HAVE_PTHREAD OR WIN32 OR CYGWIN)
  401. list (APPEND GLOG_SRCS src/signalhandler.cc)
  402. endif (HAVE_PTHREAD OR WIN32 OR CYGWIN)
  403. if (CYGWIN OR WIN32)
  404. list (APPEND GLOG_SRCS
  405. src/windows/port.cc
  406. src/windows/port.h
  407. )
  408. endif (CYGWIN OR WIN32)
  409. add_library (glog_internal OBJECT
  410. ${_glog_BINARY_CMake_MODULES}
  411. ${GLOG_SRCS}
  412. )
  413. target_compile_features (glog_internal PUBLIC $<TARGET_PROPERTY:glog,COMPILE_FEATURES>)
  414. add_library (glog
  415. $<TARGET_OBJECTS:glog_internal>
  416. )
  417. target_compile_features (glog PUBLIC cxx_std_14)
  418. add_library (glog::glog ALIAS glog)
  419. set (glog_libraries_options_for_static_linking)
  420. if (Unwind_FOUND)
  421. target_link_libraries (glog PRIVATE unwind::unwind)
  422. set (glog_libraries_options_for_static_linking "${glog_libraries_options_for_static_linking} -lunwind")
  423. set (Unwind_DEPENDENCY "find_dependency (Unwind ${Unwind_VERSION})")
  424. endif (Unwind_FOUND)
  425. if (HAVE_DBGHELP)
  426. target_link_libraries (glog PRIVATE dbghelp)
  427. set (glog_libraries_options_for_static_linking "${glog_libraries_options_for_static_linking} -ldbghelp")
  428. endif (HAVE_DBGHELP)
  429. if (HAVE_PTHREAD)
  430. target_link_libraries (glog PRIVATE ${CMAKE_THREAD_LIBS_INIT})
  431. if (CMAKE_THREAD_LIBS_INIT)
  432. set (glog_libraries_options_for_static_linking "${glog_libraries_options_for_static_linking} ${CMAKE_THREAD_LIBS_INIT}")
  433. endif (CMAKE_THREAD_LIBS_INIT)
  434. endif (HAVE_PTHREAD)
  435. if (gflags_FOUND)
  436. # Prefer the gflags target that uses double colon convention
  437. if (TARGET gflags::gflags)
  438. target_link_libraries (glog PUBLIC gflags::gflags)
  439. else (TARGET gflags::gflags)
  440. target_link_libraries (glog PUBLIC gflags)
  441. endif (TARGET gflags::gflags)
  442. set (glog_libraries_options_for_static_linking "${glog_libraries_options_for_static_linking} -lgflags")
  443. endif (gflags_FOUND)
  444. if (ANDROID)
  445. target_link_libraries (glog PRIVATE log)
  446. set (glog_libraries_options_for_static_linking "${glog_libraries_options_for_static_linking} -llog")
  447. endif (ANDROID)
  448. set_target_properties (glog PROPERTIES VERSION ${PROJECT_VERSION})
  449. set_target_properties (glog PROPERTIES SOVERSION 1)
  450. if (CYGWIN OR WIN32)
  451. target_compile_definitions (glog PUBLIC GLOG_NO_ABBREVIATED_SEVERITIES)
  452. endif (CYGWIN OR WIN32)
  453. set_target_properties (glog PROPERTIES PUBLIC_HEADER "${GLOG_PUBLIC_H}")
  454. target_include_directories (glog BEFORE PUBLIC
  455. "$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>"
  456. "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>"
  457. "$<INSTALL_INTERFACE:${_glog_CMake_INCLUDE_DIR}>"
  458. PRIVATE ${CMAKE_CURRENT_BINARY_DIR}
  459. PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src)
  460. if (CYGWIN OR WIN32)
  461. target_include_directories (glog_internal PUBLIC
  462. "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/windows>"
  463. PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src/windows)
  464. target_include_directories (glog PUBLIC
  465. "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src/windows>"
  466. PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src/windows)
  467. endif (CYGWIN OR WIN32)
  468. set_target_properties (glog PROPERTIES DEFINE_SYMBOL GOOGLE_GLOG_IS_A_DLL)
  469. target_include_directories (glog_internal PUBLIC
  470. $<TARGET_PROPERTY:glog,INCLUDE_DIRECTORIES>)
  471. target_compile_definitions (glog_internal PUBLIC
  472. $<TARGET_PROPERTY:glog,COMPILE_DEFINITIONS>
  473. PRIVATE GOOGLE_GLOG_IS_A_DLL)
  474. generate_export_header (glog
  475. EXPORT_MACRO_NAME GLOG_EXPORT
  476. EXPORT_FILE_NAME ${CMAKE_CURRENT_BINARY_DIR}/glog/export.h)
  477. string (STRIP "${glog_libraries_options_for_static_linking}" glog_libraries_options_for_static_linking)
  478. if (WITH_PKGCONFIG)
  479. set (VERSION ${PROJECT_VERSION})
  480. set (prefix ${CMAKE_INSTALL_PREFIX})
  481. set (exec_prefix ${CMAKE_INSTALL_FULL_BINDIR})
  482. set (libdir ${CMAKE_INSTALL_FULL_LIBDIR})
  483. set (includedir ${CMAKE_INSTALL_FULL_INCLUDEDIR})
  484. configure_file (
  485. "${PROJECT_SOURCE_DIR}/libglog.pc.in"
  486. "${PROJECT_BINARY_DIR}/libglog.pc"
  487. @ONLY
  488. )
  489. unset (VERSION)
  490. unset (prefix)
  491. unset (exec_prefix)
  492. unset (libdir)
  493. unset (includedir)
  494. endif (WITH_PKGCONFIG)
  495. # Unit testing
  496. if (NOT WITH_FUZZING STREQUAL "none")
  497. add_executable (fuzz_demangle
  498. src/fuzz_demangle.cc
  499. )
  500. if (WITH_FUZZING STREQUAL "ossfuzz")
  501. set (LIB_FUZZING_ENGINE $ENV{LIB_FUZZING_ENGINE})
  502. target_link_libraries (fuzz_demangle PRIVATE glog ${LIB_FUZZING_ENGINE})
  503. elseif (WITH_FUZZING STREQUAL "libfuzzer")
  504. target_compile_options (fuzz_demangle PRIVATE -fsanitize=fuzzer)
  505. target_link_libraries (fuzz_demangle PRIVATE glog)
  506. else (WITH_FUZZING STREQUAL "libfuzzer")
  507. message (FATAL_ERROR "Unsupported fuzzing engine ${WITH_FUZZING}")
  508. endif (WITH_FUZZING STREQUAL "ossfuzz")
  509. endif (NOT WITH_FUZZING STREQUAL "none")
  510. if (BUILD_TESTING)
  511. add_library (glogtest STATIC
  512. $<TARGET_OBJECTS:glog_internal>
  513. )
  514. target_include_directories (glogtest PUBLIC
  515. $<TARGET_PROPERTY:glog,INCLUDE_DIRECTORIES>)
  516. target_compile_definitions (glogtest PUBLIC
  517. $<TARGET_PROPERTY:glog,COMPILE_DEFINITIONS> GLOG_STATIC_DEFINE)
  518. target_link_libraries (glogtest PUBLIC
  519. $<TARGET_PROPERTY:glog,LINK_LIBRARIES>)
  520. set (_GLOG_TEST_LIBS glogtest)
  521. if (HAVE_LIB_GTEST)
  522. list (APPEND _GLOG_TEST_LIBS GTest::gtest)
  523. endif (HAVE_LIB_GTEST)
  524. if (HAVE_LIB_GMOCK)
  525. list (APPEND _GLOG_TEST_LIBS GTest::gmock)
  526. endif (HAVE_LIB_GMOCK)
  527. add_executable (logging_unittest
  528. src/logging_unittest.cc
  529. )
  530. target_link_libraries (logging_unittest PRIVATE ${_GLOG_TEST_LIBS})
  531. add_executable (stl_logging_unittest
  532. src/stl_logging_unittest.cc
  533. )
  534. target_link_libraries (stl_logging_unittest PRIVATE ${_GLOG_TEST_LIBS})
  535. if (HAVE_NO_DEPRECATED)
  536. set_property (TARGET stl_logging_unittest APPEND PROPERTY COMPILE_OPTIONS
  537. -Wno-deprecated)
  538. endif (HAVE_NO_DEPRECATED)
  539. if (HAVE_EXT_SLIST)
  540. target_compile_definitions (stl_logging_unittest PRIVATE
  541. GLOG_STL_LOGGING_FOR_EXT_SLIST)
  542. endif (HAVE_EXT_SLIST)
  543. if (HAVE_SYMBOLIZE)
  544. add_executable (symbolize_unittest
  545. src/symbolize_unittest.cc
  546. )
  547. target_link_libraries (symbolize_unittest PRIVATE ${_GLOG_TEST_LIBS})
  548. endif (HAVE_SYMBOLIZE)
  549. add_executable (demangle_unittest
  550. src/demangle_unittest.cc
  551. )
  552. target_link_libraries (demangle_unittest PRIVATE ${_GLOG_TEST_LIBS})
  553. if (HAVE_STACKTRACE)
  554. add_executable (stacktrace_unittest
  555. src/stacktrace_unittest.cc
  556. )
  557. target_link_libraries (stacktrace_unittest PRIVATE ${_GLOG_TEST_LIBS})
  558. endif (HAVE_STACKTRACE)
  559. add_executable (utilities_unittest
  560. src/utilities_unittest.cc
  561. )
  562. target_link_libraries (utilities_unittest PRIVATE ${_GLOG_TEST_LIBS})
  563. if (HAVE_STACKTRACE AND HAVE_SYMBOLIZE)
  564. add_executable (signalhandler_unittest
  565. src/signalhandler_unittest.cc
  566. )
  567. target_link_libraries (signalhandler_unittest PRIVATE ${_GLOG_TEST_LIBS})
  568. endif (HAVE_STACKTRACE AND HAVE_SYMBOLIZE)
  569. add_test (NAME demangle COMMAND demangle_unittest)
  570. add_test (NAME logging COMMAND logging_unittest)
  571. set_tests_properties (logging PROPERTIES TIMEOUT 30)
  572. # MacOS diff is not deterministic: use the output to determine whether the
  573. # test passed.
  574. set_tests_properties (logging PROPERTIES PASS_REGULAR_EXPRESSION ".*\nPASS\n.*")
  575. # FIXME: Skip flaky test
  576. set_tests_properties (logging PROPERTIES SKIP_REGULAR_EXPRESSION
  577. "Check failed: time_ns within LogTimes::LOG_PERIOD_TOL_NS of LogTimes::LOG_PERIOD_NS")
  578. if (APPLE)
  579. # FIXME: Skip flaky test
  580. set_property (TEST logging APPEND PROPERTY SKIP_REGULAR_EXPRESSION
  581. "unexpected new.*PASS\nTest with golden file failed. We'll try to show the diff:")
  582. endif (APPLE)
  583. if (TARGET signalhandler_unittest)
  584. add_test (NAME signalhandler COMMAND signalhandler_unittest)
  585. endif (TARGET signalhandler_unittest)
  586. if (TARGET stacktrace_unittest)
  587. add_test (NAME stacktrace COMMAND stacktrace_unittest)
  588. set_tests_properties (stacktrace PROPERTIES TIMEOUT 30)
  589. endif (TARGET stacktrace_unittest)
  590. add_test (NAME stl_logging COMMAND stl_logging_unittest)
  591. if (TARGET symbolize_unittest)
  592. add_test (NAME symbolize COMMAND symbolize_unittest)
  593. # FIXME: Skip flaky test when compiled in C++20 mode
  594. set_tests_properties (symbolize PROPERTIES SKIP_REGULAR_EXPRESSION
  595. [=[Check failed: streq\("nonstatic_func"\, TrySymbolize\(\(void \*\)\(&nonstatic_func\)\)\)]=])
  596. endif (TARGET symbolize_unittest)
  597. if (HAVE_LIB_GMOCK)
  598. add_executable (mock-log_unittest
  599. src/mock-log_unittest.cc
  600. src/mock-log.h
  601. )
  602. target_link_libraries (mock-log_unittest PRIVATE ${_GLOG_TEST_LIBS})
  603. add_test (NAME mock-log COMMAND mock-log_unittest)
  604. endif (HAVE_LIB_GMOCK)
  605. # Generate an initial cache
  606. get_cache_variables (_CACHEVARS)
  607. set (_INITIAL_CACHE
  608. ${CMAKE_CURRENT_BINARY_DIR}/test_package_config/glog_package_config_initial_cache.cmake)
  609. # Package config test
  610. add_test (NAME cmake_package_config_init COMMAND ${CMAKE_COMMAND}
  611. -DTEST_BINARY_DIR=${CMAKE_CURRENT_BINARY_DIR}/test_package_config
  612. -DINITIAL_CACHE=${_INITIAL_CACHE}
  613. -DCACHEVARS=${_CACHEVARS}
  614. -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/TestInitPackageConfig.cmake
  615. )
  616. add_test (NAME cmake_package_config_generate COMMAND ${CMAKE_COMMAND}
  617. -DGENERATOR=${CMAKE_GENERATOR}
  618. -DGENERATOR_PLATFORM=${CMAKE_GENERATOR_PLATFORM}
  619. -DGENERATOR_TOOLSET=${CMAKE_GENERATOR_TOOLSET}
  620. -DINITIAL_CACHE=${_INITIAL_CACHE}
  621. -DPACKAGE_DIR=${CMAKE_CURRENT_BINARY_DIR}
  622. -DPATH=$ENV{PATH}
  623. -DSOURCE_DIR=${CMAKE_CURRENT_SOURCE_DIR}/src/package_config_unittest/working_config
  624. -DTEST_BINARY_DIR=${CMAKE_CURRENT_BINARY_DIR}/test_package_config/working_config
  625. -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/TestPackageConfig.cmake
  626. )
  627. add_test (NAME cmake_package_config_build COMMAND
  628. ${CMAKE_COMMAND} --build ${CMAKE_CURRENT_BINARY_DIR}/test_package_config/working_config
  629. --config $<CONFIG>
  630. )
  631. add_test (NAME cmake_package_config_cleanup COMMAND ${CMAKE_COMMAND} -E
  632. remove_directory
  633. ${CMAKE_CURRENT_BINARY_DIR}/test_package_config
  634. )
  635. # Fixtures setup
  636. set_tests_properties (cmake_package_config_init PROPERTIES FIXTURES_SETUP
  637. cmake_package_config)
  638. set_tests_properties (cmake_package_config_generate PROPERTIES FIXTURES_SETUP
  639. cmake_package_config_working)
  640. # Fixtures cleanup
  641. set_tests_properties (cmake_package_config_cleanup PROPERTIES FIXTURES_CLEANUP
  642. cmake_package_config)
  643. # Fixture requirements
  644. set_tests_properties (cmake_package_config_generate PROPERTIES
  645. FIXTURES_REQUIRED cmake_package_config)
  646. set_tests_properties (cmake_package_config_build PROPERTIES
  647. FIXTURES_REQUIRED "cmake_package_config;cmake_package_config_working")
  648. add_executable (cleanup_immediately_unittest
  649. src/cleanup_immediately_unittest.cc)
  650. target_link_libraries (cleanup_immediately_unittest PRIVATE ${_GLOG_TEST_LIBS})
  651. add_executable (cleanup_with_absolute_prefix_unittest
  652. src/cleanup_with_absolute_prefix_unittest.cc)
  653. target_link_libraries (cleanup_with_absolute_prefix_unittest PRIVATE ${_GLOG_TEST_LIBS})
  654. add_executable (cleanup_with_relative_prefix_unittest
  655. src/cleanup_with_relative_prefix_unittest.cc)
  656. target_link_libraries (cleanup_with_relative_prefix_unittest PRIVATE ${_GLOG_TEST_LIBS})
  657. set (CLEANUP_LOG_DIR ${CMAKE_CURRENT_BINARY_DIR}/cleanup_tests)
  658. add_test (NAME cleanup_init COMMAND
  659. ${CMAKE_COMMAND} -E make_directory ${CLEANUP_LOG_DIR})
  660. add_test (NAME cleanup_logdir COMMAND
  661. ${CMAKE_COMMAND} -E remove_directory ${CLEANUP_LOG_DIR})
  662. add_test (NAME cleanup_immediately COMMAND
  663. ${CMAKE_COMMAND}
  664. -DLOGCLEANUP=$<TARGET_FILE:cleanup_immediately_unittest>
  665. # NOTE The trailing slash is important
  666. -DTEST_DIR=${CLEANUP_LOG_DIR}/
  667. -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/RunCleanerTest1.cmake
  668. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
  669. add_test (NAME cleanup_with_absolute_prefix COMMAND
  670. ${CMAKE_COMMAND}
  671. -DLOGCLEANUP=$<TARGET_FILE:cleanup_with_absolute_prefix_unittest>
  672. -DTEST_DIR=${CMAKE_CURRENT_BINARY_DIR}/
  673. -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/RunCleanerTest2.cmake
  674. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
  675. add_test (NAME cleanup_with_relative_prefix COMMAND
  676. ${CMAKE_COMMAND}
  677. -DLOGCLEANUP=$<TARGET_FILE:cleanup_with_relative_prefix_unittest>
  678. -DTEST_DIR=${CMAKE_CURRENT_BINARY_DIR}/
  679. -DTEST_SUBDIR=test_subdir/
  680. -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/RunCleanerTest3.cmake
  681. WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
  682. # Fixtures setup
  683. set_tests_properties (cleanup_init PROPERTIES FIXTURES_SETUP logcleanuptest)
  684. ## Fixtures cleanup
  685. set_tests_properties (cleanup_logdir PROPERTIES FIXTURES_CLEANUP logcleanuptest)
  686. # Fixture requirements
  687. set_tests_properties (cleanup_immediately PROPERTIES FIXTURES_REQUIRED logcleanuptest)
  688. set_tests_properties (cleanup_with_absolute_prefix PROPERTIES FIXTURES_REQUIRED logcleanuptest)
  689. set_tests_properties (cleanup_with_relative_prefix PROPERTIES FIXTURES_REQUIRED logcleanuptest)
  690. endif (BUILD_TESTING)
  691. install (TARGETS glog
  692. EXPORT glog-targets
  693. RUNTIME DESTINATION ${_glog_CMake_BINDIR}
  694. PUBLIC_HEADER DESTINATION ${_glog_CMake_INCLUDE_DIR}/glog
  695. LIBRARY DESTINATION ${_glog_CMake_LIBDIR}
  696. ARCHIVE DESTINATION ${_glog_CMake_LIBDIR})
  697. if (WITH_PKGCONFIG)
  698. install (
  699. FILES "${PROJECT_BINARY_DIR}/libglog.pc"
  700. DESTINATION "${_glog_CMake_LIBDIR}/pkgconfig"
  701. )
  702. endif (WITH_PKGCONFIG)
  703. set (glog_CMake_VERSION 3.0)
  704. if (gflags_FOUND)
  705. # Ensure clients locate only the package config and not third party find
  706. # modules having the same name. This avoid cmake_policy PUSH/POP errors.
  707. if (CMAKE_VERSION VERSION_LESS 3.9)
  708. set (gflags_DEPENDENCY "find_dependency (gflags ${gflags_VERSION})")
  709. else (CMAKE_VERSION VERSION_LESS 3.9)
  710. # Passing additional find_package arguments to find_dependency is possible
  711. # starting with CMake 3.9.
  712. set (glog_CMake_VERSION 3.9)
  713. set (gflags_DEPENDENCY "find_dependency (gflags ${gflags_VERSION} NO_MODULE)")
  714. endif (CMAKE_VERSION VERSION_LESS 3.9)
  715. endif (gflags_FOUND)
  716. configure_package_config_file (glog-config.cmake.in
  717. ${CMAKE_CURRENT_BINARY_DIR}/glog-config.cmake
  718. INSTALL_DESTINATION ${_glog_CMake_INSTALLDIR}
  719. NO_CHECK_REQUIRED_COMPONENTS_MACRO)
  720. write_basic_package_version_file (
  721. ${CMAKE_CURRENT_BINARY_DIR}/glog-config-version.cmake
  722. COMPATIBILITY SameMajorVersion)
  723. export (TARGETS glog NAMESPACE glog:: FILE glog-targets.cmake)
  724. export (PACKAGE glog)
  725. get_filename_component (_PREFIX "${CMAKE_INSTALL_PREFIX}" ABSOLUTE)
  726. # Directory containing the find modules relative to the config install
  727. # directory.
  728. file (RELATIVE_PATH glog_REL_CMake_MODULES
  729. ${_PREFIX}/${_glog_CMake_INSTALLDIR}
  730. ${_PREFIX}/${_glog_CMake_DATADIR}/glog-modules.cmake)
  731. get_filename_component (glog_REL_CMake_DATADIR ${glog_REL_CMake_MODULES}
  732. DIRECTORY)
  733. set (glog_FULL_CMake_DATADIR
  734. ${CMAKE_CURRENT_BINARY_DIR}/${_glog_CMake_DATADIR})
  735. configure_file (glog-modules.cmake.in
  736. ${CMAKE_CURRENT_BINARY_DIR}/glog-modules.cmake @ONLY)
  737. install (CODE
  738. "
  739. set (glog_FULL_CMake_DATADIR \"\\\${CMAKE_CURRENT_LIST_DIR}/${glog_REL_CMake_DATADIR}\")
  740. set (glog_DATADIR_DESTINATION ${_glog_CMake_INSTALLDIR})
  741. if (NOT IS_ABSOLUTE ${_glog_CMake_INSTALLDIR})
  742. set (glog_DATADIR_DESTINATION \"\${CMAKE_INSTALL_PREFIX}/\${glog_DATADIR_DESTINATION}\")
  743. endif (NOT IS_ABSOLUTE ${_glog_CMake_INSTALLDIR})
  744. configure_file (\"${CMAKE_CURRENT_SOURCE_DIR}/glog-modules.cmake.in\"
  745. \"${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/glog-modules.cmake\" @ONLY)
  746. file (INSTALL
  747. \"${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/glog-modules.cmake\"
  748. DESTINATION
  749. \"\${glog_DATADIR_DESTINATION}\")
  750. "
  751. COMPONENT Development
  752. )
  753. install (FILES
  754. ${CMAKE_CURRENT_BINARY_DIR}/glog-config.cmake
  755. ${CMAKE_CURRENT_BINARY_DIR}/glog-config-version.cmake
  756. DESTINATION ${_glog_CMake_INSTALLDIR})
  757. # Find modules in share/glog/cmake
  758. install (DIRECTORY ${_glog_BINARY_CMake_DATADIR}
  759. DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/glog
  760. COMPONENT Development
  761. FILES_MATCHING PATTERN "*.cmake"
  762. )
  763. install (EXPORT glog-targets NAMESPACE glog:: DESTINATION
  764. ${_glog_CMake_INSTALLDIR})