forked from ipfire/ipfire-2.x
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make.sh
executable file
·2643 lines (2237 loc) · 55.3 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
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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
###############################################################################
# #
# IPFire.org - A linux based firewall #
# Copyright (C) 2007-2024 IPFire Team <info@ipfire.org> #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <http://www.gnu.org/licenses/>. #
# #
###############################################################################
NAME="IPFire" # Software name
SNAME="ipfire" # Short name
# If you update the version don't forget to update backupiso and add it to core update
VERSION="2.29" # Version number
CORE="190" # Core Level (Filename)
SLOGAN="www.ipfire.org" # Software slogan
CONFIG_ROOT=/var/ipfire # Configuration rootdir
# Information from Git
GIT_BRANCH="$(git rev-parse --abbrev-ref HEAD)" # Git Branch
GIT_TAG="$(git tag | tail -1)" # Git Tag
GIT_LASTCOMMIT="$(git rev-parse --verify HEAD)" # Last commit
TOOLCHAINVER=20240827
KVER_SUFFIX="-${SNAME}"
# Kernel Version
KVER="$(grep --max-count=1 VER lfs/linux | awk '{ print $3 }')"
KVER="${KVER/-rc/.0-rc}${KVER_SUFFIX}"
###############################################################################
#
# Beautifying variables & presentation & input output interface
#
###############################################################################
# All supported architectures
ARCHES=(
aarch64
riscv64
x86_64
)
HOST_ARCH="${HOSTTYPE}"
HOST_KERNEL="$(uname -r)"
LC_ALL=POSIX
PS1='\u:\w$ '
HAS_TIME_NAMESPACE="true"
# Disable time namespaces for older kernels
case "${HOST_KERNEL}" in
4.*|5.[12345].*)
HAS_TIME_NAMESPACE="false"
;;
esac
# Are we reading from/writing to a terminal?
is_terminal() {
[ -t 0 ] && [ -t 1 ] && [ -t 2 ]
}
# Define color for messages
if is_terminal; then
BOLD="$(tput bold)"
RED="$(tput setaf 1)"
GREEN="$(tput setaf 2)"
YELLOW="$(tput setaf 3)"
CYAN="$(tput setaf 6)"
NORMAL="$(tput sgr0)"
fi
# Sets or adjusts pretty formatting variables
resize_terminal() {
# Find current screen size
COLUMNS="$(tput cols)"
# When using remote connections, such as a serial port, stty size returns 0
if ! is_terminal || [ "${COLUMNS}" = "0" ]; then
COLUMNS=80
fi
# Wipe any previous content before updating the counters
if [ -n "${TIME_COL}" ]; then
tput hpa "${TIME_COL}"
tput el
fi
# The status column is always 8 characters wide
STATUS_WIDTH=8
# The time column is always 12 characters wide
TIME_WIDTH=12
# Where do the columns start?
(( STATUS_COL = COLUMNS - STATUS_WIDTH ))
(( TIME_COL = STATUS_COL - TIME_WIDTH ))
}
# Initially setup terminal
resize_terminal
# Call resize_terminal when terminal is being resized
trap "resize_terminal" WINCH
# Writes a line to the log file
log() {
local line="$@"
# Fetch the current timestamp
local t="$(date -u "+%b %e %T")"
# Append the line to file
if [ -w "${LOGFILE}" ]; then
echo "${t}: ${line}" >> "${LOGFILE}"
fi
return 0
}
find_base() {
local path
# Figure out the absolute script path using readlink
path="$(readlink -f "${0}" 2>&1)"
# If that failed, try realpath
if [ -z "${path}" ]; then
path="$(realpath "${0}" 2>&1)"
fi
# If that failed, I am out of ideas
if [ -z "${path}" ]; then
echo "${0}: Could not determine BASEDIR" >&2
return 1
fi
# Return the dirname
dirname "${path}"
}
system_processors() {
getconf _NPROCESSORS_ONLN 2>/dev/null || echo "1"
}
system_memory() {
local key val unit
while read -r key val unit; do
case "${key}" in
MemTotal:*)
# Convert to MB
echo "$(( ${val} / 1024 ))"
break
;;
esac
done < /proc/meminfo
}
format_runtime() {
local seconds=${1}
if [ ${seconds} -ge 3600 ]; then
printf "%d:%02d:%02d\n" \
"$(( seconds / 3600 ))" \
"$(( seconds % 3600 / 60 ))" \
"$(( seconds % 3600 % 60 ))"
elif [ ${seconds} -ge 60 ]; then
printf "%d:%02d\n" \
"$(( seconds / 60 ))" \
"$(( seconds % 60 ))"
else
printf "%d\n" "${seconds}"
fi
}
print_line() {
# Print the string all the way to the status column
printf "%-${STATUS_COL}s" "$*"
}
print_headline() {
printf "${BOLD}%s${NORMAL}\n" "$*"
}
print_package() {
local name="${1}"
shift
local version="$(grep -E "^VER |^VER=|^VER " $BASEDIR/lfs/${name} | awk '{ print $3 }')"
local options="$@"
local string="${name}"
if [ -n "${version}" ] && [ "${version}" != "ipfire" ]; then
string="${string} (${version})"
fi
# Add the options
if [ -n "${options}" ]; then
string="${string} ${options[@]}"
fi
# Print the string
print_line "${string}"
}
print_runtime() {
local runtime=$(format_runtime $@)
# Move back the cursor to rewrite the runtime
if is_terminal; then
tput hpa "${TIME_COL}"
fi
printf "[ ${BOLD}%$(( TIME_WIDTH - 4 ))s${NORMAL} ]" "${runtime}"
}
print_status() {
local status="${1}"
local color
case "${status}" in
DONE)
color="${BOLD}${GREEN}"
;;
FAIL)
color="${BOLD}${RED}"
;;
SKIP)
color="${BOLD}${CYAN}"
;;
WARN)
color="${BOLD}${YELLOW}"
;;
*)
color="${BOLD}"
;;
esac
# Move to the start of the column
if is_terminal; then
tput hpa "${STATUS_COL}"
fi
printf "[ ${color}%$(( STATUS_WIDTH - 4 ))s${NORMAL} ]\n" "${status}"
}
print_build_summary() {
local runtime="${1}"
print_line "*** Build Finished"
print_runtime "${runtime}"
print_status DONE
}
# Launches a timer process as a co-process
launch_timer() {
# Do nothing if the timer is already running
if [ -n "${TIMER_PID}" ]; then
return 0
fi
# Don't launch the timer when we are not on a terminal
if ! is_terminal; then
return 0
fi
# Launch the co-process
coproc TIMER { "${0}" "__timer" "$$"; }
# Register the signal handlers
trap "__timer_event" SIGUSR1
trap "terminate_timer" EXIT
}
# Terminates a previously launched timer
terminate_timer() {
if [ -n "${TIMER_PID}" ]; then
kill -TERM "${TIMER_PID}"
fi
}
# The timer main loop
__timer() {
local pid="${1}"
# Send SIGUSR1 to the main process once a second
# If the parent process has gone away, we will terminate.
while sleep 1; do
if ! kill -USR1 "${pid}" &>/dev/null; then
break
fi
done
return 0
}
# Called when the timer triggers
# This function does nothing, but is needed interrupt the wait call
__timer_event() {
return 0
}
exiterror() {
# Dump logfile
if [ -n "${LOGFILE}" ] && [ -e "${LOGFILE}" ]; then
echo # empty line
local line
while read -r line; do
echo " ${line}"
done <<< "$(tail -n30 ${LOGFILE})"
fi
echo # empty line
local line
for line in "ERROR: $@" " Check ${LOGFILE} for errors if applicable"; do
print_line "${line}"
print_status FAIL
done
exit 1
}
prepareenv() {
local network="false"
# Are we running the right shell?
if [ -z "${BASH}" ]; then
exiterror "BASH environment variable is not set. You're probably running the wrong shell."
fi
if [ -z "${BASH_VERSION}" ]; then
exiterror "Not running BASH shell."
fi
# Trap on emergency exit
trap "exiterror 'Build process interrupted'" SIGINT SIGTERM SIGQUIT
# Checking if running as root user
if [ "${UID}" -ne 0 ]; then
exiterror "root privileges required for building"
fi
local required_space
# Parse arguments
while [ $# -gt 0 ]; do
case "${1}" in
--required-space=*)
required_space="${1#--required-space=}"
;;
--network)
network="true"
;;
*)
exiterror "Unknown argument: ${1}"
;;
esac
shift
done
# Do we need to check the required space?
if [ -n "${required_space}" ]; then
local free_space free_blocks block_size
local consumed_space path
# Fetch free blocks
read -r free_blocks block_size <<< "$(stat --file-system --format="%a %S" "${BASEDIR}")"
# Calculate free space
(( free_space = free_blocks * block_size / 1024 / 1024 ))
# If we don't have the total space free, we need to check how much we have consumed already...
if [ "${free_space}" -lt "${required_space}" ]; then
# Add any consumed space
while read -r consumed_space path; do
(( free_space += consumed_space / 1024 / 1024 ))
done <<< "$(du --summarize --bytes "${BUILD_DIR}" "${IMAGES_DIR}" "${LOG_DIR}" 2>/dev/null)"
fi
# Check that we have the required space
if [ "${free_space}" -lt "${required_space}" ]; then
exiterror "Not enough temporary space available, need at least ${required_space}MiB, but only have ${free_space}MiB"
fi
fi
# Set umask
umask 022
# Make some extra directories
mkdir -p "${CCACHE_DIR}"
mkdir -p "${IMAGES_DIR}"
mkdir -p "${PACKAGES_DIR}"
mkdir -p "${BUILD_DIR}/${TOOLS_DIR}"
mkdir -p "${BUILD_DIR}/cache"
mkdir -p "${BUILD_DIR}/dev"
mkdir -p "${BUILD_DIR}/etc"
mkdir -p "${BUILD_DIR}/proc"
mkdir -p "${BUILD_DIR}/root"
mkdir -p "${BUILD_DIR}/sys"
mkdir -p "${BUILD_DIR}/tmp"
mkdir -p "${BUILD_DIR}/usr/src"
mkdir -p "${BUILD_DIR}/usr/src/cache"
mkdir -p "${BUILD_DIR}/usr/src/ccache"
mkdir -p "${BUILD_DIR}/usr/src/config"
mkdir -p "${BUILD_DIR}/usr/src/doc"
mkdir -p "${BUILD_DIR}/usr/src/html"
mkdir -p "${BUILD_DIR}/usr/src/images"
mkdir -p "${BUILD_DIR}/usr/src/langs"
mkdir -p "${BUILD_DIR}/usr/src/lfs"
mkdir -p "${BUILD_DIR}/usr/src/log"
mkdir -p "${BUILD_DIR}/usr/src/src"
# Make BUILD_DIR a mountpoint
mount -o bind "${BUILD_DIR}" "${BUILD_DIR}"
# Create a new, minimal /dev
mount build_dev "${BUILD_DIR}/dev" \
-t devtmpfs -o "nosuid,noexec,mode=0755,size=4m,nr_inodes=64k"
# Mount a new /dev/pts
mount build_dev_pts "${BUILD_DIR}/dev/pts" \
-t devpts -o "nosuid,noexec,newinstance,ptmxmode=0666,mode=620"
# Mount a new /dev/shm
mount build_dev_shm "${BUILD_DIR}/dev/shm" \
-t tmpfs -o "nosuid,nodev,strictatime,mode=1777,size=1024m"
# Mount a new /tmp
mount build_tmp "${BUILD_DIR}/tmp" \
-t tmpfs -o "nosuid,nodev,strictatime,size=4G,nr_inodes=1M,mode=1777"
# Create an empty /proc directory and make it a mountpoint
mkdir -p "${BUILD_DIR}/proc"
mount --bind "${BUILD_DIR}/proc" "${BUILD_DIR}/proc"
# Make all sources and proc available under lfs build
mount --bind /sys "${BUILD_DIR}/sys"
mount --bind -o ro "${BASEDIR}/cache" "${BUILD_DIR}/usr/src/cache"
mount --bind -o ro "${BASEDIR}/config" "${BUILD_DIR}/usr/src/config"
mount --bind -o ro "${BASEDIR}/doc" "${BUILD_DIR}/usr/src/doc"
mount --bind -o ro "${BASEDIR}/html" "${BUILD_DIR}/usr/src/html"
mount --bind -o ro "${BASEDIR}/langs" "${BUILD_DIR}/usr/src/langs"
mount --bind -o ro "${BASEDIR}/lfs" "${BUILD_DIR}/usr/src/lfs"
mount --bind -o ro "${BASEDIR}/src" "${BUILD_DIR}/usr/src/src"
# Mount the log directory
mount --bind "${LOG_DIR}" "${BUILD_DIR}/usr/src/log"
# Mount the ccache
mount --bind "${CCACHE_DIR}" "${BUILD_DIR}/usr/src/ccache"
# Mount the images directory
mount --bind "${IMAGES_DIR}" "${BUILD_DIR}/usr/src/images"
# Bind-mount files requires for networking if requested
if [ "${network}" = "true" ]; then
local file
for file in /etc/resolv.conf /etc/hosts; do
# Skip if the source files does not exist
if [ ! -e "${file}" ]; then
continue
fi
# Create the destination if it does not exist
if [ ! -e "${BUILD_DIR}/${file}" ]; then
touch "${BUILD_DIR}/${file}"
fi
# Mount the file read-only
mount --bind -o ro "${file}" "${BUILD_DIR}/${file}"
done
fi
# Configure the ccache
export CCACHE_TEMPDIR="/tmp"
export CCACHE_COMPILERCHECK="string:toolchain-${TOOLCHAINVER} ${BUILD_ARCH}"
# Install the QEMU helper
qemu_install_helper
# Remove pre-install list of installed files in case user erase some files before rebuild
rm -f "${BUILD_DIR}/usr/src/lsalr"
# Prepare string for /etc/system-release.
local system_release="${NAME} ${VERSION} (${BUILD_ARCH})"
case "${GIT_BRANCH}" in
core*|beta?|rc?)
system_release="${system_release} - ${GIT_BRANCH}"
;;
*)
system_release="${system_release} - core${CORE} Development Build: ${GIT_BRANCH}/${GIT_LASTCOMMIT:0:8}"
;;
esac
# Append -dirty tag for local changes
if [ "$(git status -s | wc -l)" != "0" ]; then
system_release="${system_release}-dirty"
fi
# Export variable
SYSTEM_RELEASE="${system_release}"
# Decide on PAKFIRE_TREE
case "${GIT_BRANCH}" in
core*)
PAKFIRE_TREE="stable"
;;
master)
PAKFIRE_TREE="testing"
;;
*)
PAKFIRE_TREE="unstable"
;;
esac
# Setup ccache cache size
execute --chroot ccache --max-size="${CCACHE_CACHE_SIZE}"
}
entershell() {
echo "Entering to a shell inside the build environment, go out with exit"
local PS1="ipfire build chroot (${BUILD_ARCH}) \u:\w\$ "
# Run an interactive shell
execute --chroot --interactive --network bash -i
}
lfsmakecommoncheck() {
# Script present?
if [ ! -f $BASEDIR/lfs/$1 ]; then
exiterror "No such file or directory: $BASEDIR/$1"
fi
# Print package name and version
print_package $@
# Check if this package is supported by our architecture.
# If no SUP_ARCH is found, we assume the package can be built for all.
if grep "^SUP_ARCH" ${BASEDIR}/lfs/${1} >/dev/null; then
# Check if package supports ${BUILD_ARCH} or all architectures.
if ! grep -E "^SUP_ARCH.*${BUILD_ARCH}|^SUP_ARCH.*all" ${BASEDIR}/lfs/${1} >/dev/null; then
print_runtime 0
print_status SKIP
return 1
fi
fi
echo -ne "`date -u '+%b %e %T'`: Building $* " >> $LOGFILE
return 0 # pass all!
}
execute() {
local chroot="false"
local command=()
local interactive="false"
local timer
local network="false"
# Collect environment variables
local -A environ=(
[PATH]="${TOOLS_DIR}/ccache/bin:${TOOLS_DIR}/sbin:${TOOLS_DIR}/bin:${PATH}"
[HOME]="${HOME}"
[PS1]="${PS1}"
[TERM]="vt100"
# Distro Information
[NAME]="${NAME}"
[SNAME]="${SNAME}"
[VERSION]="${VERSION}"
[CORE]="${CORE}"
[SLOGAN]="${SLOGAN}"
[SYSTEM_RELEASE]="${SYSTEM_RELEASE}"
[PAKFIRE_TREE]="${PAKFIRE_TREE}"
[CONFIG_ROOT]="${CONFIG_ROOT}"
# Kernel Version
[KVER]="${KVER}"
[KVER_SUFFIX]="${KVER_SUFFIX}"
# Compiler flags
[CFLAGS]="${CFLAGS[@]}"
[CXXFLAGS]="${CFLAGS[@]}"
[RUSTFLAGS]="${RUSTFLAGS[@]}"
# ccache
[CCACHE_DIR]="${CCACHE_DIR}"
[CCACHE_TEMPDIR]="${CCACHE_TEMPDIR}"
[CCACHE_COMPILERCHECK]="${CCACHE_COMPILERCHECK}"
# System Properties
[SYSTEM_PROCESSORS]="${SYSTEM_PROCESSORS}"
[SYSTEM_MEMORY]="${SYSTEM_MEMORY}"
# Parallelism
[DEFAULT_PARALLELISM]="${DEFAULT_PARALLELISM}"
# Compression Options
[XZ_OPT]="${XZ_OPT[*]}"
[ZSTD_OPT]="${ZSTD_OPT[*]}"
# Build Architecture
[BUILD_ARCH]="${BUILD_ARCH}"
[BUILD_PLATFORM]="${BUILD_PLATFORM}"
# Targets
[CROSSTARGET]="${CROSSTARGET}"
[BUILDTARGET]="${BUILDTARGET}"
# Paths
[LFS_BASEDIR]="${BASEDIR}"
[BUILD_DIR]="${BUILD_DIR}"
[IMAGES_DIR]="${IMAGES_DIR}"
[LOG_DIR]="${LOG_DIR}"
[PACKAGES_DIR]="${PACKAGES_DIR}"
[TOOLS_DIR]="${TOOLS_DIR}"
)
local unshare=()
# Configure a new namespace
if [ -n "${IN_NAMESPACE}" ]; then
unshare+=(
# Create a new cgroup namespace
"--cgroup"
# Create a new mount namespace
"--mount"
"--propagation=slave"
# Create a new PID namespace and fork
"--pid"
"--fork"
# Create a new UTS namespace
"--uts"
# Mount /proc so that the build environment does not see
# any foreign processes.
"--mount-proc=${BUILD_DIR}/proc"
# If unshare is asked to terminate, terminate all child processes
"--kill-child"
)
# Optionally set up a new time namespace
if [ "${HAS_TIME_NAMESPACE}" = "true" ]; then
unshare+=( "--time" )
fi
fi
while [ $# -gt 0 ]; do
case "${1}" in
--chroot)
chroot="true"
# Update some variables
environ+=(
[PATH]="${TOOLS_DIR}/ccache/bin:/bin:/usr/bin:/sbin:/usr/sbin:${TOOLS_DIR}/sbin:${TOOLS_DIR}/bin"
[HOME]="/root"
# Paths
[LFS_BASEDIR]="/usr/src"
[BUILD_DIR]="/"
[IMAGES_DIR]="/usr/src/images"
[LOG_DIR]="/usr/src/log"
[PACKAGES_DIR]="/usr/src/images/packages"
# Compiler Cache
[CCACHE_DIR]="/usr/src/ccache"
# Go Cache
[GOCACHE]="/usr/src/ccache/go"
)
# Fake environment
if [ -e "${BUILD_DIR}${TOOLS_DIR}/lib/libpakfire_preload.so" ]; then
environ+=(
[LD_PRELOAD]="${TOOLS_DIR}/lib/libpakfire_preload.so"
# Fake kernel version, because some of the packages do not
# compile with kernel 3.0 and later
[UTS_RELEASE]="${KVER}"
# Fake machine
[UTS_MACHINE]="${BUILD_ARCH}"
)
fi
;;
--interactive)
interactive="true"
# Use the actual value of $TERM
environ+=(
[TERM]="${TERM}"
)
;;
--network)
network="true"
# Export the proxy configuration
environ+=(
[https_proxy]="${https_proxy}"
[http_proxy]="${http_proxy}"
)
;;
--timer=*)
timer="${1#--timer=}"
;;
-*)
echo "Unknown argument: ${1}" >&2
return 2
;;
# Parse any custom environment variables
*=*)
environ["${1%=*}"]="${1#*=}"
;;
# The rest is the command
*)
command+=( "$@" )
break
;;
esac
shift
done
# Prepend any custom changes to PATH
if [ -n "${CUSTOM_PATH}" ]; then
environ[PATH]="${CUSTOM_PATH}:${environ[PATH]}"
fi
# Setup QEMU
if qemu_is_required; then
environ+=(
[QEMU_TARGET_HELPER]="${QEMU_TARGET_HELPER}"
# Enable QEMU strace
#[QEMU_STRACE]="1"
)
case "${BUILD_ARCH}" in
arm*)
environ+=(
[QEMU_CPU]="${QEMU_CPU:-cortex-a9}"
)
;;
riscv64)
environ+=(
[QEMU_CPU]="${QEMU_CPU:-sifive-u54}"
# Bug fix for QEMU locking up
[G_SLICE]="always-malloc"
)
;;
esac
fi
# Network
if [ "${network}" = "false" ]; then
unshare+=( "--net" )
fi
local execute=()
local env
# Create new namespaces
if [ "${#unshare[@]}" -gt 0 ]; then
execute+=(
"unshare" "${unshare[@]}"
)
fi
# Call a setup script in the new namespaces to perform
# further set up, but before we change root.
execute+=( "${BASEDIR}/tools/execute.sh" )
# Run in chroot?
if [ "${chroot}" = "true" ]; then
execute+=( "chroot" "${BUILD_DIR}" )
fi
# Set PATH so that we can find the env binary
local PATH="${environ[PATH]}:${PATH}"
# Reset the environment
execute+=(
"env"
# Clear the previous environment
"--ignore-environment"
# Change to the home directory
--chdir="${environ[HOME]}"
)
# Export the environment
for env in ${!environ[@]}; do
execute+=( "${env}=${environ[${env}]}" )
done
# Append the command
execute+=( "${command[@]}" )
local pid
# Return code
local r=0
# Store the start time
local t="${SECONDS}"
# Run the command in the background and pipe all output to the logfile
case "${interactive}" in
true)
"${execute[@]}" || return $?
;;
false)
# Launch the timer if needed
if [ -n "${timer}" ]; then
launch_timer
fi
# Dispatch the command to the background
{
"${execute[@]}" >> "${LOGFILE}" 2>&1 </dev/null
} &
# Store the PID
pid="$!"
# Wait for the process to complete
while :; do
wait "${pid}"
# Store the return code
r="$?"
case "${r}" in
# Code means that we have received SIGUSR1 from the timer
138)
# Call the timer callback
if [ -n "${timer}" ]; then
"${timer}"
fi
# Go back and wait
continue
;;
# Ignore SIGWINCH
156)
continue
;;
esac
break
done
# Call the timer callback at least once
if [ -n "${timer}" ]; then
"${timer}"
fi
esac
return "${r}"
}
# Calls the makefile of a package
make_pkg() {
local args=()
local pkg
local basedir="${BASEDIR}"
while [ $# -gt 0 ]; do
local arg="${1}"
shift
case "${arg}" in
--*)
case "${arg}" in
--chroot)
basedir="/usr/src"
;;
esac
args+=( "${arg}" )
;;
*)
pkg="${arg}"
break
;;
esac
done
# Execute the make command in the environment
execute "${args[@]}" make --directory="${basedir}/lfs" --file="${pkg}" "$@"
}
lfsmake1() {
local pkg="${1}"
shift
# Run the common check
lfsmakecommoncheck "${pkg}" "$@"
[ $? == 1 ] && return 0
# Download source outside of the toolchain
if ! make_pkg --network "${pkg}" download "$@"; then
exiterror "Downloading ${pkg}"
fi
if ! make_pkg --timer="update_runtime" "${pkg}" TOOLCHAIN=1 ROOT="${BUILD_DIR}" "$@"; then
print_status FAIL
exiterror "Building ${pkg}"
fi
print_status DONE
}
lfsmake2() {
local pkg="${1}"
shift
# Run the common check
lfsmakecommoncheck "${pkg}" "$@"
[ $? == 1 ] && return 0
# Download source outside of the toolchain
if ! make_pkg --network "${pkg}" download "$@"; then
exiterror "Downloading ${pkg}"
fi
# Run install on the package
if ! make_pkg --chroot --timer="update_runtime" "${pkg}" install "$@"; then
print_status FAIL
exiterror "Building ${pkg}"
fi
print_status DONE
}
ipfiredist() {
local pkg="${1}"
shift
# Run the common check
lfsmakecommoncheck "${pkg}" "$@"
[ $? == 1 ] && return 0
# Run dist on the package
if ! make_pkg --chroot --timer="update_runtime" "${pkg}" dist "$@"; then
print_status FAIL
exiterror "Packaging ${pkg}"
fi
print_status DONE
}
update_runtime() {
print_runtime "$(( SECONDS - t ))"
}
qemu_is_required() {
local build_arch="${1}"
if [ -z "${build_arch}" ]; then
build_arch="${BUILD_ARCH}"