#!/usr/bin/env bash
set -e
progname=$0
host=
wenv=
MAKECMD="make"
function showusage {
echo ""
echo "USAGE: $progname [-l|m|c|g|n|B] [make-opts]"
echo " $progname -h"
echo ""
echo "Where:"
echo " -l|m|c|g|n|B selects Linux (l), macOS (m), Cygwin (c), BSD (B),"
echo " MSYS/MSYS2 (g) or Windows native (n). Default Linux"
echo " make-opts directly pass to make"
echo " -h will show this help test and terminate"
exit 1
}
while [ ! -z "$1" ]; do
case $1 in
-l )
host=linux
;;
-c )
host=windows
wenv=cygwin
;;
-g )
host=windows
wenv=msys
;;
-m )
host=macos
;;
-n )
host=windows
wenv=native
;;
-B )
host=bsd
MAKECMD="gmake"
;;
-h )
showusage
;;
* )
break
;;
esac
shift
done
if [ -z "$host" ]; then
case $(uname -s) in
Darwin)
host=macos
;;
*BSD)
host=bsd
MAKECMD="gmake"
;;
CYGWIN*)
host=windows
wenv=cygwin
;;
MINGW32*)
host=windows
wenv=msys
;;
MSYS*)
host=windows
wenv=msys
;;
*)
host=linux
;;
esac
fi
if [ -z "$cpu" ]; then
case $(uname -m) in
arm64)
cpu=arm64
;;
aarch64)
cpu=arm64
;;
*)
cpu=x86_64
;;
esac
fi
WD=`test -d ${0%/*} && cd ${0%/*}; pwd`
cd $WD
if [ -x sethost.sh ]; then
cd ..
fi
if [ -x tools/sethost.sh ]; then
nuttx=$PWD
else
echo "This script must be executed in nuttx/ or nuttx/tools directories"
exit 1
fi
if [ ! -r $nuttx/.config ]; then
echo "There is no .config at $nuttx"
exit 1
fi
if [ ! -r $nuttx/Make.defs ]; then
echo "ERROR: No readable Make.defs file exists at $nuttx"
exit 1
fi
if [ "X$host" == "Xlinux" -o "X$host" == "Xmacos" -o "X$host" == "Xbsd" ]; then
kconfig-tweak --file $nuttx/.config --disable CONFIG_HOST_WINDOWS
if [ "X$host" == "Xlinux" ]; then
echo " Select CONFIG_HOST_LINUX=y"
kconfig-tweak --file $nuttx/.config --disable CONFIG_HOST_MACOS
kconfig-tweak --file $nuttx/.config --disable CONFIG_HOST_BSD
kconfig-tweak --file $nuttx/.config --enable CONFIG_HOST_LINUX
if [ "X$cpu" == "Xarm64" ]; then
echo " Select CONFIG_HOST_ARM64=y"
kconfig-tweak --file $nuttx/.config --enable CONFIG_HOST_ARM64
fi
elif [ "X$host" == "Xbsd" ]; then
echo " Select CONFIG_HOST_BSD=y"
kconfig-tweak --file $nuttx/.config --disable CONFIG_HOST_MACOS
kconfig-tweak --file $nuttx/.config --disable CONFIG_HOST_LINUX
kconfig-tweak --file $nuttx/.config --enable CONFIG_HOST_BSD
else
echo " Select CONFIG_HOST_MACOS=y"
kconfig-tweak --file $nuttx/.config --disable CONFIG_HOST_LINUX
kconfig-tweak --file $nuttx/.config --disable CONFIG_HOST_BSD
kconfig-tweak --file $nuttx/.config --enable CONFIG_HOST_MACOS
if [ "X$cpu" == "Xarm64" ]; then
echo " Select CONFIG_HOST_ARM64=y"
kconfig-tweak --file $nuttx/.config --enable CONFIG_HOST_ARM64
fi
fi
kconfig-tweak --file $nuttx/.config --enable CONFIG_SIM_X8664_SYSTEMV
else
echo " Select CONFIG_HOST_WINDOWS=y"
kconfig-tweak --file $nuttx/.config --disable CONFIG_HOST_LINUX
kconfig-tweak --file $nuttx/.config --disable CONFIG_HOST_MACOS
kconfig-tweak --file $nuttx/.config --disable CONFIG_HOST_BSD
kconfig-tweak --file $nuttx/.config --enable CONFIG_HOST_WINDOWS
kconfig-tweak --file $nuttx/.config --enable CONFIG_SIM_X8664_MICROSOFT
if [ "X$wenv" == "Xcygwin" ]; then
echo " Select CONFIG_WINDOWS_CYGWIN=y"
kconfig-tweak --file $nuttx/.config --enable CONFIG_WINDOWS_CYGWIN
else
if [ "X$wenv" == "Xmsys" ]; then
echo " Select CONFIG_WINDOWS_MSYS=y"
kconfig-tweak --file $nuttx/.config --enable CONFIG_WINDOWS_MSYS
else
echo " Select CONFIG_WINDOWS_NATIVE=y"
kconfig-tweak --file $nuttx/.config --enable CONFIG_EXPERIMENTAL
kconfig-tweak --file $nuttx/.config --enable CONFIG_WINDOWS_NATIVE
fi
fi
fi
echo " Refreshing..."
${MAKECMD} olddefconfig $* || { echo "ERROR: failed to refresh"; exit 1; }