英文:
How to pass a user defined argument to setuptools in order to set a flag that changes compilation macro
问题
Here's the translated code portion you provided:
我有一个很大的 setup.py 文件,编译了几个 CUDA 文件,类似于(非常不完整,如果相关,我可以提供更多信息):
gpuUtils_ext = Extension(
"_gpuUtils",
sources=include_headers(
[
"gpuUtils.cu",
"python/utilities/cuda_interface/_gpuUtils.pxd",
"python/utilities/cuda_interface/_gpuUtils.pyx",
],
sdist=sys.argv[1] == "sdist",
),
define_macros=[("MACRO_I_WANT", None)],
library_dirs=[CUDA["lib64"]],
libraries=["cudart"],
language="c++",
runtime_library_dirs=[CUDA["lib64"]] if not IS_WINDOWS else None,
include_dirs=[NUMPY_INCLUDE, CUDA["include"], "./CUDA/"],
)
# etc
setup(
name="-",
version="-",
author="-",
packages=find_packages(),
include_package_data=True,
data_files=[("data", ["../data/somefile.file"])],
ext_modules=[foo1, foo2, gpuUtils_ext, foo3], # 我有很多
py_modules=["foo.py"],
cmdclass={"build_ext": BuildExtension},
install_requires=["Cython", "matplotlib", "numpy", "scipy", "tqdm"],
license_files=("LICENSE",),
license="BSD 3-Clause",
# 由于包含 C 代码,不能压缩 egg
zip_safe=False,
)
Please note that I've only translated the code part, as requested. If you have any further questions or need assistance with this code, feel free to ask.
英文:
I have some large setup.py file that compiles several CUDA files, something like (VERY INCOMPETE, I can provide more info if its relevant):
gpuUtils_ext = Extension(
"_gpuUtils",
sources=include_headers(
[
"gpuUtils.cu",
"python/utilities/cuda_interface/_gpuUtils.pxd",
"python/utilities/cuda_interface/_gpuUtils.pyx",
],
sdist=sys.argv[1] == "sdist",
),
define_macros=[("MACRO_I_WANT", None)],
library_dirs=[CUDA["lib64"]],
libraries=["cudart"],
language="c++",
runtime_library_dirs=[CUDA["lib64"]] if not IS_WINDOWS else None,
include_dirs=[NUMPY_INCLUDE, CUDA["include"], "./CUDA/"],
)
# etc
setup(
name="-",
version="-",
author="-",
packages=find_packages(),
include_package_data=True,
data_files=[("data", ["../data/somefile.file"])],
ext_modules=[foo1, foo2, gpuUtils_ext, foo3], # I have many
py_modules=["foo.py"],
cmdclass={"build_ext": BuildExtension},
install_requires=["Cython", "matplotlib", "numpy", "scipy", "tqdm"],
license_files=("LICENSE",),
license="BSD 3-Clause",
# since the package has c code, the egg cannot be zipped
zip_safe=False,
)
The file gpuUtils.cu
, that is being compiled in this setup.py
has a macro MACRO_I_WANT
that its defined here and the inside the file there is a #ifdef
to disable a piece of code.
I would like to change setuptools such that the user can provide a flag for this macro, e.g. python setup.py install
would not define the macro, but python setup.py intall -define-macro
would define it.
As far as I can see/test, the general option of https://stackoverflow.com/questions/677577/distutils-how-to-pass-a-user-defined-parameter-to-setup-py does not work, because by the time InstallCommand
is called, my Extensions
have already been defined and passed to setup
.
Is this doable? How can I do it? Is this the right way of approaching it?
答案1
得分: 0
import sys
用于编译的宏
define_macros=[("SOME_MACRO", None)]
my_flag=False
if len(sys.argv)>1:
if "-define-macro " in sys.argv[1:] :
my_flag=True
sys.argv.pop(sys.argv.index("-define-macro"))
else:
raise ValueError("flag not understood, only -define-macro accepted")
if my_flag:
define_macros.append(("MACRO_I_WANT",None))
然后,在扩展部分:
gpuUtils_ext = Extension(
"_gpuUtils",
sources=include_headers(
[
"gpuUtils.cu",
"python/utilities/cuda_interface/_gpuUtils.pxd",
"python/utilities/cuda_interface/_gpuUtils.pyx",
],
sdist=sys.argv[1] == "sdist",
),
define_macros=define_macros, # 修改了这一部分
library_dirs=[CUDA["lib64"]],
libraries=["cudart"],
language="c++",
runtime_library_dirs=[CUDA["lib64"]] if not IS_WINDOWS else None,
include_dirs=[NUMPY_INCLUDE, CUDA["include"], "./CUDA/"],
)
英文:
Ended up grabbing the system argument and poping it out:
import sys
# Macros for compilation
define_macros=[("SOME_MACRO", None)]
my_flag=False
if len(sys.argv)>1:
if "-define-macro " in sys.argv[1:] :
my_flag=True
sys.argv.pop(sys.argv.index("-define-macro"))
else:
raise ValueError("flag not understood, only -define-macro accepted")
if my_flag:
define_macros.append(("MACRO_I_WANT",None))
Then, in the extensions:
gpuUtils_ext = Extension(
"_gpuUtils",
sources=include_headers(
[
"gpuUtils.cu",
"python/utilities/cuda_interface/_gpuUtils.pxd",
"python/utilities/cuda_interface/_gpuUtils.pyx",
],
sdist=sys.argv[1] == "sdist",
),
define_macros=define_macros, # changed this
library_dirs=[CUDA["lib64"]],
libraries=["cudart"],
language="c++",
runtime_library_dirs=[CUDA["lib64"]] if not IS_WINDOWS else None,
include_dirs=[NUMPY_INCLUDE, CUDA["include"], "./CUDA/"],
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论