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 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
|
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright : (C) 2014 The CodeLite Team
// file name : CMake.cpp
//
// -------------------------------------------------------------------------
// A
// _____ _ _ _ _
// / __ \ | | | | (_) |
// | / \/ ___ __| | ___| | _| |_ ___
// | | / _ \ / _ |/ _ \ | | | __/ _ )
// | \__/\ (_) | (_| | __/ |___| | || __/
// \____/\___/ \__,_|\___\_____/_|\__\___|
//
// F i l e
//
// 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 2 of the License, or
// (at your option) any later version.
//
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
/* ************************************************************************ */
/* */
/* 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 */
/* ************************************************************************ */
// Declaration
#include "CMake.h"
// C++
#include <utility>
// wxWidgets
#include <wx/regex.h>
#include <wx/event.h>
#include <wx/thread.h>
#include <wx/scopedptr.h>
// Codelite
#include "procutils.h"
#include "workspace.h"
#include "globals.h"
#include "file_logger.h"
/* ************************************************************************ */
/* FUNCTIONS */
/* ************************************************************************ */
/**
* @brief Joins array of strings into single string.
*
* @param array Input array.
*
* @return Result string.
*/
static wxString CreateHtml(const wxArrayString& array)
{
wxString result;
for (wxArrayString::const_iterator it = array.begin(), ite = array.end(); it != ite; ++it) {
if (it != array.begin()) {
result += "<br />";
}
wxString esc = *it;
// Escape
esc.Replace("<", "<");
esc.Replace(">", ">");
result += esc;
}
return result;
}
/* ************************************************************************ */
/* CLASSES */
/* ************************************************************************ */
CMake::CMake(const wxFileName& path)
: m_path(path)
, m_version("?")
, m_dbFileName(wxStandardPaths::Get().GetUserDataDir(), "cmake.db")
{
// Prepare database
PrepareDatabase();
}
/* ************************************************************************ */
wxArrayString
CMake::GetVersions()
{
static const wxString VERSIONS[] = {
"2.8.11",
"2.8.10",
"2.8.9",
"2.8.8",
"2.8.7",
"2.8.6",
"2.8.5",
"2.8.4",
"2.8.3",
"2.8.2",
"2.8.1",
"2.8.0",
"2.6.4",
"2.6.3",
"2.6.2",
"2.6.1",
"2.6.0",
"2.4.8",
"2.4.7",
"2.4.6",
"2.4.5",
"2.4.4",
"2.4.3",
"2.2.3",
"2.0.6",
"1.8.3"
};
return wxArrayString(sizeof(VERSIONS) / sizeof(VERSIONS[0]), VERSIONS);
}
/* ************************************************************************ */
bool
CMake::IsOk() const
{
wxArrayString output;
ProcUtils::SafeExecuteCommand(GetPath().GetFullPath() + " -h", output);
// SafeExecuteCommand doesn't return status code so the only way
// to test the success is the output emptiness.
return !output.empty();
}
/* ************************************************************************ */
bool
CMake::LoadData(bool force, LoadNotifier* notifier)
{
// Clear old data
m_version.clear();
m_commands.clear();
m_modules.clear();
m_properties.clear();
m_variables.clear();
if (notifier) {
notifier->Start();
}
// Load data from database
if (!force && m_dbInitialized && LoadFromDatabase()) {
// Loading is done
if (notifier) {
notifier->Done();
}
return true;
}
// Unable to use CMake
if (!IsOk())
return false;
// Request to stop
if (notifier && notifier->RequestStop()) {
return false;
}
// Get cmake program path
const wxString program = GetPath().GetFullPath();
// Version
{
wxArrayString output;
ProcUtils::SafeExecuteCommand(program + " --version", output);
// Unable to find version
if (!output.IsEmpty()) {
const wxString& versionLine = output[0];
wxRegEx expression("cmake version (.+)");
if (expression.IsValid() && expression.Matches(versionLine)) {
m_version = expression.GetMatch(versionLine, 1).Trim().Trim(false);
}
}
}
// Request to stop
if (notifier && notifier->RequestStop()) {
return false;
}
// Load data
if ( !LoadFromCMake(notifier) )
return false;
// Request to stop
if (notifier && notifier->RequestStop()) {
return false;
}
// Database is open so we can store result into database
if (m_dbInitialized) {
StoreIntoDatabase();
}
// Loading is done
if (notifier) {
notifier->Update(100);
notifier->Done();
}
return true;
}
/* ************************************************************************ */
void
CMake::PrepareDatabase()
{
m_dbInitialized = false;
try {
/// Open database only for initializing
wxSQLite3Database db;
// Try to open database
db.Open(GetDatabaseFileName().GetFullPath());
// Not opened
if (!db.IsOpen())
return;
// Create tables
db.ExecuteUpdate("CREATE TABLE IF NOT EXISTS commands (name TEXT, desc TEXT)");
db.ExecuteUpdate("CREATE TABLE IF NOT EXISTS modules (name TEXT, desc TEXT)");
db.ExecuteUpdate("CREATE TABLE IF NOT EXISTS properties (name TEXT, desc TEXT)");
db.ExecuteUpdate("CREATE TABLE IF NOT EXISTS variables (name TEXT, desc TEXT)");
db.ExecuteUpdate("CREATE TABLE IF NOT EXISTS strings (name TEXT, desc TEXT)");
// Create indices
db.ExecuteUpdate("CREATE UNIQUE INDEX IF NOT EXISTS commands_idx ON commands(name)");
db.ExecuteUpdate("CREATE UNIQUE INDEX IF NOT EXISTS modules_idx ON modules(name)");
db.ExecuteUpdate("CREATE UNIQUE INDEX IF NOT EXISTS properties_idx ON properties(name)");
db.ExecuteUpdate("CREATE UNIQUE INDEX IF NOT EXISTS variables_idx ON variables(name)");
db.ExecuteUpdate("CREATE UNIQUE INDEX IF NOT EXISTS strings_idx ON strings(name)");
// Everything is OK
m_dbInitialized = true;
} catch (const wxSQLite3Exception& e) {
// Unable to use SQLite database
CL_ERROR("CMake DoPrepareDatabase error: %s", e.GetMessage());
}
}
/* ************************************************************************ */
bool
CMake::LoadFromCMake(LoadNotifier* notifier)
{
// Possible types
static const std::pair<wxString, HelpMap*> types[] = {
std::make_pair("command", &m_commands),
std::make_pair("module", &m_modules),
std::make_pair("property", &m_properties),
std::make_pair("variable", &m_variables)
// make_pair("policy", &m_policies)
};
static const int typesCount = sizeof(types) / sizeof(types[0]);
static const int PROGRESS = 90;
static const int STEP = PROGRESS / typesCount;
// Foreach all types
for (int i = 0; i < typesCount; ++i) {
// Notify??
if (notifier) {
// Stop request?
if (notifier->RequestStop())
return false;
notifier->Update(STEP * i);
}
// Load
if ( !LoadList(types[i].first, *types[i].second, notifier, STEP) )
return false;
}
return true;
}
/* ************************************************************************ */
bool
CMake::LoadFromDatabase()
{
if (!m_dbInitialized) {
return false;
}
try
{
/// Open database only for reading
wxSQLite3Database db;
// Open
db.Open(GetDatabaseFileName().GetFullPath());
// Not opened
if (!db.IsOpen())
return false;
// Strings - Version
{
wxSQLite3ResultSet res = db.ExecuteQuery("SELECT desc FROM strings WHERE name = 'version'");
if (res.NextRow()) {
m_version = res.GetAsString(0);
}
}
// No data stored
if (m_version.IsEmpty())
return false;
// Commands
{
wxSQLite3ResultSet res = db.ExecuteQuery("SELECT name, desc FROM commands");
while (res.NextRow()) {
m_commands[res.GetAsString(0)] = res.GetAsString(1);
}
}
// Modules
{
wxSQLite3ResultSet res = db.ExecuteQuery("SELECT name, desc FROM modules");
while (res.NextRow()) {
m_modules[res.GetAsString(0)] = res.GetAsString(1);
}
}
// Properties
{
wxSQLite3ResultSet res = db.ExecuteQuery("SELECT name, desc FROM properties");
while (res.NextRow()) {
m_properties[res.GetAsString(0)] = res.GetAsString(1);
}
}
// Variables
{
wxSQLite3ResultSet res = db.ExecuteQuery("SELECT name, desc FROM variables");
while (res.NextRow()) {
m_variables[res.GetAsString(0)] = res.GetAsString(1);
}
}
} catch (const wxSQLite3Exception& e) {
CL_ERROR("Error occured while loading data from CMake database: %s", e.GetMessage());
}
// Everything is loaded
return true;
}
/* ************************************************************************ */
void
CMake::StoreIntoDatabase()
{
if (!m_dbInitialized) {
CL_WARNING("CMake: can't store data into database. Database was not initialized properly");
return;
}
try
{
/// Open database only for writing
wxSQLite3Database db;
// Open
db.Open(GetDatabaseFileName().GetFullPath());
// Not opened
if (!db.IsOpen())
return;
db.Begin();
// Commands
{
db.ExecuteUpdate("DELETE FROM commands");
wxSQLite3Statement stmt = db.PrepareStatement("INSERT INTO commands (name, desc) VALUES(?, ?)");
for (HelpMap::const_iterator it = m_commands.begin(), ite = m_commands.end(); it != ite; ++it) {
stmt.Bind(1, it->first);
stmt.Bind(2, it->second);
stmt.ExecuteUpdate();
}
}
// Modules
{
db.ExecuteUpdate("DELETE FROM modules");
wxSQLite3Statement stmt = db.PrepareStatement("INSERT INTO modules (name, desc) VALUES(?, ?)");
for (HelpMap::const_iterator it = m_modules.begin(), ite = m_modules.end(); it != ite; ++it) {
stmt.Bind(1, it->first);
stmt.Bind(2, it->second);
stmt.ExecuteUpdate();
}
}
// Properties
{
db.ExecuteUpdate("DELETE FROM properties");
wxSQLite3Statement stmt = db.PrepareStatement("INSERT INTO properties (name, desc) VALUES(?, ?)");
for (HelpMap::const_iterator it = m_properties.begin(), ite = m_properties.end(); it != ite; ++it) {
stmt.Bind(1, it->first);
stmt.Bind(2, it->second);
stmt.ExecuteUpdate();
}
}
// Variables
{
db.ExecuteUpdate("DELETE FROM variables");
wxSQLite3Statement stmt = db.PrepareStatement("INSERT INTO variables (name, desc) VALUES(?, ?)");
for (HelpMap::const_iterator it = m_variables.begin(), ite = m_variables.end(); it != ite; ++it) {
stmt.Bind(1, it->first);
stmt.Bind(2, it->second);
stmt.ExecuteUpdate();
}
}
// Strings - Version
{
wxSQLite3Statement stmt = db.PrepareStatement("REPLACE INTO strings (name, desc) VALUES('version', ?)");
stmt.Bind(1, m_version);
stmt.ExecuteUpdate();
}
db.Commit();
} catch (wxSQLite3Exception &e) {
CL_ERROR("An error occured while storing CMake data into database: %s", e.GetMessage());
}
}
/* ************************************************************************ */
bool
CMake::LoadList(const wxString& type, CMake::HelpMap& list,
LoadNotifier* notifier, int limit)
{
const wxString command = GetPath().GetFullPath();
// Get list
wxArrayString names;
const wxString cmdList = command + " --help-" + type + "-list";
ProcUtils::SafeExecuteCommand(cmdList, names);
// Remove version
if (!names.IsEmpty())
names.RemoveAt(0);
const int notifyCount = (names.GetCount() / limit) + 1;
int loaded = 0;
// Foreach names
for (wxArrayString::const_iterator it = names.begin(), ite = names.end(); it != ite; ++it) {
if ( notifier && notifier->RequestStop() ) {
// Someone called 'wxThread::Delete'
return false;
}
// Trim name
wxString name = *it;
name.Trim().Trim(false);
// Export help
wxArrayString desc;
const wxString cmdItem = command + " --help-" + type + " \"" + name + "\"";
ProcUtils::SafeExecuteCommand(cmdItem, desc);
// Skip empty results
if (desc.IsEmpty())
continue;
// Remove first line (cmake version)
if (desc.Item(0).Matches("*cmake version*"))
desc.RemoveAt(0);
// Store help page
list[name] = CreateHtml(desc);
// One more loaded
loaded++;
// Add 1%
if (notifier && loaded == notifyCount) {
notifier->Inc(1);
loaded = 0;
}
}
return true;
}
/* ************************************************************************ */
|