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