1414import shutil
1515import sys
1616import warnings
17+ from pathlib import Path
1718
1819from setuptools import Extension , setup
1920from setuptools .command .build_ext import build_ext
2223# Update when tagging a new release.
2324FALLBACK_VERSION = "1.4.3"
2425
25- MYDIR = os . path . dirname ( os . path . abspath ( __file__ ))
26+ MYDIR = str ( Path ( __file__ ). parent . resolve ( ))
2627
2728# Helper functions -----------------------------------------------------------
2829
2930
3031def get_compiler_type ():
31- """find compiler used for building extensions."""
32+ """Find compiler used for building extensions."""
3233 cc_arg = [a for a in sys .argv if a .startswith ("--compiler=" )]
3334 if cc_arg :
34- compiler_type = cc_arg [- 1 ].split ("=" , 1 )[1 ]
35- else :
36- from distutils .ccompiler import new_compiler
35+ return cc_arg [- 1 ].split ("=" , 1 )[1 ]
36+ from distutils .ccompiler import new_compiler
3737
38- compiler_type = new_compiler ().compiler_type
39- return compiler_type
38+ return new_compiler ().compiler_type
4039
4140
4241def get_gsl_config ():
4342 """Return dictionary with paths to GSL library."""
44- gslcfgpaths = [os . path . join ( p , "gsl-config" ) for p in ([MYDIR ] + os .environ ["PATH" ].split (os .pathsep ))]
45- gslcfgpaths = [p for p in gslcfgpaths if os . path . isfile ( p )]
43+ gslcfgpaths = [Path ( p ) / "gsl-config" for p in ([MYDIR ] + os .environ ["PATH" ].split (os .pathsep ))]
44+ gslcfgpaths = [p for p in gslcfgpaths if p . is_file ( )]
4645 rv = {"include_dirs" : [], "library_dirs" : []}
4746 if not gslcfgpaths :
48- wmsg = "Cannot find gsl-config in {!r} nor in system PATH."
49- warnings .warn (wmsg .format (MYDIR ))
47+ warnings .warn (f"Cannot find gsl-config in { MYDIR } nor in system PATH." )
5048 return rv
5149 gslcfg = gslcfgpaths [0 ]
52- with open (gslcfg ) as fp :
53- txt = fp .read ()
54- mprefix = re .search ("(?m)^prefix=(.+)" , txt )
50+ txt = gslcfg .read_text ()
51+ mprefix = re .search (r"(?m)^prefix=(.+)" , txt )
5552 minclude = re .search (r"(?m)^[^#]*\s-I(\S+)" , txt )
5653 mlibpath = re .search (r"(?m)^[^#]*\s-L(\S+)" , txt )
5754 if not mprefix :
58- emsg = "Cannot find 'prefix=' line in {}."
59- raise RuntimeError (emsg .format (gslcfg ))
60- p = mprefix .group (1 )
61- inc = minclude .group (1 ) if minclude else (p + "/include" )
62- lib = mlibpath .group (1 ) if mlibpath else (p + "/lib" )
63- rv ["include_dirs" ] += [inc ]
64- rv ["library_dirs" ] += [lib ]
55+ raise RuntimeError (f"Cannot find 'prefix=' line in { gslcfg } ." )
56+ p = Path (mprefix .group (1 ))
57+ rv ["include_dirs" ].append (str (minclude .group (1 ) if minclude else p / "include" ))
58+ rv ["library_dirs" ].append (str (mlibpath .group (1 ) if mlibpath else p / "lib" ))
6559 return rv
6660
6761
6862def get_gsl_config_win ():
6963 """Return dictionary with paths to GSL library on Windows."""
70- gsl_path = os .environ .get ("GSL_PATH" )
64+ gsl_path = os .environ .get ("GSL_PATH" , "" )
7165 if gsl_path :
72- inc = os . path . join (gsl_path , "include" )
73- lib = os . path . join (gsl_path , "lib" )
66+ inc = Path (gsl_path ) / "include"
67+ lib = Path (gsl_path ) / "lib"
7468 else :
7569 conda_prefix = os .environ .get ("CONDA_PREFIX" )
7670 if conda_prefix :
77- inc = os . path . join (conda_prefix , "Library" , "include" )
78- lib = os . path . join (conda_prefix , "Library" , "lib" )
71+ inc = Path (conda_prefix ) / "Library" / "include"
72+ lib = Path (conda_prefix ) / "Library" / "lib"
7973 else :
8074 raise EnvironmentError (
8175 "Neither GSL_PATH nor CONDA_PREFIX environment variables are set. "
8276 "Please ensure GSL is installed and GSL_PATH is correctly set."
8377 )
84-
85- return {"include_dirs" : [inc ], "library_dirs" : [lib ]}
78+ return {"include_dirs" : [str (inc )], "library_dirs" : [str (lib )]}
8679
8780
8881class CustomBuildExt (build_ext ):
8982 def run (self ):
9083 super ().run ()
91- gsl_path = os .environ .get ("GSL_PATH" ) or os .path .join (os .environ .get ("CONDA_PREFIX" , "" ), "Library" )
92-
93- bin_path = os .path .join (gsl_path , "bin" )
94- dest_path = os .path .join (self .build_lib , "diffpy" , "pdffit2" )
95- os .makedirs (dest_path , exist_ok = True )
84+ gsl_path = (
85+ Path (os .environ .get ("GSL_PATH" ))
86+ if os .environ .get ("GSL_PATH" )
87+ else Path (os .environ .get ("CONDA_PREFIX" , "" )) / "Library"
88+ )
89+ bin_path = gsl_path / "bin"
90+ dest_path = Path (self .build_lib ) / "diffpy" / "pdffit2"
91+ dest_path .mkdir (parents = True , exist_ok = True )
9692
97- for dll_file in glob .glob (os . path . join ( bin_path , "gsl*.dll" ) ):
98- shutil .copy (dll_file , dest_path )
93+ for dll_file in bin_path .glob ("gsl*.dll" ):
94+ shutil .copy (str ( dll_file ), str ( dest_path ) )
9995
10096
10197# ----------------------------------------------------------------------------
10298
103- # compile and link options
104- define_macros = []
99+ # Compile and link options
105100os_name = os .name
106101if os_name == "nt" :
107102 gcfg = get_gsl_config_win ()
108103else :
109104 gcfg = get_gsl_config ()
110- include_dirs = [MYDIR ] + gcfg ["include_dirs" ]
111- library_dirs = []
105+
112106if sys .platform == "darwin" :
113107 libraries = []
114108else :
115109 libraries = ["gsl" ]
110+
111+ include_dirs = [MYDIR ] + gcfg ["include_dirs" ]
112+ library_dirs = []
113+ define_macros = []
116114extra_objects = []
117115extra_compile_args = []
118116extra_link_args = []
119117
118+
120119compiler_type = get_compiler_type ()
121120if compiler_type in ("unix" , "cygwin" , "mingw32" ):
122121 extra_compile_args = ["-std=c++11" , "-Wall" , "-Wno-write-strings" , "-O3" , "-funroll-loops" , "-ffast-math" ]
@@ -129,7 +128,7 @@ def run(self):
129128 library_dirs += gcfg ["library_dirs" ]
130129# add optimization flags for other compilers if needed
131130
132- # define extension arguments here
131+ # Define extension arguments
133132ext_kws = {
134133 "include_dirs" : include_dirs ,
135134 "libraries" : libraries ,
@@ -141,7 +140,7 @@ def run(self):
141140}
142141
143142
144- # define extension here
143+ # Define extensions
145144def create_extensions ():
146145 ext = Extension ("diffpy.pdffit2.pdffit2" , glob .glob ("src/extensions/**/*.cc" ), ** ext_kws )
147146 return [ext ]
0 commit comments