How to make CMake 'FILE' command dependent on TARGET or OUTPUT? - cmake

I've got some add_custom_command() stuff in a CMake build to do some things after building an elf file target: convert it to srec, fill various areas with 0xFF and create a binary image, generate a CRC and get the size of the image. add_custom_command() can have DEPENDS, making it run only when the elf file is regenerated, which is great.
What I also want to do is the create a new file using FILE() that contains the binary filename, crc, and size (maybe in simple JSON format), but the documentation implies that I can't do this file activity after the things I mention above have happened.
# This command creates the FF-filled binary file. It uses objcopy to create the srec
# file to operate on.
add_custom_command(
OUTPUT ThreadingApp.bin filled.srec
MAIN_DEPENDENCY ThreadingApp.elf
COMMAND ${CMAKE_OBJCOPY} ARGS -O srec ThreadingApp.elf ThreadingApp.srec
COMMAND srec_cat.exe ThreadingApp.srec -offset - -minimum-addr ThreadingApp.srec
−fill 0xFF -over ThreadingApp.srec -o filled.srec
COMMAND srec_cat.exe filled.srec -o ThreadingApp.bin -binary
)
# This command creates the CRC
add_custom_command(
OUTPUT crc.out size.out
MAIN_DEPENDENCY filled.srec
COMMAND srec_cat.exe filled.srec -crc32-b-e 0x08100000 -crop 0x08100000 0x08100004 -o - -hex-dump > crc.out
COMMAND srec_info.exe filled.srec > size.out
)
file(
WRITE ThreadingApp.json
)
Looking at the synopsis of 'FILE', I don't see how I can make this happen only after my custom commands have already run. Any suggestions on how to achieve this within CMake? My alternative is to write a separate Python script to execute within an add_custom_command to create the json file.
Reading
file(READ <filename> <out-var> [...])
file(STRINGS <filename> <out-var> [...])
file(<HASH> <filename> <out-var>)
file(TIMESTAMP <filename> <out-var> [...])
Writing
file({WRITE | APPEND} <filename> <content>...)
file({TOUCH | TOUCH_NOCREATE} [<file>...])
file(GENERATE OUTPUT <output-file> [...])
Filesystem
file({GLOB | GLOB_RECURSE} <out-var> [...] [<globbing-expr>...])
file(RENAME <oldname> <newname>)
file({REMOVE | REMOVE_RECURSE } [<files>...])
file(MAKE_DIRECTORY [<dir>...])
file({COPY | INSTALL} <file>... DESTINATION <dir> [...])
file(SIZE <filename> <out-var>)
file(READ_SYMLINK <linkname> <out-var>)
file(CREATE_LINK <original> <linkname> [...])
Path Conversion
file(RELATIVE_PATH <out-var> <directory> <file>)
file({TO_CMAKE_PATH | TO_NATIVE_PATH} <path> <out-var>)
Transfer
file(DOWNLOAD <url> <file> [...])
file(UPLOAD <file> <url> [...])
Locking
file(LOCK <path> [...])

Unlike to add_custom_command, which COMMAND is executed on build stage, FILE() is executed immediately, at configuration stage when CMakeLists.txt is processed.
You may however put FILE() invocation into separate CMake script, and run this script with add_custom_command. With that approach the script with FILE() will be executed at build stage of your project, and you may use OUTPUT or TARGET option:
# File: my_script.cmake
file(...)
# File: CMakeLists.txt:
add_custom_command(OUTPUT | TARGET ...
COMMAND ${CMAKE_COMMAND} -P "${CMAKE_CURRENT_SOURCE_DIR}/my_script.cmake"
)
Aside from demonstrated CMake scripting mode (cmake -P) there is a command line mode (cmake -E), which can perform basic operations without needing to write a script.
# File: CMakeLists.txt:
add_custom_command(OUTPUT | TARGET ...
COMMAND ${CMAKE_COMMAND} -E echo "<content-of-the-file>" > "</path/to/file>"
)
See also that question about redirecting output in add_custom_command: How to redirect the output of a CMake custom command to a file?.

Related

Using generator expression in `cmake -E copy` command

