CMake modules


What is CMake?

CMake is a cross-plattform build utlility written by Kitware. It is a build system that comes with some advantages over the common GNU configure and build system.

I've been using successfully the GNU autotools (after lots of hours spent debugging configure.ac and Makefile.am files) for many years. Somebody told me at the work about the configure system that is used in VTK and I decided to take a look at it. It was a good idea because it turned to be a very simple system much easier to learn and to be used than the GNU autotools.

Currently I'm migrating my projects to CMake and begun written some modules for CMake with which you are able to finde libraries with only one command. I don't really like the FindGTK.cmake module that comes with default installation of CMake. I wanted something that uses pkg-config to search for the GTK+/ and libglade libraries.

 

My CMake Modules

The PkgConfig.cmake module

Download: PkgConfig.cmake [3.3 KB]

Once you include this module a new cache entry is generated with the location of the pkg-config utility. This cache entry (PKG_CONFIG_BIN) is marked as advanced to protect it from any CMake GUI. You shouldn't change the path saved in the cache unless you know what your are doing!

A new command is declared: 'PKG_CONFIG'

CMake
PKG_CONFIG(expression
    <RESULT VAR>
    <CFLAGS VAR>
    <LDFLAGS VAR>
)

The expression should be a string that is accepted by pkg-config. For example "gtk-2,0 >= 2.10".

<RESULT VAR> is a boolean variable that saves the result of 'pkg-config --modversion ${expression}', NOT the exit status. That means that <RESULT VAR> is set to TRUE iff pkg-config returns 0.

<CFLAGS VAR> is a string variable that saves the cflags requiered by the package you are searching for.

<LDFLAGS VAR> is a string variable that saves the ldflags requiered by the package you are searching for.

Example:

CMake
SET(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/modules")
INCLUDE(PkgConfig)
 
PKG_CONFIG("gtk-2.0 >= 2.10" GTK_FOUND GTK_CFLAGS GTK_LIBS)
IF(GTK_FOUND)
    ADD_EXECUTABLE(hello-world main.c)
    SET_TARGET_PROPERTIES(hello-world
        PROPERTIES
        COMPILE_FLAGS ${GTK_CFLAGS}
        LINK_FLAGS ${GTK_LIBS}
    )
ENDIF(GTK_FOUND)

Valid XHTML 1.0 Strict   Valid CSS!