Added getopts, reordered functions and cleaned up code in mksm

This commit is contained in:
PktSurf 2022-08-17 14:14:58 +05:30
parent 60bcf6e97c
commit 77245f220d

225
mksm
View file

@ -22,9 +22,6 @@ set -e
# Date format
filedate="$(date +%Y%m%d%H%M%S)"
# Archive files
archivefiles=( *.tar.* *.zip *.t?z )
# Function to generate usage options
mkusage() {
cat << EOF
@ -36,7 +33,7 @@ supports generation of build files for sources that support compilation using:
-> Cmake
-> Meson
Usage: mksm <application_name> <application_version> <build_system>
Usage: mksm -a <app_name> -v <app_version> -b <build_system>
**You must be inside the directory containing source tarball when running mksm.**.
For example if you want to build glib 2.60.0,
@ -48,103 +45,81 @@ $ ls
glib-2.60.0.tar.xz
...
$ mksm glib 2.60.0 meson
$ mksm -a 'glib' -v '2.60.0' -b 'meson'
EOF
exit 0
}
# Generate sha512sums in the build file
gensha512sums() {
echo "[INFO] Adding new sha512sums in $appname.SMBuild..."
printf '\n' >> "$appname".SMBuild
printf 'sha512sums="\n' >> "$appname".SMBuild
# File types
files=( *.tar.* *.zip *.t?z *.patch *.diff *.c *.h )
# Checksum digest to be used along with arguments
checksumbinary="sha512sum"
for file in ${files[@]} ; do
if [ -f "$file" ] ; then
$checksumbinary $file >> "$appname".SMBuild
fi
done
printf '"' >> "$appname".SMBuild
exit 0
}
while getopts ':a:b:v:h' option; do
case "$option" in
a) envappname="$OPTARG" ;;
b) envbuildsys="$OPTARG" ;;
h) mkusage ;;
v) envversion="$OPTARG" ;;
*) mkusage ;;
esac
done
# If/else to check if first argument is set. If it isn't, invoke mkusage
# function and exit
if [ -z $1 ] ; then
if [[ $OPTIND = 1 ]] ; then
mkusage
exit 0
fi
# Explanation same as above, but this time, if first argument is set then
# set use it as a value for $appname variable
if [ -n "$1" ] ; then
if [ "$1" = "-h" ] || [ "$1" = "-help" ] || [ "$1" = "--help" ] ; then
mkusage
exit 0
else
appname="$1"
fi
fi
# If/else to check if second argument is set. If it is, set it as a value
# for $version variable. If it isn't, throw an error and exit.
if [ -n "$2" ] ; then
version="$2"
else
echo "Version not defined. Kindly define it. Exiting!"
# Show errors and exit if either of these is not set
if [[ -z $envappname ]] ; then
echo "[ERROR] Application name not provided. Please provide one."
exit 1
fi
# If/else to check if third argument is set. If it is, set it as a value for
# $buildsys variable. If it isn't, throw an error and exit.
if [ -n "$3" ] ; then
if [ "$3" = "make" ] ; then
buildsys="make"
elif [ "$3" = "autoconf" ] ; then
buildsys="autoconf"
elif [ "$3" = "cmake" ] ; then
buildsys="cmake"
elif [ "$3" = "meson" ] ; then
buildsys="meson"
else
echo "ERROR: Invalid build system. Aborting!"
exit 1
fi
else
echo "Build system not defined. Kindly define one from make, autoconf, cmake or meson. Exiting!"
if [[ -z $envversion ]] ; then
echo "[ERROR] Application version not provided. Please provide one."
exit 1
fi
# If $appname, $version and $buildsys variables are set, generate a build file.
if [ -n "$appname" ] && [ -n "$version" ] && [ -n "$buildsys" ] ; then
echo "Generating build file for application '"$appname"' version '"$version"' which uses build system '"$buildsys"'..."
echo ""
echo "Current directory is $PWD"
echo ""
if [[ -z $envbuildsys ]] ; then
echo "[ERROR] Build system not provided. Please provide one from make, cmake, autoconf or meson."
exit 1
fi
# Copy the sample build file based on the build system argument passed
if [ -f "$appname.SMBuild" ] ; then
echo "Found an existing $appname.SMBuild in the current directory."
echo "Backing it up inside a directory 'old' and creating a new one here."
mkdir -p old
mv "$appname.SMBuild" old/"$appname.SMBuild.$filedate"
fi
# Generate a build file.
# This one came in handy:
# https://unix.stackexchange.com/questions/505949/expanding-only-certain-variables-inside-a-heredoc
# Expand variables in this heredoc
cat << EOF > $appname.SMBuild
app=$appname
version=$version
build=1sml
homepage=""
download=""
desc=""
requires="musl"
EOF
# Redefine envappname, envversion and envbuildsys as appname, version and buildsys respectively
appname="$envappname"
version="$envversion"
# Single-quoted 'EOF' prevents variables from being expanded in this heredoc
cat << 'EOF' >> $appname.SMBuild
# Use case/esac to determine the value of envbuildsys supplied to us. If it isn't what's expected, throw an error and exit.
case "$envbuildsys" in
"autoconf"|"make"|"cmake"|"meson")
buildsys="$envbuildsys" ;;
*) echo "[ERROR] Unknown build system. Try 'make', 'cmake', 'autoconf' or 'meson'" ; exit 1 ;;
esac
build() {
mkandenterbuilddir
rm -rf $app-$version
tar xf $srcdir/$app-$version.tar.?z*
cd $app-$version
fixbuilddirpermissions
EOF
# Again prevent variables from being expanded in this heredoc
gen_autoconf() {
cat << 'EOF' >> $appname.SMBuild
gen_autoconf() {
cat << 'EOF' >> $appname.SMBuild
./configure \
--prefix="" \
--sysconfdir=/etc
@ -160,8 +135,9 @@ EOF
EOF
}
gen_make() {
cat << 'EOF' >> $appname.SMBuild
# Function for creating "make" template
gen_make() {
cat << 'EOF' >> $appname.SMBuild
make
make install DESTDIR=$pkg
@ -172,9 +148,10 @@ EOF
EOF
}
# And again
gen_cmake() {
cat << 'EOF' >> $appname.SMBuild
# Function for creating "cmake" template
gen_cmake() {
cat << 'EOF' >> $appname.SMBuild
mkdir smbuild && cd smbuild
cmake .. \
-DCMAKE_INSTALL_PREFIX="/" \
@ -193,9 +170,9 @@ EOF
EOF
}
# Again :)
gen_meson() {
cat << 'EOF' >> $appname.SMBuild
# Function for creating "meson" template"
gen_meson() {
cat << 'EOF' >> $appname.SMBuild
mkdir smbuild && cd smbuild
meson .. \
--prefix="/"
@ -211,36 +188,56 @@ EOF
EOF
}
case "$buildsys" in
autoconf) gen_autoconf ;;
make) gen_make ;;
cmake) gen_cmake ;;
meson) gen_meson ;;
esac
chmod +x $appname.SMBuild
echo "...done"
echo ""
echo "[INFO] Generating build file for application '"$appname"' version '"$version"' which uses build system '"$buildsys"'..."
echo ""
echo "[INFO] Current directory is $PWD"
echo ""
# Copy the sample build file based on the build system argument passed
if [[ -f $appname.SMBuild ]] ; then
echo "[INFO] Found an existing $appname.SMBuild in the current directory."
echo "[INFO] Backing it up inside a directory 'old' and creating a new one here."
mkdir -p old
mv "$appname.SMBuild" old/"$appname.SMBuild.$filedate"
fi
# Generate sha512sums in the build file
# This one came in handy:
# https://unix.stackexchange.com/questions/505949/expanding-only-certain-variables-inside-a-heredoc
# Expand variables in this heredoc
cat << EOF > $appname.SMBuild
app=$appname
version=$version
build=1sml
homepage=""
download=""
desc=""
requires="musl"
EOF
echo "Adding new sha512sums in $appname.SMBuild..."
printf '\n' >> "$appname".SMBuild
printf 'sha512sums="\n' >> "$appname".SMBuild
# Single-quoted 'EOF' prevents variables from being expanded in this heredoc
cat << 'EOF' >> $appname.SMBuild
# File types
files=( *.tar.* *.zip *.t?z *.patch *.diff *.c *.h )
build() {
mkandenterbuilddir
rm -rf $app-$version
# Checksum digest to be used along with arguments
checksumbinary="sha512sum"
tar xf $srcdir/$app-$version.tar.?z*
cd $app-$version
fixbuilddirpermissions
for file in ${files[@]} ; do
if [ -f "$file" ] ; then
$checksumbinary $file >> "$appname".SMBuild
fi
done
printf '"' >> "$appname".SMBuild
exit 0
EOF
case "$buildsys" in
autoconf) gen_autoconf ;;
make) gen_make ;;
cmake) gen_cmake ;;
meson) gen_meson ;;
esac
chmod +x $appname.SMBuild
echo "...done"
echo ""
# Generate SHA512 checksums and exit
gensha512sums
exit 0