From 7b6ca7c967f31152f5d5af208a6cbfd0abe9e896 Mon Sep 17 00:00:00 2001
From: Damiano Amatruda <damiano.amatruda@skywarder.eu>
Date: Fri, 12 Nov 2021 22:18:46 +0100
Subject: [PATCH] [SBS] Add full support for all libraries in CMake

* File 'dependencies.cmake' is updated to configure and fully support Mxgui, Eigen, {fmt} and local libraries.

* Library {fmt} now works with every board, as it is no longer compiled with hardcoded flags.

* Function 'sbs_link_mxgui' is added, allowing to link Mxgui without respecifying the board.
---
 cmake/FindEIGEN.cmake    | 39 -----------------------------------
 cmake/FindFMT.cmake      | 44 ----------------------------------------
 cmake/FindMXGUI.cmake    | 42 --------------------------------------
 cmake/FindTSCPP.cmake    | 37 ---------------------------------
 cmake/dependencies.cmake | 18 ++++++++++++----
 cmake/sbs.cmake          | 13 ++++++++++--
 6 files changed, 25 insertions(+), 168 deletions(-)
 delete mode 100644 cmake/FindEIGEN.cmake
 delete mode 100644 cmake/FindFMT.cmake
 delete mode 100644 cmake/FindMXGUI.cmake
 delete mode 100644 cmake/FindTSCPP.cmake

diff --git a/cmake/FindEIGEN.cmake b/cmake/FindEIGEN.cmake
deleted file mode 100644
index b69e4d53e..000000000
--- a/cmake/FindEIGEN.cmake
+++ /dev/null
@@ -1,39 +0,0 @@
-# Copyright (c) 2021 Skyward Experimental Rocketry
-# Author: Damiano Amatruda
-#
-# Permission is hereby granted, free of charge, to any person obtaining a copy
-# of this software and associated documentation files (the "Software"), to deal
-# in the Software without restriction, including without limitation the rights
-# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-# copies of the Software, and to permit persons to whom the Software is
-# furnished to do so, subject to the following conditions:
-#
-# The above copyright notice and this permission notice shall be included in
-# all copies or substantial portions of the Software.
-#
-# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
-# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-# THE SOFTWARE.
-
-find_path(EIGEN_INCLUDE_DIR
-    NAMES signature_of_eigen3_matrix_library
-    PATHS ${SBS_BASE}/libs/eigen
-)
-set(EIGEN_CFLAGS EIGEN_MAX_ALIGN_BYTES=0)
-
-include(FindPackageHandleStandardArgs)
-find_package_handle_standard_args(EIGEN
-    FOUND_VAR EIGEN_FOUND
-    REQUIRED_VARS EIGEN_INCLUDE_DIR
-)
-
-if(EIGEN_FOUND)
-    set(EIGEN_INCLUDE_DIRS ${EIGEN_INCLUDE_DIR})
-    set(EIGEN_DEFINITIONS ${EIGEN_CFLAGS})
-endif()
-
-mark_as_advanced(EIGEN_INCLUDE_DIR)
diff --git a/cmake/FindFMT.cmake b/cmake/FindFMT.cmake
deleted file mode 100644
index baebb9c8f..000000000
--- a/cmake/FindFMT.cmake
+++ /dev/null
@@ -1,44 +0,0 @@
-# Copyright (c) 2021 Skyward Experimental Rocketry
-# Author: Damiano Amatruda
-#
-# Permission is hereby granted, free of charge, to any person obtaining a copy
-# of this software and associated documentation files (the "Software"), to deal
-# in the Software without restriction, including without limitation the rights
-# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-# copies of the Software, and to permit persons to whom the Software is
-# furnished to do so, subject to the following conditions:
-#
-# The above copyright notice and this permission notice shall be included in
-# all copies or substantial portions of the Software.
-#
-# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
-# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-# THE SOFTWARE.
-
-find_path(FMT_INCLUDE_DIR
-    NAMES fmt/format.h
-    PATHS ${SBS_BASE}/libs/fmt/include
-)
-find_library(FMT_LIBRARY
-    NAMES fmt
-    PATHS ${SBS_BASE}/libs/fmt
-)
-set(FMT_CFLAGS FMT_STATIC_THOUSANDS_SEPARATOR _GLIBCXX_USE_WCHAR_T)
-
-include(FindPackageHandleStandardArgs)
-find_package_handle_standard_args(FMT
-    FOUND_VAR FMT_FOUND
-    REQUIRED_VARS FMT_INCLUDE_DIR FMT_LIBRARY
-)
-
-if(FMT_FOUND)
-    set(FMT_INCLUDE_DIRS ${FMT_INCLUDE_DIR})
-    set(FMT_LIBRARIES ${FMT_LIBRARY})
-    set(FMT_DEFINITIONS ${FMT_CFLAGS})
-endif()
-
-mark_as_advanced(FMT_INCLUDE_DIR FMT_LIBRARY)
diff --git a/cmake/FindMXGUI.cmake b/cmake/FindMXGUI.cmake
deleted file mode 100644
index 01d3b488e..000000000
--- a/cmake/FindMXGUI.cmake
+++ /dev/null
@@ -1,42 +0,0 @@
-# Copyright (c) 2021 Skyward Experimental Rocketry
-# Author: Damiano Amatruda
-#
-# Permission is hereby granted, free of charge, to any person obtaining a copy
-# of this software and associated documentation files (the "Software"), to deal
-# in the Software without restriction, including without limitation the rights
-# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-# copies of the Software, and to permit persons to whom the Software is
-# furnished to do so, subject to the following conditions:
-#
-# The above copyright notice and this permission notice shall be included in
-# all copies or substantial portions of the Software.
-#
-# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
-# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-# THE SOFTWARE.
-
-find_path(MXGUI_INCLUDE_DIR1
-    NAMES mxgui/display.h
-    PATHS ${SBS_BASE}/libs
-)
-
-find_path(MXGUI_INCLUDE_DIR2
-    NAMES config/mxgui_settings.h
-    PATHS ${SBS_BASE}/libs/mxgui
-)
-
-include(FindPackageHandleStandardArgs)
-find_package_handle_standard_args(MXGUI
-    FOUND_VAR MXGUI_FOUND
-    REQUIRED_VARS MXGUI_INCLUDE_DIR1 MXGUI_INCLUDE_DIR2
-)
-
-if(MXGUI_FOUND)
-    set(MXGUI_INCLUDE_DIRS ${MXGUI_INCLUDE_DIR1} ${MXGUI_INCLUDE_DIR2})
-endif()
-
-mark_as_advanced(MXGUI_INCLUDE_DIR1 MXGUI_INCLUDE_DIR2)
diff --git a/cmake/FindTSCPP.cmake b/cmake/FindTSCPP.cmake
deleted file mode 100644
index 0683dd65b..000000000
--- a/cmake/FindTSCPP.cmake
+++ /dev/null
@@ -1,37 +0,0 @@
-# Copyright (c) 2021 Skyward Experimental Rocketry
-# Author: Damiano Amatruda
-#
-# Permission is hereby granted, free of charge, to any person obtaining a copy
-# of this software and associated documentation files (the "Software"), to deal
-# in the Software without restriction, including without limitation the rights
-# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-# copies of the Software, and to permit persons to whom the Software is
-# furnished to do so, subject to the following conditions:
-#
-# The above copyright notice and this permission notice shall be included in
-# all copies or substantial portions of the Software.
-#
-# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
-# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-# THE SOFTWARE.
-
-find_path(TSCPP_INCLUDE_DIR
-    NAMES tscpp/buffer.h
-    PATHS ${SBS_BASE}/libs
-)
-
-include(FindPackageHandleStandardArgs)
-find_package_handle_standard_args(TSCPP
-    FOUND_VAR TSCPP_FOUND
-    REQUIRED_VARS TSCPP_INCLUDE_DIR
-)
-
-if(TSCPP_FOUND)
-    set(TSCPP_INCLUDE_DIRS ${TSCPP_INCLUDE_DIR})
-endif()
-
-mark_as_advanced(TSCPP_INCLUDE_DIR)
diff --git a/cmake/dependencies.cmake b/cmake/dependencies.cmake
index 324a4bc71..6d111d28c 100644
--- a/cmake/dependencies.cmake
+++ b/cmake/dependencies.cmake
@@ -19,10 +19,20 @@
 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 # THE SOFTWARE.
 
