summaryrefslogtreecommitdiff
path: root/tests/mktests.pl
blob: 2571ef21fea173e491bff445b1cd17ab4ec1a269 (plain)
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
#! /usr/bin/perl

# Build some of the Autoconf test files.

# Copyright (C) 2000-2017, 2020 Free Software Foundation, Inc.

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

use 5.006;
use strict;
use warnings FATAL => 'all';

# ac_exclude_list
# ---------------
# Check all AC_DEFUN'ed macros with AT_CHECK_MACRO, except these.
# Not every macro can be run without arguments, and some are already
# tested elsewhere.
my @ac_exclude_list = (
  # Internal macros are used elsewhere.
  qr/^_?_AC_/,

  # Used in many places.
  qr/^AC_.*_IFELSE$/,
  qr/^AC_LANG/,
  qr/^AC_RUN_LOG$/,
  qr/^AC_TRY/,

  # Need an argument.
  qr/^AC_(CANONICALIZE|PREFIX_PROGRAM|PREREQ)$/,
  qr/^AC_(SEARCH_LIBS|REPLACE_FUNCS)$/,
  qr/^AC_(CACHE_CHECK|COMPUTE)_INT$/,
  qr/^AC_ARG_VAR$/,
  qr/^AC_REQUIRE_SHELL_FN$/,

  # Performed in the semantics tests.
  qr/^AC_CHECK_(
        ALIGNOF|DECL|FILE|FUNC|HEADER|LIB|MEMBER
       |PROG|SIZEOF|(TARGET_)?TOOL|TYPE
     )S?$/x,
  qr/^AC_PATH_PROGS_FEATURE_CHECK$/,

  # Fail when the source does not exist.
  qr/^AC_CONFIG/,

  # AU defined to nothing.
  qr/^AC_(CYGWIN|CYGWIN32|EMXOS2|MING32|EXEEXT|OBJEXT)$/,

  # Produce "= val" because $1, the variable used to store the result,
  # is empty.
  qr/^AC_(F77|FC)_FUNC$/,
  qr/^AC_FC_(PP_)?SRCEXT$/,
  qr/^AC_PATH_((TARGET_)?TOOL|PROG)S?$/,

  # Is a number.
  qr/^AC_FD_CC$/,

  # Obsolete, but needs to be AC_DEFUNed and cannot safely be called
        # without arguments.  Tested in tools.at.
  qr/^AC_(DIAGNOSE|FATAL|FOREACH|OBSOLETE|WARNING)$/,

  # Require a file that is not shipped with Autoconf.  But it should.
  qr/^AC_FUNC_(GETLOADAVG|FNMATCH_GNU)$/,
  qr/^AC_REPLACE_FNMATCH$/,

  # Obsolete, checked in semantics.
  qr/^AC_FUNC_WAIT3$/,
  qr/^AC_FUNC_SETVBUF_REVERSED$/,
  qr/^AC_SYS_RESTARTABLE_SYSCALLS$/,

  # Not intended to be invoked at the top level.
  qr/^AC_INCLUDES_DEFAULT$/,

  # AC_INIT includes all the AC_INIT macros.
  # There is an infinite m4 recursion if AC_INIT is used twice.
  qr/^AC_INIT/,

  # Checked in semantics.
  qr/^AC_(PROG_CC|C_CONST|C_VOLATILE)$/,
  qr/^AC_PATH_XTRA$/,

  # Use without an argument is obsolete.
  # Checked in semantics.
  qr/^AC_PROG_LEX$/,

  # Requires a working C++ compiler, which is not a given.
  qr/^AC_PROG_CXX_C_O$/,

  # Already tested by AT_CHECK_MACRO.
  qr/^AC_OUTPUT$/,

  # Tested alongside m4_divert_text.
  qr/^AC_PRESERVE_HELP_ORDER$/,

  # Tested in erlang.at.
  qr/^AC_ERLANG_SUBST_(INSTALL_LIB_SUBDIR|ROOT_DIR)$/,
  qr/^AC_ERLANG_CHECK_LIB$/,
);

# au_exclude_list
# ---------------
# Check all AU_DEFUN'ed macros with AT_CHECK_AU_MACRO, except these.
my @au_exclude_list = (
  # Empty.
  qr/^AC_(C_CROSS|PROG_CC_(C[89]9|STDC))$/,

  # Use AC_REQUIRE.
  qr/^AC_(CYGWIN|MINGW32|EMXOS2)$/,

  # Already in configure.ac.
  qr/^AC_(INIT|OUTPUT)$/,

  # AC_LANG_SAVE needs user interaction to be removed.
  # AC_LANG_RESTORE cannot be used alone.
  qr/^AC_LANG_(SAVE|RESTORE)$/,

  # Need arguments.  Tested in tools.at.
  qr/^AC_(DIAGNOSE|FATAL|OBSOLETE|WARNING)$/,
  qr/^AC_(FOREACH|LINK_FILES|PREREQ)$/,

  # Not macros, just mapping from old variable name to a new one.
  qr/^ac_cv_prog_(gcc|gxx|g77)$/,
);


