]> git.ipfire.org Git - thirdparty/linux.git/blame - scripts/get_abi.pl
scripts: get_abi.pl: stop check loop earlier when regex is found
[thirdparty/linux.git] / scripts / get_abi.pl
CommitLineData
c25ce589 1#!/usr/bin/env 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';
ab02c515 11use Cwd 'abs_path';
46f661fd 12use Data::Dumper;
bbc249f2 13
234948bf 14my $help = 0;
ab02c515 15my $hint = 0;
234948bf
MCC
16my $man = 0;
17my $debug = 0;
18my $enable_lineno = 0;
f090db43 19my $show_warnings = 1;
33e3e991 20my $prefix="Documentation/ABI";
f090db43 21my $sysfs_prefix="/sys";
14c94257 22my $search_string;
bbc249f2 23
46f661fd
MCC
24# Debug options
25my $dbg_what_parsing = 1;
26my $dbg_what_open = 2;
27my $dbg_dump_abi_structs = 4;
28
11ce90a4
MCC
29#
30# If true, assumes that the description is formatted with ReST
31#
2fcce37a 32my $description_is_rst = 1;
11ce90a4 33
bbc249f2 34GetOptions(
46f661fd 35 "debug=i" => \$debug,
61439c4a 36 "enable-lineno" => \$enable_lineno,
11ce90a4 37 "rst-source!" => \$description_is_rst,
33e3e991 38 "dir=s" => \$prefix,
bbc249f2 39 'help|?' => \$help,
ab02c515 40 "show-hints" => \$hint,
14c94257 41 "search-string=s" => \$search_string,
bbc249f2
MCC
42 man => \$man
43) or pod2usage(2);
44
45pod2usage(1) if $help;
46pod2usage(-exitstatus => 0, -verbose => 2) if $man;
47
33e3e991 48pod2usage(2) if (scalar @ARGV < 1 || @ARGV > 2);
bbc249f2 49
33e3e991
MCC
50my ($cmd, $arg) = @ARGV;
51
f090db43 52pod2usage(2) if ($cmd ne "search" && $cmd ne "rest" && $cmd ne "validate" && $cmd ne "undefined");
33e3e991 53pod2usage(2) if ($cmd eq "search" && !$arg);
bbc249f2 54
46f661fd 55require Data::Dumper if ($debug & $dbg_dump_abi_structs);
bbc249f2
MCC
56
57my %data;
234948bf 58my %symbols;
bbc249f2
MCC
59
60#
61# Displays an error message, printing file name and line
62#
63sub parse_error($$$$) {
64 my ($file, $ln, $msg, $data) = @_;
65
f090db43
MCC
66 return if (!$show_warnings);
67
75442fb0
MCC
68 $data =~ s/\s+$/\n/;
69
70 print STDERR "Warning: file $file#$ln:\n\t$msg";
71
72 if ($data ne "") {
73 print STDERR ". Line\n\t\t$data";
74 } else {
75 print STDERR "\n";
76 }
bbc249f2
MCC
77}
78
79#
80# Parse an ABI file, storing its contents at %data
81#
82sub parse_abi {
83 my $file = $File::Find::name;
84
85 my $mode = (stat($file))[2];
86 return if ($mode & S_IFDIR);
87 return if ($file =~ m,/README,);
88
89 my $name = $file;
90 $name =~ s,.*/,,;
91
a4ea67bc
MCC
92 my $fn = $file;
93 $fn =~ s,Documentation/ABI/,,;
94
95 my $nametag = "File $fn";
d0ebaf51
MCC
96 $data{$nametag}->{what} = "File $name";
97 $data{$nametag}->{type} = "File";
98 $data{$nametag}->{file} = $name;
33e3e991 99 $data{$nametag}->{filepath} = $file;
d0ebaf51 100 $data{$nametag}->{is_file} = 1;
61439c4a 101 $data{$nametag}->{line_no} = 1;
d0ebaf51 102
bbc249f2
MCC
103 my $type = $file;
104 $type =~ s,.*/(.*)/.*,$1,;
105
106 my $what;
107 my $new_what;
234948bf 108 my $tag = "";
bbc249f2 109 my $ln;
6619c661 110 my $xrefs;
4e6a6234 111 my $space;
d0ebaf51 112 my @labels;
234948bf 113 my $label = "";
bbc249f2 114
46f661fd 115 print STDERR "Opening $file\n" if ($debug & $dbg_what_open);
bbc249f2
MCC
116 open IN, $file;
117 while(<IN>) {
118 $ln++;
4e6a6234 119 if (m/^(\S+)(:\s*)(.*)/i) {
bbc249f2 120 my $new_tag = lc($1);
4e6a6234
MCC
121 my $sep = $2;
122 my $content = $3;
bbc249f2 123
7ce7b89b 124 if (!($new_tag =~ m/(what|where|date|kernelversion|contact|description|users)/)) {
bbc249f2 125 if ($tag eq "description") {
4e6a6234
MCC
126 # New "tag" is actually part of
127 # description. Don't consider it a tag
128 $new_tag = "";
7d7ea8d2 129 } elsif ($tag ne "") {
bbc249f2
MCC
130 parse_error($file, $ln, "tag '$tag' is invalid", $_);
131 }
132 }
133
2c0700e7
MCC
134 # Invalid, but it is a common mistake
135 if ($new_tag eq "where") {
75442fb0 136 parse_error($file, $ln, "tag 'Where' is invalid. Should be 'What:' instead", "");
2c0700e7
MCC
137 $new_tag = "what";
138 }
139
bbc249f2 140 if ($new_tag =~ m/what/) {
4e6a6234 141 $space = "";
234948bf
MCC
142 $content =~ s/[,.;]$//;
143
c7ba3334
MCC
144 push @{$symbols{$content}->{file}}, " $file:" . ($ln - 1);
145
bbc249f2 146 if ($tag =~ m/what/) {
ab9c1480 147 $what .= "\xac" . $content;
bbc249f2 148 } else {
234948bf
MCC
149 if ($what) {
150 parse_error($file, $ln, "What '$what' doesn't have a description", "") if (!$data{$what}->{description});
151
ab9c1480 152 foreach my $w(split /\xac/, $what) {
c7ba3334 153 $symbols{$w}->{xref} = $what;
234948bf
MCC
154 };
155 }
4e6a6234 156
bbc249f2 157 $what = $content;
d0ebaf51 158 $label = $content;
bbc249f2
MCC
159 $new_what = 1;
160 }
d0ebaf51 161 push @labels, [($content, $label)];
bbc249f2 162 $tag = $new_tag;
6619c661 163
234948bf 164 push @{$data{$nametag}->{symbols}}, $content if ($data{$nametag}->{what});
bbc249f2
MCC
165 next;
166 }
167
7d7ea8d2 168 if ($tag ne "" && $new_tag) {
4e6a6234 169 $tag = $new_tag;
bbc249f2 170
4e6a6234 171 if ($new_what) {
234948bf 172 @{$data{$what}->{label_list}} = @labels if ($data{$nametag}->{what});
d0ebaf51
MCC
173 @labels = ();
174 $label = "";
4e6a6234 175 $new_what = 0;
bbc249f2 176
4e6a6234 177 $data{$what}->{type} = $type;
c7ba3334
MCC
178 if (!defined($data{$what}->{file})) {
179 $data{$what}->{file} = $name;
180 $data{$what}->{filepath} = $file;
181 } else {
182 if ($name ne $data{$what}->{file}) {
183 $data{$what}->{file} .= " " . $name;
184 $data{$what}->{filepath} .= " " . $file;
185 }
186 }
46f661fd 187 print STDERR "\twhat: $what\n" if ($debug & $dbg_what_parsing);
c7ba3334
MCC
188 $data{$what}->{line_no} = $ln;
189 } else {
190 $data{$what}->{line_no} = $ln if (!defined($data{$what}->{line_no}));
4e6a6234 191 }
bbc249f2 192
4e6a6234
MCC
193 if (!$what) {
194 parse_error($file, $ln, "'What:' should come first:", $_);
195 next;
196 }
f82a8a74
MCC
197 if ($new_tag eq "description") {
198 $sep =~ s,:, ,;
11ce90a4 199 $content = ' ' x length($new_tag) . $sep . $content;
f82a8a74
MCC
200 while ($content =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e) {}
201 if ($content =~ m/^(\s*)(\S.*)$/) {
202 # Preserve initial spaces for the first line
11ce90a4 203 $space = $1;
f82a8a74
MCC
204 $content = "$2\n";
205 $data{$what}->{$tag} .= $content;
206 } else {
207 undef($space);
4e6a6234 208 }
e9bca891 209
4e6a6234
MCC
210 } else {
211 $data{$what}->{$tag} = $content;
212 }
bbc249f2
MCC
213 next;
214 }
bbc249f2
MCC
215 }
216
4e6a6234 217 # Store any contents before tags at the database
d0ebaf51
MCC
218 if (!$tag && $data{$nametag}->{what}) {
219 $data{$nametag}->{description} .= $_;
6619c661
MCC
220 next;
221 }
bbc249f2 222
4e6a6234 223 if ($tag eq "description") {
e9bca891
MCC
224 my $content = $_;
225 while ($content =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e) {}
f82a8a74
MCC
226 if (m/^\s*\n/) {
227 $data{$what}->{$tag} .= "\n";
228 next;
229 }
230
231 if (!defined($space)) {
e9bca891 232 # Preserve initial spaces for the first line
f82a8a74 233 if ($content =~ m/^(\s*)(\S.*)$/) {
e9bca891 234 $space = $1;
f82a8a74 235 $content = "$2\n";
4e6a6234
MCC
236 }
237 } else {
4e6a6234 238 $space = "" if (!($content =~ s/^($space)//));
4e6a6234 239 }
f82a8a74
MCC
240 $data{$what}->{$tag} .= $content;
241
4e6a6234
MCC
242 next;
243 }
bbc249f2
MCC
244 if (m/^\s*(.*)/) {
245 $data{$what}->{$tag} .= "\n$1";
246 $data{$what}->{$tag} =~ s/\n+$//;
247 next;
248 }
249
250 # Everything else is error
75442fb0 251 parse_error($file, $ln, "Unexpected content", $_);
bbc249f2 252 }
234948bf
MCC
253 $data{$nametag}->{description} =~ s/^\n+// if ($data{$nametag}->{description});
254 if ($what) {
255 parse_error($file, $ln, "What '$what' doesn't have a description", "") if (!$data{$what}->{description});
256
ab9c1480 257 foreach my $w(split /\xac/,$what) {
c7ba3334 258 $symbols{$w}->{xref} = $what;
234948bf
MCC
259 };
260 }
bbc249f2
MCC
261 close IN;
262}
263
234948bf
MCC
264sub create_labels {
265 my %labels;
bbc249f2 266
234948bf
MCC
267 foreach my $what (keys %data) {
268 next if ($data{$what}->{file} eq "File");
4e6a6234 269
234948bf 270 foreach my $p (@{$data{$what}->{label_list}}) {
d0ebaf51
MCC
271 my ($content, $label) = @{$p};
272 $label = "abi_" . $label . " ";
273 $label =~ tr/A-Z/a-z/;
274
275 # Convert special chars to "_"
276 $label =~s/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\xff])/_/g;
277 $label =~ s,_+,_,g;
278 $label =~ s,_$,,;
279
2e7ce055
MCC
280 # Avoid duplicated labels
281 while (defined($labels{$label})) {
282 my @chars = ("A".."Z", "a".."z");
283 $label .= $chars[rand @chars];
284 }
285 $labels{$label} = 1;
286
234948bf 287 $data{$what}->{label} = $label;
d0ebaf51
MCC
288
289 # only one label is enough
290 last;
6619c661 291 }
234948bf
MCC
292 }
293}
294
295#
296# Outputs the book on ReST format
297#
298
50ebf8f4
MCC
299# \b doesn't work well with paths. So, we need to define something else:
300# Boundaries are punct characters, spaces and end-of-line
301my $start = qr {(^|\s|\() }x;
302my $bondary = qr { ([,.:;\)\s]|\z) }x;
87ec9ea1 303my $xref_match = qr { $start(\/(sys|config|proc|dev|kvd)\/[^,.:;\)\s]+)$bondary }x;
b0f9580a 304my $symbols = qr { ([\x01-\x08\x0e-\x1f\x21-\x2f\x3a-\x40\x7b-\xff]) }x;
55e5414f 305
234948bf
MCC
306sub output_rest {
307 create_labels();
308
9d4fdda3
MCC
309 my $part = "";
310
234948bf
MCC
311 foreach my $what (sort {
312 ($data{$a}->{type} eq "File") cmp ($data{$b}->{type} eq "File") ||
313 $a cmp $b
314 } keys %data) {
315 my $type = $data{$what}->{type};
c7ba3334
MCC
316
317 my @file = split / /, $data{$what}->{file};
318 my @filepath = split / /, $data{$what}->{filepath};
234948bf
MCC
319
320 if ($enable_lineno) {
321 printf "#define LINENO %s%s#%s\n\n",
c7ba3334 322 $prefix, $file[0],
234948bf
MCC
323 $data{$what}->{line_no};
324 }
6619c661 325
234948bf 326 my $w = $what;
6619c661 327
c7ba3334 328 if ($type ne "File") {
9d4fdda3
MCC
329 my $cur_part = $what;
330 if ($what =~ '/') {
331 if ($what =~ m#^(\/?(?:[\w\-]+\/?){1,2})#) {
332 $cur_part = "Symbols under $1";
333 $cur_part =~ s,/$,,;
334 }
335 }
336
337 if ($cur_part ne "" && $part ne $cur_part) {
338 $part = $cur_part;
339 my $bar = $part;
340 $bar =~ s/./-/g;
341 print "$part\n$bar\n\n";
342 }
343
234948bf 344 printf ".. _%s:\n\n", $data{$what}->{label};
45f96517 345
ab9c1480 346 my @names = split /\xac/,$w;
45f96517
MCC
347 my $len = 0;
348
349 foreach my $name (@names) {
b0f9580a 350 $name =~ s/$symbols/\\$1/g;
c01d62d3 351 $name = "**$name**";
45f96517
MCC
352 $len = length($name) if (length($name) > $len);
353 }
354
45f96517
MCC
355 print "+-" . "-" x $len . "-+\n";
356 foreach my $name (@names) {
357 printf "| %s", $name . " " x ($len - length($name)) . " |\n";
358 print "+-" . "-" x $len . "-+\n";
359 }
45f96517 360
c7ba3334
MCC
361 print "\n";
362 }
363
364 for (my $i = 0; $i < scalar(@filepath); $i++) {
365 my $path = $filepath[$i];
366 my $f = $file[$i];
367
368 $path =~ s,.*/(.*/.*),$1,;;
369 $path =~ s,[/\-],_,g;;
370 my $fileref = "abi_file_".$path;
371
372 if ($type eq "File") {
c7ba3334 373 print ".. _$fileref:\n\n";
c7ba3334
MCC
374 } else {
375 print "Defined on file :ref:`$f <$fileref>`\n\n";
376 }
234948bf 377 }
bbc249f2 378
a4ea67bc
MCC
379 if ($type eq "File") {
380 my $bar = $w;
381 $bar =~ s/./-/g;
382 print "$w\n$bar\n\n";
383 }
384
234948bf
MCC
385 my $desc = "";
386 $desc = $data{$what}->{description} if (defined($data{$what}->{description}));
387 $desc =~ s/\s+$/\n/;
bbc249f2 388
4e6a6234 389 if (!($desc =~ /^\s*$/)) {
11ce90a4 390 if ($description_is_rst) {
daaaf58a
MCC
391 # Remove title markups from the description
392 # Having titles inside ABI files will only work if extra
393 # care would be taken in order to strictly follow the same
394 # level order for each markup.
395 $desc =~ s/\n[\-\*\=\^\~]+\n/\n\n/g;
396
55e5414f
MCC
397 # Enrich text by creating cross-references
398
c27c2e34 399 my $new_desc = "";
2ae7bb57
MCC
400 my $init_indent = -1;
401 my $literal_indent = -1;
402
c27c2e34
MCC
403 open(my $fh, "+<", \$desc);
404 while (my $d = <$fh>) {
2ae7bb57
MCC
405 my $indent = $d =~ m/^(\s+)/;
406 my $spaces = length($indent);
407 $init_indent = $indent if ($init_indent < 0);
408 if ($literal_indent >= 0) {
409 if ($spaces > $literal_indent) {
410 $new_desc .= $d;
411 next;
412 } else {
413 $literal_indent = -1;
414 }
415 } else {
416 if ($d =~ /()::$/ && !($d =~ /^\s*\.\./)) {
417 $literal_indent = $spaces;
418 }
419 }
420
c27c2e34
MCC
421 $d =~ s,Documentation/(?!devicetree)(\S+)\.rst,:doc:`/$1`,g;
422
423 my @matches = $d =~ m,Documentation/ABI/([\w\/\-]+),g;
424 foreach my $f (@matches) {
425 my $xref = $f;
426 my $path = $f;
427 $path =~ s,.*/(.*/.*),$1,;;
428 $path =~ s,[/\-],_,g;;
429 $xref .= " <abi_file_" . $path . ">";
430 $d =~ s,\bDocumentation/ABI/$f\b,:ref:`$xref`,g;
431 }
55e5414f 432
c27c2e34
MCC
433 # Seek for cross reference symbols like /sys/...
434 @matches = $d =~ m/$xref_match/g;
55e5414f 435
c27c2e34
MCC
436 foreach my $s (@matches) {
437 next if (!($s =~ m,/,));
438 if (defined($data{$s}) && defined($data{$s}->{label})) {
439 my $xref = $s;
55e5414f 440
c27c2e34
MCC
441 $xref =~ s/$symbols/\\$1/g;
442 $xref = ":ref:`$xref <" . $data{$s}->{label} . ">`";
55e5414f 443
c27c2e34
MCC
444 $d =~ s,$start$s$bondary,$1$xref$2,g;
445 }
55e5414f 446 }
c27c2e34 447 $new_desc .= $d;
55e5414f 448 }
c27c2e34
MCC
449 close $fh;
450
55e5414f 451
c27c2e34 452 print "$new_desc\n\n";
4e6a6234 453 } else {
11ce90a4 454 $desc =~ s/^\s+//;
bbc249f2 455
11ce90a4
MCC
456 # Remove title markups from the description, as they won't work
457 $desc =~ s/\n[\-\*\=\^\~]+\n/\n\n/g;
458
459 if ($desc =~ m/\:\n/ || $desc =~ m/\n[\t ]+/ || $desc =~ m/[\x00-\x08\x0b-\x1f\x7b-\xff]/) {
460 # put everything inside a code block
461 $desc =~ s/\n/\n /g;
462
463 print "::\n\n";
464 print " $desc\n\n";
465 } else {
466 # Escape any special chars from description
467 $desc =~s/([\x00-\x08\x0b-\x1f\x21-\x2a\x2d\x2f\x3c-\x40\x5c\x5e-\x60\x7b-\xff])/\\$1/g;
468 print "$desc\n\n";
469 }
4e6a6234 470 }
bbc249f2 471 } else {
d0ebaf51 472 print "DESCRIPTION MISSING for $what\n\n" if (!$data{$what}->{is_file});
bbc249f2 473 }
6619c661 474
234948bf 475 if ($data{$what}->{symbols}) {
d0ebaf51
MCC
476 printf "Has the following ABI:\n\n";
477
234948bf 478 foreach my $content(@{$data{$what}->{symbols}}) {
c7ba3334 479 my $label = $data{$symbols{$content}->{xref}}->{label};
d0ebaf51
MCC
480
481 # Escape special chars from content
482 $content =~s/([\x00-\x1f\x21-\x2f\x3a-\x40\x7b-\xff])/\\$1/g;
483
484 print "- :ref:`$content <$label>`\n\n";
485 }
486 }
a16ab14e
MCC
487
488 if (defined($data{$what}->{users})) {
489 my $users = $data{$what}->{users};
490
491 $users =~ s/\n/\n\t/g;
492 printf "Users:\n\t%s\n\n", $users if ($users ne "");
493 }
494
bbc249f2
MCC
495 }
496}
497
33e3e991
MCC
498#
499# Searches for ABI symbols
500#
501sub search_symbols {
502 foreach my $what (sort keys %data) {
503 next if (!($what =~ m/($arg)/));
504
505 my $type = $data{$what}->{type};
506 next if ($type eq "File");
507
508 my $file = $data{$what}->{filepath};
509
e27c42a5 510 $what =~ s/\xac/, /g;
33e3e991
MCC
511 my $bar = $what;
512 $bar =~ s/./-/g;
513
514 print "\n$what\n$bar\n\n";
515
234948bf
MCC
516 my $kernelversion = $data{$what}->{kernelversion} if (defined($data{$what}->{kernelversion}));
517 my $contact = $data{$what}->{contact} if (defined($data{$what}->{contact}));
518 my $users = $data{$what}->{users} if (defined($data{$what}->{users}));
519 my $date = $data{$what}->{date} if (defined($data{$what}->{date}));
520 my $desc = $data{$what}->{description} if (defined($data{$what}->{description}));
521
522 $kernelversion =~ s/^\s+// if ($kernelversion);
523 $contact =~ s/^\s+// if ($contact);
524 if ($users) {
525 $users =~ s/^\s+//;
526 $users =~ s/\n//g;
527 }
528 $date =~ s/^\s+// if ($date);
529 $desc =~ s/^\s+// if ($desc);
33e3e991
MCC
530
531 printf "Kernel version:\t\t%s\n", $kernelversion if ($kernelversion);
532 printf "Date:\t\t\t%s\n", $date if ($date);
533 printf "Contact:\t\t%s\n", $contact if ($contact);
534 printf "Users:\t\t\t%s\n", $users if ($users);
c7ba3334 535 print "Defined on file(s):\t$file\n\n";
33e3e991
MCC
536 print "Description:\n\n$desc";
537 }
538}
539
f090db43 540# Exclude /sys/kernel/debug and /sys/kernel/tracing from the search path
ab02c515 541sub dont_parse_special_attributes {
f090db43
MCC
542 if (($File::Find::dir =~ m,^/sys/kernel,)) {
543 return grep {!/(debug|tracing)/ } @_;
544 }
545
546 if (($File::Find::dir =~ m,^/sys/fs,)) {
547 return grep {!/(pstore|bpf|fuse)/ } @_;
548 }
549
550 return @_
551}
552
553my %leaf;
ab02c515
MCC
554my %aliases;
555my @files;
ca8e055c
MCC
556my %root;
557
558sub graph_add_file {
559 my $file = shift;
560 my $type = shift;
561
562 my $dir = $file;
563 $dir =~ s,^(.*/).*,$1,;
564 $file =~ s,.*/,,;
565
566 my $name;
567 my $file_ref = \%root;
568 foreach my $edge(split "/", $dir) {
569 $name .= "$edge/";
570 if (!defined ${$file_ref}{$edge}) {
571 ${$file_ref}{$edge} = { };
572 }
573 $file_ref = \%{$$file_ref{$edge}};
574 ${$file_ref}{"__name"} = [ $name ];
575 }
576 $name .= "$file";
577 ${$file_ref}{$file} = {
578 "__name" => [ $name ]
579 };
580
581 return \%{$$file_ref{$file}};
582}
583
584sub graph_add_link {
585 my $file = shift;
586 my $link = shift;
587
588 # Traverse graph to find the reference
589 my $file_ref = \%root;
590 foreach my $edge(split "/", $file) {
591 $file_ref = \%{$$file_ref{$edge}} || die "Missing node!";
592 }
593
594 # do a BFS
595
596 my @queue;
597 my %seen;
598 my $base_name;
599 my $st;
600
601 push @queue, $file_ref;
602 $seen{$start}++;
603
604 while (@queue) {
605 my $v = shift @queue;
606 my @child = keys(%{$v});
607
608 foreach my $c(@child) {
609 next if $seen{$$v{$c}};
610 next if ($c eq "__name");
611
612 # Add new name
613 my $name = @{$$v{$c}{"__name"}}[0];
614 if ($name =~ s#^$file/#$link/#) {
615 push @{$$v{$c}{"__name"}}, $name;
616 }
617 # Add child to the queue and mark as seen
618 push @queue, $$v{$c};
619 $seen{$c}++;
620 }
621 }
622}
f090db43 623
ab02c515 624my $escape_symbols = qr { ([\x01-\x08\x0e-\x1f\x21-\x29\x2b-\x2d\x3a-\x40\x7b-\xfe]) }x;
f090db43
MCC
625sub parse_existing_sysfs {
626 my $file = $File::Find::name;
0b87a1b8
MCC
627
628 # Ignore cgroup and firmware
629 return if ($file =~ m#^/sys/(fs/cgroup|firmware)/#);
630
0cd9e25b
MCC
631 # Ignore some sysfs nodes
632 return if ($file =~ m#/(sections|notes)/#);
633
634 # Would need to check at
635 # Documentation/admin-guide/kernel-parameters.txt, but this
636 # is not easily parseable.
637 return if ($file =~ m#/parameters/#);
638
ab02c515
MCC
639 my $mode = (lstat($file))[2];
640 my $abs_file = abs_path($file);
f090db43 641
ab02c515
MCC
642 if (S_ISLNK($mode)) {
643 $aliases{$file} = $abs_file;
644 return;
645 }
646
647 return if (S_ISDIR($mode));
f090db43 648
ab02c515
MCC
649 # Trivial: file is defined exactly the same way at ABI What:
650 return if (defined($data{$file}));
651 return if (defined($data{$abs_file}));
f090db43 652
ca8e055c
MCC
653 push @files, graph_add_file($abs_file, "file");
654}
655
656sub get_leave($)
657{
658 my $what = shift;
659 my $leave;
660
661 my $l = $what;
662 my $stop = 1;
663
664 $leave = $l;
665 $leave =~ s,/$,,;
666 $leave =~ s,.*/,,;
667 $leave =~ s/[\(\)]//g;
668
669 # $leave is used to improve search performance at
670 # check_undefined_symbols, as the algorithm there can seek
671 # for a small number of "what". It also allows giving a
672 # hint about a leave with the same name somewhere else.
673 # However, there are a few occurences where the leave is
674 # either a wildcard or a number. Just group such cases
675 # altogether.
92635894 676 if ($leave =~ m/\.\*/ || $leave eq "" || $leave =~ /\\d/) {
ca8e055c
MCC
677 $leave = "others";
678 }
679
680 return $leave;
ab02c515 681}
f090db43 682
ab02c515 683sub check_undefined_symbols {
ca8e055c
MCC
684 foreach my $file_ref (sort @files) {
685 my @names = @{$$file_ref{"__name"}};
686 my $file = $names[0];
f090db43 687
ab02c515 688 my $exact = 0;
14c94257 689 my $found_string;
f090db43 690
ca8e055c
MCC
691 my $leave = get_leave($file);
692 if (!defined($leaf{$leave})) {
693 $leave = "others";
694 }
695 my $what = $leaf{$leave};
ab02c515
MCC
696
697 my $path = $file;
698 $path =~ s,(.*/).*,$1,;
699
14c94257
MCC
700 if ($search_string) {
701 next if (!($file =~ m#$search_string#));
702 $found_string = 1;
703 }
704
ca8e055c
MCC
705 foreach my $a (@names) {
706 print "--> $a\n" if ($found_string && $hint);
707 foreach my $w (split /\xac/, $what) {
708 if ($a =~ m#^$w$#) {
709 $exact = 1;
710 last;
ab02c515 711 }
f090db43 712 }
cb06b8dd 713 last if ($exact);
f090db43 714 }
ab02c515 715 next if ($exact);
f090db43 716
cb06b8dd 717 if ($hint && (!$search_string || $found_string)) {
ca8e055c
MCC
718 $what =~ s/\xac/\n\t/g;
719 if ($leave ne "others") {
720 print " more likely regexes:\n\t$what\n";
721 } else {
722 print " tested regexes:\n\t$what\n";
723 }
ab02c515
MCC
724 next;
725 }
14c94257 726 print "$file not found.\n" if (!$search_string || $found_string);
ab02c515 727 }
f090db43
MCC
728}
729
730sub undefined_symbols {
ab02c515
MCC
731 find({
732 wanted =>\&parse_existing_sysfs,
733 preprocess =>\&dont_parse_special_attributes,
734 no_chdir => 1
735 }, $sysfs_prefix);
736
ca8e055c
MCC
737 $leaf{"others"} = "";
738
f090db43 739 foreach my $w (sort keys %data) {
ca8e055c 740 foreach my $what (split /\xac/,$w) {
ab02c515
MCC
741 next if (!($what =~ m/^$sysfs_prefix/));
742
743 # Convert what into regular expressions
744
745 $what =~ s,/\.\.\./,/*/,g;
746 $what =~ s,\*,.*,g;
747
748 # Temporarily change [0-9]+ type of patterns
749 $what =~ s/\[0\-9\]\+/\xff/g;
750
751 # Temporarily change [\d+-\d+] type of patterns
752 $what =~ s/\[0\-\d+\]/\xff/g;
753 $what =~ s/\[(\d+)\]/\xf4$1\xf5/g;
754
755 # Temporarily change [0-9] type of patterns
756 $what =~ s/\[(\d)\-(\d)\]/\xf4$1-$2\xf5/g;
757
758 # Handle multiple option patterns
759 $what =~ s/[\{\<\[]([\w_]+)(?:[,|]+([\w_]+)){1,}[\}\>\]]/($1|$2)/g;
760
761 # Handle wildcards
762 $what =~ s/\<[^\>]+\>/.*/g;
763 $what =~ s/\{[^\}]+\}/.*/g;
764 $what =~ s/\[[^\]]+\]/.*/g;
765
766 $what =~ s/[XYZ]/.*/g;
767
768 # Recover [0-9] type of patterns
769 $what =~ s/\xf4/[/g;
770 $what =~ s/\xf5/]/g;
771
772 # Remove duplicated spaces
773 $what =~ s/\s+/ /g;
774
775 # Special case: this ABI has a parenthesis on it
776 $what =~ s/sqrt\(x^2\+y^2\+z^2\)/sqrt\(x^2\+y^2\+z^2\)/;
777
778 # Special case: drop comparition as in:
779 # What: foo = <something>
780 # (this happens on a few IIO definitions)
781 $what =~ s,\s*\=.*$,,;
782
ab02c515
MCC
783 # Escape all other symbols
784 $what =~ s/$escape_symbols/\\$1/g;
785 $what =~ s/\\\\/\\/g;
786 $what =~ s/\\([\[\]\(\)\|])/$1/g;
787 $what =~ s/(\d+)\\(-\d+)/$1$2/g;
788
14c94257
MCC
789 $what =~ s/\xff/\\d+/g;
790
14c94257
MCC
791 # Special case: IIO ABI which a parenthesis.
792 $what =~ s/sqrt(.*)/sqrt\(.*\)/;
793
45495db9 794 my $leave = get_leave($what);
14c94257 795 my $added = 0;
ab02c515
MCC
796 foreach my $l (split /\|/, $leave) {
797 if (defined($leaf{$l})) {
ca8e055c
MCC
798 next if ($leaf{$l} =~ m/\b$what\b/);
799 $leaf{$l} .= "\xac" . $what;
14c94257 800 $added = 1;
ab02c515
MCC
801 } else {
802 $leaf{$l} = $what;
14c94257 803 $added = 1;
ab02c515 804 }
f090db43 805 }
14c94257
MCC
806 if ($search_string && $added) {
807 print "What: $what\n" if ($what =~ m#$search_string#);
808 }
809
f090db43
MCC
810 }
811 }
ca8e055c
MCC
812 # Take links into account
813 foreach my $link (keys %aliases) {
814 my $abs_file = $aliases{$link};
815 graph_add_link($abs_file, $link);
816 }
ab02c515 817 check_undefined_symbols;
f090db43
MCC
818}
819
61439c4a
MCC
820# Ensure that the prefix will always end with a slash
821# While this is not needed for find, it makes the patch nicer
822# with --enable-lineno
823$prefix =~ s,/?$,/,;
33e3e991 824
f090db43
MCC
825if ($cmd eq "undefined" || $cmd eq "search") {
826 $show_warnings = 0;
827}
bbc249f2
MCC
828#
829# Parses all ABI files located at $prefix dir
830#
831find({wanted =>\&parse_abi, no_chdir => 1}, $prefix);
832
46f661fd 833print STDERR Data::Dumper->Dump([\%data], [qw(*data)]) if ($debug & $dbg_dump_abi_structs);
bbc249f2
MCC
834
835#
33e3e991 836# Handles the command
bbc249f2 837#
f090db43
MCC
838if ($cmd eq "undefined") {
839 undefined_symbols;
840} elsif ($cmd eq "search") {
33e3e991 841 search_symbols;
c7ba3334
MCC
842} else {
843 if ($cmd eq "rest") {
844 output_rest;
845 }
846
847 # Warn about duplicated ABI entries
848 foreach my $what(sort keys %symbols) {
849 my @files = @{$symbols{$what}->{file}};
850
851 next if (scalar(@files) == 1);
bbc249f2 852
c7ba3334
MCC
853 printf STDERR "Warning: $what is defined %d times: @files\n",
854 scalar(@files);
855 }
856}
bbc249f2
MCC
857
858__END__
859
860=head1 NAME
861
862abi_book.pl - parse the Linux ABI files and produce a ReST book.
863
864=head1 SYNOPSIS
865
46f661fd 866B<abi_book.pl> [--debug <level>] [--enable-lineno] [--man] [--help]
ab02c515 867 [--(no-)rst-source] [--dir=<dir>] [--show-hints]
14c94257 868 [--search-string <regex>]
ab02c515 869 <COMAND> [<ARGUMENT>]
33e3e991
MCC
870
871Where <COMMAND> can be:
872
873=over 8
874
875B<search> [SEARCH_REGEX] - search for [SEARCH_REGEX] inside ABI
876
7ce7b89b
MCC
877B<rest> - output the ABI in ReST markup language
878
879B<validate> - validate the ABI contents
33e3e991 880
f090db43
MCC
881B<undefined> - existing symbols at the system that aren't
882 defined at Documentation/ABI
883
33e3e991 884=back
bbc249f2
MCC
885
886=head1 OPTIONS
887
888=over 8
889
33e3e991
MCC
890=item B<--dir>
891
892Changes the location of the ABI search. By default, it uses
893the Documentation/ABI directory.
894
11ce90a4
MCC
895=item B<--rst-source> and B<--no-rst-source>
896
897The input file may be using ReST syntax or not. Those two options allow
898selecting between a rst-compliant source ABI (--rst-source), or a
899plain text that may be violating ReST spec, so it requres some escaping
900logic (--no-rst-source).
901
61439c4a
MCC
902=item B<--enable-lineno>
903
904Enable output of #define LINENO lines.
905
46f661fd
MCC
906=item B<--debug> I<debug level>
907
908Print debug information according with the level, which is given by the
909following bitmask:
bbc249f2 910
46f661fd
MCC
911 - 1: Debug parsing What entries from ABI files;
912 - 2: Shows what files are opened from ABI files;
913 - 4: Dump the structs used to store the contents of the ABI files.
bbc249f2 914
ab02c515
MCC
915=item B<--show-hints>
916
917Show hints about possible definitions for the missing ABI symbols.
918Used only when B<undefined>.
919
14c94257
MCC
920=item B<--search-string> [regex string]
921
922Show only occurences that match a search string.
923Used only when B<undefined>.
924
bbc249f2
MCC
925=item B<--help>
926
927Prints a brief help message and exits.
928
929=item B<--man>
930
931Prints the manual page and exits.
932
933=back
934
935=head1 DESCRIPTION
936
33e3e991
MCC
937Parse the Linux ABI files from ABI DIR (usually located at Documentation/ABI),
938allowing to search for ABI symbols or to produce a ReST book containing
939the Linux ABI documentation.
940
941=head1 EXAMPLES
942
943Search for all stable symbols with the word "usb":
944
945=over 8
946
947$ scripts/get_abi.pl search usb --dir Documentation/ABI/stable
948
949=back
950
951Search for all symbols that match the regex expression "usb.*cap":
952
953=over 8
954
955$ scripts/get_abi.pl search usb.*cap
956
957=back
958
959Output all obsoleted symbols in ReST format
960
961=over 8
962
963$ scripts/get_abi.pl rest --dir Documentation/ABI/obsolete
964
965=back
bbc249f2
MCC
966
967=head1 BUGS
968
7ce7b89b 969Report bugs to Mauro Carvalho Chehab <mchehab+samsung@kernel.org>
bbc249f2
MCC
970
971=head1 COPYRIGHT
972
7ce7b89b 973Copyright (c) 2016-2019 by Mauro Carvalho Chehab <mchehab+samsung@kernel.org>.
bbc249f2
MCC
974
975License GPLv2: GNU GPL version 2 <http://gnu.org/licenses/gpl.html>.
976
977This is free software: you are free to change and redistribute it.
978There is NO WARRANTY, to the extent permitted by law.
979
980=cut