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
|
///
///
///
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include "../minicargo/debug.h"
#include "../minicargo/path.h"
#ifdef _WIN32
# include <Windows.h>
# define MRUSTC_PATH "x64\\Release\\mrustc.exe"
#else
# include <sys/types.h>
# include <dirent.h>
# include <sys/stat.h>
# include <unistd.h>
# include <spawn.h>
# include <fcntl.h> // O_*
# include <sys/wait.h> // waitpid
# define MRUSTC_PATH "./bin/mrustc"
#endif
#include <algorithm>
struct Options
{
const char* output_dir = nullptr;
const char* input_glob = nullptr;
const char* exceptions_file = nullptr;
int parse(int argc, const char* argv[]);
void usage_short() const;
void usage_full() const;
};
struct TestDesc
{
::std::string m_name;
::std::string m_path;
::std::vector<::std::string> m_pre_build;
::std::vector<::std::string> m_extra_flags;
};
struct Timestamp
{
static Timestamp for_file(const ::helpers::path& p);
#if _WIN32
uint64_t m_val;
Timestamp(FILETIME ft):
m_val( (static_cast<uint64_t>(ft.dwHighDateTime) << 32) | static_cast<uint64_t>(ft.dwLowDateTime) )
{
}
#else
time_t m_val;
#endif
static Timestamp infinite_past() {
#if _WIN32
return Timestamp { FILETIME { 0, 0 } };
#else
return Timestamp { 0 };
#endif
}
bool operator==(const Timestamp& x) const {
return m_val == x.m_val;
}
bool operator<(const Timestamp& x) const {
return m_val < x.m_val;
}
friend ::std::ostream& operator<<(::std::ostream& os, const Timestamp& x) {
#if _WIN32
os << ::std::hex << x.m_val << ::std::dec;
#else
os << x.m_val;
#endif
return os;
}
};
bool run_executable(const ::helpers::path& file, const ::std::vector<const char*>& args, const ::helpers::path& outfile);
bool run_compiler(const ::helpers::path& source_file, const ::helpers::path& output, const ::std::vector<::std::string>& extra_flags, ::helpers::path libdir={}, bool is_dep=false)
{
::std::vector<const char*> args;
args.push_back("mrustc");
args.push_back("-L");
args.push_back("output");
if(libdir.is_valid())
{
args.push_back("-L");
args.push_back(libdir.str().c_str());
}
::helpers::path logfile;
if( !is_dep )
{
args.push_back("-o");
args.push_back(output.str().c_str());
logfile = output + "-build.log";
}
else
{
args.push_back("--crate-type");
args.push_back("rlib");
args.push_back("--out-dir");
args.push_back(output.str().c_str());
logfile = output / source_file.basename() + "-build.log";
}
args.push_back(source_file.str().c_str());
for(const auto& s : extra_flags)
args.push_back(s.c_str());
return run_executable(MRUSTC_PATH, args, logfile);
}
int main(int argc, const char* argv[])
{
Options opts;
if( int v = opts.parse(argc, argv) )
{
return v;
}
::std::vector<::std::string> skip_list;
// > Filter out tests listed in an exceptions file (newline separated, supports comments)
if( opts.exceptions_file )
{
auto exceptions_list = ::helpers::path(opts.exceptions_file);
::std::ifstream in(exceptions_list.str());
if( !in.good() )
{
// TODO: Error?
::std::cerr << "Unable to open exceptions list " << exceptions_list << ::std::endl;
return 0;
}
else
{
while( !in.eof() )
{
::std::string line;
::std::getline(in, line);
if( line == "" )
continue ;
if( line[0] == '#' )
continue ;
auto p = line.find('#');
if( p != ::std::string::npos )
{
line.resize(p);
}
while(!line.empty() && ::std::isblank(line.back()))
line.pop_back();
if( line == "" )
continue ;
skip_list.push_back(line);
}
}
}
auto outdir = opts.output_dir ? ::helpers::path(opts.output_dir) : throw "";
::std::vector<TestDesc> tests;
// 1. Take input glob/folder and enumerate .rs files/matches
// - If input path is a folder, find *.rs
// - Otherwise, accept glob.
// 2. Open each file and extract the various flags required.
// 3. Build each test to its own output subdirectory
// 4. Run tests
{
auto input_path = ::helpers::path(opts.input_glob);
#ifdef _WIN32
WIN32_FIND_DATA find_data;
auto mask = input_path / "*.rs";
HANDLE find_handle = FindFirstFile( mask.str().c_str(), &find_data );
if( find_handle == INVALID_HANDLE_VALUE ) {
::std::cerr << "Unable to find files matching " << mask << ::std::endl;
return 1;
}
do
{
auto test_file_path = input_path / find_data.cFileName;
#else
auto* dp = opendir(input_path.str().c_str());
if( dp == nullptr )
throw ::std::runtime_error(::format( "Unable to open vendor directory '", input_path, "'" ));
while( const auto* dent = readdir(dp) )
{
if( dent->d_name[0] == '.' )
continue ;
auto test_file_path = input_path / dent->d_name;
struct stat sb;
stat(test_file_path.str().c_str(), &sb);
if( (sb.st_mode & S_IFMT) != S_IFREG) {
continue ;
}
#endif
::std::ifstream in(test_file_path.str());
if(!in.good())
continue ;
DEBUG("> " << test_file_path);
TestDesc td;
do
{
::std::string line;
::std::getline(in, line);
if( !(line[0] == '/' && line[1] == '/' && line[2] == ' ') )
continue ;
// TODO Parse a skewer-case ident and check against known set?
if( line.substr(3, 10) == "aux-build:" )
{
td.m_pre_build.push_back( line.substr(13) );
}
else if( line.substr(3, 14) == "compile-flags:" )
{
auto end = line.find(' ', 3+14);
decltype(end) start = 3+14;
do
{
if( start != end )
{
auto a = line.substr(start, end-start);
DEBUG("+" << a);
td.m_extra_flags.push_back(::std::move(a));
}
if( end == ::std::string::npos )
break;
start = end + 1;
end = line.find(' ', start);
} while(1);
}
} while( !in.eof() );
td.m_name = test_file_path.basename();
td.m_name.pop_back();
td.m_name.pop_back();
td.m_name.pop_back();
td.m_path = test_file_path;
tests.push_back(td);
// ---
auto test = td;
if( ::std::find(skip_list.begin(), skip_list.end(), td.m_name) != skip_list.end() )
{
DEBUG(">> SKIP " << test.m_name);
continue ;
}
DEBUG(">> " << test.m_name);
auto depdir = outdir / "deps-" + test.m_name.c_str();
auto outfile = outdir / test.m_name + ".exe";
auto test_output_ts = Timestamp::for_file(outfile);
if( test_output_ts < Timestamp::for_file(MRUSTC_PATH) )
{
for(const auto& file : test.m_pre_build)
{
mkdir(depdir.str().c_str(), 0755);
auto infile = input_path / "auxiliary" / file;
if( !run_compiler(infile, depdir, {}, depdir, true) )
{
DEBUG("COMPILE FAIL " << infile << " (dep of " << test.m_name << ")");
return 1;
}
}
auto compile_logfile = outdir / test.m_name + "-build.log";
if( !run_compiler(test.m_path, outfile, test.m_extra_flags, depdir) )
{
DEBUG("COMPILE FAIL " << test.m_name);
return 1;
}
}
// - Run the test
if( !run_executable(outfile, { outfile.str().c_str() }, outdir / test.m_name + ".out") )
{
DEBUG("RUN FAIL " << test.m_name);
return 1;
}
#ifndef _WIN32
}
closedir(dp);
#else
} while( FindNextFile(find_handle, &find_data) );
FindClose(find_handle);
#endif
}
return 0;
}
int Options::parse(int argc, const char* argv[])
{
for(int i = 1; i < argc; i ++)
{
const char* arg = argv[i];
if( arg[0] != '-' )
{
if( !this->input_glob ) {
this->input_glob = arg;
}
// TODO: Multiple input globs?
else {
this->usage_short();
return 1;
}
}
else if( arg[1] != '-' )
{
switch(arg[1])
{
case 'o':
if( this->output_dir ) {
this->usage_short();
return 1;
}
if( i+1 == argc ) {
this->usage_short();
return 1;
}
this->output_dir = argv[++i];
break;
default:
this->usage_short();
return 1;
}
}
else
{
if( 0 == ::std::strcmp(arg, "--help") )
{
this->usage_full();
return 0;
}
else if( 0 == ::std::strcmp(arg, "--exceptions") )
{
if( this->exceptions_file ) {
this->usage_short();
return 1;
}
if( i+1 == argc ) {
this->usage_short();
return 1;
}
this->exceptions_file = argv[++i];
}
else if( 0 == ::std::strcmp(arg, "--output-dir") )
{
if( this->output_dir ) {
this->usage_short();
return 1;
}
if( i+1 == argc ) {
this->usage_short();
return 1;
}
this->output_dir = argv[++i];
}
else
{
this->usage_short();
return 1;
}
}
}
return 0;
}
void Options::usage_short() const
{
}
void Options::usage_full() const
{
}
///
bool run_executable(const ::helpers::path& exe_name, const ::std::vector<const char*>& args, const ::helpers::path& outfile)
{
#ifdef _WIN32
::std::stringstream cmdline;
for (const auto& arg : args)
cmdline << arg << " ";
auto cmdline_str = cmdline.str();
cmdline_str.pop_back();
DEBUG("Calling " << cmdline_str);
STARTUPINFO si = { 0 };
si.cb = sizeof(si);
si.dwFlags = STARTF_USESTDHANDLES;
si.hStdInput = NULL;
si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
PROCESS_INFORMATION pi = { 0 };
CreateProcessA(exe_name.str().c_str(), (LPSTR)cmdline_str.c_str(), NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi);
CloseHandle(si.hStdOutput);
WaitForSingleObject(pi.hProcess, INFINITE);
DWORD status = 1;
GetExitCodeProcess(pi.hProcess, &status);
if (status != 0)
{
DEBUG("Executable exited with non-zero exit status " << status);
return false;
}
#else
Debug_Print([&](auto& os){
os << "Calling";
for(const auto& p : args)
os << " " << p;
});
posix_spawn_file_actions_t file_actions;
posix_spawn_file_actions_init(&file_actions);
auto outfile_str = outfile.str();
if( outfile_str != "" )
{
posix_spawn_file_actions_addopen(&file_actions, 1, outfile_str.c_str(), O_CREAT|O_WRONLY|O_TRUNC, 0644);
posix_spawn_file_actions_adddup2(&file_actions, 1, 2);
}
auto argv = args;
argv.push_back(nullptr);
pid_t pid;
int rv = posix_spawn(&pid, exe_name.str().c_str(), &file_actions, nullptr, const_cast<char**>(argv.data()), environ);
if( rv != 0 )
{
DEBUG("Error in posix_spawn - " << rv);
return false;
}
posix_spawn_file_actions_destroy(&file_actions);
int status = -1;
waitpid(pid, &status, 0);
if( status != 0 )
{
if( WIFEXITED(status) )
DEBUG(exe_name << " exited with non-zero exit status " << WEXITSTATUS(status) << ", see log " << outfile_str);
else if( WIFSIGNALED(status) )
DEBUG(exe_name << " was terminated with signal " << WTERMSIG(status) << ", see log " << outfile_str);
else
DEBUG(exe_name << " terminated for unknown reason, status=" << status << ", see log " << outfile_str);
return false;
}
#endif
return true;
}
Timestamp Timestamp::for_file(const ::helpers::path& path)
{
#if _WIN32
FILETIME out;
auto handle = CreateFile(path.str().c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
if(handle == INVALID_HANDLE_VALUE) {
//DEBUG("Can't find " << path);
return Timestamp::infinite_past();
}
if( GetFileTime(handle, NULL, NULL, &out) == FALSE ) {
//DEBUG("Can't GetFileTime on " << path);
CloseHandle(handle);
return Timestamp::infinite_past();
}
CloseHandle(handle);
//DEBUG(Timestamp{out} << " " << path);
return Timestamp { out };
#else
struct stat s;
if( stat(path.str().c_str(), &s) == 0 )
{
return Timestamp { s.st_mtime };
}
else
{
return Timestamp::infinite_past();
}
#endif
}
static int giIndentLevel = 0;
void Debug_Print(::std::function<void(::std::ostream& os)> cb)
{
for(auto i = giIndentLevel; i --; )
::std::cout << " ";
cb(::std::cout);
::std::cout << ::std::endl;
}
void Debug_EnterScope(const char* name, dbg_cb_t cb)
{
for(auto i = giIndentLevel; i --; )
::std::cout << " ";
::std::cout << ">>> " << name << "(";
cb(::std::cout);
::std::cout << ")" << ::std::endl;
giIndentLevel ++;
}
void Debug_LeaveScope(const char* name, dbg_cb_t cb)
{
giIndentLevel --;
for(auto i = giIndentLevel; i --; )
::std::cout << " ";
::std::cout << "<<< " << name << ::std::endl;
}
|