From 9820816d06abda3175cecf17332d0abb81a72597 Mon Sep 17 00:00:00 2001 From: Erki Date: Sun, 28 Mar 2021 18:18:27 +0300 Subject: [PATCH] Make the project use cmake components --- CMakeLists.txt | 47 ++++++++++++++++++++++--- Messaging/CMakeLists.txt | 7 ++-- Messaging/messaging-config.cmake.in | 2 ++ Peripherals/CMakeLists.txt | 21 +++++++++++ Peripherals/peripherals-config.cmake.in | 2 ++ Tests/CMakeLists.txt | 2 +- Utility/CMakeLists.txt | 16 +++++++-- Utility/utility-config.cmake.in | 2 ++ cmake/skullc-install.cmake | 44 +++++++++++++++++++++++ skullc-config.cmake | 3 ++ 10 files changed, 136 insertions(+), 10 deletions(-) create mode 100644 Messaging/messaging-config.cmake.in create mode 100644 Peripherals/CMakeLists.txt create mode 100644 Peripherals/peripherals-config.cmake.in create mode 100644 Utility/utility-config.cmake.in create mode 100644 cmake/skullc-install.cmake create mode 100644 skullc-config.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 8d56ed3..45946b7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,11 +1,28 @@ -cmake_minimum_required(VERSION 3.14 FATAL_ERROR) -project(SkullC) +cmake_minimum_required(VERSION 3.8 FATAL_ERROR) + +set(version 0.1.0) + +project(skullc + VERSION ${version} + LANGUAGES + C + CXX +) + +list(APPEND CMAKE_MODULE_PATH ${CMAKE_BINARY_DIR}) +list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/) -set(CMAKE_MODULE_PATH ${CMAKE_BINARY_DIR}) set(CMAKE_CXX_STANDARD 14) -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra") +list(APPEND CMAKE_CXX_FLAGS "-Wall -Wextra") option(WITH_TESTS "Enable unit testing." OFF) +option(WITH_HAL "Enable the compiling and deployment of the HAL dependent sections." OFF) + +include(skullc-install) + +if(WITH_HAL) + add_subdirectory(Peripherals) +endif() add_subdirectory(Utility) add_subdirectory(Messaging) @@ -13,4 +30,24 @@ add_subdirectory(Messaging) if(WITH_TESTS) enable_testing() add_subdirectory(Tests) -endif() \ No newline at end of file +endif() + +## Install + +configure_file(skullc-config.cmake + "${CMAKE_BINARY_DIR}/skullc-config.cmake" + @ONLY +) + +include(CMakePackageConfigHelpers) +write_basic_package_version_file( + "${CMAKE_BINARY_DIR}/skullc-config-version.cmake" + VERSION ${version} + COMPATIBILITY AnyNewerVersion +) + +install( + FILES + "${CMAKE_BINARY_DIR}/skullc-config.cmake" + DESTINATION lib/cmake/skullc +) diff --git a/Messaging/CMakeLists.txt b/Messaging/CMakeLists.txt index 11088ad..fb5e9cc 100644 --- a/Messaging/CMakeLists.txt +++ b/Messaging/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.14 FATAL_ERROR) +cmake_minimum_required(VERSION 3.8 FATAL_ERROR) add_library(messaging INTERFACE) @@ -6,5 +6,8 @@ add_library(skullc::messaging ALIAS messaging) target_include_directories(messaging INTERFACE - ${CMAKE_CURRENT_SOURCE_DIR}/Inc + $ + $ ) + +skullc_install_packages(skullc messaging ${version}) diff --git a/Messaging/messaging-config.cmake.in b/Messaging/messaging-config.cmake.in new file mode 100644 index 0000000..36dd580 --- /dev/null +++ b/Messaging/messaging-config.cmake.in @@ -0,0 +1,2 @@ +include(${CMAKE_CURRENT_LIST_DIR}/skullc-messaging-config-version.cmake) +include(${CMAKE_CURRENT_LIST_DIR}/skullc-messaging-targets.cmake) diff --git a/Peripherals/CMakeLists.txt b/Peripherals/CMakeLists.txt new file mode 100644 index 0000000..5bd0d14 --- /dev/null +++ b/Peripherals/CMakeLists.txt @@ -0,0 +1,21 @@ +cmake_minimum_required(VERSION 3.8 FATAL_ERROR) + +add_library(peripherals STATIC + Src/peripherals_imu_icm.cpp + Src/peripherals_io.cpp + Src/peripherals_motors.cpp + Src/peripherals_pwm_channel.cpp + Src/peripherals_spi.cpp + Src/peripherals_utility.cpp +) + +add_library(skullc::peripherals ALIAS peripherals) + +target_include_directories(peripherals + PUBLIC + $ + $ +) + +## INSTALL +skullc_install_packages(skullc peripherals ${version}) diff --git a/Peripherals/peripherals-config.cmake.in b/Peripherals/peripherals-config.cmake.in new file mode 100644 index 0000000..70fa44c --- /dev/null +++ b/Peripherals/peripherals-config.cmake.in @@ -0,0 +1,2 @@ +include(${CMAKE_CURRENT_LIST_DIR}/skullc-peripherals-config-version.cmake) +include(${CMAKE_CURRENT_LIST_DIR}/skullc-peripherals-targets.cmake) diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt index 39fb3a0..15120a7 100644 --- a/Tests/CMakeLists.txt +++ b/Tests/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.14 FATAL_ERROR) +cmake_minimum_required(VERSION 3.8 FATAL_ERROR) find_package(Catch2 REQUIRED) diff --git a/Utility/CMakeLists.txt b/Utility/CMakeLists.txt index 2ae5354..1d0fff6 100644 --- a/Utility/CMakeLists.txt +++ b/Utility/CMakeLists.txt @@ -1,12 +1,24 @@ -cmake_minimum_required(VERSION 3.14 FATAL_ERROR) +cmake_minimum_required(VERSION 3.8 FATAL_ERROR) + +if(WITH_HAL) + set(additional_sources + Src/utility_asynchuartlogger.cpp + Src/utility_atomicscopeguard.cpp + Src/utility_itmlogger.cpp + Src/utility_uartlogger.cpp) +endif() add_library(utility STATIC Src/utility_logging.cpp + ${additional_sources} ) add_library(skullc::utility ALIAS utility) target_include_directories(utility PUBLIC - ${CMAKE_CURRENT_SOURCE_DIR}/Inc + $ + $ ) + +skullc_install_packages(skullc utility ${version}) diff --git a/Utility/utility-config.cmake.in b/Utility/utility-config.cmake.in new file mode 100644 index 0000000..19e81e0 --- /dev/null +++ b/Utility/utility-config.cmake.in @@ -0,0 +1,2 @@ +include(${CMAKE_CURRENT_LIST_DIR}/skullc-utility-config-version.cmake) +include(${CMAKE_CURRENT_LIST_DIR}/skullc-utility-targets.cmake) diff --git a/cmake/skullc-install.cmake b/cmake/skullc-install.cmake new file mode 100644 index 0000000..d4de803 --- /dev/null +++ b/cmake/skullc-install.cmake @@ -0,0 +1,44 @@ +macro(skullc_install_packages in_project in_component in_version) + # reference: https://stackoverflow.com/questions/54702582/how-to-configure-project-with-components-in-cmake + install(TARGETS ${in_component} EXPORT ${in_component}-targets + COMPONENT ${in_component} + LIBRARY DESTINATION lib + ARCHIVE DESTINATION lib + RUNTIME DESTINATION bin + INCLUDES DESTINATION include + ) + + install(EXPORT ${in_component}-targets + FILE "${in_project}-${in_component}-targets.cmake" + NAMESPACE ${in_project}:: + DESTINATION lib/cmake/${in_project} + COMPONENT ${in_component} + ) + + configure_file("${in_component}-config.cmake.in" + "${CMAKE_BINARY_DIR}/${in_project}-${in_component}-config.cmake" + @ONLY + ) + + include(CMakePackageConfigHelpers) + + write_basic_package_version_file( + "${CMAKE_BINARY_DIR}/${in_project}-${in_component}-config-version.cmake" + VERSION ${in_version} + COMPATIBILITY AnyNewerVersion + ) + + install( + FILES + "${CMAKE_BINARY_DIR}/${in_project}-${in_component}-config.cmake" + "${CMAKE_BINARY_DIR}/${in_project}-${in_component}-config-version.cmake" + DESTINATION lib/cmake/${in_project} + COMPONENT ${in_component} + ) + + install( + DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/Inc/ + COMPONENT ${in_component} + DESTINATION ${CMAKE_INSTALL_PREFIX}/include + ) +endmacro() \ No newline at end of file diff --git a/skullc-config.cmake b/skullc-config.cmake new file mode 100644 index 0000000..b65aacb --- /dev/null +++ b/skullc-config.cmake @@ -0,0 +1,3 @@ +foreach(component ${skullc_FIND_COMPONENTS}) + include(${CMAKE_CURRENT_LIST_DIR}/skullc-${component}-config.cmake) +endforeach() \ No newline at end of file