From 683547ec079ce640ce3a6b59117b1a15cc2696df Mon Sep 17 00:00:00 2001 From: Sergiu Deitsch Date: Sat, 2 Nov 2019 00:09:32 +0100 Subject: [PATCH] install unwind find module alongside glog --- CMakeLists.txt | 112 ++++++++++++++++++++++++++++++++++++------ glog-config.cmake.in | 4 +- glog-modules.cmake.in | 18 +++++++ 3 files changed, 116 insertions(+), 18 deletions(-) create mode 100644 glog-modules.cmake.in diff --git a/CMakeLists.txt b/CMakeLists.txt index 75b7df2f0..613c97304 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,12 @@ 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) +endif (Unwind_FOUND) check_include_file (dlfcn.h HAVE_DLFCN_H) check_include_file (execinfo.h HAVE_EXECINFO_H) @@ -117,8 +129,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 +490,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)") +endif (Unwind_FOUND) if (HAVE_DBGHELP) target_link_libraries (glog PUBLIC dbghelp) @@ -522,11 +573,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 +744,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 a7b3cdbdc..acd21f085 100644 --- a/glog-config.cmake.in +++ b/glog-config.cmake.in @@ -7,8 +7,6 @@ endif (CMAKE_VERSION VERSION_LESS @glog_CMake_VERSION@) include (CMakeFindDependencyMacro) @gflags_DEPENDENCY@ - -list (APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}") -find_dependency (Libunwind) +@Unwind_DEPENDENCY@ 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 000000000..71c516078 --- /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)