diff --git a/Help/manual/cmake-policies.7.rst b/Help/manual/cmake-policies.7.rst index 5879a3056..addd2a7e3 100644 --- a/Help/manual/cmake-policies.7.rst +++ b/Help/manual/cmake-policies.7.rst @@ -70,3 +70,4 @@ All Policies /policy/CMP0034 /policy/CMP0035 /policy/CMP0036 + /policy/CMP0037 diff --git a/Help/policy/CMP0037.rst b/Help/policy/CMP0037.rst new file mode 100644 index 000000000..5df3c00e3 --- /dev/null +++ b/Help/policy/CMP0037.rst @@ -0,0 +1,21 @@ +CMP0037 +------- + +Target names should match a validity pattern. + +CMake 2.8.12 and lower allowed creating targets using :command:`add_library` and +:command:`add_executable` with unrestricted choice for the target name. Newer +cmake features such as :manual:`cmake-generator-expressions(7)` and some +diagnostics expect target names to match a restricted pattern. + +Target names may contain upper and lower case letters, numbers, the underscore +character (_), dot(.), plus(+) and minus(-). As a special case, ALIAS +targets and INTERFACE library targets may contain two consequtive colons. + +The OLD behavior for this policy is to allow creating targets which do not match +the validity pattern. The NEW behavior for this policy is to report an error +if an add_* command is used with an invalid target name. + +This policy was introduced in CMake version 3.0.0. CMake version +|release| warns when the policy is not set and uses OLD behavior. Use +the cmake_policy command to set it to OLD or NEW explicitly. diff --git a/Source/cmAddExecutableCommand.cxx b/Source/cmAddExecutableCommand.cxx index 578525969..a93e83418 100644 --- a/Source/cmAddExecutableCommand.cxx +++ b/Source/cmAddExecutableCommand.cxx @@ -69,6 +69,44 @@ bool cmAddExecutableCommand } } + bool nameOk = cmGeneratorExpression::IsValidTargetName(exename); + if (nameOk && !importTarget && !isAlias) + { + nameOk = exename.find(":") == std::string::npos; + } + if (!nameOk) + { + cmake::MessageType messageType = cmake::AUTHOR_WARNING; + bool issueMessage = false; + switch(this->Makefile->GetPolicyStatus(cmPolicies::CMP0037)) + { + case cmPolicies::WARN: + issueMessage = true; + case cmPolicies::OLD: + break; + case cmPolicies::NEW: + case cmPolicies::REQUIRED_IF_USED: + case cmPolicies::REQUIRED_ALWAYS: + issueMessage = true; + messageType = cmake::FATAL_ERROR; + } + if (issueMessage) + { + cmOStringStream e; + e << (this->Makefile->GetPolicies() + ->GetPolicyWarning(cmPolicies::CMP0037)) << "\n"; + e << "The target name \"" << exename << "\" is not valid for certain " + "CMake features, such as generator expressions, and may result " + "in undefined behavior."; + this->Makefile->IssueMessage(messageType, e.str().c_str()); + + if (messageType == cmake::FATAL_ERROR) + { + return false; + } + } + } + // Special modifiers are not allowed with IMPORTED signature. if(importTarget && (use_win32 || use_macbundle || excludeFromAll)) diff --git a/Source/cmAddLibraryCommand.cxx b/Source/cmAddLibraryCommand.cxx index 0e61c9956..4c591b61b 100644 --- a/Source/cmAddLibraryCommand.cxx +++ b/Source/cmAddLibraryCommand.cxx @@ -108,6 +108,45 @@ bool cmAddLibraryCommand break; } } + + bool nameOk = cmGeneratorExpression::IsValidTargetName(libName); + if (nameOk && !importTarget && !isAlias) + { + nameOk = libName.find(":") == std::string::npos; + } + if (!nameOk) + { + cmake::MessageType messageType = cmake::AUTHOR_WARNING; + bool issueMessage = false; + switch(this->Makefile->GetPolicyStatus(cmPolicies::CMP0037)) + { + case cmPolicies::WARN: + issueMessage = type != cmTarget::INTERFACE_LIBRARY; + case cmPolicies::OLD: + break; + case cmPolicies::NEW: + case cmPolicies::REQUIRED_IF_USED: + case cmPolicies::REQUIRED_ALWAYS: + issueMessage = true; + messageType = cmake::FATAL_ERROR; + } + if (issueMessage) + { + cmOStringStream e; + e << (this->Makefile->GetPolicies() + ->GetPolicyWarning(cmPolicies::CMP0037)) << "\n"; + e << "The target name \"" << libName << "\" is not valid for certain " + "CMake features, such as generator expressions, and may result " + "in undefined behavior."; + this->Makefile->IssueMessage(messageType, e.str().c_str()); + + if (messageType == cmake::FATAL_ERROR) + { + return false; + } + } + } + if (isAlias) { if(!cmGeneratorExpression::IsValidTargetName(libName.c_str())) diff --git a/Source/cmPolicies.cxx b/Source/cmPolicies.cxx index ab822d3a3..db23f1ecd 100644 --- a/Source/cmPolicies.cxx +++ b/Source/cmPolicies.cxx @@ -286,6 +286,11 @@ cmPolicies::cmPolicies() CMP0036, "CMP0036", "The build_name command should not be called.", 3,0,0,0, cmPolicies::WARN); + + this->DefinePolicy( + CMP0037, "CMP0037", + "Target names should match a validity pattern.", + 3,0,0,0, cmPolicies::WARN); } cmPolicies::~cmPolicies() diff --git a/Source/cmPolicies.h b/Source/cmPolicies.h index 9e72bdc9b..821c58d84 100644 --- a/Source/cmPolicies.h +++ b/Source/cmPolicies.h @@ -88,6 +88,7 @@ public: CMP0034, ///< Disallow command: utility_source CMP0035, ///< Disallow command: variable_requires CMP0036, ///< Disallow command: build_name + CMP0037, ///< Target names should match a validity pattern. /** \brief Always the last entry. * diff --git a/Tests/RunCMake/CMP0037/CMP0037-NEW-result.txt b/Tests/RunCMake/CMP0037/CMP0037-NEW-result.txt new file mode 100644 index 000000000..d00491fd7 --- /dev/null +++ b/Tests/RunCMake/CMP0037/CMP0037-NEW-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/CMP0037/CMP0037-NEW-stderr.txt b/Tests/RunCMake/CMP0037/CMP0037-NEW-stderr.txt new file mode 100644 index 000000000..6de96b1fa --- /dev/null +++ b/Tests/RunCMake/CMP0037/CMP0037-NEW-stderr.txt @@ -0,0 +1,39 @@ +CMake Error at CMP0037-NEW.cmake:4 \(add_library\): + Policy CMP0037 is not set: Target names should match a validity pattern. + Run "cmake --help-policy CMP0037" for policy details. Use the cmake_policy + command to set the policy and suppress this warning. + + The target name "lib with spaces" is not valid for certain CMake features, + such as generator expressions, and may result in undefined behavior. +Call Stack \(most recent call first\): + CMakeLists.txt:3 \(include\) ++ +CMake Error at CMP0037-NEW.cmake:5 \(add_executable\): + Policy CMP0037 is not set: Target names should match a validity pattern. + Run "cmake --help-policy CMP0037" for policy details. Use the cmake_policy + command to set the policy and suppress this warning. + + The target name "exe with spaces" is not valid for certain CMake features, + such as generator expressions, and may result in undefined behavior. +Call Stack \(most recent call first\): + CMakeLists.txt:3 \(include\) ++ +CMake Error at CMP0037-NEW.cmake:6 \(add_library\): + Policy CMP0037 is not set: Target names should match a validity pattern. + Run "cmake --help-policy CMP0037" for policy details. Use the cmake_policy + command to set the policy and suppress this warning. + + The target name "lib:colon" is not valid for certain CMake features, such + as generator expressions, and may result in undefined behavior. +Call Stack \(most recent call first\): + CMakeLists.txt:3 \(include\) ++ +CMake Error at CMP0037-NEW.cmake:7 \(add_executable\): + Policy CMP0037 is not set: Target names should match a validity pattern. + Run "cmake --help-policy CMP0037" for policy details. Use the cmake_policy + command to set the policy and suppress this warning. + + The target name "exe:colon" is not valid for certain CMake features, such + as generator expressions, and may result in undefined behavior. +Call Stack \(most recent call first\): + CMakeLists.txt:3 \(include\) diff --git a/Tests/RunCMake/CMP0037/CMP0037-NEW.cmake b/Tests/RunCMake/CMP0037/CMP0037-NEW.cmake new file mode 100644 index 000000000..a6ffc592c --- /dev/null +++ b/Tests/RunCMake/CMP0037/CMP0037-NEW.cmake @@ -0,0 +1,7 @@ + +cmake_policy(SET CMP0037 NEW) + +add_library("lib with spaces" empty.cpp) +add_executable("exe with spaces" empty.cpp) +add_library("lib:colon" empty.cpp) +add_executable("exe:colon" empty.cpp) diff --git a/Tests/RunCMake/CMP0037/CMP0037-OLD-result.txt b/Tests/RunCMake/CMP0037/CMP0037-OLD-result.txt new file mode 100644 index 000000000..573541ac9 --- /dev/null +++ b/Tests/RunCMake/CMP0037/CMP0037-OLD-result.txt @@ -0,0 +1 @@ +0 diff --git a/Tests/RunCMake/CMP0037/CMP0037-OLD-stderr.txt b/Tests/RunCMake/CMP0037/CMP0037-OLD-stderr.txt new file mode 100644 index 000000000..10f32932e --- /dev/null +++ b/Tests/RunCMake/CMP0037/CMP0037-OLD-stderr.txt @@ -0,0 +1 @@ +^$ diff --git a/Tests/RunCMake/CMP0037/CMP0037-OLD.cmake b/Tests/RunCMake/CMP0037/CMP0037-OLD.cmake new file mode 100644 index 000000000..af98f12ff --- /dev/null +++ b/Tests/RunCMake/CMP0037/CMP0037-OLD.cmake @@ -0,0 +1,5 @@ + +cmake_policy(SET CMP0037 OLD) + +add_library("lib with spaces" empty.cpp) +add_executable("exe with spaces" empty.cpp) diff --git a/Tests/RunCMake/CMP0037/CMP0037-WARN-result.txt b/Tests/RunCMake/CMP0037/CMP0037-WARN-result.txt new file mode 100644 index 000000000..573541ac9 --- /dev/null +++ b/Tests/RunCMake/CMP0037/CMP0037-WARN-result.txt @@ -0,0 +1 @@ +0 diff --git a/Tests/RunCMake/CMP0037/CMP0037-WARN-stderr.txt b/Tests/RunCMake/CMP0037/CMP0037-WARN-stderr.txt new file mode 100644 index 000000000..e7a9e1d10 --- /dev/null +++ b/Tests/RunCMake/CMP0037/CMP0037-WARN-stderr.txt @@ -0,0 +1,43 @@ +CMake Warning \(dev\) at CMP0037-WARN.cmake:2 \(add_library\): + Policy CMP0037 is not set: Target names should match a validity pattern. + Run "cmake --help-policy CMP0037" for policy details. Use the cmake_policy + command to set the policy and suppress this warning. + + The target name "lib with spaces" is not valid for certain CMake features, + such as generator expressions, and may result in undefined behavior. +Call Stack \(most recent call first\): + CMakeLists.txt:3 \(include\) +This warning is for project developers. Use -Wno-dev to suppress it. ++ +CMake Warning \(dev\) at CMP0037-WARN.cmake:3 \(add_executable\): + Policy CMP0037 is not set: Target names should match a validity pattern. + Run "cmake --help-policy CMP0037" for policy details. Use the cmake_policy + command to set the policy and suppress this warning. + + The target name "exe with spaces" is not valid for certain CMake features, + such as generator expressions, and may result in undefined behavior. +Call Stack \(most recent call first\): + CMakeLists.txt:3 \(include\) +This warning is for project developers. Use -Wno-dev to suppress it. ++ +CMake Warning \(dev\) at CMP0037-WARN.cmake:4 \(add_library\): + Policy CMP0037 is not set: Target names should match a validity pattern. + Run "cmake --help-policy CMP0037" for policy details. Use the cmake_policy + command to set the policy and suppress this warning. + + The target name "lib:colon" is not valid for certain CMake features, such + as generator expressions, and may result in undefined behavior. +Call Stack \(most recent call first\): + CMakeLists.txt:3 \(include\) +This warning is for project developers. Use -Wno-dev to suppress it. ++ +CMake Warning \(dev\) at CMP0037-WARN.cmake:5 \(add_executable\): + Policy CMP0037 is not set: Target names should match a validity pattern. + Run "cmake --help-policy CMP0037" for policy details. Use the cmake_policy + command to set the policy and suppress this warning. + + The target name "exe:colon" is not valid for certain CMake features, such + as generator expressions, and may result in undefined behavior. +Call Stack \(most recent call first\): + CMakeLists.txt:3 \(include\) +This warning is for project developers. Use -Wno-dev to suppress it. diff --git a/Tests/RunCMake/CMP0037/CMP0037-WARN.cmake b/Tests/RunCMake/CMP0037/CMP0037-WARN.cmake new file mode 100644 index 000000000..2beaad02c --- /dev/null +++ b/Tests/RunCMake/CMP0037/CMP0037-WARN.cmake @@ -0,0 +1,5 @@ + +add_library("lib with spaces" empty.cpp) +add_executable("exe with spaces" empty.cpp) +add_library("lib:colon" empty.cpp) +add_executable("exe:colon" empty.cpp) diff --git a/Tests/RunCMake/CMP0037/CMakeLists.txt b/Tests/RunCMake/CMP0037/CMakeLists.txt new file mode 100644 index 000000000..f1d9caecd --- /dev/null +++ b/Tests/RunCMake/CMP0037/CMakeLists.txt @@ -0,0 +1,3 @@ +cmake_minimum_required(VERSION 2.8.4) +project(${RunCMake_TEST} CXX) +include(${RunCMake_TEST}.cmake NO_POLICY_SCOPE) diff --git a/Tests/RunCMake/CMP0037/RunCMakeTest.cmake b/Tests/RunCMake/CMP0037/RunCMakeTest.cmake new file mode 100644 index 000000000..e98352690 --- /dev/null +++ b/Tests/RunCMake/CMP0037/RunCMakeTest.cmake @@ -0,0 +1,5 @@ +include(RunCMake) + +run_cmake(CMP0037-NEW) +run_cmake(CMP0037-OLD) +run_cmake(CMP0037-WARN) diff --git a/Tests/RunCMake/CMP0037/empty.cpp b/Tests/RunCMake/CMP0037/empty.cpp new file mode 100644 index 000000000..bfbbddeb9 --- /dev/null +++ b/Tests/RunCMake/CMP0037/empty.cpp @@ -0,0 +1,7 @@ +#ifdef _WIN32 +__declspec(dllexport) +#endif +int empty() +{ + return 0; +} diff --git a/Tests/RunCMake/CMakeLists.txt b/Tests/RunCMake/CMakeLists.txt index 99a0fb3b2..a1467d0b7 100644 --- a/Tests/RunCMake/CMakeLists.txt +++ b/Tests/RunCMake/CMakeLists.txt @@ -56,6 +56,9 @@ add_RunCMake_test(CMP0022) add_RunCMake_test(CMP0026) add_RunCMake_test(CMP0027) add_RunCMake_test(CMP0028) +if (NOT "${CMAKE_TEST_GENERATOR}" MATCHES "(MSYS|MinGW|NMake|Borland) Makefiles") + add_RunCMake_test(CMP0037) +endif() add_RunCMake_test(CTest) if(UNIX AND "${CMAKE_TEST_GENERATOR}" MATCHES "Unix Makefiles") add_RunCMake_test(CompilerChange)