cmExtraCodeBlocksGenerator.cxx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. Copyright (c) 2004 Alexander Neundorf [email protected], All rights reserved.
  9. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  10. This software is distributed WITHOUT ANY WARRANTY; without even
  11. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  12. PURPOSE. See the above copyright notices for more information.
  13. =========================================================================*/
  14. #include "cmExtraCodeBlocksGenerator.h"
  15. #include "cmGlobalUnixMakefileGenerator3.h"
  16. #include "cmLocalUnixMakefileGenerator3.h"
  17. #include "cmMakefile.h"
  18. #include "cmake.h"
  19. #include "cmSourceFile.h"
  20. #include "cmGeneratedFileStream.h"
  21. #include "cmTarget.h"
  22. #include <cmsys/SystemTools.hxx>
  23. //----------------------------------------------------------------------------
  24. void cmExtraCodeBlocksGenerator
  25. ::GetDocumentation(cmDocumentationEntry& entry, const char*) const
  26. {
  27. entry.name = this->GetName();
  28. entry.brief = "Generates CodeBlocks project files.";
  29. entry.full =
  30. "Project files for CodeBlocks will be created in the top directory "
  31. "and in every subdirectory which features a CMakeLists.txt file "
  32. "containing a PROJECT() call. "
  33. "Additionally a hierarchy of makefiles is generated into the "
  34. "build tree. The appropriate make program can build the project through "
  35. "the default make target. A \"make install\" target is also provided.";
  36. }
  37. cmExtraCodeBlocksGenerator::cmExtraCodeBlocksGenerator()
  38. :cmExternalMakefileProjectGenerator()
  39. {
  40. #if defined(_WIN32)
  41. this->SupportedGlobalGenerators.push_back("NMake Makefiles");
  42. this->SupportedGlobalGenerators.push_back("MinGW Makefiles");
  43. // disable MSYS until somebody actually tests it
  44. // this->SupportedGlobalGenerators.push_back("MSYS Makefiles");
  45. #endif
  46. this->SupportedGlobalGenerators.push_back("Unix Makefiles");
  47. }
  48. void cmExtraCodeBlocksGenerator::SetGlobalGenerator(
  49. cmGlobalGenerator* generator)
  50. {
  51. cmExternalMakefileProjectGenerator::SetGlobalGenerator(generator);
  52. cmGlobalUnixMakefileGenerator3* mf = (cmGlobalUnixMakefileGenerator3*)
  53. generator;
  54. mf->SetToolSupportsColor(false);
  55. mf->SetForceVerboseMakefiles(true);
  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, if it already exists, merge it with the
  70. existing one, otherwise create a new one */
  71. void cmExtraCodeBlocksGenerator::CreateProjectFile(
  72. const std::vector<cmLocalGenerator*>& lgs)
  73. {
  74. const cmMakefile* mf=lgs[0]->GetMakefile();
  75. std::string outputDir=mf->GetStartOutputDirectory();
  76. std::string projectDir=mf->GetHomeDirectory();
  77. std::string projectName=mf->GetProjectName();
  78. std::string filename=outputDir+"/";
  79. filename+=projectName+".cbp";
  80. std::string sessionFilename=outputDir+"/";
  81. sessionFilename+=projectName+".layout";
  82. /* if (cmSystemTools::FileExists(filename.c_str()))
  83. {
  84. this->MergeProjectFiles(outputDir, projectDir, filename,
  85. cmakeFilePattern, sessionFilename);
  86. }
  87. else */
  88. {
  89. this->CreateNewProjectFile(lgs, filename);
  90. }
  91. }
  92. void cmExtraCodeBlocksGenerator
  93. ::CreateNewProjectFile(const std::vector<cmLocalGenerator*>& lgs,
  94. const std::string& filename)
  95. {
  96. const cmMakefile* mf=lgs[0]->GetMakefile();
  97. cmGeneratedFileStream fout(filename.c_str());
  98. if(!fout)
  99. {
  100. return;
  101. }
  102. std::string make = mf->GetRequiredDefinition("CMAKE_MAKE_PROGRAM");
  103. fout<<"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>\n"
  104. "<CodeBlocks_project_file>\n"
  105. " <FileVersion major=\"1\" minor=\"6\" />\n"
  106. " <Project>\n";
  107. fout<<" <Option title=\"" << mf->GetProjectName()<<"\" />\n"
  108. " <Option makefile_is_custom=\"1\" />\n"
  109. " <Option compiler=\"gcc\" />\n"
  110. " <Build>\n";
  111. bool installTargetCreated = false;
  112. bool testTargetCreated = false;
  113. bool packageTargetCreated = false;
  114. for (std::vector<cmLocalGenerator*>::const_iterator lg=lgs.begin();
  115. lg!=lgs.end(); lg++)
  116. {
  117. cmMakefile* makefile=(*lg)->GetMakefile();
  118. cmTargets& targets=makefile->GetTargets();
  119. for (cmTargets::iterator ti = targets.begin();
  120. ti != targets.end(); ti++)
  121. {
  122. switch(ti->second.GetType())
  123. {
  124. case cmTarget::GLOBAL_TARGET:
  125. if ((ti->first=="install") && (installTargetCreated==false))
  126. {
  127. installTargetCreated=true;
  128. }
  129. else if ((ti->first=="package") && (packageTargetCreated==false))
  130. {
  131. packageTargetCreated=true;
  132. }
  133. else if ((ti->first=="test") && (testTargetCreated==false))
  134. {
  135. testTargetCreated=true;
  136. }
  137. else
  138. {
  139. break;
  140. }
  141. case cmTarget::EXECUTABLE:
  142. case cmTarget::STATIC_LIBRARY:
  143. case cmTarget::SHARED_LIBRARY:
  144. case cmTarget::MODULE_LIBRARY:
  145. // case cmTarget::UTILITY:
  146. fout<<" <Target title=\""<<ti->first<<"\">\n"
  147. " <Option output=\""<<ti->second.GetLocation(0)
  148. <<"\" prefix_auto=\"0\" extension_auto=\"0\" />\n"
  149. " <Option working_dir=\""<<makefile->GetStartOutputDirectory()
  150. <<"\" />\n"
  151. " <Option type=\"0\" />\n"
  152. " <Option compiler=\"gcc\" />\n"
  153. " <MakeCommands>\n";
  154. fout<<" <Build command=\""<<make<<" -f "
  155. <<makefile->GetStartOutputDirectory()<<"/Makefile "<<ti->first<<"\" />\n";
  156. fout<<" <CompileFile command=\""<<make<<" -f "
  157. <<makefile->GetStartOutputDirectory()<<"/Makefile "<<ti->first<<"\" />\n";
  158. fout<<" <Clean command=\""<<make<<" -f "
  159. <<makefile->GetStartOutputDirectory()<<"/Makefile clean\" />\n";
  160. fout<<" <DistClean command=\""<<make<<" -f "
  161. <<makefile->GetStartOutputDirectory()<<"/Makefile clean\" />\n";
  162. fout<<" </MakeCommands>\n"
  163. " </Target>\n";
  164. break;
  165. default:
  166. break;
  167. }
  168. }
  169. }
  170. fout<<" </Build>\n";
  171. std::map<std::string, std::string> sourceFiles;
  172. for (std::vector<cmLocalGenerator*>::const_iterator lg=lgs.begin();
  173. lg!=lgs.end(); lg++)
  174. {
  175. cmMakefile* makefile=(*lg)->GetMakefile();
  176. cmTargets& targets=makefile->GetTargets();
  177. for (cmTargets::iterator ti = targets.begin();
  178. ti != targets.end(); ti++)
  179. {
  180. switch(ti->second.GetType())
  181. {
  182. case cmTarget::EXECUTABLE:
  183. case cmTarget::STATIC_LIBRARY:
  184. case cmTarget::SHARED_LIBRARY:
  185. case cmTarget::MODULE_LIBRARY:
  186. {
  187. const std::vector<cmSourceFile*>&sources=ti->second.GetSourceFiles();
  188. for (std::vector<cmSourceFile*>::const_iterator si=sources.begin();
  189. si!=sources.end(); si++)
  190. {
  191. sourceFiles[(*si)->GetFullPath()] = ti->first;
  192. }
  193. }
  194. default:
  195. break;
  196. }
  197. }
  198. }
  199. for (std::map<std::string, std::string>::const_iterator
  200. sit=sourceFiles.begin();
  201. sit!=sourceFiles.end();
  202. ++sit)
  203. {
  204. fout<<" <Unit filename=\""<<sit->first <<"\">\n";
  205. fout<<" </Unit>\n";
  206. }
  207. fout<<" </Project>\n"
  208. "</CodeBlocks_project_file>\n";
  209. }