File: cl_make_generator_app.cpp

package info (click to toggle)
codelite 14.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 112,816 kB
  • sloc: cpp: 483,662; ansic: 150,144; php: 9,569; lex: 4,186; python: 3,417; yacc: 2,820; sh: 1,147; makefile: 52; xml: 13
file content (299 lines) | stat: -rw-r--r-- 10,888 bytes parent folder | download
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
#include "cl_make_generator_app.h"
#include <workspace.h>
#include <wx/filename.h>
#include <builder_gnumake.h>
#include <configuration_mapping.h>
#include <macromanager.h>
#include <wx/crt.h>
#include <globals.h>
#include <build_settings_config.h>
#include <algorithm>

#include <builder_gnumake_default.h>

IMPLEMENT_APP_CONSOLE(clMakeGeneratorApp)

static const wxCmdLineEntryDesc g_cmdDesc[] = {
    { wxCMD_LINE_SWITCH, "h", "help", "show this help message", wxCMD_LINE_VAL_NONE, wxCMD_LINE_OPTION_HELP },
    { wxCMD_LINE_OPTION, "w", "workspace", "CodeLite workspace file", wxCMD_LINE_VAL_STRING,
      wxCMD_LINE_OPTION_MANDATORY },
    { wxCMD_LINE_OPTION, "c", "config", "Workspace configuration name to use", wxCMD_LINE_VAL_STRING,
      wxCMD_LINE_OPTION_MANDATORY },
    { wxCMD_LINE_OPTION, "d", "command",
      "Which command to run? possible values are: build, clean or rebuild. The default is to build" },
    { wxCMD_LINE_OPTION, "p", "project",
      "Project to build, if non given CodeLite will build the active project as defined in the workspace",
      wxCMD_LINE_VAL_STRING },
    { wxCMD_LINE_SWITCH, "v", "verbose", "Run in verbose mode and print all log lines to the stdout/stderr" },
    { wxCMD_LINE_SWITCH, "j", "json",
      "Generate compile_commands.json for this workspace and exit. If not specified, compile_commands.json is "
      "generated as part of the build process" },
    { wxCMD_LINE_SWITCH, "f", "compile-flags", "Generate compile_flags.txt for this workspace and exit" },
    { wxCMD_LINE_SWITCH, "e", "execute", "Instead of printing the command line, execute it" },
    { wxCMD_LINE_OPTION, "s", "settings",
      "The full path of the build_settings.xml file.\n"
      "By default, codelite-make will load the compiler definitions from\n"
      "%appdata%\\CodeLite\\config\\build_settings.xml (or the equivalent path on\n"
      "Unix systems). Passing -s|--settings will override the default search\n"
      "location",
      wxCMD_LINE_VAL_STRING },
    { wxCMD_LINE_NONE }
};

clMakeGeneratorApp::clMakeGeneratorApp()
    : m_verbose(false)
    , m_executeCommand(false)
    , m_exitCode(0)
    , m_commandType(kBuild)
{
}

clMakeGeneratorApp::~clMakeGeneratorApp() {}

int clMakeGeneratorApp::OnExit()
{
    BuildSettingsConfigST::Free();
    return TRUE;
}

