diff options
Diffstat (limited to 'cmake')
-rw-r--r-- | cmake/DetermineGflagsNamespace.cmake | 69 | ||||
-rw-r--r-- | cmake/FindUnwind.cmake | 78 | ||||
-rw-r--r-- | cmake/GetCacheVariables.cmake | 63 | ||||
-rw-r--r-- | cmake/INSTALL.md | 81 | ||||
-rw-r--r-- | cmake/TestInitPackageConfig.cmake | 12 | ||||
-rw-r--r-- | cmake/TestPackageConfig.cmake | 33 |
6 files changed, 336 insertions, 0 deletions
diff --git a/cmake/DetermineGflagsNamespace.cmake b/cmake/DetermineGflagsNamespace.cmake new file mode 100644 index 000000000000..3dde42b4b332 --- /dev/null +++ b/cmake/DetermineGflagsNamespace.cmake @@ -0,0 +1,69 @@ +macro(determine_gflags_namespace VARIABLE) + if (NOT DEFINED "${VARIABLE}") + if (CMAKE_REQUIRED_INCLUDES) + set (CHECK_INCLUDE_FILE_CXX_INCLUDE_DIRS "-DINCLUDE_DIRECTORIES=${CMAKE_REQUIRED_INCLUDES}") + else () + set (CHECK_INCLUDE_FILE_CXX_INCLUDE_DIRS) + endif () + + set(MACRO_CHECK_INCLUDE_FILE_FLAGS ${CMAKE_REQUIRED_FLAGS}) + + set(_NAMESPACES gflags google) + set(_check_code +" +#include <gflags/gflags.h> + +int main(int argc, char**argv) +{ + GLOG_GFLAGS_NAMESPACE::ParseCommandLineFlags(&argc, &argv, true); +} +") + if (NOT CMAKE_REQUIRED_QUIET) + message (STATUS "Looking for gflags namespace") + endif () + if (${ARGC} EQUAL 3) + set (CMAKE_CXX_FLAGS_SAVE ${CMAKE_CXX_FLAGS}) + set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${ARGV2}") + endif () + + set (_check_file + ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/DetermineGflagsNamespace.cxx) + + foreach (_namespace ${_NAMESPACES}) + file (WRITE "${_check_file}" "${_check_code}") + try_compile (${VARIABLE} + "${CMAKE_BINARY_DIR}" "${_check_file}" + COMPILE_DEFINITIONS "${CMAKE_REQUIRED_DEFINITIONS}" -DGLOG_GFLAGS_NAMESPACE=${_namespace} + LINK_LIBRARIES gflags + CMAKE_FLAGS -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} + OUTPUT_VARIABLE OUTPUT) + + if (${VARIABLE}) + set (${VARIABLE} ${_namespace} CACHE INTERNAL "gflags namespace" FORCE) + break () + else () + file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log + "Determining the gflags namespace ${_namespace} failed with the following output:\n" + "${OUTPUT}\n\n") + endif () + endforeach (_namespace) + + if (${ARGC} EQUAL 3) + set (CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS_SAVE}) + endif () + + if (${VARIABLE}) + if (NOT CMAKE_REQUIRED_QUIET) + message (STATUS "Looking for gflags namespace - ${${VARIABLE}}") + endif () + file (APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log + "Determining the gflags namespace passed with the following output:\n" + "${OUTPUT}\n\n") + else () + if (NOT CMAKE_REQUIRED_QUIET) + message (STATUS "Looking for gflags namespace - failed") + endif () + set (${VARIABLE} ${_namespace} CACHE INTERNAL "gflags namespace") + endif () + endif () +endmacro () diff --git a/cmake/FindUnwind.cmake b/cmake/FindUnwind.cmake new file mode 100644 index 000000000000..8941bb0dacca --- /dev/null +++ b/cmake/FindUnwind.cmake @@ -0,0 +1,78 @@ +# - 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) + if (NOT TARGET unwind::unwind) + 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 (NOT TARGET unwind::unwind) +endif (Unwind_FOUND) diff --git a/cmake/GetCacheVariables.cmake b/cmake/GetCacheVariables.cmake new file mode 100644 index 000000000000..20485a8c8f8f --- /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/INSTALL.md b/cmake/INSTALL.md new file mode 100644 index 000000000000..1377d652ed09 --- /dev/null +++ b/cmake/INSTALL.md @@ -0,0 +1,81 @@ +# Glog - CMake Support + +Glog comes with a CMake build script ([CMakeLists.txt](../CMakeLists.txt)) that can be used on a wide range of platforms. +If you don't have CMake installed already, you can download it for free from <http://www.cmake.org/>. + +CMake works by generating native makefiles or build projects that can be used in the compiler environment of your choice. +You can either build Glog with CMake as a standalone project or it can be incorporated into an existing CMake build for another project. + +## Table of Contents + +- [Building Glog with CMake](#building-glog-with-cmake) +- [Consuming Glog in a CMake Project](#consuming-glog-in-a-cmake-project) +- [Incorporating Glog into a CMake Project](#incorporating-glog-into-a-cmake-project) + +## Building Glog with CMake + +When building Glog as a standalone project, on Unix-like systems with GNU Make as build tool, the typical workflow is: + +1. Get the source code and change to it. +e.g. cloning with git: +```bash +git clone git@github.com:google/glog.git +cd glog +``` + +2. Run CMake to configure the build tree. +```bash +cmake -H. -Bbuild -G "Unix Makefiles" +``` +note: To get the list of available generators (e.g. Visual Studio), use `-G ""` + +3. Afterwards, generated files can be used to compile the project. +```bash +cmake --build build +``` + +4. Test the build software (optional). +```bash +cmake --build build --target test +``` + +5. Install the built files (optional). +```bash +cmake --build build --target install +``` + +## Consuming Glog in a CMake Project + +If you have Glog installed in your system, you can use the CMake command +`find_package()` to include it in your CMake Project. + +```cmake +cmake_minimum_required(VERSION 3.0.2) +project(myproj VERSION 1.0) + +find_package(glog 0.4.0 REQUIRED) + +add_executable(myapp main.cpp) +target_link_libraries(myapp glog::glog) +``` + +Compile definitions and options will be added automatically to your target as +needed. + +## Incorporating Glog into a CMake Project + +You can also use the CMake command `add_subdirectory()` to include Glog directly from a subdirectory of your project. +The **glog::glog** target is in this case an ALIAS library target for the **glog** library target. + +```cmake +cmake_minimum_required(VERSION 3.0.2) +project(myproj VERSION 1.0) + +add_subdirectory(glog) + +add_executable(myapp main.cpp) +target_link_libraries(myapp glog::glog) +``` + +Again, compile definitions and options will be added automatically to your target as +needed. diff --git a/cmake/TestInitPackageConfig.cmake b/cmake/TestInitPackageConfig.cmake new file mode 100644 index 000000000000..6cb11ccf2977 --- /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 000000000000..191c3cac2351 --- /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) |