Skip to content

[Flang-RT] Environment introspection for quadmath.h #130411

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion flang-rt/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,46 @@ 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 #
#####################
Expand All @@ -273,7 +313,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.
Expand Down
1 change: 1 addition & 0 deletions flang-rt/cmake/clang_gcc_root.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
int main() { return 0; }
9 changes: 9 additions & 0 deletions flang-rt/cmake/quadmath_wrapper.h.in
Original file line number Diff line number Diff line change
@@ -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}
3 changes: 1 addition & 2 deletions flang-rt/lib/quadmath/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion flang-rt/lib/quadmath/complex-math.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion flang-rt/lib/quadmath/math-entries.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down