Files
nvtop/cmake/modules/FindCurses.cmake
Huang Rui e9767ccc89 Fix crash bug when build with -DCURSES_NEED_WIDE=TRUE
Fix crash bug when build with libncursesw.so.6 and libtinfow.so.6

The FindCurses.cmake is too old to build with
```-DCURSES_NEED_WIDE=TRUE``` option.
In some latest ENV, always using libncursesw.so.6 and libtinfow.so.6.
The nvtop will crashed with these messages below:

```
AddressSanitizer:DEADLYSIGNAL
=================================================================
==50432==ERROR: AddressSanitizer: SEGV on unknown address 0x0000000000c8 (pc 0x7f9c4d507d39 bp 0x7f9c4d4e6de8 sp 0x7ffc057963a8 T0)
==50432==The signal is caused by a READ memory access.
==50432==Hint: address points to the zero page.
    #0 0x7f9c4d507d38 in termattrs_sp (/lib64/libncursesw.so.6+0x1fd38)
    #1 0x7f9c4d504f73 in _nc_setupscreen_sp (/lib64/libncursesw.so.6+0x1cf73)
    #2 0x7f9c4d50078b in newterm_sp (/lib64/libncursesw.so.6+0x1878b)
    #3 0x7f9c4d500c08 in newterm (/lib64/libncursesw.so.6+0x18c08)
    #4 0x7f9c4d4fca53 in initscr (/lib64/libncursesw.so.6+0x14a53)
    #5 0x55bd7f6d693d in initialize_curses (/usr/local/bin/nvtop+0x5893d)
    #6 0x55bd7f6c9f7a in main (/usr/local/bin/nvtop+0x4bf7a)
    #7 0x7f9c4c998f1a in __libc_start_main (/lib64/libc.so.6+0x23f1a)
    #8 0x55bd7f6ca789 in _start (/usr/local/bin/nvtop+0x4c789)
```

The load library have some problem, NOTE libtinfow.so.6 and
libtinfo.so.6 load at the same time, it is the root cause.

```
ldd /usr/local/bin/nvtop
	linux-vdso.so.1 (0x00007ffca6d82000)
	libasan.so.5 => /usr/lib/gcc/x86_64-pc-linux-gnu/9.2.0/libasan.so.5 (0x00007f8823464000)
	libm.so.6 => /lib64/libm.so.6 (0x00007f8823326000)
	libnvidia-ml.so.1 => /usr/lib64/libnvidia-ml.so.1 (0x00007f8822d00000)
	libncursesw.so.6 => /lib64/libncursesw.so.6 (0x00007f8822cc5000)
	libtinfo.so.6 => /lib64/libtinfo.so.6 (0x00007f8822c89000)
	libubsan.so.1 => /usr/lib/gcc/x86_64-pc-linux-gnu/9.2.0/libubsan.so.1 (0x00007f8822321000)
	libc.so.6 => /lib64/libc.so.6 (0x00007f8822152000)
	libdl.so.2 => /lib64/libdl.so.2 (0x00007f882214c000)
	librt.so.1 => /lib64/librt.so.1 (0x00007f8822142000)
	libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f8822120000)
	libstdc++.so.6 => /usr/lib/gcc/x86_64-pc-linux-gnu/9.2.0/libstdc++.so.6 (0x00007f8821ea8000)
	libgcc_s.so.1 => /usr/lib/gcc/x86_64-pc-linux-gnu/9.2.0/libgcc_s.so.1 (0x00007f8821e8e000)
	/lib64/ld-linux-x86-64.so.2 (0x00007f8823fc5000)
	libtinfow.so.6 => /lib64/libtinfow.so.6 (0x00007f8821e4f000)
```

To fix it, the FindCurses.cmake should using libtinfow.so.6 instead of
libtinfo.so.6 when DCURSES_NEED_WIDE=TRUE. So I updated the
FindCurses.cmake to latest version to fix this problem.

Signed-off-by: Huang Rui <vowstar@gmail.com>
2019-10-25 04:23:33 +08:00

266 lines
8.7 KiB
CMake

# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
# file Copyright.txt or https://cmake.org/licensing for details.
#[=======================================================================[.rst:
FindCurses
----------
Find the curses or ncurses include file and library.
Result Variables
^^^^^^^^^^^^^^^^
This module defines the following variables:
``CURSES_FOUND``
True if Curses is found.
``CURSES_INCLUDE_DIRS``
The include directories needed to use Curses.
``CURSES_LIBRARIES``
The libraries needed to use Curses.
``CURSES_CFLAGS``
Parameters which ought be given to C/C++ compilers when using Curses.
``CURSES_HAVE_CURSES_H``
True if curses.h is available.
``CURSES_HAVE_NCURSES_H``
True if ncurses.h is available.
``CURSES_HAVE_NCURSES_NCURSES_H``
True if ``ncurses/ncurses.h`` is available.
``CURSES_HAVE_NCURSES_CURSES_H``
True if ``ncurses/curses.h`` is available.
Set ``CURSES_NEED_NCURSES`` to ``TRUE`` before the
``find_package(Curses)`` call if NCurses functionality is required.
Set ``CURSES_NEED_WIDE`` to ``TRUE`` before the
``find_package(Curses)`` call if unicode functionality is required.
Backward Compatibility
^^^^^^^^^^^^^^^^^^^^^^
The following variable are provided for backward compatibility:
``CURSES_INCLUDE_DIR``
Path to Curses include. Use ``CURSES_INCLUDE_DIRS`` instead.
``CURSES_LIBRARY``
Path to Curses library. Use ``CURSES_LIBRARIES`` instead.
#]=======================================================================]
include(CheckLibraryExists)
# we don't know anything about cursesw, so only ncurses
# may be ncursesw
if(NOT CURSES_NEED_WIDE)
set(NCURSES_LIBRARY_NAME "ncurses")
else()
set(NCURSES_LIBRARY_NAME "ncursesw")
# Also, if we are searchig fo wide curses - we are actually searching
# for ncurses, we don't know about any other unicode version.
set(CURSES_NEED_NCURSES TRUE)
endif()
find_library(CURSES_CURSES_LIBRARY NAMES curses)
find_library(CURSES_NCURSES_LIBRARY NAMES "${NCURSES_LIBRARY_NAME}" )
set(CURSES_USE_NCURSES FALSE)
if(CURSES_NCURSES_LIBRARY AND ((NOT CURSES_CURSES_LIBRARY) OR CURSES_NEED_NCURSES))
set(CURSES_USE_NCURSES TRUE)
endif()
# http://cygwin.com/ml/cygwin-announce/2010-01/msg00002.html
# cygwin ncurses stopped providing curses.h symlinks see above
# message. Cygwin is an ncurses package, so force ncurses on
# cygwin if the curses.h is missing
if(CYGWIN)
if (CURSES_NEED_WIDE)
if(NOT EXISTS /usr/include/ncursesw/curses.h)
set(CURSES_USE_NCURSES TRUE)
endif()
else()
if(NOT EXISTS /usr/include/curses.h)
set(CURSES_USE_NCURSES TRUE)
endif()
endif()
endif()
# Not sure the logic is correct here.
# If NCurses is required, use the function wsyncup() to check if the library
# has NCurses functionality (at least this is where it breaks on NetBSD).
# If wsyncup is in curses, use this one.
# If not, try to find ncurses and check if this has the symbol.
# Once the ncurses library is found, search the ncurses.h header first, but
# some web pages also say that even with ncurses there is not always a ncurses.h:
# http://osdir.com/ml/gnome.apps.mc.devel/2002-06/msg00029.html
# So at first try ncurses.h, if not found, try to find curses.h under the same
# prefix as the library was found, if still not found, try curses.h with the
# default search paths.
if(CURSES_CURSES_LIBRARY AND CURSES_NEED_NCURSES)
include(CMakePushCheckState)
cmake_push_check_state()
set(CMAKE_REQUIRED_QUIET ${Curses_FIND_QUIETLY})
CHECK_LIBRARY_EXISTS("${CURSES_CURSES_LIBRARY}"
wsyncup "" CURSES_CURSES_HAS_WSYNCUP)
if(CURSES_NCURSES_LIBRARY AND NOT CURSES_CURSES_HAS_WSYNCUP)
CHECK_LIBRARY_EXISTS("${CURSES_NCURSES_LIBRARY}"
wsyncup "" CURSES_NCURSES_HAS_WSYNCUP)
if( CURSES_NCURSES_HAS_WSYNCUP)
set(CURSES_USE_NCURSES TRUE)
endif()
endif()
cmake_pop_check_state()
endif()
if(CURSES_USE_NCURSES)
get_filename_component(_cursesLibDir "${CURSES_NCURSES_LIBRARY}" PATH)
get_filename_component(_cursesParentDir "${_cursesLibDir}" PATH)
# Use CURSES_NCURSES_INCLUDE_PATH if set, for compatibility.
if(CURSES_NCURSES_INCLUDE_PATH)
if (CURSES_NEED_WIDE)
find_path(CURSES_INCLUDE_PATH
NAMES ncursesw/ncurses.h ncursesw/curses.h ncursesw.h cursesw.h
PATHS ${CURSES_NCURSES_INCLUDE_PATH}
NO_DEFAULT_PATH
)
else()
find_path(CURSES_INCLUDE_PATH
NAMES ncurses/ncurses.h ncurses/curses.h ncurses.h curses.h
PATHS ${CURSES_NCURSES_INCLUDE_PATH}
NO_DEFAULT_PATH
)
endif()
endif()
if (CURSES_NEED_WIDE)
set(CURSES_TINFO_LIBRARY_NAME tinfow)
find_path(CURSES_INCLUDE_PATH
NAMES ncursesw/ncurses.h ncursesw/curses.h ncursesw.h cursesw.h
HINTS "${_cursesParentDir}/include"
)
else()
set(CURSES_TINFO_LIBRARY_NAME tinfo)
find_path(CURSES_INCLUDE_PATH
NAMES ncurses/ncurses.h ncurses/curses.h ncurses.h curses.h
HINTS "${_cursesParentDir}/include"
)
endif()
# Previous versions of FindCurses provided these values.
if(NOT DEFINED CURSES_LIBRARY)
set(CURSES_LIBRARY "${CURSES_NCURSES_LIBRARY}")
endif()
CHECK_LIBRARY_EXISTS("${CURSES_NCURSES_LIBRARY}"
cbreak "" CURSES_NCURSES_HAS_CBREAK)
if(NOT CURSES_NCURSES_HAS_CBREAK)
find_library(CURSES_EXTRA_LIBRARY "${CURSES_TINFO_LIBRARY_NAME}" HINTS "${_cursesLibDir}")
find_library(CURSES_EXTRA_LIBRARY "${CURSES_TINFO_LIBRARY_NAME}" )
endif()
else()
get_filename_component(_cursesLibDir "${CURSES_CURSES_LIBRARY}" PATH)
get_filename_component(_cursesParentDir "${_cursesLibDir}" PATH)
#We can't find anything with CURSES_NEED_WIDE because we know
#only about ncursesw unicode curses version
if(NOT CURSES_NEED_WIDE)
find_path(CURSES_INCLUDE_PATH
NAMES curses.h
HINTS "${_cursesParentDir}/include"
)
endif()
# Previous versions of FindCurses provided these values.
if(NOT DEFINED CURSES_CURSES_H_PATH)
set(CURSES_CURSES_H_PATH "${CURSES_INCLUDE_PATH}")
endif()
if(NOT DEFINED CURSES_LIBRARY)
set(CURSES_LIBRARY "${CURSES_CURSES_LIBRARY}")
endif()
endif()
# Report whether each possible header name exists in the include directory.
if(NOT DEFINED CURSES_HAVE_NCURSES_NCURSES_H)
if(CURSES_NEED_WIDE)
if(EXISTS "${CURSES_INCLUDE_PATH}/ncursesw/ncurses.h")
set(CURSES_HAVE_NCURSES_NCURSES_H "${CURSES_INCLUDE_PATH}/ncursesw/ncurses.h")
endif()
elseif(EXISTS "${CURSES_INCLUDE_PATH}/ncurses/ncurses.h")
set(CURSES_HAVE_NCURSES_NCURSES_H "${CURSES_INCLUDE_PATH}/ncurses/ncurses.h")
endif()
if(NOT DEFINED CURSES_HAVE_NCURSES_NCURSES_H)
set(CURSES_HAVE_NCURSES_NCURSES_H "CURSES_HAVE_NCURSES_NCURSES_H-NOTFOUND")
endif()
endif()
if(NOT DEFINED CURSES_HAVE_NCURSES_CURSES_H)
if(CURSES_NEED_WIDE)
if(EXISTS "${CURSES_INCLUDE_PATH}/ncursesw/curses.h")
set(CURSES_HAVE_NCURSES_CURSES_H "${CURSES_INCLUDE_PATH}/ncursesw/curses.h")
endif()
elseif(EXISTS "${CURSES_INCLUDE_PATH}/ncurses/curses.h")
set(CURSES_HAVE_NCURSES_CURSES_H "${CURSES_INCLUDE_PATH}/ncurses/curses.h")
endif()
if(NOT DEFINED CURSES_HAVE_NCURSES_CURSES_H)
set(CURSES_HAVE_NCURSES_CURSES_H "CURSES_HAVE_NCURSES_CURSES_H-NOTFOUND")
endif()
endif()
if(NOT CURSES_NEED_WIDE)
#ncursesw can't be found for this paths
if(NOT DEFINED CURSES_HAVE_NCURSES_H)
if(EXISTS "${CURSES_INCLUDE_PATH}/ncurses.h")
set(CURSES_HAVE_NCURSES_H "${CURSES_INCLUDE_PATH}/ncurses.h")
else()
set(CURSES_HAVE_NCURSES_H "CURSES_HAVE_NCURSES_H-NOTFOUND")
endif()
endif()
if(NOT DEFINED CURSES_HAVE_CURSES_H)
if(EXISTS "${CURSES_INCLUDE_PATH}/curses.h")
set(CURSES_HAVE_CURSES_H "${CURSES_INCLUDE_PATH}/curses.h")
else()
set(CURSES_HAVE_CURSES_H "CURSES_HAVE_CURSES_H-NOTFOUND")
endif()
endif()
endif()
find_library(CURSES_FORM_LIBRARY form HINTS "${_cursesLibDir}")
find_library(CURSES_FORM_LIBRARY form )
# Previous versions of FindCurses provided these values.
if(NOT DEFINED FORM_LIBRARY)
set(FORM_LIBRARY "${CURSES_FORM_LIBRARY}")
endif()
# Need to provide the *_LIBRARIES
set(CURSES_LIBRARIES ${CURSES_LIBRARY})
if(CURSES_EXTRA_LIBRARY)
set(CURSES_LIBRARIES ${CURSES_LIBRARIES} ${CURSES_EXTRA_LIBRARY})
endif()
if(CURSES_FORM_LIBRARY)
set(CURSES_LIBRARIES ${CURSES_LIBRARIES} ${CURSES_FORM_LIBRARY})
endif()
# Provide the *_INCLUDE_DIRS and *_CFLAGS results.
set(CURSES_INCLUDE_DIRS ${CURSES_INCLUDE_PATH})
set(CURSES_INCLUDE_DIR ${CURSES_INCLUDE_PATH}) # compatibility
find_package(PkgConfig QUIET)
if(PKG_CONFIG_FOUND)
pkg_check_modules(NCURSES QUIET ${NCURSES_LIBRARY_NAME})
set(CURSES_CFLAGS ${NCURSES_CFLAGS_OTHER})
endif()
include(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(Curses DEFAULT_MSG
CURSES_LIBRARY CURSES_INCLUDE_PATH)
mark_as_advanced(
CURSES_INCLUDE_PATH
CURSES_CURSES_LIBRARY
CURSES_NCURSES_LIBRARY
CURSES_EXTRA_LIBRARY
CURSES_FORM_LIBRARY
)