xref: /OK3568_Linux_fs/buildroot/support/scripts/check-kernel-headers.sh (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun#!/bin/sh
2*4882a593Smuzhiyun
3*4882a593Smuzhiyun# This script (and the embedded C code) will check that the actual
4*4882a593Smuzhiyun# headers version match the user told us they were:
5*4882a593Smuzhiyun#
6*4882a593Smuzhiyun# - if both versions are the same, all is well.
7*4882a593Smuzhiyun#
8*4882a593Smuzhiyun# - if the actual headers are older than the user told us, this is
9*4882a593Smuzhiyun#   an error.
10*4882a593Smuzhiyun#
11*4882a593Smuzhiyun# - if the actual headers are more recent than the user told us, and
12*4882a593Smuzhiyun#   we are doing a strict check, then this is an error.
13*4882a593Smuzhiyun#
14*4882a593Smuzhiyun# - if the actual headers are more recent than the user told us, and
15*4882a593Smuzhiyun#   we are doing a loose check, then a warning is printed, but this is
16*4882a593Smuzhiyun#   not an error.
17*4882a593Smuzhiyun
18*4882a593SmuzhiyunBUILDDIR="${1}"
19*4882a593SmuzhiyunSYSROOT="${2}"
20*4882a593Smuzhiyun# Make sure we have enough version components
21*4882a593SmuzhiyunHDR_VER="${3}.0.0"
22*4882a593SmuzhiyunCHECK="${4}"  # 'strict' or 'loose'
23*4882a593Smuzhiyun
24*4882a593SmuzhiyunHDR_M="${HDR_VER%%.*}"
25*4882a593SmuzhiyunHDR_V="${HDR_VER#*.}"
26*4882a593SmuzhiyunHDR_m="${HDR_V%%.*}"
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun# Exit on any error, so we don't try to run an unexisting program if the
29*4882a593Smuzhiyun# compilation fails.
30*4882a593Smuzhiyunset -e
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun# Set the clean-up trap in advance to prevent a race condition in which we
33*4882a593Smuzhiyun# create the file but get a SIGTERM before setting it. Notice that we don't
34*4882a593Smuzhiyun# need to care about EXEC being empty, since 'rm -f ""' does nothing.
35*4882a593Smuzhiyuntrap 'rm -f "${EXEC}"' EXIT
36*4882a593Smuzhiyun
37*4882a593SmuzhiyunEXEC="$(mktemp -p "${BUILDDIR}" -t .check-headers.XXXXXX)"
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun# We do not want to account for the patch-level, since headers are
40*4882a593Smuzhiyun# not supposed to change for different patchlevels, so we mask it out.
41*4882a593Smuzhiyun# This only applies to kernels >= 3.0, but those are the only one
42*4882a593Smuzhiyun# we actually care about; we treat all 2.6.x kernels equally.
43*4882a593Smuzhiyun${HOSTCC} -imacros "${SYSROOT}/usr/include/linux/version.h" \
44*4882a593Smuzhiyun          -x c -o "${EXEC}" - <<_EOF_
45*4882a593Smuzhiyun#include <stdio.h>
46*4882a593Smuzhiyun#include <stdlib.h>
47*4882a593Smuzhiyun#include <string.h>
48*4882a593Smuzhiyun
49*4882a593Smuzhiyunint main(int argc __attribute__((unused)),
50*4882a593Smuzhiyun         char** argv __attribute__((unused)))
51*4882a593Smuzhiyun{
52*4882a593Smuzhiyun    int l = LINUX_VERSION_CODE & ~0xFF;
53*4882a593Smuzhiyun    int h = KERNEL_VERSION(${HDR_M},${HDR_m},0);
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun    if ((l >= h) && !strcmp("${CHECK}", "loose"))
56*4882a593Smuzhiyun        return 0;
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun    if (l != h) {
59*4882a593Smuzhiyun        printf("Incorrect selection of kernel headers: ");
60*4882a593Smuzhiyun        printf("expected %d.%d.x, got %d.%d.x\n", ${HDR_M}, ${HDR_m},
61*4882a593Smuzhiyun               ((LINUX_VERSION_CODE>>16) & 0xFF),
62*4882a593Smuzhiyun               ((LINUX_VERSION_CODE>>8) & 0xFF));
63*4882a593Smuzhiyun        return 1;
64*4882a593Smuzhiyun    }
65*4882a593Smuzhiyun    return 0;
66*4882a593Smuzhiyun}
67*4882a593Smuzhiyun_EOF_
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun"${EXEC}"
70