在 setup.py 中使用 CMake [英] Using CMake with setup.py
问题描述
对于一个项目,我构建了一个 C 库并使用 CMake 隐含 Python 绑定(通过 GObject 自省).我还想使用 distutils 分发一些 Python 帮助程序模块.我可以使用这个 CMakeLists.txt
For a project I build a C library and implict Python bindings (via GObject introspection) with CMake. I also want to distribute some Python helper modules using distutils. I am able to build and install the module with this CMakeLists.txt
find_program(PYTHON "python")
if (PYTHON)
set(SETUP_PY_IN "${CMAKE_CURRENT_SOURCE_DIR}/setup.py.in")
set(SETUP_PY "${CMAKE_CURRENT_BINARY_DIR}/setup.py")
set(DEPS "${CMAKE_CURRENT_SOURCE_DIR}/module/__init__.py")
set(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/build")
configure_file(${SETUP_PY_IN} ${SETUP_PY})
add_custom_command(OUTPUT ${OUTPUT}
COMMAND ${PYTHON}
ARGS setup.py build
DEPENDS ${DEPS})
add_custom_target(target ALL DEPENDS ${OUTPUT})
install(CODE "execute_process(COMMAND ${PYTHON} ${SETUP_PY} install)")
endif()
和以下setup.py.in
:
from distutils.core import setup, Extension
if __name__ == '__main__':
setup(name='foo',
version='${PACKAGE_VERSION}',
package_dir={ '': '${CMAKE_CURRENT_SOURCE_DIR}' },
packages=['module'])
不幸的是,每次运行 make
时都会执行构建步骤.我想,问题与自定义命令的输出有关,该命令是目录而不是文件.现在,有什么方法可以告诉 CMake 仅在 setup.py.in
或其中一个源发生更改时才运行 python setup.py build
吗?
Unfortunately, the build step is executed each time I run make
. I guess, the problem is related to the output of the custom command which is a directory rather than a file. Now, is there any way to tell CMake to run python setup.py build
only when setup.py.in
or one of the sources changed?
推荐答案
只有文件,而不是目录,才能可靠地用作 OUTPUT 和 DEPENDS.您可以修改自定义命令以生成时间戳文件,如下所示:
Only files, not directories, can be reliably used as OUTPUT and DEPENDS. You could modify your custom command to also produce a timestamp file, something like this:
add_custom_command(
OUTPUT ${OUTPUT}/timestamp
COMMAND ${PYTHON} setup.py build
COMMAND ${CMAKE_COMMAND} -E touch ${OUTPUT}/timestamp
DEPENDS ${DEPS}
)
add_custom_target(target ALL DEPENDS ${OUTPUT}/timestamp)
这篇关于在 setup.py 中使用 CMake的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!