build-modules 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #!/usr/bin/env python3
  2. import distutils.dir_util
  3. import os
  4. import platform
  5. import shutil
  6. import common
  7. from shell_helpers import LF
  8. class Main(common.BuildCliFunction):
  9. def __init__(self):
  10. super().__init__(
  11. description='''\
  12. Build our Linux kernel modules without using Buildroot.
  13. See also: https://github.com/cirosantilli/linux-kernel-module-cheat#host
  14. ''')
  15. self.add_argument(
  16. '--make-args',
  17. default='',
  18. )
  19. self.add_argument(
  20. '--host',
  21. default=False,
  22. help='''\
  23. Build the Linux kernel modules for the host instead of guest.
  24. Use the host packaged cross toolchain.
  25. ''',
  26. )
  27. self.add_argument(
  28. 'kernel-modules',
  29. default=[],
  30. help='Which kernel modules to build. Default: build all',
  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. src = os.path.join(self.env['kernel_modules_source_dir'], basename)
  57. if os.path.isfile(src):
  58. noext, ext = os.path.splitext(basename)
  59. if ext == self.env['c_ext']:
  60. all_kernel_modules.append(noext)
  61. if self.env['kernel_modules'] == []:
  62. kernel_modules = all_kernel_modules
  63. else:
  64. kernel_modules = map(lambda x: os.path.splitext(os.path.split(x)[1])[0], self.env['kernel_modules'])
  65. object_files = map(lambda x: x + self.env['obj_ext'], kernel_modules)
  66. tool = 'gcc'
  67. if self.env['host']:
  68. allowed_toolchains = ['host']
  69. build_subdir = self.env['kernel_modules_build_host_subdir']
  70. else:
  71. allowed_toolchains = None
  72. build_subdir = self.env['kernel_modules_build_subdir']
  73. gcc = self.get_toolchain_tool(tool, allowed_toolchains=allowed_toolchains)
  74. prefix = gcc[:-len(tool)]
  75. ccache = shutil.which('ccache')
  76. if ccache is not None:
  77. cc = '{} {}'.format(ccache, gcc)
  78. else:
  79. cc = gcc
  80. if self.env['verbose']:
  81. verbose = ['V=1']
  82. else:
  83. verbose = []
  84. if self.env['host']:
  85. linux_dir = os.path.join('/lib', 'modules', platform.uname().release, 'build')
  86. else:
  87. linux_dir = self.env['linux_build_dir']
  88. self.sh.run_cmd(
  89. (
  90. [
  91. 'make', LF,
  92. '-j', str(self.env['nproc']), LF,
  93. 'ARCH={}'.format(self.env['linux_arch']), LF,
  94. 'CC={}'.format(cc), LF,
  95. 'CROSS_COMPILE={}'.format(prefix), LF,
  96. 'LINUX_DIR={}'.format(linux_dir), LF,
  97. 'M={}'.format(build_subdir), LF,
  98. 'OBJECT_FILES={}'.format(' '.join(object_files)), LF,
  99. ] +
  100. self.sh.shlex_split(self.env['make_args']) +
  101. verbose
  102. ),
  103. cwd=os.path.join(self.env['kernel_modules_build_subdir']),
  104. )
  105. if not self.env['host']:
  106. self.sh.copy_dir_if_update_non_recursive(
  107. srcdir=self.env['kernel_modules_build_subdir'],
  108. destdir=self.env['out_rootfs_overlay_dir'],
  109. filter_ext=self.env['kernel_module_ext'],
  110. )
  111. def get_build_dir(self):
  112. if self.env['host']:
  113. return self.env['kernel_modules_build_host_dir']
  114. else:
  115. return self.env['kernel_modules_build_dir']
  116. if __name__ == '__main__':
  117. Main().cli()