[CMake] Configuration dependent Install and Export

Fabian Saccilotto f.saccilotto at gmx.ch
Thu Jul 25 10:37:32 EDT 2013


Dear CMake-Users,

as we normally use Visual Studio, we would like to provide pre-built 
configuration dependent 3rd-party libraries for our developers. 
Developing with sources on the same machine works, but when I try to 
build machine-independent builds library paths are not correct. Details 
and code snippet found below.

Example of a prebuilt library folder (sba stands for Sparse Bundle 
Adjustment) which could be used via find_package(sba):
sba/
   <version-number>/
     include
     <Configurationname> (Debug, Release, MinSizeRel, RelWithDebInfo)
       /lib
         sba.lib
       /bin
     sba-config.cmake
     sba-config-version.cmake
     sba-targets.cmake
     sba-targets-<Configurationname>.cmake

I successfully builded a project depending on that library when the 
library was built on the same machine and exporting the targets via the 
"export(TARGETS" CMake-Command.

What I wanted to do now is installing the library to a specific folder 
and use that one as machine-independent reference in projects that 
depend on that library. To distinct the configuration for which the 
*.lib was built, I prepend the install destination with the ${BUILD_TYPE}.

When executing the install from within Visual Studio, the libraries and 
files are copied to the correct directory and the *.cmake files are 
generated per configuration.

The problem occurs when I want to reference that library from another 
project. Because of the fact that the *-targets-<configuration>.cmake 
files are generated during execution of CMake for the sba library, they 
contain paths with ${BUILD_TYPE}. 
("${_IMPORT_PREFIX}/${BUILD_TYPE}/lib/sba.lib"). During the 
find_package(sba) stage of the parent project this variable will be 
empty and therefore an error occurs that the file can't be found.

My questions are:
- Has anyone done this before, maybe in another way?
- Is there a way to tell the exported target files the current 
configuration name?

Steps done:
- Found similar but unanswered question: 
http://public.kitware.com/pipermail/cmake/2013-January/053144.html
- I tried install(CODE "install(EXPORT...") to export the file at 
install time (when the configuration is known) which results in an error 
that install is not scriptable.
- I tried using ${CMAKE_INSTALL_CONFIG_NAME} instead of BUILD_TYPE - 
copying files was not successful as the variable is not set when 
installing from visual studio.

Thank you for your help,

Kind regards
Fabian Saccilotto


Install Snippet from CMakeLists.txt:
# Export for later reuse
set(PACKAGE_NAME sba)
set(VERSION 1.6)

# Make a directory for each config
install(TARGETS sba
   EXPORT ${PACKAGE_NAME}-targets
     RUNTIME DESTINATION ${VERSION}/bin/\${BUILD_TYPE} COMPONENT Runtime
     LIBRARY DESTINATION ${VERSION}/lib/\${BUILD_TYPE} COMPONENT Runtime
     ARCHIVE DESTINATION ${VERSION}/lib/\${BUILD_TYPE} COMPONENT 
Development}
     )

configure_file(
     ${${PROJECT_NAME}_SOURCE_DIR}/${PACKAGE_NAME}-config.cmake.in
     ${${PROJECT_NAME}_BINARY_DIR}/${PACKAGE_NAME}-config.cmake
   @ONLY
     )

configure_file(
${${PROJECT_NAME}_SOURCE_DIR}/${PACKAGE_NAME}-config-version.cmake.in
${${PROJECT_NAME}_BINARY_DIR}/${PACKAGE_NAME}-config-version.cmake
   @ONLY
     )

install(FILES
   ${${PROJECT_NAME}_BINARY_DIR}/${PACKAGE_NAME}-config.cmake
   ${${PROJECT_NAME}_BINARY_DIR}/${PACKAGE_NAME}-config-version.cmake
   DESTINATION ${VERSION}
   )

install(EXPORT ${PACKAGE_NAME}-targets DESTINATION ${VERSION})

# Export targets for local use
export(TARGETS sba FILE ${PACKAGE_NAME}-targets.cmake)



More information about the CMake mailing list