I made a project in CLion (C++ + CMake) where I have a shared library project with 2 configurations Debug | Release. I have also implemented google tests for unit testing.
When the configuration is Release I would like to run some tests (or all) before the build. When the tests fail the library should not be build.
Is this possible? If so how?
I have found the answer with add_custom_command().
In my main CMakeLists.txt I have
if(${CMAKE_BUILD_TYPE} STREQUAL "Release")
#Rebuild the tests just in case some tests has changed and the executable was not rebuild
add_custom_command(TARGET ${PROJECT_NAME} PRE_BUILD
COMMAND ${CMAKE_COMMAND} --build "${CMAKE_BINARY_DIR}" --target tests --
)
if(${WIN32})
set(TESTS_BIN tests.exe)
else()
set(TESTS_BIN tests)
endif()
#Run the tests executable
add_custom_command(TARGET ${PROJECT_NAME} PRE_BUILD
COMMAND "${CMAKE_BINARY_DIR}/${TESTS_BIN}"
)
endif()
The add_custom_command() is smart therefore when the tests executable doesn't return 0 (all test have passed successfully) the build will fail and the library will not be build.
Related
Let's say I have a Python script which does something with just built executable. And I want CMake to rebuild that executable if the script was modified (actually it is enough to just re-run the script, but rebuild an executable is fine too).
add_executable(App src/main.cpp)
add_custom_command(
TARGET App
POST_BUILD
COMMAND "${Python3_EXECUTABLE}" ARGS "scripts/do_stuff.py" "$<TARGET_FILE:App>"
WORKING_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}"
)
How can I achieve that? add_custom_command with TARGET argument doesn't support DEPENDS argument. add_dependency(App "scripts/do_stuff.py") produces an error, because "scripts/do_stuff.py" is not a target, but just a file.
Running the script is very important for correct working of the executable so I don't want define completely separate target via add_custom_command allowing bypass script execution by building just App target.
actually it is enough to just re-run the script
So the executable does not depend on the script. So re-run the script, not the executable.
add_executable(app src/main.cpp)
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/did_do_stuff
COMMAND "${Python3_EXECUTABLE}" "scripts/do_stuff.py" "$<TARGET_FILE:app>"
COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/did_do_stuff
DEPENDS "$<TARGET_FILE:app>"
"${CMAKE_CURRENT_LIST_DIR}/scripts/do_stuff.py"
WORKING_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}"
)
add_custom_target(do_stuff
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/did_do_stuff
)
And build target do_stuff (or all) to run it.
The software will eventually run on an embedded Linux platform. To start, we wrote a library, with all C files in the same folder, and C++ files in a test folder under it.
MyLib
a.c
b.c
MyLib/test
test.cpp
This works on Windows 10, using CMake, Ninja and CLang plus doctest as a test framework. All of these tools are installed under vcpkg.
I am now trying to get everything configured and running on Ubuntu 16.04. All of the tools and source code have been retrieved from our repository. I have tried to build the library and test with Qt, cmake-gui and the command line without success.
Using CMake-gui, after browsing to the source and destination, clicking the Configure button causes the error
CMake Error: CMake was unable to find a build program corresponding to "Ninja".
CMAKE_MAKE_PROGRAM was not set.
The ninja program is in ~/../vcpkg/downloads/tools/ninja-1.8.2-linux/ folder.
I had different errors when trying to build from the command line.
Are there any examples of configurations that work with controlled tools instead of those in the environment?
Edit: added
CMakeLists.txt
cmake_minimum_required(VERSION 3.14)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
project(PSOC_LIB)
option(BUILD_TESTS "bulid tests project" OFF)
set(psoc_lib_VERSION_MAJOR 0)
set(psoc_lib_VERSION_MINOR 1)
set(psoc_lib_VERSION_PATCH 0)
set(psoc_lib_VERSION "${psoc_lib_VERSION_MAJOR}.${psoc_lib_VERSION_MINOR}.${psoc_lib_VERSION_PATCH}")
set(PROJ_NAME "\"PSOC Library\"") # PROJECT_NAME is a predefined variable
set(PROJECT_DESCRIPTION "\"Crossplatform library for products\"")
configure_file(include/psoc/config.h.in
${CMAKE_BINARY_DIR}/include/psoc/config.h
)
set(SOURCES
# 5 *.c files
)
add_library(PSOC_LIB ${SOURCES})
target_include_directories(PSOC_LIB PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
target_compile_definitions(PSOC_LIB PRIVATE _CRT_SECURE_NO_WARNINGS=1)
if (BUILD_TESTS)
include(CTest)
enable_testing()
add_subdirectory(test)
endif (BUILD_TESTS)
test/CmakeLists.txt
cmake_minimum_required(VERSION 3.10)
set(TEST_SOURCES
test.cpp
)
find_package(doctest CONFIG REQUIRED)
add_executable(test_runner test_runner.cpp ${TEST_SOURCES})
target_compile_definitions(test_runner PRIVATE _SILENCE_CXX17_ALLOCATOR_VOID_DEPRECATION_WARNING) #silence warnings about allocator<void> deprecation
target_compile_definitions(test_runner PRIVATE _WIN32_WINNT=0x0601) #target windows 7
target_link_libraries(test_runner PSOC_LIB doctest::doctest)
add_test(all_tests test_runner)
Under Windows, the files in the test folder are build even when BUILD_TESTS is OFF. Under Linux, the test and Testing folders are created only when BUILD_TESTS is ON.
I created a shell script that builds an executable test file.
#!/bin/bash
# Build a debug version of the PSOC_LIB and related tests
mkdir build
cd build
../../../vcpkg_pml/vcpkg/downloads/tools/cmake-3.14.0-linux/cmake-3.14.0-Linux-x86_64/bin/cmake -G Ninja -DCMAKE_BUILD_TYPE=Debug -S ../
ninja
I am trying to use googletest with CMake/Ctest. I have several sources files for my tests (each one containing many TEST/TEST_F/... commands) which are located in several directories. I want that the tests related to a given source are executed in the same directory as their source file. Also, I prefer that the build process of a test source file is a test by itself. So I made something like:
file(GLOB_RECURSE test_srcs
RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
"tests/*.cpp")
foreach(test_src ${test_srcs})
get_filename_component(test_dir ${test_src} DIRECTORY)
get_filename_component(test_exe ${test_src} )NAME_WE)
add_executable(${test_exe} EXCLUDE_FROM_ALL tests/gtest_main.cpp ${test_src})
set_target_properties(${test_exe}
PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${test_dir}
)
target_link_libraries(${test_exe} gtest)
add_test(NAME build_${test_exe} COMMAND "${CMAKE_COMMAND}" --build ${CMAKE_BINARY_DIR} --target ${test_exe})
set_tests_properties(build_${test_exe} PROPERTIES FIXTURES_SETUP ${test_exe})
gtest_discover_tests(${test_exe}
TEST_LIST list
WORKING_DIRECTORY ${test_dir}
PROPERTIES DEPENDS build_${test_exe}
PROPERTIES FIXTURES_REQUIRED ${test_exe}
)
endforeach()
But it seems that the dependencies I am trying to declare between the tests are not taken into account: the build of the tests does not necessarily occurs before the execution of the underlying tests...
If I use the old gtest_add_tests as in the following instead of gtest_discover_tests, it works:
gtest_add_tests(
TARGET ${test_exe}
SOURCES ${test_src}
WORKING_DIRECTORY ${test_dir}
TEST_LIST tlist
)
set_tests_properties(${tlist} PROPERTIES FIXTURES_REQUIRED ${test_exe})
Am I missing something with gtest_discover_tests?
After having started the bounty, I re-started the research on my own. I found out, the simplest method out there is to have googletest installed system-wide.
So, first install the package. On Ubuntu 18.04, that was supt apt install googletest.
For some reason, I had to build the library (perhaps not necessary somehow though?):
cd /usr/src/googletest
mkdir bin && cd bin
cmake ..
make && make install
After that I have been able to compile and run a test case. My CMakeLists.txt testing section looks like this:
enable_testing()
find_package(GTest REQUIRED)
include(GoogleTest)
add_executable(tests tests/foo_test.cpp tests/bar_test.cpp)
target_link_libraries(tests GTest::GTest GTest::Main)
gtest_discover_tests(tests)
A minimal test case file looks like this in my project:
// tests/foo_test.cpp
#include "gtest/gtest.h"
TEST(Foo, Sum)
{
EXPECT_EQ(2, 1 + 1);
}
Compiling is as easy as:
mkdir bin && cd bin
cmake ..
./tests
I'm using CTest in my project. I added simple script to run tests as POST_BUILD. Everything works fine when i build project with make.
The interesting part starts when I'm building package with dpkg-buildpackage. CTest seems to look for libraries in system directories instead of using a currently built one. Is there a way to tell CTest or dpkg-buildpackage to use a currently built library while executing tests?
CMake macro i use:
add_executable(example tests/example.cpp)
target_link_libraries(example my_lib)
enable_testing()
macro(add_unit_test target test)
list(APPEND tests ${test})
add_test(${target} ${test})
endmacro(add_unit_test)
add_unit_test(test_example example)
add_custom_target(all_tests ALL DEPENDS ${tests})
add_custom_command(
TARGET all_tests
COMMENT "Run tests"
POST_BUILD COMMAND ctest ARGS --output-on-failure
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)
Okay, so i've figured it out.
Because my lib compiles in CMAKE_BINARY_DIR, all i needed is to add
LD_LIBRARY_PATH prorerty to every test in project.
So macro now looks like this:
macro(add_unit_test target test)
list(APPEND tests ${test})
add_test(${target} ${test})
set_property(TEST ${target} PROPERTY ENVIRONMENT "LD_LIBRARY_PATH=${CMAKE_BINARY_DIR}")
endmacro(add_unit_test)
Can I have a random name for the executable file of each build?
Or, in another words, a different name for the executable of each build action?
I wonder if a random-variable could be inserted into the build-tool-chain.
The reason of such a name is that my company's virus-checking is quite slow -- it took a long long time checking each executable, even longer then the build.
I'm using CLion 2016.2 on Win7, tool-chain is MinGW_w64_5.0, bundled CMake 3.5.2
You could always define POST_BUILD steps that call another CMake script. The only downside in the following approach would be that you can't - since it's random - reuse the executable's output name in CMake itself:
CMakeLists.txt
cmake_minimum_required(VERSION 3.5)
project(RandomExeName)
file(WRITE main.cpp "int main() { return 0; }")
add_executable(${PROJECT_NAME} main.cpp)
add_custom_command(
TARGET ${PROJECT_NAME}
POST_BUILD
COMMAND ${CMAKE_COMMAND} -D _file:PATH="$<TARGET_FILE:${PROJECT_NAME}>"
-P ${CMAKE_SOURCE_DIR}/CopyToRandom.cmake
)
set_property(TARGET ${PROJECT_NAME} PROPERTY SUFFIX ".temp")
CopyToRandom.cmake
string(RANDOM _random)
file(GLOB _old_files RELATIVE "${CMAKE_BINARY_DIR}" "*.exe")
execute_process(
COMMAND "${CMAKE_COMMAND}" -E remove ${_old_files}
COMMAND "${CMAKE_COMMAND}" -E copy "${_file}" "${_random}.exe"
)
# generate shortcut
get_filename_component(_name "${_file}" NAME_WE)
file(
WRITE "${_name}.sh"
"#!/bin/bash\n"
"${_random}.exe"
)
No you can't. Or you have to reconfigure for every build.
Regarding your actual problem: Advice the virus checker to exclude your build directories.