-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbsvisitor.c
1703 lines (1459 loc) · 50.6 KB
/
bsvisitor.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
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
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2023 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 "bsvisitor.h"
#include "bshost.h"
#include "bsparser.h"
#include "bslex.h" // default_logger
#include "lauxlib.h"
#include <assert.h>
#include <string.h>
enum VISIT_ARGS { PRODINST = 1, CTX };
static int calcdesig(lua_State* L, int decl)
{
return bs_declpath(L,decl,".");
}
static int isa(lua_State* L, int builtins, int cls, const char* what )
{
lua_getfield(L,builtins,what);
const int res = bs_isa(L,-1,cls);
lua_pop(L,1);
return res;
}
static void addflags(lua_State* L, int list, int out)
{
if( list < 0 )
list += lua_gettop(L) + 1;
const int n = lua_objlen(L,list);
int i;
for( i = 1; i <= n; i++ )
{
lua_pushvalue(L,out);
lua_pushstring(L," ");
lua_rawgeti(L,list,i);
lua_concat(L,3);
lua_replace(L,out);
}
}
static void addPath(lua_State* L, int lhs, int rhs)
{
if( *lua_tostring(L,rhs) == '/' )
lua_pushvalue(L, rhs);
else if( bs_add_path(L,lhs,rhs) )
luaL_error(L,"creating absolute path from provided root gives an error: %s %s",
lua_tostring(L,lhs), lua_tostring(L,rhs) );
}
static void copyItems(lua_State* L, int inlist, int outlist, BSOutKind what )
{
lua_getfield(L,inlist,"#kind");
const unsigned k = lua_tointeger(L,-1);
lua_pop(L,1); // kind
if( k == BS_Mixed )
{
size_t i, len = lua_objlen(L,inlist);
for( i = 1; i <= len; i++ )
{
lua_rawgeti(L,inlist,i);
const int sublist = lua_gettop(L);
copyItems(L,sublist,outlist,what);
lua_pop(L,1); // sublist
}
}else if( k == what )
{
// also works with BS_DynamicLib, BS_StaticLib and BS_Executable
size_t i, len = lua_objlen(L,inlist);
for( i = 1; i <= len; i++ )
{
lua_rawgeti(L,inlist,i);
lua_rawseti(L,outlist, lua_objlen(L,outlist)+1 );
}
}
}
static void prefixCmd(lua_State* L, int cmd, int binst, int to_host)
{
const char* toolchain_prefix = "#toolchain_prefix";
const char* toolchain_path = "#toolchain_path";
if( !to_host )
{
toolchain_prefix = "target_toolchain_prefix";
toolchain_path = "target_toolchain_path";
}
lua_getfield(L,binst,toolchain_prefix);
if( !lua_isnil(L,-1) && *lua_tostring(L,-1) != 0 )
{
lua_pushvalue(L,cmd);
lua_concat(L,2);
lua_replace(L,cmd); // prefix cmd with toolchain_prefix
}else
lua_pop(L,1);
lua_getfield(L,binst,toolchain_path);
if( !lua_isnil(L,-1) && strcmp( lua_tostring(L,-1), "." ) != 0 )
{
lua_pushfstring(L,"%s/%s", bs_denormalize_path(lua_tostring(L,-1)), lua_tostring(L,cmd) );
lua_replace(L,cmd); // prefix cmd with path
lua_pop(L,1);
}else
lua_pop(L,1);
}
static void emitFlags(lua_State* L,int inst, BSVisitorCtx* ctx, BSBuildParam paramType, const char* field)
{
assert( ctx->d_param != 0 );
lua_getfield(L,inst,"configs");
const int configs = lua_gettop(L);
size_t i;
for( i = 1; i <= lua_objlen(L,configs); i++ )
{
lua_rawgeti(L,configs,i);
const int config = lua_gettop(L);
// TODO: check for circular deps
emitFlags(L, config, ctx, paramType, field);
lua_pop(L,1); // config
}
lua_pop(L,1); // configs
lua_getfield(L,inst, field);
const int list = lua_gettop(L);
const size_t n = lua_objlen(L,list);
for( i = 1; i <= n; i++ )
{
lua_rawgeti(L,list,i);
ctx->d_param(paramType,lua_tostring(L,-1),ctx->d_data);
lua_pop(L,1);
}
lua_pop(L,1);
}
static void emitPaths(lua_State* L,int inst, BSVisitorCtx* ctx, BSBuildParam paramType, const char* field)
{
assert( ctx->d_param != 0 );
lua_getfield(L,inst,"configs");
const int configs = lua_gettop(L);
size_t i;
for( i = 1; i <= lua_objlen(L,configs); i++ )
{
lua_rawgeti(L,configs,i);
const int config = lua_gettop(L);
// TODO: check for circular deps
emitPaths(L,config, ctx, paramType, field);
lua_pop(L,1); // config
}
lua_pop(L,1); // configs
bs_getModuleVar(L,inst,"#dir");
const int absDir = lua_gettop(L);
lua_getfield(L,inst,field);
const int incls = lua_gettop(L);
for( i = 1; i <= lua_objlen(L,incls); i++ )
{
lua_rawgeti(L,incls,i);
const int path = lua_gettop(L);
if( *lua_tostring(L,-1) != '/' )
{
// relative path
addPath(L,absDir,path);
lua_replace(L,path);
}
ctx->d_param(paramType, bs_denormalize_path(lua_tostring(L,path)),ctx->d_data);
lua_pop(L,1); // path
}
lua_pop(L,2); // absDir, incls
}
static void emitPath(lua_State* L,int inst, BSVisitorCtx* ctx, BSBuildParam paramType, const char* field)
{
assert( ctx->d_param != 0 );
lua_getfield(L,inst,"configs");
const int configs = lua_gettop(L);
size_t i;
for( i = 1; i <= lua_objlen(L,configs); i++ )
{
lua_rawgeti(L,configs,i);
const int config = lua_gettop(L);
// TODO: check for circular deps
emitPath(L,config, ctx, paramType, field);
lua_pop(L,1); // config
}
lua_pop(L,1); // configs
bs_getModuleVar(L,inst,"#dir");
const int absDir = lua_gettop(L);
lua_getfield(L,inst,field);
const int path = lua_gettop(L);
if( lua_isstring(L,path) )
{
if( *lua_tostring(L,path) != '/' )
{
// relative path
addPath(L,absDir,path);
lua_replace(L,path);
}
ctx->d_param(paramType, bs_denormalize_path(lua_tostring(L,path)),ctx->d_data);
}
lua_pop(L,2); // absDir, path
}
static void compilesources(lua_State* L, BSVisitorCtx* ctx, int builtins, int inlist)
{
const int top = lua_gettop(L);
lua_createtable(L,0,0);
const int outlist = lua_gettop(L);
lua_pushinteger(L,BS_ObjectFiles);
lua_setfield(L,outlist,"#kind");
lua_pushvalue(L,outlist);
lua_setfield(L,PRODINST,"#out");
lua_getfield(L,builtins,"#inst");
const int binst = lua_gettop(L);
lua_getfield(L,PRODINST,"to_host");
const int to_host = lua_toboolean(L,-1);
lua_pop(L,1);
const BSToolchain toolchain = bs_getToolchain(L,binst,to_host);
lua_getfield(L,binst,"#ctdefaults");
if( to_host )
lua_getfield(L,binst,"host_toolchain");
else
lua_getfield(L,binst,"target_toolchain");
lua_rawget(L,-2);
lua_replace(L,-2);
const int ctdefaults = lua_gettop(L);
lua_getfield(L,binst,"root_build_dir");
const int rootOutDir = lua_gettop(L);
bs_getModuleVar(L,PRODINST,"#dir");
const int absDir = lua_gettop(L);
bs_getModuleVar(L,PRODINST,"#rdir");
const int relDir = lua_gettop(L);
const BSOperatingSystem os = bs_getOperatingSystem(L,binst,to_host);
size_t i;
lua_getfield(L,PRODINST,"sources");
const int sources = lua_gettop(L);
lua_createtable(L,lua_objlen(L,sources),0);
const int tmp = lua_gettop(L);
copyItems(L,inlist,tmp, BS_SourceFiles);
int n = lua_objlen(L,tmp);
lua_createtable(L,n,0);
const int generated = lua_gettop(L);
for( i = 1; i <= n; i++ )
{
lua_rawgeti(L,tmp,i);
lua_rawseti(L,generated,i);
}
lua_setfield(L,PRODINST,"#generated");
for( i = 1; i <= lua_objlen(L,sources); i++ )
{
// copy all sources to a new table to avoid changing the original sources
lua_rawgeti(L,sources,i);
lua_rawseti(L,tmp,++n);
}
lua_replace(L,sources);
// the result of source files received via dependencies appeares before the results of this source files
copyItems(L,inlist,outlist, BS_ObjectFiles);
n = lua_objlen(L,outlist);
if( ctx->d_fork )
ctx->d_fork(lua_objlen(L,sources),ctx->d_data);
for( i = 1; i <= lua_objlen(L,sources); i++ )
{
lua_rawgeti(L,sources,i);
const int file = lua_gettop(L);
const int lang = bs_guessLang(lua_tostring(L,file));
if( lang == BS_unknownLang )
luaL_error(L,"source file type not supported: %s",lua_tostring(L,file));
if( lang == BS_header )
{
lua_pop(L,1);
continue;
}
if( *lua_tostring(L,file) != '/' )
addPath(L,absDir,file); // path could be absolute!
else
lua_pushvalue(L,file);
const int src = lua_gettop(L);
addPath(L,rootOutDir,relDir);
// we need to prefix object files of separate products in the same module
// otherwise object files could overwrite each other
lua_pushstring(L,"/");
lua_getfield(L,PRODINST,"#decl");
lua_getfield(L,-1,"#name");
lua_replace(L,-2);
// strip all path segments with bs_filename; otherwise subdirs have to be created for files accessed
// from other than the module directory
#ifdef BS_HAVE_FILE_PREFIX
// the name is actually not relevant, so we can just prefix it with a number to reduce name collisions
// (possible when collecting files from different directories in the same module)
lua_pushfstring(L,"_%d_%s",i,bs_filename(lua_tostring(L,file)));
#else
lua_pushfstring(L,"_%s",bs_filename(lua_tostring(L,file)));
#endif
if( toolchain == BS_msvc )
lua_pushstring(L,".obj");
else
lua_pushstring(L,".o");
lua_concat(L,5); // dir, slash, prefix, filename, ext
const int out = lua_gettop(L);
lua_pushvalue(L,out);
lua_rawseti(L,outlist,++n);
switch(toolchain)
{
case BS_gcc:
lua_pushstring(L,"gcc");
break;
case BS_clang:
lua_pushstring(L,"clang");
break;
case BS_msvc:
lua_pushstring(L,"cl");
break;
default:
break;
}
const int cmd = lua_gettop(L);
prefixCmd(L, cmd, binst, to_host);
if( ctx->d_begin )
ctx->d_begin(BS_Compile, lua_tostring(L,cmd), toolchain, os, ctx->d_data);
lua_pop(L,1); // cmd
if( ctx->d_param )
{
if( !lua_isnil(L,ctdefaults) )
emitFlags(L,ctdefaults,ctx, BS_cflag, "cflags");
emitFlags(L,PRODINST,ctx, BS_cflag, "cflags");
switch(lang)
{
case BS_c:
if( !lua_isnil(L,ctdefaults) )
emitFlags(L,ctdefaults,ctx, BS_cflag, "cflags_c");
emitFlags(L,PRODINST,ctx, BS_cflag, "cflags_c");
break;
case BS_cc:
if( !lua_isnil(L,ctdefaults) )
emitFlags(L,ctdefaults,ctx, BS_cflag, "cflags_cc");
emitFlags(L,PRODINST,ctx, BS_cflag, "cflags_cc");
break;
case BS_objc:
if( !lua_isnil(L,ctdefaults) )
emitFlags(L,ctdefaults,ctx, BS_cflag, "cflags_objc");
emitFlags(L,PRODINST,ctx, BS_cflag, "cflags_objc");
break;
case BS_objcc:
if( !lua_isnil(L,ctdefaults) )
emitFlags(L,ctdefaults,ctx, BS_cflag, "cflags_objcc");
emitFlags(L,PRODINST,ctx, BS_cflag, "cflags_objcc");
break;
}
if( !lua_isnil(L,ctdefaults) )
emitFlags(L,ctdefaults,ctx, BS_define, "defines");
emitFlags(L,PRODINST,ctx, BS_define, "defines");
if( !lua_isnil(L,ctdefaults) )
emitPaths(L,ctdefaults,ctx, BS_include_dir, "include_dirs");
emitPaths(L,PRODINST,ctx, BS_include_dir, "include_dirs");
ctx->d_param(BS_outfile, bs_denormalize_path(lua_tostring(L,out)), ctx->d_data);
ctx->d_param(BS_infile, bs_denormalize_path(lua_tostring(L,src)), ctx->d_data);
}
if( ctx->d_end )
ctx->d_end(ctx->d_data);
lua_pop(L,3); // file, source, dest
}
lua_pop(L,1); // sources
if( ctx->d_fork )
ctx->d_fork(-1,ctx->d_data);
lua_pop(L,6); // outlist, binst, ctdefaults, rootOutDir...relDir
const int bottom = lua_gettop(L);
assert( top == bottom );
}
static void renderobjectfiles(lua_State* L, int list, BSVisitorCtx* ctx, int toolchain, int resKind)
{
assert( ctx->d_param );
// BS_ObjectFiles: list of file names
// BS_StaticLib, BS_DynamicLib, BS_Executable: one file name
// BS_Mixed: list of tables
lua_getfield(L,list,"#kind");
const int k = lua_tointeger(L,-1);
lua_pop(L,1); // kind
size_t i;
switch(k)
{
case BS_Mixed:
for( i = lua_objlen(L,list); i >= 1; i-- ) // turn dependency order for rendering Products in reverse order
{
lua_rawgeti(L,list,i);
const int sublist = lua_gettop(L);
assert( lua_istable(L,sublist) );
renderobjectfiles(L,sublist,ctx, toolchain, resKind);
lua_pop(L,1); // sublist
}
break;
case BS_ObjectFiles:
for( i = 1; i <= lua_objlen(L,list); i++ ) // keep original order
{
lua_rawgeti(L,list,i);
const int path = lua_gettop(L);
ctx->d_param(BS_infile,bs_denormalize_path(lua_tostring(L,path)),ctx->d_data);
lua_pop(L,1); // path
}
break;
case BS_StaticLib:
case BS_DynamicLib:
if( resKind != BS_StaticLib ) // only link libs from inlist if a dynamic lib or exe is created;
// otherwise they are passed on; ar doesn't seem to create a suiable .a with .a as input
{
lua_rawgeti(L,list,1); // there is only one item, which has index 1
const int path = lua_gettop(L);
if( toolchain == BS_msvc && k == BS_DynamicLib)
{
// add .lib because msvc requires an import library to use the dll
lua_pushstring(L,".lib");
lua_concat(L,2); // the name of the import library is xyz.dll.lib
}
ctx->d_param(BS_infile,bs_denormalize_path(lua_tostring(L,path)),ctx->d_data);
lua_pop(L,1); // path
}
break;
default:
// ignore
break;
}
}
static int makeCopyOfLibs(lua_State* L, int inlist)
{
const int top = lua_gettop(L);
lua_getfield(L,inlist,"#kind");
const int k = lua_tointeger(L,-1);
lua_pop(L,1); // kind
if( k != BS_Mixed )
return 0;
size_t i, len = 0;
len = lua_objlen(L,inlist);
int hasLibs = 0;
for( i = 1; i <= len; i++ )
{
lua_rawgeti(L,inlist,i);
const int sublist = lua_gettop(L);
lua_getfield(L,sublist,"#kind");
const int k = lua_tointeger(L,-1);
lua_pop(L,2); // kind, sublist
assert( k != BS_Mixed );
if( k == BS_StaticLib || k == BS_DynamicLib )
{
hasLibs = 1;
break;
}
}
if( hasLibs )
{
lua_createtable(L,0,0);
lua_pushinteger(L,BS_Mixed);
lua_setfield(L,-2,"#kind");
const int outlist = lua_gettop(L);
int n = 0;
for( i = 1; i <= len; i++ )
{
lua_rawgeti(L,inlist,i);
const int sublist = lua_gettop(L);
lua_getfield(L,sublist,"#kind");
const int k = lua_tointeger(L,-1);
lua_pop(L,1); // kind
if( k == BS_StaticLib || k == BS_DynamicLib )
lua_rawseti(L,outlist,++n);
else
lua_pop(L,1); // sublist
}
}
assert( top + ( hasLibs ? 1: 0 ) == lua_gettop(L) );
return hasLibs;
}
static void link(lua_State* L, BSVisitorCtx* ctx, int builtins, int inlist, int resKind)
{
assert( resKind == BS_Executable || resKind == BS_DynamicLib || resKind == BS_StaticLib );
const int top = lua_gettop(L);
lua_getfield(L,builtins,"#inst");
const int binst = lua_gettop(L);
lua_getfield(L,PRODINST,"to_host");
const int to_host = lua_toboolean(L,-1);
lua_pop(L,1);
const BSToolchain toolchain = bs_getToolchain(L,binst,to_host);
const BSOperatingSystem os = bs_getOperatingSystem(L,binst,to_host);
const int win32 = os == BS_windows;
const int mac = os == BS_mac;
lua_getfield(L,binst,"#ctdefaults");
if( to_host )
lua_getfield(L,binst,"host_toolchain");
else
lua_getfield(L,binst,"target_toolchain");
lua_rawget(L,-2);
lua_replace(L,-2);
const int ctdefaults = lua_gettop(L);
lua_getfield(L,binst,"root_build_dir");
const int rootOutDir = lua_gettop(L);
bs_getModuleVar(L,PRODINST,"#rdir");
const int relDir = lua_gettop(L);
addPath(L,rootOutDir,relDir);
lua_pushvalue(L,-1);
lua_pushstring(L,"/");
if( !win32 && ( resKind == BS_DynamicLib || resKind == BS_StaticLib ) )
lua_pushstring(L,"lib"); // if not on Windows prefix the lib name with "lib"
else
lua_pushstring(L,"");
lua_getfield(L,PRODINST,"name");
if( lua_isnil(L,-1) || lua_objlen(L,-1) == 0 )
{
lua_pop(L,1);
lua_getfield(L,PRODINST,"#decl");
lua_getfield(L,-1,"#name");
lua_replace(L,-2);
}
lua_concat(L,4);
lua_replace(L,-2);
const int outbase = lua_gettop(L);
lua_pushvalue(L,outbase);
switch(resKind)
{
case BS_DynamicLib:
if( win32 )
lua_pushstring(L,".dll");
else if(mac)
lua_pushstring(L,".dylib");
else
lua_pushstring(L,".so");
break;
case BS_Executable:
if( win32 )
lua_pushstring(L,".exe");
else
lua_pushstring(L,"");
break;
case BS_StaticLib:
if( win32 )
lua_pushstring(L,".lib");
else
lua_pushstring(L,".a");
break;
}
lua_concat(L,2);
const int outfile = lua_gettop(L);
lua_pushvalue(L,outfile);
lua_setfield(L,PRODINST,"#product");
switch(toolchain)
{
default:
break;
case BS_gcc:
switch(resKind)
{
case BS_Executable:
case BS_DynamicLib:
lua_pushstring(L, "gcc");
break;
case BS_StaticLib:
lua_pushstring(L, "ar");
break;
}
break;
case BS_clang:
switch(resKind)
{
case BS_Executable:
case BS_DynamicLib:
lua_pushstring(L, "clang");
break;
case BS_StaticLib:
if( win32 )
lua_pushstring(L, "llvm-lib");
else
lua_pushstring(L, "ar");
break;
}
break;
case BS_msvc:
switch(resKind)
{
case BS_Executable:
case BS_DynamicLib:
lua_pushstring(L, "link");
break;
case BS_StaticLib:
lua_pushstring(L, "lib");
break;
}
break;
}
const int cmd = lua_gettop(L);
prefixCmd(L, cmd, binst, to_host);
if( ctx->d_begin )
{
BSBuildOperation op;
switch(resKind)
{
case BS_Executable:
op = BS_LinkExe;
break;
case BS_DynamicLib:
op = BS_LinkDll;
break;
case BS_StaticLib:
op = BS_LinkLib;
break;
}
ctx->d_begin(op, lua_tostring(L,cmd), toolchain, os, ctx->d_data);
}
lua_pop(L,1); // cmd
lua_createtable(L,0,0);
const int outlist = lua_gettop(L);
lua_pushinteger(L,resKind);
lua_setfield(L,outlist,"#kind");
lua_pushvalue(L,outfile);
lua_rawseti(L,outlist,1);
if( resKind == BS_StaticLib && makeCopyOfLibs(L,inlist) )
{
const int newOut = lua_gettop(L);
lua_pushvalue(L,outlist);
lua_rawseti(L,newOut,lua_objlen(L,newOut)+1);
lua_setfield(L,PRODINST,"#out");
}else
{
lua_pushvalue(L,outlist);
lua_setfield(L,PRODINST,"#out");
}
lua_pop(L,1); // outlist
if( ctx->d_param )
{
if( !lua_isnil(L,ctdefaults) )
emitFlags(L,ctdefaults,ctx, BS_ldflag, "ldflags");
emitFlags(L,PRODINST,ctx, BS_ldflag, "ldflags");
if( !lua_isnil(L,ctdefaults) )
emitFlags(L,ctdefaults,ctx, BS_lib_name, "lib_names");
emitFlags(L,PRODINST,ctx, BS_lib_name, "lib_names");
if( !lua_isnil(L,ctdefaults) )
emitFlags(L,ctdefaults,ctx, BS_framework, "frameworks");
emitFlags(L,PRODINST,ctx, BS_framework, "frameworks");
if( !lua_isnil(L,ctdefaults) )
emitPaths(L,ctdefaults,ctx, BS_lib_dir, "lib_dirs");
emitPaths(L,PRODINST,ctx, BS_lib_dir, "lib_dirs");
if( !lua_isnil(L,ctdefaults) )
emitPaths(L,ctdefaults,ctx, BS_lib_file, "lib_files");
emitPaths(L,PRODINST,ctx, BS_lib_file, "lib_files");
lua_getfield(L,PRODINST,"def_file");
if( !lua_isnil(L,-1) && strcmp(lua_tostring(L,-1),".") != 0 )
emitPath(L,PRODINST,ctx, BS_defFile, "def_file");
lua_pop(L,1);// def_file
ctx->d_param(BS_outfile,bs_denormalize_path(lua_tostring(L,outfile)), ctx->d_data);
renderobjectfiles(L, inlist, ctx, toolchain, resKind);
}
if( ctx->d_end )
ctx->d_end(ctx->d_data);
lua_pop(L,6); // binst, rootOutDir, relDir, ctdefaults, outbase, out
const int bottom = lua_gettop(L);
assert( top == bottom );
}
static void builddeps(lua_State* L, int inst)
{
const int top = lua_gettop(L);
lua_getfield(L,inst,"deps");
const int deps = lua_gettop(L);
if( lua_isnil(L,deps) )
{
lua_pop(L,1); // nil
return;
}
lua_createtable(L,0,0);
lua_pushinteger(L,BS_Mixed);
lua_setfield(L,-2,"#kind");
const int out = lua_gettop(L);
const int ndeps = lua_objlen(L,deps);
int nout = 0;
int i;
for( i = 1; i <= ndeps; i++ )
{
lua_pushcfunction(L, bs_visit);
lua_rawgeti(L,deps,i);
lua_pushvalue(L,CTX);
lua_call(L,2,0);
lua_rawgeti(L,deps,i);
lua_getfield(L,-1,"#out");
lua_replace(L,-2);
// stack: subout
const int subout = lua_gettop(L);
int k = BS_Nothing;
if( lua_istable(L,subout) )
{
lua_getfield(L,-1,"#kind");
k = lua_tointeger(L,-1);
lua_pop(L,1);
}
if( k == BS_Mixed )
{
const int nsubout = lua_objlen(L,subout);
int j;
for( j = 1; j <= nsubout; j++ )
{
lua_rawgeti(L,subout,j);
lua_getfield(L,-1,"#kind");
const int kk = lua_tointeger(L,-1);
lua_pop(L,1);
assert( kk != BS_Mixed);
lua_rawseti(L,out,++nout);
}
lua_pop(L,1); // subout
}else if( lua_istable(L,subout) )
lua_rawseti(L,out,++nout); // eats subout
else
lua_pop(L,1);
}
lua_setfield(L,inst,"#out");
lua_pop(L,1); // deps
const int bottom = lua_gettop(L);
assert( top == bottom );
}
static void library(lua_State* L,BSVisitorCtx* ctx, int cls, int builtins)
{
const int top = lua_gettop(L);
lua_getfield(L,PRODINST,"#out");
const int inlist = lua_gettop(L); // inlist is of kind BS_Mixed and doesn't have items of kind BS_Mixed
assert( lua_istable(L,inlist) );
compilesources(L,ctx,builtins, inlist);
lua_getfield(L,PRODINST,"lib_type");
const int lib_type = ( strcmp(lua_tostring(L,-1),"shared") == 0 ? BS_DynamicLib : BS_StaticLib );
lua_pop(L,1); // libtype
lua_getfield(L,PRODINST,"#out");
// compilerOut includes the object files from inlist and the ones generated by compilesources
const int compilerOut = lua_gettop(L);
if( makeCopyOfLibs(L,inlist) )
{
// top of stack is a new BS_Mixed created by makeCopyOfLibs which includes the libs from inlist (and only those)
// store the result of makeCopyOfLibs to the inlist slot
lua_replace(L,inlist);
// now consume compilerOut and add it to inlist
lua_rawseti(L,inlist,lua_objlen(L,inlist)+1);
// now the new BS_Mixed also includes the BS_ObjectFiles from compile output
}else
{
// make BS_ObjectFiles from compile output the new inlist
// now consume compilerOut and make it the new inlist
lua_replace(L,inlist);
}
// inlist here includes the object files generated by compilesources and inherited from the initial inlist
// in case a dynamic lib is to be generated by link(), inlist also includes the libs inherited from the initial inlist
// link sets out to a new table of kind BS_DynamicLib or BS_StaticLib; inlist is not passed out
link(L,ctx,builtins,inlist,lib_type);
lua_pop(L,1); // inlist
assert( top == lua_gettop(L) );
// passes on one lib (either BS_DynamicLib or BS_StaticLib), or a BS_Mixed of libs
}
static void executable(lua_State* L,BSVisitorCtx* ctx, int cls, int builtins)
{
const int top = lua_gettop(L);
lua_getfield(L,PRODINST,"#out");
const int inlist = lua_gettop(L);
assert( lua_istable(L,inlist) );
compilesources(L,ctx,builtins,inlist);
lua_getfield(L,PRODINST,"#out");
if( makeCopyOfLibs(L,inlist) )
{
// inlist included libs, so top of stack is new BS_Mixed which includes these libs (and only those)
lua_replace(L,inlist);
lua_rawseti(L,inlist,lua_objlen(L,inlist)+1);
// now the new BS_Mixed also includes the BS_ObjectFiles from compile output
}else
lua_replace(L,inlist); // make BS_ObjectFiles from compile output the new inlist
link(L,ctx,builtins,inlist,BS_Executable);
lua_pop(L,1); // mixed
assert( top == lua_gettop(L) );
// passes on one BS_Executable
}
static void sourceset(lua_State* L,BSVisitorCtx* ctx, int cls, int builtins)
{
const int top = lua_gettop(L);
lua_getfield(L,PRODINST,"#out");
const int inlist = lua_gettop(L);
assert( lua_istable(L,inlist) );
compilesources(L,ctx,builtins,inlist);
// #out is now a BS_ObjectFiles
if( makeCopyOfLibs(L,inlist) )
{
// inlist included libs, so top of stack is new BS_Mixed which includes these libs (and only those)
lua_replace(L,inlist);
lua_getfield(L,PRODINST,"#out");
lua_rawseti(L,inlist,lua_objlen(L,inlist)+1); // add BS_ObjectFiles to the new BS_Mixed
lua_setfield(L,PRODINST,"#out"); // set mixed again as inst.#out
}else
lua_pop(L,1); // inlist
// passes on a BS_ObjectFiles or a BS_Mixed with BS_ObjectFiles and the libs from orig inlist
assert( top == lua_gettop(L) );
}
static void group(lua_State* L,BSVisitorCtx* ctx, int cls, int builtins)
{
// NOP. deps were already built and result handed to inst.#out
}
static void config(lua_State* L,BSVisitorCtx* ctx, int cls, int builtins)
{
// NOP
}
static void concatReplace(lua_State* L, int to, const char* from, int fromLen)
{
lua_pushvalue(L,to);
lua_pushlstring(L,from,fromLen);
lua_concat(L,2);
lua_replace(L,to);
}
static BSPathStatus apply_arg_expansion(lua_State* L,int inst, int builtins, const char* source, const char* string)
{
const char* s = string;
lua_pushstring(L,"");
const int out = lua_gettop(L);
while( *s )
{
int offset, len;
const BSPathStatus res = bs_findToken( s, &offset, &len );
if( res == BS_OK )
{
concatReplace(L,out,s,offset);
const char* start = s + offset + 2; // skip {{
const BSPathPart t = bs_tokenType(start, len - 4);
if( t == BS_NoPathPart )
return BS_NotSupported;
if( t <= BS_extension )
{
if( source )
{
int len2;
const char* val = bs_path_part(source,t,&len2);
if( len2 < 0 )
return BS_NotSupported;
concatReplace(L,out,val,len2);
}else
return BS_NotSupported;
}else if( t == BS_RootBuildDir || t == BS_CurBuildDir )
{
lua_getfield(L,builtins,"#inst");
lua_getfield(L,-1,"root_build_dir");
lua_replace(L,-2);
if( t == BS_RootBuildDir )
{
const char* str = bs_denormalize_path(lua_tostring(L,-1));
concatReplace(L,out,str,strlen(str));
lua_pop(L,1);
}else
{
bs_getModuleVar(L,inst,"#rdir");
addPath(L,-2,-1); // root_build_dir, rdir, root_build_dir+rdir
const char* str = bs_denormalize_path(lua_tostring(L,-1));
concatReplace(L,out,str,strlen(str));
lua_pop(L,3);
}
}
s += offset + len;
}else if( res == BS_NOP )
{
// copy rest of string
len = strlen(s);
concatReplace(L,out,s,len);
s += len;
}else
return res;
}
return BS_OK;
}
static void callLua(lua_State* L, BSVisitorCtx* ctx, int builtins, int inst, int app, int script, const char* source)
{
const int top = lua_gettop(L);
if( ctx->d_begin )
ctx->d_begin(BS_RunLua, bs_denormalize_path(lua_tostring(L,app)), BS_notc, BS_noos, ctx->d_data);
if( ctx->d_param )
{
lua_getfield(L,inst,"args");
const int arglist = lua_gettop(L);