2017-07-13 30 views
0

這是一個示例Makefile,它使用關鍵字modules兩次(除了出現在路徑和PHONY中的那些)。「模塊」在這個Makefile中引用了什麼?

# 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 

我的問題是:

  • 在第35行,確實all modules是指兩個不同的目標,例如allmodules在一起嗎?如果是這樣,爲什麼我們用兩個不同的名字命名相同的規則?規則不是遞歸的嗎?
  • 在第36行,關鍵字module出現在最後的意義是什麼?
  • 在第36行,M=代表什麼?

回答

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 
+0

'所有模塊「都是n一個目標的名字。這意味着定義了兩個完全不同的目標,「全部」和「模塊」,它們都具有相同的配方。所以如果你運行'make all'或'make modules',它將運行這個相同的配方。這與整個規則編寫兩次完全一樣,一次使用目標'all',一次使用目標'modules'。除了少打字。 – MadScientist

+0

是的,你是對的。我寫了「不幸的目標名稱」,從某種意義上說,做「all:modules」然後創建目標模塊會更加清楚。但你是對的。這不是一個單一的目標,而是兩個單獨的目標。 – mko