File: cl_make_generator_app.cpp

package info (click to toggle)
codelite 6.1.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 48,992 kB
  • ctags: 43,502
  • sloc: cpp: 334,263; ansic: 18,441; xml: 4,713; yacc: 2,653; lex: 2,449; python: 1,188; sh: 385; makefile: 40
file content (236 lines) | stat: -rw-r--r-- 7,534 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
#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>

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", "configuration name to generate", wxCMD_LINE_VAL_STRING, wxCMD_LINE_OPTION_MANDATORY },
    { wxCMD_LINE_OPTION, "d", "command", "which command to run? possible values: 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", wxCMD_LINE_VAL_STRING },
    { wxCMD_LINE_SWITCH, "v", "verbose",  "Run in verbose move and print all log lines to the stdout/stderr" },
    { wxCMD_LINE_SWITCH, "e", "execute",  "Instead of printing the command line, execute it" },
    { 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") ) {
        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 ( !WorkspaceST::Get()->OpenWorkspace(fnWorkspace.GetFullPath(), errmsg) ) {
        Error(wxString() << "Error while loading workspace: " << fnWorkspace.GetFullPath() << ". " << errmsg);
        return false;
    }

    if ( m_project.IsEmpty() ) {
        m_project = WorkspaceST::Get()->GetActiveProjectName();
    }
    
    // Set the active project to the configuration set the by the user
    WorkspaceST::Get()->GetBuildMatrix()->SetSelectedConfigurationName( m_configuration );
    
    // Which makefile should we create?
    BuilderGnuMake builder;
    ProjectPtr project = WorkspaceST::Get()->FindProjectByName(m_project, errmsg);
    if ( !project ) {
        Error(wxString() << "Could not find project " << m_project << ". " << errmsg);
        return false;
    }

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

    if ( bldConf->IsCustomBuild() ) {
        Notice(wxString() << "Configuration " << m_configuration << " 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;
        command << "cd " << MacroManager::Instance()->Expand(bldConf->GetCustomBuildWorkingDir(), NULL, m_project, m_configuration)
                << " && "
                << MacroManager::Instance()->Expand(bldConf->GetCustomBuildCmd(), NULL, m_project, m_configuration);
        Out( command );
        if ( m_executeCommand ) {
            CallAfter( &clMakeGeneratorApp::DoExecCommand, command );
            
        } else {
            CallAfter( &clMakeGeneratorApp::DoExitApp);
            
        }
        return true;
    }

    if ( !builder.Export(m_project, m_configuration, false, true, errmsg) ) {
        Error(wxString() << "Error while exporting makefile. " << errmsg);
        return false;
    }

    wxString commandToRun;
    switch ( m_commandType ) {
    case kBuild:
        commandToRun = builder.GetBuildCommand(m_project, m_configuration);
        break;
    case kClean:
        commandToRun = builder.GetCleanCommand(m_project, m_configuration);
        break;
    case kRebuild:
        commandToRun = builder.GetCleanCommand(m_project, m_configuration);
        // append the build command
        commandToRun << " && " << builder.GetBuildCommand(m_project, m_configuration);
        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 );
        CallAfter( &clMakeGeneratorApp::DoExitApp);
    }
    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;
    }
    
    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
    exit(m_exitCode);
}

// 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);
    CallAfter( &clMakeGeneratorApp::DoExitApp );
}