[Cmake] setting up a project

Brad King brad . king at kitware . com
Tue, 25 Mar 2003 17:47:23 -0500 (EST)


> I would like to know, if there is a more basic example of how to set up
> those files for use with FIND_PACKAGE or make a project easily cooperate
> with other cmake projects ?

Currently there is no example of the "standard" approach.  We have still
been experimenting with approaches to creating these files.  That used by
VTK is a way, but perhaps not THE way.  Anyway, see below for a short
example to get you started.

> Also, I have not fully understood whether the support for build and
> install trees is specific to vtk or whether this has to be done for all
> cmake projects.

Projects built by cmake require a little extra work to be used from both
the install tree and the build tree.  We find it convenient as developers
to work directly from the build tree when we have a chain of dependent
projects.  Also, there is currently no notion of "install" on windows for
CMake, but it will be added.  Supporting this is not required to make
FIND_PACKAGE work, but it doesn't take too much extra work.

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

#-----------------------------------------------------------------------------
# CMEConfig.cmake - CME CMake configuration file for external projects.
#
# This file is configured by CME and used by the UseCME.cmake module
# to load CME's settings for an external project.

# The CME include file directories.
SET(CME_INCLUDE_DIRS "@CMAKE_INSTALL_PREFIX@/include")

# The CME library directories.
SET(CME_LIBRARY_DIRS "@CMAKE_INSTALL_PREFIX@/lib")

# The CME version number.
SET(CME_VERSION_MAJOR "@CME_VERSION_MAJOR@")
SET(CME_VERSION_MINOR "@CME_VERSION_MINOR@")

# The location of the UseCME.cmake file.
SET(CME_USE_FILE "@CMAKE_INSTALL_PREFIX@/lib/cme/UseCME.cmake")

# The build settings file.
SET(CME_BUILD_SETTINGS_FILE
    "@CMAKE_INSTALL_PREFIX@/lib/cme/CMEBuildSettings.cmake")

# The CME library dependencies.  These can be blocked by projects
# 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)

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



#-----------------------------------------------------------------------------
# CMakeLists.txt

# Example project "CMake Export".
PROJECT(CME)

# Every project should have a version number.
SET(CME_VERSION_MAJOR 1)
SET(CME_VERSION_MINOR 0)

# Example library.
ADD_LIBRARY(cme cme.cxx)

# Link to libm on unix for sqrt().  This is to demonstrate library
# dependency chaining.
IF(UNIX)
  TARGET_LINK_LIBRARIES(cme m)
ENDIF(UNIX)

# Create the CMEConfig.cmake file for installation.
CONFIGURE_FILE(${CME_SOURCE_DIR}/CMEConfig.cmake.in
               ${CME_BINARY_DIR}/CMEConfig.cmake @ONLY IMMEIDATE)

# Save the compiler settings and library dependencies so another
# 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)

# Install targets.  Make sure the paths configured into
# CMEConfig.cmake match.
INSTALL_TARGETS(/lib cme)
INSTALL_FILES(/include .h cme)
INSTALL_FILES(/lib/cme .cmake CMEBuildSettings CMELibraryDepends
              UseCME CMEConfig)



#-----------------------------------------------------------------------------
# UseCME.cmake
#
# This module is provided as CME_USE_FILE by CMEConfig.cmake.  It can
# be INCLUDEd in a project to load the needed compiler and linker
# settings to use CME.
#

# 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)

# Add include directories needed to use CME.
INCLUDE_DIRECTORIES(${CME_INCLUDE_DIRS})

# Add link directories needed to use CME.
LINK_DIRECTORIES(${CME_LIBRARY_DIRS})



/*--------------------------------------------------------------------------*/
/* cme.cxx */
#include <math.h>
double cme()
{
  return sqrt(2);
}



/*--------------------------------------------------------------------------*/
/* cme.h */
#ifndef _cme_h
#define _cme_h

extern double cme();

#endif

/* END EXAMPLE */