cmExtraCodeBlocksGenerator.cxx 26 KB

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