cmExtraCodeBlocksGenerator.cxx 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  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. }
  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->GetStartOutputDirectory();
  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.c_str(), 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(), mf, 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->GetStartOutputDirectory(),
  297. makefile->GetHomeOutputDirectory())==0)
  298. {
  299. this->AppendTarget(fout, ti->first.c_str(), 0,
  300. make.c_str(), makefile, 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.c_str(), 0,
  315. make.c_str(), makefile, 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.c_str(), &ti->second,
  324. make.c_str(), makefile, compiler.c_str());
  325. std::string fastTarget = ti->first;
  326. fastTarget += "/fast";
  327. this->AppendTarget(fout, fastTarget.c_str(), &ti->second,
  328. make.c_str(), makefile, 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. // Sort them into two containers, one for C/C++ implementation files
  339. // which may have an acompanying header, one for all other files
  340. std::map<std::string, cmSourceFile*> cFiles;
  341. std::set<std::string> otherFiles;
  342. for (std::vector<cmLocalGenerator*>::const_iterator lg=lgs.begin();
  343. lg!=lgs.end(); lg++)
  344. {
  345. cmMakefile* makefile=(*lg)->GetMakefile();
  346. cmTargets& targets=makefile->GetTargets();
  347. for (cmTargets::iterator ti = targets.begin();
  348. ti != targets.end(); ti++)
  349. {
  350. switch(ti->second.GetType())
  351. {
  352. case cmTarget::EXECUTABLE:
  353. case cmTarget::STATIC_LIBRARY:
  354. case cmTarget::SHARED_LIBRARY:
  355. case cmTarget::MODULE_LIBRARY:
  356. case cmTarget::OBJECT_LIBRARY:
  357. case cmTarget::UTILITY: // can have sources since 2.6.3
  358. {
  359. std::vector<cmSourceFile*> sources;
  360. ti->second.GetSourceFiles(sources);
  361. for (std::vector<cmSourceFile*>::const_iterator si=sources.begin();
  362. si!=sources.end(); si++)
  363. {
  364. // don't add source files which have the GENERATED property set:
  365. if ((*si)->GetPropertyAsBool("GENERATED"))
  366. {
  367. continue;
  368. }
  369. // check whether it is a C/C++ implementation file
  370. bool isCFile = false;
  371. std::string lang = (*si)->GetLanguage();
  372. if (lang == "C" || lang == "CXX")
  373. {
  374. for(std::vector<std::string>::const_iterator
  375. ext = mf->GetSourceExtensions().begin();
  376. ext != mf->GetSourceExtensions().end();
  377. ++ext)
  378. {
  379. if ((*si)->GetExtension() == *ext)
  380. {
  381. isCFile = true;
  382. break;
  383. }
  384. }
  385. }
  386. // then put it accordingly into one of the two containers
  387. if (isCFile)
  388. {
  389. cFiles[(*si)->GetFullPath()] = *si ;
  390. }
  391. else
  392. {
  393. otherFiles.insert((*si)->GetFullPath());
  394. }
  395. }
  396. }
  397. default: // intended fallthrough
  398. break;
  399. }
  400. }
  401. }
  402. // The following loop tries to add header files matching to implementation
  403. // files to the project. It does that by iterating over all source files,
  404. // replacing the file name extension with ".h" and checks whether such a
  405. // file exists. If it does, it is inserted into the map of files.
  406. // A very similar version of that code exists also in the kdevelop
  407. // project generator.
  408. for (std::map<std::string, cmSourceFile*>::const_iterator
  409. sit=cFiles.begin();
  410. sit!=cFiles.end();
  411. ++sit)
  412. {
  413. std::string headerBasename=cmSystemTools::GetFilenamePath(sit->first);
  414. headerBasename+="/";
  415. headerBasename+=cmSystemTools::GetFilenameWithoutExtension(sit->first);
  416. // check if there's a matching header around
  417. for(std::vector<std::string>::const_iterator
  418. ext = mf->GetHeaderExtensions().begin();
  419. ext != mf->GetHeaderExtensions().end();
  420. ++ext)
  421. {
  422. std::string hname=headerBasename;
  423. hname += ".";
  424. hname += *ext;
  425. // if it's already in the set, don't check if it exists on disk
  426. std::set<std::string>::const_iterator headerIt=otherFiles.find(hname);
  427. if (headerIt != otherFiles.end())
  428. {
  429. break;
  430. }
  431. if(cmSystemTools::FileExists(hname.c_str()))
  432. {
  433. otherFiles.insert(hname);
  434. break;
  435. }
  436. }
  437. }
  438. // insert all source files in the CodeBlocks project
  439. // first the C/C++ implementation files, then all others
  440. for (std::map<std::string, cmSourceFile*>::const_iterator
  441. sit=cFiles.begin();
  442. sit!=cFiles.end();
  443. ++sit)
  444. {
  445. fout<<" <Unit filename=\""<< sit->first <<"\">\n"
  446. " </Unit>\n";
  447. }
  448. for (std::set<std::string>::const_iterator
  449. sit=otherFiles.begin();
  450. sit!=otherFiles.end();
  451. ++sit)
  452. {
  453. fout<<" <Unit filename=\""<< sit->c_str() <<"\">\n"
  454. " </Unit>\n";
  455. }
  456. // Add CMakeLists.txt
  457. fout<<unitFiles;
  458. fout<<" </Project>\n"
  459. "</CodeBlocks_project_file>\n";
  460. }
  461. // Write a dummy file for OBJECT libraries, so C::B can reference some file
  462. std::string cmExtraCodeBlocksGenerator::CreateDummyTargetFile(
  463. cmMakefile* mf, cmTarget* target) const
  464. {
  465. // this file doesn't seem to be used by C::B in custom makefile mode,
  466. // but we generate a unique file for each OBJECT library so in case
  467. // C::B uses it in some way, the targets don't interfere with each other.
  468. std::string filename = mf->GetCurrentOutputDirectory();
  469. filename += "/";
  470. filename += mf->GetLocalGenerator()->GetTargetDirectory(*target);
  471. filename += "/";
  472. filename += target->GetName();
  473. filename += ".objlib";
  474. cmGeneratedFileStream fout(filename.c_str());
  475. if(fout)
  476. {
  477. fout << "# This is a dummy file for the OBJECT library "
  478. << target->GetName()
  479. << " for the CMake CodeBlocks project generator.\n"
  480. << "# Don't edit, this file will be overwritten.\n";
  481. }
  482. return filename;
  483. }
  484. // Generate the xml code for one target.
  485. void cmExtraCodeBlocksGenerator::AppendTarget(cmGeneratedFileStream& fout,
  486. const std::string& targetName,
  487. cmTarget* target,
  488. const char* make,
  489. const cmMakefile* makefile,
  490. const char* compiler)
  491. {
  492. std::string makefileName = makefile->GetStartOutputDirectory();
  493. makefileName += "/Makefile";
  494. fout<<" <Target title=\"" << targetName << "\">\n";
  495. if (target!=0)
  496. {
  497. int cbTargetType = this->GetCBTargetType(target);
  498. std::string workingDir = makefile->GetStartOutputDirectory();
  499. if ( target->GetType()==cmTarget::EXECUTABLE)
  500. {
  501. // Determine the directory where the executable target is created, and
  502. // set the working directory to this dir.
  503. const char* runtimeOutputDir = makefile->GetDefinition(
  504. "CMAKE_RUNTIME_OUTPUT_DIRECTORY");
  505. if (runtimeOutputDir != 0)
  506. {
  507. workingDir = runtimeOutputDir;
  508. }
  509. else
  510. {
  511. const char* executableOutputDir = makefile->GetDefinition(
  512. "EXECUTABLE_OUTPUT_PATH");
  513. if (executableOutputDir != 0)
  514. {
  515. workingDir = executableOutputDir;
  516. }
  517. }
  518. }
  519. const char* buildType = makefile->GetDefinition("CMAKE_BUILD_TYPE");
  520. std::string location;
  521. if ( target->GetType()==cmTarget::OBJECT_LIBRARY)
  522. {
  523. location = this->CreateDummyTargetFile(const_cast<cmMakefile*>(makefile),
  524. target);
  525. }
  526. else
  527. {
  528. location = target->GetLocation(buildType);
  529. }
  530. fout<<" <Option output=\"" << location
  531. << "\" prefix_auto=\"0\" extension_auto=\"0\" />\n"
  532. " <Option working_dir=\"" << workingDir << "\" />\n"
  533. " <Option object_output=\"./\" />\n"
  534. " <Option type=\"" << cbTargetType << "\" />\n"
  535. " <Option compiler=\"" << compiler << "\" />\n"
  536. " <Compiler>\n";
  537. cmGeneratorTarget *gtgt = this->GlobalGenerator
  538. ->GetGeneratorTarget(target);
  539. // the compilerdefines for this target
  540. std::vector<std::string> cdefs;
  541. target->GetCompileDefinitions(cdefs, buildType);
  542. // Expand the list.
  543. for(std::vector<std::string>::const_iterator di = cdefs.begin();
  544. di != cdefs.end(); ++di)
  545. {
  546. cmXMLSafe safedef(di->c_str());
  547. fout <<" <Add option=\"-D" << safedef.str() << "\" />\n";
  548. }
  549. // the include directories for this target
  550. std::set<std::string> uniqIncludeDirs;
  551. std::vector<std::string> includes;
  552. target->GetMakefile()->GetLocalGenerator()->
  553. GetIncludeDirectories(includes, gtgt, "C", buildType);
  554. for(std::vector<std::string>::const_iterator dirIt=includes.begin();
  555. dirIt != includes.end();
  556. ++dirIt)
  557. {
  558. uniqIncludeDirs.insert(*dirIt);
  559. }
  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.c_str(), dirs);
  566. for(std::vector<std::string>::const_iterator dirIt=dirs.begin();
  567. dirIt != dirs.end();
  568. ++dirIt)
  569. {
  570. uniqIncludeDirs.insert(*dirIt);
  571. }
  572. }
  573. systemIncludeDirs = makefile->GetSafeDefinition(
  574. "CMAKE_EXTRA_GENERATOR_CXX_SYSTEM_INCLUDE_DIRS");
  575. if (!systemIncludeDirs.empty())
  576. {
  577. std::vector<std::string> dirs;
  578. cmSystemTools::ExpandListArgument(systemIncludeDirs.c_str(), dirs);
  579. for(std::vector<std::string>::const_iterator dirIt=dirs.begin();
  580. dirIt != dirs.end();
  581. ++dirIt)
  582. {
  583. uniqIncludeDirs.insert(*dirIt);
  584. }
  585. }
  586. for(std::set<std::string>::const_iterator dirIt=uniqIncludeDirs.begin();
  587. dirIt != uniqIncludeDirs.end();
  588. ++dirIt)
  589. {
  590. fout <<" <Add directory=\"" << dirIt->c_str() << "\" />\n";
  591. }
  592. fout<<" </Compiler>\n";
  593. }
  594. else // e.g. all and the GLOBAL and UTILITY targets
  595. {
  596. fout<<" <Option working_dir=\""
  597. << makefile->GetStartOutputDirectory() << "\" />\n"
  598. <<" <Option type=\"" << 4 << "\" />\n";
  599. }
  600. fout<<" <MakeCommands>\n"
  601. " <Build command=\""
  602. << this->BuildMakeCommand(make, makefileName.c_str(), targetName)
  603. << "\" />\n"
  604. " <CompileFile command=\""
  605. << this->BuildMakeCommand(make, makefileName.c_str(),"&quot;$file&quot;")
  606. << "\" />\n"
  607. " <Clean command=\""
  608. << this->BuildMakeCommand(make, makefileName.c_str(), "clean")
  609. << "\" />\n"
  610. " <DistClean command=\""
  611. << this->BuildMakeCommand(make, makefileName.c_str(), "clean")
  612. << "\" />\n"
  613. " </MakeCommands>\n"
  614. " </Target>\n";
  615. }
  616. // Translate the cmake compiler id into the CodeBlocks compiler id
  617. std::string cmExtraCodeBlocksGenerator::GetCBCompilerId(const cmMakefile* mf)
  618. {
  619. // figure out which language to use
  620. // for now care only for C and C++
  621. std::string compilerIdVar = "CMAKE_CXX_COMPILER_ID";
  622. if (this->GlobalGenerator->GetLanguageEnabled("CXX") == false)
  623. {
  624. compilerIdVar = "CMAKE_C_COMPILER_ID";
  625. }
  626. std::string hostSystemName = mf->GetSafeDefinition("CMAKE_HOST_SYSTEM_NAME");
  627. std::string systemName = mf->GetSafeDefinition("CMAKE_SYSTEM_NAME");
  628. std::string compilerId = mf->GetSafeDefinition(compilerIdVar.c_str());
  629. std::string compiler = "gcc"; // default to gcc
  630. if (compilerId == "MSVC")
  631. {
  632. compiler = "msvc8";
  633. }
  634. else if (compilerId == "Borland")
  635. {
  636. compiler = "bcc";
  637. }
  638. else if (compilerId == "SDCC")
  639. {
  640. compiler = "sdcc";
  641. }
  642. else if (compilerId == "Intel")
  643. {
  644. compiler = "icc";
  645. }
  646. else if (compilerId == "Watcom")
  647. {
  648. compiler = "ow";
  649. }
  650. else if (compilerId == "GNU")
  651. {
  652. compiler = "gcc";
  653. }
  654. return compiler;
  655. }
  656. // Translate the cmake target type into the CodeBlocks target type id
  657. int cmExtraCodeBlocksGenerator::GetCBTargetType(cmTarget* target)
  658. {
  659. if ( target->GetType()==cmTarget::EXECUTABLE)
  660. {
  661. if ((target->GetPropertyAsBool("WIN32_EXECUTABLE"))
  662. || (target->GetPropertyAsBool("MACOSX_BUNDLE")))
  663. {
  664. return 0;
  665. }
  666. else
  667. {
  668. return 1;
  669. }
  670. }
  671. else if (( target->GetType()==cmTarget::STATIC_LIBRARY)
  672. || (target->GetType()==cmTarget::OBJECT_LIBRARY))
  673. {
  674. return 2;
  675. }
  676. else if ((target->GetType()==cmTarget::SHARED_LIBRARY)
  677. || (target->GetType()==cmTarget::MODULE_LIBRARY))
  678. {
  679. return 3;
  680. }
  681. return 4;
  682. }
  683. // Create the command line for building the given target using the selected
  684. // make
  685. std::string cmExtraCodeBlocksGenerator::BuildMakeCommand(
  686. const std::string& make, const char* makefile,
  687. const std::string& target)
  688. {
  689. std::string command = make;
  690. if (strcmp(this->GlobalGenerator->GetName(), "NMake Makefiles")==0)
  691. {
  692. // For Windows ConvertToOutputPath already adds quotes when required.
  693. // These need to be escaped, see
  694. // http://public.kitware.com/Bug/view.php?id=13952
  695. std::string makefileName = cmSystemTools::ConvertToOutputPath(makefile);
  696. command += " /NOLOGO /f ";
  697. command += cmXMLSafe(makefileName).str();
  698. command += " VERBOSE=1 ";
  699. command += target;
  700. }
  701. else if (strcmp(this->GlobalGenerator->GetName(), "MinGW Makefiles")==0)
  702. {
  703. // no escaping of spaces in this case, see
  704. // http://public.kitware.com/Bug/view.php?id=10014
  705. std::string makefileName = makefile;
  706. command += " -f &quot;";
  707. command += makefileName;
  708. command += "&quot; ";
  709. command += " VERBOSE=1 ";
  710. command += target;
  711. }
  712. else if (strcmp(this->GlobalGenerator->GetName(), "Ninja")==0)
  713. {
  714. command += " -v ";
  715. command += target;
  716. }
  717. else
  718. {
  719. std::string makefileName = cmSystemTools::ConvertToOutputPath(makefile);
  720. command += " -f &quot;";
  721. command += makefileName;
  722. command += "&quot; ";
  723. command += " VERBOSE=1 ";
  724. command += target;
  725. }
  726. return command;
  727. }