[flang] [llvm] [flang] Generate quadmath_wrapper.h for Flang Evaluate. (PR #132817)

via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 24 12:26:22 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-flang-semantics

Author: Slava Zakharin (vzakhari)

<details>
<summary>Changes</summary>

When building Flang with Clang, we need to do the same quadmath.h
wrapping as we do for flang-rt. I extracted the CMake code
into FlangCommon.cmake, and cleaned up the arguments passing
to execute_process (note that `-###` was treated as `-` in the original
code, because `#` starts a comment). I believe the Clang command
does not require the input source file, so I removed it as well.


---
Full diff: https://github.com/llvm/llvm-project/pull/132817.diff


9 Files Affected:

- (modified) flang-rt/CMakeLists.txt (+1-39) 
- (removed) flang-rt/cmake/clang_gcc_root.cpp (-1) 
- (modified) flang-rt/cmake/quadmath_wrapper.h.in (+1-1) 
- (modified) flang-rt/lib/quadmath/CMakeLists.txt (+1-1) 
- (modified) flang/cmake/modules/FlangCommon.cmake (+47) 
- (added) flang/cmake/quadmath_wrapper.h.in (+23) 
- (modified) flang/lib/Evaluate/CMakeLists.txt (+3-3) 
- (modified) flang/lib/Evaluate/host.h (+1-1) 
- (modified) flang/lib/Evaluate/intrinsics-library.cpp (+1-1) 


``````````diff
diff --git a/flang-rt/CMakeLists.txt b/flang-rt/CMakeLists.txt
index a0b998dc3abd9..d640cf2d49e95 100644
--- a/flang-rt/CMakeLists.txt
+++ b/flang-rt/CMakeLists.txt
@@ -261,44 +261,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")
-    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 #
 #####################
@@ -320,7 +282,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED YES)
 
 
 configure_file(cmake/config.h.cmake.in config.h)
-if (FLANG_RT_INCLUDE_QUADMATH_H)
+if (FLANG_INCLUDE_QUADMATH_H)
   configure_file("cmake/quadmath_wrapper.h.in" "${FLANG_RT_BINARY_DIR}/quadmath_wrapper.h")
 endif ()
 
diff --git a/flang-rt/cmake/clang_gcc_root.cpp b/flang-rt/cmake/clang_gcc_root.cpp
deleted file mode 100644
index c3aeba4bb65d9..0000000000000
--- a/flang-rt/cmake/clang_gcc_root.cpp
+++ /dev/null
@@ -1 +0,0 @@
-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
index c29358efcdb0d..ce90b58fbce76 100644
--- a/flang-rt/cmake/quadmath_wrapper.h.in
+++ b/flang-rt/cmake/quadmath_wrapper.h.in
@@ -6,4 +6,4 @@
  *
  *===----------------------------------------------------------------------===*/
 
