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