File: macromanager.cpp

package info (click to toggle)
codelite 17.0.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 136,204 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 (446 lines) | stat: -rw-r--r-- 16,196 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright            : (C) 2014 Eran Ifrah
// file name            : macromanager.cpp
//
// -------------------------------------------------------------------------
// A
//              _____           _      _     _ _
//             /  __ \         | |    | |   (_) |
//             | /  \/ ___   __| | ___| |    _| |_ ___
//             | |    / _ \ / _  |/ _ \ |   | | __/ _ )
//             | \__/\ (_) | (_| |  __/ |___| | ||  __/
//              \____/\___/ \__,_|\___\_____/_|\__\___|
//
//                                                  F i l e
//
//    This program is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    (at your option) any later version.
//
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////

#include "macromanager.h"

#include "IWorkspace.h"
#include "build_config.h"
#include "clFileSystemWorkspace.hpp"
#include "clWorkspaceManager.h"
#include "environmentconfig.h"
#include "globals.h"
#include "imanager.h"
#include "project.h"
#include "workspace.h"
#include "wxStringHash.h"

#include <wx/regex.h>

MacroManager::MacroManager() {}

MacroManager::~MacroManager() {}

MacroManager* MacroManager::Instance()
{
    static MacroManager ms_instance;
    return &ms_instance;
}

wxString MacroManager::Expand(const wxString& expression, IManager* manager, const wxString& project,
                              const wxString& confToBuild)
{
    return DoExpand(expression, manager, project, true, confToBuild);
}

wxString MacroManager::Replace(const wxString& inString, const wxString& variableName, const wxString& replaceWith,
                               bool bIgnoreCase)
{
    size_t flags = wxRE_DEFAULT;
    if(bIgnoreCase) {
        flags |= wxRE_ICASE;
    }

    wxString strRe1;
    wxString strRe2;
    wxString strRe3;
    wxString strRe4;

    strRe1 << "\\$\\((" << variableName << ")\\)";
    strRe2 << "\\$\\{(" << variableName << ")\\}";
    strRe3 << "\\$(" << variableName << ")";
    strRe4 << "%(" << variableName << ")%";

    wxRegEx reOne(strRe1, flags);   // $(variable)
    wxRegEx reTwo(strRe2, flags);   // ${variable}
    wxRegEx reThree(strRe3, flags); // $variable
    wxRegEx reFour(strRe4, flags);  // %variable%

    wxString result = inString;
    if(reOne.Matches(result)) {
        reOne.ReplaceAll(&result, replaceWith);
    }

    if(reTwo.Matches(result)) {
        reTwo.ReplaceAll(&result, replaceWith);
    }

    if(reThree.Matches(result)) {
        reThree.ReplaceAll(&result, replaceWith);
    }

    if(reFour.Matches(result)) {
        reFour.ReplaceAll(&result, replaceWith);
    }
    return result;
}

bool MacroManager::FindVariable(const wxString& inString, wxString& name, wxString& fullname)
{
    size_t flags = wxRE_DEFAULT | wxRE_ICASE;

    wxString strRe1;
    wxString strRe2;
    wxString strRe3;
    wxString strRe4;

    strRe1 << "\\$\\(("
           << "[a-z_0-9]+"
           << ")\\)";
    strRe2 << "\\$\\{("
           << "[a-z_0-9]+"
           << ")\\}";
    strRe3 << "\\$("
           << "[a-z_0-9]+"
           << ")";
    strRe4 << "%("
           << "[a-z_0-9]+"
           << ")%";

    wxRegEx reOne(strRe1, flags);   // $(variable)
    wxRegEx reTwo(strRe2, flags);   // ${variable}
    wxRegEx reThree(strRe3, flags); // $variable
    wxRegEx reFour(strRe4, flags);  // %variable%

    if(reOne.Matches(inString)) {
        name = reOne.GetMatch(inString, 1);
        fullname = reOne.GetMatch(inString);
        return true;
    }

    if(reTwo.Matches(inString)) {
        name = reTwo.GetMatch(inString, 1);
        fullname = reTwo.GetMatch(inString);
        return true;
    }

    if(reThree.Matches(inString)) {
        name = reThree.GetMatch(inString, 1);
        fullname = reThree.GetMatch(inString);
        return true;
    }

    if(reFour.Matches(inString)) {
        name = reFour.GetMatch(inString, 1);
        fullname = reFour.GetMatch(inString);
        return true;
    }
    return false;
}

