summaryrefslogtreecommitdiffstats
path: root/commands/preprocess
blob: 7cd104161dc02bd2ef32bbc2900b15e5fba28690 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/usr/bin/perl
use strict;
use warnings;
use File::Find ();
use File::Spec ();

# Dummy filename for when we find that a module is actually built-in
use constant BUILTIN => "<builtin>";

my $defconfigdir = $ENV{KW_DEFCONFIG_DIR};
if (!defined($defconfigdir)) {
	print STDERR "$0: Required environment variable \$KW_DEFCONFIG_DIR is not defined\n";
	exit 2;
}
my $sysdir="$defconfigdir/modules/";
my $os = `dpkg-architecture -qDEB_HOST_ARCH_OS`;
chomp $os;

my @module_files;
my @modules_builtin;
my %modules;
my %missing;
my %loaded;

sub find_all_modules {
	my ($moddir) = @_;

	File::Find::find({
		follow => 1, # If $moddir is a symlink, follow it.
		wanted => sub {
			if (/\.ko(?:\.(?:xz|zstd))?$/) {
				push @module_files,
				    File::Spec->abs2rel($File::Find::name,
							$moddir);
			}
		}
	}, $moddir);

	if ($os eq 'linux') {
		if (open(my $builtin, "$moddir/modules.builtin")) {
			while (<$builtin>) {
				chomp;
				push @modules_builtin, $_;
			}
			close($builtin);
		}
	}
}

sub wildcard_to_regexp {
	my ($pattern) = @_;

	# Convert to regexp syntax.  We handle '**' as a recursive
	# match-all.  We don't bother to handle '\' or '[...]'.
	my %glob_re = ('?'  => '[^/]',
		       '*'  => '[^/]*',
		       '**' => '.*',
		       ''   => '');
	my $extra_wild;
	if ($os eq 'linux') {
		# Linux treats '-' and '_' as equivalent, and neither
		# is used consistently.  So let each match the other.
		$glob_re{'-'} = $glob_re{'_'} = '[-_]';
		$extra_wild = '|[-_]';
	} else {
		$extra_wild = '';
	}
	$pattern =~ s/(.*?)(\*\*|[?*]$extra_wild|)/
                      quotemeta($1) . $glob_re{$2}/eg;

	return $pattern;
}

sub is_really_wild {
	my ($pattern) = @_;

	return scalar($pattern =~ /[?*]/);
}

sub find_modules {
	my ($moddir, $pattern, $optional) = @_;
	my $wild = is_really_wild($pattern);

	my @regexps;
	if ($wild) {
		my $re;
		if ($pattern =~ m|^([^?*]*)/(.*)|) {
			my $subdir = $1;
			if (! -d "$moddir/$subdir") {
				if (-d "$moddir/kernel/$subdir") {
					$subdir = "kernel/$subdir";
				} elsif (!$optional) {
					print STDERR "pattern $pattern refers to nonexistent subdirectory\n";
					unless ($ENV{KW_CHECK_NONFATAL}) {
						$! = 1;
						die;
					}
				} else {
					return ();
				}
			}
			$re = quotemeta($subdir) . '/' . wildcard_to_regexp($2);
		} else {
			$re = wildcard_to_regexp($pattern);
		}

		# Add module suffix; anchor at start and end of string
		@regexps = ('^' . $re . '\.ko(?:\.(?:xz|zstd))?$');
	} else {
		# If pattern doesn't include a wildcard, find the
		# module in any subdir, but prefer a module in the
		# kernel subdir.  We still do wildcard processing
		# to handle equivalence of '-' and '_' for Linux.
		my $re = wildcard_to_regexp($pattern);
		@regexps = ('^kernel/(?:.*/)?' . $re . '\.ko(?:\.(?:xz|zstd))?$',
			    '(?:^|/)' . $re . '\.ko(?:\.(?:xz|zstd))?$');
	}

	my @modules;
      regexp_loop:
	for my $re (@regexps) {
		for (@module_files) {
			if (/$re/) {
				push @modules, $_;
				last regexp_loop unless $wild;
			}
		}
		if (!$wild && grep(/$re/, @modules_builtin)) {
			push @modules, BUILTIN;
			last;
		}
	}

	return @modules;
}

sub loadlist {
	my ($list, $moddir) = @_;
	
	if ($loaded{$list}) {
		$! = 1;
		die "include loop detected loading $list\n";
	}
	$loaded{$list}=1;
	
	my $fh;
	unless (open($fh, $list)) {
		$! = 1;
		die "cannot read $list\n";
	}
	while (<$fh>) {
		s/^\s*//;
		s/\s*$//;
		if (/^#include\s+<(.*)>$/) {
			my $basename=$1;
			loadlist($sysdir.$basename, $moddir);
		}
		elsif (/^#include\s+"(.*)"$/) {
			my $include=$1;
			my ($dirname)=$list=~m!(.*/).*!;
			loadlist($dirname.$include, $moddir);
		}
		elsif (/^$/) {
			next;
		}
		elsif (/^#/) {
			next;
		}
		elsif (/^(.*) -$/) {
			# If this was explicitly included and is missing,
			# we no longer care
			delete $missing{$1};

			for (find_modules($moddir, $1, 1)) {
				delete $modules{$_};
			}
		}
		else {
			my ($pattern, $optional, @found);

			if (/^(.*) \?$/) {
				($pattern, $optional) = ($1, 1);
			}
			# Support dash prefixing for backwards compatibility.
			elsif (/^-(.*)/) {
				($pattern, $optional) = ($1, 1);
			} else {
				($pattern, $optional) = ($_, 0);
			}

			@found = find_modules($moddir, $pattern, $optional);
			for (@found) {
				$modules{$_} = 1 unless $_ eq BUILTIN;
			}

			# Check for missing required module.  This is not
			# yet an error as it might be excluded later.
			if (!is_really_wild($pattern) && !$optional
			    && !@found) {
				$missing{$pattern} = 1;
			}
		}
	}
	close $fh;
}

if (@ARGV < 2) {
	print STDERR "$0: Required parameters missing\n";
	exit 2;
}
my ($file, $moddir) = @ARGV;
find_all_modules($moddir);
loadlist($file, $moddir);

if (keys %missing) {
	for (keys %missing) {
		print STDERR "missing module $_\n";
	}
	exit 1 unless $ENV{'KW_CHECK_NONFATAL'};
}

foreach my $m (sort keys %modules) {
	print "$m\n";
}