cmExtraCodeBlocksGenerator.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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 preinstallTargetCreated = false;
  125. bool installTargetCreated = false;
  126. bool installStripTargetCreated = false;
  127. bool testTargetCreated = false;
  128. bool experimentalTargetCreated = false;
  129. bool nightlyTargetCreated = false;
  130. bool packageTargetCreated = false;
  131. bool packageSourceTargetCreated = false;
  132. bool rebuildCacheTargetCreated = false;
  133. // add all executable and library targets and some of the GLOBAL targets
  134. for (std::vector<cmLocalGenerator*>::const_iterator lg=lgs.begin();
  135. lg!=lgs.end(); lg++)
  136. {
  137. cmMakefile* makefile=(*lg)->GetMakefile();
  138. cmTargets& targets=makefile->GetTargets();
  139. for (cmTargets::iterator ti = targets.begin();
  140. ti != targets.end(); ti++)
  141. {
  142. switch(ti->second.GetType())
  143. {
  144. case cmTarget::GLOBAL_TARGET:
  145. // only add these global targets once
  146. if ((ti->first=="preinstall") && (preinstallTargetCreated==false))
  147. {
  148. preinstallTargetCreated=true;
  149. }
  150. else if ((ti->first=="install") && (installTargetCreated==false))
  151. {
  152. installTargetCreated=true;
  153. }
  154. else if ((ti->first=="install/strip")
  155. && (installStripTargetCreated==false))
  156. {
  157. installStripTargetCreated=true;
  158. }
  159. else if ((ti->first=="test") && (testTargetCreated==false))
  160. {
  161. testTargetCreated=true;
  162. }
  163. else if ((ti->first=="Experimental")
  164. && (experimentalTargetCreated==false))
  165. {
  166. experimentalTargetCreated=true;
  167. }
  168. else if ((ti->first=="Nightly") && (nightlyTargetCreated==false))
  169. {
  170. nightlyTargetCreated=true;
  171. }
  172. else if ((ti->first=="package") && (packageTargetCreated==false))
  173. {
  174. packageTargetCreated=true;
  175. }
  176. else if ((ti->first=="package_source")
  177. && (packageSourceTargetCreated==false))
  178. {
  179. packageSourceTargetCreated=true;
  180. }
  181. else if ((ti->first=="rebuild_cache")
  182. && (rebuildCacheTargetCreated==false))
  183. {
  184. rebuildCacheTargetCreated=true;
  185. }
  186. else
  187. {
  188. break;
  189. }
  190. case cmTarget::EXECUTABLE:
  191. case cmTarget::STATIC_LIBRARY:
  192. case cmTarget::SHARED_LIBRARY:
  193. case cmTarget::MODULE_LIBRARY:
  194. {
  195. int cbTargetType = this->GetCBTargetType(&ti->second);
  196. std::string makefileName = makefile->GetStartOutputDirectory();
  197. makefileName += "/Makefile";
  198. makefileName = cmSystemTools::ConvertToOutputPath(
  199. makefileName.c_str());
  200. fout<<" <Target title=\"" << ti->first << "\">\n"
  201. " <Option output=\"" << ti->second.GetLocation(0)
  202. << "\" prefix_auto=\"0\" extension_auto=\"0\" />\n"
  203. " <Option working_dir=\"" <<makefile->GetStartOutputDirectory()
  204. <<"\" />\n"
  205. " <Option object_output=\"./\" />\n"
  206. " <Option type=\"" << cbTargetType << "\" />\n"
  207. " <Option compiler=\"" << compiler << "\" />\n"
  208. " <Compiler>\n";
  209. // the include directories for this target
  210. const std::vector<std::string>& incDirs =
  211. ti->second.GetMakefile()->GetIncludeDirectories();
  212. for(std::vector<std::string>::const_iterator dirIt=incDirs.begin();
  213. dirIt != incDirs.end();
  214. ++dirIt)
  215. {
  216. fout <<" <Add directory=\"" << dirIt->c_str() << "\" />\n";
  217. }
  218. fout<<" </Compiler>\n"
  219. " <MakeCommands>\n"
  220. " <Build command=\""
  221. << this->BuildMakeCommand(make, makefileName.c_str(), ti->first.c_str())
  222. << "\" />\n"
  223. " <CompileFile command=\""
  224. << this->BuildMakeCommand(make, makefileName.c_str(),"&quot;$file&quot;")
  225. << "\" />\n"
  226. " <Clean command=\""
  227. << this->BuildMakeCommand(make, makefileName.c_str(), "clean")
  228. << "\" />\n"
  229. " <DistClean command=\""
  230. << this->BuildMakeCommand(make, makefileName.c_str(), "clean")
  231. << "\" />\n"
  232. " </MakeCommands>\n"
  233. " </Target>\n";
  234. }
  235. break;
  236. // ignore these:
  237. case cmTarget::UTILITY:
  238. case cmTarget::INSTALL_FILES:
  239. case cmTarget::INSTALL_PROGRAMS:
  240. case cmTarget::INSTALL_DIRECTORY:
  241. default:
  242. break;
  243. }
  244. }
  245. }
  246. fout<<" </Build>\n";
  247. // Collect all used source files in the project
  248. std::map<std::string, std::string> sourceFiles;
  249. for (std::vector<cmLocalGenerator*>::const_iterator lg=lgs.begin();
  250. lg!=lgs.end(); lg++)
  251. {
  252. cmMakefile* makefile=(*lg)->GetMakefile();
  253. cmTargets& targets=makefile->GetTargets();
  254. for (cmTargets::iterator ti = targets.begin();
  255. ti != targets.end(); ti++)
  256. {
  257. switch(ti->second.GetType())
  258. {
  259. case cmTarget::EXECUTABLE:
  260. case cmTarget::STATIC_LIBRARY:
  261. case cmTarget::SHARED_LIBRARY:
  262. case cmTarget::MODULE_LIBRARY:
  263. {
  264. const std::vector<cmSourceFile*>&sources=ti->second.GetSourceFiles();
  265. for (std::vector<cmSourceFile*>::const_iterator si=sources.begin();
  266. si!=sources.end(); si++)
  267. {
  268. sourceFiles[(*si)->GetFullPath()] = ti->first;
  269. }
  270. }
  271. default: // intended fallthrough
  272. break;
  273. }
  274. }
  275. }
  276. // insert all used source files in the CodeBlocks project
  277. for (std::map<std::string, std::string>::const_iterator
  278. sit=sourceFiles.begin();
  279. sit!=sourceFiles.end();
  280. ++sit)
  281. {
  282. fout<<" <Unit filename=\""<<sit->first <<"\">\n"
  283. " </Unit>\n";
  284. }
  285. fout<<" </Project>\n"
  286. "</CodeBlocks_project_file>\n";
  287. }
  288. // Translate the cmake compiler id into the CodeBlocks compiler id
  289. std::string cmExtraCodeBlocksGenerator::GetCBCompilerId(const cmMakefile* mf)
  290. {
  291. // figure out which language to use
  292. // for now care only for C and C++
  293. std::string compilerIdVar = "CMAKE_CXX_COMPILER_ID";
  294. if (this->GlobalGenerator->GetLanguageEnabled("CXX") == false)
  295. {
  296. compilerIdVar = "CMAKE_C_COMPILER_ID";
  297. }
  298. std::string hostSystemName = mf->GetSafeDefinition("CMAKE_HOST_SYSTEM_NAME");
  299. std::string systemName = mf->GetSafeDefinition("CMAKE_SYSTEM_NAME");
  300. std::string compilerId = mf->GetRequiredDefinition(compilerIdVar.c_str());
  301. std::string compiler = "gcc";
  302. if (compilerId == "MSVC")
  303. {
  304. compiler = "msvc";
  305. }
  306. else if (compilerId == "Borland")
  307. {
  308. compiler = "bcc";
  309. }
  310. else if (compilerId == "SDCC")
  311. {
  312. compiler = "sdcc";
  313. }
  314. else if (compilerId == "Intel")
  315. {
  316. compiler = "icc";
  317. }
  318. else if (compilerId == "Watcom")
  319. {
  320. compiler = "ow";
  321. }
  322. else if (compilerId == "GNU")
  323. {
  324. compiler = "gcc";
  325. }
  326. return compiler;
  327. }
  328. // Translate the cmake target type into the CodeBlocks target type id
  329. int cmExtraCodeBlocksGenerator::GetCBTargetType(cmTarget* target)
  330. {
  331. if ( target->GetType()==cmTarget::EXECUTABLE)
  332. {
  333. if ((target->GetPropertyAsBool("WIN32_EXECUTABLE"))
  334. || (target->GetPropertyAsBool("MACOSX_BUNDLE")))
  335. {
  336. return 0;
  337. }
  338. else
  339. {
  340. return 1;
  341. }
  342. }
  343. else if ( target->GetType()==cmTarget::STATIC_LIBRARY)
  344. {
  345. return 2;
  346. }
  347. else if ((target->GetType()==cmTarget::SHARED_LIBRARY)
  348. || (target->GetType()==cmTarget::MODULE_LIBRARY))
  349. {
  350. return 3;
  351. }
  352. return 4;
  353. }
  354. // Create the command line for building the given target using the selected
  355. // make
  356. std::string cmExtraCodeBlocksGenerator::BuildMakeCommand(
  357. const std::string& make, const char* makefile, const char* target)
  358. {
  359. std::string command = make;
  360. if (strcmp(this->GlobalGenerator->GetName(), "NMake Makefiles")==0)
  361. {
  362. command += " /NOLOGO /f &quot;";
  363. command += makefile;
  364. command += "&quot; ";
  365. command += target;
  366. }
  367. else
  368. {
  369. command += " -f &quot;";
  370. command += makefile;
  371. command += "&quot; ";
  372. command += target;
  373. }
  374. return command;
  375. }