use cmake configure_file to copy recursive files - cmake

I wanna using configure_file(<input> <output> COPYONLY) to copy a specific directory files recursive , I used this command:
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/res/* ${CMAKE_CURRENT_BINARY_DIR}/res COPYONLY)
but I got some errors , How I should use this command for this porpuse? (I looked at this question too), the Errors:
CMake Error at CMakeLists.txt:86 (configure_file):
configure_file Problem configuring file
I know that I can use it with file(...) too , but I wanna use configure_file(...):
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/res/ DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/res)

I don't think it is possible to use configure_file to copy a folder directly.
The command is about
copy a file to another location and modify its contents.
It operates on one file only.
https://cmake.org/cmake/help/latest/command/configure_file.html

Related

CMake custom command always executes

In my CMakeLists.txt, I define a custom target and command:
add_custom_command(
OUTPUT
${CMAKE_CURRENT_SOURCE_DIR}/input.csv
${CMAKE_CURRENT_SOURCE_DIR}/output1.csv
${CMAKE_CURRENT_SOURCE_DIR}/output2.csv
COMMAND python3
${CMAKE_CURRENT_SOURCE_DIR}/tests/genVectors.py)
add_custom_target(TEST_VECTORS
DEPENDS
${CMAKE_CURRENT_SOURCE_DIR}/input.csv
${CMAKE_CURRENT_SOURCE_DIR}/output1.csv
${CMAKE_CURRENT_SOURCE_DIR}/output2.csv)
add_executable(VectorTest tests/VectorTest.cpp)
add_dependencies(VectorTest TEST_VECTORS)
It always generates new CSV files even though the files exist. I only need to generate the vectors (with genVectors.py python file) if they do not exist. Is that something wrong with my configuration?
The OUTPUT option of add_custom_command does not guarantee that the generated files are placed here; it just tells CMake that the generated files are expected to be placed there. It is likely that your python script is generating files at a relative path, so they are just being placed somewhere in your CMake binary directory (your build folder). So while your files may be generated correctly, your custom target doesn't see them because it is looking in CMAKE_CURRENT_SOURCE_DIR. Thus, the custom target will always trigger the custom command to re-run.
CMake runs add_custom_command from the CMAKE_CURRENT_BINARY_DIR by default, but you can change it to run from CMAKE_CURRENT_SOURCE_DIR by adding the WORKING_DIRECTORY option. This way, the generated files will be placed at the expected location, and achieve your desired behavior. Try something like this:
add_custom_command(
OUTPUT
${CMAKE_CURRENT_SOURCE_DIR}/input.csv
${CMAKE_CURRENT_SOURCE_DIR}/output1.csv
${CMAKE_CURRENT_SOURCE_DIR}/output2.csv
COMMAND python3
${CMAKE_CURRENT_SOURCE_DIR}/tests/genVectors.py
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
add_custom_target(TEST_VECTORS
DEPENDS
${CMAKE_CURRENT_SOURCE_DIR}/input.csv
${CMAKE_CURRENT_SOURCE_DIR}/output1.csv
${CMAKE_CURRENT_SOURCE_DIR}/output2.csv)
add_executable(VectorTest tests/VectorTest.cpp)
add_dependencies(VectorTest TEST_VECTORS)
You can try to generate your file at time of configuration(i.e. while calling cmake). By this way it will created only once.
You can remove add_custom_command and use execute_process to create your files.

CMake Error: include could not find load file: targets

I'm trying to run AmazonFreeRTOS on my ESP32 (at Windows). After creating build folder in my amazon-freertos main folder I've tried to build it from main folder with
cmake --build .\build
The Error I've got is
include could not find load file: targets
However, there is a idf_functions.cmake file that contains include(targets) command, and the targets.cmake file is in the same folder so I don't know why the error occured.
If you pay close attention to the error, you'd notice the full error says something like:
CMake Error at
your-amazon-freertos-directory/vendors/espressif/esp-idf/tools/cmake/idf_functions.cmake: 26 (include)
include could not find load file:
targets
This is because idf_functions.cmake sets the variable IDF_PATH to $ENV{IDF_PATH} which was configured in ~/.profile when the line export IDF_PATH=~/esp/esp-idf was added, as seen here.
If you navigate to ~/esp/esp-idf/tools/cmake/ you'd notice that files like target.cmake and ldgen.cmake, which are being included <your-amazon-freertos-directory>/vendors/espressif/esp-idf/tools/cmake/idf_functions.cmake, do not exist.
Solution 1 (somewhat hacky):
Copy the contents of <your-amazon-freertos-directory>/vendors/espressif/esp-idf/tools/cmake/ to ~/esp/esp-idf/tools/cmake/
Solution 2:
Modify the ~/.profile file to add the following lines instead of that suggested in the guide:
export IDF_PATH=~/<your-amazon-freertos-directory>/vendors/espressif/esp-idf/
export PATH="$PATH:$IDF_PATH/tools"
This should circumvent any CMake include errors during generation of build files and during build.
Since Amazon FreeRTOS supports many different platforms in addition to ESP32, you might need to supply additional commands to tell CMake that ESP32 is the target you want to build.
Try using
cmake -DVENDOR=espressif -DBOARD=esp32_wrover_kit -DCOMPILER=xtensa-esp32 -S . -B your-build-directory
from your top level folder to generate your makefiles into the build folder, and then switching to your build folder and calling
make all
(From the "Build, Flash, and Run the Amazon FreeRTOS Demo Project" section of
https://docs.aws.amazon.com/freertos/latest/userguide/getting_started_espressif.html)

CMake cannot follow symbolic links

Update: turns out the problem is not related to the Jenkins agent but to CMake. It is easily reproducible from the command line.
It was reported here once before:
CMake cannot follow symlinks on Windows 10
However, the problem is not OS-related. We encounter it on Linux also.
I'm debugging the following error:
CMake Error at C:/jenkins/trial/workspace/WWB6-6.13.0/wwb-Pilot_Build_BB/wwb6/build/cmake_install.cmake:48 (file):
file INSTALL cannot read symlink
"C:/jenkins/trial/workspace/WWB6-6.13.0/wwb6-Build-Pilot-Windows/wwb6/wwb6/HelpFiles"
to duplicate at
"C:/jenkins/trial/workspace/WWB6-6.13.0/wwb6-Build-Pilot-Windows/wwb6/build/_CPack_Packages/win64/NSIS/WWB6 Setup/./Help".
The relevant line in the make file is:
file(INSTALL DESTINATION "${CMAKE_INSTALL_PREFIX}/./Help" TYPE DIRECTORY FILES "C:/jenkins/trial/workspace/WWB6-6.13.0/wwb-Pilot_Build_BB/wwb6/wwb6/HelpFiles/")
The intent is to copy the contents of the HelpFiles directory into a new directory "Help" in the workspace. The HelpFiles directory in this scenario is a symbolic link to another directory in the source repository:
Directory of C:\jenkins\trial\workspace\WWB6-6.13.0\wwb6-Build-Pilot-Windows\wwb6\wwb6
05/17/2019 03:09 PM <SYMLINKD> HelpFiles [..\helpwwb6]
Traversing this symlink from the command line works fine:
C:\jenkins\trial\workspace\WWB6-6.13.0\wwb6-Build-Pilot-Windows\wwb6\wwb6>cd HelpFiles
C:\jenkins\trial\workspace\WWB6-6.13.0\wwb6-Build-Pilot-Windows\wwb6\wwb6\HelpFiles>
Anyone know of a workaround for this problem? The version of CMake we're running is 3.12.0.
Fortunately, there's an easy (although not elegant) workaround for this: replace the symlink with the real path in the CMakeLists file:
install ( DIRECTORY ${CMAKE_SOURCE_DIR}/../helpwwb6/
DESTINATION ${BINARY_INSTALL_LOCATION}/Help
COMPONENT Runtime
PATTERN ".svn" EXCLUDE
PATTERN ".git" EXCLUDE
)
Would rather see CMake work with symlinks though.

What is the difference between install(FILES) and file(INSTALL)?

I'm trying to move a file to a specific location, and I did it like this:
file(INSTALL file.txt DESTINATION ../install_dir)
This worked fine. This moved file.txt to the specified destination.
However then I tried like this:
install(FILES ./file.txt DESTINATION ./install_dir)
Using install(FILES) doesn't copy files like I expect. The file is not installed at that location when I run the CMake configure command.
Can someone please explain the difference to me? Why is it that file(INSTALL) works when running the configure command, but install(FILES) doesn't?
The two commands do different things. install(FILES fil DESTINATION dest) instructs CMake to generate a build rule so that file fil is copied into dest when running the install step (make install or equivalent).
file(INSTALL ...) is evaluated immediately at configure time, while CMake is parsing the CMakeLists.txt file. Note that this signature is primarily intended for CMake's internal implementation of the above mentioned installation step: it prints install-themed status messages etc. If you just want to copy a file at configure time, you might want to prefer file(COPY) or file(COPY_IF_DIFFERENT).

How to best handle data files with CMake?

I've got a CMake project that contains code and a few data files (images to be precise).
My directory structure is like this:
src
data
src contains the source code, data the data files. CMake suggests out of source builds, so when I invoke make, I have the executable program, but not the data files, thus I cannot execute the program.
Of course, make install would copy my data files to the required location and make it work, therefore I develop like this right now:
cmake -DCMAKE_INSTALL_DIR=dist
<edit source code>
make install
dist/myprogram.exe
That's okay if I'm working with the command line and an editor, but I recently decided to move to Eclipse CDT. Generating an Eclipse project from CMake works great, but manually executing the install target from Eclipse is not so nice.
How do you people tackle this problem? Does your program have some clever algorithms to try and find its data directory even if it's not where the binary is? Or do you not use out of source builds?
configure_file should solve that problem.
I have a CMakeLists.txt file in my data directory which contains the following:
configure_file(data_file ${CMAKE_CURRENT_BINARY_DIR}/data_file COPYONLY)
This copies the specified file into the build directory when cmake is invoked, so it is available in the same location even in out of source builds.
configure_file does not support directories however while the file command does:
file(COPY assets DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
And if copying the files takes too much time (they are images...) you could make it even better by creating a "custom" data_header.h with configure_file which contains the paths to the data still in your source-directory.
This is what I do: I have a file "global_build_config.h.in" in my source, containing the following:
const char* const global_testdatapath = "#Test_Data_Path#";
and then use configure_file in CMake:
# Assume CMake knows a variable Test_Data_Path, it will be filled in automatically
# in the generated config/Global_Build_Config.h
configure_file( Global_Build_Config.h.in ${CMAKE_BINARY_DIR}/config/Global_Build_Config.h )
# The config directory should be added as a include-searchpath
include_directories( ${CMAKE_BINARY_DIR}/config/ )
I can then #include "Global_Build_Config.h" in my cpp files and refer to the fixed path.
Your question is a bit old, but in case you're still interested (or someone else), I have a similar scenario where I copy testdata for a unit-test target:
add_custom_command( TARGET ${UTEST_EXE_NAME}
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E echo "Copying unit test data.."
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_HOME_DIRECTORY}/utest/testdata ${CMAKE_BINARY_DIR}
)
So the main idea is to use a post-build target, and it is executed after each build. For me, it's not much data, and the filesystem caches it, so I don't feel the copy process at all. You could probably enhance this by copying with copy_if_different. In that case, however, you have to create a list of your image files and write a loop, because the command is file based. With the GLOB command, this shouldn't be hard to do if you need to.