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 --just-linkfarm Make the linkfarm as for -u, then stop
31 -s<subcommand> Treat command as `cargo <subcommand>`
32 --subcommand-props=<prop>,... Override command props (see docs)
40 use Types::Serialiser;
41 use File::Glob qw(bsd_glob GLOB_ERR GLOB_BRACE GLOB_NOMAGIC);
46 RPI => 'arm-unknown-linux-gnueabihf',
50 $self = $0; $self =~ s{^.*/(?=.)}{};
53 while ($deref =~ m{^/}) {
54 my $link = readlink $deref;
57 or die "$self: checking our script location $deref: $!\n";
58 $deref =~ s{/[^/]+$}{}
59 or die "$self: unexpected script path: $deref\n";
61 unshift @INC, $deref."/TOML-Tiny/lib";
64 last if $link !~ m{^/};
69 use Fcntl qw(LOCK_EX);
71 use TOML::Tiny::Faithful;
73 our $src_absdir = getcwd() // die "$self: getcwd failed: $!\n";
75 our $worksphere = $src_absdir;
76 $worksphere =~ s{/([^/]+)$}{}
77 or die "$self: cwd \`$worksphere' unsupported!\n";
78 our $subdir = $1; # leafname
80 our $lockfile = "../.nailing-cargo.lock";
84 our $command_is_cargo;
86 our $cargo_lock_update;
93 # build (default) =>[qw( )],
94 'generate-lockfile'=>[qw( lock-update !target !target-dir )],
95 update =>[qw( lock-update !target online )],
96 fetch =>[qw( online !target-dir )],
99 our @subcmd_xprops = qw(!manifest-path !offline !locked);
107 print $usage or die $!;
112 my $manual = ($base_path // '.').'/README.md';
113 stat $manual or die "$self: manual not found at $manual: $!\n";;
114 exec 'sh','-ec', 'pandoc -- "$1" 2>&1 | w3m -T text/html', '--', $manual;
115 die "$self: exec sh failed: $!";
118 sub read_or_enoent ($) {
120 if (!open R, '<', $fn) {
121 return undef if $!==ENOENT;
122 die "$self: open $fn: $!\n";
125 my ($r) = <R> // die "$self: read $fn: $!\n";
129 sub stat_exists ($$) {
130 my ($fn, $what) = @_;
131 if (stat $fn) { return 1; }
132 $!==ENOENT or die "$self: stat $what: $fn: $!\n";
137 print STDERR " subcmd_p ".(join ' ', keys %$cargo_subcmd)." | @_\n"
139 $cargo_subcmd->{$_[0]}
142 sub toml_or_enoent ($$) {
144 my $toml = read_or_enoent($f) // return;
145 print STDERR "Read TOML from $f\n" if $dump;
146 my ($v,$e) = from_toml($toml);
149 die "$self: parse TOML: $what: $f: $e\n";
151 die "$e ?" if length $e;
155 sub load1config ($) {
157 my $toml = toml_or_enoent($f, "config file");
158 push @configs, $toml if defined $toml;
162 my $dotfile = ".nailing-cargo.toml";
163 load1config("../Nailing-Cargo.toml");
164 load1config($dotfile);
165 load1config("$ENV{HOME}/$dotfile") if defined $ENV{HOME};
166 load1config("/etc/nailing-cargo/cfg.toml");
169 sub unlink_or_enoent ($) { unlink $_[0] or $!==ENOENT; }
173 "@$x[0..5]" eq "@$y[0..5]";
178 open LOCK, ">", $lockfile or die "$self: open/create $lockfile: $!\n";
179 flock LOCK, LOCK_EX or die "$self: lock $lockfile: $!\n";
180 my @fstat = stat LOCK or die "$self: fstat: $!\n";
181 my @stat = stat $lockfile;
183 next if $! == ENOENT;
184 die "$self: stat $lockfile: $!\n";
186 last if same_file(\@fstat,\@stat);
190 unlink $lockfile or die "$self: removing lockfile: $!\n";
198 die "$self: config key \`@_': $m\n";
202 foreach my $cfg (@configs) {
205 last unless defined $v;
206 ref($v) eq 'HASH' or badcfg @_, "parent key \`$k' is not a hash";
209 return $v if defined $v;
217 my $got = ref($v) || 'scalar';
218 return $v if !defined($v) || $got eq $exp;
219 badcfg @_, "found \L$got\E, expected \L$exp\E";
220 # ^ toml doesn't make refs to scalars, so this is unambiguous
225 (cfge $exp, @_) // badcfg @_, "missing";
228 sub cfgs { cfge 'scalar', @_ }
229 sub cfgsn { cfgn 'scalar', @_ }
233 return $v if !defined($v) || Types::Serialiser::is_bool $v;
234 badcfg @_, "expected boolean";
238 my $l = cfge 'ARRAY', @_;
239 foreach my $x (@$l) {
240 !ref $x or badcfg @_, "list contains non-scalar element";
246 my $nailfile = "../Cargo.nail";
247 open N, '<', $nailfile or die "$self: open $nailfile: $!\n";
249 my $toml = <N> // die "$self: read $nailfile: $!";
251 if ($toml !~ m{^\s*\[/}m &&
252 $toml !~ m{^[^\n\#]*\=}m &&
253 # old non-toml syntax
254 $toml =~ s{^[ \t]*([-_0-9a-z]+)[ \t]+(\S+)[ \t]*$}{$1 = \"$2\"}mig) {
255 $toml =~ s{^}{[packages\]\n};
257 $toml =~ s{^[ \t]*\-[ \t]*\=[ \t]*(\"[-_0-9a-z]+\"\n?)$}{
260 $toml = "subdirs = [\n".(join '', map { "$_\n" } @sd)."]\n".$toml;
264 ($nail,$e) = from_toml($toml);
265 if (!defined $nail) {
268 print STDERR "$self: $nailfile transformed into TOML:\n$toml\n";
271 die "$self: parse $nailfile: $e\n";
273 die "$e ?" if length $e;
275 $nail->{subdirs} //= [ ];
277 if (!ref $nail->{subdirs}) {
280 map { s/^\s+//; s/\s+$//; $_; }
286 unshift @configs, $nail;
289 sub get_dependency_tables ($) {
291 my @keys = qw(dependencies build-dependencies dev-dependencies);
295 foreach my $k (@keys) {
296 my $deps = $node->{$k};
297 push @r, $deps if $deps;
301 foreach my $target_node (values %{ $toml->{target} // { } }) {
302 $process->($target_node);
307 our @alt_cargo_lock_stat;
309 sub consider_alt_cargo_lock () {
310 my @ck = qw(alt_cargo_lock);
311 # User should *either* have Cargo.lock in .gitignore,
312 # or expect to commit Cargo.lock.example ($alt_cargo_lock)
314 $alt_cargo_lock = (cfg_uc @ck);
317 if (defined($alt_cargo_lock) && ref($alt_cargo_lock) eq 'HASH') {
318 $force = cfg_bool qw(alt_cargo_lock force);
319 my @ck = qw(alt_cargo_lock file);
320 $alt_cargo_lock = cfg_uc @ck;
322 $alt_cargo_lock //= Types::Serialiser::true;
324 if (Types::Serialiser::is_bool $alt_cargo_lock) {
325 if (!$alt_cargo_lock) { $alt_cargo_lock = undef; return; }
326 $alt_cargo_lock = 'Cargo.lock.example';
329 if (ref($alt_cargo_lock) || $alt_cargo_lock =~ m{/}) {
330 badcfg @ck, "expected boolean, or leafname";
333 if (!stat_exists $alt_cargo_lock, "alt_cargo_lock") {
334 $alt_cargo_lock = undef unless $force;
338 @alt_cargo_lock_stat = stat _;
341 our $oot_dir; # oot.dir or "Build"
344 sub consider_oot () {
345 $oot_dir = cfgs qw(oot dir);
346 my $use = cfgs qw(oot use);
347 unless (defined($oot_dir) || defined($use) ||
348 defined(cfg_uc qw(oot user))) {
351 if (($use//'') eq 'disable') {
355 $oot_dir //= 'Build';
356 $oot_absdir = ($oot_dir !~ m{^/} ? "$worksphere/" : ""). $oot_dir;
364 sub read_manifest ($$$) {
365 my ($subdir, $org_subdir, $why) = @_;
366 my $manifest = "../$subdir/Cargo.toml";
367 print STDERR "$self: reading $manifest...\n" if $verbose>=4;
368 if (defined $manifests{$manifest}) {
370 "$self: warning: $subdir: specified more than once!".
371 " (ignoring $why)\n";
374 foreach my $try ("$manifest.unnailed", "$manifest") {
375 my $toml = toml_or_enoent($try, "manifest, in $why") // next;
376 my $ws = $toml->{workspace};
378 queue_workspace_members($subdir, $org_subdir, $ws, "$subdir, $why");
380 my $p = $toml->{package}{name};
381 if (!defined $p and !defined $ws) {
383 "$self: warning: $subdir, $why: missing package.name in $try, ignoring\n";
386 $manifests{$manifest} = [ $toml, $org_subdir ] if $p;
387 foreach my $dep (get_dependency_tables $toml) {
388 next unless defined $dep->{path};
389 queue_referenced_path($dep->{path}, $org_subdir,
390 "dependency of $subdir, $why");
397 sub queue_workspace_members ($$) {
398 my ($subdir, $org_subdir, $ws_toml, $what) = @_;
399 # We need to (more or less) reimplement the cargo workspace
400 # membership algorithm (see the "workspaces" section of the cargo
401 # reference). How tiresome.
403 # It's not quite the same for us because we aren't interested in
404 # whether cargo thinks things are "in the workspace". But we do
405 # need to do the automatic discover.
407 my @include = @{ $ws_toml->{members} // [ ] };
408 my $exclude = $ws_toml->{exclude} // [ ];
411 s/[^*?0-9a-zA-Z_]/\\$&/g;
416 foreach my $spec (@include) {
417 if ($spec =~ m{^/}) {
419 "$self: warning: absolute workspace member $spec in $what (not nailing, but cargo will probably use it)\n";
422 my $spec_glob = "../$subdir/$spec";
423 my $globflags = GLOB_ERR|GLOB_BRACE|GLOB_NOMAGIC;
424 foreach my $globent (bsd_glob($spec_glob, $globflags)) {
425 next if grep { $globent =~ m{^$_$} } @exclude;
426 queue_referenced_path($globent, $org_subdir,
427 "member of workspace $what");
432 sub queue_referenced_path ($$$) {
433 my ($spec_path, $org_subdir, $why) = @_;
435 qw(realpath), "--relative-to=../$org_subdir", "--", $spec_path
436 or die "$self: fork/pipe/exec for realpath(1)\n";
437 my $rel_path = do { local $/=undef; <REALPATH>; };
439 my $r = close(REALPATH);
440 die "$self: reap realpath: $!\n" if $!;
441 if (!chomp($rel_path) or $?) {
443 "$self: warning: failed to determine realpath for $spec_path in $org_subdir (exit code $?)\n";
446 if ($rel_path =~ m{^\.\./} or $rel_path eq '..') {
448 "$self: warning: $spec_path ($why) points outside $org_subdir, not following so not nailing (although cargo probably will follow it)\n";
452 my $q_subdir = "$org_subdir/$rel_path";
453 print STDERR "$self: making a note to look at $q_subdir, $why)\n"
456 push @queued_paths, [ "$q_subdir", $org_subdir, $why ];
460 foreach my $p (keys %{ $nail->{packages} }) {
461 my $v = $nail->{packages}{$p};
462 my $subdir = ref($v) ? $v->{subdir} : $v;
463 my ($gotpackage, $ws) = read_manifest($subdir, $subdir, "from [packages]");
464 $gotpackage //= '<nothing!>';
465 if ($gotpackage ne $p) {
467 "$self: warning: honouring Cargo.nail packages.$subdir=$p even though $subdir contains package $gotpackage!\n";
469 die if defined $packagemap{$p};
470 $packagemap{$p} = [ $subdir, $subdir ];
472 foreach my $subdir (@{ $nail->{subdirs} }) {
473 my ($gotpackage,$ws) = read_manifest($subdir, $subdir, "from [subdirs]");
474 if (!defined $gotpackage) {
476 "$self: warning: ignoring subdir $subdir which has no (suitable) Cargo.toml\n"
480 $packagemap{$gotpackage} //= [ $subdir, $subdir ];
482 while (my ($subdir, $org_subdir, $why) = @{ shift @queued_paths or [] }) {
483 next if $manifests{"../$subdir/Cargo.toml"};
484 my ($gotpackage, $ws) = read_manifest($subdir, $org_subdir, $why);
485 next unless $gotpackage;
486 $packagemap{$gotpackage} //= [ $subdir, $org_subdir ];
491 foreach my $p (sort keys %packagemap) {
492 print STDERR "$self: package $p in $packagemap{$p}[0]\n" if $verbose>=2;
494 foreach my $mf (keys %manifests) {
495 my ($toml, $mf_org_subdir) = @{ $manifests{$mf} };
496 foreach my $deps (get_dependency_tables $toml) {
498 foreach my $p (keys %packagemap) {
499 my $info = $deps->{$p};
500 next unless defined $info;
501 next if $packagemap{$p}[1] eq $mf_org_subdir;
502 $deps->{$p} = $info = { } unless ref $info;
503 my $oldpath = $info->{path};
504 delete $info->{version};
505 my $newpath = $worksphere.'/'.$packagemap{$p}[0];
506 print STDERR "in $mf set $p path=$newpath (was ".
507 ($oldpath // '<unset>').")\n"
509 $info->{path} = $newpath;
511 delete $info->{branch};
514 my $nailing = "$mf.nailing~";
515 unlink_or_enoent $nailing or die "$self: remove old $nailing: $!\n";
516 open N, '>', $nailing or die "$self: create new $nailing: $!\n";
517 print N to_toml($toml) or die "$self: write new $nailing: $!\n";
518 close N or die "$self: close new $nailing: $!\n";
523 if ($just_linkfarm) {
524 die "$self: --just-linkfarm but not doing out-of-tree builds!\n"
525 unless defined $oot_dir;
530 if (!defined $online) {
531 $_ = cfg_uc qw(misc online);
533 } elsif (Types::Serialiser::is_bool $_) {
538 } elsif (m/^[1ty]/) { # allow booleanish strings
539 $online = 1; # for less user frustration
540 } elsif (m/^[0fn]/) {
543 badcfg qw(misc online), "expected boolean or 'auto', found '$_'";
546 $online //= 1 if subcmd_p('online');
549 $cargo_lock_update //= subcmd_p('lock-update');
553 if (!$cargo_lock_update) {
554 push @add, qw(--locked) unless subcmd_p('!locked');
555 if (defined($oot_dir) && !subcmd_p('!manifest-path')) {
556 my $cargotoml = "${src_absdir}/Cargo.toml";
557 push @args_preface, "--manifest-path=$cargotoml" if $pass_options;
558 push @add, qw(--target-dir=target) unless subcmd_p('!target-dir');
562 if (defined($target) && !subcmd_p('!target')) {
563 if ($target =~ m{^[A-Z]}) {
564 $target = (cfgs 'arch', $target) // $archmap{$target}
565 // die "$self: --target=$target alias specified; not in cfg or map\n";
567 push @add, "--target=$target";
570 push @add, "--offline" unless $online || subcmd_p('!offline');
572 push @args_preface, @add if $pass_options;
573 die if grep { m/ / } @add;
574 $ENV{NAILINGCARGO_CARGO_OPTIONS} = "@add";
576 unshift @ARGV, @args_preface;
579 our $build_absdir; # .../Build/<subdir>
581 sub oot_massage_cmdline () {
582 return unless defined $oot_dir;
584 my $use = cfgs qw(oot use);
585 $use // die "$self: out-of-tree build, but \`oot.use' not configured\n";
586 $build_absdir = "$oot_absdir/$subdir";
588 my ($pre,$post) = ('','');
590 if (!$cargo_lock_update) {
591 push @xargs, $build_absdir;
592 ($pre, $post) = ('cd "$1"; shift; ', '');
594 push @xargs, $oot_absdir, $subdir, $src_absdir;
597 mkdir -p -- "$1"; cd "$1"; shift;
598 clean () { find -lname "$1/*" -print0 | xargs -0r rm --; }; clean;
599 find "$1" -maxdepth 1 \! -name Cargo.lock -print0 | xargs -0r -I_ ln -sf -- _ .;
601 $pre .= <<'ENDLK' if stat_exists 'Cargo.lock', 'working cargo lockfile';
603 cp -- "$1"/Cargo.lock .;
608 # $post = <<'ENDCLEAN';
612 my $addpath = (cfg_uc qw(oot path_add)) //
613 $use eq 'really' ? Types::Serialiser::true : Types::Serialiser::false;
615 !Types::Serialiser::is_bool $addpath ? $addpath :
616 $addpath ? '$HOME/.cargo/bin' :
618 if (defined $addpath) {
620 PATH=$addpath:\${PATH-/usr/local/bin:/bin:/usr/bin};
624 $pre =~ s/^\s+//mg; $pre =~ s/\s+/ /g;
625 $post =~ s/^\s+//mg; $post =~ s/\s+/ /g;
627 my $getuser = sub { cfgsn qw(oot user) };
629 my $xe = $verbose >= 2 ? 'xe' : 'e';
632 @command = (@_, 'sh',"-${xe}c",$pre.'exec "$@"','--',@xargs);
634 @command = (@_, 'sh',"-${xe}c",$pre.'"$@"; '.$post,'--',@xargs);
636 push @command, @ARGV;
638 my $command_sh = sub {
639 my $quoted = join ' ', map {
644 @command = @_, "set -${xe}; $pre $quoted; $post";
646 print STDERR "$self: out-of-tree, building in: \`$build_absdir'\n"
648 if ($use eq 'really') {
649 my $user = $getuser->();
650 my @pw = getpwnam $user or die "$self: oot.user \`$user' lookup failed\n";
651 my $homedir = $pw[7];
652 $sh_ec->('really','-u',$user,'env',"HOME=$homedir");
653 print STDERR "$self: using really to run as user \`$user'\n" if $verbose;
654 } elsif ($use eq 'ssh') {
655 my $user = $getuser->();
656 $user .= '@localhost' unless $user =~ m/\@/;
657 $command_sh->('ssh',$user);
658 print STDERR "$self: using ssh to run as \`$user'\n" if $verbose;
659 } elsif ($use eq 'command_args') {
660 my @c = cfgn_list qw(oot command);
662 print STDERR "$self: out-of-tree, adverbial command: @c\n" if $verbose;
663 } elsif ($use eq 'command_sh') {
664 my @c = cfgn_list qw(oot command);
666 print STDERR "$self: out-of-tree, ssh'ish command: @c\n" if $verbose;
667 } elsif ($use eq 'null') {
670 die "$self: oot.use mode $use not recognised\n";
677 $ENV{CARGO_MANIFEST_DIR} = $src_absdir;
678 $ENV{NAILINGCARGO_MANIFEST_DIR} = $src_absdir;
679 $ENV{NAILINGCARGO_WORKSPHERE} = $worksphere;
680 $ENV{NAILINGCARGO_BUILDSPHERE} = $oot_absdir;
681 delete $ENV{NAILINGCARGO_BUILDSPHERE} unless $oot_absdir;
682 $ENV{NAILINGCARGO_BUILD_DIR} = $build_absdir // $src_absdir;
688 if ($want_uninstall) {
690 foreach my $mf (keys %manifests) {
691 eval { uninstall1($mf,1); 1; } or warn "$@";
693 eval { unaltcargolock(1); 1; } or warn "$@";
697 sub consider_directories () {
698 return unless defined $oot_dir;
699 my $bsubdir = "../$oot_dir/$subdir";
700 return if stat $bsubdir;
701 die "$0: build directory $bsubdir inaccessible\n"
703 return if $cargo_lock_update; # will make it
704 die "$0: build directory $bsubdir does not exist, and not in Cargo.lock update mode!\n";
707 our $cleanup_cargo_lock;
709 foreach my $mf (keys %manifests) {
710 link "$mf", "$mf.unnailed" or $!==EEXIST
711 or die "$self: make backup link $mf.unnailed: $!\n";
714 if (defined($alt_cargo_lock)) {
715 if (@alt_cargo_lock_stat) {
716 print STDERR "$self: using alt_cargo_lock `$alt_cargo_lock'..."
718 if (link $alt_cargo_lock, 'Cargo.lock') {
719 print STDERR " linked\n" if $verbose>=3;
720 } elsif ($! != EEXIST) {
721 print STDERR "\n" if $verbose>=3;
722 die "$self: make \`Cargo.lock' available as \`$alt_cargo_lock': $!\n";
724 print STDERR "checking quality." if $verbose>=3;
725 my @lock_stat = stat 'Cargo.lock'
726 or die "$self: stat Cargo.lock (for alt check: $!\n";
727 same_file(\@alt_cargo_lock_stat, \@lock_stat)
729 "$self: \`Cargo.lock' and alt file \`$alt_cargo_lock' both exist and are not the same file!\n";
731 $cleanup_cargo_lock = 1;
733 $cleanup_cargo_lock = 1;
734 # If Cargo.lock exists and alt doesn't, that means either
735 # that a previous run was interrupted, or that the user has
743 my $nailed = "$mf.nailed~"; $nailed =~ s{/([^/]+)$}{/.$1} or die;
748 my @our_unfound_stab = stat_exists('Cargo.toml', 'local Cargo.toml')
750 foreach my $mf (keys %manifests) {
751 if (@our_unfound_stab) {
752 if (stat_exists $mf, "manifest in to-be-nailed directory") {
753 my @mf_stab = stat _ ;
754 if ("@mf_stab[0..1]" eq "@our_unfound_stab[0..1]") {
755 @our_unfound_stab = ();
760 my $nailing = "$mf.nailing~";
761 my $nailed = nailed($mf);
764 if (open NN, '<', $nailed) {
765 $diff = compare($nailing, \*NN);
766 die "$self: compare $nailing and $nailed: $!" if $diff<0;
768 $!==ENOENT or die "$self: check previous $nailed: $!\n";
778 rename $use, $mf or die "$self: install nailed $use: $!\n";
779 unlink_or_enoent $rm or die "$self: remove old $rm: $!\n";
780 print STDERR "$self: nailed $mf\n" if $verbose>=3;
783 if (@our_unfound_stab) {
785 "$self: *WARNING* cwd is not in Cargo.nail thbough it has Cargo.toml!\n";
790 my $r = system @ARGV;
794 print STDERR "$self: could not execute $ARGV[0]: $!\n";
796 } elsif ($r & 0xff00) {
797 print STDERR "$self: $ARGV[0] failed (exit status $r)\n";
800 print STDERR "$self: $ARGV[0] died due to signal! (wait status $r)\n";
805 sub cargo_lock_update_after () {
806 if ($cargo_lock_update && !$just_linkfarm) {
807 # avoids importing File::Copy and the error handling is about as good
809 my $r= system qw(cp --), "$build_absdir/Cargo.lock", "Cargo.lock";
810 die "$self: run cp: $! $?" if $r<0 || $r & 0xff;
811 die "$self: failed to update local Cargo.lock (wait status $r)\n" if $r;
815 sub uninstall1 ($$) {
816 my ($mf, $enoentok) = @_;
817 my $unnailed = "$mf.unnailed";
818 rename $unnailed, $mf or ($enoentok && $!==ENOENT)
819 or die "$self: failed to restore: rename $unnailed back to $mf: $!\n";
822 sub unaltcargolock ($) {
824 return unless $cleanup_cargo_lock;
825 die 'internal error!' unless defined $alt_cargo_lock;
827 # we ignore $enoentok because we don't know if one was supposed to
830 rename('Cargo.lock', $alt_cargo_lock) or $!==ENOENT or die
831 "$self: cleanup: rename possibly-updated \`Cargo.lock' to \`$alt_cargo_lock': $!\n";
833 unlink 'Cargo.lock' or $!==ENOENT or die
834 "$self: cleanup: remove \`Cargo.lock' in favour of \`$alt_cargo_lock': $!\n";
835 # ^ this also helps clean up the stupid rename() corner case
839 foreach my $mf (keys %manifests) {
840 my $nailed = nailed($mf);
841 link $mf, $nailed or die "$self: preserve (link) $mf as $nailed: $!\n";
850 # Loop exit condition:
853 # $is_cargo==1 <cargo-command> <cargo-opts> [--] <subcmd>...
854 # $is_cargo==0 <build-command>...
858 die "$self: need cargo subcommand\n"
859 unless $noact || $just_linkfarm;;
860 push @ARGV, "CARGO-SUBCOMMAND"; # dummy, user may see it
866 my $not_a_nailing_opt = sub { # usage 1
867 unshift @ARGV, $orgopt;
868 unshift @ARGV, 'cargo';
870 no warnings qw(exiting);
873 $not_a_nailing_opt->() unless m{^-};
874 $not_a_nailing_opt->() if $_ eq '--';
876 if ($_ eq '---') { # usage 2 or 3
878 die "$self: --- must be followed by build command\n" unless $noact;
879 push @ARGV, 'BUILD-COMMAND';
881 if ($ARGV[0] eq '--') { # usage 3
884 } elsif (grep { $_ eq '--' } @ARGV) { # usage 2
886 } elsif ($ARGV[0] =~ m{[^/]*cargo[^/]*$}) { # usage 2
897 } elsif (s{^-v}{-}) {
899 } elsif (s{^-q}{-}) {
901 } elsif (s{^-n}{-}) {
903 } elsif (s{^-s(.+)}{-}s) {
905 } elsif (s{^-([uU])}{-}) {
906 $cargo_lock_update = $1=~m/[a-z]/;
907 } elsif (s{^-([cC])}{-}) {
908 $pass_options = $1=~m/[a-z]/;
909 } elsif (s{^-D}{-}) {
911 } elsif (s{^-T(.+)}{-}s) {
913 } elsif (s{^-([oO])}{-}) {
914 $online = $1=~m/[a-z]/;
916 die "$self: unknown short option(s) $_\n" unless $_ eq $orgopt;
917 $not_a_nailing_opt->();
920 } elsif (s{^--help$}{}) {
922 } elsif (s{^--man(?:ual)?$}{}) {
924 } elsif (s{^--target=}{}) {
926 } elsif (m{^--(on|off)line$}) {
927 $online = $1 eq 'on';
928 } elsif (m{^--just-linkfarm$}) {
930 $cargo_lock_update= 1;
931 } elsif (s{^--subcommand-props=}{}) {
932 my @props = split /\,/, $_;
934 if (!%subcmd_prop_ok) {
935 foreach my $v (\@subcmd_xprops, values %subcmd_props) {
936 $subcmd_prop_ok{$_}=1 foreach @$v;
940 or die "$self: unknown subcommand property \`$_'\n"
942 $cargo_subcmd = \@props;
943 } elsif (m{^--(no-)?cargo-lock-update}) {
944 $cargo_lock_update= !!$1;
946 $not_a_nailing_opt->();
954 @args_preface = shift @ARGV;
955 while (defined($_ = shift @ARGV)) {
956 if (!m{^-}) { unshift @ARGV, $_; last; }
957 if ($_ eq '--') { last; }
958 push @args_preface, $_;
960 @ARGV || die "$self: need cargo subcommand\n";
961 $cargo_subcmd //= $ARGV[0];
964 $cargo_subcmd //= '';
967 push @args_preface, shift @ARGV;
969 if (!ref($cargo_subcmd)) {
970 print STDERR " cargo_subcmd lookup $cargo_subcmd\n" if $dump;
971 $cargo_subcmd = $subcmd_props{$cargo_subcmd} // [ ];
974 print STDERR " cargo_subcmd props @$cargo_subcmd\n" if $dump;
976 $cargo_subcmd{$_} = 1 foreach @$cargo_subcmd;
977 $cargo_subcmd = \%cargo_subcmd;
984 consider_alt_cargo_lock();
989 consider_directories();
990 our @display_cmd = @ARGV;
991 oot_massage_cmdline();
997 print STDERR Dumper(\%manifests) if $dump>=2;
998 print STDERR Dumper(\%packagemap, \@ARGV,
999 { src_absdir => $src_absdir,
1000 worksphere => $worksphere,
1002 oot_dir => $oot_dir,
1003 oot_absdir => $oot_absdir,
1004 build_absdir => $build_absdir });
1010 $want_uninstall = 1;
1014 printf STDERR "$self: nailed (%s manifests, %s packages)%s\n",
1015 (scalar keys %manifests), (scalar keys %packagemap),
1016 (defined($alt_cargo_lock) and ", using `$alt_cargo_lock'")
1019 print STDERR "$self: invoking: @display_cmd\n" if $verbose;
1020 my $estatus = invoke();
1022 cargo_lock_update_after();
1025 $want_uninstall = 0;
1027 print STDERR "$self: unnailed. status $estatus.\n" if $verbose;