cmExtraCodeLiteGenerator.cxx 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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 "cmState.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 (std::map<std::string, std::vector<cmLocalGenerator*> >::const_iterator
  55. it = projectMap.begin();
  56. it != projectMap.end(); ++it) {
  57. const cmMakefile* mf = it->second[0]->GetMakefile();
  58. this->ConfigName = GetConfigurationName(mf);
  59. if (strcmp(it->second[0]->GetCurrentBinaryDirectory(),
  60. it->second[0]->GetBinaryDirectory()) == 0) {
  61. workspaceOutputDir = it->second[0]->GetCurrentBinaryDirectory();
  62. workspaceProjectName = it->second[0]->GetProjectName();
  63. workspaceSourcePath = it->second[0]->GetSourceDirectory();
  64. workspaceFileName = workspaceOutputDir + "/";
  65. workspaceFileName += workspaceProjectName + ".workspace";
  66. this->WorkspacePath = it->second[0]->GetCurrentBinaryDirectory();
  67. ;
  68. break;
  69. }
  70. }
  71. cmGeneratedFileStream fout(workspaceFileName.c_str());
  72. cmXMLWriter xml(fout);
  73. xml.StartDocument("utf-8");
  74. xml.StartElement("CodeLite_Workspace");
  75. xml.Attribute("Name", workspaceProjectName);
  76. bool const targetsAreProjects =
  77. this->GlobalGenerator->GlobalSettingIsOn("CMAKE_CODELITE_USE_TARGETS");
  78. std::vector<std::string> ProjectNames;
  79. if (targetsAreProjects) {
  80. ProjectNames = CreateProjectsByTarget(&xml);
  81. } else {
  82. ProjectNames = CreateProjectsByProjectMaps(&xml);
  83. }
  84. xml.StartElement("BuildMatrix");
  85. xml.StartElement("WorkspaceConfiguration");
  86. xml.Attribute("Name", this->ConfigName);
  87. xml.Attribute("Selected", "yes");
  88. for (std::vector<std::string>::iterator it(ProjectNames.begin());
  89. it != ProjectNames.end(); it++) {
  90. xml.StartElement("Project");
  91. xml.Attribute("Name", *it);
  92. xml.Attribute("ConfigName", this->ConfigName);
  93. xml.EndElement();
  94. }
  95. xml.EndElement(); // WorkspaceConfiguration
  96. xml.EndElement(); // BuildMatrix
  97. xml.EndElement(); // CodeLite_Workspace
  98. }
  99. // Create projects where targets are the projects
  100. std::vector<std::string> cmExtraCodeLiteGenerator::CreateProjectsByTarget(
  101. cmXMLWriter* xml)
  102. {
  103. std::vector<std::string> retval;
  104. // for each target in the workspace create a codelite project
  105. const std::vector<cmLocalGenerator*>& lgs =
  106. this->GlobalGenerator->GetLocalGenerators();
  107. for (std::vector<cmLocalGenerator*>::const_iterator lg(lgs.begin());
  108. lg != lgs.end(); lg++) {
  109. for (std::vector<cmGeneratorTarget*>::const_iterator lt =
  110. (*lg)->GetGeneratorTargets().begin();
  111. lt != (*lg)->GetGeneratorTargets().end(); lt++) {
  112. cmState::TargetType type = (*lt)->GetType();
  113. std::string outputDir = (*lg)->GetCurrentBinaryDirectory();
  114. std::string filename = outputDir + "/" + (*lt)->GetName() + ".project";
  115. retval.push_back((*lt)->GetName());
  116. // Make the project file relative to the workspace
  117. std::string relafilename = cmSystemTools::RelativePath(
  118. this->WorkspacePath.c_str(), filename.c_str());
  119. std::string visualname = (*lt)->GetName();
  120. switch (type) {
  121. case cmState::SHARED_LIBRARY:
  122. case cmState::STATIC_LIBRARY:
  123. case cmState::MODULE_LIBRARY:
  124. visualname = "lib" + visualname;
  125. case cmState::EXECUTABLE:
  126. xml->StartElement("Project");
  127. xml->Attribute("Name", visualname);
  128. xml->Attribute("Path", relafilename);
  129. xml->Attribute("Active", "No");
  130. xml->EndElement();
  131. CreateNewProjectFile(*lt, filename);
  132. break;
  133. default:
  134. break;
  135. }
  136. }
  137. }
  138. return retval;
  139. }
  140. // The "older way of doing it.
  141. std::vector<std::string> cmExtraCodeLiteGenerator::CreateProjectsByProjectMaps(
  142. cmXMLWriter* xml)
  143. {
  144. std::vector<std::string> retval;
  145. // for each sub project in the workspace create a codelite project
  146. for (std::map<std::string, std::vector<cmLocalGenerator*> >::const_iterator
  147. it = this->GlobalGenerator->GetProjectMap().begin();
  148. it != this->GlobalGenerator->GetProjectMap().end(); it++) {
  149. std::string outputDir = it->second[0]->GetCurrentBinaryDirectory();
  150. std::string projectName = it->second[0]->GetProjectName();
  151. retval.push_back(projectName);
  152. std::string filename = outputDir + "/" + projectName + ".project";
  153. // Make the project file relative to the workspace
  154. filename = cmSystemTools::RelativePath(this->WorkspacePath.c_str(),
  155. filename.c_str());
  156. // create a project file
  157. this->CreateProjectFile(it->second);
  158. xml->StartElement("Project");
  159. xml->Attribute("Name", projectName);
  160. xml->Attribute("Path", filename);
  161. xml->Attribute("Active", "No");
  162. xml->EndElement();
  163. }
  164. return retval;
  165. }
  166. /* create the project file */
  167. void cmExtraCodeLiteGenerator::CreateProjectFile(
  168. const std::vector<cmLocalGenerator*>& lgs)
  169. {
  170. std::string outputDir = lgs[0]->GetCurrentBinaryDirectory();
  171. std::string projectName = lgs[0]->GetProjectName();
  172. std::string filename = outputDir + "/";
  173. filename += projectName + ".project";
  174. this->CreateNewProjectFile(lgs, filename);
  175. }
  176. std::string cmExtraCodeLiteGenerator::CollectSourceFiles(
  177. const cmMakefile* makefile, const cmGeneratorTarget* gt,
  178. std::map<std::string, cmSourceFile*>& cFiles,
  179. std::set<std::string>& otherFiles)
  180. {
  181. const std::vector<std::string>& srcExts =
  182. this->GlobalGenerator->GetCMakeInstance()->GetSourceExtensions();
  183. std::string projectType;
  184. switch (gt->GetType()) {
  185. case cmState::EXECUTABLE: {
  186. projectType = "Executable";
  187. } break;
  188. case cmState::STATIC_LIBRARY: {
  189. projectType = "Static Library";
  190. } break;
  191. case cmState::SHARED_LIBRARY: {
  192. projectType = "Dynamic Library";
  193. } break;
  194. case cmState::MODULE_LIBRARY: {
  195. projectType = "Dynamic Library";
  196. } break;
  197. default: // intended fallthrough
  198. break;
  199. }
  200. switch (gt->GetType()) {
  201. case cmState::EXECUTABLE:
  202. case cmState::STATIC_LIBRARY:
  203. case cmState::SHARED_LIBRARY:
  204. case cmState::MODULE_LIBRARY: {
  205. std::vector<cmSourceFile*> sources;
  206. gt->GetSourceFiles(sources,
  207. makefile->GetSafeDefinition("CMAKE_BUILD_TYPE"));
  208. for (std::vector<cmSourceFile*>::const_iterator si = sources.begin();
  209. si != sources.end(); si++) {
  210. // check whether it is a C/C++ implementation file
  211. bool isCFile = false;
  212. std::string lang = (*si)->GetLanguage();
  213. if (lang == "C" || lang == "CXX") {
  214. std::string srcext = (*si)->GetExtension();
  215. for (std::vector<std::string>::const_iterator ext = srcExts.begin();
  216. ext != srcExts.end(); ++ext) {
  217. if (srcext == *ext) {
  218. isCFile = true;
  219. break;
  220. }
  221. }
  222. }
  223. // then put it accordingly into one of the two containers
  224. if (isCFile) {
  225. cFiles[(*si)->GetFullPath()] = *si;
  226. } else {
  227. otherFiles.insert((*si)->GetFullPath());
  228. }
  229. }
  230. }
  231. default: // intended fallthrough
  232. break;
  233. }
  234. return projectType;
  235. }
  236. void cmExtraCodeLiteGenerator::CreateNewProjectFile(
  237. const std::vector<cmLocalGenerator*>& lgs, const std::string& filename)
  238. {
  239. const cmMakefile* mf = lgs[0]->GetMakefile();
  240. cmGeneratedFileStream fout(filename.c_str());
  241. if (!fout) {
  242. return;
  243. }
  244. cmXMLWriter xml(fout);
  245. ////////////////////////////////////
  246. xml.StartDocument("utf-8");
  247. xml.StartElement("CodeLite_Project");
  248. xml.Attribute("Name", lgs[0]->GetProjectName());
  249. xml.Attribute("InternalType", "");
  250. std::string projectType;
  251. // Collect all used source files in the project
  252. // Sort them into two containers, one for C/C++ implementation files
  253. // which may have an acompanying header, one for all other files
  254. std::map<std::string, cmSourceFile*> cFiles;
  255. std::set<std::string> otherFiles;
  256. for (std::vector<cmLocalGenerator*>::const_iterator lg = lgs.begin();
  257. lg != lgs.end(); lg++) {
  258. cmMakefile* makefile = (*lg)->GetMakefile();
  259. std::vector<cmGeneratorTarget*> targets = (*lg)->GetGeneratorTargets();
  260. for (std::vector<cmGeneratorTarget*>::iterator ti = targets.begin();
  261. ti != targets.end(); ti++) {
  262. projectType = CollectSourceFiles(makefile, *ti, cFiles, otherFiles);
  263. }
  264. }
  265. // Get the project path ( we need it later to convert files to
  266. // their relative path)
  267. std::string projectPath = cmSystemTools::GetFilenamePath(filename);
  268. CreateProjectSourceEntries(cFiles, otherFiles, &xml, projectPath, mf,
  269. projectType);
  270. xml.EndElement(); // CodeLite_Project
  271. }
  272. void cmExtraCodeLiteGenerator::FindMatchingHeaderfiles(
  273. std::map<std::string, cmSourceFile*>& cFiles,
  274. std::set<std::string>& otherFiles)
  275. {
  276. const std::vector<std::string>& headerExts =
  277. this->GlobalGenerator->GetCMakeInstance()->GetHeaderExtensions();
  278. // The following loop tries to add header files matching to implementation
  279. // files to the project. It does that by iterating over all source files,
  280. // replacing the file name extension with ".h" and checks whether such a
  281. // file exists. If it does, it is inserted into the map of files.
  282. // A very similar version of that code exists also in the kdevelop
  283. // project generator.
  284. for (std::map<std::string, cmSourceFile*>::const_iterator sit =
  285. cFiles.begin();
  286. sit != cFiles.end(); ++sit) {
  287. std::string headerBasename = cmSystemTools::GetFilenamePath(sit->first);
  288. headerBasename += "/";
  289. headerBasename += cmSystemTools::GetFilenameWithoutExtension(sit->first);
  290. // check if there's a matching header around
  291. for (std::vector<std::string>::const_iterator ext = headerExts.begin();
  292. ext != headerExts.end(); ++ext) {
  293. std::string hname = headerBasename;
  294. hname += ".";
  295. hname += *ext;
  296. // if it's already in the set, don't check if it exists on disk
  297. std::set<std::string>::const_iterator headerIt = otherFiles.find(hname);
  298. if (headerIt != otherFiles.end()) {
  299. break;
  300. }
  301. if (cmSystemTools::FileExists(hname.c_str())) {
  302. otherFiles.insert(hname);
  303. break;
  304. }
  305. }
  306. }
  307. }
  308. void cmExtraCodeLiteGenerator::CreateProjectSourceEntries(
  309. std::map<std::string, cmSourceFile*>& cFiles,
  310. std::set<std::string>& otherFiles, cmXMLWriter* _xml,
  311. const std::string& projectPath, const cmMakefile* mf,
  312. const std::string& projectType)
  313. {
  314. cmXMLWriter& xml(*_xml);
  315. FindMatchingHeaderfiles(cFiles, otherFiles);
  316. // Create 2 virtual folders: src and include
  317. // and place all the implementation files into the src
  318. // folder, the rest goes to the include folder
  319. xml.StartElement("VirtualDirectory");
  320. xml.Attribute("Name", "src");
  321. // insert all source files in the codelite project
  322. // first the C/C++ implementation files, then all others
  323. for (std::map<std::string, cmSourceFile*>::const_iterator sit =
  324. cFiles.begin();
  325. sit != cFiles.end(); ++sit) {
  326. xml.StartElement("File");
  327. std::string fpath(sit->first);
  328. std::string frelapath =
  329. cmSystemTools::RelativePath(projectPath.c_str(), sit->first.c_str());
  330. xml.Attribute("Name", frelapath);
  331. xml.EndElement();
  332. }
  333. xml.EndElement(); // VirtualDirectory
  334. xml.StartElement("VirtualDirectory");
  335. xml.Attribute("Name", "include");
  336. for (std::set<std::string>::const_iterator sit = otherFiles.begin();
  337. sit != otherFiles.end(); ++sit) {
  338. xml.StartElement("File");
  339. xml.Attribute(
  340. "Name", cmSystemTools::RelativePath(projectPath.c_str(), sit->c_str()));
  341. xml.EndElement();
  342. }
  343. xml.EndElement(); // VirtualDirectory
  344. // Get the number of CPUs. We use this information for the make -jN
  345. // command
  346. cmsys::SystemInformation info;
  347. info.RunCPUCheck();
  348. this->CpuCount =
  349. info.GetNumberOfLogicalCPU() * info.GetNumberOfPhysicalCPU();
  350. std::string codeliteCompilerName = this->GetCodeLiteCompilerName(mf);
  351. xml.StartElement("Settings");
  352. xml.Attribute("Type", projectType);
  353. xml.StartElement("Configuration");
  354. xml.Attribute("Name", this->ConfigName);
  355. xml.Attribute("CompilerType", this->GetCodeLiteCompilerName(mf));
  356. xml.Attribute("DebuggerType", "GNU gdb debugger");
  357. xml.Attribute("Type", projectType);
  358. xml.Attribute("BuildCmpWithGlobalSettings", "append");
  359. xml.Attribute("BuildLnkWithGlobalSettings", "append");
  360. xml.Attribute("BuildResWithGlobalSettings", "append");
  361. xml.StartElement("Compiler");
  362. xml.Attribute("Options", "-g");
  363. xml.Attribute("Required", "yes");
  364. xml.Attribute("PreCompiledHeader", "");
  365. xml.StartElement("IncludePath");
  366. xml.Attribute("Value", ".");
  367. xml.EndElement(); // IncludePath
  368. xml.EndElement(); // Compiler
  369. xml.StartElement("Linker");
  370. xml.Attribute("Options", "");
  371. xml.Attribute("Required", "yes");
  372. xml.EndElement(); // Linker
  373. xml.StartElement("ResourceCompiler");
  374. xml.Attribute("Options", "");
  375. xml.Attribute("Required", "no");
  376. xml.EndElement(); // ResourceCompiler
  377. xml.StartElement("General");
  378. std::string outputPath = mf->GetSafeDefinition("EXECUTABLE_OUTPUT_PATH");
  379. if (!outputPath.empty())
  380. xml.Attribute("OutputFile", outputPath + "/$(ProjectName)");
  381. else
  382. xml.Attribute("OutputFile", "$(IntermediateDirectory)/$(ProjectName)");
  383. xml.Attribute("IntermediateDirectory", "./");
  384. xml.Attribute("Command", "./$(ProjectName)");
  385. xml.Attribute("CommandArguments", "");
  386. if (!outputPath.empty())
  387. xml.Attribute("WorkingDirectory", outputPath);
  388. else
  389. xml.Attribute("WorkingDirectory", "$(IntermediateDirectory)");
  390. xml.Attribute("PauseExecWhenProcTerminates", "yes");
  391. xml.EndElement(); // General
  392. xml.StartElement("Debugger");
  393. xml.Attribute("IsRemote", "no");
  394. xml.Attribute("RemoteHostName", "");
  395. xml.Attribute("RemoteHostPort", "");
  396. xml.Attribute("DebuggerPath", "");
  397. xml.Element("PostConnectCommands");
  398. xml.Element("StartupCommands");
  399. xml.EndElement(); // Debugger
  400. xml.Element("PreBuild");
  401. xml.Element("PostBuild");
  402. xml.StartElement("CustomBuild");
  403. xml.Attribute("Enabled", "yes");
  404. xml.Element("RebuildCommand", GetRebuildCommand(mf));
  405. xml.Element("CleanCommand", GetCleanCommand(mf));
  406. xml.Element("BuildCommand", GetBuildCommand(mf));
  407. xml.Element("SingleFileCommand", GetSingleFileBuildCommand(mf));
  408. xml.Element("PreprocessFileCommand");
  409. xml.Element("WorkingDirectory", "$(WorkspacePath)");
  410. xml.EndElement(); // CustomBuild
  411. xml.StartElement("AdditionalRules");
  412. xml.Element("CustomPostBuild");
  413. xml.Element("CustomPreBuild");
  414. xml.EndElement(); // AdditionalRules
  415. xml.EndElement(); // Configuration
  416. xml.StartElement("GlobalSettings");
  417. xml.StartElement("Compiler");
  418. xml.Attribute("Options", "");
  419. xml.StartElement("IncludePath");
  420. xml.Attribute("Value", ".");
  421. xml.EndElement(); // IncludePath
  422. xml.EndElement(); // Compiler
  423. xml.StartElement("Linker");
  424. xml.Attribute("Options", "");
  425. xml.StartElement("LibraryPath");
  426. xml.Attribute("Value", ".");
  427. xml.EndElement(); // LibraryPath
  428. xml.EndElement(); // Linker
  429. xml.StartElement("ResourceCompiler");
  430. xml.Attribute("Options", "");
  431. xml.EndElement(); // ResourceCompiler
  432. xml.EndElement(); // GlobalSettings
  433. xml.EndElement(); // Settings
  434. }
  435. void cmExtraCodeLiteGenerator::CreateNewProjectFile(
  436. const cmGeneratorTarget* gt, const std::string& filename)
  437. {
  438. const cmMakefile* mf = gt->Makefile;
  439. cmGeneratedFileStream fout(filename.c_str());
  440. if (!fout) {
  441. return;
  442. }
  443. cmXMLWriter xml(fout);
  444. ////////////////////////////////////
  445. xml.StartDocument("utf-8");
  446. xml.StartElement("CodeLite_Project");
  447. std::string visualname = gt->GetName();
  448. switch (gt->GetType()) {
  449. case cmState::STATIC_LIBRARY:
  450. case cmState::SHARED_LIBRARY:
  451. case cmState::MODULE_LIBRARY:
  452. visualname = "lib" + visualname;
  453. default: // intended fallthrough
  454. break;
  455. }
  456. xml.Attribute("Name", visualname);
  457. xml.Attribute("InternalType", "");
  458. // Collect all used source files in the project
  459. // Sort them into two containers, one for C/C++ implementation files
  460. // which may have an acompanying header, one for all other files
  461. std::string projectType;
  462. std::vector<std::string> headerExts =
  463. this->GlobalGenerator->GetCMakeInstance()->GetHeaderExtensions();
  464. std::map<std::string, cmSourceFile*> cFiles;
  465. std::set<std::string> otherFiles;
  466. projectType = CollectSourceFiles(mf, gt, cFiles, otherFiles);
  467. // Get the project path ( we need it later to convert files to
  468. // their relative path)
  469. std::string projectPath = cmSystemTools::GetFilenamePath(filename);
  470. CreateProjectSourceEntries(cFiles, otherFiles, &xml, projectPath, mf,
  471. projectType);
  472. xml.EndElement(); // CodeLite_Project
  473. }
  474. std::string cmExtraCodeLiteGenerator::GetCodeLiteCompilerName(
  475. const cmMakefile* mf) const
  476. {
  477. // figure out which language to use
  478. // for now care only for C and C++
  479. std::string compilerIdVar = "CMAKE_CXX_COMPILER_ID";
  480. if (!this->GlobalGenerator->GetLanguageEnabled("CXX")) {
  481. compilerIdVar = "CMAKE_C_COMPILER_ID";
  482. }
  483. std::string compilerId = mf->GetSafeDefinition(compilerIdVar);
  484. std::string compiler = "gnu g++"; // default to g++
  485. // Since we need the compiler for parsing purposes only
  486. // it does not matter if we use clang or clang++, same as
  487. // "gnu gcc" vs "gnu g++"
  488. if (compilerId == "MSVC") {
  489. compiler = "VC++";
  490. } else if (compilerId == "Clang") {
  491. compiler = "clang++";
  492. } else if (compilerId == "GNU") {
  493. compiler = "gnu g++";
  494. }
  495. return compiler;
  496. }
  497. std::string cmExtraCodeLiteGenerator::GetConfigurationName(
  498. const cmMakefile* mf) const
  499. {
  500. std::string confName = mf->GetSafeDefinition("CMAKE_BUILD_TYPE");
  501. // Trim the configuration name from whitespaces (left and right)
  502. confName.erase(0, confName.find_first_not_of(" \t\r\v\n"));
  503. confName.erase(confName.find_last_not_of(" \t\r\v\n") + 1);
  504. if (confName.empty()) {
  505. confName = "NoConfig";
  506. }
  507. return confName;
  508. }
  509. std::string cmExtraCodeLiteGenerator::GetBuildCommand(
  510. const cmMakefile* mf) const
  511. {
  512. std::string generator = mf->GetSafeDefinition("CMAKE_GENERATOR");
  513. std::string make = mf->GetRequiredDefinition("CMAKE_MAKE_PROGRAM");
  514. std::string buildCommand = make; // Default
  515. if (generator == "NMake Makefiles" || generator == "Ninja") {
  516. buildCommand = make;
  517. } else if (generator == "MinGW Makefiles" || generator == "Unix Makefiles") {
  518. std::ostringstream ss;
  519. ss << make << " -j " << this->CpuCount;
  520. buildCommand = ss.str();
  521. }
  522. return buildCommand;
  523. }
  524. std::string cmExtraCodeLiteGenerator::GetCleanCommand(
  525. const cmMakefile* mf) const
  526. {
  527. return GetBuildCommand(mf) + " clean";
  528. }
  529. std::string cmExtraCodeLiteGenerator::GetRebuildCommand(
  530. const cmMakefile* mf) const
  531. {
  532. return GetCleanCommand(mf) + " && " + GetBuildCommand(mf);
  533. }
  534. std::string cmExtraCodeLiteGenerator::GetSingleFileBuildCommand(
  535. const cmMakefile* mf) const
  536. {
  537. std::string buildCommand;
  538. std::string make = mf->GetRequiredDefinition("CMAKE_MAKE_PROGRAM");
  539. std::string generator = mf->GetSafeDefinition("CMAKE_GENERATOR");
  540. if (generator == "Unix Makefiles" || generator == "MinGW Makefiles") {
  541. std::ostringstream ss;
  542. ss << make << " -f$(ProjectPath)/Makefile $(CurrentFileName).cpp.o";
  543. buildCommand = ss.str();
  544. }
  545. return buildCommand;
  546. }