bool clMakeGeneratorApp::OnInit()
{
    SetAppName("codelite");
    wxLog::EnableLogging(false);
    wxCmdLineParser parser(wxAppConsole::argc, wxAppConsole::argv);
    if(!DoParseCommandLine(parser)) return false;

    // Load compilers settings
    if(!BuildSettingsConfigST::Get()->Load("2.1", m_buildSettingsXml)) {
        Error("Could not load build settings configuration object (Version 2.1 / build_settings.xml)");
        return false;
    }

    wxFileName fnWorkspace(m_workspaceFile);
    if(fnWorkspace.IsRelative()) { fnWorkspace.MakeAbsolute(m_workingDirectory); }

    Info(wxString() << "-- Generating makefile for workspace file " << fnWorkspace.GetFullPath());
    wxString errmsg;
    if(!clCxxWorkspaceST::Get()->OpenWorkspace(fnWorkspace.GetFullPath(), errmsg)) {
        Error(wxString() << "Error while loading workspace: " << fnWorkspace.GetFullPath() << ". " << errmsg);
        return false;
    }

    if(m_project.IsEmpty()) { m_project = clCxxWorkspaceST::Get()->GetActiveProjectName(); }

    // Set the active project to the configuration set the by the user
    BuildMatrixPtr buildMatrix = clCxxWorkspaceST::Get()->GetBuildMatrix();
    WorkspaceConfigurationPtr workspaceConfig = buildMatrix->GetConfigurationByName(m_configuration);
    if(!workspaceConfig) {
        Error(wxString() << "Could not find workspace configuration: " << m_configuration);
        return false;
    }
    const WorkspaceConfiguration::ConfigMappingList& mapping = workspaceConfig->GetMapping();
    WorkspaceConfiguration::ConfigMappingList::const_iterator iter = std::find_if(
        mapping.begin(), mapping.end(), [&](const ConfigMappingEntry& entry) { return entry.m_project == m_project; });
    if(iter == mapping.end()) {
        Error(wxString() << "Unable to locate a project configuration that matches the workspace configuration '"
                         << m_configuration << "'");
        return false;
    }
    buildMatrix->SetSelectedConfigurationName(m_configuration);
    wxString projectConfiguraion = iter->m_name;
    Info(wxString() << "-- Building project configuration: " << projectConfiguraion);

    // Which makefile should we create?
    BuilderGnuMake builder;
    ProjectPtr project = clCxxWorkspaceST::Get()->FindProjectByName(m_project, errmsg);
    if(!project) {
        Error(wxString() << "Could not find project " << m_project << ". " << errmsg);
        return false;
    }

    // Load the build configuration
    BuildConfigPtr bldConf = clCxxWorkspaceST::Get()->GetProjBuildConf(m_project, projectConfiguraion);
    if(!bldConf) {
        Error(wxString() << "Could not find configuration " << projectConfiguraion << " for project " << m_project);
        return false;
    }

    // First, generate the compile_commands.json
    DoGenerateCompileCommands();

    if(this->m_generateCompileCommands || this->m_generateCompilerFlags) {
        // If the --json flag was passed, exit now
        Bye();
    } else {
        if(bldConf->IsCustomBuild()) {
            Notice(wxString() << "Configuration " << projectConfiguraion << " for project " << m_project
                              << " is using a custom build - will not generate makefile");
            Notice(wxString() << "Instead, here is the command line to use:");
            wxString command;
            wxString workingDirectory = MacroManager::Instance()->Expand(bldConf->GetCustomBuildWorkingDir(), NULL,
                                                                         m_project, bldConf->GetName());
            if(wxFileName::DirExists(workingDirectory) == false) {
                Info(wxString() << "-- Creating build directory: " << workingDirectory);
                wxFileName::Mkdir(workingDirectory, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL);
            }
            command << "cd " << workingDirectory << " && "
                    << MacroManager::Instance()->Expand(bldConf->GetCustomBuildCmd(), NULL, m_project,
                                                        bldConf->GetName());
            Out(command);
            if(m_executeCommand) {
                CallAfter(&clMakeGeneratorApp::DoExecCommand, command);
            } else {
                Bye();
            }
            return true;
        }

        wxString args = bldConf->GetBuildSystemArguments();
        if(!builder.Export(m_project, projectConfiguraion, args, false, true, errmsg)) {
            Error(wxString() << "Error while exporting makefile. " << errmsg);
            return false;
        }

        wxString commandToRun;
        switch(m_commandType) {
        case kBuild:
            commandToRun = builder.GetBuildCommand(m_project, projectConfiguraion, args);
            break;
        case kClean:
            commandToRun = builder.GetCleanCommand(m_project, projectConfiguraion, args);
            break;
        case kRebuild:
            commandToRun = builder.GetCleanCommand(m_project, projectConfiguraion, args);
            // append the build command
            commandToRun << " && " << builder.GetBuildCommand(m_project, projectConfiguraion, args);
            break;
        }

        wxString workspace_path = fnWorkspace.GetPath();
        if(workspace_path.Contains(" ") || workspace_path.Contains("\t")) { workspace_path.Prepend("\"").Append("\""); }

        Info("-- Makefile generation completed successfully!");
        wxString command;
        command << "cd " << workspace_path << " && " << commandToRun;

        if(m_executeCommand) {
            CallAfter(&clMakeGeneratorApp::DoExecCommand, command);

        } else {
            Info("-- To use the makefile, run the following commands from a terminal:");
            Out(command);
            Bye();
        }
    }
    return true;
}

