Merge pull request #411 from diamante0018/debug-regression

fix: wrong "bin" directory when using executable from $PATH
This commit is contained in:
Jan 2025-04-23 02:14:12 +02:00 committed by GitHub
commit e14f0743da
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 43 additions and 6 deletions

View File

@ -5,6 +5,7 @@
#include "ObjWriting.h" #include "ObjWriting.h"
#include "Utils/Arguments/UsageInformation.h" #include "Utils/Arguments/UsageInformation.h"
#include "Utils/FileUtils.h" #include "Utils/FileUtils.h"
#include "Utils/PathUtils.h"
#include <filesystem> #include <filesystem>
#include <format> #include <format>
@ -152,9 +153,9 @@ void LinkerArgs::PrintVersion()
std::cout << std::format("OpenAssetTools Linker {}\n", GIT_VERSION); std::cout << std::format("OpenAssetTools Linker {}\n", GIT_VERSION);
} }
void LinkerArgs::SetBinFolder(const char* argv0) void LinkerArgs::SetBinFolder()
{ {
const fs::path path(argv0); const fs::path path(utils::GetExecutablePath());
m_bin_folder = path.parent_path().string(); m_bin_folder = path.parent_path().string();
} }
@ -190,7 +191,7 @@ bool LinkerArgs::ParseArgs(const int argc, const char** argv, bool& shouldContin
return true; return true;
} }
SetBinFolder(argv[0]); SetBinFolder();
m_project_specifiers_to_build = m_argument_parser.GetArguments(); m_project_specifiers_to_build = m_argument_parser.GetArguments();
if (m_project_specifiers_to_build.empty()) if (m_project_specifiers_to_build.empty())

View File

@ -37,7 +37,7 @@ private:
void PrintUsage() const; void PrintUsage() const;
static void PrintVersion(); static void PrintVersion();
void SetBinFolder(const char* argv0); void SetBinFolder();
void SetVerbose(bool isVerbose); void SetVerbose(bool isVerbose);
ArgumentParser m_argument_parser; ArgumentParser m_argument_parser;

View File

@ -314,8 +314,8 @@ namespace
std::unique_ptr<ILinkerPaths> ILinkerPaths::FromArgs(const LinkerArgs& args) std::unique_ptr<ILinkerPaths> ILinkerPaths::FromArgs(const LinkerArgs& args)
{ {
std::string normalizedBinPath = fs::canonical(args.m_bin_folder).make_preferred().string(); std::string normalizedBinPath = fs::weakly_canonical(args.m_bin_folder).make_preferred().string();
std::string normalizedBasePath = fs::canonical(args.m_base_folder).make_preferred().string(); std::string normalizedBasePath = fs::weakly_canonical(args.m_base_folder).make_preferred().string();
LinkerSearchPathBuilder assetSearchPaths("asset", normalizedBinPath, normalizedBasePath); LinkerSearchPathBuilder assetSearchPaths("asset", normalizedBinPath, normalizedBasePath);
assetSearchPaths.BuildFromArgs(args.m_asset_search_paths); assetSearchPaths.BuildFromArgs(args.m_asset_search_paths);

View File

@ -1,4 +1,5 @@
#pragma once #pragma once
#include <cstdint> #include <cstdint>
#include <set> #include <set>
#include <string> #include <string>

View File

@ -0,0 +1,27 @@
#include "PathUtils.h"
#include <stdexcept>
#ifdef _WIN32
#include <windows.h>
#elif defined(__linux__)
#include <limits.h>
#include <unistd.h>
#endif
namespace utils
{
std::string GetExecutablePath()
{
#ifdef _WIN32
char result[MAX_PATH];
return std::string(result, GetModuleFileNameA(NULL, result, MAX_PATH));
#elif defined(__linux__)
char result[PATH_MAX];
const auto count = readlink("/proc/self/exe", result, PATH_MAX);
return std::string(result, (count > 0) ? count : 0);
#else
throw std::runtime_error("Unknown platform for executable path");
#endif
}
} // namespace utils

View File

@ -0,0 +1,8 @@
#pragma once
#include <string>
namespace utils
{
std::string GetExecutablePath();
}