You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Copy-NativeTensorFlowLibs.ps1 6.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <#
  2. .SYNOPSIS
  3. Copy the native TensorFlow library to enable the packing a nuget to make
  4. them available to TensorFlow.NET
  5. .DESCRIPTION
  6. The TensorFlow libraries are copied for Windows and Linux and it becomes
  7. possible to bundle a meta-package containing them.
  8. .PARAMETER SkipCpuLibraries
  9. Setting this to true skips the downloading of the CPU version of the
  10. TensorFlow libraries.
  11. By default the CPU version of the libraries are downloaded and put in the
  12. relevant projects.
  13. .PARAMETER SkipGpuLibraries
  14. Setting this to tru skips the downloading of the GPU version of the
  15. TensorFlow libraries.
  16. By default the GPU version of the libraries are downloaded and put in the
  17. releavant projects.
  18. #>
  19. param(
  20. [switch] $SkipCpuLibraries = $false,
  21. [switch] $SkipGpuLibraries = $false
  22. )
  23. function Expand-TarGzFiles {
  24. <#
  25. .SYNOPSIS
  26. Expands the given list of files from the given archive into the given
  27. target directory.
  28. .PARAMETER Archive
  29. Path to the archive that should be considered.
  30. .PARAMETER Files
  31. Files that should be extracted from the archive.
  32. .PARAMETER TargetDirectory
  33. Directory into which the files should be expanded.
  34. #>
  35. param
  36. (
  37. [Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] [string] $Archive,
  38. [Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] [string []] $Files,
  39. [Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] [string] $TargetDirectory
  40. )
  41. & 7z e $Archive -o"$TargetDirectory"
  42. $TarArchive = Join-Path $TargetDirectory "libtensorflow.tar"
  43. & 7z e $TarArchive $Files -o"$TargetDirectory"
  44. Remove-Item $TarArchive
  45. }
  46. function Expand-ZipFiles {
  47. <#
  48. .SYNOPSIS
  49. Expands the given list of files from the given archive into the given target directory.
  50. .PARAMETER Archive
  51. Path to the archive that should be considered.
  52. .PARAMETER Files
  53. Files that should be extracted from the archive.
  54. .PARAMETER TargetDirectory
  55. Directory into which the files should be expanded.
  56. #>
  57. param(
  58. [Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] [string] $Archive,
  59. [Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] [string []] $Files,
  60. [Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] [string] $TargetDirectory
  61. )
  62. & 7z e $Archive $Files -o"$TargetDirectory"
  63. }
  64. function Split-ArchiveFromUrl {
  65. <#
  66. .SYNOPSIS
  67. Extracts the archive name out of the given Url.
  68. .PARAMETER ArchiveUrl
  69. Url of the archive that will be downloaded.
  70. #>
  71. param(
  72. [Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] [string] $ArchiveUrl
  73. )
  74. $uriParts = $ArchiveUrl.split("/")
  75. $ArchivePath = $uriParts[$uriParts.Count - 1]
  76. return $ArchivePath
  77. }
  78. function Copy-Archive {
  79. <#
  80. .SYNOPSIS
  81. This function copies the given binary file to the given target location.
  82. .PARAMETER ArchiveUrl
  83. Url where the archive should be downloaded from.
  84. .PARAMETER TargetDirectory
  85. Target directory where the archive should be downloaded.
  86. #>
  87. param (
  88. [Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
  89. [string] $ArchiveUrl,
  90. [Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
  91. [string] $TargetDirectory
  92. )
  93. $ArchiveName = Split-ArchiveFromUrl $ArchiveUrl
  94. $TargetPath = [IO.Path]::Combine($PSScriptRoot, "..", "packages", $ArchiveName)
  95. if (Test-Path $TargetPath -PathType Leaf) {
  96. Write-Error "$TargetPath already exists, please remove to download againg."
  97. return $TargetPath
  98. }
  99. if (-not (Test-Path $TargetDirectory -PathType Container)) {
  100. Write-Host "Creating missing $TargetDirectory"
  101. New-Item -Path $TargetDirectory -ItemType Directory
  102. }
  103. Write-Host "Downloading $ArchiveUrl, this might take a while..."
  104. $wc = New-Object System.Net.WebClient
  105. $wc.DownloadFile($ArchiveUrl, $TargetPath)
  106. return $TargetPath
  107. }
  108. $LinuxGpuArchive = "https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-gpu-linux-x86_64-1.14.0.tar.gz"
  109. $LinuxCpuArchive = "https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-cpu-linux-x86_64-1.14.0.tar.gz"
  110. $LinuxFiles = @(".\libtensorflow.tar", ".\lib\libtensorflow.so", ".\lib\libtensorflow.so.1", ".\lib\libtensorflow.so.1.14.0", `
  111. ".\lib\libtensorflow_framework.so", ".\lib\libtensorflow_framework.so.1", ".\lib\libtensorflow_framework.so.1.14.0")
  112. $WindowsGpuArchive = "https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-gpu-windows-x86_64-1.14.0.zip"
  113. $WindowsCpuArchive = "https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-cpu-windows-x86_64-1.14.0.zip"
  114. $WindowsFiles = @("lib\tensorflow.dll")
  115. $PackagesDirectory = [IO.Path]::Combine($PSScriptRoot, "..", "packages")
  116. if (-not $SkipGpuLibraries) {
  117. $Archive = Copy-Archive -ArchiveUrl $WindowsGpuArchive -TargetDirectory $PackagesDirectory
  118. $TargetDirectory = [IO.Path]::Combine($PSScriptRoot, "..", "redist", "runtime.win-x64.SciSharp.TensorFlow-Gpu.Redist")
  119. Expand-ZipFiles $Archive $WindowsFiles $TargetDirectory
  120. $Archive = Copy-Archive -ArchiveUrl $LinuxGpuArchive -TargetDirectory $PackagesDirectory
  121. $TargetDirectory = [IO.Path]::Combine($PSScriptRoot, "..", "redist", "runtime.linux-x64.SciSharp.Tensorflow-Gpu.Redist")
  122. Expand-TarGzFiles $Archive $LinuxFiles $TargetDirectory
  123. }
  124. if (-not $SkipCpuLibraries) {
  125. $Archive = Copy-Archive -ArchiveUrl $WindowsCpuArchive -TargetDirectory $PackagesDirectory
  126. $TargetDirectory = [IO.Path]::Combine($PSScriptRoot, "..", "redist", "runtime.win-x64.SciSharp.TensorFlow-Cpu.Redist")
  127. Expand-ZipFiles $Archive $WindowsFiles $TargetDirectory
  128. $Archive = Copy-Archive -ArchiveUrl $LinuxCpuArchive -TargetDirectory $PackagesDirectory
  129. $TargetDirectory = [IO.Path]::Combine($PSScriptRoot, "..", "redist", "runtime.linux-x64.SciSharp.Tensorflow-Cpu.Redist")
  130. Expand-TarGzFiles $Archive $LinuxFiles $TargetDirectory
  131. }