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-sphere.lock";
55 our ($verbose,$noact,$dump);
57 sub read_or_enoent ($) {
59 if (!open R, '<', $fn) {
60 return undef if $!==ENOENT;
61 die "$self: open $fn: $!\n";
64 my ($r) = <R> // die "$self: read $fn: $!\n";
68 sub toml_or_enoent ($$) {
70 my $toml = read_or_enoent($f) // return;
71 my ($v,$e) = from_toml($toml);
72 die "$self: parse TOML: $what: $f: $e\n" unless defined $v;
73 die "$e ?" if length $e;
79 my $toml = toml_or_enoent($f, "config file");
80 push @configs, $toml if defined $toml;
84 my $cfgleaf = ".nailing-cargo-cfg.toml";
85 load1config("/etc/nailing-cargo/cfg.toml");
86 load1config("$worksphere/$cfgleaf");
87 load1config("$ENV{HOME}/$cfgleaf") if defined $ENV{HOME};
92 foreach my $cfg (@configs) {
94 return $v if defined $v;
101 open LOCK, ">", $lockfile or die "$self: open/create $lockfile: $!\n";
102 flock LOCK, LOCK_EX or die "$self: lock $lockfile: $!\n";
103 my @fstat = stat LOCK or die "$self: fstat: $!\n";
104 my @stat = stat $lockfile;
106 next if $! == ENOENT;
107 die "$self: stat $lockfile: $!\n";
109 last if "@fstat[0..5]" eq "@stat[0..5]";
113 unlink $lockfile or die "$self: removing lockfile: $!\n";
119 my $nailfile = "../Cargo.nail";
120 open N, '<', $nailfile or die "$self: open $nailfile: $!\n";
122 my $toml = <N> // die "$self: read $nailfile: $!";
124 if ($toml !~ m{^\s*\[/}m &&
125 $toml !~ m{^[^\n\#]*\=}m &&
126 # old non-toml syntax
127 $toml =~ s{^[ \t]*([-_0-9a-z]+)[ \t]+(\S+)[ \t]*$}{$1 = \"$2\"}mig) {
128 $toml =~ s{^}{[packages\]\n};
130 $toml =~ s{^[ \t]*\-[ \t]*\=[ \t]*(\"[-_0-9a-z]+\"\n?)$}{
133 $toml = "subdirs = [\n".(join '', map { "$_\n" } @sd)."]\n".$toml;
137 ($nail,$e) = from_toml($toml);
138 if (!defined $nail) {
141 print STDERR "$self: $nailfile transformed into TOML:\n$toml\n";
143 die "$self: parse $nailfile: $e\n";
145 die "$e ?" if length $e;
151 sub read_manifest ($) {
153 my $manifest = "../$subdir/Cargo.toml";
154 print STDERR "$self: reading $manifest...\n" if $verbose>=3;
155 if (defined $manifests{$manifest}) {
157 "$self: warning: $subdir: specified more than once!\n";
160 foreach my $try ("$manifest.unnailed", "$manifest") {
161 my $toml = toml_or_enoent($try, "package manifest") // next;
162 my $p = $toml->{package}{name};
165 "$self: warning: $subdir: missing package.name in $try, ignoring\n";
168 $manifests{$manifest} = $toml;
175 foreach my $p (keys %{ $nail->{packages} }) {
176 my $v = $nail->{packages}{$p};
177 my $subdir = ref($v) ? $v->{subdir} : $v;
178 my $gotpackage = read_manifest($subdir) // '<nothing!>';
179 if ($gotpackage ne $p) {
181 "$self: warning: honouring Cargo.nail packages.$subdir=$p even though $subdir contains package $gotpackage!\n";
183 die if defined $packagemap{$p};
184 $packagemap{$p} = $subdir;
186 foreach my $subdir (@{ $nail->{subdirs} }) {
187 my $gotpackage = read_manifest($subdir);
188 if (!defined $gotpackage) {
190 "$self: warning: ignoring subdir $subdir which has no Cargo.toml\n";
193 $packagemap{$gotpackage} //= $subdir;
198 foreach my $mf (keys %manifests) {
199 my $toml = $manifests{$mf};
200 foreach my $k (qw(dependencies build-dependencies dev-dependencies)) {
201 my $deps = $toml->{$k};
203 foreach my $p (keys %packagemap) {
204 my $info = $deps->{$p};
206 $info = { } unless ref $info;
207 delete $info->{version};
208 $info->{path} = $worksphere.'/'.$packagemap{$p};
211 my $nailing = "$mf.nailing~";
212 unlink $nailing or $!==ENOENT or die "$self: remove old $nailing: $!\n";
213 open N, '>', $nailing or die "$self: create new $nailing: $!\n";
214 print N to_toml($toml) or die "$self: write new $nailing: $!\n";
215 close N or die "$self: close new $nailing: $!\n";
222 if ($want_uninstall) {
224 foreach my $mf (keys %manifests) {
225 eval { uninstall1($mf, 0); 1; } or warn "$@";
231 foreach my $mf (keys %manifests) {
232 link "$mf", "$mf.unnailed" or $!==EEXIST
233 or die "$self: make backup link $mf.unnailed: $!\n";
238 foreach my $mf (keys %manifests) {
239 my $nailing = "$mf.nailing~";
240 my $nailed = "$mf.nailed~"; $nailed =~ s{/([^/]+)$}{/.$1} or die;
243 if (open NN, '<', $nailed) {
244 $diff = compare($nailing, \*NN);
245 die "$self: compare $nailing and $nailed: $!" if $diff<0;
247 $!==ENOENT or die "$self: check previous $nailed: $!\n";
257 rename $use, $mf or die "$self: install nailed $use: $!\n";
258 unlink $rm or $!==ENOENT or die "$self: remove old $rm: $!\n";
259 print STDERR "Nailed $mf\n" if $verbose>=2;
264 my $r = system @ARGV;
268 print STDERR "$self: could not execute $ARGV[0]: $!\n";
270 } elsif ($r & 0xff00) {
271 print STDERR "$self: $ARGV[0] failed (exit status $r)\n";
274 print STDERR "$self: $ARGV[0] died due to signal! (wait status $r)\n";
279 sub uninstall1 ($$) {
280 my ($mf, $enoentok) = @_;
281 my $unnailed = "$mf.unnailed";
282 rename $unnailed, $mf or ($enoentok && $!==ENOENT)
283 or die "$self: failed to revert: rename $unnailed back to $mf: $!\n";
287 foreach my $mf (keys %manifests) {
292 while (@ARGV && $ARGV[0] =~ m/^-/) {
299 } elsif (s{^-n}{-}) {
301 } elsif (s{^-D}{-}) {
304 die "$self: unknown short option(s) $_\n";
308 die "$self: unknown long option $_\n";
320 print STDERR Dumper(\%manifests, \%packagemap);
330 my $estatus = invoke();