我正在尝试按照https://bloerg.net/2012/11/10/cmake-and-distutils.html的结构与Cython绑定一起构建C ++库。
问题是在make install
期间,扩展名将被编译两次。当主文件夹中只有一个主CMakeLists.txt
(调整了路径)时,不会发生这种双重编译。详细信息如下:
我的项目结构是
.
├── CMakeLists.txt
├── python
│ ├── CMakeLists.txt
│ ├── a_py.pxd
│ ├── a_py.pyx
│ └── setup.py.in
└── src
├── A.cpp
└── A.h
顶层CMakeLists.txt
仅包含add_subdirectory(python)
。
python/CMakeLists.txt
是
IF(NOT ${PYTHON})
find_program(PYTHON "python")
ENDIF()
set(SETUP_PY_IN "${CMAKE_CURRENT_SOURCE_DIR}/setup.py.in")
set(SETUP_PY "${CMAKE_CURRENT_BINARY_DIR}/setup.py")
set(PY_OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/build/pytimestamp")
configure_file(
${SETUP_PY_IN}
${SETUP_PY}
)
add_custom_command(OUTPUT "${PY_OUTPUT}"
COMMAND ${PYTHON} ${SETUP_PY} build_ext
COMMAND ${CMAKE_COMMAND} -E touch ${PY_OUTPUT}
)
add_custom_target(a_py ALL DEPENDS ${PY_OUTPUT})
install(CODE "execute_process(COMMAND ${PYTHON} ${SETUP_PY} install)")
setup.py
是:
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
ext_modules = [
Extension(
name="a",
sources=["${CMAKE_CURRENT_SOURCE_DIR}/a_py.pyx", "${CMAKE_CURRENT_SOURCE_DIR}/../src/A.cpp"],
include_dirs = ['${CMAKE_CURRENT_SOURCE_DIR}/../src'],
language="c++",
),
]
setup(
name = 'a',
version='${PROJECT_VERSION}',
cmdclass = {'build_ext': build_ext},
ext_modules = ext_modules,
package_dir={ 'a': '${CMAKE_CURRENT_SOURCE_DIR}' },
)
在两种情况下(CMakeFile.txt
位于根目录或python
子文件夹中),首先运行build_ext
步骤:
Scanning dependencies of target a_py [100%] Generating build/pytimestamp running build_ext
并编译生成的a_py.cpp
和A.cpp
并链接库。
在安装步骤中,仅当CMakeFile.txt
位于python
子文件夹中时,再次运行编译。
这是在安装过程中发生的情况:
running build_ext skipping '/Users/xxx/tmp/ctest/t08/python/a_py.cpp' Cython extension (up-to-date) building 'a' extension creating build
请注意,a_py.pyx
不会再次被cythonized,但是将重新创建构建目录(在构建和安装步骤之间是相同的),并且将编译文件(使用完全相同的编译器和链接器调用)
可以在此处找到完整的示例:https://github.com/zeeMonkeez/cmakeCythonTest
答案 0 :(得分:0)
我有同样的问题。显然,就我而言,在安装阶段setup.py
在查找将编译后的扩展放入构建阶段的目录时遇到了麻烦,因此即使使用--skip-build
开关也可以重新编译它们。
我通过分别在--build-lib
和--build-dir
开关的构建和安装阶段指定相同的目录路径来解决:
add_custom_command(OUTPUT ${PY_OUTPUT}
COMMAND ${PYTHON} ${SETUP_PY} build_ext --build-lib ${CMAKE_CURRENT_BINARY_DIR}/mysoext
COMMAND ${CMAKE_COMMAND} -E touch ${PY_OUTPUT}
DEPENDS ${DEPS}
)
...
set(MYINSTCMD "\
EXECUTE_PROCESS(COMMAND ${PYTHON} ${SETUP_PY} install_lib \
--skip-build \
--install-dir /my/install/dir \
--build-dir ${CMAKE_CURRENT_BINARY_DIR}/mysoext)")
install(CODE "${MYINSTCMD}")