Skip to content

Commit

Permalink
Add support for linking against a curses library when available and
Browse files Browse the repository at this point in the history
using it to detect whether or not a terminal supports colors. This
replaces a particularly egregious hack that merely compared the TERM
environment variable to "dumb". That doesn't really translate to
a reasonable experience for users that have actually ensured their
terminal's capabilities are accurately reflected.

This makes testing a terminal for color support somewhat more expensive,
but it is called very rarely anyways. The important fast path when the
output is being piped somewhere is already in place.

The global lock may seem excessive, but the spec for calling into curses
is *terrible*. The whole library is terrible, and I spent quite a bit of
time looking for a better way of doing this before convincing myself
that this was the fundamentally correct way to behave. The damage of the
curses library is very narrowly confined, and we continue to use raw
escape codes for actually manipulating the colors which is a much sane
system than directly using curses here (IMO).

If this causes trouble for folks, please let me know. I've tested it on
Linux and will watch the bots carefully. I've also worked to account for
the variances of curses interfaces that I could finde documentation for,
but that may not have been sufficient.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@187874 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
chandlerc committed Aug 7, 2013
1 parent f76e118 commit f7364d5
Show file tree
Hide file tree
Showing 9 changed files with 432 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ set(FFI_INCLUDE_DIR "" CACHE PATH "Additional directory, where CMake should sear
set(LLVM_TARGET_ARCH "host"
CACHE STRING "Set target to use for LLVM JIT or use \"host\" for automatic detection.")

option(LLVM_ENABLE_CURSES "Use curses to detect terminal info if available." ON)

option(LLVM_ENABLE_THREADS "Use threads if available." ON)

option(LLVM_ENABLE_ZLIB "Use zlib for compression/decompression if available." ON)
Expand Down
24 changes: 24 additions & 0 deletions autoconf/configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -1072,6 +1072,17 @@ AC_ARG_WITH(bug-report-url,
AC_DEFINE_UNQUOTED(BUG_REPORT_URL,"$withval",
[Bug report URL.])

dnl --enable-curses: check whether the user wants to control use of curses:
AC_ARG_ENABLE(curses,AS_HELP_STRING(
[--enable-curses],
[Use curses for querying terminal infomation if available (default is YES)]),
[case "$enableval" in
yes) llvm_cv_enable_curses="yes" ;;
no) llvm_cv_enable_curses="no" ;;
*) AC_MSG_ERROR([Invalid setting for --enable-curses. Use "yes" or "no"]) ;;
esac],
llvm_cv_enable_curses="yes")

dnl --enable-libffi : check whether the user wants to turn off libffi:
AC_ARG_ENABLE(libffi,AS_HELP_STRING(
--enable-libffi,[Check for the presence of libffi (default is NO)]),
Expand Down Expand Up @@ -1378,6 +1389,14 @@ dnl macros to detect whether clock_gettime is available, this just finds the
dnl right libraries to link with.
AC_SEARCH_LIBS(clock_gettime,rt)

dnl The curses library is optional; used for querying terminal info
if test "$llvm_cv_enable_curses" = "yes" ; then
dnl We need the has_color functionality in curses for it to be useful.
AC_SEARCH_LIBS(has_colors,curses ncurses ncursesw,
AC_DEFINE([HAVE_CURSES],[1],
[Define if curses provides the has_color() function on this platform.]))
fi

dnl libffi is optional; used to call external functions from the interpreter
if test "$llvm_cv_enable_libffi" = "yes" ; then
AC_SEARCH_LIBS(ffi_call,ffi,AC_DEFINE([HAVE_FFI_CALL],[1],
Expand Down Expand Up @@ -1554,6 +1573,11 @@ else
AC_SUBST(HAVE_LIBZ, 0)
fi

dnl Try to find a suitable curses header.
if test "$llvm_cv_enable_curses" = "yes" ; then
AC_CHECK_HEADERS([curses.h ncurses.h ncursesw.h ncurses/curses.h ncursesw/curses.h])
fi

dnl Try to find ffi.h.
if test "$llvm_cv_enable_libffi" = "yes" ; then
AC_CHECK_HEADERS([ffi.h ffi/ffi.h])
Expand Down
19 changes: 19 additions & 0 deletions cmake/config-ix.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ check_symbol_exists(FE_INEXACT "fenv.h" HAVE_DECL_FE_INEXACT)
check_include_file(mach/mach.h HAVE_MACH_MACH_H)
check_include_file(mach-o/dyld.h HAVE_MACH_O_DYLD_H)

check_include_file(curses.h HAVE_CURSES_H)
check_include_file(ncurses.h HAVE_NCURSES_H)
check_include_file(ncursesw.h HAVE_NCURSESW_H)
check_include_file(ncurses/curses.h HAVE_NCURSES_CURSES_H)
check_include_file(ncursesw/curses.h HAVE_NCURSESW_CURSES_H)

# library checks
if( NOT PURE_WINDOWS )
check_library_exists(pthread pthread_create "" HAVE_LIBPTHREAD)
Expand All @@ -97,6 +103,19 @@ if( NOT PURE_WINDOWS )
else()
set(HAVE_LIBZ 0)
endif()
if(LLVM_ENABLE_CURSES)
check_library_exists(curses has_colors "" HAVE_CURSES)
if(NOT HAVE_CURSES)
check_library_exists(ncurses has_colors "" HAVE_NCURSES)
set(HAVE_CURSES ${HAVE_NCURSES})
if(NOT HAVE_CURSES)
check_library_exists(ncursesw has_colors "" HAVE_NCURSESW)
set(HAVE_CURSES ${HAVE_NCURSESW})
endif()
endif()
else()
set(HAVE_CURSES 0)
endif()
endif()

# function checks
Expand Down
9 changes: 9 additions & 0 deletions cmake/modules/LLVM-Config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ function(get_system_libs return_var)
if( HAVE_LIBDL )
set(system_libs ${system_libs} ${CMAKE_DL_LIBS})
endif()
if(LLVM_ENABLE_CURSES)
if(HAVE_NCURSESW)
set(system_libs ${system_libs} ncursesw)
elseif(HAVE_NCURSES)
set(system_libs ${system_libs} ncurses)
elseif(HAVE_CURSES)
set(system_libs ${system_libs} curses)
endif()
endif()
if( LLVM_ENABLE_THREADS AND HAVE_LIBPTHREAD )
set(system_libs ${system_libs} pthread)
endif()
Expand Down
2 changes: 2 additions & 0 deletions cmake/modules/LLVMConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ set(TARGET_TRIPLE "@TARGET_TRIPLE@")

set(LLVM_TOOLS_BINARY_DIR @LLVM_TOOLS_BINARY_DIR@)

set(LLVM_ENABLE_CURSES @LLVM_ENABLE_CURSES@)

set(LLVM_ENABLE_THREADS @LLVM_ENABLE_THREADS@)

set(LLVM_ENABLE_ZLIB @LLVM_ENABLE_ZLIB@)
Expand Down
Loading

0 comments on commit f7364d5

Please sign in to comment.