Debian Codegraph @

CodeLite/LINUX.cpp

raw
1 #include "LINUX.hpp"
2
3 #include "PlatformCommon.hpp"
4 #include "clFilesCollector.h"
5 #include "clVersionString.hpp"
6 #include "file_logger.h"
7 #include "procutils.h"
8
9 #include <algorithm>
10 #include <cmath>
11 #include <wx/arrstr.h>
12 #include <wx/filename.h>
13 #include <wx/string.h>
14 #include <wx/tokenzr.h>
15 #include <wx/utils.h>
16
17 namespace
18 {
19 #ifdef __WXMAC__
20 /// Homebrew install formulas in a specific location, this function
21 /// attempts to discover this location
22 bool macos_find_homebrew_cellar_path_for_formula(const wxString& formula, wxString* install_path)
23 {
24 wxString cellar_path = "/opt/homebrew/Cellar";
25 if (!wxFileName::DirExists(cellar_path)) {
26 cellar_path = "/usr/local/Cellar";
27 }
28
29 cellar_path << "/" << formula;
30
31 if(!wxFileName::DirExists(cellar_path)) {
32 return false;
33 }
34
35 // we take the string with the highest value
36 clFilesScanner fs;
37 clFilesScanner::EntryData::Vec_t results;
38 if(fs.ScanNoRecurse(cellar_path, results) == 0) {
39 return false;
40 }
41
42 // we are only interested in the name part
43 for(auto& result : results) {
44 result.fullpath = result.fullpath.AfterLast('/');
45 }
46
47 std::sort(results.begin(), results.end(),
48 [](const clFilesScanner::EntryData& a, const clFilesScanner::EntryData& b) {
49 clVersionString vs_a{ a.fullpath };
50 clVersionString vs_b{ b.fullpath };
51 // want to sort in descending order
52 return vs_b.to_number() < vs_a.to_number();
53 });
54
55 *install_path << cellar_path << "/" << results[0].fullpath;
56 clDEBUG() << "Using cellar path:" << *install_path << endl;
57 return true;
58 }
59 #endif
60 } // namespace
61
62 /// Locate rustup bin folder
63 /// the path is set to:
64 /// $HOME/.rustup/toolchains/TOOLCHAIN-NAME/bin
65 bool LINUX::get_rustup_bin_folder(wxString* rustup_bin_dir)
66 {
67 if(rust_toolchain_scanned) {
68 *rustup_bin_dir = RUST_TOOLCHAIN_BIN;
69 return !RUST_TOOLCHAIN_BIN.empty();
70 }
71
72 FindRustupToolchainBinDir(&RUST_TOOLCHAIN_BIN);
73 rust_toolchain_scanned = true;
74
75 // call this method again, this time rust_toolchain_scanned is set to true
76 return get_rustup_bin_folder(rustup_bin_dir);
77 }
78
79 bool LINUX::FindInstallDir(wxString* installpath)
80 {
81 *installpath = "/";
82 return true;
83 }
84
85 bool LINUX::FindHomeDir(wxString* homedir)
86 {
87 #ifdef __WXMAC__
88 *homedir << "/Users/" << ::wxGetUserId();
89 #else
90 *homedir << "/home/" << ::wxGetUserId();
91 #endif
92 return true;
93 }
94
95 bool LINUX::Which(const wxString& command, wxString* command_fullpath)
96 {
97 wxString HOME;
98 FindHomeDir(&HOME);
99
100 wxArrayString special_paths;
101
102 #ifdef __WXMAC__
103 // macOS only
104 special_paths.Add("/opt/homebrew/bin");
105 #endif
106
107 #ifdef __WXGTK__
108 special_paths.Add(wxString() << HOME << "/.local/bin");
109 // linux also supports homebrew
110 if(wxFileName::DirExists("/home/linuxbrew/.linuxbrew/bin")) {
111 special_paths.Add("/home/linuxbrew/.linuxbrew/bin");
112 }
113 #endif
114
115 // cargo
116 special_paths.Add(wxString() << HOME << "/.cargo/bin");
117
118 // rustup
119 wxString rustup_bin_folder;
120 if(get_rustup_bin_folder(&rustup_bin_folder)) {
121 special_paths.Add(rustup_bin_folder);
122 }
123
124 // /usr/local/bin is not always in the PATH, so add it
125 special_paths.Add("/usr/local/bin");
126
127 // common paths: read the env PATH and append the other paths to it
128 // so common paths found ENV:PATH will come first
129 wxArrayString paths;
130 if(m_flags & SEARCH_PATH_ENV) {
131 wxString pathenv;
132 ::wxGetEnv("PATH", &pathenv);
133 paths = ::wxStringTokenize(pathenv, ":", wxTOKEN_STRTOK);
134 }
135
136 // append the special paths to the end
137 paths.insert(paths.end(), special_paths.begin(), special_paths.end());
138
139 #ifdef __WXMAC__
140 // llvm is placed under a special location
141 // if we find it, we place it first
142 wxString llvm_path;
143 if(macos_find_homebrew_cellar_path_for_formula("llvm", &llvm_path)) {
144 paths.Insert(llvm_path + "/bin", 0);
145 }
146 #endif
147
148 // remove duplicate entries
149 wxStringSet_t S;
150 wxArrayString unique_paths;
151 unique_paths.reserve(paths.size());
152 for(const auto& path : paths) {
153 if(S.count(path) == 0) {
154 S.insert(path);
155 unique_paths.Add(path);
156 }
157 }
158
159 paths.swap(unique_paths);
160
161 for(auto path : paths) {
162 path << "/" << command;
163 if(wxFileName::FileExists(path)) {
164 *command_fullpath = path;
165 return true;
166 }
167 }
168 return false;
169 }
170
171 bool LINUX::WhichWithVersion(const wxString& command, const std::vector<int>& versions, wxString* command_fullpath)
172 {
173 return PlatformCommon::WhichWithVersion(command, versions, command_fullpath);
174 }
175
176 namespace
177 {
178 thread_local LINUX instance;
179 }
180 LINUX* LINUX::Get() { return &instance; }