Coverage Report

Created: 2026-02-09 06:05

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 "cmFileSet.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
  bool issueMessage = true;
159
0
  bool useAbsoluteContent = false;
160
0
  std::ostringstream e;
161
0
  if (checkCmp0076 == CheckCMP0076::Yes) {
162
0
    switch (this->Makefile->GetPolicyStatus(cmPolicies::CMP0076)) {
163
0
      case cmPolicies::WARN:
164
0
        e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0076) << "\n";
165
0
        break;
166
0
      case cmPolicies::OLD:
167
0
        issueMessage = false;
168
0
        break;
169
0
      case cmPolicies::NEW: {
170
0
        issueMessage = false;
171
0
        useAbsoluteContent = true;
172
0
        break;
173
0
      }
174
0
    }
175
0
  } else {
176
0
    issueMessage = false;
177
0
    useAbsoluteContent = true;
178
0
  }
179
180
0
  if (issueMessage) {
181
0
    if (isInterfaceContent == IsInterface::Yes) {
182
0
      e << "An interface source of target \"" << tgt->GetName()
183
0
        << "\" has a relative path.";
184
0
    } else {
185
0
      e << "A private source from a directory other than that of target \""
186
0
        << tgt->GetName() << "\" has a relative path.";
187
0
    }
188
0
    this->Makefile->IssueMessage(MessageType::AUTHOR_WARNING, e.str());
189
0
  }
190
191
0
  return useAbsoluteContent ? absoluteContent : content;
192
0
}
193
194
bool TargetSourcesImpl::HandleFileSetMode(
195
  std::string const& scope, std::vector<std::string> const& content)
196
0
{
197
0
  auto args = FileSetsArgsParser.Parse(content, /*unparsedArguments=*/nullptr);
198
199
0
  for (auto& argList : args.FileSets) {
200
0
    argList.emplace(argList.begin(), "FILE_SET"_s);
201
0
    if (!this->HandleOneFileSet(scope, argList)) {
202
0
      return false;
203
0
    }
204
0
  }
205
206
0
  return true;
207
0
}
208
209
bool TargetSourcesImpl::HandleOneFileSet(
210
  std::string const& scope, std::vector<std::string> const& content)
