cmake: copy .exe target output to destination fails (file extension problem?) - cmake

(cmake version 3.24.1; on Linux with mingw32)
For example, such configuration will fail with "Error copying file /build/path/to/my_program".
How to fix it without changing target name?
add_executable(my_program main.c)
set(CMAKE_C_COMPILER x86_64-w64-mingw32-gcc)
add_custom_command(TARGET my_program POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:my_program> /path/to/
)
The problem seems to be that cmake cannot match target "my_program" to actual file "my_program.exe".
According to what I tried, dll target works out of box, but exe target needs workaround to append .exe to target name.
Is there anyway to avoid changing target name?
target-type target-name real-output cmake-target-name can-copy
executable basename basename.exe basename No
module basename basename.dll basename.so Yes
executable basename.exe basename.exe basename.exe Yes

Thanks #Tsyvarev for answer in comments above.
Use a cmake toolchain and remove CMAKE_C_COMPILER fixes the problem.
-1 Sample toolchain for mingw32:
https://gist.github.com/peterspackman/8cf73f7f12ba270aa8192d6911972fe8
-2 Change CMakeLists.txt:
+++ set(CMAKE_TOOLCHAIN_FILE mingw-w64-x86_64.cmake)
--- set(CMAKE_C_COMPILER x86_64-w64-mingw32-gcc)
-3 Clean build directory (or maybe delete CMakeCache.txt).

Related

Cmake add_custom_command is never running

My question is very similar to cmake: add_custom_command / add_custom_target ignoring dependency
But the answer specified does not solve my issue, and also it is for a newer version of cmake (3.20)
I want some files (shader files) to be copied to the executable directory every time the shader source changes
So I have the following code in cmake:
add_custom_command(
OUTPUT ${CMAKE_BINARY_DIR}/Shaders.txt
COMMAND ${CMAKE_COMMAND} -E echo "Actually Copying shaders"
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/assets/ $<TARGET_FILE_DIR:Editor>/assets
COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_BINARY_DIR}/Shaders.txt
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/assets
)
add_custom_target(CopyShaders ALL DEPENDS ${CMAKE_BINARY_DIR}/Shaders.txt)
Now, as I understand, CopyShaders will always be built (since it is always out of date), but Shaders.txt should only be built once (after the shader source is changed), after which it is up to date
I'm trying to build the target CopyShaders using cmake --build build --target CopyShaders
If I'm using MinGW, then "Actually copying shaders" never gets printed if Shaders.txt is present, even if the assets folder containing the shaders has been modified
If I'm using MSVC, I get the following warning on the terminal:
warning MSB8064: Custom build for item "D:\Acads\Programming\opengl\SummerOfCode\build\CMakeFiles\05fb3856b7a5e1f6ce1ea66ca9091779\Shaders.txt.rule" succeeded, but specified dependency "d:\acads\programming\opengl\summerofcode\editor\assets" does not exist. This may cause incremental build to work incorrectly. [D:\Acads\Programming\opengl\SummerOfCode\build\Editor\CopyShaders.vcxproj]
Again, I do have the folder existing
I figured it out
DEPENDS should only take as input files, not folders
Replacing it with all the files inside /assets works

Is there a command on CMake to started executable resulted from build?

I'm trying to write generic way to run executable resulted after build using CMake's way.
git clone git#github.com:gargamel/ihatesmurfs.git
cmake -E make_directory build
cmake -Sihatesmurfs -Bbuild
cmake --build build
cmake -E chdir build
Now I want to start executable but on *nix, it's like:
./output
and on Windows:
output.exe
Is there a way to escape this with any possible CMake command?
Expanding on my comment a bit, you can modify the CMakeLists.txt file of the project to include add_custom_command. If your CMake creates an executable named HateSmurfs, you can add the custom command to run the executable after compilation completes:
add_executable(HateSmurfs smurfs.cpp)
# Add this piece of code to run the executable after it is built.
add_custom_command(
TARGET HateSmurfs
POST_BUILD
COMMAND HateSmurfs
)
According to add_custom_command documentation:
COMMAND
If COMMAND specifies an executable target name (created by the add_executable() command) it will automatically be replaced by the location of the executable created at build time.

Generate shared library name via CMake

