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 )],
113 our @subcmd_xprops = qw(!manifest-path);
121 print $usage or die $!;
126 my $manual = ($base_path // '.').'/README.md';
127 stat $manual or die "$self: manual not found at $manual: $!\n";;
128 exec 'sh','-ec', 'pandoc -- "$1" 2>&1 | w3m -T text/html', '--', $manual;
129 die "$self: exec sh failed: $!";
132 sub read_or_enoent ($) {
134 if (!open R, '<', $fn) {
135 return undef if $!==ENOENT;
136 die "$self: open $fn: $!\n";
139 my ($r) = <R> // die "$self: read $fn: $!\n";
143 sub stat_exists ($$) {
144 my ($fn, $what) = @_;
145 if (stat $fn) { return 1; }
146 $!==ENOENT or die "$self: stat $what: $fn: $!\n";
151 print STDERR " subcmd_p ".(join ' ', keys %$cargo_subcmd)." | @_\n"
153 $cargo_subcmd->{$_[0]}
156 sub toml_or_enoent ($$) {
158 my $toml = read_or_enoent($f) // return;
159 print STDERR "Read TOML from $f\n" if $dump;
160 my ($v,$e) = from_toml($toml);
163 die "$self: parse TOML: $what: $f: $e\n";
165 die "$e ?" if length $e;
169 sub load1config ($) {
171 my $toml = toml_or_enoent($f, "config file");
172 push @configs, $toml if defined $toml;
176 my $dotfile = ".nailing-cargo.toml";
177 load1config("../Nailing-Cargo.toml");
178 load1config($dotfile);
179 load1config("$ENV{HOME}/$dotfile") if defined $ENV{HOME};
180 load1config("/etc/nailing-cargo/cfg.toml");
183 sub unlink_or_enoent ($) { unlink $_[0] or $!==ENOENT; }
187 "@$x[0..5]" eq "@$y[0..5]";
191 return unless $do_lock;
194 open LOCK, ">", $lockfile or die "$self: open/create $lockfile: $!\n";
195 flock LOCK, LOCK_EX or die "$self: lock $lockfile: $!\n";
196 my @fstat = stat LOCK or die "$self: fstat: $!\n";
197 my @stat = stat $lockfile;
199 next if $! == ENOENT;
200 die "$self: stat $lockfile: $!\n";
202 last if same_file(\@fstat,\@stat);
206 unlink $lockfile or die "$self: removing lockfile: $!\n";
214 die "$self: config key \`@_': $m\n";
218 foreach my $cfg (@configs) {
221 last unless defined $v;
222 ref($v) eq 'HASH' or badcfg @_, "parent key \`$k' is not a hash";
225 return $v if defined $v;
233 my $got = ref($v) || 'scalar';
234 return $v if !defined($v) || $got eq $exp;
235 badcfg @_, "found \L$got\E, expected \L$exp\E";
236 # ^ toml doesn't make refs to scalars, so this is unambiguous
241 (cfge $exp, @_) // badcfg @_, "missing";
244 sub cfgs { cfge 'scalar', @_ }
245 sub cfgsn { cfgn 'scalar', @_ }
249 return $v if !defined($v) || Types::Serialiser::is_bool $v;
250 badcfg @_, "expected boolean";
254 my $l = cfge 'ARRAY', @_;
255 foreach my $x (@$l) {
256 !ref $x or badcfg @_, "list contains non-scalar element";
262 my $nailfile = "../Cargo.nail";
263 open N, '<', $nailfile or die "$self: open $nailfile: $!\n";
265 my $toml = <N> // die "$self: read $nailfile: $!";
267 if ($toml !~ m{^\s*\[/}m &&
268 $toml !~ m{^[^\n\#]*\=}m &&
269 # old non-toml syntax
270 $toml =~ s{^[ \t]*([-_0-9a-z]+)[ \t]+(\S+)[ \t]*$}{$1 = \"$2\"}mig) {
271 $toml =~ s{^}{[packages\]\n};
273 $toml =~ s{^[ \t]*\-[ \t]*\=[ \t]*(\"[-_0-9a-z]+\"\n?)$}{
276 $toml = "subdirs = [\n".(join '', map { "$_\n" } @sd)."]\n".$toml;
280 ($nail,$e) = from_toml($toml);
281 if (!defined $nail) {
284 print STDERR "$self: $nailfile transformed into TOML:\n$toml\n";
287 die "$self: parse $nailfile: $e\n";
289 die "$e ?" if length $e;
291 $nail->{subdirs} //= [ ];
293 if (!ref $nail->{subdirs}) {
296 map { s/^\s+//; s/\s+$//; $_; }
302 unshift @configs, $nail;
305 sub get_dependency_tables ($) {
307 my @keys = qw(dependencies build-dependencies dev-dependencies);
311 foreach my $k (@keys) {
312 my $deps = $node->{$k};
313 push @r, $deps if $deps;
317 foreach my $target_node (values %{ $toml->{target} // { } }) {
318 $process->($target_node);
323 our @alt_cargo_lock_stat;
325 sub consider_alt_cargo_lock () {
326 my @ck = qw(alt_cargo_lock);
327 # User should *either* have Cargo.lock in .gitignore,
328 # or expect to commit Cargo.lock.example ($alt_cargo_lock)
330 return unless $do_cargo_lock;
332 $alt_cargo_lock = (cfg_uc @ck);
335 if (defined($alt_cargo_lock) && ref($alt_cargo_lock) eq 'HASH') {
336 $force = cfg_bool qw(alt_cargo_lock force);
337 my @ck = qw(alt_cargo_lock file);
338 $alt_cargo_lock = cfg_uc @ck;
340 $alt_cargo_lock //= Types::Serialiser::true;
342 if (Types::Serialiser::is_bool $alt_cargo_lock) {
343 if (!$alt_cargo_lock) { $alt_cargo_lock = undef; return; }
344 $alt_cargo_lock = 'Cargo.lock.example';
347 if (ref($alt_cargo_lock) || $alt_cargo_lock =~ m{/}) {
348 badcfg @ck, "expected boolean, or leafname";
351 if (!stat_exists $alt_cargo_lock, "alt_cargo_lock") {
352 $alt_cargo_lock = undef unless $force;
356 @alt_cargo_lock_stat = stat _;
359 our $oot_dir; # oot.dir or "Build"
362 sub consider_oot () {
363 $oot_dir = cfgs qw(oot dir);
364 my $use = cfgs qw(oot use);
365 unless (defined($oot_dir) || defined($use) ||
366 defined(cfg_uc qw(oot user))) {
369 if (($use//'') eq 'disable') {
373 $oot_clean //= cfg_bool qw(oot clean);
374 $oot_dir //= 'Build';
375 $oot_absdir = ($oot_dir !~ m{^/} ? "$worksphere/" : ""). $oot_dir;
383 sub read_manifest ($$$) {
384 my ($subdir, $org_subdir, $why) = @_;
385 my $manifest = "../$subdir/Cargo.toml";
386 print STDERR "$self: reading $manifest...\n" if $verbose>=4;
387 if (defined $manifests{$manifest}) {
389 "$self: warning: $subdir: specified more than once!".
390 " (ignoring $why)\n";
393 foreach my $try ("$manifest.unnailed", "$manifest") {
394 my $toml = toml_or_enoent($try, "manifest, in $why") // next;
395 my $ws = $toml->{workspace};
397 queue_workspace_members($subdir, $org_subdir, $ws, "$subdir, $why");
399 my $p = $toml->{package}{name};
400 if (!defined $p and !defined $ws) {
402 "$self: warning: $subdir, $why: missing package.name in $try, ignoring\n";
405 $manifests{$manifest} = [ $toml, $org_subdir ] if $p;
406 foreach my $dep (get_dependency_tables $toml) {
407 next unless defined $dep->{path};
408 queue_referenced_path($dep->{path}, $org_subdir,
409 "dependency of $subdir, $why");
416 sub queue_workspace_members ($$) {
417 my ($subdir, $org_subdir, $ws_toml, $what) = @_;
418 # We need to (more or less) reimplement the cargo workspace
419 # membership algorithm (see the "workspaces" section of the cargo
420 # reference). How tiresome.
422 # It's not quite the same for us because we aren't interested in
423 # whether cargo thinks things are "in the workspace". But we do
424 # need to do the automatic discover.
426 my @include = @{ $ws_toml->{members} // [ ] };
427 my $exclude = $ws_toml->{exclude} // [ ];
430 s/[^*?0-9a-zA-Z_]/\\$&/g;
435 foreach my $spec (@include) {
436 if ($spec =~ m{^/}) {
438 "$self: warning: absolute workspace member $spec in $what (not nailing, but cargo will probably use it)\n";
441 my $spec_glob = "../$subdir/$spec";
442 my $globflags = GLOB_ERR|GLOB_BRACE|GLOB_NOMAGIC;
443 foreach my $globent (bsd_glob($spec_glob, $globflags)) {
444 next if grep { $globent =~ m{^$_$} } @exclude;
445 queue_referenced_path($globent, $org_subdir,
446 "member of workspace $what");
451 sub queue_referenced_path ($$$) {
452 my ($spec_path, $org_subdir, $why) = @_;
454 qw(realpath), "--relative-to=../$org_subdir", "--", $spec_path
455 or die "$self: fork/pipe/exec for realpath(1)\n";
456 my $rel_path = do { local $/=undef; <REALPATH>; };
458 my $r = close(REALPATH);
459 die "$self: reap realpath: $!\n" if $!;
460 if (!chomp($rel_path) or $?) {
462 "$self: warning: failed to determine realpath for $spec_path in $org_subdir (exit code $?)\n";
465 if ($rel_path =~ m{^\.\./} or $rel_path eq '..') {
467 "$self: warning: $spec_path ($why) points outside $org_subdir, not following so not nailing (although cargo probably will follow it)\n";
471 my $q_subdir = "$org_subdir/$rel_path";
472 print STDERR "$self: making a note to look at $q_subdir, $why)\n"
475 push @queued_paths, [ "$q_subdir", $org_subdir, $why ];
479 # We (and our callees) populate %packagemap and %manifest, so if we
480 # don't run, they remain empty and nothing is nailed.
481 return unless $do_nail;
483 foreach my $p (keys %{ $nail->{packages} }) {
484 my $v = $nail->{packages}{$p};
485 my $subdir = ref($v) ? $v->{subdir} : $v;
486 my ($gotpackage, $ws) = read_manifest($subdir, $subdir, "from [packages]");
487 $gotpackage //= '<nothing!>';
488 if ($gotpackage ne $p) {
490 "$self: warning: honouring Cargo.nail packages.$subdir=$p even though $subdir contains package $gotpackage!\n";
492 die if defined $packagemap{$p};
493 $packagemap{$p} = [ $subdir, $subdir ];
495 foreach my $subdir (@{ $nail->{subdirs} }) {
496 my ($gotpackage,$ws) = read_manifest($subdir, $subdir, "from [subdirs]");
497 if (!defined $gotpackage) {
499 "$self: warning: ignoring subdir $subdir which has no (suitable) Cargo.toml\n"
503 $packagemap{$gotpackage} //= [ $subdir, $subdir ];
505 while (my ($subdir, $org_subdir, $why) = @{ shift @queued_paths or [] }) {
506 next if $manifests{"../$subdir/Cargo.toml"};
507 my ($gotpackage, $ws) = read_manifest($subdir, $org_subdir, $why);
508 next unless $gotpackage;
509 $packagemap{$gotpackage} //= [ $subdir, $org_subdir ];
514 foreach my $p (sort keys %packagemap) {
515 print STDERR "$self: package $p in $packagemap{$p}[0]\n" if $verbose>=2;
517 foreach my $mf (keys %manifests) {
518 die "internal error" unless $do_nail; # belt and braces
520 my ($toml, $mf_org_subdir) = @{ $manifests{$mf} };
521 foreach my $deps (get_dependency_tables $toml) {
523 foreach my $p (keys %packagemap) {
524 my $info = $deps->{$p};
525 next unless defined $info;
526 next if $packagemap{$p}[1] eq $mf_org_subdir;
527 $deps->{$p} = $info = { } unless ref $info;
528 my $oldpath = $info->{path};
529 delete $info->{version};
530 my $newpath = $worksphere.'/'.$packagemap{$p}[0];
531 print STDERR "in $mf set $p path=$newpath (was ".
532 ($oldpath // '<unset>').")\n"
534 $info->{path} = $newpath;
536 delete $info->{branch};
539 my $nailing = "$mf.nailing~";
540 unlink_or_enoent $nailing or die "$self: remove old $nailing: $!\n";
541 open N, '>', $nailing or die "$self: create new $nailing: $!\n";
542 print N to_toml($toml) or die "$self: write new $nailing: $!\n";
543 close N or die "$self: close new $nailing: $!\n";
548 if ($just_linkfarm) {
549 die "$self: --just-linkfarm but not doing out-of-tree builds!\n"
550 unless defined $oot_dir;
555 if (!defined $online) {
556 $_ = cfg_uc qw(misc online);
558 } elsif (Types::Serialiser::is_bool $_) {
563 } elsif (m/^[1ty]/) { # allow booleanish strings
564 $online = 1; # for less user frustration
565 } elsif (m/^[0fn]/) {
568 badcfg qw(misc online), "expected boolean or 'auto', found '$_'";
571 $online //= 1 if subcmd_p('online');
574 $cargo_lock_update //= subcmd_p('lock-update');
576 subcmd_p('linkfarm-shallow') ? 'shallow' :
577 $cargo_lock_update ? 'shallow' :
582 if (!$cargo_lock_update) {
583 push @add, qw(--locked) unless subcmd_p('!locked');
585 if ($linkfarm_depth eq '') {
586 if (defined($oot_dir) && !subcmd_p('!manifest-path')) {
587 my $cargotoml = "${src_absdir}/Cargo.toml";
588 push @args_preface, "--manifest-path=$cargotoml" if $pass_options;
589 push @add, qw(--target-dir=target) unless subcmd_p('!target-dir');
593 if (defined($target) && !subcmd_p('!target')) {
594 if ($target =~ m{^[A-Z]}) {
595 $target = (cfgs 'arch', $target) // $archmap{$target}
596 // die "$self: --target=$target alias specified; not in cfg or map\n";
598 push @add, "--target=$target";
601 push @add, "--offline" unless $online || subcmd_p('!offline');
603 if (subcmd_p('edits') && $linkfarm_depth ne 'copy-edit') {
605 "$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";
608 push @args_preface, @add if $pass_options;
609 die if grep { m/ / } @add;
610 $ENV{NAILINGCARGO_CARGO_OPTIONS} = "@add";
612 unshift @ARGV, @args_preface;
615 our $build_absdir; # .../Build/<subdir>
617 sub oot_massage_cmdline () {
618 return unless defined $oot_dir;
620 my $use = cfgs qw(oot use);
621 $use // die "$self: out-of-tree build, but \`oot.use' not configured\n";
622 $build_absdir = "$oot_absdir/$subdir";
624 my ($pre,$post) = ('','');
626 if ($linkfarm_depth eq '') {
627 push @xargs, $build_absdir;
628 ($pre, $post) = ('cd "$1"; shift; ', '');
630 push @xargs, $oot_absdir, $subdir, $src_absdir;
632 bld="$1"; shift; sd="$1"; shift; src="$1"; shift;
633 cd "$bld"; mkdir -p -- "$sd"; cd "$sd";
635 if ($linkfarm_depth eq 'shallow') {
636 $pre.= <<'END_SHALLOW';
637 clean () { find -lname "$src/*" -print0 | xargs -0r rm --; }; clean;
638 find "$src" -maxdepth 1 \! -name Cargo.lock -print0 |
639 xargs -0r sh -ec 'for f in "$@"; do
644 } elsif ($linkfarm_depth =~ /full|git/) {
645 $pre .= <<'END_EITHER_DEEP_DIRS';
646 clean () { find -follow -lname "$src/*" -print0 | xargs -0r rm --; };
647 (set -e; cd "$src"; find . \! -name Cargo.lock \! \( -name .git -prune \) \! -path . \! -name .git -type d -print0) |
648 xargs -0r sh -ec 'for f in "$@"; do
649 rm -f "$f" 2>/dev/null ||:;
653 if ($linkfarm_depth eq 'git') {
654 $pre .= <<'END_FILES_GIT'
655 (set -e; cd "$src"; git ls-files --exclude-standard -co -z) |
657 } elsif ($linkfarm_depth eq 'full') {
658 $pre .= <<'END_FILES_FULL'
659 (set -e; cd "$src"; find . \! -name Cargo.lock \! \( -name .git -prune \) \! -type d -print0) |
662 $pre .= <<'END_DEEP';
664 BEGIN { $src=shift @ARGV; }
665 next if (readlink "$_"//"") eq "$src/$_";
667 symlink "$src/$_", "$_" or die "$_ $!";
670 } elsif ($linkfarm_depth eq 'copy-edit') {
671 $pre .= <<'END_COPY_EDIT';
672 find -lname "$src/*" -print0 | xargs -0r rm --;
673 (set -e; cd "$src"; git ls-files -c -z |
674 cpio --quiet -p0m --no-preserve-owner -u --make-directories "$bld/$sd");
676 (set -e; cd "$src"; git ls-files -c -z) | xargs -0r rm -f --;
679 $post .= <<'END_COPY_EDIT_BUNDLE';
680 (set -e; cd "$src"; git ls-files -c -z) |
681 cpio -Hustar -o0 --quiet >"nailing-cargo-update.tar";
684 die "$linkfarm_depth ?";
686 $pre .= <<'ENDLK' if $do_cargo_lock;
687 if test -e Cargo.lock; then
689 cp -- "$src"/Cargo.lock .;
692 $post .= <<'ENDCLEAN' if $oot_clean && !$just_linkfarm;
696 my $addpath = (cfg_uc qw(oot path_add)) //
697 $use eq 'really' ? Types::Serialiser::true : Types::Serialiser::false;
699 !Types::Serialiser::is_bool $addpath ? $addpath :
700 $addpath ? '$HOME/.cargo/bin' :
702 if (defined $addpath) {
704 PATH=$addpath:\${PATH-/usr/local/bin:/bin:/usr/bin};
708 $pre =~ s/^\s+//mg; $pre =~ s/\s+/ /g;
709 $post =~ s/^\s+//mg; $post =~ s/\s+/ /g;
711 my $getuser = sub { cfgsn qw(oot user) };
713 my $xe = $verbose >= 2 ? 'xe' : 'e';
716 @command = (@_, 'sh',"-${xe}c",$pre.'exec "$@"','--',@xargs);
718 @command = (@_, 'sh',"-${xe}c",$pre.'"$@"; '.$post,'--',@xargs);
720 push @command, @ARGV;
722 my $command_sh = sub {
723 my $quoted = join ' ', map {
728 @command = @_, "set -${xe}; $pre $quoted; $post";
730 print STDERR "$self: out-of-tree, building in: \`$build_absdir'\n"
732 if ($use eq 'really') {
733 my $user = $getuser->();
734 my @pw = getpwnam $user or die "$self: oot.user \`$user' lookup failed\n";
735 my $homedir = $pw[7];
736 $sh_ec->('really','-u',$user,'env',"HOME=$homedir");
737 print STDERR "$self: using really to run as user \`$user'\n" if $verbose;
738 } elsif ($use eq 'ssh') {
739 my $user = $getuser->();
740 $user .= '@localhost' unless $user =~ m/\@/;
741 $command_sh->('ssh',$user);
742 print STDERR "$self: using ssh to run as \`$user'\n" if $verbose;
743 } elsif ($use eq 'command_args') {
744 my @c = cfgn_list qw(oot command);
746 print STDERR "$self: out-of-tree, adverbial command: @c\n" if $verbose;
747 } elsif ($use eq 'command_sh') {
748 my @c = cfgn_list qw(oot command);
750 print STDERR "$self: out-of-tree, ssh'ish command: @c\n" if $verbose;
751 } elsif ($use eq 'null') {
754 die "$self: oot.use mode $use not recognised\n";
761 $ENV{CARGO_MANIFEST_DIR} = $src_absdir unless $linkfarm_depth;
762 $ENV{NAILINGCARGO_MANIFEST_DIR} = $src_absdir;
763 $ENV{NAILINGCARGO_WORKSPHERE} = $worksphere;
764 $ENV{NAILINGCARGO_BUILDSPHERE} = $oot_absdir;
765 delete $ENV{NAILINGCARGO_BUILDSPHERE} unless $oot_absdir;
766 $ENV{NAILINGCARGO_BUILD_DIR} = $build_absdir // $src_absdir;
772 if ($want_uninstall) {
774 foreach my $mf (keys %manifests) {
775 eval { uninstall1($mf,1); 1; } or warn "$@";
777 eval { unaltcargolock(1); 1; } or warn "$@";
781 sub consider_directories () {
782 return unless defined $oot_dir;
783 my $bsubdir = "../$oot_dir/$subdir";
784 return if stat $bsubdir;
785 die "$0: build directory $bsubdir inaccessible\n"
787 return if $cargo_lock_update; # will make it
788 die "$0: build directory $bsubdir does not exist, and not in Cargo.lock update mode!\n";
791 our $cleanup_cargo_lock;
793 foreach my $mf (keys %manifests) {
794 link "$mf", "$mf.unnailed" or $!==EEXIST
795 or die "$self: make backup link $mf.unnailed: $!\n";
798 if (defined($alt_cargo_lock)) {
799 die 'internal error' unless $do_cargo_lock;
800 if (@alt_cargo_lock_stat) {
801 print STDERR "$self: using alt_cargo_lock `$alt_cargo_lock'..."
803 if (link $alt_cargo_lock, 'Cargo.lock') {
804 print STDERR " linked\n" if $verbose>=3;
805 } elsif ($! != EEXIST) {
806 print STDERR "\n" if $verbose>=3;
807 die "$self: make \`Cargo.lock' available as \`$alt_cargo_lock': $!\n";
809 print STDERR "checking quality." if $verbose>=3;
810 my @lock_stat = stat 'Cargo.lock'
811 or die "$self: stat Cargo.lock (for alt check: $!\n";
812 same_file(\@alt_cargo_lock_stat, \@lock_stat)
814 "$self: \`Cargo.lock' and alt file \`$alt_cargo_lock' both exist and are not the same file!\n";
816 $cleanup_cargo_lock = 1;
818 $cleanup_cargo_lock = 1;
819 # If Cargo.lock exists and alt doesn't, that means either
820 # that a previous run was interrupted, or that the user has
828 my $nailed = "$mf.nailed~"; $nailed =~ s{/([^/]+)$}{/.$1} or die;
833 my @our_unfound_stab = stat_exists('Cargo.toml', 'local Cargo.toml')
835 foreach my $mf (keys %manifests) {
836 if (@our_unfound_stab) {
837 if (stat_exists $mf, "manifest in to-be-nailed directory") {
838 my @mf_stab = stat _ ;
839 if ("@mf_stab[0..1]" eq "@our_unfound_stab[0..1]") {
840 @our_unfound_stab = ();
845 my $nailing = "$mf.nailing~";
846 my $nailed = nailed($mf);
849 if (open NN, '<', $nailed) {
850 $diff = compare($nailing, \*NN);
851 die "$self: compare $nailing and $nailed: $!" if $diff<0;
853 $!==ENOENT or die "$self: check previous $nailed: $!\n";
863 rename $use, $mf or die "$self: install nailed $use: $!\n";
864 unlink_or_enoent $rm or die "$self: remove old $rm: $!\n";
865 print STDERR "$self: nailed $mf\n" if $verbose>=3;
868 if (@our_unfound_stab && $do_nail) {
870 "$self: *WARNING* cwd is not in Cargo.nail thbough it has Cargo.toml!\n";
875 my $r = system @ARGV;
879 print STDERR "$self: could not execute $ARGV[0]: $!\n";
881 } elsif ($r & 0xff00) {
882 print STDERR "$self: $ARGV[0] failed (exit status $r)\n";
885 print STDERR "$self: $ARGV[0] died due to signal! (wait status $r)\n";
890 sub files_return_after_update () {
891 if ($linkfarm_depth eq 'copy-edit') {
892 system qw(sh -ec), <<'END', 'x', "$build_absdir";
893 git ls-files -c -z | \
894 tar -x --keep-newer-files --no-same-permissions --no-same-owner \
895 --no-acls --no-selinux --no-xattrs --warning=no-ignore-newer \
896 -Hustar --null --files-from=- --force-local \
897 -f "$1/nailing-cargo-update.tar"
899 } elsif ($do_cargo_lock && $cargo_lock_update && !$just_linkfarm) {
900 # avoids importing File::Copy and the error handling is about as good
902 my $r= system qw(cp --), "$build_absdir/Cargo.lock", "Cargo.lock";
903 die "$self: run cp: $! $?" if $r<0 || $r & 0xff;
904 die "$self: failed to update local Cargo.lock (wait status $r)\n" if $r;
908 sub uninstall1 ($$) {
909 my ($mf, $enoentok) = @_;
910 my $unnailed = "$mf.unnailed";
911 rename $unnailed, $mf or ($enoentok && $!==ENOENT)
912 or die "$self: failed to restore: rename $unnailed back to $mf: $!\n";
915 sub unaltcargolock ($) {
917 return unless $cleanup_cargo_lock;
918 die 'internal error!' unless $do_cargo_lock && defined $alt_cargo_lock;
920 # we ignore $enoentok because we don't know if one was supposed to
923 rename('Cargo.lock', $alt_cargo_lock) or $!==ENOENT or die
924 "$self: cleanup: rename possibly-updated \`Cargo.lock' to \`$alt_cargo_lock': $!\n";
926 unlink 'Cargo.lock' or $!==ENOENT or die
927 "$self: cleanup: remove \`Cargo.lock' in favour of \`$alt_cargo_lock': $!\n";
928 # ^ this also helps clean up the stupid rename() corner case
932 foreach my $mf (keys %manifests) {
933 my $nailed = nailed($mf);
934 link $mf, $nailed or die "$self: preserve (link) $mf as $nailed: $!\n";
943 # Loop exit condition:
946 # $is_cargo==1 <cargo-command> <cargo-opts> [--] <subcmd>...
947 # $is_cargo==0 <build-command>...
951 die "$self: need cargo subcommand\n"
952 unless $noact || $just_linkfarm;;
953 push @ARGV, "CARGO-SUBCOMMAND"; # dummy, user may see it
959 my $not_a_nailing_opt = sub { # usage 1
960 unshift @ARGV, $orgopt;
961 unshift @ARGV, 'cargo';
963 no warnings qw(exiting);
966 $not_a_nailing_opt->() unless m{^-};
967 $not_a_nailing_opt->() if $_ eq '--';
969 if ($_ eq '---') { # usage 2 or 3
971 die "$self: --- must be followed by build command\n" unless $noact;
972 push @ARGV, 'BUILD-COMMAND';
974 if ($ARGV[0] eq '--') { # usage 3
977 } elsif (grep { $_ eq '--' } @ARGV) { # usage 2
979 } elsif ($ARGV[0] =~ m{[^/]*cargo[^/]*$}) { # usage 2
990 } elsif (s{^-v}{-}) {
992 } elsif (s{^-q}{-}) {
994 } elsif (s{^-n}{-}) {
996 } elsif (s{^-s(.+)}{-}s) {
998 } elsif (s{^-([uU])}{-}) {
999 $cargo_lock_update = $1=~m/[a-z]/;
1000 } elsif (s{^-([cC])}{-}) {
1001 $pass_options = $1=~m/[a-z]/;
1002 } elsif (s{^-D}{-}) {
1004 } elsif (s{^-E}{-}) {
1005 $linkfarm_depth = 'copy-edit';
1006 } elsif (s{^-T(.+)}{-}s) {
1008 } elsif (s{^-([oO])}{-}) {
1009 $online = $1=~m/[a-z]/;
1011 die "$self: unknown short option(s) $_\n" unless $_ eq $orgopt;
1012 $not_a_nailing_opt->();
1015 } elsif (s{^--help$}{}) {
1017 } elsif (s{^--(?:doc|man|manual)?$}{}) {
1019 } elsif (s{^--target=}{}) {
1021 } elsif (m{^--(on|off)line$}) {
1022 $online = $1 eq 'on';
1023 } elsif (m{^--just-linkfarm(?:=(shallow|git|full))?$}) {
1025 $linkfarm_depth = $1 if $1;
1026 $cargo_lock_update= 1; # will set $linkfarm_detph to 1 by default
1027 } elsif (m{^--linkfarm(?:=(no|shallow|git|full))?$}) {
1028 $linkfarm_depth = $1 || 'git';
1029 } elsif (m{^--edits?-sources?$}) {
1030 $linkfarm_depth = 'copy-edit';
1031 } elsif (m{^--just-run$}) {
1032 $do_nail = $do_cargo_lock = $do_lock = 0;
1033 } elsif (m{^--(clean|keep)-linkfarm$}) {
1034 $oot_clean = $1 eq 'clean';
1035 } elsif (m{^--(no)?-nail$}) {
1037 } elsif (m{^--(no)?-cargo-lock-manip$}) {
1038 $do_cargo_lock = !$1;
1039 } elsif (m{^--(no)?-concurrency-lock$}) {
1041 } elsif (m{^--leave-nailed$}) {
1043 } elsif (s{^--subcommand-props=}{}) {
1044 my @props = split /\,/, $_;
1045 our %subcmd_prop_ok;
1046 if (!%subcmd_prop_ok) {
1047 foreach my $v (\@subcmd_xprops, values %subcmd_props) {
1048 $subcmd_prop_ok{$_}=1 foreach @$v;
1052 or die "$self: unknown subcommand property \`$_'\n"
1054 $cargo_subcmd = \@props;
1055 } elsif (m{^--(no-)?cargo-lock-update}) {
1056 $cargo_lock_update= !!$1;
1058 $not_a_nailing_opt->();
1066 @args_preface = shift @ARGV;
1067 while (defined($_ = shift @ARGV)) {
1068 if (!m{^-}) { unshift @ARGV, $_; last; }
1069 if ($_ eq '--') { last; }
1070 push @args_preface, $_;
1072 @ARGV || die "$self: need cargo subcommand\n";
1073 $cargo_subcmd //= $ARGV[0];
1074 $pass_options //= 1;
1076 $cargo_subcmd //= '';
1077 $pass_options //= 0;
1079 push @args_preface, shift @ARGV;
1081 if (!ref($cargo_subcmd)) {
1082 print STDERR " cargo_subcmd lookup $cargo_subcmd\n" if $dump;
1083 $cargo_subcmd = $subcmd_props{$cargo_subcmd} // [ ];
1086 print STDERR " cargo_subcmd props @$cargo_subcmd\n" if $dump;
1088 $cargo_subcmd{$_} = 1 foreach @$cargo_subcmd;
1089 $cargo_subcmd = \%cargo_subcmd;
1096 consider_alt_cargo_lock();
1101 consider_directories();
1102 our @display_cmd = @ARGV;
1103 oot_massage_cmdline();
1109 print STDERR Dumper(\%manifests) if $dump>=2;
1110 print STDERR Dumper(\%packagemap, \@ARGV,
1111 { src_absdir => $src_absdir,
1112 worksphere => $worksphere,
1114 oot_dir => $oot_dir,
1115 oot_absdir => $oot_absdir,
1116 build_absdir => $build_absdir });
1122 $want_uninstall = !$leave_nailed;
1126 printf STDERR "$self: nailed (%s manifests, %s packages)%s\n",
1127 (scalar keys %manifests), (scalar keys %packagemap),
1128 (defined($alt_cargo_lock) and ", using `$alt_cargo_lock'")
1129 if $verbose && $do_nail;
1131 print STDERR "$self: invoking: @display_cmd\n" if $verbose;
1132 my $estatus = invoke();
1134 files_return_after_update();
1136 uninstall() unless $leave_nailed;
1137 $want_uninstall = 0;
1139 print STDERR "$self: ".($do_nail ? "unnailed" : "finished")
1140 .". status $estatus.\n" if $verbose;