cmExtraCodeBlocksGenerator.cxx 26 KB

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