From a0089cb62d50e32565dba1f2ccd33764661c54ca Mon Sep 17 00:00:00 2001 From: Mike Date: Thu, 4 Jan 2024 15:11:03 +0800 Subject: [PATCH] robust exe path detection (#23) --- src/CMakeLists.txt | 3 ++- src/apps/cli.cpp | 32 +++++++++++++++++++++++--------- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 652d503a..db7409c3 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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) @@ -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) diff --git a/src/apps/cli.cpp b/src/apps/cli.cpp index a5413334..ce15ad9b 100644 --- a/src/apps/cli.cpp +++ b/src/apps/cli.cpp @@ -13,10 +13,10 @@ #include #include -#ifdef LUISA_PLATFORM_WINDOWS +#if defined(LUISA_PLATFORM_WINDOWS) #include [[nodiscard]] auto get_current_exe_path() noexcept { - constexpr auto max_path_length = static_cast(4096); + constexpr auto max_path_length = std::max(MAX_PATH, 4096); std::filesystem::path::value_type path[max_path_length] = {}; auto nchar = GetModuleFileNameW(nullptr, path, max_path_length); if (nchar == 0 || @@ -27,18 +27,32 @@ } return std::filesystem::canonical(path).string(); } -#else +#elif defined(LUISA_PLATFORM_APPLE) #include #include [[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)}; + return std::filesystem::canonical(path).string(); + } + LUISA_ERROR_WITH_LOCATION( + "Failed to get current executable path (PID = {}): {}.", + pid, strerror(errno)); +} +#else +#include +[[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)}; + return std::filesystem::canonical(path).string(); + } } - return std::filesystem::canonical(pathbuf).string(); + LUISA_ERROR_WITH_LOCATION( + "Failed to get current executable path."); } #endif