Advertisement
abelharisov

Untitled

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