[flang-commits] [clang] [flang] [flang][windows] Add option to link against specific MSVC CRT (PR #70833)

David Truby via flang-commits flang-commits at lists.llvm.org
Thu Nov 2 08:02:58 PDT 2023


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 <david at truby.dev>
Date: Tue, 31 Oct 2023 15:07:13 +0000
Subject: [PATCH 1/4] [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<f_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 &CmdArgs, const ToolChain &TC,
   return true;
 }
 
-void tools::addFortranRuntimeLibs(const ToolChain &TC,
+void tools::addFortranRuntimeLibs(const ToolChain &TC, const ArgList &Args,
                                   llvm::opt::ArgStringList &CmdArgs) {
   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<unsigned>(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 b/clang/lib/Driver/ToolChains/CommonArgs.h
index f364c9793c9be62..0a0951c5386e601 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.h
+++ b/clang/lib/Driver/ToolChains/CommonArgs.h
@@ -116,7 +116,7 @@ bool addOpenMPRuntime(llvm::opt::ArgStringList &CmdArgs, const ToolChain &TC,
                       bool IsOffloadingHost = false, bool GompNeedsRT = false);
 
 /// Adds Fortran runtime libraries to \p CmdArgs.
-void addFortranRuntimeLibs(const ToolChain &TC,
+void addFortranRuntimeLibs(const ToolChain &TC, const llvm::opt::ArgList &Args,
                            llvm::opt::ArgStringList &CmdArgs);
 
 /// Adds the path for the Fortran runtime libraries to \p CmdArgs.
diff --git a/clang/lib/Driver/ToolChains/Darwin.cpp b/clang/lib/Driver/ToolChains/Darwin.cpp
index f28e08d81bf29b4..7481f6cab9a968d 100644
--- a/clang/lib/Driver/ToolChains/Darwin.cpp
+++ b/clang/lib/Driver/ToolChains/Darwin.cpp
@@ -678,7 +678,7 @@ void darwin::Linker::ConstructJob(Compilation &C, const JobAction &JA,
   // to generate executables.
   if (getToolChain().getDriver().IsFlangMode()) {
     addFortranRuntimeLibraryPath(getToolChain(), Args, CmdArgs);
-    addFortranRuntimeLibs(getToolChain(), CmdArgs);
+    addFortranRuntimeLibs(getToolChain(), Args, CmdArgs);
   }
 
   if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs))
diff --git a/clang/lib/Driver/ToolChains/DragonFly.cpp b/clang/lib/Driver/ToolChains/DragonFly.cpp
index ed7f751adc0efaf..19131f45dd1ec2c 100644
--- a/clang/lib/Driver/ToolChains/DragonFly.cpp
+++ b/clang/lib/Driver/ToolChains/DragonFly.cpp
@@ -150,7 +150,7 @@ void dragonfly::Linker::ConstructJob(Compilation &C, const JobAction &JA,
     // AddRunTimeLibs).
     if (D.IsFlangMode()) {
       addFortranRuntimeLibraryPath(ToolChain, Args, CmdArgs);
-      addFortranRuntimeLibs(ToolChain, CmdArgs);
+      addFortranRuntimeLibs(ToolChain, Args, CmdArgs);
       CmdArgs.push_back("-lm");
     }
 
diff --git a/clang/lib/Driver/ToolChains/FreeBSD.cpp b/clang/lib/Driver/ToolChains/FreeBSD.cpp
index e7dcccc9e9fc4c8..e285e07e23514a2 100644
--- a/clang/lib/Driver/ToolChains/FreeBSD.cpp
+++ b/clang/lib/Driver/ToolChains/FreeBSD.cpp
@@ -297,7 +297,7 @@ void freebsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
     // AddRunTimeLibs).
     if (D.IsFlangMode()) {
       addFortranRuntimeLibraryPath(ToolChain, Args, CmdArgs);
-      addFortranRuntimeLibs(ToolChain, CmdArgs);
+      addFortranRuntimeLibs(ToolChain, Args, CmdArgs);
       if (Profiling)
         CmdArgs.push_back("-lm_p");
       else
diff --git a/clang/lib/Driver/ToolChains/Gnu.cpp b/clang/lib/Driver/ToolChains/Gnu.cpp
index 5237951f84cce03..e38f002b04b6519 100644
--- a/clang/lib/Driver/ToolChains/Gnu.cpp
+++ b/clang/lib/Driver/ToolChains/Gnu.cpp
@@ -576,7 +576,7 @@ void tools::gnutools::Linker::ConstructJob(Compilation &C, const JobAction &JA,
   // AddRunTimeLibs).
   if (D.IsFlangMode()) {
     addFortranRuntimeLibraryPath(ToolChain, Args, CmdArgs);
-    addFortranRuntimeLibs(ToolChain, CmdArgs);
+    addFortranRuntimeLibs(ToolChain, Args, CmdArgs);
     CmdArgs.push_back("-lm");
   }
 
diff --git a/clang/lib/Driver/ToolChains/Haiku.cpp b/clang/lib/Driver/ToolChains/Haiku.cpp
index b940150788f65c7..7a41e4a99b84b39 100644
--- a/clang/lib/Driver/ToolChains/Haiku.cpp
+++ b/clang/lib/Driver/ToolChains/Haiku.cpp
@@ -101,7 +101,7 @@ void haiku::Linker::ConstructJob(Compilation &C, const JobAction &JA,
     // AddRunTimeLibs).
     if (D.IsFlangMode()) {
       addFortranRuntimeLibraryPath(ToolChain, Args, CmdArgs);
-      addFortranRuntimeLibs(ToolChain, CmdArgs);
+      addFortranRuntimeLibs(ToolChain, Args, CmdArgs);
     }
 
     CmdArgs.push_back("-lgcc");
diff --git a/clang/lib/Driver/ToolChains/MSVC.cpp b/clang/lib/Driver/ToolChains/MSVC.cpp
index 4966d102c51f1ac..8a4a174c90ea855 100644
--- a/clang/lib/Driver/ToolChains/MSVC.cpp
+++ b/clang/lib/Driver/ToolChains/MSVC.cpp
@@ -131,7 +131,7 @@ void visualstudio::Linker::ConstructJob(Compilation &C, const JobAction &JA,
 
   if (C.getDriver().IsFlangMode()) {
     addFortranRuntimeLibraryPath(TC, Args, CmdArgs);
-    addFortranRuntimeLibs(TC, CmdArgs);
+    addFortranRuntimeLibs(TC, Args, CmdArgs);
 
     // Inform the MSVC linker that we're generating a console application, i.e.
     // one with `main` as the "user-defined" entry point. The `main` function is
diff --git a/clang/lib/Driver/ToolChains/MinGW.cpp b/clang/lib/Driver/ToolChains/MinGW.cpp
index 39d767795445dbe..5d7f8675daf8d28 100644
--- a/clang/lib/Driver/ToolChains/MinGW.cpp
+++ b/clang/lib/Driver/ToolChains/MinGW.cpp
@@ -249,7 +249,7 @@ void tools::MinGW::Linker::ConstructJob(Compilation &C, const JobAction &JA,
 
   if (C.getDriver().IsFlangMode()) {
     addFortranRuntimeLibraryPath(TC, Args, CmdArgs);
-    addFortranRuntimeLibs(TC, CmdArgs);
+    addFortranRuntimeLibs(TC, Args, CmdArgs);
   }
 
   // TODO: Add profile stuff here
diff --git a/clang/lib/Driver/ToolChains/NetBSD.cpp b/clang/lib/Driver/ToolChains/NetBSD.cpp
index 7a1d4561c6f2f4f..beeea528f4c003c 100644
--- a/clang/lib/Driver/ToolChains/NetBSD.cpp
+++ b/clang/lib/Driver/ToolChains/NetBSD.cpp
@@ -321,7 +321,7 @@ void netbsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
     // AddRunTimeLibs).
     if (D.IsFlangMode()) {
       addFortranRuntimeLibraryPath(ToolChain, Args, CmdArgs);
-      addFortranRuntimeLibs(ToolChain, CmdArgs);
+      addFortranRuntimeLibs(ToolChain, Args, CmdArgs);
       CmdArgs.push_back("-lm");
     }
 
diff --git a/clang/lib/Driver/ToolChains/OpenBSD.cpp b/clang/lib/Driver/ToolChains/OpenBSD.cpp
index 16a311be31be7bc..5f531fc54ea5c0e 100644
--- a/clang/lib/Driver/ToolChains/OpenBSD.cpp
+++ b/clang/lib/Driver/ToolChains/OpenBSD.cpp
@@ -220,7 +220,7 @@ void openbsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
     // AddRunTimeLibs).
     if (D.IsFlangMode()) {
       addFortranRuntimeLibraryPath(ToolChain, Args, CmdArgs);
-      addFortranRuntimeLibs(ToolChain, CmdArgs);
+      addFortranRuntimeLibs(ToolChain, Args, CmdArgs);
       if (Profiling)
         CmdArgs.push_back("-lm_p");
       else
diff --git a/clang/lib/Driver/ToolChains/Solaris.cpp b/clang/lib/Driver/ToolChains/Solaris.cpp
index 3d5a710842eca44..a2006076004a0af 100644
--- a/clang/lib/Driver/ToolChains/Solaris.cpp
+++ b/clang/lib/Driver/ToolChains/Solaris.cpp
@@ -225,7 +225,7 @@ void solaris::Linker::ConstructJob(Compilation &C, const JobAction &JA,
     // these dependencies need to be listed before the C runtime below.
     if (D.IsFlangMode()) {
       addFortranRuntimeLibraryPath(getToolChain(), Args, CmdArgs);
-      addFortranRuntimeLibs(getToolChain(), CmdArgs);
+      addFortranRuntimeLibs(getToolChain(), Args, CmdArgs);
       CmdArgs.push_back("-lm");
     }
     if (Args.hasArg(options::OPT_fstack_protector) ||
diff --git a/flang/lib/Decimal/CMakeLists.txt b/flang/lib/Decimal/CMakeLists.txt
index 3116ff68ea2627e..95fc2188d2f6748 100644
--- a/flang/lib/Decimal/CMakeLists.txt
+++ b/flang/lib/Decimal/CMakeLists.txt
@@ -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)
+  add_flang_library(FortranDecimal.static INSTALL_WITH_TOOLCHAIN
+          binary-to-decimal.cpp
+          decimal-to-binary.cpp
+  )
+  set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreadedDLL)
+  add_flang_library(FortranDecimal.dynamic INSTALL_WITH_TOOLCHAIN
+          binary-to-decimal.cpp
+          decimal-to-binary.cpp
+  )
+  set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreadedDebug)
+  add_flang_library(FortranDecimal.static_debug INSTALL_WITH_TOOLCHAIN
+          binary-to-decimal.cpp
+          decimal-to-binary.cpp
+  )
+  set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreadedDebugDLL)
+  add_flang_library(FortranDecimal.dynamic_debug INSTALL_WITH_TOOLCHAIN
+          binary-to-decimal.cpp
+          decimal-to-binary.cpp
+  )
+endif()
\ No newline at end of file
diff --git a/flang/runtime/CMakeLists.txt b/flang/runtime/CMakeLists.txt
index f75daa373705f00..77882615c3dbce7 100644
--- a/flang/runtime/CMakeLists.txt
+++ b/flang/runtime/CMakeLists.txt
@@ -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}
+    LINK_LIBS
+    FortranDecimal.static
+    INSTALL_WITH_TOOLCHAIN)
+  set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreadedDLL)
+  add_flang_library(FortranRuntime.dynamic ${sources}
+          LINK_LIBS
+          FortranDecimal.dynamic
+          INSTALL_WITH_TOOLCHAIN)
+  set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreadedDebug)
+  add_flang_library(FortranRuntime.static_debug ${sources}
+          LINK_LIBS
+          FortranDecimal.static_debug
+          INSTALL_WITH_TOOLCHAIN)
+  set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreadedDebugDLL)
+  add_flang_library(FortranRuntime.dynamic_debug ${sources}
+          LINK_LIBS
+          FortranDecimal.dynamic_debug
+          INSTALL_WITH_TOOLCHAIN)
+endif()
diff --git a/flang/runtime/FortranMain/CMakeLists.txt b/flang/runtime/FortranMain/CMakeLists.txt
index fe0d607c3f1a951..9fff1317d386d59 100644
--- a/flang/runtime/FortranMain/CMakeLists.txt
+++ b/flang/runtime/FortranMain/CMakeLists.txt
@@ -1,3 +1,21 @@
 add_flang_library(Fortran_main STATIC INSTALL_WITH_TOOLCHAIN
   Fortran_main.c
 )
+if (DEFINED MSVC)
+    set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded)
+    add_flang_library(Fortran_main.static STATIC INSTALL_WITH_TOOLCHAIN
+            Fortran_main.c
+    )
+    set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreadedDLL)
+    add_flang_library(Fortran_main.dynamic STATIC INSTALL_WITH_TOOLCHAIN
+            Fortran_main.c
+    )
+    set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreadedDebug)
+    add_flang_library(Fortran_main.static_debug STATIC INSTALL_WITH_TOOLCHAIN
+            Fortran_main.c
+    )
+    set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreadedDebugDLL)
+    add_flang_library(Fortran_main.dynamic_debug STATIC INSTALL_WITH_TOOLCHAIN
+            Fortran_main.c
+    )
+endif()
diff --git a/flang/test/Driver/driver-help-hidden.f90 b/flang/test/Driver/driver-help-hidden.f90
index 6d399f1d179a022..37c59221c3e0bc0 100644
--- a/flang/test/Driver/driver-help-hidden.f90
+++ b/flang/test/Driver/driver-help-hidden.f90
@@ -59,6 +59,8 @@
 ! CHECK-NEXT: -flto=jobserver         Enable LTO in 'full' mode
 ! CHECK-NEXT: -flto=<value>           Set LTO mode
 ! CHECK-NEXT: -flto                   Enable LTO in 'full' mode
+! CHECK-NEXT: -fms-runtime-lib=<value>
+! CHECK-NEXT:                         Select Windows run-time library
 ! CHECK-NEXT: -fno-alias-analysis     Do not pass alias information on to LLVM (default for unoptimized builds)
 ! CHECK-NEXT: -fno-automatic          Implies the SAVE attribute for non-automatic local objects in subprograms unless RECURSIVE
 ! CHECK-NEXT: -fno-color-diagnostics  Disable colors in diagnostics
diff --git a/flang/test/Driver/driver-help.f90 b/flang/test/Driver/driver-help.f90
index 31c9caa32ea8292..2f81ec260ec964f 100644
--- a/flang/test/Driver/driver-help.f90
+++ b/flang/test/Driver/driver-help.f90
@@ -51,6 +51,8 @@
 ! HELP-NEXT: -flto=jobserver         Enable LTO in 'full' mode
 ! HELP-NEXT: -flto=<value>           Set LTO mode
 ! HELP-NEXT: -flto                   Enable LTO in 'full' mode
+! HELP-NEXT: -fms-runtime-lib=<value>
+! HELP-NEXT:                         Select Windows run-time library
 ! HELP-NEXT: -fno-alias-analysis     Do not pass alias information on to LLVM (default for unoptimized builds)
 ! HELP-NEXT: -fno-automatic          Implies the SAVE attribute for non-automatic local objects in subprograms unless RECURSIVE
 ! HELP-NEXT: -fno-color-diagnostics  Disable colors in diagnostics
diff --git a/flang/test/Driver/linker-flags.f90 b/flang/test/Driver/linker-flags.f90
index e4d713df499b7d0..63451eeab62c90d 100644
--- a/flang/test/Driver/linker-flags.f90
+++ b/flang/test/Driver/linker-flags.f90
@@ -12,11 +12,13 @@
 ! RUN: %flang -### --target=x86_64-unknown-haiku %S/Inputs/hello.f90 2>&1 | FileCheck %s --check-prefixes=CHECK,HAIKU
 ! RUN: %flang -### --target=x86_64-windows-gnu %S/Inputs/hello.f90 2>&1 | FileCheck %s --check-prefixes=CHECK,MINGW
 
-! NOTE: Clang's driver library, clangDriver, usually adds 'libcmt' and
-!       'oldnames' on Windows, but they are not needed when compiling
-!       Fortran code and they might bring in additional dependencies.
-!       Make sure they're not added.
-! RUN: %flang -### --target=aarch64-windows-msvc -fuse-ld= %S/Inputs/hello.f90 2>&1 | FileCheck %s --check-prefixes=CHECK,MSVC --implicit-check-not libcmt --implicit-check-not oldnames
+! NOTE: Clang's driver library, clangDriver, usually adds 'oldnames' on Windows,
+!       but it is not needed when compiling Fortran code and they might bring in
+!       additional dependencies. Make sure its not added.
+! RUN: %flang -### --target=aarch64-windows-msvc -fuse-ld= %S/Inputs/hello.f90 2>&1 | FileCheck %s --check-prefixes=CHECK,MSVC --implicit-check-not oldnames
+! RUN: %flang -### --target=aarch64-windows-msvc -fms-runtime-lib=static_dbg -fuse-ld= %S/Inputs/hello.f90 2>&1 | FileCheck %s --check-prefixes=CHECK,MSVC-DEBUG --implicit-check-not oldnames
+! RUN: %flang -### --target=aarch64-windows-msvc -fms-runtime-lib=dll -fuse-ld= %S/Inputs/hello.f90 2>&1 | FileCheck %s --check-prefixes=CHECK,MSVC-DLL --implicit-check-not oldnames
+! RUN: %flang -### --target=aarch64-windows-msvc -fms-runtime-lib=dll_dbg -fuse-ld= %S/Inputs/hello.f90 2>&1 | FileCheck %s --check-prefixes=CHECK,MSVC-DLL-DEBUG --implicit-check-not oldnames
 
 ! Compiler invocation to generate the object file
 ! CHECK-LABEL: {{.*}} "-emit-obj"
@@ -52,8 +54,37 @@
 !       (lld-)link.exe on Windows platforms. The suffix may not be added
 !       when the executable is not found or on non-Windows platforms.
 ! MSVC-LABEL: link
-! MSVC-SAME: Fortran_main.lib
-! MSVC-SAME: FortranRuntime.lib
-! MSVC-SAME: FortranDecimal.lib
+! MSVC-SAME: /DEFAULTLIB:clang_rt.builtins-aarch64.lib
+! MSVC-SAME: /DEFAULTLIB:libcmt
+! MSVC-SAME: Fortran_main.static.lib
+! MSVC-SAME: FortranRuntime.static.lib
+! MSVC-SAME: FortranDecimal.static.lib
 ! MSVC-SAME: /subsystem:console
 ! MSVC-SAME: "[[object_file]]"
+
+! MSVC-DEBUG-LABEL: link
+! MSVC-DEBUG-SAME: /DEFAULTLIB:clang_rt.builtins-aarch64.lib
+! MSVC-DEBUG-SAME: /DEFAULTLIB:libcmtd
+! MSVC-DEBUG-SAME: Fortran_main.static_debug.lib
+! MSVC-DEBUG-SAME: FortranRuntime.static_debug.lib
+! MSVC-DEBUG-SAME: FortranDecimal.static_debug.lib
+! MSVC-DEBUG-SAME: /subsystem:console
+! MSVC-DEBUG-SAME: "[[object_file]]"
+
+! MSVC-DLL-LABEL: link
+! MSVC-DLL-SAME: /DEFAULTLIB:clang_rt.builtins-aarch64.lib
+! MSVC-DLL-SAME: /DEFAULTLIB:msvcrt
+! MSVC-DLL-SAME: Fortran_main.dynamic.lib
+! MSVC-DLL-SAME: FortranRuntime.dynamic.lib
+! MSVC-DLL-SAME: FortranDecimal.dynamic.lib
+! MSVC-DLL-SAME: /subsystem:console
+! MSVC-DLL-SAME: "[[object_file]]"
+
+! MSVC-DLL-DEBUG-LABEL: link
+! MSVC-DLL-DEBUG-SAME: /DEFAULTLIB:clang_rt.builtins-aarch64.lib
+! MSVC-DLL-DEBUG-SAME: /DEFAULTLIB:msvcrtd
+! MSVC-DLL-DEBUG-SAME: Fortran_main.dynamic_debug.lib
+! MSVC-DLL-DEBUG-SAME: FortranRuntime.dynamic_debug.lib
+! MSVC-DLL-DEBUG-SAME: FortranDecimal.dynamic_debug.lib
+! MSVC-DLL-DEBUG-SAME: /subsystem:console
+! MSVC-DLL-DEBUG-SAME: "[[object_file]]"

>From eb8fda365d6384363a02e7caaf13d9e0719eece6 Mon Sep 17 00:00:00 2001
From: David Truby <david at truby.dev>
Date: Tue, 31 Oct 2023 17:47:43 +0000
Subject: [PATCH 2/4] Fix formatting

---
 clang/lib/Driver/ToolChains/CommonArgs.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index 1cac9a179eb960f..60dbe9f87a1fe04 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -990,7 +990,7 @@ void tools::addFortranRuntimeLibs(const ToolChain &TC, const ArgList &Args,
                        .Case("dll_dbg", options::OPT__SLASH_MDd)
                        .Default(options::OPT__SLASH_MT);
     }
-    switch(RTOptionID) {
+    switch (RTOptionID) {
     case options::OPT__SLASH_MT:
       CmdArgs.push_back("/DEFAULTLIB:libcmt");
       CmdArgs.push_back("Fortran_main.static.lib");

>From 3fbc0ba09fa7b093f882afcb2276360db3106bcb Mon Sep 17 00:00:00 2001
From: David Truby <david at truby.dev>
Date: Wed, 1 Nov 2023 14:52:47 +0000
Subject: [PATCH 3/4] Select runtime using properties

---
 flang/lib/Decimal/CMakeLists.txt         | 8 ++++----
 flang/runtime/CMakeLists.txt             | 8 ++++----
 flang/runtime/FortranMain/CMakeLists.txt | 8 ++++----
 3 files changed, 12 insertions(+), 12 deletions(-)

diff --git a/flang/lib/Decimal/CMakeLists.txt b/flang/lib/Decimal/CMakeLists.txt
index 95fc2188d2f6748..30be37c83f1163e 100644
--- a/flang/lib/Decimal/CMakeLists.txt
+++ b/flang/lib/Decimal/CMakeLists.txt
@@ -55,24 +55,24 @@ add_flang_library(FortranDecimal INSTALL_WITH_TOOLCHAIN
 )
 
 if (DEFINED MSVC)
-  set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded)
   add_flang_library(FortranDecimal.static INSTALL_WITH_TOOLCHAIN
           binary-to-decimal.cpp
           decimal-to-binary.cpp
   )
-  set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreadedDLL)
+  set_property(TARGET FortranDecimal.static PROPERTY MSVC_RUNTIME_LIBRARY MultiThreaded)
   add_flang_library(FortranDecimal.dynamic INSTALL_WITH_TOOLCHAIN
           binary-to-decimal.cpp
           decimal-to-binary.cpp
   )
-  set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreadedDebug)
+  set_property(TARGET FortranDecimal.dynamic PROPERTY MSVC_RUNTIME_LIBRARY MultiThreadedDLL)
   add_flang_library(FortranDecimal.static_debug INSTALL_WITH_TOOLCHAIN
           binary-to-decimal.cpp
           decimal-to-binary.cpp
   )
