forked from Expander/FlexibleSUSY
-
Notifications
You must be signed in to change notification settings - Fork 14
/
depgen.cpp
397 lines (334 loc) · 11.9 KB
/
depgen.cpp
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
// ====================================================================
// This file is part of FlexibleSUSY.
//
// FlexibleSUSY is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published
// by the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
//
// FlexibleSUSY is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with FlexibleSUSY. If not, see
// <http://www.gnu.org/licenses/>.
// ====================================================================
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <set>
#include <string>
#include <utility>
#include <vector>
namespace flexiblesusy {
/// returns directory from file name
std::string directory(const std::string& file_name)
{
const std::size_t pos = file_name.find_last_of("/\\");
if (pos == std::string::npos)
return ".";
return file_name.substr(0,pos);
}
/// returns file name w/o directory
std::string filename(const std::string& file_name)
{
const std::size_t pos = file_name.find_last_of("/\\");
return file_name.substr(pos+1);
}
/// returns file names w/o directory
std::vector<std::string> filenames(const std::vector<std::string>& file_names)
{
std::vector<std::string> result(file_names.size());
std::transform(file_names.begin(), file_names.end(), result.begin(), filename);
return result;
}
/// checks if given file exists
bool file_exists(const std::string& name)
{
if (FILE *file = std::fopen(name.c_str(), "r")) {
std::fclose(file);
return true;
}
return false;
}
struct Is_not_duplicate {
bool operator()(const std::string& element) {
return s.insert(element).second;
}
private:
std::set<std::string> s;
};
struct Is_not_duplicate_ignore_path {
bool operator()(const std::string& element) {
return s.insert(filename(element)).second;
}
private:
std::set<std::string> s;
};
/// deletes duplicate elements from a vector (preseves order)
template <typename Predicate = decltype(Is_not_duplicate())>
std::vector<std::string> delete_duplicates(
const std::vector<std::string>& vec,
Predicate pred = Is_not_duplicate())
{
std::vector<std::string> unique_vector;
std::copy_if(vec.begin(), vec.end(), std::back_inserter(unique_vector),
std::ref(pred));
return unique_vector;
}
/// replace file name extension by `ext'
std::string replace_extension(const std::string& str, const std::string& ext)
{
const std::string no_ext(str.substr(0, str.find_last_of(".")));
return no_ext + '.' + ext;
}
/// tests whether `str' starts with `prefix'
bool starts_with(const std::string& str, const std::string& prefix)
{
return !str.compare(0, prefix.size(), prefix);
}
/// removes whitespace from left side of string
std::string trim_left(const std::string& s)
{
std::string str(s);
str.erase(str.begin(),
std::find_if(str.begin(), str.end(),
[] (std::string::value_type c) { return !std::isspace(c); }));
return str;
}
/// print usage message
void print_usage(const std::string& program_name)
{
std::cout << "Usage: " << program_name << " [options] filename\n"
"\n"
"Options:\n"
" -I<path> Search for header files in <path>\n"
" -MF <file> Write dependencies to <file>\n"
" -MG Add missing headers to dependency list\n"
" -MM Ignore system headers enclosed by < and >\n"
" -MMD <file> Equivalent to -MM -MF <file>\n"
" -MP Add phony target for each dependency other than main file\n"
" -MT <target> Set name of the target\n"
" -o <file> Equivalent to -MF <file>\n"
" --help,-h Print this help message and exit\n"
"\n"
"Unsupported options:\n"
" -M Add system headers to dependency list\n"
" -MD <file> Equivalent to -M -MF <file>\n"
" -MQ <target> Same as -MT <traget> but quote characters special to make\n";
}
/// print dependency list
void print_dependencies(const std::string& target_name,
const std::vector<std::string>& dependencies,
std::ostream& ostr)
{
ostr << target_name << ':';
for (const auto& d: dependencies)
ostr << ' ' << d;
ostr << '\n';
}
/// print empty phony targets for each dependency
void print_empty_phony_targets(const std::vector<std::string>& dependencies,
std::ostream& ostr)
{
for (const auto& d: dependencies)
ostr << '\n' << d << ":\n";
}
/// returns file name from include "..." statement
std::string get_filename_from_include(std::string line)
{
line = trim_left(line);
if (line.empty() || line[0] != '#')
return "";
// skip `#' and following whitespace
line = trim_left(line.substr(std::strlen("#")));
if (!starts_with(line, "include"))
return "";
// skip `include'
line = trim_left(line.substr(std::strlen("include")));
// extract file name from "file-name"
std::size_t pos1 = line.find_first_of('"');
if (pos1 == std::string::npos)
return "";
pos1++;
std::size_t pos2 = line.find_first_of('"', pos1);
if (pos2 == std::string::npos)
return "";
pos2--;
return line.substr(pos1, pos2);
}
/// extract include statements from file (ignoring system headers)
std::vector<std::string> get_included_files(const std::string& file_name)
{
std::ifstream istr(file_name);
std::vector<std::string> includes;
std::string line;
while (std::getline(istr, line)) {
std::string file(get_filename_from_include(line));
if (!file.empty())
includes.push_back(std::move(file));
}
return includes;
}
/// prepend `str' to all strings
std::vector<std::string> prepend(const std::string& str,
const std::vector<std::string>& strings)
{
std::vector<std::string> result(strings);
for (auto& s: result)
s = str + s;
return result;
}
/// returns files in directory `dir' which fulfill the predicate
template <class Predicate>
std::vector<std::string> filter(const std::string& dir,
const std::vector<std::string>& files,
const Predicate& pred)
{
const std::string dirname(dir.empty() || dir == "." ? "" : dir + '/');
const std::vector<std::string> files_in_dir(prepend(dirname, files));
std::vector<std::string> existing_files;
std::copy_if(files_in_dir.begin(), files_in_dir.end(),
std::back_inserter(existing_files), pred);
return existing_files;
}
/// search recursively for include statments in `file_name'
/// taking into account only directories given in `paths'
std::vector<std::string> search_includes(const std::string& file_name,
const std::vector<std::string>& paths,
bool ignore_non_existing = true,
unsigned max_depth = 10)
{
if (max_depth == 0)
return std::vector<std::string>();
// find included files from #include statements
const std::vector<std::string> includes(get_included_files(file_name));
// select only files that exist in paths
std::vector<std::string> existing;
for (const auto& p: paths) {
const std::vector<std::string> existing_in_path(filter(p, includes, file_exists));
existing.insert(existing.end(), existing_in_path.begin(), existing_in_path.end());
}
// search recursively for included files in existing headers
const std::vector<std::string> tmp_existing(existing);
for (const auto& f: tmp_existing) {
const std::vector<std::string> sub_existing(
search_includes(f, paths, ignore_non_existing, max_depth - 1));
existing.insert(existing.end(), sub_existing.begin(), sub_existing.end());
}
// search for non-existing headers
if (!ignore_non_existing) {
std::vector<std::string> includes_without_path(filenames(includes));
std::sort(includes_without_path.begin(), includes_without_path.end());
std::vector<std::string> existing_without_path(filenames(existing));
std::sort(existing_without_path.begin(), existing_without_path.end());
std::vector<std::string> non_existing;
std::set_difference(includes_without_path.begin(), includes_without_path.end(),
existing_without_path.begin(), existing_without_path.end(),
std::back_inserter(non_existing));
existing.insert(existing.end(), non_existing.begin(), non_existing.end());
}
return existing;
}
} // namespace flexiblesusy
int main(int argc, char* argv[])
{
using namespace flexiblesusy;
if (argc < 2) {
std::cerr << "Error: no file given\n";
print_usage(argv[0]);
return EXIT_FAILURE;
}
// include paths
std::vector<std::string> paths;
std::string file_name, target_name, output_file;
bool ignore_non_existing = true; // -MG
bool add_empty_phony_targets = false; // -MP
for (int i = 1; i < argc; i++) {
const std::string arg(argv[i]);
if (starts_with(arg, "-I") && arg.length() > 2) {
paths.push_back(arg.substr(std::strlen("-I")));
continue;
}
if (arg == "-MG") {
ignore_non_existing = false;
continue;
}
if (arg == "-MM") {
// ignore headers from system directories (default)
continue;
}
if (arg == "-M") {
std::cerr << "Warning: ignoring unsupported option " << arg << '\n';
continue;
}
if (arg == "-MD" || arg == "-MQ") {
std::cerr << "Warning: ignoring unsupported option " << arg << '\n';
i++;
continue;
}
if (arg == "-MP") {
add_empty_phony_targets = true;
continue;
}
if (arg == "-MT" && i + 1 < argc) {
target_name = argv[++i];
continue;
}
if ((arg == "-MF" || arg == "-MMD" || arg == "-o") && i + 1 < argc) {
output_file = argv[++i];
continue;
}
if (arg == "--help" || arg == "-h") {
print_usage(argv[0]);
return EXIT_SUCCESS;
}
// interpret last argument as file name
if (i + 1 == argc) {
file_name = arg;
if (!file_exists(file_name)) {
std::cerr << "Error: file does not exist: " << file_name << '\n';
return EXIT_FAILURE;
}
continue;
}
std::cerr << "Error: unknown option: " << arg << '\n';
print_usage(argv[0]);
return EXIT_FAILURE;
}
// select output stream
std::ofstream fstr(output_file);
std::ostream* ostr = &std::cout;
if (!output_file.empty()) {
if (!fstr.good()) {
std::cerr << "Error: cannot write to file " << output_file << '\n';
return EXIT_FAILURE;
}
ostr = &fstr;
}
// include paths
paths.insert(paths.begin(), directory(file_name));
paths.push_back(".");
paths = delete_duplicates(paths);
// search for header inclusions in file
const std::vector<std::string> dependencies
= delete_duplicates(
search_includes(file_name, paths, ignore_non_existing),
Is_not_duplicate_ignore_path());
// prepend file itself to dependency list
std::vector<std::string> dependencies_and_main(dependencies);
dependencies_and_main.insert(dependencies_and_main.begin(), file_name);
if (target_name.empty())
target_name = replace_extension(filename(file_name), "o");
// output
print_dependencies(target_name, dependencies_and_main, *ostr);
if (add_empty_phony_targets)
print_empty_phony_targets(dependencies, *ostr);
return EXIT_SUCCESS;
}