namespace
{
// the below list are macros supported by CodeLite
const std::unordered_set<wxString> CODELITE_MACROS = {
    "WorkspaceName",
    "WorkspaceConfiguration",
    "WorkspacePath",
    "OutputDirectory",
    "ProjectOutputFile",
    "OutputFile",
    "ProjectWorkingDirectory",
    "ProjectRunWorkingDirectory",
    "ProjectPath",
    "ProjectName",
    "IntermediateDirectory",
    "ConfigurationName",
    "OutDir",
    "ProjectFiles",
    "ProjectFilesAbs",
    "CurrentFileName",
    "CurrentFilePath",
    "CurrentFileExt",
    "CurrentFileFullName",
    "CurrentFileFullPath",
    "CurrentFileRelPath",
    "CurrentSelection",
    "CurrentSelectionRange",
    "Program",
    "User",
    "Date",
    "CodeLitePath",
    "CC",
    "CFLAGS",
    "CXX",
    "CXXFLAGS",
    "LDFLAGS",
    "AS",
    "ASFLAGS",
    "RES",
    "RESFLAGS",
    "AR",
    "MAKE",
    "IncludePath",
    "LibraryPath",
    "ResourcePath",
    "LinkLibraries",
    "SSH_AccountName",
    "SSH_Host",
    "SSH_User",
};
} // namespace

wxString MacroManager::ExpandNoEnv(const wxString& expression, const wxString& project, const wxString& confToBuild)
{
    return DoExpand(expression, NULL, project, false, confToBuild);
}

