build.ps1 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #!/usr/bin/env pwsh
  2. $number_of_build_workers=8
  3. $use_vcpkg=$true
  4. $use_ninja=$false
  5. $force_cpp_build=$false
  6. function getProgramFiles32bit() {
  7. $out = ${env:PROGRAMFILES(X86)}
  8. if ($null -eq $out) {
  9. $out = ${env:PROGRAMFILES}
  10. }
  11. if ($null -eq $out) {
  12. throw "Could not find [Program Files 32-bit]"
  13. }
  14. return $out
  15. }
  16. function getLatestVisualStudioWithDesktopWorkloadPath() {
  17. $programFiles = getProgramFiles32bit
  18. $vswhereExe = "$programFiles\Microsoft Visual Studio\Installer\vswhere.exe"
  19. if (Test-Path $vswhereExe) {
  20. $output = & $vswhereExe -products * -latest -requires Microsoft.VisualStudio.Workload.NativeDesktop -format xml
  21. [xml]$asXml = $output
  22. foreach ($instance in $asXml.instances.instance) {
  23. $installationPath = $instance.InstallationPath -replace "\\$" # Remove potential trailing backslash
  24. }
  25. if (!$installationPath) {
  26. Write-Host "Warning: no full Visual Studio setup has been found, extending search to include also partial installations" -ForegroundColor Yellow
  27. $output = & $vswhereExe -products * -latest -format xml
  28. [xml]$asXml = $output
  29. foreach ($instance in $asXml.instances.instance) {
  30. $installationPath = $instance.InstallationPath -replace "\\$" # Remove potential trailing backslash
  31. }
  32. }
  33. if (!$installationPath) {
  34. Throw "Could not locate any installation of Visual Studio"
  35. }
  36. }
  37. else {
  38. Throw "Could not locate vswhere at $vswhereExe"
  39. }
  40. return $installationPath
  41. }
  42. function getLatestVisualStudioWithDesktopWorkloadVersion() {
  43. $programFiles = getProgramFiles32bit
  44. $vswhereExe = "$programFiles\Microsoft Visual Studio\Installer\vswhere.exe"
  45. if (Test-Path $vswhereExe) {
  46. $output = & $vswhereExe -products * -latest -requires Microsoft.VisualStudio.Workload.NativeDesktop -format xml
  47. [xml]$asXml = $output
  48. foreach ($instance in $asXml.instances.instance) {
  49. $installationVersion = $instance.InstallationVersion
  50. }
  51. if (!$installationVersion) {
  52. Write-Host "Warning: no full Visual Studio setup has been found, extending search to include also partial installations" -ForegroundColor Yellow
  53. $output = & $vswhereExe -products * -latest -format xml
  54. [xml]$asXml = $output
  55. foreach ($instance in $asXml.instances.instance) {
  56. $installationVersion = $instance.installationVersion
  57. }
  58. }
  59. if (!$installationVersion) {
  60. Throw "Could not locate any installation of Visual Studio"
  61. }
  62. }
  63. else {
  64. Throw "Could not locate vswhere at $vswhereExe"
  65. }
  66. return $installationVersion
  67. }
  68. if ((Test-Path env:VCPKG_ROOT) -and $use_vcpkg) {
  69. $vcpkg_path = "$env:VCPKG_ROOT"
  70. Write-Host "Found vcpkg in VCPKG_ROOT: $vcpkg_path"
  71. }
  72. elseif ((Test-Path "${env:WORKSPACE}\vcpkg") -and $use_vcpkg) {
  73. $vcpkg_path = "${env:WORKSPACE}\vcpkg"
  74. Write-Host "Found vcpkg in WORKSPACE\vcpkg: $vcpkg_path"
  75. }
  76. else {
  77. Write-Host "Skipping vcpkg-enabled builds because the VCPKG_ROOT environment variable is not defined or you requested to avoid VCPKG, using self-distributed libs`n" -ForegroundColor Yellow
  78. }
  79. if ($null -eq $env:VCPKG_DEFAULT_TRIPLET -and $use_vcpkg) {
  80. Write-Host "No default triplet has been set-up for vcpkg. Defaulting to x64-windows" -ForegroundColor Yellow
  81. $vcpkg_triplet = "x64-windows"
  82. }
  83. elseif ($use_vcpkg) {
  84. $vcpkg_triplet = $env:VCPKG_DEFAULT_TRIPLET
  85. }
  86. if ($vcpkg_triplet -Match "x86" -and $use_vcpkg) {
  87. Throw "darknet is supported only in x64 builds!"
  88. }
  89. if ($null -eq (Get-Command "cl.exe" -ErrorAction SilentlyContinue)) {
  90. $vsfound = getLatestVisualStudioWithDesktopWorkloadPath
  91. Write-Host "Found VS in ${vsfound}"
  92. Push-Location "${vsfound}\Common7\Tools"
  93. cmd.exe /c "VsDevCmd.bat -arch=x64 & set" |
  94. ForEach-Object {
  95. if ($_ -match "=") {
  96. $v = $_.split("="); Set-Item -force -path "ENV:\$($v[0])" -value "$($v[1])"
  97. }
  98. }
  99. Pop-Location
  100. Write-Host "Visual Studio Command Prompt variables set" -ForegroundColor Yellow
  101. }
  102. $tokens = getLatestVisualStudioWithDesktopWorkloadVersion
  103. $tokens = $tokens.split('.')
  104. if($use_ninja) {
  105. $generator = "Ninja"
  106. }
  107. else {
  108. if ($tokens[0] -eq "14") {
  109. $generator = "Visual Studio 14 2015"
  110. }
  111. elseif ($tokens[0] -eq "15") {
  112. $generator = "Visual Studio 15 2017"
  113. }
  114. elseif ($tokens[0] -eq "16") {
  115. $generator = "Visual Studio 16 2019"
  116. }
  117. else {
  118. throw "Unknown Visual Studio version, unsupported configuration"
  119. }
  120. }
  121. Write-Host "Setting up environment to use CMake generator: $generator" -ForegroundColor Yellow
  122. if ($null -eq (Get-Command "nvcc.exe" -ErrorAction SilentlyContinue)) {
  123. if (Test-Path env:CUDA_PATH) {
  124. $env:PATH += ";${env:CUDA_PATH}\bin"
  125. Write-Host "Found cuda in ${env:CUDA_PATH}" -ForegroundColor Yellow
  126. }
  127. else {
  128. Write-Host "Unable to find CUDA, if necessary please install it or define a CUDA_PATH env variable pointing to the install folder" -ForegroundColor Yellow
  129. }
  130. }
  131. if (Test-Path env:CUDA_PATH) {
  132. if (-Not(Test-Path env:CUDA_TOOLKIT_ROOT_DIR)) {
  133. $env:CUDA_TOOLKIT_ROOT_DIR = "${env:CUDA_PATH}"
  134. Write-Host "Added missing env variable CUDA_TOOLKIT_ROOT_DIR" -ForegroundColor Yellow
  135. }
  136. }
  137. if($force_cpp_build) {
  138. $additional_build_setup="-DBUILD_AS_CPP:BOOL=TRUE"
  139. }
  140. if ($use_vcpkg) {
  141. ## DEBUG
  142. #New-Item -Path .\build_win_debug -ItemType directory -Force
  143. #Set-Location build_win_debug
  144. #if ($use_ninja) {
  145. #cmake -G "$generator" "-DCMAKE_TOOLCHAIN_FILE=$vcpkg_path\scripts\buildsystems\vcpkg.cmake" "-DVCPKG_TARGET_TRIPLET=$vcpkg_triplet" #"-DCMAKE_BUILD_TYPE=Debug" $additional_build_setup ..
  146. #$dllfolder = "."
  147. #}
  148. #else {
  149. #cmake -G "$generator" -T "host=x64" -A "x64" "-DCMAKE_TOOLCHAIN_FILE=$vcpkg_path\scripts\buildsystems\vcpkg.cmake" "-DVCPKG_TARGET_TRIPLET=$vcpkg_triplet" "-DCMAKE_BUILD_TYPE=Debug" $additional_build_setup ..
  150. #$dllfolder = "Debug"
  151. #}
  152. #cmake --build . --config Debug --target install
  153. ##cmake --build . --config Debug --parallel ${number_of_build_workers} --target install #valid only for CMake 3.12+
  154. #Remove-Item DarknetConfig.cmake
  155. #Remove-Item DarknetConfigVersion.cmake
  156. #$dllfiles = Get-ChildItem ${dllfolder}\*.dll
  157. #if ($dllfiles) {
  158. # Copy-Item $dllfiles ..
  159. #}
  160. #Set-Location ..
  161. #Copy-Item cmake\Modules\*.cmake share\darknet\
  162. # RELEASE
  163. New-Item -Path .\build_win_release -ItemType directory -Force
  164. Set-Location build_win_release
  165. if($use_ninja) {
  166. cmake -G "$generator" "-DCMAKE_TOOLCHAIN_FILE=$vcpkg_path\scripts\buildsystems\vcpkg.cmake" "-DVCPKG_TARGET_TRIPLET=$vcpkg_triplet" "-DCMAKE_BUILD_TYPE=Release" $additional_build_setup ..
  167. $dllfolder = "."
  168. }
  169. else {
  170. cmake -G "$generator" -T "host=x64" -A "x64" "-DCMAKE_TOOLCHAIN_FILE=$vcpkg_path\scripts\buildsystems\vcpkg.cmake" "-DVCPKG_TARGET_TRIPLET=$vcpkg_triplet" "-DCMAKE_BUILD_TYPE=Release" $additional_build_setup ..
  171. $dllfolder = "Release"
  172. }
  173. cmake --build . --config Release --target install
  174. #cmake --build . --config Release --parallel ${number_of_build_workers} --target install #valid only for CMake 3.12+
  175. Remove-Item DarknetConfig.cmake
  176. Remove-Item DarknetConfigVersion.cmake
  177. $dllfiles = Get-ChildItem ${dllfolder}\*.dll
  178. if ($dllfiles) {
  179. Copy-Item $dllfiles ..
  180. }
  181. Set-Location ..
  182. Copy-Item cmake\Modules\*.cmake share\darknet\
  183. }
  184. else {
  185. # USE LOCAL PTHREAD LIB AND LOCAL STB HEADER, NO VCPKG, ONLY RELEASE MODE SUPPORTED
  186. # if you want to manually force this case, remove VCPKG_ROOT env variable and remember to use "vcpkg integrate remove" in case you had enabled user-wide vcpkg integration
  187. New-Item -Path .\build_win_release_novcpkg -ItemType directory -Force
  188. Set-Location build_win_release_novcpkg
  189. if($use_ninja) {
  190. cmake -G "$generator" $additional_build_setup ..
  191. }
  192. else {
  193. cmake -G "$generator" -T "host=x64" -A "x64" $additional_build_setup ..
  194. }
  195. cmake --build . --config Release --target install
  196. #cmake --build . --config Release --parallel ${number_of_build_workers} --target install #valid only for CMake 3.12+
  197. Remove-Item DarknetConfig.cmake
  198. Remove-Item DarknetConfigVersion.cmake
  199. $dllfolder = "..\3rdparty\pthreads\bin"
  200. $dllfiles = Get-ChildItem ${dllfolder}\*.dll
  201. if ($dllfiles) {
  202. Copy-Item $dllfiles ..
  203. }
  204. Set-Location ..
  205. Copy-Item cmake\Modules\*.cmake share\darknet\
  206. }