File: qmakegenerator.cpp

package info (click to toggle)
codelite 12.0+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 95,112 kB
  • sloc: cpp: 424,040; ansic: 18,284; php: 9,569; lex: 4,186; yacc: 2,820; python: 2,294; sh: 312; makefile: 51; xml: 13
file content (329 lines) | stat: -rw-r--r-- 12,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
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
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright            : (C) 2014 Eran Ifrah
// file name            : qmakegenerator.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 "build_settings_config.h"
#include "compiler.h"
#include "fileextmanager.h"
#include "globals.h"
#include "imanager.h"
#include "macromanager.h"
#include "project.h"
#include "qmakegenerator.h"
#include "qmakeplugindata.h"
#include "workspace.h"
#include "wxmd5.h"
#include <wx/ffile.h>
#include <wx/tokenzr.h>

wxString QMakeProFileGenerator::prepareVariable(const wxString& variable)
{
    wxString var = ExpandAllVariables(variable, m_manager->GetWorkspace(), m_project, m_configuration, wxEmptyString);
    var.Replace(wxT("\\"), wxT("/"));
    return var;
}

QMakeProFileGenerator::QMakeProFileGenerator(IManager* manager, const wxString& project, const wxString& configuration)
    : m_manager(manager)
    , m_project(project)
    , m_configuration(configuration)
{
}

QMakeProFileGenerator::~QMakeProFileGenerator() {}

bool QMakeProFileGenerator::Generate()
{
    wxString errMsg;
    wxString pro_file;
    QmakePluginData::BuildConfPluginData bcpd;
    bool needRegenration(true);

    ProjectPtr p = m_manager->GetWorkspace()->FindProjectByName(m_project, errMsg);
    if(!p) { return false; }

    wxString rawData = p->GetPluginData(wxT("qmake"));
    QmakePluginData pd(rawData);
    if(!pd.GetDataForBuildConf(m_configuration, bcpd)) { return false; }

    // Get codelite's build configuration
    ProjectSettingsPtr settings = p->GetSettings();
    BuildConfigPtr bldConf = settings->GetBuildConfiguration(m_configuration);
    if(!bldConf) { return false; }

    m_makefilePath = p->GetFileName().GetPath(wxPATH_GET_SEPARATOR | wxPATH_GET_VOLUME);
    m_makefilePath += p->GetName();
    m_makefilePath += wxT(".mk");

    pro_file << wxT("##########################################\n");
    pro_file << wxT("# codelite's qmake configuration: ") << bcpd.m_qmakeConfig << wxT("\n");
    pro_file << wxT("# codelite's qmake exec line    : ") << bcpd.m_qmakeExecutionLine << wxT("\n");
    pro_file << wxT("##########################################\n");

    // Generate the pro file content
    pro_file << wxT("DESTDIR        = ") << p->GetFileName().GetPath(wxPATH_NO_SEPARATOR | wxPATH_GET_VOLUME)
             << wxT("\n");
    SetVariables(pro_file, bldConf, settings);
    SetFiles(pro_file, p);

    pro_file << wxT("##########################################\n");
    pro_file << wxT("# User section\n");
    pro_file << wxT("##########################################\n");

    // Expands any macros available in the user free text
    wxString expandedText = MacroManager::Instance()->Expand(bcpd.m_freeText, m_manager, p->GetName(), m_configuration);
    pro_file << wxT("\n") << expandedText << wxT("\n");

    // incase we are building library, set the template back the lib
    wxString type = p->GetSettings()->GetProjectType(bldConf->GetName());
    if(type == PROJECT_TYPE_DYNAMIC_LIBRARY || type == PROJECT_TYPE_STATIC_LIBRARY) {
        pro_file << wxT("TEMPLATE       = lib\n");
    }

    // Optimization: check to see if we really need to run qmake
    if(wxFileName::FileExists(m_makefilePath) == false) {
        needRegenration = true;
    } else if(wxFileName::FileExists(GetProFileName()) == false) {
        needRegenration = true;
    } else {

        // both files exists, compare the MD5 of the pro file on the disk with the
        wxString content;
        if(!ReadFileWithConversion(GetProFileName(), content)) {
            needRegenration = true;
        } else {
            wxString diskMD5 = wxMD5::GetDigest(content);
            wxString mem_MD5 = wxMD5::GetDigest(pro_file);

            if(diskMD5 != mem_MD5) {
                needRegenration = true;
            } else {
                needRegenration = false;
            }
        }
    }

    // dump the content to a file
    wxFFile output;
    output.Open(GetProFileName(), wxT("w+b"));
    if(output.IsOpened()) {
        output.Write(pro_file);
        output.Close();
    }

    return needRegenration;
}

