简体   繁体   English

如何在 Makefile 中检查内核版本

[英]How to check kernel version in Makefile

How can I check my kernel version in my Makefile ??如何在我的 Makefile 中检查我的内核版本?

Based on the kernel version I want to select some of the header files accordingly.根据内核版本,我想相应地选择一些头文件。

If you are coding some application, you might do如果你正在编写一些应用程序,你可能会这样做

 KERNELVERSION=$(shell uname -a)

or some other shell command, perhaps cat /proc/version或其他一些 shell 命令,可能是cat /proc/version

For a kernel module, see cnicutar's answer .对于内核模块,请参阅cnicutar 的回答

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

I don't have 50 reputation so I can't answer Mr. voght's comment in a comment (upvote me so I can!) but I can do so just as another answer, so here goes.我没有 50 声望,所以我无法在评论中回答 voght 先生的评论(给我点赞,这样我就可以了!)但我可以这样做,就像另一个答案一样,所以在这里。

The code uses the shell built-in to shell out ( bash ) commands like uname and echo , and assign variable-like macros such as KVER to the result.该代码使用内置的 shell 来输出( bash )命令,如unameecho ,并将类似变量的宏(如KVER给结果。 uname provides the kernel version, the code goes on to use the unix sed (stream editor; man sed for more) to extract each of the major, minor and rev numbers from the results and assign them to discrete variable-like macros. uname提供内核版本,代码继续使用 unix sed (流编辑器; man sed获取更多信息)从结果中提取每个主要、次要和 rev 编号,并将它们分配给离散变量类宏。 Then he assigns a macro name, kver_ge to the process (using awk , test , and if shell built-ins) of testing whether the kernel version is greater than the one provided as an argument.然后,他将宏名称kver_ge分配给测试内核版本是否大于作为参数提供的版本的过程(使用awktestif shell 内置函数)。 Pretty cool, works for me.很酷,对我有用。

A simple way is to first assign a variable true/false (actually 1/0 in example below) by a test in Makefile and then use ifeq command like the answer from goodgoodstudydaydayup, here is a simpler approach:一个简单的方法是首先通过 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   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM