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.");
  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. cmLocalGenerator* lg = it.second[0];
  56. const cmMakefile* mf = lg->GetMakefile();
  57. this->ConfigName = 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 = CreateProjectsByTarget(&xml);
  78. } else {
  79. ProjectNames = 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. 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: // intended fallthrough
  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->IsSourceExtension(extLower) || cm->IsCudaExtension(extLower) ||
  203. cm->IsFortranExtension(extLower)) {
  204. cFiles[fullPath] = s;
  205. } else {
  206. otherFiles.insert(fullPath);
  207. }
  208. }
  209. }
  210. default: // intended fallthrough
  211. break;
  212. }
  213. return projectType;
  214. }
  215. void cmExtraCodeLiteGenerator::CreateNewProjectFile(
  216. const std::vector<cmLocalGenerator*>& lgs, const std::string& filename)
  217. {
  218. const cmMakefile* mf = lgs[0]->GetMakefile();
  219. cmGeneratedFileStream fout(filename);
  220. if (!fout) {
  221. return;
  222. }
  223. cmXMLWriter xml(fout);
  224. ////////////////////////////////////
  225. xml.StartDocument("utf-8");
  226. xml.StartElement("CodeLite_Project");
  227. xml.Attribute("Name", lgs[0]->GetProjectName());
  228. xml.Attribute("InternalType", "");
  229. std::string projectType;
  230. // Collect all used source files in the project
  231. // Sort them into two containers, one for C/C++ implementation files
  232. // which may have an accompanying header, one for all other files
  233. std::map<std::string, cmSourceFile*> cFiles;
  234. std::set<std::string> otherFiles;
  235. for (cmLocalGenerator* lg : lgs) {
  236. cmMakefile* makefile = lg->GetMakefile();
  237. for (const auto& target : lg->GetGeneratorTargets()) {
  238. projectType =
  239. CollectSourceFiles(makefile, target.get(), cFiles, otherFiles);
  240. }
  241. }
  242. // Get the project path ( we need it later to convert files to
  243. // their relative path)
  244. std::string projectPath = cmSystemTools::GetFilenamePath(filename);
  245. CreateProjectSourceEntries(cFiles, otherFiles, &xml, projectPath, mf,
  246. projectType, "");
  247. xml.EndElement(); // CodeLite_Project
  248. }
  249. void cmExtraCodeLiteGenerator::FindMatchingHeaderfiles(
  250. std::map<std::string, cmSourceFile*>& cFiles,
  251. std::set<std::string>& otherFiles)
  252. {
  253. const std::vector<std::string>& headerExts =
  254. this->GlobalGenerator->GetCMakeInstance()->GetHeaderExtensions();
  255. // The following loop tries to add header files matching to implementation
  256. // files to the project. It does that by iterating over all source files,
  257. // replacing the file name extension with ".h" and checks whether such a
  258. // file exists. If it does, it is inserted into the map of files.
  259. // A very similar version of that code exists also in the CodeBlocks
  260. // project generator.
  261. for (auto const& sit : cFiles) {
  262. std::string headerBasename =
  263. cmStrCat(cmSystemTools::GetFilenamePath(sit.first), '/',
  264. cmSystemTools::GetFilenameWithoutExtension(sit.first));
  265. // check if there's a matching header around
  266. for (std::string const& ext : headerExts) {
  267. std::string hname = cmStrCat(headerBasename, '.', ext);
  268. // if it's already in the set, don't check if it exists on disk
  269. auto headerIt = otherFiles.find(hname);
  270. if (headerIt != otherFiles.end()) {
  271. break;
  272. }
  273. if (cmSystemTools::FileExists(hname)) {
  274. otherFiles.insert(hname);
  275. break;
  276. }
  277. }
  278. }
  279. }
  280. void cmExtraCodeLiteGenerator::CreateFoldersAndFiles(
  281. std::set<std::string>& cFiles, cmXMLWriter& xml,
  282. const std::string& projectPath)
  283. {
  284. std::vector<std::string> tmp_path;
  285. std::vector<std::string> components;
  286. size_t numOfEndEl = 0;
  287. for (std::string const& cFile : cFiles) {
  288. std::string frelapath = cmSystemTools::RelativePath(projectPath, cFile);
  289. cmsys::SystemTools::SplitPath(frelapath, components, false);
  290. components.pop_back(); // erase last member -> it is file, not folder
  291. components.erase(components.begin()); // erase "root"
  292. size_t sizeOfSkip = 0;
  293. for (size_t i = 0; i < components.size(); ++i) {
  294. // skip relative path
  295. if (components[i] == ".." || components[i] == ".") {
  296. sizeOfSkip++;
  297. continue;
  298. }
  299. // same folder
  300. if (tmp_path.size() > i - sizeOfSkip &&
  301. tmp_path[i - sizeOfSkip] == components[i]) {
  302. continue;
  303. }
  304. // delete "old" subfolders
  305. if (tmp_path.size() > i - sizeOfSkip) {
  306. numOfEndEl = tmp_path.size() - i + sizeOfSkip;
  307. tmp_path.erase(tmp_path.end() - numOfEndEl, tmp_path.end());
  308. for (; numOfEndEl--;) {
  309. xml.EndElement();
  310. }
  311. }
  312. // add folder
  313. xml.StartElement("VirtualDirectory");
  314. xml.Attribute("Name", components[i]);
  315. tmp_path.push_back(components[i]);
  316. }
  317. // delete "old" subfolders
  318. numOfEndEl = tmp_path.size() - components.size() + sizeOfSkip;
  319. if (numOfEndEl) {
  320. tmp_path.erase(tmp_path.end() - numOfEndEl, tmp_path.end());
  321. for (; numOfEndEl--;) {
  322. xml.EndElement();
  323. }
  324. }
  325. // add file
  326. xml.StartElement("File");
  327. xml.Attribute("Name", frelapath);
  328. xml.EndElement();
  329. }
  330. // end of folders
  331. numOfEndEl = tmp_path.size();
  332. for (; numOfEndEl--;) {
  333. xml.EndElement();
  334. }
  335. }
  336. void cmExtraCodeLiteGenerator::CreateFoldersAndFiles(
  337. std::map<std::string, cmSourceFile*>& cFiles, cmXMLWriter& xml,
  338. const std::string& projectPath)
  339. {
  340. std::set<std::string> s;
  341. for (auto const& it : cFiles) {
  342. s.insert(it.first);
  343. }
  344. this->CreateFoldersAndFiles(s, xml, projectPath);
  345. }
  346. void cmExtraCodeLiteGenerator::CreateProjectSourceEntries(
  347. std::map<std::string, cmSourceFile*>& cFiles,
  348. std::set<std::string>& otherFiles, cmXMLWriter* _xml,
  349. const std::string& projectPath, const cmMakefile* mf,
  350. const std::string& projectType, const std::string& targetName)
  351. {
  352. cmXMLWriter& xml(*_xml);
  353. FindMatchingHeaderfiles(cFiles, otherFiles);
  354. // Create 2 virtual folders: src and include
  355. // and place all the implementation files into the src
  356. // folder, the rest goes to the include folder
  357. xml.StartElement("VirtualDirectory");
  358. xml.Attribute("Name", "src");
  359. // insert all source files in the codelite project
  360. // first the C/C++ implementation files, then all others
  361. this->CreateFoldersAndFiles(cFiles, xml, projectPath);
  362. xml.EndElement(); // VirtualDirectory
  363. xml.StartElement("VirtualDirectory");
  364. xml.Attribute("Name", "include");
  365. this->CreateFoldersAndFiles(otherFiles, xml, projectPath);
  366. xml.EndElement(); // VirtualDirectory
  367. // Get the number of CPUs. We use this information for the make -jN
  368. // command
  369. cmsys::SystemInformation info;
  370. info.RunCPUCheck();
  371. this->CpuCount =
  372. info.GetNumberOfLogicalCPU() * info.GetNumberOfPhysicalCPU();
  373. std::string codeliteCompilerName = this->GetCodeLiteCompilerName(mf);
  374. xml.StartElement("Settings");
  375. xml.Attribute("Type", projectType);
  376. xml.StartElement("Configuration");
  377. xml.Attribute("Name", this->ConfigName);
  378. xml.Attribute("CompilerType", this->GetCodeLiteCompilerName(mf));
  379. xml.Attribute("DebuggerType", "GNU gdb debugger");
  380. xml.Attribute("Type", projectType);
  381. xml.Attribute("BuildCmpWithGlobalSettings", "append");
  382. xml.Attribute("BuildLnkWithGlobalSettings", "append");
  383. xml.Attribute("BuildResWithGlobalSettings", "append");
  384. xml.StartElement("Compiler");
  385. xml.Attribute("Options", "-g");
  386. xml.Attribute("Required", "yes");
  387. xml.Attribute("PreCompiledHeader", "");
  388. xml.StartElement("IncludePath");
  389. xml.Attribute("Value", ".");
  390. xml.EndElement(); // IncludePath
  391. xml.EndElement(); // Compiler
  392. xml.StartElement("Linker");
  393. xml.Attribute("Options", "");
  394. xml.Attribute("Required", "yes");
  395. xml.EndElement(); // Linker
  396. xml.StartElement("ResourceCompiler");
  397. xml.Attribute("Options", "");
  398. xml.Attribute("Required", "no");
  399. xml.EndElement(); // ResourceCompiler
  400. xml.StartElement("General");
  401. std::string outputPath =
  402. mf->GetSafeDefinition("CMAKE_RUNTIME_OUTPUT_DIRECTORY");
  403. if (outputPath.empty()) {
  404. outputPath = mf->GetSafeDefinition("EXECUTABLE_OUTPUT_PATH");
  405. }
  406. std::string relapath;
  407. if (!outputPath.empty()) {
  408. relapath = cmSystemTools::RelativePath(projectPath, outputPath);
  409. xml.Attribute("OutputFile", relapath + "/$(ProjectName)");
  410. } else {
  411. xml.Attribute("OutputFile", "$(IntermediateDirectory)/$(ProjectName)");
  412. }
  413. xml.Attribute("IntermediateDirectory", "./");
  414. xml.Attribute("Command", "./$(ProjectName)");
  415. xml.Attribute("CommandArguments", "");
  416. if (!outputPath.empty()) {
  417. xml.Attribute("WorkingDirectory", relapath);
  418. } else {
  419. xml.Attribute("WorkingDirectory", "$(IntermediateDirectory)");
  420. }
  421. xml.Attribute("PauseExecWhenProcTerminates", "yes");
  422. xml.EndElement(); // General
  423. xml.StartElement("Debugger");
  424. xml.Attribute("IsRemote", "no");
  425. xml.Attribute("RemoteHostName", "");
  426. xml.Attribute("RemoteHostPort", "");
  427. xml.Attribute("DebuggerPath", "");
  428. xml.Element("PostConnectCommands");
  429. xml.Element("StartupCommands");
  430. xml.EndElement(); // Debugger
  431. xml.Element("PreBuild");
  432. xml.Element("PostBuild");
  433. xml.StartElement("CustomBuild");
  434. xml.Attribute("Enabled", "yes");
  435. xml.Element("RebuildCommand", GetRebuildCommand(mf, targetName));
  436. xml.Element("CleanCommand", GetCleanCommand(mf, targetName));
  437. xml.Element("BuildCommand", GetBuildCommand(mf, targetName));
  438. xml.Element("SingleFileCommand", GetSingleFileBuildCommand(mf));
  439. xml.Element("PreprocessFileCommand");
  440. xml.Element("WorkingDirectory", "$(WorkspacePath)");
  441. xml.EndElement(); // CustomBuild
  442. xml.StartElement("AdditionalRules");
  443. xml.Element("CustomPostBuild");
  444. xml.Element("CustomPreBuild");
  445. xml.EndElement(); // AdditionalRules
  446. xml.EndElement(); // Configuration
  447. xml.StartElement("GlobalSettings");
  448. xml.StartElement("Compiler");
  449. xml.Attribute("Options", "");
  450. xml.StartElement("IncludePath");
  451. xml.Attribute("Value", ".");
  452. xml.EndElement(); // IncludePath
  453. xml.EndElement(); // Compiler
  454. xml.StartElement("Linker");
  455. xml.Attribute("Options", "");
  456. xml.StartElement("LibraryPath");
  457. xml.Attribute("Value", ".");
  458. xml.EndElement(); // LibraryPath
  459. xml.EndElement(); // Linker
  460. xml.StartElement("ResourceCompiler");
  461. xml.Attribute("Options", "");
  462. xml.EndElement(); // ResourceCompiler
  463. xml.EndElement(); // GlobalSettings
  464. xml.EndElement(); // Settings
  465. }
  466. void cmExtraCodeLiteGenerator::CreateNewProjectFile(
  467. const cmGeneratorTarget* gt, const std::string& filename)
  468. {
  469. const cmMakefile* mf = gt->Makefile;
  470. cmGeneratedFileStream fout(filename);
  471. if (!fout) {
  472. return;
  473. }
  474. cmXMLWriter xml(fout);
  475. ////////////////////////////////////
  476. xml.StartDocument("utf-8");
  477. xml.StartElement("CodeLite_Project");
  478. std::string targetName = gt->GetName();
  479. std::string visualname = targetName;
  480. switch (gt->GetType()) {
  481. case cmStateEnums::STATIC_LIBRARY:
  482. case cmStateEnums::SHARED_LIBRARY:
  483. case cmStateEnums::MODULE_LIBRARY:
  484. visualname = "lib" + targetName;
  485. default: // intended fallthrough
  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 = 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. 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 = 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 GetCleanCommand(mf, targetName) + " && " +
  577. 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. }