diff --git a/CMakeLists.txt b/CMakeLists.txt index bcda6a645d099449296c808e2e80aaba55d481bb..16d575487f63dc628fad2393171ceb20240bd750 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,14 +6,7 @@ # Then use cmake from a build directory : cmake .. # Set the name of the package. cmake_minimum_required(VERSION 3.10) -project( CrestApiLib VERSION 5.0) # The name of the package - -# Create version header file (version.h) -configure_file( - "${PROJECT_SOURCE_DIR}/CrestApi/version.h.in" - "${PROJECT_BINARY_DIR}/version.h" -) - +project( CrestApiLib ) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) @@ -30,8 +23,7 @@ else() message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.") endif() -# External dependencies. -# CURL + find_package(CURL REQUIRED) if(CURL_FOUND) @@ -47,7 +39,6 @@ else() FetchContent_MakeAvailable(CURL) endif() -# BOOST find_package(Boost COMPONENTS system thread filesystem unit_test_framework regex REQUIRED) IF (Boost_FOUND) @@ -61,7 +52,6 @@ find_package(nlohmann_json 3.2.0 REQUIRED) SET(CMAKE_INCLUDE_PATH ${CMAKE_INCLUDE_PATH} ${Boost_INCLUDE_DIR}) SET(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} ../build/CrestApi) -# Set the path to the headers and sources set(header_path "./CrestApi") set(HEADERS ${header_path}/CrestApi.h ${header_path}/CrestModel.h @@ -76,21 +66,29 @@ set(HEADERS ${header_path}/CrestApi.h set(SOURCES src/CrestApi.cxx src/CrestRequest.cxx src/CrestApiFs.cxx src/CrestModel.cxx src/CrestContainer.cxx) -# Include the headers and the external libraries include_directories(CrestApi ./ ${Boost_INCLUDE_DIRS}) - -# Set the sources for the examples and tests set (SOURCES_EXAMPLE_FS doc/crest_example_fs.cxx) set (SOURCES_EXAMPLE_SRV doc/crest_example_server.cxx) set (SOURCES_EXAMPLE_FUNC doc/crest_functions_example.cxx) set (SOURCES_TEST test/test-json.cxx) set (SOURCES_BTEST test/CrestApi_test.cxx) -set (SOURCES_CONTAINERTEST doc/crest_condcontainer_example.cxx) set (SOURCES_JSON_TEST test/json_parse.cxx) +if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) + set(CMAKE_INSTALL_PREFIX ${CMAKE_CURRENT_SOURCE_DIR}/../installed) +endif() + # Component(s) in the package. +set(include_dest "include/CrestApi") + add_library(CrestApiLib SHARED ${SOURCES} ${HEADERS}) -target_link_libraries(CrestApiLib ${CURL_LIBRARIES} ${Boost_LIBRARIES}) +target_link_libraries(CrestApiLib ${CURL_LIBRARIES} ${Boost_LIBRARIES} nlohmann_json::nlohmann_json) +target_include_directories(CrestApiLib PUBLIC + $<BUILD_INTERFACE:${headers_path}> # for headers when building + $<INSTALL_INTERFACE:${include_dest}> # for client in install mode + ) +install(TARGETS CrestApiLib EXPORT CrestApiLib DESTINATION lib) +install(EXPORT CrestApiLib DESTINATION lib) add_executable(crest_example_server ${SOURCES_EXAMPLE_SRV} ${HEADERS}) target_link_libraries(crest_example_server CrestApiLib stdc++) @@ -107,55 +105,19 @@ add_executable(json_parse ${SOURCES_JSON_TEST} ${HEADERS}) target_link_libraries(json_parse CrestApiLib stdc++) add_executable(crestapi-test ${SOURCES_BTEST} ${HEADERS}) target_link_libraries(crestapi-test CrestApiLib ${Boost_LIBRARIES} stdc++) -add_executable(crestapi-crestcontainer-test ${SOURCES_CONTAINERTEST} ${HEADERS}) -target_link_libraries(crestapi-crestcontainer-test CrestApiLib ${Boost_LIBRARIES} stdc++) # Register the test with ctest enable_testing() add_test(NAME crestapi-test COMMAND crestapi-test) -# Set the properties for the library installation (version, soversion, output name) -set_target_properties(CrestApiLib PROPERTIES - VERSION ${PROJECT_VERSION} # Full version (used in file name for shared libraries) - ## SOVERSION ${PROJECT_VERSION_MAJOR} # ABI version (used for symbolic link) - OUTPUT_NAME "CrestApiLib" # Name with version -) -set(PACKAGE_INCLUDE_INSTALL_DIR "include/CrestApi-${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}") - +# Generate the Config.cmake file include(CMakePackageConfigHelpers) +# Note: INSTALL_DESTINATION is the relative +configure_package_config_file(CrestApiLibConfig.cmake.in CrestApiLibConfig.cmake + INSTALL_DESTINATION lib/cmake/CrestApiLib) + +install(FILES ${HEADERS} DESTINATION "${include_dest}") +install(FILES "${CMAKE_CURRENT_BINARY_DIR}/CrestApiLibConfig.cmake" + DESTINATION lib/cmake/CrestApiLib) + -# Generate the Config.cmake file -configure_file(CrestApiLibConfig.cmake.in CrestApiLibConfig.cmake @ONLY) - -write_basic_package_version_file( - "${PROJECT_BINARY_DIR}/CrestApiLibConfigVersion.cmake" - VERSION ${PROJECT_VERSION} - COMPATIBILITY AnyNewerVersion -) - -# Set the version for the library -# Install the library to a versioned directory like lib/1.2.3/ - -install(DIRECTORY ${PROJECT_SOURCE_DIR}/CrestApi - DESTINATION include/CrestApi-${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR} -) -# Install the library and headers -install(TARGETS CrestApiLib - LIBRARY DESTINATION lib/CrestApiLib/${PROJECT_VERSION} - ARCHIVE DESTINATION lib/CrestApiLib/${PROJECT_VERSION} -) -install(FILES "${PROJECT_BINARY_DIR}/version.h" DESTINATION include/CrestApi-${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}/CrestApi) - -# Create the config file -configure_package_config_file( - "${CMAKE_CURRENT_SOURCE_DIR}/CrestApiLibConfig.cmake.in" - "${CMAKE_CURRENT_BINARY_DIR}/CrestApiLibConfig.cmake" - INSTALL_DESTINATION lib/cmake/CrestApiLib - PATH_VARS PROJECT_VERSION -) -# Install the config and version files -install(FILES - "${CMAKE_CURRENT_BINARY_DIR}/CrestApiLibConfig.cmake" - "${CMAKE_CURRENT_BINARY_DIR}/CrestApiLibConfigVersion.cmake" - DESTINATION lib/cmake/CrestApiLib -) diff --git a/CrestApiLibConfig.cmake.in b/CrestApiLibConfig.cmake.in index 7f64c03c795d134ff85589464afe8703e3cf2243..ab10bfd2e08e1841fc343dfda99d07ac8c56d8cc 100644 --- a/CrestApiLibConfig.cmake.in +++ b/CrestApiLibConfig.cmake.in @@ -1,15 +1,31 @@ + +# Avoid hard-coding absolute paths +# This calculates ${PACKAGE_PREFIX_DIR} at run-time based on the +# relative locations of the CMAKE_INSTALL_PREFIX and this config file +# after installation @PACKAGE_INIT@ -include(CMakeFindDependencyMacro) -include(FindPackageHandleStandardArgs) +# - Check if the user provided a path to CrestApiLib_INCLUDE_DIRS. +# If not, set it to the correct include directory. +if(NOT CrestApiLib_INCLUDE_DIRS) + set(CrestApiLib_INCLUDE_DIRS "${PACKAGE_PREFIX_DIR}/include") +endif() -# Set the include directories for the project -set(CrestApiLib_INCLUDE_DIRS "@CMAKE_INSTALL_PREFIX@/include/CrestApi-@PROJECT_VERSION@") +# Create a CrestApiLib::CrestApiLib target to be used by consumers of CrestApiLib +add_library(CrestApiLib::CrestApiLib SHARED IMPORTED) +set_target_properties(CrestApiLib::CrestApiLib PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES "${CrestApiLib_INCLUDE_DIRS}" + IMPORTED_LOCATION "${PACKAGE_PREFIX_DIR}/lib/libCrestApiLib.so" +) -# Set the library directories and targets -set(CrestApiLib_LIBRARIES "@CMAKE_INSTALL_PREFIX@/lib/CrestApiLib/@PROJECT_VERSION@/${CMAKE_SHARED_LIBRARY_PREFIX}CrestApiLib${CMAKE_SHARED_LIBRARY_SUFFIX}") +# Set variables to help consumers locate CrestApiLib library and headers +set(CrestApiLib_FOUND TRUE) +set(CrestApiLib_LIBRARIES CrestApiLib::CrestApiLib) -message(STATUS "Found CrestApiLib includes and lib : ${CrestApiLib_INCLUDE_DIRS} ${CrestApiLib_LIBRARIES}") +# Provide configuration results +include(CMakeFindDependencyMacro) +if(NOT TARGET CURL::CURL) # This condition is optional and can be modified based on your dependencies + find_dependency(CURL REQUIRED) +endif() -# Provide the directory for includes -include_directories(${CrestApiLib_INCLUDE_DIRS}) +include("${CMAKE_CURRENT_LIST_DIR}/../../CrestApiLib.cmake")