cmExtraCodeLiteGenerator.cxx 22 KB

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