-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
909 lines (777 loc) · 27 KB
/
main.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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
// Copyright (c) 2018, Aaron Michaux
// All rights reserved.
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include <assert.h>
#include <ftw.h>
#include <libgen.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <algorithm>
#include <cctype>
#include <deque>
#include <fstream>
#include <iostream>
#include <regex>
#include <sstream>
#include <string>
#include <string_view>
#include <unordered_map>
#include <vector>
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
using std::cout;
using std::endl;
using std::istream;
using std::ostream;
using std::string;
using std::string_view;
using std::unordered_map;
using std::vector;
using namespace std::string_literals;
// ------------------------------------------------------------ global variables
static const char* empty_directory = "";
// ------------------------------------------------------------------ structures
struct Options
{
bool show_help = false;
bool has_error = false;
bool unity_build = true;
string module_dir = "";
string in_file = "";
string out_file = "";
std::unordered_map<string, string> defines = {};
};
struct State
{
State(const Options& opts_, istream& in_, ostream& out_)
: opts(opts_)
, in(in_)
, out(out_)
{
auto cwd = getcwd(nullptr, 0);
current_working_directory = cwd;
free(cwd);
// for stdin, stdout, stderr
n_descriptors = sysconf(_SC_OPEN_MAX) - 3;
env.insert(opts.defines.begin(), opts.defines.end());
}
bool has_error{false};
const Options& opts;
istream& in;
ostream& out;
std::deque<string> lines;
string current_working_directory{""};
vector<string> additional_filenames;
vector<string_view> additional_dirnames;
unordered_map<string, string> env; // cached environment variables
int n_descriptors{0};
};
struct FileCommand
{
string pattern;
std::regex glob;
string command;
vector<string> re_add_outputs;
vector<string> other_outputs;
};
struct FilterVariable
{
string variable;
string filter;
std::regex glob;
std::deque<string> products;
};
// -------------------------------------------------------------- predefinitions
static void show_help(const char* exec_name);
static Options parse_commandline(int argc, char** argv);
// Environment variables
static void substitute_env_variables(State& state, string& line_s, bool strict);
// String functions
static bool starts_with(const std::string& s, const std::string& prefix);
static void ltrim(std::string& s);
static void rtrim(std::string& s);
static void trim(std::string& s);
static bool is_empty_line(const std::string& s);
// Turns a glob pattern into a regex.
static std::regex make_glob(const string& pattern);
// Runs an individual matched substitution
static void command_substitute(State& state,
const string& fname,
const string_view dname,
const FileCommand& cmd,
vector<FilterVariable>& filters);
// Runs an entire +src command
static void process_src_command(State& state, const vector<string> command);
// The actual pipeline
static bool transform_input(State& state);
// ------------------------------------------------------------------- show-help
static void show_help(const char* exec_name)
{
printf(R"V0G0N(
Usage: %s [OPTIONS...] -i <filename>
-i <filename> Input filename (required); '-' for stdin
-o <filename> Output filename
-D <var=value> Set variable 'var' to 'value', as if an
environment variable.=
-m <dirname> The prebuilt modules path, which is prepended
to module dependencies discovered by the '?'
character.
Mobuis is a preprocessor for ninja.build files. It adds two features:
(1) Environment variable substitution using ${USER} like syntax.
(2) +src commands that search directory structures and generate build rules.
An example +src command is as follows:
+src cd=.. OBJS=*.o src tests
- */messages/*.cpp build $builddir/%%.o: cpp ^ | $builddir/%%.pb.h
~ cpp_flags = $cpp_flags -I$builddir/@
- *.cpp build $builddir/%%.o: cpp_pch ^ | $builddir/$pchfile.gch
- *.proto build $builddir/%%.pb.cc! $builddir/%%.pb.h: proto ^
'+src' starts the command. All subsequent lines being with '-' or '~'.
The command cds up one directory, and searches the 'src' and 'tests'
directories -- geting a listing of all files.
Any outputs that match '*.o' are put into the OBJS environment variable,
which can be later expanded.
Every file is matched against the first possible '-' line.
(A '~' line is merely an extension of the previous '-' line.)
When the match is made, then output is generated by text substitution, where:
+ '%%' matches the filename without extension
+ '^' is the full filename
+ '#' same as '%%', but without the search directory, and '/' => '.'
+ '@' is dirname(filename)
+ '&' is basename(filename)
If a ninja build product ends in a '!', then it is added to the list
of files found on the +src line, to be later processed through
the '-' sequence.
)V0G0N",
exec_name);
}
// ---------------------------------------------------------- parse command-line
static Options parse_commandline(int argc, char** argv)
{
Options opts;
auto safe_s = [&](int& ind) -> string {
++ind;
if(ind >= argc) {
fprintf(stderr, "Expected argument after '%s'.\n", argv[ind]);
opts.has_error = true;
return "";
}
return argv[ind];
};
auto process_define = [&](const string& s) {
auto pos = s.find('=');
if(pos == string::npos)
opts.defines[s] = "";
else
opts.defines[s.substr(0, pos)] = s.substr(pos + 1);
};
for(int i = 1; i < argc && !opts.has_error; ++i) {
string arg = argv[i];
if(arg == "-h" || arg == "--help") {
opts.show_help = true;
} else if(arg == "-i") {
opts.in_file = safe_s(i);
} else if(arg == "-") {
opts.in_file = arg;
} else if(arg == "-o") {
opts.out_file = safe_s(i);
} else if(arg == "-m") {
opts.module_dir = safe_s(i);
} else if(arg == "-D") {
process_define(safe_s(i));
} else if(starts_with(arg, "-D")) {
process_define(&arg[2]);
} else {
fprintf(stderr, "Unknown argument '%s'.\n", argv[i]);
opts.has_error = true;
}
}
if(!opts.show_help && opts.in_file == "") {
fprintf(stderr, "Must specify an input file.\n");
opts.has_error = true;
}
return opts;
}
// ------------------------------------------------------------------------ main
int main(int argc, char** argv)
{
// -- Parse command-line
auto opts = parse_commandline(argc, argv);
// -- Analyze options
if(opts.show_help) {
show_help(basename(argv[0]));
return EXIT_SUCCESS;
} else if(opts.has_error) {
return EXIT_FAILURE;
}
// -- Setup input/output files
std::istream* in = nullptr;
std::ostream* out = nullptr;
std::ifstream fin;
std::ofstream fout;
if(opts.in_file == "-") {
in = &std::cin;
} else {
try {
fin.open(opts.in_file);
in = &fin;
} catch(...) {}
}
if(opts.out_file == "-" || opts.out_file == "")
out = &std::cout;
else {
try {
fout.open(opts.out_file);
out = &fout;
} catch(...) {}
}
if(!in->good())
fprintf(stderr, "failed to open input file '%s'\n", opts.in_file.c_str());
if(!out->good())
fprintf(
stderr, "failed to open output file '%s'\n", opts.out_file.c_str());
// -- Transform input into output
try {
if(in->good() && out->good()) {
State state(opts, *in, *out);
if(transform_input(state)) return EXIT_SUCCESS;
}
} catch(std::exception& e) {
string err = e.what();
fprintf(stderr, "exception tranforming input: \"%s\"\n", err.c_str());
}
// -- Done
return EXIT_FAILURE;
}
// ------------------------------------------------ cached environment variables
static decltype(auto) getenv(State& state, const string& s)
{
auto ii = state.env.find(s);
if(ii == state.env.end()) {
auto var = getenv(s.c_str());
if(var == nullptr) return state.env.end();
state.env[s] = string(var);
ii = state.env.find(s);
}
return ii;
}
static void substitute_env_variables(State& state, string& line_s, bool strict)
{
auto pos = line_s.find('$');
if(pos == string::npos) return;
string_view line(line_s);
const auto len = int(line.size());
std::stringstream ss("");
ss << line.substr(0, pos);
auto process_variable = [&](int& i) {
assert(line[i] == '$');
if(++i == len) return;
if(line[i] != '{') {
ss << '$';
ss << line[i];
return;
}
auto pos = line.find('}', i);
if(pos == string::npos)
throw std::runtime_error("parse error reading variable name, "
"missing '}'");
string variable(line.substr(i + 1, pos - i - 1));
const auto value = getenv(state, variable);
if(value == state.env.end()) {
if(strict) {
throw std::runtime_error("environment variable '" + variable
+ "' not found");
}
ss << '$' << '{' << variable << '}';
} else {
ss << value->second;
}
i = pos;
};
for(auto i = int(pos); i < len; ++i) {
if(line[i] == '$')
process_variable(i);
else
ss << line[i];
}
line_s = ss.str();
}
// -----------------------------------------------------------------------------
// -- String Funcitons --
// -----------------------------------------------------------------------------
// ----------------------------------------------------------------- starts-with
static bool starts_with(const std::string& s, const std::string& prefix)
{
auto pos = 0u;
while(pos < s.size() && std::isspace(s[pos])) ++pos;
return std::mismatch(prefix.begin(), prefix.end(), s.begin() + pos, s.end())
.first
== prefix.end();
}
// ------------------------------------------------------------------------ trim
static void ltrim(std::string& s)
{
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch) {
return !std::isspace(ch);
}));
}
// trim from end (in place)
static void rtrim(std::string& s)
{
s.erase(std::find_if(
s.rbegin(), s.rend(), [](int ch) { return !std::isspace(ch); })
.base(),
s.end());
}
// trim from both ends (in place)
static void trim(std::string& s)
{
ltrim(s);
rtrim(s);
}
static bool is_empty_line(const std::string& s)
{
for(auto c : s)
if(!std::isspace(c)) return false;
return true;
}
template<class U, class V> static bool ends_with(const U& input, const V& match)
{
return input.size() >= match.size()
and std::equal(crbegin(match), crend(match), rbegin(input));
}
// ------------------------------------------------------------------- make-glob
static std::regex make_glob(const string& pattern)
{
int len = pattern.size();
int pos = 0;
char* buffer = static_cast<char*>(alloca(len * 2 + 1));
const char* src = pattern.c_str();
while(*src) {
switch(*src) {
case '\\':
buffer[pos++] = '\\';
buffer[pos++] = '\\';
break;
case '^':
buffer[pos++] = '\\';
buffer[pos++] = '^';
break;
case '.':
buffer[pos++] = '\\';
buffer[pos++] = '.';
break;
case '$':
buffer[pos++] = '\\';
buffer[pos++] = '$';
break;
case '|':
buffer[pos++] = '\\';
buffer[pos++] = '|';
break;
case '(':
buffer[pos++] = '\\';
buffer[pos++] = '(';
break;
case ')':
buffer[pos++] = '\\';
buffer[pos++] = ')';
break;
case '[':
buffer[pos++] = '\\';
buffer[pos++] = '[';
break;
case ']':
buffer[pos++] = '\\';
buffer[pos++] = ']';
break;
case '+':
buffer[pos++] = '\\';
buffer[pos++] = '+';
break;
case '/':
buffer[pos++] = '\\';
buffer[pos++] = '/';
break;
case '?': buffer[pos++] = '.'; break;
case '*':
buffer[pos++] = '.';
buffer[pos++] = '*';
break;
default: buffer[pos++] = *src;
}
src++;
}
buffer[pos++] = '\0';
return std::regex(buffer);
}
// ------------------------------------------------ calculate-module-dependences
static void calculate_module_dependences(string_view fname,
string_view modules_dir,
std::ostream& out)
{
static const std::regex import_regex(
"^\\s*import\\s+(export\\s+)?([a-zA-Z_][a-zA-Z0-9_\\.]*);");
static const std::regex module_regex(
"^\\s*module\\s+([a-zA-Z_][a-zA-Z0-9_\\.]*);");
int counter = 0;
auto process_dependency = [&](const string& module) {
if(counter++ > 0) out << " ";
out << modules_dir
<< (modules_dir.size() > 0 and modules_dir.back() != '/' ? "/" : "")
<< module << ".pcm";
};
std::fstream fin(fname.data());
for(string line; std::getline(fin, line);) {
std::smatch results;
if(std::regex_match(line, results, import_regex)) {
process_dependency(results[results.size() - 1].str());
} else if(std::regex_match(line, results, module_regex)) {
process_dependency(results[results.size() - 1].str());
}
}
}
// ---------------------------------------------------------- command-substitute
static void command_substitute(State& state,
const string& fname,
const string_view dname,
const FileCommand& cmd,
vector<FilterVariable>& filters)
{
// substitute:
// '^' for fname
// '%' for the filename without extension
// '#' same as '%', but without the search directory, and '/' => '.'
// '@' for dirname(fname)
// '&' for basename(fname)
// remove all '!', but...
// if a '!' exists, then add that product to the list of files in state
// Remove leading './' if it is there
string_view fnamev = fname;
if(fnamev.size() > 2 && fnamev[0] == '.' && fnamev[1] == '/')
fnamev.remove_prefix(2);
// Create the extensionless version
string_view extlessv = fnamev;
{
int i = extlessv.size() - 1;
for(; i >= 0; --i) {
if(extlessv[i] == '.') break;
if(extlessv[i] == '/') break;
}
if(extlessv[i] == '.') extlessv.remove_suffix(extlessv.size() - i);
}
auto strdup = [](string_view s) {
auto dup = static_cast<char*>(malloc((s.size() + 1) * sizeof(char)));
int pos = 0;
for(auto c : s) dup[pos++] = c;
dup[pos] = '\0';
return dup;
};
auto write_bname = [&](const char* s, std::ostream& out) {
auto dup = strdup(s);
out << basename(dup);
free(dup);
};
auto write_dir = [&](const char* s, string_view dname, std::ostream& out) {
auto dup = strdup(s);
out << dirname(dup);
free(dup);
};
// Output the command
auto process_text = [&](const string& s, std::ostream& out) {
unsigned pos = 0;
for(auto c : s) {
switch(c) {
case '^': out << fnamev; break;
case '%': out << extlessv; break;
case '@': write_dir(fnamev.data(), dname, out); break;
case '#':
pos = 0;
if(extlessv.size() > dname.size()) {
pos = dname.size();
if(extlessv[pos] == '/') ++pos;
}
for(auto i = pos; i < extlessv.size(); ++i) {
if(extlessv[i] == '/')
out << '.';
else
out << extlessv[i];
}
break;
case '&': write_bname(extlessv.data(), out); break;
case '?':
calculate_module_dependences(fname, state.opts.module_dir, out);
case '!': break;
default: out << c;
}
}
};
process_text(cmd.command, state.out);
auto handle_output = [&](const string& s) {
for(auto& filter : filters)
if(std::regex_match(s.begin(), s.end(), filter.glob))
filter.products.push_back(s);
};
// And add in the outputs with '!' on them
for(const auto& s : cmd.re_add_outputs) {
std::stringstream ss("");
process_text(s, ss);
auto output = ss.str();
handle_output(output);
state.additional_filenames.push_back(output);
state.additional_dirnames.push_back(empty_directory); //
}
for(const auto& s : cmd.other_outputs) {
std::stringstream ss("");
process_text(s, ss);
handle_output(ss.str());
}
state.out << endl;
}
// --------------------------------------------------------- process-src-command
static void process_src_command(State& state, const vector<string> command)
{
// ---- Parse the source line...
string cd_dir = "";
vector<FilterVariable> filters;
vector<string> directories;
{
vector<string> src_parts;
std::istringstream iss(command[0]);
std::copy(std::istream_iterator<string>(iss),
std::istream_iterator<string>(),
std::back_inserter(src_parts));
if(src_parts[0] != "+src")
throw std::runtime_error("failed to match '+src' in command");
for(auto i = 1u; i < src_parts.size(); ++i) {
auto& part = src_parts[i];
auto pos = part.find('=');
if(pos == string::npos) {
directories.emplace_back(std::move(part));
} else {
string var = part.substr(0, pos);
string value = part.substr(pos + 1);
if(var == "cd") {
if(cd_dir != "")
throw std::runtime_error("multiple 'cd' specifiers");
cd_dir = std::move(value);
} else {
filters.emplace_back();
filters.back().variable = std::move(var);
filters.back().glob = make_glob(value);
filters.back().filter = std::move(value);
}
}
}
}
// ---- Parse each of the command lines
vector<FileCommand> commands;
for(auto i = 1u; i < command.size(); ++i) {
if(starts_with(command[i], "~")) {
if(commands.size() == 0)
throw std::runtime_error("misplaced command extension '~'");
auto pos = command[i].find('~');
commands.back().command += "\n" + command[i].substr(pos + 1);
continue;
}
vector<string> parts;
std::istringstream iss(command[i]);
std::copy(std::istream_iterator<string>(iss),
std::istream_iterator<string>(),
std::back_inserter(parts));
assert(parts.size() > 0 && parts[0] == "-");
if(parts.size() < 2)
throw std::runtime_error("failed to match file rule '" + command[i]
+ "'");
// implode tokens [2..]
std::stringstream ss("");
for(auto j = 2u; j < parts.size(); ++j) {
if(j > 2) ss << " ";
ss << parts[j];
}
commands.emplace_back();
auto& cmd = commands.back();
cmd.pattern = parts[1];
cmd.glob = make_glob(cmd.pattern);
cmd.command = ss.str();
// Find the outputs (those parts[2..] before the first ':')
auto last_output_ind = 0u;
for(auto j = 2u; j < parts.size(); ++j) {
if(parts[j].back() == ':') {
last_output_ind = j + 1; // 1 past the end
break;
}
}
// Now add those outputs with shebangs to the 're-add' list
for(auto j = 2u; j < last_output_ind; ++j) {
if(parts[j].back() == ':') parts[j].pop_back();
if(parts[j].back() == '!') {
parts[j].pop_back();
cmd.re_add_outputs.push_back(parts[j]);
} else {
cmd.other_outputs.push_back(parts[j]);
}
}
}
// ---- Attempt to change directory
if(cd_dir != "") {
if(chdir(cd_dir.c_str()) != 0)
throw std::runtime_error("failed to change directory to: '" + cd_dir
+ "'");
}
// ---- Search directories: this is not thread-safe, for what it's worth
vector<string> nftw_files;
vector<string_view> nftw_dirs;
for(const auto& directory : directories) {
const auto opts = fs::directory_options::follow_directory_symlink;
for(auto& f : fs::recursive_directory_iterator(directory, opts)) {
const auto s = f.path().string();
if(fs::is_regular_file(s)) {
nftw_files.push_back(s);
nftw_dirs.push_back(directory);
}
}
}
// ---- Move back to original CWD if we changed directory
if(cd_dir != "")
if(chdir(state.current_working_directory.c_str()) != 0)
throw std::runtime_error("failed to change directory to: '"
+ state.current_working_directory + "'");
// ---- Match and substitude files against the commands
auto match_and_substitute
= [&](const string& fname, string_view dname, const FileCommand& cmd) {
if(std::regex_match(fname.begin(), fname.end(), cmd.glob)) {
// Parse the command and add
command_substitute(state, fname, dname, cmd, filters);
// We may filter the input file as well...
for(auto& filter : filters)
if(std::regex_match(fname.begin(), fname.end(), filter.glob))
filter.products.push_back(fname);
return true;
}
return false;
};
while(nftw_files.size() > 0) {
for(auto i = 0u; i < nftw_files.size(); ++i) {
const auto& fname = nftw_files[i];
const auto& dname = nftw_dirs[i];
for(const auto& cmd : commands) {
if(match_and_substitute(fname, dname, cmd)) break;
}
}
nftw_files = state.additional_filenames;
nftw_dirs = state.additional_dirnames;
state.additional_filenames.clear();
state.additional_dirnames.clear();
}
// ---- Ensure that environment variables exist for all filters
for(auto& filter : filters) {
auto ii = state.env.find(filter.variable);
if(ii == cend(state.env)) state.env[filter.variable] = string{};
}
// ---- Now update any environment variables (via filter products)
const string main_cpp = "main.cpp"s;
const string main_cc = "main.cc"s;
auto is_main_source_file = [&](const auto& fname) -> bool {
return ends_with(fname, main_cpp) || ends_with(fname, main_cc)
|| ends_with(fname, "main.cxx"s) || ends_with(fname, "main.c"s);
};
for(auto& filter : filters) {
if(!filter.products.empty()) {
auto ii = state.env.find(filter.variable);
std::stringstream ss("");
bool first = true; // do we place a space...
if(ii != state.env.end()) {
ss << ii->second;
first = false;
}
// sort the filter, such that any `main.cpp` files come first
std::sort(filter.products.begin(),
filter.products.end(),
[&](const auto& A, const auto& B) {
const bool a_ends = is_main_source_file(A);
const bool b_ends = is_main_source_file(B);
if(a_ends && !b_ends) return true;
if(!a_ends && b_ends) return false;
return A < B;
});
for(const auto& s : filter.products) {
if(first)
first = false;
else
ss << ' ';
ss << s;
}
state.env[filter.variable] = ss.str();
filter.products.clear();
}
}
}
// ------------------------------------------------------------------ preprocess
static bool preprocess_input(State& state)
{
for(string line; std::getline(state.in, line);) {
if(!starts_with(line, "#")) substitute_env_variables(state, line, false);
state.lines.push_back(line);
}
return true;
}
// ----------------------------------------------------------------- process+src
static bool process_source_commands(State& state)
{
vector<string> src_command;
for(auto& line : state.lines) {
// Skip comments
if(starts_with(line, "#")) {
state.out << line << std::endl;
continue;
}
if(src_command.size() > 0 // we're in a command
&& !starts_with(line, "~") // not adding to previous command
&& !starts_with(line, "-")) // not adding to a command
{
process_src_command(state, src_command);
src_command.clear();
}
// Final "strict" substitution of environment variables
substitute_env_variables(state, line, true);
// Process the line
if(starts_with(line, "+src")) {
assert(src_command.size() == 0);
src_command.push_back(line);
} else if(src_command.size() > 0
&& (starts_with(line, "-") || starts_with(line, "~"))) {
src_command.push_back(line);
} else {
state.out << line << std::endl;
}
}
if(src_command.size() > 0) {
process_src_command(state, src_command);
src_command.clear();
}
return true;
}
// ------------------------------------------------------------- transform input
static bool transform_input(State& state)
{
preprocess_input(state);
process_source_commands(state);
return true;
}