bool clMakeGeneratorApp::DoParseCommandLine(wxCmdLineParser& parser)
{
    parser.SetDesc(g_cmdDesc);
    parser.AddUsageText(_("A makefile generator based on codelite's workspace"));

    int res = parser.Parse(false);
    if(res == wxNOT_FOUND) return false;

    if(!parser.Found("w", &m_workspaceFile)) {
        parser.Usage();
        return false;
    }

    if(!parser.Found("c", &m_configuration)) {
        parser.Usage();
        return false;
    }

    if(parser.Found("e")) { m_executeCommand = true; }

    parser.Found("s", &m_buildSettingsXml);
    m_generateCompileCommands = (parser.FoundSwitch("j") == wxCMD_SWITCH_ON);
    m_generateCompilerFlags = (parser.FoundSwitch("f") == wxCMD_SWITCH_ON);

    wxString command;
    if(parser.Found("d", &command)) {
        if(command == "build") {
            m_commandType = kBuild;
        } else if(command == "rebuild") {
            m_commandType = kRebuild;
        } else if(command == "clean") {
            m_commandType = kClean;
        } else {
            parser.Usage();
            return false;
        }
    }

    parser.Found("p", &m_project);
    m_verbose = (parser.FoundSwitch("v") == wxCMD_SWITCH_ON);
    m_workingDirectory = ::wxGetCwd();
    return true;
}

void clMakeGeneratorApp::DoExitApp()
{
    ExitMainLoop();
    // Force an exit here
}

// Log functions
void clMakeGeneratorApp::Error(const wxString& msg)
{
    wxString e;
    e << "[ERROR ] " << msg;
    wxFprintf(stderr, "%s\n", e);
}

void clMakeGeneratorApp::Notice(const wxString& msg)
{
    if(m_verbose) {
        wxString e;
        e << "[NOTICE] " << msg;
        wxFprintf(stderr, "%s\n", e);
    }
}

void clMakeGeneratorApp::Info(const wxString& msg)
{
    if(m_verbose) {
        wxString e;
        e << "[INFO  ] " << msg;
        wxFprintf(stdout, "%s\n", e);
    }
}

void clMakeGeneratorApp::Out(const wxString& msg) { wxPrintf("%s\n", msg); }

void clMakeGeneratorApp::DoExecCommand(const wxString& command)
{
    wxString cmd = command;
    WrapInShell(cmd);
    wxPrintf(cmd + "\n");
    m_exitCode = ::wxExecute(cmd, wxEXEC_SYNC | wxEXEC_NOHIDE | wxEXEC_SHOW_CONSOLE);
    Bye();
}

void clMakeGeneratorApp::DoGenerateCompileCommands()
{
    wxFileName fn(clCxxWorkspaceST::Get()->GetFileName());
    fn.SetFullName("compile_commands.json");

    if(m_generateCompileCommands) {
        Info(wxString() << "-- Generating: " << fn.GetFullPath());
    } else {
        Info(wxString() << "-- Generating: compile_flags.txt files...");
    }
    JSON json(clCxxWorkspaceST::Get()->CreateCompileCommandsJSON(!m_generateCompileCommands));
    if(json.isOk()) {
        // Save the file
        json.save(fn);
    }
}

void clMakeGeneratorApp::Bye()
{
#ifdef __WXOSX__
    _exit(0);
#else
    CallAfter(&clMakeGeneratorApp::DoExitApp);
#endif
}