#!/usr/bin/perl -w
# nailing-cargo: wrapper to use unpublished local crates
# SPDX-License-Identifier: AGPL-3.0-or-later
+our $usage = <<'END';
+
+usages:
+
+ nailing-cargo <nailing-opts> <cargo-opts> [--] <subcmd>...
+ nailing-cargo <nailing-opts> --- <cargo> <cargo-opts> [--] <subcmd>...
+ nailing-cargo <nailing-opts> --- [--] <build-command>...
+
+options:
+
+ -v Increase verbosity. (Default is 1)
+ -q Set verbosity to 0
+ -D Increase amount of debugging dump.
+ -n "No action": stop after writing Cargo.toml.nailing~
+ everywhere, and do not run any build command
+
+ -c Do add cargo command line options } default is add if
+ -C Do not add cargo command line options } command is cargo
+
+ -o --online -O --offline
+ -u --cargo-lock-update -U --no-cargo-lock-update
+
+ -T<arch> --target=<arch> Specify target architecture
+ -h --help Print this message
+ --doc --man --manual Display complete manual (in w3m)
+ --leave-nailed Leave the nailed Cargo.toml in place
+ -E | --edits-sources Allow source edits (repeat: file creation)
+ -f | --force Override some warnings
+ --linkfarm[=no|shallow|git|full] (default varies, usually "no")
+ --just-linkfarm | --clean-linkfarm | --keep-linkfarm (default is keep)
+ --[no-]preclean-build[=no|src|full] (default is no)
+ --just-run Run the command, don't do cargo stuff
+ --no-nail Do not nail, just run the command.
+ --no-cargo-lock-manip Do not manipulate Cargo.lock.
+ --no-concurrency-lock Do not take the concurrency lock.
+
+ -s<subcommand> Treat command as `cargo <subcommand>`
+ --subcommand-props=<prop>,... Override command props (see docs)
+
+END
our $self;
use File::Glob qw(bsd_glob GLOB_ERR GLOB_BRACE GLOB_NOMAGIC);
use Cwd qw(realpath);
+our $base_path;
our %archmap = (
RPI => 'arm-unknown-linux-gnueabihf',
+ WASM => 'wasm32-unknown-unknown',
);
BEGIN {
$self = $0; $self =~ s{^.*/(?=.)}{};
my $deref = $0;
+ our $base_path;
while ($deref =~ m{^/}) {
my $link = readlink $deref;
if (!defined $link) {
or die "$self: checking our script location $deref: $!\n";
$deref =~ s{/[^/]+$}{}
or die "$self: unexpected script path: $deref\n";
+ $base_path = $deref;
unshift @INC, $deref."/TOML-Tiny/lib";
last;
}
our $cargo_lock_update;
our $pass_options;
our $online;
+our $just_linkfarm;
+our $leave_nailed;
+our $oot_clean;
+our $oot_preclean;
+our $do_nail=1;
+our $do_cargo_lock=1;
+our $do_lock=1;
+our $linkfarm_depth;
#
our %subcmd_props = (
-# build (default) =>[qw( )],
-'generate-lockfile'=>[qw( lock-update !target !target-dir )],
- update =>[qw( lock-update !target online )],
- fetch =>[qw( online !target-dir )],
+# build (default) =>[qw( )],
+'generate-lockfile'=>[qw( lock-update !target !target-dir )],
+ update =>[qw( lock-update !target online )],
+ fetch =>[qw( online !target-dir )],
+ fmt =>[qw( !locked !target !offline !target-dir edits )],
+ init =>[qw( creates )],
+ miri =>[qw( !locked !offline linkfarm-shallow )],
+ publish =>[qw( !offline linkfarm-gitclean )],
+ upgrades =>[qw( !locked !target-dir )],
);
-our @subcmd_xprops = qw(!manifest-path !offline !locked);
+our @subcmd_xprops = qw(!manifest-path);
our @configs;
our $verbose=1;
+our $force=0;
+our $forced=0;
our ($noact,$dump);
our $target;
+sub print_usage () {
+ print $usage or die $!;
+ exit 0;
+}
+
+sub show_manual () {
+ my $manual = ($base_path // '.').'/README.md';
+ stat $manual or die "$self: manual not found at $manual: $!\n";;
+ exec 'sh','-ec', 'pandoc -- "$1" 2>&1 | w3m -T text/html', '--', $manual;
+ die "$self: exec sh failed: $!";
+}
+
+sub forceable_warning ($) {
+ my ($m) = @_;
+ print STDERR "$self: *WARNING*: $m\n";
+ if ($force) {
+ print STDERR "$self: continuing because of --force...\n" unless $forced++;
+ return;
+ } else {
+ die "$self: Stopping due to warning (override with -f | --force)\n";
+ }
+}
+
sub read_or_enoent ($) {
my ($fn) = @_;
if (!open R, '<', $fn) {
}
sub takelock () {
+ return unless $do_lock;
+
for (;;) {
open LOCK, ">", $lockfile or die "$self: open/create $lockfile: $!\n";
flock LOCK, LOCK_EX or die "$self: lock $lockfile: $!\n";
# User should *either* have Cargo.lock in .gitignore,
# or expect to commit Cargo.lock.example ($alt_cargo_lock)
+ return unless $do_cargo_lock;
+
$alt_cargo_lock = (cfg_uc @ck);
my $force = 0;
$oot_dir = undef;
return;
}
+ $oot_clean //= cfg_bool qw(oot clean);
$oot_dir //= 'Build';
$oot_absdir = ($oot_dir !~ m{^/} ? "$worksphere/" : ""). $oot_dir;
}
next;
}
$manifests{$manifest} = [ $toml, $org_subdir ] if $p;
+ foreach my $dep (get_dependency_tables $toml) {
+ next unless defined $dep->{path};
+ queue_referenced_path($dep->{path}, $org_subdir,
+ "dependency of $subdir, $why");
+ }
return ($p, $ws);
}
return undef;
}
sub readorigs () {
+ # We (and our callees) populate %packagemap and %manifest, so if we
+ # don't run, they remain empty and nothing is nailed.
+ return unless $do_nail;
+
foreach my $p (keys %{ $nail->{packages} }) {
my $v = $nail->{packages}{$p};
my $subdir = ref($v) ? $v->{subdir} : $v;
print STDERR "$self: package $p in $packagemap{$p}[0]\n" if $verbose>=2;
}
foreach my $mf (keys %manifests) {
+ die "internal error" unless $do_nail; # belt and braces
+
my ($toml, $mf_org_subdir) = @{ $manifests{$mf} };
foreach my $deps (get_dependency_tables $toml) {
next unless $deps;
foreach my $p (keys %packagemap) {
my $info = $deps->{$p};
next unless defined $info;
+ next if $packagemap{$p}[1] eq $mf_org_subdir;
$deps->{$p} = $info = { } unless ref $info;
+ my $oldpath = $info->{path};
delete $info->{version};
my $newpath = $worksphere.'/'.$packagemap{$p}[0];
- if ($cargo_lock_update and defined $oot_dir and
- $newpath =~ m{^\Q$src_absdir\E(?=$|/)}) {
- our $oot_subdir_realpath;
- $oot_subdir_realpath //= Cwd::realpath "$oot_absdir/$subdir"
- // die "$self: cannot resolve $oot_absdir/$subdir: $!";
- $newpath = $oot_subdir_realpath.$';
- }
+ print STDERR "in $mf set $p path=$newpath (was ".
+ ($oldpath // '<unset>').")\n"
+ if $verbose >= 4;
$info->{path} = $newpath;
+ delete $info->{git};
+ delete $info->{branch};
}
}
my $nailing = "$mf.nailing~";
}
sub addargs () {
+ if ($just_linkfarm) {
+ die "$self: --just-linkfarm but not doing out-of-tree builds!\n"
+ unless defined $oot_dir;
+ @ARGV = ();
+ return;
+ }
+
if (!defined $online) {
$_ = cfg_uc qw(misc online);
if (!defined $_) {
$online //= 1 if subcmd_p('online');
$online //= 0;
+ if ($linkfarm_depth eq 'copy-edit-all') {
+ $oot_preclean //= 'src';
+ if ($oot_preclean !~ m/^(?:src|full)$/) {
+ forceable_warning
+ "-EE specified, but also --preclean=no; will probably leave your source tree full of junk";
+ }
+ }
+
+ if (subcmd_p('linkfarm-gitclean')) {
+ $linkfarm_depth //= 'git';
+ $oot_preclean //= 'src';
+ }
+
$cargo_lock_update //= subcmd_p('lock-update');
+ $linkfarm_depth //=
+ subcmd_p('linkfarm-shallow') ? 'shallow' :
+ $cargo_lock_update ? 'shallow' :
+ '';
+
+ $oot_preclean //= 'no';
our @add;
if (!$cargo_lock_update) {
push @add, qw(--locked) unless subcmd_p('!locked');
+ }
+ if ($linkfarm_depth eq '') {
if (defined($oot_dir) && !subcmd_p('!manifest-path')) {
my $cargotoml = "${src_absdir}/Cargo.toml";
push @args_preface, "--manifest-path=$cargotoml" if $pass_options;
push @add, "--offline" unless $online || subcmd_p('!offline');
+ if (subcmd_p('creates') && $linkfarm_depth !~ m/^copy-edit-all/) {
+ forceable_warning
+ "this subcommand expects to create new source files; you probably want to specify --edits-sources twice aka -EE (which is not the default even now, for safety reasons)";
+ } elsif (subcmd_p('edits') && $linkfarm_depth !~ m/^copy-edit/) {
+ forceable_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)";
+ }
+
push @args_preface, @add if $pass_options;
die if grep { m/ / } @add;
$ENV{NAILINGCARGO_CARGO_OPTIONS} = "@add";
my ($pre,$post) = ('','');
my @xargs;
- if (!$cargo_lock_update) {
+ if ($linkfarm_depth eq '') {
push @xargs, $build_absdir;
($pre, $post) = ('cd "$1"; shift; ', '');
} else {
push @xargs, $oot_absdir, $subdir, $src_absdir;
- $pre = <<'END';
- cd "$1"; shift;
- mkdir -p -- "$1"; cd "$1"; shift;
- clean () { find -lname "$1/*" -print0 | xargs -0r rm --; }; clean;
- find "$1" -maxdepth 1 \! -name Cargo.lock -print0 | xargs -0r -I_ ln -sf -- _ .;
-END
- $pre .= <<'ENDLK' if stat_exists 'Cargo.lock', 'working cargo lockfile';
- rm -f Cargo.lock;
- cp -- "$1"/Cargo.lock .;
+ $pre = <<'END_BOTH';
+ bld="$1"; shift; sd="$1"; shift; src="$1"; shift;
+ cd "$bld"; mkdir -p -- "$sd"; cd "$sd";
+END_BOTH
+ if ($oot_preclean ne 'no') {
+ $pre.= "find . -maxdepth 1 ! -path .";
+ $pre.= " ! -path ./target" if $oot_preclean ne 'full';
+ $pre.= " -print0 | xargs -0r rm -r --;"
+ }
+ if ($linkfarm_depth eq 'shallow') {
+ $pre.= <<'END_SHALLOW';
+ clean () { find -lname "$src/*" -print0 | xargs -0r rm --; }; clean;
+ find "$src" -maxdepth 1 \! -name Cargo.lock -print0 |
+ xargs -0r sh -ec 'for f in "$@"; do
+ rm -rf "${f##*/}";
+ ln -sf -- "$f" .;
+ done';
+END_SHALLOW
+ } elsif ($linkfarm_depth =~ /full|git/) {
+ $pre .= <<'END_EITHER_DEEP_DIRS';
+ clean () { find -follow -lname "$src/*" -print0 | xargs -0r rm --; };
+ (set -e; cd "$src"; find . \! -name Cargo.lock \! \( -name .git -prune \) \! -path . \! -name .git -type d -print0) |
+ xargs -0r sh -ec 'for f in "$@"; do
+ rm -f "$f" 2>/dev/null ||:;
+ mkdir -p "$f";
+ done' x;
+END_EITHER_DEEP_DIRS
+ if ($linkfarm_depth eq 'git') {
+ $pre .= <<'END_FILES_GIT'
+ (set -e; cd "$src"; git ls-files --exclude-standard -co -z) |
+END_FILES_GIT
+ } elsif ($linkfarm_depth eq 'full') {
+ $pre .= <<'END_FILES_FULL'
+ (set -e; cd "$src"; find . \! -name Cargo.lock \! \( -name .git -prune \) \! -type d -print0) |
+END_FILES_FULL
+ }
+ $pre .= <<'END_DEEP';
+ perl -0 -ne '
+ BEGIN { $src=shift @ARGV; }
+ next if (readlink "$_"//"") eq "$src/$_";
+ unlink "$_";
+ symlink "$src/$_", "$_" or die "$_ $!";
+ ' "$src";
+END_DEEP
+ } elsif ($linkfarm_depth =~ m/^copy-edit/) {
+ $pre .= <<'END_COPY_EDIT';
+ find -lname "$src/*" -print0 | xargs -0r rm --;
+ (set -e; cd "$src"; git ls-files -c -z |
+ cpio --quiet -p0m --no-preserve-owner -u --make-directories "$bld/$sd");
+ clean () {
+ (set -e; cd "$src"; git ls-files -c -z) | xargs -0r rm -f --;
+ };
+END_COPY_EDIT
+ if ($linkfarm_depth eq 'copy-edit-all') {
+ $post .= <<'END_COPY_EDIT_GENFILES_ALL';
+ find -xdev \( \( -name .git -o -path ./target -o -path ./nailing-cargo-update.tar \) -prune \) -o
+ \( -type l -o -type f \) -print0 |
+END_COPY_EDIT_GENFILES_ALL
+ } else {
+ $post .= <<'END_COPY_EDIT_GENFILES_GIT';
+ (set -e; cd "$src"; git ls-files -c -z) |
+END_COPY_EDIT_GENFILES_GIT
+ }
+ $post .= <<'END_COPY_EDIT_BUNDLE';
+ cpio -Hustar -o0 --quiet >"nailing-cargo-update.tar";
+END_COPY_EDIT_BUNDLE
+ } else {
+ die "$linkfarm_depth ?";
+ }
+ $pre .= <<'ENDLK' if $do_cargo_lock;
+ if test -e Cargo.lock; then
+ rm -f Cargo.lock;
+ cp -- "$src"/Cargo.lock .;
+ fi;
ENDLK
- $pre .= <<'ENDPRE';
- shift;
-ENDPRE
-# $post = <<'ENDCLEAN';
-# clean;
-#ENDCLEAN
+ $post .= <<'ENDCLEAN' if $oot_clean && !$just_linkfarm;
+ clean;
+ENDCLEAN
}
my $addpath = (cfg_uc qw(oot path_add)) //
$use eq 'really' ? Types::Serialiser::true : Types::Serialiser::false;
}
sub setenvs () {
- $ENV{CARGO_MANIFEST_DIR} = $src_absdir;
+ $ENV{CARGO_MANIFEST_DIR} = $src_absdir unless $linkfarm_depth;
$ENV{NAILINGCARGO_MANIFEST_DIR} = $src_absdir;
$ENV{NAILINGCARGO_WORKSPHERE} = $worksphere;
$ENV{NAILINGCARGO_BUILDSPHERE} = $oot_absdir;
}
if (defined($alt_cargo_lock)) {
+ die 'internal error' unless $do_cargo_lock;
if (@alt_cargo_lock_stat) {
print STDERR "$self: using alt_cargo_lock `$alt_cargo_lock'..."
if $verbose>=3;
print STDERR "$self: nailed $mf\n" if $verbose>=3;
}
- if (@our_unfound_stab) {
+ if (@our_unfound_stab && $do_nail) {
print STDERR
"$self: *WARNING* cwd is not in Cargo.nail thbough it has Cargo.toml!\n";
}
}
}
-sub cargo_lock_update_after () {
- if ($cargo_lock_update) {
+sub files_return_after_update () {
+ if ($linkfarm_depth =~ m/^copy-edit/) {
+ my $tar_source_opts;
+ my $tar_stdin;
+ if ($linkfarm_depth eq 'copy-edit-all') {
+ $tar_source_opts = '--null --files-from=-';
+ $tar_stdin = <<'END_GIT_FILES';
+ git ls-files -c -z | \
+END_GIT_FILES
+ } else {
+ $tar_source_opts = '--anchored --exclude=.git --exclude="*/.git" --exclude=target --exclude=nailing-cargo-update.tar';
+ $tar_stdin = '';
+ }
+ system qw(sh -ec), $tar_stdin . <<'END', 'x', "$build_absdir";
+ tar -x --keep-newer-files --no-same-permissions --no-same-owner \
+ --no-acls --no-selinux --no-xattrs --warning=no-ignore-newer \
+ -Hustar $tar_source_opts --force-local \
+ -f "$1/nailing-cargo-update.tar"
+END
+ } elsif ($do_cargo_lock && $cargo_lock_update && !$just_linkfarm) {
# avoids importing File::Copy and the error handling is about as good
$!=0; $?=0;
my $r= system qw(cp --), "$build_absdir/Cargo.lock", "Cargo.lock";
sub unaltcargolock ($) {
my ($enoentok) = @_;
return unless $cleanup_cargo_lock;
- die 'internal error!' unless defined $alt_cargo_lock;
+ die 'internal error!' unless $do_cargo_lock && defined $alt_cargo_lock;
# we ignore $enoentok because we don't know if one was supposed to
# have been created.
# $is_cargo==0 <build-command>...
OPTS: for (;;) {
- @ARGV or die "$self: need cargo subcommand\n";
+ if (!@ARGV) {
+ die "$self: need cargo subcommand\n"
+ unless $noact || $just_linkfarm;;
+ push @ARGV, "CARGO-SUBCOMMAND"; # dummy, user may see it
+ }
$_ = shift @ARGV;
my $orgopt = $_;
$not_a_nailing_opt->() unless m{^-};
$not_a_nailing_opt->() if $_ eq '--';
+ my $edits_sources = sub {
+ $linkfarm_depth =
+ ($linkfarm_depth//'') eq 'copy-edit' ? 'copy-edit-all' : 'copy-edit';
+ };
+
if ($_ eq '---') { # usage 2 or 3
- die "$self: --- must be followed by build command\n" unless @ARGV;
+ if (!@ARGV) {
+ die "$self: --- must be followed by build command\n" unless $noact;
+ push @ARGV, 'BUILD-COMMAND';
+ }
if ($ARGV[0] eq '--') { # usage 3
shift;
$is_cargo = 0;
}
if (m{^-[^-]}) {
while (m{^-.}) {
- if (s{^-v}{-}) {
+ if (s{^-h}{-}) {
+ print_usage();
+ } elsif (s{^-v}{-}) {
$verbose++;
} elsif (s{^-q}{-}) {
$verbose=0;
} elsif (s{^-n}{-}) {
$noact++;
+ } elsif (s{^-f}{-}) {
+ $force++;
} elsif (s{^-s(.+)}{-}s) {
$cargo_subcmd = $1;
} elsif (s{^-([uU])}{-}) {
$pass_options = $1=~m/[a-z]/;
} elsif (s{^-D}{-}) {
$dump++;
+ } elsif (s{^-E}{-}) {
+ $edits_sources->();
} elsif (s{^-T(.+)}{-}s) {
$target = $1;
} elsif (s{^-([oO])}{-}) {
$not_a_nailing_opt->();
}
}
+ } elsif (s{^--help$}{}) {
+ print_usage();
+ } elsif (s{^--(?:doc|man|manual)?$}{}) {
+ show_manual();
} elsif (s{^--target=}{}) {
$target = $_;
} elsif (m{^--(on|off)line$}) {
$online = $1 eq 'on';
+ } elsif (m{^--just-linkfarm(?:=(shallow|git|full))?$}) {
+ $just_linkfarm = 1;
+ $linkfarm_depth = $1 if $1;
+ $cargo_lock_update= 1; # will set $linkfarm_detph to 1 by default
+ } elsif (m{^--linkfarm(?:=(no|shallow|git|full))?$}) {
+ $linkfarm_depth = $1 || 'git';
+ } elsif (m{^--edits?-sources?$}) {
+ $edits_sources->();
+ } elsif (m{^--force$}) {
+ $force++;
+ } elsif (m{^--just-run$}) {
+ $do_nail = $do_cargo_lock = $do_lock = 0;
+ } elsif (m{^--(clean|keep)-linkfarm$}) {
+ $oot_clean = $1 eq 'clean';
+ } elsif (m{^--(no-)?preclean-build$}) {
+ $oot_preclean = $1 ? 'no' : 'src';
+ } elsif (m{^--preclean-build=(no|src|full)$}) {
+ $oot_preclean = $1;
+ } elsif (m{^--(no-)?nail$}) {
+ $do_nail = !$1;
+ } elsif (m{^--(no-)?cargo-lock-manip$}) {
+ $do_cargo_lock = !$1;
+ } elsif (m{^--(no-)?concurrency-lock$}) {
+ $do_lock = !$1;
+ } elsif (m{^--leave-nailed$}) {
+ $leave_nailed = 1;
} elsif (s{^--subcommand-props=}{}) {
my @props = split /\,/, $_;
our %subcmd_prop_ok;
if ($is_cargo) {
@args_preface = shift @ARGV;
while (defined($_ = shift @ARGV)) {
- if (!m{^-}) { unshift @ARGV, $_; last; }
+ if (!m{^-|^\+}) { unshift @ARGV, $_; last; }
if ($_ eq '--') { last; }
push @args_preface, $_;
}
parse_args();
loadconfigs();
-takelock();
readnail();
+takelock();
consider_alt_cargo_lock();
consider_oot();
readorigs();
subdir => $subdir,
oot_dir => $oot_dir,
oot_absdir => $oot_absdir,
- build_absdir => $build_absdir });
+ build_absdir => $build_absdir,
+ linkfarm_depth => $linkfarm_depth,
+ oot_preclean => $oot_preclean,
+ force => $force,,
+ forced => $forced,});
' or die $@;
}
exit 0 if $noact;
-$want_uninstall = 1;
+$want_uninstall = !$leave_nailed;
makebackups();
install();
printf STDERR "$self: nailed (%s manifests, %s packages)%s\n",
(scalar keys %manifests), (scalar keys %packagemap),
(defined($alt_cargo_lock) and ", using `$alt_cargo_lock'")
- if $verbose;
+ if $verbose && $do_nail;
print STDERR "$self: invoking: @display_cmd\n" if $verbose;
my $estatus = invoke();
-cargo_lock_update_after();
+files_return_after_update();
-uninstall();
+uninstall() unless $leave_nailed;
$want_uninstall = 0;
-print STDERR "$self: unnailed. status $estatus.\n" if $verbose;
+print STDERR "$self: ".($do_nail ? "unnailed" : "finished")
+ .". status $estatus.\n" if $verbose;
exit $estatus;