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