-  set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreadedDebugDLL)
+  set_property(TARGET FortranDecimal.static_debug PROPERTY MSVC_RUNTIME_LIBRARY MultiThreadedDebug)
   add_flang_library(FortranDecimal.dynamic_debug INSTALL_WITH_TOOLCHAIN
           binary-to-decimal.cpp
           decimal-to-binary.cpp
   )
+  set_property(TARGET FortranDecimal.dynamic_debug PROPERTY MSVC_RUNTIME_LIBRARY MultiThreadedDebugDLL)
 endif()
\ No newline at end of file
diff --git a/flang/runtime/CMakeLists.txt b/flang/runtime/CMakeLists.txt
index 77882615c3dbce7..ac8ee959f0cf2ed 100644
--- a/flang/runtime/CMakeLists.txt
+++ b/flang/runtime/CMakeLists.txt
@@ -283,24 +283,24 @@ add_flang_library(FortranRuntime
 )
 
 if (DEFINED MSVC)
-  set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded)
   add_flang_library(FortranRuntime.static ${sources}
     LINK_LIBS
     FortranDecimal.static
     INSTALL_WITH_TOOLCHAIN)
-  set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreadedDLL)
+  set_property(TARGET FortranRuntime.static PROPERTY MSVC_RUNTIME_LIBRARY MultiThreaded)
   add_flang_library(FortranRuntime.dynamic ${sources}
           LINK_LIBS
           FortranDecimal.dynamic
           INSTALL_WITH_TOOLCHAIN)
