cmExtraCodeBlocksGenerator.cxx 25 KB

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