Reputation: 1362
I have this in my top-level CMakeLists.txt
:
set( CMAKE_CXX_FLAGS_RELEASE "-O3 -march=native -mtune=native -DNDEBUG" )
Now, I realized that there is one target for which -march=native -mtune=native
should be excluded. What is the easiest way to do that? Obviously, I can set these options using target_compile_options()
for all targets except for the one, but surely there is a less verbose way?
Upvotes: 3
Views: 2413
Reputation: 18386
In modern CMake, manually manipulating the CMAKE_<LANG>_FLAGS_<CONFIG>
variables is often discouraged. It is better to set these using add_compile_options()
and add_compile_definitions()
, and you can use generator expressions to control which options apply to the Debug or Release configurations.
Note: With this add_compile_options()
syntax, separate the flags/options with a semicolon ;
since you are using GCC; if you are using Visual Studio's VC++ compiler, spaces should work fine.
add_compile_definitions("$<$<CONFIG:RELEASE>:NDEBUG>")
add_compile_options("$<$<CONFIG:RELEASE>:-O3;-march=native;-mtune=native>")
If you have some different Debug options also, you can add those:
add_compile_options(
"$<$<CONFIG:RELEASE>:-O3;-march=native;-mtune=native>"
"$<$<CONFIG:DEBUG>:-O2;-march=something-else>"
)
Then, your compilation flags will be accessible via the target property COMPILE_OPTIONS
, and you can remove the unwanted flags for a specific target using string(REPLACE ...)
:
get_target_property(MyLib_COMPILE_OPTIONS MyLib COMPILE_OPTIONS)
if(MyLib_COMPILE_OPTIONS)
string(REPLACE "-march=native" "" MyLib_COMPILE_OPTIONS "${MyLib_COMPILE_OPTIONS}")
string(REPLACE "-mtune=native" "" MyLib_COMPILE_OPTIONS "${MyLib_COMPILE_OPTIONS}")
set_target_properties(MyLib PROPERTIES COMPILE_OPTIONS "${MyLib_COMPILE_OPTIONS}")
endif()
Upvotes: 3