blob: b0ca4f4e56f2c325ae11328b7b6c8ee3cfff38ec [file] [log] [blame]
Finn Behrensc25ce582020-11-23 15:15:33 +01001#!/usr/bin/env perl
Mauro Carvalho Chehabecb351f2019-06-20 14:23:10 -03002# SPDX-License-Identifier: GPL-2.0
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -03003
4use strict;
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +01005use warnings;
Mauro Carvalho Chehab55e54142020-10-30 08:40:29 +01006use utf8;
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -03007use Pod::Usage;
8use Getopt::Long;
9use File::Find;
10use Fcntl ':mode';
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +020011use Cwd 'abs_path';
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -030012
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +010013my $help = 0;
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +020014my $hint = 0;
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +010015my $man = 0;
16my $debug = 0;
17my $enable_lineno = 0;
Mauro Carvalho Chehabf090db42021-09-18 11:52:12 +020018my $show_warnings = 1;
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -030019my $prefix="Documentation/ABI";
Mauro Carvalho Chehabf090db42021-09-18 11:52:12 +020020my $sysfs_prefix="/sys";
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -030021
Mauro Carvalho Chehab11ce90a2020-10-30 08:40:20 +010022#
23# If true, assumes that the description is formatted with ReST
24#
Mauro Carvalho Chehab2fcce372020-10-30 08:40:58 +010025my $description_is_rst = 1;
Mauro Carvalho Chehab11ce90a2020-10-30 08:40:20 +010026
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -030027GetOptions(
28 "debug|d+" => \$debug,
Mauro Carvalho Chehab61439c42020-10-30 08:40:22 +010029 "enable-lineno" => \$enable_lineno,
Mauro Carvalho Chehab11ce90a2020-10-30 08:40:20 +010030 "rst-source!" => \$description_is_rst,
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -030031 "dir=s" => \$prefix,
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -030032 'help|?' => \$help,
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +020033 "show-hints" => \$hint,
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -030034 man => \$man
35) or pod2usage(2);
36
37pod2usage(1) if $help;
38pod2usage(-exitstatus => 0, -verbose => 2) if $man;
39
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -030040pod2usage(2) if (scalar @ARGV < 1 || @ARGV > 2);
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -030041
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -030042my ($cmd, $arg) = @ARGV;
43
Mauro Carvalho Chehabf090db42021-09-18 11:52:12 +020044pod2usage(2) if ($cmd ne "search" && $cmd ne "rest" && $cmd ne "validate" && $cmd ne "undefined");
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -030045pod2usage(2) if ($cmd eq "search" && !$arg);
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -030046
47require Data::Dumper if ($debug);
48
49my %data;
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +010050my %symbols;
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -030051
52#
53# Displays an error message, printing file name and line
54#
55sub parse_error($$$$) {
56 my ($file, $ln, $msg, $data) = @_;
57
Mauro Carvalho Chehabf090db42021-09-18 11:52:12 +020058 return if (!$show_warnings);
59
Mauro Carvalho Chehab75442fb2020-10-30 08:40:45 +010060 $data =~ s/\s+$/\n/;
61
62 print STDERR "Warning: file $file#$ln:\n\t$msg";
63
64 if ($data ne "") {
65 print STDERR ". Line\n\t\t$data";
66 } else {
67 print STDERR "\n";
68 }
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -030069}
70
71#
72# Parse an ABI file, storing its contents at %data
73#
74sub parse_abi {
75 my $file = $File::Find::name;
76
77 my $mode = (stat($file))[2];
78 return if ($mode & S_IFDIR);
79 return if ($file =~ m,/README,);
80
81 my $name = $file;
82 $name =~ s,.*/,,;
83
Mauro Carvalho Chehaba4ea67b2020-10-30 08:40:27 +010084 my $fn = $file;
85 $fn =~ s,Documentation/ABI/,,;
86
87 my $nametag = "File $fn";
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -030088 $data{$nametag}->{what} = "File $name";
89 $data{$nametag}->{type} = "File";
90 $data{$nametag}->{file} = $name;
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -030091 $data{$nametag}->{filepath} = $file;
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -030092 $data{$nametag}->{is_file} = 1;
Mauro Carvalho Chehab61439c42020-10-30 08:40:22 +010093 $data{$nametag}->{line_no} = 1;
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -030094
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -030095 my $type = $file;
96 $type =~ s,.*/(.*)/.*,$1,;
97
98 my $what;
99 my $new_what;
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100100 my $tag = "";
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300101 my $ln;
Mauro Carvalho Chehab6619c662019-06-20 14:22:56 -0300102 my $xrefs;
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300103 my $space;
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300104 my @labels;
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100105 my $label = "";
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300106
107 print STDERR "Opening $file\n" if ($debug > 1);
108 open IN, $file;
109 while(<IN>) {
110 $ln++;
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300111 if (m/^(\S+)(:\s*)(.*)/i) {
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300112 my $new_tag = lc($1);
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300113 my $sep = $2;
114 my $content = $3;
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300115
Mauro Carvalho Chehab7ce7b892019-06-20 14:23:04 -0300116 if (!($new_tag =~ m/(what|where|date|kernelversion|contact|description|users)/)) {
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300117 if ($tag eq "description") {
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300118 # New "tag" is actually part of
119 # description. Don't consider it a tag
120 $new_tag = "";
Mauro Carvalho Chehab7d7ea8d2019-06-20 14:23:01 -0300121 } elsif ($tag ne "") {
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300122 parse_error($file, $ln, "tag '$tag' is invalid", $_);
123 }
124 }
125
Mauro Carvalho Chehab2c0700e2019-06-20 14:23:03 -0300126 # Invalid, but it is a common mistake
127 if ($new_tag eq "where") {
Mauro Carvalho Chehab75442fb2020-10-30 08:40:45 +0100128 parse_error($file, $ln, "tag 'Where' is invalid. Should be 'What:' instead", "");
Mauro Carvalho Chehab2c0700e2019-06-20 14:23:03 -0300129 $new_tag = "what";
130 }
131
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300132 if ($new_tag =~ m/what/) {
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300133 $space = "";
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100134 $content =~ s/[,.;]$//;
135
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100136 push @{$symbols{$content}->{file}}, " $file:" . ($ln - 1);
137
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300138 if ($tag =~ m/what/) {
Mauro Carvalho Chehabab9c1482021-09-18 11:52:11 +0200139 $what .= "\xac" . $content;
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300140 } else {
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100141 if ($what) {
142 parse_error($file, $ln, "What '$what' doesn't have a description", "") if (!$data{$what}->{description});
143
Mauro Carvalho Chehabab9c1482021-09-18 11:52:11 +0200144 foreach my $w(split /\xac/, $what) {
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100145 $symbols{$w}->{xref} = $what;
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100146 };
147 }
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300148
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300149 $what = $content;
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300150 $label = $content;
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300151 $new_what = 1;
152 }
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300153 push @labels, [($content, $label)];
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300154 $tag = $new_tag;
Mauro Carvalho Chehab6619c662019-06-20 14:22:56 -0300155
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100156 push @{$data{$nametag}->{symbols}}, $content if ($data{$nametag}->{what});
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300157 next;
158 }
159
Mauro Carvalho Chehab7d7ea8d2019-06-20 14:23:01 -0300160 if ($tag ne "" && $new_tag) {
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300161 $tag = $new_tag;
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300162
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300163 if ($new_what) {
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100164 @{$data{$what}->{label_list}} = @labels if ($data{$nametag}->{what});
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300165 @labels = ();
166 $label = "";
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300167 $new_what = 0;
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300168
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300169 $data{$what}->{type} = $type;
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100170 if (!defined($data{$what}->{file})) {
171 $data{$what}->{file} = $name;
172 $data{$what}->{filepath} = $file;
173 } else {
174 if ($name ne $data{$what}->{file}) {
175 $data{$what}->{file} .= " " . $name;
176 $data{$what}->{filepath} .= " " . $file;
177 }
178 }
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300179 print STDERR "\twhat: $what\n" if ($debug > 1);
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100180 $data{$what}->{line_no} = $ln;
181 } else {
182 $data{$what}->{line_no} = $ln if (!defined($data{$what}->{line_no}));
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300183 }
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300184
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300185 if (!$what) {
186 parse_error($file, $ln, "'What:' should come first:", $_);
187 next;
188 }
Mauro Carvalho Chehabf82a8a72020-10-30 08:40:23 +0100189 if ($new_tag eq "description") {
190 $sep =~ s,:, ,;
Mauro Carvalho Chehab11ce90a2020-10-30 08:40:20 +0100191 $content = ' ' x length($new_tag) . $sep . $content;
Mauro Carvalho Chehabf82a8a72020-10-30 08:40:23 +0100192 while ($content =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e) {}
193 if ($content =~ m/^(\s*)(\S.*)$/) {
194 # Preserve initial spaces for the first line
Mauro Carvalho Chehab11ce90a2020-10-30 08:40:20 +0100195 $space = $1;
Mauro Carvalho Chehabf82a8a72020-10-30 08:40:23 +0100196 $content = "$2\n";
197 $data{$what}->{$tag} .= $content;
198 } else {
199 undef($space);
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300200 }
Mauro Carvalho Chehabe9bca892020-10-30 08:40:21 +0100201
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300202 } else {
203 $data{$what}->{$tag} = $content;
204 }
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300205 next;
206 }
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300207 }
208
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300209 # Store any contents before tags at the database
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300210 if (!$tag && $data{$nametag}->{what}) {
211 $data{$nametag}->{description} .= $_;
Mauro Carvalho Chehab6619c662019-06-20 14:22:56 -0300212 next;
213 }
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300214
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300215 if ($tag eq "description") {
Mauro Carvalho Chehabe9bca892020-10-30 08:40:21 +0100216 my $content = $_;
217 while ($content =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e) {}
Mauro Carvalho Chehabf82a8a72020-10-30 08:40:23 +0100218 if (m/^\s*\n/) {
219 $data{$what}->{$tag} .= "\n";
220 next;
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300221 }
Mauro Carvalho Chehabf82a8a72020-10-30 08:40:23 +0100222
223 if (!defined($space)) {
224 # Preserve initial spaces for the first line
225 if ($content =~ m/^(\s*)(\S.*)$/) {
226 $space = $1;
227 $content = "$2\n";
228 }
229 } else {
230 $space = "" if (!($content =~ s/^($space)//));
231 }
232 $data{$what}->{$tag} .= $content;
233
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300234 next;
235 }
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300236 if (m/^\s*(.*)/) {
237 $data{$what}->{$tag} .= "\n$1";
238 $data{$what}->{$tag} =~ s/\n+$//;
239 next;
240 }
241
242 # Everything else is error
Mauro Carvalho Chehab75442fb2020-10-30 08:40:45 +0100243 parse_error($file, $ln, "Unexpected content", $_);
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300244 }
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100245 $data{$nametag}->{description} =~ s/^\n+// if ($data{$nametag}->{description});
246 if ($what) {
247 parse_error($file, $ln, "What '$what' doesn't have a description", "") if (!$data{$what}->{description});
248
Mauro Carvalho Chehabab9c1482021-09-18 11:52:11 +0200249 foreach my $w(split /\xac/,$what) {
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100250 $symbols{$w}->{xref} = $what;
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100251 };
252 }
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300253 close IN;
254}
255
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100256sub create_labels {
257 my %labels;
258
259 foreach my $what (keys %data) {
260 next if ($data{$what}->{file} eq "File");
261
262 foreach my $p (@{$data{$what}->{label_list}}) {
263 my ($content, $label) = @{$p};
264 $label = "abi_" . $label . " ";
265 $label =~ tr/A-Z/a-z/;
266
267 # Convert special chars to "_"
268 $label =~s/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\xff])/_/g;
269 $label =~ s,_+,_,g;
270 $label =~ s,_$,,;
271
272 # Avoid duplicated labels
273 while (defined($labels{$label})) {
274 my @chars = ("A".."Z", "a".."z");
275 $label .= $chars[rand @chars];
276 }
277 $labels{$label} = 1;
278
279 $data{$what}->{label} = $label;
280
281 # only one label is enough
282 last;
283 }
284 }
285}
286
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300287#
288# Outputs the book on ReST format
289#
Mauro Carvalho Chehab45f96512019-06-20 14:23:00 -0300290
Mauro Carvalho Chehab50ebf8f2021-03-25 11:38:24 +0100291# \b doesn't work well with paths. So, we need to define something else:
292# Boundaries are punct characters, spaces and end-of-line
293my $start = qr {(^|\s|\() }x;
294my $bondary = qr { ([,.:;\)\s]|\z) }x;
Mauro Carvalho Chehab87ec9ea12021-03-25 11:38:25 +0100295my $xref_match = qr { $start(\/(sys|config|proc|dev|kvd)\/[^,.:;\)\s]+)$bondary }x;
Mauro Carvalho Chehabb0f95802021-03-25 11:38:22 +0100296my $symbols = qr { ([\x01-\x08\x0e-\x1f\x21-\x2f\x3a-\x40\x7b-\xff]) }x;
Mauro Carvalho Chehab55e54142020-10-30 08:40:29 +0100297
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300298sub output_rest {
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100299 create_labels();
300
Mauro Carvalho Chehab9d4fdda2020-11-02 11:32:16 +0100301 my $part = "";
302
Mauro Carvalho Chehab45f96512019-06-20 14:23:00 -0300303 foreach my $what (sort {
304 ($data{$a}->{type} eq "File") cmp ($data{$b}->{type} eq "File") ||
305 $a cmp $b
306 } keys %data) {
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300307 my $type = $data{$what}->{type};
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100308
309 my @file = split / /, $data{$what}->{file};
310 my @filepath = split / /, $data{$what}->{filepath};
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300311
Mauro Carvalho Chehab61439c42020-10-30 08:40:22 +0100312 if ($enable_lineno) {
313 printf "#define LINENO %s%s#%s\n\n",
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100314 $prefix, $file[0],
Mauro Carvalho Chehab61439c42020-10-30 08:40:22 +0100315 $data{$what}->{line_no};
316 }
317
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300318 my $w = $what;
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300319
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100320 if ($type ne "File") {
Mauro Carvalho Chehab9d4fdda2020-11-02 11:32:16 +0100321 my $cur_part = $what;
322 if ($what =~ '/') {
323 if ($what =~ m#^(\/?(?:[\w\-]+\/?){1,2})#) {
324 $cur_part = "Symbols under $1";
325 $cur_part =~ s,/$,,;
326 }
327 }
328
329 if ($cur_part ne "" && $part ne $cur_part) {
330 $part = $cur_part;
331 my $bar = $part;
332 $bar =~ s/./-/g;
333 print "$part\n$bar\n\n";
334 }
335
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100336 printf ".. _%s:\n\n", $data{$what}->{label};
Mauro Carvalho Chehab45f96512019-06-20 14:23:00 -0300337
Mauro Carvalho Chehabab9c1482021-09-18 11:52:11 +0200338 my @names = split /\xac/,$w;
Mauro Carvalho Chehab45f96512019-06-20 14:23:00 -0300339 my $len = 0;
340
341 foreach my $name (@names) {
Mauro Carvalho Chehabb0f95802021-03-25 11:38:22 +0100342 $name =~ s/$symbols/\\$1/g;
Mauro Carvalho Chehabc01d62d2020-10-30 08:40:28 +0100343 $name = "**$name**";
Mauro Carvalho Chehab45f96512019-06-20 14:23:00 -0300344 $len = length($name) if (length($name) > $len);
345 }
346
Mauro Carvalho Chehab45f96512019-06-20 14:23:00 -0300347 print "+-" . "-" x $len . "-+\n";
348 foreach my $name (@names) {
349 printf "| %s", $name . " " x ($len - length($name)) . " |\n";
350 print "+-" . "-" x $len . "-+\n";
351 }
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100352
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100353 print "\n";
354 }
355
356 for (my $i = 0; $i < scalar(@filepath); $i++) {
357 my $path = $filepath[$i];
358 my $f = $file[$i];
359
360 $path =~ s,.*/(.*/.*),$1,;;
361 $path =~ s,[/\-],_,g;;
362 my $fileref = "abi_file_".$path;
363
364 if ($type eq "File") {
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100365 print ".. _$fileref:\n\n";
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100366 } else {
367 print "Defined on file :ref:`$f <$fileref>`\n\n";
368 }
Mauro Carvalho Chehab45f96512019-06-20 14:23:00 -0300369 }
370
Mauro Carvalho Chehaba4ea67b2020-10-30 08:40:27 +0100371 if ($type eq "File") {
372 my $bar = $w;
373 $bar =~ s/./-/g;
374 print "$w\n$bar\n\n";
375 }
376
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100377 my $desc = "";
378 $desc = $data{$what}->{description} if (defined($data{$what}->{description}));
379 $desc =~ s/\s+$/\n/;
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300380
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300381 if (!($desc =~ /^\s*$/)) {
Mauro Carvalho Chehab11ce90a2020-10-30 08:40:20 +0100382 if ($description_is_rst) {
Mauro Carvalho Chehabdaaaf582020-11-02 11:32:15 +0100383 # Remove title markups from the description
384 # Having titles inside ABI files will only work if extra
385 # care would be taken in order to strictly follow the same
386 # level order for each markup.
387 $desc =~ s/\n[\-\*\=\^\~]+\n/\n\n/g;
388
Mauro Carvalho Chehab55e54142020-10-30 08:40:29 +0100389 # Enrich text by creating cross-references
390
Mauro Carvalho Chehabc27c2e32021-03-25 11:38:26 +0100391 my $new_desc = "";
Mauro Carvalho Chehab2ae7bb52021-03-25 11:38:27 +0100392 my $init_indent = -1;
393 my $literal_indent = -1;
394
Mauro Carvalho Chehabc27c2e32021-03-25 11:38:26 +0100395 open(my $fh, "+<", \$desc);
396 while (my $d = <$fh>) {
Mauro Carvalho Chehab2ae7bb52021-03-25 11:38:27 +0100397 my $indent = $d =~ m/^(\s+)/;
398 my $spaces = length($indent);
399 $init_indent = $indent if ($init_indent < 0);
400 if ($literal_indent >= 0) {
401 if ($spaces > $literal_indent) {
402 $new_desc .= $d;
403 next;
404 } else {
405 $literal_indent = -1;
406 }
407 } else {
408 if ($d =~ /()::$/ && !($d =~ /^\s*\.\./)) {
409 $literal_indent = $spaces;
410 }
411 }
412
Mauro Carvalho Chehabc27c2e32021-03-25 11:38:26 +0100413 $d =~ s,Documentation/(?!devicetree)(\S+)\.rst,:doc:`/$1`,g;
Mauro Carvalho Chehab55e54142020-10-30 08:40:29 +0100414
Mauro Carvalho Chehabc27c2e32021-03-25 11:38:26 +0100415 my @matches = $d =~ m,Documentation/ABI/([\w\/\-]+),g;
416 foreach my $f (@matches) {
417 my $xref = $f;
418 my $path = $f;
419 $path =~ s,.*/(.*/.*),$1,;;
420 $path =~ s,[/\-],_,g;;
421 $xref .= " <abi_file_" . $path . ">";
422 $d =~ s,\bDocumentation/ABI/$f\b,:ref:`$xref`,g;
Mauro Carvalho Chehab55e54142020-10-30 08:40:29 +0100423 }
Mauro Carvalho Chehab55e54142020-10-30 08:40:29 +0100424
Mauro Carvalho Chehabc27c2e32021-03-25 11:38:26 +0100425 # Seek for cross reference symbols like /sys/...
426 @matches = $d =~ m/$xref_match/g;
427
428 foreach my $s (@matches) {
429 next if (!($s =~ m,/,));
430 if (defined($data{$s}) && defined($data{$s}->{label})) {
431 my $xref = $s;
432
433 $xref =~ s/$symbols/\\$1/g;
434 $xref = ":ref:`$xref <" . $data{$s}->{label} . ">`";
435
436 $d =~ s,$start$s$bondary,$1$xref$2,g;
437 }
438 }
439 $new_desc .= $d;
440 }
441 close $fh;
442
443
444 print "$new_desc\n\n";
Mauro Carvalho Chehab11ce90a2020-10-30 08:40:20 +0100445 } else {
446 $desc =~ s/^\s+//;
447
448 # Remove title markups from the description, as they won't work
449 $desc =~ s/\n[\-\*\=\^\~]+\n/\n\n/g;
450
451 if ($desc =~ m/\:\n/ || $desc =~ m/\n[\t ]+/ || $desc =~ m/[\x00-\x08\x0b-\x1f\x7b-\xff]/) {
452 # put everything inside a code block
453 $desc =~ s/\n/\n /g;
454
455 print "::\n\n";
456 print " $desc\n\n";
457 } else {
458 # Escape any special chars from description
459 $desc =~s/([\x00-\x08\x0b-\x1f\x21-\x2a\x2d\x2f\x3c-\x40\x5c\x5e-\x60\x7b-\xff])/\\$1/g;
460 print "$desc\n\n";
461 }
Mauro Carvalho Chehab4e6a6232019-06-20 14:22:57 -0300462 }
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300463 } else {
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300464 print "DESCRIPTION MISSING for $what\n\n" if (!$data{$what}->{is_file});
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300465 }
Mauro Carvalho Chehab6619c662019-06-20 14:22:56 -0300466
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100467 if ($data{$what}->{symbols}) {
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300468 printf "Has the following ABI:\n\n";
469
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100470 foreach my $content(@{$data{$what}->{symbols}}) {
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100471 my $label = $data{$symbols{$content}->{xref}}->{label};
Mauro Carvalho Chehabd0ebaf52019-06-20 14:22:58 -0300472
473 # Escape special chars from content
474 $content =~s/([\x00-\x1f\x21-\x2f\x3a-\x40\x7b-\xff])/\\$1/g;
475
476 print "- :ref:`$content <$label>`\n\n";
477 }
478 }
Mauro Carvalho Chehaba16ab142020-10-30 08:40:26 +0100479
480 if (defined($data{$what}->{users})) {
481 my $users = $data{$what}->{users};
482
483 $users =~ s/\n/\n\t/g;
484 printf "Users:\n\t%s\n\n", $users if ($users ne "");
485 }
486
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300487 }
488}
489
490#
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300491# Searches for ABI symbols
492#
493sub search_symbols {
494 foreach my $what (sort keys %data) {
495 next if (!($what =~ m/($arg)/));
496
497 my $type = $data{$what}->{type};
498 next if ($type eq "File");
499
500 my $file = $data{$what}->{filepath};
501
502 my $bar = $what;
503 $bar =~ s/./-/g;
504
505 print "\n$what\n$bar\n\n";
506
Mauro Carvalho Chehab234948b2020-10-30 08:40:24 +0100507 my $kernelversion = $data{$what}->{kernelversion} if (defined($data{$what}->{kernelversion}));
508 my $contact = $data{$what}->{contact} if (defined($data{$what}->{contact}));
509 my $users = $data{$what}->{users} if (defined($data{$what}->{users}));
510 my $date = $data{$what}->{date} if (defined($data{$what}->{date}));
511 my $desc = $data{$what}->{description} if (defined($data{$what}->{description}));
512
513 $kernelversion =~ s/^\s+// if ($kernelversion);
514 $contact =~ s/^\s+// if ($contact);
515 if ($users) {
516 $users =~ s/^\s+//;
517 $users =~ s/\n//g;
518 }
519 $date =~ s/^\s+// if ($date);
520 $desc =~ s/^\s+// if ($desc);
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300521
522 printf "Kernel version:\t\t%s\n", $kernelversion if ($kernelversion);
523 printf "Date:\t\t\t%s\n", $date if ($date);
524 printf "Contact:\t\t%s\n", $contact if ($contact);
525 printf "Users:\t\t\t%s\n", $users if ($users);
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100526 print "Defined on file(s):\t$file\n\n";
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300527 print "Description:\n\n$desc";
528 }
529}
530
Mauro Carvalho Chehabf090db42021-09-18 11:52:12 +0200531# Exclude /sys/kernel/debug and /sys/kernel/tracing from the search path
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200532sub dont_parse_special_attributes {
Mauro Carvalho Chehabf090db42021-09-18 11:52:12 +0200533 if (($File::Find::dir =~ m,^/sys/kernel,)) {
534 return grep {!/(debug|tracing)/ } @_;
535 }
536
537 if (($File::Find::dir =~ m,^/sys/fs,)) {
538 return grep {!/(pstore|bpf|fuse)/ } @_;
539 }
540
541 return @_
542}
543
544my %leaf;
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200545my %aliases;
546my @files;
Mauro Carvalho Chehabf090db42021-09-18 11:52:12 +0200547
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200548my $escape_symbols = qr { ([\x01-\x08\x0e-\x1f\x21-\x29\x2b-\x2d\x3a-\x40\x7b-\xfe]) }x;
Mauro Carvalho Chehabf090db42021-09-18 11:52:12 +0200549sub parse_existing_sysfs {
550 my $file = $File::Find::name;
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200551 my $mode = (lstat($file))[2];
552 my $abs_file = abs_path($file);
Mauro Carvalho Chehabf090db42021-09-18 11:52:12 +0200553
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200554 if (S_ISLNK($mode)) {
555 $aliases{$file} = $abs_file;
Mauro Carvalho Chehabf090db42021-09-18 11:52:12 +0200556 return;
557 }
558
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200559 return if (S_ISDIR($mode));
560
561 # Trivial: file is defined exactly the same way at ABI What:
562 return if (defined($data{$file}));
563 return if (defined($data{$abs_file}));
564
565 push @files, $abs_file;
566}
567
568sub check_undefined_symbols {
569 foreach my $file (sort @files) {
570
571 # sysfs-module is special, as its definitions are inside
572 # a text. For now, just ignore them.
573 next if ($file =~ m#^/sys/module/#);
574
575 # Ignore cgroup and firmware
576 next if ($file =~ m#^/sys/(fs/cgroup|firmware)/#);
577
578 my $defined = 0;
579 my $exact = 0;
580 my $whats = "";
581
582 my $leave = $file;
583 $leave =~ s,.*/,,;
584
585 my $path = $file;
586 $path =~ s,(.*/).*,$1,;
587
588 if (defined($leaf{$leave})) {
589 my $what = $leaf{$leave};
590 $whats .= " $what" if (!($whats =~ m/$what/));
591
592 foreach my $w (split / /, $what) {
593 if ($file =~ m#^$w$#) {
594 $exact = 1;
595 last;
596 }
597 }
598 # Check for aliases
599 #
600 # TODO: this algorithm is O(w * n²). It can be
601 # improved in the future in order to handle it
602 # faster, by changing parse_existing_sysfs to
603 # store the sysfs inside a tree, at the expense
604 # on making the code less readable and/or using some
605 # additional perl library.
606 foreach my $a (keys %aliases) {
607 my $new = $aliases{$a};
608 my $len = length($new);
609
610 if (substr($file, 0, $len) eq $new) {
611 my $newf = $a . substr($file, $len);
612
613 foreach my $w (split / /, $what) {
614 if ($newf =~ m#^$w$#) {
615 $exact = 1;
616 last;
617 }
618 }
619 }
620 }
621
622 $defined++;
623 }
624 next if ($exact);
625
626 # Ignore some sysfs nodes
627 next if ($file =~ m#/(sections|notes)/#);
628
629 # Would need to check at
630 # Documentation/admin-guide/kernel-parameters.txt, but this
631 # is not easily parseable.
632 next if ($file =~ m#/parameters/#);
633
634 if ($hint && $defined) {
635 print "$leave at $path might be one of:$whats\n";
636 next;
637 }
638 print "$file not found.\n";
639 }
Mauro Carvalho Chehabf090db42021-09-18 11:52:12 +0200640}
641
642sub undefined_symbols {
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200643 find({
644 wanted =>\&parse_existing_sysfs,
645 preprocess =>\&dont_parse_special_attributes,
646 no_chdir => 1
647 }, $sysfs_prefix);
648
Mauro Carvalho Chehabf090db42021-09-18 11:52:12 +0200649 foreach my $w (sort keys %data) {
650 foreach my $what (split /\xac /,$w) {
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200651 next if (!($what =~ m/^$sysfs_prefix/));
652
653 # Convert what into regular expressions
654
655 $what =~ s,/\.\.\./,/*/,g;
656 $what =~ s,\*,.*,g;
657
658 # Temporarily change [0-9]+ type of patterns
659 $what =~ s/\[0\-9\]\+/\xff/g;
660
661 # Temporarily change [\d+-\d+] type of patterns
662 $what =~ s/\[0\-\d+\]/\xff/g;
663 $what =~ s/\[(\d+)\]/\xf4$1\xf5/g;
664
665 # Temporarily change [0-9] type of patterns
666 $what =~ s/\[(\d)\-(\d)\]/\xf4$1-$2\xf5/g;
667
668 # Handle multiple option patterns
669 $what =~ s/[\{\<\[]([\w_]+)(?:[,|]+([\w_]+)){1,}[\}\>\]]/($1|$2)/g;
670
671 # Handle wildcards
672 $what =~ s/\<[^\>]+\>/.*/g;
673 $what =~ s/\{[^\}]+\}/.*/g;
674 $what =~ s/\[[^\]]+\]/.*/g;
675
676 $what =~ s/[XYZ]/.*/g;
677
678 # Recover [0-9] type of patterns
679 $what =~ s/\xf4/[/g;
680 $what =~ s/\xf5/]/g;
681
682 # Remove duplicated spaces
683 $what =~ s/\s+/ /g;
684
685 # Special case: this ABI has a parenthesis on it
686 $what =~ s/sqrt\(x^2\+y^2\+z^2\)/sqrt\(x^2\+y^2\+z^2\)/;
687
688 # Special case: drop comparition as in:
689 # What: foo = <something>
690 # (this happens on a few IIO definitions)
691 $what =~ s,\s*\=.*$,,;
692
Mauro Carvalho Chehabf090db42021-09-18 11:52:12 +0200693 my $leave = $what;
694 $leave =~ s,.*/,,;
695
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200696 next if ($leave =~ m/^\.\*/ || $leave eq "");
697
698 # Escape all other symbols
699 $what =~ s/$escape_symbols/\\$1/g;
700 $what =~ s/\\\\/\\/g;
701 $what =~ s/\\([\[\]\(\)\|])/$1/g;
702 $what =~ s/(\d+)\\(-\d+)/$1$2/g;
703
704 $leave =~ s/[\(\)]//g;
705
706 foreach my $l (split /\|/, $leave) {
707 if (defined($leaf{$l})) {
708 next if ($leaf{$l} =~ m/$what/);
709 $leaf{$l} .= " " . $what;
710 } else {
711 $leaf{$l} = $what;
712 }
Mauro Carvalho Chehabf090db42021-09-18 11:52:12 +0200713 }
714 }
715 }
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200716 check_undefined_symbols;
Mauro Carvalho Chehabf090db42021-09-18 11:52:12 +0200717}
718
Mauro Carvalho Chehab61439c42020-10-30 08:40:22 +0100719# Ensure that the prefix will always end with a slash
720# While this is not needed for find, it makes the patch nicer
721# with --enable-lineno
722$prefix =~ s,/?$,/,;
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300723
Mauro Carvalho Chehabf090db42021-09-18 11:52:12 +0200724if ($cmd eq "undefined" || $cmd eq "search") {
725 $show_warnings = 0;
726}
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300727#
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300728# Parses all ABI files located at $prefix dir
729#
730find({wanted =>\&parse_abi, no_chdir => 1}, $prefix);
731
732print STDERR Data::Dumper->Dump([\%data], [qw(*data)]) if ($debug);
733
734#
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300735# Handles the command
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300736#
Mauro Carvalho Chehabf090db42021-09-18 11:52:12 +0200737if ($cmd eq "undefined") {
738 undefined_symbols;
739} elsif ($cmd eq "search") {
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300740 search_symbols;
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100741} else {
742 if ($cmd eq "rest") {
743 output_rest;
744 }
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300745
Mauro Carvalho Chehabc7ba3332020-10-30 08:40:25 +0100746 # Warn about duplicated ABI entries
747 foreach my $what(sort keys %symbols) {
748 my @files = @{$symbols{$what}->{file}};
749
750 next if (scalar(@files) == 1);
751
752 printf STDERR "Warning: $what is defined %d times: @files\n",
753 scalar(@files);
754 }
755}
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300756
757__END__
758
759=head1 NAME
760
761abi_book.pl - parse the Linux ABI files and produce a ReST book.
762
763=head1 SYNOPSIS
764
Mauro Carvalho Chehab61439c42020-10-30 08:40:22 +0100765B<abi_book.pl> [--debug] [--enable-lineno] [--man] [--help]
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200766 [--(no-)rst-source] [--dir=<dir>] [--show-hints]
767 <COMAND> [<ARGUMENT>]
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300768
769Where <COMMAND> can be:
770
771=over 8
772
773B<search> [SEARCH_REGEX] - search for [SEARCH_REGEX] inside ABI
774
Mauro Carvalho Chehab7ce7b892019-06-20 14:23:04 -0300775B<rest> - output the ABI in ReST markup language
776
777B<validate> - validate the ABI contents
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300778
Mauro Carvalho Chehabf090db42021-09-18 11:52:12 +0200779B<undefined> - existing symbols at the system that aren't
780 defined at Documentation/ABI
781
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300782=back
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300783
784=head1 OPTIONS
785
786=over 8
787
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300788=item B<--dir>
789
790Changes the location of the ABI search. By default, it uses
791the Documentation/ABI directory.
792
Mauro Carvalho Chehab11ce90a2020-10-30 08:40:20 +0100793=item B<--rst-source> and B<--no-rst-source>
794
795The input file may be using ReST syntax or not. Those two options allow
796selecting between a rst-compliant source ABI (--rst-source), or a
797plain text that may be violating ReST spec, so it requres some escaping
798logic (--no-rst-source).
799
Mauro Carvalho Chehab61439c42020-10-30 08:40:22 +0100800=item B<--enable-lineno>
801
802Enable output of #define LINENO lines.
803
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300804=item B<--debug>
805
806Put the script in verbose mode, useful for debugging. Can be called multiple
807times, to increase verbosity.
808
Mauro Carvalho Chehabab02c512021-09-18 11:52:13 +0200809=item B<--show-hints>
810
811Show hints about possible definitions for the missing ABI symbols.
812Used only when B<undefined>.
813
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300814=item B<--help>
815
816Prints a brief help message and exits.
817
818=item B<--man>
819
820Prints the manual page and exits.
821
822=back
823
824=head1 DESCRIPTION
825
Mauro Carvalho Chehab33e3e992019-06-20 14:22:59 -0300826Parse the Linux ABI files from ABI DIR (usually located at Documentation/ABI),
827allowing to search for ABI symbols or to produce a ReST book containing
828the Linux ABI documentation.
829
830=head1 EXAMPLES
831
832Search for all stable symbols with the word "usb":
833
834=over 8
835
836$ scripts/get_abi.pl search usb --dir Documentation/ABI/stable
837
838=back
839
840Search for all symbols that match the regex expression "usb.*cap":
841
842=over 8
843
844$ scripts/get_abi.pl search usb.*cap
845
846=back
847
848Output all obsoleted symbols in ReST format
849
850=over 8
851
852$ scripts/get_abi.pl rest --dir Documentation/ABI/obsolete
853
854=back
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300855
856=head1 BUGS
857
Mauro Carvalho Chehab7ce7b892019-06-20 14:23:04 -0300858Report bugs to Mauro Carvalho Chehab <mchehab+samsung@kernel.org>
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300859
860=head1 COPYRIGHT
861
Mauro Carvalho Chehab7ce7b892019-06-20 14:23:04 -0300862Copyright (c) 2016-2019 by Mauro Carvalho Chehab <mchehab+samsung@kernel.org>.
Mauro Carvalho Chehabbbc249f2019-06-20 14:22:55 -0300863
864License GPLv2: GNU GPL version 2 <http://gnu.org/licenses/gpl.html>.
865
866This is free software: you are free to change and redistribute it.
867There is NO WARRANTY, to the extent permitted by law.
868
869=cut