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',
52 WASM => 'wasm32-unknown-unknown',
56 $self = $0; $self =~ s{^.*/(?=.)}{};
59 while ($deref =~ m{^/}) {
60 my $link = readlink $deref;
63 or die "$self: checking our script location $deref: $!\n";
64 $deref =~ s{/[^/]+$}{}
65 or die "$self: unexpected script path: $deref\n";
67 unshift @INC, $deref."/TOML-Tiny/lib";
70 last if $link !~ m{^/};
75 use Fcntl qw(LOCK_EX);
77 use TOML::Tiny::Faithful;
79 our $src_absdir = getcwd() // die "$self: getcwd failed: $!\n";
81 our $worksphere = $src_absdir;
82 $worksphere =~ s{/([^/]+)$}{}
83 or die "$self: cwd \`$worksphere' unsupported!\n";
84 our $subdir = $1; # leafname
86 our $lockfile = "../.nailing-cargo.lock";
90 our $command_is_cargo;
92 our $cargo_lock_update;
100 our $do_cargo_lock=1;
105 our %subcmd_props = (
106 # build (default) =>[qw( )],
107 'generate-lockfile'=>[qw( lock-update !target !target-dir )],
108 update =>[qw( lock-update !target online )],
109 fetch =>[qw( online !target-dir )],
110 fmt =>[qw( !locked !target !offline !target-dir edits )],
111 miri =>[qw( !locked !offline linkfarm-shallow )],
112 upgrades =>[qw( !locked !target-dir )],
115 our @subcmd_xprops = qw(!manifest-path);
123 print $usage or die $!;
128 my $manual = ($base_path // '.').'/README.md';
129 stat $manual or die "$self: manual not found at $manual: $!\n";;
130 exec 'sh','-ec', 'pandoc -- "$1" 2>&1 | w3m -T text/html', '--', $manual;
131 die "$self: exec sh failed: $!";
134 sub read_or_enoent ($) {
136 if (!open R, '<', $fn) {
137 return undef if $!==ENOENT;
138 die "$self: open $fn: $!\n";
141 my ($r) = <R> // die "$self: read $fn: $!\n";
145 sub stat_exists ($$) {
146 my ($fn, $what) = @_;
147 if (stat $fn) { return 1; }
148 $!==ENOENT or die "$self: stat $what: $fn: $!\n";
153 print STDERR " subcmd_p ".(join ' ', keys %$cargo_subcmd)." | @_\n"
155 $cargo_subcmd->{$_[0]}
158 sub toml_or_enoent ($$) {
160 my $toml = read_or_enoent($f) // return;
161 print STDERR "Read TOML from $f\n" if $dump;
162 my ($v,$e) = from_toml($toml);
165 die "$self: parse TOML: $what: $f: $e\n";
167 die "$e ?" if length $e;
171 sub load1config ($) {
173 my $toml = toml_or_enoent($f, "config file");
174 push @configs, $toml if defined $toml;
178 my $dotfile = ".nailing-cargo.toml";
179 load1config("../Nailing-Cargo.toml");
180 load1config($dotfile);
181 load1config("$ENV{HOME}/$dotfile") if defined $ENV{HOME};
182 load1config("/etc/nailing-cargo/cfg.toml");
185 sub unlink_or_enoent ($) { unlink $_[0] or $!==ENOENT; }
189 "@$x[0..5]" eq "@$y[0..5]";
193 return unless $do_lock;
196 open LOCK, ">", $lockfile or die "$self: open/create $lockfile: $!\n";
197 flock LOCK, LOCK_EX or die "$self: lock $lockfile: $!\n";
198 my @fstat = stat LOCK or die "$self: fstat: $!\n";
199 my @stat = stat $lockfile;
201 next if $! == ENOENT;
202 die "$self: stat $lockfile: $!\n";
204 last if same_file(\@fstat,\@stat);
208 unlink $lockfile or die "$self: removing lockfile: $!\n";
216 die "$self: config key \`@_': $m\n";
220 foreach my $cfg (@configs) {
223 last unless defined $v;
224 ref($v) eq 'HASH' or badcfg @_, "parent key \`$k' is not a hash";
227 return $v if defined $v;
235 my $got = ref($v) || 'scalar';
236 return $v if !defined($v) || $got eq $exp;
237 badcfg @_, "found \L$got\E, expected \L$exp\E";
238 # ^ toml doesn't make refs to scalars, so this is unambiguous
243 (cfge $exp, @_) // badcfg @_, "missing";
246 sub cfgs { cfge 'scalar', @_ }
247 sub cfgsn { cfgn 'scalar', @_ }
251 return $v if !defined($v) || Types::Serialiser::is_bool $v;
252 badcfg @_, "expected boolean";
256 my $l = cfge 'ARRAY', @_;
257 foreach my $x (@$l) {
258 !ref $x or badcfg @_, "list contains non-scalar element";
264 my $nailfile = "../Cargo.nail";
265 open N, '<', $nailfile or die "$self: open $nailfile: $!\n";
267 my $toml = <N> // die "$self: read $nailfile: $!";
269 if ($toml !~ m{^\s*\[/}m &&
270 $toml !~ m{^[^\n\#]*\=}m &&
271 # old non-toml syntax
272 $toml =~ s{^[ \t]*([-_0-9a-z]+)[ \t]+(\S+)[ \t]*$}{$1 = \"$2\"}mig) {
273 $toml =~ s{^}{[packages\]\n};
275 $toml =~ s{^[ \t]*\-[ \t]*\=[ \t]*(\"[-_0-9a-z]+\"\n?)$}{
278 $toml = "subdirs = [\n".(join '', map { "$_\n" } @sd)."]\n".$toml;
282 ($nail,$e) = from_toml($toml);
283 if (!defined $nail) {
286 print STDERR "$self: $nailfile transformed into TOML:\n$toml\n";
289 die "$self: parse $nailfile: $e\n";
291 die "$e ?" if length $e;
293 $nail->{subdirs} //= [ ];
295 if (!ref $nail->{subdirs}) {
298 map { s/^\s+//; s/\s+$//; $_; }
304 unshift @configs, $nail;
307 sub get_dependency_tables ($) {
309 my @keys = qw(dependencies build-dependencies dev-dependencies);
313 foreach my $k (@keys) {
314 my $deps = $node->{$k};
315 push @r, $deps if $deps;
319 foreach my $target_node (values %{ $toml->{target} // { } }) {
320 $process->($target_node);
325 our @alt_cargo_lock_stat;
327 sub consider_alt_cargo_lock () {
328 my @ck = qw(alt_cargo_lock);
329 # User should *either* have Cargo.lock in .gitignore,
330 # or expect to commit Cargo.lock.example ($alt_cargo_lock)
332 return unless $do_cargo_lock;
334 $alt_cargo_lock = (cfg_uc @ck);
337 if (defined($alt_cargo_lock) && ref($alt_cargo_lock) eq 'HASH') {
338 $force = cfg_bool qw(alt_cargo_lock force);
339 my @ck = qw(alt_cargo_lock file);
340 $alt_cargo_lock = cfg_uc @ck;
342 $alt_cargo_lock //= Types::Serialiser::true;
344 if (Types::Serialiser::is_bool $alt_cargo_lock) {
345 if (!$alt_cargo_lock) { $alt_cargo_lock = undef; return; }
346 $alt_cargo_lock = 'Cargo.lock.example';
349 if (ref($alt_cargo_lock) || $alt_cargo_lock =~ m{/}) {
350 badcfg @ck, "expected boolean, or leafname";
353 if (!stat_exists $alt_cargo_lock, "alt_cargo_lock") {
354 $alt_cargo_lock = undef unless $force;
358 @alt_cargo_lock_stat = stat _;
361 our $oot_dir; # oot.dir or "Build"
364 sub consider_oot () {
365 $oot_dir = cfgs qw(oot dir);
366 my $use = cfgs qw(oot use);
367 unless (defined($oot_dir) || defined($use) ||
368 defined(cfg_uc qw(oot user))) {
371 if (($use//'') eq 'disable') {
375 $oot_clean //= cfg_bool qw(oot clean);
376 $oot_dir //= 'Build';
377 $oot_absdir = ($oot_dir !~ m{^/} ? "$worksphere/" : ""). $oot_dir;
385 sub read_manifest ($$$) {
386 my ($subdir, $org_subdir, $why) = @_;
387 my $manifest = "../$subdir/Cargo.toml";
388 print STDERR "$self: reading $manifest...\n" if $verbose>=4;
389 if (defined $manifests{$manifest}) {
391 "$self: warning: $subdir: specified more than once!".
392 " (ignoring $why)\n";
395 foreach my $try ("$manifest.unnailed", "$manifest") {
396 my $toml = toml_or_enoent($try, "manifest, in $why") // next;
397 my $ws = $toml->{workspace};
399 queue_workspace_members($subdir, $org_subdir, $ws, "$subdir, $why");
401 my $p = $toml->{package}{name};
402 if (!defined $p and !defined $ws) {
404 "$self: warning: $subdir, $why: missing package.name in $try, ignoring\n";
407 $manifests{$manifest} = [ $toml, $org_subdir ] if $p;
408 foreach my $dep (get_dependency_tables $toml) {
409 next unless defined $dep->{path};
410 queue_referenced_path($dep->{path}, $org_subdir,
411 "dependency of $subdir, $why");
418 sub queue_workspace_members ($$) {
419 my ($subdir, $org_subdir, $ws_toml, $what) = @_;
420 # We need to (more or less) reimplement the cargo workspace
421 # membership algorithm (see the "workspaces" section of the cargo
422 # reference). How tiresome.
424 # It's not quite the same for us because we aren't interested in
425 # whether cargo thinks things are "in the workspace". But we do
426 # need to do the automatic discover.
428 my @include = @{ $ws_toml->{members} // [ ] };
429 my $exclude = $ws_toml->{exclude} // [ ];
432 s/[^*?0-9a-zA-Z_]/\\$&/g;
437 foreach my $spec (@include) {
438 if ($spec =~ m{^/}) {
440 "$self: warning: absolute workspace member $spec in $what (not nailing, but cargo will probably use it)\n";
443 my $spec_glob = "../$subdir/$spec";
444 my $globflags = GLOB_ERR|GLOB_BRACE|GLOB_NOMAGIC;
445 foreach my $globent (bsd_glob($spec_glob, $globflags)) {
446 next if grep { $globent =~ m{^$_$} } @exclude;
447 queue_referenced_path($globent, $org_subdir,
448 "member of workspace $what");
453 sub queue_referenced_path ($$$) {
454 my ($spec_path, $org_subdir, $why) = @_;
456 qw(realpath), "--relative-to=../$org_subdir", "--", $spec_path
457 or die "$self: fork/pipe/exec for realpath(1)\n";
458 my $rel_path = do { local $/=undef; <REALPATH>; };
460 my $r = close(REALPATH);
461 die "$self: reap realpath: $!\n" if $!;
462 if (!chomp($rel_path) or $?) {
464 "$self: warning: failed to determine realpath for $spec_path in $org_subdir (exit code $?)\n";
467 if ($rel_path =~ m{^\.\./} or $rel_path eq '..') {
469 "$self: warning: $spec_path ($why) points outside $org_subdir, not following so not nailing (although cargo probably will follow it)\n";
473 my $q_subdir = "$org_subdir/$rel_path";
474 print STDERR "$self: making a note to look at $q_subdir, $why)\n"
477 push @queued_paths, [ "$q_subdir", $org_subdir, $why ];
481 # We (and our callees) populate %packagemap and %manifest, so if we
482 # don't run, they remain empty and nothing is nailed.
483 return unless $do_nail;
485 foreach my $p (keys %{ $nail->{packages} }) {
486 my $v = $nail->{packages}{$p};
487 my $subdir = ref($v) ? $v->{subdir} : $v;
488 my ($gotpackage, $ws) = read_manifest($subdir, $subdir, "from [packages]");
489 $gotpackage //= '<nothing!>';
490 if ($gotpackage ne $p) {
492 "$self: warning: honouring Cargo.nail packages.$subdir=$p even though $subdir contains package $gotpackage!\n";
494 die if defined $packagemap{$p};
495 $packagemap{$p} = [ $subdir, $subdir ];
497 foreach my $subdir (@{ $nail->{subdirs} }) {
498 my ($gotpackage,$ws) = read_manifest($subdir, $subdir, "from [subdirs]");
499 if (!defined $gotpackage) {
501 "$self: warning: ignoring subdir $subdir which has no (suitable) Cargo.toml\n"
505 $packagemap{$gotpackage} //= [ $subdir, $subdir ];
507 while (my ($subdir, $org_subdir, $why) = @{ shift @queued_paths or [] }) {
508 next if $manifests{"../$subdir/Cargo.toml"};
509 my ($gotpackage, $ws) = read_manifest($subdir, $org_subdir, $why);
510 next unless $gotpackage;
511 $packagemap{$gotpackage} //= [ $subdir, $org_subdir ];
516 foreach my $p (sort keys %packagemap) {
517 print STDERR "$self: package $p in $packagemap{$p}[0]\n" if $verbose>=2;
519 foreach my $mf (keys %manifests) {
520 die "internal error" unless $do_nail; # belt and braces
522 my ($toml, $mf_org_subdir) = @{ $manifests{$mf} };
523 foreach my $deps (get_dependency_tables $toml) {
525 foreach my $p (keys %packagemap) {
526 my $info = $deps->{$p};
527 next unless defined $info;
528 next if $packagemap{$p}[1] eq $mf_org_subdir;
529 $deps->{$p} = $info = { } unless ref $info;
530 my $oldpath = $info->{path};
531 delete $info->{version};
532 my $newpath = $worksphere.'/'.$packagemap{$p}[0];
533 print STDERR "in $mf set $p path=$newpath (was ".
534 ($oldpath // '<unset>').")\n"
536 $info->{path} = $newpath;
538 delete $info->{branch};
541 my $nailing = "$mf.nailing~";
542 unlink_or_enoent $nailing or die "$self: remove old $nailing: $!\n";
543 open N, '>', $nailing or die "$self: create new $nailing: $!\n";
544 print N to_toml($toml) or die "$self: write new $nailing: $!\n";
545 close N or die "$self: close new $nailing: $!\n";
550 if ($just_linkfarm) {
551 die "$self: --just-linkfarm but not doing out-of-tree builds!\n"
552 unless defined $oot_dir;
557 if (!defined $online) {
558 $_ = cfg_uc qw(misc online);
560 } elsif (Types::Serialiser::is_bool $_) {
565 } elsif (m/^[1ty]/) { # allow booleanish strings
566 $online = 1; # for less user frustration
567 } elsif (m/^[0fn]/) {
570 badcfg qw(misc online), "expected boolean or 'auto', found '$_'";
573 $online //= 1 if subcmd_p('online');
576 $cargo_lock_update //= subcmd_p('lock-update');
578 subcmd_p('linkfarm-shallow') ? 'shallow' :
579 $cargo_lock_update ? 'shallow' :
582 $oot_preclean //= 'no';
586 if (!$cargo_lock_update) {
587 push @add, qw(--locked) unless subcmd_p('!locked');
589 if ($linkfarm_depth eq '') {
590 if (defined($oot_dir) && !subcmd_p('!manifest-path')) {
591 my $cargotoml = "${src_absdir}/Cargo.toml";
592 push @args_preface, "--manifest-path=$cargotoml" if $pass_options;
593 push @add, qw(--target-dir=target) unless subcmd_p('!target-dir');
597 if (defined($target) && !subcmd_p('!target')) {
598 if ($target =~ m{^[A-Z]}) {
599 $target = (cfgs 'arch', $target) // $archmap{$target}
600 // die "$self: --target=$target alias specified; not in cfg or map\n";
602 push @add, "--target=$target";
605 push @add, "--offline" unless $online || subcmd_p('!offline');
607 if (subcmd_p('edits') && $linkfarm_depth ne 'copy-edit') {
609 "$self: *WARNING*: this subcommand expects to edit the source code; you probably want to specify --edits-sources aka -E (which is not the default even now, for safety reasons)\n";
612 push @args_preface, @add if $pass_options;
613 die if grep { m/ / } @add;
614 $ENV{NAILINGCARGO_CARGO_OPTIONS} = "@add";
616 unshift @ARGV, @args_preface;
619 our $build_absdir; # .../Build/<subdir>
621 sub oot_massage_cmdline () {
622 return unless defined $oot_dir;
624 my $use = cfgs qw(oot use);
625 $use // die "$self: out-of-tree build, but \`oot.use' not configured\n";
626 $build_absdir = "$oot_absdir/$subdir";
628 my ($pre,$post) = ('','');
630 if ($linkfarm_depth eq '') {
631 push @xargs, $build_absdir;
632 ($pre, $post) = ('cd "$1"; shift; ', '');
634 push @xargs, $oot_absdir, $subdir, $src_absdir;
636 bld="$1"; shift; sd="$1"; shift; src="$1"; shift;
637 cd "$bld"; mkdir -p -- "$sd"; cd "$sd";
639 if ($oot_preclean ne 'no') {
640 $pre.= "find . -maxdepth 1 ! -path .";
641 $pre.= " ! -path ./target" if $oot_preclean ne 'full';
642 $pre.= " -print0 | xargs -0r echo rm -r --;"
644 if ($linkfarm_depth eq 'shallow') {
645 $pre.= <<'END_SHALLOW';
646 clean () { find -lname "$src/*" -print0 | xargs -0r rm --; }; clean;
647 find "$src" -maxdepth 1 \! -name Cargo.lock -print0 |
648 xargs -0r sh -ec 'for f in "$@"; do
653 } elsif ($linkfarm_depth =~ /full|git/) {
654 $pre .= <<'END_EITHER_DEEP_DIRS';
655 clean () { find -follow -lname "$src/*" -print0 | xargs -0r rm --; };
656 (set -e; cd "$src"; find . \! -name Cargo.lock \! \( -name .git -prune \) \! -path . \! -name .git -type d -print0) |
657 xargs -0r sh -ec 'for f in "$@"; do
658 rm -f "$f" 2>/dev/null ||:;
662 if ($linkfarm_depth eq 'git') {
663 $pre .= <<'END_FILES_GIT'
664 (set -e; cd "$src"; git ls-files --exclude-standard -co -z) |
666 } elsif ($linkfarm_depth eq 'full') {
667 $pre .= <<'END_FILES_FULL'
668 (set -e; cd "$src"; find . \! -name Cargo.lock \! \( -name .git -prune \) \! -type d -print0) |
671 $pre .= <<'END_DEEP';
673 BEGIN { $src=shift @ARGV; }
674 next if (readlink "$_"//"") eq "$src/$_";
676 symlink "$src/$_", "$_" or die "$_ $!";
679 } elsif ($linkfarm_depth eq 'copy-edit') {
680 $pre .= <<'END_COPY_EDIT';
681 find -lname "$src/*" -print0 | xargs -0r rm --;
682 (set -e; cd "$src"; git ls-files -c -z |
683 cpio --quiet -p0m --no-preserve-owner -u --make-directories "$bld/$sd");
685 (set -e; cd "$src"; git ls-files -c -z) | xargs -0r rm -f --;
688 $post .= <<'END_COPY_EDIT_BUNDLE';
689 (set -e; cd "$src"; git ls-files -c -z) |
690 cpio -Hustar -o0 --quiet >"nailing-cargo-update.tar";
693 die "$linkfarm_depth ?";
695 $pre .= <<'ENDLK' if $do_cargo_lock;
696 if test -e Cargo.lock; then
698 cp -- "$src"/Cargo.lock .;
701 $post .= <<'ENDCLEAN' if $oot_clean && !$just_linkfarm;
705 my $addpath = (cfg_uc qw(oot path_add)) //
706 $use eq 'really' ? Types::Serialiser::true : Types::Serialiser::false;
708 !Types::Serialiser::is_bool $addpath ? $addpath :
709 $addpath ? '$HOME/.cargo/bin' :
711 if (defined $addpath) {
713 PATH=$addpath:\${PATH-/usr/local/bin:/bin:/usr/bin};
717 $pre =~ s/^\s+//mg; $pre =~ s/\s+/ /g;
718 $post =~ s/^\s+//mg; $post =~ s/\s+/ /g;
720 my $getuser = sub { cfgsn qw(oot user) };
722 my $xe = $verbose >= 2 ? 'xe' : 'e';
725 @command = (@_, 'sh',"-${xe}c",$pre.'exec "$@"','--',@xargs);
727 @command = (@_, 'sh',"-${xe}c",$pre.'"$@"; '.$post,'--',@xargs);
729 push @command, @ARGV;
731 my $command_sh = sub {
732 my $quoted = join ' ', map {
737 @command = @_, "set -${xe}; $pre $quoted; $post";
739 print STDERR "$self: out-of-tree, building in: \`$build_absdir'\n"
741 if ($use eq 'really') {
742 my $user = $getuser->();
743 my @pw = getpwnam $user or die "$self: oot.user \`$user' lookup failed\n";
744 my $homedir = $pw[7];
745 $sh_ec->('really','-u',$user,'env',"HOME=$homedir");
746 print STDERR "$self: using really to run as user \`$user'\n" if $verbose;
747 } elsif ($use eq 'ssh') {
748 my $user = $getuser->();
749 $user .= '@localhost' unless $user =~ m/\@/;
750 $command_sh->('ssh',$user);
751 print STDERR "$self: using ssh to run as \`$user'\n" if $verbose;
752 } elsif ($use eq 'command_args') {
753 my @c = cfgn_list qw(oot command);
755 print STDERR "$self: out-of-tree, adverbial command: @c\n" if $verbose;
756 } elsif ($use eq 'command_sh') {
757 my @c = cfgn_list qw(oot command);
759 print STDERR "$self: out-of-tree, ssh'ish command: @c\n" if $verbose;
760 } elsif ($use eq 'null') {
763 die "$self: oot.use mode $use not recognised\n";
770 $ENV{CARGO_MANIFEST_DIR} = $src_absdir unless $linkfarm_depth;
771 $ENV{NAILINGCARGO_MANIFEST_DIR} = $src_absdir;
772 $ENV{NAILINGCARGO_WORKSPHERE} = $worksphere;
773 $ENV{NAILINGCARGO_BUILDSPHERE} = $oot_absdir;
774 delete $ENV{NAILINGCARGO_BUILDSPHERE} unless $oot_absdir;
775 $ENV{NAILINGCARGO_BUILD_DIR} = $build_absdir // $src_absdir;
781 if ($want_uninstall) {
783 foreach my $mf (keys %manifests) {
784 eval { uninstall1($mf,1); 1; } or warn "$@";
786 eval { unaltcargolock(1); 1; } or warn "$@";
790 sub consider_directories () {
791 return unless defined $oot_dir;
792 my $bsubdir = "../$oot_dir/$subdir";
793 return if stat $bsubdir;
794 die "$0: build directory $bsubdir inaccessible\n"
796 return if $cargo_lock_update; # will make it
797 die "$0: build directory $bsubdir does not exist, and not in Cargo.lock update mode!\n";
800 our $cleanup_cargo_lock;
802 foreach my $mf (keys %manifests) {
803 link "$mf", "$mf.unnailed" or $!==EEXIST
804 or die "$self: make backup link $mf.unnailed: $!\n";
807 if (defined($alt_cargo_lock)) {
808 die 'internal error' unless $do_cargo_lock;
809 if (@alt_cargo_lock_stat) {
810 print STDERR "$self: using alt_cargo_lock `$alt_cargo_lock'..."
812 if (link $alt_cargo_lock, 'Cargo.lock') {
813 print STDERR " linked\n" if $verbose>=3;
814 } elsif ($! != EEXIST) {
815 print STDERR "\n" if $verbose>=3;
816 die "$self: make \`Cargo.lock' available as \`$alt_cargo_lock': $!\n";
818 print STDERR "checking quality." if $verbose>=3;
819 my @lock_stat = stat 'Cargo.lock'
820 or die "$self: stat Cargo.lock (for alt check: $!\n";
821 same_file(\@alt_cargo_lock_stat, \@lock_stat)
823 "$self: \`Cargo.lock' and alt file \`$alt_cargo_lock' both exist and are not the same file!\n";
825 $cleanup_cargo_lock = 1;
827 $cleanup_cargo_lock = 1;
828 # If Cargo.lock exists and alt doesn't, that means either
829 # that a previous run was interrupted, or that the user has
837 my $nailed = "$mf.nailed~"; $nailed =~ s{/([^/]+)$}{/.$1} or die;
842 my @our_unfound_stab = stat_exists('Cargo.toml', 'local Cargo.toml')
844 foreach my $mf (keys %manifests) {
845 if (@our_unfound_stab) {
846 if (stat_exists $mf, "manifest in to-be-nailed directory") {
847 my @mf_stab = stat _ ;
848 if ("@mf_stab[0..1]" eq "@our_unfound_stab[0..1]") {
849 @our_unfound_stab = ();
854 my $nailing = "$mf.nailing~";
855 my $nailed = nailed($mf);
858 if (open NN, '<', $nailed) {
859 $diff = compare($nailing, \*NN);
860 die "$self: compare $nailing and $nailed: $!" if $diff<0;
862 $!==ENOENT or die "$self: check previous $nailed: $!\n";
872 rename $use, $mf or die "$self: install nailed $use: $!\n";
873 unlink_or_enoent $rm or die "$self: remove old $rm: $!\n";
874 print STDERR "$self: nailed $mf\n" if $verbose>=3;
877 if (@our_unfound_stab && $do_nail) {
879 "$self: *WARNING* cwd is not in Cargo.nail thbough it has Cargo.toml!\n";
884 my $r = system @ARGV;
888 print STDERR "$self: could not execute $ARGV[0]: $!\n";
890 } elsif ($r & 0xff00) {
891 print STDERR "$self: $ARGV[0] failed (exit status $r)\n";
894 print STDERR "$self: $ARGV[0] died due to signal! (wait status $r)\n";
899 sub files_return_after_update () {
900 if ($linkfarm_depth eq 'copy-edit') {
901 system qw(sh -ec), <<'END', 'x', "$build_absdir";
902 git ls-files -c -z | \
903 tar -x --keep-newer-files --no-same-permissions --no-same-owner \
904 --no-acls --no-selinux --no-xattrs --warning=no-ignore-newer \
905 -Hustar --null --files-from=- --force-local \
906 -f "$1/nailing-cargo-update.tar"
908 } elsif ($do_cargo_lock && $cargo_lock_update && !$just_linkfarm) {
909 # avoids importing File::Copy and the error handling is about as good
911 my $r= system qw(cp --), "$build_absdir/Cargo.lock", "Cargo.lock";
912 die "$self: run cp: $! $?" if $r<0 || $r & 0xff;
913 die "$self: failed to update local Cargo.lock (wait status $r)\n" if $r;
917 sub uninstall1 ($$) {
918 my ($mf, $enoentok) = @_;
919 my $unnailed = "$mf.unnailed";
920 rename $unnailed, $mf or ($enoentok && $!==ENOENT)
921 or die "$self: failed to restore: rename $unnailed back to $mf: $!\n";
924 sub unaltcargolock ($) {
926 return unless $cleanup_cargo_lock;
927 die 'internal error!' unless $do_cargo_lock && defined $alt_cargo_lock;
929 # we ignore $enoentok because we don't know if one was supposed to
932 rename('Cargo.lock', $alt_cargo_lock) or $!==ENOENT or die
933 "$self: cleanup: rename possibly-updated \`Cargo.lock' to \`$alt_cargo_lock': $!\n";
935 unlink 'Cargo.lock' or $!==ENOENT or die
936 "$self: cleanup: remove \`Cargo.lock' in favour of \`$alt_cargo_lock': $!\n";
937 # ^ this also helps clean up the stupid rename() corner case
941 foreach my $mf (keys %manifests) {
942 my $nailed = nailed($mf);
943 link $mf, $nailed or die "$self: preserve (link) $mf as $nailed: $!\n";
952 # Loop exit condition:
955 # $is_cargo==1 <cargo-command> <cargo-opts> [--] <subcmd>...
956 # $is_cargo==0 <build-command>...
960 die "$self: need cargo subcommand\n"
961 unless $noact || $just_linkfarm;;
962 push @ARGV, "CARGO-SUBCOMMAND"; # dummy, user may see it
968 my $not_a_nailing_opt = sub { # usage 1
969 unshift @ARGV, $orgopt;
970 unshift @ARGV, 'cargo';
972 no warnings qw(exiting);
975 $not_a_nailing_opt->() unless m{^-};
976 $not_a_nailing_opt->() if $_ eq '--';
978 if ($_ eq '---') { # usage 2 or 3
980 die "$self: --- must be followed by build command\n" unless $noact;
981 push @ARGV, 'BUILD-COMMAND';
983 if ($ARGV[0] eq '--') { # usage 3
986 } elsif (grep { $_ eq '--' } @ARGV) { # usage 2
988 } elsif ($ARGV[0] =~ m{[^/]*cargo[^/]*$}) { # usage 2
999 } elsif (s{^-v}{-}) {
1001 } elsif (s{^-q}{-}) {
1003 } elsif (s{^-n}{-}) {
1005 } elsif (s{^-s(.+)}{-}s) {
1007 } elsif (s{^-([uU])}{-}) {
1008 $cargo_lock_update = $1=~m/[a-z]/;
1009 } elsif (s{^-([cC])}{-}) {
1010 $pass_options = $1=~m/[a-z]/;
1011 } elsif (s{^-D}{-}) {
1013 } elsif (s{^-E}{-}) {
1014 $linkfarm_depth = 'copy-edit';
1015 } elsif (s{^-T(.+)}{-}s) {
1017 } elsif (s{^-([oO])}{-}) {
1018 $online = $1=~m/[a-z]/;
1020 die "$self: unknown short option(s) $_\n" unless $_ eq $orgopt;
1021 $not_a_nailing_opt->();
1024 } elsif (s{^--help$}{}) {
1026 } elsif (s{^--(?:doc|man|manual)?$}{}) {
1028 } elsif (s{^--target=}{}) {
1030 } elsif (m{^--(on|off)line$}) {
1031 $online = $1 eq 'on';
1032 } elsif (m{^--just-linkfarm(?:=(shallow|git|full))?$}) {
1034 $linkfarm_depth = $1 if $1;
1035 $cargo_lock_update= 1; # will set $linkfarm_detph to 1 by default
1036 } elsif (m{^--linkfarm(?:=(no|shallow|git|full))?$}) {
1037 $linkfarm_depth = $1 || 'git';
1038 } elsif (m{^--edits?-sources?$}) {
1039 $linkfarm_depth = 'copy-edit';
1040 } elsif (m{^--just-run$}) {
1041 $do_nail = $do_cargo_lock = $do_lock = 0;
1042 } elsif (m{^--(clean|keep)-linkfarm$}) {
1043 $oot_clean = $1 eq 'clean';
1044 } elsif (m{^--(no-)?preclean-linkfarm$}) {
1045 $oot_preclean = $1 ? 'no' : 'src';
1046 } elsif (m{^--preclean-linkfarm=(no|src|full)$}) {
1048 } elsif (m{^--(no-)?nail$}) {
1050 } elsif (m{^--(no-)?cargo-lock-manip$}) {
1051 $do_cargo_lock = !$1;
1052 } elsif (m{^--(no-)?concurrency-lock$}) {
1054 } elsif (m{^--leave-nailed$}) {
1056 } elsif (s{^--subcommand-props=}{}) {
1057 my @props = split /\,/, $_;
1058 our %subcmd_prop_ok;
1059 if (!%subcmd_prop_ok) {
1060 foreach my $v (\@subcmd_xprops, values %subcmd_props) {
1061 $subcmd_prop_ok{$_}=1 foreach @$v;
1065 or die "$self: unknown subcommand property \`$_'\n"
1067 $cargo_subcmd = \@props;
1068 } elsif (m{^--(no-)?cargo-lock-update}) {
1069 $cargo_lock_update= !!$1;
1071 $not_a_nailing_opt->();
1079 @args_preface = shift @ARGV;
1080 while (defined($_ = shift @ARGV)) {
1081 if (!m{^-|^\+}) { unshift @ARGV, $_; last; }
1082 if ($_ eq '--') { last; }
1083 push @args_preface, $_;
1085 @ARGV || die "$self: need cargo subcommand\n";
1086 $cargo_subcmd //= $ARGV[0];
1087 $pass_options //= 1;
1089 $cargo_subcmd //= '';
1090 $pass_options //= 0;
1092 push @args_preface, shift @ARGV;
1094 if (!ref($cargo_subcmd)) {
1095 print STDERR " cargo_subcmd lookup $cargo_subcmd\n" if $dump;
1096 $cargo_subcmd = $subcmd_props{$cargo_subcmd} // [ ];
1099 print STDERR " cargo_subcmd props @$cargo_subcmd\n" if $dump;
1101 $cargo_subcmd{$_} = 1 foreach @$cargo_subcmd;
1102 $cargo_subcmd = \%cargo_subcmd;
1109 consider_alt_cargo_lock();
1114 consider_directories();
1115 our @display_cmd = @ARGV;
1116 oot_massage_cmdline();
1122 print STDERR Dumper(\%manifests) if $dump>=2;
1123 print STDERR Dumper(\%packagemap, \@ARGV,
1124 { src_absdir => $src_absdir,
1125 worksphere => $worksphere,
1127 oot_dir => $oot_dir,
1128 oot_absdir => $oot_absdir,
1129 build_absdir => $build_absdir });
1135 $want_uninstall = !$leave_nailed;
1139 printf STDERR "$self: nailed (%s manifests, %s packages)%s\n",
1140 (scalar keys %manifests), (scalar keys %packagemap),
1141 (defined($alt_cargo_lock) and ", using `$alt_cargo_lock'")
1142 if $verbose && $do_nail;
1144 print STDERR "$self: invoking: @display_cmd\n" if $verbose;
1145 my $estatus = invoke();
1147 files_return_after_update();
1149 uninstall() unless $leave_nailed;
1150 $want_uninstall = 0;
1152 print STDERR "$self: ".($do_nail ? "unnailed" : "finished")
1153 .". status $estatus.\n" if $verbose;