-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlkhh-module-init
More file actions
executable file
·188 lines (148 loc) · 3.43 KB
/
lkhh-module-init
File metadata and controls
executable file
·188 lines (148 loc) · 3.43 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#! /bin/bash
_rootdir=${_rootdir:-"$(dirname "$(readlink -f "$0")")"}
_ver="0.0.1-dev"
#_author="Wei Peng <me@1pengw.com>"
source "${_rootdir}/_shared_prologue"
#
# parameters
#
declare moduleroot=${moduleroot:-"$(readlink -f $PWD)"}
declare -i main=0
declare -i force=0
#
# getopt processing
#
function my_getopt_cont () {
local n_shift=1 # number of shifts in the outer loop; shift by 1 by default
case "$1" in
-m|--moduleroot)
shift
moduleroot=$1
n_shift=2
;;
-M|--main)
((main++))
n_shift=1
;;
-f|--force)
((force++))
n_shift=1
;;
esac
return $n_shift
}
my_getopt "m:Mf" "moduleroot:,main,force" "my_getopt_cont" "Create a template Makefile" "$(
cat <<END
[-m|--moduleroot=<moduleroot>] [-M|--main]
[-f|--force]
END
)" "$(
cat <<END
-m --moduleroot Root directory of the module, where "Makefile" is found (default: $moduleroot).
-M --main Create a main.c (default: $main).
-f --force Forcibly create a file even if it exists (default: $force).
END
)" "$@"
ret=$?
eval set -- "$_getopt"
shift $ret
# sanity check
vecho 0 <<END
$(
dumpparam <<END1
$(
for name in moduleroot main force; do
echo "$name=$(echo $(eval "echo \$$name"))"
done
)
END1
)
END
if [[ ! -d "$moduleroot" ]]; then
mkdir -p "$moduleroot"
fi
moduleroot="$(readlink -f "$moduleroot")"
[[ -d "$moduleroot" ]] || die 1 <<END
ERROR: $moduleroot does not exist.
END
module=$(basename "$moduleroot" | tr -d '[:cntrl:]' | tr -s '[:space:]-' '[_*]')
#
# main.c
#
mainfile="$moduleroot/main.c"
if (( main > 0 )); then
if [[ ! -f "$mainfile" ]] || (( force > 0 )); then
>"$mainfile" cat - <<END
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/errno.h>
#include <linux/slab.h>
#include <linux/fs.h>
int ${module}_param = 0;
module_param(${module}_param, int, 0644);
static int ${module}_init(void)
{
pr_debug("Hello, world.\n");
return 0;
}
static void ${module}_exit(void)
{
pr_debug("Goodbye, world.\n");
}
MODULE_AUTHOR("$(whoami)");
MODULE_LICENSE("Dual BSD/GPL");
module_init(${module}_init);
module_exit(${module}_exit);
END
fi
fi
#
# Makefile
#
makefile="$moduleroot/Makefile"
>/dev/null pushd $moduleroot
objfiles=$(2>/dev/null find * -name '*.c' -type f | sed 's/^\(.*\).c$/\1.o/' | tr "\n" " ")
>/dev/null popd
[[ -f "$makefile" ]] && (( force <= 0 )) && die 0 <<END
$makefile exists; use -f/--force to forcibly create a new one.
END
>"$makefile" cat - <<END
_name=Makefile.pre
ifeq (\$(_name),\$(wildcard \$(_name)))
include \$(_name)
endif
ifeq (\$(DEBUG),y)
DEBFLAGS = -O -g -DDEBUG # "-O" is needed to expand inlines
else
DEBFLAGS = -O2
endif
LOCALINC=\$(PWD)
EXTRA_CFLAGS += \$(DEBFLAGS)
EXTRA_CFLAGS += -I\$(LOCALINC)
EXTRA_CFLAGS += -std=gnu17
ifneq (\$(KERNELRELEASE),)
# call from kernel build system
${module}-objs := ${objfiles}
obj-m := ${module}.o
else
KERNELDIR ?= /lib/modules/\$(shell uname -r)/build
PWD := \$(shell pwd)
modules:
\$(MAKE) -C \$(KERNELDIR) M=\$(PWD) modules
endif
clean:
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions .*.o.d
depend .depend dep:
\$(CC) \$(EXTRA_CFLAGS) -M *.c > .depend
ifeq (.depend,\$(wildcard .depend))
include .depend
endif
_name=Makefile.post
ifeq (\$(_name),\$(wildcard \$(_name)))
include \$(_name)
endif
END