From 58e3ae3bc809890a38c89c55a51e480b8606bcd9 Mon Sep 17 00:00:00 2001 From: Alberto Nidasio <nidasioalberto@gmail.com> Date: Thu, 13 Feb 2025 09:29:40 +0100 Subject: [PATCH] Fixed binary generation in CMake build system --- CMakeLists.txt | 1 + miosix/_examples/blinking_led/CMakeLists.txt | 1 + miosix/_examples/processes/CMakeLists.txt | 1 + miosix/_tools/processes/CMakeLists.txt | 1 + miosix/_tools/testsuite/CMakeLists.txt | 2 ++ miosix/cmake/AddProgramTarget.cmake | 10 ++++++++- miosix/cmake/AddRomfsImage.cmake | 12 +++++++--- miosix/cmake/LinkTarget.cmake | 23 +------------------- 8 files changed, 25 insertions(+), 26 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f052f2d1..27560fec 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -36,6 +36,7 @@ include(LinkTarget) # List here your source files (.s, .c and .cpp) add_executable(main main.cpp) miosix_link_target(main) +miosix_add_program_target(main) # List here additional include directories # target_include_directories(main PRIVATE here_your_includes) diff --git a/miosix/_examples/blinking_led/CMakeLists.txt b/miosix/_examples/blinking_led/CMakeLists.txt index 33ac7057..156d72a5 100644 --- a/miosix/_examples/blinking_led/CMakeLists.txt +++ b/miosix/_examples/blinking_led/CMakeLists.txt @@ -37,3 +37,4 @@ include(LinkTarget) add_executable(blinking_led simple.cpp) miosix_link_target(blinking_led) +miosix_add_program_target(blinking_led) diff --git a/miosix/_examples/processes/CMakeLists.txt b/miosix/_examples/processes/CMakeLists.txt index e2711da1..df98d156 100644 --- a/miosix/_examples/processes/CMakeLists.txt +++ b/miosix/_examples/processes/CMakeLists.txt @@ -55,3 +55,4 @@ miosix_add_romfs_image( KERNEL main PROCESSES process1 process2 ) +miosix_add_program_target(image DEPENDS image) diff --git a/miosix/_tools/processes/CMakeLists.txt b/miosix/_tools/processes/CMakeLists.txt index 314d0a1d..c0f924a3 100644 --- a/miosix/_tools/processes/CMakeLists.txt +++ b/miosix/_tools/processes/CMakeLists.txt @@ -54,3 +54,4 @@ miosix_add_romfs_image( KERNEL main PROCESSES hello ) +miosix_add_program_target(image DEPENDS image) diff --git a/miosix/_tools/testsuite/CMakeLists.txt b/miosix/_tools/testsuite/CMakeLists.txt index b42cda4e..660da8df 100644 --- a/miosix/_tools/testsuite/CMakeLists.txt +++ b/miosix/_tools/testsuite/CMakeLists.txt @@ -43,6 +43,7 @@ include(AddRomfsImage) # Kernel level program add_executable(testsuite testsuite.cpp) miosix_link_target(testsuite) +miosix_add_program_target(testsuite) # Processes # miosix_add_process(test_process test_process/main.cpp) @@ -56,3 +57,4 @@ miosix_link_target(testsuite) # KERNEL testsuite # PROCESSES test_process # test_execve test_global_dtor_ctor # ) +# miosix_add_program_target(image DEPENDS image) diff --git a/miosix/cmake/AddProgramTarget.cmake b/miosix/cmake/AddProgramTarget.cmake index b02fea46..5bb20db6 100644 --- a/miosix/cmake/AddProgramTarget.cmake +++ b/miosix/cmake/AddProgramTarget.cmake @@ -31,7 +31,15 @@ function(miosix_add_program_target TARGET) cmake_parse_arguments(PROGRAM "" "" "DEPENDS" ${ARGN}) if(NOT PROGRAM_DEPENDS) - set(PROGRAM_DEPENDS ${TARGET}_bin ${TARGET}_hex) + add_custom_command( + OUTPUT ${TARGET}.bin + COMMAND ${CMAKE_OBJCOPY} -O binary $<TARGET_FILE:${TARGET}> ${TARGET}.bin + DEPENDS ${TARGET} + COMMENT "Creating ${TARGET}.bin" + VERBATIM + ) + add_custom_target(${TARGET}_bin ALL DEPENDS ${TARGET}.bin) + set(PROGRAM_DEPENDS ${TARGET}_bin) endif() get_target_property(PROGRAM_CMDLINE miosix PROGRAM_CMDLINE) diff --git a/miosix/cmake/AddRomfsImage.cmake b/miosix/cmake/AddRomfsImage.cmake index ea83b609..396cef54 100644 --- a/miosix/cmake/AddRomfsImage.cmake +++ b/miosix/cmake/AddRomfsImage.cmake @@ -62,6 +62,15 @@ function(miosix_add_romfs_image) PROCESSES ${ROMFS_PROCESSES} ) + # Create the image of the kernel + add_custom_command( + OUTPUT ${ROMFS_KERNEL}.bin + DEPENDS ${ROMFS_KERNEL} + COMMAND ${CMAKE_OBJCOPY} -O binary $<TARGET_FILE:${ROMFS_KERNEL}> ${ROMFS_KERNEL}.bin + COMMENT "Creating ${ROMFS_KERNEL}.bin" + VERBATIM + ) + # Create the romfs image with the given processes add_custom_command( OUTPUT ${ROMFS_IMAGE_NAME}-romfs.bin @@ -80,7 +89,4 @@ function(miosix_add_romfs_image) # Create the custom romfs target add_custom_target(${ROMFS_IMAGE_NAME} ALL DEPENDS ${PROJECT_BINARY_DIR}/${ROMFS_IMAGE_NAME}.bin) - - # And a target to flash the image - miosix_add_program_target(${ROMFS_IMAGE_NAME} DEPENDS ${ROMFS_IMAGE_NAME}) endfunction() diff --git a/miosix/cmake/LinkTarget.cmake b/miosix/cmake/LinkTarget.cmake index a9ffad8e..fdbdbcac 100644 --- a/miosix/cmake/LinkTarget.cmake +++ b/miosix/cmake/LinkTarget.cmake @@ -32,8 +32,6 @@ include(AddProgramTarget) # What it does: # - Links the Miosix libraries to the target # - Tells the linker to generate the map file -# - Registers custom targets to create the hex and bin files (${TARGET}_bin and ${TARGET}_hex) -# - Registers a custom target to flash the program to the board (${TARGET}_program) function(miosix_link_target TARGET) if (NOT TARGET miosix) message(FATAL_ERROR "The board you selected is not supported") @@ -42,29 +40,10 @@ 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) - - # 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) - - # Generate custom build command to flash the target - miosix_add_program_target(${TARGET}) endfunction() -- GitLab