| 1 |
#!/usr/bin/perl |
| 2 |
use strict; |
| 3 |
use warnings; |
| 4 |
use File::Find (); |
| 5 |
use File::Spec (); |
| 6 |
|
| 7 |
use File::Basename; |
| 8 |
use lib dirname (__FILE__); |
| 9 |
use KernelWedge qw(MODULE_FILENAME_RE); |
| 10 |
|
| 11 |
# Dummy filename for when we find that a module is actually built-in |
| 12 |
use constant BUILTIN => "<builtin>"; |
| 13 |
|
| 14 |
my $configdir = $ENV{KW_CONFIG_DIR}; |
| 15 |
if (!defined($configdir)) { |
| 16 |
print STDERR "$0: Required environment variable \$KW_CONFIG_DIR is not defined\n"; |
| 17 |
exit 2; |
| 18 |
} |
| 19 |
my $sysdir="$configdir/modules/"; |
| 20 |
|
| 21 |
my @module_files; |
| 22 |
my @modules_builtin; |
| 23 |
my %modules; |
| 24 |
my %missing; |
| 25 |
my %loaded; |
| 26 |
|
| 27 |
sub find_all_modules { |
| 28 |
my ($moddir) = @_; |
| 29 |
|
| 30 |
File::Find::find({ |
| 31 |
follow => 1, # If $moddir is a symlink, follow it. |
| 32 |
wanted => sub { |
| 33 |
if ($_ =~ MODULE_FILENAME_RE) { |
| 34 |
push @module_files, |
| 35 |
File::Spec->abs2rel($File::Find::name, |
| 36 |
$moddir); |
| 37 |
} |
| 38 |
} |
| 39 |
}, $moddir); |
| 40 |
|
| 41 |
if (open(my $builtin, "$moddir/modules.builtin")) { |
| 42 |
while (<$builtin>) { |
| 43 |
chomp; |
| 44 |
push @modules_builtin, $_; |
| 45 |
} |
| 46 |
close($builtin); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
sub wildcard_to_regexp { |
| 51 |
my ($pattern) = @_; |
| 52 |
|
| 53 |
# Convert to regexp syntax. We handle '**' as a recursive |
| 54 |
# match-all. We don't bother to handle '\' or '[...]'. |
| 55 |
# Linux treats '-' and '_' as equivalent, and neither |
| 56 |
# is used consistently. So let each match the other. |
| 57 |
my %glob_re = ('?' => '[^/]', |
| 58 |
'*' => '[^/]*', |
| 59 |
'**' => '.*', |
| 60 |
'-' => '[-_]', |
| 61 |
'_' => '[-_]', |
| 62 |
'' => ''); |
| 63 |
$pattern =~ s/(.*?)(\*\*|[?*\-_]|)/ |
| 64 |
quotemeta($1) . $glob_re{$2}/eg; |
| 65 |
|
| 66 |
return $pattern; |
| 67 |
} |
| 68 |
|
| 69 |
sub is_really_wild { |
| 70 |
my ($pattern) = @_; |
| 71 |
|
| 72 |
return scalar($pattern =~ /[?*]/); |
| 73 |
} |
| 74 |
|
| 75 |
sub find_modules { |
| 76 |
my ($moddir, $pattern, $optional) = @_; |
| 77 |
my $wild = is_really_wild($pattern); |
| 78 |
|
| 79 |
my @regexps; |
| 80 |
if ($wild) { |
| 81 |
my $re; |
| 82 |
if ($pattern =~ m|^([^?*]*)/(.*)|) { |
| 83 |
my $subdir = $1; |
| 84 |
if (! -d "$moddir/$subdir") { |
| 85 |
if (-d "$moddir/kernel/$subdir") { |
| 86 |
$subdir = "kernel/$subdir"; |
| 87 |
} elsif (!$optional) { |
| 88 |
print STDERR "pattern $pattern refers to nonexistent subdirectory\n"; |
| 89 |
unless ($ENV{KW_CHECK_NONFATAL}) { |
| 90 |
$! = 1; |
| 91 |
die; |
| 92 |
} |
| 93 |
} else { |
| 94 |
return (); |
| 95 |
} |
| 96 |
} |
| 97 |
$re = quotemeta($subdir) . '/' . wildcard_to_regexp($2); |
| 98 |
} else { |
| 99 |
$re = wildcard_to_regexp($pattern); |
| 100 |
} |
| 101 |
|
| 102 |
# Add module suffix; anchor at start and end of string |
| 103 |
@regexps = ('^' . $re . MODULE_FILENAME_RE); |
| 104 |
} else { |
| 105 |
# If pattern doesn't include a wildcard, find the |
| 106 |
# module in any subdir, but prefer a module in the |
| 107 |
# kernel subdir. We still do wildcard processing |
| 108 |
# to handle equivalence of '-' and '_'. |
| 109 |
my $re = wildcard_to_regexp($pattern); |
| 110 |
@regexps = ('^kernel/(?:.*/)?' . $re . MODULE_FILENAME_RE, |
| 111 |
'(?:^|/)' . $re . MODULE_FILENAME_RE); |
| 112 |
} |
| 113 |
|
| 114 |
my @modules; |
| 115 |
regexp_loop: |
| 116 |
for my $re (@regexps) { |
| 117 |
for (@module_files) { |
| 118 |
if (/$re/) { |
| 119 |
push @modules, $_; |
| 120 |
last regexp_loop unless $wild; |
| 121 |
} |
| 122 |
} |
| 123 |
if (!$wild && grep(/$re/, @modules_builtin)) { |
| 124 |
push @modules, BUILTIN; |
| 125 |
last; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
return @modules; |
| 130 |
} |
| 131 |
|
| 132 |
sub loadlist { |
| 133 |
my ($list, $moddir) = @_; |
| 134 |
|
| 135 |
if ($loaded{$list}) { |
| 136 |
$! = 1; |
| 137 |
die "include loop detected loading $list\n"; |
| 138 |
} |
| 139 |
$loaded{$list}=1; |
| 140 |
|
| 141 |
my $fh; |
| 142 |
unless (open($fh, $list)) { |
| 143 |
$! = 1; |
| 144 |
die "cannot read $list\n"; |
| 145 |
} |
| 146 |
while (<$fh>) { |
| 147 |
s/^\s*//; |
| 148 |
s/\s*$//; |
| 149 |
if (/^#include\s+<(.*)>$/) { |
| 150 |
my $basename=$1; |
| 151 |
loadlist($sysdir.$basename, $moddir); |
| 152 |
} |
| 153 |
elsif (/^#include\s+"(.*)"$/) { |
| 154 |
my $include=$1; |
| 155 |
my ($dirname)=$list=~m!(.*/).*!; |
| 156 |
loadlist($dirname.$include, $moddir); |
| 157 |
} |
| 158 |
elsif (/^$/) { |
| 159 |
next; |
| 160 |
} |
| 161 |
elsif (/^#/) { |
| 162 |
next; |
| 163 |
} |
| 164 |
elsif (/^(.*) -$/) { |
| 165 |
# If this was explicitly included and is missing, |
| 166 |
# we no longer care |
| 167 |
delete $missing{$1}; |
| 168 |
|
| 169 |
for (find_modules($moddir, $1, 1)) { |
| 170 |
delete $modules{$_}; |
| 171 |
} |
| 172 |
} |
| 173 |
else { |
| 174 |
my ($pattern, $optional, @found); |
| 175 |
|
| 176 |
if (/^(.*) \?$/) { |
| 177 |
($pattern, $optional) = ($1, 1); |
| 178 |
} |
| 179 |
# Support dash prefixing for backwards compatibility. |
| 180 |
elsif (/^-(.*)/) { |
| 181 |
($pattern, $optional) = ($1, 1); |
| 182 |
} else { |
| 183 |
($pattern, $optional) = ($_, 0); |
| 184 |
} |
| 185 |
|
| 186 |
@found = find_modules($moddir, $pattern, $optional); |
| 187 |
for (@found) { |
| 188 |
$modules{$_} = 1 unless $_ eq BUILTIN; |
| 189 |
} |
| 190 |
|
| 191 |
# Check for missing required module. This is not |
| 192 |
# yet an error as it might be excluded later. |
| 193 |
if (!is_really_wild($pattern) && !$optional |
| 194 |
&& !@found) { |
| 195 |
$missing{$pattern} = 1; |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
close $fh; |
| 200 |
} |
| 201 |
|
| 202 |
if (@ARGV < 3) { |
| 203 |
print STDERR "$0: Required parameters missing\n"; |
| 204 |
exit 2; |
| 205 |
} |
| 206 |
my ($modlistdir, $list, $moddir) = @ARGV; |
| 207 |
find_all_modules($moddir); |
| 208 |
if (-e "$modlistdir/$list") { |
| 209 |
loadlist("$modlistdir/$list", $moddir); |
| 210 |
} else { |
| 211 |
loadlist("$sysdir$list", $moddir); |
| 212 |
} |
| 213 |
|
| 214 |
if (keys %missing) { |
| 215 |
for (keys %missing) { |
| 216 |
print STDERR "missing module $_\n"; |
| 217 |
} |
| 218 |
exit 1 unless $ENV{'KW_CHECK_NONFATAL'}; |
| 219 |
} |
| 220 |
|
| 221 |
foreach my $m (sort keys %modules) { |
| 222 |
print "$m\n"; |
| 223 |
} |