start core55.
[ipfire-2.x.git] / tools / make-functions
1 #!/bin/bash
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2007-2011 IPFire Team <info@ipfire.org> #
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 # Beautifying variables & presentation & input output interface
24 #
25 ###############################################################################
26
27 ## Screen Dimentions
28 # Find current screen size
29 if [ -z "${COLUMNS}" ]; then
30 COLUMNS=$(stty size)
31 COLUMNS=${COLUMNS##* }
32 fi
33
34 # When using remote connections, such as a serial port, stty size returns 0
35 if [ "${COLUMNS}" = "0" ]; then
36 COLUMNS=80
37 fi
38
39 ## Measurements for positioning result messages
40 RESULT_WIDTH=4
41 TIME_WIDTH=8
42 OPT_WIDTH=6
43 VER_WIDTH=10
44 RESULT_COL=$((${COLUMNS} - $RESULT_WIDTH - 4))
45 TIME_COL=$((${RESULT_COL} - $TIME_WIDTH - 5))
46 OPT_COL=$((${TIME_COL} - $OPT_WIDTH - 5))
47 VER_COL=$((${OPT_COL} - $VER_WIDTH - 5))
48
49 ## Set Cursur Position Commands, used via echo -e
50 SET_RESULT_COL="\\033[${RESULT_COL}G"
51 SET_TIME_COL="\\033[${TIME_COL}G"
52 SET_OPT_COL="\\033[${OPT_COL}G"
53 SET_VER_COL="\\033[${VER_COL}G"
54
55 # Define color for messages
56 BOLD="\\033[1;39m"
57 DONE="\\033[1;32m"
58 SKIP="\\033[1;34m"
59 WARN="\\033[1;35m"
60 FAIL="\\033[1;31m"
61 NORMAL="\\033[0;39m"
62
63 evaluate() {
64 if [ "$?" -eq "0" ]; then
65 beautify message DONE
66 else
67 EXITCODE=$1
68 shift 1
69 beautify message FAIL
70 $*
71 if [ $EXITCODE -ne "0" ]; then
72 exit $EXITCODE
73 fi
74 fi
75 }
76
77 position_cursor()
78 {
79 # ARG1=starting position on screen
80 # ARG2=string to be printed
81 # ARG3=offset, negative for left movement, positive for right movement, relative to ARG1
82 # For example if your starting position is column 50 and you want to print Hello three columns to the right
83 # of your starting position, your call will look like this:
84 # position_cursor 50 "Hello" 3 (you'll get the string Hello at position 53 (= 50 + 3)
85 # If on the other hand you want your string "Hello" to end three columns to the left of position 50,
86 # your call will look like this:
87 # position_cursor 50 "Hello" -3 (you'll get the string Hello at position 42 (= 50 - 5 -3)
88 # If you want to start printing at the exact starting location, use offset 0
89
90 START=$1
91 STRING=$2
92 OFFSET=$3
93
94 STRING_LENGTH=${#STRING}
95
96 if [ ${OFFSET} -lt 0 ]; then
97 COL=$((${START} + ${OFFSET} - ${STRING_LENGTH}))
98 else
99 COL=$((${START} + ${OFFSET}))
100 fi
101
102 SET_COL="\\033[${COL}G"
103
104 echo $SET_COL
105 } # End of position_cursor()
106
107
108 beautify()
109 {
110 # Commands: build_stage, make_pkg, message, result
111 case "$1" in
112 message)
113 case "$2" in
114 DONE)
115 echo -ne "${SET_RESULT_COL}[${DONE} DONE ${NORMAL}]\n"
116 ;;
117 WARN)
118 echo -ne "${WARN}${3}${NORMAL}${SET_RESULT_COL}[${WARN} WARN ${NORMAL}]\n"
119 ;;
120 FAIL)
121 echo -ne "${SET_RESULT_COL}[${FAIL} FAIL ${NORMAL}]\n"
122 ;;
123 SKIP)
124 echo -ne "${SET_RESULT_COL}[${SKIP} SKIP ${NORMAL}]\n"
125 ;;
126 esac
127 ;;
128 build_stage)
129 MESSAGE=$2
130 if [ "$STAGE_TIME_START" ]; then
131 LAST_STAGE_TIME=$[ `date +%s` - $STAGE_TIME_START ]
132 fi
133 STAGE_TIME_START=`date +%s`
134 echo -ne "${BOLD}*** ${MESSAGE}${NORMAL}"
135 if [ "$LAST_STAGE_TIME" ]; then
136 echo -ne "${DONE} (Last stage took $LAST_STAGE_TIME secs)${NORMAL}"
137 fi
138 echo -ne "${BOLD}${SET_VER_COL} version${SET_OPT_COL} options${SET_TIME_COL} time (sec)${SET_RESULT_COL} status${NORMAL}\n"
139 ;;
140 build_start)
141 BUILD_TIME_START=`date +%s`
142 ;;
143 build_end)
144 BUILD_TIME_END=`date +%s`
145 seconds=$[ $BUILD_TIME_END - $BUILD_TIME_START ]
146 hours=$((seconds / 3600))
147 seconds=$((seconds % 3600))
148 minutes=$((seconds / 60))
149 seconds=$((seconds % 60))
150
151 echo -ne "${DONE}***Build is finished now and took $hours hour(s) $minutes minute(s) $seconds second(s)!${NORMAL}\n"
152 ;;
153 make_pkg)
154 echo "$2" | while read PKG_VER PROGRAM OPTIONS
155 do
156 SET_VER_COL_REAL=`position_cursor $OPT_COL $PKG_VER -3`
157
158 if [ "$OPTIONS" == "" ]; then
159 echo -ne "${PROGRAM}${SET_VER_COL}[ ${BOLD}${SET_VER_COL_REAL}${PKG_VER}"
160 echo -ne "${NORMAL} ]${SET_RESULT_COL}"
161 else
162 echo -ne "${PROGRAM}${SET_VER_COL}[ ${BOLD}${SET_VER_COL_REAL}${PKG_VER}"
163 echo -ne "${NORMAL} ]${SET_OPT_COL}[ ${BOLD}${OPTIONS}"
164 echo -ne "${NORMAL} ]${SET_RESULT_COL}"
165 fi
166 done
167 ;;
168 result)
169 RESULT=$2
170
171 if [ ! $3 ]; then
172 PKG_TIME=0
173 else
174 PKG_TIME=$3
175 fi
176
177 SET_TIME_COL_REAL=`position_cursor $RESULT_COL $PKG_TIME -3`
178 case "$RESULT" in
179 DONE)
180 echo -ne "${SET_TIME_COL}[ ${BOLD}${SET_TIME_COL_REAL}$PKG_TIME${NORMAL} ]"
181 echo -ne "${SET_RESULT_COL}[${DONE} DONE ${NORMAL}]\n"
182 ;;
183 FAIL)
184 echo -ne "${SET_TIME_COL}[ ${BOLD}${SET_TIME_COL_REAL}$PKG_TIME${NORMAL} ]"
185 echo -ne "${SET_RESULT_COL}[${FAIL} FAIL ${NORMAL}]\n"
186 ;;
187 SKIP)
188 echo -ne "${SET_RESULT_COL}[${SKIP} SKIP ${NORMAL}]\n"
189 ;;
190 esac
191 ;;
192 esac
193 } # End of beautify()
194
195
196 get_pkg_ver()
197 {
198 PKG_VER=`grep -E "^VER |^VER=|^VER " $1 | awk '{print $3}'`
199
200 if [ -z $PKG_VER ]; then
201 PKG_VER=`grep "Exp " $1 | awk '{print $4}'`
202 fi
203 if [ -z $PKG_VER ]; then
204 PKG_VER="?"
205 fi
206 if [ ${#PKG_VER} -gt $VER_WIDTH ]; then
207 # If a package version number is greater than $VER_WIDTH, we keep the first 4 characters
208 # and replace enough characters to fit the resulting string on the screen. We'll replace
209 # the extra character with .. (two dots). That's why the "+ 2" in the formula below.
210 # Example: if we have a 21-long version number that we want to fit into a 10-long space,
211 # we have to remove 11 characters. But if we replace 11 characters with 2 characters, we'll
212 # end up with a 12-character long string. That's why we replace 12 characters with ..
213 REMOVE=`expr substr "$PKG_VER" 4 $[ ${#PKG_VER} - $VER_WIDTH + 2 ]`
214 PKG_VER=`echo ${PKG_VER/$REMOVE/..}`
215 fi
216
217 echo "$PKG_VER"
218 } # End of get_pkg_ver()
219
220 if [ 'x86_64' = $MACHINE -o 'i686' = $MACHINE -o 'i586' = $MACHINE ]; then
221 echo "`date -u '+%b %e %T'`: Machine is iX86 (or equivalent)" >> $LOGFILE
222 MACHINE=i586
223 BUILDTARGET=i586-pc-linux-gnu
224 CFLAGS="-O2 -march=i586 -pipe -fomit-frame-pointer"
225 CXXFLAGS="-O2 -march=i586 -pipe -fomit-frame-pointer"
226 C2FLAGS="-O2 -march=i586 -mtune=i586 -pipe -fomit-frame-pointer"
227 CXX2FLAGS="-O2 -march=i586 -mtune=i586 -pipe -fomit-frame-pointer"
228 elif [ 'armv5tejl' = $MACHINE -o 'armv5tel' = $MACHINE ]; then
229 echo "`date -u '+%b %e %T'`: Machine is ARM (or equivalent)" >> $LOGFILE
230 MACHINE=armv5tel
231 MACHINE_TYPE=arm
232 BUILDTARGET=${MACHINE}-unknown-linux-gnueabi
233 CFLAGS="-O2 -march=armv5te -fomit-frame-pointer -pipe"
234 CXXFLAGS="$CFLAGS"
235 C2FLAGS="$CFLAGS"
236 CXX2FLAGS="$CXXFLAGS"
237 else
238 echo "`date -u '+%b %e %T'`: Can't determine your architecture - $MACHINE" >> $LOGFILE
239 exit 1
240 fi
241
242 # Define immediately
243 stdumount() {
244 umount $BASEDIR/build/sys 2>/dev/null;
245 umount $BASEDIR/build/dev/shm 2>/dev/null;
246 umount $BASEDIR/build/dev/pts 2>/dev/null;
247 umount $BASEDIR/build/dev 2>/dev/null;
248 umount $BASEDIR/build/proc 2>/dev/null;
249 umount $BASEDIR/build/install/mnt 2>/dev/null;
250 umount $BASEDIR/build/usr/src/cache 2>/dev/null;
251 umount $BASEDIR/build/usr/src/ccache 2>/dev/null;
252 umount $BASEDIR/build/usr/src/config 2>/dev/null;
253 umount $BASEDIR/build/usr/src/doc 2>/dev/null;
254 umount $BASEDIR/build/usr/src/html 2>/dev/null;
255 umount $BASEDIR/build/usr/src/langs 2>/dev/null;
256 umount $BASEDIR/build/usr/src/lfs 2>/dev/null;
257 umount $BASEDIR/build/usr/src/log 2>/dev/null;
258 umount $BASEDIR/build/usr/src/src 2>/dev/null;
259 }
260
261 exiterror() {
262 stdumount
263 for i in `seq 0 7`; do
264 if ( losetup /dev/loop${i} 2>/dev/null | grep -q "/install/images" ); then
265 losetup -d /dev/loop${i} 2>/dev/null
266 fi;
267 done
268 echo -e "\nERROR: $*"
269 echo " Check $LOGFILE for errors if applicable"
270 exit 1
271 }
272
273 entershell() {
274 if [ ! -e $BASEDIR/build/usr/src/lfs/ ]; then
275 exiterror "No such file or directory: $BASEDIR/build/usr/src/lfs/"
276 fi
277 echo "Entering to a shell inside LFS chroot, go out with exit"
278 $linux32 chroot $LFS /tools/bin/env -i HOME=/root TERM=$TERM PS1='\u:\w\$ ' \
279 PATH=/usr/local/bin:/bin:/usr/bin:/sbin:/usr/sbin:/tools/bin \
280 VERSION=$VERSION CONFIG_ROOT=$CONFIG_ROOT \
281 NAME="$NAME" SNAME="$SNAME" SLOGAN="$SLOGAN" \
282 CFLAGS="$CF2LAGS" CXXFLAGS="$CXX2FLAGS" \
283 CCACHE_DIR=/usr/src/ccache \
284 CCACHE_COMPRESS=1 \
285 CCACHE_HASHDIR=1 \
286 KVER=$KVER \
287 BUILDTARGET="$BUILDTARGET" \
288 MACHINE="$MACHINE" \
289 MACHINE_TYPE="$MACHINE_TYPE" \
290 KGCC="ccache /usr/bin/gcc" \
291 /tools/bin/bash
292 if [ $? -ne 0 ]; then
293 beautify message FAIL
294 exiterror "chroot error"
295 else
296 stdumount
297 fi
298 }
299
300 ############################################################################
301 # #
302 # Necessary shell functions #
303 # #
304 ############################################################################
305 #
306 # Common checking before entering the chroot and compilling
307 #
308 # Return:0 caller can continue
309 # :1 skip (nothing to do)
310 # or fail if no script file found
311 #
312 lfsmakecommoncheck()
313 {
314 # Script present?
315 if [ ! -f $BASEDIR/lfs/$1 ]; then
316 exiterror "No such file or directory: $BASEDIR/$1"
317 fi
318
319 local PKG_VER=`get_pkg_ver $BASEDIR/lfs/$1`
320 beautify make_pkg "$PKG_VER $*"
321
322 # Check if this package is supported by our architecture.
323 # If no SUP_ARCH is found, we assume the package can be built for all.
324 if grep "^SUP_ARCH" ${BASEDIR}/lfs/${1} >/dev/null; then
325 # Check if package supports ${MACHINE} or all architectures.
326 if ! grep -E "^SUP_ARCH.*${MACHINE}|^SUP_ARCH.*all" ${BASEDIR}/lfs/${1} >/dev/null; then
327 beautify result SKIP
328 return 1
329 fi
330 fi
331
332 # Script slipped?
333 local i
334 for i in $SKIP_PACKAGE_LIST
335 do
336 if [ "$i" == "$1" ]; then
337 beautify result SKIP
338 return 1;
339 fi
340 done
341
342 echo -ne "`date -u '+%b %e %T'`: Building $* " >> $LOGFILE
343
344 cd $BASEDIR/lfs && make -s -f $* LFS_BASEDIR=$BASEDIR MACHINE=$MACHINE \
345 MACHINE_TYPE="$MACHINE_TYPE" \
346 MESSAGE="$1\t " download >> $LOGFILE 2>&1
347 if [ $? -ne 0 ]; then
348 exiterror "Download error in $1"
349 fi
350
351 cd $BASEDIR/lfs && make -s -f $* LFS_BASEDIR=$BASEDIR MACHINE=$MACHINE \
352 MACHINE_TYPE="$MACHINE_TYPE" \
353 MESSAGE="$1\t md5sum" md5 >> $LOGFILE 2>&1
354 if [ $? -ne 0 ]; then
355 exiterror "md5sum error in $1, check file in cache or signature"
356 fi
357
358 return 0 # pass all!
359 } # End of lfsmakecommoncheck()
360
361 lfsmake1() {
362 lfsmakecommoncheck $*
363 [ $? == 1 ] && return 0
364
365 local PKG_TIME_START=`date +%s`
366
367 cd $BASEDIR/lfs && make -f $* BUILDTARGET=$BUILDTARGET \
368 MACHINE=$MACHINE \
369 MACHINE_TYPE=$MACHINE_TYPE \
370 LFS_BASEDIR=$BASEDIR \
371 ROOT=$LFS \
372 KVER=$KVER \
373 MAKETUNING=$MAKETUNING \
374 install >> $LOGFILE 2>&1
375 local COMPILE_SUCCESS=$?
376 local PKG_TIME_END=`date +%s`
377
378 if [ $COMPILE_SUCCESS -ne 0 ]; then
379 beautify result FAIL $[ $PKG_TIME_END - $PKG_TIME_START ]
380 exiterror "Building $*";
381 else
382 beautify result DONE $[ $PKG_TIME_END - $PKG_TIME_START ]
383 fi
384
385 return 0
386 }
387
388 lfsmake2() {
389 lfsmakecommoncheck $*
390 [ $? == 1 ] && return 0
391
392 local PKG_TIME_START=`date +%s`
393 $linux32 chroot $LFS /tools/bin/env -i HOME=/root \
394 TERM=$TERM PS1='\u:\w\$ ' \
395 PATH=/usr/local/bin:/bin:/usr/bin:/sbin:/usr/sbin:/tools/bin \
396 VERSION=$VERSION \
397 CONFIG_ROOT=$CONFIG_ROOT \
398 NAME="$NAME" SNAME="$SNAME" SLOGAN="$SLOGAN" \
399 CFLAGS="$C2FLAGS" CXXFLAGS="$CXX2FLAGS" \
400 CCACHE_DIR=/usr/src/ccache CCACHE_COMPRESS=1 CCACHE_HASHDIR=1 \
401 KVER=$KVER MAKETUNING=$MAKETUNING \
402 BUILDTARGET="$BUILDTARGET" MACHINE="$MACHINE" \
403 MACHINE_TYPE="$MACHINE_TYPE" \
404 /tools/bin/bash -x -c "cd /usr/src/lfs && \
405 make -f $* LFS_BASEDIR=/usr/src install" >>$LOGFILE 2>&1
406 local COMPILE_SUCCESS=$?
407 local PKG_TIME_END=`date +%s`
408
409 if [ $COMPILE_SUCCESS -ne 0 ]; then
410 beautify result FAIL $[ $PKG_TIME_END - $PKG_TIME_START ]
411 exiterror "Building $*";
412 else
413 beautify result DONE $[ $PKG_TIME_END - $PKG_TIME_START ]
414 fi
415
416 return 0
417 }
418
419 ipfiremake() {
420 lfsmakecommoncheck $*
421 [ $? == 1 ] && return 0
422
423 local PKG_TIME_START=`date +%s`
424 $linux32 chroot $LFS /tools/bin/env -i HOME=/root \
425 TERM=$TERM PS1='\u:\w\$ ' \
426 PATH=/usr/local/bin:/bin:/usr/bin:/sbin:/usr/sbin \
427 VERSION=$VERSION \
428 CORE=$CORE \
429 CONFIG_ROOT=$CONFIG_ROOT \
430 NAME="$NAME" SNAME="$SNAME" SLOGAN="$SLOGAN" \
431 CFLAGS="$C2FLAGS" CXXFLAGS="$CXX2FLAGS" \
432 CCACHE_DIR=/usr/src/ccache CCACHE_COMPRESS=1 CCACHE_HASHDIR=1 \
433 KVER=$KVER MAKETUNING=$MAKETUNING \
434 BUILDTARGET="$BUILDTARGET" MACHINE="$MACHINE" \
435 MACHINE_TYPE="$MACHINE_TYPE" \
436 /bin/bash -x -c "cd /usr/src/lfs && \
437 make -f $* LFS_BASEDIR=/usr/src install" >>$LOGFILE 2>&1
438
439 local COMPILE_SUCCESS=$?
440 local PKG_TIME_END=`date +%s`
441
442 if [ $COMPILE_SUCCESS -ne 0 ]; then
443 beautify result FAIL $[ $PKG_TIME_END - $PKG_TIME_START ]
444 exiterror "Building $*";
445 else
446 beautify result DONE $[ $PKG_TIME_END - $PKG_TIME_START ]
447 fi
448 return 0
449 }
450
451 ipfiredist() {
452 lfsmakecommoncheck $*
453 [ $? == 1 ] && return 0
454
455 local PKG_TIME_START=`date +%s`
456 chroot $LFS /tools/bin/env -i HOME=/root \
457 TERM=$TERM PS1='\u:\w\$ ' \
458 PATH=/usr/local/bin:/bin:/usr/bin:/sbin:/usr/sbin \
459 VERSION=$VERSION \
460 CONFIG_ROOT=$CONFIG_ROOT \
461 NAME="$NAME" SNAME="$SNAME" SLOGAN="$SLOGAN" \
462 CFLAGS="$C2FLAGS" CXXFLAGS="$CXX2FLAGS" \
463 CCACHE_DIR=/usr/src/ccache CCACHE_COMPRESS=1 CCACHE_HASHDIR=1 \
464 KVER=$KVER \
465 BUILDTARGET="$BUILDTARGET" MACHINE="$MACHINE" \
466 MACHINE_TYPE="$MACHINE_TYPE" \
467 /bin/bash -x -c "cd /usr/src/lfs && \
468 make -f $1 LFS_BASEDIR=/usr/src dist" >>$LOGFILE 2>&1
469
470 local COMPILE_SUCCESS=$?
471 local PKG_TIME_END=`date +%s`
472
473 if [ $COMPILE_SUCCESS -ne 0 ]; then
474 beautify result FAIL $[ $PKG_TIME_END - $PKG_TIME_START ]
475 exiterror "Packaging $*";
476 else
477 beautify result DONE $[ $PKG_TIME_END - $PKG_TIME_START ]
478 fi
479 return 0
480 }
481
482 installmake() {
483 lfsmakecommoncheck $*
484 [ $? == 1 ] && return 0
485
486 local PKG_TIME_START=`date +%s`
487 $linux32 chroot $LFS /tools/bin/env -i HOME=/root \
488 TERM=$TERM PS1='\u:\w\$ ' \
489 PATH=/opt/i586-uClibc/i586-linux-uclibc/bin:/opt/i586-uClibc/bin:/usr/local/bin:/bin:/usr/bin:/sbin:/usr/sbin \
490 VERSION=$VERSION \
491 CONFIG_ROOT=$CONFIG_ROOT \
492 LFS_PASS="install" \
493 NAME="$NAME" SNAME="$SNAME" SLOGAN="$SLOGAN" \
494 CFLAGS="-Os" CXXFLAGS="-Os" \
495 CCACHE_DIR=/usr/src/ccache CCACHE_COMPRESS=1 CCACHE_HASHDIR=1 \
496 KVER=$KVER \
497 BUILDTARGET="$BUILDTARGET" MACHINE="$MACHINE" \
498 MACHINE_TYPE="$MACHINE_TYPE" \
499 /bin/bash -x -c "cd /usr/src/lfs && \
500 make -f $* LFS_BASEDIR=/usr/src install" >>$LOGFILE 2>&1
501
502 local COMPILE_SUCCESS=$?
503 local PKG_TIME_END=`date +%s`
504
505 if [ $COMPILE_SUCCESS -ne 0 ]; then
506 beautify result FAIL $[ $PKG_TIME_END - $PKG_TIME_START ]
507 exiterror "Building $*";
508 else
509 beautify result DONE $[ $PKG_TIME_END - $PKG_TIME_START ]
510 fi
511 return 0
512 }
513
514 update_langs() {
515 echo -ne "Checking the translations for missing or obsolete strings..."
516 chmod 755 $BASEDIR/tools/{check_strings.pl,sort_strings.pl,check_langs.sh}
517 $BASEDIR/tools/sort_strings.pl en
518 $BASEDIR/tools/sort_strings.pl de
519 $BASEDIR/tools/sort_strings.pl fr
520 $BASEDIR/tools/sort_strings.pl es
521 $BASEDIR/tools/sort_strings.pl pl
522 $BASEDIR/tools/sort_strings.pl ru
523 $BASEDIR/tools/check_strings.pl en > $BASEDIR/doc/language_issues.en
524 $BASEDIR/tools/check_strings.pl de > $BASEDIR/doc/language_issues.de
525 $BASEDIR/tools/check_strings.pl fr > $BASEDIR/doc/language_issues.fr
526 $BASEDIR/tools/check_strings.pl es > $BASEDIR/doc/language_issues.es
527 $BASEDIR/tools/check_strings.pl es > $BASEDIR/doc/language_issues.pl
528 $BASEDIR/tools/check_strings.pl ru > $BASEDIR/doc/language_issues.ru
529 $BASEDIR/tools/check_langs.sh > $BASEDIR/doc/language_missings
530 beautify message DONE
531 }