cmExtraCodeBlocksGenerator.cxx 25 KB

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