check-kernel-headers.sh 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 ret = 0;
  45. int l = LINUX_VERSION_CODE & ~0xFF;
  46. int h = KERNEL_VERSION(${HDR_M},${HDR_m},0);
  47. if (l != h) {
  48. printf("Incorrect selection of kernel headers: ");
  49. printf("expected %d.%d.x, got %d.%d.x\n", ${HDR_M}, ${HDR_m},
  50. ((LINUX_VERSION_CODE>>16) & 0xFF),
  51. ((LINUX_VERSION_CODE>>8) & 0xFF));
  52. ret = ((l >= h) && !strcmp("${CHECK}", "loose")) ? 0 : 1;
  53. }
  54. return ret;
  55. }
  56. _EOF_
  57. "${EXEC}"