From daf74ae0b11d4e6517151038b32aa0f4b731a3d3 Mon Sep 17 00:00:00 2001 From: Mario Werner Date: Mon, 11 Jan 2016 19:01:49 +0100 Subject: [PATCH 1/5] upgrades to cmake 2.8.12 and adds config and export support This commit upgrades cmake to 2.8.12 to implement proper cmake `find_package` support using config and export file generation. Having this support enables users to use installed cppformat with a simple `find_package` call. Directly using a version from a build directory is also supported. main.cpp: ``` #include int main(int argc, char** argv) { for(int i = 0; i < argc; ++i) fmt::print("{}: {}\n",i,argv[i]); return 0; } ``` CMakeLists.txt: ``` cmake_minimum_required(VERSION 2.8.12) project(cppformat-test) find_package(cppformat REQUIRED) add_executable(cppformat-test "main.cpp") target_link_libraries(cppformat-test cppformat) ``` Configuring when cppformat is installed under `CMAKE_INSTALL_PREFIX`: `cmake ` Configuring when cppformat is installed `ELSEWHERE`: `cmake -Dcppformat_DIR=/lib/cmake/cppformat ` Configuring when cppformat is only built: `cmake -Dcppformat_DIR= ` --- CMakeLists.txt | 45 +++++++++++++++++++++++--- support/cmake/cppformatConfig.cmake.in | 4 +++ 2 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 support/cmake/cppformatConfig.cmake.in diff --git a/CMakeLists.txt b/CMakeLists.txt index 59b4e8dc..5ea8e060 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ message(STATUS "CMake version: ${CMAKE_VERSION}") -cmake_minimum_required(VERSION 2.6) +cmake_minimum_required(VERSION 2.8.12) # Set the default CMAKE_BUILD_TYPE to Release. # This should be done before the project command since the latter can set @@ -163,8 +163,45 @@ endif () # Install targets. if (FMT_INSTALL) - set(FMT_LIB_DIR lib CACHE STRING + include(CMakePackageConfigHelpers) + set(config_install_dir "lib/cmake/cppformat") + set(version_config "${CMAKE_CURRENT_BINARY_DIR}/cppformatConfigVersion.cmake") + set(project_config "${CMAKE_CURRENT_BINARY_DIR}/cppformatConfig.cmake") + set(targets_export_name "cppformatTargets") + + set(FMT_LIB_DIR "lib" CACHE STRING "Installation directory for libraries, relative to ${CMAKE_INSTALL_PREFIX}.") - install(TARGETS cppformat DESTINATION ${FMT_LIB_DIR}) - install(FILES format.h DESTINATION include/cppformat) + + # copy the header into the build directory to mimic the installed tree + configure_file("format.h" "cppformat/format.h" COPYONLY) + # add the include directories for both build and install tree + target_include_directories( + cppformat PUBLIC + $ + $ + ) + + # generate the version, config and target files into the build directory + write_basic_package_version_file( + "${version_config}" + VERSION ${CPPFORMAT_VERSION} + COMPATIBILITY AnyNewerVersion + ) + configure_package_config_file( + "support/cmake/cppformatConfig.cmake.in" + "${project_config}" + INSTALL_DESTINATION "${config_install_dir}" + ) + export(TARGETS cppformat FILE "${targets_export_name}.cmake") + + # install version, config and target files + install( + FILES "${project_config}" "${version_config}" + DESTINATION "${config_install_dir}" + ) + install(EXPORT "${targets_export_name}" DESTINATION "${config_install_dir}") + + # install the library and the include file + install(TARGETS cppformat EXPORT "${targets_export_name}" DESTINATION "${FMT_LIB_DIR}") + install(FILES format.h DESTINATION "include/cppformat") endif () diff --git a/support/cmake/cppformatConfig.cmake.in b/support/cmake/cppformatConfig.cmake.in new file mode 100644 index 00000000..5ab5615c --- /dev/null +++ b/support/cmake/cppformatConfig.cmake.in @@ -0,0 +1,4 @@ +@PACKAGE_INIT@ + +include("${CMAKE_CURRENT_LIST_DIR}/@targets_export_name@.cmake") +check_required_components("cppformat") From b732455fd3d72c4ac3fbeed245f7bf6ed9cda778 Mon Sep 17 00:00:00 2001 From: Mario Werner Date: Tue, 12 Jan 2016 12:33:45 +0100 Subject: [PATCH 2/5] enable package support for out of source builds --- CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5ea8e060..40ed2e9e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -142,7 +142,7 @@ endif () set_target_properties(cppformat PROPERTIES VERSION ${CPPFORMAT_VERSION} SOVERSION ${CPACK_PACKAGE_VERSION_MAJOR}) -if (EXISTS .gitignore) +if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/.gitignore") # Get the list of ignored files from .gitignore. file (STRINGS ".gitignore" lines) LIST(REMOVE_ITEM lines /doc/html) @@ -156,7 +156,7 @@ if (EXISTS .gitignore) set(CPACK_SOURCE_GENERATOR ZIP) set(CPACK_SOURCE_IGNORE_FILES ${ignored_files}) - set(CPACK_SOURCE_PACKAGE_FILE_NAME cppformat-${CPPFORMAT_VERSION}) + set(CPACK_PACKAGE_NAME "cppformat") set(CPACK_RESOURCE_FILE_README ${FORMAT_SOURCE_DIR}/README.rst) include(CPack) endif () From 3fc3ecd1840a49abe3708eaa084a30a0f111fd94 Mon Sep 17 00:00:00 2001 From: Mario Werner Date: Tue, 12 Jan 2016 16:43:36 +0100 Subject: [PATCH 3/5] reverted removal of `CPACK_SOURCE_PACKAGE_FILE_NAME` The use of `CPACK_PACKAGE_NAME` leads to `cppformat--Source.zip` as the name for the source package which is different from the expected `cppformat-.zip`. --- CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 40ed2e9e..64d095d6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -156,6 +156,7 @@ if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/.gitignore") set(CPACK_SOURCE_GENERATOR ZIP) set(CPACK_SOURCE_IGNORE_FILES ${ignored_files}) + set(CPACK_SOURCE_PACKAGE_FILE_NAME "cppformat-${CPPFORMAT_VERSION}") set(CPACK_PACKAGE_NAME "cppformat") set(CPACK_RESOURCE_FILE_README ${FORMAT_SOURCE_DIR}/README.rst) include(CPack) From 891e9117f6c26b6a8b7e2ffbe064ec594eaa41f0 Mon Sep 17 00:00:00 2001 From: Mario Werner Date: Tue, 12 Jan 2016 16:53:09 +0100 Subject: [PATCH 4/5] trying to update cmake to 2.8.12 in travis via a ppa repo Whitelist of repos: https://github.com/travis-ci/apt-source-whitelist/blob/master/ubuntu.json Whitelist of packages: https://github.com/travis-ci/apt-package-whitelist/blob/master/ubuntu-precise --- .travis.yml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/.travis.yml b/.travis.yml index f0fbd22d..cbff7e59 100644 --- a/.travis.yml +++ b/.travis.yml @@ -20,5 +20,13 @@ matrix: - os: osx env: BUILD=Doc +addons: + apt: + sources: + - kubuntu-backports # cmake 2.8.12 +# - george-edison55-precise-backports # cmake 3.2.3 + packages: + - cmake + script: - support/travis-build.py From ef7bbfff87fdf68f530c4fb06858f24f4b595060 Mon Sep 17 00:00:00 2001 From: Mario Werner Date: Wed, 13 Jan 2016 09:54:02 +0100 Subject: [PATCH 5/5] removed workaround for cmake versions prior to 2.8.10 --- test/CMakeLists.txt | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index a612795e..79787dd0 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -10,12 +10,6 @@ function (target_link_cppformat target) endif () endfunction () -function (fmt_target_include_directories) - if (CMAKE_MAJOR_VERSION VERSION_GREATER 2.8.10) - target_include_directories(${ARGN}) - endif () -endfunction () - # We compile Google Test ourselves instead of using pre-compiled libraries. # See the Google Test FAQ "Why is it not recommended to install a # pre-compiled copy of Google Test (for example, into /usr/local)?" @@ -24,7 +18,7 @@ endfunction () add_library(gmock STATIC ${FMT_GMOCK_DIR}/gmock-gtest-all.cc ${FMT_GMOCK_DIR}/gmock/gmock.h ${FMT_GMOCK_DIR}/gtest/gtest.h ${FMT_GMOCK_DIR}/gtest/gtest-spi.h) -fmt_target_include_directories(gmock INTERFACE ${FMT_GMOCK_DIR}) +target_include_directories(gmock INTERFACE ${FMT_GMOCK_DIR}) find_package(Threads) if (Threads_FOUND) target_link_libraries(gmock ${CMAKE_THREAD_LIBS_INIT}) @@ -43,7 +37,7 @@ check_cxx_source_compiles(" check_cxx_source_compiles(" #include int main() {}" FMT_INITIALIZER_LIST) - + if (NOT FMT_VARIADIC_TEMPLATES OR NOT FMT_INITIALIZER_LIST) add_definitions(-DGTEST_LANG_CXX11=0) endif ()