[Cmake] pkg-config module (FindPkgConfig.cmake)

Albert Strasheim 13640887 at sun . ac . za
Thu, 27 Nov 2003 02:44:51 +0200


--ReaqsoxgOBHFXBhH
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

Hello,

Attached to this message is my first try at a module for finding packages 
using pkg-config.

>From the website:

"pkg-config is a system for managing library compile/link flags that 
works with automake and autoconf (and now CMake ;)). It replaces the 
ubiquitous *-config scripts you may have seen with a single tool. There's 
nothing desktop-specific or desktop-related about pkg-config, despite it 
being on freedesktop.org."

http://www . freedesktop . org/Software/pkgconfig/

Fedora Core 1 includes pkg-config files for all the libraries used by 
GNOME 2.4, some Mozilla libraries, and a few other libraries like OpenSSL.
Quite a few library packges from freshrpms.net also include pkg-config 
files, such as the ALSA library. Furthermore, pkg-config was designed to 
work with Win32.

I implemented the interface to pkg-config as a macro that can check one 
library at a time.

Example:

INCLUDE( ${CMAKE_SOURCE_DIR}/FindPkgConfig.cmake )
PKGCONFIG("glib-2.0 >= 2.2.0")
IF(PKGCONFIG_FOUND)
  IF(CMAKE_PKGCONFIG_C_FLAGS)
    SET(CMAKE_C_FLAGS "${CMAKE_PKGCONFIG_C_FLAGS} ${CMAKE_C_FLAGS}")
    #do something with ${PKGCONFIG_LIBRARIES}
  ENDIF(CMAKE_PKGCONFIG_C_FLAGS)
ELSE(PKGCONFIG_FOUND)
  MESSAGE("Cannot find glib2 version 2.2.0 or above")
ENDIF(PKGCONFIG_FOUND)

Any comments or suggestions on improving this module would be appreciated.

Cheers,

Albert

P.S. The module is currently UNIX-specific, as I don't have Win32 build 
environment. If you're going to experiment with pkg-config on Win32, note 
that pkg-config has a --msvc-syntax switch for outputting the -l and -L 
flags in a format that MSVC understands.

--ReaqsoxgOBHFXBhH
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="FindPkgConfig.cmake"

## FindPkgConfig.cmake
## by Albert Strasheim <http://students . ee . sun . ac . za/~albert/>
##
## This module finds packages using pkg-config, which retrieves
## information about packages from special metadata files.
##
## See http://www . freedesktop . org/Software/pkgconfig/
##
## -------------------------------------------------------------------
##
## Usage:
##
## INCLUDE( ${CMAKE_ROOT}/Modules/FindPkgConfig.cmake)
## PKGCONFIG("libxml-2.0 >= 1.3")
## IF(PKGCONFIG_FOUND)
##   # do something with CMAKE_PKGCONFIG_C_FLAGS
##   # do something with PKGCONFIG_LIBRARIES
## ELSE(PKGCONFIG_FOUND)
#    MESSAGE("Cannot find libxml2 version 1.3 or above")
## ENDIF(PKGCONFIG_FOUND)
##
## Notes:
## 
## You can set the PKG_CONFIG_PATH environment variable to tell
## pkg-config where to search for .pc files. See pkg-config(1) for
## more information.

#FIXME: IF(WIN32) pkg-config --msvc-syntax ENDIF(WIN32) ???

FIND_PROGRAM(CMAKE_PKGCONFIG_EXECUTABLE pkg-config)
MARK_AS_ADVANCED(CMAKE_PKGCONFIG_EXECUTABLE)

MACRO(PKGCONFIG LIBRARY)
  SET(PKGCONFIG_FOUND 0)

  MESSAGE("DEBUG: find library '${LIBRARY}'")
  
  IF(CMAKE_PKGCONFIG_EXECUTABLE)
    MESSAGE("DEBUG: pkg-config executable found")
    
    EXEC_PROGRAM(${CMAKE_PKGCONFIG_EXECUTABLE}
      ARGS "'${LIBRARY}'"
      OUTPUT_VARIABLE PKGCONFIG_OUTPUT
      RETURN_VALUE PKGCONFIG_RETURN)
    IF(NOT PKGCONFIG_RETURN)
      MESSAGE("DEBUG: packages found")
      
      # set C_FLAGS and CXX_FLAGS
      EXEC_PROGRAM(${CMAKE_PKGCONFIG_EXECUTABLE}
        ARGS "--cflags '${LIBRARY}'"
        OUTPUT_VARIABLE CMAKE_PKGCONFIG_C_FLAGS)
      SET(CMAKE_PKGCONFIG_CXX_FLAGS "${CMAKE_PKGCONFIG_C_FLAGS}")
      
      # set LIBRARIES
      EXEC_PROGRAM(${CMAKE_PKGCONFIG_EXECUTABLE}
        ARGS "--libs '${LIBRARY}'"
        OUTPUT_VARIABLE PKGCONFIG_LIBRARIES)
      
      SET(PKGCONFIG_FOUND 1)
    ELSE(NOT PKGCONFIG_RETURN)
      MESSAGE("DEBUG '${LIBRARY}' NOT FOUND by pkg-config")
      
      SET(CMAKE_PKGCONFIG_C_FLAGS "")
      SET(CMAKE_PKGCONFIG_CXX_FLAGS "")
      SET(PKGCONFIG_LIBRARIES "")
    ENDIF(NOT PKGCONFIG_RETURN)
  ELSE(CMAKE_PKGCONFIG_EXECUTABLE)
    MESSAGE("DEBUG: pkg-config executable NOT FOUND")
  ENDIF(CMAKE_PKGCONFIG_EXECUTABLE)

  MESSAGE("DEBUG: CMAKE_PKGCONFIG_C_FLAGS=${CMAKE_PKGCONFIG_C_FLAGS}")
  MESSAGE("DEBUG: PKGCONFIG_LIBRARIES=${PKGCONFIG_LIBRARIES}")
ENDMACRO(PKGCONFIG)

--ReaqsoxgOBHFXBhH--