Guest User

Compile Qt with static OpenSSL

a guest
Oct 2nd, 2019
350
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #-----------------------------------------------------------------------------
  2. #
  3. #  Copyright (c) 2013, Thierry Lelegard
  4. #  All rights reserved.
  5. #
  6. #  Redistribution and use in source and binary forms, with or without
  7. #  modification, are permitted provided that the following conditions are met:
  8. #
  9. #  1. Redistributions of source code must retain the above copyright notice,
  10. #     this list of conditions and the following disclaimer.
  11. #  2. Redistributions in binary form must reproduce the above copyright
  12. #     notice, this list of conditions and the following disclaimer in the
  13. #     documentation and/or other materials provided with the distribution.
  14. #
  15. #  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. #  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. #  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. #  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  19. #  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. #  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. #  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. #  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. #  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. #  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  25. #  THE POSSIBILITY OF SUCH DAMAGE.
  26. #
  27. #-----------------------------------------------------------------------------
  28.  
  29. <#
  30.  .SYNOPSIS
  31.  
  32.   Build a static version of Qt for Windows.
  33.  
  34.  .DESCRIPTION
  35.  
  36.   This scripts downloads Qt source code, compiles and installs a static version
  37.   of Qt. It assumes that a prebuilt Qt / MinGW environment is already installed,
  38.   typically in C:\Qt. This prebuilt environment uses shared libraries. It is
  39.   supposed to remain the main development environment for Qt. This script adds
  40.   a static version of the Qt libraries in order to allow the construction of
  41.   standalone and self-sufficient executable.
  42.  
  43.   This script is typically run from the Windows Explorer.
  44.  
  45.   Requirements:
  46.   - Windows PowerShell 3.0 or higher.
  47.   - 7-zip.
  48.  
  49.  .PARAMETER QtSrcUrl
  50.  
  51.   URL of the Qt source file archive.
  52.   By default, use the latest identified version.
  53.  
  54.  .PARAMETER QtStaticDir
  55.  
  56.   Root directory where the static versions of Qt are installed.
  57.   By default, use C:\Qt\Static.
  58.  
  59.  .PARAMETER QtVersion
  60.  
  61.   The Qt version. By default, this script tries to extract the version number
  62.   from the Qt source file name.
  63.  
  64.  .PARAMETER MingwDir
  65.  
  66.   Root directory of the MinGW prebuilt environment. By default, use the version
  67.   which was installed by the prebuilt Qt environment.
  68.  
  69.  .PARAMETER NoPause
  70.  
  71.   Do not wait for the user to press <enter> at end of execution. By default,
  72.   execute a "pause" instruction at the end of execution, which is useful
  73.   when the script was run from Windows Explorer.
  74. #>
  75.  
  76. [CmdletBinding()]
  77. param(
  78.     $QtSrcUrl = "F:\Qt\Static-OpenSSL-Linked\src\qt-everywhere-src-5.13.1.tar",
  79.     $QtStaticDir = "F:\Qt\Static-OpenSSL-Linked",
  80.     $QtVersion = "",
  81.     $arch = "32",
  82.     $MingwDir = "",
  83.     $threads = "16",
  84.     $OPENSSL_HOME = "C:\OpenSSL\OpenSSL-Win$($arch)",
  85.  
  86.     [switch]$NoPause = $false
  87. )
  88.  
  89. # PowerShell execution policy.
  90. Set-StrictMode -Version 3
  91.  
  92. #-----------------------------------------------------------------------------
  93. # Main code
  94. #-----------------------------------------------------------------------------
  95.  
  96. function Main
  97. {
  98.     # Check that 7zip is installed. We use it to expand the downloaded archive.
  99.     [void] (Get-7zip)
  100.  
  101.     # Get Qt source file name from URL.
  102.     $QtSrcFileName = Split-Path -Leaf $QtSrcUrl
  103.  
  104.     # If Qt version is not specified on the command line, try to extract the value.
  105.     if (-not $QtVersion) {
  106.         $QtVersion = $QtSrcFileName -replace "`.[^`.]*$",''
  107.         $QtVersion = $QtVersion -replace 'qt-',''
  108.         $QtVersion = $QtVersion -replace 'everywhere-',''
  109.         $QtVersion = $QtVersion -replace 'opensource-',''
  110.         $QtVersion = $QtVersion -replace 'src-',''
  111.         $QtVersion = $QtVersion -replace '-src',''
  112.     }
  113.     Write-Output "Building static Qt version $QtVersion"
  114.  
  115.     # Qt installation directory.
  116.     $QtDir = "$QtStaticDir\$($QtVersion)_x$($arch)"
  117.  
  118.     # Get MinGW root directory, if not specified on the command line.
  119.     if (-not $MingwDir) {
  120.         # Search all instances of gcc.exe from C:\Qt prebuilt environment.
  121.         $GccList = @(Get-ChildItem -Path F:\Qt\*\Tools\mingw*\bin\gcc.exe | ForEach-Object FullName | Sort-Object)
  122.         if ($GccList.Length -eq 0) {
  123.             Exit-Script "MinGW environment not found, no Qt prebuilt version?"
  124.         }
  125.         $MingwDir = (Split-Path -Parent (Split-Path -Parent $GccList[$GccList.Length - 1]))
  126.         $MingwDir = $MingwDir.Substring(0, $MingwDir.Length - 2)
  127.         $MingwDir += $arch
  128.     }
  129.     Write-Output "Using MinGW from $MingwDir"
  130.  
  131.     # Build the directory tree where the static version of Qt will be installed.
  132.     Create-Directory $QtStaticDir\src
  133.     Create-Directory $QtDir
  134.  
  135.     # Download the Qt source package if not yet done.
  136.     Download-File $QtSrcUrl $QtStaticDir\src\$QtSrcFileName
  137.  
  138.     # Directory of expanded packages.
  139.     $QtSrcDir = "$QtStaticDir\src\$((Get-Item $QtStaticDir\src\$QtSrcFileName).BaseName)"
  140.  
  141.     # Expand archives if not yet done
  142.     Expand-Archive $QtStaticDir\src\$QtSrcFileName $QtStaticDir\src $QtSrcDir
  143.  
  144.     # Patch Qt's mkspecs for static build.
  145.     $File = "$QtSrcDir\qtbase\mkspecs\win32-g++\qmake.conf"
  146.     if (-not (Select-String -Quiet -SimpleMatch -CaseSensitive "# [QT-STATIC-PATCH]" $File)) {
  147.         Write-Output "Patching $File ..."
  148.         Copy-Item $File "$File.orig"
  149.         @"
  150.  
  151. # [QT-STATIC-PATCH]
  152. QMAKE_LFLAGS += -static -static-libgcc
  153. QMAKE_CFLAGS_RELEASE -= -O2
  154. QMAKE_CFLAGS_RELEASE += -Os -momit-leaf-frame-pointer
  155. DEFINES += QT_STATIC_BUILD
  156. "@ | Out-File -Append $File -Encoding Ascii
  157.     }
  158.  
  159.     # Set a clean path including MinGW.
  160.     $env:Path = "$MingwDir\bin;$MingwDir\opt\bin;$env:SystemRoot\system32;$env:SystemRoot"
  161.  
  162.     # Force English locale to avoid weird effects of tools localization.
  163.     $env:LANG = "en"
  164.  
  165.     # Set environment variable QT_INSTALL_PREFIX. Documentation says it should be
  166.     # used by configure as prefix but this does not seem to work. So, we will
  167.     # also specify -prefix option in configure.
  168.     $env:QT_INSTALL_PREFIX = $QtDir
  169.  
  170.     # Configure, compile and install Qt.
  171.     Push-Location $QtSrcDir
  172.     set OPENSSL_LIBS="-lssl -lcrypto"
  173.     cmd /c "configure.bat -static -debug-and-release -platform win32-g++ -prefix $QtDir `
  174.        -qt-zlib -qt-pcre -qt-libpng -qt-libjpeg -qt-freetype -opengl desktop -sql-sqlite -openssl-linked -I $($OPENSSL_HOME)\include -L$($OPENSSL_HOME)\lib\MinGW `
  175.        -opensource -confirm-license `
  176.        -make libs -nomake tools -nomake examples -nomake tests"
  177.     cmd /c "mingw32-make -k -j$($threads)"
  178.     mingw32-make -k install
  179.     Pop-Location
  180.  
  181.     # Patch Qt's installed mkspecs for static build of application.
  182.     $File = "$QtDir\mkspecs\win32-g++\qmake.conf"
  183.     @"
  184. CONFIG += static
  185. "@ | Out-File -Append $File -Encoding Ascii
  186.  
  187.     Exit-Script
  188. }
  189.  
  190. #-----------------------------------------------------------------------------
  191. # A function to exit this script. The Message parameter is used on error.
  192. #-----------------------------------------------------------------------------
  193.  
  194. function Exit-Script ([string]$Message = "")
  195. {
  196.     $Code = 0
  197.     if ($Message -ne "") {
  198.         Write-Output "ERROR: $Message"
  199.         $Code = 1
  200.     }
  201.     if (-not $NoPause) {
  202.         pause
  203.     }
  204.     exit $Code
  205. }
  206.  
  207. #-----------------------------------------------------------------------------
  208. # Silently create a directory.
  209. #-----------------------------------------------------------------------------
  210.  
  211. function Create-Directory ([string]$Directory)
  212. {
  213.     [void] (New-Item -Path $Directory -ItemType "directory" -Force)
  214. }
  215.  
  216. #-----------------------------------------------------------------------------
  217. # Download a file if not yet present.
  218. # Warning: If file is present but incomplete, do not download it again.
  219. #-----------------------------------------------------------------------------
  220.  
  221. function Download-File ([string]$Url, [string]$OutputFile)
  222. {
  223.     $FileName = Split-Path $Url -Leaf
  224.     if (-not (Test-Path $OutputFile)) {
  225.         # Local file not present, start download.
  226.         Write-Output "Downloading $Url ..."
  227.         try {
  228.             $webclient = New-Object System.Net.WebClient
  229.             $webclient.DownloadFile($Url, $OutputFile)
  230.         }
  231.         catch {
  232.             # Display exception.
  233.             $_
  234.             # Delete partial file, if any.
  235.             if (Test-Path $OutputFile) {
  236.                 Remove-Item -Force $OutputFile
  237.             }
  238.             # Abort
  239.             Exit-Script "Error downloading $FileName"
  240.         }
  241.         # Check that the file is present.
  242.         if (-not (Test-Path $OutputFile)) {
  243.             Exit-Script "Error downloading $FileName"
  244.         }
  245.     }
  246. }
  247.  
  248. #-----------------------------------------------------------------------------
  249. # Get path name of 7zip, abort if not found.
  250. #-----------------------------------------------------------------------------
  251.  
  252. function Get-7zip
  253. {
  254.     $Exe = "C:\Program Files\7-Zip\7z.exe"
  255.     if (-not (Test-Path $Exe)) {
  256.         $Exe = "C:\Program Files (x86)\7-Zip\7z.exe"
  257.     }
  258.     if (-not (Test-Path $Exe)) {
  259.         Exit-Script "7-zip not found, install it first, see http://www.7-zip.org/"
  260.     }
  261.     $Exe
  262. }
  263.  
  264. #-----------------------------------------------------------------------------
  265. # Expand an archive file if not yet done.
  266. #-----------------------------------------------------------------------------
  267.  
  268. function Expand-Archive ([string]$ZipFile, [string]$OutDir, [string]$CheckFile)
  269. {
  270.     # Check presence of expected expanded file or directory.
  271.     if (-not (Test-Path $CheckFile)) {
  272.         Write-Output "Expanding $ZipFile ..."
  273.         & (Get-7zip) x $ZipFile "-o$OutDir" | Select-String -Pattern "^Extracting " -CaseSensitive -NotMatch
  274.         if (-not (Test-Path $CheckFile)) {
  275.             Exit-Script "Error expanding $ZipFile, $OutDir\$CheckFile not found"
  276.         }
  277.     }
  278. }
  279.  
  280. #-----------------------------------------------------------------------------
  281. # Execute main code.
  282. #-----------------------------------------------------------------------------
  283.  
  284. . Main
RAW Paste Data