Perl not found in PATH. Aborting.
-
wrote on 28 Jan 2017, 15:57 last edited by Denjas
I am trying to build a static version of Qt 5.8. My Perl installation is located in C:\Perl64 and the path is also included in my PATH variable: C:\Perl64\bin;C:\Perl64\site\bin;
But when configure.bat is being run, it gives me an error saying that Perl could not be found: http://i.imgur.com/5ExL8Of.png
-
Hi,
Did you check the content of PATH in that console ?
-
wrote on 29 Jan 2017, 09:18 last edited by
Yes, I tried printing the PATH variable to the console and it did have the Perl path in it: http://i.imgur.com/7NLGCgK.png
-
Might be a silly question, but is it the same console you used to build Qt ?
-
I am trying to build a static version of Qt 5.8. My Perl installation is located in C:\Perl64 and the path is also included in my PATH variable: C:\Perl64\bin;C:\Perl64\site\bin;
But when configure.bat is being run, it gives me an error saying that Perl could not be found: http://i.imgur.com/5ExL8Of.png
wrote on 30 Jan 2017, 03:24 last edited by@Denjas Also I would avoid building in powershell. It has enough differences from a normal cmd to cause issues.
I don't know if it's causing this problem, but just general info for problems I've run into building Qt before.
As others suggested verify your path in your console by running
set
and it should show you all your variables, make sure PATH has your perl in it. Or even easier, just try runningperl
in that console. -
wrote on 22 Feb 2017, 06:38 last edited by matobodo
The problem that the powershell could not find Perl is in the script line 154. There is a PATH variable which the script sets for the session.
$env:Path = "$MingwDir\bin;$MingwDir\opt\bin;$env:SystemRoot\system32;$env:SystemRoot"
Just add the perl paths so it looks like this:
$env:Path = "$MingwDir\bin;$MingwDir\opt\bin;$env:SystemRoot\system32;$env:SystemRoot;C:\Perl\site\bin;C:\Perl\bin"
This powershell script had more issues for me so after a few changes it worked for Qt 5.8.0. I have also changed default paths in script so I do not have to write it every time.
#----------------------------------------------------------------------------- # # Copyright (c) 2013, 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-project.org/official_releases/qt/5.8/5.8.0/single/qt-everywhere-opensource-src-5.8.0.7z", $QtStaticDir = "C:\Qt\Static", $QtVersion = "", $MingwDir = "C:\Qt\Tools\mingw530_32", [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;$env:SystemRoot\system32;$env:SystemRoot;C:\Perl\site\bin;C:\Perl\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 cmd /c "configure.bat -static -debug-and-release -platform win32-g++ -prefix $QtDir ` -qt-zlib -qt-pcre -qt-libpng -qt-libjpeg -qt-freetype -opengl desktop -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 = "$QtSrcDir\qtbase\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
-
The problem that the powershell could not find Perl is in the script line 154. There is a PATH variable which the script sets for the session.
$env:Path = "$MingwDir\bin;$MingwDir\opt\bin;$env:SystemRoot\system32;$env:SystemRoot"
Just add the perl paths so it looks like this:
$env:Path = "$MingwDir\bin;$MingwDir\opt\bin;$env:SystemRoot\system32;$env:SystemRoot;C:\Perl\site\bin;C:\Perl\bin"
This powershell script had more issues for me so after a few changes it worked for Qt 5.8.0. I have also changed default paths in script so I do not have to write it every time.
#----------------------------------------------------------------------------- # # Copyright (c) 2013, 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-project.org/official_releases/qt/5.8/5.8.0/single/qt-everywhere-opensource-src-5.8.0.7z", $QtStaticDir = "C:\Qt\Static", $QtVersion = "", $MingwDir = "C:\Qt\Tools\mingw530_32", [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;$env:SystemRoot\system32;$env:SystemRoot;C:\Perl\site\bin;C:\Perl\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 cmd /c "configure.bat -static -debug-and-release -platform win32-g++ -prefix $QtDir ` -qt-zlib -qt-pcre -qt-libpng -qt-libjpeg -qt-freetype -opengl desktop -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 = "$QtSrcDir\qtbase\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
@matobodo I would not use PowerShell to build Qt. CMD.exe is the supported environment.
-
wrote on 22 Feb 2017, 08:49 last edited by matobodo
@jsulm
In the Qt wiki is tutorial with PowerShell
https://wiki.qt.io/Building_a_static_Qt_for_Windows_using_MinGW#Windows_PowerShell -
wrote on 9 May 2017, 09:48 last edited by
-
@jcolominas Since I don't use PowerShell (and mainly develop on Linux) I cannot help here much.
-
wrote on 10 May 2017, 04:04 last edited by
@jcolominas I agree with @jsulm . I'm also a linux user, but have built Qt for Windows using both Visual Studio and mingw many many times.
I have never been successful using powershell though. I always did it with cygwin, msys, or more often than not cmd. Cygwin and msys come with their own set of problems. I usually use one or the other for the awesomeness of bash getting everything set up then shell out to a cmd to build.
Powershell does things that require powershell knowledge of which I have none. So things that should just work, don't. And if you don't know why you're stuck. It's not that it isn't possible to use powershell it's just that it adds problems and if you aren't good with windows/powershell you may create more headache than it's worth using it over cmd.
-
The problem that the powershell could not find Perl is in the script line 154. There is a PATH variable which the script sets for the session.
$env:Path = "$MingwDir\bin;$MingwDir\opt\bin;$env:SystemRoot\system32;$env:SystemRoot"
Just add the perl paths so it looks like this:
$env:Path = "$MingwDir\bin;$MingwDir\opt\bin;$env:SystemRoot\system32;$env:SystemRoot;C:\Perl\site\bin;C:\Perl\bin"
This powershell script had more issues for me so after a few changes it worked for Qt 5.8.0. I have also changed default paths in script so I do not have to write it every time.
#----------------------------------------------------------------------------- # # Copyright (c) 2013, 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-project.org/official_releases/qt/5.8/5.8.0/single/qt-everywhere-opensource-src-5.8.0.7z", $QtStaticDir = "C:\Qt\Static", $QtVersion = "", $MingwDir = "C:\Qt\Tools\mingw530_32", [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;$env:SystemRoot\system32;$env:SystemRoot;C:\Perl\site\bin;C:\Perl\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 cmd /c "configure.bat -static -debug-and-release -platform win32-g++ -prefix $QtDir ` -qt-zlib -qt-pcre -qt-libpng -qt-libjpeg -qt-freetype -opengl desktop -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 = "$QtSrcDir\qtbase\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