Le lun. 7 oct. 2019 à 14:41, Vincent van Beveren <v.van.beve...@nikhef.nl>
a écrit :

> Hello everyone,
>
> I'm setting up a new build targeting an embedded platform (LM32) and
> trying to integrate a CRC32 into the resulting ELF. I have previously
> done this using plain Makefiles with the following steps (pseudo code):
>
> target.elf: <obj-files>
>     link <obj-files> --symbol CRC_VALUE=0 intermediate.elf
>     objcopy --binary intermediate.elf intermediate.bin
>     link <obj-files> --symbol CRC_VALUE=`crc32 intermediate.bin` target.elf
>     rm intermediate.bin
>
> Now I would like to achieve the same thing with CMake. I'm using
> add_executable with multiple targets. The end of my CMakeLists.txt looks
> like this, where F_GEN contains generic source files and F__* source
> files specific for that variant of the build:
>
> # [...defining of cross compiler, and source files, some compile flags,
> etc...]
> # Educate the linker
> add_link_options(
>      -nostartfiles
>      -nodefaultlibs
>      -nostdlib
>      -Wl,--gc-sections
>      -T ${CMAKE_SOURCE_DIR}/${P_SRC}/romram.ld
>      -Wl,--defsym=CRC_VALUE=0
>      -Wl,--defsym=_start=0
>      )
>
> # DOM v2 target
> add_executable( clb_v2_dom.elf ${F_GEN} ${F__DOM} )
> target_compile_definitions(clb_v2_dom.elf PUBLIC -DDOM -DCLBV2 )
>
> # BASE v2 target
> add_executable( clb_v2_base.elf ${F_GEN} ${F__BASE} )
> # TODO migrate define 'DUBASE' -> 'BASE'
> target_compile_definitions(clb_v2_base.elf PUBLIC -DDUBASE -DBASE -DCLBV2)
>
> # Golden v2 target
> add_executable( clb_v2_golden.elf ${F_GEN} ${F__GLD} )
> target_compile_definitions( clb_v2_golden.elf PUBLIC -DGOLDEN -DCLBV2 )
>
> ==
>
> As you can see CRC_VALUE is now simply defined 0 for every target. Which
> works well for compiling but during runtime poses a problem to the ROM
> verification procedure. What would a 'proper' way be of adding a CRC to
> an ELF file be using CMake, and be different for each target. Any help
> is welcome!
>

I would try to add a set of custom command as POST_BUILD event.
https://cmake.org/cmake/help/v3.15/command/add_custom_command.html#build-events

add_custom_command(TARGET clb_v2_base.elf POST_BUILD

 COMMAND crc32 $<TARGET_FILE:clb_v2_base.elf>
> $<TARGET_FILE:clb_v2_base.elf>.crc32
                                   COMMAND ${CMAKE_C_COMPILER}
--symbol $<TARGET_FILE:clb_v2_base.elf>.crc32
 $<TARGET_FILE:clb_v2_base.elf>
                                  )

I don't know if the linker can read the computed crc32 from the previously
generated file but you get the idea.
Moreover if you want to easily collect objects used for linking a target
you may need to use an intermediate
OBJECT library in order to be able to retrieve $<TARGET_OBJECTS:objlib>
see:
https://cmake.org/cmake/help/latest/command/add_library.html#object-libraries
i.e. replace:
add_executable( clb_v2_dom.elf ${F_GEN} ${F__DOM} )

by
add_library(clb_v2_dom.objs OBJECT ${F_GEN} ${F__DOM})
add_executable(clb_v2_dom.elf  $<TARGET_OBJECTS:clb_v2_dom.objs>)

and then:

add_custom_command(TARGET clb_v2_dom.elf POST_BUILD

 COMMAND crc32 $<TARGET_FILE:clb_v2_dom.elf>
> $<TARGET_FILE:clb_v2_dom.elf>.crc32
                                   COMMAND
${CMAKE_C_COMPILER} $<TARGET_OBJECTS:clb_v2_dom.objs>
--symbol $<TARGET_FILE:clb_v2_dom.elf>.crc32  $<TARGET_FILE:clb_v2_dom.elf>
                                  )

If this looks ok to you I would then write my own

lm32_add_executable that would wraps this up in order to be called as:

lm32_add_executable(EXECUTABLE clb_v2_dom.elf
                                 SOURCES ${F_GEN} ${F__DOM})




> Kind regards,
> Vincent
>
>
>
> --
> National Institute for Subatomic Physics Nikhef
> Department of Computer Technology
> Science Park 105
> 1098 XG AMSTERDAM
>
> tel.  : +31 (0)20 592 2032
> e-mail: v.van.beve...@nikhef.nl
> site  : http://www.nikhef.nl/~vincentb
>
> --
>
> Powered by www.kitware.com
>
> Please keep messages on-topic and check the CMake FAQ at:
> http://www.cmake.org/Wiki/CMake_FAQ
>
> Kitware offers various services to support the CMake community. For more
> information on each offering, please visit:
>
> CMake Support: http://cmake.org/cmake/help/support.html
> CMake Consulting: http://cmake.org/cmake/help/consulting.html
> CMake Training Courses: http://cmake.org/cmake/help/training.html
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Follow this link to subscribe/unsubscribe:
> https://cmake.org/mailman/listinfo/cmake
>


-- 
Eric
-- 

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: 
http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more 
information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at 
http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake

Reply via email to