test_ccompiler_opt_conf.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import unittest
  2. from os import sys, path
  3. is_standalone = __name__ == '__main__' and __package__ is None
  4. if is_standalone:
  5. sys.path.append(path.abspath(path.join(path.dirname(__file__), "..")))
  6. from ccompiler_opt import CCompilerOpt
  7. else:
  8. from numpy.distutils.ccompiler_opt import CCompilerOpt
  9. arch_compilers = dict(
  10. x86 = ("gcc", "clang", "icc", "iccw", "msvc"),
  11. x64 = ("gcc", "clang", "icc", "iccw", "msvc"),
  12. ppc64 = ("gcc", "clang"),
  13. ppc64le = ("gcc", "clang"),
  14. armhf = ("gcc", "clang"),
  15. aarch64 = ("gcc", "clang"),
  16. narch = ("gcc",)
  17. )
  18. class FakeCCompilerOpt(CCompilerOpt):
  19. fake_info = ("arch", "compiler", "extra_args")
  20. def __init__(self, *args, **kwargs):
  21. CCompilerOpt.__init__(self, None, **kwargs)
  22. def dist_compile(self, sources, flags, **kwargs):
  23. return sources
  24. def dist_info(self):
  25. return FakeCCompilerOpt.fake_info
  26. @staticmethod
  27. def dist_log(*args, stderr=False):
  28. pass
  29. class _TestConfFeatures(FakeCCompilerOpt):
  30. """A hook to check the sanity of configured features
  31. - before it called by the abstract class '_Feature'
  32. """
  33. def conf_features_partial(self):
  34. conf_all = self.conf_features
  35. for feature_name, feature in conf_all.items():
  36. self.test_feature(
  37. "attribute conf_features",
  38. conf_all, feature_name, feature
  39. )
  40. conf_partial = FakeCCompilerOpt.conf_features_partial(self)
  41. for feature_name, feature in conf_partial.items():
  42. self.test_feature(
  43. "conf_features_partial()",
  44. conf_partial, feature_name, feature
  45. )
  46. return conf_partial
  47. def test_feature(self, log, search_in, feature_name, feature_dict):
  48. error_msg = (
  49. "during validate '{}' within feature '{}', "
  50. "march '{}' and compiler '{}'\n>> "
  51. ).format(log, feature_name, self.cc_march, self.cc_name)
  52. if not feature_name.isupper():
  53. raise AssertionError(error_msg + "feature name must be in uppercase")
  54. for option, val in feature_dict.items():
  55. self.test_option_types(error_msg, option, val)
  56. self.test_duplicates(error_msg, option, val)
  57. self.test_implies(error_msg, search_in, feature_name, feature_dict)
  58. self.test_group(error_msg, search_in, feature_name, feature_dict)
  59. self.test_extra_checks(error_msg, search_in, feature_name, feature_dict)
  60. def test_option_types(self, error_msg, option, val):
  61. for tp, available in (
  62. ((str, list), (
  63. "implies", "headers", "flags", "group", "detect", "extra_checks"
  64. )),
  65. ((str,), ("disable",)),
  66. ((int,), ("interest",)),
  67. ((bool,), ("implies_detect",)),
  68. ((bool, type(None)), ("autovec",)),
  69. ) :
  70. found_it = option in available
  71. if not found_it:
  72. continue
  73. if not isinstance(val, tp):
  74. error_tp = [t.__name__ for t in (*tp,)]
  75. error_tp = ' or '.join(error_tp)
  76. raise AssertionError(error_msg +
  77. "expected '%s' type for option '%s' not '%s'" % (
  78. error_tp, option, type(val).__name__
  79. ))
  80. break
  81. if not found_it:
  82. raise AssertionError(error_msg + "invalid option name '%s'" % option)
  83. def test_duplicates(self, error_msg, option, val):
  84. if option not in (
  85. "implies", "headers", "flags", "group", "detect", "extra_checks"
  86. ) : return
  87. if isinstance(val, str):
  88. val = val.split()
  89. if len(val) != len(set(val)):
  90. raise AssertionError(error_msg + "duplicated values in option '%s'" % option)
  91. def test_implies(self, error_msg, search_in, feature_name, feature_dict):
  92. if feature_dict.get("disabled") is not None:
  93. return
  94. implies = feature_dict.get("implies", "")
  95. if not implies:
  96. return
  97. if isinstance(implies, str):
  98. implies = implies.split()
  99. if feature_name in implies:
  100. raise AssertionError(error_msg + "feature implies itself")
  101. for impl in implies:
  102. impl_dict = search_in.get(impl)
  103. if impl_dict is not None:
  104. if "disable" in impl_dict:
  105. raise AssertionError(error_msg + "implies disabled feature '%s'" % impl)
  106. continue
  107. raise AssertionError(error_msg + "implies non-exist feature '%s'" % impl)
  108. def test_group(self, error_msg, search_in, feature_name, feature_dict):
  109. if feature_dict.get("disabled") is not None:
  110. return
  111. group = feature_dict.get("group", "")
  112. if not group:
  113. return
  114. if isinstance(group, str):
  115. group = group.split()
  116. for f in group:
  117. impl_dict = search_in.get(f)
  118. if not impl_dict or "disable" in impl_dict:
  119. continue
  120. raise AssertionError(error_msg +
  121. "in option 'group', '%s' already exists as a feature name" % f
  122. )
  123. def test_extra_checks(self, error_msg, search_in, feature_name, feature_dict):
  124. if feature_dict.get("disabled") is not None:
  125. return
  126. extra_checks = feature_dict.get("extra_checks", "")
  127. if not extra_checks:
  128. return
  129. if isinstance(extra_checks, str):
  130. extra_checks = extra_checks.split()
  131. for f in extra_checks:
  132. impl_dict = search_in.get(f)
  133. if not impl_dict or "disable" in impl_dict:
  134. continue
  135. raise AssertionError(error_msg +
  136. "in option 'extra_checks', extra test case '%s' already exists as a feature name" % f
  137. )
  138. class TestConfFeatures(unittest.TestCase):
  139. def __init__(self, methodName="runTest"):
  140. unittest.TestCase.__init__(self, methodName)
  141. self._setup()
  142. def _setup(self):
  143. FakeCCompilerOpt.conf_nocache = True
  144. def test_features(self):
  145. for arch, compilers in arch_compilers.items():
  146. for cc in compilers:
  147. FakeCCompilerOpt.fake_info = (arch, cc, "")
  148. _TestConfFeatures()
  149. if is_standalone:
  150. unittest.main()