Add multiple executable to single gtest in Bazel - c++

Colleagues,
I have 2 executable and 2 shell scripts for test. In Cmake I have:
add_executable(test1 test1.cpp)
add_executable(test2 test2.cpp)
add_test(NAME test_bin COMMAND test1 test2)
and
add_test(NAME test_sh
COMMAND s1.sh e1.sh)
set_tests_properties(test_sh PROPERTIES TIMEOUT 1.1)
how to do the same in Bazel BUILD? I cannot use separate command line because the tests are the part of the project and I cannot change cmd arguments

Related

"No tests were found!!! " in cmake tutorial step 4

The contens of CMakeLists.txt:
cmake_minimum_required(VERSION 3.10)
# set the project name and version
project(Tutorial VERSION 1.0)
# specify the C++ standard
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
# should we use our own math functions
option(USE_MYMATH "Use tutorial provided math implementation" ON)
# configure a header file to pass some of the CMake settings
# to the source code
configure_file(TutorialConfig.h.in TutorialConfig.h)
# add the MathFunctions library
if(USE_MYMATH)
add_subdirectory(MathFunctions)
list(APPEND EXTRA_LIBS MathFunctions)
endif()
# add the executable
add_executable(Tutorial tutorial.cxx)
target_link_libraries(Tutorial PUBLIC ${EXTRA_LIBS})
# add the binary tree to the search path for include files
# so that we will find TutorialConfig.h
target_include_directories(Tutorial PUBLIC
"${PROJECT_BINARY_DIR}"
)
install(TARGETS Tutorial DESTINATION bin)
install(FILES "${PROJECT_BINARY_DIR}/TutorialConfig.h"
DESTINATION include
)
enable_testing()
# does the application run
add_test(NAME Runs COMMAND Tutorial 25)
# does the usage message work?
add_test(NAME Usage COMMAND Tutorial)
set_tests_properties(Usage
PROPERTIES PASS_REGULAR_EXPRESSION "Usage:.*number"
)
# define a function to simplify adding tests
function(do_test target arg result)
add_test(NAME Comp${arg} COMMAND ${target} ${arg})
set_tests_properties(Comp${arg}
PROPERTIES PASS_REGULAR_EXPRESSION ${result}
)
endfunction()
# do a bunch of result based tests
do_test(Tutorial 4 "4 is 2")
do_test(Tutorial 9 "9 is 3")
do_test(Tutorial 5 "5 is 2.236")
do_test(Tutorial 7 "7 is 2.645")
do_test(Tutorial 25 "25 is 5")
do_test(Tutorial -25 "-25 is (-nan|nan|0)")
do_test(Tutorial 0.0001 "0.0001 is 0.01")
Rebuild the application and then cd to the binary directory and run the ctest executable: ctest -N and ctest -VV. For multi-config generators (e.g. Visual Studio), the configuration type must be specified with the -C <mode> flag. For example, to run tests in Debug mode use ctest -C Debug -VV from the binary directory (not the Debug subdirectory!). Release mode would be executed from the same location but with a -C Release. Alternatively, build the RUN_TESTS target from the IDE.
I run the cmake commands like above, but it can not run successfully.
I Rebuild the application and cd to the bin directory, and run
ctest -C Debug -VV
the rusult is
No tests were found!!!
What did I miss?
Should be run from the build directory, it's not clear in the tutorial, but if you go to the documentation you can see it.
https://cmake.org/cmake/help/book/mastering-cmake/chapter/Testing%20With%20CMake%20and%20CTest.html
I am in the same position. However I found I could run the tests from my /build dir.
ctest -n ../install/bin

windeployqt doesn't deploy qwindowsd.dll for a debug application

