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