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