source: projects/mkkpkg/trunk/mkkpkg @ 6588

Revision 6588, 28.6 KB checked in by daisuke, 10 years ago (diff)

more fix for kernel 3.x

Line 
1#!/bin/bash
2#
3# mkkpkg: make new configured kernel package from kernel-xx-xx.src.rpm
4#
5# [Version 2.3.2] Jul 10 2000
6# Modified by sagami@pc.highway.ne.jp
7# - see Chgangelog
8#
9# [Version 2.3.0] Apr 20 2000
10# Modified by SAGAMI <sagami@pc.highway.ne.jp>
11# - renewed srpm checking process
12# - renewed RPMDIR checking precess
13# - and more...
14#
15# [Version 2.2.5] Mar 3 2000
16# Modified by Jun Nishii <jun@vinelinux.org>
17# - bug fix: set ARCH as i386 (i586 does not make pcmcia now)
18#
19# [Version 2.2.4] Feb 29 2000
20# Modified by Jun Nishii <jun@vinelinux.org>
21# - fix a small bug: patch from Shoji Matsumoto
22#
23# [Version 2.2.3] Feb 29 2000
24# Modified by Jun Nishii <jun@vinelinux.org>
25# - mkkpkg-2.2.3
26# - fix a bug and typo by Toru Sagami, thanks.
27#
28# [Version 2.2.2] Feb 25 2000
29# Modified by Jun Nishii <jun@vinelinux.org>
30# - fix a bug: wrong path for SRPMS
31#
32# [Version 2.2.1] Feb 25 2000
33# Modified by Jun Nishii <jun@vinelinux.org>
34# - fix a bug: lost definition of RPMRC
35#
36# [Version 2.2] Feb 24 2000
37# Modified by SAGAMI <sagami@pc.highway.ne.jp>
38# - fixed some bugs
39#   - 2.1 exits -1 in anyways :-p
40#   - BUILDMODE -> RPMBUILDMODE
41#   - Some ENV value needs {,}?
42#   - RPMDIR sometimes NULL
43# - moved /usr/bin/mkkpkg to /usr/sbin/mkkpkg
44# - tweaken, using --showrc --rmspec
45# - dont include packager tag in new spec file, because rpm will include it
46#   if defined in rpmmacros
47# - awk ARGV problems in parser
48#
49# [Version 2.1] Jan 30 2000
50# Modified by Jun Nishii <jun@vinelinux.org>
51# - merge patch from SAGAMI <sagami@pc.highway.ne.jp>
52#      + bug fix for processing .rpmmacros
53#      + use --target
54#      + added Packager, Vendor, Distribution in new spec file
55#      + --clean, --rmsource
56#      + use new revision number for new spec file name
57#      + modify messages
58# - clean up script
59#
60# [Version 2.0]
61# Modified by Jun Nishii <jun@vinelinux.org> Jan 10 2000
62# - use parsespec to remove smp, BOOT, BOOTsmp entry
63#
64# Modified by Yasuhide OOMORI <dasen@typhoon.co.jp>
65# - Quick hack about rpmdir.
66# - Modified for mistyping of new revision:
67#     -- remove control character
68# - Modified to quickly make one kernel:
69#     -- no compile for smp or BOOT kernel.
70#
71# [Verion 1.0]
72# Written by MATSUMOTO Shoji <vine@flatout.org>
73# This script must be distributed by Project Vine <Vine@vinelinux.org>.
74# The responsibility of any trouble with this script is on you.
75#
76############################################################
77
78MKKPKGVERSION=2.5.3
79CMDNAME=`basename $0`
80RETVAL=0
81
82############################################################
83### some functions
84
85showUsage(){
86        echo "$CMDNAME: make new configured kernel package from kernel-xx-xx.src.rpm"
87        echo "usage: $CMDNAME [-v] <sourcepackage> [config|menuconfig|xconfig]"
88        echo "Try \`$CMDNAME --help' for more"
89}
90
91showHelp(){
92echo "$CMDNAME:"
93echo "  make new configured kernel package from kernel SRPM."
94echo ""
95echo "  usage:"
96echo "  $CMDNAME --version     : show version and exit"
97echo "  $CMDNAME --help        : print this message and exit"
98echo ""
99echo "  $CMDNAME [-v] <sourcepackage> [config|menuconfig|xconfig] [...]"
100echo "    [-v|--verbose]     : be a little verbose"
101echo "    <sourcepackage>    : kernel SOURCE RPM (SRPM) to use"
102echo "    [config]           : execute \`make config'"
103echo "    [menuconfig]       : execute \`make menuconfig' (default)"
104echo "    [xconfig]          : execute \`make xconfig'"
105echo "    [gconfig]          : execute \`make gconfig' (for 2.6.x)"
106echo "    [--release=RELEASE]: RELEASE for user's release number"
107echo "    [--buildmode=MODE] : rpm build mode MODE [-ba|-bb]"
108echo "    [--rpmarg=RPMARG]  : additional rpm arguments RPMARG for building"
109echo "    [--english]        : show Configure.help in English (if available)"
110echo "    [--tmpdir=TMPDIR]  : directory to which log message goes"
111echo "    [--output=FILENAME]: log message to FILENAME in TMPDIR"
112#echo "    [--fetch]          : fetch SRPM via apt-get"
113#echo "    [--disable-edit]   : disable to edit SPEC file with EDITOR"
114echo "    [--target=ARCH]    : build target architecture ARCH"
115echo "    [--enable-smp]     : create smp/BOOT packages as well"
116#echo "    [--fast]           : build rpm without \"nice\""
117#echo "    [--fastest]        : build rpm with \"nice --19\" (root privileged)"
118#echo "    [--devel]          : assuming --smp --fast --verbose..."
119#echo "    [--rebuild]        : just rebuild SRPM (what for?)"
120echo ""
121}
122
123showbuildnotice(){
124if [ $QUIET != "true" ] ; then
125echo ""
126echo "  Log output goes to $TMPDIR/$LOGFILE."
127echo "  You can read it by following sequence:"
128echo "    1. Push \"Ctrl+Z\" to suspend the next long process"
129echo "    2. Type \"bg\" to continue background"
130echo "    3. Use \"less\" to print $TMPDIR/$LOGFILE"
131echo "    3.1 Push \"Shift+F\" to \"Forward forever; like tail -f\""
132echo "    3.2 If you want to stop reading, Push \"Ctrl+C\""
133echo "    4. Type \"fg\" to get back to $CMDNAME"
134echo "    X. If you are using X, you can read log thru another term window"
135echo ""
136echo "  It takes about 10-240 minutes depending on your configuration and also"
137echo -e "  depending on your machine: It's \\033[1;31mnot freezing!\\033[0;39m"
138echo ""
139echo "Ready for Action?"
140fi
141}
142
143checkDir(){
144if [ ! -d $1 ] ; then
145        echo "directory not found: $1"
146        return 1
147elif [ ! -w $1 ] ; then
148        echo "permission denied: $1"
149        return 1
150fi
151return 0
152}
153
154checkFile(){
155if [ ! -f $1 ] ; then
156        echo "file not found: $1"
157        return 1
158fi
159return 0
160}
161
162#rpm3var () {
163#       var=`rpm --showrc | sed -e "s/%{name}/$NAME/" \
164#        | awk "/-[0-9]+[:=][[:blank:]]+$1[[:blank:]]/ {print \\$3}"`
165#       while test -n "`echo $var | egrep '%\{[_a-zA-Z]+\}'`"; do
166#               v=`echo $var | sed 's/.*%{\([_a-zA-Z]\+\)}.*/\1/'`
167#               w="`rpm3var $v`"
168#               var=`echo $var | sed "s,%{\\([_a-zA-Z]\\+\\)},$w,g"`
169#       done
170#       echo $var
171#}
172rpm3var () {
173        var=$(rpm --eval "%$1" | sed -e "s|%{name}|$NAME|" -e "s|\${HOME}|$HOME|")
174        echo $var
175}
176
177
178getrpmmacro () {
179        macro=`rpm --showrc | grep ^.*[0-9][:=][[:space:]]"$1" \
180         | sed "s/^.*$1[[:space:]]//"`
181        echo $macro
182}
183
184askyesno () {
185        yesno=""
186        while [ -z "$yesno" ] ; do
187        echo -n "$1 [y/n] "
188        read yesno
189        yesno=`echo $yesno | tr -d '[:cntrl:]' | tr '[:upper:]' '[:lower:]'`
190        [ "$yesno" = "y" ] && return 1
191        [ "$yesno" = "n" ] && return 0
192        yesno=""
193        done
194}
195
196checkTMPdir () {
197        if [ -f $TMPDIR/$LOGFILE ] ; then
198        echo "$CMDNAME found: $TMPDIR/$LOGFILE"
199        LANG=C ls -l $TMPDIR/$LOGFILE
200        echo ""
201        echo "The log might be that of previous attempt, but we don't override it"
202        echo "even if you are root or owner. Delete it and try again. Also check if"
203        echo "someone else (including you) is running $CMDNAME."
204        exit -1
205        fi
206return 0
207}
208
209listbuilddir (){
210        find $1 -maxdepth 1 -type d -printf "%f\n" >$2
211}
212
213rpmqueryformat(){
214        rpm -qp $1 --queryformat "%{$2}" 2>/dev/null
215}
216############################################################
217### runtime environment
218LANGUAGE=
219
220############################################################
221### process arguments
222
223MARCH=`uname -m`
224case "$MARCH" in
225        *86)    ARCH=i686 ;;
226        x86_64) ARCH=x86_64 ;;
227        *)      ARCH=$MARCH ;;
228esac
229# ARCH=`uname -m | sed -e s/i.86/i386/ -e s/sun4u/sparc64/`
230
231TMPDIR=${TMPDIR:=/tmp}
232DISTRIBUTION=${DISTRIBUTION:-Local}
233LOGFILE=kernel-mkkpkg.log
234CONFMETHOD=menuconfig
235MESG=true
236QUIET=echo
237NICE=nice
238CONFIGURED=
239HELP=
240SMP=
241
242############################################################
243
244while [ $# -gt 0 ] ; do
245        case "$1" in
246        ####### experts only #######
247        # Yes, SMP actually means that we(who?) also build BOOT/mol packages.
248        --clean)
249                CLEAN="1" ;;
250        --noclean)
251                CLEAN="" ;;
252        --devel|--develo|--develop)
253                DEVEL="1"
254                # assuming --fast --smp --verbose --eng
255                { NICE="" ; SMP="1" ; MESG=echo ; HELP=en ; }
256        #DATE=`date +'%Y%m%d%I%M'`
257                #LOGFILE="kernel-mkkpkg-"${DATE}.log
258                ;;
259        --disable-smp|--without-smp|--nosmp|--smp=n*)
260                SMP="" ;;
261        --noenv|--noenvc|--noenvch|--noenvche|--noenvchec|--noenvcheck)
262                NOENVCHEKCK="1" ;;
263        --enable-smp|--with-smp|--smp|--smp=y*)
264                # SMP other than == 1 have no meanings
265                SMP="1" ;;
266        --fast|--disable-nice|--without-nice)
267                NICE="" ;;
268        --fastest)
269                NICE=""
270                [ "`id -u`" = "0" ] && NICE="nice --19"
271                ;;
272        --reb|--rebu|--rebui|--rebuil|--rebuild)
273                CONFMETHOD=[none]
274                REBUILD="1" ;;
275        ####### experts only #######
276        ####### experimental #######
277        --apt-get|--aptget|--fetch)
278                if [ -x ${APTGET:=/usr/bin/apt-get} ] ; then
279                        FETCH="LC_MESSAGES=C $APTGET -qq source kernel"
280                        FETCHFILE=`eval $FETCH --no-act | awk '{print $3}'`
281                        if [ ! -z "$FETCHFILE" ] ; then
282                                $QUIET "fetching source file $FETCHFILE via apt-get..."
283                                eval $FETCH
284                                if [ $? -ne 0 ] ; then
285                                        $QUIET "fetching source file via apt-get faild."
286                                else
287                                        $QUIET "fetched $FETCHFILE"
288                                        [ -f "$FETCHFILE" ] || $QUIET ", but not found"
289                                fi
290                        else
291                                $QUIET "fetching source file via apt-get failed."
292                        fi
293                        [ -f "$FETCHFILE" ] && SRPM=$FETCHFILE
294                fi
295                ;;
296        ####### experimental #######
297        --arg=*|--rpmarg=*)
298                RPMARG=`echo $1 | sed 's/.*=//'`
299                [ "$RPMARG" = "--nobuild" -o "$RPMARG" = "--test" ] && RPMARG=""
300                ;;
301        --edit|--enable-edit)
302                EDIT=y ;;
303        --noedit|--disable-edit)
304                EDIT="n" ;;
305        --en|--eng|--english)
306                HELP=en ;;
307        --ja|--jp|--japan|--japanese)
308                HELP=ja ;;
309        -h|--h|--he|--hel|--help)
310                showHelp && exit 0 ;;
311        --mode=*|--buildmode=*|--rpmbuildmode=*)
312                RPMBUILDMODE=`echo $1 | sed 's/.*=//'`
313                case "$RPMBUILDMODE" in
314                        --rebuild|rebuild) REBUILD="1" ;;
315                        -bs|bs|-ba|ba) RPMBUILDMODE="-ba" ;;
316                        -bb|bb) RPMBUILDMODE="-bb" ;;
317#                       -bc|bc) RPMBUILDMODE="-bc" ;;
318#                       -bi|bi) RPMBUILDMODE="-bi" ;;
319                        *) RPMBUILDMODE="" ;;
320                esac
321                ;;
322        --output=*)
323                LOGFILE=`echo $1 | sed 's/.*=//' | xargs basename`
324                ;;
325        --q|--qu|--qui|--quie|--quiet|--silent)
326                QUIET=true
327                MESG=true
328                ;;
329        --rel=*|--release=*|--rev=*|--revision=*)
330                NREV=`echo $1 | sed 's/.*=//' | sed -e 's/.*smp.*//' -e 's/.*mol.*//'`
331                ;;
332        --skip|--skipconfig|--skip-config|--config=skip)
333                CONFIGURED=y
334                CONFMETHOD=[skipped]
335                ;;
336        --specfile=*)
337                SPEC=`echo $1 | sed 's/.*=//'`
338                if [ ! -f "$SPEC" ] ; then
339                        echo "$CMDNAME: missing SPECFILE $SPEC" && exit 1
340                fi
341                SRPMDIR=`rpm3var _srcrpmdir`
342                NVR=`rpm -q --specfile $SPEC \
343                        --qf '%{NAME}-%{VERSION}-%{RELEASE}\n' | head -1`
344                if [ -z "$NVR" ] ; then
345                        echo "$CMDNAME: failed to query $SPEC"
346                        exit 1
347                fi
348                rpmbuild -bs $SPEC >/dev/null 2>&1
349                if [ $? -ne 0 ] ; then
350                        echo "$CMDNAME: failed to build $SPEC"
351                        exit 1
352                fi
353                SRPM="${SRPMDIR}/${NVR}.src.rpm"
354                if [ ! -f "$SRPM" ] ; then
355                        echo "$CMDNAME: missing SRPM $SRPM"
356                        exit 1
357                fi
358                [ -z "$RPMBUILDMODE" ] && RPMBUILDMODE="-ba"
359                ;;
360        --target=*|--target|--arch=*)
361                ARCH=`echo $1 | sed 's/.*=//' | sed 's/.*noarch.*//'`
362                if [ "$1" = "--target" -a ! -f "$2" ] ; then
363                        ARCH=`echo $2 | sed 's/.*=//' | sed 's/.*noarch.*//'`
364                fi
365                COMPAT=`LANG=C rpm --showrc | grep "^compatible build archs:" | \
366                  sed 's/compatible build archs://'`
367                MATCHED=""
368                for i in $COMPAT ; do
369                        [ "$ARCH" = "$i" ] && MATCHED="y"
370                done
371                [ ! "$MATCHED" = "y" ] && ARCH=""
372                [ "$1" = "--target" -a -n "$ARCH" ] && shift
373                ;;
374        --tmpdir=*|--tmp=*)
375                TMPDIR=`echo $1 | sed 's/.*=//'`
376                if [ ! -d "$TMPDIR" -o ! -w "$TMPDIR" ] ; then
377                        echo "$CMDNAME: can't write in $TMPDIR" && exit 1
378                fi
379                ;;
380        --config|config)
381                CONFMETHOD=config ;;
382        --old|--oldconfig|oldconfig)
383                CONFMETHOD=oldconfig ;;
384        --menu|--menuconfig|menuconfig)
385                CONFMETHOD=menuconfig ;;
386        --xconfig|--Xconfig|xconfig|Xconfig)
387                CONFMETHOD=xconfig
388                [ -z "$DISPLAY" ] && CONFMETHOD=menuconfig
389                ;;
390        --gconfig|gconfig)
391                CONFMETHOD=gconfig
392                [ -z "$DISPLAY" ] && CONFMETHOD=menuconfig
393                ;;
394        -v|--verb|--verbo|--verbos|--verbose)
395                MESG=echo
396                QUIET=echo
397                ;;
398        --vers|--versi|--versio|--version)
399                echo "$CMDNAME: version $MKKPKGVERSION"
400                exit 0
401                ;;
402        *)
403                [ -f $1 ] && SRPM=$1
404                if [ -z "$SRPM" ] ; then
405                echo "$CMDNAME error: invalid option $1"
406                showUsage && exit 1
407                fi
408                ;;
409        esac
410        shift
411done
412
413# with no arguments
414if [ -z "$SRPM" ] ; then
415        showUsage && exit
416fi
417
418############################################################
419# Print first banner. Can you read?
420# 34 blue 32 green 35 purple 31 red 33 yellow
421$MESG -e "\\033[1;34m$CMDNAME\\033[0;39m verbose mode"
422$MESG "$CMDNAME will execute \"make $CONFMETHOD\""
423
424############################################################
425## check SRPM and get KVER KREV from it
426
427# checking its signiture...
428rpm -K $SRPM >/dev/null
429if [ $? -ne 0 ] ; then
430        echo "`basename $SRPM`: didn't pass \"rpm -K\""
431        exit -1
432fi
433
434# checking if it's srpm...
435if [ "`file -L $SRPM | cut -d" " -f4`" != "src" ] ; then
436        echo "`basename $SRPM`: does not appear to be src rpm"
437        exit -1
438fi
439
440NAME=`rpmqueryformat $SRPM NAME`
441KVER=`rpmqueryformat $SRPM VERSION`
442KREV=`rpmqueryformat $SRPM RELEASE`
443SRPMNAME=${NAME}-${KVER}-${KREV}
444#SRPMNAME=`file -L $SRPM | cut -d" " -f6`
445
446############################################################
447## rebuild SRPM and exit (experts only)
448## do nothing other than rebuild: what for? -- It's for logged and timed.
449
450if [ "$REBUILD" = "1" ] ; then
451        LOGFILE=kernel-$KVER-$KREV-$ARCH.log
452        $QUIET "`LANG=C date '+%c'`"
453        $QUIET -n "$CMDNAME: rebuilding `basename $SRPM` for $ARCH..."
454        KSTARTTIME=`LANG=C date '+%s'`
455        eval LANG=C $NICE rpmbuild --rebuild $SRPM --target $ARCH $RPMARG \
456         >$TMPDIR/$LOGFILE 2>&1
457        RETVAL=$?
458        $QUIET "exit $RETVAL"
459        KENDTIME=`LANG=C date '+%s'`
460        BUILDLAPSE=`expr $KENDTIME - $KSTARTTIME`
461        $QUIET "Lapse: `expr $BUILDLAPSE / 60` min `expr $BUILDLAPSE % 60` sec" \
462        | tee -a $TMPDIR/$LOGFILE
463        gzip --force --best --quiet $TMPDIR/$LOGFILE
464        exit $RETVAL
465fi
466
467############################################################
468## set and check RPMDIR
469
470PACKAGER=`getrpmmacro packager`
471[ -z "$PACKAGER" ] && PACKAGER="$(whoami) <$(whoami)@$(hostname)>"
472TOPDIR=`rpm3var _topdir`
473BUILDDIR=`rpm3var _builddir`
474RPMDIR=`rpm3var _rpmdir`
475SPECDIR=`rpm3var _specdir`
476SRCDIR=`rpm3var _sourcedir`
477SRPMDIR=`rpm3var _srcrpmdir`
478
479if [ "$NOENVCHEKCK" != "1" ] ;then
480for dir in $TOPDIR $BUILDDIR $RPMDIR $SPECDIR \
481        $SRCDIR $SRPMDIR $RPMDIR/${ARCH}; do
482        checkDir $dir || {
483                mkdir -p $dir || exit -1
484        }
485done
486#checkDir $RPMDIR/noarch || exit -1
487$MESG Using rpmdir: $TOPDIR
488fi
489
490############################################################
491# checking if it's kernel and version is supported
492
493case "$NAME" in
494        kernel30|kernel26) # random guess (will be examined later)
495                BUILDLINUXDIR=${BUILDDIR}/${NAME}-${KVER}/linux-${KVER} ;;
496        kernel25) # random guess (will be examined later)
497                BUILDLINUXDIR=${BUILDDIR}/${NAME}-${KVER}/linux ;;
498        kernel24) # support for 2.4.x
499                BUILDLINUXDIR=${BUILDDIR}/linux-${KVER} ;;
500        kernel22) # random guess (will be examined later)
501                BUILDLINUXDIR=${BUILDDIR}/linux ;;
502        kernel)
503                case "$KVER" in
504                2.6*|3.*) # support for 2.6.x/3.x
505                        BUILDLINUXDIR=${BUILDDIR}/${NAME}-${KVER}/linux-${KVER} ;;
506                2.4*) # support for 2.4.x
507                        BUILDLINUXDIR=${BUILDDIR}/linux-${KVER} ;;
508                2.2*) # as of 2.2.17, it is OK.
509                        BUILDLINUXDIR=${BUILDDIR}/linux ;;
510                *)
511                        echo "`basename $SRPM`: version $KVER package is not supported"
512                        BUILDLINUXDIR=${BUILDDIR}/${NAME}-${KVER}/linux
513                        echo "try to detect \"linux\" directory to build..."
514                        ;;
515                esac
516                ;;
517        *)
518                echo "`basename $SRPM`: does not appear to be kernel package"
519                exit -1
520                ;;
521esac
522$MESG Using buildlinuxdir: $BUILDLINUXDIR
523
524
525############################################################
526## extract file names from SRPM
527
528# first $TMPFILE is made here
529TMPFILE=`mktemp $TMPDIR/$CMDNAME.XXXXXX` || \
530        { echo "could not create temporary file" ; exit 1 ; }
531rpm -qlp $SRPM >$TMPFILE
532SPEC="$SPECDIR/`grep '.spec$' $TMPFILE`"
533KCFG="`grep \"$ARCH.config\" $TMPFILE | tail -1`"
534
535# this is unusual, almost impossible
536if [ -z "$SPEC" ] ; then
537        echo "can't find SPEC file"
538        rm -f $TMPFILE && exit -1
539fi
540
541# as for now, srpm must have "$ARCH.config"
542if [ -z "$KCFG" ] ; then
543        echo "can't find default configuration file for $ARCH"
544        rm -f $TMPFILE && exit -1
545fi
546
547$MESG Using SPEC file: $SPEC
548$MESG Using kernel config file: $KCFG
549
550############################################################
551## install SRPM: "rpm -i" by root is thought to be a hole...
552
553$MESG "Executing \"rpm -ivh $SRPM\""
554rpm -ivh $SRPM >/dev/null 2>&1
555sync && sleep 1
556
557# checking files in source dir not only for nosrc, but also for src
558if [ "$NOENVCHEKCK" != "1" ] ;then
559$MESG -n "Checking files in SOURCES..."
560for file in `cat $TMPFILE | grep -v '\.spec$'` ; do
561        checkFile $SRCDIR/$file
562        if [ $? -ne 0 ] ; then
563                echo "It must be placed in or symbolic linked from ${SRCDIR}"
564                rm -f $TMPFILE && exit -1
565        fi
566done
567$MESG "done."
568fi
569
570# first $TMPFILE is removed here
571rm -f $TMPFILE
572
573############################################################
574## before expand SRPM: it costs a while and overrides BUILD
575## check if exists kernel-mkkpkg.log ($LOGFILE)
576## perhaps someone else is running mkkpkg, really?
577
578checkTMPdir
579
580############################################################
581## expand SRPM and find BUILDLINUXDIR (kind of ugly...)
582
583# print second -for "panpie"- banner
584if [ "$MESG" != "echo" ] ; then
585        $QUIET -e "Welcome to \\033[1;35m$CMDNAME\\033[0;39m"
586        $QUIET "" && sleep 1
587fi
588$QUIET "Please wait for a while \"make $CONFMETHOD\" to be executed..."
589
590# first, we clean up existing BUILDLINUXDIR (if any) by executing -bs
591mkdir -p $SRPMDIR/.${CMDNAME}.tmp
592rpmbuild -bs --clean $SPEC --define "_srcrpmdir $SRPMDIR/.${CMDNAME}.tmp" >/dev/null 2>&1
593rm -rf $SRPMDIR/.${CMDNAME}.tmp
594TMPPRE=`mktemp $TMPDIR/$CMDNAME.XXXXXX` || \
595        { echo "could not create temporary file" ; exit 1 ; }
596listbuilddir $BUILDDIR $TMPPRE
597
598# then, we expand
599$QUIET -n "Executing \"rpmbuild -bp $SPEC\"..."
600if ! rpmbuild -bp $SPEC >/dev/null 2>&1 ; then
601        echo "" && echo "Failed to expand sources/patches."
602        rm -f $TMPPRE && exit -1
603else
604        sync && sleep 3
605fi
606$QUIET "done."
607
608# afterwards, we examine and detect buildlinuxdir
609TMPPOST=`mktemp $TMPDIR/$CMDNAME.XXXXXX` || \
610        { echo "could not create temporary file" ; exit 1 ; }
611listbuilddir $BUILDDIR $TMPPOST
612TMPDIFF=`mktemp $TMPDIR/$CMDNAME.XXXXXX` || \
613        { echo "could not create temporary file" ; exit 1 ; }
614diff -B $TMPPRE $TMPPOST | grep "^> " | sed 's,^> ,,' >$TMPDIFF
615rm -f $TMPPRE $TMPPOST
616
617$MESG -n "Examining and detecting buildlinuxdir..."
618if [ ! -d "$BUILDLINUXDIR" ] ; then
619        BUILDLINUXDIR=""
620        KMAJOR=`echo $KVER | cut -f1,2 -d.`
621        for i in `cat $TMPDIFF` ; do
622                # kernel-2.2
623                if [ "$i" = "linux" -a -d "$BUILDDIR/linux" ] ; then
624                        BUILDLINUXDIR="$BUILDDIR/linux"
625                # kernel-2.4, 2.6
626                elif [ -d "$BUILDDIR/$i/linux" ] ; then
627                        BUILDLINUXDIR="$BUILDDIR/$i/linux"
628                # kernel-3.x
629                elif [ -d "$BUILDDIR/$i/linux-$KMAJOR" ] ; then
630                        BUILDLINUXDIR="$BUILDDIR/$i/linux-$KMAJOR"
631                fi
632        done
633        if [ -z "$BUILDLINUXDIR" ] ; then
634                echo "Can't detect RPM_BUILD_ROOT/linux directory"
635                rm -f $TMPDIFF && exit -1
636        fi
637fi
638$MESG "done."
639$MESG Using buildlinuxdir: $BUILDLINUXDIR
640rm -f $TMPDIFF
641
642############################################################
643## get release(revision) number
644
645while [ "$NREV" = "" ] ; do
646        echo -n "Input your kernel Revision [SRPM = $KREV]: "
647        read NREV
648
649        # remove control character in string
650        # string "smp" is bad thing for kernel-headers and /boot/kernel.h
651        NREV=`echo $NREV | tr -d '[:cntrl:]' | sed 's/.*smp.*//'`
652
653        if [ -f ${RPMDIR}/${ARCH}/kernel-${KVER}-${NREV}.${ARCH}.rpm ] ; then
654                echo "${RPMDIR}/${ARCH}/kernel-${KVER}-${NREV}.${ARCH}.rpm found."
655                askyesno "  Override?"
656                [ $? = 0 ] && NREV=""
657        elif [ "${KVER}-${NREV}" = "`uname -r`" ] ; then
658                echo "${KVER}-${NREV} is same as currently running."
659                askyesno "  Override?"
660                [ $? = 0 ] && NREV=""
661        # Confirmation of kernel release(revision)
662        elif [ -n "$NREV" ] ; then
663                askyesno "  kernel Revision [yours = $NREV]. OK?"
664                [ $? = 0 ] && NREV=""
665        fi
666done
667
668############################################################
669## make spec file
670
671NSPEC=${SPECDIR}/kernel-${KVER}-${NREV}.spec
672if [ "$NSPEC" = "$SPEC" ] ; then
673        mv -f $SPEC ${SPEC}.${CMDNAME}
674        SPEC=${SPEC}.${CMDNAME}
675fi
676
677### poor spec type detection (new type has build* macros)
678newtypespec=""
679for i in buildsmp buildBOOT buildenterprise ; do
680        grep -q "^[[:space:]]*%define[[:space:]]$i" $SPEC && newtypespec="1"
681done
682
683if [ "$SMP" = "1" -o "$newtypespec" = "1" ] ; then
684        [ ! "$SMP" = "1" ] && SMP="0"
685        cp -f $SPEC $NSPEC
686        TMPOUT=`mktemp $TMPDIR/.specout.XXXXXX` || \
687                { echo "could not create temporary file" ; exit 1 ; }
688
689        # we leave buildenterprise as it is (might be 0 by default)
690        # for i in buildsmp buildBOOT buildmol buildenterprise; do
691        for i in buildsmp buildBOOT buildmol ; do
692        sed -e "s/^[[:space:]]*\(%define[[:space:]]$i\).*/\1 $SMP/" < $NSPEC > $TMPOUT
693        mv -f $TMPOUT $NSPEC
694        done
695
696else
697        # oldtype without smp (default)
698        PARSESPEC=/usr/share/mkkpkg/parsespec
699        if [ ! -x $PARSESPEC ] ; then
700                echo "can't execute: $PARSESPEC" && exit -1
701        fi
702        $PARSESPEC $SPEC > $NSPEC
703fi
704
705if [ ! "$DEVEL" = "1" ] ; then
706        # common substitution
707        TMPOUT=`mktemp $TMPDIR/.specout.XXXXXX` || \
708                { echo "could not create temporary file" ; exit 1 ; }
709        sed -e "s/^Release.*/Release: $NREV/" \
710         -e "s/^Vendor.*/Vendor: mkkpkg ($SRPMNAME)/" \
711         -e "s/^Distribution.*/Distribution: $DISTRIBUTION/" \
712         -e "s/^Packager.*/Packager: $PACKAGER/" < $NSPEC >$TMPOUT
713        mv -f $TMPOUT $NSPEC
714else
715        # we do not want filename and tags above to be changed!
716        mv -f $NSPEC $SPEC
717        NSPEC=$SPEC
718fi
719
720$MESG "created new SPEC file: " $NSPEC
721$MESG "  from \"Release: $KREV\" to \"Release: $NREV\""
722# dont rm -f $SPEC now
723
724############################################################
725## run editor
726
727$QUIET "" && sleep 1
728cp -f $NSPEC $NSPEC.$$
729
730if [ ! "$EDIT" = "y" -a ! "$EDIT" = "n" ] ; then
731        askyesno "Do you want to edit `basename $NSPEC`?"
732        [ $? = 1 ] && EDIT="y"
733fi
734
735while [ "$EDIT" = "y" ] ; do
736        echo -n "Push \"enter\" to edit SPEC file [${EDITOR:=vi}]: "
737        read TMPEDITOR
738        [ "$TMPEDITOR" ] && EDITOR=$TMPEDITOR
739        $EDITOR $NSPEC \
740                || { echo "can't start editor. Continue..." ; EDITOR="" ; }
741        askyesno "Have you finished editting `basename $NSPEC`?"
742        [ $? = 1 ] && EDIT=""
743done
744
745if ! diff -bB $NSPEC.$$ $NSPEC >/dev/null ; then
746        echo "detected modified SPEC file"
747        rpm -q --specfile $NSPEC >/dev/null
748        if [ $? -ne 0 ] ; then
749                echo "You made a mistake while editing $NSPEC"
750                rm -f $NSPEC.$$ && exit -1
751        fi
752        UREV="`rpm -q --queryformat "%{RELEASE}\n" --specfile $NSPEC | uniq`"
753        UVER="`rpm -q --queryformat "%{VERSION}\n" --specfile $NSPEC | uniq`"
754        if [ "$UREV" != "$NREV" ] ; then
755                echo "Change detected after edit:"
756                UREV=`echo $UREV | sed 's/.*smp.*//'`
757                if [ -z "$UREV" ] ; then
758                        echo "$CMDNAME error: still \"smp\" string in RELEASE."
759                        rm -f $NSPEC.$$ && exit -1
760                fi
761                echo "  from \"Release: $NREV\" to \"Release: $UREV\""
762                NREV=$UREV
763        fi
764        # BTW, how come you persist in using mkkpkg?
765        if [ "$UVER" != "$KVER" ] ; then
766                echo "  from \"Version: $KVER\" to \"Version: $UVER\""
767                KVER=$UVER
768        fi
769
770        askyesno "Do you want to re-expand sources/patches with new SPEC file?"
771        if [ $? = 1 ] ; then
772                echo "Executing \"rpmbuild -bp $NSPEC\""
773                TMPOUT=`mktemp $TMPDIR/${CMDNAME}-expand.XXXXXX` || \
774                        { echo "could not create temporary file" ; exit 1 ; }
775                if rpmbuild -bp $NSPEC >$TMPOUT 2>&1 ; then
776                        sync && sleep 3
777                        echo "Succeeded in expanding sources/patches."
778                        rm -f $TMPOUT
779                else
780                        echo "Failed to expand sources/patches."
781                        echo "See $TMPOUT."
782                        rm -f $NSPEC.$$ && exit -1
783                fi
784        fi
785fi
786rm -f $NSPEC.$$
787[ "$NSPEC" != "$SPEC" ] && rm -f $SPEC
788############################################################
789## asking whether to build srpm
790
791if [ -z "$RPMBUILDMODE" ] ; then
792        askyesno "Do you want to build SRPM? (If no, only RPMs are built)"
793        if [ $? = 1 ] ; then
794                RPMBUILDMODE="-ba"
795                $MESG "  OK, SRPM and RPMs will be built."
796        else
797                RPMBUILDMODE="-bb"
798                $MESG "  OK, only RPMs will be built."
799        fi
800fi
801
802############################################################
803## now make config
804
805checkDir $BUILDLINUXDIR || exit -1
806
807if [ "$HELP" = "en" ] ; then
808if [ "$NOENVCHEKCK" != "1" ] ;then
809        if [ -L $BUILDLINUXDIR/Documentation/Configure.help \
810           -a -f $BUILDLINUXDIR/Documentation/Configure.help.en ] ; then
811        pushd $BUILDLINUXDIR/linux/Documentation >/dev/null 2>&1
812        $MESG "Using Configure.help.en ..."
813        ln -sf Configure.help.en Configure.help
814        popd >/dev/null 2>&1
815        fi
816fi
817fi
818
819# make sure EXTRAVERSION says what we want it to say, but...
820perl -pi -e "s/^EXTRAVERSION.*/EXTRAVERSION = -${NREV}/" \
821 $BUILDLINUXDIR/Makefile || :
822
823if [ "$SMP" = "1" ] ; then
824        if [ ! "$ARCH" = "ppc" ] ; then
825        CONFFILE="`set +f; echo $BUILDLINUXDIR/configs/{*-$ARCH,*-$ARCH-smp,*-$ARCH-BOOT}.config`"
826        else
827        CONFFILE="`set +f; echo $BUILDLINUXDIR/configs/{*-$ARCH,*-$ARCH-smp,*-$ARCH-mol,*-$ARCH-BOOT}.config`"
828        fi
829else
830        CONFFILE="`set +f; echo $BUILDLINUXDIR/configs/*-$ARCH.config`"
831fi
832
833while [ -z "$CONFIGURED" ] ; do
834        for file in $CONFFILE ; do
835                if [ -f $file ] ; then
836                        KCFG=`basename $file`
837                        $MESG "Copying $KCFG into source tree for $ARCH"
838                        cp -f $file $BUILDLINUXDIR/.config
839                elif [ "$SMP" != "1" ] ; then
840                        # Could this sentence be evalueted?!
841                        $MESG "Missing?! `basename $file`, using default?!"
842                else
843                        $MESG -n "Ignoring... "
844                        $MESG "`basename $file` seems to be BOOT on non-base architecture"
845                        KCFG=""
846                fi
847
848                if [ -n "$KCFG" ] ; then
849                        echo "Ready for make \"$CONFMETHOD\" by $KCFG"
850                        echo -n "[Push enter to continue] "
851                        read
852                        make -s $CONFMETHOD -C $BUILDLINUXDIR
853                        if [ $? -ne 0 ] ; then
854                        askyesno "Something wrong with \"make $CONFMETHOD\". Continue?"
855                                [ $? = 0 ] && exit -1
856                        fi
857                        checkFile $BUILDLINUXDIR/.config || \
858                                { echo "$BUILDLINUXDIR/.config is missing " ; exit -1 ; }
859                        ## leaving original and user-configured .configs in SOURCES
860                        $MESG "mv $SRCDIR/${KCFG}{,.mkkpkg}"
861                        mv -f $SRCDIR/${KCFG}{,.mkkpkg}
862                        $MESG "Copying .config into $SRCDIR/${KCFG}"
863                        ## ad-hoc fix (20050621)
864                        case "$ARCH" in
865                            *86) echo "# i386"    > $SRCDIR/${KCFG} ;;
866                            ppc) echo "# powerpc" > $SRCDIR/${KCFG} ;;
867                            *)  echo "# $ARCH"    > $SRCDIR/${KCFG} ;;
868                        esac
869                        cat $BUILDLINUXDIR/.config >> $SRCDIR/${KCFG}
870                        ## cp -f $BUILDLINUXDIR/.config $SRCDIR/${KCFG}
871                fi
872        echo "" && sleep 1
873        done
874        CONFIGURED="y"
875done
876
877# it will create these binaries, but some of them are only for base arch.
878TMPFILE=`mktemp $TMPDIR/$CMDNAME.XXXXXX` || \
879        { echo "could not create temporary file" ; exit 1 ; }
880rpm -q --specfile $NSPEC | sed "s/$/\.$ARCH\.rpm/g" | sort > $TMPFILE
881
882############################################################
883## now build rpm package
884
885## test $RPMARG
886if [ -n "$RPMARG" ] ; then
887        if ! rpmbuild -bp $NSPEC --target=$ARCH $RPMARG --nobuild >/dev/null 2>&1
888        then
889                RPMARG=""
890        fi
891fi
892
893RPMBUILDCMD="rpmbuild $RPMBUILDMODE $NSPEC --target=$ARCH $RPMARG >$TMPDIR/$LOGFILE 2>&1"
894ADDITIONALFLAGS="smp=$SMP build=${NICE:-fast}"
895
896# output lines should be less than 24 lines.
897clear
898if [ $QUIET != "true" ] ;then
899echo ""
900echo -e "\\033[1;32m$CMDNAME\\033[0;39m"
901echo ""
902echo "Now we are prepared for executing:"
903echo "  rpmbuild $RPMBUILDMODE $NSPEC --target=$ARCH $RPMARG"
904fi
905
906if [ ! "$DEVEL" = "1" ] ; then
907        [ "$SMP" = "1" -o ! "$NICE" = "nice" ] && echo "With: $ADDITIONALFLAGS"
908        showbuildnotice
909        echo -n "[Push enter to continue] "
910        read
911fi
912$QUIET "Started: `LANG=C date '+%c'`"
913echo -n "Building..."
914
915## Build
916KSTARTTIME=`LANG=C date '+%s'`
917if checkTMPdir ; then
918        eval $NICE $RPMBUILDCMD
919        RETVAL=$?
920fi
921
922############################################################
923## check return value and whether package are made
924
925KENDTIME=`LANG=C date '+%s'`
926BUILDLAPSE=`expr $KENDTIME - $KSTARTTIME`
927
928# echo "EXIT $RETVAL" | mail -s "$COMNAME finised on `hostname`" `whoami`
929
930if [ $RETVAL -ne 0 ] ; then
931        echo "" && echo "Errored: `LANG=C date '+%c'`"
932        echo -n "[Push enter] "
933        read
934        echo "Some errors were occurred with non-zero status $RETVAL."
935        echo "Please check $TMPDIR/$LOGFILE."
936        rm -f $TMPFILE && exit -1
937fi
938
939echo ""
940$QUIET "Finished: `LANG=C date '+%c'`"
941$MESG "Lapse: `expr $BUILDLAPSE / 60` min `expr $BUILDLAPSE % 60` sec"
942echo -n "[Push enter] "
943read
944
945NEWRPM=${RPMDIR}/${ARCH}/${NAME}-${KVER}-${NREV}.${ARCH}.rpm
946if [ -f $NEWRPM ] ; then
947        echo "Created:"
948        for i in `cat $TMPFILE` ; do
949        [ -f $RPMDIR/${ARCH}/$i ] && LANG=C ls $RPMDIR/${ARCH}/$i
950        done
951else
952        echo "$NEWRPM does not seem to have been created."
953        echo "Please check $TMPDIR/$LOGFILE."
954        rm -f $TMPFILE && exit -1
955fi
956
957NEWSRPM="${SRPMDIR}/`rpmqueryformat $NEWRPM SOURCERPM`"
958if [ "$RPMBUILDMODE" = "-ba" ] ; then
959        if [ -f $NEWSRPM ] ; then
960                # echo "Created:"
961                LANG=C ls $NEWSRPM
962        else
963                echo "$NEWSRPM does not seem to have been created."
964                # dont exit! it is trivial
965        fi
966fi
967
968############################################################
969## clean up
970
971if [ "$CLEAN" != "1" ] ;then
972        echo "How do you want $LOGFILE in $TMPDIR?"
973        echo -n "m)move to $HOME  r)remove  *)leave in $TMPDIR : "
974        read yn
975        yn=`echo $yn | tr -d '[:cntrl:]' | tr '[:upper:]' '[:lower:]'`
976else
977        yn=r
978fi
979
980if [ "$yn" = "r" ] ; then
981        rm -f $TMPDIR/$LOGFILE
982elif [ "$yn" = "m" ] ; then
983        if [ -n "$HOME" ] ; then
984                [ "$HOME" != "$TMPDIR" ] && mv -f $TMPDIR/$LOGFILE $HOME
985                gzip --quiet --force --best $HOME/$LOGFILE
986        fi
987fi
988
989askyesno "Do you want to clean up BUILD tree and files in SOURCES?"
990[ $? = 1 ]  && CLEAN=1
991
992if [ "$CLEAN" = "1" ] ;then
993        rpmbuild --rmsource --rmspec --clean --force $NSPEC >/dev/null 2>&1
994        rm -f $SPEC
995        rm -f $SRCDIR/*.config.mkkpkg
996fi
997
998############################################################
999## terminates
1000
1001rm -f $TMPFILE
1002exit 0
1003
1004# vim6: ts=4 tw=0 nolist ai laststatus=2 statusline=%<%f%h%m%r%y%=%l,%c%V\ %P
1005# $Id: mkkpkg,v 1.22 2007/08/09 02:50:53 shaolin Exp $
Note: See TracBrowser for help on using the repository browser.