-
Notifications
You must be signed in to change notification settings - Fork 195
Add utility to get the path of the current executable #3218
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| /* Copyright (c) 2008-2025 the MRtrix3 contributors. | ||
| * | ||
| * This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
| * | ||
| * Covered Software is provided under this License on an "as is" | ||
| * basis, without warranty of any kind, either expressed, implied, or | ||
| * statutory, including, without limitation, warranties that the | ||
| * Covered Software is free of defects, merchantable, fit for a | ||
| * particular purpose or non-infringing. | ||
| * See the Mozilla Public License v. 2.0 for more details. | ||
| * | ||
| * For more details, see http://www.mrtrix.org/. | ||
| */ | ||
|
|
||
| #include "platform.h" | ||
|
|
||
| #ifdef MRTRIX_WINDOWS | ||
| #include <windows.h> | ||
| #elif defined(MRTRIX_MACOSX) | ||
| #include <mach-o/dyld.h> | ||
| #elif defined(MRTRIX_FREEBSD) | ||
| #include <sys/sysctl.h> | ||
| #include <sys/types.h> | ||
| #else | ||
| #include <unistd.h> | ||
| #endif | ||
|
|
||
| #include <cstdint> | ||
| #include <filesystem> | ||
| #include <stdexcept> | ||
| #include <string> | ||
|
|
||
| namespace MR::Platform { | ||
| std::filesystem::path get_executable_path() { | ||
| #ifdef MRTRIX_WINDOWS | ||
| std::vector<wchar_t> buffer; | ||
| buffer.resize(MAX_PATH); | ||
| for (;;) { | ||
| DWORD len = GetModuleFileNameW(nullptr, buffer.data(), static_cast<DWORD>(buffer.size())); | ||
| if (len == 0) { | ||
| throw std::system_error(std::error_code(static_cast<int>(GetLastError()), std::system_category()), | ||
| "GetModuleFileNameW failed"); | ||
| } | ||
| if (static_cast<size_t>(len) < buffer.size()) { | ||
| return std::filesystem::path(std::wstring(buffer.data(), len)); | ||
| } | ||
| size_t new_size = buffer.size() * 2; | ||
| if (new_size <= buffer.size()) | ||
| throw std::runtime_error("Executable path too long"); | ||
| buffer.resize(new_size); | ||
| } | ||
| #elif defined(MRTRIX_MACOSX) | ||
| uint32_t size = 0; | ||
| if (_NSGetExecutablePath(nullptr, &size) != -1) | ||
| throw std::runtime_error("Unexpected success getting buffer size"); | ||
| std::string buffer; | ||
| buffer.resize(size); | ||
| if (_NSGetExecutablePath(buffer.data(), &size) != 0) | ||
| throw std::runtime_error("_NSGetExecutablePath failed to fill buffer"); | ||
| try { | ||
| return std::filesystem::canonical(std::filesystem::path(buffer)); | ||
| } catch (const std::filesystem::filesystem_error &) { | ||
| return std::filesystem::weakly_canonical(std::filesystem::path(buffer)); | ||
| } | ||
| return std::filesystem::canonical(std::filesystem::path(buffer)); | ||
| #elif defined(MRTRIX_LINUX) | ||
| const std::filesystem::path link("/proc/self/exe"); | ||
| try { | ||
| const auto p = std::filesystem::read_symlink(link); | ||
| try { | ||
| return std::filesystem::canonical(p); | ||
| } catch (const std::filesystem::filesystem_error &) { | ||
| return std::filesystem::weakly_canonical(p); | ||
| } | ||
| } catch (const std::filesystem::filesystem_error &e) { | ||
| throw std::system_error(std::error_code(errno, std::system_category()), | ||
| std::string("read_symlink(\"/proc/self/exe\") failed: ") + e.what()); | ||
| } | ||
| #elif defined(MRTRIX_FREEBSD) | ||
| // See https://github.com/chromium/chromium/blob/db87c489ae756af10467897d653518f321db2f4d/base/base_paths_posix.cc | ||
| int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1}; | ||
| size_t size = 0; | ||
| if (sysctl(mib, 4, nullptr, &size, nullptr, 0) == -1) | ||
| throw std::system_error(std::error_code(errno, std::system_category()), | ||
| "sysctl(KERN_PROC_PATHNAME) failed to get size"); | ||
| std::string buffer; | ||
| buffer.resize(size); | ||
| if (sysctl(mib, 4, buffer.data(), &size, nullptr, 0) == -1) | ||
| throw std::system_error(std::error_code(errno, std::system_category()), | ||
| "sysctl(KERN_PROC_PATHNAME) failed to get path"); | ||
| // Ensure we use the null-terminated string | ||
| try { | ||
| return std::filesystem::canonical(std::filesystem::path(buffer.c_str())); | ||
| } catch (const std::filesystem::filesystem_error &) { | ||
| return std::filesystem::weakly_canonical(std::filesystem::path(buffer.c_str())); | ||
| } | ||
| #else | ||
| static_assert(false, "Unsupported platform"); | ||
| #endif | ||
| } | ||
| } // namespace MR::Platform |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| /* Copyright (c) 2008-2025 the MRtrix3 contributors. | ||
| * | ||
| * This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
| * | ||
| * Covered Software is provided under this License on an "as is" | ||
| * basis, without warranty of any kind, either expressed, implied, or | ||
| * statutory, including, without limitation, warranties that the | ||
| * Covered Software is free of defects, merchantable, fit for a | ||
| * particular purpose or non-infringing. | ||
| * See the Mozilla Public License v. 2.0 for more details. | ||
| * | ||
| * For more details, see http://www.mrtrix.org/. | ||
| */ | ||
|
|
||
| #include <filesystem> | ||
|
|
||
| namespace MR::Platform { | ||
| // A cross-platform function to get the (absolute) path of the current executable. | ||
| // Throws an exception on failure. | ||
| std::filesystem::path get_executable_path(); | ||
| } // namespace MR::Platform |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| /* Copyright (c) 2008-2025 the MRtrix3 contributors. | ||
| * | ||
| * This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
| * | ||
| * Covered Software is provided under this License on an "as is" | ||
| * basis, without warranty of any kind, either expressed, implied, or | ||
| * statutory, including, without limitation, warranties that the | ||
| * Covered Software is free of defects, merchantable, fit for a | ||
| * particular purpose or non-infringing. | ||
| * See the Mozilla Public License v. 2.0 for more details. | ||
| * | ||
| * For more details, see http://www.mrtrix.org/. | ||
| */ | ||
|
|
||
| #include "platform.h" | ||
| #include <gtest/gtest.h> | ||
|
|
||
| #if defined(MRTRIX_LINUX) || defined(MRTRIX_MACOSX) || defined(MRTRIX_FREEBSD) | ||
| #include <sys/unistd.h> | ||
| #include <unistd.h> | ||
| #endif | ||
|
|
||
| #if defined(MRTRIX_WINDOWS) | ||
| #include <algorithm> | ||
| #endif | ||
|
|
||
| #include <filesystem> | ||
|
|
||
| using namespace MR::Platform; | ||
|
|
||
| TEST(ExecutablePathTest, ReturnsExistingAbsolutePath) { | ||
daljit46 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| std::filesystem::path p; | ||
| EXPECT_NO_THROW(p = get_executable_path()); | ||
| EXPECT_FALSE(p.empty()) << "Returned path should not be empty"; | ||
| EXPECT_TRUE(p.is_absolute()) << "Returned path should be absolute: " << p; | ||
| EXPECT_TRUE(std::filesystem::exists(p)) << "Path must exist: " << p; | ||
| EXPECT_TRUE(std::filesystem::is_regular_file(p)) << "Path should be a regular file: " << p; | ||
|
|
||
| // canonicalize should succeed for a valid on-disk executable. | ||
| EXPECT_NO_THROW({ | ||
| auto c = std::filesystem::canonical(p); | ||
| (void)c; | ||
| }); | ||
| } | ||
|
|
||
| TEST(ExecutablePathTest, PlatformExecutableCheck) { | ||
daljit46 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const auto p = get_executable_path(); | ||
|
|
||
| #if defined(MRTRIX_WINDOWS) | ||
| ASSERT_TRUE(p.has_extension()) << "Executable should have an extension on Windows: " << p; | ||
| std::string ext = p.extension().string(); | ||
| std::transform( | ||
| ext.begin(), ext.end(), ext.begin(), [](auto ch) -> char { return static_cast<char>(std::tolower(ch)); }); | ||
| EXPECT_EQ(ext, ".exe") << "Expected .exe extension for test binary on Windows: " << p; | ||
| #elif defined(MRTRIX_LINUX) || defined(MRTRIX_MACOSX) || defined(MRTRIX_FREEBSD) | ||
| // On POSIX, check that the file is marked executable for current user. | ||
| const char *cpath = p.c_str(); | ||
| const int ok = access(cpath, X_OK); | ||
| EXPECT_EQ(ok, 0) << "Executable should be executable for current user: " << p; | ||
| #else | ||
| static_assert(false, "Unsupported platform"); | ||
| #endif | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.