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
#!/bin/sh
# compatibility layer
# add realpath if missed
if ! which realpath 2>/dev/null >/dev/null ; then
realpath()
{
[ -n "$*" ] || return
readlink -f "$@"
}
fi
# add subst if missed
if ! which subst 2>/dev/null >/dev/null ; then
subst()
{
sed -i -e "$@"
}
fi
# Remove file from the file system and from spec
# Usage: remove_file <path_to_file>
remove_file()
{
local file="$1"
[ -n "$file" ] || return
[ -e "$BUILDROOT$file" ] || [ -L "$BUILDROOT$file" ] || return
rm -v "$BUILDROOT$file"
subst "s|.*$file.*||" $SPEC
}
# Remove dir (recursively) from the file system and from spec
remove_dir()
{
local file="$1"
[ -n "$file" ] || return
[ -d "$BUILDROOT$file/" ] || return
# canonicalize
file="$(echo "$file" | sed -e 's|/*$||')"
echo "Removing $file dir ..."
rm -r "$BUILDROOT$file/"
subst "s|.*$file/.*||" $SPEC
subst "s|.*$file\"$||" $SPEC
subst "s|.*$file$||" $SPEC
}
has_space()
{
[ "${1/ /}" != "$1" ]
}
has_wildcard()
{
[ "${1/\*/}" != "$1" ]
}
# Add file to spec (if missed)
# Usage: pack_file <path_to_file>
pack_file()
{
local file="$1"
[ -n "$file" ] || return
grep -q "^$file$" $SPEC && return
grep -q "\"$file\"" $SPEC && return
has_space "$file" && file="\"$file\""
subst "s|%files|%files\n$file|" $SPEC
}
# Add dir (only dir) to spec (if missed)
# Usage: pack_dir <path_to_dir>
pack_dir()
{
local file="$1"
[ -n "$file" ] || return
grep -q "^%dir[[:space:]]$file/*$" $SPEC && return
grep -q "^%dir[[:space:]]\"$file/*\"$" $SPEC && return
has_space "$file" && file="\"$file\""
subst "s|%files|%files\n%dir $file|" $SPEC
}
add_bin_link_command()
{
local name="$1"
local target="$2"
[ -n "$name" ] || name="$PRODUCT"
[ -n "$target" ] || target="$PRODUCTDIR/$name"
[ -e "$BUILDROOT/usr/bin/$name" ] && return
mkdir -p $BUILDROOT/usr/bin/
ln -s "$target" "$BUILDROOT/usr/bin/$name" || return
pack_file "/usr/bin/$name"
}
add_bin_exec_command()
{
local name="$1"
local target="$2"
[ -n "$name" ] || name="$PRODUCT"
[ -n "$target" ] || target="$PRODUCTDIR/$name"
[ -e "$BUILDROOT/usr/bin/$name" ] && return
mkdir -p $BUILDROOT/usr/bin/
cat <<EOF > "$BUILDROOT/usr/bin/$name"
#!/bin/sh
exec "$target" "\$@"
EOF
chmod 0755 "$BUILDROOT/usr/bin/$name"
pack_file "/usr/bin/$name"
}
# move files to $PRODUCTDIR
move_to_opt()
{
local sdir rdir i
mkdir -p "$BUILDROOT$PRODUCTDIR/"
if [ -z "$1" ] ; then
local from
from="/usr/share/$PRODUCTCUR"
[ -d "$BUILDROOT$from" ] || from="/usr/share/$PRODUCT"
[ -d "$BUILDROOT$from" ] || from="/usr/lib/$PRODUCT"
sdir="$BUILDROOT$from"
elif has_space "$1" ; then
sdir="$BUILDROOT$1"
else
sdir=''
for i in $* ; do
# workaround for wildcards in from
sdir="$(echo $BUILDROOT$i)"
[ -d "$sdir" ] && break
done
fi
[ -d "$sdir" ] || return 1 #fatal "Can't find any dir from $from list"
rdir="$(echo "$sdir" | sed -e "s|^$BUILDROOT||")"
[ -n "$rdir" ] || return 1 #fatal "Can't resolve $from in $BUILDROOT"
[ -d "$BUILDROOT$rdir" ] || return 1 #fatal "Can't resolve $from in $BUILDROOT"
mv "$BUILDROOT$rdir"/* "$BUILDROOT$PRODUCTDIR/"
subst "s|$rdir|$PRODUCTDIR|g" $SPEC
}
fix_chrome_sandbox()
{
local sandbox="$1"
# Set SUID for chrome-sandbox if userns_clone is not supported
# CHECKME: Also userns can be enabled via sysctl-conf-userns package install
userns_path='/proc/sys/kernel/unprivileged_userns_clone'
userns_val="$(cat $userns_path 2>/dev/null)"
[ "$userns_val" = '1' ] && return
[ -n "$sandbox" ] || sandbox="$PRODUCTDIR/chrome-sandbox"
[ -e "$BUILDROOT$sandbox" ] || return 0
chmod -v 4711 "$BUILDROOT$sandbox"
}
filter_from_requires()
{
local i
for i in "$@" ; do
subst "1i%filter_from_requires /^$i.*/d" $SPEC
done
}
# ignore embedded libs
drop_embedded_reqs()
{
filter_from_requires "libGLESv2.so()" "libEGL.so()" "libffmpeg.so()"
}
if [ -n "$PRODUCT" ] ; then
[ -n "$PRODUCTCUR" ] || PRODUCTCUR="$PRODUCT"
[ -n "$PRODUCTDIR" ] || PRODUCTDIR="/opt/$PRODUCTCUR"
[ -d "$BUILDROOT$PRODUCTDIR" ] && pack_dir "$PRODUCTDIR"
fi