cmExtraCodeLiteGenerator.cxx 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmExtraCodeLiteGenerator.h"
  4. #include "cmGeneratedFileStream.h"
  5. #include "cmGeneratorTarget.h"
  6. #include "cmGlobalGenerator.h"
  7. #include "cmLocalGenerator.h"
  8. #include "cmMakefile.h"
  9. #include "cmSourceFile.h"
  10. #include "cmStateTypes.h"
  11. #include "cmSystemTools.h"
  12. #include "cmXMLWriter.h"
  13. #include "cmake.h"
  14. #include "cmsys/SystemInformation.hxx"
  15. #include <map>
  16. #include <set>
  17. #include <sstream>
  18. #include <string.h>
  19. #include <utility>
  20. cmExtraCodeLiteGenerator::cmExtraCodeLiteGenerator()
  21. : cmExternalMakefileProjectGenerator()
  22. , ConfigName("NoConfig")
  23. , CpuCount(2)
  24. {
  25. }
  26. cmExternalMakefileProjectGeneratorFactory*
  27. cmExtraCodeLiteGenerator::GetFactory()
  28. {
  29. static cmExternalMakefileProjectGeneratorSimpleFactory<
  30. cmExtraCodeLiteGenerator>
  31. factory("CodeLite", "Generates CodeLite project files.");
  32. if (factory.GetSupportedGlobalGenerators().empty()) {
  33. #if defined(_WIN32)
  34. factory.AddSupportedGlobalGenerator("MinGW Makefiles");
  35. factory.AddSupportedGlobalGenerator("NMake Makefiles");
  36. #endif
  37. factory.AddSupportedGlobalGenerator("Ninja");
  38. factory.AddSupportedGlobalGenerator("Unix Makefiles");
  39. }
  40. return &factory;
  41. }
  42. void cmExtraCodeLiteGenerator::Generate()
  43. {
  44. // Hold root tree information for creating the workspace
  45. std::string workspaceProjectName;
  46. std::string workspaceOutputDir;
  47. std::string workspaceFileName;
  48. std::string workspaceSourcePath;
  49. const std::map<std::string, std::vector<cmLocalGenerator*>>& projectMap =
  50. this->GlobalGenerator->GetProjectMap();
  51. // loop projects and locate the root project.
  52. // and extract the information for creating the worspace
  53. // root makefile
  54. for (auto const& it : projectMap) {
  55. const cmMakefile* mf = it.second[0]->GetMakefile();
  56. this->ConfigName = GetConfigurationName(mf);
  57. if (it.second[0]->GetCurrentBinaryDirectory() ==
  58. it.second[0]->GetBinaryDirectory()) {
  59. workspaceOutputDir = it.second[0]->GetCurrentBinaryDirectory();
  60. workspaceProjectName = it.second[0]->GetProjectName();
  61. workspaceSourcePath = it.second[0]->GetSourceDirectory();
  62. workspaceFileName = workspaceOutputDir + "/";
  63. workspaceFileName += workspaceProjectName + ".workspace";
  64. this->WorkspacePath = it.second[0]->GetCurrentBinaryDirectory();
  65. ;
  66. break;
  67. }
  68. }
  69. cmGeneratedFileStream fout(workspaceFileName.c_str());
  70. cmXMLWriter xml(fout);
  71. xml.StartDocument("utf-8");
  72. xml.StartElement("CodeLite_Workspace");
  73. xml.Attribute("Name", workspaceProjectName);
  74. bool const targetsAreProjects =
  75. this->GlobalGenerator->GlobalSettingIsOn("CMAKE_CODELITE_USE_TARGETS");
  76. std::vector<std::string> ProjectNames;
  77. if (targetsAreProjects) {
  78. ProjectNames = CreateProjectsByTarget(&xml);
  79. } else {
  80. ProjectNames = CreateProjectsByProjectMaps(&xml);
  81. }
  82. xml.StartElement("BuildMatrix");
  83. xml.StartElement("WorkspaceConfiguration");
  84. xml.Attribute("Name", this->ConfigName);
  85. xml.Attribute("Selected", "yes");
  86. for (std::string const& it : ProjectNames) {
  87. xml.StartElement("Project");
  88. xml.Attribute("Name", it);
  89. xml.Attribute("ConfigName", this->ConfigName);
  90. xml.EndElement();
  91. }
  92. xml.EndElement(); // WorkspaceConfiguration
  93. xml.EndElement(); // BuildMatrix
  94. xml.EndElement(); // CodeLite_Workspace
  95. }
  96. // Create projects where targets are the projects
  97. std::vector<std::string> cmExtraCodeLiteGenerator::CreateProjectsByTarget(
  98. cmXMLWriter* xml)
  99. {
  100. std::vector<std::string> retval;
  101. // for each target in the workspace create a codelite project
  102. const std::vector<cmLocalGenerator*>& lgs =
  103. this->GlobalGenerator->GetLocalGenerators();
  104. for (cmLocalGenerator* lg : lgs) {
  105. for (cmGeneratorTarget* lt : lg->GetGeneratorTargets()) {
  106. cmStateEnums::TargetType type = lt->GetType();
  107. std::string outputDir = lg->GetCurrentBinaryDirectory();
  108. std::string targetName = lt->GetName();
  109. std::string filename = outputDir + "/" + targetName + ".project";
  110. retval.push_back(targetName);
  111. // Make the project file relative to the workspace
  112. std::string relafilename =
  113. cmSystemTools::RelativePath(this->WorkspacePath, filename);
  114. std::string visualname = targetName;
  115. switch (type) {
  116. case cmStateEnums::SHARED_LIBRARY:
  117. case cmStateEnums::STATIC_LIBRARY:
  118. case cmStateEnums::MODULE_LIBRARY:
  119. visualname = "lib" + visualname;
  120. CM_FALLTHROUGH;
  121. case cmStateEnums::EXECUTABLE:
  122. xml->StartElement("Project");
  123. xml->Attribute("Name", visualname);
  124. xml->Attribute("Path", relafilename);
  125. xml->Attribute("Active", "No");
  126. xml->EndElement();
  127. CreateNewProjectFile(lt, filename);
  128. break;
  129. default:
  130. break;
  131. }
  132. }
  133. }
  134. return retval;
  135. }
  136. // The "older way of doing it.
  137. std::vector<std::string> cmExtraCodeLiteGenerator::CreateProjectsByProjectMaps(
  138. cmXMLWriter* xml)
  139. {
  140. std::vector<std::string> retval;
  141. // for each sub project in the workspace create a codelite project
  142. for (auto const& it : this->GlobalGenerator->GetProjectMap()) {
  143. std::string outputDir = it.second[0]->GetCurrentBinaryDirectory();
  144. std::string projectName = it.second[0]->GetProjectName();
  145. retval.push_back(projectName);
  146. std::string filename = outputDir + "/" + projectName + ".project";
  147. // Make the project file relative to the workspace
  148. filename = cmSystemTools::RelativePath(this->WorkspacePath, filename);
  149. // create a project file
  150. this->CreateProjectFile(it.second);
  151. xml->StartElement("Project");
  152. xml->Attribute("Name", projectName);
  153. xml->Attribute("Path", filename);
  154. xml->Attribute("Active", "No");
  155. xml->EndElement();
  156. }
  157. return retval;
  158. }
  159. /* create the project file */
  160. void cmExtraCodeLiteGenerator::CreateProjectFile(
  161. const std::vector<cmLocalGenerator*>& lgs)
  162. {
  163. std::string outputDir = lgs[0]->GetCurrentBinaryDirectory();
  164. std::string projectName = lgs[0]->GetProjectName();
  165. std::string filename = outputDir + "/";
  166. filename += projectName + ".project";
  167. this->CreateNewProjectFile(lgs, filename);
  168. }
  169. std::string cmExtraCodeLiteGenerator::CollectSourceFiles(
  170. const cmMakefile* makefile, const cmGeneratorTarget* gt,
  171. std::map<std::string, cmSourceFile*>& cFiles,
  172. std::set<std::string>& otherFiles)
  173. {
  174. std::string projectType;
  175. switch (gt->GetType()) {
  176. case cmStateEnums::EXECUTABLE: {
  177. projectType = "Executable";
  178. } break;
  179. case cmStateEnums::STATIC_LIBRARY: {
  180. projectType = "Static Library";
  181. } break;
  182. case cmStateEnums::SHARED_LIBRARY: {
  183. projectType = "Dynamic Library";
  184. } break;
  185. case cmStateEnums::MODULE_LIBRARY: {
  186. projectType = "Dynamic Library";
  187. } break;
  188. default: // intended fallthrough
  189. break;
  190. }
  191. switch (gt->GetType()) {
  192. case cmStateEnums::EXECUTABLE:
  193. case cmStateEnums::STATIC_LIBRARY:
  194. case cmStateEnums::SHARED_LIBRARY:
  195. case cmStateEnums::MODULE_LIBRARY: {
  196. std::vector<cmSourceFile*> sources;
  197. gt->GetSourceFiles(sources,
  198. makefile->GetSafeDefinition("CMAKE_BUILD_TYPE"));
  199. for (cmSourceFile* s : sources) {
  200. // check whether it is a source or a include file
  201. // then put it accordingly into one of the two containers
  202. switch (cmSystemTools::GetFileFormat(s->GetExtension().c_str())) {
  203. case cmSystemTools::C_FILE_FORMAT:
  204. case cmSystemTools::CXX_FILE_FORMAT:
  205. case cmSystemTools::CUDA_FILE_FORMAT:
  206. case cmSystemTools::FORTRAN_FILE_FORMAT: {
  207. cFiles[s->GetFullPath()] = s;
  208. } break;
  209. default: {
  210. otherFiles.insert(s->GetFullPath());
  211. }
  212. }
  213. }
  214. }
  215. default: // intended fallthrough
  216. break;
  217. }
  218. return projectType;
  219. }
  220. void cmExtraCodeLiteGenerator::CreateNewProjectFile(
  221. const std::vector<cmLocalGenerator*>& lgs, const std::string& filename)
  222. {
  223. const cmMakefile* mf = lgs[0]->GetMakefile();
  224. cmGeneratedFileStream fout(filename.c_str());
  225. if (!fout) {
  226. return;
  227. }
  228. cmXMLWriter xml(fout);
  229. ////////////////////////////////////
  230. xml.StartDocument("utf-8");
  231. xml.StartElement("CodeLite_Project");
  232. xml.Attribute("Name", lgs[0]->GetProjectName());
  233. xml.Attribute("InternalType", "");
  234. std::string projectType;
  235. // Collect all used source files in the project
  236. // Sort them into two containers, one for C/C++ implementation files
  237. // which may have an acompanying header, one for all other files
  238. std::map<std::string, cmSourceFile*> cFiles;
  239. std::set<std::string> otherFiles;
  240. for (cmLocalGenerator* lg : lgs) {
  241. cmMakefile* makefile = lg->GetMakefile();
  242. const std::vector<cmGeneratorTarget*>& targets = lg->GetGeneratorTargets();
  243. for (cmGeneratorTarget* target : targets) {
  244. projectType = CollectSourceFiles(makefile, target, cFiles, otherFiles);
  245. }
  246. }
  247. // Get the project path ( we need it later to convert files to
  248. // their relative path)
  249. std::string projectPath = cmSystemTools::GetFilenamePath(filename);
  250. CreateProjectSourceEntries(cFiles, otherFiles, &xml, projectPath, mf,
  251. projectType, "");
  252. xml.EndElement(); // CodeLite_Project
  253. }
  254. void cmExtraCodeLiteGenerator::FindMatchingHeaderfiles(
  255. std::map<std::string, cmSourceFile*>& cFiles,
  256. std::set<std::string>& otherFiles)
  257. {
  258. const std::vector<std::string>& headerExts =
  259. this->GlobalGenerator->GetCMakeInstance()->GetHeaderExtensions();
  260. // The following loop tries to add header files matching to implementation
  261. // files to the project. It does that by iterating over all source files,
  262. // replacing the file name extension with ".h" and checks whether such a
  263. // file exists. If it does, it is inserted into the map of files.
  264. // A very similar version of that code exists also in the CodeBlocks
  265. // project generator.
  266. for (auto const& sit : cFiles) {
  267. std::string headerBasename = cmSystemTools::GetFilenamePath(sit.first);
  268. headerBasename += "/";
  269. headerBasename += cmSystemTools::GetFilenameWithoutExtension(sit.first);
  270. // check if there's a matching header around
  271. for (std::string const& ext : headerExts) {
  272. std::string hname = headerBasename;
  273. hname += ".";
  274. hname += ext;
  275. // if it's already in the set, don't check if it exists on disk
  276. std::set<std::string>::const_iterator headerIt = otherFiles.find(hname);
  277. if (headerIt != otherFiles.end()) {
  278. break;
  279. }
  280. if (cmSystemTools::FileExists(hname)) {
  281. otherFiles.insert(hname);
  282. break;
  283. }
  284. }
  285. }
  286. }
  287. void cmExtraCodeLiteGenerator::CreateFoldersAndFiles(
  288. std::set<std::string>& cFiles, cmXMLWriter& xml,
  289. const std::string& projectPath)
  290. {
  291. std::vector<std::string> tmp_path;
  292. std::vector<std::string> components;
  293. size_t numOfEndEl = 0;
  294. for (std::string const& cFile : cFiles) {
  295. std::string frelapath = cmSystemTools::RelativePath(projectPath, cFile);
  296. cmsys::SystemTools::SplitPath(frelapath, components, false);
  297. components.pop_back(); // erase last member -> it is file, not folder
  298. components.erase(components.begin()); // erase "root"
  299. size_t sizeOfSkip = 0;
  300. for (size_t i = 0; i < components.size(); ++i) {
  301. // skip relative path
  302. if (components[i] == ".." || components[i] == ".") {
  303. sizeOfSkip++;
  304. continue;
  305. }
  306. // same folder
  307. if (tmp_path.size() > i - sizeOfSkip &&
  308. tmp_path[i - sizeOfSkip] == components[i]) {
  309. continue;
  310. }
  311. // delete "old" subfolders
  312. if (tmp_path.size() > i - sizeOfSkip) {
  313. numOfEndEl = tmp_path.size() - i + sizeOfSkip;
  314. tmp_path.erase(tmp_path.end() - numOfEndEl, tmp_path.end());
  315. for (; numOfEndEl--;) {
  316. xml.EndElement();
  317. }
  318. }
  319. // add folder
  320. xml.StartElement("VirtualDirectory");
  321. xml.Attribute("Name", components[i]);
  322. tmp_path.push_back(components[i]);
  323. }
  324. // delete "old" subfolders
  325. numOfEndEl = tmp_path.size() - components.size() + sizeOfSkip;
  326. if (numOfEndEl) {
  327. tmp_path.erase(tmp_path.end() - numOfEndEl, tmp_path.end());
  328. for (; numOfEndEl--;) {
  329. xml.EndElement();
  330. }
  331. }
  332. // add file
  333. xml.StartElement("File");
  334. xml.Attribute("Name", frelapath);
  335. xml.EndElement();
  336. }
  337. // end of folders
  338. numOfEndEl = tmp_path.size();
  339. for (; numOfEndEl--;) {
  340. xml.EndElement();
  341. }
  342. }
  343. void cmExtraCodeLiteGenerator::CreateFoldersAndFiles(
  344. std::map<std::string, cmSourceFile*>& cFiles, cmXMLWriter& xml,
  345. const std::string& projectPath)
  346. {
  347. std::set<std::string> s;
  348. for (auto const& it : cFiles) {
  349. s.insert(it.first);
  350. }
  351. this->CreateFoldersAndFiles(s, xml, projectPath);
  352. }
  353. void cmExtraCodeLiteGenerator::CreateProjectSourceEntries(
  354. std::map<std::string, cmSourceFile*>& cFiles,
  355. std::set<std::string>& otherFiles, cmXMLWriter* _xml,
  356. const std::string& projectPath, const cmMakefile* mf,
  357. const std::string& projectType, const std::string& targetName)
  358. {
  359. cmXMLWriter& xml(*_xml);
  360. FindMatchingHeaderfiles(cFiles, otherFiles);
  361. // Create 2 virtual folders: src and include
  362. // and place all the implementation files into the src
  363. // folder, the rest goes to the include folder
  364. xml.StartElement("VirtualDirectory");
  365. xml.Attribute("Name", "src");
  366. // insert all source files in the codelite project
  367. // first the C/C++ implementation files, then all others
  368. this->CreateFoldersAndFiles(cFiles, xml, projectPath);
  369. xml.EndElement(); // VirtualDirectory
  370. xml.StartElement("VirtualDirectory");
  371. xml.Attribute("Name", "include");
  372. this->CreateFoldersAndFiles(otherFiles, xml, projectPath);
  373. xml.EndElement(); // VirtualDirectory
  374. // Get the number of CPUs. We use this information for the make -jN
  375. // command
  376. cmsys::SystemInformation info;
  377. info.RunCPUCheck();
  378. this->CpuCount =
  379. info.GetNumberOfLogicalCPU() * info.GetNumberOfPhysicalCPU();
  380. std::string codeliteCompilerName = this->GetCodeLiteCompilerName(mf);
  381. xml.StartElement("Settings");
  382. xml.Attribute("Type", projectType);
  383. xml.StartElement("Configuration");
  384. xml.Attribute("Name", this->ConfigName);
  385. xml.Attribute("CompilerType", this->GetCodeLiteCompilerName(mf));
  386. xml.Attribute("DebuggerType", "GNU gdb debugger");
  387. xml.Attribute("Type", projectType);
  388. xml.Attribute("BuildCmpWithGlobalSettings", "append");
  389. xml.Attribute("BuildLnkWithGlobalSettings", "append");
  390. xml.Attribute("BuildResWithGlobalSettings", "append");
  391. xml.StartElement("Compiler");
  392. xml.Attribute("Options", "-g");
  393. xml.Attribute("Required", "yes");
  394. xml.Attribute("PreCompiledHeader", "");
  395. xml.StartElement("IncludePath");
  396. xml.Attribute("Value", ".");
  397. xml.EndElement(); // IncludePath
  398. xml.EndElement(); // Compiler
  399. xml.StartElement("Linker");
  400. xml.Attribute("Options", "");
  401. xml.Attribute("Required", "yes");
  402. xml.EndElement(); // Linker
  403. xml.StartElement("ResourceCompiler");
  404. xml.Attribute("Options", "");
  405. xml.Attribute("Required", "no");
  406. xml.EndElement(); // ResourceCompiler
  407. xml.StartElement("General");
  408. std::string outputPath = mf->GetSafeDefinition("EXECUTABLE_OUTPUT_PATH");
  409. std::string relapath;
  410. if (!outputPath.empty()) {
  411. relapath = cmSystemTools::RelativePath(this->WorkspacePath, outputPath);
  412. xml.Attribute("OutputFile", relapath + "/$(ProjectName)");
  413. } else {
  414. xml.Attribute("OutputFile", "$(IntermediateDirectory)/$(ProjectName)");
  415. }
  416. xml.Attribute("IntermediateDirectory", "./");
  417. xml.Attribute("Command", "./$(ProjectName)");
  418. xml.Attribute("CommandArguments", "");
  419. if (!outputPath.empty()) {
  420. xml.Attribute("WorkingDirectory", relapath);
  421. } else {
  422. xml.Attribute("WorkingDirectory", "$(IntermediateDirectory)");
  423. }
  424. xml.Attribute("PauseExecWhenProcTerminates", "yes");
  425. xml.EndElement(); // General
  426. xml.StartElement("Debugger");
  427. xml.Attribute("IsRemote", "no");
  428. xml.Attribute("RemoteHostName", "");
  429. xml.Attribute("RemoteHostPort", "");
  430. xml.Attribute("DebuggerPath", "");
  431. xml.Element("PostConnectCommands");
  432. xml.Element("StartupCommands");
  433. xml.EndElement(); // Debugger
  434. xml.Element("PreBuild");
  435. xml.Element("PostBuild");
  436. xml.StartElement("CustomBuild");
  437. xml.Attribute("Enabled", "yes");
  438. xml.Element("RebuildCommand", GetRebuildCommand(mf, targetName));
  439. xml.Element("CleanCommand", GetCleanCommand(mf, targetName));
  440. xml.Element("BuildCommand", GetBuildCommand(mf, targetName));
  441. xml.Element("SingleFileCommand", GetSingleFileBuildCommand(mf));
  442. xml.Element("PreprocessFileCommand");
  443. xml.Element("WorkingDirectory", "$(WorkspacePath)");
  444. xml.EndElement(); // CustomBuild
  445. xml.StartElement("AdditionalRules");
  446. xml.Element("CustomPostBuild");
  447. xml.Element("CustomPreBuild");
  448. xml.EndElement(); // AdditionalRules
  449. xml.EndElement(); // Configuration
  450. xml.StartElement("GlobalSettings");
  451. xml.StartElement("Compiler");
  452. xml.Attribute("Options", "");
  453. xml.StartElement("IncludePath");
  454. xml.Attribute("Value", ".");
  455. xml.EndElement(); // IncludePath
  456. xml.EndElement(); // Compiler
  457. xml.StartElement("Linker");
  458. xml.Attribute("Options", "");
  459. xml.StartElement("LibraryPath");
  460. xml.Attribute("Value", ".");
  461. xml.EndElement(); // LibraryPath
  462. xml.EndElement(); // Linker
  463. xml.StartElement("ResourceCompiler");
  464. xml.Attribute("Options", "");
  465. xml.EndElement(); // ResourceCompiler
  466. xml.EndElement(); // GlobalSettings
  467. xml.EndElement(); // Settings
  468. }
  469. void cmExtraCodeLiteGenerator::CreateNewProjectFile(
  470. const cmGeneratorTarget* gt, const std::string& filename)
  471. {
  472. const cmMakefile* mf = gt->Makefile;
  473. cmGeneratedFileStream fout(filename.c_str());
  474. if (!fout) {
  475. return;
  476. }
  477. cmXMLWriter xml(fout);
  478. ////////////////////////////////////
  479. xml.StartDocument("utf-8");
  480. xml.StartElement("CodeLite_Project");
  481. std::string targetName = gt->GetName();
  482. std::string visualname = targetName;
  483. switch (gt->GetType()) {
  484. case cmStateEnums::STATIC_LIBRARY:
  485. case cmStateEnums::SHARED_LIBRARY:
  486. case cmStateEnums::MODULE_LIBRARY:
  487. visualname = "lib" + targetName;
  488. default: // intended fallthrough
  489. break;
  490. }
  491. xml.Attribute("Name", visualname);
  492. xml.Attribute("InternalType", "");
  493. // Collect all used source files in the project
  494. // Sort them into two containers, one for C/C++ implementation files
  495. // which may have an acompanying header, one for all other files
  496. std::string projectType;
  497. std::map<std::string, cmSourceFile*> cFiles;
  498. std::set<std::string> otherFiles;
  499. projectType = CollectSourceFiles(mf, gt, cFiles, otherFiles);
  500. // Get the project path ( we need it later to convert files to
  501. // their relative path)
  502. std::string projectPath = cmSystemTools::GetFilenamePath(filename);
  503. CreateProjectSourceEntries(cFiles, otherFiles, &xml, projectPath, mf,
  504. projectType, targetName);
  505. xml.EndElement(); // CodeLite_Project
  506. }
  507. std::string cmExtraCodeLiteGenerator::GetCodeLiteCompilerName(
  508. const cmMakefile* mf) const
  509. {
  510. // figure out which language to use
  511. // for now care only for C and C++
  512. std::string compilerIdVar = "CMAKE_CXX_COMPILER_ID";
  513. if (!this->GlobalGenerator->GetLanguageEnabled("CXX")) {
  514. compilerIdVar = "CMAKE_C_COMPILER_ID";
  515. }
  516. std::string compilerId = mf->GetSafeDefinition(compilerIdVar);
  517. std::string compiler = "gnu g++"; // default to g++
  518. // Since we need the compiler for parsing purposes only
  519. // it does not matter if we use clang or clang++, same as
  520. // "gnu gcc" vs "gnu g++"
  521. if (compilerId == "MSVC") {
  522. compiler = "VC++";
  523. } else if (compilerId == "Clang") {
  524. compiler = "clang++";
  525. } else if (compilerId == "GNU") {
  526. compiler = "gnu g++";
  527. }
  528. return compiler;
  529. }
  530. std::string cmExtraCodeLiteGenerator::GetConfigurationName(
  531. const cmMakefile* mf) const
  532. {
  533. std::string confName = mf->GetSafeDefinition("CMAKE_BUILD_TYPE");
  534. // Trim the configuration name from whitespaces (left and right)
  535. confName.erase(0, confName.find_first_not_of(" \t\r\v\n"));
  536. confName.erase(confName.find_last_not_of(" \t\r\v\n") + 1);
  537. if (confName.empty()) {
  538. confName = "NoConfig";
  539. }
  540. return confName;
  541. }
  542. std::string cmExtraCodeLiteGenerator::GetBuildCommand(
  543. const cmMakefile* mf, const std::string& targetName) const
  544. {
  545. std::string generator = mf->GetSafeDefinition("CMAKE_GENERATOR");
  546. std::string make = mf->GetRequiredDefinition("CMAKE_MAKE_PROGRAM");
  547. std::string buildCommand = make; // Default
  548. std::ostringstream ss;
  549. if (generator == "NMake Makefiles" || generator == "Ninja") {
  550. ss << make;
  551. } else if (generator == "MinGW Makefiles" || generator == "Unix Makefiles") {
  552. ss << make << " -j " << this->CpuCount;
  553. }
  554. if (!targetName.empty()) {
  555. ss << " " << targetName;
  556. }
  557. buildCommand = ss.str();
  558. return buildCommand;
  559. }
  560. std::string cmExtraCodeLiteGenerator::GetCleanCommand(
  561. const cmMakefile* mf, const std::string& targetName) const
  562. {
  563. std::string generator = mf->GetSafeDefinition("CMAKE_GENERATOR");
  564. std::ostringstream ss;
  565. std::string buildcommand = GetBuildCommand(mf, "");
  566. if (!targetName.empty() && generator == "Ninja") {
  567. ss << buildcommand << " -t clean " << targetName;
  568. } else {
  569. ss << buildcommand << " clean";
  570. }
  571. return ss.str();
  572. }
  573. std::string cmExtraCodeLiteGenerator::GetRebuildCommand(
  574. const cmMakefile* mf, const std::string& targetName) const
  575. {
  576. return GetCleanCommand(mf, targetName) + " && " +
  577. GetBuildCommand(mf, targetName);
  578. }
  579. std::string cmExtraCodeLiteGenerator::GetSingleFileBuildCommand(
  580. const cmMakefile* mf) const
  581. {
  582. std::string buildCommand;
  583. std::string make = mf->GetRequiredDefinition("CMAKE_MAKE_PROGRAM");
  584. std::string generator = mf->GetSafeDefinition("CMAKE_GENERATOR");
  585. if (generator == "Unix Makefiles" || generator == "MinGW Makefiles") {
  586. std::ostringstream ss;
  587. #if defined(_WIN32)
  588. ss << make << " -f$(ProjectPath)/Makefile -B $(CurrentFileFullName).obj";
  589. #else
  590. ss << make << " -f$(ProjectPath)/Makefile -B $(CurrentFileFullName).o";
  591. #endif
  592. buildCommand = ss.str();
  593. }
  594. return buildCommand;
  595. }