cmExtraCodeBlocksGenerator.cxx 8.8 KB

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