[CMake] Use find_library for ncurses

Currently it is hard to avoid having LLVM link to the system install of
ncurses, since it uses check_library_exists to find e.g. libtinfo and
not find_library or find_package.

With this change the ncurses lib is found with find_library, which also
considers CMAKE_PREFIX_PATH. This solves an issue for the spack package
manager, where we want to use the zlib installed by spack, and spack
provides the CMAKE_PREFIX_PATH for it.

This is a similar change as https://reviews.llvm.org/D79219, which just
landed in master.

Patch By: haampie

Differential Revision: https://reviews.llvm.org/D85820
This commit is contained in:
Petr Hosek 2020-08-31 15:11:39 -07:00
parent c9a8d991e6
commit 11e2ca0270
6 changed files with 53 additions and 33 deletions

View File

@ -642,6 +642,13 @@ endif()
if (LLVM_BUILD_STATIC)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static")
# Remove shared library suffixes from use in find_library
foreach (shared_lib_suffix ${CMAKE_SHARED_LIBRARY_SUFFIX} ${CMAKE_IMPORT_LIBRARY_SUFFIX})
list(FIND CMAKE_FIND_LIBRARY_SUFFIXES ${shared_lib_suffix} shared_lib_suffix_idx)
if(NOT ${shared_lib_suffix_idx} EQUAL -1)
list(REMOVE_AT CMAKE_FIND_LIBRARY_SUFFIXES ${shared_lib_suffix_idx})
endif()
endforeach()
endif()
# Use libtool instead of ar if you are both on an Apple host, and targeting Apple.

View File

@ -148,19 +148,18 @@ if(NOT LLVM_USE_SANITIZER MATCHES "Memory.*")
else()
set(HAVE_LIBEDIT 0)
endif()
if(LLVM_ENABLE_TERMINFO)
set(HAVE_TERMINFO 0)
foreach(library terminfo tinfo curses ncurses ncursesw)
string(TOUPPER ${library} library_suffix)
check_library_exists(${library} setupterm "" HAVE_TERMINFO_${library_suffix})
if(HAVE_TERMINFO_${library_suffix})
set(HAVE_TERMINFO 1)
set(TERMINFO_LIBS "${library}")
break()
endif()
endforeach()
if(LLVM_ENABLE_TERMINFO STREQUAL FORCE_ON)
set(MAYBE_REQUIRED REQUIRED)
else()
set(HAVE_TERMINFO 0)
set(MAYBE_REQUIRED)
endif()
if(LLVM_ENABLE_TERMINFO)
find_library(TERMINFO_LIB NAMES terminfo tinfo curses ncurses ncursesw ${MAYBE_REQUIRED})
endif()
if(TERMINFO_LIB)
set(LLVM_ENABLE_TERMINFO 1)
else()
set(LLVM_ENABLE_TERMINFO 0)
endif()
find_library(ICONV_LIBRARY_PATH NAMES iconv libiconv libiconv-2 c)
@ -177,7 +176,11 @@ if(NOT LLVM_USE_SANITIZER MATCHES "Memory.*")
endif()
endif()
endif()
else()
set(LLVM_ENABLE_TERMINFO 0)
endif()
else()
set(LLVM_ENABLE_TERMINFO 0)
endif()
if (LLVM_ENABLE_LIBXML2 STREQUAL "FORCE_ON" AND NOT LLVM_LIBXML2_ENABLED)

View File

@ -212,7 +212,7 @@
#cmakedefine HAVE_SYSEXITS_H ${HAVE_SYSEXITS_H}
/* Define if the setupterm() function is supported this platform. */
#cmakedefine HAVE_TERMINFO ${HAVE_TERMINFO}
#cmakedefine LLVM_ENABLE_TERMINFO ${LLVM_ENABLE_TERMINFO}
/* Define if the xar_open() function is supported this platform. */
#cmakedefine HAVE_LIBXAR ${HAVE_LIBXAR}

View File

