08bafd
From f2357a2b5721cddb0a8924be76d0cdc9412d59dc Mon Sep 17 00:00:00 2001
08bafd
From: "Endi S. Dewata" <edewata@redhat.com>
08bafd
Date: Thu, 21 Jun 2018 21:41:21 -0600
08bafd
Subject: [PATCH] Added build.sh
08bafd
08bafd
A new build script has been added to simplify the build process.
08bafd
---
08bafd
 build.sh | 366 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
08bafd
 1 file changed, 366 insertions(+)
08bafd
 create mode 100755 build.sh
08bafd
08bafd
diff --git a/build.sh b/build.sh
08bafd
new file mode 100755
08bafd
index 00000000..ebd46e97
08bafd
--- /dev/null
08bafd
+++ b/build.sh
08bafd
@@ -0,0 +1,366 @@
08bafd
+#!/bin/bash -e
08bafd
+
08bafd
+# BEGIN COPYRIGHT BLOCK
08bafd
+# (C) 2018 Red Hat, Inc.
08bafd
+# All rights reserved.
08bafd
+# END COPYRIGHT BLOCK
08bafd
+
08bafd
+NAME=jss
08bafd
+
08bafd
+SCRIPT_PATH=`readlink -f "$0"`
08bafd
+SCRIPT_NAME=`basename "$SCRIPT_PATH"`
08bafd
+
08bafd
+SRC_DIR=`dirname "$SCRIPT_PATH"`
08bafd
+WORK_DIR="$HOME/build/$NAME"
08bafd
+
08bafd
+SOURCE_TAG=
08bafd
+
08bafd
+WITH_TIMESTAMP=
08bafd
+WITH_COMMIT_ID=
08bafd
+DIST=
08bafd
+
08bafd
+VERBOSE=
08bafd
+DEBUG=
08bafd
+
08bafd
+usage() {
08bafd
+    echo "Usage: $SCRIPT_NAME [OPTIONS] <target>"
08bafd
+    echo
08bafd
+    echo "Options:"
08bafd
+    echo "    --work-dir=<path>      Working directory (default: $WORK_DIR)."
08bafd
+    echo "    --source-tag=<tag>     Generate RPM sources from a source tag."
08bafd
+    echo "    --with-timestamp       Append timestamp to release number."
08bafd
+    echo "    --with-commit-id       Append commit ID to release number."
08bafd
+    echo "    --dist=<name>          Distribution name (e.g. fc28)."
08bafd
+    echo " -v,--verbose              Run in verbose mode."
08bafd
+    echo "    --debug                Run in debug mode."
08bafd
+    echo "    --help                 Show help message."
08bafd
+    echo
08bafd
+    echo "Target:"
08bafd
+    echo "    src    Generate RPM sources."
08bafd
+    echo "    spec   Generate RPM spec."
08bafd
+    echo "    srpm   Build SRPM package."
08bafd
+    echo "    rpm    Build RPM packages."
08bafd
+}
08bafd
+
08bafd
+generate_rpm_sources() {
08bafd
+
08bafd
+    TARBALL="$NAME-$VERSION.tar.gz"
08bafd
+
08bafd
+    if [ "$SOURCE_TAG" != "" ] ; then
08bafd
+
08bafd
+        if [ "$VERBOSE" = true ] ; then
08bafd
+            echo "Generating $TARBALL from $SOURCE_TAG tag"
08bafd
+        fi
08bafd
+
08bafd
+        git -C "$SRC_DIR" \
08bafd
+            archive \
08bafd
+            --format=tar.gz \
08bafd
+            --prefix $NAME-$VERSION/$NAME/ \
08bafd
+            -o "$WORK_DIR/SOURCES/$TARBALL" \
08bafd
+            $SOURCE_TAG
08bafd
+
08bafd
+        if [ "$SOURCE_TAG" != "HEAD" ] ; then
08bafd
+
08bafd
+            TAG_ID=`git -C "$SRC_DIR" rev-parse $SOURCE_TAG`
08bafd
+            HEAD_ID=`git -C "$SRC_DIR" rev-parse HEAD`
08bafd
+
08bafd
+            if [ "$TAG_ID" != "$HEAD_ID" ] ; then
08bafd
+                generate_patch
08bafd
+            fi
08bafd
+        fi
08bafd
+
08bafd
+        return
08bafd
+    fi
08bafd
+
08bafd
+    if [ "$VERBOSE" = true ] ; then
08bafd
+        echo "Generating $TARBALL"
08bafd
+    fi
08bafd
+
08bafd
+    tar czf "$WORK_DIR/SOURCES/$TARBALL" \
08bafd
+        --transform "s,^./,$NAME-$VERSION/$NAME/," \
08bafd
+        --exclude .git \
08bafd
+        --exclude bin \
08bafd
+        -C "$SRC_DIR" \
08bafd
+        .
08bafd
+}
08bafd
+
08bafd
+generate_patch() {
08bafd
+
08bafd
+    PATCH="$NAME-$VERSION-$RELEASE.patch"
08bafd
+
08bafd
+    if [ "$VERBOSE" = true ] ; then
08bafd
+        echo "Generating $PATCH for all changes since $SOURCE_TAG tag"
08bafd
+    fi
08bafd
+
08bafd
+    git -C "$SRC_DIR" \
08bafd
+        format-patch \
08bafd
+        --stdout \
08bafd
+        $SOURCE_TAG \
08bafd
+        > "$WORK_DIR/SOURCES/$PATCH"
08bafd
+}
08bafd
+
08bafd
+generate_rpm_spec() {
08bafd
+
08bafd
+    RPM_SPEC="$NAME.spec"
08bafd
+
08bafd
+    if [ "$VERBOSE" = true ] ; then
08bafd
+        echo "Generating $RPM_SPEC"
08bafd
+    fi
08bafd
+
08bafd
+    # hard-code timestamp
08bafd
+    commands="s/%{?_timestamp}/${_TIMESTAMP}/g"
08bafd
+
08bafd
+    # hard-code commit ID
08bafd
+    commands="${commands}; s/%{?_commit_id}/${_COMMIT_ID}/g"
08bafd
+
08bafd
+    # hard-code patch
08bafd
+    if [ "$PATCH" != "" ] ; then
08bafd
+        commands="${commands}; s/# Patch: jss-VERSION-RELEASE.patch/Patch: $PATCH/g"
08bafd
+    fi
08bafd
+
08bafd
+    sed "$commands" "$SPEC_TEMPLATE" > "$WORK_DIR/SPECS/$RPM_SPEC"
08bafd
+
08bafd
+    # rpmlint "$WORK_DIR/SPECS/$RPM_SPEC"
08bafd
+}
08bafd
+
08bafd
+while getopts v-: arg ; do
08bafd
+    case $arg in
08bafd
+    v)
08bafd
+        VERBOSE=true
08bafd
+        ;;
08bafd
+    -)
08bafd
+        LONG_OPTARG="${OPTARG#*=}"
08bafd
+
08bafd
+        case $OPTARG in
08bafd
+        work-dir=?*)
08bafd
+            WORK_DIR=`readlink -f "$LONG_OPTARG"`
08bafd
+            ;;
08bafd
+        source-tag=?*)
08bafd
+            SOURCE_TAG="$LONG_OPTARG"
08bafd
+            ;;
08bafd
+        with-timestamp)
08bafd
+            WITH_TIMESTAMP=true
08bafd
+            ;;
08bafd
+        with-commit-id)
08bafd
+            WITH_COMMIT_ID=true
08bafd
+            ;;
08bafd
+        dist=?*)
08bafd
+            DIST="$LONG_OPTARG"
08bafd
+            ;;
08bafd
+        verbose)
08bafd
+            VERBOSE=true
08bafd
+            ;;
08bafd
+        debug)
08bafd
+            VERBOSE=true
08bafd
+            DEBUG=true
08bafd
+            ;;
08bafd
+        help)
08bafd
+            usage
08bafd
+            exit
08bafd
+            ;;
08bafd
+        '')
08bafd
+            break # "--" terminates argument processing
08bafd
+            ;;
08bafd
+        work-dir* | source-tag* | dist*)
08bafd
+            echo "ERROR: Missing argument for --$OPTARG option" >&2
08bafd
+            exit 1
08bafd
+            ;;
08bafd
+        *)
08bafd
+            echo "ERROR: Illegal option --$OPTARG" >&2
08bafd
+            exit 1
08bafd
+            ;;
08bafd
+        esac
08bafd
+        ;;
08bafd
+    \?)
08bafd
+        exit 1 # getopts already reported the illegal option
08bafd
+        ;;
08bafd
+    esac
08bafd
+done
08bafd
+
08bafd
+# remove parsed options and args from $@ list
08bafd
+shift $((OPTIND-1))
08bafd
+
08bafd
+if [ "$#" -lt 1 ] ; then
08bafd
+    echo "ERROR: Missing build target" >&2
08bafd
+    usage
08bafd
+    exit 1
08bafd
+fi
08bafd
+
08bafd
+BUILD_TARGET=$1
08bafd
+
08bafd
+if [ "$DEBUG" = true ] ; then
08bafd
+    echo "WORK_DIR: $WORK_DIR"
08bafd
+    echo "BUILD_TARGET: $BUILD_TARGET"
08bafd
+fi
08bafd
+
08bafd
+if [ "$BUILD_TARGET" != "src" ] &&
08bafd
+        [ "$BUILD_TARGET" != "spec" ] &&
08bafd
+        [ "$BUILD_TARGET" != "srpm" ] &&
08bafd
+        [ "$BUILD_TARGET" != "rpm" ] ; then
08bafd
+    echo "ERROR: Invalid build target: $BUILD_TARGET" >&2
08bafd
+    exit 1
08bafd
+fi
08bafd
+
08bafd
+SPEC_TEMPLATE="$SRC_DIR/$NAME.spec.in"
08bafd
+VERSION="`rpmspec -P "$SPEC_TEMPLATE" | grep "^Version:" | awk '{print $2;}'`"
08bafd
+
08bafd
+if [ "$DEBUG" = true ] ; then
08bafd
+    echo "VERSION: $VERSION"
08bafd
+fi
08bafd
+
08bafd
+RELEASE="`rpmspec -P "$SPEC_TEMPLATE" --undefine dist | grep "^Release:" | awk '{print $2;}'`"
08bafd
+
08bafd
+if [ "$DEBUG" = true ] ; then
08bafd
+    echo "RELEASE: $RELEASE"
08bafd
+fi
08bafd
+
08bafd
+if [ "$WITH_TIMESTAMP" = true ] ; then
08bafd
+    TIMESTAMP="`date +"%Y%m%d%H%M%S"`"
08bafd
+    _TIMESTAMP=".$TIMESTAMP"
08bafd
+fi
08bafd
+
08bafd
+if [ "$DEBUG" = true ] ; then
08bafd
+    echo "TIMESTAMP: $TIMESTAMP"
08bafd
+fi
08bafd
+
08bafd
+if [ "$WITH_COMMIT_ID" = true ]; then
08bafd
+    COMMIT_ID="`git -C "$SRC_DIR" rev-parse --short=8 HEAD`"
08bafd
+    _COMMIT_ID=".$COMMIT_ID"
08bafd
+fi
08bafd
+
08bafd
+if [ "$DEBUG" = true ] ; then
08bafd
+    echo "COMMIT_ID: $COMMIT_ID"
08bafd
+fi
08bafd
+
08bafd
+echo "Building $NAME-$VERSION-$RELEASE${_TIMESTAMP}${_COMMIT_ID}"
08bafd
+
08bafd
+################################################################################
08bafd
+# Initialize working directory
08bafd
+################################################################################
08bafd
+
08bafd
+if [ "$VERBOSE" = true ] ; then
08bafd
+    echo "Initializing $WORK_DIR"
08bafd
+fi
08bafd
+
08bafd
+mkdir -p $WORK_DIR
08bafd
+cd $WORK_DIR
08bafd
+
08bafd
+rm -rf BUILD
08bafd
+rm -rf RPMS
08bafd
+rm -rf SOURCES
08bafd
+rm -rf SPECS
08bafd
+rm -rf SRPMS
08bafd
+
08bafd
+mkdir BUILD
08bafd
+mkdir RPMS
08bafd
+mkdir SOURCES
08bafd
+mkdir SPECS
08bafd
+mkdir SRPMS
08bafd
+
08bafd
+################################################################################
08bafd
+# Generate RPM sources
08bafd
+################################################################################
08bafd
+
08bafd
+generate_rpm_sources
08bafd
+
08bafd
+echo "RPM sources:"
08bafd
+find "$WORK_DIR/SOURCES" -type f -printf " %p\n"
08bafd
+
08bafd
+if [ "$BUILD_TARGET" = "src" ] ; then
08bafd
+    exit
08bafd
+fi
08bafd
+
08bafd
+################################################################################
08bafd
+# Generate RPM spec
08bafd
+################################################################################
08bafd
+
08bafd
+generate_rpm_spec
08bafd
+
08bafd
+echo "RPM spec:"
08bafd
+find "$WORK_DIR/SPECS" -type f -printf " %p\n"
08bafd
+
08bafd
+if [ "$BUILD_TARGET" = "spec" ] ; then
08bafd
+    exit
08bafd
+fi
08bafd
+
08bafd
+################################################################################
08bafd
+# Build source package
08bafd
+################################################################################
08bafd
+
08bafd
+OPTIONS=()
08bafd
+
08bafd
+OPTIONS+=(--quiet)
08bafd
+OPTIONS+=(--define "_topdir ${WORK_DIR}")
08bafd
+
08bafd
+if [ "$WITH_TIMESTAMP" = true ] ; then
08bafd
+    OPTIONS+=(--define "_timestamp ${_TIMESTAMP}")
08bafd
+fi
08bafd
+
08bafd
+if [ "$WITH_COMMIT_ID" = true ] ; then
08bafd
+    OPTIONS+=(--define "_commit_id ${_COMMIT_ID}")
08bafd
+fi
08bafd
+
08bafd
+if [ "$DIST" != "" ] ; then
08bafd
+    OPTIONS+=(--define "dist .$DIST")
08bafd
+fi
08bafd
+
08bafd
+if [ "$DEBUG" = true ] ; then
08bafd
+    echo "rpmbuild -bs ${OPTIONS[@]} $WORK_DIR/SPECS/$RPM_SPEC"
08bafd
+fi
08bafd
+
08bafd
+# build SRPM with user-provided options
08bafd
+rpmbuild -bs "${OPTIONS[@]}" "$WORK_DIR/SPECS/$RPM_SPEC"
08bafd
+
08bafd
+rc=$?
08bafd
+
08bafd
+if [ $rc != 0 ]; then
08bafd
+    echo "ERROR: Unable to build SRPM package"
08bafd
+    exit 1
08bafd
+fi
08bafd
+
08bafd
+SRPM=`find "$WORK_DIR/SRPMS" -type f`
08bafd
+
08bafd
+echo "SRPM package:"
08bafd
+echo " $SRPM"
08bafd
+
08bafd
+if [ "$BUILD_TARGET" = "srpm" ] ; then
08bafd
+    exit
08bafd
+fi
08bafd
+
08bafd
+################################################################################
08bafd
+# Build binary packages
08bafd
+################################################################################
08bafd
+
08bafd
+OPTIONS=()
08bafd
+
08bafd
+if [ "$VERBOSE" = true ] ; then
08bafd
+    OPTIONS+=(--define "_verbose 1")
08bafd
+fi
08bafd
+
08bafd
+OPTIONS+=(--define "_topdir ${WORK_DIR}")
08bafd
+
08bafd
+if [ "$DEBUG" = true ] ; then
08bafd
+    echo "rpmbuild --rebuild ${OPTIONS[@]} $SRPM"
08bafd
+fi
08bafd
+
08bafd
+# rebuild RPM with hard-coded options in SRPM
08bafd
+rpmbuild --rebuild "${OPTIONS[@]}" "$SRPM"
08bafd
+
08bafd
+rc=$?
08bafd
+
08bafd
+if [ $rc != 0 ]; then
08bafd
+    echo "ERROR: Unable to build RPM packages"
08bafd
+    exit 1
08bafd
+fi
08bafd
+
08bafd
+# install SRPM to restore sources and spec file removed during rebuild
08bafd
+rpm -i --define "_topdir $WORK_DIR" "$SRPM"
08bafd
+
08bafd
+# flatten folder
08bafd
+find "$WORK_DIR/RPMS" -mindepth 2 -type f -exec mv -i '{}' "$WORK_DIR/RPMS" ';'
08bafd
+
08bafd
+# remove empty subfolders
08bafd
+find "$WORK_DIR/RPMS" -mindepth 1 -type d -delete
08bafd
+
08bafd
+echo "RPM packages:"
08bafd
+find "$WORK_DIR/RPMS" -type f -printf " %p\n"
08bafd
-- 
08bafd
2.14.4
08bafd