cmExtraCodeBlocksGenerator.cxx 25 KB

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