Stub out a Path::GetMainExecutable call to find the path to the
authorChris Lattner <sabre@nondot.org>
Mon, 3 Mar 2008 02:55:43 +0000 (02:55 +0000)
committerChris Lattner <sabre@nondot.org>
Mon, 3 Mar 2008 02:55:43 +0000 (02:55 +0000)
main executable of a program.  This needs to be implemented on windows.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@47835 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/System/Path.h
lib/System/Unix/Path.inc
lib/System/Win32/Path.inc

index ee860e8cf91d2553937deb3a6b3777002bd46947..c2a1bbc7c8cf9dc750ba57a1fdd6a02a4d113e1a 100644 (file)
@@ -161,6 +161,10 @@ namespace sys {
       /// @returns The dynamic link library suffix for the current platform.
       /// @brief Return the dynamic link library suffix.
       static std::string GetDLLSuffix();
+    
+      /// GetMainExecutable - Return the path to the main executable, given the
+      /// value of argv[0] from program startup and the address of main itself.
+      static Path GetMainExecutable(const char *argv0, void *MainAddr);
 
       /// This is one of the very few ways in which a path can be constructed
       /// with a syntactically invalid name. The only *legal* invalid name is an
index c8bdd088c3250df14f94332cf4492eff2d112bef..8e76b543ff3befbbd25b88e1506dbfff10da1bc2 100644 (file)
 # endif
 #endif
 
+#if HAVE_DLFCN_H
+#include <dlfcn.h>
+#endif
+
 // Put in a hack for Cygwin which falsely reports that the mkdtemp function
 // is available when it is not.
 #ifdef __CYGWIN__
@@ -244,6 +248,20 @@ Path::GetCurrentDirectory() {
   return Path(pathname);
 }
 
+/// GetMainExecutable - Return the path to the main executable, given the
+/// value of argv[0] from program startup.
+Path Path::GetMainExecutable(const char *argv0, void *MainAddr) {
+  // Use dladdr to get executable path if available.
+#ifdef HAVE_DLFCN_H
+  Dl_info DLInfo;
+  int err = dladdr(MainAddr, &DLInfo);
+  if (err != 0)
+    return Path(std::string(DLInfo.dli_fname));
+#endif
+  return Path();
+}
+
+
 std::string
 Path::getBasename() const {
   // Find the last slash
index ee3f622ed32ab876ddb9e4723a321ab64c0b664f..0499e617957d0727c0872c33efb9bd4ccdb5dc17 100644 (file)
@@ -214,6 +214,12 @@ Path::GetCurrentDirectory() {
   return Path(pathname);  
 }
 
+/// GetMainExecutable - Return the path to the main executable, given the
+/// value of argv[0] from program startup.
+Path Path::GetMainExecutable(const char *argv0, void *MainAddr) {
+  return Path();
+}
+
 
 // FIXME: the above set of functions don't map to Windows very well.