Assembler and C - handle flags and defines

today I have been working with cmake in order to translate a project based on eclipse. I faced a problem and really don’t know if the founded solution is good or not. So, if you have a minute, tell me what you think about…
Problem: the project contains c-language sources and files written in assembler.
The first version of the cmake project obtained was incorrect because the include paths and options, for both kind of files, were the same.
Some errors were present on assembler compilation.
So this is the solution I found:

  • create a library with a add_library(CSources STATIC ${SOURCE});
  • append includes to the lib with target_include_directories(CSources …file1.c … filen.c);
  • set compiler flags with set_target_properties(CSources PROPERTIES COMPILE_DEFINITIONS ${PRJ_DEFINES});
  • final target add_executable(${PROJECT_NAME} file_assembly.s $<TARGET_OBJECTS:CSources> );

It works but… I’m quite sure that for the purists I made mistakes…
Personally I dislike to pass defines with a variable PRJ_DEFINES instead to use ADD_DEFINITIONS but if I use it these will be also part of the assembler compilation.

Suggestion are welcome…

If you have language-specific bits, you can use $<$<COMPILE_LANGUAGE:C>:${flags}> to make them conditional (here, they’ll only be given to C sources). See cmake-generator-expressions(7).

This way?

# tell CMake the target we want to compile
add_executable(${PROJECT_NAME}.elf ${SOURCE})

options to apply fo C-language only

target_compile_options( ${PROJECT_NAME}.elf PRIVATE $<$<COMPILE_LANGUAGE:C>: -DCORE_CM4 -DUSE_HAL_DRIVER -DSTM32H755xx > )

The spaces are a problem here. For -D, also prefer compile definitions. How about

target_compile_definitions(${PROJECT_NAME}.elf PRIVATE "$<$<COMPILE_LANGUAGE:C>:CORE_CM4;USE_HAL_DRIVER;STM32H755xx>")


1 Like

You’d need to put quotes around that generator expression, otherwise the embedded semicolons will still split the string to be seen by the command as multiple arguments, just like the situation when spaces were used.

1 Like

Whoops, indeed. Fixed. Thanks.

Thank you all very much!
Precious info.