cmExtraCodeBlocksGenerator.cxx 26 KB

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