How to turn off warning flags for project added by FetchContent_Declare?

In our project, we have a global done at the root directory

add_compile_options(
    -Wundef
    # (...)
)

However, a problem appeared with FetchContent for Google Test. The build this creates “inherits” the -Wundef. However, Google Test doesn’t support this flag - warnings are generated. And since we are also having -Werror it breaks the build.

I’m trying to work around it. As final solutions, I think we should do one of two approaches:

  1. Create a CMake function called for all our targets. It would use target_compile_options to add those flags. This way only our own targets get them. No global defaults. While we still have only a single place to list all the options.
  2. Ensure that the add_compile_options is called on a lower directory, where only our code will be located, and hence we will not affect anything taken by FetchContent at the root directory.

However, both seem like a bigger effort and I was looking for something which could unblock me for now. Hence, I tried the following:

include(FetchContent)

FetchContent_Declare(
    googletest
    GIT_REPOSITORY # (...)
    GIT_TAG # (...)
)

FetchContent_MakeAvailable(googletest)

set_property(
    DIRECTORY ${googletest_SOURCE_DIR}
    APPEND
    PROPERTY COMPILE_OPTIONS -Wno-undef
)

but this doesn’t help. I’m still seeing the warnings (turned into errors with -Werror).

Is it possible to change the properties of projects handled by FetchContent? I guess I could try to affect targets there perhaps, but for this, I would need to iterate over them (more complex CMake code) or to hardcode their list.

I ended up with the following ugliness:

include(FetchContent)

FetchContent_Declare(
    googletest
    GIT_REPOSITORY # (...)
    GIT_TAG # (...)
)

get_property(
    compile_options
    DIRECTORY
    PROPERTY COMPILE_OPTIONS
)

set_property(
    DIRECTORY
    APPEND
    PROPERTY COMPILE_OPTIONS -Wno-undef
)

FetchContent_MakeAvailable(googletest)

set_property(
    DIRECTORY
    PROPERTY COMPILE_OPTIONS ${compile_options}
)

unset(compile_options)

where I save the current state, modify it (by appending -Wno-undef), call FetchContent_MakeAvailable, and finally, restore the previous value. It works but looks poorly.

Is there any better way?

What would be the preferred way anyway? (I’m now on CMake 3.17.3.)

The COMPILE_OPTIONS directory property only affects targets created after that directory property is set. In your first example, you are pulling in googletest before you set this property, so it won’t have any effect. Your second example fixes this, which is why it works where the first one doesn’t.

You could try removing the -Wundef option from the COMPILE_OPTIONS directory property instead of appending -Wno-undef, but it’s about the same work as your second example. If you were doing this in its own directory scope, you wouldn’t need to worry about restoring the property again, which would simplify things for you I guess.