QT powershell错误windows-build-QT-static.ps1

QT powershell错误windows-build-QT-static.ps1,qt,static,compiler-errors,Qt,Static,Compiler Errors,在6-7次尝试编译QT静态版本后,我使用了本文 还有错误: 我已经安装了Qt5.7 我下载了 然后更改必要的行 [CmdletBinding()] param( $QtSrcUrl = "https://download.qt.io/snapshots/qt/5.8/5.8.0/latest_src/qt-everywhere-opensource-src-5.8.0.7z", $QtStaticDir = "C:\Qt\Static", $QtVersion = "5.

在6-7次尝试编译QT静态版本后,我使用了本文 还有错误: 我已经安装了Qt5.7

我下载了 然后更改必要的行

[CmdletBinding()]
param(
    $QtSrcUrl = "https://download.qt.io/snapshots/qt/5.8/5.8.0/latest_src/qt-everywhere-opensource-src-5.8.0.7z",
    $QtStaticDir = "C:\Qt\Static",
    $QtVersion = "5.7",
    $MingwDir = "C:\Qt\Tools\mingw530_32\bin\gcc.exe",
    [switch]$NoPause = $false
)
错误是

Windows PowerShell
Copyright (C) 2012 Microsoft Corporation. All rights reserved.

PS C:\Users\Admin> C:\Qt\windows-build-qt-static.ps1
Building static Qt version 5.7
Using MinGW from C:\Qt\Tools\mingw530_32\bin\gcc.exe
Downloading https://download.qt.io/snapshots/qt/5.8/5.8.0/latest_src/qt-everywhere-opensource-src-5.8.0.7z ...
Expanding C:\Qt\Static\src\qt-everywhere-opensource-src-5.8.0.7z ...


7-Zip [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21

Scanning the drive for archives:
1 file, 353390329 bytes (338 MiB)

--
Path = C:\Qt\Static\src\qt-everywhere-opensource-src-5.8.0.7z
Type = 7z
Physical Size = 353390329
Headers Size = 1959904
Method = LZMA2:24
Solid = +
Blocks = 1

Everything is Ok

Folders: 17053
Files: 155016
Size:       1670093035
Compressed: 353390329
Patching C:\Qt\Static\src\qt-everywhere-opensource-src-5.8.0\qtbase\mkspecs\win32-g++\qmake.conf ...
+ cd qtbase
+ C:\Qt\Static\src\qt-everywhere-opensource-src-5.8.0\qtbase\configure.bat -top-level -static -debug-and-release -platfo
rm win32-g++ -prefix C:\Qt\Static\5.7         -qt-zlib -qt-pcre -qt-libpng -qt-libjpeg -qt-freetype -opengl desktop -qt-
sql-sqlite -no-openssl         -opensource -confirm-license         -make libs -nomake tools -nomake examples -nomake te
sts
Please wait while bootstrapping configure ...
Perl not found in PATH. Aborting.
mingw32-make : The term 'mingw32-make' is not recognized as the name of a cmdlet, function, script file, or operable pr
ogram. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\Qt\windows-build-qt-static.ps1:170 char:5
+     mingw32-make -k -j4
+     ~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (mingw32-make:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

mingw32-make : The term 'mingw32-make' is not recognized as the name of a cmdlet, function, script file, or operable pr
ogram. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\Qt\windows-build-qt-static.ps1:171 char:5
+     mingw32-make -k install
+     ~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (mingw32-make:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Out-File : Could not find a part of the path 'C:\Qt\Static\5.7\mkspecs\win32-g++\qmake.conf'.
At C:\Qt\windows-build-qt-static.ps1:178 char:6
+ "@ | Out-File -Append $File -Encoding Ascii
+      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OpenError: (:) [Out-File], DirectoryNotFoundException
    + FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.OutFileCommand


一年前,这个
windows build qt static.ps1
脚本对我起了作用。 注意,在第117行,我刚刚从路径
C:\Qt\Tools\mingw*\bin\gcc.exe中删除了
Qt\
Tools
之间的一个
*

#-----------------------------------------------------------------------------
# 
#  Copyright (c) 2013-2015, Thierry Lelegard
#  All rights reserved.
# 
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions are met:
# 
#  1. Redistributions of source code must retain the above copyright notice,
#     this list of conditions and the following disclaimer. 
#  2. Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the distribution. 
# 
#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
#  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
#  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
#  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
#  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
#  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
#  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
#  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
#  THE POSSIBILITY OF SUCH DAMAGE.
# 
#-----------------------------------------------------------------------------

<#
 .SYNOPSIS

  Build a static version of Qt for Windows.

 .DESCRIPTION

  This scripts downloads Qt source code, compiles and installs a static version
  of Qt. It assumes that a prebuilt Qt / MinGW environment is already installed,
  typically in C:\Qt. This prebuilt environment uses shared libraries. It is
  supposed to remain the main development environment for Qt. This script adds
  a static version of the Qt libraries in order to allow the construction of
  standalone and self-sufficient executable.

  This script is typically run from the Windows Explorer.

  Requirements:
  - Windows PowerShell 3.0 or higher.
  - 7-zip.

 .PARAMETER QtSrcUrl

  URL of the Qt source file archive.
  By default, use the latest identified version.

 .PARAMETER QtStaticDir

  Root directory where the static versions of Qt are installed.
  By default, use C:\Qt\Static.

 .PARAMETER QtVersion

  The Qt version. By default, this script tries to extract the version number
  from the Qt source file name.

 .PARAMETER MingwDir

  Root directory of the MinGW prebuilt environment. By default, use the version
  which was installed by the prebuilt Qt environment.

 .PARAMETER NoPause

  Do not wait for the user to press <enter> at end of execution. By default,
  execute a "pause" instruction at the end of execution, which is useful
  when the script was run from Windows Explorer.
#>

[CmdletBinding()]
param(
    $QtSrcUrl = "http://download.qt.io/official_releases/qt/5.5/5.5.1/single/qt-everywhere-opensource-src-5.5.1.7z",
    $QtStaticDir = "C:\Qt\Static",
    $QtVersion = "",
    $MingwDir = "",
    [switch]$NoPause = $false
)

# PowerShell execution policy.
Set-StrictMode -Version 3

#-----------------------------------------------------------------------------
# Main code
#-----------------------------------------------------------------------------

function Main
{
    # Check that 7zip is installed. We use it to expand the downloaded archive.
    [void] (Get-7zip)

    # Get Qt source file name from URL.
    $QtSrcFileName = Split-Path -Leaf $QtSrcUrl

    # If Qt version is not specified on the command line, try to extract the value.
    if (-not $QtVersion) {
        $QtVersion = $QtSrcFileName -replace "`.[^`.]*$",''
        $QtVersion = $QtVersion -replace 'qt-',''
        $QtVersion = $QtVersion -replace 'everywhere-',''
        $QtVersion = $QtVersion -replace 'opensource-',''
        $QtVersion = $QtVersion -replace 'src-',''
        $QtVersion = $QtVersion -replace '-src',''
    }
    Write-Output "Building static Qt version $QtVersion"

    # Qt installation directory.
    $QtDir = "$QtStaticDir\$QtVersion"

    # Get MinGW root directory, if not specified on the command line.
    if (-not $MingwDir) {
        # Search all instances of gcc.exe from C:\Qt prebuilt environment.
        $GccList = @(Get-ChildItem -Path C:\Qt\Tools\mingw*\bin\gcc.exe | ForEach-Object FullName | Sort-Object)
        if ($GccList.Length -eq 0) {
            Exit-Script "MinGW environment not found, no Qt prebuilt version?"
        }
        $MingwDir = (Split-Path -Parent (Split-Path -Parent $GccList[$GccList.Length - 1]))
    }
    Write-Output "Using MinGW from $MingwDir"

    # Build the directory tree where the static version of Qt will be installed.
    Create-Directory $QtStaticDir\src
    Create-Directory $QtDir

    # Download the Qt source package if not yet done.
    Download-File $QtSrcUrl $QtStaticDir\src\$QtSrcFileName

    # Directory of expanded packages.
    $QtSrcDir = "$QtStaticDir\src\$((Get-Item $QtStaticDir\src\$QtSrcFileName).BaseName)"

    # Expand archives if not yet done
    Expand-Archive $QtStaticDir\src\$QtSrcFileName $QtStaticDir\src $QtSrcDir

    # Patch Qt's mkspecs for static build.
    $File = "$QtSrcDir\qtbase\mkspecs\win32-g++\qmake.conf"
    if (-not (Select-String -Quiet -SimpleMatch -CaseSensitive "# [QT-STATIC-PATCH]" $File)) {
        Write-Output "Patching $File ..."
        Copy-Item $File "$File.orig"
        @"

# [QT-STATIC-PATCH]
QMAKE_LFLAGS += -static -static-libgcc
QMAKE_CFLAGS_RELEASE -= -O2
QMAKE_CFLAGS_RELEASE += -Os -momit-leaf-frame-pointer
DEFINES += QT_STATIC_BUILD
"@ | Out-File -Append $File -Encoding Ascii
    }

    # Set a clean path including MinGW.
    $env:Path = "$MingwDir\bin;$MingwDir\opt\bin"

    # Force English locale to avoid weird effects of tools localization.
    $env:LANG = "en"

    # Set environment variable QT_INSTALL_PREFIX. Documentation says it should be
    # used by configure as prefix but this does not seem to work. So, we will
    # also specify -prefix option in configure.
    $env:QT_INSTALL_PREFIX = $QtDir

    # Configure, compile and install Qt.
    Push-Location $QtSrcDir
    & $env:SystemRoot\System32\cmd.exe /c "configure.bat -static -debug-and-release -platform win32-g++ -prefix $QtDir `
        -qt-zlib -qt-pcre -qt-libpng -qt-libjpeg -qt-freetype -opengl desktop -qt-sql-sqlite -no-openssl `
        -opensource -confirm-license `
        -make libs -nomake tools -nomake examples -nomake tests"
    mingw32-make -k -j4
    mingw32-make -k install
    Pop-Location

    # Patch Qt's installed mkspecs for static build of application.
    $File = "$QtDir\mkspecs\win32-g++\qmake.conf"
    @"
CONFIG += static
"@ | Out-File -Append $File -Encoding Ascii

    Exit-Script
}

#-----------------------------------------------------------------------------
# A function to exit this script. The Message parameter is used on error.
#-----------------------------------------------------------------------------

function Exit-Script ([string]$Message = "")
{
    $Code = 0
    if ($Message -ne "") {
        Write-Output "ERROR: $Message"
        $Code = 1
    }
    if (-not $NoPause) {
        pause
    }
    exit $Code
}

#-----------------------------------------------------------------------------
# Silently create a directory.
#-----------------------------------------------------------------------------

function Create-Directory ([string]$Directory)
{
    [void] (New-Item -Path $Directory -ItemType "directory" -Force)
}

#-----------------------------------------------------------------------------
# Download a file if not yet present.
# Warning: If file is present but incomplete, do not download it again.
#-----------------------------------------------------------------------------

function Download-File ([string]$Url, [string]$OutputFile)
{
    $FileName = Split-Path $Url -Leaf
    if (-not (Test-Path $OutputFile)) {
        # Local file not present, start download.
        Write-Output "Downloading $Url ..."
        try {
            $webclient = New-Object System.Net.WebClient
            $webclient.DownloadFile($Url, $OutputFile)
        }
        catch {
            # Display exception.
            $_
            # Delete partial file, if any.
            if (Test-Path $OutputFile) {
                Remove-Item -Force $OutputFile
            }
            # Abort
            Exit-Script "Error downloading $FileName"
        }
        # Check that the file is present.
        if (-not (Test-Path $OutputFile)) {
            Exit-Script "Error downloading $FileName"
        }
    }
}

#-----------------------------------------------------------------------------
# Get path name of 7zip, abort if not found.
#-----------------------------------------------------------------------------

function Get-7zip
{
    $Exe = "C:\Program Files\7-Zip\7z.exe"
    if (-not (Test-Path $Exe)) {
        $Exe = "C:\Program Files (x86)\7-Zip\7z.exe"
    }
    if (-not (Test-Path $Exe)) {
        Exit-Script "7-zip not found, install it first, see http://www.7-zip.org/"
    }
    $Exe
}

#-----------------------------------------------------------------------------
# Expand an archive file if not yet done.
#-----------------------------------------------------------------------------

function Expand-Archive ([string]$ZipFile, [string]$OutDir, [string]$CheckFile)
{
    # Check presence of expected expanded file or directory.
    if (-not (Test-Path $CheckFile)) {
        Write-Output "Expanding $ZipFile ..."
        & (Get-7zip) x $ZipFile "-o$OutDir" | Select-String -Pattern "^Extracting " -CaseSensitive -NotMatch
        if (-not (Test-Path $CheckFile)) {
            Exit-Script "Error expanding $ZipFile, $OutDir\$CheckFile not found"
        }
    }
}

#-----------------------------------------------------------------------------
# Execute main code.
#-----------------------------------------------------------------------------

. Main
#-----------------------------------------------------------------------------
# 
#版权所有(c)2013-2015,Thierry Lelegard
#版权所有。
# 
#以源代码和二进制形式重新分发和使用,带或不带
#如果满足以下条件,则允许进行修改:
# 
#  1. 源代码的重新分发必须保留上述版权声明,
#此条件列表和以下免责声明。
#  2. 以二进制形式重新分发必须复制上述版权
#请注意,此条件列表和中的以下免责声明
#随分发提供的文件和/或其他材料。
# 
#本软件由版权所有者和贡献者“按原样”提供
#以及任何明示或暗示的保证,包括但不限于
#对适销性和特定用途适用性的默示保证
#拒绝承认。在任何情况下,版权所有人或贡献者均不得
#对任何直接、间接、附带、特殊、示范或
#间接损害(包括但不限于采购
#替代商品或服务;使用、数据或利润的损失;或业务
#中断)无论是何种原因造成的,且基于任何责任理论,无论是在
#合同、严格责任或侵权行为(包括疏忽或其他)
#因使用本软件而产生的任何后果,即使
#这种损害的可能性。
# 
#-----------------------------------------------------------------------------
[CmdletBinding()]
param(
$QtSrcUrl=”http://download.qt.io/official_releases/qt/5.5/5.5.1/single/qt-everywhere-opensource-src-5.5.1.7z",
$QtStaticDir=“C:\Qt\Static”,
$QtVersion=“”,
$MingwDir=“”,
[开关]$NoPause=$false
)
#PowerShell执行策略。
设置模式-版本3
#-----------------------------------------------------------------------------
#主代码
#-----------------------------------------------------------------------------
主要功能
{
#检查是否安装了7zip。我们使用它来扩展下载的存档。
[void](Get-7zip)
#从URL获取Qt源文件名。
$QTSRCFILNAME=拆分路径-叶$QtSrcUrl
#如果命令行上未指定Qt版本,请尝试提取该值。
如果(-非$QtVersion){
$QtVersion=$QTSRCFILNAME-替换“`.[^`.]*$”,“”
$QtVersion=$QtVersion-替换“qt-”,“”
$QtVersion=$QtVersion-替换“无处不在-”,“”
$QtVersion=$QtVersion-替换“开源-”,“”
$QtVersion=$QtVersion-替换为'src-','
$QtVersion=$QtVersion-替换'-src',''
}
写入输出“构建静态Qt版本$QtVersion”
#Qt安装目录。
$QtDir=“$QtStaticDir\$QtVersion”
#如果未在命令行中指定,则获取MinGW根目录。
如果(-not$MingwDir){
#从C:\Qt预构建环境中搜索gcc.exe的所有实例。
$GccList=@(获取ChildItem-路径C:\Qt\Tools\mingw*\bin\gcc.exe | ForEach对象全名|排序对象)
如果($GccList.Length-等式0){
退出脚本“找不到MinGW环境,没有Qt预构建版本?”
}
$MingwDir=(拆分路径-父级(拆分路径-父级$GccList[$GccList.Length-1]))
}
写入输出“从$MingwDir使用MinGW”
#构建将安装Qt静态版本的目录树。
创建目录$QtStaticDir\src
创建目录$QtDir
#如果尚未完成,请下载Qt源代码包。
下载文件$QtSrcUrl$QtStaticDir\src\$QTSRCFILNAME
#扩展包的目录。
$QtSrcDir=“$QtStaticDir\src\$(获取项$QtStaticDir\src\$QTSRCDILNAME).BaseName)”
#如果尚未展开归档,请展开归档
展开归档文件$QtStaticDir\src\$QtSrcFileName$QtStaticDir\src$QtSrcDir
#为静态构建修补Qt的mkspecs。
$File=“$QtSrcDir\qtbase\mkspecs\win32-g++\qmake.conf”
if(-not)(选择String-Quiet-SimpleMatch-CaseSensitive“#[QT-STATIC-PATCH]“$File)){
写入输出“修补$File…”
复制项目$File“$File.orig”
@"
#[QT-STATIC-PATCH]
QMAKE_LFLAGS+=-静态-静态libgcc
QMAKE\u CFLAGS\u释放-=-O2
QMAKE\u CFLAGS\u RELEASE+=-Os-momit叶帧指针
定义+=QT\u静态\u构建
“@| Out File-追加$File-编码Ascii
}
#设置一个干净的路径,包括MinGW。
$env:Path=“$MingwDir\bin;$MingwDir\opt\bin”
#强制使用英语区域设置,以避免工具本地化的奇怪效果。
$env:LANG=“en”
#设置环境变量QT_INSTALL_PREFIX。文档说明应该
#由configure用作前缀,但这似乎不起作用。因此,我们将
#还可以在配置中指定-prefix选项。
$env:QT\u安装\u前缀=$QtDir
#配置、编译和安装Qt。
推送位置$QtSrcDir
&$env:SystemRoot\System32\cmd.exe/c“configure.bat-static-debug and release-platform win32-g++-前缀$QtDir`
-qt zlib-qt pcre-qt libpng-qt libjpeg-qt freetype-opengl desktop-qt sql sqlite-no openssl`
-开源-确认许可证`
-制作libs-nomake工具-nomake示例-nomake测试”
mingw32 make-k-j4
mingw32 make-k安装
流行位置
#为静态构建应用程序修补Qt安装的mkspecs。
$File=“$QtDir\mkspecs\win32-g++\qmake.conf”
@"
配置+=静态
“@| Out File-追加$File-编码Ascii
退出脚本
}
#-----------------------
Out-File : Could not find a part of the path 'C:\Qt\Static\5.7\mkspecs\win32-g++\qmake.conf'.
At C:\Qt\windows-build-qt-static.ps1:178 char:6
+ "@ | Out-File -Append $File -Encoding Ascii
#-----------------------------------------------------------------------------
# 
#  Copyright (c) 2013-2015, Thierry Lelegard
#  All rights reserved.
# 
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions are met:
# 
#  1. Redistributions of source code must retain the above copyright notice,
#     this list of conditions and the following disclaimer. 
#  2. Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the distribution. 
# 
#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
#  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
#  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
#  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
#  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
#  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
#  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
#  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
#  THE POSSIBILITY OF SUCH DAMAGE.
# 
#-----------------------------------------------------------------------------

<#
 .SYNOPSIS

  Build a static version of Qt for Windows.

 .DESCRIPTION

  This scripts downloads Qt source code, compiles and installs a static version
  of Qt. It assumes that a prebuilt Qt / MinGW environment is already installed,
  typically in C:\Qt. This prebuilt environment uses shared libraries. It is
  supposed to remain the main development environment for Qt. This script adds
  a static version of the Qt libraries in order to allow the construction of
  standalone and self-sufficient executable.

  This script is typically run from the Windows Explorer.

  Requirements:
  - Windows PowerShell 3.0 or higher.
  - 7-zip.

 .PARAMETER QtSrcUrl

  URL of the Qt source file archive.
  By default, use the latest identified version.

 .PARAMETER QtStaticDir

  Root directory where the static versions of Qt are installed.
  By default, use C:\Qt\Static.

 .PARAMETER QtVersion

  The Qt version. By default, this script tries to extract the version number
  from the Qt source file name.

 .PARAMETER MingwDir

  Root directory of the MinGW prebuilt environment. By default, use the version
  which was installed by the prebuilt Qt environment.

 .PARAMETER NoPause

  Do not wait for the user to press <enter> at end of execution. By default,
  execute a "pause" instruction at the end of execution, which is useful
  when the script was run from Windows Explorer.
#>

[CmdletBinding()]
param(
    $QtSrcUrl = "http://download.qt.io/official_releases/qt/5.5/5.5.1/single/qt-everywhere-opensource-src-5.5.1.7z",
    $QtStaticDir = "C:\Qt\Static",
    $QtVersion = "",
    $MingwDir = "",
    [switch]$NoPause = $false
)

# PowerShell execution policy.
Set-StrictMode -Version 3

#-----------------------------------------------------------------------------
# Main code
#-----------------------------------------------------------------------------

function Main
{
    # Check that 7zip is installed. We use it to expand the downloaded archive.
    [void] (Get-7zip)

    # Get Qt source file name from URL.
    $QtSrcFileName = Split-Path -Leaf $QtSrcUrl

    # If Qt version is not specified on the command line, try to extract the value.
    if (-not $QtVersion) {
        $QtVersion = $QtSrcFileName -replace "`.[^`.]*$",''
        $QtVersion = $QtVersion -replace 'qt-',''
        $QtVersion = $QtVersion -replace 'everywhere-',''
        $QtVersion = $QtVersion -replace 'opensource-',''
        $QtVersion = $QtVersion -replace 'src-',''
        $QtVersion = $QtVersion -replace '-src',''
    }
    Write-Output "Building static Qt version $QtVersion"

    # Qt installation directory.
    $QtDir = "$QtStaticDir\$QtVersion"

    # Get MinGW root directory, if not specified on the command line.
    if (-not $MingwDir) {
        # Search all instances of gcc.exe from C:\Qt prebuilt environment.
        $GccList = @(Get-ChildItem -Path C:\Qt\Tools\mingw*\bin\gcc.exe | ForEach-Object FullName | Sort-Object)
        if ($GccList.Length -eq 0) {
            Exit-Script "MinGW environment not found, no Qt prebuilt version?"
        }
        $MingwDir = (Split-Path -Parent (Split-Path -Parent $GccList[$GccList.Length - 1]))
    }
    Write-Output "Using MinGW from $MingwDir"

    # Build the directory tree where the static version of Qt will be installed.
    Create-Directory $QtStaticDir\src
    Create-Directory $QtDir

    # Download the Qt source package if not yet done.
    Download-File $QtSrcUrl $QtStaticDir\src\$QtSrcFileName

    # Directory of expanded packages.
    $QtSrcDir = "$QtStaticDir\src\$((Get-Item $QtStaticDir\src\$QtSrcFileName).BaseName)"

    # Expand archives if not yet done
    Expand-Archive $QtStaticDir\src\$QtSrcFileName $QtStaticDir\src $QtSrcDir

    # Patch Qt's mkspecs for static build.
    $File = "$QtSrcDir\qtbase\mkspecs\win32-g++\qmake.conf"
    if (-not (Select-String -Quiet -SimpleMatch -CaseSensitive "# [QT-STATIC-PATCH]" $File)) {
        Write-Output "Patching $File ..."
        Copy-Item $File "$File.orig"
        @"

# [QT-STATIC-PATCH]
QMAKE_LFLAGS += -static -static-libgcc
QMAKE_CFLAGS_RELEASE -= -O2
QMAKE_CFLAGS_RELEASE += -Os -momit-leaf-frame-pointer
DEFINES += QT_STATIC_BUILD
"@ | Out-File -Append $File -Encoding Ascii
    }

    # Set a clean path including MinGW.
    $env:Path = "$MingwDir\bin;$MingwDir\opt\bin"

    # Force English locale to avoid weird effects of tools localization.
    $env:LANG = "en"

    # Set environment variable QT_INSTALL_PREFIX. Documentation says it should be
    # used by configure as prefix but this does not seem to work. So, we will
    # also specify -prefix option in configure.
    $env:QT_INSTALL_PREFIX = $QtDir

    # Configure, compile and install Qt.
    Push-Location $QtSrcDir
    & $env:SystemRoot\System32\cmd.exe /c "configure.bat -static -debug-and-release -platform win32-g++ -prefix $QtDir `
        -qt-zlib -qt-pcre -qt-libpng -qt-libjpeg -qt-freetype -opengl desktop -qt-sql-sqlite -no-openssl `
        -opensource -confirm-license `
        -make libs -nomake tools -nomake examples -nomake tests"
    mingw32-make -k -j4
    mingw32-make -k install
    Pop-Location

    # Patch Qt's installed mkspecs for static build of application.
    $File = "$QtDir\mkspecs\win32-g++\qmake.conf"
    @"
CONFIG += static
"@ | Out-File -Append $File -Encoding Ascii

    Exit-Script
}

#-----------------------------------------------------------------------------
# A function to exit this script. The Message parameter is used on error.
#-----------------------------------------------------------------------------

function Exit-Script ([string]$Message = "")
{
    $Code = 0
    if ($Message -ne "") {
        Write-Output "ERROR: $Message"
        $Code = 1
    }
    if (-not $NoPause) {
        pause
    }
    exit $Code
}

#-----------------------------------------------------------------------------
# Silently create a directory.
#-----------------------------------------------------------------------------

function Create-Directory ([string]$Directory)
{
    [void] (New-Item -Path $Directory -ItemType "directory" -Force)
}

#-----------------------------------------------------------------------------
# Download a file if not yet present.
# Warning: If file is present but incomplete, do not download it again.
#-----------------------------------------------------------------------------

function Download-File ([string]$Url, [string]$OutputFile)
{
    $FileName = Split-Path $Url -Leaf
    if (-not (Test-Path $OutputFile)) {
        # Local file not present, start download.
        Write-Output "Downloading $Url ..."
        try {
            $webclient = New-Object System.Net.WebClient
            $webclient.DownloadFile($Url, $OutputFile)
        }
        catch {
            # Display exception.
            $_
            # Delete partial file, if any.
            if (Test-Path $OutputFile) {
                Remove-Item -Force $OutputFile
            }
            # Abort
            Exit-Script "Error downloading $FileName"
        }
        # Check that the file is present.
        if (-not (Test-Path $OutputFile)) {
            Exit-Script "Error downloading $FileName"
        }
    }
}

#-----------------------------------------------------------------------------
# Get path name of 7zip, abort if not found.
#-----------------------------------------------------------------------------

function Get-7zip
{
    $Exe = "C:\Program Files\7-Zip\7z.exe"
    if (-not (Test-Path $Exe)) {
        $Exe = "C:\Program Files (x86)\7-Zip\7z.exe"
    }
    if (-not (Test-Path $Exe)) {
        Exit-Script "7-zip not found, install it first, see http://www.7-zip.org/"
    }
    $Exe
}

#-----------------------------------------------------------------------------
# Expand an archive file if not yet done.
#-----------------------------------------------------------------------------

function Expand-Archive ([string]$ZipFile, [string]$OutDir, [string]$CheckFile)
{
    # Check presence of expected expanded file or directory.
    if (-not (Test-Path $CheckFile)) {
        Write-Output "Expanding $ZipFile ..."
        & (Get-7zip) x $ZipFile "-o$OutDir" | Select-String -Pattern "^Extracting " -CaseSensitive -NotMatch
        if (-not (Test-Path $CheckFile)) {
            Exit-Script "Error expanding $ZipFile, $OutDir\$CheckFile not found"
        }
    }
}

#-----------------------------------------------------------------------------
# Execute main code.
#-----------------------------------------------------------------------------

. Main