[CMake] Cmake and pkg-config and linking

Tim Teulings rael at edge.ping.de
Mon Jul 31 15:28:06 EDT 2006


Hello!

I would like to propose a enhanced version of the PKGCONFIG macro. This
one sets an aditional variable to return if the library was found. It
also splits the output of pkg-config into the required variables of
cmake and already does the right formatting.

See below an example for how to use it and how to return the result back
to the relevant cmake functions and variables. Detection code works well
for a number of libraries (png, cairo, xft, dbus and my own illumination
library) under unix (Linux). Windows tests (msys) will follow.

It is also possible to pass complex package strings like "dbus-1 >=
0.61" to the PKGCONFIG macro.

This macro breaks compatibility with the old macro but this was
necessary because cmake itself needs information split. Everything that
is not handled within the macro, needs to be handled outside the macro
anyway. I also have the feeling that defining a "found" variable is in
line with other functions.

What is not yet nice is, that I have to pass include and link
directories before defining my executable and all other options after I
definition of my executable. This way if illumination would be optional
I have to use two check blocks.

It also took me much time to understand when I have to use "-variable"
and when "${_variable}". Macro passing parameter variables do seem to
complicate stuff again :-/

I would be happy to get this included into cmake since I found the old
version to be more difficult to handle. Please improve it if you like.
I'll put this under any license you like (Btw., haven't found any
copyright or license information in at least some macro int he current
distribution). Have fun!

MACRO(PKGCONFIG _found _package _libs _include_dirs _lib_dirs _cflags
_ldflags)

  SET(${_found})
  SET(${_libs})
  SET(${_include_dirs})
  SET(${_lib_dirs})
  SET(${_cflags})
  SET(${_ldflags})

  FIND_PROGRAM(_pkgconfig NAMES pkg-config PATH /usr/local/bin)

  IF(_pkgconfig)
    EXEC_PROGRAM(${_pkgconfig} ARGS \"${_package}\" --exists
RETURN_VALUE _missing OUTPUT_VARIABLE _pkgconfigDevNull)

    IF(NOT _missing)
      EXEC_PROGRAM(${_pkgconfig} ARGS \"${_package}\" --libs-only-l
OUTPUT_VARIABLE _tmp)
      STRING(REPLACE " \n" "" _tmp "${_tmp}")
      STRING(REPLACE "-l" "" _tmp "${_tmp}")
      SEPARATE_ARGUMENTS(_tmp)
      SET(${_libs} ${_tmp})

      EXEC_PROGRAM(${_pkgconfig} ARGS \"${_package}\" --cflags-only-I
OUTPUT_VARIABLE _tmp)
      STRING(REPLACE " \n" "" _tmp "${_tmp}")
      STRING(REPLACE "-I" "" _tmp "${_tmp}")
      SEPARATE_ARGUMENTS(_tmp)
      SET(${_include_dirs} ${_tmp})

      EXEC_PROGRAM(${_pkgconfig} ARGS \"${_package}\" --libs-only-L
OUTPUT_VARIABLE _tmp)
      STRING(REPLACE " \n" "" _tmp "${_tmp}")
      STRING(REPLACE "-L" "" _tmp "${_tmp}")
      SEPARATE_ARGUMENTS(_tmp)
      SET(${_lib_dirs} ${_tmp})

      EXEC_PROGRAM(${_pkgconfig} ARGS \"${_package}\"
--cflags-only-other OUTPUT_VARIABLE _tmp)
      STRING(REPLACE " \n" "" _tmp "${_tmp}")
      SET(${_cflags} ${_tmp})

      EXEC_PROGRAM(${_pkgconfig} ARGS \"${_package}\" --libs-only-other
OUTPUT_VARIABLE _tmp)
      STRING(REPLACE " \n" "" _tmp "${_tmp}")
      SET(${_ldflags} ${_tmp})

      SET(${_found} TRUE)

      MESSAGE(STATUS "Package ${_package} found")
    ELSE(NOT _missing)
      MESSAGE(STATUS "Package ${_package} not found")
    ENDIF(NOT _missing)
  ELSE(_pkgconfig)
    MESSAGE(STATUS "Tool pkg-config not found")
  ENDIF(_pkgconfig)

ENDMACRO(PKGCONFIG)

PKGCONFIG(ILLUMINATION_EXISTS illumination ILLUMINATION_LIBS
                                           ILLUMINATION_INCDIRS
                                           ILLUMINATION_LDDIRS
                                           ILLUMINATION_CFLAGS
                                           ILLUMINATION_LDFLAGS)

INCLUDE_DIRECTORIES(${ILLUMINATION_INCDIRS})
LINK_DIRECTORIES(${ILLUMINATION_LDDIRS})

IF (WIN32)
  ADD_EXECUTABLE(Sudoku WIN32 Dancing.cpp GameArea.cpp Sudoku.cpp Main.cpp)
ELSE (WIN32)
  ADD_EXECUTABLE(Sudoku Dancing.cpp GameArea.cpp Sudoku.cpp Main.cpp)
ENDIF (WIN32)

SET_TARGET_PROPERTIES(Sudoku PROPERTIES COMPILE_FLAGS
"${ILLUMINATION_CFLAGS}"
                                        LINK_FLAGS
"${ILLUMINATION_LDFLAGS}")
TARGET_LINK_LIBRARIES(Sudoku ${ILLUMINATION_LIBS})

-- 
Gruß...
       Tim.


More information about the CMake mailing list