Advertisement
Guest User

Compile Qt with static OpenSSL

a guest
Oct 14th, 2019
647
0
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 D:\Qt_Win. 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 D:\Qt_Win\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 = "C:\Qt\Static-OpenSSL\src\qt-everywhere-src-5.13.1.tar",
  79.     $QtStaticDir = "C:\Qt\Static-OpenSSL",
  80.     $QtVersion = "",
  81.     $arch = "32",
  82.     $MingwDir = "",
  83.     $threads = "16",
  84.     $OPENSSL_HOME = "C:\OpenSSL\OpenSSL-Win$($arch)",
  85.     [switch]$NoPause = $false
  86. )
  87.  
  88. # PowerShell execution policy.
  89. Set-StrictMode -Version 3
  90.  
  91. #-----------------------------------------------------------------------------
  92. # Main code
  93. #-----------------------------------------------------------------------------
  94.  
  95. function Main
  96. {
  97.     # Check that 7zip is installed. We use it to expand the downloaded archive.
  98.     [void] (Get-7zip)
  99.  
  100.     # Get Qt source file name from URL.
  101.     $QtSrcFileName = Split-Path -Leaf $QtSrcUrl
  102.  
  103.     # If Qt version is not specified on the command line, try to extract the value.
  104.     if (-not $QtVersion) {
  105.         $QtVersion = $QtSrcFileName -replace "`.[^`.]*$",''
  106.         $QtVersion = $QtVersion -replace 'qt-',''
  107.         $QtVersion = $QtVersion -replace 'everywhere-',''
  108.         $QtVersion = $QtVersion -replace 'opensource-',''
  109.         $QtVersion = $QtVersion -replace 'src-',''
  110.         $QtVersion = $QtVersion -replace '-src',''
  111.     }
  112.     Write-Output "Building static Qt version $QtVersion"
  113.  
  114.     # Qt installation directory.
  115.     $QtDir = "$QtStaticDir\$($QtVersion)_x$($arch)"
  116.  
  117.     # Get MinGW root directory, if not specified on the command line.
  118.     if (-not $MingwDir) {
  119.         # Search all instances of gcc.exe from D:\Qt_Win prebuilt environment.
  120.         $GccList = @(Get-ChildItem -Path C:\Qt\*\Tools\mingw*\bin\gcc.exe | ForEach-Object FullName | Sort-Object)
  121.         if ($GccList.Length -eq 0) {
  122.             Exit-Script "MinGW environment not found, no Qt prebuilt version?"
  123.         }
  124.         $MingwDir = (Split-Path -Parent (Split-Path -Parent $GccList[$GccList.Length - 1]))
  125.         $MingwDir = $MingwDir.Substring(0, $MingwDir.Length - 2)
  126.         $MingwDir += $arch
  127.     }
  128.     Write-Output "Using MinGW from $MingwDir"
  129.  
  130.     # Build the directory tree where the static version of Qt will be installed.
  131.     Create-Directory $QtStaticDir\src
  132.     Create-Directory $QtDir
  133.  
  134.     # Download the Qt source package if not yet done.
  135.     #Download-File $QtSrcUrl $QtStaticDir\src\$QtSrcFileName
  136.  
  137.     # Directory of expanded packages.
  138.     $QtSrcDir = "$QtStaticDir\src\$((Get-Item $QtStaticDir\src\$QtSrcFileName).BaseName)"
  139.  
  140.     # Expand archives if not yet done
  141.     Expand-Archive $QtStaticDir\src\$QtSrcFileName $QtStaticDir\src $QtSrcDir
  142.  
  143.     # Patch Qt's mkspecs for static build.
  144.     $File = "$QtSrcDir\qtbase\mkspecs\win32-g++\qmake.conf"
  145.     if (-not (Select-String -Quiet -SimpleMatch -CaseSensitive "# [QT-STATIC-PATCH]" $File)) {
  146.         Write-Output "Patching $File ..."
  147.         Copy-Item $File "$File.orig"
  148.         @"
  149.  
  150. # [QT-STATIC-PATCH]
  151. QMAKE_LFLAGS += -static -static-libgcc
  152. QMAKE_CFLAGS_RELEASE -= -O2
  153. QMAKE_CFLAGS_RELEASE += -Os -momit-leaf-frame-pointer
  154. DEFINES += QT_STATIC_BUILD
  155. "@ | Out-File -Append $File -Encoding Ascii
  156.     }
  157.  
  158.     # Set a clean path including MinGW.
  159.     $env:Path = "$MingwDir\bin;$MingwDir\opt\bin;$env:SystemRoot\system32;$env:SystemRoot"
  160.  
  161.     # Force English locale to avoid weird effects of tools localization.
  162.     $env:LANG = "en"
  163.  
  164.     # Set environment variable QT_INSTALL_PREFIX. Documentation says it should be
  165.     # used by configure as prefix but this does not seem to work. So, we will
  166.     # also specify -prefix option in configure.
  167.     $env:QT_INSTALL_PREFIX = $QtDir
  168.  
  169.     # Configure, compile and install Qt.
  170.     Push-Location $QtSrcDir
  171.     cmd /C "configure.bat -static -debug-and-release -platform win32-g++ -prefix $QtDir `
  172.        -qt-zlib -qt-pcre -qt-libpng -qt-libjpeg -qt-freetype -opengl desktop -sql-sqlite -ssl -openssl -I $($OPENSSL_HOME)\include -L$($OPENSSL_HOME)\lib\MinGW`
  173.        -opensource -confirm-license `
  174.        -make libs -nomake tools -nomake examples -nomake tests -v"
  175.     cmd /C "mingw32-make -k -j$($threads)"
  176.     mingw32-make -k install
  177.     Pop-Location
  178.  
  179.     # Patch Qt's installed mkspecs for static build of application.
  180.     $File = "$QtDir\mkspecs\win32-g++\qmake.conf"
  181.     @"
  182. CONFIG += static
  183. "@ | Out-File -Append $File -Encoding Ascii
  184.  
  185.     Exit-Script
  186. }
  187.  
  188. #-----------------------------------------------------------------------------
  189. # A function to exit this script. The Message parameter is used on error.
  190. #-----------------------------------------------------------------------------
  191.  
  192. function Exit-Script ([string]$Message = "")
  193. {
  194.     $Code = 0
  195.     if ($Message -ne "") {
  196.         Write-Output "ERROR: $Message"
  197.         $Code = 1
  198.     }
  199.     if (-not $NoPause) {
  200.         pause
  201.     }
  202.     exit $Code
  203. }
  204.  
  205. #-----------------------------------------------------------------------------
  206. # Silently create a directory.
  207. #-----------------------------------------------------------------------------
  208.  
  209. function Create-Directory ([string]$Directory)
  210. {
  211.     [void] (New-Item -Path $Directory -ItemType "directory" -Force)
  212. }
  213.  
  214. #-----------------------------------------------------------------------------
  215. # Download a file if not yet present.
  216. # Warning: If file is present but incomplete, do not download it again.
  217. #-----------------------------------------------------------------------------
  218.  
  219. function Download-File ([string]$Url, [string]$OutputFile)
  220. {
  221.     $FileName = Split-Path $Url -Leaf
  222.     if (-not (Test-Path $OutputFile)) {
  223.         # Local file not present, start download.
  224.         Write-Output "Downloading $Url ..."
  225.         try {
  226.             $webclient = New-Object System.Net.WebClient
  227.             $webclient.DownloadFile($Url, $OutputFile)
  228.         }
  229.         catch {
  230.             # Display exception.
  231.             $_
  232.             # Delete partial file, if any.
  233.             if (Test-Path $OutputFile) {
  234.                 Remove-Item -Force $OutputFile
  235.             }
  236.             # Abort
  237.             Exit-Script "Error downloading $FileName"
  238.         }
  239.         # Check that the file is present.
  240.         if (-not (Test-Path $OutputFile)) {
  241.             Exit-Script "Error downloading $FileName"
  242.         }
  243.     }
  244. }
  245.  
  246. #-----------------------------------------------------------------------------
  247. # Get path name of 7zip, abort if not found.
  248. #-----------------------------------------------------------------------------
  249.  
  250. function Get-7zip
  251. {
  252.     $Exe = "C:\Program Files\7-Zip\7z.exe"
  253.     if (-not (Test-Path $Exe)) {
  254.         $Exe = "C:\Program Files (x86)\7-Zip\7z.exe"
  255.     }
  256.     if (-not (Test-Path $Exe)) {
  257.         Exit-Script "7-zip not found, install it first, see http://www.7-zip.org/"
  258.     }
  259.     $Exe
  260. }
  261.  
  262. #-----------------------------------------------------------------------------
  263. # Expand an archive file if not yet done.
  264. #-----------------------------------------------------------------------------
  265.  
  266. function Expand-Archive ([string]$ZipFile, [string]$OutDir, [string]$CheckFile)
  267. {
  268.     # Check presence of expected expanded file or directory.
  269.     if (-not (Test-Path $CheckFile)) {
  270.         Write-Output "Expanding $ZipFile ..."
  271.         & (Get-7zip) x $ZipFile "-o$OutDir" | Select-String -Pattern "^Extracting " -CaseSensitive -NotMatch
  272.         if (-not (Test-Path $CheckFile)) {
  273.             Exit-Script "Error expanding $ZipFile, $OutDir\$CheckFile not found"
  274.         }
  275.     }
  276. }
  277.  
  278. #-----------------------------------------------------------------------------
  279. # Execute main code.
  280. #-----------------------------------------------------------------------------
  281.  
  282. . Main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement