cmExtraCodeBlocksGenerator.cxx 25 KB

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