We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
`pip install cupy-wheel` - "not a valid wheel filename"
### Description

When I try to `pip install cupy-wheel` I get the error `cupy_wheel<...>.whl is not a valid wheel filename`.

### To Reproduce

```bash
pip install cupy-wheel
```
OS: Windows 10
Python: 3.8.9
pip: 23.1.2
CUDA: CUDA 11.7

### Installation

None

### Environment

Unable to install using `cupy-wheel`. Can install using `pip install cupy-cuda11x`.

### Additional Information

_No response_

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `install/universal_pkg/setup.py`
Content:
```
1 import ctypes
2 import pkg_resources
3 import os
4 import sys
5 from typing import Dict, List, Optional
6 
7 from setuptools import setup
8 
9 
10 VERSION = '13.0.0a1'
11 
12 # List of packages supported by this version of CuPy.
13 PACKAGES = [
14     'cupy-cuda102',
15     'cupy-cuda110',
16     'cupy-cuda111',
17     'cupy-cuda11x',
18     'cupy-cuda12x',
19     'cupy-rocm-4-3',
20     'cupy-rocm-5-0',
21 ]
22 
23 # List of packages NOT supported by this version of CuPy.
24 PACKAGES_OUTDATED = [
25     'cupy-cuda80',
26     'cupy-cuda90',
27     'cupy-cuda91',
28     'cupy-cuda92',
29     'cupy-cuda100',
30     'cupy-cuda101',
31     'cupy-cuda112',
32     'cupy-cuda113',
33     'cupy-cuda114',
34     'cupy-cuda115',
35     'cupy-cuda116',
36     'cupy-cuda117',
37     'cupy-rocm-4-0',
38     'cupy-rocm-4-2',
39 ]
40 
41 # List of sdist packages.
42 PACKAGES_SDIST = [
43     'cupy',
44 ]
45 
46 
47 class AutoDetectionFailed(Exception):
48     def __str__(self) -> str:
49         return f'''
50 ============================================================
51 {super().__str__()}
52 ============================================================
53 '''
54 
55 
56 def _log(msg: str) -> None:
57     sys.stdout.write(f'[cupy-wheel] {msg}\n')
58     sys.stdout.flush()
59 
60 
61 def _get_version_from_library(
62         libnames: List[str],
63         funcname: str,
64         nvrtc: bool = False,
65 ) -> Optional[int]:
66     """Returns the library version from list of candidate libraries."""
67 
68     for libname in libnames:
69         try:
70             _log(f'Looking for library: {libname}')
71             runtime_so = ctypes.CDLL(libname)
72             break
73         except Exception as e:
74             _log(f'Failed to open {libname}: {e}')
75     else:
76         _log('No more candidate library to find')
77         return None
78 
79     func = getattr(runtime_so, funcname, None)
80     if func is None:
81         raise AutoDetectionFailed(
82             f'{libname}: {func} could not be found')
83     func.restype = ctypes.c_int
84 
85     if nvrtc:
86         # nvrtcVersion
87         func.argtypes = [
88             ctypes.POINTER(ctypes.c_int),
89             ctypes.POINTER(ctypes.c_int),
90         ]
91         major = ctypes.c_int()
92         minor = ctypes.c_int()
93         retval = func(major, minor)
94         version = major.value * 1000 + minor.value * 10
95     else:
96         # cudaRuntimeGetVersion
97         func.argtypes = [
98             ctypes.POINTER(ctypes.c_int),
99         ]
100         version_ref = ctypes.c_int()
101         retval = func(version_ref)
102         version = version_ref.value
103 
104     if retval != 0:  # NVRTC_SUCCESS or cudaSuccess
105         raise AutoDetectionFailed(
106             f'{libname}: {func} returned error: {retval}')
107     _log(f'Detected version: {version}')
108     return version
109 
110 
111 def _setup_win32_dll_directory() -> None:
112     if not hasattr(os, 'add_dll_directory'):
113         # Python 3.7 or earlier.
114         return
115     cuda_path = os.environ.get('CUDA_PATH', None)
116     if cuda_path is None:
117         _log('CUDA_PATH is not set.'
118              'cupy-wheel may not be able to discover NVRTC to probe version')
119         return
120     os.add_dll_directory(os.path.join(cuda_path, 'bin'))  # type: ignore[attr-defined] # NOQA
121 
122 
123 def _get_cuda_version() -> Optional[int]:
124     """Returns the detected CUDA version or None."""
125 
126     if sys.platform == 'linux':
127         libnames = [
128             'libnvrtc.so.12',
129             'libnvrtc.so.11.2',
130             'libnvrtc.so.11.1',
131             'libnvrtc.so.11.0',
132             'libnvrtc.so.10.2',
133         ]
134     elif sys.platform == 'win32':
135         libnames = [
136             'nvrtc64_120_0.dll',
137             'nvrtc64_112_0.dll',
138             'nvrtc64_111_0.dll',
139             'nvrtc64_110_0.dll',
140             'nvrtc64_102_0.dll',
141         ]
142         _setup_win32_dll_directory()
143     else:
144         _log(f'CUDA detection unsupported on platform: {sys.platform}')
145         return None
146     _log(f'Trying to detect CUDA version from libraries: {libnames}')
147     version = _get_version_from_library(libnames, 'nvrtcVersion', True)
148     return version
149 
150 
151 def _get_rocm_version() -> Optional[int]:
152     """Returns the detected ROCm version or None."""
153     if sys.platform == 'linux':
154         libnames = ['libamdhip64.so']
155     else:
156         _log(f'ROCm detection unsupported on platform: {sys.platform}')
157         return None
158     version = _get_version_from_library(libnames, 'hipRuntimeGetVersion')
159     return version
160 
161 
162 def _find_installed_packages() -> List[str]:
163     """Returns the list of CuPy packages installed in the environment."""
164 
165     found = []
166     for pkg in (PACKAGES + PACKAGES_OUTDATED + PACKAGES_SDIST):
167         try:
168             pkg_resources.get_distribution(pkg)
169             found.append(pkg)
170         except pkg_resources.DistributionNotFound:
171             pass
172     return found
173 
174 
175 def _cuda_version_to_package(ver: int) -> str:
176     if ver < 10020:
177         raise AutoDetectionFailed(
178             f'Your CUDA version ({ver}) is too old.')
179     elif ver < 11000:
180         # CUDA 10.2
181         suffix = '102'
182     elif ver < 11010:
183         # CUDA 11.0
184         suffix = '110'
185     elif ver < 11020:
186         # CUDA 11.1
187         suffix = '111'
188     elif ver < 12000:
189         # CUDA 11.2 ~ 11.x
190         suffix = '11x'
191     elif ver < 13000:
192         # CUDA 12.x
193         suffix = '12x'
194     else:
195         raise AutoDetectionFailed(
196             f'Your CUDA version ({ver}) is too new.')
197     return f'cupy-cuda{suffix}'
198 
199 
200 def _rocm_version_to_package(ver: int) -> str:
201     """
202     ROCm 4.0.x = 3212
203     ROCm 4.1.x = 3241
204     ROCm 4.2.0 = 3275
205     ROCm 4.3.0 = 40321300
206     ROCm 4.3.1 = 40321331
207     ROCm 4.5.0 = 40421401
208     ROCm 4.5.1 = 40421432
209     ROCm 5.0.0 = 50013601
210     ROCm 5.1.0 = 50120531
211     """
212     if 4_03_00000 <= ver < 4_04_00000:
213         # ROCm 4.3
214         suffix = '4-3'
215     elif 5_00_00000 <= ver < 5_01_00000:
216         # ROCm 5.0
217         suffix = '5-0'
218     else:
219         raise AutoDetectionFailed(
220             f'Your ROCm version ({ver}) is unsupported.')
221     return f'cupy-rocm-{suffix}'
222 
223 
224 def infer_best_package() -> str:
225     """Returns the appropriate CuPy wheel package name for the environment."""
226 
227     # Find the existing CuPy wheel installation.
228     installed = _find_installed_packages()
229     if 1 < len(installed):
230         raise AutoDetectionFailed(
231             'You have multiple CuPy packages installed: \n'
232             f'  {installed}\n'
233             'Please uninstall all of them first, then try reinstalling.')
234 
235     elif 1 == len(installed):
236         if installed[0] in PACKAGES_SDIST:
237             raise AutoDetectionFailed(
238                 'You already have CuPy installed via source'
239                 ' (pip install cupy).')
240         if installed[0] in PACKAGES_OUTDATED:
241             raise AutoDetectionFailed(
242                 f'You have CuPy package "{installed[0]}" installed, but the'
243                 f' package is not available for version {VERSION}.\n'
244                 'Hint: cupy-cuda{112~117} has been merged to cupy-cuda11x in '
245                 'CuPy v11. Uninstall the package and try again.')
246         return installed[0]
247 
248     # Try CUDA.
249     version = _get_cuda_version()
250     if version is not None:
251         return _cuda_version_to_package(version)
252 
253     # Try ROCm.
254     version = _get_rocm_version()
255     if version is not None:
256         return _rocm_version_to_package(version)
257 
258     raise AutoDetectionFailed(
259         'Unable to detect NVIDIA CUDA or AMD ROCm installation.')
260 
261 
262 def _get_cmdclass(tag: str) -> Dict[str, type]:
263     try:
264         import wheel.bdist_wheel
265     except ModuleNotFoundError:
266         return {}
267 
268     class bdist_wheel_with_tag(wheel.bdist_wheel.bdist_wheel):  # type: ignore[misc] # NOQA
269         def initialize_options(self) -> None:
270             super().initialize_options()
271             self.build_number = f'0_{tag}'
272 
273     return {"bdist_wheel": bdist_wheel_with_tag}
274 
275 
276 #
277 # Entrypoint
278 #
279 
280 def main() -> None:
281     if os.environ.get('CUPY_UNIVERSAL_PKG_BUILD', None) is None:
282         package = infer_best_package()
283         requires = f'{package}=={VERSION}'
284         _log(f'Installing package: {requires}')
285         install_requires = [requires]
286         tag = package
287     else:
288         _log('Building cupy-wheel package for release.')
289         install_requires = []
290         tag = '0'
291 
292     setup(
293         name='cupy-wheel',
294         version=f'{VERSION}',
295         install_requires=install_requires,
296         cmdclass=_get_cmdclass(tag),
297     )
298 
299 
300 if __name__ == '__main__':
301     main()
302
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/install/universal_pkg/setup.py b/install/universal_pkg/setup.py
--- a/install/universal_pkg/setup.py
+++ b/install/universal_pkg/setup.py
@@ -2,12 +2,13 @@
 import pkg_resources
 import os
 import sys
-from typing import Dict, List, Optional
+from typing import List, Optional
 
 from setuptools import setup
 
 
 VERSION = '13.0.0a1'
+META_VERSION = VERSION
 
 # List of packages supported by this version of CuPy.
 PACKAGES = [
@@ -259,20 +260,6 @@
         'Unable to detect NVIDIA CUDA or AMD ROCm installation.')
 
 
-def _get_cmdclass(tag: str) -> Dict[str, type]:
-    try:
-        import wheel.bdist_wheel
-    except ModuleNotFoundError:
-        return {}
-
-    class bdist_wheel_with_tag(wheel.bdist_wheel.bdist_wheel):  # type: ignore[misc] # NOQA
-        def initialize_options(self) -> None:
-            super().initialize_options()
-            self.build_number = f'0_{tag}'
-
-    return {"bdist_wheel": bdist_wheel_with_tag}
-
-
 #
 # Entrypoint
 #
@@ -283,17 +270,14 @@
         requires = f'{package}=={VERSION}'
         _log(f'Installing package: {requires}')
         install_requires = [requires]
-        tag = package
     else:
         _log('Building cupy-wheel package for release.')
         install_requires = []
-        tag = '0'
 
     setup(
         name='cupy-wheel',
-        version=f'{VERSION}',
+        version=META_VERSION,
         install_requires=install_requires,
-        cmdclass=_get_cmdclass(tag),
     )

{"golden_diff": "diff --git a/install/universal_pkg/setup.py b/install/universal_pkg/setup.py\n--- a/install/universal_pkg/setup.py\n+++ b/install/universal_pkg/setup.py\n@@ -2,12 +2,13 @@\n import pkg_resources\n import os\n import sys\n-from typing import Dict, List, Optional\n+from typing import List, Optional\n \n from setuptools import setup\n \n \n VERSION = '13.0.0a1'\n+META_VERSION = VERSION\n \n # List of packages supported by this version of CuPy.\n PACKAGES = [\n@@ -259,20 +260,6 @@\n         'Unable to detect NVIDIA CUDA or AMD ROCm installation.')\n \n \n-def _get_cmdclass(tag: str) -> Dict[str, type]:\n-    try:\n-        import wheel.bdist_wheel\n-    except ModuleNotFoundError:\n-        return {}\n-\n-    class bdist_wheel_with_tag(wheel.bdist_wheel.bdist_wheel):  # type: ignore[misc] # NOQA\n-        def initialize_options(self) -> None:\n-            super().initialize_options()\n-            self.build_number = f'0_{tag}'\n-\n-    return {\"bdist_wheel\": bdist_wheel_with_tag}\n-\n-\n #\n # Entrypoint\n #\n@@ -283,17 +270,14 @@\n         requires = f'{package}=={VERSION}'\n         _log(f'Installing package: {requires}')\n         install_requires = [requires]\n-        tag = package\n     else:\n         _log('Building cupy-wheel package for release.')\n         install_requires = []\n-        tag = '0'\n \n     setup(\n         name='cupy-wheel',\n-        version=f'{VERSION}',\n+        version=META_VERSION,\n         install_requires=install_requires,\n-        cmdclass=_get_cmdclass(tag),\n     )\n", "issue": "`pip install cupy-wheel` - \"not a valid wheel filename\"\n### Description\n\nWhen I try to `pip install cupy-wheel` I get the error `cupy_wheel<...>.whl is not a valid wheel filename`.\n\n### To Reproduce\n\n```bash\r\npip install cupy-wheel\r\n```\r\nOS: Windows 10\r\nPython: 3.8.9\r\npip: 23.1.2\r\nCUDA: CUDA 11.7\n\n### Installation\n\nNone\n\n### Environment\n\nUnable to install using `cupy-wheel`. Can install using `pip install cupy-cuda11x`.\n\n### Additional Information\n\n_No response_\n", "before_files": [{"content": "import ctypes\nimport pkg_resources\nimport os\nimport sys\nfrom typing import Dict, List, Optional\n\nfrom setuptools import setup\n\n\nVERSION = '13.0.0a1'\n\n# List of packages supported by this version of CuPy.\nPACKAGES = [\n    'cupy-cuda102',\n    'cupy-cuda110',\n    'cupy-cuda111',\n    'cupy-cuda11x',\n    'cupy-cuda12x',\n    'cupy-rocm-4-3',\n    'cupy-rocm-5-0',\n]\n\n# List of packages NOT supported by this version of CuPy.\nPACKAGES_OUTDATED = [\n    'cupy-cuda80',\n    'cupy-cuda90',\n    'cupy-cuda91',\n    'cupy-cuda92',\n    'cupy-cuda100',\n    'cupy-cuda101',\n    'cupy-cuda112',\n    'cupy-cuda113',\n    'cupy-cuda114',\n    'cupy-cuda115',\n    'cupy-cuda116',\n    'cupy-cuda117',\n    'cupy-rocm-4-0',\n    'cupy-rocm-4-2',\n]\n\n# List of sdist packages.\nPACKAGES_SDIST = [\n    'cupy',\n]\n\n\nclass AutoDetectionFailed(Exception):\n    def __str__(self) -> str:\n        return f'''\n============================================================\n{super().__str__()}\n============================================================\n'''\n\n\ndef _log(msg: str) -> None:\n    sys.stdout.write(f'[cupy-wheel] {msg}\\n')\n    sys.stdout.flush()\n\n\ndef _get_version_from_library(\n        libnames: List[str],\n        funcname: str,\n        nvrtc: bool = False,\n) -> Optional[int]:\n    \"\"\"Returns the library version from list of candidate libraries.\"\"\"\n\n    for libname in libnames:\n        try:\n            _log(f'Looking for library: {libname}')\n            runtime_so = ctypes.CDLL(libname)\n            break\n        except Exception as e:\n            _log(f'Failed to open {libname}: {e}')\n    else:\n        _log('No more candidate library to find')\n        return None\n\n    func = getattr(runtime_so, funcname, None)\n    if func is None:\n        raise AutoDetectionFailed(\n            f'{libname}: {func} could not be found')\n    func.restype = ctypes.c_int\n\n    if nvrtc:\n        # nvrtcVersion\n        func.argtypes = [\n            ctypes.POINTER(ctypes.c_int),\n            ctypes.POINTER(ctypes.c_int),\n        ]\n        major = ctypes.c_int()\n        minor = ctypes.c_int()\n        retval = func(major, minor)\n        version = major.value * 1000 + minor.value * 10\n    else:\n        # cudaRuntimeGetVersion\n        func.argtypes = [\n            ctypes.POINTER(ctypes.c_int),\n        ]\n        version_ref = ctypes.c_int()\n        retval = func(version_ref)\n        version = version_ref.value\n\n    if retval != 0:  # NVRTC_SUCCESS or cudaSuccess\n        raise AutoDetectionFailed(\n            f'{libname}: {func} returned error: {retval}')\n    _log(f'Detected version: {version}')\n    return version\n\n\ndef _setup_win32_dll_directory() -> None:\n    if not hasattr(os, 'add_dll_directory'):\n        # Python 3.7 or earlier.\n        return\n    cuda_path = os.environ.get('CUDA_PATH', None)\n    if cuda_path is None:\n        _log('CUDA_PATH is not set.'\n             'cupy-wheel may not be able to discover NVRTC to probe version')\n        return\n    os.add_dll_directory(os.path.join(cuda_path, 'bin'))  # type: ignore[attr-defined] # NOQA\n\n\ndef _get_cuda_version() -> Optional[int]:\n    \"\"\"Returns the detected CUDA version or None.\"\"\"\n\n    if sys.platform == 'linux':\n        libnames = [\n            'libnvrtc.so.12',\n            'libnvrtc.so.11.2',\n            'libnvrtc.so.11.1',\n            'libnvrtc.so.11.0',\n            'libnvrtc.so.10.2',\n        ]\n    elif sys.platform == 'win32':\n        libnames = [\n            'nvrtc64_120_0.dll',\n            'nvrtc64_112_0.dll',\n            'nvrtc64_111_0.dll',\n            'nvrtc64_110_0.dll',\n            'nvrtc64_102_0.dll',\n        ]\n        _setup_win32_dll_directory()\n    else:\n        _log(f'CUDA detection unsupported on platform: {sys.platform}')\n        return None\n    _log(f'Trying to detect CUDA version from libraries: {libnames}')\n    version = _get_version_from_library(libnames, 'nvrtcVersion', True)\n    return version\n\n\ndef _get_rocm_version() -> Optional[int]:\n    \"\"\"Returns the detected ROCm version or None.\"\"\"\n    if sys.platform == 'linux':\n        libnames = ['libamdhip64.so']\n    else:\n        _log(f'ROCm detection unsupported on platform: {sys.platform}')\n        return None\n    version = _get_version_from_library(libnames, 'hipRuntimeGetVersion')\n    return version\n\n\ndef _find_installed_packages() -> List[str]:\n    \"\"\"Returns the list of CuPy packages installed in the environment.\"\"\"\n\n    found = []\n    for pkg in (PACKAGES + PACKAGES_OUTDATED + PACKAGES_SDIST):\n        try:\n            pkg_resources.get_distribution(pkg)\n            found.append(pkg)\n        except pkg_resources.DistributionNotFound:\n            pass\n    return found\n\n\ndef _cuda_version_to_package(ver: int) -> str:\n    if ver < 10020:\n        raise AutoDetectionFailed(\n            f'Your CUDA version ({ver}) is too old.')\n    elif ver < 11000:\n        # CUDA 10.2\n        suffix = '102'\n    elif ver < 11010:\n        # CUDA 11.0\n        suffix = '110'\n    elif ver < 11020:\n        # CUDA 11.1\n        suffix = '111'\n    elif ver < 12000:\n        # CUDA 11.2 ~ 11.x\n        suffix = '11x'\n    elif ver < 13000:\n        # CUDA 12.x\n        suffix = '12x'\n    else:\n        raise AutoDetectionFailed(\n            f'Your CUDA version ({ver}) is too new.')\n    return f'cupy-cuda{suffix}'\n\n\ndef _rocm_version_to_package(ver: int) -> str:\n    \"\"\"\n    ROCm 4.0.x = 3212\n    ROCm 4.1.x = 3241\n    ROCm 4.2.0 = 3275\n    ROCm 4.3.0 = 40321300\n    ROCm 4.3.1 = 40321331\n    ROCm 4.5.0 = 40421401\n    ROCm 4.5.1 = 40421432\n    ROCm 5.0.0 = 50013601\n    ROCm 5.1.0 = 50120531\n    \"\"\"\n    if 4_03_00000 <= ver < 4_04_00000:\n        # ROCm 4.3\n        suffix = '4-3'\n    elif 5_00_00000 <= ver < 5_01_00000:\n        # ROCm 5.0\n        suffix = '5-0'\n    else:\n        raise AutoDetectionFailed(\n            f'Your ROCm version ({ver}) is unsupported.')\n    return f'cupy-rocm-{suffix}'\n\n\ndef infer_best_package() -> str:\n    \"\"\"Returns the appropriate CuPy wheel package name for the environment.\"\"\"\n\n    # Find the existing CuPy wheel installation.\n    installed = _find_installed_packages()\n    if 1 < len(installed):\n        raise AutoDetectionFailed(\n            'You have multiple CuPy packages installed: \\n'\n            f'  {installed}\\n'\n            'Please uninstall all of them first, then try reinstalling.')\n\n    elif 1 == len(installed):\n        if installed[0] in PACKAGES_SDIST:\n            raise AutoDetectionFailed(\n                'You already have CuPy installed via source'\n                ' (pip install cupy).')\n        if installed[0] in PACKAGES_OUTDATED:\n            raise AutoDetectionFailed(\n                f'You have CuPy package \"{installed[0]}\" installed, but the'\n                f' package is not available for version {VERSION}.\\n'\n                'Hint: cupy-cuda{112~117} has been merged to cupy-cuda11x in '\n                'CuPy v11. Uninstall the package and try again.')\n        return installed[0]\n\n    # Try CUDA.\n    version = _get_cuda_version()\n    if version is not None:\n        return _cuda_version_to_package(version)\n\n    # Try ROCm.\n    version = _get_rocm_version()\n    if version is not None:\n        return _rocm_version_to_package(version)\n\n    raise AutoDetectionFailed(\n        'Unable to detect NVIDIA CUDA or AMD ROCm installation.')\n\n\ndef _get_cmdclass(tag: str) -> Dict[str, type]:\n    try:\n        import wheel.bdist_wheel\n    except ModuleNotFoundError:\n        return {}\n\n    class bdist_wheel_with_tag(wheel.bdist_wheel.bdist_wheel):  # type: ignore[misc] # NOQA\n        def initialize_options(self) -> None:\n            super().initialize_options()\n            self.build_number = f'0_{tag}'\n\n    return {\"bdist_wheel\": bdist_wheel_with_tag}\n\n\n#\n# Entrypoint\n#\n\ndef main() -> None:\n    if os.environ.get('CUPY_UNIVERSAL_PKG_BUILD', None) is None:\n        package = infer_best_package()\n        requires = f'{package}=={VERSION}'\n        _log(f'Installing package: {requires}')\n        install_requires = [requires]\n        tag = package\n    else:\n        _log('Building cupy-wheel package for release.')\n        install_requires = []\n        tag = '0'\n\n    setup(\n        name='cupy-wheel',\n        version=f'{VERSION}',\n        install_requires=install_requires,\n        cmdclass=_get_cmdclass(tag),\n    )\n\n\nif __name__ == '__main__':\n    main()\n", "path": "install/universal_pkg/setup.py"}], "after_files": [{"content": "import ctypes\nimport pkg_resources\nimport os\nimport sys\nfrom typing import List, Optional\n\nfrom setuptools import setup\n\n\nVERSION = '13.0.0a1'\nMETA_VERSION = VERSION\n\n# List of packages supported by this version of CuPy.\nPACKAGES = [\n    'cupy-cuda102',\n    'cupy-cuda110',\n    'cupy-cuda111',\n    'cupy-cuda11x',\n    'cupy-cuda12x',\n    'cupy-rocm-4-3',\n    'cupy-rocm-5-0',\n]\n\n# List of packages NOT supported by this version of CuPy.\nPACKAGES_OUTDATED = [\n    'cupy-cuda80',\n    'cupy-cuda90',\n    'cupy-cuda91',\n    'cupy-cuda92',\n    'cupy-cuda100',\n    'cupy-cuda101',\n    'cupy-cuda112',\n    'cupy-cuda113',\n    'cupy-cuda114',\n    'cupy-cuda115',\n    'cupy-cuda116',\n    'cupy-cuda117',\n    'cupy-rocm-4-0',\n    'cupy-rocm-4-2',\n]\n\n# List of sdist packages.\nPACKAGES_SDIST = [\n    'cupy',\n]\n\n\nclass AutoDetectionFailed(Exception):\n    def __str__(self) -> str:\n        return f'''\n============================================================\n{super().__str__()}\n============================================================\n'''\n\n\ndef _log(msg: str) -> None:\n    sys.stdout.write(f'[cupy-wheel] {msg}\\n')\n    sys.stdout.flush()\n\n\ndef _get_version_from_library(\n        libnames: List[str],\n        funcname: str,\n        nvrtc: bool = False,\n) -> Optional[int]:\n    \"\"\"Returns the library version from list of candidate libraries.\"\"\"\n\n    for libname in libnames:\n        try:\n            _log(f'Looking for library: {libname}')\n            runtime_so = ctypes.CDLL(libname)\n            break\n        except Exception as e:\n            _log(f'Failed to open {libname}: {e}')\n    else:\n        _log('No more candidate library to find')\n        return None\n\n    func = getattr(runtime_so, funcname, None)\n    if func is None:\n        raise AutoDetectionFailed(\n            f'{libname}: {func} could not be found')\n    func.restype = ctypes.c_int\n\n    if nvrtc:\n        # nvrtcVersion\n        func.argtypes = [\n            ctypes.POINTER(ctypes.c_int),\n            ctypes.POINTER(ctypes.c_int),\n        ]\n        major = ctypes.c_int()\n        minor = ctypes.c_int()\n        retval = func(major, minor)\n        version = major.value * 1000 + minor.value * 10\n    else:\n        # cudaRuntimeGetVersion\n        func.argtypes = [\n            ctypes.POINTER(ctypes.c_int),\n        ]\n        version_ref = ctypes.c_int()\n        retval = func(version_ref)\n        version = version_ref.value\n\n    if retval != 0:  # NVRTC_SUCCESS or cudaSuccess\n        raise AutoDetectionFailed(\n            f'{libname}: {func} returned error: {retval}')\n    _log(f'Detected version: {version}')\n    return version\n\n\ndef _setup_win32_dll_directory() -> None:\n    if not hasattr(os, 'add_dll_directory'):\n        # Python 3.7 or earlier.\n        return\n    cuda_path = os.environ.get('CUDA_PATH', None)\n    if cuda_path is None:\n        _log('CUDA_PATH is not set.'\n             'cupy-wheel may not be able to discover NVRTC to probe version')\n        return\n    os.add_dll_directory(os.path.join(cuda_path, 'bin'))  # type: ignore[attr-defined] # NOQA\n\n\ndef _get_cuda_version() -> Optional[int]:\n    \"\"\"Returns the detected CUDA version or None.\"\"\"\n\n    if sys.platform == 'linux':\n        libnames = [\n            'libnvrtc.so.12',\n            'libnvrtc.so.11.2',\n            'libnvrtc.so.11.1',\n            'libnvrtc.so.11.0',\n            'libnvrtc.so.10.2',\n        ]\n    elif sys.platform == 'win32':\n        libnames = [\n            'nvrtc64_120_0.dll',\n            'nvrtc64_112_0.dll',\n            'nvrtc64_111_0.dll',\n            'nvrtc64_110_0.dll',\n            'nvrtc64_102_0.dll',\n        ]\n        _setup_win32_dll_directory()\n    else:\n        _log(f'CUDA detection unsupported on platform: {sys.platform}')\n        return None\n    _log(f'Trying to detect CUDA version from libraries: {libnames}')\n    version = _get_version_from_library(libnames, 'nvrtcVersion', True)\n    return version\n\n\ndef _get_rocm_version() -> Optional[int]:\n    \"\"\"Returns the detected ROCm version or None.\"\"\"\n    if sys.platform == 'linux':\n        libnames = ['libamdhip64.so']\n    else:\n        _log(f'ROCm detection unsupported on platform: {sys.platform}')\n        return None\n    version = _get_version_from_library(libnames, 'hipRuntimeGetVersion')\n    return version\n\n\ndef _find_installed_packages() -> List[str]:\n    \"\"\"Returns the list of CuPy packages installed in the environment.\"\"\"\n\n    found = []\n    for pkg in (PACKAGES + PACKAGES_OUTDATED + PACKAGES_SDIST):\n        try:\n            pkg_resources.get_distribution(pkg)\n            found.append(pkg)\n        except pkg_resources.DistributionNotFound:\n            pass\n    return found\n\n\ndef _cuda_version_to_package(ver: int) -> str:\n    if ver < 10020:\n        raise AutoDetectionFailed(\n            f'Your CUDA version ({ver}) is too old.')\n    elif ver < 11000:\n        # CUDA 10.2\n        suffix = '102'\n    elif ver < 11010:\n        # CUDA 11.0\n        suffix = '110'\n    elif ver < 11020:\n        # CUDA 11.1\n        suffix = '111'\n    elif ver < 12000:\n        # CUDA 11.2 ~ 11.x\n        suffix = '11x'\n    elif ver < 13000:\n        # CUDA 12.x\n        suffix = '12x'\n    else:\n        raise AutoDetectionFailed(\n            f'Your CUDA version ({ver}) is too new.')\n    return f'cupy-cuda{suffix}'\n\n\ndef _rocm_version_to_package(ver: int) -> str:\n    \"\"\"\n    ROCm 4.0.x = 3212\n    ROCm 4.1.x = 3241\n    ROCm 4.2.0 = 3275\n    ROCm 4.3.0 = 40321300\n    ROCm 4.3.1 = 40321331\n    ROCm 4.5.0 = 40421401\n    ROCm 4.5.1 = 40421432\n    ROCm 5.0.0 = 50013601\n    ROCm 5.1.0 = 50120531\n    \"\"\"\n    if 4_03_00000 <= ver < 4_04_00000:\n        # ROCm 4.3\n        suffix = '4-3'\n    elif 5_00_00000 <= ver < 5_01_00000:\n        # ROCm 5.0\n        suffix = '5-0'\n    else:\n        raise AutoDetectionFailed(\n            f'Your ROCm version ({ver}) is unsupported.')\n    return f'cupy-rocm-{suffix}'\n\n\ndef infer_best_package() -> str:\n    \"\"\"Returns the appropriate CuPy wheel package name for the environment.\"\"\"\n\n    # Find the existing CuPy wheel installation.\n    installed = _find_installed_packages()\n    if 1 < len(installed):\n        raise AutoDetectionFailed(\n            'You have multiple CuPy packages installed: \\n'\n            f'  {installed}\\n'\n            'Please uninstall all of them first, then try reinstalling.')\n\n    elif 1 == len(installed):\n        if installed[0] in PACKAGES_SDIST:\n            raise AutoDetectionFailed(\n                'You already have CuPy installed via source'\n                ' (pip install cupy).')\n        if installed[0] in PACKAGES_OUTDATED:\n            raise AutoDetectionFailed(\n                f'You have CuPy package \"{installed[0]}\" installed, but the'\n                f' package is not available for version {VERSION}.\\n'\n                'Hint: cupy-cuda{112~117} has been merged to cupy-cuda11x in '\n                'CuPy v11. Uninstall the package and try again.')\n        return installed[0]\n\n    # Try CUDA.\n    version = _get_cuda_version()\n    if version is not None:\n        return _cuda_version_to_package(version)\n\n    # Try ROCm.\n    version = _get_rocm_version()\n    if version is not None:\n        return _rocm_version_to_package(version)\n\n    raise AutoDetectionFailed(\n        'Unable to detect NVIDIA CUDA or AMD ROCm installation.')\n\n\n#\n# Entrypoint\n#\n\ndef main() -> None:\n    if os.environ.get('CUPY_UNIVERSAL_PKG_BUILD', None) is None:\n        package = infer_best_package()\n        requires = f'{package}=={VERSION}'\n        _log(f'Installing package: {requires}')\n        install_requires = [requires]\n    else:\n        _log('Building cupy-wheel package for release.')\n        install_requires = []\n\n    setup(\n        name='cupy-wheel',\n        version=META_VERSION,\n        install_requires=install_requires,\n    )\n\n\nif __name__ == '__main__':\n    main()\n", "path": "install/universal_pkg/setup.py"}]}