This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Bump version to 5.33.5
[perl5.git] / Porting / config_h.pl
1 #!/usr/bin/perl
2
3 # This script reorders config_h.SH after metaconfig
4 # Changing metaconfig is too complicated
5 #
6 # This script is run just after metaconfig, and it
7 # is run ONLY ONCE. Not to be used afterwards
8 #
9 # Copyright (C) 2005-2020 by H.Merijn Brand (m)'20 [23-08-2020]
10 #
11 # You may distribute under the terms of either the GNU General Public
12 # License or the Artistic License, as specified in the README file.
13
14 use strict;
15 use warnings;
16
17 my ($cSH, $ch, @ch, %ch) = ("config_h.SH");
18 open $ch, "<", $cSH or die "Cannot open $cSH: $!\n";
19 {   local $/ = "\n\n";
20     @ch = <$ch>;
21     close  $ch;
22     }
23
24 sub ch_index {
25     %ch = ();
26     foreach my $ch (0 .. $#ch) {
27         while ($ch[$ch] =~ m{^/\* ([A-Z]\w+)}gm) {
28             $ch{$1} = $ch;
29             }
30         }
31     } # ch_index
32
33 my %dep = (
34     # This symbol must be defined BEFORE ...
35     BYTEORDER           => [ qw( UVSIZE                         ) ],
36     LONGSIZE            => [ qw( BYTEORDER                      ) ],
37     MULTIARCH           => [ qw( BYTEORDER MEM_ALIGNBYTES       ) ],
38     HAS_QUAD            => [ qw( I64TYPE                        ) ],
39     HAS_GETGROUPS       => [ qw( Groups_t                       ) ],
40     HAS_SETGROUPS       => [ qw( Groups_t                       ) ],
41     );
42
43 my $changed;
44 do {
45     $changed = 0;
46     foreach my $sym (keys %dep) {
47         ch_index ();
48         foreach my $dep (@{$dep{$sym}}) {
49             print STDERR "Check if $sym\t($ch{$sym}) precedes $dep\t($ch{$dep})\n";
50             $ch{$sym} < $ch{$dep} and next;
51             my $ch = splice @ch, $ch{$sym}, 1;
52             splice @ch, $ch{$dep}, 0, $ch;
53             $changed++;
54             ch_index ();
55             }
56         }
57     } while ($changed);
58
59 # 30327
60 for (grep m{echo .Extracting \$CONFIG_H} => @ch) {
61     my $case = join "\n",
62         qq{case "\$CONFIG_H" in},
63         qq{already-done) echo "Not re-extracting config.h" ;;},
64         qq{*)}, "";
65     s{^(?=echo .Extracting)}{$case}m;
66     }
67
68 unless ($ch[0] =~ m/THIS IS A GENERATED FILE/) {
69     unshift @ch, join "\n" =>
70         "#!/bin/sh",
71         "#",
72         "# THIS IS A GENERATED FILE",
73         "# DO NOT HAND-EDIT",
74         "#",
75         "# See Porting/config_h.pl",
76         "",
77         "";
78     push @ch, ";;\nesac\n";
79     }
80
81 open  $ch, ">", $cSH or die "Cannot write $cSH: $!\n";
82 print $ch @ch;
83 close $ch;