Merge branch 'master' into devel
[tools/build.git] / build-recipe-spec
1 #
2 # spec specific functions.
3 #
4 ################################################################
5 #
6 # Copyright (c) 1995-2014 SUSE Linux Products GmbH
7 #
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License version 2 or 3 as
10 # published by the Free Software Foundation.
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 (see the file COPYING); if not, write to the
19 # Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
21 #
22 ################################################################
23
24 recipe_setup_spec() {
25     TOPDIR=`chroot $BUILD_ROOT su -c "rpm --eval '%_topdir'" - $BUILD_USER`
26     if test -z "$TOPDIR"; then
27         echo "Error: TOPDIR empty"
28         cleanup_and_exit 1
29     fi
30     if [ "$NO_TOPDIR_CLEANUP" = false ]; then
31         rm -rf "$BUILD_ROOT$TOPDIR"
32         for i in BUILD RPMS/`uname -m` RPMS/i386 RPMS/noarch SOURCES SPECS SRPMS BUILDROOT OTHER ; do
33             mkdir -p $BUILD_ROOT$TOPDIR/$i
34         done
35     fi
36
37     chown -R "$ABUILD_UID:$ABUILD_GID" "$BUILD_ROOT$TOPDIR"
38     mkdir -p $BUILD_ROOT$TOPDIR/SOURCES
39     cp -p "$MYSRCDIR"/* $BUILD_ROOT$TOPDIR/SOURCES/
40 }
41
42 recipe_prepare_spec() {
43     args=()
44     if test -n "$RELEASE"; then
45         args=(--release "$RELEASE")
46     fi
47
48     # fixup specfile
49     CHANGELOGARGS=
50     test -n "$CHANGELOG" -a -f "$BUILD_ROOT/.build-changelog" && CHANGELOGARGS="--changelog $BUILD_ROOT/.build-changelog"
51     substitutedeps "${args[@]}" --root "$BUILD_ROOT" --dist "$BUILD_DIST" --archpath "$BUILD_ARCH" --configdir "$CONFIG_DIR" $CHANGELOGARGS "$BUILD_ROOT$TOPDIR/SOURCES/$RECIPEFILE" "$BUILD_ROOT/.spec.new" || cleanup_and_exit 1
52
53     # fix rpmrc if we are compiling for i686
54     test -f $BUILD_ROOT/usr/lib/rpm/rpmrc_i586 && mv $BUILD_ROOT/usr/lib/rpm/rpmrc_i586 $BUILD_ROOT/usr/lib/rpm/rpmrc
55     if test -e $BUILD_ROOT/usr/lib/rpm/rpmrc -a "$BUILD_ARCH" != "${BUILD_ARCH#i686}" ; then
56         mv $BUILD_ROOT/usr/lib/rpm/rpmrc $BUILD_ROOT/usr/lib/rpm/rpmrc_i586
57         sed -e 's/^buildarchtranslate: athlon.*/buildarchtranslate: athlon: i686/' -e 's/^buildarchtranslate: i686.*/buildarchtranslate: i686: i686/' < $BUILD_ROOT/usr/lib/rpm/rpmrc_i586 > $BUILD_ROOT/usr/lib/rpm/rpmrc
58     fi
59     if test "$DO_BUILD" = false ; then
60         cleanup_and_exit 0
61     fi
62
63     # extract macros from configuration
64     queryconfig rawmacros --dist "$BUILD_DIST" --archpath "$BUILD_ARCH" --configdir "$CONFIG_DIR" > $BUILD_ROOT/root/.rpmmacros
65     if test -n "$BUILD_DEBUG" ; then
66         echo '
67 %prep %{?!__debug_package:%{?_build_create_debug:%?_build_insert_debug_package}}%%prep
68 %package %{?!__debug_package:%{?_build_create_debug:%?_build_insert_debug_package}}%%package
69 %_build_insert_debug_package \
70 %global __debug_package 1 \
71 %undefine _enable_debug_packages \
72 %debug_package
73
74 ' >> $BUILD_ROOT/root/.rpmmacros
75     fi
76
77     if test -n "$BUILD_JOBS" ; then
78         cat >> $BUILD_ROOT/root/.rpmmacros <<-EOF
79                 %jobs $BUILD_JOBS
80                 %_smp_mflags -j$BUILD_JOBS
81                 EOF
82     fi
83     test $BUILD_USER = abuild && cp -p $BUILD_ROOT/root/.rpmmacros $BUILD_ROOT/home/abuild/.rpmmacros
84
85     # extract optflags from configuration
86     queryconfig --dist "$BUILD_DIST" --configdir "$CONFIG_DIR" --archpath "$BUILD_ARCH" optflags ${BUILD_DEBUG:+debug} > $BUILD_ROOT/root/.rpmrc
87     test $BUILD_USER = abuild && cp -p $BUILD_ROOT/root/.rpmrc $BUILD_ROOT/home/abuild/.rpmrc
88
89     if test -z "$ABUILD_TARGET"; then
90         ABUILD_TARGET=$(queryconfig target --dist "$BUILD_DIST" --configdir "$CONFIG_DIR" --archpath "$BUILD_ARCH" )
91         test -z "$ABUILD_TARGET" || echo "build target is $ABUILD_TARGET"
92     fi
93
94     # report specfile changes
95     if test -f $BUILD_ROOT/.spec.new ; then
96         if ! cmp -s $BUILD_ROOT$TOPDIR/SOURCES/$RECIPEFILE $BUILD_ROOT/.spec.new ; then
97             echo -----------------------------------------------------------------
98             echo "I have the following modifications for $RECIPEFILE:"
99             sed -e "/^%changelog/q" $BUILD_ROOT$TOPDIR/SOURCES/$RECIPEFILE > $BUILD_ROOT/.spec.t1
100             sed -e "/^%changelog/q" $BUILD_ROOT/.spec.new > $BUILD_ROOT/.spec.t2
101             diff $BUILD_ROOT/.spec.t1 $BUILD_ROOT/.spec.t2
102             rm -f $BUILD_ROOT/.spec.t1 $BUILD_ROOT/.spec.t2
103             mv $BUILD_ROOT/.spec.new $BUILD_ROOT$TOPDIR/SOURCES/$RECIPEFILE
104         else
105             rm -f $BUILD_ROOT/.spec.new
106         fi
107     fi
108 }
109
110 recipe_build_spec() {
111     test -z "$BUILD_RPM_BUILD_STAGE" && BUILD_RPM_BUILD_STAGE=-ba
112
113     rpmbuild=rpmbuild
114     test -x $BUILD_ROOT/usr/bin/rpmbuild || rpmbuild=rpm
115
116     # XXX: move _srcdefattr to macro file?
117     rpmbopts=("--define" "_srcdefattr (-,root,root)")
118     if test "$DO_CHECKS" != true ; then
119         rpmbopts[${#rpmbopts[@]}]="--nocheck"
120     fi
121     if test "$rpmbuild" == "rpmbuild" ; then
122             # use only --nosignature for rpm v4
123         rpmbopts[${#rpmbopts[@]}]="--nosignature"
124     fi
125     if test -n "$ABUILD_TARGET" ; then
126         rpmbopts[${#rpmbopts[@]}]="--target=$ABUILD_TARGET"
127     fi
128     if test -n "$BUILD_DEBUG" ; then
129         rpmbopts[${#rpmbopts[@]}]='--define'
130         rpmbopts[${#rpmbopts[@]}]="_build_create_debug 1"
131     fi
132     if test -n "$DISTURL" ; then
133         rpmbopts[${#rpmbopts[@]}]='--define'
134         rpmbopts[${#rpmbopts[@]}]="disturl $DISTURL"
135     fi
136     if test -n "$RSYNCDONE" ; then
137         rpmbopts[${#rpmbopts[@]}]='--define'
138         rpmbopts[${#rpmbopts[@]}]="RSYNCDONE 1"
139     fi
140
141     # su involves a shell which would require even more
142     # complicated quoting to bypass than this
143     if test "$SHORT_CIRCUIT" = false ; then
144         rpmbopts[${#rpmbopts[@]}]="$BUILD_RPM_BUILD_STAGE"
145         toshellscript $rpmbuild \
146                 "${definesnstuff[@]}" \
147                 "${rpmbopts[@]}" \
148                 "$TOPDIR/SOURCES/$RECIPEFILE" \
149                 > $BUILD_ROOT/.build.command
150     else
151         rpmbopts[${#rpmbopts[@]}]='--short-circuit'
152         buildopts="-bc -bi -bb -bs"
153         cmds=""
154         echo "#!/bin/sh -x" >$BUILD_ROOT/.build.command
155         echo "set -e" >>$BUILD_ROOT/.build.command
156         for opt in $buildopts
157         do
158             shellquote $rpmbuild  \
159                     "${definesnstuff[@]}" \
160                     "${rpmbopts[@]}" $opt \
161                     "$TOPDIR/SOURCES/$RECIPEFILE" \
162                     >> $BUILD_ROOT/.build.command
163             echo >>$BUILD_ROOT/.build.command
164             [ "$opt" == "$BUILD_RPM_BUILD_STAGE" ] && break
165         done
166     fi
167
168     chmod 755 $BUILD_ROOT/.build.command
169     check_exit
170     if test -n "$RUN_SHELL"; then
171         chroot $BUILD_ROOT su -
172     else
173         chroot $BUILD_ROOT su -c /.build.command - $BUILD_USER < /dev/null && BUILD_SUCCEEDED=true
174     fi
175 }
176
177 recipe_resultdirs_spec() {
178     echo RPMS SRPMS
179 }
180
181 recipe_unpack_srcrpm() {
182     test -n "$LIST_STATE" || echo "processing src rpm $SRCDIR/$RECIPEFILE ..."
183     MYSRCDIR="$BUILD_ROOT/.build-srcdir"
184     rm -rf "$MYSRCDIR"
185     mkdir -p "$MYSRCDIR"
186     cd $MYSRCDIR || cleanup_and_exit 1
187     $BUILD_DIR/unrpm -q $SRCDIR/$RECIPEFILE || {
188         echo "could not unpack $RECIPEFILE."
189         cleanup_and_exit 1
190     }
191     for RECIPEFILE in *.spec ; do : ; done
192 }
193
194 # post build functions... move somewhere else?
195
196 recipe_check_file_owners() {
197     echo "... checking for files with abuild user/group"
198     BADFILE=
199     while read un gn fn ; do
200         if test "$un" = abuild -o "$gn" = abuild -o "$un" = ${ABUILD_UID} -o "$gn" = ${ABUILD_GID} ; then
201             echo "  $un $gn $fn"
202             BADFILE=true
203         fi
204     done < <(rpm -qp --qf '[%{FILEUSERNAME} %{FILEGROUPNAME} %{FILENAMES}\n]' $RPMS)
205     if test -n "$BADFILE" ; then
206         echo "please fix your filelist (e.g. add defattr)"
207         cleanup_and_exit 1
208     fi
209 }
210
211 recipe_run_rpmlint() {
212     if ! test -x "$BUILD_ROOT/opt/testing/bin/rpmlint" ; then 
213         return
214     fi
215     LINT_RPM_FILE_LIST=($(find $BUILD_ROOT/$TOPDIR/RPMS \
216         \( -name "*-debuginfo-*" -o -name "*-debugsource-*" \
217         -o -name "*-32bit-*" -o -name "*-64bit-*" \
218         -o -name "*-x86-*" -o -name "*-ia32-*" \) -prune \
219         -o -type f -name '*.rpm' -print))
220     SRPM_FILE_LIST=($(find $BUILD_ROOT/$TOPDIR/SRPMS -type f -name "*.rpm"))
221     echo 
222     echo "RPMLINT report:"
223     echo "==============="
224     rpmlint_logfile=$TOPDIR/OTHER/rpmlint.log
225     rm -f "$BUILD_ROOT$rpmlint_logfile"
226     ret=0
227     mount -n -tproc none $BUILD_ROOT/proc 2> /dev/null
228     chroot $BUILD_ROOT su -s /opt/testing/bin/rpmlint "$BUILD_USER" -- \
229             --info ${LINT_RPM_FILE_LIST[*]#$BUILD_ROOT} \
230             ${SRPM_FILE_LIST[*]#$BUILD_ROOT} > "$BUILD_ROOT$rpmlint_logfile" || ret=1
231     cat "$BUILD_ROOT$rpmlint_logfile"
232     echo
233     umount -n $BUILD_ROOT/proc 2>/dev/null || true 
234     if test "$ret" = 1 ; then 
235         cleanup_and_exit 1
236     fi   
237 }
238
239 recipe_compare_oldpackages() {
240     if test -x "$BUILD_ROOT/usr/lib/build/same-build-result.sh" ; then 
241         echo "... comparing built packages with the former built"
242         mount -n -tproc none $BUILD_ROOT/proc 2> /dev/null
243         if chroot $BUILD_ROOT /usr/lib/build/same-build-result.sh /.build.oldpackages "$TOPDIR/RPMS" "$TOPDIR/SRPMS"; then 
244             chroot $BUILD_ROOT touch /.build/.same_result_marker
245             # XXX: dirty build service hack. fix bs_worker. Search for
246             # 'same_result_marker' for traces of a first try to get rid of this
247             if test -n "$REASON" -a -n "$DISTURL" ; then 
248                 exitcode=2
249             fi
250         fi
251         umount -n $BUILD_ROOT/proc 2>/dev/null || true 
252     fi   
253 }
254
255 recipe_create_deltarpms() {
256     if test -x "$BUILD_ROOT/usr/bin/makedeltarpm" -a -x $BUILD_ROOT/usr/lib/build/mkdrpms ; then
257         echo "... creating delta rpms"
258         ds=("$BUILD_ROOT/$TOPDIR"/RPMS/* "$BUILD_ROOT$TOPDIR/SRPMS")
259         chroot $BUILD_ROOT /usr/lib/build/mkdrpms /.build.oldpackages "${ds[@]#$BUILD_ROOT}"
260     fi
261 }