cmake add_custom_command and DEPENDS/TARGET - cmake

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.

Related

What is the function of add_custom_target in CMake?

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.

Random executable output with CMake

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.

In cmake how to create a regular file using the target generated

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
)

Execute a CMake function on every build-time

I have two cmake function to check resources/shader files are not found/ modified matching to build.
Check.cmake
function(Check sourcefile destinationfile)
if(NOT EXISTS ${destinationfile})
execute_process(COMMAND ${CMAKE_COMMAND}
-E copy ${sourcefile} ${destinationfile})
elseif(${sourcefile} IS_NEWER_THAN ${destinationfile})
execute_process(COMMAND ${MSGMERGE_EXECUTABLE}
"--update" ${destinationfile} ${sourcefile}
OUTPUT_QUIET ERROR_VARIABLE error RESULT_VARIABLE ret)
if(ret) # Have to do this hack as msgmerge prints to stderr.
message(SEND_ERROR "${error}")
endif()
endif()
endfunction()
AddResources.cmake
include(./Check.cmake)
function(AddResources project)
file(GLOB_RECURSE resources ${PROJECT_SOURCE_DIR}/src/project1/Resources/*)
file(GLOB_RECURSE shaders ${PROJECT_SOURCE_DIR}/src/project1/Shaders/*)
foreach( each_file1 ${resources} )
get_filename_component(targetFile ${each_file1} NAME)
if(WIN32)
set(destinationfile "${CMAKE_CURRENT_SOURCE_DIR}/bin/Release/${targetFile}")
set(destinationfile2 "${CMAKE_CURRENT_SOURCE_DIR}/bin/Debug/${targetFile}")
set(sourcefile ${each_file1})
Check(${sourcefile} ${destinationfile})
Check(${sourcefile} ${destinationfile2})
else ()
set(destinationfile "${CMAKE_CURRENT_SOURCE_DIR}/bin/${targetFile}")
set(sourcefile ${each_file1})
Check(${sourcefile} ${destinationfile})
endif()
endforeach(each_file1)
foreach( each_file2 ${shaders} )
get_filename_component(targetFile ${each_file2} NAME)
if(WIN32)
set(destinationfile "${CMAKE_CURRENT_SOURCE_DIR}/bin/Release/${targetFile}")
set(destinationfile2 "${CMAKE_CURRENT_SOURCE_DIR}/bin/Debug/${targetFile}")
set(sourcefile ${each_file2})
Check(${sourcefile} ${destinationfile})
Check(${sourcefile} ${destinationfile2})
else()
set(destinationfile "${CMAKE_CURRENT_SOURCE_DIR}/bin/${targetFile}")
set(sourcefile ${each_file2})
Check(${sourcefile} ${destinationfile})
endif()
endforeach(each_file2)
endfunction()
It is executed AddResources(project1) in the CMakeLists.txt.
AddResources will copy all of resources and shaders to bin folder, but it copies only once(CMake configuration process), not every build-time.
Is there any way to execute CMake function every build-time with argument?
I think you're looking for add_custom_command
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 COMPILE_DEFINITION not working

I am trying CMAKE and I have a problem with COMPILE_DEFINITION, I want to have custom debug target which defines a GIABUILD flag. The CMakeLists.txt is the following
# top level CMakeLists.txt
CMAKE_MINIMUM_REQUIRED(VERSION 2.8.8)
FIND_PACKAGE(deal.II 8.0 REQUIRED
HINTS
${DEAL_II_DIR} $ENV{DEAL_II_DIR} )
DEAL_II_INITIALIZE_CACHED_VARIABLES()
PROJECT(GIA)
SET(CLEAN_UP_FILES
*gmv *gnuplot *gpl *eps *pov *vtk *vtu *ucd *.d2 *dat *.log *.m *.1
)
INCLUDE_DIRECTORIES(include)
FILE(GLOB headers ./include/*.h)
FILE(GLOB sources ./source/*.cc)
ADD_EXECUTABLE(elastic ${sources} ${headers})
ADD_CUSTOM_TARGET(clear
COMMAND rm ${CLEAN_UP_FILES}
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)
ADD_CUSTOM_TARGET(debug
COMMAND ${CMAKE_COMMAND} -DCMAKE_BUILD_TYPE=Debug ${CMAKE_SOURCE_DIR}
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target all
COMMENT "Switch CMAKE_BUILD_TYPE to Debug"
)
#add_definitions(-DGIABUILD)
SET_TARGET_PROPERTIES(debug PROPERTIES COMPILE_DEFINITIONS "GIABUILD")
ADD_CUSTOM_TARGET(release
COMMAND ${CMAKE_COMMAND} -DCMAKE_BUILD_TYPE=Release ${CMAKE_SOURCE_DIR}
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target all
COMMENT "Switch CMAKE_BUILD_TYPE to Release"
)
set(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} /opt/local/lib)
TARGET_LINK_LIBRARIES(elastic /opt/local/lib/libboost_program_options-mt.a)
DEAL_II_SETUP_TARGET(elastic)
The problem is that SET_TARGET_PROPERTIES doesn't work here. add_definitions work but it defines the symbol for all targets which is not what I want.
Thank you.
The problem is that SET_TARGET_PROPERTIES doesn't work here
Because target is custom (created by add_custom_target command) it's no more CMake
responsibility to check/use target properties.
You need to add definitions to non custom target (elastic I guess):
target_compile_definitions(
elastic PUBLIC "$<$<CONFIG:Debug>:GIABUILD>"
)
A couple of solutions that should work. My initial thought is to add the definition directly to your CXX_FLAGS:
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DGIABUILD")
You can try adding the flag directly to your target:
ADD_CUSTOM_TARGET(debug
COMMAND ${CMAKE_COMMAND} -DCMAKE_BUILD_TYPE=Debug ${CMAKE_SOURCE_DIR} -DCMAKE_CXX_FLAGS_DEBUG="-DGIABUILD"
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target all
COMMENT "Switch CMAKE_BUILD_TYPE to Debug"
)