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:
- 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. - 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 byFetchContent
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.)