-  set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreadedDebug)
+  set_property(TARGET FortranRuntime.dynamic PROPERTY MSVC_RUNTIME_LIBRARY MultiThreadedDLL)
   add_flang_library(FortranRuntime.static_debug ${sources}
           LINK_LIBS
           FortranDecimal.static_debug
           INSTALL_WITH_TOOLCHAIN)
-  set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreadedDebugDLL)
+  set_property(TARGET FortranRuntime.static_debug PROPERTY MSVC_RUNTIME_LIBRARY MultiThreadedDebug)
   add_flang_library(FortranRuntime.dynamic_debug ${sources}
           LINK_LIBS
           FortranDecimal.dynamic_debug
           INSTALL_WITH_TOOLCHAIN)
+  set_property(TARGET FortranRuntime.dynamic_debug PROPERTY MSVC_RUNTIME_LIBRARY MultiThreadedDebugDLL)
 endif()
diff --git a/flang/runtime/FortranMain/CMakeLists.txt b/flang/runtime/FortranMain/CMakeLists.txt
index 9fff1317d386d59..c96dfdef2ff27c6 100644
--- a/flang/runtime/FortranMain/CMakeLists.txt
+++ b/flang/runtime/FortranMain/CMakeLists.txt
@@ -2,20 +2,20 @@ add_flang_library(Fortran_main STATIC INSTALL_WITH_TOOLCHAIN
   Fortran_main.c
 )
 if (DEFINED MSVC)
