2 # nailing-cargo: wrapper to use unpublished local crates
3 # SPDX-License-Identifier: AGPL-3.0-or-later
12 RPI => 'arm-unknown-linux-gnueabihf',
16 $self = $0; $self =~ s{^.*/(?=.)}{};
18 while ($deref =~ m{^/}) {
19 my $link = readlink $deref;
22 or die "$self: checking our script location $deref: $!\n";
23 $deref =~ s{/[^/]+$}{}
24 or die "$self: unexpected script path: $deref\n";
25 unshift @INC, $deref."/TOML-Tiny/lib";
28 last if $link !~ m{^/};
33 use Fcntl qw(LOCK_EX);
35 use TOML::Tiny::Faithful;
37 our $src_absdir = getcwd() // die "$self: getcwd failed: $!\n";
39 our $worksphere = $src_absdir;
40 $worksphere =~ s{/([^/]+)$}{}
41 or die "$self: cwd \`$worksphere' unsupported!\n";
42 our $subdir = $1; # leafname
44 our $lockfile = "../.nailing-cargo.lock";
46 our $cargo_lock_update;
47 our $cargo_manifest_args;
48 our $cargo_target_arg=1;
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 stat_exists ($$) {
70 if (stat $fn) { return 1; }
71 $!==ENOENT or die "$self: stat $what: $fn: $!\n";
75 sub toml_or_enoent ($$) {
77 my $toml = read_or_enoent($f) // return;
78 print STDERR "Read TOML from $f\n" if $dump;
79 my ($v,$e) = from_toml($toml);
82 die "$self: parse TOML: $what: $f: $e\n";
84 die "$e ?" if length $e;
90 my $toml = toml_or_enoent($f, "config file");
91 push @configs, $toml if defined $toml;
95 my $dotfile = ".nailing-cargo.toml";
96 load1config("../Nailing-Cargo.toml");
97 load1config($dotfile);
98 load1config("$ENV{HOME}/$dotfile") if defined $ENV{HOME};
99 load1config("/etc/nailing-cargo/cfg.toml");
102 sub unlink_or_enoent ($) { unlink $_[0] or $!==ENOENT; }
106 "@$x[0..5]" eq "@$y[0..5]";
111 open LOCK, ">", $lockfile or die "$self: open/create $lockfile: $!\n";
112 flock LOCK, LOCK_EX or die "$self: lock $lockfile: $!\n";
113 my @fstat = stat LOCK or die "$self: fstat: $!\n";
114 my @stat = stat $lockfile;
116 next if $! == ENOENT;
117 die "$self: stat $lockfile: $!\n";
119 last if same_file(\@fstat,\@stat);
123 unlink $lockfile or die "$self: removing lockfile: $!\n";
131 die "$self: config key \`@_': $m\n";
135 foreach my $cfg (@configs) {
138 last unless defined $v;
139 ref($v) eq 'HASH' or badcfg @_, "parent key \`$k' is not a hash";
142 return $v if defined $v;
150 my $got = ref($v) || 'scalar';
151 return $v if !defined($v) || $got eq $exp;
152 badcfg @_, "found \L$got\E, expected \L$exp\E";
153 # ^ toml doesn't make refs to scalars, so this is unambiguous
158 (cfge $exp, @_) // badcfg @_, "missing";
161 sub cfgs { cfge 'scalar', @_ }
162 sub cfgsn { cfgn 'scalar', @_ }
166 return $v if !defined($v) || Types::Serialiser::is_bool $v;
167 badcfg @_, "expected boolean";
171 my $l = cfge 'ARRAY', @_;
172 foreach my $x (@$l) {
173 !ref $x or badcfg @_, "list contains non-scalar element";
179 my $nailfile = "../Cargo.nail";
180 open N, '<', $nailfile or die "$self: open $nailfile: $!\n";
182 my $toml = <N> // die "$self: read $nailfile: $!";
184 if ($toml !~ m{^\s*\[/}m &&
185 $toml !~ m{^[^\n\#]*\=}m &&
186 # old non-toml syntax
187 $toml =~ s{^[ \t]*([-_0-9a-z]+)[ \t]+(\S+)[ \t]*$}{$1 = \"$2\"}mig) {
188 $toml =~ s{^}{[packages\]\n};
190 $toml =~ s{^[ \t]*\-[ \t]*\=[ \t]*(\"[-_0-9a-z]+\"\n?)$}{
193 $toml = "subdirs = [\n".(join '', map { "$_\n" } @sd)."]\n".$toml;
197 ($nail,$e) = from_toml($toml);
198 if (!defined $nail) {
201 print STDERR "$self: $nailfile transformed into TOML:\n$toml\n";
204 die "$self: parse $nailfile: $e\n";
206 die "$e ?" if length $e;
208 $nail->{subdirs} //= [ ];
210 if (!ref $nail->{subdirs}) {
213 map { s/^\s+//; s/\s+$//; $_; }
219 unshift @configs, $nail;
222 our @alt_cargo_lock_stat;
224 sub consider_alt_cargo_lock () {
225 my @ck = qw(alt_cargo_lock);
226 # User should *either* have Cargo.lock in .gitignore,
227 # or expect to commit Cargo.lock.example ($alt_cargo_lock)
229 $alt_cargo_lock = (cfg_uc @ck);
232 if (defined($alt_cargo_lock) && ref($alt_cargo_lock) eq 'HASH') {
233 $force = cfg_bool qw(alt_cargo_lock force);
234 my @ck = qw(alt_cargo_lock file);
235 $alt_cargo_lock = cfg_uc @ck;
237 $alt_cargo_lock //= Types::Serialiser::true;
239 if (Types::Serialiser::is_bool $alt_cargo_lock) {
240 if (!$alt_cargo_lock) { $alt_cargo_lock = undef; return; }
241 $alt_cargo_lock = 'Cargo.lock.example';
244 if (ref($alt_cargo_lock) || $alt_cargo_lock =~ m{/}) {
245 badcfg @ck, "expected boolean, or leafname";
248 if (!stat_exists $alt_cargo_lock, "alt_cargo_lock") {
249 $alt_cargo_lock = undef unless $force;
253 @alt_cargo_lock_stat = stat _;
256 our $oot_dir; # oot.dir or "Build"
258 sub consider_oot () {
259 $oot_dir = cfgs qw(oot dir);
260 my $use = cfgs qw(oot use);
261 unless (defined($oot_dir) || defined($use)) {
262 die "$self: specified --cargo-lock-update but not out-of-tree build!\n"
263 if $cargo_lock_update;
264 $cargo_lock_update=0;
267 if ($use eq 'disable') {
271 $oot_dir //= 'Build';
277 sub read_manifest ($) {
279 my $manifest = "../$subdir/Cargo.toml";
280 print STDERR "$self: reading $manifest...\n" if $verbose>=4;
281 if (defined $manifests{$manifest}) {
283 "$self: warning: $subdir: specified more than once!\n";
286 foreach my $try ("$manifest.unnailed", "$manifest") {
287 my $toml = toml_or_enoent($try, "package manifest") // next;
288 my $p = $toml->{package}{name};
291 "$self: warning: $subdir: missing package.name in $try, ignoring\n";
294 $manifests{$manifest} = $toml;
301 foreach my $p (keys %{ $nail->{packages} }) {
302 my $v = $nail->{packages}{$p};
303 my $subdir = ref($v) ? $v->{subdir} : $v;
304 my $gotpackage = read_manifest($subdir) // '<nothing!>';
305 if ($gotpackage ne $p) {
307 "$self: warning: honouring Cargo.nail packages.$subdir=$p even though $subdir contains package $gotpackage!\n";
309 die if defined $packagemap{$p};
310 $packagemap{$p} = $subdir;
312 foreach my $subdir (@{ $nail->{subdirs} }) {
313 my $gotpackage = read_manifest($subdir);
314 if (!defined $gotpackage) {
316 "$self: warning: ignoring subdir $subdir which has no Cargo.toml\n";
319 $packagemap{$gotpackage} //= $subdir;
324 foreach my $p (sort keys %packagemap) {
325 print STDERR "$self: package $p in $packagemap{$p}\n" if $verbose>=2;
327 foreach my $mf (keys %manifests) {
328 my $toml = $manifests{$mf};
329 foreach my $k (qw(dependencies build-dependencies dev-dependencies)) {
330 my $deps = $toml->{$k};
332 foreach my $p (keys %packagemap) {
333 my $info = $deps->{$p};
334 next unless defined $info;
335 $deps->{$p} = $info = { } unless ref $info;
336 delete $info->{version};
337 $info->{path} = $worksphere.'/'.$packagemap{$p};
340 my $nailing = "$mf.nailing~";
341 unlink_or_enoent $nailing or die "$self: remove old $nailing: $!\n";
342 open N, '>', $nailing or die "$self: create new $nailing: $!\n";
343 print N to_toml($toml) or die "$self: write new $nailing: $!\n";
344 close N or die "$self: close new $nailing: $!\n";
349 $online //= cfg_bool qw(misc online);
352 $ARGV[0] =~ m{\bcargo\b}) {
353 if ($ARGV[1] =~ m/^(?:generate-lockfile|update)$/) {
354 $cargo_lock_update //= 1;
357 if ($ARGV[1] =~ m/^(?:fetch)$/) {
362 $cargo_lock_update //= 0;
363 $cargo_manifest_args //=
364 (defined $oot_dir) && !$cargo_lock_update;
368 if ($cargo_manifest_args) {
369 push @ARGV, "--manifest-path=${src_absdir}/Cargo.toml",
371 push @ARGV, qw(--target-dir=target) if $cargo_target_arg;
374 if (defined $target) {
375 if ($target =~ m{^[A-Z]}) {
376 $target = (cfgs 'arch', $target) // $archmap{$target}
377 // die "$self: --target=$target alias specified; not in cfg or map\n";
379 push @ARGV, "--target=$target";
382 push @ARGV, "--offline" unless $online;
386 our $build_absdir; # .../Build/<subdir>
388 sub oot_massage_cmdline () {
389 return unless defined $oot_dir;
391 my $use = cfgs qw(oot use);
392 $oot_absdir = ($oot_dir !~ m{^/} ? "$worksphere/" : ""). $oot_dir;
393 $build_absdir = "$oot_absdir/$subdir";
397 if (!$cargo_lock_update) {
398 push @xargs, $build_absdir;
399 ($pre, $post) = ('cd "$1"; shift; ', '');
401 push @xargs, $oot_absdir, $subdir, $src_absdir;
404 mkdir -p -- "$1"; cd "$1"; shift;
405 cp -- "$1"/Cargo.toml
407 $pre .= <<'ENDLK' if stat_exists 'Cargo.lock', 'working cargo lockfile';
415 mkdir -p src; >src/lib.rs; >build.rs
418 rm -r src Cargo.toml build.rs;
421 my $addpath = (cfg_uc qw(oot path_add)) //
422 $use eq 'really' ? Types::Serialiser::true : Types::Serialiser::false;
424 !Types::Serialiser::is_bool $addpath ? $addpath :
425 $addpath ? '$HOME/.cargo/bin' :
427 if (defined $addpath) {
429 PATH=$addpath:\${PATH-/usr/local/bin:/bin:/usr/bin};
433 $pre =~ s/^\s+//mg; $pre =~ s/\s+/ /g;
434 $post =~ s/^\s+//mg; $post =~ s/\s+/ /g;
436 my $getuser = sub { cfgsn qw(oot user) };
438 my $xe = $verbose >= 2 ? 'xe' : 'e';
441 @command = (@_, 'sh',"-${xe}c",$pre.'exec "$@"','--',@xargs);
443 @command = (@_, 'sh',"-${xe}c",$pre.'"$@"; '.$post,'--',@xargs);
445 push @command, @ARGV;
447 my $command_sh = sub {
448 my $quoted = join ' ', map {
453 @command = @_, "set -${xe}; $pre $quoted; $post";
455 print STDERR "$self: out-of-tree, building in: \`$build_absdir'\n"
457 if ($use eq 'really') {
458 my $user = $getuser->();
459 my @pw = getpwnam $user or die "$self: oot.user \`$user' lookup failed\n";
460 my $homedir = $pw[7];
461 $sh_ec->('really','-u',$user,'env',"HOME=$homedir");
462 print STDERR "$self: using really to run as user \`$user'\n" if $verbose;
463 } elsif ($use eq 'ssh') {
464 my $user = $getuser->();
465 $user .= '@localhost' unless $user =~ m/\@/;
466 $command_sh->('ssh',$user);
467 print STDERR "$self: using ssh to run as \`$user'\n" if $verbose;
468 } elsif ($use eq 'command_args') {
469 my @c = cfgn_list qw(oot command);
471 print STDERR "$self: out-of-tree, adverbial command: @c\n" if $verbose;
472 } elsif ($use eq 'command_sh') {
473 my @c = cfgn_list qw(oot command);
475 print STDERR "$self: out-of-tree, ssh'ish command: @c\n" if $verbose;
476 } elsif ($use eq 'null') {
479 die "$self: oot.use mode $use not recognised\n";
486 $ENV{NAILINGCARGO_WORKSPHERE} = $worksphere;
487 $ENV{NAILINGCARGO_MANIFEST_DIR} = $src_absdir;
488 $ENV{NAILINGCARGO_BUILDSPHERE} = $oot_absdir;
489 delete $ENV{NAILINGCARGO_BUILDSPHERE} unless $oot_absdir;
490 $ENV{NAILINGCARGO_BUILD_DIR} = $build_absdir // $src_absdir;
496 if ($want_uninstall) {
498 foreach my $mf (keys %manifests) {
499 eval { uninstall1($mf,1); 1; } or warn "$@";
501 eval { unaltcargolock(1); 1; } or warn "$@";
505 our $cleanup_cargo_lock;
507 foreach my $mf (keys %manifests) {
508 link "$mf", "$mf.unnailed" or $!==EEXIST
509 or die "$self: make backup link $mf.unnailed: $!\n";
512 if (defined($alt_cargo_lock)) {
513 if (@alt_cargo_lock_stat) {
514 print STDERR "$self: using alt_cargo_lock `$alt_cargo_lock'..."
516 if (link $alt_cargo_lock, 'Cargo.lock') {
517 print STDERR " linked\n" if $verbose>=3;
518 } elsif ($! != EEXIST) {
519 print STDERR "\n" if $verbose>=3;
520 die "$self: make \`Cargo.lock' available as \`$alt_cargo_lock': $!\n";
522 print STDERR "checking quality." if $verbose>=3;
523 my @lock_stat = stat 'Cargo.lock'
524 or die "$self: stat Cargo.lock (for alt check: $!\n";
525 same_file(\@alt_cargo_lock_stat, \@lock_stat)
527 "$self: \`Cargo.lock' and alt file \`$alt_cargo_lock' both exist and are not the same file!\n";
529 $cleanup_cargo_lock = 1;
531 $cleanup_cargo_lock = 1;
532 # If Cargo.lock exists and alt doesn't, that means either
533 # that a previous run was interrupted, or that the user has
541 my $nailed = "$mf.nailed~"; $nailed =~ s{/([^/]+)$}{/.$1} or die;
546 my @our_unfound_stab = stat_exists('Cargo.toml', 'local Cargo.toml')
548 foreach my $mf (keys %manifests) {
549 if (@our_unfound_stab) {
550 if (stat_exists $mf, "manifest in to-be-nailed directory") {
551 my @mf_stab = stat _ ;
552 if ("@mf_stab[0..1]" eq "@our_unfound_stab[0..1]") {
553 @our_unfound_stab = ();
558 my $nailing = "$mf.nailing~";
559 my $nailed = nailed($mf);
562 if (open NN, '<', $nailed) {
563 $diff = compare($nailing, \*NN);
564 die "$self: compare $nailing and $nailed: $!" if $diff<0;
566 $!==ENOENT or die "$self: check previous $nailed: $!\n";
576 rename $use, $mf or die "$self: install nailed $use: $!\n";
577 unlink_or_enoent $rm or die "$self: remove old $rm: $!\n";
578 print STDERR "$self: nailed $mf\n" if $verbose>=3;
581 if (@our_unfound_stab) {
583 "$self: *WARNING* cwd is not in Cargo.nail thbough it has Cargo.toml!\n";
588 my $r = system @ARGV;
592 print STDERR "$self: could not execute $ARGV[0]: $!\n";
594 } elsif ($r & 0xff00) {
595 print STDERR "$self: $ARGV[0] failed (exit status $r)\n";
598 print STDERR "$self: $ARGV[0] died due to signal! (wait status $r)\n";
603 sub cargo_lock_update_after () {
604 if ($cargo_lock_update) {
605 # avoids importing File::Copy and the error handling is about as good
607 my $r= system qw(cp --), "$build_absdir/Cargo.lock", "Cargo.lock";
608 die "$self: run cp: $! $?" if $r<0 || $r & 0xff;
609 die "$self: failed to update local Cargo.lock (wait status $r)\n" if $r;
613 sub uninstall1 ($$) {
614 my ($mf, $enoentok) = @_;
615 my $unnailed = "$mf.unnailed";
616 rename $unnailed, $mf or ($enoentok && $!==ENOENT)
617 or die "$self: failed to restore: rename $unnailed back to $mf: $!\n";
620 sub unaltcargolock ($) {
622 return unless $cleanup_cargo_lock;
623 die 'internal error!' unless defined $alt_cargo_lock;
625 # we ignore $enoentok because we don't know if one was supposed to
628 rename('Cargo.lock', $alt_cargo_lock) or $!==ENOENT or die
629 "$self: cleanup: rename possibly-updated \`Cargo.lock' to \`$alt_cargo_lock': $!\n";
631 unlink 'Cargo.lock' or $!==ENOENT or die
632 "$self: cleanup: remove \`Cargo.lock' in favour of \`$alt_cargo_lock': $!\n";
633 # ^ this also helps clean up the stupid rename() corner case
637 foreach my $mf (keys %manifests) {
638 my $nailed = nailed($mf);
639 link $mf, $nailed or die "$self: preserve (link) $mf as $nailed: $!\n";
646 while (@ARGV && $ARGV[0] =~ m/^-/) {
653 } elsif (s{^-q}{-}) {
655 } elsif (s{^-n}{-}) {
657 } elsif (s{^-D}{-}) {
659 } elsif (s{^-A(.+)}{-}s) {
661 } elsif (s{^-([uU])}{-}) {
662 $cargo_lock_update= $1=~m/[a-z]/;
663 } elsif (s{^-([mM])}{-}) {
664 $cargo_manifest_args= $1=~m/[a-z]/;
665 } elsif (s{^-([tT])}{-}) {
666 $cargo_target_arg= $1=~m/[a-z]/;
667 } elsif (s{^-([oO])}{-}) {
668 $online= $1=~m/[a-z]/;
670 die "$self: unknown short option(s) $_\n";
673 } elsif (s{^--(?:target|arch)=}{}) {
675 } elsif (m{^--(no-)?cargo-lock-update}) {
676 $cargo_lock_update= !!$1;
677 } elsif (m{^--(no-)?cargo-manifest-args}) {
678 $cargo_manifest_args= !!$1;
679 } elsif (m{^--(no-)?cargo-target-dir-arg}) {
680 $cargo_target_arg= !!$1;
681 } elsif (m{^--(on|off)line$}) {
682 $online = $1 eq 'on';
684 die "$self: unknown long option $_\n";
688 die "$self: need command to run\n" unless @ARGV || $noact;
695 consider_alt_cargo_lock();
700 our @display_cmd = @ARGV;
701 oot_massage_cmdline();
707 print STDERR Dumper(\%manifests) if $dump>=2;
708 print STDERR Dumper(\%packagemap, \@ARGV,
709 { src_absdir => $src_absdir,
710 worksphere => $worksphere,
713 oot_absdir => $oot_absdir,
714 build_absdir => $build_absdir });
724 printf STDERR "$self: nailed (%s manifests, %s packages)%s\n",
725 (scalar keys %manifests), (scalar keys %packagemap),
726 (defined($alt_cargo_lock) and ", using `$alt_cargo_lock'")
729 print STDERR "$self: invoking: @display_cmd\n" if $verbose;
730 my $estatus = invoke();
732 cargo_lock_update_after();
737 print STDERR "$self: unnailed. status $estatus.\n" if $verbose;