cmTargetSourcesCommand: enhance arguments checks

This commit is contained in:
Marc Chevrier committed 2026-02-19 17:03:45 +01:00
1 parent c4c84ae0f7
commit c362c4a7b5
4 files changed
+35 -19

No files matched your search

+31 -16
View File
@@ -232,33 +232,48 @@ bool TargetSourcesImpl::HandleOneFileSet(
return false;
}
bool const isDefault = args.Type == args.FileSet ||
(args.Type.empty() && args.FileSet[0] >= 'A' && args.FileSet[0] <= 'Z');
std::string type = isDefault ? args.FileSet : args.Type;
if (!args.Type.empty() && !cmFileSet::IsKnownType(args.Type)) {
this->SetError(cmStrCat("File set TYPE may only be \"",
cmFileSet::GetKnownTypes().join("\", \""), '"'));
return false;
}
if (args.Type.empty() && args.FileSet[0] >= 'A' && args.FileSet[0] <= 'Z' &&
!cmFileSet::IsKnownType(args.FileSet)) {
this->SetError(
cmStrCat("FILE_SET names starting with a capital letter are reserved "
"for built-in file sets and may only be \"",
cmFileSet::GetKnownTypes().join("\", \""), '"'));
return false;
}
if (!args.Type.empty() && args.FileSet[0] >= 'A' && args.FileSet[0] <= 'Z' &&
args.Type != args.FileSet) {
this->SetError(cmStrCat("FILE_SET name starting with a capital letter "
"must match the TYPE name \"",
args.Type, '"'));
return false;
}
bool const isDefault = args.Type == args.FileSet ||
(args.Type.empty() && cmFileSet::IsKnownType(args.FileSet));
if (!isDefault && !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;
}
std::string type = isDefault ? args.FileSet : args.Type;
cmFileSet::Visibility visibility =
cmFileSet::VisibilityFromName(scope, this->Makefile);
auto fileSet =
this->Target->GetOrCreateFileSet(args.FileSet, type, visibility);
if (fileSet.second) {
if (!isDefault) {
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;
}
}
if (type.empty()) {
this->SetError("Must specify a TYPE when creating file set");
return false;
}
if (!cmFileSet::IsKnownType(type)) {
this->SetError(cmStrCat("File set TYPE may only be \"",
cmFileSet::GetKnownTypes().join("\", \""), '"'));
return false;
}
if (cmFileSet::VisibilityIsForSelf(visibility) &&
this->Target->GetType() == cmStateEnums::INTERFACE_LIBRARY &&
@@ -1,5 +1,5 @@
^CMake Error at FileSetChangeType\.cmake:[0-9]+ \(target_sources\):
target_sources Type "RESOURCES" for file set "a" does not match original
target_sources Type "CXX_MODULES" for file set "a" does not match original
type "HEADERS"
Call Stack \(most recent call first\):
CMakeLists\.txt:[0-9]+ \(include\)$
@@ -2,4 +2,4 @@ enable_language(C)
add_library(lib1 STATIC empty.c)
target_sources(lib1 PRIVATE FILE_SET a TYPE HEADERS)
target_sources(lib1 PRIVATE FILE_SET a TYPE RESOURCES)
target_sources(lib1 PRIVATE FILE_SET a TYPE CXX_MODULES)
@@ -1,4 +1,5 @@
^CMake Error at FileSetDefaultWrongType\.cmake:[0-9]+ \(target_sources\):
target_sources File set TYPE may only be "HEADERS", "CXX_MODULES"
target_sources FILE_SET names starting with a capital letter are reserved
for built-in file sets and may only be "HEADERS", "CXX_MODULES"
Call Stack \(most recent call first\):
CMakeLists\.txt:[0-9]+ \(include\)$