#!/usr/bin/env bash # # Configuration script to build the 2D_Engine for different platforms. # # Creates makefile configuration to build for target host. # # # Project settings # CONFIG_FILE="config.mk" # # Main # set -e exit_error_message() { echo -e "\033[31merror\033[m: $1" exit 0 } ## Parse args HOST="guess" TARGET_HOST="guess" for i in "$@"; do case $i in --target-host=*) TARGET_HOST="${i#*=}" shift ;; --current-host=*) 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 ;; -*) exit_error_message "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 [ "${HOST}" = "guess" ]; then case "${OSTYPE}" in linux*) HOST="linux" ;; msys*|cygwin*) HOST="windows" ;; # darwin*) # HOST="macos" # ;; *) exit_error_message "Current detected os is unsuppoted to build from: ${OSTYPE}\nUse --help for more informations." ;; esac fi echo "Configuring for host: ${HOST}" # If target host is guess then configure for host if [ "${TARGET_HOST}" = "guess" ]; then TARGET_HOST=${HOST} fi # Set host compiler and asset builder flags case "${HOST}" in linux) HOST_CC=gcc HOST_PKG_CONFIG=pkg-config ASSET_BUILDER_CFLAGS=( "--std=c17" "-O2" ) ASSET_BUILDER_LDFLAGS=() ASSET_BUILDER_LIBS=( "libpng|1.6.34" "json-c|0.11" ) ;; windows) HOST_CC=x86_64-w64-mingw32-gcc HOST_PKG_CONFIG=x86_64-w64-mingw32-pkg-config ASSET_BUILDER_CFLAGS=( "--std=c17" "-O2" ) ASSET_BUILDER_LDFLAGS=() ASSET_BUILDER_LIBS=( "libpng|1.6.34" "json-c|0.11" ) ;; *) exit_error_message "Compiling from ${HOST} is not supported yet." ;; esac # Set target host compiler, outputs extensions and flags case "${TARGET_HOST}" in linux) echo "Configuring for target: ${TARGET_HOST}" case "${HOST}" in linux) TARGET_HOST_CC=gcc TARGET_HOST_AR=ar TARGET_HOST_PKG_CONFIG=pkg-config TARGET_HOST_CFLAGS=( "--std=c17" "-Os" "-DASSET_FILE_NAME=\\\"\$(notdir \$(ASSET_OUTPUT))\\\"" ) TARGET_HOST_LDFLAGS=( "-lm" ) OUTPUT_EXT="" DYNAMIC_LIB_OUTPUT_EXT=".so" STATIC_LIB_OUTPUT_EXT=".a" ;; *) exit_error_message "Cross compiling from ${HOST} for ${TARGET_HOST} is not supported yet." ;; esac TARGET_HOST_LIBS=( "sdl3|3.2.10" ) ;; windows) echo "Configuring for target: ${TARGET_HOST}" case "${HOST}" in windows|linux) TARGET_HOST_CC=x86_64-w64-mingw32-gcc TARGET_HOST_AR=x86_64-w64-mingw32-gcc-ar TARGET_HOST_PKG_CONFIG=x86_64-w64-mingw32-pkg-config TARGET_HOST_CFLAGS=( "--std=c17" "-Os" "-DASSET_FILE_NAME=\\\"\$(notdir \$(ASSET_OUTPUT))\\\"" "-D_GNU_SOURCE=1" ) TARGET_HOST_LDFLAGS=() OUTPUT_EXT=".exe" DYNAMIC_LIB_OUTPUT_EXT=".dll" STATIC_LIB_OUTPUT_EXT=".dll.a" ;; *) exit_error_message "Cross compiling from ${HOST} for ${TARGET_HOST} is not supported yet." ;; esac TARGET_HOST_LIBS=( "sdl3|3.3.4" ) ;; fxcg50) echo "Configuring for target: ${TARGET_HOST}" case "${HOST}" in linux) TARGET_HOST_CC=sh-elf-gcc TARGET_HOST_AR=sh-elf-ar TARGET_HOST_CFLAGS=( "--std=c17" "-Os" "-DASSET_FILE_NAME=\\\"\$(notdir \$(ASSET_OUTPUT))\\\"" "-DFXCG50" "-DTARGET_FXCG50" "-mb" "-m4-nofpu" "-ffreestanding" "-nostdlib" "-fstrict-volatile-bitfields" ) TARGET_HOST_LDFLAGS="-T fxcg50.ld" OUTPUT_EXT="" DYNAMIC_LIB_OUTPUT_EXT=".so" STATIC_LIB_OUTPUT_EXT=".a" ;; *) exit_error_message "Cross compiling from ${HOST} for ${TARGET_HOST} is not supported yet." ;; esac ;; *) exit_error_message "Unsupported target host: ${TARGET_HOST}" esac # # Checks # echo echo "Checking host compiler \"${HOST_CC}\"." if ${HOST_CC} --version &> /dev/null; then echo " ${HOST_CC} OK." else exit_error_message "Compiler not working." fi echo "Checking asset builder libs." for LIB in ${ASSET_BUILDER_LIBS[@]}; do LIB_NAME="$(echo ${LIB} | cut -d "|" -f 1)" LIB_MIN_VERSION="$(echo ${LIB} | cut -d "|" -f 2)" if ${HOST_PKG_CONFIG} --atleast-version "${LIB_MIN_VERSION}" "${LIB_NAME}"; then echo " ${LIB_NAME} OK." else exit_error_message "Library ${LIB_NAME} version >= ${LIB_MIN_VERSION} not found." fi done echo "Checking target host compiler \"${TARGET_HOST_CC}\"." if ${TARGET_HOST_CC} --version &> /dev/null; then echo " ${TARGET_HOST_CC} OK." else exit_error_message "Compiler not working." fi echo "Checking target host archiver \"${TARGET_HOST_AR}\"." if ${TARGET_HOST_AR} --version &> /dev/null; then echo " ${TARGET_HOST_AR} OK." else exit_error_message "Compiler not working." fi echo "Checking target host libs." for LIB in ${TARGET_HOST_LIBS[@]}; do LIB_NAME="$(echo ${LIB} | cut -d "|" -f 1)" LIB_MIN_VERSION="$(echo ${LIB} | cut -d "|" -f 2)" if ${TARGET_HOST_PKG_CONFIG} --atleast-version "${LIB_MIN_VERSION}" "${LIB_NAME}"; then echo " ${LIB_NAME} OK." else exit_error_message "Library ${LIB_NAME} version >= ${LIB_MIN_VERSION} not found." fi done # # Config file # echo echo "Writing config file." echo "${CONFIG_FILE}:" echo "\`\`\`" tee "${CONFIG_FILE}" <