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{/([^/]+)$}{}
71 or die "$self: cwd \`$worksphere' unsupported!\n";
73 our $lockfile = "../.nailing-cargo.lock";
74 our $oot_cargo_lock_faff;
80 sub read_or_enoent ($) {
82 if (!open R, '<', $fn) {
83 return undef if $!==ENOENT;
84 die "$self: open $fn: $!\n";
87 my ($r) = <R> // die "$self: read $fn: $!\n";
91 sub toml_or_enoent ($$) {
93 my $toml = read_or_enoent($f) // return;
94 my ($v,$e) = from_toml($toml);
95 die "$self: parse TOML: $what: $f: $e\n" unless defined $v;
96 die "$e ?" if length $e;
100 sub load1config ($) {
102 my $toml = toml_or_enoent($f, "config file");
103 push @configs, $toml if defined $toml;
107 my $cfgleaf = ".nailing-cargo-cfg.toml";
108 load1config("/etc/nailing-cargo/cfg.toml");
109 load1config("$worksphere/$cfgleaf");
110 load1config("$ENV{HOME}/$cfgleaf") if defined $ENV{HOME};
115 foreach my $cfg (@configs) {
117 return $v if defined $v;
122 sub unlink_or_enoent ($) { unlink $_[0] or $!==ENOENT; }
126 open LOCK, ">", $lockfile or die "$self: open/create $lockfile: $!\n";
127 flock LOCK, LOCK_EX or die "$self: lock $lockfile: $!\n";
128 my @fstat = stat LOCK or die "$self: fstat: $!\n";
129 my @stat = stat $lockfile;
131 next if $! == ENOENT;
132 die "$self: stat $lockfile: $!\n";
134 last if "@fstat[0..5]" eq "@stat[0..5]";
138 unlink $lockfile or die "$self: removing lockfile: $!\n";
144 my $nailfile = "../Cargo.nail";
145 open N, '<', $nailfile or die "$self: open $nailfile: $!\n";
147 my $toml = <N> // die "$self: read $nailfile: $!";
149 if ($toml !~ m{^\s*\[/}m &&
150 $toml !~ m{^[^\n\#]*\=}m &&
151 # old non-toml syntax
152 $toml =~ s{^[ \t]*([-_0-9a-z]+)[ \t]+(\S+)[ \t]*$}{$1 = \"$2\"}mig) {
153 $toml =~ s{^}{[packages\]\n};
155 $toml =~ s{^[ \t]*\-[ \t]*\=[ \t]*(\"[-_0-9a-z]+\"\n?)$}{
158 $toml = "subdirs = [\n".(join '', map { "$_\n" } @sd)."]\n".$toml;
162 ($nail,$e) = from_toml($toml);
163 if (!defined $nail) {
166 print STDERR "$self: $nailfile transformed into TOML:\n$toml\n";
168 die "$self: parse $nailfile: $e\n";
170 die "$e ?" if length $e;
172 if (!ref $nail->{subdirs}) {
175 map { s/^\s+//; s/\s+$//; $_; }
185 sub read_manifest ($) {
187 my $manifest = "../$subdir/Cargo.toml";
188 print STDERR "$self: reading $manifest...\n" if $verbose>=4;
189 if (defined $manifests{$manifest}) {
191 "$self: warning: $subdir: specified more than once!\n";
194 foreach my $try ("$manifest.unnailed", "$manifest") {
195 my $toml = toml_or_enoent($try, "package manifest") // next;
196 my $p = $toml->{package}{name};
199 "$self: warning: $subdir: missing package.name in $try, ignoring\n";
202 $manifests{$manifest} = $toml;
209 foreach my $p (keys %{ $nail->{packages} }) {
210 my $v = $nail->{packages}{$p};
211 my $subdir = ref($v) ? $v->{subdir} : $v;
212 my $gotpackage = read_manifest($subdir) // '<nothing!>';
213 if ($gotpackage ne $p) {
215 "$self: warning: honouring Cargo.nail packages.$subdir=$p even though $subdir contains package $gotpackage!\n";
217 die if defined $packagemap{$p};
218 $packagemap{$p} = $subdir;
220 foreach my $subdir (@{ $nail->{subdirs} }) {
221 my $gotpackage = read_manifest($subdir);
222 if (!defined $gotpackage) {
224 "$self: warning: ignoring subdir $subdir which has no Cargo.toml\n";
227 $packagemap{$gotpackage} //= $subdir;
232 foreach my $p (sort keys %packagemap) {
233 print STDERR "$self: package $p in $packagemap{$p}\n" if $verbose>=2;
235 foreach my $mf (keys %manifests) {
236 my $toml = $manifests{$mf};
237 foreach my $k (qw(dependencies build-dependencies dev-dependencies)) {
238 my $deps = $toml->{$k};
240 foreach my $p (keys %packagemap) {
241 my $info = $deps->{$p};
242 next unless defined $info;
243 $deps->{$p} = $info = { } unless ref $info;
244 delete $info->{version};
245 $info->{path} = $worksphere.'/'.$packagemap{$p};
248 my $nailing = "$mf.nailing~";
249 unlink_or_enoent $nailing or die "$self: remove old $nailing: $!\n";
250 open N, '>', $nailing or die "$self: create new $nailing: $!\n";
251 print N to_toml($toml) or die "$self: write new $nailing: $!\n";
252 close N or die "$self: close new $nailing: $!\n";
259 sub calculate_oot () {
260 my $oot = $nail->{oot};
261 return unless $oot && (defined $oot->{dir} or defined $oot->{use});
262 if (@ARGV && $ARGV[0] =~ m/generate-lockfile|update/) {
263 $oot_cargo_lock_faff = 1;
267 if (!$oot_cargo_lock_faff) {
268 @xargs = $xxx_subdir;
269 ($pre, $post) = ('cd "$1"; shift', '');
271 @xargs = $xxx_builddir, $xxx_subdir, $xxx_src_abs_dir;
272 ($pre, $post) = (<<'END', <<'END');
274 mkdir -p -- "$1"; cd "$1"; shift;
275 cp -- "$1"/Cargo.toml "$1"/Cargo.lock .; shift;
276 mkdir -p src; >src/lib.rs;
278 rm -r src Cargo.toml;
280 $pre =~ s/^\s+//mg; $pre =~ s/^\s+\n/ /g;
281 $post =~ s/^\s+//mg; $post =~ s/^\s+\n/ /g;
283 my $use = $oot->{use} // die "$self: [oot] specified, need oot.use\n";
285 scalar($oot->{user} // die "$self: oot.use $use requires oot.user\n")
290 @command = @_, 'sh','-ec',$pre.' exec "$@"','--',@xargs;
292 @command = @_, 'sh','-ec',$pre.' "$@"; '.$post,'--',@xargs;
295 my $command_sh = sub {
296 my $quoted = join ' ', map {
301 @command = @_, "set -e; $pre $quoted; $post";
303 if ($use eq 'really') {
304 my $user = $getuser->();
305 my @pw = getpwnam $user or "die $self: oot.user lookup failed\n";
306 my $homedir = $pw[7];
307 $sh_ec->('really','-u',$user,'env',"HOME=$homedir");
308 } elsif ($use eq 'ssh') {
309 my $user = $getuser->();
310 $user .= '@localhost' unless $user =~ m/\@/;
311 $command_sh->('ssh',$user);
312 } elsif ($use eq 'command_sh') {
313 $command_sh->(@{ $oot->{command} // die "$self: need oot.command\n" });
314 } elsif ($use eq 'command_args') {
315 $sh_ec->(@{ $oot->{command} // die "$self: need oot.command\n" });
317 die "$self: oot.use mode $use not recognised\n";
326 if ($want_uninstall) {
328 foreach my $mf (keys %manifests) {
329 eval { uninstall1($mf,1); 1; } or warn "$@";
335 foreach my $mf (keys %manifests) {
336 link "$mf", "$mf.unnailed" or $!==EEXIST
337 or die "$self: make backup link $mf.unnailed: $!\n";
343 my $nailed = "$mf.nailed~"; $nailed =~ s{/([^/]+)$}{/.$1} or die;
348 foreach my $mf (keys %manifests) {
349 my $nailing = "$mf.nailing~";
350 my $nailed = nailed($mf);
353 if (open NN, '<', $nailed) {
354 $diff = compare($nailing, \*NN);
355 die "$self: compare $nailing and $nailed: $!" if $diff<0;
357 $!==ENOENT or die "$self: check previous $nailed: $!\n";
367 rename $use, $mf or die "$self: install nailed $use: $!\n";
368 unlink_or_enoent $rm or die "$self: remove old $rm: $!\n";
369 print STDERR "$self: nailed $mf\n" if $verbose>=3;
374 my $r = system @ARGV;
378 print STDERR "$self: could not execute $ARGV[0]: $!\n";
380 } elsif ($r & 0xff00) {
381 print STDERR "$self: $ARGV[0] failed (exit status $r)\n";
384 print STDERR "$self: $ARGV[0] died due to signal! (wait status $r)\n";
389 sub uninstall1 ($$) {
390 my ($mf, $enoentok) = @_;
391 my $unnailed = "$mf.unnailed";
392 rename $unnailed, $mf or ($enoentok && $!==ENOENT)
393 or die "$self: failed to restore: rename $unnailed back to $mf: $!\n";
397 foreach my $mf (keys %manifests) {
398 my $nailed = nailed($mf);
399 link $mf, $nailed or die "$self: preserve (link) $mf as $nailed: $!\n";
404 while (@ARGV && $ARGV[0] =~ m/^-/) {
411 } elsif (s{^-q}{-}) {
413 } elsif (s{^-n}{-}) {
415 } elsif (s{^-D}{-}) {
417 } elsif (s{^-L}{-}) {
418 $oot_cargo_lock_faff=1;
420 die "$self: unknown short option(s) $_\n";
424 die "$self: unknown long option $_\n";
428 die "$self: need command to run\n" unless @ARGV || $noact;
439 print STDERR Dumper(\%manifests, \%packagemap, \@ARGV);
449 printf STDERR "$self: Nailed (%s manifests, %s packages)\n",
450 (scalar keys %manifests), (scalar keys %packagemap)
453 my $estatus = invoke();
458 get_cargo_lock() if $oot_cargo_lock_faff;
460 print STDERR "$self: unnailed. status $estatus.\n" if $verbose;