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 $src_absdir = getcwd() // die "$self: getcwd failed: $!\n";
71 our $worksphere = $src_absdir;
72 $worksphere =~ s{/([^/]+)$}{}
73 or die "$self: cwd \`$worksphere' unsupported!\n";
74 our $subdir = $1; # leafname
76 our $lockfile = "../.nailing-cargo.lock";
77 our $oot_cargo_lock_faff;
83 sub read_or_enoent ($) {
85 if (!open R, '<', $fn) {
86 return undef if $!==ENOENT;
87 die "$self: open $fn: $!\n";
90 my ($r) = <R> // die "$self: read $fn: $!\n";
94 sub toml_or_enoent ($$) {
96 my $toml = read_or_enoent($f) // return;
97 my ($v,$e) = from_toml($toml);
98 die "$self: parse TOML: $what: $f: $e\n" unless defined $v;
99 die "$e ?" if length $e;
103 sub load1config ($) {
105 my $toml = toml_or_enoent($f, "config file");
106 push @configs, $toml if defined $toml;
110 my $cfgleaf = ".nailing-cargo-cfg.toml";
111 load1config("/etc/nailing-cargo/cfg.toml");
112 load1config("$worksphere/$cfgleaf");
113 load1config("$ENV{HOME}/$cfgleaf") if defined $ENV{HOME};
118 foreach my $cfg (@configs) {
120 return $v if defined $v;
125 sub unlink_or_enoent ($) { unlink $_[0] or $!==ENOENT; }
129 open LOCK, ">", $lockfile or die "$self: open/create $lockfile: $!\n";
130 flock LOCK, LOCK_EX or die "$self: lock $lockfile: $!\n";
131 my @fstat = stat LOCK or die "$self: fstat: $!\n";
132 my @stat = stat $lockfile;
134 next if $! == ENOENT;
135 die "$self: stat $lockfile: $!\n";
137 last if "@fstat[0..5]" eq "@stat[0..5]";
141 unlink $lockfile or die "$self: removing lockfile: $!\n";
149 die "$self: config key \`@_': $m\n";
155 last unless defined $v;
156 ref($v) eq 'HASH' or badcfg @, "parent key \`$k' is not a hash";
165 my $got = ref($v) || 'scalar';
166 return $v if !defined($v) || $got eq $exp;
167 badcfg @_, "found \L$got\E, expected \L$exp\E";
168 # ^ toml doesn't make refs to scalars, so this is unambiguous
173 cfg $exp, @_ // badcfg @_, "missing";
176 sub cfgs { cfg 'scalar', @_ }
177 sub cfgsn { cfgn 'scalar', @_ }
180 my $l = cfg 'ARRAY', @_;
182 !ref $x or badcfg @_, "list contains non-scalar element";
188 my $nailfile = "../Cargo.nail";
189 open N, '<', $nailfile or die "$self: open $nailfile: $!\n";
191 my $toml = <N> // die "$self: read $nailfile: $!";
193 if ($toml !~ m{^\s*\[/}m &&
194 $toml !~ m{^[^\n\#]*\=}m &&
195 # old non-toml syntax
196 $toml =~ s{^[ \t]*([-_0-9a-z]+)[ \t]+(\S+)[ \t]*$}{$1 = \"$2\"}mig) {
197 $toml =~ s{^}{[packages\]\n};
199 $toml =~ s{^[ \t]*\-[ \t]*\=[ \t]*(\"[-_0-9a-z]+\"\n?)$}{
202 $toml = "subdirs = [\n".(join '', map { "$_\n" } @sd)."]\n".$toml;
206 ($nail,$e) = from_toml($toml);
207 if (!defined $nail) {
210 print STDERR "$self: $nailfile transformed into TOML:\n$toml\n";
212 die "$self: parse $nailfile: $e\n";
214 die "$e ?" if length $e;
216 if (!ref $nail->{subdirs}) {
219 map { s/^\s+//; s/\s+$//; $_; }
229 sub read_manifest ($) {
231 my $manifest = "../$subdir/Cargo.toml";
232 print STDERR "$self: reading $manifest...\n" if $verbose>=4;
233 if (defined $manifests{$manifest}) {
235 "$self: warning: $subdir: specified more than once!\n";
238 foreach my $try ("$manifest.unnailed", "$manifest") {
239 my $toml = toml_or_enoent($try, "package manifest") // next;
240 my $p = $toml->{package}{name};
243 "$self: warning: $subdir: missing package.name in $try, ignoring\n";
246 $manifests{$manifest} = $toml;
253 foreach my $p (keys %{ $nail->{packages} }) {
254 my $v = $nail->{packages}{$p};
255 my $subdir = ref($v) ? $v->{subdir} : $v;
256 my $gotpackage = read_manifest($subdir) // '<nothing!>';
257 if ($gotpackage ne $p) {
259 "$self: warning: honouring Cargo.nail packages.$subdir=$p even though $subdir contains package $gotpackage!\n";
261 die if defined $packagemap{$p};
262 $packagemap{$p} = $subdir;
264 foreach my $subdir (@{ $nail->{subdirs} }) {
265 my $gotpackage = read_manifest($subdir);
266 if (!defined $gotpackage) {
268 "$self: warning: ignoring subdir $subdir which has no Cargo.toml\n";
271 $packagemap{$gotpackage} //= $subdir;
276 foreach my $p (sort keys %packagemap) {
277 print STDERR "$self: package $p in $packagemap{$p}\n" if $verbose>=2;
279 foreach my $mf (keys %manifests) {
280 my $toml = $manifests{$mf};
281 foreach my $k (qw(dependencies build-dependencies dev-dependencies)) {
282 my $deps = $toml->{$k};
284 foreach my $p (keys %packagemap) {
285 my $info = $deps->{$p};
286 next unless defined $info;
287 $deps->{$p} = $info = { } unless ref $info;
288 delete $info->{version};
289 $info->{path} = $worksphere.'/'.$packagemap{$p};
292 my $nailing = "$mf.nailing~";
293 unlink_or_enoent $nailing or die "$self: remove old $nailing: $!\n";
294 open N, '>', $nailing or die "$self: create new $nailing: $!\n";
295 print N to_toml($toml) or die "$self: write new $nailing: $!\n";
296 close N or die "$self: close new $nailing: $!\n";
302 our $oot_dir; # oot.dir or "Build"
305 our $build_absdir; # .../Build/<subdir>
307 sub calculate_oot () {
308 $oot_dir = cfgs qw(oot dir);
309 my $use = cfgs qw(oot use);
310 return unless defined($oot_dir) || defined($use);
311 $oot_dir //= 'Build';
313 if (@ARGV && $ARGV[0] =~ m/generate-lockfile|update/) {
314 $oot_cargo_lock_faff = 1;
317 $oot_absdir = ($oot_dir !~ m{^/} ? "$worksphere/"). $oot_dir;
318 $build_absdir = "$oot_absdir/$subdir";
322 if (!$oot_cargo_lock_faff) {
323 @xargs = $build_absdir;
324 ($pre, $post) = ('cd "$1"; shift', '');
326 @xargs = $build_absdir, $subdir, $src_absdir;
327 ($pre, $post) = (<<'END', <<'END');
329 mkdir -p -- "$1"; cd "$1"; shift;
330 cp -- "$1"/Cargo.toml "$1"/Cargo.lock .; shift;
331 mkdir -p src; >src/lib.rs;
333 rm -r src Cargo.toml;
335 $pre =~ s/^\s+//mg; $pre =~ s/^\s+\n/ /g;
336 $post =~ s/^\s+//mg; $post =~ s/^\s+\n/ /g;
338 my $getuser = sub { cfgsn qw(oot user) };
342 @command = @_, 'sh','-ec',$pre.' exec "$@"','--',@xargs;
344 @command = @_, 'sh','-ec',$pre.' "$@"; '.$post,'--',@xargs;
347 my $command_sh = sub {
348 my $quoted = join ' ', map {
353 @command = @_, "set -e; $pre $quoted; $post";
355 if ($use eq 'really') {
356 my $user = $getuser->();
357 my @pw = getpwnam $user or "die $self: oot.user lookup failed\n";
358 my $homedir = $pw[7];
359 $sh_ec->('really','-u',$user,'env',"HOME=$homedir");
360 } elsif ($use eq 'ssh') {
361 my $user = $getuser->();
362 $user .= '@localhost' unless $user =~ m/\@/;
363 $command_sh->('ssh',$user);
364 } elsif ($use eq 'command_sh') {
365 $command_sh->(cfgn_list qw(oot command));
366 } elsif ($use eq 'command_args') {
367 $sh_ec->(cfgn_list qw(oot command))
369 die "$self: oot.use mode $use not recognised\n";
378 if ($want_uninstall) {
380 foreach my $mf (keys %manifests) {
381 eval { uninstall1($mf,1); 1; } or warn "$@";
387 foreach my $mf (keys %manifests) {
388 link "$mf", "$mf.unnailed" or $!==EEXIST
389 or die "$self: make backup link $mf.unnailed: $!\n";
395 my $nailed = "$mf.nailed~"; $nailed =~ s{/([^/]+)$}{/.$1} or die;
400 foreach my $mf (keys %manifests) {
401 my $nailing = "$mf.nailing~";
402 my $nailed = nailed($mf);
405 if (open NN, '<', $nailed) {
406 $diff = compare($nailing, \*NN);
407 die "$self: compare $nailing and $nailed: $!" if $diff<0;
409 $!==ENOENT or die "$self: check previous $nailed: $!\n";
419 rename $use, $mf or die "$self: install nailed $use: $!\n";
420 unlink_or_enoent $rm or die "$self: remove old $rm: $!\n";
421 print STDERR "$self: nailed $mf\n" if $verbose>=3;
426 my $r = system @ARGV;
430 print STDERR "$self: could not execute $ARGV[0]: $!\n";
432 } elsif ($r & 0xff00) {
433 print STDERR "$self: $ARGV[0] failed (exit status $r)\n";
436 print STDERR "$self: $ARGV[0] died due to signal! (wait status $r)\n";
441 sub uninstall1 ($$) {
442 my ($mf, $enoentok) = @_;
443 my $unnailed = "$mf.unnailed";
444 rename $unnailed, $mf or ($enoentok && $!==ENOENT)
445 or die "$self: failed to restore: rename $unnailed back to $mf: $!\n";
449 foreach my $mf (keys %manifests) {
450 my $nailed = nailed($mf);
451 link $mf, $nailed or die "$self: preserve (link) $mf as $nailed: $!\n";
456 while (@ARGV && $ARGV[0] =~ m/^-/) {
463 } elsif (s{^-q}{-}) {
465 } elsif (s{^-n}{-}) {
467 } elsif (s{^-D}{-}) {
469 } elsif (s{^-L}{-}) {
470 $oot_cargo_lock_faff=1;
472 die "$self: unknown short option(s) $_\n";
476 die "$self: unknown long option $_\n";
480 die "$self: need command to run\n" unless @ARGV || $noact;
491 print STDERR Dumper(\%manifests, \%packagemap, \@ARGV);
501 printf STDERR "$self: Nailed (%s manifests, %s packages)\n",
502 (scalar keys %manifests), (scalar keys %packagemap)
505 my $estatus = invoke();
510 get_cargo_lock() if $oot_cargo_lock_faff;
512 print STDERR "$self: unnailed. status $estatus.\n" if $verbose;