cmExtraCodeLiteGenerator.cxx 22 KB

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