I have git submodules with CMakeLists.txt
files that are causing warnings due to CMP0048. The warnings look like this:
CMake Warning (dev) at submodule_directory/CMakeLists.txt:24 (project):
Policy CMP0048 is not set: project() command manages VERSION variables.
Run "cmake --help-policy CMP0048" for policy details. Use the cmake_policy
command to set the policy and suppress this warning.
The following variable(s) would be set to empty:
PROJECT_VERSION
PROJECT_VERSION_MAJOR
PROJECT_VERSION_MINOR
PROJECT_VERSION_PATCH
This warning is for project developers. Use -Wno-dev to suppress it.
I don't control these CMakeLists.txt
files and I don't want to fork so there's nothing to be done about this and I just want CMake to shut up about it. Using cmake_policy(SET CMP0048 OLD)
before adding the submodule directories doesn't solve this. (I guess project()
resets cmake policies?).
Is there anything I can I do about this?
The simplest solution if you don't care about enabling the policy is just to set CMAKE_POLICY_DEFAULT_CMP0048
to NEW
at the CMake command line. The following answer shows an alternative that lets you take a more piecemeal approach.
There is a more flexible solution in CMake 3.15+ using the CMAKE_PROJECT_INCLUDE_BEFORE
variable. See my example here:
$ tree
.
├── CMakeLists.txt
├── cmake
│ └── EnableCMP0048.cmake
└── subproj
└── CMakeLists.txt
I assume subproj/CMakeLists.txt
contains this:
cmake_minimum_required(VERSION 2.8.12) # Never do this
project(subproj)
In cmake/EnableCMP0048.cmake
, put:
cmake_policy(SET CMP0048 NEW)
In ./CMakeLists.txt
, put:
cmake_minimum_required(VERSION 3.15)
project(test VERSION 1.0.0)
set(CMAKE_PROJECT_INCLUDE_BEFORE "${CMAKE_CURRENT_SOURCE_DIR}/cmake/EnableCMP0048.cmake")
add_subdirectory(subproj)
unset(CMAKE_PROJECT_INCLUDE_BEFORE) # if you don't want to affect subsequent add_subdirectory calls.
Now this will enable CMP0048
and will clear the mentioned variables in the subproject. Given that in your question, you tried to fix this by modifying the policy, I assume that's okay. But if not, you could add another script, cmake/RestoreProjectVars.cmake
:
# Recall the top-level CMakeLists.txt had project(test).
set(PROJECT_VERSION "${test_VERSION}")
set(PROJECT_VERSION_MAJOR "${test_VERSION_MAJOR}")
set(PROJECT_VERSION_MINOR "${test_VERSION_MINOR}")
set(PROJECT_VERSION_PATCH "${test_VERSION_PATCH}")
and then set(CMAKE_PROJECT_INCLUDE "${CMAKE_CURRENT_SOURCE_DIR}/cmake/RestoreProjectVars.cmake")
before calling add_subdirectory
to restore those variables after the call.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With