[CMake] COMPONENTS for export files

Alexander Neundorf a.neundorf-work at gmx.net
Mon Apr 8 12:09:46 EDT 2013


On Monday 08 April 2013, Nico Schlömer wrote:
> Hi all,
> 
> I have a software project that I build and install with CMake. For
> others to link against the software project, I have a
> "MypackageConfig.cmake.in" from which I create "MypackageConfig.cmake"
> using CONFIGURE_PACKAGE_CONFIG_FILE(). Others can then invoke
> FIND_PACKAGE(Mypackage) in their own CMake files. -- Nice.
> 
> Now, the software breaks down into logical units, and it would make
> sense for other users to be able for import it with the COMPONENTS
> keyword, e.g.,
> 
> FIND_PACKAGE(Mypackage COMPONENTS reader writer parser)
> 
> Is there a recommended way of generating the package files such that
> this syntax becomes applicable?

You can export the targets into separate export sets, one for each component 
(since CMake 2.8.9 I think).
In your Config.cmake file you can then check which components have been 
requested and include() the respective target-files.
The requested components can be queried via Mypackage_FIND_COMPONENTS.
This is from readme.txt:

"Those components can be listed after the COMPONENTS (or REQUIRED)
or OPTIONAL_COMPONENTS keywords.  The set of all listed components will be
specified in a XXX_FIND_COMPONENTS variable.
For each package-specific component, say Yyy, a variable XXX_FIND_REQUIRED_Yyy
will be set to true if it listed after COMPONENTS and it will be set to false
if it was listed after OPTIONAL_COMPONENTS."

It would probably make sense to name the target-files similar to the 
components, so that you can do something like

foreach(comp ${Mypackage_FIND_COMPONENTS})
   include($CMAKE_CURRENT_LIST_DIR}/${comp}Targets.cmake)
endforeach()

You should additionally use the OPTIONAL and RESULT_VARIABLE options for 
include() and check whether the component was required, and set 
Mypackage_FOUND to FALSE if a required component could not be included.

Alex


More information about the CMake mailing list