-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.cc
830 lines (671 loc) · 16.4 KB
/
main.cc
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
#include <cassert>
#include <cerrno>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <memory>
#include <string>
#include <list>
#include <filesystem>
#include <terra/terra.h>
#include "xopt.h"
using namespace std;
// https://github.com/stevedonovan/Penlight/blob/d90956418cdf9e315719a7e4ce314db6b512ae00/lua/pl/compat.lua#L145-L155
// MIT License
// TODO convert to C
const char * const SEARCHPATH_LUA =
"if not package.searchpath then\n"
" local sep = package.config:sub(1,1)\n"
" function package.searchpath (mod,path)\n"
" mod = mod:gsub('%.',sep)\n"
" for m in path:gmatch('[^;]+') do\n"
" local nm = m:gsub('?',mod)\n"
" local f = io.open(nm,'r')\n"
" if f then f:close(); return nm end\n"
" end\n"
" end\n"
"end\n";
struct config {
int verbosity;
bool help;
bool debug;
const char *filename;
const char *output;
const char *depfile;
const char *depfile_target;
unique_ptr<list<string>> depfiles;
unique_ptr<list<filesystem::path>> include_dirs;
unique_ptr<list<filesystem::path>> lib_dirs;
unique_ptr<list<string>> libs;
};
static void on_verbose(const char *v, void *data, const struct xoptOption *option, bool longArg, const char **err) {
(void) v;
(void) option;
(void) err;
(void) longArg;
config *conf = (config *) data;
assert(!longArg);
if (conf->verbosity < 3) {
++conf->verbosity;
}
}
static void on_multi_path(const char *v, void *data, const struct xoptOption *option, bool longArg, const char **err) {
(void) option;
(void) longArg;
(void) err;
(*((unique_ptr<list<filesystem::path>> *)((char *) data + option->offset)))->push_back(filesystem::path(v));
}
static void on_multi_string(const char *v, void *data, const struct xoptOption *option, bool longArg, const char **err) {
(void) option;
(void) longArg;
(void) err;
(*((unique_ptr<list<string>> *)((char *) data + option->offset)))->push_back(v);
}
static xoptOption options[] = {
{
"output",
'o',
offsetof(config, output),
0,
XOPT_TYPE_STRING,
0,
"If specified, outputs the terra code to the given filename"
},
{
"include-dir",
'I',
offsetof(config, include_dirs),
&on_multi_path,
XOPT_TYPE_STRING,
"dir",
"Adds a search path for C header files (can be passed multiple times)"
},
{
"lib-dir",
'L',
offsetof(config, lib_dirs),
&on_multi_path,
XOPT_TYPE_STRING,
"dir",
"Adds a search path for libraries (can be passed multiple times)"
},
{
"lib",
'l',
offsetof(config, libs),
&on_multi_string,
XOPT_TYPE_STRING,
"name",
"Specifies a library to be linked against the resulting binary"
},
{
"depfile",
'D',
offsetof(config, depfile),
0,
XOPT_TYPE_STRING,
0,
"If specified, emits a Ninja-compatible depfile with all included files during the build"
},
{
"depfile-path",
'P',
offsetof(config, depfile_target),
0,
XOPT_TYPE_STRING,
0,
"If specified, all depfile paths are relativized to this path"
},
{
0, // only allow -v[v[v]]
'v',
offsetof(config, verbosity),
&on_verbose,
XOPT_TYPE_BOOL,
0,
"Increase verbosity (default level 0, max level 3)"
},
{
"debug",
'g',
offsetof(config, debug),
0,
XOPT_TYPE_BOOL,
0,
"Enable debugging information"
},
{
"help",
'h',
offsetof(config, help),
0,
XOPT_TYPE_BOOL,
0,
"Shows this help message"
},
XOPT_NULLOPTION
};
#ifdef NDEBUG
# define topcheck(...) ((void)0)
#else
struct _topcheck {
_topcheck(lua_State *_L, int ret = 0) : top(lua_gettop(_L)) {
this->L = _L;
this->ret = ret;
}
~_topcheck() {
assert(lua_gettop(this->L) == (this->top + this->ret));
}
const int top;
int ret;
lua_State *L;
};
# define topcheck(...) _topcheck __tc(__VA_ARGS__)
#endif
static int errfn_ref = -1;
static int errfn(lua_State *L) {
topcheck(L, 1);
lua_getfield(L, LUA_GLOBALSINDEX, "debug");
lua_getfield(L, -1, "traceback");
lua_remove(L, -2);
lua_pushvalue(L, 1);
lua_pushinteger(L, 2);
lua_call(L, 2, 1);
return 1;
}
static bool is_terrafn(lua_State *L) {
topcheck(L);
assert(!lua_isnil(L, -2));
assert(!lua_isnil(L, -1));
if (!lua_isstring(L, -2)) {
return false;
}
lua_getglobal(L, "terralib");
assert(!lua_isnil(L, -1));
lua_getfield(L, -1, "type");
assert(!lua_isnil(L, -1));
lua_pushvalue(L, -3);
string where = " (at global '" + string(lua_tostring(L, -5)) + "')";
if (lua_pcall(L, 1, 1, 0) != 0) {
cerr << "terrac: call to terralib.type() failed: " << lua_tostring(L, -1) << where << endl;
lua_pop(L, 2);
return false;
}
string type = lua_tostring(L, -1);
lua_pop(L, 2);
return type == "terrafunction" || type == "terraglobalvariable";
}
static void resolve_path(lua_State *L, const char *pathname) {
topcheck(L, 1);
lua_getglobal(L, "package");
assert(!lua_isnil(L, -1));
lua_getfield(L, -1, "searchpath");
assert(lua_isfunction(L, -1));
lua_pushvalue(L, -3);
lua_getfield(L, -3, pathname);
assert(!lua_isnil(L, -1));
lua_remove(L, -4);
if (lua_pcall(L, 2, 1, 0) != 0) {
cerr << "terrac: call to package.searchpath() failed: " << lua_tostring(L, -1) << endl;
lua_pop(L, 1);
lua_pushnil(L);
return;
}
if (lua_isnil(L, -1)) {
// not found
return;
}
// get realpath
char *rpth = realpath(lua_tostring(L, -1), NULL);
if (rpth == NULL) {
string where = "call to realpath() failed (module path: " + string(lua_tostring(L, -1)) + ")";
perror(where.c_str());
}
lua_pop(L, 1);
if (rpth == NULL) {
lua_pushnil(L);
} else {
lua_pushstring(L, rpth);
free(rpth);
}
}
static int on_loaded_newindex(lua_State *L) {
assert(lua_gettop(L) == 3); // we expect 3 args
lua_pushvalue(L, 2);
lua_pushvalue(L, 3);
lua_rawset(L, 1);
config *conf = (config *) lua_touserdata(L, lua_upvalueindex(1));
if (conf->verbosity > 0) {
cerr << "terrac: detected module: " << lua_tostring(L, 2) << endl;
}
lua_pushvalue(L, 2);
resolve_path(L, "path");
if (lua_isnil(L, -1)) {
lua_pop(L, 1);
resolve_path(L, "cpath");
}
if (lua_isnil(L, -1)) {
lua_pop(L, 1);
resolve_path(L, "terrapath");
}
lua_remove(L, -2);
if (lua_isnil(L, -1)) {
lua_pop(L, 1);
cerr << "terrac: could not resolve module (skipping): " << lua_tostring(L, 2) << endl;
return 0;
}
if (conf->verbosity > 0) {
cerr << "terrac: resolved " << lua_tostring(L, 2) << " -> " << lua_tostring(L, -1) << endl;
}
if (conf->depfile) {
conf->depfiles->push_back(lua_tostring(L, -1));
}
lua_pop(L, 1);
return 0;
}
static bool get_terrac(lua_State *L) {
topcheck(L, 1);
lua_getglobal(L, "terrac");
if (lua_isnil(L, -1)) {
cerr << "terrac: ERROR: global `terrac` not found" << endl;
return false;
}
return true;
}
static bool get_link_flags(lua_State *L) {
topcheck(L, 1);
if (!get_terrac(L)) {
return false;
}
lua_getfield(L, -1, "link_flags");
lua_remove(L, -2);
if (lua_isnil(L, -1)) {
lua_pop(L, 1);
lua_newtable(L);
} else if (!lua_istable(L, -1)) {
cerr << "terrac: WARNING: terrac.link_flags is not nil and is not a table - replacing with empty table (must be table or nil)" << endl;
lua_pop(L, 1);
lua_newtable(L);
}
return true;
}
static bool get_cflags(lua_State *L) {
topcheck(L, 1);
if (!get_terrac(L)) {
return false;
}
lua_getfield(L, -1, "c_flags");
lua_remove(L, -2);
if (lua_isnil(L, -1)) {
lua_pop(L, 1);
lua_newtable(L);
} else if (!lua_istable(L, -1)) {
cerr << "terrac: WARNING: terrac.c_flags is not nil and is not a table - replacing with empty table (must be table or nil)" << endl;
lua_pop(L, 1);
lua_newtable(L);
}
return true;
}
static bool inject_link_flags(lua_State *L, config &conf) {
topcheck(L);
if (!get_link_flags(L)) {
lua_pop(L, 1);
return false;
}
size_t n = lua_objlen(L, -1);
for (const filesystem::path &ip : *conf.lib_dirs) {
lua_pushinteger(L, ++n);
lua_pushstring(L, "-L");
lua_settable(L, -3);
lua_pushinteger(L, ++n);
lua_pushstring(L, ip.c_str());
lua_settable(L, -3);
}
for (const string &l : *conf.libs) {
string fullarg = "-l" + l;
lua_pushinteger(L, ++n);
lua_pushstring(L, fullarg.c_str());
lua_settable(L, -3);
}
lua_pop(L, 1);
return true;
}
/*
static void print_table(lua_State *L, int t) {
topcheck(L);
size_t len = lua_objlen(L, t);
cerr << "\x1b[35mBEGIN TABLE: " << len << " elements" << endl;
for (size_t i = 1; i <= len; i++) {
lua_pushinteger(L, i);
lua_gettable(L, t - 1);
cerr << "\t" << i << ": " << lua_tostring(L, -1) << endl;
lua_pop(L, 1);
}
cerr << "END TABLE.\x1b[m" << endl;
}
*/
static bool inject_cflags(lua_State *L, config &conf) {
topcheck(L);
if (!get_cflags(L)) {
lua_pop(L, 1);
return false;
}
size_t n = lua_objlen(L, -1);
for (const filesystem::path &ip : *conf.include_dirs) {
lua_pushinteger(L, ++n);
lua_pushstring(L, "-I");
lua_settable(L, -3);
lua_pushinteger(L, ++n);
lua_pushstring(L, ip.c_str());
lua_settable(L, -3);
}
lua_pop(L, 1);
return true;
}
static int table_assign(lua_State *L) {
int nargs = lua_gettop(L);
luaL_checktype(L, 1, LUA_TTABLE);
for (int i = 2; i <= nargs; i++) {
luaL_checktype(L, i, LUA_TTABLE);
}
topcheck(L, 1);
for (int i = 2; i <= nargs; i++) {
lua_pushnil(L);
while (lua_next(L, i)) {
lua_pushvalue(L, -2);
lua_pushvalue(L, -2);
lua_settable(L, 1);
lua_pop(L, 1);
}
}
lua_pushvalue(L, 1);
return 1;
}
/*
can be used for both includec and includecstring as it's
just a simple proxy.
if terra changes the prototype between the two, this will
have to be updated.
*/
static int terrac_includec(lua_State *L) {
luaL_checkstring(L, 1);
size_t nargs = lua_gettop(L);
/*
"artificial" means that we're artifically
injecting the second parameter where the Lua
function was caled without one.
*/
int artificial = 0;
if (nargs < 2) {
artificial = 1;
nargs = 2;
}
// upvalue 1 has the original includec[string] function
lua_pushvalue(L, lua_upvalueindex(1));
assert(!lua_isnil(L, -1));
for (size_t i = 1; i <= nargs; i++) {
if (i == 2) {
topcheck(L, 1);
size_t n = 0;
lua_newtable(L);
// cflags object
get_cflags(L);
assert(!lua_isnil(L, -1));
size_t cn = lua_objlen(L, -1);
for (size_t j = 1; j <= cn; j++) {
topcheck(L);
lua_pushinteger(L, ++n); // args_k = ++n
lua_pushinteger(L, j); // cflag_k = j
lua_gettable(L, -3); // push(cflags[cflag_k])
lua_settable(L, -4); // args[args_k] = pop()
}
lua_pop(L, 1);
// args object
if (!artificial && !lua_isnil(L, i)) {
luaL_checktype(L, i, LUA_TTABLE);
cn = lua_objlen(L, i);
for (size_t j = 1; j <= cn; j++) {
topcheck(L);
lua_pushinteger(L, ++n); // args_k = ++n
lua_pushinteger(L, j); // cflag_k = j
lua_gettable(L, i); // push(cflags[cflag_k])
lua_settable(L, -3); // args[args_k] = pop()
}
}
continue;
}
lua_pushvalue(L, i);
}
if (lua_pcall(L, nargs, LUA_MULTRET, 0) != 0) {
lua_error(L);
return 0;
}
return lua_gettop(L) - (nargs - artificial);
}
static bool inject_includec(lua_State *L, config& conf) {
topcheck(L);
lua_getglobal(L, "terralib");
assert(!lua_isnil(L, -1));
lua_getfield(L, -1, "includec");
assert(!lua_isnil(L, -1));
lua_pushlightuserdata(L, (void *) &conf);
lua_pushcclosure(L, &terrac_includec, 2);
lua_setfield(L, -2, "includec");
lua_getfield(L, -1, "includecstring");
assert(!lua_isnil(L, -1));
lua_pushlightuserdata(L, (void *) &conf);
lua_pushcclosure(L, &terrac_includec, 2);
lua_setfield(L, -2, "includecstring");
lua_pop(L, 1);
return true;
}
int pmain(config &conf) {
int status = 0;
assert(conf.filename != nullptr);
// init environment
lua_State *L = luaL_newstate();
if (L == NULL) {
cerr << "terrac: memory allocation for LuaJIT state failed" << endl;
return 42;
}
// load libraries
luaL_openlibs(L);
// patch package.searchpath
if (luaL_dostring(L, SEARCHPATH_LUA) != 0) {
cerr << "terrac: could not patch package.searchpath(): " << lua_tostring(L, -1) << endl;
return 1;
}
// configure Terra
terra_Options topts;
topts.verbose = conf.verbosity == 0 ? 0 : conf.verbosity - 1;
topts.debug = (int) conf.debug;
terra_initwithoptions(L, &topts);
// set error handler ref
lua_pushcfunction(L, &errfn);
errfn_ref = lua_gettop(L);
// override loaded table to detect dependencies
{
topcheck(L);
lua_getglobal(L, "package");
assert(!lua_isnil(L, -1));
lua_getfield(L, -1, "loaded");
lua_newtable(L);
lua_pushlightuserdata(L, (void *) &conf);
lua_pushcclosure(L, &on_loaded_newindex, 1);
lua_setfield(L, -2, "__newindex");
lua_setmetatable(L, -2);
lua_pop(L, 2);
}
// create terrac object
{
topcheck(L);
lua_newtable(L);
lua_newtable(L);
lua_setfield(L, -2, "c_flags");
lua_newtable(L);
lua_setfield(L, -2, "link_flags");
lua_setglobal(L, "terrac");
}
// inject configuration
{
topcheck(L);
if (!inject_cflags(L, conf)) return 1;
if (!inject_link_flags(L, conf)) return 1;
}
// inject includec replacements
{
topcheck(L);
if (!inject_includec(L, conf)) return 1;
}
// inject table.assign
{
topcheck(L);
lua_getglobal(L, "table");
assert(!lua_isnil(L, -1));
lua_pushcfunction(L, &table_assign);
lua_setfield(L, -2, "assign");
lua_pop(L, 1);
}
// run the file
if ((terra_loadfile(L, conf.filename) || lua_pcall(L, 0, LUA_MULTRET, errfn_ref))) {
cerr << "terrac: terra error: " << lua_tostring(L, -1) << endl;
return 1;
}
if (conf.output) {
topcheck(L);
// enumerate public globals
lua_newtable(L);
lua_pushnil(L);
while (lua_next(L, LUA_GLOBALSINDEX) != 0) {
if (is_terrafn(L)) {
if (conf.verbosity > 0) {
cerr << "terrac: export: " << lua_tostring(L, -2) << endl;
}
lua_pushvalue(L, -2);
lua_pushvalue(L, -2);
lua_settable(L, -5);
}
lua_pop(L, 1);
}
// compile it
lua_getglobal(L, "terralib");
assert(!lua_isnil(L, -1));
lua_getfield(L, -1, "saveobj");
assert(!lua_isnil(L, -1));
lua_pushstring(L, conf.output); // 1
lua_pushvalue(L, -4); // 2
if (!get_link_flags(L)) return 1; // 3
lua_pushnil(L); // 4
lua_pushboolean(L, (int) !conf.debug); // 5
if (conf.verbosity > 0) {
cerr << "terrac: exporting public symbols to " << conf.output << endl;
}
if (lua_pcall(L, 5, 0, errfn_ref) != 0) {
cerr << "terrac: call to terralib.saveobj() failed: " << lua_tostring(L, -1) << endl;
lua_pop(L, 1);
status = 1;
}
lua_pop(L, 2);
}
if (conf.depfile) {
ofstream df;
df.open(conf.depfile, ios::trunc | ios::out);
if (df.bad()) {
cerr << "terrac: failed to open depfile '" << conf.depfile << "' for writing: " << strerror(errno) << endl;
status = 1;
goto cleanup;
}
filesystem::path target;
if (conf.output) {
target = conf.output;
} else {
target = conf.filename;
}
if (conf.depfile_target) {
target = filesystem::relative(target, conf.depfile_target);
}
df << target.string() << ":";
for (const string &depstr : *conf.depfiles) {
filesystem::path dep(depstr);
if (conf.depfile_target) {
dep = filesystem::relative(dep, conf.depfile_target);
}
df << " " << dep.string();
}
df << endl;
df.close();
}
// cleanup
cleanup:
terra_llvmshutdown();
lua_close(L);
return status;
}
int main(int argc, const char **argv) {
int status = 1;
const char *err = nullptr;
int extrac = 0;
const char **extrav = nullptr;
config conf;
conf.help = false;
conf.filename = nullptr;
conf.debug = false;
conf.verbosity = 0;
conf.output = nullptr;
conf.depfile = nullptr;
conf.depfile_target = nullptr;
conf.include_dirs.reset(new list<filesystem::path>());
conf.lib_dirs.reset(new list<filesystem::path>());
conf.libs.reset(new list<string>());
conf.depfiles.reset(new list<string>());
XOPT_SIMPLE_PARSE(
argv[0],
XOPT_CTX_SLOPPYSHORTS,
&options[0], &conf,
argc, argv,
&extrac, &extrav,
&err,
stderr,
"[-h] [--] file.t",
"Unofficial Terra compiler",
nullptr,
14);
if (err) {
goto error;
}
if (extrac != 1) {
err = "expected exactly one filename";
goto error;
}
if (extrav[0][0] == 0) {
err = "specified filename is an empty string";
goto error;
}
conf.filename = extrav[0];
error:
if (err) {
cerr << "terrac: error: " << err << endl;
status = 2;
goto exit;
}
if (conf.verbosity > 0) {
for (const filesystem::path &p : *conf.include_dirs) {
cerr << "terrac: include dir: " << p << endl;
}
for (const filesystem::path &p : *conf.lib_dirs) {
cerr << "terrac: library search path: " << p << endl;
}
}
status = pmain(conf);
exit:
if (extrav != nullptr) {
free(extrav);
}
return status;
xopt_help:
status = 2;
goto exit;
}