Imported Upstream version 1.2.0
[platform/upstream/libzip.git] / create-cmake-config.h.in.pl
1 #!/usr/bin/env perl
2 # Haiku OS: we don't care!
3
4 use strict;
5
6 my $in = 'cmake-config.h.in';
7 my $out = "$in.$$";
8
9 my ($fin, $fout);
10 open $fin, "< $in" or die "can't open $in: $!";
11 open $fout, "> $out" or die "can't create $out: $!";
12
13 my $zipconf_defines = read_zipconf_defines();
14
15 my $in_defines = 0;
16 while (my $line = <$fin>) {
17         if ($in_defines) {
18                 if ($line =~ m,/* END DEFINES,) {
19                         $in_defines = 0;
20                 }
21                 else {
22                         next;
23                 }
24         }
25         print $fout $line;
26         if ($line =~ m,/\* BEGIN DEFINES,) {
27                 $in_defines = 1;
28                 add_defines($fout, $zipconf_defines);
29         }
30 }
31
32 close $fin;
33 close $fout;
34
35 rename($out, $in);
36
37 sub add_defines {
38         my ($fout, $zipconf_defines) = @_;
39
40         my $fin;
41         open $fin, "< CMakeLists.txt" or die "can't open CMakeLists.txt: $!";
42
43         while (my $line = <$fin>) {
44                 my ($key, $value);
45
46                 if ($line =~ m/CHECK_TYPE_SIZE\(.* (\S*)\)/) {
47                         $key = $1;
48                         $value = "\${$1}";
49                 }
50                 elsif ($line =~ m/CHECK_\S*\(.* (\S*)\)/) {
51                         $key = $1;
52                 }
53
54                 if (defined($key) && !defined($zipconf_defines->{$key})) {
55                         print $fout "#cmakedefine $key" . ($value ? " $value" : "") . "\n";
56                 }
57         }
58
59         close $fin;
60 }
61
62 sub read_zipconf_defines {
63         my %zipconf_defines = ();
64
65         my $fin;
66         open $fin, "< cmake-zipconf.h.in" or die "can't open cmake-zipconf.h.in: $!";
67
68         while (my $line = <$fin>) {
69                 if ($line =~ m/#cmakedefine\s+(\S+)/) {
70                         $zipconf_defines{$1} = 1;
71                 }
72         }
73
74         close $fin;
75
76         return \%zipconf_defines;
77 }