@ -2,6 +2,23 @@ if(LLVM_ENABLE_ZLIB)
set(imported_libs ZLIB::ZLIB)
endif()
function(get_system_libname libpath libname)
get_filename_component(libpath ${libpath} NAME)
set(prefixes ${CMAKE_FIND_LIBRARY_PREFIXES})
set(suffixes ${CMAKE_FIND_LIBRARY_SUFFIXES})
list(FILTER prefixes EXCLUDE REGEX "^\\s*$")
list(FILTER suffixes EXCLUDE REGEX "^\\s*$")
if( prefixes )
string(REPLACE ";" "|" prefixes "${prefixes}")
string(REGEX REPLACE "^(${prefixes})" "" libpath ${libpath})
endif()
if( suffixes )
string(REPLACE ";" "|" suffixes "${suffixes}")
string(REGEX REPLACE "(${suffixes})$" "" libpath ${libpath})
endif()
set(${libname} "${libpath}" PARENT_SCOPE)
endfunction()
if( MSVC OR MINGW )
# libuuid required for FOLDERID_Profile usage in lib/Support/Windows/Path.inc.
# advapi32 required for CryptAcquireContextW in lib/Support/Windows/Path.inc.
@ -21,10 +38,8 @@ elseif( CMAKE_HOST_UNIX )
STRING(REGEX REPLACE "^lib" "" Backtrace_LIBFILE ${Backtrace_LIBFILE})
set(system_libs ${system_libs} ${Backtrace_LIBFILE})
endif()
if(LLVM_ENABLE_TERMINFO)
if(HAVE_TERMINFO)
set(system_libs ${system_libs} ${TERMINFO_LIBS})
endif()
if( LLVM_ENABLE_TERMINFO )
set(imported_libs ${imported_libs} "${TERMINFO_LIB}")
endif()
if( LLVM_ENABLE_THREADS AND (HAVE_LIBATOMIC OR HAVE_CXX_LIBATOMICS64) )
set(system_libs ${system_libs} atomic)
@ -237,20 +252,15 @@ if(LLVM_ENABLE_ZLIB)
if(NOT zlib_library)
get_property(zlib_library TARGET ZLIB::ZLIB PROPERTY LOCATION)
endif()
get_filename_component(zlib_library ${zlib_library} NAME)
if(CMAKE_STATIC_LIBRARY_PREFIX AND CMAKE_STATIC_LIBRARY_SUFFIX AND
zlib_library MATCHES "^${CMAKE_STATIC_LIBRARY_PREFIX}.*${CMAKE_STATIC_LIBRARY_SUFFIX}$")
STRING(REGEX REPLACE "^${CMAKE_STATIC_LIBRARY_PREFIX}" "" zlib_library ${zlib_library})
STRING(REGEX REPLACE "${CMAKE_STATIC_LIBRARY_SUFFIX}$" "" zlib_library ${zlib_library})
endif()
if(CMAKE_SHARED_LIBRARY_PREFIX AND CMAKE_SHARED_LIBRARY_SUFFIX AND
zlib_library MATCHES "^${CMAKE_SHARED_LIBRARY_PREFIX}.*${CMAKE_SHARED_LIBRARY_SUFFIX}$")
STRING(REGEX REPLACE "^${CMAKE_SHARED_LIBRARY_PREFIX}" "" zlib_library ${zlib_library})
STRING(REGEX REPLACE "${CMAKE_SHARED_LIBRARY_SUFFIX}$" "" zlib_library ${zlib_library})
endif()
get_system_libname(${zlib_library} zlib_library)
set(llvm_system_libs ${llvm_system_libs} "${zlib_library}")
endif()
if(LLVM_ENABLE_TERMINFO)
get_system_libname(${TERMINFO_LIB} terminfo_library)
set(llvm_system_libs ${llvm_system_libs} "${terminfo_library}")
endif()
set_property(TARGET LLVMSupport PROPERTY LLVM_SYSTEM_LIBS "${llvm_system_libs}")
if(LLVM_WITH_Z3)

View File

@ -313,7 +313,7 @@ unsigned Process::StandardErrColumns() {
return getColumns();
}
#ifdef HAVE_TERMINFO
#ifdef LLVM_ENABLE_TERMINFO
// We manually declare these extern functions because finding the correct
// headers from various terminfo, curses, or other sources is harder than
// writing their specs down.
@ -323,12 +323,12 @@ extern "C" int del_curterm(struct term *termp);
extern "C" int tigetnum(char *capname);
#endif
#ifdef HAVE_TERMINFO
#ifdef LLVM_ENABLE_TERMINFO
static ManagedStatic<std::mutex> TermColorMutex;
#endif
static bool terminalHasColors(int fd) {
#ifdef HAVE_TERMINFO
#ifdef LLVM_ENABLE_TERMINFO
// First, acquire a global lock because these C routines are thread hostile.
std::lock_guard<std::mutex> G(*TermColorMutex);

View File

@ -286,9 +286,9 @@ write_cmake_config("config") {
}
if (llvm_enable_terminfo) {
values += [ "HAVE_TERMINFO=1" ]
values += [ "LLVM_ENABLE_TERMINFO=1" ]
} else {
values += [ "HAVE_TERMINFO=" ]
values += [ "LLVM_ENABLE_TERMINFO=" ]
}
if (llvm_enable_dia_sdk) {