From c22a518766884d9f4d36dce9c2c6dde18acb87e3 Mon Sep 17 00:00:00 2001 From: seleznevae Date: Sun, 9 Feb 2020 13:02:25 +0300 Subject: [PATCH 1/5] [A] Add target export in cmake. --- ChangeLog.md | 1 + lib/CMakeLists.txt | 24 ++++++++++++++++++------ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/ChangeLog.md b/ChangeLog.md index 992046d..21595de 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -15,6 +15,7 @@ ### Internal - Refactoring of code that uses vectors. +- Add target export in cmake. ### Docs diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index e065284..837ed5f 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -6,7 +6,8 @@ add_library(fort target_include_directories(fort PUBLIC - ${CMAKE_CURRENT_SOURCE_DIR} + $ + $ ) set_target_properties(fort @@ -30,14 +31,25 @@ endif() include(GNUInstallDirs) install( - TARGETS - fort + TARGETS fort + EXPORT LibFort LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} ) - -install(FILES fort.h fort.hpp DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) +install( + FILES fort.h fort.hpp + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} +) configure_file(libfort.pc.in libfort.pc @ONLY) -install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libfort.pc DESTINATION lib/pkgconfig) +install( + FILES ${CMAKE_CURRENT_BINARY_DIR}/libfort.pc + DESTINATION lib/pkgconfig +) + +install( + EXPORT LibFort + NAMESPACE LibFort:: + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME} +) From 3f64ceba682f62d1e1814ce90125cb0b5125c1f7 Mon Sep 17 00:00:00 2001 From: seleznevae Date: Mon, 10 Feb 2020 23:00:47 +0300 Subject: [PATCH 2/5] [A] Add cmake package files to installation phase --- CMakeLists.txt | 9 +++++++++ cmake/libfort-config.cmake.in | 5 +++++ lib/CMakeLists.txt | 32 +++++++++++++++++++++++++++++--- 3 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 cmake/libfort-config.cmake.in diff --git a/CMakeLists.txt b/CMakeLists.txt index 4103408..3492cbf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -260,3 +260,12 @@ list(APPEND ${PROJECT_NAME}_ctests foreach(exe ${${PROJECT_NAME}_ctests}) add_test(NAME ${exe} COMMAND ${exe}) endforeach() + +# ------------------------------------------------------------------------------ +# Exported targets for outer applications. +# ------------------------------------------------------------------------------ +export( + TARGETS fort + FILE libfort-exports.cmake +) + diff --git a/cmake/libfort-config.cmake.in b/cmake/libfort-config.cmake.in new file mode 100644 index 0000000..5e76e5d --- /dev/null +++ b/cmake/libfort-config.cmake.in @@ -0,0 +1,5 @@ +if(NOT TARGET @PROJECT_NAME@::fort) + include("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@-targets.cmake") +endif() + +set(@PROJECT_NAME@_LIBRARIES @PROJECT_NAME@::fort) diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 837ed5f..f8d1931 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -32,7 +32,8 @@ endif() include(GNUInstallDirs) install( TARGETS fort - EXPORT LibFort + EXPORT ${PROJECT_NAME}-targets + INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} @@ -48,8 +49,33 @@ install( DESTINATION lib/pkgconfig ) +include(CMakePackageConfigHelpers) +configure_package_config_file( + ${CMAKE_CURRENT_SOURCE_DIR}/../cmake/${PROJECT_NAME}-config.cmake.in + ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-config.cmake + INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" +) +write_basic_package_version_file( + ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-config-version.cmake + COMPATIBILITY SameMajorVersion +) install( - EXPORT LibFort - NAMESPACE LibFort:: + FILES + ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-config.cmake + ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-config-version.cmake DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME} ) + +install( + EXPORT ${PROJECT_NAME}-targets + FILE ${PROJECT_NAME}-targets.cmake + NAMESPACE ${PROJECT_NAME}:: + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME} +) + +export( + TARGETS fort + NAMESPACE ${PROJECT_NAME}:: + FILE ${PROJECT_NAME}-exports.cmake +) + From 0411dfdc2a44bc931ed09237bb7bccec2e842646 Mon Sep 17 00:00:00 2001 From: seleznevae Date: Mon, 10 Feb 2020 23:06:05 +0300 Subject: [PATCH 3/5] [C] Chage exported name --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3492cbf..09ca662 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -266,6 +266,6 @@ endforeach() # ------------------------------------------------------------------------------ export( TARGETS fort - FILE libfort-exports.cmake + FILE ${PROJECT_NAME}-targets.cmake ) From 75638e8f22e86ccf46450e9abced76929afa502f Mon Sep 17 00:00:00 2001 From: seleznevae Date: Mon, 10 Feb 2020 23:09:46 +0300 Subject: [PATCH 4/5] [D] Deleted unused export --- lib/CMakeLists.txt | 6 ------ 1 file changed, 6 deletions(-) diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index f8d1931..0ed5b4a 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -73,9 +73,3 @@ install( DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME} ) -export( - TARGETS fort - NAMESPACE ${PROJECT_NAME}:: - FILE ${PROJECT_NAME}-exports.cmake -) - From d09633df554cac678e93a46030ed1100c65c8213 Mon Sep 17 00:00:00 2001 From: seleznevae Date: Wed, 12 Feb 2020 22:41:20 +0300 Subject: [PATCH 5/5] [A] Add package installation test --- .travis.yml | 14 ++++++++++++++ CMakeLists.txt | 2 +- tests/package_tests/1-simple_table.cpp | 16 ++++++++++++++++ tests/package_tests/CMakeLists.txt | 23 +++++++++++++++++++++++ 4 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 tests/package_tests/1-simple_table.cpp create mode 100644 tests/package_tests/CMakeLists.txt diff --git a/.travis.yml b/.travis.yml index c827e42..e6cbb00 100644 --- a/.travis.yml +++ b/.travis.yml @@ -255,6 +255,20 @@ script: # scan-build make ; # fi + # Test package installation + - cd .. + - rm -r build/* + - cmake -H. -Bbuild -DCMAKE_C_STANDARD=99 -DCMAKE_CXX_STANDARD=11 -DCMAKE_INSTALL_PREFIX=`pwd`/install + - cmake --build build --target all + - cmake --build build -- install + - ls `pwd`/install + - cd tests/package_tests + - cmake -H. -B_build -DCMAKE_PREFIX_PATH=../../install -DCMAKE_C_STANDARD=99 -DCMAKE_CXX_STANDARD=11 + - cmake --build _build -- -j3 + - ./_build/app + - rm -rf _build + - cd ../../build + # Build for coveralls (should be the last) - | if [ "${CC}" = 'gcc' ]; then diff --git a/CMakeLists.txt b/CMakeLists.txt index 09ca662..a5ba808 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -265,7 +265,7 @@ endforeach() # Exported targets for outer applications. # ------------------------------------------------------------------------------ export( - TARGETS fort + TARGETS fort FILE ${PROJECT_NAME}-targets.cmake ) diff --git a/tests/package_tests/1-simple_table.cpp b/tests/package_tests/1-simple_table.cpp new file mode 100644 index 0000000..6cd3cd9 --- /dev/null +++ b/tests/package_tests/1-simple_table.cpp @@ -0,0 +1,16 @@ +#include + +#include "fort.hpp" + + +int main() +{ + fort::char_table table; + table << fort::header + << "N" << "Driver" << "Time" << "Avg Speed" << fort::endr + << "1" << "Ricciardo" << "1:25.945" << "47.362" << fort::endr + << "2" << "Hamilton" << "1:26.373" << "35.02" << fort::endr + << "3" << "Verstappen" << "1:26.469" << "29.78" << fort::endr; + + std::cout << table.to_string() << std::endl; +} diff --git a/tests/package_tests/CMakeLists.txt b/tests/package_tests/CMakeLists.txt new file mode 100644 index 0000000..cc9fd01 --- /dev/null +++ b/tests/package_tests/CMakeLists.txt @@ -0,0 +1,23 @@ +cmake_minimum_required(VERSION 3.0) + +find_package(libfort) + +# Assert exports of libfort +if (NOT libfort_FOUND) + message(FATAL_ERROR "libfort was not found") +endif() +if (NOT TARGET libfort::fort) + message(FATAL_ERROR "TARGET libfort::fort not found") +endif() +if (NOT DEFINED libfort_LIBRARIES) + message(FATAL_ERROR "libfort_LIBRARIES are not defined") +endif() +if (NOT ${libfort_VERSION} EQUAL "0.4.0") + message(FATAL_ERROR "libfort_VERSION is incorrect") +endif() + +add_executable(app + 1-simple_table.cpp) + +target_link_libraries(app + libfort::fort)