[CMake] Exclude custom install target(s) from all

Jeet Sukumaran js473 at duke.edu
Fri May 10 20:48:40 EDT 2013


Hi all,

I have a library that is built and linked to as part of my project. I 
want to provide the facility to OPTIONALLY have the library installed 
system-wide (or wherever ${CMAKE_INSTALL_PREFIX} is set). Otherwise, by 
default, the project's final build products will be linked statically to 
the library, and the former get installed, but the library binaries stay 
in the build directory.

In other words:

     $ make
     $ make install

will build and install, the programs, but only something like

     $ make install.foo

will install the library to ${CMAKE_INSTALL_PREFIX}, building it first 
if needed.

I have something like this so far (simplified from the actual script, so 
there might be errors):


     INCLUDE_DIRECTORIES( "${CMAKE_CURRENT_LIST_DIR}")
     SET (FOO_LIBRARY "foo")

     # Following builds library and makes it available to
     # be linked other targets within project by:
     # TARGET_LINK_LIBRARIES(${progname} ${FOO_LIBRARY})
     ADD_LIBRARY(${FOO_LIBRARY}
         foo/foo.cpp # and other sources ...
         )

     ###########################################################
     # Approach #1
     # -----------
     # Optionally allow users to install it by invoking:
     #
     #       cmake .. -DINSTALL_FOO="yes"
     #
     # This works, but it means that users will have to run
     # cmake again to switch back and forth between the libary
     # installation and non-library installation.
     #
     OPTION(INSTALL_FOO "Install foo" OFF)
     IF (INSTALL_FOO)
         INSTALL(TARGETS ${FOO_LIBRARY} DESTINATION lib/foo)
         SET(FOO_HEADERS foo/foo.h)
         INSTALL(FILES ${FOO_HEADERS} DESTINATION include/foo)
         UNSET(INSTALL_FOO CACHE)
     ENDIF()
     ###########################################################

     ###########################################################
     # Approach #2
     # -----------
     # Optionally allow users to install it by invoking:
     #
     #       make install.foo
     #
     # Unfortunately, this gets installed by "make install",
     # which I want to avoid
     SET(FOO_INSTALL "install.foo")
     ADD_CUSTOM_TARGET(${FOO_INSTALL}
         COMMAND ${CMAKE_COMMAND}
             -D COMPONENT=foo
             -P cmake_install.cmake)
     ADD_DEPENDENCIES(${FOO_INSTALL} ${FOO_LIBRARY})
     INSTALL(TARGETS ${FOO_LIBRRARY}
         DESTINATION lib/foo COMPONENT foo)
     SET(FOO_HEADERS foo/foo.h)
     INSTALL(FILES ${FOO_HEADERS}
         DESTINATION include/foo COMPONENT foo)
     ###########################################################

As can be seen, approach #1 sort of works, but the required steps to 
install the library are:

     $ cmake .. -DINSTALL_FOO="yes"
     $ make && make install

And then, to go back to "normal" builds, the user has to remember to run 
cmake again without the "-DINSTALL_FOO" option, otherwise the library 
will be installed on the next "make install".

The second approach works when I run "make install.foo", but it also 
install the library if I run "make install". I would like to avoid the 
latter.

Does anyone have any suggestions on how to go about achieving this?

Thanks!


-- jeet



--------------------------------------
Jeet Sukumaran
--------------------------------------
jeet.sukumaran at duke.edu
--------------------------------------
Department of Biology
Duke University
Box 90338
Durham, NC 27708
--------------------------------------
Blog/Personal Pages:
    http://jeetworks.org/
Photograph Galleries:
    http://jeet.smugmug.com/
GitHub Repositories:
    http://github.com/jeetsukumaran
--------------------------------------



More information about the CMake mailing list