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