Cmake compiling python into build folder

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-06 07:53:45

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/12272832/cmake-compiling-python-into-build-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!