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
30 --leave-nailed Leave the nailed Cargo.toml in place
32 -s<subcommand> Treat command as `cargo <subcommand>`
33 --subcommand-props=<prop>,... Override command props (see docs)
41 use Types::Serialiser;
42 use File::Glob qw(bsd_glob GLOB_ERR GLOB_BRACE GLOB_NOMAGIC);
47 RPI => 'arm-unknown-linux-gnueabihf',
51 $self = $0; $self =~ s{^.*/(?=.)}{};
54 while ($deref =~ m{^/}) {
55 my $link = readlink $deref;
58 or die "$self: checking our script location $deref: $!\n";
59 $deref =~ s{/[^/]+$}{}
60 or die "$self: unexpected script path: $deref\n";
62 unshift @INC, $deref."/TOML-Tiny/lib";
65 last if $link !~ m{^/};
70 use Fcntl qw(LOCK_EX);
72 use TOML::Tiny::Faithful;
74 our $src_absdir = getcwd() // die "$self: getcwd failed: $!\n";
76 our $worksphere = $src_absdir;
77 $worksphere =~ s{/([^/]+)$}{}
78 or die "$self: cwd \`$worksphere' unsupported!\n";
79 our $subdir = $1; # leafname
81 our $lockfile = "../.nailing-cargo.lock";
85 our $command_is_cargo;
87 our $cargo_lock_update;
95 # build (default) =>[qw( )],
96 'generate-lockfile'=>[qw( lock-update !target !target-dir )],
97 update =>[qw( lock-update !target online )],
98 fetch =>[qw( online !target-dir )],
101 our @subcmd_xprops = qw(!manifest-path !offline !locked);
109 print $usage or die $!;
114 my $manual = ($base_path // '.').'/README.md';
115 stat $manual or die "$self: manual not found at $manual: $!\n";;
116 exec 'sh','-ec', 'pandoc -- "$1" 2>&1 | w3m -T text/html', '--', $manual;
117 die "$self: exec sh failed: $!";
120 sub read_or_enoent ($) {
122 if (!open R, '<', $fn) {
123 return undef if $!==ENOENT;
124 die "$self: open $fn: $!\n";
127 my ($r) = <R> // die "$self: read $fn: $!\n";
131 sub stat_exists ($$) {
132 my ($fn, $what) = @_;
133 if (stat $fn) { return 1; }
134 $!==ENOENT or die "$self: stat $what: $fn: $!\n";
139 print STDERR " subcmd_p ".(join ' ', keys %$cargo_subcmd)." | @_\n"
141 $cargo_subcmd->{$_[0]}
144 sub toml_or_enoent ($$) {
146 my $toml = read_or_enoent($f) // return;
147 print STDERR "Read TOML from $f\n" if $dump;
148 my ($v,$e) = from_toml($toml);
151 die "$self: parse TOML: $what: $f: $e\n";
153 die "$e ?" if length $e;
157 sub load1config ($) {
159 my $toml = toml_or_enoent($f, "config file");
160 push @configs, $toml if defined $toml;
164 my $dotfile = ".nailing-cargo.toml";
165 load1config("../Nailing-Cargo.toml");
166 load1config($dotfile);
167 load1config("$ENV{HOME}/$dotfile") if defined $ENV{HOME};
168 load1config("/etc/nailing-cargo/cfg.toml");
171 sub unlink_or_enoent ($) { unlink $_[0] or $!==ENOENT; }
175 "@$x[0..5]" eq "@$y[0..5]";
180 open LOCK, ">", $lockfile or die "$self: open/create $lockfile: $!\n";
181 flock LOCK, LOCK_EX or die "$self: lock $lockfile: $!\n";
182 my @fstat = stat LOCK or die "$self: fstat: $!\n";
183 my @stat = stat $lockfile;
185 next if $! == ENOENT;
186 die "$self: stat $lockfile: $!\n";
188 last if same_file(\@fstat,\@stat);
192 unlink $lockfile or die "$self: removing lockfile: $!\n";
200 die "$self: config key \`@_': $m\n";
204 foreach my $cfg (@configs) {
207 last unless defined $v;
208 ref($v) eq 'HASH' or badcfg @_, "parent key \`$k' is not a hash";
211 return $v if defined $v;
219 my $got = ref($v) || 'scalar';
220 return $v if !defined($v) || $got eq $exp;
221 badcfg @_, "found \L$got\E, expected \L$exp\E";
222 # ^ toml doesn't make refs to scalars, so this is unambiguous
227 (cfge $exp, @_) // badcfg @_, "missing";
230 sub cfgs { cfge 'scalar', @_ }
231 sub cfgsn { cfgn 'scalar', @_ }
235 return $v if !defined($v) || Types::Serialiser::is_bool $v;
236 badcfg @_, "expected boolean";
240 my $l = cfge 'ARRAY', @_;
241 foreach my $x (@$l) {
242 !ref $x or badcfg @_, "list contains non-scalar element";
248 my $nailfile = "../Cargo.nail";
249 open N, '<', $nailfile or die "$self: open $nailfile: $!\n";
251 my $toml = <N> // die "$self: read $nailfile: $!";
253 if ($toml !~ m{^\s*\[/}m &&
254 $toml !~ m{^[^\n\#]*\=}m &&
255 # old non-toml syntax
256 $toml =~ s{^[ \t]*([-_0-9a-z]+)[ \t]+(\S+)[ \t]*$}{$1 = \"$2\"}mig) {
257 $toml =~ s{^}{[packages\]\n};
259 $toml =~ s{^[ \t]*\-[ \t]*\=[ \t]*(\"[-_0-9a-z]+\"\n?)$}{
262 $toml = "subdirs = [\n".(join '', map { "$_\n" } @sd)."]\n".$toml;
266 ($nail,$e) = from_toml($toml);
267 if (!defined $nail) {
270 print STDERR "$self: $nailfile transformed into TOML:\n$toml\n";
273 die "$self: parse $nailfile: $e\n";
275 die "$e ?" if length $e;
277 $nail->{subdirs} //= [ ];
279 if (!ref $nail->{subdirs}) {
282 map { s/^\s+//; s/\s+$//; $_; }
288 unshift @configs, $nail;
291 sub get_dependency_tables ($) {
293 my @keys = qw(dependencies build-dependencies dev-dependencies);
297 foreach my $k (@keys) {
298 my $deps = $node->{$k};
299 push @r, $deps if $deps;
303 foreach my $target_node (values %{ $toml->{target} // { } }) {
304 $process->($target_node);
309 our @alt_cargo_lock_stat;
311 sub consider_alt_cargo_lock () {
312 my @ck = qw(alt_cargo_lock);
313 # User should *either* have Cargo.lock in .gitignore,
314 # or expect to commit Cargo.lock.example ($alt_cargo_lock)
316 $alt_cargo_lock = (cfg_uc @ck);
319 if (defined($alt_cargo_lock) && ref($alt_cargo_lock) eq 'HASH') {
320 $force = cfg_bool qw(alt_cargo_lock force);
321 my @ck = qw(alt_cargo_lock file);
322 $alt_cargo_lock = cfg_uc @ck;
324 $alt_cargo_lock //= Types::Serialiser::true;
326 if (Types::Serialiser::is_bool $alt_cargo_lock) {
327 if (!$alt_cargo_lock) { $alt_cargo_lock = undef; return; }
328 $alt_cargo_lock = 'Cargo.lock.example';
331 if (ref($alt_cargo_lock) || $alt_cargo_lock =~ m{/}) {
332 badcfg @ck, "expected boolean, or leafname";
335 if (!stat_exists $alt_cargo_lock, "alt_cargo_lock") {
336 $alt_cargo_lock = undef unless $force;
340 @alt_cargo_lock_stat = stat _;
343 our $oot_dir; # oot.dir or "Build"
346 sub consider_oot () {
347 $oot_dir = cfgs qw(oot dir);
348 my $use = cfgs qw(oot use);
349 unless (defined($oot_dir) || defined($use) ||
350 defined(cfg_uc qw(oot user))) {
353 if (($use//'') eq 'disable') {
357 $oot_dir //= 'Build';
358 $oot_absdir = ($oot_dir !~ m{^/} ? "$worksphere/" : ""). $oot_dir;
366 sub read_manifest ($$$) {
367 my ($subdir, $org_subdir, $why) = @_;
368 my $manifest = "../$subdir/Cargo.toml";
369 print STDERR "$self: reading $manifest...\n" if $verbose>=4;
370 if (defined $manifests{$manifest}) {
372 "$self: warning: $subdir: specified more than once!".
373 " (ignoring $why)\n";
376 foreach my $try ("$manifest.unnailed", "$manifest") {
377 my $toml = toml_or_enoent($try, "manifest, in $why") // next;
378 my $ws = $toml->{workspace};
380 queue_workspace_members($subdir, $org_subdir, $ws, "$subdir, $why");
382 my $p = $toml->{package}{name};
383 if (!defined $p and !defined $ws) {
385 "$self: warning: $subdir, $why: missing package.name in $try, ignoring\n";
388 $manifests{$manifest} = [ $toml, $org_subdir ] if $p;
389 foreach my $dep (get_dependency_tables $toml) {
390 next unless defined $dep->{path};
391 queue_referenced_path($dep->{path}, $org_subdir,
392 "dependency of $subdir, $why");
399 sub queue_workspace_members ($$) {
400 my ($subdir, $org_subdir, $ws_toml, $what) = @_;
401 # We need to (more or less) reimplement the cargo workspace
402 # membership algorithm (see the "workspaces" section of the cargo
403 # reference). How tiresome.
405 # It's not quite the same for us because we aren't interested in
406 # whether cargo thinks things are "in the workspace". But we do
407 # need to do the automatic discover.
409 my @include = @{ $ws_toml->{members} // [ ] };
410 my $exclude = $ws_toml->{exclude} // [ ];
413 s/[^*?0-9a-zA-Z_]/\\$&/g;
418 foreach my $spec (@include) {
419 if ($spec =~ m{^/}) {
421 "$self: warning: absolute workspace member $spec in $what (not nailing, but cargo will probably use it)\n";
424 my $spec_glob = "../$subdir/$spec";
425 my $globflags = GLOB_ERR|GLOB_BRACE|GLOB_NOMAGIC;
426 foreach my $globent (bsd_glob($spec_glob, $globflags)) {
427 next if grep { $globent =~ m{^$_$} } @exclude;
428 queue_referenced_path($globent, $org_subdir,
429 "member of workspace $what");
434 sub queue_referenced_path ($$$) {
435 my ($spec_path, $org_subdir, $why) = @_;
437 qw(realpath), "--relative-to=../$org_subdir", "--", $spec_path
438 or die "$self: fork/pipe/exec for realpath(1)\n";
439 my $rel_path = do { local $/=undef; <REALPATH>; };
441 my $r = close(REALPATH);
442 die "$self: reap realpath: $!\n" if $!;
443 if (!chomp($rel_path) or $?) {
445 "$self: warning: failed to determine realpath for $spec_path in $org_subdir (exit code $?)\n";
448 if ($rel_path =~ m{^\.\./} or $rel_path eq '..') {
450 "$self: warning: $spec_path ($why) points outside $org_subdir, not following so not nailing (although cargo probably will follow it)\n";
454 my $q_subdir = "$org_subdir/$rel_path";
455 print STDERR "$self: making a note to look at $q_subdir, $why)\n"
458 push @queued_paths, [ "$q_subdir", $org_subdir, $why ];
462 foreach my $p (keys %{ $nail->{packages} }) {
463 my $v = $nail->{packages}{$p};
464 my $subdir = ref($v) ? $v->{subdir} : $v;
465 my ($gotpackage, $ws) = read_manifest($subdir, $subdir, "from [packages]");
466 $gotpackage //= '<nothing!>';
467 if ($gotpackage ne $p) {
469 "$self: warning: honouring Cargo.nail packages.$subdir=$p even though $subdir contains package $gotpackage!\n";
471 die if defined $packagemap{$p};
472 $packagemap{$p} = [ $subdir, $subdir ];
474 foreach my $subdir (@{ $nail->{subdirs} }) {
475 my ($gotpackage,$ws) = read_manifest($subdir, $subdir, "from [subdirs]");
476 if (!defined $gotpackage) {
478 "$self: warning: ignoring subdir $subdir which has no (suitable) Cargo.toml\n"
482 $packagemap{$gotpackage} //= [ $subdir, $subdir ];
484 while (my ($subdir, $org_subdir, $why) = @{ shift @queued_paths or [] }) {
485 next if $manifests{"../$subdir/Cargo.toml"};
486 my ($gotpackage, $ws) = read_manifest($subdir, $org_subdir, $why);
487 next unless $gotpackage;
488 $packagemap{$gotpackage} //= [ $subdir, $org_subdir ];
493 foreach my $p (sort keys %packagemap) {
494 print STDERR "$self: package $p in $packagemap{$p}[0]\n" if $verbose>=2;
496 foreach my $mf (keys %manifests) {
497 my ($toml, $mf_org_subdir) = @{ $manifests{$mf} };
498 foreach my $deps (get_dependency_tables $toml) {
500 foreach my $p (keys %packagemap) {
501 my $info = $deps->{$p};
502 next unless defined $info;
503 next if $packagemap{$p}[1] eq $mf_org_subdir;
504 $deps->{$p} = $info = { } unless ref $info;
505 my $oldpath = $info->{path};
506 delete $info->{version};
507 my $newpath = $worksphere.'/'.$packagemap{$p}[0];
508 print STDERR "in $mf set $p path=$newpath (was ".
509 ($oldpath // '<unset>').")\n"
511 $info->{path} = $newpath;
513 delete $info->{branch};
516 my $nailing = "$mf.nailing~";
517 unlink_or_enoent $nailing or die "$self: remove old $nailing: $!\n";
518 open N, '>', $nailing or die "$self: create new $nailing: $!\n";
519 print N to_toml($toml) or die "$self: write new $nailing: $!\n";
520 close N or die "$self: close new $nailing: $!\n";
525 if ($just_linkfarm) {
526 die "$self: --just-linkfarm but not doing out-of-tree builds!\n"
527 unless defined $oot_dir;
532 if (!defined $online) {
533 $_ = cfg_uc qw(misc online);
535 } elsif (Types::Serialiser::is_bool $_) {
540 } elsif (m/^[1ty]/) { # allow booleanish strings
541 $online = 1; # for less user frustration
542 } elsif (m/^[0fn]/) {
545 badcfg qw(misc online), "expected boolean or 'auto', found '$_'";
548 $online //= 1 if subcmd_p('online');
551 $cargo_lock_update //= subcmd_p('lock-update');
555 if (!$cargo_lock_update) {
556 push @add, qw(--locked) unless subcmd_p('!locked');
557 if (defined($oot_dir) && !subcmd_p('!manifest-path')) {
558 my $cargotoml = "${src_absdir}/Cargo.toml";
559 push @args_preface, "--manifest-path=$cargotoml" if $pass_options;
560 push @add, qw(--target-dir=target) unless subcmd_p('!target-dir');
564 if (defined($target) && !subcmd_p('!target')) {
565 if ($target =~ m{^[A-Z]}) {
566 $target = (cfgs 'arch', $target) // $archmap{$target}
567 // die "$self: --target=$target alias specified; not in cfg or map\n";
569 push @add, "--target=$target";
572 push @add, "--offline" unless $online || subcmd_p('!offline');
574 push @args_preface, @add if $pass_options;
575 die if grep { m/ / } @add;
576 $ENV{NAILINGCARGO_CARGO_OPTIONS} = "@add";
578 unshift @ARGV, @args_preface;
581 our $build_absdir; # .../Build/<subdir>
583 sub oot_massage_cmdline () {
584 return unless defined $oot_dir;
586 my $use = cfgs qw(oot use);
587 $use // die "$self: out-of-tree build, but \`oot.use' not configured\n";
588 $build_absdir = "$oot_absdir/$subdir";
590 my ($pre,$post) = ('','');
592 if (!$cargo_lock_update) {
593 push @xargs, $build_absdir;
594 ($pre, $post) = ('cd "$1"; shift; ', '');
596 push @xargs, $oot_absdir, $subdir, $src_absdir;
599 mkdir -p -- "$1"; cd "$1"; shift;
600 clean () { find -lname "$1/*" -print0 | xargs -0r rm --; }; clean;
601 find "$1" -maxdepth 1 \! -name Cargo.lock -print0 | xargs -0r -I_ ln -sf -- _ .;
603 $pre .= <<'ENDLK' if stat_exists 'Cargo.lock', 'working cargo lockfile';
605 cp -- "$1"/Cargo.lock .;
610 # $post = <<'ENDCLEAN';
614 my $addpath = (cfg_uc qw(oot path_add)) //
615 $use eq 'really' ? Types::Serialiser::true : Types::Serialiser::false;
617 !Types::Serialiser::is_bool $addpath ? $addpath :
618 $addpath ? '$HOME/.cargo/bin' :
620 if (defined $addpath) {
622 PATH=$addpath:\${PATH-/usr/local/bin:/bin:/usr/bin};
626 $pre =~ s/^\s+//mg; $pre =~ s/\s+/ /g;
627 $post =~ s/^\s+//mg; $post =~ s/\s+/ /g;
629 my $getuser = sub { cfgsn qw(oot user) };
631 my $xe = $verbose >= 2 ? 'xe' : 'e';
634 @command = (@_, 'sh',"-${xe}c",$pre.'exec "$@"','--',@xargs);
636 @command = (@_, 'sh',"-${xe}c",$pre.'"$@"; '.$post,'--',@xargs);
638 push @command, @ARGV;
640 my $command_sh = sub {
641 my $quoted = join ' ', map {
646 @command = @_, "set -${xe}; $pre $quoted; $post";
648 print STDERR "$self: out-of-tree, building in: \`$build_absdir'\n"
650 if ($use eq 'really') {
651 my $user = $getuser->();
652 my @pw = getpwnam $user or die "$self: oot.user \`$user' lookup failed\n";
653 my $homedir = $pw[7];
654 $sh_ec->('really','-u',$user,'env',"HOME=$homedir");
655 print STDERR "$self: using really to run as user \`$user'\n" if $verbose;
656 } elsif ($use eq 'ssh') {
657 my $user = $getuser->();
658 $user .= '@localhost' unless $user =~ m/\@/;
659 $command_sh->('ssh',$user);
660 print STDERR "$self: using ssh to run as \`$user'\n" if $verbose;
661 } elsif ($use eq 'command_args') {
662 my @c = cfgn_list qw(oot command);
664 print STDERR "$self: out-of-tree, adverbial command: @c\n" if $verbose;
665 } elsif ($use eq 'command_sh') {
666 my @c = cfgn_list qw(oot command);
668 print STDERR "$self: out-of-tree, ssh'ish command: @c\n" if $verbose;
669 } elsif ($use eq 'null') {
672 die "$self: oot.use mode $use not recognised\n";
679 $ENV{CARGO_MANIFEST_DIR} = $src_absdir;
680 $ENV{NAILINGCARGO_MANIFEST_DIR} = $src_absdir;
681 $ENV{NAILINGCARGO_WORKSPHERE} = $worksphere;
682 $ENV{NAILINGCARGO_BUILDSPHERE} = $oot_absdir;
683 delete $ENV{NAILINGCARGO_BUILDSPHERE} unless $oot_absdir;
684 $ENV{NAILINGCARGO_BUILD_DIR} = $build_absdir // $src_absdir;
690 if ($want_uninstall) {
692 foreach my $mf (keys %manifests) {
693 eval { uninstall1($mf,1); 1; } or warn "$@";
695 eval { unaltcargolock(1); 1; } or warn "$@";
699 sub consider_directories () {
700 return unless defined $oot_dir;
701 my $bsubdir = "../$oot_dir/$subdir";
702 return if stat $bsubdir;
703 die "$0: build directory $bsubdir inaccessible\n"
705 return if $cargo_lock_update; # will make it
706 die "$0: build directory $bsubdir does not exist, and not in Cargo.lock update mode!\n";
709 our $cleanup_cargo_lock;
711 foreach my $mf (keys %manifests) {
712 link "$mf", "$mf.unnailed" or $!==EEXIST
713 or die "$self: make backup link $mf.unnailed: $!\n";
716 if (defined($alt_cargo_lock)) {
717 if (@alt_cargo_lock_stat) {
718 print STDERR "$self: using alt_cargo_lock `$alt_cargo_lock'..."
720 if (link $alt_cargo_lock, 'Cargo.lock') {
721 print STDERR " linked\n" if $verbose>=3;
722 } elsif ($! != EEXIST) {
723 print STDERR "\n" if $verbose>=3;
724 die "$self: make \`Cargo.lock' available as \`$alt_cargo_lock': $!\n";
726 print STDERR "checking quality." if $verbose>=3;
727 my @lock_stat = stat 'Cargo.lock'
728 or die "$self: stat Cargo.lock (for alt check: $!\n";
729 same_file(\@alt_cargo_lock_stat, \@lock_stat)
731 "$self: \`Cargo.lock' and alt file \`$alt_cargo_lock' both exist and are not the same file!\n";
733 $cleanup_cargo_lock = 1;
735 $cleanup_cargo_lock = 1;
736 # If Cargo.lock exists and alt doesn't, that means either
737 # that a previous run was interrupted, or that the user has
745 my $nailed = "$mf.nailed~"; $nailed =~ s{/([^/]+)$}{/.$1} or die;
750 my @our_unfound_stab = stat_exists('Cargo.toml', 'local Cargo.toml')
752 foreach my $mf (keys %manifests) {
753 if (@our_unfound_stab) {
754 if (stat_exists $mf, "manifest in to-be-nailed directory") {
755 my @mf_stab = stat _ ;
756 if ("@mf_stab[0..1]" eq "@our_unfound_stab[0..1]") {
757 @our_unfound_stab = ();
762 my $nailing = "$mf.nailing~";
763 my $nailed = nailed($mf);
766 if (open NN, '<', $nailed) {
767 $diff = compare($nailing, \*NN);
768 die "$self: compare $nailing and $nailed: $!" if $diff<0;
770 $!==ENOENT or die "$self: check previous $nailed: $!\n";
780 rename $use, $mf or die "$self: install nailed $use: $!\n";
781 unlink_or_enoent $rm or die "$self: remove old $rm: $!\n";
782 print STDERR "$self: nailed $mf\n" if $verbose>=3;
785 if (@our_unfound_stab) {
787 "$self: *WARNING* cwd is not in Cargo.nail thbough it has Cargo.toml!\n";
792 my $r = system @ARGV;
796 print STDERR "$self: could not execute $ARGV[0]: $!\n";
798 } elsif ($r & 0xff00) {
799 print STDERR "$self: $ARGV[0] failed (exit status $r)\n";
802 print STDERR "$self: $ARGV[0] died due to signal! (wait status $r)\n";
807 sub cargo_lock_update_after () {
808 if ($cargo_lock_update && !$just_linkfarm) {
809 # avoids importing File::Copy and the error handling is about as good
811 my $r= system qw(cp --), "$build_absdir/Cargo.lock", "Cargo.lock";
812 die "$self: run cp: $! $?" if $r<0 || $r & 0xff;
813 die "$self: failed to update local Cargo.lock (wait status $r)\n" if $r;
817 sub uninstall1 ($$) {
818 my ($mf, $enoentok) = @_;
819 my $unnailed = "$mf.unnailed";
820 rename $unnailed, $mf or ($enoentok && $!==ENOENT)
821 or die "$self: failed to restore: rename $unnailed back to $mf: $!\n";
824 sub unaltcargolock ($) {
826 return unless $cleanup_cargo_lock;
827 die 'internal error!' unless defined $alt_cargo_lock;
829 # we ignore $enoentok because we don't know if one was supposed to
832 rename('Cargo.lock', $alt_cargo_lock) or $!==ENOENT or die
833 "$self: cleanup: rename possibly-updated \`Cargo.lock' to \`$alt_cargo_lock': $!\n";
835 unlink 'Cargo.lock' or $!==ENOENT or die
836 "$self: cleanup: remove \`Cargo.lock' in favour of \`$alt_cargo_lock': $!\n";
837 # ^ this also helps clean up the stupid rename() corner case
841 foreach my $mf (keys %manifests) {
842 my $nailed = nailed($mf);
843 link $mf, $nailed or die "$self: preserve (link) $mf as $nailed: $!\n";
852 # Loop exit condition:
855 # $is_cargo==1 <cargo-command> <cargo-opts> [--] <subcmd>...
856 # $is_cargo==0 <build-command>...
860 die "$self: need cargo subcommand\n"
861 unless $noact || $just_linkfarm;;
862 push @ARGV, "CARGO-SUBCOMMAND"; # dummy, user may see it
868 my $not_a_nailing_opt = sub { # usage 1
869 unshift @ARGV, $orgopt;
870 unshift @ARGV, 'cargo';
872 no warnings qw(exiting);
875 $not_a_nailing_opt->() unless m{^-};
876 $not_a_nailing_opt->() if $_ eq '--';
878 if ($_ eq '---') { # usage 2 or 3
880 die "$self: --- must be followed by build command\n" unless $noact;
881 push @ARGV, 'BUILD-COMMAND';
883 if ($ARGV[0] eq '--') { # usage 3
886 } elsif (grep { $_ eq '--' } @ARGV) { # usage 2
888 } elsif ($ARGV[0] =~ m{[^/]*cargo[^/]*$}) { # usage 2
899 } elsif (s{^-v}{-}) {
901 } elsif (s{^-q}{-}) {
903 } elsif (s{^-n}{-}) {
905 } elsif (s{^-s(.+)}{-}s) {
907 } elsif (s{^-([uU])}{-}) {
908 $cargo_lock_update = $1=~m/[a-z]/;
909 } elsif (s{^-([cC])}{-}) {
910 $pass_options = $1=~m/[a-z]/;
911 } elsif (s{^-D}{-}) {
913 } elsif (s{^-T(.+)}{-}s) {
915 } elsif (s{^-([oO])}{-}) {
916 $online = $1=~m/[a-z]/;
918 die "$self: unknown short option(s) $_\n" unless $_ eq $orgopt;
919 $not_a_nailing_opt->();
922 } elsif (s{^--help$}{}) {
924 } elsif (s{^--man(?:ual)?$}{}) {
926 } elsif (s{^--target=}{}) {
928 } elsif (m{^--(on|off)line$}) {
929 $online = $1 eq 'on';
930 } elsif (m{^--just-linkfarm$}) {
932 $cargo_lock_update= 1;
933 } elsif (m{^--leave-nailed$}) {
935 } elsif (s{^--subcommand-props=}{}) {
936 my @props = split /\,/, $_;
938 if (!%subcmd_prop_ok) {
939 foreach my $v (\@subcmd_xprops, values %subcmd_props) {
940 $subcmd_prop_ok{$_}=1 foreach @$v;
944 or die "$self: unknown subcommand property \`$_'\n"
946 $cargo_subcmd = \@props;
947 } elsif (m{^--(no-)?cargo-lock-update}) {
948 $cargo_lock_update= !!$1;
950 $not_a_nailing_opt->();
958 @args_preface = shift @ARGV;
959 while (defined($_ = shift @ARGV)) {
960 if (!m{^-}) { unshift @ARGV, $_; last; }
961 if ($_ eq '--') { last; }
962 push @args_preface, $_;
964 @ARGV || die "$self: need cargo subcommand\n";
965 $cargo_subcmd //= $ARGV[0];
968 $cargo_subcmd //= '';
971 push @args_preface, shift @ARGV;
973 if (!ref($cargo_subcmd)) {
974 print STDERR " cargo_subcmd lookup $cargo_subcmd\n" if $dump;
975 $cargo_subcmd = $subcmd_props{$cargo_subcmd} // [ ];
978 print STDERR " cargo_subcmd props @$cargo_subcmd\n" if $dump;
980 $cargo_subcmd{$_} = 1 foreach @$cargo_subcmd;
981 $cargo_subcmd = \%cargo_subcmd;
988 consider_alt_cargo_lock();
993 consider_directories();
994 our @display_cmd = @ARGV;
995 oot_massage_cmdline();
1001 print STDERR Dumper(\%manifests) if $dump>=2;
1002 print STDERR Dumper(\%packagemap, \@ARGV,
1003 { src_absdir => $src_absdir,
1004 worksphere => $worksphere,
1006 oot_dir => $oot_dir,
1007 oot_absdir => $oot_absdir,
1008 build_absdir => $build_absdir });
1014 $want_uninstall = !$leave_nailed;
1018 printf STDERR "$self: nailed (%s manifests, %s packages)%s\n",
1019 (scalar keys %manifests), (scalar keys %packagemap),
1020 (defined($alt_cargo_lock) and ", using `$alt_cargo_lock'")
1023 print STDERR "$self: invoking: @display_cmd\n" if $verbose;
1024 my $estatus = invoke();
1026 cargo_lock_update_after();
1028 uninstall() unless $leave_nailed;
1029 $want_uninstall = 0;
1031 print STDERR "$self: unnailed. status $estatus.\n" if $verbose;