-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-sysroot
executable file
·211 lines (183 loc) · 4.47 KB
/
build-sysroot
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
#!/bin/sh -eu
. $BULB_SH_UTILS
usage() {
echo "
Usage: ${0##*/} [options] executable...
options:
-i In system's root directory
-o Out system's root directory
-m Install missing libraries
-h Print this help and exit
"
}
list_append() {
l_list=${1:-}
shift
[ -z "$l_list" ] || printf '%s\n' "$l_list"
[ $# -eq 0 ] || printf '%s\n' "$@"
}
check_arch() {
l_bin_path=${1:-}
l_desired_arch=${2:-}
[ -n "$l_bin_path" ] || croak The binary path parameter is missing
l_file_arch=$(file -b "$l_bin_path" |sed 's/[^,]*, \([^,]*\).*/\1/')
l_arch=
case $l_file_arch in
ARM)
l_arch=armhf
;;
'ARM aarch64')
l_arch=arm64
;;
x86-64)
l_arch=amd64
;;
*)
croak $l_file_arch is not a supported architecture
;;
esac
[ -z "$l_desired_arch" ] || [ "$l_arch" = "$l_desired_arch" ] || croak "The binary $l_bin_path is of architecture $l_arch which is not desired (desire=$l_desired_arch)"
echo $l_arch
}
find_pkgs() {
l_arch=${1:-}
[ -n "$l_arch" ] || croak The architecture parameter is missing
shift
dpkg --add-architecture $l_arch
apt-file -a $l_arch update >/dev/null
for l_file in "$@"; do
# Find and choose the package with the shortest name that contains the needed library
apt-file -l -a $l_arch find "$l_file" |{
l_pkg=
while read l_line; do
l_pkg=${l_pkg:=$l_line}
[ ${#l_line} -lt ${#l_pkg} ] && l_pkg=$l_line
done
if [ -n "$l_pkg" ]; then
printf '%s ' $l_pkg
else
bark no package containing $l_file
fi
}
done
}
get_missing_libs() {
local l_bin_path=${1:-}
local l_root_dir=${2:+-r$2}
[ -n "$l_bin_path" ] || croak The binary path parameter is missing
ld-trace "$l_root_dir" "$l_bin_path" |{
while read l_line; do
lib_path=${l_line#*=> }
lib=${l_line% =>*}
[ "$lib_path" != 'not found' ] || printf '%s\n' "$lib"
done
}
}
get_lib_paths() {
local l_bin_path=${1:-}
local l_root_dir=${2:+-r$2}
[ -n "$l_bin_path" ] || croak The binary path parameter is missing
ld-trace "$l_root_dir" "$l_bin_path" |{
while read l_line; do
lib_path=${l_line#*=> }
lib=${l_line% =>*}
[ "$lib_path" = 'not found' ] || [ "${lib_path%% \'*}" = 'removed' ] || [ "$lib_path" = "$lib" ] || {
lib_path=${lib_path% (*}
case $lib_path in
/*)
lib_path=".$lib_path"
;;
*)
;;
esac
printf '%s\n' "$lib_path"
}
done
}
}
install_missing() {
l_root_dir=${1:-}
l_arch=${2:-}
[ -n "$l_root_dir" ] || croak The root directory parameter is missing
[ -n "$l_arch" ] || croak The architecture parameter is missing
shift 2
l_missing_libs=
for l_bin_path in "$@"; do
l_missing_libs="$(get_missing_libs "$l_bin_path" "$l_root_dir")
$l_missing_libs"
done
[ -n "$l_missing_libs" ] && install_libs_pkgs "$l_root_dir" $l_arch $l_missing_libs
}
install_libs_pkgs() {
l_root_dir=${1:-}
l_arch=${2:-}
[ -n "$l_root_dir" ] || croak The root directory parameter is missing
[ -n "$l_arch" ] || croak The architecture parameter is missing
shift 2
local l_pkgs=$(find_pkgs $l_arch $*)
[ -z "$l_pkgs" ] || install-pkgs -a $l_arch -r "$l_root_dir" $l_pkgs >/dev/null
}
in_sysroot=
out_sysroot=
install_missing=
while getopts 'i:o:mh' opt; do
case $opt in
i)
in_sysroot=$(get_absolut_path $OPTARG)
;;
o)
out_sysroot=$(get_absolut_path $OPTARG)
;;
m)
install_missing=y
;;
h)
usage
exit
;;
*)
usage
exit 1
;;
esac
done
shift $((OPTIND - 1))
[ $# -gt 0 ] || {
usage
exit
}
tmp_in_sysroot=
[ -n "$in_sysroot" ] || {
tmp_in_sysroot=$(mktemp -d)
in_sysroot=$tmp_in_sysroot
}
out_sysroot=${out_sysroot:-$(mktemp -d)}
[ -d "$out_sysroot" ] || install -d $out_sysroot 2>/dev/null || croak $out_sysroot is not a directory
echo out_sysroot: $out_sysroot
IFS="$(printf '\n\t')"
bin_paths=
arch=
rtld=
for bin in "$@"; do
bin_path=$(get_absolut_path $bin)
arch=$(check_arch $bin_path $arch)
[ -n "$rtld" ] || rtld=$(file -b "$bin_path" |sed 's/.*, interpreter \([^,]*\).*/\1/')
bin_paths=$(list_append "$bin_paths" "$bin_path")
done
[ -z "$install_missing" ] || install_missing "$in_sysroot" $arch $bin_paths
lib_paths=
for bin_path in $bin_paths; do
bin_lib_paths=$(get_lib_paths "$bin_path" "$in_sysroot")
lib_paths=$(list_append "$lib_paths" "$bin_lib_paths")
done
lib_paths=$(echo "$lib_paths" |sort -u)
[ -z "$lib_paths" ] || {
cd "$in_sysroot"
for lib_path in $lib_paths; do
cp --parents "$lib_path" "$out_sysroot"
done
}
cp --parents $rtld "$out_sysroot"
install -D -t "$out_sysroot"/usr/bin/ $bin_paths
[ -z "$tmp_in_sysroot" ] || rm -rf $tmp_in_sysroot
echo $out_sysroot