File: MSYS2.cpp

package info (click to toggle)
codelite 17.0.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 136,244 kB
  • sloc: cpp: 491,547; ansic: 280,393; php: 10,259; sh: 8,930; lisp: 7,664; vhdl: 6,518; python: 6,020; lex: 4,920; yacc: 3,123; perl: 2,385; javascript: 1,715; cs: 1,193; xml: 1,110; makefile: 804; cobol: 741; sql: 709; ruby: 620; f90: 566; ada: 534; asm: 464; fortran: 350; objc: 289; tcl: 258; java: 157; erlang: 61; pascal: 51; ml: 49; awk: 44; haskell: 36
file content (176 lines) | stat: -rw-r--r-- 4,812 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include "MSYS2.hpp"

#include "cl_standard_paths.h"

#include <functional>
#include <wx/arrstr.h>
#include <wx/stdpaths.h>
#include <wx/tokenzr.h>

#ifdef __WXMSW__
#include <wx/msw/registry.h>
#endif

bool MSYS2::FindInstallDir(wxString* msyspath)
{
    if(m_checked_for_install_dir) {
        *msyspath = m_install_dir;
        return !m_install_dir.empty();
    }

    m_checked_for_install_dir = true;

    wxString reg_install_path;
#ifdef __WXMSW__
    wxRegKey uninstall(wxRegKey::HKCU, R"(SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall)");
    wxString appname;
    long dummy;
    bool cont = uninstall.GetFirstKey(appname, dummy);
    while(cont) {
        wxString display_name;
        wxRegKey appkey(wxRegKey::HKCU, R"(SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\)" + appname);
        if(appkey.QueryValue("DisplayName", display_name) && display_name == "MSYS2 64bit") {
            appkey.QueryValue("InstallLocation", reg_install_path);
            break;
        }
        cont = uninstall.GetNextKey(appname, dummy);
    }
#endif

    if(!reg_install_path.empty()) {
        m_install_dir = reg_install_path;
        *msyspath = m_install_dir;
    } else {
        // try common paths
        std::vector<wxString> vpaths = { R"(C:\msys64)", R"(C:\msys2)", R"(C:\msys)" };
        for(const wxString& path : vpaths) {
            if(wxFileName::DirExists(path)) {
                m_install_dir = path;
                *msyspath = m_install_dir;
                break;
            }
        }
    }
    return !m_install_dir.empty();
}

bool MSYS2::FindHomeDir(wxString* homedir)
{
    wxString msyspath;
    if(!FindInstallDir(&msyspath)) {
        return false;
    }

    if(m_checked_for_home_dir) {
        *homedir = m_home_dir;
        return !m_home_dir.empty();
    }

    m_checked_for_home_dir = true;

    wxFileName cargo_dir{ msyspath, wxEmptyString };
    cargo_dir.AppendDir("home");
    cargo_dir.AppendDir(::wxGetUserId());

    if(cargo_dir.DirExists()) {
        m_home_dir = cargo_dir.GetPath();
    }

    *homedir = m_home_dir;
    return !m_home_dir.empty();
}

bool MSYS2::Which(const wxString& command, wxString* command_fullpath)
{
    wxString msyspath;
    bool has_msys2 = FindInstallDir(&msyspath);

    wxArrayString paths_to_try;

    // next in order are is the PATH environment variable
    if(m_flags & SEARCH_PATH_ENV) {
        wxString pathenv;
        wxGetEnv("PATH", &pathenv);
        paths_to_try = ::wxStringTokenize(pathenv, ";", wxTOKEN_STRTOK);
    }

    // add the executable path (this is how windows work: we first look at the executable path)
    paths_to_try.Insert(wxFileName(clStandardPaths::Get().GetExecutablePath()).GetPath(), 0);

    // if we have msys2 installed, add the bin folder (we place them at start)
    if(has_msys2) {
        for(const auto& root : m_chroots) {
            paths_to_try.Insert(msyspath + root + R"(\bin)", 0);
        }
    }

    // cargo (MSYS2 path)
    wxFileName cargo_msys_bin{ msyspath, wxEmptyString };
    cargo_msys_bin.AppendDir("home");
    cargo_msys_bin.AppendDir(::wxGetUserId());
    cargo_msys_bin.AppendDir(".cargo");
    cargo_msys_bin.AppendDir("bin");
    if(cargo_msys_bin.DirExists()) {
        paths_to_try.Add(cargo_msys_bin.GetPath());
    }

    // cargo (Windows native path)
    // e.g. /c/users/eran/.cargo/bin
    wxFileName cargo_bin{ ::wxGetHomeDir(), wxEmptyString };
    cargo_bin.AppendDir(".cargo");
    cargo_bin.AppendDir("bin");

    if(cargo_bin.DirExists()) {
        paths_to_try.Add(cargo_bin.GetPath());
    }

    // local (Windows native path)
    // e.g. /c/users/eran/.local/bin
    wxFileName local_bin{ ::wxGetHomeDir(), wxEmptyString };
    local_bin.AppendDir(".local");
    local_bin.AppendDir("bin");

    if(local_bin.DirExists()) {
        paths_to_try.Add(local_bin.GetPath());
    }

    // at the point, the order of search is:
    // MSYS2 -> Executable path -> PATH paths
    for(auto path : paths_to_try) {
        path << "\\" << command << ".exe";
        if(wxFileName::FileExists(path)) {
            *command_fullpath = path;
            return true;
        }
    }
    return false;
}

bool MSYS2::WhichWithVersion(const wxString& command, const std::vector<int>& versions, wxString* command_fullpath)
{
    return PlatformCommon::WhichWithVersion(command, versions, command_fullpath);
}

void MSYS2::SetChroot(const wxString& chroot)
{
    m_chroots.clear();
    m_chroots.Add(chroot);

    // exclude PATH from the search path
    m_flags &= ~SEARCH_PATH_ENV;
}

namespace
{
thread_local MSYS2 instance;
}

MSYS2* MSYS2::Get() { return &instance; }

MSYS2::MSYS2()
{
    // last entry -> most important (reverse order)
    m_chroots.Add(R"(\usr)");
    m_chroots.Add(R"(\mingw64)");
    m_chroots.Add(R"(\clang64)");
}