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