void QMakeProFileGenerator::SetVariables(wxString& pro_file, BuildConfigPtr bldConf, ProjectSettingsPtr settings)
{
    //--------------------------------------------
    // Define the TEMPLATE variable
    //--------------------------------------------
    wxString type = settings->GetProjectType(bldConf->GetName());

    // since LIBS does not add the -L, we need to add it ourselves here
    CompilerPtr cmp = m_manager->GetBuildSettingsConfigManager()->GetCompiler(bldConf->GetCompilerType());

    if(type == PROJECT_TYPE_EXECUTABLE) {
        pro_file << wxT("TEMPLATE       = app\n");
        pro_file << wxT("CONFIG         = release qt lex yacc uic resources warn_on precompile_header\n");

    } else if(type == PROJECT_TYPE_DYNAMIC_LIBRARY) {
        pro_file << wxT("TEMPLATE       = lib\n");
        pro_file << wxT("CONFIG         = release qt dll lex yacc uic resources warn_on precompile_header\n");

    } else {
        pro_file << wxT("TEMPLATE       = lib\n");
        pro_file << wxT("CONFIG         = release qt staticlib lex yacc uic resources warn_on precompile_header\n");
    }

    pro_file << wxT("MAKEFILE       = ") << m_makefilePath << wxT("\n");

    //--------------------------------------------
    // Define the intermediate directory and
    // the output file
    //--------------------------------------------
    pro_file << wxT("OBJECTS_DIR    = ") << bldConf->GetIntermediateDirectory() << wxT("\n");
    pro_file << wxT("TARGET         = ") << prepareVariable(bldConf->GetOutputFileName()) << wxT("\n");

    //--------------------------------------------
    // Prepare the include path
    //--------------------------------------------
    wxString includes = bldConf->GetIncludePath();
    includes.Replace(wxT(";"), wxT(" "));
    pro_file << wxT("INCLUDEPATH    = ") << includes << wxT("\n");

    // Add the global include path
    wxArrayString includes_list;
    includes.Clear();
    if(cmp) {
        includes = cmp->GetGlobalIncludePath();
        includes.Trim().Trim(false);
        if(includes.IsEmpty() == false) {
            // Get list of include paths
            includes_list = wxStringTokenize(includes, ";", wxTOKEN_STRTOK);
            /*			includes.Replace(wxT(";"), wxT(" "));
                        pro_file << wxT("INCLUDEPATH   += ") << includes << wxT("\n");*/
        }
    }

    includes.Clear();
    // Append modified values
    includes << wxT("INCLUDEPATH   += ");
    for(size_t i = 0; i < includes_list.GetCount(); ++i) {
        includes << "\"" << includes_list.Item(i) << "\" ";
    }
    pro_file << includes << wxT("\n");
    includes.Clear();

    //--------------------------------------------
    // Compiler options
    //--------------------------------------------
    wxString buildOpts = bldConf->GetCompileOptions();
    buildOpts.Replace(wxT(";"), wxT(" "));
    pro_file << wxT("QMAKE_CFLAGS_RELEASE   = ") << buildOpts << wxT("\n");
    pro_file << wxT("QMAKE_CXXFLAGS_RELEASE = ") << buildOpts << wxT("\n");

    //--------------------------------------------
    // Preprocessor
    //--------------------------------------------
    wxString defines = bldConf->GetPreprocessor();
    defines.Replace(wxT(";"), wxT(" "));
    pro_file << wxT("DEFINES                = ") << defines << wxT("\n");

    //--------------------------------------------
    // Prepare linker options
    //--------------------------------------------
    wxString links = bldConf->GetLinkOptions();
    links.Replace(wxT(";"), wxT(" "));
    pro_file << wxT("QMAKE_LFLAGS_RELEASE   = ") << links << wxT("\n");

    //--------------------------------------------
    // Lib path
    //--------------------------------------------
    wxString libPaths = bldConf->GetLibPath();
    wxString librarySwitch(wxT("-L")); // by default set it to '-L'
    if(cmp) { librarySwitch = cmp->GetSwitch(wxT("LibraryPath")); }

    wxArrayString libPathsArr = wxStringTokenize(libPaths, wxT(";"), wxTOKEN_STRTOK);
    libPaths.Clear();
    for(size_t i = 0; i < libPathsArr.GetCount(); i++) {
        libPaths << librarySwitch << wxT("\"") << libPathsArr.Item(i) << wxT("\" ");
    }

    pro_file << wxT("LIBS           = ") << libPaths << wxT("\n");

    // Add the global library path
    libPaths.Clear();
    if(cmp) {
        libPaths = cmp->GetGlobalLibPath();
        libPaths.Trim().Trim(false);
        libPathsArr = wxStringTokenize(libPaths, wxT(";"), wxTOKEN_STRTOK);
        libPaths.Clear();
        for(size_t i = 0; i < libPathsArr.GetCount(); i++) {
            libPaths << librarySwitch << wxT("\"") << libPathsArr.Item(i) << wxT("\" ");
        }
        pro_file << wxT("LIBS           += ") << libPaths << wxT("\n");
    }

    //--------------------------------------------
    // Libraries
    //--------------------------------------------
    wxString libs = bldConf->GetLibraries();
    if(cmp) {
        wxString libSwitch = cmp->GetSwitch(wxT("Library"));
        wxArrayString libsArr = wxStringTokenize(libs, wxT(";"), wxTOKEN_STRTOK);
        libs.Clear();

        for(size_t i = 0; i < libsArr.GetCount(); i++) {
            libs << libSwitch << libsArr.Item(i) << wxT(" ");
        }
        pro_file << wxT("LIBS           += ") << libs << wxT("\n");
    }
}