-    set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded)
     add_flang_library(Fortran_main.static STATIC INSTALL_WITH_TOOLCHAIN
             Fortran_main.c
     )
-    set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreadedDLL)
+    set_property(TARGET Fortran_main.static PROPERTY MSVC_RUNTIME_LIBRARY MultiThreaded)
     add_flang_library(Fortran_main.dynamic STATIC INSTALL_WITH_TOOLCHAIN
             Fortran_main.c
     )
-    set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreadedDebug)
+    set_property(TARGET Fortran_main.dynamic PROPERTY MSVC_RUNTIME_LIBRARY MultiThreadedDLL)
     add_flang_library(Fortran_main.static_debug STATIC INSTALL_WITH_TOOLCHAIN
             Fortran_main.c
     )
-    set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreadedDebugDLL)
+    set_property(TARGET Fortran_main.static_debug PROPERTY MSVC_RUNTIME_LIBRARY MultiThreadedDebug)
     add_flang_library(Fortran_main.dynamic_debug STATIC INSTALL_WITH_TOOLCHAIN
             Fortran_main.c
     )
+    set_property(TARGET Fortran_main.dynamic_debug PROPERTY MSVC_RUNTIME_LIBRARY MultiThreadedDebugDLL)
 endif()

>From f47b699159b93387f4171f9dafb3744b03314089 Mon Sep 17 00:00:00 2001
From: David Truby <david at truby.dev>
Date: Thu, 2 Nov 2023 15:01:23 +0000
Subject: [PATCH 4/4] Revert "Select runtime using properties"

