[PATCH] D133457: Add Clang driver flags equivalent to cl's /MD, /MT, /MDd, /MTd.This will allow selecting the MS C runtime library without having to usecc1 flags.

Amy Huang via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 7 14:58:51 PDT 2022


akhuang created this revision.
Herald added a project: All.
akhuang requested review of this revision.
Herald added subscribers: cfe-commits, MaskRay.
Herald added a project: clang.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D133457

Files:
  clang/include/clang/Driver/Options.td
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/test/Driver/cl-runtime-flags.c


Index: clang/test/Driver/cl-runtime-flags.c
===================================================================
--- clang/test/Driver/cl-runtime-flags.c
+++ clang/test/Driver/cl-runtime-flags.c
@@ -95,3 +95,39 @@
 // CHECK-MTZl-SAME: "-D_VC_NODEFAULTLIB"
 // CHECK-MTZl-NOT: "--dependent-lib=libcmt"
 // CHECK-MTZl-NOT: "--dependent-lib=oldnames"
+
+// Check for clang versions of the /MD and /MT flags.
+
+// RUN: %clang -### --target=x86_64-windows-msvc -fms-runtime-lib=static -- %s \
+// RUN:   2>&1 | FileCheck -check-prefix=CHECK-CLANG-STATIC %s
+// CHECK-CLANG-STATIC-NOT: "-D_DEBUG"
+// CHECK-CLANG-STATIC: "-D_MT"
+// CHECK-CLANG-STATIC-NOT: "-D_DLL"
+// CHECK-CLANG-STATIC: "-flto-visibility-public-std"
+// CHECK-CLANG-STATIC: "--dependent-lib=libcmt"
+// CHECK-CLANG-STATIC: "--dependent-lib=oldnames"
+
+// RUN: %clang -### --target=x86_64-windows-msvc -fms-runtime-lib=static_dbg \
+// RUN:   -- %s 2>&1 | FileCheck -check-prefix=CHECK-CLANG-STATIC-DBG %s
+// CHECK-CLANG-STATIC-DBG: "-D_DEBUG"
+// CHECK-CLANG-STATIC-DBG: "-D_MT"
+// CHECK-CLANG-STATIC-DBG-NOT: "-D_DLL"
+// CHECK-CLANG-STATIC-DBG: "-flto-visibility-public-std"
+// CHECK-CLANG-STATIC-DBG: "--dependent-lib=libcmtd"
+// CHECK-CLANG-STATIC-DBG: "--dependent-lib=oldnames"
+
+// RUN: %clang -### --target=x86_64-windows-msvc -fms-runtime-lib=dll -- %s \
+// RUN:   2>&1 | FileCheck -check-prefix=CHECK-CLANG-DLL %s
+// CHECK-CLANG-DLL-NOT: "-D_DEBUG"
+// CHECK-CLANG-DLL: "-D_MT"
+// CHECK-CLANG-DLL: "-D_DLL"
+// CHECK-CLANG-DLL: "--dependent-lib=msvcrt"
+// CHECK-CLANG-DLL: "--dependent-lib=oldnames"
+
+// RUN: %clang -### --target=x86_64-windows-msvc -fms-runtime-lib=dll_dbg -- \
+// RUN:   %s 2>&1 | FileCheck -check-prefix=CHECK-CLANG-DLL-DBG %s
+// CHECK-CLANG-DLL-DBG: "-D_DEBUG"
+// CHECK-CLANG-DLL-DBG: "-D_MT"
+// CHECK-CLANG-DLL-DBG: "-D_DLL"
+// CHECK-CLANG-DLL-DBG: "--dependent-lib=msvcrtd"
+// CHECK-CLANG-DLL-DBG: "--dependent-lib=oldnames"
Index: clang/lib/Driver/ToolChains/Clang.cpp
===================================================================
--- clang/lib/Driver/ToolChains/Clang.cpp
+++ clang/lib/Driver/ToolChains/Clang.cpp
@@ -6477,6 +6477,35 @@
   if (IsMSVCCompat)
     CmdArgs.push_back("-fms-compatibility");
 
+  // Process Windows runtime flags (equivalent to cl flags /MD, /MDd, /MT, /MTd)
+  if (Triple.isOSWindows()) {
+    if (Arg *A = Args.getLastArg(options::OPT_fms_runtime_lib_EQ)) {
+      StringRef Val = A->getValue();
+      if (Val == "dll") {
+        CmdArgs.push_back("-D_MT");
+        CmdArgs.push_back("-D_DLL");
+        CmdArgs.push_back("--dependent-lib=msvcrt");
+      } else if (Val == "dll_dbg") {
+        CmdArgs.push_back("-D_DEBUG");
+        CmdArgs.push_back("-D_MT");
+        CmdArgs.push_back("-D_DLL");
+        CmdArgs.push_back("--dependent-lib=msvcrtd");
+      } else if (Val == "static") {
+        CmdArgs.push_back("-D_MT");
+        CmdArgs.push_back("-flto-visibility-public-std");
+        CmdArgs.push_back("--dependent-lib=libcmt");
+      } else if (Val == "static_dbg") {
+        CmdArgs.push_back("-D_DEBUG");
+        CmdArgs.push_back("-D_MT");
+        CmdArgs.push_back("-flto-visibility-public-std");
+        CmdArgs.push_back("--dependent-lib=libcmtd");
+      } else {
+        D.Diag(diag::err_drv_invalid_value) << A->getSpelling() << Val;
+      }
+      CmdArgs.push_back("--dependent-lib=oldnames");
+    }
+  }
+
   // Handle -fgcc-version, if present.
   VersionTuple GNUCVer;
   if (Arg *A = Args.getLastArg(options::OPT_fgnuc_version_EQ)) {
Index: clang/include/clang/Driver/Options.td
===================================================================
--- clang/include/clang/Driver/Options.td
+++ clang/include/clang/Driver/Options.td
@@ -2205,6 +2205,8 @@
       HelpText<"Dot-separated value representing the Microsoft compiler "
                "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<[NoXarchOption, CoreOption]>, HelpText<"Select Windows run-time library">;
 defm delayed_template_parsing : BoolFOption<"delayed-template-parsing",
   LangOpts<"DelayedTemplateParsing">, DefaultFalse,
   PosFlag<SetTrue, [CC1Option], "Parse templated function definitions at the end of the translation unit">,


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D133457.458574.patch
Type: text/x-patch
Size: 4328 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220907/a1de4f60/attachment.bin>


More information about the cfe-commits mailing list