Skip to content

Commit

Permalink
Improved build script for Debian, Arch, and RedHat based Linux distri…
Browse files Browse the repository at this point in the history
…butions.
  • Loading branch information
LukasBanana committed Aug 23, 2023
1 parent ce01ed1 commit f38e098
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 54 deletions.
126 changes: 72 additions & 54 deletions BuildLinux.sh
Original file line number Diff line number Diff line change
@@ -1,50 +1,59 @@
#!/bin/sh
#!/bin/bash

SOURCE_DIR=$PWD
OUTPUT_DIR="build_linux"
SKIP_VALIDATION=0
CLEAR_CACHE=0
ENABLE_NULL="OFF"
ENABLE_VULKAN="OFF"
ENABLE_EXAMPLES="ON"
ENABLE_TESTS="ON"
BUILD_TYPE="Release"

print_help()
{
echo "USAGE:"
echo " BuildLinux.sh OPTIONS* [OUTPUT_DIR]"
echo "OPTIONS:"
echo " -h, --help .............. Print this help documentation and exit"
echo " -c, --clear-cache ....... Clear CMake cache and rebuild"
echo " -s, --skip-validation ... Skip check for missing packages (X11, OpenGL etc.)"
echo " -d, --debug ............. Configure Debug build (default is Release)"
echo " -null ................... Include Null renderer"
echo " -vulkan ................. Include Vulkan renderer"
echo " -no-examples ............ Exclude example projects"
echo " -no-tests ............... Exclude test projects"
echo "NOTES:"
echo " Default output directory is 'build_linux'"
}

# Parse arguments
for ARG in "$@"; do
if [ $ARG = "-h" ] || [ $ARG = "--help" ]; then
print_help
exit 0
elif [ $ARG = "-c" ] || [ $ARG = "--clear-cache" ]; then
CLEAR_CACHE=1
elif [ $ARG = "-s" ] || [ $ARG = "--skip-validation" ]; then
SKIP_VALIDATION=1
elif [ $ARG = "-d" ] || [ $ARG = "--debug" ]; then
BUILD_TYPE="Debug"
elif [ $ARG = "-null" ]; then
ENABLE_NULL="ON"
elif [ $ARG = "-vulkan" ]; then
ENABLE_VULKAN="ON"
elif [ $ARG = "-no-examples" ]; then
ENABLE_EXAMPLES="OFF"
elif [ $ARG = "-no-tests" ]; then
ENABLE_TESTS="OFF"
else
OUTPUT_DIR=$ARG
fi
done

# Check packages are installed
REQUIRED_PKG=""

dpkg -s "git" 2>&1 >/dev/null
if [ $? -ne 0 ]; then
REQUIRED_PKG="${REQUIRED_PKG} git"
fi

dpkg -s "cmake" 2>&1 >/dev/null
if [ $? -ne 0 ]; then
REQUIRED_PKG="${REQUIRED_PKG} cmake"
fi

dpkg -s "libx11-dev" 2>&1 >/dev/null
if [ $? -ne 0 ]; then
REQUIRED_PKG="${REQUIRED_PKG} libx11-dev"
fi

dpkg -s "libxrandr-dev" 2>&1 >/dev/null
if [ $? -ne 0 ]; then
REQUIRED_PKG="${REQUIRED_PKG} libxrandr-dev"
fi

dpkg -s "mesa-common-dev" 2>&1 >/dev/null
if [ $? -ne 0 ]; then
REQUIRED_PKG="${REQUIRED_PKG} mesa-common-dev"
fi

dpkg -s "libglu1-mesa-dev" 2>&1 >/dev/null
if [ $? -ne 0 ]; then
REQUIRED_PKG="${REQUIRED_PKG} libglu1-mesa-dev"
fi

dpkg -s "freeglut3-dev" 2>&1 >/dev/null
if [ $? -ne 0 ]; then
REQUIRED_PKG="${REQUIRED_PKG} freeglut3-dev"
fi

if [ ! -z "$REQUIRED_PKG" ]; then
echo "missing packages! enter the following command to install then:"
echo "sudo apt install${REQUIRED_PKG}"
exit 0
if [ $SKIP_VALIDATION -eq 0 ]; then
source scripts/ListMissingPackages.sh
fi

# Ensure we are inside the repository folder
Expand All @@ -54,30 +63,39 @@ if [ ! -f "CMakeLists.txt" ]; then
fi

# Make output build folder
if [ "$#" -eq 1 ]; then
OUTPUT_DIR=$1
else
if [ ! "$#" -eq 0 ]; then
echo "error: too many arguemnts"
echo "usage: BuildLinux.sh [OUTPUT_DIR]"
exit 1
fi
if [ $CLEAR_CACHE = 1 ] && [ -d "$OUTPUT_DIR" ]; then
rm -rf "$OUTPUT_DIR"
fi

