Skip to content

Commit

Permalink
Merge pull request cupy#8574 from kmaehashi/fix-setuptools-74
Browse files Browse the repository at this point in the history
Support building CuPy with setuptools 74
  • Loading branch information
asi1024 authored and chainer-ci committed Aug 29, 2024
1 parent 67f3c4e commit 00ea50c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pretest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ jobs:
- name: Build
run: |
pip install -U pip wheel setuptools==73.0.1
pip install -U pip wheel setuptools
READTHEDOCS=True pip install -v -e .
ccache --max-size 0.5Gi --cleanup --show-stats
Expand Down
26 changes: 17 additions & 9 deletions install/cupy_builder/_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,18 +143,26 @@ def _cythonize(self, nthreads: int) -> None:
compile_time_env=compile_time_env)

def build_extensions(self) -> None:
ctx = cupy_builder.get_context()
num_jobs = int(os.environ.get('CUPY_NUM_BUILD_JOBS', '4'))
if num_jobs > 1:
self.parallel = num_jobs
if hasattr(self.compiler, 'initialize'):
# Workarounds a bug in setuptools/distutils on Windows by
# initializing the compiler before starting a thread.
# By default, MSVCCompiler performs initialization in the
# first compilation. However, in parallel compilation mode,
# the init code runs in each thread and messes up the internal
# state as the init code is not locked and is not idempotent.
# https://github.com/pypa/setuptools/blob/v60.0.0/setuptools/_distutils/_msvccompiler.py#L322-L327
self.compiler.initialize()

if (sys.platform == 'win32' and
hasattr(self.compiler, 'initialize')): # i.e., MSVCCompiler
# Initialize to get path to the host compiler (cl.exe).
# This also workarounds a bug in setuptools/distutils on Windows by
# initializing the compiler before starting a thread.
# By default, MSVCCompiler performs initialization in the
# first compilation. However, in parallel compilation mode,
# the init code runs in each thread and messes up the internal
# state as the init code is not locked and is not idempotent.
# https://github.com/pypa/setuptools/blob/v60.0.0/setuptools/_distutils/_msvccompiler.py#L322-L327
self.compiler.initialize()
if hasattr(self.compiler, 'cc'):
cc = self.compiler.cc
print(f'Detected host compiler: {cc}')
ctx.win32_cl_exe_path = cc

# Compile "*.pyx" files into "*.cpp" files.
print('Cythonizing...')
Expand Down
14 changes: 11 additions & 3 deletions install/cupy_builder/_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from typing import Any, Optional, List

import setuptools
import setuptools.msvc
from setuptools import Extension

from cupy_builder._context import Context
Expand Down Expand Up @@ -262,8 +261,17 @@ def _find_host_compiler_path(self) -> Optional[str]:
# The compiler is already on PATH, no extra path needed.
return None

vctools: List[str] = setuptools.msvc.EnvironmentInfo(
platform.machine()).VCTools
if self._context.win32_cl_exe_path is not None:
return self._context.win32_cl_exe_path

if hasattr(setuptools, 'msvc'): # setuptools<74
# TODO(kmaehashi): Remove this code at some point
vctools: List[str] = setuptools.msvc.EnvironmentInfo(
platform.machine()).VCTools
else:
print('Warning: cl.exe could not be auto-detected')
return None

for path in vctools:
cl_exe = os.path.join(path, 'cl.exe')
if os.path.exists(cl_exe):
Expand Down
3 changes: 3 additions & 0 deletions install/cupy_builder/_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ def __init__(
f'matching {include_pattern}): {cache_key}')
self.cupy_cache_key = cache_key

# Host compiler path for Windows, see `_command.py`.
self.win32_cl_exe_path: Optional[str] = None


def parse_args(argv: List[str]) -> Tuple[Any, List[str]]:
parser = argparse.ArgumentParser(add_help=False)
Expand Down

0 comments on commit 00ea50c

Please sign in to comment.