Skip to content

Commit 3c7bfbd

Browse files
committed
[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
1 parent 63844c1 commit 3c7bfbd

File tree

9 files changed

+66
-45
lines changed

9 files changed

+66
-45
lines changed

compiler-rt/cmake/config-ix.cmake

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -133,17 +133,18 @@ check_library_exists(pthread pthread_create "" COMPILER_RT_HAS_LIBPTHREAD)
133133
check_library_exists(execinfo backtrace "" COMPILER_RT_HAS_LIBEXECINFO)
134134

135135
# Look for terminfo library, used in unittests that depend on LLVMSupport.
136+
if(LLVM_ENABLE_TERMINFO STREQUAL FORCE_ON)
137+
set(MAYBE_REQUIRED REQUIRED)
138+
else()
139+
set(MAYBE_REQUIRED)
140+
endif()
136141
if(LLVM_ENABLE_TERMINFO)
137-
foreach(library terminfo tinfo curses ncurses ncursesw)
138-
string(TOUPPER ${library} library_suffix)
139-
check_library_exists(
140-
${library} setupterm "" COMPILER_RT_HAS_TERMINFO_${library_suffix})
141-
if(COMPILER_RT_HAS_TERMINFO_${library_suffix})
142-
set(COMPILER_RT_HAS_TERMINFO TRUE)
143-
set(COMPILER_RT_TERMINFO_LIB "${library}")
144-
break()
145-
endif()
146-
endforeach()
142+
find_library(COMPILER_RT_TERMINFO_LIB NAMES terminfo tinfo curses ncurses ncursesw ${MAYBE_REQUIRED})
143+
endif()
144+
if(COMPILER_RT_TERMINFO_LIB)
145+
set(LLVM_ENABLE_TERMINFO 1)
146+
else()
147+
set(LLVM_ENABLE_TERMINFO 0)
147148
endif()
148149

149150
if (ANDROID AND COMPILER_RT_HAS_LIBDL)

compiler-rt/lib/xray/tests/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ set(XRAY_UNITTEST_LINK_FLAGS
5555
if (NOT APPLE)
5656
# Needed by LLVMSupport.
5757
append_list_if(
58-
COMPILER_RT_HAS_TERMINFO
58+
LLVM_ENABLE_TERMINFO
5959
-l${COMPILER_RT_TERMINFO_LIB} XRAY_UNITTEST_LINK_FLAGS)
6060

6161
if (COMPILER_RT_STANDALONE_BUILD)

lldb/source/Core/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ set(LLDB_LIBEDIT_LIBS)
1111

1212
if (LLDB_ENABLE_CURSES)
1313
list(APPEND LLDB_CURSES_LIBS ${CURSES_LIBRARIES} ${PANEL_LIBRARIES})
14-
if(LLVM_ENABLE_TERMINFO AND HAVE_TERMINFO)
15-
list(APPEND LLDB_CURSES_LIBS ${TERMINFO_LIBS})
14+
if(LLVM_ENABLE_TERMINFO)
15+
list(APPEND LLDB_CURSES_LIBS ${TERMINFO_LIB})
1616
endif()
1717
if (LLVM_BUILD_STATIC)
1818
list(APPEND LLDB_CURSES_LIBS gpm)

llvm/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,13 @@ endif()
642642

643643
if (LLVM_BUILD_STATIC)
644644
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static")
645+
# Remove shared library suffixes from use in find_library
646+
foreach (shared_lib_suffix ${CMAKE_SHARED_LIBRARY_SUFFIX} ${CMAKE_IMPORT_LIBRARY_SUFFIX})
647+
list(FIND CMAKE_FIND_LIBRARY_SUFFIXES ${shared_lib_suffix} shared_lib_suffix_idx)
648+
if(NOT ${shared_lib_suffix_idx} EQUAL -1)
649+
list(REMOVE_AT CMAKE_FIND_LIBRARY_SUFFIXES ${shared_lib_suffix_idx})
650+
endif()
651+
endforeach()
645652
endif()
646653

647654
# Use libtool instead of ar if you are both on an Apple host, and targeting Apple.

llvm/cmake/config-ix.cmake

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -148,19 +148,18 @@ if(NOT LLVM_USE_SANITIZER MATCHES "Memory.*")
148148
else()
149149
set(HAVE_LIBEDIT 0)
150150
endif()
151+
if(LLVM_ENABLE_TERMINFO STREQUAL FORCE_ON)
152+
set(MAYBE_REQUIRED REQUIRED)
153+
else()
154+
set(MAYBE_REQUIRED)
155+
endif()
151156
if(LLVM_ENABLE_TERMINFO)
152-
set(HAVE_TERMINFO 0)
153-
foreach(library terminfo tinfo curses ncurses ncursesw)
154-
string(TOUPPER ${library} library_suffix)
155-
check_library_exists(${library} setupterm "" HAVE_TERMINFO_${library_suffix})
156-
if(HAVE_TERMINFO_${library_suffix})
157-
set(HAVE_TERMINFO 1)
158-
set(TERMINFO_LIBS "${library}")
159-
break()
160-
endif()
161-
endforeach()
157+
find_library(TERMINFO_LIB NAMES terminfo tinfo curses ncurses ncursesw ${MAYBE_REQUIRED})
158+
endif()
159+
if(TERMINFO_LIB)
160+
set(LLVM_ENABLE_TERMINFO 1)
162161
else()
163-
set(HAVE_TERMINFO 0)
162+
set(LLVM_ENABLE_TERMINFO 0)
164163
endif()
165164

166165
find_library(ICONV_LIBRARY_PATH NAMES iconv libiconv libiconv-2 c)
@@ -177,7 +176,11 @@ if(NOT LLVM_USE_SANITIZER MATCHES "Memory.*")
177176
endif()
178177
endif()
179178
endif()
179+
else()
180+
set(LLVM_ENABLE_TERMINFO 0)
180181
endif()
182+
else()
183+
set(LLVM_ENABLE_TERMINFO 0)
181184
endif()
182185

183186
if (LLVM_ENABLE_LIBXML2 STREQUAL "FORCE_ON" AND NOT LLVM_LIBXML2_ENABLED)

llvm/include/llvm/Config/config.h.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@
212212
#cmakedefine HAVE_SYSEXITS_H ${HAVE_SYSEXITS_H}
213213

214214
/* Define if the setupterm() function is supported this platform. */
215-
#cmakedefine HAVE_TERMINFO ${HAVE_TERMINFO}
215+
#cmakedefine LLVM_ENABLE_TERMINFO ${LLVM_ENABLE_TERMINFO}
216216

217217
/* Define if the xar_open() function is supported this platform. */
218218
#cmakedefine HAVE_LIBXAR ${HAVE_LIBXAR}

llvm/lib/Support/CMakeLists.txt

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,23 @@ if(LLVM_ENABLE_ZLIB)
22
set(imported_libs ZLIB::ZLIB)
33
endif()
44

5+
function(get_system_libname libpath libname)
6+
get_filename_component(libpath ${libpath} NAME)
7+
set(prefixes ${CMAKE_FIND_LIBRARY_PREFIXES})
8+
set(suffixes ${CMAKE_FIND_LIBRARY_SUFFIXES})
9+
list(FILTER prefixes EXCLUDE REGEX "^\\s*$")
10+
list(FILTER suffixes EXCLUDE REGEX "^\\s*$")
11+
if( prefixes )
12+
string(REPLACE ";" "|" prefixes "${prefixes}")
13+
string(REGEX REPLACE "^(${prefixes})" "" libpath ${libpath})
14+
endif()
15+
if( suffixes )
16+
string(REPLACE ";" "|" suffixes "${suffixes}")
17+
string(REGEX REPLACE "(${suffixes})$" "" libpath ${libpath})
18+
endif()
19+
set(${libname} "${libpath}" PARENT_SCOPE)
20+
endfunction()
21+
522
if( MSVC OR MINGW )
623
# libuuid required for FOLDERID_Profile usage in lib/Support/Windows/Path.inc.
724
# advapi32 required for CryptAcquireContextW in lib/Support/Windows/Path.inc.
@@ -21,10 +38,8 @@ elseif( CMAKE_HOST_UNIX )
2138
STRING(REGEX REPLACE "^lib" "" Backtrace_LIBFILE ${Backtrace_LIBFILE})
2239
set(system_libs ${system_libs} ${Backtrace_LIBFILE})
2340
endif()
24-
if(LLVM_ENABLE_TERMINFO)
25-
if(HAVE_TERMINFO)
26-
set(system_libs ${system_libs} ${TERMINFO_LIBS})
27-
endif()
41+
if( LLVM_ENABLE_TERMINFO )
42+
set(imported_libs ${imported_libs} "${TERMINFO_LIB}")
2843
endif()
2944
if( LLVM_ENABLE_THREADS AND (HAVE_LIBATOMIC OR HAVE_CXX_LIBATOMICS64) )
3045
set(system_libs ${system_libs} atomic)
@@ -237,20 +252,15 @@ if(LLVM_ENABLE_ZLIB)
237252
if(NOT zlib_library)
238253
get_property(zlib_library TARGET ZLIB::ZLIB PROPERTY LOCATION)
239254
endif()
240-
get_filename_component(zlib_library ${zlib_library} NAME)
241-
if(CMAKE_STATIC_LIBRARY_PREFIX AND CMAKE_STATIC_LIBRARY_SUFFIX AND
242-
zlib_library MATCHES "^${CMAKE_STATIC_LIBRARY_PREFIX}.*${CMAKE_STATIC_LIBRARY_SUFFIX}$")
243-
STRING(REGEX REPLACE "^${CMAKE_STATIC_LIBRARY_PREFIX}" "" zlib_library ${zlib_library})
244-
STRING(REGEX REPLACE "${CMAKE_STATIC_LIBRARY_SUFFIX}$" "" zlib_library ${zlib_library})
245-
endif()
246-
if(CMAKE_SHARED_LIBRARY_PREFIX AND CMAKE_SHARED_LIBRARY_SUFFIX AND
247-
zlib_library MATCHES "^${CMAKE_SHARED_LIBRARY_PREFIX}.*${CMAKE_SHARED_LIBRARY_SUFFIX}$")
248-
STRING(REGEX REPLACE "^${CMAKE_SHARED_LIBRARY_PREFIX}" "" zlib_library ${zlib_library})
249-
STRING(REGEX REPLACE "${CMAKE_SHARED_LIBRARY_SUFFIX}$" "" zlib_library ${zlib_library})
250-
endif()
255+
get_system_libname(${zlib_library} zlib_library)
251256
set(llvm_system_libs ${llvm_system_libs} "${zlib_library}")
252257
endif()
253258

259+
if(LLVM_ENABLE_TERMINFO)
260+
get_system_libname(${TERMINFO_LIB} terminfo_library)
261+
set(llvm_system_libs ${llvm_system_libs} "${terminfo_library}")
262+
endif()
263+
254264
set_property(TARGET LLVMSupport PROPERTY LLVM_SYSTEM_LIBS "${llvm_system_libs}")
255265

256266
if(LLVM_WITH_Z3)

llvm/lib/Support/Unix/Process.inc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ unsigned Process::StandardErrColumns() {
313313
return getColumns();
314314
}
315315

316-
#ifdef HAVE_TERMINFO
316+
#ifdef LLVM_ENABLE_TERMINFO
317317
// We manually declare these extern functions because finding the correct
318318
// headers from various terminfo, curses, or other sources is harder than
319319
// writing their specs down.
@@ -323,12 +323,12 @@ extern "C" int del_curterm(struct term *termp);
323323
extern "C" int tigetnum(char *capname);
324324
#endif
325325

326-
#ifdef HAVE_TERMINFO
326+
#ifdef LLVM_ENABLE_TERMINFO
327327
static ManagedStatic<std::mutex> TermColorMutex;
328328
#endif
329329

330330
static bool terminalHasColors(int fd) {
331-
#ifdef HAVE_TERMINFO
331+
#ifdef LLVM_ENABLE_TERMINFO
332332
// First, acquire a global lock because these C routines are thread hostile.
333333
std::lock_guard<std::mutex> G(*TermColorMutex);
334334

llvm/utils/gn/secondary/llvm/include/llvm/Config/BUILD.gn

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,9 +286,9 @@ write_cmake_config("config") {
286286
}
287287

288288
if (llvm_enable_terminfo) {
289-
values += [ "HAVE_TERMINFO=1" ]
289+
values += [ "LLVM_ENABLE_TERMINFO=1" ]
290290
} else {
291-
values += [ "HAVE_TERMINFO=" ]
291+
values += [ "LLVM_ENABLE_TERMINFO=" ]
292292
}
293293

294294
if (llvm_enable_dia_sdk) {

0 commit comments

Comments
 (0)