build-buildroot 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #!/usr/bin/env python3
  2. import os
  3. import pathlib
  4. import shutil
  5. import subprocess
  6. import sys
  7. import time
  8. import re
  9. import common
  10. class BuildrootComponent(self.Component):
  11. def add_parser_arguments(self, parser):
  12. parser.add_argument(
  13. '--build-linux', default=self._defaults['build_linux'],
  14. help='''\
  15. Enable building the Linux kernel with Buildroot. This is done mostly
  16. to extract Buildroot's default kernel configurations when updating Buildroot.
  17. This kernel will not be use by our other scripts. Configuring this kernel is
  18. not currently supported, juse use ./build-linux script if you want to do that.
  19. '''
  20. )
  21. parser.add_argument(
  22. '--baseline', default=self._defaults['baseline'],
  23. help='''Do a default-ish Buildroot defconfig build, without any of our extra options.
  24. Mostly to track how much slower we are than a basic build.
  25. '''
  26. )
  27. parser.add_argument(
  28. '--config', default=self._defaults['config'], action='append',
  29. help='''Add a single Buildroot config to the current build.
  30. Example value: 'BR2_TARGET_ROOTFS_EXT2_SIZE="512M"'.
  31. Can be used multiple times to add multiple configs.
  32. Takes precedence over any Buildroot config files.
  33. '''
  34. )
  35. parser.add_argument(
  36. '--config-fragment', default=self._defaults['config_fragment'], action='append',
  37. help='''Also use the given Buildroot configuration fragment file.
  38. Pass multiple times to use multiple fragment files.
  39. '''
  40. )
  41. parser.add_argument(
  42. '--no-all', default=self._defaults['no_all'],
  43. help='''\
  44. Don't build the all target which normally gets build by default.
  45. That target builds the root filesystem and all its dependencies.
  46. '''
  47. )
  48. parser.add_argument(
  49. '--no-overlay', default=self._defaults['no_all'],
  50. help='''\
  51. Don't add our overlay which contains all files we build without going through Buildroot.
  52. This prevents us from overwriting certain Buildroot files. Remember however that you must
  53. still rebuild the Buildroot package that provides those files to actually put the Buildroot
  54. files on the root filesystem.
  55. '''
  56. )
  57. parser.add_argument(
  58. 'extra_make_args', default=self._defaults['extra_make_args'], metavar='extra-make-args', nargs='*',
  59. help='''\
  60. Extra arguments to be passed to the Buildroot make,
  61. usually extra Buildroot targets.
  62. '''
  63. )
  64. def do_build(self, args):
  65. build_dir = self.get_build_dir(args)
  66. os.makedirs(kwargs['out_dir'], exist_ok=True)
  67. extra_make_args = self.sh.add_newlines(kwargs['extra_make_args'])
  68. if kwargs['build_linux']:
  69. extra_make_args.extend(['linux-reconfigure', LF])
  70. if kwargs['emulator'] == 'gem5':
  71. extra_make_args.extend(['gem5-reconfigure', LF])
  72. if kwargs['arch'] == 'x86_64':
  73. defconfig = 'qemu_x86_64_defconfig'
  74. elif kwargs['arch'] == 'arm':
  75. defconfig = 'qemu_arm_vexpress_defconfig'
  76. elif kwargs['arch'] == 'aarch64':
  77. defconfig = 'qemu_aarch64_virt_defconfig'
  78. br2_external_dirs = []
  79. for package_dir in os.listdir(kwargs['packages_dir']):
  80. package_dir_abs = os.path.join(kwargs['packages_dir'], package_dir)
  81. if os.path.isdir(package_dir_abs):
  82. br2_external_dirs.append(self._path_relative_to_buildroot(package_dir_abs))
  83. br2_external_str = ':'.join(br2_external_dirs)
  84. self.sh.run_cmd(
  85. [
  86. 'make', LF,
  87. 'O={}'.format(kwargs['buildroot_build_dir']), LF,
  88. 'BR2_EXTERNAL={}'.format(br2_external_str), LF,
  89. defconfig, LF,
  90. ],
  91. cwd=kwargs['buildroot_src_dir'],
  92. )
  93. configs = kwargs['config']
  94. configs.extend([
  95. 'BR2_JLEVEL={}'.format(kwargs['nproc']),
  96. 'BR2_DL_DIR="{}"'.format(kwargs['buildroot_download_dir']),
  97. ])
  98. if not kwargs['build_linux']:
  99. configs.extend([
  100. '# BR2_LINUX_KERNEL is not set',
  101. ])
  102. config_fragments = []
  103. if not kwargs['baseline']:
  104. configs.extend([
  105. 'BR2_GLOBAL_PATCH_DIR="{}"'.format(
  106. self._path_relative_to_buildroot(os.path.join(kwargs['root_dir'], 'patches', 'global'))
  107. ),
  108. 'BR2_PACKAGE_BUSYBOX_CONFIG_FRAGMENT_FILES="{}"'.format(
  109. self._path_relative_to_buildroot(os.path.join(kwargs['root_dir'], 'busybox_config_fragment'))
  110. ),
  111. 'BR2_PACKAGE_OVERRIDE_FILE="{}"'.format(
  112. self._path_relative_to_buildroot(os.path.join(kwargs['root_dir'], 'buildroot_override'))
  113. ),
  114. 'BR2_ROOTFS_POST_BUILD_SCRIPT="{}"'.format(
  115. self._path_relative_to_buildroot(os.path.join(kwargs['root_dir'], 'rootfs-post-build-script'))
  116. ),
  117. 'BR2_ROOTFS_USERS_TABLES="{}"'.format(
  118. self._path_relative_to_buildroot(os.path.join(kwargs['root_dir'], 'user_table'))
  119. ),
  120. ])
  121. if not kwargs['no_overlay']:
  122. configs.append('BR2_ROOTFS_OVERLAY="{}"'.format(
  123. self._path_relative_to_buildroot(kwargs['out_rootfs_overlay_dir'])
  124. ))
  125. config_fragments = [
  126. os.path.join(kwargs['root_dir'], 'buildroot_config', 'default')
  127. ] + kwargs['config_fragment']
  128. # TODO Can't get rid of these for now with nice fragments on Buildroot:
  129. # http://stackoverflow.com/questions/44078245/is-it-possible-to-use-config-fragments-with-buildroots-config
  130. self.sh.write_configs(kwargs['buildroot_config_file'], configs, config_fragments)
  131. self.sh.run_cmd(
  132. [
  133. 'make', LF,
  134. 'O={}'.format(kwargs['buildroot_build_dir']), LF,
  135. 'olddefconfig', LF,
  136. ],
  137. cwd=kwargs['buildroot_src_dir'],
  138. )
  139. self.make_build_dirs()
  140. if not kwargs['no_all']:
  141. extra_make_args.extend(['all', LF])
  142. self.sh.run_cmd(
  143. [
  144. 'make', LF,
  145. 'LKMC_GEM5_SRCDIR="{}"'.format(kwargs['gem5_source_dir']), LF,
  146. 'LKMC_PARSEC_BENCHMARK_SRCDIR="{}"'.format(kwargs['parsec_benchmark_src_dir']), LF,
  147. 'O={}'.format(kwargs['buildroot_build_dir']), LF,
  148. 'V={}'.format(int(kwargs['verbose'])), LF,
  149. ] +
  150. extra_make_args
  151. ,
  152. out_file=os.path.join(kwargs['buildroot_build_dir'], 'lkmc.log'),
  153. delete_env=['LD_LIBRARY_PATH'],
  154. cwd=kwargs['buildroot_src_dir'],
  155. )
  156. # Create the qcow2 from ext2.
  157. # Skip if qemu is not present, because gem5 does not need the qcow2.
  158. # so we don't force a QEMU build for gem5.
  159. if not kwargs['no_all'] and os.path.exists(kwargs['qemu_img_executable']):
  160. self.raw_to_qcow2()
  161. def get_argparse_args(self):
  162. return {
  163. 'description': '''\
  164. Run Linux on an emulator
  165. '''
  166. }
  167. def get_build_dir(self, args):
  168. return kwargs['buildroot_build_dir']
  169. _defaults = {
  170. 'baseline': False,
  171. 'build_linux': False,
  172. 'config': [],
  173. 'config_fragment': [],
  174. 'extra_make_args': [],
  175. 'no_all': False,
  176. 'skip_configure': False,
  177. }
  178. def _path_relative_to_buildroot(self, abspath):
  179. return os.path.relpath(abspath, kwargs['buildroot_src_dir'])
  180. if __name__ == '__main__':
  181. BuildrootComponent().build()