Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[A] Add target export in cmake. #42

Merged
merged 5 commits into from
Feb 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 ${PROJECT_NAME}-targets.cmake
)

1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
### Internal

- Refactoring of code that uses vectors.
- Add target export in cmake.

### Docs

Expand Down
5 changes: 5 additions & 0 deletions cmake/libfort-config.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
if(NOT TARGET @PROJECT_NAME@::fort)
include("${CMAKE_CURRENT_LIST_DIR}/@[email protected]")
endif()

set(@PROJECT_NAME@_LIBRARIES @PROJECT_NAME@::fort)
44 changes: 38 additions & 6 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ add_library(fort

target_include_directories(fort
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)

set_target_properties(fort
Expand All @@ -30,14 +31,45 @@ endif()

include(GNUInstallDirs)
install(
TARGETS
fort
TARGETS fort
EXPORT ${PROJECT_NAME}-targets
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
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
)

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(
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}
)

16 changes: 16 additions & 0 deletions tests/package_tests/1-simple_table.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <iostream>

#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;
}
23 changes: 23 additions & 0 deletions tests/package_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)