[PATCH] D118070: Make lld-link work in a non-MSVC shell

2022-02-04 Thread Peter Kasting via Phabricator via cfe-commits
pkasting marked an inline comment as done.
pkasting added a comment.

In D118070#3289222 , @ychen wrote:

> Thanks for doing this! Update the release note?

Done.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D118070/new/

https://reviews.llvm.org/D118070

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D118070: Make lld-link work in a non-MSVC shell

2022-02-04 Thread Peter Kasting via Phabricator via cfe-commits
pkasting added a comment.

In D118070#3297956 , @thakis wrote:

> I think the setup matches what we have in clang, right?

Yes, I think so.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D118070/new/

https://reviews.llvm.org/D118070

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D118070: Make lld-link work in a non-MSVC shell

2022-02-04 Thread Peter Kasting via Phabricator via cfe-commits
pkasting updated this revision to Diff 406101.
Herald added a reviewer: MaskRay.

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D118070/new/

https://reviews.llvm.org/D118070

Files:
  clang/docs/tools/clang-formatted-files.txt
  clang/lib/Driver/ToolChains/MSVC.cpp
  clang/lib/Driver/ToolChains/MSVC.h
  clang/lib/Driver/ToolChains/MSVCSetupApi.h
  lld/COFF/Driver.cpp
  lld/COFF/Driver.h
  lld/COFF/Options.td
  lld/COFF/SymbolTable.cpp
  lld/docs/ReleaseNotes.rst
  lld/test/COFF/winsysroot.test
  llvm/include/llvm/Support/MSVCPaths.h
  llvm/include/llvm/Support/MSVCSetupApi.h
  llvm/lib/Support/CMakeLists.txt
  llvm/lib/Support/MSVCPaths.cpp
  llvm/utils/gn/secondary/llvm/lib/Support/BUILD.gn
  utils/bazel/llvm-project-overlay/clang/BUILD.bazel

Index: utils/bazel/llvm-project-overlay/clang/BUILD.bazel
===
--- utils/bazel/llvm-project-overlay/clang/BUILD.bazel
+++ utils/bazel/llvm-project-overlay/clang/BUILD.bazel
@@ -1309,9 +1309,6 @@
 "lib/Driver/ToolChains/Arch/*.cpp",
 "lib/Driver/ToolChains/Arch/*.h",
 ],
