0

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.

Ela782
  • 5,041
  • 5
  • 53
  • 66
  • "But shouldn't it also return the "global" flags from `CMAKE_CXX_FLAGS_RELWITHDEBINFO`?" - No, it shouldn't. These "global" flags (together with `CMAKE_CXX_FLAGS`) are combined with ones, obtaining from the target's property, only when create the compiler's command line. However, the variable `CMAKE_CXX_FLAGS` is actually "per-directory". Probably, `CMAKE_CXX_FLAGS_RELWITHDEBINFO` has the same nature. So you may create your specific target in the separate directory, and adjust values of the variables. – Tsyvarev Dec 19 '18 at 08:11

1 Answers1

0

1) No, COMPILE_OPTIONS is a target property. There is no global version of it and is not found.

2) No, these are initialized with the directory property via the add_compile_options before defining the target. That is not in the script and doesn't use CMAKE_CXX_FLAGS_RELWITHDEBINFO at all. Because of this the target property didn't get defined.

3) No, Because CMAKE_CXX_FLAGS_RELWITHDEBINFO is a variable and not a property and there is no documentation that the properties get initialized by any variables.

4) That is exactly why the answers are No and it doesn't mention variables in initializing properties.

5) You can do this but is it necessary. When the generator gathers up all the different compile options its from both the variables and the properties. So all you would be doing is just adding redundant options and the new one. It doesn't replace the old ones.

This question seems to be related to the one asked yesterday. How to override optimization level flag in modern CMake

fdk1342
  • 3,274
  • 1
  • 16
  • 17