[CMake] add_test with --build-and-run fails

Frank Stappers f.p.m.stappers at tue.nl
Fri Sep 10 09:12:58 EDT 2010


Hi David,

Thank for your information. Indeed the CMAKE_SOURCE_DIR differs.
This gave me a new lead. Following your hint allowed me to compile
the executable, however linking to "a_compiled_lib" was causing
a new problem.

So I've decided to solve the problem differently. Namely by
moving the "add_test" into my test project (after the add_executable) and
put "EXCLUDE_FROM_ALL" on the "test_tgt" (therefore excluding the target
from an all_build). The test project now looks like:

=====
project(test_tgt)
cmake_minimum_required(VERSION 2.6)
include_directories(${CMAKE_SOURCE_DIR}/include1)
add_executable(test_tgt EXCLUDE_FROM_ALL "test.cpp"  )
target_link_libraries(test_tgt     a_compiled_lib)

       ADD_TEST(test_tgt ${CMAKE_CTEST_COMMAND}
          --build-and-test
          "${CMAKE_SOURCE_DIR}"
          "${CMAKE_BINARY_DIR}"
          --build-generator "${CMAKE_GENERATOR}"
          --build-project test_tgt
          --build-makeprogram "${CMAKE_MAKE_PROGRAM}"
          --test-command ${CMAKE_CURRENT_BINARY_DIR}/test_tgt
           )
=======

To make it work from the main project, I had change the project back to:

========
.....
ENABLE_TESTING()
add_subdirectory(test)
========

Thank you for help!

Kind regards,
Frank Stappers





On Fri, Sep 10, 2010 at 1:32 PM, David Cole <david.cole at kitware.com> wrote:
> Print out the value of CMAKE_SOURCE_DIR in your CMakeLists.txt file... It's
> different in the context of your outer project and your test project.
>
> If it's "/my/proj" for your outer project, it's going to be "/my/proj/test"
> in your test project.
> Is there a directory named "/my/proj/test/include1"?
> If not, you'll need to pass down:
>
>       --build-options
>
>         -DINCLUDE_DIR:PATH=${CMAKE_SOURCE_DIR}/include1
>
> and reference that in your test/CMakeLists.txt file...
>
> HTH,
> David
>
> On Fri, Sep 10, 2010 at 5:06 AM, Frank Stappers <f.p.m.stappers at tue.nl>
> wrote:
>>
>> Hello,
>>
>> I try to accomplish to add a test, that compiles and runs afterward.
>> For the test that I want to run I have defined a project
>> CMakeLists.txt project in
>> ${CMAKE_SOURCE_DIR}/test/, which looks like:
>>
>> ========
>> project(test_tgt)
>> cmake_minimum_required(VERSION 2.6)
>> include_directories(${CMAKE_SOURCE_DIR}/include1)
>> add_executable(test_tgt "test.cpp"  )
>> target_link_libraries(test_tgt     a_compiled_lib)
>> ========
>>
>> Within the CMakeLists.txt of ${CMAKE_SOURCE_DIR}
>> it is possible to compile "test_tgt", by including
>>
>> ========
>> .....
>> ENABLE_TESTING()
>> add_subdirectory(test)
>> ========
>>
>> And run "test_tgt"
>>
>> Now I change the above code into:
>>
>> ========
>> .....
>> ENABLE_TESTING()
>>        ADD_TEST(test_tgt ${CMAKE_CTEST_COMMAND}
>>           --build-and-test
>>           "${CMAKE_SOURCE_DIR}/test"
>>           "${CMAKE_BINARY_DIR}/test"
>>           --build-generator "${CMAKE_GENERATOR}"
>>           --build-project test_tgt
>>           --build-makeprogram "${CMAKE_MAKE_PROGRAM}"
>>           --test-command test_tgt
>>            )
>> =======
>>
>> I re-run my cmake in the ${CMAKE_BINARY_DIR} and
>> execute:
>>
>> ctest -R test_tgt --verbose
>>
>> When I observe the output, I see something like:
>>
>> =======
>> ....
>> Constructing a list of tests
>> Done constructing a list of tests
>> Checking test dependency graph...
>> test 1
>>    Start 1: test_tgt
>>
>> 1: Test command: ...
>> 1: Test timeout computed to be: 1500
>> 1: Internal cmake changing into directory: ${CMAKE_BINARY_DIR}/test
>> 1: ======== CMake output     ======
>> 1: Configuring
>> 1: Configuring done
>> 1: Generating
>> 1: Generating done
>> 1: Build files have been written to: ${CMAKE_BINARY_DIR}/test
>> 1: ======== End CMake output ======
>> 1: Change Dir: ${CMAKE_BINARY_DIR}/test
>> 1:
>> 1: Run Clean Command:/usr/bin/gmake "clean"
>> 1:
>> 1: Run Build Command:/usr/bin/gmake
>> 1: [100%] Building CXX object CMakeFiles/test_tgt.dir/test.cpp.o
>> 1: In file included from ${CMAKE_SOURCE_DIR}/test/test.cpp:5:0:
>> 1: /usr/include/include1/xxx.h:18:20: fatal error: yyy.h: No such file
>> or directory
>> 1: compilation terminated.
>> 1: gmake[2]: *** [CMakeFiles/test_tgt.dir/test.cpp.o] Error 1
>> 1: gmake[1]: *** [CMakeFiles/test_tgt.dir/all] Error 2
>> 1: gmake: *** [all] Error 2
>> 1/1 Test #1: test_tgt .........................***Failed    0.13 sec
>> =======
>>
>> Observe that the test fails, while it tries to include "yyy.h" from
>> "xxx.h" /usr/include/include1 from, whereas it
>> should:
>> 1) pick the "xxx.h", as it is located in the ${CMAKE_SOURCE_DIR}
>> 2) find "yyy.h" in the include_directories of project "test_tgt", as
>> it is stored in that location.
>>
>> My question:
>> Is there something wrong with my code? Do I have to set additional
>> variables? Or is it a bug?
>>
>> For the record, I'm using Cmake/Ctest 2.8.2.
>>
>> Kind regards,
>> Frank Stappers
>> _______________________________________________
>> Powered by www.kitware.com
>>
>> Visit other Kitware open-source projects at
>> http://www.kitware.com/opensource/opensource.html
>>
>> Please keep messages on-topic and check the CMake FAQ at:
>> http://www.cmake.org/Wiki/CMake_FAQ
>>
>> Follow this link to subscribe/unsubscribe:
>> http://www.cmake.org/mailman/listinfo/cmake
>
>


More information about the CMake mailing list