cmExtraCodeLiteGenerator.cxx 22 KB

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