checkversion.pl 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #! /usr/bin/env perl
  2. # SPDX-License-Identifier: GPL-2.0
  3. #
  4. # checkversion find uses of LINUX_VERSION_CODE or KERNEL_VERSION
  5. # without including <linux/version.h>, or cases of
  6. # including <linux/version.h> that don't need it.
  7. # Copyright (C) 2003, Randy Dunlap <rdunlap@xenotime.net>
  8. use strict;
  9. $| = 1;
  10. my $debugging;
  11. foreach my $file (@ARGV) {
  12. next if $file =~ "include/linux/version\.h";
  13. # Open this file.
  14. open( my $f, '<', $file )
  15. or die "Can't open $file: $!\n";
  16. # Initialize variables.
  17. my ($fInComment, $fInString, $fUseVersion);
  18. my $iLinuxVersion = 0;
  19. while (<$f>) {
  20. # Strip comments.
  21. $fInComment && (s+^.*?\*/+ +o ? ($fInComment = 0) : next);
  22. m+/\*+o && (s+/\*.*?\*/+ +go, (s+/\*.*$+ +o && ($fInComment = 1)));
  23. # Pick up definitions.
  24. if ( m/^\s*#/o ) {
  25. $iLinuxVersion = $. if m/^\s*#\s*include\s*"linux\/version\.h"/o;
  26. }
  27. # Strip strings.
  28. $fInString && (s+^.*?"+ +o ? ($fInString = 0) : next);
  29. m+"+o && (s+".*?"+ +go, (s+".*$+ +o && ($fInString = 1)));
  30. # Pick up definitions.
  31. if ( m/^\s*#/o ) {
  32. $iLinuxVersion = $. if m/^\s*#\s*include\s*<linux\/version\.h>/o;
  33. }
  34. # Look for uses: LINUX_VERSION_CODE, KERNEL_VERSION, UTS_RELEASE
  35. if (($_ =~ /LINUX_VERSION_CODE/) || ($_ =~ /\WKERNEL_VERSION/)) {
  36. $fUseVersion = 1;
  37. last if $iLinuxVersion;
  38. }
  39. }
  40. # Report used version IDs without include?
  41. if ($fUseVersion && ! $iLinuxVersion) {
  42. print "$file: $.: need linux/version.h\n";
  43. }
  44. # Report superfluous includes.
  45. if ($iLinuxVersion && ! $fUseVersion) {
  46. print "$file: $iLinuxVersion linux/version.h not needed.\n";
  47. }
  48. # debug: report OK results:
  49. if ($debugging) {
  50. if ($iLinuxVersion && $fUseVersion) {
  51. print "$file: version use is OK ($iLinuxVersion)\n";
  52. }
  53. if (! $iLinuxVersion && ! $fUseVersion) {
  54. print "$file: version use is OK (none)\n";
  55. }
  56. }
  57. close($f);
  58. }