-exclude = [
-"lib/Driver/ToolChains/MSVCSetupApi.h",
-],
 ),
 hdrs = glob([
 "include/clang/Driver/*.h",
Index: llvm/utils/gn/secondary/llvm/lib/Support/BUILD.gn
===
--- llvm/utils/gn/secondary/llvm/lib/Support/BUILD.gn
+++ llvm/utils/gn/secondary/llvm/lib/Support/BUILD.gn
@@ -98,6 +98,7 @@
 "MD5.cpp",
 "MSP430AttributeParser.cpp",
 "MSP430Attributes.cpp",
+"MSVCPaths.cpp",
 "ManagedStatic.cpp",
 "MathExtras.cpp",
 "MemAlloc.cpp",
Index: llvm/lib/Support/MSVCPaths.cpp
===
--- /dev/null
+++ llvm/lib/Support/MSVCPaths.cpp
@@ -0,0 +1,707 @@
+//===-- MSVCPaths.cpp - MSVC path-parsing helpers -===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "llvm/Support/MSVCPaths.h"
+#include "llvm/ADT/Optional.h"
+#include "llvm/ADT/SmallString.h"
+#include "llvm/ADT/SmallVector.h"
+#include "llvm/ADT/StringExtras.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/ADT/Triple.h"
+#include "llvm/ADT/Twine.h"
+#include "llvm/Option/Arg.h"
+#include "llvm/Option/ArgList.h"
+#include "llvm/Support/ConvertUTF.h"
+#include "llvm/Support/Host.h"
+#include "llvm/Support/Path.h"
+#include "llvm/Support/Process.h"
+#include "llvm/Support/Program.h"
+#include "llvm/Support/VersionTuple.h"
+#include "llvm/Support/VirtualFileSystem.h"
+#include 
+
+#ifdef _WIN32
+  #define WIN32_LEAN_AND_MEAN
+  #define NOGDI
+  #ifndef NOMINMAX
+#define NOMINMAX
+  #endif
+  #include 
+#endif
+
+#ifdef _MSC_VER
+// Don't support SetupApi on MinGW.
+#define USE_MSVC_SETUP_API
+
+// Make sure this comes before MSVCSetupApi.h
+#include 
+
+#include "llvm/Support/COM.h"
+#ifdef __clang__
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wnon-virtual-dtor"
+#endif
+#include "llvm/Support/MSVCSetupApi.h"
+#ifdef __clang__
+#pragma clang diagnostic pop
+#endif
+_COM_SMARTPTR_TYPEDEF(ISetupConfiguration, __uuidof(ISetupConfiguration));
+_COM_SMARTPTR_TYPEDEF(ISetupConfiguration2, __uuidof(ISetupConfiguration2));
+_COM_SMARTPTR_TYPEDEF(ISetupHelper, __uuidof(ISetupHelper));
+_COM_SMARTPTR_TYPEDEF(IEnumSetupInstances, __uuidof(IEnumSetupInstances));
+_COM_SMARTPTR_TYPEDEF(ISetupInstance, __uuidof(ISetupInstance));
+_COM_SMARTPTR_TYPEDEF(ISetupInstance2, __uuidof(ISetupInstance2));
+#endif
+
+static std::string
+getHighestNumericTupleInDirectory(llvm::vfs::FileSystem &VFS,
+  llvm::StringRef Directory) {
+  std::string Highest;
+  llvm::VersionTuple HighestTuple;
+
+  std::error_code EC;
+  for (llvm::vfs::directory_iterator DirIt = VFS.dir_begin(Directory, EC),
+ DirEnd;
+   !EC && DirIt != DirEnd; DirIt.increment(EC)) {
+auto Status = VFS.status(DirIt->path());
+if (!Status || !Status->isDirectory())
+  continue;
+llvm::StringRef CandidateName = llvm::sys::path::filename(DirIt->path());
+llvm::VersionTuple Tuple;
+if (Tuple.tryParse(CandidateName)) // tryParse() returns true on error.
+  continue;
+if (Tuple > HighestTuple) {
+  HighestTuple = Tuple;
+  Highest = CandidateName.str();
+}
+  }
+
+  return Highest;
+}
+
+static bool getWindows10SDKVersionFromPath(llvm::vfs::FileSystem &VFS,
+   const std::string &SDKPath,
+   std::string &SDKVersion) {
+  llvm::SmallString<128> IncludePath(SDKPath);
+  llvm::sys::path::append(In

[PATCH] D118070: Make lld-link work in a non-MSVC shell

2022-02-07 Thread Peter Kasting via Phabricator via cfe-commits
pkasting updated this revision to Diff 406526.
pkasting added a comment.

This fixes a failing test.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D118070/new/

https://reviews.llvm.org/D118070

Files:
  clang/docs/tools/clang-formatted-files.txt
  clang/lib/Driver/ToolChains/MSVC.cpp
  clang/lib/Driver/ToolChains/MSVC.h
  clang/lib/Driver/ToolChains/MSVCSetupApi.h
  lld/COFF/Driver.cpp
  lld/COFF/Driver.h
  lld/COFF/Options.td
  lld/COFF/SymbolTable.cpp
  lld/docs/ReleaseNotes.rst
  lld/test/COFF/winsysroot.test
  llvm/include/llvm/Support/MSVCPaths.h
  llvm/include/llvm/Support/MSVCSetupApi.h
  llvm/lib/Support/CMakeLists.txt
  llvm/lib/Support/MSVCPaths.cpp
  llvm/utils/gn/secondary/llvm/lib/Support/BUILD.gn
  utils/bazel/llvm-project-overlay/clang/BUILD.bazel

Index: utils/bazel/llvm-project-overlay/clang/BUILD.bazel
===
--- utils/bazel/llvm-project-overlay/clang/BUILD.bazel
+++ utils/bazel/llvm-project-overlay/clang/BUILD.bazel
@@ -1309,9 +1309,6 @@
 "lib/Driver/ToolChains/Arch/*.cpp",
 "lib/Driver/ToolChains/Arch/*.h",
 ],
-exclude = [
-"lib/Driver/ToolChains/MSVCSetupApi.h",
-],
 ),
 hdrs = glob([
 "include/clang/Driver/*.h",
Index: llvm/utils/gn/secondary/llvm/lib/Support/BUILD.gn
===
--- llvm/utils/gn/secondary/llvm/lib/Support/BUILD.gn
+++ llvm/utils/gn/secondary/llvm/lib/Support/BUILD.gn
@@ -98,6 +98,7 @@
 "MD5.cpp",
 "MSP430AttributeParser.cpp",
 "MSP430Attributes.cpp",
+"MSVCPaths.cpp",
 "ManagedStatic.cpp",
 "MathExtras.cpp",
 "MemAlloc.cpp",
Index: llvm/lib/Support/MSVCPaths.cpp
===
--- /dev/null
+++ llvm/lib/Support/MSVCPaths.cpp
@@ -0,0 +1,707 @@
+//===-- MSVCPaths.cpp - MSVC path-parsing helpers -===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "llvm/Support/MSVCPaths.h"
+#include "llvm/ADT/Optional.h"
+#include "llvm/ADT/SmallString.h"
+#include "llvm/ADT/SmallVector.h"
+#include "llvm/ADT/StringExtras.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/ADT/Triple.h"
+#include "llvm/ADT/Twine.h"
+#include "llvm/Option/Arg.h"
+#include "llvm/Option/ArgList.h"
+#include "llvm/Support/ConvertUTF.h"
+#include "llvm/Support/Host.h"
+#include "llvm/Support/Path.h"
+#include "llvm/Support/Process.h"
+#include "llvm/Support/Program.h"
+#include "llvm/Support/VersionTuple.h"
+#include "llvm/Support/VirtualFileSystem.h"
+#include 
+
+#ifdef _WIN32
+  #define WIN32_LEAN_AND_MEAN
+  #define NOGDI
+  #ifndef NOMINMAX
+#define NOMINMAX
+  #endif
+  #include 
+#endif
+
+#ifdef _MSC_VER
+// Don't support SetupApi on MinGW.
+#define USE_MSVC_SETUP_API
+
+// Make sure this comes before MSVCSetupApi.h
+#include 
+
+#include "llvm/Support/COM.h"
+#ifdef __clang__
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wnon-virtual-dtor"
+#endif
+#include "llvm/Support/MSVCSetupApi.h"
+#ifdef __clang__
+#pragma clang diagnostic pop
+#endif
+_COM_SMARTPTR_TYPEDEF(ISetupConfiguration, __uuidof(ISetupConfiguration));
+_COM_SMARTPTR_TYPEDEF(ISetupConfiguration2, __uuidof(ISetupConfiguration2));
+_COM_SMARTPTR_TYPEDEF(ISetupHelper, __uuidof(ISetupHelper));
+_COM_SMARTPTR_TYPEDEF(IEnumSetupInstances, __uuidof(IEnumSetupInstances));
+_COM_SMARTPTR_TYPEDEF(ISetupInstance, __uuidof(ISetupInstance));
+_COM_SMARTPTR_TYPEDEF(ISetupInstance2, __uuidof(ISetupInstance2));
+#endif
+
+static std::string
+getHighestNumericTupleInDirectory(llvm::vfs::FileSystem &VFS,
+  llvm::StringRef Directory) {
+  std::string Highest;
+  llvm::VersionTuple HighestTuple;
+
+  std::error_code EC;
+  for (llvm::vfs::directory_iterator DirIt = VFS.dir_begin(Directory, EC),
+ DirEnd;
+   !EC && DirIt != DirEnd; DirIt.increment(EC)) {
+auto Status = VFS.status(DirIt->path());
+if (!Status || !Status->isDirectory())
+  continue;
+llvm::StringRef CandidateName = llvm::sys::path::filename(DirIt->path());
+llvm::VersionTuple Tuple;
+if (Tuple.tryParse(CandidateName)) // tryParse() returns true on error.
+  continue;
+if (Tuple > HighestTuple) {
+  HighestTuple = Tuple;
+  Highest = CandidateName.str();
+}
+  }
+
+  return Highest;
+}
+
+static bool getWindows10SDKVersionFromPath(llvm::vfs::FileSystem &VFS,
+   const std::string &SDKPath,
+   std::string &SDKVersion) {
+  llvm::SmallString<128> IncludePath(SDKPath);
+  llvm:

[PATCH] D118070: Make lld-link work in a non-MSVC shell

2022-02-07 Thread Peter Kasting via Phabricator via cfe-commits
pkasting updated this revision to Diff 406570.
pkasting marked 2 inline comments as done.
pkasting added a comment.

Review comments.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D118070/new/

https://reviews.llvm.org/D118070

Files:
  clang/docs/tools/clang-formatted-files.txt
  clang/lib/Driver/ToolChains/MSVC.cpp
  clang/lib/Driver/ToolChains/MSVC.h
  clang/lib/Driver/ToolChains/MSVCSetupApi.h
  lld/COFF/Driver.cpp
  lld/COFF/Driver.h
  lld/COFF/Options.td
  lld/COFF/SymbolTable.cpp
  lld/docs/ReleaseNotes.rst
  lld/test/COFF/winsysroot.test
  llvm/include/llvm/Support/MSVCPaths.h
  llvm/include/llvm/Support/MSVCSetupApi.h
  llvm/lib/Support/CMakeLists.txt
  llvm/lib/Support/MSVCPaths.cpp
  llvm/utils/gn/secondary/llvm/lib/Support/BUILD.gn
  utils/bazel/llvm-project-overlay/clang/BUILD.bazel

Index: utils/bazel/llvm-project-overlay/clang/BUILD.bazel
===
--- utils/bazel/llvm-project-overlay/clang/BUILD.bazel
+++ utils/bazel/llvm-project-overlay/clang/BUILD.bazel
@@ -1309,9 +1309,6 @@
 "lib/Driver/ToolChains/Arch/*.cpp",
 "lib/Driver/ToolChains/Arch/*.h",
 ],
-exclude = [
-"lib/Driver/ToolChains/MSVCSetupApi.h",
-],
 ),
 hdrs = glob([
 "include/clang/Driver/*.h",
Index: llvm/utils/gn/secondary/llvm/lib/Support/BUILD.gn
===
--- llvm/utils/gn/secondary/llvm/lib/Support/BUILD.gn
+++ llvm/utils/gn/secondary/llvm/lib/Support/BUILD.gn
@@ -98,6 +98,7 @@
 "MD5.cpp",
 "MSP430AttributeParser.cpp",
 "MSP430Attributes.cpp",
+"MSVCPaths.cpp",
 "ManagedStatic.cpp",
 "MathExtras.cpp",
 "MemAlloc.cpp",
Index: llvm/lib/Support/MSVCPaths.cpp
===
--- /dev/null
+++ llvm/lib/Support/MSVCPaths.cpp
@@ -0,0 +1,709 @@
+//===-- MSVCPaths.cpp - MSVC path-parsing helpers -===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "llvm/Support/MSVCPaths.h"
+#include "llvm/ADT/Optional.h"
+#include "llvm/ADT/SmallString.h"
+#include "llvm/ADT/SmallVector.h"
+#include "llvm/ADT/StringExtras.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/ADT/Triple.h"
+#include "llvm/ADT/Twine.h"
+#include "llvm/Option/Arg.h"
+#include "llvm/Option/ArgList.h"
+#include "llvm/Support/ConvertUTF.h"
+#include "llvm/Support/Host.h"
+#include "llvm/Support/Path.h"
+#include "llvm/Support/Process.h"
+#include "llvm/Support/Program.h"
+#include "llvm/Support/VersionTuple.h"
+#include "llvm/Support/VirtualFileSystem.h"
+#include 
+
+#ifdef _WIN32
+#define WIN32_LEAN_AND_MEAN
+#define NOGDI
+#ifndef NOMINMAX
+#define NOMINMAX
+#endif
+#include 
+#endif
+
+#ifdef _MSC_VER
+// Don't support SetupApi on MinGW.
+#define USE_MSVC_SETUP_API
+
+// Make sure this comes before MSVCSetupApi.h
+#include 
+
+#include "llvm/Support/COM.h"
+#ifdef __clang__
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wnon-virtual-dtor"
+#endif
+#include "llvm/Support/MSVCSetupApi.h"
+#ifdef __clang__
+#pragma clang diagnostic pop
+#endif
+_COM_SMARTPTR_TYPEDEF(ISetupConfiguration, __uuidof(ISetupConfiguration));
+_COM_SMARTPTR_TYPEDEF(ISetupConfiguration2, __uuidof(ISetupConfiguration2));
+_COM_SMARTPTR_TYPEDEF(ISetupHelper, __uuidof(ISetupHelper));
+_COM_SMARTPTR_TYPEDEF(IEnumSetupInstances, __uuidof(IEnumSetupInstances));
+_COM_SMARTPTR_TYPEDEF(ISetupInstance, __uuidof(ISetupInstance));
+_COM_SMARTPTR_TYPEDEF(ISetupInstance2, __uuidof(ISetupInstance2));
+#endif
+
+static std::string
+getHighestNumericTupleInDirectory(llvm::vfs::FileSystem &VFS,
+  llvm::StringRef Directory) {
+  std::string Highest;
+  llvm::VersionTuple HighestTuple;
+
+  std::error_code EC;
+  for (llvm::vfs::directory_iterator DirIt = VFS.dir_begin(Directory, EC),
+ DirEnd;
+   !EC && DirIt != DirEnd; DirIt.increment(EC)) {
+auto Status = VFS.status(DirIt->path());
+if (!Status || !Status->isDirectory())
+  continue;
+llvm::StringRef CandidateName = llvm::sys::path::filename(DirIt->path());
+llvm::VersionTuple Tuple;
+if (Tuple.tryParse(CandidateName)) // tryParse() returns true on error.
+  continue;
+if (Tuple > HighestTuple) {
+  HighestTuple = Tuple;
+  Highest = CandidateName.str();
+}
+  }
+
+  return Highest;
+}
+
+static bool getWindows10SDKVersionFromPath(llvm::vfs::FileSystem &VFS,
+   const std::string &SDKPath,
+   std::string &SDKVersion) {
+  llvm::SmallString<128> IncludePath

[PATCH] D118070: Make lld-link work in a non-MSVC shell

2022-02-09 Thread Peter Kasting via Phabricator via cfe-commits
pkasting added a comment.

MaskRay: Friendly ping.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D118070/new/

https://reviews.llvm.org/D118070

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D118070: Make lld-link work in a non-MSVC shell

2022-02-15 Thread Peter Kasting via Phabricator via cfe-commits
pkasting updated this revision to Diff 409046.
pkasting added a comment.

Fix test failure.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D118070/new/

https://reviews.llvm.org/D118070

Files:
  clang/docs/tools/clang-formatted-files.txt
  clang/lib/Driver/CMakeLists.txt
  clang/lib/Driver/ToolChains/MSVC.cpp
  clang/lib/Driver/ToolChains/MSVC.h
  clang/lib/Driver/ToolChains/MSVCSetupApi.h
  lld/COFF/CMakeLists.txt
  lld/COFF/Driver.cpp
  lld/COFF/Driver.h
  lld/COFF/Options.td
  lld/COFF/SymbolTable.cpp
  lld/docs/ReleaseNotes.rst
  lld/test/COFF/winsysroot.test
  llvm/include/llvm/WindowsDriver/MSVCPaths.h
  llvm/include/llvm/WindowsDriver/MSVCSetupApi.h
  llvm/lib/CMakeLists.txt
  llvm/lib/WindowsDriver/CMakeLists.txt
  llvm/lib/WindowsDriver/MSVCPaths.cpp
  llvm/utils/gn/secondary/clang/lib/Driver/BUILD.gn
  llvm/utils/gn/secondary/lld/COFF/BUILD.gn
  llvm/utils/gn/secondary/llvm/lib/WindowsDriver/BUILD.gn

Index: llvm/utils/gn/secondary/llvm/lib/WindowsDriver/BUILD.gn
===
--- /dev/null
+++ llvm/utils/gn/secondary/llvm/lib/WindowsDriver/BUILD.gn
@@ -0,0 +1,8 @@
+static_library("WindowsDriver") {
+  output_name = "LLVMWindowsDriver"
+  deps = [
+"//llvm/lib/Option",
+"//llvm/lib/Support",
+  ]
+  sources = [ "MSVCPaths.cpp" ]
+}
Index: llvm/utils/gn/secondary/lld/COFF/BUILD.gn
===
--- llvm/utils/gn/secondary/lld/COFF/BUILD.gn
+++ llvm/utils/gn/secondary/lld/COFF/BUILD.gn
@@ -23,6 +23,7 @@
 "//llvm/lib/Support",
 "//llvm/lib/Target:TargetsToBuild",
 "//llvm/lib/ToolDrivers/llvm-lib:LibDriver",
+"//llvm/lib/WindowsDriver",
 "//llvm/lib/WindowsManifest",
   ]
   sources = [
Index: llvm/utils/gn/secondary/clang/lib/Driver/BUILD.gn
===
--- llvm/utils/gn/secondary/clang/lib/Driver/BUILD.gn
+++ llvm/utils/gn/secondary/clang/lib/Driver/BUILD.gn
@@ -18,6 +18,7 @@
 "//llvm/lib/BinaryFormat",
 "//llvm/lib/Option",
 "//llvm/lib/Support",
+"//llvm/lib/WindowsDriver",
   ]
   public_deps = [
 # public_dep because public header Options.h includes generated Options.inc.
Index: llvm/lib/WindowsDriver/MSVCPaths.cpp
===
--- /dev/null
+++ llvm/lib/WindowsDriver/MSVCPaths.cpp
@@ -0,0 +1,710 @@
+//===-- MSVCPaths.cpp - MSVC path-parsing helpers -===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "llvm/WindowsDriver/MSVCPaths.h"
+#include "llvm/ADT/Optional.h"
+#include "llvm/ADT/SmallString.h"
+#include "llvm/ADT/SmallVector.h"
+#include "llvm/ADT/StringExtras.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/ADT/Triple.h"
+#include "llvm/ADT/Twine.h"
+#include "llvm/Option/Arg.h"
+#include "llvm/Option/ArgList.h"
+#include "llvm/Support/ConvertUTF.h"
+#include "llvm/Support/Host.h"
+#include "llvm/Support/Path.h"
+#include "llvm/Support/Process.h"
+#include "llvm/Support/Program.h"
+#include "llvm/Support/VersionTuple.h"
+#include "llvm/Support/VirtualFileSystem.h"
+#include 
+
+#ifdef _WIN32
+#define WIN32_LEAN_AND_MEAN
+#define NOGDI
+#ifndef NOMINMAX
+#define NOMINMAX
+#endif
+#include 
+#endif
+
+#ifdef _MSC_VER
+// Don't support SetupApi on MinGW.
+#define USE_MSVC_SETUP_API
+
+// Make sure this comes before MSVCSetupApi.h
+#include 
+
+#include "llvm/Support/COM.h"
+#ifdef __clang__
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wnon-virtual-dtor"
+#endif
+#include "llvm/WindowsDriver/MSVCSetupApi.h"
+#ifdef __clang__
+#pragma clang diagnostic pop
+#endif
+_COM_SMARTPTR_TYPEDEF(ISetupConfiguration, __uuidof(ISetupConfiguration));
+_COM_SMARTPTR_TYPEDEF(ISetupConfiguration2, __uuidof(ISetupConfiguration2));
+_COM_SMARTPTR_TYPEDEF(ISetupHelper, __uuidof(ISetupHelper));
+_COM_SMARTPTR_TYPEDEF(IEnumSetupInstances, __uuidof(IEnumSetupInstances));
+_COM_SMARTPTR_TYPEDEF(ISetupInstance, __uuidof(ISetupInstance));
+_COM_SMARTPTR_TYPEDEF(ISetupInstance2, __uuidof(ISetupInstance2));
+#endif
+
+static std::string
+getHighestNumericTupleInDirectory(llvm::vfs::FileSystem &VFS,
+  llvm::StringRef Directory) {
+  std::string Highest;
+  llvm::VersionTuple HighestTuple;
+
+  std::error_code EC;
+  for (llvm::vfs::directory_iterator DirIt = VFS.dir_begin(Directory, EC),
+ DirEnd;
+   !EC && DirIt != DirEnd; DirIt.increment(EC)) {
+auto Status = VFS.status(DirIt->path());
+if (!Status || !Status->isDirectory())
+  continue;
+llvm::StringRef CandidateName = llvm::sys::path::filename(DirIt->path());
+

[PATCH] D149713: [Sema] Avoid emitting warnings for constant destruction.

2023-05-02 Thread Peter Kasting via Phabricator via cfe-commits
pkasting created this revision.
pkasting added a reviewer: rsmith.
Herald added a project: All.
pkasting requested review of this revision.
Herald added a project: clang.

Fixes https://github.com/llvm/llvm-project/issues/62436.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D149713

Files:
  clang/lib/Sema/SemaDeclCXX.cpp
  clang/test/SemaCXX/warn-exit-time-destructors.cpp
  clang/test/SemaCXX/warn-global-constructors.cpp


Index: clang/test/SemaCXX/warn-global-constructors.cpp
===
--- clang/test/SemaCXX/warn-global-constructors.cpp
+++ clang/test/SemaCXX/warn-global-constructors.cpp
@@ -1,4 +1,5 @@
-// RUN: %clang_cc1 -std=c++11 -fsyntax-only -Wglobal-constructors %s -verify
+// RUN: %clang_cc1 -std=c++11 -fsyntax-only -Wglobal-constructors %s 
-verify=expected,cxx11
+// RUN: %clang_cc1 -std=c++20 -fsyntax-only -Wglobal-constructors %s 
-verify=expected
 
 int opaque_int();
 
@@ -145,3 +146,16 @@
   const HasUnnamedBitfield nonConstexprConst{1}; // expected-warning {{global 
constructor}}
   HasUnnamedBitfield nonConstexprMutable{1}; // expected-warning {{global 
constructor}}
 }
+
+namespace test7 {
+#if __cplusplus >= 202002L
+#define CPP20_CONSTEXPR constexpr
+#else
+#define CPP20_CONSTEXPR
+#endif
+  struct S {
+CPP20_CONSTEXPR ~S() {}
+  };
+  CPP20_CONSTEXPR S s; // cxx11-warning {{global constructor}}
+#undef CPP20_CONSTEXPR
+}
Index: clang/test/SemaCXX/warn-exit-time-destructors.cpp
===
--- clang/test/SemaCXX/warn-exit-time-destructors.cpp
+++ clang/test/SemaCXX/warn-exit-time-destructors.cpp
@@ -1,4 +1,5 @@
-// RUN: %clang_cc1 -std=c++11 -fsyntax-only -Wexit-time-destructors %s -verify
+// RUN: %clang_cc1 -std=c++11 -fsyntax-only -Wexit-time-destructors %s 
-verify=expected,cxx11
+// RUN: %clang_cc1 -std=c++20 -fsyntax-only -Wexit-time-destructors %s 
-verify=expected
 
 namespace test1 {
   struct A { ~A(); };
@@ -48,3 +49,16 @@
 struct A { ~A(); };
 [[clang::no_destroy]] A a; // no warning
 }
+
+namespace test5 {
+#if __cplusplus >= 202002L
+#define CPP20_CONSTEXPR constexpr
+#else
+#define CPP20_CONSTEXPR
+#endif
+  struct S {
+CPP20_CONSTEXPR ~S() {}
+  };
+  CPP20_CONSTEXPR S s; // cxx11-warning {{exit-time destructor}}
+#undef CPP20_CONSTEXPR
+}
Index: clang/lib/Sema/SemaDeclCXX.cpp
===
--- clang/lib/Sema/SemaDeclCXX.cpp
+++ clang/lib/Sema/SemaDeclCXX.cpp
@@ -15718,7 +15718,8 @@
 }
   }
 
-  if (!VD->hasGlobalStorage()) return;
+  if (!VD->hasGlobalStorage() || !VD->needsDestruction(Context))
+return;
 
   // Emit warning for non-trivial dtor in global scope (a real global,
   // class-static, function-static).


Index: clang/test/SemaCXX/warn-global-constructors.cpp
===
--- clang/test/SemaCXX/warn-global-constructors.cpp
+++ clang/test/SemaCXX/warn-global-constructors.cpp
@@ -1,4 +1,5 @@
-// RUN: %clang_cc1 -std=c++11 -fsyntax-only -Wglobal-constructors %s -verify
+// RUN: %clang_cc1 -std=c++11 -fsyntax-only -Wglobal-constructors %s -verify=expected,cxx11
+// RUN: %clang_cc1 -std=c++20 -fsyntax-only -Wglobal-constructors %s -verify=expected
 
 int opaque_int();
 
@@ -145,3 +146,16 @@
   const HasUnnamedBitfield nonConstexprConst{1}; // expected-warning {{global constructor}}
   HasUnnamedBitfield nonConstexprMutable{1}; // expected-warning {{global constructor}}
 }
