Kamil Rytarowski | cb77f0d | 2017-05-07 23:25:26 +0200 | [diff] [blame] | 1 | #! /usr/bin/env perl |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | # |
Sam Ravnborg | 63104ee | 2006-07-03 23:30:54 +0200 | [diff] [blame] | 3 | # checkversion find uses of LINUX_VERSION_CODE or KERNEL_VERSION |
| 4 | # without including <linux/version.h>, or cases of |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | # including <linux/version.h> that don't need it. |
Adrian Bunk | f4b09eb | 2006-01-03 13:37:51 +0100 | [diff] [blame] | 6 | # Copyright (C) 2003, Randy Dunlap <rdunlap@xenotime.net> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | |
Stephen Hemminger | a208868 | 2010-02-22 15:17:14 -0800 | [diff] [blame] | 8 | use strict; |
| 9 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | $| = 1; |
| 11 | |
Stephen Hemminger | a208868 | 2010-02-22 15:17:14 -0800 | [diff] [blame] | 12 | my $debugging; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | |
Stephen Hemminger | a208868 | 2010-02-22 15:17:14 -0800 | [diff] [blame] | 14 | foreach my $file (@ARGV) { |
Peter Foley | 6088e9f | 2011-04-26 19:07:56 -0400 | [diff] [blame] | 15 | next if $file =~ "include/linux/version\.h"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | # Open this file. |
Stephen Hemminger | a208868 | 2010-02-22 15:17:14 -0800 | [diff] [blame] | 17 | open( my $f, '<', $file ) |
| 18 | or die "Can't open $file: $!\n"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
| 20 | # Initialize variables. |
Stephen Hemminger | a208868 | 2010-02-22 15:17:14 -0800 | [diff] [blame] | 21 | my ($fInComment, $fInString, $fUseVersion); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | my $iLinuxVersion = 0; |
| 23 | |
Stephen Hemminger | a208868 | 2010-02-22 15:17:14 -0800 | [diff] [blame] | 24 | while (<$f>) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | # Strip comments. |
| 26 | $fInComment && (s+^.*?\*/+ +o ? ($fInComment = 0) : next); |
| 27 | m+/\*+o && (s+/\*.*?\*/+ +go, (s+/\*.*$+ +o && ($fInComment = 1))); |
| 28 | |
| 29 | # Pick up definitions. |
| 30 | if ( m/^\s*#/o ) { |
| 31 | $iLinuxVersion = $. if m/^\s*#\s*include\s*"linux\/version\.h"/o; |
| 32 | } |
| 33 | |
| 34 | # Strip strings. |
| 35 | $fInString && (s+^.*?"+ +o ? ($fInString = 0) : next); |
| 36 | m+"+o && (s+".*?"+ +go, (s+".*$+ +o && ($fInString = 1))); |
| 37 | |
| 38 | # Pick up definitions. |
| 39 | if ( m/^\s*#/o ) { |
| 40 | $iLinuxVersion = $. if m/^\s*#\s*include\s*<linux\/version\.h>/o; |
| 41 | } |
| 42 | |
| 43 | # Look for uses: LINUX_VERSION_CODE, KERNEL_VERSION, UTS_RELEASE |
Sam Ravnborg | 63104ee | 2006-07-03 23:30:54 +0200 | [diff] [blame] | 44 | if (($_ =~ /LINUX_VERSION_CODE/) || ($_ =~ /\WKERNEL_VERSION/)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | $fUseVersion = 1; |
Stephen Hemminger | a208868 | 2010-02-22 15:17:14 -0800 | [diff] [blame] | 46 | last if $iLinuxVersion; |
| 47 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | # Report used version IDs without include? |
| 51 | if ($fUseVersion && ! $iLinuxVersion) { |
| 52 | print "$file: $.: need linux/version.h\n"; |
| 53 | } |
| 54 | |
| 55 | # Report superfluous includes. |
| 56 | if ($iLinuxVersion && ! $fUseVersion) { |
| 57 | print "$file: $iLinuxVersion linux/version.h not needed.\n"; |
| 58 | } |
| 59 | |
| 60 | # debug: report OK results: |
| 61 | if ($debugging) { |
| 62 | if ($iLinuxVersion && $fUseVersion) { |
| 63 | print "$file: version use is OK ($iLinuxVersion)\n"; |
| 64 | } |
| 65 | if (! $iLinuxVersion && ! $fUseVersion) { |
| 66 | print "$file: version use is OK (none)\n"; |
| 67 | } |
| 68 | } |
| 69 | |
Stephen Hemminger | a208868 | 2010-02-22 15:17:14 -0800 | [diff] [blame] | 70 | close($f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | } |