|
3 | 3 |
|
4 | 4 | #include <llvm/Support/CommandLine.h> |
5 | 5 |
|
| 6 | +#include <algorithm> |
| 7 | +#include <array> |
6 | 8 | #include <cstdlib> |
7 | 9 | #include <filesystem> |
8 | 10 | #include <fstream> |
9 | 11 | #include <vector> |
10 | 12 |
|
| 13 | +#if defined(_WIN32) |
| 14 | +#include <windows.h> |
| 15 | +#elif defined(__linux__) |
| 16 | +#include <limits.h> |
| 17 | +#include <unistd.h> |
| 18 | +#elif defined(__APPLE__) |
| 19 | +#include <mach-o/dyld.h> |
| 20 | +#endif |
| 21 | + |
11 | 22 | #include "cpp2rust_lib.h" |
12 | 23 | #include "logging.h" |
13 | | -#include "rules_dir.h" |
14 | 24 |
|
15 | 25 | namespace fs = std::filesystem; |
16 | 26 |
|
@@ -56,6 +66,41 @@ llvm::cl::list<std::string> CXXFlags("cxxflags", |
56 | 66 |
|
57 | 67 | } // namespace |
58 | 68 |
|
| 69 | +static fs::path GetExecutableDir() { |
| 70 | +#if defined(_WIN32) |
| 71 | + char path[MAX_PATH]; |
| 72 | + GetModuleFileNameA(NULL, path, MAX_PATH); |
| 73 | + return fs::path(path).parent_path(); |
| 74 | +#elif defined(__linux__) |
| 75 | + char path[PATH_MAX]; |
| 76 | + ssize_t count = readlink("/proc/self/exe", path, PATH_MAX); |
| 77 | + return fs::path(std::string_view(path, std::max((ssize_t)0, count))) |
| 78 | + .parent_path(); |
| 79 | +#elif defined(__APPLE__) |
| 80 | + uint32_t size = 0; |
| 81 | + _NSGetExecutablePath(nullptr, &size); |
| 82 | + std::vector<char> buffer(size); |
| 83 | + _NSGetExecutablePath(buffer.data(), &size); |
| 84 | + return fs::path(buffer.data()).parent_path(); |
| 85 | +#endif |
| 86 | + return "."; |
| 87 | +} |
| 88 | + |
| 89 | +static bool ResolveRulesDir() { |
| 90 | + std::array<fs::path, 3> candidates = {fs::path("./rules"), |
| 91 | + fs::path("../rules"), |
| 92 | + GetExecutableDir() / "../../rules"}; |
| 93 | + |
| 94 | + for (const auto &dir : candidates) { |
| 95 | + if (fs::exists(dir) && fs::is_directory(dir)) { |
| 96 | + RulesDir = fs::canonical(dir).string(); |
| 97 | + llvm::errs() << "Using rules directory: " << RulesDir << '\n'; |
| 98 | + return true; |
| 99 | + } |
| 100 | + } |
| 101 | + return false; |
| 102 | +} |
| 103 | + |
59 | 104 | int main(int argc, char *argv[]) { |
60 | 105 | llvm::cl::HideUnrelatedOptions(cpp2rust_cmdargs); |
61 | 106 | llvm::cl::ParseCommandLineOptions(argc, argv); |
@@ -104,7 +149,9 @@ int main(int argc, char *argv[]) { |
104 | 149 |
|
105 | 150 | std::vector<std::string_view> cxx_flags(CXXFlags.begin(), CXXFlags.end()); |
106 | 151 |
|
107 | | - if (RulesDir.empty() && !cpp2rust::ResolveRulesDir(RulesDir)) { |
| 152 | + if (RulesDir.empty() && !ResolveRulesDir()) { |
| 153 | + llvm::errs() << "ERROR: rules directory not found. " |
| 154 | + "Please specify one with --rules\n"; |
108 | 155 | return EXIT_FAILURE; |
109 | 156 | } |
110 | 157 |
|
|
0 commit comments