[CMake] pkg-config

Alex Brooks a.brooks at acfr.usyd.edu.au
Tue Mar 8 08:59:20 EST 2005


On Wed, 2005-03-09 at 00:17, Brad King wrote:
> Alex Brooks wrote:
> > I have a project with lots of dependencies, and I use pkg-config to
> > check for some of them.  I had a look around for pkg-config support in
> > cmake, and found nothing substantial since this post:
> > 
> > http://public.kitware.com/pipermail/cmake/2003-November/004522.html
> > 
> > Does anyone know if there's been anything more recent?
> 
> I do not think there has been anything more recent.  If you are 
> interested in contributing a CMake module based on the one from that 
> list thread (or from scratch) to use pkg-config we could get you 
> started.  Otherwise you can submit a feature request here:

I actually had a shot at improving the one from that thread already
(attached below).  It works fine for my purposes, but there are a few
deficiencies (listed in the FIXME lines below).

Also, I call this thing from the top level of my source tree, but the
variables seem to get forgotten lower down (with the exception of
lib_FOUND for some reason) so I have to call it again.  I'm also not
sure how many of the outputs, if any, should go in the cache.  (I'm
pretty new to CMake, so I must confess to not really understanding
what's supposed to be cached and what isn't).

That's a start, anyway.  Anyone know what needs to be done to it for
inclusion into CMake?


Cheers,

Alex



## FindPkgConfig.cmake
## by  Albert Strasheim <http://students . ee . sun . ac . za/~albert/>
## and Alex Brooks (a.brooks at acfr . usyd . edu . au)
##
## 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)
## 
## IF ( CMAKE_PKGCONFIG_EXECUTABLE )
##
##     # Find all the librtk stuff with pkg-config
##     PKGCONFIG( "librtk >= 2.0" HAVE_RTK RTK_INCLUDE_DIRS RTK_DEFINES RTK_LINK_DIRS RTK_LIBS )
##
## ELSE  ( CMAKE_PKGCONFIG_EXECUTABLE )
##
##     # Can't find pkg-config -- have to find librtk somehow else
##
## ENDIF ( CMAKE_PKGCONFIG_EXECUTABLE )
##
##
## 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) ???
#
# FIXME: Parsing of pkg-config output is specific to gnu-style flags
#

FIND_PROGRAM(CMAKE_PKGCONFIG_EXECUTABLE pkg-config)
MARK_AS_ADVANCED(CMAKE_PKGCONFIG_EXECUTABLE)

########################################

MACRO(PKGCONFIG_PARSE_FLAGS FLAGS INCLUDES DEFINES)

  #MESSAGE("DEBUG: FLAGS: ${FLAGS}")

  STRING(REGEX MATCHALL "-I[^ ]*" ${INCLUDES} "${FLAGS}")
  STRING(REGEX REPLACE "-I" "" ${INCLUDES} "${${INCLUDES}}")
  #MESSAGE("DEBUG: INCLUDES: ${${INCLUDES}}")
  
  STRING(REGEX REPLACE "-I[^ ]*" "" ${DEFINES} "${FLAGS}")
  #MESSAGE("DEBUG: DEFINES: ${${DEFINES}}")

ENDMACRO(PKGCONFIG_PARSE_FLAGS)

########################################

MACRO(PKGCONFIG_PARSE_LIBS LIBS LINKDIRS LINKLIBS)

  #MESSAGE("DEBUG: LIBS: ${LIBS}")

  STRING(REGEX MATCHALL "-L[^ ]*" ${LINKDIRS} "${LIBS}")
  STRING(REGEX REPLACE "-L" "" ${LINKDIRS} "${${LINKDIRS}}")
  #MESSAGE("DEBUG: LINKDIRS: ${${LINKDIRS}}")

  STRING(REGEX MATCHALL "-l[^ ]*" ${LINKLIBS} "${LIBS}")
  STRING(REGEX REPLACE "-l" "" ${LINKLIBS} "${${LINKLIBS}}")
  #MESSAGE("DEBUG: LINKLIBS: ${${LINKLIBS}}")

ENDMACRO(PKGCONFIG_PARSE_LIBS)

########################################

MACRO(PKGCONFIG LIBRARY FOUND INCLUDE_DIRS DEFINES LINKDIRS LINKLIBS)

  SET(${FOUND} 0)

  MESSAGE("-- Looking for ${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)
      
      # 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}")
      PKGCONFIG_PARSE_FLAGS( "${CMAKE_PKGCONFIG_C_FLAGS}" ${INCLUDE_DIRS} ${DEFINES} )
      
      # set LIBRARIES
      EXEC_PROGRAM(${CMAKE_PKGCONFIG_EXECUTABLE}
        ARGS "--libs '${LIBRARY}'"
        OUTPUT_VARIABLE CMAKE_PKGCONFIG_LIBRARIES)
      PKGCONFIG_PARSE_LIBS ( "${CMAKE_PKGCONFIG_LIBRARIES}" ${LINKDIRS} ${LINKLIBS} )

      SET(${FOUND} 1)
      MESSAGE("-- Looking for ${LIBRARY} -- found")

    ELSE(NOT PKGCONFIG_RETURN)
      MESSAGE("-- Looking for ${LIBRARY} -- not found")
      
      SET(CMAKE_PKGCONFIG_C_FLAGS "")
      SET(CMAKE_PKGCONFIG_CXX_FLAGS "")
      SET(CMAKE_PKGCONFIG_LIBRARIES "")
      SET(${INCLUDE_DIRS} "")
      SET(${DEFINES} "")
      SET(${LINKDIRS} "")
      SET(${LINKLIBS} "")

    ENDIF(NOT PKGCONFIG_RETURN)

  ELSE(CMAKE_PKGCONFIG_EXECUTABLE)
    MESSAGE("-- pkg-config executable NOT FOUND")
  ENDIF(CMAKE_PKGCONFIG_EXECUTABLE)

  #MESSAGE("Have  ${LIBRARY}       : ${${FOUND}}")
  #MESSAGE("${LIBRARY} include dirs: ${${INCLUDE_DIRS}}")
  #MESSAGE("${LIBRARY} defines     : ${${DEFINES}}")
  #MESSAGE("${LIBRARY} link dirs   : ${${LINKDIRS}}")
  #MESSAGE("${LIBRARY} link libs   : ${${LINKLIBS}}")

ENDMACRO(PKGCONFIG)




More information about the CMake mailing list