Compiler Option De-duplication with IAR

I’m using the IAR compiler and trying to override the optimization level on a specific source file.

The default optimization level is specified in our toolchain file, using CMAKE_CXX_FLAGS_RELWITHDEBINFO. I’m trying to override it using set_source_files_properties(foo.cpp PROPERTIES COMPILE_FLAGS -Ol). However, when I try to build, I get this error from the compiler: Command line error: Option -O can only occur once. Inspecting the compile command, the original -Ohs from the toolchain file appears first and the added -Ol appears second.

I understand the compile flags are supposed to be de-duplicated, but that doesn’t seem to be happening, at least for this specific option. I’ve tried looking through the CMake source code, but it wasn’t immediately apparent where the de-duplication was supposed to be happening or where I might provide a compiler-specific list of flags that should be de-duplicated (e.g., -O) vs. ones that should not (e.g., -D)

Any help would be appreciated!

target_compile_options de-duplicates multiple occurrences of identical flags. There is no de-duplication of two -O... flags with different ... values.

If the compiler doesn’t support overriding -O flags then you’ll have to teach the project to not use such flags globally.

I assume there are no hooks enabling post-processing of the options that would enable us to de-duplicate them?

The use case here is when debugging, we can’t de-optimize all files and still hit timing deadlines. So ideally, we could add a quick additional line to de-optimize a file or two that we’re actively debugging.

If the compiler doesn’t support overriding -O flags then you’ll have to teach the project to not use such flags globally.

As you can imagine, this would be challenging as we normally try to avoid setting compile flags in CMakeLists.txt files, especially since we build many of the files for host and target. We also have a reasonably complex build structure with several static libraries, some of which are pulled in through FetchContent, further complicating any scheme for setting the optimization level at a granular level.

Any ideas/workarounds/hacks would be helpful as even if we were able to convince the IAR team to treat the -O flag like other compilers and take the last one, it takes a long time to get a new version that is safety-certified.

Wrap the compiler in your own shell script that filters -O flags.

1 Like

Ah, great idea, thanks for your help!