9

I am using a few external libraries that are included as git submodules using the add_subdirectory command. Some of them are using old versions of cmake and they're issuing warnings about policies CMP0048 and CMP0077.

Is there a way to turn off all cmake warnings for these libraries?

I've tried explicitly setting the policies to OLD before including the projects but it didn't help.

I'd prefer not to edit any files in the git submodule because then there would be extra steps when someone has to clone my project's repo and build it on their machine.

Michael
  • 1,263
  • 1
  • 12
  • 28

1 Answers1

1

I know this is a old questions, but in case someone stumbles upon the same problem, here are my findings.

Why didn't your attempts work?

Setting the policies before the add_subdirectory() call won't work, because cmake_minimum_required() implicitly set a specific set of policies, depending on the required version, which might override the set policies. Also, setting them explicitly to OLD has no effect even if they weren't overridden, since there is no distinction between being OLD because it is the default for that version or because it was explicitly set. A warning is issued in both cases, because as the documentation of every policy states, the old behavior is deprecated by definition.

Possible Solutions

I can think of the following solutions:

  1. Use the undocumented variable CMAKE_SUPPRESS_DEVELOPER_WARNINGS, which is also set when passing -Wno-dev on the command line (see cmake.cxx). Since you probably don't want this to have an influence on your project and disable all developer warning there as well, you should restore the original value after the add_subdirectory() call:

    set(no_dev_warnings_backup "$CACHE{CMAKE_SUPPRESS_DEVELOPER_WARNINGS}")
    set(CMAKE_SUPPRESS_DEVELOPER_WARNINGS ON CACHE INTERNAL "" FORCE)
    add_subdirectory(...)
    set(CMAKE_SUPPRESS_DEVELOPER_WARNINGS ${no_dev_warnings_backup} CACHE INTERNAL "" FORCE)
    

    The variable might be undocumented and therefore not that reliable as documented variables, but I wouldn't worry too much about it. It has been around since the CMake 2.x days and is unlikely to go anywhere.

  2. Set CMAKE_POLICY_DEFAULT_CMP<NNNN> to NEW:

    set(CMAKE_POLICY_DEFAULT_CMP0048 "NEW")
    set(CMAKE_POLICY_DEFAULT_CMP0077 "NEW")
    add_subdirectory(...)
    

    Note that this doesn't just disable the warnings, but alters how CMake processes the affected CMakeLists.txt files. This might break the build of your external libraries!

  3. Use CMAKE_PROJECT__INCLUDE or one of its related variables to inject some CMake code into the build of the external libraries. The injected code could then call cmake_policy(SET CMP<NNNN> NEW) to override the policy. Note that just like the 2. solution, this could break the build!

  4. Instead of a git submodule, you could use FetchContent which fetches the external libraries and calls add_subdirectory() one them. Since FetchContent is quite related to ExternalProject it allows to patch the retrieved sources, so you can fix/customize the build of the external libraries:

    include(FetchContent)
    find_package(Patch REQUIRED)
    FetchContent_Declare(myextlib
       GIT_REPOSITORY <url>
       GIT_TAG <tag_or_hash>
       PATCH_COMMAND "${Patch_EXECUTABLE}" -p1 < path/to/myextlib.patch
    )
    FetchContent_MakeAvailable(myextlib)
    

    More on diffs and patches can be found here. Alternatively, you also use git apply as patch command, since you use Git anyway.

Recommendations

I would recommend either 1 or 4. Approach 4 is the most flexible, since you could improve the build in other aspects as well, if necessary, but it is also the most involved one, since whenever you bump the version of your external libraries you also need to take care of your patches and check whether they still work etc. In case you don't need that flexibility and only care about disabling warnings, approach 1 would be the most straight forward solution.

petwu
  • 241
  • 2
  • 5