build-win-installer.ps1 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #-----------------------------------------------------------------------------
  2. #
  3. # SRT - Secure, Reliable, Transport
  4. # Copyright (c) 2021, Thierry Lelegard
  5. #
  6. # This Source Code Form is subject to the terms of the Mozilla Public
  7. # License, v. 2.0. If a copy of the MPL was not distributed with this
  8. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. #
  10. #-----------------------------------------------------------------------------
  11. <#
  12. .SYNOPSIS
  13. Build the SRT static libraries installer for Windows.
  14. .PARAMETER Version
  15. Use the specified string as version number from libsrt. By default, if
  16. the current commit has a tag, use that tag (initial 'v' removed). Otherwise,
  17. the defaut version is a detailed version number (most recent version, number
  18. of commits since then, short commit SHA).
  19. .PARAMETER NoPause
  20. Do not wait for the user to press <enter> at end of execution. By default,
  21. execute a "pause" instruction at the end of execution, which is useful
  22. when the script was run from Windows Explorer.
  23. #>
  24. [CmdletBinding()]
  25. param(
  26. [string]$Version = "",
  27. [switch]$NoPause = $false
  28. )
  29. Write-Output "Building the SRT static libraries installer for Windows"
  30. # Directory containing this script:
  31. $ScriptDir = $PSScriptRoot
  32. # The root of the srt repository is two levels up.
  33. $RepoDir = (Split-Path -Parent (Split-Path -Parent $ScriptDir))
  34. # Output directory for final installers:
  35. $OutDir = "$ScriptDir\installers"
  36. # Temporary directory for build operations:
  37. $TmpDir = "$ScriptDir\tmp"
  38. #-----------------------------------------------------------------------------
  39. # A function to exit this script with optional error message, using -NoPause
  40. #-----------------------------------------------------------------------------
  41. function Exit-Script([string]$Message = "")
  42. {
  43. $Code = 0
  44. if ($Message -ne "") {
  45. Write-Output "ERROR: $Message"
  46. $Code = 1
  47. }
  48. if (-not $NoPause) {
  49. pause
  50. }
  51. exit $Code
  52. }
  53. #-----------------------------------------------------------------------------
  54. # Build SRT version strings
  55. #-----------------------------------------------------------------------------
  56. # By default, let git format a decent version number.
  57. if (-not $Version) {
  58. $Version = (git describe --tags ) -replace '-g','-'
  59. }
  60. $Version = $Version -replace '^v',''
  61. # Split version string in pieces and make sure to get at least four elements.
  62. $VField = ($Version -split "[-\. ]") + @("0", "0", "0", "0") | Select-String -Pattern '^\d*$'
  63. $VersionInfo = "$($VField[0]).$($VField[1]).$($VField[2]).$($VField[3])"
  64. Write-Output "SRT version: $Version"
  65. Write-Output "Windows version info: $VersionInfo"
  66. #-----------------------------------------------------------------------------
  67. # Initialization phase, verify prerequisites
  68. #-----------------------------------------------------------------------------
  69. # Locate OpenSSL root from local installation.
  70. $SslRoot = @{
  71. "x64" = "C:\Program Files\OpenSSL-Win64";
  72. "Win32" = "C:\Program Files (x86)\OpenSSL-Win32"
  73. }
  74. # Verify OpenSSL directories.
  75. $Missing = 0
  76. foreach ($file in @($SslRoot["x64"], $SslRoot["Win32"])) {
  77. if (-not (Test-Path $file)) {
  78. Write-Output "**** Missing $file"
  79. $Missing = $Missing + 1
  80. }
  81. }
  82. if ($Missing -gt 0) {
  83. Exit-Script "Missing $Missing OpenSSL files, use install-openssl.ps1 to install OpenSSL"
  84. }
  85. # Locate MSBuild and CMake, regardless of Visual Studio version.
  86. Write-Output "Searching MSBuild ..."
  87. $MSRoots = @("C:\Program Files*\MSBuild", "C:\Program Files*\Microsoft Visual Studio", "C:\Program Files*\CMake*")
  88. $MSBuild = Get-ChildItem -Recurse -Path $MSRoots -Include MSBuild.exe -ErrorAction Ignore |
  89. ForEach-Object { (Get-Command $_).FileVersionInfo } |
  90. Sort-Object -Unique -Property FileVersion |
  91. ForEach-Object { $_.FileName} |
  92. Select-Object -Last 1
  93. if (-not $MSBuild) {
  94. Exit-Script "MSBuild not found"
  95. }
  96. Write-Output "Searching CMake ..."
  97. $CMake = Get-ChildItem -Recurse -Path $MSRoots -Include cmake.exe -ErrorAction Ignore |
  98. ForEach-Object { (Get-Command $_).FileVersionInfo } |
  99. Sort-Object -Unique -Property FileVersion |
  100. ForEach-Object { $_.FileName} |
  101. Select-Object -Last 1
  102. if (-not $CMake) {
  103. Exit-Script "CMake not found, check option 'C++ CMake tools for Windows' in Visual Studio installer"
  104. }
  105. # Locate NSIS, the Nullsoft Scriptable Installation System.
  106. Write-Output "Searching NSIS ..."
  107. $NSIS = Get-Item "C:\Program Files*\NSIS\makensis.exe" | ForEach-Object { $_.FullName} | Select-Object -Last 1
  108. if (-not $NSIS) {
  109. Exit-Script "NSIS not found, use install-nsis.ps1 to install NSIS"
  110. }
  111. Write-Output "MSBuild: $MSBuild"
  112. Write-Output "CMake: $CMake"
  113. Write-Output "NSIS: $NSIS"
  114. # Create the directories for builds when necessary.
  115. [void](New-Item -Path $TmpDir -ItemType Directory -Force)
  116. [void](New-Item -Path $OutDir -ItemType Directory -Force)
  117. #-----------------------------------------------------------------------------
  118. # Configure and build SRT library using CMake on two architectures.
  119. #-----------------------------------------------------------------------------
  120. foreach ($Platform in @("x64", "Win32")) {
  121. # Build directory. Cleanup to force a fresh cmake config.
  122. $BuildDir = "$TmpDir\build.$Platform"
  123. Remove-Item -Recurse -Force -ErrorAction SilentlyContinue $BuildDir
  124. [void](New-Item -Path $BuildDir -ItemType Directory -Force)
  125. # Run CMake.
  126. Write-Output "Configuring build for platform $Platform ..."
  127. $SRoot = $SslRoot[$Platform]
  128. & $CMake -S $RepoDir -B $BuildDir -A $Platform `
  129. -DENABLE_STDCXX_SYNC=ON `
  130. -DOPENSSL_ROOT_DIR="$SRoot" `
  131. -DOPENSSL_LIBRARIES="$SRoot\lib\libssl_static.lib;$SRoot\lib\libcrypto_static.lib" `
  132. -DOPENSSL_INCLUDE_DIR="$SRoot\include"
  133. # Patch version string in version.h
  134. Get-Content "$BuildDir\version.h" |
  135. ForEach-Object {
  136. $_ -replace "#define *SRT_VERSION_STRING .*","#define SRT_VERSION_STRING `"$Version`""
  137. } |
  138. Out-File "$BuildDir\version.new" -Encoding ascii
  139. Move-Item "$BuildDir\version.new" "$BuildDir\version.h" -Force
  140. # Compile SRT.
  141. Write-Output "Building for platform $Platform ..."
  142. foreach ($Conf in @("Release", "Debug")) {
  143. & $MSBuild "$BuildDir\SRT.sln" /nologo /maxcpucount /property:Configuration=$Conf /property:Platform=$Platform /target:srt_static
  144. }
  145. }
  146. # Verify the presence of compiled libraries.
  147. Write-Output "Checking compiled libraries ..."
  148. $Missing = 0
  149. foreach ($Conf in @("Release", "Debug")) {
  150. foreach ($Platform in @("x64", "Win32")) {
  151. $Path = "$TmpDir\build.$Platform\$Conf\srt_static.lib"
  152. if (-not (Test-Path $Path)) {
  153. Write-Output "**** Missing $Path"
  154. $Missing = $Missing + 1
  155. }
  156. }
  157. }
  158. if ($Missing -gt 0) {
  159. Exit-Script "Missing $Missing files"
  160. }
  161. #-----------------------------------------------------------------------------
  162. # Build the binary installer.
  163. #-----------------------------------------------------------------------------
  164. $InstallExe = "$OutDir\libsrt-$Version.exe"
  165. $InstallZip = "$OutDir\libsrt-$Version-win-installer.zip"
  166. Write-Output "Building installer ..."
  167. & $NSIS /V2 `
  168. /DVersion="$Version" `
  169. /DVersionInfo="$VersionInfo" `
  170. /DOutDir="$OutDir" `
  171. /DBuildRoot="$TmpDir" `
  172. /DRepoDir="$RepoDir" `
  173. "$ScriptDir\libsrt.nsi"
  174. if (-not (Test-Path $InstallExe)) {
  175. Exit-Script "**** Missing $InstallExe"
  176. }
  177. Write-Output "Building installer archive ..."
  178. Remove-Item -Force -ErrorAction SilentlyContinue $InstallZip
  179. Compress-Archive -Path $InstallExe -DestinationPath $InstallZip -CompressionLevel Optimal
  180. if (-not (Test-Path $InstallZip)) {
  181. Exit-Script "**** Missing $InstallZip"
  182. }
  183. Exit-Script