1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmSourceGroupCommand.h"
#include <cstddef>
#include <map>
#include <memory>
#include <set>
#include <utility>
#include <cmext/algorithm>
#include "cmExecutionStatus.h"
#include "cmMakefile.h"
#include "cmSourceFile.h"
#include "cmSourceFileLocation.h"
#include "cmSourceGroup.h"
#include "cmStringAlgorithms.h"
#include "cmSystemTools.h"
namespace {
using ParsedArguments = std::map<std::string, std::vector<std::string>>;
using ExpectedOptions = std::vector<std::string>;
const std::string kTreeOptionName = "TREE";
const std::string kPrefixOptionName = "PREFIX";
const std::string kFilesOptionName = "FILES";
const std::string kRegexOptionName = "REGULAR_EXPRESSION";
const std::string kSourceGroupOptionName = "<sg_name>";
std::vector<std::string> tokenizePath(const std::string& path)
{
return cmTokenize(path, "\\/");
}
std::set<std::string> getSourceGroupFilesPaths(
const std::string& root, const std::vector<std::string>& files)
{
std::set<std::string> ret;
const std::string::size_type rootLength = root.length();
for (std::string const& file : files) {
ret.insert(file.substr(rootLength + 1)); // +1 to also omnit last '/'
}
return ret;
}
bool rootIsPrefix(const std::string& root,
const std::vector<std::string>& files, std::string& error)
{
for (std::string const& file : files) {
if (!cmHasPrefix(file, root)) {
error = cmStrCat("ROOT: ", root, " is not a prefix of file: ", file);
return false;
}
}
return true;
}
std::vector<std::string> prepareFilesPathsForTree(
const std::vector<std::string>& filesPaths,
const std::string& currentSourceDir)
{
std::vector<std::string> prepared;
prepared.reserve(filesPaths.size());
for (auto const& filePath : filesPaths) {
std::string fullPath =
cmSystemTools::CollapseFullPath(filePath, currentSourceDir);
// If provided file path is actually not a directory, silently ignore it.
if (cmSystemTools::FileIsDirectory(fullPath)) {
continue;
}
// Handle directory that doesn't exist yet.
if (!fullPath.empty() &&
(fullPath.back() == '/' || fullPath.back() == '\\')) {
continue;
}
prepared.emplace_back(std::move(fullPath));
}
return prepared;
}
bool addFilesToItsSourceGroups(const std::string& root,
const std::set<std::string>& sgFilesPaths,
const std::string& prefix, cmMakefile& makefile,
std::string& errorMsg)
{
cmSourceGroup* sg;
for (std::string const& sgFilesPath : sgFilesPaths) {
std::vector<std::string> tokenizedPath;
if (!prefix.empty()) {
tokenizedPath = tokenizePath(cmStrCat(prefix, '/', sgFilesPath));
} else {
tokenizedPath = tokenizePath(sgFilesPath);
}
if (!tokenizedPath.empty()) {
tokenizedPath.pop_back();
if (tokenizedPath.empty()) {
tokenizedPath.emplace_back();
}
sg = makefile.GetOrCreateSourceGroup(tokenizedPath);
if (!sg) {
errorMsg = "Could not create source group for file: " + sgFilesPath;
return false;
}
const std::string fullPath =
cmSystemTools::CollapseFullPath(sgFilesPath, root);
sg->AddGroupFile(fullPath);
}
}
return true;
}
ExpectedOptions getExpectedOptions()
{
ExpectedOptions options;
options.push_back(kTreeOptionName);
options.push_back(kPrefixOptionName);
options.push_back(kFilesOptionName);
options.push_back(kRegexOptionName);
return options;
}
bool isExpectedOption(const std::string& argument,
const ExpectedOptions& expectedOptions)
{
return cm::contains(expectedOptions, argument);
}
void parseArguments(const std::vector<std::string>& args,
ParsedArguments& parsedArguments)
{
const ExpectedOptions expectedOptions = getExpectedOptions();
size_t i = 0;
// at this point we know that args vector is not empty
// if first argument is not one of expected options it's source group name
if (!isExpectedOption(args[0], expectedOptions)) {
// get source group name and go to next argument
parsedArguments[kSourceGroupOptionName].push_back(args[0]);
++i;
}
for (; i < args.size();) {
// get current option and increment index to go to next argument
const std::string& currentOption = args[i++];
// create current option entry in parsed arguments
std::vector<std::string>& currentOptionArguments =
parsedArguments[currentOption];
// collect option arguments while we won't find another expected option
while (i < args.size() && !isExpectedOption(args[i], expectedOptions)) {
currentOptionArguments.push_back(args[i++]);
}
}
}
} // namespace
static bool checkArgumentsPreconditions(const ParsedArguments& parsedArguments,
std::string& errorMsg);
static bool processTree(cmMakefile& mf, ParsedArguments& parsedArguments,
std::string& errorMsg);
static bool checkSingleParameterArgumentPreconditions(
const std::string& argument, const ParsedArguments& parsedArguments,
std::string& errorMsg);
bool cmSourceGroupCommand(std::vector<std::string> const& args,
cmExecutionStatus& status)
{
if (args.empty()) {
status.SetError("called with incorrect number of arguments");
return false;
}
cmMakefile& mf = status.GetMakefile();
// If only two arguments are given, the pre-1.8 version of the
// command is being invoked.
bool isShortTreeSyntax =
((args.size() == 2) && (args[0] == kTreeOptionName) &&
cmSystemTools::FileIsDirectory(args[1]));
if (args.size() == 2 && args[1] != kFilesOptionName && !isShortTreeSyntax) {
cmSourceGroup* sg = mf.GetOrCreateSourceGroup(args[0]);
if (!sg) {
status.SetError("Could not create or find source group");
return false;
}
sg->SetGroupRegex(args[1].c_str());
return true;
}
ParsedArguments parsedArguments;
std::string errorMsg;
parseArguments(args, parsedArguments);
if (!checkArgumentsPreconditions(parsedArguments, errorMsg)) {
return false;
}
if (parsedArguments.find(kTreeOptionName) != parsedArguments.end()) {
if (!processTree(mf, parsedArguments, errorMsg)) {
status.SetError(errorMsg);
return false;
}
} else {
if (parsedArguments.find(kSourceGroupOptionName) ==
parsedArguments.end()) {
status.SetError("Missing source group name.");
return false;
}
cmSourceGroup* sg = mf.GetOrCreateSourceGroup(args[0]);
if (!sg) {
status.SetError("Could not create or find source group");
return false;
}
// handle regex
if (parsedArguments.find(kRegexOptionName) != parsedArguments.end()) {
const std::string& sgRegex = parsedArguments[kRegexOptionName].front();
sg->SetGroupRegex(sgRegex.c_str());
}
// handle files
const std::vector<std::string>& filesArguments =
parsedArguments[kFilesOptionName];
for (auto const& filesArg : filesArguments) {
std::string src = filesArg;
src =
cmSystemTools::CollapseFullPath(src, mf.GetCurrentSourceDirectory());
sg->AddGroupFile(src);
}
}
return true;
}
static bool checkArgumentsPreconditions(const ParsedArguments& parsedArguments,
std::string& errorMsg)
{
return checkSingleParameterArgumentPreconditions(
kPrefixOptionName, parsedArguments, errorMsg) &&
checkSingleParameterArgumentPreconditions(kTreeOptionName, parsedArguments,
errorMsg) &&
checkSingleParameterArgumentPreconditions(kRegexOptionName,
parsedArguments, errorMsg);
}
static bool processTree(cmMakefile& mf, ParsedArguments& parsedArguments,
std::string& errorMsg)
{
const std::string root =
cmSystemTools::CollapseFullPath(parsedArguments[kTreeOptionName].front());
std::string prefix = parsedArguments[kPrefixOptionName].empty()
? ""
: parsedArguments[kPrefixOptionName].front();
std::vector<std::string> files = parsedArguments[kFilesOptionName];
if (files.empty()) {
const std::vector<std::unique_ptr<cmSourceFile>>& srcFiles =
mf.GetSourceFiles();
for (const auto& srcFile : srcFiles) {
if (!srcFile->GetIsGenerated()) {
files.push_back(srcFile->GetLocation().GetFullPath());
}
}
}
const std::vector<std::string> filesVector =
prepareFilesPathsForTree(files, mf.GetCurrentSourceDirectory());
if (!rootIsPrefix(root, filesVector, errorMsg)) {
return false;
}
std::set<std::string> sourceGroupPaths =
getSourceGroupFilesPaths(root, filesVector);
return addFilesToItsSourceGroups(root, sourceGroupPaths, prefix, mf,
errorMsg);
}
static bool checkSingleParameterArgumentPreconditions(
const std::string& argument, const ParsedArguments& parsedArguments,
std::string& errorMsg)
{
auto foundArgument = parsedArguments.find(argument);
if (foundArgument != parsedArguments.end()) {
const std::vector<std::string>& optionArguments = foundArgument->second;
if (optionArguments.empty()) {
errorMsg = argument + " argument given without an argument.";
return false;
}
if (optionArguments.size() > 1) {
errorMsg = "too many arguments passed to " + argument + ".";
return false;
}
}
return true;
}
|