CMake/Tutorials/How to create a ProjectConfig.cmake file: Difference between revisions

From KitwarePublic
Jump to navigationJump to search
(Added link to downloadable zip file containing the example)
(Use relative paths in FooBarConfig.cmake.in and a FooBarBuildTreeSettings.cmake.in file. Added remark about drawbacks of doing that.)
Line 14: Line 14:
|-- FooBarConfig.cmake.in
|-- FooBarConfig.cmake.in
|-- FooBarConfigVersion.cmake.in
|-- FooBarConfigVersion.cmake.in
|-- FooBarBuildTreeSettings.cmake.in
|-- foo/
|-- foo/
|  |-- CMakeLists.txt
|  |-- CMakeLists.txt
Line 45: Line 46:
set(INSTALL_INCLUDE_DIR include CACHE PATH
set(INSTALL_INCLUDE_DIR include CACHE PATH
   "Installation directory for header files")
   "Installation directory for header files")
set(INSTALL_DATA_DIR share CACHE PATH
if(WIN32 AND NOT CYGWIN)
   "Installation directory for data files")
  set(DEF_INSTALL_CMAKE_DIR CMake)
else()
  set(DEF_INSTALL_CMAKE_DIR lib/CMake/FooBar)
endif()
set(INSTALL_CMAKE_DIR ${DEF_INSTALL_CMAKE_DIR} CACHE PATH
   "Installation directory for CMake files")


# Make relative paths absolute (needed later on)
# Make relative paths absolute (needed later on)
foreach(p LIB BIN INCLUDE DATA)
foreach(p LIB BIN INCLUDE CMAKE)
   set(var INSTALL_${p}_DIR)
   set(var INSTALL_${p}_DIR)
   if(NOT IS_ABSOLUTE "${${var}}")
   if(NOT IS_ABSOLUTE "${${var}}")
Line 76: Line 82:
export(PACKAGE FooBar)
export(PACKAGE FooBar)


# Create a FooBarConfig.cmake file for the use from the build tree
# Create a FooBarBuildTreeSettings.cmake file for the use from the build tree
set(FOOBAR_INCLUDE_DIRS "${PROJECT_SOURCE_DIR}" "${PROJECT_BINARY_DIR}")
configure_file(FooBarBuildTreeSettings.cmake.in
set(FOOBAR_LIB_DIR "${PROJECT_BINARY_DIR}/foo")
  "${PROJECT_BINARY_DIR}/FooBarBuildTreeSettings.cmake" @ONLY)
set(FOOBAR_CMAKE_DIR "${PROJECT_BINARY_DIR}")
 
# Create the FooBarConfig.cmake and FooBarConfigVersion files
file(RELATIVE_PATH CONF_REL_INCLUDE_DIR "${INSTALL_CMAKE_DIR}"
  "${INSTALL_INCLUDE_DIR}")
configure_file(FooBarConfig.cmake.in
configure_file(FooBarConfig.cmake.in
   "${PROJECT_BINARY_DIR}/FooBarConfig.cmake" @ONLY)
   "${PROJECT_BINARY_DIR}/FooBarConfig.cmake" @ONLY)
Line 89: Line 98:
   "${INSTALL_DATA_DIR}/FooBar/CMake"
   "${INSTALL_DATA_DIR}/FooBar/CMake"
   COMPONENT dev)
   COMPONENT dev)
# Create a FooBarConfig.cmake file for the use from the install tree
# and install it
set(FOOBAR_INCLUDE_DIRS "${INSTALL_INCLUDE_DIR}")
set(FOOBAR_LIB_DIR "${INSTALL_LIB_DIR}")
set(FOOBAR_CMAKE_DIR "${INSTALL_DATA_DIR}/FooBar/CMake")
configure_file(FooBarConfig.cmake.in
  "${PROJECT_BINARY_DIR}/InstallFiles/FooBarConfig.cmake" @ONLY)
configure_file(FooBarConfigVersion.cmake.in
  "${PROJECT_BINARY_DIR}/InstallFiles/FooBarConfigVersion.cmake" @ONLY)
install(FILES
  "${PROJECT_BINARY_DIR}/InstallFiles/FooBarConfig.cmake"
  "${PROJECT_BINARY_DIR}/InstallFiles/FooBarConfigVersion.cmake"
  DESTINATION "${FOOBAR_CMAKE_DIR}" COMPONENT dev)
</source>
</source>


Line 147: Line 142:
# It defines the following variables
# It defines the following variables
#  FOOBAR_INCLUDE_DIRS - include directories for FooBar
#  FOOBAR_INCLUDE_DIRS - include directories for FooBar
#  FOOBAR_LIBRARY_DIRS - library directories for FooBar (normally not used!)
#  FOOBAR_LIBRARIES    - libraries to link against
#  FOOBAR_LIBRARIES    - libraries to link against
#  FOOBAR_EXECUTABLE  - the bar executable
#  FOOBAR_EXECUTABLE  - the bar executable


