build-userland 4.2 KB

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