cmExtraCodeBlocksGenerator.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 "cmSystemTools.h"
  23. #include <cmsys/SystemTools.hxx>
  24. /* Some useful URLs:
  25. Homepage:
  26. http://www.codeblocks.org
  27. File format docs:
  28. http://wiki.codeblocks.org/index.php?title=File_formats_description
  29. http://wiki.codeblocks.org/index.php?title=Workspace_file
  30. http://wiki.codeblocks.org/index.php?title=Project_file
  31. Discussion:
  32. http://forums.codeblocks.org/index.php/topic,6789.0.html
  33. */
  34. //----------------------------------------------------------------------------
  35. void cmExtraCodeBlocksGenerator
  36. ::GetDocumentation(cmDocumentationEntry& entry, const char*) const
  37. {
  38. entry.name = this->GetName();
  39. entry.brief = "Generates CodeBlocks project files.";
  40. entry.full =
  41. "Project files for CodeBlocks will be created in the top directory "
  42. "and in every subdirectory which features a CMakeLists.txt file "
  43. "containing a PROJECT() call. "
  44. "Additionally a hierarchy of makefiles is generated into the "
  45. "build tree. The appropriate make program can build the project through "
  46. "the default make target. A \"make install\" target is also provided.";
  47. }
  48. cmExtraCodeBlocksGenerator::cmExtraCodeBlocksGenerator()
  49. :cmExternalMakefileProjectGenerator()
  50. {
  51. #if defined(_WIN32)
  52. this->SupportedGlobalGenerators.push_back("MinGW Makefiles");
  53. // disable until somebody actually tests it:
  54. // this->SupportedGlobalGenerators.push_back("NMake Makefiles");
  55. // this->SupportedGlobalGenerators.push_back("MSYS Makefiles");
  56. #endif
  57. this->SupportedGlobalGenerators.push_back("Unix Makefiles");
  58. }
  59. void cmExtraCodeBlocksGenerator::SetGlobalGenerator(
  60. cmGlobalGenerator* generator)
  61. {
  62. cmExternalMakefileProjectGenerator::SetGlobalGenerator(generator);
  63. cmGlobalUnixMakefileGenerator3* mf = (cmGlobalUnixMakefileGenerator3*)
  64. generator;
  65. mf->SetToolSupportsColor(false);
  66. mf->SetForceVerboseMakefiles(true);
  67. }
  68. void cmExtraCodeBlocksGenerator::Generate()
  69. {
  70. // for each sub project in the project create a codeblocks project
  71. for (std::map<cmStdString, std::vector<cmLocalGenerator*> >::const_iterator
  72. it = this->GlobalGenerator->GetProjectMap().begin();
  73. it!= this->GlobalGenerator->GetProjectMap().end();
  74. ++it)
  75. {
  76. // create a project file
  77. this->CreateProjectFile(it->second);
  78. }
  79. }
  80. /* create the project file, if it already exists, merge it with the
  81. existing one, otherwise create a new one */
  82. void cmExtraCodeBlocksGenerator::CreateProjectFile(
  83. const std::vector<cmLocalGenerator*>& lgs)
  84. {
  85. const cmMakefile* mf=lgs[0]->GetMakefile();
  86. std::string outputDir=mf->GetStartOutputDirectory();
  87. std::string projectDir=mf->GetHomeDirectory();
  88. std::string projectName=mf->GetProjectName();
  89. std::string filename=outputDir+"/";
  90. filename+=projectName+".cbp";
  91. std::string sessionFilename=outputDir+"/";
  92. sessionFilename+=projectName+".layout";
  93. /* if (cmSystemTools::FileExists(filename.c_str()))
  94. {
  95. this->MergeProjectFiles(outputDir, projectDir, filename,
  96. cmakeFilePattern, sessionFilename);
  97. }
  98. else */
  99. {
  100. this->CreateNewProjectFile(lgs, filename);
  101. }
  102. }
  103. void cmExtraCodeBlocksGenerator
  104. ::CreateNewProjectFile(const std::vector<cmLocalGenerator*>& lgs,
  105. const std::string& filename)
  106. {
  107. const cmMakefile* mf=lgs[0]->GetMakefile();
  108. cmGeneratedFileStream fout(filename.c_str());
  109. if(!fout)
  110. {
  111. return;
  112. }
  113. // figure out the compiler
  114. std::string compiler = this->GetCBCompilerId(mf);
  115. std::string make = mf->GetRequiredDefinition("CMAKE_MAKE_PROGRAM");
  116. fout<<"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>\n"
  117. "<CodeBlocks_project_file>\n"
  118. " <FileVersion major=\"1\" minor=\"6\" />\n"
  119. " <Project>\n"
  120. " <Option title=\"" << mf->GetProjectName()<<"\" />\n"
  121. " <Option makefile_is_custom=\"1\" />\n"
  122. " <Option compiler=\"" << compiler << "\" />\n"
  123. " <Build>\n";
  124. bool installTargetCreated = false;
  125. bool testTargetCreated = false;
  126. bool packageTargetCreated = false;
  127. for (std::vector<cmLocalGenerator*>::const_iterator lg=lgs.begin();
  128. lg!=lgs.end(); lg++)
  129. {
  130. cmMakefile* makefile=(*lg)->GetMakefile();
  131. cmTargets& targets=makefile->GetTargets();
  132. for (cmTargets::iterator ti = targets.begin();
  133. ti != targets.end(); ti++)
  134. {
  135. switch(ti->second.GetType())
  136. {
  137. case cmTarget::GLOBAL_TARGET:
  138. if ((ti->first=="install") && (installTargetCreated==false))
  139. {
  140. installTargetCreated=true;
  141. }
  142. else if ((ti->first=="package") && (packageTargetCreated==false))
  143. {
  144. packageTargetCreated=true;
  145. }
  146. else if ((ti->first=="test") && (testTargetCreated==false))
  147. {
  148. testTargetCreated=true;
  149. }
  150. else
  151. {
  152. break;
  153. }
  154. case cmTarget::EXECUTABLE:
  155. case cmTarget::STATIC_LIBRARY:
  156. case cmTarget::SHARED_LIBRARY:
  157. case cmTarget::MODULE_LIBRARY:
  158. {
  159. int cbTargetType = this->GetCBTargetType(&ti->second);
  160. std::string makefileName = makefile->GetStartOutputDirectory();
  161. makefileName += "/Makefile";
  162. makefileName = cmSystemTools::ConvertToOutputPath(
  163. makefileName.c_str());
  164. fout<<" <Target title=\"" << ti->first << "\">\n"
  165. " <Option output=\"" << ti->second.GetLocation(0) << "\" prefix_auto=\"0\" extension_auto=\"0\" />\n"
  166. " <Option working_dir=\"" << makefile->GetStartOutputDirectory() <<"\" />\n"
  167. " <Option type=\"" << cbTargetType << "\" />\n"
  168. " <Option compiler=\"" << compiler << "\" />\n"
  169. " <Compiler>\n";
  170. // the include directories for this target
  171. const std::vector<std::string>& incDirs =
  172. ti->second.GetMakefile()->GetIncludeDirectories();
  173. for(std::vector<std::string>::const_iterator dirIt=incDirs.begin();
  174. dirIt != incDirs.end();
  175. ++dirIt)
  176. {
  177. fout <<" <Add directory=\"" << dirIt->c_str() << "\" />\n";
  178. }
  179. fout<<" </Compiler>\n"
  180. " <MakeCommands>\n"
  181. " <Build command=\"" << this->BuildMakeCommand(make, makefileName.c_str(), ti->first.c_str()) << "\" />\n"
  182. " <CompileFile command=\"" << this->BuildMakeCommand(make, makefileName.c_str(), "&quot;$file&quot;") << "\" />\n"
  183. " <Clean command=\"" << this->BuildMakeCommand(make, makefileName.c_str(), "clean") << "\" />\n"
  184. " <DistClean command=\"" << this->BuildMakeCommand(make, makefileName.c_str(), "clean") << "\" />\n"
  185. " </MakeCommands>\n"
  186. " </Target>\n";
  187. }
  188. break;
  189. default:
  190. break;
  191. }
  192. }
  193. }
  194. fout<<" </Build>\n";
  195. std::map<std::string, std::string> sourceFiles;
  196. for (std::vector<cmLocalGenerator*>::const_iterator lg=lgs.begin();
  197. lg!=lgs.end(); lg++)
  198. {
  199. cmMakefile* makefile=(*lg)->GetMakefile();
  200. cmTargets& targets=makefile->GetTargets();
  201. for (cmTargets::iterator ti = targets.begin();
  202. ti != targets.end(); ti++)
  203. {
  204. switch(ti->second.GetType())
  205. {
  206. case cmTarget::EXECUTABLE:
  207. case cmTarget::STATIC_LIBRARY:
  208. case cmTarget::SHARED_LIBRARY:
  209. case cmTarget::MODULE_LIBRARY:
  210. {
  211. const std::vector<cmSourceFile*>&sources=ti->second.GetSourceFiles();
  212. for (std::vector<cmSourceFile*>::const_iterator si=sources.begin();
  213. si!=sources.end(); si++)
  214. {
  215. sourceFiles[(*si)->GetFullPath()] = ti->first;
  216. }
  217. }
  218. default: // intended fallthrough
  219. break;
  220. }
  221. }
  222. }
  223. for (std::map<std::string, std::string>::const_iterator
  224. sit=sourceFiles.begin();
  225. sit!=sourceFiles.end();
  226. ++sit)
  227. {
  228. fout<<" <Unit filename=\""<<sit->first <<"\">\n"
  229. " </Unit>\n";
  230. }
  231. fout<<" </Project>\n"
  232. "</CodeBlocks_project_file>\n";
  233. }
  234. std::string cmExtraCodeBlocksGenerator::GetCBCompilerId(const cmMakefile* mf)
  235. {
  236. // figure out which language to use
  237. // for now care only for C and C++
  238. std::string compilerIdVar = "CMAKE_CXX_COMPILER_ID";
  239. if (this->GlobalGenerator->GetLanguageEnabled("CXX") == false)
  240. {
  241. compilerIdVar = "CMAKE_C_COMPILER_ID";
  242. }
  243. std::string hostSystemName = mf->GetSafeDefinition("CMAKE_HOST_SYSTEM_NAME");
  244. std::string systemName = mf->GetSafeDefinition("CMAKE_SYSTEM_NAME");
  245. std::string compilerId = mf->GetRequiredDefinition(compilerIdVar.c_str());
  246. std::string compiler = "gcc";
  247. if (compilerId == "MSVC")
  248. {
  249. compiler = "msvc";
  250. }
  251. else if (compilerId == "Borland")
  252. {
  253. compiler = "bcc";
  254. }
  255. else if (compilerId == "SDCC")
  256. {
  257. compiler = "sdcc";
  258. }
  259. else if (compilerId == "Intel")
  260. {
  261. compiler = "icc";
  262. }
  263. else if (compilerId == "Watcom")
  264. {
  265. compiler = "ow";
  266. }
  267. else if (compilerId == "GNU")
  268. {
  269. compiler = "gcc";
  270. }
  271. return compiler;
  272. }
  273. int cmExtraCodeBlocksGenerator::GetCBTargetType(cmTarget* target)
  274. {
  275. if ( target->GetType()==cmTarget::EXECUTABLE)
  276. {
  277. if ((target->GetPropertyAsBool("WIN32_EXECUTABLE"))
  278. || (target->GetPropertyAsBool("MACOSX_BUNDLE")))
  279. {
  280. return 0;
  281. }
  282. else
  283. {
  284. return 1;
  285. }
  286. }
  287. else if ( target->GetType()==cmTarget::STATIC_LIBRARY)
  288. {
  289. return 2;
  290. }
  291. else if ((target->GetType()==cmTarget::SHARED_LIBRARY)
  292. || (target->GetType()==cmTarget::MODULE_LIBRARY))
  293. {
  294. return 3;
  295. }
  296. return 4;
  297. }
  298. std::string cmExtraCodeBlocksGenerator::BuildMakeCommand(
  299. const std::string& make, const char* makefile, const char* target)
  300. {
  301. std::string command = make;
  302. if (strcmp(this->GlobalGenerator->GetName(), "NMake Makefiles")==0)
  303. {
  304. command += " /NOLOGO /f &quot;";
  305. command += makefile;
  306. command += "&quot; ";
  307. command += target;
  308. }
  309. else
  310. {
  311. command += " -f &quot;";
  312. command += makefile;
  313. command += "&quot; ";
  314. command += target;
  315. }
  316. return command;
  317. }