如何将用户定义的参数传递给setuptools,以设置更改编译宏的标志。

huangapple go评论112阅读模式
英文:

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:

  1. 我有一个很大的 setup.py 文件编译了几个 CUDA 文件类似于非常不完整如果相关我可以提供更多信息):
  2. gpuUtils_ext = Extension(
  3. "_gpuUtils",
  4. sources=include_headers(
  5. [
  6. "gpuUtils.cu",
  7. "python/utilities/cuda_interface/_gpuUtils.pxd",
  8. "python/utilities/cuda_interface/_gpuUtils.pyx",
  9. ],
  10. sdist=sys.argv[1] == "sdist",
  11. ),
  12. define_macros=[("MACRO_I_WANT", None)],
  13. library_dirs=[CUDA["lib64"]],
  14. libraries=["cudart"],
  15. language="c++",
  16. runtime_library_dirs=[CUDA["lib64"]] if not IS_WINDOWS else None,
  17. include_dirs=[NUMPY_INCLUDE, CUDA["include"], "./CUDA/"],
  18. )
  19. # etc
  20. setup(
  21. name="-",
  22. version="-",
  23. author="-",
  24. packages=find_packages(),
  25. include_package_data=True,
  26. data_files=[("data", ["../data/somefile.file"])],
  27. ext_modules=[foo1, foo2, gpuUtils_ext, foo3], # 我有很多
  28. py_modules=["foo.py"],
  29. cmdclass={"build_ext": BuildExtension},
  30. install_requires=["Cython", "matplotlib", "numpy", "scipy", "tqdm"],
  31. license_files=("LICENSE",),
  32. license="BSD 3-Clause",
  33. # 由于包含 C 代码,不能压缩 egg
  34. zip_safe=False,
  35. )

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

  1. gpuUtils_ext = Extension(
  2. "_gpuUtils",
  3. sources=include_headers(
  4. [
  5. "gpuUtils.cu",
  6. "python/utilities/cuda_interface/_gpuUtils.pxd",
  7. "python/utilities/cuda_interface/_gpuUtils.pyx",
  8. ],
  9. sdist=sys.argv[1] == "sdist",
  10. ),
  11. define_macros=[("MACRO_I_WANT", None)],
  12. library_dirs=[CUDA["lib64"]],
  13. libraries=["cudart"],
  14. language="c++",
  15. runtime_library_dirs=[CUDA["lib64"]] if not IS_WINDOWS else None,
  16. include_dirs=[NUMPY_INCLUDE, CUDA["include"], "./CUDA/"],
  17. )
  18. # etc
  19. setup(
  20. name="-",
  21. version="-",
  22. author="-",
  23. packages=find_packages(),
  24. include_package_data=True,
  25. data_files=[("data", ["../data/somefile.file"])],
  26. ext_modules=[foo1, foo2, gpuUtils_ext, foo3], # I have many
  27. py_modules=["foo.py"],
  28. cmdclass={"build_ext": BuildExtension},
  29. install_requires=["Cython", "matplotlib", "numpy", "scipy", "tqdm"],
  30. license_files=("LICENSE",),
  31. license="BSD 3-Clause",
  32. # since the package has c code, the egg cannot be zipped
  33. zip_safe=False,
  34. )

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, # 修改了这一部分

  1. library_dirs=[CUDA["lib64"]],
  2. libraries=["cudart"],
  3. language="c++",
  4. runtime_library_dirs=[CUDA["lib64"]] if not IS_WINDOWS else None,
  5. include_dirs=[NUMPY_INCLUDE, CUDA["include"], "./CUDA/"],

)

英文:

Ended up grabbing the system argument and poping it out:

  1. import sys
  2. # Macros for compilation
  3. define_macros=[("SOME_MACRO", None)]
  4. my_flag=False
  5. if len(sys.argv)>1:
  6. if "-define-macro " in sys.argv[1:] :
  7. my_flag=True
  8. sys.argv.pop(sys.argv.index("-define-macro"))
  9. else:
  10. raise ValueError("flag not understood, only -define-macro accepted")
  11. if my_flag:
  12. define_macros.append(("MACRO_I_WANT",None))

Then, in the extensions:

  1. gpuUtils_ext = Extension(
  2. "_gpuUtils",
  3. sources=include_headers(
  4. [
  5. "gpuUtils.cu",
  6. "python/utilities/cuda_interface/_gpuUtils.pxd",
  7. "python/utilities/cuda_interface/_gpuUtils.pyx",
  8. ],
  9. sdist=sys.argv[1] == "sdist",
  10. ),
  11. define_macros=define_macros, # changed this
  12. library_dirs=[CUDA["lib64"]],
  13. libraries=["cudart"],
  14. language="c++",
  15. runtime_library_dirs=[CUDA["lib64"]] if not IS_WINDOWS else None,
  16. include_dirs=[NUMPY_INCLUDE, CUDA["include"], "./CUDA/"],
  17. )

huangapple
  • 本文由 发表于 2023年5月20日 23:04:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76295882.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定