-find_package(EIGEN)
-find_package(FMT)
-find_package(MXGUI)
-find_package(TSCPP)
+add_subdirectory(${KPATH} EXCLUDE_FROM_ALL)
+
+add_subdirectory(${SBS_BASE}/libs/mxgui EXCLUDE_FROM_ALL)
+
+set(CMAKE_BUILD_WITH_INSTALL_RPATH ON)
+set(BUILD_TESTING OFF CACHE BOOL "Enable creation of Eigen tests.")
+set(EIGEN_TEST_NOQT ON CACHE BOOL "Disable Qt support in unit tests")
+set(CMAKE_Fortran_COMPILER NOTFOUND)
+add_subdirectory(${SBS_BASE}/libs/eigen EXCLUDE_FROM_ALL)
+target_compile_definitions(eigen INTERFACE EIGEN_MAX_ALIGN_BYTES=0)
+
+add_subdirectory(${SBS_BASE}/libs/fmt EXCLUDE_FROM_ALL)
+target_compile_definitions(fmt-header-only INTERFACE _GLIBCXX_USE_WCHAR_T FMT_UNICODE=0 FMT_STATIC_THOUSANDS_SEPARATOR=0)
+target_compile_options(fmt-header-only INTERFACE -fno-math-errno)
 
 set(SHARED_SOURCES
     ${SBS_BASE}/src/shared/scheduler/TaskScheduler.cpp
diff --git a/cmake/sbs.cmake b/cmake/sbs.cmake
index ba29b7b61..f2a25c086 100644
--- a/cmake/sbs.cmake
+++ b/cmake/sbs.cmake
@@ -37,7 +37,16 @@ endif()
 
 include(dependencies)
 
-function(sbs_get_board TARGET)
+function(sbs_mxgui TARGET)
+    get_target_property(OPT_BOARD ${TARGET} OPT_BOARD)
+    if(NOT OPT_BOARD)
+        message(FATAL_ERROR "No board selected")
+    endif()
+
+    target_link_libraries(${TARGET} mxgui::mxgui-${OPT_BOARD})
+endfunction()
+
+function(sbs_target TARGET)
     get_target_property(OPT_BOARD ${TARGET} OPT_BOARD)
     if(NOT OPT_BOARD)
         message(FATAL_ERROR "No board selected")
@@ -45,7 +54,7 @@ function(sbs_get_board TARGET)
     set(OPT_BOARD ${OPT_BOARD} PARENT_SCOPE)
 endfunction()
 
-function(sbs_mxgui TARGET)
+function(sbs_link_mxgui TARGET)
     sbs_get_board(${TARGET})
     target_link_libraries(${TARGET} PRIVATE mxgui::mxgui-${OPT_BOARD})
 endfunction()
-- 
GitLab