0

I am trying to build a ROM image using CMake. I have a project root directory CMakeLists.txt file which calls add_subdirectory() to build the different components for the ROM image. These components are output in binary format. Once this is done I wish to combine the resulting binaries into a single image.

Currently I have a final add_subdirectory() call that is meant to use objdump to convert the binary output of the other modules to an object file and then link these into a unified elf that is then converted with objdump.

add_executable(rom_image.bin)

foreach (COMPONENT ${COMPONENTS})
    set(COMPONENT_BINARY_FILENAME "${CMAKE_BINARY_DIR}/${COMPONENT}/output.bin")
    set(COMPONENT_OBJECT_FILENAME "${COMPONENT}.o")
    add_custom_command(
        TARGET rom_image.bin
        PRE_BUILD
        OUTPUT ${COMPONENT_OBJECT_FILENAME}
        DEPENDS ${COMPONENT_BINARY_FILENAME}
        COMMAND ${OBJCOPY_PROGRAM}
        ARGS --input-target=binary --output-target=elf32-littlearm --binary-architecture=arm ${COMPONENT_BINARY_FILENAME} ${COMPONENT_OBJECT_FILENAME}
    )
endforeach()

This does not work as CMake complains that the target (rom_image.bin) has no source files.

Tintin
  • 547
  • 5
  • 17
  • Because `add_executable(rom_image.bin)` doesn't specify any libraries. You want to use `add_custom_target` with custom build steps, `rom_image.bin` is not a C executable program, I guess the C linker shouldn't be invoked when creating `rom_image.bin` right? – KamilCuk Aug 30 '19 at 10:50
  • @KamilCuk Actually, I was hoping to use the linker to build the `rom_image.bin` which is why `add_executable` seemed to make sense. I have tried a few ways to `add_custom_target` but I can't figure out how to set that up to copy over the files to the current directory when their dependencies change. – Tintin Aug 30 '19 at 11:00
  • I think it would be just easier to recreate the behavior with add_custom_target + add_custom_command. You could do [this](https://stackoverflow.com/questions/38609303/how-to-add-prebuilt-object-files-to-executable-in-cmake) on generated .bin files, see how it acts. `copy over the files` chain add_custom_target+custom_command, no? – KamilCuk Aug 30 '19 at 11:42

0 Answers0