Development/Tutorials/CMake/KDE4: Difference between revisions

From KDE TechBase
m (add template for simplified chinese cmake)
No edit summary
Line 1: Line 1:
{{Template:I18n/Development/Tutorials/CMake}}
{{Template:I18n_Development_Tutorials_CMake}}


== Introduction ==
== Introduction ==

Revision as of 13:33, 4 April 2007

Template:I18n Development Tutorials CMake

Introduction

CMake reads script files and produces input files for the native buildsystem of the platform where it runs on. It can create GNU Makefiles, KDevelop project files, XCode project files, and Visual Studio project files.

CMake is free software and released under a BSD-style license. It is developed by Kitware Inc.

Here are some good places to learn about CMake in general:

Also, consider joining the CMake mailing list.

Why use CMake ?

CMake is the official tool of KDE 4 release, decided in March 2006, primarily due to technical merits as compared to the older KDE tools automake and unsermake:

  • CMake is developed independently of KDE, it can be used by other projects as well
  • Compilation time is much faster, mainly due to not using libtool anymore
  • The build files are easier to write

How to compile KDE using CMake

Get and install CMake

Linux, BSD, and other Unix Systems

Retrieve the latest stable version of CMake from [1].

Once downloaded, unpack and compile it: $ mkdir cmake-build $ cd cmake-build $ ../bootstrap $ make $ make install

By default, this will install CMake in /usr/local, so make sure to have /usr/local/bin in your execute path. To change the installation prefix (e.g. to /usr in debian), add the '--prefix=PATH' option to the bootstrap command.

Please follow the instructions located here if you would like to use the current development version.

Windows

Retrieve the latest stable version of CMake from [2].

Once downloaded, run the cmake installer.

By default, this will install CMake in C:\Program Files\CMake 2.4, so make sure to have <installpath>\bin in your execute path.

Please follow the instructions at here if you would like to use the current development version.

Mac OSX

Run CMake

Linux, BSD, and other Unix Systems

You have to run CMake so that it generates the build files for your system. Both in-source and out-of-source builds are supported by CMake, but currently in-source builds are prevented by the KDE implementation.

So, let's say you have kdelibs/ in ~/src/kdelibs/, then do the following: $ ls kdelibs/ $ mkdir kdelibs-build $ cd kdelibs-build $ cmake ../kdelibs This will generated the Makefiles for building kdelibs/ in kdelibs-build/.

Windows

You have to run CMake so that it generates the build files for your system. Both in-source and out-of-source builds are supported by CMake, but currently in-source builds are prevented by the KDE implementation.

So, let's say you have kdelibs\ in c:\daten\kde4, then do the following: c:\daten\kde4> cd kdelibs\win c:\daten\kde4> cmake c:\daten\kde4> make c:\daten\kde4> make install c:\daten\kde4> cd .. c:\daten\kde4> mkdir kdelibs-build c:\daten\kde4> cd kdelibs-build c:\daten\kde4\kdelibs-build> cmake ..\kdelibs

This will generated the Makefiles for building kdelibs\ in kdelibs-build\. See KDE On Windows for more informations about compiling KDE on Windows.

Mac OSX

KDevelop Project Files

If you prefer project files for KDevelop (which will basically be Makefiles accompanied by some extra files for KDevelop), run it like this: $ cmake ../kdelibs -GKDevelop3 Use 'cmake -h' to find out which generators CMake supports and the other options.

CMake and Qt4

To locate Qt 4, CMake searches for qmake in your execute path. CMake does not use the QTDIR environment variable. So make sure that the first qmake found in the execution path is the one you like to use.

More Details

When CMake has finished, it will have created a file called "CMakeCache.txt". This file contains all the settings CMake has detected on your system. If you want to run CMake with another generator or you want CMake to detect everything again, delete this file.

If CMake didn't find something, but you know it is somewere on your box, you can tell CMake manually where to find it. CMake uses variables to store this information. These variables are cached in the already mentioned file CMakeCache.txt. You have three options to adjust these variables manually:

  • tell CMake the correct value via the command line: cmake ../kdelibs -DNAME_OF_THE_VARIABLE=value
  • use ccmake, which provides a curses based GUI to adjust the CMake variables (run: ccmake ../kdelibs)
  • edit the file CMakeCache.txt directly (not recommended)

You should run "ccmake ../kdelibs" at least once so that you get an impression which variables CMake uses. Press "T" to see also the "advanced" variables. So, if CMake didn't find something, start ccmake and adjust it manually.

