diff --git a/windows/deploy_windows.ps1 b/windows/deploy_windows.ps1 index be67cab418..613a8c42cb 100644 --- a/windows/deploy_windows.ps1 +++ b/windows/deploy_windows.ps1 @@ -58,8 +58,12 @@ param ( # The following version pinnings are semi-automatically checked for # updates. Verify .github/workflows/bump-dependencies.yaml when changing those manually: [string] $AsioSDKUrl = "https://download.steinberg.net/sdk_downloads/ASIO-SDK_2.3.4_2025-10-15.zip", - [string] $NsisUrl = "https://downloads.sourceforge.net/project/nsis/NSIS%203/3.13/nsis-3.13.zip", - [string] $BuildOption = "" + [string] $NsisUrl = "https://downloads.sourceforge.net/project/nsis/NSIS%203/3.12/nsis-3.12.zip", + [string] $BuildOption = "", + # Toggles for debugging and targeted builds + [switch] $DebugMode, + [switch] $Skip64Bit, + [switch] $Skip32Bit ) # Fail early on all errors @@ -79,18 +83,74 @@ $DeployPath = "$RootPath\deploy" $WindowsPath ="$RootPath\windows" $AppName = "Jamulus" +Function Write-Log { + param( + [Parameter(Position=0)] + [ValidateSet("INFO","WARN","ERROR","STEP","DEBUG","NATIVE","NATIVE-ERR")] + [string] $Level = "INFO", + + [Parameter(Position=1, Mandatory=$true)] + [AllowNull()] + [AllowEmptyString()] + [string] $Message + ) + + if ($null -eq $Message) { $Message = "" } + $Stamp = (Get-Date).ToString("yyyy-MM-dd HH:mm:ss") + $Log = "[$Stamp] [$Level] $Message" + + switch ($Level) { + "INFO" { Write-Host $Log } + "STEP" { Write-Host "`n>>> [$Stamp] $Message" -ForegroundColor Cyan } + "WARN" { Write-Host $Log -ForegroundColor Yellow } + "ERROR" { Write-Host $Log -ForegroundColor Red } + "DEBUG" { if ($DebugMode) { Write-Host $Log -ForegroundColor DarkGray } } + "NATIVE" { Write-Host " $Message" -ForegroundColor DarkGray } + "NATIVE-ERR" { Write-Host " $Message" -ForegroundColor DarkYellow } + } +} + # Execute native command with errorlevel handling Function Invoke-Native-Command { param( [string] $Command, - [string[]] $Arguments + [string[]] $Arguments, + [switch] $SuppressStdErr ) - & "$Command" @Arguments + Write-Log "DEBUG" "Executing: $Command $($Arguments -join ' ')" + $Out = [Collections.Generic.List[string]]::new() - if ($LastExitCode -Ne 0) - { - Throw "Native command $Command returned with exit code $LastExitCode" + $PrevEA = $ErrorActionPreference + $ErrorActionPreference = "Continue" + + try { + & "$Command" @Arguments 2>&1 | ForEach-Object { + $IsErr = $_ -is [Management.Automation.ErrorRecord] + if ($IsErr -and $SuppressStdErr) { return } + + $Line = if ($IsErr) { $_.TargetObject -as [string] } else { $_.ToString() } + if ([string]::IsNullOrWhiteSpace($Line)) { return } + + $Out.Add($Line) + + if ($IsErr) { + Write-Log "NATIVE-ERR" $Line + } else { + Write-Log "NATIVE" $Line + } + } + } finally { + $ErrorActionPreference = $PrevEA + } + + if ($LastExitCode -Ne 0) { + $Err = "Native command $Command returned with exit code $LastExitCode" + Write-Log "ERROR" $Err + if (-not $DebugMode) { + $Out.ForEach({ Write-Log "ERROR" $_ }) + } + Throw $Err } } @@ -124,47 +184,18 @@ Function Get-RedirectedUrl { return } catch { if ($attempt -lt $numAttempts) { - Write-Warning "Caught error: $_" - Write-Warning "Get-RedirectedUrl: Fetch attempt #${attempt}/${numAttempts} for $url failed, trying again in ${sleepTime}s" + Write-Log "WARN" "Caught error: $_" + Write-Log "WARN" "Get-RedirectedUrl: Fetch attempt #${attempt}/${numAttempts} for $url failed, trying again in ${sleepTime}s" Start-Sleep -Seconds $sleepTime $sleepTime = [Math]::Min($sleepTime * 2, $maxSleepTime) continue } - Write-Error "Get-RedirectedUrl: All ${numAttempts} fetch attempts for $url failed, failing whole call" + Write-Log "ERROR" "Get-RedirectedUrl: All ${numAttempts} fetch attempts for $url failed, failing whole call" throw } } } -function Initialize-Module-Here ($m) { # see https://stackoverflow.com/a/51692402 - - # If module is imported say that and do nothing - if (Get-Module | Where-Object {$_.Name -eq $m}) { - Write-Output "Module $m is already imported." - } - else { - - # If module is not imported, but available on disk then import - if (Get-Module -ListAvailable | Where-Object {$_.Name -eq $m}) { - Import-Module $m - } - else { - - # If module is not imported, not available on disk, but is in online gallery then install and import - if (Find-Module -Name $m | Where-Object {$_.Name -eq $m}) { - Install-Module -Name $m -Force -Verbose -Scope CurrentUser - Import-Module $m - } - else { - - # If module is not imported, not available and not in online gallery then abort - Write-Output "Module $m not imported, not available and not in online gallery, exiting." - EXIT 1 - } - } - } -} - # Download and uncompress dependency in ZIP format Function Install-Dependency { @@ -177,7 +208,7 @@ Function Install-Dependency if (Test-Path -Path "$WindowsPath\$Destination") { - echo "Using ${WindowsPath}\${Destination} from previous run (e.g. actions/cache)" + Write-Log "INFO" "Using ${WindowsPath}\${Destination} from previous run (e.g. actions/cache)" return } @@ -193,10 +224,12 @@ Function Install-Dependency $Uri = Get-RedirectedUrl -URL $Uri } + Write-Log "INFO" "Downloading $Uri..." Invoke-WebRequest -Uri $Uri -OutFile $TempFileName - echo $TempFileName + Write-Log "DEBUG" "Saved to: $TempFileName" Expand-Archive -Path $TempFileName -DestinationPath $TempDir -Force - echo $WindowsPath\$Destination + Write-Log "DEBUG" "Extracting to: $WindowsPath\$Destination" + # Because we unpacked into a new directory, we can use * for the directory in the archive, # so that we do not need to know the directory name the archive was packed from. Move-Item -Path "$TempDir\*" -Destination "$WindowsPath\$Destination" -Force @@ -204,19 +237,18 @@ Function Install-Dependency Remove-Item -Path $TempFileName -Force } -# Install VSSetup (Visual Studio detection), ASIO SDK and NSIS Installer +# Install ASIO SDK and NSIS Installer Function Install-Dependencies { - if (-not (Get-PackageProvider -Name nuget).Name -eq "nuget") { - Install-PackageProvider -Name "Nuget" -Scope CurrentUser -Force - } - Initialize-Module-Here -m "VSSetup" + Write-Log "STEP" "Installing Dependencies..." Install-Dependency -Uri $NsisUrl -Destination "..\libs\NSIS\NSIS-source" if ($BuildOption -Notmatch "jack") { # Don't download ASIO SDK on Jamulus JACK builds to save # resources and to be extra-sure license-wise. Install-Dependency -Uri $AsioSDKUrl -Destination "..\libs\ASIOSDK2" + } else { + Write-Log "INFO" "Skipping ASIO SDK (JACK build detected)." } } @@ -229,11 +261,43 @@ Function Initialize-Build-Environment ) # Look for Visual Studio/Build Tools 2017 or later (version 15.0 or above) - $VsInstallPath = Get-VSSetupInstance | ` - Select-VSSetupInstance -Product "*" -Version "15.0" -Latest | ` - Select-Object -ExpandProperty "InstallationPath" + $ProgramFilesX86 = [Environment]::GetEnvironmentVariable("ProgramFiles(x86)") + $ProgramFiles = [Environment]::GetEnvironmentVariable("ProgramFiles") + $VsWhereCandidates = @( + $(if ($ProgramFilesX86) { Join-Path $ProgramFilesX86 "Microsoft Visual Studio\Installer\vswhere.exe" }), + $(if ($ProgramFiles) { Join-Path $ProgramFiles "Microsoft Visual Studio\Installer\vswhere.exe" }) + ) + $VsWherePath = $VsWhereCandidates | Where-Object { Test-Path -Path $_ } | Select-Object -First 1 + + if (-not $VsWherePath) + { + $VsWhereCommand = Get-Command "vswhere.exe" -ErrorAction SilentlyContinue + if ($VsWhereCommand) { $VsWherePath = $VsWhereCommand.Source } + } - if ($VsInstallPath -Eq "") { $VsInstallPath = "" } + if (-not $VsWherePath) + { + Throw "vswhere.exe could not be located. Please install Visual Studio 2017 or newer." + } + + $VsInstallPath = (& "$VsWherePath" -latest -products "*" ` + -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 ` + -property installationPath 2>$null | Select-Object -First 1) + + if ([string]::IsNullOrWhiteSpace($VsInstallPath)) + { + $VsInstallPath = (& "$VsWherePath" -latest -products "*" ` + -property installationPath 2>$null | Select-Object -First 1) + } + + if (-not [string]::IsNullOrWhiteSpace($VsInstallPath)) + { + $VsInstallPath = $VsInstallPath.Trim() + } + else + { + $VsInstallPath = "" + } if ($BuildArch -Eq "x86_64") { @@ -273,6 +337,39 @@ Function Initialize-Build-Environment Remove-Item -Path $EnvDump -Force } +# Resolve Qt path by falling back to Registry lookups if the default path is missing +Function Resolve-Qt-Path +{ + param( + [Parameter(Mandatory=$true)] + [string] $DefaultPath + ) + + if (Test-Path -Path $DefaultPath) { return $DefaultPath } + + Write-Log "DEBUG" "Qt path '$DefaultPath' not found. Searching registry..." + $QtVer = Split-Path $DefaultPath -Leaf + $RegPaths = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*", + "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*", + "HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*" + + foreach ($Reg in $RegPaths) + { + $Installs = Get-ItemProperty $Reg -ErrorAction SilentlyContinue | Where-Object { $_.DisplayName -match "Qt" -and $_.InstallLocation } + foreach ($Inst in $Installs) + { + $TryPath = Join-Path $Inst.InstallLocation $QtVer + if (Test-Path -Path $TryPath) + { + Write-Log "INFO" "Discovered Qt at: $TryPath" + return $TryPath + } + } + } + + return $DefaultPath +} + # Setup Qt environment variables and build tool paths Function Initialize-Qt-Build-Environment { @@ -289,6 +386,31 @@ Function Initialize-Qt-Build-Environment Set-Item Env:QtQmakePath "$QtMsvcSpecPath\qmake.exe" Set-Item Env:QtWinDeployPath "$QtMsvcSpecPath\windeployqt.exe" + # Check if jom.exe (Qt's clone of nmake that supports parallel execution) is + # available in the system PATH. This speeds up the build process significantly. + if (Get-Command "jom.exe" -ErrorAction SilentlyContinue) + { + Set-Item Env:QtJomPath "jom.exe" + } + else + { + $QtRoot = Split-Path $QtInstallPath -Parent + $JomExes = "$QtRoot\Tools\QtCreator\bin\jom\jom.exe", + "$QtRoot\Tools\jom\jom.exe", + "C:\Qt\Tools\QtCreator\bin\jom\jom.exe", + "D:\Qt\Tools\QtCreator\bin\jom\jom.exe" + + $FoundJom = $JomExes | Where-Object { Test-Path -Path $_ } | Select-Object -First 1 + if ($FoundJom) + { + Set-Item Env:QtJomPath $FoundJom + } + else + { + Set-Item Env:QtJomPath "" + } + } + "**********************************************************************" "Using Qt binaries for Visual C++ located at" $QtMsvcSpecPath @@ -313,45 +435,70 @@ Function Build-App [string] $BuildArch ) + $QmkCfg = "CONFIG+=$BuildConfig $BuildArch $BuildOption" + Invoke-Native-Command -Command "$Env:QtQmakePath" ` - -Arguments ("$RootPath\$AppName.pro", "CONFIG+=$BuildConfig $BuildArch $BuildOption", ` - "-o", "$BuildPath\Makefile") + -Arguments ("$RootPath\$AppName.pro", $QmkCfg, "-o", "$BuildPath\Makefile") Set-Location -Path $BuildPath - if (Get-Command "jom.exe" -ErrorAction SilentlyContinue) + + $MkArgs = @("/NOLOGO", $BuildConfig) + + if ($Env:QtJomPath) { - echo "Building with jom /J ${Env:NUMBER_OF_PROCESSORS}" - Invoke-Native-Command -Command "jom" -Arguments ("/J", "${Env:NUMBER_OF_PROCESSORS}", "$BuildConfig") + $Cores = [Math]::Max(1, [Math]::Floor([int]$Env:NUMBER_OF_PROCESSORS / 2)) + Write-Log "INFO" "Building with jom /J $Cores (half of $Env:NUMBER_OF_PROCESSORS cores)" + Invoke-Native-Command -Command "$Env:QtJomPath" -Arguments (@("/J", "$Cores") + $MkArgs) } else { - echo "Building with nmake (install Qt jom if you want parallel builds)" - Invoke-Native-Command -Command "nmake" -Arguments ("$BuildConfig") + Write-Log "INFO" "Building with nmake (install Qt jom if you want parallel builds)" + Invoke-Native-Command -Command "nmake" -Arguments $MkArgs } + Invoke-Native-Command -Command "$Env:QtWinDeployPath" ` -Arguments ("--$BuildConfig", "--compiler-runtime", "--dir=$DeployPath\$BuildArch", "$BuildPath\$BuildConfig\$AppName.exe") Move-Item -Path "$BuildPath\$BuildConfig\$AppName.exe" -Destination "$DeployPath\$BuildArch" -Force - Invoke-Native-Command -Command "nmake" -Arguments ("clean") + + $CleanArgs = @("clean", "/NOLOGO") + Invoke-Native-Command -Command "nmake" -Arguments $CleanArgs -SuppressStdErr + Set-Location -Path $RootPath } # Build and deploy Jamulus 64bit and 32bit variants function Build-App-Variants { + if ($Skip64Bit -and $Skip32Bit) + { + Throw "Both 64-bit and 32-bit builds are skipped; no application binaries will be produced." + } + foreach ($_ in ("x86_64", "x86")) { + if ($_ -eq "x86_64" -and $Skip64Bit) + { + Write-Log "INFO" "Skipping 64-bit build." + continue + } + if ($_ -eq "x86" -and $Skip32Bit) + { + Write-Log "INFO" "Skipping 32-bit build." + continue + } + $OriginalEnv = Get-ChildItem Env: if ($_ -eq "x86") { Initialize-Build-Environment -BuildArch $_ - Initialize-Qt-Build-Environment -QtInstallPath $QtInstallPath32 -QtCompile $QtCompile32 + Initialize-Qt-Build-Environment -QtInstallPath (Resolve-Qt-Path -DefaultPath $QtInstallPath32) -QtCompile $QtCompile32 } else { Initialize-Build-Environment -BuildArch $_ - Initialize-Qt-Build-Environment -QtInstallPath $QtInstallPath64 -QtCompile $QtCompile64 + Initialize-Qt-Build-Environment -QtInstallPath (Resolve-Qt-Path -DefaultPath $QtInstallPath64) -QtCompile $QtCompile64 } Build-App -BuildConfig "release" -BuildArch $_ $OriginalEnv | % { Set-Item "Env:$($_.Name)" $_.Value } @@ -365,6 +512,14 @@ Function Build-Installer [string] $BuildOption ) + # --- SAFETY GATE: Do not build if no executables were generated --- + $Has64 = Test-Path -Path "$DeployPath\x86_64\$AppName.exe" + $Has32 = Test-Path -Path "$DeployPath\x86\$AppName.exe" + + if (-not $Has64 -and -not $Has32) { + Throw "Installer build aborted: No application binaries found." + } + foreach ($_ in Get-Content -Path "$RootPath\$AppName.pro") { if ($_ -Match "^VERSION *= *(.*)$") @@ -374,19 +529,24 @@ Function Build-Installer } } + $NsisVerb = if ($DebugMode) { "/v4" } else { "/v2" } + $Has64Define = "/DHAS_X86_64=$([int]$Has64)" + $Has32Define = "/DHAS_X86=$([int]$Has32)" + if ($BuildOption -ne "") { Invoke-Native-Command -Command "$RootPath\libs\NSIS\NSIS-source\makensis" ` - -Arguments ("/v4", "/DAPP_NAME=$AppName", "/DAPP_VERSION=$AppVersion", ` + -Arguments ($NsisVerb, "/DAPP_NAME=$AppName", "/DAPP_VERSION=$AppVersion", ` "/DROOT_PATH=$RootPath", "/DWINDOWS_PATH=$WindowsPath", "/DDEPLOY_PATH=$DeployPath", ` - "/DBUILD_OPTION=$BuildOption", ` + "/DBUILD_OPTION=$BuildOption", $Has64Define, $Has32Define, ` "$WindowsPath\installer.nsi") } else { Invoke-Native-Command -Command "$RootPath\libs\NSIS\NSIS-source\makensis" ` - -Arguments ("/v4", "/DAPP_NAME=$AppName", "/DAPP_VERSION=$AppVersion", ` + -Arguments ($NsisVerb, "/DAPP_NAME=$AppName", "/DAPP_VERSION=$AppVersion", ` "/DROOT_PATH=$RootPath", "/DWINDOWS_PATH=$WindowsPath", "/DDEPLOY_PATH=$DeployPath", ` + $Has64Define, $Has32Define, ` "$WindowsPath\installer.nsi") } } @@ -396,7 +556,7 @@ Function Build-NSProcess { if (!(Test-Path -path "$RootPath\libs\NSIS\nsProcess.dll")) { - echo "Building nsProcess..." + Write-Log "INFO" "Building nsProcess..." $OriginalEnv = Get-ChildItem Env: Initialize-Build-Environment -BuildArch "x86" @@ -416,3 +576,4 @@ Install-Dependencies Build-App-Variants Build-NSProcess Build-Installer -BuildOption $BuildOption +Write-Log "STEP" "Build process completed successfully." diff --git a/windows/installer.nsi b/windows/installer.nsi index bd69b3ca7f..10be73f99b 100644 --- a/windows/installer.nsi +++ b/windows/installer.nsi @@ -56,6 +56,13 @@ ; Compile-time definitions +; CodeRabbit review: match install/uninstall payload and registry view to built architectures. +!ifndef HAS_X86_64 + !define HAS_X86_64 1 +!endif +!ifndef HAS_X86 + !define HAS_X86 1 +!endif !define VC_REDIST32_EXE "vc_redist.x86.exe" !define VC_REDIST64_EXE "vc_redist.x64.exe" !define APP_INSTALL_KEY "Software\${APP_NAME}" @@ -298,6 +305,7 @@ Var bRunApp !define UnSelectSection '!insertmacro SecUnSelect' +!if ${HAS_X86_64} == 1 Section "Install_64Bit" INST_64 ; check if old, wrongly installed Jamulus exists. See https://stackoverflow.com/questions/27839860/nsis-check-if-registry-key-value-exists#27841158 IfFileExists "$PROGRAMFILES32\Jamulus\Uninstall.exe" 0 continueinstall @@ -358,7 +366,9 @@ Section "Install_64Bit" INST_64 ExecWait "$\"$INSTDIR\${VC_REDIST64_EXE}$\" /q /norestart" Delete "$INSTDIR\${VC_REDIST64_EXE}" SectionEnd +!endif +!if ${HAS_X86} == 1 Section "Install_32Bit" INST_32 ; Install the main application @@ -370,6 +380,7 @@ Section "Install_32Bit" INST_32 Delete "$INSTDIR\${VC_REDIST32_EXE}" SectionEnd +!endif Function .onInit @@ -382,9 +393,24 @@ Function .onInit IfErrors 0 +2 StrCpy $INSTDIR "$PROGRAMFILES64\${APP_NAME}" - ; enable the 64 bit install section + ; Enable the matching payload; fall back to 32-bit only for a 32-bit payload in a 64-bit build. + !if ${HAS_X86_64} == 1 ${SelectSection} ${INST_64} + !if ${HAS_X86} == 1 ${UnSelectSection} ${INST_32} + !endif + !else + !if ${HAS_X86} == 1 + ${SelectSection} ${INST_32} + SetRegView 32 + ReadRegStr $INSTDIR HKLM "${APP_INSTALL_KEY}" "${APP_INSTALL_VALUE}" + IfErrors 0 +2 + StrCpy $INSTDIR "$PROGRAMFILES32\${APP_NAME}" + !else + MessageBox MB_OK|MB_ICONSTOP "This installer contains no supported application payload." + Abort + !endif + !endif ${Else} SetRegView 32 @@ -394,9 +420,15 @@ Function .onInit IfErrors 0 +2 StrCpy $INSTDIR "$PROGRAMFILES32\${APP_NAME}" - ; enable the 32 bit install section + !if ${HAS_X86} == 1 ${SelectSection} ${INST_32} + !if ${HAS_X86_64} == 1 ${UnSelectSection} ${INST_64} + !endif + !else + MessageBox MB_OK|MB_ICONSTOP "This installer does not contain a 32-bit application payload." + Abort + !endif ${EndIf} ; Install for all users @@ -584,9 +616,20 @@ FunctionEnd Section "un.Install" ; Delete the main application ${If} ${RunningX64} + !if ${HAS_X86_64} == 1 !insertmacro un.InstallFiles x86_64 + !else + !if ${HAS_X86} == 1 + !insertmacro un.InstallFiles x86 + !endif + !endif ${Else} + !if ${HAS_X86} == 1 !insertmacro un.InstallFiles x86 + !else + MessageBox MB_OK|MB_ICONSTOP "This uninstaller does not contain a 32-bit application payload." + Abort + !endif ${EndIf} ; Remove the Start Menu and desktop shortcuts @@ -607,9 +650,15 @@ SectionEnd Function un.onInit - ; Set up registry access for current architecture + ; Match the registry view to the payload selected by the installer. ${If} ${RunningX64} + !if ${HAS_X86_64} == 1 SetRegView 64 + !else + !if ${HAS_X86} == 1 + SetRegView 32 + !endif + !endif ${Else} SetRegView 32 ${EndIf}