CMake Package

From KitwarePublic
Revision as of 10:26, 19 June 2007 by Mathieu (talk | contribs) (New page: The example shown below will work for INSTALLED projects only. I just hacked it out in a couple minutes, and it is not well tested. It should get you started, though. After installation...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

The example shown below will work for INSTALLED projects only. I just hacked it out in a couple minutes, and it is not well tested. It should get you started, though. After installation, this "CME" sample project can be used by another project like this:

FIND_PACKAGE(CME) IF(CME_FOUND)

 INCLUDE(${CME_USE_FILE})
 # ... Use library ...

ENDIF(CME_FOUND)

-Brad

  1. -----------------------------------------------------------------------------
  2. CMEConfig.cmake - CME CMake configuration file for external projects.
  3. This file is configured by CME and used by the UseCME.cmake module
  4. to load CME's settings for an external project.
  1. The CME include file directories.

SET(CME_INCLUDE_DIRS "@CMAKE_INSTALL_PREFIX@/include")

  1. The CME library directories.

SET(CME_LIBRARY_DIRS "@CMAKE_INSTALL_PREFIX@/lib")

  1. The CME version number.

SET(CME_VERSION_MAJOR "@CME_VERSION_MAJOR@") SET(CME_VERSION_MINOR "@CME_VERSION_MINOR@")

  1. The location of the UseCME.cmake file.

SET(CME_USE_FILE "@CMAKE_INSTALL_PREFIX@/lib/cme/UseCME.cmake")

  1. The build settings file.

SET(CME_BUILD_SETTINGS_FILE

   "@CMAKE_INSTALL_PREFIX@/lib/cme/CMEBuildSettings.cmake")
  1. The CME library dependencies. These can be blocked by projects
  2. not interested in linking to CME's library.

IF(NOT CME_NO_LIBRARY_DEPENDS)

 INCLUDE("@CMAKE_INSTALL_PREFIX@/lib/cme/CMELibraryDepends.cmake")

ENDIF(NOT CME_NO_LIBRARY_DEPENDS)

  1. Additional project-specific configuration settings can be set here.


  1. -----------------------------------------------------------------------------
  2. CMakeLists.txt
  1. Example project "CMake Export".

PROJECT(CME)

  1. Every project should have a version number.

SET(CME_VERSION_MAJOR 1) SET(CME_VERSION_MINOR 0)

  1. Example library.

ADD_LIBRARY(cme cme.cxx)

  1. Link to libm on unix for sqrt(). This is to demonstrate library
  2. dependency chaining.

IF(UNIX)

 TARGET_LINK_LIBRARIES(cme m)

ENDIF(UNIX)

  1. Create the CMEConfig.cmake file for installation.

CONFIGURE_FILE(${CME_SOURCE_DIR}/CMEConfig.cmake.in

              ${CME_BINARY_DIR}/CMEConfig.cmake @ONLY IMMEIDATE)
  1. Save the compiler settings and library dependencies so another
  2. project can import them.

INCLUDE(${CMAKE_ROOT}/Modules/CMakeExportBuildSettings.cmake) CMAKE_EXPORT_BUILD_SETTINGS(${CME_BINARY_DIR}/CMEBuildSettings.cmake) EXPORT_LIBRARY_DEPENDENCIES(${CME_BINARY_DIR}/CMELibraryDepends.cmake)

  1. Install targets. Make sure the paths configured into
  2. CMEConfig.cmake match.

INSTALL_TARGETS(/lib cme) INSTALL_FILES(/include .h cme) INSTALL_FILES(/lib/cme .cmake CMEBuildSettings CMELibraryDepends

             UseCME CMEConfig)


  1. -----------------------------------------------------------------------------
  2. UseCME.cmake
  3. This module is provided as CME_USE_FILE by CMEConfig.cmake. It can
  4. be INCLUDEd in a project to load the needed compiler and linker
  5. settings to use CME.
  1. Load the compiler settings used for CME.

IF(CME_BUILD_SETTINGS_FILE)

 INCLUDE(${CMAKE_ROOT}/Modules/CMakeImportBuildSettings.cmake)
 CMAKE_IMPORT_BUILD_SETTINGS(${CME_BUILD_SETTINGS_FILE})

ENDIF(CME_BUILD_SETTINGS_FILE)

  1. Add include directories needed to use CME.

INCLUDE_DIRECTORIES(${CME_INCLUDE_DIRS})

  1. Add link directories needed to use CME.

LINK_DIRECTORIES(${CME_LIBRARY_DIRS})


/*--------------------------------------------------------------------------*/ /* cme.cxx */

  1. include <math.h>

double cme() {

 return sqrt(2);

}


/*--------------------------------------------------------------------------*/ /* cme.h */

  1. ifndef _cme_h
  2. define _cme_h

extern double cme();

  1. endif

/* END EXAMPLE */

Reference