|
| 1 | +# Kanged out of numpy.f2py.tests.util for test_build_ext |
| 2 | +from numpy.testing import IS_WASM |
| 3 | +import textwrap |
| 4 | +import shutil |
| 5 | +import tempfile |
| 6 | +import os |
| 7 | +import re |
| 8 | +import subprocess |
| 9 | +import sys |
| 10 | + |
| 11 | +# |
| 12 | +# Check if compilers are available at all... |
| 13 | +# |
| 14 | + |
| 15 | +_compiler_status = None |
| 16 | + |
| 17 | + |
| 18 | +def _get_compiler_status(): |
| 19 | + global _compiler_status |
| 20 | + if _compiler_status is not None: |
| 21 | + return _compiler_status |
| 22 | + |
| 23 | + _compiler_status = (False, False, False) |
| 24 | + if IS_WASM: |
| 25 | + # Can't run compiler from inside WASM. |
| 26 | + return _compiler_status |
| 27 | + |
| 28 | + # XXX: this is really ugly. But I don't know how to invoke Distutils |
| 29 | + # in a safer way... |
| 30 | + code = textwrap.dedent( |
| 31 | + f"""\ |
| 32 | + import os |
| 33 | + import sys |
| 34 | + sys.path = {repr(sys.path)} |
| 35 | +
|
| 36 | + def configuration(parent_name='',top_path=None): |
| 37 | + global config |
| 38 | + from numpy.distutils.misc_util import Configuration |
| 39 | + config = Configuration('', parent_name, top_path) |
| 40 | + return config |
| 41 | +
|
| 42 | + from numpy.distutils.core import setup |
| 43 | + setup(configuration=configuration) |
| 44 | +
|
| 45 | + config_cmd = config.get_config_cmd() |
| 46 | + have_c = config_cmd.try_compile('void foo() {{}}') |
| 47 | + print('COMPILERS:%%d,%%d,%%d' %% (have_c, |
| 48 | + config.have_f77c(), |
| 49 | + config.have_f90c())) |
| 50 | + sys.exit(99) |
| 51 | + """ |
| 52 | + ) |
| 53 | + code = code % dict(syspath=repr(sys.path)) |
| 54 | + |
| 55 | + tmpdir = tempfile.mkdtemp() |
| 56 | + try: |
| 57 | + script = os.path.join(tmpdir, "setup.py") |
| 58 | + |
| 59 | + with open(script, "w") as f: |
| 60 | + f.write(code) |
| 61 | + |
| 62 | + cmd = [sys.executable, "setup.py", "config"] |
| 63 | + p = subprocess.Popen( |
| 64 | + cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=tmpdir |
| 65 | + ) |
| 66 | + out, err = p.communicate() |
| 67 | + finally: |
| 68 | + shutil.rmtree(tmpdir) |
| 69 | + |
| 70 | + m = re.search(rb"COMPILERS:(\d+),(\d+),(\d+)", out) |
| 71 | + if m: |
| 72 | + _compiler_status = ( |
| 73 | + bool(int(m.group(1))), |
| 74 | + bool(int(m.group(2))), |
| 75 | + bool(int(m.group(3))), |
| 76 | + ) |
| 77 | + # Finished |
| 78 | + return _compiler_status |
| 79 | + |
| 80 | + |
| 81 | +def has_c_compiler(): |
| 82 | + return _get_compiler_status()[0] |
| 83 | + |
| 84 | + |
| 85 | +def has_f77_compiler(): |
| 86 | + return _get_compiler_status()[1] |
| 87 | + |
| 88 | + |
| 89 | +def has_f90_compiler(): |
| 90 | + return _get_compiler_status()[2] |
0 commit comments