This reverts commit 3fbc0ba09fa7b093f882afcb2276360db3106bcb.
---
 flang/lib/Decimal/CMakeLists.txt         | 8 ++++----
 flang/runtime/CMakeLists.txt             | 8 ++++----
 flang/runtime/FortranMain/CMakeLists.txt | 8 ++++----
 3 files changed, 12 insertions(+), 12 deletions(-)

diff --git a/flang/lib/Decimal/CMakeLists.txt b/flang/lib/Decimal/CMakeLists.txt
index 30be37c83f1163e..95fc2188d2f6748 100644
--- a/flang/lib/Decimal/CMakeLists.txt
+++ b/flang/lib/Decimal/CMakeLists.txt
@@ -55,24 +55,24 @@ add_flang_library(FortranDecimal INSTALL_WITH_TOOLCHAIN
 )
 
 if (DEFINED MSVC)
+  set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded)
   add_flang_library(FortranDecimal.static INSTALL_WITH_TOOLCHAIN
           binary-to-decimal.cpp
           decimal-to-binary.cpp
   )
-  set_property(TARGET FortranDecimal.static PROPERTY MSVC_RUNTIME_LIBRARY MultiThreaded)
+  set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreadedDLL)
   add_flang_library(FortranDecimal.dynamic INSTALL_WITH_TOOLCHAIN
           binary-to-decimal.cpp
           decimal-to-binary.cpp
   )
