autodist.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. """This module implements additional tests ala autoconf which can be useful.
  2. """
  3. import textwrap
  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 = textwrap.dedent("""
  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 = textwrap.dedent("""
  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_gcc(cmd):
  39. """Check if the compiler is GCC."""
  40. cmd._check_compiler()
  41. body = textwrap.dedent("""
  42. int
  43. main()
  44. {
  45. #if (! defined __GNUC__)
  46. #error gcc required
  47. #endif
  48. return 0;
  49. }
  50. """)
  51. return cmd.try_compile(body, None, None)
  52. def check_gcc_version_at_least(cmd, major, minor=0, patchlevel=0):
  53. """
  54. Check that the gcc version is at least the specified version."""
  55. cmd._check_compiler()
  56. version = '.'.join([str(major), str(minor), str(patchlevel)])
  57. body = textwrap.dedent("""
  58. int
  59. main()
  60. {
  61. #if (! defined __GNUC__) || (__GNUC__ < %(major)d) || \\
  62. (__GNUC_MINOR__ < %(minor)d) || \\
  63. (__GNUC_PATCHLEVEL__ < %(patchlevel)d)
  64. #error gcc >= %(version)s required
  65. #endif
  66. return 0;
  67. }
  68. """)
  69. kw = {'version': version, 'major': major, 'minor': minor,
  70. 'patchlevel': patchlevel}
  71. return cmd.try_compile(body % kw, None, None)
  72. def check_gcc_function_attribute(cmd, attribute, name):
  73. """Return True if the given function attribute is supported."""
  74. cmd._check_compiler()
  75. body = textwrap.dedent("""
  76. #pragma GCC diagnostic error "-Wattributes"
  77. #pragma clang diagnostic error "-Wattributes"
  78. int %s %s(void* unused)
  79. {
  80. return 0;
  81. }
  82. int
  83. main()
  84. {
  85. return 0;
  86. }
  87. """) % (attribute, name)
  88. return cmd.try_compile(body, None, None) != 0
  89. def check_gcc_function_attribute_with_intrinsics(cmd, attribute, name, code,
  90. include):
  91. """Return True if the given function attribute is supported with
  92. intrinsics."""
  93. cmd._check_compiler()
  94. body = textwrap.dedent("""
  95. #include<%s>
  96. int %s %s(void)
  97. {
  98. %s;
  99. return 0;
  100. }
  101. int
  102. main()
  103. {
  104. return 0;
  105. }
  106. """) % (include, attribute, name, code)
  107. return cmd.try_compile(body, None, None) != 0
  108. def check_gcc_variable_attribute(cmd, attribute):
  109. """Return True if the given variable attribute is supported."""
  110. cmd._check_compiler()
  111. body = textwrap.dedent("""
  112. #pragma GCC diagnostic error "-Wattributes"
  113. #pragma clang diagnostic error "-Wattributes"
  114. int %s foo;
  115. int
  116. main()
  117. {
  118. return 0;
  119. }
  120. """) % (attribute, )
  121. return cmd.try_compile(body, None, None) != 0