build-gem5 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #!/usr/bin/env python3
  2. import os
  3. import pathlib
  4. import subprocess
  5. import common
  6. from shell_helpers import LF
  7. class Main(common.BuildCliFunction):
  8. def __init__(self):
  9. super().__init__(
  10. description='''\
  11. Build gem5.
  12. https://github.com/cirosantilli/linux-kernel-module-cheat-regression#gem5-buildroot-setup
  13. '''
  14. )
  15. self.add_argument(
  16. '--regression-test',
  17. action='append',
  18. default=[],
  19. help='''\
  20. Build and run the given gem5 regression test.
  21. https://github.com/cirosantilli/linux-kernel-module-cheat-regression#gem5-unit-tests
  22. '''
  23. )
  24. self.add_argument(
  25. '--unit-test',
  26. action='append',
  27. default=[],
  28. help='''\
  29. Build and run the given unit test. Paths are relative to src/ without the .opt suffix.
  30. If given multiple times, runs multiple unit tests. Ignore --unit-tests.
  31. https://github.com/cirosantilli/linux-kernel-module-cheat-regression#gem5-unit-tests
  32. '''
  33. )
  34. self.add_argument(
  35. '--unit-tests',
  36. default=False,
  37. help='''\
  38. Build and run all the gem5 unit tests instead of the gem5 executable.
  39. https://github.com/cirosantilli/linux-kernel-module-cheat-regression#gem5-unit-tests
  40. '''
  41. )
  42. self.add_argument(
  43. 'extra_scons_args',
  44. metavar='extra-scons-args',
  45. nargs='*',
  46. )
  47. def build(self):
  48. build_dir = self.get_build_dir()
  49. binaries_dir = os.path.join(self.env['gem5_system_dir'], 'binaries')
  50. disks_dir = os.path.join(self.env['gem5_system_dir'], 'disks')
  51. os.makedirs(binaries_dir, exist_ok=True)
  52. os.makedirs(disks_dir, exist_ok=True)
  53. if not os.path.exists(os.path.join(self.env['gem5_source_dir'], '.git')):
  54. if self.env['_args_given']['gem5_worktree']:
  55. self.sh.run_cmd([
  56. 'git', LF,
  57. '-C', self.env['gem5_default_source_dir'], LF,
  58. 'worktree', 'add', LF,
  59. '-b', os.path.join('wt', self.env['gem5_build_id']), LF,
  60. self.env['gem5_source_dir'], LF,
  61. ])
  62. else:
  63. if not self.env['dry_run']:
  64. raise Exception('gem5 submodule not checked out')
  65. if self.env['verbose']:
  66. verbose = ['--verbose', LF]
  67. else:
  68. verbose = []
  69. if self.env['arch'] == 'x86_64':
  70. dummy_img_path = os.path.join(disks_dir, 'linux-bigswap2.img')
  71. with open(dummy_img_path, 'wb') as dummy_img_file:
  72. zeroes = b'\x00' * (2 ** 16)
  73. for i in range(2 ** 10):
  74. dummy_img_file.write(zeroes)
  75. self.sh.run_cmd(['mkswap', dummy_img_path, LF])
  76. with open(os.path.join(binaries_dir, 'x86_64-vmlinux-2.6.22.9'), 'w'):
  77. # This file must always be present, despite --kernel overriding that default and selecting the kernel.
  78. # I'm not even joking. No one has ever built x86 gem5 without the magic dist dir present.
  79. pass
  80. elif self.env['arch'] == 'arm' or self.env['arch'] == 'aarch64':
  81. gem5_system_source_dir = os.path.join(self.env['gem5_source_dir'], 'system')
  82. # dtb
  83. dt_source_dir = os.path.join(gem5_system_source_dir, 'arm', 'dt')
  84. dt_build_dir = os.path.join(self.env['gem5_system_dir'], 'arm', 'dt')
  85. self.sh.run_cmd(['make', '-C', dt_source_dir, LF])
  86. self.sh.copy_dir_if_update_non_recursive(
  87. srcdir=dt_source_dir,
  88. destdir=dt_build_dir,
  89. filter_ext='.dtb',
  90. )
  91. # Bootloader 32.
  92. bootloader32_dir = os.path.join(gem5_system_source_dir, 'arm', 'simple_bootloader')
  93. # TODO use the buildroot cross compiler here, and remove the dependencies from configure.
  94. self.sh.run_cmd([
  95. 'make', LF,
  96. '-C', bootloader32_dir, LF,
  97. 'CROSS_COMPILE=arm-linux-gnueabihf-', LF,
  98. ])
  99. # bootloader
  100. self.sh.cp(os.path.join(bootloader32_dir, 'boot_emm.arm'), binaries_dir)
  101. # Bootloader 64.
  102. bootloader64_dir = os.path.join(gem5_system_source_dir, 'arm', 'aarch64_bootloader')
  103. # TODO cross_compile is ignored because the make does not use CC...
  104. self.sh.run_cmd(['make', '-C', bootloader64_dir, LF])
  105. self.sh.cp(os.path.join(bootloader64_dir, 'boot_emm.arm64'), binaries_dir)
  106. term_source_dir = os.path.join(self.env['gem5_source_dir'], 'util/term')
  107. m5term_build = os.path.join(term_source_dir, 'm5term')
  108. self.sh.run_cmd(['make', '-C', term_source_dir, LF])
  109. if os.path.exists(self.env['gem5_m5term']):
  110. # Otherwise self.sh.cp would fail with "Text file busy" if you
  111. # tried to rebuild while running m5term:
  112. # https://stackoverflow.com/questions/16764946/what-generates-the-text-file-busy-message-in-unix/52427512#52427512
  113. self.sh.rmrf(self.env['gem5_m5term'])
  114. self.sh.cp(m5term_build, self.env['gem5_m5term'])
  115. if self.env['unit_test']:
  116. targets = [self.get_gem5_target_path(self.env, test) for test in self.env['unit_test']]
  117. elif self.env['regression_test']:
  118. targets = [
  119. os.path.join(
  120. self.env['gem5_executable_dir'],
  121. 'tests',
  122. self.env['gem5_build_type'],
  123. test
  124. )
  125. for test
  126. in self.env['regression_test']
  127. ]
  128. elif self.env['unit_tests']:
  129. targets = [self.env['gem5_unit_test_target']]
  130. else:
  131. targets = [self.env['gem5_executable']]
  132. if self.env['clang']:
  133. extra_env = {
  134. 'CC': 'clang',
  135. 'CXX': 'clang++',
  136. }
  137. gold_linker_cmd = []
  138. else:
  139. extra_env = {}
  140. gold_linker_cmd = ['--gold-linker', LF,]
  141. exit_status = self.sh.run_cmd(
  142. (
  143. [
  144. 'scons', LF,
  145. '-j', str(self.env['nproc']), LF,
  146. '--ignore-style', LF,
  147. ] +
  148. gold_linker_cmd +
  149. verbose +
  150. [
  151. 'SLICC_HTML=True', LF,
  152. ] +
  153. self.sh.add_newlines(targets) +
  154. self.sh.add_newlines(self.env['extra_scons_args'])
  155. ),
  156. cwd=self.env['gem5_source_dir'],
  157. extra_paths=[self.env['ccache_dir']],
  158. extra_env=extra_env,
  159. raise_on_failure = False,
  160. )
  161. return exit_status
  162. def get_build_dir(self):
  163. return self.env['gem5_build_dir']
  164. if __name__ == '__main__':
  165. Main().cli()