Coverage Report

Created: 2026-06-15 07:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Source/cmTargetSourcesCommand.cxx
Line
Count
Source
1
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2
   file LICENSE.rst or https://cmake.org/licensing for details.  */
3
#include "cmTargetSourcesCommand.h"
4
5
#include <utility>
6
7
#include <cm/optional>
8
#include <cm/string_view>
9
#include <cmext/string_view>
10
11
#include "cmArgumentParser.h"
12
#include "cmArgumentParserTypes.h"
13
#include "cmFileSet.h"
14
#include "cmFileSetMetadata.h"
15
#include "cmGeneratorExpression.h"
16
#include "cmList.h"
17
#include "cmListFileCache.h"
18
#include "cmMakefile.h"
19
#include "cmMessageType.h"
20
#include "cmPolicies.h"
21
#include "cmStateTypes.h"
22
#include "cmStringAlgorithms.h"
23
#include "cmSystemTools.h"
24
#include "cmTarget.h"
25
#include "cmTargetPropCommandBase.h"
26
27
namespace {
28
29
struct FileSetArgs
30
{
31
  std::string Type;
32
  std::string FileSet;
33
  ArgumentParser::MaybeEmpty<std::vector<std::string>> BaseDirs;
34
  ArgumentParser::MaybeEmpty<std::vector<std::string>> Files;
35
};
36
37
auto const FileSetArgsParser = cmArgumentParser<FileSetArgs>()
38
                                 .Bind("TYPE"_s, &FileSetArgs::Type)
39
                                 .Bind("FILE_SET"_s, &FileSetArgs::FileSet)
40
                                 .Bind("BASE_DIRS"_s, &FileSetArgs::BaseDirs)
41
                                 .Bind("FILES"_s, &FileSetArgs::Files);
42
43
struct FileSetsArgs
44
{
45
  std::vector<std::vector<std::string>> FileSets;
46
};
47
48
auto const FileSetsArgsParser =
49
  cmArgumentParser<FileSetsArgs>().Bind("FILE_SET"_s, &FileSetsArgs::FileSets);
50
51
class TargetSourcesImpl : public cmTargetPropCommandBase
52
{
53
public:
54
  using cmTargetPropCommandBase::cmTargetPropCommandBase;
55
56
protected:
57
  void HandleInterfaceContent(cmTarget* tgt,
58
                              std::vector<std::string> const& content,
59
                              bool prepend, bool system) override
60
0
  {
61
0
    this->cmTargetPropCommandBase::HandleInterfaceContent(
62
0
      tgt,
63
0
      this->ConvertToAbsoluteContent(tgt, content, IsInterface::Yes,
64
0
                                     CheckCMP0076::Yes),
65
0
      prepend, system);
66
0
  }
67
68
private:
69
  void HandleMissingTarget(std::string const& name) override
70
0
  {
71
0
    this->Makefile->IssueMessage(
72
0
      MessageType::FATAL_ERROR,
73
0
      cmStrCat("Cannot specify sources for target \"", name,
74
0
               "\" which is not built by this project."));
75
0
  }
76
77
  bool HandleDirectContent(cmTarget* tgt,
78
                           std::vector<std::string> const& content,
79
                           bool /*prepend*/, bool /*system*/) override
80
0
  {
81
0
    tgt->AppendProperty("SOURCES",
82
0
                        this->Join(this->ConvertToAbsoluteContent(
83
0
                          tgt, content, IsInterface::No, CheckCMP0076::Yes)),
84
0
                        this->Makefile->GetBacktrace());
85
0
    return true; // Successfully handled.
86
0
  }
87
88
  bool PopulateTargetProperties(std::string const& scope,
89
                                std::vector<std::string> const& content,
90
                                bool prepend, bool system) override
91
0
  {
92
0
    if (!content.empty() && content.front() == "FILE_SET"_s) {
93
0
      return this->HandleFileSetMode(scope, content);
94
0
    }
95
0
    return this->cmTargetPropCommandBase::PopulateTargetProperties(
96
0
      scope, content, prepend, system);
97
0
  }
98
99
  std::string Join(std::vector<std::string> const& content) override
100
0
  {
101
0
    return cmList::to_string(content);
102
0
  }
103
104
  enum class IsInterface
105
  {
106
    Yes,
107
    No,
108
  };
109
  enum class CheckCMP0076
110
  {
111
    Yes,
112
    No,
113
  };
114
  std::vector<std::string> ConvertToAbsoluteContent(
115
    cmTarget* tgt, std::vector<std::string> const& content,
116
    IsInterface isInterfaceContent, CheckCMP0076 checkCmp0076);
117
118
  bool HandleFileSetMode(std::string const& scope,
119
                         std::vector<std::string> const& content);
120
  bool HandleOneFileSet(std::string const& scope,
121
                        std::vector<std::string> const& content);
122
};
123
124
std::vector<std::string> TargetSourcesImpl::ConvertToAbsoluteContent(
125
  cmTarget* tgt, std::vector<std::string> const& content,
126
  IsInterface isInterfaceContent, CheckCMP0076 checkCmp0076)
127
0
{
128
  // Skip conversion in case old behavior has been explicitly requested
129
0
  if (checkCmp0076 == CheckCMP0076::Yes &&
130
0
      this->Makefile->GetPolicyStatus(cmPolicies::CMP0076) ==
131
0
        cmPolicies::OLD) {
132
0
    return content;
133
0
  }
134
135
0
  bool changedPath = false;
136
0
  std::vector<std::string> absoluteContent;
137
0
  absoluteContent.reserve(content.size());
138
0
  for (std::string const& src : content) {
139
0
    std::string absoluteSrc;
140
0
    if (cmSystemTools::FileIsFullPath(src) ||
141
0
        cmGeneratorExpression::Find(src) == 0 ||
142
0
        (isInterfaceContent == IsInterface::No &&
143
0
         (this->Makefile->GetCurrentSourceDirectory() ==
144
0
          tgt->GetMakefile()->GetCurrentSourceDirectory()))) {
145
0
      absoluteSrc = src;
146
0
    } else {
147
0
      changedPath = true;
148
0
      absoluteSrc =
149
0
        cmStrCat(this->Makefile->GetCurrentSourceDirectory(), '/', src);
150
0
    }
151
0
    absoluteContent.push_back(absoluteSrc);
152
0
  }
153
154
0
  if (!changedPath) {
155
0
    return content;
156
0
  }
157
158
0
  if (checkCmp0076 == CheckCMP0076::Yes) {
159
0
    switch (this->Makefile->GetPolicyStatus(cmPolicies::CMP0076)) {
160
0
      case cmPolicies::WARN:
161
0
        if (isInterfaceContent == IsInterface::Yes) {
162
0
          this->Makefile->IssuePolicyWarning(
163
0
            cmPolicies::CMP0076, {},
164
0
            cmStrCat("An interface source of target \""_s, tgt->GetName(),
165
0
                     "\" has a relative path."_s));
166
0
        } else {
167
0
          this->Makefile->IssuePolicyWarning(
168
0
            cmPolicies::CMP0076, {},
169
0
            cmStrCat("A private source from a directory "
170
0
                     "other than that of target \""_s,
171
0
                     tgt->GetName(), "\" has a relative path."_s));
172
0
        }
173
0
        CM_FALLTHROUGH;
174
0
      case cmPolicies::OLD:
175
0
        return content;
176
0
      case cmPolicies::NEW:
177
0
        break;
178
0
    }
179
0
  }
180
181
0
  return absoluteContent;
182
0
}
183
184
bool TargetSourcesImpl::HandleFileSetMode(
185
  std::string const& scope, std::vector<std::string> const& content)
186
0
{
187
0
  auto args = FileSetsArgsParser.Parse(content, /*unparsedArguments=*/nullptr);
188
189
0
  for (auto& argList : args.FileSets) {
190
0
    argList.emplace(argList.begin(), "FILE_SET"_s);
191
0
    if (!this->HandleOneFileSet(scope, argList)) {
192
0
      return false;
193
0
    }
194
0
  }
195
196
0
  return true;
197
0
}
198
199
bool TargetSourcesImpl::HandleOneFileSet(
200
  std::string const& scope, std::vector<std::string> const& content)
201
0
{
202
0
  std::vector<std::string> unparsed;
203
0
  auto args = FileSetArgsParser.Parse(content, &unparsed);
204
205
0
  if (!unparsed.empty()) {
206
0
    this->SetError(
207
0
      cmStrCat("Unrecognized keyword: \"", unparsed.front(), '"'));
208
0
    return false;
209
0
  }
210
211
0
  if (args.FileSet.empty()) {
212
0
    this->SetError("FILE_SET must not be empty");
213
0
    return false;
214
0
  }
215
216
0
  if (this->Target->GetType() == cmStateEnums::UTILITY) {
217
0
    this->SetError("FILE_SETs may not be added to custom targets");
218
0
    return false;
219
0
  }
220
221
0
  if (!args.Type.empty() && !cm::FileSetMetadata::IsKnownType(args.Type)) {
222
0
    this->SetError(
223
0
      cmStrCat("File set TYPE may only be \"",
224
0
               cmJoin(cm::FileSetMetadata::GetKnownTypes(), "\", \""), '"'));
225
0
    return false;
226
0
  }
227
0
  if (args.Type.empty() && args.FileSet[0] >= 'A' && args.FileSet[0] <= 'Z' &&
228
0
      !cm::FileSetMetadata::IsKnownType(args.FileSet)) {
229
0
    this->SetError(
230
0
      cmStrCat("FILE_SET names starting with a capital letter are reserved "
231
0
               "for built-in file sets and may only be \"",
232
0
               cmJoin(cm::FileSetMetadata::GetKnownTypes(), "\", \""), '"'));
233
0
    return false;
234
0
  }
235
0
  if (!args.Type.empty() && args.FileSet[0] >= 'A' && args.FileSet[0] <= 'Z' &&
236
0
      args.Type != args.FileSet) {
237
0
    this->SetError(cmStrCat("FILE_SET name starting with a capital letter "
238
0
                            "must match the TYPE name \"",
239
0
                            args.Type, '"'));
240
0
    return false;
241
0
  }
242
243
0
  bool const isDefault = args.Type == args.FileSet ||
244
0
    (args.Type.empty() && cm::FileSetMetadata::IsKnownType(args.FileSet));
245
246
0
  if (!isDefault && !cm::FileSetMetadata::IsValidName(args.FileSet)) {
247
0
    this->SetError("Non-default file set name must contain only letters, "
248
0
                   "numbers, and underscores, and must not start with a "
249
0
                   "capital letter or underscore");
250
0
    return false;
251
0
  }
252
253
0
  std::string type = isDefault ? args.FileSet : args.Type;
254
0
  cm::FileSetMetadata::Visibility visibility =
255
0
    cm::FileSetMetadata::VisibilityFromName(scope, this->Makefile);
256
257
0
  if (this->Target->IsFrameworkOnApple() &&
258
0
      !cm::FileSetMetadata::IsFrameworkSupported(type)) {
259
0
    this->SetError(cmStrCat(R"(FILE_SETs, of type ")", type,
260
0
                            R"(", may not be added to FRAMEWORK targets)"));
261
0
    return false;
262
0
  }
263
264
0
  auto fileSet =
265
0
    this->Target->GetOrCreateFileSet(args.FileSet, type, visibility);
266
0
  if (fileSet.second) {
267
0
    if (type.empty()) {
268
0
      this->SetError("Must specify a TYPE when creating file set");
269
0
      return false;
270
0
    }
271
272
0
    if (cm::FileSetMetadata::VisibilityIsForSelf(visibility) &&
273
0
        this->Target->GetType() == cmStateEnums::INTERFACE_LIBRARY &&
274
0
        !this->Target->IsImported()) {
275
0
      if (type == cm::FileSetMetadata::CXX_MODULES) {
276
0
        this->SetError(
277
0
          cmStrCat(R"(File set TYPE ")", cm::FileSetMetadata::CXX_MODULES,
278
0
                   R"(" may not have "PUBLIC" )"
279
0
                   R"(or "PRIVATE" scope on INTERFACE libraries.)"));
280
0
        return false;
281
0
      }
282
0
    }
283
284
    // FIXME(https://wg21.link/P3470): This condition can go
285
    // away when interface-only module units are a thing.
286
0
    if (cm::FileSetMetadata::VisibilityIsForInterface(visibility) &&
287
0
        !cm::FileSetMetadata::VisibilityIsForSelf(visibility) &&
288
0
        !this->Target->IsImported()) {
289
0
      if (type == cm::FileSetMetadata::CXX_MODULES) {
290
0
        this->SetError(cmStrCat(R"(File set TYPE ")",
291
0
                                cm::FileSetMetadata::CXX_MODULES,
292
0
                                R"(" may not have "INTERFACE" scope)"));
293
0
        return false;
294
0
      }
295
0
    }
296
297
0
    if (cm::FileSetMetadata::VisibilityIsForSelf(visibility) &&
298
0
        this->Target->GetType() == cmStateEnums::INTERFACE_LIBRARY &&
299
0
        type == cm::FileSetMetadata::SOURCES) {
300
0
      this->SetError(
301
0
        cmStrCat(R"(File set TYPE ")", cm::FileSetMetadata::SOURCES,
302
0
                 R"(" may not have "PUBLIC" )"
303
0
                 R"(or "PRIVATE" scope on INTERFACE libraries.)"));
