[llvm] [Support] Resolve symlinks in `getMainExecutable()` on Windows (PR #76304)

Alexandre Ganea via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 25 05:55:01 PST 2023


https://github.com/aganea updated https://github.com/llvm/llvm-project/pull/76304

>From 51d89243cdc1239e79a2829d2735c3ebdade0fed Mon Sep 17 00:00:00 2001
From: Alexandre Ganea <alex_toresh at yahoo.fr>
Date: Sat, 23 Dec 2023 19:03:09 -0500
Subject: [PATCH 1/3] [Support] Resolve symlinks in `getMainExecutable()`.

This makes the Windows implementation for `getMainExecutable()` behave the same as its Linux counterpart, in regards to symlinks. Previously, when using `cmake ... -DLLVM_USE_SYMLINKS=ON`, calling this function wouldn't resolve to the "real", non-symlinked path.
---
 llvm/lib/Support/Windows/Path.inc | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/Support/Windows/Path.inc b/llvm/lib/Support/Windows/Path.inc
index 168a63bb2d969d..6b50309be94d77 100644
--- a/llvm/lib/Support/Windows/Path.inc
+++ b/llvm/lib/Support/Windows/Path.inc
@@ -154,7 +154,10 @@ std::string getMainExecutable(const char *argv0, void *MainExecAddr) {
     return "";
 
   llvm::sys::path::make_preferred(PathNameUTF8);
-  return std::string(PathNameUTF8.data());
+
+  SmallString<256> RealPath;
+  sys::fs::real_path(PathNameUTF8, RealPath);
+  return (std::string)RealPath;
 }
 
 UniqueID file_status::getUniqueID() const {

>From ea26bc48c253799c3e6b7f000ed8ae23915d0db4 Mon Sep 17 00:00:00 2001
From: Alexandre Ganea <alex_toresh at yahoo.fr>
Date: Sun, 24 Dec 2023 15:50:30 -0500
Subject: [PATCH 2/3] Change std::string cast to .str().str()

---
 llvm/lib/Support/Windows/Path.inc | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/lib/Support/Windows/Path.inc b/llvm/lib/Support/Windows/Path.inc
index 6b50309be94d77..46c8ac57ad99ce 100644
--- a/llvm/lib/Support/Windows/Path.inc
+++ b/llvm/lib/Support/Windows/Path.inc
@@ -157,7 +157,7 @@ std::string getMainExecutable(const char *argv0, void *MainExecAddr) {
 
   SmallString<256> RealPath;
   sys::fs::real_path(PathNameUTF8, RealPath);
-  return (std::string)RealPath;
+  return RealPath.str().str();
 }
 
 UniqueID file_status::getUniqueID() const {

>From b410519d548fad092be308eb483cfda7f2521ad1 Mon Sep 17 00:00:00 2001
From: Alexandre Ganea <alex_toresh at yahoo.fr>
Date: Mon, 25 Dec 2023 08:54:47 -0500
Subject: [PATCH 3/3] Use std::string operator.

---
 llvm/lib/Support/Windows/Path.inc | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/lib/Support/Windows/Path.inc b/llvm/lib/Support/Windows/Path.inc
index 46c8ac57ad99ce..2bf68b7972e746 100644
--- a/llvm/lib/Support/Windows/Path.inc
+++ b/llvm/lib/Support/Windows/Path.inc
@@ -157,7 +157,7 @@ std::string getMainExecutable(const char *argv0, void *MainExecAddr) {
 
   SmallString<256> RealPath;
   sys::fs::real_path(PathNameUTF8, RealPath);
-  return RealPath.str().str();
+  return std::string(RealPath);
 }
 
 UniqueID file_status::getUniqueID() const {



More information about the llvm-commits mailing list