make.sh: Use exiterror to end script safely
[ipfire-2.x.git] / make.sh
1 #!/bin/bash
2 ############################################################################
3 #                                                                          #
4 # This file is part of the IPFire Firewall.                                #
5 #                                                                          #
6 # IPFire is free software; you can redistribute it and/or modify           #
7 # it under the terms of the GNU General Public License as published by     #
8 # the Free Software Foundation; either version 2 of the License, or        #
9 # (at your option) any later version.                                      #
10 #                                                                          #
11 # IPFire is distributed in the hope that it will be useful,                #
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of           #
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            #
14 # GNU General Public License for more details.                             #
15 #                                                                          #
16 # You should have received a copy of the GNU General Public License        #
17 # along with IPFire; if not, write to the Free Software                    #
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA #
19 #                                                                          #
20 # Copyright (C) 2007-2018 IPFire Team <info@ipfire.org>.                   #
21 #                                                                          #
22 ############################################################################
23 #
24
25 NAME="IPFire"                                                   # Software name
26 SNAME="ipfire"                                                  # Short name
27 VERSION="2.19"                                                  # Version number
28 CORE="121"                                                      # Core Level (Filename)
29 PAKFIRE_CORE="120"                                              # Core Level (PAKFIRE)
30 GIT_BRANCH=`git rev-parse --abbrev-ref HEAD`                    # Git Branch
31 SLOGAN="www.ipfire.org"                                         # Software slogan
32 CONFIG_ROOT=/var/ipfire                                         # Configuration rootdir
33 NICE=10                                                         # Nice level
34 MAX_RETRIES=1                                                   # prefetch/check loop
35 BUILD_IMAGES=1                                                  # Flash and Xen Downloader
36 KVER=`grep --max-count=1 VER lfs/linux | awk '{ print $3 }'`
37 GIT_TAG=$(git tag | tail -1)                                    # Git Tag
38 GIT_LASTCOMMIT=$(git log | head -n1 | cut -d" " -f2 |head -c8)  # Last commit
39
40 TOOLCHAINVER=20180213
41
42 ###############################################################################
43 #
44 # Beautifying variables & presentation & input output interface
45 #
46 ###############################################################################
47
48 # Remember if the shell is interactive or not
49 if [ -t 0 ] && [ -t 1 ]; then
50         INTERACTIVE=true
51 else
52         INTERACTIVE=false
53 fi
54
55 # Sets or adjusts pretty formatting variables
56 resize_terminal() {
57         ## Screen Dimentions
58         # Find current screen size
59         COLUMNS=$(tput cols)
60
61         # When using remote connections, such as a serial port, stty size returns 0
62         if ! ${INTERACTIVE} || [ "${COLUMNS}" = "0" ]; then
63                 COLUMNS=80
64         fi
65
66         # Measurements for positioning result messages
67         OPTIONS_WIDTH=20
68         TIME_WIDTH=12
69         STATUS_WIDTH=8
70         NAME_WIDTH=$(( COLUMNS - OPTIONS_WIDTH - TIME_WIDTH - STATUS_WIDTH ))
71         LINE_WIDTH=$(( COLUMNS - STATUS_WIDTH ))
72
73         TIME_COL=$(( NAME_WIDTH + OPTIONS_WIDTH ))
74         STATUS_COL=$(( TIME_COL + TIME_WIDTH ))
75 }
76
77 # Initially setup terminal
78 resize_terminal
79
80 # Call resize_terminal when terminal is being resized
81 trap "resize_terminal" WINCH
82
83 # Define color for messages
84 BOLD="\\033[1;39m"
85 DONE="\\033[1;32m"
86 SKIP="\\033[1;34m"
87 WARN="\\033[1;35m"
88 FAIL="\\033[1;31m"
89 NORMAL="\\033[0;39m"
90
91 # New architecture variables
92 HOST_ARCH="$(uname -m)"
93
94 PWD=$(pwd)
95 BASENAME=$(basename $0)
96
97 # Debian specific settings
98 if [ ! -e /etc/debian_version ]; then
99         FULLPATH=`which $0`
100 else
101         if [ -x /usr/bin/realpath ]; then
102                 FULLPATH=`/usr/bin/realpath $0`
103         else
104                 echo "ERROR: Need to do apt-get install realpath"
105                 exit 1
106         fi
107 fi
108
109 # This is the directory where make.sh is in
110 export BASEDIR=$(echo $FULLPATH | sed "s/\/$BASENAME//g")
111
112 LOGFILE=$BASEDIR/log/_build.preparation.log
113 export LOGFILE
114 DIR_CHK=$BASEDIR/cache/check
115 mkdir $BASEDIR/log/ 2>/dev/null
116
117 system_processors() {
118         getconf _NPROCESSORS_ONLN 2>/dev/null || echo "1"
119 }
120
121 system_memory() {
122         local key val unit
123
124         while read -r key val unit; do
125                 case "${key}" in
126                         MemTotal:*)
127                                 # Convert to MB
128                                 echo "$(( ${val} / 1024 ))"
129                                 break
130                                 ;;
131                 esac
132         done < /proc/meminfo
133 }
134
135 configure_build() {
136         local build_arch="${1}"
137
138         if [ "${build_arch}" = "default" ]; then
139                 build_arch="$(configure_build_guess)"
140         fi
141
142         case "${build_arch}" in
143                 x86_64)
144                         BUILDTARGET="${build_arch}-unknown-linux-gnu"
145                         CROSSTARGET="${build_arch}-cross-linux-gnu"
146                         BUILD_PLATFORM="x86"
147                         CFLAGS_ARCH="-m64 -mindirect-branch=thunk -mfunction-return=thunk -mtune=generic"
148                         ;;
149
150                 i586)
151                         BUILDTARGET="${build_arch}-pc-linux-gnu"
152                         CROSSTARGET="${build_arch}-cross-linux-gnu"
153                         BUILD_PLATFORM="x86"
154                         CFLAGS_ARCH="-march=i586 -mindirect-branch=thunk -mfunction-return=thunk -mtune=generic -fomit-frame-pointer"
155                         ;;
156
157                 aarch64)
158                         BUILDTARGET="${build_arch}-unknown-linux-gnu"
159                         CROSSTARGET="${build_arch}-cross-linux-gnu"
160                         BUILD_PLATFORM="arm"
161                         CFLAGS_ARCH=""
162                         ;;
163
164                 armv7hl)
165                         BUILDTARGET="${build_arch}-unknown-linux-gnueabi"
166                         CROSSTARGET="${build_arch}-cross-linux-gnueabi"
167                         BUILD_PLATFORM="arm"
168                         CFLAGS_ARCH="-march=armv7-a -mfpu=vfpv3-d16 -mfloat-abi=hard"
169                         ;;
170
171                 armv5tel)
172                         BUILDTARGET="${build_arch}-unknown-linux-gnueabi"
173                         CROSSTARGET="${build_arch}-cross-linux-gnueabi"
174                         BUILD_PLATFORM="arm"
175                         CFLAGS_ARCH="-march=armv5te -mfloat-abi=soft -fomit-frame-pointer"
176                         ;;
177
178                 *)
179                         exiterror "Cannot build for architure ${build_arch}"
180                         ;;
181         esac
182
183         # Check if the QEMU helper is available if needed.
184         if qemu_is_required "${build_arch}"; then
185                 local qemu_build_helper="$(qemu_find_build_helper_name "${build_arch}")"
186
187                 if [ -n "${qemu_build_helper}" ]; then
188                         QEMU_TARGET_HELPER="${qemu_build_helper}"
189                 else
190                         exiterror "Could not find a binfmt_misc helper entry for ${build_arch}"
191                 fi
192         fi
193
194         BUILD_ARCH="${build_arch}"
195         TOOLS_DIR="/tools_${BUILD_ARCH}"
196
197         # Enables hardening
198         HARDENING_CFLAGS="-Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fstack-protector-strong"
199
200         CFLAGS="-O2 -pipe -Wall -fexceptions -fPIC ${CFLAGS_ARCH}"
201         CXXFLAGS="${CFLAGS}"
202
203         # Determine parallelism
204         if [ -z "${MAKETUNING}" ]; then
205                 # We assume that each process consumes about
206                 # 192MB of memory. Therefore we find out how
207                 # many processes fit into memory.
208                 local mem_max=$(( ${HOST_MEM} / 192 ))
209
210                 local processors="$(system_processors)"
211                 local cpu_max=$(( ${processors} + 1 ))
212
213                 local parallelism
214                 if [ ${mem_max} -lt ${cpu_max} ]; then
215                         parallelism=${mem_max}
216                 else
217                         parallelism=${cpu_max}
218                 fi
219
220                 # limit to -j23 because perl will not build
221                 # more
222                 if [ ${parallelism} -gt 23 ]; then
223                         parallelism=23
224                 fi
225
226                 MAKETUNING="-j${parallelism}"
227         fi
228 }
229
230 configure_build_guess() {
231         case "${HOST_ARCH}" in
232                 x86_64|i686|i586)
233                         echo "i586"
234                         ;;
235
236                 aarch64)
237                         echo "aarch64"
238                         ;;
239
240                 armv7*|armv6*|armv5*)
241                         echo "armv5tel"
242                         ;;
243
244                 *)
245                         exiterror "Cannot guess build architecture"
246                         ;;
247         esac
248 }
249
250 stdumount() {
251         umount $BASEDIR/build/sys                       2>/dev/null;
252         umount $BASEDIR/build/dev/shm           2>/dev/null;
253         umount $BASEDIR/build/dev/pts           2>/dev/null;
254         umount $BASEDIR/build/dev                       2>/dev/null;
255         umount $BASEDIR/build/proc                      2>/dev/null;
256         umount $BASEDIR/build/install/mnt               2>/dev/null;
257         umount $BASEDIR/build/usr/src/cache     2>/dev/null;
258         umount $BASEDIR/build/usr/src/ccache    2>/dev/null;
259         umount $BASEDIR/build/usr/src/config    2>/dev/null;
260         umount $BASEDIR/build/usr/src/doc               2>/dev/null;
261         umount $BASEDIR/build/usr/src/html              2>/dev/null;
262         umount $BASEDIR/build/usr/src/langs     2>/dev/null;
263         umount $BASEDIR/build/usr/src/lfs               2>/dev/null;
264         umount $BASEDIR/build/usr/src/log               2>/dev/null;
265         umount $BASEDIR/build/usr/src/src               2>/dev/null;
266 }
267
268 now() {
269         date -u "+%s"
270 }
271
272 format_runtime() {
273         local seconds=${1}
274
275         if [ ${seconds} -ge 3600 ]; then
276                 printf "%d:%02d:%02d\n" \
277                         "$(( seconds / 3600 ))" \
278                         "$(( seconds % 3600 / 60 ))" \
279                         "$(( seconds % 3600 % 60 ))"
280         elif [ ${seconds} -ge 60 ]; then
281                 printf "%d:%02d\n" \
282                         "$(( seconds / 60 ))" \
283                         "$(( seconds % 60 ))"
284         else
285                 printf "%d\n" "${seconds}"
286         fi
287 }
288
289 print_line() {
290         local line="$@"
291
292         printf "%-${LINE_WIDTH}s" "${line}"
293 }
294
295 _print_line() {
296         local status="${1}"
297         shift
298
299         if ${INTERACTIVE}; then
300                 printf "${!status}"
301         fi
302
303         print_line "$@"
304
305         if ${INTERACTIVE}; then
306                 printf "${NORMAL}"
307         fi
308 }
309
310 print_headline() {
311         _print_line BOLD "$@"
312 }
313
314 print_error() {
315         _print_line FAIL "$@"
316 }
317
318 print_package() {
319         local name="${1}"
320         shift
321
322         local version="$(grep -E "^VER |^VER=|^VER      " $BASEDIR/lfs/${name} | awk '{ print $3 }')"
323         local options="$@"
324
325         local string="${name}"
326         if [ -n "${version}" ] && [ "${version}" != "ipfire" ]; then
327                 string="${string} (${version})"
328         fi
329
330         printf "%-$(( ${NAME_WIDTH} - 1 ))s " "${string}"
331         printf "%$(( ${OPTIONS_WIDTH} - 1 ))s " "${options}"
332 }
333
334 print_runtime() {
335         local runtime=$(format_runtime $@)
336
337         if ${INTERACTIVE}; then
338                 printf "\\033[${TIME_COL}G[ ${BOLD}%$(( ${TIME_WIDTH} - 4 ))s${NORMAL} ]" "${runtime}"
339         else
340                 printf "[ %$(( ${TIME_WIDTH} - 4 ))s ]" "${runtime}"
341         fi
342 }
343
344 print_status() {
345         local status="${1}"
346
347         local color="${!status}"
348
349         if ${INTERACTIVE}; then
350                 printf "\\033[${STATUS_COL}G[${color-${BOLD}} %-$(( ${STATUS_WIDTH} - 4 ))s ${NORMAL}]\n" "${status}"
351         else
352                 printf "[ %-$(( ${STATUS_WIDTH} - 4 ))s ]\n" "${status}"
353         fi
354 }
355
356 print_build_stage() {
357         print_headline "$@"
358
359         # end line
360         printf "\n"
361 }
362
363 print_build_summary() {
364         local runtime=$(format_runtime $@)
365
366         print_line "*** Build finished in ${runtime}"
367         print_status DONE
368 }
369
370 exiterror() {
371         stdumount
372         for i in `seq 0 7`; do
373                 if ( losetup /dev/loop${i} 2>/dev/null | grep -q "/install/images" ); then
374                 losetup -d /dev/loop${i} 2>/dev/null
375                 fi;
376         done
377
378         # Dump logfile
379         if [ -n "${LOGFILE}" ] && [ -e "${LOGFILE}" ]; then
380                 echo # empty line
381
382                 local line
383                 while read -r line; do
384                         echo "    ${line}"
385                 done <<< "$(tail -n30 ${LOGFILE})"
386         fi
387
388         echo # empty line
389
390         local line
391         for line in "ERROR: $@" "    Check ${LOGFILE} for errors if applicable"; do
392                 print_error "${line}"
393                 print_status FAIL
394         done
395
396         exit 1
397 }
398
399 prepareenv() {
400         # Are we running the right shell?
401         if [ -z "${BASH}" ]; then
402                 exiterror "BASH environment variable is not set.  You're probably running the wrong shell."
403         fi
404
405         if [ -z "${BASH_VERSION}" ]; then
406                 exiterror "Not running BASH shell."
407         fi
408
409         # Trap on emergency exit
410         trap "exiterror 'Build process interrupted'" SIGINT SIGTERM SIGKILL SIGSTOP SIGQUIT
411
412         # Resetting our nice level
413         if ! renice ${NICE} $$ >/dev/null; then
414                         exiterror "Failed to set nice level to ${NICE}"
415         fi
416
417         # Checking if running as root user
418         if [ $(id -u) -ne 0 ]; then
419                         exiterror "root privileges required for building"
420         fi
421
422         # Checking for necessary temporary space
423         print_line "Checking for necessary space on disk $BASE_DEV"
424         BASE_DEV=`df -P -k $BASEDIR | tail -n 1 | awk '{ print $1 }'`
425         BASE_ASPACE=`df -P -k $BASEDIR | tail -n 1 | awk '{ print $4 }'`
426         if (( 2048000 > $BASE_ASPACE )); then
427                         BASE_USPACE=`du -skx $BASEDIR | awk '{print $1}'`
428                         if (( 2048000 - $BASE_USPACE > $BASE_ASPACE )); then
429                                 print_status FAIL
430                                 exiterror "Not enough temporary space available, need at least 2GB on $BASE_DEV"
431                         fi
432         else
433                         print_status DONE
434         fi
435
436         # Set umask
437         umask 022
438
439         # Set LFS Directory
440         LFS=$BASEDIR/build
441
442         # Setup environment
443         set +h
444         LC_ALL=POSIX
445         export LFS LC_ALL CFLAGS CXXFLAGS MAKETUNING
446         unset CC CXX CPP LD_LIBRARY_PATH LD_PRELOAD
447
448         # Make some extra directories
449         mkdir -p "${BASEDIR}/build${TOOLS_DIR}" 2>/dev/null
450         mkdir -p $BASEDIR/build/{etc,usr/src} 2>/dev/null
451         mkdir -p $BASEDIR/build/{dev/{shm,pts},proc,sys}
452         mkdir -p $BASEDIR/{cache,ccache} 2>/dev/null
453         mkdir -p $BASEDIR/build/usr/src/{cache,config,doc,html,langs,lfs,log,src,ccache}
454
455         mknod -m 600 $BASEDIR/build/dev/console c 5 1 2>/dev/null
456         mknod -m 666 $BASEDIR/build/dev/null c 1 3 2>/dev/null
457
458         # Make all sources and proc available under lfs build
459         mount --bind /dev            $BASEDIR/build/dev
460         mount --bind /dev/pts        $BASEDIR/build/dev/pts
461         mount --bind /dev/shm        $BASEDIR/build/dev/shm
462         mount --bind /proc           $BASEDIR/build/proc
463         mount --bind /sys            $BASEDIR/build/sys
464         mount --bind $BASEDIR/cache  $BASEDIR/build/usr/src/cache
465         mount --bind $BASEDIR/ccache $BASEDIR/build/usr/src/ccache
466         mount --bind $BASEDIR/config $BASEDIR/build/usr/src/config
467         mount --bind $BASEDIR/doc    $BASEDIR/build/usr/src/doc
468         mount --bind $BASEDIR/html   $BASEDIR/build/usr/src/html
469         mount --bind $BASEDIR/langs  $BASEDIR/build/usr/src/langs
470         mount --bind $BASEDIR/lfs    $BASEDIR/build/usr/src/lfs
471         mount --bind $BASEDIR/log    $BASEDIR/build/usr/src/log
472         mount --bind $BASEDIR/src    $BASEDIR/build/usr/src/src
473
474         # Run LFS static binary creation scripts one by one
475         export CCACHE_DIR=$BASEDIR/ccache
476         export CCACHE_COMPRESS=1
477         export CCACHE_COMPILERCHECK="string:toolchain-${TOOLCHAINVER} ${BUILD_ARCH}"
478
479         # Remove pre-install list of installed files in case user erase some files before rebuild
480         rm -f $BASEDIR/build/usr/src/lsalr 2>/dev/null
481
482         # Prepare string for /etc/system-release.
483         SYSTEM_RELEASE="${NAME} ${VERSION} (${BUILD_ARCH})"
484         if [ "$(git status -s | wc -l)" == "0" ]; then
485         GIT_STATUS=""
486         else
487         GIT_STATUS="-dirty"
488         fi
489         case "$GIT_BRANCH" in
490         core*|beta?|rc?)
491                 SYSTEM_RELEASE="${SYSTEM_RELEASE} - $GIT_BRANCH$GIT_STATUS"
492                 ;;
493         *)
494                 SYSTEM_RELEASE="${SYSTEM_RELEASE} - Development Build: $GIT_BRANCH/$GIT_LASTCOMMIT$GIT_STATUS"
495                 ;;
496         esac
497 }
498
499 enterchroot() {
500         # Install QEMU helper, if needed
501         qemu_install_helper
502
503         local PATH="${TOOLS_DIR}/ccache/bin:/bin:/usr/bin:/sbin:/usr/sbin:${TOOLS_DIR}/bin"
504
505         PATH="${PATH}" chroot ${LFS} env -i \
506                 HOME="/root" \
507                 TERM="${TERM}" \
508                 PS1="${PS1}" \
509                 PATH="${PATH}" \
510                 SYSTEM_RELEASE="${SYSTEM_RELEASE}" \
511                 PAKFIRE_CORE="${PAKFIRE_CORE}" \
512                 NAME="${NAME}" \
513                 SNAME="${SNAME}" \
514                 VERSION="${VERSION}" \
515                 CORE="${CORE}" \
516                 SLOGAN="${SLOGAN}" \
517                 TOOLS_DIR="${TOOLS_DIR}" \
518                 CONFIG_ROOT="${CONFIG_ROOT}" \
519                 CFLAGS="${CFLAGS} ${HARDENING_CFLAGS}" \
520                 CXXFLAGS="${CXXFLAGS} ${HARDENING_CFLAGS}" \
521                 BUILDTARGET="${BUILDTARGET}" \
522                 CROSSTARGET="${CROSSTARGET}" \
523                 BUILD_ARCH="${BUILD_ARCH}" \
524                 BUILD_PLATFORM="${BUILD_PLATFORM}" \
525                 CCACHE_DIR=/usr/src/ccache \
526                 CCACHE_COMPRESS="${CCACHE_COMPRESS}" \
527                 CCACHE_COMPILERCHECK="${CCACHE_COMPILERCHECK}" \
528                 KVER="${KVER}" \
529                 XZ_OPT="${XZ_OPT}" \
530                 $(fake_environ) \
531                 $(qemu_environ) \
532                 "$@"
533 }
534
535 entershell() {
536         if [ ! -e $BASEDIR/build/usr/src/lfs/ ]; then
537                 exiterror "No such file or directory: $BASEDIR/build/usr/src/lfs/"
538         fi
539
540         echo "Entering to a shell inside LFS chroot, go out with exit"
541         local PS1="ipfire build chroot (${BUILD_ARCH}) \u:\w\$ "
542
543         if enterchroot bash -i; then
544                 stdumount
545         else
546                 print_status FAIL
547                 exiterror "chroot error"
548         fi
549 }
550
551 lfsmakecommoncheck() {
552         # Script present?
553         if [ ! -f $BASEDIR/lfs/$1 ]; then
554                 exiterror "No such file or directory: $BASEDIR/$1"
555         fi
556
557         # Print package name and version
558         print_package $@
559
560         # Check if this package is supported by our architecture.
561         # If no SUP_ARCH is found, we assume the package can be built for all.
562         if grep "^SUP_ARCH" ${BASEDIR}/lfs/${1} >/dev/null; then
563                 # Check if package supports ${BUILD_ARCH} or all architectures.
564                 if ! grep -E "^SUP_ARCH.*${BUILD_ARCH}|^SUP_ARCH.*all" ${BASEDIR}/lfs/${1} >/dev/null; then
565                         print_runtime 0
566                         print_status SKIP
567                         return 1
568                 fi
569         fi
570
571         # Script slipped?
572         local i
573         for i in $SKIP_PACKAGE_LIST
574         do
575                 if [ "$i" == "$1" ]; then
576                         print_status SKIP
577                         return 1;
578                 fi
579         done
580
581         echo -ne "`date -u '+%b %e %T'`: Building $* " >> $LOGFILE
582
583         cd $BASEDIR/lfs && make -s -f $* LFS_BASEDIR=$BASEDIR BUILD_ARCH="${BUILD_ARCH}" \
584                 MESSAGE="$1\t " download  >> $LOGFILE 2>&1
585         if [ $? -ne 0 ]; then
586                 exiterror "Download error in $1"
587         fi
588
589         cd $BASEDIR/lfs && make -s -f $* LFS_BASEDIR=$BASEDIR BUILD_ARCH="${BUILD_ARCH}" \
590                 MESSAGE="$1\t md5sum" md5  >> $LOGFILE 2>&1
591         if [ $? -ne 0 ]; then
592                 exiterror "md5sum error in $1, check file in cache or signature"
593         fi
594
595         return 0        # pass all!
596 }
597
598 lfsmake1() {
599         lfsmakecommoncheck $*
600         [ $? == 1 ] && return 0
601
602         cd $BASEDIR/lfs && env -i \
603                 PATH="${TOOLS_DIR}/ccache/bin:${TOOLS_DIR}/bin:$PATH" \
604                 CCACHE_DIR="${CCACHE_DIR}" \
605                 CCACHE_COMPRESS="${CCACHE_COMPRESS}" \
606                 CCACHE_COMPILERCHECK="${CCACHE_COMPILERCHECK}" \
607                 CFLAGS="${CFLAGS}" \
608                 CXXFLAGS="${CXXFLAGS}" \
609                 MAKETUNING="${MAKETUNING}" \
610                 make -f $* \
611                         TOOLCHAIN=1 \
612                         TOOLS_DIR="${TOOLS_DIR}" \
613                         CROSSTARGET="${CROSSTARGET}" \
614                         BUILDTARGET="${BUILDTARGET}" \
615                         BUILD_ARCH="${BUILD_ARCH}" \
616                         BUILD_PLATFORM="${BUILD_PLATFORM}" \
617                         LFS_BASEDIR="${BASEDIR}" \
618                         ROOT="${LFS}" \
619                         KVER="${KVER}" \
620                         install >> $LOGFILE 2>&1 &
621
622         if ! wait_until_finished $!; then
623                 print_status FAIL
624                 exiterror "Building $*"
625         fi
626
627         print_status DONE
628 }
629
630 lfsmake2() {
631         lfsmakecommoncheck $*
632         [ $? == 1 ] && return 0
633
634         local PS1='\u:\w$ '
635
636         enterchroot \
637                 ${EXTRA_PATH}bash -x -c "cd /usr/src/lfs && \
638                         MAKETUNING=${MAKETUNING} \
639                         make -f $* \
640                         LFS_BASEDIR=/usr/src install" \
641                 >> ${LOGFILE} 2>&1 &
642
643         if ! wait_until_finished $!; then
644                 print_status FAIL
645                 exiterror "Building $*"
646         fi
647
648         print_status DONE
649 }
650
651 ipfiredist() {
652         lfsmakecommoncheck $*
653         [ $? == 1 ] && return 0
654
655         local PS1='\u:\w$ '
656
657         enterchroot \
658                 bash -x -c "cd /usr/src/lfs && make -f $* LFS_BASEDIR=/usr/src dist" \
659                 >> ${LOGFILE} 2>&1 &
660
661         if ! wait_until_finished $!; then
662                 print_status FAIL
663                 exiterror "Packaging $*"
664         fi
665
666         print_status DONE
667 }
668
669 wait_until_finished() {
670         local pid=${1}
671
672         local start_time=$(now)
673
674         # Show progress
675         if ${INTERACTIVE}; then
676                 # Wait a little just in case the process
677                 # has finished very quickly.
678                 sleep 0.1
679
680                 local runtime
681                 while kill -0 ${pid} 2>/dev/null; do
682                         print_runtime $(( $(now) - ${start_time} ))
683
684                         # Wait a little
685                         sleep 1
686                 done
687         fi
688
689         # Returns the exit code of the child process
690         wait ${pid}
691         local ret=$?
692
693         if ! ${INTERACTIVE}; then
694                 print_runtime $(( $(now) - ${start_time} ))
695         fi
696
697         return ${ret}
698 }
699
700 fake_environ() {
701         [ -e "${BASEDIR}/build${TOOLS_DIR}/lib/libpakfire_preload.so" ] || return
702
703         local env="LD_PRELOAD=${TOOLS_DIR}/lib/libpakfire_preload.so"
704
705         # Fake kernel version, because some of the packages do not compile
706         # with kernel 3.0 and later.
707         env="${env} UTS_RELEASE=${KVER}"
708
709         # Fake machine version.
710         env="${env} UTS_MACHINE=${BUILD_ARCH}"
711
712         echo "${env}"
713 }
714
715 qemu_environ() {
716         local env
717
718         # Don't add anything if qemu is not used.
719         if ! qemu_is_required; then
720                 return
721         fi
722
723         # Set default qemu options
724         case "${BUILD_ARCH}" in
725                 arm*)
726                         QEMU_CPU="${QEMU_CPU:-cortex-a9}"
727
728                         env="${env} QEMU_CPU=${QEMU_CPU}"
729                         ;;
730         esac
731
732         # Enable QEMU strace
733         #env="${env} QEMU_STRACE=1"
734
735         echo "${env}"
736 }
737
738 qemu_is_required() {
739         local build_arch="${1}"
740
741         if [ -z "${build_arch}" ]; then
742                 build_arch="${BUILD_ARCH}"
743         fi
744
745         case "${HOST_ARCH},${build_arch}" in
746                 x86_64,arm*|i?86,arm*|i?86,x86_64)
747                         return 0
748                         ;;
749                 *)
750                         return 1
751                         ;;
752         esac
753 }
754
755 qemu_install_helper() {
756         # Do nothing, if qemu is not required
757         if ! qemu_is_required; then
758                 return 0
759         fi
760
761         if [ ! -e /proc/sys/fs/binfmt_misc/status ]; then
762                 exiterror "binfmt_misc not mounted. QEMU_TARGET_HELPER not useable."
763         fi
764
765         if [ ! $(cat /proc/sys/fs/binfmt_misc/status) = 'enabled' ]; then
766                 exiterror "binfmt_misc not enabled. QEMU_TARGET_HELPER not useable."
767         fi
768
769
770         if [ -z "${QEMU_TARGET_HELPER}" ]; then
771                 exiterror "QEMU_TARGET_HELPER not set"
772         fi
773
774         # Check if the helper is already installed.
775         if [ -x "${LFS}${QEMU_TARGET_HELPER}" ]; then
776                 return 0
777         fi
778
779         # Try to find a suitable binary that we can install
780         # to the build environment.
781         local file
782         for file in "${QEMU_TARGET_HELPER}" "${QEMU_TARGET_HELPER}-static"; do
783                 # file must exist and be executable.
784                 [ -x "${file}" ] || continue
785
786                 # Must be static.
787                 file_is_static "${file}" || continue
788
789                 local dirname="${LFS}$(dirname "${file}")"
790                 mkdir -p "${dirname}"
791
792                 install -m 755 "${file}" "${LFS}${QEMU_TARGET_HELPER}"
793                 return 0
794         done
795
796         exiterror "Could not find a statically-linked QEMU emulator: ${QEMU_TARGET_HELPER}"
797 }
798
799 qemu_find_build_helper_name() {
800         local build_arch="${1}"
801
802         local magic
803         case "${build_arch}" in
804                 arm*)
805                         magic="7f454c4601010100000000000000000002002800"
806                         ;;
807                 x86_64)
808                         magic="7f454c4602010100000000000000000002003e00"
809                         ;;
810         esac
811
812         [ -z "${magic}" ] && return 1
813
814         local file
815         for file in /proc/sys/fs/binfmt_misc/*; do
816                 # skip write only register entry
817                 [ $(basename "${file}") = "register" ] && continue
818                 # Search for the file with the correct magic value.
819                 grep -qE "^magic ${magic}$" "${file}" || continue
820
821                 local interpreter="$(grep "^interpreter" "${file}" | awk '{ print $2 }')"
822
823                 [ -n "${interpreter}" ] || continue
824                 [ "${interpreter:0:1}" = "/" ] || continue
825                 [ -x "${interpreter}" ] || continue
826
827                 echo "${interpreter}"
828                 return 0
829         done
830
831         return 1
832 }
833
834 file_is_static() {
835         local file="${1}"
836
837         file ${file} 2>/dev/null | grep -q "statically linked"
838 }
839
840 update_language_list() {
841         local path="${1}"
842
843         local lang
844         for lang in ${path}/*.po; do
845                 lang="$(basename "${lang}")"
846                 echo "${lang%*.po}"
847         done | sort -u > "${path}/LINGUAS"
848 }
849
850 # Load configuration file
851 if [ -f .config ]; then
852         . .config
853 fi
854
855 # TARGET_ARCH is BUILD_ARCH now
856 if [ -n "${TARGET_ARCH}" ]; then
857         BUILD_ARCH="${TARGET_ARCH}"
858         unset TARGET_ARCH
859 fi
860
861 # Get the amount of memory in this build system
862 HOST_MEM=$(system_memory)
863
864 # Checking host memory, tuning XZ_OPT
865 if [ ${HOST_MEM} -lt 1024 ]; then
866         exiterror "You will need more than 1GB or host memory to run the build"
867 fi
868
869 XZ_MEM="$(( HOST_MEM * 7 / 10 ))MiB"
870 XZ_OPT="-T4 -8 --memory=$XZ_MEM"
871
872 if [ -n "${BUILD_ARCH}" ]; then
873         configure_build "${BUILD_ARCH}"
874 else
875         configure_build "default"
876 fi
877
878 buildtoolchain() {
879         local error=false
880         case "${BUILD_ARCH}:${HOST_ARCH}" in
881                 # x86_64
882                 x86_64:x86_64)
883                          # This is working.
884                          ;;
885
886                 # x86
887                 i586:i586|i586:i686|i586:x86_64)
888                         # These are working.
889                         ;;
890                 i586:*)
891                         error=true
892                         ;;
893
894                 # ARM
895                 arvm7hl:armv7hl|armv7hl:armv7l)
896                         # These are working.
897                         ;;
898
899                 armv5tel:armv5tel|armv5tel:armv5tejl|armv5tel:armv6l|armv5tel:armv7l|armv5tel:aarch64)
900                         # These are working.
901                         ;;
902                 armv5tel:*)
903                         error=true
904                         ;;
905         esac
906
907         ${error} && \
908                 exiterror "Cannot build ${BUILD_ARCH} toolchain on $(uname -m). Please use the download if any."
909
910         local gcc=$(type -p gcc)
911         if [ -z "${gcc}" ]; then
912                 exiterror "Could not find GCC. You will need a working build enviroment in order to build the toolchain."
913         fi
914
915         # Check ${TOOLS_DIR} symlink
916         if [ -h "${TOOLS_DIR}" ]; then
917                 rm -f "${TOOLS_DIR}"
918         fi
919
920         if [ ! -e "${TOOLS_DIR}" ]; then
921                 ln -s "${BASEDIR}/build${TOOLS_DIR}" "${TOOLS_DIR}"
922         fi
923
924         if [ ! -h "${TOOLS_DIR}" ]; then
925                 exiterror "Could not create ${TOOLS_DIR} symbolic link"
926         fi
927
928         LOGFILE="$BASEDIR/log/_build.toolchain.log"
929         export LOGFILE
930
931         lfsmake1 stage1
932         lfsmake1 ccache                 PASS=1
933         lfsmake1 binutils                       PASS=1
934         lfsmake1 gcc                    PASS=1
935         lfsmake1 linux                  KCFG="-headers"
936         lfsmake1 glibc
937         lfsmake1 gcc                    PASS=L
938         lfsmake1 binutils                       PASS=2
939         lfsmake1 gcc                    PASS=2
940         lfsmake1 zlib
941         lfsmake1 ccache                 PASS=2
942         lfsmake1 tcl
943         lfsmake1 expect
944         lfsmake1 dejagnu
945         lfsmake1 pkg-config
946         lfsmake1 ncurses
947         lfsmake1 bash
948         lfsmake1 bzip2
949         lfsmake1 automake
950         lfsmake1 coreutils
951         lfsmake1 diffutils
952         lfsmake1 findutils
953         lfsmake1 gawk
954         lfsmake1 gettext
955         lfsmake1 grep
956         lfsmake1 gzip
957         lfsmake1 m4
958         lfsmake1 make
959         lfsmake1 patch
960         lfsmake1 perl
961         lfsmake1 sed
962         lfsmake1 tar
963         lfsmake1 texinfo
964         lfsmake1 xz
965         lfsmake1 bison
966         lfsmake1 flex
967         lfsmake1 fake-environ
968         lfsmake1 strip
969         lfsmake1 cleanup-toolchain
970 }
971
972 buildbase() {
973         LOGFILE="$BASEDIR/log/_build.base.log"
974         export LOGFILE
975         lfsmake2 stage2
976         lfsmake2 linux                  KCFG="-headers"
977         lfsmake2 man-pages
978         lfsmake2 glibc
979         lfsmake2 tzdata
980         lfsmake2 cleanup-toolchain
981         lfsmake2 zlib
982         lfsmake2 binutils
983         lfsmake2 gmp
984         lfsmake2 gmp-compat
985         lfsmake2 mpfr
986         lfsmake2 libmpc
987         lfsmake2 file
988         lfsmake2 gcc
989         lfsmake2 sed
990         lfsmake2 autoconf
991         lfsmake2 automake
992         lfsmake2 berkeley
993         lfsmake2 coreutils
994         lfsmake2 iana-etc
995         lfsmake2 m4
996         lfsmake2 bison
997         lfsmake2 ncurses
998         lfsmake2 procps
999         lfsmake2 libtool
1000         lfsmake2 perl
1001         lfsmake2 readline
1002         lfsmake2 readline-compat
1003         lfsmake2 bzip2
1004         lfsmake2 pcre
1005         lfsmake2 pcre-compat
1006         lfsmake2 bash
1007         lfsmake2 diffutils
1008         lfsmake2 e2fsprogs
1009         lfsmake2 ed
1010         lfsmake2 findutils
1011         lfsmake2 flex
1012         lfsmake2 gawk
1013         lfsmake2 gettext
1014         lfsmake2 grep
1015         lfsmake2 groff
1016         lfsmake2 gperf
1017         lfsmake2 gzip
1018         lfsmake2 hostname
1019         lfsmake2 iproute2
1020         lfsmake2 jwhois
1021         lfsmake2 kbd
1022         lfsmake2 less
1023         lfsmake2 pkg-config
1024         lfsmake2 make
1025         lfsmake2 man
1026         lfsmake2 kmod
1027         lfsmake2 net-tools
1028         lfsmake2 patch
1029         lfsmake2 psmisc
1030         lfsmake2 shadow
1031         lfsmake2 sysklogd
1032         lfsmake2 sysvinit
1033         lfsmake2 tar
1034         lfsmake2 texinfo
1035         lfsmake2 util-linux
1036         lfsmake2 udev
1037         lfsmake2 vim
1038         lfsmake2 xz
1039         lfsmake2 paxctl
1040 }
1041
1042 buildipfire() {
1043   LOGFILE="$BASEDIR/log/_build.ipfire.log"
1044   export LOGFILE
1045   lfsmake2 configroot
1046   lfsmake2 initscripts
1047   lfsmake2 backup
1048   lfsmake2 libusb
1049   lfsmake2 libusb-compat
1050   lfsmake2 libpcap
1051   lfsmake2 ppp
1052   lfsmake2 pptp
1053   lfsmake2 unzip
1054   lfsmake2 which
1055   lfsmake2 linux-firmware
1056   lfsmake2 dvb-firmwares
1057   lfsmake2 xr819-firmware
1058   lfsmake2 zd1211-firmware
1059   lfsmake2 rpi-firmware
1060   lfsmake2 bc
1061   lfsmake2 u-boot MKIMAGE=1
1062   lfsmake2 cpio
1063   lfsmake2 mdadm
1064   lfsmake2 dracut
1065   lfsmake2 lvm2
1066   lfsmake2 multipath-tools
1067   lfsmake2 freetype
1068   lfsmake2 grub
1069   lfsmake2 libmnl
1070   lfsmake2 libnfnetlink
1071   lfsmake2 libnetfilter_queue
1072   lfsmake2 libnetfilter_conntrack
1073   lfsmake2 libnetfilter_cthelper
1074   lfsmake2 libnetfilter_cttimeout
1075   lfsmake2 iptables
1076   lfsmake2 screen
1077   lfsmake2 elfutils
1078
1079   case "${BUILD_ARCH}" in
1080         x86_64)
1081                 lfsmake2 linux                  KCFG=""
1082 #               lfsmake2 backports                      KCFG=""
1083 #               lfsmake2 e1000e                 KCFG=""
1084 #               lfsmake2 igb                            KCFG=""
1085 #               lfsmake2 ixgbe                  KCFG=""
1086                 lfsmake2 xtables-addons         KCFG=""
1087                 lfsmake2 linux-initrd                   KCFG=""
1088                 ;;
1089         i586)
1090                 # x86-pae (Native and new XEN) kernel build
1091                 lfsmake2 linux                  KCFG="-pae"
1092 #               lfsmake2 backports                      KCFG="-pae"
1093 #               lfsmake2 e1000e                 KCFG="-pae"
1094 #               lfsmake2 igb                            KCFG="-pae"
1095 #               lfsmake2 ixgbe                  KCFG="-pae"
1096                 lfsmake2 xtables-addons         KCFG="-pae"
1097                 lfsmake2 linux-initrd                   KCFG="-pae"
1098
1099                 # x86 kernel build
1100                 lfsmake2 linux                  KCFG=""
1101 #               lfsmake2 backports                      KCFG=""
1102 #               lfsmake2 e1000e                 KCFG=""
1103 #               lfsmake2 igb                            KCFG=""
1104 #               lfsmake2 ixgbe                  KCFG=""
1105                 lfsmake2 xtables-addons         KCFG=""
1106                 lfsmake2 linux-initrd                   KCFG=""
1107                 ;;
1108
1109         armv5tel)
1110                 # arm-kirkwood (Dreamplug, ICY-Box ...) kernel build
1111                 lfsmake2 linux                  KCFG="-kirkwood"
1112 #               lfsmake2 backports                      KCFG="-kirkwood"
1113 #               lfsmake2 e1000e                 KCFG="-kirkwood"
1114 #               lfsmake2 igb                            KCFG="-kirkwood"
1115 #               lfsmake2 ixgbe                  KCFG="-kirkwood"
1116                 lfsmake2 xtables-addons         KCFG="-kirkwood"
1117                 lfsmake2 linux-initrd                   KCFG="-kirkwood"
1118
1119                 # arm multi platform (Panda, Wandboard ...) kernel build
1120                 lfsmake2 linux                  KCFG="-multi"
1121 #               lfsmake2 backports                      KCFG="-multi"
1122 #               lfsmake2 e1000e                 KCFG="-multi"
1123 #               lfsmake2 igb                            KCFG="-multi"
1124 #               lfsmake2 ixgbe                  KCFG="-multi"
1125                 lfsmake2 xtables-addons         KCFG="-multi"
1126                 lfsmake2 linux-initrd                   KCFG="-multi"
1127                 ;;
1128   esac
1129   lfsmake2 xtables-addons                       USPACE="1"
1130   lfsmake2 openssl
1131   [ "${BUILD_ARCH}" = "i586" ] && lfsmake2 openssl KCFG='-sse2'
1132   lfsmake2 openssl-compat
1133   lfsmake2 libgpg-error
1134   lfsmake2 libgcrypt
1135   lfsmake2 libassuan
1136   lfsmake2 nettle
1137   lfsmake2 libevent
1138   lfsmake2 libevent2
1139   lfsmake2 expat
1140   lfsmake2 apr
1141   lfsmake2 aprutil
1142   lfsmake2 unbound
1143   lfsmake2 gnutls
1144   lfsmake2 bind
1145   lfsmake2 dhcp
1146   lfsmake2 dhcpcd
1147   lfsmake2 boost
1148   lfsmake2 linux-atm
1149   lfsmake2 gdbm
1150   lfsmake2 pam
1151   lfsmake2 curl
1152   lfsmake2 tcl
1153   lfsmake2 sqlite
1154   lfsmake2 libffi
1155   lfsmake2 python
1156   lfsmake2 python3
1157   lfsmake2 ca-certificates
1158   lfsmake2 fireinfo
1159   lfsmake2 libnet
1160   lfsmake2 libnl
1161   lfsmake2 libnl-3
1162   lfsmake2 libidn
1163   lfsmake2 nasm
1164   lfsmake2 libjpeg
1165   lfsmake2 libjpeg-compat
1166   lfsmake2 libexif
1167   lfsmake2 libpng
1168   lfsmake2 libtiff
1169   lfsmake2 libart
1170   lfsmake2 gd
1171   lfsmake2 popt
1172   lfsmake2 slang
1173   lfsmake2 newt
1174   lfsmake2 libsmooth
1175   lfsmake2 attr
1176   lfsmake2 acl
1177   lfsmake2 libcap
1178   lfsmake2 pciutils
1179   lfsmake2 usbutils
1180   lfsmake2 libxml2
1181   lfsmake2 libxslt
1182   lfsmake2 BerkeleyDB
1183   lfsmake2 cyrus-sasl
1184   lfsmake2 openldap
1185   lfsmake2 apache2
1186   lfsmake2 web-user-interface
1187   lfsmake2 flag-icons
1188   lfsmake2 jquery
1189   lfsmake2 bootstrap
1190   lfsmake2 arping
1191   lfsmake2 beep
1192   lfsmake2 dvdrtools
1193   lfsmake2 dosfstools
1194   lfsmake2 reiserfsprogs
1195   lfsmake2 xfsprogs
1196   lfsmake2 sysfsutils
1197   lfsmake2 fuse
1198   lfsmake2 ntfs-3g
1199   lfsmake2 ethtool
1200   lfsmake2 ez-ipupdate
1201   lfsmake2 fcron
1202   lfsmake2 perl-GD
1203   lfsmake2 GD-Graph
1204   lfsmake2 GD-TextUtil
1205   lfsmake2 perl-Device-SerialPort
1206   lfsmake2 perl-Device-Modem
1207   lfsmake2 perl-Apache-Htpasswd
1208   lfsmake2 gnupg
1209   lfsmake2 hdparm
1210   lfsmake2 sdparm
1211   lfsmake2 mtools
1212   lfsmake2 whatmask
1213   lfsmake2 conntrack-tools
1214   lfsmake2 libupnp
1215   lfsmake2 ipaddr
1216   lfsmake2 iputils
1217   lfsmake2 l7-protocols
1218   lfsmake2 hwdata
1219   lfsmake2 logrotate
1220   lfsmake2 logwatch
1221   lfsmake2 misc-progs
1222   lfsmake2 nano
1223   lfsmake2 URI
1224   lfsmake2 HTML-Tagset
1225   lfsmake2 HTML-Parser
1226   lfsmake2 HTML-Template
1227   lfsmake2 Compress-Zlib
1228   lfsmake2 Digest
1229   lfsmake2 Digest-SHA1
1230   lfsmake2 Digest-HMAC
1231   lfsmake2 libwww-perl
1232   lfsmake2 Net-DNS
1233   lfsmake2 Net-IPv4Addr
1234   lfsmake2 Net_SSLeay
1235   lfsmake2 IO-Stringy
1236   lfsmake2 IO-Socket-SSL
1237   lfsmake2 Unix-Syslog
1238   lfsmake2 Mail-Tools
1239   lfsmake2 MIME-Tools
1240   lfsmake2 Net-Server
1241   lfsmake2 Convert-TNEF
1242   lfsmake2 Convert-UUlib
1243   lfsmake2 Archive-Tar
1244   lfsmake2 Archive-Zip
1245   lfsmake2 Text-Tabs+Wrap
1246   lfsmake2 Locale-Country
1247   lfsmake2 XML-Parser
1248   lfsmake2 Crypt-PasswdMD5
1249   lfsmake2 Net-Telnet
1250   lfsmake2 python-setuptools
1251   lfsmake2 python-clientform
1252   lfsmake2 python-mechanize
1253   lfsmake2 python-feedparser
1254   lfsmake2 python-rssdler
1255   lfsmake2 python-inotify
1256   lfsmake2 python-docutils
1257   lfsmake2 python-daemon
1258   lfsmake2 python-ipaddress
1259   lfsmake2 glib
1260   lfsmake2 GeoIP
1261   lfsmake2 ntp
1262   lfsmake2 openssh
1263   lfsmake2 fontconfig
1264   lfsmake2 dejavu-fonts-ttf
1265   lfsmake2 ubuntu-font-family
1266   lfsmake2 freefont
1267   lfsmake2 pixman
1268   lfsmake2 cairo
1269   lfsmake2 pango
1270   lfsmake2 rrdtool
1271   lfsmake2 setserial
1272   lfsmake2 setup
1273   lfsmake2 libdnet
1274   lfsmake2 daq
1275   lfsmake2 snort
1276   lfsmake2 oinkmaster
1277   lfsmake2 squid
1278   lfsmake2 squidguard
1279   lfsmake2 calamaris
1280   lfsmake2 tcpdump
1281   lfsmake2 traceroute
1282   lfsmake2 vlan
1283   lfsmake2 wireless
1284   lfsmake2 pakfire
1285   lfsmake2 spandsp
1286   lfsmake2 lz4
1287   lfsmake2 lzo
1288   lfsmake2 openvpn
1289   lfsmake2 mpage
1290   lfsmake2 dbus
1291   lfsmake2 intltool
1292   lfsmake2 libdaemon
1293   lfsmake2 cups
1294   lfsmake2 lcms2
1295   lfsmake2 ghostscript
1296   lfsmake2 qpdf
1297   lfsmake2 poppler
1298   lfsmake2 cups-filters
1299   lfsmake2 epson-inkjet-printer-escpr
1300   lfsmake2 foomatic
1301   lfsmake2 hplip
1302   lfsmake2 cifs-utils
1303   lfsmake2 krb5
1304   lfsmake2 samba
1305   lfsmake2 sudo
1306   lfsmake2 mc
1307   lfsmake2 wget
1308   lfsmake2 bridge-utils
1309 #  lfsmake2 screen
1310   lfsmake2 smartmontools
1311   lfsmake2 htop
1312   lfsmake2 chkconfig
1313   lfsmake2 postfix
1314   lfsmake2 fetchmail
1315   lfsmake2 cyrus-imapd
1316   lfsmake2 clamav
1317   lfsmake2 spamassassin
1318   lfsmake2 amavisd
1319   lfsmake2 dma
1320   lfsmake2 alsa
1321   lfsmake2 mpfire
1322   lfsmake2 guardian
1323   lfsmake2 libid3tag
1324   lfsmake2 libmad
1325   lfsmake2 libogg
1326   lfsmake2 libvorbis
1327   lfsmake2 libdvbpsi
1328   lfsmake2 flac
1329   lfsmake2 lame
1330   lfsmake2 sox
1331   lfsmake2 libshout
1332   lfsmake2 xvid
1333   lfsmake2 libmpeg2
1334   lfsmake2 libarchive
1335   lfsmake2 cmake
1336   lfsmake2 gnump3d
1337   lfsmake2 rsync
1338   lfsmake2 libtirpc
1339   lfsmake2 rpcbind
1340   lfsmake2 nfs
1341   lfsmake2 gnu-netcat
1342   lfsmake2 ncat
1343   lfsmake2 nmap
1344   lfsmake2 etherwake
1345   lfsmake2 bwm-ng
1346   lfsmake2 sysstat
1347   lfsmake2 strongswan
1348   lfsmake2 rng-tools
1349   lfsmake2 lsof
1350   lfsmake2 br2684ctl
1351   lfsmake2 pcmciautils
1352   lfsmake2 lm_sensors
1353   lfsmake2 liboping
1354   lfsmake2 collectd
1355   lfsmake2 elinks
1356   lfsmake2 igmpproxy
1357   lfsmake2 fbset
1358   lfsmake2 opus
1359   lfsmake2 python-six
1360   lfsmake2 python-pyparsing
1361   lfsmake2 spice-protocol
1362   lfsmake2 spice
1363   lfsmake2 sdl
1364   lfsmake2 libusbredir
1365   lfsmake2 qemu
1366   lfsmake2 sane
1367   lfsmake2 netpbm
1368   lfsmake2 netsnmpd
1369   lfsmake2 nagios_nrpe
1370   lfsmake2 nagios-plugins
1371   lfsmake2 icinga
1372   lfsmake2 ebtables
1373   lfsmake2 directfb
1374   lfsmake2 faad2
1375   lfsmake2 ffmpeg
1376   lfsmake2 vdr
1377   lfsmake2 vdr_streamdev
1378   lfsmake2 vdr_epgsearch
1379   lfsmake2 vdr_dvbapi
1380   lfsmake2 vdr_eepg
1381   lfsmake2 w_scan
1382   lfsmake2 icecast
1383   lfsmake2 icegenerator
1384   lfsmake2 mpd
1385   lfsmake2 libmpdclient
1386   lfsmake2 mpc
1387   lfsmake2 perl-Net-SMTP-SSL
1388   lfsmake2 perl-MIME-Base64
1389   lfsmake2 perl-Authen-SASL
1390   lfsmake2 perl-MIME-Lite
1391   lfsmake2 perl-Email-Date-Format
1392   lfsmake2 git
1393   lfsmake2 squidclamav
1394   lfsmake2 vnstat
1395   lfsmake2 iw
1396   lfsmake2 wpa_supplicant
1397   lfsmake2 hostapd
1398   lfsmake2 pycurl
1399   lfsmake2 urlgrabber
1400   lfsmake2 syslinux
1401   lfsmake2 tftpd
1402   lfsmake2 cpufrequtils
1403   lfsmake2 bluetooth
1404   lfsmake2 gutenprint
1405   lfsmake2 apcupsd
1406   lfsmake2 iperf
1407   lfsmake2 iperf3
1408   lfsmake2 7zip
1409   lfsmake2 lynis
1410   lfsmake2 streamripper
1411   lfsmake2 sshfs
1412   lfsmake2 taglib
1413   lfsmake2 sslh
1414   lfsmake2 perl-gettext
1415   lfsmake2 perl-Sort-Naturally
1416   lfsmake2 vdradmin
1417   lfsmake2 miau
1418   lfsmake2 perl-DBI
1419   lfsmake2 perl-DBD-SQLite
1420   lfsmake2 perl-File-ReadBackwards
1421   lfsmake2 openvmtools
1422   lfsmake2 motion
1423   lfsmake2 joe
1424   lfsmake2 monit
1425   lfsmake2 nut
1426   lfsmake2 watchdog
1427   lfsmake2 libpri
1428   lfsmake2 libsrtp
1429   lfsmake2 jansson
1430   lfsmake2 asterisk
1431   lfsmake2 usb_modeswitch
1432   lfsmake2 usb_modeswitch_data
1433   lfsmake2 zerofree
1434   lfsmake2 minicom
1435   lfsmake2 ddrescue
1436   lfsmake2 miniupnpd
1437   lfsmake2 client175
1438   lfsmake2 powertop
1439   lfsmake2 parted
1440   lfsmake2 swig
1441   lfsmake2 u-boot
1442   lfsmake2 u-boot-kirkwood
1443   lfsmake2 python-typing
1444   lfsmake2 python-m2crypto
1445   lfsmake2 wireless-regdb
1446   lfsmake2 crda
1447   lfsmake2 libsolv
1448   lfsmake2 python-distutils-extra
1449   lfsmake2 python-lzma
1450   lfsmake2 python-progressbar
1451   lfsmake2 python-xattr
1452   lfsmake2 ddns
1453   lfsmake2 transmission
1454   lfsmake2 dpfhack
1455   lfsmake2 lcd4linux
1456   lfsmake2 mtr
1457   lfsmake2 minidlna
1458   lfsmake2 acpid
1459   lfsmake2 fping
1460   lfsmake2 telnet
1461   lfsmake2 xinetd
1462   lfsmake2 gpgme
1463   lfsmake2 pygpgme
1464   lfsmake2 pakfire3
1465   lfsmake2 stress
1466   lfsmake2 libstatgrab
1467   lfsmake2 sarg
1468   lfsmake2 check_mk_agent
1469   lfsmake2 nginx
1470   lfsmake2 sendEmail
1471   lfsmake2 sysbench
1472   lfsmake2 strace
1473   lfsmake2 ltrace
1474   lfsmake2 ipfire-netboot
1475   lfsmake2 lcdproc
1476   lfsmake2 bitstream
1477   lfsmake2 multicat
1478   lfsmake2 keepalived
1479   lfsmake2 ipvsadm
1480   lfsmake2 perl-Carp-Clan
1481   lfsmake2 perl-Date-Calc
1482   lfsmake2 perl-Date-Manip
1483   lfsmake2 perl-File-Tail
1484   lfsmake2 perl-TimeDate
1485   lfsmake2 swatch
1486   lfsmake2 tor
1487   lfsmake2 arm
1488   lfsmake2 wavemon
1489   lfsmake2 iptraf-ng
1490   lfsmake2 iotop
1491   lfsmake2 stunnel
1492   lfsmake2 bacula
1493   lfsmake2 batctl
1494   lfsmake2 perl-Font-TTF
1495   lfsmake2 perl-IO-String
1496   lfsmake2 perl-PDF-API2
1497   lfsmake2 squid-accounting
1498   lfsmake2 pigz
1499   lfsmake2 tmux
1500   lfsmake2 perl-Text-CSV_XS
1501   lfsmake2 haproxy
1502   lfsmake2 ipset
1503   lfsmake2 lua
1504   lfsmake2 dnsdist
1505   lfsmake2 bird
1506   lfsmake2 dmidecode
1507   lfsmake2 mcelog
1508   lfsmake2 rtpproxy
1509   lfsmake2 util-macros
1510   lfsmake2 libpciaccess
1511   lfsmake2 libyajl
1512   lfsmake2 libvirt
1513   lfsmake2 freeradius
1514   lfsmake2 perl-common-sense
1515   lfsmake2 perl-inotify2
1516   lfsmake2 perl-Net-IP
1517   lfsmake2 wio
1518   lfsmake2 iftop
1519   lfsmake2 mdns-repeater
1520   lfsmake2 i2c-tools
1521 }
1522
1523 buildinstaller() {
1524   # Run installer scripts one by one
1525   LOGFILE="$BASEDIR/log/_build.installer.log"
1526   export LOGFILE
1527   lfsmake2 memtest
1528   lfsmake2 installer
1529   # use toolchain bash for chroot to strip
1530   EXTRA_PATH=${TOOLS_DIR}/bin/ lfsmake2 strip
1531 }
1532
1533 buildpackages() {
1534   LOGFILE="$BASEDIR/log/_build.packages.log"
1535   export LOGFILE
1536   echo "... see detailed log in _build.*.log files" >> $LOGFILE
1537
1538   
1539   # Generating list of packages used
1540   print_line "Generating packages list from logs"
1541   rm -f $BASEDIR/doc/packages-list
1542   for i in `ls -1tr $BASEDIR/log/[^_]*`; do
1543         if [ "$i" != "$BASEDIR/log/FILES" -a -n $i ]; then
1544                 echo "* `basename $i`" >>$BASEDIR/doc/packages-list
1545         fi
1546   done
1547   echo "== List of softwares used to build $NAME Version: $VERSION ==" > $BASEDIR/doc/packages-list.txt
1548   grep -v 'configroot$\|img$\|initrd$\|initscripts$\|installer$\|install$\|setup$\|pakfire$\|stage2$\|smp$\|tools$\|tools1$\|tools2$\|.tgz$\|-config$\|_missing_rootfile$\|install1$\|install2$\|pass1$\|pass2$\|pass3$' \
1549         $BASEDIR/doc/packages-list | sort >> $BASEDIR/doc/packages-list.txt
1550   rm -f $BASEDIR/doc/packages-list
1551   # packages-list.txt is ready to be displayed for wiki page
1552   print_status DONE
1553   
1554   # Update changelog
1555   cd $BASEDIR
1556   [ -z $GIT_TAG ]  || LAST_TAG=$GIT_TAG
1557   [ -z $LAST_TAG ] || EXT="$LAST_TAG..HEAD"
1558   git log -n 500 --no-merges --pretty=medium --shortstat $EXT > $BASEDIR/doc/ChangeLog
1559
1560   # Create images for install
1561   lfsmake2 cdrom
1562
1563   # Check if there is a loop device for building in virtual environments
1564   modprobe loop 2>/dev/null
1565   if [ $BUILD_IMAGES == 1 ] && ([ -e /dev/loop/0 ] || [ -e /dev/loop0 ] || [ -e "/dev/loop-control" ]); then
1566         lfsmake2 flash-images
1567   fi
1568
1569   mv $LFS/install/images/{*.iso,*.tgz,*.img.xz,*.bz2} $BASEDIR >> $LOGFILE 2>&1
1570
1571   ipfirepackages
1572
1573   lfsmake2 xen-image
1574   mv $LFS/install/images/*.bz2 $BASEDIR >> $LOGFILE 2>&1
1575
1576   cd $BASEDIR
1577
1578   # remove not useable iso on armv5tel (needed to build flash images)
1579   [ "${BUILD_ARCH}" = "armv5tel" ] && rm -rf *.iso
1580
1581   for i in `ls *.bz2 *.img.gz *.iso`; do
1582         md5sum $i > $i.md5
1583   done
1584   cd $PWD
1585
1586   # Cleanup
1587   stdumount
1588   rm -rf $BASEDIR/build/tmp/*
1589
1590   cd $PWD
1591 }
1592
1593 ipfirepackages() {
1594         lfsmake2 core-updates
1595
1596         local i
1597         for i in $(find $BASEDIR/config/rootfiles/packages{/${BUILD_ARCH},} -maxdepth 1 -type f); do
1598                 i=$(basename ${i})
1599                 if [ -e $BASEDIR/lfs/$i ]; then
1600                         ipfiredist $i
1601                 else
1602                         echo -n $i
1603                         print_status SKIP
1604                 fi
1605         done
1606   test -d $BASEDIR/packages || mkdir $BASEDIR/packages
1607   mv -f $LFS/install/packages/* $BASEDIR/packages >> $LOGFILE 2>&1
1608   rm -rf  $BASEDIR/build/install/packages/*
1609 }
1610
1611 while [ $# -gt 0 ]; do
1612         case "${1}" in
1613                 --target=*)
1614                         configure_build "${1#--target=}"
1615                         ;;
1616                 -*)
1617                         exiterror "Unknown configuration option: ${1}"
1618                         ;;
1619                 *)
1620                         # Found a command, so exit options parsing.
1621                         break
1622                         ;;
1623         esac
1624         shift
1625 done
1626
1627 # See what we're supposed to do
1628 case "$1" in 
1629 build)
1630         START_TIME=$(now)
1631
1632         # Clear screen
1633         ${INTERACTIVE} && clear
1634
1635         PACKAGE=`ls -v -r $BASEDIR/cache/toolchains/$SNAME-$VERSION-toolchain-$TOOLCHAINVER-${BUILD_ARCH}.tar.xz 2> /dev/null | head -n 1`
1636         #only restore on a clean disk
1637         if [ ! -e "${BASEDIR}/build${TOOLS_DIR}/.toolchain-successful" ]; then
1638                 if [ ! -n "$PACKAGE" ]; then
1639                         print_build_stage "Full toolchain compilation"
1640                         prepareenv
1641                         buildtoolchain
1642                 else
1643                         PACKAGENAME=${PACKAGE%.tar.xz}
1644                         print_build_stage "Packaged toolchain compilation"
1645                         if [ `md5sum $PACKAGE | awk '{print $1}'` == `cat $PACKAGENAME.md5 | awk '{print $1}'` ]; then
1646                                 tar axf $PACKAGE
1647                                 prepareenv
1648                         else
1649                                 exiterror "$PACKAGENAME md5 did not match, check downloaded package"
1650                         fi
1651                 fi
1652         else
1653                 prepareenv
1654         fi
1655
1656         print_build_stage "Building LFS"
1657         buildbase
1658
1659         print_build_stage "Building IPFire"
1660         buildipfire
1661
1662         print_build_stage "Building installer"
1663         buildinstaller
1664
1665         print_build_stage "Building packages"
1666         buildpackages
1667         
1668         print_build_stage "Checking Logfiles for new Files"
1669
1670         cd $BASEDIR
1671         tools/checknewlog.pl
1672         tools/checkrootfiles
1673         cd $PWD
1674
1675         print_build_summary $(( $(now) - ${START_TIME} ))
1676         ;;
1677 shell)
1678         # enter a shell inside LFS chroot
1679         # may be used to changed kernel settings
1680         prepareenv
1681         entershell
1682         ;;
1683 clean)
1684         print_line "Cleaning build directory..."
1685
1686         for i in `mount | grep $BASEDIR | sed 's/^.*loop=\(.*\))/\1/'`; do
1687                 $LOSETUP -d $i 2>/dev/null
1688         done
1689         for i in `mount | grep $BASEDIR | cut -d " " -f 1`; do
1690                 umount $i
1691         done
1692         stdumount
1693         for i in `seq 0 7`; do
1694                 if ( losetup /dev/loop${i} 2>/dev/null | grep -q "/install/images" ); then
1695                 umount /dev/loop${i}     2>/dev/null;
1696                 losetup -d /dev/loop${i} 2>/dev/null;
1697                 fi;
1698         done
1699         rm -rf $BASEDIR/build
1700         rm -rf $BASEDIR/cdrom
1701         rm -rf $BASEDIR/packages
1702         rm -rf $BASEDIR/log
1703         if [ -h "${TOOLS_DIR}" ]; then
1704                 rm -f "${TOOLS_DIR}"
1705         fi
1706         rm -f $BASEDIR/ipfire-*
1707         print_status DONE
1708         ;;
1709 downloadsrc)
1710         if [ ! -d $BASEDIR/cache ]; then
1711                 mkdir $BASEDIR/cache
1712         fi
1713         mkdir -p $BASEDIR/log
1714         echo -e "${BOLD}Preload all source files${NORMAL}" | tee -a $LOGFILE
1715         FINISHED=0
1716         cd $BASEDIR/lfs
1717         for c in `seq $MAX_RETRIES`; do
1718                 if (( FINISHED==1 )); then 
1719                         break
1720                 fi
1721                 FINISHED=1
1722                 cd $BASEDIR/lfs
1723                 for i in *; do
1724                         if [ -f "$i" -a "$i" != "Config" ]; then
1725                                 lfsmakecommoncheck ${i} || continue
1726
1727                                 make -s -f $i LFS_BASEDIR=$BASEDIR BUILD_ARCH="${BUILD_ARCH}" \
1728                                         MESSAGE="$i\t ($c/$MAX_RETRIES)" download >> $LOGFILE 2>&1
1729                                 if [ $? -ne 0 ]; then
1730                                         print_status FAIL
1731                                         FINISHED=0
1732                                 else
1733                                         if [ $c -eq 1 ]; then
1734                                         print_status DONE
1735                                         fi
1736                                 fi
1737                         fi
1738                 done
1739         done
1740         echo -e "${BOLD}***Verifying md5sums${NORMAL}"
1741         ERROR=0
1742         for i in *; do
1743                 if [ -f "$i" -a "$i" != "Config" ]; then
1744                         lfsmakecommoncheck ${i} > /dev/null || continue
1745                         make -s -f $i LFS_BASEDIR=$BASEDIR BUILD_ARCH="${BUILD_ARCH}" \
1746                                 MESSAGE="$i\t " md5 >> $LOGFILE 2>&1
1747                         if [ $? -ne 0 ]; then
1748                                 echo -ne "MD5 difference in lfs/$i"
1749                                 print_status FAIL
1750                                 ERROR=1
1751                         fi
1752                 fi
1753         done
1754         if [ $ERROR -eq 0 ]; then
1755                 echo -ne "${BOLD}all files md5sum match${NORMAL}"
1756                 print_status DONE
1757         else
1758                 echo -ne "${BOLD}not all files were correctly download${NORMAL}"
1759                 print_status FAIL
1760         fi
1761         cd - >/dev/null 2>&1
1762         ;;
1763 toolchain)
1764         # Clear screen
1765         ${INTERACTIVE} && clear
1766
1767         prepareenv
1768         print_build_stage "Toolchain compilation (${BUILD_ARCH})"
1769         buildtoolchain
1770         echo "`date -u '+%b %e %T'`: Create toolchain image for ${BUILD_ARCH}" | tee -a $LOGFILE
1771         test -d $BASEDIR/cache/toolchains || mkdir -p $BASEDIR/cache/toolchains
1772         cd $BASEDIR && XZ_OPT="$(XZ_OPT)" tar -Jc --exclude='log/_build.*.log' -f cache/toolchains/$SNAME-$VERSION-toolchain-$TOOLCHAINVER-${BUILD_ARCH}.tar.xz --xz \
1773                 build/${TOOLS_DIR} build/bin/sh log >> $LOGFILE
1774         md5sum cache/toolchains/$SNAME-$VERSION-toolchain-$TOOLCHAINVER-${BUILD_ARCH}.tar.xz \
1775                 > cache/toolchains/$SNAME-$VERSION-toolchain-$TOOLCHAINVER-${BUILD_ARCH}.md5
1776         stdumount
1777         ;;
1778 gettoolchain)
1779         # arbitrary name to be updated in case of new toolchain package upload
1780         PACKAGE=$SNAME-$VERSION-toolchain-$TOOLCHAINVER-${BUILD_ARCH}
1781         if [ ! -f $BASEDIR/cache/toolchains/$PACKAGE.tar.xz ]; then
1782                 URL_TOOLCHAIN=`grep URL_TOOLCHAIN lfs/Config | awk '{ print $3 }'`
1783                 test -d $BASEDIR/cache/toolchains || mkdir -p $BASEDIR/cache/toolchains
1784                 echo "`date -u '+%b %e %T'`: Load toolchain image for ${BUILD_ARCH}" | tee -a $LOGFILE
1785                 cd $BASEDIR/cache/toolchains
1786                 wget -U "IPFireSourceGrabber/2.x" $URL_TOOLCHAIN/$PACKAGE.tar.xz $URL_TOOLCHAIN/$PACKAGE.md5 >& /dev/null
1787                 if [ $? -ne 0 ]; then
1788                         echo "`date -u '+%b %e %T'`: error downloading $PACKAGE toolchain for ${BUILD_ARCH} machine" | tee -a $LOGFILE
1789                 else
1790                         if [ "`md5sum $PACKAGE.tar.xz | awk '{print $1}'`" = "`cat $PACKAGE.md5 | awk '{print $1}'`" ]; then
1791                                 echo "`date -u '+%b %e %T'`: toolchain md5 ok" | tee -a $LOGFILE
1792                         else
1793                                 exiterror "$PACKAGE.md5 did not match, check downloaded package"
1794                         fi
1795                 fi
1796         else
1797                 echo "Toolchain is already downloaded. Exiting..."
1798         fi
1799         ;;
1800 uploadsrc)
1801         PWD=`pwd`
1802         if [ -z $IPFIRE_USER ]; then
1803                 echo -n "You have to setup IPFIRE_USER first. See .config for details."
1804                 print_status FAIL
1805                 exit 1
1806         fi
1807
1808         URL_SOURCE=$(grep URL_SOURCE lfs/Config | awk '{ print $3 }')
1809         REMOTE_FILES=$(echo "ls -1" | sftp -C ${IPFIRE_USER}@${URL_SOURCE})
1810
1811         for file in ${BASEDIR}/cache/*; do
1812                 [ -d "${file}" ] && continue
1813                 grep -q "$(basename ${file})" <<<$REMOTE_FILES && continue
1814                 NEW_FILES="$NEW_FILES $file"
1815         done
1816         [ -n "$NEW_FILES" ] && scp -2 $NEW_FILES ${IPFIRE_USER}@${URL_SOURCE}
1817         cd $BASEDIR
1818         cd $PWD
1819         exit 0
1820         ;;
1821 lang)
1822         echo -ne "Checking the translations for missing or obsolete strings..."
1823         chmod 755 $BASEDIR/tools/{check_strings.pl,sort_strings.pl,check_langs.sh}
1824         $BASEDIR/tools/sort_strings.pl en
1825         $BASEDIR/tools/sort_strings.pl de
1826         $BASEDIR/tools/sort_strings.pl fr
1827         $BASEDIR/tools/sort_strings.pl es
1828         $BASEDIR/tools/sort_strings.pl pl
1829         $BASEDIR/tools/sort_strings.pl ru
1830         $BASEDIR/tools/sort_strings.pl nl
1831         $BASEDIR/tools/sort_strings.pl tr
1832         $BASEDIR/tools/sort_strings.pl it
1833         $BASEDIR/tools/check_strings.pl en > $BASEDIR/doc/language_issues.en
1834         $BASEDIR/tools/check_strings.pl de > $BASEDIR/doc/language_issues.de
1835         $BASEDIR/tools/check_strings.pl fr > $BASEDIR/doc/language_issues.fr
1836         $BASEDIR/tools/check_strings.pl es > $BASEDIR/doc/language_issues.es
1837         $BASEDIR/tools/check_strings.pl es > $BASEDIR/doc/language_issues.pl
1838         $BASEDIR/tools/check_strings.pl ru > $BASEDIR/doc/language_issues.ru
1839         $BASEDIR/tools/check_strings.pl nl > $BASEDIR/doc/language_issues.nl
1840         $BASEDIR/tools/check_strings.pl tr > $BASEDIR/doc/language_issues.tr
1841         $BASEDIR/tools/check_strings.pl it > $BASEDIR/doc/language_issues.it
1842         $BASEDIR/tools/check_langs.sh > $BASEDIR/doc/language_missings
1843         print_status DONE
1844
1845         echo -ne "Updating language lists..."
1846         update_language_list ${BASEDIR}/src/installer/po
1847         update_language_list ${BASEDIR}/src/setup/po
1848         print_status DONE
1849         ;;
1850 *)
1851         echo "Usage: $0 {build|changelog|clean|gettoolchain|downloadsrc|shell|sync|toolchain}"
1852         cat doc/make.sh-usage
1853         ;;
1854 esac