Skip to content

Commit 5d1948b

Browse files
Explorer09BenBE
authored andcommitted
build: Rewrite curses detection code in configure
The new curses library detection code in configure script * Uses pkg-config for detecting CFLAGS and LIBS for curses library, falls back to 'ncurses*-config' if pkg-config is not available, and falls back to "-l${library}" if both fail. * Supports override of compiling flags and linking flags through CURSES_CFLAGS and CURSES_LIBS variables. * Supports '--with-curses=library' option to manually specify the curses library name. Signed-off-by: Kang-Che Sung <explorer09@gmail.com>
1 parent c802dc4 commit 5d1948b

1 file changed

Lines changed: 172 additions & 56 deletions

File tree

configure.ac

Lines changed: 172 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -391,61 +391,184 @@ m4_ifdef([PKG_PROG_PKG_CONFIG], [
391391
[pkg.m4 is absent or older than version 0.16; this 'configure' would have incomplete pkg-config support])
392392
])
393393

394-
# HTOP_CHECK_SCRIPT(LIBNAME, FUNCTION, DEFINE, CONFIG_SCRIPT, ELSE_PART)
395-
m4_define([HTOP_CHECK_SCRIPT],
396-
[
397-
if test ! -z "m4_toupper($HTOP_[$1]_CONFIG_SCRIPT)"; then
398-
# to be used to set the path to ncurses*-config when cross-compiling
399-
htop_config_script_libs=$(m4_toupper($HTOP_[$1]_CONFIG_SCRIPT) --libs 2> /dev/null)
400-
htop_config_script_cflags=$(m4_toupper($HTOP_[$1]_CONFIG_SCRIPT) --cflags 2> /dev/null)
401-
else
402-
htop_config_script_libs=$([$4] --libs 2> /dev/null)
403-
htop_config_script_cflags=$([$4] --cflags 2> /dev/null)
404-
fi
405-
htop_script_success=no
406-
htop_save_CFLAGS="$AM_CFLAGS"
407-
if test ! "x$htop_config_script_libs" = x; then
408-
AM_CFLAGS="$AM_CFLAGS $htop_config_script_cflags"
409-
AC_CHECK_LIB([$1], [$2], [
410-
AC_DEFINE([$3], 1, [The library is present.])
411-
LIBS="$htop_config_script_libs $LIBS "
412-
htop_script_success=yes
413-
], [
414-
AM_CFLAGS="$htop_save_CFLAGS"
415-
], [
416-
$htop_config_script_libs
417-
])
418-
fi
419-
if test "x$htop_script_success" = xno; then
420-
[$5]
421-
fi
422-
])
423-
424-
# HTOP_CHECK_LIB(LIBNAME, FUNCTION, DEFINE, ELSE_PART)
425-
m4_define([HTOP_CHECK_LIB],
426-
[
427-
AC_CHECK_LIB([$1], [$2], [
428-
AC_DEFINE([$3], [1], [The library is present.])
429-
LIBS="-l[$1] $LIBS "
430-
], [$4])
431-
])
432-
433394
AC_ARG_ENABLE([unicode],
434395
[AS_HELP_STRING([--enable-unicode],
435396
[enable Unicode support @<:@default=yes@:>@])],
436397
[],
437398
[enable_unicode=yes])
399+
400+
AC_ARG_VAR([CURSES_CFLAGS], [C compiler flags for curses; this overrides auto detected values])
401+
AC_ARG_VAR([CURSES_LIBS], [linker flags for curses; this overrides auto detected values])
402+
403+
curses_pkg_names="ncurses6 ncurses5 ncurses curses"
404+
438405
if test "x$enable_unicode" = xyes; then
439-
HTOP_CHECK_SCRIPT([ncursesw6], [waddwstr], [HAVE_LIBNCURSESW], "ncursesw6-config",
440-
HTOP_CHECK_SCRIPT([ncursesw], [waddwstr], [HAVE_LIBNCURSESW], "ncursesw6-config",
441-
HTOP_CHECK_SCRIPT([ncursesw], [wadd_wch], [HAVE_LIBNCURSESW], "ncursesw5-config",
442-
HTOP_CHECK_SCRIPT([ncurses], [wadd_wch], [HAVE_LIBNCURSESW], "ncurses5-config",
443-
HTOP_CHECK_LIB([ncursesw6], [addnwstr], [HAVE_LIBNCURSESW],
444-
HTOP_CHECK_LIB([ncursesw], [addnwstr], [HAVE_LIBNCURSESW],
445-
HTOP_CHECK_LIB([ncurses], [addnwstr], [HAVE_LIBNCURSESW],
446-
AC_MSG_ERROR([can not find required library libncursesw; you may want to use --disable-unicode])
447-
)))))))
406+
curses_pkg_names="ncursesw6 ncursesw5 ncursesw $curses_pkg_names"
407+
fi
408+
409+
AC_ARG_WITH([curses],
410+
[AS_HELP_STRING([--with-curses=NAME],
411+
[select curses package NAME to link with; e.g. ncursesw6])],
412+
[],
413+
[with_curses=check])
414+
case $with_curses in
415+
check)
416+
: # No-op. Use default list.
417+
;;
418+
yes|no)
419+
AC_MSG_ERROR([bad value '$with_curses' for --with-curses option])
420+
;;
421+
*)
422+
if test "x${CURSES_CFLAGS+y}${CURSES_LIBS+y}" = xyy; then
423+
AC_MSG_WARN([ignoring --with-curses value due to override])
424+
fi
425+
curses_pkg_names=`echo "x$with_curses" | sed 's/^x\(lib\)\{0,1\}//'`
426+
;;
427+
esac
428+
429+
# $1: C preprocessor and compiler flags for curses library
430+
# $2: linker flags for curses library
431+
htop_check_curses_capability () {
432+
htop_curses_cflags=${CURSES_CFLAGS-"$1"}
433+
htop_curses_libs=${CURSES_LIBS-"$2"}
434+
435+
echo "curses cflags${CURSES_CFLAGS+ (override)}: $htop_curses_cflags" >&AS_MESSAGE_LOG_FD
436+
echo "curses libs${CURSES_LIBS+ (override)}: $htop_curses_libs" >&AS_MESSAGE_LOG_FD
437+
438+
htop_msg_linker_flags=$htop_curses_libs
439+
if test "`echo x $htop_msg_linker_flags`" = x; then
440+
htop_msg_linker_flags="(no linker flags)"
441+
fi
442+
443+
htop_curses_status=0 # 0 for success; nonzero for failure
444+
445+
htop_save_CFLAGS=$CFLAGS
446+
htop_save_LIBS=$LIBS
447+
CFLAGS="$AM_CFLAGS $htop_curses_cflags $CFLAGS"
448+
LIBS="$htop_curses_libs $LIBS"
449+
450+
# At this point we have not checked the name of curses header, so
451+
# use forward declaration for the linking tests below.
452+
453+
# htop calls refresh(), which might be implemented as a macro.
454+
# It is more reliable to test linking with doupdate(), which
455+
# refresh() would call internally.
456+
AC_MSG_CHECKING([for doupdate in $htop_msg_linker_flags])
457+
AC_LINK_IFELSE([AC_LANG_PROGRAM([[
458+
int doupdate(void);
459+
]], [[
460+
doupdate();
461+
]])],
462+
[AC_MSG_RESULT(yes)
463+
htop_curses_capability=nonwide],
464+
[AC_MSG_RESULT(no)
465+
htop_curses_status=1])
466+
467+
if test "x$htop_curses_status$enable_unicode" = x0yes; then
468+
AC_MSG_CHECKING([for mvadd_wchnstr in $htop_msg_linker_flags])
469+
AC_LINK_IFELSE([AC_LANG_PROGRAM([[
470+
/* int mvadd_wchnstr(int y, int x, const cchar_t* wchstr, int n); */
471+
int mvadd_wchnstr(int y, int x, const void* wchstr, int n);
472+
]], [[
473+
mvadd_wchnstr(0, 0, (void*)0, 0);
474+
]])],
475+
[AC_MSG_RESULT(yes)
476+
htop_curses_capability=wide],
477+
[AC_MSG_RESULT(no)
478+
htop_curses_status=1])
479+
fi
448480

481+
if test "$htop_curses_status" -eq 0; then
482+
AM_CFLAGS="$AM_CFLAGS $htop_curses_cflags"
483+
if test "$htop_curses_capability" = wide; then
484+
AC_DEFINE([HAVE_LIBNCURSESW], 1, [libncursesw is present])
485+
else
486+
AC_DEFINE([HAVE_LIBNCURSES], 1, [libcurses is present])
487+
fi
488+
else
489+
LIBS=$htop_save_LIBS
490+
fi
491+
CFLAGS=$htop_save_CFLAGS
492+
return "$htop_curses_status"
493+
} # htop_check_curses_capability
494+
495+
htop_curses_capability=none
496+
497+
if test "x${CURSES_CFLAGS+y}${CURSES_LIBS+y}" = xyy; then
498+
curses_pkg_names=""
499+
htop_check_curses_capability "$CURSES_CFLAGS" "$CURSES_LIBS"
500+
fi
501+
502+
# Prioritize $PKG_CONFIG over ncurses*-config, as users might need to
503+
# cross-compile htop.
504+
if test "x$PKG_CONFIG" != x; then
505+
pkg_config_static_flag=""
506+
if test "$enable_static" = yes; then
507+
pkg_config_static_flag=--static
508+
fi
509+
510+
for curses_name in $curses_pkg_names; do
511+
echo retrieving $curses_name information through $PKG_CONFIG >&AS_MESSAGE_LOG_FD
512+
$PKG_CONFIG --exists --print-errors $curses_name >&AS_MESSAGE_LOG_FD 2>&AS_MESSAGE_LOG_FD || continue
513+
514+
: # Resets "$?" to 0
515+
htop_config_cflags=`$PKG_CONFIG --cflags $pkg_config_static_flag $curses_name 2>/dev/null` || continue
516+
: # Resets "$?" to 0
517+
htop_config_libs=`$PKG_CONFIG --libs $pkg_config_static_flag $curses_name 2>/dev/null` || continue
518+
519+
AC_MSG_RESULT([found $curses_name information through $PKG_CONFIG])
520+
521+
if htop_check_curses_capability "$htop_config_cflags" "$htop_config_libs"; then
522+
break
523+
fi
524+
done
525+
fi
526+
527+
case ${htop_curses_capability}-$enable_unicode in
528+
none-*|nonwide-yes)
529+
for curses_name in $curses_pkg_names; do
530+
echo retrieving $curses_name information through ${curses_name}-config >&AS_MESSAGE_LOG_FD
531+
532+
${curses_name}-config --cflags >/dev/null 2>&AS_MESSAGE_LOG_FD || continue
533+
534+
: # Resets "$?" to 0
535+
htop_config_cflags=`${curses_name}-config --cflags 2>/dev/null` || continue
536+
: # Resets "$?" to 0
537+
htop_config_libs=`${curses_name}-config --libs 2>/dev/null` || continue
538+
539+
AC_MSG_RESULT([found $curses_name information through ${curses_name}-config])
540+
541+
if htop_check_curses_capability "$htop_config_cflags" "$htop_config_libs"; then
542+
break
543+
fi
544+
done
545+
;;
546+
esac
547+
548+
case ${htop_curses_capability}-$enable_unicode in
549+
none-*|nonwide-yes)
550+
# OpenBSD and Solaris are known to not provide '*curses*.pc' files.
551+
AC_MSG_RESULT([no curses information found through '*-config' utilities; will guess the linker flags])
552+
for curses_name in $curses_pkg_names; do
553+
if htop_check_curses_capability "" "-l$curses_name"; then
554+
break
555+
fi
556+
done
557+
;;
558+
esac
559+
560+
case ${htop_curses_capability}-$enable_unicode in
561+
nonwide-yes)
562+
AC_MSG_ERROR([cannot find required ncursesw library; you may want to use --disable-unicode])
563+
;;
564+
none-*)
565+
AC_MSG_ERROR([cannot find required curses/ncurses library])
566+
;;
567+
esac
568+
569+
htop_save_CFLAGS=$CFLAGS
570+
CFLAGS="$AM_CFLAGS $CFLAGS"
571+
if test "x$enable_unicode" = xyes; then
449572
AC_CHECK_HEADERS([ncursesw/curses.h], [],
450573
[AC_CHECK_HEADERS([ncurses/ncurses.h], [],
451574
[AC_CHECK_HEADERS([ncurses/curses.h], [],
@@ -461,14 +584,6 @@ if test "x$enable_unicode" = xyes; then
461584
# (at this point we already link against a working ncurses library with wide character support)
462585
AC_SEARCH_LIBS([keypad], [tinfow tinfo])
463586
else
464-
HTOP_CHECK_SCRIPT([ncurses6], [wnoutrefresh], [HAVE_LIBNCURSES], [ncurses6-config],
465-
HTOP_CHECK_SCRIPT([ncurses], [wnoutrefresh], [HAVE_LIBNCURSES], [ncurses5-config],
466-
HTOP_CHECK_LIB([ncurses6], [doupdate], [HAVE_LIBNCURSES],
467-
HTOP_CHECK_LIB([ncurses], [doupdate], [HAVE_LIBNCURSES],
468-
HTOP_CHECK_LIB([curses], [doupdate], [HAVE_LIBNCURSES],
469-
AC_MSG_ERROR([can not find required curses/ncurses library])
470-
)))))
471-
472587
AC_CHECK_HEADERS([curses.h], [],
473588
[AC_CHECK_HEADERS([ncurses/curses.h], [],
474589
[AC_CHECK_HEADERS([ncurses/ncurses.h], [],
@@ -483,6 +598,7 @@ else
483598
# (at this point we already link against a working ncurses library)
484599
AC_SEARCH_LIBS([keypad], [tinfo])
485600
fi
601+
CFLAGS=$htop_save_CFLAGS
486602

487603
if test "$enable_static" = yes; then
488604
AC_SEARCH_LIBS([Gpm_GetEvent], [gpm])

0 commit comments

Comments
 (0)