0

I am a newbie to cmake. This is what I'm trying to achieve:

  1. Run a build command that would generate an elf file.
  2. Trigger a python script to run after the elf has been generated - as it takes the elf as an input.
  3. Return a code from python script to the cmake build system.
  4. 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()
2
  • According to the messages you print, you want running py_script.py to be some sort of a test. CMake supports tests out of the box. Just call enable_testing() near the beginning of your CMakeLists.txt script, and specify each test with add_test command. E.g. in your case the test could be defined as add_test(NAME test1 COMMAND py_script.py elf_file.elf). After configuring your project (cmake) and building (make) you may run ctest from the build directory. CTest will run all your tests and nicely print their results. Commented Jun 7 at 7:42
  • As for your described steps, at the build stage you cannot create a CMake variable for configuration context: this context is lost. But at the build stage you may execute a CMake script, in the same way your are executing a Python one. E.g. remove from your file.cmake script function() and endfunction() lines (thus leaving only the function's body), and define the target in your CMakeListst.txt as add_custom_target(target_name ALL ${CMAKE_COMMAND} -P file.cmake). That way your CMake script will be run at the build stage. Commented Jun 7 at 8:00

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.