-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbsrunner.c
2422 lines (2129 loc) · 73.7 KB
/
bsrunner.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 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 "bsrunner.h"
#include "bshost.h"
#include "bsparser.h"
#include "lauxlib.h"
#include <assert.h>
#include <string.h>
// TODO: reimplement bsrunner using bsvisitor; BS_ALT_RUNCMD no longer needed
#ifdef BS_ALT_RUNCMD
void bs_preset_runcmd(lua_State *L, BSRunCmd cmd, void* data)
{
if( cmd == 0 )
lua_pushnil(L);
else
lua_pushlightuserdata(L, cmd);
lua_setglobal(L,"#runcmd");
if( cmd == 0 )
lua_pushnil(L);
else
lua_pushlightuserdata(L, data);
lua_setglobal(L,"#runcmddata");
}
#endif
static int runcmd(lua_State* L, const char* cmd)
{
#ifdef BS_ALT_RUNCMD
lua_getglobal(L,"#runcmd");
if( lua_islightuserdata(L,-1) )
{
BSRunCmd f = (BSRunCmd)lua_topointer(L,-1);
void* data = 0;
lua_getglobal(L,"#runcmddata");
if( lua_islightuserdata(L,-1) )
data = (void*)lua_topointer(L,-1);
const int res = f(cmd,data);
lua_pop(L,2);
return res;
}else
{
lua_pop(L,1);
return bs_exec(cmd);
}
#else
return bs_exec(cmd);
#endif
}
static int copycmd(lua_State* L, const char* normalizedToPath, const char* normalizedFromPath)
{
#ifdef BS_ALT_RUNCMD
const char* to = bs_denormalize_path(normalizedToPath);
const char* from = bs_denormalize_path(normalizedFromPath);
const int len = 4 + 1 + 2 + strlen(from) + 1 + 2 + strlen(to) + 1;
sprintf( (char*)bs_global_buffer(), "copy \"%s\" \"%s\"", from, to );
return runcmd(L, bs_global_buffer());
#else
return bs_copy(normalizedToPath,normalizedFromPath);
#endif
}
int bs_declpath(lua_State* L, int decl, const char* separator)
{
const int top = lua_gettop(L);
if( decl < 0 )
decl += top + 1;
lua_getfield(L,decl,"#name");
const int name = lua_gettop(L);
lua_getfield(L,decl,"#owner");
const int module = lua_gettop(L);
while(!lua_isnil(L,module))
{
lua_getfield(L,module,"#name");
if( lua_isnil(L,-1) )
{
lua_pop(L,1);
break;
}
lua_pushstring(L,separator);
lua_pushvalue(L,name);
lua_concat(L,3);
lua_replace(L,name);
lua_getfield(L,module,"#owner");
lua_replace(L,module);
}
lua_pop(L,1); // module
assert( top + 1 == lua_gettop(L) );
return 1;
}
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;
}
int bs_guessLang(const char* name)
{
const int len = strlen(name);
const char* p = name + len - 1;
while( p != name && *p != '.' )
p--;
// see https://gcc.gnu.org/onlinedocs/gcc-4.7.2/gcc/Overall-Options.html
if( strcmp(p,".c") == 0 )
return BS_c;
if( strcmp(p,".h") == 0 )
return BS_header;
if( strcmp(p,".cc") == 0 )
return BS_cc;
if( strcmp(p,".hh") == 0 )
return BS_header;
#ifndef _WIN32
if( strcmp(p,".C") == 0 )
return BS_cc;
if( strcmp(p,".H") == 0 )
return BS_header;
if( strcmp(p,".HPP") == 0 )
return BS_header;
if( strcmp(p,".M") == 0 )
return BS_objcc;
#endif
if( strcmp(p,".cpp") == 0 )
return BS_cc;
if( strcmp(p,".hpp") == 0 )
return BS_header;
if( strcmp(p,".c++") == 0 )
return BS_cc;
if( strcmp(p,".h++") == 0 )
return BS_header;
if( strcmp(p,".cp") == 0 )
return BS_cc;
if( strcmp(p,".hp") == 0 )
return BS_header;
if( strcmp(p,".cxx") == 0 )
return BS_cc;
if( strcmp(p,".hxx") == 0 )
return BS_header;
if( strcmp(p,".m") == 0 )
return BS_objc;
if( strcmp(p,".mm") == 0 )
return BS_objcc;
return BS_unknownLang;
}
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) );
}
int bs_getModuleVar(lua_State* L, int inst, const char* name )
{
const int top = lua_gettop(L);
lua_getfield(L,inst,"#decl");
if( lua_isnil(L,-1) )
return 1; // apparently a predeclared global object
lua_getfield(L,-1,"#owner");
lua_replace(L,-2);
lua_getfield(L,-1,name);
lua_replace(L,-2);
const int bottom = lua_gettop(L);
assert( top + 1 == bottom );
return 1;
}
static void addall(lua_State* L,int inst,int cflags, int cflags_c, int cflags_cc, int cflags_objc, int cflags_objcc,
int defines, int includes, int ismsvc)
{
// TODO: avoid duplicates
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
addall(L,config,cflags,cflags_c,cflags_cc,cflags_objc,cflags_objcc,defines,includes,ismsvc);
lua_pop(L,1); // config
}
lua_pop(L,1); // configs
lua_getfield(L,inst,"cflags");
addflags(L,-1,cflags);
lua_pop(L,1);
lua_getfield(L,inst,"cflags_c");
addflags(L,-1,cflags_c);
lua_pop(L,1);
lua_getfield(L,inst,"cflags_cc");
addflags(L,-1,cflags_cc);
lua_pop(L,1);
lua_getfield(L,inst,"cflags_objc");
addflags(L,-1,cflags_objc);
lua_pop(L,1);
lua_getfield(L,inst,"cflags_objcc");
addflags(L,-1,cflags_objcc);
lua_pop(L,1);
bs_getModuleVar(L,inst,"#dir");
const int absDir = lua_gettop(L);
lua_getfield(L,inst,"include_dirs");
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);
}
lua_pushvalue(L,includes);
lua_pushfstring(L," -I\"%s\" ", bs_denormalize_path(lua_tostring(L,path)) );
lua_concat(L,2);
lua_replace(L,includes);
lua_pop(L,1); // path
}
lua_pop(L,2); // absDir, incls
lua_getfield(L,inst,"defines");
const int defs = lua_gettop(L);
for( i = 1; i <= lua_objlen(L,defs); i++ )
{
lua_rawgeti(L,defs,i);
lua_pushvalue(L,defines);
if( strstr(lua_tostring(L,-2),"\\\"") != NULL )
lua_pushfstring(L," \"-D%s\" ", lua_tostring(L,-2)); // strings can potentially include whitespace, thus quotes
else
lua_pushfstring(L," -D%s ", lua_tostring(L,-2));
lua_concat(L,2);
lua_replace(L,defines);
lua_pop(L,1); // def
}
lua_pop(L,1); // defs
}
BSToolchain bs_getToolchain(lua_State* L, int builtinsInst, int to_host)
{
if( to_host )
lua_getfield(L,builtinsInst,"host_toolchain");
else
lua_getfield(L,builtinsInst,"target_toolchain");
BSToolchain toolchain;
if( strcmp(lua_tostring(L,-1),"msvc") == 0 )
toolchain = BS_msvc;
else if( strcmp(lua_tostring(L,-1),"gcc") == 0 )
toolchain = BS_gcc;
else if( strcmp(lua_tostring(L,-1),"clang") == 0 )
toolchain = BS_clang;
else
luaL_error(L,"toolchain not supported: %s",lua_tostring(L,-1) );
lua_pop(L,1);
return toolchain;
}
BSOperatingSystem bs_getOperatingSystem(lua_State* L, int builtinsInst, int to_host)
{
if( to_host )
lua_getfield(L,builtinsInst,"host_os");
else
lua_getfield(L,builtinsInst,"target_os");
BSOperatingSystem toolchain;
if( strcmp(lua_tostring(L,-1),"linux") == 0 )
toolchain = BS_linux;
else if( strcmp(lua_tostring(L,-1),"macos") == 0 || strcmp(lua_tostring(L,-1),"darwin") == 0 )
toolchain = BS_mac;
else if( strcmp(lua_tostring(L,-1),"win32") == 0 || strcmp(lua_tostring(L,-1),"winrt") == 0 )
toolchain = BS_windows;
else
luaL_error(L,"operating system not supported: %s",lua_tostring(L,-1) );
lua_pop(L,1);
return toolchain;
}
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 )
{
#if defined(_WIN32) && !defined(BS_ALT_RUNCMD)
lua_pushfstring(L,"%s/%s",
#else
lua_pushfstring(L,"\"%s/%s\"",
#endif
bs_denormalize_path(lua_tostring(L,-1)), lua_tostring(L,cmd) );
lua_replace(L,cmd); // prefix cmd with path and enclose in ""
lua_pop(L,1);
}else
lua_pop(L,1);
}
static void compilesources(lua_State* L, int inst, 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,inst,"#out");
lua_getfield(L,builtins,"#inst");
const int binst = lua_gettop(L);
lua_getfield(L,inst,"to_host");
const int to_host = lua_toboolean(L,-1);
lua_pop(L,1);
const int 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,inst,"#dir");
const int absDir = lua_gettop(L);
bs_getModuleVar(L,inst,"#rdir");
const int relDir = lua_gettop(L);
lua_pushstring(L,"");
const int cflags = lua_gettop(L);
lua_pushstring(L,"");
const int cflags_c = lua_gettop(L);
lua_pushstring(L,"");
const int cflags_cc = lua_gettop(L);
lua_pushstring(L,"");
const int cflags_objc = lua_gettop(L);
lua_pushstring(L,"");
const int cflags_objcc = lua_gettop(L);
lua_pushstring(L,"");
const int defines = lua_gettop(L);
lua_pushstring(L,"");
const int includes = lua_gettop(L);
if( !lua_isnil(L,ctdefaults) )
addall(L,ctdefaults,cflags,cflags_c,cflags_cc,cflags_objc,cflags_objcc,defines,includes,toolchain == BS_msvc);
// TODO: fix order according to specs
addall(L,inst,cflags,cflags_c,cflags_cc,cflags_objc,cflags_objcc,defines,includes,toolchain == BS_msvc);
size_t i;
lua_getfield(L,inst,"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);
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);
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 0
FILE* tmp = fopen("files.txt","a");
fwrite(lua_tostring(L,file),1,lua_objlen(L,file),tmp);
fwrite("\n",1,1,tmp);
fclose(tmp);
#endif
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,inst,"#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);
const time_t srcExists = bs_exists(lua_tostring(L,src));
const time_t outExists = bs_exists(lua_tostring(L,out));
// check if out is older than source; this is just to avoid total recompile in case of an error,
// not for development
if( !outExists || outExists < srcExists )
{
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;
}
const int cmd = lua_gettop(L);
prefixCmd(L, cmd, binst, to_host);
lua_pushstring(L," ");
lua_concat(L,2); // append " " to cmd
lua_pushvalue(L,cmd);
lua_pushvalue(L,cflags);
switch(lang)
{
case BS_c:
lua_pushvalue(L,cflags_c);
break;
case BS_cc:
lua_pushvalue(L,cflags_cc);
break;
case BS_objc:
lua_pushvalue(L,cflags_objc);
break;
case BS_objcc:
lua_pushvalue(L,cflags_objcc);
break;
default:
lua_pushstring(L,"");
break;
}
lua_pushvalue(L,defines);
lua_pushvalue(L,includes);
switch(toolchain)
{
case BS_gcc:
case BS_clang:
lua_pushstring(L," -c -o ");
lua_pushfstring(L,"\"%s\" ", bs_denormalize_path(lua_tostring(L,out) ) );
lua_pushfstring(L,"\"%s\" ", bs_denormalize_path(lua_tostring(L,src) ) );
break;
case BS_msvc:
lua_pushstring(L," /nologo /c /Fo");
lua_pushfstring(L,"\"%s\" ", bs_denormalize_path(lua_tostring(L,out) ) );
lua_pushfstring(L,"\"%s\" ", bs_denormalize_path(lua_tostring(L,src) ) );
break;
default:
lua_pushstring(L,"");
break;
}
lua_concat(L,8);
lua_replace(L,cmd);
fprintf(stdout,"%s\n", lua_tostring(L,cmd));
fflush(stdout);
if( runcmd(L,lua_tostring(L,cmd)) != 0 ) // works for all gcc, clang and cl
{
// stderr was already written to the console
lua_pushnil(L);
lua_error(L);
}
lua_pop(L,1); // cmd
}
lua_pop(L,3); // file, source, dest
}
lua_pop(L,1); // sources
lua_pop(L,13); // outlist, binst, ctdefaults, rootOutDir...relDir, cflags...includes
const int bottom = lua_gettop(L);
assert( top == bottom );
}
static void addall2(lua_State* L,int inst,int ldflags, int lib_dirs, int lib_names,
int lib_files, int frameworks, int ismsvc, int ismac, int iswin)
{
const int top = lua_gettop(L);
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 conf = lua_gettop(L);
// TODO: check for circular deps
addall2(L,conf,ldflags,lib_dirs,lib_names,lib_files,frameworks, ismsvc, ismac, iswin);
lua_pop(L,1);
}
lua_pop(L,1); // configs
lua_getfield(L,inst,"ldflags");
addflags(L,-1,ldflags);
lua_pop(L,1);
bs_getModuleVar(L,inst,"#dir");
const int absDir = lua_gettop(L);
lua_getfield(L,inst,"lib_dirs");
const int ldirs = lua_gettop(L);
for( i = 1; i <= lua_objlen(L,ldirs); i++ )
{
lua_rawgeti(L,ldirs,i);
const int path = lua_gettop(L);
if( *lua_tostring(L,-1) != '/' )
{
// relative path
addPath(L,absDir,path);
lua_replace(L,path);
}
lua_pushvalue(L,lib_dirs);
if(ismsvc)
lua_pushfstring(L," /libpath:\"%s\" ", bs_denormalize_path(lua_tostring(L,path)) );
else
lua_pushfstring(L," -L\"%s\" ", bs_denormalize_path(lua_tostring(L,path)) );
lua_concat(L,2);
lua_replace(L,lib_dirs);
lua_pop(L,1); // path
}
lua_pop(L,1); // ldirs
lua_getfield(L,inst,"lib_names");
const int lnames = lua_gettop(L);
for( i = 1; i <= lua_objlen(L,lnames); i++ )
{
lua_rawgeti(L,lnames,i);
lua_pushvalue(L,lib_names);
if(ismsvc)
lua_pushfstring(L," %s.lib ", lua_tostring(L,-2));
else
lua_pushfstring(L," -l%s ", lua_tostring(L,-2));
lua_concat(L,2);
lua_replace(L,lib_names);
lua_pop(L,1); // name
}
lua_pop(L,1); // lnames
if(ismac)
{
lua_getfield(L,inst,"frameworks");
const int fw = lua_gettop(L);
for( i = 1; i <= lua_objlen(L,lnames); i++ )
{
lua_rawgeti(L,fw,i);
lua_pushvalue(L,frameworks);
lua_pushfstring(L," -framework %s ", lua_tostring(L,-2));
lua_concat(L,2);
lua_replace(L,frameworks);
lua_pop(L,1); // name
}
lua_pop(L,1); // fw
}
if( iswin )
{
lua_getfield(L,inst,"def_file");
const int def_file = lua_gettop(L);
if( !lua_isnil(L,-1) && strcmp(lua_tostring(L,-1),".") != 0 )
{
lua_pushvalue(L,ldflags);
lua_pushstring(L," ");
if( *lua_tostring(L,def_file) != '/' )
{
// relative path
addPath(L,absDir,def_file);
lua_replace(L,def_file);
}
if(ismsvc)
lua_pushfstring(L," /def:\"%s\" ", bs_denormalize_path(lua_tostring(L,def_file)) );
else
lua_pushfstring(L," \"%s\" ", bs_denormalize_path(lua_tostring(L,def_file)) );
lua_concat(L,3);
lua_replace(L,ldflags);
}
lua_pop(L,1);// def_file
}
// TODO: lib_files
lua_pop(L,1); // absDir
const int bottom = lua_gettop(L);
assert( top == bottom );
}
static time_t renderobjectfiles(lua_State* L, int list, FILE* out, int buf, int toolchain, int resKind)
{
// 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
time_t srcExists = 0;
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) );
const time_t exists = renderobjectfiles(L,sublist,out, buf, toolchain, resKind);
if( exists > srcExists )
srcExists = exists;
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);
const time_t exists = bs_exists(lua_tostring(L,path));
if( exists > srcExists )
srcExists = exists;
if( buf )
{
lua_pushvalue(L,buf);
lua_pushfstring(L,"\"%s\" ", bs_denormalize_path(lua_tostring(L,path)) );
lua_concat(L,2);
lua_replace(L,buf);
}else
fprintf(out,"\"%s\" ", bs_denormalize_path(lua_tostring(L,path)) );
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
}
const time_t exists = bs_exists(lua_tostring(L,path));
if( exists > srcExists )
srcExists = exists;
if( buf )
{
lua_pushvalue(L,buf);
lua_pushfstring(L,"\"%s\" ", bs_denormalize_path(lua_tostring(L,path)) );
lua_concat(L,2);
lua_replace(L,buf);
}else
fprintf(out,"\"%s\" ", bs_denormalize_path(lua_tostring(L,path)) );
lua_pop(L,1); // path
}
break;
default:
// ignore
break;
}
return srcExists;
}
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, int inst, 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,inst,"to_host");
const int to_host = lua_toboolean(L,-1);
lua_pop(L,1);
const int toolchain = bs_getToolchain(L,binst,to_host);
if( to_host )
lua_getfield(L,binst,"host_os");
else
lua_getfield(L,binst,"target_os");
const int win32 = strcmp(lua_tostring(L,-1),"win32") == 0 || strcmp(lua_tostring(L,-1),"winrt") == 0;
const int mac = strcmp(lua_tostring(L,-1),"darwin") == 0 || strcmp(lua_tostring(L,-1),"macos") == 0;
lua_pop(L,1);
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,inst,"#rdir");
const int relDir = lua_gettop(L);
lua_pushstring(L,"");
const int ldflags = lua_gettop(L);
lua_pushstring(L,"");
const int lib_dirs = lua_gettop(L);
lua_pushstring(L,"");
const int lib_names = lua_gettop(L);
lua_pushstring(L,"");
const int lib_files = lua_gettop(L);
lua_pushstring(L,"");
const int frameworks = lua_gettop(L);
if( !lua_isnil(L,ctdefaults) )
addall2(L,ctdefaults,ldflags,lib_dirs,lib_names,lib_files,frameworks,
toolchain == BS_msvc || (win32 && toolchain == BS_clang), mac, win32);
addall2(L,inst,ldflags,lib_dirs,lib_names,lib_files,frameworks,
toolchain == BS_msvc || (win32 && toolchain == BS_clang), mac, win32);
// clang on windows uses the lib.exe compatible llvm-lib.exe tool
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,inst,"name");
if( lua_isnil(L,-1) || lua_objlen(L,-1) == 0 )
{
lua_pop(L,1);
lua_getfield(L,inst,"#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,inst,"#product");
const time_t outExists = bs_exists(lua_tostring(L,outfile));
lua_pushvalue(L,outbase);
lua_pushstring(L,".rsp");
lua_concat(L,2);
const int rsp = lua_gettop(L);
int useRsp = 1;
switch(toolchain)
{
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;