/
probe.pm
1144 lines (998 loc) · 29.3 KB
/
probe.pm
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
package build::probe;
use strict;
use warnings;
use File::Path qw(mkpath rmtree);
use File::Spec::Functions qw(curdir catdir rel2abs devnull);
my $devnull = devnull();
my $probe_dir;
END {
rmtree($probe_dir)
if defined $probe_dir;
}
{
package build::probe::restore_cwd;
use Cwd;
sub new {
my $cwd = getcwd;
die "Can't getcwd: $!"
unless defined $cwd && length $cwd;
bless \$cwd;
}
sub DESTROY {
chdir ${$_[0]}
or warn "Can't restore cwd to ${$_[0]}: $!";
}
}
sub _to_probe_dir {
unless (defined $probe_dir) {
$probe_dir = rel2abs(catdir(curdir(), 'probe'));
mkpath($probe_dir);
}
my $restore = build::probe::restore_cwd->new();
chdir $probe_dir
or die "Can't chir $probe_dir: $!";
return $restore;
}
sub compile {
my ($config, $leaf, $defines, $files, $extra_libs) = @_;
my $restore = _to_probe_dir();
my $cl_define = join ' ', map {$config->{ccdef} . $_} @$defines;
my @objs;
foreach my $file ("$leaf.c", @$files) {
(my $obj = $file) =~ s/\.c/$config->{obj}/;
my $command = "$config->{cc} $ENV{CFLAGS} $cl_define $config->{ccout}$obj $config->{ccswitch} $file >$devnull 2>&1";
system $command
and return;
push @objs, $obj;
}
my $libs = join ' ', $config->{ldlibs}, @{$extra_libs // []};
my $command = "$config->{ld} $ENV{LDFLAGS} $config->{ldout}$leaf @objs $libs >$devnull 2>&1";
system $command
and return;
return 1;
}
sub _spew {
my ($filename, $content) = @_;
open my $fh, '>', $filename
or die "Can't open $filename: $!";
print $fh $content
or die "Can't write to $filename: $!";
close $fh
or die "Can't close $filename: $!";
}
sub simple_compile_probe {
my %args = @_;
my ($config, $probing, $code, $key, $invert)
= @args{qw(config probing code key invert)};
my $restore = _to_probe_dir();
_spew('try.c', $code);
print ::dots(' probing ' . $probing);
my $good = compile($config, 'try');
unless ($config->{crossconf}) {
$good &&= !system './try';
}
print $good ? "YES\n": "NO\n";
$good = !$good
if $invert;
$config->{$key} = $good ? 1 : 0;
return;
}
sub compile_probe_first_of {
my %args = @_;
my ($config, $probing, $code, $key, $fallback) = @args{qw(config probing code key fallback)};
my @options = @{$args{options}};
my $restore = _to_probe_dir();
_spew('try.c', $code);
print ::dots(' probing ' . $probing);
my $candidate;
while ($candidate = shift @options) {
next unless compile($config, 'try', ["PROBE_MACRO=$candidate"]);
# If cross compiling we have to assume that the first thing that
# compiles is workable :-(
last if $config->{crossconf};
last if !system "./try";
}
if (defined $candidate) {
print "$candidate\n";
$config->{"has_$key"} = 1;
$config->{$key} = $candidate;
} else {
print "$fallback\n";
$config->{"has_$key"} = 0;
$config->{$key} = "";
}
}
sub compiler_usability {
my ($config) = @_;
my $restore = _to_probe_dir();
my $leaf = 'try';
my $file = "$leaf.c";
_spew('try.c', <<'EOT');
#include <stdlib.h>
int main(int argc, char **argv) {
return EXIT_SUCCESS;
}
EOT
print ::dots(' trying to compile a simple C program');
my ($can_compile, $can_link, $command_errored, $error_message);
(my $obj = $file) =~ s/\.c/$config->{obj}/;
$ENV{CFLAGS} //= '';
my $command = "$config->{cc} $ENV{CFLAGS} $config->{ccout}$obj $config->{ccswitch} $file 2>&1";
my $output = `$command` || $!;
if ($? >> 8 == 0) {
$can_compile = 1;
}
else {
$command_errored = $command;
$error_message = $output;
}
if ($can_compile) {
$ENV{LDFLAGS} //= '';
$command = "$config->{ld} $ENV{LDFLAGS} $config->{ldout}$leaf $obj 2>&1";
$output = `$command` || $!;
if ($? >> 8 == 0) {
$can_link = 1;
}
else {
$command_errored = $command;
$error_message = $output;
}
}
if (!$can_compile || !$can_link) {
die "ERROR\n\n" .
" Can't " . ($can_compile ? 'link' : 'compile') . " simple C program.\n" .
" Failing command: $command_errored\n" .
" Error: $error_message\n\n" .
"Cannot continue after this error.\n" .
"On linux, maybe you need something like 'sudo apt-get install build-essential'.\n" .
"On macOS, maybe you need to install XCode and accept the XCode EULA.\n";
}
print "YES\n";
}
sub static_inline {
my ($config) = @_;
if ($config->{crossconf}) {
# FIXME. Needs testing, but might be robust enough to do what the native
# code does, but just skip the system() to run the executable. Although
# this might get confused by link time optimisations that only fail at
# run time, which the system test does detect.
$config->{static_inline} = 'static';
return;
}
my $restore = _to_probe_dir();
_spew('try.c', <<'EOT');
#include <stdlib.h>
int main(int argc, char **argv) {
#ifdef __GNUC__
return EXIT_SUCCESS;
#else
return EXIT_FAILURE;
#endif
}
EOT
print ::dots(' probing whether your compiler thinks that it is gcc');
compile($config, 'try')
or die "Can't compile simple gcc probe, so something is badly wrong";
my $gcc = !system './try';
print $gcc ? "YES\n": "NO\n";
print ::dots(' probing how your compiler does static inline');
_spew('inline.c', <<'EOCP');
#include <stdlib.h>
extern int f_via_a(int x);
extern int f_via_b(int x);
int main(int argc, char **argv)
{
int y;
y = f_via_a(0);
#ifdef USE_B
y = f_via_b(0);
#endif
if (y == 42) {
return EXIT_SUCCESS;
}
else {
return EXIT_FAILURE;
}
}
EOCP
_spew('a.c', <<'EOCP');
static INLINE int f(int x) {
int y;
y = x + 42;
return y;
}
int f_via_a(int x)
{
return f(x);
}
EOCP
_spew('b.c', <<'EOCP');
extern int f(int x);
int f_via_b(int x)
{
return f(x);
}
EOCP
# For gcc, prefer __inline__, which permits the cflags to add -ansi
my @try = $gcc ? qw(__inline__ inline __inline _inline)
: qw(inline __inline__ __inline _inline);
my $s_i;
while (my $try = shift @try) {
next unless compile($config, 'inline', ["INLINE=$try"], ['a.c']);
next if system "./inline";
# Now make sure there is no external linkage of static functions
if(!compile($config, 'inline', ["INLINE=$try", "USE_B"], ['a.c', 'b.c'])
|| system "./inline") {
$s_i = "static $try";
last;
}
}
if ($s_i) {
print "$s_i\n";
} else {
print "none, so falling back to static\n";
$s_i = 'static';
}
$config->{static_inline} = $s_i;
}
sub thread_local {
my ($config) = @_;
if ($config->{crossconf}) {
$config->{has_thread_local} = 0;
$config->{thread_local} = "";
return;
}
# We don't need to probe for this on Win32, as UV sets it for us
if ($^O eq 'MSWin32') {
$config->{has_thread_local} = 0;
$config->{thread_local} = "";
return;
}
return compile_probe_first_of(config => $config,
probing => 'if your compiler offers thread local storage',
fallback => "it doesn't, so falling back to UV's API",
options => [qw(_Thread_local __thread)],
key => 'thread_local',
code => <<'EOT');
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
static int plus_one = 1;
static int minus_one = -1;
PROBE_MACRO int *minion;
int callback (const void *a, const void *b) {
int val_a = *minion * *(const int *)a;
int val_b = *minion * *(const int *)b;
return val_a < val_b ? -1 : val_a > val_b;
}
#define SIZE 8
void *thread_function(void *arg) {
/* thread local variables should start zeroed in each thread. */
if (minion != NULL) {
fprintf(stderr, "__thread variable started with %p, should be NULL\n",
minion);
exit(2);
}
minion = &minus_one;
int array[SIZE];
unsigned int i;
for (i = 0; i < SIZE; ++i) {
/* "Hash randomisation" - this array isn't in sorted order: */
array[i ^ 5] = i * i;
}
qsort(array, SIZE, sizeof(int), callback);
int bad = 0;
for (i = 0; i < SIZE; ++i) {
int want = (SIZE - 1 - i) * (SIZE - 1 - i);
int have = array[i];
if (want != have) {
++bad;
fprintf(stderr, "array[%u] - want %i, have %i\n", i, want, have);
}
}
if (bad)
exit(3);
return NULL;
}
int main(int argc, char **argv) {
if (minion != NULL) {
fprintf(stderr, "__thread variable started with %p, should be NULL\n",
minion);
exit(4);
}
minion = &plus_one;
pthread_t tid;
int result = pthread_create(&tid, NULL, thread_function, NULL);
if (result) {
fprintf(stderr, "pthread_create failed (%d)\n", result);
exit(5);
}
result = pthread_join(tid, NULL);
if (result) {
fprintf(stderr, "pthread_join failed (%d)\n", result);
exit(6);
}
if (minion == NULL) {
fprintf(stderr, "__thread variable should not be NULL\n");
exit(7);
}
if (!(minion == &plus_one && *minion == 1)) {
fprintf(stderr, "__thread variable should be %d @ %p, not %d @ %p\n",
1, &plus_one, *minion, minion);
exit(8);
}
return 0;
}
EOT
}
my @floating_point_vals = qw(zero one nan inf neg_inf neg_zero
int_less_than_minus_one int_more_than_one
roughly_ten roughly_minus_ten);
my $floating_point_main = <<'EOT';
#include <math.h>
#include <stdio.h>
int main(int argc, char **argv) {
/* Hopefully these games defeat the optimiser, such that we call the runtime
* library pow, instead of having the C compiler optimiser constant fold it.
* Without this (for me on Solaris with gcc)
* pow(1.0, NaN) will be constant folded always
* pow(1.0, Inf) will be constant folded if optimisation is enabled
*/
double zero = argv[0][0] == '\0';
double one = zero + 1.0;
double int_less_than_minus_one = 34 - argv[0][0];
double int_more_than_one = argv[0][0] - 30;
double inf = int_more_than_one * pow(10.0, pow(10.0, 100));
double neg_inf = int_less_than_minus_one * pow(10.0, pow(10.0, 100));
double neg_zero = one / neg_inf;
double roughly_ten = atan2(zero, -one) * atan2(zero, -one);
double roughly_minus_ten = -roughly_ten;
double nan = sqrt(roughly_minus_ten);
if (zero) {
#ifdef CHATTY
fprintf(stderr, "Can't generate zero - get %g\n", zero);
#endif
return 1;
}
if (nan == nan) {
#ifdef CHATTY
fprintf(stderr, "Can't generate NaN - get %g\n", nan);
#endif
return 2;
}
if (! (inf > 0.0)) {
#ifdef CHATTY
fprintf(stderr, "Can't generate Inf - get %g\n", inf);
#endif
return 3;
}
if (inf != 2.0 * inf) {
#ifdef CHATTY
fprintf(stderr, "Can't generate Inf - get %g\n", inf);
#endif
return 4;
}
if (! (neg_inf < 0.0)) {
#ifdef CHATTY
fprintf(stderr, "Can't generate -Inf - get %g\n", neg_inf);
#endif
return 5;
}
if (neg_inf != 2.0 * neg_inf) {
#ifdef CHATTY
fprintf(stderr, "Can't generate -Inf - get %g\n", neg_inf);
#endif
return 6;
}
if (neg_zero != 0.0) {
fprintf(stderr, "Can't generate -0.0 - get %g\n", neg_zero);
return 7;
}
if (1.0 / neg_zero != neg_inf) {
fprintf(stderr, "Can't generate -0.0 - get %g\n", neg_zero);
return 8;
}
EOT
sub substandard_pow {
my ($config) = @_;
if ($config->{crossconf}) {
# A guess, but so far only pre-C99 Solaris has failed this:
$config->{has_substandard_pow} = 0;
return;
}
return simple_compile_probe(config => $config,
probing => 'if your pow() handles NaN and Inf corner cases',
invert => 1,
key => 'has_substandard_pow',
code => $floating_point_main . <<'EOT');
/* Solaris is documented not to be conformant with SUSv3 (which I think
* is POSIX 2001) unless
* "the application was compiled with the c99 compiler driver"
* which as best I can tell gcc doesn't, even with -std=c99 */
double got = pow(one, nan);
if (got != one) {
#ifdef CHATTY
fprintf(stderr, "1.0, NaN: pow(%g, %g) is %g, not 1.0\n", one, nan, got);
#endif
return 11;
}
got = pow(one, inf);
if (got != one) {
#ifdef CHATTY
fprintf(stderr, "1.0, Inf: pow(%g, %g) is %g, not 1.0\n", one, inf, got);
#endif
return 12;
}
got = pow(one, neg_inf);
if (got != one) {
#ifdef CHATTY
fprintf(stderr, "1.0, -Inf: pow(%g, %g) is %g, not 1.0\n", one, neg_inf, got);
#endif
return 13;
}
/* However, Solaris does state that unconditionally:
* For any value of x (including NaN), if y is +0, 1.0 is returned.
* (without repeating that caveat about the c99 compiler driver)
* and yet behaviour of gcc and *Solaris* studio is consistent in returning
* NaN for pow(NaN, 0.0). Oracle studio seems to default to c99.
*/
got = pow(nan, zero);
if (got != one) {
#ifdef CHATTY
fprintf(stderr, "NaN, 0.0: pow(%g, %g) is %g, not 1.0\n", nan, zero, got);
#endif
return 14;
}
/* Not seen either of these fail anywhere, but let's test anyway: */
got = pow(inf, zero);
if (got != one) {
#ifdef CHATTY
fprintf(stderr, "Inf, 0.0: pow(%g, %g) is %g, not 1.0\n", inf, zero, got);
#endif
return 15;
}
got = pow(neg_inf, zero);
if (got != one) {
#ifdef CHATTY
fprintf(stderr, "-Inf, 0.0: pow(%g, %g) is %g, not 1.0\n", neg_inf, zero, got);
#endif
return 16;
}
/* Negative odd integers preserve sign */
got = pow(zero, -3.0);
if (!(got > 0)) {
#ifdef CHATTY
fprintf(stderr, "0.0, -3.0: pow(%g, -3.0) is %g, not +Inf\n", zero, got);
#else
return 17;
#endif
}
if (got != 2 * got) {
#ifdef CHATTY
fprintf(stderr, "0.0, -3.0: pow(%g, -3.0) is %g, not +Inf\n", zero, got);
#else
return 18;
#endif
}
got = pow(neg_zero, -3.0);
if (!(got < 0)) {
#ifdef CHATTY
fprintf(stderr, "-0.0, -3.0: pow(%g, -3.0) is %g, not -Inf\n", neg_zero, got);
#else
return 19;
#endif
}
if (got != 2 * got) {
#ifdef CHATTY
fprintf(stderr, "-0.0, -3.0: pow(%g, -3.0) is %g, not -Inf\n", neg_zero, got);
#else
return 20;
#endif
}
/* Everything else is positive infinity. */
got = pow(zero, -2.0);
if (!(got > 0)) {
#ifdef CHATTY
fprintf(stderr, "0.0, -2.0: pow(%g, -2.0) is %g, not +Inf\n", zero, got);
#else
return 21;
#endif
}
if (got != 2 * got) {
#ifdef CHATTY
fprintf(stderr, "0.0, -2.0: pow(%g, -2.0) is %g, not +Inf\n", zero, got);
#else
return 21;
#endif
}
got = pow(neg_zero, -2.0);
if (!(got > 0)) {
#ifdef CHATTY
fprintf(stderr, "-0.0, -2.0: pow(%g, -2.0) is %g, not +Inf\n", neg_zero, got);
#else
return 22;
#endif
}
if (got != 2 * got) {
#ifdef CHATTY
fprintf(stderr, "-0.0, -2.0: pow(%g, -2.0) is %g, not +Inf\n", neg_zero, got);
#else
return 23;
#endif
}
got = pow(zero, -2.78);
if (!(got > 0)) {
#ifdef CHATTY
fprintf(stderr, "0.0, -2.78: pow(%g, -2.78) is %g, not +Inf\n", zero, got);
#else
return 24;
#endif
}
if (got != 2 * got) {
#ifdef CHATTY
fprintf(stderr, "0.0, -2.78: pow(%g, -2.78) is %g, not +Inf\n", zero, got);
#else
return 25;
#endif
}
got = pow(neg_zero, -2.78);
if (!(got > 0)) {
#ifdef CHATTY
fprintf(stderr, "-0.0, -2.78: pow(%g, -2.78) is %g, not +Inf\n", neg_zero, got);
#else
return 26;
#endif
}
if (got != 2 * got) {
#ifdef CHATTY
fprintf(stderr, "-0.0, -2.78: pow(%g, -2.78) is %g, not +Inf\n", neg_zero, got);
#else
return 27;
#endif
}
return 0;
}
EOT
}
sub substandard_log {
my ($config) = @_;
if ($config->{crossconf}) {
# A guess, but so far only pre-C99 Solaris has failed this:
$config->{has_substandard_log} = 0;
$config->{has_substandard_log10} = 0;
return;
}
for my $fn (qw(log log10)) {
simple_compile_probe(config => $config,
probing => "if your $fn() returns NaN for negative values",
invert => 1,
key => "has_substandard_$fn",
code => $floating_point_main . <<"EOT");
double got = $fn(neg_inf);
if (got == got) {
#ifdef CHATTY
fprintf(stderr, "-Inf: $fn(%g) is %g, not NaN\\n", neg_inf, got);
#else
return 11;
#endif
}
got = $fn(int_less_than_minus_one);
if (got == got) {
#ifdef CHATTY
fprintf(stderr, "minus something: $fn(%g) is %g, not NaN\\n", int_less_than_minus_one, got);
#else
return 12;
#endif
}
return 0;
}
EOT
}
}
sub substandard_trig {
my ($config) = @_;
if ($config->{crossconf}) {
# A guess, but so far only pre-C99 Solaris has failed this:
$config->{has_substandard_asin} = 0;
$config->{has_substandard_acos} = 0;
return;
}
for my $fn (qw(asin acos)) {
simple_compile_probe(config => $config,
probing => "if your $fn() returns NaN for negative values",
invert => 1,
key => "has_substandard_$fn",
code => $floating_point_main . <<"EOT");
double got = $fn(int_more_than_one);
if (got == got) {
#ifdef CHATTY
fprintf(stderr, "int_more_than_one: $fn(%g) is %g, not NaN\\n", int_more_than_one, got);
#else
return 11;
#endif
}
got = $fn(inf);
if (got == got) {
#ifdef CHATTY
fprintf(stderr, "Inf: $fn(%g) is %g, not NaN\\n", inf, got);
#else
return 12;
#endif
}
got = $fn(int_less_than_minus_one);
if (got == got) {
#ifdef CHATTY
fprintf(stderr, "int_less_than_minus_one: $fn(%g) is %g, not NaN\\n", int_less_than_minus_one, got);
#else
return 13;
#endif
}
got = $fn(neg_inf);
if (got == got) {
#ifdef CHATTY
fprintf(stderr, "-Inf: $fn(%g) is %g, not NaN\\n", neg_inf, got);
#else
return 14;
#endif
}
return 0;
}
EOT
}
}
sub has_isinf_and_isnan {
my ($config) = @_;
my @probes = (
[ isnan => 'nan' ],
[ isinf => qw(inf neg_inf) ],
[ signbit => qw(neg_inf neg_zero int_less_than_minus_one roughly_minus_ten)],
);
if ($config->{crossconf}) {
# A guess
for (@probes) {
my $func = $_->[0];
$config->{"has_$func"} = 0;
}
return;
}
for (@probes) {
my ($func, @vals) = @$_;
my %true;
@true{@vals} = (1) x @vals;
my $code = $floating_point_main;
my $exit = 11;
for my $val (@floating_point_vals) {
# NaNs can actually have sign bits, so the sign bit might/might not
# be set, and we can't know (and shouldn't care):
next
if $func eq 'signbit' && $val eq 'nan';
my $want = $true{$val} ? 'T' : 'f';
$code .= <<"EOT";
{
int want = '$want';
int have = $func($val) ? 'T' : 'f';
if (want != have) {
#ifdef CHATTY
fprintf(stderr, "$func($val): Have %c, Want $want\\n", have);
#else
return $exit;
#endif
}
}
EOT
++$exit;
}
$code .= <<"EOT";
return 0;
}
EOT
simple_compile_probe(config => $config,
probing => "if you have $func",
key => "has_$func",
code => $code);
}
return;
}
sub specific_werror {
my ($config) = @_;
my $restore = _to_probe_dir();
if ($config->{cc} ne 'gcc') {
return;
}
my $file = 'try.c';
_spew($file, <<'EOT');
#include <stdlib.h>
int main(int argc, char **argv) {
return EXIT_SUCCESS;
}
EOT
print ::dots(' probing support of -Werror=*');
(my $obj = $file) =~ s/\.c/$config->{obj}/;
my $command = "gcc -Werror=declaration-after-statement $config->{ccout}$obj try.c >$devnull 2>&1";
my $can_specific_werror = !( system $command );
print $can_specific_werror ? "YES\n": "NO\n";
$config->{can_specific_werror} = $can_specific_werror || 0
}
sub _gen_unaligned_access {
my ($config, $can) = @_;
my @align = qw(int32 int64 num64);
my $no_msg = "your CPU can't";
if ($config->{cflags} =~ /\B-fsanitize=undefined\b/) {
$can = '';
$no_msg = "with UBSAN we won't";
}
if ($can eq 'all') {
++$config->{"can_unaligned_$_"}
foreach @align;
print " your CPU can read unaligned values for all of @align\n";
} else {
my %can;
++$can{$_}
for split ' ', $can;
$config->{"can_unaligned_$_"} = $can{$_} || 0
foreach @align;
if ($can) {
print " your CPU can read unaligned values for only $can\n";
} else {
print " $no_msg read unaligned values for any of @align\n";
}
}
}
sub unaligned_access {
my ($config) = @_;
if ($config->{crossconf}) {
_gen_unaligned_access($config, '');
} elsif ($^O eq 'MSWin32') {
# Needs FIXME for Windows on ARM, but not sure how to detect that
_gen_unaligned_access($config, 'all');
} else {
# AIX:
# uname -m: 00F84C0C4C00
# uname -p: powerpc
# HP/UX
# uname -m: 9000/800
# (but will be ia64 on Itanium)
# uname -p illegal
# Solaris
# uname -m: i86pc
# uname -p: i386
# FreeBSD
# uname -m: amd64
# uname -p: amd64
# NetBSD
# uname -m: amd64
# uname -p: x86_64
# OpenBSD
# uname -m: amd64
# uname -p: amd64
# Assuming that the 50 other *BSD variants are forks of the 3 above
# Linux
# uname -p can return 'unknown'
my $flag;
if ($^O eq 'aix' || $^O eq 'solaris') {
$flag = '-p';
} else {
$flag = '-m';
}
my $command = "uname $flag";
my $arch = `$command`;
if (defined $arch) {
chomp $arch;
if ($arch =~ /^(?:x86_64|amd64|i[0-9]86)$/) {
# Don't know alignment constraints for ARMv8
_gen_unaligned_access($config, 'all');
} elsif ($arch =~ /armv(?:6|7)/) {
_gen_unaligned_access($config, 'int32');
} else {
# ARMv5 and earlier do "interesting" things on unaligned 32 bit
# For other architectures, play it safe by default.
# Updates welcome.
_gen_unaligned_access($config, '');
}
} else {
print STDERR "Problem running $command, so assuming no unaligned access\n";
}
}
}
# It would be good to find a robust way to do this without needing to *run* the
# compiled code. At which point we could also use it for the native build.
sub ptr_size {
my ($config) = @_;
if ($config->{crossconf}) {
warn "Guessing :-(";
$config->{ptr_size} = 4;
return;
}
my $restore = _to_probe_dir();
_spew('try.c', <<'EOT');
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
printf("%u\n", (unsigned int) sizeof(void *));
return EXIT_SUCCESS;
}
EOT
print ::dots(' probing the size of pointers');
compile($config, 'try')
or die "Can't compile simple probe, so something is badly wrong";
my $size = `./try`;
die "Unable to run probe, so something is badly wrong"
unless defined $size;
chomp $size;
die "Probe gave nonsensical answer '$size', so something it badly wrong"
unless $size =~ /\A[0-9]+\z/;
print "$size\n";
$config->{ptr_size} = $size;
}
sub computed_goto {
my ($config) = @_;
return simple_compile_probe(config => $config,
probing => 'computed goto support',
key => 'cancgoto',
code => <<'EOT');
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
void *cgoto_ptr;
cgoto_ptr = &&cgoto_label;
goto *cgoto_ptr;
return EXIT_FAILURE;
cgoto_label:
return EXIT_SUCCESS;
}
EOT
}
sub check_fn_malloc_trim {
my ($config) = @_;
# Seems that this was written with a plan to be generic, but so far nothing
# else has wanted it.
my $includes = '#include <malloc.h>';
my $function = 'malloc_trim(0)';
return simple_compile_probe(config => $config,
probing => 'existance of optional malloc_trim()',
key => 'has_fn_malloc_trim',
code => <<"EOT");
$includes
int main(int argc, char **argv) {
$function;
return 0;
}
EOT
}
sub C_type_bool {
my ($config) = @_;
return compile_probe_first_of(config => $config,
probing => 'C type support for booleans',
fallback => '(none found)',
options => [qw(_Bool bool)],
key => 'booltype',
code => <<'EOT');
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>