304
0
      return false;
305
0
    }
306
307
0
    if (args.BaseDirs.empty()) {
308
0
      args.BaseDirs.emplace_back(this->Makefile->GetCurrentSourceDirectory());
309
0
    }
310
0
  } else {
311
0
    type = fileSet.first->GetType();
312
0
    if (!args.Type.empty() && args.Type != type) {
313
0
      this->SetError(cmStrCat(
314
0
        "Type \"", args.Type, "\" for file set \"", fileSet.first->GetName(),
315
0
        "\" does not match original type \"", type, '"'));
316
0
      return false;
317
0
    }
318
319
0
    if (visibility != fileSet.first->GetVisibility()) {
320
0
      this->SetError(cmStrCat("Scope ", scope, " for file set \"",
321
0
                              args.FileSet,
322
0
                              "\" does not match original scope ",
323
0
                              cm::FileSetMetadata::VisibilityToName(
324
0
                                fileSet.first->GetVisibility())));
325
0
      return false;
326
0
    }
327
0
  }
328
329
0
  auto files = this->Join(this->ConvertToAbsoluteContent(
330
0
    this->Target, args.Files, IsInterface::Yes, CheckCMP0076::No));
331
0
  if (!files.empty()) {
332
0
    fileSet.first->AddFileEntry(
333
0
      BT<std::string>(files, this->Makefile->GetBacktrace()));
334
0
  }
