I have custom target to run my test after test target is built
add_executable( my_unit_test
${SRC}
)
Before running the test I patch elf
add_custom_command(
TARGET my_unit_test POST_BUILD
COMMENT "=================== PATCH UNIT TEST ELF ==================="
COMMAND patchelf --set-interpreter /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2 my_unit_test
VERBATIM
)
And this is the target to run test
add_test(
NAME UT
COMMAND ${CMAKE_CURRENT_BINARY_DIR}/my_unit_test
)
add_custom_target( run_ut_tests ALL
DEPENDS my_unit_test
)
add_custom_command( TARGET run_ut_tests POST_BUILD
COMMENT "=================== RUN UNIT TESTS ==================="
COMMAND ctest ARGS --output-on-failure
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
)
The problem is the test runs twice. What is the mistake here ?
Related
I have a (power)shell script that generates a version file used in other source files in the project.
How can I "register" this script to be used with CMake in build time? Here is what I have tried:
function(version)
set(SRC version.h)
set(VERSION_CMD ${CMAKE_SOURCE_DIR}/fw_lib/version/version.ps1)
ADD_CUSTOM_TARGET(version DEPENDS ${SRC})
ADD_CUSTOM_COMMAND(
OUTPUT ${SRC} COMMAND ${VERSION_CMD}
${CMAKE_SOURCE_DIR} ${CMAKE_BINARY_DIR}
)
endfunction(version)
Note that ${CMAKE_SOURCE_DIR} and ${CMAKE_BINARY_DIR} are input arguments to the script.
I get the following error:
process_begin: CreateProcess(....) failed.
make (e=193): Error 193
How can I make this work?
Maybe it is too late, but for future:
# find Powershell executable
find_program(POWERSHELL_PATH NAMES powershell)
add_custom_command(
TARGET "program"
POST_BUILD
COMMAND ${POWERSHELL_PATH} "Some Powershell command")
From the CMake Cookbook, I see that we can use the command add_custom_command and add_custom_target to run a custom command at build time. There is a toy example that I want to extract compressed files in subdirectory and link it to the final executable files. We have two CMakeLists.txt files and the following one is in the subdirectory.
find_package(BLAS REQUIRED)
find_package(LAPACK REQUIRED)
set(MATH_SRCS
${CMAKE_CURRENT_BINARY_DIR}/wrap_BLAS_LAPACK/CxxBLAS.cpp
${CMAKE_CURRENT_BINARY_DIR}/wrap_BLAS_LAPACK/CxxLAPACK.cpp
${CMAKE_CURRENT_BINARY_DIR}/wrap_BLAS_LAPACK/CxxBLAS.hpp
${CMAKE_CURRENT_BINARY_DIR}/wrap_BLAS_LAPACK/CxxLAPACK.hpp
)
add_custom_target(BLAS_LAPACK_wrappers
WORKING_DIRECTORY
${CMAKE_CURRENT_BINARY_DIR}
DEPENDS
${MATH_SRCS}
COMMENT
"Intermediate BLAS_LAPACK_wrappers target"
VERBATIM
)
add_custom_command(
OUTPUT
${MATH_SRCS}
COMMAND
${CMAKE_COMMAND} -E tar xzf ${CMAKE_CURRENT_SOURCE_DIR}/wrap_BLAS_LAPACK.tar.gz
WORKING_DIRECTORY
${CMAKE_CURRENT_BINARY_DIR}
DEPENDS
${CMAKE_CURRENT_SOURCE_DIR}/wrap_BLAS_LAPACK.tar.gz
COMMENT
"Unpacking C++ wrappers for BLAS/LAPACK"
)
add_library(math "")
target_sources(math
PRIVATE
${MATH_SRCS}
)
target_include_directories(math
INTERFACE
${CMAKE_CURRENT_BINARY_DIR}/wrap_BLAS_LAPACK
)
# BLAS_LIBRARIES are included in LAPACK_LIBRARIES
target_link_libraries(math
PUBLIC
${LAPACK_LIBRARIES}
)
The following CMakeLists.txt is in the main directory.
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
# Fortran needed to discover LAPACK with some compilers
project(recipe-04 LANGUAGES CXX Fortran)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_subdirectory(deps)
add_executable(linear-algebra linear-algebra.cpp)
target_link_libraries(linear-algebra
PRIVATE
math
)
I do not know why we need the add_custom_target in this example.
The command add_custom_command cannot be executed without generating any target. That is why we need the custom target to run this command. What's more, it can only execute at the configure time. If we want to run the command after we configure the CMakeLists.txt file, we can create a custom target to achieve that as the following.
add_custom_target(BLAS_LAPACK_wrappers
WORKING_DIRECTORY
${CMAKE_CURRENT_BINARY_DIR}
DEPENDS
${MATH_SRCS}
COMMENT
"Intermediate BLAS_LAPACK_wrappers target"
VERBATIM
)
add_custom_command(
OUTPUT
${MATH_SRCS}
COMMAND
${CMAKE_COMMAND} -E tar xzf ${CMAKE_CURRENT_SOURCE_DIR}/wrap_BLAS_LAPACK.tar.gz
WORKING_DIRECTORY
${CMAKE_CURRENT_BINARY_DIR}
DEPENDS
${CMAKE_CURRENT_SOURCE_DIR}/wrap_BLAS_LAPACK.tar.gz
COMMENT
"Unpacking C++ wrappers for BLAS/LAPACK"
)
After we configure the CMakeLists.txt file, we use the Cmake --build . --target=BLAS_LAPACK_wrappers the execute it at the build time.
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.
I want to create a regular file using the target generated, here is the sample code:
cmake_minimum_required(VERSION 3.2 FATAL_ERROR)
add_executable (write_fields #write_fields will create test.dat
main.cpp
)
add_custom_command (
OUTPUT test.dat
DEPENDS write_fields
COMMAND ${CMAKE_BINARY_DIR}/write_fields
VERBATIM
)
But it seems that the custom command never get executed
UPDATE:
The following code doesn’t work either
cmake_minimum_required(VERSION 3.2 FATAL_ERROR)
project(myproj)
add_executable (write_fields
main.cpp
)
add_custom_command (
OUTPUT ${CMAKE_SOURCE_DIR}/test.dat
DEPENDS write_fields
COMMAND ${CMAKE_BINARY_DIR}/write_fields
VERBATIM
)
add_custom_target(myproj DEPENDS ${CMAKE_SOURCE_DIR}/test.dat)
Please read documentation carefully:
If COMMAND specifies an executable target (created by the add_executable() command) it will automatically be replaced by the location of the executable created at build time.
in other words you can use just a plain target name to execute it:
cmake_minimum_required(VERSION 3.2 FATAL_ERROR)
add_executable (
write_fields #write_fields will create test.dat
main.cpp
)
add_custom_command(
COMMAND write_fields
OUTPUT test.dat
DEPENDS write_fields
VERBATIM
)
The second signature adds a custom command to a target such as a
library or executable. This is useful for performing an operation
before or after building the target. The command becomes part of the
target and will only execute when the target itself is built. If the
target is already built, the command will not execute.
add_custom_command(TARGET target
PRE_BUILD | PRE_LINK | POST_BUILD
COMMAND command1 [ARGS] [args1...]
[COMMAND command2 [ARGS] [args2...] ...]
[WORKING_DIRECTORY dir]
[COMMENT comment] [VERBATIM])
This defines a new command that will be associated with building the
specified target. When the command will happen is determined by which
of the following is specified:
PRE_BUILD - run before all other dependencies
PRE_LINK - run after other dependencies
POST_BUILD - run after the target has been built
cmake_minimum_required(VERSION 3.2 FATAL_ERROR)
add_executable (write_fields
main.cpp
)
add_custom_command (
TARGET write_fields
POST_BUILD
COMMAND write_fields
VERBATIM
)
I have this in my toplevel CMakeLists.txt:
add_subdirectory (src) # add_executable (${PROJECT_NAME} ${_SOURCES})
add_subdirectory (data)
In data subdirectory I want to create a file, when ${PROJECT_NAME} is build. The following doesn't work, returns target "foo" does not exist:
add_custom_command(
OUTPUT "${PROJECT_BINARY_DIR}/${PROJECT_NAME}.desktop"
WORKING_DIRECTORY "${PROJECT_BINARY_DIR}"
COMMAND ${INTLTOOLMERGE} -d -u ../po ${PROJECT_NAME}.desktop.in "${PROJECT_NAME}.desktop"
COMMENT "Creating desktop file"
DEPENDS ${PROJECT_NAME}
)
This also doesn't work. Returns: The target name "foo" is unknown in this context
add_custom_command(
TARGET ${PROJECT_NAME}
WORKING_DIRECTORY "${PROJECT_BINARY_DIR}"
COMMAND ${INTLTOOLMERGE} -d -u ../po ${PROJECT_NAME}.desktop.in "${PROJECT_NAME}.desktop"
COMMENT "Creating desktop file"
)
But this works as expected:
add_custom_command(
OUTPUT "${PROJECT_BINARY_DIR}/${PROJECT_NAME}.desktop"
WORKING_DIRECTORY "${PROJECT_BINARY_DIR}"
COMMAND ${INTLTOOLMERGE} -d -u ../po ${PROJECT_NAME}.desktop.in "${PROJECT_NAME}.desktop"
COMMENT "Creating desktop file"
)
add_custom_target (desktopfile DEPENDS "${PROJECT_BINARY_DIR}/${PROJECT_NAME}.desktop")
add_dependencies (${PROJECT_NAME} desktopfile)
Question:
1.- How can I use add_custom_command to run the command when "foo" is build without using a target?
2.- How come add_dependencies knows about "foo" but pure add_custom_command doesn't?
update #1: Simple code:
# /CMakeLists.txt
cmake_minimum_required (VERSION 3.0)
project ("foo")
add_subdirectory (src)
add_subdirectory (data)
# : EOF~
# src/CMakeLists.txt
add_executable (${PROJECT_NAME} main.c)
# : EOF:~
# data/CMakeLists.txt
add_custom_command (
TARGET ${PROJECT_NAME}
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E sleep 5
COMMENT "We're going to try to pause here for 5 seconds"
)
# EOF:~
# src/main.c
#include <stdio.h>
int main () {
printf ("Hello world");
return 0;
}
Please use POST_BUILD option.
From the cmake documentation:
add_custom_command(TARGET target
PRE_BUILD | PRE_LINK | POST_BUILD
COMMAND command1 [ARGS] [args1...]
[COMMAND command2 [ARGS] [args2...] ...]
[WORKING_DIRECTORY dir]
[COMMENT comment] [VERBATIM])
This defines a new command that will be associated with building the specified target. When the command will happen is determined by which of the following is specified:
PRE_BUILD - run before all other dependencies
PRE_LINK - run after other dependencies
POST_BUILD - run after the target has been built
Note that the PRE_BUILD option is only supported on Visual Studio 7 or later. For all other generators PRE_BUILD will be treated as PRE_LINK.