chiark / gitweb /
nailing-cargo: Implement new arg parsing rules
[nailing-cargo.git] / nailing-cargo
1 #!/usr/bin/perl -w
2 # nailing-cargo: wrapper to use unpublished local crates
3 # SPDX-License-Identifier: AGPL-3.0-or-later
4
5 our $self;
6
7 use strict;
8 use POSIX;
9 use Types::Serialiser;
10
11 our %archmap = (
12     RPI => 'arm-unknown-linux-gnueabihf',
13 );
14
15 BEGIN {
16   $self = $0;  $self =~ s{^.*/(?=.)}{};
17   my $deref = $0;
18   while ($deref =~ m{^/}) {
19     my $link = readlink $deref;
20     if (!defined $link) {
21       $! == EINVAL
22         or die "$self: checking our script location $deref: $!\n";
23       $deref =~ s{/[^/]+$}{}
24         or die "$self: unexpected script path: $deref\n";
25       unshift @INC, $deref."/TOML-Tiny/lib";
26       last;
27     }
28     last if $link !~ m{^/};
29     $deref = $link;
30   }
31 }
32
33 use Fcntl qw(LOCK_EX);
34 use File::Compare;
35 use TOML::Tiny::Faithful;
36
37 our $src_absdir = getcwd() // die "$self: getcwd failed: $!\n";
38
39 our $worksphere = $src_absdir;
40 $worksphere =~ s{/([^/]+)$}{}
41   or die "$self: cwd \`$worksphere' unsupported!\n";
42 our $subdir = $1; # leafname
43
44 our $lockfile = "../.nailing-cargo.lock";
45
46 our $cargo_subcmd;
47 our $cargo_lock_update;
48 our $cargo_manifest_args;
49 our $cargo_target_arg=1;
50 our $alt_cargo_lock;
51 our $online;
52
53 our @configs;
54 our $verbose=1;
55 our ($noact,$dump);
56 our $target;
57
58 sub read_or_enoent ($) {
59   my ($fn) = @_;
60   if (!open R, '<', $fn) {
61     return undef if $!==ENOENT;
62     die "$self: open $fn: $!\n";
63   }
64   local ($/) = undef;
65   my ($r) = <R> // die "$self: read $fn: $!\n";
66   $r;
67 }
68
69 sub stat_exists ($$) {
70   my ($fn, $what) = @_;
71   if (stat $fn) { return 1; }
72   $!==ENOENT or die "$self: stat $what: $fn: $!\n";
73   return 0;
74 }
75
76 sub toml_or_enoent ($$) {
77   my ($f,$what) = @_;
78   my $toml = read_or_enoent($f) // return;
79   print STDERR "Read TOML from $f\n" if $dump;
80   my ($v,$e) = from_toml($toml);
81   if (!defined $v) {
82     chomp $e;
83     die "$self: parse TOML: $what: $f: $e\n";
84   }
85   die "$e ?" if length $e;
86   $v;
87 }
88
89 sub load1config ($) {
90   my ($f) = @_;
91   my $toml = toml_or_enoent($f, "config file");
92   push @configs, $toml if defined $toml;
93 }
94
95 sub loadconfigs () {
96   my $dotfile = ".nailing-cargo.toml";
97   load1config("../Nailing-Cargo.toml");
98   load1config($dotfile);
99   load1config("$ENV{HOME}/$dotfile") if defined $ENV{HOME};
100   load1config("/etc/nailing-cargo/cfg.toml");
101 }
102
103 sub unlink_or_enoent ($) { unlink $_[0] or $!==ENOENT; }
104
105 sub same_file ($$) {
106   my ($x,$y) = @_;
107   "@$x[0..5]" eq "@$y[0..5]";
108 }
109
110 sub takelock () {
111   for (;;) {
112     open LOCK, ">", $lockfile or die "$self: open/create $lockfile: $!\n";
113     flock LOCK, LOCK_EX or die "$self: lock $lockfile: $!\n";
114     my @fstat = stat LOCK or die "$self: fstat: $!\n";
115     my @stat  = stat $lockfile;
116     if (!@stat) {
117       next if $! == ENOENT;
118       die "$self: stat $lockfile: $!\n";
119     }
120     last if same_file(\@fstat,\@stat);
121   }
122 }
123 sub unlock () {
124   unlink $lockfile or die "$self: removing lockfile: $!\n";
125 }
126
127 our $nail;
128
129 sub badcfg {
130   my $m = pop @_;
131   $" = '.';
132   die "$self: config key \`@_': $m\n";
133 }
134
135 sub cfg_uc {
136   foreach my $cfg (@configs) {
137     my $v = $cfg;
138     foreach my $k (@_) {
139       last unless defined $v;
140       ref($v) eq 'HASH' or badcfg @_, "parent key \`$k' is not a hash";
141       $v = $v->{$k};
142     }
143     return $v if defined $v;
144   }
145   return undef;
146 }
147
148 sub cfge {
149   my $exp = shift @_;
150   my $v = cfg_uc @_;
151   my $got = ref($v) || 'scalar';
152   return $v if !defined($v) || $got eq $exp;
153   badcfg @_, "found \L$got\E, expected \L$exp\E";
154   # ^ toml doesn't make refs to scalars, so this is unambiguous
155 }
156
157 sub cfgn {
158   my $exp = shift @_;
159   (cfge $exp, @_) // badcfg @_, "missing";
160 }
161
162 sub cfgs  { cfge 'scalar', @_ }
163 sub cfgsn { cfgn 'scalar', @_ }
164
165 sub cfg_bool {
166   my $v = cfg_uc @_;
167   return $v if !defined($v) || Types::Serialiser::is_bool $v;
168   badcfg @_, "expected boolean";
169 }
170
171 sub cfgn_list {
172   my $l = cfge 'ARRAY', @_;
173   foreach my $x (@$l) {
174     !ref $x or badcfg @_, "list contains non-scalar element";
175   }
176   @$l
177 }
178
179 sub readnail () {
180   my $nailfile = "../Cargo.nail";
181   open N, '<', $nailfile or die "$self: open $nailfile: $!\n";
182   local ($/) = undef;
183   my $toml = <N> // die "$self: read $nailfile: $!";
184   my $transformed;
185   if ($toml !~ m{^\s*\[/}m &&
186       $toml !~ m{^[^\n\#]*\=}m &&
187       # old non-toml syntax
188       $toml =~ s{^[ \t]*([-_0-9a-z]+)[ \t]+(\S+)[ \t]*$}{$1 = \"$2\"}mig) {
189     $toml =~ s{^}{[packages\]\n};
190     my @sd;
191     $toml =~ s{^[ \t]*\-[ \t]*\=[ \t]*(\"[-_0-9a-z]+\"\n?)$}{
192       push @sd, $1; '';
193     }mige;
194     $toml = "subdirs = [\n".(join '', map { "$_\n" } @sd)."]\n".$toml;
195     $transformed = 1;
196   }
197   my $e;
198   ($nail,$e) = from_toml($toml);
199   if (!defined $nail) {
200     if ($transformed) {
201       $toml =~ s/^/    /mg;
202       print STDERR "$self: $nailfile transformed into TOML:\n$toml\n";
203     }
204     $/="\n"; chomp $e;
205     die "$self: parse $nailfile: $e\n";
206   }
207   die "$e ?" if length $e;
208
209   $nail->{subdirs} //= [ ];
210
211   if (!ref $nail->{subdirs}) {
212     $nail->{subdirs} = [
213       grep /^[^\#]/,
214       map { s/^\s+//; s/\s+$//; $_; }
215       split m{\n},
216       $nail->{subdirs}
217     ];
218   }
219
220   unshift @configs, $nail;
221 }
222
223 our @alt_cargo_lock_stat;
224
225 sub consider_alt_cargo_lock () {
226   my @ck = qw(alt_cargo_lock);
227   # User should *either* have Cargo.lock in .gitignore,
228   # or expect to commit Cargo.lock.example ($alt_cargo_lock)
229
230   $alt_cargo_lock = (cfg_uc @ck);
231
232   my $force = 0;
233   if (defined($alt_cargo_lock) && ref($alt_cargo_lock) eq 'HASH') {
234     $force = cfg_bool qw(alt_cargo_lock force);
235     my @ck = qw(alt_cargo_lock file);
236     $alt_cargo_lock = cfg_uc @ck;
237   }
238   $alt_cargo_lock //= Types::Serialiser::true;
239
240   if (Types::Serialiser::is_bool $alt_cargo_lock) {
241     if (!$alt_cargo_lock) { $alt_cargo_lock = undef; return; }
242     $alt_cargo_lock = 'Cargo.lock.example';
243   }
244
245   if (ref($alt_cargo_lock) || $alt_cargo_lock =~ m{/}) {
246     badcfg @ck, "expected boolean, or leafname";
247   }
248
249   if (!stat_exists $alt_cargo_lock, "alt_cargo_lock") {
250     $alt_cargo_lock = undef unless $force;
251     return;
252   }
253   
254   @alt_cargo_lock_stat = stat _;
255 }
256
257 our $oot_dir;      # oot.dir or "Build"
258
259 sub consider_oot () {
260   $oot_dir = cfgs qw(oot dir);
261   my $use = cfgs qw(oot use);
262   unless (defined($oot_dir) || defined($use)) {
263     die "$self: specified --cargo-lock-update but not out-of-tree build!\n"
264       if $cargo_lock_update;
265     $cargo_lock_update=0;
266     return;
267   }
268   if ($use eq 'disable') {
269     $oot_dir = undef;
270     return;
271   }
272   $oot_dir //= 'Build';
273 }
274
275 our %manifests;
276 our %packagemap;
277
278 sub read_manifest ($) {
279   my ($subdir) = @_;
280   my $manifest = "../$subdir/Cargo.toml";
281   print STDERR "$self: reading $manifest...\n" if $verbose>=4;
282   if (defined $manifests{$manifest}) {
283     print STDERR
284  "$self: warning: $subdir: specified more than once!\n";
285     return undef;
286   }
287   foreach my $try ("$manifest.unnailed", "$manifest") {
288     my $toml = toml_or_enoent($try, "package manifest") // next;
289     my $p = $toml->{package}{name};
290     if (!defined $p) {
291       print STDERR
292  "$self: warning: $subdir: missing package.name in $try, ignoring\n";
293       next;
294     }
295     $manifests{$manifest} = $toml;
296     return $p;
297   }
298   return undef;
299 }
300
301 sub readorigs () {
302   foreach my $p (keys %{ $nail->{packages} }) {
303     my $v = $nail->{packages}{$p};
304     my $subdir = ref($v) ? $v->{subdir} : $v;
305     my $gotpackage = read_manifest($subdir) // '<nothing!>';
306     if ($gotpackage ne $p) {
307       print STDERR
308  "$self: warning: honouring Cargo.nail packages.$subdir=$p even though $subdir contains package $gotpackage!\n";
309     }
310     die if defined $packagemap{$p};
311     $packagemap{$p} = $subdir;
312   }
313   foreach my $subdir (@{ $nail->{subdirs} }) {
314     my $gotpackage = read_manifest($subdir);
315     if (!defined $gotpackage) {
316       print STDERR
317  "$self: warning: ignoring subdir $subdir which has no Cargo.toml\n";
318       next;
319     }
320     $packagemap{$gotpackage} //= $subdir;
321   }
322 }
323
324 sub calculate () {
325   foreach my $p (sort keys %packagemap) {
326     print STDERR "$self: package $p in $packagemap{$p}\n" if $verbose>=2;
327   }
328   foreach my $mf (keys %manifests) {
329     my $toml = $manifests{$mf};
330     foreach my $k (qw(dependencies build-dependencies dev-dependencies)) {
331       my $deps = $toml->{$k};
332       next unless $deps;
333       foreach my $p (keys %packagemap) {
334         my $info = $deps->{$p};
335         next unless defined $info;
336         $deps->{$p} = $info = { } unless ref $info;
337         delete $info->{version};
338         $info->{path} = $worksphere.'/'.$packagemap{$p};
339       }
340     }
341     my $nailing = "$mf.nailing~";
342     unlink_or_enoent $nailing or die "$self: remove old $nailing: $!\n";
343     open N, '>', $nailing or die "$self: create new $nailing: $!\n";
344     print N to_toml($toml) or die "$self: write new $nailing: $!\n";
345     close N or die "$self: close new $nailing: $!\n";
346   }
347 }
348
349 sub addargs () {
350   $online //= cfg_bool qw(misc online);
351
352   if ($cargo_subcmd =~ m/^(?:generate-lockfile|update)$/) {
353     $cargo_lock_update //= 1;
354     $target = undef;
355   }
356   if ($cargo_subcmd =~ m/^(?:fetch)$/) {
357     $cargo_target_arg=0;
358     $online //= 1;
359   }
360   $cargo_lock_update //= 0;
361   $cargo_manifest_args //=
362     (defined $oot_dir) && !$cargo_lock_update;
363
364   $online //= 0;
365
366   if ($cargo_manifest_args) {
367     push @ARGV, "--manifest-path=${src_absdir}/Cargo.toml",
368       qw(--locked);
369     push @ARGV, qw(--target-dir=target) if $cargo_target_arg;
370   }
371
372   if (defined $target) {
373     if ($target =~ m{^[A-Z]}) {
374       $target = (cfgs 'arch', $target) // $archmap{$target}
375         // die "$self: --target=$target alias specified; not in cfg or map\n";
376     }
377     push @ARGV, "--target=$target";
378   }
379
380   push @ARGV, "--offline" unless $online;
381 }
382
383 our $oot_absdir;
384 our $build_absdir; # .../Build/<subdir>
385
386 sub oot_massage_cmdline () {
387   return unless defined $oot_dir;
388
389   my $use = cfgs qw(oot use);
390   $oot_absdir = ($oot_dir !~ m{^/} ? "$worksphere/" : ""). $oot_dir;
391   $build_absdir = "$oot_absdir/$subdir";
392
393   my ($pre,$post);
394   my @xargs;
395   if (!$cargo_lock_update) {
396     push @xargs, $build_absdir;
397     ($pre, $post) = ('cd "$1"; shift; ', '');
398   } else {
399     push @xargs, $oot_absdir, $subdir, $src_absdir;
400     $pre =  <<'END';
401         cd "$1"; shift;
402         mkdir -p -- "$1"; cd "$1"; shift;
403         cp -- "$1"/Cargo.toml
404 END
405     $pre .= <<'ENDLK' if stat_exists 'Cargo.lock', 'working cargo lockfile';
406               "$1"/Cargo.lock
407 ENDLK
408     $pre .= <<'ENDCP';
409                               .;
410 ENDCP
411     $pre .= <<'ENDPRE';
412         shift;
413         mkdir -p src; >src/lib.rs; >build.rs
414 ENDPRE
415     $post = <<'ENDPOST';
416         rm -r src Cargo.toml build.rs;
417 ENDPOST
418   }
419   my $addpath = (cfg_uc qw(oot path_add)) //
420     $use eq 'really' ? Types::Serialiser::true : Types::Serialiser::false;
421   $addpath =
422     !Types::Serialiser::is_bool $addpath ? $addpath           :
423     $addpath                             ? '$HOME/.cargo/bin' :
424                                            undef;
425   if (defined $addpath) {
426     $pre .= <<END
427         PATH=$addpath:\${PATH-/usr/local/bin:/bin:/usr/bin};
428         export PATH;
429 END
430   }
431   $pre  =~ s/^\s+//mg; $pre  =~ s/\s+/ /g;
432   $post =~ s/^\s+//mg; $post =~ s/\s+/ /g;
433
434   my $getuser = sub { cfgsn qw(oot user) };
435   my @command;
436   my $xe = $verbose >= 2 ? 'xe' : 'e';
437   my $sh_ec = sub {
438     if (!length $post) {
439       @command = (@_, 'sh',"-${xe}c",$pre.'exec "$@"','--',@xargs);
440     } else {
441       @command = (@_, 'sh',"-${xe}c",$pre.'"$@"; '.$post,'--',@xargs);
442     }
443     push @command, @ARGV;
444   };
445   my $command_sh = sub {
446     my $quoted = join ' ', map {
447       return $_ if !m/\W/;
448       s/\'/\'\\'\'/g;
449       "'$_'"
450     } @ARGV;
451     @command = @_, "set -${xe}; $pre $quoted; $post";
452   };
453   print STDERR "$self: out-of-tree, building in: \`$build_absdir'\n"
454     if $verbose;
455   if ($use eq 'really') {
456     my $user = $getuser->();
457     my @pw = getpwnam $user or die "$self: oot.user \`$user' lookup failed\n";
458     my $homedir = $pw[7];
459     $sh_ec->('really','-u',$user,'env',"HOME=$homedir");
460     print STDERR "$self: using really to run as user \`$user'\n" if $verbose;
461   } elsif ($use eq 'ssh') {
462     my $user = $getuser->();
463     $user .= '@localhost' unless $user =~ m/\@/;
464     $command_sh->('ssh',$user);
465     print STDERR "$self: using ssh to run as \`$user'\n" if $verbose;
466   } elsif ($use eq 'command_args') {
467     my @c = cfgn_list qw(oot command);
468     $sh_ec->(@c);
469     print STDERR "$self: out-of-tree, adverbial command: @c\n" if $verbose;
470   } elsif ($use eq 'command_sh') {
471     my @c = cfgn_list qw(oot command);
472     $command_sh->(@c);
473     print STDERR "$self: out-of-tree, ssh'ish command: @c\n" if $verbose;
474   } elsif ($use eq 'null') {
475     $sh_ec->();
476   } else {
477     die "$self: oot.use mode $use not recognised\n";
478   }
479   die unless @command;
480   @ARGV = @command;
481 }
482
483 sub setenvs () {
484   $ENV{NAILINGCARGO_WORKSPHERE}   = $worksphere;
485   $ENV{NAILINGCARGO_MANIFEST_DIR} = $src_absdir;
486   $ENV{NAILINGCARGO_BUILDSPHERE}  = $oot_absdir;
487   delete $ENV{NAILINGCARGO_BUILDSPHERE} unless $oot_absdir;
488   $ENV{NAILINGCARGO_BUILD_DIR}    = $build_absdir // $src_absdir;
489 }
490
491 our $want_uninstall;
492
493 END {
494   if ($want_uninstall) {
495     local ($?);
496     foreach my $mf (keys %manifests) {
497       eval { uninstall1($mf,1); 1; } or warn "$@";
498     }
499     eval { unaltcargolock(1); 1; } or warn "$@";
500   }
501 }
502
503 our $cleanup_cargo_lock;
504 sub makebackups () {
505   foreach my $mf (keys %manifests) {
506     link "$mf", "$mf.unnailed" or $!==EEXIST
507       or die "$self: make backup link $mf.unnailed: $!\n";
508   }
509
510   if (defined($alt_cargo_lock)) {
511     if (@alt_cargo_lock_stat) {
512       print STDERR "$self: using alt_cargo_lock `$alt_cargo_lock'..."
513         if $verbose>=3;
514       if (link $alt_cargo_lock, 'Cargo.lock') {
515         print STDERR " linked\n" if $verbose>=3;
516       } elsif ($! != EEXIST) {
517         print STDERR "\n" if $verbose>=3;
518         die "$self: make \`Cargo.lock' available as \`$alt_cargo_lock': $!\n";
519       } else {
520         print STDERR "checking quality." if $verbose>=3;
521         my @lock_stat = stat 'Cargo.lock'
522           or die "$self: stat Cargo.lock (for alt check: $!\n";
523         same_file(\@alt_cargo_lock_stat, \@lock_stat)
524           or die
525 "$self: \`Cargo.lock' and alt file \`$alt_cargo_lock' both exist and are not the same file!\n";
526       }
527       $cleanup_cargo_lock = 1;
528     } else {
529       $cleanup_cargo_lock = 1;
530       # If Cargo.lock exists and alt doesn't, that means either
531       # that a previous run was interrupted, or that the user has
532       # messed up.
533     }
534   }
535 }
536
537 sub nailed ($) {
538   my ($mf) = @_;
539   my $nailed  = "$mf.nailed~"; $nailed =~ s{/([^/]+)$}{/.$1} or die;
540   $nailed;
541 }    
542
543 sub install () {
544   my @our_unfound_stab = stat_exists('Cargo.toml', 'local Cargo.toml')
545     ? (stat _) : ();
546   foreach my $mf (keys %manifests) {
547     if (@our_unfound_stab) {
548       if (stat_exists $mf, "manifest in to-be-nailed directory") {
549         my @mf_stab = stat _ ;
550         if ("@mf_stab[0..1]" eq "@our_unfound_stab[0..1]") {
551           @our_unfound_stab = ();
552         }
553       }
554     }
555
556     my $nailing = "$mf.nailing~";
557     my $nailed = nailed($mf);
558     my ($use, $rm);
559     my $diff;
560     if (open NN, '<', $nailed) {
561       $diff = compare($nailing, \*NN);
562       die "$self: compare $nailing and $nailed: $!" if $diff<0;
563     } else {
564       $!==ENOENT or die "$self: check previous $nailed: $!\n";
565       $diff = 1;
566     }
567     if ($diff) {
568       $use = $nailing;
569       $rm  = $nailed;
570     } else {
571       $use = $nailed;
572       $rm  = $nailing;
573     }
574     rename $use, $mf or die "$self: install nailed $use: $!\n";
575     unlink_or_enoent $rm or die "$self: remove old $rm: $!\n";
576     print STDERR "$self: nailed $mf\n" if $verbose>=3;
577   }
578
579   if (@our_unfound_stab) {
580     print STDERR
581  "$self: *WARNING* cwd is not in Cargo.nail thbough it has Cargo.toml!\n";
582   }
583 }
584
585 sub invoke () {
586   my $r = system @ARGV;
587   if (!$r) {
588     return 0;
589   } elsif ($r<0) {
590     print STDERR "$self: could not execute $ARGV[0]: $!\n";
591     return 127;
592   } elsif ($r & 0xff00) {
593     print STDERR "$self: $ARGV[0] failed (exit status $r)\n";
594     return $r >> 8;
595   } else {
596     print STDERR "$self: $ARGV[0] died due to signal! (wait status $r)\n";
597     return 125;
598   }
599 }
600
601 sub cargo_lock_update_after () {
602   if ($cargo_lock_update) {
603     # avoids importing File::Copy and the error handling is about as good
604     $!=0; $?=0;
605     my $r= system qw(cp --), "$build_absdir/Cargo.lock", "Cargo.lock";
606     die "$self: run cp: $! $?" if $r<0 || $r & 0xff;
607     die "$self: failed to update local Cargo.lock (wait status $r)\n" if $r;
608   }
609 }
610
611 sub uninstall1 ($$) {
612   my ($mf, $enoentok) = @_;
613   my $unnailed = "$mf.unnailed";
614   rename $unnailed, $mf or ($enoentok && $!==ENOENT)
615     or die "$self: failed to restore: rename $unnailed back to $mf: $!\n";
616 }
617
618 sub unaltcargolock ($) {
619   my ($enoentok) = @_;
620   return unless $cleanup_cargo_lock;
621   die 'internal error!' unless defined $alt_cargo_lock;
622
623   # we ignore $enoentok because we don't know if one was supposed to
624   # have been created.
625
626   rename('Cargo.lock', $alt_cargo_lock) or $!==ENOENT or die
627  "$self: cleanup: rename possibly-updated \`Cargo.lock' to \`$alt_cargo_lock': $!\n";
628
629   unlink 'Cargo.lock' or $!==ENOENT or die
630  "$self: cleanup: remove \`Cargo.lock' in favour of \`$alt_cargo_lock': $!\n";
631   # ^ this also helps clean up the stupid rename() corner case
632 }
633
634 sub uninstall () {
635   foreach my $mf (keys %manifests) {
636     my $nailed = nailed($mf);
637     link $mf, $nailed or die "$self: preserve (link) $mf as $nailed: $!\n";
638     uninstall1($mf,0);
639   }
640   unaltcargolock(0);
641 }
642
643 sub parse_args () {
644   my $is_cargo;
645
646   # Loop exit condition:
647   #   $is_cargo is set
648   #   @ARGV contains
649   #    $is_cargo==1   <cargo-command> <cargo-opts> [--] <subcmd>...
650   #    $is_cargo==0   <build-command>...
651
652   for (;;) {
653     @ARGV or die "$self: need cargo subcommand\n";
654
655     $_ = shift @ARGV;
656     my $orgopt = $_;
657
658     my $not_a_nailing_opt = sub { # usage 1
659       unshift @ARGV, $orgopt;
660       unshift @ARGV, 'cargo';
661       $is_cargo = 1;
662       no warnings qw(exiting);
663       last;
664     };
665     $not_a_nailing_opt->() unless m{^-};
666     $not_a_nailing_opt->() if $_ eq '--';
667
668     if ($_ eq '---') { # usage 2 or 3
669       die "$self: --- must be followed by build command\n" unless @ARGV;
670       if ($ARGV[0] eq '--') { # usage 3
671         shift;
672         $is_cargo = 0;
673       } elsif (grep { $_ eq '--' } @ARGV) { # usage 2
674         $is_cargo = 1;
675       } elsif ($ARGV[0] =~ m{[^/]*cargo[^/]*$}) { # usage 2
676         $is_cargo = 1;
677       } else {  # usage 3
678         $is_cargo = 0;
679       }
680       last;
681     }
682     if (m{^-[^-]}) {
683       while (m{^-.}) {
684         if (s{^-v}{-}) {
685           $verbose++;
686         } elsif (s{^-q}{-}) {
687           $verbose=0;
688         } elsif (s{^-n}{-}) {
689           $noact++;
690         } elsif (s{^-D}{-}) {
691           $dump++;
692         } elsif (s{^-A(.+)}{-}s) {
693           $target = $1;
694         } elsif (s{^-([uU])}{-}) {
695           $cargo_lock_update= $1=~m/[a-z]/;
696         } elsif (s{^-([mM])}{-}) {
697           $cargo_manifest_args= $1=~m/[a-z]/;
698         } elsif (s{^-([tT])}{-}) {
699           $cargo_target_arg= $1=~m/[a-z]/;
700         } elsif (s{^-([oO])}{-}) {
701           $online= $1=~m/[a-z]/;
702         } else {
703           die "$self: unknown short option(s) $_\n" unless $_ eq $orgopt;
704           $not_a_nailing_opt->();
705         }
706       }
707     } elsif (s{^--(?:target|arch)=}{}) {
708       $target = $_;
709     } elsif (m{^--(no-)?cargo-lock-update}) {
710       $cargo_lock_update= !!$1;
711     } elsif (m{^--(no-)?cargo-manifest-args}) {
712       $cargo_manifest_args= !!$1;
713     } elsif (m{^--(no-)?cargo-target-dir-arg}) {
714       $cargo_target_arg= !!$1;
715     } elsif (m{^--(on|off)line$}) {
716       $online = $1 eq 'on';
717     } else {
718       $not_a_nailing_opt->();
719     }
720   }
721
722   $is_cargo // die;
723   @ARGV || die;
724
725   if ($is_cargo) {
726     my @cargo_and_opts = shift @ARGV;
727     while (defined($_ = shift @ARGV)) {
728       if (!m{^-}) { unshift @ARGV, $_; last; }
729       if ($_ eq '--') { last; }
730       push @cargo_and_opts, $_;
731     }
732     @ARGV || die "$self: need cargo subcommand\n";
733     $cargo_subcmd = $ARGV[0];
734     unshift @ARGV, @cargo_and_opts;
735   } else {
736     $cargo_subcmd = '';
737   }
738 }
739
740 parse_args();
741 loadconfigs();
742 takelock();
743 readnail();
744 consider_alt_cargo_lock();
745 consider_oot();
746 readorigs();
747 calculate();
748 addargs();
749 our @display_cmd = @ARGV;
750 oot_massage_cmdline();
751 setenvs();
752
753 if ($dump) {
754   eval '
755     use Data::Dumper;
756     print STDERR Dumper(\%manifests) if $dump>=2;
757     print STDERR Dumper(\%packagemap, \@ARGV,
758                         { src_absdir => $src_absdir,
759                           worksphere => $worksphere,
760                           subdir => $subdir,
761                           oot_dir => $oot_dir,
762                           oot_absdir => $oot_absdir,
763                           build_absdir => $build_absdir });
764   ' or die $@;
765 }
766
767 exit 0 if $noact;
768
769 $want_uninstall = 1;
770 makebackups();
771 install();
772
773 printf STDERR "$self: nailed (%s manifests, %s packages)%s\n",
774   (scalar keys %manifests), (scalar keys %packagemap),
775   (defined($alt_cargo_lock) and ", using `$alt_cargo_lock'")
776   if $verbose;
777
778 print STDERR "$self: invoking: @display_cmd\n" if $verbose;
779 my $estatus = invoke();
780
781 cargo_lock_update_after();
782
783 uninstall();
784 $want_uninstall = 0;
785
786 print STDERR "$self: unnailed.  status $estatus.\n" if $verbose;
787
788 exit $estatus;