~ubuntu-branches/ubuntu/hardy/codeblocks/hardy-backports

« back to all changes in this revision

Viewing changes to src/sdk/compilerfactory.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michael Casadevall
  • Date: 2008-07-17 04:39:23 UTC
  • Revision ID: james.westby@ubuntu.com-20080717043923-gmsy5cwkdjswghkm
Tags: upstream-8.02
ImportĀ upstreamĀ versionĀ 8.02

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This file is part of the Code::Blocks IDE and licensed under the GNU Lesser General Public License, version 3
 
3
 * http://www.gnu.org/licenses/lgpl-3.0.html
 
4
 *
 
5
 * $Revision: 4909 $
 
6
 * $Id: compilerfactory.cpp 4909 2008-02-27 13:15:26Z mortenmacfly $
 
7
 * $HeadURL: svn://svn.berlios.de/codeblocks/tags/8.02/src/sdk/compilerfactory.cpp $
 
8
 */
 
9
 
 
10
#include "sdk_precomp.h"
 
11
 
 
12
#ifndef CB_PRECOMP
 
13
    #include <wx/choicdlg.h> // wxSingleChoiceDialog
 
14
    #include "compilerfactory.h"
 
15
    #include "manager.h"
 
16
    #include "logmanager.h"
 
17
    #include "configmanager.h"
 
18
    #include "compiler.h"
 
19
#endif
 
20
 
 
21
#include "autodetectcompilers.h"
 
22
 
 
23
// statics
 
24
CompilersArray CompilerFactory::Compilers;
 
25
Compiler* CompilerFactory::s_DefaultCompiler = 0;
 
26
 
 
27
size_t CompilerFactory::GetCompilersCount()
 
28
{
 
29
    return Compilers.GetCount();
 
30
}
 
31
 
 
32
Compiler* CompilerFactory::GetCompiler(size_t index)
 
33
{
 
34
    // NOTE: The index can be -1 , if there is no compiler at all or less than number of compilers.
 
35
    /* NOTE: Any negative value of index will be converted to a large unsigned integer.
 
36
    Therefore it's safe to check if the index equals or exceeds the compiler count. */
 
37
    if ((Compilers.GetCount() < 1) || (index >= Compilers.GetCount()))
 
38
        return 0;
 
39
    return Compilers[index];
 
40
}
 
41
 
 
42
Compiler* CompilerFactory::GetCompiler(const wxString& id)
 
43
{
 
44
    const wxString lid = id.Lower();
 
45
    for (size_t i = 0; i < Compilers.GetCount(); ++i)
 
46
    {
 
47
        if (Compilers[i]->GetID().IsSameAs(lid))
 
48
        {
 
49
            return Compilers[i];
 
50
        }
 
51
    }
 
52
    return 0;
 
53
}
 
54
 
 
55
Compiler* CompilerFactory::GetCompilerByName(const wxString& title)
 
56
{
 
57
    for (size_t i = 0; i < Compilers.GetCount(); ++i)
 
58
    {
 
59
        if (Compilers[i]->GetName().IsSameAs(title))
 
60
        {
 
61
            return Compilers[i];
 
62
        }
 
63
    }
 
64
    return 0;
 
65
}
 
66
 
 
67
int CompilerFactory::GetCompilerIndex(const wxString& id)
 
68
{
 
69
    const wxString lid = id.Lower();
 
70
    for (size_t i = 0; i < Compilers.GetCount(); ++i)
 
71
    {
 
72
        if (Compilers[i]->GetID().IsSameAs(lid))
 
73
        {
 
74
            return i;
 
75
        }
 
76
    }
 
77
    return -1;
 
78
}
 
79
 
 
80
int CompilerFactory::GetCompilerIndex(Compiler* compiler)
 
81
{
 
82
    for (size_t i = 0; i < Compilers.GetCount(); ++i)
 
83
    {
 
84
        if (Compilers[i] == compiler)
 
85
        {
 
86
            return i;
 
87
        }
 
88
    }
 
89
    return -1;
 
90
}
 
91
 
 
92
bool CompilerFactory::CompilerInheritsFrom(const wxString& id, const wxString& from_id)
 
93
{
 
94
    return CompilerInheritsFrom(GetCompiler(id), from_id);
 
95
}
 
96
 
 
97
bool CompilerFactory::CompilerInheritsFrom(Compiler* compiler, const wxString& from_id)
 
