cmExtraCodeBlocksGenerator.cxx 27 KB

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