-
Notifications
You must be signed in to change notification settings - Fork 130
/
fbc.bas
4408 lines (3745 loc) · 129 KB
/
fbc.bas
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
'' main module, front-end
''
'' chng: sep/2004 written [v1ctor]
'' dec/2004 linux support added [lillo]
'' jan/2005 dos support added [DrV]
#include once "fb.bi"
#include once "hlp.bi"
#include once "hash.bi"
#include once "list.bi"
#include once "objinfo.bi"
#include once "file.bi"
#if defined( ENABLE_STANDALONE ) and defined( __FB_WIN32__ )
#define ENABLE_GORC
#endif
enum
PRINT_HOST
PRINT_TARGET
PRINT_X
PRINT_FBLIBDIR
PRINT_SHA1
end enum
type FBC_EXTOPT
gas as string
ld as string
gcc as string
end type
type FBCIOFILE
'' Input file name (usually *.bas, but also *.rc, *.res, *.xpm)
srcfile as string '' input file
'' Output .o file
'' - for modules from the command line this points to a node from
'' fbc.objlist, see also fbcAddObj()
'' - for example in hCompileFbctinf(), add temporary FBCIOFILE is used,
'' with objfile pointing to a string var on stack
objfile as string ptr
'' Whether -o was used to override the default .o file name
is_custom_objfile as integer
end type
type FBC_OBJINF
lang as FB_LANG
mt as integer
end type
type FBCCTX
'' For command line parsing
optid as integer '' Current option
lastmodule as FBCIOFILE ptr '' module for last input file, so the default .o name can be overwritten with a following -o filename
objfile as string '' -o filename waiting for next input file
backend as integer '' FB_BACKEND_* given via -gen, or -1 if -gen wasn't given
cputype as integer '' FB_CPUTYPE_* (-arch's argument), or -1
cputype_is_native as integer '' Whether -arch native was used
asmsyntax as integer '' FB_ASMSYNTAX_* from -asm, or -1 if not given
emitasmonly as integer '' write out FB backend output file only (.asm/.c)
keepasm as integer '' preserve FB backend output file (.asm/.c)
emitfinalasmonly as integer '' write out final .asm file only
keepfinalasm as integer '' preserve final .asm
keepobj as integer
verbose as integer
showversion as integer
showhelp as integer
print as integer '' PRINT_* (-print option)
'' Command line input
modules as TLIST '' FBCIOFILE's for input .bas files
rcs as TLIST '' FBCIOFILE's for input .rc/.res files
xpm as FBCIOFILE '' .xpm input file
temps as TSTRSET '' Temporary files to delete at shutdown
objlist as TLIST '' Objects from command line and from compilation
libfiles as TLIST
libs as TSTRSET
libpaths as TSTRSET
excludedlibs as TSTRSET '' lib names explicitly excluded via -nodeflib option(s)
'' Final list of libs and paths for linking
'' (each module can have #inclibs and #libpaths and add more, and for
'' objinfo emitting only the module-specific libs are wanted, so there
'' are multiple lists necessary to allow each module to start fresh
'' with the same input libs)
finallibs as TSTRSET
finallibpaths as TSTRSET
outname as zstring * FB_MAXPATHLEN+1
mainname as zstring * FB_MAXPATHLEN+1
entry as zstring * FB_MAXNAMELEN+1
mainset as integer
mapfile as zstring * FB_MAXPATHLEN+1
subsystem as zstring * FB_MAXNAMELEN+1
extopt as FBC_EXTOPT
#ifndef ENABLE_STANDALONE
target as zstring * FB_MAXNAMELEN+1 '' Target system identifier (e.g. a name like "win32", or a GNU triplet) to prefix in front of cross-compiling tool names
targetprefix as zstring * FB_MAXNAMELEN+1 '' same, but with "-" appended, if there was a target id given; otherwise empty.
#endif
xbe_title as zstring * FB_MAXNAMELEN+1 '' For the '-title <title>' xbox option
nodeflibs as integer
staticlink as integer
stripsymbols as integer
'' Compiler paths
prefix as zstring * FB_MAXPATHLEN+1 '' Path from -prefix or empty
binpath as zstring * FB_MAXPATHLEN+1 '' standalone=prefix/bin/target/ normal=prefix/<fbname>/(target|build)prefix
incpath as zstring * FB_MAXPATHLEN+1 '' standalone=prefix/inc normal=prefix/include/<fbname>
libpath as zstring * FB_MAXPATHLEN+1 '' standalone=prefix/lib/target normal=prefix/lib[64]/<fbname>/target
'' Tool prefix
buildprefix as zstring * FB_MAXPATHLEN+1 '' command line option to override target prefix (affects tool names executed)
objinf as FBC_OBJINF
end type
enum FBCTOOL
FBCTOOL_AS = 0
FBCTOOL_AR
FBCTOOL_LD
FBCTOOL_GCC
FBCTOOL_LLC
FBCTOOL_CLANG
FBCTOOL_DLLTOOL
FBCTOOL_GORC
FBCTOOL_WINDRES
FBCTOOL_CXBE
FBCTOOL_DXEGEN
FBCTOOL_EMAS
FBCTOOL_EMAR
FBCTOOL_EMLD
FBCTOOL_EMCC
FBCTOOL__COUNT
end enum
enum FBCTOOLFLAG
FBCTOOLFLAG_INVALID = 0 '' tool is disabled
FBCTOOLFLAG_ASSUME_EXISTS = 1 '' assume the tool exists
FBCTOOLFLAG_CAN_USE_ENVIRON = 2 '' allow path to tool to specified by environment variable
FBCTOOLFLAG_FOUND = 4 '' tool was checked for
FBCTOOLFLAG_RELYING_ON_SYSTEM = 8 '' tool is expected to be on system PATH
FBCTOOLFLAG_DEFAULT = FBCTOOLFLAG_ASSUME_EXISTS or FBCTOOLFLAG_CAN_USE_ENVIRON
end enum
type FBCTOOLINFO
name as zstring * 16
flags as FBCTOOLFLAG
path as zstring * (FB_MAXPATHLEN + 1)
end type
#define fbctoolGetFlags( tool, f ) ((fbctoolTB( tool ).flags and (f)) <> 0)
#define fbctoolSetFlags( tool, f ) fbctoolTB( tool ).flags or= f
#define fbctoolUnsetFlags( tool, f ) fbctoolTB( tool ).flags and= not f
'' must be same order as enum FBCTOOL
static shared as FBCTOOLINFO fbctoolTB(0 to FBCTOOL__COUNT-1) = _
{ _
/' FBCTOOL_AS '/ ( "as" , FBCTOOLFLAG_DEFAULT ), _
/' FBCTOOL_AR '/ ( "ar" , FBCTOOLFLAG_DEFAULT ), _
/' FBCTOOL_LD '/ ( "ld" , FBCTOOLFLAG_DEFAULT ), _
/' FBCTOOL_GCC '/ ( "gcc" , FBCTOOLFLAG_DEFAULT ), _
/' FBCTOOL_LLC '/ ( "llc" , FBCTOOLFLAG_DEFAULT ), _
/' FBCTOOL_CLANG '/ ( "clang" , FBCTOOLFLAG_DEFAULT ), _
/' FBCTOOL_DLLTOOL '/ ( "dlltool", FBCTOOLFLAG_DEFAULT ), _
/' FBCTOOL_GORC '/ ( "GoRC" , FBCTOOLFLAG_DEFAULT ), _
/' FBCTOOL_WINDRES '/ ( "windres", FBCTOOLFLAG_DEFAULT ), _
/' FBCTOOL_CXBE '/ ( "cxbe" , FBCTOOLFLAG_DEFAULT ), _
/' FBCTOOL_DXEGEN '/ ( "dxe3gen", FBCTOOLFLAG_DEFAULT ), _
/' FBCTOOL_EMAS '/ ( "emcc" , FBCTOOLFLAG_DEFAULT ), _
/' FBCTOOL_EMAR '/ ( "emar" , FBCTOOLFLAG_DEFAULT ), _
/' FBCTOOL_EMLD '/ ( "emcc" , FBCTOOLFLAG_DEFAULT ), _
/' FBCTOOL_EMCC '/ ( "emcc" , FBCTOOLFLAG_DEFAULT ) _
}
declare sub fbcFindBin _
( _
byval tool as integer, _
byref path as string _
)
declare sub hPrintVersion( byval verbose as integer )
#macro safeKill(f)
if( kill( f ) <> 0 ) then
end if
#endmacro
dim shared as FBCCTX fbc
private sub fbcInit( )
const FBC_INITFILES = 64
fbc.backend = -1
fbc.cputype = -1
fbc.asmsyntax = -1
listInit( @fbc.modules, FBC_INITFILES, sizeof(FBCIOFILE) )
listInit( @fbc.rcs, FBC_INITFILES\4, sizeof(FBCIOFILE) )
strsetInit( @fbc.temps, FBC_INITFILES\4 )
strlistInit( @fbc.objlist, FBC_INITFILES )
strlistInit( @fbc.libfiles, FBC_INITFILES\4 )
strsetInit( @fbc.libs, FBC_INITFILES\4 )
strsetInit( @fbc.libpaths, FBC_INITFILES\4 )
strsetInit( @fbc.excludedlibs, FBC_INITFILES\4 )
strsetInit(@fbc.finallibs, FBC_INITFILES\2)
strsetInit(@fbc.finallibpaths, FBC_INITFILES\2)
fbGlobalInit()
#ifdef ENABLE_STRIPALL
fbc.stripsymbols = TRUE
#endif
fbc.objinf.lang = fbGetOption( FB_COMPOPT_LANG )
fbc.print = -1
end sub
private sub hSetOutName( )
'' Determine the output binary/archive's name if not given via -x
if( len( fbc.outname ) > 0 ) then
exit sub
end if
'' Creating a static lib?
if( fbGetOption( FB_COMPOPT_OUTTYPE ) = FB_OUTTYPE_STATICLIB ) then
fbc.outname = hStripFilename( fbc.mainname ) + _
"lib" + hStripPath( fbc.mainname ) + ".a"
exit sub
end if
'' Otherwise, we're creating an .exe or DLL/shared lib
fbc.outname = fbc.mainname
select case( fbGetOption( FB_COMPOPT_OUTTYPE ) )
case FB_OUTTYPE_EXECUTABLE
select case( fbGetOption( FB_COMPOPT_TARGET ) )
case FB_COMPTARGET_DOS, FB_COMPTARGET_CYGWIN, _
FB_COMPTARGET_WIN32, FB_COMPTARGET_XBOX
'' Note: XBox target creates an .exe first,
'' then uses cxbe to turn it into an .xbe later
fbc.outname += ".exe"
case FB_COMPTARGET_JS
fbc.outname += ".html"
end select
case FB_OUTTYPE_DYNAMICLIB
select case( fbGetOption( FB_COMPOPT_TARGET ) )
case FB_COMPTARGET_CYGWIN, FB_COMPTARGET_WIN32
fbc.outname += ".dll"
case FB_COMPTARGET_LINUX, FB_COMPTARGET_DARWIN, _
FB_COMPTARGET_FREEBSD, FB_COMPTARGET_OPENBSD, _
FB_COMPTARGET_NETBSD, FB_COMPTARGET_DRAGONFLY, FB_COMPTARGET_SOLARIS
fbc.outname = hStripFilename( fbc.outname ) + _
"lib" + hStripPath( fbc.outname ) + ".so"
case FB_COMPTARGET_DOS
fbc.outname += ".dxe"
end select
end select
end sub
private sub fbcEnd( byval errnum as integer )
'' Clean up temporary files
dim as TSTRSETITEM ptr file = listGetHead(@fbc.temps.list)
while( file )
safeKill( file->s )
file = listGetNext( file )
wend
end errnum
end sub
private sub fbcAddTemp(byref file as string)
strsetAdd(@fbc.temps, file, 0)
end sub
private sub fbcRemoveTemp(byref file as string)
strsetDel(@fbc.temps, file)
end sub
private function fbcAddObj( byref file as string ) as string ptr
'' .o's should be linked/archived in the order they were found on
'' command line, so callers of this function must take care to preserve
'' the order...
dim as string ptr s = listNewNode( @fbc.objlist )
*s = file
function = s
end function
private function hGet1stOutputLineFromCommand( byref cmd as string ) as string
var f = freefile( )
if( open pipe( cmd, for input, as f ) <> 0 ) then
exit function
end if
dim ln as string
input #f, ln
close f
return ln
end function
'' Pass some arguments to gcc and read the results. Returns an empty string on
'' an error.
private function fbcQueryGcc( byref options as string ) as string
dim as string path
fbcFindBin( FBCTOOL_GCC, path )
select case( fbGetCpuFamily( ) )
case FB_CPUFAMILY_X86
path += " -m32"
case FB_CPUFAMILY_X86_64
path += " -m64"
case FB_CPUFAMILY_PPC
path += " -m32"
case FB_CPUFAMILY_PPC64, FB_CPUFAMILY_PPC64LE
path += " -m64"
end select
path += options
dim as integer ff = freefile( )
if( open pipe( path, for input, as ff ) <> 0 ) then
exit function
end if
dim ret as string
input #ff, ret
close ff
return ret
end function
''
'' Build the path to a certain file in our lib/ directory (or, in case of
'' non-standalone, somewhere in a system directory such as /usr/lib).
''
'' standalone: Will always return the path to lib/<target>/<file>, no matter
'' whether it exists or not - because that's where it should be.
'' This way the "file not found" errors will be prettier.
''
'' normal: Will check lib/ and query gcc if not found. Querying gcc may fail,
'' because of that an empty string may be returned.
''
private function fbcBuildPathToLibFile( byval file as zstring ptr ) as string
dim as string found
''
'' The Standalone build expects to have all needed files in its lib/,
'' so it needs to do nothing but build up the path and use that.
''
'' Normal however wants to use the "system's" files (and only has few
'' files in its own lib/).
''
'' Typically libgcc.a, crtbegin.o, crtend.o will be inside
'' gcc's sub-directory in lib/gcc/target/version, i.e. Normal can only
'' find them via 'gcc -print-file-name=foo' (except for hard-coding
'' against a specific gcc target/version, but that's not a good option).
''
found = fbc.libpath + FB_HOST_PATHDIV + *file
#ifndef ENABLE_STANDALONE
'' Does it exist in our lib/?
if( hFileExists( found ) ) then
'' Overrides anything else
return found
end if
'' Not found in our lib/, query the target-specific gcc
dim as string path
fbcFindBin( FBCTOOL_GCC, path )
select case( fbGetCpuFamily( ) )
case FB_CPUFAMILY_X86
path += " -m32"
case FB_CPUFAMILY_X86_64
path += " -m64"
case FB_CPUFAMILY_PPC
path += " -m32"
case FB_CPUFAMILY_PPC64, FB_CPUFAMILY_PPC64LE
path += " -m64"
end select
path += " -print-file-name=" + *file
found = hGet1stOutputLineFromCommand( path )
if( len( found ) = 0 ) then
exit function
end if
if( found = hStripPath( found ) ) then
exit function
end if
#endif
function = found
end function
'' Retrieve the path to a library file, or an empty string if it can't be found.
private function fbcFindLibFile( byval file as zstring ptr ) as string
dim as string found
found = fbcBuildPathToLibFile( file )
if( len( found ) > 0 ) then
if( hFileExists( found ) = FALSE ) then
found = ""
end if
end if
function = found
end function
private sub fbcAddDefLibPath(byref path as string)
strsetAdd(@fbc.finallibpaths, path, TRUE)
end sub
#ifndef ENABLE_STANDALONE
private sub fbcAddLibPathFor( byval libname as zstring ptr )
dim as string path
path = hStripFilename( fbcBuildPathToLibFile( libname ) )
path = pathStripDiv( path )
if( len( path ) > 0 ) then
fbcAddDefLibPath( path )
end if
end sub
#endif
private sub fbcFindBin _
( _
byval tool as integer, _
byref path as string _
)
'' Re-use path from last time if possible
if( fbctoolGetFlags( tool, FBCTOOLFLAG_FOUND ) ) then
path = fbctoolTB( tool ).path
exit sub
end if
fbctoolUnsetFlags( tool, FBCTOOLFLAG_RELYING_ON_SYSTEM )
'' a) Use the path from the corresponding environment variable if it's set
if( (fbctoolTB(tool).flags and FBCTOOLFLAG_CAN_USE_ENVIRON) <> 0 ) then
path = environ( ucase( fbctoolTB(tool).name ) )
end if
if( len( path ) = 0 ) then
'' b) Try bin/ directory
#ifndef ENABLE_STANDALONE
'' normal build, the build/target prefix is already appended to binpath
path = fbc.binpath + fbctoolTB(tool).name + FB_HOST_EXEEXT
#else
'' standalone build, we need to use insert it here
path = fbc.binpath + fbc.buildprefix + fbctoolTB(tool).name + FB_HOST_EXEEXT
#endif
#ifndef ENABLE_STANDALONE
if( hFileExists( path ) = FALSE ) then
select case fbGetOption( FB_COMPOPT_BACKEND )
case FB_BACKEND_GCC
'' c) Ask GCC where it is, if applicable (GCC might have its
'' own copy which we must use instead of the system one)
if( tool = FBCTOOL_AS ) then
path = fbcQueryGcc( " -print-prog-name=as" )
elseif( tool = FBCTOOL_LD ) then
path = fbcQueryGcc( " -print-prog-name=ld" )
end if
case FB_BACKEND_GAS, FB_BACKEND_GAS64
#if defined( __FB_FREEBSD__ )
'' gas backend? and we are looking for the linker? and we're hosted freebsd?
'' switch to ld.bfd instead...
if( tool = FBCTOOL_LD ) then
fbctoolTB(tool).name = "ld.bfd"
path = fbc.binpath + fbctoolTB(tool).name + FB_HOST_EXEEXT
end if
#endif
end select
end if
if( hFileExists( path ) = FALSE ) then
'' d) Rely on PATH
if( fbGetOption( FB_COMPOPT_TARGET ) <> FB_COMPTARGET_JS ) then
if( len(fbc.buildprefix) > 0 ) then
path = fbc.buildprefix + fbctoolTB(tool).name + FB_HOST_EXEEXT
else
path = fbc.targetprefix + fbctoolTB(tool).name + FB_HOST_EXEEXT
end if
else
path = fbctoolTB(tool).name
end if
fbctoolSetFlags( tool, FBCTOOLFLAG_RELYING_ON_SYSTEM )
end if
#endif
end if
fbctoolTB( tool ).path = path
fbctoolSetFlags( tool, FBCTOOLFLAG_FOUND )
end sub
private function fbcRunBin _
( _
byval action as zstring ptr, _
byval tool as integer, _
byref ln as string _
) as integer
dim as integer result = any
dim as string path
fbcFindBin( tool, path )
if( fbc.verbose ) then
print *action + ": ", path + " " + ln
end if
'' Always use exec() on Unix or for standalone because
'' - Unix exec() already searches the PATH, so shell() isn't needed,
'' - standalone doesn't use system-wide tools
#if defined( __FB_UNIX__ ) or defined( ENABLE_STANDALONE )
result = exec( path, ln )
#else
'' Found at bin/?
if( fbctoolGetFlags( tool, FBCTOOLFLAG_RELYING_ON_SYSTEM ) = FALSE ) then
result = exec( path, ln )
else
result = shell( path + " " + ln )
end if
#endif
if( result = 0 ) then
function = TRUE
elseif( result < 0 ) then
errReportEx( FB_ERRMSG_EXEMISSING, path, -1, FB_ERRMSGOPT_ADDCOLON or FB_ERRMSGOPT_ADDQUOTES )
else
'' Report bad exit codes only in verbose mode; normally the
'' program should already have shown an error message, and the
'' exit code is only interesting for debugging purposes.
if( fbc.verbose ) then
print *action + " failed: '" + path + "' terminated with exit code " + str( result )
end if
end if
end function
#if defined( __FB_WIN32__ ) or defined( __FB_DOS__ )
private function hPutLdArgsIntoFile( byref ldcline as string ) as integer
dim as string argsfile, ln
dim as integer f = any
argsfile = hStripFilename( fbc.outname ) + "ldopt.tmp"
f = freefile( )
if( open( argsfile, for output, as #f ) ) then
exit function
end if
''
'' MinGW ld (including the MinGW-to-DJGPP cross-compiling ld) treats \
'' backslashes in @files (response files) as escape sequence, so \ must
'' be escaped as \\. ld seems to behave pretty much like Unixish shells
'' would: all \'s indicate an escape sequence. (For reference,
'' binutils/libiberty source code: expandargv(), buildargv())
''
'' With DJGPP ld however, \ chars do not seem to indicate escape
'' sequences, despite the DJGPP FAQ (http://www.delorie.com/djgpp/v2faq/faq16_3.html)
'' which says that \ is special in some cases such as \" or \\.
'' Thus we mustn't (and don't need to) use \\ when using DJGPP ld.
''
ln = ldcline
#ifdef __FB_WIN32__
ln = hReplace( ln, $"\", $"\\" )
#endif
print #f, ln
close #f
'' Clean up the @file if -R wasn't given
if( fbc.keepasm = FALSE ) then
fbcAddTemp( argsfile )
end if
if( fbc.verbose ) then
print "ld options in '" & argsfile & "': ", ldcline
end if
ldcline = "@" + argsfile
function = TRUE
end function
#endif
private function clearDefList(byref deffile as string) as integer
dim as integer fi = freefile()
if (open(deffile, for input, as #fi)) then
return FALSE
end if
dim as string cleaned = hStripExt(deffile) + ".clean.def"
dim as integer fo = freefile()
if (open(cleaned, for output, as #fo)) then
close #fi
return FALSE
end if
dim as string ln
while (eof(fi) = FALSE)
line input #fi, ln
if (right(ln, 4) = "DATA") then
ln = left(ln, len(ln) - 4)
end if
print #fo, ln
wend
close #fo
close #fi
kill(deffile)
return (name(cleaned, deffile) = 0)
end function
private function hGenerateEmptyDefFile( byref deffile as string ) as integer
var f = freefile( )
if( open( deffile, for output, as #f ) ) then
exit function
end if
print #f, "EXPORTS"
close #f
function = TRUE
end function
private function makeImpLib _
( _
byref dllname as string, _
byref deffile as string _
) as integer
'' for some weird reason, LD will declare all functions exported as if they were
'' from DATA segment, causing an exception (UPPERCASE'd symbols assumption??)
if( clearDefList( deffile ) = FALSE ) then
exit function
end if
'' If the .def file is empty (happens if there were no EXPORTs),
'' then add a single "EXPORTS" line, otherwise dlltool will complain
'' about a syntax error. (ld --output-def should probably do this
'' automatically, or dlltool should be fixed, but oh well)
if( filelen( deffile ) = 0 ) then
if( hGenerateEmptyDefFile( deffile ) = FALSE ) then
exit function
end if
end if
dim as string ln
ln += "--def """ + deffile + """"
ln += " --dllname """ + hStripPath( fbc.outname ) + """"
ln += " --output-lib """ + hStripFilename( fbc.outname ) + "lib" + dllname + ".dll.a"""
if( fbcRunBin( "creating import library", FBCTOOL_DLLTOOL, ln ) = FALSE ) then
exit function
end if
'' Clean up the .def file if -R wasn't given
if( fbc.keepasm = FALSE ) then
fbcAddTemp( deffile )
end if
function = TRUE
end function
'' Find a library file and wrap it into ""'s for passing it on the ld command
'' line. Or if it couldn't be found, show an error.
private function hFindLib( byval file as zstring ptr ) as string
dim as string found = fbcBuildPathToLibFile( file )
if( len( found ) > 0 ) then
function = " """ + found + """"
else
errReportEx( FB_ERRMSG_FILENOTFOUND, file, -1 )
end if
end function
private function fbcLinkerIsGold( ) as integer
'' This is needed otherwise it will wrongly pass --version into the linker on Solaris
'' caused the linker version to be printed everytime we compile with fbc
if(fbGetOption( FB_COMPOPT_TARGET ) = FB_COMPTARGET_SOLARIS) then
return FALSE
else
dim ldcmd as string
fbcFindBin( FBCTOOL_LD, ldcmd )
ldcmd += " --version"
return (instr( hGet1stOutputLineFromCommand( ldcmd ), "GNU gold" ) > 0)
end if
end function
'' Check whether we're using the gold linker.
private function fbcIsUsingGoldLinker( ) as integer
'' gold only supports ELF, we only need to check for it when targetting ELF.
if( fbTargetSupportsELF( ) ) then
return fbcLinkerIsGold( )
end if
return FALSE
end function
private function hLinkFiles( ) as integer
dim as string ldcline, dllname, deffile
function = FALSE
hSetOutName( )
select case( fbGetOption( FB_COMPOPT_TARGET ) )
case FB_COMPTARGET_WIN32
select case( fbGetCpuFamily( ) )
case FB_CPUFAMILY_X86
ldcline += "-m i386pe "
case FB_CPUFAMILY_X86_64
ldcline += "-m i386pep "
end select
case FB_COMPTARGET_LINUX
select case( fbGetCpuFamily( ) )
case FB_CPUFAMILY_X86
ldcline += "-m elf_i386 "
case FB_CPUFAMILY_X86_64
ldcline += "-m elf_x86_64 "
case FB_CPUFAMILY_ARM
ldcline += "-m armelf_linux_eabi "
end select
case FB_COMPTARGET_DARWIN
select case( fbGetCpuFamily( ) )
case FB_CPUFAMILY_X86
ldcline += "-arch i386 "
case FB_CPUFAMILY_X86_64
ldcline += "-arch x86_64 "
case FB_CPUFAMILY_ARM
'' fixme: this is clearly too specific
ldcline += "-arch armv6 "
end select
end select
'' Set executable name
ldcline += "-o " + QUOTE + fbc.outname + QUOTE
'' dll dos targets need to run DXE3GEN
if (fbGetOption( FB_COMPOPT_TARGET ) = FB_COMPTARGET_DOS) and _
(fbGetOption( FB_COMPOPT_OUTTYPE ) = FB_OUTTYPE_DYNAMICLIB) then
'' Building an import library is usable but need to watch
'' the initialization order. libfb exports need to be
'' initialized before trying to load the DXE. FYI, if this
'' is handled at load time by the loader code (global
'' constructor) user may have issues with the ctor order.
'' Which means that user is expected to have called
'' defined a call to DyLibLoad("") in a global ctor and
'' no other global ctors that depend on libfb functions
''
ldcline += " -I ""lib" + hStripExt( fbc.outname ) + "_il.a"""
''
'' the -U switch allows the DXE to be created with unresolved
'' symbols. FYI, we won't know which symbols are needed until
'' the loader tries to load the DXE. This seems to be the only
'' kind we can make (i.e. not possible to build a fully resolved DXE
'' because dxe3gen seems to only resolve with in the current DXE and
'' not any statically linked library)
''
ldcline += " -U"
''
scope
dim as string ptr objfile = listGetHead( @fbc.objlist )
while( objfile )
ldcline += " """ + *objfile + """"
objfile = listGetNext( objfile )
wend
end scope
scope
dim as string ptr libfile = listGetHead(@fbc.libfiles)
if (libfile) then
ldcline += " -lc"
end if
while (libfile)
ldcline += " """ + *libfile + """"
libfile = listGetNext(libfile)
wend
end scope
#ifdef __FB_DOS__
'' windows (maybe others) version of DXE3GEN doesn't seem to be able
'' to handle @ldopt.tmp argument when cross compiling - only write to
'' lpopt.tmp if we are hosted on DOS
if( hPutLdArgsIntoFile( ldcline ) = FALSE ) then
exit function
end if
#endif
#ifdef ENABLE_STANDALONE
dim as string dxepath = environ( "DXE_LD_LIBRARY_PATH" )
if( dxepath = "" ) then
setenviron "DXE_LD_LIBRARY_PATH=" + fbc.libpath + FB_HOST_PATHDIV
if( fbc.verbose ) then
print "DXE_LD_LIBRARY_PATH=" + fbc.libpath + FB_HOST_PATHDIV
end if
end if
#endif
function = fbcRunBin( "making DXE", FBCTOOL_DXEGEN, ldcline )
exit function
end if
select case as const fbGetOption( FB_COMPOPT_TARGET )
case FB_COMPTARGET_CYGWIN, FB_COMPTARGET_WIN32
'' set default subsystem mode
if( len( fbc.subsystem ) = 0 ) then
fbc.subsystem = "console"
else
if( fbc.subsystem = "gui" ) then
fbc.subsystem = "windows"
end if
end if
ldcline += " -subsystem " + fbc.subsystem
if( fbGetOption( FB_COMPOPT_OUTTYPE ) = FB_OUTTYPE_DYNAMICLIB ) then
''
dllname = hStripPath( hStripExt( fbc.outname ) )
'' create a dll
ldcline += " --dll --enable-stdcall-fixup"
'' Specify the DLL entry-point, like gcc:
'' with underscore and @N stdcall suffix on x86,
'' but without that for x86_64.
if( fbGetCpuFamily( ) = FB_CPUFAMILY_X86 ) then
ldcline += " -e _DllMainCRTStartup@12"
else
ldcline += " -e DllMainCRTStartup"
end if
end if
case FB_COMPTARGET_LINUX, FB_COMPTARGET_DARWIN, _
FB_COMPTARGET_FREEBSD, FB_COMPTARGET_OPENBSD, _
FB_COMPTARGET_NETBSD, FB_COMPTARGET_DRAGONFLY, FB_COMPTARGET_SOLARIS
if( fbGetOption( FB_COMPOPT_OUTTYPE ) = FB_OUTTYPE_DYNAMICLIB ) then
dllname = hStripPath( hStripExt( fbc.outname ) )
ldcline += " -shared -h" + hStripPath( fbc.outname )
'' Turn libfoo into foo, so it can be checked against -l foo below
if( left( dllname, 3 ) = "lib" ) then
dllname = right( dllname, len( dllname ) - 3 )
end if
else
select case as const fbGetOption( FB_COMPOPT_TARGET )
case FB_COMPTARGET_FREEBSD
ldcline += " -dynamic-linker /libexec/ld-elf.so.1"
case FB_COMPTARGET_DRAGONFLY
ldcline += " -dynamic-linker /libexec/ld-elf.so.2"
case FB_COMPTARGET_SOLARIS
ldcline += " --dynamic-linker /lib/64/ld.so.1"
case FB_COMPTARGET_LINUX
select case( fbGetCpuFamily( ) )
case FB_CPUFAMILY_X86
ldcline += " -dynamic-linker /lib/ld-linux.so.2"
case FB_CPUFAMILY_X86_64
ldcline += " -dynamic-linker /lib64/ld-linux-x86-64.so.2"
case FB_CPUFAMILY_ARM
ldcline += " -dynamic-linker /lib/ld-linux-armhf.so.3"
case FB_CPUFAMILY_AARCH64
ldcline += " -dynamic-linker /lib/ld-linux-aarch64.so.1"
end select
case FB_COMPTARGET_NETBSD
ldcline += " -dynamic-linker /usr/libexec/ld.elf_so"
case FB_COMPTARGET_OPENBSD
ldcline += " -dynamic-linker /usr/libexec/ld.so"
end select
end if
'' Add all symbols to the dynamic symbol table
'' The Solaris linker doesn't support --export-dynamic
'' I don't know the equivalent option for it so I just disable this for Solaris
'' Don't know the side effects, though
'' But able to have shared library generated successfully afterward
if( (fbGetOption( FB_COMPOPT_OUTTYPE ) = FB_OUTTYPE_DYNAMICLIB) or _
fbGetOption( FB_COMPOPT_EXPORT ) ) and _
(fbGetOption( FB_COMPOPT_TARGET ) <> FB_COMPTARGET_SOLARIS) then
ldcline += " --export-dynamic"
end if
case FB_COMPTARGET_XBOX
ldcline += " -nostdlib --file-alignment 0x20 --section-alignment 0x20 -shared"
case FB_COMPTARGET_JS
ldcline += " -O" + str( fbGetOption( FB_COMPOPT_OPTIMIZELEVEL ) )
static as zstring*32 emscripten_options(...) = _
{ _
"CASE_INSENSITIVE_FS=1", _
"TOTAL_MEMORY=67108864", _
"ALLOW_MEMORY_GROWTH=1", _
"RETAIN_COMPILER_SETTINGS=1", _
"ASYNCIFY=1" _
}
'"WARN_UNALIGNED=1", _
ldcline += " -Wno-warn-absolute-paths"
for i as integer = 0 to ubound(emscripten_options)
ldcline += " -s " + emscripten_options(i)
next
ldcline += " --shell-file" + hFindLib("fb_shell.html")
ldcline += " --post-js" + hFindLib("fb_rtlib.js")
if( len(fbc.subsystem) = 0 ) then
ldcline += " --post-js" + hFindLib("termlib_min.js")
end if
end select
if (fbGetOption( FB_COMPOPT_TARGET ) = FB_COMPTARGET_DOS) then
'' For DJGPP, the custom ldscript must always be used,
'' to get ctors/dtors into the correct order that lets
'' fbrt0's c/dtor be the first/last respectively.
'' (needed until binutils' default DJGPP ldscripts are fixed)
ldcline += " -T """ + fbc.libpath + (FB_HOST_PATHDIV + "i386go32.x""")
else
'' Supplementary ld script to drop the fbctinf objinfo section
'' - only if objinfo is enabled
'' - only with ld.bfd, not ld.gold, because gold doesn't support this kind
'' of linker script (results in broken binaries).
'' Solaris uses it own linker
'' - don't know if it support this kind of linker script or not
'' - so just disable it for the solaris target, too
'' - the solaris linker will cause major problems
'' - because of imcompatibilities with the GNU linker
'' - we can only know if when really testing on the platform
'' - and adapt the code over time
if( fbGetOption( FB_COMPOPT_OBJINFO ) and _
(fbGetOption( FB_COMPOPT_TARGET ) <> FB_COMPTARGET_DARWIN) and _
(fbGetOption( FB_COMPOPT_TARGET ) <> FB_COMPTARGET_SOLARIS) and _
( fbGetOption( FB_COMPOPT_TARGET ) <> FB_COMPTARGET_JS ) and _
(not fbcIsUsingGoldLinker( )) ) then
ldcline += " -T """ + fbc.libpath + (FB_HOST_PATHDIV + "fbextra.x""")
end if
end if
select case as const fbGetOption( FB_COMPOPT_TARGET )
case FB_COMPTARGET_CYGWIN, FB_COMPTARGET_WIN32
'' stack size
dim as integer stacksize = fbGetOption(FB_COMPOPT_STACKSIZE)
ldcline += " --stack " + str(stacksize) + "," + str(stacksize)
if( fbGetOption( FB_COMPOPT_OUTTYPE ) = FB_OUTTYPE_DYNAMICLIB ) then
'' When building DLLs, we also create an import library, as
'' convenience for the user. There are several ways to do this:
'' a) ld --output-def + dlltool
'' b) ld --output-def --out-implib
'' c) pexports + dlltool
'' At the moment it seems like "ld --out-implib" alone
'' does not work, and b) shows a verbose message that
'' wouldn't be nice to have, so a) is the best way.
deffile = hStripExt( fbc.outname ) + ".def"
ldcline += " --output-def """ + deffile + """"
end if
case FB_COMPTARGET_XBOX
'' set entry point
ldcline += " -e _WinMainCRTStartup"
end select
if (fbc.staticlink) then
ldcline += " -Bstatic"
end if
if( len( fbc.mapfile ) > 0) then
ldcline += " -Map " + fbc.mapfile
end if
if( fbGetOption( FB_COMPOPT_DEBUGINFO ) = FALSE ) then
if( fbGetOption( FB_COMPOPT_PROFILE ) = FALSE ) then
if(( fbGetOption( FB_COMPOPT_TARGET ) <> FB_COMPTARGET_DARWIN ) and _
( fbGetOption( FB_COMPOPT_TARGET ) <> FB_COMPTARGET_JS )) then
if( fbc.stripsymbols ) then
ldcline += " -s"
end if
end if
end if
end if
'' Add the library search paths
scope
dim as TSTRSETITEM ptr i = listGetHead(@fbc.finallibpaths.list)
dim as string L
if( fbGetOption( FB_COMPOPT_TARGET ) <> FB_COMPTARGET_JS ) then
L = " -L """
else
L = " -L"""