availability.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import os
  2. from .compilation import compile_run_strings
  3. from .util import CompilerNotFoundError
  4. def has_fortran():
  5. if not hasattr(has_fortran, 'result'):
  6. try:
  7. (stdout, stderr), info = compile_run_strings(
  8. [('main.f90', (
  9. 'program foo\n'
  10. 'print *, "hello world"\n'
  11. 'end program'
  12. ))], clean=True
  13. )
  14. except CompilerNotFoundError:
  15. has_fortran.result = False
  16. if os.environ.get('SYMPY_STRICT_COMPILER_CHECKS', '0') == '1':
  17. raise
  18. else:
  19. if info['exit_status'] != os.EX_OK or 'hello world' not in stdout:
  20. if os.environ.get('SYMPY_STRICT_COMPILER_CHECKS', '0') == '1':
  21. raise ValueError("Failed to compile test program:\n%s\n%s\n" % (stdout, stderr))
  22. has_fortran.result = False
  23. else:
  24. has_fortran.result = True
  25. return has_fortran.result
  26. def has_c():
  27. if not hasattr(has_c, 'result'):
  28. try:
  29. (stdout, stderr), info = compile_run_strings(
  30. [('main.c', (
  31. '#include <stdio.h>\n'
  32. 'int main(){\n'
  33. 'printf("hello world\\n");\n'
  34. 'return 0;\n'
  35. '}'
  36. ))], clean=True
  37. )
  38. except CompilerNotFoundError:
  39. has_c.result = False
  40. if os.environ.get('SYMPY_STRICT_COMPILER_CHECKS', '0') == '1':
  41. raise
  42. else:
  43. if info['exit_status'] != os.EX_OK or 'hello world' not in stdout:
  44. if os.environ.get('SYMPY_STRICT_COMPILER_CHECKS', '0') == '1':
  45. raise ValueError("Failed to compile test program:\n%s\n%s\n" % (stdout, stderr))
  46. has_c.result = False
  47. else:
  48. has_c.result = True
  49. return has_c.result
  50. def has_cxx():
  51. if not hasattr(has_cxx, 'result'):
  52. try:
  53. (stdout, stderr), info = compile_run_strings(
  54. [('main.cxx', (
  55. '#include <iostream>\n'
  56. 'int main(){\n'
  57. 'std::cout << "hello world" << std::endl;\n'
  58. '}'
  59. ))], clean=True
  60. )
  61. except CompilerNotFoundError:
  62. has_cxx.result = False
  63. if os.environ.get('SYMPY_STRICT_COMPILER_CHECKS', '0') == '1':
  64. raise
  65. else:
  66. if info['exit_status'] != os.EX_OK or 'hello world' not in stdout:
  67. if os.environ.get('SYMPY_STRICT_COMPILER_CHECKS', '0') == '1':
  68. raise ValueError("Failed to compile test program:\n%s\n%s\n" % (stdout, stderr))
  69. has_cxx.result = False
  70. else:
  71. has_cxx.result = True
  72. return has_cxx.result