build-gem5 4.2 KB

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