Thursday, April 14, 2022

[SOLVED] How to run a python script from CMakeLists.txt

Issue

I have a DDS project that takes IDL files and generates C++ header and footer files from them using a "ProcessIDL.py" script. Previously this was done in as a pre build step in Visual Studio. I am in the process of converting the project to CMake to come in line with other repositories so thought it made sense to run the ProcessIDL.py as part of the cmake build step.

I have tried using execute_process but it doesn't seem to be doing anything, no error, no output from the python script so I don't think it is being run at all. Here is my attempt:

include(FindPythonInterp)
set (py_cmd "python ProcessIDL.py")
message(STATUS ${PYTHON_EXECUTABLE})

execute_process(
                  COMMAND ${py_cmd}
                  WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/DdsIdl
                  ERROR_VARIABLE error
                  OUTPUT_VARIABLE output
               )

I have also tried this:

include(FindPythonInterp)
set (py_cmd "python ProcessIDL.py")
message(STATUS ${PYTHON_EXECUTABLE})

execute_process(
                  COMMAND ${PYTHON_EXECUTABLE} ${py_cmd}
                  WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/DdsIdl
                  ERROR_VARIABLE error
                  OUTPUT_VARIABLE output
               )

The "error" and "output" variables are empty.

I am pretty stumped so any tips would be appreciated!!


Solution

This was solved by changing the working directory:

execute_process(COMMAND python ProcessIDL.py 
    WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR})


Answered By - tashywashyy
Answer Checked By - Gilberto Lyons (WPSolving Admin)