2 # SPDX-License-Identifier: GPL-2.0
17 my $basename = abs_path($0);
18 $basename =~ s,/[^/]+$,/,;
20 my $prefix=$basename . "../Documentation/features";
22 # Used only at for full features output. The script will auto-adjust
23 # such values for the minimal possible values
25 my $description_size = 1;
28 "debug|d+" => \$debug,
33 'feature=s' => \$feat,
37 pod2usage(1) if $help;
38 pod2usage(-exitstatus => 0, -verbose => 2) if $man;
40 pod2usage(1) if (scalar @ARGV < 1 || @ARGV > 2);
42 my ($cmd, $arg) = @ARGV;
44 pod2usage(2) if ($cmd ne "current" && $cmd ne "rest" && $cmd ne "validate"
45 && $cmd ne "ls" && $cmd ne "list");
47 require Data::Dumper if ($debug);
53 # Displays an error message, printing file name and line
55 sub parse_error($$$$) {
56 my ($file, $ln, $msg, $data) = @_;
60 print STDERR "Warning: file $file#$ln:\n\t$msg";
63 print STDERR ". Line\n\t\t$data";
70 # Parse a features file, storing its contents at %data
73 my $h_name = "Feature";
74 my $h_kconfig = "Kconfig";
75 my $h_description = "Description";
76 my $h_subsys = "Subsystem";
77 my $h_status = "Status";
78 my $h_arch = "Architecture";
80 my $max_size_name = length($h_name);
81 my $max_size_kconfig = length($h_kconfig);
82 my $max_size_description = length($h_description);
83 my $max_size_subsys = length($h_subsys);
84 my $max_size_status = length($h_status);
86 my $max_size_arch = 0;
87 my $max_size_arch_with_header;
88 my $max_description_word = 0;
91 my $file = $File::Find::name;
93 my $mode = (stat($file))[2];
94 return if ($mode & S_IFDIR);
95 return if ($file =~ m,($prefix)/arch-support.txt,);
96 return if (!($file =~ m,arch-support.txt$,));
99 $subsys = $2 if ( m,.*($prefix)/([^/]+).*,);
101 if (length($subsys) > $max_size_subsys) {
102 $max_size_subsys = length($subsys);
113 print STDERR "Opening $file\n" if ($debug > 1);
119 if (m/^\#\s+Feature\s+name:\s*(.*\S)/) {
121 if (length($name) > $max_size_name) {
122 $max_size_name = length($name);
126 if (m/^\#\s+Kconfig:\s*(.*\S)/) {
128 if (length($kconfig) > $max_size_kconfig) {
129 $max_size_kconfig = length($kconfig);
133 if (m/^\#\s+description:\s*(.*\S)/) {
135 if (length($description) > $max_size_description) {
136 $max_size_description = length($description);
139 foreach my $word (split /\s+/, $description) {
140 if (length($word) > $max_description_word) {
141 $max_description_word = length($word);
148 next if (m/^\s*\-+\s*$/);
149 next if (m/^\s*\|\s*arch\s*\|\s*status\s*\|\s*$/);
155 if (m/^\s*\|\s*(\S+):\s*\|\s*(\S+)\s*\|\s*$/) {
159 if (length($status) > $max_size_status) {
160 $max_size_status = length($status);
162 if (length($a) > $max_size_arch) {
163 $max_size_arch = length($a);
166 $status = "---" if ($status =~ m/^\.\.$/);
169 $arch_table{$a} = $status;
173 #Everything else is an error
174 parse_error($file, $ln, "line is invalid", $_);
179 parse_error($file, $ln, "Feature name not found", "");
183 parse_error($file, $ln, "Subsystem not found", "") if (!$subsys);
184 parse_error($file, $ln, "Kconfig not found", "") if (!$kconfig);
185 parse_error($file, $ln, "Description not found", "") if (!$description);
188 parse_error($file, $ln, "Architecture table not found", "");
192 $data{$name}->{where} = $file;
193 $data{$name}->{subsys} = $subsys;
194 $data{$name}->{kconfig} = $kconfig;
195 $data{$name}->{description} = $description;
196 $data{$name}->{comments} = $comments;
197 $data{$name}->{table} = \%arch_table;
199 $max_size_arch_with_header = $max_size_arch + length($h_arch);
203 # Output feature(s) for a given architecture
205 sub output_arch_table {
206 my $title = "Feature status on $arch architecture";
208 print "=" x length($title) . "\n";
210 print "=" x length($title) . "\n\n";
212 print "=" x $max_size_subsys;
214 print "=" x $max_size_name;
216 print "=" x $max_size_kconfig;
218 print "=" x $max_size_status;
220 print "=" x $max_size_description;
222 printf "%-${max_size_subsys}s ", $h_subsys;
223 printf "%-${max_size_name}s ", $h_name;
224 printf "%-${max_size_kconfig}s ", $h_kconfig;
225 printf "%-${max_size_status}s ", $h_status;
226 printf "%-${max_size_description}s\n", $h_description;
227 print "=" x $max_size_subsys;
229 print "=" x $max_size_name;
231 print "=" x $max_size_kconfig;
233 print "=" x $max_size_status;
235 print "=" x $max_size_description;
238 foreach my $name (sort {
239 ($data{$a}->{subsys} cmp $data{$b}->{subsys}) ||
242 next if ($feat && $name ne $feat);
244 my %arch_table = %{$data{$name}->{table}};
245 printf "%-${max_size_subsys}s ", $data{$name}->{subsys};
246 printf "%-${max_size_name}s ", $name;
247 printf "%-${max_size_kconfig}s ", $data{$name}->{kconfig};
248 printf "%-${max_size_status}s ", $arch_table{$arch};
249 printf "%-s\n", $data{$name}->{description};
252 print "=" x $max_size_subsys;
254 print "=" x $max_size_name;
256 print "=" x $max_size_kconfig;
258 print "=" x $max_size_status;
260 print "=" x $max_size_description;
265 # list feature(s) for a given architecture
267 sub list_arch_features {
268 print "#\n# Kernel feature support matrix of the '$arch' architecture:\n#\n";
270 foreach my $name (sort {
271 ($data{$a}->{subsys} cmp $data{$b}->{subsys}) ||
274 next if ($feat && $name ne $feat);
276 my %arch_table = %{$data{$name}->{table}};
278 my $status = $arch_table{$arch};
279 $status = " " x ((4 - length($status)) / 2) . $status;
281 printf " %${max_size_subsys}s/ ", $data{$name}->{subsys};
282 printf "%-${max_size_name}s: ", $name;
283 printf "%-5s| ", $status;
284 printf "%${max_size_kconfig}s # ", $data{$name}->{kconfig};
285 printf " %s\n", $data{$name}->{description};
290 # Output a feature on all architectures
293 my $title = "Feature $feat";
295 print "=" x length($title) . "\n";
297 print "=" x length($title) . "\n\n";
299 print ":Subsystem: $data{$feat}->{subsys} \n" if ($data{$feat}->{subsys});
300 print ":Kconfig: $data{$feat}->{kconfig} \n" if ($data{$feat}->{kconfig});
302 my $desc = $data{$feat}->{description};
303 $desc =~ s/^([a-z])/\U$1/;
305 print "\n$desc.\n\n";
307 my $com = $data{$feat}->{comments};
312 print "--------\n\n";
316 print "=" x $max_size_arch_with_header;
318 print "=" x $max_size_status;
321 printf "%-${max_size_arch}s ", $h_arch;
322 printf "%-${max_size_status}s", $h_status . "\n";
324 print "=" x $max_size_arch_with_header;
326 print "=" x $max_size_status;
329 my %arch_table = %{$data{$feat}->{table}};
330 foreach my $arch (sort keys %arch_table) {
331 printf "%-${max_size_arch}s ", $arch;
332 printf "%-${max_size_status}s\n", $arch_table{$arch};
335 print "=" x $max_size_arch_with_header;
337 print "=" x $max_size_status;
342 # Output all features for all architectures
345 sub matrix_lines($$$) {
346 my $desc_size = shift;
347 my $status_size = shift;
361 print $fill x $max_size_name;
363 print $fill x $desc_size;
365 print $ln_marker x $status_size;
370 my $title = "Feature status on all architectures";
371 my $notcompat = "Not compatible";
373 print "=" x length($title) . "\n";
375 print "=" x length($title) . "\n\n";
377 my $desc_title = "$h_kconfig / $h_description";
379 my $desc_size = $max_size_kconfig + 4;
380 if (!$description_size) {
381 $desc_size = $max_size_description if ($max_size_description > $desc_size);
383 $desc_size = $description_size if ($description_size > $desc_size);
385 $desc_size = $max_description_word if ($max_description_word > $desc_size);
387 $desc_size = length($desc_title) if (length($desc_title) > $desc_size);
389 $max_size_status = length($notcompat) if (length($notcompat) > $max_size_status);
391 # Ensure that the status will fit
392 my $min_status_size = $max_size_status + $max_size_arch + 6;
393 $status_size = $min_status_size if ($status_size < $min_status_size);
397 foreach my $name (sort {
398 ($data{$a}->{subsys} cmp $data{$b}->{subsys}) or
402 if ($cur_subsys ne $data{$name}->{subsys}) {
403 if ($cur_subsys ne "") {
407 $cur_subsys = $data{$name}->{subsys};
409 my $title = "Subsystem: $cur_subsys";
411 print "=" x length($title) . "\n\n";
414 matrix_lines($desc_size, $status_size, 0);
416 printf "|%-${max_size_name}s", $h_name;
417 printf "|%-${desc_size}s", $desc_title;
419 printf "|%-${status_size}s|\n", "Status per architecture";
420 matrix_lines($desc_size, $status_size, 1);
423 my %arch_table = %{$data{$name}->{table}};
428 foreach my $arch (sort {
429 ($arch_table{$b} cmp $arch_table{$a}) or
431 } keys %arch_table) {
433 my $status = $arch_table{$arch};
435 if ($status eq "---") {
436 $status = $notcompat;
439 if ($status ne $cur_status) {
444 $line = "- **" . $status . "**: " . $arch;
445 } elsif (length($line) + length ($arch) + 2 < $status_size) {
446 $line .= ", " . $arch;
451 $cur_status = $status;
453 push @lines, $line if ($line ne "");
455 my $description = $data{$name}->{description};
456 while (length($description) > $desc_size) {
457 my $d = substr $description, 0, $desc_size;
459 # Ensure that it will end on a space
460 # if it can't, it means that the size is too small
461 # Instead of aborting it, let's print what we have
462 if (!($d =~ s/^(.*)\s+.*/$1/)) {
463 $d = substr $d, 0, -1;
465 $description =~ s/^\Q$d\E//;
468 $description =~ s/^\Q$d\E\s+//;
471 push @descs, $description;
473 # Ensure that the full description will be printed
474 push @lines, "" while (scalar(@lines) < 2 + scalar(@descs));
477 for my $line(@lines) {
479 printf "|%-${max_size_name}s", $name;
480 printf "|%-${desc_size}s", "``" . $data{$name}->{kconfig} . "``";
481 } elsif ($ln >= 2 && scalar(@descs)) {
482 printf "|%-${max_size_name}s", "";
483 printf "|%-${desc_size}s", shift @descs;
485 printf "|%-${max_size_name}s", "";
486 printf "|%-${desc_size}s", "";
489 printf "|%-${status_size}s|\n", $line;
493 matrix_lines($desc_size, $status_size, 0);
499 # Parses all feature files located at $prefix dir
501 find({wanted =>\&parse_feat, no_chdir => 1}, $prefix);
503 print STDERR Data::Dumper->Dump([\%data], [qw(*data)]) if ($debug);
506 # Handles the command
508 if ($cmd eq "current") {
509 $arch = qx(uname -m | sed 's/x86_64/x86/' | sed 's/i386/x86/');
513 if ($cmd eq "ls" or $cmd eq "list") {
515 $arch = qx(uname -m | sed 's/x86_64/x86/' | sed 's/i386/x86/');
524 if ($cmd ne "validate") {
538 get_feat.pl - parse the Linux Feature files and produce a ReST book.
542 B<get_feat.pl> [--debug] [--man] [--help] [--dir=<dir>] [--arch=<arch>]
543 [--feature=<feature>|--feat=<feature>] <COMAND> [<ARGUMENT>]
545 Where <COMMAND> can be:
549 B<current> - output table in ReST compatible ASCII format
550 with features for this machine's architecture
552 B<rest> - output table(s) in ReST compatible ASCII format
553 with features in ReST markup language. The output
554 is affected by --arch or --feat/--feature flags.
556 B<validate> - validate the contents of the files under
557 Documentation/features.
559 B<ls> or B<list> - list features for this machine's architecture,
560 using an easier to parse format.
561 The output is affected by --arch flag.
571 Output features for an specific architecture, optionally filtering for
572 a single specific feature.
574 =item B<--feat> or B<--feature>
576 Output features for a single specific feature.
580 Changes the location of the Feature files. By default, it uses
581 the Documentation/features directory.
585 Put the script in verbose mode, useful for debugging. Can be called multiple
586 times, to increase verbosity.
590 Prints a brief help message and exits.
594 Prints the manual page and exits.
600 Parse the Linux feature files from Documentation/features (by default),
601 optionally producing results at ReST format.
603 It supports output data per architecture, per feature or a
604 feature x arch matrix.
606 When used with B<rest> command, it will use either one of the tree formats:
608 If neither B<--arch> or B<--feature> arguments are used, it will output a
609 matrix with features per architecture.
611 If B<--arch> argument is used, it will output the features availability for
612 a given architecture.
614 If B<--feat> argument is used, it will output the content of the feature
615 file using ReStructured Text markup.
619 Report bugs to Mauro Carvalho Chehab <mchehab+samsung@kernel.org>
623 Copyright (c) 2019 by Mauro Carvalho Chehab <mchehab+samsung@kernel.org>.
625 License GPLv2: GNU GPL version 2 <http://gnu.org/licenses/gpl.html>.
627 This is free software: you are free to change and redistribute it.
628 There is NO WARRANTY, to the extent permitted by law.