xref: /openbsd/gnu/usr.bin/perl/Porting/config_h.pl (revision f2a19305)
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
14use strict;
15use warnings;
16
17my ($cSH, $ch, @ch, %ch) = ("config_h.SH");
18open $ch, "<", $cSH or die "Cannot open $cSH: $!\n";
19{   local $/ = "\n\n";
20    @ch = <$ch>;
21    close  $ch;
22    }
23
24sub 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
33my %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
43my $changed;
44do {
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
60for (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
68unless ($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
81s/^(\s*)#(\s*)define\t\s*/${1}#${2}define /gm for @ch;
82
83open  $ch, ">", $cSH or die "Cannot write $cSH: $!\n";
84print $ch @ch;
85close $ch;
86