summaryrefslogtreecommitdiff
path: root/build-chroot
blob: dd0edee750e8a6fd9ea6c849486002ffd4985cf3 (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
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
#!/bin/bash
VERSION=0.1
find_libs=$(dirname "$(readlink -f "$0")")/find-libs
usage() {
	cat <<HELP
Copies binaries and their dependencies for chroot usage.

Copyright (C) 2012 Peter Wu
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Written by Peter Wu <lekensteyn@gmail.com>

Usage: $0 [OPTION] file...

Options:
  -n, --dry-run           Do not copy files, just print the action that would be
                            performed.
  -d DIR, --destdir DIR   Destination directory for the files.
  -c, --continue          Continue even if a given file cannot be located.
  -h, --help              Print this help text, then exit.
  --version               Prints the version of this program, then exit.

Environment:
  DESTDIR   If --destdir is not given, this environment variable is sought. If
            this variable does not exist either, a temporary directory is
            created which is printed on termination.
HELP
}

opts=`getopt --name "$0" -o nd:ch \
	--longoptions dry-run,destdir:,continue,help,version \
	-- "$@"`
if [ $? != 0 -o -z "$opts" ]; then
	usage
	exit 1
fi
eval set -- "$opts"

DRY_RUN=false
CONT_ON_ERR=false
while :; do
	case "$1" in
	-n|--dry-run) DRY_RUN=true ;;
	-d|--destdir) DESTDIR="$2" ; shift ;;
	-c|--continue) CONT_ON_ERR=true ;;
	-h|--help) usage; exit 0 ;;
	--version) echo "$VERSION" ; exit 0 ;;
	--) shift; break ;;
	*) echo "Internal error!"; exit 1 ;;
	esac
	shift
done

if [ $# -eq 0 ]; then
	echo "Need at least one binary or library to isolate."
	echo "Try '$0 --help' for more information."
	exit 1
fi

resolve_interpreter() {
	local file="$1"
	local interp
	interp=$(head -c 256 "$file" | awk -F '[#! \t]+' 'NR==1 && /^#!/{print $2;exit}')
	[ -n "$interp" ] || return

	echo "$interp"

	# /usr/bin/env python2 -> python2
	case "$interp" in
	env|/usr/bin/env)
		awk -F '[#! \t]+' '{
			for (i=3;i<NF;i++)
				if ($i !~ /^-/) {
					print $i;exit
				}
		}'
		;;
	esac
}

errors=0
subjects=()
for subject; do
	realsubject="$subject"
	#realsubject="$(readlink -e "$subject")"
	[ -e "$realsubject" ] || realsubject="$(which "$subject" 2>/dev/null)" || true
	if [ -n "$realsubject" ]; then
		subjects[${#subjects[@]}]="$realsubject"
		for extra in $(resolve_interpreter "$realsubject"); do
			if path="$(which "$extra" 2>/dev/null)"; then
				subjects[${#subjects[@]}]="$path"
			else
				echo "Missing program: $extra"
				((errors++))
			fi
		done
	else
		echo "Missing file: $subject"
		((errors++))
	fi
done

if [ $errors -ne 0 ]; then
	echo "$errors files could not be found"
	if ! $CONT_ON_ERR; then
		echo "Aborting. To ignore this, pass the '--continue' option."
		exit 255
	fi
fi

info="$("$find_libs" "${subjects[@]}")"
echo "$info"
count=${info%% *}
filelist="${info#* }"

try_install() {
	local file="$1"
	# target without leading ../../etc
	local target="$DESTDIR/$(sed 's,\(\.\./\)*,,' <<<"$file")"
	if [ ! -e "$target" ]; then
		if $DRY_RUN; then
			echo "Installing $file"
		else
			install -v -D "$file" "$target"
		fi
	fi
}
if [ $count -gt 0 ]; then
	tmpdir=
	if [ -z "$DESTDIR" ]; then
		tmpdir="$(mktemp -d)"
		DESTDIR="$tmpdir"
	fi
	umask 002
	while read file; do
		try_install "$file"
	done < "$filelist"
	[ -z "$tmpdir" ] || rmdir --ignore-fail-on-non-empty "$tmpdir"
else
	echo "No files to copy"
fi
for subject in "${subjects[@]}"; do
	try_install "$subject"
done
rm "$filelist"