Skip to content

Instantly share code, notes, and snippets.

@doug65536
Created March 10, 2023 04:22
Show Gist options
  • Save doug65536/afc93ba841b09701743da73fce539db5 to your computer and use it in GitHub Desktop.
Save doug65536/afc93ba841b09701743da73fce539db5 to your computer and use it in GitHub Desktop.
#!/bin/bash
ABS_SRCDIR="$( cd "$( dirname \
"${BASH_SOURCE[0]}" )"&> /dev/null && pwd )" || exit
SRCDIR=$("${REALPATH:-realpath}" --relative-to="." "$ABS_SRCDIR") || \
fail 'Unable to deduce relative path to source'
BUILDDIR=.
ABS_BUILDDIR=$(pwd)
# These are the programs that have an environment
# variable that differs from the executable names,
# that might be -posix or -win32 suffixed,
# and always have the cross-compiler host prefix
declare -A namedtools=(
[CC]=gcc
[CXX]=g++
)
# These are the programs that are named the same
# as the name here, converted to uppercase,
# and might be gcc- prefixed,
# and might be -posix or -win32 suffixed,
# and always have the cross-compiler host prefix
declare -a hosttoolnames=(
"cpp"
"ar"
"nm"
"ranlib"
"gcov-dump"
"gcov"
"gcov-tool"
"nm"
"objdump"
"objcopy"
"ld"
"addr2line"
"as"
"gprof"
)
# These are tools that are not cross-compilation related
declare -a buildtoolnames=(
"rm"
"mkdir"
"grep"
"find"
"sed"
"awk"
)
# Use the HOST environment variable if there is one,
# otherwise, begin with the assumption that it is
# a windows host
host=${HOST:-x86_64-w64-mingw32}
suffix=posix
# -h overrides the host compiler name prefix
# -s overrides the host compiler name suffix
while getopts "h:s:?" opt; do
case $opt in
h) host="$OPTARG"
;;
s) suffix="$OPTARG"
;;
\?)
echo Produces a config.mak for the Makefile, for cross-compiler selection
echo and to allow out-of-source builds.
echo Usage:
echo configure [-h prefix] [-s suffix]
echo example:
echo configure -h x86_64-w64-mingw32 -s posix
exit 1
;;
esac
done
CONFIGFILE=config.mak
if [[ -n $host ]]; then
host=${host}-
fi
function envvar_name() {
local name="$1"
name="${name//-/_}"
name="${name^^}"
echo -n "$name"
}
function work {
echo "# This file is automatically generated!"
echo "# This is meant to be included by the main Makefile"
echo "# Running configure in the source tree, "
echo "# from the build directory should update this file"
echo "HOST=${host}"
echo "ABS_BUILDDIR=${ABS_BUILDDIR}"
echo "ABS_SRCDIR=${ABS_SRCDIR}"
echo "SRCDIR=${SRCDIR}"
echo "BUILDDIR=${BUILDDIR}"
for name in "${!namedtools[@]}"; do
name=$(envvar_name "$name")
program="${namedtools[$name]}"
selected=""
if [[ "$suffix" == "posix" ]] && \
which "${host}${program}-posix" >/dev/null; then
selected="\$(HOST)${program}-posix"
elif [[ "$suffix" == "" ]] && \
which "${host}${program}" >/dev/null; then
selected="\$(HOST)${program}"
elif [[ "$suffix" == "win32" ]] && \
which "${host}${program}-win32" >/dev/null; then
selected="\$(HOST)${program}-win32"
else
echo "Can't find ${host}${program}" >&2
exit 1
fi
echo "Found ${selected} for ${name}" >&2
echo "${name}=${selected}"
done
for name in "${hosttoolnames[@]}"; do
program="$name"
name=$(envvar_name "$name")
selected=
if [[ "$suffix" == "posix" ]] && \
which "${host}gcc-${program}-posix" >/dev/null; then
selected="\$(HOST)gcc-${program}-posix"
elif [[ "$suffix" != "win32" ]] && \
which "${host}gcc-${program}" >/dev/null; then
selected="\$(HOST)gcc-${program}"
elif which "${host}gcc-${program}-win32" >/dev/null; then
selected="\$(HOST)gcc-${program}-win32"
elif [[ "$suffix" == "posix" ]] && \
which "${host}${program}-posix" >/dev/null; then
selected="\$(HOST)${program}-posix"
elif [[ "$suffix" != "win32" ]] && \
which "${host}${program}" >/dev/null; then
selected="\$(HOST)${program}"
elif which "${host}${program}-win32" >/dev/null; then
selected="\$(HOST)${program}-win32"
elif which "${host}${program}-win32" >/dev/null; then
selected="\$(HOST)${program}-win32"
elif which "${program}" >/dev/null; then
selected="${program}"
else
echo "Can't find ${program}" >&2
exit 1
fi
echo "Found ${selected} for ${name}" >&2
echo "${name}=${selected}"
done
for name in "${buildtoolnames[@]}"; do
program="$name"
name="$(envvar_name "$name")"
selected=
if which "${program}" >/dev/null; then
selected="${program}"
else
echo "Can't find ${program}" >&2
exit 1
fi
echo "Found ${selected} for ${name}" >&2
echo "${name}=${selected}"
done
WINHEADERS=${WINHEADERS:-/usr/share/mingw-w64/include}
echo "WINHEADERS=${WINHEADERS}"
if [[ ! -f Makefile ]]; then
echo need a makefile >&2
ln -rs "${SRCDIR}/Makefile" . || exit
fi
}
( work ) >"$CONFIGFILE" || exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment