From 1527defbfe4ae9cb8f74d5954c790c403727063b Mon Sep 17 00:00:00 2001 From: Bartosz Kosiorek Date: Fri, 19 Apr 2019 10:44:41 +0200 Subject: cmMakefile: Enforce explicit use of project() command Fixes: 17714 --- Help/command/project.rst | 15 ++++++++------- Source/cmMakefile.cxx | 10 ++++++++++ Tests/RunCMake/project_injected/CMP0048-WARN-stderr.txt | 12 ++++++++++++ 3 files changed, 30 insertions(+), 7 deletions(-) create mode 100644 Tests/RunCMake/project_injected/CMP0048-WARN-stderr.txt diff --git a/Help/command/project.rst b/Help/command/project.rst index 7e33ccd..41e1112 100644 --- a/Help/command/project.rst +++ b/Help/command/project.rst @@ -112,12 +112,13 @@ Usage The top-level ``CMakeLists.txt`` file for a project must contain a literal, direct call to the :command:`project` command; loading one through the :command:`include` command is not sufficient. If no such -call exists CMake will implicitly add one to the top that enables the -default languages (``C`` and ``CXX``). +call exists, CMake will issue a warning and pretend there is a +``project(Project)`` at the top to enable the default languages +(``C`` and ``CXX``). .. note:: - Call the :command:`cmake_minimum_required` command at the beginning - of the top-level ``CMakeLists.txt`` file even before calling the - :command:`project()` command. It is important to establish version and - policy settings before invoking other commands whose behavior they - may affect. See also policy :policy:`CMP0000`. + Call the :command:`project()` command near the top of the top-level + ``CMakeLists.txt``, but *after* calling :command:`cmake_minimum_required`. + It is important to establish version and policy settings before invoking + other commands whose behavior they may affect. + See also policy :policy:`CMP0000`. diff --git a/Source/cmMakefile.cxx b/Source/cmMakefile.cxx index 3832427..6c390f7 100644 --- a/Source/cmMakefile.cxx +++ b/Source/cmMakefile.cxx @@ -1599,6 +1599,16 @@ void cmMakefile::Configure() } // if no project command is found, add one if (!hasProject) { + this->GetCMakeInstance()->IssueMessage( + MessageType::AUTHOR_WARNING, + "No project() command is present. The top-level CMakeLists.txt " + "file must contain a literal, direct call to the project() command. " + "Add a line of code such as\n" + " project(ProjectName)\n" + "near the top of the file, but after cmake_minimum_required().\n" + "CMake is pretending there is a \"project(Project)\" command on " + "the first line.", + this->Backtrace); cmListFileFunction project; project.Name.Lower = "project"; project.Arguments.emplace_back("Project", cmListFileArgument::Unquoted, diff --git a/Tests/RunCMake/project_injected/CMP0048-WARN-stderr.txt b/Tests/RunCMake/project_injected/CMP0048-WARN-stderr.txt new file mode 100644 index 0000000..fc1a02b --- /dev/null +++ b/Tests/RunCMake/project_injected/CMP0048-WARN-stderr.txt @@ -0,0 +1,12 @@ +^CMake Warning \(dev\) in CMakeLists.txt: + No project\(\) command is present. The top-level CMakeLists.txt file must + contain a literal, direct call to the project\(\) command. Add a line of + code such as + + project\(ProjectName\) + + near the top of the file, but after cmake_minimum_required\(\). + + CMake is pretending there is a "project\(Project\)" command on the first + line. +This warning is for project developers. Use -Wno-dev to suppress it.$ -- cgit v0.12