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
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
55 # try to find a checked-out project src dir
57 # stop when we find the git folder or the top level svn folder
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" = "/"
66 report
"Can't find top level repository directory, using $BASED"
74 # read a config item from a file and set environment variables.
77 # sep="=" get_config PATH
78 # prefix="#mksrpm-" RELEASE
83 then export $g="`sed "/^
$prefix$g *${sep:-:} */!d;s/^[^${sep:-:}]*${sep:-:} *//;" "$SPEC_SRC"`"
88 # generate the $SPEC spec file from $SPEC.in
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" \
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"
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"
122 # export current checked out source to file "$2" with tar path prefix of $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
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
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";;
149 # export_src exports some "clean" source tree to $SRC_EXPORT
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";;
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
;;
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
187 define_
*=*) defines
[${#defines[@]}]="${1#define_}"; shift;;
188 _define_
*=*) _defines
[${#_defines[@]}]="${1#_define_}"; shift;;
189 *=*) export "${1%%=*}"="${1#*=}"; shift;;
194 VERSION
=${VERSION%%-*}
196 # get to the top level project folder
198 test -n "$TOPDIR" || TOPDIR
="`find_topdir`"
199 cd "$TOPDIR" || die
"Can't cd to $TOPDIR from $BASED"
201 if [ "$SOURCE_TYPE" = "" ]
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"
218 # fixup relative paths to what PWD was when we launched
221 *) spec
="$BASED/$spec";;
225 *.spec.
in) test -z "$SPECIN" && SPECIN
="$spec";;
226 *.spec
) test -z "$SPEC" && SPEC
="$spec";;
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