autodist.py 3.0 KB

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