cmExtraCodeBlocksGenerator.cxx 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  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 "cmExtraCodeBlocksGenerator.h"
  4. #include <map>
  5. #include <memory>
  6. #include <ostream>
  7. #include <set>
  8. #include <utility>
  9. #include <cmext/algorithm>
  10. #include "cmAlgorithms.h"
  11. #include "cmGeneratedFileStream.h"
  12. #include "cmGeneratorTarget.h"
  13. #include "cmGlobalGenerator.h"
  14. #include "cmList.h"
  15. #include "cmLocalGenerator.h"
  16. #include "cmMakefile.h"
  17. #include "cmRange.h"
  18. #include "cmSourceFile.h"
  19. #include "cmStateTypes.h"
  20. #include "cmStringAlgorithms.h"
  21. #include "cmSystemTools.h"
  22. #include "cmValue.h"
  23. #include "cmXMLWriter.h"
  24. #include "cmake.h"
  25. /* Some useful URLs:
  26. Homepage:
  27. http://www.codeblocks.org
  28. File format docs:
  29. http://wiki.codeblocks.org/index.php?title=File_formats_description
  30. http://wiki.codeblocks.org/index.php?title=Workspace_file
  31. http://wiki.codeblocks.org/index.php?title=Project_file
  32. Discussion:
  33. http://forums.codeblocks.org/index.php/topic,6789.0.html
  34. */
  35. cmExtraCodeBlocksGenerator::cmExtraCodeBlocksGenerator() = default;
  36. cmExternalMakefileProjectGeneratorFactory*
  37. cmExtraCodeBlocksGenerator::GetFactory()
  38. {
  39. static cmExternalMakefileProjectGeneratorSimpleFactory<
  40. cmExtraCodeBlocksGenerator>
  41. factory("CodeBlocks", "Generates CodeBlocks project files (deprecated).");
  42. if (factory.GetSupportedGlobalGenerators().empty()) {
  43. #if defined(_WIN32)
  44. factory.AddSupportedGlobalGenerator("MinGW Makefiles");
  45. factory.AddSupportedGlobalGenerator("NMake Makefiles");
  46. factory.AddSupportedGlobalGenerator("NMake Makefiles JOM");
  47. // disable until somebody actually tests it:
  48. // this->AddSupportedGlobalGenerator("MSYS Makefiles");
  49. #endif
  50. factory.AddSupportedGlobalGenerator("Ninja");
  51. factory.AddSupportedGlobalGenerator("Unix Makefiles");
  52. }
  53. return &factory;
  54. }
  55. void cmExtraCodeBlocksGenerator::Generate()
  56. {
  57. // for each sub project in the project create a codeblocks project
  58. for (auto const& it : this->GlobalGenerator->GetProjectMap()) {
  59. // create a project file
  60. this->CreateProjectFile(it.second);
  61. }
  62. }
  63. /* create the project file */
  64. void cmExtraCodeBlocksGenerator::CreateProjectFile(
  65. const std::vector<cmLocalGenerator*>& lgs)
  66. {
  67. std::string outputDir = lgs[0]->GetCurrentBinaryDirectory();
  68. std::string projectName = lgs[0]->GetProjectName();
  69. std::string filename = cmStrCat(outputDir, '/', projectName, ".cbp");
  70. std::string sessionFilename =
  71. cmStrCat(outputDir, '/', projectName, ".layout");
  72. this->CreateNewProjectFile(lgs, filename);
  73. }
  74. /* Tree is used to create a "Virtual Folder" in CodeBlocks, in which all
  75. CMake files this project depends on will be put. This means additionally
  76. to the "Sources" and "Headers" virtual folders of CodeBlocks, there will
  77. now also be a "CMake Files" virtual folder.
  78. Patch by Daniel Teske <daniel.teske AT nokia.com> (which use C::B project
  79. files in QtCreator).*/
  80. struct Tree
  81. {
  82. std::string path; // only one component of the path
  83. std::vector<Tree> folders;
  84. std::set<std::string> files;
  85. void InsertPath(const std::vector<std::string>& split,
  86. std::vector<std::string>::size_type start,
  87. const std::string& fileName);
  88. void BuildVirtualFolder(cmXMLWriter& xml) const;
  89. void BuildVirtualFolderImpl(std::string& virtualFolders,
  90. const std::string& prefix) const;
  91. void BuildUnit(cmXMLWriter& xml, const std::string& fsPath) const;
  92. void BuildUnitImpl(cmXMLWriter& xml, const std::string& virtualFolderPath,
  93. const std::string& fsPath) const;
  94. };
  95. void Tree::InsertPath(const std::vector<std::string>& split,
  96. std::vector<std::string>::size_type start,
  97. const std::string& fileName)
  98. {
  99. if (start == split.size()) {
  100. this->files.insert(fileName);
  101. return;
  102. }
  103. for (Tree& folder : this->folders) {
  104. if (folder.path == split[start]) {
  105. if (start + 1 < split.size()) {
  106. folder.InsertPath(split, start + 1, fileName);
  107. return;
  108. }
  109. // last part of split
  110. folder.files.insert(fileName);
  111. return;
  112. }
  113. }
  114. // Not found in folders, thus insert
  115. Tree newFolder;
  116. newFolder.path = split[start];
  117. if (start + 1 < split.size()) {
  118. newFolder.InsertPath(split, start + 1, fileName);
  119. this->folders.push_back(newFolder);
  120. return;
  121. }
  122. // last part of split
  123. newFolder.files.insert(fileName);
  124. this->folders.push_back(newFolder);
  125. }
  126. void Tree::BuildVirtualFolder(cmXMLWriter& xml) const
  127. {
  128. xml.StartElement("Option");
  129. std::string virtualFolders = "CMake Files\\;";
  130. for (Tree const& folder : this->folders) {
  131. folder.BuildVirtualFolderImpl(virtualFolders, "");
  132. }
  133. xml.Attribute("virtualFolders", virtualFolders);
  134. xml.EndElement();
  135. }
  136. void Tree::BuildVirtualFolderImpl(std::string& virtualFolders,
  137. const std::string& prefix) const
  138. {
  139. virtualFolders += "CMake Files\\" + prefix + this->path + "\\;";
  140. for (Tree const& folder : this->folders) {
  141. folder.BuildVirtualFolderImpl(virtualFolders, prefix + this->path + "\\");
  142. }
  143. }
  144. void Tree::BuildUnit(cmXMLWriter& xml, const std::string& fsPath) const
  145. {
  146. for (std::string const& f : this->files) {
  147. xml.StartElement("Unit");
  148. xml.Attribute("filename", fsPath + f);
  149. xml.StartElement("Option");
  150. xml.Attribute("virtualFolder", "CMake Files\\");
  151. xml.EndElement();
  152. xml.EndElement();
  153. }
  154. for (Tree const& folder : this->folders) {
  155. folder.BuildUnitImpl(xml, "", fsPath);
  156. }
  157. }
  158. void Tree::BuildUnitImpl(cmXMLWriter& xml,
  159. const std::string& virtualFolderPath,
  160. const std::string& fsPath) const
  161. {
  162. for (std::string const& f : this->files) {
  163. xml.StartElement("Unit");
  164. xml.Attribute("filename", cmStrCat(fsPath, this->path, "/", f));
  165. xml.StartElement("Option");
  166. xml.Attribute(
  167. "virtualFolder",
  168. cmStrCat("CMake Files\\", virtualFolderPath, this->path, "\\"));
  169. xml.EndElement();
  170. xml.EndElement();
  171. }
  172. for (Tree const& folder : this->folders) {
  173. folder.BuildUnitImpl(xml, cmStrCat(virtualFolderPath, this->path, "\\"),
  174. cmStrCat(fsPath, this->path, "/"));
  175. }
  176. }
  177. void cmExtraCodeBlocksGenerator::CreateNewProjectFile(
  178. const std::vector<cmLocalGenerator*>& lgs, const std::string& filename)
  179. {
  180. const cmMakefile* mf = lgs[0]->GetMakefile();
  181. cmGeneratedFileStream fout(filename);
  182. if (!fout) {
  183. return;
  184. }
  185. Tree tree;
  186. // build tree of virtual folders
  187. for (auto const& it : this->GlobalGenerator->GetProjectMap()) {
  188. // Collect all files
  189. std::vector<std::string> listFiles;
  190. for (cmLocalGenerator* lg : it.second) {
  191. cm::append(listFiles, lg->GetMakefile()->GetListFiles());
  192. }
  193. // Convert
  194. for (std::string const& listFile : listFiles) {
  195. // don't put cmake's own files into the project (#12110):
  196. if (cmHasPrefix(listFile, cmSystemTools::GetCMakeRoot())) {
  197. continue;
  198. }
  199. const std::string& relative = cmSystemTools::RelativePath(
  200. it.second[0]->GetSourceDirectory(), listFile);
  201. std::vector<std::string> split;
  202. cmSystemTools::SplitPath(relative, split, false);
  203. // Split filename from path
  204. std::string fileName = *(split.end() - 1);
  205. split.erase(split.end() - 1, split.end());
  206. // We don't want paths with CMakeFiles in them
  207. // or do we?
  208. // In speedcrunch those where purely internal
  209. //
  210. // Also we can disable external (outside the project) files by setting ON
  211. // CMAKE_CODEBLOCKS_EXCLUDE_EXTERNAL_FILES variable.
  212. const bool excludeExternal = it.second[0]->GetMakefile()->IsOn(
  213. "CMAKE_CODEBLOCKS_EXCLUDE_EXTERNAL_FILES");
  214. if (!split.empty() &&
  215. (!excludeExternal || (relative.find("..") == std::string::npos)) &&
  216. relative.find("CMakeFiles") == std::string::npos) {
  217. tree.InsertPath(split, 1, fileName);
  218. }
  219. }
  220. }
  221. // figure out the compiler
  222. std::string compiler = this->GetCBCompilerId(mf);
  223. const std::string& make = mf->GetRequiredDefinition("CMAKE_MAKE_PROGRAM");
  224. const std::string& makeArgs =
  225. mf->GetSafeDefinition("CMAKE_CODEBLOCKS_MAKE_ARGUMENTS");
  226. cmXMLWriter xml(fout);
  227. xml.StartDocument();
  228. xml.StartElement("CodeBlocks_project_file");
  229. xml.StartElement("FileVersion");
  230. xml.Attribute("major", 1);
  231. xml.Attribute("minor", 6);
  232. xml.EndElement();
  233. xml.StartElement("Project");
  234. xml.StartElement("Option");
  235. xml.Attribute("title", lgs[0]->GetProjectName());
  236. xml.EndElement();
  237. xml.StartElement("Option");
  238. xml.Attribute("makefile_is_custom", 1);
  239. xml.EndElement();
  240. xml.StartElement("Option");
  241. xml.Attribute("compiler", compiler);
  242. xml.EndElement();
  243. // Now build a virtual tree
  244. tree.BuildVirtualFolder(xml);
  245. xml.StartElement("Build");
  246. this->AppendTarget(xml, "all", nullptr, make, lgs[0], compiler, makeArgs);
  247. // add all executable and library targets and some of the GLOBAL
  248. // and UTILITY targets
  249. for (cmLocalGenerator* lg : lgs) {
  250. const auto& targets = lg->GetGeneratorTargets();
  251. for (const auto& target : targets) {
  252. std::string targetName = target->GetName();
  253. switch (target->GetType()) {
  254. case cmStateEnums::GLOBAL_TARGET: {
  255. // Only add the global targets from CMAKE_BINARY_DIR,
  256. // not from the subdirs
  257. if (lg->GetCurrentBinaryDirectory() == lg->GetBinaryDirectory()) {
  258. this->AppendTarget(xml, targetName, nullptr, make, lg, compiler,
  259. makeArgs);
  260. }
  261. } break;
  262. case cmStateEnums::UTILITY:
  263. // Add all utility targets, except the Nightly/Continuous/
  264. // Experimental-"sub"targets as e.g. NightlyStart
  265. if ((cmHasLiteralPrefix(targetName, "Nightly") &&
  266. (targetName != "Nightly")) ||
  267. (cmHasLiteralPrefix(targetName, "Continuous") &&
  268. (targetName != "Continuous")) ||
  269. (cmHasLiteralPrefix(targetName, "Experimental") &&
  270. (targetName != "Experimental"))) {
  271. break;
  272. }
  273. this->AppendTarget(xml, targetName, nullptr, make, lg, compiler,
  274. makeArgs);
  275. break;
  276. case cmStateEnums::EXECUTABLE:
  277. case cmStateEnums::STATIC_LIBRARY:
  278. case cmStateEnums::SHARED_LIBRARY:
  279. case cmStateEnums::MODULE_LIBRARY:
  280. case cmStateEnums::OBJECT_LIBRARY: {
  281. cmGeneratorTarget* gt = target.get();
  282. this->AppendTarget(xml, targetName, gt, make, lg, compiler,
  283. makeArgs);
  284. std::string fastTarget = cmStrCat(targetName, "/fast");
  285. this->AppendTarget(xml, fastTarget, gt, make, lg, compiler,
  286. makeArgs);
  287. } break;
  288. default:
  289. break;
  290. }
  291. }
  292. }
  293. xml.EndElement(); // Build
  294. // Collect all used source files in the project.
  295. // Keep a list of C/C++ source files which might have an accompanying header
  296. // that should be looked for.
  297. using all_files_map_t = std::map<std::string, CbpUnit>;
  298. all_files_map_t allFiles;
  299. std::vector<std::string> cFiles;
  300. auto* cm = this->GlobalGenerator->GetCMakeInstance();
  301. for (cmLocalGenerator* lg : lgs) {
  302. cmMakefile* makefile = lg->GetMakefile();
  303. const auto& targets = lg->GetGeneratorTargets();
  304. for (const auto& target : targets) {
  305. switch (target->GetType()) {
  306. case cmStateEnums::EXECUTABLE:
  307. case cmStateEnums::STATIC_LIBRARY:
  308. case cmStateEnums::SHARED_LIBRARY:
  309. case cmStateEnums::MODULE_LIBRARY:
  310. case cmStateEnums::OBJECT_LIBRARY:
  311. case cmStateEnums::UTILITY: // can have sources since 2.6.3
  312. {
  313. std::vector<cmSourceFile*> sources;
  314. target->GetSourceFiles(
  315. sources, makefile->GetSafeDefinition("CMAKE_BUILD_TYPE"));
  316. for (cmSourceFile* s : sources) {
  317. // don't add source files from UTILITY target which have the
  318. // GENERATED property set:
  319. if (target->GetType() == cmStateEnums::UTILITY &&
  320. s->GetIsGenerated()) {
  321. continue;
  322. }
  323. // check whether it is a C/C++/CUDA/HIP implementation file
  324. bool isCFile = false;
  325. std::string lang = s->GetOrDetermineLanguage();
  326. if (lang == "C" || lang == "CXX" || lang == "CUDA" ||
  327. lang == "HIP") {
  328. std::string const& srcext = s->GetExtension();
  329. isCFile = cm->IsACLikeSourceExtension(srcext);
  330. }
  331. std::string const& fullPath = s->ResolveFullPath();
  332. // Check file position relative to project root dir.
  333. const std::string relative =
  334. cmSystemTools::RelativePath(lg->GetSourceDirectory(), fullPath);
  335. // Do not add this file if it has ".." in relative path and
  336. // if CMAKE_CODEBLOCKS_EXCLUDE_EXTERNAL_FILES variable is on.
  337. const bool excludeExternal = lg->GetMakefile()->IsOn(
  338. "CMAKE_CODEBLOCKS_EXCLUDE_EXTERNAL_FILES");
  339. if (excludeExternal &&
  340. (relative.find("..") != std::string::npos)) {
  341. continue;
  342. }
  343. if (isCFile) {
  344. cFiles.push_back(fullPath);
  345. }
  346. CbpUnit& cbpUnit = allFiles[fullPath];
  347. cbpUnit.Targets.push_back(target.get());
  348. }
  349. } break;
  350. default:
  351. break;
  352. }
  353. }
  354. }
  355. std::vector<std::string> const& headerExts =
  356. this->GlobalGenerator->GetCMakeInstance()->GetHeaderExtensions();
  357. // The following loop tries to add header files matching to implementation
  358. // files to the project. It does that by iterating over all
  359. // C/C++ source files,
  360. // replacing the file name extension with ".h" and checks whether such a
  361. // file exists. If it does, it is inserted into the map of files.
  362. // A very similar version of that code exists also in the CodeLite
  363. // project generator.
  364. for (std::string const& fileName : cFiles) {
  365. std::string headerBasename =
  366. cmStrCat(cmSystemTools::GetFilenamePath(fileName), '/',
  367. cmSystemTools::GetFilenameWithoutExtension(fileName));
  368. // check if there's a matching header around
  369. for (std::string const& ext : headerExts) {
  370. std::string hname = cmStrCat(headerBasename, '.', ext);
  371. // if it's already in the set, don't check if it exists on disk
  372. if (allFiles.find(hname) != allFiles.end()) {
  373. break;
  374. }
  375. if (cmSystemTools::FileExists(hname)) {
  376. allFiles[hname].Targets = allFiles[fileName].Targets;
  377. break;
  378. }
  379. }
  380. }
  381. // insert all source files in the CodeBlocks project
  382. for (auto const& s : allFiles) {
  383. std::string const& unitFilename = s.first;
  384. CbpUnit const& unit = s.second;
  385. xml.StartElement("Unit");
  386. xml.Attribute("filename", unitFilename);
  387. for (cmGeneratorTarget const* tgt : unit.Targets) {
  388. xml.StartElement("Option");
  389. xml.Attribute("target", tgt->GetName());
  390. xml.EndElement();
  391. }
  392. xml.EndElement();
  393. }
  394. // Add CMakeLists.txt
  395. tree.BuildUnit(xml, mf->GetHomeDirectory() + "/");
  396. xml.EndElement(); // Project
  397. xml.EndElement(); // CodeBlocks_project_file
  398. xml.EndDocument();
  399. }
  400. // Write a dummy file for OBJECT libraries, so C::B can reference some file
  401. std::string cmExtraCodeBlocksGenerator::CreateDummyTargetFile(
  402. cmLocalGenerator* lg, cmGeneratorTarget* target) const
  403. {
  404. // this file doesn't seem to be used by C::B in custom makefile mode,
  405. // but we generate a unique file for each OBJECT library so in case
  406. // C::B uses it in some way, the targets don't interfere with each other.
  407. std::string filename = cmStrCat(lg->GetCurrentBinaryDirectory(), '/',
  408. lg->GetTargetDirectory(target), '/',
  409. target->GetName(), ".objlib");
  410. cmGeneratedFileStream fout(filename);
  411. if (fout) {
  412. /* clang-format off */
  413. fout << "# This is a dummy file for the OBJECT library "
  414. << target->GetName()
  415. << " for the CMake CodeBlocks project generator.\n"
  416. << "# Don't edit, this file will be overwritten.\n";
  417. /* clang-format on */
  418. }
  419. return filename;
  420. }
  421. // Generate the xml code for one target.
  422. void cmExtraCodeBlocksGenerator::AppendTarget(
  423. cmXMLWriter& xml, const std::string& targetName, cmGeneratorTarget* target,
  424. const std::string& make, const cmLocalGenerator* lg,
  425. const std::string& compiler, const std::string& makeFlags)
  426. {
  427. cmMakefile const* makefile = lg->GetMakefile();
  428. std::string makefileName =
  429. cmStrCat(lg->GetCurrentBinaryDirectory(), "/Makefile");
  430. xml.StartElement("Target");
  431. xml.Attribute("title", targetName);
  432. if (target != nullptr) {
  433. int cbTargetType = this->GetCBTargetType(target);
  434. std::string workingDir = lg->GetCurrentBinaryDirectory();
  435. if (target->GetType() == cmStateEnums::EXECUTABLE) {
  436. // Determine the directory where the executable target is created, and
  437. // set the working directory to this dir.
  438. cmValue runtimeOutputDir =
  439. makefile->GetDefinition("CMAKE_RUNTIME_OUTPUT_DIRECTORY");
  440. if (runtimeOutputDir) {
  441. workingDir = *runtimeOutputDir;
  442. } else {
  443. cmValue executableOutputDir =
  444. makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  445. if (executableOutputDir) {
  446. workingDir = *executableOutputDir;
  447. }
  448. }
  449. }
  450. std::string buildType = makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  451. std::string location;
  452. if (target->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  453. location =
  454. this->CreateDummyTargetFile(const_cast<cmLocalGenerator*>(lg), target);
  455. } else {
  456. location = target->GetLocation(buildType);
  457. }
  458. xml.StartElement("Option");
  459. xml.Attribute("output", location);
  460. xml.Attribute("prefix_auto", 0);
  461. xml.Attribute("extension_auto", 0);
  462. xml.EndElement();
  463. xml.StartElement("Option");
  464. xml.Attribute("working_dir", workingDir);
  465. xml.EndElement();
  466. xml.StartElement("Option");
  467. xml.Attribute("object_output", "./");
  468. xml.EndElement();
  469. xml.StartElement("Option");
  470. xml.Attribute("type", cbTargetType);
  471. xml.EndElement();
  472. xml.StartElement("Option");
  473. xml.Attribute("compiler", compiler);
  474. xml.EndElement();
  475. xml.StartElement("Compiler");
  476. // the compilerdefines for this target
  477. std::vector<std::string> cdefs;
  478. target->GetCompileDefinitions(cdefs, buildType, "C");
  479. // Expand the list.
  480. for (std::string const& d : cdefs) {
  481. xml.StartElement("Add");
  482. xml.Attribute("option", "-D" + d);
  483. xml.EndElement();
  484. }
  485. // the include directories for this target
  486. std::vector<std::string> allIncludeDirs;
  487. {
  488. std::vector<std::string> includes;
  489. lg->GetIncludeDirectories(includes, target, "C", buildType);
  490. cm::append(allIncludeDirs, includes);
  491. }
  492. std::string systemIncludeDirs = makefile->GetSafeDefinition(
  493. "CMAKE_EXTRA_GENERATOR_CXX_SYSTEM_INCLUDE_DIRS");
  494. if (!systemIncludeDirs.empty()) {
  495. cm::append(allIncludeDirs, cmList{ systemIncludeDirs });
  496. }
  497. systemIncludeDirs = makefile->GetSafeDefinition(
  498. "CMAKE_EXTRA_GENERATOR_C_SYSTEM_INCLUDE_DIRS");
  499. if (!systemIncludeDirs.empty()) {
  500. cm::append(allIncludeDirs, cmList{ systemIncludeDirs });
  501. }
  502. auto end = cmRemoveDuplicates(allIncludeDirs);
  503. for (std::string const& str : cmMakeRange(allIncludeDirs.cbegin(), end)) {
  504. xml.StartElement("Add");
  505. xml.Attribute("directory", str);
  506. xml.EndElement();
  507. }
  508. xml.EndElement(); // Compiler
  509. } else // e.g. all and the GLOBAL and UTILITY targets
  510. {
  511. xml.StartElement("Option");
  512. xml.Attribute("working_dir", lg->GetCurrentBinaryDirectory());
  513. xml.EndElement();
  514. xml.StartElement("Option");
  515. xml.Attribute("type", 4);
  516. xml.EndElement();
  517. }
  518. xml.StartElement("MakeCommands");
  519. xml.StartElement("Build");
  520. xml.Attribute(
  521. "command",
  522. this->BuildMakeCommand(make, makefileName, targetName, makeFlags));
  523. xml.EndElement();
  524. xml.StartElement("CompileFile");
  525. xml.Attribute(
  526. "command",
  527. this->BuildMakeCommand(make, makefileName, "\"$file\"", makeFlags));
  528. xml.EndElement();
  529. xml.StartElement("Clean");
  530. xml.Attribute(
  531. "command", this->BuildMakeCommand(make, makefileName, "clean", makeFlags));
  532. xml.EndElement();
  533. xml.StartElement("DistClean");
  534. xml.Attribute(
  535. "command", this->BuildMakeCommand(make, makefileName, "clean", makeFlags));
  536. xml.EndElement();
  537. xml.EndElement(); // MakeCommands
  538. xml.EndElement(); // Target
  539. }
  540. // Translate the cmake compiler id into the CodeBlocks compiler id
  541. std::string cmExtraCodeBlocksGenerator::GetCBCompilerId(const cmMakefile* mf)
  542. {
  543. // allow the user to overwrite the detected compiler
  544. std::string userCompiler =
  545. mf->GetSafeDefinition("CMAKE_CODEBLOCKS_COMPILER_ID");
  546. if (!userCompiler.empty()) {
  547. return userCompiler;
  548. }
  549. // figure out which language to use
  550. // for now care only for C, C++, and Fortran
  551. // projects with C/C++ and Fortran are handled as C/C++ projects
  552. bool pureFortran = false;
  553. std::string compilerIdVar;
  554. if (this->GlobalGenerator->GetLanguageEnabled("CXX")) {
  555. compilerIdVar = "CMAKE_CXX_COMPILER_ID";
  556. } else if (this->GlobalGenerator->GetLanguageEnabled("C")) {
  557. compilerIdVar = "CMAKE_C_COMPILER_ID";
  558. } else if (this->GlobalGenerator->GetLanguageEnabled("Fortran")) {
  559. compilerIdVar = "CMAKE_Fortran_COMPILER_ID";
  560. pureFortran = true;
  561. }
  562. std::string const& compilerId = mf->GetSafeDefinition(compilerIdVar);
  563. std::string compiler = "gcc"; // default to gcc
  564. if (compilerId == "MSVC") {
  565. if (mf->IsDefinitionSet("MSVC10")) {
  566. compiler = "msvc10";
  567. } else {
  568. compiler = "msvc8";
  569. }
  570. } else if (compilerId == "Borland") {
  571. compiler = "bcc";
  572. } else if (compilerId == "SDCC") {
  573. compiler = "sdcc";
  574. } else if (compilerId == "Intel") {
  575. if (pureFortran && mf->IsDefinitionSet("WIN32")) {
  576. compiler = "ifcwin"; // Intel Fortran for Windows (known by cbFortran)
  577. } else {
  578. compiler = "icc";
  579. }
  580. } else if (compilerId == "Watcom" || compilerId == "OpenWatcom") {
  581. compiler = "ow";
  582. } else if (compilerId == "Clang") {
  583. compiler = "clang";
  584. } else if (compilerId == "PGI") {
  585. if (pureFortran) {
  586. compiler = "pgifortran";
  587. } else {
  588. compiler = "pgi"; // does not exist as default in CodeBlocks 16.01
  589. }
  590. } else if (compilerId == "LCC") {
  591. if (pureFortran) {
  592. compiler = "lfortran";
  593. } else {
  594. compiler = "lcc";
  595. }
  596. } else if (compilerId == "GNU") {
  597. if (pureFortran) {
  598. compiler = "gfortran";
  599. } else {
  600. compiler = "gcc";
  601. }
  602. }
  603. return compiler;
  604. }
  605. // Translate the cmake target type into the CodeBlocks target type id
  606. int cmExtraCodeBlocksGenerator::GetCBTargetType(cmGeneratorTarget* target)
  607. {
  608. switch (target->GetType()) {
  609. case cmStateEnums::EXECUTABLE:
  610. if ((target->IsWin32Executable(
  611. target->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE"))) ||
  612. (target->GetPropertyAsBool("MACOSX_BUNDLE"))) {
  613. return 0;
  614. }
  615. return 1;
  616. case cmStateEnums::STATIC_LIBRARY:
  617. case cmStateEnums::OBJECT_LIBRARY:
  618. return 2;
  619. case cmStateEnums::SHARED_LIBRARY:
  620. case cmStateEnums::MODULE_LIBRARY:
  621. return 3;
  622. default:
  623. return 4;
  624. }
  625. }
  626. // Create the command line for building the given target using the selected
  627. // make
  628. std::string cmExtraCodeBlocksGenerator::BuildMakeCommand(
  629. const std::string& make, const std::string& makefile,
  630. const std::string& target, const std::string& makeFlags)
  631. {
  632. std::string command = make;
  633. if (!makeFlags.empty()) {
  634. command += " ";
  635. command += makeFlags;
  636. }
  637. std::string generator = this->GlobalGenerator->GetName();
  638. if (generator == "NMake Makefiles" || generator == "NMake Makefiles JOM") {
  639. // For Windows ConvertToOutputPath already adds quotes when required.
  640. // These need to be escaped, see
  641. // https://gitlab.kitware.com/cmake/cmake/-/issues/13952
  642. std::string makefileName = cmSystemTools::ConvertToOutputPath(makefile);
  643. command += " /NOLOGO /f ";
  644. command += makefileName;
  645. command += " VERBOSE=1 ";
  646. command += target;
  647. } else if (generator == "MinGW Makefiles") {
  648. // no escaping of spaces in this case, see
  649. // https://gitlab.kitware.com/cmake/cmake/-/issues/10014
  650. std::string const& makefileName = makefile;
  651. command += " -f \"";
  652. command += makefileName;
  653. command += "\" ";
  654. command += " VERBOSE=1 ";
  655. command += target;
  656. } else if (generator == "Ninja") {
  657. command += " -v ";
  658. command += target;
  659. } else {
  660. std::string makefileName = cmSystemTools::ConvertToOutputPath(makefile);
  661. command += " -f \"";
  662. command += makefileName;
  663. command += "\" ";
  664. command += " VERBOSE=1 ";
  665. command += target;
  666. }
  667. return command;
  668. }