File: copy_dlls.ps1

package info (click to toggle)
qpdf 12.2.0-1
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 71,572 kB
  • sloc: cpp: 56,805; perl: 12,015; ansic: 6,547; sh: 1,181; python: 1,027; xml: 43; makefile: 42
file content (42 lines) | stat: -rwxr-xr-x 1,131 bytes parent folder | download
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
param (
    [string]$exe,
    [string]$dest,
    [string]$mingwBinDir
)

function Get-DllDependencies {
    param([string]$binary)
    & objdump.exe -p $binary 2>$null |
      Select-String 'DLL Name:' |
      ForEach-Object { ($_ -split ':')[1].Trim() }
}

if (-not $exe -or -not $dest -or -not $mingwBinDir) {
    Write-Error "Usage: $(Split-Path -Leaf $MyInvocation.MyCommand.Name) -exe exe -dest dest -mingwBinDir mingw-bin-dir"
    exit 2
}

New-Item -ItemType Directory -Path $dest -Force | Out-Null
$dlls = [System.Collections.Generic.Queue[string]]::new()
[void]$dlls.Enqueue($exe)
$seen = @{}
foreach ($dll in Get-DllDependencies $exe) {
    $dlls.Enqueue($dll)
}

while ($dlls.Count -gt 0) {
    $item = $dlls.Dequeue()
    $basename = if (Test-Path $item) { Split-Path $item -Leaf } else { $item }

    if ($seen[$basename]) {
        continue
    }
    $seen[$basename] = $true
    $full = Join-Path $mingwBinDir $basename
    if (Test-Path $full -PathType Leaf) {
        Copy-Item $full -Destination $dest -Force
        foreach ($dll in Get-DllDependencies $full) {
            $dlls.Enqueue($dll)
        }
    }
}