1#!/usr/bin/env bash
2# Check ncurses compatibility
3
4if [ "$OSTYPE" != "msys" ]; then
5  echo "The old menuconfig is expected to be built only in MSYS2. Please report this issue if you encounter it." >&2
6  exit 1
7fi
8
9# What library to link
10ldflags()
11{
12	if [ $(uname -s) == "Darwin" ]; then
13		#OSX seems to need ncurses too
14		echo -n "-lncurses "
15	elif [ $(uname -s) == "FreeBSD" ]; then
16		# On FreeBSD, the linker needs to know to search port libs for
17		# libintl
18		echo -n "-L/usr/local/lib -lintl "
19	fi
20	for ext in so a dll.a dylib ; do
21		for lib in ncursesw ncurses curses ; do
22			$cc -print-file-name=lib${lib}.${ext} $extra_libs | grep -q /
23			if [ $? -eq 0 ]; then
24				echo "-l${lib}"
25				exit
26			fi
27		done
28	done
29	exit 1
30}
31
32# Where is ncurses.h?
33ccflags()
34{
35    # pkg-config doesn't exist in older MSYS (ESP-IDF v20190611). In newer environment, it will find ncurses bundled with MINGW
36    # Pythons and the compilation will fail.
37    # NOTE: Only MSYS is using tools/kconfig.
38	if [ -f /usr/include/ncursesw/curses.h ]; then
39		echo '-I/usr/include/ncursesw -DCURSES_LOC="<curses.h>"'
40		echo ' -DNCURSES_WIDECHAR=1'
41	elif [ -f /usr/include/ncurses/ncurses.h ]; then
42		echo '-I/usr/include/ncurses -DCURSES_LOC="<ncurses.h>"'
43	elif [ -f /usr/include/ncurses/curses.h ]; then
44		echo '-I/usr/include/ncurses -DCURSES_LOC="<curses.h>"'
45	elif [ -f /usr/include/ncurses.h ]; then
46		echo '-DCURSES_LOC="<ncurses.h>"'
47	else
48		echo '-DCURSES_LOC="<curses.h>"'
49	fi
50	if [ $(uname -s) == "Darwin" ]; then
51		#OSX doesn't have libintl
52		echo -n "-DKBUILD_NO_NLS -Wno-format-security "
53	elif [ $(uname -s) == "FreeBSD" ]; then
54		# FreeBSD gettext port has libintl.h, but the compiler needs
55		# to know to search port includes
56		echo -n "-I/usr/local/include "
57	fi
58}
59
60# Temp file, try to clean up after us
61tmp=.lxdialog.tmp
62trap "rm -f $tmp ${tmp%.tmp}.d" 0 1 2 3 15
63
64# Check if we can link to ncurses
65check() {
66        $cc -x c - -o $tmp 2>/dev/null <<'EOF'
67#include CURSES_LOC
68main() {}
69EOF
70	if [ $? != 0 ]; then
71	    echo " *** Unable to find the ncurses libraries or the"       1>&2
72	    echo " *** required header files."                            1>&2
73	    echo " *** 'make menuconfig' requires the ncurses libraries." 1>&2
74	    echo " *** "                                                  1>&2
75	    echo " *** Install ncurses (ncurses-devel) and try again."    1>&2
76	    echo " *** "                                                  1>&2
77	    exit 1
78	fi
79}
80
81usage() {
82	printf "Usage: $0 [-check compiler options|-ccflags|-ldflags compiler options]\n"
83}
84
85if [ $# -eq 0 ]; then
86	usage
87	exit 1
88fi
89
90cc=""
91case "$1" in
92	"-check")
93		shift
94		cc="$@"
95		check
96		;;
97	"-ccflags")
98		ccflags
99		;;
100	"-ldflags")
101		shift
102		cc="$@"
103		ldflags
104		;;
105	"*")
106		usage
107		exit 1
108		;;
109esac
110