I am trying to retrieve the current compile flags from a target (which would be the default flags, from CMAKE_CXX_FLAGS_RELWITHDEBINFO
), to print them, and then replace one of those flags, and set the altered list of flags via target_compile_options
. However this post is only about the first part, getting the compile options. I've put the questions/issues in comments right in below code:
cmake_minimum_required(VERSION 3.13.2)
project(hello)
add_executable(hello WIN32 hello.cpp)
# target_compile_options(hello PUBLIC "$<$<CONFIG:RELWITHDEBINFO>:/Od>")
# (see below comment for this)
get_property(HELLO_GLOBAL_COMPILE_OPTIONS GLOBAL PROPERTY COMPILE_OPTIONS) # both GLOBAL and DIRECTORY return an empty variable
message(${HELLO_GLOBAL_COMPILE_OPTIONS})
# 1) => This returns an empty variable. Shouldn't this have been populated with the contents of CMAKE_CXX_FLAGS_RELWITHDEBINFO?
get_target_property(HELLO_COMPILE_OPTIONS hello COMPILE_OPTIONS)
get_target_property(HELLO_COMPILE_OPTIONS_INTERFACE hello INTERFACE_COMPILE_OPTIONS)
message(${HELLO_COMPILE_OPTIONS})
message(${HELLO_COMPILE_OPTIONS_INTERFACE})
# 2) => These both print HELLO_COMPILE_OPTIONS-NOTFOUND. Shouldn't they have been populated with the contents of CMAKE_CXX_FLAGS_RELWITHDEBINFO at this stage?
message(${CMAKE_CXX_FLAGS_RELWITHDEBINFO})
# This prints as expected, "/MD /Zi /O2 /Ob1 /DNDEBUG" (for MSVC).
3) If I uncomment the target_compile_options
on the 5th line, HELLO_COMPILE_OPTIONS
returns $<$<CONFIG:RELWITHDEBINFO>:/Od>
. But shouldn't it also return the "global" flags from CMAKE_CXX_FLAGS_RELWITHDEBINFO
?
Because the hello
executable is being compiled with both the global flags and the ones attached to the hello
target.
4) Maybe relevant: prop_tgt:COMPILE_OPTIONS documentation says "This property is initialized by the COMPILE_OPTIONS directory property when a target is created, and is used by the generators to set the options for the compiler." and prop_dir:COMPILE_OPTIONS documentation says "This property is used to initialize the COMPILE_OPTIONS target property when a target is created, which is used by the generators to set the options for the compiler.".
So what I want to do is the following. I want to get the cmake-default compile flags for the RELWITHDEBINFO
configuration. Then, for one specific target, I want to replace /O2
(the default) with /Od
, and set the compile options for that target to these modified ones.
I can't seem to find a way to do this without either modifying global flags (very bad practice), or adding compile options twice to my target.