Debian Codegraph @

source/util/tag-major-upgrade

raw
1 #!/usr/bin/env perl
2
3 use warnings;
4 use Getopt::Std;
5 use strict;
6
7 our ($opt_n, $opt_t);
8 getopts('nt:');
9 my $dry_run = $opt_n;
10 my $tag = $opt_t;
11 my $any_changed = 0;
12
13 sub fix_tag($$$$$)
14 {
15 my ($op, $ver, $body, $else, $full) = @_;
16 my $matches;
17
18 # Only obsolete tags
19 return $full unless $ver < $tag;
20
21 if ($op eq '==')
22 {
23 $matches = $tag == $ver;
24 }
25 elsif ($op eq '!=')
26 {
27 $matches = $tag != $ver;
28 }
29 elsif ($op eq '<=')
30 {
31 $matches = $tag <= $ver;
32 }
33 elsif ($op eq '<')
34 {
35 $matches = $tag < $ver;
36 }
37 elsif ($op eq '>=')
38 {
39 $matches = $tag >= $ver;
40 }
41 elsif ($op eq '>')
42 {
43 $matches = $tag > $ver;
44 }
45 die "Unknown operator '$op'" unless defined $matches;
46
47 return $matches ? $body
48 : defined $else ? $else
49 : "";
50 }
51
52 if (!defined $tag)
53 {
54 my $tagf = `git ls-files '*tag-version.h'`;
55 chomp $tagf;
56 die "Too many matches for tag-version.h\n" if $tagf =~ /\n/;
57
58 open T, "<", $tagf or die "Could not open $tagf: $!\n";
59 while (<T>)
60 {
61 /^#define\s+TAG_MAJOR_VERSION\s+(\d+)/ and $tag = $1;
62 }
63 }
64
65 die "Could not determine version tag\n" unless defined $tag;
66
67 my @files = @ARGV;
68 undef $/;
69 unless (@files)
70 {
71 @files = (grep /\.(cc|h)$/ && !/(^|\/)prebuilt\//, split /\n/, `git ls-files`);
72 }
73
74 for my $f (@files)
75 {
76 open F, "<", $f or die "Can't read $f\n";
77 my $old = $_ = <F>;
78 close F;
79
80 s/# Full match in $1
81 (
82 # #if line. Comparison operator in $2, tag in $3
83 ^\s* \# \s* if \s+ TAG_MAJOR_VERSION \s* (!=|==|>=|>|<=|<) \s* (\d+) \n
84
85 # body of #if in $4. We don't handle #if TAG_MAJOR_VERSION blocks
86 # that contain nested #ifs.
87 ((?: (?! \s* \# \s* (?:if|elif|else|endif)) .* \n)+)
88
89 # optional #else, body in $5
90 (?:
91 \s* \# \s* else \n
92 ((?: (?! \s* \# \s* (?:if|elif|else|endif)) .* \n)+)
93 )?
94
95 \s* \# \s* endif \n
96 )
97 /fix_tag($2, $3, $4, $5, $1)/gexm;
98
99 if ($old ne $_)
100 {
101 print "$f\n";
102 $any_changed = 1;
103 if (!$dry_run)
104 {
105 open F, ">", $f or die "Can't write $f\n";
106 print F;
107 close F;
108 }
109 }
110 }
111
112 exit 1 if ($dry_run and $any_changed);