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