cmExtraCodeBlocksGenerator.cxx 9.0 KB

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