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