[Support][Haiku] Use the app image path in getMainExecutable() #246810
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-support
Author: Brad Smith (brad0)
<details>
<summary>Changes</summary>
On Haiku getMainExecutable() only searched for argv[0]. Callers such
as LibTooling pass a placeholder ("clang_tool"), so the lookup failed
and tools like clang-tidy could not find the Clang resource directory.
Get the path of the app image from get_next_image_info() instead and
only fall back to argv[0] if no app image is found.
https://github.com/haikuports/haikuports/issues/15124
---
Full diff: https://github.com/llvm/llvm-project/pull/246810.diff
1 Files Affected:
````````````diff --git a/llvm/lib/Support/Unix/Path.inc b/llvm/lib/Support/Unix/Path.inc
index 427d0474bc3129..0de49be102a89b 100644
--- a/llvm/lib/Support/Unix/Path.inc
+++ b/llvm/lib/Support/Unix/Path.inc
@@ -53,6 +53,8 @@ extern char **environ;
#elif defined(__MVS__)
#include "llvm/Support/AutoConvert.h"
#include <sys/ps.h>
+#elif defined(__HAIKU__)
+#include <image.h>
#endif
// Both stdio.h and cstdio are included via different paths and
@@ -286,8 +288,20 @@ std::string getMainExecutable(const char *argv0, void *MainAddr) {
return exe_path;
#endif
#elif defined(__HAIKU__)
+ image_info info;
+ int32 cookie = 0;
+ while (get_next_image_info(B_CURRENT_TEAM, &cookie, &info) == B_OK) {
+ if (info.type == B_APP_IMAGE) {
+ char real_path[PATH_MAX];
+ if (realpath(info.name, real_path))
+ return std::string(real_path);
+ if (info.name[0] != '\0')
+ return std::string(info.name);
+ break;
+ }
+ }
+ // Fall back to argv[0].
char exe_path[PATH_MAX];
- // argv[0] only
if (getprogpath(exe_path, argv0) != NULL)
return exe_path;
#elif defined(__sun__) && defined(__svr4__)
</details>
Arguably this should be llvm_unreachable(). The loader would have to be quite broken for there to be no B_APP_IMAGE in the process.
Approved these changes.
I'm not a big fan of llvm_unreachable(). Enough times it does become reachable.
alexrp
brad0
On Haiku getMainExecutable() only searched for argv[0]. Callers such
as LibTooling pass a placeholder ("clang_tool"), so the lookup failed
and tools like clang-tidy could not find the Clang resource directory.
Get the path of the app image from get_next_image_info() instead and
only fall back to argv[0] if no app image is found.
https://github.com/haikuports/haikuports/issues/15124
Assisted-by: Claude Opus 5.5