]> mj.ucw.cz Git - libucw.git/blob - build/genconf
EXTRA_RUNDIRS needn't form a strict hierarchy, so add a -p.
[libucw.git] / build / genconf
1 #!/usr/bin/perl
2 # Configuration file preprocessor
3 # (c) 2003 Martin Mares <mj@ucw.cz>
4
5 use strict;
6 use warnings;
7
8 @ARGV == 3 or die "Usage: genconf <src> <dest> <config.mk>";
9
10 open CF, $ARGV[2] or die "Unable to open $ARGV[2]";
11 my %options = ();
12 while (<CF>) {
13         /^(CONFIG_\w+)=1/ || next;
14         $options{$1} = 1;
15 }
16 close CF;
17
18 open IN, $ARGV[0] or die "Unable to open $ARGV[0]";
19 open OUT, ">$ARGV[1]" or die "Unable to create $ARGV[1]";
20 my @ifs = ();   # stack of conditions, 1=satisfied
21 my $empty = 0;  # last line was empty
22 while (<IN>) {
23         if (/^#?ifdef\s+(\w+)/) {
24                 push @ifs, !defined $options{$1};
25         } elsif (/^#ifndef\s+(\w+)/) {
26                 push @ifs, defined $options{$1};
27         } elsif (/^#?endif/) {
28                 defined pop @ifs || die "Improper nesting of conditionals";
29         } elsif (/^#?else/) {
30                 my $x = pop @ifs;
31                 defined $x || die "Improper nesting of conditionals";
32                 push @ifs, !$x;
33         } else {
34                 @ifs && $ifs[$#ifs] && next;
35                 if (/^$/) {
36                         $empty && next;
37                         $empty = 1;
38                 } else { $empty = 0; }
39                 print OUT;
40         }
41 }
42 @ifs && die "Unterminated #ifdef";
43 close IN;
44 close OUT;