Building Software With CMake

From OMAPpedia

Revision as of 08:34, 9 September 2010 by Michael Opdenacker (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Contents

[edit] Introduction

During software development, you need a build system to compile your software over and over again. For simple program, a simple Makefile is usually sufficient.

However, when the time has come to release your software to others, you often need something more elaborate to help users compile your application from source, check that all prerequisites are available on their system, and also to install your compiled program and all the files that come with it.

In the GNU/Linux world, many software packages are built using the autotools. These tools have made it possible for GNU project components to be cross-platform and support multiple UNIX flavors and multiple CPU architectures.

However, though this build system is still very useful today, it is also far from being perfect. It has a steep learning curve for new developers, which basically have to get familiar with a complex set of tools and macro language. Debugging user issues can also be very difficult, because of the complexity of the generated Makefiles and scripts put in the end of the users. See this article for criticism about the autotools and make .

Fortunately, more modern, smarter, and easier to learn alternatives now exist. Let's have a look at CMake.

[edit] Building a simple program with CMake

First, install CMake. For example, if you are using a Debian based distro:

sudo apt-get install cmake

Let's take an example program, which doesn't have a build system yet: devmem2.c

To build it with CMake, you just need to create a CMakeLists.txt file, such as:

cmake_minimum_required(VERSION 2.8)
project(devmem2)
add_executable(devmem2 devmem2.c)
install(TARGETS devmem2 DESTINATION "/usr/sbin")

CMake won't actually build the software by itself. It will first check for available compiling software and depending on your operating system, generate a native build environment (Makefile on Unix, project / workspace on Windows...). Here's the command to do it:

cmake .

Now, you can build and install your software:

make
sudo make install

[edit] Slightly more elaborate examples

What's good about CMake, compared to the autotools and make, is that you just have to tell it which source .c and .h files you have, and which libraries you will compile your program with, and it will automatically figure out how to build it. That's no rocket science anyway, but with the autotools, you have to explicit many more things.

Here is another, easy to understand example of a program depending on an internal library and on zlib and libm:

PROJECT(MyProject C)
ADD_LIBRARY(MyLibrary STATIC libSource.c)
ADD_EXECUTABLE(MyProgram main.c)
TARGET_LINK_LIBRARIES(MyProgram MyLibrary z m)

You will find many nice introductory information about CMake on the Internet, like this Linux Journal article.

[edit] Debian packaging

This can be useful for people creating software for Ubuntu on OMAP, but the following applies to any other use in a Debian system, of course.

In the same way it supports the autotools, the Debian build system also has native support for CMake.

Back to our devmem2 example above, here's what you need to do to create a Debian package for your application.

You will need a few packages to build your Debian package:

sudo apt-get install dh-make devscripts cdbs

First, put your source code in a project-x.y.z directory, and from this directory, create a basing Debian skeleton, for example:

dh_make --createorig -c gpl2 -s -e michael@free-electrons.com

Explore the debian directory, and adapt the files according to your application. You probably will have to remove multiple .ex example files that are not needed in your package.

You will see that a very small number of files are needed to build a Debian package with no Lintian warnings!

devmem2-1.0.0/
devmem2-1.0.0/debian/
devmem2-1.0.0/debian/devmem2.manpages
devmem2-1.0.0/debian/compat
devmem2-1.0.0/debian/changelog
devmem2-1.0.0/debian/rules
devmem2-1.0.0/debian/control
devmem2-1.0.0/debian/copyright
devmem2-1.0.0/debian/source/
devmem2-1.0.0/debian/source/format
devmem2-1.0.0/devmem2.1
devmem2-1.0.0/CMakeLists.txt
devmem2-1.0.0/devmem2.c

The complete sources can be found on http://free-electrons.com/pub/ubuntu/cmake/devmem2-1.0.0.tar.bz2. Don't hesitate to use them as a starting template, instead of starting from scratch with dh_make!

At least, let's see how simple the debian/rules file is:

#!/usr/bin/make -f
include /usr/share/cdbs/1/rules/debhelper.mk
include /usr/share/cdbs/1/class/cmake.mk

To clear all Lintian errors and warnings, you will need to file a Debian Intent to Package. See the Debian Mentors FAQ if you wish to contribute a package to Debian and maintain it.

Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox