2020
2121from Cython .Build import cythonize
2222from Cython .Distutils .build_ext import new_build_ext as build_ext
23- import distutils .ccompiler
2423from setuptools import Extension , setup
2524
2625TRACE = bool (int (os .getenv ('TRACE' , 0 )))
2726DEBUG = bool (int (os .getenv ('DEBUG' , 0 ))) or TRACE
2827ASAN = bool (int (os .getenv ('ASAN' , 0 )))
2928
3029ROOT_DIR = os .path .abspath (os .path .dirname (__file__ ))
31- CXX = distutils .ccompiler .get_default_compiler ()
32-
33- # Construct vcpkg lib and include paths
34- def _vcpkg_path ():
35- osname = platform .system ().lower ().replace ('darwin' , 'osx' )
36- arch = platform .machine ().lower ()
37- if os .environ .get ('_PYTHON_HOST_PLATFORM' , '' ).startswith ('macosx-' ):
38- arch = os .environ ['_PYTHON_HOST_PLATFORM' ].split ('-' )[- 1 ]
39- elif osname == 'linux' and arch == 'arm64' :
40- arch = 'aarch64'
41- arch = arch .replace ('x86_64' , 'x64' ).replace ('amd64' , 'x64' )
42- triplet = f'{ arch } -{ osname } '
43-
44- if os .environ .get ('RESILIPARSE_VCPKG_PATH' ):
45- return os .path .join (os .environ ['RESILIPARSE_VCPKG_PATH' ], triplet )
46- return os .path .join (os .path .dirname (ROOT_DIR ), 'vcpkg_installed' , triplet )
47-
48- INCLUDE_PATH = os .path .join (_vcpkg_path (), 'include' )
49- LIBRARY_PATH = os .path .join (_vcpkg_path (), 'lib' )
50-
51-
52- def get_cpp_args ():
53- cpp_args = {}
54-
55- if TRACE :
56- cpp_args .update (dict (define_macros = [('CYTHON_TRACE_NOGIL' , '1' )]))
57-
58- if CXX == 'unix' :
59- cpp_args .update (dict (
60- extra_compile_args = ['-std=c++17' ,
61- f'-O{ 0 if DEBUG else 3 } ' ,
62- f'-I{ INCLUDE_PATH } ' ,
63- '-Wall' ,
64- '-Wno-deprecated-declarations' ,
65- '-Wno-unreachable-code' ,
66- '-Wno-unused-function' ],
67- extra_link_args = ['-std=c++17' , f'-L{ LIBRARY_PATH } ' , f'-Wl,-rpath,{ LIBRARY_PATH } ' ]
68- ))
69- if DEBUG :
70- cpp_args ['extra_compile_args' ].append ('-Werror' )
71- if ASAN :
72- cpp_args ['extra_compile_args' ].append ('-fsanitize=address' )
73- cpp_args ['extra_link_args' ].append ('-fsanitize=address' )
74- if platform .system () == 'Darwin' :
75- cpp_args ['extra_link_args' ].append ('-headerpad_max_install_names' )
76-
77- elif CXX == 'msvc' :
78- cpp_args .update (dict (
79- extra_compile_args = ['/std:c++latest' ,
80- '/W3' ,
81- f'/O{ "d" if DEBUG else 2 } ' ,
82- f'/I{ INCLUDE_PATH } ' ],
83- extra_link_args = [f'/LIBPATH:{ LIBRARY_PATH } ' ]
84- ))
85- if DEBUG :
86- cpp_args ['extra_compile_args' ].append ('/WX' )
87- cpp_args ['extra_link_args' ].append ('/WX' )
88-
89- return cpp_args
30+
31+
32+ class resiliparse_build_ext (build_ext ):
33+ def build_extension (self , ext ):
34+ for k , v in self .get_cpp_args ().items ():
35+ setattr (ext , k , v )
36+ return super ().build_extension (ext )
37+
38+ def get_vcpkg_path (self ):
39+ osname = platform .system ().lower ().replace ('darwin' , 'osx' )
40+ arch = platform .machine ().lower ()
41+ if os .environ .get ('_PYTHON_HOST_PLATFORM' , '' ).startswith ('macosx-' ):
42+ arch = os .environ ['_PYTHON_HOST_PLATFORM' ].split ('-' )[- 1 ]
43+ elif osname == 'linux' and arch == 'arm64' :
44+ arch = 'aarch64'
45+ arch = arch .replace ('x86_64' , 'x64' ).replace ('amd64' , 'x64' )
46+ triplet = f'{ arch } -{ osname } '
47+
48+ if os .environ .get ('RESILIPARSE_VCPKG_PATH' ):
49+ return os .path .join (os .environ ['RESILIPARSE_VCPKG_PATH' ], triplet )
50+ return os .path .join (os .path .dirname (ROOT_DIR ), 'vcpkg_installed' , triplet )
51+
52+ def get_cpp_args (self ):
53+ include_path = os .path .join (self .get_vcpkg_path (), 'include' )
54+ library_path = os .path .join (self .get_vcpkg_path (), 'lib' )
55+ cpp_args = {}
56+
57+ if TRACE :
58+ cpp_args .update (dict (define_macros = [('CYTHON_TRACE_NOGIL' , '1' )]))
59+
60+ if self .compiler .compiler_type == 'unix' :
61+ cpp_args .update (dict (
62+ extra_compile_args = ['-std=c++17' ,
63+ f'-O{ 0 if DEBUG else 3 } ' ,
64+ f'-I{ include_path } ' ,
65+ '-Wall' ,
66+ '-Wno-deprecated-declarations' ,
67+ '-Wno-unreachable-code' ,
68+ '-Wno-unused-function' ],
69+ extra_link_args = ['-std=c++17' , f'-L{ library_path } ' , f'-Wl,-rpath,{ library_path } ' ]
70+ ))
71+ if DEBUG :
72+ cpp_args ['extra_compile_args' ].append ('-Werror' )
73+ if ASAN :
74+ cpp_args ['extra_compile_args' ].append ('-fsanitize=address' )
75+ cpp_args ['extra_link_args' ].append ('-fsanitize=address' )
76+ if platform .system () == 'Darwin' :
77+ cpp_args ['extra_link_args' ].append ('-headerpad_max_install_names' )
78+
79+ elif self .compiler .compiler_type == 'msvc' :
80+ cpp_args .update (dict (
81+ extra_compile_args = ['/std:c++latest' ,
82+ '/W3' ,
83+ f'/O{ "d" if DEBUG else 2 } ' ,
84+ f'/I{ include_path } ' ],
85+ extra_link_args = [f'/LIBPATH:{ library_path } ' ]
86+ ))
87+ if DEBUG :
88+ cpp_args ['extra_compile_args' ].append ('/WX' )
89+ cpp_args ['extra_link_args' ].append ('/WX' )
90+
91+ return cpp_args
9092
9193
9294def get_cython_args ():
@@ -105,27 +107,25 @@ def get_cython_args():
105107
106108
107109def get_ext_modules ():
108- cpp_args = get_cpp_args ()
109-
110110 resiliparse_extensions = [
111111 Extension ('resiliparse.itertools' ,
112- sources = [f'resiliparse/itertools.pyx' ], ** cpp_args ),
112+ sources = [f'resiliparse/itertools.pyx' ]),
113113 Extension ('resiliparse.extract.html2text' ,
114- sources = [f'resiliparse/extract/html2text.pyx' ], libraries = ['lexbor' , 're2' ], ** cpp_args ),
114+ sources = [f'resiliparse/extract/html2text.pyx' ], libraries = ['lexbor' , 're2' ]),
115115 Extension ('resiliparse.parse.encoding' ,
116- sources = [f'resiliparse/parse/encoding.pyx' ], libraries = ['uchardet' , 'lexbor' ], ** cpp_args ),
116+ sources = [f'resiliparse/parse/encoding.pyx' ], libraries = ['uchardet' , 'lexbor' ]),
117117 Extension ('resiliparse.parse.html' ,
118- sources = [f'resiliparse/parse/html.pyx' ], libraries = ['lexbor' ], ** cpp_args ),
118+ sources = [f'resiliparse/parse/html.pyx' ], libraries = ['lexbor' ]),
119119 Extension ('resiliparse.parse.http' ,
120- sources = [f'resiliparse/parse/http.pyx' ], ** cpp_args ),
120+ sources = [f'resiliparse/parse/http.pyx' ]),
121121 Extension ('resiliparse.parse.lang' ,
122- sources = [f'resiliparse/parse/lang.pyx' ], ** cpp_args ),
122+ sources = [f'resiliparse/parse/lang.pyx' ]),
123123 ]
124124 if os .name == 'posix' :
125125 # Process Guards are unsupported on Windows
126126 resiliparse_extensions .append (
127127 Extension ('resiliparse.process_guard' , sources = [f'resiliparse/process_guard.pyx' ],
128- libraries = ['pthread' ], ** cpp_args )
128+ libraries = ['pthread' ])
129129 )
130130
131131 return cythonize (resiliparse_extensions , ** get_cython_args ())
@@ -138,7 +138,7 @@ def get_ext_modules():
138138
139139setup (
140140 ext_modules = get_ext_modules (),
141- cmdclass = dict (build_ext = build_ext ),
141+ cmdclass = dict (build_ext = resiliparse_build_ext ),
142142 exclude_package_data = {
143143 '' : [] if 'sdist' in sys .argv else ['*.pxd' , '*.pxi' , '*.pyx' , '*.h' , '*.cpp' ]
144144 }
0 commit comments