Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use strict;
- use File::Basename;
- use File::Path qw/make_path/;
- use File::Copy;
- use Data::Dumper;
- #my $qt_plugins_root = '/c/Qt/4.7.4/plugins';
- my %qt_plugins = (
- 'home' => '/c/Qt/4.7.4/plugins',
- 'dirs' => {
- '--imageformats' => 'imageformats',
- '--sqldrivers' => 'sqldrivers'
- },
- 'files' => {
- },
- 'assoc' => {
- }
- );
- for my $arg (keys %{$qt_plugins{'dirs'}}) {
- my $dir = $qt_plugins{'dirs'}{$arg};
- my $dir_ = $qt_plugins{'home'} . '/' . $dir;
- if ( -d $dir_) {
- opendir(my $dh, $dir_);
- my @files = grep { !/d4\.dll/ && /\.dll/ } readdir($dh);
- closedir($dh);
- for my $file (@files) {
- my $key = '--' . $file;
- $key =~ s/4?\.dll//g;
- my $file_ = $dir_ . '/' . $file;
- $qt_plugins{'files'}{$key} = $file_;
- push(@{$qt_plugins{'assoc'}{$arg}},$file_);
- }
- } else {
- print STDERR $dir_ . ' not exists, set $qt_plugins{"home"} please' . "\n";
- }
- }
- $qt_plugins{'files'}{'--qmysql'} = $qt_plugins{'files'}{'--qsqlmysql'};
- $qt_plugins{'files'}{'--qodbc'} = $qt_plugins{'files'}{'--qsqlodbc'};
- $qt_plugins{'files'}{'--qpsql'} = $qt_plugins{'files'}{'--qsqlpsql'};
- sub my_wrap {
- my @parts = @_;
- my @result = ();
- my $l = 0;
- my @line = ();
- for my $part (@parts) {
- if ($l + length($part) < 80) {
- $l += length($part);
- push(@line,$part);
- } else {
- push(@result,join('',@line));
- $l = 0;
- @line = ();
- $l += length($part);
- push(@line,$part);
- }
- }
- push(@result,join('',@line));
- return @result;
- }
- sub usage {
- print STDERR "Usage: " . $0 . " [-f] [-n] [-s] [-c] app.exe [foo.dll] [bar.dll] \n";
- print STDERR " -f print found dlls\n";
- print STDERR " -n print not found dlls\n";
- print STDERR " -s print system dlls\n";
- print STDERR " -c copy dependecies to 'distribution' directory\n";
- print STDERR "Include Qt plugins: \n";
- print STDERR " " . join("\n ",my_wrap(split("#",join(",#",keys %{$qt_plugins{'files'}})))) . "\n";
- print STDERR " " . join(",",keys %{$qt_plugins{'dirs'}}) . "\n";
- print STDERR "Example:\n " . $0 . " -c myapp.exe --qmysql options.ini\n";
- }
- sub dlls {
- my ($path) = @_;
- #print STDERR $path . "\n";
- my %result = ();
- my @deps = split("\n",`objdump -p "$path" | grep "DLL Name:"`);
- for my $dep (@deps) {
- if ($dep =~ /DLL Name: (.*)/) {
- my $n = $1;
- my $p = `which "$n"`;
- $p =~ s/\n//;
- $result{$n} = $p;
- }
- }
- return %result;
- }
- # -f found -n not found -s system
- #ls /c/Qt/4.7.4/plugins/sqldrivers/ | grep '.*[^d]\.dll' | sed 's,4.dll,,'
- my $print_found = 0;
- my $print_notfound = 0;
- my $print_system = 0;
- my $copy_dependencies = 0;
- my @bins = ();
- my %deps = ();
- if (scalar(@ARGV)<1) {
- usage();
- exit(0);
- }
- for my $arg (@ARGV) {
- if ($arg eq '-f') {
- $print_found = 1;
- } elsif ($arg eq '-n') {
- $print_notfound = 1;
- } elsif ($arg eq '-s') {
- $print_system = 1;
- } elsif ($arg eq '-c') {
- $copy_dependencies = 1;
- } elsif ($arg eq "-h" || $arg eq "--help") {
- usage();
- exit(0);
- } elsif ( exists $qt_plugins{'files'}{$arg} ) {
- push(@bins,$qt_plugins{'files'}{$arg});
- } elsif ( exists $qt_plugins{'dirs'}{$arg} ) {
- push(@bins,$_) foreach @{$qt_plugins{'assoc'}{$arg}};
- } elsif ( $arg =~ '^--' ) {
- print STDERR 'error: undefined option ' . $arg . "\n";
- } else {
- push(@bins,$arg);
- }
- }
- #print join("\n",@bins) . "\n"; exit(0); #debug
- #print Dumper(@bins); exit(0);
- if ($print_found+$print_notfound+$print_system == 0) {
- $print_found = 1;
- $print_notfound = 1;
- $print_system = 1;
- }
- if (scalar(@bins)==0) {
- print STDERR "Error: pass executable[s] path[es].\n";
- usage();
- exit(-1);
- }
- for my $bin (@bins) {
- %deps = (%deps, dlls($bin));
- }
- #my @deps_ = keys(%deps);
- #print join("\n",values(%deps)) . "\n";
- my %complete = ();
- my $iscomplete = 0;
- while (!$iscomplete) {
- $iscomplete = 1;
- for my $dep (keys %deps) {
- #print STDERR "dep $dep\n";
- #print STDERR "exists\n" if exists $complete{$dep};
- #print STDERR "matches\n" if $deps{$dep} =~ /Windows.system32/;
- if (!exists $complete{$dep} && !($deps{$dep} =~ /Windows.system32/)) {
- $iscomplete = 0;
- my %deps_ = dlls($deps{$dep});
- #print STDERR "deps for " . $deps{$dep} . ":\n" . join("\n",values(%deps_)) . "\n";
- %deps = (%deps, %deps_);
- $complete{$dep} = 1;
- }
- }
- }
- my @found = sort map { $deps{$_} } grep { length($deps{$_}) > 0 && !($deps{$_} =~ /Windows.system32/) } keys %deps;
- my @notfound = sort grep { length($deps{$_}) == 0 } keys %deps;
- my @system = sort map { $deps{$_} } grep { $deps{$_} =~ /Windows.system32/ } keys %deps;
- my $print_header = 0;
- if ($print_found+$print_notfound+$print_system > 1) {
- $print_header = 1;
- }
- if ($print_found) {
- print "FOUND DLLS:\n" if $print_header;
- print join("\n",@found) . "\n" if scalar(@found)>0;
- }
- if ($print_notfound) {
- print "NOT FOUND DLLS:\n" if $print_header;
- print join("\n",@notfound) . "\n" if scalar(@notfound)>0;
- }
- if ($print_system) {
- print "SYSTEM DLLS:\n" if $print_header;
- print join("\n",@system) . "\n" if scalar(@system)>0;
- }
- my $qt_conf = 0;
- my $dest = './distribution';
- if ($copy_dependencies) {
- if ( ! -d $dest ) {
- mkdir($dest);
- }
- for my $dep (@found) {
- #print "cp $dep $dest\n";
- copy($dep,$dest);
- }
- for my $bin (@bins) {
- if (index($bin,$qt_plugins{'home'})>-1) {
- if ($bin =~ m,.*/(plugins/.*),) {
- my $dest_ = dirname($dest . '/' . $1);
- #print $bin . ' dest: ' . $dest_ . "\n"; #debug
- if (! -d $dest_) {
- make_path($dest_);
- }
- copy($bin,$dest_); # @todo overwrite only if newer
- $qt_conf = 1;
- }
- } else {
- #print 'cp ' . $bin . ' ' . $dest . "\n"; #debug
- copy($bin,$dest); # @todo overwrite only if newer
- }
- }
- }
- if ($qt_conf) {
- my $dest_ = $dest . '/qt.conf';
- if ( ! -f $dest_) {
- open OUTPUT,'>', $dest_;
- print OUTPUT "[Paths]\nPlugins = ./plugins\n";
- close OUTPUT;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment