ExternalWarningLevel automatically set

I was investigating why external warnings were turned off in our generated projects. I found out that imported libraries, i.e. add_library(library_name INTERFACE IMPORTED), automatically sets the SYSTEM property, which in turn has the effect that warnings from e.g. externally included header files are disabled (It sets the ExternalWarningLevel tag in the vcxproj file to TurnOffAllWarnings). In some cases I would like these warnings to be enabled, for example for template functions. Unfortunately, many cmake config files contain such add_library lines, and there is no easy way to force ‘/external:W4’ for a particular target, setting this in the target compile options does not have an effect.

Is there some way to force it anyway without going through each imported library and setting the SYSTEM property to FALSE? Perhaps some options when calling configure_package_config_file?

Btw, here is a minimal example showcasing this behavior:

project(test LANGUAGES CXX)

set(ALL_FILES
    test.cpp
)

add_executable(test ${ALL_FILES})

add_library(not_a_real_library INTERFACE IMPORTED)
set_target_properties(not_a_real_library PROPERTIES
    INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_CURRENT_SOURCE_DIR}
)

target_link_libraries(test PRIVATE not_a_real_library)

If you also set the SYSTEM property to FALSE, the ExternalWarningLevel tag will not be generated in the vcxproj file.

I have no direct experience with this, but this should be possible based on docs:

  • You can turn off the “imported implies SYSTEM” behaviour for a consuming target by setting its NO_SYSTEM_FROM_IMPORTED property, which can be pre-initialised by setting CMAKE_NO_SYSTEM_FROM_IMPORTED variable.
  • If you’re in control of the installation of the imported targets, you can set the EXPORT_NO_SYSTEM property on them to have them generated with SYSTEM set to false.