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

Eric Kilmer via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 12 06:18:34 PDT 2023


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

>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 1/2] [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()
 

>From 1ab145fe11fdb0938a6871b510d15631f5ae8d33 Mon Sep 17 00:00:00 2001
From: Eric Kilmer <eric.d.kilmer at gmail.com>
Date: Thu, 12 Oct 2023 09:17:47 -0400
Subject: [PATCH 2/2] Descriptive variable name for source code being checked

---
 llvm/cmake/modules/FindFFI.cmake      | 6 +++---
 llvm/cmake/modules/FindTerminfo.cmake | 6 +++---
 2 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/llvm/cmake/modules/FindFFI.cmake b/llvm/cmake/modules/FindFFI.cmake
index 265cc438d522c55..c9ba104601872eb 100644
--- a/llvm/cmake/modules/FindFFI.cmake
+++ b/llvm/cmake/modules/FindFFI.cmake
@@ -40,7 +40,7 @@ if(FFI_LIBRARIES)
   include(CMakePushCheckState)
   cmake_push_check_state()
   list(APPEND CMAKE_REQUIRED_LIBRARIES ${FFI_LIBRARIES})
-  set(SRC [=[
+  set(HAVE_FFI_CALL_SRC [=[
     #ifdef __cplusplus
     extern "C" {
     #endif
@@ -54,10 +54,10 @@ if(FFI_LIBRARIES)
     ]=])
   if(DEFINED CMAKE_C_COMPILER)
     include(CheckCSourceCompiles)
-    check_c_source_compiles("${SRC}" HAVE_FFI_CALL)
+    check_c_source_compiles("${HAVE_FFI_CALL_SRC}" HAVE_FFI_CALL)
   else()
     include(CheckCXXSourceCompiles)
-    check_cxx_source_compiles("${SRC}" HAVE_FFI_CALL)
+    check_cxx_source_compiles("${HAVE_FFI_CALL_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 3f7c8112ee26a4a..163af6697067710 100644
--- a/llvm/cmake/modules/FindTerminfo.cmake
+++ b/llvm/cmake/modules/FindTerminfo.cmake
@@ -17,7 +17,7 @@ if(Terminfo_LIBRARIES)
   include(CMakePushCheckState)
   cmake_push_check_state()
   list(APPEND CMAKE_REQUIRED_LIBRARIES ${Terminfo_LIBRARIES})
-  set(SRC [=[
+  set(Terminfo_LINKABLE_SRC [=[
     #ifdef __cplusplus
     extern "C" {
     #endif
@@ -29,10 +29,10 @@ if(Terminfo_LIBRARIES)
     ]=])
   if(DEFINED CMAKE_C_COMPILER)
     include(CheckCSourceCompiles)
-    check_c_source_compiles("${SRC}" Terminfo_LINKABLE)
+    check_c_source_compiles("${Terminfo_LINKABLE_SRC}" Terminfo_LINKABLE)
   else()
     include(CheckCXXSourceCompiles)
-    check_cxx_source_compiles("${SRC}" Terminfo_LINKABLE)
+    check_cxx_source_compiles("${Terminfo_LINKABLE_SRC}" Terminfo_LINKABLE)
   endif()
   cmake_pop_check_state()
 endif()



More information about the llvm-commits mailing list