这是一个示例Makefile,它使用关键字modules
两次(除了出现在路径和PHONY中的那些)。
# Path to the kbuild Makefile of the kernel to compile against
export KERNEL_BUILD_PATH := /lib/modules/$(shell uname -r)/build
# Name of this kernel module
export KERNEL_MODULE := hello
# List of kernel headers to include (e.g.: "linux/netdevice.h")
export KERNEL_INCLUDE :=
# Path to the directory where kernel build artifacts should be stored
export BUILD_DIRECTORY := build
# List of C files to compile into this kernel module
export C_FILES := $(wildcard src/*.c)
# List of all Rust files that will be compiled into this kernel module
export RUST_FILES := $(wildcard src/*.rs)
# Base directory of the Rust compiler
export RUST_ROOT := /usr
# Rust compiler settings
export CARGO = $(RUST_ROOT)/bin/xargo
export CARGOFLAGS =
export RCFLAGS =
export RELEASE =
-include ./config.mk
# Top-level project directory
export BASE_DIR := $(patsubst %/,%,$(dir $(abspath $(lastword $(MAKEFILE_LIST)))))
# Prevent command echoing, unless the (Kbuild-style) `V=1` parameter is set
ifneq "$(V)" "1"
.SILENT:
endif
all modules: ${BUILD_DIRECTORY}/Makefile
@$(MAKE) -C "${KERNEL_BUILD_PATH}" M="${BASE_DIR}/${BUILD_DIRECTORY}" modules
cp "${BUILD_DIRECTORY}/${KERNEL_MODULE}.ko" "${KERNEL_MODULE}.ko"
# Make sure there always is a target `Makefile` for kbuild in place
${BUILD_DIRECTORY}/Makefile: kbuild.mk
@mkdir -p "${BUILD_DIRECTORY}/src"
cp "kbuild.mk" "${BUILD_DIRECTORY}/Makefile"
insmod:
sudo insmod "${KERNEL_MODULE}.ko"
dmesg | tail
rmmod:
sudo rmmod "${KERNEL_MODULE}"
dmesg | tail
clean:
rm -rf "${BUILD_DIRECTORY}"
$(CARGO) clean
test: ${KERNEL_MODULE}.ko
sudo insmod "${KERNEL_MODULE}.ko"
sudo rmmod "${KERNEL_MODULE}"
dmesg | tail -3
.PHONY: all modules clean insmod rmmod test
我的问题是:
all modules
是否指两个单独的目标,例如all
和modules
在一起?如果是这样,为什么我们用两个不同的名称命名相同的规则?规则不会递归吗?module
出现在最后的重要性是什么?M=
代表什么?答案 0 :(得分:0)
all modules
是非常不幸的目标名称。每次Makefile被执行时都会被调用"没有指定任何目标。当您致电make -f Makefile modules
时,也会调用它。我不会那样打电话给目标。
all modules:
@echo "Hello"
.PHONY: all modules
如果你运行这个
> make
Hello
至于 M =
value:
@echo $(M)
M =简单地传递将在Makefile
中可见的变量> make M=hohoho -f Makefile value
hohoho