-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCbProjectFromCompileCommands.cpp
More file actions
353 lines (315 loc) · 12.5 KB
/
CbProjectFromCompileCommands.cpp
File metadata and controls
353 lines (315 loc) · 12.5 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
350
351
352
353
/*
* This file uses parts of the Code::Blocks IDE and licensed under the GNU General Public License, version 3
* http://www.gnu.org/licenses/gpl-3.0.html
*/
#include <sdk.h> // Code::Blocks SDK
#include <wx/filename.h>
#include <wx/intl.h>
#include <wx/utils.h>
#include <wx/filename.h>
#include <wx/fs_zip.h>
#include <wx/menu.h>
#include <wx/xrc/xmlres.h>
#include <wx/dir.h>
#include <configurationpanel.h>
#include "CbProjectFromCompileCommands.h"
#include "projectmanager.h"
#include "logmanager.h"
#include "cbproject.h"
#include "globals.h"
#include "filefilters.h"
#include "filegroupsandmasks.h"
#include "multiselectdlg.h"
#include <fstream>
#include <sstream>
#include "nlohmann/json.hpp"
#define DEBUG
using json = nlohmann::json;
// Register the plugin with Code::Blocks.
// We are using an anonymous namespace so we don't litter the global one.
namespace
{
PluginRegistrant<CbProjectFromCompileCommands> reg(_T("CbProjectFromCompileCommands"));
const int idCbProjectFromCompileCommands = wxNewId();
} // namespace
BEGIN_EVENT_TABLE(CbProjectFromCompileCommands, cbPlugin)
EVT_MENU(idCbProjectFromCompileCommands, CbProjectFromCompileCommands::OnCbProjectFromCompileCommands)
END_EVENT_TABLE()
const char* pluginName = "CbProjectFromCompileCommands";
CbProjectFromCompileCommands::CbProjectFromCompileCommands()
{
// Make sure our resources are available.
// In the generated boilerplate code we have no resources but when
// we add some, it will be nice that this code is in place already ;)
if (!Manager::LoadResource(_T("CbProjectFromCompileCommands.zip")))
{
NotifyMissingFile(_T("CbProjectFromCompileCommands.zip"));
}
}
void CbProjectFromCompileCommands::BuildMenu(wxMenuBar* menuBar)
{
LogManager* logManager = Manager::Get()->GetLogManager();
if (!IsAttached() || !menuBar)
{
logManager->LogError(wxString::Format(_("%s Preconditions not met!"), pluginName));
return;
}
const int fileMenuIndex = menuBar->FindMenu(_("&File"));
if (fileMenuIndex == wxNOT_FOUND)
{
logManager->LogError(wxString::Format(_("%s Could not get File menu Idx!"), pluginName));
return;
}
wxMenu* fileMenu = menuBar->GetMenu(fileMenuIndex);
if (!fileMenu)
{
logManager->LogError(wxString::Format(_("%s Could not get File menu!"), pluginName));
return;
}
const int pluginMenuId = fileMenu->FindItem(_("&Create Project from compile commands"));
if (pluginMenuId == wxNOT_FOUND)
{
wxMenuItemList menuItems = fileMenu->GetMenuItems();
const int menuId = fileMenu->FindItem(_("R&ecent files"));
wxMenuItem* recentFileItem = fileMenu->FindItem(menuId);
int id = menuItems.IndexOf(recentFileItem);
if (id == wxNOT_FOUND)
{
id = 7;
}
else
{
++id;
}
// The position is hard-coded to "Recent Files" menu. Please adjust it if necessary
fileMenu->Insert(++id, idCbProjectFromCompileCommands, _("&Create Project from compile commands"),
_("&Create Project from compile commands"));
fileMenu->InsertSeparator(++id);
}
else
{
logManager->LogError(wxString::Format(_("%s Menu already present!"), pluginName));
}
}
void CbProjectFromCompileCommands::OnCbProjectFromCompileCommands(wxCommandEvent& event)
{
wxString errorString;
if (!CreateCbProjectFromCompileCommands(errorString))
{
if (!errorString.IsEmpty())
{
Manager::Get()->GetLogManager()->LogError(errorString);
cbMessageBox(_(errorString), _("Error"), wxICON_ERROR);
}
}
}
bool CbProjectFromCompileCommands::CreateProjectInternal(const wxString& fileName, const wxArrayString& filelist, wxString& errorString)
{
bool ret = true;
ProjectManager* projectManager = Manager::Get()->GetProjectManager();
cbProject* prj = projectManager->NewProject(fileName);
if (!prj)
{
errorString = wxString::Format(_("%s : Could not create project!"), pluginName);
ret = false;
}
else
{
prj->SetMakefileCustom(true);
prj->AddBuildTarget("all");
wxArrayInt targets;
targets.Add(0);
#ifdef DEBUG
for (const wxString& file : filelist)
{
fprintf(stderr, "%s\n", file.ToUTF8().data());
}
#endif // DEBUG
projectManager->AddMultipleFilesToProject(filelist, prj, targets);
Manager::Get()->GetLogManager()->Log(wxString::Format(_("%s Added %d files!"), pluginName, (int)filelist.GetCount()));
prj->SetModified(true);
prj->Save();
if (!projectManager->IsLoadingWorkspace())
{
projectManager->SetProject(prj);
/*Changes from Pecan - instead of PM SetProject, close and open projects
https://forums.codeblocks.org/index.php/topic,25509.msg173704.html#msg173704
*/
bool ok = projectManager->CloseProject(prj, true, true);
if (!ok)
{
errorString = wxString::Format(_("%s : Could not close project!"), pluginName);
ret = false;
}
cbProject* prj = projectManager->LoadProject(fileName, true);
if (!prj)
{
errorString = wxString::Format(_("%s : Could not open project!"), pluginName);
ret = false;
}
}
projectManager->GetUI().RebuildTree();
}
return ret;
}
static inline std::string get_updated_compile_command(const json& jentry, const std::string& originalDirectory, const std::string& projectDirectory,
size_t itemIdx)
{
std::ostringstream updatedCommand;
std::istringstream iss(jentry.at("command").get<std::string>());
std::string entry;
while (getline(iss, entry, ' '))
{
#ifdef DEBUG
fprintf(stderr, "file idx %zu entry %s\n", itemIdx, entry.c_str());
#endif
if (entry.rfind("-I", 0) == 0)
{
#ifdef DEBUG
fprintf(stderr, "file idx %zu matched. entry %s\n", itemIdx, entry.c_str());
#endif
std::string includePath = entry.substr(2);
includePath.erase(includePath.begin(),
std::find_if(includePath.begin(), includePath.end(), [](unsigned char ch) { return !std::isspace(ch); }));
char first = includePath.front();
char last = includePath.back();
if ((first == '"' && last == '"') || (first == '\'' && last == '\''))
{
includePath = includePath.substr(1, includePath.size() - 2);
}
wxFileName includeDirectory(includePath);
#ifdef DEBUG
fprintf(stderr, "file idx %zu includePath %s\n", itemIdx, includeDirectory.GetFullPath().ToUTF8().data());
#endif
if (includeDirectory.IsRelative())
{
includeDirectory.Assign(originalDirectory + wxFILE_SEP_PATH + includeDirectory.GetFullPath());
#ifdef DEBUG
fprintf(stderr, "file idx %zu appended directory . includeDirectory %s\n", itemIdx, includeDirectory.GetFullPath().ToUTF8().data());
#endif
}
includeDirectory.MakeRelativeTo(projectDirectory);
#ifdef DEBUG
fprintf(stderr, "file idx %zu after make relative . includeDirectory %s\n", itemIdx, includeDirectory.GetFullPath().ToUTF8().data());
#endif
updatedCommand << "-I" << includeDirectory.GetFullPath().ToStdString();
}
else
{
updatedCommand << entry;
}
updatedCommand << ' ';
}
return updatedCommand.str();
}
bool CbProjectFromCompileCommands::CreateCbProjectFromCompileCommands(wxString& errorString)
{
LogManager* logManager = Manager::Get()->GetLogManager();
logManager->Log(wxString::Format(_("%s Yay!"), pluginName));
wxFileDialog openFileDialog(nullptr, _("Open compile_commands.json file"), wxEmptyString, wxEmptyString,
"JSON files (*.json)|*.json|All files (*.*)|*.*", wxFD_OPEN | wxFD_FILE_MUST_EXIST);
if (openFileDialog.ShowModal() == wxID_CANCEL)
{
errorString = wxString::Format(_("%s: User canceled !"), pluginName);
return false;
}
wxFileDialog saveFileDialog(nullptr, _("Save Project file"), openFileDialog.GetDirectory(), "", "CBP files (*.cbp)|*.cbp",
wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
if (saveFileDialog.ShowModal() == wxID_CANCEL)
{
errorString = wxString::Format(_("%s: User canceled !"), pluginName);
return false;
}
std::ifstream f(openFileDialog.GetPath());
json jArray = json::parse(f);
f.close();
wxFileName cbpNameFull = saveFileDialog.GetPath();
wxString projectDirectory = cbpNameFull.GetPath();
bool generateCompileCommands = (projectDirectory != openFileDialog.GetDirectory());
wxArrayString array;
for (size_t i = 0; i < jArray.size(); i++)
{
json& jentry = jArray.at(i);
std::string jDirectory = jentry.at("directory").get<std::string>();
wxFileName fileName(jentry.at("file").get<std::string>());
#ifdef DEBUG
fprintf(stderr, "file idx %zu name %s jDirectory %s\n", i, fileName.GetFullPath().ToUTF8().data(), jDirectory.c_str());
#endif
if (fileName.IsRelative())
{
fileName.Assign(jDirectory + wxFILE_SEP_PATH + fileName.GetFullPath());
#ifdef DEBUG
fprintf(stderr, "file idx %zu appended directory . name %s\n", i, fileName.GetFullPath().ToUTF8().data());
#endif
}
fileName.MakeRelativeTo(projectDirectory);
#ifdef DEBUG
fprintf(stderr, "file idx %zu after make relative . name %s\n", i, fileName.GetFullPath().ToUTF8().data());
#endif
array.push_back(fileName.GetFullPath());
if (generateCompileCommands)
{
std::string projectDirectoryStd = projectDirectory.ToStdString();
if (jentry.contains("command"))
{
std::string updatedCommand = get_updated_compile_command(jentry, jDirectory, projectDirectoryStd, i);
#ifdef DEBUG
fprintf(stderr, "file idx %zu updated command %s\n", i, updatedCommand.c_str());
#endif
jentry["command"] = std::move(updatedCommand);
}
jentry["file"] = fileName.GetFullPath().ToStdString();
jentry["directory"] = std::move(projectDirectoryStd);
}
}
cbpNameFull.SetExt(FileFilters::CODEBLOCKS_EXT);
logManager->Log(wxString::Format(_("%s cbpNameFull %s!"), pluginName, cbpNameFull.GetFullPath()));
bool projectSetupCompleted = false;
// generate list of files to add
if (array.IsEmpty())
{
errorString = wxString::Format(_("%s : Could not find any files!"), pluginName);
}
else
{
logManager->Log(wxString::Format(_("%s : Before filter %d files present!"), pluginName, (int)array.GetCount()));
wxString wild;
const FilesGroupsAndMasks* fgam = Manager::Get()->GetProjectManager()->GetFilesGroupsAndMasks();
for (unsigned fm_idx = 0; fm_idx < fgam->GetGroupsCount(); fm_idx++) wild += fgam->GetFileMasks(fm_idx);
MultiSelectDlg dlg(nullptr, array, wild, _("Select the files to add to the project:"));
PlaceWindow(&dlg);
if (dlg.ShowModal() != wxID_OK)
{
logManager->Log(wxString::Format(_("%s : Dialog ShowModal canceled!"), pluginName));
}
else
{
array = dlg.GetSelectedStrings();
if (array.IsEmpty())
{
errorString = wxString::Format(_("%s : No files selected!"), pluginName);
}
else
{
if (generateCompileCommands)
{
wxString compileCommandsFullPath = projectDirectory + wxFILE_SEP_PATH + "compile_commands.json";
std::ofstream jsonFile(compileCommandsFullPath.ToStdString());
if (!jsonFile.is_open())
{
wxString msg(wxString::Format(_("'compile_commands.json' file %s\nfailed to open for output.\n"), compileCommandsFullPath));
cbMessageBox(msg);
}
else
{
jsonFile << std::setw(4) << jArray;
}
}
projectSetupCompleted = CreateProjectInternal(cbpNameFull.GetFullPath(), array, errorString);
}
}
}
return projectSetupCompleted;
}
void CbProjectFromCompileCommands::OnAttach() {}
void CbProjectFromCompileCommands::OnRelease(bool appShutDown) {}