8000 MAINT: Update ``extbuild.py`` from main. by charris · Pull Request #24353 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

MAINT: Update extbuild.py from main. #24353

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 39 additions & 42 deletions numpy/testing/_private/extbuild.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

import os
import pathlib
import subprocess
import sys
import sysconfig
import textwrap

__all__ = ['build_and_import_extension', 'compile_extension_module']

Expand Down Expand Up @@ -51,8 +53,6 @@ def build_and_import_extension(
>>> assert not mod.test_bytes(u'abc')
>>> assert mod.test_bytes(b'abc')
"""
from distutils.errors import CompileError

body = prologue + _make_methods(functions, modname)
init = """PyObject *mod = PyModule_Create(&moduledef);
"""
Expand All @@ -67,7 +67,7 @@ def build_and_import_extension(
try:
mod_so = compile_extension_module(
modname, build_dir, include_dirs, source_string)
except CompileError as e:
except Exception as e:
# shorten the exception chain
raise RuntimeError(f"could not compile in {build_dir}:") from e
import importlib.util
Expand Down Expand Up @@ -186,9 +186,9 @@ def _c_compile(cfile, outputfilename, include_dirs=[], libraries=[],
elif sys.platform.startswith('linux'):
compile_extra = [
"-O0", "-g", "-Werror=implicit-function-declaration", "-fPIC"]
link_extra = None
link_extra = []
else:
compile_extra = link_extra = None
compile_extra = link_extra = []
pass
if sys.platform == 'win32':
link_extra = link_extra + ['/DEBUG'] # generate .pdb file
Expand All @@ -202,49 +202,46 @@ def _c_compile(cfile, outputfilename, include_dirs=[], libraries=[],
library_dirs.append(s + 'lib')

outputfilename = outputfilename.with_suffix(get_so_suffix())
saved_environ = os.environ.copy()
try:
build(
cfile, outputfilename,
compile_extra, link_extra,
include_dirs, libraries, library_dirs)
finally:
# workaround for a distutils bugs where some env vars can
# become longer and longer every time it is used
for key, value in saved_environ.items():
if os.environ.get(key) != value:
os.environ[key] = value
build(
cfile, outputfilename,
compile_extra, link_extra,
include_dirs, libraries, library_dirs)
return outputfilename


def build(cfile, outputfilename, compile_extra, link_extra,
include_dirs, libraries, library_dirs):
"cd into the directory where the cfile is, use distutils to build"
from numpy.distutils.ccompiler import new_compiler

compiler = new_compiler(force=1, verbose=2)
compiler.customize('')
objects = []

old = os.getcwd()
os.chdir(cfile.parent)
try:
res = compiler.compile(
[str(cfile.name)],
include_dirs=include_dirs,
extra_preargs=compile_extra
"use meson to build"

build_dir = cfile.parent / "build"
os.makedirs(build_dir, exist_ok=True)
so_name = outputfilename.parts[-1]
with open(cfile.parent / "meson.build", "wt") as fid:
includes = ['-I' + d for d in include_dirs]
link_dirs = ['-L' + d for d in library_dirs]
fid.write(textwrap.dedent(f"""\
project('foo', 'c')
shared_module('{so_name}', '{cfile.parts[-1]}',
c_args: {includes} + {compile_extra},
link_args: {link_dirs} + {link_extra},
link_with: {libraries},
name_prefix: '',
name_suffix: 'dummy',
)
objects += [str(cfile.parent / r) for r in res]
finally:
os.chdir(old)

compiler.link_shared_object(
objects, str(outputfilename),
libraries=libraries,
extra_preargs=link_extra,
library_dirs=library_dirs)


"""))
if sys.platform == "win32":
subprocess.check_call(["meson", "setup",
"--buildtype=release",
"--vsenv", ".."],
cwd=build_dir,
)
else:
subprocess.check_call(["meson", "setup", "--vsenv", ".."],
cwd=build_dir
)
subprocess.check_call(["meson", "compile"], cwd=build_dir)
os.rename(str(build_dir / so_name) + ".dummy", cfile.parent / so_name)

def get_so_suffix():
ret = sysconfig.get_config_var('EXT_SUFFIX')
assert ret
Expand Down
0