void QMakeProFileGenerator::SetFiles(wxString& pro_file, ProjectPtr proj)
{
    std::vector<wxFileName> files;
    proj->GetFilesAsVectorOfFileName(files);

    wxString sources, headers, forms, yacc, lex, qresources;

    sources << wxT("SOURCES     = \\\n");
    headers << wxT("HEADERS     = \\\n");
    forms << wxT("FORMS       = \\\n");
    yacc << wxT("YACCSOURCES = \\\n");
    lex << wxT("LEXSOURCES  = \\\n");
    qresources << wxT("RESOURCES   = \\\n");
    wxString BUFF(wxT("             "));

    for(size_t i = 0; i < files.size(); i++) {
        wxFileName fn = files.at(i);

        switch(FileExtManager::GetType(fn.GetFullName())) {
        case FileExtManager::TypeSourceC:
        case FileExtManager::TypeSourceCpp:
            sources << BUFF << fn.GetFullPath() << wxT("\\\n");
            break;
        case FileExtManager::TypeHeader:
            headers << BUFF << fn.GetFullPath() << wxT("\\\n");
            break;
        case FileExtManager::TypeQtForm:
            forms << BUFF << fn.GetFullPath() << wxT("\\\n");
            break;
        case FileExtManager::TypeYacc:
            yacc << BUFF << fn.GetFullPath() << wxT("\\\n");
            break;
        case FileExtManager::TypeLex:
            lex << BUFF << fn.GetFullPath() << wxT("\\\n");
            break;
        case FileExtManager::TypeQtResource:
            qresources << BUFF << fn.GetFullPath() << wxT("\\\n");
            break;
        default:
            break;
        }
    }

    pro_file << sources << wxT("\n");
    pro_file << headers << wxT("\n");
    pro_file << forms << wxT("\n");
    pro_file << yacc << wxT("\n");
    pro_file << lex << wxT("\n");
    pro_file << qresources << wxT("\n");
}

wxString QMakeProFileGenerator::GetProFileName()
{
    wxString errMsg;
    ProjectPtr p = m_manager->GetWorkspace()->FindProjectByName(m_project, errMsg);
    if(p) {
        wxFileName fn(wxString::Format(wxT("%s/%s.pro"),
                                       p->GetFileName().GetPath(wxPATH_GET_SEPARATOR | wxPATH_GET_VOLUME).c_str(),
                                       m_project.c_str()));
        return fn.GetFullPath();
    }

    return wxEmptyString;
}