summaryrefslogtreecommitdiffstats
path: root/debian/prune-unused-deps
blob: 32063b14e30968b90431f7fe64f7a5ee21cc9094 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/bin/bash
# Run this script in an unpacked upstream tarball directory, and it will update
# (i.e. overwrite) the "unused deps" part of Files-Excluded in d/copyright.

set -e

scriptdir=$(dirname "$(dirname "$(readlink -f "$0")")")
had_config_toml=$(if test -e "$scriptdir/debian/config.toml"; then echo true; else echo false; fi)

( cd "$scriptdir" && debian/rules debian/config.toml )
cp "$scriptdir/debian/config.toml" config.toml

for i in "$scriptdir/debian/patches"/d-00*.patch; do
	"$scriptdir/debian/ensure-patch" -N "$i"
done

test -f Cargo.lock.orig || cp Cargo.lock Cargo.lock.orig
test -f src/bootstrap/Cargo.lock.orig || cp src/bootstrap/Cargo.lock src/bootstrap/Cargo.lock.orig
test -f src/tools/rust-analyzer/Cargo.lock.orig || cp src/tools/rust-analyzer/Cargo.lock src/tools/rust-analyzer/Cargo.lock.orig
rm -f Cargo.lock src/bootstrap/Cargo.lock src/tools/rust-analyzer/Cargo.lock

find vendor -name .cargo-checksum.json -execdir "$scriptdir/debian/prune-checksums" "{}" +

# re-generate Cargo.lock after patching
cargo update --offline

# re-generate src/bootstrap/Cargo.lock after patching
(cd src/bootstrap && cargo update --offline)

# re-generate src/tools/rust-analyzer/Cargo.lock after patching
( cd src/tools/rust-analyzer && cargo update --offline )

needed_crates() {
	cat Cargo.lock \
	    src/bootstrap/Cargo.lock \
	    src/tools/rust-analyzer/Cargo.lock \
	  | sed -z -e 's/\nname = /name = /g' -e 's/\nversion = /version = /g' \
	  | sed -ne 's/\[\[package\]\]name = "\(.*\)"version = "\(.*\)"/\1 \2/gp'
}

ghetto_parse_cargo() {
	cat "$1" \
	 | tr '\n' '\t' \
	 | sed -e 's/\t\[/\n[/g' \
	 | perl -ne 'print if s/^\[(?:package|project)\].*\tname\s*=\s*"(.*?)".*\tversion\s*=\s*"(.*?)".*/\1 \2/g'
}

pruned_paths() {
	for i in vendor/*/Cargo.toml; do
		pkgnamever=
		pkgnamever=$(ghetto_parse_cargo "$i")
		if [ -z "$pkgnamever" ]; then
			echo >&2 "failed to parse: $i"
			exit 1
		fi
		echo "$pkgnamever $i"
	done | grep -v -F -f <(needed_crates) | cut '-d ' -f3 | while read x; do
		echo " $(dirname $x)"
	done
}

header='# DO NOT EDIT below, AUTOGENERATED'
footer='# DO NOT EDIT above, AUTOGENERATED'
{
echo "$header"
pruned_paths
echo "$footer"
} > $scriptdir/debian/copyright.unused-deps

cd $scriptdir/debian
sed -i -e "/^$header/,/^$footer/d" -e '/^# unused dependencies/rcopyright.unused-deps' copyright
rm copyright.unused-deps
$had_config_toml || rm "$scriptdir/debian/config.toml"