/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 "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 "cmStateTypes.h" |
23 | | #include "cmStringAlgorithms.h" |
24 | | #include "cmSystemTools.h" |
25 | | #include "cmTarget.h" |
26 | | #include "cmTargetPropCommandBase.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 | | 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 | 0 | { |
93 | 0 | if (!content.empty() && content.front() == "FILE_SET"_s) { |
94 | 0 | return this->HandleFileSetMode(scope, content); |
95 | 0 | } |
96 | 0 | return this->cmTargetPropCommandBase::PopulateTargetProperties( |
97 | 0 | scope, content, prepend, system); |
98 | 0 | } |
99 | | |
100 | | std::string Join(std::vector<std::string> const& content) override |
101 | 0 | { |
102 | 0 | return cmList::to_string(content); |
103 | 0 | } |
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 | 0 | { |
129 | | // Skip conversion in case old behavior has been explicitly requested |
130 | 0 | if (checkCmp0076 == CheckCMP0076::Yes && |
131 | 0 | this->Makefile->GetPolicyStatus(cmPolicies::CMP0076) == |
132 | 0 | cmPolicies::OLD) { |
133 | 0 | return content; |
134 | 0 | } |
135 | | |
136 | 0 | bool changedPath = false; |
137 | 0 | std::vector<std::string> absoluteContent; |
138 | 0 | absoluteContent.reserve(content.size()); |
139 | 0 | for (std::string const& src : content) { |
140 | 0 | std::string absoluteSrc; |
141 | 0 | if (cmSystemTools::FileIsFullPath(src) || |
142 | 0 | cmGeneratorExpression::Find(src) == 0 || |
143 | 0 | (isInterfaceContent == IsInterface::No && |
144 | 0 | (this->Makefile->GetCurrentSourceDirectory() == |
145 | 0 | tgt->GetMakefile()->GetCurrentSourceDirectory()))) { |
146 | 0 | absoluteSrc = src; |
147 | 0 | } else { |
148 | 0 | changedPath = true; |
149 | 0 | absoluteSrc = |
150 | 0 | cmStrCat(this->Makefile->GetCurrentSourceDirectory(), '/', src); |
151 | 0 | } |
152 | 0 | absoluteContent.push_back(absoluteSrc); |
153 | 0 | } |
154 | |
|
155 | 0 | if (!changedPath) { |
156 | 0 | return content; |
157 | 0 | } |
158 | | |
159 | 0 | bool issueMessage = true; |
160 | 0 | bool useAbsoluteContent = false; |
161 | 0 | std::ostringstream e; |
162 | 0 | if (checkCmp0076 == CheckCMP0076::Yes) { |
163 | 0 | switch (this->Makefile->GetPolicyStatus(cmPolicies::CMP0076)) { |
164 | 0 | case cmPolicies::WARN: |
165 | 0 | e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0076) << "\n"; |
166 | 0 | break; |
167 | 0 | case cmPolicies::OLD: |
168 | 0 | issueMessage = false; |
169 | 0 | break; |
170 | 0 | case cmPolicies::NEW: { |
171 | 0 | issueMessage = false; |
172 | 0 | useAbsoluteContent = true; |
173 | 0 | break; |
174 | 0 | } |
175 | 0 | } |
176 | 0 | } else { |
177 | 0 | issueMessage = false; |
178 | 0 | useAbsoluteContent = true; |
179 | 0 | } |
180 | | |
181 | 0 | if (issueMessage) { |
182 | 0 | if (isInterfaceContent == IsInterface::Yes) { |
183 | 0 | e << "An interface source of target \"" << tgt->GetName() |
184 | 0 | << "\" has a relative path."; |
185 | 0 | } else { |
186 | 0 | e << "A private source from a directory other than that of target \"" |
187 | 0 | << tgt->GetName() << "\" has a relative path."; |
188 | 0 | } |
189 | 0 | this->Makefile->IssueMessage(MessageType::AUTHOR_WARNING, e.str()); |
190 | 0 | } |
191 | |
|
192 | 0 | return useAbsoluteContent ? absoluteContent : content; |
193 | 0 | } |
194 | | |
195 | | bool TargetSourcesImpl::HandleFileSetMode( |
196 | | std::string const& scope, std::vector<std::string> const& content) |
197 | 0 | { |
198 | 0 | auto args = FileSetsArgsParser.Parse(content, /*unparsedArguments=*/nullptr); |
199 | |
|
200 | 0 | for (auto& argList : args.FileSets) { |
201 | 0 | argList.emplace(argList.begin(), "FILE_SET"_s); |
202 | 0 | if (!this->HandleOneFileSet(scope, argList)) { |
203 | 0 | return false; |
204 | 0 | } |
205 | 0 | } |
206 | | |
207 | 0 | return true; |
208 | 0 | } |
209 | | |
210 | | bool TargetSourcesImpl::HandleOneFileSet( |
211 | | std::string const& scope, std::vector<std::string> const& content) |
212 | 0 | { |
213 | 0 | std::vector<std::string> unparsed; |
214 | 0 | auto args = FileSetArgsParser.Parse(content, &unparsed); |
215 | |
|
216 | 0 | if (!unparsed.empty()) { |
217 | 0 | this->SetError( |
218 | 0 | cmStrCat("Unrecognized keyword: \"", unparsed.front(), '"')); |
219 | 0 | return false; |
220 | 0 | } |
221 | | |
222 | 0 | if (args.FileSet.empty()) { |
223 | 0 | this->SetError("FILE_SET must not be empty"); |
224 | 0 | return false; |
225 | 0 | } |
226 | | |
227 | 0 | if (this->Target->GetType() == cmStateEnums::UTILITY) { |
228 | 0 | this->SetError("FILE_SETs may not be added to custom targets"); |
229 | 0 | return false; |
230 | 0 | } |
231 | 0 | if (this->Target->IsFrameworkOnApple()) { |
232 | 0 | this->SetError("FILE_SETs may not be added to FRAMEWORK targets"); |
233 | 0 | return false; |
234 | 0 | } |
235 | | |
236 | 0 | if (!args.Type.empty() && !cm::FileSetMetadata::IsKnownType(args.Type)) { |
237 | 0 | this->SetError( |
238 | 0 | cmStrCat("File set TYPE may only be \"", |
239 | 0 | cmJoin(cm::FileSetMetadata::GetKnownTypes(), "\", \""), '"')); |
240 | 0 | return false; |
241 | 0 | } |
242 | 0 | if (args.Type.empty() && args.FileSet[0] >= 'A' && args.FileSet[0] <= 'Z' && |
243 | 0 | !cm::FileSetMetadata::IsKnownType(args.FileSet)) { |
244 | 0 | this->SetError( |
245 | 0 | cmStrCat("FILE_SET names starting with a capital letter are reserved " |
246 | 0 | "for built-in file sets and may only be \"", |
247 | 0 | cmJoin(cm::FileSetMetadata::GetKnownTypes(), "\", \""), '"')); |
248 | 0 | return false; |
249 | 0 | } |
250 | 0 | if (!args.Type.empty() && args.FileSet[0] >= 'A' && args.FileSet[0] <= 'Z' && |
251 | 0 | args.Type != args.FileSet) { |
252 | 0 | this->SetError(cmStrCat("FILE_SET name starting with a capital letter " |
253 | 0 | "must match the TYPE name \"", |
254 | 0 | args.Type, '"')); |
255 | 0 | return false; |
256 | 0 | } |
257 | | |
258 | 0 | bool const isDefault = args.Type == args.FileSet || |
259 | 0 | (args.Type.empty() && cm::FileSetMetadata::IsKnownType(args.FileSet)); |
260 | |
|
261 | 0 | if (!isDefault && !cm::FileSetMetadata::IsValidName(args.FileSet)) { |
262 | 0 | this->SetError("Non-default file set name must contain only letters, " |
263 | 0 | "numbers, and underscores, and must not start with a " |
264 | 0 | "capital letter or underscore"); |
265 | 0 | return false; |
266 | 0 | } |
267 | | |
268 | 0 | std::string type = isDefault ? args.FileSet : args.Type; |
269 | 0 | cm::FileSetMetadata::Visibility visibility = |
270 | 0 | cm::FileSetMetadata::VisibilityFromName(scope, this->Makefile); |
271 | |
|
272 | 0 | auto fileSet = |
273 | 0 | this->Target->GetOrCreateFileSet(args.FileSet, type, visibility); |
274 | 0 | if (fileSet.second) { |
275 | 0 | if (type.empty()) { |
276 | 0 | this->SetError("Must specify a TYPE when creating file set"); |
277 | 0 | return false; |
278 | 0 | } |
279 | | |
280 | 0 | if (cm::FileSetMetadata::VisibilityIsForSelf(visibility) && |
281 | 0 | this->Target->GetType() == cmStateEnums::INTERFACE_LIBRARY && |
282 | 0 | !this->Target->IsImported()) { |
283 | 0 | if (type == cm::FileSetMetadata::CXX_MODULES) { |
284 | 0 | this->SetError( |
285 | 0 | cmStrCat(R"(File set TYPE ")", cm::FileSetMetadata::CXX_MODULES, |
286 | 0 | R"(" may not have "PUBLIC" )" |
287 | 0 | R"(or "PRIVATE" visibility on INTERFACE libraries.)")); |
288 | 0 | return false; |
289 | 0 | } |
290 | 0 | } |
291 | | |
292 | | // FIXME(https://wg21.link/P3470): This condition can go |
293 | | // away when interface-only module units are a thing. |
294 | 0 | if (cm::FileSetMetadata::VisibilityIsForInterface(visibility) && |
295 | 0 | !cm::FileSetMetadata::VisibilityIsForSelf(visibility) && |
296 | 0 | !this->Target->IsImported()) { |
297 | 0 | if (type == cm::FileSetMetadata::CXX_MODULES) { |
298 | 0 | this->SetError(cmStrCat(R"(File set TYPE ")", |
299 | 0 | cm::FileSetMetadata::CXX_MODULES, |
300 | 0 | R"(" may not have "INTERFACE" visibility)")); |
301 | 0 | return false; |
302 | 0 | } |
303 | 0 | } |
304 | | |
305 | 0 | if (args.BaseDirs.empty()) { |
306 | 0 | args.BaseDirs.emplace_back(this->Makefile->GetCurrentSourceDirectory()); |
307 | 0 | } |
308 | 0 | } else { |
309 | 0 | type = fileSet.first->GetType(); |
310 | 0 | if (!args.Type.empty() && args.Type != type) { |
311 | 0 | this->SetError(cmStrCat( |
312 | 0 | "Type \"", args.Type, "\" for file set \"", fileSet.first->GetName(), |
313 | 0 | "\" does not match original type \"", type, '"')); |
314 | 0 | return false; |
315 | 0 | } |
316 | | |
317 | 0 | if (visibility != fileSet.first->GetVisibility()) { |
318 | 0 | this->SetError(cmStrCat("Scope ", scope, " for file set \"", |
319 | 0 | args.FileSet, |
320 | 0 | "\" does not match original scope ", |
321 | 0 | cm::FileSetMetadata::VisibilityToName( |
322 | 0 | fileSet.first->GetVisibility()))); |
323 | 0 | return false; |
324 | 0 | } |
325 | 0 | } |
326 | | |
327 | 0 | auto files = this->Join(this->ConvertToAbsoluteContent( |
328 | 0 | this->Target, args.Files, IsInterface::Yes, CheckCMP0076::No)); |
329 | 0 | if (!files.empty()) { |
330 | 0 | fileSet.first->AddFileEntry( |
331 | 0 | BT<std::string>(files, this->Makefile->GetBacktrace())); |
332 | 0 | } |
333 | |
|
334 | 0 | auto baseDirectories = this->Join(this->ConvertToAbsoluteContent( |
335 | 0 | this->Target, args.BaseDirs, IsInterface::Yes, CheckCMP0076::No)); |
336 | 0 | if (!baseDirectories.empty()) { |
337 | 0 | fileSet.first->AddDirectoryEntry( |
338 | 0 | BT<std::string>(baseDirectories, this->Makefile->GetBacktrace())); |
339 | 0 | if (type == cm::FileSetMetadata::HEADERS) { |
340 | 0 | for (auto const& dir : cmList{ baseDirectories }) { |
341 | 0 | auto interfaceDirectoriesGenex = |
342 | 0 | cmStrCat("$<BUILD_INTERFACE:", dir, '>'); |
343 | 0 | if (cm::FileSetMetadata::VisibilityIsForSelf(visibility)) { |
344 | 0 | this->Target->AppendProperty("INCLUDE_DIRECTORIES", |
345 | 0 | interfaceDirectoriesGenex, |
346 | 0 | this->Makefile->GetBacktrace()); |
347 | 0 | } |
348 | 0 | if (cm::FileSetMetadata::VisibilityIsForInterface(visibility)) { |
349 | 0 | this->Target->AppendProperty("INTERFACE_INCLUDE_DIRECTORIES", |
350 | 0 | interfaceDirectoriesGenex, |
351 | 0 | this->Makefile->GetBacktrace()); |
352 | 0 | } |
353 | 0 | } |
354 | 0 | } |
355 | 0 | } |
356 | |
|
357 | 0 | return true; |
358 | 0 | } |
359 | | |
360 | | } // namespace |
361 | | |
362 | | bool cmTargetSourcesCommand(std::vector<std::string> const& args, |
363 | | cmExecutionStatus& status) |
364 | 0 | { |
365 | 0 | return TargetSourcesImpl(status).HandleArguments(args, "SOURCES"); |
366 | 0 | } |