-  set_property(TARGET FortranDecimal.dynamic PROPERTY MSVC_RUNTIME_LIBRARY MultiThreadedDLL)
+  set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreadedDebug)
   add_flang_library(FortranDecimal.static_debug INSTALL_WITH_TOOLCHAIN
           binary-to-decimal.cpp
           decimal-to-binary.cpp
   )
-  set_property(TARGET FortranDecimal.static_debug PROPERTY MSVC_RUNTIME_LIBRARY MultiThreadedDebug)
+  set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreadedDebugDLL)
   add_flang_library(FortranDecimal.dynamic_debug INSTALL_WITH_TOOLCHAIN
           binary-to-decimal.cpp
           decimal-to-binary.cpp
   )
-  set_property(TARGET FortranDecimal.dynamic_debug PROPERTY MSVC_RUNTIME_LIBRARY MultiThreadedDebugDLL)
 endif()
\ No newline at end of file
diff --git a/flang/runtime/CMakeLists.txt b/flang/runtime/CMakeLists.txt
index ac8ee959f0cf2ed..77882615c3dbce7 100644
--- a/flang/runtime/CMakeLists.txt
+++ b/flang/runtime/CMakeLists.txt
@@ -283,24 +283,24 @@ add_flang_library(FortranRuntime
 )
 
 if (DEFINED MSVC)
