compiler_test.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. # python3
  2. # Copyright 2021 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. import re
  6. import unittest
  7. from lib import compiler
  8. class CompilerTestCase(unittest.TestCase):
  9. def assertListSortedEqual(self, a, b, msg=None):
  10. a.sort()
  11. b.sort()
  12. if msg:
  13. self.assertListEqual(a, b, msg=msg)
  14. else:
  15. self.assertListEqual(a, b)
  16. def matching_archs(self, matching: str) -> set[str]:
  17. return {
  18. arch
  19. for arch in compiler._RUSTC_ARCH_TO_BUILD_CONDITION
  20. if re.search(matching, arch)
  21. }
  22. def not_matching_archs(self, matching: str) -> set[str]:
  23. """The inverse of matching_archs()."""
  24. return {
  25. arch
  26. for arch in compiler._RUSTC_ARCH_TO_BUILD_CONDITION
  27. if not re.search(matching, arch)
  28. }
  29. class TestGnConditions(CompilerTestCase):
  30. def test_all_platforms(self):
  31. s = compiler.BuildConditionSet(compiler.ArchSet.ALL())
  32. self.assertListSortedEqual([], s.get_gn_conditions())
  33. def test_one_platform(self):
  34. for a in compiler.ArchSet.ALL().as_strings():
  35. s = compiler.BuildConditionSet(compiler.ArchSet(initial={a}))
  36. mode: compiler.BuildCondition = \
  37. compiler._RUSTC_ARCH_TO_BUILD_CONDITION[a]
  38. self.assertListSortedEqual([mode.gn_condition()],
  39. s.get_gn_conditions())
  40. def test_os(self):
  41. # One OS.
  42. for (matching, mode) in [
  43. (compiler._RUSTC_ARCH_MATCH_ANDROID,
  44. compiler.BuildCondition.ALL_ANDROID),
  45. (compiler._RUSTC_ARCH_MATCH_FUCHSIA,
  46. compiler.BuildCondition.ALL_FUCHSIA),
  47. (compiler._RUSTC_ARCH_MATCH_IOS, compiler.BuildCondition.ALL_IOS),
  48. (compiler._RUSTC_ARCH_MATCH_WINDOWS,
  49. compiler.BuildCondition.ALL_WINDOWS),
  50. (compiler._RUSTC_ARCH_MATCH_LINUX,
  51. compiler.BuildCondition.ALL_LINUX),
  52. (compiler._RUSTC_ARCH_MATCH_MAC, compiler.BuildCondition.ALL_MAC),
  53. ]:
  54. archs = self.matching_archs(matching)
  55. s = compiler.BuildConditionSet(compiler.ArchSet(initial=archs))
  56. cond = mode.gn_condition()
  57. self.assertListSortedEqual([cond],
  58. s.get_gn_conditions(),
  59. msg=repr(archs))
  60. # Two OSs.
  61. archs = self.matching_archs(compiler._RUSTC_ARCH_MATCH_WINDOWS + r"|" +
  62. compiler._RUSTC_ARCH_MATCH_MAC)
  63. s = compiler.BuildConditionSet(compiler.ArchSet(initial=archs))
  64. cond1 = compiler.BuildCondition.ALL_WINDOWS.gn_condition()
  65. cond2 = compiler.BuildCondition.ALL_MAC.gn_condition()
  66. self.assertListSortedEqual([cond1, cond2], s.get_gn_conditions())
  67. # All but one OS.
  68. for (matching_os, mode) in [
  69. (compiler._RUSTC_ARCH_MATCH_ANDROID,
  70. compiler.BuildCondition.NOT_ANDROID),
  71. (compiler._RUSTC_ARCH_MATCH_FUCHSIA,
  72. compiler.BuildCondition.NOT_FUCHSIA),
  73. (compiler._RUSTC_ARCH_MATCH_IOS, compiler.BuildCondition.NOT_IOS),
  74. (compiler._RUSTC_ARCH_MATCH_WINDOWS,
  75. compiler.BuildCondition.NOT_WINDOWS),
  76. (compiler._RUSTC_ARCH_MATCH_LINUX,
  77. compiler.BuildCondition.NOT_LINUX),
  78. (compiler._RUSTC_ARCH_MATCH_MAC, compiler.BuildCondition.NOT_MAC),
  79. ]:
  80. s = compiler.BuildConditionSet(
  81. compiler.ArchSet(initial=self.not_matching_archs(matching_os)))
  82. cond = mode.gn_condition()
  83. self.assertListSortedEqual([cond], s.get_gn_conditions())
  84. def test_one_cpu(self):
  85. for (matching, mode) in [
  86. (compiler._RUSTC_ARCH_MATCH_X86, compiler.BuildCondition.ALL_X86),
  87. (compiler._RUSTC_ARCH_MATCH_X64, compiler.BuildCondition.ALL_X64),
  88. (compiler._RUSTC_ARCH_MATCH_ARM32,
  89. compiler.BuildCondition.ALL_ARM32),
  90. (compiler._RUSTC_ARCH_MATCH_ARM64,
  91. compiler.BuildCondition.ALL_ARM64)
  92. ]:
  93. s = compiler.BuildConditionSet(
  94. compiler.ArchSet(initial=self.matching_archs(matching)))
  95. cond = mode.gn_condition()
  96. self.assertListSortedEqual([cond], s.get_gn_conditions())
  97. def test_combining_os_and_cpu(self):
  98. # One Cpu and one OS (with overlap).
  99. archs = self.matching_archs(compiler._RUSTC_ARCH_MATCH_LINUX + r"|" +
  100. compiler._RUSTC_ARCH_MATCH_X86)
  101. s = compiler.BuildConditionSet(compiler.ArchSet(initial=archs))
  102. cond1 = compiler.BuildCondition.ALL_LINUX.gn_condition()
  103. cond2 = compiler.BuildCondition.ALL_X86.gn_condition()
  104. self.assertListSortedEqual([cond1, cond2], s.get_gn_conditions())
  105. # One Cpu and one OS (without overlap).
  106. archs = self.matching_archs(compiler._RUSTC_ARCH_MATCH_MAC + r"|" +
  107. compiler._RUSTC_ARCH_MATCH_X86)
  108. s = compiler.BuildConditionSet(compiler.ArchSet(initial=archs))
  109. cond1 = compiler.BuildCondition.ALL_MAC.gn_condition()
  110. cond2 = compiler.BuildCondition.ALL_X86.gn_condition()
  111. self.assertListSortedEqual([cond1, cond2], s.get_gn_conditions())
  112. def test_invert(self):
  113. all = compiler.BuildConditionSet.ALL()
  114. none = compiler.BuildConditionSet.EMPTY()
  115. self.assertEqual(none, all.inverted())
  116. self.assertEqual(all, none.inverted())
  117. one = compiler.BuildConditionSet(
  118. compiler.ArchSet(
  119. initial=self.matching_archs(compiler._RUSTC_ARCH_MATCH_MAC)))
  120. the_rest = compiler.BuildConditionSet(
  121. compiler.ArchSet(initial=self.not_matching_archs(
  122. compiler._RUSTC_ARCH_MATCH_MAC)))
  123. self.assertListSortedEqual(one.inverted().get_gn_conditions(),
  124. the_rest.get_gn_conditions())
  125. class TestCompiler(unittest.TestCase):
  126. def test_all_and_one(self):
  127. self.assertEqual(len(compiler.ArchSet.ALL().as_strings()),
  128. len(compiler._RUSTC_ARCH_TO_BUILD_CONDITION))
  129. self.assertEqual(len(compiler.ArchSet.ONE()), 1)
  130. class TestArchSet(CompilerTestCase):
  131. def test_construct(self):
  132. a = compiler.ArchSet(
  133. initial=self.matching_archs(compiler._RUSTC_ARCH_MATCH_ARM32))
  134. self.assertSetEqual({
  135. "armv7-linux-androideabi",
  136. "armv7-apple-ios",
  137. }, a.as_strings())
  138. a = compiler.ArchSet.EMPTY()
  139. self.assertSetEqual(set(), a.as_strings())
  140. a = compiler.ArchSet(
  141. initial=self.matching_archs(compiler._RUSTC_ARCH_MATCH_ARM32))
  142. self.assertSetEqual({
  143. "armv7-linux-androideabi",
  144. "armv7-apple-ios",
  145. }, a.as_strings())
  146. a = compiler.ArchSet(
  147. initial=self.matching_archs(compiler._RUSTC_ARCH_MATCH_ARM32))
  148. self.assertTrue(a.has_arch("armv7-linux-androideabi"))
  149. self.assertFalse(a.has_arch("i686-pc-windows-msvc"))
  150. def test_bool(self):
  151. a = compiler.ArchSet.EMPTY()
  152. self.assertFalse(bool(a))
  153. a = compiler.ArchSet.ONE()
  154. self.assertTrue(bool(a))
  155. a = compiler.ArchSet.ALL()
  156. self.assertTrue(bool(a))
  157. def test_eq(self):
  158. self.assertEqual(compiler.ArchSet.EMPTY(), compiler.ArchSet.EMPTY())
  159. self.assertEqual(compiler.ArchSet.ONE(), compiler.ArchSet.ONE())
  160. self.assertEqual(compiler.ArchSet.ALL(), compiler.ArchSet.ALL())
  161. def test_len(self):
  162. self.assertEqual(len(compiler.ArchSet.EMPTY()), 0)
  163. self.assertEqual(len(compiler.ArchSet.ONE()), 1)
  164. self.assertEqual(len(compiler.ArchSet.ALL()),
  165. len(compiler._RUSTC_ARCH_TO_BUILD_CONDITION))
  166. def test_add_archset(self):
  167. a = compiler.ArchSet.EMPTY()
  168. a.add_archset(compiler.ArchSet.ALL())
  169. self.assertEqual(a, compiler.ArchSet.ALL())
  170. a = compiler.ArchSet.ONE()
  171. a.add_archset(compiler.ArchSet.ALL())
  172. self.assertEqual(a, compiler.ArchSet.ALL())
  173. a = compiler.ArchSet.ALL()
  174. a.add_archset(compiler.ArchSet.ALL())
  175. self.assertEqual(a, compiler.ArchSet.ALL())
  176. def test_and(self):
  177. a = compiler.ArchSet.EMPTY()
  178. a = a & compiler.ArchSet.ALL()
  179. self.assertSetEqual(set(), a.as_strings())
  180. a = compiler.ArchSet.EMPTY()
  181. a &= compiler.ArchSet.ALL()
  182. self.assertSetEqual(set(), a.as_strings())
  183. a = compiler.ArchSet.EMPTY()
  184. a = a & compiler.ArchSet.EMPTY()
  185. self.assertSetEqual(set(), a.as_strings())
  186. a = compiler.ArchSet.EMPTY()
  187. a &= compiler.ArchSet.EMPTY()
  188. self.assertSetEqual(set(), a.as_strings())
  189. a = compiler.ArchSet.ALL()
  190. a = a & compiler.ArchSet.ALL()
  191. self.assertSetEqual(compiler.ArchSet.ALL().as_strings(), a.as_strings())
  192. a = compiler.ArchSet.ALL()
  193. a &= compiler.ArchSet.ALL()
  194. self.assertSetEqual(compiler.ArchSet.ALL().as_strings(), a.as_strings())
  195. a = compiler.ArchSet.ALL()
  196. a = a & compiler.ArchSet.ONE()
  197. self.assertSetEqual(compiler.ArchSet.ONE().as_strings(), a.as_strings())
  198. a = compiler.ArchSet.ALL()
  199. a &= compiler.ArchSet.ONE()
  200. self.assertSetEqual(compiler.ArchSet.ONE().as_strings(), a.as_strings())
  201. a = compiler.ArchSet.ALL()
  202. a = a & compiler.ArchSet.EMPTY()
  203. self.assertSetEqual(set(), a.as_strings())
  204. a = compiler.ArchSet.ALL()
  205. a &= compiler.ArchSet.EMPTY()
  206. self.assertSetEqual(set(), a.as_strings())