Importing (RTEMS ) libraries in CMake

308 Views Asked by At

I am trying to import a library in CMake the modern way like shown in this thread: How do I add a library path in cmake?

The goal is to build a RTEMS test program. I'm building on a Ubuntu 20.04 machine, and I am cross compiling for an ARM target with the arm/stm32h7 BSP.

The libraries are located inside an external lib folder. I almost got the build process working, however CMake appears to do something which breaks the linking process. I propably did the mistake but I have problems figuring it out.

This is the basic setup of my CMake file, after I set up everything for cross compilation of RTEMS binaries:

...
# Here comes application stuff again

add_executable(${CMAKE_PROJECT_NAME} init.c led.c stm32h7xx_nucleo.c)

set(RTEMS_LIB_NAME "rtems_${RTEMS_ARCH_NAME}_${RTEMS_BSP_NAME}")
add_library(${RTEMS_LIB_NAME} SHARED IMPORTED)
set_target_properties(${RTEMS_LIB_NAME} PROPERTIES
    IMPORTED_LOCATION ${RTEMS_BSP_LIB_PATH}
    INTERFACE_INCLUDE_DIRECTORIES ${RTEMS_BSP_INC_PATH}
)

#target_link_directories(${RTEMS_LIB_NAME} INTERFACE
#   ${RTEMS_BSP_LIB_PATH}
#)
#target_include_directories(${RTEMS_LIB_NAME} INTERFACE
#   ${RTEMS_BSP_INC_PATH}
#)
target_link_options(${RTEMS_LIB_NAME} INTERFACE
#   -I${RTEMS_BSP_INC_PATH}
#   -B${RTEMS_BSP_LIB_PATH}
    -Wl,--gc-sections 
    -Wl,-Bstatic 
    -Wl,-Bdynamic 
    -qrtems
)

target_link_libraries(${CMAKE_PROJECT_NAME} ${RTEMS_LIB_NAME})

Building the individual source files appears to work fine. The raw link command attempted by CMake will be the following:

/home/rmueller/Documents/RTEMS/toolchain/rtems/6/bin/arm-rtems6-gcc   
-mthumb -mcpu=cortex-m7 -mfpu=fpv5-d16 -mfloat-abi=hard 
-Wl,--gc-sections -Wl,-Bstatic -Wl,-Bdynamic 
-qrtems CMakeFiles/blinky.dir/init.c.o CMakeFiles/blinky.dir/led.c.o CMakeFiles/blinky.dir/stm32h7xx_nucleo.c.o  
-o blinky -Wl,-rpath,/home/rmueller/Documents/RTEMS/toolchain/rtems
/6/arm-rtems6/stm32h7 /home/rmueller/Documents/RTEMS/toolchain/rtems/6/arm-rtems6/stm32h7/lib 

And I get the error:

./../../../arm-rtems6/bin/ld: cannot open linker script file linkcmds: No such file or directory

This is propably because the libraries are not in the search path somehow. I then found out that the following command links the binary properly:

/home/rmueller/Documents/RTEMS/toolchain/rtems/6/bin/arm-rtems6-gcc  
-mthumb -mcpu=cortex-m7 -mfpu=fpv5-d16 -mfloat-abi=hard 
-Wl,--gc-sections -Wl,-Bstatic -Wl,-Bdynamic 
-qrtems CMakeFiles/blinky.dir/init.c.o CMakeFiles/blinky.dir/led.c.o 
CMakeFiles/blinky.dir/stm32h7xx_nucleo.c.o  -o blinky   
-L/home/rmueller/Documents/RTEMS/toolchain/rtems/6/arm-rtems6/stm32h7/lib

Is the way to import the library wrong? I could just add the -L flag manually to my build target using commands like target_link_options , but I was thinking it would be nice if the search path could just be an interface requirement when linking the RTEMS library.

UPDATE: I think I found one error: I imported the library path as a SHARED library and it propably has to be imported as STATIC. THe command now looks like this:

/home/rmueller/Documents/RTEMS/toolchain/rtems/6/bin/arm-rtems6-gcc   -mthumb 
-mcpu=cortex-m7 -mfpu=fpv5-d16 -mfloat-abi=hard -Wl,--gc-sections -Wl,-Bstatic -Wl,-Bdynamic -qrtems 
CMakeFiles/blinky.dir/init.c.o CMakeFiles/blinky.dir/led.c.o CMakeFiles/blinky.dir/stm32h7xx_nucleo.c.o  
-o blinky  /home/rmueller/Documents/RTEMS/toolchain/rtems/6/arm-rtems6/stm32h7/lib 

UPDATE2:

I solved the problem. There was still a little syntax error, I think the quotes were missing. The command to set the library properties looks like this now :

set_target_properties(${RTEMS_LIB_NAME} PROPERTIES
    IMPORTED_LOCATION "${RTEMS_BSP_LIB_PATH}"
    INTERFACE_INCLUDE_DIRECTORIES "${RTEMS_BSP_INC_PATH}"
)

And the binary is linked properly :-)

UPDATE3:

And it has stopped working again. This is really weird. The -L flag appears to be missing..

Kind Regards

Robin

1

There are 1 best solutions below

0
On

Okay, I finally solved the issue. The above option is used to explicitely include libraries. In the RTEMS case, simply adding the library path and using -qrtems is sufficient.

The resulting and working CMakeLists.txt file can be found here: https://github.com/rmspacefish/rtems-demo/blob/master/applications/stm32/blinky/CMakeLists.txt