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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
|
# ccenv.sh: sofort's tool-finding bits,
# invoked from within the project-agnostic configure script.
# invocation and names of binary tools:
# agnostic names (ar, nm, objdump, ...);
# target-prefixed agnostic names (x86_64-nt64-midipix-ar, ...);
# branded names (llvm-ar, llvm-nm, llvm-objdump, ...);
# target-prefixed branded names (x86_64-linux-gnu-gcc-ar, ...);
# target-specifying branded tools (llvm-ar --target=x86_64-linux, ...).
# cross-compilation: default search order:
# target-prefixed agnostic tools;
# target-prefixed branded tools, starting with the prefix
# most commonly associated with the selected compiler (that is,
# ``gcc'' when using gcc, and ``llvm'' when using clang);
# target-speficying branded tools, starting once again with the
# prefix most commonly associated with the selected compiler.
# internal variables of interest:
# ccenv_cfgtype: the type of host being tested (host/native)
# ccenv_cfgfile: the configuration file for the host being tested
# ccenv_cflags: the comprehensive cflags for the host being tested
# ccenv_cchost: the host being tested, as reported by -dumpmachine
ccenv_usage()
{
cat "$mb_project_dir"/sofort/ccenv/ccenv.usage
exit 0
}
ccenv_newline()
{
printf '\n' >> "$ccenv_cfgfile"
}
ccenv_comment()
{
ccenv_internal_str='#'
for ccenv_internal_arg ; do
ccenv_internal_str="$ccenv_internal_str $ccenv_internal_arg"
done
printf '%s\n' "$ccenv_internal_str" >> "$ccenv_cfgfile"
}
ccenv_tool_prolog()
{
ccenv_line_dots='.....................................'
ccenv_tool_desc="=== checking for ${1}"
ccenv_tool_dlen="${#ccenv_line_dots}"
printf '\n%s\n' '________________________' >&3
printf "ccenv: checking for ${1}\n\n" >&3
printf "%${ccenv_tool_dlen}.${ccenv_tool_dlen}s" \
"${ccenv_tool_desc} ${mb_line_dots}"
}
ccenv_tool_epilog()
{
ccenv_line_dots='................................'
ccenv_tool_dlen="$((${#ccenv_line_dots} - ${#1}))"
printf "%${ccenv_tool_dlen}.${ccenv_tool_dlen}s %s.\n" \
"${ccenv_line_dots}" "${1}"
if [ "${1}" = 'false' ]; then
printf '\n\nccenv: not (yet) found.\n' >&3
else
printf "\n\nccenv : found $(command -v ${1}).\n" >&3
fi
printf '%s\n' '------------------------' >&3
}
ccenv_tool_variant_epilog()
{
ccenv_expr=${1}='${'${1}':-false}'
eval "$ccenv_expr"
ccenv_expr='${'${1}'}'
eval ccenv_tool_epilog "$ccenv_expr"
}
ccenv_attr_prolog()
{
ccenv_line_dots=' .....................................'
ccenv_attr_desc=" == detect ${ccenv_cfgtype} ${1}"
ccenv_attr_dlen="${#ccenv_line_dots}"
printf "%${ccenv_attr_dlen}.${ccenv_attr_dlen}s" \
"${ccenv_attr_desc} ${ccenv_line_dots}"
printf '\n%s\n' '________________________' >&3
printf "ccenv: detecting ${1}\n\n" >&3
}
ccenv_attr_epilog()
{
ccenv_line_dots='................................'
ccenv_tool_dlen="$((${#ccenv_line_dots} - 1 - ${#1}))"
printf "%${ccenv_tool_dlen}.${ccenv_tool_dlen}s %s.\n" \
"${ccenv_line_dots}" "${1}"
printf '\n\nccenv: detected result: %s\n' "${1}" >&3
printf '%s\n' '------------------------' >&3
}
ccenv_find_tool()
{
if [ -z "$ccenv_prefixes" ]; then
for ccenv_candidate in $(printf '%s' "$ccenv_candidates"); do
if [ -z ${@:-} ]; then
if command -v "$ccenv_candidate" > /dev/null; then
ccenv_tool="$ccenv_candidate"
return 0
fi
else
if command -v "$ccenv_candidate" > /dev/null; then
if "$ccenv_candidate" $@ > /dev/null 2>&3; then
ccenv_tool="$ccenv_candidate"
return 0
fi
fi
fi
done
ccenv_tool=false
return 0
fi
for ccenv_prefix in $(printf '%s' "$ccenv_prefixes"); do
for ccenv_candidate in $(printf '%s' "$ccenv_candidates"); do
ccenv_tool="$ccenv_prefix$ccenv_candidate"
if command -v "$ccenv_tool" > /dev/null; then
return 0
fi
done
done
for ccenv_candidate in $(printf '%s' "$ccenv_candidates"); do
if command -v "$ccenv_candidate" > /dev/null; then
ccenv_tool="$ccenv_candidate"
return 0
fi
done
ccenv_tool=false
return 0
}
ccenv_set_primary_tools()
{
ccenv_core_tools="ar nm objdump ranlib size strip strings objcopy"
ccenv_hack_tools="addr2line cov elfedit readelf readobj otool"
ccenv_peep_tools="perk mdso dlltool windmc windres"
for __tool in $(printf '%s' "$ccenv_core_tools $ccenv_hack_tools $ccenv_peep_tools"); do
ccenv_tool_prolog "$__tool"
if [ -n "$mb_agnostic" ]; then
ccenv_candidates=" $__tool"
elif [ -n "$mb_zealous" ]; then
ccenv_candidates="$mb_zealous-$__tool"
elif [ "$mb_toolchain" = 'gcc' ]; then
ccenv_candidates="gcc-$__tool"
ccenv_candidates="$ccenv_candidates $__tool"
ccenv_candidates="$ccenv_candidates llvm-$__tool"
elif [ "$mb_toolchain" = 'llvm' ]; then
ccenv_candidates="llvm-$__tool"
ccenv_candidates="$ccenv_candidates $__tool"
ccenv_candidates="$ccenv_candidates gcc-$__tool"
elif [ -n "$mb_toolchain" ]; then
ccenv_candidates="$mb_toolchain-$__tool"
ccenv_candidates="$ccenv_candidates $__tool"
ccenv_candidates="$ccenv_candidates gcc-$__tool"
ccenv_candidates="$ccenv_candidates llvm-$__tool"
else
ccenv_candidates="$__tool"
ccenv_candidates="$ccenv_candidates gcc-$__tool"
ccenv_candidates="$ccenv_candidates llvm-$__tool"
fi
if [ "$ccenv_cfgtype" = 'host' ]; then
ccenv_var_prefix='mb_'
else
ccenv_var_prefix='mb_native_'
fi
ccenv_var_name=$ccenv_var_prefix$__tool
ccenv_var_expr='${'$ccenv_var_name':-}'
eval ccenv_var_val=$ccenv_var_expr
if [ -n "$ccenv_var_val" ]; then
eval ccenv_$__tool="$ccenv_var_val"
else
ccenv_find_tool
eval ccenv_$__tool="$ccenv_tool"
fi
ccenv_tool_epilog "$ccenv_tool"
done
# windrc
ccenv_windrc="$ccenv_windres"
}
ccenv_set_tool_variants()
{
# as (asm)
ccenv_tool_prolog 'as (asm)'
ccenv_candidates=as
ccenv_find_tool
if [ "$ccenv_tool" = false ]; then
ccenv_as_asm=
else
$ccenv_tool --help | grep -i '.bc assembler' \
|| ccenv_as_asm="$ccenv_tool"
fi
ccenv_tool_variant_epilog 'ccenv_as_asm'
# as (ll)
ccenv_tool_prolog 'as (ll)'
ccenv_candidates=llvm-as
ccenv_find_tool
if [ "$ccenv_tool" != false ]; then
ccenv_as_ll="$ccenv_tool"
fi
ccenv_tool_variant_epilog 'ccenv_as_ll'
# as (mc)
ccenv_tool_prolog 'as (mc)'
ccenv_candidates=llvm-mc
ccenv_find_tool
if [ "$ccenv_tool" != false ]; then
ccenv_as_mc="$ccenv_tool"
fi
ccenv_tool_variant_epilog 'ccenv_as_mc'
# ld (bfd)
ccenv_tool_prolog 'ld (bfd)'
ccenv_candidates=ld.bfd
ccenv_find_tool
if [ "$ccenv_tool" != false ]; then
ccenv_ld_bfd="$ccenv_tool"
fi
ccenv_tool_variant_epilog 'ccenv_ld_bfd'
# ld (gold)
ccenv_tool_prolog 'ld (gold)'
ccenv_candidates=ld.gold
ccenv_find_tool
if [ "$ccenv_tool" != false ]; then
ccenv_ld_gold="$ccenv_tool"
fi
ccenv_tool_variant_epilog 'ccenv_ld_gold'
# ld (lld)
ccenv_tool_prolog 'ld (lld)'
ccenv_candidates=lld
ccenv_find_tool
if [ "$ccenv_tool" != false ]; then
ccenv_ld_lld="$ccenv_tool"
fi
ccenv_tool_variant_epilog 'ccenv_ld_lld'
# objdump (bfd)
ccenv_tool_prolog 'objdump (bfd)'
ccenv_candidates=objdump
ccenv_find_tool
if $ccenv_tool --version | grep -i Binutils > /dev/null; then
ccenv_objdump_bfd="$ccenv_tool"
fi
ccenv_tool_variant_epilog 'ccenv_objdump_bfd'
# objdump (llvm)
ccenv_tool_prolog 'objdump (llvm)'
ccenv_candidates=llvm-objdump
ccenv_find_tool
if $ccenv_tool --version | grep -i LLVM > /dev/null; then
ccenv_objdump_llvm="$ccenv_tool"
fi
ccenv_tool_variant_epilog 'ccenv_objdump_llvm'
# readelf (bfd)
ccenv_tool_prolog 'readelf (bfd)'
ccenv_candidates=readelf
ccenv_find_tool
if $ccenv_tool --version | grep -i Binutils > /dev/null; then
ccenv_readelf_bfd="$ccenv_tool"
fi
ccenv_tool_variant_epilog 'ccenv_readelf_bfd'
# readelf (llvm)
ccenv_tool_prolog 'readelf (llvm)'
ccenv_candidates=llvm-readelf
ccenv_find_tool
if $ccenv_tool --version | grep -i LLVM > /dev/null; then
ccenv_readelf_llvm="$ccenv_tool"
fi
ccenv_tool_variant_epilog 'ccenv_readelf_llvm'
# as
if [ -n "$ccenv_cc" ]; then
ccenv_as='$('"$ccenv_makevar_prefix"'CC) -c -x assembler'
elif [ -n "$mb_agnostic" ]; then
ccenv_as='$('"$ccenv_makevar_prefix"'AS_ASM)'
elif [ "$mb_zealous" = 'gcc' ]; then
ccenv_as='$('"$ccenv_makevar_prefix"'AS_ASM)'
elif [ -n "$mb_zealous" = 'llvm' ]; then
ccenv_as='$('"$ccenv_makevar_prefix"'AS_MC)'
elif [ "$mb_toolchain" = 'gcc' ]; then
ccenv_as='$('"$ccenv_makevar_prefix"'AS_ASM)'
elif [ "$mb_toolchain" = 'llvm' ]; then
ccenv_as='$('"$ccenv_makevar_prefix"'AS_MC)'
fi
# ld
if [ -n "$ccenv_cc" ]; then
ccenv_ld='$('"$ccenv_makevar_prefix"'CC) -nostdlib -nostartfiles'
fi
}
ccenv_set_c_compiler_candidates()
{
if [ -n "$mb_compiler" ]; then
ccenv_candidates="$mb_compiler"
elif [ -n "$mb_agnostic" ]; then
ccenv_candidates="c99 c11 cc"
elif [ "$mb_zealous" = 'gcc' ]; then
ccenv_candidates="gcc"
elif [ "$mb_zealous" = 'llvm' ]; then
ccenv_candidates="clang"
elif [ "$mb_toolchain" = 'gcc' ]; then
ccenv_candidates="gcc c99 c11 cc clang"
elif [ "$mb_toolchain" = 'llvm' ]; then
ccenv_candidates="clang c99 c11 cc gcc"
elif [ -n "$mb_toolchain" ]; then
ccenv_candidates="$mb_toolchain c99 c11 cc gcc clang"
else
ccenv_candidates="c99 c11 cc gcc clang"
fi
}
ccenv_set_cc()
{
ccenv_tool_prolog 'C compiler'
if [ -z "$ccenv_cc" ]; then
ccenv_set_c_compiler_candidates
ccenv_find_tool -dumpmachine
ccenv_cc="$ccenv_tool"
fi
if [ "$ccenv_cc" = false ] && [ -n "$mb_compiler" ]; then
ccenv_cc="$mb_compiler"
fi
ccenv_cc_cmd="$ccenv_cc"
ccenv_errors=
if [ "$ccenv_cfgtype" = 'native' ]; then
ccenv_host=$($ccenv_cc $(printf '%s' "$ccenv_cflags") -dumpmachine 2>&3)
ccenv_cchost=$ccenv_host
ccenv_tool_epilog "$ccenv_cc"
return 0
fi
if [ -n "$mb_cchost" ]; then
ccenv_host="$mb_cchost"
elif [ -n "$mb_host" ]; then
ccenv_host="$mb_host"
else
ccenv_host=
fi
if [ -z "$ccenv_host" ]; then
ccenv_host=$($ccenv_cc $(printf '%s' "$ccenv_cflags") -dumpmachine 2>&3)
ccenv_cchost=$ccenv_host
else
ccenv_tmp=$(mktemp ./tmp_XXXXXXXXXXXXXXXX)
ccenv_cmd="$ccenv_cc --target=$ccenv_host -E -xc -"
if [ -z "$mb_user_cc" ]; then
$(printf %s "$ccenv_cmd") < /dev/null > /dev/null \
2>"$ccenv_tmp" || true
ccenv_errors=$(cat "$ccenv_tmp")
if [ -z "$ccenv_errors" ]; then
ccenv_tflags="--target=$ccenv_host"
ccenv_cc="$ccenv_cc $ccenv_tflags"
else
printf '%s' "$ccenv_errors" >&3
fi
fi
rm -f "$ccenv_tmp"
unset ccenv_tmp
ccenv_cchost=$($ccenv_cc $(printf '%s' "$ccenv_cflags") -dumpmachine 2>&3)
fi
if [ "$ccenv_cchost" != "$ccenv_host" ]; then
printf 'error!\n' >&2
printf 'ccenv:\n' >&2
printf 'ccenv: ccenv_host: %s \n' $ccenv_host >&2
printf 'ccenv: ccenv_cchost: %s \n' $ccenv_cchost >&2
if [ -z "$ccenv_tflags" ]; then
printf 'ccenv:\n' >&2
printf 'ccenv: ccenv_host and ccenv_cchost do not match, most likely because:\n' >&2
printf 'ccenv: (1) you explicitly set CC (or passed --compiler=...)\n' >&2
printf 'ccenv: (2) the selected compiler does not accept --target=...\n' >&2
printf 'ccenv: (3) the host reported by -dumpmachine differs from the one you requested.\n' >&2
fi
if [ -n "$ccenv_errors" ]; then
printf 'ccenv:\n' >&2
printf 'ccenv: something went wrong, see the command and compiler message below.\n\n' >&2
printf 'cmd: %s < /dev/null > /dev/null\n' "$ccenv_cmd" >&2
printf '%s\n\n' "$ccenv_errors" >&2
else
printf 'ccenv:\n' >&2
printf 'ccenv: something went wrong, bailing out.\n\n' >&2
fi
return 2
fi
ccenv_tool_epilog "$ccenv_cc"
}
ccenv_set_cpp()
{
ccenv_tool_prolog 'C pre-processor'
case "$ccenv_cc_cmd" in
cc | c99 | c11 | gcc)
ccenv_cpp_prefix=
ccenv_candidates="cpp" ;;
clang )
ccenv_cpp_prefix=
ccenv_candidates="clang-cpp" ;;
*-cc )
ccenv_cpp_prefix=${ccenv_cc_cmd%-cc*}-
ccenv_candidates="${ccenv_cpp_prefix}cpp" ;;
*-c99 )
ccenv_cpp_prefix=${ccenv_cc_cmd%-c99*}-
ccenv_candidates="${ccenv_cpp_prefix}cpp" ;;
*-c11 )
ccenv_cpp_prefix=${ccenv_cc_cmd%-c11*}-
ccenv_candidates="${ccenv_cpp_prefix}cpp" ;;
*-gcc )
ccenv_cpp_prefix=${ccenv_cc_cmd%-gcc*}-
ccenv_candidates="${ccenv_cpp_prefix}cpp" ;;
*-clang )
ccenv_cpp_prefix=${ccenv_cc_cmd%-clang*}-
ccenv_candidates="${ccenv_cpp_prefix}clang-cpp" ;;
* )
ccenv_cpp="$ccenv_cc -E"
ccenv_tool_epilog "$ccenv_cpp"
return 0
esac
ccenv_find_tool
if [ "$ccenv_tool" = false ]; then
ccenv_cpp="$ccenv_cc -E"
elif [ -n "$ccenv_tflags" ]; then
ccenv_cpp="$ccenv_tool $ccenv_tflags"
else
ccenv_cpp="$ccenv_tool"
fi
ccenv_tool_epilog "$ccenv_cpp"
}
ccenv_set_cxx()
{
ccenv_tool_prolog 'C++ compiler'
case "$ccenv_cc_cmd" in
cc | c99 | c11 )
ccenv_cxx_prefix=
ccenv_candidates="cxx c++" ;;
gcc )
ccenv_cxx_prefix=
ccenv_candidates="g++" ;;
clang )
ccenv_cxx_prefix=
ccenv_candidates="clang++" ;;
*-gcc )
ccenv_cpp_prefix=${ccenv_cc_cmd%-gcc*}-
ccenv_candidates="${ccenv_cpp_prefix}g++" ;;
*-clang )
ccenv_cpp_prefix=${ccenv_cc_cmd%-clang*}-
ccenv_candidates="${ccenv_cpp_prefix}clang++" ;;
*cc )
ccenv_cxx_prefix=${ccenv_cc_cmd%cc*}
ccenv_candidates="${ccenv_cpp_prefix}++" ;;
* )
ccenv_cxx="$ccenv_cc -xc++"
ccenv_tool_epilog "$ccenv_cxx"
return 0
esac
ccenv_find_tool
if [ "$ccenv_tool" = false ]; then
ccenv_cxx="$ccenv_cc -xc++"
elif [ -n "$ccenv_tflags" ]; then
ccenv_cxx="$ccenv_tool $ccenv_tflags"
else
ccenv_cxx="$ccenv_tool"
fi
ccenv_tool_epilog "$ccenv_cxx"
}
ccenv_set_cc_host()
{
ccenv_attr_prolog 'system'
ccenv_cc_host="$ccenv_cchost"
ccenv_attr_epilog "$ccenv_cc_host"
}
ccenv_set_cc_bits()
{
ccenv_attr_prolog 'bits'
ccenv_internal_size=
ccenv_internal_type='void *'
ccenv_internal_test='char x[(sizeof(%s) == %s/8) ? 1 : -1];'
for ccenv_internal_guess in 64 32 128; do
if [ -z $ccenv_internal_size ]; then
ccenv_internal_str=$(printf "$ccenv_internal_test" \
"$ccenv_internal_type" \
"$ccenv_internal_guess")
printf '%s' "$ccenv_internal_str" \
| $ccenv_cc -S -xc - -o - \
$(printf '%s' "$ccenv_cflags") \
> /dev/null 2>&3 \
&& ccenv_internal_size=$ccenv_internal_guess
fi
done
ccenv_cc_bits=$ccenv_internal_size
ccenv_attr_epilog "$ccenv_cc_bits"
}
ccenv_set_cc_underscore()
{
ccenv_attr_prolog 'prepended underscores'
ccenv_fn_name='ZmYaXyWbVe_UuTnSdReQrPsOcNoNrLe'
ccenv_fn_code='int %s(void){return 0;}'
if printf "$ccenv_fn_code" $ccenv_fn_name \
| $ccenv_cc -xc - -S -o - \
| grep "^_$ccenv_fn_name:" \
> /dev/null; then
ccenv_cc_underscore='_'
ccenv_attr_epilog 'yes'
fi
ccenv_attr_epilog 'no'
return 0
}
ccenv_create_framework_executable()
{
if [ -f $ccenv_image ]; then
mv $ccenv_image $ccenv_image.tmp
rm -f $ccenv_image.tmp
fi
printf 'int main(void){return 0;}' \
| $ccenv_cc -xc - \
-o $ccenv_image \
|| return 1
return 0
}
ccenv_create_freestanding_executable()
{
if [ -f $ccenv_image ]; then
mv $ccenv_image $ccenv_image.tmp
rm -f $ccenv_image.tmp
fi
if [ -z "ccenv_cc_underscore" ]; then
ccenv_start_fn='_start'
else
ccenv_start_fn='start'
fi
printf 'int %s(void){return 0;}' "$ccenv_start_fn" \
| $ccenv_cc -xc - \
-ffreestanding \
-nostdlib -nostartfiles \
-o $ccenv_image \
|| return 1
ccenv_freestd=yes
return 0
}
ccenv_set_cc_binfmt_error()
{
ccenv_attr_epilog '(unable to create executable)'
}
ccenv_set_cc_binfmt()
{
ccenv_use_perk=
ccenv_use_otool=
ccenv_use_readelf=
ccenv_use_readobj=
ccenv_use_bfd_objdump=
ccenv_use_llvm_objdump=
ccenv_attr_prolog 'binary format'
ccenv_create_framework_executable \
|| ccenv_create_freestanding_executable \
|| ccenv_set_cc_binfmt_error \
|| return 0
# PE / perk
if [ -n "$ccenv_perk" ]; then
if $ccenv_perk $ccenv_image 2>&3; then
ccenv_cc_binfmt='PE'
ccenv_use_perk=yes
fi
fi
# ELF / readelf
if [ -n "$ccenv_readelf" ] && [ -z "$ccenv_cc_binfmt" ]; then
if $ccenv_readelf -h $ccenv_image 2>&3 \
| grep 'Magic:' | sed -e 's/[ ]*//g' \
| grep 'Magic:7f454c46' \
> /dev/null; then
ccenv_cc_binfmt='ELF'
ccenv_use_readelf=yes
fi
fi
# a marble of astonishing design:
# llvm-readelf also parses PE and Mach-O
if [ -n "$ccenv_readelf_llvm" ]; then
ccenv_readany="$ccenv_readelf_llvm"
else
ccenv_readany="$ccenv_readelf"
fi
# PE / readelf
if [ -n "$ccenv_readany" ] && [ -z "$ccenv_cc_binfmt" ]; then
if $ccenv_readany -h $ccenv_image 2>&3 \
| grep 'Magic:' | sed -e 's/[ ]*//g' \
| grep 'Magic:MZ' \
> /dev/null; then
ccenv_cc_binfmt='PE'
ccenv_use_readelf=yes
fi
fi
# MACHO-64 / otool
if [ -n "$ccenv_otool" ] && [ -z "$ccenv_cc_binfmt" ]; then
if $ccenv_otool -hv $ccenv_image 2>&3 \
| grep -i 'MH_MAGIC_64' \
> /dev/null; then
ccenv_cc_binfmt='MACHO'
ccenv_use_otool=yes
fi
fi
# MACHO-32 / otool
if [ -n "$ccenv_otool" ] && [ -z "$ccenv_cc_binfmt" ]; then
if $ccenv_otool -hv $ccenv_image 2>&3 \
| grep -i 'MH_MAGIC' \
> /dev/null; then
ccenv_cc_binfmt='MACHO'
ccenv_use_otool=yes
fi
fi
# MACHO-64 / readelf
if [ -n "$ccenv_readany" ] && [ -z "$ccenv_cc_binfmt" ]; then
if $ccenv_readany -h $ccenv_image 2>&3 \
| grep -i 'Magic:' | sed -e 's/[ ]*//g' \
| grep -i '(0xfeedfacf)' \
> /dev/null; then
ccenv_cc_binfmt='MACHO'
ccenv_use_readelf=yes
fi
fi
# MACHO-32 / readelf
if [ -n "$ccenv_readany" ] && [ -z "$ccenv_cc_binfmt" ]; then
if $ccenv_readany -h $ccenv_image 2>&3 \
| grep -i 'Magic:' | sed -e 's/[ ]*//g' \
| grep -i '(0xcafebabe)' \
> /dev/null; then
ccenv_cc_binfmt='MACHO'
ccenv_use_readelf=yes
fi
fi
# MACHO / readobj
if [ -n "$ccenv_readobj" ] && [ -z "$ccenv_cc_binfmt" ]; then
if $ccenv_readobj $ccenv_image 2>&3 \
| grep -i 'Format:' | sed 's/ /_/g' \
| grep -i '_Mach-O_' \
> /dev/null; then
ccenv_cc_binfmt='MACHO'
ccenv_use_readobj=yes
fi
fi
# MACHO / objdump (llvm)
if [ -n "$ccenv_objdump" ] && [ -z "$ccenv_cc_binfmt" ]; then
if $ccenv_objdump -section-headers $ccenv_image \
2>&3 \
| grep -i 'file format Mach-O' \
> /dev/null; then
ccenv_cc_binfmt='MACHO'
ccenv_use_objdump=yes
fi
fi
# MACHO / objdump (bfd)
if [ -n "$ccenv_objdump" ] && [ -z "$ccenv_cc_binfmt" ]; then
$ccenv_objdump -h $ccenv_image 2>&3 \
| grep -i 'file format Mach-O' \
> /dev/null \
&& ccenv_cc_binfmt='MACHO' \
&& ccenv_use_objdump=yes
fi
# PE / objdump (bfd)
if [ -n "$ccenv_objdump" ] && [ -z "$ccenv_cc_binfmt" ]; then
if $ccenv_objdump -h $ccenv_image 2>&3 \
| grep -i 'file format pei-' \
> /dev/null; then
ccenv_cc_binfmt='PE'
ccenv_use_bfd_objdump=yes
fi
fi
ccenv_attr_epilog "$ccenv_cc_binfmt"
}
ccenv_set_os_pe()
{
if [ -n "$ccenv_freestd" ]; then
case "$ccenv_cchost" in
*-midipix | *-midipix-* )
ccenv_os='midipix' ;;
*-mingw | *-mingw32 | *-mingw64 )
ccenv_os='mingw' ;;
*-mingw-* | *-mingw32-* | *-mingw64 )
ccenv_os='mingw' ;;
*-msys | *-msys2 | *-msys-* | *-msys2-* )
ccenv_os='msys' ;;
*-cygwin | *-cygwin-* )
ccenv_os='cygwin' ;;
esac
fi
if [ -n "$ccenv_os" ]; then
return 0
fi
if [ -n "$ccenv_use_perk" ]; then
ccenv_framework=$($ccenv_perk -y $ccenv_image)
ccenv_os=${ccenv_framework#*-*-*-*}
fi
if [ -z "$ccenv_os" ] && [ -n "$ccenv_objdump_bfd" ]; then
$ccenv_objdump_bfd -x $ccenv_image | grep -i 'DLL Name' \
| grep 'cygwin1.dll' > /dev/null \
&& ccenv_os='cygwin'
fi
if [ -z "$ccenv_os" ] && [ -n "$ccenv_objdump_bfd" ]; then
$ccenv_objdump_bfd -x $ccenv_image | grep -i 'DLL Name' \
| grep 'msys-2.0.dll' > /dev/null \
&& ccenv_os='msys'
fi
if [ -z "$ccenv_os" ] && [ -n "$ccenv_objdump_bfd" ]; then
$ccenv_objdump_bfd -x $ccenv_image \
| grep -i 'DLL Name' | grep '.CRT' \
> /dev/null \
&& $ccenv_objdump_bfd -x $ccenv_image \
| grep -i 'DLL Name' | grep '.bss' \
> /dev/null \
&& $ccenv_objdump_bfd -x $ccenv_image \
| grep -i 'DLL Name' | grep '.tls' \
> /dev/null \
&& ccenv_os='mingw'
fi
}
ccenv_set_os_macho()
{
case "$ccenv_cchost" in
*-apple-darwin* )
ccenv_os='darwin' ;;
esac
}
ccenv_set_os()
{
ccenv_attr_prolog 'os name'
case "$ccenv_cc_binfmt" in
PE )
ccenv_set_os_pe ;;
MACHO )
ccenv_set_os_macho ;;
esac
if [ -n "$ccenv_os" ]; then
ccenv_attr_epilog "$ccenv_os"
return 0
fi
case "$ccenv_cchost" in
*-*-*-* )
ccenv_tip=${ccenv_cchost%-*}
ccenv_os=${ccenv_tip#*-*-}
;;
*-*-musl | *-*-gnu )
ccenv_tip=${ccenv_cchost%-*}
ccenv_os=${ccenv_tip#*-}
;;
*-*-* )
ccenv_os=${ccenv_cchost#*-*-}
;;
*-* )
ccenv_os=${ccenv_cchost#*-}
;;
esac
if [ -z "$ccenv_os" ]; then
ccenv_os='anyos'
fi
ccenv_attr_epilog "$ccenv_os"
}
ccenv_set_os_flags()
{
case "$ccenv_os" in
darwin )
ccenv_cflags_os='-D_DARWIN_C_SOURCE'
ccenv_cflags_pic='-fPIC'
;;
midipix )
ccenv_cflags_os=
ccenv_cflags_pic='-fPIC'
;;
cygwin )
ccenv_cflags_os=
ccenv_cflags_pic=
;;
msys | msys* | mingw | mingw* )
ccenv_cflags_os='-U__STRICT_ANSI__'
ccenv_cflags_pic=
;;
* )
ccenv_cflags_os=
ccenv_cflags_pic='-fPIC'
;;
esac
}
ccenv_set_os_semantics()
{
# binary_format - core_api - ex_api - dependency_resolution
ccenv_attr_prolog 'os semantics'
case "$ccenv_os" in
linux )
ccenv_os_semantics='elf-posix-linux-ldso'
;;
bsd )
ccenv_os_semantics='elf-posix-bsd-ldso'
;;
darwin )
ccenv_os_semantics='macho-posix-osx-ldso'
;;
midipix )
ccenv_os_semantics='pe-posix-winnt-ldso'
;;
cygwin )
ccenv_os_semantics='pe-hybrid-winnt-unsafe'
;;
msys )
ccenv_os_semantics='pe-hybrid-winnt-unsafe'
;;
mingw )
ccenv_os_semantics='pe-win32-winnt-unsafe'
;;
esac
if [ -n "$ccenv_os_semantics" ]; then
ccenv_attr_epilog "$ccenv_os_semantics"
return 0
fi
if [ -n "$ccenv_cc_binfmt" ]; then
ccenv_os_semantics_pattern='%s-posix-anyos-unknown'
ccenv_os_semantics=$(printf \
"$ccenv_os_semantics_pattern" \
"$ccenv_cc_binfmt" \
| tr '[:upper:]' '[:lower:]')
else
ccenv_os_semantics='unknown-posix-anyos-unknown'
fi
ccenv_attr_epilog "$ccenv_os_semantics"
}
ccenv_set_os_dso_exrules()
{
ccenv_attr_prolog 'os dso exrules'
case "$ccenv_os" in
midipix )
ccenv_os_dso_exrules='pe-mdso'
;;
* )
if [ "$ccenv_cc_binfmt" = 'PE' ]; then
ccenv_os_dso_exrules='pe-dlltool'
else
ccenv_os_dso_exrules='default'
fi
esac
ccenv_attr_epilog "$ccenv_os_dso_exrules"
}
ccenv_set_os_dso_linkage()
{
# todo: PIC, PIE, and friends
ccenv_attr_prolog 'os linkage'
ccenv_os_dso_linkage='default'
ccenv_attr_epilog "$ccenv_os_dso_linkage"
}
ccenv_set_os_dso_patterns_darwin()
{
ccenv_os_app_prefix=
ccenv_os_app_suffix=
ccenv_os_lib_prefix=lib
ccenv_os_lib_suffix=.dylib
ccenv_os_implib_ext=.invalid
ccenv_os_libdef_ext=.invalid
ccenv_os_archive_ext=.a
ccenv_os_soname=symlink
ccenv_os_lib_prefixed_suffix=
ccenv_os_lib_suffixed_suffix='$(OS_LIB_SUFFIX)'
}
ccenv_set_os_dso_patterns_mdso()
{
ccenv_os_app_prefix=
ccenv_os_app_suffix=
ccenv_os_lib_prefix=lib
ccenv_os_lib_suffix=.so
ccenv_os_implib_ext=.lib.a
ccenv_os_libdef_ext=.so.def
ccenv_os_archive_ext=.a
ccenv_os_soname=symlink
ccenv_os_lib_prefixed_suffix='$(OS_LIB_SUFFIX)'
ccenv_os_lib_suffixed_suffix=
}
ccenv_set_os_dso_patterns_dlltool()
{
ccenv_os_app_prefix=
ccenv_os_app_suffix=.exe
ccenv_os_lib_prefix=lib
ccenv_os_lib_suffix=.dll
ccenv_os_implib_ext=.dll.a
ccenv_os_libdef_ext=.def
ccenv_os_archive_ext=.a
ccenv_os_soname=copy
ccenv_os_lib_prefixed_suffix='$(OS_LIB_SUFFIX)'
ccenv_os_lib_suffixed_suffix=
}
ccenv_set_os_dso_patterns_default()
{
ccenv_os_app_prefix=
ccenv_os_app_suffix=
ccenv_os_lib_prefix=lib
ccenv_os_lib_suffix=.so
ccenv_os_implib_ext=.invalid
ccenv_os_libdef_ext=.invalid
ccenv_os_archive_ext=.a
ccenv_os_soname=symlink
ccenv_os_lib_prefixed_suffix='$(OS_LIB_SUFFIX)'
ccenv_os_lib_suffixed_suffix=
}
ccenv_set_os_dso_patterns()
{
# sover: .so.x.y.z
# verso: .x.y.z.so
case "$ccenv_os" in
darwin )
ccenv_set_os_dso_patterns_darwin
;;
midipix )
ccenv_set_os_dso_patterns_mdso
;;
cygwin | msys | mingw )
ccenv_set_os_dso_patterns_dlltool
;;
* )
ccenv_set_os_dso_patterns_default
;;
esac
}
ccenv_set_os_pe_switches()
{
if [ "$ccenv_cc_binfmt" = 'PE' ] && [ -z "$ccenv_pe_subsystem" ]; then
case "$ccenv_os" in
midipix | mingw )
ccenv_pe_subsystem='windows'
;;
* )
ccenv_pe_subsystem='console'
;;
esac
fi
}
ccenv_output_defs()
{
ccenv_in="$mb_project_dir/sofort/ccenv/ccenv.in"
ccenv_mk="$mb_pwd/ccenv/$ccenv_cfgtype.mk"
ccenv_tmp=
if [ "$ccenv_cc_binfmt" = 'PE' ]; then
ccenv_pe="$mb_project_dir/sofort/ccenv/pedefs.in"
ccenv_in="$ccenv_in $ccenv_pe"
fi
if [ $ccenv_cfgtype = 'native' ]; then
ccenv_tmp=$(mktemp ./tmp_XXXXXXXXXXXXXXXX)
sed \
-e 's/^\s*$/@/g' \
-e 's/^/NATIVE_/' \
-e 's/NATIVE_@//g' \
-e 's/NATIVE_#/#/g' \
-e 's/ =/=/g' \
-e 's/ +=/+=/g' \
$ccenv_in > "$ccenv_tmp"
ccenv_in="$ccenv_tmp"
else
unset ccenv_tmp
fi
ccenv_vars=$(cut -d'=' -f1 "$mb_project_dir/sofort/ccenv/ccenv.vars" \
| grep -v '^#')
ccenv_exvars="ccenv_cfgtype ccenv_makevar_prefix"
ccenv_sed_substs=" \
$(for __var in $(printf '%s' "$ccenv_vars $ccenv_exvars"); do \
printf '%s"$%s"%s' "-e 's/@$__var@/'" \
"$__var" "'/g' "; \
done)"
eval sed $ccenv_sed_substs $ccenv_in \
| sed -e 's/[[:blank:]]*$//g' \
> "$ccenv_mk"
if [ "$ccenv_cfgtype" = 'host' ]; then
for __var in $(printf '%s' "$ccenv_vars"); do
ccenv_src_var=$__var
ccenv_dst_var=mb_${__var#*ccenv_}
ccenv_var_expr='${'$ccenv_src_var':-}'
eval $ccenv_dst_var=$ccenv_var_expr
done
mb_host=$ccenv_host
mb_cchost=$ccenv_cchost
else
for __var in $(printf '%s' "$ccenv_vars"); do
ccenv_src_var=$__var
ccenv_dst_var=mb_native_${__var#*ccenv_}
ccenv_var_expr='${'$ccenv_src_var':-}'
eval "$ccenv_dst_var=$ccenv_var_expr"
done
mb_native_host=$ccenv_host
mb_native_cchost=$ccenv_cchost
fi
if [ -n "${ccenv_tmp:-}" ]; then
rm -f "$ccenv_tmp"
unset ccenv_tmp
fi
}
ccenv_dso_verify()
{
ccenv_str='int foo(int x){return ++x;}'
ccenv_cmd="$ccenv_cc -xc - -shared -o a.out"
rm -f a.out
printf '%s' "$ccenv_str" | $(printf %s "$ccenv_cmd") \
> /dev/null 2>&3 \
|| mb_disable_shared=yes
rm -f a.out
}
ccenv_clean_up()
{
rm -f $ccenv_image
}
ccenv_common_init()
{
. "$mb_project_dir/sofort/ccenv/ccenv.vars"
ccenv_cfgtype=$1
ccenv_cfgfile="$mb_pwd/ccenv/$ccenv_cfgtype.mk"
ccenv_freestd=
ccenv_cchost=
if [ $ccenv_cfgtype = 'native' ]; then
ccenv_makevar_prefix='NATIVE_'
ccenv_image='./ccenv/native.a.out'
else
ccenv_makevar_prefix=
ccenv_image='./ccenv/host.a.out'
fi
if [ $ccenv_cfgtype = 'native' ]; then
ccenv_prefixes=
elif [ -n "$mb_cross_compile" ]; then
ccenv_prefixes="$mb_cross_compile"
elif [ -n "$mb_host" ]; then
ccenv_prefixes="$mb_host-"
else
ccenv_prefixes=
fi
if [ $ccenv_cfgtype = 'host' ]; then
ccenv_tflags=
ccenv_cflags=
ccenv_cflags="$ccenv_cflags $mb_cflags_debug $mb_cflags_config"
ccenv_cflags="$ccenv_cflags $mb_cflags_sysroot $mb_cflags_common"
ccenv_cflags="$ccenv_cflags $mb_cflags_cmdline $mb_cflags"
ccenv_cflags="$ccenv_cflags $mb_cflags_path $mb_cflags_os"
ccenv_cflags="$ccenv_cflags $mb_cflags_site $mb_cflags_strict"
ccenv_cflags="$ccenv_cflags $mb_cflags_last $mb_cflags_once"
ccenv_cc="$mb_user_cc"
ccenv_cpp="$mb_user_cpp"
ccenv_cxx="$mb_user_cxx"
ccenv_pe_subsystem="$mb_pe_subsystem"
ccenv_pe_image_base="$mb_pe_image_base"
else
ccenv_tflags=
ccenv_cflags="$mb_native_cflags"
ccenv_cc="$mb_native_cc"
ccenv_cpp="$mb_native_cpp"
ccenv_cxx="$mb_native_cxx"
ccenv_pe_subsystem="$mb_native_pe_subsystem"
ccenv_pe_image_base="$mb_native_pe_image_base"
fi
}
ccenv_set_characteristics()
{
ccenv_set_cc_host
ccenv_set_cc_bits
ccenv_set_cc_binfmt
ccenv_set_cc_underscore
}
ccenv_set_toolchain_variables()
{
ccenv_common_init $1
ccenv_set_cc
ccenv_set_cpp
ccenv_set_cxx
ccenv_set_primary_tools
ccenv_set_tool_variants
ccenv_set_characteristics
ccenv_set_os
ccenv_set_os_flags
ccenv_set_os_semantics
ccenv_set_os_dso_exrules
ccenv_set_os_dso_linkage
ccenv_set_os_dso_patterns
ccenv_set_os_pe_switches
ccenv_output_defs
ccenv_clean_up
eval 'ccenv_'${ccenv_cfgtype}'_cc'=\'$ccenv_cc\'
}
ccenv_set_host_variables()
{
output_script_status ${mb_script} \
'detect and query host (targeted) system'
ccenv_set_toolchain_variables 'host'
ccenv_dso_verify
}
ccenv_set_native_variables()
{
output_script_status ${mb_script} \
'detect and query native (local build) system'
if [ _$mb_ccenv_skip_native != _yes ]; then
ccenv_set_toolchain_variables 'native'
fi
}
|