Skip to content

Commit

Permalink
Common CMake code
Browse files Browse the repository at this point in the history
  • Loading branch information
Meinersbur committed Oct 2, 2024
1 parent 20fc7c5 commit 871c690
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 78 deletions.
52 changes: 1 addition & 51 deletions FortranRuntime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ list(APPEND CMAKE_MODULE_PATH
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules"
)
include(AddFortranRuntime)
include(FlangCommon)


############################
Expand Down Expand Up @@ -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)
Expand All @@ -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 <cfloat>
#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 <cfloat>
#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"
Expand All @@ -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)


Expand Down
9 changes: 7 additions & 2 deletions FortranRuntime/cmake/modules/AddFortranRuntime.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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 $<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions -fno-rtti -fno-unwind-tables -fno-asynchronous-unwind-tables>)
target_compile_options(${name} PRIVATE $<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions -fno-rtti -fno-unwind-tables -fno-asynchronous-unwind-tables>)
elseif (MSVC)
target_compile_options (${name} PRIVATE $<$<COMPILE_LANGUAGE:CXX>:/EHs-c- /GR->)
target_compile_options(${name} PRIVATE $<$<COMPILE_LANGUAGE:CXX>:/EHs-c- /GR->)
endif ()

# FortranRuntime's public headers
Expand All @@ -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)
Expand Down
31 changes: 31 additions & 0 deletions FortranRuntime/cmake/modules/FlangCommon.cmake
Original file line number Diff line number Diff line change
@@ -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 <cfloat>
#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 ()
17 changes: 0 additions & 17 deletions FortranRuntime/lib/Runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.<mode>.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()
10 changes: 2 additions & 8 deletions flang/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -432,6 +425,7 @@ endif()

include(CMakeParseArguments)
include(AddFlang)
include(FlangCommon)

if (FLANG_INCLUDE_TESTS)
add_compile_definitions(FLANG_INCLUDE_TESTS=1)
Expand Down

0 comments on commit 871c690

Please sign in to comment.