+
+namespace test7 {
+#if __cplusplus >= 202002L
+#define CPP20_CONSTEXPR constexpr
+#else
+#define CPP20_CONSTEXPR
+#endif
+  struct S {
+CPP20_CONSTEXPR ~S() {}
+  };
+  CPP20_CONSTEXPR S s; // cxx11-warning {{global constructor}}
+#undef CPP20_CONSTEXPR
+}
Index: clang/test/SemaCXX/warn-exit-time-destructors.cpp
===
--- clang/test/SemaCXX/warn-exit-time-destructors.cpp
+++ clang/test/SemaCXX/warn-exit-time-destructors.cpp
@@ -1,4 +1,5 @@
-// RUN: %clang_cc1 -std=c++11 -fsyntax-only -Wexit-time-destructors %s -verify
+// RUN: %clang_cc1 -std=c++11 -fsyntax-only -Wexit-time-destructors %s -verify=expected,cxx11
+// RUN: %clang_cc1 -std=c++20 -fsyntax-only -Wexit-time-destructors %s -verify=expected
 
 namespace test1 {
   struct A { ~A(); };
@@ -48,3 +49,16 @@
 struct A { ~A(); };
 [[clang::no_destroy]] A a; // no warning
 }
+
+namespace test5 {
+#if __cplusplus >= 202002L
+#define CPP20_CONSTEXPR constexpr
+#else
+#define CPP20_CONSTEXPR
+#endif
+  struct S {
+CPP20_CONSTEXPR ~S() {}
+  };
+  CPP20_CONSTEXPR S s; // cxx11-warning {{exit-time destructor}}
+#undef CPP20_CONSTEXPR
+}
Index: clang/lib/Sema/SemaDeclCXX.cpp
===
--- clang/lib/Sema/SemaDeclCXX.cpp
+++ clang/lib/Sema/SemaDeclCXX.cpp
@@ -15718,7 +15718,8 @@
 }
   }
 
