-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
toolchain.fdoc
2460 lines (2162 loc) · 74.8 KB
/
toolchain.fdoc
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
@title C and C++ toolchains
@tangler flx_find_cxx_packages.flx = $PWD/src/tools/flx_find_cxx_packages.flx
@tangler flx_gen_cxx_includes.flx = $PWD/src/tools/flx_gen_cxx_includes.flx
@tangler toolchain_config.flx = share/lib/std/felix/toolchain_config.flx
@tangler toolchain_interface.flx = share/lib/std/felix/toolchain_interface.flx
@tangler flx_cxx.flx = share/lib/std/felix/flx_cxx.flx
@tangler flx_depchk.flx = share/lib/std/felix/flx/flx_depchk.flx
@tangler flx_mklib.flx = share/lib/std/felix/flx_mklib.flx
@tangler gcc_linux.flx = share/lib/std/felix/toolchain/gcc_linux.flx
@tangler gcc_macosx.flx = share/lib/std/felix/toolchain/gcc_macosx.flx
@tangler clang_linux.flx = share/lib/std/felix/toolchain/clang_linux.flx
@tangler clang_macosx.flx = share/lib/std/felix/toolchain/clang_macosx.flx
@tangler clang_iOS_generic.flx = share/lib/std/felix/toolchain/clang_iOS_generic.flx
@tangler msvc_win.flx = share/lib/std/felix/toolchain/msvc_win.flx
@tangler cygwin.fpc = $PWD/src/config/cygwin.fpc
@tangler cygwin.flx = share/lib/std/cygwin/cygwin.flx
@tangler toolchain_gcc_linux.flx = share/lib/plugins/toolchain_gcc_linux.flx
@tangler toolchain_gcc_macosx.flx = share/lib/plugins/toolchain_gcc_macosx.flx
@tangler toolchain_clang_linux.flx = share/lib/plugins/toolchain_clang_linux.flx
@tangler toolchain_clang_macosx.flx = share/lib/plugins/toolchain_clang_macosx.flx
@tangler toolchain_iphoneos.flx = share/lib/plugins/toolchain_iphoneos.flx
@tangler toolchain_iphonesimulator.flx= share/lib/plugins/toolchain_iphonesimulator.flx
@tangler toolchain_msvc_win.flx = share/lib/plugins/toolchain_msvc_win.flx
@tangler flx_plugin.flx = share/lib/plugins/flx_plugin.flx
@tangler build_flx_rtl_gcc_linux.fpc = $PWD/src/config/build_flx_rtl_gcc_linux.fpc
@tangler build_flx_rtl_gcc_macosx.fpc = $PWD/src/config/build_flx_rtl_gcc_macosx.fpc
@tangler build_flx_rtl_clang_linux.fpc = $PWD/src/config/build_flx_rtl_clang_linux.fpc
@tangler build_flx_rtl_clang_macosx.fpc = $PWD/src/config/build_flx_rtl_clang_macosx.fpc
@tangler build_flx_rtl_clang_iphoneos.fpc = $PWD/src/config/build_flx_rtl_clang_iphoneos.fpc
@tangler build_flx_rtl_msvc_win.fpc = $PWD/src/config/build_flx_rtl_msvc_win.fpc
@h1 Find C++ packages.
Doesn't really belong here but had to go somewhere!
Scans a C or C++ files and produces a list of required packages
on standard output. To be redirected to a *.resh file.
Felix compiler generates *.resh file for Felix generated C++,
and puts the #include "filename.includes" into filename.hpp.
For C++ only builds, the user has to put the #include in themselves.
The resulting C++ won't work. This tool makes the filename.resh
file which can then be fed to flx_pkgconfig along with a request
for the include field, to help generate the include file.
@tangle flx_find_cxx_packages.flx
// FIXME: this function cut and paste from flx.fdoc
// hidden inside an object
fun find_cxx_pkgs (src:string) : list[string] =
{
//eprintln$ "[flx_find_cxx_pkgs] Scanning " + src + " for package requirements";
var out = Empty[string];
var pat = RE2('.*@requires package ([A-Za-z][A-Za-z0-9_-]*).*');
var f = fopen_input_text src;
if valid f do
for line in f do
var result = Match (pat,line);
match result do
| #None => ;
| Some v => out = v.1 + out;
done
done
fclose f;
else
eprintln("Can't find C++ source file " + src);
System::exit(1);
done
out = rev out;
//if out != Empty[string] call
// eprintln$ "[flx_find_cxx_packages] C++ file "+src+" requires packages " + str (out);
return out;
}
var filename = System::argv_dflt 1 "";
if filename == "" do
println$ "Usage: flx_find_cxx_packages filename.cxx > filename.resh";
System::exit 1;
done
var pkgs = find_cxx_pkgs filename;
for pkg in pkgs perform println$ pkg;
@
@tangle flx_gen_cxx_includes.flx
include "std/felix/flx_pkgconfig";
var pkgconfig_flags = tail #System::args;
//println$ "Args to flx_gen_cxx_includes=" + pkgconfig_flags.str;
var infile = stdin;
var pkgdata = load infile;
var pkgs = split (pkgdata, "\n");
pkgs = filter (fun (x:string) => x != "") pkgs;
//for pkg in pkgs perform println$ "PKG=" + pkg;
//var err, result = System::get_stdout("flx_pkgconfig " + cat " " pkgconfig_flags + " --field=includes " + cat " " pkgs);
var allargs = pkgconfig_flags + "--field=includes" + pkgs;
//println$ "Calling flx_pkgconfig with args = " + allargs.str;
var err,result = FlxPkgConfig::flx_pkgconfig(allargs);
if err == 0 do
//println$ "Result = " + result;
//var files = split (result," "); // won't work on Windows well ... spaces .. ugg
var files = result;
for file in files perform println$ "#include " + file;
else
eprintln$ "Error " + err.str + " running flx_pkgconfig";
done
@
@h1 Toolchain support
@tangle toolchain_config.flx
typedef toolchain_config_t = (
c_compiler_executable: string,
cxx_compiler_executable: string,
header_search_dirs: list[string],
macros : list[string],
library_search_dirs: list[string],
ccflags: list[string],
dynamic_libraries: list[string],
static_libraries: list[string],
debugln : string -> void
);
@
@tangle toolchain_interface.flx
interface toolchain_t {
whatami : 1 -> string;
host_os : 1 -> string;
target_os : 1 -> string;
cxx_compiler_vendor : 1 -> string;
// Note: this information is available for
// the host platform in the Filename class.
// and for any platform using the Filename_class[os]
// class. The methods below, however, reflect a cross-compilation
// target filesystem. For example, on Linux with shared libs .so
// you can target Windows with shared libs .dll if you have a
// cross compiler.
//
// This toolchain facility should be separated from
// the compiler object, even though the extensions are primarily
// about compiler product file names, because other tools may wish
// to assist building by, for example, deleting all object files.
// Currently you'd have to instantiate a toolchain object to find
// this information, needlessly providing dummy header files,
// macros, etc, which are primarily useful to compilers.
dependency_extension: 1 -> string;
executable_extension : 1 -> string;
static_object_extension: 1 -> string;
dynamic_object_extension: 1 -> string;
static_library_extension: 1 -> string;
dynamic_library_extension: 1 -> string;
pathname_separator : 1 -> string;
get_base_c_compile_flags: 1 -> list[string];
get_base_cxx_compile_flags: 1 -> list[string];
cxx_dependency_generator : (src:string) -> int * string;
c_dependency_generator : (src:string) -> int * string;
dependency_parser : string -> list[string];
cxx_static_object_compiler : (dst:string,src: string) -> int;
cxx_static_library_object_compiler : (dst:string,src: string) -> int;
c_static_object_compiler : (dst:string,src: string) -> int;
static_library_linker : (dst:string,srcs:list[string]) -> int;
static_executable_linker : (dst:string,srcs:list[string]) -> int;
dynamic_executable_linker : (dst:string,srcs:list[string]) -> int;
cxx_dynamic_object_compiler : (dst:string,src: string) -> int;
c_dynamic_object_compiler : (dst:string,src: string) -> int;
dynamic_library_linker : (dst:string,srcs: list[string]) -> int;
debug_flags : 1 -> list[string];
}
@
@h1 Generic C/C++ compiler
For compilers with a gcc like command line interface:
gcc and clang basically.
@tangle flx_cxx.flx
class CxxCompiler
{
typedef cxx_dep_spec_t =
(
CCDEP: string,
CCFLAGS: list[string],
INCLUDE_DIRS: list[string],
MACROS: list[string],
debugln: string -> 0
);
fun mkinc (s:string) => "-I" + s;
fun mkincs (ss:list[string]) => map mkinc ss;
fun mkmac (s:string) => "-D" + s;
fun mkmacs (ss:list[string]) => map mkmac ss;
//---------------------------------------------------------------
// Generating #include dependencies
//---------------------------------------------------------------
gen generic_cxx_gen_deps (spec: cxx_dep_spec_t) (src:string) : int * string =
{
var cmd=
spec.CCDEP !
spec.CCFLAGS +
mkincs spec.INCLUDE_DIRS +
mkmacs spec.MACROS +
src
;
var CMD = catmap ' ' Shell::quote_arg cmd;
spec.debugln$ "C++ generate dependencies : " + CMD;
var result, data = System::get_stdout(CMD);
if result != 0 do
eprintln $ "C++ command="+CMD + " FAILED";
done
return result, data;
}
// parse the "make" file generated by gcc -M
// GIGO: this routine can't fail, but it can return rubbish
gen generic_dependency_parser (data:string) : list[string] =
{
var pcolon = match find (data ,':') with | Some i => i+1uz | #None => 0uz;
var txt = data.[pcolon to];
txt = search_and_replace (txt,'\\\n','');
var files = respectful_split txt;
files = unbox (map Directory::mk_absolute_filename files);
return files;
}
//---------------------------------------------------------------
// Compiling object files for dynamic links
//---------------------------------------------------------------
typedef cxx_dynamic_spec_t =
(
CCOBJ_DLLIB: string,
CCFLAGS: list[string],
INCLUDE_DIRS: list[string],
MACROS: list[string],
SPEC_OBJ_FILENAME:string,
debugln: string -> 0
);
gen generic_cxx_compile_for_dynamic (spec: cxx_dynamic_spec_t) (src:string, dst:string) : int =
{
var cmd=
spec.CCOBJ_DLLIB !
spec.CCFLAGS +
mkincs spec.INCLUDE_DIRS +
mkmacs spec.MACROS +
src
;
var CMD = catmap ' ' Shell::quote_arg cmd + ' ' +
(spec.SPEC_OBJ_FILENAME+Shell::quote_arg dst)
;
spec.debugln$ "C++ compile: " + CMD;
var result = System::system(CMD);
if result != 0 do
eprintln $ "C++ command="+CMD + " FAILED";
done
return result;
}
//---------------------------------------------------------------
// Compiling object files for static links
//---------------------------------------------------------------
typedef cxx_compile_static_t =
(
CCOBJ_STATIC_LIB:string,
CCFLAGS:list[string],
INCLUDE_DIRS:list[string],
MACROS:list[string],
SPEC_OBJ_FILENAME:string,
debugln: string -> 0
);
gen generic_cxx_compile_for_static
(spec:cxx_compile_static_t)
(src:string, dst:string) : int =
{
var cmd=
spec.CCOBJ_STATIC_LIB !
spec.CCFLAGS +
mkincs spec.INCLUDE_DIRS +
mkmacs spec.MACROS +
src
;
var CMD = catmap ' ' Shell::quote_arg cmd + ' ' +
(spec.SPEC_OBJ_FILENAME+Shell::quote_arg dst)
;
spec.debugln$ "C++ command="+CMD;
var result=System::system(CMD);
if result != 0 do
eprintln$ "C++ compilation "+src+" failed";
done
return result;
}
//---------------------------------------------------------------
// Making a shared library or DLL
//---------------------------------------------------------------
typedef link_lib_dynamic_spec_t =
(
CCLINK_DLLIB: string,
CCFLAGS: list[string],
EXT_SHARED_OBJ:string,
SPEC_EXE_FILENAME: string,
LINK_STRINGS: list[string],
debugln: string -> 0
);
gen generic_link_lib_dynamic
(spec:link_lib_dynamic_spec_t)
(cppos: list[string],
LINKER_OUTPUT_FILENAME:string)
: int =
{
var cmd =
spec.CCLINK_DLLIB !
spec.CCFLAGS +
cppos
;
// This weird shit is because Unix use -o filename (space)
// But Windows uses /Fefilename (no space)
var CMD = catmap ' ' Shell::quote_arg cmd + ' ' +
spec.SPEC_EXE_FILENAME+Shell::quote_arg LINKER_OUTPUT_FILENAME+ ' ' +
catmap ' ' Shell::quote_arg spec.LINK_STRINGS
;
spec.debugln$ "Link command="+CMD;
var result = System::system(CMD);
if result != 0 do
eprintln $ "Dynamic link command="+CMD + " FAILED";
done
return result;
}
//---------------------------------------------------------------
// Making a executable which uses shared libraroes
//---------------------------------------------------------------
typedef generic_link_exe_dynamic_t =
(
CCLINK_STATIC: string, // yeah, weird, but it means linker for executables ..
CCFLAGS: list[string],
SPEC_EXE_FILENAME: string,
LINK_STRINGS: list[string],
debugln: string->0
);
gen generic_link_exe_dynamic
(spec:generic_link_exe_dynamic_t)
(cppos:list[string], LINKER_OUTPUT_FILENAME:string) : int =
{
/*
println$ "[generic_link_exe_dynamic] cppos=" + cppos.str;
println$ "[generic_link_exe_dynamic] link strings=" + spec.LINK_STRINGS.str;
*/
var CMD =
Shell::quote_arg spec.CCLINK_STATIC + ' ' +
catmap ' ' Shell::quote_arg spec.CCFLAGS + ' ' +
(spec.SPEC_EXE_FILENAME+Shell::quote_arg(LINKER_OUTPUT_FILENAME)) + ' ' +
catmap ' ' Shell::quote_arg cppos + ' ' +
catmap ' ' Shell::quote_arg spec.LINK_STRINGS
;
spec.debugln$ "Link command="+CMD;
var result=System::system(CMD);
if result != 0 do
eprintln$ "Link command="+CMD+ " FAILED";
done
return result;
}
//---------------------------------------------------------------
// Making a fully linked statically executable
//---------------------------------------------------------------
typedef generic_link_exe_static_t =
(
CCLINK_STATIC: string,
CCFLAGS: list[string],
SPEC_EXE_FILENAME: string,
LINK_STRINGS: list[string],
debugln: string->0
);
gen generic_link_exe_static
(spec:generic_link_exe_static_t)
(cppos:list[string], LINKER_OUTPUT_FILENAME:string) : int =
{
var CMD =
Shell::quote_arg spec.CCLINK_STATIC + ' ' +
catmap ' ' Shell::quote_arg spec.CCFLAGS + ' ' +
(spec.SPEC_EXE_FILENAME+Shell::quote_arg(LINKER_OUTPUT_FILENAME)) + ' ' +
catmap ' ' Shell::quote_arg cppos + ' ' +
catmap ' ' Shell::quote_arg spec.LINK_STRINGS
;
spec.debugln$ "Link command="+CMD;
var result=System::system(CMD);
if result != 0 do
eprintln$ "Link command="+CMD+ " FAILED";
done
return result;
}
//---------------------------------------------------------------
// Making a library archive
//---------------------------------------------------------------
typedef generic_lib_static_t =
(
CCLINK_STATIC_LIB: string,
CCFLAGS : list[string],
SPEC_LIB_FILENAME: string,
debugln: string->0
);
gen generic_static_library
(spec:generic_lib_static_t)
(cppos:list[string], LINKER_OUTPUT_FILENAME:string) : int =
{
var CMD =
Shell::quote_arg(spec.CCLINK_STATIC_LIB) + ' ' +
catmap ' ' Shell::quote_arg spec.CCFLAGS + ' ' +
(spec.SPEC_LIB_FILENAME+Shell::quote_arg(LINKER_OUTPUT_FILENAME)) + ' ' +
catmap ' ' Shell::quote_arg cppos
;
spec.debugln$ "Library archive command="+CMD;
var result=System::system(CMD);
if result != 0 do
eprintln$ "Library archive command="+CMD+ " FAILED";
done
return result;
}
}
@
@h1 Dependency Checker
The dependency checker is used to examine a single C or C++
source file and check if the file, or any of dependencies,
has changed. To do this it records a dependency file with a ".d"
suffix as its output which lists all the files which are
depended on as well as the command line switches used
to invoke the compiler. The dependent file list is generated
by the underlying compiler, which must support this ability.
@tangle flx_depchk.flx
include "std/felix/toolchain_interface";
gen cxx_depcheck (tc: toolchain_t, src:string, dst:string) : bool =
{
fun == (a:list[string], b:list[string]) =
{
match a,b with
| #Empty,Empty => return true;
| Cons (h1,t1), Cons (h2,t2) =>
if h1 != h2 do
return false;
done
return t1 == t2; // tail call
| _ =>return false;
endmatch;
}
fun maxf (t:double) (f:string) => max (t, FileStat::dfiletime (f, #FileStat::future_time));
var new_switches = cat ' ' #(tc.get_base_cxx_compile_flags);
var result, deps = tc.cxx_dependency_generator (src=src);
if result != 0 do
println$ "[flx_depchk] C++ Dependency generator FAILED on " + src;
return false;
done
var newdeps = tc.dependency_parser deps;
var depfile = dst + ".d";
var olddeptxt = load depfile;
var old_switches, olddeps =
match filter (fun (s:string)=> s != "") (split (olddeptxt,"\n")) with
| h ! t => h,t
| _ => "",Empty[string]
;
var samedeps = new_switches == old_switches and newdeps == olddeps;
//if not samedeps do
// println$ "DEPS CHANGED";
// println$ "Old deps = " + olddeps.str;
// println$ "New deps = " + newdeps.str;
//done
save$ depfile, new_switches ! newdeps;
var fresh = samedeps and #{
var t = fold_left maxf #FileStat::past_time newdeps;
return t < FileStat::dfiletime (dst, #FileStat::past_time);
};
//println$ "[flx] Output " + dst + " is " + if fresh then "FRESH" else "STALE" endif;
return fresh;
}
gen c_depcheck (tc: toolchain_t, src:string, dst:string) : bool =
{
fun == (a:list[string], b:list[string]) =
{
match a,b with
| #Empty,Empty => return true;
| Cons (h1,t1), Cons (h2,t2) =>
if h1 != h2 do
return false;
done
return t1 == t2; // tail call
| _ => return false;
endmatch;
}
fun maxf (t:double) (f:string) =>
max(t, FileStat::dfiletime (f, #FileStat::future_time))
;
var new_switches = cat ' ' #(tc.get_base_c_compile_flags);
var result, deps = tc.c_dependency_generator (src=src);
if result != 0 do
println$ "[flx_depchk] C Dependency generator FAILED on " + src;
return false;
done
var newdeps = tc.dependency_parser deps;
var depfile = dst + ".d";
var olddeptxt = load depfile;
var old_switches, olddeps =
match filter (fun (s:string)=> s != "") (split (olddeptxt,"\n")) with
| h ! t => h,t
| _ => "",Empty[string]
;
var samedeps = new_switches == old_switches and newdeps == olddeps;
save$ depfile, new_switches ! newdeps;
var fresh = samedeps and #{
var t = fold_left maxf #FileStat::past_time newdeps;
return t < FileStat::dfiletime (dst, #FileStat::past_time);
};
//println$ "[flx] Output " + dst + " is " + if fresh then "FRESH" else "STALE" endif;
return fresh;
}
@
@h1 Library Builder
Builds a complete library from a flx_pkgconfig database
specification. Used by the flx_build_rtl tool.
@tangle flx_mklib.flx
include "std/felix/toolchain_config";
include "std/felix/flx_pkg"; // only for "fix2word_flags"
include "std/felix/flx_cp";
include "std/felix/flx/flx_depchk";
class FlxLibBuild
{
private fun / (x:string,y:string) => Filename::join(x,y);
noinline gen make_lib
(
db: FlxPkgConfig::FlxPkgConfigQuery_t,
toolchain-maker: toolchain_config_t -> toolchain_t,
c_compiler_executable: string,
cxx_compiler_executable: string,
src_dir:string,
target_dir:string,
share_rtl:string,
pkg:string,
tmpdir:string,
static_only:bool,
debug: bool
) () : bool =
{
proc dbug (x:string) => if debug call println$ '[make_lib: '+pkg+']' x;
proc ehandler () {
eprintln$ "toolchain: make_lib failed, temporary ehandler invoked";
System::exit 1;
}
println$ "------------";
println$ "Make lib " + pkg;
println$ "------------";
var srcdir = db.getpkgfielddflt ehandler (pkg,"srcdir");
var srcpath = src_dir / srcdir;
println$ "[make_lib] source directory " + srcpath;
var build_includes= db.getpkgfield ehandler (pkg,"build_includes");
println$ "[make_lib] build_includes " + build_includes;
var result3,ddeps= db.query$ list$ pkg, "--keepleftmost", "--field=requires_dlibs";
ddeps = FlxPkg::fix2word_flags ddeps;
var deps = db.getpkgfield ehandler (pkg,"Requires");
var result,depdlibs = db.query("--field=provides_dlib"+deps); // packaged dlibs
var macros = db.getpkgfield ehandler (pkg,"macros");
var result2,ccflags = db.query$ list$ pkg, "--keepleftmost", "--field=cflags";
var toolchain_config =
(
c_compiler_executable = c_compiler_executable,
cxx_compiler_executable = cxx_compiler_executable,
header_search_dirs= list[string] (target_dir, srcpath, share_rtl)+build_includes,
macros= macros,
ccflags = ccflags,
library_search_dirs= list[string] ("-L"+target_dir), // HACK!!!
dynamic_libraries= ddeps+depdlibs,
static_libraries= Empty[string],
debugln = dbug
)
;
var toolchain = toolchain-maker toolchain_config;
println$ #(toolchain.whatami);
// THIS DOES NOT SEEM RIGHT, we're copying headers from share/src
// into share/lib/rtl
//
// previously we copied into host/lib/rtl but that's even wronger
// because only calculated configuration headers go there
//
// the thing is, the share directory is supposed to be read-only,
// and the files in it immutable, so the contents should already
// have been put there direct from the repository
//
// of course, for add on packages, share may need updating ..
// its all confusing :)
//
// Leave this in there for now because demux is not actually packaged.
// the fbuild process has put stuff in share already though!
var headers = db.getpkgfielddflt ehandler (pkg,"headers");
if headers == "" do headers = r".*\.h(pp)?"; println$ "copying all header files"; done
var hsrc, hdst = "","";
match split (headers, ">") with
| #Empty => ;
| Cons (h,#Empty) => hsrc = h;
| Cons (h,Cons (d,#Empty)) => hsrc = h; hdst = d;
| _ => println$ "Header file too many > characters " + headers;
endmatch;
if hdst == "" do hdst = "${0}"; done
println$ "Copying headers " + hsrc + " > " + hdst;
CopyFiles::copyfiles (srcpath, hsrc,share_rtl/hdst,true, true);
var pats = db.getpkgfield ehandler (pkg,"src");
var pat = catmap '|' (fun (x:string)=>"("+x+")") pats;
//println$ "Finding Sources in "+srcpath;
//println$ "Matching pattern "+pat;
var files = FileSystem::regfilesin (srcpath,pat);
//println$ "Sources = " + str files;
if not static_only
do
begin
fun objname (file:string) => let
dstobj = file.Filename::strip_extension + #(toolchain.dynamic_object_extension) in
tmpdir/ dstobj
;
for file in files do
var srcfile = srcpath/ file;
var dst = objname file;
Directory::mkdirs (Filename::dirname dst);
match Filename::get_extension srcfile with
| x when x == ".cc" or x == ".cpp" =>
var fresh = cxx_depcheck (toolchain, srcfile, dst);
if fresh do
println$ "C++: Up to date [dynamic] " + file " -> " + objname file;
result = 0;
else
println$ "C++: Compiling [dynamic] " + file " -> " + objname file;
result = toolchain.cxx_dynamic_object_compiler (src=srcfile, dst=dst);
done
| ".c" =>
fresh = c_depcheck (toolchain, srcfile, dst);
if fresh do
println$ "C: Up to date [dynamic] " + file " -> " + objname file;
result = 0;
else
println$ "C: Compiling [dynamic] " + file " -> " + objname file;
result = toolchain.c_dynamic_object_compiler (src=srcfile, dst=dst) ;
done
| x =>
println$ "Unknown extension " + x;
goto bad;
endmatch
;
if result != 0 do
println$ "Compiler result " + str result;
goto bad;
done
done
var objs = unbox (map objname files);
var libname =
let dlib_root = db.getpkgfield1 ehandler (pkg,"provides_dlib") in
if prefix (dlib_root,"-l") then "lib"+dlib_root.[2 to]
elif prefix (dlib_root,"/DEFAULTLIB:") then dlib_root.[12 to]
else dlib_root
endif
+#(toolchain.dynamic_library_extension)
;
var dstlib = target_dir/libname;
println$ "Dynamic Linking library " + dstlib;
//println$ " Source object files = " + objs.str;
result = toolchain.dynamic_library_linker(srcs=objs, dst=dstlib);
if result != 0 do
println$ "Linker result " + str result;
goto bad;
done
end
done
begin
fun objname (file:string) => let
dstobj = file.Filename::strip_extension + #(toolchain.static_object_extension) in
tmpdir/ dstobj
;
for file in files do
var srcfile = srcpath/ file;
var dst = objname file;
Directory::mkdirs (Filename::dirname dst);
match Filename::get_extension srcfile with
| x when x == ".cc" or x == ".cpp" =>
var fresh = cxx_depcheck (toolchain, srcfile, dst);
if fresh do
println$ "C++: Up to date [static] " + file " -> " + objname file;
result = 0;
else
println$ "C++: Compiling [static] " + file " -> " + objname file;
result = toolchain.cxx_static_library_object_compiler (src=srcfile, dst=dst);
done
| ".c" =>
fresh = c_depcheck (toolchain, srcfile, dst);
if fresh do
println$ "C: Up to date [static] " + file " -> " + objname file;
result = 0;
else
println$ "C: Compiling [static] " + file " -> " + objname file;
result = toolchain.c_static_object_compiler (src=srcfile, dst=dst);
done
| x => println$
"Unknown extension " + x;
println$ "Compiler result " + str result;
goto bad;
endmatch
;
if result != 0 do
println$ "Compiler result " + str result;
goto bad;
done
done
var objs = unbox (map objname files);
var libname =
let dlib_root = db.getpkgfield1 ehandler (pkg,"provides_slib") in
if prefix (dlib_root,"-l") then "lib"+dlib_root.[2 to]
elif prefix (dlib_root,"/DEFAULTLIB:") then dlib_root.[12 to]
else dlib_root
endif
+#(toolchain.static_library_extension);
;
var dstlib = target_dir/libname;
println$ "Static Linking Library " + dstlib;
//println$ " Source object files = " + objs.str;
result = toolchain.static_library_linker(srcs=objs, dst=dstlib);
if result != 0 do
println$ "Linker result " + str result;
goto bad;
done
end
return true;
bad:>
return false;
}
}
@
@h1 Toolchains
Toolchains for specific vendor compilers and operating
system combinations.
Each specific toolchain is an object which implements
the toolchain interface.
@h2 Object for gcc on Linux
@tangle gcc_linux.flx
include "std/felix/toolchain_interface";
include "std/felix/toolchain_config";
include "std/felix/flx_cxx";
object toolchain_gcc_linux (config:toolchain_config_t) implements toolchain_t =
{
var cxx_compile_warning_flags = list$ "-w",
"-Wfatal-errors",
"-Wno-invalid-offsetof",
"-Wno-parentheses",
"-Wno-unused-variable",
"-Wno-unused-label",
"-Wno-unused-function",
"-Wno-sign-compare",
"-Wno-missing-braces"
;
var c_compile_warning_flags = list[string]$ "-w", "-Wfatal-errors";
var c_compiler = let x = config.c_compiler_executable in if x == "" then "gcc" else x;
var cxx_compiler = let x = config.cxx_compiler_executable in if x == "" then "g++" else x;
var linker = cxx_compiler;
var ccflags_for_dynamic_link = list[string] ("-shared");
var base_c_compile_flags =
"-D_POSIX" ! "-g" ! "-c" ! "-O1" ! "-fno-common"
! "-fno-strict-aliasing" ! (c_compile_warning_flags+config.ccflags)
;
var base_cxx_compile_flags =
"-D_POSIX" ! "-g"! "-c" ! "-O1" ! "-fno-common"
! "-fno-strict-aliasing" ! "-std=gnu++14" ! (cxx_compile_warning_flags+config.ccflags)
;
method fun whatami () => "toolchain_gcc_linux (version 2)";
method fun host_os () => "LINUX";
method fun target_os () => "LINUX";
method fun cxx_compiler_vendor () => "GNU";
method fun dependency_extension () => ".d";
method fun executable_extension () => "";
method fun static_object_extension () => "_static.o";
method fun dynamic_object_extension () => "_dynamic.o";
method fun static_library_extension () => ".a";
method fun dynamic_library_extension () => ".so";
method fun pathname_separator () => "/";
method fun debug_flags () =>list[string] "-g";
method fun get_base_c_compile_flags () => base_c_compile_flags;
method fun get_base_cxx_compile_flags () => base_cxx_compile_flags;
// Boilerplate
method gen c_dependency_generator (spec:(src:string)) =
{
var result, data =
CxxCompiler::generic_cxx_gen_deps
(
CCDEP=c_compiler,
CCFLAGS = "-MM" ! "-D_POSIX" ! config.ccflags,
INCLUDE_DIRS=config.header_search_dirs,
MACROS=config.macros,
debugln = config.debugln
)
(spec.src)
;
return result, data;
}
method gen cxx_dependency_generator (spec:(src:string)) =
{
var result, data =
CxxCompiler::generic_cxx_gen_deps
(
CCDEP=cxx_compiler,
CCFLAGS = "-MM" ! "-D_POSIX" ! "-std=gnu++14" ! config.ccflags,
INCLUDE_DIRS=config.header_search_dirs,
MACROS=config.macros,
debugln = config.debugln
)
(spec.src)
;
return result, data;
}
method gen dependency_parser (data:string) : list[string] =>
CxxCompiler::generic_dependency_parser data
;
method gen c_static_object_compiler (spec:(dst:string, src:string)) : int =
{
var result =
CxxCompiler::generic_cxx_compile_for_static
// case 2 of dflt
(
CCOBJ_STATIC_LIB = c_compiler,
CCFLAGS = "-fvisibility=hidden" ! base_c_compile_flags,
INCLUDE_DIRS = config.header_search_dirs,
MACROS = config.macros,
SPEC_OBJ_FILENAME = "-o ",
debugln = config.debugln
)
(spec.src, spec.dst)
;
return result;
}
method gen c_dynamic_object_compiler (spec:(dst:string, src:string)) : int =
{
var result =
CxxCompiler::generic_cxx_compile_for_dynamic
(
CCOBJ_DLLIB = c_compiler,
CCFLAGS = "-fPIC" ! "-fvisibility=hidden" ! base_c_compile_flags,
INCLUDE_DIRS = config.header_search_dirs,
MACROS = config.macros,
SPEC_OBJ_FILENAME = "-o ",
debugln = config.debugln
)
(spec.src, spec.dst)
;
return result;
}
method gen cxx_static_object_compiler (spec:(dst:string, src:string)) : int =
{
var result =
CxxCompiler::generic_cxx_compile_for_static
(
CCOBJ_STATIC_LIB = cxx_compiler,
CCFLAGS = "-fvisibility=hidden" !"-g"! "-c" ! "-O1" ! "-fno-common"! "-fno-strict-aliasing"
! "-D_POSIX" ! "-std=gnu++14" ! "-D_GLIBCXX_USE_CXX11_ABI=1"
! (cxx_compile_warning_flags+config.ccflags),
INCLUDE_DIRS = config.header_search_dirs,
MACROS = config.macros,
SPEC_OBJ_FILENAME = "-o ",
debugln = config.debugln
)
(spec.src, spec.dst)
;
return result;
}
method gen cxx_static_library_object_compiler (spec:(dst:string, src:string)) : int =
{
var result =
CxxCompiler::generic_cxx_compile_for_static
(
CCOBJ_STATIC_LIB = cxx_compiler,
CCFLAGS = "-fvisibility=hidden" ! "-D_GLIBCXX_USE_CXX11_ABI=1"!base_cxx_compile_flags,
INCLUDE_DIRS = config.header_search_dirs,
MACROS = "FLX_STATIC_LINK"+config.macros,
SPEC_OBJ_FILENAME = "-o ",
debugln = config.debugln
)
(spec.src, spec.dst)
;
return result;
}
method gen cxx_dynamic_object_compiler (spec:(dst:string, src:string)) : int =
{
var result =
CxxCompiler::generic_cxx_compile_for_dynamic
(
CCOBJ_DLLIB = linker,
CCFLAGS = "-fPIC" ! "-fvisibility=hidden" ! "-D_GLIBCXX_USE_CXX11_ABI=1"! base_cxx_compile_flags,
INCLUDE_DIRS = config.header_search_dirs,
MACROS = config.macros,
SPEC_OBJ_FILENAME = "-o ",
debugln = config.debugln
)
(spec.src, spec.dst)
;
return result;
}
method gen static_library_linker (spec:(dst:string, srcs:list[string])): int =
{
var result =
CxxCompiler::generic_static_library
(
CCLINK_STATIC_LIB = "ar",
CCFLAGS = list[string]("-rcs"),
SPEC_LIB_FILENAME = "",
debugln = config.debugln
)
(spec.srcs, spec.dst)
;
return result;