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