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