Saturday, August 9, 2014

CMake Problems Windows 8.1 – Part 2

The second problem was with the paths to the test files generated in the make file.  When CMake generates a custom command for making the test directory and copying a test file, it uses the Unix style path forward slash separator regardless of the generator being used.  While cmd.exe on Windows does support these style paths, the md and copy commands do not (in fact the forward slash indicates an option like the minus does for Linux and MSYS commands).  This obviously causes problems with the custom commands.

In CMake, the file command supports a TO_NATIVE_PATH option, but when used, this command didn't work as expected (didn't do anything).  Apparently this is a known issue and according to what I read, this is not the purpose of this command anyway.  So instead, a ConvertPath function was added to the CMake build file, which if the MinGW generator is being used, changes forward slashes to back slashes and surrounds that path with double quotes to deal with paths that may have spaces in them (which doesn't occur with the MinGW generator like with the MSYS generator).  For any other generator, the path is not changed.

Another build problem was found with the awk program required for building.  When using MSYS with MinGW, MSYS included many of the Unix utilities including the awk program.  Fortunately, Git for Windows includes many of the MSYS utilities.  However, the gawk program is included instead (stands for GNU awk).  Also included is an awk script, but this shell script is meant to be run by the sh.exe command that is part of MSYS, which just calls gawk.  When run from cmd.exe the script doesn't work.

When looking for the awk program, CMake found this awk script and so an error occurred from the make command running under cmd.exe.  To make CMake find gawk.exe instead of this script, the CMake build file was modified to look for gawk first, and if this is not found, then looks for awk.  In fact, on Linux, awk is also called gawk, but there is an awk file that soft links to it, so that either can be used.

While testing a minor mismatch was found with encoder test #2 with recreator testing.  The issue was that lines with error now store the text of the line, which is recreated.  This change was made in the last commit but the expect results were not updated.

[commit 727f7d223f]

Build Problems on Windows 8.1 – Part 1

Several problems were discovered when attempting to build and run on Windows 8.1 using Qt Creator.  The first problem that occurred was with the copying of the test files from the source directory to the build (binary) directory.  This is accomplished by creating custom commands for each test file.  These custom commands generate a mkdir -p command to make the test directory if not already there and the cp command to copy the file.  Both of these commands are available on Linux and with MSYS on Windows.

When building on the command line on Windows using MSYS to create the make file or using the CMake GUI to generator the make file, the "MSYS Makefiles" generator is used.  This creates the make file assuming that the make command will be run using a Unix shell (specifically bash or the Bourne Again SHell).

On Windows when running CMake from Qt Creator, it uses the "MinGW Makefiles" generator to generator the make file.  This generator assumes that the make command is going to be run using cmd.exe, the Windows command prompt program, which Qt Creator uses cmd.exe to run the make command.  The make command uses the program under which it is run to execute commands in the make file like for custom commands.

The CMake build file needed to be modified to detect when the MinGW generator was being used and use the appropriate make directory and copy commands for cmd.exe.  Fortunately, the CMake CMAKE_GENERATOR variable is set to the generator is being used.  So, if the generator is MinGW Makefiles, the md and copy commands are used, otherwise the mkdir -p and cp and used.

Another problem with the md command is that if the directory already exists, the command returns an error and the make command aborts.  With Linux/MSYS, the -p option solves this problem.  With cmd.exe, the md command has no similar option.  The only solution that worked was using an if not exist command (so the md command is only performed when the test directory doesn't exist):
if not exist test-directory md test-directory
When the generator is not MinGW, CMake adds double quotes about each component of an individual command in the custom command if the component has a space in it, because otherwise the shell parse a path with a space incorrectly as two arguments and not one.  For this reason, the command variable could not be set to mkdir -p because it would create the command "mkdir -p" and this is not a valid command.  Therefore, the mkdir and -p had to be put into two separate variables.  This does not occur with the MinGW generator.

Installing QtCreator on Windows 8.1

Downloading Qt Creator was described a few posts ago.  The latest version of Qt Creator at the time is 3.1.2.  Open the qt‑creator‑opensource‑windows‑x86‑3.1.2.exe download file and click through, accepting the recommended Installation Folder, the license, and Start Menu shortcuts.  Accept the User Access Control if asked.  At the end go ahead and Launch Qt Creator as some post install configuration is needed.

Qt Creator should be able to find the various utilities and Qt 4.8.6 since these are on the path, but it does not appear to actually look for some of these, so these need to be configured manually.  In Qt Creator selected Tools and Options...  Click the Build & Run page.

On the Qt Versions tab, if Qt 4.8.6 was not auto-detected, it needs to be set manually.  Click the Add button and browse to the C:\Qt\4.8.6\bin directory and select the qmake.exe (just qmake if extensions are not displayed).  Click the Apply button.

On the Compilers tab, if MinGW was not auto-detected, it needs to be set manually.   as MinGW (x86 32bit in C:\mingw32\bin).  Click the Add button and select MinGW (from the drop down menu).  On the Compiler path field, click the Browse button and navigate to the C:\mingw32\bin directory and select g++.  Click the Apply button.

On the Debuggers tab, it GDB was not auto-detected, it needs to be set manually.  Click the Add and replace the default New Debugger name with something like GDB 7.7 (which is the version installed with MinGW).  Click the Browse... button and browse to This PC, Local Disk (C:) (or whatever it may be called), mingw32, bin, gdb and click the Open button.  Click the Apply button.

On the CMake tab, click the Browse... button and navigate to This PC, Local Disk (C:), Program Files (x86), Cmake 2.8, bin, cmake and click the Open button.  Click the Apply button.

Finally on the Kits tab, select the Desktop (default) under manual, which is italicized with a yellow triangle warning indicating it is not configured properly.  Under the Debugger field, select GDB 7.7 on the drop down (only debugger listed).  The warning should go away since the Compiler and Qt Version fields are already set.  Click the OK button.

The configuration can be testing by compiling one of the Examples.  On the Welcome page (icon on upper-left), select Examples.  I selected the first example: 2D Painting Example.  On the Configure Project page that appears next, click the Configure Project button.  Test building by clicking the Hammer icon (lower-left or type Ctrl+B).  After a few moments it should finish.  Click the Run/Play icon (or type Ctrl+R) to run the program.  It should present two rotating patterns.  Qt and Qt Creator have been successfully installed.

Installing Git on Windows 8.1

Finally, to obtain this project from this project's Git repository on GitHub, the Git utility is required.  The latest Git for Windows can be obtained from their home page (which has changed since I previously described how to install Git).  Click the Download button to download the latest version.

Open the download file (Git‑1.9.4‑preview20140611) and accept the User Control Access if asked.  Click through and accept the license.  On the Select Components dialog, select additional icons if desired and click through accepting the defaults.  It's not necessary to view the release notes, so disable and click Finish.

The paths to these programs along with Qt needs to be added to Windows.  From Windows Explorer, select This PC, right-click and select Properties, Advanced system settings, and on the Advanced tab, click Environment Variables...  Under System variables, scroll down to Path and add:
;C:\mingw32\bin;C:\Qt\4.8.6\bin;C:\Program Files (x86)\Git\bin
Select Path, click Edit..., add the string above to the end of Variable value, and click OK (the whole way out).  This will allow the utilities git, g++ (compiler), gdb (debugger) and qmake (how it is determined where Qt is installed) to be found on the command line or by a program that is started under Windows.  The path to the CMake directory should already have been added during CMake installation.

Installing CMake on Windows 8.1

Building project requires the CMake utility.  CMake can be obtained from the CMake download page.  For now I suggest installing the latest 2.8 version, which is 2.8.12.2.  There is a newer version (3.0.1 at this time), but Linux only has 2.8.7 (Linux Mint 13/Ubuntu 12.04) or 2.8.12.2 (with kubuntu backports or Linux Mint 17/Ubuntu 14.04).  This project only requires 2.8, so 2.8.12.2 for Windows is sufficient.

Open the cmake‑2.8.12.2‑win32‑x86.exe download file and accept the User Control Access if asked.  Click through and accept the license.  On the Install Options dialog, select the Add CMake to the system PATH for all users or current user as desired, and the Create CMake Desktop Icon if desired.   Continue clicking through and finally click Finish.

Installing Qt For MinGW on Windows 8.1

Download the latest Qt4 for Windows with MinGW at the Qt Project's download page.  Upon first opening this page, only the latest Qt5 version is shown (which at this time is 5.3.1).  To see more versions, click the Show Downloads button and scroll down to Qt 4.8 (4.8.6 at this moment).  Download the one for MinGW with GCC 4.8.2 (note that this download does not actually include MinGW).  While here, the latest version of Qt Creator can also be downloaded (3.1.2 at this moment).

For now, I plan to continue using Qt4 even though Qt5 has been out for a while.  I will describe installing the current latest Qt4.  The version present on Linux Mint 13 (Ubuntu 12.04) is 4.8.1 unless the kubuntu backports are being used to get the latest version of KDE, in which case 4.8.2 is present.  Linux Mint 17 (Ubuntu 14.04) has version 4.8.5.  These versions are sufficient so there is no need to install 4.8.6 for Linux.  (I don't know enough about Qt5, which is something that may be revisited some time in the future.)

Now Qt4 can be installed.  Open the qt-opensource-windows-x86-mingw482-4.8.6-1.exe download file and accept the User Account Control if asked.  Click though, accept license, continue clicking through, accept the default C:\Qt\4.8.6 installation location, and continue clicking through.  It will ask where the MinGW installation is located.  If MinGW was installed as previously described, the default C:\mingw32 can be accepted, so click Install.

At the end of the install process, the setup program will ask to Run Examples and Demos and Show Documentation.  This is not necessary, so disable these before clicking Finish.  These can be accessed by the Start Menu (Classic Shell) by Programs, Qt by Digia v4.8.6 (MinGW 4.8.2 OpenSource), Examples and Demos and Assistant.  From the Start Screen on Windows 8 I'm guessing the install throws all the programs on the Start Screen itself.  However, on Windows 8.1, click the Down Arrow (lower left where it says new apps installed), and scroll right to where the Qt by Digia v4.8.6 (MinGW 4.8.2 OpenS... programs are listed where Assistant and Examples and Demos will be.  These can be pinned to the Start Screen if desired (Pin to Start on right-click menu).

Installing MinGW on Windows 8.1

The procedure for installing MinGW (that came with GCC 4.6.2 at the time) was previously described.  Later once Qt was being utilized, the procedure for installing the QtSDK with MinGW (that came with the older GCC 4.4) was given, and then it was detailed how to connect the MinGW with GCC 4.6.2 to QtCreator since 4.4 was too old.

The Qt Project team no longer provides the QtSDK (that included the Qt library development files, MinGW and QtCreator).  Therefore, the Qt4 libraries and QtCreator now need to be installed separately.  The latest Qt4 (4.8.6) for MinGW does not actually come with MinGW, which needs to be install separately beforehand.  They suggest installing MinGW-w64 first (which includes GCC 4.8.2) and give a specific version to install.

MinGW-w64 can be downloaded from here.  This is revision 3 and there is now a revision 4 (they don't detail very well what was changed in revision 4), but I installed the recommended revision 3.  The download file is in 7-Zip format, which Windows 8.1 does not have native support for.   To install 7-Zip, go to the 7-Zip website and click the Download .msi (for 64-bit Windows 8 or Download .exe if using 32-bit Windows 8).  Once downloaded open the program.  (Don't install the 7z922.exe program, the default program on their SourceForge page, as it doesn't connect into Windows Explorer.)

MinGW can now be installed by extracting the i686-4.8.2-release-posix-dwarf-rt_v3-rev3.7z download file.  Right-click on this file, select 7-Zip, and Extract files...  Set the Extract to directory to C:\ and click OK.  This will install MinGW at C:\mingw32, which is the location Qt expects to find it.  Next post will describe installing Qt.