211
0
{
212
0
  std::vector<std::string> unparsed;
213
0
  auto args = FileSetArgsParser.Parse(content, &unparsed);
214
215
0
  if (!unparsed.empty()) {
216
0
    this->SetError(
217
0
      cmStrCat("Unrecognized keyword: \"", unparsed.front(), '"'));
218
0
    return false;
219
0
  }
220
221
0
  if (args.FileSet.empty()) {
222
0
    this->SetError("FILE_SET must not be empty");
223
0
    return false;
224
0
  }
225
226
0
  if (this->Target->GetType() == cmStateEnums::UTILITY) {
227
0
    this->SetError("FILE_SETs may not be added to custom targets");
228
0
    return false;
229
0
  }
230
0
  if (this->Target->IsFrameworkOnApple()) {
231
0
    this->SetError("FILE_SETs may not be added to FRAMEWORK targets");
232
0
    return false;
233
0
  }
234
235
0
  bool const isDefault = args.Type == args.FileSet ||
236
0
    (args.Type.empty() && args.FileSet[0] >= 'A' && args.FileSet[0] <= 'Z');
237
0
  std::string type = isDefault ? args.FileSet : args.Type;
238
239
0
  cmFileSetVisibility visibility =
240
0
    cmFileSetVisibilityFromName(scope, this->Makefile);
241
242
0
  auto fileSet =
243
0
    this->Target->GetOrCreateFileSet(args.FileSet, type, visibility);
244
0
  if (fileSet.second) {
245
0
    if (!isDefault) {
246
0
      if (!cmFileSet::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
0
    }
253
0
    if (type.empty()) {
254
0
      this->SetError("Must specify a TYPE when creating file set");
255
0
      return false;
256
0
    }
257
0
    if (type != "HEADERS"_s && type != "CXX_MODULES"_s) {
258
0
      this->SetError(
259
0
        R"(File set TYPE may only be "HEADERS" or "CXX_MODULES")");
260
0
      return false;
261
0
    }
262
263
0
    if (cmFileSetVisibilityIsForSelf(visibility) &&
264
0
        this->Target->GetType() == cmStateEnums::INTERFACE_LIBRARY &&
265
0
        !this->Target->IsImported()) {
266
0
      if (type == "CXX_MODULES"_s) {
267
0
        this->SetError(R"(File set TYPE "CXX_MODULES" may not have "PUBLIC" )"
268
0
                       R"(or "PRIVATE" visibility on INTERFACE libraries.)");
269
0
        return false;
270
0
      }
271
0
    }
272
273
    // FIXME(https://wg21.link/P3470): This condition can go
274
    // away when interface-only module units are a thing.
275
0
    if (cmFileSetVisibilityIsForInterface(visibility) &&
276
0
        !cmFileSetVisibilityIsForSelf(visibility) &&
277
0
        !this->Target->IsImported()) {
278
0
      if (type == "CXX_MODULES"_s) {
279
0
        this->SetError(
280
0
          R"(File set TYPE "CXX_MODULES" may not have "INTERFACE" visibility)");
281
0
        return false;
282
0
      }
283
0
    }
284
285
0
    if (args.BaseDirs.empty()) {
286
0
      args.BaseDirs.emplace_back(this->Makefile->GetCurrentSourceDirectory());
287
0
    }
288
0
  } else {
289
0
    type = fileSet.first->GetType();
290
0
    if (!args.Type.empty() && args.Type != type) {
291
0
      this->SetError(cmStrCat(
292
0
        "Type \"", args.Type, "\" for file set \"", fileSet.first->GetName(),
293
0
        "\" does not match original type \"", type, '"'));
294
0
      return false;
295
0
    }
296
297
0
    if (visibility != fileSet.first->GetVisibility()) {
298
0
      this->SetError(
299
0
        cmStrCat("Scope ", scope, " for file set \"", args.FileSet,
300
0
                 "\" does not match original scope ",
301
0
                 cmFileSetVisibilityToName(fileSet.first->GetVisibility())));
302
0
      return false;
303
0
    }
304
0
  }
305
306
0
  auto files = this->Join(this->ConvertToAbsoluteContent(
307
0
    this->Target, args.Files, IsInterface::Yes, CheckCMP0076::No));
308
0
  if (!files.empty()) {
309
0
    fileSet.first->AddFileEntry(
310
0
      BT<std::string>(files, this->Makefile->GetBacktrace()));
311
0
  }
312
313
0
  auto baseDirectories = this->Join(this->ConvertToAbsoluteContent(
314
0
    this->Target, args.BaseDirs, IsInterface::Yes, CheckCMP0076::No));
315
0
  if (!baseDirectories.empty()) {
316
0
    fileSet.first->AddDirectoryEntry(
317
0
      BT<std::string>(baseDirectories, this->Makefile->GetBacktrace()));
318
0
    if (type == "HEADERS"_s) {
319
0
      for (auto const& dir : cmList{ baseDirectories }) {
320
0
        auto interfaceDirectoriesGenex =
321
0
          cmStrCat("$<BUILD_INTERFACE:", dir, '>');
322
0
        if (cmFileSetVisibilityIsForSelf(visibility)) {
323
0
          this->Target->AppendProperty("INCLUDE_DIRECTORIES",
324
0
                                       interfaceDirectoriesGenex,
325
0
                                       this->Makefile->GetBacktrace());
326
0
        }
327
0
        if (cmFileSetVisibilityIsForInterface(visibility)) {
328
0
          this->Target->AppendProperty("INTERFACE_INCLUDE_DIRECTORIES",
329
0
                                       interfaceDirectoriesGenex,
330
0
                                       this->Makefile->GetBacktrace());
331
0
        }
332
0
      }
333
0
    }
334
0
  }
335
336
0
  return true;
337
0
}
338
339
} // namespace
340
341
bool cmTargetSourcesCommand(std::vector<std::string> const& args,
342
                            cmExecutionStatus& status)
343
0
{
344
0
  return TargetSourcesImpl(status).HandleArguments(args, "SOURCES");
345
0
}