98
{
 
99
    if (!compiler)
 
100
        return false;
 
101
 
 
102
    wxString id = compiler->GetID();
 
103
    if (id.Matches(from_id))
 
104
        return true;
 
105
 
 
106
    while (compiler)
 
107
    {
 
108
        wxString id = compiler->GetParentID();
 
109
        if (id.Matches(from_id))
 
110
            return true;
 
111
 
 
112
        // traverse up the chain
 
113
        Compiler* newcompiler = GetCompiler(id);
 
114
        if (compiler == newcompiler)
 
115
        {
 
116
            Manager::Get()->GetLogManager()->DebugLog(_T("Compiler circular dependency detected?!?!?"));
 
117
            break;
 
118
        }
 
119
        compiler = newcompiler;
 
120
    }
 
121
    return false;
 
122
}
 
123
 
 
124
void CompilerFactory::RegisterCompiler(Compiler* compiler)
 
125
{
 
126
    CompilerFactory::Compilers.Add(compiler);
 
127
    // if it's the first one, set it as default
 
128
    if (!s_DefaultCompiler)
 
129
        s_DefaultCompiler = compiler;
 
130
}
 
131
 
 
132
void CompilerFactory::RegisterUserCompilers()
 
133
{
 
134
    ConfigManager* cfg = Manager::Get()->GetConfigManager(_T("compiler"));
 
135
    wxArrayString paths = cfg->EnumerateSubPaths(_T("/user_sets"));
 
136
    for (unsigned int i = 0; i < paths.GetCount(); ++i)
 
137
    {
 
138
        wxString base = _T("/user_sets/") + paths[i];
 
139
                wxString parent = cfg->Read(base + _T("/parent"), wxEmptyString);
 
140
        if (!parent.IsEmpty())
 
141
        {
 
142
            Compiler* compiler = GetCompiler(parent);
 
143
            wxString name = cfg->Read(base + _T("/name"), wxEmptyString);
 
144
            CreateCompilerCopy(compiler, name);
 
145
        }
 
146
        }
 
147
}
 
148
 
 
149
Compiler* CompilerFactory::CreateCompilerCopy(Compiler* compiler, const wxString& newName)
 
150
{
 
151
    if (!compiler)
 
152
        return 0;
 
153
 
 
154
    // abort if an existing compiler with the same name exists
 
155
    // this also avoids the possibility of throwing an exception
 
156
    // in the compiler->CreateCopy() call below...
 
157
    for (size_t i = 0; i < Compilers.GetCount(); ++i)
 
158
    {
 
159
        if (Compilers[i]->GetName() == newName)
 
160
        {
 
161
            return 0;
 
162
        }
 
163
    }
 
164
 
 
165
    Compiler* newC = compiler->CreateCopy();
 
166
    if (!newName.IsEmpty())
 
167
    {
 
168
        Compiler::m_CompilerIDs.Remove(newC->GetID());
 
169
        newC->SetName(newName);
 
170
        newC->m_ID = newName;
 
171
        newC->MakeValidID();
 
172
    }
 
173
    RegisterCompiler(newC);
 
174
    newC->LoadSettings(_T("/user_sets"));
 
175
    Manager::Get()->GetLogManager()->DebugLog(F(_T("Added compiler \"%s\""), newC->GetName().c_str()));
 
176
    return newC; // return the index for the new compiler
 
177
}
 
178
 
 
179
void CompilerFactory::RemoveCompiler(Compiler* compiler)
 
180
{
 
181
    if (!compiler || compiler->m_ParentID.IsEmpty())
 
182
        return;
 
183
    Manager::Get()->GetConfigManager(_T("compiler"))->DeleteSubPath(_T("/user_sets/") + compiler->GetID());
 
184
 
 
185
    Compilers.Remove(compiler);
 
186
    Manager::Get()->GetLogManager()->DebugLog(F(_T("Compiler \"%s\" removed"), compiler->GetName().c_str()));
 
187
 
 
188
    Compiler::m_CompilerIDs.Remove(compiler->GetID());
 
189
    delete compiler;
 
190
 
 
191
    SaveSettings();
 
192
}
 
193
 
 
194
void CompilerFactory::UnregisterCompilers()
 
195
{
 
196
    WX_CLEAR_ARRAY(CompilerFactory::Compilers);
 
197
    CompilerFactory::Compilers.Empty();
 
198
    Compiler::m_CompilerIDs.Empty();
 
199
 
 
200
    s_DefaultCompiler = 0;
 
201
}
 
202
 
 
203
const wxString& CompilerFactory::GetDefaultCompilerID()
 
