用CMake扩展setuptools未安装构建扩展



我正在尝试使用CMake在setup.py.

中构建一个fortran扩展。当我运行pip install . -v时,我可以看到我的扩展构建得很好,但是然后二进制文件(.so文件)没有安装在将尝试导入它的python脚本旁边。我怎么能保证我的扩展安装?

这里是我的setup.py供参考

import os
import pathlib
from setuptools import setup, Extension
from setuptools.command.build_ext import build_ext as build_ext_orig
class CMakeExtension(Extension):
def __init__(self, name):
# don't invoke the original build_ext for this special extension
super().__init__(name, sources=[])

class build_ext(build_ext_orig):
def run(self):
for ext in self.extensions:
self.build_cmake(ext)
super().run()
def build_cmake(self, ext):
cwd = pathlib.Path().absolute()
# these dirs will be created in build_py, so if you don't have
# any python sources to bundle, the dirs will be missing
build_temp = pathlib.Path(self.build_temp)
build_temp.mkdir(parents=True, exist_ok=True)
extdir = pathlib.Path(self.get_ext_fullpath(ext.name))
extdir.mkdir(parents=True, exist_ok=True)

# example of cmake args
config = 'Debug' if self.debug else 'Release'
cmake_args = [
'-DCMAKE_LIBRARY_OUTPUT_DIRECTORY='+str(extdir.parent.absolute()),
'-DCMAKE_BUILD_TYPE=' + config,
'-DBUILD_PYTHON_MODULE=ON',
'-DBUILD_FORTRAN_LIBRARY=OFf',
'-DBUILD_THE_EXAMPLES=OFF',
]

# example of build args
build_args = [
'--config', config, 
]

os.chdir(str(build_temp))
self.spawn(['cmake', str(cwd)] + cmake_args)
if not self.dry_run:
self.spawn(['cmake', '--build', '.'] + build_args)
# Troubleshooting: if fail on line above then delete all possible 
# temporary CMake files including "CMakeCache.txt" in top level dir.
os.chdir(str(cwd))

setup(name = 'mypythonlibrary',
packages=['mypythonlibrary'],
version='0.1',
ext_modules=[CMakeExtension('mypythonlibrary/myfortranlibrary')],
cmdclass={'build_ext': build_ext,})

最好只使用skbuild。他们有c++, cython, c的例子。我只是把f2py/fortran的一些例子放在一起:https://github.com/Nicholaswogan/skbuild-f2py-examples

最新更新