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 = ... }
43 use TOML::Tiny::Faithful;
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.lock";
58 sub read_or_enoent ($) {
60 if (!open R, '<', $fn) {
61 return undef if $!==ENOENT;
62 die "$self: open $fn: $!\n";
65 my ($r) = <R> // die "$self: read $fn: $!\n";
69 sub toml_or_enoent ($$) {
71 my $toml = read_or_enoent($f) // return;
72 my ($v,$e) = from_toml($toml);
73 die "$self: parse TOML: $what: $f: $e\n" unless defined $v;
74 die "$e ?" if length $e;
80 my $toml = toml_or_enoent($f, "config file");
81 push @configs, $toml if defined $toml;
85 my $cfgleaf = ".nailing-cargo-cfg.toml";
86 load1config("/etc/nailing-cargo/cfg.toml");
87 load1config("$worksphere/$cfgleaf");
88 load1config("$ENV{HOME}/$cfgleaf") if defined $ENV{HOME};
93 foreach my $cfg (@configs) {
95 return $v if defined $v;
100 sub unlink_or_enoent ($) { unlink $_[0] or $!==ENOENT; }
104 open LOCK, ">", $lockfile or die "$self: open/create $lockfile: $!\n";
105 flock LOCK, LOCK_EX or die "$self: lock $lockfile: $!\n";
106 my @fstat = stat LOCK or die "$self: fstat: $!\n";
107 my @stat = stat $lockfile;
109 next if $! == ENOENT;
110 die "$self: stat $lockfile: $!\n";
112 last if "@fstat[0..5]" eq "@stat[0..5]";
116 unlink $lockfile or die "$self: removing lockfile: $!\n";
122 my $nailfile = "../Cargo.nail";
123 open N, '<', $nailfile or die "$self: open $nailfile: $!\n";
125 my $toml = <N> // die "$self: read $nailfile: $!";
127 if ($toml !~ m{^\s*\[/}m &&
128 $toml !~ m{^[^\n\#]*\=}m &&
129 # old non-toml syntax
130 $toml =~ s{^[ \t]*([-_0-9a-z]+)[ \t]+(\S+)[ \t]*$}{$1 = \"$2\"}mig) {
131 $toml =~ s{^}{[packages\]\n};
133 $toml =~ s{^[ \t]*\-[ \t]*\=[ \t]*(\"[-_0-9a-z]+\"\n?)$}{
136 $toml = "subdirs = [\n".(join '', map { "$_\n" } @sd)."]\n".$toml;
140 ($nail,$e) = from_toml($toml);
141 if (!defined $nail) {
144 print STDERR "$self: $nailfile transformed into TOML:\n$toml\n";
146 die "$self: parse $nailfile: $e\n";
148 die "$e ?" if length $e;
154 sub read_manifest ($) {
156 my $manifest = "../$subdir/Cargo.toml";
157 print STDERR "$self: reading $manifest...\n" if $verbose>=4;
158 if (defined $manifests{$manifest}) {
160 "$self: warning: $subdir: specified more than once!\n";
163 foreach my $try ("$manifest.unnailed", "$manifest") {
164 my $toml = toml_or_enoent($try, "package manifest") // next;
165 my $p = $toml->{package}{name};
168 "$self: warning: $subdir: missing package.name in $try, ignoring\n";
171 $manifests{$manifest} = $toml;
178 foreach my $p (keys %{ $nail->{packages} }) {
179 my $v = $nail->{packages}{$p};
180 my $subdir = ref($v) ? $v->{subdir} : $v;
181 my $gotpackage = read_manifest($subdir) // '<nothing!>';
182 if ($gotpackage ne $p) {
184 "$self: warning: honouring Cargo.nail packages.$subdir=$p even though $subdir contains package $gotpackage!\n";
186 die if defined $packagemap{$p};
187 $packagemap{$p} = $subdir;
189 foreach my $subdir (@{ $nail->{subdirs} }) {
190 my $gotpackage = read_manifest($subdir);
191 if (!defined $gotpackage) {
193 "$self: warning: ignoring subdir $subdir which has no Cargo.toml\n";
196 $packagemap{$gotpackage} //= $subdir;
201 foreach my $mf (keys %manifests) {
202 my $toml = $manifests{$mf};
203 foreach my $k (qw(dependencies build-dependencies dev-dependencies)) {
204 my $deps = $toml->{$k};
206 foreach my $p (keys %packagemap) {
207 my $info = $deps->{$p};
208 next unless defined $info;
209 $deps->{$p} = $info = { } unless ref $info;
210 delete $info->{version};
211 $info->{path} = $worksphere.'/'.$packagemap{$p};
214 my $nailing = "$mf.nailing~";
215 unlink_or_enoent $nailing or die "$self: remove old $nailing: $!\n";
216 open N, '>', $nailing or die "$self: create new $nailing: $!\n";
217 print N to_toml($toml) or die "$self: write new $nailing: $!\n";
218 close N or die "$self: close new $nailing: $!\n";
225 if ($want_uninstall) {
227 foreach my $mf (keys %manifests) {
228 eval { uninstall1($mf,1); 1; } or warn "$@";
234 foreach my $mf (keys %manifests) {
235 link "$mf", "$mf.unnailed" or $!==EEXIST
236 or die "$self: make backup link $mf.unnailed: $!\n";
242 my $nailed = "$mf.nailed~"; $nailed =~ s{/([^/]+)$}{/.$1} or die;
247 foreach my $mf (keys %manifests) {
248 my $nailing = "$mf.nailing~";
249 my $nailed = nailed($mf);
252 if (open NN, '<', $nailed) {
253 $diff = compare($nailing, \*NN);
254 die "$self: compare $nailing and $nailed: $!" if $diff<0;
256 $!==ENOENT or die "$self: check previous $nailed: $!\n";
266 rename $use, $mf or die "$self: install nailed $use: $!\n";
267 unlink_or_enoent $rm or die "$self: remove old $rm: $!\n";
268 print STDERR "Nailed $mf\n" if $verbose>=3;
273 my $r = system @ARGV;
277 print STDERR "$self: could not execute $ARGV[0]: $!\n";
279 } elsif ($r & 0xff00) {
280 print STDERR "$self: $ARGV[0] failed (exit status $r)\n";
283 print STDERR "$self: $ARGV[0] died due to signal! (wait status $r)\n";
288 sub uninstall1 ($$) {
289 my ($mf, $enoentok) = @_;
290 my $unnailed = "$mf.unnailed";
291 rename $unnailed, $mf or ($enoentok && $!==ENOENT)
292 or die "$self: failed to restore: rename $unnailed back to $mf: $!\n";
296 foreach my $mf (keys %manifests) {
297 my $nailed = nailed($mf);
298 link $mf, $nailed or die "$self: preserve (link) $mf as $nailed: $!\n";
303 while (@ARGV && $ARGV[0] =~ m/^-/) {
310 } elsif (s{^-q}{-}) {
312 } elsif (s{^-n}{-}) {
314 } elsif (s{^-D}{-}) {
317 die "$self: unknown short option(s) $_\n";
321 die "$self: unknown long option $_\n";
333 print STDERR Dumper(\%manifests, \%packagemap);
343 printf STDERR "$self: Nailed (%d manifests, %d packages)\n",
344 (scalar %manifests, scalar %packagemap)
347 my $estatus = invoke();
351 print STDERR "$self: Unnailed.\n" if $verbose;