Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 1 | #!/usr/bin/env perl |
| 2 | # SPDX-License-Identifier: GPL-2.0 |
| 3 | # |
Jani Nikula | e893922 | 2017-10-09 18:26:15 +0300 | [diff] [blame] | 4 | # Treewide grep for references to files under Documentation, and report |
| 5 | # non-existing files in stderr. |
| 6 | |
Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 7 | use warnings; |
| 8 | use strict; |
| 9 | use Getopt::Long qw(:config no_auto_abbrev); |
Jani Nikula | e893922 | 2017-10-09 18:26:15 +0300 | [diff] [blame] | 10 | |
Mauro Carvalho Chehab | aeaacbf | 2019-05-29 20:09:28 -0300 | [diff] [blame] | 11 | # 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. |
| 14 | my %false_positives = ( |
Mauro Carvalho Chehab | ce5c5d65 | 2020-03-02 09:16:04 +0100 | [diff] [blame] | 15 | "Documentation/scsi/scsi_mid_low_api.rst" => "Documentation/Configure.help", |
Mauro Carvalho Chehab | aeaacbf | 2019-05-29 20:09:28 -0300 | [diff] [blame] | 16 | "drivers/vhost/vhost.c" => "Documentation/virtual/lguest/lguest.c", |
| 17 | ); |
| 18 | |
Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 19 | my $scriptname = $0; |
| 20 | $scriptname =~ s,.*/([^/]+/),$1,; |
| 21 | |
| 22 | # Parse arguments |
| 23 | my $help = 0; |
| 24 | my $fix = 0; |
Mauro Carvalho Chehab | b1663d7 | 2019-06-04 09:26:27 -0300 | [diff] [blame] | 25 | my $warn = 0; |
| 26 | |
Rob Herring | 709dedf | 2021-06-02 20:28:06 -0500 | [diff] [blame] | 27 | if (! -e ".git") { |
Tiezhu Yang | d98dbbe | 2020-04-14 17:41:48 +0800 | [diff] [blame] | 28 | printf "Warning: can't check if file exists, as this is not a git tree\n"; |
Mauro Carvalho Chehab | b1663d7 | 2019-06-04 09:26:27 -0300 | [diff] [blame] | 29 | exit 0; |
| 30 | } |
Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 31 | |
| 32 | GetOptions( |
| 33 | 'fix' => \$fix, |
Mauro Carvalho Chehab | b1663d7 | 2019-06-04 09:26:27 -0300 | [diff] [blame] | 34 | 'warn' => \$warn, |
Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 35 | 'h|help|usage' => \$help, |
| 36 | ); |
| 37 | |
| 38 | if ($help != 0) { |
Mauro Carvalho Chehab | 40fc3eb | 2018-06-14 07:11:02 -0300 | [diff] [blame] | 39 | print "$scriptname [--help] [--fix]\n"; |
Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 40 | exit -1; |
| 41 | } |
| 42 | |
| 43 | # Step 1: find broken references |
| 44 | print "Finding broken references. This may take a while... " if ($fix); |
| 45 | |
| 46 | my %broken_ref; |
| 47 | |
Mauro Carvalho Chehab | 894ee5f | 2019-04-24 13:25:33 -0300 | [diff] [blame] | 48 | my $doc_fix = 0; |
| 49 | |
| 50 | open IN, "git grep ':doc:\`' Documentation/|" |
| 51 | or die "Failed to run git grep"; |
| 52 | while (<IN>) { |
| 53 | next if (!m,^([^:]+):.*\:doc\:\`([^\`]+)\`,); |
Mauro Carvalho Chehab | 290d5388 | 2020-02-22 10:00:01 +0100 | [diff] [blame] | 54 | next if (m,sphinx/,); |
Mauro Carvalho Chehab | 894ee5f | 2019-04-24 13:25:33 -0300 | [diff] [blame] | 55 | |
Mauro Carvalho Chehab | 290d5388 | 2020-02-22 10:00:01 +0100 | [diff] [blame] | 56 | my $file = $1; |
Mauro Carvalho Chehab | 894ee5f | 2019-04-24 13:25:33 -0300 | [diff] [blame] | 57 | 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 Chehab | 290d5388 | 2020-02-22 10:00:01 +0100 | [diff] [blame] | 65 | if ($f =~ m,^/,) { |
| 66 | $f = "$f.rst"; |
| 67 | $f =~ s,^/,Documentation/,; |
| 68 | } else { |
| 69 | $f = "$d$f.rst"; |
| 70 | } |
Mauro Carvalho Chehab | 894ee5f | 2019-04-24 13:25:33 -0300 | [diff] [blame] | 71 | |
| 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 Chehab | 290d5388 | 2020-02-22 10:00:01 +0100 | [diff] [blame] | 79 | print STDERR "$file: :doc:`$doc_ref`\n"; |
Mauro Carvalho Chehab | 894ee5f | 2019-04-24 13:25:33 -0300 | [diff] [blame] | 80 | } |
| 81 | close IN; |
| 82 | |
Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 83 | open IN, "git grep 'Documentation/'|" |
| 84 | or die "Failed to run git grep"; |
| 85 | while (<IN>) { |
| 86 | next if (!m/^([^:]+):(.*)/); |
| 87 | |
| 88 | my $f = $1; |
| 89 | my $ln = $2; |
| 90 | |
Mauro Carvalho Chehab | fe3e4b9 | 2019-04-22 08:42:02 -0300 | [diff] [blame] | 91 | # On linux-next, discard the Next/ directory |
| 92 | next if ($f =~ m,^Next/,); |
| 93 | |
Mauro Carvalho Chehab | 2d69708 | 2018-06-14 10:47:29 -0300 | [diff] [blame] | 94 | # Makefiles and scripts contain nasty expressions to parse docs |
| 95 | next if ($f =~ m/Makefile/ || $f =~ m/\.sh$/); |
| 96 | |
Mauro Carvalho Chehab | 14efb27 | 2021-10-19 08:42:50 +0100 | [diff] [blame] | 97 | # It doesn't make sense to parse hidden files |
| 98 | next if ($f =~ m#/\.#); |
| 99 | |
Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 100 | # Skip this script |
| 101 | next if ($f eq $scriptname); |
| 102 | |
Mauro Carvalho Chehab | 407b584 | 2019-06-13 07:29:17 -0300 | [diff] [blame] | 103 | # Ignore the dir where documentation will be built |
| 104 | next if ($ln =~ m,\b(\S*)Documentation/output,); |
| 105 | |
Mauro Carvalho Chehab | 2d69708 | 2018-06-14 10:47:29 -0300 | [diff] [blame] | 106 | if ($ln =~ m,\b(\S*)(Documentation/[A-Za-z0-9\_\.\,\~/\*\[\]\?+-]*)(.*),) { |
Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 107 | my $prefix = $1; |
| 108 | my $ref = $2; |
| 109 | my $base = $2; |
Mauro Carvalho Chehab | 2d69708 | 2018-06-14 10:47:29 -0300 | [diff] [blame] | 110 | my $extra = $3; |
Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 111 | |
Mauro Carvalho Chehab | 2d69708 | 2018-06-14 10:47:29 -0300 | [diff] [blame] | 112 | # some file references are like: |
| 113 | # /usr/src/linux/Documentation/DMA-{API,mapping}.txt |
| 114 | # For now, ignore them |
| 115 | next if ($extra =~ m/^{/); |
| 116 | |
| 117 | # Remove footnotes at the end like: |
| 118 | # Documentation/devicetree/dt-object-internal.txt[1] |
| 119 | $ref =~ s/(txt|rst)\[\d+]$/$1/; |
| 120 | |
| 121 | # Remove ending ']' without any '[' |
| 122 | $ref =~ s/\].*// if (!($ref =~ m/\[/)); |
| 123 | |
| 124 | # Remove puntuation marks at the end |
Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 125 | $ref =~ s/[\,\.]+$//; |
| 126 | |
| 127 | my $fulref = "$prefix$ref"; |
| 128 | |
| 129 | $fulref =~ s/^(\<file|ref)://; |
| 130 | $fulref =~ s/^[\'\`]+//; |
| 131 | $fulref =~ s,^\$\(.*\)/,,; |
| 132 | $base =~ s,.*/,,; |
| 133 | |
| 134 | # Remove URL false-positives |
| 135 | next if ($fulref =~ m/^http/); |
| 136 | |
Mauro Carvalho Chehab | 5d395fa | 2018-06-26 06:49:04 -0300 | [diff] [blame] | 137 | # Remove sched-pelt false-positive |
| 138 | next if ($fulref =~ m,^Documentation/scheduler/sched-pelt$,); |
| 139 | |
Mauro Carvalho Chehab | 4ca9bc2 | 2019-06-12 14:52:59 -0300 | [diff] [blame] | 140 | # Discard some build examples from Documentation/target/tcm_mod_builder.rst |
Mauro Carvalho Chehab | d25c063 | 2018-06-26 06:49:03 -0300 | [diff] [blame] | 141 | next if ($fulref =~ m,mnt/sdb/lio-core-2.6.git/Documentation/target,); |
| 142 | |
Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 143 | # Check if exists, evaluating wildcards |
| 144 | next if (grep -e, glob("$ref $fulref")); |
| 145 | |
Mauro Carvalho Chehab | a78513c | 2018-06-14 11:06:08 -0300 | [diff] [blame] | 146 | # Accept relative Documentation patches for tools/ |
| 147 | if ($f =~ m/tools/) { |
| 148 | my $path = $f; |
| 149 | $path =~ s,(.*)/.*,$1,; |
Mauro Carvalho Chehab | 6e74e68d | 2021-10-19 08:42:51 +0100 | [diff] [blame] | 150 | $path =~ s,testing/selftests/bpf,bpf/bpftool,; |
Mauro Carvalho Chehab | 4904aee | 2019-05-29 20:09:29 -0300 | [diff] [blame] | 151 | next if (grep -e, glob("$path/$ref $path/../$ref $path/$fulref")); |
Mauro Carvalho Chehab | a78513c | 2018-06-14 11:06:08 -0300 | [diff] [blame] | 152 | } |
| 153 | |
Mauro Carvalho Chehab | aeaacbf | 2019-05-29 20:09:28 -0300 | [diff] [blame] | 154 | # Discard known false-positives |
| 155 | if (defined($false_positives{$f})) { |
| 156 | next if ($false_positives{$f} eq $fulref); |
| 157 | } |
| 158 | |
Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 159 | if ($fix) { |
Mauro Carvalho Chehab | be600e5 | 2018-06-14 09:36:35 -0300 | [diff] [blame] | 160 | if (!($ref =~ m/(scripts|Kconfig|Kbuild)/)) { |
Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 161 | $broken_ref{$ref}++; |
| 162 | } |
Mauro Carvalho Chehab | b1663d7 | 2019-06-04 09:26:27 -0300 | [diff] [blame] | 163 | } elsif ($warn) { |
| 164 | print STDERR "Warning: $f references a file that doesn't exist: $fulref\n"; |
Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 165 | } else { |
| 166 | print STDERR "$f: $fulref\n"; |
| 167 | } |
| 168 | } |
| 169 | } |
Mauro Carvalho Chehab | 894ee5f | 2019-04-24 13:25:33 -0300 | [diff] [blame] | 170 | close IN; |
Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 171 | |
| 172 | exit 0 if (!$fix); |
| 173 | |
| 174 | # Step 2: Seek for file name alternatives |
| 175 | print "Auto-fixing broken references. Please double-check the results\n"; |
| 176 | |
| 177 | foreach my $ref (keys %broken_ref) { |
| 178 | my $new =$ref; |
| 179 | |
Mauro Carvalho Chehab | 9e78e7f | 2019-05-29 20:09:27 -0300 | [diff] [blame] | 180 | my $basedir = "."; |
| 181 | # On translations, only seek inside the translations directory |
| 182 | $basedir = $1 if ($ref =~ m,(Documentation/translations/[^/]+),); |
| 183 | |
Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 184 | # get just the basename |
| 185 | $new =~ s,.*/,,; |
| 186 | |
Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 187 | my $f=""; |
| 188 | |
Mauro Carvalho Chehab | be600e5 | 2018-06-14 09:36:35 -0300 | [diff] [blame] | 189 | # usual reason for breakage: DT file moved around |
| 190 | if ($ref =~ /devicetree/) { |
Mauro Carvalho Chehab | 0ca862e | 2019-05-29 20:09:30 -0300 | [diff] [blame] | 191 | # usual reason for breakage: DT file renamed to .yaml |
Mauro Carvalho Chehab | be600e5 | 2018-06-14 09:36:35 -0300 | [diff] [blame] | 192 | if (!$f) { |
Mauro Carvalho Chehab | 0ca862e | 2019-05-29 20:09:30 -0300 | [diff] [blame] | 193 | my $new_ref = $ref; |
| 194 | $new_ref =~ s/\.txt$/.yaml/; |
| 195 | $f=$new_ref if (-f $new_ref); |
| 196 | } |
| 197 | |
| 198 | if (!$f) { |
| 199 | my $search = $new; |
| 200 | $search =~ s,^.*/,,; |
Mauro Carvalho Chehab | be600e5 | 2018-06-14 09:36:35 -0300 | [diff] [blame] | 201 | $f = qx(find Documentation/devicetree/ -iname "*$search*") if ($search); |
Mauro Carvalho Chehab | 0ca862e | 2019-05-29 20:09:30 -0300 | [diff] [blame] | 202 | if (!$f) { |
| 203 | # Manufacturer name may have changed |
| 204 | $search =~ s/^.*,//; |
| 205 | $f = qx(find Documentation/devicetree/ -iname "*$search*") if ($search); |
| 206 | } |
Mauro Carvalho Chehab | be600e5 | 2018-06-14 09:36:35 -0300 | [diff] [blame] | 207 | } |
| 208 | } |
Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 209 | |
| 210 | # usual reason for breakage: file renamed to .rst |
| 211 | if (!$f) { |
| 212 | $new =~ s/\.txt$/.rst/; |
Mauro Carvalho Chehab | 9e78e7f | 2019-05-29 20:09:27 -0300 | [diff] [blame] | 213 | $f=qx(find $basedir -iname $new) if ($new); |
Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 214 | } |
| 215 | |
Mauro Carvalho Chehab | e1f319f | 2018-06-14 10:14:54 -0300 | [diff] [blame] | 216 | # usual reason for breakage: use dash or underline |
| 217 | if (!$f) { |
| 218 | $new =~ s/[-_]/[-_]/g; |
Mauro Carvalho Chehab | 9e78e7f | 2019-05-29 20:09:27 -0300 | [diff] [blame] | 219 | $f=qx(find $basedir -iname $new) if ($new); |
Mauro Carvalho Chehab | e1f319f | 2018-06-14 10:14:54 -0300 | [diff] [blame] | 220 | } |
| 221 | |
Mauro Carvalho Chehab | be600e5 | 2018-06-14 09:36:35 -0300 | [diff] [blame] | 222 | # Wild guess: seek for the same name on another place |
| 223 | if (!$f) { |
Mauro Carvalho Chehab | 9e78e7f | 2019-05-29 20:09:27 -0300 | [diff] [blame] | 224 | $f = qx(find $basedir -iname $new) if ($new); |
Mauro Carvalho Chehab | be600e5 | 2018-06-14 09:36:35 -0300 | [diff] [blame] | 225 | } |
| 226 | |
Mauro Carvalho Chehab | d265609 | 2018-05-09 10:18:49 -0300 | [diff] [blame] | 227 | my @find = split /\s+/, $f; |
| 228 | |
| 229 | if (!$f) { |
| 230 | print STDERR "ERROR: Didn't find a replacement for $ref\n"; |
| 231 | } elsif (scalar(@find) > 1) { |
| 232 | print STDERR "WARNING: Won't auto-replace, as found multiple files close to $ref:\n"; |
| 233 | foreach my $j (@find) { |
| 234 | $j =~ s,^./,,; |
| 235 | print STDERR " $j\n"; |
| 236 | } |
| 237 | } else { |
| 238 | $f = $find[0]; |
| 239 | $f =~ s,^./,,; |
| 240 | print "INFO: Replacing $ref to $f\n"; |
| 241 | foreach my $j (qx(git grep -l $ref)) { |
| 242 | qx(sed "s\@$ref\@$f\@g" -i $j); |
| 243 | } |
| 244 | } |
| 245 | } |