How to easily* compile KDE4 from sources (* if you're a guru like me)

I found that on my system, using a compiled KDE 4 from the sources goes much better than Mandriva's provided packages. And since it's pretty easy to do, I'm writing an article detailing the steps. Back in 2004 I wrote an article about running a KDE 3 version compiled from CVS (in spanish that time). Since your distribution probably already have packages for KDE4 you could be wondering what are the advantages of compiling KDE4 over using the distro packages. These are:

  • You can optimize the compilation for your target machine. This means that if you, for example, have a Dual Core 2 CPU you can instruct the GCC compiler to generate code specific to the features of your processor (like SSE and a lot of other things) that will make the binary run noticeably faster on your computer than the generic code compiled for Pentium.
  • You'll always be on the cutting edge, having the very last KDE version. Of course this is both good and bad; it means you'll have always the latest features but you'll have too the latest bugs. My advice is to only install form sources when the current KDE version (4.1.0, 4.2.0, etc) is at least at beta stage.
  • If you're a programmer and find a bug or want to implement some feature, you'll have the source code to it.
  • You'll not depend on the options choosen by your distribution package maintainer.

The steps involving in compiling KDE4 from sources are:

  1. Download, install and configure kdesvn-build.
  2. Install some optional dependencies in the form of your distribution development packages to enable more features once KDE is compiled.
  3. Run kdesvn-build to compile the selected modules with the selected options.
  4. Check the modules logs to see if there are optional features being left out because of missing development packages.
  5. Change your .bashrc to source a file containing environment variables pointing to your custom Qt4 and KDE4.
  6. Create a desktop file for your desktop manager to add a session enabling you to choose your custom KDE from the graphical login.
  7. Reboot, test, and enjoy.

The first step is to download kdesvn-build . This is a script created by people at the KDE project that will take care of most of the complications involving the source download, compilation and installation from sources. It's very much alike what konstruct was for KDE3, but better. Once you've downloaded the kdesvn-build tar gz package, uncompress it and put the kdesvn-build script on some place on your path (usually /usr/local/bin or /usr/bin). Then, copy the "kdesvn-buildrc-sample" file to ~/.kdesvn-buildrc and open it with an editor. This is the configuration file that controls how KDE will be downloaded, compiled and installed and among other things it controls what KDE modules will be build. Of the default values in the file I recommend you to change:

source-dir ~/kdesvn
Specifies the directory where the KDE sources will be downloaded. Just uncomment it (removing the '#' at the start of the line) until you want to change it.
build-dir build
Same. Just uncomment it
kdedir ~/kde4
This (once uncommented) specified where you KDE will be installed. I recommend using a directory on your home directory (I'll show you later how to make the system find the binaries and libraries on it) because if later you want to completely remove any trace of your compiled KDE you can just delete it, but if you want to install it like the packages on your distribution would, you can put /usr here.
qtdir ~/qt4
Same as with kdedir but for the Qt libraries. Again, I recommend using a local directory instead of making the installation using /usr as root.
configure-flags --enable-debug
If you want to speed a little your KDE, you can comment this line, which cames uncommented by default. As you can guess, its activation enables the including of debug symbols and code in the generated binaries, that could make the code more easy to debug.
cmake-options -DCMAKE_BUILD_TYPE=none
The default value of this option is "RelWithDebInfo", but if you want to optimize your KDE using some custom compilation options later (and you want it) you have to change the value "RelWithDebInfo" to "none".
cxxflags -pipe -march=i686
These are the default values for the GCC compiler. Here is where we can change some things to optimize a lot more. To find the best options for your CPU type you can use this script. On my system, which is an Intel Core 2 Duo, the options I'm using are:
cxxflags -pipe -march=core2 -ffast-math -mfpmath=sse -fomit-frame-pointer -O3 If your system is not an Intel Core 2 Duo you can check on GCC manual what -march options you've to use for you CPU. Please note that if you have a GCC version 4.3 or newer you can change -march=core2 for -march=native which will automatically select the best -march option for you current CPU.

After these .kdesvn-buildrc global options comes the section where you select what modules you want built. These always start with "module modulename" followed (sometimes) by some (usually commented) build options and an "end module" clause. What modules do you want is up to you; you should at least select qt-copy, kdesupport, kdelibs, kdepimlibs and kdebase. I've also selected kdemultimedia, kdenetwork, kdepim, kdegraphics, kdeplasma-addons, extragear/libs, and extragear/multimedia. Please note that if you comment/uncomment some "module modulename" command you'll need to take care to also comment/uncomment the closing "end module". A double check once you've finished editing the file and selecting the modules will not harm.

The next step is to install lots of libraries (including the development header files) on your system using you distribution package installer. This will enable more features once KDE compiles. The package names will change a little from distribution to distribution but you can use as reference a list of what development packages I've installed on my system that you can see clicking on this link.

Now is the time to download the KDE4 sources and compile everything. Depending on your CPU, network connection and hard disk it will take from little more than one hour to (more realistically) a few hours and the system will be very slow during this time so you better don't plan to do anything important for a while with it. The only thing you've to do on this step is to run on the console:

kdesvn-build

It will start to output text informing you of the progress downloading, patching, building and installing every module, including a nifty % (except for qt-copy). If you want even more information on what is being done you can check the logs for every step (for example using tail -f) in ~/kdesvn/logs/latest/modulename (change modulename for the module currently being compiled and kdesvn for the directory you choosed to download the sources.) If some module build fails, a file fail.log will also be created on the module log dir where you will be able to check what failed (usually a missing development library on your system.)

Even if everything compiled sucessfully, you're encouraged to check if the build system is not disabling some optional feature because of some missing library on your system. For this you'll make a tour of the logs directory (~/kdesvn/logs/latest/modulename) and search for "Could not find" in the cmake.log file, which you can easily do with:

grep -i "Could not find" cmake.log

You'll see some lines like "cmake.log:-- Could NOT find Sane (missing: SANE_INCLUDE_DIR SANE_LIBRARY" which are telling you about some missing dependency you could install to enable some feature (in this case, the SANE scanner package.) Now you usually will install the missing dependencies (you'll probably have to search a little on your package repositories) including the development packages (libname-dev or libname-devel name on most distributions) and install them; once you've done this, you can run kdesvn-build again to enable these optional features this time.

If everything went well you'll have a compiled and optimized KDE4 on your ~/kde4 directory (or the one you choosed configuring the .kdesvn-builrc file.) What we've to do now is to enable your system to find and use that KDE4 before any other that you could have installed (from your distribution packages, usually.) For this, you'll create a new file with the following content, changuing the directory ~/qt4 and ~/kde4 to the ones you choosed for Qt and KDE:

            ###################### SCRIPT START ##########################################
            prepend() { [ -d "$2" ] && eval $1=\"$2\$\{$1:+':'\$$1\}\" && export $1 ; }
            # Qt
            # only set Qt related variables if you compiled Qt on your own
            # (which is discouraged). if you use the distro provided Qt, skip
            # this section. Comment it if necessary.
            export QTDIR=$HOME/qt4
            prepend PATH $QTDIR/bin
            prepend LD_LIBRARY_PATH $QTDIR/lib
            prepend PKG_CONFIG_PATH $QTDIR/lib/pkgconfig
            
            
            # KDE
            export KDEDIR=$HOME/kde4
            export KDEHOME=$HOME/.kde4
            export KDETMP=/tmp/$USER-kde4
            mkdir -p $KDETMP
            export KDEDIRS=$KDEDIR
            prepend PATH $KDEDIR/bin
            prepend LD_LIBRARY_PATH $KDEDIR/lib
            prepend PKG_CONFIG_PATH $KDEDIR/lib/pkgconfig
            prepend QT_PLUGIN_PATH $KDEDIR/lib/kde4/plugins
            
            # DBus
            # only set DBUS related variables if you compiled dbus on your own
            # (which is really discouraged). if you use the distro provided dbus,
            # skip this variable. Uncomment it if necessary.
            #export DBUSDIR=$KDEDIR
            #prepend PKG_CONFIG_PATH $DBUSDIR/lib/pkgconfig
            
            # only needed on some strange systems for compiling Qt. do not set
            # it unless you have to.
            #export YACC='byacc -d'
            
            # XDG
            unset XDG_DATA_DIRS # to avoid seeing kde3 files from /usr
            unset XDG_CONFIG_DIRS
            
            # make the debug output prettier
            export KDE_COLOR_DEBUG=1
            export QTEST_COLORED=1
            ################################ SCRIPT END ######################################
            

I've called this file ~/.bashrc_kde4. Now edit your ~/.bashrc file to make it use the new file, just add to the end of it:

. $HOME/.bashrc_kde4

Once this is done your future sessions will also find your new Qt and KDE before other options but if you use a graphical login -like most distributions nowadays do by default- you'll have to create a file to add a new session type using the new KDE as desktop. In the case of the KDM session manager (as used in Mandriva, SuSE and others) you have to create a file in the sessions directory of your login manager. In my case I use Mandriva's default (KDM) and the file was to be added to /usr/share/apps/kdm/sessions. I called it "09KDE4SVN.desktop" with this content:

               [Desktop Entry]
               Encoding=UTF-8
               Name=KDE4SVN
               Comment=KDE4SVN
               TryExec=/home/juanjux/kde4/bin/startkde
               Exec=KDE4SVN
               Icon=
               Type=Application
               

(Change /home/juanjux/kde4/bin/startkde to the path to the startkde of your compile KDE.)

Once you've done this (or the variant for your distribution), a new session called "KDE4SVN" will show in the list of sessions of your graphical login manager and choosing it should boot your new compiled, shiny and optimized KDE.

Problems?

If at some point some module that formerly compiled start to fail compilation, my advice is to delete your ~/kdesvn directory and run kdesvn-build again letting it download again and compile the sources from the start instead of updating it using subversion. This has worked for me sometimes.
tags: [ articles · software libre · linux · kde ]
comments powered by Disqus