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 = ... }
46 use Types::Serialiser;
49 $self = $0; $self =~ s{^.*/(?=.)}{};
51 while ($deref =~ m{^/}) {
52 my $link = readlink $deref;
55 or die "$self: checking our script location $deref: $!\n";
56 $deref =~ s{/[^/]+$}{}
57 or die "$self: unexpected script path: $deref\n";
58 unshift @INC, $deref."/TOML-Tiny/lib";
61 last if $link !~ m{^/};
66 use Fcntl qw(LOCK_EX);
68 use TOML::Tiny::Faithful;
70 our $src_absdir = getcwd() // die "$self: getcwd failed: $!\n";
72 our $worksphere = $src_absdir;
73 $worksphere =~ s{/([^/]+)$}{}
74 or die "$self: cwd \`$worksphere' unsupported!\n";
75 our $subdir = $1; # leafname
77 our $lockfile = "../.nailing-cargo.lock";
78 our $oot_cargo_lock_faff;
84 sub read_or_enoent ($) {
86 if (!open R, '<', $fn) {
87 return undef if $!==ENOENT;
88 die "$self: open $fn: $!\n";
91 my ($r) = <R> // die "$self: read $fn: $!\n";
95 sub toml_or_enoent ($$) {
97 my $toml = read_or_enoent($f) // return;
98 my ($v,$e) = from_toml($toml);
101 die "$self: parse TOML: $what: $f: $e\n";
103 die "$e ?" if length $e;
107 sub load1config ($) {
109 my $toml = toml_or_enoent($f, "config file");
110 push @configs, $toml if defined $toml;
114 my $cfgleaf = ".nailing-cargo-cfg.toml";
115 load1config("/etc/nailing-cargo/cfg.toml");
116 load1config("$worksphere/$cfgleaf");
117 load1config("$ENV{HOME}/$cfgleaf") if defined $ENV{HOME};
122 foreach my $cfg (@configs) {
124 return $v if defined $v;
129 sub unlink_or_enoent ($) { unlink $_[0] or $!==ENOENT; }
133 open LOCK, ">", $lockfile or die "$self: open/create $lockfile: $!\n";
134 flock LOCK, LOCK_EX or die "$self: lock $lockfile: $!\n";
135 my @fstat = stat LOCK or die "$self: fstat: $!\n";
136 my @stat = stat $lockfile;
138 next if $! == ENOENT;
139 die "$self: stat $lockfile: $!\n";
141 last if "@fstat[0..5]" eq "@stat[0..5]";
145 unlink $lockfile or die "$self: removing lockfile: $!\n";
153 die "$self: config key \`@_': $m\n";
159 last unless defined $v;
160 ref($v) eq 'HASH' or badcfg @_, "parent key \`$k' is not a hash";
169 my $got = ref($v) || 'scalar';
170 return $v if !defined($v) || $got eq $exp;
171 badcfg @_, "found \L$got\E, expected \L$exp\E";
172 # ^ toml doesn't make refs to scalars, so this is unambiguous
177 (cfg $exp, @_) // badcfg @_, "missing";
180 sub cfgs { cfg 'scalar', @_ }
181 sub cfgsn { cfgn 'scalar', @_ }
184 my $l = cfg 'ARRAY', @_;
185 foreach my $x (@$l) {
186 !ref $x or badcfg @_, "list contains non-scalar element";
192 my $nailfile = "../Cargo.nail";
193 open N, '<', $nailfile or die "$self: open $nailfile: $!\n";
195 my $toml = <N> // die "$self: read $nailfile: $!";
197 if ($toml !~ m{^\s*\[/}m &&
198 $toml !~ m{^[^\n\#]*\=}m &&
199 # old non-toml syntax
200 $toml =~ s{^[ \t]*([-_0-9a-z]+)[ \t]+(\S+)[ \t]*$}{$1 = \"$2\"}mig) {
201 $toml =~ s{^}{[packages\]\n};
203 $toml =~ s{^[ \t]*\-[ \t]*\=[ \t]*(\"[-_0-9a-z]+\"\n?)$}{
206 $toml = "subdirs = [\n".(join '', map { "$_\n" } @sd)."]\n".$toml;
210 ($nail,$e) = from_toml($toml);
211 if (!defined $nail) {
214 print STDERR "$self: $nailfile transformed into TOML:\n$toml\n";
217 die "$self: parse $nailfile: $e\n";
219 die "$e ?" if length $e;
221 if (!ref $nail->{subdirs}) {
224 map { s/^\s+//; s/\s+$//; $_; }
234 sub read_manifest ($) {
236 my $manifest = "../$subdir/Cargo.toml";
237 print STDERR "$self: reading $manifest...\n" if $verbose>=4;
238 if (defined $manifests{$manifest}) {
240 "$self: warning: $subdir: specified more than once!\n";
243 foreach my $try ("$manifest.unnailed", "$manifest") {
244 my $toml = toml_or_enoent($try, "package manifest") // next;
245 my $p = $toml->{package}{name};
248 "$self: warning: $subdir: missing package.name in $try, ignoring\n";
251 $manifests{$manifest} = $toml;
258 foreach my $p (keys %{ $nail->{packages} }) {
259 my $v = $nail->{packages}{$p};
260 my $subdir = ref($v) ? $v->{subdir} : $v;
261 my $gotpackage = read_manifest($subdir) // '<nothing!>';
262 if ($gotpackage ne $p) {
264 "$self: warning: honouring Cargo.nail packages.$subdir=$p even though $subdir contains package $gotpackage!\n";
266 die if defined $packagemap{$p};
267 $packagemap{$p} = $subdir;
269 foreach my $subdir (@{ $nail->{subdirs} }) {
270 my $gotpackage = read_manifest($subdir);
271 if (!defined $gotpackage) {
273 "$self: warning: ignoring subdir $subdir which has no Cargo.toml\n";
276 $packagemap{$gotpackage} //= $subdir;
281 foreach my $p (sort keys %packagemap) {
282 print STDERR "$self: package $p in $packagemap{$p}\n" if $verbose>=2;
284 foreach my $mf (keys %manifests) {
285 my $toml = $manifests{$mf};
286 foreach my $k (qw(dependencies build-dependencies dev-dependencies)) {
287 my $deps = $toml->{$k};
289 foreach my $p (keys %packagemap) {
290 my $info = $deps->{$p};
291 next unless defined $info;
292 $deps->{$p} = $info = { } unless ref $info;
293 delete $info->{version};
294 $info->{path} = $worksphere.'/'.$packagemap{$p};
297 my $nailing = "$mf.nailing~";
298 unlink_or_enoent $nailing or die "$self: remove old $nailing: $!\n";
299 open N, '>', $nailing or die "$self: create new $nailing: $!\n";
300 print N to_toml($toml) or die "$self: write new $nailing: $!\n";
301 close N or die "$self: close new $nailing: $!\n";
307 our $oot_dir; # oot.dir or "Build"
310 our $build_absdir; # .../Build/<subdir>
312 sub calculate_oot () {
313 $oot_dir = cfgs qw(oot dir);
314 my $use = cfgs qw(oot use);
315 return unless defined($oot_dir) || defined($use);
316 $oot_dir //= 'Build';
318 if (@ARGV && $ARGV[0] =~ m/generate-lockfile|update/) {
319 $oot_cargo_lock_faff = 1;
322 $oot_absdir = ($oot_dir !~ m{^/} ? "$worksphere/" : ""). $oot_dir;
323 $build_absdir = "$oot_absdir/$subdir";
327 if (!$oot_cargo_lock_faff) {
328 push @xargs, $build_absdir;
329 ($pre, $post) = ('cd "$1"; shift;', '');
331 push @xargs, $build_absdir, $subdir, $src_absdir;
332 ($pre, $post) = (<<'END', <<'END');
334 mkdir -p -- "$1"; cd "$1"; shift;
335 cp -- "$1"/Cargo.toml "$1"/Cargo.lock .; shift;
336 mkdir -p src; >src/lib.rs;
338 rm -r src Cargo.toml;
340 $pre =~ s/^\s+//mg; $pre =~ s/^\s+\n/ /g;
341 $post =~ s/^\s+//mg; $post =~ s/^\s+\n/ /g;
343 my $addpath = (cfg qw(oot path_add)) //
344 $use eq 'really' ? Types::Serialiser::true : Types::Serialiser::false;
346 !Types::Serialiser::is_bool $addpath ? $addpath :
347 $addpath ? '$HOME/.cargo/bin' :
349 if (defined $addpath) {
351 PATH=$addpath:\${PATH-/usr/local/bin:/bin:/usr/bin};
356 my $getuser = sub { cfgsn qw(oot user) };
358 my $xe = $verbose >= 2 ? 'xe' : 'e';
361 @command = (@_, 'sh',"-${xe}c",$pre.' exec "$@"','--',@xargs);
363 @command = (@_, 'sh',"-${xe}c",$pre.' "$@"; '.$post,'--',@xargs);
365 push @command, @ARGV;
367 my $command_sh = sub {
368 my $quoted = join ' ', map {
373 @command = @_, "set -${xe}; $pre $quoted; $post";
375 print STDERR "$self: out-of-tree, building in: \`$build_absdir'\n"
377 if ($use eq 'really') {
378 my $user = $getuser->();
379 my @pw = getpwnam $user or die "$self: oot.user \`$user' lookup failed\n";
380 my $homedir = $pw[7];
381 $sh_ec->('really','-u',$user,'env',"HOME=$homedir");
382 print STDERR "$self: using really to run as user \`$user'\n" if $verbose;
383 } elsif ($use eq 'ssh') {
384 my $user = $getuser->();
385 $user .= '@localhost' unless $user =~ m/\@/;
386 $command_sh->('ssh',$user);
387 print STDERR "$self: using ssh to run as \`$user'\n" if $verbose;
388 } elsif ($use eq 'command_args') {
389 my @c = cfgn_list qw(oot command);
391 print STDERR "$self: out-of-tree, adverbial command: @c\n" if $verbose;
392 } elsif ($use eq 'command_sh') {
393 my @c = cfgn_list qw(oot command);
395 print STDERR "$self: out-of-tree, ssh'ish command: @c\n" if $verbose;
397 die "$self: oot.use mode $use not recognised\n";
406 if ($want_uninstall) {
408 foreach my $mf (keys %manifests) {
409 eval { uninstall1($mf,1); 1; } or warn "$@";
415 foreach my $mf (keys %manifests) {
416 link "$mf", "$mf.unnailed" or $!==EEXIST
417 or die "$self: make backup link $mf.unnailed: $!\n";
423 my $nailed = "$mf.nailed~"; $nailed =~ s{/([^/]+)$}{/.$1} or die;
428 foreach my $mf (keys %manifests) {
429 my $nailing = "$mf.nailing~";
430 my $nailed = nailed($mf);
433 if (open NN, '<', $nailed) {
434 $diff = compare($nailing, \*NN);
435 die "$self: compare $nailing and $nailed: $!" if $diff<0;
437 $!==ENOENT or die "$self: check previous $nailed: $!\n";
447 rename $use, $mf or die "$self: install nailed $use: $!\n";
448 unlink_or_enoent $rm or die "$self: remove old $rm: $!\n";
449 print STDERR "$self: nailed $mf\n" if $verbose>=3;
454 my $r = system @ARGV;
458 print STDERR "$self: could not execute $ARGV[0]: $!\n";
460 } elsif ($r & 0xff00) {
461 print STDERR "$self: $ARGV[0] failed (exit status $r)\n";
464 print STDERR "$self: $ARGV[0] died due to signal! (wait status $r)\n";
469 sub uninstall1 ($$) {
470 my ($mf, $enoentok) = @_;
471 my $unnailed = "$mf.unnailed";
472 rename $unnailed, $mf or ($enoentok && $!==ENOENT)
473 or die "$self: failed to restore: rename $unnailed back to $mf: $!\n";
477 foreach my $mf (keys %manifests) {
478 my $nailed = nailed($mf);
479 link $mf, $nailed or die "$self: preserve (link) $mf as $nailed: $!\n";
484 while (@ARGV && $ARGV[0] =~ m/^-/) {
491 } elsif (s{^-q}{-}) {
493 } elsif (s{^-n}{-}) {
495 } elsif (s{^-D}{-}) {
497 } elsif (s{^-L}{-}) {
498 $oot_cargo_lock_faff=1;
500 die "$self: unknown short option(s) $_\n";
504 die "$self: unknown long option $_\n";
508 die "$self: need command to run\n" unless @ARGV || $noact;
519 print STDERR Dumper(\%manifests) if $dump>=2;
520 print STDERR Dumper(\%packagemap, \@ARGV,
521 { src_absdir => $src_absdir,
522 worksphere => $worksphere,
525 oot_absdir => $oot_absdir,
526 build_absdir => $build_absdir });
536 printf STDERR "$self: nailed (%s manifests, %s packages)\n",
537 (scalar keys %manifests), (scalar keys %packagemap)
540 my $estatus = invoke();
545 get_cargo_lock() if $oot_cargo_lock_faff;
547 print STDERR "$self: unnailed. status $estatus.\n" if $verbose;