enscons软件包似乎旨在完成所提问题。使用它来构建带有 C 扩展的包的示例在这里。
你可以有一个基本的包结构,比如:
pkgroot/
pyproject.toml
setup.py
SConstruct
README.md
pkgname/
__init__.py
pkgname.py
cfile.c
在这个pyproject.toml
文件可能看起来像:
[build-system]
requires = ["enscons"]
[tool.enscons]
name = "pkgname"
description = "My nice packahe"
version = "0.0.1"
author = "Me"
author_email = "me@me.com"
keywords = ["spam"]
url = "https://github.com/me/pkgname"
src_root = ""
packages = ["pkgname"]
其中该[tool.enscons]
部分包含 setuptools/distutilssetup
函数熟悉的许多内容。从这里复制,该setup.py
函数可能包含以下内容:
#!/usr/bin/env python
# Call enscons to emulate setup.py, installing if necessary.
import sys, subprocess, os.path
sys.path[0:0] = ['setup-requires']
try:
import enscons.setup
except ImportError:
requires = ["enscons"]
subprocess.check_call([sys.executable, "-m", "pip", "install",
"-t", "setup-requires"] + requires)
del sys.path_importer_cache['setup-requires'] # needed if setup-requires was absent
import enscons.setup
enscons.setup.setup()
最后,该SConstruct
文件可能类似于:
# Build pkgname
import sys, os
import pytoml as toml
import enscons, enscons.cpyext
metadata = dict(toml.load(open('pyproject.toml')))['tool']['enscons']
# most specific binary, non-manylinux1 tag should be at the top of this list
import wheel.pep425tags
full_tag = next(tag for tag in wheel.pep425tags.get_supported() if not 'manylinux' in tag)
env = Environment(tools=['default', 'packaging', enscons.generate, enscons.cpyext.generate],
PACKAGE_METADATA=metadata,
WHEEL_TAG=full_tag)
ext_filename = os.path.join('pkgname', 'libcfile')
extension = env.SharedLibrary(target=ext_filename,
source=['pkgname/cfile.c'])
py_source = Glob('pkgname/*.py')
platlib = env.Whl('platlib', py_source + extension, root='')
whl = env.WhlFile(source=platlib)
# Add automatic source files, plus any other needed files.
sdist_source=list(set(FindSourceFiles() +
['PKG-INFO', 'setup.py'] +
Glob('pkgname/*', exclude=['pkgname/*.os'])))
sdist = env.SDist(source=sdist_source)
env.Alias('sdist', sdist)
install = env.Command("#DUMMY", whl,
' '.join([sys.executable, '-m', 'pip', 'install', '--no-deps', '$SOURCE']))
env.Alias('install', install)
env.AlwaysBuild(install)
env.Default(whl, sdist)
在此之后你应该能够运行
sudo python setup.py install
编译 C 扩展并构建一个轮子,并安装 python 包,或者
python setup.py sdist
构建源代码分发。
我认为你基本上可以用SConstruct
文件中的 SCons 做任何你可以做的事情。