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

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

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/"],
)

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:

确定