Same sources, 2 different binaries with a linker script variable
With CMake, I am trying to use the same sources and the same linker script to generate two different binaries.
/* Specify the memory areas */
MEMORY
{
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 320K
FLASH (rx) : ORIGIN = APP_ORIGIN, LENGTH = 0x40000
}
APP_ORIGIN is set as input in linker script, using --defsym=APP_ORIGIN=VALUE.
set(APP1_ORIGIN 0x0800C200)
set(APP2_ORIGIN 0x0804C200)
SET(CMAKE_EXE_LINKER_FLAGS "-T${LINKER_FILE} -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard -specs=nano.specs -lc -lm -lnosys -Wl,-Map=${PROJECT_NAME}.map,--cref -Wl,--gc-sections -Xlinker -print-memory-usage -Xlinker --defsym=APP_ORIGIN=${APP1_ORIGIN}")
The point is I do not know how to modify CMAKE_EXE_LINKER_FLAGS with APP2_ORIGIN value and trigger link again.
This could be done with 2 different target, but if there is only one linker script, only have to maintain one, because the application is exactly the same, only the offset varies.
I tried with different approaches of POST_BUILD custom targets, but no success until now.
Solution 1:
Files are generated from input template.
Create a file inputfile
:
/* Specify the memory areas */
MEMORY
{
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 320K
FLASH (rx) : ORIGIN = @APP_ORIGIN@, LENGTH = 0x40000
}
Then do:
configure_file(inputfile ${CMAKE_BUILD_DIR}/linkerscript1 @ONLY)
configure_file(inputfile ${CMAKE_BUILD_DIR}/linkerscript1 @ONLY)
Or alternatively the same with add_custom_command
and a custom script.
And then do:
add_library(allobjectfiles all_your_files.c) # maybe OBJECT library?
add_executable(yourtarget)
target_link_libraries(yourtarget allobjectfiles)
target_link_options(yourtarget PUBLIC
-T${CMAKE_BUILD_DIR}/linkerscript1
)
set_target_properties(yourtarget PROPERTIES LINK_DEPENDS
${CMAKE_BUILD_DIR}/linkerscript1
)
# Repeat above for linkerscript2 with different target name