Coverage Report

Created: 2026-04-29 07:01

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