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