2D_Engine_C/configure

243 lines
6.2 KiB
Bash
Executable File

#!/usr/bin/env bash
#
# Configuration script to build the 2D_Engine for different platforms.
#
# Creates makefile configuration to build for target host.
#
#
# Project settings no matter current and target hosts
#
CONFIG_FILE="config.mk"
#LUA_VERSION="5.5.1"
#LUA_CHECKSUM="1c4b4068d67061f2a2231ad2b5422e77acea1487ea9890f6320af614f4373dce"
#
# Main
#
print_error()
{
echo -e "\033[31merror\033[m: $1"
exit 0
}
## Parse args
CURRENT_HOST="guess"
TARGET_HOST="guess"
for i in "$@"; do
case $i in
--target-host=*)
TARGET_HOST="${i#*=}"
shift
;;
--current-host=*)
CURRENT_HOST="${i#*=}"
shift
;;
--clean)
CLEAN=1
EXIT_AFTER_CLEAN=1
shift
;;
--reconfigure)
CLEAN=1
shift
;;
-h|--help)
echo "Usage : ./configure [OPTION]..."
echo "Add submodules depending on target host platform and"
echo "create makefile configuration to build for target host"
echo
echo -e "\033[1m --target-host=\033[mHOST"
echo -e "\tselect target host platform, default is \"guess\" and"
echo -e "\twill try to configure for your current OS"
echo -e "\033[1m --current-host=\033[mHOST"
echo -e "\tselect current host platform, default is \"guess\" and"
echo -e "\twill try to detect your current OS"
echo -e "\033[1m --clean\033[m"
echo -e "\tclean build, config, cache, etc, and exit"
echo -e "\033[1m --reconfigure\033[m"
echo -e "\tcall clean, and configure"
echo -e "\033[1m -h, --help\033[m"
echo -e "\tshow help and exit"
exit 0
;;
-*)
print_error "Unknown option $i"
;;
*)
;;
esac
done
## Clean
if [ -n "${CLEAN}" ]; then
make clean-all
rm -rf ${CONFIG_FILE}
fi
if [ -n "${EXIT_AFTER_CLEAN}" ]; then
exit 0
fi
## Deduce current and target hosts
if [ "${CURRENT_HOST}" = "guess" ]; then
case "${OSTYPE}" in
linux*)
CURRENT_HOST="linux"
;;
msys*|cygwin*)
CURRENT_HOST="windows"
;;
# darwin*)
# CURRENT_HOST="macos"
# ;;
*)
print_error "Current detected os is unsuppoted to build from: ${OSTYPE}\nUse --help for more informations."
;;
esac
fi
echo "Configuring for current host: ${CURRENT_HOST}"
if [ "${TARGET_HOST}" = "guess" ]; then
TARGET_HOST=${CURRENT_HOST}
fi
case "${CURRENT_HOST}" in
linux)
HOST_CC="gcc"
HOST_AR="ar"
ASSET_BUILDER_CFLAGS="--std=c17 -O2 \$(shell pkg-config --cflags libpng json-c)"
ASSET_BUILDER_LDFLAGS="\$(shell pkg-config --libs libpng json-c)"
;;
windows)
HOST_CC="x86_64-w64-mingw32-gcc"
HOST_AR="x86_64-w64-mingw32-ar"
ASSET_BUILDER_CFLAGS="--std=c17 -O2 \$(shell x86_64-w64-mingw32-pkg-config --cflags libpng json-c)"
ASSET_BUILDER_LDFLAGS="\$(shell x86_64-w64-mingw32-pkg-config --libs libpng json-c)"
;;
*)
print_error "Compiling from ${CURRENT_HOST} is not supported yet."
;;
esac
case "${TARGET_HOST}" in
linux)
echo "Configuring for target: ${TARGET_HOST}"
case "${CURRENT_HOST}" in
linux)
TARGET_HOST_CC="gcc"
TARGET_HOST_AR="ar"
TARGET_HOST_CFLAGS="--std=c17 -Os -DASSET_FILE_NAME=\\\"\$(notdir \$(ASSET_OUTPUT))\\\" -D_GNU_SOURCE=1 \$(shell pkg-config --cflags sdl3 lua)"
TARGET_HOST_LDFLAGS="\$(shell pkg-config --libs sdl3 lua) -lm"
TARGET_HOST_LIBCORE="libcore.a"
;;
*)
print_error "Cross compiling from ${CURRENT_HOST} for ${TARGET_HOST} is not supported yet."
;;
esac
;;
windows)
echo "Configuring for target: ${TARGET_HOST}"
case "${CURRENT_HOST}" in
windows)
TARGET_HOST_CC="x86_64-w64-mingw32-gcc"
TARGET_HOST_AR="x86_64-w64-mingw32-ar"
TARGET_HOST_CFLAGS="--std=c17 -Os -DASSET_FILE_NAME=\\\"\$(notdir \$(ASSET_OUTPUT))\\\" -D_GNU_SOURCE=1 \$(shell x86_64-w64-mingw32-pkg-config --cflags sdl3 lua)"
TARGET_HOST_LDFLAGS="\$(shell x86_64-w64-mingw32-pkg-config --libs sdl3 lua) -lm"
TARGET_HOST_LIBCORE="libcore.dll"
;;
*)
print_error "Cross compiling from ${CURRENT_HOST} for ${TARGET_HOST} is not supported yet."
;;
esac
;;
fxcg50)
echo "Configuring for target: ${TARGET_HOST}"
case "${CURRENT_HOST}" in
linux)
TARGET_HOST_CC="sh-elf-gcc"
TARGET_HOST_AR="sh-elf-ar"
TARGET_HOST_CFLAGS="--std=c17 -Os"
TARGET_HOST_LDFLAGS=""
TARGET_HOST_LIBCORE="libcore.a"
;;
*)
print_error "Cross compiling from ${CURRENT_HOST} for ${TARGET_HOST} is not supported yet."
;;
esac
;;
*)
print_error "Unsupported target host: ${TARGET_HOST}"
esac
echo "Writing config file."
echo
echo "${CONFIG_FILE}:"
echo "\`\`\`"
tee "${CONFIG_FILE}" <<EOF
# This file was generated by the configure script.
TARGET_HOST = ${TARGET_HOST}
LIBCORE = ${TARGET_HOST_LIBCORE}
TARGET_HOST_CC = ${TARGET_HOST_CC}
TARGET_HOST_AR = ${TARGET_HOST_AR}
TARGET_HOST_CFLAGS = ${TARGET_HOST_CFLAGS}
TARGET_HOST_LDFLAGS = ${TARGET_HOST_LDFLAGS}
HOST_CC = ${HOST_CC}
ASSET_BUILDER_CFLAGS = ${ASSET_BUILDER_CFLAGS}
ASSET_BUILDER_LDFLAGS = ${ASSET_BUILDER_LDFLAGS}
EOF
if [ "${CURRENT_HOST}" = "windows" ]; then
tee -a "${CONFIG_FILE}" <<EOF
# OS specific options
ECHO_OPTION = -e
EOF
fi
echo "\`\`\`"
#
### Download libs
#echo "===== downloading lua ====="
#
#LUA_TAR_GZ="${CACHE_DIR}/lua-${LUA_VERSION}.tar.gz"
#if [ ! -e "${LUA_TAR_GZ}" ]; then
# echo "Downloading lua source..."
# if ! curl --output "${LUA_TAR_GZ}" "https://www.lua.org/ftp/lua-${LUA_VERSION}.tar.gz"; then
# print_error "Failed to download lua source."
# fi
#fi
#
#echo "Checking lua source sum..."
#if ! echo "${LUA_CHECKSUM} ${LUA_TAR_GZ}" | sha256sum -c; then
# CHECKSUM="$(sha256sum "${LUA_TAR_GZ}" | cut -d " " -f 1)"
# rm "${LUA_TAR_GZ}"
# print_error "Lua zip checksum not valid.\nExpected: \"${LUA_CHECKSUM}\"\nGot: \"${CHECKSUM}\"\nPlease rerun command to retry downloading."
#fi
#
#echo "Extracting lua source"
#if ! tar -xzf "${LUA_TAR_GZ}" --directory "${LIB_DIR}"; then
# print_error "Failed to extract lua source."
#fi
#