Commit | Line | Data |
---|---|---|
253687a9 MB |
1 | #!/usr/bin/perl |
2 | ||
3 | # This script reorders config_h.SH after metaconfig | |
4 | # Changing metaconfig is too complicated | |
5 | # | |
5b46e172 MB |
6 | # This script is run just after metaconfig, and it |
7 | # is run ONLY ONCE. Not to be used afterwards | |
8 | # | |
0c7e85c3 | 9 | # Copyright (C) 2005-2020 by H.Merijn Brand (m)'20 [23-08-2020] |
253687a9 MB |
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"); | |
0c7e85c3 | 18 | open $ch, "<", $cSH or die "Cannot open $cSH: $!\n"; |
253687a9 MB |
19 | { local $/ = "\n\n"; |
20 | @ch = <$ch>; | |
21 | close $ch; | |
22 | } | |
23 | ||
0c7e85c3 | 24 | sub ch_index { |
253687a9 MB |
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 ... | |
9b70c55f | 35 | BYTEORDER => [ qw( UVSIZE ) ], |
253687a9 MB |
36 | LONGSIZE => [ qw( BYTEORDER ) ], |
37 | MULTIARCH => [ qw( BYTEORDER MEM_ALIGNBYTES ) ], | |
38 | HAS_QUAD => [ qw( I64TYPE ) ], | |
9b70c55f MB |
39 | HAS_GETGROUPS => [ qw( Groups_t ) ], |
40 | HAS_SETGROUPS => [ qw( Groups_t ) ], | |
253687a9 MB |
41 | ); |
42 | ||
43 | my $changed; | |
44 | do { | |
45 | $changed = 0; | |
46 | foreach my $sym (keys %dep) { | |
0c7e85c3 | 47 | ch_index (); |
253687a9 MB |
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++; | |
0c7e85c3 | 54 | ch_index (); |
253687a9 MB |
55 | } |
56 | } | |
57 | } while ($changed); | |
58 | ||
43dddb59 MB |
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 | } | |
43dddb59 | 67 | |
0c7e85c3 MB |
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 | } | |
43dddb59 | 80 | |
0c7e85c3 | 81 | open $ch, ">", $cSH or die "Cannot write $cSH: $!\n"; |
253687a9 MB |
82 | print $ch @ch; |
83 | close $ch; |