I'm trying to use windeployqt.exe (Qt 5.13.2) to deploy dlls for a debug application generated by CMake 3.16. All the dlls are deployed correctly except for the platform plugin dll, which deploys qwindows.dll instead of qwindowsd.dll and results in the following error when I try to run the executable:
This application failed to start because no Qt platform plugin could be initialized.
So far, I've tried:
Specifying --debug on the windeployqt command line. That failed because Qt5Coredd.dll could not be found (note the double d's).
Verifying that no Qt plugin related environment variables are set.
Checked PATH to make sure it doesn't contain any folder with a platforms directory.
If I copy qwindowsd.dll manually, everything works fine. However I'd really like to figure out what I'm doing wrong with windeployqt.
This is apparently a known problem that Qt have dragged their heels on fixing, but I figured out a workaround in CMake - this works for both the Ninja generator/Visual Studio's built in CMake support as well as the regular Visual Studio solution generator
# Split windeployqt into 2 parts to fix issue with deploying debug plugins
add_custom_command(TARGET MyApp POST_BUILD
COMMAND ${QT_PATH}/bin/windeployqt --compiler-runtime --no-plugins ${MY_APP_EXE})
if (CMAKE_GENERATOR STREQUAL "Ninja")
# Ninja is a single-config generator so we can use CMAKE_BUILD_TYPE to generate different commands
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
add_custom_command(TARGET MyApp POST_BUILD
COMMAND ${QT_PATH}/bin/windeployqt --debug --no-compiler-runtime --no-translations --no-libraries ${MY_APP_EXE})
else()
add_custom_command(TARGET MyApp POST_BUILD
COMMAND ${QT_PATH}/bin/windeployqt --release --no-compiler-runtime --no-translations --no-libraries ${MY_APP_EXE})
endif()
else()
# if in MSVC we have to check the configuration at runtime instead of generating different commands
add_custom_command(TARGET MyApp POST_BUILD
COMMAND cmd.exe /c if "$(Configuration)" == "Debug" ${QT_PATH}/bin/windeployqt --debug --no-compiler-runtime --no-translations --no-libraries ${MY_APP_EXE})
add_custom_command(TARGET MyApp POST_BUILD
COMMAND cmd.exe /c if not "$(Configuration)" == "Debug" ${QT_PATH}/bin/windeployqt --release --no-compiler-runtime --no-translations --no-libraries ${MY_APP_EXE})
endif()

Running Tests in CLion using CMake's CTest

I have a C++ project using CMake. The project is built on CentOS machine. I have configured CLion to build remotely from MacOS. I have unit tests for the project and I am trying to run them from CLion. I can run the tests from CentOS machine using CTest like below
ctest -r utCppProject -v
CLion tries to run the executable directly with gtest flags like below
./utCppProject --gtest_filter=* --gtest_color=no
Process finished with exit code 0
No tests are actually run.
How can I configure CLion to be able to use CTest for running unit tests?
Here is my CMakeLists.txt for the unit test project
cmake_minimum_required(VERSION 3.4.1)
include(../../cmake-dependencies/Boost.cmake)
include(../../cmake-dependencies/GoogleTest.cmake)
set(CMAKE_BINARY_DIR "${CMAKE_CURRENT_LIST_DIR}/../build")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
include_directories(${GOOGLE_TEST_DIR}/googletest/include)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/.)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../include)
set(TARGET utCppProject)
add_executable (
${TARGET}
utCppProject.cpp
)
target_link_libraries (
${TARGET}
CppProject
gtest
boost_system
pthread
)
set(CMAKE_CXX_FLAGS "-fPIC -DPIC -Wall -Werror -std=c++0x")
set(TEST_OUTPUT "${CMAKE_BINARY_DIR}/test_results/${TARGET}.xml")
add_test(${TARGET} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${TARGET})
set_tests_properties(${TARGET} PROPERTIES
ENVIRONMENT
"UT_FOLDER_PATH=${CMAKE_CURRENT_SOURCE_DIR};GTEST_OUTPUT=xml:${TEST_OUTPUT}")
Since CLion 2020.3 EAP CTest is supported from the box.
You can easily configure CLion to use CTest. Simply duplicate the default configuration for the test target and configure it to use the CTest executable and set the working directory to the build directory:
In detail:
Executable > Select other... > Find and select ctest (for me it is /usr/bin/ctest, on UNIX-like systems you can use which ctest to find it)
Set program arguments - -j sets the amount of threads to use, then the name of the executable to test, and --output-on-failure to get the test output when something went wrong - you could simply set it to -r utCppProject -v
Set working directory to cmake-build-debug under the project dir, the default build directory for CLion
There is currently no CTest support in CLion.
The feature request is here.
Since version CLion 2020.3 the CTest is supported from the box for CMake 3.14 and above.

How do I make CMake run a python script before building, in order to generate files for my project to be used in the build?

