cmExtraCodeBlocksGenerator.cxx 26 KB

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