autodist.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. """This module implements additional tests ala autoconf which can be useful.
  2. """
  3. from __future__ import division, absolute_import, print_function
  4. # We put them here since they could be easily reused outside numpy.distutils
  5. def check_inline(cmd):
  6. """Return the inline identifier (may be empty)."""
  7. cmd._check_compiler()
  8. body = """
  9. #ifndef __cplusplus
  10. static %(inline)s int static_func (void)
  11. {
  12. return 0;
  13. }
  14. %(inline)s int nostatic_func (void)
  15. {
  16. return 0;
  17. }
  18. #endif"""
  19. for kw in ['inline', '__inline__', '__inline']:
  20. st = cmd.try_compile(body % {'inline': kw}, None, None)
  21. if st:
  22. return kw
  23. return ''
  24. def check_restrict(cmd):
  25. """Return the restrict identifier (may be empty)."""
  26. cmd._check_compiler()
  27. body = """
  28. static int static_func (char * %(restrict)s a)
  29. {
  30. return 0;
  31. }
  32. """
  33. for kw in ['restrict', '__restrict__', '__restrict']:
  34. st = cmd.try_compile(body % {'restrict': kw}, None, None)
  35. if st:
  36. return kw
  37. return ''
  38. def check_compiler_gcc4(cmd):
  39. """Return True if the C compiler is GCC 4.x."""
  40. cmd._check_compiler()
  41. body = """
  42. int
  43. main()
  44. {
  45. #if (! defined __GNUC__) || (__GNUC__ < 4)
  46. #error gcc >= 4 required
  47. #endif
  48. return 0;
  49. }
  50. """
  51. return cmd.try_compile(body, None, None)
  52. def check_gcc_function_attribute(cmd, attribute, name):
  53. """Return True if the given function attribute is supported."""
  54. cmd._check_compiler()
  55. body = """
  56. #pragma GCC diagnostic error "-Wattributes"
  57. #pragma clang diagnostic error "-Wattributes"
  58. int %s %s(void*);
  59. int
  60. main()
  61. {
  62. return 0;
  63. }
  64. """ % (attribute, name)
  65. return cmd.try_compile(body, None, None) != 0
  66. def check_gcc_variable_attribute(cmd, attribute):
  67. """Return True if the given variable attribute is supported."""
  68. cmd._check_compiler()
  69. body = """
  70. #pragma GCC diagnostic error "-Wattributes"
  71. #pragma clang diagnostic error "-Wattributes"
  72. int %s foo;
  73. int
  74. main()
  75. {
  76. return 0;
  77. }
  78. """ % (attribute, )
  79. return cmd.try_compile(body, None, None) != 0