-  if (!VD->hasGlobalStorage()) return;
+  if (!VD->hasGlobalStorage() || !VD->n

[PATCH] D149713: [Sema] Avoid emitting warnings for constant destruction.

2023-05-03 Thread Peter Kasting via Phabricator via cfe-commits
pkasting updated this revision to Diff 519188.
pkasting added a comment.

Update test per review comments.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D149713/new/

https://reviews.llvm.org/D149713

Files:
  clang/lib/Sema/SemaDeclCXX.cpp
  clang/test/SemaCXX/warn-exit-time-destructors.cpp
  clang/test/SemaCXX/warn-global-constructors.cpp


Index: clang/test/SemaCXX/warn-global-constructors.cpp
===
--- clang/test/SemaCXX/warn-global-constructors.cpp
+++ clang/test/SemaCXX/warn-global-constructors.cpp
@@ -1,4 +1,5 @@
-// RUN: %clang_cc1 -std=c++11 -fsyntax-only -Wglobal-constructors %s -verify
+// RUN: %clang_cc1 -std=c++11 -fsyntax-only -Wglobal-constructors %s 
-verify=expected,cxx11
+// RUN: %clang_cc1 -std=c++20 -fsyntax-only -Wglobal-constructors %s 
-verify=expected
 
 int opaque_int();
 
@@ -145,3 +146,22 @@
   const HasUnnamedBitfield nonConstexprConst{1}; // expected-warning {{global 
constructor}}
   HasUnnamedBitfield nonConstexprMutable{1}; // expected-warning {{global 
