test_build_ext.py 2.6 KB

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