204
{
 
205
    if (s_DefaultCompiler)
 
206
        return s_DefaultCompiler->GetID();
 
207
 
 
208
    static wxString empty = wxEmptyString;
 
209
    return empty;
 
210
}
 
211
 
 
212
Compiler* CompilerFactory::GetDefaultCompiler()
 
213
{
 
214
    return s_DefaultCompiler;
 
215
}
 
216
 
 
217
void CompilerFactory::SetDefaultCompiler(size_t index)
 
218
{
 
219
    if ((Compilers.GetCount() > 0) && (index < Compilers.GetCount()))
 
220
        s_DefaultCompiler = Compilers[index];
 
221
}
 
222
 
 
223
void CompilerFactory::SetDefaultCompiler(const wxString& id)
 
224
{
 
225
    Compiler* compiler = GetCompiler(id.Lower());
 
226
    SetDefaultCompiler(compiler);
 
227
}
 
228
 
 
229
void CompilerFactory::SetDefaultCompiler(Compiler* compiler)
 
230
{
 
231
    if (compiler)
 
232
        s_DefaultCompiler = compiler;
 
233
}
 
234
 
 
235
void CompilerFactory::SaveSettings()
 
236
{
 
237
        // clear old keys before saving
 
238
        Manager::Get()->GetConfigManager(_T("compiler"))->DeleteSubPath(_T("/sets"));
 
239
        Manager::Get()->GetConfigManager(_T("compiler"))->DeleteSubPath(_T("/user_sets"));
 
240
 
 
241
    for (size_t i = 0; i < Compilers.GetCount(); ++i)
 
242
    {
 
243
        wxString baseKey = Compilers[i]->GetParentID().IsEmpty() ? _T("/sets") : _T("/user_sets");
 
244
        Compilers[i]->SaveSettings(baseKey);
 
245
    }
 
246
}
 
247
 
 
248
void CompilerFactory::LoadSettings()
 
249
{
 
250
    bool needAutoDetection = false;
 
251
    for (size_t i = 0; i < Compilers.GetCount(); ++i)
 
252
    {
 
253
        wxString baseKey = Compilers[i]->GetParentID().IsEmpty() ? _T("/sets") : _T("/user_sets");
 
254
        Compilers[i]->LoadSettings(baseKey);
 
255
        if (Compilers[i]->GetMasterPath().IsEmpty())
 
256
            needAutoDetection = true;
 
257
    }
 
258
 
 
259
    // auto-detect missing compilers
 
260
    if (needAutoDetection)
 
261
    {
 
262
        AutoDetectCompilers adc(Manager::Get()->GetAppWindow());
 
263
        PlaceWindow(&adc);
 
264
        adc.ShowModal();
 
265
    }
 
266
}
 
267
 
 
268
Compiler* CompilerFactory::SelectCompilerUI(const wxString& message, const wxString& preselectedID)
 
269
{
 
270
    int selected = -1;
 
271
    const wxString lid = preselectedID.Lower();
 
272
 
 
273
    // first build a list of available compilers
 
274
    wxString* comps = new wxString[Compilers.GetCount()];
 
275
    for (size_t i = 0; i < Compilers.GetCount(); ++i)
 
276
    {
 
277
        comps[i] = Compilers[i]->GetName();
 
278
        if (selected == -1)
 
279
        {
 
280
            if (lid.IsEmpty())
 
281
            {
 
282
                if (Compilers[i] == s_DefaultCompiler)
 
283
                    selected = i;
 
284
            }
 
285
            else
 
286
            {
 
287
                if (Compilers[i]->GetID().IsSameAs(lid))
 
288
                    selected = i;
 
289
            }
 
290
        }
 
291
    }
 
292
    // now display a choice dialog
 
293
    wxSingleChoiceDialog dlg(0,
 
294
                        message,
 
295
                        _("Compiler selection"),
 
296
                        CompilerFactory::Compilers.GetCount(),
 
297
                        comps);
 
298
    dlg.SetSelection(selected);
 
299
    PlaceWindow(&dlg);
 
300
    if (dlg.ShowModal() == wxID_OK)
 
301
        return Compilers[dlg.GetSelection()];
 
302
    return 0;
 
303
}
 
304
 
 
305
wxString CompilerFactory::GetCompilerVersionString(const wxString& Id)
 
306
{
 
307
        wxString Version;
 
308
        if(const Compiler* pCompiler = GetCompiler(Id))
 
309
        {
 
310
                Version = pCompiler->GetVersionString();
 
311
        }
 
312
        return Version;
 
313
} // end of GetCompilerVersionString