constructor}}
 }
+
+namespace test7 {
+#if __cplusplus >= 202002L
+#define CPP20_CONSTEXPR constexpr
+#else
+#define CPP20_CONSTEXPR
+#endif
+  struct S {
+CPP20_CONSTEXPR ~S() {}
+  };
+  S s; // cxx11-warning {{global destructor}}
+
+  struct T {
+CPP20_CONSTEXPR ~T() { if (b) {} }
+bool b;
+  };
+  T t; // expected-warning {{global destructor}}
+#undef CPP20_CONSTEXPR
+}
Index: clang/test/SemaCXX/warn-exit-time-destructors.cpp
===
--- clang/test/SemaCXX/warn-exit-time-destructors.cpp
+++ clang/test/SemaCXX/warn-exit-time-destructors.cpp
@@ -1,4 +1,5 @@
-// RUN: %clang_cc1 -std=c++11 -fsyntax-only -Wexit-time-destructors %s -verify
+// RUN: %clang_cc1 -std=c++11 -fsyntax-only -Wexit-time-destructors %s 
-verify=expected,cxx11
+// RUN: %clang_cc1 -std=c++20 -fsyntax-only -Wexit-time-destructors %s 
-verify=expected
 
 namespace test1 {
   struct A { ~A(); };
@@ -48,3 +49,22 @@
 struct A { ~A(); };
 [[clang::no_destroy]] A a; // no warning
 }
+
+namespace test5 {
+#if __cplusplus >= 202002L
+#define CPP20_CONSTEXPR constexpr
+#else
+#define CPP20_CONSTEXPR
+#endif
+  struct S {
+CPP20_CONSTEXPR ~S() {}
+  };
+  S s; // cxx11-warning {{exit-time destructor}}
+
+  struct T {
+CPP20_CONSTEXPR ~T() { if (b) {} }
+bool b;
+  };
+  T t; // expected-warning {{exit-time destructor}}
+#undef CPP20_CONSTEXPR
+}
Index: clang/lib/Sema/SemaDeclCXX.cpp
===
--- clang/lib/Sema/SemaDeclCXX.cpp
+++ clang/lib/Sema/SemaDeclCXX.cpp
@@ -15718,7 +15718,8 @@
 }
   }
 
-  if (!VD->hasGlobalStorage()) return;
+  if (!VD->hasGlobalStorage() || !VD->needsDestruction(Context))
+return;
 
   // Emit warning for non-trivial dtor in global scope (a real global,
   // class-static, function-static).


Index: clang/test/SemaCXX/warn-global-constructors.cpp
===
--- clang/test/SemaCXX/warn-global-constructors.cpp
+++ clang/test/SemaCXX/warn-global-constructors.cpp
@@ -1,4 +1,5 @@
-// RUN: %clang_cc1 -std=c++11 -fsyntax-only -Wglobal-constructors %s -verify
+// RUN: %clang_cc1 -std=c++11 -fsyntax-only -Wglobal-constructors %s -verify=expected,cxx11
+// RUN: %clang_cc1 -std=c++20 -fsyntax-only -Wglobal-constructors %s -verify=expected
 
 int opaque_int();
 
@@ -145,3 +146,22 @@
   const HasUnnamedBitfield nonConstexprConst{1}; // expected-warning {{global constructor}}
   HasUnnamedBitfield nonConstexprMutable{1}; // expected-warning {{global constructor}}
 }
+
+namespace test7 {
+#if __cplusplus >= 202002L
+#define CPP20_CONSTEXPR constexpr
+#else
+#define CPP20_CONSTEXPR
+#endif
+  struct S {
+CPP20_CONSTEXPR ~S() {}
+  };
+  S s; // cxx11-warning {{global destructor}}
+
+  struct T {
+CPP20_CONSTEXPR ~T() { if (b) {} }
+bool b;
+  };
+  T t; // expected-warning {{global destructor}}
+#undef CPP20_CONSTEXPR
+}
Index: clang/test/SemaCXX/warn-exit-time-destructors.cpp
===
--- clang/test/SemaCXX/warn-exit-time-destructors.cpp
+++ clang/test/SemaCXX/warn-exit-time-destructors.cpp
@@ -1,4 +1,5 @@
-// RUN: %clang_cc1 -std=c++11 -fsyntax-only -Wexit-time-destructors %s -verify
+// RUN: %clang_cc1 -std=c++11 -fsyntax-only -Wexit-time-destructors %s -verify=expected,cxx11
+// RUN: %clang_cc1 -std=c++20 -fsyntax-only -Wexit-time-destructors %s -verify=expected
 
 namespace test1 {
   struct A { ~A(); };
@@ -48,3 +49,22 @@
 struct A { ~A(); };
 [[clang::no_destroy]] A a; // no warning
 }
+
+namespace test5 {
+#if __cplusplus >= 202002L
+#define CPP20_CONSTEXPR constexpr
+#else
+#define CPP20_CONSTEXPR
+#endif
+  struct S {
+CPP20_CONSTEXPR ~S() {}
+  };
+  S s; // cxx11-warning {{exit-time destructor}}
+
+  struct T {
+CPP20_CONSTEXPR ~T() { if (b) {} }
+boo

[PATCH] D149713: [Sema] Avoid emitting warnings for constant destruction.

2023-05-03 Thread Peter Kasting via Phabricator via cfe-commits
pkasting added a comment.

Thanks @rsmith; I don't have commit access, so can you land the updated patch 
for me?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D149713/new/

https://reviews.llvm.org/D149713

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D118070: Add /winsysroot support to lld

2022-01-26 Thread Peter Kasting via Phabricator via cfe-commits
pkasting updated this revision to Diff 403265.
pkasting added a comment.
Herald added subscribers: cfe-commits, dexonsmith, mstorsjo.
Herald added a project: clang.

Adds autodetection and full support for other clang-cl switches, but doesn't 
work: config->machine is not set this early, so it's always UNKNOWN.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D118070/new/

https://reviews.llvm.org/D118070

Files:
  clang/docs/tools/clang-formatted-files.txt
  clang/lib/Driver/ToolChains/MSVC.cpp
  clang/lib/Driver/ToolChains/MSVCSetupApi.h
  lld/COFF/Driver.cpp
  lld/COFF/Driver.h
  lld/COFF/Options.td
  llvm/include/llvm/Support/MSVCSetupApi.h
  utils/bazel/llvm-project-overlay/clang/BUILD.bazel

Index: utils/bazel/llvm-project-overlay/clang/BUILD.bazel
===
--- utils/bazel/llvm-project-overlay/clang/BUILD.bazel
+++ utils/bazel/llvm-project-overlay/clang/BUILD.bazel
@@ -1309,9 +1309,6 @@
 "lib/Driver/ToolChains/Arch/*.cpp",
 "lib/Driver/ToolChains/Arch/*.h",
 ],
-exclude = [
-"lib/Driver/ToolChains/MSVCSetupApi.h",
-],
 ),
 hdrs = glob([
 "include/clang/Driver/*.h",
Index: lld/COFF/Options.td
===
--- lld/COFF/Options.td
+++ lld/COFF/Options.td
@@ -41,6 +41,7 @@
 MetaVarName<"[auto,always,never]">;
 def defaultlib : P<"defaultlib", "Add the library to the list of input files">;
 def delayload : P<"delayload", "Delay loaded DLL name">;
+def diasdkdir : P<"diasdkdir", "Set the location of the DIA SDK">;
 def entry   : P<"entry", "Name of entry point symbol">;
 def errorlimit : P<"errorlimit",
 "Maximum number of errors to emit before stopping (0 = no limit)">;
@@ -89,9 +90,16 @@
 def stub: P<"stub", "Specify DOS stub file">;
 def subsystem : P<"subsystem", "Specify subsystem">;
 def timestamp : P<"timestamp", "Specify the PE header timestamp">;
+def vctoolsdir : P<"vctoolsdir", "Set the location of the VC tools">;
+def vctoolsversion : P<"vctoolsversion",
+"Specify which VC tools version to use">;
 def version : P<"version", "Specify a version number in the PE header">;
 def wholearchive_file : P<"wholearchive",
 "Include all object files from this library">;
+def winsdkdir : P<"winsdkdir", "Set the location of the Windows SDK">;
+def winsdkversion : P<"winsdkversion", "Specify which SDK version to use">;
+def winsysroot : P<"winsysroot",
+"Adds several subdirectories to the library search paths">;
 
 def disallowlib : Joined<["/", "-", "/?", "-?"], "disallowlib:">,
 Alias;
Index: lld/COFF/Driver.h
===
--- lld/COFF/Driver.h
+++ lld/COFF/Driver.h
@@ -107,6 +107,9 @@
 
   bool findUnderscoreMangle(StringRef sym);
 
+  // Adds appropriate search paths from `sysroot`.
+  void addWinSysRootLibSearchPaths(const llvm::opt::InputArgList &args);
+
   // Parses LIB environment which contains a list of search paths.
   void addLibSearchPaths();
 
Index: lld/COFF/Driver.cpp
===
--- lld/COFF/Driver.cpp
+++ lld/COFF/Driver.cpp
@@ -37,6 +37,7 @@
 #include "llvm/Support/BinaryStreamReader.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Debug.h"
+#include "llvm/Support/Host.h"
 #include "llvm/Support/LEB128.h"
 #include "llvm/Support/MathExtras.h"
 #include "llvm/Support/Parallel.h"
@@ -50,6 +51,44 @@
 #include 
 #include 
 
+#ifdef _WIN32
+#define WIN32_LEAN_AND_MEAN
+#define NOGDI
+#ifndef NOMINMAX
+#define NOMINMAX
+#endif
+#include 
+#endif
+
+#ifdef _MSC_VER
+// Don't support SetupApi on MinGW.
+#define USE_MSVC_SETUP_API
+
+// Make sure this comes before MSVCSetupApi.h
+#include 
+
+#ifdef __clang__
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wnon-virtual-dtor"
+#endif
+#include "llvm/Support/MSVCSetupApi.h"
+#ifdef __clang__
+#pragma clang diagnostic pop
+#endif
+#include "llvm/Support/COM.h"
+_COM_SMARTPTR_TYPEDEF(ISetupConfiguration, __uuidof(ISetupConfiguration));
+_COM_SMARTPTR_TYPEDEF(ISetupConfiguration2, __uuidof(ISetupConfiguration2));
+_COM_SMARTPTR_TYPEDEF(ISetupHelper, __uuidof(ISetupHelper));
+_COM_SMARTPTR_TYPEDEF(IEnumSetupInstances, __uuidof(IEnumSetupInstances));
+_COM_SMARTPTR_TYPEDEF(ISetupInstance, __uuidof(ISetupInstance));
+_COM_SMARTPTR_TYPEDEF(ISetupInstance2, __uuidof(ISetupInstance2));
+
+// TODO(pkasting): Remove
+#undef IMAGE_SUBSYSTEM_UNKNOWN
+#undef IMAGE_SUBSYSTEM_WINDOWS_GUI
+#undef IMAGE_SUBSYSTEM_WINDOWS_CUI
+#endif
+
 using namespace llvm;
 using namespace llvm::object;
 using namespace llvm::COFF;
@@ -102,7 +141,7 @@
   else if (config->driver)
 ext = ".sys";
 
-  return (sys::path::stem(path) + ext).str();
+  return (path::stem(path) + ext).str();
 }
 
 // Returns true if S matches /crtend.?\.o$/.
@@ -150,6 +189,704 @@
   return sym;
 }
 
