Debian Codegraph @

test/symbols-renames.pl

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 # Check that all of the symbols renamed by crypt-port.h
10 # still appear somewhere in the source code. This test does not attempt
11 # to parse the source code, so it can get false negatives (e.g. a word used
12 # in a comment will be enough).
13 #
14 # Due to limitations in Automake, this program takes parameters from
15 # the environment:
16 # $lib_la - full pathname of libcrypt.la
17 # $SYMBOL_PREFIX - prefix, if any, added to global symbols defined from C
18 # $NM, $CPP, $CPPFLAGS - nm utility, C preprocessor, and parameters
19
20 use v5.14; # implicit use strict, use feature ':5.14'
21 use warnings FATAL => 'all';
22 use utf8;
23 use open qw(:std :utf8);
24 no if $] >= 5.022, warnings => 'experimental::re_strict';
25 use if $] >= 5.022, re => 'strict';
26
27 use File::Temp ();
28
29 use FindBin ();
30 use lib $FindBin::Bin;
31 use TestCommon qw(
32 compare_symbol_lists
33 ensure_C_locale
34 find_real_library
35 get_symbols
36 popen
37 sh_split
38 skip
39 subprocess_error
40 which
41 );
42
43 sub list_library_internals {
44 # We are only interested in symbols with the internal prefix,
45 # _crypt_.
46 return get_symbols(find_real_library(shift, 'static'),
47 sub { $_[0] =~ /^_crypt_/ });
48 }
49
50 sub list_symbol_renames {
51 state @CPP;
52 if (!@CPP) {
53 @CPP = which($ENV{CPP} || 'cc -E');
54 skip('C compiler not available') unless @CPP;
55 }
56 state @CPPFLAGS;
57 if (!@CPPFLAGS) {
58 @CPPFLAGS = sh_split($ENV{CPPFLAGS} || q{});
59 }
60
61 my $tmp = File::Temp->new(
62 DIR => '.',
63 TEMPLATE => 'symbols-renames-XXXXXX',
64 SUFFIX => '.c',
65 EXLOCK => 0,
66 );
67 print {$tmp} qq{#include "crypt-port.h"\n};
68
69 my $fh = popen('-|', @CPP, @CPPFLAGS, '-dD', $tmp->filename);
70 local $_;
71 my %symbols;
72 my $pp_define = qr{
73 ^\#define \s+
74 [a-zA-Z_][a-zA-Z0-9_(),]* \s+
75 (_crypt_[a-zA-Z0-9_]*) \b
76 }x;
77 while (<$fh>) {
78 chomp;
79 s/\s+$//;
80 if ($_ =~ $pp_define) {
81 print {*STDERR} "| $1\n";
82 $symbols{$1} = 1;
83 }
84 }
85 close $fh or subprocess_error($CPP[0]);
86 return \%symbols;
87 }
88
89 #
90 # Main
91 #
92 my $lib_la = $ENV{lib_la} || '/nonexistent';
93 if (!-f $lib_la) {
94 print {*STDERR} "usage: lib_la=/path/to/library.la $0";
95 exit 1;
96 }
97 if (($ENV{HAVE_CPP_dD} // 'yes') eq 'no') {
98 skip('cpp -dD not available');
99 }
100
101 ensure_C_locale();
102 exit compare_symbol_lists(
103 list_library_internals($lib_la),
104 list_symbol_renames(),
105 'renames',
106 0, # extra symbols not allowed
107 );