source: trunk/technical/common/build/build.sh@ 733

Last change on this file since 733 was 733, checked in by joergs, on Jan 26, 2009 at 4:33:42 PM

users /usr/local/bin/buildscript for sudo instead of sudo su -

  • Property svn:executable set to *
  • Property svn:keywords set to Id LastChangedDate LastChangedRevision
File size: 8.6 KB
Line 
1#!/bin/bash
2# build script
3#
4# $Id: build.sh 733 2009-01-26 15:33:42Z joergs $
5#
6# builds RPM in an clean chroot build environment.
7# uses the Suse build script.
8#
9# without parameter, it checks the directory for the files
10# SOURCES or SPECSOURCES.
11# The files definied in there are copied to the
12# build system.
13#
14# when used with parameter,
15# this parameter is expected to be a source rpm
16#
17# config file:
18# /usr/local/etc/build
19# lists the sources for the different distributions
20#
21# the rest is controlled by environment variables
22#
23# build.sh uses sudo. /etc/sudoers needs a line like:
24# %users ALL=(ALL) NOPASSWD: /usr/local/bin/buildscript
25# for automatic building (create a symlink or copy the file,
26# see also the Variable BUILDSCRIPT and content of this script)
27#
28# return codes:
29# 0: success
30# 1: skipped (current RPM already in dest dir)
31# >1: error
32#
33# RPM releases are set
34# to the svn version number of the project directory.
35# Modifiers are:
36# m: release + local modifications
37# p: release + submited modifications. svn update project directory is needed
38
39
40# set LANG to default (english)
41# required for SVN
42export LC_ALL=C
43
44CONFIG_FILE=/usr/local/etc/build
45BUILDSCRIPT=buildscript
46BUILD_CMD=build
47BUILDUSER=$USER
48
49[ -z "$BUILD_MODE" ] && BUILD_MODE="--clean"
50[ -z "$BUILD_DIST" ] && BUILD_DIST="sles9-i386"
51[ -z "$BUILD_ROOT" ] && BUILD_ROOT="/var/tmp/buildsystem.$USER.$BUILD_DIST"
52[ -z "$BUILD_PREPARE_SPEC" ] && BUILD_PREPARE_SPEC="no"
53
54[ -z $DEST_DIR ] && DEST_DIR="/tmp/build.$BUILDUSER"
55BUILDTEMP=$DEST_DIR/buildsrc/
56#BUILDSCRIPT=$BUILDTEMP/buildscript.$$
57BUILDVARS=$BUILDTEMP/build.vars
58
59[ -z "$BUILD_USES_SVN" ] && BUILD_USES_SVN="yes"
60
61
62
63if [ -r $CONFIG_FILE ]; then
64 source $CONFIG_FILE
65 # case "$BUILD_DIST" in
66 # "9.0-i386")
67 # BUILD_RPMS_TEST=$BUILD_RPMS_9_0_i386 ;;
68 # "sles8-i386")
69 # BUILD_RPMS_TEST=$BUILD_RPMS_sles8_i386 ;;
70 # "sles8-i386+update")
71 # BUILD_RPMS_TEST=$BUILD_RPMS_sles8_i386_update ;;
72 # "9.1-i386")
73 # BUILD_RPMS_TEST=$BUILD_RPMS_9_1_i386 ;;
74 # "sles9-i386")
75 # BUILD_RPMS_TEST=$BUILD_RPMS_sles9_i386 ;;
76 # "9.2-i386")
77 # BUILD_RPMS_TEST=$BUILD_RPMS_9_2_i386 ;;
78 # "9.3-i386")
79 # BUILD_RPMS_TEST=$BUILD_RPMS_9_3_i386 ;;
80 # "10.0-i386")
81 # BUILD_RPMS_TEST=$BUILD_RPMS_10_0_i386 ;;
82 # "10.1-i386")
83 # BUILD_RPMS_TEST=$BUILD_RPMS_10_1_i386 ;;
84 # "10.2-i386")
85 # BUILD_RPMS_TEST=$BUILD_RPMS_10_2_i386 ;;
86 # "10.3-i386")
87 # BUILD_RPMS_TEST=$BUILD_RPMS_10_3_i386 ;;
88 # esac
89
90 MOD_BUILD_DIST=`sed "s/[-+.]/_/g" <<< $BUILD_DIST`
91 echo MOD_BUILD_DIST: $MOD_BUILD_DIST
92 eval BUILD_RPMS_TEST=\$BUILD_RPMS_$MOD_BUILD_DIST
93 #echo $BUILD_RPMS_TEST
94
95 if [ "$BUILD_RPMS_TEST" ]; then
96 if [ "$BUILD_RPMS" ]; then
97 BUILD_RPMS=$BUILD_RPMS:$BUILD_RPMS_TEST
98 else
99 BUILD_RPMS=$BUILD_RPMS_TEST
100 fi
101 fi
102fi
103
104echo BUILD_RPMS: $BUILD_RPMS
105
106ERROR=0
107
108# Hier gehts los:
109
110if [ -z "$1" ]; then
111 # without parameter, we are in source directory
112 SPECFILE=$(ls *.spec | awk '{print $1}')
113 PACKAGE=$(sed -n -e 's/^Name:\W*//pi' <$SPECFILE)
114 VERSION=$(sed -n -e 's/^Version:\W*//pi' <$SPECFILE)
115elif [ -r "$1" ]; then
116 # parameter is src.rpm file
117 PACKAGE=`rpm -qp --qf "%{NAME}" $1`
118 VERSION=`rpm -qp --qf "%{VERSION}" $1`
119fi
120
121echo $PACKAGE $VERSION $BUILD_DIST $BUILD_MODE
122
123if [ -z "$PACKAGE" ]; then
124 echo "nothing to build found!"
125 exit 1
126fi
127
128
129
130rm -rf ${BUILDTEMP}/src/${PACKAGE};
131mkdir -p ${BUILDTEMP}/src/${PACKAGE}
132# ${BUILDTEMP}/${BUILD_DIST};
133
134if [ "$1" ]; then
135 EXPECTED_DEST_RPM_FILENAME=${DEST_DIR}/${BUILD_DIST}/`echo $1 | sed 's/\.src\./.*./'`
136 if [ -f $EXPECTED_DEST_RPM_FILENAME ]; then
137 echo
138 echo "Build of \"${PACKAGE}\" unnecessary. Current RPM already at " $EXPECTED_DEST_RPM_FILENAME
139 echo
140 exit 1
141 else
142 cp -a "$1" ${BUILDTEMP}/src/${PACKAGE}
143 cd ${BUILDTEMP}/src/${PACKAGE}; rpm2cpio $1 | cpio -i
144 fi
145else
146 cp -a $SPECFILE ${BUILDTEMP}/src/${PACKAGE}
147
148 # uses Subversion (svn) to generate more information for the RPM
149 if [ "$BUILD_USES_SVN" = "yes" ]; then
150 echo "receiving subversion information ..."
151 # set RELEASE number (last modification in this directory)
152 # requires subversion (but connection to server isn't neccessary)
153 SVN_BUILD_RELEASE=$(unset LANG; svn info . | sed -n 's/^Last Changed Rev: \([0-9]*\)/\1/p')
154 # in case of local modification a "m" is added to the release number
155 SVN_BUILD_MODIFIED=$(svn status -v | grep "^ *M" | wc -l)
156 # checks if a file is commited,
157 # but update on its directory is not performed
158 SVN_LAST_MODIFICATION=$(svn status -v | sed -n 's/^ *[0-9]* *\([0-9]*\) .*/\1/p' | sort -n | tail -1)
159 if [ $SVN_BUILD_MODIFIED -gt 0 ]; then
160 SVN_BUILD_RELEASE=${SVN_BUILD_RELEASE}m
161 elif [ $SVN_LAST_MODIFICATION -ne $SVN_BUILD_RELEASE ]; then
162 SVN_BUILD_RELEASE=${SVN_BUILD_RELEASE}p
163 else # check if RPM has been build already
164 EXPECTED_DEST_RPM_FILENAME=${DEST_DIR}/${BUILD_DIST}/${PACKAGE}-${VERSION}-${SVN_BUILD_RELEASE}.*.rpm
165 if [ -f $EXPECTED_DEST_RPM_FILENAME ]; then
166 echo
167 echo "Build of \"${PACKAGE}\" unnecessary. Current RPM already at " $EXPECTED_DEST_RPM_FILENAME
168 echo
169 exit 1
170 fi
171 fi
172 if [ "$SVN_BUILD_RELEASE" ]; then
173 sed "s/^Release:.*/Release: $SVN_BUILD_RELEASE/ig" $SPECFILE > ${BUILDTEMP}/src/${PACKAGE}/$SPECFILE
174 fi
175 echo "SVN_BUILD_RELEASE: $SVN_BUILD_RELEASE"
176
177 # changelog header
178 DATE=`unset LANG; date +"%a %b %d %Y"`
179 echo -e "* $DATE -\n" > Changes
180 echo "- Subversion status:" >> Changes
181 # additional infos for the Changes file (see if everything is checked in)
182 svn status -v | grep -v '^? ' >> Changes
183 echo >> Changes
184 echo "- ChangeLog:" >> Changes
185 # requires connection to the subversion server
186 svn log -v . > ChangeLog.new && mv ChangeLog.new ChangeLog
187 cat ChangeLog >> Changes
188
189 # if there is no changelog section in the spec file,
190 # also append the Changes there
191 if ! grep -q -i "%changelog" $SPECFILE ; then
192 # paste "Changes" into SPEC file.
193 echo "%changelog" >> ${BUILDTEMP}/src/${PACKAGE}/$SPECFILE
194 # without trailing *, except for the initial
195 echo -n "*" >> ${BUILDTEMP}/src/${PACKAGE}/$SPECFILE
196 cat Changes | sed 's/\(^[\*]\+\)//g' >> ${BUILDTEMP}/src/${PACKAGE}/$SPECFILE
197 fi
198 fi
199 # just make sure, that the Changes file exist
200 touch Changes
201
202 # if "SOURCES" file exist, create tar file on the fly
203 if [ -r "SOURCES" ]; then
204 # create a tar file from the sources
205 tar cvjf ${BUILDTEMP}/src/${PACKAGE}/$PACKAGE-$VERSION.tar.bz2 --files-from SOURCES --exclude .svn --exclude "*~"
206
207 elif [ -r "SPECSOURCES" ]; then
208 # SPECSOURCES has to list the files
209 # that are referenced in the .spec file
210 # these files are copied to the build environment
211 cp -av `cat SPECSOURCES` ${BUILDTEMP}/src/${PACKAGE}
212
213 else
214 # legacy method:
215 # call the existing Makefile
216 # to generate a tar file
217 test -e Makefile && make
218 TARFILE=$(ls *.tar.[gb]z*| awk '{print $1}')
219 cp -av $TARFILE ${BUILDTEMP}/src/${PACKAGE}
220 fi
221fi
222
223# # create buildscript
224# cat > $BUILDSCRIPT <<EOF
225# #!/bin/bash
226# # local buildscript
227# export BUILD_ROOT=$BUILD_ROOT
228# export BUILD_DIST=$BUILD_DIST
229# export BUILD_RPMS=${DEST_DIR}/${BUILD_DIST}:$BUILD_RPMS
230# export BUILD_PREPARE_SPEC=$BUILD_PREPARE_SPEC
231#
232# cd ${BUILDTEMP}/src/${PACKAGE}
233# $BUILD_CMD $BUILD_MODE
234# EOF
235#
236# chmod a+x $BUILDSCRIPT
237# sudo /bin/su -c $BUILDSCRIPT
238# rm -f $BUILDSCRIPT
239
240# create file for buildvars (see $BUILDSCRIPT)
241cat > $BUILDVARS <<EOF
242# Variables for build-Prozess (export is needed in buildscript)
243BUILD_ROOT=$BUILD_ROOT
244BUILD_DIST=$BUILD_DIST
245BUILD_RPMS=${DEST_DIR}/${BUILD_DIST}:$BUILD_RPMS
246
247# Variables for execute the build-proggy
248BUILD_MODE="$BUILD_MODE"
249BUILD_PLACE=${BUILDTEMP}/src/${PACKAGE}
250EXTRA_RPMS="$EXTRA_RPMS"
251EOF
252
253sudo $BUILDSCRIPT $BUILDVARS
254#rm -f $BUILDVARS
255
256
257
258mkdir -p ${DEST_DIR}/src
259# delete all old versions of this package
260for i in ${DEST_DIR}/src/${PACKAGE}-*.src.rpm; do
261 if [ -f $i -a ${PACKAGE} = "`rpm -qp --qf "%{NAME}\n" $i`" ]; then
262 rm $i
263 fi
264done
265#rm -f ${DEST_DIR}/src/${PACKAGE}-*.src.rpm
266
267if !(cp -a ${BUILD_ROOT}/usr/src/packages/SRPMS/${PACKAGE}-${VERSION}-*.src.rpm ${DEST_DIR}/src 2> /dev/null); then
268 ERROR=4
269fi
270
271mkdir -p ${DEST_DIR}/${BUILD_DIST}
272# delete all old versions of this package
273for i in ${DEST_DIR}/${BUILD_DIST}/${PACKAGE}-*.rpm; do
274 if [ -f $i -a ${PACKAGE} = "`rpm -qp --qf "%{NAME}\n" $i`" ]; then
275 rm $i
276 fi
277done
278#rm -f ${DEST_DIR}/${BUILD_DIST}/${PACKAGE}-*.rpm
279
280if !(cp -a ${BUILD_ROOT}/usr/src/packages/RPMS/*/${PACKAGE}*${VERSION}-*.*.rpm ${DEST_DIR}/${BUILD_DIST} ); then
281 ERROR=8
282fi
283
284echo
285if [ $ERROR -ne 0 ]; then
286
287 echo "ERROR while building packet $PACKAGE";
288else
289 echo "Build of \"${PACKAGE}\" completed!"
290 echo "Packages can be found in ${DEST_DIR}"
291fi
292echo
293
294exit $ERROR
Note: See TracBrowser for help on using the repository browser.