I am trying to add post build event for my project in CMakeLists file.
This post-build event must put Qt libraries near my executable file.
I use add_custom_command to do it:
set(libraryFileName ${QtDir}/bin/${packageName}.dll)
# Copy qt library after build
add_custom_command(
TARGET ${target} POST_BUILD #Path to cmake executable file
COMMAND "${CMAKE_COMMAND}" -E #CMake in command mode
copy #Copy command
"${libraryFileName}" #Path to the file
"$<TARGET_FILE_DIR:${target}>" #Where to copy
COMMENT "Copying to output directory")
The main problem is how to generate libraryFileName correctly for every system? I mean, my soultion works for Windows, but it will fail for other systems types, I guess. Is there any way to get the extension for shared library instead of hardcoding it?
I have found the solution for my case. But it may work for Qt only.
# Get package location
get_target_property(location ${qtVersion}::${shortPackageName} LOCATION)
# Copy qt library after build
add_custom_command(
TARGET ${target} POST_BUILD #Path to cmake executable file
COMMAND "${CMAKE_COMMAND}" -E #CMake in command mode
copy #Copy command
"${location}" #Path to the file
"$<TARGET_FILE_DIR:${target}>" #Where to copy
COMMENT "Copying ${packageName}...")
You can add a platform checker and set the variable depending on that check.
message(STATUS "Current CMAKE_SYSTEM_NAME=${CMAKE_SYSTEM_NAME}")
if("${CMAKE_SYSTEM_NAME}" STREQUAL "WIN98")
set(libraryFileName ${QtDir}/bin/${packageName}.dll)
elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "LINUX")
set(libraryFileName ${QtDir}/bin/${packageName}.so)
else
message(ERROR "The system ${CMAKE_SYSTEM_NAME} is not supported.")
endif()

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.

How to download a toolchain for cross compilation in cmake from separate file?

I have a project with a CMakeLists.txt files in the root and the project compiles fine on Linux and OSX. Now I want to cross compile it for MIPS OpenWRT.
I would like to automate it as much as possible, so I would use following code to download the toolchain and set the compiler variables:
ExternalProject_Add(ar71xx-toolchain
PREFIX "${PROJECT_BINARY_DIR}/external/openwrt"
URL "http://downloads.openwrt.org/barrier_breaker/14.07/ar71xx/generic/OpenWrt-Toolchain-ar71xx-for-mips_34kc-gcc-4.8-linaro_uClibc-0.9.33.2.tar.bz2"
UPDATE_COMMAND ""
PATCH_COMMAND ""
BUILD_COMMAND ""
CONFIGURE_COMMAND ""
INSTALL_COMMAND ""
)
ExternalProject_Get_Property(ar71xx-toolchain SOURCE_DIR)
SET(CMAKE_C_COMPILER ${SOURCE_DIR}/toolchain-mips_34kc_gcc-4.8-linaro_uClibc-0.9.33.2/bin/mips-openwrt-linux-gcc)
SET(CMAKE_CXX_COMPILER ${SOURCE_DIR}/toolchain-mips_34kc_gcc-4.8-linaro_uClibc-0.9.33.2/bin/mips-openwrt-linux-g++)
SET(CMAKE_STRIP ${SOURCE_DIR}/toolchain-mips_34kc_gcc-4.8-linaro_uClibc-0.9.33.2/bin/mips-openwrt-linux-strip)
I thought that I can put it in a separate toolchain file and pass it with -DCMAKE_TOOLCHAIN_FILE, but it seems that ExternalProject_Add is not executed inside the toolchain file.
I would like to avoid putting the toolchain download step into the main CMakeLists.txt since it's actually not essential for the project itself and would require doing the same for each target platform...
So is there a way to define optional steps for a current cross compile build and pass it somehow as command line parameter to be executed before the main project build?
UPDATE:
Based on Tsyvarev's answer that works for me in the toolchain file:
set(CMAKE_SYSTEM_NAME Linux)
set(TOOLCHAIN_DIR ${PROJECT_BINARY_DIR}/external/openwrt/toolchain)
if(NOT EXISTS ${TOOLCHAIN_DIR})
file(DOWNLOAD http://downloads.openwrt.org/barrier_breaker/14.07/ar71xx/generic/OpenWrt-Toolchain-ar71xx-for-mips_34kc-gcc-4.8-linaro_uClibc-0.9.33.2.tar.bz2 ${TOOLCHAIN_DIR}/toolchain.tar.bz2 SHOW_PROGRESS)
execute_process(COMMAND tar --strip-components=2 -xjf ${TOOLCHAIN_DIR}/toolchain.tar.bz2 WORKING_DIRECTORY ${TOOLCHAIN_DIR})
execute_process(COMMAND rm ${TOOLCHAIN_DIR}/toolchain.tar.bz2)
endif()
SET(CMAKE_C_COMPILER ${TOOLCHAIN_DIR}/bin/mips-openwrt-linux-gcc)
SET(CMAKE_CXX_COMPILER ${TOOLCHAIN_DIR}/bin/mips-openwrt-linux-g++)
SET(CMAKE_STRIP ${TOOLCHAIN_DIR}/bin/mips-openwrt-linux-strip)
SET(CMAKE_FIND_ROOT_PATH ${TOOLCHAIN_DIR})
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
There is one issue when passing -DCMAKE_TOOLCHAIN_FILE as CMAKE parameter to other projects added with ExternalProject_Add. Because of it's own ${PROJECT_BINARY_DIR} it will download the toolchain again. But this is another problem...
ExternalProject_add executes all steps at build time, not at configuration time.
For download file you can use file(DOWNLOAD ...) command. For extract files from archive just use execute_process with appropriate command.