# Tell the user project where to find our headers and libraries
# Compute paths
set(FOOBAR_INCLUDE_DIRS "@FOOBAR_INCLUDE_DIRS@")
get_filename_component(FOOBAR_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
set(FOOBAR_LIBRARY_DIRS "@FOOBAR_LIB_DIR@")
if(EXISTS "${FOOBAR_CMAKE_DIR}/CMakeCache.txt")
  # In build tree
  include("${FOOBAR_CMAKE_DIR}/FooBarBuildTreeSettings.cmake")
else()
  set(FOOBAR_INCLUDE_DIRS "${FOOBAR_CMAKE_DIR}/@CONF_REL_INCLUDE_DIR@")
endif()


# Our library dependencies (contains definitions for IMPORTED targets)
# Our library dependencies (contains definitions for IMPORTED targets)
include("@FOOBAR_CMAKE_DIR@/FooBarLibraryDepends.cmake")
include("${FOOBAR_CMAKE_DIR}/FooBarLibraryDepends.cmake")


# These are IMPORTED targets created by FooBarLibraryDepends.cmake
# These are IMPORTED targets created by FooBarLibraryDepends.cmake
Line 163: Line 162:
</source>
</source>


If your package also provides CMake macros or functions, you might want to put them in a file <tt>FooBarUse.cmake</tt> (or similar), install it alongside <tt>FooBarConfig.cmake</tt> and define the variable <tt>FOOBAR_USE_FILE</tt> in above code and set it to the install-tree location of the <tt>FooBarUse.cmake</tt> file.
If your package also provides CMake macros or functions, you might want to put them in a file <tt>FooBarUse.cmake</tt> (or similar), install it alongside <tt>FooBarConfig.cmake</tt> and define the variable <tt>FOOBAR_USE_FILE</tt> in above code and set it to the location of the <tt>FooBarUse.cmake</tt> file.
 
=== The <tt>FooBar/FooBarBuildTreeSettings.cmake.in</tt> file ===
 
The file <tt>FooBar/FooBarBuildTreeSettings.cmake.in</tt> is a simple helper file that is used to contain hard-coded paths to the build and source tree we ideally wouldn't want in any of the installed files. For the FooBar project it is as simple as
 
<source lang="cmake">
set(FOOBAR_INCLUDE_DIRS
  "@PROJECT_SOURCE_DIR@"
  "@PROJECT_BINARY_DIR@")
</source>


=== The <tt>FooBar/FooBarConfigVersion.cmake.in</tt> file ===
=== The <tt>FooBar/FooBarConfigVersion.cmake.in</tt> file ===


The last file to discuss is the <tt>FooBar/FooBarConfigVersion.cmake.in</tt>. It is important because it allows client projects to determine the version of FooBar they found using the <tt>find_package</tt> command, but more importantly it allows the same command to automatically determine whether the detected version is suitable if the client-project requested a minimum (or even exact) version of FooBar. The file is also straightforward and usually takes the following form:
The last file to discuss is the <tt>FooBar/FooBarConfigVersion.cmake.in</tt>. It is important because it allows client projects to determine the version of FooBar they found using the <tt>find_package</tt> command, but more importantly, it allows the same command to automatically determine whether the detected version is suitable if the client-project requested a minimum (or even exact) version of FooBar. The file is also straightforward and usually takes the following form:


<source lang="cmake">
<source lang="cmake">
Line 182: Line 191:
endif()
endif()
</source>
</source>
=== Closing Remarks ====
In the above approach the <tt>FooBar/FooBarConfig.cmake.in</tt> file uses a relative path to locate the include directory. This has the advantage of making the installed package ''relocatable'', i.e. the user can relocate the whole installation tree and as long as the relative paths remain the same, the package will continue to work with no need for manual tweaking of the installed files. However, the approach also has a serious drawback: On Windows it requires that the variables <tt>INSTALL_INCLUDE_DIR</tt> and <tt>INSTALL_CMAKE_DIR</tt> are set to paths with the same drive letter. Otherwise there is no valid relative path connecting the two. If such an installation is a realistic scenario for your project, you might need to hard-code the full <tt>INSTALL_INCLUDE_DIR</tt> path when configuring <tt>FooBar/FooBarConfig.cmake.in</tt> at the cost of your package being no longer relocatable.

Revision as of 06:02, 12 August 2011

Introduction

Native CMake projects that are intended to be used by other projects (e.g. libraries, but also tools that could be useful as a build-utility, such as documentation generators, wrapper generators, etc.) should provide at a minimum a <name>Config.cmake or a <lower-name>-config.cmake file. This file can then be used by the find_package() command in config-mode to provide information about include-directories, libraries and their dependencies, required compile-flags or locations of executables. You are advised to carefully read the documentation of the find_package() command before proceeding. This short article will show you how to do so for a very simple project.

The full example is available as a download in the zip-file FooBar.zip.

The FooBar project

Let's assume the project contains a simple shared library, foo and a utility that uses the library, bar. The source tree could have the following layout:

FooBar/
|-- CMakeLists.txt
|-- FooBarConfig.cmake.in
|-- FooBarConfigVersion.cmake.in
|-- FooBarBuildTreeSettings.cmake.in
|-- foo/
|   |-- CMakeLists.txt
|   |-- config.h.in
|   |-- foo.h
|   `-- foo.c
`-- bar/
    |-- CMakeLists.txt
    `-- bar.c

The files FooBar/foo/{config.h.in,foo.h,foo.c} and FooBar/bar/bar.c are of little interest here and their contents are left to the imagination of the reader.

The main FooBar/CMakeLists.txt file

A simple FooBar/CMakeLists.txt could look like the following, where the really interesting stuff starts after the respective comment.

<source lang="cmake"> cmake_minimum_required(VERSION 2.8) project(FooBar C)

set(FOOBAR_MAJOR_VERSION 0) set(FOOBAR_MINOR_VERSION 1) set(FOOBAR_PATCH_VERSION 0) set(FOOBAR_VERSION

 ${FOOBAR_MAJOR_VERSION}.${FOOBAR_MINOR_VERSION}.${FOOBAR_PATCH_VERSION})
  1. Offer the user the choice of overriding the installation directories

set(INSTALL_LIB_DIR lib CACHE PATH "Installation directory for libraries") set(INSTALL_BIN_DIR bin CACHE PATH "Installation directory for executables") set(INSTALL_INCLUDE_DIR include CACHE PATH

 "Installation directory for header files")

if(WIN32 AND NOT CYGWIN)

 set(DEF_INSTALL_CMAKE_DIR CMake)

else()

 set(DEF_INSTALL_CMAKE_DIR lib/CMake/FooBar)

endif() set(INSTALL_CMAKE_DIR ${DEF_INSTALL_CMAKE_DIR} CACHE PATH

 "Installation directory for CMake files")
  1. Make relative paths absolute (needed later on)

foreach(p LIB BIN INCLUDE CMAKE)

 set(var INSTALL_${p}_DIR)
 if(NOT IS_ABSOLUTE "${${var}}")
   set(${var} "${CMAKE_INSTALL_PREFIX}/${${var}}")
 endif()

endforeach()

  1. set up include-directories

include_directories(

 "${PROJECT_SOURCE_DIR}"   # to find foo/foo.h
 "${PROJECT_BINARY_DIR}")  # to find foo/config.h
  1. Add sub-directories

add_subdirectory(foo) add_subdirectory(bar)

  1. The interesting stuff goes here
  2. ===============================
  1. Add all targets to the build-tree export set

export(TARGETS foo bar

 FILE "${PROJECT_BINARY_DIR}/FooBarLibraryDepends.cmake")
  1. Export the package for use from the build-tree
  2. (this registers the build-tree with a global CMake-registry)

export(PACKAGE FooBar)

  1. Create a FooBarBuildTreeSettings.cmake file for the use from the build tree

configure_file(FooBarBuildTreeSettings.cmake.in

 "${PROJECT_BINARY_DIR}/FooBarBuildTreeSettings.cmake" @ONLY)
  1. Create the FooBarConfig.cmake and FooBarConfigVersion files

file(RELATIVE_PATH CONF_REL_INCLUDE_DIR "${INSTALL_CMAKE_DIR}"

  "${INSTALL_INCLUDE_DIR}")

configure_file(FooBarConfig.cmake.in

 "${PROJECT_BINARY_DIR}/FooBarConfig.cmake" @ONLY)

configure_file(FooBarConfigVersion.cmake.in

 "${PROJECT_BINARY_DIR}/FooBarConfigVersion.cmake" @ONLY)
  1. Install the export set for use with the install-tree

install(EXPORT FooBarLibraryDepends DESTINATION

 "${INSTALL_DATA_DIR}/FooBar/CMake"
 COMPONENT dev)

</source>

The files FooBar/{foo,bar}/CMakeLists.txt

The file FooBar/foo/CMakeLists.txt is pretty simple and looks like expected:

<source lang="cmake"> configure_file(config.h.in "${CMAKE_CURRENT_BINARY_DIR}/config.h" @ONLY)

add_library(foo SHARED foo.c foo.h config.h.in)

set_target_properties(foo PROPERTIES

 PUBLIC_HEADER "foo.h;${CMAKE_CURRENT_BINARY_DIR}/config.h")

install(TARGETS foo

 # IMPORTANT: Add the foo library to the "export-set"
 EXPORT FooBarLibraryDepends
 RUNTIME DESTINATION "${INSTALL_BIN_DIR}" COMPONENT bin
 LIBRARY DESTINATION "${INSTALL_LIB_DIR}" COMPONENT shlib
 PUBLIC_HEADER DESTINATION "${INSTALL_INCLUDE_DIR}/foo"
   COMPONENT dev)

</source>

The file FooBar/bar/CMakeLists.txt is even shorter:

<source lang="cmake"> add_executable(bar bar.c)

target_link_libraries(bar foo)

install(TARGETS bar

 # IMPORTANT: Add the bar executable to the "export-set"
 EXPORT FooBarLibraryDepends
 RUNTIME DESTINATION "${INSTALL_BIN_DIR}" COMPONENT bin)

</source>

The FooBar/FooBarConfig.cmake.in file

The really interesting file is FooBar/FooBarConfig.cmake.in. Although it usually can be quite simple, it seems to cause considerable confusion to new CMake-users. For the FooBar project the following is a plausible implementation:

<source lang="cmake">

  1. - Config file for the FooBar package
  2. It defines the following variables
  3. FOOBAR_INCLUDE_DIRS - include directories for FooBar
  4. FOOBAR_LIBRARIES - libraries to link against
  5. FOOBAR_EXECUTABLE - the bar executable
  1. Compute paths

get_filename_component(FOOBAR_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH) if(EXISTS "${FOOBAR_CMAKE_DIR}/CMakeCache.txt")

  # In build tree
  include("${FOOBAR_CMAKE_DIR}/FooBarBuildTreeSettings.cmake")

else()

  set(FOOBAR_INCLUDE_DIRS "${FOOBAR_CMAKE_DIR}/@CONF_REL_INCLUDE_DIR@")

endif()

  1. Our library dependencies (contains definitions for IMPORTED targets)

include("${FOOBAR_CMAKE_DIR}/FooBarLibraryDepends.cmake")

  1. These are IMPORTED targets created by FooBarLibraryDepends.cmake

set(FOOBAR_LIBRARIES foo) set(FOOBAR_EXECUTABLE bar) </source>

If your package also provides CMake macros or functions, you might want to put them in a file FooBarUse.cmake (or similar), install it alongside FooBarConfig.cmake and define the variable FOOBAR_USE_FILE in above code and set it to the location of the FooBarUse.cmake file.

The FooBar/FooBarBuildTreeSettings.cmake.in file

The file FooBar/FooBarBuildTreeSettings.cmake.in is a simple helper file that is used to contain hard-coded paths to the build and source tree we ideally wouldn't want in any of the installed files. For the FooBar project it is as simple as

<source lang="cmake"> set(FOOBAR_INCLUDE_DIRS

 "@PROJECT_SOURCE_DIR@"
 "@PROJECT_BINARY_DIR@")

</source>

The FooBar/FooBarConfigVersion.cmake.in file

The last file to discuss is the FooBar/FooBarConfigVersion.cmake.in. It is important because it allows client projects to determine the version of FooBar they found using the find_package command, but more importantly, it allows the same command to automatically determine whether the detected version is suitable if the client-project requested a minimum (or even exact) version of FooBar. The file is also straightforward and usually takes the following form:

<source lang="cmake"> set(PACKAGE_VERSION "@FOOBAR_VERSION@")

  1. Check whether the requested PACKAGE_FIND_VERSION is compatible

if("${PACKAGE_VERSION}" VERSION_LESS "${PACKAGE_FIND_VERSION}")

 set(PACKAGE_VERSION_COMPATIBLE FALSE)

else()

 set(PACKAGE_VERSION_COMPATIBLE TRUE)
 if ("${PACKAGE_VERSION}" VERSION_EQUAL "${PACKAGE_FIND_VERSION}")
   set(PACKAGE_VERSION_EXACT TRUE)
 endif()

endif() </source>

Closing Remarks =

In the above approach the FooBar/FooBarConfig.cmake.in file uses a relative path to locate the include directory. This has the advantage of making the installed package relocatable, i.e. the user can relocate the whole installation tree and as long as the relative paths remain the same, the package will continue to work with no need for manual tweaking of the installed files. However, the approach also has a serious drawback: On Windows it requires that the variables INSTALL_INCLUDE_DIR and INSTALL_CMAKE_DIR are set to paths with the same drive letter. Otherwise there is no valid relative path connecting the two. If such an installation is a realistic scenario for your project, you might need to hard-code the full INSTALL_INCLUDE_DIR path when configuring FooBar/FooBarConfig.cmake.in at the cost of your package being no longer relocatable.