summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKyle Edwards <kyle.edwards@kitware.com>2022-03-03 15:15:08 (GMT)
committerKyle Edwards <kyle.edwards@kitware.com>2022-03-03 15:17:10 (GMT)
commitb357d334fc0c2f47ebc6a3a2b0ceba25d4818bb0 (patch)
treed925af359e631e47cfe061c6560fe0a443c04aec
parent04a7200c7561509a83c34f2af45b2d091ae59248 (diff)
downloadCMake-b357d334fc0c2f47ebc6a3a2b0ceba25d4818bb0.zip
CMake-b357d334fc0c2f47ebc6a3a2b0ceba25d4818bb0.tar.gz
CMake-b357d334fc0c2f47ebc6a3a2b0ceba25d4818bb0.tar.bz2
target_sources(): Enforce stricter requirements for FILE_SET name
Fixes: #23286
-rw-r--r--Help/command/target_sources.rst3
-rw-r--r--Source/cmFileSet.cxx10
-rw-r--r--Source/cmFileSet.h2
-rw-r--r--Source/cmTargetSourcesCommand.cxx7
-rw-r--r--Tests/RunCMake/target_sources/FileSetBadName-result.txt1
-rw-r--r--Tests/RunCMake/target_sources/FileSetBadName-stderr.txt6
-rw-r--r--Tests/RunCMake/target_sources/FileSetBadName.cmake4
-rw-r--r--Tests/RunCMake/target_sources/RunCMakeTest.cmake1
8 files changed, 30 insertions, 4 deletions
diff --git a/Help/command/target_sources.rst b/Help/command/target_sources.rst
index e482117..5e35eae 100644
--- a/Help/command/target_sources.rst
+++ b/Help/command/target_sources.rst
@@ -91,7 +91,8 @@ Each ``target_sources(FILE_SET)`` entry starts with ``INTERFACE``, ``PUBLIC``, o
``FILE_SET <set>``
A string representing the name of the file set to create or add to. This must
- not start with a capital letter, unless its name is ``HEADERS``.
+ contain only numbers, letters, and underscores, and must not start with a
+ capital letter or underscore, unless its name is ``HEADERS``.
``TYPE <type>``
diff --git a/Source/cmFileSet.cxx b/Source/cmFileSet.cxx
index 08d56ba..2c06dc6 100644
--- a/Source/cmFileSet.cxx
+++ b/Source/cmFileSet.cxx
@@ -7,6 +7,8 @@
#include <utility>
#include <vector>
+#include "cmsys/RegularExpression.hxx"
+
#include "cmGeneratorExpression.h"
#include "cmListFileCache.h"
#include "cmLocalGenerator.h"
@@ -149,3 +151,11 @@ void cmFileSet::EvaluateFileEntry(
filesPerDir[relDir].push_back(file);
}
}
+
+bool cmFileSet::IsValidName(const std::string& name)
+{
+ static const cmsys::RegularExpression regex("^[a-z0-9][a-zA-Z0-9_]*$");
+
+ cmsys::RegularExpressionMatch match;
+ return regex.find(name.c_str(), match);
+}
diff --git a/Source/cmFileSet.h b/Source/cmFileSet.h
index 5ee4a98..3aad75f 100644
--- a/Source/cmFileSet.h
+++ b/Source/cmFileSet.h
@@ -56,6 +56,8 @@ public:
const cmGeneratorTarget* target,
cmGeneratorExpressionDAGChecker* dagChecker = nullptr) const;
+ static bool IsValidName(const std::string& name);
+
private:
std::string Name;
std::string Type;
diff --git a/Source/cmTargetSourcesCommand.cxx b/Source/cmTargetSourcesCommand.cxx
index b425acb..954fbb5 100644
--- a/Source/cmTargetSourcesCommand.cxx
+++ b/Source/cmTargetSourcesCommand.cxx
@@ -215,9 +215,10 @@ bool TargetSourcesImpl::HandleFileSetMode(
auto fileSet = this->Target->GetOrCreateFileSet(args.FileSet, type);
if (fileSet.second) {
if (!isDefault) {
- if (args.FileSet[0] >= 'A' && args.FileSet[0] <= 'Z') {
- this->SetError(
- "Non-default file set name must not start with a capital letter");
+ if (!cmFileSet::IsValidName(args.FileSet)) {
+ this->SetError("Non-default file set name must contain only letters, "
+ "numbers, and underscores, and must not start with a "
+ "capital letter or underscore");
return false;
}
}
diff --git a/Tests/RunCMake/target_sources/FileSetBadName-result.txt b/Tests/RunCMake/target_sources/FileSetBadName-result.txt
new file mode 100644
index 0000000..d00491f
--- /dev/null
+++ b/Tests/RunCMake/target_sources/FileSetBadName-result.txt
@@ -0,0 +1 @@
+1
diff --git a/Tests/RunCMake/target_sources/FileSetBadName-stderr.txt b/Tests/RunCMake/target_sources/FileSetBadName-stderr.txt
new file mode 100644
index 0000000..a0b8054
--- /dev/null
+++ b/Tests/RunCMake/target_sources/FileSetBadName-stderr.txt
@@ -0,0 +1,6 @@
+^CMake Error at FileSetBadName\.cmake:[0-9]+ \(target_sources\):
+ target_sources Non-default file set name must contain only letters,
+ numbers, and underscores, and must not start with a capital letter or
+ underscore
+Call Stack \(most recent call first\):
+ CMakeLists\.txt:[0-9]+ \(include\)$
diff --git a/Tests/RunCMake/target_sources/FileSetBadName.cmake b/Tests/RunCMake/target_sources/FileSetBadName.cmake
new file mode 100644
index 0000000..325843f
--- /dev/null
+++ b/Tests/RunCMake/target_sources/FileSetBadName.cmake
@@ -0,0 +1,4 @@
+enable_language(C)
+
+add_library(lib1 STATIC empty.c)
+target_sources(lib1 INTERFACE FILE_SET a+ TYPE HEADERS)
diff --git a/Tests/RunCMake/target_sources/RunCMakeTest.cmake b/Tests/RunCMake/target_sources/RunCMakeTest.cmake
index da9944d..d23bce1 100644
--- a/Tests/RunCMake/target_sources/RunCMakeTest.cmake
+++ b/Tests/RunCMake/target_sources/RunCMakeTest.cmake
@@ -39,6 +39,7 @@ run_cmake(FileSetNoExistInterface)
run_cmake(FileSetNoExistInstall)
run_cmake(FileSetDirectories)
run_cmake(FileSetCustomTarget)
+run_cmake(FileSetBadName)
set(RunCMake_TEST_OPTIONS -DCMAKE_POLICY_DEFAULT_CMP0115=NEW)
run_cmake(FileSetFileNoExist)