使用CMake与setup.py [英] Using CMake with setup.py

查看:515
本文介绍了使用CMake与setup.py的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于一个项目,我构建一个C库,并通过CMake隐藏Python绑定(通过GObject内省)。我还想使用distutils分发一些Python帮助程序模块。我可以使用 CMakeLists.txt

  set_program(PYTHONpython)

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(CODEexecute_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 = {'':不幸的是,很多人认为这是一个非常有趣的事情,每次运行 make 时,都会执行构建步骤。我想,问题是与自定义命令的输出相关,这是一个目录,而不是一个文件。现在,有没有办法告诉CMake只有当 setup.py.in 或时运行 python setup.py build 其中一个源已更改?

解决方案

只有文件而不是目录才能可靠地用作OUTPUT和DEPENDS。您可以修改自定义命令以生成时间戳文件,如下所示:

  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)


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()

and the following 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'])

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?

解决方案

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)

这篇关于使用CMake与setup.py的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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