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 --doc --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 --just-run Run the command, don't do cargo stuff
32 --no-nail Do not nail, just run the command.
33 --no-cargo-lock-manip Do not manipulate Cargo.lock.
34 --no-concurrency-lock Do not take the concurrency lock.
36 -s<subcommand> Treat command as `cargo <subcommand>`
37 --subcommand-props=<prop>,... Override command props (see docs)
45 use Types::Serialiser;
46 use File::Glob qw(bsd_glob GLOB_ERR GLOB_BRACE GLOB_NOMAGIC);
51 RPI => 'arm-unknown-linux-gnueabihf',
55 $self = $0; $self =~ s{^.*/(?=.)}{};
58 while ($deref =~ m{^/}) {
59 my $link = readlink $deref;
62 or die "$self: checking our script location $deref: $!\n";
63 $deref =~ s{/[^/]+$}{}
64 or die "$self: unexpected script path: $deref\n";
66 unshift @INC, $deref."/TOML-Tiny/lib";
69 last if $link !~ m{^/};
74 use Fcntl qw(LOCK_EX);
76 use TOML::Tiny::Faithful;
78 our $src_absdir = getcwd() // die "$self: getcwd failed: $!\n";
80 our $worksphere = $src_absdir;
81 $worksphere =~ s{/([^/]+)$}{}
82 or die "$self: cwd \`$worksphere' unsupported!\n";
83 our $subdir = $1; # leafname
85 our $lockfile = "../.nailing-cargo.lock";
89 our $command_is_cargo;
91 our $cargo_lock_update;
103 our %subcmd_props = (
104 # build (default) =>[qw( )],
105 'generate-lockfile'=>[qw( lock-update !target !target-dir )],
106 update =>[qw( lock-update !target online )],
107 fetch =>[qw( online !target-dir )],
110 our @subcmd_xprops = qw(!manifest-path !offline !locked);
118 print $usage or die $!;
123 my $manual = ($base_path // '.').'/README.md';
124 stat $manual or die "$self: manual not found at $manual: $!\n";;
125 exec 'sh','-ec', 'pandoc -- "$1" 2>&1 | w3m -T text/html', '--', $manual;
126 die "$self: exec sh failed: $!";
129 sub read_or_enoent ($) {
131 if (!open R, '<', $fn) {
132 return undef if $!==ENOENT;
133 die "$self: open $fn: $!\n";
136 my ($r) = <R> // die "$self: read $fn: $!\n";
140 sub stat_exists ($$) {
141 my ($fn, $what) = @_;
142 if (stat $fn) { return 1; }
143 $!==ENOENT or die "$self: stat $what: $fn: $!\n";
148 print STDERR " subcmd_p ".(join ' ', keys %$cargo_subcmd)." | @_\n"
150 $cargo_subcmd->{$_[0]}
153 sub toml_or_enoent ($$) {
155 my $toml = read_or_enoent($f) // return;
156 print STDERR "Read TOML from $f\n" if $dump;
157 my ($v,$e) = from_toml($toml);
160 die "$self: parse TOML: $what: $f: $e\n";
162 die "$e ?" if length $e;
166 sub load1config ($) {
168 my $toml = toml_or_enoent($f, "config file");
169 push @configs, $toml if defined $toml;
173 my $dotfile = ".nailing-cargo.toml";
174 load1config("../Nailing-Cargo.toml");
175 load1config($dotfile);
176 load1config("$ENV{HOME}/$dotfile") if defined $ENV{HOME};
177 load1config("/etc/nailing-cargo/cfg.toml");
180 sub unlink_or_enoent ($) { unlink $_[0] or $!==ENOENT; }
184 "@$x[0..5]" eq "@$y[0..5]";
188 return unless $do_lock;
191 open LOCK, ">", $lockfile or die "$self: open/create $lockfile: $!\n";
192 flock LOCK, LOCK_EX or die "$self: lock $lockfile: $!\n";
193 my @fstat = stat LOCK or die "$self: fstat: $!\n";
194 my @stat = stat $lockfile;
196 next if $! == ENOENT;
197 die "$self: stat $lockfile: $!\n";
199 last if same_file(\@fstat,\@stat);
203 unlink $lockfile or die "$self: removing lockfile: $!\n";
211 die "$self: config key \`@_': $m\n";
215 foreach my $cfg (@configs) {
218 last unless defined $v;
219 ref($v) eq 'HASH' or badcfg @_, "parent key \`$k' is not a hash";
222 return $v if defined $v;
230 my $got = ref($v) || 'scalar';
231 return $v if !defined($v) || $got eq $exp;
232 badcfg @_, "found \L$got\E, expected \L$exp\E";
233 # ^ toml doesn't make refs to scalars, so this is unambiguous
238 (cfge $exp, @_) // badcfg @_, "missing";
241 sub cfgs { cfge 'scalar', @_ }
242 sub cfgsn { cfgn 'scalar', @_ }
246 return $v if !defined($v) || Types::Serialiser::is_bool $v;
247 badcfg @_, "expected boolean";
251 my $l = cfge 'ARRAY', @_;
252 foreach my $x (@$l) {
253 !ref $x or badcfg @_, "list contains non-scalar element";
259 my $nailfile = "../Cargo.nail";
260 open N, '<', $nailfile or die "$self: open $nailfile: $!\n";
262 my $toml = <N> // die "$self: read $nailfile: $!";
264 if ($toml !~ m{^\s*\[/}m &&
265 $toml !~ m{^[^\n\#]*\=}m &&
266 # old non-toml syntax
267 $toml =~ s{^[ \t]*([-_0-9a-z]+)[ \t]+(\S+)[ \t]*$}{$1 = \"$2\"}mig) {
268 $toml =~ s{^}{[packages\]\n};
270 $toml =~ s{^[ \t]*\-[ \t]*\=[ \t]*(\"[-_0-9a-z]+\"\n?)$}{
273 $toml = "subdirs = [\n".(join '', map { "$_\n" } @sd)."]\n".$toml;
277 ($nail,$e) = from_toml($toml);
278 if (!defined $nail) {
281 print STDERR "$self: $nailfile transformed into TOML:\n$toml\n";
284 die "$self: parse $nailfile: $e\n";
286 die "$e ?" if length $e;
288 $nail->{subdirs} //= [ ];
290 if (!ref $nail->{subdirs}) {
293 map { s/^\s+//; s/\s+$//; $_; }
299 unshift @configs, $nail;
302 sub get_dependency_tables ($) {
304 my @keys = qw(dependencies build-dependencies dev-dependencies);
308 foreach my $k (@keys) {
309 my $deps = $node->{$k};
310 push @r, $deps if $deps;
314 foreach my $target_node (values %{ $toml->{target} // { } }) {
315 $process->($target_node);
320 our @alt_cargo_lock_stat;
322 sub consider_alt_cargo_lock () {
323 my @ck = qw(alt_cargo_lock);
324 # User should *either* have Cargo.lock in .gitignore,
325 # or expect to commit Cargo.lock.example ($alt_cargo_lock)
327 return unless $do_cargo_lock;
329 $alt_cargo_lock = (cfg_uc @ck);
332 if (defined($alt_cargo_lock) && ref($alt_cargo_lock) eq 'HASH') {
333 $force = cfg_bool qw(alt_cargo_lock force);
334 my @ck = qw(alt_cargo_lock file);
335 $alt_cargo_lock = cfg_uc @ck;
337 $alt_cargo_lock //= Types::Serialiser::true;
339 if (Types::Serialiser::is_bool $alt_cargo_lock) {
340 if (!$alt_cargo_lock) { $alt_cargo_lock = undef; return; }
341 $alt_cargo_lock = 'Cargo.lock.example';
344 if (ref($alt_cargo_lock) || $alt_cargo_lock =~ m{/}) {
345 badcfg @ck, "expected boolean, or leafname";
348 if (!stat_exists $alt_cargo_lock, "alt_cargo_lock") {
349 $alt_cargo_lock = undef unless $force;
353 @alt_cargo_lock_stat = stat _;
356 our $oot_dir; # oot.dir or "Build"
359 sub consider_oot () {
360 $oot_dir = cfgs qw(oot dir);
361 my $use = cfgs qw(oot use);
362 unless (defined($oot_dir) || defined($use) ||
363 defined(cfg_uc qw(oot user))) {
366 if (($use//'') eq 'disable') {
370 $oot_clean //= cfg_bool qw(oot clean);
371 $oot_dir //= 'Build';
372 $oot_absdir = ($oot_dir !~ m{^/} ? "$worksphere/" : ""). $oot_dir;
380 sub read_manifest ($$$) {
381 my ($subdir, $org_subdir, $why) = @_;
382 my $manifest = "../$subdir/Cargo.toml";
383 print STDERR "$self: reading $manifest...\n" if $verbose>=4;
384 if (defined $manifests{$manifest}) {
386 "$self: warning: $subdir: specified more than once!".
387 " (ignoring $why)\n";
390 foreach my $try ("$manifest.unnailed", "$manifest") {
391 my $toml = toml_or_enoent($try, "manifest, in $why") // next;
392 my $ws = $toml->{workspace};
394 queue_workspace_members($subdir, $org_subdir, $ws, "$subdir, $why");
396 my $p = $toml->{package}{name};
397 if (!defined $p and !defined $ws) {
399 "$self: warning: $subdir, $why: missing package.name in $try, ignoring\n";
402 $manifests{$manifest} = [ $toml, $org_subdir ] if $p;
403 foreach my $dep (get_dependency_tables $toml) {
404 next unless defined $dep->{path};
405 queue_referenced_path($dep->{path}, $org_subdir,
406 "dependency of $subdir, $why");
413 sub queue_workspace_members ($$) {
414 my ($subdir, $org_subdir, $ws_toml, $what) = @_;
415 # We need to (more or less) reimplement the cargo workspace
416 # membership algorithm (see the "workspaces" section of the cargo
417 # reference). How tiresome.
419 # It's not quite the same for us because we aren't interested in
420 # whether cargo thinks things are "in the workspace". But we do
421 # need to do the automatic discover.
423 my @include = @{ $ws_toml->{members} // [ ] };
424 my $exclude = $ws_toml->{exclude} // [ ];
427 s/[^*?0-9a-zA-Z_]/\\$&/g;
432 foreach my $spec (@include) {
433 if ($spec =~ m{^/}) {
435 "$self: warning: absolute workspace member $spec in $what (not nailing, but cargo will probably use it)\n";
438 my $spec_glob = "../$subdir/$spec";
439 my $globflags = GLOB_ERR|GLOB_BRACE|GLOB_NOMAGIC;
440 foreach my $globent (bsd_glob($spec_glob, $globflags)) {
441 next if grep { $globent =~ m{^$_$} } @exclude;
442 queue_referenced_path($globent, $org_subdir,
443 "member of workspace $what");
448 sub queue_referenced_path ($$$) {
449 my ($spec_path, $org_subdir, $why) = @_;
451 qw(realpath), "--relative-to=../$org_subdir", "--", $spec_path
452 or die "$self: fork/pipe/exec for realpath(1)\n";
453 my $rel_path = do { local $/=undef; <REALPATH>; };
455 my $r = close(REALPATH);
456 die "$self: reap realpath: $!\n" if $!;
457 if (!chomp($rel_path) or $?) {
459 "$self: warning: failed to determine realpath for $spec_path in $org_subdir (exit code $?)\n";
462 if ($rel_path =~ m{^\.\./} or $rel_path eq '..') {
464 "$self: warning: $spec_path ($why) points outside $org_subdir, not following so not nailing (although cargo probably will follow it)\n";
468 my $q_subdir = "$org_subdir/$rel_path";
469 print STDERR "$self: making a note to look at $q_subdir, $why)\n"
472 push @queued_paths, [ "$q_subdir", $org_subdir, $why ];
476 # We (and our callees) populate %packagemap and %manifest, so if we
477 # don't run, they remain empty and nothing is nailed.
478 return unless $do_nail;
480 foreach my $p (keys %{ $nail->{packages} }) {
481 my $v = $nail->{packages}{$p};
482 my $subdir = ref($v) ? $v->{subdir} : $v;
483 my ($gotpackage, $ws) = read_manifest($subdir, $subdir, "from [packages]");
484 $gotpackage //= '<nothing!>';
485 if ($gotpackage ne $p) {
487 "$self: warning: honouring Cargo.nail packages.$subdir=$p even though $subdir contains package $gotpackage!\n";
489 die if defined $packagemap{$p};
490 $packagemap{$p} = [ $subdir, $subdir ];
492 foreach my $subdir (@{ $nail->{subdirs} }) {
493 my ($gotpackage,$ws) = read_manifest($subdir, $subdir, "from [subdirs]");
494 if (!defined $gotpackage) {
496 "$self: warning: ignoring subdir $subdir which has no (suitable) Cargo.toml\n"
500 $packagemap{$gotpackage} //= [ $subdir, $subdir ];
502 while (my ($subdir, $org_subdir, $why) = @{ shift @queued_paths or [] }) {
503 next if $manifests{"../$subdir/Cargo.toml"};
504 my ($gotpackage, $ws) = read_manifest($subdir, $org_subdir, $why);
505 next unless $gotpackage;
506 $packagemap{$gotpackage} //= [ $subdir, $org_subdir ];
511 foreach my $p (sort keys %packagemap) {
512 print STDERR "$self: package $p in $packagemap{$p}[0]\n" if $verbose>=2;
514 foreach my $mf (keys %manifests) {
515 die "internal error" unless $do_nail; # belt and braces
517 my ($toml, $mf_org_subdir) = @{ $manifests{$mf} };
518 foreach my $deps (get_dependency_tables $toml) {
520 foreach my $p (keys %packagemap) {
521 my $info = $deps->{$p};
522 next unless defined $info;
523 next if $packagemap{$p}[1] eq $mf_org_subdir;
524 $deps->{$p} = $info = { } unless ref $info;
525 my $oldpath = $info->{path};
526 delete $info->{version};
527 my $newpath = $worksphere.'/'.$packagemap{$p}[0];
528 print STDERR "in $mf set $p path=$newpath (was ".
529 ($oldpath // '<unset>').")\n"
531 $info->{path} = $newpath;
533 delete $info->{branch};
536 my $nailing = "$mf.nailing~";
537 unlink_or_enoent $nailing or die "$self: remove old $nailing: $!\n";
538 open N, '>', $nailing or die "$self: create new $nailing: $!\n";
539 print N to_toml($toml) or die "$self: write new $nailing: $!\n";
540 close N or die "$self: close new $nailing: $!\n";
545 if ($just_linkfarm) {
546 die "$self: --just-linkfarm but not doing out-of-tree builds!\n"
547 unless defined $oot_dir;
552 if (!defined $online) {
553 $_ = cfg_uc qw(misc online);
555 } elsif (Types::Serialiser::is_bool $_) {
560 } elsif (m/^[1ty]/) { # allow booleanish strings
561 $online = 1; # for less user frustration
562 } elsif (m/^[0fn]/) {
565 badcfg qw(misc online), "expected boolean or 'auto', found '$_'";
568 $online //= 1 if subcmd_p('online');
571 $cargo_lock_update //= subcmd_p('lock-update');
572 $linkfarm_depth //= $cargo_lock_update ? 'shallow' : '';
576 if (!$cargo_lock_update) {
577 push @add, qw(--locked) unless subcmd_p('!locked');
579 if ($linkfarm_depth eq '') {
580 if (defined($oot_dir) && !subcmd_p('!manifest-path')) {
581 my $cargotoml = "${src_absdir}/Cargo.toml";
582 push @args_preface, "--manifest-path=$cargotoml" if $pass_options;
583 push @add, qw(--target-dir=target) unless subcmd_p('!target-dir');
587 if (defined($target) && !subcmd_p('!target')) {
588 if ($target =~ m{^[A-Z]}) {
589 $target = (cfgs 'arch', $target) // $archmap{$target}
590 // die "$self: --target=$target alias specified; not in cfg or map\n";
592 push @add, "--target=$target";
595 push @add, "--offline" unless $online || subcmd_p('!offline');
597 push @args_preface, @add if $pass_options;
598 die if grep { m/ / } @add;
599 $ENV{NAILINGCARGO_CARGO_OPTIONS} = "@add";
601 unshift @ARGV, @args_preface;
604 our $build_absdir; # .../Build/<subdir>
606 sub oot_massage_cmdline () {
607 return unless defined $oot_dir;
609 my $use = cfgs qw(oot use);
610 $use // die "$self: out-of-tree build, but \`oot.use' not configured\n";
611 $build_absdir = "$oot_absdir/$subdir";
613 my ($pre,$post) = ('','');
615 if ($linkfarm_depth eq '') {
616 push @xargs, $build_absdir;
617 ($pre, $post) = ('cd "$1"; shift; ', '');
619 push @xargs, $oot_absdir, $subdir, $src_absdir;
622 mkdir -p -- "$1"; cd "$1"; shift; src="$1"; shift;
624 if ($linkfarm_depth eq 'shallow') {
625 $pre.= <<'END_SHALLOW';
626 clean () { find -lname "$src/*" -print0 | xargs -0r rm --; }; clean;
627 find "$src" -maxdepth 1 \! -name Cargo.lock -print0 |
628 xargs -0r sh -ec 'for f in "$@"; do
634 $pre .= <<'END_EITHER_DEEP_DIRS';
635 clean () { find -follow -lname "$src/*" -print0 | xargs -0r rm --; };
636 (set -e; cd "$src"; find . \! -name Cargo.lock \! \( -name .git -prune \) \! -path . \! -name .git -type d -print0) |
637 xargs -0r sh -ec 'for f in "$@"; do
638 rm -f "$f" 2>/dev/null ||:;
642 if ($linkfarm_depth eq 'git') {
643 $pre .= <<'END_FILES_GIT'
644 (set -e; cd "$src"; git ls-files --exclude-standard -co -z) |
646 } elsif ($linkfarm_depth eq 'full') {
647 $pre .= <<'END_FILES_FULL'
648 (set -e; cd "$src"; find . \! -name Cargo.lock \! \( -name .git -prune \) \! -type d -print0) |
651 $pre .= <<'END_DEEP';
652 xargs -0r sh -ec 'src="$1"; shift; for f in "$@"; do
653 ln -sf -- "$src/${f#./}" "$f";
657 $pre .= <<'ENDLK' if $do_cargo_lock;
658 if test -e Cargo.lock; then
660 cp -- "$src"/Cargo.lock .;
663 $post = <<'ENDCLEAN' if $oot_clean && !$just_linkfarm;
667 my $addpath = (cfg_uc qw(oot path_add)) //
668 $use eq 'really' ? Types::Serialiser::true : Types::Serialiser::false;
670 !Types::Serialiser::is_bool $addpath ? $addpath :
671 $addpath ? '$HOME/.cargo/bin' :
673 if (defined $addpath) {
675 PATH=$addpath:\${PATH-/usr/local/bin:/bin:/usr/bin};
679 $pre =~ s/^\s+//mg; $pre =~ s/\s+/ /g;
680 $post =~ s/^\s+//mg; $post =~ s/\s+/ /g;
682 my $getuser = sub { cfgsn qw(oot user) };
684 my $xe = $verbose >= 2 ? 'xe' : 'e';
687 @command = (@_, 'sh',"-${xe}c",$pre.'exec "$@"','--',@xargs);
689 @command = (@_, 'sh',"-${xe}c",$pre.'"$@"; '.$post,'--',@xargs);
691 push @command, @ARGV;
693 my $command_sh = sub {
694 my $quoted = join ' ', map {
699 @command = @_, "set -${xe}; $pre $quoted; $post";
701 print STDERR "$self: out-of-tree, building in: \`$build_absdir'\n"
703 if ($use eq 'really') {
704 my $user = $getuser->();
705 my @pw = getpwnam $user or die "$self: oot.user \`$user' lookup failed\n";
706 my $homedir = $pw[7];
707 $sh_ec->('really','-u',$user,'env',"HOME=$homedir");
708 print STDERR "$self: using really to run as user \`$user'\n" if $verbose;
709 } elsif ($use eq 'ssh') {
710 my $user = $getuser->();
711 $user .= '@localhost' unless $user =~ m/\@/;
712 $command_sh->('ssh',$user);
713 print STDERR "$self: using ssh to run as \`$user'\n" if $verbose;
714 } elsif ($use eq 'command_args') {
715 my @c = cfgn_list qw(oot command);
717 print STDERR "$self: out-of-tree, adverbial command: @c\n" if $verbose;
718 } elsif ($use eq 'command_sh') {
719 my @c = cfgn_list qw(oot command);
721 print STDERR "$self: out-of-tree, ssh'ish command: @c\n" if $verbose;
722 } elsif ($use eq 'null') {
725 die "$self: oot.use mode $use not recognised\n";
732 $ENV{CARGO_MANIFEST_DIR} = $src_absdir;
733 $ENV{NAILINGCARGO_MANIFEST_DIR} = $src_absdir;
734 $ENV{NAILINGCARGO_WORKSPHERE} = $worksphere;
735 $ENV{NAILINGCARGO_BUILDSPHERE} = $oot_absdir;
736 delete $ENV{NAILINGCARGO_BUILDSPHERE} unless $oot_absdir;
737 $ENV{NAILINGCARGO_BUILD_DIR} = $build_absdir // $src_absdir;
743 if ($want_uninstall) {
745 foreach my $mf (keys %manifests) {
746 eval { uninstall1($mf,1); 1; } or warn "$@";
748 eval { unaltcargolock(1); 1; } or warn "$@";
752 sub consider_directories () {
753 return unless defined $oot_dir;
754 my $bsubdir = "../$oot_dir/$subdir";
755 return if stat $bsubdir;
756 die "$0: build directory $bsubdir inaccessible\n"
758 return if $cargo_lock_update; # will make it
759 die "$0: build directory $bsubdir does not exist, and not in Cargo.lock update mode!\n";
762 our $cleanup_cargo_lock;
764 foreach my $mf (keys %manifests) {
765 link "$mf", "$mf.unnailed" or $!==EEXIST
766 or die "$self: make backup link $mf.unnailed: $!\n";
769 if (defined($alt_cargo_lock)) {
770 die 'internal error' unless $do_cargo_lock;
771 if (@alt_cargo_lock_stat) {
772 print STDERR "$self: using alt_cargo_lock `$alt_cargo_lock'..."
774 if (link $alt_cargo_lock, 'Cargo.lock') {
775 print STDERR " linked\n" if $verbose>=3;
776 } elsif ($! != EEXIST) {
777 print STDERR "\n" if $verbose>=3;
778 die "$self: make \`Cargo.lock' available as \`$alt_cargo_lock': $!\n";
780 print STDERR "checking quality." if $verbose>=3;
781 my @lock_stat = stat 'Cargo.lock'
782 or die "$self: stat Cargo.lock (for alt check: $!\n";
783 same_file(\@alt_cargo_lock_stat, \@lock_stat)
785 "$self: \`Cargo.lock' and alt file \`$alt_cargo_lock' both exist and are not the same file!\n";
787 $cleanup_cargo_lock = 1;
789 $cleanup_cargo_lock = 1;
790 # If Cargo.lock exists and alt doesn't, that means either
791 # that a previous run was interrupted, or that the user has
799 my $nailed = "$mf.nailed~"; $nailed =~ s{/([^/]+)$}{/.$1} or die;
804 my @our_unfound_stab = stat_exists('Cargo.toml', 'local Cargo.toml')
806 foreach my $mf (keys %manifests) {
807 if (@our_unfound_stab) {
808 if (stat_exists $mf, "manifest in to-be-nailed directory") {
809 my @mf_stab = stat _ ;
810 if ("@mf_stab[0..1]" eq "@our_unfound_stab[0..1]") {
811 @our_unfound_stab = ();
816 my $nailing = "$mf.nailing~";
817 my $nailed = nailed($mf);
820 if (open NN, '<', $nailed) {
821 $diff = compare($nailing, \*NN);
822 die "$self: compare $nailing and $nailed: $!" if $diff<0;
824 $!==ENOENT or die "$self: check previous $nailed: $!\n";
834 rename $use, $mf or die "$self: install nailed $use: $!\n";
835 unlink_or_enoent $rm or die "$self: remove old $rm: $!\n";
836 print STDERR "$self: nailed $mf\n" if $verbose>=3;
839 if (@our_unfound_stab && $do_nail) {
841 "$self: *WARNING* cwd is not in Cargo.nail thbough it has Cargo.toml!\n";
846 my $r = system @ARGV;
850 print STDERR "$self: could not execute $ARGV[0]: $!\n";
852 } elsif ($r & 0xff00) {
853 print STDERR "$self: $ARGV[0] failed (exit status $r)\n";
856 print STDERR "$self: $ARGV[0] died due to signal! (wait status $r)\n";
861 sub cargo_lock_update_after () {
862 if ($do_cargo_lock && $cargo_lock_update && !$just_linkfarm) {
863 # avoids importing File::Copy and the error handling is about as good
865 my $r= system qw(cp --), "$build_absdir/Cargo.lock", "Cargo.lock";
866 die "$self: run cp: $! $?" if $r<0 || $r & 0xff;
867 die "$self: failed to update local Cargo.lock (wait status $r)\n" if $r;
871 sub uninstall1 ($$) {
872 my ($mf, $enoentok) = @_;
873 my $unnailed = "$mf.unnailed";
874 rename $unnailed, $mf or ($enoentok && $!==ENOENT)
875 or die "$self: failed to restore: rename $unnailed back to $mf: $!\n";
878 sub unaltcargolock ($) {
880 return unless $cleanup_cargo_lock;
881 die 'internal error!' unless $do_cargo_lock && defined $alt_cargo_lock;
883 # we ignore $enoentok because we don't know if one was supposed to
886 rename('Cargo.lock', $alt_cargo_lock) or $!==ENOENT or die
887 "$self: cleanup: rename possibly-updated \`Cargo.lock' to \`$alt_cargo_lock': $!\n";
889 unlink 'Cargo.lock' or $!==ENOENT or die
890 "$self: cleanup: remove \`Cargo.lock' in favour of \`$alt_cargo_lock': $!\n";
891 # ^ this also helps clean up the stupid rename() corner case
895 foreach my $mf (keys %manifests) {
896 my $nailed = nailed($mf);
897 link $mf, $nailed or die "$self: preserve (link) $mf as $nailed: $!\n";
906 # Loop exit condition:
909 # $is_cargo==1 <cargo-command> <cargo-opts> [--] <subcmd>...
910 # $is_cargo==0 <build-command>...
914 die "$self: need cargo subcommand\n"
915 unless $noact || $just_linkfarm;;
916 push @ARGV, "CARGO-SUBCOMMAND"; # dummy, user may see it
922 my $not_a_nailing_opt = sub { # usage 1
923 unshift @ARGV, $orgopt;
924 unshift @ARGV, 'cargo';
926 no warnings qw(exiting);
929 $not_a_nailing_opt->() unless m{^-};
930 $not_a_nailing_opt->() if $_ eq '--';
932 if ($_ eq '---') { # usage 2 or 3
934 die "$self: --- must be followed by build command\n" unless $noact;
935 push @ARGV, 'BUILD-COMMAND';
937 if ($ARGV[0] eq '--') { # usage 3
940 } elsif (grep { $_ eq '--' } @ARGV) { # usage 2
942 } elsif ($ARGV[0] =~ m{[^/]*cargo[^/]*$}) { # usage 2
953 } elsif (s{^-v}{-}) {
955 } elsif (s{^-q}{-}) {
957 } elsif (s{^-n}{-}) {
959 } elsif (s{^-s(.+)}{-}s) {
961 } elsif (s{^-([uU])}{-}) {
962 $cargo_lock_update = $1=~m/[a-z]/;
963 } elsif (s{^-([cC])}{-}) {
964 $pass_options = $1=~m/[a-z]/;
965 } elsif (s{^-D}{-}) {
967 } elsif (s{^-T(.+)}{-}s) {
969 } elsif (s{^-([oO])}{-}) {
970 $online = $1=~m/[a-z]/;
972 die "$self: unknown short option(s) $_\n" unless $_ eq $orgopt;
973 $not_a_nailing_opt->();
976 } elsif (s{^--help$}{}) {
978 } elsif (s{^--(?:doc|man|manual)?$}{}) {
980 } elsif (s{^--target=}{}) {
982 } elsif (m{^--(on|off)line$}) {
983 $online = $1 eq 'on';
984 } elsif (m{^--just-linkfarm(?:=(shallow|git|full))?$}) {
986 $linkfarm_depth = 1 if $1;
987 $cargo_lock_update= 1; # will set $linkfarm_detph to 1 by default
988 } elsif (m{^--linkfarm(?:=(no|shallow|git|full))?$}) {
989 $linkfarm_depth = $1 || 'git';
990 } elsif (m{^--just-run$}) {
991 $do_nail = $do_cargo_lock = $do_lock = 0;
992 } elsif (m{^--(clean|keep)-linkfarm$}) {
993 $oot_clean = $1 eq 'clean';
994 } elsif (m{^--(no)?-nail$}) {
996 } elsif (m{^--(no)?-cargo-lock-manip$}) {
997 $do_cargo_lock = !$1;
998 } elsif (m{^--(no)?-concurrency-lock$}) {
1000 } elsif (m{^--leave-nailed$}) {
1002 } elsif (s{^--subcommand-props=}{}) {
1003 my @props = split /\,/, $_;
1004 our %subcmd_prop_ok;
1005 if (!%subcmd_prop_ok) {
1006 foreach my $v (\@subcmd_xprops, values %subcmd_props) {
1007 $subcmd_prop_ok{$_}=1 foreach @$v;
1011 or die "$self: unknown subcommand property \`$_'\n"
1013 $cargo_subcmd = \@props;
1014 } elsif (m{^--(no-)?cargo-lock-update}) {
1015 $cargo_lock_update= !!$1;
1017 $not_a_nailing_opt->();
1025 @args_preface = shift @ARGV;
1026 while (defined($_ = shift @ARGV)) {
1027 if (!m{^-}) { unshift @ARGV, $_; last; }
1028 if ($_ eq '--') { last; }
1029 push @args_preface, $_;
1031 @ARGV || die "$self: need cargo subcommand\n";
1032 $cargo_subcmd //= $ARGV[0];
1033 $pass_options //= 1;
1035 $cargo_subcmd //= '';
1036 $pass_options //= 0;
1038 push @args_preface, shift @ARGV;
1040 if (!ref($cargo_subcmd)) {
1041 print STDERR " cargo_subcmd lookup $cargo_subcmd\n" if $dump;
1042 $cargo_subcmd = $subcmd_props{$cargo_subcmd} // [ ];
1045 print STDERR " cargo_subcmd props @$cargo_subcmd\n" if $dump;
1047 $cargo_subcmd{$_} = 1 foreach @$cargo_subcmd;
1048 $cargo_subcmd = \%cargo_subcmd;
1055 consider_alt_cargo_lock();
1060 consider_directories();
1061 our @display_cmd = @ARGV;
1062 oot_massage_cmdline();
1068 print STDERR Dumper(\%manifests) if $dump>=2;
1069 print STDERR Dumper(\%packagemap, \@ARGV,
1070 { src_absdir => $src_absdir,
1071 worksphere => $worksphere,
1073 oot_dir => $oot_dir,
1074 oot_absdir => $oot_absdir,
1075 build_absdir => $build_absdir });
1081 $want_uninstall = !$leave_nailed;
1085 printf STDERR "$self: nailed (%s manifests, %s packages)%s\n",
1086 (scalar keys %manifests), (scalar keys %packagemap),
1087 (defined($alt_cargo_lock) and ", using `$alt_cargo_lock'")
1088 if $verbose && $do_nail;
1090 print STDERR "$self: invoking: @display_cmd\n" if $verbose;
1091 my $estatus = invoke();
1093 cargo_lock_update_after();
1095 uninstall() unless $leave_nailed;
1096 $want_uninstall = 0;
1098 print STDERR "$self: ".($do_nail ? "unnailed" : "finished")
1099 .". status $estatus.\n" if $verbose;