]> git.ipfire.org Git - thirdparty/linux.git/blame - scripts/get_abi.pl
scripts: get_abi.pl: use bold font for ABI definitions
[thirdparty/linux.git] / scripts / get_abi.pl
CommitLineData
bbc249f2 1#!/usr/bin/perl
ecb351f1 2# SPDX-License-Identifier: GPL-2.0
bbc249f2
MCC
3
4use strict;
234948bf 5use warnings;
bbc249f2
MCC
6use Pod::Usage;
7use Getopt::Long;
8use File::Find;
9use Fcntl ':mode';
10
234948bf
MCC
11my $help = 0;
12my $man = 0;
13my $debug = 0;
14my $enable_lineno = 0;
33e3e991 15my $prefix="Documentation/ABI";
bbc249f2 16
11ce90a4
MCC
17#
18# If true, assumes that the description is formatted with ReST
19#
20my $description_is_rst = 0;
21
bbc249f2
MCC
22GetOptions(
23 "debug|d+" => \$debug,
61439c4a 24 "enable-lineno" => \$enable_lineno,
11ce90a4 25 "rst-source!" => \$description_is_rst,
33e3e991 26 "dir=s" => \$prefix,
bbc249f2
MCC
27 'help|?' => \$help,
28 man => \$man
29) or pod2usage(2);
30
31pod2usage(1) if $help;
32pod2usage(-exitstatus => 0, -verbose => 2) if $man;
33
33e3e991 34pod2usage(2) if (scalar @ARGV < 1 || @ARGV > 2);
bbc249f2 35
33e3e991
MCC
36my ($cmd, $arg) = @ARGV;
37
7ce7b89b 38pod2usage(2) if ($cmd ne "search" && $cmd ne "rest" && $cmd ne "validate");
33e3e991 39pod2usage(2) if ($cmd eq "search" && !$arg);
bbc249f2
MCC
40
41require Data::Dumper if ($debug);
42
43my %data;
234948bf 44my %symbols;
bbc249f2
MCC
45
46#
47# Displays an error message, printing file name and line
48#
49sub parse_error($$$$) {
50 my ($file, $ln, $msg, $data) = @_;
51
52 print STDERR "file $file#$ln: $msg at\n\t$data";
53}
54
55#
56# Parse an ABI file, storing its contents at %data
57#
58sub parse_abi {
59 my $file = $File::Find::name;
60
61 my $mode = (stat($file))[2];
62 return if ($mode & S_IFDIR);
63 return if ($file =~ m,/README,);
64
65 my $name = $file;
66 $name =~ s,.*/,,;
67
a4ea67bc
MCC
68 my $fn = $file;
69 $fn =~ s,Documentation/ABI/,,;
70
71 my $nametag = "File $fn";
d0ebaf51
MCC
72 $data{$nametag}->{what} = "File $name";
73 $data{$nametag}->{type} = "File";
74 $data{$nametag}->{file} = $name;
33e3e991 75 $data{$nametag}->{filepath} = $file;
d0ebaf51 76 $data{$nametag}->{is_file} = 1;
61439c4a 77 $data{$nametag}->{line_no} = 1;
d0ebaf51 78
bbc249f2
MCC
79 my $type = $file;
80 $type =~ s,.*/(.*)/.*,$1,;
81
82 my $what;
83 my $new_what;
234948bf 84 my $tag = "";
bbc249f2 85 my $ln;
6619c661 86 my $xrefs;
4e6a6234 87 my $space;
d0ebaf51 88 my @labels;
234948bf 89 my $label = "";
bbc249f2
MCC
90
91 print STDERR "Opening $file\n" if ($debug > 1);
92 open IN, $file;
93 while(<IN>) {
94 $ln++;
4e6a6234 95 if (m/^(\S+)(:\s*)(.*)/i) {
bbc249f2 96 my $new_tag = lc($1);
4e6a6234
MCC
97 my $sep = $2;
98 my $content = $3;
bbc249f2 99
7ce7b89b 100 if (!($new_tag =~ m/(what|where|date|kernelversion|contact|description|users)/)) {
bbc249f2 101 if ($tag eq "description") {
4e6a6234
MCC
102 # New "tag" is actually part of
103 # description. Don't consider it a tag
104 $new_tag = "";
7d7ea8d2 105 } elsif ($tag ne "") {
bbc249f2
MCC
106 parse_error($file, $ln, "tag '$tag' is invalid", $_);
107 }
108 }
109
2c0700e7
MCC
110 # Invalid, but it is a common mistake
111 if ($new_tag eq "where") {
112 parse_error($file, $ln, "tag 'Where' is invalid. Should be 'What:' instead", $_);
113 $new_tag = "what";
114 }
115
bbc249f2 116 if ($new_tag =~ m/what/) {
4e6a6234 117 $space = "";
234948bf
MCC
118 $content =~ s/[,.;]$//;
119
c7ba3334
MCC
120 push @{$symbols{$content}->{file}}, " $file:" . ($ln - 1);
121
bbc249f2
MCC
122 if ($tag =~ m/what/) {
123 $what .= ", " . $content;
124 } else {
234948bf
MCC
125 if ($what) {
126 parse_error($file, $ln, "What '$what' doesn't have a description", "") if (!$data{$what}->{description});
127
128 foreach my $w(split /, /, $what) {
c7ba3334 129 $symbols{$w}->{xref} = $what;
234948bf
MCC
130 };
131 }
4e6a6234 132
bbc249f2 133 $what = $content;
d0ebaf51 134 $label = $content;
bbc249f2
MCC
135 $new_what = 1;
136 }
d0ebaf51 137 push @labels, [($content, $label)];
bbc249f2 138 $tag = $new_tag;
6619c661 139
234948bf 140 push @{$data{$nametag}->{symbols}}, $content if ($data{$nametag}->{what});
bbc249f2
MCC
141 next;
142 }
143
7d7ea8d2 144 if ($tag ne "" && $new_tag) {
4e6a6234 145 $tag = $new_tag;
bbc249f2 146
4e6a6234 147 if ($new_what) {
234948bf 148 @{$data{$what}->{label_list}} = @labels if ($data{$nametag}->{what});
d0ebaf51
MCC
149 @labels = ();
150 $label = "";
4e6a6234 151 $new_what = 0;
bbc249f2 152
4e6a6234 153 $data{$what}->{type} = $type;
c7ba3334
MCC
154 if (!defined($data{$what}->{file})) {
155 $data{$what}->{file} = $name;
156 $data{$what}->{filepath} = $file;
157 } else {
158 if ($name ne $data{$what}->{file}) {
159 $data{$what}->{file} .= " " . $name;
160 $data{$what}->{filepath} .= " " . $file;
161 }
162 }
4e6a6234 163 print STDERR "\twhat: $what\n" if ($debug > 1);
c7ba3334
MCC
164 $data{$what}->{line_no} = $ln;
165 } else {
166 $data{$what}->{line_no} = $ln if (!defined($data{$what}->{line_no}));
4e6a6234 167 }
bbc249f2 168
4e6a6234
MCC
169 if (!$what) {
170 parse_error($file, $ln, "'What:' should come first:", $_);
171 next;
172 }
f82a8a74
MCC
173 if ($new_tag eq "description") {
174 $sep =~ s,:, ,;
11ce90a4 175 $content = ' ' x length($new_tag) . $sep . $content;
f82a8a74
MCC
176 while ($content =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e) {}
177 if ($content =~ m/^(\s*)(\S.*)$/) {
178 # Preserve initial spaces for the first line
11ce90a4 179 $space = $1;
f82a8a74
MCC
180 $content = "$2\n";
181 $data{$what}->{$tag} .= $content;
182 } else {
183 undef($space);
4e6a6234 184 }
e9bca891 185
4e6a6234
MCC
186 } else {
187 $data{$what}->{$tag} = $content;
188 }
bbc249f2
MCC
189 next;
190 }
bbc249f2
MCC
191 }
192
4e6a6234 193 # Store any contents before tags at the database
d0ebaf51
MCC
194 if (!$tag && $data{$nametag}->{what}) {
195 $data{$nametag}->{description} .= $_;
6619c661
MCC
196 next;
197 }
bbc249f2 198
4e6a6234 199 if ($tag eq "description") {
e9bca891
MCC
200 my $content = $_;
201 while ($content =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e) {}
f82a8a74
MCC
202 if (m/^\s*\n/) {
203 $data{$what}->{$tag} .= "\n";
204 next;
205 }
206
207 if (!defined($space)) {
e9bca891 208 # Preserve initial spaces for the first line
f82a8a74 209 if ($content =~ m/^(\s*)(\S.*)$/) {
e9bca891 210 $space = $1;
f82a8a74 211 $content = "$2\n";
4e6a6234
MCC
212 }
213 } else {
4e6a6234 214 $space = "" if (!($content =~ s/^($space)//));
4e6a6234 215 }
f82a8a74
MCC
216 $data{$what}->{$tag} .= $content;
217
4e6a6234
MCC
218 next;
219 }
bbc249f2
MCC
220 if (m/^\s*(.*)/) {
221 $data{$what}->{$tag} .= "\n$1";
222 $data{$what}->{$tag} =~ s/\n+$//;
223 next;
224 }
225
226 # Everything else is error
227 parse_error($file, $ln, "Unexpected line:", $_);
228 }
234948bf
MCC
229 $data{$nametag}->{description} =~ s/^\n+// if ($data{$nametag}->{description});
230 if ($what) {
231 parse_error($file, $ln, "What '$what' doesn't have a description", "") if (!$data{$what}->{description});
232
233 foreach my $w(split /, /,$what) {
c7ba3334 234 $symbols{$w}->{xref} = $what;
234948bf
MCC
235 };
236 }
bbc249f2
MCC
237 close IN;
238}
239
234948bf
MCC
240sub create_labels {
241 my %labels;
bbc249f2 242
234948bf
MCC
243 foreach my $what (keys %data) {
244 next if ($data{$what}->{file} eq "File");
4e6a6234 245
234948bf 246 foreach my $p (@{$data{$what}->{label_list}}) {
d0ebaf51
MCC
247 my ($content, $label) = @{$p};
248 $label = "abi_" . $label . " ";
249 $label =~ tr/A-Z/a-z/;
250
251 # Convert special chars to "_"
252 $label =~s/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\xff])/_/g;
253 $label =~ s,_+,_,g;
254 $label =~ s,_$,,;
255
2e7ce055
MCC
256 # Avoid duplicated labels
257 while (defined($labels{$label})) {
258 my @chars = ("A".."Z", "a".."z");
259 $label .= $chars[rand @chars];
260 }
261 $labels{$label} = 1;
262
234948bf 263 $data{$what}->{label} = $label;
d0ebaf51
MCC
264
265 # only one label is enough
266 last;
6619c661 267 }
234948bf
MCC
268 }
269}
270
271#
272# Outputs the book on ReST format
273#
274
275sub output_rest {
276 create_labels();
277
278 foreach my $what (sort {
279 ($data{$a}->{type} eq "File") cmp ($data{$b}->{type} eq "File") ||
280 $a cmp $b
281 } keys %data) {
282 my $type = $data{$what}->{type};
c7ba3334
MCC
283
284 my @file = split / /, $data{$what}->{file};
285 my @filepath = split / /, $data{$what}->{filepath};
234948bf
MCC
286
287 if ($enable_lineno) {
288 printf "#define LINENO %s%s#%s\n\n",
c7ba3334 289 $prefix, $file[0],
234948bf
MCC
290 $data{$what}->{line_no};
291 }
6619c661 292
234948bf
MCC
293 my $w = $what;
294 $w =~ s/([\(\)\_\-\*\=\^\~\\])/\\$1/g;
6619c661 295
c7ba3334 296 if ($type ne "File") {
234948bf 297 printf ".. _%s:\n\n", $data{$what}->{label};
45f96517 298
234948bf 299 my @names = split /, /,$w;
45f96517
MCC
300 my $len = 0;
301
302 foreach my $name (@names) {
c01d62d3 303 $name = "**$name**";
45f96517
MCC
304 $len = length($name) if (length($name) > $len);
305 }
306
307 print "What:\n\n";
308
309 print "+-" . "-" x $len . "-+\n";
310 foreach my $name (@names) {
311 printf "| %s", $name . " " x ($len - length($name)) . " |\n";
312 print "+-" . "-" x $len . "-+\n";
313 }
45f96517 314
c7ba3334
MCC
315 print "\n";
316 }
317
318 for (my $i = 0; $i < scalar(@filepath); $i++) {
319 my $path = $filepath[$i];
320 my $f = $file[$i];
321
322 $path =~ s,.*/(.*/.*),$1,;;
323 $path =~ s,[/\-],_,g;;
324 my $fileref = "abi_file_".$path;
325
326 if ($type eq "File") {
c7ba3334 327 print ".. _$fileref:\n\n";
c7ba3334
MCC
328 } else {
329 print "Defined on file :ref:`$f <$fileref>`\n\n";
330 }
234948bf 331 }
bbc249f2 332
a4ea67bc
MCC
333 if ($type eq "File") {
334 my $bar = $w;
335 $bar =~ s/./-/g;
336 print "$w\n$bar\n\n";
337 }
338
234948bf
MCC
339 my $desc = "";
340 $desc = $data{$what}->{description} if (defined($data{$what}->{description}));
341 $desc =~ s/\s+$/\n/;
bbc249f2 342
4e6a6234 343 if (!($desc =~ /^\s*$/)) {
11ce90a4
MCC
344 if ($description_is_rst) {
345 print "$desc\n\n";
4e6a6234 346 } else {
11ce90a4 347 $desc =~ s/^\s+//;
bbc249f2 348
11ce90a4
MCC
349 # Remove title markups from the description, as they won't work
350 $desc =~ s/\n[\-\*\=\^\~]+\n/\n\n/g;
351
352 if ($desc =~ m/\:\n/ || $desc =~ m/\n[\t ]+/ || $desc =~ m/[\x00-\x08\x0b-\x1f\x7b-\xff]/) {
353 # put everything inside a code block
354 $desc =~ s/\n/\n /g;
355
356 print "::\n\n";
357 print " $desc\n\n";
358 } else {
359 # Escape any special chars from description
360 $desc =~s/([\x00-\x08\x0b-\x1f\x21-\x2a\x2d\x2f\x3c-\x40\x5c\x5e-\x60\x7b-\xff])/\\$1/g;
361 print "$desc\n\n";
362 }
4e6a6234 363 }
bbc249f2 364 } else {
d0ebaf51 365 print "DESCRIPTION MISSING for $what\n\n" if (!$data{$what}->{is_file});
bbc249f2 366 }
6619c661 367
234948bf 368 if ($data{$what}->{symbols}) {
d0ebaf51
MCC
369 printf "Has the following ABI:\n\n";
370
234948bf 371 foreach my $content(@{$data{$what}->{symbols}}) {
c7ba3334 372 my $label = $data{$symbols{$content}->{xref}}->{label};
d0ebaf51
MCC
373
374 # Escape special chars from content
375 $content =~s/([\x00-\x1f\x21-\x2f\x3a-\x40\x7b-\xff])/\\$1/g;
376
377 print "- :ref:`$content <$label>`\n\n";
378 }
379 }
a16ab14e
MCC
380
381 if (defined($data{$what}->{users})) {
382 my $users = $data{$what}->{users};
383
384 $users =~ s/\n/\n\t/g;
385 printf "Users:\n\t%s\n\n", $users if ($users ne "");
386 }
387
bbc249f2
MCC
388 }
389}
390
33e3e991
MCC
391#
392# Searches for ABI symbols
393#
394sub search_symbols {
395 foreach my $what (sort keys %data) {
396 next if (!($what =~ m/($arg)/));
397
398 my $type = $data{$what}->{type};
399 next if ($type eq "File");
400
401 my $file = $data{$what}->{filepath};
402
403 my $bar = $what;
404 $bar =~ s/./-/g;
405
406 print "\n$what\n$bar\n\n";
407
234948bf
MCC
408 my $kernelversion = $data{$what}->{kernelversion} if (defined($data{$what}->{kernelversion}));
409 my $contact = $data{$what}->{contact} if (defined($data{$what}->{contact}));
410 my $users = $data{$what}->{users} if (defined($data{$what}->{users}));
411 my $date = $data{$what}->{date} if (defined($data{$what}->{date}));
412 my $desc = $data{$what}->{description} if (defined($data{$what}->{description}));
413
414 $kernelversion =~ s/^\s+// if ($kernelversion);
415 $contact =~ s/^\s+// if ($contact);
416 if ($users) {
417 $users =~ s/^\s+//;
418 $users =~ s/\n//g;
419 }
420 $date =~ s/^\s+// if ($date);
421 $desc =~ s/^\s+// if ($desc);
33e3e991
MCC
422
423 printf "Kernel version:\t\t%s\n", $kernelversion if ($kernelversion);
424 printf "Date:\t\t\t%s\n", $date if ($date);
425 printf "Contact:\t\t%s\n", $contact if ($contact);
426 printf "Users:\t\t\t%s\n", $users if ($users);
c7ba3334 427 print "Defined on file(s):\t$file\n\n";
33e3e991
MCC
428 print "Description:\n\n$desc";
429 }
430}
431
61439c4a
MCC
432# Ensure that the prefix will always end with a slash
433# While this is not needed for find, it makes the patch nicer
434# with --enable-lineno
435$prefix =~ s,/?$,/,;
33e3e991 436
bbc249f2
MCC
437#
438# Parses all ABI files located at $prefix dir
439#
440find({wanted =>\&parse_abi, no_chdir => 1}, $prefix);
441
442print STDERR Data::Dumper->Dump([\%data], [qw(*data)]) if ($debug);
443
444#
33e3e991 445# Handles the command
bbc249f2 446#
c7ba3334 447if ($cmd eq "search") {
33e3e991 448 search_symbols;
c7ba3334
MCC
449} else {
450 if ($cmd eq "rest") {
451 output_rest;
452 }
453
454 # Warn about duplicated ABI entries
455 foreach my $what(sort keys %symbols) {
456 my @files = @{$symbols{$what}->{file}};
457
458 next if (scalar(@files) == 1);
bbc249f2 459
c7ba3334
MCC
460 printf STDERR "Warning: $what is defined %d times: @files\n",
461 scalar(@files);
462 }
463}
bbc249f2
MCC
464
465__END__
466
467=head1 NAME
468
469abi_book.pl - parse the Linux ABI files and produce a ReST book.
470
471=head1 SYNOPSIS
472
61439c4a
MCC
473B<abi_book.pl> [--debug] [--enable-lineno] [--man] [--help]
474 [--(no-)rst-source] [--dir=<dir>] <COMAND> [<ARGUMENT>]
33e3e991
MCC
475
476Where <COMMAND> can be:
477
478=over 8
479
480B<search> [SEARCH_REGEX] - search for [SEARCH_REGEX] inside ABI
481
7ce7b89b
MCC
482B<rest> - output the ABI in ReST markup language
483
484B<validate> - validate the ABI contents
33e3e991
MCC
485
486=back
bbc249f2
MCC
487
488=head1 OPTIONS
489
490=over 8
491
33e3e991
MCC
492=item B<--dir>
493
494Changes the location of the ABI search. By default, it uses
495the Documentation/ABI directory.
496
11ce90a4
MCC
497=item B<--rst-source> and B<--no-rst-source>
498
499The input file may be using ReST syntax or not. Those two options allow
500selecting between a rst-compliant source ABI (--rst-source), or a
501plain text that may be violating ReST spec, so it requres some escaping
502logic (--no-rst-source).
503
61439c4a
MCC
504=item B<--enable-lineno>
505
506Enable output of #define LINENO lines.
507
bbc249f2
MCC
508=item B<--debug>
509
510Put the script in verbose mode, useful for debugging. Can be called multiple
511times, to increase verbosity.
512
513=item B<--help>
514
515Prints a brief help message and exits.
516
517=item B<--man>
518
519Prints the manual page and exits.
520
521=back
522
523=head1 DESCRIPTION
524
33e3e991
MCC
525Parse the Linux ABI files from ABI DIR (usually located at Documentation/ABI),
526allowing to search for ABI symbols or to produce a ReST book containing
527the Linux ABI documentation.
528
529=head1 EXAMPLES
530
531Search for all stable symbols with the word "usb":
532
533=over 8
534
535$ scripts/get_abi.pl search usb --dir Documentation/ABI/stable
536
537=back
538
539Search for all symbols that match the regex expression "usb.*cap":
540
541=over 8
542
543$ scripts/get_abi.pl search usb.*cap
544
545=back
546
547Output all obsoleted symbols in ReST format
548
549=over 8
550
551$ scripts/get_abi.pl rest --dir Documentation/ABI/obsolete
552
553=back
bbc249f2
MCC
554
555=head1 BUGS
556
7ce7b89b 557Report bugs to Mauro Carvalho Chehab <mchehab+samsung@kernel.org>
bbc249f2
MCC
558
559=head1 COPYRIGHT
560
7ce7b89b 561Copyright (c) 2016-2019 by Mauro Carvalho Chehab <mchehab+samsung@kernel.org>.
bbc249f2
MCC
562
563License GPLv2: GNU GPL version 2 <http://gnu.org/licenses/gpl.html>.
564
565This is free software: you are free to change and redistribute it.
566There is NO WARRANTY, to the extent permitted by law.
567
568=cut