build-userland 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #!/usr/bin/env python3
  2. import os
  3. import shlex
  4. import subprocess
  5. import threading
  6. from shell_helpers import LF
  7. import common
  8. import thread_pool
  9. class Main(common.BuildCliFunction):
  10. def __init__(self, *args, **kwargs):
  11. if not 'description' in kwargs:
  12. kwargs['description'] = '''\
  13. Build our compiled userland examples.
  14. '''
  15. if not 'defaults' in kwargs:
  16. kwargs['defaults'] = {}
  17. if not 'mode' in kwargs['defaults']:
  18. kwargs['defaults']['mode'] = 'userland'
  19. super().__init__(*args, **kwargs)
  20. self._add_argument('--ccflags')
  21. self._add_argument('--force-rebuild')
  22. self._add_argument('--optimization-level')
  23. self.add_argument(
  24. 'targets',
  25. default=[],
  26. help='''\
  27. Select to build only the given userland programs, or all programs under
  28. the given directories.
  29. Default: build all.
  30. Must point to either sources or directories under userland/, or to LKMC
  31. toplevel which is a synonym for userland/.
  32. Default: build all examples that have their package dependencies met, e.g.:
  33. - userland/arch/ programs only build if the target arch matches
  34. - an OpenBLAS example can only be built if the target root filesystem
  35. has the OpenBLAS libraries and headers installed, which you must inform
  36. with --package
  37. ''',
  38. nargs='*',
  39. )
  40. def build(self):
  41. build_dir = self.get_build_dir()
  42. cc_flags = [
  43. '-I', self.env['root_dir'], LF,
  44. '-O{}'.format(self.env['optimization_level']), LF,
  45. ] + self.sh.shlex_split(self.env['ccflags'])
  46. if self.env['static']:
  47. cc_flags.extend(['-static', LF])
  48. extra_obj_lkmc_common = os.path.join(
  49. build_dir,
  50. self.env['common_basename_noext'] + self.env['obj_ext']
  51. )
  52. self._build_one(
  53. in_path=self.env['common_c'],
  54. out_path=extra_obj_lkmc_common,
  55. cc_flags=cc_flags,
  56. extra_deps=[self.env['common_h']],
  57. link=False,
  58. )
  59. with thread_pool.ThreadPool(
  60. self._build_one,
  61. nthreads=self.env['nproc'],
  62. submit_raise_exit=self.env['quit_on_fail'],
  63. ) as my_thread_pool:
  64. for target in self.env['targets']:
  65. for path, in_dirnames, in_filenames in self.sh.walk(target):
  66. for in_filename in in_filenames:
  67. in_ext = os.path.splitext(in_filename)[1]
  68. if not in_ext in self.env['build_in_exts']:
  69. continue
  70. in_path = os.path.join(path, in_filename)
  71. my_thread_pool.submit({
  72. 'cc_flags': cc_flags,
  73. 'extra_objs_lkmc_common': [extra_obj_lkmc_common],
  74. 'in_path': in_path,
  75. 'out_path': self.resolve_userland_executable(in_path),
  76. })
  77. exit_status = self._handle_thread_pool_errors(my_thread_pool)
  78. if exit_status != 0:
  79. return exit_status
  80. if self.env['copy_overlay']:
  81. self.sh.copy_dir_if_update(
  82. srcdir=build_dir,
  83. destdir=os.path.join(
  84. self.env['out_rootfs_overlay_lkmc_dir'],
  85. self.env['out_rootfs_overlay_dir_prefix']
  86. ),
  87. filter_ext=self.env['userland_executable_ext'],
  88. )
  89. return exit_status
  90. def clean(self):
  91. if self.env['in_tree']:
  92. for target in self.env['targets']:
  93. if os.path.exists(target):
  94. if os.path.isfile(target):
  95. self.sh.rmrf(self.resolve_userland_executable(target))
  96. else:
  97. for path, dirnames, filenames in self.sh.walk(target):
  98. for filename in filenames:
  99. if os.path.splitext(filename)[1] in self.env['userland_out_exts']:
  100. self.sh.rmrf(os.path.join(path, filename))
  101. else:
  102. for target in self.env['targets']:
  103. self.sh.rmrf(self.resolve_userland_executable(target))
  104. def get_build_dir(self):
  105. return self.env['userland_build_dir']
  106. def setup_one(self):
  107. self.env['targets'] = self.resolve_targets(
  108. [self.env['userland_source_dir']],
  109. self.env['targets']
  110. )
  111. if __name__ == '__main__':
  112. Main().cli()