build-modules 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #!/usr/bin/env python3
  2. import distutils.dir_util
  3. import os
  4. import platform
  5. import shlex
  6. import shutil
  7. import common
  8. from shell_helpers import LF
  9. import path_properties
  10. class Main(common.BuildCliFunction):
  11. def __init__(self):
  12. super().__init__(
  13. description='''\
  14. Build our Linux kernel modules without using Buildroot.
  15. See also: https://cirosantilli.com/linux-kernel-module-cheat#host
  16. ''')
  17. self.add_argument(
  18. '--make-args',
  19. default='',
  20. help='''
  21. Pass custom options to make.
  22. ''',
  23. )
  24. self._add_argument('--force-rebuild')
  25. self.add_argument(
  26. 'kernel-modules',
  27. default=[],
  28. help='''\
  29. Which kernel modules to build. Default: build all.
  30. Can be either the path to the C file, or its basename without extension.''',
  31. nargs='*',
  32. )
  33. def build(self):
  34. build_dir = self.get_build_dir()
  35. os.makedirs(build_dir, exist_ok=True)
  36. # I kid you not, out-of-tree build is not possible, O= does not work as for the kernel build:
  37. #
  38. # * https://stackoverflow.com/questions/5718899/building-an-out-of-tree-linux-kernel-module-in-a-separate-object-directory
  39. # * https://stackoverflow.com/questions/12244979/build-kernel-module-into-a-specific-directory
  40. # * https://stackoverflow.com/questions/18386182/out-of-tree-kernel-modules-multiple-module-single-makefile-same-source-file
  41. #
  42. # This copies only modified files as per:
  43. # https://stackoverflow.com/questions/5718899/building-an-out-of-tree-linux-kernel-module-in-a-separate-object-directory
  44. distutils.dir_util.copy_tree(
  45. self.env['kernel_modules_source_dir'],
  46. os.path.join(build_dir, self.env['kernel_modules_subdir']),
  47. update=1,
  48. )
  49. distutils.dir_util.copy_tree(
  50. self.env['include_source_dir'],
  51. os.path.join(build_dir, self.env['include_subdir']),
  52. update=1,
  53. )
  54. all_kernel_modules = []
  55. for basename in os.listdir(self.env['kernel_modules_source_dir']):
  56. abspath = os.path.join(self.env['kernel_modules_source_dir'], basename)
  57. if os.path.isfile(abspath):
  58. noext, ext = os.path.splitext(basename)
  59. if ext == self.env['c_ext']:
  60. relpath = abspath[len(self.env['root_dir']) + 1:]
  61. my_path_properties = path_properties.get(relpath)
  62. if my_path_properties.should_be_built(self.env):
  63. all_kernel_modules.append(noext)
  64. if self.env['kernel_modules'] == []:
  65. kernel_modules = all_kernel_modules
  66. else:
  67. kernel_modules = map(lambda x: os.path.splitext(os.path.split(x)[1])[0], self.env['kernel_modules'])
  68. object_files = map(lambda x: x + self.env['obj_ext'], kernel_modules)
  69. if self.env['host']:
  70. build_subdir = self.env['kernel_modules_build_host_subdir']
  71. else:
  72. build_subdir = self.env['kernel_modules_build_subdir']
  73. ccache = shutil.which('ccache')
  74. if ccache is not None:
  75. cc = '{} {}'.format(ccache, self.env['gcc_path'])
  76. else:
  77. cc = self.env['gcc_path']
  78. if self.env['host']:
  79. linux_dir = os.path.join('/lib', 'modules', platform.uname().release, 'build')
  80. else:
  81. linux_dir = self.env['linux_build_dir']
  82. ccflags = [
  83. '-I', self.env['root_dir'], LF,
  84. ]
  85. make_args_extra = []
  86. if self.env['verbose']:
  87. make_args_extra.extend(['V=1', LF])
  88. if self.env['force_rebuild']:
  89. make_args_extra.extend(['-B', LF])
  90. self.sh.run_cmd(
  91. (
  92. [
  93. 'make', LF,
  94. '-j', str(self.env['nproc']), LF,
  95. 'ARCH={}'.format(self.env['linux_arch']), LF,
  96. 'CC={}'.format(cc), LF,
  97. 'CCFLAGS={}'.format(self.sh.cmd_to_string(ccflags)), LF,
  98. 'CROSS_COMPILE={}'.format(self.env['toolchain_prefix_dash']), LF,
  99. 'LINUX_DIR={}'.format(linux_dir), LF,
  100. 'M={}'.format(build_subdir), LF,
  101. 'OBJECT_FILES={}'.format(' '.join(object_files)), LF,
  102. ] +
  103. make_args_extra +
  104. self.sh.shlex_split(self.env['make_args'])
  105. ),
  106. cwd=os.path.join(self.env['kernel_modules_build_subdir']),
  107. )
  108. if not self.env['host']:
  109. self.sh.copy_dir_if_update_non_recursive(
  110. srcdir=self.env['kernel_modules_build_subdir'],
  111. destdir=self.env['out_rootfs_overlay_lkmc_dir'],
  112. filter_ext=self.env['kernel_module_ext'],
  113. )
  114. def get_build_dir(self):
  115. if self.env['host']:
  116. return self.env['kernel_modules_build_host_dir']
  117. else:
  118. return self.env['kernel_modules_build_dir']
  119. if __name__ == '__main__':
  120. Main().cli()