From 72e7305c3e27a53a9a7e019b9e5e973e342ae510 Mon Sep 17 00:00:00 2001 From: Matthew Woehlke Date: Wed, 6 May 2026 13:06:18 -0400 Subject: [PATCH] Diagnostics: Handle CMAKE_{WARN,ERROR}_DEPRECATED Update documentation for aforementioned variables, as their use is deprecated under the new diagnostic system. Introduce a policy which reintroduces their use to control specific deprecation diagnostics if the policy is not NEW. Note that this means there is NO synchronization between the variables and the new diagnostic state. --- Help/command/message.rst | 6 +- Help/diagnostic/CMD_DEPRECATED.rst | 8 +++ Help/manual/cmake-policies.7.rst | 1 + Help/manual/cmake-variables.7.rst | 4 +- Help/policy/CMP0218.rst | 35 +++++++++ Help/variable/CMAKE_ERROR_DEPRECATED.rst | 27 ++++++- Help/variable/CMAKE_WARN_DEPRECATED.rst | 29 ++++++-- Source/cmMakefile.cxx | 28 ++++++++ Source/cmMessageCommand.cxx | 36 +++++++++- Source/cmPolicies.h | 6 +- Source/cmake.cxx | 54 ++++++++------ Tests/RunCMake/CMP0218/CMP0218-NEW.cmake | 72 +++++++++++++++++++ Tests/RunCMake/CMP0218/CMP0218-OLD-result.txt | 1 + Tests/RunCMake/CMP0218/CMP0218-OLD-stderr.txt | 14 ++++ Tests/RunCMake/CMP0218/CMP0218-OLD.cmake | 15 ++++ .../RunCMake/CMP0218/CMP0218-WARN-stderr.txt | 31 ++++++++ Tests/RunCMake/CMP0218/CMP0218-WARN.cmake | 8 +++ Tests/RunCMake/CMP0218/CMakeLists.txt | 3 + Tests/RunCMake/CMP0218/ExpectError-stderr.txt | 1 + Tests/RunCMake/CMP0218/ExpectError.cmake | 4 ++ .../RunCMake/CMP0218/ExpectIgnore-stderr.txt | 1 + Tests/RunCMake/CMP0218/ExpectIgnore.cmake | 4 ++ Tests/RunCMake/CMP0218/RunCMakeTest.cmake | 8 +++ Tests/RunCMake/CMakeLists.txt | 2 + Tests/RunCMake/GenerateExportHeader/GEH.cmake | 1 + Tests/RunCMake/message/CMakeLists.txt | 2 +- 26 files changed, 363 insertions(+), 38 deletions(-) create mode 100644 Help/policy/CMP0218.rst create mode 100644 Tests/RunCMake/CMP0218/CMP0218-NEW.cmake create mode 100644 Tests/RunCMake/CMP0218/CMP0218-OLD-result.txt create mode 100644 Tests/RunCMake/CMP0218/CMP0218-OLD-stderr.txt create mode 100644 Tests/RunCMake/CMP0218/CMP0218-OLD.cmake create mode 100644 Tests/RunCMake/CMP0218/CMP0218-WARN-stderr.txt create mode 100644 Tests/RunCMake/CMP0218/CMP0218-WARN.cmake create mode 100644 Tests/RunCMake/CMP0218/CMakeLists.txt create mode 100644 Tests/RunCMake/CMP0218/ExpectError-stderr.txt create mode 100644 Tests/RunCMake/CMP0218/ExpectError.cmake create mode 100644 Tests/RunCMake/CMP0218/ExpectIgnore-stderr.txt create mode 100644 Tests/RunCMake/CMP0218/ExpectIgnore.cmake create mode 100644 Tests/RunCMake/CMP0218/RunCMakeTest.cmake diff --git a/Help/command/message.rst b/Help/command/message.rst index e8a4ea000c..a25a5a6c04 100644 --- a/Help/command/message.rst +++ b/Help/command/message.rst @@ -44,12 +44,10 @@ influences the way the message is handled: CMake Warning, continue processing. ``AUTHOR_WARNING`` - CMake Warning (dev), continue processing. + CMake :diagnostic:`CMD_AUTHOR` diagnostic. ``DEPRECATION`` - CMake Deprecation Error or Warning if variable - :variable:`CMAKE_ERROR_DEPRECATED` or :variable:`CMAKE_WARN_DEPRECATED` - is enabled, respectively, else no message. + CMake :diagnostic:`CMD_DEPRECATED` diagnostic. (none) or ``NOTICE`` Important message printed to stderr to attract user's attention. diff --git a/Help/diagnostic/CMD_DEPRECATED.rst b/Help/diagnostic/CMD_DEPRECATED.rst index dfb23d59fc..85b956423f 100644 --- a/Help/diagnostic/CMD_DEPRECATED.rst +++ b/Help/diagnostic/CMD_DEPRECATED.rst @@ -7,3 +7,11 @@ CMD_DEPRECATED Warn about use of a deprecated function or package. This is the category triggered by :command:`message(DEPRECATION)`. + + .. note:: + + If :policy:`CMP0218` is not set to ``NEW``, :command:`message(DEPRECATION)` + invocations, along with builtin deprecation messages that existed prior to + CMake 4.4, will ignore this diagnostic state and will instead use the + :variable:`CMAKE_WARN_DEPRECATED` and :variable:`CMAKE_ERROR_DEPRECATED` + variables to determine the severity of deprecation messages. diff --git a/Help/manual/cmake-policies.7.rst b/Help/manual/cmake-policies.7.rst index 2ab7290de7..6728fee232 100644 --- a/Help/manual/cmake-policies.7.rst +++ b/Help/manual/cmake-policies.7.rst @@ -100,6 +100,7 @@ Policies Introduced by CMake 4.4 .. toctree:: :maxdepth: 1 + CMP0218: The CMAKE_WARN_DEPRECATED and CMAKE_ERROR_DEPRECATED variables are ignored. CMP0217: The MACROS directory property does not exist anymore. CMP0216: Swift targets have a default project name. CMP0215: Ninja generators emit Swift modules separately from compilation. diff --git a/Help/manual/cmake-variables.7.rst b/Help/manual/cmake-variables.7.rst index 71318ddc4b..9b9208f638 100644 --- a/Help/manual/cmake-variables.7.rst +++ b/Help/manual/cmake-variables.7.rst @@ -224,7 +224,6 @@ Variables that Change Behavior /variable/CMAKE_ECLIPSE_MAKE_ARGUMENTS /variable/CMAKE_ECLIPSE_RESOURCE_ENCODING /variable/CMAKE_ECLIPSE_VERSION - /variable/CMAKE_ERROR_DEPRECATED /variable/CMAKE_ERROR_ON_ABSOLUTE_INSTALL_DESTINATION /variable/CMAKE_EXECUTE_PROCESS_COMMAND_ECHO /variable/CMAKE_EXECUTE_PROCESS_COMMAND_ERROR_IS_FATAL @@ -318,7 +317,6 @@ Variables that Change Behavior /variable/CMAKE_TLS_VERIFY /variable/CMAKE_TLS_VERSION /variable/CMAKE_USER_MAKE_RULES_OVERRIDE - /variable/CMAKE_WARN_DEPRECATED /variable/CMAKE_WARN_ON_ABSOLUTE_INSTALL_DESTINATION /variable/CMAKE_XCODE_GENERATE_SCHEME /variable/CMAKE_XCODE_GENERATE_TOP_LEVEL_PROJECT_ONLY @@ -878,8 +876,10 @@ Deprecated Variables that Change Behavior /variable/CMAKE_AUTOMOC_RELAXED_MODE /variable/CMAKE_BACKWARDS_COMPATIBILITY + /variable/CMAKE_ERROR_DEPRECATED /variable/CMAKE_FIND_PACKAGE_NO_PACKAGE_REGISTRY /variable/CMAKE_FIND_PACKAGE_NO_SYSTEM_PACKAGE_REGISTRY + /variable/CMAKE_WARN_DEPRECATED Deprecated Variables that Describe the System ============================================= diff --git a/Help/policy/CMP0218.rst b/Help/policy/CMP0218.rst new file mode 100644 index 0000000000..2cba81c3ff --- /dev/null +++ b/Help/policy/CMP0218.rst @@ -0,0 +1,35 @@ +CMP0218 +------- + +.. versionadded:: 4.4 + +The :variable:`CMAKE_WARN_DEPRECATED` and :variable:`CMAKE_ERROR_DEPRECATED` +variables are ignored. + +In CMake 4.3 and below, these variables controlled whether deprecation messages +resulted in warnings, errors, or were suppressed. + +CMake 4.4 introduced a new, unified system for managing +:manual:`diagnostics ` that prefers the +:command:`cmake_diagnostic` command for manipulating diagnostic actions. + +This policy provides compatibility with projects that relied on these variables +and have not been updated to be aware of the change. + +The ``OLD`` behavior for this policy is to use the variables to control +deprecation messages which existed prior to CMake 4.4. + +The ``NEW`` behavior does not ascribe any special meaning to these variables +and uses the :diagnostic:`CMD_DEPRECATED` diagnostic state to control all +deprecation messages. + +Any deprecation messages introduced in CMake 4.4 or later are controlled by the +:diagnostic:`CMD_DEPRECATED` diagnostic regardless of the setting of this +policy. + +.. |INTRODUCED_IN_CMAKE_VERSION| replace:: 4.4 +.. |WARNS_OR_DOES_NOT_WARN| replace:: + warns when either variable is accessed or modified +.. include:: include/STANDARD_ADVICE.rst + +.. include:: include/DEPRECATED.rst diff --git a/Help/variable/CMAKE_ERROR_DEPRECATED.rst b/Help/variable/CMAKE_ERROR_DEPRECATED.rst index 12aa50bf19..0bf7d55304 100644 --- a/Help/variable/CMAKE_ERROR_DEPRECATED.rst +++ b/Help/variable/CMAKE_ERROR_DEPRECATED.rst @@ -5,5 +5,28 @@ CMAKE_ERROR_DEPRECATED Whether to issue errors for deprecated functionality. -If ``TRUE``, use of deprecated functionality will issue fatal errors. -If this variable is not set, CMake behaves as if it were set to ``FALSE``. +This is a deprecated mechanism for interacting with the +:diagnostic:`CMD_DEPRECATED` :manual:`diagnostic `. +If policy :policy:`CMP0218` is set to ``NEW``, this variable is ignored. + +If policy :policy:`CMP0218` is not set to ``NEW``, the :command:`message` +command's ``DEPRECATION`` message type will use +:variable:`CMAKE_WARN_DEPRECATED` and ``CMAKE_ERROR_DEPRECATED`` +to determine the severity of a deprecation diagnostic. The severity will be: + +* ``IGNORE``, if ``CMAKE_WARN_DEPRECATED`` is ``OFF`` and + ``CMAKE_ERROR_DEPRECATED`` is unset or ``OFF``. + +* ``WARN``, if ``CMAKE_WARN_DEPRECATED`` is unset or ``ON`` and + ``CMAKE_ERROR_DEPRECATED`` is unset or ``OFF``. + +* ``FATAL_ERROR`` if ``CMAKE_ERROR_DEPRECATED`` is ``ON``. + +Setting ``CMAKE_ERROR_DEPRECATED`` in the cache will alter the default state of +the :diagnostic:`CMD_DEPRECATED` :manual:`diagnostic `; +however, the :option:`-W[no-][error=]deprecated ` option, and +:preset:`warnings.deprecated ` and/or +:preset:`errors.deprecated ` preset +fields, will take precedence. CMake will also update the cached value of +``CMAKE_ERROR_DEPRECATED`` to reflect the actual state of the diagnostic as of +the start of script execution. diff --git a/Help/variable/CMAKE_WARN_DEPRECATED.rst b/Help/variable/CMAKE_WARN_DEPRECATED.rst index ffe24bdc9d..482b982a44 100644 --- a/Help/variable/CMAKE_WARN_DEPRECATED.rst +++ b/Help/variable/CMAKE_WARN_DEPRECATED.rst @@ -5,9 +5,28 @@ CMAKE_WARN_DEPRECATED Whether to issue warnings for deprecated functionality. -If not ``FALSE``, use of deprecated functionality will issue warnings. -If this variable is not set, CMake behaves as if it were set to ``TRUE``. +This is a deprecated mechanism for interacting with the +:diagnostic:`CMD_DEPRECATED` :manual:`diagnostic `. +If policy :policy:`CMP0218` is set to ``NEW``, this variable is ignored. -When running :manual:`cmake(1)`, this option can be enabled with the -:option:`-Wdeprecated ` option, or disabled with the -:option:`-Wno-deprecated ` option. +If policy :policy:`CMP0218` is not set to ``NEW``, the :command:`message` +command's ``DEPRECATION`` message type will use +``CMAKE_WARN_DEPRECATED`` and :variable:`CMAKE_ERROR_DEPRECATED` +to determine the severity of a deprecation diagnostic. The severity will be: + +* ``IGNORE``, if ``CMAKE_WARN_DEPRECATED`` is ``OFF`` and + ``CMAKE_ERROR_DEPRECATED`` is unset or ``OFF``. + +* ``WARN``, if ``CMAKE_WARN_DEPRECATED`` is unset or ``ON`` and + ``CMAKE_ERROR_DEPRECATED`` is unset or ``OFF``. + +* ``FATAL_ERROR`` if ``CMAKE_ERROR_DEPRECATED`` is ``ON``. + +Setting ``CMAKE_WARN_DEPRECATED`` in the cache will alter the default state of +the :diagnostic:`CMD_DEPRECATED` :manual:`diagnostic `; +however, the :option:`-W[no-][error=]deprecated ` option, and +:preset:`warnings.deprecated ` and/or +:preset:`errors.deprecated ` preset +fields, will take precedence. CMake will also update the cached value of +``CMAKE_WARN_DEPRECATED`` to reflect the actual state of the diagnostic as of +the start of script execution. diff --git a/Source/cmMakefile.cxx b/Source/cmMakefile.cxx index d7553602bb..cc2c759c95 100644 --- a/Source/cmMakefile.cxx +++ b/Source/cmMakefile.cxx @@ -4136,6 +4136,34 @@ bool cmMakefile::SetPolicy(cmPolicies::PolicyID id, id == cmPolicies::CMP0126 || id == cmPolicies::CMP0128 || id == cmPolicies::CMP0136 || id == cmPolicies::CMP0141 || id == cmPolicies::CMP0155))) { + std::unique_ptr ps; + std::unique_ptr ds; + + cmPolicies::PolicyStatus const cmp0218 = + this->GetPolicyStatus(cmPolicies::CMP0218); + if (cmp0218 != cmPolicies::NEW) { + if (cmp0218 != cmPolicies::OLD) { + // Suppress warnings about using old variables. + ps = cm::make_unique(this); + this->SetPolicy(cmPolicies::CMP0218, cmPolicies::OLD); + } + + ds = cm::make_unique(this); + + // Use old variables to determine diagnostic action. + cmValue const warn = this->GetDefinition("CMAKE_WARN_DEPRECATED"); + if (warn.IsSet() && !warn.IsOn()) { + this->SetDiagnostic(cmDiagnostics::CMD_DEPRECATED, + cmDiagnostics::Ignore); + } else if (this->IsOn("CMAKE_ERROR_DEPRECATED")) { + this->SetDiagnostic(cmDiagnostics::CMD_DEPRECATED, + cmDiagnostics::SendError); + } else { + this->SetDiagnostic(cmDiagnostics::CMD_DEPRECATED, + cmDiagnostics::Warn); + } + } + this->IssueDiagnostic(cmDiagnostics::CMD_DEPRECATED, cmPolicies::GetPolicyDeprecatedWarning(id)); } diff --git a/Source/cmMessageCommand.cxx b/Source/cmMessageCommand.cxx index 8ce1a7c5ed..67b5af580a 100644 --- a/Source/cmMessageCommand.cxx +++ b/Source/cmMessageCommand.cxx @@ -3,9 +3,9 @@ #include "cmMessageCommand.h" #include -#include #include +#include #include #include @@ -16,9 +16,11 @@ #include "cmMakefile.h" #include "cmMessageType.h" #include "cmMessenger.h" +#include "cmPolicies.h" #include "cmRange.h" #include "cmStringAlgorithms.h" #include "cmSystemTools.h" +#include "cmValue.h" #include "cmake.h" #ifdef CMake_ENABLE_DEBUGGER @@ -96,6 +98,7 @@ bool cmMessageCommand(std::vector const& args, auto i = args.cbegin(); + std::unique_ptr ds; auto category = cmDiagnostics::CMD_NONE; auto type = MessageType::MESSAGE; auto fatal = false; @@ -116,7 +119,7 @@ bool cmMessageCommand(std::vector const& args, ++i; } else if (*i == "AUTHOR_WARNING") { category = cmDiagnostics::CMD_AUTHOR; - switch (mf.GetDiagnosticAction(cmDiagnostics::CMD_AUTHOR)) { + switch (mf.GetDiagnosticAction(category)) { case cmDiagnostics::Ignore: return true; case cmDiagnostics::FatalError: @@ -166,7 +169,34 @@ bool cmMessageCommand(std::vector const& args, ++i; } else if (*i == "DEPRECATION") { category = cmDiagnostics::CMD_DEPRECATED; - switch (mf.GetDiagnosticAction(cmDiagnostics::CMD_DEPRECATED)) { + + cmPolicies::PolicyStatus const cmp0218 = + mf.GetPolicyStatus(cmPolicies::CMP0218); + if (cmp0218 != cmPolicies::NEW) { + std::unique_ptr ps; + + if (cmp0218 != cmPolicies::OLD) { + // Suppress warnings about using old variables. + ps = cm::make_unique(&mf); + mf.SetPolicy(cmPolicies::CMP0218, cmPolicies::OLD); + } + + ds = cm::make_unique(&mf); + + // Use old variables to determine diagnostic action. + if (mf.IsOn("CMAKE_ERROR_DEPRECATED")) { + mf.SetDiagnostic(category, cmDiagnostics::FatalError); + } else { + cmValue const warn = mf.GetDefinition("CMAKE_WARN_DEPRECATED"); + if (warn.IsSet() && !warn.IsOn()) { + mf.SetDiagnostic(category, cmDiagnostics::Ignore); + } else { + mf.SetDiagnostic(category, cmDiagnostics::Warn); + } + } + } + + switch (mf.GetDiagnosticAction(category)) { case cmDiagnostics::Ignore: return true; case cmDiagnostics::FatalError: diff --git a/Source/cmPolicies.h b/Source/cmPolicies.h index 1ef7f104fd..a7aab42e29 100644 --- a/Source/cmPolicies.h +++ b/Source/cmPolicies.h @@ -650,7 +650,11 @@ class cmMakefile; 0, WARN) \ SELECT(POLICY, CMP0217, \ "The MACROS directory property does not exist anymore.", 4, 4, 0, \ - WARN) + WARN) \ + SELECT(POLICY, CMP0218, \ + "The CMAKE_WARN_DEPRECATED and CMAKE_ERROR_DEPRECATED variables " \ + "are ignored.", \ + 4, 4, 0, WARN) #define CM_SELECT_ID(F, A1, A2, A3, A4, A5, A6) F(A1) #define CM_FOR_EACH_POLICY_ID(POLICY) \ diff --git a/Source/cmake.cxx b/Source/cmake.cxx index 6a4bab6d17..ae17b90ea8 100644 --- a/Source/cmake.cxx +++ b/Source/cmake.cxx @@ -55,6 +55,7 @@ #include "cmLocalGenerator.h" #include "cmMakefile.h" #include "cmMessenger.h" +#include "cmPolicies.h" #include "cmState.h" #include "cmStateDirectory.h" #include "cmStringAlgorithms.h" @@ -174,6 +175,17 @@ void cmWarnUnusedCliWarning(std::string const& variable, cmake* cm = reinterpret_cast(ctx); cm->MarkCliAsUsed(variable); } + +void cmDeprecatedWatch(std::string const& /*unused*/, + cmVariableWatch::AccessType /*unused*/, + void* /*unused*/, char const* /*unused*/, + cmMakefile const* mf) +{ + if (mf->GetPolicyStatus(cmPolicies::CMP0218) == cmPolicies::WARN) { + mf->IssueDiagnostic(cmDiagnostics::CMD_AUTHOR, + cmPolicies::GetPolicyWarning(cmPolicies::CMP0218)); + } +} #endif void warnDeprecated(cm::string_view oldOption, cm::string_view newOption) @@ -830,6 +842,15 @@ void cmake::ProcessCacheArg(std::string const& var, std::string const& value, } } + // See also CMP0218. + if (var == "CMAKE_WARN_DEPRECATED") { + std::cerr << "The CMAKE_WARN_DEPRECATED variable is deprecated. " + "Use -W[no-]deprecated instead.\n"_s; + } else if (var == "CMAKE_ERROR_DEPRECATED") { + std::cerr << "The CMAKE_ERROR_DEPRECATED variable is deprecated. " + "Use -W[no-]error=deprecated instead.\n"_s; + } + this->AddCacheEntry( var, value, "No help, variable specified on the command line.", type); @@ -2439,8 +2460,6 @@ int cmake::Configure() cmValue cachedWarnDeprecated = this->State->GetCacheEntryValue("CMAKE_WARN_DEPRECATED"); if (cachedWarnDeprecated) { - std::cerr << "The CMAKE_WARN_DEPRECATED variable is deprecated. " - "Use CMAKE_DIAGNOSTIC_INIT instead.\n"_s; if (cachedWarnDeprecated.IsOn()) { deprecated = cmDiagnostics::Warn; } else { @@ -2451,8 +2470,6 @@ int cmake::Configure() cmValue cachedErrorDeprecated = this->State->GetCacheEntryValue("CMAKE_ERROR_DEPRECATED"); if (cachedErrorDeprecated) { - std::cerr << "The CMAKE_ERROR_DEPRECATED variable is deprecated. " - "Use CMAKE_DIAGNOSTIC_INIT instead.\n"_s; if (cachedErrorDeprecated.IsOn()) { deprecated = cmDiagnostics::SendError; } @@ -2476,22 +2493,16 @@ int cmake::Configure() cmDiagnostics::GetActionString(action))); if (category == cmDiagnostics::CMD_DEPRECATED) { - // Set deprecated CMAKE_{WARN,ERROR}_DEPRECATED, but only in the cache, - // and only if they were already set in the cache. - if (cachedWarnDeprecated) { - std::string const value = - (action >= cmDiagnostics::Warn ? "ON" : "OFF"); - this->AddCacheEntry("CMAKE_WARN_DEPRECATED", value, - "Deprecated. Use CMAKE_DIAGNOSTIC_INIT instead.", - cmStateEnums::INTERNAL); - } - if (cachedErrorDeprecated) { - std::string const value = - (action >= cmDiagnostics::SendError ? "ON" : "OFF"); - this->AddCacheEntry("CMAKE_ERROR_DEPRECATED", value, - "Deprecated. Use CMAKE_DIAGNOSTIC_INIT instead.", - cmStateEnums::INTERNAL); - } + std::string const warnValue = + (action >= cmDiagnostics::Warn ? "ON" : "OFF"); + this->AddCacheEntry("CMAKE_WARN_DEPRECATED", warnValue, + "Deprecated. Use -W[no-]deprecated instead.", + cmStateEnums::INTERNAL); + std::string const errorValue = + (action >= cmDiagnostics::SendError ? "ON" : "OFF"); + this->AddCacheEntry("CMAKE_ERROR_DEPRECATED", errorValue, + "Deprecated. Use -W[no-]error=deprecated instead.", + cmStateEnums::INTERNAL); } } @@ -3057,6 +3068,9 @@ int cmake::Run(std::vector const& args, bool noconfigure) if (!sarifLogFileWriter.ConfigureForCMakeRun(*this)) { return -1; } + + this->VariableWatch->AddWatch("CMAKE_WARN_DEPRECATED", cmDeprecatedWatch); + this->VariableWatch->AddWatch("CMAKE_ERROR_DEPRECATED", cmDeprecatedWatch); #endif // Log the trace format version to the desired output diff --git a/Tests/RunCMake/CMP0218/CMP0218-NEW.cmake b/Tests/RunCMake/CMP0218/CMP0218-NEW.cmake new file mode 100644 index 0000000000..035be35fb1 --- /dev/null +++ b/Tests/RunCMake/CMP0218/CMP0218-NEW.cmake @@ -0,0 +1,72 @@ +cmake_policy(SET CMP0218 NEW) + +function(expect NAME ACTUAL EXPECTED) + if(NOT "${ACTUAL}" STREQUAL "${EXPECTED}") + message(SEND_ERROR "${NAME} is '${ACTUAL}'; should be '${EXPECTED}'") + endif() +endfunction() + +if(DEFINED CMAKE_WARN_DEPRECATED) + message(SEND_ERROR "CMAKE_WARN_DEPRECATED should not be defined") +endif() + +if(DEFINED CMAKE_ERROR_DEPRECATED) + message(SEND_ERROR "CMAKE_ERROR_DEPRECATED should not be defined") +endif() + +cmake_diagnostic(GET CMD_DEPRECATED action) +if(NOT "${action}" STREQUAL "WARN") + message(SEND_ERROR "CMD_DEPRECATED is ${action}; should be WARN") +endif() + +# ----------------------------------------------------------------------------- +# Setting CMAKE_WARN_DEPRECATED should have no effect on CMD_DEPRECATED +block() + set(CMAKE_WARN_DEPRECATED OFF) + + cmake_diagnostic(GET CMD_DEPRECATED action) + if(NOT "${action}" STREQUAL "WARN") + message(SEND_ERROR "CMD_DEPRECATED is ${action}; should be WARN") + endif() +endblock() + +# ----------------------------------------------------------------------------- +# Setting CMAKE_ERROR_DEPRECATED should have no effect on CMD_DEPRECATED +block() + set(CMAKE_ERROR_DEPRECATED ON) + + cmake_diagnostic(GET CMD_DEPRECATED action) + if(NOT "${action}" STREQUAL "WARN") + message(SEND_ERROR "CMD_DEPRECATED is ${action}; should be WARN") + endif() +endblock() + +# ----------------------------------------------------------------------------- +# Changing CMD_DEPRECATED should have no effect on variables +block() + cmake_diagnostic(SET CMD_DEPRECATED IGNORE) + + if(DEFINED CMAKE_WARN_DEPRECATED) + message(SEND_ERROR "CMAKE_WARN_DEPRECATED should not be defined") + endif() + + if(DEFINED CMAKE_ERROR_DEPRECATED) + message(SEND_ERROR "CMAKE_ERROR_DEPRECATED should not be defined") + endif() + + expect(CMAKE_WARN_DEPRECATED "${CMAKE_WARN_DEPRECATED}" "") + expect(CMAKE_ERROR_DEPRECATED "${CMAKE_ERROR_DEPRECATED}" "") + + cmake_diagnostic(SET CMD_DEPRECATED SEND_ERROR) + + if(DEFINED CMAKE_WARN_DEPRECATED) + message(SEND_ERROR "CMAKE_WARN_DEPRECATED should not be defined") + endif() + + if(DEFINED CMAKE_ERROR_DEPRECATED) + message(SEND_ERROR "CMAKE_ERROR_DEPRECATED should not be defined") + endif() + + expect(CMAKE_WARN_DEPRECATED "${CMAKE_WARN_DEPRECATED}" "") + expect(CMAKE_ERROR_DEPRECATED "${CMAKE_ERROR_DEPRECATED}" "") +endblock() diff --git a/Tests/RunCMake/CMP0218/CMP0218-OLD-result.txt b/Tests/RunCMake/CMP0218/CMP0218-OLD-result.txt new file mode 100644 index 0000000000..d00491fd7e --- /dev/null +++ b/Tests/RunCMake/CMP0218/CMP0218-OLD-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/CMP0218/CMP0218-OLD-stderr.txt b/Tests/RunCMake/CMP0218/CMP0218-OLD-stderr.txt new file mode 100644 index 0000000000..2e27a1c09b --- /dev/null +++ b/Tests/RunCMake/CMP0218/CMP0218-OLD-stderr.txt @@ -0,0 +1,14 @@ +CMake Warning \(deprecated\) at [^ +]+/Tests/RunCMake/CMP0218/CMP0218-OLD\.cmake:[0-9]+ \(cmake_policy\): + The OLD behavior for policy CMP0153 will be removed from a future version + of CMake\. + + The cmake-policies\(7\) manual explains that the OLD behaviors of all + policies are deprecated and that a policy should be set to OLD only under + specific short-term circumstances\. Projects should be ported to the NEW + behavior and not rely on setting a policy to OLD\. + + +CMake Error \(deprecated\) at [^ +]+/Tests/RunCMake/CMP0218/CMP0218-OLD\.cmake:[0-9]+ \(message\): + Test diff --git a/Tests/RunCMake/CMP0218/CMP0218-OLD.cmake b/Tests/RunCMake/CMP0218/CMP0218-OLD.cmake new file mode 100644 index 0000000000..83043f3032 --- /dev/null +++ b/Tests/RunCMake/CMP0218/CMP0218-OLD.cmake @@ -0,0 +1,15 @@ +cmake_diagnostic(SET CMD_DEPRECATED IGNORE) + +# Note: We're using the deprecation warning from cmMakefile::SetPolicy as a +# test, which requires use of policies within the deprecation window (as of +# CMake 4.4, CMP0066...CMP0155). These will likely need to be rotated to newer +# policies when that window changes. + +cmake_policy(SET CMP0218 NEW) +cmake_policy(SET CMP0152 OLD) # should be silent + +cmake_policy(SET CMP0218 OLD) +cmake_policy(SET CMP0153 OLD) # should NOT be silent + +set(CMAKE_ERROR_DEPRECATED ON) +message(DEPRECATION "Test") # should be error diff --git a/Tests/RunCMake/CMP0218/CMP0218-WARN-stderr.txt b/Tests/RunCMake/CMP0218/CMP0218-WARN-stderr.txt new file mode 100644 index 0000000000..183c126f76 --- /dev/null +++ b/Tests/RunCMake/CMP0218/CMP0218-WARN-stderr.txt @@ -0,0 +1,31 @@ +CMake Warning \(author\) at [^ +]+/Tests/RunCMake/CMP0218/CMP0218-WARN\.cmake:[0-9]+ \(set\): + Policy CMP0218 is not set: The CMAKE_WARN_DEPRECATED and + CMAKE_ERROR_DEPRECATED variables are ignored\. Run "cmake --help-policy + CMP0218" for policy details\. Use the cmake_policy command to set the + policy and suppress this warning\. +This warning is for project developers\. Use -Wno-author to suppress it\. + +CMake Warning \(author\) at [^ +]+/Tests/RunCMake/CMP0218/CMP0218-WARN\.cmake:[0-9]+ \(set\): + Policy CMP0218 is not set: The CMAKE_WARN_DEPRECATED and + CMAKE_ERROR_DEPRECATED variables are ignored\. Run "cmake --help-policy + CMP0218" for policy details\. Use the cmake_policy command to set the + policy and suppress this warning\. +This warning is for project developers\. Use -Wno-author to suppress it\. + +CMake Warning \(author\) at [^ +]+/Tests/RunCMake/CMP0218/CMP0218-WARN\.cmake:[0-9]+ \(set\): + Policy CMP0218 is not set: The CMAKE_WARN_DEPRECATED and + CMAKE_ERROR_DEPRECATED variables are ignored\. Run "cmake --help-policy + CMP0218" for policy details\. Use the cmake_policy command to set the + policy and suppress this warning\. +This warning is for project developers\. Use -Wno-author to suppress it\. + +CMake Warning \(author\) at [^ +]+/Tests/RunCMake/CMP0218/CMP0218-WARN\.cmake:[0-9]+ \(set\): + Policy CMP0218 is not set: The CMAKE_WARN_DEPRECATED and + CMAKE_ERROR_DEPRECATED variables are ignored\. Run "cmake --help-policy + CMP0218" for policy details\. Use the cmake_policy command to set the + policy and suppress this warning\. +This warning is for project developers\. Use -Wno-author to suppress it\. diff --git a/Tests/RunCMake/CMP0218/CMP0218-WARN.cmake b/Tests/RunCMake/CMP0218/CMP0218-WARN.cmake new file mode 100644 index 0000000000..5e57da37d5 --- /dev/null +++ b/Tests/RunCMake/CMP0218/CMP0218-WARN.cmake @@ -0,0 +1,8 @@ +# Test warning when setting deprecated variables. +set(CMAKE_WARN_DEPRECATED OFF) +message(DEPRECATION "Test") +set(CMAKE_ERROR_DEPRECATED ON) + +# Test warning when reading deprecated variables. +set(warn "${CMAKE_WARN_DEPRECATED}") +set(error "${CMAKE_ERROR_DEPRECATED}") diff --git a/Tests/RunCMake/CMP0218/CMakeLists.txt b/Tests/RunCMake/CMP0218/CMakeLists.txt new file mode 100644 index 0000000000..ad13db5d65 --- /dev/null +++ b/Tests/RunCMake/CMP0218/CMakeLists.txt @@ -0,0 +1,3 @@ +cmake_minimum_required(VERSION 4.3) # Must be < 4.4 to test WARN +project(${RunCMake_TEST} NONE) +include(${RunCMake_TEST}.cmake) diff --git a/Tests/RunCMake/CMP0218/ExpectError-stderr.txt b/Tests/RunCMake/CMP0218/ExpectError-stderr.txt new file mode 100644 index 0000000000..0c122de63e --- /dev/null +++ b/Tests/RunCMake/CMP0218/ExpectError-stderr.txt @@ -0,0 +1 @@ +^The CMAKE_ERROR_DEPRECATED variable is deprecated\. Use -W\[no-\]error=deprecated instead\.$ diff --git a/Tests/RunCMake/CMP0218/ExpectError.cmake b/Tests/RunCMake/CMP0218/ExpectError.cmake new file mode 100644 index 0000000000..a867217476 --- /dev/null +++ b/Tests/RunCMake/CMP0218/ExpectError.cmake @@ -0,0 +1,4 @@ +cmake_diagnostic(GET CMD_DEPRECATED action) +if(NOT "${action}" STREQUAL "SEND_ERROR") + message(SEND_ERROR "CMD_DEPRECATED is ${action}; should be SEND_ERROR") +endif() diff --git a/Tests/RunCMake/CMP0218/ExpectIgnore-stderr.txt b/Tests/RunCMake/CMP0218/ExpectIgnore-stderr.txt new file mode 100644 index 0000000000..9eaed9053f --- /dev/null +++ b/Tests/RunCMake/CMP0218/ExpectIgnore-stderr.txt @@ -0,0 +1 @@ +^The CMAKE_WARN_DEPRECATED variable is deprecated\. Use -W\[no-\]deprecated instead\.$ diff --git a/Tests/RunCMake/CMP0218/ExpectIgnore.cmake b/Tests/RunCMake/CMP0218/ExpectIgnore.cmake new file mode 100644 index 0000000000..c84b0f9a89 --- /dev/null +++ b/Tests/RunCMake/CMP0218/ExpectIgnore.cmake @@ -0,0 +1,4 @@ +cmake_diagnostic(GET CMD_DEPRECATED action) +if(NOT "${action}" STREQUAL "IGNORE") + message(SEND_ERROR "CMD_DEPRECATED is ${action}; should be IGNORE") +endif() diff --git a/Tests/RunCMake/CMP0218/RunCMakeTest.cmake b/Tests/RunCMake/CMP0218/RunCMakeTest.cmake new file mode 100644 index 0000000000..59530751f5 --- /dev/null +++ b/Tests/RunCMake/CMP0218/RunCMakeTest.cmake @@ -0,0 +1,8 @@ +include(RunCMake) + +run_cmake_script(CMP0218-WARN) +run_cmake_script(CMP0218-OLD) +run_cmake_script(CMP0218-NEW) + +run_cmake_with_options(ExpectIgnore -DCMAKE_WARN_DEPRECATED=OFF) +run_cmake_with_options(ExpectError -DCMAKE_ERROR_DEPRECATED=ON) diff --git a/Tests/RunCMake/CMakeLists.txt b/Tests/RunCMake/CMakeLists.txt index 62619473ca..a459e2fe1b 100644 --- a/Tests/RunCMake/CMakeLists.txt +++ b/Tests/RunCMake/CMakeLists.txt @@ -190,6 +190,8 @@ if(CMAKE_C_COMPILER_ID STREQUAL "MSVC") add_RunCMake_test(CMP0194 -DCMAKE_C_COMPILER_VERSION=${CMAKE_C_COMPILER_VERSION}) endif() +add_RunCMake_test(CMP0218) + # The test for Policy 65 requires the use of the # CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS variable, which both the VS and Xcode # generators ignore. The policy will have no effect on those generators. diff --git a/Tests/RunCMake/GenerateExportHeader/GEH.cmake b/Tests/RunCMake/GenerateExportHeader/GEH.cmake index 3820dcf85e..6158c5d84f 100644 --- a/Tests/RunCMake/GenerateExportHeader/GEH.cmake +++ b/Tests/RunCMake/GenerateExportHeader/GEH.cmake @@ -1,4 +1,5 @@ # Test add_compiler_export_flags without deprecation warning. +cmake_policy(SET CMP0218 NEW) cmake_diagnostic(SET CMD_DEPRECATED IGNORE) project(GenerateExportHeader) diff --git a/Tests/RunCMake/message/CMakeLists.txt b/Tests/RunCMake/message/CMakeLists.txt index bf2ef1506e..8ad75a102d 100644 --- a/Tests/RunCMake/message/CMakeLists.txt +++ b/Tests/RunCMake/message/CMakeLists.txt @@ -1,3 +1,3 @@ -cmake_minimum_required(VERSION 3.10) +cmake_minimum_required(VERSION 4.0...4.4) project(${RunCMake_TEST} NONE) include(${RunCMake_TEST}.cmake)