3 # nailing-cargo: wrapper to use unpublished local crates
5 # Copyright (C) 2019-2020 Ian Jackson
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as
9 # published by the Free Software Foundation, either version 3 of the
10 # License, or (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 # ../nailing-cargo/nailing-caretwgo make
22 # ../nailing-cargo/nailing-cargo cargo build
23 # CARGO='../nailing-cargo/nailing-cargo cargo' make
25 # Why do we need this ?
27 # https://github.com/rust-lang/cargo/issues/6713
28 # https://stackoverflow.com/questions/33025887/how-to-use-a-local-unpublished-crate
29 # https://github.com/rust-lang/cargo/issues/1481
37 # package = { subdir = ... }
45 use Fcntl qw(LOCK_EX);
48 my $self = $0; $self =~ s{^.*/(?=.)}{};
50 our $worksphere = getcwd() // die "$self: getcwd failed: $!\n";
51 $worksphere =~ s{/[^/]+}{} or die "$self: cwd \`$worksphere' unsupported!\n";
52 our $lockfile = "../.nailing-cargo-sphere.lock";
56 sub read_or_enoent ($) {
58 if (!open R, '<', $fn) {
59 return undef if $!==ENOENT;
60 die "$self: open $fn: $!\n";
63 my ($r) = <R> // die "$self: read $fn: $!\n";
67 sub toml_or_enoent ($$) {
69 my $toml = read_or_enoent($f) // return;
70 my ($v,$e) = from_toml($toml);
71 die "$self: parse TOML: $what: $f: $e\n" unless defined $v;
72 die "$e ?" if length $e;
78 my $toml = toml_or_enoent($f, "config file");
79 push @configs, $toml if defined $toml;
83 my $cfgleaf = ".nailing-cargo-cfg.toml";
84 load1config("/etc/nailing-cargo/cfg.toml");
85 load1config("$worksphere/$cfgleaf");
86 load1config("$ENV{HOME}/$cfgleaf") if defined $ENV{HOME};
91 foreach my $cfg (@configs) {
93 return $v if defined $v;
100 open LOCK, ">", $lockfile or die "$self: open/create $lockfile: $!\n";
101 flock LOCK, LOCK_EX or die "$self: lock $lockfile: $!\n";
102 my @fstat = stat LOCK or die "$self: fstat: $!\n";
103 my @stat = stat $lockfile;
105 next if $! == ENOENT;
106 die "$self: stat $lockfile: $!\n";
108 last if "@fstat[0..5]" eq "@stat[0..5]";
112 unlink $lockfile or die "$self: removing lockfile: $!\n";
118 my $nailfile = "../Cargo.nail";
119 open N, '<', $nailfile or die "$self: open $nailfile: $!\n";
121 my $toml = <N> // die "$self: read $nailfile: $!";
123 if ($toml !~ m{^\s*\[/}m &&
124 $toml !~ m{^[^\n\#]*\=}m &&
125 # old non-toml syntax
126 $toml =~ s{^[ \t]*([-_0-9a-z]+)[ \t]+(\S+)[ \t]*$}{$1 = \"$2\"}mig) {
127 $toml =~ s{^}{[packages\]\n};
131 ($nail,$e) = from_toml($toml);
132 if (!defined $nail) {
135 print STDERR "$self: $nailfile transformed into TOML:\n$toml\n";
137 die "$self: parse $nailfile: $e\n";
139 die "$e ?" if length $e;
145 sub read_manifest ($) {
147 my $manifest = "../$subdir/Cargo.toml";
148 if (defined $manifests{$manifest}) {
150 "$self: warning: $subdir: specified more than once!\n";
153 foreach my $try ("$manifest.unnailed", "$manifest") {
154 my $toml = toml_or_enoent($try, "package manifest") // next;
155 my $p = $toml->{package}{name};
158 "$self: warning: $subdir: missing package.name in $try, ignoring\n";
161 $manifests{$manifest} = $toml;
168 foreach my $p (keys %{ $nail->{packages} }) {
169 my $v = $nail->{packages}{$p};
170 my $subdir = ref($v) ? $v->{subdir} : $v;
171 my $gotpackage = read_manifest($subdir) // '<nothing!>';
172 if ($gotpackage ne $p) {
174 "$self: warning: honouring Cargo.nail [packages.$subdir]=$p even though $subdir contains package $gotpackage!\n";
176 die if defined $packagemap{$p};
177 $packagemap{$p} = $subdir;
179 foreach my $subdir (@{ $nail->{subdirs} }) {
180 my $gotpackage = read_manifest($subdir);
181 if (!defined $gotpackage) {
183 "$self: warning: ignoring subdir $subdir which has no Cargo.toml\n";
186 $packagemap{$gotpackage} //= $subdir;
191 foreach my $mf (keys %manifests) {
192 my $toml = $manifests{$mf};
193 foreach my $k (qw(dependencies build-dependencies dev-dependencies)) {
194 my $deps = $toml->{$k};
196 foreach my $p (keys %packagemap) {
197 my $info = $deps->{$p};
199 $info = { } unless ref $info;
200 delete $info->{version};
201 $info->{path} = $packagemap{$p};
204 my $nailing = "$mf.nailing~";
205 unlink $nailing or $!==ENOENT or die "$self: remove old $nailing: $!\n";
206 open N, '>', $nailing or die "$self: create new $nailing: $!\n";
207 print N to_toml($toml) or die "$self: write new $nailing: $!\n";
208 close N or die "$self: close new $nailing: $!\n";
215 if ($want_uninstall) {
217 foreach my $mf (keys %manifests) {
218 eval { uninstall1($mf, 0); 1; } or warn "$@";
224 foreach my $mf (keys %manifests) {
225 link "$mf", "$mf.unnailed" or $!==EEXIST
226 or die "$self: make backup link $mf.unnailed: $!\n";
231 foreach my $mf (keys %manifests) {
232 my $nailing = "$mf.nailing~";
233 my $nailed = "$mf.nailed~";
236 if (open NN, '<', $nailed) {
237 $diff = compare($nailing, \*NN);
238 die "$self: compare $nailing and $nailed: $!" if $diff<0;
240 $!==ENOENT or die "$self: check previous $nailed: $!\n";
250 rename $use, $mf or die "$self: install nailed $use: $!\n";
251 unlink $rm or $!==ENOENT or die "$self: remove old $rm: $!\n";
256 my $r = system @ARGV;
260 print STDERR "$self: could not execute $ARGV[0]: $!\n";
262 } elsif ($r & 0xff00) {
263 print STDERR "$self: $ARGV[0] failed (exit status $r)\n";
266 print STDERR "$self: $ARGV[0] died due to signal! (wait status $r)\n";
271 sub uninstall1 ($$) {
272 my ($mf, $enoentok) = @_;
273 my $unnailed = "$mf.unnailed";
274 rename $unnailed, $mf or ($enoentok && $!==ENOENT)
275 or die "$self: failed to revert: rename $unnailed back to $mf: $!\n";
279 foreach my $mf (keys %manifests) {
284 while (@ARGV && $ARGV[0] =~ m/^-/) {
297 my $estatus = invoke();
306 print STDERR Dumper(\%packagemap, \%manifests);
310 lock=${PWD%/*}/.nail.lock
311 if [ "x$NAILING_CARGO" != "x$lock" ]; then
312 NAILING_CARGO=$lock \
313 exec with-lock-ex -w "$lock" "$self" "$@"
316 exec 203<../Cargo.nail
320 /^ *\[\(build-\)\?dependencies\]/,/^ \[/{
323 if test -e ../Cargo.nail-env; then
327 exec 204<../Cargo.nail
328 while read <&204 what where; do
329 if [ "x$what" = x- ]; then continue; fi
330 if [ "x$what" = 'x#' ]; then continue; fi
331 qwhere="${where//\//\\/}"
334 s/^'$what' *= *\(\".*\"\) *$/'$what' = { version = \1 }/;
335 s#^'$what' *= *{#'$what' = { path = "'"${PWD%/*}"'/'"${qwhere}"'", #;
336 /^'$what' *=/ s/version *= *\"[^"]*\"//;
337 /^'$what' *=/ s/, *\([,}]\)/\1/;
343 exec 204<../Cargo.nail
344 while read <&204 what where; do
345 if [ "x$what" = 'x#' ]; then continue; fi
348 sed <$wf >$wf.nailing~ "$sed"
351 exec 204<../Cargo.nail
352 while read <&204 what where; do
353 if [ "x$what" = 'x#' ]; then continue; fi
355 if ! test -e $wf.unnailed~; then
362 while read <&203 what where; do
363 if [ "x$what" = "x#" ]; then continue; fi
365 if test -e $wf.unnailed~; then
368 mv -f $wf.unnailed~ $wf
374 exec 204<../Cargo.nail
376 while read <&204 what where; do
377 if [ "x$what" = 'x#' ]; then continue; fi
379 printf >&2 ' %s' "$what"
380 if cmp -s $wf.nailed~ $wf.nailing~; then
381 mv -f $wf.nailed~ $wf
384 mv -f $wf.nailing~ $wf