Advertisement
overloop

dependencies.pl

Jun 26th, 2015
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 5.87 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use File::Basename;
  5. use File::Path qw/make_path/;
  6. use File::Copy;
  7.  
  8. my $qt_plugins_root = "/c/Qt/4.7.4/plugins";
  9.  
  10. # @todo opendir; readdir; closedir
  11. my %qt_plugins = (
  12.   '--qsqlite' => $qt_plugins_root . '/sqldrivers/qsqlite4.dll',
  13.   '--qsqlmysql' => $qt_plugins_root . '/sqldrivers/qsqlmysql4.dll',
  14.   '--qsqlodbc' => $qt_plugins_root . '/sqldrivers/qsqlodbc4.dll',
  15.   '--qsqlpsql' => $qt_plugins_root . '/sqldrivers/qsqlpsql4.dll',
  16.  
  17.   '--qmysql' => $qt_plugins_root . '/sqldrivers/qsqlmysql4.dll',
  18.   '--qodbc' => $qt_plugins_root . '/sqldrivers/qsqlodbc4.dll',
  19.   '--qpsql' => $qt_plugins_root . '/sqldrivers/qsqlpsql4.dll',
  20.  
  21.   '--qgif' => $qt_plugins_root . '/imageformats/qgif4.dll',
  22.   '--qico' => $qt_plugins_root . '/imageformats/qico4.dll',
  23.   '--qjpeg' => $qt_plugins_root . '/imageformats/qjpeg4.dll',
  24.   '--qmng' => $qt_plugins_root . '/imageformats/qmng4.dll',
  25.   '--qsvg' => $qt_plugins_root . '/imageformats/qsvg4.dll',
  26.   '--qtiff' => $qt_plugins_root . '/imageformats/qtiff4.dll'
  27. );
  28.  
  29. sub usage {
  30.     print STDERR "Usage: " . $0 . " [-f] [-n] [-s] [-c] app.exe [foo.dll] [bar.dll] \n";
  31.     print STDERR "  -f print found dlls\n";
  32.     print STDERR "  -n print not found dlls\n";
  33.     print STDERR "  -s print system dlls\n";
  34.     print STDERR "  -c copy dependecies to 'distribution' directory\n";
  35.    
  36.     print STDERR "Include Qt plugins:  \n";
  37.    
  38.     my @plugins = keys %qt_plugins;
  39.     my @plugins1 = @plugins[0..8];
  40.     my @plugins2 = @plugins[9..scalar(@plugins)-1];
  41.     my @plugins3 = ('--imageformats','--qsql');
  42.    
  43.     print STDERR "  " . join(',',@plugins1) . ",\n";
  44.     print STDERR "  " . join(',',@plugins2) . ",\n";
  45.     print STDERR "  " . join(',',@plugins3) . "\n";
  46.    
  47.     print STDERR "Example: " . $0 . " -c release/myapp.exe --qmysql debug/options.ini\n";
  48. }
  49.  
  50. sub dlls {
  51.   my ($path) = @_;
  52.   #print STDERR $path . "\n";
  53.   my %result = ();
  54.   my @deps = split("\n",`objdump -p "$path" | grep "DLL Name:"`);
  55.   for my $dep (@deps) {
  56.     if ($dep =~ /DLL Name: (.*)/) {
  57.       my $n = $1;
  58.       my $p = `which "$n"`;
  59.       $p =~ s/\n//;
  60.       $result{$n} = $p;
  61.     }
  62.   }
  63.   return %result;
  64. }
  65.  
  66. # -f found -n not found -s system
  67.  
  68.  
  69.  
  70. #ls /c/Qt/4.7.4/plugins/sqldrivers/ | grep '.*[^d]\.dll' | sed 's,4.dll,,'
  71.  
  72.  
  73. my $print_found = 0;
  74. my $print_notfound = 0;
  75. my $print_system = 0;
  76. my $copy_dependencies = 0;
  77.  
  78. my @bins = ();
  79. my %deps = ();
  80.  
  81. if (scalar(@ARGV)<1) {
  82.   usage();
  83.   exit(0);
  84. }
  85.  
  86. sub plugin_bin {
  87.   my ($bins,$arg) = @_;
  88.   push(@$bins,$qt_plugins{$arg});
  89. }
  90.  
  91. #my $include_dll = 0;
  92. #my $include_exe = 0;
  93.  
  94.  
  95.  
  96. for my $arg (@ARGV) {
  97.   if ($arg eq '-f') {
  98.     $print_found = 1;
  99.   } elsif ($arg eq '-n') {
  100.     $print_notfound = 1;
  101.   } elsif ($arg eq '-s') {
  102.     $print_system = 1;
  103.   } elsif ($arg eq '-c') {
  104.     $copy_dependencies = 1;
  105.   } elsif ($arg eq "-h" || $arg eq "--help") {
  106.     usage();
  107.     exit(0);
  108.   } elsif ( exists $qt_plugins{$arg} ) {
  109.     plugin_bin(\@bins,$arg);
  110.   } elsif ($arg eq '--imageformats') {
  111.     plugin_bin(\@bins,$_) foreach ('--qgif','--qico','--qjpeg','--qmng','--qsvg','--qtiff');
  112.   } elsif ($arg eq '--qsql') {
  113.     plugin_bin(\@bins,$_) foreach ('--qsqlite','--qsqlmysql','--qsqlodbc','--qsqlpsql');
  114.   } else {
  115.     push(@bins,$arg);
  116.   }
  117. }
  118.  
  119. #print join("\n",@bins) . "\n"; exit(0); #debug
  120.  
  121. if ($print_found+$print_notfound+$print_system == 0) {
  122.   $print_found = 1;
  123.   $print_notfound = 1;
  124.   $print_system = 1;
  125. }
  126.  
  127. if (scalar(@bins)==0) {
  128.   print STDERR "Error: pass executable[s] path[es].\n";
  129.   usage();
  130.   exit(-1);
  131. }
  132.  
  133. for my $bin (@bins) {
  134.   %deps = (%deps, dlls($bin));
  135. }
  136.  
  137. #my @deps_ = keys(%deps);
  138. #print join("\n",values(%deps)) . "\n";
  139.  
  140. my %complete = ();
  141. my $iscomplete = 0;
  142. while (!$iscomplete) {
  143.   $iscomplete = 1;
  144.   for my $dep (keys %deps) {
  145.     #print STDERR "dep $dep\n";
  146.     #print STDERR "exists\n" if exists $complete{$dep};
  147.     #print STDERR "matches\n" if $deps{$dep} =~ /Windows.system32/;
  148.     if (!exists $complete{$dep} && !($deps{$dep} =~ /Windows.system32/)) {
  149.       $iscomplete = 0;
  150.       my %deps_ = dlls($deps{$dep});
  151.       #print STDERR "deps for " . $deps{$dep} . ":\n" . join("\n",values(%deps_)) . "\n";
  152.       %deps = (%deps, %deps_);
  153.       $complete{$dep} = 1;
  154.     }
  155.   }
  156. }
  157.  
  158. my @found = sort map { $deps{$_} } grep { length($deps{$_}) > 0 && !($deps{$_} =~ /Windows.system32/) } keys %deps;
  159. my @notfound = sort grep { length($deps{$_}) == 0 } keys %deps;
  160. my @system = sort map { $deps{$_} } grep { $deps{$_} =~ /Windows.system32/ } keys %deps;
  161.  
  162. my $print_header = 0;
  163. if ($print_found+$print_notfound+$print_system > 1) {
  164.   $print_header = 1;
  165. }
  166. if ($print_found) {
  167.   print "FOUND DLLS:\n" if $print_header;
  168.   print join("\n",@found) . "\n" if scalar(@found)>0;
  169. }
  170. if ($print_notfound) {
  171.   print "NOT FOUND DLLS:\n" if $print_header;
  172.   print join("\n",@notfound) . "\n" if scalar(@notfound)>0;
  173. }
  174. if ($print_system) {
  175.   print "SYSTEM DLLS:\n" if $print_header;
  176.   print join("\n",@system) . "\n" if scalar(@system)>0;
  177. }
  178.  
  179. my $qt_conf = 0;
  180.  
  181. my $dest = './distribution';
  182.  
  183. if ($copy_dependencies) {
  184.   if ( ! -d $dest ) {
  185.     mkdir($dest);
  186.   }
  187.   for my $dep (@found) {
  188.     #print "cp $dep $dest\n";
  189.     copy($dep,$dest);
  190.   }
  191.   for my $bin (@bins) {
  192.     if (index($bin,$qt_plugins_root)>-1) {
  193.       if ($bin =~ m,.*/(plugins/.*),) {
  194.         my $dest_ = dirname($dest . '/' . $1);
  195.         #print $bin . ' dest: ' . $dest_ . "\n"; #debug
  196.         if (! -d $dest_) {
  197.           make_path($dest_);
  198.         }
  199.         copy($bin,$dest_); # @todo overwrite only if newer
  200.         $qt_conf = 1;
  201.       }
  202.     } else {
  203.       #print 'cp ' . $bin . ' ' . $dest . "\n"; #debug
  204.       copy($bin,$dest); # @todo overwrite only if newer
  205.     }
  206.   }
  207. }
  208.  
  209. if ($qt_conf) {
  210.   my $dest_ = $dest . '/qt.conf';
  211.   if ( ! -f $dest_) {
  212.     open OUTPUT,'>', $dest_;
  213.     print OUTPUT "[Paths]\nPlugins = ./plugins\n";
  214.     close OUTPUT;
  215.   }
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement