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