-
Notifications
You must be signed in to change notification settings - Fork 0
/
qpkg
executable file
·207 lines (193 loc) · 5.43 KB
/
qpkg
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/bin/sh
#https://github.com/skeeto/dotfiles/blob/master/bin/qpkg
# qpkg ("Quick Package") crudely manages user-built and installed
# software. Package files are tarballs created (-C) via DESTDIR
# installs. These package files can be installed (-I) and uninstalled
# (-U) at will.
#
# Special Notes
# =============
#
# The build helper (-B) will run the configure script and fire off make
# with the appropriate number of jobs. It automatically handles the
# --prefix=... configure argument. You may still need to manually
# override LDFLAGS with the compiler's -L for $LIBRARY_PATH since that
# environment variable does *not* override system libraries. The -l
# option can do this for you.
#
# There is no check that a package being installed will clobber an
# existing package. It will overwrite any existing files. If you are
# concerned, you can verify (-V) a package before installing it.
#
# To assist with software that doesn't use a configure script, PREFIX is
# automatically set during "make install" (-C). Like with build (-B),
# all additional arguments are passed to make.
#
# When using the -l (set $LDFLAGS) option without a standard configure
# script, you probably need to use make's -e option to ensure it picks
# it up from the environment. For example:
#
# $ qpkg -lC foo-1.2.3 -e
#
# Environment
# ===========
#
# The environment variable QPKG_PREFIX determines the installation
# prefix. It defaults to "$HOME/.local". This path is not stored
# anywhere in the package, so you *must* ensure it is consistent across
# all commands for a particular package.
#
# To make proper use of the packages you install, you very likely want
# to set and export the following environment variables:
#
# export C_INCLUDE_PATH=$QPKG_PREFIX/include
# export CPLUS_INCLUDE_PATH=$QPKG_PREFIX/include
# export LIBRARY_PATH=$QPKG_PREFIX/lib
# export LD_LIBRARY_PATH=$QPKG_PREFIX/lib
# export LD_RUN_PATH=$QPKG_PREFIX/lib
# export PKG_CONFIG_PATH=$QPKG_PREFIX/lib/pkgconfig
#
# Example Usage
# =============
#
# This sequence of commands will create an package file named
# "emacs-25-minimal.tgz".
#
# $ tar xzf emacs-25.3.tar.gz
# $ qpkg -B -d minimal emacs-25.3/ --without-x --without-all
# $ qpkg -C -d minimal emacs-25-minimal
# $ rm -rf minimal/
#
# The build (-B) command creates a "minimal/" directory (-d) for the
# build. This directory is used by the create (-C) command to generate a
# package (via "make install"). The directory otherwise defaults to "."
# for an in-source build.
#
# Then to install this package:
#
# $ qpkg -I emacs-25-minimal.tgz
#
# And uninstall later:
#
# $ qpkg -U emacs-25-minimal.tgz
#
# This is free and unencumbered software released into the public domain.
set -e
IFS='
'
command='?'
directory=.
make=make
prefix=${QPKG_PREFIX:-$HOME/.local}
keep=false
ldflags=false
usage() {
printf "Usage: qpkg -B [-d DIR] [-l] [-m MAKE] SRC/ [configure args...]\n"
printf " -C [-d DIR] [-kl] [-m MAKE] PKG [make args...]\n"
printf " -I PKG.tgz\n"
printf " -U PKG.tgz\n"
printf " -V PKG.tgz\n"
printf "Options:\n"
printf " -d DIR path of the build directory\n"
printf " -k keep the DESTDIR install directory (_destdir/)\n"
printf " -l set and export LDFLAGS to use LIBRARY_PATH\n"
printf " -m MAKE name of the make program (make)\n"
}
while getopts BCIUVd:hklm: name
do
case $name in
B) command=B;;
C) command=C;;
I) command=I;;
U) command=U;;
V) command=V;;
d) directory="$OPTARG";;
h) usage
exit 0;;
k) keep=true;;
l) ldflags=true;;
m) make="$OPTARG";;
?) usage
exit 2;;
esac
done
shift $(($OPTIND - 1))
if [ $ldflags = true ]; then
export LDFLAGS=$(
IFS=:
for path in $LIBRARY_PATH; do
printf '%s ' -L$path
done
)
fi
nproc() {
if $(which nproc >/dev/null 2>&1); then
command nproc
elif [ -e /proc/cpuinfo ]; then
grep ^processor < /proc/cpuinfo | wc -l
else
sysctl -n hw.ncpu
fi
}
realpath() {
case "$1" in
/*) printf '%s' "$1" ;;
*) printf '%s/%s' "$PWD" "$1" ;;
esac
}
build() {
src="$(realpath "$1")"
mkdir -p "$directory"
build="$(realpath "$directory")"
jobs=$(nproc)
shift
(cd "$build"
$src/configure --prefix="$prefix" "$@"
"$make" -kj$jobs)
}
create() {
pkgfile="$(realpath $(dirname "$1.tgz"))/$(basename "$1.tgz")"
destdir="$(realpath _destdir)"
shift
if [ $keep = false ]; then rm -rf "$destdir"; fi
mkdir -p "$destdir"
(cd "$directory"
"$make" install prefix="$prefix" PREFIX="$prefix" DESTDIR="$destdir" "$@"
tar czf "$pkgfile" -C "$destdir$prefix" .
if [ $keep = false ]; then rm -rf "$destdir"; fi)
}
install() {
mkdir -p "$prefix"
pkgfile=$(realpath "$1")
(cd "$prefix"
tar xzf "$pkgfile")
}
uninstall() {
pkgfile=$(realpath "$1")
(cd "$prefix"
for file in $(tar tzf "$pkgfile" | grep -v '/$'); do
rm -f "$file"
done)
find "$prefix" -type d -empty -delete
}
verify() {
pkgfile=$(realpath "$1")
status=0
cd "$prefix"
for file in $(tar tzf "$pkgfile" | grep -v '/$'); do
if [ -e "$file" ]; then
printf "would clobber '%s'\n" "${file#./}"
status=2
fi
done
exit $status
}
case $command in
B) build "$@";;
C) create "$@";;
I) install "$@";;
U) uninstall "$@";;
V) verify "$@";;
?) usage
exit 2;;
esac