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 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875
|
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright : (C) 2008 by Eran Ifrah
// file name : project.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.
//
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
#include "project.h"
#include "workspace.h"
#include <wx/app.h>
#include <wx/regex.h>
#include "globals.h"
#include <wx/log.h>
#include "fileextmanager.h"
#include "xmlutils.h"
#include <wx/tokenzr.h>
#include "wx/arrstr.h"
#include "dirsaver.h"
#include "globals.h"
#include "macros.h"
#include "wx_xml_compatibility.h"
#include "plugin.h"
#include "event_notifier.h"
#include <wx/sstream.h>
#include <wx/ffile.h>
#include "cl_command_event.h"
#include "environmentconfig.h"
#include "compiler_command_line_parser.h"
#include <algorithm>
#include "wxArrayStringAppender.h"
const wxString Project::STATIC_LIBRARY = wxT("Static Library");
const wxString Project::DYNAMIC_LIBRARY = wxT("Dynamic Library");
const wxString Project::EXECUTABLE = wxT("Executable");
static wxStringMap_t s_backticks;
#define EXCLUDE_FROM_BUILD_FOR_CONFIG "ExcludeProjConfig"
// ============---------------------
// Project class
// ============---------------------
Project::Project()
: m_tranActive(false)
, m_isModified(false)
, m_workspace(NULL)
{
}
Project::~Project()
{
m_vdCache.clear();
m_settings.Reset( NULL );
}
bool Project::Create(const wxString &name, const wxString &description, const wxString &path, const wxString &projType)
{
m_vdCache.clear();
m_fileName = path + wxFileName::GetPathSeparator() + name + wxT(".project");
m_fileName.MakeAbsolute();
m_projectPath = m_fileName.GetPath();
wxXmlNode *root = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("CodeLite_Project"));
m_doc.SetRoot(root);
m_doc.GetRoot()->AddProperty(wxT("Name"), name);
wxXmlNode *descNode = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("Description"));
XmlUtils::SetNodeContent(descNode, description);
m_doc.GetRoot()->AddChild(descNode);
// Create the default virtual directories
wxXmlNode *srcNode = NULL, *headNode = NULL;
srcNode = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("VirtualDirectory"));
srcNode->AddProperty(wxT("Name"), wxT("src"));
m_doc.GetRoot()->AddChild(srcNode);
headNode = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("VirtualDirectory"));
headNode->AddProperty(wxT("Name"), wxT("include"));
m_doc.GetRoot()->AddChild(headNode);
//creae dependencies node
wxXmlNode *depNode = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("Dependencies"));
root->AddChild(depNode);
// this will also create settings
SaveXmlFile();
ProjectSettingsPtr settings = GetSettings();
settings->SetProjectType(projType);
SetSettings(settings);
SetModified(true);
return true;
}
bool Project::Load(const wxString &path)
{
if ( !m_doc.Load(path) ) {
return false;
}
ConvertToUnixFormat(m_doc.GetRoot());
// Workaround WX bug: load the plugins data (GetAllPluginsData will strip any trailing whitespaces)
// and then set them back
std::map<wxString, wxString> pluginsData;
GetAllPluginsData(pluginsData);
SetAllPluginsData(pluginsData, false);
m_vdCache.clear();
m_fileName = path;
m_fileName.MakeAbsolute();
m_projectPath = m_fileName.GetPath();
SetModified(true);
SetProjectLastModifiedTime(GetFileLastModifiedTime());
DoUpdateProjectSettings();
return true;
}
wxXmlNode *Project::GetVirtualDir(const wxString &vdFullPath)
{
wxArrayString paths = wxStringTokenize( vdFullPath, ":", wxTOKEN_STRTOK );
// test the cache
std::map<wxString, wxXmlNode*>::iterator iter = m_vdCache.find(vdFullPath);
if(iter != m_vdCache.end()) {
return iter->second;
}
wxString filename = m_fileName.GetFullPath();
wxXmlNode *parent = m_doc.GetRoot();
for(size_t i=0; i<paths.GetCount(); ++i) {
wxString curpath = paths.Item(i);
parent = XmlUtils::FindNodeByName(parent, wxT("VirtualDirectory"), curpath);
if ( !parent ) {
m_vdCache[vdFullPath] = NULL;
return NULL;
}
}
// cache the result
m_vdCache[vdFullPath] = parent;
return parent;
}
wxXmlNode *Project::CreateVD(const wxString &vdFullPath, bool mkpath)
{
wxXmlNode *oldVd = GetVirtualDir(vdFullPath);
if ( oldVd ) {
// VD already exist
return oldVd;
}
wxStringTokenizer tkz(vdFullPath, wxT(":"));
wxXmlNode *parent = m_doc.GetRoot();
size_t count = tkz.CountTokens();
for (size_t i=0; i<count-1; i++) {
wxString token = tkz.NextToken();
wxXmlNode *p = XmlUtils::FindNodeByName(parent, wxT("VirtualDirectory"), token);
if ( !p ) {
if ( mkpath ) {
//add the node
p = new wxXmlNode(parent, wxXML_ELEMENT_NODE, wxT("VirtualDirectory"));
p->AddProperty(wxT("Name"), token);
} else {
return NULL;
}
}
parent = p;
}
wxXmlNode *node = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("VirtualDirectory"));
node->AddProperty(wxT("Name"), tkz.GetNextToken());
parent->AddChild(node);
//if not in transaction save the changes
if (!InTransaction()) {
SaveXmlFile();
}
// cache the result
m_vdCache[vdFullPath] = node;
return node;
}
bool Project::IsFileExist(const wxString &fileName)
{
//find the file under this node
// Convert the file path to be relative to
// the project path
DirSaver ds;
::wxSetWorkingDirectory(m_fileName.GetPath());
wxFileName tmp(fileName);
tmp.MakeRelativeTo(m_fileName.GetPath());
std::vector<wxFileName> files;
GetFiles(files);
for (size_t i=0; i<files.size(); i++) {
if (files.at(i).GetFullPath().CmpNoCase(tmp.GetFullPath(wxPATH_UNIX)) == 0) {
return true;
}
}
return false;
}
bool Project::AddFile(const wxString &fileName, const wxString &virtualDirPath)
{
wxXmlNode *vd = GetVirtualDir(virtualDirPath);
if ( !vd ) {
return false;
}
// Convert the file path to be relative to
// the project path
DirSaver ds;
::wxSetWorkingDirectory(m_fileName.GetPath());
wxFileName tmp(fileName);
tmp.MakeRelativeTo(m_fileName.GetPath());
// if we already have a file with the same name, return false
if (this->IsFileExist(fileName)) {
return false;
}
wxXmlNode *node = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("File"));
node->AddProperty(wxT("Name"), tmp.GetFullPath(wxPATH_UNIX));
vd->AddChild(node);
if (!InTransaction()) {
SaveXmlFile();
}
SetModified(true);
return true;
}
bool Project::CreateVirtualDir(const wxString &vdFullPath, bool mkpath)
{
return CreateVD(vdFullPath, mkpath) != NULL;
}
bool Project::DeleteVirtualDir(const wxString &vdFullPath)
{
wxXmlNode *vd = GetVirtualDir(vdFullPath);
if ( vd ) {
wxXmlNode *parent = vd->GetParent();
if ( parent ) {
parent->RemoveChild( vd );
}
// remove the entry from the cache
DoDeleteVDFromCache(vdFullPath);
delete vd;
SetModified(true);
return SaveXmlFile();
}
return false;
}
bool Project::RemoveFile(const wxString &fileName, const wxString &virtualDir)
{
wxXmlNode *vd = GetVirtualDir(virtualDir);
if ( !vd ) {
return false;
}
wxFileName tmp(fileName);
tmp.MakeRelativeTo(m_fileName.GetPath());
wxXmlNode *node = XmlUtils::FindNodeByName(vd, wxT("File"), tmp.GetFullPath(wxPATH_UNIX));
if ( node ) {
node->GetParent()->RemoveChild( node );
delete node;
} else {
wxLogMessage(wxT("Failed to remove file %s from project"), tmp.GetFullPath(wxPATH_UNIX).c_str());
}
SetModified(true);
if ( InTransaction() )
return true;
else
return SaveXmlFile();
}
wxString Project::GetName() const
{
return m_doc.GetRoot()->GetPropVal(wxT("Name"), wxEmptyString);
}
ProjectTreePtr Project::AsTree()
{
ProjectItem item(GetName(), GetName(), GetFileName().GetFullPath(), ProjectItem::TypeProject);
ProjectTreePtr ptp(new ProjectTree(item.Key(), item));
wxXmlNode *child = m_doc.GetRoot()->GetChildren();
while ( child ) {
RecursiveAdd(child, ptp, ptp->GetRoot());
child = child->GetNext();
}
return ptp;
}
void Project::RecursiveAdd(wxXmlNode *xmlNode, ProjectTreePtr &ptp, ProjectTreeNode *nodeParent)
{
// Build the key for this node
std::list<wxString> nameList;
wxXmlNode *parent = xmlNode->GetParent();
while ( parent ) {
nameList.push_front(parent->GetPropVal(wxT("Name"), wxEmptyString));
parent = parent->GetParent();
}
wxString key;
for (size_t i=0; i<nameList.size(); i++) {
key += nameList.front();
key += wxT(":");
nameList.pop_front();
}
key += xmlNode->GetPropVal(wxT("Name"), wxEmptyString);
// Create the tree node data
ProjectItem item;
if ( xmlNode->GetName() == wxT("Project") ) {
item = ProjectItem(key, xmlNode->GetPropVal(wxT("Name"), wxEmptyString), wxEmptyString, ProjectItem::TypeProject);
} else if ( xmlNode->GetName() == wxT("VirtualDirectory") ) {
item = ProjectItem(key, xmlNode->GetPropVal(wxT("Name"), wxEmptyString), wxEmptyString, ProjectItem::TypeVirtualDirectory);
} else if ( xmlNode->GetName() == wxT("File") ) {
wxFileName filename(xmlNode->GetPropVal(wxT("Name"), wxEmptyString));
// Convert this file name to absolute path
filename.MakeAbsolute(m_fileName.GetPath());
item = ProjectItem(key, filename.GetFullName(), filename.GetFullPath(), ProjectItem::TypeFile);
} else {
// un-recognised or not viewable item in the tree,
// skip it and its children
return;
}
ProjectTreeNode *newNode = ptp->AddChild(item.Key(), item, nodeParent);
// This node has children, add them as well
wxXmlNode *children = xmlNode->GetChildren();
while ( children ) {
RecursiveAdd(children, ptp, newNode);
children = children->GetNext();
}
SetModified(true);
}
bool Project::SaveXmlFile()
{
wxString projectXml;
wxStringOutputStream sos( &projectXml );
bool ok = m_doc.Save( sos );
wxFFile file(m_fileName.GetFullPath(), wxT("w+b"));
if ( !file.IsOpened() ) {
ok = false;
} else {
file.Write( projectXml );
file.Close();
}
SetProjectLastModifiedTime(GetFileLastModifiedTime());
EventNotifier::Get()->PostFileSavedEvent( m_fileName.GetFullPath() );
DoUpdateProjectSettings();
return ok;
}
void Project::Save()
{
m_tranActive = false;
if ( m_doc.IsOk() ) {
SaveXmlFile();
}
}
void Project::GetFilesByVirtualDir(const wxString &vdFullPath, wxArrayString &files)
{
wxXmlNode *vd = GetVirtualDir(vdFullPath);
if ( vd ) {
wxXmlNode *child = vd->GetChildren();
while ( child ) {
if ( child->GetName() == wxT("File")) {
wxFileName fileName(
child->GetPropVal(wxT("Name"), wxEmptyString)
);
fileName.MakeAbsolute(m_fileName.GetPath());
files.Add(fileName.GetFullPath());
}
child = child->GetNext();
}
}
}
wxString Project::GetFiles(bool absPath)
{
std::vector<wxFileName> files;
GetFiles(files, absPath);
wxString temp;
for (size_t i = 0; i < files.size(); i++)
temp << wxT("\"") << files.at(i).GetFullPath() << wxT("\" ");
if(temp.IsEmpty() == false)
temp.RemoveLast();
return temp;
}
void Project::GetFiles(wxStringSet_t& files)
{
DirSaver ds;
FileNameVector_t v;
::wxSetWorkingDirectory(m_fileName.GetPath());
GetFiles(m_doc.GetRoot(), v, true);
for(size_t i=0; i<v.size(); i++) {
files.insert(v.at(i).GetFullPath());
}
}
void Project::GetFiles(std::vector<wxFileName> &files, bool absPath)
{
if (absPath) {
GetFiles(m_doc.GetRoot(), files, true);
} else {
GetFiles(m_doc.GetRoot(), files, false);
}
}
void Project::GetFiles(wxXmlNode *parent, std::vector<wxFileName> &files, bool absPath)
{
if ( !parent ) {
return;
}
wxXmlNode *child = parent->GetChildren();
while (child) {
if (child->GetName() == wxT("File")) {
wxString fileName = child->GetPropVal(wxT("Name"), wxEmptyString);
wxFileName tmp(fileName);
if ( absPath ) {
tmp.MakeAbsolute( GetProjectPath() );
}
files.push_back(tmp);
} else if (child->GetChildren()) {// we could also add a check for VirtualDirectory only
GetFiles(child, files, absPath);
}
child = child->GetNext();
}
}
wxXmlNode* Project::GetProjectEditorOptions() const
{
return XmlUtils::FindFirstByTagName(m_doc.GetRoot(), wxT("Options"));
}
void Project::SetProjectEditorOptions(LocalOptionsConfigPtr opts)
{
wxXmlNode *parent = m_doc.GetRoot();
wxXmlNode *oldOptions = XmlUtils::FindFirstByTagName(parent, wxT("Options"));
if (oldOptions) {
oldOptions->GetParent()->RemoveChild(oldOptions);
delete oldOptions;
}
parent->AddChild(opts->ToXml());
SaveXmlFile();
}
ProjectSettingsPtr Project::GetSettings() const
{
return m_settings;
}
void Project::SetSettings(ProjectSettingsPtr settings)
{
wxXmlNode *oldSettings = XmlUtils::FindFirstByTagName(m_doc.GetRoot(), wxT("Settings"));
if (oldSettings) {
oldSettings->GetParent()->RemoveChild(oldSettings);
delete oldSettings;
}
m_doc.GetRoot()->AddChild(settings->ToXml());
// SaveXmlFile will update the internal pointer
SaveXmlFile();
}
void Project::SetGlobalSettings(BuildConfigCommonPtr globalSettings)
{
wxXmlNode *settings = XmlUtils::FindFirstByTagName(m_doc.GetRoot(), wxT("Settings"));
wxXmlNode *oldSettings = XmlUtils::FindFirstByTagName(settings, wxT("GlobalSettings"));
if (oldSettings) {
oldSettings->GetParent()->RemoveChild(oldSettings);
delete oldSettings;
}
settings->AddChild(globalSettings->ToXml());
SaveXmlFile();
}
wxArrayString Project::GetDependencies() const
{
wxArrayString result;
wxXmlNode *node = XmlUtils::FindFirstByTagName(m_doc.GetRoot(), wxT("Dependencies"));
if (node) {
wxXmlNode *child = node->GetChildren();
while (child) {
if (child->GetName() == wxT("Project")) {
result.Add(XmlUtils::ReadString(child, wxT("Name")));
}
child = child->GetNext();
}
}
return result;
}
void Project::SetModified(bool mod)
{
m_isModified = mod;
}
bool Project::IsModified()
{
return m_isModified;
}
wxString Project::GetDescription() const
{
wxXmlNode *root = m_doc.GetRoot();
if (root) {
wxXmlNode *node = XmlUtils::FindFirstByTagName(root, wxT("Description"));
if (node) {
return node->GetNodeContent();
}
}
return wxEmptyString;
}
void Project::CopyTo(const wxString& new_path, const wxString& new_name, const wxString& description)
{
// first save the xml document to the destination folder
wxString newFile = new_path + new_name + wxT(".project");
if ( !m_doc.Save( newFile ) ) {
return;
}
// load the new xml and modify it
wxXmlDocument doc;
if ( !doc.Load( newFile ) ) {
return;
}
// update the 'Name' property
XmlUtils::UpdateProperty(doc.GetRoot(), wxT("Name"), new_name);
// set description
wxXmlNode *descNode(NULL);
// update the description
descNode = XmlUtils::FindFirstByTagName(doc.GetRoot(), wxT("Description"));
if (!descNode) {
descNode = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("Description"));
doc.GetRoot()->AddChild(descNode);
}
XmlUtils::SetNodeContent(descNode, description);
// Remove the 'Dependencies'
wxXmlNode *deps = doc.GetRoot()->GetChildren();
while(deps) {
if(deps->GetName() == wxT("Dependencies")) {
doc.GetRoot()->RemoveChild(deps);
delete deps;
// restart the search from the begining
deps = doc.GetRoot()->GetChildren();
} else {
// try next child
deps = deps->GetNext();
}
}
// add an empty deps node
deps = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("Dependencies"));
doc.GetRoot()->AddChild(deps);
// Remove virtual folders
wxXmlNode *vd = XmlUtils::FindFirstByTagName(doc.GetRoot(), wxT("VirtualDirectory"));
while (vd) {
doc.GetRoot()->RemoveChild( vd );
delete vd;
vd = XmlUtils::FindFirstByTagName(doc.GetRoot(), wxT("VirtualDirectory"));
}
// add all files under this path
std::vector<wxFileName> files;
GetFiles(files, true);
wxXmlNode *srcNode(NULL);
wxXmlNode *headNode(NULL);
wxXmlNode *rcNode(NULL);
// copy the files to their new location
for (size_t i=0; i<files.size(); i++) {
wxFileName fn = files.at(i);
wxCopyFile(fn.GetFullPath(), new_path + wxT("/") + fn.GetFullName());
wxXmlNode *file_node = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("File"));
file_node->AddProperty(wxT("Name"), fn.GetFullName());
switch ( FileExtManager::GetType( fn.GetFullName() ) ) {
case FileExtManager::TypeSourceC:
case FileExtManager::TypeSourceCpp:
// source file
if ( !srcNode ) {
srcNode = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("VirtualDirectory"));
srcNode->AddProperty(wxT("Name"), wxT("src"));
doc.GetRoot()->AddChild(srcNode);
}
srcNode->AddChild(file_node);
break;
case FileExtManager::TypeHeader:
// header file
if (!headNode) {
headNode = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("VirtualDirectory"));
headNode->AddProperty(wxT("Name"), wxT("include"));
doc.GetRoot()->AddChild(headNode);
}
headNode->AddChild(file_node);
break;
default:
// resource file
if ( !rcNode ) {
rcNode = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("VirtualDirectory"));
rcNode->AddProperty(wxT("Name"), wxT("resources"));
doc.GetRoot()->AddChild(rcNode);
}
rcNode->AddChild(file_node);
break;
}
}
doc.Save(newFile);
}
void Project::SetFiles(ProjectPtr src)
{
// first remove all the virtual directories from this project
// Remove virtual folders
wxXmlNode *vd = XmlUtils::FindFirstByTagName(m_doc.GetRoot(), wxT("VirtualDirectory"));
while (vd) {
m_doc.GetRoot()->RemoveChild( vd );
delete vd;
vd = XmlUtils::FindFirstByTagName(m_doc.GetRoot(), wxT("VirtualDirectory"));
}
// copy the virtual directories from the src project
wxXmlNode *child = src->m_doc.GetRoot()->GetChildren();
while ( child ) {
if ( child->GetName() == wxT("VirtualDirectory") ) {
// create a new VirtualDirectory like this one
wxXmlNode *newNode = new wxXmlNode(*child);
m_doc.GetRoot()->AddChild(newNode);
}
child = child->GetNext();
}
SaveXmlFile();
}
bool Project::RenameFile(const wxString& oldName, const wxString& virtualDir, const wxString& newName)
{
wxXmlNode *vd = GetVirtualDir(virtualDir);
if ( !vd ) {
return false;
}
// Convert the file path to be relative to
// the project path
DirSaver ds;
::wxSetWorkingDirectory(m_fileName.GetPath());
wxFileName tmp(oldName);
tmp.MakeRelativeTo(m_fileName.GetPath());
wxXmlNode *node = XmlUtils::FindNodeByName(vd, wxT("File"), tmp.GetFullPath(wxPATH_UNIX));
if ( node ) {
// update the new name
tmp.SetFullName(newName);
XmlUtils::UpdateProperty(node, wxT("Name"), tmp.GetFullPath(wxPATH_UNIX));
}
SetModified(true);
if ( InTransaction() )
return true;
else
return SaveXmlFile();
}
wxString Project::GetVDByFileName(const wxString& file)
{
//find the file under this node
// Convert the file path to be relative to
// the project path
DirSaver ds;
::wxSetWorkingDirectory(m_fileName.GetPath());
wxFileName tmp(file);
tmp.MakeRelativeTo(m_fileName.GetPath());
wxString path(wxEmptyString);
wxXmlNode *fileNode = FindFile(m_doc.GetRoot(), tmp.GetFullPath(wxPATH_UNIX));
if (fileNode) {
wxXmlNode *parent = fileNode->GetParent();
while ( parent ) {
if (parent->GetName() == wxT("VirtualDirectory")) {
path.Prepend(parent->GetPropVal(wxT("Name"), wxEmptyString));
path.Prepend(wxT(":"));
} else {
break;
}
parent = parent->GetParent();
}
}
wxString trunc_path(path);
path.StartsWith(wxT(":"), &trunc_path);
return trunc_path;
}
wxXmlNode* Project::FindFile(wxXmlNode* parent, const wxString& file)
{
wxXmlNode *child = parent->GetChildren();
while ( child ) {
wxString name = child->GetName();
if (name == wxT("File") && child->GetPropVal(wxT("Name"), wxEmptyString) == file) {
return child;
}
if (child->GetName() == wxT("VirtualDirectory")) {
wxXmlNode *n = FindFile(child, file);
if (n) {
return n;
}
}
child = child->GetNext();
}
return NULL;
}
bool Project::RenameVirtualDirectory(const wxString& oldVdPath, const wxString& newName)
{
wxXmlNode *vdNode = GetVirtualDir(oldVdPath);
if (vdNode) {
XmlUtils::UpdateProperty(vdNode, wxT("Name"), newName);
return SaveXmlFile();
}
return false;
}
wxArrayString Project::GetDependencies(const wxString& configuration) const
{
wxArrayString result;
// dependencies are located directly under the root level
wxXmlNode *node = m_doc.GetRoot()->GetChildren();
while (node) {
if ( node->GetName() == wxT("Dependencies") && node->GetPropVal(wxT("Name"), wxEmptyString) == configuration) {
// we have our match
wxXmlNode *child = node->GetChildren();
while (child) {
if (child->GetName() == wxT("Project")) {
result.Add(XmlUtils::ReadString(child, wxT("Name")));
}
child = child->GetNext();
}
return result;
}
node = node->GetNext();
}
// if we are here, it means no match for the given configuration
// return the default dependencies
return GetDependencies();
}
void Project::SetDependencies(wxArrayString& deps, const wxString& configuration)
{
// first try to locate the old node
wxXmlNode *node = m_doc.GetRoot()->GetChildren();
while (node) {
if ( node->GetName() == wxT("Dependencies") && node->GetPropVal(wxT("Name"), wxEmptyString) == configuration) {
// we have our match
node->GetParent()->RemoveChild(node);
delete node;
break;
}
node = node->GetNext();
}
// create new dependencies node
node = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("Dependencies"));
node->AddProperty(wxT("Name"), configuration);
m_doc.GetRoot()->AddChild(node);
//create a node for each dependency in the array
for (size_t i=0; i<deps.GetCount(); i++) {
wxXmlNode *child = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("Project"));
child->AddProperty(wxT("Name"), deps.Item(i));
node->AddChild(child);
}
//save changes
SaveXmlFile();
SetModified(true);
}
void Project::GetFiles(std::vector<wxFileName>& files, std::vector<wxFileName>& absFiles)
{
DirSaver ds;
::wxSetWorkingDirectory(m_fileName.GetPath());
GetFiles(m_doc.GetRoot(), files, absFiles);
}
void Project::GetFiles(wxXmlNode *parent, std::vector<wxFileName>& files, std::vector<wxFileName>& absFiles)
{
if ( !parent ) {
return;
}
wxXmlNode *child = parent->GetChildren();
while (child) {
if (child->GetName() == wxT("File")) {
wxString fileName = child->GetPropVal(wxT("Name"), wxEmptyString);
wxFileName tmp(fileName);
// append the file as it appears
files.push_back(tmp);
// convert to absolute path
tmp.MakeAbsolute();
absFiles.push_back(tmp);
} else if (child->GetChildren()) {// we could also add a check for VirtualDirectory only
GetFiles(child, files, absFiles);
}
child = child->GetNext();
}
}
bool Project::FastAddFile(const wxString& fileName, const wxString& virtualDir)
{
wxXmlNode *vd = GetVirtualDir(virtualDir);
if ( !vd ) {
return false;
}
// Convert the file path to be relative to
// the project path
DirSaver ds;
::wxSetWorkingDirectory(m_fileName.GetPath());
wxFileName tmp(fileName);
tmp.MakeRelativeTo(m_fileName.GetPath());
wxXmlNode *node = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("File"));
node->AddProperty(wxT("Name"), tmp.GetFullPath(wxPATH_UNIX));
vd->AddChild(node);
if (!InTransaction()) {
SaveXmlFile();
}
SetModified(true);
return true;
}
void Project::DoGetVirtualDirectories(wxXmlNode* parent, TreeNode<wxString, VisualWorkspaceNode>* tree)
{
wxXmlNode *child = parent->GetChildren();
while(child) {
if(child->GetName() == wxT("VirtualDirectory")) {
VisualWorkspaceNode data;
data.name = XmlUtils::ReadString(child, wxT("Name"));
data.type = ProjectItem::TypeVirtualDirectory;
TreeNode<wxString, VisualWorkspaceNode>* node = new TreeNode<wxString, VisualWorkspaceNode>(data.name, data, tree);
tree->AddChild(node);
// test to see if it has children
if(child->GetChildren()) {
DoGetVirtualDirectories(child, node);
}
}
child = child->GetNext();
}
}
TreeNode<wxString, VisualWorkspaceNode>* Project::GetVirtualDirectories(TreeNode<wxString, VisualWorkspaceNode> *workspace)
{
VisualWorkspaceNode data;
data.name = GetName();
data.type = ProjectItem::TypeProject;
TreeNode<wxString, VisualWorkspaceNode> *parent = new TreeNode<wxString, VisualWorkspaceNode>(GetName(), data, workspace);
DoGetVirtualDirectories(m_doc.GetRoot(), parent);
workspace->AddChild(parent);
return parent;
}
bool Project::GetUserData(const wxString& name, SerializedObject* obj)
{
if(!m_doc.IsOk()) {
return false;
}
Archive arch;
wxXmlNode *userData = XmlUtils::FindFirstByTagName(m_doc.GetRoot(), wxT("UserData"));
if(userData) {
wxXmlNode *dataNode = XmlUtils::FindNodeByName(userData, wxT("Data"), name);
if(dataNode) {
arch.SetXmlNode(dataNode);
obj->DeSerialize(arch);
return true;
}
}
return false;
}
bool Project::SetUserData(const wxString& name, SerializedObject* obj)
{
if(!m_doc.IsOk()) {
return false;
}
Archive arch;
// locate the 'UserData' node
wxXmlNode *userData = XmlUtils::FindFirstByTagName(m_doc.GetRoot(), wxT("UserData"));
if( !userData ) {
userData = new wxXmlNode(m_doc.GetRoot(), wxXML_ELEMENT_NODE, wxT("UserData"));
}
// try to find a previous data stored under the same name, if we succeed - remove it
wxXmlNode *dataNode = XmlUtils::FindNodeByName(userData, wxT("Data"), name);
if(dataNode) {
// remove old node
userData->RemoveChild(dataNode);
delete dataNode;
}
// create a new node and set the userData node as the parent
dataNode = new wxXmlNode(userData, wxXML_ELEMENT_NODE, wxT("Data"));
dataNode->AddProperty(wxT("Name"), name);
// serialize the data
arch.SetXmlNode(dataNode);
obj->Serialize(arch);
return SaveXmlFile();
}
void Project::SetProjectInternalType(const wxString& internalType)
{
XmlUtils::UpdateProperty(m_doc.GetRoot(), wxT("InternalType"), internalType);
}
wxString Project::GetProjectInternalType() const
{
return m_doc.GetRoot()->GetPropVal(wxT("InternalType"), wxEmptyString);
}
void Project::GetAllPluginsData(std::map<wxString, wxString>& pluginsDataMap)
{
if(!m_doc.IsOk()) {
return;
}
// locate the 'Plugins' node
wxXmlNode *plugins = XmlUtils::FindFirstByTagName(m_doc.GetRoot(), wxT("Plugins"));
if( !plugins ) {
return;
}
wxXmlNode *child = plugins->GetChildren();
while( child ) {
if( child->GetName() == wxT("Plugin") ) {
// get the content
wxString content = child->GetNodeContent();
// overcome bug in WX where CDATA content comes out with extra \n and 4xspaces
content.Trim().Trim(false);
pluginsDataMap[child->GetPropVal(wxT("Name"), wxEmptyString)] = content;
}
child = child->GetNext();
}
}
wxString Project::GetPluginData(const wxString& pluginName)
{
if(!m_doc.IsOk()) {
return wxEmptyString;
}
// locate the 'Plugins' node
wxXmlNode *plugins = XmlUtils::FindFirstByTagName(m_doc.GetRoot(), wxT("Plugins"));
if( !plugins ) {
return wxEmptyString;
}
// find the node and return its content
wxXmlNode *dataNode = XmlUtils::FindNodeByName(plugins, wxT("Plugin"), pluginName);
if( dataNode ) {
return dataNode->GetNodeContent().Trim().Trim(false);
}
return wxEmptyString;
}
void Project::SetPluginData(const wxString& pluginName, const wxString& data, bool saveToXml)
{
if(!m_doc.IsOk()) {
return ;
}
// locate the 'Plugins' node
wxXmlNode *plugins = XmlUtils::FindFirstByTagName(m_doc.GetRoot(), wxT("Plugins"));
if( !plugins ) {
plugins = new wxXmlNode(m_doc.GetRoot(), wxXML_ELEMENT_NODE, wxT("Plugins"));
}
wxXmlNode *plugin = XmlUtils::FindNodeByName(plugins, wxT("Plugin"), pluginName);
if( !plugin ) {
plugin = new wxXmlNode(plugins, wxXML_ELEMENT_NODE, wxT("Plugin"));
plugin->AddProperty(wxT("Name"), pluginName);
}
wxString tmpData ( data );
tmpData.Trim().Trim(false);
XmlUtils::SetCDATANodeContent(plugin, tmpData);
if ( saveToXml ) {
SaveXmlFile();
}
}
void Project::SetAllPluginsData(const std::map<wxString, wxString>& pluginsDataMap, bool saveToFile /* true */)
{
if(!m_doc.IsOk()) {
return;
}
// locate the 'Plugins' node
wxXmlNode *plugins = XmlUtils::FindFirstByTagName(m_doc.GetRoot(), wxT("Plugins"));
if( plugins ) {
m_doc.GetRoot()->RemoveChild( plugins );
delete plugins;
}
std::map<wxString, wxString>::const_iterator iter = pluginsDataMap.begin();
for(; iter != pluginsDataMap.end(); iter ++) {
SetPluginData( iter->first, iter->second, saveToFile );
}
if ( saveToFile ) {
SaveXmlFile();
}
}
time_t Project::GetFileLastModifiedTime() const
{
return GetFileModificationTime(GetFileName());
}
void Project::ConvertToUnixFormat(wxXmlNode* parent)
{
if(!parent)
return;
wxXmlNode *child = parent->GetChildren();
while(child) {
if(child->GetName() == wxT("VirtualDirectory")) {
ConvertToUnixFormat(child);
} else if(child->GetName() == wxT("File")) {
wxXmlProperty *props = child->GetProperties();
// Convert the path to unix format
while ( props ) {
if(props->GetName() == wxT("Name")) {
wxString val = props->GetValue();
val.Replace(wxT("\\"), wxT("/"));
props->SetValue(val);
break;
}
props = props->GetNext();
}
}
child = child->GetNext();
}
}
wxString Project::GetBestPathForVD(const wxString& vdPath)
{
// Project name
const wxString basePath = GetFileName().GetPath();
wxString bestPath;
// try to open the dir dialog as close as we can to the virtual folder ones
int matches(0);
wxArrayString subDirs = wxStringTokenize(vdPath, wxT(":"), wxTOKEN_STRTOK);
bestPath = basePath;
for(size_t i=0; i<subDirs.GetCount(); i++) {
wxFileName fn(bestPath + wxFileName::GetPathSeparator() + subDirs.Item(i), wxEmptyString);
if(fn.DirExists()) {
bestPath << wxFileName::GetPathSeparator() << subDirs.Item(i);
matches++;
} else {
break;
}
}
if(matches) {
return bestPath;
}
// Could not find any match for the virtual directory when tested
// directly under the project path. Try it again using a path from
// the first file that we could find under the virtual directory
wxArrayString files;
GetFilesByVirtualDir(vdPath, files);
if(files.IsEmpty() == false) {
wxFileName f(files.Item(0));
if(f.MakeAbsolute(GetFileName().GetPath())) {
bestPath = f.GetPath();
return bestPath;
}
}
// all failed, return the project path as our default path
return basePath;
}
wxArrayString Project::GetIncludePaths(bool clearCache)
{
wxArrayString paths;
BuildMatrixPtr matrix = GetWorkspace()->GetBuildMatrix();
if(!matrix) {
return paths;
}
wxString workspaceSelConf = matrix->GetSelectedConfigurationName();
wxString projectSelConf = matrix->GetProjectSelectedConf(workspaceSelConf, GetName());
BuildConfigPtr buildConf = GetWorkspace()->GetProjBuildConf(this->GetName(), projectSelConf);
// for non custom projects, take the settings from the build configuration
if(buildConf && !buildConf->IsCustomBuild()) {
// Apply the environment
EnvSetter es(NULL, NULL, GetName());
if ( clearCache ) {
s_backticks.clear();
}
// Get the include paths and add them
wxString projectIncludePaths = buildConf->GetIncludePath();
wxArrayString projectIncludePathsArr = ::wxStringTokenize(projectIncludePaths, wxT(";"), wxTOKEN_STRTOK);
for(size_t i=0; i<projectIncludePathsArr.GetCount(); i++) {
wxFileName fn;
if(projectIncludePathsArr.Item(i) == wxT("..")) {
fn = wxFileName(GetFileName().GetPath(), wxT(""));
fn.RemoveLastDir();
} else if(projectIncludePathsArr.Item(i) == wxT(".")) {
fn = wxFileName(GetFileName().GetPath(), wxT(""));
} else {
fn = projectIncludePathsArr.Item(i);
if(fn.IsRelative()) {
fn.MakeAbsolute( GetFileName().GetPath() );
}
}
paths.Add( fn.GetFullPath() );
}
// get the compiler options and add them
wxString projectCompileOptions = buildConf->GetCompileOptions();
wxArrayString projectCompileOptionsArr = wxStringTokenize(projectCompileOptions, wxT(";"), wxTOKEN_STRTOK);
for(size_t i=0; i<projectCompileOptionsArr.GetCount(); i++) {
wxString cmpOption (projectCompileOptionsArr.Item(i));
cmpOption.Trim().Trim(false);
// expand backticks, if the option is not a backtick the value remains
// unchanged
wxArrayString includePaths = DoBacktickToIncludePath(cmpOption);
if ( !includePaths.IsEmpty() ) {
paths.insert(paths.end(), includePaths.begin(), includePaths.end());
}
}
}
return paths;
}
wxArrayString Project::DoBacktickToPreProcessors(const wxString& backtick)
{
wxArrayString paths;
wxString cmpOption = DoExpandBacktick( backtick );
CompilerCommandLineParser cclp(cmpOption);
return cclp.GetMacros();
}
wxArrayString Project::DoBacktickToIncludePath(const wxString& backtick)
{
wxArrayString paths;
wxString cmpOption = DoExpandBacktick( backtick );
CompilerCommandLineParser cclp(cmpOption, GetFileName().GetPath());
return cclp.GetIncludes();
}
void Project::DoDeleteVDFromCache(const wxString& vd)
{
NodeMap_t::iterator iter = m_vdCache.lower_bound(vd);
if ( iter == m_vdCache.end() )
return;
if ( iter->first.StartsWith(vd) == false )
return;
NodeMap_t::iterator first = iter;
++iter;
// Loop and search for the first iterator that does not start with our prefix
for(; iter != m_vdCache.end(); ++iter) {
if ( iter->first.StartsWith(vd) == false )
break;
}
m_vdCache.erase(first, iter);
}
void Project::ClearAllVirtDirs()
{
// remove all the virtual directories from this project
wxXmlNode *vd = XmlUtils::FindFirstByTagName(m_doc.GetRoot(), wxT("VirtualDirectory"));
while (vd) {
m_doc.GetRoot()->RemoveChild( vd );
delete vd;
vd = XmlUtils::FindFirstByTagName(m_doc.GetRoot(), wxT("VirtualDirectory"));
}
m_vdCache.clear();
SetModified(true);
SaveXmlFile();
}
wxString Project::GetProjectIconName() const
{
return m_doc.GetRoot()->GetPropVal(wxT("IconIndex"), "gear16");
}
void Project::GetReconciliationData(wxString& toplevelDir, wxString& extensions, wxArrayString& ignoreFiles, wxArrayString& excludePaths, wxArrayString& regexes)
{
if (!m_doc.IsOk()) {
return;
}
// locate the 'Reconciliation' node
wxXmlNode* reconciliation = XmlUtils::FindFirstByTagName(m_doc.GetRoot(), wxT("Reconciliation"));
if( !reconciliation ) {
return;
}
wxXmlNode* dirnode = XmlUtils::FindFirstByTagName(reconciliation, wxT("Topleveldir"));
if (dirnode) {
toplevelDir = dirnode->GetNodeContent().Trim().Trim(false);
}
wxXmlNode* extnode = XmlUtils::FindFirstByTagName(reconciliation, wxT("Extensions"));
if (extnode) {
extensions = extnode->GetNodeContent().Trim().Trim(false);
}
wxXmlNode* ignorefilesnode = XmlUtils::FindFirstByTagName(reconciliation, wxT("Ignorefiles"));
if (ignorefilesnode) {
ignoreFiles = XmlUtils::ChildNodesContentToArray(ignorefilesnode, "Ignore");
}
wxXmlNode* excludesnode = XmlUtils::FindFirstByTagName(reconciliation, wxT("Excludepaths"));
if (excludesnode) {
excludePaths = XmlUtils::ChildNodesContentToArray(excludesnode, "Path");
}
wxXmlNode* regexnode = XmlUtils::FindFirstByTagName(reconciliation, wxT("Regexes"));
if (regexnode) {
regexes = XmlUtils::ChildNodesContentToArray(regexnode, "Regex");
}
}
void Project::SetReconciliationData(const wxString& toplevelDir, const wxString& extensions, const wxArrayString& ignoreFiles, const wxArrayString& excludePaths, wxArrayString& regexes)
{
if (!m_doc.IsOk()) {
return;
}
wxXmlNode* reconciliation = XmlUtils::FindFirstByTagName(m_doc.GetRoot(), wxT("Reconciliation"));
if (!reconciliation) {
reconciliation = new wxXmlNode(m_doc.GetRoot(), wxXML_ELEMENT_NODE, wxT("Reconciliation"));
}
wxXmlNode* dirnode = XmlUtils::FindFirstByTagName(reconciliation, wxT("Topleveldir"));
if (!dirnode) {
dirnode = new wxXmlNode(reconciliation, wxXML_ELEMENT_NODE, wxT("Topleveldir"));
}
XmlUtils::SetNodeContent(dirnode, toplevelDir);
wxXmlNode* extsnode = XmlUtils::FindFirstByTagName(reconciliation, wxT("Extensions"));
if (!extsnode) {
extsnode = new wxXmlNode(reconciliation, wxXML_ELEMENT_NODE, wxT("Extensions"));
}
wxString tmpData(extensions);
tmpData.Trim().Trim(false);
XmlUtils::SetCDATANodeContent(extsnode, tmpData);
wxXmlNode* ignorefilesnode = XmlUtils::FindFirstByTagName(reconciliation, wxT("Ignorefiles"));
if (!ignorefilesnode) {
ignorefilesnode = new wxXmlNode(reconciliation, wxXML_ELEMENT_NODE, wxT("Ignorefiles"));
} else {
XmlUtils::RemoveChildren(ignorefilesnode);
}
for (size_t n = 0; n < ignoreFiles.GetCount(); ++n) {
wxXmlNode* pathnode = new wxXmlNode(ignorefilesnode, wxXML_ELEMENT_NODE, "Ignore");
XmlUtils::SetNodeContent(pathnode, ignoreFiles.Item(n));
}
wxXmlNode* excludesnode = XmlUtils::FindFirstByTagName(reconciliation, wxT("Excludepaths"));
if (!excludesnode) {
excludesnode = new wxXmlNode(reconciliation, wxXML_ELEMENT_NODE, wxT("Excludepaths"));
} else {
XmlUtils::RemoveChildren(excludesnode);
}
for (size_t n = 0; n < excludePaths.GetCount(); ++n) {
wxXmlNode* pathnode = new wxXmlNode(excludesnode, wxXML_ELEMENT_NODE, "Path");
XmlUtils::SetNodeContent(pathnode, excludePaths.Item(n));
}
wxXmlNode* regexnode = XmlUtils::FindFirstByTagName(reconciliation, wxT("Regexes"));
if (!regexnode) {
regexnode = new wxXmlNode(reconciliation, wxXML_ELEMENT_NODE, wxT("Regexes"));
} else {
XmlUtils::RemoveChildren(regexnode);
}
for (size_t n = 0; n < regexes.GetCount(); ++n) {
wxXmlNode* itemnode = new wxXmlNode(regexnode, wxXML_ELEMENT_NODE, "Regex");
XmlUtils::SetNodeContent(itemnode, regexes.Item(n));
}
SaveXmlFile();
}
void Project::GetFilesMetadata(Project::FileInfoVector_t& files) const
{
std::queue<wxXmlNode*> elements;
if ( !m_doc.IsOk() || !m_doc.GetRoot())
return;
elements.push( m_doc.GetRoot() );
files.reserve( 1000 ); // make room for at least for 1000 files (should be enough for most projects)
while ( !elements.empty() ) {
wxXmlNode *element = elements.front();
elements.pop();
while ( element ) {
if ( element->GetName() == wxT("File") ) {
// files are kept relative to the project file
wxString fileName = element->GetAttribute(wxT("Name"), wxEmptyString);
wxFileName tmp(fileName);
tmp.MakeAbsolute( GetProjectPath() );
FileInfo fi;
fi.SetFilenameRelpath(fileName);
fi.SetFilename( tmp.GetFullPath() );
fi.SetFlags( XmlUtils::ReadLong(element, "Flags", 0) );
wxString excludeConfigs = XmlUtils::ReadString(element, EXCLUDE_FROM_BUILD_FOR_CONFIG);
fi.SetExcludeConfigs( ::wxStringTokenize(excludeConfigs, ";", wxTOKEN_STRTOK) );
fi.SetVirtualFolder( DoFormatVirtualFolderName(element) );
files.push_back( fi );
} else if ( element->GetChildren() ) {
elements.push( element->GetChildren() );
}
element = element->GetNext();
}
}
// releaes any un-needed memory
Project::FileInfoVector_t( files ).swap( files );
}
wxString Project::DoFormatVirtualFolderName(const wxXmlNode* node) const
{
// we assume that 'node' is a 'File' element
wxString name;
wxXmlNode *p = node->GetParent();
std::list<wxString> q;
while ( p ) {
if ( p->GetName() == "VirtualDirectory" )
q.push_front( p->GetPropVal("Name", "") );
else
break;
p = p->GetParent();
}
while ( !q.empty() ) {
name << q.front() << ":";
q.pop_front();
}
if ( name.IsEmpty() == false )
name.RemoveLast();
return name;
}
void Project::SetFileFlags(const wxString& fileName, const wxString& virtualDirPath, size_t flags)
{
wxXmlNode *vdNode = GetVirtualDir(virtualDirPath);
if ( !vdNode ) {
return;
}
// locate our file
wxFileName tmp(fileName);
tmp.MakeRelativeTo(m_fileName.GetPath());
wxString filepath = tmp.GetFullPath(wxPATH_UNIX);
wxXmlNode *fileNode = XmlUtils::FindNodeByName(vdNode, "File", filepath);
if ( !fileNode ) {
return;
}
// we have located the file node
// updat the flags
XmlUtils::UpdateProperty(fileNode, "Flags", wxString() << flags );
SaveXmlFile();
}
size_t Project::GetFileFlags(const wxString& fileName, const wxString& virtualDirPath)
{
wxXmlNode *vdNode = GetVirtualDir(virtualDirPath);
if ( !vdNode ) {
return 0;
}
// locate our file
wxFileName tmp(fileName);
tmp.MakeRelativeTo(m_fileName.GetPath());
wxString filepath = tmp.GetFullPath(wxPATH_UNIX);
wxXmlNode *fileNode = XmlUtils::FindNodeByName(vdNode, "File", filepath);
if ( !fileNode ) {
return 0;
}
return XmlUtils::ReadLong(fileNode, "Flags", 0);
}
wxArrayString Project::GetExcludeConfigForFile(const wxString& filename, const wxString& virtualDirPath)
{
wxArrayString configs;
wxXmlNode *vdNode = GetVirtualDir(virtualDirPath);
if ( !vdNode ) {
return configs;
}
// locate our file
wxFileName tmp(filename);
tmp.MakeRelativeTo(m_fileName.GetPath());
wxString filepath = tmp.GetFullPath(wxPATH_UNIX);
wxXmlNode *fileNode = XmlUtils::FindNodeByName(vdNode, "File", filepath);
if ( !fileNode ) {
return configs;
}
wxString excludeConfigs = XmlUtils::ReadString(fileNode, EXCLUDE_FROM_BUILD_FOR_CONFIG);
configs = ::wxStringTokenize(excludeConfigs, ";", wxTOKEN_STRTOK);
return configs;
}
void Project::SetExcludeConfigForFile(const wxString& filename, const wxString& virtualDirPath, const wxArrayString& configs)
{
wxXmlNode *vdNode = GetVirtualDir(virtualDirPath);
if ( !vdNode ) {
return ;
}
// locate our file
wxFileName tmp(filename);
tmp.MakeRelativeTo(m_fileName.GetPath());
wxString filepath = tmp.GetFullPath(wxPATH_UNIX);
wxXmlNode *fileNode = XmlUtils::FindNodeByName(vdNode, "File", filepath);
if ( !fileNode ) {
return ;
}
// Make sure the list is unique
wxStringSet_t unique_set;
unique_set.insert(configs.begin(), configs.end());
wxArrayString uniqueArr;
wxStringSet_t::iterator iter = unique_set.begin();
for( ; iter != unique_set.end(); ++iter ) {
uniqueArr.Add( *iter );
}
wxString excludeConfigs = ::wxJoin(uniqueArr, ';');
XmlUtils::UpdateProperty(fileNode, EXCLUDE_FROM_BUILD_FOR_CONFIG, excludeConfigs );
SaveXmlFile();
}
wxString Project::GetCompileLineForCXXFile(const wxString& filenamePlaceholder, bool cxxFile) const
{
// Return a compilation line for a CXX file
BuildMatrixPtr matrix = GetWorkspace()->GetBuildMatrix();
if(!matrix) {
return "";
}
wxString workspaceSelConf = matrix->GetSelectedConfigurationName();
wxString projectSelConf = matrix->GetProjectSelectedConf(workspaceSelConf, GetName());
BuildConfigPtr buildConf = GetWorkspace()->GetProjBuildConf(this->GetName(), projectSelConf);
if ( !buildConf || buildConf->IsCustomBuild() || !buildConf->IsCompilerRequired()) {
return "";
}
CompilerPtr compiler = buildConf->GetCompiler();
if ( !compiler ) {
return "";
}
// Build the command line
wxString commandLine;
wxString compilerExe = compiler->GetTool(cxxFile ? "CXX" : "CC");
commandLine << compilerExe << " -c " << filenamePlaceholder << " -o " << filenamePlaceholder << ".o ";
// Apply the environment
EnvSetter es(NULL, NULL, GetName());
// Clear the backticks cache
s_backticks.clear();
// Get the compile options
wxString projectCompileOptions = cxxFile ? buildConf->GetCompileOptions() : buildConf->GetCCompileOptions();
wxArrayString projectCompileOptionsArr = ::wxStringTokenize(projectCompileOptions, ";", wxTOKEN_STRTOK);
for(size_t i=0; i<projectCompileOptionsArr.GetCount(); ++i) {
wxString cmpOption (projectCompileOptionsArr.Item(i));
cmpOption.Trim().Trim(false);
// expand backticks, if the option is not a backtick the value remains
// unchanged
commandLine << " " << DoExpandBacktick(cmpOption) << " ";
}
// Add the macros
wxArrayString prepArr;
buildConf->GetPreprocessor( prepArr );
for ( size_t i=0; i<prepArr.GetCount(); ++i ) {
commandLine << "-D" << prepArr.Item(i) << " ";
}
// Add the include paths
wxString inclPathAsString = buildConf->GetIncludePath();
wxArrayString inclPathArr = ::wxStringTokenize(inclPathAsString, ";", wxTOKEN_STRTOK);
for(size_t i=0; i<inclPathArr.GetCount(); ++i) {
wxString incl_path = inclPathArr.Item(i);
incl_path.Trim().Trim(false);
if ( incl_path.IsEmpty() )
continue;
if ( incl_path.Contains(" ") ) {
incl_path.Prepend("\"").Append("\"");
}
commandLine << "-I" << incl_path << " ";
}
commandLine.Trim().Trim(false);
return commandLine;
}
wxString Project::DoExpandBacktick(const wxString& backtick) const
{
wxString tmp;
wxString cmpOption = backtick;
cmpOption.Trim().Trim(false);
// Expand backticks / $(shell ...) syntax supported by codelite
if(cmpOption.StartsWith(wxT("$(shell "), &tmp) || cmpOption.StartsWith(wxT("`"), &tmp)) {
cmpOption = tmp;
tmp.Clear();
if(cmpOption.EndsWith(wxT(")"), &tmp) || cmpOption.EndsWith(wxT("`"), &tmp)) {
cmpOption = tmp;
}
if(s_backticks.find(cmpOption) == s_backticks.end()) {
// Expand the backticks into their value
wxString expandedValue = ::wxShellExec(cmpOption, GetName());
s_backticks[cmpOption] = expandedValue;
cmpOption = expandedValue;
} else {
cmpOption = s_backticks.find(cmpOption)->second;
}
}
return cmpOption;
}
void Project::CreateCompileCommandsJSON(JSONElement& compile_commands)
{
FileNameVector_t files;
GetFiles(files, true);
wxString cFilePattern = GetCompileLineForCXXFile("$FileName", false);
wxString cxxFilePattern = GetCompileLineForCXXFile("$FileName", true);
wxString workingDirectory = m_fileName.GetPath();
for(size_t i=0; i<files.size(); ++i) {
const wxFileName &fn = files.at(i);
const wxString fullpath = fn.GetFullPath();
wxString pattern;
FileExtManager::FileType fileType = FileExtManager::GetType( fullpath );
if ( fileType == FileExtManager::TypeSourceC ) {
pattern = cFilePattern;
} else if ( fileType == FileExtManager::TypeSourceCpp ) {
pattern = cxxFilePattern;
} else {
continue;
}
wxString file_name = fullpath;
if ( file_name.Contains(" ") ) {
file_name.Prepend("\"").Append("\"");
}
pattern.Replace("$FileName", file_name);
JSONElement json = JSONElement::createObject();
json.addProperty("file", fullpath);
json.addProperty("directory", workingDirectory);
json.addProperty("command", pattern);
compile_commands.append( json );
}
}
BuildConfigPtr Project::GetBuildConfiguration(const wxString& configName)
{
BuildMatrixPtr matrix = GetWorkspace()->GetBuildMatrix();
if( !matrix ) {
return NULL;
}
wxString workspaceSelConf = matrix->GetSelectedConfigurationName();
wxString projectSelConf = configName.IsEmpty() ? matrix->GetProjectSelectedConf(workspaceSelConf, GetName()) : configName;
BuildConfigPtr buildConf = GetWorkspace()->GetProjBuildConf(this->GetName(), projectSelConf);
return buildConf;
}
void Project::AssociateToWorkspace(Workspace* workspace)
{
m_workspace = workspace;
}
Workspace* Project::GetWorkspace()
{
if ( !m_workspace ) {
return WorkspaceST::Get();
} else {
return m_workspace;
}
}
const Workspace* Project::GetWorkspace() const
{
if ( !m_workspace ) {
return WorkspaceST::Get();
} else {
return m_workspace;
}
}
void Project::GetCompilers(wxStringSet_t& compilers)
{
ProjectSettingsPtr pSettings = GetSettings();
CHECK_PTR_RET( pSettings );
BuildConfigPtr buildConf = GetBuildConfiguration();
if ( buildConf ) {
compilers.insert( buildConf->GetCompilerType() );
}
}
void Project::ReplaceCompilers(wxStringMap_t& compilers)
{
ProjectSettingsPtr pSettings = GetSettings();
CHECK_PTR_RET( pSettings );
ProjectSettingsCookie cookie;
BuildConfigPtr buildConf = pSettings->GetFirstBuildConfiguration( cookie );
while ( buildConf ) {
if ( compilers.count(buildConf->GetCompilerType()) ) {
buildConf->SetCompilerType( compilers.find(buildConf->GetCompilerType())->second );
}
buildConf = pSettings->GetNextBuildConfiguration( cookie );
}
// and update the settings (+ save the XML file)
SetSettings( pSettings );
}
void Project::DoUpdateProjectSettings()
{
m_settings.Reset( new ProjectSettings(XmlUtils::FindFirstByTagName(m_doc.GetRoot(), wxT("Settings"))) );
}
wxArrayString Project::GetPreProcessors(bool clearCache)
{
wxArrayString pps;
BuildConfigPtr buildConf = GetBuildConfiguration();
// for non custom projects, take the settings from the build configuration
if(buildConf && !buildConf->IsCustomBuild()) {
// Apply the environment
EnvSetter es(NULL, NULL, GetName());
if ( clearCache ) {
s_backticks.clear();
}
// Get the pre-processors and add them to the array
wxString projectPPS = buildConf->GetPreprocessor();
wxArrayString projectPPSArr = ::wxStringTokenize(projectPPS, wxT(";"), wxTOKEN_STRTOK);
for(size_t i=0; i<projectPPSArr.GetCount(); i++) {
projectPPSArr.Item(i).Trim();
if ( pps.Index(projectPPSArr.Item(i)) != wxNOT_FOUND )
continue;
pps.Add( projectPPSArr.Item(i) );
}
// get the compiler options and add them
wxString projectCompileOptions = buildConf->GetCompileOptions();
wxArrayString projectCompileOptionsArr = wxStringTokenize(projectCompileOptions, wxT(";"), wxTOKEN_STRTOK);
for(size_t i=0; i<projectCompileOptionsArr.GetCount(); i++) {
wxString cmpOption (projectCompileOptionsArr.Item(i));
cmpOption.Trim().Trim(false);
// expand backticks, if the option is not a backtick the value remains
// unchanged
wxArrayString pparr = DoBacktickToPreProcessors(cmpOption);
if ( !pparr.IsEmpty() ) {
pps.insert(pps.end(), pparr.begin(), pparr.end());
}
}
}
return pps;
}
wxArrayString Project::GetCXXCompilerOptions(bool clearCache, bool noDefines, bool noIncludePaths)
{
return DoGetCompilerOptions(true, clearCache, noDefines, noIncludePaths);
}
wxArrayString Project::GetCCompilerOptions(bool clearCache, bool noDefines, bool noIncludePaths)
{
return DoGetCompilerOptions(false, clearCache, noDefines, noIncludePaths);
}
wxArrayString Project::DoGetCompilerOptions(bool cxxOptions, bool clearCache, bool noDefines, bool noIncludePaths)
{
wxArrayString options;
BuildConfigPtr buildConf = GetBuildConfiguration();
// for non custom projects, take the settings from the build configuration
if(buildConf && !buildConf->IsCustomBuild()) {
// Apply the environment
EnvSetter es(NULL, NULL, GetName());
if ( clearCache ) {
s_backticks.clear();
}
// Get the switches from
wxString optionsStr = cxxOptions ? buildConf->GetCompileOptions() : buildConf->GetCCompileOptions();
wxArrayString optionsArr = wxStringTokenize(optionsStr, wxT(";"), wxTOKEN_STRTOK);
for(size_t i=0; i<optionsArr.GetCount(); ++i) {
wxString cmpOption( optionsArr.Item(i) );
cmpOption.Trim().Trim(false);
if ( cmpOption.IsEmpty() )
continue;
wxString expandedCmpOption = DoExpandBacktick( cmpOption );
if ( expandedCmpOption != cmpOption ) {
// this was indeed a backtick
CompilerCommandLineParser cclp(expandedCmpOption, GetFileName().GetPath());
const wxArrayString& opts = cclp.GetOtherOptions();
options.insert(options.end(), opts.begin(), opts.end());
} else {
options.Add( cmpOption );
}
}
if ( !noDefines ) {
wxArrayString macros = GetPreProcessors();
std::for_each(macros.begin(), macros.end(), wxArrayStringAppender(macros, "-D", true));
options.insert(options.end(), macros.begin(), macros.end());
}
if ( !noIncludePaths ) {
wxArrayString includes = GetIncludePaths();
std::for_each(includes.begin(), includes.end(), wxArrayStringAppender(includes, "-I", true));
options.insert(options.end(), includes.begin(), includes.end());
}
}
return options;
}
|