如何在我的 Makefile 中检查我的内核版本?
根据内核版本,我想相应地选择一些头文件。
KVER = $(shell uname -r)
KMAJ = $(shell echo $(KVER) | \
sed -e 's/^\([0-9][0-9]*\)\.[0-9][0-9]*\.[0-9][0-9]*.*/\1/')
KMIN = $(shell echo $(KVER) | \
sed -e 's/^[0-9][0-9]*\.\([0-9][0-9]*\)\.[0-9][0-9]*.*/\1/')
KREV = $(shell echo $(KVER) | \
sed -e 's/^[0-9][0-9]*\.[0-9][0-9]*\.\([0-9][0-9]*\).*/\1/')
kver_ge = $(shell \
echo test | awk '{if($(KMAJ) < $(1)) {print 0} else { \
if($(KMAJ) > $(1)) {print 1} else { \
if($(KMIN) < $(2)) {print 0} else { \
if($(KMIN) > $(2)) {print 1} else { \
if($(KREV) < $(3)) {print 0} else { print 1 } \
}}}}}' \
)
ifeq ($(call kver_ge,3,8,0),1)
echo great or equal than 3.8.0
else
echo less than 3.8.0
endif
如果您正在编写一些应用程序,您可能会这样做
KERNELVERSION=$(shell uname -a)
或其他一些shell命令,也许cat /proc/version
对于内核模块,请参阅cnicutar 的回答。
我没有 50 名声望,所以我无法在评论中回答 voght 先生的评论(请给我投票,这样我就可以了!)但我可以这样做,就像另一个答案一样,所以就这样吧。
该代码使用内置的 shell 来输出(bash)命令,如uname和echo,并为结果分配类似变量的宏KVER
。uname提供内核版本,代码继续使用 unix sed(流编辑器;man sed更多)从结果中提取每个主要、次要和 rev 号,并将它们分配给离散变量类宏。然后他为测试内核版本是否大于作为参数提供的版本kver_ge
的进程(使用awk、test和if shell 内置函数)分配一个宏名称。很酷,对我有用。
一个简单的方法是首先通过 Makefile 中的测试分配一个变量 true/false(在下面的示例中实际上是 1/0),然后使用 ifeq 命令,就像 goodgoodstudydaydayup 的答案一样,这是一个更简单的方法:
KERNEL_GT_5_4 = $(shell expr `uname -r` \> "5.4.0-0-generic")
all:
ifeq (${KERNEL_GT_5_4},1)
echo greater than 5.4.0-0-generic
else
echo less than 5.4.0-0-generic
endif