wxString MacroManager::DoExpand(const wxString& expression, IManager* manager, const wxString& project, bool applyEnv,
                                const wxString& confToBuild)
{
    wxString expandedString(expression);
    clCxxWorkspace* workspace = nullptr;

    if(!manager) {
        manager = clGetManager();
    }

    wxString wspName;
    wxString wspConfig;
    wxString wspPath;
    wxString program_to_run;

    // ssh macros
    wxString ssh_host;
    wxString account_name;
    wxString ssh_user;

    auto w = clWorkspaceManager::Get().GetWorkspace();
    if(w && w->IsRemote()) {
        wxString ssh_account = w->GetSshAccount();
        auto account = SSHAccountInfo::LoadAccount(ssh_account);
        ssh_host = account.GetHost();
        account_name = ssh_account;
        ssh_user = account.GetUsername();
    }

    if(clCxxWorkspaceST::Get()->IsOpen()) {
        wspName = clCxxWorkspaceST::Get()->GetName();
        wspConfig = clCxxWorkspaceST::Get()->GetSelectedConfig()
                        ? clCxxWorkspaceST::Get()->GetSelectedConfig()->GetName()
                        : wxString();
        wspPath = clCxxWorkspaceST::Get()->GetDir();
        workspace = clCxxWorkspaceST::Get();
    } else if(clFileSystemWorkspace::Get().IsOpen()) {
        wspName = clFileSystemWorkspace::Get().GetName();
        if(clFileSystemWorkspace::Get().GetSettings().GetSelectedConfig()) {
            program_to_run = clFileSystemWorkspace::Get().GetSettings().GetSelectedConfig()->GetExecutable();
            wspConfig = clFileSystemWorkspace::Get().GetSettings().GetSelectedConfig()->GetName();
        }
        wspPath = clFileSystemWorkspace::Get().GetDir();
    } else if(clWorkspaceManager::Get().IsWorkspaceOpened()) {
        wspPath = clWorkspaceManager::Get().GetWorkspace()->GetDir();
        wspName = clWorkspaceManager::Get().GetWorkspace()->GetName();
    }

    size_t retries = 0;
    wxString dummyname, dummfullname;
    while((retries < 5) && FindVariable(expandedString, dummyname, dummyname)) {
        ++retries;
        DollarEscaper de(expandedString);
        expandedString.Replace("$(WorkspaceName)", wspName);
        expandedString.Replace("$(WorkspaceConfiguration)", wspConfig);
        expandedString.Replace("$(WorkspacePath)", wspPath);

        if(workspace) {
            ProjectPtr proj = workspace->GetProject(project);
            if(proj) {
                wxString prjBuildWd;
                wxString prjRunWd;
                wxString project_name(proj->GetName());

                // make sure that the project name does not contain any spaces
                project_name.Replace(" ", "_");

                BuildConfigPtr bldConf = workspace->GetProjBuildConf(proj->GetName(), confToBuild);
                if(bldConf) {
                    bool isCustom = bldConf->IsCustomBuild();
                    expandedString.Replace("$(OutputDirectory)", bldConf->GetOutputDirectory());
                    expandedString.Replace("$(ProjectOutputFile)", bldConf->GetOutputFileName());
                    // An alias
                    expandedString.Replace("$(OutputFile)", bldConf->GetOutputFileName());
                    expandedString.Replace("$(Program)", bldConf->GetCommand());

                    // When custom build project, use the working directory set in the
                    // custom build tab, otherwise use the project file's path
                    prjBuildWd = isCustom ? bldConf->GetCustomBuildWorkingDir() : proj->GetFileName().GetPath();
                    prjRunWd = bldConf->GetWorkingDirectory();
                }

                expandedString.Replace("$(ProjectWorkingDirectory)", prjBuildWd);
                expandedString.Replace("$(ProjectRunWorkingDirectory)", prjRunWd);
                expandedString.Replace("$(ProjectPath)", proj->GetFileName().GetPath());
                expandedString.Replace("$(WorkspacePath)", workspace->GetWorkspaceFileName().GetPath());
                expandedString.Replace("$(ProjectName)", project_name);

                if(bldConf) {
                    expandedString.Replace("$(IntermediateDirectory)", bldConf->GetIntermediateDirectory());
                    expandedString.Replace("$(ConfigurationName)", bldConf->GetName());
                    expandedString.Replace("$(OutDir)", bldConf->GetIntermediateDirectory());

                    // Compiler-related variables
                    wxString cFlags = bldConf->GetCCompileOptions();
                    cFlags.Replace(";", " ");
                    expandedString.Replace("$(CC)", bldConf->GetCompiler()->GetTool("CC"));
                    expandedString.Replace("$(CFLAGS)", cFlags);

                    wxString cxxFlags = bldConf->GetCompileOptions();
                    cxxFlags.Replace(";", " ");
                    expandedString.Replace("$(CXX)", bldConf->GetCompiler()->GetTool("CXX"));
                    expandedString.Replace("$(CXXFLAGS)", cxxFlags);

                    wxString ldFlags = bldConf->GetLinkOptions();
                    ldFlags.Replace(";", " ");
                    expandedString.Replace("$(LDFLAGS)", ldFlags);

                    wxString asFlags = bldConf->GetAssmeblerOptions();
                    asFlags.Replace(";", " ");
                    expandedString.Replace("$(AS)", bldConf->GetCompiler()->GetTool("AS"));
                    expandedString.Replace("$(ASFLAGS)", asFlags);

                    wxString resFlags = bldConf->GetResCompileOptions();
                    resFlags.Replace(";", " ");
                    expandedString.Replace("$(RES)", bldConf->GetCompiler()->GetTool("ResourceCompiler"));
                    expandedString.Replace("$(RESFLAGS)", resFlags);

                    expandedString.Replace("$(AR)", bldConf->GetCompiler()->GetTool("AR"));

                    expandedString.Replace("$(MAKE)", bldConf->GetCompiler()->GetTool("MAKE"));

                    expandedString.Replace("$(IncludePath)", bldConf->GetIncludePath());
                    expandedString.Replace("$(LibraryPath)", bldConf->GetLibPath());
                    expandedString.Replace("$(ResourcePath)", bldConf->GetResCmpIncludePath());
                    expandedString.Replace("$(LinkLibraries)", bldConf->GetLibraries());
                }

                if(expandedString.Find("$(ProjectFiles)") != wxNOT_FOUND) {
                    expandedString.Replace("$(ProjectFiles)", proj->GetFilesAsString(false));
                }

                if(expandedString.Find("$(ProjectFilesAbs)") != wxNOT_FOUND) {
                    expandedString.Replace("$(ProjectFilesAbs)", proj->GetFilesAsString(true));
                }
            }
        }

        if(!program_to_run.empty()) {
            expandedString.Replace("$(Program)", program_to_run);
        }

        if(manager) {
            IEditor* editor = manager->GetActiveEditor();
            if(editor) {
                wxFileName fn(editor->GetRemotePathOrLocal());

                expandedString.Replace("$(CurrentFileName)", fn.GetName());

                wxString fpath(fn.GetPath());

                // build the relative path
                wxString rel_path = fn.GetFullPath();
                if(w) {
                    wxFileName _f(fn);
                    _f.MakeRelativeTo(w->GetDir());
                    rel_path = _f.GetFullPath(w->IsRemote() ? wxPATH_UNIX : wxPATH_NATIVE);
                }

                fpath.Replace("\\", "/");
                expandedString.Replace("$(CurrentFilePath)", fpath);
                expandedString.Replace("$(CurrentFileExt)", fn.GetExt());
                expandedString.Replace("$(CurrentFileFullName)", fn.GetFullName());
                expandedString.Replace("$(CurrentFileRelPath)", rel_path);

                wxString ffullpath(fn.GetFullPath());
                ffullpath.Replace("\\", "/");
                expandedString.Replace("$(CurrentFileFullPath)", ffullpath);
                expandedString.Replace("$(CurrentSelection)", editor->GetSelection());
                if(expandedString.Find("$(CurrentSelectionRange)") != wxNOT_FOUND) {
                    int start = editor->GetSelectionStart(), end = editor->GetSelectionEnd();

                    wxString output = wxString::Format("%i:%i", start, end);
                    expandedString.Replace("$(CurrentSelectionRange)", output);
                }
            }
        }

        // exapand common macros
        wxDateTime now = wxDateTime::Now();
        expandedString.Replace("$(User)", wxGetUserName());
        expandedString.Replace("$(Date)", now.FormatDate());

        // ssh related
        expandedString.Replace("$(SSH_AccountName)", account_name);
        expandedString.Replace("$(SSH_Host)", ssh_host);
        expandedString.Replace("$(SSH_User)", ssh_user);

        if(manager && applyEnv) {
            expandedString.Replace("$(CodeLitePath)", manager->GetInstallDirectory());

            // Apply the environment and expand the variables
            EnvSetter es(NULL, NULL, project, confToBuild);
            expandedString = manager->GetEnv()->ExpandVariables(expandedString, false);
        } else if(applyEnv) {
            expandedString = EnvironmentConfig::Instance()->ExpandVariables(expandedString, false);
        }
    }
    return expandedString;
}

