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