在寫 Linux 核心的 Kconfig 時,default 決定了一個選項在 make menuconfig 中預設的狀態。它看起來簡單,但 n/y/m 三個值對 tristate 類型選項的意義各不相同。
config TEST_counter1
tristate "test counter1"
# 預設不編譯
default n
help
this is for pulse counter test.
config TEST_counter2
tristate "test counter2"
# 預設編譯進核心
default y
help
this is for pulse counter test.
config TEST_counter3
tristate "test counter3"
# 預設編譯為核心模組
default m
help
this is for pulse counter test.
這三個值只有對 tristate 類型的選項才全部有意義。若選項宣告為 bool,就只接受 n 與 y;寫成 default m 是不合理的。
Kconfig 寫好後執行 make menuconfig,會看到:
< > test counter1 (NEW) <*> test counter2 (NEW) <M> test counter3 (NEW)
三行分別對應 default n、default y、default m:空的方括號代表不編譯,星號代表內建,M 代表編譯成模組。
default 並不限於固定值,實務上常見的是依其他選項決定:
config FOO
bool "Enable foo"
default y
config FOO_DEBUG
bool "Foo debug messages"
depends on FOO
default n
也可以指定字串或數值,例如:
config DEFAULT_TTY_PORT
int "Default TTY port"
default "4"
當 default 的值引用了另一個符號時,該符號必須先被定義,否則 Kconfig 會報錯。這也是為什麼 Kconfig 檔裡的定義順序並非完全自由的。