bool MacroManager::IsCodeLiteMacro(const wxString& macroname) const { return CODELITE_MACROS.count(macroname) != 0; }

wxString MacroManager::ExpandFileMacros(const wxString& expression, const wxString& filepath)
{
    wxString wsp_dir;
    bool is_remote = false;
    auto workspace = clWorkspaceManager::Get().GetWorkspace();
    if(workspace) {
        is_remote = workspace->IsRemote();
        wsp_dir = workspace->GetDir();
    }

    // replace file macros
    wxString filepath_relative;
    wxString fullname;
    wxString filedir;
    wxString fullpath = filepath;

    wxFileName fn{ filepath };
    if(!wsp_dir.empty()) {
        fn.MakeRelativeTo(wsp_dir);
    }

    filepath_relative = fn.GetFullPath(is_remote ? wxPATH_UNIX : wxPATH_NATIVE);
    fullname = fn.GetFullName();
    filedir = fn.GetPath(is_remote ? wxPATH_UNIX : wxPATH_NATIVE);

    EnvSetter env;
    wxString tmp_expr = expression;
    tmp_expr.Replace("$(CurrentFileName)", fn.GetName());
    tmp_expr.Replace("$(CurrentFilePath)", filedir);
    tmp_expr.Replace("$(CurrentFileExt)", fn.GetExt());
    tmp_expr.Replace("$(CurrentFileFullName)", fullname);
    tmp_expr.Replace("$(CurrentFileFullPath)", fullpath);
    tmp_expr.Replace("$(CurrentFileRelPath)", filepath_relative);
    return tmp_expr;
}