# skip_macro MACRO, EXCLUDE-LIST
# ------------------------------
# Returns truthy if any of the regexes in EXCLUDE-LIST match MACRO.
sub skip_macro
{
  my $macro = shift;
  for my $pat (@_)
    {
      return 1 if $macro =~ $pat;
    }
  return 0;
}


# scan_m4_files
# -------------
# Scan all of the Autoconf source files and produce lists of macros
# to be tested
sub scan_m4_files
{
  my @macros_to_test;
  my %required_macros;

  for my $file (@_)
    {
      open my $fh, "<", $file
        or die "$file: $!\n";

      my (@ac_macros, @au_macros);
      while (<$fh>)
        {
          chomp;
          s/\bdnl\b.*$//;
          if (/\bAC_REQUIRE\(\[*([a-zA-Z0-9_]+)/)
            {
              $required_macros{$1} = 1;
            }
          elsif (/^AC_DEFUN(?:_ONCE)?\(\[*([a-zA-Z0-9_]+)/)
            {
              push @ac_macros, $1
                unless skip_macro $1, @ac_exclude_list;
            }
          elsif (/^AU_DEFUN?\(\[*([a-zA-Z0-9_]+)/)
            {
              push @au_macros, $1
                unless skip_macro $1, @au_exclude_list;
            }
        }
      push @macros_to_test, [ $file, \@ac_macros, \@au_macros ];
    }

  # Filter out macros that are AC_REQUIREd by some other macro;
  # it's not necessary to test them directly.
  my @pruned_macros_to_test;
  for my $elt (@macros_to_test)
    {
      my ($file, $ac_macros, $au_macros) = @$elt;
      my (@pruned_ac_macros, @pruned_au_macros);

      for my $macro (@$ac_macros)
        {
          push @pruned_ac_macros, $macro
            unless defined $required_macros{$macro};
        }
      for my $macro (@$au_macros)
        {
          push @pruned_au_macros, $macro
            unless defined $required_macros{$macro};
        }

      push @pruned_macros_to_test, [
        $file, \@pruned_ac_macros, \@pruned_au_macros
      ];
    }

  return @pruned_macros_to_test;
}


# make_read_only FILE
# -------------------
# Make FILE read-only on disk.  Also clears the execute and special bits.
sub make_read_only
{
  my $f = shift;
  my $mode = (stat $f)[2];
  die "stat($f): $!\n" unless defined $mode;
  # clear all the bits in $mode except r--r--r--
  $mode &= 00444;
  chmod $mode, $f or die "making $f read-only: $!\n";
}


# create_test_files OUTDIR, M4-FILES...
# -----------------
# Main loop: for each file listed in M4-FILES, generate an .at file
# named "ac${file}.at" that does cursory tests on each of the macros
# defined in $file.
sub create_test_files
{
  my $outdir = shift;
  if (@_ == 0)
    {
      print STDERR "usage: $0 outdir m4-files...\n";
      exit 1;
    }

  for my $elt (scan_m4_files @_)
    {
      my ($file, $ac_macros, $au_macros) = @$elt;

      my $base = $file;
      $base =~ s|^.*/([^/.]+)(?:.[^/]*)?$|$1|;

      my $tmpout = "${outdir}/ac${base}.tat";
      my $out = "${outdir}/ac${base}.at";

      open my $fh, ">", $tmpout
        or die "$tmpout: $!\n";

      print $fh <<"EOF";
# -*- autotest -*-
# Generated by $0 from $file.
# Do not edit this file by hand.

EOF

      if (@$ac_macros || @$au_macros)
        {
          print $fh "AT_BANNER([Testing autoconf/$base macros.])\n";

          if (@$ac_macros)
            {
              print $fh "\n# Modern macros.\n";
              print $fh "AT_CHECK_MACRO([$_])\n" for sort @$ac_macros;
            }
          if (@$au_macros)
            {
              print $fh "\n# Obsolete macros.\n";
              print $fh "AT_CHECK_AU_MACRO([$_])\n" for sort @$au_macros;
            }
        }
      else
        {
          print $fh "# Nothing to test.\n";
        }

      close $fh or die "writing to $tmpout: $!\n";
      make_read_only $tmpout;
      rename $tmpout, $out or die "updating $out: $!\n";
    }
}

create_test_files @ARGV;
exit 0;

### Setup "GNU" style for perl-mode and cperl-mode.
## Local Variables:
## perl-indent-level: 2
## perl-continued-statement-offset: 2
## perl-continued-brace-offset: 0
## perl-brace-offset: 0
## perl-brace-imaginary-offset: 0
## perl-label-offset: -2
## cperl-indent-level: 2
## cperl-brace-offset: 0
## cperl-continued-brace-offset: 0
## cperl-label-offset: -2
## cperl-extra-newline-before-brace: t
## cperl-merge-trailing-else: nil
## cperl-continued-statement-offset: 2
## End: