I am a newbie to cmake. This is what I'm trying to achieve:
- Run a build command that would generate an elf file.
- Trigger a python script to run after the elf has been generated - as it takes the elf as an input.
- Return a code from python script to the cmake build system.
- Store the code in a variable in cmake and print a message depending on the code.
I understand that cmake has mainly 2 stages - configuration and build. The issue I'm facing is, for reading a code from the script, I see the only option to do this is by using execute_process(). But execute_process() seems to run in configuration phase itself. Is there any way I can achieve what I want to do above?
I also tried to use a function to wrap my execute_process(), cause this is what I read about functions: "The commands in the function definition are recorded; they are not executed until the function is invoked." But it says it cannot find the elf file cause it's not generated yet.
Another solution I have tried is to write the returned value to a file - which I can. But I won't be able to read the file and store it in a variable during build phase.
I ran below code, but it says the elf file isn't generated yet.
CMakeLists.txt:
py_script()
add_custom_target(
target_name ALL
)
#dependency on elf file being generated
add_dependencies(target_name ${elf_target})
file.cmake:
function(py_script)
execute_process(
COMMAND py_script.py elf_file.elf
OUTPUT_VARIABLE ret_val
)
if (${ret_val} EQUAL 0)
message(STATUS "Pass.")
else()
message(STATUS "Fail.")
endif()
endfunction()
py_script.pyto be some sort of a test. CMake supports tests out of the box. Just callenable_testing()near the beginning of yourCMakeLists.txtscript, and specify each test with add_test command. E.g. in your case the test could be defined asadd_test(NAME test1 COMMAND py_script.py elf_file.elf). After configuring your project (cmake) and building (make) you may runctestfrom the build directory. CTest will run all your tests and nicely print their results.file.cmakescriptfunction()andendfunction()lines (thus leaving only the function's body), and define the target in yourCMakeListst.txtasadd_custom_target(target_name ALL ${CMAKE_COMMAND} -P file.cmake). That way your CMake script will be run at the build stage.