Cmake编译python到构建文件夹 [英] Cmake compiling python into build folder

查看:1086
本文介绍了Cmake编译python到构建文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为一个例子是最好的方式来证明我的问题:

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

假设我有一个名为src的源目录。此目录有两个文件,一个CMakeLists.txt文件和一个名为blah的python文件。在存储src的相同位置,有一个名为build的文件夹。现在,我想能够运行cmake,并让它编译我的python文件blah,并将其放入我的构建目录。目前,我的CMakeLists.txt只是使用调用将未编译的代码直接复制到我的构建目录:

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)

终端编译实际文件。这留下uncompiled python文件在我的构建目录,这是不好的。我尝试使用ADD_EXECUTABLES,创建自定义目标和使用COMMAND,但我的语法必须在某处。任何帮助将非常感激。

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!

推荐答案

CMake不支持Python开箱即用AFAIK。因为这个,你需要编写规则来生成.py文件,类似这样:

CMake doesn't support Python out of the box, AFAIK. because of this, you will need to write rule to build .py 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()

定义后,方式:

add_python_target(blabla blah.py)

如果您想进行调整或需要更多信息,请参阅 CMake文档

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

这篇关于Cmake编译python到构建文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