+  set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded)
   add_flang_library(FortranRuntime.static ${sources}
     LINK_LIBS
     FortranDecimal.static
     INSTALL_WITH_TOOLCHAIN)
-  set_property(TARGET FortranRuntime.static PROPERTY MSVC_RUNTIME_LIBRARY MultiThreaded)
+  set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreadedDLL)
   add_flang_library(FortranRuntime.dynamic ${sources}
           LINK_LIBS
           FortranDecimal.dynamic
           INSTALL_WITH_TOOLCHAIN)
-  set_property(TARGET FortranRuntime.dynamic PROPERTY MSVC_RUNTIME_LIBRARY MultiThreadedDLL)
+  set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreadedDebug)
   add_flang_library(FortranRuntime.static_debug ${sources}
           LINK_LIBS
           FortranDecimal.static_debug
           INSTALL_WITH_TOOLCHAIN)
-  set_property(TARGET FortranRuntime.static_debug PROPERTY MSVC_RUNTIME_LIBRARY MultiThreadedDebug)
+  set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreadedDebugDLL)
   add_flang_library(FortranRuntime.dynamic_debug ${sources}
           LINK_LIBS
           FortranDecimal.dynamic_debug
           INSTALL_WITH_TOOLCHAIN)
-  set_property(TARGET FortranRuntime.dynamic_debug PROPERTY MSVC_RUNTIME_LIBRARY MultiThreadedDebugDLL)
 endif()
