[llvm] [Support] Implement getMainExecutable for OpenBSD with getexecpath() (PR #220141)

Brad Smith via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 9 19:00:04 PDT 2026


https://github.com/brad0 updated https://github.com/llvm/llvm-project/pull/220141

>From 925c30e8cbfbb58c229d604d748ab91fdef6bda3 Mon Sep 17 00:00:00 2001
From: Brad Smith <brad at comstyle.com>
Date: Mon, 31 Aug 2026 21:36:47 -0400
Subject: [PATCH] [Support] Implement getMainExecutable for OpenBSD with
 getexecpath()

OpenBSD now has a proper function to retrieve the executable and path.
---
 llvm/lib/Support/CMakeLists.txt |  7 +++++++
 llvm/lib/Support/Unix/Path.inc  | 22 +++++++++++++++-------
 2 files changed, 22 insertions(+), 7 deletions(-)

diff --git a/llvm/lib/Support/CMakeLists.txt b/llvm/lib/Support/CMakeLists.txt
index 1a3fa39c81e29..25683b85aaeef 100644
--- a/llvm/lib/Support/CMakeLists.txt
+++ b/llvm/lib/Support/CMakeLists.txt
@@ -1,3 +1,4 @@
+include(CheckFunctionExists)
 include(GetLibraryName)
 
 # Ensure that libSupport does not carry any static global initializer.
@@ -68,6 +69,12 @@ elseif( CMAKE_HOST_UNIX )
   if( UNIX AND "${CMAKE_SYSTEM_NAME}" MATCHES "SunOS" )
     set(system_libs ${system_libs} kstat socket)
   endif()
+  if( UNIX AND "${CMAKE_SYSTEM_NAME}" MATCHES "OpenBSD" )
+    check_function_exists(getexecpath HAVE_GETEXECPATH)
+    if (HAVE_GETEXECPATH)
+      add_compile_definitions(HAVE_GETEXECPATH)
+    endif()
+  endif()
   if( FUCHSIA )
     set(system_libs ${system_libs} zircon)
   endif()
diff --git a/llvm/lib/Support/Unix/Path.inc b/llvm/lib/Support/Unix/Path.inc
index d01967330e12f..309bfb7363f25 100644
--- a/llvm/lib/Support/Unix/Path.inc
+++ b/llvm/lib/Support/Unix/Path.inc
@@ -125,12 +125,11 @@ namespace fs {
 
 const file_t kInvalidFile = -1;
 
-#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) ||     \
-    defined(__FreeBSD_kernel__) || defined(__linux__) ||                       \
-    defined(__CYGWIN__) || defined(__DragonFly__) || defined(_AIX) ||          \
-    defined(__GNU__) ||                                                        \
-    (defined(__sun__) && defined(__svr4__) || defined(__HAIKU__)) ||           \
-    defined(__managarm__)
+#if defined(__FreeBSD__) || defined(__NetBSD__) || (defined(__OpenBSD__) &&    \
+    !defined(HAVE_GETEXECPATH)) || defined(__FreeBSD_kernel__) ||              \
+    defined(__linux__) || defined(__CYGWIN__) || defined(__DragonFly__) ||     \
+    defined(_AIX) || defined(__GNU__) || (defined(__sun__) &&                  \
+    defined(__svr4__) || defined(__HAIKU__)) || defined(__managarm__)
 static int test_dir(char ret[PATH_MAX], const char *dir, const char *bin) {
   struct stat sb;
   char fullpath[PATH_MAX];
@@ -287,7 +286,16 @@ std::string getMainExecutable(const char *argv0, void *MainAddr) {
   // Fall back to the classical detection.
   if (getprogpath(exe_path, argv0))
     return exe_path;
-#elif defined(__OpenBSD__) || defined(__HAIKU__)
+#elif defined(__OpenBSD__)
+  char exe_path[PATH_MAX];
+#ifdef HAVE_GETEXECPATH
+  if (getexecpath(exe_path, sizeof(exe_path)) == 0)
+    return exe_path;
+#else
+  if (getprogpath(exe_path, argv0) != NULL)
+    return exe_path;
+#endif
+#elif defined(__HAIKU__)
   char exe_path[PATH_MAX];
   // argv[0] only
   if (getprogpath(exe_path, argv0) != NULL)



More information about the llvm-commits mailing list