cmExtraCodeBlocksGenerator.cxx 27 KB

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