Skip to content

Instantly share code, notes, and snippets.

@gabrieleara
Last active March 14, 2022 09:55
Show Gist options
  • Select an option

  • Save gabrieleara/4167a8db15612260d9e82b8664ee4ba5 to your computer and use it in GitHub Desktop.

Select an option

Save gabrieleara/4167a8db15612260d9e82b8664ee4ba5 to your computer and use it in GitHub Desktop.
A Makefile for a Linux kernel module with some quality of life improvements
#!/bin/bash
function get_script_path() {
realpath "$(dirname "${BASH_SOURCE[0]}")"
}
function main() {
local module_dir
module_dir="$(get_script_path)"
# if [ -z "$DEBUG" ]; then
# export ARCH=arm64
# export CROSS_COMPILE="/usr/bin/aarch64-linux-gnu-"
# else
# echo "DEBUG BUILD!"
# fi
# Passes the arguments of the script to make directly!
make -C "$module_dir" "$@" # -j $(($(nproc) * 6 / 8))
}
(
set -e
main "$@"
)
# ╔═══════════════════════════════════════════════════════╗
# ║ Configuration ║
# ╚═══════════════════════════════════════════════════════╝
MODULE_NAME=<put-module-name-here>
obj-m += $(MODULE_NAME).o
# ┌───────────────────────────────────────────────────────┐
# │ Sources │
# └───────────────────────────────────────────────────────┘
$(MODULE_NAME)-objs += <put-list-of-source-files-here>
# ┌───────────────────────────────────────────────────────┐
# │ Compilation Parameters │
# └───────────────────────────────────────────────────────┘
# Export this variable or pass it to make to supply custom
# flags to the compiler
USER_CFLAGS ?=
# The path to use for the Linux kernel to build against
KERNEL_DIR ?= /lib/modules/$(shell uname -r)/build
# ╔═══════════════════════════════════════════════════════╗
# ║ Build Stuff -- Do Not Touch Below This Line ║
# ╚═══════════════════════════════════════════════════════╝
# This is necessary to be able to build this module when
# invoking make with -C
MODULE_PATH := $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))
# Flags to supply when building the module
EXTRA_CFLAGS := # put here any flags that are always necessary when building the module
EXTRA_CFLAGS += $(USER_CFLAGS) # user flags override defaults
.PHONY: modules clean # debug
modules clean:
$(MAKE) -C $(KERNEL_DIR) M=$(MODULE_PATH) $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment