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