scripts: get_abi.pl: prevent duplicated file names
[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;
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) {
303 $len = length($name) if (length($name) > $len);
304 }
305
306 print "What:\n\n";
307
308 print "+-" . "-" x $len . "-+\n";
309 foreach my $name (@names) {
310 printf "| %s", $name . " " x ($len - length($name)) . " |\n";
311 print "+-" . "-" x $len . "-+\n";
312 }
45f96517 313
c7ba3334
MCC
314 print "\n";
315 }
316
317 for (my $i = 0; $i < scalar(@filepath); $i++) {
318 my $path = $filepath[$i];
319 my $f = $file[$i];
320
321 $path =~ s,.*/(.*/.*),$1,;;
322 $path =~ s,[/\-],_,g;;
323 my $fileref = "abi_file_".$path;
324
325 if ($type eq "File") {
c7ba3334 326 print ".. _$fileref:\n\n";
c7ba3334
MCC
327 } else {
328 print "Defined on file :ref:`$f <$fileref>`\n\n";
329 }
234948bf 330 }
bbc249f2 331
a4ea67bc
MCC
332 if ($type eq "File") {
333 my $bar = $w;
334 $bar =~ s/./-/g;
335 print "$w\n$bar\n\n";
336 }
337
234948bf
MCC
338 my $desc = "";
339 $desc = $data{$what}->{description} if (defined($data{$what}->{description}));
340 $desc =~ s/\s+$/\n/;
bbc249f2 341
4e6a6234 342 if (!($desc =~ /^\s*$/)) {
11ce90a4
MCC
343 if ($description_is_rst) {
344 print "$desc\n\n";
4e6a6234 345 } else {
11ce90a4 346 $desc =~ s/^\s+//;
bbc249f2 347
11ce90a4
MCC
348 # Remove title markups from the description, as they won't work
349 $desc =~ s/\n[\-\*\=\^\~]+\n/\n\n/g;
350
351 if ($desc =~ m/\:\n/ || $desc =~ m/\n[\t ]+/ || $desc =~ m/[\x00-\x08\x0b-\x1f\x7b-\xff]/) {
352 # put everything inside a code block
353 $desc =~ s/\n/\n /g;
354
355 print "::\n\n";
356 print " $desc\n\n";
357 } else {
358 # Escape any special chars from description
359 $desc =~s/([\x00-\x08\x0b-\x1f\x21-\x2a\x2d\x2f\x3c-\x40\x5c\x5e-\x60\x7b-\xff])/\\$1/g;
360 print "$desc\n\n";
361 }
4e6a6234 362 }
bbc249f2 363 } else {
d0ebaf51 364 print "DESCRIPTION MISSING for $what\n\n" if (!$data{$what}->{is_file});
bbc249f2 365 }
6619c661 366
234948bf 367 if ($data{$what}->{symbols}) {
d0ebaf51
MCC
368 printf "Has the following ABI:\n\n";
369
234948bf 370 foreach my $content(@{$data{$what}->{symbols}}) {
c7ba3334 371 my $label = $data{$symbols{$content}->{xref}}->{label};
d0ebaf51
MCC
372
373 # Escape special chars from content
374 $content =~s/([\x00-\x1f\x21-\x2f\x3a-\x40\x7b-\xff])/\\$1/g;
375
376 print "- :ref:`$content <$label>`\n\n";
377 }
378 }
a16ab14e
MCC
379
380 if (defined($data{$what}->{users})) {
381 my $users = $data{$what}->{users};
382
383 $users =~ s/\n/\n\t/g;
384 printf "Users:\n\t%s\n\n", $users if ($users ne "");
385 }
386
bbc249f2
MCC
387 }
388}
389
33e3e991
MCC
390#
391# Searches for ABI symbols
392#
393sub search_symbols {
394 foreach my $what (sort keys %data) {
395 next if (!($what =~ m/($arg)/));
396
397 my $type = $data{$what}->{type};
398 next if ($type eq "File");
399
400 my $file = $data{$what}->{filepath};
401
402 my $bar = $what;
403 $bar =~ s/./-/g;
404
405 print "\n$what\n$bar\n\n";
406
234948bf
MCC
407 my $kernelversion = $data{$what}->{kernelversion} if (defined($data{$what}->{kernelversion}));
408 my $contact = $data{$what}->{contact} if (defined($data{$what}->{contact}));
409 my $users = $data{$what}->{users} if (defined($data{$what}->{users}));
410 my $date = $data{$what}->{date} if (defined($data{$what}->{date}));
411 my $desc = $data{$what}->{description} if (defined($data{$what}->{description}));
412
413 $kernelversion =~ s/^\s+// if ($kernelversion);
414 $contact =~ s/^\s+// if ($contact);
415 if ($users) {
416 $users =~ s/^\s+//;
417 $users =~ s/\n//g;
418 }
419 $date =~ s/^\s+// if ($date);
420 $desc =~ s/^\s+// if ($desc);
33e3e991
MCC
421
422 printf "Kernel version:\t\t%s\n", $kernelversion if ($kernelversion);
423 printf "Date:\t\t\t%s\n", $date if ($date);
424 printf "Contact:\t\t%s\n", $contact if ($contact);
425 printf "Users:\t\t\t%s\n", $users if ($users);
c7ba3334 426 print "Defined on file(s):\t$file\n\n";
33e3e991
MCC
427 print "Description:\n\n$desc";
428 }
429}
430
61439c4a
MCC
431# Ensure that the prefix will always end with a slash
432# While this is not needed for find, it makes the patch nicer
433# with --enable-lineno
434$prefix =~ s,/?$,/,;
33e3e991 435
bbc249f2
MCC
436#
437# Parses all ABI files located at $prefix dir
438#
439find({wanted =>\&parse_abi, no_chdir => 1}, $prefix);
440
441print STDERR Data::Dumper->Dump([\%data], [qw(*data)]) if ($debug);
442
443#
33e3e991 444# Handles the command
bbc249f2 445#
c7ba3334 446if ($cmd eq "search") {
33e3e991 447 search_symbols;
c7ba3334
MCC
448} else {
449 if ($cmd eq "rest") {
450 output_rest;
451 }
452
453 # Warn about duplicated ABI entries
454 foreach my $what(sort keys %symbols) {
455 my @files = @{$symbols{$what}->{file}};
456
457 next if (scalar(@files) == 1);
bbc249f2 458
c7ba3334
MCC
459 printf STDERR "Warning: $what is defined %d times: @files\n",
460 scalar(@files);
461 }
462}
bbc249f2
MCC
463
464__END__
465
466=head1 NAME
467
468abi_book.pl - parse the Linux ABI files and produce a ReST book.
469
470=head1 SYNOPSIS
471
61439c4a
MCC
472B<abi_book.pl> [--debug] [--enable-lineno] [--man] [--help]
473 [--(no-)rst-source] [--dir=<dir>] <COMAND> [<ARGUMENT>]
33e3e991
MCC
474
475Where <COMMAND> can be:
476
477=over 8
478
479B<search> [SEARCH_REGEX] - search for [SEARCH_REGEX] inside ABI
480
7ce7b89b
MCC
481B<rest> - output the ABI in ReST markup language
482
483B<validate> - validate the ABI contents
33e3e991
MCC
484
485=back
bbc249f2
MCC
486
487=head1 OPTIONS
488
489=over 8
490
33e3e991
MCC
491=item B<--dir>
492
493Changes the location of the ABI search. By default, it uses
494the Documentation/ABI directory.
495
11ce90a4
MCC
496=item B<--rst-source> and B<--no-rst-source>
497
498The input file may be using ReST syntax or not. Those two options allow
499selecting between a rst-compliant source ABI (--rst-source), or a
500plain text that may be violating ReST spec, so it requres some escaping
501logic (--no-rst-source).
502
61439c4a
MCC
503=item B<--enable-lineno>
504
505Enable output of #define LINENO lines.
506
bbc249f2
MCC
507=item B<--debug>
508
509Put the script in verbose mode, useful for debugging. Can be called multiple
510times, to increase verbosity.
511
512=item B<--help>
513
514Prints a brief help message and exits.
515
516=item B<--man>
517
518Prints the manual page and exits.
519
520=back
521
522=head1 DESCRIPTION
523
33e3e991
MCC
524Parse the Linux ABI files from ABI DIR (usually located at Documentation/ABI),
525allowing to search for ABI symbols or to produce a ReST book containing
526the Linux ABI documentation.
527
528=head1 EXAMPLES
529
530Search for all stable symbols with the word "usb":
531
532=over 8
533
534$ scripts/get_abi.pl search usb --dir Documentation/ABI/stable
535
536=back
537
538Search for all symbols that match the regex expression "usb.*cap":
539
540=over 8
541
542$ scripts/get_abi.pl search usb.*cap
543
544=back
545
546Output all obsoleted symbols in ReST format
547
548=over 8
549
550$ scripts/get_abi.pl rest --dir Documentation/ABI/obsolete
551
552=back
bbc249f2
MCC
553
554=head1 BUGS
555
7ce7b89b 556Report bugs to Mauro Carvalho Chehab <mchehab+samsung@kernel.org>
bbc249f2
MCC
557
558=head1 COPYRIGHT
559
7ce7b89b 560Copyright (c) 2016-2019 by Mauro Carvalho Chehab <mchehab+samsung@kernel.org>.
bbc249f2
MCC
561
562License GPLv2: GNU GPL version 2 <http://gnu.org/licenses/gpl.html>.
563
564This is free software: you are free to change and redistribute it.
565There is NO WARRANTY, to the extent permitted by law.
566
567=cut