]> git.ipfire.org Git - ipfire-3.x.git/blob - tools/make-include
Merge branch 'ssp'
[ipfire-3.x.git] / tools / make-include
1 #!/bin/bash
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2008 Michael Tremer & Christian Schmidt #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21 ###############################################################################
22 #
23 # System variables
24 #
25 ###############################################################################
26
27 CONFIG_ROOT=/etc/$SNAME # Configuration rootdir
28 NICE=10 # Nice level
29 TARGET=i686 # Default target
30 KVER=`grep --max-count=1 VER lfs/linux | awk '{ print $3 }' | tr -d '\n'; grep --max-count=1 FULLVER lfs/linux | awk '{ print $3 }' | cut -c 7-| tail -1`
31 MACHINE_REAL=`uname -m`
32 GIT_TAG=$(git tag | tail -1)
33
34 # Security options
35 SSP=1
36 PIE=1
37 PAX=1
38
39 # Parallelism flag
40 PARALLELISMFLAGS=-j$(( $(grep processor < /proc/cpuinfo | wc -l) * 2 + 1 ))
41 DISTCC_HOSTS=localhost
42
43 PWD=`pwd`
44 BASENAME=`basename $0`
45
46 # Debian specific settings
47 if [ ! -e /etc/debian_version ]; then
48 FULLPATH=`which $0`
49 else
50 if [ -x /usr/bin/realpath ]; then
51 FULLPATH=`/usr/bin/realpath $0`
52 else
53 echo "ERROR: Need to do apt-get install realpath"
54 exit 1
55 fi
56 fi
57
58 BASEDIR=`echo $FULLPATH | sed "s/\/$BASENAME//g"`
59 export BASEDIR
60
61 ###############################################################################
62 #
63 # Read the local configuration to override the environment variables
64 #
65 ###############################################################################
66
67 if [ -e .config ]; then
68 . $BASEDIR/.config
69 fi
70
71
72 ###############################################################################
73 #
74 # Variables that are not modifyable by .config
75 #
76 ###############################################################################
77
78 if [ 'i686' = $MACHINE_REAL \
79 -o 'i586' = $MACHINE_REAL \
80 -o 'i486' = $MACHINE_REAL \
81 -o 'x86_64' = $MACHINE_REAL ]; then
82 IFS_HOST="$(echo $MACHTYPE | sed "s/$(echo $MACHTYPE | cut -d- -f2)/cross/")"
83 else
84 beautify message FAIL
85 echo "Can't determine your architecture - $MACHINE_REAL"
86 exit 1
87 fi
88
89 if [ 'i686' = $TARGET -o 'i586' = $TARGET \
90 -o 'i486' = $TARGET ]; then
91 MACHINE=${TARGET}
92 MACHINE_REAL=${MACHINE_REAL}
93 LINKER=/lib/ld-linux.so.2
94 IFS_TARGET="${MACHINE}-pc-linux-gnu"
95 CFLAGS="-march=${MACHINE} -O2 -pipe -fomit-frame-pointer"
96 CXXFLAGS="${CFLAGS}"
97 UCLIBC_TARGET=${MACHINE}-pc-linux-uclibc
98 UCLIBC_CFLAGS="-march=${MACHINE} -Os -pipe"
99 elif [ 'via-c7' = $TARGET ]; then
100 MACHINE=i686
101 MACHINE_REAL=${MACHINE_REAL}
102 LINKER=/lib/ld-linux.so.2
103 IFS_TARGET="${MACHINE}-pc-linux-gnu"
104 CFLAGS="-march=${MACHINE} -mmmx -msse -msse2 -msse3 -O2 -pipe"
105 CXXFLAGS="${CFLAGS}"
106 UCLIBC_TARGET=${MACHINE}-pc-linux-uclibc
107 UCLIBC_CFLAGS="-march=i386 -Os -pipe"
108 elif [ 'via-c3' = $TARGET ]; then
109 MACHINE=i586
110 MACHINE_REAL=${MACHINE_REAL}
111 LINKER=/lib/ld-linux.so.2
112 IFS_TARGET="${MACHINE}-pc-linux-gnu"
113 CFLAGS="-march=c3 -m3dnow -O2 -pipe -fomit-frame-pointer"
114 CXXFLAGS="${CFLAGS}"
115 UCLIBC_TARGET=${MACHINE}-pc-linux-uclibc
116 UCLIBC_CFLAGS="-march=${MACHINE} -Os -pipe"
117 else
118 beautify message FAIL
119 echo "Not a valid target arch (i686|i586|i486|i386|via-c7|via-c3) - $TARGET"
120 exit 1
121 fi
122
123 mkdir $BASEDIR/log_${MACHINE}/ 2>/dev/null
124
125 # Set up what used to be /tools
126 TOOLS_DIR=/tools_${MACHINE}
127 CTOOLS_DIR=/cross-tools_${MACHINE}
128
129 # Set up /installer
130 INSTALLER_DIR=/installer
131
132 # A place to build the iso
133 CDROM_DIR=/cdrom
134
135 # A place to keep the images
136 IMAGES_DIR=/images
137
138 # include machine in TOOLCHAINNAME
139 TOOLCHAINNAME=$SNAME-$TOOLCHAINVERSION-toolchain-t${TARGET}-m${MACHINE}
140
141 # The place where all uclibc files are stored in
142 UCLIBC_DIR=/usr/${UCLIBC_TARGET}
143 UCLIBC_CC_CORE_STATIC_DIR=${UCLIBC_DIR}/gcc-core-static
144
145 #UCLIBC_SYSROOT_DIR=${UCLIBC_DIR}/${UCLIBC_TARGET}/sys-root
146 UCLIBC_SYSROOT_DIR=${INSTALLER_DIR}
147
148 ###############################################################################
149 #
150 # Beautifying variables & presentation & input output interface
151 #
152 ###############################################################################
153
154 ## Screen Dimentions
155 # Find current screen size
156 if [ -z "${COLUMNS}" ]; then
157 COLUMNS=$(stty size)
158 COLUMNS=${COLUMNS##* }
159 fi
160
161 # When using remote connections, such as a serial port, stty size returns 0
162 if [ "${COLUMNS}" = "0" ]; then
163 COLUMNS=80
164 fi
165
166 ## Measurements for positioning result messages
167 RESULT_WIDTH=4
168 TIME_WIDTH=8
169 OPT_WIDTH=7
170 VER_WIDTH=10
171 RESULT_COL=$((${COLUMNS} - $RESULT_WIDTH - 4))
172 TIME_COL=$((${RESULT_COL} - $TIME_WIDTH - 5))
173 VER_COL=$((${TIME_COL} - $VER_WIDTH - 5))
174 OPT_COL=$((${VER_COL} - $OPT_WIDTH - 5))
175
176 ## Set Cursur Position Commands, used via echo -e
177 SET_RESULT_COL="\\033[${RESULT_COL}G"
178 SET_TIME_COL="\\033[${TIME_COL}G"
179 SET_VER_COL="\\033[${VER_COL}G"
180 SET_OPT_COL="\\033[${OPT_COL}G"
181
182 # Define color for messages
183 BOLD="\\033[1;39m"
184 DONE="\\033[1;32m"
185 SKIP="\\033[1;34m"
186 WARN="\\033[1;35m"
187 FAIL="\\033[1;31m"
188 NORMAL="\\033[0;39m"
189
190
191 ################################################################################
192 # #
193 # Necessary shell functions #
194 # #
195 ################################################################################
196
197 evaluate() {
198 if [ "$?" -eq "0" ]; then
199 beautify message DONE
200 else
201 EXITCODE=$1
202 shift 1
203 beautify message FAIL
204 $*
205 if [ $EXITCODE -ne "0" ]; then
206 exit $EXITCODE
207 fi
208 fi
209 }
210
211 position_cursor()
212 {
213 # ARG1=starting position on screen
214 # ARG2=string to be printed
215 # ARG3=offset, negative for left movement, positive for right movement, relative to ARG1
216 # For example if your starting position is column 50 and you want to print Hello three columns to the right
217 # of your starting position, your call will look like this:
218 # position_cursor 50 "Hello" 3 (you'll get the string Hello at position 53 (= 50 + 3)
219 # If on the other hand you want your string "Hello" to end three columns to the left of position 50,
220 # your call will look like this:
221 # position_cursor 50 "Hello" -3 (you'll get the string Hello at position 42 (= 50 - 5 -3)
222 # If you want to start printing at the exact starting location, use offset 0
223
224 START=$1
225 STRING=$2
226 OFFSET=$3
227
228 STRING_LENGTH=${#STRING}
229
230 if [ ${OFFSET} -lt 0 ]; then
231 COL=$((${START} + ${OFFSET} - ${STRING_LENGTH}))
232 else
233 COL=$((${START} + ${OFFSET}))
234 fi
235
236 SET_COL="\\033[${COL}G"
237
238 echo $SET_COL
239 } # End of position_cursor()
240
241
242 beautify()
243 {
244 # Commands: build_stage, make_pkg, message, result
245 case "$1" in
246 message)
247 MESSAGE="$3"
248 case "$2" in
249 DONE)
250 echo -ne "${BOLD}${MESSAGE}${NORMAL}${SET_RESULT_COL}[${DONE} DONE ${NORMAL}]\n"
251 ;;
252 WARN)
253 echo -ne "${WARN}${MESSAGE}${NORMAL}${SET_RESULT_COL}[${WARN} WARN ${NORMAL}]\n"
254 ;;
255 FAIL)
256 echo -ne "${BOLD}${MESSAGE}${NORMAL}${SET_RESULT_COL}[${FAIL} FAIL ${NORMAL}]\n"
257 ;;
258 SKIP)
259 echo -ne "${BOLD}${MESSAGE}${NORMAL}${SET_RESULT_COL}[${SKIP} SKIP ${NORMAL}]\n"
260 ;;
261 ON)
262 echo -ne "${BOLD}${MESSAGE}${NORMAL}${SET_RESULT_COL}[${DONE} ON ${NORMAL}]\n"
263 ;;
264 OFF)
265 echo -ne "${BOLD}${MESSAGE}${NORMAL}${SET_RESULT_COL}[${FAIL} OFF ${NORMAL}]\n"
266 ;;
267 esac
268 ;;
269 build_stage)
270 MESSAGE=$2
271 echo -ne "${BOLD}*** ${MESSAGE}${SET_OPT_COL} options${SET_VER_COL} version "
272 echo -ne "${SET_TIME_COL} time (sec)${SET_RESULT_COL} status${NORMAL}\n"
273 ;;
274 make_pkg)
275 echo "$2" | while read PKG_VER PROGRAM OPTIONS
276 do
277 SET_VER_COL_REAL=`position_cursor $TIME_COL $PKG_VER -3`
278
279 echo -ne "${PROGRAM}"
280 if ! [ "$OPTIONS" == "" ]; then
281 echo -ne "${SET_OPT_COL}[ ${BOLD}${OPTIONS}${NORMAL} ]"
282 fi
283
284 if [ "${PKG_VER}" == "${SNAME}" ] || [ "${PKG_VER}" == "LFS" ]; then
285 echo -ne "${SET_RESULT_COL}"
286 else
287 echo -ne "${SET_VER_COL}[ ${BOLD}${SET_VER_COL_REAL}${PKG_VER}"
288 echo -ne "${NORMAL} ]${SET_RESULT_COL}"
289 fi
290 done
291 ;;
292 result)
293 RESULT=$2
294
295 if [ ! $3 ]; then
296 PKG_TIME=0
297 else
298 PKG_TIME=$3
299 fi
300
301 SET_TIME_COL_REAL=`position_cursor $RESULT_COL $PKG_TIME -3`
302 case "$RESULT" in
303 DONE)
304 echo -ne "${SET_TIME_COL}[ ${BOLD}${SET_TIME_COL_REAL}$PKG_TIME${NORMAL} ]"
305 echo -ne "${SET_RESULT_COL}[${DONE} DONE ${NORMAL}]\n"
306 ;;
307 FAIL)
308 echo -ne "${SET_TIME_COL}[ ${BOLD}${SET_TIME_COL_REAL}$PKG_TIME${NORMAL} ]"
309 echo -ne "${SET_RESULT_COL}[${FAIL} FAIL ${NORMAL}]\n"
310 ;;
311 SKIP)
312 echo -ne "${SET_TIME_COL}[ ${BOLD}${SET_TIME_COL_REAL}$PKG_TIME${NORMAL} ]"
313 echo -ne "${SET_RESULT_COL}[${SKIP} SKIP ${NORMAL}]\n"
314 ;;
315 esac
316 ;;
317 esac
318 } # End of beautify()
319
320
321 get_pkg_ver()
322 {
323 PKG_VER=`grep ^VER $1 | awk '{print $3}'`
324
325 if [ -z $PKG_VER ]; then
326 PKG_VER=`grep "Exp " $1 | awk '{print $4}'`
327 fi
328
329 if [ ${#PKG_VER} -gt $VER_WIDTH ]; then
330 # If a package version number is greater than $VER_WIDTH, we keep the first 4 characters
331 # and replace enough characters to fit the resulting string on the screen. We'll replace
332 # the extra character with .. (two dots). That's why the "+ 2" in the formula below.
333 # Example: if we have a 21-long version number that we want to fit into a 10-long space,
334 # we have to remove 11 characters. But if we replace 11 characters with 2 characters, we'll
335 # end up with a 12-character long string. That's why we replace 12 characters with ..
336 REMOVE=`expr substr "$PKG_VER" 4 $[ ${#PKG_VER} - $VER_WIDTH + 2 ]`
337 PKG_VER=`echo ${PKG_VER/$REMOVE/..}`
338 fi
339
340 echo "$PKG_VER"
341 } # End of get_pkg_ver()
342
343 stdumount() {
344 sleep 0.3 # Wait one second for finish of processes
345 for fs in `mount | grep $BASEDIR/build_${MACHINE} | awk '{print $3}'`; do
346 umount $fs #2>/dev/null;
347 done
348 } # End of stdumount()
349
350 exiterror() {
351 stdumount
352
353 beautify message FAIL
354
355 echo -ne "${FAIL}ERROR${NORMAL}: ${BOLD}$*${NORMAL}\n"
356 [ -z "$LOGFILE" ] || \
357 echo " Check $LOGFILE for errors if applicable"
358 exit 1
359 } # End of exiterror()
360
361 ################################################################################
362 # This is the function that sets the environment of a chroot and enters it #
363 ################################################################################
364 entershell() {
365 PATH=${TOOLS_DIR}/usr/bin:/bin:/usr/bin:/sbin:/usr/sbin:${TOOLS_DIR}/bin:/usr/${MACHINE_REAL}-linux/bin
366
367 if [ ! -e $LFS/usr/src/lfs/ ]; then
368 exiterror "No such file or directory: $LFS/usr/src/lfs/"
369 fi
370
371 echo -ne "Entering ${BOLD}$MACHINE${NORMAL} LFS chroot, type exit to return to host environment\n"
372
373 chroot $LFS $TOOLS_DIR/bin/env -i \
374 HOME=/root \
375 TERM=$TERM \
376 PS1="${BOLD}[chroot-${TARGET}(${MACHINE})]${NORMAL} \u:\w\$ " \
377 PATH=$PATH \
378 CONFIG_ROOT=${CONFIG_ROOT} \
379 VERSION=${VERSION} \
380 NAME=${NAME} \
381 SNAME=${SNAME} \
382 SLOGAN="$SLOGAN" \
383 CCACHE_DIR=/usr/src/ccache \
384 CCACHE_PREFIX=${CCACHE_PREFIX} \
385 CCACHE_HASHDIR=${CCACHE_HASHDIR} \
386 DISTCC_DIR=/usr/src/distcc \
387 PARALLELISMFLAGS=$PARALLELISMFLAGS \
388 LINKER=$LINKER \
389 TOOLS_DIR=$TOOLS_DIR \
390 INSTALLER_DIR=$INSTALLER_DIR \
391 MACHINE="$MACHINE" \
392 MACHINE_REAL="$MACHINE_REAL" \
393 CFLAGS="$CFLAGS" \
394 CXXFLAGS="$CXXFLAGS" \
395 IFS_HOST="$IFS_HOST" \
396 IFS_TARGET="$IFS_TARGET" \
397 KVER=$KVER \
398 STAGE=$STAGE \
399 STAGE_ORDER=$STAGE_ORDER \
400 LOGFILE=`echo $LOGFILE | sed "s,$BASEDIR,/usr/src,g"` \
401 bash
402
403 if [ $? -ne 0 ]; then
404 exiterror "chroot error"
405 else
406 stdumount
407 fi
408 } # End of entershell()
409
410
411
412 ################################################################################
413 # Common checking before entering the chroot and compilling #
414 # Return:0 caller can continue #
415 # :1 skip (nothing to do) #
416 # or fail if no script file found #
417 ################################################################################
418 lfsmakecommoncheck()
419 {
420 # Script present?
421 if [ ! -f $BASEDIR/lfs/$1 ]; then
422 exiterror "No such file or directory: $BASEDIR/lfs/$1"
423 fi
424
425 local PKG_VER=`get_pkg_ver $BASEDIR/lfs/$1`
426 beautify make_pkg "$PKG_VER $*"
427
428 # Script slipped?
429 local i
430 for i in $SKIP_PACKAGE_LIST
431 do
432 if [ "$i" == "$1" ]; then
433 beautify result SKIP
434 return 1;
435 fi
436 done
437
438 echo -e "`date -u '+%b %e %T'`: Building $* " >> $LOGFILE
439
440 cd $BASEDIR/lfs && make -s -f $* MACHINE=$MACHINE LFS_BASEDIR=$BASEDIR MESSAGE="$1\t " download >> $LOGFILE 2>&1
441 if [ $? -ne 0 ]; then
442 exiterror "Download error in $1"
443 fi
444
445 return 0 # pass all!
446 } # End of lfsmakecommoncheck()
447
448 ################################################################################
449 # This is the function that builds every package in stage "toolchain" #
450 ################################################################################
451 toolchain_make() {
452 lfsmakecommoncheck $*
453 [ $? == 1 ] && return 0
454
455 local PKG_VER=`get_pkg_ver $BASEDIR/lfs/$1`
456
457 local EXTRA_MAKE=$EXTRA_MAKE
458
459 local PKG_TIME_START=`date +%s`
460 cd $BASEDIR/lfs && $EXTRA_MAKE make -f $* \
461 CONFIG_ROOT=$CONFIG_ROOT \
462 LINKER=$LINKER \
463 TOOLS_DIR=$TOOLS_DIR \
464 CTOOLS_DIR=$CTOOLS_DIR \
465 MACHINE="$MACHINE" \
466 MACHINE_REAL="$MACHINE_REAL" \
467 IFS_HOST="$IFS_HOST" \
468 IFS_TARGET="$IFS_TARGET" \
469 LFS_BASEDIR=$BASEDIR \
470 LFS=$LFS \
471 INSTALLER_DIR=$INSTALLER_DIR \
472 PARALLELISMFLAGS=$PARALLELISMFLAGS \
473 KVER=$KVER \
474 STAGE=$STAGE \
475 STAGE_ORDER=$STAGE_ORDER \
476 SSP=$SSP \
477 PIE=$PIE \
478 PAX=$PAX \
479 install >> $LOGFILE 2>&1
480
481 local COMPILE_SUCCESS=$?
482 local PKG_TIME_END=`date +%s`
483
484 if [ $COMPILE_SUCCESS -ne 0 ]; then
485 beautify result FAIL $[ $PKG_TIME_END - $PKG_TIME_START ] $1 $PKG_VER $STAGE_ORDER $STAGE
486 exiterror "Building $*";
487 else
488 beautify result DONE $[ $PKG_TIME_END - $PKG_TIME_START ] $1 $PKG_VER $STAGE_ORDER $STAGE
489 fi
490
491 return 0
492 } # End of toolchain_make()
493
494 ################################################################################
495 # This is the function that builds every package in stage "base" and "ipfire" #
496 ################################################################################
497 ipfire_make() {
498 lfsmakecommoncheck $*
499 [ $? == 1 ] && return 0
500
501 local PKG_VER=`get_pkg_ver $BASEDIR/lfs/$1`
502
503 local EXTRA_MAKE=$EXTRA_MAKE
504 # When cross-compiling, make sure the kernel is compiled for the target
505 [ "$MACHINE" != "$MACHINE_REAL" -a "$1" == "linux" ] && unset EXTRA_MAKE
506
507 # Also, make sure external kernel modules are compiled 64bit
508 if grep -qEi 'KERNEL_MOD = yes' $1 ; then
509 unset EXTRA_MAKE
510 fi
511
512 local MYCFLAGS MYCXXFLAGS
513 if [ "${STAGE}" = "installer" ]; then
514 MYCFLAGS=${UCLIBC_CFLAGS}
515 MYCXXFLAGS=${UCLIBC_CFLAGS}
516 else
517 MYCFLAGS=${CFLAGS}
518 MYCXXFLAGS=${CXXFLAGS}
519 fi
520
521 local PKG_TIME_START=`date +%s`
522 chroot $LFS $TOOLS_DIR/bin/env -i \
523 HOME=/root \
524 TERM=$TERM \
525 PS1='\u:\w\$ ' \
526 PATH=$PATH \
527 CONFIG_ROOT=${CONFIG_ROOT} \
528 VERSION=${VERSION} \
529 NAME=${NAME} \
530 SNAME=${SNAME} \
531 SLOGAN="$SLOGAN" \
532 CCACHE_DIR=/usr/src/ccache \
533 CCACHE_PREFIX=${CCACHE_PREFIX} \
534 CCACHE_HASHDIR=${CCACHE_HASHDIR} \
535 DISTCC_DIR=/usr/src/distcc \
536 PARALLELISMFLAGS=$PARALLELISMFLAGS \
537 LINKER=$LINKER \
538 TOOLS_DIR=$TOOLS_DIR \
539 INSTALLER_DIR=$INSTALLER_DIR \
540 CDROM_DIR=$CDROM_DIR \
541 IMAGES_DIR=$IMAGES_DIR \
542 UCLIBC_DIR=$UCLIBC_DIR \
543 UCLIBC_SYSROOT_DIR=$UCLIBC_SYSROOT_DIR \
544 UCLIBC_TARGET=$UCLIBC_TARGET \
545 UCLIBC_CC_CORE_STATIC_DIR=${UCLIBC_CC_CORE_STATIC_DIR} \
546 MACHINE="$MACHINE" \
547 MACHINE_REAL="$MACHINE_REAL" \
548 CFLAGS="$MYCFLAGS" \
549 CXXFLAGS="$MYCXXFLAGS" \
550 IFS_HOST="$IFS_HOST" \
551 IFS_TARGET="$IFS_TARGET" \
552 KVER=$KVER \
553 STAGE=$STAGE \
554 STAGE_ORDER=$STAGE_ORDER \
555 SSP=$SSP \
556 PIE=$PIE \
557 PAX=$PAX \
558 LOGFILE=`echo $LOGFILE | sed "s,$BASEDIR,/usr/src,g"` \
559 bash -x -c "cd /usr/src/lfs && \
560 $EXTRA_MAKE make -f $* LFS_BASEDIR=/usr/src install" >>$LOGFILE 2>&1
561
562 local COMPILE_SUCCESS=$?
563 local PKG_TIME_END=`date +%s`
564
565 if [ $COMPILE_SUCCESS -ne 0 ]; then
566 beautify result FAIL $[ $PKG_TIME_END - $PKG_TIME_START ] $1 $PKG_VER $STAGE_ORDER $STAGE
567 exiterror "Building $*";
568 else
569 beautify result DONE $[ $PKG_TIME_END - $PKG_TIME_START ] $1 $PKG_VER $STAGE_ORDER $STAGE
570 fi
571
572 return 0
573 } # End of ipfire_make()
574
575 ################################################################################
576 # This prepares the build environment #
577 ################################################################################
578 prepareenv() {
579
580 LOGFILE=$BASEDIR/log_${MACHINE}/_build.00-preparation.log
581 export LOGFILE
582 mkdir -p $BASEDIR/log_${MACHINE}/01_toolchain 2>/dev/null
583 mkdir -p $BASEDIR/log_${MACHINE}/02_base 2>/dev/null
584 mkdir -p $BASEDIR/log_${MACHINE}/03_${SNAME} 2>/dev/null
585 mkdir -p $BASEDIR/log_${MACHINE}/04_misc 2>/dev/null
586 mkdir -p $BASEDIR/log_${MACHINE}/05_installer 2>/dev/null
587 mkdir -p $BASEDIR/log_${MACHINE}/06_packages 2>/dev/null
588
589 #############################################################################
590 # Are we running the right shell? #
591 #############################################################################
592
593 if [ ! "$BASH" ]; then
594 exiterror "BASH environment variable is not set. You're probably running the wrong shell."
595 fi
596
597 if [ -z "${BASH_VERSION}" ]; then
598 exiterror "Not running BASH shell."
599 fi
600
601 #############################################################################
602 # Trap on emergency exit #
603 #############################################################################
604 trap "exiterror 'Build process interrupted'" SIGINT SIGTERM SIGKILL SIGSTOP SIGQUIT
605
606
607 #############################################################################
608 # Resetting our nice level #
609 #############################################################################
610 echo -ne "Resetting our nice level to $NICE"
611 renice $NICE $$ > /dev/null
612 if [ `nice` != "$NICE" ]; then
613 beautify message FAIL
614 exiterror "Failed to set correct nice level"
615 else
616 beautify message DONE
617 fi
618
619 # Set SCHED_BATCH
620 if [ -x /usr/bin/schedtool ]; then
621 echo -ne "Setting kernel schedular to SCHED_BATCH"
622 /usr/bin/schedtool -B $$
623 if [ $? -eq 0 ]; then
624 beautify message DONE
625 else
626 beautify message FAIL
627 fi
628 fi
629
630 ##############################################################################
631 # Checking if running as root user #
632 ##############################################################################
633 echo -ne "Checking if we're running as root user"
634 if [ `id -u` != 0 ]; then
635 beautify message FAIL
636 exiterror "Not building as root"
637 else
638 beautify message DONE
639 fi
640
641
642 ##############################################################################
643 # Checking for necessary temporary space #
644 ##############################################################################
645 echo -ne "Checking for necessary space on disk $BASE_DEV"
646 BASE_DEV=`df -P -k $BASEDIR | tail -n 1 | awk '{ print $1 }'`
647 BASE_ASPACE=`df -P -k $BASEDIR | tail -n 1 | awk '{ print $4 }'`
648 if (( 2048000 > $BASE_ASPACE )); then
649 BASE_USPACE=`du -skx $BASEDIR | awk '{print $1}'`
650 if (( 2048000 - $BASE_USPACE > $BASE_ASPACE )); then
651 beautify message FAIL
652 exiterror "Not enough temporary space available, need at least 2GB on $BASE_DEV"
653 fi
654 beautify message DONE
655 else
656 beautify message DONE
657 fi
658
659 ##############################################################################
660 # Setting security features #
661 ##############################################################################
662
663 echo -ne "Stack smashing protector (SSP)"
664 if [ $SSP -eq 1 ]; then
665 beautify message ON
666 else
667 beautify message OFF
668 fi
669
670 echo -ne "Position independent executeables (PIE)"
671 if [ $PIE -eq 1 ]; then
672 beautify message ON
673 else
674 beautify message OFF
675 fi
676
677 echo -ne "GRSecurity (PAX)"
678 if [ $PAX -eq 1 ]; then
679 beautify message ON
680 else
681 beautify message OFF
682 fi
683
684 export SSP PIE PAX
685
686 ##############################################################################
687 # Building Linux From Scratch system configuration #
688 ##############################################################################
689
690 # Set umask
691 umask 022
692
693 # Set LFS Directory
694 LFS=$BASEDIR/build_${MACHINE}/${SNAME}
695
696 # Check /tools symlink
697 if [ -h $TOOLS_DIR ]; then
698 rm -f $TOOLS_DIR
699 fi
700 if [ ! -a $TOOLS_DIR ]; then
701 ln -s $BASEDIR/build_${MACHINE}/$TOOLS_DIR /
702 fi
703 if [ ! -h $TOOLS_DIR ]; then
704 exiterror "Could not create $TOOLS_DIR symbolic link."
705 fi
706
707 # Setup environment
708 set +h
709 LC_ALL=POSIX
710 export LFS LC_ALL
711 unset CC CXX CPP LD_LIBRARY_PATH LD_PRELOAD
712
713 # Make some extra directories
714 mkdir -p $BASEDIR/build_${MACHINE}/{$TOOLS_DIR,cdrom} 2>/dev/null
715 mkdir -p $BASEDIR/{cache,ccache,distcc} 2>/dev/null
716 mkdir -p $BASEDIR/cache/toolchains 2>/dev/null
717 mkdir -p $LFS/{$TOOLS_DIR/usr,usr/src} 2>/dev/null
718 mkdir -p $LFS/dev/pts
719 mkdir -p $LFS/proc
720 mkdir -p $LFS/usr/src/{cache,config,doc,lfs,log_${MACHINE},src,ccache,distcc}
721 mkdir -p $LFS/{$INSTALLER_DIR,cdrom,images}
722 mkdir -p $LFS/etc
723
724 mknod -m 600 $BASEDIR/build/dev/console c 5 1 2>/dev/null
725 mknod -m 666 $BASEDIR/build/dev/null c 1 3 2>/dev/null
726
727 # Make all sources and proc available under lfs build
728 mount --bind /dev $LFS/dev
729 mount --bind /proc $LFS/proc
730 mount --bind $BASEDIR/cache $LFS/usr/src/cache
731 mount --bind $BASEDIR/ccache $LFS/usr/src/ccache
732 mount --bind $BASEDIR/distcc $LFS/usr/src/distcc
733 mount --bind $BASEDIR/config $LFS/usr/src/config
734 mount --bind $BASEDIR/doc $LFS/usr/src/doc
735 mount --bind $BASEDIR/lfs $LFS/usr/src/lfs
736 mount --bind $BASEDIR/log_${MACHINE} $LFS/usr/src/log_${MACHINE}
737 mount --bind $BASEDIR/src $LFS/usr/src/src
738 mount --bind $BASEDIR/build_${MACHINE}/$TOOLS_DIR $LFS/$TOOLS_DIR
739 mount --bind $BASEDIR/build_${MACHINE}/$CDROM_DIR $LFS/$CDROM_DIR
740
741 # Run LFS static binary creation scripts one by one
742 export CCACHE_DIR=$BASEDIR/ccache
743 export CCACHE_HASHDIR=1
744 if [ ! -z "$DISTCC_HOSTS" ]; then
745 export CCACHE_PREFIX="distcc"
746 export DISTCC_DIR=$BASEDIR/distcc
747 fi
748
749 [ -z "$DISTCC_HOSTS" ] || echo "$DISTCC_HOSTS" > $DISTCC_DIR/hosts
750
751 # Remove pre-install list of installed files in case user erase some files before rebuild
752 rm -f $LFS/usr/src/lsalr 2>/dev/null
753 }
754
755 build() {
756 clear
757 #a prebuilt toolchain package is only used if found in cache
758 if [ ! -d $BASEDIR/cache ]; then
759 exiterror "Use make.sh downloadsrc first!"
760 fi
761 cd $BASEDIR/cache/toolchains
762 PACKAGE=`ls -v -r $TOOLCHAINNAME.tar.bz2 2> /dev/null | head -n 1`
763 #only restore on a clean disk
764
765 echo -ne "Building for ${BOLD}${TARGET} (${MACHINE}) on ${MACHINE_REAL}${NORMAL}\n"
766
767 if [ -f $BASEDIR/log_${MACHINE}/02_base/stage2-LFS ]; then
768 prepareenv
769 echo "Using installed toolchain" >> $LOGFILE
770 beautify message DONE "Stage toolchain already built or extracted"
771 else
772 if [ -z "$PACKAGE" ]; then
773 echo "Full toolchain compilation"
774 prepareenv
775
776 . $BASEDIR/tools/make-check
777 check_toolchain
778
779 beautify build_stage "Building toolchain"
780 toolchain_build
781 else
782 echo "Restore from $PACKAGE"
783 cd $BASEDIR && tar jxf $BASEDIR/cache/toolchains/$PACKAGE
784 prepareenv
785 fi
786 fi
787
788 beautify build_stage "Building base"
789 base_build
790
791 beautify build_stage "Building $SNAME"
792 ipfire_build
793
794 beautify build_stage "Building miscellaneous"
795 misc_build
796
797 beautify build_stage "Building installer"
798 installer_build
799
800 beautify build_stage "Building packages"
801 packages_build
802
803 echo ""
804 echo "... and all this hard work for this:"
805 ls -sh $BASEDIR/${SNAME}-${VERSION}.${MACHINE}.iso
806 }