3131MAC = sys .platform .startswith ("darwin" )
3232
3333
34+ # check for mingw compiler on windows
35+ is_mingw = False
36+ if WIN :
37+ from setuptools ._distutils import ccompiler
38+ is_mingw = ccompiler .get_default_compiler () == 'mingw32'
39+
40+
3441# Have to copy from "create_shadertext.py" script due to the use of pyproject.toml
3542# Full explanation:
3643# https://github.com/pypa/setuptools/issues/3939
@@ -269,6 +276,10 @@ def get_prefix_path() -> list[str]:
269276
270277 paths += [sys .prefix ] + paths
271278
279+ if WIN and is_mingw :
280+ if os .path .isdir (sys .prefix ):
281+ paths .append (os .path .normpath (sys .prefix ))
282+
272283 paths += ["/usr" ]
273284
274285 return paths
@@ -566,8 +577,9 @@ def make_launch_script(self):
566577
567578libs = ["png" , "freetype" ]
568579lib_dirs = []
569- ext_comp_args = (
570- [
580+ ext_comp_args = []
581+ if is_mingw or not WIN :
582+ ext_comp_args .extend ([
571583 "-Werror=return-type" ,
572584 "-Wunused-variable" ,
573585 "-Wno-switch" ,
@@ -576,27 +588,27 @@ def make_launch_script(self):
576588 "-Wno-char-subscripts" ,
577589 # optimizations
578590 "-Og" if DEBUG else "-O3" ,
579- ]
580- if not WIN
581- else ["/MP" ]
582- )
591+ ])
592+ else : # MSVC
593+ ext_comp_args .extend ([
594+ "/MP" ,
595+ "/std:c++17" ,
596+ ])
583597ext_link_args = []
584598ext_objects = []
585599data_files = []
586600ext_modules = []
587601
588602if options .use_openmp == "yes" :
589- def_macros += [
590- ("PYMOL_OPENMP" , None ),
591- ]
592- if MAC :
593- ext_comp_args += ["-Xpreprocessor" , "-fopenmp" ]
594- libs += ["omp" ]
595- elif WIN :
596- ext_comp_args += ["/openmp" ]
597- else :
598- ext_comp_args += ["-fopenmp" ]
599- ext_link_args += ["-fopenmp" ]
603+ def_macros += [("PYMOL_OPENMP" , None )]
604+ if WIN and not is_mingw : # MSVC
605+ ext_comp_args .append ("/openmp" )
606+ elif MAC : # macOS Clang
607+ ext_comp_args .extend (["-Xpreprocessor" , "-fopenmp" ])
608+ libs .append ("omp" )
609+ else : # GCC/Clang on Linux, and MinGW on Windows
610+ ext_comp_args .append ("-fopenmp" )
611+ ext_link_args .append ("-fopenmp" )
600612
601613if options .vmd_plugins :
602614 # VMD plugin support
@@ -701,14 +713,18 @@ def make_launch_script(self):
701713 )
702714
703715 if DEBUG :
704- ext_comp_args += ["/Z7" ]
705- ext_link_args += ["/DEBUG" ]
716+ if is_mingw :
717+ ext_comp_args += ["-g" ]
718+ else :
719+ ext_comp_args += ["/Z7" ]
720+ ext_link_args += ["/DEBUG" ]
706721
707722 libs += [
708723 "opengl32" ,
709724 ]
710- # TODO: Remove when we move to setup-CMake
711- ext_comp_args += ["/std:c++17" ]
725+ if not is_mingw :
726+ # TODO: Remove when we move to setup-CMake
727+ ext_comp_args += ["/std:c++17" ]
712728
713729if not (MAC or WIN ):
714730 libs += [
@@ -814,10 +830,26 @@ def get_packages(base, parent="", r=None):
814830champ_inc_dirs .append (sysconfig .get_paths ()["include" ])
815831champ_inc_dirs .append (sysconfig .get_paths ()["platinclude" ])
816832
833+ champ_libs = []
834+
817835if WIN :
818- # pyconfig.py forces linking against pythonXY.lib on MSVC
819- py_lib = pathlib .Path (sysconfig .get_paths ()["stdlib" ]).parent / "libs"
820- lib_dirs .append (str (py_lib ))
836+ if not is_mingw :
837+ # pyconfig.py forces linking against pythonXY.lib on MSVC
838+ py_lib = pathlib .Path (sysconfig .get_paths ()["stdlib" ]).parent / "libs"
839+ lib_dirs .append (str (py_lib ))
840+ else :
841+ ldversion = (
842+ sysconfig .get_config_var ("LDVERSION" )
843+ or f"{ sys .version_info .major } .{ sys .version_info .minor } "
844+ )
845+ libs .append (f"python{ ldversion } " )
846+ champ_libs .append (f"python{ ldversion } " )
847+ mingw_libdir = (
848+ sysconfig .get_config_var ("LIBDIR" )
849+ or os .path .join (sys .prefix , "lib" )
850+ )
851+ if mingw_libdir and os .path .isdir (mingw_libdir ):
852+ lib_dirs .append (mingw_libdir )
821853
822854ext_modules += [
823855 CMakeExtension (
@@ -834,6 +866,7 @@ def get_packages(base, parent="", r=None):
834866 name = "chempy.champ._champ" ,
835867 sources = get_sources (["contrib/champ" ]),
836868 include_dirs = champ_inc_dirs ,
869+ libraries = champ_libs ,
837870 library_dirs = lib_dirs ,
838871 ),
839872]
0 commit comments