|
1 | 1 | // Copyright (c) 2022-present INESC-ID. |
2 | 2 | // Distributed under the MIT license that can be found in the LICENSE file. |
3 | 3 |
|
4 | | -#include <algorithm> |
5 | | -#include <array> |
| 4 | +#include <llvm/Support/CommandLine.h> |
| 5 | + |
6 | 6 | #include <cstdlib> |
7 | 7 | #include <filesystem> |
8 | 8 | #include <fstream> |
9 | 9 | #include <vector> |
10 | | -#if defined(_WIN32) |
11 | | -#include <windows.h> |
12 | | -#elif defined(__linux__) |
13 | | -#include <limits.h> |
14 | | -#include <unistd.h> |
15 | | -#elif defined(__APPLE__) |
16 | | -#include <mach-o/dyld.h> |
17 | | -#endif |
18 | | - |
19 | | -#include <llvm/Support/CommandLine.h> |
20 | 10 |
|
21 | 11 | #include "cpp2rust_lib.h" |
22 | 12 | #include "logging.h" |
| 13 | +#include "rules_dir.h" |
23 | 14 |
|
24 | 15 | namespace fs = std::filesystem; |
25 | 16 |
|
@@ -65,42 +56,6 @@ llvm::cl::list<std::string> CXXFlags("cxxflags", |
65 | 56 |
|
66 | 57 | } // namespace |
67 | 58 |
|
68 | | -// Get the directory of the running executable |
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); // get path length |
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 | | - |
104 | 59 | int main(int argc, char *argv[]) { |
105 | 60 | llvm::cl::HideUnrelatedOptions(cpp2rust_cmdargs); |
106 | 61 | llvm::cl::ParseCommandLineOptions(argc, argv); |
@@ -149,9 +104,7 @@ int main(int argc, char *argv[]) { |
149 | 104 |
|
150 | 105 | std::vector<std::string_view> cxx_flags(CXXFlags.begin(), CXXFlags.end()); |
151 | 106 |
|
152 | | - if (RulesDir.empty() && !ResolveRulesDir()) { |
153 | | - llvm::errs() << "ERROR: rules directory not found. " |
154 | | - "Please specify one with --rules\n"; |
| 107 | + if (RulesDir.empty() && !cpp2rust::ResolveRulesDir(RulesDir)) { |
155 | 108 | return EXIT_FAILURE; |
156 | 109 | } |
157 | 110 |
|
|
0 commit comments