2 # nailing-cargo: wrapper to use unpublished local crates
3 # SPDX-License-Identifier: AGPL-3.0-or-later
8 nailing-cargo <nailing-opts> <cargo-opts> [--] <subcmd>...
9 nailing-cargo <nailing-opts> --- <cargo> <cargo-opts> [--] <subcmd>...
10 nailing-cargo <nailing-opts> --- [--] <build-command>...
14 -v Increase verbosity. (Default is 1)
16 -D Increase amount of debugging dump.
17 -n "No action": stop after writing Cargo.toml.nailing~
18 everywhere, and do not run any build command
20 -c Do add cargo command line options } default is add if
21 -C Do not add cargo command line options } command is cargo
23 -o --online -O --offline
24 -u --cargo-lock-update -U --no-cargo-lock-update
26 -T<arch> --target=<arch> Specify target architecture
27 -h --help Print this message
28 --man --manual Display complete manual (in w3m)
29 --leave-nailed Leave the nailed Cargo.toml in place
30 --just-linkfarm | --clean-linkfarm | --keep-linkfarm (default)
31 --no-lock Do not take the lock.
33 -s<subcommand> Treat command as `cargo <subcommand>`
34 --subcommand-props=<prop>,... Override command props (see docs)
42 use Types::Serialiser;
43 use File::Glob qw(bsd_glob GLOB_ERR GLOB_BRACE GLOB_NOMAGIC);
48 RPI => 'arm-unknown-linux-gnueabihf',
52 $self = $0; $self =~ s{^.*/(?=.)}{};
55 while ($deref =~ m{^/}) {
56 my $link = readlink $deref;
59 or die "$self: checking our script location $deref: $!\n";
60 $deref =~ s{/[^/]+$}{}
61 or die "$self: unexpected script path: $deref\n";
63 unshift @INC, $deref."/TOML-Tiny/lib";
66 last if $link !~ m{^/};
71 use Fcntl qw(LOCK_EX);
73 use TOML::Tiny::Faithful;
75 our $src_absdir = getcwd() // die "$self: getcwd failed: $!\n";
77 our $worksphere = $src_absdir;
78 $worksphere =~ s{/([^/]+)$}{}
79 or die "$self: cwd \`$worksphere' unsupported!\n";
80 our $subdir = $1; # leafname
82 our $lockfile = "../.nailing-cargo.lock";
86 our $command_is_cargo;
88 our $cargo_lock_update;
98 # build (default) =>[qw( )],
99 'generate-lockfile'=>[qw( lock-update !target !target-dir )],
100 update =>[qw( lock-update !target online )],
101 fetch =>[qw( online !target-dir )],
104 our @subcmd_xprops = qw(!manifest-path !offline !locked);
112 print $usage or die $!;
117 my $manual = ($base_path // '.').'/README.md';
118 stat $manual or die "$self: manual not found at $manual: $!\n";;
119 exec 'sh','-ec', 'pandoc -- "$1" 2>&1 | w3m -T text/html', '--', $manual;
120 die "$self: exec sh failed: $!";
123 sub read_or_enoent ($) {
125 if (!open R, '<', $fn) {
126 return undef if $!==ENOENT;
127 die "$self: open $fn: $!\n";
130 my ($r) = <R> // die "$self: read $fn: $!\n";
134 sub stat_exists ($$) {
135 my ($fn, $what) = @_;
136 if (stat $fn) { return 1; }
137 $!==ENOENT or die "$self: stat $what: $fn: $!\n";
142 print STDERR " subcmd_p ".(join ' ', keys %$cargo_subcmd)." | @_\n"
144 $cargo_subcmd->{$_[0]}
147 sub toml_or_enoent ($$) {
149 my $toml = read_or_enoent($f) // return;
150 print STDERR "Read TOML from $f\n" if $dump;
151 my ($v,$e) = from_toml($toml);
154 die "$self: parse TOML: $what: $f: $e\n";
156 die "$e ?" if length $e;
160 sub load1config ($) {
162 my $toml = toml_or_enoent($f, "config file");
163 push @configs, $toml if defined $toml;
167 my $dotfile = ".nailing-cargo.toml";
168 load1config("../Nailing-Cargo.toml");
169 load1config($dotfile);
170 load1config("$ENV{HOME}/$dotfile") if defined $ENV{HOME};
171 load1config("/etc/nailing-cargo/cfg.toml");
174 sub unlink_or_enoent ($) { unlink $_[0] or $!==ENOENT; }
178 "@$x[0..5]" eq "@$y[0..5]";
182 return unless $do_lock;
185 open LOCK, ">", $lockfile or die "$self: open/create $lockfile: $!\n";
186 flock LOCK, LOCK_EX or die "$self: lock $lockfile: $!\n";
187 my @fstat = stat LOCK or die "$self: fstat: $!\n";
188 my @stat = stat $lockfile;
190 next if $! == ENOENT;
191 die "$self: stat $lockfile: $!\n";
193 last if same_file(\@fstat,\@stat);
197 unlink $lockfile or die "$self: removing lockfile: $!\n";
205 die "$self: config key \`@_': $m\n";
209 foreach my $cfg (@configs) {
212 last unless defined $v;
213 ref($v) eq 'HASH' or badcfg @_, "parent key \`$k' is not a hash";
216 return $v if defined $v;
224 my $got = ref($v) || 'scalar';
225 return $v if !defined($v) || $got eq $exp;
226 badcfg @_, "found \L$got\E, expected \L$exp\E";
227 # ^ toml doesn't make refs to scalars, so this is unambiguous
232 (cfge $exp, @_) // badcfg @_, "missing";
235 sub cfgs { cfge 'scalar', @_ }
236 sub cfgsn { cfgn 'scalar', @_ }
240 return $v if !defined($v) || Types::Serialiser::is_bool $v;
241 badcfg @_, "expected boolean";
245 my $l = cfge 'ARRAY', @_;
246 foreach my $x (@$l) {
247 !ref $x or badcfg @_, "list contains non-scalar element";
253 my $nailfile = "../Cargo.nail";
254 open N, '<', $nailfile or die "$self: open $nailfile: $!\n";
256 my $toml = <N> // die "$self: read $nailfile: $!";
258 if ($toml !~ m{^\s*\[/}m &&
259 $toml !~ m{^[^\n\#]*\=}m &&
260 # old non-toml syntax
261 $toml =~ s{^[ \t]*([-_0-9a-z]+)[ \t]+(\S+)[ \t]*$}{$1 = \"$2\"}mig) {
262 $toml =~ s{^}{[packages\]\n};
264 $toml =~ s{^[ \t]*\-[ \t]*\=[ \t]*(\"[-_0-9a-z]+\"\n?)$}{
267 $toml = "subdirs = [\n".(join '', map { "$_\n" } @sd)."]\n".$toml;
271 ($nail,$e) = from_toml($toml);
272 if (!defined $nail) {
275 print STDERR "$self: $nailfile transformed into TOML:\n$toml\n";
278 die "$self: parse $nailfile: $e\n";
280 die "$e ?" if length $e;
282 $nail->{subdirs} //= [ ];
284 if (!ref $nail->{subdirs}) {
287 map { s/^\s+//; s/\s+$//; $_; }
293 unshift @configs, $nail;
296 sub get_dependency_tables ($) {
298 my @keys = qw(dependencies build-dependencies dev-dependencies);
302 foreach my $k (@keys) {
303 my $deps = $node->{$k};
304 push @r, $deps if $deps;
308 foreach my $target_node (values %{ $toml->{target} // { } }) {
309 $process->($target_node);
314 our @alt_cargo_lock_stat;
316 sub consider_alt_cargo_lock () {
317 my @ck = qw(alt_cargo_lock);
318 # User should *either* have Cargo.lock in .gitignore,
319 # or expect to commit Cargo.lock.example ($alt_cargo_lock)
321 $alt_cargo_lock = (cfg_uc @ck);
324 if (defined($alt_cargo_lock) && ref($alt_cargo_lock) eq 'HASH') {
325 $force = cfg_bool qw(alt_cargo_lock force);
326 my @ck = qw(alt_cargo_lock file);
327 $alt_cargo_lock = cfg_uc @ck;
329 $alt_cargo_lock //= Types::Serialiser::true;
331 if (Types::Serialiser::is_bool $alt_cargo_lock) {
332 if (!$alt_cargo_lock) { $alt_cargo_lock = undef; return; }
333 $alt_cargo_lock = 'Cargo.lock.example';
336 if (ref($alt_cargo_lock) || $alt_cargo_lock =~ m{/}) {
337 badcfg @ck, "expected boolean, or leafname";
340 if (!stat_exists $alt_cargo_lock, "alt_cargo_lock") {
341 $alt_cargo_lock = undef unless $force;
345 @alt_cargo_lock_stat = stat _;
348 our $oot_dir; # oot.dir or "Build"
351 sub consider_oot () {
352 $oot_dir = cfgs qw(oot dir);
353 my $use = cfgs qw(oot use);
354 unless (defined($oot_dir) || defined($use) ||
355 defined(cfg_uc qw(oot user))) {
358 if (($use//'') eq 'disable') {
362 $oot_clean //= cfg_bool qw(oot clean);
363 $oot_dir //= 'Build';
364 $oot_absdir = ($oot_dir !~ m{^/} ? "$worksphere/" : ""). $oot_dir;
372 sub read_manifest ($$$) {
373 my ($subdir, $org_subdir, $why) = @_;
374 my $manifest = "../$subdir/Cargo.toml";
375 print STDERR "$self: reading $manifest...\n" if $verbose>=4;
376 if (defined $manifests{$manifest}) {
378 "$self: warning: $subdir: specified more than once!".
379 " (ignoring $why)\n";
382 foreach my $try ("$manifest.unnailed", "$manifest") {
383 my $toml = toml_or_enoent($try, "manifest, in $why") // next;
384 my $ws = $toml->{workspace};
386 queue_workspace_members($subdir, $org_subdir, $ws, "$subdir, $why");
388 my $p = $toml->{package}{name};
389 if (!defined $p and !defined $ws) {
391 "$self: warning: $subdir, $why: missing package.name in $try, ignoring\n";
394 $manifests{$manifest} = [ $toml, $org_subdir ] if $p;
395 foreach my $dep (get_dependency_tables $toml) {
396 next unless defined $dep->{path};
397 queue_referenced_path($dep->{path}, $org_subdir,
398 "dependency of $subdir, $why");
405 sub queue_workspace_members ($$) {
406 my ($subdir, $org_subdir, $ws_toml, $what) = @_;
407 # We need to (more or less) reimplement the cargo workspace
408 # membership algorithm (see the "workspaces" section of the cargo
409 # reference). How tiresome.
411 # It's not quite the same for us because we aren't interested in
412 # whether cargo thinks things are "in the workspace". But we do
413 # need to do the automatic discover.
415 my @include = @{ $ws_toml->{members} // [ ] };
416 my $exclude = $ws_toml->{exclude} // [ ];
419 s/[^*?0-9a-zA-Z_]/\\$&/g;
424 foreach my $spec (@include) {
425 if ($spec =~ m{^/}) {
427 "$self: warning: absolute workspace member $spec in $what (not nailing, but cargo will probably use it)\n";
430 my $spec_glob = "../$subdir/$spec";
431 my $globflags = GLOB_ERR|GLOB_BRACE|GLOB_NOMAGIC;
432 foreach my $globent (bsd_glob($spec_glob, $globflags)) {
433 next if grep { $globent =~ m{^$_$} } @exclude;
434 queue_referenced_path($globent, $org_subdir,
435 "member of workspace $what");
440 sub queue_referenced_path ($$$) {
441 my ($spec_path, $org_subdir, $why) = @_;
443 qw(realpath), "--relative-to=../$org_subdir", "--", $spec_path
444 or die "$self: fork/pipe/exec for realpath(1)\n";
445 my $rel_path = do { local $/=undef; <REALPATH>; };
447 my $r = close(REALPATH);
448 die "$self: reap realpath: $!\n" if $!;
449 if (!chomp($rel_path) or $?) {
451 "$self: warning: failed to determine realpath for $spec_path in $org_subdir (exit code $?)\n";
454 if ($rel_path =~ m{^\.\./} or $rel_path eq '..') {
456 "$self: warning: $spec_path ($why) points outside $org_subdir, not following so not nailing (although cargo probably will follow it)\n";
460 my $q_subdir = "$org_subdir/$rel_path";
461 print STDERR "$self: making a note to look at $q_subdir, $why)\n"
464 push @queued_paths, [ "$q_subdir", $org_subdir, $why ];
468 foreach my $p (keys %{ $nail->{packages} }) {
469 my $v = $nail->{packages}{$p};
470 my $subdir = ref($v) ? $v->{subdir} : $v;
471 my ($gotpackage, $ws) = read_manifest($subdir, $subdir, "from [packages]");
472 $gotpackage //= '<nothing!>';
473 if ($gotpackage ne $p) {
475 "$self: warning: honouring Cargo.nail packages.$subdir=$p even though $subdir contains package $gotpackage!\n";
477 die if defined $packagemap{$p};
478 $packagemap{$p} = [ $subdir, $subdir ];
480 foreach my $subdir (@{ $nail->{subdirs} }) {
481 my ($gotpackage,$ws) = read_manifest($subdir, $subdir, "from [subdirs]");
482 if (!defined $gotpackage) {
484 "$self: warning: ignoring subdir $subdir which has no (suitable) Cargo.toml\n"
488 $packagemap{$gotpackage} //= [ $subdir, $subdir ];
490 while (my ($subdir, $org_subdir, $why) = @{ shift @queued_paths or [] }) {
491 next if $manifests{"../$subdir/Cargo.toml"};
492 my ($gotpackage, $ws) = read_manifest($subdir, $org_subdir, $why);
493 next unless $gotpackage;
494 $packagemap{$gotpackage} //= [ $subdir, $org_subdir ];
499 foreach my $p (sort keys %packagemap) {
500 print STDERR "$self: package $p in $packagemap{$p}[0]\n" if $verbose>=2;
502 foreach my $mf (keys %manifests) {
503 die "internal error" if $do_nail; # belt and braces
505 my ($toml, $mf_org_subdir) = @{ $manifests{$mf} };
506 foreach my $deps (get_dependency_tables $toml) {
508 foreach my $p (keys %packagemap) {
509 my $info = $deps->{$p};
510 next unless defined $info;
511 next if $packagemap{$p}[1] eq $mf_org_subdir;
512 $deps->{$p} = $info = { } unless ref $info;
513 my $oldpath = $info->{path};
514 delete $info->{version};
515 my $newpath = $worksphere.'/'.$packagemap{$p}[0];
516 print STDERR "in $mf set $p path=$newpath (was ".
517 ($oldpath // '<unset>').")\n"
519 $info->{path} = $newpath;
521 delete $info->{branch};
524 my $nailing = "$mf.nailing~";
525 unlink_or_enoent $nailing or die "$self: remove old $nailing: $!\n";
526 open N, '>', $nailing or die "$self: create new $nailing: $!\n";
527 print N to_toml($toml) or die "$self: write new $nailing: $!\n";
528 close N or die "$self: close new $nailing: $!\n";
533 if ($just_linkfarm) {
534 die "$self: --just-linkfarm but not doing out-of-tree builds!\n"
535 unless defined $oot_dir;
540 if (!defined $online) {
541 $_ = cfg_uc qw(misc online);
543 } elsif (Types::Serialiser::is_bool $_) {
548 } elsif (m/^[1ty]/) { # allow booleanish strings
549 $online = 1; # for less user frustration
550 } elsif (m/^[0fn]/) {
553 badcfg qw(misc online), "expected boolean or 'auto', found '$_'";
556 $online //= 1 if subcmd_p('online');
559 $cargo_lock_update //= subcmd_p('lock-update');
563 if (!$cargo_lock_update) {
564 push @add, qw(--locked) unless subcmd_p('!locked');
565 if (defined($oot_dir) && !subcmd_p('!manifest-path')) {
566 my $cargotoml = "${src_absdir}/Cargo.toml";
567 push @args_preface, "--manifest-path=$cargotoml" if $pass_options;
568 push @add, qw(--target-dir=target) unless subcmd_p('!target-dir');
572 if (defined($target) && !subcmd_p('!target')) {
573 if ($target =~ m{^[A-Z]}) {
574 $target = (cfgs 'arch', $target) // $archmap{$target}
575 // die "$self: --target=$target alias specified; not in cfg or map\n";
577 push @add, "--target=$target";
580 push @add, "--offline" unless $online || subcmd_p('!offline');
582 push @args_preface, @add if $pass_options;
583 die if grep { m/ / } @add;
584 $ENV{NAILINGCARGO_CARGO_OPTIONS} = "@add";
586 unshift @ARGV, @args_preface;
589 our $build_absdir; # .../Build/<subdir>
591 sub oot_massage_cmdline () {
592 return unless defined $oot_dir;
594 my $use = cfgs qw(oot use);
595 $use // die "$self: out-of-tree build, but \`oot.use' not configured\n";
596 $build_absdir = "$oot_absdir/$subdir";
598 my ($pre,$post) = ('','');
600 if (!$cargo_lock_update) {
601 push @xargs, $build_absdir;
602 ($pre, $post) = ('cd "$1"; shift; ', '');
604 push @xargs, $oot_absdir, $subdir, $src_absdir;
607 mkdir -p -- "$1"; cd "$1"; shift;
608 clean () { find -lname "$1/*" -print0 | xargs -0r rm --; }; clean;
609 find "$1" -maxdepth 1 \! -name Cargo.lock -print0 | xargs -0r -I_ ln -sf -- _ .;
611 $pre .= <<'ENDLK' if stat_exists 'Cargo.lock', 'working cargo lockfile';
613 cp -- "$1"/Cargo.lock .;
618 $post = <<'ENDCLEAN' if $oot_clean && !$just_linkfarm;
622 my $addpath = (cfg_uc qw(oot path_add)) //
623 $use eq 'really' ? Types::Serialiser::true : Types::Serialiser::false;
625 !Types::Serialiser::is_bool $addpath ? $addpath :
626 $addpath ? '$HOME/.cargo/bin' :
628 if (defined $addpath) {
630 PATH=$addpath:\${PATH-/usr/local/bin:/bin:/usr/bin};
634 $pre =~ s/^\s+//mg; $pre =~ s/\s+/ /g;
635 $post =~ s/^\s+//mg; $post =~ s/\s+/ /g;
637 my $getuser = sub { cfgsn qw(oot user) };
639 my $xe = $verbose >= 2 ? 'xe' : 'e';
642 @command = (@_, 'sh',"-${xe}c",$pre.'exec "$@"','--',@xargs);
644 @command = (@_, 'sh',"-${xe}c",$pre.'"$@"; '.$post,'--',@xargs);
646 push @command, @ARGV;
648 my $command_sh = sub {
649 my $quoted = join ' ', map {
654 @command = @_, "set -${xe}; $pre $quoted; $post";
656 print STDERR "$self: out-of-tree, building in: \`$build_absdir'\n"
658 if ($use eq 'really') {
659 my $user = $getuser->();
660 my @pw = getpwnam $user or die "$self: oot.user \`$user' lookup failed\n";
661 my $homedir = $pw[7];
662 $sh_ec->('really','-u',$user,'env',"HOME=$homedir");
663 print STDERR "$self: using really to run as user \`$user'\n" if $verbose;
664 } elsif ($use eq 'ssh') {
665 my $user = $getuser->();
666 $user .= '@localhost' unless $user =~ m/\@/;
667 $command_sh->('ssh',$user);
668 print STDERR "$self: using ssh to run as \`$user'\n" if $verbose;
669 } elsif ($use eq 'command_args') {
670 my @c = cfgn_list qw(oot command);
672 print STDERR "$self: out-of-tree, adverbial command: @c\n" if $verbose;
673 } elsif ($use eq 'command_sh') {
674 my @c = cfgn_list qw(oot command);
676 print STDERR "$self: out-of-tree, ssh'ish command: @c\n" if $verbose;
677 } elsif ($use eq 'null') {
680 die "$self: oot.use mode $use not recognised\n";
687 $ENV{CARGO_MANIFEST_DIR} = $src_absdir;
688 $ENV{NAILINGCARGO_MANIFEST_DIR} = $src_absdir;
689 $ENV{NAILINGCARGO_WORKSPHERE} = $worksphere;
690 $ENV{NAILINGCARGO_BUILDSPHERE} = $oot_absdir;
691 delete $ENV{NAILINGCARGO_BUILDSPHERE} unless $oot_absdir;
692 $ENV{NAILINGCARGO_BUILD_DIR} = $build_absdir // $src_absdir;
698 if ($want_uninstall) {
700 foreach my $mf (keys %manifests) {
701 eval { uninstall1($mf,1); 1; } or warn "$@";
703 eval { unaltcargolock(1); 1; } or warn "$@";
707 sub consider_directories () {
708 return unless defined $oot_dir;
709 my $bsubdir = "../$oot_dir/$subdir";
710 return if stat $bsubdir;
711 die "$0: build directory $bsubdir inaccessible\n"
713 return if $cargo_lock_update; # will make it
714 die "$0: build directory $bsubdir does not exist, and not in Cargo.lock update mode!\n";
717 our $cleanup_cargo_lock;
719 foreach my $mf (keys %manifests) {
720 link "$mf", "$mf.unnailed" or $!==EEXIST
721 or die "$self: make backup link $mf.unnailed: $!\n";
724 if (defined($alt_cargo_lock)) {
725 if (@alt_cargo_lock_stat) {
726 print STDERR "$self: using alt_cargo_lock `$alt_cargo_lock'..."
728 if (link $alt_cargo_lock, 'Cargo.lock') {
729 print STDERR " linked\n" if $verbose>=3;
730 } elsif ($! != EEXIST) {
731 print STDERR "\n" if $verbose>=3;
732 die "$self: make \`Cargo.lock' available as \`$alt_cargo_lock': $!\n";
734 print STDERR "checking quality." if $verbose>=3;
735 my @lock_stat = stat 'Cargo.lock'
736 or die "$self: stat Cargo.lock (for alt check: $!\n";
737 same_file(\@alt_cargo_lock_stat, \@lock_stat)
739 "$self: \`Cargo.lock' and alt file \`$alt_cargo_lock' both exist and are not the same file!\n";
741 $cleanup_cargo_lock = 1;
743 $cleanup_cargo_lock = 1;
744 # If Cargo.lock exists and alt doesn't, that means either
745 # that a previous run was interrupted, or that the user has
753 my $nailed = "$mf.nailed~"; $nailed =~ s{/([^/]+)$}{/.$1} or die;
758 my @our_unfound_stab = stat_exists('Cargo.toml', 'local Cargo.toml')
760 foreach my $mf (keys %manifests) {
761 if (@our_unfound_stab) {
762 if (stat_exists $mf, "manifest in to-be-nailed directory") {
763 my @mf_stab = stat _ ;
764 if ("@mf_stab[0..1]" eq "@our_unfound_stab[0..1]") {
765 @our_unfound_stab = ();
770 my $nailing = "$mf.nailing~";
771 my $nailed = nailed($mf);
774 if (open NN, '<', $nailed) {
775 $diff = compare($nailing, \*NN);
776 die "$self: compare $nailing and $nailed: $!" if $diff<0;
778 $!==ENOENT or die "$self: check previous $nailed: $!\n";
788 rename $use, $mf or die "$self: install nailed $use: $!\n";
789 unlink_or_enoent $rm or die "$self: remove old $rm: $!\n";
790 print STDERR "$self: nailed $mf\n" if $verbose>=3;
793 if (@our_unfound_stab) {
795 "$self: *WARNING* cwd is not in Cargo.nail thbough it has Cargo.toml!\n";
800 my $r = system @ARGV;
804 print STDERR "$self: could not execute $ARGV[0]: $!\n";
806 } elsif ($r & 0xff00) {
807 print STDERR "$self: $ARGV[0] failed (exit status $r)\n";
810 print STDERR "$self: $ARGV[0] died due to signal! (wait status $r)\n";
815 sub cargo_lock_update_after () {
816 if ($cargo_lock_update && !$just_linkfarm) {
817 # avoids importing File::Copy and the error handling is about as good
819 my $r= system qw(cp --), "$build_absdir/Cargo.lock", "Cargo.lock";
820 die "$self: run cp: $! $?" if $r<0 || $r & 0xff;
821 die "$self: failed to update local Cargo.lock (wait status $r)\n" if $r;
825 sub uninstall1 ($$) {
826 my ($mf, $enoentok) = @_;
827 my $unnailed = "$mf.unnailed";
828 rename $unnailed, $mf or ($enoentok && $!==ENOENT)
829 or die "$self: failed to restore: rename $unnailed back to $mf: $!\n";
832 sub unaltcargolock ($) {
834 return unless $cleanup_cargo_lock;
835 die 'internal error!' unless defined $alt_cargo_lock;
837 # we ignore $enoentok because we don't know if one was supposed to
840 rename('Cargo.lock', $alt_cargo_lock) or $!==ENOENT or die
841 "$self: cleanup: rename possibly-updated \`Cargo.lock' to \`$alt_cargo_lock': $!\n";
843 unlink 'Cargo.lock' or $!==ENOENT or die
844 "$self: cleanup: remove \`Cargo.lock' in favour of \`$alt_cargo_lock': $!\n";
845 # ^ this also helps clean up the stupid rename() corner case
849 foreach my $mf (keys %manifests) {
850 my $nailed = nailed($mf);
851 link $mf, $nailed or die "$self: preserve (link) $mf as $nailed: $!\n";
860 # Loop exit condition:
863 # $is_cargo==1 <cargo-command> <cargo-opts> [--] <subcmd>...
864 # $is_cargo==0 <build-command>...
868 die "$self: need cargo subcommand\n"
869 unless $noact || $just_linkfarm;;
870 push @ARGV, "CARGO-SUBCOMMAND"; # dummy, user may see it
876 my $not_a_nailing_opt = sub { # usage 1
877 unshift @ARGV, $orgopt;
878 unshift @ARGV, 'cargo';
880 no warnings qw(exiting);
883 $not_a_nailing_opt->() unless m{^-};
884 $not_a_nailing_opt->() if $_ eq '--';
886 if ($_ eq '---') { # usage 2 or 3
888 die "$self: --- must be followed by build command\n" unless $noact;
889 push @ARGV, 'BUILD-COMMAND';
891 if ($ARGV[0] eq '--') { # usage 3
894 } elsif (grep { $_ eq '--' } @ARGV) { # usage 2
896 } elsif ($ARGV[0] =~ m{[^/]*cargo[^/]*$}) { # usage 2
907 } elsif (s{^-v}{-}) {
909 } elsif (s{^-q}{-}) {
911 } elsif (s{^-n}{-}) {
913 } elsif (s{^-s(.+)}{-}s) {
915 } elsif (s{^-([uU])}{-}) {
916 $cargo_lock_update = $1=~m/[a-z]/;
917 } elsif (s{^-([cC])}{-}) {
918 $pass_options = $1=~m/[a-z]/;
919 } elsif (s{^-D}{-}) {
921 } elsif (s{^-T(.+)}{-}s) {
923 } elsif (s{^-([oO])}{-}) {
924 $online = $1=~m/[a-z]/;
926 die "$self: unknown short option(s) $_\n" unless $_ eq $orgopt;
927 $not_a_nailing_opt->();
930 } elsif (s{^--help$}{}) {
932 } elsif (s{^--man(?:ual)?$}{}) {
934 } elsif (s{^--target=}{}) {
936 } elsif (m{^--(on|off)line$}) {
937 $online = $1 eq 'on';
938 } elsif (m{^--just-linkfarm$}) {
940 $cargo_lock_update= 1;
941 } elsif (m{^--(clean|keep)-linkfarm$}) {
942 $oot_clean = $1 eq 'clean';
943 } elsif (m{^--(no)?-lock$}) {
945 } elsif (m{^--leave-nailed$}) {
947 } elsif (s{^--subcommand-props=}{}) {
948 my @props = split /\,/, $_;
950 if (!%subcmd_prop_ok) {
951 foreach my $v (\@subcmd_xprops, values %subcmd_props) {
952 $subcmd_prop_ok{$_}=1 foreach @$v;
956 or die "$self: unknown subcommand property \`$_'\n"
958 $cargo_subcmd = \@props;
959 } elsif (m{^--(no-)?cargo-lock-update}) {
960 $cargo_lock_update= !!$1;
962 $not_a_nailing_opt->();
970 @args_preface = shift @ARGV;
971 while (defined($_ = shift @ARGV)) {
972 if (!m{^-}) { unshift @ARGV, $_; last; }
973 if ($_ eq '--') { last; }
974 push @args_preface, $_;
976 @ARGV || die "$self: need cargo subcommand\n";
977 $cargo_subcmd //= $ARGV[0];
980 $cargo_subcmd //= '';
983 push @args_preface, shift @ARGV;
985 if (!ref($cargo_subcmd)) {
986 print STDERR " cargo_subcmd lookup $cargo_subcmd\n" if $dump;
987 $cargo_subcmd = $subcmd_props{$cargo_subcmd} // [ ];
990 print STDERR " cargo_subcmd props @$cargo_subcmd\n" if $dump;
992 $cargo_subcmd{$_} = 1 foreach @$cargo_subcmd;
993 $cargo_subcmd = \%cargo_subcmd;
1000 consider_alt_cargo_lock();
1005 consider_directories();
1006 our @display_cmd = @ARGV;
1007 oot_massage_cmdline();
1013 print STDERR Dumper(\%manifests) if $dump>=2;
1014 print STDERR Dumper(\%packagemap, \@ARGV,
1015 { src_absdir => $src_absdir,
1016 worksphere => $worksphere,
1018 oot_dir => $oot_dir,
1019 oot_absdir => $oot_absdir,
1020 build_absdir => $build_absdir });
1026 $want_uninstall = !$leave_nailed;
1030 printf STDERR "$self: nailed (%s manifests, %s packages)%s\n",
1031 (scalar keys %manifests), (scalar keys %packagemap),
1032 (defined($alt_cargo_lock) and ", using `$alt_cargo_lock'")
1035 print STDERR "$self: invoking: @display_cmd\n" if $verbose;
1036 my $estatus = invoke();
1038 cargo_lock_update_after();
1040 uninstall() unless $leave_nailed;
1041 $want_uninstall = 0;
1043 print STDERR "$self: unnailed. status $estatus.\n" if $verbose;