2019-02-18 06:56:25 +08:00
#!/usr/bin/env pwsh
2019-02-19 21:18:08 +08:00
$number_of_build_workers = 8
2019-06-13 12:54:54 +08:00
$use_vcpkg = $true
2019-07-18 19:14:09 +08:00
$use_ninja = $false
2019-07-23 21:59:02 +08:00
$force_cpp_build = $false
2019-02-19 21:18:08 +08:00
2019-04-09 15:06:58 +08:00
function getProgramFiles32bit ( ) {
$out = $ { env : PROGRAMFILES ( X86 ) }
if ( $null -eq $out ) {
$out = $ { env : PROGRAMFILES }
}
if ( $null -eq $out ) {
throw " Could not find [Program Files 32-bit] "
}
return $out
}
function getLatestVisualStudioWithDesktopWorkloadPath ( ) {
$programFiles = getProgramFiles32bit
$vswhereExe = " $programFiles \Microsoft Visual Studio\Installer\vswhere.exe "
if ( Test-Path $vswhereExe ) {
2019-04-10 16:51:54 +08:00
$output = & $vswhereExe -products * -latest -requires Microsoft . VisualStudio . Workload . NativeDesktop -format xml
2019-04-09 15:06:58 +08:00
[ xml ] $asXml = $output
2019-04-10 16:51:54 +08:00
foreach ( $instance in $asXml . instances . instance ) {
$installationPath = $instance . InstallationPath -replace " \\ $ " # Remove potential trailing backslash
}
if ( ! $installationPath ) {
Write-Host " Warning: no full Visual Studio setup has been found, extending search to include also partial installations " -ForegroundColor Yellow
$output = & $vswhereExe -products * -latest -format xml
[ xml ] $asXml = $output
foreach ( $instance in $asXml . instances . instance ) {
2019-04-09 15:06:58 +08:00
$installationPath = $instance . InstallationPath -replace " \\ $ " # Remove potential trailing backslash
2019-04-10 16:51:54 +08:00
}
}
if ( ! $installationPath ) {
Throw " Could not locate any installation of Visual Studio "
2019-04-09 15:06:58 +08:00
}
}
else {
2019-04-10 16:51:54 +08:00
Throw " Could not locate vswhere at $vswhereExe "
2019-04-09 15:06:58 +08:00
}
return $installationPath
}
function getLatestVisualStudioWithDesktopWorkloadVersion ( ) {
$programFiles = getProgramFiles32bit
$vswhereExe = " $programFiles \Microsoft Visual Studio\Installer\vswhere.exe "
if ( Test-Path $vswhereExe ) {
2019-04-10 16:51:54 +08:00
$output = & $vswhereExe -products * -latest -requires Microsoft . VisualStudio . Workload . NativeDesktop -format xml
2019-04-09 15:06:58 +08:00
[ xml ] $asXml = $output
2019-04-10 16:51:54 +08:00
foreach ( $instance in $asXml . instances . instance ) {
$installationVersion = $instance . InstallationVersion
}
if ( ! $installationVersion ) {
Write-Host " Warning: no full Visual Studio setup has been found, extending search to include also partial installations " -ForegroundColor Yellow
$output = & $vswhereExe -products * -latest -format xml
[ xml ] $asXml = $output
foreach ( $instance in $asXml . instances . instance ) {
$installationVersion = $instance . installationVersion
}
}
if ( ! $installationVersion ) {
Throw " Could not locate any installation of Visual Studio "
2019-04-09 15:06:58 +08:00
}
}
else {
2019-04-10 16:51:54 +08:00
Throw " Could not locate vswhere at $vswhereExe "
2019-04-09 15:06:58 +08:00
}
return $installationVersion
}
2019-06-13 12:54:54 +08:00
if ( ( Test-Path env : VCPKG_ROOT ) -and $use_vcpkg ) {
2019-03-03 21:34:50 +08:00
$vcpkg_path = " $env:VCPKG_ROOT "
Write-Host " Found vcpkg in VCPKG_ROOT: $vcpkg_path "
}
2019-06-13 12:54:54 +08:00
elseif ( ( Test-Path " ${env:WORKSPACE} \vcpkg " ) -and $use_vcpkg ) {
2019-03-03 21:34:50 +08:00
$vcpkg_path = " ${env:WORKSPACE} \vcpkg "
Write-Host " Found vcpkg in WORKSPACE\vcpkg: $vcpkg_path "
}
else {
2019-06-13 12:54:54 +08:00
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
2019-03-03 21:34:50 +08:00
}
2019-06-13 12:54:54 +08:00
if ( $null -eq $env:VCPKG_DEFAULT_TRIPLET -and $use_vcpkg ) {
2019-04-10 16:51:54 +08:00
Write-Host " No default triplet has been set-up for vcpkg. Defaulting to x64-windows " -ForegroundColor Yellow
2019-03-03 21:34:50 +08:00
$vcpkg_triplet = " x64-windows "
}
2019-06-13 12:54:54 +08:00
elseif ( $use_vcpkg ) {
2019-03-03 21:34:50 +08:00
$vcpkg_triplet = $env:VCPKG_DEFAULT_TRIPLET
}
2019-06-13 12:54:54 +08:00
if ( $vcpkg_triplet -Match " x86 " -and $use_vcpkg ) {
2019-03-03 21:34:50 +08:00
Throw " darknet is supported only in x64 builds! "
}
if ( $null -eq ( Get-Command " cl.exe " -ErrorAction SilentlyContinue ) ) {
2019-04-10 16:51:54 +08:00
$vsfound = getLatestVisualStudioWithDesktopWorkloadPath
2019-04-09 15:06:58 +08:00
Write-Host " Found VS in ${vsfound} "
Push-Location " ${vsfound} \Common7\Tools "
2019-04-10 16:51:54 +08:00
cmd . exe / c " VsDevCmd.bat -arch=x64 & set " |
ForEach-Object {
2019-02-19 21:18:08 +08:00
if ( $_ -match " = " ) {
2019-04-10 16:51:54 +08:00
$v = $_ . split ( " = " ) ; Set-Item -force -path " ENV:\ $( $v [ 0 ] ) " -value " $( $v [ 1 ] ) "
2019-02-19 21:18:08 +08:00
}
2019-02-18 06:56:25 +08:00
}
2019-03-03 21:34:50 +08:00
Pop-Location
2019-04-10 16:51:54 +08:00
Write-Host " Visual Studio Command Prompt variables set " -ForegroundColor Yellow
2019-02-18 06:56:25 +08:00
}
2019-04-09 15:06:58 +08:00
$tokens = getLatestVisualStudioWithDesktopWorkloadVersion
$tokens = $tokens . split ( '.' )
2019-07-18 19:14:09 +08:00
if ( $use_ninja ) {
2019-07-12 04:35:08 +08:00
$generator = " Ninja "
2019-04-09 15:06:58 +08:00
}
else {
2019-07-12 04:35:08 +08:00
if ( $tokens [ 0 ] -eq " 14 " ) {
$generator = " Visual Studio 14 2015 "
}
elseif ( $tokens [ 0 ] -eq " 15 " ) {
$generator = " Visual Studio 15 2017 "
}
elseif ( $tokens [ 0 ] -eq " 16 " ) {
$generator = " Visual Studio 16 2019 "
}
else {
throw " Unknown Visual Studio version, unsupported configuration "
}
2019-04-09 15:06:58 +08:00
}
2019-04-10 16:51:54 +08:00
Write-Host " Setting up environment to use CMake generator: $generator " -ForegroundColor Yellow
2019-04-09 15:06:58 +08:00
2019-03-04 17:22:13 +08:00
if ( $null -eq ( Get-Command " nvcc.exe " -ErrorAction SilentlyContinue ) ) {
if ( Test-Path env : CUDA_PATH ) {
$env:PATH + = " ; ${env:CUDA_PATH} \bin "
2019-04-10 16:51:54 +08:00
Write-Host " Found cuda in ${env:CUDA_PATH} " -ForegroundColor Yellow
2019-03-04 17:22:13 +08:00
}
else {
2019-04-10 16:51:54 +08:00
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
2019-03-04 17:22:13 +08:00
}
}
if ( Test-Path env : CUDA_PATH ) {
if ( -Not ( Test-Path env : CUDA_TOOLKIT_ROOT_DIR ) ) {
$env:CUDA_TOOLKIT_ROOT_DIR = " ${env:CUDA_PATH} "
2019-04-10 16:51:54 +08:00
Write-Host " Added missing env variable CUDA_TOOLKIT_ROOT_DIR " -ForegroundColor Yellow
2019-03-04 17:22:13 +08:00
}
}
2019-07-23 21:59:02 +08:00
if ( $force_cpp_build ) {
$additional_build_setup = " -DBUILD_AS_CPP:BOOL=TRUE "
}
2019-03-04 06:35:38 +08:00
2019-06-13 12:54:54 +08:00
if ( $use_vcpkg ) {
2019-04-11 00:20:55 +08:00
## DEBUG
#New-Item -Path .\build_win_debug -ItemType directory -Force
#Set-Location build_win_debug
2019-07-18 19:14:09 +08:00
#if ($use_ninja) {
2019-07-12 04:35:08 +08:00
#cmake -G "$generator" "-DCMAKE_TOOLCHAIN_FILE=$vcpkg_path\scripts\buildsystems\vcpkg.cmake" "-DVCPKG_TARGET_TRIPLET=$vcpkg_triplet" #"-DCMAKE_BUILD_TYPE=Debug" $additional_build_setup ..
#$dllfolder = "."
#}
#else {
#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 ..
#$dllfolder = "Debug"
#}
2019-04-11 00:20:55 +08:00
#cmake --build . --config Debug --target install
##cmake --build . --config Debug --parallel ${number_of_build_workers} --target install #valid only for CMake 3.12+
#Remove-Item DarknetConfig.cmake
#Remove-Item DarknetConfigVersion.cmake
2019-07-12 04:35:08 +08:00
#$dllfiles = Get-ChildItem ${dllfolder}\*.dll
#if ($dllfiles) {
# Copy-Item $dllfiles ..
#}
2019-04-11 00:20:55 +08:00
#Set-Location ..
#Copy-Item cmake\Modules\*.cmake share\darknet\
2019-03-20 03:07:02 +08:00
# RELEASE
New-Item -Path . \ build_win_release -ItemType directory -Force
Set-Location build_win_release
2019-07-18 19:14:09 +08:00
if ( $use_ninja ) {
2019-07-12 04:35:08 +08:00
cmake -G " $generator " " -DCMAKE_TOOLCHAIN_FILE= $vcpkg_path \scripts\buildsystems\vcpkg.cmake " " -DVCPKG_TARGET_TRIPLET= $vcpkg_triplet " " -DCMAKE_BUILD_TYPE=Release " $additional_build_setup . .
$dllfolder = " . "
}
else {
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 . .
$dllfolder = " Release "
}
2019-03-29 00:54:27 +08:00
cmake - -build . - -config Release - -target install
#cmake --build . --config Release --parallel ${number_of_build_workers} --target install #valid only for CMake 3.12+
2019-03-20 03:07:02 +08:00
Remove-Item DarknetConfig . cmake
Remove-Item DarknetConfigVersion . cmake
2019-07-12 04:35:08 +08:00
$dllfiles = Get-ChildItem $ { dllfolder } \ * . dll
if ( $dllfiles ) {
Copy-Item $dllfiles . .
}
2019-03-20 03:07:02 +08:00
Set-Location . .
2019-03-29 00:54:27 +08:00
Copy-Item cmake \ Modules \ * . cmake share \ darknet \
2019-02-22 23:56:58 +08:00
}
else {
2019-03-20 03:07:02 +08:00
# USE LOCAL PTHREAD LIB AND LOCAL STB HEADER, NO VCPKG, ONLY RELEASE MODE SUPPORTED
2019-03-03 21:34:50 +08:00
# 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
New-Item -Path . \ build_win_release_novcpkg -ItemType directory -Force
Set-Location build_win_release_novcpkg
2019-07-18 19:14:09 +08:00
if ( $use_ninja ) {
2019-07-12 04:35:08 +08:00
cmake -G " $generator " $additional_build_setup . .
}
else {
cmake -G " $generator " -T " host=x64 " -A " x64 " $additional_build_setup . .
}
2019-03-29 00:54:27 +08:00
cmake - -build . - -config Release - -target install
#cmake --build . --config Release --parallel ${number_of_build_workers} --target install #valid only for CMake 3.12+
2019-03-04 22:15:19 +08:00
Remove-Item DarknetConfig . cmake
Remove-Item DarknetConfigVersion . cmake
2019-07-12 04:35:08 +08:00
$dllfolder = " ..\3rdparty\pthreads\bin "
$dllfiles = Get-ChildItem $ { dllfolder } \ * . dll
if ( $dllfiles ) {
Copy-Item $dllfiles . .
}
2019-03-03 21:34:50 +08:00
Set-Location . .
2019-03-29 00:54:27 +08:00
Copy-Item cmake \ Modules \ * . cmake share \ darknet \
2019-02-22 23:56:58 +08:00
}