Allow "INLINEABLE" as a synonym
[ghc-hetmet.git] / darcs-all
1 #!/usr/bin/perl -w
2
3 use strict;
4
5 # Usage:
6 #
7 # ./darcs-all [-q] [-s] [-i] [-r repo]
8 #             [--nofib] [--testsuite] [--checked-out] cmd [darcs flags]
9 #
10 # Applies the darcs command "cmd" to each repository in the tree.
11 #
12 # e.g.
13 #      ./darcs-all -r http://darcs.haskell.org/ghc get
14 #          To get any repos which do not exist in the local tree
15 #
16 #      ./darcs-all -r ~/ghc-validate push
17 #          To push all your repos to the ~/ghc-validate tree
18 #
19 #      ./darcs-all pull -a
20 #          To pull everything from the default repos
21 #
22 #      ./darc-all push --dry-run
23 #          To see what local patches you have relative to the main repos
24 #
25 # -------------- Flags -------------------
26 #   -q says to be quite, and -s to be silent.
27 #
28 #   -i says to ignore darcs errors and move on to the next repository
29 #
30 #   -r repo says to use repo as the location of package repositories
31 #
32 #   --checked-out says that the remote repo is in checked-out layout, as
33 #   opposed to the layout used for the main repo.  By default a repo on
34 #   the local filesystem is assumed to be checked-out, and repos accessed
35 #   via HTTP or SSH are assumed to be in the main repo layout; use
36 #   --checked-out to override the latter.
37 #
38 #   --nofib, --testsuite also get the nofib and testsuite repos respectively
39 #
40 #   The darcs get flag you are most likely to want is --complete. By
41 #   default we pass darcs the --partial flag.
42 #
43 # ------------ Which repos to use -------------
44 # darcs-all uses the following algorithm to decide which remote repos to use
45 #
46 #  It always computes the remote repos from a single base, $repo_base
47 #  How is $repo_base set?  
48 #    If you say "-r repo", then that's $repo_base
49 #    othewise $repo_base is set thus:
50 #       look in _darcs/prefs/defaultrepo, 
51 #       and remove the trailing 'ghc'
52 #
53 #  Then darcs-all iterates over the package found in the file
54 #  ./packages, which has entries like:
55 #         libraries/array  packages/array  darcs
56 #    or, in general
57 #         <local-path>  <remote-path> <vcs>
58
59 #    If $repo_base looks like a local filesystem path, or if you give
60 #    the --checked-out flag, darcs-all works on repos of form
61 #          $repo_base/<local-path>
62 #    otherwise darcs-all works on repos of form
63 #          $repo_base/<remote-path>
64 #    This logic lets you say
65 #      both    darcs-all -r http://darcs.haskell.org/ghc-6.12 pull
66 #      and     darcs-all -r ../HEAD pull
67 #    The latter is called a "checked-out tree".
68
69 # NB: darcs-all *ignores* the defaultrepo of all repos other than the
70 # root one.  So the remote repos must be laid out in one of the two
71 # formats given by <local-path> and <remote-path> in the file 'packages'.
72
73
74 $| = 1; # autoflush stdout after each print, to avoid output after die
75
76 my $defaultrepo;
77
78 my $verbose = 2;
79 my $ignore_failure = 0;
80 my $want_remote_repo = 0;
81 my $checked_out_flag = 0;
82
83 my %tags;
84
85 my @packages;
86
87 # Figure out where to get the other repositories from.
88 sub getrepo {
89     my $basedir = ".";
90     my $repo = $defaultrepo || `cat $basedir/_darcs/prefs/defaultrepo`;
91     chomp $repo;
92
93     my $repo_base;
94     my $checked_out_tree;
95
96     if ($repo =~ /^...*:/) {
97         # HTTP or SSH
98         # Above regex says "at least two chars before the :", to avoid
99         # catching Win32 drives ("C:\").
100         $repo_base = $repo;
101
102         # --checked-out is needed if you want to use a checked-out repo
103         # over SSH or HTTP
104         if ($checked_out_flag) {
105             $checked_out_tree = 1;
106         } else {
107             $checked_out_tree = 0;
108         }
109
110         # Don't drop the last part of the path if specified with -r, as
111         # it expects repos of the form:
112         #
113         #   http://darcs.haskell.org
114         #
115         # rather than
116         #   
117         #   http://darcs.haskell.org/ghc
118         #
119         if (!$defaultrepo) {
120             $repo_base =~ s#/[^/]+/?$##;
121         }
122     }
123     elsif ($repo =~ /^\/|\.\.\/|.:(\/|\\)/) {
124         # Local filesystem, either absolute or relative path
125         # (assumes a checked-out tree):
126         $repo_base = $repo;
127         $checked_out_tree = 1;
128     }
129     else {
130         die "Couldn't work out repo";
131     }
132
133     return $repo_base, $checked_out_tree;
134 }
135
136 sub message {
137     if ($verbose >= 2) {
138         print "@_\n";
139     }
140 }
141
142 sub warning {
143     if ($verbose >= 1) {
144         print "warning: @_\n";
145     }
146 }
147
148 sub darcs {
149     message "== running darcs @_";
150     system ("darcs", @_) == 0
151        or $ignore_failure
152        or die "darcs failed: $?";
153 }
154
155 sub parsePackages {
156     my @repos;
157     my $lineNum;
158
159     my ($repo_base, $checked_out_tree) = getrepo();
160
161     open IN, "< packages" or die "Can't open packages file";
162     @repos = <IN>;
163     close IN;
164
165     @packages = ();
166     $lineNum = 0;
167     foreach (@repos) {
168         chomp;
169         $lineNum++;
170         if (/^([^# ]+) +([^ ]+) +([^ ]+) +([^ ]+) +([^ ]+)$/) {
171             my %line;
172             $line{"localpath"}  = $1;
173             $line{"tag"}        = $2;
174             $line{"remotepath"} = $3;
175             $line{"vcs"}        = $4;
176             $line{"upstream"}   = $5;
177             push @packages, \%line;
178         }
179         elsif (! /^(#.*)?$/) {
180             die "Bad content on line $lineNum of packages file: $_";
181         }
182     }
183 }
184
185 sub darcsall {
186     my $localpath;
187     my $remotepath;
188     my $path;
189     my $tag;
190     my @repos;
191     my $command = $_[0];
192     my $line;
193
194     my ($repo_base, $checked_out_tree) = getrepo();
195
196     for $line (@packages) {
197         $localpath  = $$line{"localpath"};
198         $tag        = $$line{"tag"};
199         $remotepath = $$line{"remotepath"};
200
201         if ($checked_out_tree) {
202             $path = "$repo_base/$localpath";
203         }
204         else {
205             $path = "$repo_base/$remotepath";
206         }
207
208         if (-d "$localpath/_darcs") {
209             if ($want_remote_repo) {
210                 if ($command =~ /^opt/) {
211                     # Allows ./darcs-all optimize --relink
212                     darcs (@_, "--repodir", $localpath, "--sibling=$path");
213                 } else {
214                     darcs (@_, "--repodir", $localpath, $path);
215                 }
216             } else {
217                 darcs (@_, "--repodir", $localpath);
218             }
219         }
220         elsif ($tag eq "-") {
221             message "== Required repo $localpath is missing! Skipping";
222         }
223         else {
224             message "== $localpath repo not present; skipping";
225         }
226     }
227 }
228
229 sub darcsget {
230     my $r_flags;
231     my $localpath;
232     my $remotepath;
233     my $path;
234     my $tag;
235     my @repos;
236     my $line;
237
238     my ($repo_base, $checked_out_tree) = getrepo();
239
240     if (! grep /(?:--complete|--partial|--lazy)/, @_) {
241         warning("adding --partial, to override use --complete");
242         $r_flags = [@_, "--partial"];
243     }
244     else {
245         $r_flags = \@_;
246     }
247
248     for $line (@packages) {
249         $localpath  = $$line{"localpath"};
250         $tag        = $$line{"tag"};
251         $remotepath = $$line{"remotepath"};
252
253         if ($checked_out_tree) {
254             $path = "$repo_base/$localpath";
255         }
256         else {
257             $path = "$repo_base/$remotepath";
258         }
259
260         if (($tag eq "-") || defined($tags{$tag})) {
261             if (-d $localpath) {
262                 warning("$localpath already present; omitting");
263             }
264             else {
265                 darcs (@$r_flags, $path, $localpath);
266             }
267         }
268     }
269 }
270
271 sub darcsupstreampull {
272     my $localpath;
273     my $upstream;
274     my $line;
275
276     for $line (@packages) {
277         $localpath  = $$line{"localpath"};
278         $upstream   = $$line{"upstream"};
279
280         if ($upstream ne "-") {
281             if (-d $localpath) {
282                 darcs ("pull", @_, "--repodir", $localpath, $upstream);
283             }
284         }
285     }
286 }
287
288 sub main {
289     if (! -d "compiler") {
290         die "error: darcs-all must be run from the top level of the ghc tree."
291     }
292
293     while ($#_ ne -1) {
294         my $arg = shift;
295         # We handle -q here as well as lower down as we need to skip over it
296         # if it comes before the darcs command
297         if ($arg eq "-q") {
298             $verbose = 1;
299         }
300         elsif ($arg eq "-s") {
301             $verbose = 0;
302         }
303         elsif ($arg eq "-r") {
304             $defaultrepo = shift;
305         }
306         elsif ($arg eq "-i") {
307             $ignore_failure = 1;
308         }
309         # --nofib tells get to also grab the nofib repo.
310         # It has no effect on the other commands.
311         elsif ($arg eq "--nofib") {
312             $tags{"nofib"} = 1;
313         }
314         # --testsuite tells get to also grab the testsuite repo.
315         # It has no effect on the other commands.
316         elsif ($arg eq "--testsuite") {
317             $tags{"testsuite"} = 1;
318         }
319         # --extralibs tells get to also grab the extra repos.
320         # It has no effect on the other commands.
321         elsif ($arg eq "--extra") {
322             $tags{"extra"} = 1;
323         }
324         elsif ($arg eq "--checked-out") {
325             $checked_out_flag = 1;
326         }
327         else {
328             unshift @_, $arg;
329             if (grep /^-q$/, @_) {
330                 $verbose = 1;
331             }
332             last;
333         }
334     }
335
336     if ($#_ eq -1) {
337         die "What do you want to do?";
338     }
339     my $command = $_[0];
340     parsePackages;
341     if ($command eq "get") {
342         darcsget @_;
343     }
344     elsif ($command eq "upstreampull") {
345         shift;
346         darcsupstreampull @_;
347     }
348     else {
349         if ($command =~ /^(?:w|wh|wha|what|whats|whatsn|whatsne|whatsnew)$/) {
350             # Hack around whatsnew failing if there are no changes
351             $ignore_failure = 1;
352         }
353         if ($command =~ /^(pul|pus|sen|put|opt)/) {
354             $want_remote_repo = 1;
355         }
356         darcsall @_;
357     }
358 }
359
360 END {
361     my $ec = $?;
362
363     message "== Checking for old bytestring repo";
364     if (-d "libraries/bytestring/_darcs") {
365         if ((system "darcs annotate --repodir libraries/bytestring --match 'hash 20080118173113-3fd76-d5b74c04372a297b585ebea4e16d524551ce5035' > /dev/null 2> /dev/null") == 0) {
366             print <<EOF;
367 ============================
368 ATTENTION!
369
370 You have an old bytestring repository in your GHC tree!
371
372 Please remove it (e.g. "rm -r libraries/bytestring"), and the new
373 version of bytestring will be used from a tarball instead.
374 ============================
375 EOF
376         }
377     }
378
379     message "== Checking for bytestring tarball";
380     if (-d "libraries/bytestring" && not -d "libraries/bytestring/_darcs") {
381         print <<EOF;
382 ============================
383 ATTENTION!
384
385 You have an old bytestring in your GHC tree!
386
387 Please remove it (e.g. "rm -r libraries/bytestring"), and then run
388 "./darcs-all get" to get the darcs repository.
389 ============================
390 EOF
391     }
392
393     message "== Checking for unpulled tarball patches";
394     if ((system "darcs annotate --match 'hash 20090930200358-3fd76-cab3bf4a0a9e3902eb6dd41f71712ad3a6a9bcd1' > /dev/null 2> /dev/null") == 0) {
395         print <<EOF;
396 ============================
397 ATTENTION!
398
399 You have the unpulled tarball patches in your GHC tree!
400
401 Please remove them:
402     darcs unpull -p "Use mingw tarballs to get mingw on Windows"
403 and say yes to each patch.
404 ============================
405 EOF
406     }
407
408     $? = $ec;
409 }
410
411 main(@ARGV);
412