[flang] [clang] [flang][windows] Add option to link against specific MSVC CRT (PR #70833)
https://github.com/DavidTruby updated https://github.com/llvm/llvm-project/pull/70833 >From 46057145d229e5b6ee7e6891f34d47b0d2aa818d Mon Sep 17 00:00:00 2001 From: David Truby Date: Tue, 31 Oct 2023 15:07:13 + Subject: [PATCH] [flang][windows] Add option to link against specific MSVC CRT Currently flang's runtime libraries are only built for the specific CRT that LLVM itself was built against. This patch adds the cmake logic for building a separate runtime for each CRT configuration and adds a flag for selecting a CRT configuration to link against. --- clang/include/clang/Driver/Options.td | 2 +- clang/lib/Driver/ToolChains/CommonArgs.cpp | 42 +-- clang/lib/Driver/ToolChains/CommonArgs.h | 2 +- clang/lib/Driver/ToolChains/Darwin.cpp | 2 +- clang/lib/Driver/ToolChains/DragonFly.cpp | 2 +- clang/lib/Driver/ToolChains/FreeBSD.cpp| 2 +- clang/lib/Driver/ToolChains/Gnu.cpp| 2 +- clang/lib/Driver/ToolChains/Haiku.cpp | 2 +- clang/lib/Driver/ToolChains/MSVC.cpp | 2 +- clang/lib/Driver/ToolChains/MinGW.cpp | 2 +- clang/lib/Driver/ToolChains/NetBSD.cpp | 2 +- clang/lib/Driver/ToolChains/OpenBSD.cpp| 2 +- clang/lib/Driver/ToolChains/Solaris.cpp| 4 +- flang/lib/Decimal/CMakeLists.txt | 23 +++ flang/runtime/CMakeLists.txt | 40 +++--- flang/runtime/FortranMain/CMakeLists.txt | 18 + flang/test/Driver/driver-help-hidden.f90 | 2 + flang/test/Driver/driver-help.f90 | 2 + flang/test/Driver/linker-flags.f90 | 47 ++ 19 files changed, 169 insertions(+), 31 deletions(-) diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index 3409ce29ac56302..dacc4442b338a29 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -2858,7 +2858,7 @@ def fms_compatibility_version "version number to report in _MSC_VER (0 = don't define it " "(default))">; def fms_runtime_lib_EQ : Joined<["-"], "fms-runtime-lib=">, Group, - Flags<[]>, Visibility<[ClangOption, CLOption]>, + Flags<[]>, Visibility<[ClangOption, CLOption, FlangOption]>, Values<"static,static_dbg,dll,dll_dbg">, HelpText<"Select Windows run-time library">, DocBrief<[{ diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp index e19f1829c9fa82b..a81c9b6201f8158 100644 --- a/clang/lib/Driver/ToolChains/CommonArgs.cpp +++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp @@ -976,12 +976,46 @@ bool tools::addOpenMPRuntime(ArgStringList , const ToolChain , return true; } -void tools::addFortranRuntimeLibs(const ToolChain , +void tools::addFortranRuntimeLibs(const ToolChain , const ArgList , llvm::opt::ArgStringList ) { if (TC.getTriple().isKnownWindowsMSVCEnvironment()) { -CmdArgs.push_back("Fortran_main.lib"); -CmdArgs.push_back("FortranRuntime.lib"); -CmdArgs.push_back("FortranDecimal.lib"); +CmdArgs.push_back(Args.MakeArgString( +"/DEFAULTLIB:" + TC.getCompilerRTBasename(Args, "builtins"))); +unsigned RTOptionID = options::OPT__SLASH_MT; +if (auto *rtl = Args.getLastArg(options::OPT_fms_runtime_lib_EQ)) { + RTOptionID = llvm::StringSwitch(rtl->getValue()) + .Case("static", options::OPT__SLASH_MT) + .Case("static_dbg", options::OPT__SLASH_MTd) + .Case("dll", options::OPT__SLASH_MD) + .Case("dll_dbg", options::OPT__SLASH_MDd) + .Default(options::OPT__SLASH_MT); +} +switch (RTOptionID) { +case options::OPT__SLASH_MT: + CmdArgs.push_back("/DEFAULTLIB:libcmt"); + CmdArgs.push_back("Fortran_main.static.lib"); + CmdArgs.push_back("FortranRuntime.static.lib"); + CmdArgs.push_back("FortranDecimal.static.lib"); + break; +case options::OPT__SLASH_MTd: + CmdArgs.push_back("/DEFAULTLIB:libcmtd"); + CmdArgs.push_back("Fortran_main.static_dbg.lib"); + CmdArgs.push_back("FortranRuntime.static_dbg.lib"); + CmdArgs.push_back("FortranDecimal.static_dbg.lib"); + break; +case options::OPT__SLASH_MD: + CmdArgs.push_back("/DEFAULTLIB:msvcrt"); + CmdArgs.push_back("Fortran_main.dynamic.lib"); + CmdArgs.push_back("FortranRuntime.dynamic.lib"); + CmdArgs.push_back("FortranDecimal.dynamic.lib"); + break; +case options::OPT__SLASH_MDd: + CmdArgs.push_back("/DEFAULTLIB:msvcrtd"); + CmdArgs.push_back("Fortran_main.dynamic_dbg.lib"); + CmdArgs.push_back("FortranRuntime.dynamic_dbg.lib"); + CmdArgs.push_back("FortranDecimal.dynamic_dbg.lib"); + break; +} } else { CmdArgs.push_back("-lFortran_main"); CmdArgs.push_back("-lFortranRuntime"); diff --git a/clang/lib/Driver/ToolChains/CommonArgs.h
[flang] [clang] [flang][windows] Add option to link against specific MSVC CRT (PR #70833)
DavidTruby wrote: I've also started the work to get linker option directives (like "/DEFAULTLIB") added to MLIR's LLVMIR dialect so that we can do this the correct way: see #71720. I'd still like to merge this patch first though for the incremental improvement it does provide. Then once the MLIR support is merged I can switch over to using that and this should be resolved in the way that would help CMake's support for flang. https://github.com/llvm/llvm-project/pull/70833 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[flang] [clang] [flang][windows] Add option to link against specific MSVC CRT (PR #70833)
bradking wrote: @DavidTruby please see my above retraction of the suggestion to rename `.dynamic.lib` to `.dll.lib`. https://github.com/llvm/llvm-project/pull/70833 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[flang] [clang] [flang][windows] Add option to link against specific MSVC CRT (PR #70833)
@@ -281,3 +281,26 @@ add_flang_library(FortranRuntime INSTALL_WITH_TOOLCHAIN ) + +if (DEFINED MSVC) + set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded) + add_flang_library(FortranRuntime.static ${sources} bradking wrote: When targeting the MSVC ABI, the plain `FortranRuntime` library added above should be excluded. Only the per-CRT variants should exist, because choosing a CRT variant is not optional. https://github.com/llvm/llvm-project/pull/70833 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[flang] [clang] [flang][windows] Add option to link against specific MSVC CRT (PR #70833)
https://github.com/DavidTruby updated https://github.com/llvm/llvm-project/pull/70833 >From 9e84729cada6c032c64934ee519e605407aab049 Mon Sep 17 00:00:00 2001 From: David Truby Date: Tue, 31 Oct 2023 15:07:13 + Subject: [PATCH 1/6] [flang][windows] Add option to link against specific MSVC CRT Currently flang's runtime libraries are only built for the specific CRT that LLVM itself was built against. This patch adds the cmake logic for building a separate runtime for each CRT configuration and adds a flag for selecting a CRT configuration to link against. --- clang/include/clang/Driver/Options.td | 2 +- clang/lib/Driver/ToolChains/CommonArgs.cpp | 42 +-- clang/lib/Driver/ToolChains/CommonArgs.h | 2 +- clang/lib/Driver/ToolChains/Darwin.cpp | 2 +- clang/lib/Driver/ToolChains/DragonFly.cpp | 2 +- clang/lib/Driver/ToolChains/FreeBSD.cpp| 2 +- clang/lib/Driver/ToolChains/Gnu.cpp| 2 +- clang/lib/Driver/ToolChains/Haiku.cpp | 2 +- clang/lib/Driver/ToolChains/MSVC.cpp | 2 +- clang/lib/Driver/ToolChains/MinGW.cpp | 2 +- clang/lib/Driver/ToolChains/NetBSD.cpp | 2 +- clang/lib/Driver/ToolChains/OpenBSD.cpp| 2 +- clang/lib/Driver/ToolChains/Solaris.cpp| 2 +- flang/lib/Decimal/CMakeLists.txt | 23 +++ flang/runtime/CMakeLists.txt | 23 +++ flang/runtime/FortranMain/CMakeLists.txt | 18 + flang/test/Driver/driver-help-hidden.f90 | 2 + flang/test/Driver/driver-help.f90 | 2 + flang/test/Driver/linker-flags.f90 | 47 ++ 19 files changed, 157 insertions(+), 24 deletions(-) diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index c8b730e0f7ecd84..66d4794714c9529 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -2842,7 +2842,7 @@ def fms_compatibility_version "version number to report in _MSC_VER (0 = don't define it " "(default))">; def fms_runtime_lib_EQ : Joined<["-"], "fms-runtime-lib=">, Group, - Flags<[]>, Visibility<[ClangOption, CLOption]>, + Flags<[]>, Visibility<[ClangOption, CLOption, FlangOption]>, Values<"static,static_dbg,dll,dll_dbg">, HelpText<"Select Windows run-time library">, DocBrief<[{ diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp index ad012d3d0d4b46f..1cac9a179eb960f 100644 --- a/clang/lib/Driver/ToolChains/CommonArgs.cpp +++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp @@ -976,12 +976,46 @@ bool tools::addOpenMPRuntime(ArgStringList , const ToolChain , return true; } -void tools::addFortranRuntimeLibs(const ToolChain , +void tools::addFortranRuntimeLibs(const ToolChain , const ArgList , llvm::opt::ArgStringList ) { if (TC.getTriple().isKnownWindowsMSVCEnvironment()) { -CmdArgs.push_back("Fortran_main.lib"); -CmdArgs.push_back("FortranRuntime.lib"); -CmdArgs.push_back("FortranDecimal.lib"); +CmdArgs.push_back(Args.MakeArgString( +"/DEFAULTLIB:" + TC.getCompilerRTBasename(Args, "builtins"))); +unsigned RTOptionID = options::OPT__SLASH_MT; +if (auto *rtl = Args.getLastArg(options::OPT_fms_runtime_lib_EQ)) { + RTOptionID = llvm::StringSwitch(rtl->getValue()) + .Case("static", options::OPT__SLASH_MT) + .Case("static_dbg", options::OPT__SLASH_MTd) + .Case("dll", options::OPT__SLASH_MD) + .Case("dll_dbg", options::OPT__SLASH_MDd) + .Default(options::OPT__SLASH_MT); +} +switch(RTOptionID) { +case options::OPT__SLASH_MT: + CmdArgs.push_back("/DEFAULTLIB:libcmt"); + CmdArgs.push_back("Fortran_main.static.lib"); + CmdArgs.push_back("FortranRuntime.static.lib"); + CmdArgs.push_back("FortranDecimal.static.lib"); + break; +case options::OPT__SLASH_MTd: + CmdArgs.push_back("/DEFAULTLIB:libcmtd"); + CmdArgs.push_back("Fortran_main.static_debug.lib"); + CmdArgs.push_back("FortranRuntime.static_debug.lib"); + CmdArgs.push_back("FortranDecimal.static_debug.lib"); + break; +case options::OPT__SLASH_MD: + CmdArgs.push_back("/DEFAULTLIB:msvcrt"); + CmdArgs.push_back("Fortran_main.dynamic.lib"); + CmdArgs.push_back("FortranRuntime.dynamic.lib"); + CmdArgs.push_back("FortranDecimal.dynamic.lib"); + break; +case options::OPT__SLASH_MDd: + CmdArgs.push_back("/DEFAULTLIB:msvcrtd"); + CmdArgs.push_back("Fortran_main.dynamic_debug.lib"); + CmdArgs.push_back("FortranRuntime.dynamic_debug.lib"); + CmdArgs.push_back("FortranDecimal.dynamic_debug.lib"); + break; +} } else { CmdArgs.push_back("-lFortran_main"); CmdArgs.push_back("-lFortranRuntime"); diff --git a/clang/lib/Driver/ToolChains/CommonArgs.h
[flang] [clang] [flang][windows] Add option to link against specific MSVC CRT (PR #70833)
@@ -976,12 +976,46 @@ bool tools::addOpenMPRuntime(ArgStringList , const ToolChain , return true; } -void tools::addFortranRuntimeLibs(const ToolChain , +void tools::addFortranRuntimeLibs(const ToolChain , const ArgList , llvm::opt::ArgStringList ) { if (TC.getTriple().isKnownWindowsMSVCEnvironment()) { -CmdArgs.push_back("Fortran_main.lib"); -CmdArgs.push_back("FortranRuntime.lib"); -CmdArgs.push_back("FortranDecimal.lib"); +CmdArgs.push_back(Args.MakeArgString( +"/DEFAULTLIB:" + TC.getCompilerRTBasename(Args, "builtins"))); +unsigned RTOptionID = options::OPT__SLASH_MT; bradking wrote: The errors were due to https://github.com/llvm/llvm-project/pull/70833#pullrequestreview-1710341215 because the runtime library variants not being built with the correct CRT themselves. After switching back to the `CMAKE_MSVC_RUNTIME_LIBRARY` the problem is resolved. https://github.com/llvm/llvm-project/pull/70833 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[flang] [clang] [flang][windows] Add option to link against specific MSVC CRT (PR #70833)
https://github.com/bradking edited https://github.com/llvm/llvm-project/pull/70833 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[flang] [clang] [flang][windows] Add option to link against specific MSVC CRT (PR #70833)
@@ -976,12 +976,46 @@ bool tools::addOpenMPRuntime(ArgStringList , const ToolChain , return true; } -void tools::addFortranRuntimeLibs(const ToolChain , +void tools::addFortranRuntimeLibs(const ToolChain , const ArgList , llvm::opt::ArgStringList ) { if (TC.getTriple().isKnownWindowsMSVCEnvironment()) { -CmdArgs.push_back("Fortran_main.lib"); -CmdArgs.push_back("FortranRuntime.lib"); -CmdArgs.push_back("FortranDecimal.lib"); +CmdArgs.push_back(Args.MakeArgString( +"/DEFAULTLIB:" + TC.getCompilerRTBasename(Args, "builtins"))); +unsigned RTOptionID = options::OPT__SLASH_MT; +if (auto *rtl = Args.getLastArg(options::OPT_fms_runtime_lib_EQ)) { + RTOptionID = llvm::StringSwitch(rtl->getValue()) + .Case("static", options::OPT__SLASH_MT) + .Case("static_dbg", options::OPT__SLASH_MTd) + .Case("dll", options::OPT__SLASH_MD) + .Case("dll_dbg", options::OPT__SLASH_MDd) + .Default(options::OPT__SLASH_MT); bradking wrote: Hmm. Now that I see those names on disk after building from your update, file names like `FortranRuntime.dll.lib` might be confusing since they do not actually have a corresponding `FortranRuntime.dll`. Maybe `.dynamic` was better. https://github.com/llvm/llvm-project/pull/70833 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[flang] [clang] [flang][windows] Add option to link against specific MSVC CRT (PR #70833)
https://github.com/DavidTruby updated https://github.com/llvm/llvm-project/pull/70833 >From 9e84729cada6c032c64934ee519e605407aab049 Mon Sep 17 00:00:00 2001 From: David Truby Date: Tue, 31 Oct 2023 15:07:13 + Subject: [PATCH 1/5] [flang][windows] Add option to link against specific MSVC CRT Currently flang's runtime libraries are only built for the specific CRT that LLVM itself was built against. This patch adds the cmake logic for building a separate runtime for each CRT configuration and adds a flag for selecting a CRT configuration to link against. --- clang/include/clang/Driver/Options.td | 2 +- clang/lib/Driver/ToolChains/CommonArgs.cpp | 42 +-- clang/lib/Driver/ToolChains/CommonArgs.h | 2 +- clang/lib/Driver/ToolChains/Darwin.cpp | 2 +- clang/lib/Driver/ToolChains/DragonFly.cpp | 2 +- clang/lib/Driver/ToolChains/FreeBSD.cpp| 2 +- clang/lib/Driver/ToolChains/Gnu.cpp| 2 +- clang/lib/Driver/ToolChains/Haiku.cpp | 2 +- clang/lib/Driver/ToolChains/MSVC.cpp | 2 +- clang/lib/Driver/ToolChains/MinGW.cpp | 2 +- clang/lib/Driver/ToolChains/NetBSD.cpp | 2 +- clang/lib/Driver/ToolChains/OpenBSD.cpp| 2 +- clang/lib/Driver/ToolChains/Solaris.cpp| 2 +- flang/lib/Decimal/CMakeLists.txt | 23 +++ flang/runtime/CMakeLists.txt | 23 +++ flang/runtime/FortranMain/CMakeLists.txt | 18 + flang/test/Driver/driver-help-hidden.f90 | 2 + flang/test/Driver/driver-help.f90 | 2 + flang/test/Driver/linker-flags.f90 | 47 ++ 19 files changed, 157 insertions(+), 24 deletions(-) diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index c8b730e0f7ecd84..66d4794714c9529 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -2842,7 +2842,7 @@ def fms_compatibility_version "version number to report in _MSC_VER (0 = don't define it " "(default))">; def fms_runtime_lib_EQ : Joined<["-"], "fms-runtime-lib=">, Group, - Flags<[]>, Visibility<[ClangOption, CLOption]>, + Flags<[]>, Visibility<[ClangOption, CLOption, FlangOption]>, Values<"static,static_dbg,dll,dll_dbg">, HelpText<"Select Windows run-time library">, DocBrief<[{ diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp index ad012d3d0d4b46f..1cac9a179eb960f 100644 --- a/clang/lib/Driver/ToolChains/CommonArgs.cpp +++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp @@ -976,12 +976,46 @@ bool tools::addOpenMPRuntime(ArgStringList , const ToolChain , return true; } -void tools::addFortranRuntimeLibs(const ToolChain , +void tools::addFortranRuntimeLibs(const ToolChain , const ArgList , llvm::opt::ArgStringList ) { if (TC.getTriple().isKnownWindowsMSVCEnvironment()) { -CmdArgs.push_back("Fortran_main.lib"); -CmdArgs.push_back("FortranRuntime.lib"); -CmdArgs.push_back("FortranDecimal.lib"); +CmdArgs.push_back(Args.MakeArgString( +"/DEFAULTLIB:" + TC.getCompilerRTBasename(Args, "builtins"))); +unsigned RTOptionID = options::OPT__SLASH_MT; +if (auto *rtl = Args.getLastArg(options::OPT_fms_runtime_lib_EQ)) { + RTOptionID = llvm::StringSwitch(rtl->getValue()) + .Case("static", options::OPT__SLASH_MT) + .Case("static_dbg", options::OPT__SLASH_MTd) + .Case("dll", options::OPT__SLASH_MD) + .Case("dll_dbg", options::OPT__SLASH_MDd) + .Default(options::OPT__SLASH_MT); +} +switch(RTOptionID) { +case options::OPT__SLASH_MT: + CmdArgs.push_back("/DEFAULTLIB:libcmt"); + CmdArgs.push_back("Fortran_main.static.lib"); + CmdArgs.push_back("FortranRuntime.static.lib"); + CmdArgs.push_back("FortranDecimal.static.lib"); + break; +case options::OPT__SLASH_MTd: + CmdArgs.push_back("/DEFAULTLIB:libcmtd"); + CmdArgs.push_back("Fortran_main.static_debug.lib"); + CmdArgs.push_back("FortranRuntime.static_debug.lib"); + CmdArgs.push_back("FortranDecimal.static_debug.lib"); + break; +case options::OPT__SLASH_MD: + CmdArgs.push_back("/DEFAULTLIB:msvcrt"); + CmdArgs.push_back("Fortran_main.dynamic.lib"); + CmdArgs.push_back("FortranRuntime.dynamic.lib"); + CmdArgs.push_back("FortranDecimal.dynamic.lib"); + break; +case options::OPT__SLASH_MDd: + CmdArgs.push_back("/DEFAULTLIB:msvcrtd"); + CmdArgs.push_back("Fortran_main.dynamic_debug.lib"); + CmdArgs.push_back("FortranRuntime.dynamic_debug.lib"); + CmdArgs.push_back("FortranDecimal.dynamic_debug.lib"); + break; +} } else { CmdArgs.push_back("-lFortran_main"); CmdArgs.push_back("-lFortranRuntime"); diff --git a/clang/lib/Driver/ToolChains/CommonArgs.h
[flang] [clang] [flang][windows] Add option to link against specific MSVC CRT (PR #70833)
@@ -1,3 +1,21 @@ add_flang_library(Fortran_main STATIC INSTALL_WITH_TOOLCHAIN Fortran_main.c ) +if (DEFINED MSVC) +add_flang_library(Fortran_main.static STATIC INSTALL_WITH_TOOLCHAIN +Fortran_main.c +) bradking wrote: The style elsewhere seems to use 2 spaces for indentation. https://github.com/llvm/llvm-project/pull/70833 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[flang] [clang] [flang][windows] Add option to link against specific MSVC CRT (PR #70833)
@@ -976,12 +976,46 @@ bool tools::addOpenMPRuntime(ArgStringList , const ToolChain , return true; } -void tools::addFortranRuntimeLibs(const ToolChain , +void tools::addFortranRuntimeLibs(const ToolChain , const ArgList , llvm::opt::ArgStringList ) { if (TC.getTriple().isKnownWindowsMSVCEnvironment()) { -CmdArgs.push_back("Fortran_main.lib"); -CmdArgs.push_back("FortranRuntime.lib"); -CmdArgs.push_back("FortranDecimal.lib"); +CmdArgs.push_back(Args.MakeArgString( +"/DEFAULTLIB:" + TC.getCompilerRTBasename(Args, "builtins"))); +unsigned RTOptionID = options::OPT__SLASH_MT; +if (auto *rtl = Args.getLastArg(options::OPT_fms_runtime_lib_EQ)) { + RTOptionID = llvm::StringSwitch(rtl->getValue()) + .Case("static", options::OPT__SLASH_MT) + .Case("static_dbg", options::OPT__SLASH_MTd) + .Case("dll", options::OPT__SLASH_MD) + .Case("dll_dbg", options::OPT__SLASH_MDd) + .Default(options::OPT__SLASH_MT); bradking wrote: The switch accepts names `static,static_dbg,dll,dbg_dll`. Should we use matching names for the `FortranRuntime.*.lib` variants? https://github.com/llvm/llvm-project/pull/70833 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[flang] [clang] [flang][windows] Add option to link against specific MSVC CRT (PR #70833)
@@ -281,3 +281,26 @@ add_flang_library(FortranRuntime INSTALL_WITH_TOOLCHAIN ) + +if (DEFINED MSVC) + add_flang_library(FortranRuntime.static ${sources} +LINK_LIBS +FortranDecimal.static +INSTALL_WITH_TOOLCHAIN) + set_property(TARGET FortranRuntime.static PROPERTY MSVC_RUNTIME_LIBRARY MultiThreaded) bradking wrote: After local testing, it seems my earlier advice to set the `MSVC_RUNTIME_LIBRARY` property directly instead of using `CMAKE_MSVC_RUNTIME_LIBRARY` was incorrect. LLVM's CMake infrastructure has options for using object libraries, in which case the compilation might not actually happen in the targets we name here. Please switch back to the `set(CMAKE_MSVC_RUNTIME_LIBRARY ...)` pattern. https://github.com/llvm/llvm-project/pull/70833 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[flang] [clang] [flang][windows] Add option to link against specific MSVC CRT (PR #70833)
https://github.com/rnk approved this pull request. https://github.com/llvm/llvm-project/pull/70833 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[flang] [clang] [flang][windows] Add option to link against specific MSVC CRT (PR #70833)
rnk wrote: > Do we really need to have all 4 variants of the 3 fortran runtime libraries? > That's a lot of complexity. Can we pare it down to just static/dynamic? It's > also sometimes possible to generate code that works in both the static and > dynamic context, depending on what is in those libraries. We don't create 4 > variants of clang_rt.builtins, for examle. >From glancing at the fortran runtime code, I think the answer is probably >"no". There is too much C++ standard library usage. If you wish to avoid this >build complexity, you may consider writing code in the STL-less style that is >used for C++ code in the sanitizers in compiler-rt. https://github.com/llvm/llvm-project/pull/70833 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[flang] [clang] [flang][windows] Add option to link against specific MSVC CRT (PR #70833)
rnk wrote: Do we really need to have all 4 variants of the 3 fortran runtime libraries? That's a lot of complexity. Can we pare it down to just static/dynamic? It's also sometimes possible to generate code that works in both the static and dynamic context, depending on what is in those libraries. We don't create 4 variants of clang_rt.builtins, for examle. https://github.com/llvm/llvm-project/pull/70833 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[flang] [clang] [flang][windows] Add option to link against specific MSVC CRT (PR #70833)
@@ -976,12 +976,46 @@ bool tools::addOpenMPRuntime(ArgStringList , const ToolChain , return true; } -void tools::addFortranRuntimeLibs(const ToolChain , +void tools::addFortranRuntimeLibs(const ToolChain , const ArgList , llvm::opt::ArgStringList ) { if (TC.getTriple().isKnownWindowsMSVCEnvironment()) { -CmdArgs.push_back("Fortran_main.lib"); -CmdArgs.push_back("FortranRuntime.lib"); -CmdArgs.push_back("FortranDecimal.lib"); +CmdArgs.push_back(Args.MakeArgString( +"/DEFAULTLIB:" + TC.getCompilerRTBasename(Args, "builtins"))); +unsigned RTOptionID = options::OPT__SLASH_MT; bradking wrote: In local testing, `flang-new -fms-runtime-lib=static foo.f90 -v`, where `foo.f90` is an empty `program` statement, fails with a bunch of unresolved CRT symbols. https://github.com/llvm/llvm-project/pull/70833 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[flang] [clang] [flang][windows] Add option to link against specific MSVC CRT (PR #70833)
@@ -976,12 +976,46 @@ bool tools::addOpenMPRuntime(ArgStringList , const ToolChain , return true; } -void tools::addFortranRuntimeLibs(const ToolChain , +void tools::addFortranRuntimeLibs(const ToolChain , const ArgList , llvm::opt::ArgStringList ) { if (TC.getTriple().isKnownWindowsMSVCEnvironment()) { -CmdArgs.push_back("Fortran_main.lib"); -CmdArgs.push_back("FortranRuntime.lib"); -CmdArgs.push_back("FortranDecimal.lib"); +CmdArgs.push_back(Args.MakeArgString( +"/DEFAULTLIB:" + TC.getCompilerRTBasename(Args, "builtins"))); +unsigned RTOptionID = options::OPT__SLASH_MT; +if (auto *rtl = Args.getLastArg(options::OPT_fms_runtime_lib_EQ)) { + RTOptionID = llvm::StringSwitch(rtl->getValue()) + .Case("static", options::OPT__SLASH_MT) + .Case("static_dbg", options::OPT__SLASH_MTd) + .Case("dll", options::OPT__SLASH_MD) + .Case("dll_dbg", options::OPT__SLASH_MDd) + .Default(options::OPT__SLASH_MT); +} +switch (RTOptionID) { +case options::OPT__SLASH_MT: + CmdArgs.push_back("/DEFAULTLIB:libcmt"); + CmdArgs.push_back("Fortran_main.static.lib"); + CmdArgs.push_back("FortranRuntime.static.lib"); + CmdArgs.push_back("FortranDecimal.static.lib"); + break; +case options::OPT__SLASH_MTd: + CmdArgs.push_back("/DEFAULTLIB:libcmtd"); + CmdArgs.push_back("Fortran_main.static_debug.lib"); + CmdArgs.push_back("FortranRuntime.static_debug.lib"); + CmdArgs.push_back("FortranDecimal.static_debug.lib"); + break; +case options::OPT__SLASH_MD: + CmdArgs.push_back("/DEFAULTLIB:msvcrt"); + CmdArgs.push_back("Fortran_main.dynamic.lib"); + CmdArgs.push_back("FortranRuntime.dynamic.lib"); + CmdArgs.push_back("FortranDecimal.dynamic.lib"); + break; DavidTruby wrote: Is there a way to add defaultlib directives using a CLI tool, or do they _have_ to be added when the compiler creates the object file? The main issue is we have an MLIR step in between flang and LLVM IR and I don't believe MLIR supports the attributes for setting these defaultlib directives yet, so implimenting that might be quite a lot of work... It's not so much that we would have difficulty adding the specific defaultlib directive we need, as that we'd have an issue adding any of them. https://github.com/llvm/llvm-project/pull/70833 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[flang] [clang] [flang][windows] Add option to link against specific MSVC CRT (PR #70833)
https://github.com/DavidTruby updated https://github.com/llvm/llvm-project/pull/70833 >From 9e84729cada6c032c64934ee519e605407aab049 Mon Sep 17 00:00:00 2001 From: David Truby Date: Tue, 31 Oct 2023 15:07:13 + Subject: [PATCH 1/3] [flang][windows] Add option to link against specific MSVC CRT Currently flang's runtime libraries are only built for the specific CRT that LLVM itself was built against. This patch adds the cmake logic for building a separate runtime for each CRT configuration and adds a flag for selecting a CRT configuration to link against. --- clang/include/clang/Driver/Options.td | 2 +- clang/lib/Driver/ToolChains/CommonArgs.cpp | 42 +-- clang/lib/Driver/ToolChains/CommonArgs.h | 2 +- clang/lib/Driver/ToolChains/Darwin.cpp | 2 +- clang/lib/Driver/ToolChains/DragonFly.cpp | 2 +- clang/lib/Driver/ToolChains/FreeBSD.cpp| 2 +- clang/lib/Driver/ToolChains/Gnu.cpp| 2 +- clang/lib/Driver/ToolChains/Haiku.cpp | 2 +- clang/lib/Driver/ToolChains/MSVC.cpp | 2 +- clang/lib/Driver/ToolChains/MinGW.cpp | 2 +- clang/lib/Driver/ToolChains/NetBSD.cpp | 2 +- clang/lib/Driver/ToolChains/OpenBSD.cpp| 2 +- clang/lib/Driver/ToolChains/Solaris.cpp| 2 +- flang/lib/Decimal/CMakeLists.txt | 23 +++ flang/runtime/CMakeLists.txt | 23 +++ flang/runtime/FortranMain/CMakeLists.txt | 18 + flang/test/Driver/driver-help-hidden.f90 | 2 + flang/test/Driver/driver-help.f90 | 2 + flang/test/Driver/linker-flags.f90 | 47 ++ 19 files changed, 157 insertions(+), 24 deletions(-) diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index c8b730e0f7ecd84..66d4794714c9529 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -2842,7 +2842,7 @@ def fms_compatibility_version "version number to report in _MSC_VER (0 = don't define it " "(default))">; def fms_runtime_lib_EQ : Joined<["-"], "fms-runtime-lib=">, Group, - Flags<[]>, Visibility<[ClangOption, CLOption]>, + Flags<[]>, Visibility<[ClangOption, CLOption, FlangOption]>, Values<"static,static_dbg,dll,dll_dbg">, HelpText<"Select Windows run-time library">, DocBrief<[{ diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp index ad012d3d0d4b46f..1cac9a179eb960f 100644 --- a/clang/lib/Driver/ToolChains/CommonArgs.cpp +++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp @@ -976,12 +976,46 @@ bool tools::addOpenMPRuntime(ArgStringList , const ToolChain , return true; } -void tools::addFortranRuntimeLibs(const ToolChain , +void tools::addFortranRuntimeLibs(const ToolChain , const ArgList , llvm::opt::ArgStringList ) { if (TC.getTriple().isKnownWindowsMSVCEnvironment()) { -CmdArgs.push_back("Fortran_main.lib"); -CmdArgs.push_back("FortranRuntime.lib"); -CmdArgs.push_back("FortranDecimal.lib"); +CmdArgs.push_back(Args.MakeArgString( +"/DEFAULTLIB:" + TC.getCompilerRTBasename(Args, "builtins"))); +unsigned RTOptionID = options::OPT__SLASH_MT; +if (auto *rtl = Args.getLastArg(options::OPT_fms_runtime_lib_EQ)) { + RTOptionID = llvm::StringSwitch(rtl->getValue()) + .Case("static", options::OPT__SLASH_MT) + .Case("static_dbg", options::OPT__SLASH_MTd) + .Case("dll", options::OPT__SLASH_MD) + .Case("dll_dbg", options::OPT__SLASH_MDd) + .Default(options::OPT__SLASH_MT); +} +switch(RTOptionID) { +case options::OPT__SLASH_MT: + CmdArgs.push_back("/DEFAULTLIB:libcmt"); + CmdArgs.push_back("Fortran_main.static.lib"); + CmdArgs.push_back("FortranRuntime.static.lib"); + CmdArgs.push_back("FortranDecimal.static.lib"); + break; +case options::OPT__SLASH_MTd: + CmdArgs.push_back("/DEFAULTLIB:libcmtd"); + CmdArgs.push_back("Fortran_main.static_debug.lib"); + CmdArgs.push_back("FortranRuntime.static_debug.lib"); + CmdArgs.push_back("FortranDecimal.static_debug.lib"); + break; +case options::OPT__SLASH_MD: + CmdArgs.push_back("/DEFAULTLIB:msvcrt"); + CmdArgs.push_back("Fortran_main.dynamic.lib"); + CmdArgs.push_back("FortranRuntime.dynamic.lib"); + CmdArgs.push_back("FortranDecimal.dynamic.lib"); + break; +case options::OPT__SLASH_MDd: + CmdArgs.push_back("/DEFAULTLIB:msvcrtd"); + CmdArgs.push_back("Fortran_main.dynamic_debug.lib"); + CmdArgs.push_back("FortranRuntime.dynamic_debug.lib"); + CmdArgs.push_back("FortranDecimal.dynamic_debug.lib"); + break; +} } else { CmdArgs.push_back("-lFortran_main"); CmdArgs.push_back("-lFortranRuntime"); diff --git a/clang/lib/Driver/ToolChains/CommonArgs.h
[flang] [clang] [flang][windows] Add option to link against specific MSVC CRT (PR #70833)
@@ -53,3 +53,26 @@ add_flang_library(FortranDecimal INSTALL_WITH_TOOLCHAIN binary-to-decimal.cpp decimal-to-binary.cpp ) + +if (DEFINED MSVC) + set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded) bradking wrote: `add_flang_library` eventually ends up in `llvm/cmake/modules/AddLLVM.cmake`'s `llvm_add_library` which calls `add_library(${name} STATIC ...)`. All `CMAKE_MSVC_RUNTIME_LIBRARY` does is initialize the `MSVC_RUNTIME_LIBRARY` property on that target when it is created. You should be able to do ```cmake add_flang_library(FortranDecimal.static ...) set_property(TARGET FortranDecimal.static PROPERTY MSVC_RUNTIME_LIBRARY MultiThreaded) ``` https://github.com/llvm/llvm-project/pull/70833 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[flang] [clang] [flang][windows] Add option to link against specific MSVC CRT (PR #70833)
@@ -976,12 +976,46 @@ bool tools::addOpenMPRuntime(ArgStringList , const ToolChain , return true; } -void tools::addFortranRuntimeLibs(const ToolChain , +void tools::addFortranRuntimeLibs(const ToolChain , const ArgList , llvm::opt::ArgStringList ) { if (TC.getTriple().isKnownWindowsMSVCEnvironment()) { -CmdArgs.push_back("Fortran_main.lib"); -CmdArgs.push_back("FortranRuntime.lib"); -CmdArgs.push_back("FortranDecimal.lib"); +CmdArgs.push_back(Args.MakeArgString( +"/DEFAULTLIB:" + TC.getCompilerRTBasename(Args, "builtins"))); +unsigned RTOptionID = options::OPT__SLASH_MT; bradking wrote: As of LLVM 17.0's distribution, the Fortran runtime libraries are built with `msvcrt`, so I think the current default is actually `/MD`. Since this wasn't really modeled before, and CMake will be taught to pass one of the four flags explicitly anyway, changing the default may not matter, but it's something to be aware of. https://github.com/llvm/llvm-project/pull/70833 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[flang] [clang] [flang][windows] Add option to link against specific MSVC CRT (PR #70833)
https://github.com/bradking edited https://github.com/llvm/llvm-project/pull/70833 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[flang] [clang] [flang][windows] Add option to link against specific MSVC CRT (PR #70833)
@@ -976,12 +976,46 @@ bool tools::addOpenMPRuntime(ArgStringList , const ToolChain , return true; } -void tools::addFortranRuntimeLibs(const ToolChain , +void tools::addFortranRuntimeLibs(const ToolChain , const ArgList , llvm::opt::ArgStringList ) { if (TC.getTriple().isKnownWindowsMSVCEnvironment()) { -CmdArgs.push_back("Fortran_main.lib"); -CmdArgs.push_back("FortranRuntime.lib"); -CmdArgs.push_back("FortranDecimal.lib"); +CmdArgs.push_back(Args.MakeArgString( +"/DEFAULTLIB:" + TC.getCompilerRTBasename(Args, "builtins"))); +unsigned RTOptionID = options::OPT__SLASH_MT; +if (auto *rtl = Args.getLastArg(options::OPT_fms_runtime_lib_EQ)) { + RTOptionID = llvm::StringSwitch(rtl->getValue()) + .Case("static", options::OPT__SLASH_MT) + .Case("static_dbg", options::OPT__SLASH_MTd) + .Case("dll", options::OPT__SLASH_MD) + .Case("dll_dbg", options::OPT__SLASH_MDd) + .Default(options::OPT__SLASH_MT); +} +switch (RTOptionID) { +case options::OPT__SLASH_MT: + CmdArgs.push_back("/DEFAULTLIB:libcmt"); + CmdArgs.push_back("Fortran_main.static.lib"); + CmdArgs.push_back("FortranRuntime.static.lib"); + CmdArgs.push_back("FortranDecimal.static.lib"); + break; +case options::OPT__SLASH_MTd: + CmdArgs.push_back("/DEFAULTLIB:libcmtd"); + CmdArgs.push_back("Fortran_main.static_debug.lib"); + CmdArgs.push_back("FortranRuntime.static_debug.lib"); + CmdArgs.push_back("FortranDecimal.static_debug.lib"); + break; +case options::OPT__SLASH_MD: + CmdArgs.push_back("/DEFAULTLIB:msvcrt"); + CmdArgs.push_back("Fortran_main.dynamic.lib"); + CmdArgs.push_back("FortranRuntime.dynamic.lib"); + CmdArgs.push_back("FortranDecimal.dynamic.lib"); + break; bradking wrote: >From https://github.com/llvm/llvm-project/pull/70833#issuecomment-1787651022: > I think you're probably right in the linked issue that it'd be better to add > defaultlib directives to the object files, but that appears to be quite > difficult as we'd need to track the attributes all the way through our MLIR > lowering, so as a (hopefully temporary) shortcut I have just passed the > libraries on the link line. This temporary approach will actually make things harder for CMake to support `flang-new`. In order to support mixed-language (e.g., Fortran and C++) binaries we detect the implicit link directories and libraries that each compiler driver passes to the linker when used to drive linking. Then if we have to link using a different language's tooling, we can add them explicitly. We don't typically do that for the MSVC ABI though because the set of runtime libraries varies with the CRT choice and the defaultlib directives in object files handle it automatically anyway. Currently CMake is working around the lack of defaultlib directives for `flang-new` by using the implicit-lib-detection approach. Once the implicitly linked runtime libraries vary with the CRT, we would need a lot of dedicated non-trivial infrastructure to handle all the `MSVC_RUNTIME_LIBRARY` variants, and I'm not sure it's possible in all cases. Can you instead add these four CRT-specific libraries as defaultlib directives in all object files, and add the more detailed conditions to remove unnecessary libraries later? Since they are all static `.lib` files, unused directives may not hurt. https://github.com/llvm/llvm-project/pull/70833 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[flang] [clang] [flang][windows] Add option to link against specific MSVC CRT (PR #70833)
https://github.com/bradking requested changes to this pull request. https://github.com/llvm/llvm-project/pull/70833 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[flang] [clang] [flang][windows] Add option to link against specific MSVC CRT (PR #70833)
@@ -53,3 +53,26 @@ add_flang_library(FortranDecimal INSTALL_WITH_TOOLCHAIN binary-to-decimal.cpp decimal-to-binary.cpp ) + +if (DEFINED MSVC) + set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded) DavidTruby wrote: Yeah I tried this but inside add_flang_library these are made into custom targets (meaning the target property doesn’t do anything) and I’m not entirely sure why so I didn’t want to mess with it too much. I’ll give it another try https://github.com/llvm/llvm-project/pull/70833 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[flang] [clang] [flang][windows] Add option to link against specific MSVC CRT (PR #70833)
https://github.com/DavidTruby edited https://github.com/llvm/llvm-project/pull/70833 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[flang] [clang] [flang][windows] Add option to link against specific MSVC CRT (PR #70833)
@@ -53,3 +53,26 @@ add_flang_library(FortranDecimal INSTALL_WITH_TOOLCHAIN binary-to-decimal.cpp decimal-to-binary.cpp ) + +if (DEFINED MSVC) + set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded) mstorsjo wrote: Instead of redefining `CMAKE_MSVC_RUNTIME_LIBRARY` repeatedly, if you really want to set it specifically for one library, it's better to set the `MSVC_RUNTIME_LIBRARY` target property instead - see https://cmake.org/cmake/help/latest/prop_tgt/MSVC_RUNTIME_LIBRARY.html. https://github.com/llvm/llvm-project/pull/70833 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits