diff --git a/llvm/cmake/config-ix.cmake b/llvm/cmake/config-ix.cmake index 9895469973e47..0fcd73e752311 100644 --- a/llvm/cmake/config-ix.cmake +++ b/llvm/cmake/config-ix.cmake @@ -19,6 +19,7 @@ if (ANDROID OR CYGWIN OR CMAKE_SYSTEM_NAME MATCHES "AIX|DragonFly|FreeBSD|Haiku| set(HAVE_SYS_MMAN_H 1) set(HAVE_SYSEXITS_H 1) set(HAVE_UNISTD_H 1) + set(HAVE_SYS_IOCTL_H 1) elseif (APPLE) set(HAVE_MACH_MACH_H 1) set(HAVE_MALLOC_MALLOC_H 1) @@ -26,6 +27,7 @@ elseif (APPLE) set(HAVE_SYS_MMAN_H 1) set(HAVE_SYSEXITS_H 1) set(HAVE_UNISTD_H 1) + set(HAVE_SYS_IOCTL_H 1) elseif (WIN32) set(HAVE_MACH_MACH_H 0) set(HAVE_MALLOC_MALLOC_H 0) @@ -33,6 +35,7 @@ elseif (WIN32) set(HAVE_SYS_MMAN_H 0) set(HAVE_SYSEXITS_H 0) set(HAVE_UNISTD_H 0) + set(HAVE_SYS_IOCTL_H 0) elseif (ZOS) # Confirmed in # https://github.com/llvm/llvm-project/pull/104706#issuecomment-2297109613 @@ -42,6 +45,7 @@ elseif (ZOS) set(HAVE_SYS_MMAN_H 1) set(HAVE_SYSEXITS_H 0) set(HAVE_UNISTD_H 1) + set(HAVE_SYS_IOCTL_H 1) else() # Other platforms that we don't promise support for. check_include_file(mach/mach.h HAVE_MACH_MACH_H) @@ -50,6 +54,7 @@ else() check_include_file(sys/mman.h HAVE_SYS_MMAN_H) check_include_file(sysexits.h HAVE_SYSEXITS_H) check_include_file(unistd.h HAVE_UNISTD_H) + check_include_file(sys/ioctl.h HAVE_SYS_IOCTL_H) endif() if( UNIX AND NOT (APPLE OR BEOS OR HAIKU) ) diff --git a/llvm/include/llvm/Config/config.h.cmake b/llvm/include/llvm/Config/config.h.cmake index 06d4756397911..ce83de8e4cba9 100644 --- a/llvm/include/llvm/Config/config.h.cmake +++ b/llvm/include/llvm/Config/config.h.cmake @@ -164,6 +164,9 @@ /* Define to 1 if you have the header file. */ #cmakedefine HAVE_SYS_MMAN_H ${HAVE_SYS_MMAN_H} +/* Define to 1 if you have the header file. */ +#cmakedefine HAVE_SYS_IOCTL_H ${HAVE_SYS_IOCTL_H} + /* Define to 1 if stat struct has st_mtimespec member .*/ #cmakedefine HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC ${HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC} diff --git a/llvm/lib/Support/Unix/Process.inc b/llvm/lib/Support/Unix/Process.inc index b5c3719f57963..db735b7484ad8 100644 --- a/llvm/lib/Support/Unix/Process.inc +++ b/llvm/lib/Support/Unix/Process.inc @@ -34,6 +34,9 @@ #ifdef HAVE_GETAUXVAL #include #endif +#ifdef HAVE_SYS_IOCTL_H +#include +#endif //===----------------------------------------------------------------------===// //=== WARNING: Implementation here must contain only generic UNIX code that @@ -304,31 +307,40 @@ bool Process::FileDescriptorIsDisplayed(int fd) { #endif } -static unsigned getColumns() { +static unsigned getColumns(int FileID) { // If COLUMNS is defined in the environment, wrap to that many columns. + // This matches GCC. if (const char *ColumnsStr = std::getenv("COLUMNS")) { int Columns = std::atoi(ColumnsStr); if (Columns > 0) return Columns; } - // We used to call ioctl TIOCGWINSZ to determine the width. It is considered - // unuseful. - return 0; + // Some shells do not export COLUMNS; query the column count via ioctl() + // instead if it isn't available. + unsigned Columns = 0; + +#ifdef HAVE_SYS_IOCTL_H + struct winsize ws; + if (ioctl(FileID, TIOCGWINSZ, &ws) == 0) + Columns = ws.ws_col; +#endif + + return Columns; } unsigned Process::StandardOutColumns() { if (!StandardOutIsDisplayed()) return 0; - return getColumns(); + return getColumns(0); } unsigned Process::StandardErrColumns() { if (!StandardErrIsDisplayed()) return 0; - return getColumns(); + return getColumns(1); } static bool terminalHasColors() {