From 42e245b1da2db0486089ac753910dc39d377e671 Mon Sep 17 00:00:00 2001 From: Raphael Coeffic Date: Thu, 14 Dec 2023 00:12:31 +0100 Subject: [PATCH] chore: Use CMake way for linker options (#4396) Co-authored-by: Richard Li --- radio/src/CMakeLists.txt | 64 +++++++++++-------- .../linker/stm32f429_sdram/layout.ld | 4 +- .../targets/common/arm/stm32/CMakeLists.txt | 3 + .../arm/stm32/bootloader/CMakeLists.txt | 30 ++++++--- .../common/arm/stm32/f2/CMakeLists.txt | 6 ++ .../common/arm/stm32/f4/CMakeLists.txt | 6 ++ radio/src/targets/nv14/CMakeLists.txt | 2 +- radio/src/targets/pl18/CMakeLists.txt | 4 +- 8 files changed, 76 insertions(+), 43 deletions(-) diff --git a/radio/src/CMakeLists.txt b/radio/src/CMakeLists.txt index e72fe13f0..1cebc62f3 100644 --- a/radio/src/CMakeLists.txt +++ b/radio/src/CMakeLists.txt @@ -500,33 +500,14 @@ set(COMMON_FLAGS "${COMMON_FLAGS} ${WARNING_FLAGS}") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${COMMON_FLAGS} -Wimplicit") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COMMON_FLAGS} -fno-rtti") -# these are in addition to CMAKE_CXX_FLAGS -set(LINKER_DIR ${RADIO_SRC_DIR}/boards/generic_stm32/linker) -set(CMAKE_EXE_LINKER_FLAGS "-lm -T${LINKER_DIR}/firmware.ld -L${LINKER_DIR} -L${LINKER_DIR}/${TARGET_LINKER_DIR}") -set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-Map=firmware.map,--cref,--no-warn-mismatch,--gc-sections") -set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${TARGET_LINKER_PARAMS}") - -if(SEMIHOSTING) - add_definitions(-DSEMIHOSTING) - set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --specs=rdimon.specs") -endif() - -# Use newlib nano, which saves a few kilobytes. -if(NOT NANO STREQUAL NO) - set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --specs=nano.specs") - if(LUA) - # Lua needs %g and %f - set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -u _printf_float -u _scanf_float") - endif() - if(SDRAM) - # Target with SDRAM do not need a custom allocator for now - # as the custom allocator has alignement issues - message("Target has SDRAM, do not use BIN_ALLOCATOR") - else() - # Nano's malloc does work well with lua, use our own - add_definitions(-DUSE_BIN_ALLOCATOR) - set(SRC ${SRC} bin_allocator.cpp) - endif() +if(SDRAM) + # Target with SDRAM do not need a custom allocator for now + # as the custom allocator has alignement issues + message("Target has SDRAM, do not use BIN_ALLOCATOR") +else() + # Nano's malloc does work well with lua, use our own + add_definitions(-DUSE_BIN_ALLOCATOR) + set(SRC ${SRC} bin_allocator.cpp) endif() # Bootloader @@ -543,13 +524,40 @@ link_libraries(firmware -lstdc++) add_dependencies(firmware ${RADIO_DEPENDENCIES}) set_target_properties(firmware PROPERTIES EXCLUDE_FROM_ALL TRUE) +target_link_options(firmware PRIVATE + -lm -T${LINKER_DIR}/firmware.ld + -Wl,-Map=firmware.map,--cref,--no-warn-mismatch,--gc-sections + --specs=nano.specs +) + if (DEFINED TARGET_FLASH_SIZE) - message("TARGET_FLASH_SIZE = ${TARGET_FLASH_SIZE}") + # message("TARGET_FLASH_SIZE = ${TARGET_FLASH_SIZE}") target_link_options(firmware PRIVATE -Wl,--defsym=__flash_size=${TARGET_FLASH_SIZE} ) endif() +if (DEFINED TARGET_SDRAM_START) + # message("TARGET_SDRAM_START = ${TARGET_SDRAM_START}") + target_link_options(firmware PRIVATE + -Wl,--defsym=__sdram_start=${TARGET_SDRAM_START} + ) +endif() + +if (DEFINED TARGET_SDRAM_SIZE) + # message("TARGET_SDRAM_SIZE = ${TARGET_SDRAM_SIZE}") + target_link_options(firmware PRIVATE + -Wl,--defsym=__sdram_size=${TARGET_SDRAM_SIZE} + ) +endif() + +if (LUA) + # Lua needs %g and %f + target_link_options(firmware PRIVATE + "SHELL:-u _printf_float" "SHELL:-u _scanf_float" + ) +endif() + if(ACCESS_DENIED) add_subdirectory(thirdparty/AccessDenied) include_directories(thirdparty/AccessDenied) diff --git a/radio/src/boards/generic_stm32/linker/stm32f429_sdram/layout.ld b/radio/src/boards/generic_stm32/linker/stm32f429_sdram/layout.ld index 5e26ddd82..1f83dbe3b 100644 --- a/radio/src/boards/generic_stm32/linker/stm32f429_sdram/layout.ld +++ b/radio/src/boards/generic_stm32/linker/stm32f429_sdram/layout.ld @@ -12,8 +12,8 @@ BOOTLOADER_SIZE = 0x20000; MIN_HEAP_SIZE = 4096K; /* SDRAM definitions */ -SDRAM_START = DEFINED(__SDRAM_START__) ? __SDRAM_START__ : 0xD0000000; -SDRAM_SIZE = DEFINED(__SDRAM_SIZE__) ? __SDRAM_SIZE__ : 8192K; +SDRAM_START = DEFINED(__sdram_start) ? __sdram_start : 0xD0000000; +SDRAM_SIZE = DEFINED(__sdram_size) ? __sdram_size : 8192K; /* Highest heap address */ HEAP_ADDRESS = SDRAM_START + SDRAM_SIZE; diff --git a/radio/src/targets/common/arm/stm32/CMakeLists.txt b/radio/src/targets/common/arm/stm32/CMakeLists.txt index 214d39d0b..c98b51e10 100644 --- a/radio/src/targets/common/arm/stm32/CMakeLists.txt +++ b/radio/src/targets/common/arm/stm32/CMakeLists.txt @@ -10,6 +10,9 @@ set(ARCH ARM) set(STM32USB_DIR ${THIRDPARTY_DIR}/STM32_USB-Host-Device_Lib_V2.2.0/Libraries) add_definitions(-DSTM32 -DLUA_INPUTS -DVARIO) +set(LINKER_DIR ${RADIO_SRC_DIR}/boards/generic_stm32/linker) +add_link_options(-L${LINKER_DIR} -L${LINKER_DIR}/${TARGET_LINKER_DIR}) + include_directories(${RADIO_SRC_DIR}/targets/common/arm/stm32) include_directories(${STM32USB_DIR}/STM32_USB_OTG_Driver/inc) include_directories(${STM32USB_DIR}/STM32_USB_Device_Library/Core/inc) diff --git a/radio/src/targets/common/arm/stm32/bootloader/CMakeLists.txt b/radio/src/targets/common/arm/stm32/bootloader/CMakeLists.txt index 85e66458c..77fcfac19 100644 --- a/radio/src/targets/common/arm/stm32/bootloader/CMakeLists.txt +++ b/radio/src/targets/common/arm/stm32/bootloader/CMakeLists.txt @@ -151,19 +151,29 @@ remove_definitions(-DUSE_WATCHDOG) add_definitions(-DBOOT) -set(CMAKE_EXE_LINKER_FLAGS "-mcpu=${MCU} -mthumb -nostartfiles -lm") - -set(LINKER_DIR ${RADIO_SRC_DIR}/boards/generic_stm32/linker) -set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -T${LINKER_DIR}/bootloader.ld -L${LINKER_DIR} -L${LINKER_DIR}/${TARGET_LINKER_DIR}") - -set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-Map=bootloader.map,--cref,--no-warn-mismatch,--gc-sections") - -# use smaller nano libc -set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --specs=nano.specs") - add_executable(bootloader EXCLUDE_FROM_ALL ${BOOTLOADER_SRC}) add_dependencies(bootloader ${BITMAPS_TARGET}) +target_link_options(bootloader PRIVATE + -lm -nostartfiles -T${LINKER_DIR}/bootloader.ld + -Wl,-Map=bootloader.map,--cref,--no-warn-mismatch,--gc-sections + --specs=nano.specs +) + +if (DEFINED TARGET_SDRAM_START) + # message("TARGET_SDRAM_START = ${TARGET_SDRAM_START}") + target_link_options(bootloader PRIVATE + -Wl,--defsym=__sdram_start=${TARGET_SDRAM_START} + ) +endif() + +if (DEFINED TARGET_SDRAM_SIZE) + # message("TARGET_SDRAM_SIZE = ${TARGET_SDRAM_SIZE}") + target_link_options(bootloader PRIVATE + -Wl,--defsym=__sdram_size=${TARGET_SDRAM_SIZE} + ) +endif() + target_include_directories(bootloader PRIVATE ${CMAKE_CURRENT_BINARY_DIR} ) diff --git a/radio/src/targets/common/arm/stm32/f2/CMakeLists.txt b/radio/src/targets/common/arm/stm32/f2/CMakeLists.txt index 23de4ea7f..ac8aef7b6 100644 --- a/radio/src/targets/common/arm/stm32/f2/CMakeLists.txt +++ b/radio/src/targets/common/arm/stm32/f2/CMakeLists.txt @@ -3,6 +3,12 @@ set(MCU cortex-m3) set(FPU_FLAGS "") if(NOT NATIVE_BUILD) + + # Add MCU specific flags + add_link_options( + -mcpu=cortex-m3 -mfloat-abi=soft + ) + # STM32 HAL/LL library set(STM32CUBE_DIR ${THIRDPARTY_DIR}/STM32F2xx_HAL_Driver) include_directories(${STM32CUBE_DIR}/Inc) diff --git a/radio/src/targets/common/arm/stm32/f4/CMakeLists.txt b/radio/src/targets/common/arm/stm32/f4/CMakeLists.txt index c7f651859..71d72dae3 100644 --- a/radio/src/targets/common/arm/stm32/f4/CMakeLists.txt +++ b/radio/src/targets/common/arm/stm32/f4/CMakeLists.txt @@ -3,6 +3,12 @@ set(MCU cortex-m4) set(FPU_FLAGS "-mfloat-abi=hard -mfpu=fpv4-sp-d16") if(NOT NATIVE_BUILD) + + # Add MCU specific flags + add_link_options( + -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16 + ) + # STM32 HAL/LL library set(STM32CUBE_DIR ${THIRDPARTY_DIR}/STM32F4xx_HAL_Driver) include_directories(${STM32CUBE_DIR}/Inc) diff --git a/radio/src/targets/nv14/CMakeLists.txt b/radio/src/targets/nv14/CMakeLists.txt index 3913b075e..3defb7e8a 100644 --- a/radio/src/targets/nv14/CMakeLists.txt +++ b/radio/src/targets/nv14/CMakeLists.txt @@ -26,7 +26,7 @@ set(PPM_LIMITS_SYMETRICAL YES) # for size report script set(CPU_TYPE_FULL STM32F429xI) set(TARGET_LINKER_DIR stm32f429_sdram) -set(TARGET_LINKER_PARAMS "-Wl,--defsym=__SDRAM_START__=0xC0000000") +set(TARGET_SDRAM_START 0xC0000000) set(SIZE_TARGET_MEM_DEFINE "MEM_SIZE_SDRAM1=8192") diff --git a/radio/src/targets/pl18/CMakeLists.txt b/radio/src/targets/pl18/CMakeLists.txt index 10eb2ed14..8296d404e 100644 --- a/radio/src/targets/pl18/CMakeLists.txt +++ b/radio/src/targets/pl18/CMakeLists.txt @@ -16,7 +16,6 @@ set(HAPTIC YES) set(GUI_DIR colorlcd) set(BITMAPS_DIR 480x272) set(TARGET_DIR pl18) -set(LINKER_SCRIPT targets/pl18/stm32f4_flash.ld) set(RTC_BACKUP_RAM YES) set(PPM_LIMITS_SYMETRICAL YES) set(USB_SERIAL ON CACHE BOOL "Enable USB serial (CDC)") @@ -29,7 +28,8 @@ set(WIRELESS_CHARGER YES) # for size report script set(CPU_TYPE_FULL STM32F429xI) set(TARGET_LINKER_DIR stm32f429_sdram) -set(TARGET_LINKER_PARAMS "-Wl,--defsym=__SDRAM_START__=0xC0000000") +set(TARGET_SDRAM_START 0xC0000000) + set(SIZE_TARGET_MEM_DEFINE "MEM_SIZE_SDRAM1=8192") #set(RF_BAUD_RATE 921600 230400 115200 57600 38400 19200 9600 4800 2400 1200)