source: branches/stable/SOURCE_TOOLS/provide_libs.pl

Last change on this file was 7739, checked in by westram, 13 years ago
  • fix: print exitcode of failed system calls
File size: 4.8 KB
Line 
1# ============================================================ #
2#                                                              #
3#   File      : provide_libs.pl                                #
4#   Purpose   :                                                #
5#                                                              #
6#   Coded by Ralf Westram (coder@reallysoft.de) in June 2009   #
7#   Institute of Microbiology (Technical University Munich)    #
8#   www.arb-home.de                                            #
9#                                                              #
10# ============================================================ #
11
12use strict;
13use warnings;
14
15my %needed_by = (
16                 # 'libXm.so' => 'arb_ntree', # this would also link the motif lib (not done atm)
17                );
18
19my %needed_by_opengl = (
20                        'libGLEW.so' => 'arb_edit4',
21                        'libGLw.so' => 'arb_edit4',
22                       );
23
24my $verbose = 0; # for debugging this script
25
26#  libs get copied to ../lib/addlibs
27
28sub getModtime($) {
29  my ($fileOrDir) = @_;
30  my $modtime = (stat($fileOrDir))[9];
31  return $modtime;
32}
33
34sub update_lib($$$) {
35  my ($libname, $binary, $addlibsdir) = @_;
36
37  if (not -f $binary) {
38    print "Can't use '$binary' to detect used '$libname' (no such file)\n";
39  }
40  else {
41    my ($linked_symbolic,$linked_real) = (undef,undef);
42    my $command = 'ldd '.$binary;
43
44    $verbose==0 || print "$command\n";
45    open(LDD,$command.'|') || die "can't execute '$command' (Reason: $!)";
46    foreach (<LDD>) {
47      chomp;
48      if (/^\s+([^\s]+)\s+=>\s+([^\s]*)\s+\(.*\)$/) {
49        my ($symbolic,$real) = ($1,$2);
50        $verbose==0 || print "ldd-out: symbolic='$symbolic' real='$real'\n";
51        if ($symbolic =~ /^$libname\./) {
52          ($linked_symbolic,$linked_real) = ($symbolic,$real);
53        }
54      }
55      else {
56        $verbose==0 || print "cant parse ldd-out: '$_'\n";
57      }
58    }
59    close(LDD);
60
61    my $keep = undef;
62    if (not defined $linked_symbolic) {
63      print "Warning: $binary is NOT linked against $libname ($libname will not be provided)\n";
64    }
65    else {
66      print "$binary links against '$linked_symbolic' ($linked_real)\n";
67
68      my $source = $linked_real;
69      my $dest   = $addlibsdir.'/'.$linked_symbolic;
70      my $doCopy = 0;
71
72      if (-f $dest) {
73        my $mod_source = getModtime($source);
74        my $mod_dest   = getModtime($dest);
75
76        if ($mod_dest != $mod_source) {
77          print "Modification time of $source differs -> force update\n";
78          $doCopy = 1;
79        }
80        else {
81          print "$dest is up-to-date\n";
82        }
83      }
84      else { $doCopy = 1; }
85
86      if ($doCopy==1) {
87        $command = "cp -p '$source' '$dest'";
88        print "Updating $dest\n";
89        system($command)==0 || die "Failed to execute '$command' (Reason: $?)";
90      }
91
92      if (not -f $dest) { die "oops .. where is $dest?"; }
93      $keep = $linked_symbolic;
94    }
95
96    # delete older copies
97    opendir(DIR,$addlibsdir) || die "can't read directory '$addlibsdir' (Reason: $!)";
98    foreach (readdir(DIR)) {
99      if (/^$libname/) {
100        # print "Matching file: '$_'\n";
101        if ((not defined $keep) or ($_ ne $keep)) {
102          my $toRemove = $addlibsdir.'/'.$_;
103          print "removing obsolete file '$toRemove'\n";
104          unlink($toRemove) || die "Failed to unlink '$toRemove' (Reason: $!)";
105        }
106      }
107    }
108    closedir(DIR);
109  }
110}
111sub provide_libs($$$) {
112  my ($arbhome,$addlibsdir,$opengl) = @_;
113  my $bindir = $arbhome.'/bin';
114
115  chdir($arbhome) || die "can't cd to '$arbhome' (Reason: $!)";
116  print "Updating additional libs\n";
117
118  foreach my $lib (keys %needed_by) {
119    update_lib($lib, $bindir.'/'.$needed_by{$lib}, $addlibsdir);
120  }
121  if ($opengl==1) {
122    foreach my $lib (keys %needed_by_opengl) {
123      update_lib($lib, $bindir.'/'.$needed_by_opengl{$lib}, $addlibsdir);
124    }
125  }
126}
127
128sub main() {
129  my $args = scalar(@ARGV);
130  if ($args==0) {
131    die 'Usage: provide_libs.pl "opengl=$(OPENGL)" "link_static=$(LINK_STATIC)" "arbhome=$(ARBHOME)"';
132  }
133
134  my %arg = ();
135  foreach (@ARGV) {
136    if (/^([^=]+)=(.*)$/) { $arg{$1} = $2; }
137    else { die "can't handle argument '$_'"; }
138  }
139
140  if ($verbose>0) { foreach (sort keys %arg) { print "$_='".$arg{$_}."'\n"; } }
141
142  my $opengl = $arg{opengl};
143  my $arbhome = $arg{arbhome};
144  my $link_static = $arg{link_static};
145
146  if (defined $opengl and defined $arbhome and defined $link_static) {
147    if (not -d $arbhome) { die "Expected directory '$arbhome'"; }
148
149    my $addlibsdir = $arbhome.'/lib/addlibs';
150    if (not -d $addlibsdir) { die "Expected directory '$addlibsdir'"; }
151
152    if ($link_static == 1) {
153      print "No libs provided to $addlibsdir (we are linking static)\n";
154    }
155    else {
156      provide_libs($arbhome,$addlibsdir,$opengl);
157    }
158  }
159  else {
160    die "Expected arguments are 'devel=', 'arbhome=' and 'link_static='";
161  }
162}
163main();
Note: See TracBrowser for help on using the repository browser.