Some variables you probably will want to set:

  • CMAKE_INSTALL_PREFIX: cmake ../kdelibs -DCMAKE_INSTALL_PREFIX=/opt/kde4 is the equivalent to ./configure --prefix=/opt/kde4
  • CMAKE_BUILD_TYPE: decide which type of build you want. You can chose between "debugfull", "debug", "profile", "relwithdebinfo" and "release". The default is "relwithdebinfo" (-O2 -g). See FindKDE4Internal.cmake for details.
  • RPATH_STYLE: This is not available anymore. By default everything will be built with RPATH set correctly. If you don't want RPATH at all, set CMAKE_SKIP_RPATH to TRUE.
  • WITH_FOO: there are several options, e.g. WITH_CUPS or WITH_Jasper. If you disable them, cmake will not even try to find this package. If it is enabled, cmake will try to find it. If it fails with this, you can still adjust it manually as described above.

Environment Variables

If you have headers and libraries installed in non-standard locations that cmake cannot find (e.g., fink on Mac OSX installs to /sw), then set the following as environment variables. Despite the similar naming convention, these will not work as arguments on the cmake command line:

  • CMAKE_INCLUDE_PATH, eg. export CMAKE_INCLUDE_PATH=/sw/include
  • CMAKE_LIBRARY_PATH, eg. export CMAKE_LIBRARY_PATH=/sw/lib

For more information on variables, see this cmake.org wiki page

Going Further

If cmake finishes with "Generating done" then there was no errors, but if it finishes with "Configuring done" then there was errors that you have to fix. Once cmake finishes successfully, run your buildtool (i.e. make, KDevelop, XCode or MSVC) and build and wait until it has finished. Then "make install".

If you got a failure that says something like CMake Error: This project requires some variables to be set, and cmake can not find them. Please set the following variables: X11_XTest_LIB (ADVANCED) then you may have a missing library (or other dependency). To find out which library, search in the cmake/modules directory for the variable that cmake can't find. In the example above, it is FIND_LIBRARY(X11_XTest_LIB Xtst ${X11_LIB_SEARCH_PATH}) So the missing library is Xtst. You then need to find it (perhaps installing a libXtst-devel library) and re-run cmake.

Using CMake for a Simple Application

Here's the most simple CMakeLists.txt: add_executable(hello main.cpp) This will create an executable named "hello" (or "hello.exe" under Windows) from the source file main.cpp. You can mix C and C++ files as you want. You can have multiple executables and libraries in one CMakeLists.txt. The same source file can be used in multiple targets, it will be compiled for each target independently from the other targets. Probably the most important part of the cmake language are the variables: SET( MY_SOURCES main.cpp widget.cpp) MESSAGE(STATUS "my sources: ${MY_SOURCES}") So, use the SET() command to set the value of a variable. If you list more than one string, the variable will be a list. A list is a list of strings separated by semicolons. If you set it to only one item, it will have just that value. To get the value of a variable, use ${VAR}. You can iterate over a list using FOREACH(): FOREACH(next_ITEM ${MY_SOURCES})

  MESSAGE(STATUS "next item: ${next_ITEM}")

ENDFOREACH(next_ITEM ${MY_SOURCES}) The commands in CMake are case-insensitive. Names of variables and names of parameter are case-sensitive.

You can also test for various things: IF (UNIX)

  MESSAGE(STATUS "This is UNIX (including OS X and CygWin)")

ENDIF (UNIX)

IF (MSVC)

  SET(MY_SRCS ${MY_SRCS} winextra.cpp)

ENDIF (MSVC) In this second example you can see also how to append items to a list.

In the cmake Wiki there is also a tutorial on using cmake to build KDE 4 software. It is recommended reading.

Using CMake for a KDE Project

Here's a basic CMakeList file that builds a small KDE 4 project: PROJECT( kde4project) FIND_PACKAGE(KDE4 REQUIRED) INCLUDE_DIRECTORIES( ${KDE4_INCLUDES} )

SET(KDE4ProjectSources kde4mainapp.cpp someclass.cpp someotherclass.cpp)

KDE4_AUTOMOC( ${KDE4ProjectSources} )

KDE4_ADD_EXECUTABLE(kde4project ${KDE4ProjectSources} )

TARGET_LINK_LIBRARIES(kde4project ${KDE4_KDEUI_LIBS} ${KDE4_KPARTS_LIBS} )

Extending CMake

