[CMake] How to handle COMPONENT arguments to find_package in my Config file?

Clinton Stimpson clinton at elemtech.com
Wed Jun 22 12:30:40 EDT 2011


On Wednesday, June 22, 2011 10:01:37 am Stephen Kelly wrote:
> Hi,
> 
> In my GrantleeConfigVersion.cmake.in I can use ${PACKAGE_FIND_VERSION}
> which I presume is filled from the find_package command (I just copied the
> file from elsewhere).
> 
> Is there an equivalent for COMPONENTS so that if someone does a
> 
> find_package(Grantlee COMPONENTS Foo Bar)
> 
> my config file would do something like:
> 
> if (${PACKAGE_FIND_COMPONENTS} CONTAINS "Foo")
>    # Find the dependencies of Foo
> endif()
> 
> if (${PACKAGE_FIND_COMPONENTS} CONTAINS "Bar")
>    # Find the dependencies of Bar
> endif()
> 

You can use Grantlee_FIND_COMPONENTS which would contain the components passed 
in by the user.

Here's an example of using it:

...
# the Grantlee exports file to accompany this config auto generated by cmake's
# export() if this config is used in the build tree, 
# or install(EXPORT ...) if used in the installation tree
SET(GrantleeExportsFile "@GrantleeExportsFile@")
...

include("${GrantleeExportsFile}")

# check that the user requested components 
# are actually targets that are part of this build
IF( Grantlee_FIND_COMPONENTS )
  FOREACH(comp ${Grantlee_FIND_COMPONENTS})
    if(NOT TARGET ${comp})
      SET(Grantlee_${comp}_FOUND 0)
      IF(Grantlee_FIND_REQUIRED_${comp})
        MESSAGE(FATAL_ERROR "Grantlee ${comp} not available.")
      ENDIF()
    ELSE()
      SET(Grantlee_${comp}_FOUND 1)
    ENDIF()
  ENDFOREACH()
ENDIF()



More information about the CMake mailing list