Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional buildscripts #370

Merged
merged 18 commits into from Jan 14, 2024
59 changes: 59 additions & 0 deletions tools/make-pkgs/make-build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/bin/bash

# If insufficent args provided, exit
if [ -z "$1" ] || [ -z "$2" ]; then
This conversation was marked as resolved.
Show resolved Hide resolved
echo "Insufficent arguments for this script, Exiting..."
exit 1
fi

FILE_DIR_OBK="$2"
# shellcheck source=/dev/null
source "$FILE_DIR_OBK"config.bash
cd "$FILE_DIR_OBK" || { echo "Error entering FILE_DIR_OBK, Exiting..." && exit 1 ;}

# This script takes the first argument as the "distro string", and that is used to name builddirs and determine the distro/version etc
# The second argument points to FILE_DIR_OBK to load vars from

#git clone obk, update if exists
if [ -d "$FILE_DIR_OBK"OpenBangla-Keyboard ]; then
cd OpenBangla-Keyboard && git pull
else
( git clone --recursive https://github.com/OpenBangla/OpenBangla-Keyboard.git && cd OpenBangla-Keyboard ) || { echo "Error cloning git repo, Exiting..." && rm -rf "$FILE_DIR_OBK" && exit 1 ;}
fi
#determine branch
if [ "$BRANCH_OBK" = 'develop' ]; then
git checkout develop
else
git checkout master
fi
#update submodules
git submodule update

#determine packaging format
PACKAGE_FORMAT=""
case "$1" in
(*fedora*) PACKAGE_FORMAT="RPM" ;;
(*debian*) PACKAGE_FORMAT="DEB" ;;
esac

#compile for ibus and fcitx
if [ "$IM_IBUS_OBK" = 'YES' ]; then
mkdir build-ibus-"$1"
cmake -Bbuild-ibus-"$1" -GNinja -DCPACK_GENERATOR="$PACKAGE_FORMAT" -DCMAKE_INSTALL_PREFIX="/usr" -DENABLE_IBUS=ON && ninja package -C build-ibus-"$1"
fi
if [ "$IM_FCITX_OBK" = 'YES' ]; then
mkdir build-fcitx-"$1"
cmake -Bbuild-fcitx-"$1" -GNinja -DCPACK_GENERATOR="$PACKAGE_FORMAT" -DCMAKE_INSTALL_PREFIX="/usr" -DENABLE_FCITX=ON && ninja package -C build-fcitx-"$1"
fi


#copying generated file to builds folder of make-pkgs.sh's location
if [ ! -d "$START_DIR_OBK/builds/" ]; then
( mkdir "$START_DIR_OBK/builds/" ) || { echo "Build was successful, but failed to copy files to $START_DIR_OBK, Exiting..." && exit 1 ;}
fi

find "$FILE_DIR_OBK" -type f -iname "*.$PACKAGE_FORMAT" -print0 | while IFS= read -r -d $'\0' PACKAGE_FILE; do
# Copy each file to the destination directory
cp "$PACKAGE_FILE" "$START_DIR_OBK/builds/"
echo "Copied: $PACKAGE_FILE to $START_DIR_OBK/builds/"
done
20 changes: 20 additions & 0 deletions tools/make-pkgs/make-debian.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash

# If insufficent args provided, exit
if [ -z "$1" ]; then
echo "Insufficent arguments for this script, Exiting..."
exit 1
fi

FILE_DIR_OBK="$1"
# shellcheck source=/dev/null
source "$FILE_DIR_OBK"config.bash > /dev/null 2>&1
echo "Generating Debian packages..."
#cd "$FILE_DIR_OBK" || exit 1

#update the system and install builddeps
sudo apt update -y && sudo apt upgrade -y
sudo apt-get install -y build-essential file rustc cargo cmake meson ninja-build libibus-1.0-dev libfcitx5core-dev fcitx5 qtbase5-dev qtbase5-dev-tools libzstd-dev git

#call buildscript
chmod +x make-build.sh && ./make-build.sh debian-"$DEBIAN_VERSION_OBK" "$FILE_DIR_OBK"
20 changes: 20 additions & 0 deletions tools/make-pkgs/make-fedora.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash

# If insufficent args provided, exit
if [ -z "$1" ]; then
echo "Insufficent arguments for this script, Exiting..."
exit 1
fi

FILE_DIR_OBK="$1"
# shellcheck source=/dev/null
source "$FILE_DIR_OBK"config.bash > /dev/null 2>&1
echo "Generating Fedora packages..."
#cd "$FILE_DIR_OBK" || exit 1

#update the system and install builddeps
sudo dnf -y update
sudo dnf -y install @buildsys-build rust cargo cmake qt5-qtdeclarative-devel libzstd-devel fcitx5 fcitx5-devel ibus ibus-devel ninja-build curl meson git

#call buildscript
chmod +x make-build.sh && ./make-build.sh fedora-"$FEDORA_VERSION_OBK" "$FILE_DIR_OBK"
157 changes: 157 additions & 0 deletions tools/make-pkgs/make-pkgs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
#!/bin/bash

#VARDOC
#FILE_DIR_OBK (PERSIST) : General path where the scripts will store config, git repos etc etc
#DISTRO_COUNT (TEMP) : Counter to check if user tries to compile for multiple distros without --toolbox flag
#config.bash : As toolbox containers donot inherit any environment variables, PERSISTENT variables are stored in here and loaded into other .sh files using source command
#IM_*_OBK, TOOLBOX_ENABLE_OBK, <DISTRO>*_OBK (PERSIST) : self-explanatory
#cleanup : cleans FILE_DIR_OBK and removes containers created by the script
#help : shows helpinfo

# Guide to add new distros
# 1. add distro switch to the #ARGUMENT HANDLING section of make-pkgs.sh and declare variables appropriately
# 2. add appropriate distro-specific commands to #TOOLBOX USAGE section
# 3. add appropriate distro-specific commands to #TOOLBOX NO USAGE section
# 4. add appropriate make-<DISTRO>.sh file (optional but recommended)
# 5. call make-build.sh with appropriate arguments

#Initial vars
FILE_DIR_OBK="$HOME/.obk-build/"
mkdir "$FILE_DIR_OBK" > /dev/null 2>&1
rm -r "$FILE_DIR_OBK"config.bash > /dev/null 2>&1
touch "$FILE_DIR_OBK"config.bash > /dev/null 2>&1
echo "START_DIR_OBK=$(pwd)" >> "$FILE_DIR_OBK"config.bash
#rm -r "$FILE_DIR_OBK"distro.list > /dev/null 2>&1
#touch "$FILE_DIR_OBK"distro.list > /dev/null 2>&1

#cleanup function
cleanup () {
echo "Removing obk-toolbox containers..."
for tbc in $(toolbox list | sed -n 's/.*\(obk-toolbox[^\ ]*\).*/\1/p'); do
toolbox rm -f "$tbc"
done
echo "Cleaning build directory..."
rm -rf "$FILE_DIR_OBK" > /dev/null 2>&1
}
#help function
help () {
echo "Usage: $0 [OPTIONS]"
echo "Options:"
echo " --ibus Compile IBus version"
echo " --fcitx Compile Fcitx version (Doesn't work for OBK2 and earlier)"
echo " --develop Compile from the develop branch"
echo " --fedora Compile for Fedora"
echo " --debian Compile for Debian"
echo " --toolbox Compile inside of a toolbox"
echo " --clean Clean \"$FILE_DIR_OBK\" directory and remove obk-toolbox-* containers"
echo " --help Display this help message"
echo " "
echo " -> NOTE: User needs to specify the --toolbox flag to compile for multiple distros at the same time."
echo " Otherwise, only one flag can be used at a time (e.g. --fedora and --debian can't be used together without --toolbox)"
echo " -> NOTE: When compiling inside of a toolbox, an additional <version> can be specified for the distro flags."
echo " (e.g. --fedora 37) This will generate packages for a specific version of a distro."
echo "Example: "
echo " $0 --ibus --fcitx --develop --fedora 37 --debian --toolbox"
echo " $0 --ibus --develop --debian"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using multiple echos, try using

cat <<EOF
...
EOF

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless absolutely mandatory, I would personally like to avoid the change, as using the echos make the script more readable and understandable compared to using cat <<EOF

}

#storing vars
DISTRO_COUNT=0

#ARGUMENT HANDLING
for arg in "$@"; do
case $arg in
("--ibus") echo 'IM_IBUS_OBK='"'YES'" >> "$FILE_DIR_OBK"config.bash ;;
("--fcitx") echo 'IM_FCITX_OBK='"'YES'" >> "$FILE_DIR_OBK"config.bash ;;
("--develop") echo 'BRANCH_OBK='"'develop'" >> "$FILE_DIR_OBK"config.bash ;;
("--toolbox") echo 'TOOLBOX_ENABLE_OBK='"'YES'" >> "$FILE_DIR_OBK"config.bash ;;
("--fedora")
echo 'FEDORA_OBK='"'YES'" >> "$FILE_DIR_OBK"config.bash
echo "FEDORA_VERSION_OBK=$(echo "$@" | sed -n 's/.*--fedora \([0-9]*\).*/\1/p')" >> "$FILE_DIR_OBK"config.bash
((DISTRO_COUNT++))
;;
("--debian")
echo 'DEBIAN_OBK='"'YES'" >> "$FILE_DIR_OBK"config.bash
echo "DEBIAN_VERSION_OBK=$(echo "$@" | sed -n 's/.*--debian \([0-9]*\).*/\1/p')" >> "$FILE_DIR_OBK"config.bash
((DISTRO_COUNT++))
;;
("--clean")
cleanup
exit 1
;;
("--help")
help
exit 1
;;
(--*)
echo "Unknown argument: $arg"
help
echo "Exiting..."
exit 1 ;;
esac
done

#load vars from file
# shellcheck source=/dev/null
source "$FILE_DIR_OBK"config.bash 2>&1
cat "$FILE_DIR_OBK"config.bash

#TOOLBOX USAGE
if [ "$TOOLBOX_ENABLE_OBK" = 'YES' ]; then
#create toolbox
echo "Creating toolbox..."
toolbox_error () { echo "Something went wrong with the toolbox, Exiting..." && exit 1 ;}
# i attempted at rewriting this part to be distro-agnostic and setup toolboxes from a distro.list file, but the current approach is more modular and more extensible
# e.g. if different distros want to target different/specific toolbox images/repos

# disabled the checking for versions in toolobx sections, the container and pathnames will end up with an extra hyphen - in the end, but it will simplify the toolbox aspect

#fedora toolbox
if [ "$FEDORA_OBK" = 'YES' ]; then
chmod +x make-fedora.sh
echo "Setting up Fedora toolbox."
# if [ -z "$FEDORA_VERSION_OBK" ]; then
# #if the user didnt specified a version
# ( toolbox create obk-toolbox-fedora -d fedora ) || echo "Failed creating Fedora toolbox" && exit 1
# ( toolbox run -c obk-toolbox-fedora ./make-fedora.sh ) || echo "Failed entering Fedora toolbox" && exit 1
# else
#if user did
( toolbox create obk-toolbox-fedora-"$FEDORA_VERSION_OBK" -d fedora -r "$FEDORA_VERSION_OBK" ) || toolbox_error
( toolbox run -c obk-toolbox-fedora-"$FEDORA_VERSION_OBK" ./make-fedora.sh "$FILE_DIR_OBK" ) || toolbox_error
# fi
fi

#debian toolbox
if [ "$DEBIAN_OBK" = 'YES' ]; then
chmod +x make-debian.sh
echo "Setting up Debian toolbox."
# if [ -z "$DEBIAN_VERSION_OBK" ]; then
# #if the user didnt specified a version
# toolbox create obk-toolbox-debian -i quay.io/toolbx-images/debian-toolbox
# toolbox run -c obk-toolbox-debian ./make-debian.sh
# else
#if user did
( toolbox create obk-toolbox-debian-"$DEBIAN_VERSION_OBK" -i quay.io/toolbx-images/debian-toolbox:"$DEBIAN_VERSION_OBK" ) || toolbox_error
( toolbox run -c obk-toolbox-debian-"$DEBIAN_VERSION_OBK" ./make-debian.sh "$FILE_DIR_OBK" ) || toolbox_error
# fi
fi

#TOOLBOX NO USAGE
elif [[ "$DISTRO_COUNT" -ne 1 ]]; then
echo "Cannot use more than one distro without --toolbox flag."
echo "Exiting..."
exit 1
else
if [ "$FEDORA_OBK" = 'YES' ]; then
chmod +x make-fedora.sh
./make-fedora.sh "$FILE_DIR_OBK"
exit 1
fi
if [ "$DEBIAN_OBK" = 'YES' ]; then
chmod +x make-debian.sh
./make-debian.sh "$FILE_DIR_OBK"

exit 1
fi
fi

36 changes: 36 additions & 0 deletions tools/make-pkgs/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
## make-pkgs
**make-pkgs** is a collection of bash scripts, intended to make the experience of compiling OBK and generating packages for various distros simpler and user-friendly.
This is made as an user-facing tool, with simple switches for operation.
\
**NOTE** : It is **HIGHLY** recommended you have the `toolbox` package installed.

## The importance of Toolbx containers
Toolbx is a user-friendly CLI utility for creating and entering "containers" for various linux distros, similar to Distrobox.
\
Toolbx is used in make-pkgs to compile and package OBK for distros other than your own.
\
For example, If you are on Debian and you want to generate `.rpm`s specifically for Fedora 37, you can do so.

## How to use
Copy this folder, make the make-pkgs.sh script executable by doing `chmod +x make-pkgs.sh`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@larina3315 why do we need to copy this folder?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was writing this from the POV that if a user clones the OBK git repo with this tool in it, then the make-pkgs.sh file will reside in ./OpenBangla-Keyboard/tools/make-pkgs/ , so the user might want to copy the folder to some other conveinent location and run it from there.

It could be worded in a better way as a suggestion.

\
Then use the script using `./make-pkgs.sh --<commands>`
\
For a list of commands, use `./make-pkgs.sh --help`
\
Generated packages are stored inside the make-pkgs folder inside a directory named "builds"

## Examples
Here are some examples and their explanation on how to use make-pkgs

`./make-pkgs.sh --ibus --develop --debian`
\
This will compile and generate ibus OBK packages for Debian from the `develop` branch, development tools and libraries will be installed on your host system.

`./make-pkgs.sh --ibus --fcitx --develop --debian 11 --fedora 38 --toolbox`
\
This will compile and generate ibus and fcitx OBK packages for Debian 11 and Fedora 38 from the `develop` branch, development tools and libraries will be installed inside of toolbox containers, your host system will remain untouched.

`./make-pkgs.sh --clean`
\
This will remove all toolbox containers named "obk-toolbox-*" and clear the directory where the script stores it's files (by default in ~/.obk-build/)
Loading