cmExtraCodeBlocksGenerator.cxx 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  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.empty() &&
  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", CM_NULLPTR, make.c_str(), lgs[0],
  254. compiler.c_str(), 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, CM_NULLPTR, 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, CM_NULLPTR, 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 from UTILITY target which have the
  335. // GENERATED property set:
  336. if (gt->GetType() == cmState::UTILITY &&
  337. (*si)->GetPropertyAsBool("GENERATED")) {
  338. continue;
  339. }
  340. // check whether it is a C/C++ implementation file
  341. bool isCFile = false;
  342. std::string lang = (*si)->GetLanguage();
  343. if (lang == "C" || lang == "CXX") {
  344. std::string srcext = (*si)->GetExtension();
  345. for (std::vector<std::string>::const_iterator ext =
  346. srcExts.begin();
  347. ext != srcExts.end(); ++ext) {
  348. if (srcext == *ext) {
  349. isCFile = true;
  350. break;
  351. }
  352. }
  353. }
  354. std::string fullPath = (*si)->GetFullPath();
  355. if (isCFile) {
  356. cFiles.push_back(fullPath);
  357. }
  358. CbpUnit& cbpUnit = allFiles[fullPath];
  359. cbpUnit.Targets.push_back(*ti);
  360. }
  361. }
  362. default: // intended fallthrough
  363. break;
  364. }
  365. }
  366. }
  367. std::vector<std::string> headerExts =
  368. this->GlobalGenerator->GetCMakeInstance()->GetHeaderExtensions();
  369. // The following loop tries to add header files matching to implementation
  370. // files to the project. It does that by iterating over all
  371. // C/C++ source files,
  372. // replacing the file name extension with ".h" and checks whether such a
  373. // file exists. If it does, it is inserted into the map of files.
  374. // A very similar version of that code exists also in the kdevelop
  375. // project generator.
  376. for (std::vector<std::string>::const_iterator sit = cFiles.begin();
  377. sit != cFiles.end(); ++sit) {
  378. std::string const& fileName = *sit;
  379. std::string headerBasename = cmSystemTools::GetFilenamePath(fileName);
  380. headerBasename += "/";
  381. headerBasename += cmSystemTools::GetFilenameWithoutExtension(fileName);
  382. // check if there's a matching header around
  383. for (std::vector<std::string>::const_iterator ext = headerExts.begin();
  384. ext != headerExts.end(); ++ext) {
  385. std::string hname = headerBasename;
  386. hname += ".";
  387. hname += *ext;
  388. // if it's already in the set, don't check if it exists on disk
  389. if (allFiles.find(hname) != allFiles.end()) {
  390. break;
  391. }
  392. if (cmSystemTools::FileExists(hname.c_str())) {
  393. allFiles[hname].Targets = allFiles[fileName].Targets;
  394. break;
  395. }
  396. }
  397. }
  398. // insert all source files in the CodeBlocks project
  399. for (all_files_map_t::const_iterator sit = allFiles.begin();
  400. sit != allFiles.end(); ++sit) {
  401. std::string const& unitFilename = sit->first;
  402. CbpUnit const& unit = sit->second;
  403. xml.StartElement("Unit");
  404. xml.Attribute("filename", unitFilename);
  405. for (std::vector<const cmGeneratorTarget*>::const_iterator ti =
  406. unit.Targets.begin();
  407. ti != unit.Targets.end(); ++ti) {
  408. xml.StartElement("Option");
  409. xml.Attribute("target", (*ti)->GetName());
  410. xml.EndElement();
  411. }
  412. xml.EndElement();
  413. }
  414. // Add CMakeLists.txt
  415. tree.BuildUnit(xml, std::string(mf->GetHomeDirectory()) + "/");
  416. xml.EndElement(); // Project
  417. xml.EndElement(); // CodeBlocks_project_file
  418. xml.EndDocument();
  419. }
  420. // Write a dummy file for OBJECT libraries, so C::B can reference some file
  421. std::string cmExtraCodeBlocksGenerator::CreateDummyTargetFile(
  422. cmLocalGenerator* lg, cmGeneratorTarget* target) const
  423. {
  424. // this file doesn't seem to be used by C::B in custom makefile mode,
  425. // but we generate a unique file for each OBJECT library so in case
  426. // C::B uses it in some way, the targets don't interfere with each other.
  427. std::string filename = lg->GetCurrentBinaryDirectory();
  428. filename += "/";
  429. filename += lg->GetTargetDirectory(target);
  430. filename += "/";
  431. filename += target->GetName();
  432. filename += ".objlib";
  433. cmGeneratedFileStream fout(filename.c_str());
  434. if (fout) {
  435. /* clang-format off */
  436. fout << "# This is a dummy file for the OBJECT library "
  437. << target->GetName()
  438. << " for the CMake CodeBlocks project generator.\n"
  439. << "# Don't edit, this file will be overwritten.\n";
  440. /* clang-format on */
  441. }
  442. return filename;
  443. }
  444. // Generate the xml code for one target.
  445. void cmExtraCodeBlocksGenerator::AppendTarget(
  446. cmXMLWriter& xml, const std::string& targetName, cmGeneratorTarget* target,
  447. const char* make, const cmLocalGenerator* lg, const char* compiler,
  448. const std::string& makeFlags)
  449. {
  450. cmMakefile const* makefile = lg->GetMakefile();
  451. std::string makefileName = lg->GetCurrentBinaryDirectory();
  452. makefileName += "/Makefile";
  453. xml.StartElement("Target");
  454. xml.Attribute("title", targetName);
  455. if (target != CM_NULLPTR) {
  456. int cbTargetType = this->GetCBTargetType(target);
  457. std::string workingDir = lg->GetCurrentBinaryDirectory();
  458. if (target->GetType() == cmState::EXECUTABLE) {
  459. // Determine the directory where the executable target is created, and
  460. // set the working directory to this dir.
  461. const char* runtimeOutputDir =
  462. makefile->GetDefinition("CMAKE_RUNTIME_OUTPUT_DIRECTORY");
  463. if (runtimeOutputDir != CM_NULLPTR) {
  464. workingDir = runtimeOutputDir;
  465. } else {
  466. const char* executableOutputDir =
  467. makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  468. if (executableOutputDir != CM_NULLPTR) {
  469. workingDir = executableOutputDir;
  470. }
  471. }
  472. }
  473. std::string buildType = makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  474. std::string location;
  475. if (target->GetType() == cmState::OBJECT_LIBRARY) {
  476. location =
  477. this->CreateDummyTargetFile(const_cast<cmLocalGenerator*>(lg), target);
  478. } else {
  479. location = target->GetLocation(buildType);
  480. }
  481. xml.StartElement("Option");
  482. xml.Attribute("output", location);
  483. xml.Attribute("prefix_auto", 0);
  484. xml.Attribute("extension_auto", 0);
  485. xml.EndElement();
  486. xml.StartElement("Option");
  487. xml.Attribute("working_dir", workingDir);
  488. xml.EndElement();
  489. xml.StartElement("Option");
  490. xml.Attribute("object_output", "./");
  491. xml.EndElement();
  492. xml.StartElement("Option");
  493. xml.Attribute("type", cbTargetType);
  494. xml.EndElement();
  495. xml.StartElement("Option");
  496. xml.Attribute("compiler", compiler);
  497. xml.EndElement();
  498. xml.StartElement("Compiler");
  499. // the compilerdefines for this target
  500. std::vector<std::string> cdefs;
  501. target->GetCompileDefinitions(cdefs, buildType, "C");
  502. // Expand the list.
  503. for (std::vector<std::string>::const_iterator di = cdefs.begin();
  504. di != cdefs.end(); ++di) {
  505. xml.StartElement("Add");
  506. xml.Attribute("option", "-D" + *di);
  507. xml.EndElement();
  508. }
  509. // the include directories for this target
  510. std::vector<std::string> allIncludeDirs;
  511. std::vector<std::string> includes;
  512. lg->GetIncludeDirectories(includes, target, "C", buildType);
  513. allIncludeDirs.insert(allIncludeDirs.end(), includes.begin(),
  514. includes.end());
  515. std::string systemIncludeDirs = makefile->GetSafeDefinition(
  516. "CMAKE_EXTRA_GENERATOR_C_SYSTEM_INCLUDE_DIRS");
  517. if (!systemIncludeDirs.empty()) {
  518. std::vector<std::string> dirs;
  519. cmSystemTools::ExpandListArgument(systemIncludeDirs, dirs);
  520. allIncludeDirs.insert(allIncludeDirs.end(), dirs.begin(), dirs.end());
  521. }
  522. systemIncludeDirs = makefile->GetSafeDefinition(
  523. "CMAKE_EXTRA_GENERATOR_CXX_SYSTEM_INCLUDE_DIRS");
  524. if (!systemIncludeDirs.empty()) {
  525. std::vector<std::string> dirs;
  526. cmSystemTools::ExpandListArgument(systemIncludeDirs, dirs);
  527. allIncludeDirs.insert(allIncludeDirs.end(), dirs.begin(), dirs.end());
  528. }
  529. std::vector<std::string>::const_iterator end =
  530. cmRemoveDuplicates(allIncludeDirs);
  531. for (std::vector<std::string>::const_iterator i = allIncludeDirs.begin();
  532. i != end; ++i) {
  533. xml.StartElement("Add");
  534. xml.Attribute("directory", *i);
  535. xml.EndElement();
  536. }
  537. xml.EndElement(); // Compiler
  538. } else // e.g. all and the GLOBAL and UTILITY targets
  539. {
  540. xml.StartElement("Option");
  541. xml.Attribute("working_dir", lg->GetCurrentBinaryDirectory());
  542. xml.EndElement();
  543. xml.StartElement("Option");
  544. xml.Attribute("type", 4);
  545. xml.EndElement();
  546. }
  547. xml.StartElement("MakeCommands");
  548. xml.StartElement("Build");
  549. xml.Attribute("command", this->BuildMakeCommand(make, makefileName.c_str(),
  550. targetName, makeFlags));
  551. xml.EndElement();
  552. xml.StartElement("CompileFile");
  553. xml.Attribute("command", this->BuildMakeCommand(make, makefileName.c_str(),
  554. "\"$file\"", makeFlags));
  555. xml.EndElement();
  556. xml.StartElement("Clean");
  557. xml.Attribute("command", this->BuildMakeCommand(make, makefileName.c_str(),
  558. "clean", makeFlags));
  559. xml.EndElement();
  560. xml.StartElement("DistClean");
  561. xml.Attribute("command", this->BuildMakeCommand(make, makefileName.c_str(),
  562. "clean", makeFlags));
  563. xml.EndElement();
  564. xml.EndElement(); // MakeCommands
  565. xml.EndElement(); // Target
  566. }
  567. // Translate the cmake compiler id into the CodeBlocks compiler id
  568. std::string cmExtraCodeBlocksGenerator::GetCBCompilerId(const cmMakefile* mf)
  569. {
  570. // figure out which language to use
  571. // for now care only for C, C++, and Fortran
  572. // projects with C/C++ and Fortran are handled as C/C++ projects
  573. bool pureFortran = false;
  574. std::string compilerIdVar;
  575. if (this->GlobalGenerator->GetLanguageEnabled("CXX")) {
  576. compilerIdVar = "CMAKE_CXX_COMPILER_ID";
  577. } else if (this->GlobalGenerator->GetLanguageEnabled("C")) {
  578. compilerIdVar = "CMAKE_C_COMPILER_ID";
  579. } else if (this->GlobalGenerator->GetLanguageEnabled("Fortran")) {
  580. compilerIdVar = "CMAKE_Fortran_COMPILER_ID";
  581. pureFortran = true;
  582. }
  583. std::string compilerId = mf->GetSafeDefinition(compilerIdVar);
  584. std::string compiler = "gcc"; // default to gcc
  585. if (compilerId == "MSVC") {
  586. if (mf->IsDefinitionSet("MSVC10")) {
  587. compiler = "msvc10";
  588. } else {
  589. compiler = "msvc8";
  590. }
  591. } else if (compilerId == "Borland") {
  592. compiler = "bcc";
  593. } else if (compilerId == "SDCC") {
  594. compiler = "sdcc";
  595. } else if (compilerId == "Intel") {
  596. if (pureFortran && mf->IsDefinitionSet("WIN32")) {
  597. compiler = "ifcwin"; // Intel Fortran for Windows (known by cbFortran)
  598. } else {
  599. compiler = "icc";
  600. }
  601. } else if (compilerId == "Watcom" || compilerId == "OpenWatcom") {
  602. compiler = "ow";
  603. } else if (compilerId == "Clang") {
  604. compiler = "clang";
  605. } else if (compilerId == "PGI") {
  606. if (pureFortran) {
  607. compiler = "pgifortran";
  608. } else {
  609. compiler = "pgi"; // does not exist as default in CodeBlocks 16.01
  610. }
  611. } else if (compilerId == "GNU") {
  612. if (pureFortran) {
  613. compiler = "gfortran";
  614. } else {
  615. compiler = "gcc";
  616. }
  617. }
  618. return compiler;
  619. }
  620. // Translate the cmake target type into the CodeBlocks target type id
  621. int cmExtraCodeBlocksGenerator::GetCBTargetType(cmGeneratorTarget* target)
  622. {
  623. if (target->GetType() == cmState::EXECUTABLE) {
  624. if ((target->GetPropertyAsBool("WIN32_EXECUTABLE")) ||
  625. (target->GetPropertyAsBool("MACOSX_BUNDLE"))) {
  626. return 0;
  627. } else {
  628. return 1;
  629. }
  630. } else if ((target->GetType() == cmState::STATIC_LIBRARY) ||
  631. (target->GetType() == cmState::OBJECT_LIBRARY)) {
  632. return 2;
  633. } else if ((target->GetType() == cmState::SHARED_LIBRARY) ||
  634. (target->GetType() == cmState::MODULE_LIBRARY)) {
  635. return 3;
  636. }
  637. return 4;
  638. }
  639. // Create the command line for building the given target using the selected
  640. // make
  641. std::string cmExtraCodeBlocksGenerator::BuildMakeCommand(
  642. const std::string& make, const char* makefile, const std::string& target,
  643. const std::string& makeFlags)
  644. {
  645. std::string command = make;
  646. if (!makeFlags.empty()) {
  647. command += " ";
  648. command += makeFlags;
  649. }
  650. std::string generator = this->GlobalGenerator->GetName();
  651. if (generator == "NMake Makefiles") {
  652. // For Windows ConvertToOutputPath already adds quotes when required.
  653. // These need to be escaped, see
  654. // http://public.kitware.com/Bug/view.php?id=13952
  655. std::string makefileName = cmSystemTools::ConvertToOutputPath(makefile);
  656. command += " /NOLOGO /f ";
  657. command += makefileName;
  658. command += " VERBOSE=1 ";
  659. command += target;
  660. } else if (generator == "MinGW Makefiles") {
  661. // no escaping of spaces in this case, see
  662. // http://public.kitware.com/Bug/view.php?id=10014
  663. std::string makefileName = makefile;
  664. command += " -f \"";
  665. command += makefileName;
  666. command += "\" ";
  667. command += " VERBOSE=1 ";
  668. command += target;
  669. } else if (generator == "Ninja") {
  670. command += " -v ";
  671. command += target;
  672. } else {
  673. std::string makefileName = cmSystemTools::ConvertToOutputPath(makefile);
  674. command += " -f \"";
  675. command += makefileName;
  676. command += "\" ";
  677. command += " VERBOSE=1 ";
  678. command += target;
  679. }
  680. return command;
  681. }