recplacing linker script option

I have an arm-eabi-none environment (mbed-os) and want to add a custom linker script option to the build system. The current system uses a fixed linker script, assigned to the used target mcu/board.

The original script is set by

    add_custom_target(mbed-linker-script DEPENDS ${LINKER_SCRIPT_PATH} VERBATIM)
    foreach(TARGET ${mbed_baremetal_target} ${mbed_os_target})
        add_dependencies(${TARGET} mbed-linker-script)

        # Add linker flags to the MCU target to pick up the preprocessed linker script
        target_link_options(${TARGET}
            INTERFACE
                "-T" "${LINKER_SCRIPT_PATH}"
        )
    endforeach()

Now I want to override this option in a later, optional step.

        remove_flag_from_target(${TARGET} "-T" "${LINKER_SCRIPT_PATH}")

        # Add linker flags to the MCU target to pick up the preprocessed linker script
        target_link_options(${TARGET}
            INTERFACE
                "-T" "${CUSTOM_LINKER_SCRIPT_PATH}"
        )

The remove flag macro is from Stackoverflow, it reads the options into a list and removes the item if found::

macro(remove_flag_from_target _target _flag)
    get_target_property(_target_cxx_flags ${_target} INTERFACE_LINK_OPTIONS)
    if(_target_cxx_flags)
        list(REMOVE_ITEM _target_cxx_flags ${_flag})
        set_target_properties(${_target} PROPERTIES INTERFACE_LINK_OPTIONS "${_target_cxx_flags}")
    endif()
endmacro()

But this does not work, I have tried now many versions of writing the flags, it will not remove -T and the script name. What is missing?

after executing this, the remaining INTERFACE_LINK_OPTIONS are:

[cmake] INTERFACE_LINK_OPTIONS in mbed-os: -Wl,--gc-sections-Wl,--wrap,main-Wl,--wrap,_malloc_r-Wl,--wrap,_free_r-Wl,--wrap,_realloc_r-Wl,--wrap,_memalign_r-Wl,--wrap,_calloc_r-Wl,--wrap,exit-Wl,--wrap,atexit-Wl,-n/home/jojo/projects/HeaterController/build/F769NI-STM32CUBE-Debug/mbed-disco-f769ni.link_script.ld-T/home/jojo/projects/HeaterController/build/F769NI-STM32CUBE-Debug/app_Hello/app_hello.link_spript.ld

only the first flag id ‘-T’ is removed.

That remove_flag_from_target function only removes the first argument you pass. I think you should experiment with using that to remove its arguments from a list and then move it to doing the target property stuff.

thanks, good idea.
It did not work, but I’ve printed the name of the LINKER_SCRIPT_PATH that I want to remove and it was empty at that point. So I have a clue where to find the problem.

        message("LINKDER_SCRIPT_PATH: " ${LINKER_SCRIPT_PATH})
        remove_flag_from_target(${TARGET} "-T")
        remove_flag_from_target(${TARGET} "${LINKER_SCRIPT_PATH}")
[cmake] LINKDER_SCRIPT_PATH: /home/jojo/projects/HeaterController/build/F769NI-STM32CUBE-Debug/.link_script.ld

does not match

[cmake] INTERFACE_LINK_OPTIONS in mbed-os: -Wl,--gc-sections-Wl,--wrap,main-Wl,--wrap,_malloc_r-Wl,--wrap,_free_r-Wl,--wrap,_realloc_r-Wl,--wrap,_memalign_r-Wl,--wrap,_calloc_r-Wl,--wrap,exit-Wl,--wrap,atexit-Wl,-n/home/jojo/projects/HeaterController/build/F769NI-STM32CUBE-Debug/mbed-disco-f769ni.link_script.ld-T/home/jojo/projects/HeaterController/build/F769NI-STM32CUBE-Debug/app_Hello/app_hello.link_spript.ld

still learning CMake.