blob: d95be30452316c01bdcab3a1964fe2da2d1e5a04 (
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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
|
#!/bin/bash
hash -r
function spm_abspath() {
local filename="${1}"
local start="$(dirname ${filename})"
pushd "${start}" &>/dev/null
end="$(pwd)"
popd &>/dev/null
if [[ -f ${filename} ]]; then
end="${end}/$(basename ${filename})"
fi
echo "${end}"
}
SPM_ORIGIN=$(dirname $(spm_abspath $0))
TMPDIR=${TMPDIR:-/tmp}
OLD_PATH="${PATH}"
default_script=build.sh
build_order=${SPM_ORIGIN}/include/build.order
build_scripts=$(cat ${build_order})
# TODO: Make this part of MKTC
SPM_PROG_RELOC=reloc
SPM_RELOC_COUNT=0
SPM_PREFIX_BIN=.SPM_PREFIX_BIN
SPM_PREFIX_TEXT=.SPM_PREFIX_TEXT
SPM_DEPENDS=.SPM_DEPENDS
SPM_VERBOSE=0
export prefix_placeholder=_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_0123456789_
export prefix="/${prefix_placeholder}"
export maxjobs=4
export pkgdir=${SPM_ORIGIN}/pkgs
mkdir -p ${pkgdir}
source ${SPM_ORIGIN}/include/9999-template.sh
function spm_rpath_nearest() {
local cwd="$(pwd)"
local start=$(dirname $(spm_abspath ${1}))
local result=
# Jump to location of file
cd "$(dirname ${start})"
# Scan upward until we find a "lib" directory with shared libraries
# OR when:
# - Top of filesystem is reached (pretty much total failure [missing local dep])
# - Top of active environment is reached (post installation)
# - Top of default installation prefix is reached (during packaging)
while [[ $(pwd) != / ]]
do
result+="../"
if [[ -d lib ]] || [[ $(pwd) == ${SPM_ENV} ]] || [[ $(pwd) == *${prefix} ]]; then
lib_count=$(find . -type f \( -name '*.so' -o -name '*.dylib' \) | wc -l)
if (( lib_count )); then
result+="lib"
break
elif [[ $(pwd) == *${prefix} ]] && [[ ! -d lib ]]; then
result+="lib"
break;
fi
fi
cd ..
done
# Sanitize: removing double-slashes (if any)
result=${result/\/\//\/}
# Return to where we were instantiated
cd "${cwd}"
echo "${result}"
}
function spm_gen_package_rpath() {
local rpath_orig
local rpath_new
# Assimilate all file paths that contain an RPATH
for path in $(find . -type f -not -name '.SPM-*')
do
readelf -d "${path}" 2>/dev/null | grep RPATH &>/dev/null
if (( $? )); then
continue
fi
rpath_orig="$(readelf -d "${path}" | grep RPATH | awk -F'[][]' '{ print $2 }')"
rpath_new='$ORIGIN/'"$(spm_rpath_nearest ${path})"
echo "${path}: ${rpath_orig} -> ${rpath_new}"
patchelf --set-rpath "${rpath_new}" "${path}"
done
}
function spm_gen_package_prefixes() {
echo "Generating build prefix manifest"
# Create record files
>${SPM_PREFIX_BIN}
>${SPM_PREFIX_TEXT}
# Assimilate file path for anything containing our prefix
local count_text=0
local count_bin=0
local count_total=0
local prefixes=(
${prefix}
${build_runtime}
${build_root}
${destdir}
)
for pkg_prefix in "${prefixes[@]}"; do
for path in $(find . -type f -not -name ".SPM_*"); do
# Check for prefix
grep -l "${pkg_prefix}" "${path}" &>/dev/null
# Prefix present? (0: yes, 1: no)
if (( $? )); then
continue
fi
# Get file type
local mimetype="$(file -i ${path} | awk -F': ' '{ print $2 }')"
local outfile
# Record prefix data
if [[ ${mimetype} = *text/* ]]; then
outfile=${SPM_PREFIX_TEXT}
(( count_text++ ))
else
outfile=${SPM_PREFIX_BIN}
(( count_bin++ ))
fi
echo "#${pkg_prefix}" >> "${outfile}"
echo "${path}" >> "${outfile}"
done
done
count_total=$(( count_text + count_bin ))
if (( ${count_total} )); then
echo "Text: ${count_text}"
echo "Binary: ${count_bin}"
echo "Total: ${count_total}"
else
echo "No prefixes detected"
fi
}
function spm_gen_package_depends() {
echo "Generating dependency manifest"
local outfile="${SPM_DEPENDS}"
>${outfile}
for dep in "${depends[@]}"; do
echo "${dep}" >> "${outfile}"
done
}
_spm_install_depends=()
_spm_install_seen=()
function spm_install() {
local pkg="$(pkg_match $1)"
if [[ ! -f ${pkg} ]]; then
echo "Package not found: ${pkg}" >&2
exit 1
fi
local destroot="$2"
if [[ -z ${destroot} ]]; then
echo "destination root undefined" >&2
exit 1
elif [[ ! -d ${destroot} ]]; then
mkdir -p "${destroot}"
fi
# extract package into temp directory
local pkgtmp=$(mktemp -d ${TMPDIR}/spm.XXXX)
pushd "${pkgtmp}" &>/dev/null
echo "Unpacking: ${pkg}"
tar xf "${pkg}"
local prefix_base
# relocate binaries
if [[ -f ${SPM_PREFIX_BIN} ]]; then
while read path; do
if [[ -z $path ]]; then
continue
elif [[ $path =~ ^#.* ]]; then
prefix_base="${path#\#}"
continue
fi
if [[ ! -f $path ]]; then
echo "WARNING: ${path} does not exist!" >&2
continue
fi
if [[ $path =~ .pyc$ ]]; then
continue
fi
if (( $SPM_VERBOSE )); then
echo "Relocating binary paths: ${path}"
fi
${SPM_PROG_RELOC} "${prefix_base}" "${destroot}" "${path}" "${path}" >/dev/null
done < "${SPM_PREFIX_BIN}"
rm -f ${SPM_PREFIX_BIN}
fi
prefix_base=""
# relocate text
if [[ -f ${SPM_PREFIX_TEXT} ]]; then
while read path; do
if [[ -z $path ]]; then
continue
elif [[ $path =~ ^# ]]; then
prefix_base="${path#\#}"
continue
fi
if [[ ! -f $path ]]; then
echo "WARNING: ${path} does not exist!" >&2
continue
fi
if [[ $path =~ .pyc$ ]]; then
continue
fi
if (( $SPM_VERBOSE )); then
echo "Relocating text paths: ${path}"
fi
sed -i -e "s|${prefix_base}|${destroot}|g" "${path}"
done < "${SPM_PREFIX_TEXT}"
rm -f ${SPM_PREFIX_TEXT}
fi
# service package dependencies
if [[ -f ${SPM_DEPENDS} ]]; then
if [[ -z ${_spm_install_depends} ]]; then
_spm_install_depends=($(cat ${SPM_DEPENDS}))
else
_spm_install_depends+=($(cat ${SPM_DEPENDS}))
fi
for dep in "${_spm_install_depends[@]}"; do
# Track dependencies we have already processed to avoid infinite recursion
if [[ "${_spm_install_seen[@]}" =~ "$dep" ]]; then
# Pop dependency and do nothing
_spm_install_depends=("${_spm_install_depends[@]:1}")
continue
fi
# Pop dependency and process it
_spm_install_depends=("${_spm_install_depends[@]:1}")
# Stop processing when the array is totally empty
if (( ${#_spm_install_depends[@]} < 0 )); then
break
fi
_spm_install_seen+=("${dep}")
spm_install "${dep}" "${destroot}"
done
rm -f "${SPM_DEPENDS}"
fi
# install package
echo "Installing: ${pkg}"
rsync -a ./ "${destroot}"
popd &>/dev/null
if [[ -d $pkgtmp ]]; then
rm -rf "${pkgtmp}"
fi
}
function builder() {
for build_script in ${build_scripts}; do
if [[ ${build_script} =~ ^#.* ]]; then
continue
fi
if [[ ! -f ${build_script} ]]; then
build_script=$(spm_abspath ${build_script}/build.sh)
build_script_root=$(dirname ${build_script})
else
build_script_root=$(spm_abspath ${build_script})
fi
local output_dir="${SPM_ORIGIN}/output/$(basename ${build_script_root})"
export build_sources="${SPM_ORIGIN}/sources/$(basename ${build_script_root})"
export build_root="${output_dir}/buildroot"
export build_runtime="${output_dir}/runtime"
export destdir="${output_dir}/root"
export CC=gcc
export CXX=g++
export LD_LIBRARY_PATH="${build_runtime}/lib"
export CFLAGS="-I${build_runtime}/include"
export CPPFLAGS="${CFLAGS}"
export LDFLAGS="-L${build_runtime}/lib -Wl,-rpath="'\$$ORIGIN'"/../lib"
echo "Building: ${build_script}"
if [[ ! -f ${build_script} ]]; then
echo "${build_script} does not exist, check ${build_order}" >&2
exit 1
fi
if [[ ! -d ${build_sources} ]]; then
mkdir -p ${build_sources}
fi
if [[ -d ${build_root} ]]; then
rm -rf ${build_root}
fi
mkdir -p ${build_root}
if [[ -d ${build_runtime} ]]; then
rm -rf ${build_runtime}
fi
mkdir -p ${build_runtime}
if [[ -d ${destdir} ]]; then
rm -rf ${destdir}
fi
mkdir -p ${destdir}
export PATH="${build_runtime}/bin:${build_runtime}/sbin:$OLD_PATH"
export PKG_CONFIG_PATH="${build_runtime}/lib/pkgconfig"
pushd ${build_root} &>/dev/null
# Assimilate build script's contents
source ${build_script}
if [[ -f ${pkgdir}/${name}-${version}-${revision}.tar.gz ]]; then
echo "Skipping: Package exists"
popd &>/dev/null
continue
fi
# Download source files
for url in "${sources[@]}"; do
archive_path="${build_sources}/$(basename $url)"
if [[ -f ${archive_path} ]]; then
echo "Cached source: ${archive_path}"
continue
fi
echo "Downloading source: ${url}"
curl -L "$url" > "${archive_path}"
done
# Copy source files to build root
for archive in "${build_sources}"/*; do
echo "Copying source to build root: ${archive}"
cp -a "${archive}" "${build_root}"
done
# Make base package implicit
if [[ -z $disable_base ]]; then
base_package=$(pkg_match "base")
if [[ -n ${base_package} ]]; then
build_depends+=(${base_package})
depends+=(${base_package})
fi
fi
# Install build dependencies
for dep in "${build_depends[@]}"; do
echo "Build depends on: ${dep}"
pkg=$(pkg_match "${dep}")
if [[ -z ${pkg} ]]; then
echo "Package not found" >&2
exit 1
fi
spm_install "${pkg}" "${build_runtime}"
done
# Install package dependencies
for dep in "${depends[@]}"; do
echo "Depends on: ${dep}"
pkg=$(pkg_match "${dep}")
if [[ -z ${pkg} ]]; then
echo "Package not found" >&2
exit 1
fi
spm_install "${pkg}" "${build_runtime}"
done
# Rehash the runtime environment
hash -r
# No failures allowed in predefined build stages
set -e
prepare
build
package
set +e
# Post-process the installation root (i.e. make install DESTDIR=${destdir})
if [[ -d ${destdir} ]]; then
pushd ${destdir} &>/dev/null
if [[ -d ${destdir}/${prefix} ]]; then
pushd ${destdir}/${prefix} &>/dev/null
fi
spm_gen_package_rpath
spm_gen_package_prefixes
spm_gen_package_depends
pkg="${pkgdir}/${name}-${version}-${revision}.tar.gz"
echo "Creating package: ${pkg}"
pwd
ls -la
tar cfz "${pkg}" .SPM_* .
if [[ -d ${destdir}/${prefix} ]]; then
popd &>/dev/null
fi
popd &>/dev/null
fi
popd &>/dev/null
done
}
function pkg_match() {
if [[ -z $1 ]]; then
echo "pkg_match: missing argument, package" >&2
exit 1
fi
match=$(find "${pkgdir}" -type f -regex ".*${1}\-?[0-9]+?.*" 2>/dev/null | sort | head -n 1)
echo "${match}"
}
function installer() {
local root=""
local packages=()
while [[ $# != 0 ]]; do
case "$1" in
--verbose|-v)
SPM_VERBOSE=1
;;
--root|-r)
root="$2"
shift
;;
-*|--*)
echo "installer: unknown argument: $1" >&2
exit 1
;;
*)
# "Most-likely" match the requested package by name
p=$(basename $(pkg_match "${1}"))
packages+=("$p")
;;
esac
shift
done
if [[ -z $root ]]; then
echo "missing required argument: --root {destination}" >&2
exit 1
fi
export PATH="${root}/bin:${PATH}"
for pkg in "${packages[@]}"; do
spm_install "${pkg}" "${root}"
done
}
function usage() {
echo "Usage: $0 [build|install] {package}
Options:
--help (-h) this message
--verbose (-v) increase verbosity
Commands:
build build a package
install install a package
Positional arguments:
package package to interact with
"
}
if [[ $# < 2 ]]; then
usage
exit 1
fi
fn=
args=
while [[ $# != 0 ]]; do
case "$1" in
--verbose|-v)
SPM_VERBOSE=1
;;
--help|-h)
usage
exit 0
;;
build)
fn=builder
if [[ $2 != "all" ]]; then
build_scripts="$2"
if [[ $build_scripts =~ .*/build.sh ]]; then
build_scripts=$(dirname ${build_scripts})
fi
fi
shift 2
break
;;
install)
fn=installer
shift
args=("$@")
break
;;
*)
echo "unknown argument: $1"
exit 1
;;
esac
shift
done
$fn "${args[@]}"
|