-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsetup_env
More file actions
executable file
·595 lines (490 loc) · 12.9 KB
/
setup_env
File metadata and controls
executable file
·595 lines (490 loc) · 12.9 KB
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
#!/bin/bash
# SPDX-FileCopyrightText: 2024 Edrick Sinsuan
# SPDX-License-Identifier: Apache-2.0
# List of functions
env_functions=(
# Environment init functions
"ederevx_init"
"script_init"
"git_init"
"env_init"
"dependencies_init"
"repo_init"
"build_init"
"vsftpd_init"
# Utility functions
"border"
"decho"
"decho_log"
"check_dir"
"check_file_silent"
"check_file"
"create_dir"
"pause_script"
"update_repo"
"update_linux"
"check_valid"
"commit_repo"
"push_repo"
"make_cmd"
"zip_image"
"cleanup_path"
"restore_path"
"setup_dynamic"
"rom_copy_and_clean"
"timestamp"
"exit_timestamp"
"cmd_exec"
"monitor_exec"
"detach_exec"
"kill_exec"
)
functions_deinit() {
for func in ${env_functions[@]}; do
unset -f $func
done
}
functions_deinit
ederevx_init() {
local script_dir=~/scripts
local script_path=$script_dir/setup_env
# Allow other scripts to get script_dir
export SCRIPTS_DIR=$script_dir
# Export global clean and backup PATH
export PATH_CLEAN=/usr/lib/ccache:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/home/ederevx/scripts:/home/ederevx/repo/bin
# Clone the script repo to ~/scripts and repo to ~/repo/bin
if [[ -z $(echo $PATH | grep -i $script_dir) ]]; then
export PATH=$PATH:$script_dir:~/repo/bin
fi
# Make sure ccache is in $PATH
if [[ -z $(echo $PATH | grep -i /usr/lib/ccache) ]]; then
export PATH=/usr/lib/ccache:$PATH
fi
export PATH_BKP=$PATH
# Export shared directories
local kname=$(uname -r)
if [[ ! -z $(echo $kname | grep -i wsl) ]]; then
export C_USER_DIR=/mnt/c/Users/evcsi
export SHARED_ROOT_DIR=$C_USER_DIR/WSL-Shared
else
export SHARED_ROOT_DIR=/home/shared
fi
# Execute this script at bash init
local bashrc=~/.bashrc
if [[ -z $(cat $bashrc | grep -i setup_env) ]]; then
echo "source $script_path" >> $bashrc
fi
# Setup nopasswd rule for calling commonly used commands
local setup_env_sudo=/etc/sudoers.d/setup_env
if ! check_file $setup_env_sudo; then
{
echo "$userhost ALL=(ALL:ALL) NOPASSWD: /usr/bin/apt-get"
echo "$userhost ALL=(ALL:ALL) NOPASSWD: /usr/bin/cpupower"
} | sudo tee $setup_env_sudo
decho $setup_env_sudo
fi
if [[ ! -z $(echo $kname | grep -i wsl) ]]; then
if [[ ! -z $(echo $0 | grep -i bash) ]]; then
# Only execute the following at session init
decho "Current kernel: $kname"
fi
fi
if [[ ! -z $(echo $0 | grep -i bash) ]]; then
update_linux
# Make sure we're set to performance
if [[ ! -z $(cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor | grep -iv performance) ]]; then
sudo cpupower frequency-set -g performance
fi
# Update repo at init always
curl -s https://storage.googleapis.com/git-repo-downloads/repo > ~/repo/bin/repo
chmod a+x ~/repo/bin/repo
fi
}
script_init() {
# Environment directories
local kname=$(uname -r)
readonly SHARED_ROMS_DIR="$SHARED_ROOT_DIR/roms"
readonly ROM_PRODUCT_DIR="out/target/product"
# Common variables
devices_list=(
"dumpling"
"cheeseburger"
)
curr_date=$(date +'%Y%m%d')
}
git_init() {
# Git properties
git_auth="$(git config --get user.name) <$(git config --get user.email)>"
}
exec_init() {
export LOCK_FILE=~/exec.lck
export CANCEL_FILE=~/kill.exec
export CMD_LOG=~/cmd.log
}
env_init() {
# Identifier
userhost=$(whoami)
if [[ $userhost == "ederevx" ]]; then
ederevx_init
git_init
fi
# Only execute the following if not session init
if [[ -z $(echo $0 | grep -i bash) ]]; then
script_init
fi
exec_init
timestamp
}
dependencies_init() {
# Install all needed packages
sudo apt-get install -y git-core git-lfs gnupg flex bison build-essential zip curl zlib1g-dev libc6-dev-i386 x11proto-core-dev libx11-dev lib32z1-dev libgl1-mesa-dev libxml2-utils xsltproc unzip fontconfig
# More packages needed for kernel compilation
sudo apt-get install -y gcc-aarch64-linux-gnu gcc-arm-linux-gnueabi
sudo apt-get install -y fakeroot ncurses-dev xz-utils libssl-dev bc libelf-dev ccache
}
repo_init() {
create_dir ~/repo
create_dir ~/repo/bin
create_dir ~/kernel
create_dir ~/kernel/toolchain
if [[ $userhost == "ederevx" ]]; then
git config --global user.name "Edrick Sinsuan"
git config --global user.email "[email protected]"
fi
# Get repo bin from google
curl -s https://storage.googleapis.com/git-repo-downloads/repo > ~/repo/bin/repo
chmod a+x ~/repo/bin/repo
# Clone kernel repositories
git clone [email protected]:ederevx/x-ft_kernel_oneplus_msm8998.git \
~/kernel/x-ft
git clone [email protected]:ederevx/x_kernel_oneplus_msm8998.git \
~/kernel/x
# Clone kernel dependencies
git clone [email protected]:ederevx/kernel_zip.git \
~/kernel/zip
git clone --depth 1 -b android-15.0.0_r6 --single-branch https://android.googlesource.com/platform/prebuilts/clang/host/linux-x86 \
~/kernel/toolchain/clang
# Clone website
git clone [email protected]:ederevx/ederevx-website.git \
~/ederevx-website
}
build_init() {
export USE_CCACHE=1
export CCACHE_EXEC=/usr/bin/ccache
ccache -M 50G
ccache -o compression=true
}
vsftpd_init() {
local vsftpd_conf=/etc/vsftpd.conf
local vsftpd_ssl=/etc/ssl/private/vsftpd.pem
sudo apt-get update
sudo apt-get install -y vsftpd
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-s|--ssl)
sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout $vsftpd_ssl -out $vsftpd_ssl
;;
esac
shift
done
if [[ -z $(cat $vsftpd_conf | grep -i "custom config") ]]; then
{
# SSL setup
echo "# Start custom config"
echo "ssl_enable=YES"
echo "ssl_tlsv1=YES"
echo "ssl_sslv2=NO"
echo "ssl_sslv3=NO"
echo "require_ssl_reuse=NO"
echo "allow_anon_ssl=NO"
echo "rsa_cert_file=$vsftpd_ssl"
echo "rsa_private_key_file=$vsftpd_ssl"
echo "force_local_logins_ssl=YES"
echo "force_local_data_ssl=YES"
# Users setup
echo "anonymous_enable=NO"
echo "local_enable=YES"
echo "write_enable=YES"
echo "chroot_local_user=YES"
echo "user_sub_token=shared"
# Local root setup
echo "local_root=$SHARED_ROOT_DIR"
# Port setup
echo "pasv_min_port=32768"
echo "pasv_max_port=65535"
echo "# End custom config"
} | sudo tee -a $vsftpd_conf
fi
# Double check if the conf is good
sudo nano $vsftpd_conf
sudo systemctl restart vsftpd
sudo systemctl status vsftpd
}
# Environment init functions - end
# Utility functions
border() {
printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
}
decho() {
border
echo "$@"
border
}
decho_log() {
decho "$@"
decho "$@" >> $log
}
check_dir() {
if [ ! -d "$@" ]; then
decho "Can't find $@"
return 1
else
return 0
fi
}
check_file_silent() {
if [ ! -f "$@" ]; then
return 1
else
return 0
fi
}
check_file() {
if [ ! -f "$@" ]; then
decho "Can't find $@"
return 1
else
return 0
fi
}
create_dir() {
if ! check_dir "$@"; then
mkdir -p $@
return 1
else
return 0
fi
}
pause_script() {
if [[ $nopause == "y" ]]; then
return
fi
border
read -p "Press enter to continue"
border
}
update_repo() {
local curr_dir=$(pwd)
if ! check_dir "$@/.git" || ! cd "$@"; then
return
fi
decho "Updating $@ repository..."
if [[ $1 == "$ZIP_DIR" ]]; then
git pull --no-rebase upstream master
git merge FETCH_HEAD
fi
git pull --ff-only
cd $curr_dir
}
update_linux() {
decho "Updating applications and binaries..."
sudo apt-get update
sudo apt-get upgrade -y
sudo apt-get dist-upgrade -y
sudo apt-get autoremove -y
sudo apt-get clean -y
}
check_valid() {
if [[ ${1:0:1} != "-" ]] && [[ $1 != "" ]]; then
return 0
else
return 1
fi
}
commit_repo() {
local curr_dir=$(pwd)
if ! check_dir "$1/.git" || ! cd "$1"; then
return
fi
decho "Commiting in $1 repository..."
c_cmd=""
git_auth_ovrid="$git_auth"
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-a|--add)
git add -A
;;
--author)
if check_valid "$2"; then
git_auth_ovrid="$2"
shift
fi
;;
-m|--message)
if check_valid "$2"; then
c_msg="$2"
shift
fi
;;
-e|--edit)
c_cmd="-e $c_cmd"
;;
esac
shift
done
git_init
git commit $c_cmd -s -m "$c_msg" --author="$git_auth_ovrid" --date="$date_full"
cd $curr_dir
}
push_repo() {
local curr_dir=$(pwd)
if ! check_dir "$1/.git" || ! cd "$1"; then
return
fi
decho "Pushing $1 repository..."
git_init
shift # Skip first variable used to define the directory
git push "$@"
cd $curr_dir
}
make_cmd() {
decho_log "Executing make $1..."
make $mkp "$@" 2>> $log
}
zip_image() {
decho_log "Zipping the image..."
zip -r9 $1.zip * -x .git README.md 2>> $log
}
cleanup_path() {
export PATH=$PATH_CLEAN
}
restore_path() {
export PATH=$PATH_BKP
}
setup_dynamic() {
local commonbranch=$1
local device=$2
local curr_dir=$(pwd)
local device_common="$ROM_DIR/device/oneplus/msm8998-common"
if [[ $support_nondyn_dyn != "y" ]]; then
return
fi
cd $device_common
decho "Setting up dyn_branch=$dyn_branch..."
if [[ $dyn_branch == "y" ]]; then
commonbranch=$commonbranch-dyn
fi
git fetch ed
git checkout $commonbranch
cd $curr_dir
}
rom_copy_and_clean() {
local romname=$1
local device=$2
local rom_product="$ROM_DIR/$ROM_PRODUCT_DIR"
local rom_target="$rom_product/$device/$romname"
local target_dir
local type
if [[ $support_nondyn_dyn == "y" ]] &&
[[ $dyn_branch == "y" ]]; then
type=dyn
fi
if [[ $gapps == "y" ]]; then
type=gapps
fi
if check_file $rom_target*.zip; then
rom_copy() {
if [[ ! -z $type ]]; then
target_dir="$target_dir/$type"
fi
create_dir "$target_dir"
decho "Copying $rom_target*.zip to $target_dir"
cp $rom_target*.zip* $target_dir
}
target_dir="$SHARED_ROMS_DIR"
rom_copy
fi
# Cleanup afterwards to prevent overlapping builds
rm -f $rom_target*.zip*
}
timestamp() {
date_full=$(date -R)
decho $date_full
}
exit_timestamp() {
timestamp
exit $1
}
cmd_exec() {
wait_lock_exec() {
if check_file_silent $CANCEL_FILE; then
rm $CANCEL_FILE
fi
warning=0
while [ -f "$LOCK_FILE" ]
do
if [ $warning -lt 1 ]; then
decho "$LOCK_FILE detected"
decho "Create $CANCEL_FILE if you do not want to wait."
warning=$((warning + 1))
fi
if check_file_silent $CANCEL_FILE; then
decho "$CANCEL_FILE has been created, queued execs are cancelled"
return 1
fi
sleep 5
done
decho "exec has been unlocked!"
}
lock_exec() {
if check_file $LOCK_FILE; then
decho "There is something building! Wait for it to complete!"
return 1
fi
touch $LOCK_FILE
decho "Locked cmd!"
return 0
}
unlock_exec() {
if ! check_file $LOCK_FILE; then
decho "There is no lock to remove!"
return 1
fi
rm $LOCK_FILE
decho "Unlocked cmd!"
return 0
}
decho "Waiting for exec to be unlocked..."
wait_lock_exec
decho "$CMD_TO_BE_EXEC"
if lock_exec; then
bash -c "$CMD_TO_BE_EXEC"
unlock_exec
else
decho "Did not execute cmd!"
fi
}
monitor_exec() {
tail -f $CMD_LOG
}
detach_exec() {
export CMD_TO_BE_EXEC="$@"
nohup bash -c cmd_exec > $CMD_LOG & disown
monitor_exec
}
kill_exec() {
touch $CANCEL_FILE
}
# Utility functions end
# Initialize environment
functions_init() {
for func in ${env_functions[@]}; do
export -f $func
done
}
functions_init
env_init