]> git.ipfire.org Git - people/teissler/ipfire-2.x.git/blob - tools/make-functions
Merge commit 'origin/master' into next
[people/teissler/ipfire-2.x.git] / tools / make-functions
1 #!/bin/bash
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2010 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 ^VER $1 | awk '{print $3}'`
199
200 if [ -z $PKG_VER ]; then
201 PKG_VER=`grep "Exp " $1 | awk '{print $4}'`
202 fi
203
204 if [ ${#PKG_VER} -gt $VER_WIDTH ]; then
205 # If a package version number is greater than $VER_WIDTH, we keep the first 4 characters
206 # and replace enough characters to fit the resulting string on the screen. We'll replace
207 # the extra character with .. (two dots). That's why the "+ 2" in the formula below.
208 # Example: if we have a 21-long version number that we want to fit into a 10-long space,
209 # we have to remove 11 characters. But if we replace 11 characters with 2 characters, we'll
210 # end up with a 12-character long string. That's why we replace 12 characters with ..
211 REMOVE=`expr substr "$PKG_VER" 4 $[ ${#PKG_VER} - $VER_WIDTH + 2 ]`
212 PKG_VER=`echo ${PKG_VER/$REMOVE/..}`
213 fi
214
215 echo "$PKG_VER"
216 } # End of get_pkg_ver()
217
218 if [ 'x86_64' = $MACHINE -o 'i686' = $MACHINE -o 'i586' = $MACHINE ]; then
219 echo "`date -u '+%b %e %T'`: Machine is iX86 (or equivalent)" >> $LOGFILE
220 MACHINE=i586
221 BUILDTARGET=i586-pc-linux-gnu
222 CFLAGS="-O2 -march=i586 -pipe -fomit-frame-pointer"
223 CXXFLAGS="-O2 -march=i586 -pipe -fomit-frame-pointer"
224 C2FLAGS="-O2 -march=i586 -mtune=i586 -pipe -fomit-frame-pointer"
225 CXX2FLAGS="-O2 -march=i586 -mtune=i586 -pipe -fomit-frame-pointer"
226 elif [ 'armv5tejl' = $MACHINE -o 'armv5tel' = $MACHINE ]; then
227 echo "`date -u '+%b %e %T'`: Machine is ARM (or equivalent)" >> $LOGFILE
228 MACHINE=armv5tel
229 MACHINE_TYPE=arm
230 BUILDTARGET=${MACHINE}-unknown-linux-gnueabi
231 CFLAGS="-O2 -march=armv5te -fomit-frame-pointer -pipe"
232 CXXFLAGS="$CFLAGS"
233 C2FLAGS="$CFLAGS"
234 CXX2FLAGS="$CXXFLAGS"
235 else
236 echo "`date -u '+%b %e %T'`: Can't determine your architecture - $MACHINE" >> $LOGFILE
237 exit 1
238 fi
239
240 # Define immediately
241 stdumount() {
242 umount $BASEDIR/build/sys 2>/dev/null;
243 umount $BASEDIR/build/dev/shm 2>/dev/null;
244 umount $BASEDIR/build/dev/pts 2>/dev/null;
245 umount $BASEDIR/build/dev 2>/dev/null;
246 umount $BASEDIR/build/proc 2>/dev/null;
247 umount $BASEDIR/build/install/mnt 2>/dev/null;
248 umount $BASEDIR/build/usr/src/cache 2>/dev/null;
249 umount $BASEDIR/build/usr/src/ccache 2>/dev/null;
250 umount $BASEDIR/build/usr/src/config 2>/dev/null;
251 umount $BASEDIR/build/usr/src/doc 2>/dev/null;
252 umount $BASEDIR/build/usr/src/html 2>/dev/null;
253 umount $BASEDIR/build/usr/src/langs 2>/dev/null;
254 umount $BASEDIR/build/usr/src/lfs 2>/dev/null;
255 umount $BASEDIR/build/usr/src/log 2>/dev/null;
256 umount $BASEDIR/build/usr/src/src 2>/dev/null;
257 }
258
259 exiterror() {
260 stdumount
261 for i in `seq 0 7`; do
262 if ( losetup /dev/loop${i} 2>/dev/null | grep -q "/install/images" ); then
263 losetup -d /dev/loop${i} 2>/dev/null
264 fi;
265 done
266 echo -e "\nERROR: $*"
267 echo " Check $LOGFILE for errors if applicable"
268 exit 1
269 }
270
271 entershell() {
272 if [ ! -e $BASEDIR/build/usr/src/lfs/ ]; then
273 exiterror "No such file or directory: $BASEDIR/build/usr/src/lfs/"
274 fi
275 echo "Entering to a shell inside LFS chroot, go out with exit"
276 $linux32 chroot $LFS /tools/bin/env -i HOME=/root TERM=$TERM PS1='\u:\w\$ ' \
277 PATH=/usr/local/bin:/bin:/usr/bin:/sbin:/usr/sbin:/tools/bin \
278 VERSION=$VERSION CONFIG_ROOT=$CONFIG_ROOT \
279 NAME="$NAME" SNAME="$SNAME" SLOGAN="$SLOGAN" \
280 CFLAGS="$CF2LAGS" CXXFLAGS="$CXX2FLAGS" \
281 CCACHE_DIR=/usr/src/ccache \
282 CCACHE_COMPRESS=1 \
283 CCACHE_HASHDIR=1 \
284 KVER=$KVER \
285 BUILDTARGET="$BUILDTARGET" \
286 MACHINE="$MACHINE" \
287 MACHINE_TYPE="$MACHINE_TYPE" \
288 KGCC="ccache /usr/bin/gcc" \
289 /tools/bin/bash
290 if [ $? -ne 0 ]; then
291 beautify message FAIL
292 exiterror "chroot error"
293 else
294 stdumount
295 fi
296 }
297
298 ############################################################################
299 # #
300 # Necessary shell functions #
301 # #
302 ############################################################################
303 #
304 # Common checking before entering the chroot and compilling
305 #
306 # Return:0 caller can continue
307 # :1 skip (nothing to do)
308 # or fail if no script file found
309 #
310 lfsmakecommoncheck()
311 {
312 # Script present?
313 if [ ! -f $BASEDIR/lfs/$1 ]; then
314 exiterror "No such file or directory: $BASEDIR/$1"
315 fi
316
317 local PKG_VER=`get_pkg_ver $BASEDIR/lfs/$1`
318 beautify make_pkg "$PKG_VER $*"
319
320 # Check if this package is supported by our architecture.
321 # If no SUP_ARCH is found, we assume the package can be built for all.
322 if grep "^SUP_ARCH" ${BASEDIR}/lfs/${1} >/dev/null; then
323 # Check if package supports ${MACHINE} or all architectures.
324 if ! grep -E "^SUP_ARCH.*${MACHINE}|^SUP_ARCH.*all" ${BASEDIR}/lfs/${1} >/dev/null; then
325 beautify result SKIP
326 return 1
327 fi
328 fi
329
330 # Script slipped?
331 local i
332 for i in $SKIP_PACKAGE_LIST
333 do
334 if [ "$i" == "$1" ]; then
335 beautify result SKIP
336 return 1;
337 fi
338 done
339
340 echo -ne "`date -u '+%b %e %T'`: Building $* " >> $LOGFILE
341
342 cd $BASEDIR/lfs && make -s -f $* LFS_BASEDIR=$BASEDIR MACHINE=$MACHINE \
343 MACHINE_TYPE="$MACHINE_TYPE" \
344 MESSAGE="$1\t " download >> $LOGFILE 2>&1
345 if [ $? -ne 0 ]; then
346 exiterror "Download error in $1"
347 fi
348
349 cd $BASEDIR/lfs && make -s -f $* LFS_BASEDIR=$BASEDIR MACHINE=$MACHINE \
350 MACHINE_TYPE="$MACHINE_TYPE" \
351 MESSAGE="$1\t md5sum" md5 >> $LOGFILE 2>&1
352 if [ $? -ne 0 ]; then
353 exiterror "md5sum error in $1, check file in cache or signature"
354 fi
355
356 return 0 # pass all!
357 } # End of lfsmakecommoncheck()
358
359 lfsmake1() {
360 lfsmakecommoncheck $*
361 [ $? == 1 ] && return 0
362
363 local PKG_TIME_START=`date +%s`
364
365 cd $BASEDIR/lfs && make -f $* BUILDTARGET=$BUILDTARGET \
366 MACHINE=$MACHINE \
367 MACHINE_TYPE=$MACHINE_TYPE \
368 LFS_BASEDIR=$BASEDIR \
369 ROOT=$LFS \
370 KVER=$KVER \
371 MAKETUNING=$MAKETUNING \
372 install >> $LOGFILE 2>&1
373 local COMPILE_SUCCESS=$?
374 local PKG_TIME_END=`date +%s`
375
376 if [ $COMPILE_SUCCESS -ne 0 ]; then
377 beautify result FAIL $[ $PKG_TIME_END - $PKG_TIME_START ]
378 exiterror "Building $*";
379 else
380 beautify result DONE $[ $PKG_TIME_END - $PKG_TIME_START ]
381 fi
382
383 return 0
384 }
385
386 lfsmake2() {
387 lfsmakecommoncheck $*
388 [ $? == 1 ] && return 0
389
390 local PKG_TIME_START=`date +%s`
391 $linux32 chroot $LFS /tools/bin/env -i HOME=/root \
392 TERM=$TERM PS1='\u:\w\$ ' \
393 PATH=/usr/local/bin:/bin:/usr/bin:/sbin:/usr/sbin:/tools/bin \
394 VERSION=$VERSION \
395 CONFIG_ROOT=$CONFIG_ROOT \
396 NAME="$NAME" SNAME="$SNAME" SLOGAN="$SLOGAN" \
397 CFLAGS="$C2FLAGS" CXXFLAGS="$CXX2FLAGS" \
398 CCACHE_DIR=/usr/src/ccache CCACHE_COMPRESS=1 CCACHE_HASHDIR=1 \
399 KVER=$KVER MAKETUNING=$MAKETUNING \
400 BUILDTARGET="$BUILDTARGET" MACHINE="$MACHINE" \
401 MACHINE_TYPE="$MACHINE_TYPE" \
402 /tools/bin/bash -x -c "cd /usr/src/lfs && \
403 make -f $* LFS_BASEDIR=/usr/src install" >>$LOGFILE 2>&1
404 local COMPILE_SUCCESS=$?
405 local PKG_TIME_END=`date +%s`
406
407 if [ $COMPILE_SUCCESS -ne 0 ]; then
408 beautify result FAIL $[ $PKG_TIME_END - $PKG_TIME_START ]
409 exiterror "Building $*";
410 else
411 beautify result DONE $[ $PKG_TIME_END - $PKG_TIME_START ]
412 fi
413
414 return 0
415 }
416
417 ipfiremake() {
418 lfsmakecommoncheck $*
419 [ $? == 1 ] && return 0
420
421 local PKG_TIME_START=`date +%s`
422 $linux32 chroot $LFS /tools/bin/env -i HOME=/root \
423 TERM=$TERM PS1='\u:\w\$ ' \
424 PATH=/usr/local/bin:/bin:/usr/bin:/sbin:/usr/sbin \
425 VERSION=$VERSION \
426 CORE=$CORE \
427 CONFIG_ROOT=$CONFIG_ROOT \
428 NAME="$NAME" SNAME="$SNAME" SLOGAN="$SLOGAN" \
429 CFLAGS="$C2FLAGS" CXXFLAGS="$CXX2FLAGS" \
430 CCACHE_DIR=/usr/src/ccache CCACHE_COMPRESS=1 CCACHE_HASHDIR=1 \
431 KVER=$KVER MAKETUNING=$MAKETUNING \
432 BUILDTARGET="$BUILDTARGET" MACHINE="$MACHINE" \
433 MACHINE_TYPE="$MACHINE_TYPE" \
434 /bin/bash -x -c "cd /usr/src/lfs && \
435 make -f $* LFS_BASEDIR=/usr/src install" >>$LOGFILE 2>&1
436
437 local COMPILE_SUCCESS=$?
438 local PKG_TIME_END=`date +%s`
439
440 if [ $COMPILE_SUCCESS -ne 0 ]; then
441 beautify result FAIL $[ $PKG_TIME_END - $PKG_TIME_START ]
442 exiterror "Building $*";
443 else
444 beautify result DONE $[ $PKG_TIME_END - $PKG_TIME_START ]
445 fi
446 return 0
447 }
448
449 ipfiredist() {
450 lfsmakecommoncheck $*
451 [ $? == 1 ] && return 0
452
453 local PKG_TIME_START=`date +%s`
454 chroot $LFS /tools/bin/env -i HOME=/root \
455 TERM=$TERM PS1='\u:\w\$ ' \
456 PATH=/usr/local/bin:/bin:/usr/bin:/sbin:/usr/sbin \
457 VERSION=$VERSION \
458 CONFIG_ROOT=$CONFIG_ROOT \
459 NAME="$NAME" SNAME="$SNAME" SLOGAN="$SLOGAN" \
460 CFLAGS="$C2FLAGS" CXXFLAGS="$CXX2FLAGS" \
461 CCACHE_DIR=/usr/src/ccache CCACHE_COMPRESS=1 CCACHE_HASHDIR=1 \
462 KVER=$KVER \
463 BUILDTARGET="$BUILDTARGET" MACHINE="$MACHINE" \
464 MACHINE_TYPE="$MACHINE_TYPE" \
465 /bin/bash -x -c "cd /usr/src/lfs && \
466 make -f $1 LFS_BASEDIR=/usr/src dist" >>$LOGFILE 2>&1
467
468 local COMPILE_SUCCESS=$?
469 local PKG_TIME_END=`date +%s`
470
471 if [ $COMPILE_SUCCESS -ne 0 ]; then
472 beautify result FAIL $[ $PKG_TIME_END - $PKG_TIME_START ]
473 exiterror "Packaging $*";
474 else
475 beautify result DONE $[ $PKG_TIME_END - $PKG_TIME_START ]
476 fi
477 return 0
478 }
479
480 installmake() {
481 lfsmakecommoncheck $*
482 [ $? == 1 ] && return 0
483
484 local PKG_TIME_START=`date +%s`
485 $linux32 chroot $LFS /tools/bin/env -i HOME=/root \
486 TERM=$TERM PS1='\u:\w\$ ' \
487 PATH=/opt/i586-uClibc/i586-linux-uclibc/bin:/opt/i586-uClibc/bin:/usr/local/bin:/bin:/usr/bin:/sbin:/usr/sbin \
488 VERSION=$VERSION \
489 CONFIG_ROOT=$CONFIG_ROOT \
490 LFS_PASS="install" \
491 NAME="$NAME" SNAME="$SNAME" SLOGAN="$SLOGAN" \
492 CFLAGS="-Os" CXXFLAGS="-Os" \
493 CCACHE_DIR=/usr/src/ccache CCACHE_COMPRESS=1 CCACHE_HASHDIR=1 \
494 KVER=$KVER \
495 BUILDTARGET="$BUILDTARGET" MACHINE="$MACHINE" \
496 MACHINE_TYPE="$MACHINE_TYPE" \
497 /bin/bash -x -c "cd /usr/src/lfs && \
498 make -f $* LFS_BASEDIR=/usr/src install" >>$LOGFILE 2>&1
499
500 local COMPILE_SUCCESS=$?
501 local PKG_TIME_END=`date +%s`
502
503 if [ $COMPILE_SUCCESS -ne 0 ]; then
504 beautify result FAIL $[ $PKG_TIME_END - $PKG_TIME_START ]
505 exiterror "Building $*";
506 else
507 beautify result DONE $[ $PKG_TIME_END - $PKG_TIME_START ]
508 fi
509 return 0
510 }
511
512 update_langs() {
513 echo -ne "Checking the translations for missing or obsolete strings..."
514 chmod 755 $BASEDIR/tools/{check_strings.pl,sort_strings.pl,check_langs.sh}
515 $BASEDIR/tools/sort_strings.pl en
516 $BASEDIR/tools/sort_strings.pl de
517 $BASEDIR/tools/sort_strings.pl fr
518 $BASEDIR/tools/sort_strings.pl es
519 $BASEDIR/tools/sort_strings.pl pl
520 $BASEDIR/tools/sort_strings.pl ru
521 $BASEDIR/tools/check_strings.pl en > $BASEDIR/doc/language_issues.en
522 $BASEDIR/tools/check_strings.pl de > $BASEDIR/doc/language_issues.de
523 $BASEDIR/tools/check_strings.pl fr > $BASEDIR/doc/language_issues.fr
524 $BASEDIR/tools/check_strings.pl es > $BASEDIR/doc/language_issues.es
525 $BASEDIR/tools/check_strings.pl es > $BASEDIR/doc/language_issues.pl
526 $BASEDIR/tools/check_strings.pl ru > $BASEDIR/doc/language_issues.ru
527 $BASEDIR/tools/check_langs.sh > $BASEDIR/doc/language_missings
528 beautify message DONE
529 }