Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cmake compiling python into build folder

I think an example would be the best way to demonstrate my problem:

Lets say I have a source directory named src. This directory has two files, a CMakeLists.txt file and a python file named blah. In the same place where src is stored, there is a folder named build. Now, I would like to be able to run cmake and make it compile my python file blah and place it into my build directory. Currently, my CMakeLists.txt is just copying the uncompiled code straight into my build directory using the call:

CONFIGURE_FILE(${PATH_TO_SOURCE}/blah.py ${PATH_TO_BUILD}/blah.py COPYONLY)

Then I run make in the terminal to compile the actual file. This leaves the uncompiled python file in my build directory, which is no good. I've tried using ADD_EXECUTABLES, creating a custom target and using COMMAND, but my syntax must be off somewhere. Any help would be greatly appreciated!

like image 961
user1553248 Avatar asked Dec 07 '25 02:12

user1553248


1 Answers

CMake doesn't support Python out of the box, AFAIK. Because of this, you will need to write rule to build python files, something like that:

macro(add_python_target tgt)
  foreach(file ${ARGN})
    set(OUT ${CMAKE_CURRENT_BINARY_DIR}/${file}.pyo)
    list(APPEND OUT_FILES ${OUT})
    add_custom_command(OUTPUT ${OUT}
        COMMAND <python command you use to byte-compile .py file>)
  endforeach()

  add_custom_target(${tgt} ALL DEPENDS ${OUT_FILES})
endmacro()

After defining it, you can use it this way:

add_python_target(blabla blah.py)

If you want to make adjustmenets or need more information, see CMake documentation.

like image 137
arrowd Avatar answered Dec 08 '25 15:12

arrowd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!