Fix a couple of loops.
[fedora-mingw.git] / crossreport / update-crossreport-db.pl
1 #!/usr/bin/perl -w
2 #
3 # Update CrossReport database.
4 # Copyright (C) 2009 Red Hat Inc.
5 # Written by Richard W.M. Jones <rjones@redhat.com>,
6 # http://fedoraproject.org/wiki/MinGW
7 #
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or (at
11 # your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 # General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
22 use strict;
23
24 use GDBM_File;
25
26 # Map of symbol name -> RPM owner.
27 my %symbols;
28 my $symdbm =
29     tie %symbols, "GDBM_File", "crossreport.db", &GDBM_NEWDB, 0666;
30 main ();
31 $symbols{__crossreport_time} = time ();
32 $symdbm->sync ();
33
34 sub add_symbol {
35     my $symbol = shift;
36     my $rpm_name = shift;
37
38     if (exists $symbols{$symbol} &&
39         $symbols{$symbol} ne $rpm_name) {
40         # Suppress this warning - it is quite common and probably
41         # doesn't matter.
42         #warn "duplicate symbol: $symbol: $rpm_name and $symbols{$symbol}\n"
43     }
44
45     $symbols{$symbol} = $rpm_name;
46 }
47
48 sub main {
49     print <<EOT;
50 Just a note: You should have ALL mingw32-* libraries installed
51 when you run this, otherwise you will get an incomplete database.
52 I do not have a way to test this, so I print this note.
53
54 EOT
55
56     my @implibs = </usr/i686-pc-mingw32/sys-root/mingw/lib/*.dll.a>;
57
58     print "Analyzing ", 0+@implibs, " libraries ...\n";
59
60     foreach my $implib (@implibs) {
61         # What MinGW library provides this file?
62         my $cmd = "rpm -qf $implib";
63         open CMD, "$cmd |" or die "$cmd: $!";
64         my $r = <CMD>;
65         close CMD;
66         my $rpm_name;
67         if ($r =~ /^(mingw32-[-+\w]+)-\d/) {
68             $rpm_name = $1;
69         } else {
70             die "$implib: Cannot find RPM owning this file.\n"
71         }
72
73         $cmd = "i686-pc-mingw32-nm $implib | grep ' [A-HJ-TV-Z] ' | i686-pc-mingw32-c++filt -_";
74         open CMD, "$cmd |" or die "$cmd: $!";
75         foreach (<CMD>) {
76             chomp;
77             if (m/^[[:xdigit:]]+ T _(\w+)(@\d+)?$/) {
78                 add_symbol ($1, $rpm_name);
79             } elsif (m/^[[:xdigit:]]+ T (.*)(@\d+)?$/) {
80                 add_symbol ($1, $rpm_name);
81             } else {
82                 die "$_: ?\n";
83             }
84         }
85     }
86
87     print "Found ", 0+(keys %symbols), " symbols.\n";
88 }