cmExtraCodeBlocksGenerator.cxx 24 KB

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