-#include ${FLANG_RT_INCLUDE_QUADMATH_H}
+#include ${FLANG_INCLUDE_QUADMATH_H}
diff --git a/flang-rt/lib/quadmath/CMakeLists.txt b/flang-rt/lib/quadmath/CMakeLists.txt
index eaf8202d7bce9..353c72def24de 100644
--- a/flang-rt/lib/quadmath/CMakeLists.txt
+++ b/flang-rt/lib/quadmath/CMakeLists.txt
@@ -78,7 +78,7 @@ target_include_directories(FortranFloat128MathILib INTERFACE
 
 if (FLANG_RUNTIME_F128_MATH_LIB)
   if (${FLANG_RUNTIME_F128_MATH_LIB} STREQUAL "libquadmath")
-    if(FLANG_RT_INCLUDE_QUADMATH_H)
+    if(FLANG_INCLUDE_QUADMATH_H)
       add_compile_definitions(HAS_QUADMATHLIB)
     else()
       message(FATAL_ERROR
diff --git a/flang/cmake/modules/FlangCommon.cmake b/flang/cmake/modules/FlangCommon.cmake
index bb2a76cb19453..240608c61f974 100644
--- a/flang/cmake/modules/FlangCommon.cmake
+++ b/flang/cmake/modules/FlangCommon.cmake
@@ -45,3 +45,50 @@ if (IS_BIGENDIAN)
 else ()
   add_compile_definitions(FLANG_LITTLE_ENDIAN=1)
 endif ()
+
+# Discover the GCC installation, when the build compiler is Clang,
+# and try to find quadmath.h there. Set FLANG_INCLUDE_QUADMATH_H
+# to the path to quadmath.h, if found.
+if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
+  if (NOT DEFINED FLANG_GCC_RESOURCE_DIR)
+    set(FLANG_GCC_RESOURCE_DIR "FLANG_GCC_RESOURCE_DIR-NOTFOUND")
+    # Prepare CMAKE_CXX_FLAGS so that they can be passed to execute_process
+    # as separate flags.
+    separate_arguments(flags UNIX_COMMAND "${CMAKE_CXX_FLAGS}")
+    set(hash_flag "-###")
+    set(command ${CMAKE_CXX_COMPILER} ${flags} ${hash_flag} -v)
+    execute_process(
+      COMMAND ${command}
+      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_GCC_RESOURCE_DIR "${CMAKE_MATCH_1}")
+        message(STATUS "Found GCC installation selected by Clang: ${FLANG_GCC_RESOURCE_DIR}")
+        break()
+      endif ()
+    endforeach ()
+    set(FLANG_GCC_RESOURCE_DIR "${FLANG_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_INCLUDE_QUADMATH_H "<quadmath.h>")
+elseif (FLANG_GCC_RESOURCE_DIR)
+  cmake_push_check_state()
+    list(APPEND CMAKE_REQUIRED_INCLUDES "${FLANG_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_INCLUDE_QUADMATH_H "\"${FLANG_GCC_RESOURCE_DIR}/include/quadmath.h\"")
+  endif ()
+endif ()
+
diff --git a/flang/cmake/quadmath_wrapper.h.in b/flang/cmake/quadmath_wrapper.h.in
new file mode 100644
index 0000000000000..da08ec1230ada
--- /dev/null
+++ b/flang/cmake/quadmath_wrapper.h.in
@@ -0,0 +1,23 @@
+/*===-- 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
+ *
+ *===----------------------------------------------------------------------===*/
+
+#ifdef __clang_major__
+/*
+ * _Complex inside quadmath.h triggers c99-extension warnings,
+ * when this header file is included into a C++ file.
+ * Disable them just during the inclusion of quadmath.h.
+ */
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wc99-extensions"
+#endif
+
+#include ${FLANG_INCLUDE_QUADMATH_H}
+
+#ifdef __clang_major__
+#pragma clang diagnostic pop
+#endif
diff --git a/flang/lib/Evaluate/CMakeLists.txt b/flang/lib/Evaluate/CMakeLists.txt
index e0bdb6b2d24d8..7eb8ff934af46 100644
--- a/flang/lib/Evaluate/CMakeLists.txt
+++ b/flang/lib/Evaluate/CMakeLists.txt
@@ -21,12 +21,12 @@ if (LIBPGMATH_DIR)
   endif()
 endif()
 
-check_include_file(quadmath.h FOUND_QUADMATH_HEADER)
 check_library_exists(quadmath sinq "" FOUND_QUADMATH_LIB)
-if(FOUND_QUADMATH_HEADER AND FOUND_QUADMATH_LIB)
+if (FLANG_INCLUDE_QUADMATH_H AND FOUND_QUADMATH_LIB)
+  configure_file("${FLANG_SOURCE_DIR}/cmake/quadmath_wrapper.h.in" "${CMAKE_CURRENT_BINARY_DIR}/quadmath_wrapper.h")
   add_compile_definitions(HAS_QUADMATHLIB)
   set(QUADMATHLIB quadmath)
-endif()
+endif ()
 
 add_flang_library(FortranEvaluate
   call.cpp
diff --git a/flang/lib/Evaluate/host.h b/flang/lib/Evaluate/host.h
index 7edc254d6d4c7..fbb52f2886a40 100644
--- a/flang/lib/Evaluate/host.h
+++ b/flang/lib/Evaluate/host.h
@@ -18,7 +18,7 @@
 // to safely refer to this hardware type.
 
 #if HAS_QUADMATHLIB
-#include "quadmath.h"
+#include "quadmath_wrapper.h"
 #include "flang/Common/float128.h"
 #endif
 #include "flang/Evaluate/type.h"
diff --git a/flang/lib/Evaluate/intrinsics-library.cpp b/flang/lib/Evaluate/intrinsics-library.cpp
index d2c1be65dca44..6363acfc5eb40 100644
--- a/flang/lib/Evaluate/intrinsics-library.cpp
+++ b/flang/lib/Evaluate/intrinsics-library.cpp
@@ -23,7 +23,7 @@
 #include <complex>
 #include <functional>
 #if HAS_QUADMATHLIB
-#include "quadmath.h"
+#include "quadmath_wrapper.h"
 #endif
 #include "flang/Common/float128.h"
 #include "flang/Common/float80.h"

``````````

</details>


https://github.com/llvm/llvm-project/pull/132817


More information about the llvm-commits mailing list