Debian Codegraph @

CodeLite/MSYS2.cpp

raw
1 #include "MSYS2.hpp"
2
3 #include "cl_standard_paths.h"
4
5 #include <functional>
6 #include <wx/arrstr.h>
7 #include <wx/stdpaths.h>
8 #include <wx/tokenzr.h>
9
10 #ifdef __WXMSW__
11 #include <wx/msw/registry.h>
12 #endif
13
14 bool MSYS2::FindInstallDir(wxString* msyspath)
15 {
16 if(m_checked_for_install_dir) {
17 *msyspath = m_install_dir;
18 return !m_install_dir.empty();
19 }
20
21 m_checked_for_install_dir = true;
22
23 wxString reg_install_path;
24 #ifdef __WXMSW__
25 wxRegKey uninstall(wxRegKey::HKCU, R"(SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall)");
26 wxString appname;
27 long dummy;
28 bool cont = uninstall.GetFirstKey(appname, dummy);
29 while(cont) {
30 wxString display_name;
31 wxRegKey appkey(wxRegKey::HKCU, R"(SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\)" + appname);
32 if(appkey.QueryValue("DisplayName", display_name) && display_name == "MSYS2 64bit") {
33 appkey.QueryValue("InstallLocation", reg_install_path);
34 break;
35 }
36 cont = uninstall.GetNextKey(appname, dummy);
37 }
38 #endif
39
40 if(!reg_install_path.empty()) {
41 m_install_dir = reg_install_path;
42 *msyspath = m_install_dir;
43 } else {
44 // try common paths
45 std::vector<wxString> vpaths = { R"(C:\msys64)", R"(C:\msys2)", R"(C:\msys)" };
46 for(const wxString& path : vpaths) {
47 if(wxFileName::DirExists(path)) {
48 m_install_dir = path;
49 *msyspath = m_install_dir;
50 break;
51 }
52 }
53 }
54 return !m_install_dir.empty();
55 }
56
57 bool MSYS2::FindHomeDir(wxString* homedir)
58 {
59 wxString msyspath;
60 if(!FindInstallDir(&msyspath)) {
61 return false;
62 }
63
64 if(m_checked_for_home_dir) {
65 *homedir = m_home_dir;
66 return !m_home_dir.empty();
67 }
68
69 m_checked_for_home_dir = true;
70
71 wxFileName cargo_dir{ msyspath, wxEmptyString };
72 cargo_dir.AppendDir("home");
73 cargo_dir.AppendDir(::wxGetUserId());
74
75 if(cargo_dir.DirExists()) {
76 m_home_dir = cargo_dir.GetPath();
77 }
78
79 *homedir = m_home_dir;
80 return !m_home_dir.empty();
81 }
82
83 bool MSYS2::Which(const wxString& command, wxString* command_fullpath)
84 {
85 wxString msyspath;
86 bool has_msys2 = FindInstallDir(&msyspath);
87
88 wxArrayString paths_to_try;
89
90 // next in order are is the PATH environment variable
91 if(m_flags & SEARCH_PATH_ENV) {
92 wxString pathenv;
93 wxGetEnv("PATH", &pathenv);
94 paths_to_try = ::wxStringTokenize(pathenv, ";", wxTOKEN_STRTOK);
95 }
96
97 // add the executable path (this is how windows work: we first look at the executable path)
98 paths_to_try.Insert(wxFileName(clStandardPaths::Get().GetExecutablePath()).GetPath(), 0);
99
100 // if we have msys2 installed, add the bin folder (we place them at start)
101 if(has_msys2) {
102 for(const auto& root : m_chroots) {
103 paths_to_try.Insert(msyspath + root + R"(\bin)", 0);
104 }
105 }
106
107 // cargo (MSYS2 path)
108 wxFileName cargo_msys_bin{ msyspath, wxEmptyString };
109 cargo_msys_bin.AppendDir("home");
110 cargo_msys_bin.AppendDir(::wxGetUserId());
111 cargo_msys_bin.AppendDir(".cargo");
112 cargo_msys_bin.AppendDir("bin");
113 if(cargo_msys_bin.DirExists()) {
114 paths_to_try.Add(cargo_msys_bin.GetPath());
115 }
116
117 // cargo (Windows native path)
118 // e.g. /c/users/eran/.cargo/bin
119 wxFileName cargo_bin{ ::wxGetHomeDir(), wxEmptyString };
120 cargo_bin.AppendDir(".cargo");
121 cargo_bin.AppendDir("bin");
122
123 if(cargo_bin.DirExists()) {
124 paths_to_try.Add(cargo_bin.GetPath());
125 }
126
127 // local (Windows native path)
128 // e.g. /c/users/eran/.local/bin
129 wxFileName local_bin{ ::wxGetHomeDir(), wxEmptyString };
130 local_bin.AppendDir(".local");
131 local_bin.AppendDir("bin");
132
133 if(local_bin.DirExists()) {
134 paths_to_try.Add(local_bin.GetPath());
135 }
136
137 // at the point, the order of search is:
138 // MSYS2 -> Executable path -> PATH paths
139 for(auto path : paths_to_try) {
140 path << "\\" << command << ".exe";
141 if(wxFileName::FileExists(path)) {
142 *command_fullpath = path;
143 return true;
144 }
145 }
146 return false;
147 }
148
149 bool MSYS2::WhichWithVersion(const wxString& command, const std::vector<int>& versions, wxString* command_fullpath)
150 {
151 return PlatformCommon::WhichWithVersion(command, versions, command_fullpath);
152 }
153
154 void MSYS2::SetChroot(const wxString& chroot)
155 {
156 m_chroots.clear();
157 m_chroots.Add(chroot);
158
159 // exclude PATH from the search path
160 m_flags &= ~SEARCH_PATH_ENV;
161 }
162
163 namespace
164 {
165 thread_local MSYS2 instance;
166 }
167
168 MSYS2* MSYS2::Get() { return &instance; }
169
170 MSYS2::MSYS2()
171 {
172 // last entry -> most important (reverse order)
173 m_chroots.Add(R"(\usr)");
174 m_chroots.Add(R"(\mingw64)");
175 m_chroots.Add(R"(\clang64)");
176 }