From 79303cd85ce9926930c0d653865e48ca4d84202e Mon Sep 17 00:00:00 2001
From: Emilio Corigliano <emilio.corigliano@skywarder.eu>
Date: Sun, 27 Oct 2024 18:42:29 +0100
Subject: [PATCH] [cmake] Supporting usage of miosix as library with external
 board definitions

---
 miosix/CMakeLists.txt                       |  7 ++--
 miosix/arch/cortexM7_stm32f7/CMakeLists.txt |  4 ++-
 miosix/cmake/LinkTarget.cmake               | 37 +++++++++++----------
 3 files changed, 28 insertions(+), 20 deletions(-)

diff --git a/miosix/CMakeLists.txt b/miosix/CMakeLists.txt
index accf16fb..4751904d 100644
--- a/miosix/CMakeLists.txt
+++ b/miosix/CMakeLists.txt
@@ -78,11 +78,14 @@ set(MIOSIX_KERNEL_SRC
 )
 
 # Include the architecture options
-include(${MIOSIX_KPATH}/arch/CMakeLists.txt)
+if(DEFINED MIOSIX_OPT_BOARD)
+    include(${MIOSIX_KPATH}/arch/CMakeLists.txt)
+else()
+    message(WARNING "MIOSIX_OPT_BOARD not set")
+endif()
 
 # Verify that all the required variables have been defined, otherwise abort
 set(REQUIRED_VARIABLES
-    MIOSIX_OPT_BOARD
     MIOSIX_ARCH_INC
     MIOSIX_BOARD_INC
     MIOSIX_BOARD_SETTINGS_INC
diff --git a/miosix/arch/cortexM7_stm32f7/CMakeLists.txt b/miosix/arch/cortexM7_stm32f7/CMakeLists.txt
index 87f048c3..0c358960 100644
--- a/miosix/arch/cortexM7_stm32f7/CMakeLists.txt
+++ b/miosix/arch/cortexM7_stm32f7/CMakeLists.txt
@@ -27,7 +27,9 @@
 set(MIOSIX_ARCH_INC ${MIOSIX_KPATH}/arch/${MIOSIX_ARCH_NAME}/common)
 
 # Include board specific options
-include(${MIOSIX_KPATH}/arch/${MIOSIX_ARCH_NAME}/${MIOSIX_OPT_BOARD}/CMakeLists.txt)
+if(DEFINED MIOSIX_OPT_BOARD)
+    include(${MIOSIX_KPATH}/arch/${MIOSIX_ARCH_NAME}/${MIOSIX_OPT_BOARD}/CMakeLists.txt)
+endif()
 
 # Automatically transform the architecture name into upper cases
 string(TOUPPER ${MIOSIX_ARCH_NAME} MIOSIX_ARCH_NAME_UPPER)
diff --git a/miosix/cmake/LinkTarget.cmake b/miosix/cmake/LinkTarget.cmake
index a9ffad8e..706f6879 100644
--- a/miosix/cmake/LinkTarget.cmake
+++ b/miosix/cmake/LinkTarget.cmake
@@ -42,28 +42,31 @@ function(miosix_link_target TARGET)
     # Linker script and linking options are eredited from miosix libraries
 
     # Link libraries
-    target_link_libraries(${TARGET} PRIVATE
+    target_link_libraries(${TARGET} PUBLIC
         -Wl,--start-group miosix stdc++ c m gcc atomic -Wl,--end-group
     )
 
     # Tell the linker to produce the map file
-    target_link_options(${TARGET} PRIVATE -Wl,-Map,$<TARGET_FILE_DIR:${TARGET}>/$<TARGET_FILE_BASE_NAME:${TARGET}>.map)
+    target_link_options(${TARGET} PUBLIC -Wl,-Map,$<TARGET_FILE_DIR:${TARGET}>/$<TARGET_FILE_BASE_NAME:${TARGET}>.map)
 
-    # Add a post build command to create the hex file to flash on the board
-    add_custom_command(
-        OUTPUT ${TARGET}.hex
-        COMMAND ${CMAKE_OBJCOPY} -O ihex $<TARGET_FILE:${TARGET}> ${TARGET}.hex
-        COMMENT "Creating ${TARGET}.hex"
-        VERBATIM
-    )
-    add_custom_target(${TARGET}_hex ALL DEPENDS ${TARGET}.hex)
-    add_custom_command(
-        OUTPUT ${TARGET}.bin
-        COMMAND ${CMAKE_OBJCOPY} -O binary $<TARGET_FILE:${TARGET}> ${TARGET}.bin
-        COMMENT "Creating ${TARGET}.bin"
-        VERBATIM
-    )
-    add_custom_target(${TARGET}_bin ALL DEPENDS ${TARGET}.bin)
+    get_target_property(target_type ${TARGET} TYPE)
+    if (target_type STREQUAL "EXECUTABLE")
+        # Add a post build command to create the hex file to flash on the board
+        add_custom_command(
+            OUTPUT ${TARGET}.hex
+            COMMAND ${CMAKE_OBJCOPY} -O ihex $<TARGET_FILE:${TARGET}> ${TARGET}.hex
+            COMMENT "Creating ${TARGET}.hex"
+            VERBATIM
+        )
+        add_custom_target(${TARGET}_hex ALL DEPENDS ${TARGET}.hex)
+        add_custom_command(
+            OUTPUT ${TARGET}.bin
+            COMMAND ${CMAKE_OBJCOPY} -O binary $<TARGET_FILE:${TARGET}> ${TARGET}.bin
+            COMMENT "Creating ${TARGET}.bin"
+            VERBATIM
+        )
+        add_custom_target(${TARGET}_bin ALL DEPENDS ${TARGET}.bin)
+    endif ()
 
     # Generate custom build command to flash the target
     miosix_add_program_target(${TARGET})
-- 
GitLab