I am trying to copy dll files from my bin folder to a different folder. I want to copy files from bin/Debug when building in Debug and from bin/Release when building in Release. This is what I currently use (and which does not work).
file(GLOB library_files_debug ${outputdirectory_root}/Debug/*.dll)
file(GLOB library_files_release ${outputdirectory_root}/Release/*.dll)
add_custom_target(copy_dlls_to_wheel ALL
DEPENDS setup.py
COMMAND ${CMAKE_COMMAND} -E echo "Debug files: $<$<CONFIG:Debug>:${library_files_debug}>"
COMMAND ${CMAKE_COMMAND} -E echo "Release files: $<$<CONFIG:Release>:${library_files_release}>"
COMMAND ${CMAKE_COMMAND} -E echo "Destination dir: ${CMAKE_BINARY_DIR}/python/${PROJECT_NAME}"
COMMAND ${CMAKE_COMMAND} -E copy $<$<CONFIG:Debug>:${library_files_debug}> $<$<CONFIG:Release>:${library_files_release}> ${CMAKE_BINARY_DIR}/python/${PROJECT_NAME}
)
I am running on Windows 10, and use Visual Studio to build. When the above target copy_dlls_to_wheel is built in Debug, the first echo statement prints out the correct dll files, and the second echo is empty. However, no files are copied. Instead I get the error message The system cannot find the path specified.
I have also tried to replace the last line with
COMMAND ${CMAKE_COMMAND} -E copy $<$<CONFIG:Debug>:${library_files_debug}> ${CMAKE_BINARY_DIR}/python/${PROJECT_NAME}
, but I get the same result.
However, when I remove the generator expression, and use
COMMAND ${CMAKE_COMMAND} -E copy ${library_files_debug} ${CMAKE_BINARY_DIR}/python/${PROJECT_NAME}
the files are copied correctly to my output folder. I am pretty confident my generator expression is correct, since I get the expected output from the echo commands. Are generator expressions not supported when using cmake -E copy, or is there something else I am doing wrong?
CMake's command line copy is able to process multiple files when you simply provide a list, which is why this works:
COMMAND ${CMAKE_COMMAND} -E copy ${library_files_debug} ${CMAKE_BINARY_DIR}/python/${PROJECT_NAME}
This is expanded to a space-separated list of files when the copy command is ultimately executed, which is the expected syntax.
cmake.exe -E copy mylib1.dll mylib2.dll /your/binary/dir/python/proj
However, when wrapped in a generator expression, the list will not be interpreted correctly by CMake. While the generator expression will be evaluated correctly, the list will be kept as a semicolon-separated list of files, which is the incorrect syntax:
cmake.exe -E copy "mylib1.dll;mylib2.dll" /your/binary/dir/python/proj
This causes the copy command to fail.
To work-around this issue, you could loop over each DLL file you want to copy, if there aren't too many. Something like this could work:
# Loop through the Debug files.
foreach(cur_file ${library_files_debug})
get_filename_component(file_name ${cur_file} NAME)
add_custom_target(copy_dlls_to_wheel_debug_${file_name} ALL
DEPENDS setup.py
COMMAND ${CMAKE_COMMAND} -E echo "DLL file: ${cur_file}"
COMMAND ${CMAKE_COMMAND} -E echo "Destination dir: ${CMAKE_BINARY_DIR}/python/${PROJECT_NAME}"
COMMAND ${CMAKE_COMMAND} -E copy $<$<CONFIG:Debug>:${cur_file}> ${CMAKE_BINARY_DIR}/python/${PROJECT_NAME}
)
endforeach()
# Loop through the Release files.
foreach(cur_file ${library_files_release})
get_filename_component(file_name ${cur_file} NAME)
add_custom_target(copy_dlls_to_wheel_release_${file_name} ALL
DEPENDS setup.py
COMMAND ${CMAKE_COMMAND} -E echo "DLL file: ${cur_file}"
COMMAND ${CMAKE_COMMAND} -E echo "Destination dir: ${CMAKE_BINARY_DIR}/python/${PROJECT_NAME}"
COMMAND ${CMAKE_COMMAND} -E copy $<$<CONFIG:Release>:${cur_file}> ${CMAKE_BINARY_DIR}/python/${PROJECT_NAME}
)
endforeach()
A quicker solution might be to bundle up your DLLs, using CMake's tar command line utility, copy them, then extract them, as suggested in this answer. CMake's tar command does not seem to accept lists wrapped in generator expressions either, so the list of files to bundle together is written to a file.
file(GLOB library_files_debug ${outputdirectory_root}/Debug/*.dll)
file(GLOB library_files_release ${outputdirectory_root}/Release/*.dll)
# Write the filenames (not full path) of the files to pack to file
set(debug_content "")
set(release_content "")
foreach(lib_file ${library_files_debug})
get_filename_component(file_name ${lib_file} NAME)
set(debug_content "${debug_content}${file_name}\n")
endforeach(lib_file ${library_files_debug})
foreach(lib_file ${library_files_release})
get_filename_component(file_name ${lib_file} NAME)
set(release_content "${release_content}${file_name}\n")
endforeach(lib_file ${library_files_release})
set(filenames_debug ${outputdirectory_root}/debug_files.txt)
set(filenames_release ${outputdirectory_root}/release_files.txt)
file(WRITE ${filenames_debug} ${debug_content})
file(WRITE ${filenames_release} ${release_content})
# Read either the list of debug or release files, and pack files
add_custom_command(
TARGET bdist PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E tar "cfj" ${outputdirectory_root}/temp.tar --files-from="$<IF:$<CONFIG:Debug>,${filenames_debug},${filenames_release}>"
WORKING_DIRECTORY ${outputdirectory_root}/$<CONFIG>)
# Unpack the files in the folder building the python wheel
add_custom_command(
TARGET bdist PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E rename ${outputdirectory_root}/temp.tar temp.tar
COMMAND ${CMAKE_COMMAND} -E tar "xfj" temp.tar
COMMAND ${CMAKE_COMMAND} -E remove temp.tar
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/python/${PROJECT_NAME})

add_custom_command for copy *.y to *.txt file

I am trying to copy the generated *.y output (plane luma values, no header info) to text file using the add_custom_command CMake command. How can I add the copy command in add_custom_command?
I want to redirect the generated *.y output to text file. But the > redirection was not running directly in CMake without add_custom_command, so I tried to use add_custom_command. Here I thought to use copy instead of >,
but still in both cases I am not able to redirect or copy the *.y output to *.txt file.
macro(COMPARE file function type type_out)
get_filename_component(main_base_name ${file} NAME_WE)
set(main_base_name_mangled ${main_base_name}_${function}_${type_out})
# file generated by simulation
set(output_file ${function}_${type_out}_0_opt.y)
# reference file generated by generator
set(reference_file ${function}_${type_out}_0_ref.y)
set(output_file_txt ${function}_${type_out}_0_opt.txt)
set(reference_file_txt ${function}_${type_out}_0_ref.txt)
add_custom_target(compare_${main_base_name_mangled}
COMMAND cmp ${reference_file} ${output_file}
COMMENT "Comparing ${reference_file} ${output_file}")
add_dependencies(compare_${main_base_name_mangled}
simulate_${main_base_name_mangled})
add_test(NAME Compare_${main_base_name_mangled}
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR}
--target compare_${main_base_name_mangled})
# add dependency to compile first
set_tests_properties(Compare_${main_base_name_mangled}
PROPERTIES REQUIRED_FILES ${reference_file})
set_tests_properties(Compare_${main_base_name_mangled}
PROPERTIES REQUIRED_FILES ${output_file})
# test Compare_X depends on Simulate_X
set_tests_properties(Compare_${main_base_name_mangled}
PROPERTIES DEPENDS Simulate_${base_name_mangled})
#Here I have added my code to redirect my *.y output to *.txt file
add_custom_command(
OUTPUT ${output_file}
${CMAKE_COMMAND} -E copy ${output_file} ${output_file_txt}
MAIN_DEPENDENCY ${output_file}
COMMENT "Redirecting ${output_file} to ${output_file_txt}")
add_custom_command(
OUTPUT ${reference_file}
${CMAKE_COMMAND} -E copy ${reference_file} ${reference_file_txt}
MAIN_DEPENDENCY ${reference_file}
COMMENT "Redirecting ${reference_file} to ${reference_file_txt}")
endmacro()
After running this code, I want to generate two text files, which is still not possible with above CMake changes.
The OUTPUT directive in your add_custom_command calls should specify the file produced by the custom command (i.e. the output file), instead of the input file. Here is the fixed snippet from your code:
# Here I have added my code to redirect my *.y output to *.txt file
add_custom_command(
OUTPUT ${output_file_txt}
${CMAKE_COMMAND} -E copy ${output_file} ${output_file_txt}
MAIN_DEPENDENCY ${output_file}
COMMENT "Redirecting ${output_file} to ${output_file_txt}")
add_custom_command(
OUTPUT ${reference_file_txt}
${CMAKE_COMMAND} -E copy ${reference_file} ${reference_file_txt}
MAIN_DEPENDENCY ${reference_file}
COMMENT "Redirecting ${reference_file} to ${reference_file_txt}")

During build, how to create a directory for a file which is generated by some COMMAND?

I'm writing a custom command in CMake to convert a file to binary format at build time. However, the tool I'm using requires directory for a file to be pre-existed; it doesn't create a directory automatically.
Currently my CMake macro looks like this:
MACRO(ConvertToBinary _name)
ADD_CUSTOM_COMMAND(
OUTPUT ${CMAKE_BINARY_DIR}/${_name}.bin
COMMAND ${EXE_DIR}/toBinary -i ${CMAKE_CURRENT_SOURCE_DIR}/${_name} -o ${CMAKE_BINARY_DIR}/${_name}.bin
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_name}
)
ENDMACRO(ConvertToBinary)
This macro doesn't work for _name parameter equal, e.g., to "test/sample.txt", because the tool is run with test/ folder under ${CMAKE_BINARY_DIR} being not existed.
How to create the directory before the tool, generating the file, is run?
I have tried to overcome the problem by pre-creating the file (and directory) with the help of CMake. So the tool will be run with the directory created, and can override the output file. I have tried the following:
MACRO(ConvertToBinary _name)
ADD_CUSTOM_COMMAND(
OUTPUT ${CMAKE_BINARY_DIR}/${_name}.bin
FILE(WRITE ${CMAKE_BINARY_DIR}/${_name}.bin "Dummy")
COMMAND ${EXE_DIR}/toBinary -i ${CMAKE_CURRENT_SOURCE_DIR}/${_name} -o ${CMAKE_BINARY_DIR}/${_name}.bin
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_name}
)
ENDMACRO(ConvertToBinary)
But it did not work. I can't find information on how to create file at build time with CMake, so any help is appreciated.
In CMake you may extract directory component of the file and create this directory.
Below code can be used for a tool, which generates a file but doesn't create directory for it.
# Assume some tool generates file '${file}', but don't create directory for it.
#
# Extract directory component for a file
get_filename_component(_dir ${file} DIRECTORY)
add_custom_command(OUTPUT ${file}
COMMAND ${CMAKE_COMMAND} -E make_directory ${_dir} # Create output directory
COMMAND <tool-which-generates-file> ... ${file}
)
See also that question about ways for create a directory in CMake.

CMake not redirecting stderr with execute_process

I'm trying to redirect stdout and stderr to the same file using CMake. I'm using the execute_process option in CMake with the ERROR_FILE and OUTPUT_FILE option specified.
I'm successfully capturing the output, but the error is not there. What am I doing wrong?
File CMakeLists.txt
add_test(NAME test${ID}
COMMAND ${CMAKE_COMMAND}
-DEXE=../examples/test${exampleID}
-DID=${ID}
-DARGS=${args}
-P ${CMAKE_CURRENT_SOURCE_DIR}/Tester.cmake
)
File Tester.cmake
separate_arguments( ARGS )
# Run the test
execute_process(
COMMAND "${EXE}" ${ARGS}
ERROR_FILE test${ID}.out
OUTPUT_FILE test${ID}.out
)
Specifying the same file for both OUTPUT_FILE and ERROR_FILE has only recently been added in CMake 3.3. See release notes.
As a work-around for earlier versions, use the options OUTPUT_VARIABLE and ERROR_VARIABLE with the same variable and then write the contents of the variable to the file, e.g.:
execute_process(
COMMAND "${EXE}" ${ARGS}
ERROR_VARIABLE _testOut
OUTPUT_VARIABLE _testOut
)
file (WRITE "test${ID}.out" "${_testOut}")

CMake can I filter compiler output through another program

Can I tell CMake to pipe all stderr output from the compiler and/or linker through an external program and showing the output of that on the terminal instead?
For Makefile generators you can apply a filter to the compiler output by setting a custom launcher for compile rules. The following CMake list file sketches the necessary steps:
cmake_minimum_required(VERSION 2.8)
project (Main)
configure_file(
"${PROJECT_SOURCE_DIR}/gcc_filter.sh.in"
"${PROJECT_BINARY_DIR}/gcc_filter.sh"
#ONLY)
add_executable (Main Main.cpp)
set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${PROJECT_BINARY_DIR}/gcc_filter.sh")
In the project root directory add the following shell script template file gcc_filter.sh.in:
#!/bin/sh
# shell script invoked with the following arguments
# $(CXX) $(CXX_DEFINES) $(CXX_FLAGS) -o OBJECT_FILE -c SOURCE_FILE
# invoke compiler
exec "$#" 2>&1 | "#PROJECT_SOURCE_DIR#/myfilter.sh"
The actual shell script invoked by the custom launcher rule is first copied to the project binary directory with the configure_file call in the CMake list file. The executable bit of the file gcc_filter.sh.in must be set. The shell script forwards all arguments to the compiler and then pipes the compiler output to another shell script myfilter.sh in the project root directory:
#!/bin/sh
exec wc
The example myfilter.sh just invokes wc but more elaborate output filtering can be easily done using the above recipe.