|
1 |
| -# Makefile : auto-generated by script xcc_lkm.sh |
| 1 | +# ch5/lkm_template/Makefile |
| 2 | +# *************************************************************** |
| 3 | +# This program is part of the source code released for the book |
| 4 | +# "Linux Kernel Programming" |
| 5 | +# (c) Author: Kaiwan N Billimoria |
| 6 | +# Publisher: Packt |
| 7 | +# GitHub repository: |
| 8 | +# https://github.com/PacktPublishing/Linux-Kernel-Programming |
| 9 | +# |
| 10 | +# From: Ch 5 : Writing Your First Kernel Module LKMs, Part 2 |
| 11 | +# *************************************************************** |
| 12 | +# Brief Description: |
| 13 | +# A 'better' Makefile template for Linux LKMs (Loadable Kernel Modules); besides |
| 14 | +# the 'usual' targets (the build, install and clean), we incorporate targets to |
| 15 | +# do useful (and indeed required) stuff like: |
| 16 | +# - adhering to kernel coding style (indent+checkpatch) |
| 17 | +# - several static analysis targets (via sparse, gcc, flawfinder, cppcheck) |
| 18 | +# - two 'dummy' dynamic analysis targets (KASAN, LOCKDEP) |
| 19 | +# - a packaging (.tar.xz) target and |
| 20 | +# - a help target. |
| 21 | +# |
| 22 | +# To get started, just type: |
| 23 | +# make help |
| 24 | +# |
| 25 | +# For details, please refer the book, Ch 5. |
2 | 26 |
|
3 | 27 | # To support cross-compiling for kernel modules:
|
4 | 28 | # For architecture (cpu) 'arch', invoke make as:
|
5 |
| -# make ARCH=<arch> CROSS_COMPILE=<cross-compiler-prefix> |
| 29 | +# make ARCH=<arch> CROSS_COMPILE=<cross-compiler-prefix> |
6 | 30 | ifeq ($(ARCH),arm)
|
7 |
| - # *UPDATE* 'KDIR' below to point to the ARM Linux kernel source tree on your box |
8 |
| - KDIR ?= ~/rpi_work/kernel_rpi |
| 31 | + # *UPDATE* 'KDIR' below to point to the ARM Linux kernel source tree on your box |
| 32 | + KDIR ?= ~/rpi_work/kernel_rpi/linux |
| 33 | +else ifeq ($(ARCH),arm64) |
| 34 | + # *UPDATE* 'KDIR' below to point to the ARM64 (Aarch64) Linux kernel source |
| 35 | + # tree on your box |
| 36 | + KDIR ?= ~/kernel/linux-4.14 |
9 | 37 | else ifeq ($(ARCH),powerpc)
|
10 |
| - # *UPDATE* 'KDIR' below to point to the PPC64 Linux kernel source tree on your box |
11 |
| - KDIR ?= ~/kernel/linux-4.9.1 |
| 38 | + # *UPDATE* 'KDIR' below to point to the PPC64 Linux kernel source tree on your box |
| 39 | + KDIR ?= ~/kernel/linux-4.9.1 |
12 | 40 | else
|
13 |
| - KDIR ?= /lib/modules/$(shell uname -r)/build |
| 41 | + # 'KDIR' is the Linux 'kernel headers' package on your host system; this is |
| 42 | + # usually an x86_64, but could be anything, really (f.e. building directly |
| 43 | + # on a Raspberry Pi implies that it's the host) |
| 44 | + KDIR ?= /lib/modules/$(shell uname -r)/build |
14 | 45 | endif
|
15 | 46 |
|
16 |
| -obj-m += deadlock_eg_AB-BA.o |
| 47 | +# Set FNAME_C to the kernel module name source filename (without .c) |
| 48 | +FNAME_C := deadlock_eg_AB-BA |
| 49 | + |
| 50 | +PWD := $(shell pwd) |
| 51 | +obj-m += ${FNAME_C}.o |
17 | 52 | EXTRA_CFLAGS += -DDEBUG
|
18 |
| -$(info Building for: ARCH=${ARCH} CROSS_COMPILE=${CROSS_COMPILE} EXTRA_CFLAGS=${EXTRA_CFLAGS}) |
19 | 53 |
|
20 | 54 | all:
|
| 55 | + @echo |
| 56 | + @echo '--- Building : KDIR=${KDIR} ARCH=${ARCH} CROSS_COMPILE=${CROSS_COMPILE} EXTRA_CFLAGS=${EXTRA_CFLAGS} ---' |
| 57 | + @echo |
21 | 58 | make -C $(KDIR) M=$(PWD) modules
|
22 | 59 | install:
|
23 |
| - make -C $(KDIR) M=$(PWD) modules_install |
| 60 | + @echo |
| 61 | + @echo "--- installing ---" |
| 62 | + @echo " [First, invoke the 'make' ]" |
| 63 | + make |
| 64 | + @echo |
| 65 | + @echo " [Now for the 'sudo make install' ]" |
| 66 | + sudo make -C $(KDIR) M=$(PWD) modules_install |
| 67 | + sudo depmod |
24 | 68 | clean:
|
| 69 | + @echo |
| 70 | + @echo "--- cleaning ---" |
| 71 | + @echo |
25 | 72 | make -C $(KDIR) M=$(PWD) clean
|
| 73 | + rm -f *~ # from 'indent' |
| 74 | + |
| 75 | +#--------------- More (useful) targets! ------------------------------- |
| 76 | +INDENT := indent |
| 77 | + |
| 78 | +# code-style : "wrapper" target over the following kernel code style targets |
| 79 | +code-style: |
| 80 | + make indent |
| 81 | + make checkpatch |
| 82 | + |
| 83 | +# indent- "beautifies" C code - to conform to the the Linux kernel |
| 84 | +# coding style guidelines. |
| 85 | +# Note! original source file(s) is overwritten, so we back it up. |
| 86 | +indent: |
| 87 | + @echo |
| 88 | + @echo "--- applying kernel code style indentation with indent ---" |
| 89 | + @echo |
| 90 | + mkdir bkp 2> /dev/null; cp -f *.[chsS] bkp/ |
| 91 | + ${INDENT} -linux --line-length95 *.[chsS] |
| 92 | + # add source files as required |
| 93 | + |
| 94 | +# Detailed check on the source code styling / etc |
| 95 | +checkpatch: |
| 96 | + make clean |
| 97 | + @echo |
| 98 | + @echo "--- kernel code style check with checkpatch.pl ---" |
| 99 | + @echo |
| 100 | + $(KDIR)/scripts/checkpatch.pl --no-tree -f --max-line-length=95 *.[ch] |
| 101 | + # add source files as required |
| 102 | + |
| 103 | +#--- Static Analysis |
| 104 | +# sa : "wrapper" target over the following kernel static analyzer targets |
| 105 | +sa: |
| 106 | + make sa_sparse |
| 107 | + make sa_gcc |
| 108 | + make sa_flawfinder |
| 109 | + make sa_cppcheck |
| 110 | + |
| 111 | +# static analysis with sparse |
| 112 | +sa_sparse: |
| 113 | + make clean |
| 114 | + @echo |
| 115 | + @echo "--- static analysis with sparse ---" |
| 116 | + @echo |
| 117 | +# if you feel it's too much, use C=1 instead |
| 118 | + make C=2 CHECK="/usr/bin/sparse" -C $(KDIR) M=$(PWD) modules |
| 119 | + |
| 120 | +# static analysis with gcc |
| 121 | +sa_gcc: |
| 122 | + make clean |
| 123 | + @echo |
| 124 | + @echo "--- static analysis with gcc ---" |
| 125 | + @echo |
| 126 | + make W=1 -C $(KDIR) M=$(PWD) modules |
| 127 | + |
| 128 | +# static analysis with flawfinder |
| 129 | +sa_flawfinder: |
| 130 | + make clean |
| 131 | + @echo |
| 132 | + @echo "--- static analysis with flawfinder ---" |
| 133 | + @echo |
| 134 | + flawfinder *.[ch] |
| 135 | + |
| 136 | +# static analysis with cppcheck |
| 137 | +sa_cppcheck: |
| 138 | + make clean |
| 139 | + @echo |
| 140 | + @echo "--- static analysis with cppcheck ---" |
| 141 | + @echo |
| 142 | + cppcheck -v --force --enable=all -i .tmp_versions/ -i *.mod.c -i bkp/ --suppress=missingIncludeSystem . |
| 143 | + |
| 144 | +# Packaging; just tar.xz as of now |
| 145 | +PKG_NAME := ${FNAME_C} |
| 146 | +tarxz-pkg: |
| 147 | + rm -f ../${PKG_NAME}.tar.xz 2>/dev/null |
| 148 | + make clean |
| 149 | + @echo |
| 150 | + @echo "--- packaging ---" |
| 151 | + @echo |
| 152 | + tar caf ../${PKG_NAME}.tar.xz * |
| 153 | + ls -l ../${PKG_NAME}.tar.xz |
| 154 | + @echo '=== package created: ../$(PKG_NAME).tar.xz ===' |
| 155 | + @echo 'Tip: when extracting, to extract into a dir of the same name as the tar file,' |
| 156 | + @echo ' do: tar -xvf ${PKG_NAME}.tar.xz --one-top-level' |
| 157 | + |
| 158 | +help: |
| 159 | + @echo '=== Makefile Help : additional targets available ===' |
| 160 | + @echo |
| 161 | + @echo 'TIP: type make <tab><tab> to show all valid targets' |
| 162 | + @echo |
| 163 | + |
| 164 | + @echo '--- 'usual' kernel LKM targets ---' |
| 165 | + @echo 'typing "make" or "all" target : builds the kernel module object (the .ko)' |
| 166 | + @echo 'install : installs the kernel module(s) to INSTALL_MOD_PATH (default here: /lib/modules/$(shell uname -r)/)' |
| 167 | + @echo 'clean : cleanup - remove all kernel objects, temp files/dirs, etc' |
| 168 | + |
| 169 | + @echo |
| 170 | + @echo '--- kernel code style targets ---' |
| 171 | + @echo 'code-style : "wrapper" target over the following kernel code style targets' |
| 172 | + @echo ' indent : run the $(INDENT) utility on source file(s) to indent them as per the kernel code style' |
| 173 | + @echo ' checkpatch : run the kernel code style checker tool on source file(s)' |
| 174 | + |
| 175 | + @echo |
| 176 | + @echo '--- kernel static analyzer targets ---' |
| 177 | + @echo 'sa : "wrapper" target over the following kernel static analyzer targets' |
| 178 | + @echo ' sa_sparse : run the static analysis sparse tool on the source file(s)' |
| 179 | + @echo ' sa_gcc : run gcc with option -W1 ("Generally useful warnings") on the source file(s)' |
| 180 | + @echo ' sa_flawfinder : run the static analysis flawfinder tool on the source file(s)' |
| 181 | + @echo ' sa_cppcheck : run the static analysis cppcheck tool on the source file(s)' |
| 182 | + @echo 'TIP: use coccinelle as well (requires spatch): https://www.kernel.org/doc/html/v4.15/dev-tools/coccinelle.html' |
| 183 | + |
| 184 | + @echo |
| 185 | + @echo '--- kernel dynamic analysis targets ---' |
| 186 | + @echo 'da_kasan : DUMMY target: this is to remind you to run your code with the dynamic analysis KASAN tool enabled; requires configuring the kernel with CONFIG_KASAN On, rebuild and boot it' |
| 187 | + @echo 'da_lockdep : DUMMY target: this is to remind you to run your code with the dynamic analysis LOCKDEP tool (for deep locking issues analysis) enabled; requires configuring the kernel with CONFIG_PROVE_LOCKING On, rebuild and boot it' |
| 188 | + @echo 'TIP: best to build a debug kernel with several kernel debug config options turned On, boot via it and run all your test cases' |
| 189 | + |
| 190 | + @echo |
| 191 | + @echo '--- misc targets ---' |
| 192 | + @echo 'tarxz-pkg : tar and compress the LKM source files as a tar.xz into the dir above; allows one to transfer and build the module on another system' |
| 193 | + @echo ' Tip: when extracting, to extract into a dir of the same name as the tar file,' |
| 194 | + @echo ' do: tar -xvf ${PKG_NAME}.tar.xz --one-top-level' |
| 195 | + @echo 'help : this help target' |
0 commit comments