CMake can be extended using cmake scripts. CMake comes with a number of scripts; under UNIX they are by default installed to /usr/local/share/CMake/Modules/. The KDE libraries install also a set of cmake modules into share/apps/cmake/modules/. The files located there will be preferred over the ones in the system global cmake module path. For detecting software packages there are FindFOO.cmake files, see here for more information. You can also write macros in CMake. They are powerful enough to do most things you will need to build software, but they are not intended to be used as a general purpose programming language.

Converting autotools-based KDE software to CMake

In kdesdk/cmake/ you can find a script am2cmake . This is a ruby script, so you need to have ruby installed. Run am2cmake in the toplevel directory of your sources: $ cd src/mykooltool/ $ am2cmake --kde4 Don't forget the switch "--kde4", otherwise it won't generate files suitable for KDE 4 software. The converted files 'may' work as they are, but complicated projects will require some additional editing.

You may have to:

  • add more include direcories, using INCLUDE_DIRECTORIES()
  • add more link libraries, using TARGET_LINK_LIBRARIES()
  • add some compile switches, using ADD_DEFINITIONS()
  • add some "configure" checks, see How To Do Platform Checks and How To Find Installed Software
  • take special care of former libtool convenience libraries. They are not supported by cmake, instead there will be a file ConvenienceLibs.cmake created. In this file you will find for every convenience lib a variable, which contains all source files of this convenience lib. For the targets which linked to this convenience lib, just add the variable to the sources.
  • a file AdditionalInfo.txt will be created. There you will see all *.in and *.in.in files of your project. The stuff done in these files will have to be converted manually to cmake.

Frequently Asked Questions

How can I teach my favorite editor about CMake syntax and indentation ?

Read the CMake Wiki section CMake Editors Support. It describes how to setup Emacs (XEmacs works too), VIM, Kate, KWrite, and KDevelop.

I need to generate some files during the build. How do I do this ?

Use ADD_CUSTOM_COMMAND(). It's explained here in the CMake wiki: How can I generate a source file during the build

I need to build an executable which is used later on during the build to generate files. How do I do this ?

Let's say the executable is called genembed. Then use KDE4_ADD_EXECUTABLE(foo RUN_UNINSTALLED ${fooSources})to create the executable. The RUN_UNINSTALLED option is important, because the executable has to run from the build dir and has to link to the libraries in the builddir. To achieve this, the executable is compiled with RPATH set accordingly and a wrapper shell script, named just like the executable but with the suffix ".sh" is created. This shell scripts sets up LD_LIBRARY_PATH and the calls the actual executable. Use this wrapper shell script in the ADD_CUSTOM_COMMAND() as described above. You can find out the name and exact location by querying the property WRAPPER_SCRIPT. Here's a full example taken from kdelibs/kstyles/keramik/ :

  1. build the executable

KDE4_ADD_EXECUTABLE(genembed RUN_UNINSTALLED ${genembed_SRCS})

  1. get the name of the generated wrapper script (which sets up LD_LIBRARY_PATH)

GET_TARGET_PROPERTY(GENEMBED_EXECUTABLE genembed WRAPPER_SCRIPT)

  1. and the custom command

ADD_CUSTOM_COMMAND(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/keramikrc.h

 COMMAND ${GENEMBED_EXECUTABLE} --file ${CMAKE_CURRENT_BINARY_DIR}/keramikPics.txt > \
 ${CMAKE_CURRENT_BINARY_DIR}/pixmaps.keramik
 DEPENDS genembed ${keramikPics}

)

As you can see genembed is also listed as a dependency, this means cmake knows that it has to build the executable genembed before executing this rule.

I don't want to set the -DCMAKE_INSTALL_PREFIX command line option. Does cmake support the KDEDIR environment variable?

No. $KDEDIR is deprecated in KDE 4.

Why do i get compile errors like /usr/lib/qt4/include/QtCore/qstring.h:536: undefined reference to `QString::fromLatin1_helper(char const*, int)'?

A: If you have an old Qt4 version in your qt/lib directory you must delete the old (4.0.1) files.

How do I tell cmake to create noisy makefiles? I want to see the exact commands that are run during the make process.

Pass the VERBOSE variable to make, i.e. % make VERBOSE=1 or % VERBOSE=1 make

For more details see the CMake wiki: Is there an option to produce more 'verbose' compiling?

There is no 'make distclean' target in the generated Makefiles. How do I clean up everything, including the cache files?

Simply remove the build directory, or just the contents of the build directory.