UtilityFunctions.ps1 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. # Set-up Visual Studio Command Prompt environment for PowerShell
  2. pushd "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\Tools\"
  3. cmd /c "VsDevCmd.bat -arch=x64 & set" | foreach {
  4. if ($_ -match "=") {
  5. $v = $_.split("="); Set-Item -Force -Path "ENV:\$($v[0])" -Value "$($v[1])"
  6. }
  7. }
  8. popd
  9. function Which ($search_path, $name) {
  10. ($search_path).Split(";") | Get-ChildItem -Filter $name | Select -First 1 -Exp FullName
  11. }
  12. function GetDeps ($search_path, $binary) {
  13. ((dumpbin /dependents $binary).Where({ $_ -match "dependencies:"}, "SkipUntil") | Select-String "[^ ]*\.dll").Matches | foreach {
  14. Which $search_path $_.Value
  15. }
  16. }
  17. function RecursivelyGetDeps ($search_path, $binary) {
  18. $final_deps = @()
  19. $deps_to_process = GetDeps $search_path $binary
  20. while ($deps_to_process.Count -gt 0) {
  21. $current, $deps_to_process = $deps_to_process
  22. if ($final_deps -contains $current) { continue }
  23. # Is this a system dll file?
  24. # We use the same algorithm that cmake uses to determine this.
  25. if ($current -match "$([regex]::Escape($env:SystemRoot))\\sys") { continue }
  26. if ($current -match "$([regex]::Escape($env:WinDir))\\sys") { continue }
  27. if ($current -match "\\msvc[^\\]+dll") { continue }
  28. if ($current -match "\\api-ms-win-[^\\]+dll") { continue }
  29. $final_deps += $current
  30. $new_deps = GetDeps $search_path $current
  31. $deps_to_process += ($new_deps | ?{-not ($final_deps -contains $_)})
  32. }
  33. return $final_deps
  34. }