I have a python script that parses all of the C++ source files in the project's directory, looks for some stuff in the files, and then generates a file. This python script works fine, but I want it to automatically run before building my C++ project.
So basically, I want this python script to run before every build, so if any .h or .cpp files have been changed. I'd also like it to run if the python script itself has been changed. I have the python script in question, genenums.py, in the same directory as my C++ source files such as main.cpp, etc.
I've tried experimenting with add_custom_command based on documentation, but I can't get cmake to ever run this python script in any instance. I'm not sure how to make this work right, as I'm new to cmake.
Here's my current cmake file:
cmake_minimum_required(VERSION 3.9)
project(enum_test)
set(CMAKE_CXX_STANDARD 17)
include_directories(include)
find_package( PythonInterp 2.7 REQUIRED )
find_package( PythonLibs 2.7 REQUIRED )
add_custom_command(
COMMAND ${PYTHON_EXECUTABLE} genenums.py
DEPENDS genenums.py $(CMAKE_CURRENT_BINARY_DIR)
OUTPUT enums.h
WORKING_DIRECTORY $(CMAKE_CURRENT_BINARY_DIR)
COMMENT "Generating enums"
)
add_executable(enum_test main.cpp test.h test.cpp)
Ok, I have a foolproof, non-ugly way to have cmake run any kind of commands right before a build to build a dependency, waiting until the commands are done before doing the build.
add_custom_target(
run ALL
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/genenums.py ${CMAKE_CURRENT_SOURCE_DIR}
BYPRODUCTS enums.h
COMMENT "Generating enums"
)
add_dependencies(enum_test run)
The two key parts are the add_custom_target and add_dependencies, both are required to make this work. Place both after the add_executable in CMakeLists.txt. enum_test refers to the target created by add_executable (the first name in its list), so you'd set that to your project's name.
You can name the custom target to whatever you like (I used run here) by changing the run in both add_custom_target and add_dependencies to something else.
There's one additional catch with add_custom_target... WORKING_DIRECTORY in that did nothing for my python script. Even when I tried to set the WORKING_DIRECTORY to ${CMAKE_CURRENT_SOURCE_DIR}, the script executed in the default ${CMAKE_CURRENT_BINARY_DIR} anyway.
So for this one catch, whatever commands you're using need to be able to take a command line argument of ${CMAKE_CURRENT_SOURCE_DIR} and use that to operate in the source directory properly (assuming that's your goal). That's why I have ${CMAKE_CURRENT_SOURCE_DIR} at the end of this line:
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/genenums.py ${CMAKE_CURRENT_SOURCE_DIR}
Here's the full CMakeLists.txt with the working setup, should be fairly easy to adapt to any particular project's CMakeLists.txt.
cmake_minimum_required(VERSION 3.9)
project(enum_test)
set(CMAKE_CXX_STANDARD 17)
include_directories(include)
find_package( PythonInterp 2.7 REQUIRED )
add_executable(enum_test enums.h main.cpp test.h test.cpp)
add_custom_target(
run ALL
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/genenums.py ${CMAKE_CURRENT_SOURCE_DIR}
BYPRODUCTS enums.h
COMMENT "Generating enums"
)
add_dependencies(enum_test run)

Run npm command before specific cmake target

I am trying to build QT based application with CMake and everything goes well enough. I followed this tutorial and I can build my application. Now I want to run npm run build before build using add_custom_command it does not seems to goes as expected.
The build process fails with
RCC: Error in 'tray-icon/systray.qrc': Cannot find file 'html/js/app.full.js'
AUTORCC: error: process for.build/Debug/x64/tray-icon/CMakeFiles/tray-icon.dir/qrc_systray.cpp failed:
RCC: Error in 'tray-icon/systray.qrc': Cannot find file 'html/js/app.full.js'
Can you tell me how to execute npm command before the build verification step on tray-icon/systray.qrc?
This is my cmake file
cmake_minimum_required(VERSION 2.8.11)
project(tray-icon)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
find_package(Qt5Widgets 5.5)
find_package(Qt5Qml 5.5)
find_package(Qt5WebEngine 5.5)
find_package(Qt5WebEngineCore 5.5)
find_package(Qt5WebEngineWidgets 5.5)
# generate rules for building source files from the resources
set(SOURCES tray-icon.cpp window.cpp systray.qrc)
set(CMAKE_VERBOSE_MAKEFILE 1)
#adds target
add_executable(tray-icon ${SOURCES})
# custom build command for javascript part of the application
add_custom_command (
TARGET "tray-icon"
PRE_BUILD COMMAND npm run build
)
# Find the QtWidgets library
target_link_libraries(tray-icon
Qt5::Widgets
Qt5::WebEngine
Qt5::WebEngineWidgets)
install(TARGETS tray-icon DESTINATION .)
PS: My final solution looks like this
# custom build command for javascript part of the application
add_custom_target(
tray-icon_automoc
)
add_custom_target (
npm-target
COMMAND cd ${PROJECT_SOURCE_DIR} && cd html && npm install && npm run build
)
You could try add_custom_target and add_dependencies.
add_custom_target (
npm-target
COMMAND npm run build
)
add_dependencies(tray-icon npm-target)
To overcome the issue mentioned in the comments you should be able to add dependency using AUTOGEN_TARGET_DEPENDS target property. It can be set instead to a list of dependencies for the _automoc target.