Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use generator expression to set build type specific flags

Tags:

cmake

I'd like to set some specific compile flags based on my current build configuration in cmake. I thought generator expressions would allow me to do this, but they don't appear to be working the way I expected.

I'm using the following command to set compile options to my main target. Both the expressions appear to always evaluate true as --debug and -Oh are passed to compiler no matter what CMAKE_BUILD_TYPE is set to.

target_compile_options(${PROJECT_NAME}
   PUBLIC
   ${COMMON_COMPILER_FLAGS}
   $<$<CONFIG:Debug>:--debug>
   $<$<CONFIG:Release>:-Oh>
   )

I'm using cmake 3.4.1 on Windows and I'm cross-compiling with the IAR toolchain. To be more specific, I'm executing cmake from the bash shell in Cygwin, but its still the Windows executable. I'm using the Unix Makefile generator.

like image 320
grifcj Avatar asked Aug 30 '25 17:08

grifcj


1 Answers

It looks like the second flag is getting picked up from somewhere and just so happens to be ordered at the right spot to give me the above impression.

I can update with an explanation for that when I determine it, but the generator expressions appear to have always been working. I inserted some keyboard-smash in for both and it became apparent only one was getting through to command line.

EDIT: The unwanted optimization flag was coming from target_compile_options calls on a couple libraries that are linked to my main target. I had the scope options set to PUBLIC, which means the options populate INTERFACE_COMPILE_OPTIONS. I changed the scopes to PRIVATE and it got rid of the unwanted flag.

like image 182
grifcj Avatar answered Sep 02 '25 17:09

grifcj