|
16 | 16 |
|
17 | 17 | def build_and_import_extension( |
18 | 18 | modname, functions, *, prologue="", build_dir=None, |
19 | | - include_dirs=[], more_init=""): |
| 19 | + include_dirs=None, more_init=""): |
20 | 20 | """ |
21 | 21 | Build and imports a c-extension module `modname` from a list of function |
22 | 22 | fragments `functions`. |
@@ -53,6 +53,8 @@ def build_and_import_extension( |
53 | 53 | >>> assert not mod.test_bytes('abc') |
54 | 54 | >>> assert mod.test_bytes(b'abc') |
55 | 55 | """ |
| 56 | + if include_dirs is None: |
| 57 | + include_dirs = [] |
56 | 58 | body = prologue + _make_methods(functions, modname) |
57 | 59 | init = """ |
58 | 60 | PyObject *mod = PyModule_Create(&moduledef); |
@@ -68,12 +70,8 @@ def build_and_import_extension( |
68 | 70 | init += more_init |
69 | 71 | init += "\nreturn mod;" |
70 | 72 | source_string = _make_source(modname, init, body) |
71 | | - try: |
72 | | - mod_so = compile_extension_module( |
73 | | - modname, build_dir, include_dirs, source_string) |
74 | | - except Exception as e: |
75 | | - # shorten the exception chain |
76 | | - raise RuntimeError(f"could not compile in {build_dir}:") from e |
| 73 | + mod_so = compile_extension_module( |
| 74 | + modname, build_dir, include_dirs, source_string) |
77 | 75 | import importlib.util |
78 | 76 | spec = importlib.util.spec_from_file_location(modname, mod_so) |
79 | 77 | foo = importlib.util.module_from_spec(spec) |
@@ -83,7 +81,7 @@ def build_and_import_extension( |
83 | 81 |
|
84 | 82 | def compile_extension_module( |
85 | 83 | name, builddir, include_dirs, |
86 | | - source_string, libraries=[], library_dirs=[]): |
| 84 | + source_string, libraries=None, library_dirs=None): |
87 | 85 | """ |
88 | 86 | Build an extension module and return the filename of the resulting |
89 | 87 | native code file. |
@@ -106,11 +104,14 @@ def compile_extension_module( |
106 | 104 | dirname = builddir / name |
107 | 105 | dirname.mkdir(exist_ok=True) |
108 | 106 | cfile = _convert_str_to_file(source_string, dirname) |
109 | | - include_dirs = include_dirs + [sysconfig.get_config_var('INCLUDEPY')] |
| 107 | + include_dirs = include_dirs if include_dirs else [] |
| 108 | + libraries = libraries if libraries else [] |
| 109 | + library_dirs = library_dirs if library_dirs else [] |
110 | 110 |
|
111 | 111 | return _c_compile( |
112 | 112 | cfile, outputfilename=dirname / modname, |
113 | | - include_dirs=include_dirs, libraries=[], library_dirs=[], |
| 113 | + include_dirs=include_dirs, libraries=libraries, |
| 114 | + library_dirs=library_dirs, |
114 | 115 | ) |
115 | 116 |
|
116 | 117 |
|
@@ -182,35 +183,23 @@ def _make_source(name, init, body): |
182 | 183 | return code |
183 | 184 |
|
184 | 185 |
|
185 | | -def _c_compile(cfile, outputfilename, include_dirs=[], libraries=[], |
186 | | - library_dirs=[]): |
| 186 | +def _c_compile(cfile, outputfilename, include_dirs, libraries, |
| 187 | + library_dirs): |
| 188 | + link_extra = [] |
187 | 189 | if sys.platform == 'win32': |
188 | 190 | compile_extra = ["/we4013"] |
189 | | - link_extra = ["/LIBPATH:" + os.path.join(sys.base_prefix, 'libs')] |
| 191 | + link_extra.append('/DEBUG') # generate .pdb file |
190 | 192 | elif sys.platform.startswith('linux'): |
191 | 193 | compile_extra = [ |
192 | 194 | "-O0", "-g", "-Werror=implicit-function-declaration", "-fPIC"] |
193 | | - link_extra = [] |
194 | 195 | else: |
195 | | - compile_extra = link_extra = [] |
| 196 | + compile_extra = [] |
196 | 197 | pass |
197 | | - if sys.platform == 'win32': |
198 | | - link_extra = link_extra + ['/DEBUG'] # generate .pdb file |
199 | | - if sys.platform == 'darwin': |
200 | | - # support Fink & Darwinports |
201 | | - for s in ('/sw/', '/opt/local/'): |
202 | | - if (s + 'include' not in include_dirs |
203 | | - and os.path.exists(s + 'include')): |
204 | | - include_dirs.append(s + 'include') |
205 | | - if s + 'lib' not in library_dirs and os.path.exists(s + 'lib'): |
206 | | - library_dirs.append(s + 'lib') |
207 | | - |
208 | | - outputfilename = outputfilename.with_suffix(get_so_suffix()) |
209 | | - build( |
| 198 | + |
| 199 | + return build( |
210 | 200 | cfile, outputfilename, |
211 | 201 | compile_extra, link_extra, |
212 | 202 | include_dirs, libraries, library_dirs) |
213 | | - return outputfilename |
214 | 203 |
|
215 | 204 |
|
216 | 205 | def build(cfile, outputfilename, compile_extra, link_extra, |
@@ -219,18 +208,17 @@ def build(cfile, outputfilename, compile_extra, link_extra, |
219 | 208 |
|
220 | 209 | build_dir = cfile.parent / "build" |
221 | 210 | os.makedirs(build_dir, exist_ok=True) |
222 | | - so_name = outputfilename.parts[-1] |
223 | 211 | with open(cfile.parent / "meson.build", "wt") as fid: |
224 | | - includes = ['-I' + d for d in include_dirs] |
225 | 212 | link_dirs = ['-L' + d for d in library_dirs] |
226 | 213 | fid.write(textwrap.dedent(f"""\ |
227 | 214 | project('foo', 'c') |
228 | | - shared_module('{so_name}', '{cfile.parts[-1]}', |
229 | | - c_args: {includes} + {compile_extra}, |
230 | | - link_args: {link_dirs} + {link_extra}, |
231 | | - link_with: {libraries}, |
232 | | - name_prefix: '', |
233 | | - name_suffix: 'dummy', |
| 215 | + py = import('python').find_installation(pure: false) |
| 216 | + py.extension_module( |
| 217 | + '{outputfilename.parts[-1]}', |
| 218 | + '{cfile.parts[-1]}', |
| 219 | + c_args: {compile_extra}, |
| 220 | + link_args: {link_dirs}, |
| 221 | + include_directories: {include_dirs}, |
234 | 222 | ) |
235 | 223 | """)) |
236 | 224 | if sys.platform == "win32": |
@@ -243,8 +231,12 @@ def build(cfile, outputfilename, compile_extra, link_extra, |
243 | 231 | subprocess.check_call(["meson", "setup", "--vsenv", ".."], |
244 | 232 | cwd=build_dir |
245 | 233 | ) |
| 234 | + |
| 235 | + so_name = outputfilename.parts[-1] + get_so_suffix() |
246 | 236 | subprocess.check_call(["meson", "compile"], cwd=build_dir) |
247 | | - os.rename(str(build_dir / so_name) + ".dummy", cfile.parent / so_name) |
| 237 | + os.rename(str(build_dir / so_name), cfile.parent / so_name) |
| 238 | + return cfile.parent / so_name |
| 239 | + |
248 | 240 |
|
249 | 241 | def get_so_suffix(): |
250 | 242 | ret = sysconfig.get_config_var('EXT_SUFFIX') |
|