Skip to content

Commit

Permalink
Cmake target (#55)
Browse files Browse the repository at this point in the history
* Added and exported CMake target

- Main CMake file now has a target for the library.
- Main library target has nlohmann_json as a dependency via find_package.
- 'cmake --build . --target install' can now be ran, which neatly installs the library and produces config and target files for other targets to consume.

* Changed test CMakeLists.txt to link against targets instead of including

- All the test targets now properly link against library targets for nlohmann_json, Catch2 and fx-gltf.
- Catch2 and nlohmann_json are found via find_package instead of being pulled from the thirdparty folder.

* Removed third party libraries from repo

These files are no longer needed in the repository, as they are now found via find_package in CMake. This requires that the user has them installed on their system and that CMake knows where to look for them, but using find_package is the proper way of referencing external libraries in CMake.

* Revert "Removed third party libraries from repo"

This reverts commit 81a8a30.

* Added options in CMake files for installation and using installed deps

* Made target_link_libraries signatures consistent in test CMakeLists.txt
  • Loading branch information
Ansoulom authored and jessey-git committed May 6, 2019
1 parent 85ac827 commit 7568a3b
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ install:
- git submodule update --init --recursive

build_script:
- cmake . -G"Visual Studio 15 2017 Win64" -DTEST_LanguageStandard="%language_standard%"
- cmake . -G"Visual Studio 15 2017 Win64" -DFX_GLTF_INSTALL=OFF -DTEST_LanguageStandard="%language_standard%"
- cmake --build . --config Release

test_script:
Expand Down
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ script:

# compile and execute unit tests
- mkdir -p build && cd build
- cmake .. ${CMAKE_OPTIONS} -DTEST_LanguageStandard=${TEST_LANGUAGE_STANDARD} -GNinja
- cmake .. ${CMAKE_OPTIONS} -DFX_GLTF_INSTALL=OFF -DTEST_LanguageStandard=${TEST_LANGUAGE_STANDARD} -GNinja
- cmake --build . --config Release
- ctest -C Release -V -j
- cd ..
90 changes: 89 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,45 @@
cmake_minimum_required (VERSION 3.8)
project(fx-gltf VERSION 1.0.1)

option(FX_GLTF_INSTALL "If the library should be installed" ON)
option(FX_GLTF_USE_INSTALLED_DEPS "If installed or repo local dependencies should be used" OFF)

# Force use of installed deps if installing
if(FX_GLTF_INSTALL)
set(FX_GLTF_USE_INSTALLED_DEPS ON)
endif()

##
## Config
##
set(FX_GLTF_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/include)
set(FX_GLTF_LIB_TARGET_NAME ${PROJECT_NAME})
set(FX_GLTF_TARGETS_EXPORT_NAME ${PROJECT_NAME}Targets)
set(FX_GLTF_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include/)
set(FX_GLTF_INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_PREFIX}/include)
set(FX_GLTF_INSTALL_CONFIG_DIR ${CMAKE_INSTALL_PREFIX}/lib/cmake/${PROJECT_NAME})
set(FX_GLTF_LICENSE_FILE ${CMAKE_CURRENT_SOURCE_DIR}/LICENSE)
set(FX_GLTF_TARGETS_FILE ${PROJECT_NAME}Targets.cmake)
set(FX_GLTF_CONFIG_FILE ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake)
set(FX_GLTF_CONFIG_VERSION_FILE ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake)
set(FX_GLTF_CONFIG_TEMPLATE_FILE ${CMAKE_CURRENT_SOURCE_DIR}/cmake/${PROJECT_NAME}Config.cmake.in)

##
## Library target
##
add_library(${FX_GLTF_LIB_TARGET_NAME} INTERFACE)
add_library(${PROJECT_NAME}::${FX_GLTF_LIB_TARGET_NAME} ALIAS ${FX_GLTF_LIB_TARGET_NAME})
target_include_directories(${FX_GLTF_LIB_TARGET_NAME}
INTERFACE
$<INSTALL_INTERFACE:include>
$<BUILD_INTERFACE:${FX_GLTF_INCLUDE_DIR}>
)
target_compile_features(${FX_GLTF_LIB_TARGET_NAME} INTERFACE cxx_std_14)

## Dependencies
if(FX_GLTF_USE_INSTALLED_DEPS)
find_package(nlohmann_json 3.5.0 REQUIRED)
target_link_libraries(${FX_GLTF_LIB_TARGET_NAME} INTERFACE nlohmann_json::nlohmann_json)
endif()