diff --git a/flang/runtime/FortranMain/CMakeLists.txt b/flang/runtime/FortranMain/CMakeLists.txt
index c96dfdef2ff27c6..9fff1317d386d59 100644
--- a/flang/runtime/FortranMain/CMakeLists.txt
+++ b/flang/runtime/FortranMain/CMakeLists.txt
@@ -2,20 +2,20 @@ add_flang_library(Fortran_main STATIC INSTALL_WITH_TOOLCHAIN
   Fortran_main.c
 )
 if (DEFINED MSVC)
+    set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded)
     add_flang_library(Fortran_main.static STATIC INSTALL_WITH_TOOLCHAIN
             Fortran_main.c
     )
-    set_property(TARGET Fortran_main.static PROPERTY MSVC_RUNTIME_LIBRARY MultiThreaded)
+    set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreadedDLL)
     add_flang_library(Fortran_main.dynamic STATIC INSTALL_WITH_TOOLCHAIN
             Fortran_main.c
     )
-    set_property(TARGET Fortran_main.dynamic PROPERTY MSVC_RUNTIME_LIBRARY MultiThreadedDLL)
+    set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreadedDebug)
     add_flang_library(Fortran_main.static_debug STATIC INSTALL_WITH_TOOLCHAIN
             Fortran_main.c
     )
-    set_property(TARGET Fortran_main.static_debug PROPERTY MSVC_RUNTIME_LIBRARY MultiThreadedDebug)
+    set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreadedDebugDLL)
     add_flang_library(Fortran_main.dynamic_debug STATIC INSTALL_WITH_TOOLCHAIN
             Fortran_main.c
     )
-    set_property(TARGET Fortran_main.dynamic_debug PROPERTY MSVC_RUNTIME_LIBRARY MultiThreadedDebugDLL)
 endif()



More information about the flang-commits mailing list