]> mj.ucw.cz Git - libucw.git/blob - tools/rename-symbols
72c3528e97bc3f1fc3ed9579a8e994f61d0e654c
[libucw.git] / tools / rename-symbols
1 #!/usr/bin/perl
2 # Re-generate symbol renaming defines
3 # (c) 2013 Martin Mares <mj@ucw.cz>
4
5 use common::sense;
6
7 my %renames = ();
8 open my $f, '<', 'tools/libucw.api' or die;
9 my $current;
10 while (<$f>) {
11         chomp;
12         if (/^#\s*(.*)/) {
13                 $current = $1;
14         } else {
15                 push @{$renames{$current}}, $_;
16         }
17 }
18 close $f;
19
20 for my $g (sort keys %renames) {
21         my @symbols = sort @{$renames{$g}};
22         @symbols or next;
23         print "### $g\n";
24
25         open my $in, '<', $g or die;
26         open my $out, '>', "$g.new" or die;
27         my $mode = 0;
28         my $cmt = 0;
29         while (<$in>) {
30                 if (!$mode) {
31                         if (m{^/\*$} && !$cmt) {
32                                 $cmt = 1;
33                         } elsif (m{^\s} ||
34                             m{^#include\s} ||
35                             m{^#define\s+_} ||
36                             m{^#ifndef\s+_} ||
37                             m{^\s*$}
38                            ) {
39                                 # Waiting for the right spot
40                         } elsif (m{^#ifdef CONFIG_UCW_CLEAN_ABI$}) {
41                                 $mode = 2;
42                                 next;
43                         } else {
44                                 $mode = 1;
45                                 print $out "#ifdef CONFIG_UCW_CLEAN_ABI\n";
46                                 for my $sym (@symbols) {
47                                         print $out "#define $sym ucw_$sym\n";
48                                 }
49                                 print $out "#endif\n\n";
50                         }
51                 } elsif ($mode == 2) {
52                         if (m{^$}) {
53                                 $mode = 0;
54                         }
55                         next;
56                 }
57                 print $out "$_";
58         }
59         $mode or die;
60         close $out;
61         close $in;
62         rename "$g.new", $g or die;
63 }