cmExtraCodeBlocksGenerator.cxx 25 KB

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