CMAKE_CTEST_COMMAND not defined - cmake

I am using cmake 3.0.2 (on debian 8). I am trying to add some tests using a custom check target like this:
ADD_CUSTOM_TARGET(check
COMMAND ${CMAKE_COMMAND} -E env CTEST_OUTPUT_ON_FAILURE=1 ${CMAKE_CTEST_COMMAND}
DEPENDS test_1 test_2)
I am using the built-in CMAKE_CTEST_COMMAND which is supposedly available in cmake However, the variable is undefined and the command does not work. My questions are the following:
1: Am I doing it wrong? It seems to work with cmake 3.3.2, when was this variable added?
2: How can I get this to work in cmake 3.0.2, should I just replace CMAKE_CTEST_COMMAND with "ctest"?

Related

How to add and remove some flag to cmake in commandline?

In the command line ubuntu, I want to directly delete a series of flags contained in the source code of the HHVM when using cmake for install HHVM, if available, and add another set of flags to it.
What should I do about this?
please guide me.
for use linker -Wl,--emit-relocs should don't use -E , -s and -c.
example :
$ cmake + (-O3 -fno-reorder-blocks-and-partition -Wl,--emit-relocs)
and
$ cmake - (-E ,-s,-c)

set cmake variable from custom command before every build

How can I run a custom command before every build and pass the result to a cmake variable?
I know I can do that on the terminal for example
cmake -DMY_VARIABLE=$(echo FOOBAR)
But I'd like to integrate that in my CMakeLists.txt using
add_custom_command(TARGET ${MY_APP} PRE_BUILD ...).
Then I want to pass that variable as a compile definition.
add_cmpile_definitions(MY_DEFINITION="${MY_VARIABLE}")
I found something similar in the command execute_process which has an argument OUTPUT_VARIABLE that stores the output of the command into that variable. But I think it doesn't run before every build.

How can I pass arguments to ranlib using cmake?

How can I pass an argument to ranlib when compiling a static library with CMake?
I tried:
set_target_properties(myLibrary STATIC_LIBRARY_FLAGS "--plugin /usr/lib/gcc/x86_64-linux-gnu/4.9/liblto_plugin.so")
and this worked for ar but not for the subsequent ranlib command.
Have you tried this?
SET(CMAKE_C_ARCHIVE_FINISH "<CMAKE_RANLIB> -no_warning_for_no_symbols -c <TARGET>")
SET(CMAKE_CXX_ARCHIVE_FINISH "<CMAKE_RANLIB> -no_warning_for_no_symbols -c <TARGET>")
On the Mac, this is how I pass the "-no_warning_for_no_symbols" flag to ranlib.
Note: The SET commands don't modify the ranlib command used as part of an installation by running "make install." CMake's installer code does not generate installation scripts that allow for options to be added to ranlib.
Adding
set_property(
TARGET myLibrary
APPEND
PROPERTY STATIC_LIBRARY_FLAGS "-no_warning_for_no_symbols"
)
worked for me.

How to add_custom_target that depends on "make install"

