-
Notifications
You must be signed in to change notification settings - Fork 25
/
make.sh
executable file
·357 lines (313 loc) · 12.9 KB
/
make.sh
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
#!/bin/bash
# Project OEM-GSI Porter by Erfan Abdi <[email protected]>
# Project Treble Experience by Hitalo <[email protected]> and Velosh <[email protected]>
# Core variables, do not edit.
LOCALDIR=`cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd`
tempdirname="tmp"
tempdir="$LOCALDIR/$tempdirname"
builddir="$LOCALDIR/build"
prebuiltdir="$LOCALDIR/prebuilt"
romsdir="$LOCALDIR/roms"
scriptsdir="$LOCALDIR/scripts"
systemdir="$tempdir/system"
toolsdir="$LOCALDIR/tools"
vendordir="$LOCALDIR/vendor"
sourcepath=$1
romtype=$2
outputtype=$3
novndk=$4
gapps=$5
# Util functions
usage() {
echo "Usage: $0 <Path to GSI system> <Firmware type> <Output type> <Extra VNDK> <GApps> [Output Dir]"
echo -e "\tPath to GSI system: Mount GSI and set mount point"
echo -e "\tFirmware type: Firmware mode"
echo -e "\tOutput type: AB or Aonly"
echo -e "\tExtra VNDK: Use it to not include extra VNDK, with false & true args"
echo -e "\tGApps: Push GApps"
echo -e "\tOutput Dir: set output dir"
}
# Need at least 5 args
if [ "$5" == "" ]; then
echo "-> ERROR!"
echo " - Enter all needed parameters"
usage
exit 1
fi
# Check output arg
if [ "$6" == "" ]; then
echo "-> Create out dir"
outdirname="out"
outdir="$LOCALDIR/$outdirname"
mkdir -p "$outdir"
else
outdir="$6"
fi
# Check if have special name.
if [[ $romtype == *":"* ]]; then
romtypename=`echo "$romtype" | cut -d ":" -f 2`
romtype=`echo "$romtype" | cut -d ":" -f 1`
else
romtypename=$romtype
fi
# Init variables for ROM check
flag=false
roms=("$LOCALDIR"/roms/*/*)
# Check ROM type
for dir in "${roms[@]}"
do
rom=`echo "$dir" | rev | cut -d "/" -f 1 | rev`
if [ "$rom" == "$romtype" ]; then
flag=true
fi
done
# If flag variable is false, then the ROM isn't even supported.
if [ "$flag" == "false" ]; then
echo "-> Heyaa! $romtype is not a supported ROM! Please review the list of supported ROMs."
exit 1
fi
# Re-init variable for layout check
flag=false
# Check layout type
case "$outputtype" in
*"AB"*) flag=true ;;
*"Aonly"*) flag=true ;;
esac
# If flag variable is false, then the layout type isn't even supported.
if [ "$flag" == "false" ]; then
echo "-> Hey, $outputtype is not supported type, supported types:"
echo " - AB"
echo " - Aonly"
exit 1
fi
# Detect Source type, AB or not
sourcetype="Aonly"
if [[ -e "$sourcepath/system" ]]; then
sourcetype="AB"
fi
# GSI process message
echo "-> Initializing process, creating temporary directory..."
rm -rf $tempdir
mkdir -p "$systemdir"
# Check layout type (I guess I'll drop it soon.)
if [ "$sourcetype" == "Aonly" ]; then
echo "-> Warning: Aonly source detected, using P AOSP rootdir"
cd "$systemdir"
tar xf "$prebuiltdir/ABrootDir.tar"
cd "$LOCALDIR"
echo "-> Making copy of source rom to temp..."
( cd "$sourcepath" ; sudo tar cf - . ) | ( cd "$systemdir/system" ; sudo tar xf - ) &> /dev/null
cd "$LOCALDIR"
sed -i "/ro.build.system_root_image/d" "$systemdir/system/build.prop"
sed -i "/ro.build.ab_update/d" "$systemdir/system/build.prop"
echo "# That image hasn't been built as SAR" >> "$systemdir/system/build.prop"
echo "ro.build.system_root_image=false" >> "$systemdir/system/build.prop"
echo "" >> "$systemdir/system/build.prop"
else
echo "-> Making copy of source rom to temp..."
( cd "$sourcepath" ; sudo tar cf - . ) | ( cd "$systemdir" ; sudo tar xf - ) &> /dev/null
cd "$LOCALDIR"
sed -i "/ro.build.system_root_image/d" "$systemdir/system/build.prop"
sed -i "/ro.build.ab_update/d" "$systemdir/system/build.prop"
echo "# That image has been built as SAR" >> "$systemdir/system/build.prop"
echo "ro.build.system_root_image=true" >> "$systemdir/system/build.prop"
echo "" >> "$systemdir/system/build.prop"
fi
# Detect is the src treble ro.treble.enabled=true
istreble=`cat $systemdir/system/build.prop | grep ro.treble.enabled | cut -d "=" -f 2`
if [[ ! "$istreble" == "true" ]]; then
if [ ! -f "$LOCALDIR/working/vendor.img" ]; then
echo "-> Hey, the source is not treble supported."
exit 1
else
echo "-> Treble source detected but with disabled treble prop"
fi
fi
# Detect Source API level
if grep -q ro.build.version.release_or_codename $systemdir/system/build.prop; then
sourcever=`grep ro.build.version.release_or_codename $systemdir/system/build.prop | cut -d "=" -f 2`
else
sourcever=`grep ro.build.version.release $systemdir/system/build.prop | cut -d "=" -f 2`
fi
if [ $(echo $sourcever | cut -d "." -f 2) == 0 ]; then
sourcever=$(echo $sourcever | cut -d "." -f 1)
fi
# Re-init variable for Android version
flag=false
# Check Android version
case "$sourcever" in
*"9"*) flag=true ;;
*"10"*) flag=true ;;
*"11"*) flag=true ;;
*"12"*) flag=true ;;
*"Sv2"*) flag=true ;;
esac
# I need to say something?
if [ "$flag" == "false" ]; then
echo "-> $sourcever is not supported."
exit 1
fi
# Detect rom folder again
if [[ ! -d "$romsdir/$sourcever/$romtype" ]]; then
echo "-> $romtype is not a supported ROM for Android $sourcever. Please review the list of supported ROMs."
exit 1
fi
if [ "$sourcetype" == "Aonly" ]; then
if [[ "$sourcever" == "11" || "$sourcever" == "12" ]]; then
echo "-> Building a GSI as system-as-system is not possible in Android R/S, reconsider building with AB ramdisk."
exit 1
fi
fi
# Detect arch
if [[ ! -f "$systemdir/system/lib64/libandroid.so" ]]; then
echo "-> A64/ARM32 ROM detected! Can't build due missing A64/ARM32 VNDK/Libraries for it."
exit 1
fi
# Init date var first
date=`date +%Y%m%d%s`
hashdate=`echo $date | md5sum | awk '{print substr($1,1,10)}'`
# Get build display id & model
displayid=$(grep -oP "(?<=^ro.build.display.id=).*" -hs $systemdir/system/build.prop | head -1)
[[ -z "$displayid" ]] && displayid=$(grep -oP "(?<=^ro.system.build.id=).*" -hs $systemdir/system/build.prop | head -1)
[[ -z "$displayid" ]] && displayid=$(grep -oP "(?<=^ro.build.id=).*" -hs $systemdir/system/build.prop | head -1)
displayid=`echo $displayid | sed "s/ /-/g"`
codename=$(grep -oP "(?<=^ro.product.vendor.device=).*" -hs "$LOCALDIR/working/vendor/build.prop" | head -1)
[[ -z "${codename}" ]] && codename=$(grep -oP "(?<=^ro.product.system.device=).*" -hs $systemdir/system/build.prop | head -1)
[[ -z "${codename}" ]] && codename=$(grep -oP "(?<=^ro.product.device=).*" -hs $systemdir/system/build.prop | head -1)
[[ -z "${codename}" ]] && codename=Generic
# System tree thing
outputtreename="trebleExp[$romtypename]-[$codename]-[GSI+SGSI]-[$displayid]-[$sourcever]-[$date-$hashdate]-System-Tree.txt"
outputtree="$outdir/$outputtreename"
if [ ! -f "$outputtree" ]; then
echo "-> Generating the system tree..."
tree $systemdir >> "$outputtree" 2> "$outputtree"
echo " - Done!"
fi
# Check if GApps has been requested
if [[ $gapps == "false" ]]; then
echo "-> Google Apps supply was not requested, ignore."
else
echo "-> Google Apps supply was requested, copying into system..."
$vendordir/google/make.sh "$systemdir/system" "$sourcever" 2>/dev/null
fi
# Debloat
echo "-> De-bloating"
$romsdir/$sourcever/$romtype/debloat.sh "$systemdir/system" 2>/dev/null
$romsdir/$sourcever/$romtype/$romtypename/debloat.sh "$systemdir/system" 2>/dev/null
$builddir/debloat/$sourcever/debloat.sh "$systemdir/system" 2>/dev/null # "Common" debloat for 9, 10, 11 & 12 (It's useful for Generic GSI)
# Start patching
echo "-> Patching started..."
$scriptsdir/fixsymlinks.sh "$systemdir/system" 2>/dev/null
$scriptsdir/nukeABstuffs.sh "$systemdir/system" 2>/dev/null
$builddir/patches/common/make.sh "$systemdir/system" "$romsdir/$sourcever/$romtype" "$sourcever"
# Check if extra VNDK has been requested
if [[ $novndk == "false" ]]; then
echo "-> Extra Vendor Native Development Kit supply was requested, copying into system..."
$vendordir/vndk/make$sourcever.sh "$systemdir/system" 2>/dev/null
else
echo "-> Extra Vendor Native Development Kit supply not requested, ignore."
fi
# Patching moment
$builddir/gsi/$sourcever/make.sh "$systemdir/system" "$romsdir/$sourcever/$romtype" 2>/dev/null
$builddir/gsi/$sourcever/makeroot.sh "$systemdir" "$romsdir/$sourcever/$romtype" 2>/dev/null
$builddir/gsi/common/make.sh "$systemdir/system" "$romsdir/$sourcever/$romtype" 2>/dev/null
[[ -f $LOCALDIR/tmp/FATALERROR ]] && exit 1
$romsdir/$sourcever/$romtype/make.sh "$systemdir/system" 2>/dev/null
$romsdir/$sourcever/$romtype/makeroot.sh "$systemdir" 2>/dev/null
if [ ! "$romtype" == "$romtypename" ]; then
$romsdir/$sourcever/$romtype/$romtypename/make.sh "$systemdir/system" 2>/dev/null
$romsdir/$sourcever/$romtype/$romtypename/makeroot.sh "$systemdir" 2>/dev/null
fi
if [ "$outputtype" == "Aonly" ] && [ ! "$romtype" == "$romtypename" ]; then
$romsdir/$sourcever/$romtype/$romtypename/makeA.sh "$systemdir/system" 2>/dev/null
fi
if [ "$outputtype" == "Aonly" ]; then
$builddir/gsi/$sourcever/makeA.sh "$systemdir/system" 2>/dev/null
$romsdir/$sourcever/$romtype/makeA.sh "$systemdir/system" 2>/dev/null
fi
# Resign to AOSP keys
if [[ ! -e $romsdir/$sourcever/$romtype/$romtypename/DONTRESIGN ]]; then
if [[ ! -e $romsdir/$sourcever/$romtype/DONTRESIGN ]]; then
echo "-> Resigning to AOSP keys, just wait."
ispython2=`python -c 'import sys; print("%i" % (sys.hexversion<0x03000000))'`
if [ $ispython2 -eq 0 ]; then
python2=python2
else
python2=python
fi
$python2 $toolsdir/ROM_resigner/resign.py "$systemdir/system" $toolsdir/ROM_resigner/AOSP_security > $tempdir/resign.log 2>&1
$builddir/gsi/resigned/make.sh "$systemdir/system" 2>/dev/null
fi
fi
# Fixing environ
if [ "$outputtype" == "Aonly" ]; then
if [[ ! $(ls "$systemdir/system/etc/init/" | grep *environ*) ]]; then
echo "-> Generating environ.rc"
echo "# AUTOGENERATED FILE BY ERFANGSI TOOLS" > "$systemdir/system/etc/init/init.treble-environ.rc"
echo "on init" >> "$systemdir/system/etc/init/init.treble-environ.rc"
cat "$systemdir/init.environ.rc" | grep BOOTCLASSPATH >> "$systemdir/system/etc/init/init.treble-environ.rc"
cat "$systemdir/init.environ.rc" | grep SYSTEMSERVERCLASSPATH >> "$systemdir/system/etc/init/init.treble-environ.rc"
fi
fi
# Out info
outputname="trebleExp[$romtypename]-[$codename]-[GSI+SGSI]-[$displayid]-[$outputtype]-[$sourcever]-[$date-$hashdate]"
outputimagename="$outputname".img
outputtextname="$outputname".txt
outputvendoroverlaysname="trebleExp[$romtypename]-[$codename]-[GSI+SGSI]-[$displayid]-[$sourcever]-[$date-$hashdate]-VendorOverlays.tar.gz"
outputodmoverlaysname="trebleExp[$romtypename]-[$codename]-[GSI+SGSI]-[$displayid]-[$sourcever]-[$date-$hashdate]-ODMOverlays.tar.gz"
output="$outdir/$outputimagename"
outputvendoroverlays="$outdir/$outputvendoroverlaysname"
outputodmoverlays="$outdir/$outputodmoverlaysname"
outputinfo="$outdir/$outputtextname"
# Get info
$scriptsdir/getinfo.sh "$systemdir/system" > "$outputinfo"
# Getting system size and add approximately 5% on it just for free space
systemsize=`du -sk $systemdir | awk '{$1*=1024;$1=int($1*1.05);printf $1}'`
bytesToHuman() {
b=${1:-0}; d=''; s=0; S=(Bytes {K,M,G,T,P,E,Z,Y}iB)
while ((b > 1024)); do
d="$(printf ".%02d" $((b % 1024 * 100 / 1024)))"
b=$((b / 1024))
let s++
done
echo "$b$d ${S[$s]}"
}
echo "Raw Image Size: $(bytesToHuman $systemsize)" >> "$outputinfo"
echo "-> Creating Image (This may take a while to finish): $outputimagename"
# Use ext4fs to make image in P or older!
if [ "$sourcever" == "9" ]; then
useold="--old"
fi
# Build the GSI image
if [ ! -f "$romsdir/$sourcever/$romtype/build/file_contexts" ]; then
echo "-> Note: Custom security contexts not found for this ROM, errors or SELinux problem may appear"
$scriptsdir/mkimage.sh $systemdir $outputtype $systemsize $output false $useold > $tempdir/mkimage.log || rm -rf $output
else
echo "-> Note: Custom security contexts found!"
$scriptsdir/mkimage.sh $systemdir $outputtype $systemsize $output $romsdir/$sourcever/$romtype/build $useold > $tempdir/mkimage.log || rm -rf $output
fi
# Check if the output image has been built
if [ -f "$output" ]; then
# Builded
echo "-> Created image ($outputtype): $outputimagename | Size: $(bytesToHuman $systemsize)"
else
# Oops... Error found
echo "-> Error: Output image for $outputimagename ($outputtype) doesn't exists!"
exit 1
fi
# Overlays
if [ -f "$LOCALDIR/output/.tmp" ]; then
mv "$LOCALDIR/output/.tmp" "$outputvendoroverlays"
else
if [[ -d "$LOCALDIR/working/vendor/overlay" && ! -f "$outputvendoroverlays" ]]; then
mkdir -p "$LOCALDIR/output/vendorOverlays"
cp -frp working/vendor/overlay/* "output/vendorOverlays" >> /dev/null 2>&1
tar -zcvf "$outputvendoroverlays" "output/vendorOverlays" >> /dev/null 2>&1
rm -rf "output/vendorOverlays"
fi
fi
if [ -f "$LOCALDIR/output/.otmp" ]; then
mv "$LOCALDIR/output/.otmp" "$outputodmoverlays"
fi
echo "-> Done! Delete temporary folder."
rm -rf "$tempdir"