[llvm] [llvm][CMake] Check dependency cxx source compiles (PR #68549)

Eric Kilmer via llvm-commits llvm-commits at lists.llvm.org
Sun Oct 8 19:46:26 PDT 2023


https://github.com/ekilmer created https://github.com/llvm/llvm-project/pull/68549

If a CMake project doesn't enable the C language, then the CMake FFI and Terminfo find modules will fail their checks for linking.

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

Fixes #53950

>From 65cfd5e81e8d5344a03b87e5e3b7f2d9bcae7f13 Mon Sep 17 00:00:00 2001
From: Eric Kilmer <eric.d.kilmer at gmail.com>
Date: Sun, 8 Oct 2023 22:05:44 -0400
Subject: [PATCH] [llvm][CMake] Check dependency cxx source compiles

If a CMake project doesn't enable the C language, then the CMake FFI and
Terminfo find modules will fail their checks for linking.

This commit allows projects to enable only C++ by checking if a C
compiler is set, and if it is not, it checks whether C++ compilation
succeeds.
---
 llvm/cmake/modules/FindFFI.cmake      | 20 ++++++++++++++++----
 llvm/cmake/modules/FindTerminfo.cmake | 20 ++++++++++++++++----
 2 files changed, 32 insertions(+), 8 deletions(-)

diff --git a/llvm/cmake/modules/FindFFI.cmake b/llvm/cmake/modules/FindFFI.cmake
index a493a89d630171f..265cc438d522c55 100644
--- a/llvm/cmake/modules/FindFFI.cmake
+++ b/llvm/cmake/modules/FindFFI.cmake
@@ -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(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("${SRC}" HAVE_FFI_CALL)
+  else()
+    include(CheckCXXSourceCompiles)
+    check_cxx_source_compiles("${SRC}" HAVE_FFI_CALL)
+  endif()
   cmake_pop_check_state()
 endif()
 
diff --git a/llvm/cmake/modules/FindTerminfo.cmake b/llvm/cmake/modules/FindTerminfo.cmake
index eef1f95853eb27c..3f7c8112ee26a4a 100644
--- a/llvm/cmake/modules/FindTerminfo.cmake
+++ b/llvm/cmake/modules/FindTerminfo.cmake
@@ -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(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("${SRC}" Terminfo_LINKABLE)
+  else()
+    include(CheckCXXSourceCompiles)
+    check_cxx_source_compiles("${SRC}" Terminfo_LINKABLE)
+  endif()
   cmake_pop_check_state()
 endif()
 



More information about the llvm-commits mailing list