check-kernel-headers.sh 2.1 KB

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