Recorder.pm 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. # $OpenBSD: Recorder.pm,v 1.6 2014/03/24 15:18:17 afresh1 Exp $
  2. # Copyright (c) 2004-2010 Marc Espie <espie@openbsd.org>
  3. #
  4. # Permission to use, copy, modify, and distribute this software for any
  5. # purpose with or without fee is hereby granted, provided that the above
  6. # copyright notice and this permission notice appear in all copies.
  7. #
  8. # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. use strict;
  16. use warnings;
  17. # part of check-lib-depends
  18. # Recorder: how we keep track of which binary uses which library
  19. package OpenBSD::Recorder;
  20. sub new
  21. {
  22. my $class = shift;
  23. return bless {}, $class;
  24. }
  25. sub reduce_libname
  26. {
  27. my ($self, $lib) = @_;
  28. $lib =~ s/^(.*\/)?lib(.*)\.so\.(\d+)\.\d+$/$2.$3/;
  29. return $lib;
  30. }
  31. sub libs
  32. {
  33. my $self = shift;
  34. return keys %$self;
  35. }
  36. sub record_rpath
  37. {
  38. }
  39. # SimpleRecorder: remember one single binary for each library
  40. package OpenBSD::SimpleRecorder;
  41. our @ISA = qw(OpenBSD::Recorder);
  42. sub record
  43. {
  44. my ($self, $lib, $filename) = @_;
  45. $self->{$self->reduce_libname($lib)} = $filename;
  46. }
  47. sub binary
  48. {
  49. my ($self, $lib) = @_;
  50. return $self->{$lib};
  51. }
  52. # AllRecorder: remember all binaries for each library
  53. package OpenBSD::AllRecorder;
  54. our @ISA = qw(OpenBSD::Recorder);
  55. sub record
  56. {
  57. my ($self, $lib, $filename) = @_;
  58. push(@{$self->{$self->reduce_libname($lib)}}, $filename);
  59. }
  60. sub binaries
  61. {
  62. my ($self, $lib) = @_;
  63. return @{$self->{$lib}};
  64. }
  65. sub binary
  66. {
  67. my ($self, $lib) = @_;
  68. return $self->{$lib}->[0];
  69. }
  70. sub dump
  71. {
  72. my ($self, $fh) = @_;
  73. for my $lib (sort $self->libs) {
  74. print $fh "$lib:\t\n";
  75. for my $binary (sort $self->binaries($lib)) {
  76. print $fh "\t$binary\n";
  77. }
  78. }
  79. }
  80. package OpenBSD::DumpRecorder;
  81. sub new
  82. {
  83. my $class = shift;
  84. return bless {}, $class;
  85. }
  86. sub record
  87. {
  88. my ($self, $lib, $filename) = @_;
  89. push(@{$self->{$filename}->{libs}}, $lib);
  90. }
  91. sub record_rpath
  92. {
  93. my ($self, $path, $filename) = @_;
  94. push(@{$self->{$filename}->{rpath}}, $path);
  95. }
  96. sub dump
  97. {
  98. my ($self, $fh) = @_;
  99. while (my ($binary, $v) = each %$self) {
  100. print $fh $binary;
  101. if (defined $v->{rpath}) {
  102. print $fh "(", join(':', @{$v->{rpath}}), ")";
  103. }
  104. $v->{libs} //= [];
  105. print $fh ": ", join(',', @{$v->{libs}}), "\n";
  106. }
  107. }
  108. sub libraries
  109. {
  110. my ($self, $fullname) = @_;
  111. if (defined $self->{$fullname} && defined $self->{$fullname}{libs}) {
  112. return @{$self->{$fullname}{libs}};
  113. } else {
  114. return ();
  115. }
  116. }
  117. sub rpath
  118. {
  119. my ($self, $fullname) = @_;
  120. if (defined $self->{$fullname} && defined $self->{$fullname}{rpath}) {
  121. return @{$self->{$fullname}{rpath}};
  122. } else {
  123. return ();
  124. }
  125. }
  126. sub retrieve
  127. {
  128. my ($self, $state, $filename) = @_;
  129. open(my $fh, '<', $filename) or
  130. $state->fatal("Can't read #1: #2", $filename, $!);
  131. while (my $line = <$fh>) {
  132. chomp $line;
  133. if ($line =~ m/^(.*?)\:\s(.*)$/) {
  134. my ($binary, $libs) = ($1, $2);
  135. if ($binary =~ m/^(.*)\((.*)\)$/) {
  136. $binary = $1;
  137. my @path = split(':', $2);
  138. $self->{$binary}{rpath} = \@path;
  139. }
  140. my @libs = split(/,/, $libs);
  141. $self->{$binary}{libs}= \@libs;
  142. } else {
  143. $state->errsay("Can't parse #1", $line);
  144. }
  145. }
  146. close $fh;
  147. }
  148. 1;