source: trunk/SOURCE_TOOLS/filter_defined_symbols.pl

Last change on this file was 19073, checked in by westram, 2 years ago
  • fix spelling of nm CLI flag (introduced with r18905)
    • failed under OSX
    • apparently auto-completed under Linux
  • Property svn:executable set to *
File size: 2.5 KB
Line 
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6sub cmd2arr($\@) {
7  my ($cmd, $arr_r) = @_;
8  print STDERR "[Action: '$cmd']\n";
9  open(CMD, $cmd.'|') || die "failed to fork '$cmd' (Reason: $!)";
10  my $line;
11  while (defined($line=<CMD>)) {
12    chomp($line);
13    push @$arr_r, $line;
14  }
15  close(CMD) || die "failed to execute '$cmd' (Reason: $! exitcode=$?)";
16}
17
18my $COMMON_NM_OPTIONS = "--demangle --format=posix";
19
20sub list_undefined($\@) {
21  my ($source, $arr_r) = @_;
22  my $cmd = "nm $COMMON_NM_OPTIONS --undefined-only $source";
23  cmd2arr($cmd,@$arr_r);
24}
25
26sub list_defined($\@) {
27  my ($source, $arr_r) = @_;
28  my $cmd = "nm $COMMON_NM_OPTIONS --defined-only --extern-only $source";
29  cmd2arr($cmd,@$arr_r);
30}
31
32sub die_usage($) {
33  my ($msg) = @_;
34
35  print STDERR "Usage: filter_defined_symbols.pl OBJECT1 OBJECT2 [ ... OBJECTN ]\n";
36  print STDERR "Test whether OBJECT2 ... OBJECTN define all symbols which are required by (=undefined in) OBJECT1\n";
37  print STDERR "List all symbols which are required by (=undefined in) OBJECT1 and\n";
38  print STDERR "are not defined in OBJECT2 ... OBJECTN\n";
39
40  die $msg."\n";
41}
42
43sub eliminate_defined(\@\@\@) {
44  my ($undefined_r, $defined_r, $notresolved_r) = @_;
45
46  my %undefined = map { if (/ U /o) { $` => $_; } else { ; } } @$undefined_r;
47  die "nothing undefined (probably broken)" if not scalar(%undefined);
48
49  my %defined = ();
50  foreach (@$defined_r) {
51    if (/ [TB] /o) {
52      my $sym = $`;
53      if (exists $undefined{$sym}) {
54        $defined{$sym} = 1;
55      }
56    }
57  }
58
59  foreach (keys %undefined) {
60    if (not exists $defined{$_}) {
61      push @$notresolved_r, $undefined{$_};
62    }
63  }
64
65}
66
67sub filter_defined_symbols() {
68  my $args = scalar(@ARGV);
69  if ($args < 2) {
70    die_usage("Missing arguments");
71  }
72  else {
73    my $OBJECT1 = shift @ARGV;
74    my $OBJECT2 = shift @ARGV;
75
76    my @undefined = (); # undefined symbols from OBJECT1
77    my @defined = ();   # defined symbols from OBJECT2
78
79    list_undefined($OBJECT1, @undefined);
80    list_defined($OBJECT2, @defined);
81
82    my @notresolved = ();
83
84    eliminate_defined(@undefined, @defined, @notresolved);
85
86    while (scalar(@ARGV)) {
87      my $OBJECTN = shift @ARGV;
88
89      if (not scalar(@notresolved)) {
90        die "no notresolved symbols left (before considering '$OBJECTN')";
91      }
92
93      @undefined = @notresolved;
94      @defined = ();
95
96      list_defined($OBJECTN, @defined);
97
98      @notresolved = ();
99      eliminate_defined(@undefined, @defined, @notresolved);
100    }
101
102    foreach (sort @notresolved) {
103      print $_."\n";
104    }
105  }
106}
107
108filter_defined_symbols();
Note: See TracBrowser for help on using the repository browser.