I'd like to add a custom target named "package" which depends on install target.
When I run make package it should cause first running make install and after that, running my custom command to create a package.
I have tried the following DEPENDS install but it does not work.
I get error message: No rule to make target CMakeFiles/install.dir/all, needed by CMakeFiles/package.dir/all
install(FILES
"module/module.pexe"
"module/module.nmf"
DESTINATION "./extension")
add_custom_target(package
COMMAND "chromium-browser" "--pack-extension=./extension"
DEPENDS install)
EDIT: I tried DEPENDS install keyword and add_dependencies(package install) but neither of them works.
According to http://public.kitware.com/Bug/view.php?id=8438
it is not possible to add dependencies to built-in targets like install or test
You can create custom target which will run install and some other script after.
CMake script
For instance if you have a CMake script MyScript.cmake:
add_custom_target(
MyInstall
COMMAND
"${CMAKE_COMMAND}" --build . --target install
COMMAND
"${CMAKE_COMMAND}" -P "${CMAKE_CURRENT_LIST_DIR}/MyScript.cmake"
WORKING_DIRECTORY
"${CMAKE_BINARY_DIR}"
)
You can run it by building target MyInstall:
cmake --build /path/to/build/directory --target MyInstall
Python script
Of course you can use any scripting language. Just remember to be polite to other platforms
(so probably it's a bad idea to write bash script, it will not work on windows).
For example python script MyScript.py:
find_package(PythonInterp 3.2 REQUIRED)
add_custom_target(
MyInstall
COMMAND
"${CMAKE_COMMAND}" --build . --target install
COMMAND
"${PYTHON_EXECUTABLE}" "${CMAKE_CURRENT_LIST_DIR}/MyScript.py"
WORKING_DIRECTORY
"${CMAKE_BINARY_DIR}"
)
One of the solutions is to install a script which runs the custom target:
add_custom_target(
custom_target
[...]
)
install(CODE "execute_process(COMMAND make custom_target)")
Refs:
https://cmake.org/cmake/help/v3.13/command/install.html#custom-installation-logic
https://cmake.org/cmake/help/v3.5/command/execute_process.html
EDIT: I tried DEPENDS install keyword and add_dependencies(package install) but neither of them works.
Documentation of add_dependencies mentions that: [...](but not targets generated by CMake like install)[...]

Using CMake, how do I get verbose output from CTest?

I'm using CMake to build my project. I have added a unit test binary which is using the Boost unit testing framework. This one binary contains all of the unit tests. I've added that binary to be run by CTest:
ADD_EXECUTABLE( tftest test-main.cpp )
ENABLE_TESTING()
ADD_TEST( UnitTests tftest)
But the build output in Visual Studio only shows the result of running CTest:
Start 1: UnitTests
1/1 Test #1: UnitTests ................***Failed 0.05 sec
0% tests passed, 1 tests failed out of 1
This is not very helpful, because I can't see which test failed. If I run ctest manually from the command line with --verbose I get the output from a Boost unit test which tells what actually failed:
1: Test command: tftest.exe
1: Test timeout computed to be: 9.99988e+006
1: Running 4 test cases...
1: test-main.cpp(20): error in "sanity_check3": check 1 == 2 failed
1:
1: *** 1 failure detected in test suite "Master Test Suite"
1/1 Test #1: UnitTests ................***Failed 0.00 sec
So, what do I need to change in the CMakeLists.txt to have CTest run with --verbose at all times? Is there a better way to use Boost unit tests with CMake/CTest?
You can use the ctest --output-on-failure option, or set the environment variable CTEST_OUTPUT_ON_FAILURE, which will show you any output from the test program whenever the test fails. One way to do this when using Makefiles and the command line would be as follows:
env CTEST_OUTPUT_ON_FAILURE=1 make check
This Stack Overflow question and answer shows how to set environment variables in Visual Studio.
You could call ctest directly, after cmaking and making your project.
ctest --verbose
There is a very simple solution (which for some reason is difficult to find via Google Search):
ctest --output-on-failure
If you use CMake with Visual Studio's open folder function you can add the
"ctestCommandArgs": "--output-on-failure"
setting to your build configuration.
You can check the Testing/Temporary subfolder. It is automatically created after running make test. This folder contains two files: LastTest.log and LastTestsFailed.log. LastTest.log contains desired output for run tests. LastTestFailed.log contains names of failed tests. So you can check them manually after executing make test.
The second way is to get ctest to show you the content of log files after running tests:
place in build dir (from which you run make test) file CTestCustom.ctest (you can do it with configure file command, for example) with following contents
CTEST_CUSTOM_POST_TEST("cat Testing/Temporary/LastTest.log")
Instead of cat you may use whatever Windows cmd command that does similar things.
run make test again and get profit!
additional info about customizing ctest you can find here. Just step to "Customizing cmake" section.
Good luck!
I had to add "check" target by myself. "make tests" does nothing by some reason. So what I did (as was suggest somewhere on stackoverflow) - I added this target manually. To get verbose output I just wrote it like:
add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} --verbose)
make check CTEST_OUTPUT_ON_FAILURE=TRUE
This makes test output more verbose:
make test ARGS="-V"
My approach is a combination of the answers from ony, from zbyszek, and from tarc. I use the ${CMAKE_COMMAND} variable (which is set to the absolute path to the invoked cmake executable) with the -E env CTEST_OUTPUT_ON_FAILURE=1 argument to invoke the actual ctest command using ${CMAKE_CTEST_COMMAND} -C $<CONFIG>. To help clarify what is going on, I start with three cmake -E echo commands to show the current working directory and the ctest command to be invoked. Here is how I call add_custom_target.
add_custom_target(check
${CMAKE_COMMAND} -E echo CWD=${CMAKE_BINARY_DIR}
COMMAND ${CMAKE_COMMAND} -E echo CMD=${CMAKE_CTEST_COMMAND} -C $<CONFIG>
COMMAND ${CMAKE_COMMAND} -E echo ----------------------------------
COMMAND ${CMAKE_COMMAND} -E env CTEST_OUTPUT_ON_FAILURE=1
${CMAKE_CTEST_COMMAND} -C $<CONFIG>
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
DEPENDS ALL_BUILD
)
This plays nice with the MSVC IDE where any test errors are shown as clickable compilation errors. See cmake -E env for documentation of the cmake -E portable command line tool mode. I also add a dependency on ALL_BUILD so that all projects will be built before invoking the check target. (On Linux builds, one may need to replace ALL_BUILD with ALL; I have not tested this on Linux yet.)
For people using Visual Studio, here another variation (hack) on the theme:
cmake -E env CTEST_OUTPUT_ON_FAILURE=1 cmake --build . --target RUN_TESTS
ctest -VV or ctest --extra-verbose
From documentation:
Enable more verbose output from tests.
Test output is normally suppressed and only summary information is
displayed. This option will show even more test output.
There's now a CMake variable that allows you to modify the behaviour of make test. CMAKE_CTEST_ARGUMENTS lets you set a list of arguments to pass to ctest when run via make test.
So adding this to your CMake file:
set(CMAKE_CTEST_ARGUMENTS "--verbose")
Means CTest will always run verbose. Or for just the output of the failed tests, use:
set(CMAKE_CTEST_ARGUMENTS "--output-on-failure")
Edit:
As suggested by RobLoach, since it's a list of arguments, you'll want to append to the list instead.
list(APPEND CMAKE_CTEST_ARGUMENTS "--output-on-failure")
to show the result with XML file you have to execute the test with the following command
~$ ctest -T Test
and we found the result in the Testing/1234123432/test.xml
and other files are generated too in Testing Folder