cmExtraCodeBlocksGenerator.cxx 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. const cmMakefile* topLevelMakefile =
  60. this->GlobalGenerator->GetLocalGenerators()[0]->GetMakefile();
  61. std::string workspaceName = topLevelMakefile->GetProjectName();
  62. std::string outputDir=topLevelMakefile->GetStartOutputDirectory();
  63. std::string workspaceFilename = outputDir;
  64. workspaceFilename += "/";
  65. workspaceFilename += workspaceName;
  66. workspaceFilename += ".workspace";
  67. cmGeneratedFileStream fout(workspaceFilename.c_str());
  68. if(!fout)
  69. {
  70. return;
  71. }
  72. fout<<"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>\n"
  73. "<CodeBlocks_workspace_file>\n"
  74. " <Workspace title=\""<<workspaceName<<"\">\n";
  75. bool firstProject = true;
  76. // for each sub project in the project create a codeblocks project
  77. for (std::map<cmStdString, std::vector<cmLocalGenerator*> >::const_iterator
  78. it = this->GlobalGenerator->GetProjectMap().begin();
  79. it!= this->GlobalGenerator->GetProjectMap().end();
  80. ++it)
  81. {
  82. const cmMakefile* mf=it->second[0]->GetMakefile();
  83. std::string filename=mf->GetStartOutputDirectory();
  84. filename+="/";
  85. filename+=mf->GetProjectName();
  86. filename+=".cbp";
  87. if (firstProject)
  88. {
  89. fout<<" <Project filename=\""<< filename<<"\" active=\"1\"/>\n";
  90. }
  91. else
  92. {
  93. fout<<" <Project filename=\""<< filename<<"\" />\n";
  94. }
  95. // create a project file
  96. this->CreateProjectFile(it->second);
  97. }
  98. fout<<" </Workspace>\n"
  99. "</CodeBlocks_workspace_file>\n";
  100. }
  101. /* create the project file, if it already exists, merge it with the
  102. existing one, otherwise create a new one */
  103. void cmExtraCodeBlocksGenerator::CreateProjectFile(
  104. const std::vector<cmLocalGenerator*>& lgs)
  105. {
  106. const cmMakefile* mf=lgs[0]->GetMakefile();
  107. std::string outputDir=mf->GetStartOutputDirectory();
  108. std::string projectDir=mf->GetHomeDirectory();
  109. std::string projectName=mf->GetProjectName();
  110. std::string filename=outputDir+"/";
  111. filename+=projectName+".cbp";
  112. std::string sessionFilename=outputDir+"/";
  113. sessionFilename+=projectName+".layout";
  114. /* if (cmSystemTools::FileExists(filename.c_str()))
  115. {
  116. this->MergeProjectFiles(outputDir, projectDir, filename,
  117. cmakeFilePattern, sessionFilename);
  118. }
  119. else */
  120. {
  121. this->CreateNewProjectFile(lgs, filename);
  122. }
  123. }
  124. void cmExtraCodeBlocksGenerator
  125. ::CreateNewProjectFile(const std::vector<cmLocalGenerator*>& lgs,
  126. const std::string& filename)
  127. {
  128. const cmMakefile* mf=lgs[0]->GetMakefile();
  129. cmGeneratedFileStream fout(filename.c_str());
  130. if(!fout)
  131. {
  132. return;
  133. }
  134. std::string make = mf->GetRequiredDefinition("CMAKE_MAKE_PROGRAM");
  135. fout<<"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>\n"
  136. "<CodeBlocks_project_file>\n"
  137. " <FileVersion major=\"1\" minor=\"6\" />\n"
  138. " <Project>\n";
  139. fout<<" <Option title=\"" << mf->GetProjectName()<<"\" />\n"
  140. " <Option makefile_is_custom=\"1\" />\n"
  141. " <Option compiler=\"gcc\" />\n"
  142. " <Build>\n";
  143. bool installTargetCreated = false;
  144. bool testTargetCreated = false;
  145. bool packageTargetCreated = false;
  146. for (std::vector<cmLocalGenerator*>::const_iterator lg=lgs.begin();
  147. lg!=lgs.end(); lg++)
  148. {
  149. cmMakefile* makefile=(*lg)->GetMakefile();
  150. cmTargets& targets=makefile->GetTargets();
  151. for (cmTargets::iterator ti = targets.begin();
  152. ti != targets.end(); ti++)
  153. {
  154. switch(ti->second.GetType())
  155. {
  156. case cmTarget::GLOBAL_TARGET:
  157. if ((ti->first=="install") && (installTargetCreated==false))
  158. {
  159. installTargetCreated=true;
  160. }
  161. else if ((ti->first=="package") && (packageTargetCreated==false))
  162. {
  163. packageTargetCreated=true;
  164. }
  165. else if ((ti->first=="test") && (testTargetCreated==false))
  166. {
  167. testTargetCreated=true;
  168. }
  169. else
  170. {
  171. break;
  172. }
  173. case cmTarget::EXECUTABLE:
  174. case cmTarget::STATIC_LIBRARY:
  175. case cmTarget::SHARED_LIBRARY:
  176. case cmTarget::MODULE_LIBRARY:
  177. // case cmTarget::UTILITY:
  178. fout<<" <Target title=\""<<ti->first<<"\">\n"
  179. " <Option output=\""<<ti->second.GetLocation(0)
  180. <<"\" prefix_auto=\"0\" extension_auto=\"0\" />\n"
  181. " <Option working_dir=\""<<makefile->GetStartOutputDirectory()
  182. <<"\" />\n"
  183. " <Option type=\"0\" />\n"
  184. " <Option compiler=\"gcc\" />\n"
  185. " <MakeCommands>\n";
  186. fout<<" <Build command=\""<<make<<" -f "
  187. <<makefile->GetStartOutputDirectory()<<"/Makefile "<<ti->first<<"\" />\n";
  188. fout<<" <CompileFile command=\""<<make<<" -f "
  189. <<makefile->GetStartOutputDirectory()<<"/Makefile "<<ti->first<<"\" />\n";
  190. fout<<" <Clean command=\""<<make<<" -f "
  191. <<makefile->GetStartOutputDirectory()<<"/Makefile clean\" />\n";
  192. fout<<" <DistClean command=\""<<make<<" -f "
  193. <<makefile->GetStartOutputDirectory()<<"/Makefile clean\" />\n";
  194. fout<<" </MakeCommands>\n"
  195. " </Target>\n";
  196. break;
  197. default:
  198. break;
  199. }
  200. }
  201. }
  202. fout<<" </Build>\n";
  203. std::map<std::string, std::string> sourceFiles;
  204. for (std::vector<cmLocalGenerator*>::const_iterator lg=lgs.begin();
  205. lg!=lgs.end(); lg++)
  206. {
  207. cmMakefile* makefile=(*lg)->GetMakefile();
  208. cmTargets& targets=makefile->GetTargets();
  209. for (cmTargets::iterator ti = targets.begin();
  210. ti != targets.end(); ti++)
  211. {
  212. switch(ti->second.GetType())
  213. {
  214. case cmTarget::EXECUTABLE:
  215. case cmTarget::STATIC_LIBRARY:
  216. case cmTarget::SHARED_LIBRARY:
  217. case cmTarget::MODULE_LIBRARY:
  218. {
  219. const std::vector<cmSourceFile*>&sources=ti->second.GetSourceFiles();
  220. for (std::vector<cmSourceFile*>::const_iterator si=sources.begin();
  221. si!=sources.end(); si++)
  222. {
  223. sourceFiles[(*si)->GetFullPath()] = ti->first;
  224. }
  225. }
  226. default:
  227. break;
  228. }
  229. }
  230. }
  231. for (std::map<std::string, std::string>::const_iterator
  232. sit=sourceFiles.begin();
  233. sit!=sourceFiles.end();
  234. ++sit)
  235. {
  236. fout<<" <Unit filename=\""<<sit->first <<"\">\n";
  237. fout<<" </Unit>\n";
  238. }
  239. fout<<" </Project>\n"
  240. "</CodeBlocks_project_file>\n";
  241. }