cmExtraCodeBlocksGenerator.cxx 24 KB

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