Menu

[r13158]: / trunk / src / sdk / compilerfactory.cpp  Maximize  Restore  History

Download this file

350 lines (304 with data), 10.1 kB

  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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
* This file is part of the Code::Blocks IDE and licensed under the GNU Lesser General Public License, version 3
* http://www.gnu.org/licenses/lgpl-3.0.html
*
* $Revision$
* $Id$
* $HeadURL$
*/
#include "sdk_precomp.h"
#ifndef CB_PRECOMP
#include <wx/choicdlg.h> // wxSingleChoiceDialog
#include "compilerfactory.h"
#include "manager.h"
#include "logmanager.h"
#include "configmanager.h"
#include "compiler.h"
#endif
#include "autodetectcompilers.h"
// statics
CompilersArray CompilerFactory::Compilers;
Compiler* CompilerFactory::s_DefaultCompiler = nullptr;
size_t CompilerFactory::GetCompilersCount()
{
return Compilers.GetCount();
}
Compiler* CompilerFactory::GetCompiler(size_t index)
{
// NOTE: The index can be -1 , if there is no compiler at all or less than number of compilers.
/* NOTE: Any negative value of index will be converted to a large unsigned integer.
Therefore it's safe to check if the index equals or exceeds the compiler count. */
if ((Compilers.GetCount() < 1) || (index >= Compilers.GetCount()))
return nullptr;
return Compilers[index];
}
Compiler* CompilerFactory::GetCompiler(const wxString& id)
{
for (size_t i = 0; i < Compilers.GetCount(); ++i)
{
if (Compilers[i]->GetID().IsSameAs(id, false))
return Compilers[i];
}
// try again using previous id format
for (size_t i = 0; i < Compilers.GetCount(); ++i)
{
wxString oldId = Compilers[i]->GetID();
oldId.Replace(wxT("-"), wxEmptyString);
if (oldId.IsSameAs(id, false))
return Compilers[i];
}
return nullptr;
}
Compiler* CompilerFactory::GetCompilerByName(const wxString& title)
{
for (size_t i = 0; i < Compilers.GetCount(); ++i)
{
if (Compilers[i]->GetName().IsSameAs(title))
return Compilers[i];
}
return nullptr;
}
int CompilerFactory::GetCompilerIndex(const wxString& id)
{
const wxString lid = id.Lower();
for (size_t i = 0; i < Compilers.GetCount(); ++i)
{
if (Compilers[i]->GetID().IsSameAs(lid))
return i;
}
// try again using previous id format
for (size_t i = 0; i < Compilers.GetCount(); ++i)
{
wxString oldId = Compilers[i]->GetID();
oldId.Replace(wxT("-"), wxEmptyString);
if (oldId.IsSameAs(lid))
return i;
}
return -1;
}
int CompilerFactory::GetCompilerIndex(Compiler* compiler)
{
for (size_t i = 0; i < Compilers.GetCount(); ++i)
{
if (Compilers[i] == compiler)
return i;
}
return -1;
}
bool CompilerFactory::CompilerInheritsFrom(const wxString& id, const wxString& from_id)
{
return CompilerInheritsFrom(GetCompiler(id), from_id);
}
bool CompilerFactory::CompilerInheritsFrom(Compiler* compiler, const wxString& from_id)
{
if (!compiler)
return false;
wxString id = compiler->GetID();
if (id.Matches(from_id))
return true;
while (compiler)
{
id = compiler->GetParentID();
if (id.Matches(from_id))
return true;
// traverse up the chain
Compiler* newcompiler = GetCompiler(id);
if (compiler == newcompiler)
{
Manager::Get()->GetLogManager()->DebugLog(_T("Compiler circular dependency detected?!?!?"));
break;
}
compiler = newcompiler;
}
return false;
}
void CompilerFactory::RegisterCompiler(Compiler* compiler)
{
size_t idx = CompilerFactory::Compilers.GetCount();
for (; idx > 0; --idx)
{
if (compiler->m_Weight >= Compilers[idx - 1]->m_Weight)
break;
}
CompilerFactory::Compilers.Insert(compiler, idx);
// if it's the first one, set it as default
if (!s_DefaultCompiler)
s_DefaultCompiler = compiler;
}
void CompilerFactory::RegisterUserCompilers()
{
ConfigManager* cfg = Manager::Get()->GetConfigManager(_T("compiler"));
wxArrayString paths = cfg->EnumerateSubPaths(_T("/user_sets"));
for (unsigned int i = 0; i < paths.GetCount(); ++i)
{
wxString base = _T("/user_sets/") + paths[i];
wxString parent = cfg->Read(base + _T("/parent"), wxEmptyString);
if (!parent.IsEmpty())
{
Compiler* compiler = GetCompiler(parent);
wxString name = cfg->Read(base + _T("/name"), wxEmptyString);
CreateCompilerCopy(compiler, name);
}
}
}
Compiler* CompilerFactory::CreateCompilerCopy(Compiler* compiler, const wxString& newName)
{
if (!compiler)
return nullptr;
// abort if an existing compiler with the same name exists
// this also avoids the possibility of throwing an exception
// in the compiler->CreateCopy() call below...
for (size_t i = 0; i < Compilers.GetCount(); ++i)
{
if (Compilers[i]->GetName() == newName)
return nullptr;
}
Compiler* newC = compiler->CreateCopy();
if (!newName.IsEmpty())
{
Compiler::m_CompilerIDs.Remove(newC->GetID());
newC->SetName(newName);
newC->m_ID = newName;
newC->MakeValidID();
}
newC->ReloadOptions();
RegisterCompiler(newC);
newC->LoadSettings(_T("/user_sets"));
Manager::Get()->GetLogManager()->DebugLog(wxString::Format("Added compiler \"%s\"", newC->GetName()));
return newC; // return the index for the new compiler
}
void CompilerFactory::RemoveCompiler(Compiler* compiler)
{
if (!compiler || compiler->m_ParentID.IsEmpty())
return;
Manager::Get()->GetConfigManager(_T("compiler"))->DeleteSubPath(_T("/user_sets/") + compiler->GetID());
Compilers.Remove(compiler);
Manager::Get()->GetLogManager()->DebugLog(wxString::Format("Compiler \"%s\" removed", compiler->GetName()));
Compiler::m_CompilerIDs.Remove(compiler->GetID());
delete compiler;
SaveSettings();
}
void CompilerFactory::UnregisterCompilers()
{
WX_CLEAR_ARRAY(CompilerFactory::Compilers);
CompilerFactory::Compilers.Empty();
Compiler::m_CompilerIDs.Empty();
s_DefaultCompiler = nullptr;
}
const wxString& CompilerFactory::GetDefaultCompilerID()
{
if (s_DefaultCompiler)
return s_DefaultCompiler->GetID();
static wxString empty = wxEmptyString;
return empty;
}
Compiler* CompilerFactory::GetDefaultCompiler()
{
return s_DefaultCompiler;
}
void CompilerFactory::SetDefaultCompiler(size_t index)
{
if ((Compilers.GetCount() > 0) && (index < Compilers.GetCount()))
s_DefaultCompiler = Compilers[index];
}
void CompilerFactory::SetDefaultCompiler(const wxString& id)
{
Compiler* compiler = GetCompiler(id.Lower());
SetDefaultCompiler(compiler);
}
void CompilerFactory::SetDefaultCompiler(Compiler* compiler)
{
if (compiler)
s_DefaultCompiler = compiler;
}
void CompilerFactory::SaveSettings()
{
// clear old keys before saving
Manager::Get()->GetConfigManager(_T("compiler"))->DeleteSubPath(_T("/sets"));
Manager::Get()->GetConfigManager(_T("compiler"))->DeleteSubPath(_T("/user_sets"));
for (size_t i = 0; i < Compilers.GetCount(); ++i)
{
wxString baseKey = Compilers[i]->GetParentID().IsEmpty() ? _T("/sets") : _T("/user_sets");
Compilers[i]->SaveSettings(baseKey);
CodeBlocksEvent event(cbEVT_COMPILER_SETTINGS_CHANGED);
event.SetString(Compilers[i]->GetID());
event.SetInt(static_cast<int>(i));
event.SetClientData(static_cast<void*>(Compilers[i]));
Manager::Get()->ProcessEvent(event);
}
}
void CompilerFactory::LoadSettings()
{
bool needAutoDetection = false;
for (size_t i = 0; i < Compilers.GetCount(); ++i)
{
wxString baseKey = Compilers[i]->GetParentID().IsEmpty() ? _T("/sets") : _T("/user_sets");
Compilers[i]->LoadSettings(baseKey);
CodeBlocksEvent event(cbEVT_COMPILER_SETTINGS_CHANGED);
event.SetString(Compilers[i]->GetID());
event.SetInt(static_cast<int>(i));
event.SetClientData(static_cast<void*>(Compilers[i]));
Manager::Get()->ProcessEvent(event);
if (Compilers[i]->GetMasterPath().IsEmpty())
{
Manager::Get()->GetLogManager()->DebugLog(wxString::Format("Master path of compiler ID \"%s\" is empty -> triggers auto-detection.", Compilers[i]->GetID()));
needAutoDetection = true;
}
}
// auto-detect missing compilers
if (needAutoDetection)
{
AutoDetectCompilers adc(Manager::Get()->GetAppWindow());
PlaceWindow(&adc);
adc.ShowModal();
adc.Raise();
}
}
Compiler* CompilerFactory::SelectCompilerUI(const wxString& message, const wxString& preselectedID)
{
const size_t compCount = Compilers.GetCount();
if (!compCount)
return nullptr;
// first build a list of valid compilers
int selected = -1;
const wxString lid(preselectedID.Lower());
wxArrayString comps;
comps.Alloc(compCount);
for (size_t i = 0; i < compCount; ++i)
{
if (Compilers[i]->IsValid())
{
const size_t pos = comps.Add(Compilers[i]->GetName());
if (selected == -1)
{
if (lid.empty())
{
if (Compilers[i] == s_DefaultCompiler)
selected = pos;
}
else
{
if (Compilers[i]->GetID() == lid)
selected = pos;
}
}
}
}
// sort it alphabetically
comps.Sort();
// now display a choice dialog
wxSingleChoiceDialog dlg(nullptr,
message,
_("Compiler selection"),
comps);
dlg.SetSelection(selected);
PlaceWindow(&dlg);
return (dlg.ShowModal() == wxID_OK) ? GetCompilerByName(dlg.GetStringSelection()) : nullptr;
}
wxString CompilerFactory::GetCompilerVersionString(const wxString& Id)
{
wxString Version;
if (const Compiler* pCompiler = GetCompiler(Id))
Version = pCompiler->GetVersionString();
return Version;
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.