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
36 # package = { subdir = ... }
44 use Fcntl qw(LOCK_EX);
47 my $self = $0; $self =~ s{^.*/(?=.)}{};
49 my $worksphere = fastcwd // die "$self: Cwd::fastcwd failed: $!\n";
50 $worksphere =~ s{/[^/]+}{} or die "$self: cwd \`$worksphere' unsupported!\n";
51 our $lockfile = "../.nailing-cargo-sphere.lock";
55 sub read_or_enoent ($) {
57 if (!open R, '<', $fn) {
58 return undef if $!==ENOENT;
59 die "$self: open $fn: $!\n";
62 my ($r) = <R> // die "$self: read $fn: $!\n";
66 sub toml_or_enoent ($$) {
68 my $toml = read_or_enoent($f) // return;
69 my ($toml,$e) = from_toml($toml);
70 die "$self: parse TOML: $what: $f: $e\n" if defined $e;
76 my $toml = toml_or_enoent($f, "config file");
77 push @configs, $toml if defined $toml;
81 my $cfgleaf = ".nailing-cargo-cfg.toml";
82 load1config("/etc/nailing-cargo/cfg.toml");
83 load1config("$worksphere/$cfgleaf");
84 load1config("$HOME/$cfgleaf") if defined $HOME;
89 foreach my $cfg (@configs) {
91 return $v if defined $v;
98 open LOCK, ">", $lockfile or die "$0: open/create $lockfile: $!\n";
99 flock LOCK, LOCK_EX or die "$0: lock $lockfile: $!\n";
100 my @fstat = stat LOCK or die "$0: fstat: $!\n";
101 my @stat = stat $lockfile;
103 next if $! == ENOENT;
104 die "$0: stat $lockfile: $!\n";
106 last if "@fstat[0..5]" == "@stat[0..5]";
110 unlink $lockfile or die "$0: removing lockfile: $!\n";
116 my $nailfile = "../Cargo.nail";
117 open N, '<', $nailfile or die "$0: open $nailfile: $!\n";
119 my $toml = <N> // die "$0: read $nailfile: $!";
121 if ($toml !~ m{^\s*\[/}m &&
122 $toml !~ m{^[^\n\#]*\=}m &&
123 # old non-toml syntax
124 $toml =~ s{^[ \t]*([-_0-9a-z]+)[ \t]+(\S+)[ \t]*$}{$1 = $2}mig) {
125 $toml =~ s{^}{[packages\]\n};
129 ($nail,$e) = from_toml($toml);
133 print STDERR "$self: $nailfile transformed into TOML:\n$toml\n";
135 die "$0: parse $nailfile: $e\n";
137 die unless defined $nail;
143 sub read_manifest ($) {
145 my $manifest = "../$subdir/Cargo.toml";
146 if (defined $manifests{$manifesst}) {
148 "$self: warning: $subdir: specified more than once!\n";
151 foreach my $try ("$manifest.unnailed", "$manifest") {
152 my $toml = toml_or_enoent($try, "package manifest") // next;
153 my $p = $toml->{package}{name};
156 "$self: warning: $subdir: missing package.name in $try, ignoring\n";
159 $manifests{$manifest} = $toml;
166 foreach my $p (keys %{ $nail->{packages} }) {
167 my $v = $nail->{packages}{$p};
168 my $subdir = ref($v) ? $v->{subdir} : $v;
169 my $gotpackage = read_manifest($subdir) // '<nothing!>';
170 if ($gotpackage ne $p) {
172 "$self: warning: honouring Cargo.nail [packages.$subdir]=$p even though $subdir contains package $gotpackage!\n";
174 die if defined $packagemap{$p};
175 $packagemap{$p} = $subdir;
177 foreach my $subdir (@{ $nail->{subdirs} }) {
178 my $gotpackage = read_manifest($subdir);
179 if (!defined $gotpackage) {
181 "$self: warning: ignoring subdir $subdir which has no Cargo.toml\n";
184 $packagemap{$gotpackage} //= $subdir;
188 while (@ARGV && $ARGV[0] =~ m/^-/) {
199 lock=${PWD%/*}/.nail.lock
200 if [ "x$NAILING_CARGO" != "x$lock" ]; then
201 NAILING_CARGO=$lock \
202 exec with-lock-ex -w "$lock" "$0" "$@"
205 exec 203<../Cargo.nail
209 /^ *\[\(build-\)\?dependencies\]/,/^ \[/{
212 if test -e ../Cargo.nail-env; then
216 exec 204<../Cargo.nail
217 while read <&204 what where; do
218 if [ "x$what" = x- ]; then continue; fi
219 if [ "x$what" = 'x#' ]; then continue; fi
220 qwhere="${where//\//\\/}"
223 s/^'$what' *= *\(\".*\"\) *$/'$what' = { version = \1 }/;
224 s#^'$what' *= *{#'$what' = { path = "'"${PWD%/*}"'/'"${qwhere}"'", #;
225 /^'$what' *=/ s/version *= *\"[^"]*\"//;
226 /^'$what' *=/ s/, *\([,}]\)/\1/;
232 exec 204<../Cargo.nail
233 while read <&204 what where; do
234 if [ "x$what" = 'x#' ]; then continue; fi
237 sed <$wf >$wf.nailing~ "$sed"
240 exec 204<../Cargo.nail
241 while read <&204 what where; do
242 if [ "x$what" = 'x#' ]; then continue; fi
244 if ! test -e $wf.unnailed~; then
251 while read <&203 what where; do
252 if [ "x$what" = "x#" ]; then continue; fi
254 if test -e $wf.unnailed~; then
257 mv -f $wf.unnailed~ $wf
263 exec 204<../Cargo.nail
265 while read <&204 what where; do
266 if [ "x$what" = 'x#' ]; then continue; fi
268 printf >&2 ' %s' "$what"
269 if cmp -s $wf.nailed~ $wf.nailing~; then
270 mv -f $wf.nailed~ $wf
273 mv -f $wf.nailing~ $wf