blob: c71832b2312b94ba31ef060860bb9c6c04717183 [file] [log] [blame]
Mauro Carvalho Chehabd2656092018-05-09 10:18:49 -03001#!/usr/bin/env perl
2# SPDX-License-Identifier: GPL-2.0
3#
Jani Nikulae8939222017-10-09 18:26:15 +03004# Treewide grep for references to files under Documentation, and report
5# non-existing files in stderr.
6
Mauro Carvalho Chehabd2656092018-05-09 10:18:49 -03007use warnings;
8use strict;
9use Getopt::Long qw(:config no_auto_abbrev);
Jani Nikulae8939222017-10-09 18:26:15 +030010
Mauro Carvalho Chehabaeaacbf2019-05-29 20:09:28 -030011# NOTE: only add things here when the file was gone, but the text wants
12# to mention a past documentation file, for example, to give credits for
13# the original work.
14my %false_positives = (
Mauro Carvalho Chehabce5c5d652020-03-02 09:16:04 +010015 "Documentation/scsi/scsi_mid_low_api.rst" => "Documentation/Configure.help",
Mauro Carvalho Chehabaeaacbf2019-05-29 20:09:28 -030016 "drivers/vhost/vhost.c" => "Documentation/virtual/lguest/lguest.c",
17);
18
Mauro Carvalho Chehabd2656092018-05-09 10:18:49 -030019my $scriptname = $0;
20$scriptname =~ s,.*/([^/]+/),$1,;
21
22# Parse arguments
23my $help = 0;
24my $fix = 0;
Mauro Carvalho Chehabb1663d72019-06-04 09:26:27 -030025my $warn = 0;
26
27if (! -d ".git") {
Tiezhu Yangd98dbbe2020-04-14 17:41:48 +080028 printf "Warning: can't check if file exists, as this is not a git tree\n";
Mauro Carvalho Chehabb1663d72019-06-04 09:26:27 -030029 exit 0;
30}
Mauro Carvalho Chehabd2656092018-05-09 10:18:49 -030031
32GetOptions(
33 'fix' => \$fix,
Mauro Carvalho Chehabb1663d72019-06-04 09:26:27 -030034 'warn' => \$warn,
Mauro Carvalho Chehabd2656092018-05-09 10:18:49 -030035 'h|help|usage' => \$help,
36);
37
38if ($help != 0) {
Mauro Carvalho Chehab40fc3eb2018-06-14 07:11:02 -030039 print "$scriptname [--help] [--fix]\n";
Mauro Carvalho Chehabd2656092018-05-09 10:18:49 -030040 exit -1;
41}
42
43# Step 1: find broken references
44print "Finding broken references. This may take a while... " if ($fix);
45
46my %broken_ref;
47
Mauro Carvalho Chehab894ee5f2019-04-24 13:25:33 -030048my $doc_fix = 0;
49
50open IN, "git grep ':doc:\`' Documentation/|"
51 or die "Failed to run git grep";
52while (<IN>) {
53 next if (!m,^([^:]+):.*\:doc\:\`([^\`]+)\`,);
Mauro Carvalho Chehab290d53882020-02-22 10:00:01 +010054 next if (m,sphinx/,);
Mauro Carvalho Chehab894ee5f2019-04-24 13:25:33 -030055
Mauro Carvalho Chehab290d53882020-02-22 10:00:01 +010056 my $file = $1;
Mauro Carvalho Chehab894ee5f2019-04-24 13:25:33 -030057 my $d = $1;
58 my $doc_ref = $2;
59
60 my $f = $doc_ref;
61
62 $d =~ s,(.*/).*,$1,;
63 $f =~ s,.*\<([^\>]+)\>,$1,;
64
Mauro Carvalho Chehab290d53882020-02-22 10:00:01 +010065 if ($f =~ m,^/,) {
66 $f = "$f.rst";
67 $f =~ s,^/,Documentation/,;
68 } else {
69 $f = "$d$f.rst";
70 }
Mauro Carvalho Chehab894ee5f2019-04-24 13:25:33 -030071
72 next if (grep -e, glob("$f"));
73
74 if ($fix && !$doc_fix) {
75 print STDERR "\nWARNING: Currently, can't fix broken :doc:`` fields\n";
76 }
77 $doc_fix++;
78
Mauro Carvalho Chehab290d53882020-02-22 10:00:01 +010079 print STDERR "$file: :doc:`$doc_ref`\n";
Mauro Carvalho Chehab894ee5f2019-04-24 13:25:33 -030080}
81close IN;
82
Mauro Carvalho Chehabd2656092018-05-09 10:18:49 -030083open IN, "git grep 'Documentation/'|"
84 or die "Failed to run git grep";
85while (<IN>) {
86 next if (!m/^([^:]+):(.*)/);
87
88 my $f = $1;
89 my $ln = $2;
90
Mauro Carvalho Chehabfe3e4b92019-04-22 08:42:02 -030091 # On linux-next, discard the Next/ directory
92 next if ($f =~ m,^Next/,);
93
Mauro Carvalho Chehab2d697082018-06-14 10:47:29 -030094 # Makefiles and scripts contain nasty expressions to parse docs
95 next if ($f =~ m/Makefile/ || $f =~ m/\.sh$/);
96
Mauro Carvalho Chehabd2656092018-05-09 10:18:49 -030097 # Skip this script
98 next if ($f eq $scriptname);
99
Mauro Carvalho Chehab407b5842019-06-13 07:29:17 -0300100 # Ignore the dir where documentation will be built
101 next if ($ln =~ m,\b(\S*)Documentation/output,);
102
Mauro Carvalho Chehab2d697082018-06-14 10:47:29 -0300103 if ($ln =~ m,\b(\S*)(Documentation/[A-Za-z0-9\_\.\,\~/\*\[\]\?+-]*)(.*),) {
Mauro Carvalho Chehabd2656092018-05-09 10:18:49 -0300104 my $prefix = $1;
105 my $ref = $2;
106 my $base = $2;
Mauro Carvalho Chehab2d697082018-06-14 10:47:29 -0300107 my $extra = $3;
Mauro Carvalho Chehabd2656092018-05-09 10:18:49 -0300108
Mauro Carvalho Chehab2d697082018-06-14 10:47:29 -0300109 # some file references are like:
110 # /usr/src/linux/Documentation/DMA-{API,mapping}.txt
111 # For now, ignore them
112 next if ($extra =~ m/^{/);
113
114 # Remove footnotes at the end like:
115 # Documentation/devicetree/dt-object-internal.txt[1]
116 $ref =~ s/(txt|rst)\[\d+]$/$1/;
117
118 # Remove ending ']' without any '['
119 $ref =~ s/\].*// if (!($ref =~ m/\[/));
120
121 # Remove puntuation marks at the end
Mauro Carvalho Chehabd2656092018-05-09 10:18:49 -0300122 $ref =~ s/[\,\.]+$//;
123
124 my $fulref = "$prefix$ref";
125
126 $fulref =~ s/^(\<file|ref)://;
127 $fulref =~ s/^[\'\`]+//;
128 $fulref =~ s,^\$\(.*\)/,,;
129 $base =~ s,.*/,,;
130
131 # Remove URL false-positives
132 next if ($fulref =~ m/^http/);
133
Mauro Carvalho Chehab5d395fa2018-06-26 06:49:04 -0300134 # Remove sched-pelt false-positive
135 next if ($fulref =~ m,^Documentation/scheduler/sched-pelt$,);
136
Mauro Carvalho Chehab4ca9bc22019-06-12 14:52:59 -0300137 # Discard some build examples from Documentation/target/tcm_mod_builder.rst
Mauro Carvalho Chehabd25c0632018-06-26 06:49:03 -0300138 next if ($fulref =~ m,mnt/sdb/lio-core-2.6.git/Documentation/target,);
139
Mauro Carvalho Chehabd2656092018-05-09 10:18:49 -0300140 # Check if exists, evaluating wildcards
141 next if (grep -e, glob("$ref $fulref"));
142
Mauro Carvalho Chehaba78513c2018-06-14 11:06:08 -0300143 # Accept relative Documentation patches for tools/
144 if ($f =~ m/tools/) {
145 my $path = $f;
146 $path =~ s,(.*)/.*,$1,;
Mauro Carvalho Chehab4904aee2019-05-29 20:09:29 -0300147 next if (grep -e, glob("$path/$ref $path/../$ref $path/$fulref"));
Mauro Carvalho Chehaba78513c2018-06-14 11:06:08 -0300148 }
149
Mauro Carvalho Chehabaeaacbf2019-05-29 20:09:28 -0300150 # Discard known false-positives
151 if (defined($false_positives{$f})) {
152 next if ($false_positives{$f} eq $fulref);
153 }
154
Mauro Carvalho Chehabd2656092018-05-09 10:18:49 -0300155 if ($fix) {
Mauro Carvalho Chehabbe600e52018-06-14 09:36:35 -0300156 if (!($ref =~ m/(scripts|Kconfig|Kbuild)/)) {
Mauro Carvalho Chehabd2656092018-05-09 10:18:49 -0300157 $broken_ref{$ref}++;
158 }
Mauro Carvalho Chehabb1663d72019-06-04 09:26:27 -0300159 } elsif ($warn) {
160 print STDERR "Warning: $f references a file that doesn't exist: $fulref\n";
Mauro Carvalho Chehabd2656092018-05-09 10:18:49 -0300161 } else {
162 print STDERR "$f: $fulref\n";
163 }
164 }
165}
Mauro Carvalho Chehab894ee5f2019-04-24 13:25:33 -0300166close IN;
Mauro Carvalho Chehabd2656092018-05-09 10:18:49 -0300167
168exit 0 if (!$fix);
169
170# Step 2: Seek for file name alternatives
171print "Auto-fixing broken references. Please double-check the results\n";
172
173foreach my $ref (keys %broken_ref) {
174 my $new =$ref;
175
Mauro Carvalho Chehab9e78e7f2019-05-29 20:09:27 -0300176 my $basedir = ".";
177 # On translations, only seek inside the translations directory
178 $basedir = $1 if ($ref =~ m,(Documentation/translations/[^/]+),);
179
Mauro Carvalho Chehabd2656092018-05-09 10:18:49 -0300180 # get just the basename
181 $new =~ s,.*/,,;
182
Mauro Carvalho Chehabd2656092018-05-09 10:18:49 -0300183 my $f="";
184
Mauro Carvalho Chehabbe600e52018-06-14 09:36:35 -0300185 # usual reason for breakage: DT file moved around
186 if ($ref =~ /devicetree/) {
Mauro Carvalho Chehab0ca862e2019-05-29 20:09:30 -0300187 # usual reason for breakage: DT file renamed to .yaml
Mauro Carvalho Chehabbe600e52018-06-14 09:36:35 -0300188 if (!$f) {
Mauro Carvalho Chehab0ca862e2019-05-29 20:09:30 -0300189 my $new_ref = $ref;
190 $new_ref =~ s/\.txt$/.yaml/;
191 $f=$new_ref if (-f $new_ref);
192 }
193
194 if (!$f) {
195 my $search = $new;
196 $search =~ s,^.*/,,;
Mauro Carvalho Chehabbe600e52018-06-14 09:36:35 -0300197 $f = qx(find Documentation/devicetree/ -iname "*$search*") if ($search);
Mauro Carvalho Chehab0ca862e2019-05-29 20:09:30 -0300198 if (!$f) {
199 # Manufacturer name may have changed
200 $search =~ s/^.*,//;
201 $f = qx(find Documentation/devicetree/ -iname "*$search*") if ($search);
202 }
Mauro Carvalho Chehabbe600e52018-06-14 09:36:35 -0300203 }
204 }
Mauro Carvalho Chehabd2656092018-05-09 10:18:49 -0300205
206 # usual reason for breakage: file renamed to .rst
207 if (!$f) {
208 $new =~ s/\.txt$/.rst/;
Mauro Carvalho Chehab9e78e7f2019-05-29 20:09:27 -0300209 $f=qx(find $basedir -iname $new) if ($new);
Mauro Carvalho Chehabd2656092018-05-09 10:18:49 -0300210 }
211
Mauro Carvalho Chehabe1f319f2018-06-14 10:14:54 -0300212 # usual reason for breakage: use dash or underline
213 if (!$f) {
214 $new =~ s/[-_]/[-_]/g;
Mauro Carvalho Chehab9e78e7f2019-05-29 20:09:27 -0300215 $f=qx(find $basedir -iname $new) if ($new);
Mauro Carvalho Chehabe1f319f2018-06-14 10:14:54 -0300216 }
217
Mauro Carvalho Chehabbe600e52018-06-14 09:36:35 -0300218 # Wild guess: seek for the same name on another place
219 if (!$f) {
Mauro Carvalho Chehab9e78e7f2019-05-29 20:09:27 -0300220 $f = qx(find $basedir -iname $new) if ($new);
Mauro Carvalho Chehabbe600e52018-06-14 09:36:35 -0300221 }
222
Mauro Carvalho Chehabd2656092018-05-09 10:18:49 -0300223 my @find = split /\s+/, $f;
224
225 if (!$f) {
226 print STDERR "ERROR: Didn't find a replacement for $ref\n";
227 } elsif (scalar(@find) > 1) {
228 print STDERR "WARNING: Won't auto-replace, as found multiple files close to $ref:\n";
229 foreach my $j (@find) {
230 $j =~ s,^./,,;
231 print STDERR " $j\n";
232 }
233 } else {
234 $f = $find[0];
235 $f =~ s,^./,,;
236 print "INFO: Replacing $ref to $f\n";
237 foreach my $j (qx(git grep -l $ref)) {
238 qx(sed "s\@$ref\@$f\@g" -i $j);
239 }
240 }
241}