Commit be76ab69 authored by Andrew Gallagher's avatar Andrew Gallagher Committed by Facebook GitHub Bot

Make `executable_path()` work on apple platforms

Summary:
`/proc/self/exe` doesn't exist on apple platforms, so provide an
implementation that does.

Reviewed By: yfeldblum

Differential Revision: D26599826

fbshipit-source-id: e00dde59d639063e59cb97feb75caf0a49a01954
parent b8116108
...@@ -18,6 +18,11 @@ ...@@ -18,6 +18,11 @@
#include <folly/Exception.h> #include <folly/Exception.h>
#ifdef __APPLE__
#include <glog/logging.h>
#include <mach-o/dyld.h> // @manual
#endif
namespace bsys = ::boost::system; namespace bsys = ::boost::system;
namespace folly { namespace folly {
...@@ -70,7 +75,16 @@ path canonical_parent(const path& pth, const path& base) { ...@@ -70,7 +75,16 @@ path canonical_parent(const path& pth, const path& base) {
} }
path executable_path() { path executable_path() {
#ifdef __APPLE__
uint32_t size = 0;
_NSGetExecutablePath(nullptr, &size);
std::string buf(size - 1, '\0');
auto data = const_cast<char*>(&*buf.data());
_NSGetExecutablePath(data, &size);
return path(std::move(buf));
#else
return read_symlink("/proc/self/exe"); return read_symlink("/proc/self/exe");
#endif
} }
} // namespace fs } // namespace fs
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment