[llvm] [Flang-RT] Environment introspection for quadmath.h (PR #130411)

Michael Kruse via llvm-commits llvm-commits at lists.llvm.org
Sat Mar 8 04:56:47 PST 2025


https://github.com/Meinersbur updated https://github.com/llvm/llvm-project/pull/130411

>From 74fc2b5adf9018c8415b59d4a2865149b11eff99 Mon Sep 17 00:00:00 2001
From: Michael Kruse <llvm-project at meinersbur.de>
Date: Sat, 8 Mar 2025 12:58:09 +0100
Subject: [PATCH 1/2] System introspection for quadmath

---
 flang-rt/CMakeLists.txt              | 47 +++++++++++++++++++++++++++-
 flang-rt/cmake/clang_gcc_root.cpp    |  3 ++
 flang-rt/cmake/quadmath_wrapper.h.in |  9 ++++++
 flang-rt/lib/quadmath/CMakeLists.txt |  3 +-
 flang-rt/lib/quadmath/complex-math.h |  2 +-
 flang-rt/lib/quadmath/math-entries.h |  2 +-
 6 files changed, 61 insertions(+), 5 deletions(-)
 create mode 100644 flang-rt/cmake/clang_gcc_root.cpp
 create mode 100644 flang-rt/cmake/quadmath_wrapper.h.in

diff --git a/flang-rt/CMakeLists.txt b/flang-rt/CMakeLists.txt
index 9f890a015621a..bc16ef4523410 100644
--- a/flang-rt/CMakeLists.txt
+++ b/flang-rt/CMakeLists.txt
@@ -252,6 +252,49 @@ set(HAVE_BACKTRACE ${Backtrace_FOUND})
 set(BACKTRACE_HEADER ${Backtrace_HEADER})
 
 
+
+if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
+  if (NOT DEFINED FLANG_RT_GCC_RESOURCE_DIR)
+    set(FLANG_RT_GCC_RESOURCE_DIR "FLANG_RT_GCC_RESOURCE_DIR-NOTFOUND")
+    execute_process(
+      COMMAND "${CMAKE_CXX_COMPILER}" -v -c "${FLANG_RT_SOURCE_DIR}/cmake/clang_gcc_root.cpp" ${CMAKE_CXX_FLAGS} -###
+      ERROR_FILE "${CMAKE_CURRENT_BINARY_DIR}/clang_gcc_root_result"
+    )
+    file(STRINGS "${CMAKE_CURRENT_BINARY_DIR}/clang_gcc_root_result" _errorresult)
+    foreach (_line IN LISTS _errorresult)
+      string(REGEX MATCH
+        "^Selected GCC installation: (.+)$"
+        _match
+        "${_line}")
+      if (CMAKE_MATCH_1)
+        set(FLANG_RT_GCC_RESOURCE_DIR "${CMAKE_MATCH_1}")
+        message(STATUS "Found GCC installation selected by Clang: ${FLANG_RT_GCC_RESOURCE_DIR}")
+        break()
+      endif ()
+    endforeach ()
+    set(FLANG_RT_GCC_RESOURCE_DIR "${FLANG_RT_GCC_RESOURCE_DIR}" CACHE INTERNAL "Path to GCC's resource dir selected by Clang" FORCE)
+  endif ()
+endif ()
+
+
+check_include_file("quadmath.h" FOUND_QUADMATH_H)
+if (FOUND_QUADMATH_H)
+  message(STATUS "quadmath.h found without additional include paths")
+  set(FLANG_RT_INCLUDE_QUADMATH_H "<quadmath.h>")
+elseif (FLANG_RT_GCC_RESOURCE_DIR)
+  cmake_push_check_state()
+    list(APPEND CMAKE_REQUIRED_INCLUDES "${FLANG_RT_GCC_RESOURCE_DIR}/include")
+    check_include_file("quadmath.h" FOUND_GCC_QUADMATH_H)
+  cmake_pop_check_state()
+  if (FOUND_GCC_QUADMATH_H)
+    message(STATUS "quadmath.h found in Clang's selected GCC installation")
+    set(FLANG_RT_INCLUDE_QUADMATH_H "\"${FLANG_RT_GCC_RESOURCE_DIR}/include/quadmath.h\"")
+  endif ()
+endif ()
+
+
+
+
 #####################
 # Build Preparation #
 #####################
@@ -273,7 +316,9 @@ set(CMAKE_CXX_STANDARD_REQUIRED YES)
 
 
 configure_file(cmake/config.h.cmake.in config.h)
-
+if (FLANG_RT_INCLUDE_QUADMATH_H)
+  configure_file("cmake/quadmath_wrapper.h.in" "${FLANG_RT_BINARY_DIR}/quadmath_wrapper.h")
+endif ()
 
 # The bootstrap build will create a phony target with the same as the top-level
 # directory ("flang-rt") and delegate it to the runtimes build dir.
diff --git a/flang-rt/cmake/clang_gcc_root.cpp b/flang-rt/cmake/clang_gcc_root.cpp
new file mode 100644
index 0000000000000..52b490a10a0af
--- /dev/null
+++ b/flang-rt/cmake/clang_gcc_root.cpp
@@ -0,0 +1,3 @@
+int main() {
+  return 0;
+}
\ No newline at end of file
diff --git a/flang-rt/cmake/quadmath_wrapper.h.in b/flang-rt/cmake/quadmath_wrapper.h.in
new file mode 100644
index 0000000000000..c29358efcdb0d
--- /dev/null
+++ b/flang-rt/cmake/quadmath_wrapper.h.in
@@ -0,0 +1,9 @@
+/*===-- cmake/quadmath_wrapper.h.in ---------------------=-----------*- C -*-===
+ *
+ * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+ * See https://llvm.org/LICENSE.txt for license information.
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ *
+ *===----------------------------------------------------------------------===*/
+
+#include ${FLANG_RT_INCLUDE_QUADMATH_H}
diff --git a/flang-rt/lib/quadmath/CMakeLists.txt b/flang-rt/lib/quadmath/CMakeLists.txt
index 4f113216b42c8..eaf8202d7bce9 100644
--- a/flang-rt/lib/quadmath/CMakeLists.txt
+++ b/flang-rt/lib/quadmath/CMakeLists.txt
@@ -78,8 +78,7 @@ target_include_directories(FortranFloat128MathILib INTERFACE
 
 if (FLANG_RUNTIME_F128_MATH_LIB)
   if (${FLANG_RUNTIME_F128_MATH_LIB} STREQUAL "libquadmath")
-    check_include_file(quadmath.h FOUND_QUADMATH_HEADER)
-    if(FOUND_QUADMATH_HEADER)
+    if(FLANG_RT_INCLUDE_QUADMATH_H)
       add_compile_definitions(HAS_QUADMATHLIB)
     else()
       message(FATAL_ERROR
diff --git a/flang-rt/lib/quadmath/complex-math.h b/flang-rt/lib/quadmath/complex-math.h
index 424ed84da4e01..b76bf03732938 100644
--- a/flang-rt/lib/quadmath/complex-math.h
+++ b/flang-rt/lib/quadmath/complex-math.h
@@ -13,7 +13,7 @@
 #include "flang/Runtime/entry-names.h"
 
 #if HAS_QUADMATHLIB
-#include "quadmath.h"
+#include "quadmath_wrapper.h"
 #define CAbs(x) cabsq(x)
 #define CAcos(x) cacosq(x)
 #define CAcosh(x) cacoshq(x)
diff --git a/flang-rt/lib/quadmath/math-entries.h b/flang-rt/lib/quadmath/math-entries.h
index 6e47f32cc8a43..91ad80c8197f4 100644
--- a/flang-rt/lib/quadmath/math-entries.h
+++ b/flang-rt/lib/quadmath/math-entries.h
@@ -112,7 +112,7 @@ DEFINE_FALLBACK_F128(Yn)
 
 #if HAS_QUADMATHLIB
 // Define wrapper callers for libquadmath.
-#include "quadmath.h"
+#include "quadmath_wrapper.h"
 DEFINE_SIMPLE_ALIAS(Abs, fabsq)
 DEFINE_SIMPLE_ALIAS(Acos, acosq)
 DEFINE_SIMPLE_ALIAS(Acosh, acoshq)

>From 36e922511f56a2fed19f29b76bf70d6c64943c7b Mon Sep 17 00:00:00 2001
From: Michael Kruse <llvm-project at meinersbur.de>
Date: Sat, 8 Mar 2025 13:56:21 +0100
Subject: [PATCH 2/2] clang-format

---
 flang-rt/CMakeLists.txt           | 3 ---
 flang-rt/cmake/clang_gcc_root.cpp | 4 +---
 2 files changed, 1 insertion(+), 6 deletions(-)

diff --git a/flang-rt/CMakeLists.txt b/flang-rt/CMakeLists.txt
index bc16ef4523410..704eed67e9493 100644
--- a/flang-rt/CMakeLists.txt
+++ b/flang-rt/CMakeLists.txt
@@ -252,7 +252,6 @@ set(HAVE_BACKTRACE ${Backtrace_FOUND})
 set(BACKTRACE_HEADER ${Backtrace_HEADER})
 
 
-
 if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
   if (NOT DEFINED FLANG_RT_GCC_RESOURCE_DIR)
     set(FLANG_RT_GCC_RESOURCE_DIR "FLANG_RT_GCC_RESOURCE_DIR-NOTFOUND")
@@ -276,7 +275,6 @@ if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
   endif ()
 endif ()
 
-
 check_include_file("quadmath.h" FOUND_QUADMATH_H)
 if (FOUND_QUADMATH_H)
   message(STATUS "quadmath.h found without additional include paths")
@@ -294,7 +292,6 @@ endif ()
 
 
 
-
 #####################
 # Build Preparation #
 #####################
diff --git a/flang-rt/cmake/clang_gcc_root.cpp b/flang-rt/cmake/clang_gcc_root.cpp
index 52b490a10a0af..c3aeba4bb65d9 100644
--- a/flang-rt/cmake/clang_gcc_root.cpp
+++ b/flang-rt/cmake/clang_gcc_root.cpp
@@ -1,3 +1 @@
-int main() {
-  return 0;
-}
\ No newline at end of file
+int main() { return 0; }
\ No newline at end of file



More information about the llvm-commits mailing list