Debian Codegraph @

build-aux/scripts/compute-symver-floor

raw
1 #! /usr/bin/perl
2 # Written by Zack Weinberg <zackw at panix.com> in 2017 and 2020.
3 # To the extent possible under law, Zack Weinberg has waived all
4 # copyright and related or neighboring rights to this work.
5 #
6 # See https://creativecommons.org/publicdomain/zero/1.0/ for further
7 # details.
8
9 # Process libcrypt.minver and determine the minumum symbol version to
10 # use for compatibility symbols. Called from configure.ac.
11
12 use v5.14; # implicit use strict, use feature ':5.14'
13 use warnings FATAL => 'all';
14 use utf8;
15 use open qw(:std :utf8);
16 no if $] >= 5.022, warnings => 'experimental::re_strict';
17 use if $] >= 5.022, re => 'strict';
18
19 use FindBin ();
20 use lib ${FindBin::Bin};
21 use BuildCommon qw(
22 ensure_C_locale
23 sh_split
24 popen
25 subprocess_error
26 which
27 );
28
29 sub preprocessor_check {
30 my ($expr) = @_;
31
32 state @CC;
33 state @CFLAGS;
34 if (!@CC) {
35 @CC = which($ENV{CC} // 'cc');
36 die "C compiler not available\n" unless @CC;
37
38 @CFLAGS = sh_split($ENV{CFLAGS} // q{});
39
40 # Remove empty elements, particularly leading ones which
41 # cause issues with popen below.
42 @CFLAGS = grep { $_ } @CFLAGS;
43
44 # We call ensure_C_locale here, not from the main section,
45 # because this sub might not get called at all, in which
46 # case it would be unnecessary work.
47 ensure_C_locale();
48 }
49
50 # FIXME: We assume the compiler understands "-fsyntax-only -xc -"
51 # to mean "read standard input, interpret as C, check for errors,
52 # produce no output." This is true for all commonly used C
53 # compilers on operating systems where this feature is necessary.
54 print {*STDERR} "${FindBin::Bin}: testing $expr\n";
55
56 my $ccpipe = popen('|-', @CC, @CFLAGS, '-fsyntax-only', '-xc', '-');
57 print {$ccpipe} <<"EOF";
58 #include <limits.h>
59 #if !($expr)
60 #error nope
61 #endif
62 int avoid_empty_translation_unit;
63 EOF
64 if (close $ccpipe) {
65 return 1; # compilation successful
66 } elsif ($! == 0 && ($? & 0x7F) == 0) {
67 return 0; # compilation failed
68 } else {
69 subprocess_error(@CC);
70 }
71 }
72
73 sub parse_minver_file {
74 my ($minver_file, $system, $cpu) = @_;
75 $system = lc $system;
76 $cpu = lc $cpu;
77
78 local $_;
79 open my $fh, '<', $minver_file
80 or die "$minver_file: $!\n";
81 while (<$fh>) {
82 next if /^#/;
83 chomp;
84 s/\s+$//;
85 next if $_ eq q{};
86
87 my ($vers, $vsys, $vcpu, $ppck) = split q{ }, $_, 4;
88 $vsys = lc $vsys;
89 $vcpu = lc $vcpu;
90
91 return $vers
92 if $system =~ /\A$vsys/
93 && $cpu =~ /\A$vcpu/
94 && (!defined $ppck || preprocessor_check($ppck));
95 }
96 print {*STDERR} "$0: no match for ${cpu}-${system}\n";
97 return 'ERROR';
98 }
99
100 #
101 # Main
102 #
103 if (scalar(@ARGV) != 3) {
104 print {*STDERR} "usage: $0 libcrypt.minver host-os host-cpu\n";
105 print {*STDERR} "Environment variables \$CC and \$CFLAGS are honored.\n";
106 exit 2;
107 }
108
109 exit 0 if eval {
110 print parse_minver_file(@ARGV), "\n";
111 close STDOUT or die "write error: $!\n";
112 1;
113 };
114
115 print {*STDERR} "${FindBin::Script}: $@";
116 exit 1;