From 742fe94f22650515b7c71fb272af1d76915f40b9 Mon Sep 17 00:00:00 2001 From: Vyacheslav Yurkov Date: Tue, 9 Oct 2018 09:56:16 +0200 Subject: [PATCH 1/4] Use libunwind as an imported target When UNWIND_LIBRARY is used directly as a public dependency then absolute path is stored in cmake config file. This is an issue when glog is used as part of an SDK, which was built somewhere else. When SDK is installed on developer's machine, cmake config contains a full path to non-existent location. The solution is to find libunwind during configure stage and store target name as a dependency, not a full path. Cmake module looks for libunwind and libunwind-PLAT, where PLAT is one of supported platforms. Signed-off-by: Vyacheslav Yurkov --- CMakeLists.txt | 13 ++++----- cmake/FindLibunwind.cmake | 55 +++++++++++++++++++++++++++++++++++++++ glog-config.cmake.in | 3 +++ 3 files changed, 63 insertions(+), 8 deletions(-) create mode 100644 cmake/FindLibunwind.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 4f64b41..57e3e11 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -65,7 +65,6 @@ check_include_file (dlfcn.h HAVE_DLFCN_H) check_include_file (execinfo.h HAVE_EXECINFO_H) check_include_file (glob.h HAVE_GLOB_H) check_include_file (inttypes.h HAVE_INTTYPES_H) -check_include_file (libunwind.h HAVE_LIBUNWIND_H) check_include_file (memory.h HAVE_MEMORY_H) check_include_file (pwd.h HAVE_PWD_H) check_include_file (stdint.h HAVE_STDINT_H) @@ -81,7 +80,6 @@ check_include_file (syscall.h HAVE_SYSCALL_H) check_include_file (syslog.h HAVE_SYSLOG_H) check_include_file (ucontext.h HAVE_UCONTEXT_H) check_include_file (unistd.h HAVE_UNISTD_H) -check_include_file (unwind.h HAVE_UNWIND_H) check_include_file (pwd.h HAVE_PWD_H) check_include_file_cxx ("ext/hash_map" HAVE_EXT_HASH_MAP) @@ -117,11 +115,9 @@ check_cxx_compiler_flag (-Wunnamed-type-template-args # snprintf as an inline function check_symbol_exists (snprintf stdio.h HAVE_SNPRINTF) -check_library_exists (unwind get_static_proc_name "" HAVE_LIB_UNWIND) check_library_exists (dbghelp UnDecorateSymbolName "" HAVE_DBGHELP) -find_library (UNWIND_LIBRARY NAMES unwind DOC "unwind library") -mark_as_advanced (UNWIND_LIBRARY) +find_package(Libunwind) check_c_source_compiles (" #include @@ -491,9 +487,9 @@ add_library(glog::glog ALIAS glog) set_target_properties (glog PROPERTIES POSITION_INDEPENDENT_CODE ON) -if (UNWIND_LIBRARY) - target_link_libraries (glog PUBLIC ${UNWIND_LIBRARY}) -endif (UNWIND_LIBRARY) +if (Libunwind_FOUND) + target_link_libraries (glog PUBLIC unwind) +endif (Libunwind_FOUND) if (HAVE_DBGHELP) target_link_libraries (glog PUBLIC dbghelp) @@ -705,6 +701,7 @@ export (PACKAGE glog) install (FILES ${CMAKE_CURRENT_BINARY_DIR}/glog-config.cmake ${CMAKE_CURRENT_BINARY_DIR}/glog-config-version.cmake + ${CMAKE_CURRENT_SOURCE_DIR}/cmake/FindLibunwind.cmake DESTINATION ${_glog_CMake_INSTALLDIR}) install (EXPORT glog-targets NAMESPACE glog:: DESTINATION diff --git a/cmake/FindLibunwind.cmake b/cmake/FindLibunwind.cmake new file mode 100644 index 0000000..ef4f5bc --- /dev/null +++ b/cmake/FindLibunwind.cmake @@ -0,0 +1,55 @@ +# - Try to find libunwind +# Once done this will define +# +# Libunwind_FOUND - system has libunwind +# unwind - cmake target for libunwind + +find_library (UNWIND_LIBRARY NAMES unwind DOC "unwind library") +include (CheckIncludeFile) +check_include_file (libunwind.h HAVE_LIBUNWIND_H) +check_include_file (unwind.h HAVE_UNWIND_H) + +if (CMAKE_SYSTEM_PROCESSOR MATCHES "^arm") + set(LIBUNWIND_ARCH "arm") +elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "^aarch64") + set(LIBUNWIND_ARCH "aarch64") +elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64" OR + CMAKE_SYSTEM_PROCESSOR STREQUAL "amd64" OR + CMAKE_SYSTEM_PROCESSOR STREQUAL "corei7-64") + set(LIBUNWIND_ARCH "x86_64") +elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "^i.86$") + set(LIBUNWIND_ARCH "x86") +elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "^ppc64") + set(LIBUNWIND_ARCH "ppc64") +elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "^ppc") + set(LIBUNWIND_ARCH "ppc32") +elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "^mips") + set(LIBUNWIND_ARCH "mips") +elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "^hppa") + set(LIBUNWIND_ARCH "hppa") +elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "^ia64") + set(LIBUNWIND_ARCH "ia64") +endif() + +find_library (UNWIND_LIBRARY_PLATFORM NAMES "unwind-${LIBUNWIND_ARCH}" DOC "unwind library platform") +if (UNWIND_LIBRARY_PLATFORM) + set(HAVE_LIB_UNWIND "1") +endif() + +include(FindPackageHandleStandardArgs) +# handle the QUIETLY and REQUIRED arguments and set Libunwind_FOUND to TRUE +# if all listed variables are TRUE +find_package_handle_standard_args(Libunwind DEFAULT_MSG + UNWIND_LIBRARY HAVE_LIBUNWIND_H HAVE_UNWIND_H HAVE_LIB_UNWIND) + +mark_as_advanced (UNWIND_LIBRARY UNWIND_LIBRARY_PLATFORM) + +if (Libunwind_FOUND) + add_library(unwind INTERFACE IMPORTED) + set_target_properties(unwind PROPERTIES + INTERFACE_LINK_LIBRARIES "${UNWIND_LIBRARY};${UNWIND_LIBRARY_PLATFORM}" + ) +else() + message("Can't find libunwind library") +endif() + diff --git a/glog-config.cmake.in b/glog-config.cmake.in index aabdedc..a7b3cdb 100644 --- a/glog-config.cmake.in +++ b/glog-config.cmake.in @@ -8,4 +8,7 @@ include (CMakeFindDependencyMacro) @gflags_DEPENDENCY@ +list (APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}") +find_dependency (Libunwind) + include ("${CMAKE_CURRENT_LIST_DIR}/glog-targets.cmake") From 19570c69c976b2a402b8658e67e2272ed910ff68 Mon Sep 17 00:00:00 2001 From: Sergiu Deitsch Date: Sat, 2 Nov 2019 00:09:21 +0100 Subject: [PATCH 2/4] renamed and cleaned up the unwind find module --- CMakeLists.txt | 2 +- cmake/FindLibunwind.cmake | 55 ---------------------------- cmake/FindUnwind.cmake | 76 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 56 deletions(-) delete mode 100644 cmake/FindLibunwind.cmake create mode 100644 cmake/FindUnwind.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 57e3e11..75b7df2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -117,7 +117,7 @@ check_symbol_exists (snprintf stdio.h HAVE_SNPRINTF) check_library_exists (dbghelp UnDecorateSymbolName "" HAVE_DBGHELP) -find_package(Libunwind) +find_package (Unwind) check_c_source_compiles (" #include diff --git a/cmake/FindLibunwind.cmake b/cmake/FindLibunwind.cmake deleted file mode 100644 index ef4f5bc..0000000 --- a/cmake/FindLibunwind.cmake +++ /dev/null @@ -1,55 +0,0 @@ -# - Try to find libunwind -# Once done this will define -# -# Libunwind_FOUND - system has libunwind -# unwind - cmake target for libunwind - -find_library (UNWIND_LIBRARY NAMES unwind DOC "unwind library") -include (CheckIncludeFile) -check_include_file (libunwind.h HAVE_LIBUNWIND_H) -check_include_file (unwind.h HAVE_UNWIND_H) - -if (CMAKE_SYSTEM_PROCESSOR MATCHES "^arm") - set(LIBUNWIND_ARCH "arm") -elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "^aarch64") - set(LIBUNWIND_ARCH "aarch64") -elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64" OR - CMAKE_SYSTEM_PROCESSOR STREQUAL "amd64" OR - CMAKE_SYSTEM_PROCESSOR STREQUAL "corei7-64") - set(LIBUNWIND_ARCH "x86_64") -elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "^i.86$") - set(LIBUNWIND_ARCH "x86") -elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "^ppc64") - set(LIBUNWIND_ARCH "ppc64") -elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "^ppc") - set(LIBUNWIND_ARCH "ppc32") -elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "^mips") - set(LIBUNWIND_ARCH "mips") -elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "^hppa") - set(LIBUNWIND_ARCH "hppa") -elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "^ia64") - set(LIBUNWIND_ARCH "ia64") -endif() - -find_library (UNWIND_LIBRARY_PLATFORM NAMES "unwind-${LIBUNWIND_ARCH}" DOC "unwind library platform") -if (UNWIND_LIBRARY_PLATFORM) - set(HAVE_LIB_UNWIND "1") -endif() - -include(FindPackageHandleStandardArgs) -# handle the QUIETLY and REQUIRED arguments and set Libunwind_FOUND to TRUE -# if all listed variables are TRUE -find_package_handle_standard_args(Libunwind DEFAULT_MSG - UNWIND_LIBRARY HAVE_LIBUNWIND_H HAVE_UNWIND_H HAVE_LIB_UNWIND) - -mark_as_advanced (UNWIND_LIBRARY UNWIND_LIBRARY_PLATFORM) - -if (Libunwind_FOUND) - add_library(unwind INTERFACE IMPORTED) - set_target_properties(unwind PROPERTIES - INTERFACE_LINK_LIBRARIES "${UNWIND_LIBRARY};${UNWIND_LIBRARY_PLATFORM}" - ) -else() - message("Can't find libunwind library") -endif() - diff --git a/cmake/FindUnwind.cmake b/cmake/FindUnwind.cmake new file mode 100644 index 0000000..ae530df --- /dev/null +++ b/cmake/FindUnwind.cmake @@ -0,0 +1,76 @@ +# - Try to find libunwind +# Once done this will define +# +# Unwind_FOUND - system has libunwind +# unwind::unwind - cmake target for libunwind + +include (FindPackageHandleStandardArgs) + +find_path (Unwind_INCLUDE_DIR NAMES unwind.h libunwind.h DOC "unwind include directory") +find_library (Unwind_LIBRARY NAMES unwind DOC "unwind library") + +if (CMAKE_SYSTEM_PROCESSOR MATCHES "^arm") + set (Unwind_ARCH "arm") +elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "^aarch64") + set (Unwind_ARCH "aarch64") +elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64" OR + CMAKE_SYSTEM_PROCESSOR STREQUAL "amd64" OR + CMAKE_SYSTEM_PROCESSOR STREQUAL "corei7-64") + set (Unwind_ARCH "x86_64") +elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "^i.86$") + set (Unwind_ARCH "x86") +elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "^ppc64") + set (Unwind_ARCH "ppc64") +elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "^ppc") + set (Unwind_ARCH "ppc32") +elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "^mips") + set (Unwind_ARCH "mips") +elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "^hppa") + set (Unwind_ARCH "hppa") +elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "^ia64") + set (Unwind_ARCH "ia64") +endif (CMAKE_SYSTEM_PROCESSOR MATCHES "^arm") + +find_library (Unwind_PLATFORM_LIBRARY NAMES "unwind-${Unwind_ARCH}" + DOC "unwind library platform") + +mark_as_advanced (Unwind_INCLUDE_DIR Unwind_LIBRARY Unwind_PLATFORM_LIBRARY) + +# Extract version information +if (Unwind_LIBRARY) + set (_Unwind_VERSION_HEADER ${Unwind_INCLUDE_DIR}/libunwind-common.h) + + if (EXISTS ${_Unwind_VERSION_HEADER}) + FILE (READ ${_Unwind_VERSION_HEADER} _Unwind_VERSION_CONTENTS) + + string (REGEX REPLACE ".*#define UNW_VERSION_MAJOR[ \t]+([0-9]+).*" "\\1" + Unwind_VERSION_MAJOR "${_Unwind_VERSION_CONTENTS}") + string (REGEX REPLACE ".*#define UNW_VERSION_MINOR[ \t]+([0-9]+).*" "\\1" + Unwind_VERSION_MINOR "${_Unwind_VERSION_CONTENTS}") + string (REGEX REPLACE ".*#define UNW_VERSION_EXTRA[ \t]+([0-9]+).*" "\\1" + Unwind_VERSION_PATCH "${_Unwind_VERSION_CONTENTS}") + + set (Unwind_VERSION + ${Unwind_VERSION_MAJOR}.${Unwind_VERSION_MINOR}.${Unwind_VERSION_PATCH}) + set (Unwind_VERSION_COMPONENTS 3) + endif (EXISTS ${_Unwind_VERSION_HEADER}) +endif (Unwind_LIBRARY) + +# handle the QUIETLY and REQUIRED arguments and set Unwind_FOUND to TRUE +# if all listed variables are TRUE +find_package_handle_standard_args (Unwind REQUIRED_VARS Unwind_INCLUDE_DIR + Unwind_LIBRARY Unwind_PLATFORM_LIBRARY VERSION_VAR Unwind_VERSION) + +if (Unwind_FOUND) + add_library (unwind::unwind INTERFACE IMPORTED) + + set_property (TARGET unwind::unwind PROPERTY + INTERFACE_INCLUDE_DIRECTORIES ${Unwind_INCLUDE_DIR} + ) + set_property (TARGET unwind::unwind PROPERTY + INTERFACE_LINK_LIBRARIES ${Unwind_LIBRARY} ${Unwind_PLATFORM_LIBRARY} + ) + set_property (TARGET unwind::unwind PROPERTY + IMPORTED_CONFIGURATIONS RELEASE + ) +endif (Unwind_FOUND) From 9a43cfb8d2fa17eea27c73cf3f812de1df3b5e93 Mon Sep 17 00:00:00 2001 From: Sergiu Deitsch Date: Sat, 2 Nov 2019 00:09:32 +0100 Subject: [PATCH 3/4] install unwind find module alongside glog --- CMakeLists.txt | 113 ++++++++++++++++++++++++++++++----- glog-config.cmake.in | 7 +-- glog-modules.cmake.in | 18 ++++++ src/stacktrace_windows-inl.h | 2 +- 4 files changed, 120 insertions(+), 20 deletions(-) create mode 100644 glog-modules.cmake.in diff --git a/CMakeLists.txt b/CMakeLists.txt index 75b7df2..48afb07 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,7 +23,16 @@ option (WITH_TLS "Enable Thread Local Storage (TLS) support" ON) option (BUILD_SHARED_LIBS "Build shared libraries" OFF) option (PRINT_UNSYMBOLIZED_STACK_TRACES "Print raw pc values on symbolization failure" OFF) -option (WITH_PKGCONFIG " Enable pkg-config support" ON) +option (WITH_PKGCONFIG "Enable pkg-config support" ON) +option (WITH_UNWIND "Enable libunwind support" ON) + +if (NOT WITH_UNWIND) + set (CMAKE_DISABLE_FIND_PACKAGE_Unwind ON) +endif (NOT WITH_UNWIND) + +if (NOT WITH_THREADS) + set (CMAKE_DISABLE_FIND_PACKAGE_Threads ON) +endif (NOT WITH_THREADS) list (APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake) @@ -57,9 +66,13 @@ if (WITH_GFLAGS) endif (gflags_FOUND) endif (WITH_GFLAGS) -if (WITH_THREADS) - find_package (Threads) -endif (WITH_THREADS) +find_package (Threads) +find_package (Unwind) + +if (Unwind_FOUND) + set (HAVE_LIB_UNWIND 1) + set (HAVE_UNWIND_H 1) +endif (Unwind_FOUND) check_include_file (dlfcn.h HAVE_DLFCN_H) check_include_file (execinfo.h HAVE_EXECINFO_H) @@ -117,8 +130,6 @@ check_symbol_exists (snprintf stdio.h HAVE_SNPRINTF) check_library_exists (dbghelp UnDecorateSymbolName "" HAVE_DBGHELP) -find_package (Unwind) - check_c_source_compiles (" #include static void foo(void) __attribute__ ((unused)); @@ -480,16 +491,57 @@ endif (WIN32) add_compile_options ($<$,$>>:-Wno-unnamed-type-template-args>) +set (_glog_CMake_BINDIR ${CMAKE_INSTALL_BINDIR}) +set (_glog_CMake_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR}) +set (_glog_CMake_LIBDIR ${CMAKE_INSTALL_LIBDIR}) +set (_glog_CMake_INSTALLDIR ${_glog_CMake_LIBDIR}/cmake/glog) + +set (_glog_CMake_DIR glog/cmake) +set (_glog_CMake_DATADIR ${CMAKE_INSTALL_DATAROOTDIR}/${_glog_CMake_DIR}) +set (_glog_BINARY_CMake_DATADIR + ${CMAKE_CURRENT_BINARY_DIR}/${_glog_CMake_DATADIR}) + +# Add additional CMake find modules here. +set (_glog_CMake_MODULES) + +if (Unwind_FOUND) + # Copy the module only if libunwind is actually used. + list (APPEND _glog_CMake_MODULES ${CMAKE_CURRENT_SOURCE_DIR}/cmake/FindUnwind.cmake) +endif (Unwind_FOUND) + +# Generate file name for each module in the binary directory +foreach (_file ${_glog_CMake_MODULES}) + get_filename_component (_module "${_file}" NAME) + + list (APPEND _glog_BINARY_CMake_MODULES + ${_glog_BINARY_CMake_DATADIR}/${_module}) +endforeach (_file) + +if (_glog_CMake_MODULES) + # Copy modules to binary directory during the build + add_custom_command (OUTPUT ${_glog_BINARY_CMake_MODULES} + COMMAND ${CMAKE_COMMAND} -E make_directory + ${_glog_BINARY_CMake_DATADIR} + COMMAND ${CMAKE_COMMAND} -E copy ${_glog_CMake_MODULES} + ${_glog_BINARY_CMake_DATADIR} + DEPENDS ${_glog_CMake_MODULES} + COMMENT "Copying find modules..." + ) +endif (_glog_CMake_MODULES) + add_library (glog ${GLOG_SRCS} + ${_glog_BINARY_CMake_MODULES} ) + add_library(glog::glog ALIAS glog) set_target_properties (glog PROPERTIES POSITION_INDEPENDENT_CODE ON) -if (Libunwind_FOUND) - target_link_libraries (glog PUBLIC unwind) -endif (Libunwind_FOUND) +if (Unwind_FOUND) + target_link_libraries (glog PUBLIC unwind::unwind) + set (Unwind_DEPENDENCY "find_dependency (Unwind ${Unwind_VERSION})") +endif (Unwind_FOUND) if (HAVE_DBGHELP) target_link_libraries (glog PUBLIC dbghelp) @@ -522,11 +574,6 @@ endif (WIN32) set_target_properties (glog PROPERTIES PUBLIC_HEADER "${GLOG_PUBLIC_H}") -set (_glog_CMake_BINDIR ${CMAKE_INSTALL_BINDIR}) -set (_glog_CMake_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR}) -set (_glog_CMake_LIBDIR ${CMAKE_INSTALL_LIBDIR}) -set (_glog_CMake_INSTALLDIR ${_glog_CMake_LIBDIR}/cmake/glog) - target_include_directories (glog BEFORE PUBLIC "$" "$" @@ -698,11 +745,47 @@ write_basic_package_version_file (glog-config-version.cmake VERSION export (TARGETS glog NAMESPACE glog:: FILE glog-targets.cmake) export (PACKAGE glog) +get_filename_component (_PREFIX "${CMAKE_INSTALL_PREFIX}" ABSOLUTE) + +# Directory containing the find modules relative to the config install +# directory. +file (RELATIVE_PATH glog_REL_CMake_MODULES + ${_PREFIX}/${_glog_CMake_INSTALLDIR} + ${_PREFIX}/${_glog_CMake_DATADIR}/glog-modules.cmake) + +get_filename_component (glog_REL_CMake_DATADIR ${glog_REL_CMake_MODULES} + DIRECTORY) + +set (glog_FULL_CMake_DATADIR + ${CMAKE_CURRENT_BINARY_DIR}/${_glog_CMake_DATADIR}) + +configure_file (glog-modules.cmake.in + ${CMAKE_CURRENT_BINARY_DIR}/glog-modules.cmake @ONLY) + +install (CODE +" +set (glog_FULL_CMake_DATADIR \"\\\${CMAKE_CURRENT_LIST_DIR}/${glog_REL_CMake_DATADIR}\") +configure_file (${CMAKE_CURRENT_SOURCE_DIR}/glog-modules.cmake.in + \"${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/glog-modules.cmake\" @ONLY) +file (INSTALL + \"${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/glog-modules.cmake\" + DESTINATION + \"\${CMAKE_INSTALL_PREFIX}/${_glog_CMake_INSTALLDIR}\") +" + COMPONENT Development +) + install (FILES ${CMAKE_CURRENT_BINARY_DIR}/glog-config.cmake ${CMAKE_CURRENT_BINARY_DIR}/glog-config-version.cmake - ${CMAKE_CURRENT_SOURCE_DIR}/cmake/FindLibunwind.cmake DESTINATION ${_glog_CMake_INSTALLDIR}) +# Find modules in share/glog/cmake +install (DIRECTORY ${_glog_BINARY_CMake_DATADIR} + DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/glog + COMPONENT Development + FILES_MATCHING PATTERN "*.cmake" +) + install (EXPORT glog-targets NAMESPACE glog:: DESTINATION ${_glog_CMake_INSTALLDIR}) diff --git a/glog-config.cmake.in b/glog-config.cmake.in index a7b3cdb..5c5c9c0 100644 --- a/glog-config.cmake.in +++ b/glog-config.cmake.in @@ -5,10 +5,9 @@ endif (CMAKE_VERSION VERSION_LESS @glog_CMake_VERSION@) @PACKAGE_INIT@ include (CMakeFindDependencyMacro) +include (${CMAKE_CURRENT_LIST_DIR}/glog-modules.cmake) @gflags_DEPENDENCY@ +@Unwind_DEPENDENCY@ -list (APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}") -find_dependency (Libunwind) - -include ("${CMAKE_CURRENT_LIST_DIR}/glog-targets.cmake") +include (${CMAKE_CURRENT_LIST_DIR}/glog-targets.cmake) diff --git a/glog-modules.cmake.in b/glog-modules.cmake.in new file mode 100644 index 0000000..71c5160 --- /dev/null +++ b/glog-modules.cmake.in @@ -0,0 +1,18 @@ +cmake_policy (PUSH) +cmake_policy (SET CMP0057 NEW) + +if (CMAKE_VERSION VERSION_LESS 3.3) + message (FATAL_ERROR "glog-modules.cmake requires the consumer " + "to use CMake 3.3 (or newer)") +endif (CMAKE_VERSION VERSION_LESS 3.3) + +set (glog_MODULE_PATH "@glog_FULL_CMake_DATADIR@") +list (APPEND CMAKE_MODULE_PATH ${glog_MODULE_PATH}) + +if (NOT glog_MODULE_PATH IN_LIST CMAKE_MODULE_PATH) + message (FATAL_ERROR "Cannot add '${glog_MODULE_PATH}' to " + "CMAKE_MODULE_PATH. This will cause glog-config.cmake to fail at " + "locating required find modules. Make sure CMAKE_MODULE_PATH is not a cache variable.") +endif (NOT glog_MODULE_PATH IN_LIST CMAKE_MODULE_PATH) + +cmake_policy (POP) diff --git a/src/stacktrace_windows-inl.h b/src/stacktrace_windows-inl.h index 7263188..f7553a6 100644 --- a/src/stacktrace_windows-inl.h +++ b/src/stacktrace_windows-inl.h @@ -34,7 +34,7 @@ #include "config.h" #include "port.h" #include "stacktrace.h" -#include +#include _START_GOOGLE_NAMESPACE_ From c0463b17b6e88868498f5d9fd0db1b271d7c67ae Mon Sep 17 00:00:00 2001 From: Sergiu Deitsch Date: Sun, 10 Nov 2019 11:29:52 +0100 Subject: [PATCH 4/4] added cmake package config unit test --- .gitignore | 16 ++--- CMakeLists.txt | 52 +++++++++++++++ cmake/GetCacheVariables.cmake | 63 +++++++++++++++++++ cmake/TestInitPackageConfig.cmake | 12 ++++ cmake/TestPackageConfig.cmake | 33 ++++++++++ .../working_config/CMakeLists.txt | 8 +++ .../working_config/glog_package_config.cc | 6 ++ 7 files changed, 182 insertions(+), 8 deletions(-) create mode 100644 cmake/GetCacheVariables.cmake create mode 100644 cmake/TestInitPackageConfig.cmake create mode 100644 cmake/TestPackageConfig.cmake create mode 100644 src/package_config_unittest/working_config/CMakeLists.txt create mode 100644 src/package_config_unittest/working_config/glog_package_config.cc diff --git a/.gitignore b/.gitignore index 76d251f..b3158cf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,12 +1,12 @@ +/*.cmake +/*.filters +/*.sln +/*.vcxproj autom4te.cache -glog-*.tar.gz -packages/rpm-unknown -packages/debian-* +bazel-* CMakeCache.txt CMakeFiles/ -*.cmake config.h -*.sln -*.vcxproj -*.filters -bazel-* +glog-*.tar.gz +packages/debian-* +packages/rpm-unknown diff --git a/CMakeLists.txt b/CMakeLists.txt index 48afb07..1b8a66b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -52,6 +52,7 @@ include (CMakePushCheckState) include (CPack) include (CTest) include (DetermineGflagsNamespace) +include (GetCacheVariables) include (GNUInstallDirs) set (CMAKE_DEBUG_POSTFIX d) @@ -703,6 +704,57 @@ if (BUILD_TESTING) if (TARGET symbolize_unittest) add_test (NAME symbolize COMMAND symbolize_unittest) endif (TARGET symbolize_unittest) + + # Generate an initial cache + + get_cache_variables (_CACHEVARS EXCLUDE CMAKE_MAKE_PROGRAM) + + set (_INITIAL_CACHE + ${CMAKE_CURRENT_BINARY_DIR}/test_package_config/glog_package_config_initial_cache.cmake) + + # Package config test + + add_test (NAME cmake_package_config_init COMMAND ${CMAKE_COMMAND} + -DTEST_BINARY_DIR=${CMAKE_CURRENT_BINARY_DIR}/test_package_config + -DINITIAL_CACHE=${_INITIAL_CACHE} + -DCACHEVARS=${_CACHEVARS} + -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/TestInitPackageConfig.cmake + ) + + add_test (NAME cmake_package_config_generate COMMAND ${CMAKE_COMMAND} + -DPATH=$ENV{PATH} + -DINITIAL_CACHE=${_INITIAL_CACHE} + -DTEST_BINARY_DIR=${CMAKE_CURRENT_BINARY_DIR}/test_package_config/working_config + -DSOURCE_DIR=${CMAKE_CURRENT_SOURCE_DIR}/src/package_config_unittest/working_config + -DPACKAGE_DIR=${CMAKE_CURRENT_BINARY_DIR} + -DGENERATOR=${CMAKE_GENERATOR} + -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/TestPackageConfig.cmake + ) + + add_test (NAME cmake_package_config_build COMMAND + ${CMAKE_COMMAND} --build ${CMAKE_CURRENT_BINARY_DIR}/test_package_config/working_config + ) + + add_test (NAME cmake_package_config_cleanup COMMAND ${CMAKE_COMMAND} -E + remove_directory + ${CMAKE_CURRENT_BINARY_DIR}/test_package_config + ) + + # Fixtures setup + set_tests_properties (cmake_package_config_init PROPERTIES FIXTURES_SETUP + cmake_package_config) + set_tests_properties (cmake_package_config_generate PROPERTIES FIXTURES_SETUP + cmake_package_config_working) + + # Fixtures cleanup + set_tests_properties (cmake_package_config_cleanup PROPERTIES FIXTURES_CLEANUP + cmake_package_config) + + # Fixture requirements + set_tests_properties (cmake_package_config_generate PROPERTIES + FIXTURES_REQUIRED cmake_package_config) + set_tests_properties (cmake_package_config_build PROPERTIES + FIXTURES_REQUIRED "cmake_package_config;cmake_package_config_working") endif (BUILD_TESTING) install (TARGETS glog diff --git a/cmake/GetCacheVariables.cmake b/cmake/GetCacheVariables.cmake new file mode 100644 index 0000000..20485a8 --- /dev/null +++ b/cmake/GetCacheVariables.cmake @@ -0,0 +1,63 @@ +cmake_policy (PUSH) +cmake_policy (VERSION 3.3) + +include (CMakeParseArguments) + +function (get_cache_variables _CACHEVARS) + set (_SINGLE) + set (_MULTI EXCLUDE) + set (_OPTIONS) + + cmake_parse_arguments (_ARGS "${_OPTIONS}" "${_SINGLE}" "${_MULTI}" ${ARGS} ${ARGN}) + + get_cmake_property (_VARIABLES VARIABLES) + + set (CACHEVARS) + + foreach (_VAR ${_VARIABLES}) + if (DEFINED _ARGS_EXCLUDE) + if ("${_VAR}" IN_LIST _ARGS_EXCLUDE) + continue () + endif ("${_VAR}" IN_LIST _ARGS_EXCLUDE) + endif (DEFINED _ARGS_EXCLUDE) + + get_property (_CACHEVARTYPE CACHE ${_VAR} PROPERTY TYPE) + + if ("${_CACHEVARTYPE}" STREQUAL INTERNAL OR + "${_CACHEVARTYPE}" STREQUAL STATIC OR + "${_CACHEVARTYPE}" STREQUAL UNINITIALIZED) + continue () + endif ("${_CACHEVARTYPE}" STREQUAL INTERNAL OR + "${_CACHEVARTYPE}" STREQUAL STATIC OR + "${_CACHEVARTYPE}" STREQUAL UNINITIALIZED) + + get_property (_CACHEVARVAL CACHE ${_VAR} PROPERTY VALUE) + + if ("${_CACHEVARVAL}" STREQUAL "") + continue () + endif ("${_CACHEVARVAL}" STREQUAL "") + + get_property (_CACHEVARDOC CACHE ${_VAR} PROPERTY HELPSTRING) + + # Escape " in values + string (REPLACE "\"" "\\\"" _CACHEVARVAL "${_CACHEVARVAL}") + # Escape " in help strings + string (REPLACE "\"" "\\\"" _CACHEVARDOC "${_CACHEVARDOC}") + # Escape ; in values + string (REPLACE ";" "\\\;" _CACHEVARVAL "${_CACHEVARVAL}") + # Escape backslash in values + string (REGEX REPLACE "\\\\([^\"])" "\\\\\\1" _CACHEVARVAL "${_CACHEVARVAL}") + + if (NOT "${_CACHEVARTYPE}" STREQUAL BOOL) + set (_CACHEVARVAL "\"${_CACHEVARVAL}\"") + endif (NOT "${_CACHEVARTYPE}" STREQUAL BOOL) + + if (NOT "${_CACHEVARTYPE}" STREQUAL "" AND NOT "${_CACHEVARVAL}" STREQUAL "") + set (CACHEVARS "${CACHEVARS}set (${_VAR} ${_CACHEVARVAL} CACHE ${_CACHEVARTYPE} \"${_CACHEVARDOC}\")\n") + endif (NOT "${_CACHEVARTYPE}" STREQUAL "" AND NOT "${_CACHEVARVAL}" STREQUAL "") + endforeach (_VAR) + + set (${_CACHEVARS} ${CACHEVARS} PARENT_SCOPE) +endfunction (get_cache_variables) + +cmake_policy (POP) diff --git a/cmake/TestInitPackageConfig.cmake b/cmake/TestInitPackageConfig.cmake new file mode 100644 index 0000000..6cb11cc --- /dev/null +++ b/cmake/TestInitPackageConfig.cmake @@ -0,0 +1,12 @@ +# Create the build directory +execute_process ( + COMMAND ${CMAKE_COMMAND} -E make_directory ${TEST_BINARY_DIR} + RESULT_VARIABLE _DIRECTORY_CREATED_SUCCEEDED +) + +if (NOT _DIRECTORY_CREATED_SUCCEEDED EQUAL 0) + message (FATAL_ERROR "Failed to create build directory") +endif (NOT _DIRECTORY_CREATED_SUCCEEDED EQUAL 0) + +file (WRITE ${INITIAL_CACHE} "${CACHEVARS}") + diff --git a/cmake/TestPackageConfig.cmake b/cmake/TestPackageConfig.cmake new file mode 100644 index 0000000..191c3ca --- /dev/null +++ b/cmake/TestPackageConfig.cmake @@ -0,0 +1,33 @@ +# Create the build directory +execute_process ( + COMMAND ${CMAKE_COMMAND} -E make_directory ${TEST_BINARY_DIR} + RESULT_VARIABLE _DIRECTORY_CREATED_SUCCEEDED +) + +if (NOT _DIRECTORY_CREATED_SUCCEEDED EQUAL 0) + message (FATAL_ERROR "Failed to create build directory") +endif (NOT _DIRECTORY_CREATED_SUCCEEDED EQUAL 0) + +# Capture the PATH environment variable content set during project +# generation stage. This is required because later during the build stage +# the PATH is modified again (e.g., for MinGW AppVeyor CI builds) by adding +# back the directory containing git.exe. Incidently, the Git installation +# directory also contains sh.exe which causes MinGW Makefile generation to +# fail. +set (ENV{PATH} ${PATH}) + +# Run CMake +execute_process ( + COMMAND ${CMAKE_COMMAND} -C ${INITIAL_CACHE} + -G ${GENERATOR} + -DCMAKE_PREFIX_PATH=${PACKAGE_DIR} + -DCMAKE_FIND_PACKAGE_NO_PACKAGE_REGISTRY=ON + -DCMAKE_FIND_PACKAGE_NO_SYSTEM_PACKAGE_REGISTRY=ON + ${SOURCE_DIR} + WORKING_DIRECTORY ${TEST_BINARY_DIR} + RESULT_VARIABLE _GENERATE_SUCCEEDED +) + +if (NOT _GENERATE_SUCCEEDED EQUAL 0) + message (FATAL_ERROR "Failed to generate project files using CMake") +endif (NOT _GENERATE_SUCCEEDED EQUAL 0) diff --git a/src/package_config_unittest/working_config/CMakeLists.txt b/src/package_config_unittest/working_config/CMakeLists.txt new file mode 100644 index 0000000..d701aa7 --- /dev/null +++ b/src/package_config_unittest/working_config/CMakeLists.txt @@ -0,0 +1,8 @@ +cmake_minimum_required (VERSION 3.1) +project (glog_package_config) + +find_package (glog REQUIRED NO_MODULE) + +add_executable (glog_package_config glog_package_config.cc) + +target_link_libraries (glog_package_config PRIVATE glog::glog) diff --git a/src/package_config_unittest/working_config/glog_package_config.cc b/src/package_config_unittest/working_config/glog_package_config.cc new file mode 100644 index 0000000..b7b5cf6 --- /dev/null +++ b/src/package_config_unittest/working_config/glog_package_config.cc @@ -0,0 +1,6 @@ +#include + +int main(int /*argc*/, char** argv) +{ + google::InitGoogleLogging(argv[0]); +}