build-buildroot 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. from shell_helpers import LF
  11. class Main(common.BuildCliFunction):
  12. def __init__(self):
  13. super().__init__(
  14. description='''\
  15. Build Buildroot. This includes, notably: the userland GCC cross-toolchain,
  16. and the root filesystem.
  17. ''')
  18. self.add_argument(
  19. '--build-linux', default=False,
  20. help='''\
  21. See https://cirosantilli.com/linux-kernel-module-cheat#buildroot-vanilla-kernel
  22. '''
  23. )
  24. self.add_argument(
  25. '--baseline', default=False,
  26. help='''Do a default-ish Buildroot defconfig build, without any of our extra options.
  27. Mostly to track how much slower we are than a basic build.
  28. '''
  29. )
  30. self.add_argument(
  31. '--config', default=[], action='append',
  32. help='''Add a single Buildroot config to the current build.
  33. Example value: 'BR2_TARGET_ROOTFS_EXT2_SIZE="512M"'.
  34. Can be used multiple times to add multiple configs.
  35. Takes precedence over any Buildroot config files.
  36. '''
  37. )
  38. self.add_argument(
  39. '--config-fragment', default=[], action='append',
  40. help='''Also use the given Buildroot configuration fragment file.
  41. Pass multiple times to use multiple fragment files.
  42. '''
  43. )
  44. self.add_argument(
  45. '--no-all', default=False,
  46. help='''\
  47. Don't build the all target which normally gets build by default.
  48. That target builds the root filesystem and all its dependencies.
  49. '''
  50. )
  51. self.add_argument(
  52. '--no-overlay', default=False,
  53. help='''\
  54. Don't add our overlay which contains all files we build without going through Buildroot.
  55. This prevents us from overwriting certain Buildroot files. Remember however that you must
  56. still rebuild the Buildroot package that provides those files to actually put the Buildroot
  57. files on the root filesystem.
  58. '''
  59. )
  60. self._add_argument('--force-rebuild')
  61. self._add_argument('extra_make_args')
  62. def build(self):
  63. build_dir = self.get_build_dir()
  64. os.makedirs(self.env['out_dir'], exist_ok=True)
  65. extra_make_args = self.sh.add_newlines(self.env['extra_make_args'])
  66. if self.env['build_linux']:
  67. extra_make_args.extend(['linux-reconfigure', LF])
  68. if self.env['arch'] == 'x86_64':
  69. defconfig = 'qemu_x86_64_defconfig'
  70. elif self.env['arch'] == 'arm':
  71. defconfig = 'qemu_arm_vexpress_defconfig'
  72. elif self.env['arch'] == 'aarch64':
  73. defconfig = 'qemu_aarch64_virt_defconfig'
  74. br2_external_dirs = []
  75. for package_dir in sorted(os.listdir(self.env['packages_dir'])):
  76. package_dir_abs = os.path.join(self.env['packages_dir'], package_dir)
  77. if os.path.isdir(package_dir_abs):
  78. br2_external_dirs.append(self._path_relative_to_buildroot(package_dir_abs))
  79. br2_external_str = ':'.join(br2_external_dirs)
  80. self.sh.run_cmd(
  81. [
  82. 'make', LF,
  83. 'O={}'.format(self.env['buildroot_build_dir']), LF,
  84. 'BR2_EXTERNAL={}'.format(br2_external_str), LF,
  85. defconfig, LF,
  86. ],
  87. cwd=self.env['buildroot_source_dir'],
  88. )
  89. configs = self.env['config']
  90. configs.extend([
  91. 'BR2_JLEVEL={}'.format(self.env['nproc']),
  92. 'BR2_DL_DIR="{}"'.format(self.env['buildroot_download_dir']),
  93. ])
  94. if not self.env['build_linux']:
  95. configs.extend([
  96. '# BR2_LINUX_KERNEL is not set',
  97. ])
  98. config_fragments = []
  99. if not self.env['baseline']:
  100. configs.extend([
  101. 'BR2_GLOBAL_PATCH_DIR="{}"'.format(
  102. self._path_relative_to_buildroot(os.path.join(self.env['root_dir'], 'patches', 'global'))
  103. ),
  104. 'BR2_PACKAGE_BUSYBOX_CONFIG_FRAGMENT_FILES="{}"'.format(
  105. self._path_relative_to_buildroot(os.path.join(self.env['root_dir'], 'busybox_config_fragment'))
  106. ),
  107. 'BR2_PACKAGE_OVERRIDE_FILE="{}"'.format(
  108. self._path_relative_to_buildroot(os.path.join(self.env['root_dir'], 'buildroot_override'))
  109. ),
  110. 'BR2_ROOTFS_POST_BUILD_SCRIPT="{}"'.format(
  111. self._path_relative_to_buildroot(os.path.join(self.env['root_dir'], 'rootfs-post-build-script'))
  112. ),
  113. 'BR2_ROOTFS_USERS_TABLES="{}"'.format(
  114. self._path_relative_to_buildroot(os.path.join(self.env['root_dir'], 'user_table'))
  115. ),
  116. ])
  117. if not self.env['no_overlay']:
  118. configs.append('BR2_ROOTFS_OVERLAY="{}"'.format(
  119. self._path_relative_to_buildroot(self.env['out_rootfs_overlay_dir'])
  120. ))
  121. config_fragments = [
  122. os.path.join(self.env['root_dir'], 'buildroot_config', 'default')
  123. ] + self.env['config_fragment']
  124. if self.env['initrd'] or self.env['initramfs']:
  125. configs.append('BR2_TARGET_ROOTFS_CPIO=y')
  126. # TODO Can't get rid of these for now with nice fragments on Buildroot:
  127. # http://stackoverflow.com/questions/44078245/is-it-possible-to-use-config-fragments-with-buildroots-config
  128. self.sh.write_configs(self.env['buildroot_config_file'], configs, config_fragments)
  129. self.sh.run_cmd(
  130. [
  131. 'make', LF,
  132. 'O={}'.format(self.env['buildroot_build_dir']), LF,
  133. 'olddefconfig', LF,
  134. ],
  135. cwd=self.env['buildroot_source_dir'],
  136. )
  137. self.make_build_dirs()
  138. if self.env['force_rebuild']:
  139. extra_make_args.extend(['-B', LF])
  140. if not self.env['no_all']:
  141. extra_make_args.extend(['all', LF])
  142. self.sh.run_cmd(
  143. [
  144. 'make', LF,
  145. 'LKMC_PARSEC_BENCHMARK_SRCDIR="{}"'.format(self.env['parsec_benchmark_source_dir']), LF,
  146. 'O={}'.format(self.env['buildroot_build_dir']), LF,
  147. 'V={}'.format(int(self.env['verbose'])), LF,
  148. ] +
  149. extra_make_args
  150. ,
  151. cwd=self.env['buildroot_source_dir'],
  152. delete_env=['LD_LIBRARY_PATH', 'PERL_MM_OPT'],
  153. extra_env={
  154. # In Docker, >>> host-tar 1.29 Configuring
  155. # checking whether mknod can create fifo without root privileges... configure: error: in `/root/lkmc/out.docker/buildroot/build/default/aarch64/build/host-tar-1.29':
  156. # configure: error: you should not run configure as root (set FORCE_UNSAFE_CONFIGURE=1 in environment to bypass this check)
  157. 'FORCE_UNSAFE_CONFIGURE': '1',
  158. },
  159. out_file=os.path.join(self.env['buildroot_build_dir'], self.env['repo_short_id'] + '.log'),
  160. )
  161. # Create the qcow2 from ext2.
  162. # Skip if qemu is not present, because gem5 does not need the qcow2.
  163. # so we don't force a QEMU build for gem5.
  164. if not self.env['no_all'] and os.path.exists(self.env['qemu_img_executable']):
  165. self.raw_to_qcow2()
  166. def get_build_dir(self):
  167. return self.env['buildroot_build_dir']
  168. def _path_relative_to_buildroot(self, abspath):
  169. return os.path.relpath(abspath, self.env['buildroot_source_dir'])
  170. if __name__ == '__main__':
  171. Main().cli()