get_abi.pl 14 KB

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