Reputation: 4595
How can I check my kernel version in my Makefile ??
Based on the kernel version I want to select some of the header files accordingly.
Upvotes: 0
Views: 5717
Reputation: 193
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:
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
Upvotes: 0
Reputation: 31
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
Upvotes: 3
Reputation: 84
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.
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. 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. 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.
Pretty cool, works for me.
Upvotes: 0
Reputation: 1
If you are coding some application, you might do
KERNELVERSION=$(shell uname -a)
or some other shell command, perhaps cat /proc/version
For a kernel module, see cnicutar's answer.
Upvotes: 1