chiark / gitweb /
Change target-dir option
[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   $oot_dir //= 'Build';
268 }
269
270 our %manifests;
271 our %packagemap;
272
273 sub read_manifest ($) {
274   my ($subdir) = @_;
275   my $manifest = "../$subdir/Cargo.toml";
276   print STDERR "$self: reading $manifest...\n" if $verbose>=4;
277   if (defined $manifests{$manifest}) {
278     print STDERR
279  "$self: warning: $subdir: specified more than once!\n";
280     return undef;
281   }
282   foreach my $try ("$manifest.unnailed", "$manifest") {
283     my $toml = toml_or_enoent($try, "package manifest") // next;
284     my $p = $toml->{package}{name};
285     if (!defined $p) {
286       print STDERR
287  "$self: warning: $subdir: missing package.name in $try, ignoring\n";
288       next;
289     }
290     $manifests{$manifest} = $toml;
291     return $p;
292   }
293   return undef;
294 }
295
296 sub readorigs () {
297   foreach my $p (keys %{ $nail->{packages} }) {
298     my $v = $nail->{packages}{$p};
299     my $subdir = ref($v) ? $v->{subdir} : $v;
300     my $gotpackage = read_manifest($subdir) // '<nothing!>';
301     if ($gotpackage ne $p) {
302       print STDERR
303  "$self: warning: honouring Cargo.nail packages.$subdir=$p even though $subdir contains package $gotpackage!\n";
304     }
305     die if defined $packagemap{$p};
306     $packagemap{$p} = $subdir;
307   }
308   foreach my $subdir (@{ $nail->{subdirs} }) {
309     my $gotpackage = read_manifest($subdir);
310     if (!defined $gotpackage) {
311       print STDERR
312  "$self: warning: ignoring subdir $subdir which has no Cargo.toml\n";
313       next;
314     }
315     $packagemap{$gotpackage} //= $subdir;
316   }
317 }
318
319 sub calculate () {
320   foreach my $p (sort keys %packagemap) {
321     print STDERR "$self: package $p in $packagemap{$p}\n" if $verbose>=2;
322   }
323   foreach my $mf (keys %manifests) {
324     my $toml = $manifests{$mf};
325     foreach my $k (qw(dependencies build-dependencies dev-dependencies)) {
326       my $deps = $toml->{$k};
327       next unless $deps;
328       foreach my $p (keys %packagemap) {
329         my $info = $deps->{$p};
330         next unless defined $info;
331         $deps->{$p} = $info = { } unless ref $info;
332         delete $info->{version};
333         $info->{path} = $worksphere.'/'.$packagemap{$p};
334       }
335     }
336     my $nailing = "$mf.nailing~";
337     unlink_or_enoent $nailing or die "$self: remove old $nailing: $!\n";
338     open N, '>', $nailing or die "$self: create new $nailing: $!\n";
339     print N to_toml($toml) or die "$self: write new $nailing: $!\n";
340     close N or die "$self: close new $nailing: $!\n";
341   }
342 }
343
344 sub addargs () {
345   $online //= cfg_bool qw(misc online);
346
347   if (@ARGV>=2 &&
348       $ARGV[0] =~ m{\bcargo\b}) {
349     if ($ARGV[1] =~ m/^(?:generate-lockfile|update)$/) {
350       $cargo_lock_update //= 1;
351       $target = undef;
352     }
353     if ($ARGV[1] =~ m/^(?:fetch)$/) {
354       $cargo_target_arg=0;
355       $online //= 1;
356     }
357   }
358   $cargo_lock_update //= 0;
359   $cargo_manifest_args //=
360     (defined $oot_dir) && !$cargo_lock_update;
361
362   $online //= 0;
363
364   if ($cargo_manifest_args) {
365     push @ARGV, "--manifest-path=${src_absdir}/Cargo.toml",
366       qw(--locked);
367     push @ARGV, qw(--target-dir=target) if $cargo_target_arg;
368   }
369
370   if (defined $target) {
371     if ($target =~ m{^[A-Z]}) {
372       $target = (cfgs 'arch', $target) // $archmap{$target}
373         // die "$self: --target=$target alias specified; not in cfg or map\n";
374     }
375     push @ARGV, "--target=$target";
376   }
377
378   push @ARGV, "--offline" unless $online;
379 }
380
381 our $oot_absdir;
382 our $build_absdir; # .../Build/<subdir>
383
384 sub oot_massage_cmdline () {
385   return unless defined $oot_dir;
386
387   my $use = cfgs qw(oot use);
388   $oot_absdir = ($oot_dir !~ m{^/} ? "$worksphere/" : ""). $oot_dir;
389   $build_absdir = "$oot_absdir/$subdir";
390
391   my ($pre,$post);
392   my @xargs;
393   if (!$cargo_lock_update) {
394     push @xargs, $build_absdir;
395     ($pre, $post) = ('cd "$1"; shift; ', '');
396   } else {
397     push @xargs, $oot_absdir, $subdir, $src_absdir;
398     $pre =  <<'END';
399         cd "$1"; shift;
400         mkdir -p -- "$1"; cd "$1"; shift;
401         cp -- "$1"/Cargo.toml
402 END
403     $pre .= <<'ENDLK' if stat_exists 'Cargo.lock', 'working cargo lockfile';
404               "$1"/Cargo.lock
405 ENDLK
406     $pre .= <<'ENDCP';
407                               .;
408 ENDCP
409     $pre .= <<'ENDPRE';
410         shift;
411         mkdir -p src; >src/lib.rs; >build.rs
412 ENDPRE
413     $post = <<'ENDPOST';
414         rm -r src Cargo.toml build.rs;
415 ENDPOST
416   }
417   my $addpath = (cfg_uc qw(oot path_add)) //
418     $use eq 'really' ? Types::Serialiser::true : Types::Serialiser::false;
419   $addpath =
420     !Types::Serialiser::is_bool $addpath ? $addpath           :
421     $addpath                             ? '$HOME/.cargo/bin' :
422                                            undef;
423   if (defined $addpath) {
424     $pre .= <<END
425         PATH=$addpath:\${PATH-/usr/local/bin:/bin:/usr/bin};
426         export PATH;
427 END
428   }
429   $pre  =~ s/^\s+//mg; $pre  =~ s/\s+/ /g;
430   $post =~ s/^\s+//mg; $post =~ s/\s+/ /g;
431
432   my $getuser = sub { cfgsn qw(oot user) };
433   my @command;
434   my $xe = $verbose >= 2 ? 'xe' : 'e';
435   my $sh_ec = sub {
436     if (!length $post) {
437       @command = (@_, 'sh',"-${xe}c",$pre.'exec "$@"','--',@xargs);
438     } else {
439       @command = (@_, 'sh',"-${xe}c",$pre.'"$@"; '.$post,'--',@xargs);
440     }
441     push @command, @ARGV;
442   };
443   my $command_sh = sub {
444     my $quoted = join ' ', map {
445       return $_ if !m/\W/;
446       s/\'/\'\\'\'/g;
447       "'$_'"
448     } @ARGV;
449     @command = @_, "set -${xe}; $pre $quoted; $post";
450   };
451   print STDERR "$self: out-of-tree, building in: \`$build_absdir'\n"
452     if $verbose;
453   if ($use eq 'really') {
454     my $user = $getuser->();
455     my @pw = getpwnam $user or die "$self: oot.user \`$user' lookup failed\n";
456     my $homedir = $pw[7];
457     $sh_ec->('really','-u',$user,'env',"HOME=$homedir");
458     print STDERR "$self: using really to run as user \`$user'\n" if $verbose;
459   } elsif ($use eq 'ssh') {
460     my $user = $getuser->();
461     $user .= '@localhost' unless $user =~ m/\@/;
462     $command_sh->('ssh',$user);
463     print STDERR "$self: using ssh to run as \`$user'\n" if $verbose;
464   } elsif ($use eq 'command_args') {
465     my @c = cfgn_list qw(oot command);
466     $sh_ec->(@c);
467     print STDERR "$self: out-of-tree, adverbial command: @c\n" if $verbose;
468   } elsif ($use eq 'command_sh') {
469     my @c = cfgn_list qw(oot command);
470     $command_sh->(@c);
471     print STDERR "$self: out-of-tree, ssh'ish command: @c\n" if $verbose;
472   } elsif ($use eq 'null') {
473     $sh_ec->();
474   } else {
475     die "$self: oot.use mode $use not recognised\n";
476   }
477   die unless @command;
478   @ARGV = @command;
479 }
480
481 sub setenvs () {
482   $ENV{NAILINGCARGO_WORKSPHERE}   = $worksphere;
483   $ENV{NAILINGCARGO_MANIFEST_DIR} = $src_absdir;
484   $ENV{NAILINGCARGO_BUILDSPHERE}  = $oot_absdir;
485   delete $ENV{NAILINGCARGO_BUILDSPHERE} unless $oot_absdir;
486   $ENV{NAILINGCARGO_BUILD_DIR}    = $build_absdir // $src_absdir;
487 }
488
489 our $want_uninstall;
490
491 END {
492   if ($want_uninstall) {
493     local ($?);
494     foreach my $mf (keys %manifests) {
495       eval { uninstall1($mf,1); 1; } or warn "$@";
496     }
497     eval { unaltcargolock(1); 1; } or warn "$@";
498   }
499 }
500
501 our $cleanup_cargo_lock;
502 sub makebackups () {
503   foreach my $mf (keys %manifests) {
504     link "$mf", "$mf.unnailed" or $!==EEXIST
505       or die "$self: make backup link $mf.unnailed: $!\n";
506   }
507
508   if (defined($alt_cargo_lock)) {
509     if (@alt_cargo_lock_stat) {
510       print STDERR "$self: using alt_cargo_lock `$alt_cargo_lock'..."
511         if $verbose>=3;
512       if (link $alt_cargo_lock, 'Cargo.lock') {
513         print STDERR " linked\n" if $verbose>=3;
514       } elsif ($! != EEXIST) {
515         print STDERR "\n" if $verbose>=3;
516         die "$self: make \`Cargo.lock' available as \`$alt_cargo_lock': $!\n";
517       } else {
518         print STDERR "checking quality." if $verbose>=3;
519         my @lock_stat = stat 'Cargo.lock'
520           or die "$self: stat Cargo.lock (for alt check: $!\n";
521         same_file(\@alt_cargo_lock_stat, \@lock_stat)
522           or die
523 "$self: \`Cargo.lock' and alt file \`$alt_cargo_lock' both exist and are not the same file!\n";
524       }
525       $cleanup_cargo_lock = 1;
526     } else {
527       $cleanup_cargo_lock = 1;
528       # If Cargo.lock exists and alt doesn't, that means either
529       # that a previous run was interrupted, or that the user has
530       # messed up.
531     }
532   }
533 }
534
535 sub nailed ($) {
536   my ($mf) = @_;
537   my $nailed  = "$mf.nailed~"; $nailed =~ s{/([^/]+)$}{/.$1} or die;
538   $nailed;
539 }    
540
541 sub install () {
542   my @our_unfound_stab = stat_exists('Cargo.toml', 'local Cargo.toml')
543     ? (stat _) : ();
544   foreach my $mf (keys %manifests) {
545     if (@our_unfound_stab) {
546       if (stat_exists $mf, "manifest in to-be-nailed directory") {
547         my @mf_stab = stat _ ;
548         if ("@mf_stab[0..1]" eq "@our_unfound_stab[0..1]") {
549           @our_unfound_stab = ();
550         }
551       }
552     }
553
554     my $nailing = "$mf.nailing~";
555     my $nailed = nailed($mf);
556     my ($use, $rm);
557     my $diff;
558     if (open NN, '<', $nailed) {
559       $diff = compare($nailing, \*NN);
560       die "$self: compare $nailing and $nailed: $!" if $diff<0;
561     } else {
562       $!==ENOENT or die "$self: check previous $nailed: $!\n";
563       $diff = 1;
564     }
565     if ($diff) {
566       $use = $nailing;
567       $rm  = $nailed;
568     } else {
569       $use = $nailed;
570       $rm  = $nailing;
571     }
572     rename $use, $mf or die "$self: install nailed $use: $!\n";
573     unlink_or_enoent $rm or die "$self: remove old $rm: $!\n";
574     print STDERR "$self: nailed $mf\n" if $verbose>=3;
575   }
576
577   if (@our_unfound_stab) {
578     print STDERR
579  "$self: *WARNING* cwd is not in Cargo.nail thbough it has Cargo.toml!\n";
580   }
581 }
582
583 sub invoke () {
584   my $r = system @ARGV;
585   if (!$r) {
586     return 0;
587   } elsif ($r<0) {
588     print STDERR "$self: could not execute $ARGV[0]: $!\n";
589     return 127;
590   } elsif ($r & 0xff00) {
591     print STDERR "$self: $ARGV[0] failed (exit status $r)\n";
592     return $r >> 8;
593   } else {
594     print STDERR "$self: $ARGV[0] died due to signal! (wait status $r)\n";
595     return 125;
596   }
597 }
598
599 sub cargo_lock_update_after () {
600   if ($cargo_lock_update) {
601     # avoids importing File::Copy and the error handling is about as good
602     $!=0; $?=0;
603     my $r= system qw(cp --), "$build_absdir/Cargo.lock", "Cargo.lock";
604     die "$self: run cp: $! $?" if $r<0 || $r & 0xff;
605     die "$self: failed to update local Cargo.lock (wait status $r)\n" if $r;
606   }
607 }
608
609 sub uninstall1 ($$) {
610   my ($mf, $enoentok) = @_;
611   my $unnailed = "$mf.unnailed";
612   rename $unnailed, $mf or ($enoentok && $!==ENOENT)
613     or die "$self: failed to restore: rename $unnailed back to $mf: $!\n";
614 }
615
616 sub unaltcargolock ($) {
617   my ($enoentok) = @_;
618   return unless $cleanup_cargo_lock;
619   die 'internal error!' unless defined $alt_cargo_lock;
620
621   # we ignore $enoentok because we don't know if one was supposed to
622   # have been created.
623
624   rename('Cargo.lock', $alt_cargo_lock) or $!==ENOENT or die
625  "$self: cleanup: rename possibly-updated \`Cargo.lock' to \`$alt_cargo_lock': $!\n";
626
627   unlink 'Cargo.lock' or $!==ENOENT or die
628  "$self: cleanup: remove \`Cargo.lock' in favour of \`$alt_cargo_lock': $!\n";
629   # ^ this also helps clean up the stupid rename() corner case
630 }
631
632 sub uninstall () {
633   foreach my $mf (keys %manifests) {
634     my $nailed = nailed($mf);
635     link $mf, $nailed or die "$self: preserve (link) $mf as $nailed: $!\n";
636     uninstall1($mf,0);
637   }
638   unaltcargolock(0);
639 }
640
641 while (@ARGV && $ARGV[0] =~ m/^-/) {
642   $_ = shift @ARGV;
643   last if m{^--$};
644   if (m{^-[^-]}) {
645     while (m{^-.}) {
646       if (s{^-v}{-}) {
647         $verbose++;
648       } elsif (s{^-q}{-}) {
649         $verbose=0;
650       } elsif (s{^-n}{-}) {
651         $noact++;
652       } elsif (s{^-D}{-}) {
653         $dump++;
654       } elsif (s{^-A(.+)}{-}s) {
655         $target = $1;
656       } elsif (s{^-([uU])}{-}) {
657         $cargo_lock_update= $1=~m/[a-z]/;
658       } elsif (s{^-([mM])}{-}) {
659         $cargo_manifest_args= $1=~m/[a-z]/;
660       } elsif (s{^-([tT])}{-}) {
661         $cargo_target_arg= $1=~m/[a-z]/;
662       } elsif (s{^-([oO])}{-}) {
663         $online= $1=~m/[a-z]/;
664       } else {
665         die "$self: unknown short option(s) $_\n";
666       }
667     }
668   } elsif (s{^--(?:target|arch)=}{}) {
669     $target = $_;
670   } elsif (m{^--(no-)?cargo-lock-update}) {
671     $cargo_lock_update= !!$1;
672   } elsif (m{^--(no-)?cargo-manifest-args}) {
673     $cargo_manifest_args= !!$1;
674   } elsif (m{^--(no-)?cargo-target-dir-arg}) {
675     $cargo_target_arg= !!$1;
676   } elsif (m{^--(on|off)line$}) {
677     $online = $1 eq 'on';
678   } else {
679     die "$self: unknown long option $_\n";
680   }
681 }
682
683 die "$self: need command to run\n" unless @ARGV || $noact;
684
685 loadconfigs();
686 takelock();
687 readnail();
688 consider_alt_cargo_lock();
689 consider_oot();
690 readorigs();
691 calculate();
692 addargs();
693 our @display_cmd = @ARGV;
694 oot_massage_cmdline();
695 setenvs();
696
697 if ($dump) {
698   eval '
699     use Data::Dumper;
700     print STDERR Dumper(\%manifests) if $dump>=2;
701     print STDERR Dumper(\%packagemap, \@ARGV,
702                         { src_absdir => $src_absdir,
703                           worksphere => $worksphere,
704                           subdir => $subdir,
705                           oot_dir => $oot_dir,
706                           oot_absdir => $oot_absdir,
707                           build_absdir => $build_absdir });
708   ' or die $@;
709 }
710
711 exit 0 if $noact;
712
713 $want_uninstall = 1;
714 makebackups();
715 install();
716
717 printf STDERR "$self: nailed (%s manifests, %s packages)%s\n",
718   (scalar keys %manifests), (scalar keys %packagemap),
719   (defined($alt_cargo_lock) and ", using `$alt_cargo_lock'")
720   if $verbose;
721
722 print STDERR "$self: invoking: @display_cmd\n" if $verbose;
723 my $estatus = invoke();
724
725 cargo_lock_update_after();
726
727 uninstall();
728 $want_uninstall = 0;
729
730 print STDERR "$self: unnailed.  status $estatus.\n" if $verbose;
731
732 exit $estatus;