From ffdd5e41a83e87dee978575a3435d3083fe53286 Mon Sep 17 00:00:00 2001 From: Stefano Sinigardi Date: Fri, 3 May 2024 15:35:17 +0200 Subject: [PATCH] build.ps1 is arm64 compatible (#8900) --- build.ps1 | 115 +++++++++++++++++++++++++++++++++++---------- scripts/utils.psm1 | 112 +++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 198 insertions(+), 29 deletions(-) diff --git a/build.ps1 b/build.ps1 index e378e4b4536..de8916d94a6 100755 --- a/build.ps1 +++ b/build.ps1 @@ -6,7 +6,7 @@ build Created By: Stefano Sinigardi Created Date: February 18, 2019 - Last Modified Date: September 25, 2023 + Last Modified Date: April 29, 2024 .DESCRIPTION Build tool using CMake, trying to properly setup the environment around compiler @@ -92,6 +92,12 @@ Force using a different buildtrees dir for vcpkg .PARAMETER ForceVCPKGPackagesRemoval Force clean up of vcpkg packages folder at the end of the script +.PARAMETER CloneVCPKGShallow +Clone vcpkg as shallow repository + +.PARAMETER ForceDisableVCPKGShallow +Force vcpkg clone to NOT be a shallow one + .PARAMETER ForceSetupVS Forces Visual Studio setup, also on systems on which it would not have been enabled automatically @@ -179,6 +185,8 @@ param ( [switch]$ForceVCPKGBuildtreesRemoval = $false, [string]$ForceVCPKGBuildtreesPath = "", [switch]$ForceVCPKGPackagesRemoval = $false, + [switch]$CloneVCPKGShallow = $false, + [switch]$ForceDisableVCPKGShallow = $false, [switch]$ForceSetupVS = $false, [switch]$ForceCMakeFromVS = $false, [switch]$ForceNinjaFromVS = $false, @@ -193,13 +201,33 @@ param ( $global:DisableInteractive = $DisableInteractive -$build_ps1_version = "3.6.1" +$build_ps1_version = "4.0.1" $script_name = $MyInvocation.MyCommand.Name $utils_psm1_avail = $false -if (Test-Path $PSScriptRoot/scripts/utils.psm1) { +if (Test-Path $PSScriptRoot/utils.psm1) { + Import-Module -Name $PSScriptRoot/utils.psm1 -Force + $utils_psm1_avail = $true +} +elseif (Test-Path $PSScriptRoot/cmake/utils.psm1) { + Import-Module -Name $PSScriptRoot/cmake/utils.psm1 -Force + $utils_psm1_avail = $true + $IsInGitSubmodule = $false +} +elseif (Test-Path $PSScriptRoot/ci/utils.psm1) { + Import-Module -Name $PSScriptRoot/ci/utils.psm1 -Force + $utils_psm1_avail = $true + $IsInGitSubmodule = $false +} +elseif (Test-Path $PSScriptRoot/ccm/utils.psm1) { + Import-Module -Name $PSScriptRoot/ccm/utils.psm1 -Force + $utils_psm1_avail = $true + $IsInGitSubmodule = $false +} +elseif (Test-Path $PSScriptRoot/scripts/utils.psm1) { Import-Module -Name $PSScriptRoot/scripts/utils.psm1 -Force $utils_psm1_avail = $true + $IsInGitSubmodule = $false } else { $utils_psm1_version = "unavail" @@ -306,6 +334,10 @@ if ($InstallDARKNETthroughVCPKG -and -not $EnableOPENCV) { if ($UseVCPKG) { Write-Host "vcpkg bootstrap script: bootstrap-vcpkg${bootstrap_ext}" + if(($ForceOpenCVVersion -eq 0) -and -Not $ForceDisableVCPKGShallow) { + Write-Host "vcpkg will be cloned in shallow mode since baseline is not needed" + $CloneVCPKGShallow = $true + } } if ((-Not $IsWindows) -and (-Not $IsWindowsPowerShell) -and (-Not $ForceSetupVS)) { @@ -323,6 +355,31 @@ if (($IsLinux -or $IsMacOS) -and ($ForceGCCVersion -gt 0)) { $env:CXX = "g++-$ForceGCCVersion" } +$osArchitecture = [System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture +switch ($osArchitecture) { + "X86" { + $vcpkgArchitecture = "x86" + $vsArchitecture = "Win32" + } + "X64" { + $vcpkgArchitecture = "x64" + $vsArchitecture = "x64" + } + "Arm" { + $vcpkgArchitecture = "arm" + $vsArchitecture = "arm" + } + "Arm64" { + $vcpkgArchitecture = "arm64" + $vsArchitecture = "arm64" + } + default { + $vcpkgArchitecture = "x64" + $vsArchitecture = "x64" + Write-Output "Unknown architecture. Trying x64" + } +} + $vcpkg_triplet_set_by_this_script = $false $vcpkg_host_triplet_set_by_this_script = $false @@ -336,48 +393,48 @@ if (($IsWindows -or $IsWindowsPowerShell) -and (-Not $env:VCPKG_DEFAULT_TRIPLET) $DoNotUseNinja = $true Write-Host "Warning: when building for 32bit windows target, only msbuild can be used and ninja will be disabled. Doing that for you!" -ForegroundColor Yellow } - $env:VCPKG_DEFAULT_TRIPLET = "x86-windows" + $env:VCPKG_DEFAULT_TRIPLET = "${vcpkgArchitecture}-windows" $vcpkg_triplet_set_by_this_script = $true } else { if($BuildDebug) { - $env:VCPKG_DEFAULT_TRIPLET = "x64-windows" + $env:VCPKG_DEFAULT_TRIPLET = "${vcpkgArchitecture}-windows" $vcpkg_triplet_set_by_this_script = $true } else { - $env:VCPKG_DEFAULT_TRIPLET = "x64-windows-release" + $env:VCPKG_DEFAULT_TRIPLET = "${vcpkgArchitecture}-windows-release" $vcpkg_triplet_set_by_this_script = $true } } } if (($IsWindows -or $IsWindowsPowerShell) -and (-Not $env:VCPKG_DEFAULT_HOST_TRIPLET)) { if ($BuildDebug) { - $env:VCPKG_DEFAULT_HOST_TRIPLET = "x64-windows" + $env:VCPKG_DEFAULT_HOST_TRIPLET = "${vcpkgArchitecture}-windows" $vcpkg_host_triplet_set_by_this_script = $true } else { - $env:VCPKG_DEFAULT_HOST_TRIPLET = "x64-windows-release" + $env:VCPKG_DEFAULT_HOST_TRIPLET = "${vcpkgArchitecture}-windows-release" $vcpkg_host_triplet_set_by_this_script = $true } } if ($IsMacOS -and (-Not $env:VCPKG_DEFAULT_TRIPLET)) { if ($BuildDebug) { - $env:VCPKG_DEFAULT_TRIPLET = "x64-osx" + $env:VCPKG_DEFAULT_TRIPLET = "${vcpkgArchitecture}-osx" $vcpkg_triplet_set_by_this_script = $true } else { - $env:VCPKG_DEFAULT_TRIPLET = "x64-osx-release" + $env:VCPKG_DEFAULT_TRIPLET = "${vcpkgArchitecture}-osx-release" $vcpkg_triplet_set_by_this_script = $true } } if ($IsMacOS -and (-Not $env:VCPKG_DEFAULT_HOST_TRIPLET)) { if ($BuildDebug) { - $env:VCPKG_DEFAULT_HOST_TRIPLET = "x64-osx" + $env:VCPKG_DEFAULT_HOST_TRIPLET = "${vcpkgArchitecture}-osx" $vcpkg_host_triplet_set_by_this_script = $true } else { - $env:VCPKG_DEFAULT_HOST_TRIPLET = "x64-osx-release" + $env:VCPKG_DEFAULT_HOST_TRIPLET = "${vcpkgArchitecture}-osx-release" $vcpkg_host_triplet_set_by_this_script = $true } } @@ -385,22 +442,22 @@ if ($IsMacOS -and (-Not $env:VCPKG_DEFAULT_HOST_TRIPLET)) { if ($IsLinux -and (-Not $env:VCPKG_DEFAULT_TRIPLET)) { if ($true) { if ($BuildDebug) { - $env:VCPKG_DEFAULT_TRIPLET = "x64-linux" + $env:VCPKG_DEFAULT_TRIPLET = "${vcpkgArchitecture}-linux" $vcpkg_triplet_set_by_this_script = $true } else { - $env:VCPKG_DEFAULT_TRIPLET = "x64-linux-release" + $env:VCPKG_DEFAULT_TRIPLET = "${vcpkgArchitecture}-linux-release" $vcpkg_triplet_set_by_this_script = $true } } } if ($IsLinux -and (-Not $env:VCPKG_DEFAULT_HOST_TRIPLET)) { if ($BuildDebug) { - $env:VCPKG_DEFAULT_HOST_TRIPLET = "x64-linux" + $env:VCPKG_DEFAULT_HOST_TRIPLET = "${vcpkgArchitecture}-linux" $vcpkg_host_triplet_set_by_this_script = $true } else { - $env:VCPKG_DEFAULT_HOST_TRIPLET = "x64-linux-release" + $env:VCPKG_DEFAULT_HOST_TRIPLET = "${vcpkgArchitecture}-linux-release" $vcpkg_host_triplet_set_by_this_script = $true } } @@ -630,11 +687,11 @@ if (-Not $DoNotUseNinja) { if (-Not $DoNotSetupVS) { $CL_EXE = Get-Command "cl" -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Definition - if ((-Not $CL_EXE) -or ($CL_EXE -match "HostX86\\x86") -or ($CL_EXE -match "HostX64\\x86")) { + if (-Not $CL_EXE) { $vsfound = getLatestVisualStudioWithDesktopWorkloadPath Write-Host "Found VS in ${vsfound}" Push-Location "${vsfound}/Common7/Tools" - cmd.exe /c "VsDevCmd.bat -arch=x64 & set" | + cmd.exe /c "VsDevCmd.bat -arch=${vsArchitecture} & set" | ForEach-Object { if ($_ -match "=") { $v = $_.split("="); Set-Item -force -path "ENV:\$($v[0])" -value "$($v[1])" @@ -650,26 +707,26 @@ if (-Not $DoNotSetupVS) { $debugConfig = " --config Debug " $releaseConfig = " --config Release " if ($Use32bitTriplet) { - $targetArchitecture = "`"Win32`"" + $targetArchitecture = "`"${vsArchitecture}`"" } else { - $targetArchitecture = "`"x64`"" + $targetArchitecture = "`"${vsArchitecture}`"" } if ($tokens[0] -eq "14") { $generator = "Visual Studio 14 2015" - $AdditionalBuildSetup = $AdditionalBuildSetup + " -T `"host=x64`" -A $targetArchitecture" + $AdditionalBuildSetup = $AdditionalBuildSetup + " -A $targetArchitecture" } elseif ($tokens[0] -eq "15") { $generator = "Visual Studio 15 2017" - $AdditionalBuildSetup = $AdditionalBuildSetup + " -T `"host=x64`" -A $targetArchitecture" + $AdditionalBuildSetup = $AdditionalBuildSetup + " -A $targetArchitecture" } elseif ($tokens[0] -eq "16") { $generator = "Visual Studio 16 2019" - $AdditionalBuildSetup = $AdditionalBuildSetup + " -T `"host=x64`" -A $targetArchitecture" + $AdditionalBuildSetup = $AdditionalBuildSetup + " -A $targetArchitecture" } elseif ($tokens[0] -eq "17") { $generator = "Visual Studio 17 2022" - $AdditionalBuildSetup = $AdditionalBuildSetup + " -T `"host=x64`" -A $targetArchitecture" + $AdditionalBuildSetup = $AdditionalBuildSetup + " -A $targetArchitecture" } else { MyThrow("Unknown Visual Studio version, unsupported configuration") @@ -739,7 +796,7 @@ if ($UseVCPKG -And -Not $ForceLocalVCPKG) { if (($null -eq $vcpkg_path) -and $UseVCPKG) { if (-Not (Test-Path "$PWD/vcpkg${VCPKGSuffix}")) { $shallow_copy = "" - if(($ForceOpenCVVersion -eq 0)) { + if($CloneVCPKGShallow) { $shallow_copy = " --depth 1 " } $proc = Start-Process -NoNewWindow -PassThru -FilePath $GIT_EXE -ArgumentList "clone $shallow_copy https://github.com/microsoft/vcpkg vcpkg${VCPKGSuffix}" @@ -999,6 +1056,9 @@ else { Remove-Item -Force -Recurse -ErrorAction SilentlyContinue $debug_build_folder } + if (-Not (Test-Path $DebugInstallPrefix)) { + New-Item -Path $DebugInstallPrefix -ItemType directory -Force | Out-Null + } New-Item -Path $debug_build_folder -ItemType directory -Force | Out-Null Set-Location $debug_build_folder $cmake_args = "-G `"$generator`" ${DebugBuildSetup} ${AdditionalBuildSetup} -S .." @@ -1031,6 +1091,9 @@ else { Remove-Item -Force -Recurse -ErrorAction SilentlyContinue $release_build_folder } + if (-Not (Test-Path $ReleaseInstallPrefix)) { + New-Item -Path $ReleaseInstallPrefix -ItemType directory -Force | Out-Null + } New-Item -Path $release_build_folder -ItemType directory -Force | Out-Null Set-Location $release_build_folder $cmake_args = "-G `"$generator`" ${ReleaseBuildSetup} ${AdditionalBuildSetup} -S .." @@ -1056,7 +1119,7 @@ else { if (-Not ($exitCode -eq 0)) { MyThrow("Build failed! Exited with error code $exitCode.") } - if (-Not $UseVCPKG -And -Not $DisableDLLcopy) { + if ($IsWindows -And -Not $UseVCPKG -And -Not $DisableDLLcopy) { $dllfiles = Get-ChildItem ./${dllfolder}/*.dll if ($dllfiles) { Copy-Item $dllfiles .. diff --git a/scripts/utils.psm1 b/scripts/utils.psm1 index 928d9339ae8..6e22056957c 100644 --- a/scripts/utils.psm1 +++ b/scripts/utils.psm1 @@ -22,7 +22,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #> -$utils_psm1_version = "0.3.0" +$utils_psm1_version = "1.2.3" $IsWindowsPowerShell = switch ( $PSVersionTable.PSVersion.Major ) { 5 { $true } 4 { $true } @@ -167,6 +167,24 @@ function getLatestVisualStudioWithDesktopWorkloadVersion([bool]$required = $true return $installationVersion } +function setupVisualStudio([bool]$required = $true) { + $CL_EXE = Get-Command "cl" -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Definition + if ((-Not $CL_EXE) -or ($CL_EXE -match "HostX86\\x86") -or ($CL_EXE -match "HostX64\\x86")) { + $vsfound = getLatestVisualStudioWithDesktopWorkloadPath + Write-Host "Found VS in ${vsfound}" + Push-Location "${vsfound}/Common7/Tools" + cmd.exe /c "VsDevCmd.bat -arch=x64 & set" | + ForEach-Object { + if ($_ -match "=") { + $v = $_.split("="); Set-Item -force -path "ENV:\$($v[0])" -value "$($v[1])" + } + } + Pop-Location + $env:PATH = "${vsfound}/VC/Tools/Llvm/x64/bin;$env:PATH" + Write-Host "Visual Studio Command Prompt variables set" + } +} + function DownloadNinja() { Write-Host "Downloading a portable version of Ninja" -ForegroundColor Yellow Remove-Item -Force -Recurse -ErrorAction SilentlyContinue ninja @@ -192,12 +210,12 @@ function DownloadNinja() { function DownloadAria2() { Write-Host "Downloading a portable version of Aria2" -ForegroundColor Yellow if ($IsWindows -or $IsWindowsPowerShell) { - $basename = "aria2-1.35.0-win-32bit-build1" + $basename = "aria2-1.37.0-win-32bit-build1" $zipName = "${basename}.zip" $outFolder = "$basename/$basename" Remove-Item -Force -Recurse -ErrorAction SilentlyContinue $outFolder Remove-Item -Force -ErrorAction SilentlyContinue $zipName - $url = "https://github.com/aria2/aria2/releases/download/release-1.35.0/$zipName" + $url = "https://github.com/aria2/aria2/releases/download/release-1.37.0/$zipName" Invoke-RestMethod -Uri $url -Method Get -ContentType application/zip -OutFile $zipName Expand-Archive -Path $zipName } @@ -228,6 +246,29 @@ function DownloadAria2() { return "./$outFolder/aria2c${ExecutableSuffix}" } +function DownloadLicencpp() { + $licencpp_version = "0.2.1" + Write-Host "Downloading a portable version of licencpp v${licencpp_version}" -ForegroundColor Yellow + if ($IsWindows -or $IsWindowsPowerShell) { + $basename = "licencpp-Windows" + } + elseif ($IsLinux) { + $basename = "licencpp-Linux" + } + else { + MyThrow("Unknown OS, unsupported") + } + $zipName = "${basename}.zip" + $outFolder = "${basename}" + Remove-Item -Force -Recurse -ErrorAction SilentlyContinue $outFolder + Remove-Item -Force -ErrorAction SilentlyContinue $zipName + $url = "https://github.com/cenit/licencpp/releases/download/v${licencpp_version}/$zipName" + Invoke-RestMethod -Uri $url -Method Get -ContentType application/zip -OutFile $zipName + Expand-Archive -Path $zipName + Remove-Item -Force -ErrorAction SilentlyContinue $zipName + return "./$outFolder/licencpp${ExecutableSuffix}" +} + function Download7Zip() { Write-Host "Downloading a portable version of 7-Zip" -ForegroundColor Yellow if ($IsWindows -or $IsWindowsPowerShell) { @@ -324,6 +365,65 @@ Function MyThrow ($Message) { } } +Function CopyTexFile ($MyFile) { + $MyFileName = Split-Path $MyFile -Leaf + New-Item -ItemType Directory -Force -Path "~/${latex_path}" | Out-Null + if (-Not (Test-Path "~/${latex_path}/$MyFileName" )) { + Write-Host "Copying $MyFile to ~/${latex_path}" + Copy-Item "$MyFile" "~/${latex_path}" + } + else { + Write-Host "~/${latex_path}/$MyFileName already present" + } +} + +Function dos2unix { + Param ( + [Parameter(mandatory = $true)] + [string[]]$path + ) + + Get-ChildItem -File -Recurse -Path $path | + ForEach-Object { + Write-Host "Converting $_" + $x = get-content -raw -path $_.fullname; $x -replace "`r`n", "`n" | Set-Content -NoNewline -Force -path $_.fullname + } +} + +Function unix2dos { + Param ( + [Parameter(mandatory = $true)] + [string[]]$path + ) + + Get-ChildItem -File -Recurse -Path $path | + ForEach-Object { + $x = get-content -raw -path $_.fullname + $SearchStr = [regex]::Escape("`r`n") + $SEL = Select-String -InputObject $x -Pattern $SearchStr + if ($null -ne $SEL) { + Write-Host "Converting $_" + # do nothing: avoid creating files containing `r`r`n when using unix2dos twice on the same file + } + else { + Write-Host "Converting $_" + $x -replace "`n", "`r`n" | Set-Content -NoNewline -Force -path $_.fullname + } + } +} + +Function UpdateRepo { + if ($GIT_EXE) { + Get-ChildItem -Directory | + ForEach-Object { + Set-Location $_.Name + git pull + git submodule update --recursive + Set-Location .. + } + } +} + Export-ModuleMember -Variable utils_psm1_version Export-ModuleMember -Variable IsWindowsPowerShell Export-ModuleMember -Variable IsInGitSubmodule @@ -336,7 +436,13 @@ Export-ModuleMember -Variable cuda_version_short_dashed Export-ModuleMember -Function getProgramFiles32bit Export-ModuleMember -Function getLatestVisualStudioWithDesktopWorkloadPath Export-ModuleMember -Function getLatestVisualStudioWithDesktopWorkloadVersion +Export-ModuleMember -Function setupVisualStudio Export-ModuleMember -Function DownloadNinja Export-ModuleMember -Function DownloadAria2 Export-ModuleMember -Function Download7Zip +Export-ModuleMember -Function DownloadLicencpp Export-ModuleMember -Function MyThrow +Export-ModuleMember -Function CopyTexFile +Export-ModuleMember -Function dos2unix +Export-ModuleMember -Function unix2dos +Export-ModuleMember -Function UpdateRepo