66from setuptools import setup , Extension
77from setuptools .command .build_ext import build_ext
88
9- PLAT_TO_CMAKE = {
10- "win32" : "Win32" ,
11- "win-amd64" : "x64" ,
12- "win-arm32" : "ARM" ,
13- "win-arm64" : "ARM64" ,
14- }
15-
169
1710class CMakeExtension (Extension ):
1811 def __init__ (self , name : str , sourcedir : str = "" ) -> None :
@@ -30,26 +23,23 @@ def build_extension(self, ext: CMakeExtension) -> None:
3023 cmake_args = [
3124 f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={ extdir } { os .sep } " ,
3225 f"-DPython_EXECUTABLE={ sys .executable } " ,
26+ f"-DCMAKE_BUILD_TYPE=Release" ,
3327 ]
3428
3529 build_args = ["--config" , "Release" ]
3630
37- if sys .platform == "win32" :
38- # MSVC: set architecture and output directory per-config
39- single_config = any (
40- x in cmake_generator for x in ("NMake" , "Ninja" )
41- )
42- contains_arch = any (
43- x in cmake_generator for x in ("ARM" , "Win64" )
44- )
45- if not single_config and not contains_arch :
46- cmake_args += ["-A" , PLAT_TO_CMAKE [self .plat_name ]]
47- if not single_config :
31+ # On Windows, use Ninja if no generator is specified to avoid
32+ # Visual Studio multi-config generator issues with pybind11
33+ if sys .platform == "win32" and not cmake_generator :
34+ try :
35+ import ninja
36+ ninja_executable_path = Path (ninja .BIN_DIR ) / "ninja"
4837 cmake_args += [
49- f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE={ extdir } { os .sep } " ,
38+ "-GNinja" ,
39+ f"-DCMAKE_MAKE_PROGRAM:FILEPATH={ ninja_executable_path } " ,
5040 ]
51- else :
52- cmake_args += [ "-DCMAKE_BUILD_TYPE=Release" ]
41+ except ImportError :
42+ pass
5343
5444 if "CMAKE_BUILD_PARALLEL_LEVEL" not in os .environ :
5545 if hasattr (self , "parallel" ) and self .parallel :
0 commit comments