forked from eranif/codelite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMakeGenerator.cpp
335 lines (260 loc) · 10.4 KB
/
CMakeGenerator.cpp
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
/* ************************************************************************ */
/* */
/* CMakePlugin for Codelite */
/* Copyright (C) 2013 Jiří Fatka <ntsfka@gmail.com> */
/* */
/* 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 3 of the License, or */
/* (at your option) any later version. */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/* ************************************************************************ */
/* ************************************************************************ */
/* INCLUDES */
/* ************************************************************************ */
// Declarations
#include "CMakeGenerator.h"
// wxWidgets
#include <wx/tokenzr.h>
#include <wx/ffile.h>
#include <wx/filename.h>
#include <wx/msgdlg.h>
// Plugin
#include "CMakePlugin.h"
/* ************************************************************************ */
/* FUNCTIONS */
/* ************************************************************************ */
/**
* @brief Check if filename exists and asks if user want it overwrite if
* exists.
*
* @param filename Tested filename.
*
* @return If file can be written.
*/
static bool CheckExists(const wxFileName& filename)
{
// Output file exists, overwrite?
if (filename.Exists()) {
int res = wxMessageBox(CMakePlugin::CMAKELISTS_FILE + " exists. Overwrite?\n" +
"(" + filename.GetFullPath() + ")",
wxMessageBoxCaptionStr, wxYES | wxNO | wxCENTER | wxICON_QUESTION);
// Only for YES
if (res != wxYES)
return false;
}
// File doesn't exists, write it
return true;
}
/* ************************************************************************ */
/**
* @brief Writes content into file.
*
* @param filename Output file name.
* @param content Written text.
*/
static void WriteContent(const wxFileName& filename, const wxString& content)
{
// Write result CMakeLists.txt
wxFFile output(filename.GetFullPath(), "w+b");
if (output.IsOpened()) {
output.Write(content);
output.Close();
}
}
/* ************************************************************************ */
/* CLASSES */
/* ************************************************************************ */
void
CMakeGenerator::Generate(Workspace* workspace)
{
// Get workspace directory.
const wxFileName workspaceDir = workspace->GetWorkspaceFileName().
GetPath(wxPATH_GET_SEPARATOR | wxPATH_GET_VOLUME);
// Output file name
const wxFileName filename(workspaceDir.GetPath(), CMakePlugin::CMAKELISTS_FILE);
if (!CheckExists(filename))
return;
// File content
wxString content;
// Print project name
content << "# Workspace name\n";
content << "project(" << workspace->GetName() << ")\n\n";
// Environment variables
{
wxString variables = workspace->GetEnvironmentVariabels(); // Nice typo
variables.Trim().Trim(false);
if (!variables.IsEmpty()) {
// Split into a list of pairs
const wxArrayString list = wxStringTokenize(variables, "\n;");
for (wxArrayString::const_iterator it = list.begin(),
ite = list.end(); it != ite; ++it) {
// Split into name, value pair
const wxArrayString pair = wxSplit(*it, '=');
const wxString& name = pair[0];
const wxString value = (pair.GetCount() >= 2) ? pair[1] : "";
// Set environment variable
content << "set(" << name << " \"" << value << "\")\n";
}
content << "\n";
}
}
content << "# Projects\n";
// Get full paths to all projects
const wxArrayString projects = workspace->GetAllProjectPaths();
// Foreach projects path
for (wxArrayString::const_iterator it = projects.begin(),
ite = projects.end(); it != ite; ++it) {
wxFileName fullpath = *it;
fullpath.MakeRelativeTo(workspaceDir.GetPath());
// Create path to CMakeLists.txt in the project
const wxFileName cmakelist(fullpath.GetPath(), CMakePlugin::CMAKELISTS_FILE);
// Skip directories without CMakeLists.txt
if (!cmakelist.Exists())
continue;
// Write
content << "add_subdirectory(" << fullpath.GetPath() << ")\n";
}
// Write result
WriteContent(filename, content);
}
/* ************************************************************************ */
void
CMakeGenerator::Generate(ProjectPtr project, BuildConfigPtr configuration,
CompilerPtr compiler)
{
wxASSERT(project);
wxASSERT(configuration);
// Get project directory
const wxFileName projectDir = project->GetFileName().GetPath(wxPATH_GET_SEPARATOR | wxPATH_GET_VOLUME);
// Output file name
const wxFileName filename(projectDir.GetPath(), CMakePlugin::CMAKELISTS_FILE);
if (!CheckExists(filename))
return;
// File content
wxString content;
// TODO custom version
content << "cmake_minimum_required(VERSION 2.6.2)\n\n";
// Print project name
content << "project(" << project->GetName() << ")\n\n";
// Add include directories
{
wxString includes = configuration->GetIncludePath();
// Get includes from project
wxArrayString project_includes = project->GetIncludePaths();
// Add projects includes
includes << ";" << wxJoin(project_includes, ';');
if (compiler) {
// Append global include paths
includes << ";" << compiler->GetGlobalIncludePath();
}
// Trim all whitespaces
includes.Trim().Trim(false);
// Ignore empty include paths
if (!includes.IsEmpty()) {
// Separators
includes.Replace(";", "\n ");
// Replace Windows backslashes
includes.Replace("\\", "/");
content << "include_directories(\n " << includes << "\n)\n\n";
}
}
// Add preprocessor definitions
{
wxString defines = configuration->GetPreprocessor();
defines.Trim().Trim(false);
defines.Replace(";", "\n -D");
if (!defines.IsEmpty())
{
content << "add_definitions(\n -D" << defines << "\n)\n\n";
}
}
// Add compiler options
{
wxString buildOpts = configuration->GetCompileOptions();
buildOpts.Trim().Trim(false);
buildOpts.Replace(";", " ");
if (!buildOpts.IsEmpty()) {
content << "set(CMAKE_CXXFLAGS \"${CMAKE_CXXFLAGS} " << buildOpts << "\")\n\n";
}
}
// Add linker options
{
wxString links = configuration->GetLinkOptions();
links.Trim().Trim(false);
links.Replace(";", " ");
if (!links.IsEmpty()) {
content << "# Linker options\n";
content << "set(CMAKE_LDFLAGS \"${CMAKE_LDFLAGS} " << links << "\")\n\n";
}
}
// Add libraries paths
{
wxString lib_paths = configuration->GetLibPath();
wxString lib_switch = "-L";
// Get switch from compiler
if (compiler) {
lib_switch = compiler->GetSwitch("LibraryPath");
// Append global library paths
lib_paths << ";" << compiler->GetGlobalLibPath();
}
// Get list of library paths
wxArrayString lib_paths_list = wxStringTokenize(lib_paths, ";", wxTOKEN_STRTOK);
// Clear string
lib_paths.Clear();
// Append modified values
for (size_t i = 0; i < lib_paths_list.GetCount(); ++i) {
lib_paths << lib_switch << "\\\"" << lib_paths_list.Item(i) << "\\\" ";
}
content << "# Library path\n";
content << "set(CMAKE_LDFLAGS \"${CMAKE_LDFLAGS} " << lib_paths << "\")\n\n";
}
// Write sources
{
content << "set(SRCS\n";
// Get files in the project
std::vector<wxFileName> files;
project->GetFiles(files, true);
for (size_t i = 0; i < files.size(); i++) {
wxFileName src_filename = files.at(i);
src_filename.MakeRelativeTo(project->GetFileName().GetPath());
// Store file name into SRCS
content << " " << src_filename.GetFullPath(wxPATH_UNIX) << "\n";
}
content << ")\n\n";
}
// Get project type
{
ProjectSettingsPtr settings = project->GetSettings();
wxString type = settings->GetProjectType(configuration->GetName());
if (type == Project::EXECUTABLE) {
content << "add_executable(" << project->GetName() << " ${SRCS})\n\n";
} else if (type == Project::DYNAMIC_LIBRARY) {
content << "add_library(" << project->GetName() << " SHARED ${SRCS})\n\n";
} else {
content << "add_library(" << project->GetName() << " ${SRCS})\n\n";
}
}
// Add link libraries
{
wxString libs = configuration->GetLibraries();
libs.Trim().Trim(false);
if (!libs.IsEmpty()) {
libs.Replace(";", "\n ");
content << "target_link_libraries(" <<
project->GetName() << "\n " <<
libs << "\n)\n\n";
}
}
// Write result
WriteContent(filename, content);
}
/* ************************************************************************ */