Skip to content

Commit

Permalink
Build RPM packages.
Browse files Browse the repository at this point in the history
Both .deb and .rpm are copied to target/packages.
  • Loading branch information
ayosec committed Aug 26, 2021
1 parent 83c342e commit e4503e2
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 31 deletions.
21 changes: 21 additions & 0 deletions pkg/common/metadata
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash
#
# Load fields from the `[package]` section of the `Cargo.toml` file.
#
# For every field, exports a `PACKAGE_<NAME>` variable.

METADATA_TABLE=''

cat "$(git rev-parse --show-toplevel)/Cargo.toml" |
while read -r LINE
do
if [[ $LINE =~ ^\[(.*)\] ]]
then
METADATA_TABLE="${BASH_REMATCH[1]}"
elif [ "$METADATA_TABLE" = package ] &&
[[ $LINE =~ ^([^[:space:]]*)[[:space:]]*=[[:space:]]*\"(.*)\" ]]
then
METADATA_VARNAME="PACKAGE_${BASH_REMATCH[1]^^}"
printf 'export %s=%q\n' "$METADATA_VARNAME" "${BASH_REMATCH[2]}"
fi
done
38 changes: 7 additions & 31 deletions pkg/debian/build.sh
Original file line number Diff line number Diff line change
@@ -1,40 +1,19 @@
#!/bin/bash
#
# This script generates a debian package with the .so file built from
# This script generates a Debian package with the .so file built from
# cargo build --release.
#
# The .deb files are copied to '$ROOT/target/debian/$VERSION'.

# The .deb files are copied to '$ROOT/target/packages'.

set -euo pipefail

SOURCE=$(git rev-parse --show-toplevel)



# Collect data from Cargo.toml
#
# For every field under '[package]', generates a variable PACKAGE_$FIELD=$VALUE.
# This variable is exported, so it can be used by envsubst to generate the files
# for the Debian scripts.

TABLE=''
PACKAGE_NAME=''
SOURCE=$(git rev-parse --show-toplevel)

while read -r LINE
do
if [[ $LINE =~ ^\[(.*)\] ]]
then
TABLE="${BASH_REMATCH[1]}"
elif [ "$TABLE" = package ] &&
[[ $LINE =~ ^([^[:space:]]*)[[:space:]]*=[[:space:]]*\"(.*)\" ]]
then
VARNAME="PACKAGE_${BASH_REMATCH[1]^^}"
declare "$VARNAME=${BASH_REMATCH[2]}"
export "${VARNAME?}"
fi
done < "$SOURCE/Cargo.toml"
cd "$(dirname "$0")"

# shellcheck source=/dev/null
source <(../common/metadata)


# Build the shared library.
Expand All @@ -43,7 +22,6 @@ cd "$SOURCE"
cargo build --release



# Copy the generated files to the destination.
#
# .md files in the root are copied as .txt files to the
Expand All @@ -54,7 +32,7 @@ DEST=$(mktemp -d)
DEST_BIN="$DEST/debian/$PACKAGE_NAME/usr/lib/bash"
DEST_DOC="$DEST/debian/$PACKAGE_NAME/usr/share/doc/$PACKAGE_NAME"

DEST_DEB="$SOURCE/target/debian/$PACKAGE_VERSION"
DEST_DEB="$SOURCE/target/packages"

mkdir -p "$DEST_BIN"
find target/release -maxdepth 1 -name '*.so' -exec cp -a {} "$DEST_BIN" \;
Expand All @@ -66,7 +44,6 @@ rename.ul .md .txt "$DEST_DOC"/*.md
mkdir -p "$DEST_DEB"



# Generate files for the Debian scripts.

PACKAGE_AUTHOR=$(git log --pretty='%an <%ae>' -1 pkg/debian)
Expand All @@ -78,7 +55,6 @@ envsubst < pkg/debian/control > "$DEST/debian/control"
envsubst < pkg/debian/changelog > "$DEST/debian/changelog"



# Build the package.

set -x
Expand Down
21 changes: 21 additions & 0 deletions pkg/rpm/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash
#
# This script generates a RPM package with the .so file built from
# cargo build --release.

set -euo pipefail

cd "$(dirname "$0")"

# shellcheck source=/dev/null
source <(../common/metadata)

DEST=$(mktemp -d)

envsubst < timehistory.spec > "$DEST/timehistory.spec"

cd "$(git rev-parse --show-toplevel)"
HOME="$DEST" rpmbuild --build-in-place -bb "$DEST/timehistory.spec"

mkdir -p target/packages
find "$DEST/rpmbuild/RPMS" -type f -name '*.rpm' -exec cp -t target/packages {} +
18 changes: 18 additions & 0 deletions pkg/rpm/timehistory.spec
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Name: $PACKAGE_NAME
Release: 1%{?dist}
Version: $PACKAGE_VERSION
Summary: $PACKAGE_DESCRIPTION
License: $PACKAGE_LICENSE

%description
A loadable builtin that records the resources used by every executed program,
and report them in a custom format string (like GNU time) or in JSON.

%build
cargo build --release

%install
install -t %{buildroot}/usr/lib/bash -s -D -o root -g root target/release/libtimehistory_bash.so

%files
/usr/lib/bash/libtimehistory_bash.so

0 comments on commit e4503e2

Please sign in to comment.