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);
46 my $self = $0; $self =~ s{^.*/(?=.)}{};
48 our $worksphere = getcwd() // die "$self: getcwd failed: $!\n";
49 $worksphere =~ s{/[^/]+}{} or die "$self: cwd \`$worksphere' unsupported!\n";
50 our $lockfile = "../.nailing-cargo-sphere.lock";
54 sub read_or_enoent ($) {
56 if (!open R, '<', $fn) {
57 return undef if $!==ENOENT;
58 die "$self: open $fn: $!\n";
61 my ($r) = <R> // die "$self: read $fn: $!\n";
65 sub toml_or_enoent ($$) {
67 my $toml = read_or_enoent($f) // return;
68 my ($v,$e) = from_toml($toml);
69 die "$self: parse TOML: $what: $f: $e\n" unless defined $v;
70 die "$e ?" if length $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("$ENV{HOME}/$cfgleaf") if defined $ENV{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]" eq "@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);
130 if (!defined $nail) {
133 print STDERR "$self: $nailfile transformed into TOML:\n$toml\n";
135 die "$0: parse $nailfile: $e\n";
137 die "$e ?" if length $e;
143 sub read_manifest ($) {
145 my $manifest = "../$subdir/Cargo.toml";
146 if (defined $manifests{$manifest}) {
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/^-/) {
198 print STDERR Dumper(\%packagemap, \%manifests);
202 lock=${PWD%/*}/.nail.lock
203 if [ "x$NAILING_CARGO" != "x$lock" ]; then
204 NAILING_CARGO=$lock \
205 exec with-lock-ex -w "$lock" "$0" "$@"
208 exec 203<../Cargo.nail
212 /^ *\[\(build-\)\?dependencies\]/,/^ \[/{
215 if test -e ../Cargo.nail-env; then
219 exec 204<../Cargo.nail
220 while read <&204 what where; do
221 if [ "x$what" = x- ]; then continue; fi
222 if [ "x$what" = 'x#' ]; then continue; fi
223 qwhere="${where//\//\\/}"
226 s/^'$what' *= *\(\".*\"\) *$/'$what' = { version = \1 }/;
227 s#^'$what' *= *{#'$what' = { path = "'"${PWD%/*}"'/'"${qwhere}"'", #;
228 /^'$what' *=/ s/version *= *\"[^"]*\"//;
229 /^'$what' *=/ s/, *\([,}]\)/\1/;
235 exec 204<../Cargo.nail
236 while read <&204 what where; do
237 if [ "x$what" = 'x#' ]; then continue; fi
240 sed <$wf >$wf.nailing~ "$sed"
243 exec 204<../Cargo.nail
244 while read <&204 what where; do
245 if [ "x$what" = 'x#' ]; then continue; fi
247 if ! test -e $wf.unnailed~; then
254 while read <&203 what where; do
255 if [ "x$what" = "x#" ]; then continue; fi
257 if test -e $wf.unnailed~; then
260 mv -f $wf.unnailed~ $wf
266 exec 204<../Cargo.nail
268 while read <&204 what where; do
269 if [ "x$what" = 'x#' ]; then continue; fi
271 printf >&2 ' %s' "$what"
272 if cmp -s $wf.nailed~ $wf.nailing~; then
273 mv -f $wf.nailed~ $wf
276 mv -f $wf.nailing~ $wf