+st

[PATCH] D118070: Add /winsysroot support to lld

2022-01-26 Thread Peter Kasting via Phabricator via cfe-commits
pkasting updated this revision to Diff 403316.
pkasting edited the summary of this revision.
pkasting added a comment.

Functional, but not yet refactored to share code with clang-cl.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D118070/new/

https://reviews.llvm.org/D118070

Files:
  clang/docs/tools/clang-formatted-files.txt
  clang/lib/Driver/ToolChains/MSVC.cpp
  clang/lib/Driver/ToolChains/MSVCSetupApi.h
  lld/COFF/Driver.cpp
  lld/COFF/Driver.h
  lld/COFF/Options.td
  lld/COFF/SymbolTable.cpp
  llvm/include/llvm/Support/MSVCSetupApi.h
  utils/bazel/llvm-project-overlay/clang/BUILD.bazel

Index: utils/bazel/llvm-project-overlay/clang/BUILD.bazel
===
--- utils/bazel/llvm-project-overlay/clang/BUILD.bazel
+++ utils/bazel/llvm-project-overlay/clang/BUILD.bazel
@@ -1309,9 +1309,6 @@
 "lib/Driver/ToolChains/Arch/*.cpp",
 "lib/Driver/ToolChains/Arch/*.h",
 ],
