File: copy_dlls

package info (click to toggle)
qpdf 8.0.2-2~bpo9+1
  • links: PTS
  • area: main
  • in suites: stretch-backports
  • size: 20,056 kB
  • sloc: cpp: 29,209; perl: 6,102; xml: 5,043; sh: 3,932; ansic: 2,743; makefile: 92
file content (144 lines) | stat: -rw-r--r-- 3,208 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env perl

require 5.008;
BEGIN { $^W = 1; }
use strict;
use File::Basename;

my $whoami = basename($0);

usage() unless @ARGV == 4;
my ($file, $destdir, $objdump, $windows_wordsize) = @ARGV;
my $filedir = dirname($file);

my $sep = ($^O eq 'MSWin32' ? ';' : ':');
my @path = ($filedir, '.', split($sep, $ENV{'PATH'}));
foreach my $var (qw(LIB))
{
    if (exists $ENV{$var})
    {
        push(@path, split($sep, $ENV{$var}));
    }
}
my $redist_suffix = (($windows_wordsize eq '64') ? "x64" : "x86");
if (exists $ENV{'VCINSTALLDIR'})
{
    my $redist = $ENV{'VCINSTALLDIR'} . "/Redist/$redist_suffix";
    if (opendir(D, $redist))
    {
        my @entries = readdir(D);
        closedir(D);
        foreach my $e (@entries)
        {
            if ($e =~ m/\.CRT$/i)
            {
                unshift(@path, "$redist/$e");
            }
        }
    }
}
if (exists $ENV{'UniversalCRTSdkDir'})
{
    my $redist = $ENV{'UniversalCRTSdkDir'} . "/Redist/ucrt/DLLs/$redist_suffix";
    unshift(@path, $redist);
}

my $format = undef;
my @to_find = get_dlls($file);

my %final = ();
my @notfound = ();

while (@to_find)
{
    my $dll = shift(@to_find);
    my $found = 0;
    foreach my $dir (@path)
    {
        if ((-f "$dir/$dll") && is_format("$dir/$dll", $format))
        {
            if (! exists $final{$dll})
            {
                $final{$dll} = "$dir/$dll";
                push(@to_find, get_dlls("$dir/$dll"));
            }
            $found = 1;
            last;
        }
    }
    if (! $found)
    {
        push(@notfound, $dll);
    }
}
if (@notfound)
{
    die "$whoami: can't find the following dlls: " .
	join(', ', @notfound), "\n";
}

foreach my $dll (sort keys (%final))
{
    my $f = $final{$dll};
    $f =~ s,\\,/,g;
    print "Copying $f to $destdir\n";
    system("cp -p '$f' '$destdir'") == 0 or
	die "$whoami: copy $f to $destdir failed\n";
}

sub get_dlls
{
    my @result = ();
    my $exe = shift;
    open(O, "$objdump -p \"$exe\"|") or die "$whoami: can't run objdump\n";
    while (<O>)
    {
        if (m/^\s+DLL Name:\s+(.+\.dll)/i)
        {
            my $dll = $1;
            $dll =~ tr/A-Z/a-z/;
            next if $dll =~ m/^(kernel32|user32|msvcrt|advapi32)\.dll$/;
            push(@result, $dll);
        }
        elsif (m/^Magic.*\((PE.+?)\)/)
        {
            $format = $1;
        }
    }
    close(O);
    if (! defined $format)
    {
        die "$whoami: can't determine format of $exe\n";
    }
    @result;
}

sub is_format
{
    my ($file, $format) = @_;
    $file =~ s,\\,/,g;
    # Special case: msvc*.dll seem to be able to behave both as 32-bit
    # and 64-bit DLLs.  Either that, or this logic is wrong for those
    # DLLs and it doesn't matter because they're already installed on
    # my test system (which doesn't have msvc installed on it).
    if ($file =~ m,/msvc,i)
    {
        return 1;
    }
    my $result = 0;
    my $file_format = `file "$file"`;
    print "$file $format $file_format\n";
    if ($? == 0)
    {
        if ($file_format =~ m/\Q${format}\E executable/)
        {
            $result = 1;
        }
    }
    $result;
}

sub usage
{
    die "Usage: $whoami {exe|dll} destdir\n";
}