cmExtraCodeBlocksGenerator.cxx 25 KB

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