Skip to content

Commit 7341753

Browse files
authored
[Flang-RT] Environment introspection for quadmath.h (#130411)
When compiling Flang-RT with Clang, query Clang for the GCC installation it uses. If found, create `quadmath_wrapper.h` that points to the `quadmath.h` of that GCC installation. `quadmath.h` is only available when compiling with gcc, and Clang has no equivalent even though gcc's version compiles fine with Clang (at least up to and including gcc 13). It is still available into gcc's installation resource dir (in constrast to a system-wide indirectory such as `/usr/include` or `/usr/local/include`) and therefore not available to any compiler other than the gcc of that installation. quadmath may also be a different OS package than gcc itself, so it is not necessarily presesent. Clang actually already appropriates a GCC installation for its libraries such that `libquadmath.a` is already found, but it does not do so for the include paths. Because adding that directory to the header search path may have wide-reaching consquences, we create only a wrapper header that points to the real `quadmath.h` in the same GCC installation that Clang uses.
1 parent 389a705 commit 7341753

File tree

6 files changed

+56
-5
lines changed

6 files changed

+56
-5
lines changed

flang-rt/CMakeLists.txt

+43-1
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,46 @@ set(HAVE_BACKTRACE ${Backtrace_FOUND})
256256
set(BACKTRACE_HEADER ${Backtrace_HEADER})
257257

258258

259+
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
260+
if (NOT DEFINED FLANG_RT_GCC_RESOURCE_DIR)
261+
set(FLANG_RT_GCC_RESOURCE_DIR "FLANG_RT_GCC_RESOURCE_DIR-NOTFOUND")
262+
execute_process(
263+
COMMAND "${CMAKE_CXX_COMPILER}" -v -c "${FLANG_RT_SOURCE_DIR}/cmake/clang_gcc_root.cpp" ${CMAKE_CXX_FLAGS} -###
264+
ERROR_FILE "${CMAKE_CURRENT_BINARY_DIR}/clang_gcc_root_result"
265+
)
266+
file(STRINGS "${CMAKE_CURRENT_BINARY_DIR}/clang_gcc_root_result" _errorresult)
267+
foreach (_line IN LISTS _errorresult)
268+
string(REGEX MATCH
269+
"^Selected GCC installation: (.+)$"
270+
_match
271+
"${_line}")
272+
if (CMAKE_MATCH_1)
273+
set(FLANG_RT_GCC_RESOURCE_DIR "${CMAKE_MATCH_1}")
274+
message(STATUS "Found GCC installation selected by Clang: ${FLANG_RT_GCC_RESOURCE_DIR}")
275+
break()
276+
endif ()
277+
endforeach ()
278+
set(FLANG_RT_GCC_RESOURCE_DIR "${FLANG_RT_GCC_RESOURCE_DIR}" CACHE INTERNAL "Path to GCC's resource dir selected by Clang" FORCE)
279+
endif ()
280+
endif ()
281+
282+
check_include_file("quadmath.h" FOUND_QUADMATH_H)
283+
if (FOUND_QUADMATH_H)
284+
message(STATUS "quadmath.h found without additional include paths")
285+
set(FLANG_RT_INCLUDE_QUADMATH_H "<quadmath.h>")
286+
elseif (FLANG_RT_GCC_RESOURCE_DIR)
287+
cmake_push_check_state()
288+
list(APPEND CMAKE_REQUIRED_INCLUDES "${FLANG_RT_GCC_RESOURCE_DIR}/include")
289+
check_include_file("quadmath.h" FOUND_GCC_QUADMATH_H)
290+
cmake_pop_check_state()
291+
if (FOUND_GCC_QUADMATH_H)
292+
message(STATUS "quadmath.h found in Clang's selected GCC installation")
293+
set(FLANG_RT_INCLUDE_QUADMATH_H "\"${FLANG_RT_GCC_RESOURCE_DIR}/include/quadmath.h\"")
294+
endif ()
295+
endif ()
296+
297+
298+
259299
#####################
260300
# Build Preparation #
261301
#####################
@@ -277,7 +317,9 @@ set(CMAKE_CXX_STANDARD_REQUIRED YES)
277317

278318

279319
configure_file(cmake/config.h.cmake.in config.h)
280-
320+
if (FLANG_RT_INCLUDE_QUADMATH_H)
321+
configure_file("cmake/quadmath_wrapper.h.in" "${FLANG_RT_BINARY_DIR}/quadmath_wrapper.h")
322+
endif ()
281323

282324
# The bootstrap build will create a phony target with the same as the top-level
283325
# directory ("flang-rt") and delegate it to the runtimes build dir.

flang-rt/cmake/clang_gcc_root.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
int main() { return 0; }

flang-rt/cmake/quadmath_wrapper.h.in

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/*===-- cmake/quadmath_wrapper.h.in ---------------------=-----------*- C -*-===
2+
*
3+
* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
* See https://llvm.org/LICENSE.txt for license information.
5+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
*
7+
*===----------------------------------------------------------------------===*/
8+
9+
#include ${FLANG_RT_INCLUDE_QUADMATH_H}

flang-rt/lib/quadmath/CMakeLists.txt

+1-2
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,7 @@ target_include_directories(FortranFloat128MathILib INTERFACE
7878

7979
if (FLANG_RUNTIME_F128_MATH_LIB)
8080
if (${FLANG_RUNTIME_F128_MATH_LIB} STREQUAL "libquadmath")
81-
check_include_file(quadmath.h FOUND_QUADMATH_HEADER)
82-
if(FOUND_QUADMATH_HEADER)
81+
if(FLANG_RT_INCLUDE_QUADMATH_H)
8382
add_compile_definitions(HAS_QUADMATHLIB)
8483
else()
8584
message(FATAL_ERROR

flang-rt/lib/quadmath/complex-math.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include "flang/Runtime/entry-names.h"
1414

1515
#if HAS_QUADMATHLIB
16-
#include "quadmath.h"
16+
#include "quadmath_wrapper.h"
1717
#define CAbs(x) cabsq(x)
1818
#define CAcos(x) cacosq(x)
1919
#define CAcosh(x) cacoshq(x)

flang-rt/lib/quadmath/math-entries.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ DEFINE_FALLBACK_F128(Yn)
112112

113113
#if HAS_QUADMATHLIB
114114
// Define wrapper callers for libquadmath.
115-
#include "quadmath.h"
115+
#include "quadmath_wrapper.h"
116116
DEFINE_SIMPLE_ALIAS(Abs, fabsq)
117117
DEFINE_SIMPLE_ALIAS(Acos, acosq)
118118
DEFINE_SIMPLE_ALIAS(Acosh, acoshq)

0 commit comments

Comments
 (0)