diff options
author | Håkon Hallingstad <hakon@oath.com> | 2017-12-04 10:06:48 +0100 |
---|---|---|
committer | Håkon Hallingstad <hakon@oath.com> | 2017-12-04 10:06:48 +0100 |
commit | b9ca52f0a1fdde9c26e526b3d935c2a01711e1c2 (patch) | |
tree | 3d59c16199a5c0d4fd82ca41e38337ebef35f130 /dist | |
parent | 42305213fb1584fdcf8bee8e0142727fe74a8874 (diff) |
Move build-rpm to dist
Diffstat (limited to 'dist')
-rwxr-xr-x | dist/build-rpm.sh | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/dist/build-rpm.sh b/dist/build-rpm.sh new file mode 100755 index 00000000000..e86eebe9380 --- /dev/null +++ b/dist/build-rpm.sh @@ -0,0 +1,124 @@ +#!/bin/bash + +set -e + +Usage() { + cat <<EOF +Usage: ${0##*/} [OPTIONS]...SPECFILE +Run rpmbuild with the given specfile macros, creating TOPDIR if necessary. + +Options: + -b BUILDDIR Overrides %_builddir. + -d DIST The %dist to build for (e.g. .el7 for RHEL 7). Can be specified + multiple time to build multiple RPMs. The default %dist is used + if no -d options have been specified. + -h Print this help text and exit. + -t TOPDIR Overrides %_topdir. + -v VERSION [Required] The version of the RPM. +EOF + + exit 1 +} + +Fail() { + printf "%s\n" "$*" + exit 1 +} + +Run() { + local command="$1" + shift + printf "%q" "$command" + + local arg + for arg in "$@"; do + printf " %q" "$arg" + done + printf "\n" + + "$command" "$@" +} + +Main() { + local -a dists=() + local version= topdir= builddir= + while (( $# > 0 )); do + case "$1" in + -b|--builddir) + builddir="$2" + shift 2 + if ! test -d "$builddir"; then + Fail "BUILDDIR '$builddir' does not exist" + # Make builddir an absolute path + elif ! builddir=$(readlink -e "$builddir"); then + Fail "Failed to resolve BUILDDIR '$builddir'" + fi + ;; + -d|--dist) + local dist="$2" + shift 2 + case "$dist" in + .el6|.el7) : ;; + *) Fail "Bad DIST value '$dist'" ;; + esac + dists+=("$dist") + ;; + -t|--topdir) + topdir="$2" + shift 2 + if ! [[ "$topdir" =~ ^/ ]]; then + Fail "TOPDIR must be an absolute path" + fi + ;; + -v|--version) + version="$2" + shift 2 + if ! [[ "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then + Fail "VERSION must be a version of the form X.Y.Z" + fi + ;; + *) break ;; + esac + done + + if (( $# == 0 )); then + Fail "Missing SPECFILE" + elif (( $# > 1 )); then + Fail "Too many arguments" + else + case "$1" in + help|-h|--help) Usage ;; + esac + fi + local specfile="$1" + + local -a defines=() + + if test -n "$builddir"; then + defines+=(--define "_builddir $builddir") + fi + + if test -n "$topdir"; then + if ! mkdir -p "$topdir"; then + Fail "Failed to create TOPDIR directory '$topdir'" + fi + defines+=(--define "_topdir $topdir") + fi + + if test -n "$version"; then + defines+=(--define "version $version") + else + Fail "VERSION is required" + fi + + if (( ${#dists[@]} == 0 )); then + Run rpmbuild -bb "${defines[@]}" "$specfile" + else + local dist + for dist in "${dists[@]}"; do + Run rpmbuild -bb "${defines[@]}" --define "dist $dist" "$specfile" + done + fi +} + +Main "$@" |