cmExtraCodeBlocksGenerator.cxx 24 KB

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