Skip to content

Commit

Permalink
[llvm][CMake] Check dependency cxx source compiles (#68549)
Browse files Browse the repository at this point in the history
If a CMake project doesn't enable the C language, then the CMake FFI and
Terminfo find modules will fail their checks for compilation and
linking.

This commit allows projects to enable only C++ by first checking if a C
compiler is set before testing C source compilation; if not, it checks
whether C++ compilation succeeds.

Fixes #53950
  • Loading branch information
ekilmer committed Oct 18, 2023
1 parent cba4e7e commit e3ae2a5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
20 changes: 16 additions & 4 deletions llvm/cmake/modules/FindFFI.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,27 @@ find_library(FFI_LIBRARIES ffi PATHS ${FFI_LIBRARY_DIR})

if(FFI_LIBRARIES)
include(CMakePushCheckState)
include(CheckCSourceCompiles)
cmake_push_check_state()
list(APPEND CMAKE_REQUIRED_LIBRARIES ${FFI_LIBRARIES})
check_c_source_compiles("
set(HAVE_FFI_CALL_SRC [=[
#ifdef __cplusplus
extern "C" {
#endif
struct ffi_cif;
typedef struct ffi_cif ffi_cif;
void ffi_call(ffi_cif *cif, void (*fn)(void), void *rvalue, void **avalue);
int main(void) { ffi_call(0, 0, 0, 0); }"
HAVE_FFI_CALL)
#ifdef __cplusplus
}
#endif
int main(void) { ffi_call(0, 0, 0, 0); }
]=])
if(DEFINED CMAKE_C_COMPILER)
include(CheckCSourceCompiles)
check_c_source_compiles("${HAVE_FFI_CALL_SRC}" HAVE_FFI_CALL)
else()
include(CheckCXXSourceCompiles)
check_cxx_source_compiles("${HAVE_FFI_CALL_SRC}" HAVE_FFI_CALL)
endif()
cmake_pop_check_state()
endif()

Expand Down
20 changes: 16 additions & 4 deletions llvm/cmake/modules/FindTerminfo.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,25 @@ find_library(Terminfo_LIBRARIES NAMES terminfo tinfo curses ncurses ncursesw)

if(Terminfo_LIBRARIES)
include(CMakePushCheckState)
include(CheckCSourceCompiles)
cmake_push_check_state()
list(APPEND CMAKE_REQUIRED_LIBRARIES ${Terminfo_LIBRARIES})
check_c_source_compiles("
set(Terminfo_LINKABLE_SRC [=[
#ifdef __cplusplus
extern "C" {
#endif
int setupterm(char *term, int filedes, int *errret);
int main(void) { return setupterm(0, 0, 0); }"
Terminfo_LINKABLE)
#ifdef __cplusplus
}
#endif
int main(void) { return setupterm(0, 0, 0); }
]=])
if(DEFINED CMAKE_C_COMPILER)
include(CheckCSourceCompiles)
check_c_source_compiles("${Terminfo_LINKABLE_SRC}" Terminfo_LINKABLE)
else()
include(CheckCXXSourceCompiles)
check_cxx_source_compiles("${Terminfo_LINKABLE_SRC}" Terminfo_LINKABLE)
endif()
cmake_pop_check_state()
endif()

Expand Down

0 comments on commit e3ae2a5

Please sign in to comment.