From 871c6900569bb92a652a447b1b481b7627cc50d4 Mon Sep 17 00:00:00 2001 From: Michael Kruse Date: Wed, 2 Oct 2024 12:24:01 +0200 Subject: [PATCH] Common CMake code --- FortranRuntime/CMakeLists.txt | 52 +------------------ .../cmake/modules/AddFortranRuntime.cmake | 9 +++- .../cmake/modules/FlangCommon.cmake | 31 +++++++++++ FortranRuntime/lib/Runtime/CMakeLists.txt | 17 ------ flang/CMakeLists.txt | 10 +--- 5 files changed, 41 insertions(+), 78 deletions(-) create mode 100644 FortranRuntime/cmake/modules/FlangCommon.cmake diff --git a/FortranRuntime/CMakeLists.txt b/FortranRuntime/CMakeLists.txt index 5c20b496dea821..bda76dd87c0d7a 100644 --- a/FortranRuntime/CMakeLists.txt +++ b/FortranRuntime/CMakeLists.txt @@ -23,6 +23,7 @@ list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules" ) include(AddFortranRuntime) +include(FlangCommon) ############################ @@ -65,42 +66,16 @@ option(FORTRANRUNTIME_INCLUDE_TESTS "Generate build targets for the FortranRuntime unit tests." ${LLVM_INCLUDE_TESTS}) -# The out of tree builds of the compiler and the Fortran runtime -# must use the same setting of FLANG_RUNTIME_F128_MATH_LIB -# to be composable. Failure to synchronize this setting may result -# in linking errors or fatal failures in F128 runtime functions. -set(FORTRARUNTIME_F128_MATH_LIB "" CACHE STRING - "Specifies the target library used for implementing IEEE-754 128-bit float \ - math in F18 runtime, e.g. it might be libquadmath for targets where \ - REAL(16) is mapped to __float128, or libm for targets where REAL(16) \ - is mapped to long double, etc." - ) -if (FLANG_RUNTIME_F128_MATH_LIB) - add_compile_definitions( - FLANG_RUNTIME_F128_MATH_LIB="${FLANG_RUNTIME_F128_MATH_LIB}" - ) -endif() - option(FLANG_EXPERIMENTAL_CUDA_RUNTIME "Compile Fortran runtime as CUDA sources (experimental)" OFF) if (FLANG_EXPERIMENTAL_CUDA_RUNTIME) message(FATAL_ERROOR "FLANG_EXPERIMENTAL_CUDA_RUNTIME currently not supported") endif() - - ######################## # System Introspection # ######################## -include(TestBigEndian) -test_big_endian(IS_BIGENDIAN) -if (IS_BIGENDIAN) - add_compile_definitions(FLANG_BIG_ENDIAN=1) -else () - add_compile_definitions(FLANG_LITTLE_ENDIAN=1) -endif () - include(CheckCXXSymbolExists) include(CheckCXXSourceCompiles) check_cxx_symbol_exists(strerror_r string.h HAVE_STRERROR_R) @@ -114,26 +89,6 @@ check_cxx_source_compiles( " HAVE_DECL_STRERROR_S) -# Check if 128-bit float computations can be done via long double -check_cxx_source_compiles( - "#include - #if LDBL_MANT_DIG != 113 - #error LDBL_MANT_DIG != 113 - #endif - int main() { return 0; } - " - HAVE_LDBL_MANT_DIG_113) - -# Check if 128-bit float computations can be done via long double -check_cxx_source_compiles( - "#include - #if LDBL_MANT_DIG != 113 - #error LDBL_MANT_DIG != 113 - #endif - int main() { return 0; } - " - HAVE_LDBL_MANT_DIG_113) - # Search for clang_rt.builtins execute_process( COMMAND "${CMAKE_CXX_COMPILER}" "-print-libgcc-file-name" "-rtlib=compiler-rt" @@ -156,11 +111,6 @@ endif () set(CMAKE_CXX_STANDARD 17 CACHE STRING "C++ standard to conform to") set(CMAKE_CXX_STANDARD_REQUIRED YES) -# Disable libstdc++/libc++ assertions, even in an LLVM_ENABLE_ASSERTIONS build, -# to avoid an unwanted dependency on libstdc++/libc++.so. -add_definitions(-U_GLIBCXX_ASSERTIONS) -add_definitions(-U_LIBCPP_ENABLE_ASSERTIONS) - configure_file(cmake/config.h.cmake.in config.h) diff --git a/FortranRuntime/cmake/modules/AddFortranRuntime.cmake b/FortranRuntime/cmake/modules/AddFortranRuntime.cmake index e4dff982e6deea..5410cb769754b5 100644 --- a/FortranRuntime/cmake/modules/AddFortranRuntime.cmake +++ b/FortranRuntime/cmake/modules/AddFortranRuntime.cmake @@ -65,9 +65,9 @@ function (add_fortranruntime_library name) target_compile_features(${name} PRIVATE cxx_std_17) if (LLVM_COMPILER_IS_GCC_COMPATIBLE) - target_compile_options (${name} PRIVATE $<$:-fno-exceptions -fno-rtti -fno-unwind-tables -fno-asynchronous-unwind-tables>) + target_compile_options(${name} PRIVATE $<$:-fno-exceptions -fno-rtti -fno-unwind-tables -fno-asynchronous-unwind-tables>) elseif (MSVC) - target_compile_options (${name} PRIVATE $<$:/EHs-c- /GR->) + target_compile_options(${name} PRIVATE $<$:/EHs-c- /GR->) endif () # FortranRuntime's public headers @@ -79,6 +79,11 @@ function (add_fortranruntime_library name) # For configured config.h for be found target_include_directories(${name} PRIVATE "${FORTRANRUNTIME_BINARY_DIR}") + # Disable libstdc++/libc++ assertions, even in an LLVM_ENABLE_ASSERTIONS build, + # to avoid an unwanted dependency on libstdc++/libc++.so. + target_compile_definitions(${name} PRIVATE -U_GLIBCXX_ASSERTIONS) + target_compile_definitions(${name} PRIVATE -U_LIBCPP_ENABLE_ASSERTIONS) + # Clang/Flang, targeting the MSVC ABI, including clang-cl, should only depends on msv(u)crt. LLVM still emits libgcc/compiler-rt functions for 128-bit integer math (__udivti3, __modti3, __fixsfti, __floattidf, ...) that msvc does not support. # We are injecting a dependency to Compiler-RT where these are implemented. if (MSVC AND (CMAKE_CXX_COMPILER_ID MATCHES ".*Clang") AND FORTRANRUNTIME_LIBCALL) diff --git a/FortranRuntime/cmake/modules/FlangCommon.cmake b/FortranRuntime/cmake/modules/FlangCommon.cmake new file mode 100644 index 00000000000000..34c91048f47887 --- /dev/null +++ b/FortranRuntime/cmake/modules/FlangCommon.cmake @@ -0,0 +1,31 @@ +# The out of tree builds of the compiler and the Fortran runtime +# must use the same setting of FLANG_RUNTIME_F128_MATH_LIB +# to be composable. Failure to synchronize this setting may result +# in linking errors or fatal failures in F128 runtime functions. +set(FLANG_RUNTIME_F128_MATH_LIB "" CACHE STRING + "Specifies the target library used for implementing IEEE-754 128-bit float \ + math in F18 runtime, e.g. it might be libquadmath for targets where \ + REAL(16) is mapped to __float128, or libm for targets where REAL(16) \ + is mapped to long double, etc." + ) +if (FLANG_RUNTIME_F128_MATH_LIB) + add_compile_definitions(FLANG_RUNTIME_F128_MATH_LIB="${FLANG_RUNTIME_F128_MATH_LIB}") +endif() + +# Check if 128-bit float computations can be done via long double +check_cxx_source_compiles( + "#include + #if LDBL_MANT_DIG != 113 + #error LDBL_MANT_DIG != 113 + #endif + int main() { return 0; } + " + HAVE_LDBL_MANT_DIG_113) + +include(TestBigEndian) +test_big_endian(IS_BIGENDIAN) +if (IS_BIGENDIAN) + add_compile_definitions(FLANG_BIG_ENDIAN=1) +else () + add_compile_definitions(FLANG_LITTLE_ENDIAN=1) +endif () diff --git a/FortranRuntime/lib/Runtime/CMakeLists.txt b/FortranRuntime/lib/Runtime/CMakeLists.txt index 5a0cc2ff14383a..1938ed6e21f54a 100644 --- a/FortranRuntime/lib/Runtime/CMakeLists.txt +++ b/FortranRuntime/lib/Runtime/CMakeLists.txt @@ -153,21 +153,4 @@ else() FortranRuntime.dynamic FortranRuntime.dynamic_dbg ) - - if (MSVC) - # The Fortran files compiled with flang-new generate libcall functions such as `__udivti3` - # Add LLVM_ENABLE_RUNTIMES=compiler-rt for it to be available during the runtimes build - # FIXME: Can embed linker command --dependent-lib into object files as already done with FortranRuntime..lib - #if (NOT TARGET clang_rt.builtins-x86_64) - # message(WARNING - # "compiler-rt needed when compiling with msvc\n" - # "Flang/LLVM will emit code that calls into libgcc/compiler-rt, but using the MSVC linker will only link Microsoft's CRT automatically\n" - # "Add compiler-rt to LLVM_ENABLE_RUNTIMES to fix" - # ) - #endif () - #target_link_libraries(FortranRuntime.static PUBLIC clang_rt.builtins-x86_64) - #target_link_libraries(FortranRuntime.static_dbg PUBLIC clang_rt.builtins-x86_64) - #target_link_libraries(FortranRuntime.dynamic PUBLIC clang_rt.builtins-x86_64) - #target_link_libraries(FortranRuntime.dynamic_dbg PUBLIC clang_rt.builtins-x86_64) - endif () endif() diff --git a/flang/CMakeLists.txt b/flang/CMakeLists.txt index 827ed11e55001c..ac189de2386b16 100644 --- a/flang/CMakeLists.txt +++ b/flang/CMakeLists.txt @@ -253,6 +253,7 @@ include_directories(BEFORE # Add Flang-centric modules to cmake path. list(INSERT CMAKE_MODULE_PATH 0 "${FLANG_SOURCE_DIR}/cmake/modules" + "${FORTRANRUNTIME_SOURCE_DIR}/cmake/modules" "${LLVM_COMMON_CMAKE_UTILS}/Modules" ) include(AddFlang) @@ -326,14 +327,6 @@ if (FLANG_REPOSITORY_STRING) add_definitions(-DFLANG_REPOSITORY_STRING="${FLANG_REPOSITORY_STRING}") endif() -include(TestBigEndian) -test_big_endian(IS_BIGENDIAN) -if (IS_BIGENDIAN) - add_compile_definitions(FLANG_BIG_ENDIAN=1) -else () - add_compile_definitions(FLANG_LITTLE_ENDIAN=1) -endif () - # Configure Flang's Version.inc file. configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/include/flang/Version.inc.in @@ -432,6 +425,7 @@ endif() include(CMakeParseArguments) include(AddFlang) +include(FlangCommon) if (FLANG_INCLUDE_TESTS) add_compile_definitions(FLANG_INCLUDE_TESTS=1)