From 1c07758ca2ee8dfbd43ca582d16f36a97177b24c Mon Sep 17 00:00:00 2001 From: Brad King Date: Mon, 4 Dec 2023 17:15:19 -0500 Subject: CMakeParseImplicitLinkInfo: Exclude LLVMFlang program entry point for MSVC As of llvm-project `main` branch commit `86accd4e03` (2023-12-04), LLVMFlang 18.0.0, when used to drive linking an executable, emits a MSVC linker flag to use all object files from the `Fortran_main` library. These object files are meant for use when linking the program entry point, and so are not implicit link dependencies of Fortran libraries. --- Modules/CMakeParseImplicitLinkInfo.cmake | 2 ++ ...indows_x86_64-Fortran-LLVMFlang-18.0.0-MSVC.input | 20 ++++++++++++++++++++ .../ParseImplicitLinkInfo.cmake | 1 + ...ndows_x86_64-Fortran-LLVMFlang-18.0.0-MSVC.output | 2 ++ 4 files changed, 25 insertions(+) create mode 100644 Tests/RunCMake/ParseImplicitData/windows_x86_64-Fortran-LLVMFlang-18.0.0-MSVC.input create mode 100644 Tests/RunCMake/ParseImplicitLinkInfo/results/windows_x86_64-Fortran-LLVMFlang-18.0.0-MSVC.output diff --git a/Modules/CMakeParseImplicitLinkInfo.cmake b/Modules/CMakeParseImplicitLinkInfo.cmake index b48eb43..0c92634 100644 --- a/Modules/CMakeParseImplicitLinkInfo.cmake +++ b/Modules/CMakeParseImplicitLinkInfo.cmake @@ -163,6 +163,8 @@ function(cmake_parse_implicit_link_info2 text log_var obj_regex) string(APPEND log " arg [${arg}] ==> ignore MSVC cl option\n") elseif(is_msvc AND "${arg}" MATCHES "^[-/][Ii][Mm][Pp][Ll][Ii][Bb]:") string(APPEND log " arg [${arg}] ==> ignore MSVC link option\n") + elseif(is_msvc AND "${arg}" MATCHES "^[-/][Ww][Hh][Oo][Ll][Ee][Aa][Rr][Cc][Hh][Ii][Vv][Ee]:Fortran_main") + string(APPEND log " arg [${arg}] ==> ignore LLVMFlang program entry point\n") elseif(is_msvc AND "${arg}" MATCHES "^(.*\\.[Ll][Ii][Bb])$") if(EXTRA_PARSE_COMPUTE_IMPLICIT_LIBS) set(lib "${CMAKE_MATCH_1}") diff --git a/Tests/RunCMake/ParseImplicitData/windows_x86_64-Fortran-LLVMFlang-18.0.0-MSVC.input b/Tests/RunCMake/ParseImplicitData/windows_x86_64-Fortran-LLVMFlang-18.0.0-MSVC.input new file mode 100644 index 0000000..c567f06 --- /dev/null +++ b/Tests/RunCMake/ParseImplicitData/windows_x86_64-Fortran-LLVMFlang-18.0.0-MSVC.input @@ -0,0 +1,20 @@ +CMAKE_LANG=Fortran +CMAKE_LINKER= +CMAKE_Fortran_COMPILER_ABI= +CMAKE_Fortran_COMPILER_AR= +CMAKE_Fortran_COMPILER_ARCHITECTURE_ID=x64 +CMAKE_Fortran_COMPILER_EXTERNAL_TOOLCHAIN= +CMAKE_Fortran_COMPILER_ID=LLVMFlang +CMAKE_Fortran_COMPILER_LAUNCHER= +CMAKE_Fortran_COMPILER_LOADED=1 +CMAKE_Fortran_COMPILER_RANLIB= +CMAKE_Fortran_COMPILER_TARGET= +CMAKE_Fortran_COMPILER_VERSION=18.0.0 +CMAKE_Fortran_COMPILER_VERSION_INTERAL= +CMAKE_Fortran_SIMULATE_ID=MSVC +flang-new version 18.0.0 +Target: x86_64-pc-windows-msvc +Thread model: posix +InstalledDir: C:\DoesNotExist\LLVM\bin + "C:\\DoesNotExist\\LLVM\\bin\\flang-new" -fc1 -triple x86_64-pc-windows-msvc19.36.32543 -emit-obj -mrelocation-model pic -pic-level 2 -target-cpu x86-64 --dependent-lib=clang_rt.builtins-x86_64.lib -D_MT --dependent-lib=libcmt --dependent-lib=Fortran_main.static.lib --dependent-lib=FortranRuntime.static.lib --dependent-lib=FortranDecimal.static.lib -D_MSC_VER=1936 -D_MSC_FULL_VER=193632543 -D_WIN32 -D_M_X64=100 -mframe-pointer=none -o "C:\\DoesNotExist\\Temp\\CMakeFortranCompilerABI-e91f95.o" -x f95-cpp-input CMakeFortranCompilerABI.F + "C:\\Program Files\\Microsoft Visual Studio\\2022\\Professional\\VC\\Tools\\MSVC\\14.36.32532\\bin\\Hostx64\\x64\\link.exe" -out:a.exe "-libpath:C:\\DoesNotExist\\LLVM\\lib" /WHOLEARCHIVE:Fortran_main.static.lib /subsystem:console "-libpath:C:\\DoesNotExist\\LLVM\\lib\\clang\\18\\lib\\windows" -nologo "C:\\DoesNotExist\\Temp\\CMakeFortranCompilerABI-e91f95.o" diff --git a/Tests/RunCMake/ParseImplicitLinkInfo/ParseImplicitLinkInfo.cmake b/Tests/RunCMake/ParseImplicitLinkInfo/ParseImplicitLinkInfo.cmake index 04998a2..0ede9ee 100644 --- a/Tests/RunCMake/ParseImplicitLinkInfo/ParseImplicitLinkInfo.cmake +++ b/Tests/RunCMake/ParseImplicitLinkInfo/ParseImplicitLinkInfo.cmake @@ -43,6 +43,7 @@ set(targets openbsd-C-Clang-5.0.1 openbsd-CXX-Clang-5.0.1 sunos-C-SunPro-5.13.0 sunos-CXX-SunPro-5.13.0 sunos-Fortran-SunPro-8.8.0 windows_x86_64-C-Clang-17.0.1-MSVC windows_x86_64-CXX-Clang-17.0.1-MSVC windows_x86_64-Fortran-LLVMFlang-17.0.1-MSVC + windows_x86_64-Fortran-LLVMFlang-18.0.0-MSVC windows_arm64-C-Clang-17.0.1-MSVC windows_arm64-CXX-Clang-17.0.1-MSVC windows_arm64-Fortran-LLVMFlang-17.0.1-MSVC ) diff --git a/Tests/RunCMake/ParseImplicitLinkInfo/results/windows_x86_64-Fortran-LLVMFlang-18.0.0-MSVC.output b/Tests/RunCMake/ParseImplicitLinkInfo/results/windows_x86_64-Fortran-LLVMFlang-18.0.0-MSVC.output new file mode 100644 index 0000000..c8266a5 --- /dev/null +++ b/Tests/RunCMake/ParseImplicitLinkInfo/results/windows_x86_64-Fortran-LLVMFlang-18.0.0-MSVC.output @@ -0,0 +1,2 @@ +libs= +dirs=C:/DoesNotExist/LLVM/lib;C:/DoesNotExist/LLVM/lib/clang/18/lib/windows -- cgit v0.12 From 48302b469e10f37b8961b6556080df66ba11289f Mon Sep 17 00:00:00 2001 From: Brad King Date: Mon, 20 Nov 2023 14:15:05 -0500 Subject: LLVMFlang: Update MSVC runtime library selection for LLVMFlang 18.0 LLVMFlang 18.0 adds MSVC runtime library selection flags and associated Fortran runtime library variants. Resolve the corresponding FIXME left by commit 26bf32cdc6 (LLVMFlang: Add support for targeting MSVC ABI on Windows, 2023-09-28, v3.28.0-rc1~10^2). Issue: #24840 --- Modules/CMakeDetermineFortranCompiler.cmake | 4 ++-- Modules/Platform/Windows-LLVMFlang-Fortran.cmake | 27 ++++++++++++++---------- Tests/MSVCRuntimeLibrary/Fortran/CMakeLists.txt | 8 ++++--- 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/Modules/CMakeDetermineFortranCompiler.cmake b/Modules/CMakeDetermineFortranCompiler.cmake index 3ed7d58..613b0c4 100644 --- a/Modules/CMakeDetermineFortranCompiler.cmake +++ b/Modules/CMakeDetermineFortranCompiler.cmake @@ -265,8 +265,8 @@ if("${CMAKE_Fortran_COMPILER_ID};${CMAKE_Fortran_SIMULATE_ID}" STREQUAL "LLVMFla "${log}\n" ) set(_CMAKE_Fortran_IMPLICIT_LINK_INFORMATION_DETERMINED_EARLY 1) - if("x${CMAKE_Fortran_COMPILER_ARCHITECTURE_ID}" STREQUAL "xARM64") - # FIXME(LLVMFlang): It does not add `-defaultlib:` fields to object + if("x${CMAKE_Fortran_COMPILER_ARCHITECTURE_ID}" STREQUAL "xARM64" AND CMAKE_Fortran_COMPILER_VERSION VERSION_LESS 18.0) + # LLVMFlang < 18.0 does not add `-defaultlib:` fields to object # files to specify link dependencies on its runtime libraries. # For now, we add them ourselves. list(APPEND CMAKE_Fortran_IMPLICIT_LINK_LIBRARIES "clang_rt.builtins-aarch64.lib") diff --git a/Modules/Platform/Windows-LLVMFlang-Fortran.cmake b/Modules/Platform/Windows-LLVMFlang-Fortran.cmake index 57e36c6..10e3b2c 100644 --- a/Modules/Platform/Windows-LLVMFlang-Fortran.cmake +++ b/Modules/Platform/Windows-LLVMFlang-Fortran.cmake @@ -5,17 +5,22 @@ elseif("x${CMAKE_Fortran_SIMULATE_ID}" STREQUAL "xMSVC") include(Platform/Windows-MSVC) __windows_compiler_msvc(Fortran) - # FIXME(LLVMFlang): It does not provides MSVC runtime library selection flags. - # It should be given a flag like classic Flang's `-Xclang --dependent-lib=`, or a - # dedicated flag to select among multiple `Fortran*.lib` runtime library variants - # that each depend on a different MSVC runtime library. For now, LLVMFlang's - # `Fortran*.lib` runtime libraries hard-code use of msvcrt (MultiThreadedDLL), - # so we link to it ourselves. - set(_LLVMFlang_LINK_RUNTIME "-defaultlib:msvcrt") - set(CMAKE_Fortran_COMPILE_OPTIONS_MSVC_RUNTIME_LIBRARY_MultiThreaded "") - set(CMAKE_Fortran_COMPILE_OPTIONS_MSVC_RUNTIME_LIBRARY_MultiThreadedDLL "") - set(CMAKE_Fortran_COMPILE_OPTIONS_MSVC_RUNTIME_LIBRARY_MultiThreadedDebug "") - set(CMAKE_Fortran_COMPILE_OPTIONS_MSVC_RUNTIME_LIBRARY_MultiThreadedDebugDLL "") + if(CMAKE_Fortran_COMPILER_VERSION VERSION_GREATER_EQUAL 18.0) + set(_LLVMFlang_LINK_RUNTIME "") + set(CMAKE_Fortran_COMPILE_OPTIONS_MSVC_RUNTIME_LIBRARY_MultiThreaded "-fms-runtime-lib=static") + set(CMAKE_Fortran_COMPILE_OPTIONS_MSVC_RUNTIME_LIBRARY_MultiThreadedDLL "-fms-runtime-lib=dll") + set(CMAKE_Fortran_COMPILE_OPTIONS_MSVC_RUNTIME_LIBRARY_MultiThreadedDebug "-fms-runtime-lib=static_dbg") + set(CMAKE_Fortran_COMPILE_OPTIONS_MSVC_RUNTIME_LIBRARY_MultiThreadedDebugDLL "-fms-runtime-lib=dll_dbg") + else() + # LLVMFlang < 18.0 does not have MSVC runtime library selection flags. + # The official distrubtion's `Fortran*.lib` runtime libraries hard-code + # use of msvcrt (MultiThreadedDLL), so we link to it ourselves. + set(_LLVMFlang_LINK_RUNTIME "-defaultlib:msvcrt") + set(CMAKE_Fortran_COMPILE_OPTIONS_MSVC_RUNTIME_LIBRARY_MultiThreaded "") + set(CMAKE_Fortran_COMPILE_OPTIONS_MSVC_RUNTIME_LIBRARY_MultiThreadedDLL "") + set(CMAKE_Fortran_COMPILE_OPTIONS_MSVC_RUNTIME_LIBRARY_MultiThreadedDebug "") + set(CMAKE_Fortran_COMPILE_OPTIONS_MSVC_RUNTIME_LIBRARY_MultiThreadedDebugDLL "") + endif() # LLVMFlang, like Clang, does not provide all debug information format flags. # In order to provide easy integration with C and C++ projects that use the diff --git a/Tests/MSVCRuntimeLibrary/Fortran/CMakeLists.txt b/Tests/MSVCRuntimeLibrary/Fortran/CMakeLists.txt index 2a8a152..4cd200a 100644 --- a/Tests/MSVCRuntimeLibrary/Fortran/CMakeLists.txt +++ b/Tests/MSVCRuntimeLibrary/Fortran/CMakeLists.txt @@ -21,14 +21,16 @@ foreach(t MultiThreaded SingleThreaded) endforeach() endforeach() endforeach() -if(CMAKE_Fortran_COMPILER_ID STREQUAL "LLVMFlang") - # LLVMFlang does not actually define these, so inject them +if(CMAKE_Fortran_COMPILER_ID STREQUAL "LLVMFlang" AND CMAKE_Fortran_COMPILER_VERSION VERSION_LESS 18.0) + # LLVMFlang < 18.0 does not define these, so inject them. set(CMAKE_Fortran_COMPILE_OPTIONS_MSVC_RUNTIME_LIBRARY_MultiThreaded "-D_MT") set(CMAKE_Fortran_COMPILE_OPTIONS_MSVC_RUNTIME_LIBRARY_MultiThreadedDLL "-D_MT;-D_DLL") set(CMAKE_Fortran_COMPILE_OPTIONS_MSVC_RUNTIME_LIBRARY_MultiThreadedDebug "-D_MT;-D_DEBUG") set(CMAKE_Fortran_COMPILE_OPTIONS_MSVC_RUNTIME_LIBRARY_MultiThreadedDebugDLL "-D_MT;-D_DEBUG;-D_DLL") endif() -string(APPEND CMAKE_Fortran_FLAGS " -w") +if(NOT CMAKE_Fortran_SIMULATE_ID STREQUAL "MSVC") + string(APPEND CMAKE_Fortran_FLAGS " -w") +endif() function(verify_combinations threads lang src) set(verify_tc_config_ Release) -- cgit v0.12 From cd28915260432cdbcdfb1f715477c262d41867d1 Mon Sep 17 00:00:00 2001 From: Brad King Date: Mon, 27 Nov 2023 16:02:07 -0500 Subject: LLVMFlang: Update MSVC ABI and architecture detection for LLVMFlang 18.0 LLVMFlang 18.0 adds MSVC ABI and architecture macros. Resolve the corresponding FIXME left by commit 26bf32cdc6 (LLVMFlang: Add support for targeting MSVC ABI on Windows, 2023-09-28, v3.28.0-rc1~10^2). Issue: #24840 --- Modules/CMakeDetermineCompilerId.cmake | 37 ++++++++++++++++++---------------- Modules/CMakeFortranCompilerId.F.in | 2 +- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/Modules/CMakeDetermineCompilerId.cmake b/Modules/CMakeDetermineCompilerId.cmake index ffba1cb..b380aa5 100644 --- a/Modules/CMakeDetermineCompilerId.cmake +++ b/Modules/CMakeDetermineCompilerId.cmake @@ -174,27 +174,30 @@ function(CMAKE_DETERMINE_COMPILER_ID lang flagvar src) endif() endif() - # FIXME(LLVMFlang): It does not provide predefines identifying the MSVC ABI or architecture. - # It should be taught to define _MSC_VER and its _M_* architecture flags. if("x${lang}" STREQUAL "xFortran" AND "x${CMAKE_${lang}_COMPILER_ID}" STREQUAL "xLLVMFlang") - # Parse the target triple to detect information we should later be able - # to get during preprocessing above, once LLVMFlang provides it. + # Parse the target triple to detect information not always available from the preprocessor. if(COMPILER_${lang}_PRODUCED_OUTPUT MATCHES "-triple ([0-9a-z_]*)-.*windows-msvc([0-9]+)\\.([0-9]+)") - set(CMAKE_${lang}_SIMULATE_ID "MSVC") + # CMakeFortranCompilerId.F.in does not extract the _MSC_VER minor version. + # We can do better using the version parsed here. set(CMAKE_${lang}_SIMULATE_VERSION "${CMAKE_MATCH_2}.${CMAKE_MATCH_3}") - set(arch ${CMAKE_MATCH_1}) - if(arch STREQUAL "x86_64") - set(CMAKE_${lang}_COMPILER_ARCHITECTURE_ID "x64") - elseif(arch STREQUAL "aarch64") - set(CMAKE_${lang}_COMPILER_ARCHITECTURE_ID "ARM64") - elseif(arch STREQUAL "arm64ec") - set(CMAKE_${lang}_COMPILER_ARCHITECTURE_ID "ARM64EC") - elseif(arch MATCHES "^i[3-9]86$") - set(CMAKE_${lang}_COMPILER_ARCHITECTURE_ID "X86") - else() - message(FATAL_ERROR "LLVMFlang target architecture unrecognized: ${arch}") + + if (CMAKE_${lang}_COMPILER_VERSION VERSION_LESS 18.0) + # LLVMFlang < 18.0 does not provide predefines identifying the MSVC ABI or architecture. + set(CMAKE_${lang}_SIMULATE_ID "MSVC") + set(arch ${CMAKE_MATCH_1}) + if(arch STREQUAL "x86_64") + set(CMAKE_${lang}_COMPILER_ARCHITECTURE_ID "x64") + elseif(arch STREQUAL "aarch64") + set(CMAKE_${lang}_COMPILER_ARCHITECTURE_ID "ARM64") + elseif(arch STREQUAL "arm64ec") + set(CMAKE_${lang}_COMPILER_ARCHITECTURE_ID "ARM64EC") + elseif(arch MATCHES "^i[3-9]86$") + set(CMAKE_${lang}_COMPILER_ARCHITECTURE_ID "X86") + else() + message(FATAL_ERROR "LLVMFlang target architecture unrecognized: ${arch}") + endif() + set(MSVC_${lang}_ARCHITECTURE_ID "${CMAKE_${lang}_COMPILER_ARCHITECTURE_ID}") endif() - set(MSVC_${lang}_ARCHITECTURE_ID "${CMAKE_${lang}_COMPILER_ARCHITECTURE_ID}") elseif(COMPILER_${lang}_PRODUCED_OUTPUT MATCHES "-triple ([0-9a-z_]*)-.*windows-gnu") set(CMAKE_${lang}_SIMULATE_ID "GNU") endif() diff --git a/Modules/CMakeFortranCompilerId.F.in b/Modules/CMakeFortranCompilerId.F.in index f5c2ab5..a040073 100644 --- a/Modules/CMakeFortranCompilerId.F.in +++ b/Modules/CMakeFortranCompilerId.F.in @@ -240,7 +240,7 @@ #else PRINT *, 'INFO:platform[]' #endif -#if defined(_WIN32) && (defined(__INTEL_COMPILER) || defined(__ICC)) +#if defined(_MSC_VER) # if defined(_M_IA64) PRINT *, 'INFO:arch[IA64]' # elif defined(_M_X64) || defined(_M_AMD64) -- cgit v0.12