[CMake] Install targets

Michael Hertling mhertling at online.de
Sun Jul 3 03:29:07 EDT 2011


On 07/02/2011 04:34 AM, Tim Gallagher wrote:
> Hi,
> 
> We have a project that has a main executable and dozens of utility executables that may or may not be needed. We can add an executable for each util with EXCLUDE_FROM_ALL defined so just typing make only does the main code and something like 'make grid' compiles the grid generator. 
> 
> But, how can we install these things? I see from the documentation that if EXCLUDE_FROM_ALL is set to true, the behavior is undefined. More generally, is there a way to set 'make install' to only install things that have been built (rather than the current behavior which is to build the default target if it's not built)? 
> 
> A work around would be to create custom install targets that use the cmake copy command to manually move the utility executable to the install directory, but it would be nice if 'make install' installed all targets built without building unbuilt ones...
> 
> Thanks,
> 
> Tim

You might use the OPTIONAL flag of INSTALL(TARGETS ...), cf. [1,2]:

CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)
PROJECT(OPTIONAL C)
SET(CMAKE_VERBOSE_MAKEFILE ON)
FILE(WRITE ${CMAKE_BINARY_DIR}/main.c "int main(void){return 0;}\n")
FILE(WRITE ${CMAKE_BINARY_DIR}/utility1.c "int main(void){return 0;}\n")
FILE(WRITE ${CMAKE_BINARY_DIR}/utility2.c "int main(void){return 0;}\n")
FILE(WRITE ${CMAKE_BINARY_DIR}/utility3.c "int main(void){return 0;}\n")
ADD_EXECUTABLE(main main.c)
ADD_EXECUTABLE(utility1 EXCLUDE_FROM_ALL utility1.c)
ADD_EXECUTABLE(utility2 EXCLUDE_FROM_ALL utility2.c)
ADD_EXECUTABLE(utility3 EXCLUDE_FROM_ALL utility3.c)
INSTALL(TARGETS main
    RUNTIME DESTINATION bin
)
INSTALL(TARGETS utility1 utility2 utility3
    RUNTIME DESTINATION bin OPTIONAL
)

AFAIK, the warning issued w.r.t. EXCLUDE_FROM_ALL can be safely ignored
as long as the respective INSTALL() command includes the OPTIONAL flag.

Regards,

Michael

[1] http://www.mail-archive.com/cmake@cmake.org/msg28569.html
[2] http://www.mail-archive.com/cmake@cmake.org/msg14997.html


More information about the CMake mailing list