Skip to content

Instantly share code, notes, and snippets.

@eljamm
Last active March 18, 2023 18:17
Show Gist options
  • Save eljamm/81133d5066d4d20c0657208e61a56d40 to your computer and use it in GitHub Desktop.
Save eljamm/81133d5066d4d20c0657208e61a56d40 to your computer and use it in GitHub Desktop.
Script for building the project management software masterplan
#!/bin/bash
function print_help() {
echo -e "Usage: masterbuilder [OPTIONS ...]\n"
echo "Options:"
echo " -t, --target target OS to build --> 'linux', 'windows' or 'darwin'"
echo " -a, --arch target architecture --> depends on OS"
echo " -r, --release masterplan release version"
echo " -gv, --gs-version go-sdl2 version to use"
echo " -gl, --gs-libs unpacked go-sdl2 library path"
echo " -s, --source path to the project directory"
echo -e " -o, --output directory for output files\n"
echo "Examples:"
echo " $ cd ~/masterplan && masterbuilder -t linux -a amd64"
echo " $ cd ~/masterplan && masterbuilder -t windows -a amd64"
echo " $ masterbuilder -t windows -a amd64 -r 0.8-Release-64 -s ~/masterplan -o ~/build"
}
# Initialize
target="windows" # linux, windows or darwin
arch="amd64" # amd64, 386 and arm for linux; amd64 and 386 for windows; amd64 for darwin
release="0.8-Release-64"
gs_version="0.4.21"
ldflags="-s -w"
source="$PWD"
output_dir="$PWD"
gs_libs="$PWD/go-sdl-$gs_version"
# Parse Arguments
while [[ $# -gt 0 ]]; do
case $1 in
-t | --target)
target="$2"
shift # past argument
shift # past value
;;
-a | --arch)
arch="$2"
shift # past argument
shift # past value
;;
-r | --release)
release="$2"
shift # past argument
shift # past value
;;
-gv | --gs-version)
gs_version="$2"
gs_libs="$PWD/go-sdl-$gs_version"
shift # past argument
shift # past value
;;
-gl | --gs-libs)
gs_libs="$2"
shift # past argument
shift # past value
;;
-s | --source)
source="$2"
shift # past argument
shift # past value
;;
-o | --output)
output_dir="$2"
shift # past argument
shift # past value
;;
-h | --help)
print_help
exit
;;
-* | --*)
echo "Unknown option $1"
exit 1
;;
esac
done
# OS-Specific Options
if [[ $target == "windows" ]]; then
compiler="/usr/bin/x86_64-w64-mingw32-gcc"
out_ext="zip"
bin_ext=".exe"
CGO_LDFLAGS="-lmingw32"
ldflags="$ldflags -H windowsgui"
elif [[ $target == "darwin" ]]; then # Untested
compiler="x86_64-apple-darwin20.4-clang"
out_ext="tar.gz"
else
compiler="/usr/bin/gcc"
out_ext="tar.gz"
fi
# Download Dependencies
if [ ! -d $gs_libs ]; then
mkdir $gs_libs
if [ ! -f "go-sdl-${gs_version}.tar.gz" ]; then
echo "<Downloading go-sdl-${gs_version}...>"
wget \
-q --show-progress \
-O "go-sdl-${gs_version}.tar.gz" \
"https://github.com/veandco/go-sdl2/archive/refs/tags/v${gs_version}.tar.gz"
fi
echo "<Unpacking go-sdl-${gs_version}...>"
tar -zxf \
"go-sdl-${gs_version}.tar.gz" \
--directory=${gs_libs} \
--strip-components=2 \
--wildcards go-sdl2-*/.go-sdl2-libs/*
rm "go-sdl-${gs_version}.tar.gz"
fi
cd $source
# Compile Project
echo "<Building for ${target}-${arch}-${release}...>"
env \
CGO_ENABLED=1 \
GOOS="${target}" \
GOARCH="${arch}" \
CC="${compiler}" \
CGO_LDFLAGS="-L${gs_libs} -lSDL2_${target}_${arch} -lSDL2_gfx_${target}_${arch} ${CGO_LDFLAGS}" \
CGO_CFLAGS="-I${gs_libs}/include -D_REENTRANT -pthread" \
go build -ldflags "${ldflags}" -tags "release static" -o "bin/${target}/${arch}-${release}/MasterPlan${bin_ext}" ./
if [ $? -eq 0 ]; then
# Copy assets to build directory
if [[ $target == "windows" ]]; then
cp -r changelog.txt assets other_sources/*.dll bin/$target/$arch-$release/
else
cp -r changelog.txt assets bin/$target/$arch-$release/
fi
# Create compressed file
go run ./build_script/main.go -c
cd - &>/dev/null # Return to account for relative paths
# Copy output file
output_file="$source/bin/$target.$out_ext"
if [ -f $output_file ]; then
if [ ! -d $output_dir ]; then
mkdir $output_dir
fi
cp $output_file $output_dir
fi
else
echo "<Build failed>"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment