-
Notifications
You must be signed in to change notification settings - Fork 1
/
apbs
executable file
·395 lines (368 loc) · 11.5 KB
/
apbs
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#!/usr/bin/env bash
#
# Android Portable Build Script (apbs)
# Isaac Pateau (Zaclimon)
#
# Introduction:
#
# As I was starting to use the Fish Shell, I've seen that it couldn't be used
# with pure POSIX based scripts or even those hardcoded for bash. One of those
# scripts is the Android environment setup script (envsetup.sh)
#
# The main purpose of this script will be to accomodate those
# who use the Fish Shell (or any not compatible shell for that matter) to at least
# build an Android ROM.
#
# By default, the number of threads used are the total available on the computer.
# That said, the user can modify as he/she wishes by modyfing the "NB_CORES" attribute.
#
#
# Syntax:
#
# apbs COMMAND SOURCE TARGET
# apbs COMMAND TARGET
#
# COMMAND: The command as used in the source's envsetup.sh
#
# SOURCE: Location of the Android source tree.
#
# TARGET: Optional, it is mainly used for specyfing when building for a device
# for example.
#
COMMAND=$1
SOURCE=$2
TARGET=$3
TEMP_DEVICE_FILE=$HOME/.apbs_temp_file
NB_CORES=$(grep -c "processor" /proc/cpuinfo)
VERSION="1.2"
# Execute the command based on the user input
function main()
{
case $COMMAND in
-b|--build)
check_source_path
build_device
;;
-br|--brecovery)
check_source_path
build_recovery
;;
-cg|-gg|-jg|-mg|-rc|-rg|-se|-sg)
check_source_path
execute_grep
;;
--cgrep|--ggrep|--jgrep|--mangrep|--rcgrep|--resgrep|--sepgrep|--sgrep)
check_source_path
execute_grep
;;
-e|--examples)
show_examples
;;
-h|--help)
show_help_message
;;
-i|--info)
check_source_path
show_source_info
;;
-l|--list)
check_source_path
list_devices
;;
-m1|--mmm)
check_source_path
build_mmm
;;
-m2|--mmma)
check_source_path
build_mmma
;;
-s|--search)
check_source_path
search_devices "$TARGET"
;;
-v|--version)
show_version
;;
"")
show_intro_message
;;
*)
echo "apbs: Invalid command '$COMMAND'"
esac
exit 0
}
# Build if we can find the device in the list that we have currently available
function build_device()
{
check_device
lunch "$VALID_DEVICE"
make -j"$NB_CORES" otapackage
}
# Build all the required modules of a project without it's prerequisites
function build_mmm()
{
local PROJECT=${TARGET//_/\/}
if [ -d "$PROJECT" ] ; then
mmm "$PROJECT"
else
echo "Project $PROJECT not found"
exit 1
fi
}
# Build all the required modules of a project with it's prerequisites
function build_mmma()
{
local PROJECT=${TARGET//_/\/}
if [ -d "$PROJECT" ] ; then
mmma "$PROJECT"
else
echo "Project $PROJECT not found"
exit 1
fi
}
# Call the necessary functions to build the device's recovery image
function build_recovery()
{
check_device
lunch "$VALID_DEVICE"
make -j"$NB_CORES" recoveryimage
}
# Check for the device existence or possible duplicates
function check_device()
{
if [ -f "$TEMP_DEVICE_FILE" ] ; then
VALID_DEVICE=$(cat "$TEMP_DEVICE_FILE")
rm -f "$TEMP_DEVICE_FILE"
else
local DEVICE_COUNT=0
for CHOICE in "${LUNCH_MENU_CHOICES[@]}" ; do
if [[ $CHOICE == *"$TARGET"* ]] ; then
VALID_DEVICE=$CHOICE
((DEVICE_COUNT++))
fi
done
if [ $DEVICE_COUNT -eq 0 ] ; then
echo "The device $TARGET doesn't exist"
exit 1
elif [[ $DEVICE_COUNT -gt 1 && ! -z $TARGET ]] ; then
echo "There are $DEVICE_COUNT '$TARGET' defined in the source:"
echo ""
select_duplicate_device "$TARGET"
elif [ -z "$TARGET" ] ; then
echo "No device specified"
exit 1
fi
fi
}
# Check the path from multiple situations:
# 1. If the current directory is a source directory
# 2. If the absolute supplied path is a valid source
# 3. If the supplied path isn't an absolute one, check in the user's home folder
function check_source_path()
{
if [ -f "$PWD/build/envsetup.sh" ] ; then
. build/envsetup.sh > /dev/null
TARGET=$SOURCE
elif [[ -d $SOURCE && -f $SOURCE/build/envsetup.sh ]] ; then
cd "$SOURCE"
. build/envsetup.sh > /dev/null
elif [[ -d $HOME/$SOURCE && -f $HOME/$SOURCE/build/envsetup.sh ]] ; then
cd "$HOME/$SOURCE"
. build/envsetup.sh > /dev/null
else
if [ -z "$SOURCE" ] ; then
echo "No source has been given"
else
echo "The given source '$SOURCE' isn't a valid Android one"
fi
exit 1
fi
}
# Check if the command can be executed based on the version of the actual source
function check_version_sdk()
{
local SDK_VERSION
SDK_VERSION=$(grep "PLATFORM_SDK_VERSION :" build/core/version_defaults.mk | cut -d = -f2 | cut -d ' ' -f2)
if [[ ! $SDK_VERSION -ge $1 ]] ; then
echo "Android version not compatible with this command"
exit 1
fi
}
# Host here the various "grep" commands (if used)
function execute_grep()
{
local ORIGINAL_COMMAND=${COMMAND//-/}
case $ORIGINAL_COMMAND in
cg|cgrep)
cgrep "$TARGET"
;;
gg|ggrep)
check_version_sdk 21
ggrep "$TARGET"
;;
jg|jgrep)
jgrep "$TARGET"
;;
mg|mangrep)
check_version_sdk 18
mangrep "$TARGET"
;;
mg|mgrep)
mgrep "$TARGET"
;;
rc|rcgrep)
check_version_sdk 23
rcgrep "$TARGET"
;;
rg|resgrep)
resgrep "$TARGET"
;;
se|sepgrep)
check_version_sdk 19
sepgrep "$TARGET"
;;
sg|sgrep)
sgrep "$TARGET"
esac
}
# List board devices as invoked when executing envsetup.sh
function list_devices()
{
for CHOICE in "${LUNCH_MENU_CHOICES[@]}" ; do
echo "$CHOICE"
done
}
# Search for the different devices here.
# Duplicates can possibly be listed as well
function search_devices()
{
if [[ ! -z $1 ]] ; then
for CHOICE in "${LUNCH_MENU_CHOICES[@]}" ; do
if [[ $CHOICE == *"$1"* ]] ; then
echo "$CHOICE"
fi
done
else
echo "No device specified"
exit 1
fi
}
# Let the user choose which device he/she wants to take. Write the value in a file
# because Bash child shells cannot overwrite parent variables.
function select_duplicate_device()
{
PS3="Select your device: "
local DEVICE_ARRAY=()
for CHOICE in "${LUNCH_MENU_CHOICES[@]}" ; do
if [[ $CHOICE == *"$1"* ]] ; then
DEVICE_ARRAY+=("$CHOICE")
fi
done
# ShellCheck will see this $DEVICE as unused but it is truly not needed.
# shellcheck disable=SC2034
select DEVICE in "${DEVICE_ARRAY[@]}" ; do
if [[ $REPLY -ge 1 && $REPLY -le ${#DEVICE_ARRAY[@]} ]] ; then
local DEVICE_INDEX=$REPLY-1
TARGET=${DEVICE_ARRAY[$DEVICE_INDEX]}
echo "$TARGET" > "$TEMP_DEVICE_FILE"
build_device
exit 0
else
echo "Value not valid"
exit 1
fi
done
}
# In case people don't understand how to use this script, show examples
function show_examples()
{
echo "Here are some examples on how to use this script:"
echo ""
echo " Example 1: How to build a ROM for a device"
echo " abps [ -b | --build ] source_path device_name"
echo ""
echo " Example 2: List the devices for a given source"
echo " abps [ -l | --list ] source_path"
echo ""
echo " Example 3: Search for device name (If we don't know it)"
echo " abps [ -s | --search ] source_path device_name"
echo ""
echo " Example 4: Search a specific word in the source"
echo " abps [ -sg | --sgrep ] source_path"
echo ""
echo " Example 5: Using without specifying the source"
echo " abps COMMAND [ device_name ]"
}
# Self-explanatory... But shows the help message
function show_help_message()
{
echo "Usage: apbs COMMAND SOURCE TARGET"
echo " apbs COMMAND SOURCE"
echo " apbs COMMAND TARGET"
echo " abps COMMAND"
echo " apbs [ -h | --help | -v | --version ]"
echo ""
echo "A portable script to build Android on-the-go"
echo ""
echo "Description"
echo ""
echo "COMMAND: One of this script's commands. They are defined below"
echo "SOURCE: Directory of the related Android OS source"
echo "TARGET: Points toward what this script will build"
echo ""
echo "Note: Specifying the source is not necessary if the current directory is a"
echo " valid one"
echo ""
echo "Commands:"
echo ""
echo " -b, --build Builds the operating system based on the source and device"
echo " given"
echo " -br, --brecovery Builds the recovery image based on the source and device"
echo " given"
echo " -cg, --cgrep Greps on all local C/C++ files."
echo " -gg, --ggrep Greps on all local Gradle files."
echo " -h, --help Displays this help message"
echo " -i, --info Displays some information about the Android source"
echo " -jg, --jgrep Greps on all local Java files."
echo " -l, --list Lists the devices that the source can build"
echo " -mg, --mangrep Greps on all local AndroidManifest.xml files"
echo " -m1, --mmm Builds all of the modules in the supplied directory"
echo " without their dependencies"
echo " -m2, --mmma Builds all of the modules in the supplied directory"
echo " with their dependencies"
echo " -rc, --rcgrep Greps on all local *.rc files"
echo " -rg, --resgrep Greps on all local res/*.xml files"
echo " -s, --search Searches for a specific device in the source"
echo " -se, --sepgrep Greps on all local sepolicy files"
echo " -sg, --sgrep Greps on all local source files"
echo " -v, --version Displays the version of the script"
}
function show_intro_message()
{
echo "apbs: A portable script to build Android on-the-go"
echo "Execute 'apbs -h' or 'apbs --help' for more information"
}
# Displays the source basic information (such as version, full path)
function show_source_info()
{
local ANDROID_VERSION
local ANDROID_SDK_VERSION
local ANDROID_VERSION_BUILD_ID
ANDROID_VERSION=$(grep "PLATFORM_VERSION :" build/core/version_defaults.mk | cut -d = -f2 | cut -d ' ' -f2)
ANDROID_SDK_VERSION=$(grep "PLATFORM_SDK_VERSION :" build/core/version_defaults.mk | cut -d = -f2 | cut -d ' ' -f2)
ANDROID_VERSION_BUILD_ID=$(grep "BUILD_ID=" build/core/build_id.mk | cut -d = -f2)
echo "Source information:"
echo ""
echo "Android Version: $ANDROID_VERSION"
echo "Android SDK Version: $ANDROID_SDK_VERSION"
echo "Android Build ID: $ANDROID_VERSION_BUILD_ID"
echo "Android Source Full Path: $(gettop)"
}
# Show the version as defined by "$VERSION"
function show_version()
{
echo "Android Portable Build Script (apbs) version $VERSION"
}
# Call the main function on the command received
main