summaryrefslogtreecommitdiff
path: root/windows-NT/mkconfig.pl
blob: 3a478e4fb72fbba365f9d755c3eb7807d776f8ff (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
308
309
310
311
312
313
314
#! perl -w

use strict;

# For the `mv' function which is smart enough to cross device boundries.
use File::Copy qw{mv};
# For the `basename' function.
use File::Basename;



###
### FUNCTIONS
###
sub save_edit
{
    my ($same, $file_name, $temp_name) = @_;

    if ($same)
    {
	unlink $temp_name
	    or warn "Failed to unlink ", $temp_name, ": $!";
	print "no change: ", $file_name, "\n";
    }
    else
    {
	mv $temp_name, $file_name
	    or die "Failed to rename ", $temp_name, " to ", $file_name, ": $!";

	print "save edit: ", $file_name, "\n";
    }
}

sub get_default
{
    my ($value, $default) = @_;

    if ($value eq "")
    {
	$value = $default;
    }

    return $value;
}



sub show_repeat
{
    my ($file, $new_no, $old_no, $line) = @_;

    print $file, " line ", $new_no, " duplicates line ", $old_no, ": ", $line;
}



sub show_orphan
{
    my ($case, $that, $this, $this_key, %this_macros) = @_;
    my $type = $this_macros{$this_key}[1];

    if ($case eq 0)
    {
	# roots file has extra macro statement
	# tell only of #undef
	return if $type eq "d";
    }
    elsif ($case eq 1)
    {
	# build file has extra macro statement
	# tell only of #define
	return if $type eq "u";
    }
    else
    {
	die "Internal script error";
    }

    if ($type eq "d")
    {
	    $type = "#define";
    }
    elsif ($type eq "u")
    {
	    $type = "#undef";
    }
    else
    {
	die "Internal script error";
    }

    print $this, " line ", $this_macros{$this_key}[0], " has ", $type, " ",
	  $this_key, " not found in ", $that, "\n";
}



sub make_config_h
{
    my $quiet;
    if ($_[0] eq "-q")
    {
	$quiet = 1;
        shift;
    }

    my ($ph_name, $out_name, $inp_name, $end_name) = @_;

    $ph_name = get_default $ph_name, "../config.h.in";
    $out_name = get_default $out_name, "config.h.in";
    $inp_name = get_default $inp_name, $out_name . ".in";
    $end_name = get_default $end_name, $out_name . ".footer";

    print STDERR "($inp_name + $ph_name) . $end_name --> $out_name\n"
	if !$quiet;

    #==========================================================================
    # scan build level configuration to collect define/undef values
    #==========================================================================

    open FINP, "< $inp_name"
	or die "error opening ", $inp_name, " for read: $!";
    my %build_macros;
    while (<FINP>)
    {
	if (/^#\s*define\s*(\w+)(\s+(.+))?$/)
	{
	    if (exists $build_macros{$1})
	    {
		show_repeat $inp_name, $., $build_macros{$1}[0], $_;
	    }
	    else
	    {
		$build_macros{$1} = [$., "d", $3];
	    }
	}
	elsif (/^\s*#\s*undef\s+(\w+)/)
	{
	    if (exists $build_macros{$1})
	    {
		show_repeat $inp_name, $., $build_macros{$1}[0], $_;
	    }
	    else
	    {
		$build_macros{$1} = [$., "u"];
	    }
	}
    }
    close FINP;
    #==========================================================================

    #==========================================================================
    # temporary output file
    #==========================================================================
    my $temp_name = basename($out_name) . ".tmp";

    open FOUT, "> $temp_name"
	or die "error opening ", $temp_name, " for write: $!";

    #==========================================================================
    # copy build level configuration append file to output file
    #==========================================================================
    my $base_out = basename $out_name;
    my $base_prog = basename $0;
    my $base_inp = basename $inp_name;
    my $base_ph = basename $ph_name;
    my $base_end = basename $end_name;

    print FOUT <<EOF;
/***
 *** $base_out, generated by $base_prog:
 ***
 ***   ($base_inp
 ***    + ../$base_ph)
 ***   . $base_end
 ***   --> $base_out
 ***
 *** ***** DO NOT ALTER THIS FILE!!! *****
 ***
 *** Changes to this file will be overwritten by automatic script runs.
 *** Changes should be made to the $base_inp & $base_end
 *** files instead.
 ***/

EOF

    #==========================================================================
    # copy root level configuration to output file
    # while keeping track of conditional compile nesting level
    #==========================================================================
    open FINP, "< $ph_name"
	or die "error opening ", $ph_name, " for read: $!";
    my %ph_macros;
    while (<FINP>)
    {

	my $out_line = $_;

	if (/^\s*#\s*undef\s+(\w+)/)
	{
	    if (exists $ph_macros{$1})
	    {
		    show_repeat $ph_name, $., $ph_macros{$1}[0], $_;
	    }
	    else
	    {
		    $ph_macros{$1} = [$., "u"];
	    }

	    if (exists $build_macros{$1}
	        and $build_macros{$1}[1] eq "d")
	    {
		$out_line = "#define $1";

		$out_line .= " " . $build_macros{$1}[2]
		    if defined $build_macros{$1}[2];

		$out_line .= "\n";
	    }
	}
	print FOUT $out_line;
    }
    close FINP;
    #==========================================================================

    #==========================================================================
    # copy build level configuration append file to output file
    #==========================================================================
    if (open FINP, "< $end_name")
    {
	while (<FINP>)
	{
		print FOUT $_;
	}
	close FINP;
    }
    #==========================================================================
    close FOUT;
    #==========================================================================

    #==========================================================================
    # determine whether output (if any) has changed from last run
    #==========================================================================
    my $same = 0;

    if (open FINP, "< $out_name")
    {
	open FOUT, "< $temp_name"
	    or die "error opening ", $temp_name, " for read: $!";

	$same = 1;
	while ($same)
	{
	    last if eof FINP and eof FOUT;
	    if (eof FINP or eof FOUT or <FINP> ne <FOUT>)
	    {
		$same = 0;
		last;
	    }
	}
	close FOUT;
	close FINP;
    }

    #==========================================================================
    # nag the guilty
    #==========================================================================
    my @keys_build = sort keys %build_macros;
    my @keys_roots = sort keys %ph_macros;
    my ($idx_build, $idx_roots) = (0, 0);
    while ($idx_build < @keys_build or $idx_roots < @keys_roots) {
	if ($idx_build >= @keys_build)
	{
	    show_orphan 0, $inp_name, $ph_name, $keys_roots[$idx_roots],
	                %ph_macros;
	    $idx_roots++;
	}
	elsif ($idx_roots >= @keys_roots)
	{
	    show_orphan 1, $ph_name, $inp_name, $keys_build[$idx_build],
	                   %build_macros;
	    $idx_build++;
	}
	elsif ($keys_build[$idx_build] gt $keys_roots[$idx_roots])
	{
	    show_orphan 0, $inp_name, $ph_name, $keys_roots[$idx_roots],
	                %ph_macros;
	    $idx_roots++;
	}
	elsif ($keys_roots[$idx_roots] gt $keys_build[$idx_build])
	{
	    show_orphan 1, $ph_name, $inp_name, $keys_build[$idx_build],
	                %build_macros;
	    $idx_build++;
	}
	else
	{
	    $idx_build++;
	    $idx_roots++;
	}
    }

    #==========================================================================
    # save output only if changed
    #==========================================================================
    save_edit $same, $out_name, $temp_name;
}



###
### MAIN
###
make_config_h @ARGV;