Skip to content

Commit

Permalink
robust exe path detection (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike-Leo-Smith committed Jan 4, 2024
1 parent 9c14177 commit a0089cb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ if (CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
/Zc:preprocessor)
endif ()

set(LUISA_COMPUTE_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(LUISA_COMPUTE_ENABLE_LTO OFF CACHE BOOL "" FORCE)
set(LUISA_COMPUTE_ENABLE_PYTHON OFF CACHE BOOL "" FORCE)
set(LUISA_COMPUTE_ENABLE_UNITY_BUILD ${LUISA_RENDER_ENABLE_UNITY_BUILD} CACHE BOOL "" FORCE)
Expand All @@ -46,7 +47,7 @@ function(luisa_render_add_plugin name)
target_compile_definitions(${lib_name} PRIVATE LUISA_RENDER_PLUGIN_NAME="${name}")
if (${PLUGIN_CATEGORY} STREQUAL "spectrum")
set(plugin_lib_name luisa-render-spectra)
elseif(${PLUGIN_CATEGORY} STREQUAL "medium")
elseif (${PLUGIN_CATEGORY} STREQUAL "medium")
set(plugin_lib_name luisa-render-media)
else ()
set(plugin_lib_name luisa-render-${PLUGIN_CATEGORY}s)
Expand Down
32 changes: 23 additions & 9 deletions src/apps/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
#include <base/scene.h>
#include <base/pipeline.h>

#ifdef LUISA_PLATFORM_WINDOWS
#if defined(LUISA_PLATFORM_WINDOWS)
#include <windows.h>
[[nodiscard]] auto get_current_exe_path() noexcept {
constexpr auto max_path_length = static_cast<size_t>(4096);
constexpr auto max_path_length = std::max<size_t>(MAX_PATH, 4096);
std::filesystem::path::value_type path[max_path_length] = {};
auto nchar = GetModuleFileNameW(nullptr, path, max_path_length);
if (nchar == 0 ||
Expand All @@ -27,18 +27,32 @@
}
return std::filesystem::canonical(path).string();
}
#else
#elif defined(LUISA_PLATFORM_APPLE)
#include <libproc.h>
#include <unistd.h>
[[nodiscard]] auto get_current_exe_path() noexcept {
char pathbuf[PROC_PIDPATHINFO_MAXSIZE];
char pathbuf[PROC_PIDPATHINFO_MAXSIZE] = {};
auto pid = getpid();
if (proc_pidpath(pid, pathbuf, sizeof(pathbuf)) <= 0) {
LUISA_ERROR_WITH_LOCATION(
"Failed to get current executable path (PID = {}): {}.",
pid, strerror(errno));
if (auto size = proc_pidpath(pid, pathbuf, sizeof(pathbuf)); size > 0) {
luisa::string_view path{pathbuf, static_cast<size_t>(size)};
return std::filesystem::canonical(path).string();
}
LUISA_ERROR_WITH_LOCATION(
"Failed to get current executable path (PID = {}): {}.",
pid, strerror(errno));
}
#else
#include <unistd.h>
[[nodiscard]] auto get_current_exe_path() noexcept {
char pathbuf[PATH_MAX] = {};
for (auto p : {"/proc/self/exe", "/proc/curproc/file", "/proc/self/path/a.out"}) {
if (auto size = readlink(p, pathbuf, sizeof(pathbuf)); size > 0) {
luisa::string_view path{pathbuf, static_cast<size_t>(size)};
return std::filesystem::canonical(path).string();
}
}
return std::filesystem::canonical(pathbuf).string();
LUISA_ERROR_WITH_LOCATION(
"Failed to get current executable path.");
}
#endif

Expand Down

0 comments on commit a0089cb

Please sign in to comment.