|
6 | 6 |
|
7 | 7 | #include <sys/stat.h> |
8 | 8 |
|
| 9 | +#if defined(__APPLE__) |
| 10 | +#include <mach-o/dyld.h> // _NSGetExecutablePath |
| 11 | +#elif defined(__linux__) |
| 12 | +#include <unistd.h> // readlink |
| 13 | +#elif !defined(_WIN32) |
| 14 | +#error "How to get the executable path for this platform?" |
| 15 | +#endif // _WIN32 ? |
| 16 | + |
9 | 17 | //TODO: x-plat definitions |
10 | 18 | #ifdef _WIN32 |
11 | 19 | typedef char16 TTDHostCharType; |
@@ -725,3 +733,97 @@ void CALLBACK Helpers::TTFlushAndCloseStreamCallback(JsTTDStreamHandle handle, b |
725 | 733 | fclose((FILE*)handle); |
726 | 734 | } |
727 | 735 |
|
| 736 | +#define SET_BINARY_PATH_ERROR_MESSAGE(path, msg) \ |
| 737 | + str_len = (int) strlen(msg); \ |
| 738 | + memcpy(path, msg, (size_t)str_len); \ |
| 739 | + path[str_len] = char(0) |
| 740 | + |
| 741 | +void GetBinaryLocation(char *path, const unsigned size) |
| 742 | +{ |
| 743 | + AssertMsg(path != nullptr, "Path can not be nullptr"); |
| 744 | + AssertMsg(size < INT_MAX, "Isn't it too big for a path buffer?"); |
| 745 | +#ifdef _WIN32 |
| 746 | + LPWSTR wpath = (WCHAR*)malloc(sizeof(WCHAR) * size); |
| 747 | + int str_len; |
| 748 | + if (!wpath) |
| 749 | + { |
| 750 | + SET_BINARY_PATH_ERROR_MESSAGE(path, "GetBinaryLocation: GetModuleFileName has failed. OutOfMemory!"); |
| 751 | + return; |
| 752 | + } |
| 753 | + str_len = GetModuleFileNameW(NULL, wpath, size - 1); |
| 754 | + if (str_len <= 0) |
| 755 | + { |
| 756 | + SET_BINARY_PATH_ERROR_MESSAGE(path, "GetBinaryLocation: GetModuleFileName has failed."); |
| 757 | + free(wpath); |
| 758 | + return; |
| 759 | + } |
| 760 | + |
| 761 | + str_len = WideCharToMultiByte(CP_UTF8, 0, wpath, str_len, path, size, NULL, NULL); |
| 762 | + free(wpath); |
| 763 | + |
| 764 | + if (str_len <= 0) |
| 765 | + { |
| 766 | + SET_BINARY_PATH_ERROR_MESSAGE(path, "GetBinaryLocation: GetModuleFileName (WideCharToMultiByte) has failed."); |
| 767 | + return; |
| 768 | + } |
| 769 | + |
| 770 | + if ((unsigned)str_len > size - 1) |
| 771 | + { |
| 772 | + str_len = (int) size - 1; |
| 773 | + } |
| 774 | + path[str_len] = char(0); |
| 775 | +#elif defined(__APPLE__) |
| 776 | + uint32_t path_size = (uint32_t)size; |
| 777 | + char *tmp = nullptr; |
| 778 | + int str_len; |
| 779 | + if (_NSGetExecutablePath(path, &path_size)) |
| 780 | + { |
| 781 | + SET_BINARY_PATH_ERROR_MESSAGE(path, "GetBinaryLocation: _NSGetExecutablePath has failed."); |
| 782 | + return; |
| 783 | + } |
| 784 | + |
| 785 | + tmp = (char*)malloc(size); |
| 786 | + char *result = realpath(path, tmp); |
| 787 | + str_len = strlen(result); |
| 788 | + memcpy(path, result, str_len); |
| 789 | + free(tmp); |
| 790 | + path[str_len] = char(0); |
| 791 | +#elif defined(__linux__) |
| 792 | + int str_len = readlink("/proc/self/exe", path, size - 1); |
| 793 | + if (str_len <= 0) |
| 794 | + { |
| 795 | + SET_BINARY_PATH_ERROR_MESSAGE(path, "GetBinaryLocation: /proc/self/exe has failed."); |
| 796 | + return; |
| 797 | + } |
| 798 | + path[str_len] = char(0); |
| 799 | +#else |
| 800 | +#warning "Implement GetBinaryLocation for this platform" |
| 801 | +#endif |
| 802 | +} |
| 803 | + |
| 804 | +// xplat-todo: Implement a corresponding solution for GetModuleFileNameW |
| 805 | +// and cleanup PAL. [ https://github.com/Microsoft/ChakraCore/pull/2288 should be merged first ] |
| 806 | +// GetModuleFileName* PAL is not reliable and forces us to explicitly double initialize PAL |
| 807 | +// with argc / argv.... |
| 808 | +void GetBinaryPathWithFileNameA(char *path, const size_t buffer_size, const char* filename) |
| 809 | +{ |
| 810 | + char fullpath[_MAX_PATH]; |
| 811 | + char drive[_MAX_DRIVE]; |
| 812 | + char dir[_MAX_DIR]; |
| 813 | + |
| 814 | + char modulename[_MAX_PATH]; |
| 815 | + GetBinaryLocation(modulename, _MAX_PATH); |
| 816 | + _splitpath_s(modulename, drive, _MAX_DRIVE, dir, _MAX_DIR, nullptr, 0, nullptr, 0); |
| 817 | + _makepath_s(fullpath, drive, dir, filename, nullptr); |
| 818 | + |
| 819 | + size_t len = strlen(fullpath); |
| 820 | + if (len < buffer_size) |
| 821 | + { |
| 822 | + memcpy(path, fullpath, len * sizeof(char)); |
| 823 | + } |
| 824 | + else |
| 825 | + { |
| 826 | + len = 0; |
| 827 | + } |
| 828 | + path[len] = char(0); |
| 829 | +} |
0 commit comments