cmExtraCodeLiteGenerator.cxx 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  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. : ConfigName("NoConfig")
  22. {
  23. }
  24. cmExternalMakefileProjectGeneratorFactory*
  25. cmExtraCodeLiteGenerator::GetFactory()
  26. {
  27. static cmExternalMakefileProjectGeneratorSimpleFactory<
  28. cmExtraCodeLiteGenerator>
  29. factory("CodeLite", "Generates CodeLite project files.");
  30. if (factory.GetSupportedGlobalGenerators().empty()) {
  31. #if defined(_WIN32)
  32. factory.AddSupportedGlobalGenerator("MinGW Makefiles");
  33. factory.AddSupportedGlobalGenerator("NMake Makefiles");
  34. #endif
  35. factory.AddSupportedGlobalGenerator("Ninja");
  36. factory.AddSupportedGlobalGenerator("Unix Makefiles");
  37. }
  38. return &factory;
  39. }
  40. void cmExtraCodeLiteGenerator::Generate()
  41. {
  42. // Hold root tree information for creating the workspace
  43. std::string workspaceProjectName;
  44. std::string workspaceOutputDir;
  45. std::string workspaceFileName;
  46. std::string workspaceSourcePath;
  47. const std::map<std::string, std::vector<cmLocalGenerator*>>& projectMap =
  48. this->GlobalGenerator->GetProjectMap();
  49. // loop projects and locate the root project.
  50. // and extract the information for creating the worspace
  51. // root makefile
  52. for (auto const& it : projectMap) {
  53. cmLocalGenerator* lg = it.second[0];
  54. const cmMakefile* mf = lg->GetMakefile();
  55. this->ConfigName = GetConfigurationName(mf);
  56. if (lg->GetCurrentBinaryDirectory() == lg->GetBinaryDirectory()) {
  57. workspaceOutputDir = lg->GetCurrentBinaryDirectory();
  58. workspaceProjectName = lg->GetProjectName();
  59. workspaceSourcePath = lg->GetSourceDirectory();
  60. workspaceFileName = workspaceOutputDir + "/";
  61. workspaceFileName += workspaceProjectName + ".workspace";
  62. this->WorkspacePath = lg->GetCurrentBinaryDirectory();
  63. break;
  64. }
  65. }
  66. cmGeneratedFileStream fout(workspaceFileName);
  67. cmXMLWriter xml(fout);
  68. xml.StartDocument("utf-8");
  69. xml.StartElement("CodeLite_Workspace");
  70. xml.Attribute("Name", workspaceProjectName);
  71. bool const targetsAreProjects =
  72. this->GlobalGenerator->GlobalSettingIsOn("CMAKE_CODELITE_USE_TARGETS");
  73. std::vector<std::string> ProjectNames;
  74. if (targetsAreProjects) {
  75. ProjectNames = CreateProjectsByTarget(&xml);
  76. } else {
  77. ProjectNames = CreateProjectsByProjectMaps(&xml);
  78. }
  79. xml.StartElement("BuildMatrix");
  80. xml.StartElement("WorkspaceConfiguration");
  81. xml.Attribute("Name", this->ConfigName);
  82. xml.Attribute("Selected", "yes");
  83. for (std::string const& it : ProjectNames) {
  84. xml.StartElement("Project");
  85. xml.Attribute("Name", it);
  86. xml.Attribute("ConfigName", this->ConfigName);
  87. xml.EndElement();
  88. }
  89. xml.EndElement(); // WorkspaceConfiguration
  90. xml.EndElement(); // BuildMatrix
  91. xml.EndElement(); // CodeLite_Workspace
  92. }
  93. // Create projects where targets are the projects
  94. std::vector<std::string> cmExtraCodeLiteGenerator::CreateProjectsByTarget(
  95. cmXMLWriter* xml)
  96. {
  97. std::vector<std::string> retval;
  98. // for each target in the workspace create a codelite project
  99. const std::vector<cmLocalGenerator*>& lgs =
  100. this->GlobalGenerator->GetLocalGenerators();
  101. for (cmLocalGenerator* lg : lgs) {
  102. for (cmGeneratorTarget* lt : lg->GetGeneratorTargets()) {
  103. cmStateEnums::TargetType type = lt->GetType();
  104. std::string const& outputDir = lg->GetCurrentBinaryDirectory();
  105. std::string targetName = lt->GetName();
  106. std::string filename = outputDir + "/" + targetName + ".project";
  107. retval.push_back(targetName);
  108. // Make the project file relative to the workspace
  109. std::string relafilename =
  110. cmSystemTools::RelativePath(this->WorkspacePath, filename);
  111. std::string visualname = targetName;
  112. switch (type) {
  113. case cmStateEnums::SHARED_LIBRARY:
  114. case cmStateEnums::STATIC_LIBRARY:
  115. case cmStateEnums::MODULE_LIBRARY:
  116. visualname = "lib" + visualname;
  117. CM_FALLTHROUGH;
  118. case cmStateEnums::EXECUTABLE:
  119. xml->StartElement("Project");
  120. xml->Attribute("Name", visualname);
  121. xml->Attribute("Path", relafilename);
  122. xml->Attribute("Active", "No");
  123. xml->EndElement();
  124. CreateNewProjectFile(lt, filename);
  125. break;
  126. default:
  127. break;
  128. }
  129. }
  130. }
  131. return retval;
  132. }
  133. // The "older way of doing it.
  134. std::vector<std::string> cmExtraCodeLiteGenerator::CreateProjectsByProjectMaps(
  135. cmXMLWriter* xml)
  136. {
  137. std::vector<std::string> retval;
  138. // for each sub project in the workspace create a codelite project
  139. for (auto const& it : this->GlobalGenerator->GetProjectMap()) {
  140. std::string const& outputDir = it.second[0]->GetCurrentBinaryDirectory();
  141. std::string projectName = it.second[0]->GetProjectName();
  142. retval.push_back(projectName);
  143. std::string filename = outputDir + "/" + projectName + ".project";
  144. // Make the project file relative to the workspace
  145. filename = cmSystemTools::RelativePath(this->WorkspacePath, filename);
  146. // create a project file
  147. this->CreateProjectFile(it.second);
  148. xml->StartElement("Project");
  149. xml->Attribute("Name", projectName);
  150. xml->Attribute("Path", filename);
  151. xml->Attribute("Active", "No");
  152. xml->EndElement();
  153. }
  154. return retval;
  155. }
  156. /* create the project file */
  157. void cmExtraCodeLiteGenerator::CreateProjectFile(
  158. const std::vector<cmLocalGenerator*>& lgs)
  159. {
  160. std::string const& outputDir = lgs[0]->GetCurrentBinaryDirectory();
  161. std::string projectName = lgs[0]->GetProjectName();
  162. std::string filename = outputDir + "/";
  163. filename += projectName + ".project";
  164. this->CreateNewProjectFile(lgs, filename);
  165. }
  166. std::string cmExtraCodeLiteGenerator::CollectSourceFiles(
  167. const cmMakefile* makefile, const cmGeneratorTarget* gt,
  168. std::map<std::string, cmSourceFile*>& cFiles,
  169. std::set<std::string>& otherFiles)
  170. {
  171. std::string projectType;
  172. switch (gt->GetType()) {
  173. case cmStateEnums::EXECUTABLE: {
  174. projectType = "Executable";
  175. } break;
  176. case cmStateEnums::STATIC_LIBRARY: {
  177. projectType = "Static Library";
  178. } break;
  179. case cmStateEnums::SHARED_LIBRARY: {
  180. projectType = "Dynamic Library";
  181. } break;
  182. case cmStateEnums::MODULE_LIBRARY: {
  183. projectType = "Dynamic Library";
  184. } break;
  185. default: // intended fallthrough
  186. break;
  187. }
  188. switch (gt->GetType()) {
  189. case cmStateEnums::EXECUTABLE:
  190. case cmStateEnums::STATIC_LIBRARY:
  191. case cmStateEnums::SHARED_LIBRARY:
  192. case cmStateEnums::MODULE_LIBRARY: {
  193. cmake const* cm = makefile->GetCMakeInstance();
  194. std::vector<cmSourceFile*> sources;
  195. gt->GetSourceFiles(sources,
  196. makefile->GetSafeDefinition("CMAKE_BUILD_TYPE"));
  197. for (cmSourceFile* s : sources) {
  198. std::string const& fullPath = s->GetFullPath();
  199. std::string const& extLower =
  200. cmSystemTools::LowerCase(s->GetExtension());
  201. // check whether it is a source or a include file
  202. // then put it accordingly into one of the two containers
  203. if (cm->IsSourceExtension(extLower) || cm->IsCudaExtension(extLower) ||
  204. cm->IsFortranExtension(extLower)) {
  205. cFiles[fullPath] = s;
  206. } else {
  207. otherFiles.insert(fullPath);
  208. }
  209. }
  210. }
  211. default: // intended fallthrough
  212. break;
  213. }
  214. return projectType;
  215. }
  216. void cmExtraCodeLiteGenerator::CreateNewProjectFile(
  217. const std::vector<cmLocalGenerator*>& lgs, const std::string& filename)
  218. {
  219. const cmMakefile* mf = lgs[0]->GetMakefile();
  220. cmGeneratedFileStream fout(filename);
  221. if (!fout) {
  222. return;
  223. }
  224. cmXMLWriter xml(fout);
  225. ////////////////////////////////////
  226. xml.StartDocument("utf-8");
  227. xml.StartElement("CodeLite_Project");
  228. xml.Attribute("Name", lgs[0]->GetProjectName());
  229. xml.Attribute("InternalType", "");
  230. std::string projectType;
  231. // Collect all used source files in the project
  232. // Sort them into two containers, one for C/C++ implementation files
  233. // which may have an accompanying header, one for all other files
  234. std::map<std::string, cmSourceFile*> cFiles;
  235. std::set<std::string> otherFiles;
  236. for (cmLocalGenerator* lg : lgs) {
  237. cmMakefile* makefile = lg->GetMakefile();
  238. const std::vector<cmGeneratorTarget*>& targets = lg->GetGeneratorTargets();
  239. for (cmGeneratorTarget* target : targets) {
  240. projectType = CollectSourceFiles(makefile, target, cFiles, otherFiles);
  241. }
  242. }
  243. // Get the project path ( we need it later to convert files to
  244. // their relative path)
  245. std::string projectPath = cmSystemTools::GetFilenamePath(filename);
  246. CreateProjectSourceEntries(cFiles, otherFiles, &xml, projectPath, mf,
  247. projectType, "");
  248. xml.EndElement(); // CodeLite_Project
  249. }
  250. void cmExtraCodeLiteGenerator::FindMatchingHeaderfiles(
  251. std::map<std::string, cmSourceFile*>& cFiles,
  252. std::set<std::string>& otherFiles)
  253. {
  254. const std::vector<std::string>& headerExts =
  255. this->GlobalGenerator->GetCMakeInstance()->GetHeaderExtensions();
  256. // The following loop tries to add header files matching to implementation
  257. // files to the project. It does that by iterating over all source files,
  258. // replacing the file name extension with ".h" and checks whether such a
  259. // file exists. If it does, it is inserted into the map of files.
  260. // A very similar version of that code exists also in the CodeBlocks
  261. // project generator.
  262. for (auto const& sit : cFiles) {
  263. std::string headerBasename = cmSystemTools::GetFilenamePath(sit.first);
  264. headerBasename += "/";
  265. headerBasename += cmSystemTools::GetFilenameWithoutExtension(sit.first);
  266. // check if there's a matching header around
  267. for (std::string const& ext : headerExts) {
  268. std::string hname = headerBasename;
  269. hname += ".";
  270. hname += ext;
  271. // if it's already in the set, don't check if it exists on disk
  272. std::set<std::string>::const_iterator headerIt = otherFiles.find(hname);
  273. if (headerIt != otherFiles.end()) {
  274. break;
  275. }
  276. if (cmSystemTools::FileExists(hname)) {
  277. otherFiles.insert(hname);
  278. break;
  279. }
  280. }
  281. }
  282. }
  283. void cmExtraCodeLiteGenerator::CreateFoldersAndFiles(
  284. std::set<std::string>& cFiles, cmXMLWriter& xml,
  285. const std::string& projectPath)
  286. {
  287. std::vector<std::string> tmp_path;
  288. std::vector<std::string> components;
  289. size_t numOfEndEl = 0;
  290. for (std::string const& cFile : cFiles) {
  291. std::string frelapath = cmSystemTools::RelativePath(projectPath, cFile);
  292. cmsys::SystemTools::SplitPath(frelapath, components, false);
  293. components.pop_back(); // erase last member -> it is file, not folder
  294. components.erase(components.begin()); // erase "root"
  295. size_t sizeOfSkip = 0;
  296. for (size_t i = 0; i < components.size(); ++i) {
  297. // skip relative path
  298. if (components[i] == ".." || components[i] == ".") {
  299. sizeOfSkip++;
  300. continue;
  301. }
  302. // same folder
  303. if (tmp_path.size() > i - sizeOfSkip &&
  304. tmp_path[i - sizeOfSkip] == components[i]) {
  305. continue;
  306. }
  307. // delete "old" subfolders
  308. if (tmp_path.size() > i - sizeOfSkip) {
  309. numOfEndEl = tmp_path.size() - i + sizeOfSkip;
  310. tmp_path.erase(tmp_path.end() - numOfEndEl, tmp_path.end());
  311. for (; numOfEndEl--;) {
  312. xml.EndElement();
  313. }
  314. }
  315. // add folder
  316. xml.StartElement("VirtualDirectory");
  317. xml.Attribute("Name", components[i]);
  318. tmp_path.push_back(components[i]);
  319. }
  320. // delete "old" subfolders
  321. numOfEndEl = tmp_path.size() - components.size() + sizeOfSkip;
  322. if (numOfEndEl) {
  323. tmp_path.erase(tmp_path.end() - numOfEndEl, tmp_path.end());
  324. for (; numOfEndEl--;) {
  325. xml.EndElement();
  326. }
  327. }
  328. // add file
  329. xml.StartElement("File");
  330. xml.Attribute("Name", frelapath);
  331. xml.EndElement();
  332. }
  333. // end of folders
  334. numOfEndEl = tmp_path.size();
  335. for (; numOfEndEl--;) {
  336. xml.EndElement();
  337. }
  338. }
  339. void cmExtraCodeLiteGenerator::CreateFoldersAndFiles(
  340. std::map<std::string, cmSourceFile*>& cFiles, cmXMLWriter& xml,
  341. const std::string& projectPath)
  342. {
  343. std::set<std::string> s;
  344. for (auto const& it : cFiles) {
  345. s.insert(it.first);
  346. }
  347. this->CreateFoldersAndFiles(s, xml, projectPath);
  348. }
  349. void cmExtraCodeLiteGenerator::CreateProjectSourceEntries(
  350. std::map<std::string, cmSourceFile*>& cFiles,
  351. std::set<std::string>& otherFiles, cmXMLWriter* _xml,
  352. const std::string& projectPath, const cmMakefile* mf,
  353. const std::string& projectType, const std::string& targetName)
  354. {
  355. cmXMLWriter& xml(*_xml);
  356. FindMatchingHeaderfiles(cFiles, otherFiles);
  357. // Create 2 virtual folders: src and include
  358. // and place all the implementation files into the src
  359. // folder, the rest goes to the include folder
  360. xml.StartElement("VirtualDirectory");
  361. xml.Attribute("Name", "src");
  362. // insert all source files in the codelite project
  363. // first the C/C++ implementation files, then all others
  364. this->CreateFoldersAndFiles(cFiles, xml, projectPath);
  365. xml.EndElement(); // VirtualDirectory
  366. xml.StartElement("VirtualDirectory");
  367. xml.Attribute("Name", "include");
  368. this->CreateFoldersAndFiles(otherFiles, xml, projectPath);
  369. xml.EndElement(); // VirtualDirectory
  370. // Get the number of CPUs. We use this information for the make -jN
  371. // command
  372. cmsys::SystemInformation info;
  373. info.RunCPUCheck();
  374. this->CpuCount =
  375. info.GetNumberOfLogicalCPU() * info.GetNumberOfPhysicalCPU();
  376. std::string codeliteCompilerName = this->GetCodeLiteCompilerName(mf);
  377. xml.StartElement("Settings");
  378. xml.Attribute("Type", projectType);
  379. xml.StartElement("Configuration");
  380. xml.Attribute("Name", this->ConfigName);
  381. xml.Attribute("CompilerType", this->GetCodeLiteCompilerName(mf));
  382. xml.Attribute("DebuggerType", "GNU gdb debugger");
  383. xml.Attribute("Type", projectType);
  384. xml.Attribute("BuildCmpWithGlobalSettings", "append");
  385. xml.Attribute("BuildLnkWithGlobalSettings", "append");
  386. xml.Attribute("BuildResWithGlobalSettings", "append");
  387. xml.StartElement("Compiler");
  388. xml.Attribute("Options", "-g");
  389. xml.Attribute("Required", "yes");
  390. xml.Attribute("PreCompiledHeader", "");
  391. xml.StartElement("IncludePath");
  392. xml.Attribute("Value", ".");
  393. xml.EndElement(); // IncludePath
  394. xml.EndElement(); // Compiler
  395. xml.StartElement("Linker");
  396. xml.Attribute("Options", "");
  397. xml.Attribute("Required", "yes");
  398. xml.EndElement(); // Linker
  399. xml.StartElement("ResourceCompiler");
  400. xml.Attribute("Options", "");
  401. xml.Attribute("Required", "no");
  402. xml.EndElement(); // ResourceCompiler
  403. xml.StartElement("General");
  404. std::string outputPath =
  405. mf->GetSafeDefinition("CMAKE_RUNTIME_OUTPUT_DIRECTORY");
  406. if (outputPath.empty()) {
  407. outputPath = mf->GetSafeDefinition("EXECUTABLE_OUTPUT_PATH");
  408. }
  409. std::string relapath;
  410. if (!outputPath.empty()) {
  411. relapath = cmSystemTools::RelativePath(projectPath, 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);
  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 accompanying 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 const& 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. const std::string& generator = mf->GetSafeDefinition("CMAKE_GENERATOR");
  546. const 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 << " -f$(ProjectPath)/Makefile -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. const std::string& make = mf->GetRequiredDefinition("CMAKE_MAKE_PROGRAM");
  584. const 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. }