[CMake] How to "install" then "test"?

Michael Hertling mhertling at online.de
Thu Jan 5 21:34:47 EST 2012


On 12/29/2011 08:01 PM, Denis Scherbakov wrote:
> Dear All!
> 
> Maybe someone can help me: I have a project, we compile binaries and then
> using various INSTALL directives finish the job by copying files where they
> belong: to "bin", "man", "libexec", etc. The point is, we need to run executables after they got installed into this tree, because otherwise
> they won't find all auxiliary files during runtime.
> 
> So I am really missing the point here: how to install files (to a temporary
> directory, for example) and then run various tests? Maybe someone has ideas...
> 
> Sincerely,
> Denis

As an alternative to what Eric has already suggested, you might use
installation components or POST_BUILD custom commands to achieve a
temporary installation for testing purposes:

CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)
PROJECT(P C)
SET(CMAKE_VERBOSE_MAKEFILE ON)
ENABLE_TESTING()
FILE(WRITE ${CMAKE_BINARY_DIR}/main.c "int main(void){return 0;}\n")
# Target main1:
ADD_EXECUTABLE(main1 main.c)
INSTALL(TARGETS main1 DESTINATION bin
    COMPONENT main)
INSTALL(TARGETS main1 DESTINATION ${CMAKE_BINARY_DIR}/test/bin
    COMPONENT test)
ADD_CUSTOM_TARGET(install.main ${CMAKE_COMMAND}
    -DCOMPONENT=main -P ${CMAKE_BINARY_DIR}/cmake_install.cmake)
ADD_CUSTOM_TARGET(install.test ${CMAKE_COMMAND}
    -DCOMPONENT=test -P ${CMAKE_BINARY_DIR}/cmake_install.cmake)
ADD_TEST(NAME main1
    COMMAND ${CMAKE_BINARY_DIR}/test/bin/$<TARGET_FILE_NAME:main1>)
# Target main2:
ADD_EXECUTABLE(main2 main.c)
ADD_CUSTOM_COMMAND(TARGET main2 POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_if_different
        $<TARGET_FILE:main2>
        ${CMAKE_BINARY_DIR}/test/bin/$<TARGET_FILE_NAME:main2>)
INSTALL(TARGETS main2 DESTINATION bin)
ADD_TEST(NAME main2
    COMMAND ${CMAKE_BINARY_DIR}/test/bin/$<TARGET_FILE_NAME:main2>)

For target main1, there're the installation components "main" and
"test", each one triggered via a custom target as usual. Component
"main" does the regular installation, and component "test" installs
to a temporary location where the test "main1" looks for the "main1"
executable. The downside of this approach is that the installation
components are part of the common "install" target - therefor the
additional "install.main" component - and you need to trigger the
"install.test" target before testing because of issue #8438, as
Eric has already pointed out.

The binary built by target "main2" is copied to a temporary location
via a POST_BUILD custom command, so the test "main2" is able to find
it there. Probably, this approach is less complicated than the first
one and won't compromise the common "install" target with additional
installation components, but you may lose the benefits of the quite
powerful INSTALL() command for the temporary installation.

Regards,

Michael


More information about the CMake mailing list