-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbslib.c
679 lines (615 loc) · 19.4 KB
/
bslib.c
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
/*
* Copyright 2022 Rochus Keller <mailto:me@rochus-keller.ch>
*
* This file is part of the BUSY build system.
*
* The following is the license that applies to this copy of the
* application. For a license to use the application under conditions
* other than those described here, please email to me@rochus-keller.ch.
*
* GNU General Public License Usage
* This file may be used under the terms of the GNU General Public
* License (GPL) versions 2.0 or 3.0 as published by the Free Software
* Foundation and appearing in the file LICENSE.GPL included in
* the packaging of this file. Please review the following information
* to ensure GNU General Public Licensing requirements will be met:
* http://www.fsf.org/licensing/licenses/info/GPLv2.html and
* http://www.gnu.org/copyleft/gpl.html.
*/
#include "bslib.h"
#include "lauxlib.h"
#include "bslex.h"
#include "bsparser.h"
#include "bsrunner.h"
#include "bsqmakegen.h"
#include "bshost.h"
#include "bsunicode.h"
#include "bsvisitor.h"
#include <ctype.h>
#include <string.h>
#include <assert.h>
static int copy(lua_State* L)
{
enum Params { FROMFILE = 1, TOFILE };
BSPathStatus res = bs_normalize_path2(lua_tostring(L,FROMFILE));
if( res != BS_OK )
luaL_error(L,"invalid from-file: %s", lua_tostring(L,FROMFILE) );
lua_pushstring(L, bs_global_buffer() );
const int fromFile = lua_gettop(L);
res = bs_normalize_path2(lua_tostring(L,TOFILE));
if( res != BS_OK )
luaL_error(L,"invalid to-file: %s", lua_tostring(L,TOFILE) );
lua_pushstring(L, bs_global_buffer() );
const int toFile = lua_gettop(L);
bs_copy(lua_tostring(L,toFile),lua_tostring(L,fromFile));
return 0;
}
static int bs_version(lua_State *L)
{
lua_pushstring(L,BS_BSVERSION);
return 1;
}
// returns: current directory in normalized form
static int bs_getcwd (lua_State *L)
{
if( bs_cwd() == BS_OK )
lua_pushstring(L, bs_global_buffer());
else
luaL_error(L,"getcwd: received non supported path from OS");
return 1;
}
static int host_cpu(lua_State *L)
{
BSCpu c = bs_host_cpu();
lua_pushstring(L,c.name);
lua_pushinteger(L,c.ver);
return 2;
}
static int host_os(lua_State *L)
{
lua_pushstring(L,bs_host_os());
return 1;
}
static int host_wordsize(lua_State *L)
{
lua_pushinteger(L,bs_wordsize());
return 1;
}
static int host_compiler(lua_State *L)
{
BSCompiler c = bs_host_compiler();
lua_pushstring(L,c.name);
lua_pushinteger(L,c.ver);
return 2;
}
// returns: file path of the currently running application in normalized form
static int thisapp (lua_State *L)
{
return bs_thisapp2(L);
}
static void push_normalized(lua_State *L, int path)
{
int res = bs_normalize_path2(lua_tostring(L,path));
switch(res)
{
case BS_OK:
break;
case BS_NotSupported:
luaL_error(L,"path format is not supported: %s", lua_tostring(L,path) );
break;
case BS_InvalidFormat:
luaL_error(L,"path format is invalid: %s", lua_tostring(L,path) );
break;
case BS_OutOfSpace:
luaL_error(L,"path is too long to be handled: %s", lua_tostring(L,path) );
break;
default:
luaL_error(L,"unknown error for path: %s", lua_tostring(L,path) );
break;
}
lua_pushstring(L, bs_global_buffer());
if( strncmp(bs_global_buffer(),"//",2) != 0 )
{
// relative path
res = bs_cwd();
if( res != BS_OK )
luaL_error(L,"getcwd delivered a path not supported by this application" );
lua_pushstring(L,bs_global_buffer());
res = bs_add_path(L,-1,-2);
if( res != 0 )
luaL_error(L,"creating absolute path from provided root gives an error: %s", lua_tostring(L,1) );
// stack: rhs, lhs, abspath
lua_replace(L,-3);
lua_pop(L,1);
// stack: abspath
}
}
// NOTE: if BUSY is built information about the OS and toolchain is melted into the executable and doesn't have
// to be explicitly set when BUSY is used.
int bs_compile (lua_State *L)
{
enum { SOURCE_DIR = 1, BUILD_DIR, PARAMS };
int i;
for( i = lua_gettop(L); i < 3; i++ )
lua_pushnil(L); // add the missing args
const int top = lua_gettop(L);
if( lua_isnil(L,SOURCE_DIR) )
{
lua_pushstring(L, "..");
lua_replace(L,SOURCE_DIR);
}
if( lua_isnil(L,BUILD_DIR) )
{
lua_pushstring(L, "./output");
lua_replace(L,BUILD_DIR);
}
if( lua_isnil(L,PARAMS) )
{
lua_createtable(L,0,0);
lua_replace(L,PARAMS);
}
// set lua path so no environment can intervene
lua_getglobal(L, "package");
lua_pushstring(L,"./?.lua");
lua_setfield(L,-2,"path");
lua_pushstring(L,"");
lua_setfield(L,-2,"cpath");
lua_pop(L,1);
lua_getglobal(L, "require");
lua_pushstring(L, "builtins");
lua_call(L,1,1);
const int builtins = lua_gettop(L);
lua_getfield(L,-1,"#inst");
const int binst = lua_gettop(L);
push_normalized(L,BUILD_DIR);
lua_setfield(L,binst,"root_build_dir");
push_normalized(L,SOURCE_DIR);
fprintf(stdout,"# running parser\n# root source directory is %s\n",lua_tostring(L,-1));
fflush(stdout);
lua_setfield(L,binst,"root_source_dir");
lua_pushnil(L); /* first key */
while (lua_next(L, PARAMS) != 0) {
// go through all params and see if there is a global param with the same name and if so apply it
const int key = lua_gettop(L)-1;
lua_pushvalue(L,key);
lua_rawget(L,builtins);
if( !lua_isnil(L,-1) )
{
const int decl = lua_gettop(L);
lua_getfield(L,decl,"#kind");
const int k = lua_tointeger(L,-1);
lua_getfield(L,decl,"#rw");
const int rw = lua_tointeger(L,-1);
lua_pop(L, 2); // k, rw
lua_getfield(L,decl,"#type");
const int refType = lua_gettop(L);
if( k == BS_VarDecl && rw == BS_param )
{
lua_pushvalue(L,key);
if( bs_getAndCheckParam( L, builtins, PARAMS, key, 1, refType ) == 0 )
{
if( !lua_isnil(L,-1) )
// the param value is ok, assign it
lua_rawset(L,binst); // eats value
else
lua_pop(L,1); // value
}else
lua_error(L);
}
lua_pop(L, 1); // refType
}
lua_pop(L, 2); // value, decl
}
lua_pop(L,2); // builtins, binst
lua_createtable(L,0,0);
lua_setglobal(L,"#xref"); // overwrite an existing #xref if present
lua_createtable(L,0,0);
lua_createtable(L,0,0);
lua_pushstring(L, "v");
lua_setfield(L, -2, "__mode");
lua_setmetatable(L, -2);
lua_setglobal(L,"#refs"); // overwrite an existing #refs if present
lua_pushcfunction(L, bs_parse);
push_normalized(L,SOURCE_DIR);
lua_createtable(L,0,0); // module definition
const int module = lua_gettop(L);
lua_pushinteger(L, BS_ModuleDef);
lua_setfield(L,module,"#kind");
lua_pushstring(L,"."); // start rdir from '.'
lua_setfield(L,module,"#rdir"); // virtual directory relative to source root
lua_pushstring(L,"."); // start rdir from '.'
lua_setfield(L,module,"#fsrdir"); // file system directory relative to source root
lua_pushvalue(L,module);
lua_setglobal(L, "#root");
lua_pushvalue(L,PARAMS);
lua_call(L,3,1);
// module is on the stack
lua_pushnil(L);
while (lua_next(L, 3) != 0)
{
luaL_error(L,"cannot set unknown parameter: %s", lua_tostring(L,-2) );
lua_pop(L, 1);
}
const int bottom = lua_gettop(L);
assert( top + 1 == bottom );
return 1;
}
static int resolvedesig(lua_State *L, const char* desig, int root)
{
const char* p = desig;
const char* q = desig;
lua_pushvalue(L,root);
const int module = lua_gettop(L);
while( 1 )
{
uchar len = 0;
const uint ch = unicode_decode_utf8((const uchar*)q,&len);
if( len == 0 )
luaL_error(L,"the passed-in product designator has invalid UTF-8 format: %s",desig);
if( ch == 0 || ch == '.' )
{
if( p == q )
luaL_error(L,"the passed-in product designator has invalid syntax: %s",desig);
if( p != desig )
{
if( !lua_istable(L,-1) )
{
lua_pushlstring(L,desig,p-desig-1);
luaL_error(L,"'%s' of passed-in designator '%s' cannot be dereferenced",lua_tostring(L,-1),desig);
}
assert(lua_istable(L,-1));
lua_getfield(L,-1,"#kind");
const int k = lua_tointeger(L,-1);
lua_pop(L,1);
if( k != BS_ModuleDef )
{
lua_pushlstring(L,desig,p-desig-1);
luaL_error(L,"'%s' of passed-in designator '%s' must be a subdir declaration",lua_tostring(L,-1),desig);
}
lua_getfield(L,-1,"#visi");
const int v = lua_tointeger(L,-1);
lua_pop(L,1);
if( v != BS_Public )
{
lua_pushlstring(L,desig,p-desig-1);
luaL_error(L,"subdir '%s' of passed-in designator '%s' is not public",lua_tostring(L,-1),desig);
}
}
lua_pushlstring(L,p,q-p);
lua_rawget(L,module);
if( lua_isnil(L,-1) )
{
lua_pushlstring(L,p,q-p);
luaL_error(L,"identifier '%s' of passed-in designator '%s' not found",lua_tostring(L,-1),desig);
}
p = q + 1;
if( ch == '.' && *p == 0 )
luaL_error(L,"the passed-in product designator has invalid syntax: %s",desig);
lua_replace(L,module);
}
if( ch == 0 )
break;
q += len;
}
return 1; // returns found decl or doesn't return
}
static int isproductvardecl(lua_State *L, int t, int bi)
{
if( t <= 0 )
t += lua_gettop(L) + 1;
if( !lua_istable(L,t) )
return 0;
lua_getfield(L,t,"#kind");
const int k = lua_tointeger(L,-1);
lua_pop(L,1);
if( k != BS_VarDecl )
return 0;
lua_getfield(L,t,"#type");
lua_getfield(L,bi,"Product");
const int res = bs_isa(L,-1,-2);
lua_pop(L,2);
return res;
}
static int fetchInstOfDecl(lua_State* L, int decl)
{
const int top = lua_gettop(L);
if( decl < 0 )
decl += top + 1;
lua_getfield(L,decl,"#owner");
lua_getfield(L,-1,"#inst");
lua_replace(L,-2);
// stack: modinst
lua_getfield(L,decl,"#name");
lua_rawget(L,-2);
// stack: modinst, classinst
lua_replace(L,-2);
assert(top + 1 == lua_gettop(L) );
return 1;
}
int bs_findProductsToProcess(lua_State *L )
{
enum { ROOT = 1, PRODS, builtins };
lua_createtable(L,10,0);
const int res = lua_gettop(L);
int n = 0;
if( lua_isnil(L,PRODS) )
{
// search for default products an run them
const int len = lua_objlen(L,ROOT);
int i;
for( i = 1; i <= len; i++ )
{
lua_rawgeti(L,ROOT,i);
const int decl = lua_gettop(L);
if( isproductvardecl(L,decl,builtins) )
{
lua_getfield(L,-1,"#visi");
const int visi = lua_tointeger(L,-1);
lua_pop(L,1);
if( visi == BS_PublicDefault )
{
fetchInstOfDecl(L,decl);
lua_rawseti(L,res,++n);
}
}
lua_pop(L,1); // decl
}
if( lua_objlen(L,res) == 0 )
luaL_error(L,"the module doesn't have any default product declarations");
}else
{
// run through all products in the set; get insts; error if not visible
lua_pushnil(L);
while (lua_next(L, PRODS) != 0)
{
resolvedesig(L,lua_tostring(L,-2),ROOT);
const int decl = lua_gettop(L);
if( isproductvardecl(L,decl,builtins) )
{
lua_getfield(L,-1,"#visi");
const int visi = lua_tointeger(L,-1);
lua_pop(L,1);
if( visi >= BS_Public )
{
fetchInstOfDecl(L,decl);
lua_rawseti(L,res,++n);
}else
luaL_error(L,"the declaration is not visible from outside: %s", lua_tostring(L,-3));
}else
luaL_error(L,"no valid product declaration: %s", lua_tostring(L,-3));
lua_pop(L,2); // key, value
}
}
return 1; // leaves res on stack
}
int bs_execute (lua_State *L)
{
enum { ROOT = 1, PRODS };
const int top = lua_gettop(L);
if( !lua_istable(L,ROOT) )
luaL_error(L,"expecting a module definition");
lua_getfield(L,ROOT,"#kind");
if( lua_tointeger(L,-1) != BS_ModuleDef )
luaL_error(L,"expecting a module definition");
else
lua_pop(L,1); // kind
lua_getglobal(L, "require");
lua_pushstring(L, "builtins");
lua_call(L,1,1);
const int builtins = lua_gettop(L);
lua_getfield(L,ROOT,"#dir");
const int source_dir = lua_gettop(L);
fprintf(stdout,"# running build for %s\n",lua_tostring(L,source_dir));
lua_getfield(L,builtins,"#inst");
const int binst = lua_gettop(L);
lua_getfield(L,binst,"root_build_dir");
const int build_dir = lua_gettop(L);
fprintf(stdout,"# root build directory is %s\n",lua_tostring(L,-1));
fflush(stdout);
// we create build dir tree here because target dependencies can skip directories and cause
// mkdir errors if levels are (temporarily) missing.
lua_pushcfunction(L, bs_createBuildDirs);
lua_pushvalue(L,ROOT);
lua_pushvalue(L,build_dir);
lua_call(L,2,0);
lua_pop(L,3); // source_dir, binst, build_dir
lua_pushcfunction(L, bs_findProductsToProcess);
lua_pushvalue(L,ROOT);
lua_pushvalue(L,PRODS);
lua_pushvalue(L,builtins);
lua_call(L,3,1);
lua_replace(L,PRODS);
lua_pop(L,1); // builtins
// build all products in the set; first check for error message dependents
size_t i;
for( i = 1; i <= lua_objlen(L,PRODS); i++ )
{
lua_pushcfunction(L, bs_precheck);
lua_rawgeti(L,PRODS,i);
lua_call(L,1,0);
}
for( i = 1; i <= lua_objlen(L,PRODS); i++ )
{
lua_pushcfunction(L, bs_run);
lua_rawgeti(L,PRODS,i);
lua_call(L,1,0);
}
const int bottom = lua_gettop(L);
assert( top == bottom );
return 0;
}
static int Test_BSBeginOp(BSBuildOperation op, const char* command, int t, int o, void* data)
{
switch(op)
{
case BS_Compile:
fprintf(stdout,"COMPILE: ");
break;
case BS_LinkExe:
case BS_LinkDll:
case BS_LinkLib:
fprintf(stdout,"LINK: ");
break;
case BS_RunMoc:
fprintf(stdout,"MOC: ");
break;
case BS_RunRcc:
fprintf(stdout,"RCC: ");
break;
case BS_RunUic:
fprintf(stdout,"UIC: ");
break;
case BS_RunLua:
fprintf(stdout,"LUA: ");
break;
case BS_Copy:
fprintf(stdout,"COPY: ");
break;
case BS_EnteringProduct:
fprintf(stdout,"### running ");
break;
default:
fprintf(stdout,"BEGIN OP: %d ", op);
break;
}
fprintf(stdout, "%s\n", command );
fflush(stdout);
return 0;
}
static void Test_BSOpParam(BSBuildParam p, const char* value, void* data)
{
switch(p)
{
case BS_infile:
fprintf(stdout," INFILE: ");
break;
case BS_outfile:
fprintf(stdout," OUTFILE: ");
break;
case BS_cflag:
fprintf(stdout," CFLAG: ");
break;
case BS_define:
fprintf(stdout," DEFINE: ");
break;
case BS_include_dir:
fprintf(stdout," INCLUDEDIR: ");
break;
case BS_ldflag:
fprintf(stdout," LDFLAG: ");
break;
case BS_lib_dir:
fprintf(stdout," LIBDIR: ");
break;
case BS_lib_name:
fprintf(stdout," LIBNAME: ");
break;
default:
fprintf(stdout," PARAM: ");
break;
}
fprintf(stdout, "%s\n", value );
fflush(stdout);
}
static void Test_BSForkGroup(int n, void* data)
{
if( n >= 0 )
fprintf(stdout,"BEGIN PARALLEL: %d\n", n);
else
fprintf(stdout,"END PARALLEL\n");
fflush(stdout);
}
// param: what, root module def
// opt param: set of product desigs to be built
static int bs_generate (lua_State *L)
{
enum { WHAT = 1, ROOT, PRODS };
const int top = lua_gettop(L);
if( !lua_isstring(L,WHAT) )
luaL_error(L,"invalid generator selected");
if( !lua_istable(L,ROOT) )
luaL_error(L,"expecting a module definition");
lua_getfield(L,ROOT,"#kind");
if( lua_tointeger(L,-1) != BS_ModuleDef )
luaL_error(L,"expecting a module definition");
else
lua_pop(L,1); // kind
lua_getglobal(L, "require");
lua_pushstring(L, "builtins");
lua_call(L,1,1);
const int builtins = lua_gettop(L);
lua_getfield(L,ROOT,"#dir");
const int source_dir = lua_gettop(L);
fprintf(stdout,"# running generator '%s' for %s\n",lua_tostring(L,WHAT),lua_tostring(L,source_dir));
lua_getfield(L,builtins,"#inst");
const int binst = lua_gettop(L);
lua_getfield(L,binst,"root_build_dir");
const int build_dir = lua_gettop(L);
fprintf(stdout,"# root output directory is %s\n",lua_tostring(L,-1));
fflush(stdout);
lua_pop(L,3); // source_dir, binst, build_dir
lua_pushcfunction(L, bs_findProductsToProcess);
lua_pushvalue(L,ROOT);
lua_pushvalue(L,PRODS);
lua_pushvalue(L,builtins);
lua_call(L,3,1);
lua_replace(L,PRODS);
lua_pop(L,1); // builtins
// generate all products in the set; first check for error message dependents
size_t i;
for( i = 1; i <= lua_objlen(L,PRODS); i++ )
{
lua_pushcfunction(L, bs_precheck);
lua_rawgeti(L,PRODS,i);
lua_call(L,1,0);
}
if( strcmp(lua_tostring(L,WHAT),"qmake") == 0 )
{
lua_pushcfunction(L, bs_genQmake);
lua_pushvalue(L,ROOT);
lua_pushvalue(L,PRODS);
lua_call(L,2,0);
}else if( strcmp(lua_tostring(L,WHAT),"test") == 0 )
{
for( i = 1; i <= lua_objlen(L,PRODS); i++ )
{
lua_pushcfunction(L, bs_visit);
lua_rawgeti(L,PRODS,i);
BSVisitorCtx* ctx = bs_newctx(L);
ctx->d_data = 0;
ctx->d_log = 0;
ctx->d_loggerData = 0;
ctx->d_end = 0;
ctx->d_begin = Test_BSBeginOp;
ctx->d_param = Test_BSOpParam;
ctx->d_fork = Test_BSForkGroup;
lua_call(L,2,0);
}
}else
luaL_error(L,"unknown generator '%s'", lua_tostring(L,WHAT));
const int bottom = lua_gettop(L);
assert( top == bottom );
return 0;
}
static const luaL_Reg bslib[] = {
{"compile", bs_compile},
{"execute", bs_execute},
{"generate", bs_generate},
{"dump", bs_dump},
{"getcwd", bs_getcwd},
{"thisapp", thisapp},
{"moc", bs_runmoc},
{"moc_name", bs_mocname},
{"copy", copy},
{"run", bs_run},
{"cpu", host_cpu },
{"os", host_os },
{"wordsize", host_wordsize },
{"compiler", host_compiler },
{"version", bs_version },
{NULL, NULL}
};
LUALIB_API int bs_open_busy (lua_State *L) {
luaL_register(L, BS_BSLIBNAME, bslib);
return 1;
}