335
336
0
  auto baseDirectories = this->Join(this->ConvertToAbsoluteContent(
337
0
    this->Target, args.BaseDirs, IsInterface::Yes, CheckCMP0076::No));
338
0
  if (!baseDirectories.empty()) {
339
0
    fileSet.first->AddDirectoryEntry(
340
0
      BT<std::string>(baseDirectories, this->Makefile->GetBacktrace()));
341
0
    if (type == cm::FileSetMetadata::HEADERS) {
342
0
      for (auto const& dir : cmList{ baseDirectories }) {
343
0
        auto interfaceDirectoriesGenex =
344
0
          cmStrCat("$<BUILD_INTERFACE:", dir, '>');
345
0
        if (cm::FileSetMetadata::VisibilityIsForSelf(visibility)) {
346
0
          this->Target->AppendProperty("INCLUDE_DIRECTORIES",
347
0
                                       interfaceDirectoriesGenex,
348
0
                                       this->Makefile->GetBacktrace());
349
0
        }
350
0
        if (cm::FileSetMetadata::VisibilityIsForInterface(visibility)) {
351
0
          this->Target->AppendProperty("INTERFACE_INCLUDE_DIRECTORIES",
352
0
                                       interfaceDirectoriesGenex,
353
0
                                       this->Makefile->GetBacktrace());
354
0
        }
355
0
      }
356
0
    }
357
0
  }
358
359
0
  return true;
360
0
}
361
362
} // namespace
363
364
bool cmTargetSourcesCommand(std::vector<std::string> const& args,
365
                            cmExecutionStatus& status)
366
0
{
367
0
  return TargetSourcesImpl(status).HandleArguments(args, "SOURCES");
368
0
}