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
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 $
10
#include "sdk_precomp.h"
13
#include <wx/choicdlg.h> // wxSingleChoiceDialog
14
#include "compilerfactory.h"
16
#include "logmanager.h"
17
#include "configmanager.h"
21
#include "autodetectcompilers.h"
24
CompilersArray CompilerFactory::Compilers;
25
Compiler* CompilerFactory::s_DefaultCompiler = 0;
27
size_t CompilerFactory::GetCompilersCount()
29
return Compilers.GetCount();
32
Compiler* CompilerFactory::GetCompiler(size_t index)
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()))
39
return Compilers[index];
42
Compiler* CompilerFactory::GetCompiler(const wxString& id)
44
const wxString lid = id.Lower();
45
for (size_t i = 0; i < Compilers.GetCount(); ++i)
47
if (Compilers[i]->GetID().IsSameAs(lid))
55
Compiler* CompilerFactory::GetCompilerByName(const wxString& title)
57
for (size_t i = 0; i < Compilers.GetCount(); ++i)
59
if (Compilers[i]->GetName().IsSameAs(title))
67
int CompilerFactory::GetCompilerIndex(const wxString& id)
69
const wxString lid = id.Lower();
70
for (size_t i = 0; i < Compilers.GetCount(); ++i)
72
if (Compilers[i]->GetID().IsSameAs(lid))
80
int CompilerFactory::GetCompilerIndex(Compiler* compiler)
82
for (size_t i = 0; i < Compilers.GetCount(); ++i)
84
if (Compilers[i] == compiler)
92
bool CompilerFactory::CompilerInheritsFrom(const wxString& id, const wxString& from_id)
94
return CompilerInheritsFrom(GetCompiler(id), from_id);
97
bool CompilerFactory::CompilerInheritsFrom(Compiler* compiler, const wxString& from_id)
102
wxString id = compiler->GetID();
103
if (id.Matches(from_id))
108
wxString id = compiler->GetParentID();
109
if (id.Matches(from_id))
112
// traverse up the chain
113
Compiler* newcompiler = GetCompiler(id);
114
if (compiler == newcompiler)
116
Manager::Get()->GetLogManager()->DebugLog(_T("Compiler circular dependency detected?!?!?"));
119
compiler = newcompiler;
124
void CompilerFactory::RegisterCompiler(Compiler* compiler)
126
CompilerFactory::Compilers.Add(compiler);
127
// if it's the first one, set it as default
128
if (!s_DefaultCompiler)
129
s_DefaultCompiler = compiler;
132
void CompilerFactory::RegisterUserCompilers()
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)
138
wxString base = _T("/user_sets/") + paths[i];
139
wxString parent = cfg->Read(base + _T("/parent"), wxEmptyString);
140
if (!parent.IsEmpty())
142
Compiler* compiler = GetCompiler(parent);
143
wxString name = cfg->Read(base + _T("/name"), wxEmptyString);
144
CreateCompilerCopy(compiler, name);
149
Compiler* CompilerFactory::CreateCompilerCopy(Compiler* compiler, const wxString& newName)
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)
159
if (Compilers[i]->GetName() == newName)
165
Compiler* newC = compiler->CreateCopy();
166
if (!newName.IsEmpty())
168
Compiler::m_CompilerIDs.Remove(newC->GetID());
169
newC->SetName(newName);
170
newC->m_ID = newName;
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
179
void CompilerFactory::RemoveCompiler(Compiler* compiler)
181
if (!compiler || compiler->m_ParentID.IsEmpty())
183
Manager::Get()->GetConfigManager(_T("compiler"))->DeleteSubPath(_T("/user_sets/") + compiler->GetID());
185
Compilers.Remove(compiler);
186
Manager::Get()->GetLogManager()->DebugLog(F(_T("Compiler \"%s\" removed"), compiler->GetName().c_str()));
188
Compiler::m_CompilerIDs.Remove(compiler->GetID());
194
void CompilerFactory::UnregisterCompilers()
196
WX_CLEAR_ARRAY(CompilerFactory::Compilers);
197
CompilerFactory::Compilers.Empty();
198
Compiler::m_CompilerIDs.Empty();
200
s_DefaultCompiler = 0;
203
const wxString& CompilerFactory::GetDefaultCompilerID()
205
if (s_DefaultCompiler)
206
return s_DefaultCompiler->GetID();
208
static wxString empty = wxEmptyString;
212
Compiler* CompilerFactory::GetDefaultCompiler()
214
return s_DefaultCompiler;
217
void CompilerFactory::SetDefaultCompiler(size_t index)
219
if ((Compilers.GetCount() > 0) && (index < Compilers.GetCount()))
220
s_DefaultCompiler = Compilers[index];
223
void CompilerFactory::SetDefaultCompiler(const wxString& id)
225
Compiler* compiler = GetCompiler(id.Lower());
226
SetDefaultCompiler(compiler);
229
void CompilerFactory::SetDefaultCompiler(Compiler* compiler)
232
s_DefaultCompiler = compiler;
235
void CompilerFactory::SaveSettings()
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"));
241
for (size_t i = 0; i < Compilers.GetCount(); ++i)
243
wxString baseKey = Compilers[i]->GetParentID().IsEmpty() ? _T("/sets") : _T("/user_sets");
244
Compilers[i]->SaveSettings(baseKey);
248
void CompilerFactory::LoadSettings()
250
bool needAutoDetection = false;
251
for (size_t i = 0; i < Compilers.GetCount(); ++i)
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;
259
// auto-detect missing compilers
260
if (needAutoDetection)
262
AutoDetectCompilers adc(Manager::Get()->GetAppWindow());
268
Compiler* CompilerFactory::SelectCompilerUI(const wxString& message, const wxString& preselectedID)
271
const wxString lid = preselectedID.Lower();
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)
277
comps[i] = Compilers[i]->GetName();
282
if (Compilers[i] == s_DefaultCompiler)
287
if (Compilers[i]->GetID().IsSameAs(lid))
292
// now display a choice dialog
293
wxSingleChoiceDialog dlg(0,
295
_("Compiler selection"),
296
CompilerFactory::Compilers.GetCount(),
298
dlg.SetSelection(selected);
300
if (dlg.ShowModal() == wxID_OK)
301
return Compilers[dlg.GetSelection()];
305
wxString CompilerFactory::GetCompilerVersionString(const wxString& Id)
308
if(const Compiler* pCompiler = GetCompiler(Id))
310
Version = pCompiler->GetVersionString();
313
} // end of GetCompilerVersionString