]> git.pld-linux.org Git - packages/rpm-build-tools.git/blame - builder.sh
- %systemdunitdir macro
[packages/rpm-build-tools.git] / builder.sh
CommitLineData
10d92f71 1#!/bin/ksh
44058243 2#
dece74ed
AM
3# This program is free software, distributed under the terms of
4# the GNU General Public License Version 2.
5#
cd445739 6# -----------
cd445739 7# Exit codes:
2a5f078d 8# 0 - succesful
9# 1 - help displayed
10# 2 - no spec file name in cmdl parameters
11# 3 - spec file not stored in repo
12# 4 - some source, patch or icon files not stored in repo
13# 5 - package build failed
14# 6 - spec file with errors
15# 7 - wrong source in /etc/poldek.conf
16# 8 - Failed installing buildrequirements and subrequirements
17# 9 - Requested tag already exist
18# 10 - Refused to build fractional release
19# 100 - Unknown error (should not happen)
cd445739 20
f8e50be5 21# Notes (todo/bugs):
f8e50be5
ER
22# - when Icon: field is present, -5 and -a5 doesn't work
23# - builder -R skips installing BR if spec is not present before builder invocation (need to run builder twice)
19aa2571 24# - does not respect NoSource: X, and tries to cvs up such files [ example: VirtualBox-bin.spec and its Source0 ]
f8e50be5
ER
25# TODO:
26# - ability to do ./builder -bb foo.spec foo2.spec foo3.spec
1ffcdaff
ER
27# - funny bug, if source-md5 is set then builder will download from distfiles even if there is no url present:
28# Source10: forwardfix.pl
29# # Source10-md5: 8bf85f7368933a4e0cb4f875bac28733
6ca6f876
ZU
30# - builder --help:
31# basename: missing operand
32# Try `basename --help' for more information.
33# -- and the normal usage info --
34
9d99a240
ER
35PROGRAM=${0##*/}
36APPDIR=$(d=$0; [ -L "$d" ] && d=$(readlink -f "$d"); dirname "$d")
37RCSID='$Id$' r=${RCSID#* * } rev=${r%% *}
b0f031e7 38VERSION="v0.35/$rev"
d43959a2 39VERSIONSTRING="\
b71f5024
ER
40Build package utility from PLD Linux Packages repository
41$VERSION (C) 1999-2011 Free Penguins".
d43959a2 42
d6a77ddb 43PATH="/bin:/usr/bin:/usr/sbin:/sbin:/usr/X11R6/bin"
b03f053b 44
b7cc0c94
ER
45# required rpm-build-macros
46RPM_MACROS_VER=1.534
47
cd445739 48COMMAND="build"
64ea5308 49TARGET=""
cd445739 50
bde1c404 51SPECFILE=""
d287305c 52BE_VERBOSE=""
4003ad8c 53QUIET=""
cd445739
AM
54CLEAN=""
55DEBUG=""
56NOURLS=""
57NOCVS=""
58NOCVSSPEC=""
59NODIST=""
47761337 60NOINIT=""
bc10f694 61PREFMIRRORS=""
cd445739 62UPDATE=""
cd445739
AM
63ADD5=""
64NO5=""
65ALWAYS_CVSUP=${ALWAYS_CVSUP:-"yes"}
c8f50498 66CVSROOT=""
cdbd30e8 67GREEDSRC=""
cd445739 68
6c1b2e7d
PZ
69# use rpm 4.4.6+ digest format instead of comments if non-zero
70USEDIGEST=
71
e3aa6f5e
ER
72# user agent when fetching files
73USER_AGENT="PLD/Builder($VERSION)"
74
cd445739
AM
75# It can be used i.e. in log file naming.
76# See LOGFILE example.
77DATE=`date +%Y-%m-%d_%H-%M-%S`
78
79# Example: LOGFILE='../log.$PACKAGE_NAME'
80# Example: LOGFILE='../LOGS/log.$PACKAGE_NAME.$DATE'
d280fd97 81# Example: LOGFILE='$PACKAGE_NAME/$PACKAGE_NAME.$DATE.log'
f5687002 82# Example: LOGFILE='$PACKAGE_NAME.$DATE.log'
cd445739
AM
83# Yes, you can use variable name! Note _single_ quotes!
84LOGFILE=''
85
86LOGDIR=""
87LOGDIROK=""
88LOGDIRFAIL=""
89LASTLOG_FILE=""
90
91CHMOD="no"
1da227e8 92CHMOD_MODE="0644"
cd445739 93RPMOPTS=""
d1c5a9a7 94RPMBUILDOPTS=""
cd445739
AM
95BCOND=""
96GROUP_BCONDS="no"
ccffa567
ER
97
98# create symlinks for tools in PACKAGE_DIR, see get_spec()
2dd0af86 99SYMLINK_TOOLS="no"
d287305c 100
101PATCHES=""
102SOURCES=""
cd445739 103ICONS=""
d287305c 104PACKAGE_RELEASE=""
105PACKAGE_VERSION=""
106PACKAGE_NAME=""
14bead22 107ASSUMED_NAME=""
fd71d65c 108PROTOCOL="http"
2060f498
AM
109
110# use lftp by default when available
f4bac425 111USE_LFTP=
2060f498
AM
112lftp --version > /dev/null 2>&1 && USE_LFTP=yes
113
cd445739 114WGET_RETRIES=${MAX_WGET_RETRIES:-0}
cd445739 115
db5831da 116CVS_COMMAND=${CVS_COMMAND:-cvs}
939f2c12 117CVS_FORCE=""
f123bcb5 118CVSIGNORE_DF="yes"
939f2c12
ER
119CVS_RETRIES=${MAX_CVS_RETRIES:-1000}
120CVS_SERVER="cvs.pld-linux.org"
cd445739 121CVSTAG=""
939f2c12 122
cd445739
AM
123RES_FILE=""
124
cd445739 125DISTFILES_SERVER="://distfiles.pld-linux.org"
d4730dae 126ATTICDISTFILES_SERVER="://attic-distfiles.pld-linux.org"
cd445739 127
36d03934 128DEF_NICE_LEVEL=19
86aafdbe 129SCHEDTOOL="auto"
cd445739
AM
130
131FAIL_IF_NO_SOURCES="yes"
132
18e5347d
ER
133# let get_files skip over files which are present to get those damn files fetched
134SKIP_EXISTING_FILES="no"
135
975d7b23 136TRY_UPGRADE=""
257af81b
ER
137# should the specfile be restored if upgrade failed?
138REVERT_BROKEN_UPGRADE="yes"
139
90baf90b
ER
140if rpm --specsrpm 2>/dev/null; then
141 FETCH_BUILD_REQUIRES_RPMSPECSRPM="yes"
ce40491a 142 FETCH_BUILD_REQUIRES_RPMGETDEPS="no"
90baf90b
ER
143else
144 FETCH_BUILD_REQUIRES_RPMSPECSRPM="no"
145 if [ -x /usr/bin/rpm-getdeps ]; then
146 FETCH_BUILD_REQUIRES_RPMGETDEPS="yes"
147 else
148 FETCH_BUILD_REQUIRES_RPMGETDEPS="no"
149 fi
ab4a2b6e
ER
150fi
151
90baf90b 152
cd445739
AM
153# Here we load saved user environment used to
154# predefine options set above, or passed to builder
155# in command line.
156# This one reads global system environment settings:
157if [ -f ~/etc/builderrc ]; then
158 . ~/etc/builderrc
159fi
160# And this one cascades settings using user personal
161# builder settings.
162# Example of ~/.builderrc:
163#
164#UPDATE_POLDEK_INDEXES="yes"
165#FETCH_BUILD_REQUIRES="yes"
166#REMOVE_BUILD_REQUIRES="force"
167#GROUP_BCONDS="yes"
168#LOGFILE='../LOGS/log.$PACKAGE_NAME.$DATE'
982e358c 169#TITLECHANGE=no
cd445739 170#
a83e5405 171SU_SUDO=""
cd445739
AM
172if [ -n "$HOME_ETC" ]; then
173 USER_CFG="$HOME_ETC/.builderrc"
20117088 174 BUILDER_MACROS="$HOME_ETC/.builder-rpmmacros"
cd445739
AM
175else
176 USER_CFG=~/.builderrc
20117088 177 BUILDER_MACROS=~/.builder-rpmmacros
cd445739
AM
178fi
179
180[ -f "$USER_CFG" ] && . "$USER_CFG"
181
86aafdbe
ER
182if [ "$SCHEDTOOL" = "auto" ]; then
183 if [ -x /usr/bin/schedtool ] && schedtool -B -e echo >/dev/null; then
184 SCHEDTOOL="schedtool -B -e"
185 else
186 SCHEDTOOL="no"
187 fi
188fi
189
ca2e6c31 190if [ -n "$USE_PROZILLA" ]; then
7e365483
KK
191 GETURI="proz --no-getch -r -P ./ -t$WGET_RETRIES $PROZILLA_OPTS"
192 GETURI2="$GETURI"
193 OUTFILEOPT="-O"
ca2e6c31 194elif [ -n "$USE_AXEL" ]; then
cede92b3 195 GETURI="axel -a $AXEL_OPTS"
7e365483
KK
196 GETURI2="$GETURI"
197 OUTFILEOPT="-o"
9639be96 198elif [ -n "$USE_LFTP" ]; then
9639be96
AM
199 GETURI=download_lftp
200 GETURI2=$GETURI
201 OUTFILEOPT=""
ca2e6c31 202else
5700ee32
ER
203 wget --help 2>&1 | grep -q -- ' --no-check-certificate ' && WGET_OPTS="$WGET_OPTS --no-check-certificate"
204 wget --help 2>&1 | grep -q -- ' --inet ' && WGET_OPTS="$WGET_OPTS --inet"
205 wget --help 2>&1 | grep -q -- ' --retry-connrefused ' && WGET_OPTS="$WGET_OPTS --retry-connrefused"
e3aa6f5e 206 WGET_OPTS="$WGET_OPTS --user-agent=$USER_AGENT"
6f9a5d76 207
7e365483
KK
208 GETURI="wget --passive-ftp -c -nd -t$WGET_RETRIES $WGET_OPTS"
209 GETURI2="wget -c -nd -t$WGET_RETRIES $WGET_OPTS"
210 OUTFILEOPT="-O"
2bc5451f 211fi
11468b23 212
11468b23
AM
213GETLOCAL="cp -a"
214
15e34b63 215if rpm --version 2>&1 | grep -q '4.0.[0-2]'; then
11468b23
AM
216 RPM="rpm"
217 RPMBUILD="rpm"
218else
219 RPM="rpm"
220 RPMBUILD="rpmbuild"
221fi
222
c86939e2 223#
224# sanity checks
225#
226if [ -d $HOME/rpm/SOURCES ]; then
227 echo "ERROR: ~/rpm/{SPECS,SOURCES} structure is obsolete" >&2
228 echo "ERROR: get rid of your ~/rpm/SOURCES" >&2
229 exit 1
230fi
231
28c23a72
AG
232#
233# are we using cvs-nserver ?
234#
e21039da 235CVS_NSERVER=0
939f2c12 236$CVS_COMMAND --version 2>&1 | grep -q 'CVS-nserver'
e21039da 237[ $? -eq 0 ] && CVS_NSERVER=1
28c23a72 238
a8c9a155 239POLDEK_INDEX_DIR="$($RPM --eval %_rpmdir)/"
a83e5405 240POLDEK_CMD="$SU_SUDO /usr/bin/poldek --noask"
11468b23 241
f6711e2a 242run_poldek() {
390d081d 243 RES_FILE=$(tempfile)
cd445739
AM
244 if [ -n "$LOGFILE" ]; then
245 LOG=`eval echo $LOGFILE`
246 if [ -n "$LASTLOG_FILE" ]; then
247 echo "LASTLOG=$LOG" > $LASTLOG_FILE
248 fi
86aafdbe 249 (${NICE_COMMAND} ${POLDEK_CMD} `while test $# -gt 0; do echo "$1 ";shift;done` ; echo $? > ${RES_FILE})|tee -a $LOG
cd445739
AM
250 return $exit_pldk
251 else
86aafdbe 252 (${NICE_COMMAND} ${POLDEK_CMD} `while test $# -gt 0; do echo "$1 ";shift;done` ; echo $? > ${RES_FILE}) 1>&2 >/dev/null
cd445739
AM
253 return `cat ${RES_FILE}`
254 rm -rf ${RES_FILE}
255 fi
256}
257
cd445739
AM
258#---------------------------------------------
259# functions
da946cd6 260
f4bac425 261download_lftp() {
6928bb29 262 local outfile=$1 url=$2 retval tmpfile
f4bac425
ER
263 # TODO: use mktemp
264 tmpfile=$outfile.tmp
4d1fc6ce
AM
265 lftp -c "
266 $([ "$DEBUG" = "yes" ] && echo "debug 5;")
267 set net:max-retries $WGET_RETRIES;
268 set http:user-agent \"$USER_AGENT\";
269 pget -n 10 -c \"$url\" -o \"$tmpfile\"
270 "
f4bac425
ER
271
272 retval=$?
273 if [ $retval -eq 0 ]; then
274 mv -f "$tmpfile" "$outfile"
275 else
276 rm -f "$tmpfile"
277 fi
278 return $retval
279}
280
f6711e2a 281usage() {
cd445739
AM
282 if [ -n "$DEBUG" ]; then set -xv; fi
283 echo "\
db9fdf51 284Usage: builder [-D|--debug] [-V|--version] [--short-version] [--as_anon] [-a|--add_cvs] [-b|-ba|--build]
6594293d 285[-bb|--build-binary] [-bs|--build-source] [-bc] [-bi] [-bl] [-u|--try-upgrade]
4efa0b98 286[{-cf|--cvs-force}] [{-B|--branch} <branch>] [{-d|--cvsroot} <cvsroot>]
fd71d65c 287[-g|--get] [-h|--help] [--ftp] [--http] [{-l|--logtofile} <logfile>] [-m|--mr-proper]
19dc5591 288[-q|--quiet] [--date <yyyy-mm-dd> [-r <cvstag>] [{-T|--tag <cvstag>]
a7b2951c 289[-Tvs|--tag-version-stable] [-Ts|--tag-stable] [-Tv|--tag-version]
f8dba812 290[{-Tp|--tag-prefix} <prefix>] [{-tt|--test-tag}] [--use-greed-sources]
6594293d 291[-nu|--no-urls] [-v|--verbose] [--opts <rpm opts>] [--short-circuit]
3009b80d 292[--show-bconds] [--with/--without <feature>] [--define <macro> <value>]
6594293d 293<package>[.spec][:cvstag]
2a5f078d 294
5efcb865 295-4 - force ipv4 when transferring files
cd445739 296-5, --update-md5 - update md5 comments in spec, implies -nd -ncs
5efcb865 297-6 - force ipv6 when transferring files
cd445739
AM
298-a5, --add-md5 - add md5 comments to URL sources, implies -nc -nd -ncs
299-n5, --no-md5 - ignore md5 comments in spec
9243e80b
ER
300-D, --debug - enable builder script debugging mode,
301-debug - produce rpm debug package (same as --opts -debug)
d43959a2
ER
302-V, --version - output builder version string
303--short-version - output builder short version
db9fdf51
ER
304--as_anon - get files via pserver as cvs@$CVS_SERVER,
305-a, --add_cvs - try add new package to CVS.
cd445739
AM
306-b, -ba, --build - get all files from CVS repo or HTTP/FTP and build package
307 from <package>.spec,
308-bb, --build-binary - get all files from CVS repo or HTTP/FTP and build binary
309 only package from <package>.spec,
c56807e0 310-bp, --build-prep - execute the %prep phase of <package>.spec,
6594293d
AG
311-bc - execute the %build phase of <package>.spec,
312-bi - execute the %install phase of <package>.spec
b05205af 313-bl - execute the %files phase of <package>.spec
cd445739
AM
314-bs, --build-source - get all files from CVS repo or HTTP/FTP and only pack
315 them into src.rpm,
6594293d 316--short-circuit - short-circuit build
cd445739 317-B, --branch - add branch
387eaf99
ER
318-c, --clean - clean all temporarily created files (in BUILD\$RPM_BUILD_ROOT) after rpmbuild commands.
319 may be used with building process.
b05205af
BZ
320-m, --mr-proper - clean all temporarily created files (in BUILD, SOURCES, SPECS and \$RPM_BUILD_ROOT
321 and CVS/Entries). Doesn't run any rpm building.
322-cf, --cvs-force - use -F when tagging (useful when moving branches)
2a5f078d 323-d <cvsroot>, --cvsroot <cvsroot>
cd445739 324 - setup \$CVSROOT,
b05205af
BZ
325--define <macro> <value> - define a macro <macro> with value <value>,
326--alt_kernel <kernel> - same as --define 'alt_kernel <kernel>'
cd445739 327--nodeps - rpm won't check any dependences
b05205af
BZ
328-g, --get - get <package>.spec and all related files from CVS repo or HTTP/FTP
329-h, --help - this message
6c5362e5 330-jN, -j N - set %_smp_mflags to propagate concurrent jobs
fd71d65c 331--ftp, --http - use ftp or http protocol to access distfiles server
cd445739
AM
332-l <logfile>, --logtofile <logfile>
333 - log all to file,
cd445739 334-nc, --no-cvs - don't download sources from CVS, if source URL is given,
b05205af 335-ncs, --no-cvs-specs - don't check specs in CVS
cd445739
AM
336-nd, --no-distfiles - don't download from distfiles
337-nm, --no-mirrors - don't download from mirror, if source URL is given,
338-nu, --no-urls - don't try to download from FTP/HTTP location,
7cbc2d88 339-ns, --no-srcs - don't download Sources/Patches
cd445739
AM
340-ns0, --no-source0 - don't download Source0
341-nn, --no-net - don't download anything from the net
a93e8cdf 342-pm, --prefer-mirrors - prefer mirrors (if any) over distfiles for SOURCES
b572253e 343--no-init - don't initialize builder paths (SPECS and SOURCES)
18e5347d
ER
344-ske,
345--skip-existing-files - skip existing files in get_files
cd445739
AM
346--opts <rpm opts> - additional options for rpm
347-q, --quiet - be quiet,
348--date yyyy-mm-dd - build package using resources from specified CVS date,
349-r <cvstag>, --cvstag <cvstag>
350 - build package using resources from specified CVS tag,
5323fffd 351-A - build package using CVS resources as any sticky tags/date/kopts being reset.
cd445739
AM
352-R, --fetch-build-requires
353 - fetch what is BuildRequired,
354-RB, --remove-build-requires
355 - remove all you fetched with -R or --fetch-build-requires
356 remember, this option requires confirmation,
357-FRB, --force-remove-build-requires
358 - remove all you fetched with -R or --fetch-build-requires
359 remember, this option works without confirmation,
06541433
JR
360-sd, --source-distfiles - list sources available from distfiles (intended for offline
361 operations; does not work when Icon field is present
362 but icon file is absent),
a6494cd4 363-sc, --source-cvs - list sources available from CVS
06541433
JR
364-sdp, --source-distfiles-paths - list sources available from distfiles -
365 paths relative to distfiles directory (intended for offline
366 operations; does not work when Icon field is present
367 but icon file is absent),
a7eefc54 368-sf, --source-files - list sources - bare filenames (intended for offline
369 operations; does not work when Icon field is present
3621e588 370 but icon file is absent),
8bd5e66d 371-lsp, --source-paths - list sources - filenames with full local paths (intended for
a7eefc54 372 offline operations; does not work when Icon field is present
3621e588 373 but icon file is absent),
2dddf439
ER
374-su, --source-urls - list urls - urls to sources and patches
375 intended for copying urls with spec with lots of macros in urls
cd445739
AM
376-T <cvstag> , --tag <cvstag>
377 - add cvs tag <cvstag> for files,
378-Tvs, --tag-version-stable
094bd95b 379 - add cvs tags STABLE and NAME-VERSION-RELEASE for files,
b05205af
BZ
380-Ts, --tag-stable - add cvs tag STABLE for files,
381-Tv, --tag-version - add cvs tag NAME-VERSION-RELEASE for files,
cd445739
AM
382-Tp, --tag-prefix <prefix>
383 - add <prefix> to NAME-VERSION-RELEASE tags,
384-tt, --test-tag <prefix>
385 - fail if tag is already present,
50321881
JK
386-ir, --integer-release-only
387 - allow only integer and snapshot releases
cd445739
AM
388-v, --verbose - be verbose,
389-u, --try-upgrade - check version, and try to upgrade package
390-un, --try-upgrade-with-float-version
391 - as above, but allow float version
a6494cd4 392 php-pear-Services_Digg/
0907f1eb 393--upgrade-version - upgrade to specified version in try-upgrade
f8dba812 394--use-greed-sources
395 - try download source from tag head if don't find it in
f5141bbc 396 current tag
deccc50e 397-U, --update - refetch sources, don't use distfiles, and update md5 comments
cd445739
AM
398-Upi, --update-poldek-indexes
399 - refresh or make poldek package index files.
8bd5e66d
ER
400-sp, --skip-patch <patchnumber>
401 - don't apply <patchnumber>. may be repeated.
c890b916 402-np, --nopatch <patchnumber>
8bd5e66d 403 - abort instead of applying patch <patchnumber>
dfe2cb9a
MK
404--show-bconds - show available conditional builds, which can be used
405 - with --with and/or --without switches.
b05205af
BZ
406--show-bcond-args - show active bconds, from ~/.bcondrc. this is used by ./repackage.sh script.
407 In other words, the output is parseable by scripts.
24a97174 408--show-avail-bconds - show available bconds
cd445739
AM
409--with/--without <feature>
410 - conditional build package depending on %_with_<feature>/
411 %_without_<feature> macro switch. You may now use
412 --with feat1 feat2 feat3 --without feat4 feat5 --with feat6
413 constructions. Set GROUP_BCONDS to yes to make use of it.
6dd19291 414--target <platform>, --target=<platform>
a6494cd4
ER
415 - build for platform <platform>.
416--init-rpm-dir - initialize ~/rpm directory structure
5a491465 417"
418}
419
390d081d
ER
420# create tempfile. as secure as possible
421tempfile() {
422 mktemp -t builder.XXXXXX || ${TMPDIR:-/tmp}/builder.$RANDOM.$$
423}
424
31d2bd0b
ER
425# change dependency to specname
426# common changes:
427# - perl(Package::Name) -> perl-Package-Name
428depspecname() {
429 local package="$1"
430
560c4b1a 431 package=$(echo "$package" | sed -e '/perl(.*)/{s,perl(\(.*\)),perl-\1,;s,::,-,g};' -e 's/-\(devel\|static\)$//' )
31d2bd0b
ER
432 echo "$package"
433}
434
c890b916 435update_shell_title() {
a0d6396b 436 [ -t 1 ] || return
0b65d15e 437 local len=${COLUMNS:-80}
5c795f16 438 local msg="$(echo "$*" | cut -c-$len)"
0b65d15e 439
e225de91
ER
440 if [ -n "$BE_VERBOSE" ]; then
441 echo >&2 "$(date +%s.%N) $*"
442 fi
443
5c795f16 444 if [ "x$TITLECHANGE" = "xyes" -o "x$TITLECHANGE" = "x" ]; then
138bbc98
ER
445 local pkg
446 if [ -n "$PACKAGE_NAME" ]; then
447 pkg=${PACKAGE_NAME}-${PACKAGE_VERSION}-${PACKAGE_RELEASE}
448 else
449 pkg=${SPECFILE}
450 fi
451
452 msg="$pkg: ${SHELL_TITLE_PREFIX:+$SHELL_TITLE_PREFIX }$msg"
a8c9a155 453 msg=$(echo $msg | tr -d '\n\r')
982e358c
MP
454 case "$TERM" in
455 cygwin|xterm*)
456 echo >&2 -ne "\033]1;$msg\007\033]2;$msg\007"
457 ;;
458 screen*)
459 echo >&2 -ne "\033]0;$msg\007"
460 ;;
461 esac
462 fi
c890b916
ER
463}
464
7e40520f
ER
465# set TARGET from BuildArch: from SPECFILE
466set_spec_target() {
ce40491a 467 if [ -n "$SPECFILE" ] && [ -z "$TARGET" ]; then
b45a3b2a 468 tmp=$(awk '/^BuildArch:/ { print $NF; exit }' $ASSUMED_NAME/$SPECFILE)
ce40491a 469 if [ "$tmp" ]; then
f9109a96 470 target_platform=$(rpm -E '%{_target_vendor}-%{_target_os}%{?_gnu}')
7e40520f
ER
471 TARGET="$tmp"
472 case "$RPMBUILD" in
473 "rpmbuild")
f9109a96 474 TARGET_SWITCH="--target ${TARGET}-${target_platform}" ;;
7e40520f 475 "rpm")
ce40491a 476 TARGET_SWITCH="--target=$TARGET" ;;
7e40520f 477 esac
ce40491a
ER
478 fi
479 fi
7e40520f
ER
480}
481
ace3fd80
ER
482# runs rpm with minimal macroset
483minirpm() {
fa4bf1df 484 # we reset macros not to contain macros.build as all the %() macros are
559d511f 485 # executed here, while none of them are actually needed.
559d511f 486 # at the time of this writing macros.build + macros contained 70 "%(...)" macros.
525bf240 487 safe_macrofiles=$(rpm $TARGET_SWITCH --showrc | awk -F: '/^macrofiles/ { gsub(/^macrofiles[ \t]+:/, "", $0); gsub(/:.*macros.build:/, ":", $0); print $0 } ')
ace3fd80
ER
488
489 # TODO: move these to /usr/lib/rpm/macros
20117088 490 cat > $BUILDER_MACROS <<'EOF'
5b5e5f7f 491%x8664 x86_64 amd64 ia32e
f78a0bb7 492%alt_kernel %{nil}
493%_alt_kernel %{nil}
5ff15c97 494%bootstrap_release() %{1}
407b204b
ER
495%requires_releq_kernel_up(s:n:) %{nil}
496%requires_releq_kernel_smp(s:n:) %{nil}
497%requires_releq_kernel(s:n:) %{nil}
6b8134e3 498%requires_releq() %{nil}
bb01dee9 499%pyrequires_eq() %{nil}
559d511f 500%requires_eq() %{nil}
c13be3d1 501%requires_eq_to() %{nil}
99c05e13 502%requires_ge() %{nil}
500b0eb8
ER
503%requires_ge_to() %{nil}
504%requires_ge() %{nil}
407b204b
ER
505%releq_kernel_up(n:) ERROR
506%releq_kernel_smp(n:) ERROR
507%releq_kernel(n:) ERROR
74d9fd14 508%kgcc_package ERROR
237bd3f1 509%_fontsdir ERROR
7141278d 510%ruby_version ERROR
511%ruby_ver_requires_eq() %{nil}
512%ruby_mod_ver_requires_eq() %{nil}
c13be3d1 513%__php_api_requires() %{nil}
7141278d 514%php_major_version ERROR
515%php_api_version ERROR
99f819cf
AM
516%requires_xorg_xserver_extension %{nil}
517%requires_xorg_xserver_xinput %{nil}
518%requires_xorg_xserver_font %{nil}
519%requires_xorg_xserver_videodrv %{nil}
7141278d 520%py_ver ERROR
41035426
ER
521%perl_vendorarch ERROR
522%perl_vendorlib ERROR
703ae20b
ER
523# damn. need it here! - copied from /usr/lib/rpm/macros.build
524%tmpdir %(echo "${TMPDIR:-/tmp}")
1f28fcb7
ER
525%patchset_source(f:b:) %(
526 base=%{-b*}%{!-b*:10000};
527 start=$(expr $base + %1);
528 end=$(expr $base + %{?2}%{!?2:%{1}});
529 # we need to call seq twice as it doesn't allow two formats
530 seq -f 'Patch%g:' $start $end > %{tmpdir}/__ps1;
531 seq -f '%{-f*}' %1 %{?2}%{!?2:%{1}} > %{tmpdir}/__ps2;
532 paste %{tmpdir}/__ps{1,2};
533 rm -f %{tmpdir}/__ps{1,2};
534) \
535%{nil}
991f09a5
ER
536%add_etc_shells(p) %{p:<lua>}
537%remove_etc_shells(p) %{p:<lua>}
709995c4
ER
538%lua_add_etc_shells() %{nil}
539%lua_remove_etc_shells() %{nil}
0ca65fa4
JK
540%required_jdk %{nil}
541%buildrequires_jdk %{nil}
f9063abe 542%pear_package_print_optionalpackages %{nil}
fb198857 543EOF
3d12d055 544 if [ "$NOINIT" = "yes" ] ; then
20117088 545 cat >> $BUILDER_MACROS <<'EOF'
3d12d055
JR
546%_specdir ./
547%_sourcedir ./
548EOF
c039643c 549 fi
fff1b991 550 eval $RPMBUILD $TARGET_SWITCH --macros "$safe_macrofiles:$BUILDER_MACROS" $QUIET $RPMOPTS $RPMBUILDOPTS $BCOND $* 2>&1
ace3fd80
ER
551}
552
553cache_rpm_dump() {
554 if [ -n "$DEBUG" ]; then
555 set -x
556 set -v
557 fi
558
d1d24c14 559 if [ -x /usr/bin/rpm-specdump ]; then
525bf240 560 update_shell_title "cache_rpm_dump using rpm-specdump command"
fff1b991 561 rpm_dump_cache=$(rpm-specdump $TARGET_SWITCH $BCOND $SPECFILE)
d1d24c14 562 else
525bf240 563 update_shell_title "cache_rpm_dump using rpmbuild command"
d1d24c14
ER
564 local rpm_dump
565 rpm_dump=`
566 # what we need from dump is NAME, VERSION, RELEASE and PATCHES/SOURCES.
567 dump='%{echo:dummy: PACKAGE_NAME %{name} }%dump'
568 case "$RPMBUILD" in
569 rpm)
570 ARGS='-bp'
571 ;;
572 rpmbuild)
573 ARGS='--nodigest --nosignature --nobuild'
574 ;;
575 esac
576 minirpm $ARGS --define "'prep $dump'" --nodeps $SPECFILE
577 `
578 if [ $? -gt 0 ]; then
579 error=$(echo "$rpm_dump" | sed -ne '/^error:/,$p')
580 echo "$error" >&2
581 Exit_error err_build_fail
582 fi
d05e8080 583
d1d24c14
ER
584 # make small dump cache
585 rpm_dump_cache=`echo "$rpm_dump" | awk '
586 $2 ~ /^SOURCEURL/ {print}
587 $2 ~ /^PATCHURL/ {print}
588 $2 ~ /^nosource/ {print}
589 $2 ~ /^PACKAGE_/ {print}
590 '`
591 fi
d05e8080 592
13974367 593 update_shell_title "cache_rpm_dump: OK!"
cd445739
AM
594}
595
ace3fd80 596rpm_dump() {
cd445739 597 if [ -z "$rpm_dump_cache" ] ; then
45e2a818 598 echo >&2 "internal error: cache_rpm_dump not called! (missing %prep?)"
cd445739
AM
599 fi
600 echo "$rpm_dump_cache"
601}
602
f6711e2a 603get_icons() {
d05e8080 604 update_shell_title "get icons"
9d99a240 605 ICONS=$(awk '/^Icon:/ {print $2}' $PACKAGE_DIR/${SPECFILE})
d05e8080
ER
606 if [ -z "$ICONS" ]; then
607 return
608 fi
609
975d7b23 610 rpm_dump_cache="kalasaba" NODIST="yes" get_files $ICONS
d05e8080
ER
611}
612
f6711e2a 613parse_spec() {
af4d5315 614 update_shell_title "parsing specfile"
cd445739 615 if [ -n "$DEBUG" ]; then
518ff1fb
ER
616 set -x
617 set -v
cd445739 618 fi
b2975fc3 619
d05e8080 620 # icons are needed for successful spec parse
518ff1fb 621 get_icons
d05e8080 622
9c909460 623 cd $PACKAGE_DIR
cd445739 624 cache_rpm_dump
00fcec7e 625
15e34b63 626 if rpm_dump | grep -qEi ":.*nosource.*1"; then
cd445739
AM
627 FAIL_IF_NO_SOURCES="no"
628 fi
629
7cbc2d88
ER
630 if [ "$NOSRCS" != "yes" ]; then
631 SOURCES=$(rpm_dump | awk '$2 ~ /^SOURCEURL[0-9]+/ {print substr($2, length("SOURCEURL") + 1), $3}' | LC_ALL=C sort -n | awk '{print $2}')
632 PATCHES=$(rpm_dump | awk '$2 ~ /^PATCHURL[0-9]+/ {print substr($2, length("PATCHURL") + 1), $3}' | LC_ALL=C sort -n | awk '{print $2}')
633 ICONS=$(awk '/^Icon:/ {print $2}' ${SPECFILE})
634 fi
635
dfe2b2d9
ER
636 PACKAGE_NAME=$(rpm_dump | awk '$2 == "PACKAGE_NAME" { print $3; exit}')
637 PACKAGE_VERSION=$(rpm_dump | awk '$2 == "PACKAGE_VERSION" { print $3; exit}')
638 PACKAGE_RELEASE=$(rpm_dump | awk '$2 == "PACKAGE_RELEASE" { print $3; exit}')
78eab2a1 639
0b4119a2 640 if [ "$PACKAGE_NAME" != "$ASSUMED_NAME" ]; then
7854d40c 641 echo >&2 "WARNING! Spec name ($ASSUMED_NAME) does not agree with package name ($PACKAGE_NAME)"
0b4119a2
JR
642 fi
643
cd445739
AM
644 if [ -n "$BE_VERBOSE" ]; then
645 echo "- Sources : `nourl $SOURCES`"
646 if [ -n "$PATCHES" ]; then
647 echo "- Patches : `nourl $PATCHES`"
648 else
649 echo "- Patches : *no patches needed*"
650 fi
651 if [ -n "$ICONS" ]; then
652 echo "- Icon : `nourl $ICONS`"
653 else
654 echo "- Icon : *no package icon*"
655 fi
656 echo "- Name : $PACKAGE_NAME"
657 echo "- Version : $PACKAGE_VERSION"
658 echo "- Release : $PACKAGE_RELEASE"
659 fi
13974367
ER
660
661 update_shell_title "parse_spec: OK!"
cd445739
AM
662}
663
f6711e2a 664Exit_error() {
cd445739 665 if [ -n "$DEBUG" ]; then
518ff1fb
ER
666 set -x
667 set -v
cd445739
AM
668 fi
669
670 cd "$__PWD"
671
672 case "$1" in
673 "err_no_spec_in_cmdl" )
674 remove_build_requires
45e2a818 675 echo >&2 "ERROR: spec file name not specified."
cd445739 676 exit 2 ;;
a08d92fc 677 "err_invalid_cmdline" )
45e2a818 678 echo >&2 "ERROR: invalid command line arg ($2)."
a08d92fc 679 exit 2 ;;
cd445739
AM
680 "err_no_spec_in_repo" )
681 remove_build_requires
45e2a818 682 echo >&2 "Error: spec file not stored in CVS repo."
cd445739
AM
683 exit 3 ;;
684 "err_no_source_in_repo" )
685 remove_build_requires
45e2a818 686 echo >&2 "Error: some source, patch or icon files not stored in CVS repo. ($2)"
cd445739 687 exit 4 ;;
703b6deb
ER
688 "err_cvs_add_failed" )
689 echo >&2 "Error: failed to add package to CVS repo."
690 exit 4 ;;
cd445739
AM
691 "err_build_fail" )
692 remove_build_requires
45e2a818 693 echo >&2 "Error: package build failed. (${2:-no more info})"
cd445739 694 exit 5 ;;
78eab2a1
AM
695 "err_no_package_data" )
696 remove_build_requires
45e2a818 697 echo >&2 "Error: couldn't get out package name/version/release from spec file."
78eab2a1 698 exit 6 ;;
ce40491a 699 "err_tag_exists" )
50321881 700 remove_build_requires
45e2a818 701 echo >&2 "Tag ${2} already exists (spec release: ${3})."
50321881 702 exit 9 ;;
ce40491a 703 "err_fract_rel" )
50321881 704 remove_build_requires
45e2a818 705 echo >&2 "Release ${2} not integer and not a snapshot."
50321881 706 exit 10 ;;
fb96e0b5
AM
707 "err_branch_exists" )
708 remove_build_requires
45e2a818 709 echo >&2 "Tree branch already exists (${2})."
fb96e0b5 710 exit 11 ;;
cfc497c0
ER
711 "err_acl_deny" )
712 remove_build_requires
45e2a818 713 echo >&2 "Error: conditions reject building this spec (${2})."
cfc497c0 714 exit 12 ;;
cd445739 715 esac
45e2a818 716 echo >&2 "Unknown error."
73848362 717 exit 100
cd445739
AM
718}
719
f6711e2a 720init_builder() {
cd445739 721 if [ -n "$DEBUG" ]; then
518ff1fb
ER
722 set -x
723 set -v
0dd6320d 724 fi
cd445739 725
4aeb9973 726 if [ "$NOINIT" != "yes" ] ; then
9c909460 727 TOP_DIR=$(eval $RPM $RPMOPTS --eval '%{_topdir}')
b7cc0c94 728
09bf17c9 729 local macros_ver=$(rpm -E %?rpm_build_macros)
b7cc0c94
ER
730 if [ -z "$macros_ver" ]; then
731 REPO_DIR=$TOP_DIR/packages
732 PACKAGE_DIR=$TOP_DIR/packages/$ASSUMED_NAME
733 else
734 if awk "BEGIN{exit($macros_ver>=$RPM_MACROS_VER)}"; then
735 echo >&2 "builder requires rpm-build-macros >= $RPM_MACROS_VER"
736 exit 1
737 fi
738 REPO_DIR=$TOP_DIR
739 PACKAGE_DIR=$REPO_DIR/$ASSUMED_NAME
740 fi
29e90b02 741 else
9c909460
AM
742 REPO_DIR="."
743 PACKAGE_DIR="."
29e90b02 744 fi
cd445739 745
a8c9a155 746 __PWD=$(pwd)
00fcec7e 747}
748
f6711e2a 749get_spec() {
c890b916 750
2e5be021 751 update_shell_title "get_spec"
c890b916 752
cd445739 753 if [ -n "$DEBUG" ]; then
518ff1fb
ER
754 set -x
755 set -v
cd445739
AM
756 fi
757
9c909460
AM
758 cd "$REPO_DIR"
759 if [ ! -f "$ASSUMED_NAME/$SPECFILE" ]; then
6277f15f
ER
760 # XXX: still needed?
761 SPECFILE=$(basename $SPECFILE)
cd445739 762 fi
cd445739 763
1ef6f5de 764 if [ "$NOCVSSPEC" != "yes" ]; then
049ab214 765 if [ ! -s CVS/Root -a "$NOCVSSPEC" != "yes" ]; then
15e34b63 766 echo "Warning: No CVS access defined in $(pwd)- using local .spec file"
049ab214 767 NOCVSSPEC="yes"
cd445739
AM
768 fi
769
1ef6f5de 770 if [ -d "$ASSUMED_NAME" -a -s "$ASSUMED_NAME/CVS/Root" ]; then
9c909460 771 cvsup "$ASSUMED_NAME/$SPECFILE" || Exit_error err_no_spec_in_repo
703b6deb 772 elif [ "$ADD_PACKAGE_CVS" = "yes" ]; then
1a135961 773 if [ ! -r "$ASSUMED_NAME/$SPECFILE" ]; then
774 echo "ERROR: No package to add ($ASSUMED_NAME/$SPECFILE)" >&2
775 exit 101
703b6deb
ER
776 fi
777 if [ ! -s "$ASSUMED_NAME/CVS/Root" ]; then
778 cvsup -a $ASSUMED_NAME || Exit_error err_cvs_add_failed
779 fi
780 cvsup -a "$ASSUMED_NAME/$SPECFILE" || Exit_error err_cvs_add_failed
9c909460 781 else
1ef6f5de
ER
782 cvsup -c -d $ASSUMED_NAME "packages/$ASSUMED_NAME/$SPECFILE" || {
783 # softfail if new package, i.e not yet added to cvs
784 [ ! -f "$ASSUMED_NAME/$SPECFILE" ] && Exit_error err_no_spec_in_repo
785 echo "Warning: package not in CVS - assuming new package"
786 NOCVSSPEC="yes"
787 NOCVS="yes"
788 }
f123bcb5
ER
789
790 # remove Entries.Static -- so 'cvs up' would update all files in a repo
1ef6f5de 791 rm -f "$ASSUMED_NAME/CVS/Entries.Static"
703b6deb 792 fi
0c3beb90 793
703b6deb
ER
794 cvsignore_df .cvsignore
795
796 # add default log format to .cvsignore if it is relative to package dir
797 if [ -n "$LOGFILE" -a "$LOGFILE" = "${LOGFILE##*/}" ]; then
798 # substitute known "macros" to glob
799 local logfile=$(echo "$LOGFILE" | sed -e 's,\$\(PACKAGE_NAME\|DATE\),*,g')
800 if [ "$logfile" ]; then
801 cvsignore_df "$logfile"
daeab113 802 fi
9c909460 803 fi
703b6deb
ER
804
805 # create symlinks for tools
806 if [ "$SYMLINK_TOOLS" != "no" ]; then
807 for a in dropin md5 adapter builder {relup,compile,repackage,rsync,pearize}.sh pldnotify.awk; do
046b715f 808 # skip tools that don't exist in top dir
703b6deb 809 [ -f $a ] || continue
046b715f
ER
810 # skip tools that already exist
811 [ -f $ASSUMED_NAME/$a ] && continue
f1081b0a 812 ln -s ../$a $ASSUMED_NAME
703b6deb
ER
813 cvsignore_df $a
814 done
815 fi
cd445739 816 fi
049ab214 817
9c909460 818 if [ ! -f "$ASSUMED_NAME/$SPECFILE" ]; then
518ff1fb 819 Exit_error err_no_spec_in_repo
cd445739
AM
820 fi
821
822 if [ "$CHMOD" = "yes" -a -n "$SPECFILE" ]; then
9c909460 823 chmod $CHMOD_MODE $ASSUMED_NAME/$SPECFILE
cd445739
AM
824 fi
825 unset OPTIONS
9c909460 826 [ -n "$DONT_PRINT_REVISION" ] || grep -E -m 1 "^#.*Revision:.*Date" $ASSUMED_NAME/$SPECFILE
7e40520f 827
ce40491a 828 set_spec_target
cd445739
AM
829}
830
f6711e2a 831find_mirror() {
9c909460 832 cd "$REPO_DIR"
782518da 833 local url="$1"
cd445739 834 if [ ! -f "mirrors" -a "$NOCVSSPEC" != "yes" ] ; then
939f2c12 835 $CVS_COMMAND update mirrors >&2
cd445739
AM
836 fi
837
838 IFS="|"
782518da
ER
839 local origin mirror name rest ol prefix
840 while read origin mirror name rest; do
841 # skip comments and empty lines
2faa4c32 842 if [ -z "$origin" ] || [ "${origin#\#}" != "$origin" ]; then
782518da
ER
843 continue
844 fi
a8c9a155
ER
845 ol=$(echo -n "$origin" | wc -c)
846 prefix=$(echo -n "$url" | head -c $ol)
cd445739 847 if [ "$prefix" = "$origin" ] ; then
a8c9a155 848 suffix=$(echo "$url" | cut -b $((ol+1))-)
cd445739
AM
849 echo -n "$mirror$suffix"
850 return 0
851 fi
852 done < mirrors
853 echo "$url"
854}
855
e7e0d4ec 856# Warning: unpredictable results if same URL used twice
f6711e2a 857src_no() {
8510980a
AM
858 local file="$1"
859 # escape some regexp characters if part of file name
298e75e2 860 file=$(echo "$file" | sed -e 's#\([\+\*\.\&\#\?]\)#\\\1#g')
9c909460 861 cd $PACKAGE_DIR
cd445739 862 rpm_dump | \
8510980a 863 grep -E "(SOURCE|PATCH)URL[0-9]*[ ]*${file}""[ ]*$" | \
647e3d7f
KK
864 sed -e 's/.*\(SOURCE\|PATCH\)URL\([0-9][0-9]*\).*/\1\2/' | \
865 head -n 1 | tr OURCEATH ourceath | xargs
1471f6d6 866}
867
f6711e2a 868src_md5() {
e7e0d4ec 869 [ "$NO5" = "yes" ] && return
cd445739
AM
870 no=$(src_no "$1")
871 [ -z "$no" ] && return
9c909460 872 cd $PACKAGE_DIR
e7e0d4ec
ER
873 local md5
874
875 if [ -f additional-md5sums ]; then
876 local spec_rev=$(grep $SPECFILE CVS/Entries 2>/dev/null | sed -e s:/$SPECFILE/:: -e s:/.*::)
877 if [ -z "$spec_rev" ]; then
a8c9a155 878 spec_rev=$(head -n 1 $SPECFILE | sed -e 's/.*\$Revision: \([0-9.]*\).*/\1/')
e7e0d4ec
ER
879 fi
880 local spec="$SPECFILE[0-9.,]*,$(echo $spec_rev | sed 's/\./\\./g')"
881 md5=$(grep -s -v '^#' additional-md5sums | \
882 grep -E "[ ]$(basename "$1")[ ]+${spec}([ ,]|\$)" | \
883 sed -e 's/^\([0-9a-f]\{32\}\).*/\1/' | \
884 grep -E '^[0-9a-f]{32}$')
885
886 if [ "$md5" ]; then
887 if [ $(echo "$md5" | wc -l) != 1 ] ; then
888 echo "$SPECFILE: more then one entry in additional-md5sums for $1" 1>&2
889 fi
890 echo "$md5" | tail -n 1
891 return
892 fi
cd445739 893 fi
e7e0d4ec 894
647e3d7f 895 source_md5=`grep -i "^#[ ]*$no-md5[ ]*:" $SPECFILE | sed -e 's/.*://'`
e7e0d4ec
ER
896 if [ -n "$source_md5" ]; then
897 echo $source_md5
cd445739 898 else
7805dcb4
PZ
899 source_md5=`grep -i "BuildRequires:[ ]*digest(%SOURCE$no)[ ]*=" $SPECFILE | sed -e 's/.*=//'`
900 if [ -n "$source_md5" ]; then
901 echo $source_md5
902 else
903 # we have empty SourceX-md5, but it is still possible
904 # that we have NoSourceX-md5 AND NoSource: X
647e3d7f 905 nosource_md5=`grep -i "^#[ ]*No$no-md5[ ]*:" $SPECFILE | sed -e 's/.*://'`
7805dcb4
PZ
906 if [ -n "$nosource_md5" -a -n "`grep -i "^NoSource:[ ]*$no$" $SPECFILE`" ] ; then
907 echo $nosource_md5
908 fi
cd445739 909 fi
cd445739
AM
910 fi
911}
912
f6711e2a 913distfiles_path() {
06541433
JR
914 echo "by-md5/$(src_md5 "$1" | sed -e 's|^\(.\)\(.\)|\1/\2/&|')/$(basename "$1")"
915}
916
f6711e2a 917distfiles_url() {
06541433 918 echo "$PROTOCOL$DISTFILES_SERVER/distfiles/$(distfiles_path "$1")"
cd445739
AM
919}
920
f6711e2a 921distfiles_attic_url() {
06541433 922 echo "$PROTOCOL$ATTICDISTFILES_SERVER/distfiles/Attic/$(distfiles_path "$1")"
cd445739
AM
923}
924
f6711e2a 925good_md5() {
cd445739
AM
926 md5=$(src_md5 "$1")
927 [ "$md5" = "" ] || \
928 [ "$md5" = "$(md5sum $(nourl "$1") 2> /dev/null | sed -e 's/ .*//')" ]
8ba5cdda
PG
929}
930
f6711e2a 931good_size() {
a8c9a155 932 size=$(find $(nourl "$1") -printf "%s" 2>/dev/null)
ce40491a 933 [ -n "$size" -a "$size" -gt 0 ]
a4b50627
AF
934}
935
f6711e2a 936cvsignore_df() {
947025e5 937 if [ "$CVSIGNORE_DF" != "yes" ]; then
938 return
939 fi
9c909460 940 cvsignore=${PACKAGE_DIR}/.cvsignore
7dc8d095
ER
941
942 # add only if not yet there
aa189b05 943 if ! awk -vf="$1" -vc=1 '$0 == f { c = 0 } END { exit c }' $cvsignore 2>/dev/null; then
947025e5 944 echo "$1" >> $cvsignore
945 fi
946}
947
f6711e2a 948cvsup() {
ce40491a 949 update_shell_title "cvsup"
703b6deb 950 local OPTIONS="" ACTION="up"
9c909460 951
703b6deb 952 # checkout
9c909460 953 if [ "$1" = "-c" ]; then
703b6deb
ER
954 ACTION="co"
955 shift
956 fi
957 # add
958 if [ "$1" = "-a" ]; then
959 ACTION="add"
9c909460
AM
960 shift
961 fi
703b6deb
ER
962
963 OPTIONS="$ACTION "
964
ce40491a
ER
965 if [ -n "$CVSROOT" ]; then
966 OPTIONS="-d $CVSROOT $OPTIONS"
967 fi
b1c6aace 968
703b6deb
ER
969 if [ "$ACTION" != "add" ]; then
970 if [ -z "$CVSDATE" -a -z "$CVSTAG" ]; then
971 OPTIONS="$OPTIONS -A"
972 else
973 if [ -n "$CVSDATE" ]; then
974 OPTIONS="$OPTIONS -D $CVSDATE"
975 fi
976 if [ -n "$CVSTAG" ]; then
977 # FIXME: cvs add actually works with -r ?
978 OPTIONS="$OPTIONS -r $CVSTAG"
979 fi
ce40491a
ER
980 fi
981 fi
e7fa3b9f 982
ce40491a
ER
983 local result=1
984 local retries_counter=0
985 if [ $# = 1 ]; then
986 update_shell_title "cvsup: $*"
987 else
988 update_shell_title "cvsup: $# files"
989 fi
990 while [ "$result" != "0" -a "$retries_counter" -le "$CVS_RETRIES" ]; do
991 retries_counter=$(( $retries_counter + 1 ))
939f2c12 992 output=$(LC_ALL=C $CVS_COMMAND $OPTIONS "$@" 2>&1)
ce40491a
ER
993 result=$?
994 [ -n "$output" ] && echo "$output"
703b6deb
ER
995 if echo "$output" | grep -qE "(Cannot connect to|connect to .* failed|Connection reset by peer|Connection timed out|Unknown host)" \
996 && [ "$result" -ne "0" -a "$retries_counter" -le "$CVS_RETRIES" ]; then
ce40491a
ER
997 echo "Trying again [$*]... ($retries_counter)"
998 update_shell_title "cvsup: retry #$retries_counter"
999 sleep 2
1000 continue
1001 else
1002 break
1003 fi
1004 done
1005 update_shell_title "cvsup: done!"
1006 return $result
e7fa3b9f
ER
1007}
1008
71f8e908 1009# returns true if "$1" is ftp, http or https protocol url
f6711e2a 1010is_url() {
71f8e908
ER
1011 case "$1" in
1012 ftp://*|http://*|https://*)
1013 return 0
1014 ;;
1015 esac
1016 return 1
1017}
1018
f6711e2a 1019update_md5() {
ecba6ad5
ER
1020 if [ $# -eq 0 ]; then
1021 return
1022 fi
1023
1024 update_shell_title "update md5"
1025 if [ -n "$DEBUG" ]; then
518ff1fb
ER
1026 set -x
1027 set -v
ecba6ad5
ER
1028 fi
1029
9c909460 1030 cd "$PACKAGE_DIR"
ecba6ad5
ER
1031
1032 # pass 1: check files to be fetched
1033 local todo
1034 local need_files
1035 for i in "$@"; do
1036 local fp=$(nourl "$i")
1037 local srcno=$(src_no "$i")
6392b5fd
ER
1038 if [ -n "$ADD5" ]; then
1039 [ "$fp" = "$i" ] && continue # FIXME what is this check doing?
647e3d7f 1040 grep -qiE '^#[ ]*'$srcno'-md5[ ]*:' $PACKAGE_DIR/$SPECFILE && continue
9c909460 1041 grep -qiE '^BuildRequires:[ ]*digest[(]%SOURCE'$srcno'[)][ ]*=' $PACKAGE_DIR/$SPECFILE && continue
6392b5fd 1042 else
647e3d7f 1043 grep -qiE '^#[ ]*'$srcno'-md5[ ]*:' $PACKAGE_DIR/$SPECFILE || grep -qiE '^BuildRequires:[ ]*digest[(]%SOURCE'$srcno'[)][ ]*=' $PACKAGE_DIR/$SPECFILE || continue
ecba6ad5
ER
1044 fi
1045 if [ ! -f "$fp" ] || [ $ALWAYS_CVSUP = "yes" ]; then
1046 need_files="$need_files $i"
1047 fi
1048 done
1049
1050 # pass 1a: get needed files
1051 if [ "$need_files" ]; then
1052 get_files $need_files
1053 fi
1054
1055 # pass 2: proceed with md5 adding or updating
1056 for i in "$@"; do
1057 local fp=$(nourl "$i")
1058 local srcno=$(src_no "$i")
647e3d7f 1059 local md5=$(grep -iE '^#[ ]*(No)?'$srcno'-md5[ ]*:' $PACKAGE_DIR/$SPECFILE )
e19ad799 1060 if [ -z "$md5" ]; then
9c909460 1061 md5=$(grep -iE '^[ ]*BuildRequires:[ ]*digest[(]%SOURCE'$srcno'[)][ ]*=' $PACKAGE_DIR/$SPECFILE )
6c1b2e7d 1062 fi
71f8e908 1063 if [ -n "$ADD5" ] && is_url $i || [ -n "$md5" ]; then
647e3d7f 1064 local tag="# $srcno-md5:\t"
71f8e908 1065 if [[ "$md5" == *NoSource* ]]; then
6663e1f3 1066 tag="# No$srcno-md5:\t"
6c1b2e7d
PZ
1067 elif [ -n "$USEDIGEST" ]; then
1068 tag="BuildRequires:\tdigest(%SOURCE$srcno) = "
71f8e908 1069 fi
ecba6ad5 1070 md5=$(md5sum "$fp" | cut -f1 -d' ')
647e3d7f 1071 echo "Updating $srcno ($md5: $fp)."
ecba6ad5 1072 perl -i -ne '
647e3d7f
KK
1073 print unless (/^\s*#\s*(No)?'$srcno'-md5\s*:/i or /^\s*BuildRequires:\s*digest\(%SOURCE'$srcno'\)/i);
1074 print "'"$tag$md5"'\n" if /^'$srcno'\s*:\s+/i;
ecba6ad5 1075 ' \
9c909460 1076 $PACKAGE_DIR/$SPECFILE
ecba6ad5
ER
1077 fi
1078 done
1079}
1080
f6711e2a 1081check_md5() {
60499e54 1082 local bad
3fbff8b8
ER
1083 [ "$NO5" = "yes" ] && return
1084
ecba6ad5
ER
1085 update_shell_title "check md5"
1086
1087 for i in "$@"; do
60499e54
AM
1088 bad=0
1089 if ! good_md5 "$i"; then
1090 echo -n "MD5 sum mismatch."
1091 bad=1
1092 fi
1093 if ! good_size "$i"; then
1094 echo -n "0 sized file."
1095 bad=1
ecba6ad5
ER
1096 fi
1097
60499e54
AM
1098 if [ $bad -eq 1 ]; then
1099 echo " Use -U to refetch sources,"
1100 echo "or -5 to update md5 sums, if you're sure files are correct."
1101 Exit_error err_no_source_in_repo $i
1102 fi
ecba6ad5
ER
1103 done
1104}
1105
f6711e2a 1106get_files() {
7cb24972 1107 update_shell_title "get_files"
cd445739
AM
1108
1109 if [ -n "$DEBUG" ]; then
518ff1fb
ER
1110 set -x
1111 set -v
cd445739
AM
1112 fi
1113
f09c0772 1114 if [ $# -gt 0 ]; then
9c909460 1115 cd "$PACKAGE_DIR"
cd445739 1116
b1c6aace 1117 if [ ! -s CVS/Root -a "$NOCVS" != "yes" ]; then
049ab214 1118 echo "Warning: No CVS access defined for SOURCES"
b1c6aace
ER
1119 NOCVS="yes"
1120 fi
1121
f2e42a41 1122 local nc=0
e7fa3b9f 1123 local get_files_cvs=""
3815b69a 1124 for i in "$@"; do
f2e42a41 1125 nc=$((nc + 1))
dcebdffb 1126 local cvsup=0
3815b69a 1127 SHELL_TITLE_PREFIX="get_files[$nc/$#]"
f2e42a41 1128 update_shell_title "$i"
f09c0772 1129 local fp=`nourl "$i"`
ecba6ad5 1130 if [ "$SKIP_EXISTING_FILES" = "yes" ] && [ -f "$fp" ]; then
ce40491a 1131 continue
18e5347d 1132 fi
ecba6ad5 1133
cd445739 1134 FROM_DISTFILES=0
e7e0d4ec 1135 local srcmd5=$(src_md5 "$i")
ecba6ad5
ER
1136
1137 # we know if source/patch is present in cvs/distfiles
1138 # - has md5 (in distfiles)
1139 # - in cvs... ideas?
1140
1141 # CHECK: local file didn't exist or always cvs up (first) requested.
f09c0772 1142 if [ ! -f "$fp" ] || [ $ALWAYS_CVSUP = "yes" ]; then
cd445739
AM
1143 if echo $i | grep -vE '(http|ftp|https|cvs|svn)://' | grep -qE '\.(gz|bz2)$']; then
1144 echo "Warning: no URL given for $i"
1145 fi
08908161 1146 target="$fp"
cd445739 1147
e7e0d4ec 1148 if [ -z "$NODIST" ] && [ -n "$srcmd5" ]; then
a4b50627 1149 if good_md5 "$i" && good_size "$i"; then
e7e0d4ec 1150 echo "$fp having proper md5sum already exists"
cd445739
AM
1151 continue
1152 fi
28b34560 1153
bc10f694 1154 # optionally prefer mirror over distfiles if there's mirror
28b34560 1155 # TODO: build url list and then try each url from the list
bc10f694 1156 if [ -n "$PREFMIRRORS" ] && [ -z "$NOMIRRORS" ] && im=$(find_mirror "$i") && [ "$im" != "$i" ]; then
28b34560
ER
1157 url="$im"
1158 else
1159 url=$(distfiles_url "$i")
1160 fi
1161
cd445739
AM
1162 url_attic=$(distfiles_attic_url "$i")
1163 FROM_DISTFILES=1
ecba6ad5
ER
1164 # is $url local file?
1165 if [[ "$url" = [./]* ]]; then
18a52930 1166 update_shell_title "${GETLOCAL%% *}: $url"
cd445739
AM
1167 ${GETLOCAL} $url $target
1168 else
1169 if [ -z "$NOMIRRORS" ]; then
9e9f8920 1170 url=$(find_mirror "$url")
cd445739 1171 fi
9e9f8920
ER
1172
1173 local uri=${url}
1174 # make shorter message for distfiles urls
1175 if [[ "$uri" = ${PROTOCOL}${DISTFILES_SERVER}* ]] || [[ "$uri" = ${PROTOCOL}${ATTICDISTFILES_SERVER}* ]]; then
1176 uri=${uri#${PROTOCOL}${DISTFILES_SERVER}/distfiles/by-md5/?/?/*/}
1177 uri=${uri#${PROTOCOL}${ATTICDISTFILES_SERVER}/distfiles/by-md5/?/?/*/}
1178 uri="df: $uri"
1179 fi
1180 update_shell_title "${GETURI%% *}: $uri"
ca2e6c31 1181 ${GETURI} ${OUTFILEOPT} "$target" "$url" || \
33da77c4 1182 if [ "`echo $url | grep -E 'ftp://'`" ]; then
18a52930 1183 update_shell_title "${GETURI2%% *}: $url"
ca2e6c31 1184 ${GETURI2} ${OUTFILEOPT} "$target" "$url"
cd445739
AM
1185 fi
1186 fi
ecba6ad5
ER
1187
1188 # is it empty file?
1189 if [ ! -s "$target" ]; then
cd445739
AM
1190 rm -f "$target"
1191 if [ `echo $url_attic | grep -E '^(\.|/)'` ]; then
18a52930 1192 update_shell_title "${GETLOCAL%% *}: $url_attic"
cd445739
AM
1193 ${GETLOCAL} $url_attic $target
1194 else
1195 if [ -z "$NOMIRRORS" ]; then
a8c9a155 1196 url_attic=$(find_mirror "$url_attic")
cd445739 1197 fi
18a52930 1198 update_shell_title "${GETURI%% *}: $url_attic"
ca2e6c31 1199 ${GETURI} ${OUTFILEOPT} "$target" "$url_attic" || \
33da77c4 1200 if [ "`echo $url_attic | grep -E 'ftp://'`" ]; then
ce40491a 1201 update_shell_title "${GETURI2%% *}: $url_attic"
ca2e6c31 1202 ${GETURI2} ${OUTFILEOPT} "$target" "$url_attic"
cd445739 1203 fi
1cd7fc57 1204 test -s "$target" || rm -f "$target"
cd445739
AM
1205 fi
1206 fi
ecba6ad5
ER
1207
1208 if [ -s "$target" ]; then
947025e5 1209 cvsignore_df $target
1210 else
cd445739
AM
1211 rm -f "$target"
1212 FROM_DISTFILES=0
1213 fi
e7e0d4ec 1214 elif [ "$NOCVS" != "yes" -a -z "$srcmd5" ]; then
d05e8080
ER
1215 if [ $# -gt 1 ]; then
1216 get_files_cvs="$get_files_cvs $fp"
1217 update_shell_title "$fp (will cvs up later)"
1218 cvsup=1
1219 else
1220 cvsup $fp
1221 fi
cd445739
AM
1222 fi
1223
f09c0772 1224 if [ -z "$NOURLS" ] && [ ! -f "$fp" -o -n "$UPDATE" ] && [ "`echo $i | grep -E 'ftp://|http://|https://'`" ]; then
cd445739 1225 if [ -z "$NOMIRRORS" ]; then
a8c9a155 1226 im=$(find_mirror "$i")
cd445739
AM
1227 else
1228 im="$i"
1229 fi
ce40491a 1230 update_shell_title "${GETURI%% *}: $im"
99b8f51f 1231 ${GETURI} ${OUTFILEOPT} "$target" "$im" || \
33da77c4 1232 if [ "`echo $im | grep -E 'ftp://'`" ]; then
ce40491a 1233 update_shell_title "${GETURI2%% *}: $im"
99b8f51f 1234 ${GETURI2} ${OUTFILEOPT} "$target" "$im"
7e365483 1235 fi
1cd7fc57 1236 test -s "$target" || rm -f "$target"
cd445739
AM
1237 fi
1238
dcebdffb
ER
1239 if [ "$cvsup" = 1 ]; then
1240 continue
1241 fi
1242
cd445739 1243 fi
ecba6ad5
ER
1244
1245 # the md5 check must be moved elsewhere as if we've called from update_md5 the md5 is wrong.
f09c0772 1246 if [ ! -f "$fp" -a "$FAIL_IF_NO_SOURCES" != "no" ]; then
cdbd30e8 1247 if [ -n "GREEDSRC" ]; then
1248 get_greed_sources $i
1249 else
1250 Exit_error err_no_source_in_repo $i
1251 fi
cd445739 1252 fi
2a5f078d 1253
ecba6ad5 1254 # we check md5 here just only to refetch immediately
a4b50627 1255 if good_md5 "$i" && good_size "$i"; then
cd445739
AM
1256 :
1257 elif [ "$FROM_DISTFILES" = 1 ]; then
deccc50e 1258 # wrong md5 from distfiles: remove the file and try again
cd445739 1259 # but only once ...
deccc50e 1260 echo "MD5 sum mismatch. Trying full fetch."
cd445739
AM
1261 FROM_DISTFILES=2
1262 rm -f $target
18a52930 1263 update_shell_title "${GETURI%% *}: $url"
ca2e6c31 1264 ${GETURI} ${OUTFILEOPT} "$target" "$url" || \
33da77c4 1265 if [ "`echo $url | grep -E 'ftp://'`" ]; then
ce40491a 1266 update_shell_title "${GETURI2%% *}: $url"
ca2e6c31 1267 ${GETURI2} ${OUTFILEOPT} "$target" "$url"
cd445739 1268 fi
ecba6ad5 1269 if [ ! -s "$target" ]; then
cd445739 1270 rm -f "$target"
18a52930 1271 update_shell_title "${GETURI%% *}: $url_attic"
ca2e6c31 1272 ${GETURI} ${OUTFILEOPT} "$target" "$url_attic" || \
33da77c4 1273 if [ "`echo $url_attic | grep -E 'ftp://'`" ]; then
ce40491a 1274 update_shell_title "${GETURI2%% *}: $url_attic"
ca2e6c31 1275 ${GETURI2} ${OUTFILEOPT} "$target" "$url_attic"
cd445739
AM
1276 fi
1277 fi
1278 test -s "$target" || rm -f "$target"
1279 fi
cd445739 1280 done
e7fa3b9f
ER
1281 SHELL_TITLE_PREFIX=""
1282
1283 if [ "$get_files_cvs" ]; then
ce40491a 1284 cvsup $get_files_cvs
e7fa3b9f 1285 fi
cd445739
AM
1286
1287 if [ "$CHMOD" = "yes" ]; then
a8c9a155 1288 CHMOD_FILES=$(nourl "$@")
cd445739
AM
1289 if [ -n "$CHMOD_FILES" ]; then
1290 chmod $CHMOD_MODE $CHMOD_FILES
1291 fi
1292 fi
cd445739
AM
1293 fi
1294}
1295
1296make_tagver() {
ce40491a 1297 if [ -n "$DEBUG" ]; then
518ff1fb
ER
1298 set -x
1299 set -v
ce40491a 1300 fi
78eab2a1 1301
ce40491a
ER
1302 # Check whether first character of PACKAGE_NAME is legal for tag name
1303 if [ -z "${PACKAGE_NAME##[_0-9]*}" -a -z "$TAG_PREFIX" ]; then
1304 TAG_PREFIX=tag_
1305 fi
d712cc12
ER
1306
1307 # NOTE: CVS tags may must not contain the characters `$,.:;@'
392187d9 1308 TAGVER=$(echo $TAG_PREFIX$PACKAGE_NAME-$PACKAGE_VERSION-$PACKAGE_RELEASE | tr '[.@]' '[_#]')
d712cc12 1309
ce40491a
ER
1310 # Remove #kernel.version_release from TAGVER because tagging sources
1311 # could occur with different kernel-headers than kernel-headers used at build time.
d712cc12
ER
1312 # besides, %{_kernel_ver_str} is not expanded.
1313
1314 # TAGVER=auto-ac-madwifi-ng-0-0_20070225_1#%{_kernel_ver_str}
1315 # TAGVER=auto-ac-madwifi-ng-0-0_20070225_1
1316
1317 TAGVER=${TAGVER%#*}
ce40491a 1318 echo -n "$TAGVER"
cd445739
AM
1319}
1320
c713aac3
AG
1321# bool is_tag_a_branch(tag)
1322#
1323# returns 1 if a tag is a branch set on SPECFILE
1324is_tag_a_branch() {
1325 if [ -n "$DEBUG" ]; then
1326 set -x
1327 set -v
1328 fi
1329
1330 if [ $# -ne 1 ]; then
1331 return 0;
1332 fi
1333
8efe84f4 1334 local _TAG=$1
607e2d9d 1335 # escape some regexp characters if part of TAG
8efe84f4 1336 _TAG=$(echo "$_TAG" | sed -e 's#\([\+\*\.]\)#\\\1#g')
607e2d9d 1337
f5141bbc 1338
9c909460 1339 cd "$PACKAGE_DIR"
8efe84f4 1340 $CVS_COMMAND status -v $SPECFILE | grep -Eiq "${_TAG}.+(branch: [0-9.]+)"
c713aac3
AG
1341 return $?
1342}
1343
f6711e2a 1344tag_files() {
cd445739
AM
1345 TAG_FILES="$@"
1346
1347 if [ -n "$DEBUG" ]; then
518ff1fb
ER
1348 set -x
1349 set -v
cd445739
AM
1350 fi
1351
73ef63d8
ER
1352 echo "Version: $PACKAGE_VERSION"
1353 echo "Release: $PACKAGE_RELEASE"
cd445739 1354
a18a9761 1355 local TAGVER
73ef63d8 1356 if [ "$TAG_VERSION" = "yes" ]; then
a18a9761 1357 TAGVER=`make_tagver`
73ef63d8
ER
1358 echo "CVS tag: $TAGVER"
1359 fi
1360 if [ -n "$TAG" ]; then
1361 echo "CVS tag: $TAG"
1362 fi
cd445739 1363
39dcc7de 1364 local OPTIONS="tag $CVS_FORCE"
73ef63d8
ER
1365 if [ -n "$CVSROOT" ]; then
1366 OPTIONS="-d $CVSROOT $OPTIONS"
1367 fi
cd445739 1368
c713aac3
AG
1369 # if a tagname we are about to set already exists
1370 # and happens to be a branch (common case with AC-branch)
1371 # pass -B (allows -F to disturb branch tag)
1372 local _tag=$TAG
1373 if [ "$TAG_VERSION" = "yes" ]; then
1374 _tag=$TAGVER
1375 fi;
1376 is_tag_a_branch $_tag
28c23a72 1377 if [ $? -eq 0 -a $CVS_NSERVER -eq 0 ]; then
c713aac3
AG
1378 OPTIONS="$OPTIONS -B"
1379 fi;
1380
9c909460 1381 cd "$PACKAGE_DIR"
73ef63d8
ER
1382 local tag_files
1383 for i in $TAG_FILES; do
1384 # don't tag files stored on distfiles
1385 [ -n "`src_md5 $i`" ] && continue
1386 local fp=`nourl "$i"`
1387 if [ -f "$fp" ]; then
1388 tag_files="$tag_files $fp"
cdbd30e8 1389 elif [ -n "GREEDSRC" ]; then
1390 get_greed_sources $i
73ef63d8
ER
1391 else
1392 Exit_error err_no_source_in_repo $i
03db3356 1393 fi
73ef63d8 1394 done
03db3356 1395
73ef63d8 1396 if [ "$tag_files" ]; then
cd445739 1397 if [ "$TAG_VERSION" = "yes" ]; then
73ef63d8 1398 update_shell_title "tag sources: $TAGVER"
19ca62ef 1399 printf "Tagging %d files\n" $(echo $tag_files | wc -w)
939f2c12 1400 $CVS_COMMAND $OPTIONS $TAGVER $tag_files || exit
cd445739
AM
1401 fi
1402 if [ -n "$TAG" ]; then
73ef63d8 1403 update_shell_title "tag sources: $TAG"
c4ff623e
ER
1404
1405 while [ "$tag_files" ]; do
1406 local chunk=$(echo $tag_files | tr ' ' '\n' | head -n 100)
1407 printf "Tagging %d files\n" $(echo $chunk | wc -w)
939f2c12 1408 $CVS_COMMAND $OPTIONS $TAG $chunk || exit
c4ff623e
ER
1409 tag_files=$(echo $tag_files | tr ' ' '\n' | tail +101)
1410 done
cd445739 1411 fi
73ef63d8
ER
1412 fi
1413
9c909460 1414 cd "$PACKAGE_DIR"
73ef63d8
ER
1415 if [ "$TAG_VERSION" = "yes" ]; then
1416 update_shell_title "tag spec: $TAGVER"
939f2c12 1417 $CVS_COMMAND $OPTIONS $TAGVER $SPECFILE || exit
73ef63d8
ER
1418 fi
1419 if [ -n "$TAG" ]; then
1420 update_shell_title "tag spec: $TAG"
939f2c12 1421 $CVS_COMMAND $OPTIONS $TAG $SPECFILE || exit
73ef63d8 1422 fi
cd445739
AM
1423}
1424
f6711e2a 1425branch_files() {
cd445739
AM
1426 TAG=$1
1427 echo "CVS branch tag: $TAG"
518ff1fb 1428 shift
cd445739
AM
1429
1430 TAG_FILES="$@"
1431
1432 if [ -n "$DEBUG" ]; then
518ff1fb
ER
1433 set -x
1434 set -v
cd445739
AM
1435 fi
1436
78a5a506 1437 local OPTIONS="tag $CVS_FORCE -b"
ec6b5dfc
AG
1438
1439 # branch exists?
1440 is_tag_a_branch $TAG
1441 if [ $? -eq 1 ]; then
1442 OPTIONS="$OPTIONS -B"
1443 fi
1444
78a5a506
ER
1445 if [ -n "$CVSROOT" ]; then
1446 OPTIONS="-d $CVSROOT $OPTIONS"
1447 fi
9c909460 1448 cd "$PACKAGE_DIR"
78a5a506
ER
1449 local tag_files
1450 for i in $TAG_FILES; do
1451 local fp=`nourl "$i"`
1452 if [ -f "$fp" ]; then
1453 tag_files="$tag_files $fp"
cdbd30e8 1454 elif [ -n "GREEDSRC" ]; then
1455 get_greed_sources $i
78a5a506
ER
1456 else
1457 Exit_error err_no_source_in_repo $i
c10fa4fa 1458 fi
78a5a506
ER
1459 done
1460 if [ "$tag_files" ]; then
939f2c12 1461 $CVS_COMMAND $OPTIONS $TAG $tag_files || exit
78a5a506 1462 fi
c10fa4fa 1463
9c909460 1464 cd "$PACKAGE_DIR"
939f2c12 1465 $CVS_COMMAND $OPTIONS $TAG $SPECFILE || exit
cd445739
AM
1466}
1467
1468
3009b80d
ER
1469# this function should exit early if package can't be built for this arch
1470# this avoids unneccessary BR filling.
1471check_buildarch() {
1472 local out ret
13c3c336 1473 out=$(minirpm --short-circuit -bp --define "'prep exit 0'" --nodeps $SPECFILE 2>&1)
3009b80d
ER
1474 ret=$?
1475 if [ $ret -ne 0 ]; then
1476 echo >&2 "$out"
1477 exit $ret
1478 fi
1479}
1480
9d99a240
ER
1481# from relup.sh
1482set_release() {
1483 local specfile="$1"
1484 local rel="$2"
1485 local newrel="$3"
1486 sed -i -e "
1487 s/^\(%define[ \t]\+_\?rel[ \t]\+\)$rel\$/\1$newrel/
1488 s/^\(Release:[ \t]\+\)$rel\$/\1$newrel/
1489 " $specfile
1490}
1491
1492set_version() {
1493 local specfile="$1"
bc8502e6
ER
1494 local ver="$2" subver=$ver
1495 local newver="$3" newsubver=$newver
1496
1497 # try handling subver, everything that's not numeric-dotted in version
1498 if grep -Eq '%define\s+subver' $specfile; then
1499 subver=$(echo "$ver" | sed -re 's,^[0-9.]+,,')
1500 ver=${ver%$subver}
1501 newsubver=$(echo "$newver" | sed -re 's,^[0-9.]+,,')
1502 newver=${newver%$newsubver}
1503 fi
9d99a240
ER
1504 sed -i -e "
1505 s/^\(%define[ \t]\+_\?ver[ \t]\+\)$ver\$/\1$newver/
bc8502e6 1506 s/^\(%define[ \t]\+subver[ \t]\+\)$subver\$/\1$newsubver/
9d99a240
ER
1507 s/^\(Version:[ \t]\+\)$ver\$/\1$newver/
1508 " $specfile
1509}
1510
cacef28c 1511try_upgrade() {
cd445739 1512 if [ -n "$TRY_UPGRADE" ]; then
9d99a240 1513 local TNOTIFY TNEWVER TOLDVER
ce40491a 1514 update_shell_title "build_package: try_upgrade"
9d99a240 1515
cacef28c 1516 cd "$PACKAGE_DIR"
1517
0907f1eb
ER
1518 if [ "$UPGRADE_VERSION" ]; then
1519 TNEWVER=$UPGRADE_VERSION
cd445739 1520 else
0907f1eb
ER
1521 if [ -n "$FLOAT_VERSION" ]; then
1522 TNOTIFY=$($APPDIR/pldnotify.awk ${BE_VERBOSE:+-vDEBUG=1} $SPECFILE -n) || exit 1
1523 else
1524 TNOTIFY=$($APPDIR/pldnotify.awk ${BE_VERBOSE:+-vDEBUG=1} $SPECFILE) || exit 1
1525 fi
cd445739 1526
b9bef5ce
ER
1527 # pldnotify.awk does not set exit codes, but it has match for ERROR
1528 # in output which means so.
1529 if [[ "$TNOTIFY" = *ERROR* ]]; then
1530 echo >&2 "$TNOTIFY"
1531 exit 1
1532 fi
1533
0907f1eb
ER
1534 TNEWVER=$(echo $TNOTIFY | awk '{ match($4,/\[NEW\]/); print $5 }')
1535 fi
cd445739
AM
1536
1537 if [ -n "$TNEWVER" ]; then
1538 TOLDVER=`echo $TNOTIFY | awk '{ print $3; }'`
9d99a240 1539 echo "New version found, updating spec file from $TOLDVER to version $TNEWVER"
257af81b
ER
1540 if [ "$REVERT_BROKEN_UPGRADE" = "yes" ]; then
1541 cp -f $SPECFILE $SPECFILE.bak
1542 fi
cd445739 1543 chmod +w $SPECFILE
cacef28c 1544 set_release $SPECFILE $PACKAGE_RELEASE 1
9d99a240 1545 set_version $SPECFILE $PACKAGE_VERSION $TNEWVER
518ff1fb 1546 parse_spec
6decb793
ER
1547 if [ "$PACKAGE_VERSION" != "$TNEWVER" ]; then
1548 echo >&2 "Upgrading version failed, you need to update spec yourself"
1549 exit 1
1550 fi
cacef28c 1551 return 1
cd445739
AM
1552 fi
1553 fi
cacef28c 1554 return 0
1555}
1556
1557build_package() {
1558 update_shell_title "build_package"
1559 if [ -n "$DEBUG" ]; then
1560 set -x
1561 set -v
1562 fi
1563
9c909460 1564 cd "$PACKAGE_DIR"
2a5f078d 1565
cd445739
AM
1566 case "$COMMAND" in
1567 build )
1568 BUILD_SWITCH="-ba" ;;
1569 build-binary )
1570 BUILD_SWITCH="-bb" ;;
1571 build-source )
1572 BUILD_SWITCH="-bs --nodeps" ;;
1573 build-prep )
1574 BUILD_SWITCH="-bp --nodeps" ;;
6594293d
AG
1575 build-build )
1576 BUILD_SWITCH="-bc" ;;
1577 build-install )
1578 BUILD_SWITCH="-bi" ;;
1579 build-list )
1580 BUILD_SWITCH="-bl" ;;
1581
cd445739 1582 esac
c890b916 1583
2e5be021 1584 update_shell_title "build_package: $COMMAND"
cd445739
AM
1585 if [ -n "$LOGFILE" ]; then
1586 LOG=`eval echo $LOGFILE`
1587 if [ -d "$LOG" ]; then
1588 echo "Log file $LOG is a directory."
1589 echo "Parse error in the spec?"
518ff1fb 1590 Exit_error err_build_fail
cd445739
AM
1591 fi
1592 if [ -n "$LASTLOG_FILE" ]; then
1593 echo "LASTLOG=$LOG" > $LASTLOG_FILE
1594 fi
390d081d 1595 RES_FILE=$(tempfile)
525bf240 1596
9c909460 1597 (time eval ${NICE_COMMAND} $RPMBUILD $TARGET_SWITCH $BUILD_SWITCH -v $QUIET $CLEAN $RPMOPTS $RPMBUILDOPTS $BCOND --define \'_specdir $PACKAGE_DIR\' --define \'_sourcedir $PACKAGE_DIR\' $SPECFILE; echo $? > $RES_FILE) 2>&1 |tee $LOG
cd445739
AM
1598 RETVAL=`cat $RES_FILE`
1599 rm $RES_FILE
1600 if [ -n "$LOGDIROK" ] && [ -n "$LOGDIRFAIL" ]; then
1601 if [ "$RETVAL" -eq "0" ]; then
1602 mv $LOG $LOGDIROK
1603 else
1604 mv $LOG $LOGDIRFAIL
1605 fi
1606 fi
8ba5cdda 1607 else
9c909460 1608 eval ${NICE_COMMAND} $RPMBUILD $TARGET_SWITCH $BUILD_SWITCH -v $QUIET $CLEAN $RPMOPTS $RPMBUILDOPTS $BCOND --define \'_specdir $PACKAGE_DIR\' --define \'_sourcedir $PACKAGE_DIR\' $SPECFILE
cd445739 1609 RETVAL=$?
8ba5cdda 1610 fi
cd445739
AM
1611 if [ "$RETVAL" -ne "0" ]; then
1612 if [ -n "$TRY_UPGRADE" ]; then
cacef28c 1613 echo "\nUpgrade package to new version failed."
257af81b 1614 if [ "$REVERT_BROKEN_UPGRADE" = "yes" ]; then
cacef28c 1615 echo "Restoring old spec file."
257af81b
ER
1616 mv -f $SPECFILE.bak $SPECFILE
1617 fi
cacef28c 1618 echo ""
cd445739 1619 fi
518ff1fb 1620 Exit_error err_build_fail
cd445739
AM
1621 fi
1622 unset BUILD_SWITCH
1623}
1624
f6711e2a 1625nourl() {
cd445739
AM
1626 echo "$@" | sed 's#\<\(ftp\|http\|https\|cvs\|svn\)://[^ ]*/##g'
1627}
1628
f6711e2a 1629install_required_packages() {
cd445739
AM
1630 run_poldek -vi $1
1631 return $?
1632}
1633
f6711e2a 1634find_spec_bcond() { # originally from /usr/lib/rpm/find-spec-bcond
fa4bf1df 1635 local SPEC="$1"
fa4bf1df
ER
1636 awk -F"\n" '
1637 /^%changelog/ { exit }
fa4bf1df
ER
1638 /^%bcond_with/{
1639 match($0, /bcond_with(out)?[ \t]+[_a-zA-Z0-9]+/);
8ff97f39
ER
1640 bcond = substr($0, RSTART + 6, RLENGTH - 6);
1641 gsub(/[ \t]+/, "_", bcond);
fa4bf1df
ER
1642 print bcond
1643 }' $SPEC | LC_ALL=C sort -u
1644}
1645
117d9861 1646process_bcondrc() {
301cbfd4
ER
1647 # expand bconds from ~/.bcondrc
1648 # The file structure is like gentoo's package.use:
1649 # ---
1650 # * -selinux
1651 # samba -mysql -pgsql
47a8df12 1652 # w32codec-installer license_agreement
301cbfd4
ER
1653 # php +mysqli
1654 # ---
15e34b63 1655 if [ -f $HOME/.bcondrc ] || ([ -n $HOME_ETC ] && [ -f $HOME_ETC/.bcondrc ]); then
117d9861
ER
1656 :
1657 else
1658 return
1659 fi
301cbfd4 1660
117d9861 1661 SN=${SPECFILE%%\.spec}
c8a3e2ac 1662
117d9861
ER
1663 local bcondrc=$HOME/.bcondrc
1664 [ -n $HOME_ETC ] && [ -f $HOME_ETC/.bcondrc ] && bcondrc=$HOME_ETC/.bcondrc
301cbfd4 1665
117d9861 1666 local bcond_avail=$(find_spec_bcond $SPECFILE)
301cbfd4 1667
117d9861
ER
1668 while read pkg flags; do
1669 # ignore comments
1670 [[ "$pkg" == \#* ]] && continue
1671
1672 # any package or current package?
1673 if [ "$pkg" = "*" ] || [ "$pkg" = "$PACKAGE_NAME" ] || [ "$pkg" = "$SN" ]; then
1674 for flag in $flags; do
1675 local opt=${flag#[+-]}
1676
1677 # use only flags which are in this package.
1678 if [[ $bcond_avail = *${opt}* ]]; then
1679 if [[ $flag = -* ]]; then
1680 if [[ $BCOND != *--with?${opt}* ]]; then
1681 BCOND="$BCOND --without $opt"
1682 fi
1683 else
1684 if [[ $BCOND != *--without?${opt}* ]]; then
1685 BCOND="$BCOND --with $opt"
301cbfd4
ER
1686 fi
1687 fi
117d9861
ER
1688 fi
1689 done
1690 fi
1691 done < $bcondrc
1692 update_shell_title "parse ~/.bcondrc: DONE!"
1693}
1694
8ff97f39 1695set_bconds_values() {
117d9861
ER
1696 update_shell_title "set bcond values"
1697
1698 AVAIL_BCONDS_WITHOUT=""
1699 AVAIL_BCONDS_WITH=""
8ff97f39 1700
5a28189c 1701 if grep -Eq '^# *_with' ${SPECFILE}; then
8ff97f39
ER
1702 echo >&2 "ERROR: This spec has old style bconds."
1703 exit 1
1704 fi
1705
1706 if ! grep -q '^%bcond' ${SPECFILE}; then
117d9861 1707 return
301cbfd4
ER
1708 fi
1709
117d9861
ER
1710 local bcond_avail=$(find_spec_bcond $SPECFILE)
1711 process_bcondrc "$SPECFILE"
1712
fa4bf1df 1713 update_shell_title "parse bconds"
2a5f078d 1714
8ff97f39
ER
1715 local opt bcond
1716 for opt in $bcond_avail; do
1717 case "$opt" in
1718 without_*)
1719 bcond=${opt#without_}
10d92f71
ER
1720 case "$BCOND" in
1721 *--without?${bcond}*)
8ff97f39 1722 AVAIL_BCONDS_WITHOUT="$AVAIL_BCONDS_WITHOUT <$bcond>"
10d92f71
ER
1723 ;;
1724 *)
8ff97f39 1725 AVAIL_BCONDS_WITHOUT="$AVAIL_BCONDS_WITHOUT $bcond"
10d92f71
ER
1726 ;;
1727 esac
cd445739 1728 ;;
8ff97f39
ER
1729 with_*)
1730 bcond=${opt#with_}
10d92f71
ER
1731 case "$BCOND" in
1732 *--with?${bcond}*)
8ff97f39 1733 AVAIL_BCONDS_WITH="$AVAIL_BCONDS_WITH <$bcond>"
10d92f71
ER
1734 ;;
1735 *)
8ff97f39 1736 AVAIL_BCONDS_WITH="$AVAIL_BCONDS_WITH $bcond"
10d92f71
ER
1737 ;;
1738 esac
cd445739 1739 ;;
8ff97f39
ER
1740 *)
1741 echo >&2 "ERROR: unexpected '$opt' in set_bconds_values"
1742 exit 1
1743 ;;
1744 esac
1745 done
cd445739
AM
1746}
1747
f6711e2a 1748run_sub_builder() {
cd445739 1749 package_name="${1}"
af6b168f 1750 update_shell_title "run_sub_builder $package_name"
cd445739
AM
1751 #
1752