build-gem5 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. self.add_argument(
  11. 'extra_scons_args',
  12. metavar='extra-scons-args',
  13. nargs='*',
  14. )
  15. def build(self):
  16. build_dir = self.get_build_dir()
  17. binaries_dir = os.path.join(self.env['gem5_system_dir'], 'binaries')
  18. disks_dir = os.path.join(self.env['gem5_system_dir'], 'disks')
  19. os.makedirs(binaries_dir, exist_ok=True)
  20. os.makedirs(disks_dir, exist_ok=True)
  21. if self.env['gem5_source_dir'] is None:
  22. if not os.path.exists(os.path.join(self.env['gem5_source_dir'], '.git')):
  23. if self.env['gem5_source_dir'] == self.env['gem5_default_source_dir']:
  24. raise Exception('gem5 submodule not checked out')
  25. self.sh.run_cmd([
  26. 'git', LF,
  27. '-C', self.env['gem5_default_source_dir'], LF,
  28. 'worktree', 'add', LF,
  29. '-b', os.path.join('wt', self.env['gem5_build_id']), LF,
  30. self.env['gem5_source_dir'], LF,
  31. ])
  32. if self.env['verbose']:
  33. verbose = ['--verbose', LF]
  34. else:
  35. verbose = []
  36. if self.env['arch'] == 'x86_64':
  37. dummy_img_path = os.path.join(disks_dir, 'linux-bigswap2.img')
  38. with open(dummy_img_path, 'wb') as dummy_img_file:
  39. zeroes = b'\x00' * (2 ** 16)
  40. for i in range(2 ** 10):
  41. dummy_img_file.write(zeroes)
  42. self.sh.run_cmd(['mkswap', dummy_img_path, LF])
  43. with open(os.path.join(binaries_dir, 'x86_64-vmlinux-2.6.22.9'), 'w'):
  44. # This file must always be present, despite --kernel overriding that default and selecting the kernel.
  45. # I'm not even joking. No one has ever built x86 gem5 without the magic dist dir present.
  46. pass
  47. elif self.env['arch'] == 'arm' or self.env['arch'] == 'aarch64':
  48. gem5_system_source_dir = os.path.join(self.env['gem5_source_dir'], 'system')
  49. # dtb
  50. dt_source_dir = os.path.join(gem5_system_source_dir, 'arm', 'dt')
  51. dt_build_dir = os.path.join(self.env['gem5_system_dir'], 'arm', 'dt')
  52. self.sh.run_cmd(['make', '-C', dt_source_dir, LF])
  53. self.sh.copy_dir_if_update_non_recursive(
  54. srcdir=dt_source_dir,
  55. destdir=dt_build_dir,
  56. filter_ext='.dtb',
  57. )
  58. # Bootloader 32.
  59. bootloader32_dir = os.path.join(gem5_system_source_dir, 'arm', 'simple_bootloader')
  60. # TODO use the buildroot cross compiler here, and remove the dependencies from configure.
  61. self.sh.run_cmd([
  62. 'make', LF,
  63. '-C', bootloader32_dir, LF,
  64. 'CROSS_COMPILE=arm-linux-gnueabihf-', LF,
  65. ])
  66. # bootloader
  67. self.sh.cp(os.path.join(bootloader32_dir, 'boot_emm.arm'), binaries_dir)
  68. # Bootloader 64.
  69. bootloader64_dir = os.path.join(gem5_system_source_dir, 'arm', 'aarch64_bootloader')
  70. # TODO cross_compile is ignored because the make does not use CC...
  71. self.sh.run_cmd(['make', '-C', bootloader64_dir, LF])
  72. self.sh.cp(os.path.join(bootloader64_dir, 'boot_emm.arm64'), binaries_dir)
  73. self.sh.run_cmd(
  74. (
  75. [
  76. 'scons', LF,
  77. '-j', str(self.env['nproc']), LF,
  78. '--gold-linker', LF,
  79. '--ignore-style', LF,
  80. self.env['gem5_executable'], LF,
  81. ] +
  82. verbose +
  83. self.sh.add_newlines(self.env['extra_scons_args'])
  84. ),
  85. cwd=self.env['gem5_source_dir'],
  86. extra_paths=[self.env['ccache_dir']],
  87. )
  88. term_source_dir = os.path.join(self.env['gem5_source_dir'], 'util/term')
  89. m5term_build = os.path.join(term_source_dir, 'm5term')
  90. self.sh.run_cmd(['make', '-C', term_source_dir, LF])
  91. if os.path.exists(self.env['gem5_m5term']):
  92. # Otherwise self.sh.cp would fail with "Text file busy" if you
  93. # tried to rebuild while running m5term:
  94. # https://stackoverflow.com/questions/16764946/what-generates-the-text-file-busy-message-in-unix/52427512#52427512
  95. self.sh.rmrf(self.env['gem5_m5term'])
  96. self.sh.cp(m5term_build, self.env['gem5_m5term'])
  97. def get_build_dir(self):
  98. return self.env['gem5_build_dir']
  99. if __name__ == '__main__':
  100. Main().cli()