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 -E | --edits-sources Allow source edits
31 --linkfarm[=no|shallow|git|full] (default varies, usually "no")
32 --just-linkfarm | --clean-linkfarm | --keep-linkfarm (default is keep)
33 --[no-]preclean-build[=no|src|full] (default is no)
34 --just-run Run the command, don't do cargo stuff
35 --no-nail Do not nail, just run the command.
36 --no-cargo-lock-manip Do not manipulate Cargo.lock.
37 --no-concurrency-lock Do not take the concurrency lock.
39 -s<subcommand> Treat command as `cargo <subcommand>`
40 --subcommand-props=<prop>,... Override command props (see docs)
48 use Types::Serialiser;
49 use File::Glob qw(bsd_glob GLOB_ERR GLOB_BRACE GLOB_NOMAGIC);
54 RPI => 'arm-unknown-linux-gnueabihf',
55 WASM => 'wasm32-unknown-unknown',
59 $self = $0; $self =~ s{^.*/(?=.)}{};
62 while ($deref =~ m{^/}) {
63 my $link = readlink $deref;
66 or die "$self: checking our script location $deref: $!\n";
67 $deref =~ s{/[^/]+$}{}
68 or die "$self: unexpected script path: $deref\n";
70 unshift @INC, $deref."/TOML-Tiny/lib";
73 last if $link !~ m{^/};
78 use Fcntl qw(LOCK_EX);
80 use TOML::Tiny::Faithful;
82 our $src_absdir = getcwd() // die "$self: getcwd failed: $!\n";
84 our $worksphere = $src_absdir;
85 $worksphere =~ s{/([^/]+)$}{}
86 or die "$self: cwd \`$worksphere' unsupported!\n";
87 our $subdir = $1; # leafname
89 our $lockfile = "../.nailing-cargo.lock";
93 our $command_is_cargo;
95 our $cargo_lock_update;
103 our $do_cargo_lock=1;
108 our %subcmd_props = (
109 # build (default) =>[qw( )],
110 'generate-lockfile'=>[qw( lock-update !target !target-dir )],
111 update =>[qw( lock-update !target online )],
112 fetch =>[qw( online !target-dir )],
113 fmt =>[qw( !locked !target !offline !target-dir edits )],
114 miri =>[qw( !locked !offline linkfarm-shallow )],
115 publish =>[qw( !offline linkfarm-gitclean )],
116 upgrades =>[qw( !locked !target-dir )],
119 our @subcmd_xprops = qw(!manifest-path);
127 print $usage or die $!;
132 my $manual = ($base_path // '.').'/README.md';
133 stat $manual or die "$self: manual not found at $manual: $!\n";;
134 exec 'sh','-ec', 'pandoc -- "$1" 2>&1 | w3m -T text/html', '--', $manual;
135 die "$self: exec sh failed: $!";
138 sub read_or_enoent ($) {
140 if (!open R, '<', $fn) {
141 return undef if $!==ENOENT;
142 die "$self: open $fn: $!\n";
145 my ($r) = <R> // die "$self: read $fn: $!\n";
149 sub stat_exists ($$) {
150 my ($fn, $what) = @_;
151 if (stat $fn) { return 1; }
152 $!==ENOENT or die "$self: stat $what: $fn: $!\n";
157 print STDERR " subcmd_p ".(join ' ', keys %$cargo_subcmd)." | @_\n"
159 $cargo_subcmd->{$_[0]}
162 sub toml_or_enoent ($$) {
164 my $toml = read_or_enoent($f) // return;
165 print STDERR "Read TOML from $f\n" if $dump;
166 my ($v,$e) = from_toml($toml);
169 die "$self: parse TOML: $what: $f: $e\n";
171 die "$e ?" if length $e;
175 sub load1config ($) {
177 my $toml = toml_or_enoent($f, "config file");
178 push @configs, $toml if defined $toml;
182 my $dotfile = ".nailing-cargo.toml";
183 load1config("../Nailing-Cargo.toml");
184 load1config($dotfile);
185 load1config("$ENV{HOME}/$dotfile") if defined $ENV{HOME};
186 load1config("/etc/nailing-cargo/cfg.toml");
189 sub unlink_or_enoent ($) { unlink $_[0] or $!==ENOENT; }
193 "@$x[0..5]" eq "@$y[0..5]";
197 return unless $do_lock;
200 open LOCK, ">", $lockfile or die "$self: open/create $lockfile: $!\n";
201 flock LOCK, LOCK_EX or die "$self: lock $lockfile: $!\n";
202 my @fstat = stat LOCK or die "$self: fstat: $!\n";
203 my @stat = stat $lockfile;
205 next if $! == ENOENT;
206 die "$self: stat $lockfile: $!\n";
208 last if same_file(\@fstat,\@stat);
212 unlink $lockfile or die "$self: removing lockfile: $!\n";
220 die "$self: config key \`@_': $m\n";
224 foreach my $cfg (@configs) {
227 last unless defined $v;
228 ref($v) eq 'HASH' or badcfg @_, "parent key \`$k' is not a hash";
231 return $v if defined $v;
239 my $got = ref($v) || 'scalar';
240 return $v if !defined($v) || $got eq $exp;
241 badcfg @_, "found \L$got\E, expected \L$exp\E";
242 # ^ toml doesn't make refs to scalars, so this is unambiguous
247 (cfge $exp, @_) // badcfg @_, "missing";
250 sub cfgs { cfge 'scalar', @_ }
251 sub cfgsn { cfgn 'scalar', @_ }
255 return $v if !defined($v) || Types::Serialiser::is_bool $v;
256 badcfg @_, "expected boolean";
260 my $l = cfge 'ARRAY', @_;
261 foreach my $x (@$l) {
262 !ref $x or badcfg @_, "list contains non-scalar element";
268 my $nailfile = "../Cargo.nail";
269 open N, '<', $nailfile or die "$self: open $nailfile: $!\n";
271 my $toml = <N> // die "$self: read $nailfile: $!";
273 if ($toml !~ m{^\s*\[/}m &&
274 $toml !~ m{^[^\n\#]*\=}m &&
275 # old non-toml syntax
276 $toml =~ s{^[ \t]*([-_0-9a-z]+)[ \t]+(\S+)[ \t]*$}{$1 = \"$2\"}mig) {
277 $toml =~ s{^}{[packages\]\n};
279 $toml =~ s{^[ \t]*\-[ \t]*\=[ \t]*(\"[-_0-9a-z]+\"\n?)$}{
282 $toml = "subdirs = [\n".(join '', map { "$_\n" } @sd)."]\n".$toml;
286 ($nail,$e) = from_toml($toml);
287 if (!defined $nail) {
290 print STDERR "$self: $nailfile transformed into TOML:\n$toml\n";
293 die "$self: parse $nailfile: $e\n";
295 die "$e ?" if length $e;
297 $nail->{subdirs} //= [ ];
299 if (!ref $nail->{subdirs}) {
302 map { s/^\s+//; s/\s+$//; $_; }
308 unshift @configs, $nail;
311 sub get_dependency_tables ($) {
313 my @keys = qw(dependencies build-dependencies dev-dependencies);
317 foreach my $k (@keys) {
318 my $deps = $node->{$k};
319 push @r, $deps if $deps;
323 foreach my $target_node (values %{ $toml->{target} // { } }) {
324 $process->($target_node);
329 our @alt_cargo_lock_stat;
331 sub consider_alt_cargo_lock () {
332 my @ck = qw(alt_cargo_lock);
333 # User should *either* have Cargo.lock in .gitignore,
334 # or expect to commit Cargo.lock.example ($alt_cargo_lock)
336 return unless $do_cargo_lock;
338 $alt_cargo_lock = (cfg_uc @ck);
341 if (defined($alt_cargo_lock) && ref($alt_cargo_lock) eq 'HASH') {
342 $force = cfg_bool qw(alt_cargo_lock force);
343 my @ck = qw(alt_cargo_lock file);
344 $alt_cargo_lock = cfg_uc @ck;
346 $alt_cargo_lock //= Types::Serialiser::true;
348 if (Types::Serialiser::is_bool $alt_cargo_lock) {
349 if (!$alt_cargo_lock) { $alt_cargo_lock = undef; return; }
350 $alt_cargo_lock = 'Cargo.lock.example';
353 if (ref($alt_cargo_lock) || $alt_cargo_lock =~ m{/}) {
354 badcfg @ck, "expected boolean, or leafname";
357 if (!stat_exists $alt_cargo_lock, "alt_cargo_lock") {
358 $alt_cargo_lock = undef unless $force;
362 @alt_cargo_lock_stat = stat _;
365 our $oot_dir; # oot.dir or "Build"
368 sub consider_oot () {
369 $oot_dir = cfgs qw(oot dir);
370 my $use = cfgs qw(oot use);
371 unless (defined($oot_dir) || defined($use) ||
372 defined(cfg_uc qw(oot user))) {
375 if (($use//'') eq 'disable') {
379 $oot_clean //= cfg_bool qw(oot clean);
380 $oot_dir //= 'Build';
381 $oot_absdir = ($oot_dir !~ m{^/} ? "$worksphere/" : ""). $oot_dir;
389 sub read_manifest ($$$) {
390 my ($subdir, $org_subdir, $why) = @_;
391 my $manifest = "../$subdir/Cargo.toml";
392 print STDERR "$self: reading $manifest...\n" if $verbose>=4;
393 if (defined $manifests{$manifest}) {
395 "$self: warning: $subdir: specified more than once!".
396 " (ignoring $why)\n";
399 foreach my $try ("$manifest.unnailed", "$manifest") {
400 my $toml = toml_or_enoent($try, "manifest, in $why") // next;
401 my $ws = $toml->{workspace};
403 queue_workspace_members($subdir, $org_subdir, $ws, "$subdir, $why");
405 my $p = $toml->{package}{name};
406 if (!defined $p and !defined $ws) {
408 "$self: warning: $subdir, $why: missing package.name in $try, ignoring\n";
411 $manifests{$manifest} = [ $toml, $org_subdir ] if $p;
412 foreach my $dep (get_dependency_tables $toml) {
413 next unless defined $dep->{path};
414 queue_referenced_path($dep->{path}, $org_subdir,
415 "dependency of $subdir, $why");
422 sub queue_workspace_members ($$) {
423 my ($subdir, $org_subdir, $ws_toml, $what) = @_;
424 # We need to (more or less) reimplement the cargo workspace
425 # membership algorithm (see the "workspaces" section of the cargo
426 # reference). How tiresome.
428 # It's not quite the same for us because we aren't interested in
429 # whether cargo thinks things are "in the workspace". But we do
430 # need to do the automatic discover.
432 my @include = @{ $ws_toml->{members} // [ ] };
433 my $exclude = $ws_toml->{exclude} // [ ];
436 s/[^*?0-9a-zA-Z_]/\\$&/g;
441 foreach my $spec (@include) {
442 if ($spec =~ m{^/}) {
444 "$self: warning: absolute workspace member $spec in $what (not nailing, but cargo will probably use it)\n";
447 my $spec_glob = "../$subdir/$spec";
448 my $globflags = GLOB_ERR|GLOB_BRACE|GLOB_NOMAGIC;
449 foreach my $globent (bsd_glob($spec_glob, $globflags)) {
450 next if grep { $globent =~ m{^$_$} } @exclude;
451 queue_referenced_path($globent, $org_subdir,
452 "member of workspace $what");
457 sub queue_referenced_path ($$$) {
458 my ($spec_path, $org_subdir, $why) = @_;
460 qw(realpath), "--relative-to=../$org_subdir", "--", $spec_path
461 or die "$self: fork/pipe/exec for realpath(1)\n";
462 my $rel_path = do { local $/=undef; <REALPATH>; };
464 my $r = close(REALPATH);
465 die "$self: reap realpath: $!\n" if $!;
466 if (!chomp($rel_path) or $?) {
468 "$self: warning: failed to determine realpath for $spec_path in $org_subdir (exit code $?)\n";
471 if ($rel_path =~ m{^\.\./} or $rel_path eq '..') {
473 "$self: warning: $spec_path ($why) points outside $org_subdir, not following so not nailing (although cargo probably will follow it)\n";
477 my $q_subdir = "$org_subdir/$rel_path";
478 print STDERR "$self: making a note to look at $q_subdir, $why)\n"
481 push @queued_paths, [ "$q_subdir", $org_subdir, $why ];
485 # We (and our callees) populate %packagemap and %manifest, so if we
486 # don't run, they remain empty and nothing is nailed.
487 return unless $do_nail;
489 foreach my $p (keys %{ $nail->{packages} }) {
490 my $v = $nail->{packages}{$p};
491 my $subdir = ref($v) ? $v->{subdir} : $v;
492 my ($gotpackage, $ws) = read_manifest($subdir, $subdir, "from [packages]");
493 $gotpackage //= '<nothing!>';
494 if ($gotpackage ne $p) {
496 "$self: warning: honouring Cargo.nail packages.$subdir=$p even though $subdir contains package $gotpackage!\n";
498 die if defined $packagemap{$p};
499 $packagemap{$p} = [ $subdir, $subdir ];
501 foreach my $subdir (@{ $nail->{subdirs} }) {
502 my ($gotpackage,$ws) = read_manifest($subdir, $subdir, "from [subdirs]");
503 if (!defined $gotpackage) {
505 "$self: warning: ignoring subdir $subdir which has no (suitable) Cargo.toml\n"
509 $packagemap{$gotpackage} //= [ $subdir, $subdir ];
511 while (my ($subdir, $org_subdir, $why) = @{ shift @queued_paths or [] }) {
512 next if $manifests{"../$subdir/Cargo.toml"};
513 my ($gotpackage, $ws) = read_manifest($subdir, $org_subdir, $why);
514 next unless $gotpackage;
515 $packagemap{$gotpackage} //= [ $subdir, $org_subdir ];
520 foreach my $p (sort keys %packagemap) {
521 print STDERR "$self: package $p in $packagemap{$p}[0]\n" if $verbose>=2;
523 foreach my $mf (keys %manifests) {
524 die "internal error" unless $do_nail; # belt and braces
526 my ($toml, $mf_org_subdir) = @{ $manifests{$mf} };
527 foreach my $deps (get_dependency_tables $toml) {
529 foreach my $p (keys %packagemap) {
530 my $info = $deps->{$p};
531 next unless defined $info;
532 next if $packagemap{$p}[1] eq $mf_org_subdir;
533 $deps->{$p} = $info = { } unless ref $info;
534 my $oldpath = $info->{path};
535 delete $info->{version};
536 my $newpath = $worksphere.'/'.$packagemap{$p}[0];
537 print STDERR "in $mf set $p path=$newpath (was ".
538 ($oldpath // '<unset>').")\n"
540 $info->{path} = $newpath;
542 delete $info->{branch};
545 my $nailing = "$mf.nailing~";
546 unlink_or_enoent $nailing or die "$self: remove old $nailing: $!\n";
547 open N, '>', $nailing or die "$self: create new $nailing: $!\n";
548 print N to_toml($toml) or die "$self: write new $nailing: $!\n";
549 close N or die "$self: close new $nailing: $!\n";
554 if ($just_linkfarm) {
555 die "$self: --just-linkfarm but not doing out-of-tree builds!\n"
556 unless defined $oot_dir;
561 if (!defined $online) {
562 $_ = cfg_uc qw(misc online);
564 } elsif (Types::Serialiser::is_bool $_) {
569 } elsif (m/^[1ty]/) { # allow booleanish strings
570 $online = 1; # for less user frustration
571 } elsif (m/^[0fn]/) {
574 badcfg qw(misc online), "expected boolean or 'auto', found '$_'";
577 $online //= 1 if subcmd_p('online');
580 if (subcmd_p('linkfarm-gitclean')) {
581 $linkfarm_depth //= 'git';
582 $oot_preclean //= 'src';
585 $cargo_lock_update //= subcmd_p('lock-update');
587 subcmd_p('linkfarm-shallow') ? 'shallow' :
588 $cargo_lock_update ? 'shallow' :
591 $oot_preclean //= 'no';
595 if (!$cargo_lock_update) {
596 push @add, qw(--locked) unless subcmd_p('!locked');
598 if ($linkfarm_depth eq '') {
599 if (defined($oot_dir) && !subcmd_p('!manifest-path')) {
600 my $cargotoml = "${src_absdir}/Cargo.toml";
601 push @args_preface, "--manifest-path=$cargotoml" if $pass_options;
602 push @add, qw(--target-dir=target) unless subcmd_p('!target-dir');
606 if (defined($target) && !subcmd_p('!target')) {
607 if ($target =~ m{^[A-Z]}) {
608 $target = (cfgs 'arch', $target) // $archmap{$target}
609 // die "$self: --target=$target alias specified; not in cfg or map\n";
611 push @add, "--target=$target";
614 push @add, "--offline" unless $online || subcmd_p('!offline');
616 if (subcmd_p('edits') && $linkfarm_depth ne 'copy-edit') {
618 "$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";
621 push @args_preface, @add if $pass_options;
622 die if grep { m/ / } @add;
623 $ENV{NAILINGCARGO_CARGO_OPTIONS} = "@add";
625 unshift @ARGV, @args_preface;
628 our $build_absdir; # .../Build/<subdir>
630 sub oot_massage_cmdline () {
631 return unless defined $oot_dir;
633 my $use = cfgs qw(oot use);
634 $use // die "$self: out-of-tree build, but \`oot.use' not configured\n";
635 $build_absdir = "$oot_absdir/$subdir";
637 my ($pre,$post) = ('','');
639 if ($linkfarm_depth eq '') {
640 push @xargs, $build_absdir;
641 ($pre, $post) = ('cd "$1"; shift; ', '');
643 push @xargs, $oot_absdir, $subdir, $src_absdir;
645 bld="$1"; shift; sd="$1"; shift; src="$1"; shift;
646 cd "$bld"; mkdir -p -- "$sd"; cd "$sd";
648 if ($oot_preclean ne 'no') {
649 $pre.= "find . -maxdepth 1 ! -path .";
650 $pre.= " ! -path ./target" if $oot_preclean ne 'full';
651 $pre.= " -print0 | xargs -0r rm -r --;"
653 if ($linkfarm_depth eq 'shallow') {
654 $pre.= <<'END_SHALLOW';
655 clean () { find -lname "$src/*" -print0 | xargs -0r rm --; }; clean;
656 find "$src" -maxdepth 1 \! -name Cargo.lock -print0 |
657 xargs -0r sh -ec 'for f in "$@"; do
662 } elsif ($linkfarm_depth =~ /full|git/) {
663 $pre .= <<'END_EITHER_DEEP_DIRS';
664 clean () { find -follow -lname "$src/*" -print0 | xargs -0r rm --; };
665 (set -e; cd "$src"; find . \! -name Cargo.lock \! \( -name .git -prune \) \! -path . \! -name .git -type d -print0) |
666 xargs -0r sh -ec 'for f in "$@"; do
667 rm -f "$f" 2>/dev/null ||:;
671 if ($linkfarm_depth eq 'git') {
672 $pre .= <<'END_FILES_GIT'
673 (set -e; cd "$src"; git ls-files --exclude-standard -co -z) |
675 } elsif ($linkfarm_depth eq 'full') {
676 $pre .= <<'END_FILES_FULL'
677 (set -e; cd "$src"; find . \! -name Cargo.lock \! \( -name .git -prune \) \! -type d -print0) |
680 $pre .= <<'END_DEEP';
682 BEGIN { $src=shift @ARGV; }
683 next if (readlink "$_"//"") eq "$src/$_";
685 symlink "$src/$_", "$_" or die "$_ $!";
688 } elsif ($linkfarm_depth eq 'copy-edit') {
689 $pre .= <<'END_COPY_EDIT';
690 find -lname "$src/*" -print0 | xargs -0r rm --;
691 (set -e; cd "$src"; git ls-files -c -z |
692 cpio --quiet -p0m --no-preserve-owner -u --make-directories "$bld/$sd");
694 (set -e; cd "$src"; git ls-files -c -z) | xargs -0r rm -f --;
697 $post .= <<'END_COPY_EDIT_BUNDLE';
698 (set -e; cd "$src"; git ls-files -c -z) |
699 cpio -Hustar -o0 --quiet >"nailing-cargo-update.tar";
702 die "$linkfarm_depth ?";
704 $pre .= <<'ENDLK' if $do_cargo_lock;
705 if test -e Cargo.lock; then
707 cp -- "$src"/Cargo.lock .;
710 $post .= <<'ENDCLEAN' if $oot_clean && !$just_linkfarm;
714 my $addpath = (cfg_uc qw(oot path_add)) //
715 $use eq 'really' ? Types::Serialiser::true : Types::Serialiser::false;
717 !Types::Serialiser::is_bool $addpath ? $addpath :
718 $addpath ? '$HOME/.cargo/bin' :
720 if (defined $addpath) {
722 PATH=$addpath:\${PATH-/usr/local/bin:/bin:/usr/bin};
726 $pre =~ s/^\s+//mg; $pre =~ s/\s+/ /g;
727 $post =~ s/^\s+//mg; $post =~ s/\s+/ /g;
729 my $getuser = sub { cfgsn qw(oot user) };
731 my $xe = $verbose >= 2 ? 'xe' : 'e';
734 @command = (@_, 'sh',"-${xe}c",$pre.'exec "$@"','--',@xargs);
736 @command = (@_, 'sh',"-${xe}c",$pre.'"$@"; '.$post,'--',@xargs);
738 push @command, @ARGV;
740 my $command_sh = sub {
741 my $quoted = join ' ', map {
746 @command = @_, "set -${xe}; $pre $quoted; $post";
748 print STDERR "$self: out-of-tree, building in: \`$build_absdir'\n"
750 if ($use eq 'really') {
751 my $user = $getuser->();
752 my @pw = getpwnam $user or die "$self: oot.user \`$user' lookup failed\n";
753 my $homedir = $pw[7];
754 $sh_ec->('really','-u',$user,'env',"HOME=$homedir");
755 print STDERR "$self: using really to run as user \`$user'\n" if $verbose;
756 } elsif ($use eq 'ssh') {
757 my $user = $getuser->();
758 $user .= '@localhost' unless $user =~ m/\@/;
759 $command_sh->('ssh',$user);
760 print STDERR "$self: using ssh to run as \`$user'\n" if $verbose;
761 } elsif ($use eq 'command_args') {
762 my @c = cfgn_list qw(oot command);
764 print STDERR "$self: out-of-tree, adverbial command: @c\n" if $verbose;
765 } elsif ($use eq 'command_sh') {
766 my @c = cfgn_list qw(oot command);
768 print STDERR "$self: out-of-tree, ssh'ish command: @c\n" if $verbose;
769 } elsif ($use eq 'null') {
772 die "$self: oot.use mode $use not recognised\n";
779 $ENV{CARGO_MANIFEST_DIR} = $src_absdir unless $linkfarm_depth;
780 $ENV{NAILINGCARGO_MANIFEST_DIR} = $src_absdir;
781 $ENV{NAILINGCARGO_WORKSPHERE} = $worksphere;
782 $ENV{NAILINGCARGO_BUILDSPHERE} = $oot_absdir;
783 delete $ENV{NAILINGCARGO_BUILDSPHERE} unless $oot_absdir;
784 $ENV{NAILINGCARGO_BUILD_DIR} = $build_absdir // $src_absdir;
790 if ($want_uninstall) {
792 foreach my $mf (keys %manifests) {
793 eval { uninstall1($mf,1); 1; } or warn "$@";
795 eval { unaltcargolock(1); 1; } or warn "$@";
799 sub consider_directories () {
800 return unless defined $oot_dir;
801 my $bsubdir = "../$oot_dir/$subdir";
802 return if stat $bsubdir;
803 die "$0: build directory $bsubdir inaccessible\n"
805 return if $cargo_lock_update; # will make it
806 die "$0: build directory $bsubdir does not exist, and not in Cargo.lock update mode!\n";
809 our $cleanup_cargo_lock;
811 foreach my $mf (keys %manifests) {
812 link "$mf", "$mf.unnailed" or $!==EEXIST
813 or die "$self: make backup link $mf.unnailed: $!\n";
816 if (defined($alt_cargo_lock)) {
817 die 'internal error' unless $do_cargo_lock;
818 if (@alt_cargo_lock_stat) {
819 print STDERR "$self: using alt_cargo_lock `$alt_cargo_lock'..."
821 if (link $alt_cargo_lock, 'Cargo.lock') {
822 print STDERR " linked\n" if $verbose>=3;
823 } elsif ($! != EEXIST) {
824 print STDERR "\n" if $verbose>=3;
825 die "$self: make \`Cargo.lock' available as \`$alt_cargo_lock': $!\n";
827 print STDERR "checking quality." if $verbose>=3;
828 my @lock_stat = stat 'Cargo.lock'
829 or die "$self: stat Cargo.lock (for alt check: $!\n";
830 same_file(\@alt_cargo_lock_stat, \@lock_stat)
832 "$self: \`Cargo.lock' and alt file \`$alt_cargo_lock' both exist and are not the same file!\n";
834 $cleanup_cargo_lock = 1;
836 $cleanup_cargo_lock = 1;
837 # If Cargo.lock exists and alt doesn't, that means either
838 # that a previous run was interrupted, or that the user has
846 my $nailed = "$mf.nailed~"; $nailed =~ s{/([^/]+)$}{/.$1} or die;
851 my @our_unfound_stab = stat_exists('Cargo.toml', 'local Cargo.toml')
853 foreach my $mf (keys %manifests) {
854 if (@our_unfound_stab) {
855 if (stat_exists $mf, "manifest in to-be-nailed directory") {
856 my @mf_stab = stat _ ;
857 if ("@mf_stab[0..1]" eq "@our_unfound_stab[0..1]") {
858 @our_unfound_stab = ();
863 my $nailing = "$mf.nailing~";
864 my $nailed = nailed($mf);
867 if (open NN, '<', $nailed) {
868 $diff = compare($nailing, \*NN);
869 die "$self: compare $nailing and $nailed: $!" if $diff<0;
871 $!==ENOENT or die "$self: check previous $nailed: $!\n";
881 rename $use, $mf or die "$self: install nailed $use: $!\n";
882 unlink_or_enoent $rm or die "$self: remove old $rm: $!\n";
883 print STDERR "$self: nailed $mf\n" if $verbose>=3;
886 if (@our_unfound_stab && $do_nail) {
888 "$self: *WARNING* cwd is not in Cargo.nail thbough it has Cargo.toml!\n";
893 my $r = system @ARGV;
897 print STDERR "$self: could not execute $ARGV[0]: $!\n";
899 } elsif ($r & 0xff00) {
900 print STDERR "$self: $ARGV[0] failed (exit status $r)\n";
903 print STDERR "$self: $ARGV[0] died due to signal! (wait status $r)\n";
908 sub files_return_after_update () {
909 if ($linkfarm_depth eq 'copy-edit') {
910 system qw(sh -ec), <<'END', 'x', "$build_absdir";
911 git ls-files -c -z | \
912 tar -x --keep-newer-files --no-same-permissions --no-same-owner \
913 --no-acls --no-selinux --no-xattrs --warning=no-ignore-newer \
914 -Hustar --null --files-from=- --force-local \
915 -f "$1/nailing-cargo-update.tar"
917 } elsif ($do_cargo_lock && $cargo_lock_update && !$just_linkfarm) {
918 # avoids importing File::Copy and the error handling is about as good
920 my $r= system qw(cp --), "$build_absdir/Cargo.lock", "Cargo.lock";
921 die "$self: run cp: $! $?" if $r<0 || $r & 0xff;
922 die "$self: failed to update local Cargo.lock (wait status $r)\n" if $r;
926 sub uninstall1 ($$) {
927 my ($mf, $enoentok) = @_;
928 my $unnailed = "$mf.unnailed";
929 rename $unnailed, $mf or ($enoentok && $!==ENOENT)
930 or die "$self: failed to restore: rename $unnailed back to $mf: $!\n";
933 sub unaltcargolock ($) {
935 return unless $cleanup_cargo_lock;
936 die 'internal error!' unless $do_cargo_lock && defined $alt_cargo_lock;
938 # we ignore $enoentok because we don't know if one was supposed to
941 rename('Cargo.lock', $alt_cargo_lock) or $!==ENOENT or die
942 "$self: cleanup: rename possibly-updated \`Cargo.lock' to \`$alt_cargo_lock': $!\n";
944 unlink 'Cargo.lock' or $!==ENOENT or die
945 "$self: cleanup: remove \`Cargo.lock' in favour of \`$alt_cargo_lock': $!\n";
946 # ^ this also helps clean up the stupid rename() corner case
950 foreach my $mf (keys %manifests) {
951 my $nailed = nailed($mf);
952 link $mf, $nailed or die "$self: preserve (link) $mf as $nailed: $!\n";
961 # Loop exit condition:
964 # $is_cargo==1 <cargo-command> <cargo-opts> [--] <subcmd>...
965 # $is_cargo==0 <build-command>...
969 die "$self: need cargo subcommand\n"
970 unless $noact || $just_linkfarm;;
971 push @ARGV, "CARGO-SUBCOMMAND"; # dummy, user may see it
977 my $not_a_nailing_opt = sub { # usage 1
978 unshift @ARGV, $orgopt;
979 unshift @ARGV, 'cargo';
981 no warnings qw(exiting);
984 $not_a_nailing_opt->() unless m{^-};
985 $not_a_nailing_opt->() if $_ eq '--';
987 my $edits_sources = sub {
988 $linkfarm_depth = 'copy-edit';
991 if ($_ eq '---') { # usage 2 or 3
993 die "$self: --- must be followed by build command\n" unless $noact;
994 push @ARGV, 'BUILD-COMMAND';
996 if ($ARGV[0] eq '--') { # usage 3
999 } elsif (grep { $_ eq '--' } @ARGV) { # usage 2
1001 } elsif ($ARGV[0] =~ m{[^/]*cargo[^/]*$}) { # usage 2
1012 } elsif (s{^-v}{-}) {
1014 } elsif (s{^-q}{-}) {
1016 } elsif (s{^-n}{-}) {
1018 } elsif (s{^-s(.+)}{-}s) {
1020 } elsif (s{^-([uU])}{-}) {
1021 $cargo_lock_update = $1=~m/[a-z]/;
1022 } elsif (s{^-([cC])}{-}) {
1023 $pass_options = $1=~m/[a-z]/;
1024 } elsif (s{^-D}{-}) {
1026 } elsif (s{^-E}{-}) {
1028 } elsif (s{^-T(.+)}{-}s) {
1030 } elsif (s{^-([oO])}{-}) {
1031 $online = $1=~m/[a-z]/;
1033 die "$self: unknown short option(s) $_\n" unless $_ eq $orgopt;
1034 $not_a_nailing_opt->();
1037 } elsif (s{^--help$}{}) {
1039 } elsif (s{^--(?:doc|man|manual)?$}{}) {
1041 } elsif (s{^--target=}{}) {
1043 } elsif (m{^--(on|off)line$}) {
1044 $online = $1 eq 'on';
1045 } elsif (m{^--just-linkfarm(?:=(shallow|git|full))?$}) {
1047 $linkfarm_depth = $1 if $1;
1048 $cargo_lock_update= 1; # will set $linkfarm_detph to 1 by default
1049 } elsif (m{^--linkfarm(?:=(no|shallow|git|full))?$}) {
1050 $linkfarm_depth = $1 || 'git';
1051 } elsif (m{^--edits?-sources?$}) {
1053 } elsif (m{^--just-run$}) {
1054 $do_nail = $do_cargo_lock = $do_lock = 0;
1055 } elsif (m{^--(clean|keep)-linkfarm$}) {
1056 $oot_clean = $1 eq 'clean';
1057 } elsif (m{^--(no-)?preclean-build$}) {
1058 $oot_preclean = $1 ? 'no' : 'src';
1059 } elsif (m{^--preclean-build=(no|src|full)$}) {
1061 } elsif (m{^--(no-)?nail$}) {
1063 } elsif (m{^--(no-)?cargo-lock-manip$}) {
1064 $do_cargo_lock = !$1;
1065 } elsif (m{^--(no-)?concurrency-lock$}) {
1067 } elsif (m{^--leave-nailed$}) {
1069 } elsif (s{^--subcommand-props=}{}) {
1070 my @props = split /\,/, $_;
1071 our %subcmd_prop_ok;
1072 if (!%subcmd_prop_ok) {
1073 foreach my $v (\@subcmd_xprops, values %subcmd_props) {
1074 $subcmd_prop_ok{$_}=1 foreach @$v;
1078 or die "$self: unknown subcommand property \`$_'\n"
1080 $cargo_subcmd = \@props;
1081 } elsif (m{^--(no-)?cargo-lock-update}) {
1082 $cargo_lock_update= !!$1;
1084 $not_a_nailing_opt->();
1092 @args_preface = shift @ARGV;
1093 while (defined($_ = shift @ARGV)) {
1094 if (!m{^-|^\+}) { unshift @ARGV, $_; last; }
1095 if ($_ eq '--') { last; }
1096 push @args_preface, $_;
1098 @ARGV || die "$self: need cargo subcommand\n";
1099 $cargo_subcmd //= $ARGV[0];
1100 $pass_options //= 1;
1102 $cargo_subcmd //= '';
1103 $pass_options //= 0;
1105 push @args_preface, shift @ARGV;
1107 if (!ref($cargo_subcmd)) {
1108 print STDERR " cargo_subcmd lookup $cargo_subcmd\n" if $dump;
1109 $cargo_subcmd = $subcmd_props{$cargo_subcmd} // [ ];
1112 print STDERR " cargo_subcmd props @$cargo_subcmd\n" if $dump;
1114 $cargo_subcmd{$_} = 1 foreach @$cargo_subcmd;
1115 $cargo_subcmd = \%cargo_subcmd;
1122 consider_alt_cargo_lock();
1127 consider_directories();
1128 our @display_cmd = @ARGV;
1129 oot_massage_cmdline();
1135 print STDERR Dumper(\%manifests) if $dump>=2;
1136 print STDERR Dumper(\%packagemap, \@ARGV,
1137 { src_absdir => $src_absdir,
1138 worksphere => $worksphere,
1140 oot_dir => $oot_dir,
1141 oot_absdir => $oot_absdir,
1142 build_absdir => $build_absdir,
1143 linkfarm_depth => $linkfarm_depth,
1144 oot_preclean => $oot_preclean, });
1150 $want_uninstall = !$leave_nailed;
1154 printf STDERR "$self: nailed (%s manifests, %s packages)%s\n",
1155 (scalar keys %manifests), (scalar keys %packagemap),
1156 (defined($alt_cargo_lock) and ", using `$alt_cargo_lock'")
1157 if $verbose && $do_nail;
1159 print STDERR "$self: invoking: @display_cmd\n" if $verbose;
1160 my $estatus = invoke();
1162 files_return_after_update();
1164 uninstall() unless $leave_nailed;
1165 $want_uninstall = 0;
1167 print STDERR "$self: ".($do_nail ? "unnailed" : "finished")
1168 .". status $estatus.\n" if $verbose;