about summary refs log tree commit diff
path: root/xpcdeps
blob: a7fe5d71e8e66c2f162c59fcde0806df90f1ffdd (plain) (blame)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/bin/sh
# xpcdeps pcfile ... - finds package matching the Requires: section of pkg-config files
cleanup() {
	[ -n "$tempdir" ] &&
	[ -d "$tempdir" ] &&
	rm -rf -- "$tempdir"
}
trap cleanup EXIT INT TERM

tempdir="$(mktemp -d)"

# This takes 1 argument which is the name of the file to write to and reads
# lines from stdin in the form of '$pkgname $file'
create_pcfile() {
	branch=$(git symbolic-ref -q --short HEAD 2>/dev/null)
	while read -r pkgname file ; do
		pkgname="$(xbps-uhelper getpkgname "$pkgname")"
		# We search for Requires.private: because even they are required
		# for usage with --cflags
		for rpkg in $( xbps-query \
					   --repository=hostdir/binpkgs/"$branch" \
					   --cat="$file" "$pkgname" \
					   | grep -E "Requires:|Requires.private" \
					   | cut -d: -f2 \
					   | sed 's/,/ /g' ) ; do
			printf "%s\\n" "$rpkg" >> "$tempdir/$1.pc"
		done
	done
}

grab_local() {
	if [ "$(xdistdir)" != "$(pwd -P)" ]; then
		return 0
	fi

	branch=$(git symbolic-ref -q --short HEAD 2>/dev/null)

	for pkg in hostdir/binpkgs/$branch/*.xbps ; do
		[ "$pkg" = "hostdir/binpkgs/$branch/*.xbps" ] && continue
		pkg="${pkg##*/}"
		pkg="$(echo "$pkg" | sed 's|-musl||g')"
		pcfile="$( xbps-query -i \
				   --repository=hostdir/binpkgs/"$branch" \
				   -f "$(xbps-uhelper getpkgname "$pkg")" \
				   | grep "/$1.pc" )"
		if [ -n "$pcfile" ] ; then
			printf "%s %s\\n" "$pkg" "$pcfile"
			touch "$tempdir/$1.pc"
		fi
	done | create_pcfile "$1"
}

grab_requires() {
	for pkg; do
		xlocate "usr/\(lib\|share\)/pkgconfig/$pkg.pc" 2>/dev/null |
		{ grep . || { echo "xpcdeps: No pkg-config file for $pkg found." 1>&2 ; exit 1; } } |
		create_pcfile "$pkg"
	done
}

get_package_from_requires() {
	while read -r pkg ; do
		xlocate "usr/\(lib\|share\)/pkgconfig/$pkg.pc" 2>/dev/null |
		{ grep . || { printf -- "UNKNOWN PKG PLEASE FIX -> %s\\n" "$pkg" 1>&2 ; return; } } |
		while read -r pkgname file ; do
			file="${file##*/}"
			printf "%s -> %s\\n" "${pkgname%-*}" "${file%*.pc}"
		done
	done
}

sanitize_pcfile() {
	# remove special symbols ( < > = { } $ ) 
	# remove version numbers
	# remove all duplicates
	sed -i -e '/[${<=>}]/d' \
		   -e '/[a-zA-Z]/!d' "$1"
	sort -u "$1"
}

for arg; do
	grab_local "$arg"

	[ -f "$tempdir/$arg.pc" ] || grab_requires "$arg"

	if [ -f "$tempdir/$arg.pc" ]; then
		sanitize_pcfile "$tempdir/$arg.pc" | get_package_from_requires
	fi
done