Use srpm building tools
[Samba/vfs_proxy.git] / packaging4 / utils / make-srpm
blob3feedba45c88b4e867e89a73fe33e02953b239bd
1 #! /bin/bash
2 # make-srpm: srpm builder, helps build srpm out of rcs sources
4 # Copyright (C) 2008 Sam Liddicott <sam@liddicott.com>
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 # make-srpm will export from git or subversion and produce a src.rpm
21 # It will try and pick a recent tagged release for the "pristine source" and
22 # then produce patches up to the selected release as part of the rpm.
24 # It requires a .spec or .spec.in specfile template, from which it can also
25 # read some configuration, but this can be overridden on the command line
27 # arguments can be specified in the template spec file, or in the environment
28 # or as name=value pairs on the command line. (Once a non name=value argument
29 # is found, others are not looked for).
31 # $1 should be a spec file template
33 # arguments are:
34 # ORIGIN - an rcs reference to the revision that should be tar'd
35 # ORIGIN_PATTERN - a glob or regex used to look for rcs tags to guess ORIGIN
36 # RELEASE - an rcs reference to the revision you want to build. Patches
37 # are generated from ORIGIN to RELEASE. RELEASE can be:
38 # - HEAD whatever os checked out
39 # - LOCAL means whatever is checked out + local changes
41 #include svn and git helpers
43 PATH="$PATH:`dirname $0`" . make-srpm_git
44 PATH="$PATH:`dirname $0`" . make-srpm_svn
46 die() {
47 echo "!! $@" >&2
48 exit 1
51 report() {
52 echo "-- $@" >&2
55 # try to find a checked-out project src dir
56 find_topdir() {
57 # stop when we find the git folder or the top level svn folder
58 while :
60 echo "Checking for top level source directory in $PWD" >&2
61 git_toplevel && break # found top level git
62 svn_toplevel && break # found top level svn
64 if ! cd .. || test "$PWD" = "/"
65 then
66 report "Can't find top level repository directory, using $BASED"
67 cd "$BASED"
68 break
70 done
71 echo "$PWD"
74 # read a config item from a file and set environment variables.
75 # e.g.
76 # get_config Version
77 # sep="=" get_config PATH
78 # prefix="#mksrpm-" RELEASE
79 get_config() {
80 for g in "$@"
82 if test -z "${!g}"
83 then export $g="`sed "/^$prefix$g *${sep:-:} */!d;s/^[^${sep:-:}]*${sep:-:} *//;" "$SPEC_SRC"`"
85 done
88 # generate the $SPEC spec file from $SPEC.in
89 make_spec() {
90 _SPEC="$SRC_EXPORT/`basename "$SPECIN" .in`"
92 cat "$SPEC_SRC" > "$_SPEC" || die "Can't copy $SPECIN to $_SPEC"
94 report "Updating $_SPEC"
95 sed -i -e "1i%define makesrpm_tarname $TAR_NAME" \
96 -e "1i%define makesrpm_tarprefix $TAR_PREFIX" \
97 -e "s/^\(Version: *\).*/\1$SPEC_VERSION/" \
98 -e "s/^\(Release: *\).*/\1${SPEC_RELEASE:-0}/" \
99 -e "s/^\(Source: *\).*/\1$TAR_NAME/" \
100 -e "/^Source: /r$SRC_EXPORT/patches.list" \
101 -e "/^%setup/r$SRC_EXPORT/patches.apply" \
102 "$_SPEC"
103 for define in "${defines[@]}"
105 if grep "^%define ${define%%=*} " 2>/dev/null "$_SPEC"
106 then sed -i -e "/^%define[ \t][ \t]*${define%%=*}[ \t]/c%define ${define%%=*} ${define#*=}" "$_SPEC"
107 else sed -i -e "1i%define ${define%%=*} ${define#*=}" "$_SPEC"
109 done
110 # _defines are like defines except subject to variable interpolation so
111 # that inner values like TAR_NAME can be accessed
112 for define in "${_defines[@]}"
114 eval "define=$define"
115 if grep "^%define ${define%%=*} " 2>/dev/null "$_SPEC"
116 then sed -i -e "/^%define[ \t][ \t]*${define%%=*}[ \t]/c%define ${define%%=*} ${define#*=}" "$_SPEC"
117 else sed -i -e "1i%define ${define%%=*} ${define#*=}" "$_SPEC"
119 done
122 # export current checked out source to file "$2" with tar path prefix of $1
123 source_export() {
124 mkdir -p "$1"
125 # we want to specify the top-level folder name so we use a ghastly
126 # cpio hard-link trick to avoid copying the whole tree before tar-ing it
127 find ${SOURCE_PATHS:-.} '(' -wholename "$SRC_EXPORT*" -o -wholename "./.git*" -o -name '*.o' \
128 -o -name '*.a' -o -name '*.so' -o -name '.svn' \
129 -o -name "$Name-*gz" -o -name "$Name-*src.rpm" \
130 ')' -prune -o -print |\
131 cpio -p -d -l "$1" >/dev/null
133 source_archive() {
134 source_export "$SRC_EXPORT/$1" &&
135 ( cd "$SRC_EXPORT" && tar -czf - "1"; rm -fr "$1") > "$2"
138 # export_archive exports some "clean" source tree to $TAR_NAME
139 archive_version() {
140 report "Writing $SOURCE_TYPE to $TAR_NAME"
141 case "$SOURCE_TYPE" in
142 git) git_archive "$DIRNAME" "$TAR_NAME" ;;
143 svn) svn_archive "$DIRNAME" "$TAR_NAME" ;;
144 source) source_archive "$DIRNAME" "$TAR_NAME" ;;
145 *) die "Can't export from unknown source type $SOURCE_TYPE";;
146 esac
149 # export_src exports some "clean" source tree to $SRC_EXPORT
150 export_version() {
151 report "Exporting $SOURCE_TYPE to $1"
152 case "$SOURCE_TYPE" in
153 git) git_export "$1" ;;
154 svn) svn_export "$1" ;;
155 source) source_export "$1";;
156 *) die "Can't export from unknown source type $SOURCE_TYPE";;
157 esac
160 make_srpm() {
161 rm -fr "$SRC_EXPORT"
162 mkdir -p "$SRC_EXPORT"
164 # copy any local dependancies
165 cp `dirname "$SPEC_SRC"`/* "$SRC_EXPORT"
167 # what number should our patches start at?
168 START_PATCH_NO=$(( 1 + 0`sed -n 's/^\(%\|\)\(patch\)//i;T;s/[: ].*//;T;p' "$SPEC_SRC" | sort -nr | head -1` ))
170 # get patches ready according to rebase, origin etc
171 case "$SOURCE_TYPE" in
172 git) prepare_git_src;;
173 svn) prepare_svn_src;;
174 *);;
175 esac
177 # make spec file and calculate START_PATCH_NO
178 TAR_NAME="`basename "$TAR_NAME"`" make_spec
180 rpmbuild --define "_sourcedir $SRC_EXPORT" --define "_srcrpmdir ." -bs "$SRC_EXPORT/`basename "$SPEC"`" >&2 || "Failed to build src.rpm"
183 # set environment from args of style var=value
184 while test -n "$*"
186 case "$1" in
187 define_*=*) defines[${#defines[@]}]="${1#define_}"; shift;;
188 _define_*=*) _defines[${#_defines[@]}]="${1#_define_}"; shift;;
189 *=*) export "${1%%=*}"="${1#*=}"; shift;;
190 *) break;;
191 esac
192 done
194 VERSION=${VERSION%%-*}
196 # get to the top level project folder
197 BASED="$PWD"
198 test -n "$TOPDIR" || TOPDIR="`find_topdir`"
199 cd "$TOPDIR" || die "Can't cd to $TOPDIR from $BASED"
201 if [ "$SOURCE_TYPE" = "" ]
202 then
204 SOURCE_TYPE="source"
205 test -d .svn && SOURCE_TYPE="svn"
206 test -d .git && SOURCE_TYPE="git"
209 echo "Source type: $SOURCE_TYPE"
211 SRC_EXPORT="./dist_export"
212 TMPDIR="$SRC_EXPORT"
213 rm -fr "$TMPDIR"
214 mkdir -p "$TMPDIR"
216 for spec in "$@"
217 do (
218 # fixup relative paths to what PWD was when we launched
219 case "$spec" in
220 /*) ;;
221 *) spec="$BASED/$spec";;
222 esac
224 case "$spec" in
225 *.spec.in) test -z "$SPECIN" && SPECIN="$spec";;
226 *.spec) test -z "$SPEC" && SPEC="$spec";;
227 esac
229 test -z "$SPECIN" -a -n "$SPEC" && SPECIN="$SPEC.in"
230 test -z "$SPECIN" && SPECIN="`set -- *spec.in && echo $spec`"
231 test -z "$SPEC" && SPEC="`dirname "$SPECIN"`/`basename "$SPECIN" .in`"
232 test -r "$SPECIN" -o -r "$SPEC" || die "Can't find spec file to work with"
233 # find the project parameters
234 test -r "$SPECIN" && SPEC_SRC="$SPECIN" || SPEC_SRC="$SPEC"
236 CASE="i" get_config Name Version
237 prefix="#makesrpm-" get_config SOURCE_PATHS ORIGIN ORIGIN_PATTERN RELEASE
239 make_srpm
240 ) ; done