if [ ! -d "$OUTPUT_DIR" ]; then
mkdir "$OUTPUT_DIR"
fi

cd "$OUTPUT_DIR"

# Checkout external depenencies
GAUSSIAN_LIB_DIR="GaussianLib/include"

if [ ! -d "$GAUSSIAN_LIB_DIR" ]; then
if [ -f "$SOURCE_DIR/external/$GAUSSIAN_LIB_DIR/Gauss/Gauss.h" ]; then
GAUSSIAN_LIB_DIR=$(realpath "$SOURCE_DIR/external/$GAUSSIAN_LIB_DIR")
elif [ ! -d "$OUTPUT_DIR/$GAUSSIAN_LIB_DIR" ]; then
pushd "$OUTPUT_DIR"
git clone https://github.com/LukasBanana/GaussianLib.git
popd
GAUSSIAN_LIB_DIR=$(realpath "$OUTPUT_DIR/$GAUSSIAN_LIB_DIR")
fi

# Build into output directory
cmake -DLLGL_BUILD_RENDERER_OPENGL=ON -DLLGL_GL_ENABLE_OPENGL2X=ON -DLLGL_BUILD_RENDERER_VULKAN=OFF -DLLGL_BUILD_EXAMPLES=ON -DGaussLib_INCLUDE_DIR:STRING="$OUTPUT_DIR/$GAUSSIAN_LIB_DIR" -S "$SOURCE_DIR"
cd "$OUTPUT_DIR"

cmake \
-DCMAKE_BUILD_TYPE=$BUILD_TYPE \
-DLLGL_BUILD_RENDERER_OPENGL=ON \
-DLLGL_GL_ENABLE_OPENGL2X=ON \
-DLLGL_BUILD_RENDERER_NULL=$ENABLE_NULL \
-DLLGL_BUILD_RENDERER_VULKAN=$ENABLE_VULKAN \
-DLLGL_BUILD_EXAMPLES=$ENABLE_EXAMPLES \
-DLLGL_BUILD_TESTS=$ENABLE_TESTS \
-DGaussLib_INCLUDE_DIR:STRING="$GAUSSIAN_LIB_DIR" \
-S "$SOURCE_DIR"

cmake --build .

91 changes: 91 additions & 0 deletions scripts/ListMissingPackages.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#!/bin/bash

find_package_manager()
{
declare -A osInfo;
osInfo[/etc/alpine-release]=apk
osInfo[/etc/arch-release]=pacman
osInfo[/etc/debian_version]=apt-get
osInfo[/etc/gentoo-release]=emerge
osInfo[/etc/redhat-release]=yum
osInfo[/etc/SuSE-release]=zypp

for F in ${!osInfo[@]}; do
if [[ -f $F ]]; then
echo ${osInfo[$F]}
return 0
fi
done

return 1 # Could not identify package manager
}

list_missing_packages_generic()
{
ARGS=("$@")
CHECK_CMD="${ARGS[0]}"
REQUEST=("${ARGS[@]:1}")
MISSING=""

for PKG in ${REQUEST[@]}; do
eval $CHECK_CMD "$PKG" &> /dev/null
if [ $? -ne 0 ]; then
if [ ! -z "$MISSING" ]; then
MISSING="$MISSING $PKG"
else
MISSING="$PKG"
fi
fi
done

if [ ! -z "$MISSING" ]; then
echo "$MISSING"
fi
}

list_missing_packages_arch()
{
list_missing_packages_generic "pacman -Qi" $@
}

list_missing_packages_debian()
{
list_missing_packages_generic "dpkg -s" $@
}

list_missing_packages_redhat()
{
list_missing_packages_generic "yum list installed" $@
}

report_missing_packages()
{
CMD=$1
MISSING=$2
if [ ! -z "$MISSING" ]; then
echo "missing packages! enter the following command to install them:"
echo "$CMD $MISSING"
exit 1
fi
}

PKG_MGR=$(find_package_manager)

if [ $PKG_MGR = "pacman" ]; then
MISSING=$( list_missing_packages_arch \
git cmake libx11 libxrandr libgl )
report_missing_packages "sudo pacman -S" "$MISSING"
elif [ $PKG_MGR = "apt-get" ]; then
MISSING=$( list_missing_packages_debian \
git cmake libx11-dev libxrandr-dev mesa-common-dev libglu1-mesa-dev freeglut3-dev )
report_missing_packages "sudo apt-get install" "$MISSING"
elif [ $PKG_MGR = "yum" ]; then
MISSING=$( list_missing_packages_redhat \
git cmake libx11-devel libXrandr-devel mesa-libGL-devel )
report_missing_packages "sudo yum install" "$MISSING"
else
echo error: could not identify Linux distribution
exit 1
fi


0 comments on commit f38e098

Please sign in to comment.