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;
104 our %subcmd_props = (
105 # build (default) =>[qw( )],
106 'generate-lockfile'=>[qw( lock-update !target !target-dir )],
107 update =>[qw( lock-update !target online )],
108 fetch =>[qw( online !target-dir )],
109 fmt =>[qw( !locked !target !offline !target-dir edits )],
110 miri =>[qw( !locked !offline linkfarm-shallow )],
111 upgrades =>[qw( !locked !target-dir )],
114 our @subcmd_xprops = qw(!manifest-path);
122 print $usage or die $!;
127 my $manual = ($base_path // '.').'/README.md';
128 stat $manual or die "$self: manual not found at $manual: $!\n";;
129 exec 'sh','-ec', 'pandoc -- "$1" 2>&1 | w3m -T text/html', '--', $manual;
130 die "$self: exec sh failed: $!";
133 sub read_or_enoent ($) {
135 if (!open R, '<', $fn) {
136 return undef if $!==ENOENT;
137 die "$self: open $fn: $!\n";
140 my ($r) = <R> // die "$self: read $fn: $!\n";
144 sub stat_exists ($$) {
145 my ($fn, $what) = @_;
146 if (stat $fn) { return 1; }
147 $!==ENOENT or die "$self: stat $what: $fn: $!\n";
152 print STDERR " subcmd_p ".(join ' ', keys %$cargo_subcmd)." | @_\n"
154 $cargo_subcmd->{$_[0]}
157 sub toml_or_enoent ($$) {
159 my $toml = read_or_enoent($f) // return;
160 print STDERR "Read TOML from $f\n" if $dump;
161 my ($v,$e) = from_toml($toml);
164 die "$self: parse TOML: $what: $f: $e\n";
166 die "$e ?" if length $e;
170 sub load1config ($) {
172 my $toml = toml_or_enoent($f, "config file");
173 push @configs, $toml if defined $toml;
177 my $dotfile = ".nailing-cargo.toml";
178 load1config("../Nailing-Cargo.toml");
179 load1config($dotfile);
180 load1config("$ENV{HOME}/$dotfile") if defined $ENV{HOME};
181 load1config("/etc/nailing-cargo/cfg.toml");
184 sub unlink_or_enoent ($) { unlink $_[0] or $!==ENOENT; }
188 "@$x[0..5]" eq "@$y[0..5]";
192 return unless $do_lock;
195 open LOCK, ">", $lockfile or die "$self: open/create $lockfile: $!\n";
196 flock LOCK, LOCK_EX or die "$self: lock $lockfile: $!\n";
197 my @fstat = stat LOCK or die "$self: fstat: $!\n";
198 my @stat = stat $lockfile;
200 next if $! == ENOENT;
201 die "$self: stat $lockfile: $!\n";
203 last if same_file(\@fstat,\@stat);
207 unlink $lockfile or die "$self: removing lockfile: $!\n";
215 die "$self: config key \`@_': $m\n";
219 foreach my $cfg (@configs) {
222 last unless defined $v;
223 ref($v) eq 'HASH' or badcfg @_, "parent key \`$k' is not a hash";
226 return $v if defined $v;
234 my $got = ref($v) || 'scalar';
235 return $v if !defined($v) || $got eq $exp;
236 badcfg @_, "found \L$got\E, expected \L$exp\E";
237 # ^ toml doesn't make refs to scalars, so this is unambiguous
242 (cfge $exp, @_) // badcfg @_, "missing";
245 sub cfgs { cfge 'scalar', @_ }
246 sub cfgsn { cfgn 'scalar', @_ }
250 return $v if !defined($v) || Types::Serialiser::is_bool $v;
251 badcfg @_, "expected boolean";
255 my $l = cfge 'ARRAY', @_;
256 foreach my $x (@$l) {
257 !ref $x or badcfg @_, "list contains non-scalar element";
263 my $nailfile = "../Cargo.nail";
264 open N, '<', $nailfile or die "$self: open $nailfile: $!\n";
266 my $toml = <N> // die "$self: read $nailfile: $!";
268 if ($toml !~ m{^\s*\[/}m &&
269 $toml !~ m{^[^\n\#]*\=}m &&
270 # old non-toml syntax
271 $toml =~ s{^[ \t]*([-_0-9a-z]+)[ \t]+(\S+)[ \t]*$}{$1 = \"$2\"}mig) {
272 $toml =~ s{^}{[packages\]\n};
274 $toml =~ s{^[ \t]*\-[ \t]*\=[ \t]*(\"[-_0-9a-z]+\"\n?)$}{
277 $toml = "subdirs = [\n".(join '', map { "$_\n" } @sd)."]\n".$toml;
281 ($nail,$e) = from_toml($toml);
282 if (!defined $nail) {
285 print STDERR "$self: $nailfile transformed into TOML:\n$toml\n";
288 die "$self: parse $nailfile: $e\n";
290 die "$e ?" if length $e;
292 $nail->{subdirs} //= [ ];
294 if (!ref $nail->{subdirs}) {
297 map { s/^\s+//; s/\s+$//; $_; }
303 unshift @configs, $nail;
306 sub get_dependency_tables ($) {
308 my @keys = qw(dependencies build-dependencies dev-dependencies);
312 foreach my $k (@keys) {
313 my $deps = $node->{$k};
314 push @r, $deps if $deps;
318 foreach my $target_node (values %{ $toml->{target} // { } }) {
319 $process->($target_node);
324 our @alt_cargo_lock_stat;
326 sub consider_alt_cargo_lock () {
327 my @ck = qw(alt_cargo_lock);
328 # User should *either* have Cargo.lock in .gitignore,
329 # or expect to commit Cargo.lock.example ($alt_cargo_lock)
331 return unless $do_cargo_lock;
333 $alt_cargo_lock = (cfg_uc @ck);
336 if (defined($alt_cargo_lock) && ref($alt_cargo_lock) eq 'HASH') {
337 $force = cfg_bool qw(alt_cargo_lock force);
338 my @ck = qw(alt_cargo_lock file);
339 $alt_cargo_lock = cfg_uc @ck;
341 $alt_cargo_lock //= Types::Serialiser::true;
343 if (Types::Serialiser::is_bool $alt_cargo_lock) {
344 if (!$alt_cargo_lock) { $alt_cargo_lock = undef; return; }
345 $alt_cargo_lock = 'Cargo.lock.example';
348 if (ref($alt_cargo_lock) || $alt_cargo_lock =~ m{/}) {
349 badcfg @ck, "expected boolean, or leafname";
352 if (!stat_exists $alt_cargo_lock, "alt_cargo_lock") {
353 $alt_cargo_lock = undef unless $force;
357 @alt_cargo_lock_stat = stat _;
360 our $oot_dir; # oot.dir or "Build"
363 sub consider_oot () {
364 $oot_dir = cfgs qw(oot dir);
365 my $use = cfgs qw(oot use);
366 unless (defined($oot_dir) || defined($use) ||
367 defined(cfg_uc qw(oot user))) {
370 if (($use//'') eq 'disable') {
374 $oot_clean //= cfg_bool qw(oot clean);
375 $oot_dir //= 'Build';
376 $oot_absdir = ($oot_dir !~ m{^/} ? "$worksphere/" : ""). $oot_dir;
384 sub read_manifest ($$$) {
385 my ($subdir, $org_subdir, $why) = @_;
386 my $manifest = "../$subdir/Cargo.toml";
387 print STDERR "$self: reading $manifest...\n" if $verbose>=4;
388 if (defined $manifests{$manifest}) {
390 "$self: warning: $subdir: specified more than once!".
391 " (ignoring $why)\n";
394 foreach my $try ("$manifest.unnailed", "$manifest") {
395 my $toml = toml_or_enoent($try, "manifest, in $why") // next;
396 my $ws = $toml->{workspace};
398 queue_workspace_members($subdir, $org_subdir, $ws, "$subdir, $why");
400 my $p = $toml->{package}{name};
401 if (!defined $p and !defined $ws) {
403 "$self: warning: $subdir, $why: missing package.name in $try, ignoring\n";
406 $manifests{$manifest} = [ $toml, $org_subdir ] if $p;
407 foreach my $dep (get_dependency_tables $toml) {
408 next unless defined $dep->{path};
409 queue_referenced_path($dep->{path}, $org_subdir,
410 "dependency of $subdir, $why");
417 sub queue_workspace_members ($$) {
418 my ($subdir, $org_subdir, $ws_toml, $what) = @_;
419 # We need to (more or less) reimplement the cargo workspace
420 # membership algorithm (see the "workspaces" section of the cargo
421 # reference). How tiresome.
423 # It's not quite the same for us because we aren't interested in
424 # whether cargo thinks things are "in the workspace". But we do
425 # need to do the automatic discover.
427 my @include = @{ $ws_toml->{members} // [ ] };
428 my $exclude = $ws_toml->{exclude} // [ ];
431 s/[^*?0-9a-zA-Z_]/\\$&/g;
436 foreach my $spec (@include) {
437 if ($spec =~ m{^/}) {
439 "$self: warning: absolute workspace member $spec in $what (not nailing, but cargo will probably use it)\n";
442 my $spec_glob = "../$subdir/$spec";
443 my $globflags = GLOB_ERR|GLOB_BRACE|GLOB_NOMAGIC;
444 foreach my $globent (bsd_glob($spec_glob, $globflags)) {
445 next if grep { $globent =~ m{^$_$} } @exclude;
446 queue_referenced_path($globent, $org_subdir,
447 "member of workspace $what");
452 sub queue_referenced_path ($$$) {
453 my ($spec_path, $org_subdir, $why) = @_;
455 qw(realpath), "--relative-to=../$org_subdir", "--", $spec_path
456 or die "$self: fork/pipe/exec for realpath(1)\n";
457 my $rel_path = do { local $/=undef; <REALPATH>; };
459 my $r = close(REALPATH);
460 die "$self: reap realpath: $!\n" if $!;
461 if (!chomp($rel_path) or $?) {
463 "$self: warning: failed to determine realpath for $spec_path in $org_subdir (exit code $?)\n";
466 if ($rel_path =~ m{^\.\./} or $rel_path eq '..') {
468 "$self: warning: $spec_path ($why) points outside $org_subdir, not following so not nailing (although cargo probably will follow it)\n";
472 my $q_subdir = "$org_subdir/$rel_path";
473 print STDERR "$self: making a note to look at $q_subdir, $why)\n"
476 push @queued_paths, [ "$q_subdir", $org_subdir, $why ];
480 # We (and our callees) populate %packagemap and %manifest, so if we
481 # don't run, they remain empty and nothing is nailed.
482 return unless $do_nail;
484 foreach my $p (keys %{ $nail->{packages} }) {
485 my $v = $nail->{packages}{$p};
486 my $subdir = ref($v) ? $v->{subdir} : $v;
487 my ($gotpackage, $ws) = read_manifest($subdir, $subdir, "from [packages]");
488 $gotpackage //= '<nothing!>';
489 if ($gotpackage ne $p) {
491 "$self: warning: honouring Cargo.nail packages.$subdir=$p even though $subdir contains package $gotpackage!\n";
493 die if defined $packagemap{$p};
494 $packagemap{$p} = [ $subdir, $subdir ];
496 foreach my $subdir (@{ $nail->{subdirs} }) {
497 my ($gotpackage,$ws) = read_manifest($subdir, $subdir, "from [subdirs]");
498 if (!defined $gotpackage) {
500 "$self: warning: ignoring subdir $subdir which has no (suitable) Cargo.toml\n"
504 $packagemap{$gotpackage} //= [ $subdir, $subdir ];
506 while (my ($subdir, $org_subdir, $why) = @{ shift @queued_paths or [] }) {
507 next if $manifests{"../$subdir/Cargo.toml"};
508 my ($gotpackage, $ws) = read_manifest($subdir, $org_subdir, $why);
509 next unless $gotpackage;
510 $packagemap{$gotpackage} //= [ $subdir, $org_subdir ];
515 foreach my $p (sort keys %packagemap) {
516 print STDERR "$self: package $p in $packagemap{$p}[0]\n" if $verbose>=2;
518 foreach my $mf (keys %manifests) {
519 die "internal error" unless $do_nail; # belt and braces
521 my ($toml, $mf_org_subdir) = @{ $manifests{$mf} };
522 foreach my $deps (get_dependency_tables $toml) {
524 foreach my $p (keys %packagemap) {
525 my $info = $deps->{$p};
526 next unless defined $info;
527 next if $packagemap{$p}[1] eq $mf_org_subdir;
528 $deps->{$p} = $info = { } unless ref $info;
529 my $oldpath = $info->{path};
530 delete $info->{version};
531 my $newpath = $worksphere.'/'.$packagemap{$p}[0];
532 print STDERR "in $mf set $p path=$newpath (was ".
533 ($oldpath // '<unset>').")\n"
535 $info->{path} = $newpath;
537 delete $info->{branch};
540 my $nailing = "$mf.nailing~";
541 unlink_or_enoent $nailing or die "$self: remove old $nailing: $!\n";
542 open N, '>', $nailing or die "$self: create new $nailing: $!\n";
543 print N to_toml($toml) or die "$self: write new $nailing: $!\n";
544 close N or die "$self: close new $nailing: $!\n";
549 if ($just_linkfarm) {
550 die "$self: --just-linkfarm but not doing out-of-tree builds!\n"
551 unless defined $oot_dir;
556 if (!defined $online) {
557 $_ = cfg_uc qw(misc online);
559 } elsif (Types::Serialiser::is_bool $_) {
564 } elsif (m/^[1ty]/) { # allow booleanish strings
565 $online = 1; # for less user frustration
566 } elsif (m/^[0fn]/) {
569 badcfg qw(misc online), "expected boolean or 'auto', found '$_'";
572 $online //= 1 if subcmd_p('online');
575 $cargo_lock_update //= subcmd_p('lock-update');
577 subcmd_p('linkfarm-shallow') ? 'shallow' :
578 $cargo_lock_update ? 'shallow' :
583 if (!$cargo_lock_update) {
584 push @add, qw(--locked) unless subcmd_p('!locked');
586 if ($linkfarm_depth eq '') {
587 if (defined($oot_dir) && !subcmd_p('!manifest-path')) {
588 my $cargotoml = "${src_absdir}/Cargo.toml";
589 push @args_preface, "--manifest-path=$cargotoml" if $pass_options;
590 push @add, qw(--target-dir=target) unless subcmd_p('!target-dir');
594 if (defined($target) && !subcmd_p('!target')) {
595 if ($target =~ m{^[A-Z]}) {
596 $target = (cfgs 'arch', $target) // $archmap{$target}
597 // die "$self: --target=$target alias specified; not in cfg or map\n";
599 push @add, "--target=$target";
602 push @add, "--offline" unless $online || subcmd_p('!offline');
604 if (subcmd_p('edits') && $linkfarm_depth ne 'copy-edit') {
606 "$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";
609 push @args_preface, @add if $pass_options;
610 die if grep { m/ / } @add;
611 $ENV{NAILINGCARGO_CARGO_OPTIONS} = "@add";
613 unshift @ARGV, @args_preface;
616 our $build_absdir; # .../Build/<subdir>
618 sub oot_massage_cmdline () {
619 return unless defined $oot_dir;
621 my $use = cfgs qw(oot use);
622 $use // die "$self: out-of-tree build, but \`oot.use' not configured\n";
623 $build_absdir = "$oot_absdir/$subdir";
625 my ($pre,$post) = ('','');
627 if ($linkfarm_depth eq '') {
628 push @xargs, $build_absdir;
629 ($pre, $post) = ('cd "$1"; shift; ', '');
631 push @xargs, $oot_absdir, $subdir, $src_absdir;
633 bld="$1"; shift; sd="$1"; shift; src="$1"; shift;
634 cd "$bld"; mkdir -p -- "$sd"; cd "$sd";
636 if ($linkfarm_depth eq 'shallow') {
637 $pre.= <<'END_SHALLOW';
638 clean () { find -lname "$src/*" -print0 | xargs -0r rm --; }; clean;
639 find "$src" -maxdepth 1 \! -name Cargo.lock -print0 |
640 xargs -0r sh -ec 'for f in "$@"; do
645 } elsif ($linkfarm_depth =~ /full|git/) {
646 $pre .= <<'END_EITHER_DEEP_DIRS';
647 clean () { find -follow -lname "$src/*" -print0 | xargs -0r rm --; };
648 (set -e; cd "$src"; find . \! -name Cargo.lock \! \( -name .git -prune \) \! -path . \! -name .git -type d -print0) |
649 xargs -0r sh -ec 'for f in "$@"; do
650 rm -f "$f" 2>/dev/null ||:;
654 if ($linkfarm_depth eq 'git') {
655 $pre .= <<'END_FILES_GIT'
656 (set -e; cd "$src"; git ls-files --exclude-standard -co -z) |
658 } elsif ($linkfarm_depth eq 'full') {
659 $pre .= <<'END_FILES_FULL'
660 (set -e; cd "$src"; find . \! -name Cargo.lock \! \( -name .git -prune \) \! -type d -print0) |
663 $pre .= <<'END_DEEP';
665 BEGIN { $src=shift @ARGV; }
666 next if (readlink "$_"//"") eq "$src/$_";
668 symlink "$src/$_", "$_" or die "$_ $!";
671 } elsif ($linkfarm_depth eq 'copy-edit') {
672 $pre .= <<'END_COPY_EDIT';
673 find -lname "$src/*" -print0 | xargs -0r rm --;
674 (set -e; cd "$src"; git ls-files -c -z |
675 cpio --quiet -p0m --no-preserve-owner -u --make-directories "$bld/$sd");
677 (set -e; cd "$src"; git ls-files -c -z) | xargs -0r rm -f --;
680 $post .= <<'END_COPY_EDIT_BUNDLE';
681 (set -e; cd "$src"; git ls-files -c -z) |
682 cpio -Hustar -o0 --quiet >"nailing-cargo-update.tar";
685 die "$linkfarm_depth ?";
687 $pre .= <<'ENDLK' if $do_cargo_lock;
688 if test -e Cargo.lock; then
690 cp -- "$src"/Cargo.lock .;
693 $post .= <<'ENDCLEAN' if $oot_clean && !$just_linkfarm;
697 my $addpath = (cfg_uc qw(oot path_add)) //
698 $use eq 'really' ? Types::Serialiser::true : Types::Serialiser::false;
700 !Types::Serialiser::is_bool $addpath ? $addpath :
701 $addpath ? '$HOME/.cargo/bin' :
703 if (defined $addpath) {
705 PATH=$addpath:\${PATH-/usr/local/bin:/bin:/usr/bin};
709 $pre =~ s/^\s+//mg; $pre =~ s/\s+/ /g;
710 $post =~ s/^\s+//mg; $post =~ s/\s+/ /g;
712 my $getuser = sub { cfgsn qw(oot user) };
714 my $xe = $verbose >= 2 ? 'xe' : 'e';
717 @command = (@_, 'sh',"-${xe}c",$pre.'exec "$@"','--',@xargs);
719 @command = (@_, 'sh',"-${xe}c",$pre.'"$@"; '.$post,'--',@xargs);
721 push @command, @ARGV;
723 my $command_sh = sub {
724 my $quoted = join ' ', map {
729 @command = @_, "set -${xe}; $pre $quoted; $post";
731 print STDERR "$self: out-of-tree, building in: \`$build_absdir'\n"
733 if ($use eq 'really') {
734 my $user = $getuser->();
735 my @pw = getpwnam $user or die "$self: oot.user \`$user' lookup failed\n";
736 my $homedir = $pw[7];
737 $sh_ec->('really','-u',$user,'env',"HOME=$homedir");
738 print STDERR "$self: using really to run as user \`$user'\n" if $verbose;
739 } elsif ($use eq 'ssh') {
740 my $user = $getuser->();
741 $user .= '@localhost' unless $user =~ m/\@/;
742 $command_sh->('ssh',$user);
743 print STDERR "$self: using ssh to run as \`$user'\n" if $verbose;
744 } elsif ($use eq 'command_args') {
745 my @c = cfgn_list qw(oot command);
747 print STDERR "$self: out-of-tree, adverbial command: @c\n" if $verbose;
748 } elsif ($use eq 'command_sh') {
749 my @c = cfgn_list qw(oot command);
751 print STDERR "$self: out-of-tree, ssh'ish command: @c\n" if $verbose;
752 } elsif ($use eq 'null') {
755 die "$self: oot.use mode $use not recognised\n";
762 $ENV{CARGO_MANIFEST_DIR} = $src_absdir unless $linkfarm_depth;
763 $ENV{NAILINGCARGO_MANIFEST_DIR} = $src_absdir;
764 $ENV{NAILINGCARGO_WORKSPHERE} = $worksphere;
765 $ENV{NAILINGCARGO_BUILDSPHERE} = $oot_absdir;
766 delete $ENV{NAILINGCARGO_BUILDSPHERE} unless $oot_absdir;
767 $ENV{NAILINGCARGO_BUILD_DIR} = $build_absdir // $src_absdir;
773 if ($want_uninstall) {
775 foreach my $mf (keys %manifests) {
776 eval { uninstall1($mf,1); 1; } or warn "$@";
778 eval { unaltcargolock(1); 1; } or warn "$@";
782 sub consider_directories () {
783 return unless defined $oot_dir;
784 my $bsubdir = "../$oot_dir/$subdir";
785 return if stat $bsubdir;
786 die "$0: build directory $bsubdir inaccessible\n"
788 return if $cargo_lock_update; # will make it
789 die "$0: build directory $bsubdir does not exist, and not in Cargo.lock update mode!\n";
792 our $cleanup_cargo_lock;
794 foreach my $mf (keys %manifests) {
795 link "$mf", "$mf.unnailed" or $!==EEXIST
796 or die "$self: make backup link $mf.unnailed: $!\n";
799 if (defined($alt_cargo_lock)) {
800 die 'internal error' unless $do_cargo_lock;
801 if (@alt_cargo_lock_stat) {
802 print STDERR "$self: using alt_cargo_lock `$alt_cargo_lock'..."
804 if (link $alt_cargo_lock, 'Cargo.lock') {
805 print STDERR " linked\n" if $verbose>=3;
806 } elsif ($! != EEXIST) {
807 print STDERR "\n" if $verbose>=3;
808 die "$self: make \`Cargo.lock' available as \`$alt_cargo_lock': $!\n";
810 print STDERR "checking quality." if $verbose>=3;
811 my @lock_stat = stat 'Cargo.lock'
812 or die "$self: stat Cargo.lock (for alt check: $!\n";
813 same_file(\@alt_cargo_lock_stat, \@lock_stat)
815 "$self: \`Cargo.lock' and alt file \`$alt_cargo_lock' both exist and are not the same file!\n";
817 $cleanup_cargo_lock = 1;
819 $cleanup_cargo_lock = 1;
820 # If Cargo.lock exists and alt doesn't, that means either
821 # that a previous run was interrupted, or that the user has
829 my $nailed = "$mf.nailed~"; $nailed =~ s{/([^/]+)$}{/.$1} or die;
834 my @our_unfound_stab = stat_exists('Cargo.toml', 'local Cargo.toml')
836 foreach my $mf (keys %manifests) {
837 if (@our_unfound_stab) {
838 if (stat_exists $mf, "manifest in to-be-nailed directory") {
839 my @mf_stab = stat _ ;
840 if ("@mf_stab[0..1]" eq "@our_unfound_stab[0..1]") {
841 @our_unfound_stab = ();
846 my $nailing = "$mf.nailing~";
847 my $nailed = nailed($mf);
850 if (open NN, '<', $nailed) {
851 $diff = compare($nailing, \*NN);
852 die "$self: compare $nailing and $nailed: $!" if $diff<0;
854 $!==ENOENT or die "$self: check previous $nailed: $!\n";
864 rename $use, $mf or die "$self: install nailed $use: $!\n";
865 unlink_or_enoent $rm or die "$self: remove old $rm: $!\n";
866 print STDERR "$self: nailed $mf\n" if $verbose>=3;
869 if (@our_unfound_stab && $do_nail) {
871 "$self: *WARNING* cwd is not in Cargo.nail thbough it has Cargo.toml!\n";
876 my $r = system @ARGV;
880 print STDERR "$self: could not execute $ARGV[0]: $!\n";
882 } elsif ($r & 0xff00) {
883 print STDERR "$self: $ARGV[0] failed (exit status $r)\n";
886 print STDERR "$self: $ARGV[0] died due to signal! (wait status $r)\n";
891 sub files_return_after_update () {
892 if ($linkfarm_depth eq 'copy-edit') {
893 system qw(sh -ec), <<'END', 'x', "$build_absdir";
894 git ls-files -c -z | \
895 tar -x --keep-newer-files --no-same-permissions --no-same-owner \
896 --no-acls --no-selinux --no-xattrs --warning=no-ignore-newer \
897 -Hustar --null --files-from=- --force-local \
898 -f "$1/nailing-cargo-update.tar"
900 } elsif ($do_cargo_lock && $cargo_lock_update && !$just_linkfarm) {
901 # avoids importing File::Copy and the error handling is about as good
903 my $r= system qw(cp --), "$build_absdir/Cargo.lock", "Cargo.lock";
904 die "$self: run cp: $! $?" if $r<0 || $r & 0xff;
905 die "$self: failed to update local Cargo.lock (wait status $r)\n" if $r;
909 sub uninstall1 ($$) {
910 my ($mf, $enoentok) = @_;
911 my $unnailed = "$mf.unnailed";
912 rename $unnailed, $mf or ($enoentok && $!==ENOENT)
913 or die "$self: failed to restore: rename $unnailed back to $mf: $!\n";
916 sub unaltcargolock ($) {
918 return unless $cleanup_cargo_lock;
919 die 'internal error!' unless $do_cargo_lock && defined $alt_cargo_lock;
921 # we ignore $enoentok because we don't know if one was supposed to
924 rename('Cargo.lock', $alt_cargo_lock) or $!==ENOENT or die
925 "$self: cleanup: rename possibly-updated \`Cargo.lock' to \`$alt_cargo_lock': $!\n";
927 unlink 'Cargo.lock' or $!==ENOENT or die
928 "$self: cleanup: remove \`Cargo.lock' in favour of \`$alt_cargo_lock': $!\n";
929 # ^ this also helps clean up the stupid rename() corner case
933 foreach my $mf (keys %manifests) {
934 my $nailed = nailed($mf);
935 link $mf, $nailed or die "$self: preserve (link) $mf as $nailed: $!\n";
944 # Loop exit condition:
947 # $is_cargo==1 <cargo-command> <cargo-opts> [--] <subcmd>...
948 # $is_cargo==0 <build-command>...
952 die "$self: need cargo subcommand\n"
953 unless $noact || $just_linkfarm;;
954 push @ARGV, "CARGO-SUBCOMMAND"; # dummy, user may see it
960 my $not_a_nailing_opt = sub { # usage 1
961 unshift @ARGV, $orgopt;
962 unshift @ARGV, 'cargo';
964 no warnings qw(exiting);
967 $not_a_nailing_opt->() unless m{^-};
968 $not_a_nailing_opt->() if $_ eq '--';
970 if ($_ eq '---') { # usage 2 or 3
972 die "$self: --- must be followed by build command\n" unless $noact;
973 push @ARGV, 'BUILD-COMMAND';
975 if ($ARGV[0] eq '--') { # usage 3
978 } elsif (grep { $_ eq '--' } @ARGV) { # usage 2
980 } elsif ($ARGV[0] =~ m{[^/]*cargo[^/]*$}) { # usage 2
991 } elsif (s{^-v}{-}) {
993 } elsif (s{^-q}{-}) {
995 } elsif (s{^-n}{-}) {
997 } elsif (s{^-s(.+)}{-}s) {
999 } elsif (s{^-([uU])}{-}) {
1000 $cargo_lock_update = $1=~m/[a-z]/;
1001 } elsif (s{^-([cC])}{-}) {
1002 $pass_options = $1=~m/[a-z]/;
1003 } elsif (s{^-D}{-}) {
1005 } elsif (s{^-E}{-}) {
1006 $linkfarm_depth = 'copy-edit';
1007 } elsif (s{^-T(.+)}{-}s) {
1009 } elsif (s{^-([oO])}{-}) {
1010 $online = $1=~m/[a-z]/;
1012 die "$self: unknown short option(s) $_\n" unless $_ eq $orgopt;
1013 $not_a_nailing_opt->();
1016 } elsif (s{^--help$}{}) {
1018 } elsif (s{^--(?:doc|man|manual)?$}{}) {
1020 } elsif (s{^--target=}{}) {
1022 } elsif (m{^--(on|off)line$}) {
1023 $online = $1 eq 'on';
1024 } elsif (m{^--just-linkfarm(?:=(shallow|git|full))?$}) {
1026 $linkfarm_depth = $1 if $1;
1027 $cargo_lock_update= 1; # will set $linkfarm_detph to 1 by default
1028 } elsif (m{^--linkfarm(?:=(no|shallow|git|full))?$}) {
1029 $linkfarm_depth = $1 || 'git';
1030 } elsif (m{^--edits?-sources?$}) {
1031 $linkfarm_depth = 'copy-edit';
1032 } elsif (m{^--just-run$}) {
1033 $do_nail = $do_cargo_lock = $do_lock = 0;
1034 } elsif (m{^--(clean|keep)-linkfarm$}) {
1035 $oot_clean = $1 eq 'clean';
1036 } elsif (m{^--(no)?-nail$}) {
1038 } elsif (m{^--(no)?-cargo-lock-manip$}) {
1039 $do_cargo_lock = !$1;
1040 } elsif (m{^--(no)?-concurrency-lock$}) {
1042 } elsif (m{^--leave-nailed$}) {
1044 } elsif (s{^--subcommand-props=}{}) {
1045 my @props = split /\,/, $_;
1046 our %subcmd_prop_ok;
1047 if (!%subcmd_prop_ok) {
1048 foreach my $v (\@subcmd_xprops, values %subcmd_props) {
1049 $subcmd_prop_ok{$_}=1 foreach @$v;
1053 or die "$self: unknown subcommand property \`$_'\n"
1055 $cargo_subcmd = \@props;
1056 } elsif (m{^--(no-)?cargo-lock-update}) {
1057 $cargo_lock_update= !!$1;
1059 $not_a_nailing_opt->();
1067 @args_preface = shift @ARGV;
1068 while (defined($_ = shift @ARGV)) {
1069 if (!m{^-}) { unshift @ARGV, $_; last; }
1070 if ($_ eq '--') { last; }
1071 push @args_preface, $_;
1073 @ARGV || die "$self: need cargo subcommand\n";
1074 $cargo_subcmd //= $ARGV[0];
1075 $pass_options //= 1;
1077 $cargo_subcmd //= '';
1078 $pass_options //= 0;
1080 push @args_preface, shift @ARGV;
1082 if (!ref($cargo_subcmd)) {
1083 print STDERR " cargo_subcmd lookup $cargo_subcmd\n" if $dump;
1084 $cargo_subcmd = $subcmd_props{$cargo_subcmd} // [ ];
1087 print STDERR " cargo_subcmd props @$cargo_subcmd\n" if $dump;
1089 $cargo_subcmd{$_} = 1 foreach @$cargo_subcmd;
1090 $cargo_subcmd = \%cargo_subcmd;
1097 consider_alt_cargo_lock();
1102 consider_directories();
1103 our @display_cmd = @ARGV;
1104 oot_massage_cmdline();
1110 print STDERR Dumper(\%manifests) if $dump>=2;
1111 print STDERR Dumper(\%packagemap, \@ARGV,
1112 { src_absdir => $src_absdir,
1113 worksphere => $worksphere,
1115 oot_dir => $oot_dir,
1116 oot_absdir => $oot_absdir,
1117 build_absdir => $build_absdir });
1123 $want_uninstall = !$leave_nailed;
1127 printf STDERR "$self: nailed (%s manifests, %s packages)%s\n",
1128 (scalar keys %manifests), (scalar keys %packagemap),
1129 (defined($alt_cargo_lock) and ", using `$alt_cargo_lock'")
1130 if $verbose && $do_nail;
1132 print STDERR "$self: invoking: @display_cmd\n" if $verbose;
1133 my $estatus = invoke();
1135 files_return_after_update();
1137 uninstall() unless $leave_nailed;
1138 $want_uninstall = 0;
1140 print STDERR "$self: ".($do_nail ? "unnailed" : "finished")
1141 .". status $estatus.\n" if $verbose;