[PATCH] D12711: ScanDirForExecutable on Windows fails to find executables with the "exe" extension in name

Oleg Ranevskyy via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 8 16:01:09 PDT 2015


iid_iunknown created this revision.
iid_iunknown added a reviewer: rnk.
iid_iunknown added a subscriber: llvm-commits.
iid_iunknown set the repository for this revision to rL LLVM.

Hello Reid

This is a patch for the problem we discussed some time ago. Would you be able to take a look and share your opinion, please?
Thank you.

**Description**

When the driver tries to locate a program by its name, e.g. a linker, it scans the paths provided by the toolchain using the ScanDirForExecutable function. If the lookup fails, the driver uses llvm::sys::findProgramByName. Unlike llvm::sys::findProgramByName, ScanDirForExecutable is not aware of file extensions. If the program has the "exe" extension in its name, which is very common on Windows, ScanDirForExecutable won't find it under the toolchain-provided paths.

This patch changes the Windows version of the "`access`" function called by ScanDirForExecutable to respect file extensions, similarly to llvm::sys::findProgramByName.

Repository:
  rL LLVM

http://reviews.llvm.org/D12711

Files:
  lib/Support/Windows/Path.inc

Index: lib/Support/Windows/Path.inc
===================================================================
--- lib/Support/Windows/Path.inc
+++ lib/Support/Windows/Path.inc
@@ -17,6 +17,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/StringExtras.h"
 #include "llvm/Support/WindowsError.h"
 #include <fcntl.h>
 #include <io.h>
@@ -280,12 +281,26 @@
 }
 
 std::error_code access(const Twine &Path, AccessMode Mode) {
-  SmallVector<wchar_t, 128> PathUtf16;
+  SmallVector<StringRef, 12> PathExts;
+  PathExts.push_back("");
+  PathExts.push_back(".exe"); // FIXME: This must be in %PATHEXT%.
+  if (const char *PathExtEnv = std::getenv("PATHEXT"))
+    SplitString(PathExtEnv, PathExts, ";");
 
-  if (std::error_code EC = widenPath(Path, PathUtf16))
-    return EC;
+  DWORD Attributes = INVALID_FILE_ATTRIBUTES;
+
+  for (StringRef Ext : PathExts) {
+    SmallVector<wchar_t, 128> PathUtf16;
+
+    if (std::error_code EC = widenPath(Path + Ext, PathUtf16))
+      return EC;
 
-  DWORD Attributes = ::GetFileAttributesW(PathUtf16.begin());
+    Attributes = ::GetFileAttributesW(PathUtf16.begin());
+
+    if (Attributes != INVALID_FILE_ATTRIBUTES)
+      // the file exists
+      break;
+  }
 
   if (Attributes == INVALID_FILE_ATTRIBUTES) {
     // See if the file didn't actually exist.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D12711.34274.patch
Type: text/x-patch
Size: 1421 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150908/f029d8c0/attachment.bin>


More information about the llvm-commits mailing list