cmExtraCodeBlocksGenerator.cxx 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  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 <string.h>
  8. #include <utility>
  9. #include "cmAlgorithms.h"
  10. #include "cmGeneratedFileStream.h"
  11. #include "cmGeneratorTarget.h"
  12. #include "cmGlobalGenerator.h"
  13. #include "cmLocalGenerator.h"
  14. #include "cmMakefile.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()
  31. : cmExternalMakefileProjectGenerator()
  32. {
  33. }
  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 = outputDir + "/";
  68. filename += projectName + ".cbp";
  69. std::string sessionFilename = outputDir + "/";
  70. sessionFilename += 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>& splitted,
  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>& splitted,
  95. std::vector<std::string>::size_type start,
  96. const std::string& fileName)
  97. {
  98. if (start == splitted.size()) {
  99. files.insert(fileName);
  100. return;
  101. }
  102. for (Tree& folder : folders) {
  103. if (folder.path == splitted[start]) {
  104. if (start + 1 < splitted.size()) {
  105. folder.InsertPath(splitted, start + 1, fileName);
  106. return;
  107. }
  108. // last part of splitted
  109. folder.files.insert(fileName);
  110. return;
  111. }
  112. }
  113. // Not found in folders, thus insert
  114. Tree newFolder;
  115. newFolder.path = splitted[start];
  116. if (start + 1 < splitted.size()) {
  117. newFolder.InsertPath(splitted, start + 1, fileName);
  118. folders.push_back(newFolder);
  119. return;
  120. }
  121. // last part of splitted
  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", fsPath + path + "/" + f);
  164. xml.StartElement("Option");
  165. xml.Attribute("virtualFolder",
  166. "CMake Files\\" + virtualFolderPath + path + "\\");
  167. xml.EndElement();
  168. xml.EndElement();
  169. }
  170. for (Tree const& folder : folders) {
  171. folder.BuildUnitImpl(xml, virtualFolderPath + path + "\\",
  172. 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.c_str());
  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. const std::vector<std::string>& files =
  190. lg->GetMakefile()->GetListFiles();
  191. listFiles.insert(listFiles.end(), files.begin(), files.end());
  192. }
  193. // Convert
  194. for (std::string const& listFile : listFiles) {
  195. // don't put cmake's own files into the project (#12110):
  196. if (listFile.find(cmSystemTools::GetCMakeRoot()) == 0) {
  197. continue;
  198. }
  199. const std::string& relative = cmSystemTools::RelativePath(
  200. it.second[0]->GetSourceDirectory(), listFile.c_str());
  201. std::vector<std::string> splitted;
  202. cmSystemTools::SplitPath(relative, splitted, false);
  203. // Split filename from path
  204. std::string fileName = *(splitted.end() - 1);
  205. splitted.erase(splitted.end() - 1, splitted.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 =
  213. cmSystemTools::IsOn(it.second[0]->GetMakefile()->GetSafeDefinition(
  214. "CMAKE_CODEBLOCKS_EXCLUDE_EXTERNAL_FILES"));
  215. if (!splitted.empty() &&
  216. (!excludeExternal || (relative.find("..") == std::string::npos)) &&
  217. relative.find("CMakeFiles") == std::string::npos) {
  218. tree.InsertPath(splitted, 1, fileName);
  219. }
  220. }
  221. }
  222. // figure out the compiler
  223. std::string compiler = this->GetCBCompilerId(mf);
  224. std::string make = mf->GetRequiredDefinition("CMAKE_MAKE_PROGRAM");
  225. const std::string makeArgs =
  226. mf->GetSafeDefinition("CMAKE_CODEBLOCKS_MAKE_ARGUMENTS");
  227. cmXMLWriter xml(fout);
  228. xml.StartDocument();
  229. xml.StartElement("CodeBlocks_project_file");
  230. xml.StartElement("FileVersion");
  231. xml.Attribute("major", 1);
  232. xml.Attribute("minor", 6);
  233. xml.EndElement();
  234. xml.StartElement("Project");
  235. xml.StartElement("Option");
  236. xml.Attribute("title", lgs[0]->GetProjectName());
  237. xml.EndElement();
  238. xml.StartElement("Option");
  239. xml.Attribute("makefile_is_custom", 1);
  240. xml.EndElement();
  241. xml.StartElement("Option");
  242. xml.Attribute("compiler", compiler);
  243. xml.EndElement();
  244. // Now build a virtual tree
  245. tree.BuildVirtualFolder(xml);
  246. xml.StartElement("Build");
  247. this->AppendTarget(xml, "all", nullptr, make.c_str(), lgs[0],
  248. compiler.c_str(), makeArgs);
  249. // add all executable and library targets and some of the GLOBAL
  250. // and UTILITY targets
  251. for (cmLocalGenerator* lg : lgs) {
  252. const std::vector<cmGeneratorTarget*>& targets = lg->GetGeneratorTargets();
  253. for (cmGeneratorTarget* target : targets) {
  254. std::string targetName = target->GetName();
  255. switch (target->GetType()) {
  256. case cmStateEnums::GLOBAL_TARGET: {
  257. // Only add the global targets from CMAKE_BINARY_DIR,
  258. // not from the subdirs
  259. if (strcmp(lg->GetCurrentBinaryDirectory(),
  260. lg->GetBinaryDirectory()) == 0) {
  261. this->AppendTarget(xml, targetName, nullptr, make.c_str(), lg,
  262. compiler.c_str(), makeArgs);
  263. }
  264. } break;
  265. case cmStateEnums::UTILITY:
  266. // Add all utility targets, except the Nightly/Continuous/
  267. // Experimental-"sub"targets as e.g. NightlyStart
  268. if (((targetName.find("Nightly") == 0) &&
  269. (targetName != "Nightly")) ||
  270. ((targetName.find("Continuous") == 0) &&
  271. (targetName != "Continuous")) ||
  272. ((targetName.find("Experimental") == 0) &&
  273. (targetName != "Experimental"))) {
  274. break;
  275. }
  276. this->AppendTarget(xml, targetName, nullptr, make.c_str(), lg,
  277. compiler.c_str(), makeArgs);
  278. break;
  279. case cmStateEnums::EXECUTABLE:
  280. case cmStateEnums::STATIC_LIBRARY:
  281. case cmStateEnums::SHARED_LIBRARY:
  282. case cmStateEnums::MODULE_LIBRARY:
  283. case cmStateEnums::OBJECT_LIBRARY: {
  284. cmGeneratorTarget* gt = target;
  285. this->AppendTarget(xml, targetName, gt, make.c_str(), lg,
  286. compiler.c_str(), makeArgs);
  287. std::string fastTarget = targetName;
  288. fastTarget += "/fast";
  289. this->AppendTarget(xml, fastTarget, gt, make.c_str(), lg,
  290. compiler.c_str(), makeArgs);
  291. } break;
  292. default:
  293. break;
  294. }
  295. }
  296. }
  297. xml.EndElement(); // Build
  298. // Collect all used source files in the project.
  299. // Keep a list of C/C++ source files which might have an acompanying header
  300. // that should be looked for.
  301. typedef std::map<std::string, CbpUnit> all_files_map_t;
  302. all_files_map_t allFiles;
  303. std::vector<std::string> cFiles;
  304. std::vector<std::string> const& srcExts =
  305. this->GlobalGenerator->GetCMakeInstance()->GetSourceExtensions();
  306. for (cmLocalGenerator* lg : lgs) {
  307. cmMakefile* makefile = lg->GetMakefile();
  308. const std::vector<cmGeneratorTarget*>& targets = lg->GetGeneratorTargets();
  309. for (cmGeneratorTarget* target : targets) {
  310. switch (target->GetType()) {
  311. case cmStateEnums::EXECUTABLE:
  312. case cmStateEnums::STATIC_LIBRARY:
  313. case cmStateEnums::SHARED_LIBRARY:
  314. case cmStateEnums::MODULE_LIBRARY:
  315. case cmStateEnums::OBJECT_LIBRARY:
  316. case cmStateEnums::UTILITY: // can have sources since 2.6.3
  317. {
  318. std::vector<cmSourceFile*> sources;
  319. cmGeneratorTarget* gt = target;
  320. gt->GetSourceFiles(sources,
  321. makefile->GetSafeDefinition("CMAKE_BUILD_TYPE"));
  322. for (cmSourceFile* s : sources) {
  323. // don't add source files from UTILITY target which have the
  324. // GENERATED property set:
  325. if (gt->GetType() == cmStateEnums::UTILITY &&
  326. s->GetPropertyAsBool("GENERATED")) {
  327. continue;
  328. }
  329. // check whether it is a C/C++ implementation file
  330. bool isCFile = false;
  331. std::string lang = s->GetLanguage();
  332. if (lang == "C" || lang == "CXX") {
  333. std::string const& srcext = s->GetExtension();
  334. for (std::string const& ext : srcExts) {
  335. if (srcext == ext) {
  336. isCFile = true;
  337. break;
  338. }
  339. }
  340. }
  341. std::string const& fullPath = s->GetFullPath();
  342. // Check file position relative to project root dir.
  343. const std::string& relative = cmSystemTools::RelativePath(
  344. (*lg).GetSourceDirectory(), fullPath.c_str());
  345. // Do not add this file if it has ".." in relative path and
  346. // if CMAKE_CODEBLOCKS_EXCLUDE_EXTERNAL_FILES variable is on.
  347. const bool excludeExternal =
  348. cmSystemTools::IsOn((*lg).GetMakefile()->GetSafeDefinition(
  349. "CMAKE_CODEBLOCKS_EXCLUDE_EXTERNAL_FILES"));
  350. if (excludeExternal &&
  351. (relative.find("..") != std::string::npos)) {
  352. continue;
  353. }
  354. if (isCFile) {
  355. cFiles.push_back(fullPath);
  356. }
  357. CbpUnit& cbpUnit = allFiles[fullPath];
  358. cbpUnit.Targets.push_back(target);
  359. }
  360. }
  361. default: // intended fallthrough
  362. break;
  363. }
  364. }
  365. }
  366. std::vector<std::string> const& headerExts =
  367. this->GlobalGenerator->GetCMakeInstance()->GetHeaderExtensions();
  368. // The following loop tries to add header files matching to implementation
  369. // files to the project. It does that by iterating over all
  370. // C/C++ source files,
  371. // replacing the file name extension with ".h" and checks whether such a
  372. // file exists. If it does, it is inserted into the map of files.
  373. // A very similar version of that code exists also in the kdevelop
  374. // project generator.
  375. for (std::string const& fileName : cFiles) {
  376. std::string headerBasename = cmSystemTools::GetFilenamePath(fileName);
  377. headerBasename += "/";
  378. headerBasename += cmSystemTools::GetFilenameWithoutExtension(fileName);
  379. // check if there's a matching header around
  380. for (std::string const& ext : headerExts) {
  381. std::string hname = headerBasename;
  382. hname += ".";
  383. hname += ext;
  384. // if it's already in the set, don't check if it exists on disk
  385. if (allFiles.find(hname) != allFiles.end()) {
  386. break;
  387. }
  388. if (cmSystemTools::FileExists(hname.c_str())) {
  389. allFiles[hname].Targets = allFiles[fileName].Targets;
  390. break;
  391. }
  392. }
  393. }
  394. // insert all source files in the CodeBlocks project
  395. for (auto const& s : allFiles) {
  396. std::string const& unitFilename = s.first;
  397. CbpUnit const& unit = s.second;
  398. xml.StartElement("Unit");
  399. xml.Attribute("filename", unitFilename);
  400. for (cmGeneratorTarget const* tgt : unit.Targets) {
  401. xml.StartElement("Option");
  402. xml.Attribute("target", tgt->GetName());
  403. xml.EndElement();
  404. }
  405. xml.EndElement();
  406. }
  407. // Add CMakeLists.txt
  408. tree.BuildUnit(xml, std::string(mf->GetHomeDirectory()) + "/");
  409. xml.EndElement(); // Project
  410. xml.EndElement(); // CodeBlocks_project_file
  411. xml.EndDocument();
  412. }
  413. // Write a dummy file for OBJECT libraries, so C::B can reference some file
  414. std::string cmExtraCodeBlocksGenerator::CreateDummyTargetFile(
  415. cmLocalGenerator* lg, cmGeneratorTarget* target) const
  416. {
  417. // this file doesn't seem to be used by C::B in custom makefile mode,
  418. // but we generate a unique file for each OBJECT library so in case
  419. // C::B uses it in some way, the targets don't interfere with each other.
  420. std::string filename = lg->GetCurrentBinaryDirectory();
  421. filename += "/";
  422. filename += lg->GetTargetDirectory(target);
  423. filename += "/";
  424. filename += target->GetName();
  425. filename += ".objlib";
  426. cmGeneratedFileStream fout(filename.c_str());
  427. if (fout) {
  428. /* clang-format off */
  429. fout << "# This is a dummy file for the OBJECT library "
  430. << target->GetName()
  431. << " for the CMake CodeBlocks project generator.\n"
  432. << "# Don't edit, this file will be overwritten.\n";
  433. /* clang-format on */
  434. }
  435. return filename;
  436. }
  437. // Generate the xml code for one target.
  438. void cmExtraCodeBlocksGenerator::AppendTarget(
  439. cmXMLWriter& xml, const std::string& targetName, cmGeneratorTarget* target,
  440. const char* make, const cmLocalGenerator* lg, const char* compiler,
  441. const std::string& makeFlags)
  442. {
  443. cmMakefile const* makefile = lg->GetMakefile();
  444. std::string makefileName = lg->GetCurrentBinaryDirectory();
  445. makefileName += "/Makefile";
  446. xml.StartElement("Target");
  447. xml.Attribute("title", targetName);
  448. if (target != nullptr) {
  449. int cbTargetType = this->GetCBTargetType(target);
  450. std::string workingDir = lg->GetCurrentBinaryDirectory();
  451. if (target->GetType() == cmStateEnums::EXECUTABLE) {
  452. // Determine the directory where the executable target is created, and
  453. // set the working directory to this dir.
  454. const char* runtimeOutputDir =
  455. makefile->GetDefinition("CMAKE_RUNTIME_OUTPUT_DIRECTORY");
  456. if (runtimeOutputDir != nullptr) {
  457. workingDir = runtimeOutputDir;
  458. } else {
  459. const char* executableOutputDir =
  460. makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  461. if (executableOutputDir != nullptr) {
  462. workingDir = executableOutputDir;
  463. }
  464. }
  465. }
  466. std::string buildType = makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  467. std::string location;
  468. if (target->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  469. location =
  470. this->CreateDummyTargetFile(const_cast<cmLocalGenerator*>(lg), target);
  471. } else {
  472. location = target->GetLocation(buildType);
  473. }
  474. xml.StartElement("Option");
  475. xml.Attribute("output", location);
  476. xml.Attribute("prefix_auto", 0);
  477. xml.Attribute("extension_auto", 0);
  478. xml.EndElement();
  479. xml.StartElement("Option");
  480. xml.Attribute("working_dir", workingDir);
  481. xml.EndElement();
  482. xml.StartElement("Option");
  483. xml.Attribute("object_output", "./");
  484. xml.EndElement();
  485. xml.StartElement("Option");
  486. xml.Attribute("type", cbTargetType);
  487. xml.EndElement();
  488. xml.StartElement("Option");
  489. xml.Attribute("compiler", compiler);
  490. xml.EndElement();
  491. xml.StartElement("Compiler");
  492. // the compilerdefines for this target
  493. std::vector<std::string> cdefs;
  494. target->GetCompileDefinitions(cdefs, buildType, "C");
  495. // Expand the list.
  496. for (std::string const& d : cdefs) {
  497. xml.StartElement("Add");
  498. xml.Attribute("option", "-D" + d);
  499. xml.EndElement();
  500. }
  501. // the include directories for this target
  502. std::vector<std::string> allIncludeDirs;
  503. std::vector<std::string> includes;
  504. lg->GetIncludeDirectories(includes, target, "C", buildType);
  505. allIncludeDirs.insert(allIncludeDirs.end(), includes.begin(),
  506. includes.end());
  507. std::string systemIncludeDirs = makefile->GetSafeDefinition(
  508. "CMAKE_EXTRA_GENERATOR_CXX_SYSTEM_INCLUDE_DIRS");
  509. if (!systemIncludeDirs.empty()) {
  510. std::vector<std::string> dirs;
  511. cmSystemTools::ExpandListArgument(systemIncludeDirs, dirs);
  512. allIncludeDirs.insert(allIncludeDirs.end(), dirs.begin(), dirs.end());
  513. }
  514. systemIncludeDirs = makefile->GetSafeDefinition(
  515. "CMAKE_EXTRA_GENERATOR_C_SYSTEM_INCLUDE_DIRS");
  516. if (!systemIncludeDirs.empty()) {
  517. std::vector<std::string> dirs;
  518. cmSystemTools::ExpandListArgument(systemIncludeDirs, dirs);
  519. allIncludeDirs.insert(allIncludeDirs.end(), dirs.begin(), dirs.end());
  520. }
  521. std::vector<std::string>::const_iterator end =
  522. cmRemoveDuplicates(allIncludeDirs);
  523. for (std::vector<std::string>::const_iterator i = allIncludeDirs.begin();
  524. i != end; ++i) {
  525. xml.StartElement("Add");
  526. xml.Attribute("directory", *i);
  527. xml.EndElement();
  528. }
  529. xml.EndElement(); // Compiler
  530. } else // e.g. all and the GLOBAL and UTILITY targets
  531. {
  532. xml.StartElement("Option");
  533. xml.Attribute("working_dir", lg->GetCurrentBinaryDirectory());
  534. xml.EndElement();
  535. xml.StartElement("Option");
  536. xml.Attribute("type", 4);
  537. xml.EndElement();
  538. }
  539. xml.StartElement("MakeCommands");
  540. xml.StartElement("Build");
  541. xml.Attribute("command", this->BuildMakeCommand(make, makefileName.c_str(),
  542. targetName, makeFlags));
  543. xml.EndElement();
  544. xml.StartElement("CompileFile");
  545. xml.Attribute("command", this->BuildMakeCommand(make, makefileName.c_str(),
  546. "\"$file\"", makeFlags));
  547. xml.EndElement();
  548. xml.StartElement("Clean");
  549. xml.Attribute("command", this->BuildMakeCommand(make, makefileName.c_str(),
  550. "clean", makeFlags));
  551. xml.EndElement();
  552. xml.StartElement("DistClean");
  553. xml.Attribute("command", this->BuildMakeCommand(make, makefileName.c_str(),
  554. "clean", makeFlags));
  555. xml.EndElement();
  556. xml.EndElement(); // MakeCommands
  557. xml.EndElement(); // Target
  558. }
  559. // Translate the cmake compiler id into the CodeBlocks compiler id
  560. std::string cmExtraCodeBlocksGenerator::GetCBCompilerId(const cmMakefile* mf)
  561. {
  562. // figure out which language to use
  563. // for now care only for C, C++, and Fortran
  564. // projects with C/C++ and Fortran are handled as C/C++ projects
  565. bool pureFortran = false;
  566. std::string compilerIdVar;
  567. if (this->GlobalGenerator->GetLanguageEnabled("CXX")) {
  568. compilerIdVar = "CMAKE_CXX_COMPILER_ID";
  569. } else if (this->GlobalGenerator->GetLanguageEnabled("C")) {
  570. compilerIdVar = "CMAKE_C_COMPILER_ID";
  571. } else if (this->GlobalGenerator->GetLanguageEnabled("Fortran")) {
  572. compilerIdVar = "CMAKE_Fortran_COMPILER_ID";
  573. pureFortran = true;
  574. }
  575. std::string compilerId = mf->GetSafeDefinition(compilerIdVar);
  576. std::string compiler = "gcc"; // default to gcc
  577. if (compilerId == "MSVC") {
  578. if (mf->IsDefinitionSet("MSVC10")) {
  579. compiler = "msvc10";
  580. } else {
  581. compiler = "msvc8";
  582. }
  583. } else if (compilerId == "Borland") {
  584. compiler = "bcc";
  585. } else if (compilerId == "SDCC") {
  586. compiler = "sdcc";
  587. } else if (compilerId == "Intel") {
  588. if (pureFortran && mf->IsDefinitionSet("WIN32")) {
  589. compiler = "ifcwin"; // Intel Fortran for Windows (known by cbFortran)
  590. } else {
  591. compiler = "icc";
  592. }
  593. } else if (compilerId == "Watcom" || compilerId == "OpenWatcom") {
  594. compiler = "ow";
  595. } else if (compilerId == "Clang") {
  596. compiler = "clang";
  597. } else if (compilerId == "PGI") {
  598. if (pureFortran) {
  599. compiler = "pgifortran";
  600. } else {
  601. compiler = "pgi"; // does not exist as default in CodeBlocks 16.01
  602. }
  603. } else if (compilerId == "GNU") {
  604. if (pureFortran) {
  605. compiler = "gfortran";
  606. } else {
  607. compiler = "gcc";
  608. }
  609. }
  610. return compiler;
  611. }
  612. // Translate the cmake target type into the CodeBlocks target type id
  613. int cmExtraCodeBlocksGenerator::GetCBTargetType(cmGeneratorTarget* target)
  614. {
  615. switch (target->GetType()) {
  616. case cmStateEnums::EXECUTABLE:
  617. if ((target->GetPropertyAsBool("WIN32_EXECUTABLE")) ||
  618. (target->GetPropertyAsBool("MACOSX_BUNDLE"))) {
  619. return 0;
  620. }
  621. return 1;
  622. case cmStateEnums::STATIC_LIBRARY:
  623. case cmStateEnums::OBJECT_LIBRARY:
  624. return 2;
  625. case cmStateEnums::SHARED_LIBRARY:
  626. case cmStateEnums::MODULE_LIBRARY:
  627. return 3;
  628. default:
  629. return 4;
  630. }
  631. }
  632. // Create the command line for building the given target using the selected
  633. // make
  634. std::string cmExtraCodeBlocksGenerator::BuildMakeCommand(
  635. const std::string& make, const char* makefile, const std::string& target,
  636. const std::string& makeFlags)
  637. {
  638. std::string command = make;
  639. if (!makeFlags.empty()) {
  640. command += " ";
  641. command += makeFlags;
  642. }
  643. std::string generator = this->GlobalGenerator->GetName();
  644. if (generator == "NMake Makefiles" || generator == "NMake Makefiles JOM") {
  645. // For Windows ConvertToOutputPath already adds quotes when required.
  646. // These need to be escaped, see
  647. // https://gitlab.kitware.com/cmake/cmake/issues/13952
  648. std::string makefileName = cmSystemTools::ConvertToOutputPath(makefile);
  649. command += " /NOLOGO /f ";
  650. command += makefileName;
  651. command += " VERBOSE=1 ";
  652. command += target;
  653. } else if (generator == "MinGW Makefiles") {
  654. // no escaping of spaces in this case, see
  655. // https://gitlab.kitware.com/cmake/cmake/issues/10014
  656. std::string makefileName = makefile;
  657. command += " -f \"";
  658. command += makefileName;
  659. command += "\" ";
  660. command += " VERBOSE=1 ";
  661. command += target;
  662. } else if (generator == "Ninja") {
  663. command += " -v ";
  664. command += target;
  665. } else {
  666. std::string makefileName = cmSystemTools::ConvertToOutputPath(makefile);
  667. command += " -f \"";
  668. command += makefileName;
  669. command += "\" ";
  670. command += " VERBOSE=1 ";
  671. command += target;
  672. }
  673. return command;
  674. }