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