test_build_ext.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. '''Tests for numpy.distutils.build_ext.'''
  2. import os
  3. import subprocess
  4. import sys
  5. from textwrap import indent, dedent
  6. import pytest
  7. from numpy.testing import IS_WASM
  8. @pytest.mark.skipif(IS_WASM, reason="cannot start subprocess in wasm")
  9. @pytest.mark.slow
  10. def test_multi_fortran_libs_link(tmp_path):
  11. '''
  12. Ensures multiple "fake" static libraries are correctly linked.
  13. see gh-18295
  14. '''
  15. # We need to make sure we actually have an f77 compiler.
  16. # This is nontrivial, so we'll borrow the utilities
  17. # from f2py tests:
  18. from numpy.f2py.tests.util import has_f77_compiler
  19. if not has_f77_compiler():
  20. pytest.skip('No F77 compiler found')
  21. # make some dummy sources
  22. with open(tmp_path / '_dummy1.f', 'w') as fid:
  23. fid.write(indent(dedent('''\
  24. FUNCTION dummy_one()
  25. RETURN
  26. END FUNCTION'''), prefix=' '*6))
  27. with open(tmp_path / '_dummy2.f', 'w') as fid:
  28. fid.write(indent(dedent('''\
  29. FUNCTION dummy_two()
  30. RETURN
  31. END FUNCTION'''), prefix=' '*6))
  32. with open(tmp_path / '_dummy.c', 'w') as fid:
  33. # doesn't need to load - just needs to exist
  34. fid.write('int PyInit_dummyext;')
  35. # make a setup file
  36. with open(tmp_path / 'setup.py', 'w') as fid:
  37. srctree = os.path.join(os.path.dirname(__file__), '..', '..', '..')
  38. fid.write(dedent(f'''\
  39. def configuration(parent_package="", top_path=None):
  40. from numpy.distutils.misc_util import Configuration
  41. config = Configuration("", parent_package, top_path)
  42. config.add_library("dummy1", sources=["_dummy1.f"])
  43. config.add_library("dummy2", sources=["_dummy2.f"])
  44. config.add_extension("dummyext", sources=["_dummy.c"], libraries=["dummy1", "dummy2"])
  45. return config
  46. if __name__ == "__main__":
  47. import sys
  48. sys.path.insert(0, r"{srctree}")
  49. from numpy.distutils.core import setup
  50. setup(**configuration(top_path="").todict())'''))
  51. # build the test extensino and "install" into a temporary directory
  52. build_dir = tmp_path
  53. subprocess.check_call([sys.executable, 'setup.py', 'build', 'install',
  54. '--prefix', str(tmp_path / 'installdir'),
  55. '--record', str(tmp_path / 'tmp_install_log.txt'),
  56. ],
  57. cwd=str(build_dir),
  58. )
  59. # get the path to the so
  60. so = None
  61. with open(tmp_path /'tmp_install_log.txt') as fid:
  62. for line in fid:
  63. if 'dummyext' in line:
  64. so = line.strip()
  65. break
  66. assert so is not None