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 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915
|
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright : (C) 2008 by Eran Ifrah
// file name : builder_NMake.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 "fileextmanager.h"
#include <wx/app.h>
#include <wx/msgdlg.h>
#include "event_notifier.h"
#include "file_logger.h"
#include "editor_config.h"
#include <wx/stopwatch.h>
#include "environmentconfig.h"
#include "evnvarlist.h"
#include "environmentconfig.h"
#include "builder_NMake.h"
#include "configuration_mapping.h"
#include "dirsaver.h"
#include "wx/tokenzr.h"
#include "macros.h"
#include "build_settings_config.h"
#include "buildmanager.h"
#include "wx/sstream.h"
#include "globals.h"
#include "cl_command_event.h"
#include <algorithm>
#include "macromanager.h"
static bool OS_WINDOWS = wxGetOsVersion() & wxOS_WINDOWS ? true : false;
static wxString ReplaceBackslashes(const wxString& instr)
{
wxString tmpstr = instr;
tmpstr.Trim().Trim(false);
if(tmpstr.StartsWith("\"")) {
tmpstr = tmpstr.Mid(1);
wxString strBefore = tmpstr.BeforeFirst('"');
wxString strAfter = tmpstr.AfterFirst('"');
strBefore.Prepend("\"").Append("\""); // restore the " that we removed
strBefore.Replace("/", "\\");
strBefore << strAfter;
tmpstr.swap(strBefore);
} else {
wxString strBefore = tmpstr.BeforeFirst(' ');
wxString strAfter = tmpstr.AfterFirst(' ');
strAfter.Prepend(" ");
strBefore.Replace("/", "\\");
strBefore << strAfter;
tmpstr.swap(strBefore);
}
return tmpstr;
}
static wxString GetMakeDirCmd(BuildConfigPtr bldConf, const wxString& relPath = wxEmptyString)
{
wxString intermediateDirectory(bldConf->GetIntermediateDirectory());
wxString relativePath(relPath);
intermediateDirectory.Replace(wxT("\\"), wxT("/"));
intermediateDirectory.Trim().Trim(false);
if(intermediateDirectory.StartsWith(wxT("./")) && relativePath == wxT("./")) {
relativePath.Clear();
}
if(intermediateDirectory.StartsWith(wxT("./")) && relativePath.IsEmpty() == false) {
intermediateDirectory = intermediateDirectory.Mid(2);
}
wxString text;
text << wxT("@$(MakeDirCommand) \"") << relativePath << intermediateDirectory << wxT("\"");
return text;
}
BuilderNMake::BuilderNMake()
: Builder(wxT("NMakefile for MSVC toolset"))
, m_objectChunks(1)
{
}
BuilderNMake::BuilderNMake(const wxString& name, const wxString& buildTool, const wxString& buildToolOptions)
: Builder(name)
{
}
BuilderNMake::~BuilderNMake() {}
bool BuilderNMake::Export(const wxString& project, const wxString& confToBuild, const wxString& arguments,
bool isProjectOnly, bool force, wxString& errMsg)
{
if(project.IsEmpty()) {
return false;
}
ProjectPtr proj = clCxxWorkspaceST::Get()->FindProjectByName(project, errMsg);
if(!proj) {
errMsg << _("Cant open project '") << project << wxT("'");
return false;
}
// get the selected build configuration
wxString bld_conf_name(confToBuild);
if(confToBuild.IsEmpty()) {
BuildConfigPtr bldConf = clCxxWorkspaceST::Get()->GetProjBuildConf(project, confToBuild);
if(!bldConf) {
errMsg << _("Cant find build configuration for project '") << project << wxT("'");
return false;
}
bld_conf_name = bldConf->GetName();
}
BuildConfigPtr bldConf = clCxxWorkspaceST::Get()->GetProjBuildConf(project, bld_conf_name);
if(!bldConf) {
errMsg << _("Cant find build configuration for project '") << project << wxT("'");
return false;
}
if(!bldConf->GetCompiler()) {
errMsg << _("Cant find proper compiler for project '") << project << wxT("'");
return false;
}
wxArrayString depsArr = proj->GetDependencies(bld_conf_name);
CL_DEBUG("Generating Makefile...");
// Filter all disabled projects from the dependencies array
wxArrayString updatedDepsArr;
for(size_t i = 0; i < depsArr.GetCount(); ++i) {
wxString errmsg;
ProjectPtr dependProj = clCxxWorkspaceST::Get()->FindProjectByName(depsArr.Item(i), errmsg);
if(!dependProj) {
continue;
}
wxString projectSelConf = clCxxWorkspaceST::Get()->GetBuildMatrix()->GetProjectSelectedConf(
clCxxWorkspaceST::Get()->GetBuildMatrix()->GetSelectedConfigurationName(), dependProj->GetName());
BuildConfigPtr dependProjbldConf =
clCxxWorkspaceST::Get()->GetProjBuildConf(dependProj->GetName(), projectSelConf);
if(dependProjbldConf && dependProjbldConf->IsProjectEnabled()) {
updatedDepsArr.Add(depsArr.Item(i));
}
}
// Update the dependencies with the correct list
depsArr.swap(updatedDepsArr);
wxArrayString removeList;
if(!isProjectOnly) {
// this function assumes that the working directory is located at the workspace path
// make sure that all dependencies exists
for(size_t i = 0; i < depsArr.GetCount(); i++) {
ProjectPtr dependProj = clCxxWorkspaceST::Get()->FindProjectByName(depsArr.Item(i), errMsg);
// Missing dependencies project?
// this can happen if a project was removed from the workspace, but
// is still on the depdendencie list of this project
if(!dependProj) {
wxString msg;
msg << _("CodeLite can not find project '") << depsArr.Item(i) << _("' which is required\n");
msg << _("for building project '") << project
<< _("'.\nWould you like to remove it from the dependency list?");
if(wxMessageBox(msg, _("CodeLite"), wxYES_NO | wxICON_QUESTION) == wxYES) {
// remove the project from the dependecie list, and continue
removeList.Add(depsArr.Item(i));
}
}
}
bool settingsChanged(false);
// remove the unfound projects from the dependencies array
for(size_t i = 0; i < removeList.GetCount(); i++) {
int where = depsArr.Index(removeList.Item(i));
if(where != wxNOT_FOUND) {
depsArr.RemoveAt(where);
settingsChanged = true;
}
}
// update the project dependencies
bool modified = proj->IsModified();
// Update the dependencies only if needed
if(settingsChanged) proj->SetDependencies(depsArr, bld_conf_name);
// the set settings functions marks the project as 'modified' this causes
// an unneeded makefile generation if the settings was not really modified
if(!modified && !settingsChanged) {
proj->SetModified(false);
}
}
wxString fn;
fn << "Makefile";
wxString text;
wxFileName wspfile(clCxxWorkspaceST::Get()->GetWorkspaceFileName());
text << wxT("All:\n");
// iterate over the dependencies projects and generate makefile
wxString buildTool = GetBuildToolCommand(project, confToBuild, "", false);
buildTool = EnvironmentConfig::Instance()->ExpandVariables(buildTool, true);
// fix: replace all Windows like slashes to POSIX
// buildTool.Replace(wxT("\\"), wxT("/"));
// generate the makefile for the selected workspace configuration
BuildMatrixPtr matrix = clCxxWorkspaceST::Get()->GetBuildMatrix();
wxString workspaceSelConf = matrix->GetSelectedConfigurationName();
wxArrayString depsProjs;
if(!isProjectOnly) {
for(size_t i = 0; i < depsArr.GetCount(); ++i) {
bool isCustom(false);
ProjectPtr dependProj = clCxxWorkspaceST::Get()->FindProjectByName(depsArr.Item(i), errMsg);
if(!dependProj) {
continue;
}
wxString projectSelConf = matrix->GetProjectSelectedConf(workspaceSelConf, dependProj->GetName());
BuildConfigPtr dependProjbldConf =
clCxxWorkspaceST::Get()->GetProjBuildConf(dependProj->GetName(), projectSelConf);
if(dependProjbldConf && dependProjbldConf->IsCustomBuild()) {
isCustom = true;
}
// incase we manually specified the configuration to be built, set the project
// as modified, so on next attempt to build it, CodeLite will sync the configuration
if(confToBuild.IsEmpty() == false) {
dependProj->SetModified(true);
}
if(!dependProjbldConf->IsProjectEnabled()) {
// Ignore disabled projects
continue;
}
text << wxT("\t@echo \"") << wxGetTranslation(BUILD_PROJECT_PREFIX) << dependProj->GetName() << wxT(" - ")
<< projectSelConf << wxT(" ]----------\"\n");
// make the paths relative, if it's sensible to do so
wxFileName fn(dependProj->GetFileName());
MakeRelativeIfSensible(fn, wspfile.GetPath());
bool isPluginGeneratedMakefile =
SendBuildEvent(wxEVT_GET_IS_PLUGIN_MAKEFILE, depsArr.Item(i), projectSelConf);
// we handle custom builds and non-custom build separatly:
if(isPluginGeneratedMakefile) {
// this project makefile is generated by a plugin
// query the plugin about the build command
clBuildEvent e(wxEVT_GET_PROJECT_BUILD_CMD);
e.SetProjectName(depsArr.Item(i));
e.SetConfigurationName(projectSelConf);
e.SetProjectOnly(false);
EventNotifier::Get()->ProcessEvent(e);
text << wxT("\t") << e.GetCommand() << wxT("\n");
} else if(isCustom) {
CreateCustomPreBuildEvents(dependProjbldConf, text);
wxString customWd = dependProjbldConf->GetCustomBuildWorkingDir();
wxString build_cmd = dependProjbldConf->GetCustomBuildCmd();
wxString customWdCmd;
customWd = ExpandAllVariables(customWd, clCxxWorkspaceST::Get(), dependProj->GetName(),
dependProjbldConf->GetName(), wxEmptyString);
build_cmd = ExpandAllVariables(build_cmd, clCxxWorkspaceST::Get(), dependProj->GetName(),
dependProjbldConf->GetName(), wxEmptyString);
build_cmd.Trim().Trim(false);
if(build_cmd.empty()) {
build_cmd << wxT("@echo Project has no custom build command!");
}
// if a working directory is provided apply it, otherwise use the project
// path
customWd.Trim().Trim(false);
if(customWd.empty() == false) {
customWdCmd << wxT("@cd \"") << ExpandVariables(customWd, dependProj, NULL) << wxT("\" && ");
} else {
customWdCmd << GetCdCmd(wspfile, fn);
}
text << wxT("\t") << customWdCmd << build_cmd << wxT("\n");
CreateCustomPostBuildEvents(dependProjbldConf, text);
} else {
// generate the dependency project makefile
wxString dep_file = DoGetMarkerFileDir(wxT(""));
dep_file << wxFileName::GetPathSeparator() << dependProj->GetName();
depsProjs.Add(dep_file);
GenerateMakefile(dependProj, projectSelConf, confToBuild.IsEmpty() ? force : true, wxArrayString());
text << GetProjectMakeCommand(wspfile, fn, dependProj, projectSelConf);
}
}
}
// Generate makefile for the project itself
GenerateMakefile(proj, confToBuild, confToBuild.IsEmpty() ? force : true, depsProjs);
// incase we manually specified the configuration to be built, set the project
// as modified, so on next attempt to build it, CodeLite will sync the configuration
if(confToBuild.IsEmpty() == false) {
proj->SetModified(true);
}
wxString projectSelConf = matrix->GetProjectSelectedConf(workspaceSelConf, project);
if(isProjectOnly && confToBuild.IsEmpty() == false) {
// incase we use to generate a 'Project Only' makefile,
// we allow the caller to override the selected configuration with 'confToBuild' parameter
projectSelConf = confToBuild;
}
text << wxT("\t@echo \"") << wxGetTranslation(BUILD_PROJECT_PREFIX) << project << wxT(" - ") << projectSelConf
<< wxT(" ]----------\"\n");
// make the paths relative, if it's sensible to do so
wxFileName projectPath(proj->GetFileName());
MakeRelativeIfSensible(projectPath, wspfile.GetPath());
wxString pname(proj->GetName());
bool isPluginGeneratedMakefile = SendBuildEvent(wxEVT_GET_IS_PLUGIN_MAKEFILE, pname, projectSelConf);
if(isPluginGeneratedMakefile) {
wxString cmd;
clBuildEvent e(wxEVT_GET_PROJECT_BUILD_CMD);
e.SetProjectName(pname);
e.SetConfigurationName(projectSelConf);
e.SetProjectOnly(isProjectOnly);
EventNotifier::Get()->ProcessEvent(e);
cmd = e.GetCommand();
text << wxT("\t") << cmd << wxT("\n");
} else {
text << GetProjectMakeCommand(wspfile, projectPath, proj, projectSelConf);
}
// create the clean target
text << wxT("clean:\n");
if(!isProjectOnly) {
for(size_t i = 0; i < depsArr.GetCount(); i++) {
bool isCustom(false);
wxString projectSelConf = matrix->GetProjectSelectedConf(workspaceSelConf, depsArr.Item(i));
ProjectPtr dependProj = clCxxWorkspaceST::Get()->FindProjectByName(depsArr.Item(i), errMsg);
// Missing dependencies project? just skip it
if(!dependProj) {
continue;
}
text << wxT("\t@echo \"") << wxGetTranslation(CLEAN_PROJECT_PREFIX) << dependProj->GetName() << wxT(" - ")
<< projectSelConf << wxT(" ]----------\"\n");
// make the paths relative
wxFileName fn(dependProj->GetFileName());
MakeRelativeIfSensible(fn, wspfile.GetPath());
// if the dependencie project is project of type 'Custom Build' - do the custom build instead
// of the geenrated makefile
BuildConfigPtr dependProjbldConf =
clCxxWorkspaceST::Get()->GetProjBuildConf(dependProj->GetName(), projectSelConf);
if(dependProjbldConf && dependProjbldConf->IsCustomBuild()) {
isCustom = true;
}
// if we are using custom build command, invoke it instead of calling the generated makefile
wxString pname = dependProj->GetName();
bool isPluginGeneratedMakefile = SendBuildEvent(wxEVT_GET_IS_PLUGIN_MAKEFILE, pname, projectSelConf);
// we handle custom builds and non-custom build separately:
if(isPluginGeneratedMakefile) {
// this project makefile is generated by a plugin
// query the plugin about the build command
clBuildEvent e(wxEVT_GET_PROJECT_CLEAN_CMD);
e.SetProjectOnly(false);
e.SetProjectName(pname);
e.SetConfigurationName(projectSelConf);
EventNotifier::Get()->ProcessEvent(e);
text << wxT("\t") << e.GetCommand() << wxT("\n");
} else if(!isCustom) {
text << wxT("\t") << GetCdCmd(wspfile, fn) << buildTool << wxT(" \"") << dependProj->GetName()
<< wxT(".mk\" clean\n");
} else {
wxString customWd = dependProjbldConf->GetCustomBuildWorkingDir();
wxString clean_cmd = dependProjbldConf->GetCustomCleanCmd();
customWd = ExpandAllVariables(customWd, clCxxWorkspaceST::Get(), dependProj->GetName(),
dependProjbldConf->GetName(), wxEmptyString);
clean_cmd = ExpandAllVariables(clean_cmd, clCxxWorkspaceST::Get(), dependProj->GetName(),
dependProjbldConf->GetName(), wxEmptyString);
wxString customWdCmd;
clean_cmd.Trim().Trim(false);
if(clean_cmd.empty()) {
clean_cmd << wxT("@echo Project has no custom clean command!");
}
// if a working directory is provided apply it, otherwise use the project
// path
customWd.Trim().Trim(false);
if(customWd.empty() == false) {
customWdCmd << wxT("@cd \"") << ExpandVariables(customWd, dependProj, NULL) << wxT("\" && ");
} else {
customWdCmd << GetCdCmd(wspfile, fn);
}
text << wxT("\t") << customWdCmd << clean_cmd << wxT("\n");
}
}
}
// generate makefile for the project itself
projectSelConf = matrix->GetProjectSelectedConf(workspaceSelConf, project);
if(isProjectOnly && confToBuild.IsEmpty() == false) {
// incase we use to generate a 'Project Only' makefile,
// we allow the caller to override the selected configuration with 'confToBuild' parameter
projectSelConf = confToBuild;
}
text << wxT("\t@echo \"") << wxGetTranslation(CLEAN_PROJECT_PREFIX) << project << wxT(" - ") << projectSelConf
<< wxT(" ]----------\"\n");
if(isPluginGeneratedMakefile) {
wxString cmd;
clBuildEvent e(wxEVT_GET_PROJECT_CLEAN_CMD);
e.SetProjectName(pname);
e.SetConfigurationName(projectSelConf);
e.SetProjectOnly(isProjectOnly);
EventNotifier::Get()->ProcessEvent(e);
cmd = e.GetCommand();
text << wxT("\t") << cmd << wxT("\n");
} else {
text << wxT("\t") << GetCdCmd(wspfile, projectPath) << buildTool << wxT(" \"") << proj->GetName()
<< wxT(".mk\" clean\n");
}
text << wxT(".PHONY: clean All\n\n");
// dump the content to file
wxFileOutputStream output(fn);
wxStringInputStream content(text);
output << content;
CL_DEBUG("Generating Makefile...is completed");
return true;
}
void BuilderNMake::GenerateMakefile(ProjectPtr proj, const wxString& confToBuild, bool force,
const wxArrayString& depsProj)
{
wxString pname(proj->GetName());
wxString tmpConfigName(confToBuild.c_str());
if(confToBuild.IsEmpty()) {
BuildMatrixPtr matrix = clCxxWorkspaceST::Get()->GetBuildMatrix();
tmpConfigName = matrix->GetProjectSelectedConf(matrix->GetSelectedConfigurationName(), proj->GetName());
}
bool isPluginGeneratedMakefile = SendBuildEvent(wxEVT_GET_IS_PLUGIN_MAKEFILE, pname, tmpConfigName);
// we handle custom builds and non-custom build separatly:
if(isPluginGeneratedMakefile) {
if(force) {
// Generate the makefile
SendBuildEvent(wxEVT_PLUGIN_EXPORT_MAKEFILE, pname, tmpConfigName);
}
return;
}
ProjectSettingsPtr settings = proj->GetSettings();
if(!settings) {
return;
}
// get the selected build configuration for this project
BuildConfigPtr bldConf = clCxxWorkspaceST::Get()->GetProjBuildConf(proj->GetName(), confToBuild);
if(!bldConf) {
return;
}
wxString path = proj->GetFileName().GetPath();
DirSaver ds;
// change directory to the project base dir
::wxSetWorkingDirectory(path);
// create new makefile file
wxString fn(path);
fn << PATH_SEP << proj->GetName() << wxT(".mk");
// skip the next test if the makefile does not exist
if(wxFileName::FileExists(fn)) {
if(!force) {
if(proj->IsModified() == false) {
return;
}
}
}
// Load the current project files
m_allFiles.clear();
proj->GetFilesAsVector(m_allFiles);
// generate the selected configuration for this project
// wxTextOutputStream text(output);
wxString text;
text << wxT("##") << wxT("\n");
text << wxT("## Auto Generated makefile by CodeLite IDE") << wxT("\n");
text << wxT("## any manual changes will be erased ") << wxT("\n");
text << wxT("##") << wxT("\n");
// Create the makefile variables
CreateConfigsVariables(proj, bldConf, text);
//----------------------------------------------------------
// copy environment variables to the makefile
// We put them after the 'hard-coeded' ones
// so user will be able to override any of the default
// variables by defining its own
//----------------------------------------------------------
EvnVarList vars;
EnvironmentConfig::Instance()->ReadObject(wxT("Variables"), &vars);
EnvMap varMap = vars.GetVariables(wxT(""), true, proj->GetName(), bldConf->GetName());
text << wxT("##") << wxT("\n");
text << wxT("## User defined environment variables") << wxT("\n");
text << wxT("##") << wxT("\n");
for(size_t i = 0; i < varMap.GetCount(); i++) {
wxString name, value;
varMap.Get(i, name, value);
text << name << wxT("=") << value << wxT("") << wxT("\n");
}
CreateListMacros(proj, confToBuild, text); // list of srcs and list of objects
//-----------------------------------------------------------
// create the build targets
//-----------------------------------------------------------
text << wxT("##\n");
text << wxT("## Main Build Targets \n");
text << wxT("##\n");
// incase project is type exe or dll, force link
// this is to workaround bug in the generated makefiles
// which causes the makefile to report 'nothing to be done'
// even when a dependency was modified
wxString targetName(bldConf->GetIntermediateDirectory());
CreateLinkTargets(proj->GetSettings()->GetProjectType(bldConf->GetName()), bldConf, text, targetName,
proj->GetName(), depsProj);
CreatePostBuildEvents(proj, bldConf, text);
CreateMakeDirsTarget(proj, bldConf, targetName, text);
CreatePreBuildEvents(proj, bldConf, text);
CreatePreCompiledHeaderTarget(bldConf, text);
//-----------------------------------------------------------
// Create a list of targets that should be built according to
// projects' file list
//-----------------------------------------------------------
CreateFileTargets(proj, confToBuild, text);
CreateCleanTargets(proj, confToBuild, text);
// dump the content to a file
wxFFile output;
output.Open(fn, wxT("w+"));
if(output.IsOpened()) {
output.Write(text);
output.Close();
}
// mark the project as non-modified one
proj->SetModified(false);
}
void BuilderNMake::CreateMakeDirsTarget(ProjectPtr proj, BuildConfigPtr bldConf, const wxString& targetName,
wxString& text)
{
text << "\n";
text << "MakeIntermediateDirs:\n";
text << "\t" << GetMakeDirCmd(bldConf) << "\n\n";
text << wxT("\n");
text << targetName << wxT(":\n");
text << wxT("\t") << wxT("@echo Creating Intermediate Directory\n");
text << wxT("\t") << GetMakeDirCmd(bldConf) << wxT("\n");
text << wxT("\t") << wxT("@echo Intermediate directories created");
}
void BuilderNMake::CreateSrcList(ProjectPtr proj, const wxString& confToBuild, wxString& text)
{
std::vector<wxFileName> files;
const Project::FilesMap_t& all_files = proj->GetFiles();
// Remove excluded files
std::for_each(all_files.begin(), all_files.end(), [&](const Project::FilesMap_t::value_type& vt) {
clProjectFile::Ptr_t f = vt.second;
if(!f->IsExcludeFromConfiguration(confToBuild)) {
files.push_back(wxFileName(f->GetFilenameRelpath()));
}
});
text << wxT("Srcs=");
BuildConfigPtr bldConf = clCxxWorkspaceST::Get()->GetProjBuildConf(proj->GetName(), confToBuild);
wxString cmpType = bldConf->GetCompilerType();
wxString relPath;
// get the compiler settings
CompilerPtr cmp = BuildSettingsConfigST::Get()->GetCompiler(cmpType);
int counter = 1;
Compiler::CmpFileTypeInfo ft;
for(size_t i = 0; i < files.size(); i++) {
// is this a valid file?
if(!cmp->GetCmpFileType(files[i].GetExt(), ft)) continue;
if(ft.kind == Compiler::CmpFileKindResource) {
#ifndef __WXMSW__
// Resource compiler "windres" is not supported under
// *nix OS
continue;
#endif
}
relPath = files.at(i).GetPath(true, wxPATH_UNIX);
relPath.Trim().Trim(false);
// Compiler::CmpFileKindSource , Compiler::CmpFileKindResource file
text << relPath << files[i].GetFullName() << wxT(" ");
if(counter % 10 == 0) {
text << wxT("\\\n\t");
}
counter++;
}
text << wxT("\n\n");
}
void BuilderNMake::CreateObjectList(ProjectPtr proj, const wxString& confToBuild, wxString& text)
{
m_objectChunks = 1;
std::vector<wxFileName> files;
std::for_each(m_allFiles.begin(), m_allFiles.end(), [&](clProjectFile::Ptr_t file) {
// Include only files that don't have the 'exclude from build' flag set
if(!file->IsExcludeFromConfiguration(confToBuild)) {
files.push_back(wxFileName(file->GetFilename()));
}
});
BuildConfigPtr bldConf = clCxxWorkspaceST::Get()->GetProjBuildConf(proj->GetName(), confToBuild);
wxString cmpType = bldConf->GetCompilerType();
// get the compiler settings
CompilerPtr cmp = BuildSettingsConfigST::Get()->GetCompiler(cmpType);
int counter = 1;
Compiler::CmpFileTypeInfo ft;
wxString projectPath = proj->GetFileName().GetPath();
wxString objectsList;
size_t objCounter = 0;
int numOfObjectsInCurrentChunk = 0;
wxString curChunk;
// We break the list of files into a seriese of objects variables
// each variable contains up to 100 files.
// This is needed because on MSW, the ECHO command can not handle over 8K bytes
// and codelite uses the ECHO command to create a file with all the objects
// so we can pass it to the liker
for(size_t i = 0; i < files.size(); ++i) {
// Did we collect 100 objects yet?
if(i && ((i % 100) == 0)) {
// only if this chunk contains objects (even one), add it to the makefile
// otherwise, clear it and continue collecting
if(numOfObjectsInCurrentChunk) {
curChunk.Prepend(wxString() << "Objects" << objCounter << "=");
curChunk << "\n\n";
text << curChunk;
objCounter++;
}
// start a new chunk
curChunk.clear();
numOfObjectsInCurrentChunk = 0;
}
// is this a valid file?
if(!cmp->GetCmpFileType(files[i].GetExt(), ft)) continue;
if(ft.kind == Compiler::CmpFileKindResource && !OS_WINDOWS) {
continue;
}
wxString objPrefix = DoGetTargetPrefix(files.at(i), projectPath, cmp);
curChunk << wxT("$(IntermediateDirectory)\\") << objPrefix << files[i].GetFullName() << wxT("$(ObjectSuffix) ");
// for readability, break every 10 objects and start a new line
if(counter % 10 == 0) {
curChunk << wxT("\\\n\t");
}
counter++;
numOfObjectsInCurrentChunk++;
}
text << wxT("##\n");
text << wxT("## Object Targets Lists \n");
text << wxT("##\n");
// Add any leftovers...
if(numOfObjectsInCurrentChunk) {
curChunk.Prepend(wxString() << "Objects" << objCounter << "=");
curChunk << "\n\n";
text << curChunk;
objCounter++;
}
text << "\n\nObjects=";
for(size_t i = 0; i < objCounter; ++i) text << "$(Objects" << i << ") ";
text << wxT("\n\n");
m_objectChunks = objCounter;
}
void BuilderNMake::CreateFileTargets(ProjectPtr proj, const wxString& confToBuild, wxString& text)
{
// get the project specific build configuration for the workspace active
// configuration
BuildConfigPtr bldConf = clCxxWorkspaceST::Get()->GetProjBuildConf(proj->GetName(), confToBuild);
wxString cmpType = bldConf->GetCompilerType();
// get the compiler settings
CompilerPtr cmp = BuildSettingsConfigST::Get()->GetCompiler(cmpType);
bool generateDependenciesFiles = cmp->GetGenerateDependeciesFile() && !cmp->GetDependSuffix().IsEmpty();
bool supportPreprocessOnlyFiles =
!cmp->GetSwitch(wxT("PreprocessOnly")).IsEmpty() && !cmp->GetPreprocessSuffix().IsEmpty();
std::vector<wxFileName> abs_files, rel_paths;
abs_files.reserve(m_allFiles.size());
rel_paths.reserve(m_allFiles.size());
clProjectFile::Vec_t::const_iterator iterFile = m_allFiles.begin();
for(; iterFile != m_allFiles.end(); ++iterFile) {
// Include only files that don't have the 'exclude from build' flag set
if(!(*iterFile)->IsExcludeFromConfiguration(confToBuild)) {
abs_files.push_back(wxFileName((*iterFile)->GetFilename()));
rel_paths.push_back(wxFileName((*iterFile)->GetFilenameRelpath()));
}
}
text << wxT("\n\n");
// create rule per object
text << wxT("##\n");
text << wxT("## Objects\n");
text << wxT("##\n");
Compiler::CmpFileTypeInfo ft;
// Collect all the sub-directories that we generate files for
wxArrayString subDirs;
wxString cwd = proj->GetFileName().GetPath();
for(size_t i = 0; i < abs_files.size(); i++) {
// is this file interests the compiler?
if(cmp->GetCmpFileType(abs_files.at(i).GetExt().Lower(), ft)) {
wxString absFileName;
wxFileName fn(abs_files.at(i));
wxString filenameOnly = fn.GetName();
wxString fullpathOnly = fn.GetFullPath();
wxString fullnameOnly = fn.GetFullName();
wxString compilationLine = ft.compilation_line;
// use UNIX style slashes
absFileName = abs_files[i].GetFullPath();
// absFileName.Replace(wxT("\\"), wxT("/"));
wxString relPath;
relPath = rel_paths.at(i).GetPath(true, wxPATH_UNIX);
relPath.Trim().Trim(false);
wxString objPrefix = DoGetTargetPrefix(abs_files.at(i), cwd, cmp);
compilationLine.Replace(wxT("$(FileName)"), filenameOnly);
compilationLine.Replace(wxT("$(FileFullName)"), fullnameOnly);
compilationLine.Replace(wxT("$(FileFullPath)"), fullpathOnly);
compilationLine.Replace(wxT("$(FilePath)"), relPath);
// The object name is handled differently when using resource files
compilationLine.Replace(wxT("$(ObjectName)"), objPrefix + fullnameOnly);
// compilationLine.Replace(wxT("\\"), wxT("/"));
if(ft.kind == Compiler::CmpFileKindSource) {
wxString objectName;
wxString dependFile;
wxString preprocessedFile;
bool isCFile = FileExtManager::GetType(rel_paths.at(i).GetFullName()) == FileExtManager::TypeSourceC;
objectName << wxT("$(IntermediateDirectory)\\") << objPrefix << fullnameOnly << wxT("$(ObjectSuffix)");
if(generateDependenciesFiles) {
dependFile << wxT("$(IntermediateDirectory)\\") << objPrefix << fullnameOnly
<< wxT("$(DependSuffix)");
}
if(supportPreprocessOnlyFiles) {
preprocessedFile << wxT("$(IntermediateDirectory)\\") << objPrefix << fullnameOnly
<< wxT("$(PreprocessSuffix)");
}
if(!isCFile) {
// Add the PCH include line
compilationLine.Replace(wxT("$(CXX)"), wxT("$(CXX) $(IncludePCH)"));
}
// set the file rule
text << objectName << wxT(": ") << rel_paths.at(i).GetFullPath(wxPATH_WIN) << wxT(" ") << dependFile
<< wxT("\n");
text << wxT("\t") << compilationLine << wxT("\n");
wxString cmpOptions(wxT("$(CXXFLAGS) $(IncludePCH)"));
if(isCFile) {
cmpOptions = wxT("$(CFLAGS)");
}
// set the source file we want to compile
wxString source_file_to_compile = rel_paths.at(i).GetFullPath(wxPATH_WIN);
wxString compilerMacro = DoGetCompilerMacro(rel_paths.at(i).GetFullPath(wxPATH_WIN));
if(generateDependenciesFiles) {
text << dependFile << wxT(": ") << rel_paths.at(i).GetFullPath(wxPATH_UNIX) << wxT("\n");
text << wxT("\t") << wxT("@") << compilerMacro << wxT(" ") << cmpOptions
<< wxT(" $(IncludePath) -MG -MP -MT") << objectName << wxT(" -MF") << dependFile
<< wxT(" -MM \"") << source_file_to_compile << wxT("\"\n\n");
}
if(supportPreprocessOnlyFiles) {
text << preprocessedFile << wxT(": ") << rel_paths.at(i).GetFullPath(wxPATH_UNIX) << wxT("\n");
text << wxT("\t") << compilerMacro << wxT(" ") << cmpOptions
<< wxT(" $(IncludePath) $(PreprocessOnlySwitch) $(OutputSwitch) ") << preprocessedFile
<< wxT(" \"") << source_file_to_compile << wxT("\"\n\n");
}
} else if(ft.kind == Compiler::CmpFileKindResource && OS_WINDOWS) {
// we construct an object name which also includes the full name of the reousrce file and appends a .o
// to the name (to be more
// precised, $(ObjectSuffix))
wxString objectName;
objectName << wxT("$(IntermediateDirectory)\\") << objPrefix << fullnameOnly << wxT("$(ObjectSuffix)");
text << objectName << wxT(": ") << rel_paths.at(i).GetFullPath(wxPATH_WIN) << wxT("\n");
text << wxT("\t") << compilationLine << wxT("\n");
}
}
}
if(generateDependenciesFiles) {
text << wxT("\n");
text << wxT("-include ") << wxT("$(IntermediateDirectory)\\*$(DependSuffix)\n");
}
}
static wxString GetIntermediateFolder(BuildConfigPtr bldConf)
{
wxString IntermediateDirectory = bldConf->GetIntermediateDirectory();
if(IntermediateDirectory == "/" || IntermediateDirectory.IsEmpty()) {
return wxEmptyString;
}
// not empty and does not equal to "/"
if(!IntermediateDirectory.EndsWith("/")) {
IntermediateDirectory.Append('/');
}
return IntermediateDirectory;
}
void BuilderNMake::CreateCleanTargets(ProjectPtr proj, const wxString& confToBuild, wxString& text)
{
// get the project specific build configuration for the workspace active
// configuration
BuildConfigPtr bldConf = clCxxWorkspaceST::Get()->GetProjBuildConf(proj->GetName(), confToBuild);
wxString cmpType = bldConf->GetCompilerType();
// get the compiler settings
CompilerPtr cmp = BuildSettingsConfigST::Get()->GetCompiler(cmpType);
// Can we use asterisk in the clean command?
wxString imd = GetIntermediateFolder(bldConf);
// add clean target
text << wxT("##\n");
text << wxT("## Clean\n");
text << wxT("##\n");
text << wxT("clean:\n");
wxString cwd = proj->GetFileName().GetPath();
if(!imd.IsEmpty()) {
// Remove the entire build folder
text << wxT("\t") << wxT("@if exist $(IntermediateDirectory) rmdir /S /Q $(IntermediateDirectory)") << "\n";
// Remove the pre-compiled header
wxString pchFile = bldConf->GetPrecompiledHeader();
pchFile.Trim().Trim(false);
if(pchFile.IsEmpty() == false) {
text << wxT("\t") << wxT("del /Q ") << pchFile << wxT(".gch") << wxT("\n");
}
} else if(OS_WINDOWS) {
text << wxT("\t") << wxT("@del /Q ") << imd << "*$(ObjectSuffix)" << wxT("\n");
text << wxT("\t") << wxT("@del /Q ") << imd << "*$(DependSuffix)" << wxT("\n");
// delete the output file as well
wxString exeExt(wxEmptyString);
if(proj->GetSettings()->GetProjectType(bldConf->GetName()) == PROJECT_TYPE_EXECUTABLE) {
// under windows, g++ automatically adds the .exe extension to executable
// make sure we delete it as well
exeExt = wxT(".exe");
}
text << wxT("\t") << wxT("@del /Q ") << wxT("$(OutputFile)") << wxT("\n");
text << wxT("\t") << wxT("@del /Q ") << wxT("$(OutputFile)") << exeExt << wxT("\n");
text << wxT("\t") << wxT("@del /Q ") << DoGetMarkerFileDir(proj->GetName(), proj->GetFileName().GetPath())
<< wxT("\n");
// Remove the pre-compiled header
wxString pchFile = bldConf->GetPrecompiledHeader();
pchFile.Trim().Trim(false);
if(pchFile.IsEmpty() == false) {
text << wxT("\t") << wxT("@del /Q ") << pchFile << wxT(".gch") << wxT("\n");
}
}
text << wxT("\n\n");
}
void BuilderNMake::CreateListMacros(ProjectPtr proj, const wxString& confToBuild, wxString& text)
{
// create a list of objects
CreateObjectList(proj, confToBuild, text);
}
void BuilderNMake::CreateLinkTargets(const wxString& type, BuildConfigPtr bldConf, wxString& text, wxString& targetName,
const wxString& projName, const wxArrayString& depsProj)
{
wxString extraDeps;
wxString depsRules;
wxString errMsg;
ProjectPtr proj = clCxxWorkspaceST::Get()->FindProjectByName(projName, errMsg);
for(size_t i = 0; i < depsProj.GetCount(); i++) {
wxFileName fn(depsProj.Item(i));
fn.MakeRelativeTo(proj->GetProjectPath());
extraDeps << "\"" << fn.GetFullPath() << wxT("\" ");
depsRules << "\"" << fn.GetFullPath() << wxT("\":\n");
// Make sure the dependecy directory exists
depsRules << "\t@$(MakeDirCommand) \"" << fn.GetPath() << "\"\n";
depsRules << wxT("\t@echo stam > ") << wxT("\"") << fn.GetFullPath() << wxT("\"\n");
depsRules << wxT("\n\n");
}
if(type == PROJECT_TYPE_EXECUTABLE || type == PROJECT_TYPE_DYNAMIC_LIBRARY) {
text << wxT("all: ");
text << wxT("$(OutputFile)\n\n");
text << wxT("$(OutputFile): $(IntermediateDirectory)\\.d ");
if(extraDeps.IsEmpty() == false) text << extraDeps;
text << wxT("$(Objects) \n");
targetName = wxT("$(IntermediateDirectory)\\.d");
} else {
text << wxT("all: $(IntermediateDirectory) ");
text << wxT("$(OutputFile)\n\n");
text << wxT("$(OutputFile): $(Objects)\n");
}
if(bldConf->IsLinkerRequired()) {
CreateTargets(type, bldConf, text, projName);
if(type == PROJECT_TYPE_EXECUTABLE || type == PROJECT_TYPE_DYNAMIC_LIBRARY) {
if(depsRules.IsEmpty() == false) {
text << wxT("\n") << depsRules << wxT("\n");
}
}
}
// incase project is type exe or dll, force link
// this is to workaround bug in the generated makefiles
// which causes the makefile to report 'nothing to be done'
// even when a dependency was modified
text << wxT(".PHONY: clean PrePreBuild PreBuild MakeIntermediateDirs all PostBuild \n");
}
void BuilderNMake::CreateTargets(const wxString& type, BuildConfigPtr bldConf, wxString& text, const wxString& projName)
{
bool markRebuilt(true);
text << wxT("\t@$(MakeDirCommand) $(@D)\n");
text << wxT("\t@echo \"\" > $(IntermediateDirectory)\\.d\n");
CompilerPtr cmp = bldConf->GetCompiler();
// this is a special target that creates a file with the content of the
// $(Objects) variable (to be used with the @<file-name> option of the LD
for(size_t i = 0; i < m_objectChunks; ++i) {
wxString oper = ">>";
if(i == 0) oper = " >";
text << "\t@echo $(Objects" << i << ") " << oper << " $(ObjectsFileList)\n";
}
if(type == PROJECT_TYPE_STATIC_LIBRARY) {
// create a static library
// In any case add the 'objects_file' target here
text << wxT("\t") << wxT("$(AR) $(ArchiveOutputSwitch)$(OutputFile)");
if(cmp && cmp->GetReadObjectFilesFromList()) {
text << wxT(" @$(ObjectsFileList) $(ArLibs)\n");
} else {
text << wxT(" $(Objects) $(ArLibs)\n");
}
} else if(type == PROJECT_TYPE_DYNAMIC_LIBRARY) {
// create a shared library
text << wxT("\t") << wxT("$(SharedObjectLinkerName) $(OutputSwitch)$(OutputFile)");
if(cmp && cmp->GetReadObjectFilesFromList()) {
text << wxT(" @$(ObjectsFileList) ");
} else {
text << wxT(" $(Objects) ");
}
text << wxT("$(LibPath) $(Libs) $(LinkOptions)\n");
} else if(type == PROJECT_TYPE_EXECUTABLE) {
// create an executable
text << wxT("\t") << wxT("$(LinkerName) $(OutputSwitch)$(OutputFile)");
if(cmp && cmp->GetReadObjectFilesFromList()) {
text << wxT(" @$(ObjectsFileList) ");
} else {
text << wxT(" $(Objects) ");
}
text << wxT("$(LibPath) $(Libs) $(LinkOptions)\n");
markRebuilt = false;
}
// If a link occurred, mark this project as "rebuilt" so the parent project will
// know that a re-link is required
if(bldConf->IsLinkerRequired() && markRebuilt) {
text << wxT("\t@$(MakeDirCommand) \"") << DoGetMarkerFileDir(wxEmptyString) << wxT("\"\n");
text << wxT("\t@echo rebuilt > ") << DoGetMarkerFileDir(projName) << wxT("\n");
}
}
void BuilderNMake::CreatePostBuildEvents(ProjectPtr proj, BuildConfigPtr bldConf, wxString& text)
{
if(!HasPostbuildCommands(bldConf)) return;
// generate postbuild commands
BuildCommandList cmds;
bldConf->GetPostBuildCommands(cmds);
// Loop over the commands and replace any macros
std::for_each(cmds.begin(), cmds.end(), [&](BuildCommand& cmd) {
cmd.SetCommand(
MacroManager::Instance()->Expand(cmd.GetCommand(), clGetManager(), proj->GetName(), bldConf->GetName()));
});
text << wxT("\n");
text << wxT("PostBuild:\n");
text << wxT("\t@echo Executing Post Build commands ...\n");
BuildCommandList::const_iterator iter = cmds.begin();
for(; iter != cmds.end(); iter++) {
if(iter->GetEnabled()) {
// HACK:
// If the command is 'copy' under Windows, make sure that
// we set all slashes to backward slashes
wxString command = iter->GetCommand();
command.Trim().Trim(false);
if(OS_WINDOWS && command.StartsWith(wxT("copy"))) {
command.Replace(wxT("/"), wxT("\\"));
}
if(OS_WINDOWS && command.EndsWith(wxT("\\"))) {
command.RemoveLast();
}
text << wxT("\t") << iter->GetCommand() << wxT("\n");
}
}
text << wxT("\t@echo Done\n");
}
bool BuilderNMake::HasPrebuildCommands(BuildConfigPtr bldConf) const
{
BuildCommandList cmds;
bldConf->GetPreBuildCommands(cmds);
BuildCommandList::const_iterator iter = cmds.begin();
for(; iter != cmds.end(); iter++) {
if(iter->GetEnabled()) {
return true;
}
}
return false;
}
void BuilderNMake::CreatePreBuildEvents(ProjectPtr proj, BuildConfigPtr bldConf, wxString& text)
{
BuildCommandList cmds;
BuildCommandList::iterator iter;
wxString name = bldConf->GetName();
name = NormalizeConfigName(name);
// add PrePreBuild
wxString preprebuild = bldConf->GetPreBuildCustom();
preprebuild.Trim().Trim(false);
if(preprebuild.IsEmpty() == false) {
text << wxT("PrePreBuild: ");
text << bldConf->GetPreBuildCustom() << wxT("\n");
}
text << wxT("\n");
bldConf->GetPreBuildCommands(cmds);
// Loop over the commands and replace any macros
std::for_each(cmds.begin(), cmds.end(), [&](BuildCommand& cmd) {
cmd.SetCommand(
MacroManager::Instance()->Expand(cmd.GetCommand(), clGetManager(), proj->GetName(), bldConf->GetName()));
});
bool first(true);
text << wxT("PreBuild:\n");
if(!cmds.empty()) {
iter = cmds.begin();
for(; iter != cmds.end(); iter++) {
if(iter->GetEnabled()) {
if(first) {
text << wxT("\t@echo Executing Pre Build commands ...\n");
first = false;
}
text << wxT("\t") << iter->GetCommand() << wxT("\n");
}
}
if(!first) {
text << wxT("\t@echo Done\n");
}
}
}
void BuilderNMake::CreateConfigsVariables(ProjectPtr proj, BuildConfigPtr bldConf, wxString& text)
{
wxString name = bldConf->GetName();
name = NormalizeConfigName(name);
wxString cmpType = bldConf->GetCompilerType();
CompilerPtr cmp = BuildSettingsConfigST::Get()->GetCompiler(cmpType);
wxString objectsFileName(proj->GetFileName().GetPath());
objectsFileName << PATH_SEP << proj->GetName() << wxT(".txt");
text << wxT("## ") << name << wxT("\n");
wxString outputFile = bldConf->GetOutputFileName();
if(OS_WINDOWS && (bldConf->GetProjectType() == PROJECT_TYPE_EXECUTABLE || bldConf->GetProjectType().IsEmpty())) {
outputFile.Trim().Trim(false);
outputFile.Replace(wxT("/"), wxT("\\"));
}
// Expand the build macros into the generated makefile
wxString projectName = proj->GetName();
text << wxT("ProjectName =") << projectName << wxT("\n");
text << wxT("ConfigurationName =") << name << wxT("\n");
text << wxT("WorkspacePath = \"") << clCxxWorkspaceST::Get()->GetWorkspaceFileName().GetPath()
<< wxT("\"\n");
text << wxT("ProjectPath = \"") << proj->GetFileName().GetPath() << wxT("\"\n");
text << wxT("IntermediateDirectory =") << bldConf->GetIntermediateDirectory() << wxT("\n");
text << wxT("OutDir = $(IntermediateDirectory)\n");
text << wxT("CurrentFileName =\n"); // TODO:: Need implementation
text << wxT("CurrentFilePath =\n"); // TODO:: Need implementation
text << wxT("CurrentFileFullPath =\n"); // TODO:: Need implementation
text << wxT("User =") << wxGetUserName() << wxT("\n");
text << wxT("Date =") << wxDateTime::Now().FormatDate() << wxT("\n");
text << wxT("CodeLitePath =\"") << clCxxWorkspaceST::Get()->GetStartupDir() << wxT("\"\n");
// replace all occurence of forward slash with double backslash
wxString linkerStr(cmp->GetTool(wxT("LinkerName")));
if(OS_WINDOWS) {
linkerStr = ReplaceBackslashes(linkerStr);
}
text << wxT("LinkerName =") << linkerStr << wxT("\n");
wxString shObjlinkerStr(cmp->GetTool(wxT("SharedObjectLinkerName")));
if(OS_WINDOWS) {
shObjlinkerStr = ReplaceBackslashes(shObjlinkerStr);
}
text << wxT("SharedObjectLinkerName =") << shObjlinkerStr << wxT("\n");
text << wxT("ObjectSuffix =") << cmp->GetObjectSuffix() << wxT("\n");
text << wxT("DependSuffix =") << cmp->GetDependSuffix() << wxT("\n");
text << wxT("PreprocessSuffix =") << cmp->GetPreprocessSuffix() << wxT("\n");
text << wxT("DebugSwitch =") << cmp->GetSwitch(wxT("Debug")) << wxT("\n");
text << wxT("IncludeSwitch =") << cmp->GetSwitch(wxT("Include")) << wxT("\n");
text << wxT("LibrarySwitch =") << cmp->GetSwitch(wxT("Library")) << wxT("\n");
text << wxT("OutputSwitch =") << cmp->GetSwitch(wxT("Output")) << wxT("\n");
text << wxT("LibraryPathSwitch =") << cmp->GetSwitch(wxT("LibraryPath")) << wxT("\n");
text << wxT("PreprocessorSwitch =") << cmp->GetSwitch(wxT("Preprocessor")) << wxT("\n");
text << wxT("SourceSwitch =") << cmp->GetSwitch(wxT("Source")) << wxT("\n");
text << wxT("OutputFile =") << outputFile << wxT("\n");
text << wxT("Preprocessors =") << ParsePreprocessor(bldConf->GetPreprocessor()) << wxT("\n");
text << wxT("ObjectSwitch =") << cmp->GetSwitch(wxT("Object")) << wxT("\n");
text << wxT("ArchiveOutputSwitch =") << cmp->GetSwitch(wxT("ArchiveOutput")) << wxT("\n");
text << wxT("PreprocessOnlySwitch =") << cmp->GetSwitch(wxT("PreprocessOnly")) << wxT("\n");
wxFileName fnObjectsFileName(objectsFileName);
fnObjectsFileName.MakeRelativeTo(proj->GetFileName().GetPath());
text << wxT("ObjectsFileList =\"") << fnObjectsFileName.GetFullPath() << wxT("\"\n");
text << wxT("PCHCompileFlags =") << bldConf->GetPchCompileFlags() << wxT("\n");
wxString mkdirCommand = cmp->GetTool("MakeDirCommand");
if(!mkdirCommand.IsEmpty()) {
// use the compiler defined one
if(OS_WINDOWS) {
mkdirCommand = ReplaceBackslashes(mkdirCommand);
}
text << wxT("MakeDirCommand =") << mkdirCommand << wxT("\n");
} else {
text << wxT("MakeDirCommand =") << wxT("makedir") << wxT("\n");
}
wxString buildOpts = bldConf->GetCompileOptions();
buildOpts.Replace(wxT(";"), wxT(" "));
wxString cBuildOpts = bldConf->GetCCompileOptions();
cBuildOpts.Replace(wxT(";"), wxT(" "));
wxString asOptions = bldConf->GetAssmeblerOptions();
asOptions.Replace(";", " ");
// Let the plugins add their content here
clBuildEvent e(wxEVT_GET_ADDITIONAL_COMPILEFLAGS);
e.SetProjectName(proj->GetName());
e.SetConfigurationName(bldConf->GetName());
EventNotifier::Get()->ProcessEvent(e);
wxString additionalCompileFlags = e.GetCommand();
if(additionalCompileFlags.IsEmpty() == false) {
buildOpts << wxT(" ") << additionalCompileFlags;
cBuildOpts << wxT(" ") << additionalCompileFlags;
}
// only if resource compiler required, evaluate the resource variables
if(OS_WINDOWS) {
wxString rcBuildOpts = bldConf->GetResCompileOptions();
rcBuildOpts.Replace(wxT(";"), wxT(" "));
text << wxT("RcCmpOptions =") << rcBuildOpts << wxT("\n");
wxString rcCompilerstr(cmp->GetTool(wxT("ResourceCompiler")));
rcCompilerstr.Replace(wxT("/"), wxT("\\"));
text << wxT("RcCompilerName =") << rcCompilerstr << wxT("\n");
}
wxString linkOpt = bldConf->GetLinkOptions();
linkOpt.Replace(wxT(";"), wxT(" "));
// link options are kept with semi-colons, strip them
text << wxT("LinkOptions = ") << linkOpt << wxT("\n");
// add the global include path followed by the project include path
wxString pchFile;
// If the PCH is required to be in the command line, add it here
// otherwise, we just make sure it is generated and the compiler will pick it by itself
if(bldConf->GetPchInCommandLine()) {
pchFile = bldConf->GetPrecompiledHeader();
pchFile.Trim().Trim(false);
if(pchFile.IsEmpty() == false) {
pchFile.Prepend(wxT(" -include ")).Append(wxT(" "));
}
}
wxString libraries = bldConf->GetLibraries();
wxArrayString libsArr = ::wxStringTokenize(libraries, wxT(";"), wxTOKEN_STRTOK);
libraries.Clear();
libraries << wxT(" ");
for(size_t i = 0; i < libsArr.GetCount(); i++) {
libsArr.Item(i).Trim().Trim(false);
libraries << wxT("\"") << libsArr.Item(i) << wxT("\" ");
}
libraries.Replace(wxT("/"), wxT("\\"));
text << wxT("IncludePath = ")
<< ParseIncludePath(cmp->GetGlobalIncludePath(), proj->GetName(), bldConf->GetName()) << wxT(" ")
<< ParseIncludePath(bldConf->GetIncludePath(), proj->GetName(), bldConf->GetName()) << wxT("\n");
text << wxT("IncludePCH = ") << pchFile << wxT("\n");
text << wxT("RcIncludePath = ")
<< ParseIncludePath(bldConf->GetResCmpIncludePath(), proj->GetName(), bldConf->GetName()) << wxT("\n");
text << wxT("Libs = ") << ParseLibs(bldConf->GetLibraries()) << wxT("\n");
text << wxT("ArLibs = ") << libraries << wxT("\n");
// add the global library path followed by the project library path
text << wxT("LibPath =")
<< ParseLibPath(cmp->GetGlobalLibPath(), proj->GetName(), bldConf->GetName()) << wxT(" ")
<< ParseLibPath(bldConf->GetLibPath(), proj->GetName(), bldConf->GetName()) << wxT("\n");
text << wxT("\n");
text << wxT("##\n");
text << wxT("## Common variables\n");
text << wxT("## AR, CXX, CC, AS, CXXFLAGS and CFLAGS can be overriden using an environment variables\n");
text << wxT("##\n");
wxString arStr(cmp->GetTool(wxT("AR")));
wxString cxxStr(cmp->GetTool(wxT("CXX")));
wxString ccStr(cmp->GetTool(wxT("CC")));
wxString asStr(cmp->GetTool(wxT("AS")));
if(OS_WINDOWS) {
arStr = ReplaceBackslashes(arStr);
cxxStr = ReplaceBackslashes(cxxStr);
ccStr = ReplaceBackslashes(ccStr);
asStr = ReplaceBackslashes(asStr);
}
text << wxT("AR = ") << arStr << wxT("\n");
text << wxT("CXX = ") << cxxStr << wxT("\n");
text << wxT("CC = ") << ccStr << wxT("\n");
text << wxT("CXXFLAGS = ") << buildOpts << wxT(" $(Preprocessors)") << wxT("\n");
text << wxT("CFLAGS = ") << cBuildOpts << wxT(" $(Preprocessors)") << wxT("\n");
text << wxT("ASFLAGS = ") << asOptions << "\n";
text << wxT("AS = ") << asStr << "\n";
text << wxT("\n\n");
}
wxString BuilderNMake::ParseIncludePath(const wxString& paths, const wxString& projectName, const wxString& selConf)
{
// convert semi-colon delimited string into GNU list of
// include paths:
wxString incluedPath(wxEmptyString);
wxStringTokenizer tkz(paths, wxT(";"), wxTOKEN_STRTOK);
// prepend each include path with -I
while(tkz.HasMoreTokens()) {
wxString path(tkz.NextToken());
TrimString(path);
// path.Replace(wxT("\\"), wxT("/"));
wxString wrapper;
if(path.Contains(wxT(" "))) wrapper = wxT("\"");
incluedPath << wxT("$(IncludeSwitch)") << wrapper << path << wrapper << wxT(" ");
}
return incluedPath;
}
wxString BuilderNMake::ParseLibPath(const wxString& paths, const wxString& projectName, const wxString& selConf)
{
// convert semi-colon delimited string into GNU list of
// lib path
wxString libPath(wxEmptyString);
wxStringTokenizer tkz(paths, wxT(";"), wxTOKEN_STRTOK);
// prepend each include path with libpath switch
while(tkz.HasMoreTokens()) {
wxString path(tkz.NextToken());
path.Trim().Trim(false);
// path.Replace(wxT("\\"), wxT("/"));
wxString wrapper;
if(path.Contains(wxT(" "))) wrapper = wxT("\"");
libPath << wxT("$(LibraryPathSwitch)") << wrapper << path << wrapper << wxT(" ");
}
return libPath;
}
wxString BuilderNMake::ParsePreprocessor(const wxString& prep)
{
wxString preprocessor(wxEmptyString);
wxStringTokenizer tkz(prep, wxT(";"), wxTOKEN_STRTOK);
// prepend each include path with libpath switch
while(tkz.HasMoreTokens()) {
wxString p(tkz.NextToken());
p.Trim().Trim(false);
preprocessor << wxT("$(PreprocessorSwitch)") << p << wxT(" ");
}
// if the macro contains # escape it
// But first remove any manual escaping done by the user
preprocessor.Replace(wxT("\\#"), wxT("#"));
preprocessor.Replace(wxT("#"), wxT("\\#"));
return preprocessor;
}
wxString BuilderNMake::ParseLibs(const wxString& libs)
{
// convert semi-colon delimited string into GNU list of
// libs
wxString slibs(wxEmptyString);
wxStringTokenizer tkz(libs, wxT(";"), wxTOKEN_STRTOK);
// prepend each include path with -l and strip trailing lib string
// also, if the file contains an extension (.a, .so, .dynlib) remove them as well
while(tkz.HasMoreTokens()) {
wxString lib(tkz.NextToken());
TrimString(lib);
// remove lib prefix
if(lib.StartsWith(wxT("lib"))) {
lib = lib.Mid(3);
}
// remove known suffixes
if(lib.EndsWith(wxT(".a")) || lib.EndsWith(wxT(".so")) || lib.EndsWith(wxT(".dylib")) ||
lib.EndsWith(wxT(".dll"))) {
lib = lib.BeforeLast(wxT('.'));
}
slibs << wxT("$(LibrarySwitch)") << lib << wxT(" ");
}
return slibs;
}
wxString BuilderNMake::GetBuildCommand(const wxString& project, const wxString& confToBuild, const wxString& arguments)
{
wxString errMsg, cmd;
BuildConfigPtr bldConf = clCxxWorkspaceST::Get()->GetProjBuildConf(project, confToBuild);
if(!bldConf) {
return wxEmptyString;
}
// generate the makefile
Export(project, confToBuild, arguments, false, false, errMsg);
wxString buildTool = GetBuildToolCommand(project, confToBuild, arguments, true);
buildTool = EnvironmentConfig::Instance()->ExpandVariables(buildTool, true);
// fix: replace all Windows like slashes to POSIX
// buildTool.Replace(wxT("\\"), wxT("/"));
cmd << buildTool << wxT(" Makefile");
return cmd;
}
wxString BuilderNMake::GetCleanCommand(const wxString& project, const wxString& confToBuild, const wxString& arguments)
{
wxString errMsg, cmd;
BuildConfigPtr bldConf = clCxxWorkspaceST::Get()->GetProjBuildConf(project, confToBuild);
if(!bldConf) {
return wxEmptyString;
}
// generate the makefile
Export(project, confToBuild, arguments, false, false, errMsg);
wxString buildTool = GetBuildToolCommand(project, confToBuild, arguments, true);
buildTool = EnvironmentConfig::Instance()->ExpandVariables(buildTool, true);
// fix: replace all Windows like slashes to POSIX
// buildTool.Replace(wxT("\\"), wxT("/"));
BuildMatrixPtr matrix = clCxxWorkspaceST::Get()->GetBuildMatrix();
wxString type = Builder::NormalizeConfigName(matrix->GetSelectedConfigurationName());
cmd << buildTool << wxT(" Makefile clean");
return cmd;
}
wxString BuilderNMake::GetPOBuildCommand(const wxString& project, const wxString& confToBuild,
const wxString& arguments)
{
wxString errMsg, cmd;
ProjectPtr proj = clCxxWorkspaceST::Get()->FindProjectByName(project, errMsg);
if(!proj) {
return wxEmptyString;
}
// generate the makefile
Export(project, confToBuild, arguments, true, false, errMsg);
cmd = GetProjectMakeCommand(proj, confToBuild, wxT("all"), false, false);
return cmd;
}
wxString BuilderNMake::GetPOCleanCommand(const wxString& project, const wxString& confToBuild,
const wxString& arguments)
{
wxString errMsg, cmd;
ProjectPtr proj = clCxxWorkspaceST::Get()->FindProjectByName(project, errMsg);
if(!proj) {
return wxEmptyString;
}
// generate the makefile
Export(project, confToBuild, arguments, true, false, errMsg);
cmd = GetProjectMakeCommand(proj, confToBuild, wxT("clean"), false, true);
return cmd;
}
wxString BuilderNMake::GetSingleFileCmd(const wxString& project, const wxString& confToBuild, const wxString& arguments,
const wxString& fileName)
{
wxString errMsg, cmd;
ProjectPtr proj = clCxxWorkspaceST::Get()->FindProjectByName(project, errMsg);
if(!proj) {
return wxEmptyString;
}
// generate the makefile
Export(project, confToBuild, arguments, true, false, errMsg);
// Build the target list
wxString target;
wxString cmpType;
wxFileName fn(fileName);
BuildConfigPtr bldConf = clCxxWorkspaceST::Get()->GetProjBuildConf(project, confToBuild);
if(!bldConf) {
return wxEmptyString;
}
cmpType = bldConf->GetCompilerType();
CompilerPtr cmp = BuildSettingsConfigST::Get()->GetCompiler(cmpType);
// fn.MakeRelativeTo(proj->GetFileName().GetPath());
wxString relPath = fn.GetPath(true, wxPATH_UNIX);
wxString objNamePrefix = DoGetTargetPrefix(fn, proj->GetFileName().GetPath(), cmp);
target << bldConf->GetIntermediateDirectory() << wxT("/") << objNamePrefix << fn.GetFullName()
<< cmp->GetObjectSuffix();
target = ExpandAllVariables(target, clCxxWorkspaceST::Get(), proj->GetName(), confToBuild, wxEmptyString);
cmd = GetProjectMakeCommand(proj, confToBuild, target, false, false);
return EnvironmentConfig::Instance()->ExpandVariables(cmd, true);
}
wxString BuilderNMake::GetPreprocessFileCmd(const wxString& project, const wxString& confToBuild,
const wxString& arguments, const wxString& fileName, wxString& errMsg)
{
ProjectPtr proj = clCxxWorkspaceST::Get()->FindProjectByName(project, errMsg);
if(!proj) {
return wxEmptyString;
}
wxString cmd;
BuildConfigPtr bldConf = clCxxWorkspaceST::Get()->GetProjBuildConf(project, confToBuild);
if(!bldConf) {
return wxEmptyString;
}
// generate the makefile
Export(project, confToBuild, arguments, true, false, errMsg);
BuildMatrixPtr matrix = clCxxWorkspaceST::Get()->GetBuildMatrix();
wxString buildTool = GetBuildToolCommand(project, confToBuild, arguments, true);
wxString type = matrix->GetProjectSelectedConf(matrix->GetSelectedConfigurationName(), project);
// fix: replace all Windows like slashes to POSIX
// buildTool.Replace(wxT("\\"), wxT("/"));
// create the target
wxString target;
wxString objSuffix;
wxFileName fn(fileName);
wxString cmpType = bldConf->GetCompilerType();
CompilerPtr cmp = BuildSettingsConfigST::Get()->GetCompiler(cmpType);
wxString objNamePrefix = DoGetTargetPrefix(fn, proj->GetFileName().GetPath(), cmp);
target << bldConf->GetIntermediateDirectory() << wxT("/") << objNamePrefix << fn.GetFullName()
<< cmp->GetPreprocessSuffix();
target = ExpandAllVariables(target, clCxxWorkspaceST::Get(), proj->GetName(), confToBuild, wxEmptyString);
cmd = GetProjectMakeCommand(proj, confToBuild, target, false, false);
return EnvironmentConfig::Instance()->ExpandVariables(cmd, true);
}
wxString BuilderNMake::GetCdCmd(const wxFileName& path1, const wxFileName& path2)
{
wxString cd_cmd(wxT("@"));
if(path2.GetPath().IsEmpty()) {
return cd_cmd;
}
if(path1.GetPath() != path2.GetPath()) {
cd_cmd << wxT("cd \"") << path2.GetPath() << wxT("\" && ");
}
return cd_cmd;
}
void BuilderNMake::CreateCustomPostBuildEvents(BuildConfigPtr bldConf, wxString& text)
{
BuildCommandList cmds;
BuildCommandList::iterator iter;
cmds.clear();
bldConf->GetPostBuildCommands(cmds);
bool first(true);
if(!cmds.empty()) {
iter = cmds.begin();
for(; iter != cmds.end(); iter++) {
if(iter->GetEnabled()) {
if(first) {
text << wxT("\t@echo Executing Post Build commands ...\n");
first = false;
}
text << wxT("\t") << iter->GetCommand() << wxT("\n");
}
}
if(!first) {
text << wxT("\t@echo Done\n");
}
}
}
void BuilderNMake::CreateCustomPreBuildEvents(BuildConfigPtr bldConf, wxString& text)
{
BuildCommandList cmds;
BuildCommandList::iterator iter;
cmds.clear();
bldConf->GetPreBuildCommands(cmds);
bool first(true);
if(!cmds.empty()) {
iter = cmds.begin();
for(; iter != cmds.end(); iter++) {
if(iter->GetEnabled()) {
if(first) {
text << wxT("\t@echo Executing Pre Build commands ...\n");
first = false;
}
text << wxT("\t") << iter->GetCommand() << wxT("\n");
}
}
if(!first) {
text << wxT("\t@echo Done\n");
}
}
}
wxString BuilderNMake::GetProjectMakeCommand(const wxFileName& wspfile, const wxFileName& projectPath, ProjectPtr proj,
const wxString& confToBuild)
{
BuildConfigPtr bldConf = clCxxWorkspaceST::Get()->GetProjBuildConf(proj->GetName(), confToBuild);
// iterate over the dependencies projects and generate makefile
wxString makeCommand;
wxString basicMakeCommand;
wxString buildTool = GetBuildToolCommand(proj->GetName(), confToBuild, "", false);
buildTool = EnvironmentConfig::Instance()->ExpandVariables(buildTool, true);
basicMakeCommand << buildTool << wxT(" \"") << proj->GetName() << wxT(".mk\"");
makeCommand << wxT("\t") << GetCdCmd(wspfile, projectPath);
if(bldConf) {
wxString preprebuild = bldConf->GetPreBuildCustom();
wxString precmpheader = bldConf->GetPrecompiledHeader();
precmpheader.Trim().Trim(false);
preprebuild.Trim().Trim(false);
if(preprebuild.IsEmpty() == false) {
makeCommand << basicMakeCommand << wxT(" PrePreBuild && ");
}
if(HasPrebuildCommands(bldConf)) {
makeCommand << basicMakeCommand << wxT(" PreBuild && ");
}
// Run pre-compiled header compilation if any
if(precmpheader.IsEmpty() == false) {
makeCommand << basicMakeCommand << wxT(" ") << precmpheader << wxT(".gch") << wxT(" && ");
}
}
// the main command
makeCommand << basicMakeCommand;
// post
if(bldConf && HasPostbuildCommands(bldConf)) {
makeCommand << wxT(" && ") << basicMakeCommand << wxT(" PostBuild");
}
makeCommand << wxT("\n");
return makeCommand;
}
wxString BuilderNMake::GetProjectMakeCommand(ProjectPtr proj, const wxString& confToBuild, const wxString& target,
bool addCleanTarget, bool cleanOnly)
{
BuildConfigPtr bldConf = clCxxWorkspaceST::Get()->GetProjBuildConf(proj->GetName(), confToBuild);
// iterate over the dependencies projects and generate makefile
wxString makeCommand;
wxString basicMakeCommand;
wxString buildTool = GetBuildToolCommand(proj->GetName(), confToBuild, "", true);
buildTool = EnvironmentConfig::Instance()->ExpandVariables(buildTool, true);
basicMakeCommand << buildTool << wxT(" \"") << proj->GetName() << wxT(".mk\" ");
if(addCleanTarget) {
makeCommand << basicMakeCommand << wxT(" clean && ");
}
if(bldConf && !cleanOnly) {
wxString preprebuild = bldConf->GetPreBuildCustom();
wxString precmpheader = bldConf->GetPrecompiledHeader();
precmpheader.Trim().Trim(false);
preprebuild.Trim().Trim(false);
makeCommand << basicMakeCommand << " MakeIntermediateDirs && ";
if(preprebuild.IsEmpty() == false) {
makeCommand << basicMakeCommand << wxT(" PrePreBuild && ");
}
if(HasPrebuildCommands(bldConf)) {
makeCommand << basicMakeCommand << wxT(" PreBuild && ");
}
// Run pre-compiled header compilation if any
if(precmpheader.IsEmpty() == false) {
makeCommand << basicMakeCommand << wxT(" ") << precmpheader << wxT(".gch") << wxT(" && ");
}
}
makeCommand << basicMakeCommand << wxT(" ") << target;
// post
if(bldConf && !cleanOnly && HasPostbuildCommands(bldConf)) {
makeCommand << wxT(" && ") << basicMakeCommand << wxT(" PostBuild");
}
return makeCommand;
}
void BuilderNMake::CreatePreCompiledHeaderTarget(BuildConfigPtr bldConf, wxString& text)
{
wxString filename = bldConf->GetPrecompiledHeader();
filename.Trim().Trim(false);
if(filename.IsEmpty()) return;
text << wxT("\n");
text << wxT("# PreCompiled Header\n");
text << filename << wxT(".gch: ") << filename << wxT("\n");
if(bldConf->GetPCHFlagsPolicy() == BuildConfig::kPCHPolicyReplace) {
text << wxT("\t") << DoGetCompilerMacro(filename) << wxT(" $(SourceSwitch) ") << filename
<< wxT(" $(PCHCompileFlags)\n");
} else { // BuildConfig::kPCHPolicyAppend
text << wxT("\t") << DoGetCompilerMacro(filename) << wxT(" $(SourceSwitch) ") << filename
<< wxT(" $(PCHCompileFlags) $(CXXFLAGS) $(IncludePath)\n");
}
text << wxT("\n");
}
wxString BuilderNMake::GetPORebuildCommand(const wxString& project, const wxString& confToBuild,
const wxString& arguments)
{
wxString errMsg, cmd;
ProjectPtr proj = clCxxWorkspaceST::Get()->FindProjectByName(project, errMsg);
if(!proj) {
return wxEmptyString;
}
// generate the makefile
Export(project, confToBuild, arguments, true, false, errMsg);
cmd = GetProjectMakeCommand(proj, confToBuild, wxT("all"), true, false);
return cmd;
}
wxString BuilderNMake::GetBuildToolCommand(const wxString& project, const wxString& confToBuild,
const wxString& arguments, bool isCommandlineCommand) const
{
wxString jobsCmd;
wxString buildTool;
BuildConfigPtr bldConf = clCxxWorkspaceST::Get()->GetProjBuildConf(project, confToBuild);
if(!bldConf) return wxEmptyString;
CompilerPtr compiler = bldConf->GetCompiler();
if(!compiler) return wxEmptyString;
if(isCommandlineCommand) {
buildTool = compiler->GetTool("MAKE");
} else {
jobsCmd = wxEmptyString;
buildTool = wxT("\"$(MAKE)\"");
}
if(isCommandlineCommand) {
return buildTool + " -e -f ";
} else {
return buildTool + " -f ";
}
}
wxString BuilderNMake::DoGetCompilerMacro(const wxString& filename)
{
wxString compilerMacro(wxT("$(CXX)"));
switch(FileExtManager::GetType(filename)) {
case FileExtManager::TypeSourceC:
compilerMacro = wxT("$(CC)");
break;
case FileExtManager::TypeSourceCpp:
default:
compilerMacro = wxT("$(CXX)");
break;
}
return compilerMacro;
}
wxString BuilderNMake::DoGetTargetPrefix(const wxFileName& filename, const wxString& cwd, CompilerPtr cmp)
{
size_t count = filename.GetDirCount();
const wxArrayString& dirs = filename.GetDirs();
wxString lastDir;
if(cwd == filename.GetPath()) return wxEmptyString;
if(cmp && cmp->GetObjectNameIdenticalToFileName()) return wxEmptyString;
if(cwd == filename.GetPath()) {
return wxEmptyString;
}
if(count) {
lastDir = dirs.Item(count - 1);
// Handle special directory paths
if(lastDir == wxT("..")) {
lastDir = wxT("up");
} else if(lastDir == wxT(".")) {
lastDir = wxT("cur");
}
if(lastDir.IsEmpty() == false) {
lastDir << wxT("_");
}
}
return lastDir;
}
wxString BuilderNMake::DoGetMarkerFileDir(const wxString& projname, const wxString& projectPath)
{
BuildMatrixPtr matrix = clCxxWorkspaceST::Get()->GetBuildMatrix();
wxString workspaceSelConf = matrix->GetSelectedConfigurationName();
workspaceSelConf = NormalizeConfigName(workspaceSelConf);
workspaceSelConf.MakeLower();
wxString path;
if(projname.IsEmpty()) {
path << clCxxWorkspaceST::Get()->GetWorkspaceFileName().GetPath() << "/" << wxT(".build-") << workspaceSelConf;
} else {
path << clCxxWorkspaceST::Get()->GetWorkspaceFileName().GetPath() << "/" << wxT(".build-") << workspaceSelConf
<< "/" << projname;
}
if(projectPath.IsEmpty() == false) {
wxFileName fn(path);
fn.MakeRelativeTo(projectPath);
path = fn.GetFullPath(wxPATH_WIN);
}
if(!projname.IsEmpty())
return "\"" + path + "\"";
else
return path;
}
bool BuilderNMake::HasPostbuildCommands(BuildConfigPtr bldConf) const
{
BuildCommandList cmds;
bldConf->GetPostBuildCommands(cmds);
BuildCommandList::const_iterator iter = cmds.begin();
for(; iter != cmds.end(); iter++) {
if(iter->GetEnabled()) {
return true;
}
}
return false;
}
bool BuilderNMake::SendBuildEvent(int eventId, const wxString& projectName, const wxString& configurationName)
{
clBuildEvent e(eventId);
e.SetProjectName(projectName);
e.SetConfigurationName(configurationName);
return EventNotifier::Get()->ProcessEvent(e);
}
|