forked from tdm/android-scripts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathandroid-build
executable file
·266 lines (233 loc) · 5.76 KB
/
android-build
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
#!/bin/bash -e
# Acquire lock on source tree
parent=$(ps h -o comm $PPID)
if [ "$parent" != "flock" ]; then
exec flock ".lock" $0 "$@"
fi
usage()
{
prog=$(basename $0)
echo ""
echo "Usage:"
echo " $prog [options] <build_combo>"
echo ""
echo " Options:"
echo " -a Automated build (check for clean working dir)"
echo " -c Clean output before build"
echo " Given once, clean device output (out/target/product)"
echo " Given twice, clean target output (out/target)"
echo " Given thrice, clean all output (out)"
echo " -C Clean output after successful build"
echo " Given once, clean device output (out/target/product)"
echo " Given twice, clean target output (out/target)"
echo " Given thrice, clean all output (out)"
echo " -j Specify -j value for make [CPUS]"
echo " -l Save build output to log file"
echo " -o Specify output directory"
echo " -O Specify Android out dir"
echo " -s Show commands"
echo " -t Specify make target [auto]"
echo " -v Specify version [none]"
echo ""
echo "Exit status: 0 on success, !0 on failure"
echo ""
echo "On success, output will be copied to \$TARGET_PRODUCT.zip."
echo ""
exit 1
}
# Tool locations
# XXX: not darwin compatible
CCACHE="prebuilts/misc/linux-x86/ccache/ccache"
ccache_stats()
{
# Calculate ccache stats
local ch=0
local cm=0
local ct=0
local cpct="NA"
OLDIFS="$IFS"
IFS=$'\n'
lines=($($CCACHE -s))
IFS="$OLDIFS"
for (( n=0; $n < ${#lines[*]}; n=$(($n+1)) )); do
line=${lines[$n]}
if [ "${line:0:9}" = "cache hit" ]; then
ch=$(( ch + $(echo $line | sed 's/[^0-9]*//') ))
elif [ "${line:0:10}" = "cache miss" ]; then
read -a fields <<<$line
cm=$(( cm + $(echo $line | sed 's/[^0-9]*//') ))
fi
done
IFS="$OLDIFS"
if [ -n "$ch" -a -n "$cm" ]; then
ct=$(($ch+$cm))
fi
if [ "$ct" -gt 0 ]; then
cpct=$((ch*100/ct))
fi
echo "ch=$ch"
echo "cm=$cm"
echo "ct=$ct"
echo "cpct=$cpct"
}
# Set some useful vars
export USE_CCACHE=1
export CPUS=$(grep "^processor" /proc/cpuinfo | wc -l)
export MEM=$(cat /proc/meminfo | grep "^MemTotal" | grep -o "[0-9]*")
# Find actual value for jobs.
# - No more than 8 jobs
# - No more than one job per cpu
# - No more than one job per 1.5gb
max_jobs=8
cpu_jobs="$CPUS"
if [ "$cpu_jobs" -lt "$max_jobs" ]; then
max_jobs="$cpu_jobs"
fi
mem_jobs=$(($MEM/1572864))
if [ "$mem_jobs" -lt "$max_jobs" ]; then
max_jobs="$mem_jobs"
fi
# Init options
opt_automated=0
opt_clean=0
opt_postclean=0
opt_jobs="$max_jobs"
opt_log=0
opt_outdir="."
opt_outandroid=""
opt_maketarget="auto"
opt_showcommands=0
opt_version=""
while getopts "acCj:lo:O:st:v:" opt; do
case "$opt" in
a) opt_automated=1 ;;
c) opt_clean=$(($opt_clean+1)) ;;
C) opt_postclean=$(($opt_postclean+1)) ;;
j) opt_jobs="$OPTARG" ;;
l) opt_log=1 ;;
o) opt_outdir="$OPTARG" ;;
O) opt_outandroid="$OPTARG" ;;
s) opt_showcommands=1 ;;
t) opt_maketarget="$OPTARG" ;;
v) opt_version="$OPTARG" ;;
*) usage
esac
done
shift $(($OPTIND-1))
if [ $# -ne 1 ]; then
usage
fi
if [ -n "$opt_outandroid" ]; then
export OUT_DIR_COMMON_BASE="$opt_outandroid"
fi
mkdir -p "$opt_outdir"
# NB: "$combo" is used by build scripts
build_combo="$1"
project_device=$(echo $build_combo | cut -d'-' -f1)
project=$(echo $project_device | cut -d'_' -f1)
device=$(echo $project_device | cut -d'_' -f2)
# Desired output files
if [ -n "$opt_version" ]; then
out_name="${opt_outdir}/${project_device}-${opt_version}"
else
out_name="${opt_outdir}/${project_device}"
fi
if [ "$opt_log" -ne 0 ]; then
rm -f "${out_name}.log"
exec >> "${out_name}.log" 2>&1
fi
if [ ! -d ".repo" ]; then
echo "Invalid build tree"
exit 1
fi
case "$opt_clean" in
0) # Do nothing
;;
1) # Clean device output
rm -rf "$OUT_DIR_COMMON_BASE/target/product"
;;
2) # Clean target output
rm -rf "$OUT_DIR_COMMON_BASE/target"
;;
*) # Clean all output
rm -rf "$OUT_DIR_COMMON_BASE"
;;
esac
# Detect make target
if [ -z "$opt_maketarget" -o "$opt_maketarget" = "auto" ]; then
opt_maketarget="otapackage"
if [ -f "build/core/Makefile" ]; then
if grep -q "^bacon:" "build/core/Makefile"; then
opt_maketarget="bacon"
fi
fi
fi
if [ "$opt_showcommands" -ne 0 ]; then
opt_maketarget="showcommands $opt_maketarget"
fi
# Ensure working directory is clean
if [ "$opt_automated" -ne 0 ]; then
android-repo status | grep -q "working directory clean"
fi
. build/envsetup.sh
lunch "$build_combo"
# Setup ccache
if [ -z "$CCACHE_ROOT" ]; then
CCACHE_ROOT="$HOME"
fi
export CCACHE_DIR="$CCACHE_ROOT/.ccache-$project_device"
if [ ! -d "$CCACHE_DIR" ]; then
mkdir -p "$CCACHE_DIR"
$CCACHE -M 8G
fi
# Clean some things
rm -f $OUT/system/build.prop
eval $(ccache_stats)
sch=$ch
scm=$cm
sct=$ct
scpct=$cpct
# Do the build
stime=$(date +%s)
make -j${opt_jobs} ${opt_maketarget}
etime=$(date +%s)
# Find output zip
built_zip=$(ls -t $OUT/*.zip | head -1)
if [ ! -f "$built_zip" ]; then
echo "Error: cannot find built zip in $OUT"
exit 1
fi
# Copy output zip to well known place
cp "$built_zip" "${out_name}.zip"
# Calculate elapsed time
elapsedsec=$((etime - stime))
elapsed=$(printf "%02d:%02d" $((elapsedsec/60)) $((elapsedsec%60)))
eval $(ccache_stats)
ech=$ch
ecm=$cm
ect=$ct
ecpct=$cpct
dch=$((ech - sch))
dcm=$((ecm - scm))
dct=$((ect - sct))
dcpct="NA"
if [ "$dct" -gt 0 ]; then
dcpct=$((dch*100/dct))
fi
echo "Build complete."
echo "elapsed time : $elapsed"
echo "ccache totals: $ech/$ect ($ecpct%)"
echo "ccache deltas: $dch/$dct ($dcpct%)"
case "$opt_postclean" in
0) # Do nothing
;;
1) # Clean device output
rm -rf "$OUT_DIR_COMMON_BASE/target/product"
;;
2) # Clean target output
rm -rf "$OUT_DIR_COMMON_BASE/target"
;;
*) # Clean all output
rm -rf "$OUT_DIR_COMMON_BASE"
;;
esac