build-modules 5.2 KB

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