Guidelines and HOWTOs/Build from source/Windows: Difference between revisions

From KDE Community Wiki
(87 intermediate revisions by 21 users not shown)
Line 1: Line 1:
''emerge'' is a tool to build the KDE sources and its third-party requirements on MS Windows. It is the '''easy''' way to build KDE on MS Windows.
''Craft'' is a tool to build the sources and its third-party requirements. It is the '''easy''' way to build software.


== Introduction ==
== Setting up craft ==
Emerge is a tool that can build the different parts of KDE and its dependencies under Windows. We created this tool to automate and simplify the build process under Windows. We try to build all packages that we offer in the KDE installer with emerge. That has some advantages for us:
=== Installing python ===
=== Easy for people to join us ===
While Craft is capable of installing an embedded Python version sufficient for most packages, we recommend a full installation of Python. The current version is 3.7. [https://www.python.org/downloads/ Download Python 3.6+]
Before emerge it was quite some work to set a system up for development. There were some quirks, which were documented in some mailing lists, but you had to remember them or you ran into an already solved problem again, etc.
Now to get a development machine you need a Windows computer, need to install Python and Git and do the emerge checkout. Then execute emerge to build what you want to build. This is easy for developers coming from Windows to KDE, and also for KDE developers coming to Windows.
=== Easy for us to do build ===
With emerge you can build the whole software stack (low-level libs, Qt, kdelibs, things above that) with only one command. You can start that build, and some hours later you can check if it worked, or if something broke. So we can spot problems easier and earlier. We can also start with a "naked" Windows computer without any other installed software and bootstrap kde on it. That ensures, that no hidden dependencies on some pieces of software sneak in, because then the builds on a "naked" computer would break and show the problem.
=== Easy to collaborate ===
We can test the same emerge build description for a package on different Windows versions/computers before we do binary releases. People can also add build descriptions for new packages to the Subversion repository.


This emerge tool was inspired by the Gentoo emerge tool.
=== Setting up a powershell ===
Powershell is required to be at least version 5.0.
If you're running Windows 10 you most probably won't need to update Powershell.


== Set up the environment ==
You can check the version by invocating <tt>$PSVersionTable.PSVersion</tt> in a Powershell terminal.
=== Root directory ===
Create a directory if possible in your harddrive's root e.g. C:\kderoot or D:\kderoot (You will need this PATH later). This directory will contain the whole kde installation later. We will refer to it as KDEROOT.


=== Python interpreter ===
In case you need a newer one, we recommend
<tt>emerge.bat</tt> invokes an <tt>emerge.py</tt> script written in [http://en.wikipedia.org/wiki/Python_%28programming_language%29 Python] programming language, so you first need to [https://www.python.org/downloads/ install the Python 3.4 Interpreter]. The ''python'' installation directory will be added to the PATH later by <tt>KDEROOT\etc\kdesettings.ini</tt>.
[https://docs.microsoft.com/en-us/powershell/scripting/install/installing-powershell-core-on-windows?view=powershell-6 Powershell Core].


=== Git client ===
The latest version of the standard powershell can be found [https://www.microsoft.com/en-us/download/details.aspx?id=50395 here].
The latest source code for Windows ''emerge'' and a lot of the rest of KDE is stored in a repository created and managed using the [http://git-scm.com/ Git] version control tool. You need a git client for the first checkout of emerge. There are at least two applications:
*a command line client, available at [https://git-scm.com/download/win git-scm.com], aimed at developers or power users accustomed with the command line,
*a GUI program like [https://tortoisegit.org/ TortoiseGit], optional, useful for displaying differences between various versions of files in the repository in a graphical way.


Emerge uses its own git client for checking out all KDE source code. You can find it in KDEROOT\dev-utils\git\bin. It will be used by emerge even if you have another git executable installed.
=== Setting up a compiler ===
Currently, Craft supports both the MinGW and Microsoft Visual Studio (msvc) compiler.
While MinGW can be installed by Craft, [https://www.visualstudio.com/ Visual Studio] must be installed independently by the user.


=== Getting the ''emerge'' tool ===
=== Direct X SDK (only for MinGW setups) ===
The source code of the ''emerge'' tool and the recipes for creating KDE packages are located at <tt>git://anongit.kde.org/emerge.git</tt>, which is an URL based on the git-specific ''git'' protocol.
If you are using the MinGW compiler, in order to compile the Qt5 ''qtbase'' package you will also need to install the [https://www.microsoft.com/en-us/download/details.aspx?id=6812 Microsoft DirectX SDK]. Make sure to open a new command line window after the installation for the environment variables to be set.


You need to clone the source code from the ''emerge'' git repository into a new directory below your root directory (the root directory is normally called %KDEROOT% here) or get it inside a self extracting archive (not tested yet).
=== (Optional) Enable development mode ===
Windows 10 supports the creation of symbolic links, but only if the development mode is activated.
Allowing symbolic links allows faster (much faster) extraction of archives.
[https://docs.microsoft.com/en-us/windows/uwp/get-started/enable-your-device-for-development Enable development mode]


==== Check out using the 'git' command ====
=== Installing Craft===
# [https://docs.microsoft.com/en-us/powershell/scripting/setup/starting-windows-powershell?view=powershell-6 Start a PowerShell environment] (Do not use PowerShell ISE, it doesn't work very well with python). It might be required to run it [https://docs.microsoft.com/en-us/powershell/scripting/setup/starting-windows-powershell?view=powershell-6#with-administrative-privileges-run-as-administrator as administrator].
# Allow execution of powershell scripts by executing the following command:
#:<pre>Set-ExecutionPolicy -Scope CurrentUser RemoteSigned</pre>
# Install Craft and follow the instructions by executing the following command:
#: <pre>iex ((new-object net.webclient).DownloadString('https://raw.githubusercontent.com/KDE/craft/master/setup/install_craft.ps1'))</pre>
# Consider adding exception for the entire craft root directory to your antivirus' exceptions, otherwise build of some packages can fail (example: build of <code>kde/frameworks/tier2/kdoctools</code> when AVG is used).


With the ''git'' command line tool, you can accomplish this with the following commands:
:: <pre>cd KDEROOT</pre>
*if you will only use anonymous (read-only) access to the KDE git repository:<pre>git clone git://anongit.kde.org/emerge.git</pre>
*if the git and ssh ports are blocked, and only http is allowed, it is better to clone over the http port for getting (read-only) access to the KDE git repository:<pre>git clone http://anongit.kde.org/emerge.git</pre>
*or, if you plan to use write access (commit) to the emerge repository using your existing account & OpenSSH private key: <pre>git clone [email protected]:emerge.git</pre>


=== Configure the ''emerge'' tool ===
Note: For Microsoft Visual Studio compiler, it's necessary to have VCTOOLSREDISTDIR environment variable pointing to:
# Create the directory <tt>KDEROOT\etc</tt>.
"C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Redist\MSVC\xx.xx.xxxxx" for MSVS 2017 and  
# Copy the file <tt>KDEROOT\emerge\kdesettings.ini</tt> as <tt>KDEROOT\etc\kdesettings.ini</tt> and change its contents according to your needs.
VCINSTALLDIR environment pointing to "C:\Program Files (x86)\Microsoft Visual Studio\2015\Community\VC\" for MSVS 2015.


Note 0: '''Read the comments in that file very carefully'''
=== Using the stock Qt SDK===
This will skip all Qt packages and use the official Qt builds instead.
It will work fine for most CMake based recipes but definitely cause problems with QMake based projects.
You will of course also miss all patches we usually apply to Qt.
This is only recommended when you know what you are doing and you won't get support for in our channel.


Note 1: '''Be sure that you neither have the msys/bin nor the cygwin/bin in your path. If so you have to definitely remove it from the path.'''
To activate the SDK mode adapt the [QtSDK] section in your etc/kdesettings.ini to something like:
    [QtSDK]
    ## Whether to use prebuild Qt binaries.
    Enabled = True
    ## The path to the Qt sdk.
    Path = C:\Qt
    ## The version of Qt.
    Version = 5.9
    ## The compiler version, if you are not sure what to use, have a look at the directory set in QtSDK/Path.
    ## The compiler must be of the same type as General/KDECOMPILER.
    ## If you are using mingw please make sure you have installed the mingw using the Qt installer.
    Compiler = msvc2017_64


Note 2 '''from a user: The applications gimp, inkscape and graphviz are also a problem. To make sure that there's nothing wrong I stripped my path to contain only what I needed to build.'''
== Using Craft ==
To use Craft you need to start a Powershell window, point that to <tt>KDEROOT\craft </tt> and run the initalization script. For example:


emerge has several branches which contain specific package versions. E.g. if you want to build the 4.13 branch of KDE (or one of the 4.13 releases) you should checkout the ''kde-4.13'' branch of emerge; in the ''master'' branch, you can find the KDE Frameweorks 5. At the moment, only the master branch is actively maintained, so you will likely experience problems (outdated package urls etc.) on the other branches.
C:\CraftRoot\craft\craftenv.ps1


To view all branches, use the following command:
This tells Craft about your environment settings (e.g. paths). It will load your configuration from <tt>CraftRoot\etc\CraftSettings.ini</tt>. If there are any error messages Craft will not work as expected. The output should look similar to this one (of course with your paths):
<pre>git branch -a</pre>
To change the branch of emerge, do the following:
<pre>cd emerge && git checkout kde-4.13</pre>


=== [Optional, Advanced] emerge-boost-config.jam ===
PS C:\CraftRoot\craft>.\craftenv.ps1
Add a file emerge-boost-config.jam in the <tt>%KDEROOT%\etc</tt> directory to build boost in a specific way. The only current use case is for the following problem:
Craft              : C:\CraftRoot
If you build 32bit binaries with emerge on a 64bit system you need to link boost-python against a 32bit python library. In case your standard python installation is 64bit though, you can specify the python installation in the following way:
Version            : master
<syntaxhighlight lang="text">
ABI                : windows-msvc2017_64-cl
# ---------------------
Download directory  : C:\CraftRoot\download
# Python configuration.
# ---------------------


# Configure specific Python version.
Then [https://community.kde.org/Craft start crafting]
using python : 3.2 : C:\\Python32_x86 ;
</syntaxhighlight>
This way boost-python would be using the headers & libraries from C:\\Python32_x86 instead of the default one.
Please remember that for mingw compilers you must regenerate the import library for the python dll (also for the mingw 64bit compiler):
<syntaxhighlight lang="text">
R:\> emerge pexports
R:\> pexports C:\Python32_x86\python32.dll > C:\Python32_x86\libs\python32.def
</syntaxhighlight>
NOTE: In case you generate the import library for the 64bit compiler, add the following line to the file python32.def:
<syntaxhighlight lang="text">
Py_InitModule4 = Py_InitModule4_64
</syntaxhighlight>
For both compilers you should now run:
<syntaxhighlight lang="text">
dlltool -d C:\Python32_x86\libs\python32.def -l C:\Python32_x86\libs\libpython32.dll.a
</syntaxhighlight>
Now you should be able to do emerge -i boost-python-src without errors.
 
== Using emerge ==
 
To use emerge you need to start a Powershell window and point that to <tt>KDEROOT\emerge</tt>. For example:
 
C:
cd KDEROOT\emerge
 
Then in the windows powershell you have to load the script with
 
". .\kdeenv.ps1"
 
This tells emerge about your environment settings (e.g. paths). It will load your configuration from <tt>KDEROOT\etc\kdesettings.ini</tt>. It should not give any error messages, otherwise emerge will not work as expected. The output should look similar to this one (of course with your paths):
 
PS C:\kderoot\emerge>.\kdeenv.ps1
KDEROOT    : r:\
KDECOMPILER : mingw-w64
KDESVNDIR  : s:\
KDEGITDIR  : q:\
DOWNLOADDIR : t:\
PYTHONPATH  : C:\python34
PS C:\kderoot\emerge>
 
''Note: There is a short path option in kdesettings which you will need if you want to build Qt in a directory that has a pathlength of more then around 5 characters. This problem is due to limitations of the command line length and the Qt build system.''
 
Next, if you have configured your kdesettings.ini to use svn+ssh for your subversion checkout, then you need to run:
 
<syntaxhighlight lang="text">plink <your-svn-username>@svn.kde.org
plink <your-kde-username>@git.kde.org</syntaxhighlight>
 
This will prompt you to accept the fingerprint of the server, otherwise svn will hang forever when trying to download from the server.
 
Now you should be able to use emerge. To get some help on usage:
 
emerge --help
 
To get a list of available packages:
emerge --print-installable
 
To get a list of currently installed packages:
emerge --print-installed
 
== Setting up a compiler ==
Currently emerge supports both the MinGW and MS Visual C++ (msvc) compilers. We did not add dependencies for the compilers, so you have to make sure to install a compiler by yourself. There are three ways to set up a compiler for emerge.
We assumed you have set KDECOMPILER variable properly in the <tt>KDEROOT\etc\kdesettings.ini</tt>.
 
In the following sections you can find information on how to install or reuse an existing compiler.
 
=== Install the MinGW compiler with emerge ===
Let emerge install the MinGW compiler, as soon as emerge needs MinGW it will automatically fetch the correct version for you.
 
=== Install MS Visual C++ ===
Read [[../MS_Visual_Studio#The_Compiler|here]].
 
=== Point to an existing MS Visual C++ installation ===
You need to point emerge to an existing msvc installation. This is run automatically for you from kdeenv.bat if configured properly in kdesettings.ini. Check your kdesettings.ini file to know where to set it.
 
== Installing the base system ==
Once you have emerge and a compiler installed and working, try:
 
You are now ready to start building KDE, it is recommended to do so progressively, relying on emerge to automatically resolve the required dependencies at each set step:
 
* Enter <tt>emerge qt</tt>.  This will fetch and install Windows versions of numerous UNIX-like utilities and libraries, then checkout, compile and install Qt.  This will take several hours.
* Enter <tt>emerge frameworks</tt>.  This will checkout, compile and install the kde frameworks 5 modules.
 
You will now have successfully installed a base KDE system and can now install other KDE modules as required.
 
Note that this will install the development version of KDE (master in most git repositories), if you wish to install a particular stable branch then you should change the branch of emerge to the specific branch of that release. '''NOTE:''' You should not mix kde packages from different branches.
 
It is strongly recommended you do not choose to manually install any of the utilities and libraries yourself, as you may install the wrong version and cause installation failures.  Instead allow emerge to resolve the dependencies for you.
 
Every time you want to update or install a package, you should first update your emerge checkout (simply run emerge --update emerge) to ensure you are using the latest package recipies.
 
== What emerge does ==
Emerge can be thought of as performing many of the functions of automated tools like cmake, but in a flexible Python scripting framework.  The benefit of this is that new libraries with idiosyncratic installation procedures, conflicting library and header installation names, and complex rules for building on different setups can be generated automatically, and all directory management should be taken care of without the user's input. 
 
The primary logic for the program is contained in the /bin folder of the Git repository.  The script emerge.py serves as the entry point to the system; it runs appropriate code according to the command line arguments.  The basic command '''emerge ''packageName'' ''' performs the five most important actions <tt>--fetch</tt>,  <tt>--unpack</tt>, <tt>--compile</tt>,  <tt>--install</tt>, and <tt>--qmerge</tt>.  The definition for each of these steps is defined using a flexible system called Portage, after the Gentoo package management system. The basic goals are:
 
1. Fetch action retrieves either a binary or the source code for the package. 
 
2. Unpack action installs the source code in a source folder and applies KDE-specific patches.
 
3. Compile action runs package-dependent configure make steps.
 
4. Install action installs the headers and compiled library and executable outputs. 
 
5. Qmerge action does something, but what?
 
 
Emerge also offers functionality to document dependency trees, create patches to upload tweaks and fixes, and update and clean existing installs.
 
The actual commands for fetching, unpacking etc. are defined by three increasingly specialized levels of logic.  The first level is the code in the /bin folder and determines the overall order, steps should be taken, reading environment variables to configure the build environment and compiler set by <tt>kdeenv.bat</tt>, and parsing the directory tree. 
 
The second set of logic is found in the /bin/BuildSystem, /bin/Package, /bin/Packager, and /bin/Source folders.  This is used to determine general procedures for different classes of packages.  For example, the "Source" folder contains the logic for running the <tt>--fetch step</tt> for compressed files, git repositories, SVN, and so on.  The "Package" system contains logic for libraries that need to be configured with e.g. CMake, QMake, or internal make systems. 
 
The final set of logic is at the per-package level.  This is what is contained in the /portage/ directory.  Emerge is able to automatically search through the Portage folders to find the name of the package you specify.  This is where dependencies, special build configurations and special commands are set up.  Individual patch files and different version configuration information is also stored here.  It is relatively straightforward to add a new package to Portage, especially if the package itself can be downloaded and installed with CMake using minimal configuration. 
 
A good way to prepare a package for wider distribution is to create a simple <tt>CMakeLists.txt</tt> it.  You can format the addition of this file as a patch, and create a Portage script which merges the patch into the public code repository.
 
== ''emerge'' command line options and settings ==
There are some options that can be used when building with ''emerge''.
 
{| border="0" cellpadding="2"
!Command line switch
!Command line argument
!
!Description
|-valign="top"
|<tt>-v</tt>
|<tt>EMERGE_VERBOSE</tt>
|width="5%"|
|This option sets the verbosity level. Currently the highest verbosity level is 3 (<tt>-v -v -v</tt>). A verbosity level of 0 should give no output and equals to <tt>-q</tt>. You can set <tt>EMERGE_VERBOSE=3</tt> instead in the environment of the commandline or within your <tt>kdesettings.ini</tt> file.
|-valign="top"
|<tt>--offline</tt>
|
|
|This option suppresses the update step of the local tree - which needs some time. Be aware though that you have to have existing sources already if you want to use this option.
|-valign="top"
|<tt>-t</tt>
|<tt>EMERGE_BUILDTESTS</tt>
|
|This option enables or disables KDE4 buildtests for KDE modules. Other packages will not change. Use <tt>EMERGE_BUILDTESTS=True</tt> or <tt>=False</tt>.
|-valign="top"
|<tt>--print-targets</tt>
|
|
|This option will display all "targets" a certain package has. Normally targets are fixed releases or different branches. They are defined in the portage file.
|-valign="top"
|<tt>--target=TARGET</tt>
|
|
|This sets a specific target for this package. If not added, the default target is used, which can be checked by looking at the output of '''--print-targets'''.
|-valign="top"
|<tt>-i</tt>
|
|
|This option ignores that a package is already installed. It builds it completely new, but keeps the dependencies.
|-valign="top"
|<tt>--update</tt>
|
|
|This option ignores that a package is already installed but doesn't cleanup an already existing build directory. Thus you will only rebuild files that have changed since the last build.
|}
 
== Hints ==
=== Updating packages ===
*Once you have ''packagename'' built, type <syntaxhighlight lang="text">emerge --update packagename</syntaxhighlight> to update <tt>packagename</tt> from the Subversion and compile it without removing the build dir or <syntaxhighlight lang="text">emerge --update-all</syntaxhighlight> to update all packages that can be rebuild (they are rebuild with --update).
 
=== General setup ===
For Fine Tuning see here:
[[Projects/KDE_on_Windows/Installation#Fine-tuning|Fine-tuning]]
 
==Vista issues==
*[[User:Jstaniek|jstaniek]] 12:02, 15 January 2008 (CET): UAC has infamous heuristics that make programs like patch.exe treat as installers and try to run them with admin rights (!). This heuristics can be tricked by renaming patch.exe to something like pch.exe ([http://nevali.net/2007/01/update-workaround-for-the-cygwin-uac-problem/ example]) but we did not want to add item to our infrastructure. Instead it is possibleto turn off the heuristics (see the screenshot [http://theinvisiblethings.blogspot.com/2007/02/running-vista-every-day.html here in the security blog calling the heuristics 'severe hole in the design of UAC']). If you happen to disable the UAC, as many annoyed users and devs do (msvc demands admin rights anyway!), patch.exe should already work for you as in older Windows. Alternatively you may want to disable UAC [http://www.howtogeek.com/howto/windows-vista/disable-user-account-controluac-for-administrators-only/ for admins only], but this makes no sense if you are the only user of your machine and use only the admin account.
* [http://ben.versionzero.org/wiki/Fixing_the_way_Vista_Auto-detects_Installers This wiki page] lists instructions on how to use program manifest to disable privilege elevation for a single binary and makes patch play nice with UAC.  This [https://bugs.kde.org/show_bug.cgi?id=186712 should] eventually be integrated to emerge scripts.
 
[[Category:MS Windows]]

Revision as of 13:21, 23 December 2019

Craft is a tool to build the sources and its third-party requirements. It is the easy way to build software.

Setting up craft

Installing python

While Craft is capable of installing an embedded Python version sufficient for most packages, we recommend a full installation of Python. The current version is 3.7. Download Python 3.6+

Setting up a powershell

Powershell is required to be at least version 5.0. If you're running Windows 10 you most probably won't need to update Powershell.

You can check the version by invocating $PSVersionTable.PSVersion in a Powershell terminal.

In case you need a newer one, we recommend Powershell Core.

The latest version of the standard powershell can be found here.

Setting up a compiler

Currently, Craft supports both the MinGW and Microsoft Visual Studio (msvc) compiler. While MinGW can be installed by Craft, Visual Studio must be installed independently by the user.

Direct X SDK (only for MinGW setups)

If you are using the MinGW compiler, in order to compile the Qt5 qtbase package you will also need to install the Microsoft DirectX SDK. Make sure to open a new command line window after the installation for the environment variables to be set.

(Optional) Enable development mode

Windows 10 supports the creation of symbolic links, but only if the development mode is activated. Allowing symbolic links allows faster (much faster) extraction of archives. Enable development mode

Installing Craft

  1. Start a PowerShell environment (Do not use PowerShell ISE, it doesn't work very well with python). It might be required to run it as administrator.
  2. Allow execution of powershell scripts by executing the following command:
    Set-ExecutionPolicy -Scope CurrentUser RemoteSigned
  3. Install Craft and follow the instructions by executing the following command:
    iex ((new-object net.webclient).DownloadString('https://raw.githubusercontent.com/KDE/craft/master/setup/install_craft.ps1'))
  4. Consider adding exception for the entire craft root directory to your antivirus' exceptions, otherwise build of some packages can fail (example: build of kde/frameworks/tier2/kdoctools when AVG is used).


Note: For Microsoft Visual Studio compiler, it's necessary to have VCTOOLSREDISTDIR environment variable pointing to: "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Redist\MSVC\xx.xx.xxxxx" for MSVS 2017 and VCINSTALLDIR environment pointing to "C:\Program Files (x86)\Microsoft Visual Studio\2015\Community\VC\" for MSVS 2015.

Using the stock Qt SDK

This will skip all Qt packages and use the official Qt builds instead. It will work fine for most CMake based recipes but definitely cause problems with QMake based projects. You will of course also miss all patches we usually apply to Qt. This is only recommended when you know what you are doing and you won't get support for in our channel.

To activate the SDK mode adapt the [QtSDK] section in your etc/kdesettings.ini to something like:

   [QtSDK]
   ## Whether to use prebuild Qt binaries.
   Enabled = True
   ## The path to the Qt sdk.
   Path = C:\Qt
   ## The version of Qt.
   Version = 5.9
   ## The compiler version, if you are not sure what to use, have a look at the directory set in QtSDK/Path.
   ## The compiler must be of the same type as General/KDECOMPILER.
   ## If you are using mingw please make sure you have installed the mingw using the Qt installer.
   Compiler = msvc2017_64

Using Craft

To use Craft you need to start a Powershell window, point that to KDEROOT\craft and run the initalization script. For example:

C:\CraftRoot\craft\craftenv.ps1

This tells Craft about your environment settings (e.g. paths). It will load your configuration from CraftRoot\etc\CraftSettings.ini. If there are any error messages Craft will not work as expected. The output should look similar to this one (of course with your paths):

PS C:\CraftRoot\craft>.\craftenv.ps1
Craft               : C:\CraftRoot
Version             : master
ABI                 : windows-msvc2017_64-cl
Download directory  : C:\CraftRoot\download

Then start crafting