##
## Testing
Expand All @@ -14,3 +50,55 @@ if(BUILD_TESTING)
enable_testing()
add_subdirectory(test)
endif()

##
## Install
##
if(FX_GLTF_INSTALL)
include(CMakePackageConfigHelpers)
write_basic_package_version_file(
${FX_GLTF_CONFIG_VERSION_FILE}
VERSION ${PROJECT_VERSION}
COMPATIBILITY SameMajorVersion
)
configure_package_config_file(
${FX_GLTF_CONFIG_TEMPLATE_FILE}
${FX_GLTF_CONFIG_FILE}
INSTALL_DESTINATION ${FX_GLTF_INSTALL_CONFIG_DIR}
)

install(
TARGETS ${FX_GLTF_LIB_TARGET_NAME}
EXPORT ${FX_GLTF_TARGETS_EXPORT_NAME}
)
install(
DIRECTORY ${FX_GLTF_INCLUDE_DIR}
DESTINATION ${FX_GLTF_INSTALL_INCLUDE_DIR}
)
install(
EXPORT ${FX_GLTF_TARGETS_EXPORT_NAME}
FILE ${FX_GLTF_TARGETS_FILE}
NAMESPACE ${PROJECT_NAME}::
DESTINATION ${FX_GLTF_INSTALL_CONFIG_DIR}
)
install(
FILES
${FX_GLTF_CONFIG_FILE}
${FX_GLTF_CONFIG_VERSION_FILE}
DESTINATION ${FX_GLTF_INSTALL_CONFIG_DIR}
)
install(
FILES ${FX_GLTF_LICENSE_FILE}
DESTINATION ${CMAKE_INSTALL_PREFIX}
)

##
## Export
##
export(
EXPORT ${FX_GLTF_TARGETS_EXPORT_NAME}
FILE ${CMAKE_CURRENT_BINARY_DIR}/${FX_GLTF_TARGETS_FILE}
NAMESPACE ${PROJECT_NAME}::
)
export(PACKAGE ${PROJECT_NAME})
endif()
8 changes: 8 additions & 0 deletions cmake/fx-gltfConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
get_filename_component(fx-gltf_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
include(CMakeFindDependencyMacro)

find_dependency(nlohmann_json 3.5.0 REQUIRED)

if(NOT TARGET fx-gltf::fx-gltf)
include("${fx-gltf_CMAKE_DIR}/fx-gltfTargets.cmake")
endif()
22 changes: 18 additions & 4 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@ set(TEST_LanguageStandard "17" CACHE STRING "14 or 17")
# Catch library with the main function to speed up build
#############################################################################
add_library(catch2_main OBJECT "src/unit.cpp" "src/utility.cpp")
target_include_directories(catch2_main PRIVATE ${THIRDPARTY_INCLUDE_DIR})
target_include_directories(catch2_main PRIVATE "src")

if(FX_GLTF_USE_INSTALLED_DEPS)
find_package(Catch2 2.6.0 REQUIRED)

target_link_libraries(catch2_main PRIVATE nlohmann_json::nlohmann_json)
target_link_libraries(catch2_main PRIVATE Catch2::Catch2)
else()
target_include_directories(catch2_main PRIVATE ${THIRDPARTY_INCLUDE_DIR})
endif()

if(TEST_LanguageStandard STREQUAL "17")
target_compile_features(catch2_main PUBLIC cxx_std_17)
Expand Down Expand Up @@ -47,11 +56,16 @@ foreach(file ${files})
endif()

target_compile_definitions(${testcase} PRIVATE CATCH_CONFIG_FAST_COMPILE)
target_include_directories(${testcase} PRIVATE ${THIRDPARTY_INCLUDE_DIR})
target_include_directories(${testcase} PRIVATE ${FX_GLTF_INCLUDE_DIR})
target_link_libraries(${testcase} PRIVATE ${FX_GLTF_LIB_TARGET_NAME})
if(FX_GLTF_USE_INSTALLED_DEPS)
target_link_libraries(${testcase} PRIVATE nlohmann_json::nlohmann_json)
target_link_libraries(${testcase} PRIVATE Catch2::Catch2)
else()
target_include_directories(${testcase} PRIVATE ${THIRDPARTY_INCLUDE_DIR})
endif()

if(CMAKE_CXX_COMPILER_ID STREQUAL GNU OR CMAKE_CXX_COMPILER_ID STREQUAL Clang)
target_link_libraries(${testcase} stdc++fs)
target_link_libraries(${testcase} PRIVATE stdc++fs)
endif()

add_test(NAME "${testcase}"
Expand Down

0 comments on commit 7568a3b

Please sign in to comment.