After introducing the automoc feature in my last blog, here comes the next part of this series. More will follow.
The new --find-package mode of CMake
Typically, in projects which are built using autotools or handwritten Makefiles, the tool pkg-config is used to find whether and where some library, used by the software, is installed on the current system, and prints the respective command line options for the compiler to stdout.
Since CMake 2.8.6, also CMake can be used additionally to or instead of pkg-config in such projects to find installed libraries.
With version 2.8.6 CMake features the new command line flag --find-package. When called in this mode, CMake produces results compatible to pkg-config, and can thus be used in a similar way.
E.g. to get the compiler command line arguments for compiling an object file, it can be called like this:
$ cmake --find-package -DNAME=LibXml2 -DLANGUAGE=C -DCOMPILER_ID=GNU -DMODE=COMPILE -I/usr/include/libxml2 $
To get the flags needed for linking, do
$ cmake --find-package -DNAME=LibXml2 -DLANGUAGE=C -DCOMPILER_ID=GNU -DMODE=LINK -rdynamic -lxml2 $
As result, the flags are printed to stdout, as you can see.
The required parameters are
So, you can insert calls like the above in your hand-written Makefiles.
... PKG_CHECK_MODULES(XFT, xft >= 2.1.0, have_xft=true, have_xft=false) if test $have_xft = "true"; then AC_MSG_RESULT(Result: CFLAGS: $XFT_CFLAGS LIBS: $XFT_LIBS) fi CMAKE_FIND_PACKAGE(LibXml2, C, GNU) AC_MSG_RESULT(Result: CFLAGS: $LibXml2_CFLAGS LIBS: $LibXml2_LIBS) ...
This will define the variables LibXml2_CFLAGS and LibXml2_LIBS, which can then be used in the Makefile.in/Makefiles.
What does that mean for developers of CMake-based libraries ?
You don't have to install pkg-config pc-files anymore, just install a Config.cmake file for CMake, and both CMake-based and also autotools-based or any other projects can make use of your library without problems.
What does that mean for developers working on e.g. autotools-based projects, and using a project built with CMake ?
Take a look at the cmake_find_package() m4-macro installed since CMake 2.8.6 in share/aclocal/cmake.m4, it contains documentation, and will help you using that library.
Internally, CMake basically executes a find_package() with the given name, turns the results into the command line options for the compiler and prints them to stdout.
All this stuff is still very new, and has not yet seen wide real world testing.