project: Warn at top-level if cmake_minimum_required wasn't called

The top-level project() call will now issue an AUTHOR_WARNING if it
wasn't called after cmake_minimum_required().

Fixes: #24071
This commit is contained in:
friendlyanon
2022-11-11 16:15:30 +01:00
parent 23f3dd9f7c
commit 59573bf5b9
9 changed files with 30 additions and 2 deletions

View File

@@ -188,5 +188,6 @@ call exists, CMake will issue a warning and pretend there is a
Call the ``project()`` command near the top of the top-level Call the ``project()`` command near the top of the top-level
``CMakeLists.txt``, but *after* calling :command:`cmake_minimum_required`. ``CMakeLists.txt``, but *after* calling :command:`cmake_minimum_required`.
It is important to establish version and policy settings before invoking It is important to establish version and policy settings before invoking
other commands whose behavior they may affect. other commands whose behavior they may affect and for this reason the
``project()`` command will issue a warning if this order is not kept.
See also policy :policy:`CMP0000`. See also policy :policy:`CMP0000`.

View File

@@ -0,0 +1,6 @@
top-level-command-order
-----------------------
* The top-level :command:`project` call will now emit an author warning if the
documented command order in relation to :command:`cmake_minimum_required` is
not respected.

View File

@@ -34,6 +34,15 @@ bool cmProjectCommand(std::vector<std::string> const& args,
} }
cmMakefile& mf = status.GetMakefile(); cmMakefile& mf = status.GetMakefile();
if (mf.IsRootMakefile() &&
!mf.GetDefinition("CMAKE_MINIMUM_REQUIRED_VERSION")) {
mf.IssueMessage(
MessageType::AUTHOR_WARNING,
"cmake_minimum_required() should be called prior to this top-level "
"project() call. Please see the cmake-commands(7) manual for usage "
"documentation of both commands.");
}
if (!IncludeByVariable(status, "CMAKE_PROJECT_INCLUDE_BEFORE")) { if (!IncludeByVariable(status, "CMAKE_PROJECT_INCLUDE_BEFORE")) {
return false; return false;
} }

View File

@@ -1,3 +1,5 @@
set(CMAKE_MINIMUM_REQUIRED_VERSION "" CACHE STRING "")
# Used to verify that the values match what is passed via -S and -B, and are retained in cache. # Used to verify that the values match what is passed via -S and -B, and are retained in cache.
set(INITIAL_SOURCE_DIR "${CMAKE_SOURCE_DIR}" CACHE PATH "defined in initial.cmake") set(INITIAL_SOURCE_DIR "${CMAKE_SOURCE_DIR}" CACHE PATH "defined in initial.cmake")
set(INITIAL_BINARY_DIR "${CMAKE_BINARY_DIR}" CACHE PATH "defined in initial.cmake") set(INITIAL_BINARY_DIR "${CMAKE_BINARY_DIR}" CACHE PATH "defined in initial.cmake")

View File

@@ -1,3 +1,5 @@
cmake_minimum_required(VERSION 2.8.12) if(NOT "x${RunCMake_TEST}" STREQUAL "xNoMinimumRequired")
cmake_minimum_required(VERSION 2.8.12)
endif()
project(${RunCMake_TEST} NONE) project(${RunCMake_TEST} NONE)
include(${RunCMake_TEST}.cmake) include(${RunCMake_TEST}.cmake)

View File

@@ -0,0 +1,5 @@
CMake Warning \(dev\) at CMakeLists\.txt:[0-9]+ \(project\):
cmake_minimum_required\(\) should be called prior to this top-level project\(\)
call\. Please see the cmake-commands\(7\) manual for usage documentation of
both commands\.
This warning is for project developers\. Use -Wno-dev to suppress it\.$

View File

@@ -52,3 +52,5 @@ run_cmake(CMP0048-NEW)
run_cmake(CMP0096-WARN) run_cmake(CMP0096-WARN)
run_cmake(CMP0096-OLD) run_cmake(CMP0096-OLD)
run_cmake(CMP0096-NEW) run_cmake(CMP0096-NEW)
run_cmake(NoMinimumRequired)

View File

@@ -1,6 +1,7 @@
include(RunCMake) include(RunCMake)
set(RunCMake_TEST_OPTIONS set(RunCMake_TEST_OPTIONS
-DCMAKE_MINIMUM_REQUIRED_VERSION:STATIC=
# Simulate a previous CMake run that used `project(... VERSION ...)` # Simulate a previous CMake run that used `project(... VERSION ...)`
# in a non-injected call site. # in a non-injected call site.
-DCMAKE_PROJECT_VERSION:STATIC=1.2.3 -DCMAKE_PROJECT_VERSION:STATIC=1.2.3