target_sources(): Prohibit FILE_SET on custom targets

Fixes: #23262
This commit is contained in:
Kyle Edwards 2022-02-28 17:11:53 -05:00
parent 07a7772968
commit ad41c9cd11
6 changed files with 16 additions and 1 deletions

View File

@ -75,7 +75,8 @@ Adds a file set to a target, or adds files to an existing file set. Targets
have zero or more named file sets. Each file set has a name, a type, a scope of
``INTERFACE``, ``PUBLIC``, or ``PRIVATE``, one or more base directories, and
files within those directories. The only acceptable type is ``HEADERS``. The
optional default file sets are named after their type.
optional default file sets are named after their type. The target may not be a
custom target.
Files in a ``PRIVATE`` or ``PUBLIC`` file set are marked as source files for
the purposes of IDE integration. Additionally, files in ``HEADERS`` file sets

View File

@ -16,6 +16,7 @@
#include "cmMakefile.h"
#include "cmMessageType.h"
#include "cmPolicies.h"
#include "cmStateTypes.h"
#include "cmStringAlgorithms.h"
#include "cmSystemTools.h"
#include "cmTarget.h"
@ -202,6 +203,11 @@ bool TargetSourcesImpl::HandleFileSetMode(
return false;
}
if (this->Target->GetType() == cmStateEnums::UTILITY) {
this->SetError("FILE_SETs may not be added to custom targets");
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;

View File

@ -0,0 +1 @@
1

View File

@ -0,0 +1,4 @@
^CMake Error at FileSetCustomTarget\.cmake:[0-9]+ \(target_sources\):
target_sources FILE_SETs may not be added to custom targets
Call Stack \(most recent call first\):
CMakeLists\.txt:[0-9]+ \(include\)$

View File

@ -0,0 +1,2 @@
add_custom_target(tgt)
target_sources(tgt PRIVATE FILE_SET HEADERS FILES h1.h)

View File

@ -38,6 +38,7 @@ run_cmake(FileSetNoExistPrivate)
run_cmake(FileSetNoExistInterface)
run_cmake(FileSetNoExistInstall)
run_cmake(FileSetDirectories)
run_cmake(FileSetCustomTarget)
set(RunCMake_TEST_OPTIONS -DCMAKE_POLICY_DEFAULT_CMP0115=NEW)
run_cmake(FileSetFileNoExist)