-exclude = [
-"lib/Driver/ToolChains/MSVCSetupApi.h",
-],
 ),
 hdrs = glob([
 "include/clang/Driver/*.h",
Index: lld/COFF/SymbolTable.cpp
===
--- lld/COFF/SymbolTable.cpp
+++ lld/COFF/SymbolTable.cpp
@@ -56,6 +56,7 @@
   MachineTypes mt = file->getMachineType();
   if (config->machine == IMAGE_FILE_MACHINE_UNKNOWN) {
 config->machine = mt;
+driver->addWinSysRootLibSearchPaths();
   } else if (mt != IMAGE_FILE_MACHINE_UNKNOWN && config->machine != mt) {
 error(toString(file) + ": machine type " + machineToStr(mt) +
   " conflicts with " + machineToStr(config->machine));
Index: lld/COFF/Options.td
===
--- lld/COFF/Options.td
+++ lld/COFF/Options.td
@@ -41,6 +41,7 @@
 MetaVarName<"[auto,always,never]">;
 def defaultlib : P<"defaultlib", "Add the library to the list of input files">;
 def delayload : P<"delayload", "Delay loaded DLL name">;
+def diasdkdir : P<"diasdkdir", "Set the location of the DIA SDK">;
 def entry   : P<"entry", "Name of entry point symbol">;
 def errorlimit : P<"errorlimit",
 "Maximum number of errors to emit before stopping (0 = no limit)">;
@@ -89,9 +90,16 @@
 def stub: P<"stub", "Specify DOS stub file">;
 def subsystem : P<"subsystem", "Specify subsystem">;
 def timestamp : P<"timestamp", "Specify the PE header timestamp">;
+def vctoolsdir : P<"vctoolsdir", "Set the location of the VC tools">;
+def vctoolsversion : P<"vctoolsversion",
+"Specify which VC tools version to use">;
 def version : P<"version", "Specify a version number in the PE header">;
 def wholearchive_file : P<"wholearchive",
 "Include all object files from this library">;
+def winsdkdir : P<"winsdkdir", "Set the location of the Windows SDK">;
+def winsdkversion : P<"winsdkversion", "Specify which SDK version to use">;
+def winsysroot : P<"winsysroot",
+"Adds several subdirectories to the library search paths">;
 
 def disallowlib : Joined<["/", "-", "/?", "-?"], "disallowlib:">,
 Alias;
Index: lld/COFF/Driver.h
===
--- lld/COFF/Driver.h
+++ lld/COFF/Driver.h
@@ -36,6 +36,12 @@
 using llvm::COFF::WindowsSubsystem;
 using llvm::Optional;
 
+enum class ToolsetLayout {
+  OlderVS,
+  VS2017OrNewer,
+  DevDivInternal,
+};
+
 class COFFOptTable : public llvm::opt::OptTable {
 public:
   COFFOptTable();
@@ -82,6 +88,10 @@
 
   void linkerMain(llvm::ArrayRef args);
 
+  // Adds various search paths based on the sysroot.  Must only be called once
+  // config->machine has been set.
+  void addWinSysRootLibSearchPaths();
+
   // Used by the resolver to parse .drectve section contents.
   void parseDirectives(InputFile *file);
 
@@ -107,11 +117,17 @@
 
   bool findUnderscoreMangle(StringRef sym);
 
+  // Determines the location of the sysroot based on `args`, environment, etc.
+  void detectWinSysRoot(const llvm::opt::InputArgList &args);
+
+  void getUniversalCRTLibraryPath(const llvm::opt::InputArgList &Args);
+  void getWindowsSDKLibraryPath(const llvm::opt::InputArgList &Args);
+
   // Parses LIB environment which contains a list of search paths.
   void addLibSearchPaths();
 
   // Library search path. The first element is always "" (current directory).
-  std::vector searchPaths;
+  std::vector searchPaths;
 
   // Convert resource files and potentially merge input resource object
   // trees into one resource tree.
@@ -154,6 +170,14 @@
   llvm::StringSet<> directivesExports;
 
   COFFLinkerContext &ctx;
+
+  ToolsetLayout vsLayout = ToolsetLayout::OlderVS;
+  std::string vcToolChainPath;
+  llvm::SmallString<128> diaPath;
+  bool useWinSysRootLibPath = false;
+  llvm::SmallString<128> universalCRTLibPath;
+  int sdkMajor = 0;
+  llvm::SmallString<128> windowsSdkLibPath;
 };
 
 // Functions below this line are defined in DriverUtils.cpp.
Index: lld/

[PATCH] D118070: Add /winsysroot support to lld

2022-01-27 Thread Peter Kasting via Phabricator via cfe-commits
pkasting updated this revision to Diff 403679.
pkasting added a comment.
Herald added subscribers: hiraditya, mgorny.

Refactored to share code with clang-cl.  Still no tests.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D118070/new/

https://reviews.llvm.org/D118070

Files:
  clang/docs/tools/clang-formatted-files.txt
  clang/lib/Driver/ToolChains/MSVC.cpp
  clang/lib/Driver/ToolChains/MSVC.h
  clang/lib/Driver/ToolChains/MSVCSetupApi.h
  lld/COFF/Driver.cpp
  lld/COFF/Driver.h
  lld/COFF/Options.td
  lld/COFF/SymbolTable.cpp
  llvm/include/llvm/Support/MSVCPaths.h
  llvm/include/llvm/Support/MSVCSetupApi.h
  llvm/lib/Support/CMakeLists.txt
  llvm/lib/Support/MSVCPaths.cpp
  llvm/utils/gn/secondary/llvm/lib/Support/BUILD.gn
  utils/bazel/llvm-project-overlay/clang/BUILD.bazel

Index: utils/bazel/llvm-project-overlay/clang/BUILD.bazel
===
--- utils/bazel/llvm-project-overlay/clang/BUILD.bazel
+++ utils/bazel/llvm-project-overlay/clang/BUILD.bazel
@@ -1309,9 +1309,6 @@
 "lib/Driver/ToolChains/Arch/*.cpp",
 "lib/Driver/ToolChains/Arch/*.h",
 ],
-exclude = [
-"lib/Driver/ToolChains/MSVCSetupApi.h",
-],
 ),
 hdrs = glob([
 "include/clang/Driver/*.h",
Index: llvm/utils/gn/secondary/llvm/lib/Support/BUILD.gn
===
--- llvm/utils/gn/secondary/llvm/lib/Support/BUILD.gn
+++ llvm/utils/gn/secondary/llvm/lib/Support/BUILD.gn
@@ -98,6 +98,7 @@
 "MD5.cpp",
 "MSP430AttributeParser.cpp",
 "MSP430Attributes.cpp",
+"MSVCPaths.cpp",
 "ManagedStatic.cpp",
 "MathExtras.cpp",
 "MemAlloc.cpp",
Index: llvm/lib/Support/MSVCPaths.cpp
===
--- /dev/null
+++ llvm/lib/Support/MSVCPaths.cpp
@@ -0,0 +1,589 @@
+//===-- MSVCPaths.cpp - MSVC path-parsing helpers -===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "llvm/Support/MSVCPaths.h"
+#include "llvm/ADT/Optional.h"
+#include "llvm/ADT/SmallString.h"
+#include "llvm/ADT/SmallVector.h"
+#include "llvm/ADT/StringExtras.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/ADT/Twine.h"
+#include "llvm/Option/Arg.h"
+#include "llvm/Option/ArgList.h"
+#include "llvm/Support/ConvertUTF.h"
+#include "llvm/Support/Path.h"
+#include "llvm/Support/Process.h"
+#include "llvm/Support/Program.h"
+#include "llvm/Support/VersionTuple.h"
+#include "llvm/Support/VirtualFileSystem.h"
+#include 
+
+#ifdef _WIN32
+#define WIN32_LEAN_AND_MEAN
+#define NOGDI
+#ifndef NOMINMAX
+#define NOMINMAX
+#endif
+#include 
+#endif
+
+#ifdef _MSC_VER
+// Don't support SetupApi on MinGW.
+#define USE_MSVC_SETUP_API
+
+// Make sure this comes before MSVCSetupApi.h
+#include 
+
+#include "llvm/Support/COM.h"
+#ifdef __clang__
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wnon-virtual-dtor"
+#endif
+#include "llvm/Support/MSVCSetupApi.h"
+#ifdef __clang__
+#pragma clang diagnostic pop
+#endif
+_COM_SMARTPTR_TYPEDEF(ISetupConfiguration, __uuidof(ISetupConfiguration));
+_COM_SMARTPTR_TYPEDEF(ISetupConfiguration2, __uuidof(ISetupConfiguration2));
+_COM_SMARTPTR_TYPEDEF(ISetupHelper, __uuidof(ISetupHelper));
+_COM_SMARTPTR_TYPEDEF(IEnumSetupInstances, __uuidof(IEnumSetupInstances));
+_COM_SMARTPTR_TYPEDEF(ISetupInstance, __uuidof(ISetupInstance));
+_COM_SMARTPTR_TYPEDEF(ISetupInstance2, __uuidof(ISetupInstance2));
+#endif
+
+static std::string
+getHighestNumericTupleInDirectory(llvm::vfs::FileSystem &VFS,
+  llvm::StringRef Directory) {
+  std::string Highest;
+  llvm::VersionTuple HighestTuple;
+
+  std::error_code EC;
+  for (llvm::vfs::directory_iterator DirIt = VFS.dir_begin(Directory, EC),
+ DirEnd;
+   !EC && DirIt != DirEnd; DirIt.increment(EC)) {
+auto Status = VFS.status(DirIt->path());
+if (!Status || !Status->isDirectory())
+  continue;
+llvm::StringRef CandidateName = llvm::sys::path::filename(DirIt->path());
+llvm::VersionTuple Tuple;
+if (Tuple.tryParse(CandidateName)) // tryParse() returns true on error.
+  continue;
+if (Tuple > HighestTuple) {
+  HighestTuple = Tuple;
+  Highest = CandidateName.str();
+}
+  }
+
+  return Highest;
+}
+
+static bool getWindows10SDKVersionFromPath(llvm::vfs::FileSystem &VFS,
+   const std::string &SDKPath,
+   std::string &SDKVersion) {
+  llvm::SmallString<128> IncludePath(SDKPath);
+  llvm::sys::path::append(IncludePath, "Include");
+  SDKVersion = g

[PATCH] D118070: Add /winsysroot support to lld

2022-02-01 Thread Peter Kasting via Phabricator via cfe-commits
pkasting marked 9 inline comments as done.
pkasting added a comment.

In D118070#3278123 , @thakis wrote:

> Things that can still improve:
>
> 1. This now does way more than just /winsysroot:. It also makes it so that 
> lld-link works in a non-msvc shell by looking up libpaths in either registry 
> or via setup api.

Updated description.

> 2. Some of the new lld-link code still looks (from a distance, might be 
> wrong) like it could be shared more

Did what I think I could, see details in comments.

> 3. Still needs tests.

Added a few basic tests, please review.




Comment at: lld/COFF/Driver.cpp:157
+// on the target architecture.
+static const char *getWindowsSDKArch() {
+  switch (config->machine) {

Found a way to share these.



Comment at: lld/COFF/Driver.cpp:666
+
+if (useUniversalCRT(vsLayout, vcToolChainPath)) {
+  std::string UniversalCRTSdkPath;

thakis wrote:
> Do you think it's possible to get this logic here (this path if ucrt else 
> this other path, etc) shared between the two places? Maybe a getLibPaths() 
> function that's passed arch etc?
The big problem is that in lld this logic is split between the two phases 
(pre-run() and post-run()) and can't easily be moved entirely into the 
post-run() phase.  So we'd only be able to share the logic that is here in the 
pre-run phase (the code that doesn't need the arch), which would make the clang 
code quite cumbersome.

I think it's not worth trying.



Comment at: lld/COFF/Driver.cpp:710
+  if (!windowsSdkLibPath.empty()) {
+if (sdkMajor >= 8) {
+  path::append(windowsSdkLibPath, getWindowsSDKArch());

Found a way to share this.



Comment at: lld/COFF/Driver.cpp:257
+
+  path::append(SDKPath, "Include");
+  Version = getHighestNumericTupleInDirectory(SDKPath);

thakis wrote:
> `Include` seems like the wrong directory to look in in the linker (…i think?)
I think this is OK.  It's not obvious to me that we can change this to a .lib 
check safely, and I think clang and lld should use the same logic to detect 
ucrt.



Comment at: lld/COFF/Driver.cpp:687
 void LinkerDriver::addLibSearchPaths() {
   Optional envOpt = Process::GetEnv("LIB");
   if (!envOpt.hasValue())

thakis wrote:
> thakis wrote:
> > We shouldn't look at `%LIB%` here when `/winsysroot:` is passed.
> Oh I see, this is now no longer called if winsysroot is passed.
Yep, added a test for this too.



Comment at: lld/COFF/SymbolTable.cpp:59
 config->machine = mt;
+driver->addWinSysRootLibSearchPaths();
   } else if (mt != IMAGE_FILE_MACHINE_UNKNOWN && config->machine != mt) {

thakis wrote:
> I suppose this is good enough :)
> 
> If we wanted to get fancy, we could put in code to explicitly detect the case 
> of a .lib file not being found, and addWinSysRootLibSearchPaths() not having 
> been called yet (due to the machine type not yet being know), and then diag 
> something like ".lib not found. you passed /winsysroot:, try passing 
> /machine: too". But I don't think this will ever happen in practice, so I 
> think it's not worth worrying about.
I think this can only happen for defaultlibs?  ...and those get processed after 
we set the machine type now, so there can't be a problem here.  Or at least, I 
couldn't find a way to trigger one in practice -- all my attempts led the 
linker to instead complain "No input files."  So I'm gonna call this N/A, but 
if you think of a command line to trigger it so I can test it, let me know.



Comment at: utils/bazel/llvm-project-overlay/clang/BUILD.bazel:1314
-"lib/Driver/ToolChains/MSVCSetupApi.h",
-],
 ),

thakis wrote:
> (fwiw you don't have to update BUILD.bazel files)
Shrug, it's not hard to do so.  And already done now, so...


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D118070/new/

https://reviews.llvm.org/D118070

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D118070: Make lld-link work in a non-MSVC shell

2022-02-01 Thread Peter Kasting via Phabricator via cfe-commits
pkasting updated this revision to Diff 404994.
pkasting marked 5 inline comments as done.
pkasting retitled this revision from "Add /winsysroot support to lld" to "Make 
lld-link work in a non-MSVC shell".
pkasting edited the summary of this revision.

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D118070/new/

https://reviews.llvm.org/D118070

Files:
  clang/docs/tools/clang-formatted-files.txt
  clang/lib/Driver/ToolChains/MSVC.cpp
  clang/lib/Driver/ToolChains/MSVC.h
  clang/lib/Driver/ToolChains/MSVCSetupApi.h
  lld/COFF/Driver.cpp
  lld/COFF/Driver.h
  lld/COFF/Options.td
  lld/COFF/SymbolTable.cpp
  lld/test/COFF/winsysroot.test
  llvm/include/llvm/Support/MSVCPaths.h
  llvm/include/llvm/Support/MSVCSetupApi.h
  llvm/lib/Support/CMakeLists.txt
  llvm/lib/Support/MSVCPaths.cpp
  llvm/utils/gn/secondary/llvm/lib/Support/BUILD.gn
  utils/bazel/llvm-project-overlay/clang/BUILD.bazel

Index: utils/bazel/llvm-project-overlay/clang/BUILD.bazel
===
--- utils/bazel/llvm-project-overlay/clang/BUILD.bazel
+++ utils/bazel/llvm-project-overlay/clang/BUILD.bazel
@@ -1309,9 +1309,6 @@
 "lib/Driver/ToolChains/Arch/*.cpp",
 "lib/Driver/ToolChains/Arch/*.h",
 ],
-exclude = [
-"lib/Driver/ToolChains/MSVCSetupApi.h",
-],
 ),
 hdrs = glob([
 "include/clang/Driver/*.h",
Index: llvm/utils/gn/secondary/llvm/lib/Support/BUILD.gn
===
--- llvm/utils/gn/secondary/llvm/lib/Support/BUILD.gn
+++ llvm/utils/gn/secondary/llvm/lib/Support/BUILD.gn
@@ -98,6 +98,7 @@
 "MD5.cpp",
 "MSP430AttributeParser.cpp",
 "MSP430Attributes.cpp",
+"MSVCPaths.cpp",
 "ManagedStatic.cpp",
 "MathExtras.cpp",
 "MemAlloc.cpp",
Index: llvm/lib/Support/MSVCPaths.cpp
===
--- /dev/null
+++ llvm/lib/Support/MSVCPaths.cpp
@@ -0,0 +1,707 @@
+//===-- MSVCPaths.cpp - MSVC path-parsing helpers -===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "llvm/Support/MSVCPaths.h"
+#include "llvm/ADT/Optional.h"
+#include "llvm/ADT/SmallString.h"
+#include "llvm/ADT/SmallVector.h"
+#include "llvm/ADT/StringExtras.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/ADT/Triple.h"
+#include "llvm/ADT/Twine.h"
+#include "llvm/Option/Arg.h"
+#include "llvm/Option/ArgList.h"
+#include "llvm/Support/ConvertUTF.h"
+#include "llvm/Support/Host.h"
+#include "llvm/Support/Path.h"
+#include "llvm/Support/Process.h"
+#include "llvm/Support/Program.h"
+#include "llvm/Support/VersionTuple.h"
+#include "llvm/Support/VirtualFileSystem.h"
+#include 
+
+#ifdef _WIN32
+#define WIN32_LEAN_AND_MEAN
+#define NOGDI
+#ifndef NOMINMAX
+#define NOMINMAX
+#endif
+#include 
+#endif
+
+#ifdef _MSC_VER
+// Don't support SetupApi on MinGW.
+#define USE_MSVC_SETUP_API
+
+// Make sure this comes before MSVCSetupApi.h
+#include 
+
+#include "llvm/Support/COM.h"
+#ifdef __clang__
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wnon-virtual-dtor"
+#endif
+#include "llvm/Support/MSVCSetupApi.h"
+#ifdef __clang__
+#pragma clang diagnostic pop
+#endif
+_COM_SMARTPTR_TYPEDEF(ISetupConfiguration, __uuidof(ISetupConfiguration));
+_COM_SMARTPTR_TYPEDEF(ISetupConfiguration2, __uuidof(ISetupConfiguration2));
+_COM_SMARTPTR_TYPEDEF(ISetupHelper, __uuidof(ISetupHelper));
+_COM_SMARTPTR_TYPEDEF(IEnumSetupInstances, __uuidof(IEnumSetupInstances));
+_COM_SMARTPTR_TYPEDEF(ISetupInstance, __uuidof(ISetupInstance));
+_COM_SMARTPTR_TYPEDEF(ISetupInstance2, __uuidof(ISetupInstance2));
+#endif
+
+static std::string
+getHighestNumericTupleInDirectory(llvm::vfs::FileSystem &VFS,
+  llvm::StringRef Directory) {
+  std::string Highest;
+  llvm::VersionTuple HighestTuple;
+
+  std::error_code EC;
+  for (llvm::vfs::directory_iterator DirIt = VFS.dir_begin(Directory, EC),
+ DirEnd;
+   !EC && DirIt != DirEnd; DirIt.increment(EC)) {
+auto Status = VFS.status(DirIt->path());
+if (!Status || !Status->isDirectory())
+  continue;
+llvm::StringRef CandidateName = llvm::sys::path::filename(DirIt->path());
+llvm::VersionTuple Tuple;
+if (Tuple.tryParse(CandidateName)) // tryParse() returns true on error.
+  continue;
+if (Tuple > HighestTuple) {
+  HighestTuple = Tuple;
+  Highest = CandidateName.str();
+}
+  }
+
+  return Highest;
+}
+
+static bool getWindows10SDKVersionFromPath(llvm::vfs::FileSystem &VFS,
+   const std::string &SDKPath,
+