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 = ... }
48 $self = $0; $self =~ s{^.*/(?=.)}{};
50 while ($deref =~ m{^/}) {
51 my $link = readlink $deref;
54 or die "$self: checking our script location $deref: $!\n";
55 $deref =~ s{/[^/]+$}{}
56 or die "$self: unexpected script path: $deref\n";
57 unshift @INC, $deref."/TOML-Tiny/lib";
60 last if $link !~ m{^/};
65 use Fcntl qw(LOCK_EX);
67 use TOML::Tiny::Faithful;
69 our $worksphere = getcwd() // die "$self: getcwd failed: $!\n";
70 $worksphere =~ s{/[^/]+$}{} or die "$self: cwd \`$worksphere' unsupported!\n";
71 our $lockfile = "../.nailing-cargo.lock";
77 sub read_or_enoent ($) {
79 if (!open R, '<', $fn) {
80 return undef if $!==ENOENT;
81 die "$self: open $fn: $!\n";
84 my ($r) = <R> // die "$self: read $fn: $!\n";
88 sub toml_or_enoent ($$) {
90 my $toml = read_or_enoent($f) // return;
91 my ($v,$e) = from_toml($toml);
92 die "$self: parse TOML: $what: $f: $e\n" unless defined $v;
93 die "$e ?" if length $e;
99 my $toml = toml_or_enoent($f, "config file");
100 push @configs, $toml if defined $toml;
104 my $cfgleaf = ".nailing-cargo-cfg.toml";
105 load1config("/etc/nailing-cargo/cfg.toml");
106 load1config("$worksphere/$cfgleaf");
107 load1config("$ENV{HOME}/$cfgleaf") if defined $ENV{HOME};
112 foreach my $cfg (@configs) {
114 return $v if defined $v;
119 sub unlink_or_enoent ($) { unlink $_[0] or $!==ENOENT; }
123 open LOCK, ">", $lockfile or die "$self: open/create $lockfile: $!\n";
124 flock LOCK, LOCK_EX or die "$self: lock $lockfile: $!\n";
125 my @fstat = stat LOCK or die "$self: fstat: $!\n";
126 my @stat = stat $lockfile;
128 next if $! == ENOENT;
129 die "$self: stat $lockfile: $!\n";
131 last if "@fstat[0..5]" eq "@stat[0..5]";
135 unlink $lockfile or die "$self: removing lockfile: $!\n";
141 my $nailfile = "../Cargo.nail";
142 open N, '<', $nailfile or die "$self: open $nailfile: $!\n";
144 my $toml = <N> // die "$self: read $nailfile: $!";
146 if ($toml !~ m{^\s*\[/}m &&
147 $toml !~ m{^[^\n\#]*\=}m &&
148 # old non-toml syntax
149 $toml =~ s{^[ \t]*([-_0-9a-z]+)[ \t]+(\S+)[ \t]*$}{$1 = \"$2\"}mig) {
150 $toml =~ s{^}{[packages\]\n};
152 $toml =~ s{^[ \t]*\-[ \t]*\=[ \t]*(\"[-_0-9a-z]+\"\n?)$}{
155 $toml = "subdirs = [\n".(join '', map { "$_\n" } @sd)."]\n".$toml;
159 ($nail,$e) = from_toml($toml);
160 if (!defined $nail) {
163 print STDERR "$self: $nailfile transformed into TOML:\n$toml\n";
165 die "$self: parse $nailfile: $e\n";
167 die "$e ?" if length $e;
169 if (!ref $nail->{subdirs}) {
172 map { s/^\s+//; s/\s+$//; $_; }
182 sub read_manifest ($) {
184 my $manifest = "../$subdir/Cargo.toml";
185 print STDERR "$self: reading $manifest...\n" if $verbose>=4;
186 if (defined $manifests{$manifest}) {
188 "$self: warning: $subdir: specified more than once!\n";
191 foreach my $try ("$manifest.unnailed", "$manifest") {
192 my $toml = toml_or_enoent($try, "package manifest") // next;
193 my $p = $toml->{package}{name};
196 "$self: warning: $subdir: missing package.name in $try, ignoring\n";
199 $manifests{$manifest} = $toml;
206 foreach my $p (keys %{ $nail->{packages} }) {
207 my $v = $nail->{packages}{$p};
208 my $subdir = ref($v) ? $v->{subdir} : $v;
209 my $gotpackage = read_manifest($subdir) // '<nothing!>';
210 if ($gotpackage ne $p) {
212 "$self: warning: honouring Cargo.nail packages.$subdir=$p even though $subdir contains package $gotpackage!\n";
214 die if defined $packagemap{$p};
215 $packagemap{$p} = $subdir;
217 foreach my $subdir (@{ $nail->{subdirs} }) {
218 my $gotpackage = read_manifest($subdir);
219 if (!defined $gotpackage) {
221 "$self: warning: ignoring subdir $subdir which has no Cargo.toml\n";
224 $packagemap{$gotpackage} //= $subdir;
229 foreach my $p (sort keys %packagemap) {
230 print STDERR "$self: package $p in $packagemap{$p}\n" if $verbose>=2;
232 foreach my $mf (keys %manifests) {
233 my $toml = $manifests{$mf};
234 foreach my $k (qw(dependencies build-dependencies dev-dependencies)) {
235 my $deps = $toml->{$k};
237 foreach my $p (keys %packagemap) {
238 my $info = $deps->{$p};
239 next unless defined $info;
240 $deps->{$p} = $info = { } unless ref $info;
241 delete $info->{version};
242 $info->{path} = $worksphere.'/'.$packagemap{$p};
245 my $nailing = "$mf.nailing~";
246 unlink_or_enoent $nailing or die "$self: remove old $nailing: $!\n";
247 open N, '>', $nailing or die "$self: create new $nailing: $!\n";
248 print N to_toml($toml) or die "$self: write new $nailing: $!\n";
249 close N or die "$self: close new $nailing: $!\n";
256 if ($want_uninstall) {
258 foreach my $mf (keys %manifests) {
259 eval { uninstall1($mf,1); 1; } or warn "$@";
265 foreach my $mf (keys %manifests) {
266 link "$mf", "$mf.unnailed" or $!==EEXIST
267 or die "$self: make backup link $mf.unnailed: $!\n";
273 my $nailed = "$mf.nailed~"; $nailed =~ s{/([^/]+)$}{/.$1} or die;
278 foreach my $mf (keys %manifests) {
279 my $nailing = "$mf.nailing~";
280 my $nailed = nailed($mf);
283 if (open NN, '<', $nailed) {
284 $diff = compare($nailing, \*NN);
285 die "$self: compare $nailing and $nailed: $!" if $diff<0;
287 $!==ENOENT or die "$self: check previous $nailed: $!\n";
297 rename $use, $mf or die "$self: install nailed $use: $!\n";
298 unlink_or_enoent $rm or die "$self: remove old $rm: $!\n";
299 print STDERR "$self: nailed $mf\n" if $verbose>=3;
304 my $r = system @ARGV;
308 print STDERR "$self: could not execute $ARGV[0]: $!\n";
310 } elsif ($r & 0xff00) {
311 print STDERR "$self: $ARGV[0] failed (exit status $r)\n";
314 print STDERR "$self: $ARGV[0] died due to signal! (wait status $r)\n";
319 sub uninstall1 ($$) {
320 my ($mf, $enoentok) = @_;
321 my $unnailed = "$mf.unnailed";
322 rename $unnailed, $mf or ($enoentok && $!==ENOENT)
323 or die "$self: failed to restore: rename $unnailed back to $mf: $!\n";
327 foreach my $mf (keys %manifests) {
328 my $nailed = nailed($mf);
329 link $mf, $nailed or die "$self: preserve (link) $mf as $nailed: $!\n";
334 while (@ARGV && $ARGV[0] =~ m/^-/) {
341 } elsif (s{^-q}{-}) {
343 } elsif (s{^-n}{-}) {
345 } elsif (s{^-D}{-}) {
348 die "$self: unknown short option(s) $_\n";
352 die "$self: unknown long option $_\n";
356 die "$self: need command to run\n" unless @ARGV || $noact;
366 print STDERR Dumper(\%manifests, \%packagemap);
376 printf STDERR "$self: Nailed (%s manifests, %s packages)\n",
377 (scalar keys %manifests), (scalar keys %packagemap)
380 my $estatus = invoke();
384 print STDERR "$self: unnailed.\n" if $verbose;