-
Notifications
You must be signed in to change notification settings - Fork 6
/
atom-build.sh
executable file
·455 lines (387 loc) · 11.3 KB
/
atom-build.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
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
#!/bin/bash
######################### CONFIGURATION ##############################
# User details
KBUILD_USER="$USER"
KBUILD_HOST=$(uname -n)
############################################################################
######################## DIRECTORY PATHS ###############################
# Kernel Directory
KERNEL_DIR=$(pwd)
# Propriatary Directory (default paths may not work!)
PRO_PATH="$KERNEL_DIR/.."
# Toolchain Directory
TLDR="$PRO_PATH/toolchains"
# Anykernel Directories
AK3_DIR="$PRO_PATH/AnyKernel3"
AKVDR="$AK3_DIR/modules/vendor/lib/modules"
AKVRD="$AK3_DIR/vendor_ramdisk/lib/modules"
# Device Tree Blob Directory
DTB_PATH="$KERNEL_DIR/work/arch/arm64/boot/dts/vendor/qcom"
############################################################################
############################### COLORS #################################
R='\033[1;31m'
G='\033[1;32m'
B='\033[1;34m'
W='\033[1;37m'
############################################################################
################################ MISC ##################################
# functions
error()
{
echo -e ""
echo -e "$R ${FUNCNAME[0]}: $W" "$@"
echo -e ""
exit 1
}
success()
{
echo -e ""
echo -e "$G ${FUNCNAME[1]}: $W" "$@"
echo -e ""
exit 0
}
inform()
{
if [[ $SILENCE != 1 || $* =~ "--force" ]]; then
echo -e ""
echo -e "$B ${FUNCNAME[1]}: $W" "$@" "$G" | sed 's/--force//'
echo -e ""
else
echo -e "$G"
fi
}
muke()
{
if [[ -z $COMPILER || -z $COMPILER32 ]]; then
error "Compiler is missing"
fi
make "$@" "${MAKE_ARGS[@]}"
}
usage()
{
inform " ./AtomX.sh <arg>
--compiler Sets the compiler to be used.
--compiler32 Sets the 32bit compiler to be used,
(defaults to clang).
--device Sets the device for kernel build.
--clean Clean up build directory before running build,
(default behaviour is incremental).
--dtbs Builds dtbs, dtbo & dtbo.img.
--dtb_zip Builds flashable zip with dtbs, dtbo.
--obj Builds specified objects.
--regen Regenerates defconfig
--log Builds logs saved to log.txt in current dir.
--silence Silence shell output of Kbuild".
exit 2
}
############################################################################
compiler_setup()
{
############################ COMPILER SETUP ##############################
# default to clang
CC='clang'
C_PATH="$TLDR/$CC"
CROSS_COMPILE="aarch64-linux-gnu-"
LLVM_PATH="$C_PATH/bin"
# Just override the existing declarations
if [[ $COMPILER == gcc ]]; then
CC='aarch64-elf-gcc'
C_PATH="$TLDR/gcc-arm64"
CROSS_COMPILE="aarch64-elf-"
fi
C_NAME=$("$C_PATH"/bin/$CC --version | head -n 1 | perl -pe 's/\(http.*?\)//gs')
if [[ $COMPILER32 == "gcc" || $COMPILER == "gcc" ]]; then
MAKE_ARGS+=("CC_COMPAT=$TLDR/gcc-arm/bin/arm-eabi-gcc" "CROSS_COMPILE_COMPAT=$TLDR/gcc-arm/bin/arm-eabi-")
C_NAME_32=$($(echo "${MAKE_ARGS[@]}" | sed s/' '/'\n'/g | grep CC_COMPAT | cut -c 11-) --version | head -n 1)
else
MAKE_ARGS+=("CROSS_COMPILE_COMPAT=arm-linux-gnueabi-")
C_NAME_32="$C_NAME"
fi
MAKE_ARGS+=("O=work" "ARCH=arm64"
"CROSS_COMPILE=$CROSS_COMPILE"
"TARGET_PRODUCT=dubai_g"
"DTC_FLAGS+=-q" "DTC_EXT=$(which dtc)"
"LLVM_IAS=1" "LLVM=1" "CC=$CC"
"HOSTLD=ld.lld" "PATH=$C_PATH/bin:$PATH"
"KBUILD_BUILD_USER=$KBUILD_USER" "KBUILD_BUILD_HOST=$KBUILD_HOST"
"$(head -1 build.config.common)" "$(head -2 build.config.common | tail -1)")
############################################################################
}
config_generator()
{
######################### .config GENERATOR ############################
if [[ -z $CODENAME ]]; then
error 'Codename not present connot proceed'
exit 1
fi
DFCF="vendor/${CODENAME}-${SUFFIX}_defconfig"
if [[ ! -f arch/arm64/configs/$DFCF ]]; then
inform "Generating defconfig"
export "${MAKE_ARGS[@]}" "TARGET_BUILD_VARIANT=user" "TARGET_PRODUCT=dubai_g"
bash scripts/gki/generate_defconfig.sh "${CODENAME}-${SUFFIX}_defconfig"
bash scripts/gki/generate_defconfig.sh "lahaina-qgki_defconfig"
rm -rf arch/arm64/configs/vendor/lahaina-qgki_defconfig
muke vendor/lahaina-qgki_defconfig vendor/"${CODENAME}"-"${SUFFIX}"_defconfig savedefconfig
muke "$DFCF" vendor/lahaina_QGKI.config vendor/debugfs.config vendor/ext_config/moto-lahaina.config vendor/ext_config/moto-lahaina-dubai.config vendor/ext_config/debug-lahaina-dubai.config savedefconfig
cat work/defconfig >arch/arm64/configs/"$DFCF"
else
inform "Generating .config"
# Make .config
muke "$DFCF" savedefconfig
fi
if [[ $TEST == "1" ]]; then
./scripts/config --file work/.config -d CONFIG_LTO_CLANG
./scripts/config --file work/.config -d CONFIG_HEADERS_INSTALL
fi
############################################################################
}
config_regenerator()
{
######################## DEFCONFIG REGENERATOR ###########################
config_generator
inform "Regenerating defconfig"
cat work/defconfig >arch/arm64/configs/"$DFCF"
success "Regeneration completed"
############################################################################
}
obj_builder()
{
############################## OBJ BUILD #################################
if [[ $OBJ == "" ]]; then
error "obj not defined"
fi
config_generator
inform "Building $OBJ"
if [[ $OBJ =~ "defconfig" ]]; then
muke "$OBJ"
else
muke -j"$(nproc)" INSTALL_HDR_PATH="headers" "$OBJ"
fi
if [[ $TEST == "1" ]]; then
rm -rf arch/arm64/configs/vendor/lahaina-qgki_defconfig
fi
if [[ $DTB_ZIP != "1" ]]; then
exit 0
fi
############################################################################
}
dtb_zip()
{
############################## DTB BUILD #################################
obj_builder
source work/.config
if [[ ! -d $AK3_DIR ]]; then
error 'Anykernel not present cannot zip'
fi
if [[ ! -d "$KERNEL_DIR/out" ]]; then
mkdir "$KERNEL_DIR"/out
fi
cp "$DTB_PATH"/yupik*dtb "$AK3_DIR"/dtb
cp "$DTB_PATH"/*.img "$AK3_DIR"/
cd "$AK3_DIR" || exit
make zip VERSION="$(echo "$CONFIG_LOCALVERSION" | cut -c 8-)-dtbs-only"
cp ./*-signed.zip "$KERNEL_DIR"/out
make clean
cd "$KERNEL_DIR" || exit
success "dtbs zip built"
############################################################################
}
kernel_builder()
{
################################## BUILD #################################
if [[ $BUILD == "clean" ]]; then
inform "Cleaning work directory, please wait...."
muke -s clean mrproper distclean
fi
config_generator
# Build Start
BUILD_START=$(date +"%s")
source work/.config
MOD_NAME="$(muke kernelrelease -s)"
KERNEL_VERSION=$(echo "$MOD_NAME" | cut -c -7)
inform --force "
*************Build Triggered*************
CI: $KBUILD_HOST
Core count: $(nproc)
Device: $DEVICENAME
Codename: $CODENAME
Compiler: $C_NAME
Compiler_32: $C_NAME_32
Kernel Name: $MOD_NAME
Linux Version: $KERNEL_VERSION
Build Date: $(date +"%Y-%m-%d %H:%M")
*****************************************
"
# Compile
if [[ $LOG != 1 ]]; then
muke -j"$(nproc)"
else
muke -j"$(nproc)" 2>&1 | tee log.txt
fi
if [[ $CONFIG_MODULES == "y" ]]; then
muke -j"$(nproc)" \
'modules_install' \
INSTALL_MOD_STRIP=1 \
INSTALL_MOD_PATH="modules"
fi
# Build End
BUILD_END=$(date +"%s")
DIFF=$(("$BUILD_END" - "$BUILD_START"))
zipper
############################################################################
}
zipper()
{
#################################### ZIP #################################
TARGET="$(muke image_name -s)"
if [[ ! -f $KERNEL_DIR/work/$TARGET ]]; then
error 'Kernel image not found'
fi
if [[ ! -d $AK3_DIR ]]; then
error 'Anykernel not present cannot zip'
fi
if [[ ! -d "$KERNEL_DIR/out" ]]; then
mkdir "$KERNEL_DIR"/out
fi
# Making sure everything is ok before making zip
cd "$AK3_DIR" || exit
make clean
cd "$KERNEL_DIR" || exit
cp "$KERNEL_DIR"/work/"$TARGET" "$AK3_DIR"
cp "$DTB_PATH"/yupik*dtb "$AK3_DIR"/dtb
cp "$DTB_PATH"/*.img "$AK3_DIR"/
if [[ $CONFIG_MODULES == "y" ]]; then
MOD_PATH="work/modules/lib/modules/$MOD_NAME"
sed -i 's/\(kernel\/[^: ]*\/\)\([^: ]*\.ko\)/\/vendor\/lib\/modules\/\2/g' "$MOD_PATH"/modules.dep
sed -i 's/.*\///g' "$MOD_PATH"/modules.order
if [[ $DRM_VENDOR_MODULE == "1" ]]; then
DRM_AS_MODULE=1
if [ ! -d "$AK3_DIR"/vendor_ramdisk/lib/modules/ ]; then
VENDOR_RAMDISK_CREATE=1
mkdir -p "$AK3_DIR"/vendor_ramdisk/lib/modules/
fi
mv "$(find "$MOD_PATH" -name 'msm_drm.ko')" "$AKVRD"
grep drm "$MOD_PATH/modules.alias" >"$AKVRD"/modules.alias
grep drm "$MOD_PATH/modules.dep" | sed 's/^........//' >"$AKVRD"/modules.dep
grep drm "$MOD_PATH/modules.softdep" >"$AKVRD"/modules.softdep
grep drm "$MOD_PATH/modules.order" >"$AKVRD"/modules.load
sed -i s/split_boot/dump_boot/g "$AK3_DIR"/anykernel.sh
fi
# shellcheck disable=SC2046
# cp breaks with advised follow up
cp $(find "$MOD_PATH" -name '*.ko') "$AKVDR"/
cp "$MOD_PATH"/modules.{alias,dep,softdep} "$AKVDR"/
cp "$MOD_PATH"/modules.order "$AKVDR"/modules.load
fi
LAST_COMMIT=$(git show -s --format=%s)
LAST_HASH=$(git rev-parse --short HEAD)
cd "$AK3_DIR" || exit
make zip VERSION="$(echo "$CONFIG_LOCALVERSION" | cut -c 8-)" CUSTOM="$LAST_HASH"
if [ "$DRM_AS_MODULE" = "1" ]; then
if [ "$VENDOR_RAMDISK_CREATE" = "1" ]; then
rm -rf "$AK3_DIR"/vendor_ramdisk/
fi
sed -i s/'dump_boot; # skip unpack'/'split_boot; # skip unpack'/g "$AK3_DIR"/anykernel.sh
fi
inform --force "
***************AtomX-Kernel**************
CI: $KBUILD_HOST
Core count: $(nproc)
Device: $DEVICENAME
Codename: $CODENAME
Compiler: $C_NAME
Compiler_32: $C_NAME_32
Kernel Name: $MOD_NAME
Linux Version: $KERNEL_VERSION
Build Date: $(date +"%Y-%m-%d %H:%M")
***********last commit details***********
Last commit (name): $LAST_COMMIT
Last commit (hash): $LAST_HASH
*****************************************
"
cp ./*-signed.zip "$KERNEL_DIR"/out
make clean
cd "$KERNEL_DIR" || exit
success "build completed in $((DIFF / 60)).$((DIFF % 60)) mins"
############################################################################
}
############################### COMMAND_MODE ##############################
if [[ -z $* ]]; then
usage
fi
if [[ $* =~ "--log" ]]; then
LOG=1
fi
if [[ $* =~ "--silence" ]]; then
MAKE_ARGS+=("-s")
SILENCE=1
fi
for arg in "$@"; do
case "${arg}" in
"--compiler="*)
COMPILER=${arg#*=}
COMPILER=${COMPILER,,}
if [[ -z $COMPILER ]]; then
usage
break
fi
;&
"--compiler32="*)
COMPILER32=${arg#*=}
COMPILER32=${COMPILER32,,}
if [[ -z $COMPILER32 ]]; then
COMPILER32="clang"
fi
compiler_setup
;;
"--device="*)
CODE_NAME=${arg#*=}
case $CODE_NAME in
dubai)
DEVICENAME='Motorola Edge 30'
CODENAME='dubai'
SUFFIX='qgki'
;;
lahaina)
DEVICENAME='lahaina common qgki kernel'
CODENAME='lahaina'
SUFFIX='qgki'
;;
*)
inform 'device not supported: fallback to manual configuration'
read -rp 'DEVICENAME: ' DEVICENAME
read -rp 'CODENAME: ' CODENAME
read -rp 'SUFFIX: ' SUFFIX
;;
esac
;;
"--clean")
BUILD='clean'
;;
"--test")
TEST='1'
CODENAME=lahaina
;;
"--dtb_zip")
DTB_ZIP=1
;&
"--dtbs")
OBJ=dtbs
dtb_zip
;;
"--obj="*)
OBJ=${arg#*=}
obj_builder
;;
"--regen")
config_regenerator
;;
"--log" | "--silence")
;;
*)
usage
;;
esac
done
############################################################################
kernel_builder