110 lines
2.2 KiB
PowerShell
110 lines
2.2 KiB
PowerShell
param(
|
|
[Parameter(Mandatory = $true)]
|
|
[string]$Executable,
|
|
|
|
[Parameter(Mandatory = $false)]
|
|
[string]$Output = "artifacts"
|
|
)
|
|
|
|
$ErrorActionPreference = "Stop"
|
|
|
|
if (-not (Test-Path $Executable)) {
|
|
throw "Executable does not exist: $Executable"
|
|
}
|
|
|
|
New-Item -ItemType Directory -Force -Path $Output | Out-Null
|
|
|
|
$windowsDirectories = @(
|
|
$env:WINDIR,
|
|
(Join-Path $env:WINDIR "System32"),
|
|
(Join-Path $env:WINDIR "SysWOW64")
|
|
)
|
|
|
|
$processed = @{}
|
|
|
|
function Test-IsWindowsSystemDll {
|
|
param(
|
|
[string]$Name
|
|
)
|
|
|
|
foreach ($directory in $windowsDirectories) {
|
|
$path = Join-Path $directory $Name
|
|
|
|
if (Test-Path $path) {
|
|
return $true
|
|
}
|
|
}
|
|
|
|
return $false
|
|
}
|
|
|
|
function Find-Dependency {
|
|
param(
|
|
[string]$Name,
|
|
[string]$BinaryDirectory
|
|
)
|
|
|
|
$localPath = Join-Path $BinaryDirectory $Name
|
|
|
|
if (Test-Path $localPath) {
|
|
return $localPath
|
|
}
|
|
|
|
$command = Get-Command $Name -ErrorAction SilentlyContinue
|
|
|
|
if ($command) {
|
|
return $command.Source
|
|
}
|
|
|
|
return $null
|
|
}
|
|
|
|
function Copy-Dependencies {
|
|
param(
|
|
[string]$Binary
|
|
)
|
|
|
|
$binaryDirectory = Split-Path -Parent $Binary
|
|
|
|
$dependencies = dumpbin /DEPENDENTS $Binary |
|
|
Select-String '^\s+.*\.dll$' |
|
|
ForEach-Object { $_.Line.Trim() } |
|
|
Sort-Object -Unique
|
|
|
|
foreach ($dll in $dependencies) {
|
|
|
|
$key = $dll.ToLowerInvariant()
|
|
|
|
if ($processed.ContainsKey($key)) {
|
|
continue
|
|
}
|
|
|
|
$processed[$key] = $true
|
|
|
|
if (Test-IsWindowsSystemDll $dll) {
|
|
Write-Host "Skipping Windows system dependency: $dll"
|
|
continue
|
|
}
|
|
|
|
$source = Find-Dependency `
|
|
-Name $dll `
|
|
-BinaryDirectory $binaryDirectory
|
|
|
|
if (-not $source) {
|
|
Write-Warning "Could not locate dependency: $dll"
|
|
continue
|
|
}
|
|
|
|
$destination = Join-Path $Output (Split-Path -Leaf $source)
|
|
|
|
if (-not (Test-Path $destination)) {
|
|
Write-Host "Copying dependency: $source"
|
|
Copy-Item $source $destination -Force
|
|
}
|
|
|
|
Copy-Dependencies -Binary $source
|
|
}
|
|
}
|
|
|
|
Copy-Dependencies -Binary $Executable
|