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 is keep)
31 --[no-]preclean-linkfarm[=no|src|full] (default is no)
32 --just-run Run the command, don't do cargo stuff
33 --no-nail Do not nail, just run the command.
34 --no-cargo-lock-manip Do not manipulate Cargo.lock.
35 --no-concurrency-lock Do not take the concurrency lock.
37 -s<subcommand> Treat command as `cargo <subcommand>`
38 --subcommand-props=<prop>,... Override command props (see docs)
46 use Types::Serialiser;
47 use File::Glob qw(bsd_glob GLOB_ERR GLOB_BRACE GLOB_NOMAGIC);
52 RPI => 'arm-unknown-linux-gnueabihf',
53 WASM => 'wasm32-unknown-unknown',
57 $self = $0; $self =~ s{^.*/(?=.)}{};
60 while ($deref =~ m{^/}) {
61 my $link = readlink $deref;
64 or die "$self: checking our script location $deref: $!\n";
65 $deref =~ s{/[^/]+$}{}
66 or die "$self: unexpected script path: $deref\n";
68 unshift @INC, $deref."/TOML-Tiny/lib";
71 last if $link !~ m{^/};
76 use Fcntl qw(LOCK_EX);
78 use TOML::Tiny::Faithful;
80 our $src_absdir = getcwd() // die "$self: getcwd failed: $!\n";
82 our $worksphere = $src_absdir;
83 $worksphere =~ s{/([^/]+)$}{}
84 or die "$self: cwd \`$worksphere' unsupported!\n";
85 our $subdir = $1; # leafname
87 our $lockfile = "../.nailing-cargo.lock";
91 our $command_is_cargo;
93 our $cargo_lock_update;
101 our $do_cargo_lock=1;
106 our %subcmd_props = (
107 # build (default) =>[qw( )],
108 'generate-lockfile'=>[qw( lock-update !target !target-dir )],
109 update =>[qw( lock-update !target online )],
110 fetch =>[qw( online !target-dir )],
111 fmt =>[qw( !locked !target !offline !target-dir edits )],
112 miri =>[qw( !locked !offline linkfarm-shallow )],
113 upgrades =>[qw( !locked !target-dir )],
116 our @subcmd_xprops = qw(!manifest-path);
124 print $usage or die $!;
129 my $manual = ($base_path // '.').'/README.md';
130 stat $manual or die "$self: manual not found at $manual: $!\n";;
131 exec 'sh','-ec', 'pandoc -- "$1" 2>&1 | w3m -T text/html', '--', $manual;
132 die "$self: exec sh failed: $!";
135 sub read_or_enoent ($) {
137 if (!open R, '<', $fn) {
138 return undef if $!==ENOENT;
139 die "$self: open $fn: $!\n";
142 my ($r) = <R> // die "$self: read $fn: $!\n";
146 sub stat_exists ($$) {
147 my ($fn, $what) = @_;
148 if (stat $fn) { return 1; }
149 $!==ENOENT or die "$self: stat $what: $fn: $!\n";
154 print STDERR " subcmd_p ".(join ' ', keys %$cargo_subcmd)." | @_\n"
156 $cargo_subcmd->{$_[0]}
159 sub toml_or_enoent ($$) {
161 my $toml = read_or_enoent($f) // return;
162 print STDERR "Read TOML from $f\n" if $dump;
163 my ($v,$e) = from_toml($toml);
166 die "$self: parse TOML: $what: $f: $e\n";
168 die "$e ?" if length $e;
172 sub load1config ($) {
174 my $toml = toml_or_enoent($f, "config file");
175 push @configs, $toml if defined $toml;
179 my $dotfile = ".nailing-cargo.toml";
180 load1config("../Nailing-Cargo.toml");
181 load1config($dotfile);
182 load1config("$ENV{HOME}/$dotfile") if defined $ENV{HOME};
183 load1config("/etc/nailing-cargo/cfg.toml");
186 sub unlink_or_enoent ($) { unlink $_[0] or $!==ENOENT; }
190 "@$x[0..5]" eq "@$y[0..5]";
194 return unless $do_lock;
197 open LOCK, ">", $lockfile or die "$self: open/create $lockfile: $!\n";
198 flock LOCK, LOCK_EX or die "$self: lock $lockfile: $!\n";
199 my @fstat = stat LOCK or die "$self: fstat: $!\n";
200 my @stat = stat $lockfile;
202 next if $! == ENOENT;
203 die "$self: stat $lockfile: $!\n";
205 last if same_file(\@fstat,\@stat);
209 unlink $lockfile or die "$self: removing lockfile: $!\n";
217 die "$self: config key \`@_': $m\n";
221 foreach my $cfg (@configs) {
224 last unless defined $v;
225 ref($v) eq 'HASH' or badcfg @_, "parent key \`$k' is not a hash";
228 return $v if defined $v;
236 my $got = ref($v) || 'scalar';
237 return $v if !defined($v) || $got eq $exp;
238 badcfg @_, "found \L$got\E, expected \L$exp\E";
239 # ^ toml doesn't make refs to scalars, so this is unambiguous
244 (cfge $exp, @_) // badcfg @_, "missing";
247 sub cfgs { cfge 'scalar', @_ }
248 sub cfgsn { cfgn 'scalar', @_ }
252 return $v if !defined($v) || Types::Serialiser::is_bool $v;
253 badcfg @_, "expected boolean";
257 my $l = cfge 'ARRAY', @_;
258 foreach my $x (@$l) {
259 !ref $x or badcfg @_, "list contains non-scalar element";
265 my $nailfile = "../Cargo.nail";
266 open N, '<', $nailfile or die "$self: open $nailfile: $!\n";
268 my $toml = <N> // die "$self: read $nailfile: $!";
270 if ($toml !~ m{^\s*\[/}m &&
271 $toml !~ m{^[^\n\#]*\=}m &&
272 # old non-toml syntax
273 $toml =~ s{^[ \t]*([-_0-9a-z]+)[ \t]+(\S+)[ \t]*$}{$1 = \"$2\"}mig) {
274 $toml =~ s{^}{[packages\]\n};
276 $toml =~ s{^[ \t]*\-[ \t]*\=[ \t]*(\"[-_0-9a-z]+\"\n?)$}{
279 $toml = "subdirs = [\n".(join '', map { "$_\n" } @sd)."]\n".$toml;
283 ($nail,$e) = from_toml($toml);
284 if (!defined $nail) {
287 print STDERR "$self: $nailfile transformed into TOML:\n$toml\n";
290 die "$self: parse $nailfile: $e\n";
292 die "$e ?" if length $e;
294 $nail->{subdirs} //= [ ];
296 if (!ref $nail->{subdirs}) {
299 map { s/^\s+//; s/\s+$//; $_; }
305 unshift @configs, $nail;
308 sub get_dependency_tables ($) {
310 my @keys = qw(dependencies build-dependencies dev-dependencies);
314 foreach my $k (@keys) {
315 my $deps = $node->{$k};
316 push @r, $deps if $deps;
320 foreach my $target_node (values %{ $toml->{target} // { } }) {
321 $process->($target_node);
326 our @alt_cargo_lock_stat;
328 sub consider_alt_cargo_lock () {
329 my @ck = qw(alt_cargo_lock);
330 # User should *either* have Cargo.lock in .gitignore,
331 # or expect to commit Cargo.lock.example ($alt_cargo_lock)
333 return unless $do_cargo_lock;
335 $alt_cargo_lock = (cfg_uc @ck);
338 if (defined($alt_cargo_lock) && ref($alt_cargo_lock) eq 'HASH') {
339 $force = cfg_bool qw(alt_cargo_lock force);
340 my @ck = qw(alt_cargo_lock file);
341 $alt_cargo_lock = cfg_uc @ck;
343 $alt_cargo_lock //= Types::Serialiser::true;
345 if (Types::Serialiser::is_bool $alt_cargo_lock) {
346 if (!$alt_cargo_lock) { $alt_cargo_lock = undef; return; }
347 $alt_cargo_lock = 'Cargo.lock.example';
350 if (ref($alt_cargo_lock) || $alt_cargo_lock =~ m{/}) {
351 badcfg @ck, "expected boolean, or leafname";
354 if (!stat_exists $alt_cargo_lock, "alt_cargo_lock") {
355 $alt_cargo_lock = undef unless $force;
359 @alt_cargo_lock_stat = stat _;
362 our $oot_dir; # oot.dir or "Build"
365 sub consider_oot () {
366 $oot_dir = cfgs qw(oot dir);
367 my $use = cfgs qw(oot use);
368 unless (defined($oot_dir) || defined($use) ||
369 defined(cfg_uc qw(oot user))) {
372 if (($use//'') eq 'disable') {
376 $oot_clean //= cfg_bool qw(oot clean);
377 $oot_dir //= 'Build';
378 $oot_absdir = ($oot_dir !~ m{^/} ? "$worksphere/" : ""). $oot_dir;
386 sub read_manifest ($$$) {
387 my ($subdir, $org_subdir, $why) = @_;
388 my $manifest = "../$subdir/Cargo.toml";
389 print STDERR "$self: reading $manifest...\n" if $verbose>=4;
390 if (defined $manifests{$manifest}) {
392 "$self: warning: $subdir: specified more than once!".
393 " (ignoring $why)\n";
396 foreach my $try ("$manifest.unnailed", "$manifest") {
397 my $toml = toml_or_enoent($try, "manifest, in $why") // next;
398 my $ws = $toml->{workspace};
400 queue_workspace_members($subdir, $org_subdir, $ws, "$subdir, $why");
402 my $p = $toml->{package}{name};
403 if (!defined $p and !defined $ws) {
405 "$self: warning: $subdir, $why: missing package.name in $try, ignoring\n";
408 $manifests{$manifest} = [ $toml, $org_subdir ] if $p;
409 foreach my $dep (get_dependency_tables $toml) {
410 next unless defined $dep->{path};
411 queue_referenced_path($dep->{path}, $org_subdir,
412 "dependency of $subdir, $why");
419 sub queue_workspace_members ($$) {
420 my ($subdir, $org_subdir, $ws_toml, $what) = @_;
421 # We need to (more or less) reimplement the cargo workspace
422 # membership algorithm (see the "workspaces" section of the cargo
423 # reference). How tiresome.
425 # It's not quite the same for us because we aren't interested in
426 # whether cargo thinks things are "in the workspace". But we do
427 # need to do the automatic discover.
429 my @include = @{ $ws_toml->{members} // [ ] };
430 my $exclude = $ws_toml->{exclude} // [ ];
433 s/[^*?0-9a-zA-Z_]/\\$&/g;
438 foreach my $spec (@include) {
439 if ($spec =~ m{^/}) {
441 "$self: warning: absolute workspace member $spec in $what (not nailing, but cargo will probably use it)\n";
444 my $spec_glob = "../$subdir/$spec";
445 my $globflags = GLOB_ERR|GLOB_BRACE|GLOB_NOMAGIC;
446 foreach my $globent (bsd_glob($spec_glob, $globflags)) {
447 next if grep { $globent =~ m{^$_$} } @exclude;
448 queue_referenced_path($globent, $org_subdir,
449 "member of workspace $what");
454 sub queue_referenced_path ($$$) {
455 my ($spec_path, $org_subdir, $why) = @_;
457 qw(realpath), "--relative-to=../$org_subdir", "--", $spec_path
458 or die "$self: fork/pipe/exec for realpath(1)\n";
459 my $rel_path = do { local $/=undef; <REALPATH>; };
461 my $r = close(REALPATH);
462 die "$self: reap realpath: $!\n" if $!;
463 if (!chomp($rel_path) or $?) {
465 "$self: warning: failed to determine realpath for $spec_path in $org_subdir (exit code $?)\n";
468 if ($rel_path =~ m{^\.\./} or $rel_path eq '..') {
470 "$self: warning: $spec_path ($why) points outside $org_subdir, not following so not nailing (although cargo probably will follow it)\n";
474 my $q_subdir = "$org_subdir/$rel_path";
475 print STDERR "$self: making a note to look at $q_subdir, $why)\n"
478 push @queued_paths, [ "$q_subdir", $org_subdir, $why ];
482 # We (and our callees) populate %packagemap and %manifest, so if we
483 # don't run, they remain empty and nothing is nailed.
484 return unless $do_nail;
486 foreach my $p (keys %{ $nail->{packages} }) {
487 my $v = $nail->{packages}{$p};
488 my $subdir = ref($v) ? $v->{subdir} : $v;
489 my ($gotpackage, $ws) = read_manifest($subdir, $subdir, "from [packages]");
490 $gotpackage //= '<nothing!>';
491 if ($gotpackage ne $p) {
493 "$self: warning: honouring Cargo.nail packages.$subdir=$p even though $subdir contains package $gotpackage!\n";
495 die if defined $packagemap{$p};
496 $packagemap{$p} = [ $subdir, $subdir ];
498 foreach my $subdir (@{ $nail->{subdirs} }) {
499 my ($gotpackage,$ws) = read_manifest($subdir, $subdir, "from [subdirs]");
500 if (!defined $gotpackage) {
502 "$self: warning: ignoring subdir $subdir which has no (suitable) Cargo.toml\n"
506 $packagemap{$gotpackage} //= [ $subdir, $subdir ];
508 while (my ($subdir, $org_subdir, $why) = @{ shift @queued_paths or [] }) {
509 next if $manifests{"../$subdir/Cargo.toml"};
510 my ($gotpackage, $ws) = read_manifest($subdir, $org_subdir, $why);
511 next unless $gotpackage;
512 $packagemap{$gotpackage} //= [ $subdir, $org_subdir ];
517 foreach my $p (sort keys %packagemap) {
518 print STDERR "$self: package $p in $packagemap{$p}[0]\n" if $verbose>=2;
520 foreach my $mf (keys %manifests) {
521 die "internal error" unless $do_nail; # belt and braces
523 my ($toml, $mf_org_subdir) = @{ $manifests{$mf} };
524 foreach my $deps (get_dependency_tables $toml) {
526 foreach my $p (keys %packagemap) {
527 my $info = $deps->{$p};
528 next unless defined $info;
529 next if $packagemap{$p}[1] eq $mf_org_subdir;
530 $deps->{$p} = $info = { } unless ref $info;
531 my $oldpath = $info->{path};
532 delete $info->{version};
533 my $newpath = $worksphere.'/'.$packagemap{$p}[0];
534 print STDERR "in $mf set $p path=$newpath (was ".
535 ($oldpath // '<unset>').")\n"
537 $info->{path} = $newpath;
539 delete $info->{branch};
542 my $nailing = "$mf.nailing~";
543 unlink_or_enoent $nailing or die "$self: remove old $nailing: $!\n";
544 open N, '>', $nailing or die "$self: create new $nailing: $!\n";
545 print N to_toml($toml) or die "$self: write new $nailing: $!\n";
546 close N or die "$self: close new $nailing: $!\n";
551 if ($just_linkfarm) {
552 die "$self: --just-linkfarm but not doing out-of-tree builds!\n"
553 unless defined $oot_dir;
558 if (!defined $online) {
559 $_ = cfg_uc qw(misc online);
561 } elsif (Types::Serialiser::is_bool $_) {
566 } elsif (m/^[1ty]/) { # allow booleanish strings
567 $online = 1; # for less user frustration
568 } elsif (m/^[0fn]/) {
571 badcfg qw(misc online), "expected boolean or 'auto', found '$_'";
574 $online //= 1 if subcmd_p('online');
577 $cargo_lock_update //= subcmd_p('lock-update');
579 subcmd_p('linkfarm-shallow') ? 'shallow' :
580 $cargo_lock_update ? 'shallow' :
583 $oot_preclean //= 'no';
587 if (!$cargo_lock_update) {
588 push @add, qw(--locked) unless subcmd_p('!locked');
590 if ($linkfarm_depth eq '') {
591 if (defined($oot_dir) && !subcmd_p('!manifest-path')) {
592 my $cargotoml = "${src_absdir}/Cargo.toml";
593 push @args_preface, "--manifest-path=$cargotoml" if $pass_options;
594 push @add, qw(--target-dir=target) unless subcmd_p('!target-dir');
598 if (defined($target) && !subcmd_p('!target')) {
599 if ($target =~ m{^[A-Z]}) {
600 $target = (cfgs 'arch', $target) // $archmap{$target}
601 // die "$self: --target=$target alias specified; not in cfg or map\n";
603 push @add, "--target=$target";
606 push @add, "--offline" unless $online || subcmd_p('!offline');
608 if (subcmd_p('edits') && $linkfarm_depth ne 'copy-edit') {
610 "$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";
613 push @args_preface, @add if $pass_options;
614 die if grep { m/ / } @add;
615 $ENV{NAILINGCARGO_CARGO_OPTIONS} = "@add";
617 unshift @ARGV, @args_preface;
620 our $build_absdir; # .../Build/<subdir>
622 sub oot_massage_cmdline () {
623 return unless defined $oot_dir;
625 my $use = cfgs qw(oot use);
626 $use // die "$self: out-of-tree build, but \`oot.use' not configured\n";
627 $build_absdir = "$oot_absdir/$subdir";
629 my ($pre,$post) = ('','');
631 if ($linkfarm_depth eq '') {
632 push @xargs, $build_absdir;
633 ($pre, $post) = ('cd "$1"; shift; ', '');
635 push @xargs, $oot_absdir, $subdir, $src_absdir;
637 bld="$1"; shift; sd="$1"; shift; src="$1"; shift;
638 cd "$bld"; mkdir -p -- "$sd"; cd "$sd";
640 if ($oot_preclean ne 'no') {
641 $pre.= "find . -maxdepth 1 ! -path .";
642 $pre.= " ! -path ./target" if $oot_preclean ne 'full';
643 $pre.= " -print0 | xargs -0r rm -r --;"
645 if ($linkfarm_depth eq 'shallow') {
646 $pre.= <<'END_SHALLOW';
647 clean () { find -lname "$src/*" -print0 | xargs -0r rm --; }; clean;
648 find "$src" -maxdepth 1 \! -name Cargo.lock -print0 |
649 xargs -0r sh -ec 'for f in "$@"; do
654 } elsif ($linkfarm_depth =~ /full|git/) {
655 $pre .= <<'END_EITHER_DEEP_DIRS';
656 clean () { find -follow -lname "$src/*" -print0 | xargs -0r rm --; };
657 (set -e; cd "$src"; find . \! -name Cargo.lock \! \( -name .git -prune \) \! -path . \! -name .git -type d -print0) |
658 xargs -0r sh -ec 'for f in "$@"; do
659 rm -f "$f" 2>/dev/null ||:;
663 if ($linkfarm_depth eq 'git') {
664 $pre .= <<'END_FILES_GIT'
665 (set -e; cd "$src"; git ls-files --exclude-standard -co -z) |
667 } elsif ($linkfarm_depth eq 'full') {
668 $pre .= <<'END_FILES_FULL'
669 (set -e; cd "$src"; find . \! -name Cargo.lock \! \( -name .git -prune \) \! -type d -print0) |
672 $pre .= <<'END_DEEP';
674 BEGIN { $src=shift @ARGV; }
675 next if (readlink "$_"//"") eq "$src/$_";
677 symlink "$src/$_", "$_" or die "$_ $!";
680 } elsif ($linkfarm_depth eq 'copy-edit') {
681 $pre .= <<'END_COPY_EDIT';
682 find -lname "$src/*" -print0 | xargs -0r rm --;
683 (set -e; cd "$src"; git ls-files -c -z |
684 cpio --quiet -p0m --no-preserve-owner -u --make-directories "$bld/$sd");
686 (set -e; cd "$src"; git ls-files -c -z) | xargs -0r rm -f --;
689 $post .= <<'END_COPY_EDIT_BUNDLE';
690 (set -e; cd "$src"; git ls-files -c -z) |
691 cpio -Hustar -o0 --quiet >"nailing-cargo-update.tar";
694 die "$linkfarm_depth ?";
696 $pre .= <<'ENDLK' if $do_cargo_lock;
697 if test -e Cargo.lock; then
699 cp -- "$src"/Cargo.lock .;
702 $post .= <<'ENDCLEAN' if $oot_clean && !$just_linkfarm;
706 my $addpath = (cfg_uc qw(oot path_add)) //
707 $use eq 'really' ? Types::Serialiser::true : Types::Serialiser::false;
709 !Types::Serialiser::is_bool $addpath ? $addpath :
710 $addpath ? '$HOME/.cargo/bin' :
712 if (defined $addpath) {
714 PATH=$addpath:\${PATH-/usr/local/bin:/bin:/usr/bin};
718 $pre =~ s/^\s+//mg; $pre =~ s/\s+/ /g;
719 $post =~ s/^\s+//mg; $post =~ s/\s+/ /g;
721 my $getuser = sub { cfgsn qw(oot user) };
723 my $xe = $verbose >= 2 ? 'xe' : 'e';
726 @command = (@_, 'sh',"-${xe}c",$pre.'exec "$@"','--',@xargs);
728 @command = (@_, 'sh',"-${xe}c",$pre.'"$@"; '.$post,'--',@xargs);
730 push @command, @ARGV;
732 my $command_sh = sub {
733 my $quoted = join ' ', map {
738 @command = @_, "set -${xe}; $pre $quoted; $post";
740 print STDERR "$self: out-of-tree, building in: \`$build_absdir'\n"
742 if ($use eq 'really') {
743 my $user = $getuser->();
744 my @pw = getpwnam $user or die "$self: oot.user \`$user' lookup failed\n";
745 my $homedir = $pw[7];
746 $sh_ec->('really','-u',$user,'env',"HOME=$homedir");
747 print STDERR "$self: using really to run as user \`$user'\n" if $verbose;
748 } elsif ($use eq 'ssh') {
749 my $user = $getuser->();
750 $user .= '@localhost' unless $user =~ m/\@/;
751 $command_sh->('ssh',$user);
752 print STDERR "$self: using ssh to run as \`$user'\n" if $verbose;
753 } elsif ($use eq 'command_args') {
754 my @c = cfgn_list qw(oot command);
756 print STDERR "$self: out-of-tree, adverbial command: @c\n" if $verbose;
757 } elsif ($use eq 'command_sh') {
758 my @c = cfgn_list qw(oot command);
760 print STDERR "$self: out-of-tree, ssh'ish command: @c\n" if $verbose;
761 } elsif ($use eq 'null') {
764 die "$self: oot.use mode $use not recognised\n";
771 $ENV{CARGO_MANIFEST_DIR} = $src_absdir unless $linkfarm_depth;
772 $ENV{NAILINGCARGO_MANIFEST_DIR} = $src_absdir;
773 $ENV{NAILINGCARGO_WORKSPHERE} = $worksphere;
774 $ENV{NAILINGCARGO_BUILDSPHERE} = $oot_absdir;
775 delete $ENV{NAILINGCARGO_BUILDSPHERE} unless $oot_absdir;
776 $ENV{NAILINGCARGO_BUILD_DIR} = $build_absdir // $src_absdir;
782 if ($want_uninstall) {
784 foreach my $mf (keys %manifests) {
785 eval { uninstall1($mf,1); 1; } or warn "$@";
787 eval { unaltcargolock(1); 1; } or warn "$@";
791 sub consider_directories () {
792 return unless defined $oot_dir;
793 my $bsubdir = "../$oot_dir/$subdir";
794 return if stat $bsubdir;
795 die "$0: build directory $bsubdir inaccessible\n"
797 return if $cargo_lock_update; # will make it
798 die "$0: build directory $bsubdir does not exist, and not in Cargo.lock update mode!\n";
801 our $cleanup_cargo_lock;
803 foreach my $mf (keys %manifests) {
804 link "$mf", "$mf.unnailed" or $!==EEXIST
805 or die "$self: make backup link $mf.unnailed: $!\n";
808 if (defined($alt_cargo_lock)) {
809 die 'internal error' unless $do_cargo_lock;
810 if (@alt_cargo_lock_stat) {
811 print STDERR "$self: using alt_cargo_lock `$alt_cargo_lock'..."
813 if (link $alt_cargo_lock, 'Cargo.lock') {
814 print STDERR " linked\n" if $verbose>=3;
815 } elsif ($! != EEXIST) {
816 print STDERR "\n" if $verbose>=3;
817 die "$self: make \`Cargo.lock' available as \`$alt_cargo_lock': $!\n";
819 print STDERR "checking quality." if $verbose>=3;
820 my @lock_stat = stat 'Cargo.lock'
821 or die "$self: stat Cargo.lock (for alt check: $!\n";
822 same_file(\@alt_cargo_lock_stat, \@lock_stat)
824 "$self: \`Cargo.lock' and alt file \`$alt_cargo_lock' both exist and are not the same file!\n";
826 $cleanup_cargo_lock = 1;
828 $cleanup_cargo_lock = 1;
829 # If Cargo.lock exists and alt doesn't, that means either
830 # that a previous run was interrupted, or that the user has
838 my $nailed = "$mf.nailed~"; $nailed =~ s{/([^/]+)$}{/.$1} or die;
843 my @our_unfound_stab = stat_exists('Cargo.toml', 'local Cargo.toml')
845 foreach my $mf (keys %manifests) {
846 if (@our_unfound_stab) {
847 if (stat_exists $mf, "manifest in to-be-nailed directory") {
848 my @mf_stab = stat _ ;
849 if ("@mf_stab[0..1]" eq "@our_unfound_stab[0..1]") {
850 @our_unfound_stab = ();
855 my $nailing = "$mf.nailing~";
856 my $nailed = nailed($mf);
859 if (open NN, '<', $nailed) {
860 $diff = compare($nailing, \*NN);
861 die "$self: compare $nailing and $nailed: $!" if $diff<0;
863 $!==ENOENT or die "$self: check previous $nailed: $!\n";
873 rename $use, $mf or die "$self: install nailed $use: $!\n";
874 unlink_or_enoent $rm or die "$self: remove old $rm: $!\n";
875 print STDERR "$self: nailed $mf\n" if $verbose>=3;
878 if (@our_unfound_stab && $do_nail) {
880 "$self: *WARNING* cwd is not in Cargo.nail thbough it has Cargo.toml!\n";
885 my $r = system @ARGV;
889 print STDERR "$self: could not execute $ARGV[0]: $!\n";
891 } elsif ($r & 0xff00) {
892 print STDERR "$self: $ARGV[0] failed (exit status $r)\n";
895 print STDERR "$self: $ARGV[0] died due to signal! (wait status $r)\n";
900 sub files_return_after_update () {
901 if ($linkfarm_depth eq 'copy-edit') {
902 system qw(sh -ec), <<'END', 'x', "$build_absdir";
903 git ls-files -c -z | \
904 tar -x --keep-newer-files --no-same-permissions --no-same-owner \
905 --no-acls --no-selinux --no-xattrs --warning=no-ignore-newer \
906 -Hustar --null --files-from=- --force-local \
907 -f "$1/nailing-cargo-update.tar"
909 } elsif ($do_cargo_lock && $cargo_lock_update && !$just_linkfarm) {
910 # avoids importing File::Copy and the error handling is about as good
912 my $r= system qw(cp --), "$build_absdir/Cargo.lock", "Cargo.lock";
913 die "$self: run cp: $! $?" if $r<0 || $r & 0xff;
914 die "$self: failed to update local Cargo.lock (wait status $r)\n" if $r;
918 sub uninstall1 ($$) {
919 my ($mf, $enoentok) = @_;
920 my $unnailed = "$mf.unnailed";
921 rename $unnailed, $mf or ($enoentok && $!==ENOENT)
922 or die "$self: failed to restore: rename $unnailed back to $mf: $!\n";
925 sub unaltcargolock ($) {
927 return unless $cleanup_cargo_lock;
928 die 'internal error!' unless $do_cargo_lock && defined $alt_cargo_lock;
930 # we ignore $enoentok because we don't know if one was supposed to
933 rename('Cargo.lock', $alt_cargo_lock) or $!==ENOENT or die
934 "$self: cleanup: rename possibly-updated \`Cargo.lock' to \`$alt_cargo_lock': $!\n";
936 unlink 'Cargo.lock' or $!==ENOENT or die
937 "$self: cleanup: remove \`Cargo.lock' in favour of \`$alt_cargo_lock': $!\n";
938 # ^ this also helps clean up the stupid rename() corner case
942 foreach my $mf (keys %manifests) {
943 my $nailed = nailed($mf);
944 link $mf, $nailed or die "$self: preserve (link) $mf as $nailed: $!\n";
953 # Loop exit condition:
956 # $is_cargo==1 <cargo-command> <cargo-opts> [--] <subcmd>...
957 # $is_cargo==0 <build-command>...
961 die "$self: need cargo subcommand\n"
962 unless $noact || $just_linkfarm;;
963 push @ARGV, "CARGO-SUBCOMMAND"; # dummy, user may see it
969 my $not_a_nailing_opt = sub { # usage 1
970 unshift @ARGV, $orgopt;
971 unshift @ARGV, 'cargo';
973 no warnings qw(exiting);
976 $not_a_nailing_opt->() unless m{^-};
977 $not_a_nailing_opt->() if $_ eq '--';
979 if ($_ eq '---') { # usage 2 or 3
981 die "$self: --- must be followed by build command\n" unless $noact;
982 push @ARGV, 'BUILD-COMMAND';
984 if ($ARGV[0] eq '--') { # usage 3
987 } elsif (grep { $_ eq '--' } @ARGV) { # usage 2
989 } elsif ($ARGV[0] =~ m{[^/]*cargo[^/]*$}) { # usage 2
1000 } elsif (s{^-v}{-}) {
1002 } elsif (s{^-q}{-}) {
1004 } elsif (s{^-n}{-}) {
1006 } elsif (s{^-s(.+)}{-}s) {
1008 } elsif (s{^-([uU])}{-}) {
1009 $cargo_lock_update = $1=~m/[a-z]/;
1010 } elsif (s{^-([cC])}{-}) {
1011 $pass_options = $1=~m/[a-z]/;
1012 } elsif (s{^-D}{-}) {
1014 } elsif (s{^-E}{-}) {
1015 $linkfarm_depth = 'copy-edit';
1016 } elsif (s{^-T(.+)}{-}s) {
1018 } elsif (s{^-([oO])}{-}) {
1019 $online = $1=~m/[a-z]/;
1021 die "$self: unknown short option(s) $_\n" unless $_ eq $orgopt;
1022 $not_a_nailing_opt->();
1025 } elsif (s{^--help$}{}) {
1027 } elsif (s{^--(?:doc|man|manual)?$}{}) {
1029 } elsif (s{^--target=}{}) {
1031 } elsif (m{^--(on|off)line$}) {
1032 $online = $1 eq 'on';
1033 } elsif (m{^--just-linkfarm(?:=(shallow|git|full))?$}) {
1035 $linkfarm_depth = $1 if $1;
1036 $cargo_lock_update= 1; # will set $linkfarm_detph to 1 by default
1037 } elsif (m{^--linkfarm(?:=(no|shallow|git|full))?$}) {
1038 $linkfarm_depth = $1 || 'git';
1039 } elsif (m{^--edits?-sources?$}) {
1040 $linkfarm_depth = 'copy-edit';
1041 } elsif (m{^--just-run$}) {
1042 $do_nail = $do_cargo_lock = $do_lock = 0;
1043 } elsif (m{^--(clean|keep)-linkfarm$}) {
1044 $oot_clean = $1 eq 'clean';
1045 } elsif (m{^--(no-)?preclean-linkfarm$}) {
1046 $oot_preclean = $1 ? 'no' : 'src';
1047 } elsif (m{^--preclean-linkfarm=(no|src|full)$}) {
1049 } elsif (m{^--(no-)?nail$}) {
1051 } elsif (m{^--(no-)?cargo-lock-manip$}) {
1052 $do_cargo_lock = !$1;
1053 } elsif (m{^--(no-)?concurrency-lock$}) {
1055 } elsif (m{^--leave-nailed$}) {
1057 } elsif (s{^--subcommand-props=}{}) {
1058 my @props = split /\,/, $_;
1059 our %subcmd_prop_ok;
1060 if (!%subcmd_prop_ok) {
1061 foreach my $v (\@subcmd_xprops, values %subcmd_props) {
1062 $subcmd_prop_ok{$_}=1 foreach @$v;
1066 or die "$self: unknown subcommand property \`$_'\n"
1068 $cargo_subcmd = \@props;
1069 } elsif (m{^--(no-)?cargo-lock-update}) {
1070 $cargo_lock_update= !!$1;
1072 $not_a_nailing_opt->();
1080 @args_preface = shift @ARGV;
1081 while (defined($_ = shift @ARGV)) {
1082 if (!m{^-|^\+}) { unshift @ARGV, $_; last; }
1083 if ($_ eq '--') { last; }
1084 push @args_preface, $_;
1086 @ARGV || die "$self: need cargo subcommand\n";
1087 $cargo_subcmd //= $ARGV[0];
1088 $pass_options //= 1;
1090 $cargo_subcmd //= '';
1091 $pass_options //= 0;
1093 push @args_preface, shift @ARGV;
1095 if (!ref($cargo_subcmd)) {
1096 print STDERR " cargo_subcmd lookup $cargo_subcmd\n" if $dump;
1097 $cargo_subcmd = $subcmd_props{$cargo_subcmd} // [ ];
1100 print STDERR " cargo_subcmd props @$cargo_subcmd\n" if $dump;
1102 $cargo_subcmd{$_} = 1 foreach @$cargo_subcmd;
1103 $cargo_subcmd = \%cargo_subcmd;
1110 consider_alt_cargo_lock();
1115 consider_directories();
1116 our @display_cmd = @ARGV;
1117 oot_massage_cmdline();
1123 print STDERR Dumper(\%manifests) if $dump>=2;
1124 print STDERR Dumper(\%packagemap, \@ARGV,
1125 { src_absdir => $src_absdir,
1126 worksphere => $worksphere,
1128 oot_dir => $oot_dir,
1129 oot_absdir => $oot_absdir,
1130 build_absdir => $build_absdir });
1136 $want_uninstall = !$leave_nailed;
1140 printf STDERR "$self: nailed (%s manifests, %s packages)%s\n",
1141 (scalar keys %manifests), (scalar keys %packagemap),
1142 (defined($alt_cargo_lock) and ", using `$alt_cargo_lock'")
1143 if $verbose && $do_nail;
1145 print STDERR "$self: invoking: @display_cmd\n" if $verbose;
1146 my $estatus = invoke();
1148 files_return_after_update();
1150 uninstall() unless $leave_nailed;
1151 $want_uninstall = 0;
1153 print STDERR "$self: ".($do_nail ? "unnailed" : "finished")
1154 .". status $estatus.\n" if $verbose;