2 # SPDX-License-Identifier: GPL-2.0
17 my $basename = abs_path($0);
18 $basename =~ s,/[^/]+$,/,;
20 my $prefix=$basename . "../Documentation/features";
23 "debug|d+" => \$debug,
28 'feature=s' => \$feat,
32 pod2usage(1) if $help;
33 pod2usage(-exitstatus => 0, -verbose => 2) if $man;
35 pod2usage(1) if (scalar @ARGV < 1 || @ARGV > 2);
37 my ($cmd, $arg) = @ARGV;
39 pod2usage(2) if ($cmd ne "current" && $cmd ne "rest" && $cmd ne "validate"
40 && $cmd ne "ls" && $cmd ne "list");
42 require Data::Dumper if ($debug);
48 # Displays an error message, printing file name and line
50 sub parse_error($$$$) {
51 my ($file, $ln, $msg, $data) = @_;
55 print STDERR "Warning: file $file#$ln:\n\t$msg";
58 print STDERR ". Line\n\t\t$data";
65 # Parse a features file, storing its contents at %data
68 my $h_name = "Feature";
69 my $h_kconfig = "Kconfig";
70 my $h_description = "Description";
71 my $h_subsys = "Subsystem";
72 my $h_status = "Status";
73 my $h_arch = "Architecture";
75 my $max_size_name = length($h_name);
76 my $max_size_kconfig = length($h_kconfig);
77 my $max_size_description = length($h_description);
78 my $max_size_subsys = length($h_subsys);
79 my $max_size_status = length($h_status);
80 my $max_size_arch = length($h_arch);
83 my $file = $File::Find::name;
85 my $mode = (stat($file))[2];
86 return if ($mode & S_IFDIR);
87 return if ($file =~ m,($prefix)/arch-support.txt,);
88 return if (!($file =~ m,arch-support.txt$,));
91 $subsys = $2 if ( m,.*($prefix)/([^/]+).*,);
93 if (length($subsys) > $max_size_subsys) {
94 $max_size_subsys = length($subsys);
105 print STDERR "Opening $file\n" if ($debug > 1);
111 if (m/^\#\s+Feature\s+name:\s*(.*\S)/) {
113 if (length($name) > $max_size_name) {
114 $max_size_name = length($name);
118 if (m/^\#\s+Kconfig:\s*(.*\S)/) {
120 if (length($kconfig) > $max_size_kconfig) {
121 $max_size_kconfig = length($kconfig);
125 if (m/^\#\s+description:\s*(.*\S)/) {
127 if (length($description) > $max_size_description) {
128 $max_size_description = length($description);
133 next if (m/^\s*\-+\s*$/);
134 next if (m/^\s*\|\s*arch\s*\|\s*status\s*\|\s*$/);
140 if (m/^\s*\|\s*(\S+):\s*\|\s*(\S+)\s*\|\s*$/) {
144 if (length($status) > $max_size_status) {
145 $max_size_status = length($status);
147 if (length($a) > $max_size_arch) {
148 $max_size_arch = length($a);
151 $status = "---" if ($status =~ m/^\.\.$/);
154 $arch_table{$a} = $status;
158 #Everything else is an error
159 parse_error($file, $ln, "line is invalid", $_);
164 parse_error($file, $ln, "Feature name not found", "");
168 parse_error($file, $ln, "Subsystem not found", "") if (!$subsys);
169 parse_error($file, $ln, "Kconfig not found", "") if (!$kconfig);
170 parse_error($file, $ln, "Description not found", "") if (!$description);
173 parse_error($file, $ln, "Architecture table not found", "");
177 $data{$name}->{where} = $file;
178 $data{$name}->{subsys} = $subsys;
179 $data{$name}->{kconfig} = $kconfig;
180 $data{$name}->{description} = $description;
181 $data{$name}->{comments} = $comments;
182 $data{$name}->{table} = \%arch_table;
186 # Output feature(s) for a given architecture
188 sub output_arch_table {
189 my $title = "Feature status on $arch architecture";
191 print "=" x length($title) . "\n";
193 print "=" x length($title) . "\n\n";
195 print "=" x $max_size_subsys;
197 print "=" x $max_size_name;
199 print "=" x $max_size_kconfig;
201 print "=" x $max_size_status;
203 print "=" x $max_size_description;
205 printf "%-${max_size_subsys}s ", $h_subsys;
206 printf "%-${max_size_name}s ", $h_name;
207 printf "%-${max_size_kconfig}s ", $h_kconfig;
208 printf "%-${max_size_status}s ", $h_status;
209 printf "%-${max_size_description}s\n", $h_description;
210 print "=" x $max_size_subsys;
212 print "=" x $max_size_name;
214 print "=" x $max_size_kconfig;
216 print "=" x $max_size_status;
218 print "=" x $max_size_description;
221 foreach my $name (sort {
222 ($data{$a}->{subsys} cmp $data{$b}->{subsys}) ||
225 next if ($feat && $name ne $feat);
227 my %arch_table = %{$data{$name}->{table}};
228 printf "%-${max_size_subsys}s ", $data{$name}->{subsys};
229 printf "%-${max_size_name}s ", $name;
230 printf "%-${max_size_kconfig}s ", $data{$name}->{kconfig};
231 printf "%-${max_size_status}s ", $arch_table{$arch};
232 printf "%-s\n", $data{$name}->{description};
235 print "=" x $max_size_subsys;
237 print "=" x $max_size_name;
239 print "=" x $max_size_kconfig;
241 print "=" x $max_size_status;
243 print "=" x $max_size_description;
248 # list feature(s) for a given architecture
250 sub list_arch_features {
251 print "#\n# Kernel feature support matrix of the '$arch' architecture:\n#\n";
253 foreach my $name (sort {
254 ($data{$a}->{subsys} cmp $data{$b}->{subsys}) ||
257 next if ($feat && $name ne $feat);
259 my %arch_table = %{$data{$name}->{table}};
261 my $status = $arch_table{$arch};
262 $status = " " x ((4 - length($status)) / 2) . $status;
264 printf " %${max_size_subsys}s/ ", $data{$name}->{subsys};
265 printf "%-${max_size_name}s: ", $name;
266 printf "%-5s| ", $status;
267 printf "%${max_size_kconfig}s # ", $data{$name}->{kconfig};
268 printf " %s\n", $data{$name}->{description};
273 # Output a feature on all architectures
276 my $title = "Feature $feat";
278 print "=" x length($title) . "\n";
280 print "=" x length($title) . "\n\n";
282 print ":Subsystem: $data{$feat}->{subsys} \n" if ($data{$feat}->{subsys});
283 print ":Kconfig: $data{$feat}->{kconfig} \n" if ($data{$feat}->{kconfig});
285 my $desc = $data{$feat}->{description};
286 $desc =~ s/^([a-z])/\U$1/;
288 print "\n$desc.\n\n";
290 my $com = $data{$feat}->{comments};
295 print "--------\n\n";
299 print "=" x $max_size_arch;
301 print "=" x $max_size_status;
304 printf "%-${max_size_arch}s ", $h_arch;
305 printf "%-${max_size_status}s", $h_status . "\n";
307 print "=" x $max_size_arch;
309 print "=" x $max_size_status;
312 my %arch_table = %{$data{$feat}->{table}};
313 foreach my $arch (sort keys %arch_table) {
314 printf "%-${max_size_arch}s ", $arch;
315 printf "%-${max_size_status}s\n", $arch_table{$arch};
318 print "=" x $max_size_arch;
320 print "=" x $max_size_status;
325 # Output all features for all architectures
328 sub matrix_lines($$) {
350 print $fill x $max_size_name;
352 print $fill x $max_size_kconfig;
354 print $fill x $max_size_description;
356 print $ln_marker x $max_size_arch;
358 print $ln_marker x $max_size_status;
363 my $title = "Feature status on all architectures";
365 print "=" x length($title) . "\n";
367 print "=" x length($title) . "\n\n";
370 foreach my $name (sort {
371 ($data{$a}->{subsys} cmp $data{$b}->{subsys}) or
375 if ($cur_subsys ne $data{$name}->{subsys}) {
376 if ($cur_subsys ne "") {
380 $cur_subsys = $data{$name}->{subsys};
382 my $title = "Subsystem: $cur_subsys";
384 print "=" x length($title) . "\n\n";
387 printf "|%-${max_size_name}s", $h_name;
388 printf "|%-${max_size_kconfig}s", $h_kconfig;
389 printf "|%-${max_size_description}s", $h_description;
391 printf "|%-${max_size_arch}s", $h_arch;
392 printf "|%-${max_size_status}s|\n", $h_status;
397 my %arch_table = %{$data{$name}->{table}};
399 foreach my $arch (sort keys %arch_table) {
401 printf "|%-${max_size_name}s", $name;
402 printf "|%-${max_size_kconfig}s", $data{$name}->{kconfig};
403 printf "|%-${max_size_description}s", $data{$name}->{description};
408 printf "|%-${max_size_name}s", "";
409 printf "|%-${max_size_kconfig}s", "";
410 printf "|%-${max_size_description}s", "";
412 printf "|%-${max_size_arch}s", $arch;
413 printf "|%-${max_size_status}s|\n", $arch_table{$arch};
421 # Parses all feature files located at $prefix dir
423 find({wanted =>\&parse_feat, no_chdir => 1}, $prefix);
425 print STDERR Data::Dumper->Dump([\%data], [qw(*data)]) if ($debug);
428 # Handles the command
430 if ($cmd eq "current") {
431 $arch = qx(uname -m | sed 's/x86_64/x86/' | sed 's/i386/x86/');
435 if ($cmd eq "ls" or $cmd eq "list") {
437 $arch = qx(uname -m | sed 's/x86_64/x86/' | sed 's/i386/x86/');
446 if ($cmd ne "validate") {
460 get_feat.pl - parse the Linux Feature files and produce a ReST book.
464 B<get_feat.pl> [--debug] [--man] [--help] [--dir=<dir>] [--arch=<arch>]
465 [--feature=<feature>|--feat=<feature>] <COMAND> [<ARGUMENT>]
467 Where <COMMAND> can be:
471 B<current> - output table in ReST compatible ASCII format
472 with features for this machine's architecture
474 B<rest> - output table(s) in ReST compatible ASCII format
475 with features in ReST markup language. The output
476 is affected by --arch or --feat/--feature flags.
478 B<validate> - validate the contents of the files under
479 Documentation/features.
481 B<ls> or B<list> - list features for this machine's architecture,
482 using an easier to parse format.
483 The output is affected by --arch flag.
493 Output features for an specific architecture, optionally filtering for
494 a single specific feature.
496 =item B<--feat> or B<--feature>
498 Output features for a single specific feature.
502 Changes the location of the Feature files. By default, it uses
503 the Documentation/features directory.
507 Put the script in verbose mode, useful for debugging. Can be called multiple
508 times, to increase verbosity.
512 Prints a brief help message and exits.
516 Prints the manual page and exits.
522 Parse the Linux feature files from Documentation/features (by default),
523 optionally producing results at ReST format.
525 It supports output data per architecture, per feature or a
526 feature x arch matrix.
528 When used with B<rest> command, it will use either one of the tree formats:
530 If neither B<--arch> or B<--feature> arguments are used, it will output a
531 matrix with features per architecture.
533 If B<--arch> argument is used, it will output the features availability for
534 a given architecture.
536 If B<--feat> argument is used, it will output the content of the feature
537 file using ReStructured Text markup.
541 Report bugs to Mauro Carvalho Chehab <mchehab+samsung@kernel.org>
545 Copyright (c) 2019 by Mauro Carvalho Chehab <mchehab+samsung@kernel.org>.
547 License GPLv2: GNU GPL version 2 <http://gnu.org/licenses/gpl.html>.
549 This is free software: you are free to change and redistribute it.
550 There is NO WARRANTY, to the extent permitted by law.